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
0ac7f648d71500c3721a5cdfb1e608519e602363
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/Elab/Tactic/BuiltinTactic.lean
9cba9ec60364127394732ed37e006bb7ceda2ffd
[ "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
14,866
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.Tactic.Assumption import Lean.Meta.Tactic.Contradiction import Lean.Meta.Tactic.Refl import Lean.Elab.Binders import Lean.Elab.Open import Lean.Elab.SetOption import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.ElabTerm namespace Lean.Elab.Tactic open Meta open Parser.Tactic @[builtinTactic withAnnotateState] def evalWithAnnotateState : Tactic | `(tactic| with_annotate_state $stx $t) => withTacticInfoContext stx (evalTactic t) | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.«done»] def evalDone : Tactic := fun _ => done @[builtinTactic seq1] def evalSeq1 : Tactic := fun stx => do let args := stx[0].getArgs for i in [:args.size] do if i % 2 == 0 then evalTactic args[i]! else saveTacticInfoForToken args[i]! -- add `TacticInfo` node for `;` @[builtinTactic paren] def evalParen : Tactic := fun stx => evalTactic stx[1] def isCheckpointableTactic (arg : Syntax) : TacticM Bool := do -- TODO: make it parametric let kind := arg.getKind return kind == ``Lean.Parser.Tactic.save /-- Takes a `sepByIndent tactic "; "`, and inserts `checkpoint` blocks for `save` tactics. Input: ``` a b save c d save e ``` Output: ``` checkpoint a b save checkpoint c d save e ``` -/ -- Note that we need to preserve the separators to show the right goals after semicolons. def addCheckpoints (stx : Syntax) : TacticM Syntax := do -- if (← readThe Term.Context).tacticCache? |>.isSome then if !(← stx.getSepArgs.anyM isCheckpointableTactic) then return stx let mut currentCheckpointBlock := #[] let mut output := #[] for i in [:stx.getArgs.size / 2] do let tac := stx[2*i] let sep? := stx.getArgs[2*i+1]? if (← isCheckpointableTactic tac) then let checkpoint : Syntax := mkNode ``checkpoint #[ mkAtomFrom tac "checkpoint", mkNode ``tacticSeq #[ mkNode ``tacticSeq1Indented #[ -- HACK: null node is not a valid tactic, but prevents infinite loop mkNullNode (currentCheckpointBlock.push tac) ] ] ] currentCheckpointBlock := #[] output := output.push checkpoint if let some sep := sep? then output := output.push sep else currentCheckpointBlock := currentCheckpointBlock.push tac if let some sep := sep? then currentCheckpointBlock := currentCheckpointBlock.push sep output := output ++ currentCheckpointBlock return stx.setArgs output /-- Evaluate `sepByIndent tactic "; " -/ def evalSepByIndentTactic (stx : Syntax) : TacticM Unit := do let stx ← addCheckpoints stx for arg in stx.getArgs, i in [:stx.getArgs.size] do if i % 2 == 0 then evalTactic arg else saveTacticInfoForToken arg @[builtinTactic tacticSeq1Indented] def evalTacticSeq1Indented : Tactic := fun stx => evalSepByIndentTactic stx[0] @[builtinTactic tacticSeqBracketed] def evalTacticSeqBracketed : Tactic := fun stx => do let initInfo ← mkInitialTacticInfo stx[0] withRef stx[2] <| closeUsingOrAdmit do -- save state before/after entering focus on `{` withInfoContext (pure ()) initInfo evalSepByIndentTactic stx[1] @[builtinTactic Parser.Tactic.focus] def evalFocus : Tactic := fun stx => do let mkInfo ← mkInitialTacticInfo stx[0] focus do -- show focused state on `focus` withInfoContext (pure ()) mkInfo evalTactic stx[1] private def getOptRotation (stx : Syntax) : Nat := if stx.isNone then 1 else stx[0].toNat @[builtinTactic Parser.Tactic.rotateLeft] def evalRotateLeft : Tactic := fun stx => do let n := getOptRotation stx[1] setGoals <| (← getGoals).rotateLeft n @[builtinTactic Parser.Tactic.rotateRight] def evalRotateRight : Tactic := fun stx => do let n := getOptRotation stx[1] setGoals <| (← getGoals).rotateRight n @[builtinTactic Parser.Tactic.open] def evalOpen : Tactic := fun stx => do let `(tactic| open $decl in $tac) := stx | throwUnsupportedSyntax try pushScope let openDecls ← elabOpenDecl decl withTheReader Core.Context (fun ctx => { ctx with openDecls := openDecls }) do evalTactic tac finally popScope @[builtinTactic Parser.Tactic.set_option] def elabSetOption : Tactic := fun stx => do let options ← Elab.elabSetOption stx[1] stx[2] withTheReader Core.Context (fun ctx => { ctx with maxRecDepth := maxRecDepth.get options, options := options }) do evalTactic stx[4] @[builtinTactic Parser.Tactic.allGoals] def evalAllGoals : Tactic := fun stx => do let mvarIds ← getGoals let mut mvarIdsNew := #[] for mvarId in mvarIds do unless (← mvarId.isAssigned) do setGoals [mvarId] try evalTactic stx[1] mvarIdsNew := mvarIdsNew ++ (← getUnsolvedGoals) catch ex => if (← read).recover then logException ex mvarIdsNew := mvarIdsNew.push mvarId else throw ex setGoals mvarIdsNew.toList @[builtinTactic Parser.Tactic.anyGoals] def evalAnyGoals : Tactic := fun stx => do let mvarIds ← getGoals let mut mvarIdsNew := #[] let mut succeeded := false for mvarId in mvarIds do unless (← mvarId.isAssigned) do setGoals [mvarId] try evalTactic stx[1] mvarIdsNew := mvarIdsNew ++ (← getUnsolvedGoals) succeeded := true catch _ => mvarIdsNew := mvarIdsNew.push mvarId unless succeeded do throwError "failed on all goals" setGoals mvarIdsNew.toList @[builtinTactic tacticSeq] def evalTacticSeq : Tactic := fun stx => evalTactic stx[0] partial def evalChoiceAux (tactics : Array Syntax) (i : Nat) : TacticM Unit := if h : i < tactics.size then let tactic := tactics.get ⟨i, h⟩ catchInternalId unsupportedSyntaxExceptionId (evalTactic tactic) (fun _ => evalChoiceAux tactics (i+1)) else throwUnsupportedSyntax @[builtinTactic choice] def evalChoice : Tactic := fun stx => evalChoiceAux stx.getArgs 0 @[builtinTactic skip] def evalSkip : Tactic := fun _ => pure () @[builtinTactic unknown] def evalUnknown : Tactic := fun stx => do addCompletionInfo <| CompletionInfo.tactic stx (← getGoals) @[builtinTactic failIfSuccess] def evalFailIfSuccess : Tactic := fun stx => Term.withoutErrToSorry <| withoutRecover do let tactic := stx[1] if (← try evalTactic tactic; pure true catch _ => pure false) then throwError "tactic succeeded" @[builtinTactic traceState] def evalTraceState : Tactic := fun _ => do let gs ← getUnsolvedGoals withPPForTacticGoal <| addRawTrace (goalsToMessageData gs) @[builtinTactic traceMessage] def evalTraceMessage : Tactic := fun stx => do match stx[1].isStrLit? with | none => throwIllFormedSyntax | some msg => withRef stx[0] <| addRawTrace msg @[builtinTactic Lean.Parser.Tactic.assumption] def evalAssumption : Tactic := fun _ => liftMetaTactic fun mvarId => do mvarId.assumption; pure [] @[builtinTactic Lean.Parser.Tactic.contradiction] def evalContradiction : Tactic := fun _ => liftMetaTactic fun mvarId => do mvarId.contradiction; pure [] @[builtinTactic Lean.Parser.Tactic.refl] def evalRefl : Tactic := fun _ => liftMetaTactic fun mvarId => do mvarId.refl; pure [] @[builtinTactic Lean.Parser.Tactic.intro] def evalIntro : Tactic := fun stx => do match stx with | `(tactic| intro) => introStep none `_ | `(tactic| intro $h:ident) => introStep h h.getId | `(tactic| intro _%$tk) => introStep tk `_ | `(tactic| intro $pat:term) => evalTactic (← `(tactic| intro h; match h with | $pat:term => ?_; try clear h)) | `(tactic| intro $h:term $hs:term*) => evalTactic (← `(tactic| intro $h:term; intro $hs:term*)) | _ => throwUnsupportedSyntax where introStep (ref : Option Syntax) (n : Name) : TacticM Unit := do let fvar ← liftMetaTacticAux fun mvarId => do let (fvar, mvarId) ← mvarId.intro n pure (fvar, [mvarId]) if let some stx := ref then withMainContext do Term.addLocalVarInfo stx (mkFVar fvar) @[builtinTactic Lean.Parser.Tactic.introMatch] def evalIntroMatch : Tactic := fun stx => do let matchAlts := stx[1] let stxNew ← liftMacroM <| Term.expandMatchAltsIntoMatchTactic stx matchAlts withMacroExpansion stx stxNew <| evalTactic stxNew @[builtinTactic «intros»] def evalIntros : Tactic := fun stx => match stx with | `(tactic| intros) => liftMetaTactic fun mvarId => do let (_, mvarId) ← mvarId.intros return [mvarId] | `(tactic| intros $ids*) => do let fvars ← liftMetaTacticAux fun mvarId => do let (fvars, mvarId) ← mvarId.introN ids.size (ids.map getNameOfIdent').toList return (fvars, [mvarId]) withMainContext do for stx in ids, fvar in fvars do Term.addLocalVarInfo stx (mkFVar fvar) | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.revert] def evalRevert : Tactic := fun stx => match stx with | `(tactic| revert $hs*) => do let (_, mvarId) ← (← getMainGoal).revert (← getFVarIds hs) replaceMainGoal [mvarId] | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.clear] def evalClear : Tactic := fun stx => match stx with | `(tactic| clear $hs*) => do let fvarIds ← getFVarIds hs let fvarIds ← withMainContext <| sortFVarIds fvarIds for fvarId in fvarIds.reverse do withMainContext do let mvarId ← (← getMainGoal).clear fvarId replaceMainGoal [mvarId] | _ => throwUnsupportedSyntax def forEachVar (hs : Array Syntax) (tac : MVarId → FVarId → MetaM MVarId) : TacticM Unit := do for h in hs do withMainContext do let fvarId ← getFVarId h let mvarId ← tac (← getMainGoal) fvarId replaceMainGoal [mvarId] @[builtinTactic Lean.Parser.Tactic.subst] def evalSubst : Tactic := fun stx => match stx with | `(tactic| subst $hs*) => forEachVar hs Meta.subst | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.substVars] def evalSubstVars : Tactic := fun _ => liftMetaTactic fun mvarId => return [← substVars mvarId] /-- Searches for a metavariable `g` s.t. `tag` is its exact name. If none then searches for a metavariable `g` s.t. `tag` is a suffix of its name. If none, then it searches for a metavariable `g` s.t. `tag` is a prefix of its name. -/ private def findTag? (mvarIds : List MVarId) (tag : Name) : TacticM (Option MVarId) := do match (← mvarIds.findM? fun mvarId => return tag == (← mvarId.getDecl).userName) with | some mvarId => return mvarId | none => match (← mvarIds.findM? fun mvarId => return tag.isSuffixOf (← mvarId.getDecl).userName) with | some mvarId => return mvarId | none => mvarIds.findM? fun mvarId => return tag.isPrefixOf (← mvarId.getDecl).userName def renameInaccessibles (mvarId : MVarId) (hs : TSyntaxArray ``binderIdent) : TacticM MVarId := do if hs.isEmpty then return mvarId else let mvarDecl ← mvarId.getDecl let mut lctx := mvarDecl.lctx let mut hs := hs let mut info := #[] let mut found : NameSet := {} let n := lctx.numIndices for i in [:n] do let j := n - i - 1 match lctx.getAt? j with | none => pure () | some localDecl => if localDecl.userName.hasMacroScopes || found.contains localDecl.userName then if let `(binderIdent| $h:ident) := hs.back then let newName := h.getId lctx := lctx.setUserName localDecl.fvarId newName info := info.push (localDecl.fvarId, h) hs := hs.pop if hs.isEmpty then break found := found.insert localDecl.userName unless hs.isEmpty do logError m!"too many variable names provided" let mvarNew ← mkFreshExprMVarAt lctx mvarDecl.localInstances mvarDecl.type MetavarKind.syntheticOpaque mvarDecl.userName withSaveInfoContext <| mvarNew.mvarId!.withContext do for (fvarId, stx) in info do Term.addLocalVarInfo stx (mkFVar fvarId) mvarId.assign mvarNew return mvarNew.mvarId! private def getCaseGoals (tag : TSyntax ``binderIdent) : TacticM (MVarId × List MVarId) := do let gs ← getUnsolvedGoals let g ← if let `(binderIdent| $tag:ident) := tag then let tag := tag.getId let some g ← findTag? gs tag | throwError "tag not found" pure g else getMainGoal return (g, gs.erase g) @[builtinTactic «case»] def evalCase : Tactic | stx@`(tactic| case $[$tag $hs*]|* =>%$arr $tac:tacticSeq) => for tag in tag, hs in hs do let (g, gs) ← getCaseGoals tag let g ← renameInaccessibles g hs setGoals [g] g.setTag Name.anonymous withCaseRef arr tac do closeUsingOrAdmit (withTacticInfoContext stx (evalTactic tac)) setGoals gs | _ => throwUnsupportedSyntax @[builtinTactic «case'»] def evalCase' : Tactic | `(tactic| case' $[$tag $hs*]|* =>%$arr $tac:tacticSeq) => do let mut acc := #[] for tag in tag, hs in hs do let (g, gs) ← getCaseGoals tag let g ← renameInaccessibles g hs let mvarTag ← g.getTag setGoals [g] withCaseRef arr tac (evalTactic tac) let gs' ← getUnsolvedGoals if let [g'] := gs' then g'.setTag mvarTag acc := acc ++ gs' setGoals gs setGoals (acc.toList ++ (← getGoals)) | _ => throwUnsupportedSyntax @[builtinTactic «renameI»] def evalRenameInaccessibles : Tactic | `(tactic| rename_i $hs*) => do replaceMainGoal [← renameInaccessibles (← getMainGoal) hs] | _ => throwUnsupportedSyntax @[builtinTactic «first»] partial def evalFirst : Tactic := fun stx => do let tacs := stx[1].getArgs if tacs.isEmpty then throwUnsupportedSyntax loop tacs 0 where loop (tacs : Array Syntax) (i : Nat) := if i == tacs.size - 1 then evalTactic tacs[i]![1] else evalTactic tacs[i]![1] <|> loop tacs (i+1) @[builtinTactic «fail»] def evalFail : Tactic := fun stx => do let goals ← getGoals let goalsMsg := MessageData.joinSep (goals.map MessageData.ofGoal) m!"\n\n" match stx with | `(tactic| fail) => throwError "tactic 'fail' failed\n{goalsMsg}" | `(tactic| fail $msg:str) => throwError "{msg.getString}\n{goalsMsg}" | _ => throwUnsupportedSyntax @[builtinTactic dbgTrace] def evalDbgTrace : Tactic := fun stx => do match stx[1].isStrLit? with | none => throwIllFormedSyntax | some msg => dbg_trace msg @[builtinTactic sleep] def evalSleep : Tactic := fun stx => do match stx[1].isNatLit? with | none => throwIllFormedSyntax | some ms => IO.sleep ms.toUInt32 end Lean.Elab.Tactic
ce1022d78e3c0eabfab76eb17fe78e2b737930c0
9338c56dfd6ceacc3e5e63e32a7918cfec5d5c69
/src/sheaves/covering/covering.lean
bba527658e6d1afcf05a9a549c8585facaa20ede
[]
no_license
Project-Reykjavik/lean-scheme
7322eefce504898ba33737970be89dc751108e2b
6d3ec18fecfd174b79d0ce5c85a783f326dd50f6
refs/heads/master
1,669,426,172,632
1,578,284,588,000
1,578,284,588,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,240
lean
/- Package the definition of an open cover of an open set. -/ import topology.basic import topology.opens import to_mathlib.opens universes u open topological_space lattice section covering variables {α : Type u} [topological_space α] -- Open cover. structure covering (U : opens α) := {γ : Type u} (Uis : γ → opens α) (Hcov : ⋃ Uis = U) variable (α) def covering.univ := covering (@opens.univ α _) variable {α} -- If ⋃ Ui = U then for all i, Ui ⊆ U. lemma subset_covering {U : opens α} {OC : covering U} : ∀ i, OC.Uis i ⊆ U := λ i x Hx, OC.Hcov ▸ opens_supr_mem OC.Uis i x Hx -- Make covering from standard definition. Used for instance in compactness. def opens.from_sets {A : Type*} [topological_space A] : set (set A) → set (opens A) := λ C, { x | x.1 ∈ C } lemma opens.from_sets.eq {A : Type*} [topological_space A] (S : set (set A)) (HS : ∀ (t : set A), t ∈ S → is_open t) : subtype.val '' (opens.from_sets S) = S := set.ext $ λ x, ⟨ λ ⟨x', Hx', Hval⟩, Hval ▸ Hx', λ Hx, by simp [HS x Hx]; by exact Hx⟩ @[reducible] def covering.from_cover {A : Type*} [topological_space A] (U : opens A) (C : set (set A)) (HC : ∀ (t : set A), t ∈ C → is_open t) (Hcov : U.1 = ⋃₀ C) : covering U := { γ := opens.from_sets C, Uis := λ x, x, Hcov := begin apply subtype.ext.2, rw Hcov, apply set.ext, intros x, split, { intros Hx, rcases Hx with ⟨U, HU, HxU⟩, existsi U, simp at HU, rcases HU with ⟨OU, HU⟩, rw ←opens.from_sets.eq C HC, split, { simp [HU], use OU, }, { exact HxU, } }, { intros Hx, rcases Hx with ⟨U, HU, HxU⟩, use U, simp, use (HC U HU), { simp [opens.from_sets], exact HU, }, { exact HxU, } } end, } lemma covering.from_cover.Uis {A : Type*} [topological_space A] (U : opens A) (C : set (set A)) (HC : ∀ (t : set A), t ∈ C → is_open t) (Hcov : U.1 = ⋃₀ C) : ∀ i, ((covering.from_cover U C HC Hcov).Uis i).1 ∈ C := begin intros i, simp [covering.from_cover] at *, cases i with i Hi, simp, simp [opens.from_sets] at *, exact Hi, end -- TODO -- should be in mathlib? lemma opens.supr_val {X γ : Type*} [topological_space X] (ι : γ → opens X) : (⨆ i, ι i).val = ⨆ i, (ι i).val := @galois_connection.l_supr (opens X) (set X) _ _ _ (subtype.val : opens X → set X) opens.interior opens.gc _ --lemma opens.supr_comap_val {X Y : Type*} [topological_space X] [topological_space Y] {f : X → Y} -- (hf : continuous f) {γ : Type*} -- (ι : γ → opens Y) : --(⨆ (j : γ), hf.comap (ι j)).val = set.Union (λ (j : γ), f ⁻¹' (ι j).val) := --by simp [set.ext_iff, opens.supr_val, continuous.comap] /-- pullback of a covering is a covering -/ def covering.comap {X Y : Type*} [topological_space X] [topological_space Y] {U : opens Y} (OC : covering U) {f : X → Y} (hf : continuous f) : covering (hf.comap U) := { γ := OC.γ, Uis := λ i, hf.comap $ OC.Uis i, Hcov := by simp [subtype.ext, continuous.comap, (subtype.ext.1 OC.Hcov).symm, opens.supr_val] } end covering
844c9db9581dc9a84c9d8ebc72b1c20dba940f0d
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Server/Rpc/RequestHandling.lean
2c7293329f764bb41ad245540d702cf89c8a4c3e
[ "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
5,737
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.Data.Lsp.Extra import Lean.Server.Requests import Lean.Server.Rpc.Basic namespace Lean.Server private structure RpcProcedure where wrapper : (sessionId : UInt64) → Json → RequestM (RequestTask Json) deriving Inhabited /- We store the builtin RPC handlers in a Ref and users' handlers in an extension. This ensures that users don't need to import core Lean modules to make builtin handlers work, but also that they *can* easily create custom handlers and use them in the same file. -/ builtin_initialize builtinRpcProcedures : IO.Ref (PHashMap Name RpcProcedure) ← IO.mkRef {} builtin_initialize userRpcProcedures : MapDeclarationExtension Name ← mkMapDeclarationExtension private unsafe def evalRpcProcedureUnsafe (env : Environment) (opts : Options) (procName : Name) : Except String RpcProcedure := env.evalConstCheck RpcProcedure opts ``RpcProcedure procName @[implemented_by evalRpcProcedureUnsafe] opaque evalRpcProcedure (env : Environment) (opts : Options) (procName : Name) : Except String RpcProcedure open RequestM in def handleRpcCall (p : Lsp.RpcCallParams) : RequestM (RequestTask Json) := do -- The imports are finished at this point, because the handleRequest function -- waits for the header. (Therefore the built-in RPC procedures won't change -- if we wait for further snapshots.) if let some proc := (← builtinRpcProcedures.get).find? p.method then proc.wrapper p.sessionId p.params else let doc ← readDoc let text := doc.meta.text let callPos := text.lspPosToUtf8Pos p.position let throwNotFound := throwThe RequestError { code := .methodNotFound message := s!"No RPC method '{p.method}' found"} bindWaitFindSnap doc (notFoundX := throwNotFound) (fun s => s.endPos >= callPos || (userRpcProcedures.find? s.env p.method).isSome) fun snap => do if let some procName := userRpcProcedures.find? snap.env p.method then let options := snap.cmdState.scopes.head!.opts match evalRpcProcedure snap.env options procName with | .ok x => x.wrapper p.sessionId p.params | .error e => throwThe RequestError { code := .internalError message := s!"Failed to evaluate RPC constant '{procName}': {e}" } else throwNotFound builtin_initialize registerLspRequestHandler "$/lean/rpc/call" Lsp.RpcCallParams Json handleRpcCall def wrapRpcProcedure (method : Name) paramType respType [RpcEncodable paramType] [RpcEncodable respType] (handler : paramType → RequestM (RequestTask respType)) : RpcProcedure := ⟨fun seshId j => do let rc ← read let some seshRef := rc.rpcSessions.find? seshId | throwThe RequestError { code := JsonRpc.ErrorCode.rpcNeedsReconnect message := s!"Outdated RPC session" } let t ← RequestM.asTask do match rpcDecode j (← seshRef.get).objects with | Except.ok v => return v | Except.error e => throwThe RequestError { code := JsonRpc.ErrorCode.invalidParams message := s!"Cannot decode params in RPC call '{method}({j.compress})'\n{e}" } let t ← RequestM.bindTask t fun | Except.error e => throw e | Except.ok ps => handler ps RequestM.mapTask t fun | Except.error e => throw e | Except.ok ret => seshRef.modifyGet fun st => rpcEncode ret st.objects |>.map id ({st with objects := ·})⟩ def registerBuiltinRpcProcedure (method : Name) paramType respType [RpcEncodable paramType] [RpcEncodable respType] (handler : paramType → RequestM (RequestTask respType)) : IO Unit := do let errMsg := s!"Failed to register builtin RPC call handler for '{method}'" unless (← initializing) do throw <| IO.userError s!"{errMsg}: only possible during initialization" if (←builtinRpcProcedures.get).contains method then throw <| IO.userError s!"{errMsg}: already registered" let proc := wrapRpcProcedure method paramType respType handler builtinRpcProcedures.modify fun ps => ps.insert method proc open Lean Elab Command Term Meta in def registerRpcProcedure (method : Name) : CoreM Unit := do let env ← getEnv let errMsg := "Failed to register RPC call handler for '{method}'" if (←builtinRpcProcedures.get).contains method then throwError s!"{errMsg}: already registered (builtin)" if userRpcProcedures.contains env method then throwError s!"{errMsg}: already registered" let wrappedName := method ++ `_rpc_wrapped let procT := mkConst ``RpcProcedure let proc ← MetaM.run' <| TermElabM.run' <| withoutErrToSorry do let stx ← ``(wrapRpcProcedure $(quote method) _ _ $(mkIdent method)) let c ← Lean.Elab.Term.elabTerm stx procT instantiateMVars c addAndCompile <| Declaration.defnDecl { name := wrappedName type := procT value := proc safety := DefinitionSafety.safe levelParams := [] hints := ReducibilityHints.opaque } setEnv <| userRpcProcedures.insert (← getEnv) method wrappedName builtin_initialize registerBuiltinAttribute { name := `server_rpc_method descr := "Marks a function as a Lean server RPC method. Shorthand for `registerRpcProcedure`. The function must have type `α → RequestM (RequestTask β)` with `[RpcEncodable α]` and `[RpcEncodable β]`." applicationTime := AttributeApplicationTime.afterCompilation add := fun decl _ _ => registerRpcProcedure decl } end Lean.Server
af4cefed6d288f5938b45d3d17c0fc7345bd5e12
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/linear_algebra/determinant.lean
5ea4f49404df724b10934b9d7c9691c47013c70e
[ "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
8,870
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Tim Baanen -/ import data.matrix.basic import data.matrix.pequiv import data.fintype.card import group_theory.perm.sign import tactic.ring universes u v open equiv equiv.perm finset function namespace matrix open_locale matrix variables {n : Type u} [fintype n] [decidable_eq n] {R : Type v} [comm_ring R] local notation `ε` σ:max := ((sign σ : ℤ ) : R) /-- The determinant of a matrix given by the Leibniz formula. -/ definition det (M : matrix n n R) : R := univ.sum (λ (σ : perm n), ε σ * univ.prod (λ i, M (σ i) i)) @[simp] lemma det_diagonal {d : n → R} : det (diagonal d) = univ.prod d := begin refine (finset.sum_eq_single 1 _ _).trans _, { intros σ h1 h2, cases not_forall.1 (mt (equiv.ext _ _) h2) with x h3, convert ring.mul_zero _, apply finset.prod_eq_zero, { change x ∈ _, simp }, exact if_neg h3 }, { simp }, { simp } end @[simp] lemma det_zero (h : nonempty n) : det (0 : matrix n n R) = 0 := by rw [← diagonal_zero, det_diagonal, finset.prod_const, ← fintype.card, zero_pow (fintype.card_pos_iff.2 h)] @[simp] lemma det_one : det (1 : matrix n n R) = 1 := by rw [← diagonal_one]; simp [-diagonal_one] lemma det_eq_one_of_card_eq_zero {A : matrix n n R} (h : fintype.card n = 0) : det A = 1 := begin have perm_eq : (univ : finset (perm n)) = finset.singleton 1 := univ_eq_singleton_of_card_one (1 : perm n) (by simp [card_univ, fintype.card_perm, h]), simp [det, card_eq_zero.mp h, perm_eq], end lemma det_mul_aux {M N : matrix n n R} {p : n → n} (H : ¬bijective p) : univ.sum (λ σ : perm n, (ε σ) * (univ.prod (λ x, M (σ x) (p x) * N (p x) x))) = 0 := begin obtain ⟨i, j, hpij, hij⟩ : ∃ i j, p i = p j ∧ i ≠ j, { rw [← fintype.injective_iff_bijective, injective] at H, push_neg at H, exact H }, exact sum_involution (λ σ _, σ * swap i j) (λ σ _, have ∀ a, p (swap i j a) = p a := λ a, by simp only [swap_apply_def]; split_ifs; cc, have univ.prod (λ x, M (σ x) (p x)) = univ.prod (λ x, M ((σ * swap i j) x) (p x)), from prod_bij (λ a _, swap i j a) (λ _ _, mem_univ _) (by simp [this]) (λ _ _ _ _ h, (swap i j).injective h) (λ b _, ⟨swap i j b, mem_univ _, by simp⟩), by simp [sign_mul, this, sign_swap hij, prod_mul_distrib]) (λ σ _ _ h, hij (σ.injective $ by conv {to_lhs, rw ← h}; simp)) (λ _ _, mem_univ _) (λ _ _, equiv.ext _ _ $ by simp) end @[simp] lemma det_mul (M N : matrix n n R) : det (M ⬝ N) = det M * det N := calc det (M ⬝ N) = univ.sum (λ p : n → n, univ.sum (λ σ : perm n, ε σ * univ.prod (λ i, M (σ i) (p i) * N (p i) i))) : by simp only [det, mul_val, prod_univ_sum, mul_sum, fintype.pi_finset_univ]; rw [finset.sum_comm] ... = ((@univ (n → n) _).filter bijective).sum (λ p : n → n, univ.sum (λ σ : perm n, ε σ * univ.prod (λ i, M (σ i) (p i) * N (p i) i))) : eq.symm $ sum_subset (filter_subset _) (λ f _ hbij, det_mul_aux $ by simpa using hbij) ... = (@univ (perm n) _).sum (λ τ, univ.sum (λ σ : perm n, ε σ * univ.prod (λ i, M (σ i) (τ i) * N (τ i) i))) : sum_bij (λ p h, equiv.of_bijective (mem_filter.1 h).2) (λ _ _, mem_univ _) (λ _ _, rfl) (λ _ _ _ _ h, by injection h) (λ b _, ⟨b, mem_filter.2 ⟨mem_univ _, b.bijective⟩, eq_of_to_fun_eq rfl⟩) ... = univ.sum (λ σ : perm n, univ.sum (λ τ : perm n, (univ.prod (λ i, N (σ i) i) * ε τ) * univ.prod (λ j, M (τ j) (σ j)))) : by simp [mul_sum, det, mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc] ... = univ.sum (λ σ : perm n, univ.sum (λ τ : perm n, (univ.prod (λ i, N (σ i) i) * (ε σ * ε τ)) * univ.prod (λ i, M (τ i) i))) : sum_congr rfl (λ σ _, sum_bij (λ τ _, τ * σ⁻¹) (λ _ _, mem_univ _) (λ τ _, have univ.prod (λ j, M (τ j) (σ j)) = univ.prod (λ j, M ((τ * σ⁻¹) j) j), by rw prod_univ_perm σ⁻¹; simp [mul_apply], have h : ε σ * ε (τ * σ⁻¹) = ε τ := calc ε σ * ε (τ * σ⁻¹) = ε ((τ * σ⁻¹) * σ) : by rw [mul_comm, sign_mul (τ * σ⁻¹)]; simp [sign_mul] ... = ε τ : by simp, by rw h; simp [this, mul_comm, mul_assoc, mul_left_comm]) (λ _ _ _ _, (mul_right_inj _).1) (λ τ _, ⟨τ * σ, by simp⟩)) ... = det M * det N : by simp [det, mul_assoc, mul_sum, mul_comm, mul_left_comm] instance : is_monoid_hom (det : matrix n n R → R) := { map_one := det_one, map_mul := det_mul } /-- Transposing a matrix preserves the determinant. -/ @[simp] lemma det_transpose (M : matrix n n R) : Mᵀ.det = M.det := begin apply sum_bij (λ σ _, σ⁻¹), { intros σ _, apply mem_univ }, { intros σ _, rw [sign_inv], congr' 1, apply prod_bij (λ i _, σ i), { intros i _, apply mem_univ }, { intros i _, simp }, { intros i j _ _ h, simp at h, assumption }, { intros i _, use σ⁻¹ i, finish } }, { intros σ σ' _ _ h, simp at h, assumption }, { intros σ _, use σ⁻¹, finish } end /-- The determinant of a permutation matrix equals its sign. -/ @[simp] lemma det_permutation (σ : perm n) : matrix.det (σ.to_pequiv.to_matrix : matrix n n R) = σ.sign := begin suffices : matrix.det (σ.to_pequiv.to_matrix) = ↑σ.sign * det (1 : matrix n n R), { simp [this] }, unfold det, rw mul_sum, apply sum_bij (λ τ _, σ * τ), { intros τ _, apply mem_univ }, { intros τ _, conv_lhs { rw [←one_mul (sign τ), ←int.units_pow_two (sign σ)] }, conv_rhs { rw [←mul_assoc, coe_coe, sign_mul, units.coe_mul, int.cast_mul, ←mul_assoc] }, congr, { simp [pow_two] }, { ext i, apply pequiv.equiv_to_pequiv_to_matrix } }, { intros τ τ' _ _, exact (mul_left_inj σ).mp }, { intros τ _, use σ⁻¹ * τ, use (mem_univ _), exact (mul_inv_cancel_left _ _).symm } end /-- Permuting the columns changes the sign of the determinant. -/ lemma det_permute (σ : perm n) (M : matrix n n R) : matrix.det (λ i, M (σ i)) = σ.sign * M.det := by rw [←det_permutation, ←det_mul, pequiv.to_pequiv_mul_matrix] @[simp] lemma det_smul {A : matrix n n R} {c : R} : det (c • A) = c ^ fintype.card n * det A := calc det (c • A) = det (matrix.mul (diagonal (λ _, c)) A) : by rw [smul_eq_diagonal_mul] ... = det (diagonal (λ _, c)) * det A : det_mul _ _ ... = c ^ fintype.card n * det A : by simp [card_univ] section det_zero /-! ### `det_zero` section Prove that a matrix with a repeated column has determinant equal to zero. -/ lemma det_eq_zero_of_column_eq_zero {A : matrix n n R} (i : n) (h : ∀ j, A i j = 0) : det A = 0 := begin rw [←det_transpose, det], convert @sum_const_zero _ _ (univ : finset (perm n)) _, ext σ, convert mul_zero ↑(sign σ), apply prod_eq_zero (mem_univ i), rw [transpose_val], apply h end /-- `mod_swap i j` contains permutations up to swapping `i` and `j`. We use this to partition permutations in the expression for the determinant, such that each partitions sums up to `0`. -/ def mod_swap {n : Type u} [decidable_eq n] (i j : n) : setoid (perm n) := ⟨ λ σ τ, σ = τ ∨ σ = swap i j * τ, λ σ, or.inl (refl σ), λ σ τ h, or.cases_on h (λ h, or.inl h.symm) (λ h, or.inr (by rw [h, swap_mul_self_mul])), λ σ τ υ hστ hτυ, by cases hστ; cases hτυ; try {rw [hστ, hτυ, swap_mul_self_mul]}; finish⟩ instance (i j : n) : decidable_rel (mod_swap i j).r := λ σ τ, or.decidable variables {M : matrix n n R} {i j : n} /-- If a matrix has a repeated column, the determinant will be zero. -/ theorem det_zero_of_column_eq (i_ne_j : i ≠ j) (hij : M i = M j) : M.det = 0 := begin have swap_invariant : ∀ k, M (swap i j k) = M k, { intros k, rw [swap_apply_def], by_cases k = i, { rw [if_pos h, h, ←hij] }, rw [if_neg h], by_cases k = j, { rw [if_pos h, h, hij] }, rw [if_neg h] }, have : ∀ σ, _root_.disjoint (_root_.singleton σ) (_root_.singleton (swap i j * σ)), { intros σ, rw [finset.singleton_eq_singleton, finset.singleton_eq_singleton, disjoint_singleton], apply (not_congr mem_singleton).mpr, exact (not_congr swap_mul_eq_iff).mpr i_ne_j }, apply finset.sum_cancels_of_partition_cancels (mod_swap i j), intros σ _, erw [filter_or, filter_eq', filter_eq', if_pos (mem_univ σ), if_pos (mem_univ (swap i j * σ)), sum_union (this σ), sum_singleton, sum_singleton], convert add_right_neg (↑↑(sign σ) * finset.prod univ (λ (i : n), M (σ i) i)), rw [neg_mul_eq_neg_mul], congr, { rw [sign_mul, sign_swap i_ne_j], norm_num }, ext j, rw [mul_apply, swap_invariant] end end det_zero end matrix
235d4495bb835c5d9202e15bec4c738890fd3918
cc62cd292c1acc80a10b1c645915b70d2cdee661
/src/category_theory/universal/kernels.lean
89706f34099f8e6cbd9494955718dadd51cd8228
[]
no_license
RitaAhmadi/lean-category-theory
4afb881c4b387ee2c8ce706c454fbf9db8897a29
a27b4ae5eac978e9188d2e867c3d11d9a5b87a9e
refs/heads/master
1,651,786,183,402
1,565,604,314,000
1,565,604,314,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,590
lean
-- import category_theory.universal.zero -- import category_theory.limits.equalizers -- import category_theory.over -- open category_theory -- universes u v -- namespace category_theory.limits -- variables {C : Type u} [𝒞 : category.{u v} C] [has_zero_object.{u v} C] -- include 𝒞 -- variables {X Y Z : C} -- structure is_kernel (f : Y ⟶ Z) (ι : X ⟶ Y) := -- (w' : ι ≫ f = zero_morphism _ _) -- (lift : Π {X' : C} {ι' : X' ⟶ Y} (w : ι' ≫ f = zero_morphism X' Z), X' ⟶ X) -- (fac' : Π {X' : C} {ι' : X' ⟶ Y} (w : ι' ≫ f = zero_morphism X' Z), (lift w) ≫ ι = ι' . obviously) -- (uniq' : Π {X' : C} {ι' : X' ⟶ Y} (w : ι' ≫ f = zero_morphism X' Z) {m : X' ⟶ X} (h : m ≫ ι = ι'), m = lift w . obviously) -- restate_axiom is_kernel.w' -- attribute [search] is_kernel.w -- restate_axiom is_kernel.fac' -- attribute [simp,search] is_kernel.fac -- restate_axiom is_kernel.uniq' -- attribute [search,elim] is_kernel.uniq -- @[extensionality] lemma is_kernel.ext {f : Y ⟶ Z} {ι : X ⟶ Y} (P Q : is_kernel f ι) : P = Q := -- begin cases P, cases Q, obviously end -- -- TODO should be marked [search]? -- lemma kernel.w {f : Y ⟶ Z} {X : C} (ι : X ⟶ Y) (k : is_kernel f ι) : ι ≫ f = zero_morphism _ _ := by rw k.w -- variable (C) -- class has_kernels := -- (kernel : Π {Y Z : C} (f : Y ⟶ Z), C) -- (ι : Π {Y Z : C} (f : Y ⟶ Z), kernel f ⟶ Y) -- (is : Π {Y Z : C} (f : Y ⟶ Z), is_kernel f (ι f)) -- variable {C} -- variable [has_kernels.{u v} C] -- def kernel (f : Y ⟶ Z) : C := has_kernels.kernel.{u v} f -- def kernel.ι (f : Y ⟶ Z) : kernel f ⟶ Y := has_kernels.ι.{u v} f -- def kernel.subobject (f : Y ⟶ Z) : over Y := ⟨ kernel f, kernel.ι f ⟩ -- def kernel_of_equalizer {f : Y ⟶ Z} {t : fork f (zero_morphism _ _)} (e : is_equalizer t) : is_kernel f t.ι := -- { w' := begin have p := t.w, simp at p, exact p end, -- lift := λ X' ι' w, e.lift { X := X', ι := ι' }, -- uniq' := λ X' ι' w m h, begin tidy, apply e.uniq { X := X', ι := m ≫ t.ι }, tidy end } -- -- def equalizer_of_kernel {f : Y ⟶ Z} {t : fork f (zero_morphism _ _)} (k : is_kernel f t.ι) : is_equalizer t := -- -- { lift := λ s, begin have e := s.w, tidy, exact k.lift e, end, -- -- uniq := sorry, } -- -- def kernels_are_equalizers {f : Y ⟶ Z} (t : fork f (zero_morphism _ _)) : equiv (is_kernel f t.ι) (is_equalizer t) := -- -- { to_fun := equalizer_of_kernel, -- -- inv_fun := kernel_of_equalizer, -- -- left_inv := sorry, -- -- right_inv := sorry } -- end category_theory.limits
e141b27ec26ce381ad6e7cf0c386af42f2469cc9
704c0b4221e71a413a9270e3160af0813f426c91
/src/simplex.lean
2bb50651875a2c29f889893d36e0010d8b8f1d14
[]
no_license
CohenCyril/LP
d39b766199d2176b592ff223f9c00cdcb4d5f76d
4dca829aa0fe94c0627410b88d9dad54e6ed2c63
refs/heads/master
1,596,132,000,357
1,568,817,913,000
1,568,817,913,000
210,310,499
0
0
null
1,569,229,479,000
1,569,229,479,000
null
UTF-8
Lean
false
false
44,223
lean
import tableau order.lexicographic open matrix fintype finset function pequiv partition variables {m n : ℕ} local notation `rvec`:2000 n := matrix (fin 1) (fin n) ℚ local notation `cvec`:2000 m := matrix (fin m) (fin 1) ℚ local infix ` ⬝ `:70 := matrix.mul local postfix `ᵀ` : 1500 := transpose namespace tableau def pivot_col (T : tableau m n) (obj : fin m) : option (fin n) := option.cases_on (fin.find (λ c : fin n, T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted ∧ c ∉ T.dead)) (((list.fin_range n).filter (λ c : fin n, 0 < T.to_matrix obj c ∧ c ∉ T.dead)).argmin T.to_partition.colg) some def to_lex (T : tableau m n) (c : fin n) (r' : fin m) : lex ℚ (fin (m + n)) := (abs (T.const r' 0 / T.to_matrix r' c), T.to_partition.rowg r') lemma to_lex_le_iff (T : tableau m n) (c : fin n) (i i' : fin m) : to_lex T c i ≤ to_lex T c i' ↔ abs (T.const i 0 / T.to_matrix i c) < abs (T.const i' 0 / T.to_matrix i' c) ∨ (abs (T.const i 0 / T.to_matrix i c) = abs (T.const i' 0 / T.to_matrix i' c) ∧ T.to_partition.rowg i ≤ T.to_partition.rowg i') := prod.lex_def _ _ def pivot_row (T : tableau m n) (obj: fin m) (c : fin n) : option (fin m) := let l := (list.fin_range m).filter (λ r : fin m, obj ≠ r ∧ T.to_partition.rowg r ∈ T.restricted ∧ T.to_matrix obj c / T.to_matrix r c < 0) in list.argmin (to_lex T c) l lemma pivot_col_spec {T : tableau m n} {obj : fin m} {c : fin n} : c ∈ pivot_col T obj → ((T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted) ∨ (0 < T.to_matrix obj c ∧ T.to_partition.colg c ∈ T.restricted)) ∧ c ∉ T.dead := begin dsimp only [pivot_col], cases h : fin.find (λ c : fin n, T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted ∧ c ∉ T.dead), { have := not_and.1 (fin.find_eq_none_iff.1 h c) ∘ ne_of_gt, simp only [list.argmin_eq_some_iff, option.mem_def, list.mem_filter, list.mem_fin_range, gt] at * {contextual := tt}, tauto }, { finish [fin.find_eq_some_iff] } end lemma nonpos_of_lt_pivot_col {T : tableau m n} {obj : fin m} {c j : fin n} (hc : c ∈ pivot_col T obj) (hcres : T.to_partition.colg c ∈ T.restricted) (hdead : j ∉ T.dead) (hjc : T.to_partition.colg j < T.to_partition.colg c) : T.to_matrix obj j ≤ 0 := begin rw [pivot_col] at hc, cases h : fin.find (λ c, T.to_matrix obj c ≠ 0 ∧ colg (T.to_partition) c ∉ T.restricted ∧ c ∉ T.dead), { rw h at hc, refine le_of_not_lt (λ hj0, _), exact not_le_of_gt hjc ((list.mem_argmin_iff.1 hc).2.1 j (list.mem_filter.2 (by simp [hj0, hdead]))) }, { rw h at hc, simp [*, fin.find_eq_some_iff] at * } end lemma pivot_col_eq_none_aux {T : tableau m n} {obj : fin m} (hT : T.feasible) {c : fin n} : pivot_col T obj = none → c ∉ T.dead → ((T.to_matrix obj c = 0 ∧ T.to_partition.colg c ∉ T.restricted) ∨ (T.to_matrix obj c ≤ 0 ∧ T.to_partition.colg c ∈ T.restricted)) := begin simp only [pivot_col], cases h : fin.find (λ c : fin n, T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted ∧ c ∉ T.dead), { simp only [list.filter_eq_nil, list.argmin_eq_none, not_and', list.mem_fin_range, true_implies_iff, not_lt, fin.find_eq_none_iff, and_imp, not_not] at *, assume hnonneg hdead, by_cases hres : T.to_partition.colg c ∈ T.restricted; simp * at * }, { simp } end lemma pivot_col_eq_none {T : tableau m n} {obj : fin m} (hT : T.feasible) (h : pivot_col T obj = none) : T.is_optimal (T.of_col 0) (T.to_partition.rowg obj) := is_optimal_of_col_zero hT (λ j hj, begin have := pivot_col_eq_none_aux hT h hj, finish [lt_irrefl] end) lemma pivot_row_spec {T : tableau m n} {obj r : fin m} {c : fin n} : r ∈ pivot_row T obj c → obj ≠ r ∧ T.to_partition.rowg r ∈ T.restricted ∧ T.to_matrix obj c / T.to_matrix r c < 0 ∧ (∀ r' : fin m, obj ≠ r' → T.to_partition.rowg r' ∈ T.restricted → T.to_matrix obj c / T.to_matrix r' c < 0 → abs (T.const r 0 / T.to_matrix r c) ≤ abs (T.const r' 0 / T.to_matrix r' c)) := begin simp only [list.mem_filter, pivot_row, option.mem_def, list.argmin_eq_some_iff, list.mem_fin_range, true_and, and_imp], simp only [to_lex_le_iff], intros hor hres hr0 h _, simp only [*, true_and, ne.def, not_false_iff], intros r' hor' hres' hr0', cases h r' hor' hres' hr0', { exact le_of_lt (by assumption) }, { exact le_of_eq (by tauto) } end lemma nonneg_of_lt_pivot_row {T : tableau m n} {obj : fin m} {r i : fin m} {c : fin n} (hc0 : 0 < T.to_matrix obj c) (hres : T.to_partition.rowg i ∈ T.restricted) (hc : c ∈ pivot_col T obj) (hr : r ∈ pivot_row T obj c) (hconst : T.const i 0 = 0) (hjc : T.to_partition.rowg i < T.to_partition.rowg r) : 0 ≤ T.to_matrix i c := if hobj : obj = i then le_of_lt $ hobj ▸ hc0 else le_of_not_gt $ λ hic, not_le_of_lt hjc begin have := (list.argmin_eq_some_iff.1 hr).2.1 i (list.mem_filter.2 ⟨list.mem_fin_range _, hobj, hres, div_neg_of_pos_of_neg hc0 hic⟩), simp [hconst, not_lt_of_ge (abs_nonneg _), *, to_lex_le_iff] at * end lemma ne_zero_of_mem_pivot_row {T : tableau m n} {obj r : fin m} {c : fin n} (hr : r ∈ pivot_row T obj c) : T.to_matrix r c ≠ 0 := assume hrc, by simpa [lt_irrefl, hrc] using pivot_row_spec hr lemma ne_zero_of_mem_pivot_col {T : tableau m n} {obj : fin m} {c : fin n} (hc : c ∈ pivot_col T obj) : T.to_matrix obj c ≠ 0 := λ h, by simpa [h, lt_irrefl] using pivot_col_spec hc lemma pivot_row_eq_none_aux {T : tableau m n} {obj : fin m} {c : fin n} (hrow : pivot_row T obj c = none) (hs : c ∈ pivot_col T obj) : ∀ r, obj ≠ r → T.to_partition.rowg r ∈ T.restricted → 0 ≤ T.to_matrix obj c / T.to_matrix r c := by simpa [pivot_row, list.filter_eq_nil] using hrow lemma pivot_row_eq_none {T : tableau m n} {obj : fin m} {c : fin n} (hT : T.feasible) (hrow : pivot_row T obj c = none) (hs : c ∈ pivot_col T obj) : T.is_unbounded_above (T.to_partition.rowg obj) := have hrow : ∀ r, obj ≠ r → T.to_partition.rowg r ∈ T.restricted → 0 ≤ T.to_matrix obj c / T.to_matrix r c, from pivot_row_eq_none_aux hrow hs, have hc : ((T.to_matrix obj c ≠ 0 ∧ T.to_partition.colg c ∉ T.restricted) ∨ (0 < T.to_matrix obj c ∧ T.to_partition.colg c ∈ T.restricted)) ∧ c ∉ T.dead, from pivot_col_spec hs, have hToc : T.to_matrix obj c ≠ 0, from λ h, by simpa [h, lt_irrefl] using hc, (lt_or_gt_of_ne hToc).elim (λ hToc : T.to_matrix obj c < 0, is_unbounded_above_rowg_of_nonpos hT c (hc.1.elim and.right (λ h, (not_lt_of_gt hToc h.1).elim)) hc.2 (λ i hi, classical.by_cases (λ hoi : obj = i, le_of_lt (hoi ▸ hToc)) (λ hoi : obj ≠ i, inv_nonpos.1 $ nonpos_of_mul_nonneg_right (hrow _ hoi hi) hToc)) hToc) (λ hToc : 0 < T.to_matrix obj c, is_unbounded_above_rowg_of_nonneg hT c (λ i hi, classical.by_cases (λ hoi : obj = i, le_of_lt (hoi ▸ hToc)) (λ hoi : obj ≠ i, inv_nonneg.1 $ nonneg_of_mul_nonneg_left (hrow _ hoi hi) hToc)) hc.2 hToc) def feasible_of_mem_pivot_row_and_col {T : tableau m n} {obj : fin m} (hT : T.feasible) {c} (hc : c ∈ pivot_col T obj) {r} (hr : r ∈ pivot_row T obj c) : feasible (T.pivot r c) := begin have := pivot_col_spec hc, have := pivot_row_spec hr, have := @feasible_simplex_pivot _ _ _ obj hT r c, tauto end section blands_rule local attribute [instance, priority 0] classical.dec variable (obj : fin m) def fickle (T T' : tableau m n) (v : fin (m + n)) : Prop := T.to_partition.rowp v ≠ T'.to_partition.rowp v ∨ T.to_partition.colp v ≠ T'.to_partition.colp v lemma fickle_symm {T T' : tableau m n} {v : fin (m + n)} : fickle T T' v ↔ fickle T' T v := by simp [fickle, eq_comm] lemma fickle_colg_iff_ne {T T' : tableau m n} {j : fin n} : fickle T T' (T.to_partition.colg j) ↔ T.to_partition.colg j ≠ T'.to_partition.colg j := ⟨λ h h', h.elim (by rw [rowp_colg_eq_none, h', rowp_colg_eq_none]; simp) (by rw [colp_colg, h', colp_colg]; simp), λ h, or.inr $ λ h', h begin have : T'.to_partition.colp (T.to_partition.colg j) = some j, { simpa [eq_comm] using h' }, rwa [← pequiv.eq_some_iff, colp_symm_eq_some_colg, option.some_inj, eq_comm] at this end⟩ lemma fickle_rowg_iff_ne {T T' : tableau m n} {i : fin m} : fickle T T' (T.to_partition.rowg i) ↔ T.to_partition.rowg i ≠ T'.to_partition.rowg i := ⟨λ h h', h.elim (by rw [rowp_rowg, h', rowp_rowg]; simp) (by rw [colp_rowg_eq_none, h', colp_rowg_eq_none]; simp), λ h, or.inl $ λ h', h begin have : T'.to_partition.rowp (T.to_partition.rowg i) = some i, { simpa [eq_comm] using h' }, rwa [← pequiv.eq_some_iff, rowp_symm_eq_some_rowg, option.some_inj, eq_comm] at this end⟩ lemma not_unique_row_and_unique_col {T T' : tableau m n} {r c c'} (hcobj0 : 0 < T.to_matrix obj c) (hc'obj0 : 0 < T'.to_matrix obj c') (hrc0 : T.to_matrix r c < 0) (hflat : T.flat = T'.flat) (hs : T.to_partition.rowg r = T'.to_partition.colg c') (hrobj : T.to_partition.rowg obj = T'.to_partition.rowg obj) (hfickle : ∀ i, (fickle T T' (T.to_partition.rowg i)) → T.const i 0 = 0) (hobj : T.const obj 0 = T'.const obj 0) (nonpos_of_colg_eq : ∀ j, j ≠ c' → T'.to_partition.colg j = T.to_partition.colg c → T'.to_matrix obj j ≤ 0) (unique_col : ∀ j, (fickle T' T (T'.to_partition.colg j)) → j ≠ c' → T'.to_matrix obj j ≤ 0) (unique_row : ∀ i ≠ r, T.const i 0 = 0 → fickle T T' (T.to_partition.rowg i) → 0 ≤ T.to_matrix i c) : false := let objr := T.to_partition.rowg obj in let x := λ y : ℚ, T.of_col (y • (single c 0).to_matrix) in have hxflatT' : ∀ {y}, x y ∈ flat T', from hflat ▸ λ _, of_col_mem_flat _ _, have hxrow : ∀ y i, x y (T.to_partition.rowg i) 0 = T.const i 0 + y * T.to_matrix i c, by simp [x, of_col_single_rowg], have hxcol : ∀ {y j}, j ≠ c → x y (T.to_partition.colg j) 0 = 0, from λ y j hjc, by simp [x, of_col_colg, pequiv.to_matrix, single_apply_of_ne hjc.symm], have hxcolc : ∀ {y}, x y (T.to_partition.colg c) 0 = y, by simp [x, of_col_colg, pequiv.to_matrix], let c_star : fin (m + n) → ℚ := λ v, option.cases_on (T'.to_partition.colp v) 0 (T'.to_matrix obj) in have hxobj : ∀ y, x y objr 0 = T.const obj 0 + y * T.to_matrix obj c, from λ y, hxrow _ _, have hgetr : ∀ {y v}, c_star v * x y v 0 ≠ 0 → (T'.to_partition.colp v).is_some, from λ y v, by cases h : T'.to_partition.colp v; dsimp [c_star]; rw h; simp, have c_star_eq_get : ∀ {v} (hv : (T'.to_partition.colp v).is_some), c_star v = T'.to_matrix obj (option.get hv), from λ v hv, by dsimp only [c_star]; conv_lhs{rw [← option.some_get hv]}; refl, have hsummmn : ∀ {y}, sum univ (λ j, T'.to_matrix obj j * x y (T'.to_partition.colg j) 0) = sum univ (λ v, c_star v * x y v 0), from λ y, sum_bij_ne_zero (λ j _ _, T'.to_partition.colg j) (λ _ _ _, mem_univ _) (λ _ _ _ _ _ _ h, T'.to_partition.injective_colg h) (λ v _ h0, ⟨option.get (hgetr h0), mem_univ _, by rw [← c_star_eq_get (hgetr h0)]; simpa using h0, by simp⟩) (λ _ _ h0, by dsimp [c_star]; rw [colp_colg]), have hgetc : ∀ {y v}, c_star v * x y v 0 ≠ 0 → v ≠ T.to_partition.colg c → (T.to_partition.rowp v).is_some, from λ y v, (eq_rowg_or_colg T.to_partition v).elim (λ ⟨i, hi⟩, by rw [hi, rowp_rowg]; simp) (λ ⟨j, hj⟩ h0 hvc, by rw [hj, hxcol (mt (congr_arg T.to_partition.colg) (hvc ∘ hj.trans)), mul_zero] at h0; exact (h0 rfl).elim), have hsummmnn : ∀ {y}, (univ.erase (T.to_partition.colg c)).sum (λ v, c_star v * x y v 0) = univ.sum (λ i, c_star (T.to_partition.rowg i) * x y (T.to_partition.rowg i) 0), from λ y, eq.symm $ sum_bij_ne_zero (λ i _ _, T.to_partition.rowg i) (by simp) (λ _ _ _ _ _ _ h, T.to_partition.injective_rowg h) (λ v hvc h0, ⟨option.get (hgetc h0 (mem_erase.1 hvc).1), mem_univ _, by simpa using h0⟩) (by intros; refl), have hsumm : ∀ {y}, univ.sum (λ i, c_star (T.to_partition.rowg i) * x y (T.to_partition.rowg i) 0) = univ.sum (λ i, c_star (T.to_partition.rowg i) * T.const i 0) + y * univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c), from λ y, by simp only [hxrow, mul_add, add_mul, sum_add_distrib, mul_assoc, mul_left_comm _ y, mul_sum.symm], have hxobj' : ∀ y, x y objr 0 = univ.sum (λ v, c_star v * x y v 0) + T'.const obj 0, from λ y, by dsimp [objr]; rw [hrobj, mem_flat_iff.1 hxflatT', hsummmn], have hy : ∀ {y}, y * T.to_matrix obj c = c_star (T.to_partition.colg c) * y + univ.sum (λ i, c_star (T.to_partition.rowg i) * T.const i 0) + y * univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c), from λ y, by rw [← add_left_inj (T.const obj 0), ← hxobj, hxobj', ← insert_erase (mem_univ (T.to_partition.colg c)), sum_insert (not_mem_erase _ _), hsummmnn, hobj, hsumm, hxcolc]; simp, have hy' : ∀ (y), y * (T.to_matrix obj c - c_star (T.to_partition.colg c) - univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c)) = univ.sum (λ i, c_star (T.to_partition.rowg i) * T.const i 0), from λ y, by rw [mul_sub, mul_sub, hy]; simp [mul_comm, mul_assoc, mul_left_comm], have h0 : T.to_matrix obj c - c_star (T.to_partition.colg c) - univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c) = 0, by rw [← (domain.mul_left_inj (@one_ne_zero ℚ _)), hy', ← hy' 0, zero_mul, mul_zero], have hcolnec' : T'.to_partition.colp (T.to_partition.colg c) ≠ some c', from λ h, by simpa [hs.symm] using congr_arg T'.to_partition.colg (option.eq_some_iff_get_eq.1 h).snd, have eq_of_roweqc' : ∀ {i}, T'.to_partition.colp (T.to_partition.rowg i) = some c' → i = r, from λ i h, by simpa [hs.symm, T.to_partition.injective_rowg.eq_iff] using congr_arg T'.to_partition.colg (option.eq_some_iff_get_eq.1 h).snd, have sumpos : 0 < univ.sum (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c), by rw [← sub_eq_zero.1 h0]; exact add_pos_of_pos_of_nonneg hcobj0 (begin simp only [c_star, neg_nonneg], cases h : T'.to_partition.colp (T.to_partition.colg c) with j, { refl }, { exact nonpos_of_colg_eq j (mt (congr_arg some) (h ▸ hcolnec')) (by rw [← (option.eq_some_iff_get_eq.1 h).snd]; simp) } end), have hexi : ∃ i, 0 < c_star (T.to_partition.rowg i) * T.to_matrix i c, from imp_of_not_imp_not _ _ (by simpa using @sum_nonpos _ _ (@univ (fin m) _) (λ i, c_star (T.to_partition.rowg i) * T.to_matrix i c) _ _) sumpos, let ⟨i, hi⟩ := hexi in have hi0 : T.const i 0 = 0, from hfickle i (fickle_rowg_iff_ne.2 $ λ h, by dsimp [c_star] at hi; rw [h, colp_rowg_eq_none] at hi; simpa [lt_irrefl] using hi), have hi_some : (T'.to_partition.colp (T.to_partition.rowg i)).is_some, from option.ne_none_iff_is_some.1 (λ h, by dsimp only [c_star] at hi; rw h at hi; simpa [lt_irrefl] using hi), have hi' : 0 < T'.to_matrix obj (option.get hi_some) * T.to_matrix i c, by dsimp only [c_star] at hi; rwa [← option.some_get hi_some] at hi, have hir : i ≠ r, from λ hir, begin have : option.get hi_some = c', from T'.to_partition.injective_colg (by rw [colg_get_colp_symm, ← hs, hir]), rw [this, hir] at hi', exact not_lt_of_gt hi' (mul_neg_of_pos_of_neg hc'obj0 hrc0) end, have hnec' : option.get hi_some ≠ c', from λ eq_c', hir $ @eq_of_roweqc' i (eq_c' ▸ by simp), have hic0 : T.to_matrix i c < 0, from neg_of_mul_pos_right hi' (unique_col _ (by rw [fickle_colg_iff_ne]; simp) hnec'), not_le_of_gt hic0 (unique_row _ hir hi0 (by rw [fickle_rowg_iff_ne, ← colg_get_colp_symm _ _ hi_some]; exact colg_ne_rowg _ _ _)) inductive rel : tableau m n → tableau m n → Prop | pivot : ∀ {T}, feasible T → ∀ {r c}, c ∈ pivot_col T obj → r ∈ pivot_row T obj c → rel (T.pivot r c) T | trans_pivot : ∀ {T₁ T₂ r c}, rel T₁ T₂ → c ∈ pivot_col T₁ obj → r ∈ pivot_row T₁ obj c → rel (T₁.pivot r c) T₂ lemma feasible_of_rel_right {T T' : tableau m n} (h : rel obj T' T) : T.feasible := rel.rec_on h (by tauto) (by tauto) lemma feasible_of_rel_left {T T' : tableau m n} (h : rel obj T' T) : T'.feasible := rel.rec_on h (λ _ hT _ _ hc hr, feasible_of_mem_pivot_row_and_col hT hc hr) (λ _ _ _ _ _ hc hr hT, feasible_of_mem_pivot_row_and_col hT hc hr) /-- Slightly stronger recursor than the default recursor -/ @[elab_as_eliminator] lemma rel.rec_on' {obj : fin m} {C : tableau m n → tableau m n → Prop} {T T' : tableau m n} (hrel : rel obj T T') (hpivot : ∀ {T : tableau m n} {r : fin m} {c : fin n}, feasible T → c ∈ pivot_col T obj → r ∈ pivot_row T obj c → C (pivot T r c) T) (hpivot_trans : ∀ {T₁ T₂ : tableau m n} {r : fin m} {c : fin n}, rel obj (T₁.pivot r c) T₁ → rel obj T₁ T₂ → c ∈ pivot_col T₁ obj → r ∈ pivot_row T₁ obj c → C (T₁.pivot r c) T₁ → C T₁ T₂ → C (pivot T₁ r c) T₂) : C T T' := rel.rec_on hrel (λ T hT r c hc hr, hpivot hT hc hr) (λ T₁ T₂ r c hrelT₁₂ hc hr ih, hpivot_trans (rel.pivot (feasible_of_rel_left obj hrelT₁₂) hc hr) hrelT₁₂ hc hr (hpivot (feasible_of_rel_left obj hrelT₁₂) hc hr) ih) lemma rel.trans {obj : fin m} {T₁ T₂ T₃ : tableau m n} (h₁₂ : rel obj T₁ T₂) : rel obj T₂ T₃ → rel obj T₁ T₃ := rel.rec_on h₁₂ (λ T r c hT hc hr hrelT, rel.trans_pivot hrelT hc hr) (λ T₁ T₂ r c hrelT₁₂ hc hr ih hrelT₂₃, rel.trans_pivot (ih hrelT₂₃) hc hr) instance : is_trans (tableau m n) (rel obj) := ⟨@rel.trans _ _ obj⟩ lemma flat_eq_of_rel {T T' : tableau m n} (h : rel obj T' T) : flat T' = flat T := rel.rec_on' h (λ _ _ _ _ _ hr, flat_pivot (ne_zero_of_mem_pivot_row hr)) (λ _ _ _ _ _ _ _ _, eq.trans) lemma rowg_obj_eq_of_rel {T T' : tableau m n} (h : rel obj T T') : T.to_partition.rowg obj = T'.to_partition.rowg obj := rel.rec_on' h (λ T r c hfT hc hr, by simp [rowg_swap_of_ne _ (pivot_row_spec hr).1]) (λ _ _ _ _ _ _ _ _, eq.trans) lemma restricted_eq_of_rel {T T' : tableau m n} (h : rel obj T T') : T.restricted = T'.restricted := rel.rec_on' h (λ _ _ _ _ _ _, rfl) (λ _ _ _ _ _ _ _ _, eq.trans) lemma dead_eq_of_rel {T T' : tableau m n} (h : rel obj T T') : T.dead = T'.dead := rel.rec_on' h (λ _ _ _ _ _ _, rfl) (λ _ _ _ _ _ _ _ _, eq.trans) lemma dead_eq_of_rel_or_eq {T T' : tableau m n} (h : T = T' ∨ rel obj T T') : T.dead = T'.dead := h.elim (congr_arg _) $ dead_eq_of_rel _ lemma exists_mem_pivot_row_col_of_rel {T T' : tableau m n} (h : rel obj T' T) : ∃ r c, c ∈ pivot_col T obj ∧ r ∈ pivot_row T obj c := rel.rec_on' h (λ _ r c _ hc hr, ⟨r, c, hc, hr⟩) (λ _ _ _ _ _ _ _ _ _, id) lemma exists_mem_pivot_row_of_rel {T T' : tableau m n} (h : rel obj T' T) {c : fin n} (hc : c ∈ pivot_col T obj) : ∃ r, r ∈ pivot_row T obj c := let ⟨r, c', hc', hr⟩ := exists_mem_pivot_row_col_of_rel obj h in ⟨r, by simp * at *⟩ lemma exists_mem_pivot_col_of_fickle {T₁ T₂ : tableau m n} (h : rel obj T₂ T₁) {c : fin n} : fickle T₁ T₂ (T₁.to_partition.colg c) → ∃ T₃, (T₃ = T₁ ∨ rel obj T₃ T₁) ∧ (rel obj T₂ T₃) ∧ T₃.to_partition.colg c = T₁.to_partition.colg c ∧ c ∈ pivot_col T₃ obj := rel.rec_on' h begin assume T r c' hT hc' hr, rw fickle_colg_iff_ne, by_cases hcc : c = c', { subst hcc, exact λ _, ⟨T, or.inl rfl, rel.pivot hT hc' hr, rfl, hc'⟩ }, { simp [colg_swap_of_ne _ hcc] } end (λ T₁ T₂ r c hrelp₁ hrel₁₂ hc hr ihp₁ ih₁₂, (imp_iff_not_or.1 ih₁₂).elim (λ ih₁₂, (imp_iff_not_or.1 ihp₁).elim (λ ihp₁ hf, (fickle_colg_iff_ne.1 hf (by simp [*, fickle_colg_iff_ne] at *)).elim) (λ ⟨T₃, hT₃⟩ hf, ⟨T₃, hT₃.1.elim (λ h, h.symm ▸ or.inr hrel₁₂) (λ h, or.inr $ h.trans hrel₁₂), hT₃.2.1, hT₃.2.2.1.trans (by simpa [eq_comm, fickle_colg_iff_ne] using ih₁₂), hT₃.2.2.2⟩)) (λ ⟨T₃, hT₃⟩ hf, ⟨T₃, hT₃.1, hrelp₁.trans hT₃.2.1, hT₃.2.2⟩)) lemma exists_mem_pivot_row_of_fickle {T₁ T₂ : tableau m n} (h : rel obj T₂ T₁) (r : fin m) : fickle T₁ T₂ (T₁.to_partition.rowg r) → ∃ (T₃ : tableau m n) c, (T₃ = T₁ ∨ rel obj T₃ T₁) ∧ (rel obj T₂ T₃) ∧ T₃.to_partition.rowg r = T₁.to_partition.rowg r ∧ c ∈ pivot_col T₃ obj ∧ r ∈ pivot_row T₃ obj c := rel.rec_on' h begin assume T r' c hT hc hr', rw fickle_rowg_iff_ne, by_cases hrr : r = r', { subst hrr, exact λ _, ⟨T, c, or.inl rfl, rel.pivot hT hc hr', rfl, hc, hr'⟩ }, { simp [rowg_swap_of_ne _ hrr] } end (λ T₁ T₂ r c hrelp₁ hrel₁₂ hc hr ihp₁ ih₁₂, (imp_iff_not_or.1 ih₁₂).elim (λ ih₁₂, (imp_iff_not_or.1 ihp₁).elim (λ ihp₁ hf, (fickle_rowg_iff_ne.1 hf (by simp [*, fickle_rowg_iff_ne] at *)).elim) (λ ⟨T₃, c', hT₃⟩ hf, ⟨T₃, c', hT₃.1.elim (λ h, h.symm ▸ or.inr hrel₁₂) (λ h, or.inr $ h.trans hrel₁₂), hT₃.2.1, hT₃.2.2.1.trans (by simpa [eq_comm, fickle_rowg_iff_ne] using ih₁₂), by clear_aux_decl; tauto⟩)) (λ ⟨T₃, c', hT₃⟩ _, ⟨T₃, c', hT₃.1, (rel.pivot (feasible_of_rel_left _ hrel₁₂) hc hr).trans hT₃.2.1, hT₃.2.2⟩)) lemma eq_or_rel_pivot_of_rel {T₁ T₂ : tableau m n} (h : rel obj T₁ T₂) : ∀ {r c} (hc : c ∈ pivot_col T₂ obj) (hr : r ∈ pivot_row T₂ obj c), T₁ = T₂.pivot r c ∨ rel obj T₁ (T₂.pivot r c) := rel.rec_on' h (λ T r c hT hc hr r' c' hc' hr', by simp * at *) (λ T₁ T₂ r c hrelp₁ hrel₁₂ hc hr ihp₁ ih₁₂ r' c' hc' hr', (ih₁₂ hc' hr').elim (λ ih₁₂, or.inr $ ih₁₂ ▸ rel.pivot (feasible_of_rel_left _ hrel₁₂) hc hr) (λ ih₁₂, or.inr $ (rel.pivot (feasible_of_rel_left _ hrel₁₂) hc hr).trans ih₁₂)) lemma exists_mem_pivot_col_of_mem_pivot_row {T : tableau m n} (hrelTT : rel obj T T) {r c} (hc : c ∈ pivot_col T obj) (hr : r ∈ pivot_row T obj c) : ∃ (T' : tableau m n), c ∈ pivot_col T' obj ∧ T'.to_partition.colg c = T.to_partition.rowg r ∧ rel obj T' T ∧ rel obj T T' := have hrelTTp : rel obj T (T.pivot r c), from (eq_or_rel_pivot_of_rel _ hrelTT hc hr).elim (λ h, h ▸ hrelTT ) id, let ⟨T', hT'⟩ := exists_mem_pivot_col_of_fickle obj hrelTTp $ fickle_colg_iff_ne.2 $ (show (T.pivot r c).to_partition.colg c ≠ T.to_partition.colg c, by simp) in ⟨T', hT'.2.2.2, by simp [hT'.2.2.1], hT'.1.elim (λ h, h.symm ▸ rel.pivot (feasible_of_rel_left _ hrelTT) hc hr) (λ h, h.trans $ rel.pivot (feasible_of_rel_left _ hrelTT) hc hr), hT'.2.1⟩ lemma exists_mem_pivot_col_of_fickle_row {T T' : tableau m n} (hrelTT' : rel obj T T') {r : fin m} (hrelT'T : rel obj T' T) (hrow : fickle T T' (T.to_partition.rowg r)) : ∃ (T₃ : tableau m n) c, c ∈ pivot_col T₃ obj ∧ T₃.to_partition.colg c = T.to_partition.rowg r ∧ rel obj T₃ T ∧ rel obj T T₃ := let ⟨T₃, c, hT₃, hrelT₃T, hrow₃, hc, hr⟩ := exists_mem_pivot_row_of_fickle obj hrelT'T _ hrow in let ⟨T₄, hT₄⟩ := exists_mem_pivot_col_of_mem_pivot_row obj (show rel obj T₃ T₃, from hT₃.elim (λ h, h.symm ▸ hrelTT'.trans hrelT'T) (λ h, h.trans $ hrelTT'.trans hrelT₃T)) hc hr in ⟨T₄, c, hT₄.1, hT₄.2.1.trans hrow₃, hT₄.2.2.1.trans $ hT₃.elim (λ h, h.symm ▸ hrelTT'.trans hrelT'T) (λ h, h.trans $ hrelTT'.trans hrelT'T), hrelTT'.trans (hrelT₃T.trans hT₄.2.2.2)⟩ lemma const_obj_le_of_rel {T₁ T₂ : tableau m n} (h : rel obj T₁ T₂) : T₂.const obj 0 ≤ T₁.const obj 0 := rel.rec_on' h (λ T r c hT hc hr, have hr' : _ := pivot_row_spec hr, simplex_const_obj_le hT (by tauto) (by tauto)) (λ _ _ _ _ _ _ _ _ h₁ h₂, le_trans h₂ h₁) lemma const_obj_eq_of_rel_of_rel {T₁ T₂ : tableau m n} (h₁₂ : rel obj T₁ T₂) (h₂₁ : rel obj T₂ T₁) : T₁.const obj 0 = T₂.const obj 0 := le_antisymm (const_obj_le_of_rel _ h₂₁) (const_obj_le_of_rel _ h₁₂) lemma const_eq_const_of_const_obj_eq {T₁ T₂ : tableau m n} (h₁₂ : rel obj T₁ T₂) : ∀ (hobj : T₁.const obj 0 = T₂.const obj 0) (i : fin m), T₁.const i 0 = T₂.const i 0 := rel.rec_on' h₁₂ (λ T r c hfT hc hr hobj i, have hr0 : T.const r 0 = 0, from const_eq_zero_of_const_obj_eq hfT (ne_zero_of_mem_pivot_col hc) (ne_zero_of_mem_pivot_row hr) (pivot_row_spec hr).1 hobj, if hir : i = r then by simp [hir, hr0] else by simp [const_pivot_of_ne _ hir, hr0]) (λ T₁ T₂ r c hrelp₁ hrel₁₂ hc hr ihp₁ ih₁₂ hobj i, have hobjp : (pivot T₁ r c).const obj 0 = T₁.const obj 0, from le_antisymm (hobj.symm ▸ const_obj_le_of_rel _ hrel₁₂) (const_obj_le_of_rel _ hrelp₁), by rw [ihp₁ hobjp, ih₁₂ (hobjp.symm.trans hobj)]) lemma const_eq_zero_of_fickle_of_rel_self {T T' : tableau m n} (hrelTT' : rel obj T T') (hrelT'T : rel obj T' T) (i : fin m) (hrow : fickle T T' (T.to_partition.rowg i)) : T.const i 0 = 0 := let ⟨T₃, c, hT₃₁, hT'₃, hrow₃, hc, hi⟩ := exists_mem_pivot_row_of_fickle obj hrelT'T _ hrow in have T₃.const i 0 = 0, from const_eq_zero_of_const_obj_eq (feasible_of_rel_right _ hT'₃) (ne_zero_of_mem_pivot_col hc) (ne_zero_of_mem_pivot_row hi) (pivot_row_spec hi).1 (const_obj_eq_of_rel_of_rel _ (rel.pivot (feasible_of_rel_right _ hT'₃) hc hi) ((eq_or_rel_pivot_of_rel _ hT'₃ hc hi).elim (λ h, h ▸ hT₃₁.elim (λ h, h.symm ▸ hrelTT') (λ h, h.trans hrelTT')) (λ hrelT'p, hT₃₁.elim (λ h, h.symm ▸ hrelTT'.trans (h ▸ hrelT'p)) (λ h, h.trans $ hrelTT'.trans hrelT'p)))), have hobj : T₃.const obj 0 = T.const obj 0, from hT₃₁.elim (λ h, h ▸ rfl) (λ h, const_obj_eq_of_rel_of_rel _ h (hrelTT'.trans hT'₃)), hT₃₁.elim (λ h, h ▸ this) (λ h, const_eq_const_of_const_obj_eq obj h hobj i ▸ this) lemma colg_mem_restricted_of_rel_self {T : tableau m n} (hrelTT : rel obj T T) {c} (hc : c ∈ pivot_col T obj) : T.to_partition.colg c ∈ T.restricted := let ⟨r, hr⟩ := exists_mem_pivot_row_of_rel obj hrelTT hc in let ⟨T', c', hT', hrelTT', hrowcol, _, hr'⟩ := exists_mem_pivot_row_of_fickle obj ((eq_or_rel_pivot_of_rel _ hrelTT hc hr).elim (λ h, show rel obj T (T.pivot r c), from h ▸ hrelTT) id) _ (fickle_rowg_iff_ne.2 $ show (T.pivot r c).to_partition.rowg r ≠ T.to_partition.rowg r, by simp) in (restricted_eq_of_rel _ hrelTT').symm ▸ by convert (pivot_row_spec hr').2.1; simp [hrowcol] lemma eq_zero_of_not_mem_restricted_of_rel_self {T : tableau m n} (hrelTT : rel obj T T) {j} (hjres : T.to_partition.colg j ∉ T.restricted) (hdead : j ∉ T.dead) : T.to_matrix obj j = 0 := let ⟨r, c, hc, hr⟩ := exists_mem_pivot_row_col_of_rel obj hrelTT in have hcres : T.to_partition.colg c ∈ T.restricted, from colg_mem_restricted_of_rel_self obj hrelTT hc, by_contradiction $ λ h0, begin simp [pivot_col] at hc, cases h : fin.find (λ c, T.to_matrix obj c ≠ 0 ∧ colg (T.to_partition) c ∉ T.restricted ∧ c ∉ T.dead), { simp [*, fin.find_eq_none_iff] at * }, { rw h at hc, clear_aux_decl, have := (fin.find_eq_some_iff.1 h).1, simp * at * } end lemma rel.irrefl {obj : fin m} : ∀ (T : tableau m n), ¬ rel obj T T := λ T1 hrelT1, let ⟨rT1 , cT1, hrT1, hcT1⟩ := exists_mem_pivot_row_col_of_rel obj hrelT1 in let ⟨t, ht⟩ := finset.max_of_mem (show T1.to_partition.colg cT1 ∈ univ.filter (λ v, ∃ (T' : tableau m n) (c : fin n), rel obj T' T' ∧ c ∈ pivot_col T' obj ∧ T'.to_partition.colg c = v), by simp only [true_and, mem_filter, mem_univ, exists_and_distrib_left]; exact ⟨T1, hrelT1, cT1, hrT1, rfl⟩) in let ⟨_, T', c', hrelTT'', hcT', hct⟩ := finset.mem_filter.1 (finset.mem_of_max ht) in have htmax : ∀ (s : fin (m + n)) (T : tableau m n), rel obj T T → ∀ (j : fin n), pivot_col T obj = some j → T.to_partition.colg j = s → s ≤ t, by simpa using λ s (h : s ∈ _), finset.le_max_of_mem h ht, let ⟨r, hrT'⟩ := exists_mem_pivot_row_of_rel obj hrelTT'' hcT' in have hrelTT''p : rel obj T' (T'.pivot r c'), from (eq_or_rel_pivot_of_rel obj hrelTT'' hcT' hrT').elim (λ h, h ▸ hrelTT'') id, let ⟨T, c, hTT', hrelT'T, hT'Tr, hc, hr⟩ := exists_mem_pivot_row_of_fickle obj hrelTT''p r (by rw fickle_symm; simp [fickle_colg_iff_ne]) in have hfT' : feasible T', from feasible_of_rel_left _ hrelTT'', have hfT : feasible T, from feasible_of_rel_right _ hrelT'T, have hrelT'pT' : rel obj (T'.pivot r c') T', from rel.pivot hfT' hcT' hrT', have hrelTT' : rel obj T T', from hTT'.elim (λ h, h.symm ▸ hrelT'pT') (λ h, h.trans hrelT'pT'), have hrelTT : rel obj T T, from hrelTT'.trans hrelT'T, have hc't : T.to_partition.colg c ≤ t, from htmax _ T hrelTT _ hc rfl, have hoT'T : T'.const obj 0 = T.const obj 0, from const_obj_eq_of_rel_of_rel _ hrelT'T hrelTT', have hfickle : ∀ i, fickle T T' (T.to_partition.rowg i) → T.const i 0 = 0, from const_eq_zero_of_fickle_of_rel_self obj hrelTT' hrelT'T, have hobj : T.const obj 0 = T'.const obj 0, from const_obj_eq_of_rel_of_rel _ hrelTT' hrelT'T, have hflat : T.flat = T'.flat, from flat_eq_of_rel obj hrelTT', have hrobj : T.to_partition.rowg obj = T'.to_partition.rowg obj, from rowg_obj_eq_of_rel _ hrelTT', have hs : T.to_partition.rowg r = T'.to_partition.colg c', by simpa using hT'Tr, have hc'res : T'.to_partition.colg c' ∈ T'.restricted, from hs ▸ restricted_eq_of_rel _ hrelTT' ▸ (pivot_row_spec hr).2.1, have hc'obj0 : 0 < T'.to_matrix obj c' ∧ c' ∉ T'.dead, by simpa [hc'res] using pivot_col_spec hcT', have hcres : T.to_partition.colg c ∈ T.restricted, from colg_mem_restricted_of_rel_self obj hrelTT hc, have hcobj0 : 0 < to_matrix T obj c ∧ c ∉ T.dead, by simpa [hcres] using pivot_col_spec hc, have hrc0 : T.to_matrix r c < 0, from inv_neg'.1 $ neg_of_mul_neg_left (pivot_row_spec hr).2.2.1 (le_of_lt hcobj0.1), have nonpos_of_colg_ne : ∀ j, (fickle T' T (T'.to_partition.colg j)) → j ≠ c' → T'.to_matrix obj j ≤ 0, from λ j hj hjc', let ⟨T₃, hT₃⟩ := exists_mem_pivot_col_of_fickle obj hrelTT' hj in nonpos_of_lt_pivot_col hcT' hc'res (dead_eq_of_rel_or_eq obj hT₃.1 ▸ (pivot_col_spec hT₃.2.2.2).2) (lt_of_le_of_ne (hct.symm ▸ hT₃.2.2.1 ▸ htmax _ T₃ (hT₃.1.elim (λ h, h.symm ▸ hrelTT'') (λ h, h.trans (hrelT'T.trans hT₃.2.1))) _ hT₃.2.2.2 rfl) (by rwa [ne.def, T'.to_partition.injective_colg.eq_iff])), have nonpos_of_colg_eq : ∀ j, j ≠ c' → T'.to_partition.colg j = T.to_partition.colg c → T'.to_matrix obj j ≤ 0, from λ j hjc' hj, if hjc : j = c then by clear_aux_decl; subst hjc; exact nonpos_of_lt_pivot_col hcT' hc'res (by rw [dead_eq_of_rel obj hrelT'T]; tauto) (lt_of_le_of_ne (hj.symm ▸ hct.symm ▸ hc't) (by simpa)) else nonpos_of_colg_ne _ (fickle_colg_iff_ne.2 $ by simpa [hj, eq_comm] using hjc) hjc', have unique_row : ∀ i ≠ r, T.const i 0 = 0 → fickle T T' (T.to_partition.rowg i) → 0 ≤ T.to_matrix i c, from λ i hir hi0 hrow, let ⟨T₃, c₃, hc₃, hrow₃, hrelT₃T, hrelTT₃⟩ := exists_mem_pivot_col_of_fickle_row _ hrelTT' hrelT'T hrow in have hrelT₃T₃ : rel obj T₃ T₃, from hrelT₃T.trans hrelTT₃, nonneg_of_lt_pivot_row (by exact hcobj0.1) (by rw [← hrow₃, ← restricted_eq_of_rel _ hrelT₃T]; exact colg_mem_restricted_of_rel_self _ hrelT₃T₃ hc₃) hc hr hi0 (lt_of_le_of_ne (by rw [hs, hct, ← hrow₃]; exact htmax _ _ hrelT₃T₃ _ hc₃ rfl) (by simpa [fickle_rowg_iff_ne] using hrow)), not_unique_row_and_unique_col obj hcobj0.1 hc'obj0.1 hrc0 hflat hs hrobj hfickle hobj nonpos_of_colg_eq nonpos_of_colg_ne unique_row noncomputable instance fintype_rel (T : tableau m n) : fintype {T' | rel obj T' T} := fintype.of_injective (λ T', T'.val.to_partition) (λ T₁ T₂ h, subtype.eq $ tableau.ext (by rw [flat_eq_of_rel _ T₁.2, flat_eq_of_rel _ T₂.2]) h (by rw [dead_eq_of_rel _ T₁.2, dead_eq_of_rel _ T₂.2]) (by rw [restricted_eq_of_rel _ T₁.2, restricted_eq_of_rel _ T₂.2])) lemma rel_wf (m n : ℕ) (obj : fin m): well_founded (@rel m n obj) := subrelation.wf (show subrelation (@rel m n obj) (measure (λ T, fintype.card {T' | rel obj T' T})), from assume T₁ T₂ h, set.card_lt_card (set.ssubset_iff_subset_not_subset.2 ⟨λ T' hT', hT'.trans h, classical.not_forall.2 ⟨T₁, λ h', rel.irrefl _ (h' h)⟩⟩)) (measure_wf (λ T, fintype.card {T' | rel obj T' T})) end blands_rule inductive termination (n : ℕ) : Type | while {} : termination | unbounded (c : fin n) : termination | optimal {} : termination namespace termination lemma injective_unbounded : function.injective (@unbounded n) := λ _ _ h, by injection h @[simp] lemma unbounded_inj {c c' : fin n} : unbounded c = unbounded c' ↔ c = c' := injective_unbounded.eq_iff end termination open termination instance {n : ℕ} : has_repr $ termination n := ⟨λ t, termination.cases_on t "while" (λ c, "unbounded " ++ repr c) "optimal"⟩ open termination /-- The simplex algorithm -/ def simplex (w : tableau m n → bool) (obj : fin m) : Π (T : tableau m n) (hT : feasible T), tableau m n × termination n | T := λ hT, cond (w T) (match pivot_col T obj, @feasible_of_mem_pivot_row_and_col _ _ _ obj hT, @rel.pivot m n obj _ hT with | none, hc, hrel := (T, optimal) | some c, hc, hrel := match pivot_row T obj c, @hc _ rfl, (λ r, @hrel r c rfl) with | none, hr, hrel := (T, unbounded c) | some r, hr, hrel := have wf : rel obj (pivot T r c) T, from hrel _ rfl, simplex (T.pivot r c) (hr rfl) end end) (T, while) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, rel_wf m n obj⟩], dec_tac := tactic.assumption} lemma simplex_pivot {w : tableau m n → bool} {T : tableau m n} (hT : feasible T) (hw : w T = tt) {obj : fin m} {r : fin m} {c : fin n} (hc : c ∈ pivot_col T obj) (hr : r ∈ pivot_row T obj c) : (T.pivot r c).simplex w obj (feasible_of_mem_pivot_row_and_col hT hc hr) = T.simplex w obj hT := by conv_rhs { rw simplex }; simp [hw, show _ = _, from hr, show _ = _, from hc, simplex._match_1, simplex._match_2] lemma simplex_spec_aux (w : tableau m n → bool) (obj : fin m) : Π (T : tableau m n) (hT : feasible T), ((T.simplex w obj hT).2 = while ∧ w (T.simplex w obj hT).1 = ff) ∨ ((T.simplex w obj hT).2 = optimal ∧ w (T.simplex w obj hT).1 = tt ∧ pivot_col (T.simplex w obj hT).1 obj = none) ∨ ∃ c, ((T.simplex w obj hT).2 = unbounded c ∧ w (T.simplex w obj hT).1 = tt ∧ c ∈ pivot_col (T.simplex w obj hT).1 obj ∧ pivot_row (T.simplex w obj hT).1 obj c = none) | T := λ hT, begin cases hw : w T, { rw simplex, simp [hw] }, { cases hc : pivot_col T obj with c, { rw simplex, simp [hc, hw, simplex._match_1] }, { cases hr : pivot_row T obj c with r, { rw simplex, simp [hr, hc, hw, simplex._match_1, simplex._match_2] }, { rw [← simplex_pivot hT hw hc hr], exact have wf : rel obj (T.pivot r c) T, from rel.pivot hT hc hr, simplex_spec_aux _ _ } } } end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, rel_wf m n obj⟩], dec_tac := tactic.assumption} lemma simplex_while_eq_ff {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} (hw : w T = ff) : T.simplex w obj hT = (T, while) := by rw [simplex, hw]; refl lemma simplex_pivot_col_eq_none {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} (hw : w T = tt) {obj : fin m} (hc : pivot_col T obj = none) : T.simplex w obj hT = (T, optimal) := by rw simplex; simp [hc, hw, simplex._match_1] lemma simplex_pivot_row_eq_none {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} (hw : w T = tt) {c} (hc : c ∈ pivot_col T obj) (hr : pivot_row T obj c = none) : T.simplex w obj hT = (T, unbounded c) := by rw simplex; simp [hw, show _ = _, from hc, hr, simplex._match_1, simplex._match_2] lemma simplex_induction (P : tableau m n → Prop) (w : tableau m n → bool) (obj : fin m): Π {T : tableau m n} (hT : feasible T) (h0 : P T) (hpivot : ∀ {T' r c}, w T' = tt → c ∈ pivot_col T' obj → r ∈ pivot_row T' obj c → feasible T' → P T' → P (T'.pivot r c)), P (T.simplex w obj hT).1 | T := λ hT h0 hpivot, begin cases hw : w T, { rwa [simplex_while_eq_ff hw] }, { cases hc : pivot_col T obj with c, { rwa [simplex_pivot_col_eq_none hw hc] }, { cases hr : pivot_row T obj c with r, { rwa simplex_pivot_row_eq_none hw hc hr }, { rw [← simplex_pivot _ hw hc hr], exact have wf : rel obj (pivot T r c) T, from rel.pivot hT hc hr, simplex_induction (feasible_of_mem_pivot_row_and_col hT hc hr) (hpivot hw hc hr hT h0) @hpivot } } } end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, rel_wf m n obj⟩], dec_tac := `[tauto]} @[simp] lemma feasible_simplex {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} : feasible (T.simplex w obj hT).1 := simplex_induction feasible _ _ hT hT (λ _ _ _ _ hc hr _ hT', feasible_of_mem_pivot_row_and_col hT' hc hr) @[simp] lemma simplex_simplex {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} : (T.simplex w obj hT).1.simplex w obj feasible_simplex = T.simplex w obj hT := simplex_induction (λ T', ∀ (hT' : feasible T'), T'.simplex w obj hT' = T.simplex w obj hT) w _ _ (λ _, rfl) (λ T' r c hw hc hr hT' ih hpivot, by rw [simplex_pivot hT' hw hc hr, ih]) _ /-- `simplex` does not move the row variable it is trying to maximise. -/ @[simp] lemma rowg_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.to_partition.rowg obj = T.to_partition.rowg obj := simplex_induction (λ T', T'.to_partition.rowg obj = T.to_partition.rowg obj) _ _ _ rfl (λ T' r c hw hc hr, by simp [rowg_swap_of_ne _ (pivot_row_spec hr).1]) @[simp] lemma flat_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.flat = T.flat := simplex_induction (λ T', T'.flat = T.flat) w obj _ rfl (λ T' r c hw hc hr hT' ih, have T'.to_matrix r c ≠ 0, from λ h, by simpa [h, lt_irrefl] using pivot_row_spec hr, by rw [flat_pivot this, ih]) @[simp] lemma restricted_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.restricted = T.restricted := simplex_induction (λ T', T'.restricted = T.restricted) _ _ _ rfl (by simp { contextual := tt }) @[simp] lemma dead_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.dead = T.dead := simplex_induction (λ T', T'.dead = T.dead) _ _ _ rfl (by simp { contextual := tt }) @[simp] lemma res_set_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.res_set = T.res_set := simplex_induction (λ T', T'.res_set = T.res_set) w obj _ rfl (λ T' r c hw hc hr, by simp [res_set_pivot (ne_zero_of_mem_pivot_row hr)] {contextual := tt}) @[simp] lemma dead_set_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.dead_set = T.dead_set := simplex_induction (λ T', T'.dead_set = T.dead_set) w obj _ rfl (λ T' r c hw hc hr, by simp [dead_set_pivot (ne_zero_of_mem_pivot_row hr) (pivot_col_spec hc).2] {contextual := tt}) @[simp] lemma sol_set_simplex (T : tableau m n) (hT : feasible T) (w : tableau m n → bool) (obj : fin m) : (T.simplex w obj hT).1.sol_set = T.sol_set := by simp [sol_set_eq_res_set_inter_dead_set] @[simp] lemma of_col_simplex_zero_mem_sol_set {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} : (T.simplex w obj hT).1.of_col 0 ∈ sol_set T := by rw [← sol_set_simplex, of_col_zero_mem_sol_set_iff]; exact feasible_simplex @[simp] lemma of_col_simplex_rowg {w : tableau m n → bool} {T : tableau m n} {hT : feasible T} {obj : fin m} (x : cvec n) : (T.simplex w obj hT).1.of_col x (T.to_partition.rowg obj) = ((T.simplex w obj hT).1.to_matrix ⬝ x + (T.simplex w obj hT).1.const) obj := by rw [← of_col_rowg (T.simplex w obj hT).1 x obj, rowg_simplex] @[simp] lemma is_unbounded_above_simplex {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} {v : fin (m + n)} : is_unbounded_above (T.simplex w obj hT).1 v ↔ is_unbounded_above T v := by simp [is_unbounded_above] @[simp] lemma is_optimal_simplex {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} {x : cvec (m + n)} {v : fin (m + n)} : is_optimal (T.simplex w obj hT).1 x v ↔ is_optimal T x v := by simp [is_optimal] lemma termination_eq_while_iff {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} : (T.simplex w obj hT).2 = while ↔ w (T.simplex w obj hT).1 = ff := by have := simplex_spec_aux w obj T hT; finish lemma termination_eq_optimal_iff_pivot_col_eq_none {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} : (T.simplex w obj hT).2 = optimal ↔ w (T.simplex w obj hT).1 = tt ∧ pivot_col (T.simplex w obj hT).1 obj = none := by rcases simplex_spec_aux w obj T hT with _ | ⟨_, _, _⟩ | ⟨⟨_, _⟩, _, _, _, _⟩; simp * at * lemma termination_eq_unbounded_iff_pivot_row_eq_none {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} {c : fin n} : (T.simplex w obj hT).2 = unbounded c ↔ w (T.simplex w obj hT).1 = tt ∧ c ∈ pivot_col (T.simplex w obj hT).1 obj ∧ pivot_row (T.simplex w obj hT).1 obj c = none := by split; intros; rcases simplex_spec_aux w obj T hT with _ | ⟨_, _, _⟩ | ⟨⟨⟨_, _⟩, _⟩, _, _, _, _⟩; simp * at * lemma unbounded_of_termination_eq_unbounded {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} {c : fin n} : (T.simplex w obj hT).2 = unbounded c → w (T.simplex w obj hT).1 = tt ∧ is_unbounded_above T (T.to_partition.rowg obj) := begin rw termination_eq_unbounded_iff_pivot_row_eq_none, rintros ⟨_, hc⟩, simpa * using pivot_row_eq_none feasible_simplex hc.2 hc.1 end lemma termination_eq_optimal_iff {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} : (T.simplex w obj hT).2 = optimal ↔ w (T.simplex w obj hT).1 = tt ∧ is_optimal T ((T.simplex w obj hT).1.of_col 0) (T.to_partition.rowg obj) := begin rw [termination_eq_optimal_iff_pivot_col_eq_none], split, { rintros ⟨_, hc⟩, simpa * using pivot_col_eq_none feasible_simplex hc }, { cases ht : (T.simplex w obj hT).2, { simp [*, termination_eq_while_iff] at * }, { cases unbounded_of_termination_eq_unbounded ht, simp [*, not_optimal_of_unbounded_above right] }, { simp [*, termination_eq_optimal_iff_pivot_col_eq_none] at * } } end lemma termination_eq_unbounded_iff {T : tableau m n} {hT : feasible T} {w : tableau m n → bool} {obj : fin m} {c : fin n}: (T.simplex w obj hT).2 = unbounded c ↔ w (T.simplex w obj hT).1 = tt ∧ is_unbounded_above T (T.to_partition.rowg obj) ∧ c ∈ pivot_col (T.simplex w obj hT).1 obj := ⟨λ hc, and.assoc.1 $ ⟨unbounded_of_termination_eq_unbounded hc, (termination_eq_unbounded_iff_pivot_row_eq_none.1 hc).2.1⟩, begin have := @not_optimal_of_unbounded_above m n (T.simplex w obj hT).1 (T.to_partition.rowg obj) ((T.simplex w obj hT).1.of_col 0), cases ht : (T.simplex w obj hT).2; simp [termination_eq_optimal_iff, termination_eq_while_iff, termination_eq_unbounded_iff_pivot_row_eq_none, *] at * end⟩ end tableau
be85235c29fa1b09223804116e2518787500a699
acc85b4be2c618b11fc7cb3005521ae6858a8d07
/data/fin.lean
bf1fd763eade7beb725089f1fcb40eb5348b96e1
[ "Apache-2.0" ]
permissive
linpingchuan/mathlib
d49990b236574df2a45d9919ba43c923f693d341
5ad8020f67eb13896a41cc7691d072c9331b1f76
refs/heads/master
1,626,019,377,808
1,508,048,784,000
1,508,048,784,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
487
lean
import data.nat.basic open fin nat def raise_fin {n : ℕ} (k : fin n) : fin (n + 1) := ⟨val k, lt_succ_of_lt (is_lt k)⟩ theorem eq_of_lt_succ_of_not_lt {a b : ℕ} (h1 : a < b + 1) (h2 : ¬ a < b) : a = b := have h3 : a ≤ b, from le_of_lt_succ h1, or.elim (eq_or_lt_of_not_lt h2) (λ h, h) (λ h, absurd h (not_lt_of_ge h3)) instance fin_to_nat (n : ℕ) : has_coe (fin n) nat := ⟨fin.val⟩ instance fin_to_int (n : ℕ) : has_coe (fin n) int := ⟨λ k, ↑(fin.val k)⟩
e4c36ea2046c402b3bf26f177c32760bae735f7a
761fea1362b10b4c588c2dfc0ae90c70b119e35d
/src/provers/ljt.lean
f9f919ec47485bc14a23bbd10dd75100713413ba
[]
no_license
holtzermann17/mm-lean
382a29fca5245f97cf488c525ed0c9594917f73b
a9130d71ed448f62df28d4128043b707bad85ccd
refs/heads/master
1,588,477,413,982
1,553,885,046,000
1,553,885,046,000
178,404,617
0
0
null
1,553,863,829,000
1,553,863,828,000
null
UTF-8
Lean
false
false
3,782
lean
import .utils open tactic expr util /- the intuitionitsic prover -/ -- Assumes pr is a proof of t. Adds the consequences of t to the context -- and returns tt if anything nontrivial has been added. meta def add_consequences : expr → expr → tactic bool := λ pr t, let assert_consequences := λ e t, mcond (add_consequences e t) skip (assertv_fresh t e) in match t with | `(¬ %%a) := do e ← mk_mapp ``imp_false_of_not [some a, some pr], na ← to_expr ``(%%a → false), assert_consequences e na, return tt | `(%%a ∧ %%b) := do e₁ ← mk_app ``and.left [pr], assert_consequences e₁ a, e₂ ← mk_app ``and.right [pr], assert_consequences e₂ b, return tt | `(%%a → %%b) := do ctx ← local_context, (do h ← find_same_type a ctx, assert_consequences (pr h) b, return tt) <|> match a with | `(%%c ∨ %%d) := do e₁ ← mk_mapp ``imp_of_or_imp_left [some c, some d, some b, pr], t₁ ← to_expr ``(%%c → %%b), assert_consequences e₁ t₁, e₂ ← mk_mapp ``imp_of_or_imp_right [some c, some d, some b, pr], t₂ ← to_expr ``(%%d → %%b), assert_consequences e₂ t₂, return tt | `(%%c ∧ %%d) := do e ← mk_mapp ``uncurry [some c, some d, some b, pr], t ← to_expr ``(%%c → (%%d → %%b)), util.assertv_fresh t e, return tt | _ := return ff end | _ := return ff end -- return tt if any progress is made meta def process_hyp (h : expr) : tactic bool := do t ← infer_type h, mcond (add_consequences h t) (clear h >> return tt) (return ff) -- return tt if any progress is made meta def process_hyps_aux : list expr → tactic bool | [] := return ff | (h :: hs) := do b₁ ← process_hyp h, b₂ ← process_hyps_aux hs, return (b₁ || b₂) -- fail if no progress is made meta def process_hyps : tactic unit := do ctx ← local_context, mcond (process_hyps_aux ctx) skip failed meta def apply_nonsplitting_rules : tactic unit := do repeat (intro_fresh >> skip), repeat process_hyps -- test it example {p q r s : Prop} (h : p ∨ q → r) (h' : q ∧ r ∧ (s ∨ r → p)) : true := by do apply_nonsplitting_rules, triv meta def is_nested_imp (e : expr) : bool := match e with | `((%%a → %%b) → %%c) := tt | `(¬%%a → %%c) := tt | _ := ff end meta def find_hyp_aux (p : expr → bool) : list expr → tactic expr | [] := failed | (h :: hs) := do t ← infer_type h, if p t then return h else find_hyp_aux hs meta def find_hyp (p : expr → bool) : tactic expr := do local_context >>= find_hyp_aux p meta def apply_splitting_rule : tactic unit := (applyc ``and.intro) <|> (do h ← find_hyp (λ e, is_app_of e `or), e ← to_expr ``(or.elim %%h), apply e >> clear h) meta def elim_nested_imp (cont : tactic unit) : list expr → tactic unit | [] := failed | (h :: hs) := do t ← infer_type h, (guard (is_nested_imp t) >> (do e ← to_expr ``(nested_imp_elim %%h), apply e >> clear h >> try intros; cont)) <|> (elim_nested_imp hs) meta def apply_backtracking_rule (cont : tactic unit) : tactic unit := do t ← target, (guard (is_app_of t `or) >> ((left >> cont) <|> (right >> cont))) <|> (local_context >>= elim_nested_imp cont) meta def intuit : tactic unit := do finish <|> apply_nonsplitting_rules >> (finish <|> apply_splitting_rule >> (intuit >> intuit) <|> (apply_backtracking_rule intuit)) meta def glivenko : tactic unit := do repeat (intro_fresh >> skip), deny_conclusion, intuit
2949819824b5278e4290e0492b8dacb5b20f716e
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/bad9.lean
9415c179774495c9c716f450482ca639300fa3e8
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
203
lean
set_option pp::implicit true. set_option pp::colors false. variable N : Type. check fun (a : N) (f : N -> N) (H : f a = a), let calc1 : f a = a := substp (fun x : N, f a = _) (refl (f a)) H in calc1.
962f47c34d5cc8e763f293b5a79a36d7944029f3
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/set_theory/zfc.lean
705f5da6c65f4202988bfc27613ee7aafb5cecec
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
30,760
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro A model of ZFC in Lean. -/ import data.set.basic universes u v /-- The type of `n`-ary functions `α → α → ... → α`. -/ def arity (α : Type u) : nat → Type u | 0 := α | (n+1) := α → arity n namespace arity /-- Constant `n`-ary function with value `a`. -/ def const {α : Type u} (a : α) : ∀ n, arity α n | 0 := a | (n+1) := λ _, const n instance arity.inhabited {α n} [inhabited α] : inhabited (arity α n) := ⟨const (default _) _⟩ end arity /-- The type of pre-sets in universe `u`. A pre-set is a family of pre-sets indexed by a type in `Type u`. The ZFC universe is defined as a quotient of this to ensure extensionality. -/ inductive pSet : Type (u+1) | mk (α : Type u) (A : α → pSet) : pSet namespace pSet /-- The underlying type of a pre-set -/ def type : pSet → Type u | ⟨α, A⟩ := α /-- The underlying pre-set family of a pre-set -/ def func : Π (x : pSet), x.type → pSet | ⟨α, A⟩ := A theorem mk_type_func : Π (x : pSet), mk x.type x.func = x | ⟨α, A⟩ := rfl /-- Two pre-sets are extensionally equivalent if every element of the first family is extensionally equivalent to some element of the second family and vice-versa. -/ def equiv (x y : pSet) : Prop := pSet.rec (λα z m ⟨β, B⟩, (∀a, ∃b, m a (B b)) ∧ (∀b, ∃a, m a (B b))) x y theorem equiv.refl (x) : equiv x x := pSet.rec_on x $ λα A IH, ⟨λa, ⟨a, IH a⟩, λa, ⟨a, IH a⟩⟩ theorem equiv.euc {x} : Π {y z}, equiv x y → equiv z y → equiv x z := pSet.rec_on x $ λα A IH y, pSet.cases_on y $ λβ B ⟨γ, Γ⟩ ⟨αβ, βα⟩ ⟨γβ, βγ⟩, ⟨λa, let ⟨b, ab⟩ := αβ a, ⟨c, bc⟩ := βγ b in ⟨c, IH a ab bc⟩, λc, let ⟨b, cb⟩ := γβ c, ⟨a, ba⟩ := βα b in ⟨a, IH a ba cb⟩⟩ theorem equiv.symm {x y} : equiv x y → equiv y x := equiv.euc (equiv.refl y) theorem equiv.trans {x y z} (h1 : equiv x y) (h2 : equiv y z) : equiv x z := equiv.euc h1 (equiv.symm h2) instance setoid : setoid pSet := ⟨pSet.equiv, equiv.refl, λx y, equiv.symm, λx y z, equiv.trans⟩ protected def subset : pSet → pSet → Prop | ⟨α, A⟩ ⟨β, B⟩ := ∀a, ∃b, equiv (A a) (B b) instance : has_subset pSet := ⟨pSet.subset⟩ theorem equiv.ext : Π (x y : pSet), equiv x y ↔ (x ⊆ y ∧ y ⊆ x) | ⟨α, A⟩ ⟨β, B⟩ := ⟨λ⟨αβ, βα⟩, ⟨αβ, λb, let ⟨a, h⟩ := βα b in ⟨a, equiv.symm h⟩⟩, λ⟨αβ, βα⟩, ⟨αβ, λb, let ⟨a, h⟩ := βα b in ⟨a, equiv.symm h⟩⟩⟩ theorem subset.congr_left : Π {x y z : pSet}, equiv x y → (x ⊆ z ↔ y ⊆ z) | ⟨α, A⟩ ⟨β, B⟩ ⟨γ, Γ⟩ ⟨αβ, βα⟩ := ⟨λαγ b, let ⟨a, ba⟩ := βα b, ⟨c, ac⟩ := αγ a in ⟨c, equiv.trans (equiv.symm ba) ac⟩, λβγ a, let ⟨b, ab⟩ := αβ a, ⟨c, bc⟩ := βγ b in ⟨c, equiv.trans ab bc⟩⟩ theorem subset.congr_right : Π {x y z : pSet}, equiv x y → (z ⊆ x ↔ z ⊆ y) | ⟨α, A⟩ ⟨β, B⟩ ⟨γ, Γ⟩ ⟨αβ, βα⟩ := ⟨λγα c, let ⟨a, ca⟩ := γα c, ⟨b, ab⟩ := αβ a in ⟨b, equiv.trans ca ab⟩, λγβ c, let ⟨b, cb⟩ := γβ c, ⟨a, ab⟩ := βα b in ⟨a, equiv.trans cb (equiv.symm ab)⟩⟩ /-- `x ∈ y` as pre-sets if `x` is extensionally equivalent to a member of the family `y`. -/ def mem : pSet → pSet → Prop | x ⟨β, B⟩ := ∃b, equiv x (B b) instance : has_mem pSet.{u} pSet.{u} := ⟨mem⟩ theorem mem.mk {α: Type u} (A : α → pSet) (a : α) : A a ∈ mk α A := show mem (A a) ⟨α, A⟩, from ⟨a, equiv.refl (A a)⟩ theorem mem.ext : Π {x y : pSet.{u}}, (∀w:pSet.{u}, w ∈ x ↔ w ∈ y) → equiv x y | ⟨α, A⟩ ⟨β, B⟩ h := ⟨λa, (h (A a)).1 (mem.mk A a), λb, let ⟨a, ha⟩ := (h (B b)).2 (mem.mk B b) in ⟨a, equiv.symm ha⟩⟩ theorem mem.congr_right : Π {x y : pSet.{u}}, equiv x y → (∀{w:pSet.{u}}, w ∈ x ↔ w ∈ y) | ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩ w := ⟨λ⟨a, ha⟩, let ⟨b, hb⟩ := αβ a in ⟨b, equiv.trans ha hb⟩, λ⟨b, hb⟩, let ⟨a, ha⟩ := βα b in ⟨a, equiv.euc hb ha⟩⟩ theorem equiv_iff_mem {x y : pSet.{u}} : equiv x y ↔ (∀{w:pSet.{u}}, w ∈ x ↔ w ∈ y) := ⟨mem.congr_right, match x, y with | ⟨α, A⟩, ⟨β, B⟩, h := ⟨λ a, h.1 (mem.mk A a), λ b, let ⟨a, h⟩ := h.2 (mem.mk B b) in ⟨a, h.symm⟩⟩ end⟩ theorem mem.congr_left : Π {x y : pSet.{u}}, equiv x y → (∀{w : pSet.{u}}, x ∈ w ↔ y ∈ w) | x y h ⟨α, A⟩ := ⟨λ⟨a, ha⟩, ⟨a, equiv.trans (equiv.symm h) ha⟩, λ⟨a, ha⟩, ⟨a, equiv.trans h ha⟩⟩ /-- Convert a pre-set to a `set` of pre-sets. -/ def to_set (u : pSet.{u}) : set pSet.{u} := {x | x ∈ u} /-- Two pre-sets are equivalent iff they have the same members. -/ theorem equiv.eq {x y : pSet} : equiv x y ↔ to_set x = to_set y := equiv_iff_mem.trans set.ext_iff.symm instance : has_coe pSet (set pSet) := ⟨to_set⟩ /-- The empty pre-set -/ protected def empty : pSet := ⟨ulift empty, λe, match e with end⟩ instance : has_emptyc pSet := ⟨pSet.empty⟩ instance : inhabited pSet := ⟨∅⟩ theorem mem_empty (x : pSet.{u}) : x ∉ (∅:pSet.{u}) := λe, match e with end /-- Insert an element into a pre-set -/ protected def insert : pSet → pSet → pSet | u ⟨α, A⟩ := ⟨option α, λo, option.rec u A o⟩ instance : has_insert pSet pSet := ⟨pSet.insert⟩ /-- The n-th von Neumann ordinal -/ def of_nat : ℕ → pSet | 0 := ∅ | (n+1) := pSet.insert (of_nat n) (of_nat n) /-- The von Neumann ordinal ω -/ def omega : pSet := ⟨ulift ℕ, λn, of_nat n.down⟩ /-- The separation operation `{x ∈ a | p x}` -/ protected def sep (p : set pSet) : pSet → pSet | ⟨α, A⟩ := ⟨{a // p (A a)}, λx, A x.1⟩ instance : has_sep pSet pSet := ⟨pSet.sep⟩ /-- The powerset operator -/ def powerset : pSet → pSet | ⟨α, A⟩ := ⟨set α, λp, ⟨{a // p a}, λx, A x.1⟩⟩ theorem mem_powerset : Π {x y : pSet}, y ∈ powerset x ↔ y ⊆ x | ⟨α, A⟩ ⟨β, B⟩ := ⟨λ⟨p, e⟩, (subset.congr_left e).2 $ λ⟨a, pa⟩, ⟨a, equiv.refl (A a)⟩, λβα, ⟨{a | ∃b, equiv (B b) (A a)}, λb, let ⟨a, ba⟩ := βα b in ⟨⟨a, b, ba⟩, ba⟩, λ⟨a, b, ba⟩, ⟨b, ba⟩⟩⟩ /-- The set union operator -/ def Union : pSet → pSet | ⟨α, A⟩ := ⟨Σx, (A x).type, λ⟨x, y⟩, (A x).func y⟩ theorem mem_Union : Π {x y : pSet.{u}}, y ∈ Union x ↔ ∃ z:pSet.{u}, ∃_:z ∈ x, y ∈ z | ⟨α, A⟩ y := ⟨λ⟨⟨a, c⟩, (e : equiv y ((A a).func c))⟩, have func (A a) c ∈ mk (A a).type (A a).func, from mem.mk (A a).func c, ⟨_, mem.mk _ _, (mem.congr_left e).2 (by rwa mk_type_func at this)⟩, λ⟨⟨β, B⟩, ⟨a, (e:equiv (mk β B) (A a))⟩, ⟨b, yb⟩⟩, by rw ←(mk_type_func (A a)) at e; exact let ⟨βt, tβ⟩ := e, ⟨c, bc⟩ := βt b in ⟨⟨a, c⟩, equiv.trans yb bc⟩⟩ /-- The image of a function -/ def image (f : pSet.{u} → pSet.{u}) : pSet.{u} → pSet | ⟨α, A⟩ := ⟨α, λa, f (A a)⟩ theorem mem_image {f : pSet.{u} → pSet.{u}} (H : ∀{x y}, equiv x y → equiv (f x) (f y)) : Π {x y : pSet.{u}}, y ∈ image f x ↔ ∃z ∈ x, equiv y (f z) | ⟨α, A⟩ y := ⟨λ⟨a, ya⟩, ⟨A a, mem.mk A a, ya⟩, λ⟨z, ⟨a, za⟩, yz⟩, ⟨a, equiv.trans yz (H za)⟩⟩ /-- Universe lift operation -/ protected def lift : pSet.{u} → pSet.{max u v} | ⟨α, A⟩ := ⟨ulift α, λ⟨x⟩, lift (A x)⟩ /-- Embedding of one universe in another -/ def embed : pSet.{max (u+1) v} := ⟨ulift.{v u+1} pSet, λ⟨x⟩, pSet.lift.{u (max (u+1) v)} x⟩ theorem lift_mem_embed : Π (x : pSet.{u}), pSet.lift.{u (max (u+1) v)} x ∈ embed.{u v} := λx, ⟨⟨x⟩, equiv.refl _⟩ /-- Function equivalence is defined so that `f ~ g` iff `∀ x y, x ~ y → f x ~ g y`. This extends to equivalence of n-ary functions. -/ def arity.equiv : Π {n}, arity pSet.{u} n → arity pSet.{u} n → Prop | 0 a b := equiv a b | (n+1) a b := ∀ x y, equiv x y → arity.equiv (a x) (b y) lemma arity.equiv_const {a : pSet.{u}} : ∀ n, arity.equiv (arity.const a n) (arity.const a n) | 0 := equiv.refl _ | (n+1) := λ x y h, arity.equiv_const _ /-- `resp n` is the collection of n-ary functions on `pSet` that respect equivalence, i.e. when the inputs are equivalent the output is as well. -/ def resp (n) := { x : arity pSet.{u} n // arity.equiv x x } instance resp.inhabited {n} : inhabited (resp n) := ⟨⟨arity.const (default _) _, arity.equiv_const _⟩⟩ def resp.f {n} (f : resp (n+1)) (x : pSet) : resp n := ⟨f.1 x, f.2 _ _ $ equiv.refl x⟩ def resp.equiv {n} (a b : resp n) : Prop := arity.equiv a.1 b.1 theorem resp.refl {n} (a : resp n) : resp.equiv a a := a.2 theorem resp.euc : Π {n} {a b c : resp n}, resp.equiv a b → resp.equiv c b → resp.equiv a c | 0 a b c hab hcb := equiv.euc hab hcb | (n+1) a b c hab hcb := by delta resp.equiv; simp [arity.equiv]; exact λx y h, @resp.euc n (a.f x) (b.f y) (c.f y) (hab _ _ h) (hcb _ _ $ equiv.refl y) instance resp.setoid {n} : setoid (resp n) := ⟨resp.equiv, resp.refl, λx y h, resp.euc (resp.refl y) h, λx y z h1 h2, resp.euc h1 $ resp.euc (resp.refl z) h2⟩ end pSet /-- The ZFC universe of sets consists of the type of pre-sets, quotiented by extensional equivalence. -/ def Set : Type (u+1) := quotient pSet.setoid.{u} namespace pSet namespace resp def eval_aux : Π {n}, { f : resp n → arity Set.{u} n // ∀ (a b : resp n), resp.equiv a b → f a = f b } | 0 := ⟨λa, ⟦a.1⟧, λa b h, quotient.sound h⟩ | (n+1) := let F : resp (n + 1) → arity Set (n + 1) := λa, @quotient.lift _ _ pSet.setoid (λx, eval_aux.1 (a.f x)) (λb c h, eval_aux.2 _ _ (a.2 _ _ h)) in ⟨F, λb c h, funext $ @quotient.ind _ _ (λq, F b q = F c q) $ λz, eval_aux.2 (resp.f b z) (resp.f c z) (h _ _ (equiv.refl z))⟩ /-- An equivalence-respecting function yields an n-ary Set function. -/ def eval (n) : resp n → arity Set.{u} n := eval_aux.1 theorem eval_val {n f x} : (@eval (n+1) f : Set → arity Set n) ⟦x⟧ = eval n (resp.f f x) := rfl end resp /-- A set function is "definable" if it is the image of some n-ary pre-set function. This isn't exactly definability, but is useful as a sufficient condition for functions that have a computable image. -/ @[class] inductive definable (n) : arity Set.{u} n → Type (u+1) | mk (f) : definable (resp.eval _ f) attribute [instance] definable.mk def definable.eq_mk {n} (f) : Π {s : arity Set.{u} n} (H : resp.eval _ f = s), definable n s | ._ rfl := ⟨f⟩ def definable.resp {n} : Π (s : arity Set.{u} n) [definable n s], resp n | ._ ⟨f⟩ := f theorem definable.eq {n} : Π (s : arity Set.{u} n) [H : definable n s], (@definable.resp n s H).eval _ = s | ._ ⟨f⟩ := rfl end pSet namespace classical open pSet noncomputable def all_definable : Π {n} (F : arity Set.{u} n), definable n F | 0 F := let p := @quotient.exists_rep pSet _ F in definable.eq_mk ⟨some p, equiv.refl _⟩ (some_spec p) | (n+1) (F : arity Set.{u} (n + 1)) := begin have I := λx, (all_definable (F x)), refine definable.eq_mk ⟨λx:pSet, (@definable.resp _ _ (I ⟦x⟧)).1, _⟩ _, { dsimp [arity.equiv], introsI x y h, rw @quotient.sound pSet _ _ _ h, exact (definable.resp (F ⟦y⟧)).2 }, exact funext (λq, quotient.induction_on q $ λx, by simp [resp.eval_val, resp.f]; exact @definable.eq _ (F ⟦x⟧) (I ⟦x⟧)) end end classical namespace Set open pSet def mk : pSet → Set := quotient.mk @[simp] theorem mk_eq (x : pSet) : @eq Set ⟦x⟧ (mk x) := rfl @[simp] lemma eval_mk {n f x} : (@resp.eval (n+1) f : Set → arity Set n) (mk x) = resp.eval n (resp.f f x) := rfl def mem : Set → Set → Prop := quotient.lift₂ pSet.mem (λx y x' y' hx hy, propext (iff.trans (mem.congr_left hx) (mem.congr_right hy))) instance : has_mem Set Set := ⟨mem⟩ /-- Convert a ZFC set into a `set` of sets -/ def to_set (u : Set.{u}) : set Set.{u} := {x | x ∈ u} protected def subset (x y : Set.{u}) := ∀ ⦃z⦄, z ∈ x → z ∈ y instance has_subset : has_subset Set := ⟨Set.subset⟩ theorem subset_iff : Π (x y : pSet), mk x ⊆ mk y ↔ x ⊆ y | ⟨α, A⟩ ⟨β, B⟩ := ⟨λh a, @h ⟦A a⟧ (mem.mk A a), λh z, quotient.induction_on z (λz ⟨a, za⟩, let ⟨b, ab⟩ := h a in ⟨b, equiv.trans za ab⟩)⟩ theorem ext {x y : Set.{u}} : (∀z:Set.{u}, z ∈ x ↔ z ∈ y) → x = y := quotient.induction_on₂ x y (λu v h, quotient.sound (mem.ext (λw, h ⟦w⟧))) theorem ext_iff {x y : Set.{u}} : (∀z:Set.{u}, z ∈ x ↔ z ∈ y) ↔ x = y := ⟨ext, λh, by simp [h]⟩ /-- The empty set -/ def empty : Set := mk ∅ instance : has_emptyc Set := ⟨empty⟩ instance : inhabited Set := ⟨∅⟩ @[simp] theorem mem_empty (x) : x ∉ (∅:Set.{u}) := quotient.induction_on x pSet.mem_empty theorem eq_empty (x : Set.{u}) : x = ∅ ↔ ∀y:Set.{u}, y ∉ x := ⟨λh, by rw h; exact mem_empty, λh, ext (λy, ⟨λyx, absurd yx (h y), λy0, absurd y0 (mem_empty _)⟩)⟩ /-- `insert x y` is the set `{x} ∪ y` -/ protected def insert : Set → Set → Set := resp.eval 2 ⟨pSet.insert, λu v uv ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨λo, match o with | some a := let ⟨b, hb⟩ := αβ a in ⟨some b, hb⟩ | none := ⟨none, uv⟩ end, λo, match o with | some b := let ⟨a, ha⟩ := βα b in ⟨some a, ha⟩ | none := ⟨none, uv⟩ end⟩⟩ instance : has_insert Set Set := ⟨Set.insert⟩ @[simp] theorem mem_insert {x y z : Set.{u}} : x ∈ insert y z ↔ x = y ∨ x ∈ z := quotient.induction_on₃ x y z (λx y ⟨α, A⟩, show x ∈ pSet.mk (option α) (λo, option.rec y A o) ↔ mk x = mk y ∨ x ∈ pSet.mk α A, from ⟨λm, match m with | ⟨some a, ha⟩ := or.inr ⟨a, ha⟩ | ⟨none, h⟩ := or.inl (quotient.sound h) end, λm, match m with | or.inr ⟨a, ha⟩ := ⟨some a, ha⟩ | or.inl h := ⟨none, quotient.exact h⟩ end⟩) @[simp] theorem mem_singleton {x y : Set.{u}} : x ∈ @singleton Set.{u} Set.{u} _ _ y ↔ x = y := iff.trans mem_insert ⟨λo, or.rec (λh, h) (λn, absurd n (mem_empty _)) o, or.inl⟩ @[simp] theorem mem_singleton' {x y : Set.{u}} : x ∈ @insert Set.{u} Set.{u} _ y ∅ ↔ x = y := mem_singleton @[simp] theorem mem_pair {x y z : Set.{u}} : x ∈ ({y, z} : Set) ↔ x = y ∨ x = z := iff.trans mem_insert $ iff.trans or.comm $ let m := @mem_singleton x y in ⟨or.imp_left m.1, or.imp_left m.2⟩ /-- `omega` is the first infinite von Neumann ordinal -/ def omega : Set := mk omega @[simp] theorem omega_zero : ∅ ∈ omega := show pSet.mem ∅ pSet.omega, from ⟨⟨0⟩, equiv.refl _⟩ @[simp] theorem omega_succ {n} : n ∈ omega.{u} → insert n n ∈ omega.{u} := quotient.induction_on n (λx ⟨⟨n⟩, h⟩, ⟨⟨n+1⟩, have Set.insert ⟦x⟧ ⟦x⟧ = Set.insert ⟦of_nat n⟧ ⟦of_nat n⟧, by rw (@quotient.sound pSet _ _ _ h), quotient.exact this⟩) /-- `{x ∈ a | p x}` is the set of elements in `a` satisfying `p` -/ protected def sep (p : Set → Prop) : Set → Set := resp.eval 1 ⟨pSet.sep (λy, p ⟦y⟧), λ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨λ⟨a, pa⟩, let ⟨b, hb⟩ := αβ a in ⟨⟨b, by rwa ←(@quotient.sound pSet _ _ _ hb)⟩, hb⟩, λ⟨b, pb⟩, let ⟨a, ha⟩ := βα b in ⟨⟨a, by rwa (@quotient.sound pSet _ _ _ ha)⟩, ha⟩⟩⟩ instance : has_sep Set Set := ⟨Set.sep⟩ @[simp] theorem mem_sep {p : Set.{u} → Prop} {x y : Set.{u}} : y ∈ {y ∈ x | p y} ↔ y ∈ x ∧ p y := quotient.induction_on₂ x y (λ⟨α, A⟩ y, ⟨λ⟨⟨a, pa⟩, h⟩, ⟨⟨a, h⟩, by rw (@quotient.sound pSet _ _ _ h); exact pa⟩, λ⟨⟨a, h⟩, pa⟩, ⟨⟨a, by rw ←(@quotient.sound pSet _ _ _ h); exact pa⟩, h⟩⟩) /-- The powerset operation, the collection of subsets of a set -/ def powerset : Set → Set := resp.eval 1 ⟨powerset, λ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨λp, ⟨{b | ∃a, p a ∧ equiv (A a) (B b)}, λ⟨a, pa⟩, let ⟨b, ab⟩ := αβ a in ⟨⟨b, a, pa, ab⟩, ab⟩, λ⟨b, a, pa, ab⟩, ⟨⟨a, pa⟩, ab⟩⟩, λq, ⟨{a | ∃b, q b ∧ equiv (A a) (B b)}, λ⟨a, b, qb, ab⟩, ⟨⟨b, qb⟩, ab⟩, λ⟨b, qb⟩, let ⟨a, ab⟩ := βα b in ⟨⟨a, b, qb, ab⟩, ab⟩⟩⟩⟩ @[simp] theorem mem_powerset {x y : Set} : y ∈ powerset x ↔ y ⊆ x := quotient.induction_on₂ x y (λ⟨α, A⟩ ⟨β, B⟩, show (⟨β, B⟩ : pSet) ∈ (pSet.powerset ⟨α, A⟩) ↔ _, by simp [mem_powerset, subset_iff]) theorem Union_lem {α β : Type u} (A : α → pSet) (B : β → pSet) (αβ : ∀a, ∃b, equiv (A a) (B b)) : ∀a, ∃b, (equiv ((Union ⟨α, A⟩).func a) ((Union ⟨β, B⟩).func b)) | ⟨a, c⟩ := let ⟨b, hb⟩ := αβ a in begin induction ea : A a with γ Γ, induction eb : B b with δ Δ, rw [ea, eb] at hb, cases hb with γδ δγ, exact let c : type (A a) := c, ⟨d, hd⟩ := γδ (by rwa ea at c) in have equiv ((A a).func c) ((B b).func (eq.rec d (eq.symm eb))), from match A a, B b, ea, eb, c, d, hd with ._, ._, rfl, rfl, x, y, hd := hd end, ⟨⟨b, eq.rec d (eq.symm eb)⟩, this⟩ end /-- The union operator, the collection of elements of elements of a set -/ def Union : Set → Set := resp.eval 1 ⟨pSet.Union, λ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩, ⟨Union_lem A B αβ, λa, exists.elim (Union_lem B A (λb, exists.elim (βα b) (λc hc, ⟨c, equiv.symm hc⟩)) a) (λb hb, ⟨b, equiv.symm hb⟩)⟩⟩ notation `⋃` := Union @[simp] theorem mem_Union {x y : Set.{u}} : y ∈ Union x ↔ ∃ z ∈ x, y ∈ z := quotient.induction_on₂ x y (λx y, iff.trans mem_Union ⟨λ⟨z, h⟩, ⟨⟦z⟧, h⟩, λ⟨z, h⟩, quotient.induction_on z (λz h, ⟨z, h⟩) h⟩) @[simp] theorem Union_singleton {x : Set.{u}} : Union {x} = x := ext $ λy, by simp; exact ⟨λ⟨z, zx, yz⟩, by subst z; exact yz, λyx, ⟨x, by simp, yx⟩⟩ theorem singleton_inj {x y : Set.{u}} (H : ({x} : Set) = {y}) : x = y := let this := congr_arg Union H in by rwa [Union_singleton, Union_singleton] at this /-- The binary union operation -/ protected def union (x y : Set.{u}) : Set.{u} := ⋃ {x, y} /-- The binary intersection operation -/ protected def inter (x y : Set.{u}) : Set.{u} := {z ∈ x | z ∈ y} /-- The set difference operation -/ protected def diff (x y : Set.{u}) : Set.{u} := {z ∈ x | z ∉ y} instance : has_union Set := ⟨Set.union⟩ instance : has_inter Set := ⟨Set.inter⟩ instance : has_sdiff Set := ⟨Set.diff⟩ @[simp] theorem mem_union {x y z : Set.{u}} : z ∈ x ∪ y ↔ z ∈ x ∨ z ∈ y := iff.trans mem_Union ⟨λ⟨w, wxy, zw⟩, match mem_pair.1 wxy with | or.inl wx := or.inl (by rwa ←wx) | or.inr wy := or.inr (by rwa ←wy) end, λzxy, match zxy with | or.inl zx := ⟨x, mem_pair.2 (or.inl rfl), zx⟩ | or.inr zy := ⟨y, mem_pair.2 (or.inr rfl), zy⟩ end⟩ @[simp] theorem mem_inter {x y z : Set.{u}} : z ∈ x ∩ y ↔ z ∈ x ∧ z ∈ y := @@mem_sep (λz:Set.{u}, z ∈ y) @[simp] theorem mem_diff {x y z : Set.{u}} : z ∈ x \ y ↔ z ∈ x ∧ z ∉ y := @@mem_sep (λz:Set.{u}, z ∉ y) theorem induction_on {p : Set → Prop} (x) (h : ∀x, (∀y ∈ x, p y) → p x) : p x := quotient.induction_on x $ λu, pSet.rec_on u $ λα A IH, h _ $ λy, show @has_mem.mem _ _ Set.has_mem y ⟦⟨α, A⟩⟧ → p y, from quotient.induction_on y (λv ⟨a, ha⟩, by rw (@quotient.sound pSet _ _ _ ha); exact IH a) theorem regularity (x : Set.{u}) (h : x ≠ ∅) : ∃ y ∈ x, x ∩ y = ∅ := classical.by_contradiction $ λne, h $ (eq_empty x).2 $ λy, induction_on y $ λz (IH : ∀w:Set.{u}, w ∈ z → w ∉ x), show z ∉ x, from λzx, ne ⟨z, zx, (eq_empty _).2 (λw wxz, let ⟨wx, wz⟩ := mem_inter.1 wxz in IH w wz wx)⟩ /-- The image of a (definable) set function -/ def image (f : Set → Set) [H : definable 1 f] : Set → Set := let r := @definable.resp 1 f _ in resp.eval 1 ⟨image r.1, λx y e, mem.ext $ λz, iff.trans (mem_image r.2) $ iff.trans (by exact ⟨λ⟨w, h1, h2⟩, ⟨w, (mem.congr_right e).1 h1, h2⟩, λ⟨w, h1, h2⟩, ⟨w, (mem.congr_right e).2 h1, h2⟩⟩) $ iff.symm (mem_image r.2)⟩ theorem image.mk : Π (f : Set.{u} → Set.{u}) [H : definable 1 f] (x) {y} (h : y ∈ x), f y ∈ @image f H x | ._ ⟨F⟩ x y := quotient.induction_on₂ x y $ λ⟨α, A⟩ y ⟨a, ya⟩, ⟨a, F.2 _ _ ya⟩ @[simp] theorem mem_image : Π {f : Set.{u} → Set.{u}} [H : definable 1 f] {x y : Set.{u}}, y ∈ @image f H x ↔ ∃z ∈ x, f z = y | ._ ⟨F⟩ x y := quotient.induction_on₂ x y $ λ⟨α, A⟩ y, ⟨λ⟨a, ya⟩, ⟨⟦A a⟧, mem.mk A a, eq.symm $ quotient.sound ya⟩, λ⟨z, hz, e⟩, e ▸ image.mk _ _ hz⟩ /-- Kuratowski ordered pair -/ def pair (x y : Set.{u}) : Set.{u} := {{x}, {x, y}} /-- A subset of pairs `{(a, b) ∈ x × y | p a b}` -/ def pair_sep (p : Set.{u} → Set.{u} → Prop) (x y : Set.{u}) : Set.{u} := {z ∈ powerset (powerset (x ∪ y)) | ∃a ∈ x, ∃b ∈ y, z = pair a b ∧ p a b} @[simp] theorem mem_pair_sep {p} {x y z : Set.{u}} : z ∈ pair_sep p x y ↔ ∃a ∈ x, ∃b ∈ y, z = pair a b ∧ p a b := by refine iff.trans mem_sep ⟨and.right, λe, ⟨_, e⟩⟩; exact let ⟨a, ax, b, bY, ze, pab⟩ := e in by rw ze; exact mem_powerset.2 (λu uz, mem_powerset.2 $ (mem_pair.1 uz).elim (λua, by rw ua; exact λv vu, by rw mem_singleton.1 vu; exact mem_union.2 (or.inl ax)) (λuab, by rw uab; exact λv vu, (mem_pair.1 vu).elim (λva, by rw va; exact mem_union.2 (or.inl ax)) (λvb, by rw vb; exact mem_union.2 (or.inr bY)))) theorem pair_inj {x y x' y' : Set.{u}} (H : pair x y = pair x' y') : x = x' ∧ y = y' := begin have ae := ext_iff.2 H, simp [pair] at ae, have : x = x', { cases (ae {x}).1 (by simp) with h h, { exact singleton_inj h }, { have m : x' ∈ ({x} : Set), { rw h, simp }, simp at m, simp [*] } }, subst x', have he : y = x → y = y', { intro yx, subst y, cases (ae {x, y'}).2 (by simp) with xy'x xy'xx, { have y'x : y' ∈ ({x} : Set) := by rw ← xy'x; simp, simp at y'x, simp [*] }, { have yxx := (ext_iff.2 xy'xx y').1 (by simp), simp at yxx, subst y' } }, have xyxy' := (ae {x, y}).1 (by simp), cases xyxy' with xyx xyy', { have yx := (ext_iff.2 xyx y).1 (by simp), simp at yx, simp [he yx] }, { have yxy' := (ext_iff.2 xyy' y).1 (by simp), simp at yxy', cases yxy' with yx yy', { simp [he yx] }, { simp [yy'] } } end /-- The cartesian product, `{(a, b) | a ∈ x, b ∈ y}` -/ def prod : Set.{u} → Set.{u} → Set.{u} := pair_sep (λa b, true) @[simp] theorem mem_prod {x y z : Set.{u}} : z ∈ prod x y ↔ ∃a ∈ x, ∃b ∈ y, z = pair a b := by simp [prod] @[simp] theorem pair_mem_prod {x y a b : Set.{u}} : pair a b ∈ prod x y ↔ a ∈ x ∧ b ∈ y := ⟨λh, let ⟨a', a'x, b', b'y, e⟩ := mem_prod.1 h in match a', b', pair_inj e, a'x, b'y with ._, ._, ⟨rfl, rfl⟩, ax, bY := ⟨ax, bY⟩ end, λ⟨ax, bY⟩, by simp; exact ⟨a, ax, b, bY, rfl⟩⟩ /-- `is_func x y f` is the assertion `f : x → y` where `f` is a ZFC function (a set of ordered pairs) -/ def is_func (x y f : Set.{u}) : Prop := f ⊆ prod x y ∧ ∀z:Set.{u}, z ∈ x → ∃! w, pair z w ∈ f /-- `funs x y` is `y ^ x`, the set of all set functions `x → y` -/ def funs (x y : Set.{u}) : Set.{u} := {f ∈ powerset (prod x y) | is_func x y f} @[simp] theorem mem_funs {x y f : Set.{u}} : f ∈ funs x y ↔ is_func x y f := by simp [funs]; exact and_iff_right_of_imp and.left -- TODO(Mario): Prove this computably noncomputable instance map_definable_aux (f : Set → Set) [H : definable 1 f] : definable 1 (λy, pair y (f y)) := @classical.all_definable 1 _ /-- Graph of a function: `map f x` is the ZFC function which maps `a ∈ x` to `f a` -/ noncomputable def map (f : Set → Set) [H : definable 1 f] : Set → Set := image (λy, pair y (f y)) @[simp] theorem mem_map {f : Set → Set} [H : definable 1 f] {x y : Set} : y ∈ map f x ↔ ∃z ∈ x, pair z (f z) = y := mem_image theorem map_unique {f : Set.{u} → Set.{u}} [H : definable 1 f] {x z : Set.{u}} (zx : z ∈ x) : ∃! w, pair z w ∈ map f x := ⟨f z, image.mk _ _ zx, λy yx, let ⟨w, wx, we⟩ := mem_image.1 yx, ⟨wz, fy⟩ := pair_inj we in by rw[←fy, wz]⟩ @[simp] theorem map_is_func {f : Set → Set} [H : definable 1 f] {x y : Set} : is_func x y (map f x) ↔ ∀z ∈ x, f z ∈ y := ⟨λ⟨ss, h⟩ z zx, let ⟨t, t1, t2⟩ := h z zx in by rw (t2 (f z) (image.mk _ _ zx)); exact (pair_mem_prod.1 (ss t1)).right, λh, ⟨λy yx, let ⟨z, zx, ze⟩ := mem_image.1 yx in by rw ←ze; exact pair_mem_prod.2 ⟨zx, h z zx⟩, λz, map_unique⟩⟩ end Set def Class := set Set namespace Class instance : has_subset Class := ⟨set.subset⟩ instance : has_sep Set Class := ⟨set.sep⟩ instance : has_emptyc Class := ⟨λ a, false⟩ instance : inhabited Class := ⟨∅⟩ instance : has_insert Set Class := ⟨set.insert⟩ instance : has_union Class := ⟨set.union⟩ instance : has_inter Class := ⟨set.inter⟩ instance : has_neg Class := ⟨set.compl⟩ instance : has_sdiff Class := ⟨set.diff⟩ /-- Coerce a set into a class -/ def of_Set (x : Set.{u}) : Class.{u} := {y | y ∈ x} instance : has_coe Set Class := ⟨of_Set⟩ /-- The universal class -/ def univ : Class := set.univ /-- Assert that `A` is a set satisfying `p` -/ def to_Set (p : Set.{u} → Prop) (A : Class.{u}) : Prop := ∃x, ↑x = A ∧ p x /-- `A ∈ B` if `A` is a set which is a member of `B` -/ protected def mem (A B : Class.{u}) : Prop := to_Set.{u} B A instance : has_mem Class Class := ⟨Class.mem⟩ theorem mem_univ {A : Class.{u}} : A ∈ univ.{u} ↔ ∃ x : Set.{u}, ↑x = A := exists_congr $ λx, and_true _ /-- Convert a conglomerate (a collection of classes) into a class -/ def Cong_to_Class (x : set Class.{u}) : Class.{u} := {y | ↑y ∈ x} /-- Convert a class into a conglomerate (a collection of classes) -/ def Class_to_Cong (x : Class.{u}) : set Class.{u} := {y | y ∈ x} /-- The power class of a class is the class of all subclasses that are sets -/ def powerset (x : Class) : Class := Cong_to_Class (set.powerset x) /-- The union of a class is the class of all members of sets in the class -/ def Union (x : Class) : Class := set.sUnion (Class_to_Cong x) notation `⋃` := Union theorem of_Set.inj {x y : Set.{u}} (h : (x : Class.{u}) = y) : x = y := Set.ext $ λz, by change (x : Class.{u}) z ↔ (y : Class.{u}) z; simp [*] @[simp] theorem to_Set_of_Set (p : Set.{u} → Prop) (x : Set.{u}) : to_Set p x ↔ p x := ⟨λ⟨y, yx, py⟩, by rwa of_Set.inj yx at py, λpx, ⟨x, rfl, px⟩⟩ @[simp] theorem mem_hom_left (x : Set.{u}) (A : Class.{u}) : (x : Class.{u}) ∈ A ↔ A x := to_Set_of_Set _ _ @[simp] theorem mem_hom_right (x y : Set.{u}) : (y : Class.{u}) x ↔ x ∈ y := iff.rfl @[simp] theorem subset_hom (x y : Set.{u}) : (x : Class.{u}) ⊆ y ↔ x ⊆ y := iff.rfl @[simp] theorem sep_hom (p : Set.{u} → Prop) (x : Set.{u}) : (↑{y ∈ x | p y} : Class.{u}) = {y ∈ x | p y} := set.ext $ λy, Set.mem_sep @[simp] theorem empty_hom : ↑(∅ : Set.{u}) = (∅ : Class.{u}) := set.ext $ λy, show _ ↔ false, by simp; exact Set.mem_empty y @[simp] theorem insert_hom (x y : Set.{u}) : (@insert Set.{u} Class.{u} _ x y) = ↑(insert x y) := set.ext $ λz, iff.symm Set.mem_insert @[simp] theorem union_hom (x y : Set.{u}) : (x : Class.{u}) ∪ y = (x ∪ y : Set.{u}) := set.ext $ λz, iff.symm Set.mem_union @[simp] theorem inter_hom (x y : Set.{u}) : (x : Class.{u}) ∩ y = (x ∩ y : Set.{u}) := set.ext $ λz, iff.symm Set.mem_inter @[simp] theorem diff_hom (x y : Set.{u}) : (x : Class.{u}) \ y = (x \ y : Set.{u}) := set.ext $ λz, iff.symm Set.mem_diff @[simp] theorem powerset_hom (x : Set.{u}) : powerset.{u} x = Set.powerset x := set.ext $ λz, iff.symm Set.mem_powerset @[simp] theorem Union_hom (x : Set.{u}) : Union.{u} x = Set.Union x := set.ext $ λz, by refine iff.trans _ (iff.symm Set.mem_Union); exact ⟨λ⟨._, ⟨a, rfl, ax⟩, za⟩, ⟨a, ax, za⟩, λ⟨a, ax, za⟩, ⟨_, ⟨a, rfl, ax⟩, za⟩⟩ /-- The definite description operator, which is {x} if `{a | p a} = {x}` and ∅ otherwise -/ def iota (p : Set → Prop) : Class := Union {x | ∀y, p y ↔ y = x} theorem iota_val (p : Set → Prop) (x : Set) (H : ∀y, p y ↔ y = x) : iota p = ↑x := set.ext $ λy, ⟨λ⟨._, ⟨x', rfl, h⟩, yx'⟩, by rwa ←((H x').1 $ (h x').2 rfl), λyx, ⟨_, ⟨x, rfl, H⟩, yx⟩⟩ /-- Unlike the other set constructors, the `iota` definite descriptor is a set for any set input, but not constructively so, so there is no associated `(Set → Prop) → Set` function. -/ theorem iota_ex (p) : iota.{u} p ∈ univ.{u} := mem_univ.2 $ or.elim (classical.em $ ∃x, ∀y, p y ↔ y = x) (λ⟨x, h⟩, ⟨x, eq.symm $ iota_val p x h⟩) (λhn, ⟨∅, by simp; exact set.ext (λz, ⟨false.rec _, λ⟨._, ⟨x, rfl, H⟩, zA⟩, hn ⟨x, H⟩⟩)⟩) /-- Function value -/ def fval (F A : Class.{u}) : Class.{u} := iota (λy, to_Set (λx, F (Set.pair x y)) A) infixl `′`:100 := fval theorem fval_ex (F A : Class.{u}) : F ′ A ∈ univ.{u} := iota_ex _ end Class namespace Set @[simp] theorem map_fval {f : Set.{u} → Set.{u}} [H : pSet.definable 1 f] {x y : Set.{u}} (h : y ∈ x) : (Set.map f x ′ y : Class.{u}) = f y := Class.iota_val _ _ (λz, by simp; exact ⟨λ⟨w, wz, pr⟩, let ⟨wy, fw⟩ := Set.pair_inj pr in by rw[←fw, wy], λe, by cases e; exact ⟨_, h, rfl⟩⟩) variables (x : Set.{u}) (h : ∅ ∉ x) /-- A choice function on the set of nonempty sets `x` -/ noncomputable def choice : Set := @map (λy, classical.epsilon (λz, z ∈ y)) (classical.all_definable _) x include h theorem choice_mem_aux (y : Set.{u}) (yx : y ∈ x) : classical.epsilon (λz:Set.{u}, z ∈ y) ∈ y := @classical.epsilon_spec _ (λz:Set.{u}, z ∈ y) $ classical.by_contradiction $ λn, h $ by rwa ←((eq_empty y).2 $ λz zx, n ⟨z, zx⟩) theorem choice_is_func : is_func x (Union x) (choice x) := (@map_is_func _ (classical.all_definable _) _ _).2 $ λy yx, by simp; exact ⟨y, yx, choice_mem_aux x h y yx⟩ theorem choice_mem (y : Set.{u}) (yx : y ∈ x) : (choice x ′ y : Class.{u}) ∈ (y : Class.{u}) := by delta choice; rw map_fval yx; simp [choice_mem_aux x h y yx] end Set
46482478577f143f70257e3d898f0849c8a86d13
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Data/JsonRpc.lean
7b215f9571662e66f8e14f56f6730002de26e722
[ "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
7,266
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Init.Control import Init.System.IO import Std.Data.RBTree import Lean.Data.Json /-! Implementation of JSON-RPC 2.0 (https://www.jsonrpc.org/specification) for use in the LSP server. -/ namespace Lean namespace JsonRpc open Json open Std (RBNode) inductive RequestID | str (s : String) | num (n : JsonNumber) | null /-- Error codes defined by JSON-RPC and LSP. -/ inductive ErrorCode | parseError | invalidRequest | methodNotFound | invalidParams | internalError | serverErrorStart | serverErrorEnd | serverNotInitialized | unknownErrorCode -- LSP-specific codes below. | requestCancelled | contentModified instance ErrorCode.hasFromJson : HasFromJson ErrorCode := ⟨fun j => match j with | num (-32700 : Int) => ErrorCode.parseError | num (-32600 : Int) => ErrorCode.invalidRequest | num (-32601 : Int) => ErrorCode.methodNotFound | num (-32602 : Int) => ErrorCode.invalidParams | num (-32603 : Int) => ErrorCode.internalError | num (-32099 : Int) => ErrorCode.serverErrorStart | num (-32000 : Int) => ErrorCode.serverErrorEnd | num (-32002 : Int) => ErrorCode.serverNotInitialized | num (-32001 : Int) => ErrorCode.unknownErrorCode | num (-32800 : Int) => ErrorCode.requestCancelled | num (-32801 : Int) => ErrorCode.contentModified | _ => none⟩ instance ErrorCode.hasToJson : HasToJson ErrorCode := ⟨fun e => match e with | ErrorCode.parseError => (-32700 : Int) | ErrorCode.invalidRequest => (-32600 : Int) | ErrorCode.methodNotFound => (-32601 : Int) | ErrorCode.invalidParams => (-32602 : Int) | ErrorCode.internalError => (-32603 : Int) | ErrorCode.serverErrorStart => (-32099 : Int) | ErrorCode.serverErrorEnd => (-32000 : Int) | ErrorCode.serverNotInitialized => (-32002 : Int) | ErrorCode.unknownErrorCode => (-32001 : Int) | ErrorCode.requestCancelled => (-32800 : Int) | ErrorCode.contentModified => (-32801 : Int)⟩ /- Uses separate constructors for notifications and errors because client and server behavior is expected to be wildly different for both. -/ inductive Message | request (id : RequestID) (method : String) (params? : Option Structured) | notification (method : String) (params? : Option Structured) | response (id : RequestID) (result : Json) | responseError (id : RequestID) (code : ErrorCode) (message : String) (data? : Option Json) def Batch := Array Message -- data types for reading expected messages structure Request (α : Type*) := (id : RequestID) (param : α) structure Response (α : Type*) := (id : RequestID) (result : α) structure Error := (id : RequestID) (code : JsonNumber) (message : String) (data? : Option Json) instance stringToRequestID : HasCoe String RequestID := ⟨RequestID.str⟩ instance numToRequestID : HasCoe JsonNumber RequestID := ⟨RequestID.num⟩ instance RequestId.hasFromJson : HasFromJson RequestID := ⟨fun j => match j with | str s => RequestID.str s | num n => RequestID.num n | _ => none⟩ instance RequestID.hasToJson : HasToJson RequestID := ⟨fun rid => match rid with | RequestID.str s => s | RequestID.num n => num n | RequestID.null => null⟩ instance Message.hasToJson : HasToJson Message := ⟨fun m => mkObj $ ⟨"jsonrpc", "2.0"⟩ :: match m with | Message.request id method params? => [ ⟨"id", toJson id⟩, ⟨"method", method⟩ ] ++ opt "params" params? | Message.notification method params? => ⟨"method", method⟩ :: opt "params" params? | Message.response id result => [ ⟨"id", toJson id⟩, ⟨"result", result⟩] | Message.responseError id code message data? => [ ⟨"id", toJson id⟩, ⟨"error", mkObj $ [ ⟨"code", toJson code⟩, ⟨"message", message⟩ ] ++ opt "data" data?⟩ ]⟩ def aux1 (j : Json) : Option Message := do id ← j.getObjValAs? RequestID "id"; method ← j.getObjValAs? String "method"; let params? := j.getObjValAs? Structured "params"; pure (Message.request id method params?) def aux2 (j : Json) : Option Message := do method ← j.getObjValAs? String "method"; let params? := j.getObjValAs? Structured "params"; pure (Message.notification method params?) def aux3 (j : Json) : Option Message := do id ← j.getObjValAs? RequestID "id"; result ← j.getObjVal? "result"; pure (Message.response id result) def aux4 (j : Json) : Option Message := do id ← j.getObjValAs? RequestID "id"; err ← j.getObjVal? "error"; code ← err.getObjValAs? ErrorCode "code"; message ← err.getObjValAs? String "message"; let data? := err.getObjVal? "data"; pure (Message.responseError id code message data?) -- HACK: The implementation must be made up of several `auxN`s instead -- of one large block because of a bug in the compiler. instance Message.hasFromJson : HasFromJson Message := ⟨fun j => do "2.0" ← j.getObjVal? "jsonrpc" | none; aux1 j <|> aux2 j <|> aux3 j <|> aux4 j⟩ end JsonRpc end Lean namespace IO.FS.Stream open Lean open Lean.JsonRpc open IO def readMessage (h : FS.Stream) (nBytes : Nat) : IO Message := do j ← h.readJson nBytes; match fromJson? j with | some m => pure m | none => throw (userError ("JSON '" ++ j.compress ++ "' did not have the format of a JSON-RPC message")) def readRequestAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α : Type*) [HasFromJson α] : IO (Request α) := do m ← h.readMessage nBytes; match m with | Message.request id method params? => if method = expectedMethod then match params? with | some params => let j := toJson params; match fromJson? j with | some v => pure ⟨id, v⟩ | none => throw (userError ("unexpected param '" ++ j.compress ++ "' for method '" ++ expectedMethod ++ "'")) | none => throw (userError ("unexpected lack of param for method '" ++ expectedMethod ++ "'")) else throw (userError ("expected method '" ++ expectedMethod ++ "', got method '" ++ method ++ "'")) | _ => throw (userError "expected request, got other type of message") def readNotificationAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α : Type*) [HasFromJson α] : IO α := do m ← h.readMessage nBytes; match m with | Message.notification method params? => if method = expectedMethod then match params? with | some params => let j := toJson params; match fromJson? j with | some v => pure v | none => throw (userError ("unexpected param '" ++ j.compress ++ "' for method '" ++ expectedMethod ++ "'")) | none => throw (userError ("unexpected lack of param for method '" ++ expectedMethod ++ "'")) else throw (userError ("expected method '" ++ expectedMethod ++ "', got method '" ++ method ++ "'")) | _ => throw (userError "expected notification, got other type of message") def writeMessage (h : FS.Stream) (m : Message) : IO Unit := h.writeJson (toJson m) def writeResponse {α : Type*} [HasToJson α] (h : FS.Stream) (id : RequestID) (r : α) : IO Unit := h.writeMessage (Message.response id (toJson r)) end IO.FS.Stream
f4deea4b99b4c8d9c1a00821b3ee709182c93d8e
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch5/ex0722.lean
bd322d4f929eb2d8f24aeb7dfa9d2f351c211c10
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
816
lean
import data.list.basic open list universe u variables {α : Type} (x y z : α) (xs ys zs : list α) def mk_symm (xs : list α) := xs ++ reverse xs theorem reverse_mk_symm (xs : list α) : reverse (mk_symm xs) = mk_symm xs := by simp [mk_symm] attribute [simp] reverse_mk_symm example (xs ys : list ℕ) (p : list ℕ → Prop) (h : p (reverse (xs ++ (mk_symm ys)))) : p (mk_symm ys ++ reverse xs) := by { simp at h; assumption } -- example (xs ys : list ℕ) (p : list ℕ → Prop) (h : p (reverse (xs ++ (mk_symm ys)))) : -- p (mk_symm ys ++ reverse xs) := -- by { simp [-reverse_mk_symm] at h; assumption } -- example (xs ys : list ℕ) (p : list ℕ → Prop) (h : p (reverse (xs ++ (mk_symm ys)))) : -- p (mk_symm ys ++ reverse xs) := -- by { simp only [reverse_append] at h; assumption }
5f2b6cd5379c4639d2675f2e6592d7da6334a49e
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/12_Axioms.org.6.lean
2078eb39029e080089446d8ec0dd5b613c614f6d
[]
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
261
lean
import standard import data.nat open nat definition f₁ (x : ℕ) := x definition f₂ (x : ℕ) := 0 + x theorem feq : f₁ = f₂ := funext (take x, eq.subst !zero_add rfl) check eq.rec (0 : ℕ) feq -- ℕ eval eq.rec (0 : ℕ) feq -- eq.rec 0 feq
d353cf0a810031d8d1fc59e51764e284ddbf0515
c2b0bd5aa4b3f4fb8a887fab57c5fc08be03aa01
/ski.lean
def9501cb9bfb8939814d2d3f826f16a325d5cb9
[]
no_license
cipher1024/lambda-calc
f72aa6dedea96fc7831d1898f3150a18baefdf0b
327eaa12588018c01b2205f3e4e0048de1d97afd
refs/heads/master
1,625,672,854,392
1,507,233,523,000
1,507,233,523,000
99,383,225
4
0
null
null
null
null
UTF-8
Lean
false
false
7,528
lean
import .variables.unary universes u v u₀ u₁ u₂ namespace ski open has_map unary inductive expr (t : Type u) (α : Type u) : ℕ → Type u | var {n : ℕ} : var α n → expr n | app {n : ℕ} : expr n → expr n → expr n | abstr (n : ℕ) : t → expr (n+1) → expr n | skip {n : ℕ} : expr n → expr (n+1) def bind {t α α'} : ∀ {v v'}, expr t α v → (var α v → expr t α' v') → expr t α' v' | v v' (expr.var ._ x) f := f x | v v' (expr.app e₀ e₁) f := expr.app (bind e₀ f) (bind e₁ f) | v v' (expr.abstr ._ x e) f := expr.abstr _ x (bind e $ var.rec' (expr.var _ bound) (expr.skip ∘ f)) | (v+1) v' (expr.skip e) f := bind e (f ∘ bump) def substitute {γ α β : Type u} {xs : ℕ} (f : α → expr γ β xs) (e : expr γ α xs) : expr γ β xs := bind e (λ v, var.rec_on v f (expr.var _ ∘ var.bound _)) def instantiate {α γ : Type u} {n : ℕ} (v : α) (e : expr γ α (n+1)) : expr γ α n := bind e (var.rec' (expr.var _ $ var.free _ v) (@expr.var _ _ n)) def abstract {γ α : Type u} {n : ℕ} (p : α) [decidable_eq α] (e : expr γ α n) : expr γ α (n + 1) := bind e $ λ v, expr.var _ $ match v with | (var.free ._ x) := if p = x then bound else var.free _ x | (var.bound ._ v) := bump $ var.bound α v end def size {t α : Type u} : ∀ {n : ℕ}, expr t α n → ℕ | n (expr.var ._ _) := 0 | n (expr.app e₀ e₁) := 1 + size e₀ + size e₁ | n (expr.abstr ._ _ e) := 1 + size e | ._ (@expr.skip ._ ._ n e) := 1 + size e def traverse {f : Type u → Type v} [applicative f] {t α α' : Type u} : ∀ {n n'}, (var α n → f (var α' n')) → expr t α n → f (expr t α' n') | n n' f (expr.var ._ v) := expr.var _ <$> f v | n n' f (expr.app e₀ e₁) := expr.app <$> traverse f e₀ <*> traverse f e₁ | n n' f (expr.abstr ._ t e) := expr.abstr _ t <$> traverse (dumping f) e | (nat.succ n) n' f (expr.skip e) := traverse (f ∘ bump) e def dec_t {t α : Type u} {n : ℕ} (α' : Type u) (n' : ℕ) (x : expr t α n) := { y : expr t α' n' // size y ≤ size x } def expr_wf {t α : Type u} {n : ℕ} : has_well_founded (expr t α n) := ⟨ measure size, measure_wf size ⟩ def mk_var {t α α' : Type u} {n n' : ℕ} {v : var α n} (v' : var α' n') : dec_t α' n' (expr.var t v) := ⟨ expr.var _ v', by simp [size] ⟩ def mk_app {t α α' : Type u} {n n' : ℕ} {e₀ e₁ : expr t α n} (e₀' : dec_t α' n' e₀) (e₁' : dec_t α' n' e₁) : dec_t α' n' (expr.app e₀ e₁) := have h : size (expr.app (e₀'.val) (e₁'.val)) ≤ size (expr.app e₀ e₁), begin simp [size], apply add_le_add_left, apply add_le_add e₀'.property e₁'.property end, ⟨ expr.app e₀'.val e₁'.val, h ⟩ def mk_abstr {t α α' : Type u} {n n' : ℕ} {i : t} {e : expr t α (n + 1)} (e' : dec_t α' (n' + 1) e) : dec_t α' n' (expr.abstr n i e) := have size (expr.abstr n' i (e'.val)) ≤ size (expr.abstr n i e), by { simp [size], apply add_le_add_left, apply e'.property }, ⟨ expr.abstr _ i e'.val , this ⟩ def mk_skip {t α α' : Type u} {n n' : ℕ} {e : expr t α n} (e' : dec_t α' n' e) : dec_t α' n' (expr.skip e) := have size (e'.val) ≤ size (expr.skip e), by { simp [size], transitivity size e, { apply e'.property }, { apply nat.le_add_left } }, ⟨ e'.val, this ⟩ def traverse' {f : Type u → Type v} [applicative f] {t α α' : Type u} : ∀ {n n'}, (var α n → f (var α' n')) → ∀ e : expr t α n, f (dec_t α' n' e) | n n' f (expr.var ._ v) := mk_var <$> f v | n n' f (expr.app e₀ e₁) := mk_app <$> traverse' f e₀ <*> traverse' f e₁ | n n' f (expr.abstr ._ i e) := mk_abstr <$> traverse' (dumping f) e | (n+1) n' f (expr.skip e) := mk_skip <$> traverse' (f ∘ bump) e inductive ski (α : Type u) : Type u | S {} : ski | K {} : ski | I {} : ski | var {} : α → ski def S {t α : Type u} {n : ℕ} : expr t (ski α) n := expr.var _ (var.free n ski.S) def K {t α : Type u} {n : ℕ} : expr t (ski α) n := expr.var _ (var.free n ski.K) def I {t α : Type u} {n : ℕ} : expr t (ski α) n := expr.var _ (var.free n ski.I) def expr.ski_var {t α : Type u} {n : ℕ} (v : var α n) : expr t (ski α) n := expr.var _ $ var.rec_on v (var.free _ ∘ ski.var) (var.bound _) def is_free {t α : Type u} {n : ℕ} (e : expr t α (n+1)) : option (dec_t α n e) := traverse' (var.rec' none some) e section well_founded_tactics open tactic well_founded_tactics meta def unfold_size : tactic unit := dunfold_target [``ski.size] {fail_if_unchanged := ff} meta def trivial_expr_size_lt : tactic unit := comp_val <|> `[apply nat.zero_lt_one] <|> `[apply nat.zero_le] <|> assumption <|> `[apply nat.le_refl] <|> (do (`[apply @le_add_of_le_of_nonneg ℕ] >> trivial_expr_size_lt >> trivial_expr_size_lt) <|> (`[apply @le_add_of_nonneg_of_le ℕ] >> trivial_expr_size_lt >> trivial_expr_size_lt) <|> (`[apply nat.lt_add_of_pos_of_le_left] >> trivial_expr_size_lt >> trivial_expr_size_lt) <|> (`[apply nat.lt_add_of_pos_of_le_right] >> trivial_expr_size_lt >> trivial_expr_size_lt)) <|> failed meta def variant_dec : tactic unit := cancel_nat_add_lt >> trivial_nat_lt meta def expr_size_wf_tac : well_founded_tactics := { dec_tac := do clear_internals, unfold_wf_rel, subst_vars, process_lex (unfold_sizeof >> unfold_size >> cancel_nat_add_lt >> trivial_expr_size_lt), return () } end well_founded_tactics lemma nat.lt_add_of_zero_lt_right (a b : nat) (h : 0 < a) : b < a + b := suffices 0 + b < a + b, by {simp at this, assumption}, by {apply nat.add_lt_add_right, assumption} lemma nat.lt_add_of_pos_of_le_left {a b k : ℕ} (h : 0 < k) (h' : a ≤ b) : a < k + b := begin apply lt_of_le_of_lt h', apply nat.lt_add_of_zero_lt_right _ _ h, end lemma nat.lt_add_of_pos_of_le_right {a b k : ℕ} (h : 0 < k) (h' : a ≤ b) : a < b + k := begin rw add_comm, apply nat.lt_add_of_pos_of_le_left h h', end def exists_eq_self {α : Sort u} (x : α) : { y // x = y } := ⟨ _ , rfl ⟩ def abstr_to_ski {α : Type u} : ∀ {n}, expr (ulift empty) (ski α) (n+1) → expr (ulift empty) (ski α) n | n (expr.var ._ v) := var.rec' I (expr.app K ∘ expr.var _) v | n (expr.app e₀ e₁) := expr.app (expr.app S (abstr_to_ski e₀)) (abstr_to_ski e₁) | n (expr.skip e) := expr.app K e def psigma_expr_wf {t α : Type u} : has_well_founded (@psigma ℕ $ expr t α) := ⟨ _ , measure_wf (λ e, size e.2) ⟩ local attribute [instance] psigma_expr_wf open expr def to_ski {t α : Type u} : ∀ {n}, expr t α n → expr (ulift empty) (ski α) n | n (expr.var ._ v) := expr.var _ $ var.rec_on v (var.free _ ∘ ski.var) (var.bound _) | n (expr.app e₀ e₁) := expr.app (to_ski e₀) (to_ski e₁) | n (expr.abstr ._ t e) := match is_free e with | (some ⟨e',h⟩ ) := expr.app K (to_ski e') | none := match exists_eq_self e with | ⟨ (expr.var ._ v), _ ⟩ := var.rec' I (expr.app K ∘ expr.ski_var) v | ⟨ (expr.app e₀ e₁), (h : e = _) ⟩ := expr.app S (expr.app (to_ski (expr.abstr _ t e₀)) (to_ski (expr.abstr _ t e₁))) | ⟨ (expr.abstr ._ t' e'), (h : e = _) ⟩ := abstr_to_ski (to_ski (expr.abstr _ t' e')) | ⟨ (expr.skip e'), (h : e = _) ⟩ := to_ski e' end end | (n+1) (expr.skip e') := expr.skip $ to_ski e' using_well_founded expr_size_wf_tac end ski
1570bbff47a0235958aff7d91777abfd955f775a
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/linear_algebra/dimension.lean
3c68c4b22a9e2ab8d6e791276375bd26bee8985c
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
16,665
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Johannes Hölzl, Sander Dahmen -/ import linear_algebra.basis import set_theory.ordinal /-! # Dimension of modules and vector spaces ## Main definitions * The dimension of a vector space is defined as `vector_space.dim : cardinal`. ## Main statements * `mk_eq_mk_of_basis`: the dimension theorem, any two bases of the same vector space have the same cardinality. * `dim_quotient_add_dim`: if V' is a submodule of V, then dim (V/V') + dim V' = dim V. * `dim_range_add_dim_ker`: the rank-nullity theorem. -/ noncomputable theory universes u u' u'' v v' w w' variables {K : Type u} {V V₂ V₃ V₄ : Type v} variables {ι : Type w} {ι' : Type w'} {η : Type u''} {φ : η → Type u'} -- TODO: relax these universe constraints open_locale classical section vector_space variables [field K] [add_comm_group V] [vector_space K V] include K open submodule function set variables (K V) /-- the dimension of a vector space, defined as a term of type `cardinal` -/ def vector_space.dim : cardinal := cardinal.min (nonempty_subtype.2 (@exists_is_basis K V _ _ _)) (λ b, cardinal.mk b.1) variables {K V} open vector_space section theorem is_basis.le_span (zero_ne_one : (0 : K) ≠ 1) {v : ι → V} {J : set V} (hv : is_basis K v) (hJ : span K J = ⊤) : cardinal.mk (range v) ≤ cardinal.mk J := begin cases le_or_lt cardinal.omega (cardinal.mk J) with oJ oJ, { have := cardinal.mk_range_eq_of_inj (linear_independent.injective zero_ne_one hv.1), let S : J → set ι := λ j, ↑(is_basis.repr hv j).support, let S' : J → set V := λ j, v '' S j, have hs : range v ⊆ ⋃ j, S' j, { intros b hb, rcases mem_range.1 hb with ⟨i, hi⟩, have : span K J ≤ comap hv.repr (finsupp.supported K K (⋃ j, S j)) := span_le.2 (λ j hj x hx, ⟨_, ⟨⟨j, hj⟩, rfl⟩, hx⟩), rw hJ at this, replace : hv.repr (v i) ∈ (finsupp.supported K K (⋃ j, S j)) := this trivial, rw [hv.repr_eq_single, finsupp.mem_supported, finsupp.support_single_ne_zero zero_ne_one.symm] at this, subst b, rcases mem_Union.1 (this (finset.mem_singleton_self _)) with ⟨j, hj⟩, exact mem_Union.2 ⟨j, (mem_image _ _ _).2 ⟨i, hj, rfl⟩⟩ }, refine le_of_not_lt (λ IJ, _), suffices : cardinal.mk (⋃ j, S' j) < cardinal.mk (range v), { exact not_le_of_lt this ⟨set.embedding_of_subset _ _ hs⟩ }, refine lt_of_le_of_lt (le_trans cardinal.mk_Union_le_sum_mk (cardinal.sum_le_sum _ (λ _, cardinal.omega) _)) _, { exact λ j, le_of_lt (cardinal.lt_omega_iff_finite.2 $ finite_image _ (finset.finite_to_set _)) }, { rwa [cardinal.sum_const, cardinal.mul_eq_max oJ (le_refl _), max_eq_left oJ] } }, { rcases exists_finite_card_le_of_finite_of_linear_independent_of_span (cardinal.lt_omega_iff_finite.1 oJ) hv.1.to_subtype_range _ with ⟨fI, hi⟩, { rwa [← cardinal.nat_cast_le, cardinal.finset_card, set.finite.coe_to_finset, cardinal.finset_card, set.finite.coe_to_finset] at hi, }, { rw hJ, apply set.subset_univ } }, end end /-- dimension theorem -/ theorem mk_eq_mk_of_basis {v : ι → V} {v' : ι' → V} (hv : is_basis K v) (hv' : is_basis K v') : cardinal.lift.{w w'} (cardinal.mk ι) = cardinal.lift.{w' w} (cardinal.mk ι') := begin rw ←cardinal.lift_inj.{(max w w') v}, rw [cardinal.lift_lift, cardinal.lift_lift], apply le_antisymm, { convert cardinal.lift_le.{v (max w w')}.2 (hv.le_span zero_ne_one hv'.2), { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_inj (hv.injective zero_ne_one)).symm, }, { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_inj (hv'.injective zero_ne_one)).symm, }, }, { convert cardinal.lift_le.{v (max w w')}.2 (hv'.le_span zero_ne_one hv.2), { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_inj (hv'.injective zero_ne_one)).symm, }, { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_inj (hv.injective zero_ne_one)).symm, }, } end theorem is_basis.mk_range_eq_dim {v : ι → V} (h : is_basis K v) : cardinal.mk (range v) = dim K V := begin have := show ∃ v', dim K V = _, from cardinal.min_eq _ _, rcases this with ⟨v', e⟩, rw e, apply cardinal.lift_inj.1, rw cardinal.mk_range_eq_of_inj (h.injective zero_ne_one), convert @mk_eq_mk_of_basis _ _ _ _ _ _ _ _ _ h v'.property end theorem is_basis.mk_eq_dim {v : ι → V} (h : is_basis K v) : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{v w} (dim K V) := by rw [←h.mk_range_eq_dim, cardinal.mk_range_eq_of_inj (h.injective zero_ne_one)] variables [add_comm_group V₂] [vector_space K V₂] /-- Two linearly equivalent vector spaces have the same dimension. -/ theorem linear_equiv.dim_eq (f : V ≃ₗ[K] V₂) : dim K V = dim K V₂ := by letI := classical.dec_eq V; letI := classical.dec_eq V₂; exact let ⟨b, hb⟩ := exists_is_basis K V in cardinal.lift_inj.1 $ hb.mk_eq_dim.symm.trans (f.is_basis hb).mk_eq_dim @[simp] lemma dim_bot : dim K (⊥ : submodule K V) = 0 := by letI := classical.dec_eq V; rw [← cardinal.lift_inj, ← (@is_basis_empty_bot pempty K V _ _ _ not_nonempty_pempty).mk_eq_dim, cardinal.mk_pempty] @[simp] lemma dim_top : dim K (⊤ : submodule K V) = dim K V := linear_equiv.dim_eq (linear_equiv.of_top _ rfl) lemma dim_of_field (K : Type*) [field K] : dim K K = 1 := by rw [←cardinal.lift_inj, ← (@is_basis_singleton_one punit K _ _).mk_eq_dim, cardinal.mk_punit] lemma dim_span {v : ι → V} (hv : linear_independent K v) : dim K ↥(span K (range v)) = cardinal.mk (range v) := by rw [←cardinal.lift_inj, ← (is_basis_span hv).mk_eq_dim, cardinal.mk_range_eq_of_inj (@linear_independent.injective ι K V v _ _ _ zero_ne_one hv)] lemma dim_span_set {s : set V} (hs : linear_independent K (λ x, x : s → V)) : dim K ↥(span K s) = cardinal.mk s := by rw [← @set_of_mem_eq _ s, ← subtype.val_range]; exact dim_span hs lemma dim_span_le (s : set V) : dim K (span K s) ≤ cardinal.mk s := begin classical, rcases exists_linear_independent linear_independent_empty (set.empty_subset s) with ⟨b, hb, _, hsb, hlib⟩, have hsab : span K s = span K b, from span_eq_of_le _ hsb (span_le.2 (λ x hx, subset_span (hb hx))), convert cardinal.mk_le_mk_of_subset hb, rw [hsab, dim_span_set hlib] end lemma dim_span_of_finset (s : finset V) : dim K (span K (↑s : set V)) < cardinal.omega := calc dim K (span K (↑s : set V)) ≤ cardinal.mk (↑s : set V) : dim_span_le ↑s ... = s.card : by rw ←cardinal.finset_card ... < cardinal.omega : cardinal.nat_lt_omega _ theorem dim_prod : dim K (V × V₂) = dim K V + dim K V₂ := begin letI := classical.dec_eq V, letI := classical.dec_eq V₂, rcases exists_is_basis K V with ⟨b, hb⟩, rcases exists_is_basis K V₂ with ⟨c, hc⟩, rw [← cardinal.lift_inj, ← @is_basis.mk_eq_dim K (V × V₂) _ _ _ _ _ (is_basis_inl_union_inr hb hc), cardinal.lift_add, cardinal.lift_mk, ← hb.mk_eq_dim, ← hc.mk_eq_dim, cardinal.lift_mk, cardinal.lift_mk, cardinal.add_def (ulift b) (ulift c)], exact cardinal.lift_inj.1 (cardinal.lift_mk_eq.2 ⟨equiv.ulift.trans (equiv.sum_congr (@equiv.ulift b) (@equiv.ulift c)).symm ⟩), end theorem dim_quotient_add_dim (p : submodule K V) : dim K p.quotient + dim K p = dim K V := by classical; exact let ⟨f⟩ := quotient_prod_linear_equiv p in dim_prod.symm.trans f.dim_eq theorem dim_quotient_le (p : submodule K V) : dim K p.quotient ≤ dim K V := by { rw ← dim_quotient_add_dim p, exact cardinal.le_add_right _ _ } /-- rank-nullity theorem -/ theorem dim_range_add_dim_ker (f : V →ₗ[K] V₂) : dim K f.range + dim K f.ker = dim K V := begin haveI := λ (p : submodule K V), classical.dec_eq p.quotient, rw [← f.quot_ker_equiv_range.dim_eq, dim_quotient_add_dim] end lemma dim_range_le (f : V →ₗ[K] V₂) : dim K f.range ≤ dim K V := by rw ← dim_range_add_dim_ker f; exact le_add_right (le_refl _) lemma dim_map_le (f : V →ₗ V₂) (p : submodule K V) : dim K (p.map f) ≤ dim K p := begin have h := dim_range_le (f.comp (submodule.subtype p)), rwa [linear_map.range_comp, range_subtype] at h, end lemma dim_range_of_surjective (f : V →ₗ[K] V₂) (h : surjective f) : dim K f.range = dim K V₂ := begin refine linear_equiv.dim_eq (linear_equiv.of_bijective (submodule.subtype _) _ _), exact linear_map.ker_eq_bot.2 subtype.val_injective, rwa [range_subtype, linear_map.range_eq_top] end lemma dim_eq_surjective (f : V →ₗ[K] V₂) (h : surjective f) : dim K V = dim K V₂ + dim K f.ker := by rw [← dim_range_add_dim_ker f, ← dim_range_of_surjective f h] lemma dim_le_surjective (f : V →ₗ[K] V₂) (h : surjective f) : dim K V₂ ≤ dim K V := by rw [dim_eq_surjective f h]; refine le_add_right (le_refl _) lemma dim_eq_injective (f : V →ₗ[K] V₂) (h : injective f) : dim K V = dim K f.range := by rw [← dim_range_add_dim_ker f, linear_map.ker_eq_bot.2 h]; simp [dim_bot] lemma dim_submodule_le (s : submodule K V) : dim K s ≤ dim K V := by { rw ← dim_quotient_add_dim s, exact cardinal.le_add_left _ _ } lemma dim_le_injective (f : V →ₗ[K] V₂) (h : injective f) : dim K V ≤ dim K V₂ := by { rw [dim_eq_injective f h], exact dim_submodule_le _ } lemma dim_le_of_submodule (s t : submodule K V) (h : s ≤ t) : dim K s ≤ dim K t := dim_le_injective (of_le h) $ assume ⟨x, hx⟩ ⟨y, hy⟩ eq, subtype.eq $ show x = y, from subtype.ext.1 eq section variables [add_comm_group V₃] [vector_space K V₃] variables [add_comm_group V₄] [vector_space K V₄] open linear_map /-- This is mostly an auxiliary lemma for `dim_sup_add_dim_inf_eq`. -/ lemma dim_add_dim_split (db : V₃ →ₗ[K] V) (eb : V₄ →ₗ[K] V) (cd : V₂ →ₗ[K] V₃) (ce : V₂ →ₗ[K] V₄) (hde : ⊤ ≤ db.range ⊔ eb.range) (hgd : ker cd = ⊥) (eq : db.comp cd = eb.comp ce) (eq₂ : ∀d e, db d = eb e → (∃c, cd c = d ∧ ce c = e)) : dim K V + dim K V₂ = dim K V₃ + dim K V₄ := have hf : surjective (coprod db eb), begin refine (range_eq_top.1 $ top_unique $ _), rwa [← map_top, ← prod_top, map_coprod_prod] end, begin conv {to_rhs, rw [← dim_prod, dim_eq_surjective _ hf] }, congr' 1, apply linear_equiv.dim_eq, fapply linear_equiv.of_bijective, { refine cod_restrict _ (prod cd (- ce)) _, { assume c, simp only [add_eq_zero_iff_eq_neg, prod_apply, mem_ker, coprod_apply, neg_neg, map_neg, neg_apply], exact linear_map.ext_iff.1 eq c } }, { rw [ker_cod_restrict, ker_prod, hgd, bot_inf_eq] }, { rw [eq_top_iff, range_cod_restrict, ← map_le_iff_le_comap, map_top, range_subtype], rintros ⟨d, e⟩, have h := eq₂ d (-e), simp only [add_eq_zero_iff_eq_neg, prod_apply, mem_ker, mem_coe, prod.mk.inj_iff, coprod_apply, map_neg, neg_apply, linear_map.mem_range] at ⊢ h, assume hde, rcases h hde with ⟨c, h₁, h₂⟩, refine ⟨c, h₁, _⟩, rw [h₂, _root_.neg_neg] } end lemma dim_sup_add_dim_inf_eq (s t : submodule K V) : dim K (s ⊔ t : submodule K V) + dim K (s ⊓ t : submodule K V) = dim K s + dim K t := dim_add_dim_split (of_le le_sup_left) (of_le le_sup_right) (of_le inf_le_left) (of_le inf_le_right) begin rw [← map_le_map_iff' (ker_subtype $ s ⊔ t), map_sup, map_top, ← linear_map.range_comp, ← linear_map.range_comp, subtype_comp_of_le, subtype_comp_of_le, range_subtype, range_subtype, range_subtype], exact le_refl _ end (ker_of_le _ _ _) begin ext ⟨x, hx⟩, refl end begin rintros ⟨b₁, hb₁⟩ ⟨b₂, hb₂⟩ eq, have : b₁ = b₂ := congr_arg subtype.val eq, subst this, exact ⟨⟨b₁, hb₁, hb₂⟩, rfl, rfl⟩ end lemma dim_add_le_dim_add_dim (s t : submodule K V) : dim K (s ⊔ t : submodule K V) ≤ dim K s + dim K t := by rw [← dim_sup_add_dim_inf_eq]; exact le_add_right (le_refl _) end section fintype variable [fintype η] variables [∀i, add_comm_group (φ i)] [∀i, vector_space K (φ i)] open linear_map lemma dim_pi : vector_space.dim K (Πi, φ i) = cardinal.sum (λi, vector_space.dim K (φ i)) := begin choose b hb using assume i, exists_is_basis K (φ i), have : is_basis K (λ (ji : Σ j, b j), std_basis K (λ j, φ j) ji.fst ji.snd.val), by apply pi.is_basis_std_basis _ hb, rw [←cardinal.lift_inj, ← this.mk_eq_dim], simp [λ i, (hb i).mk_range_eq_dim.symm, cardinal.sum_mk] end lemma dim_fun {V η : Type u} [fintype η] [add_comm_group V] [vector_space K V] : vector_space.dim K (η → V) = fintype.card η * vector_space.dim K V := by rw [dim_pi, cardinal.sum_const, cardinal.fintype_card] lemma dim_fun_eq_lift_mul : vector_space.dim K (η → V) = (fintype.card η : cardinal.{max u'' v}) * cardinal.lift.{v u''} (vector_space.dim K V) := by rw [dim_pi, cardinal.sum_const_eq_lift_mul, cardinal.fintype_card, cardinal.lift_nat_cast] lemma dim_fun' : vector_space.dim K (η → K) = fintype.card η := by rw [dim_fun_eq_lift_mul, dim_of_field K, cardinal.lift_one, mul_one, cardinal.nat_cast_inj] lemma dim_fin_fun (n : ℕ) : dim K (fin n → K) = n := by simp [dim_fun'] end fintype lemma exists_mem_ne_zero_of_ne_bot {s : submodule K V} (h : s ≠ ⊥) : ∃ b : V, b ∈ s ∧ b ≠ 0 := begin classical, by_contradiction hex, have : ∀x∈s, (x:V) = 0, { simpa only [not_exists, not_and, not_not, ne.def] using hex }, exact (h $ bot_unique $ assume s hs, (submodule.mem_bot K).2 $ this s hs) end lemma exists_mem_ne_zero_of_dim_pos {s : submodule K V} (h : vector_space.dim K s > 0) : ∃ b : V, b ∈ s ∧ b ≠ 0 := exists_mem_ne_zero_of_ne_bot $ assume eq, by rw [(>), eq, dim_bot] at h; exact lt_irrefl _ h lemma exists_is_basis_fintype (h : dim K V < cardinal.omega) : ∃ s : (set V), (is_basis K (subtype.val : s → V)) ∧ nonempty (fintype s) := begin cases exists_is_basis K V with s hs, rw [←cardinal.lift_lt, ← is_basis.mk_eq_dim hs, cardinal.lift_lt, cardinal.lt_omega_iff_fintype] at h, exact ⟨s, hs, h⟩ end section rank /-- `rank f` is the rank of a `linear_map f`, defined as the dimension of `f.range`. -/ def rank (f : V →ₗ[K] V₂) : cardinal := dim K f.range lemma rank_le_domain (f : V →ₗ[K] V₂) : rank f ≤ dim K V := by rw [← dim_range_add_dim_ker f]; exact le_add_right (le_refl _) lemma rank_le_range (f : V →ₗ[K] V₂) : rank f ≤ dim K V₂ := dim_submodule_le _ lemma rank_add_le (f g : V →ₗ[K] V₂) : rank (f + g) ≤ rank f + rank g := calc rank (f + g) ≤ dim K (f.range ⊔ g.range : submodule K V₂) : begin refine dim_le_of_submodule _ _ _, exact (linear_map.range_le_iff_comap.2 $ eq_top_iff'.2 $ assume x, show f x + g x ∈ (f.range ⊔ g.range : submodule K V₂), from mem_sup.2 ⟨_, mem_image_of_mem _ (mem_univ _), _, mem_image_of_mem _ (mem_univ _), rfl⟩) end ... ≤ rank f + rank g : dim_add_le_dim_add_dim _ _ @[simp] lemma rank_zero : rank (0 : V →ₗ[K] V₂) = 0 := by rw [rank, linear_map.range_zero, dim_bot] lemma rank_finset_sum_le {η} (s : finset η) (f : η → V →ₗ[K] V₂) : rank (s.sum f) ≤ s.sum (λ d, rank (f d)) := @finset.sum_hom_rel _ _ _ _ _ (λa b, rank a ≤ b) f (λ d, rank (f d)) s (le_of_eq rank_zero) (λ i g c h, le_trans (rank_add_le _ _) (add_le_add_left' h)) variables [add_comm_group V₃] [vector_space K V₃] lemma rank_comp_le1 (g : V →ₗ[K] V₂) (f : V₂ →ₗ[K] V₃) : rank (f.comp g) ≤ rank f := begin refine dim_le_of_submodule _ _ _, rw [linear_map.range_comp], exact image_subset _ (subset_univ _) end lemma rank_comp_le2 (g : V →ₗ[K] V₂) (f : V₂ →ₗ V₃) : rank (f.comp g) ≤ rank g := by rw [rank, rank, linear_map.range_comp]; exact dim_map_le _ _ end rank end vector_space section unconstrained_universes variables {E : Type v'} variables [field K] [add_comm_group V] [vector_space K V] [add_comm_group E] [vector_space K E] open vector_space /-- Version of linear_equiv.dim_eq without universe constraints. -/ theorem linear_equiv.dim_eq_lift (f : V ≃ₗ[K] E) : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K E) := begin cases exists_is_basis K V with b hb, rw [← cardinal.lift_inj.1 hb.mk_eq_dim, ← (f.is_basis hb).mk_eq_dim, cardinal.lift_mk], end end unconstrained_universes
62a67141e47c0c1a2df95b19825a7df9b766e98c
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/coe9.lean
d0867e25ee7e64ea84b2b6b3f59ed59987bcf5a4
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
768
lean
import data.nat open nat constant list.{l} : Type.{l} → Type.{l} constant vector.{l} : Type.{l} → nat → Type.{l} constant matrix.{l} : Type.{l} → nat → nat → Type.{l} constant length : Pi {A : Type}, list A → nat constant list_to_vec {A : Type} (l : list A) : vector A (length l) constant to_row {A : Type} {n : nat} : vector A n → matrix A 1 n constant to_col {A : Type} {n : nat} : vector A n → matrix A n 1 constant to_list {A : Type} {n : nat} : vector A n → list A coercion to_row coercion to_col coercion list_to_vec coercion to_list constant f {A : Type} {n : nat} (M : matrix A n 1) : nat constant g {A : Type} {n : nat} (M : matrix A 1 n) : nat constant v : vector nat 10 constant l : list nat check f v check g v check f l check g l
3a8d65eb7dff0f4680436f8589bfcb75f58373e3
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/run/tele_eq.lean
1797756be9f4300892f97f9d0081c68fd9e30328
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
541
lean
constant A₁ : Type constant A₂ : A₁ → Type constant A₃ : Π (a₁ : A₁), A₂ a₁ → Type structure foo := mk :: (a₁ : A₁) (a₂ : A₂ a₁) (a₃ : A₃ a₁ a₂) theorem foo.eq {a₁ b₁ : A₁} {a₂ : A₂ a₁} {b₂ : A₂ b₁} {a₃ : A₃ a₁ a₂} {b₃ : A₃ b₁ b₂} (H₁ : a₁ = b₁) (H₂ : a₂ == b₂) (H₃ : a₃ == b₃) : foo.mk a₁ a₂ a₃ = foo.mk b₁ b₂ b₃ := begin cases H₁, cases H₂, cases H₃, apply rfl end print definition foo.eq
271c05d74153ba38bdaa9aea8707b56e32b4de56
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/squarefree.lean
ef1e9064d8728d1dac9af00435df044333401dcf
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,972
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.unique_factorization_domain import Mathlib.ring_theory.int.basic import Mathlib.number_theory.divisors import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Squarefree elements of monoids An element of a monoid is squarefree when it is not divisible by any squares except the squares of units. ## Main Definitions - `squarefree r` indicates that `r` is only divisible by `x * x` if `x` is a unit. ## Main Results - `multiplicity.squarefree_iff_multiplicity_le_one`: `x` is `squarefree` iff for every `y`, either `multiplicity y x ≤ 1` or `is_unit y`. - `unique_factorization_monoid.squarefree_iff_nodup_factors`: A nonzero element `x` of a unique factorization monoid is squarefree iff `factors x` has no duplicate factors. - `nat.squarefree_iff_nodup_factors`: A positive natural number `x` is squarefree iff the list `factors x` has no duplicate factors. ## Tags squarefree, multiplicity -/ /-- An element of a monoid is squarefree if the only squares that divide it are the squares of units. -/ def squarefree {R : Type u_1} [monoid R] (r : R) := ∀ (x : R), x * x ∣ r → is_unit x @[simp] theorem is_unit.squarefree {R : Type u_1} [comm_monoid R] {x : R} (h : is_unit x) : squarefree x := fun (y : R) (hdvd : y * y ∣ x) => is_unit_of_mul_is_unit_left (is_unit_of_dvd_unit hdvd h) @[simp] theorem squarefree_one {R : Type u_1} [comm_monoid R] : squarefree 1 := is_unit.squarefree is_unit_one @[simp] theorem not_squarefree_zero {R : Type u_1} [monoid_with_zero R] [nontrivial R] : ¬squarefree 0 := sorry @[simp] theorem irreducible.squarefree {R : Type u_1} [comm_monoid R] {x : R} (h : irreducible x) : squarefree x := sorry @[simp] theorem prime.squarefree {R : Type u_1} [comm_cancel_monoid_with_zero R] {x : R} (h : prime x) : squarefree x := irreducible.squarefree (irreducible_of_prime h) theorem squarefree_of_dvd_of_squarefree {R : Type u_1} [comm_monoid R] {x : R} {y : R} (hdvd : x ∣ y) (hsq : squarefree y) : squarefree x := fun (a : R) (h : a * a ∣ x) => hsq a (dvd.trans h hdvd) namespace multiplicity theorem squarefree_iff_multiplicity_le_one {R : Type u_1} [comm_monoid R] [DecidableRel has_dvd.dvd] (r : R) : squarefree r ↔ ∀ (x : R), multiplicity x r ≤ 1 ∨ is_unit x := sorry end multiplicity namespace unique_factorization_monoid theorem squarefree_iff_nodup_factors {R : Type u_1} [comm_cancel_monoid_with_zero R] [nontrivial R] [unique_factorization_monoid R] [normalization_monoid R] [DecidableEq R] {x : R} (x0 : x ≠ 0) : squarefree x ↔ multiset.nodup (factors x) := sorry theorem dvd_pow_iff_dvd_of_squarefree {R : Type u_1} [comm_cancel_monoid_with_zero R] [nontrivial R] [unique_factorization_monoid R] [normalization_monoid R] {x : R} {y : R} {n : ℕ} (hsq : squarefree x) (h0 : n ≠ 0) : x ∣ y ^ n ↔ x ∣ y := sorry end unique_factorization_monoid namespace nat theorem squarefree_iff_nodup_factors {n : ℕ} (h0 : n ≠ 0) : squarefree n ↔ list.nodup (factors n) := sorry protected instance squarefree.decidable_pred : decidable_pred squarefree := sorry theorem divisors_filter_squarefree {n : ℕ} (h0 : n ≠ 0) : finset.val (finset.filter squarefree (divisors n)) = multiset.map (fun (x : finset ℕ) => multiset.prod (finset.val x)) (finset.val (finset.powerset (multiset.to_finset (unique_factorization_monoid.factors n)))) := sorry theorem sum_divisors_filter_squarefree {n : ℕ} (h0 : n ≠ 0) {α : Type u_1} [add_comm_monoid α] {f : ℕ → α} : (finset.sum (finset.filter squarefree (divisors n)) fun (i : ℕ) => f i) = finset.sum (finset.powerset (multiset.to_finset (unique_factorization_monoid.factors n))) fun (i : finset ℕ) => f (multiset.prod (finset.val i)) := sorry
f0b0fd8c426c862b7cb68f565d53935f5957c93d
798dd332c1ad790518589a09bc82459fb12e5156
/order/bounded_lattice.lean
a90a2601b34e3302be32f5b006fd7876d6e82224
[ "Apache-2.0" ]
permissive
tobiasgrosser/mathlib
b040b7eb42d5942206149371cf92c61404de3c31
120635628368ec261e031cefc6d30e0304088b03
refs/heads/master
1,644,803,442,937
1,536,663,752,000
1,536,663,907,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,723
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Defines bounded lattice type class hierarchy. Includes the Prop and fun instances. -/ import order.lattice data.option tactic.pi_instances set_option old_structure_cmd true universes u v namespace lattice variable {α : Type u} /-- Typeclass for the `⊤` (`\top`) notation -/ class has_top (α : Type u) := (top : α) /-- Typeclass for the `⊥` (`\bot`) notation -/ class has_bot (α : Type u) := (bot : α) notation `⊤` := has_top.top _ notation `⊥` := has_bot.bot _ /-- An `order_top` is a partial order with a maximal element. (We could state this on preorders, but then it wouldn't be unique so distinguishing one would seem odd.) -/ class order_top (α : Type u) extends has_top α, partial_order α := (le_top : ∀ a : α, a ≤ ⊤) section order_top variables [order_top α] {a : α} @[simp] theorem le_top : a ≤ ⊤ := order_top.le_top a theorem top_unique (h : ⊤ ≤ a) : a = ⊤ := le_antisymm le_top h -- TODO: delete in favor of the next? theorem eq_top_iff : a = ⊤ ↔ ⊤ ≤ a := ⟨assume eq, eq.symm ▸ le_refl ⊤, top_unique⟩ @[simp] theorem top_le_iff : ⊤ ≤ a ↔ a = ⊤ := ⟨top_unique, λ h, h.symm ▸ le_refl ⊤⟩ @[simp] theorem not_top_lt : ¬ ⊤ < a := assume h, lt_irrefl a (lt_of_le_of_lt le_top h) end order_top theorem order_top.ext_top {α} {A B : order_top α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : (by haveI := A; exact ⊤ : α) = ⊤ := top_unique $ by rw ← H; apply le_top theorem order_top.ext {α} {A B : order_top α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin haveI this := partial_order.ext H, have tt := order_top.ext_top H, cases A; cases B; injection this; congr' end /-- An `order_bot` is a partial order with a minimal element. (We could state this on preorders, but then it wouldn't be unique so distinguishing one would seem odd.) -/ class order_bot (α : Type u) extends has_bot α, partial_order α := (bot_le : ∀ a : α, ⊥ ≤ a) section order_bot variables [order_bot α] {a : α} @[simp] theorem bot_le : ⊥ ≤ a := order_bot.bot_le a theorem bot_unique (h : a ≤ ⊥) : a = ⊥ := le_antisymm h bot_le -- TODO: delete? theorem eq_bot_iff : a = ⊥ ↔ a ≤ ⊥ := ⟨assume eq, eq.symm ▸ le_refl ⊥, bot_unique⟩ @[simp] theorem le_bot_iff : a ≤ ⊥ ↔ a = ⊥ := ⟨bot_unique, assume h, h.symm ▸ le_refl ⊥⟩ @[simp] theorem not_lt_bot : ¬ a < ⊥ := assume h, lt_irrefl a (lt_of_lt_of_le h bot_le) theorem neq_bot_of_le_neq_bot {a b : α} (hb : b ≠ ⊥) (hab : b ≤ a) : a ≠ ⊥ := assume ha, hb $ bot_unique $ ha ▸ hab end order_bot theorem order_bot.ext_bot {α} {A B : order_bot α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : (by haveI := A; exact ⊥ : α) = ⊥ := bot_unique $ by rw ← H; apply bot_le theorem order_bot.ext {α} {A B : order_bot α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin haveI this := partial_order.ext H, have tt := order_bot.ext_bot H, cases A; cases B; injection this; congr' end /-- A `semilattice_sup_top` is a semilattice with top and join. -/ class semilattice_sup_top (α : Type u) extends order_top α, semilattice_sup α section semilattice_sup_top variables [semilattice_sup_top α] {a : α} @[simp] theorem top_sup_eq : ⊤ ⊔ a = ⊤ := sup_of_le_left le_top @[simp] theorem sup_top_eq : a ⊔ ⊤ = ⊤ := sup_of_le_right le_top end semilattice_sup_top /-- A `semilattice_sup_bot` is a semilattice with bottom and join. -/ class semilattice_sup_bot (α : Type u) extends order_bot α, semilattice_sup α section semilattice_sup_bot variables [semilattice_sup_bot α] {a b : α} @[simp] theorem bot_sup_eq : ⊥ ⊔ a = a := sup_of_le_right bot_le @[simp] theorem sup_bot_eq : a ⊔ ⊥ = a := sup_of_le_left bot_le @[simp] theorem sup_eq_bot_iff : a ⊔ b = ⊥ ↔ (a = ⊥ ∧ b = ⊥) := by rw [eq_bot_iff, sup_le_iff]; simp end semilattice_sup_bot instance nat.semilattice_sup_bot : semilattice_sup_bot ℕ := { bot := 0, bot_le := nat.zero_le, .. nat.distrib_lattice } /-- A `semilattice_inf_top` is a semilattice with top and meet. -/ class semilattice_inf_top (α : Type u) extends order_top α, semilattice_inf α section semilattice_inf_top variables [semilattice_inf_top α] {a b : α} @[simp] theorem top_inf_eq : ⊤ ⊓ a = a := inf_of_le_right le_top @[simp] theorem inf_top_eq : a ⊓ ⊤ = a := inf_of_le_left le_top @[simp] theorem inf_eq_top_iff : a ⊓ b = ⊤ ↔ (a = ⊤ ∧ b = ⊤) := by rw [eq_top_iff, le_inf_iff]; simp end semilattice_inf_top /-- A `semilattice_inf_bot` is a semilattice with bottom and meet. -/ class semilattice_inf_bot (α : Type u) extends order_bot α, semilattice_inf α section semilattice_inf_bot variables [semilattice_inf_bot α] {a : α} @[simp] theorem bot_inf_eq : ⊥ ⊓ a = ⊥ := inf_of_le_left bot_le @[simp] theorem inf_bot_eq : a ⊓ ⊥ = ⊥ := inf_of_le_right bot_le end semilattice_inf_bot /- Bounded lattices -/ /-- A bounded lattice is a lattice with a top and bottom element, denoted `⊤` and `⊥` respectively. This allows for the interpretation of all finite suprema and infima, taking `inf ∅ = ⊤` and `sup ∅ = ⊥`. -/ class bounded_lattice (α : Type u) extends lattice α, order_top α, order_bot α instance semilattice_inf_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_top α := { le_top := assume x, @le_top α _ x, ..bl } instance semilattice_inf_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_bot α := { bot_le := assume x, @bot_le α _ x, ..bl } instance semilattice_sup_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_top α := { le_top := assume x, @le_top α _ x, ..bl } instance semilattice_sup_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_bot α := { bot_le := assume x, @bot_le α _ x, ..bl } theorem bounded_lattice.ext {α} {A B : bounded_lattice α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin haveI H1 : @bounded_lattice.to_lattice α A = @bounded_lattice.to_lattice α B := lattice.ext H, haveI H2 := order_bot.ext H, haveI H3 : @bounded_lattice.to_order_top α A = @bounded_lattice.to_order_top α B := order_top.ext H, have tt := order_bot.ext_bot H, cases A; cases B; injection H1; injection H2; injection H3; congr' end /-- A bounded distributive lattice is exactly what it sounds like. -/ class bounded_distrib_lattice α extends distrib_lattice α, bounded_lattice α lemma inf_eq_bot_iff_le_compl {α : Type u} [bounded_distrib_lattice α] {a b c : α} (h₁ : b ⊔ c = ⊤) (h₂ : b ⊓ c = ⊥) : a ⊓ b = ⊥ ↔ a ≤ c := ⟨assume : a ⊓ b = ⊥, calc a ≤ a ⊓ (b ⊔ c) : by simp [h₁] ... = (a ⊓ b) ⊔ (a ⊓ c) : by simp [inf_sup_left] ... ≤ c : by simp [this, inf_le_right], assume : a ≤ c, bot_unique $ calc a ⊓ b ≤ b ⊓ c : by rw [inf_comm]; exact inf_le_inf (le_refl _) this ... = ⊥ : h₂⟩ /- Prop instance -/ instance bounded_lattice_Prop : bounded_lattice Prop := { lattice.bounded_lattice . le := λa b, a → b, le_refl := assume _, id, le_trans := assume a b c f g, g ∘ f, le_antisymm := assume a b Hab Hba, propext ⟨Hab, Hba⟩, sup := or, le_sup_left := @or.inl, le_sup_right := @or.inr, sup_le := assume a b c, or.rec, inf := and, inf_le_left := @and.left, inf_le_right := @and.right, le_inf := assume a b c Hab Hac Ha, and.intro (Hab Ha) (Hac Ha), top := true, le_top := assume a Ha, true.intro, bot := false, bot_le := @false.elim } section logic variable [preorder α] theorem monotone_and {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone (λx, p x ∧ q x) := assume a b h, and.imp (m_p h) (m_q h) -- Note: by finish [monotone] doesn't work theorem monotone_or {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone (λx, p x ∨ q x) := assume a b h, or.imp (m_p h) (m_q h) end logic /- Function lattices -/ /- TODO: * build up the lattice hierarchy for `fun`-functor piecewise. semilattic_*, bounded_lattice, lattice ... * can this be generalized to the dependent function space? -/ instance pi.bounded_lattice {α : Type u} {β : Type v} [bounded_lattice β] : bounded_lattice (α → β) := by pi_instance end lattice def with_bot (α : Type*) := option α namespace with_bot variable {α : Type u} open lattice instance : has_coe_t α (with_bot α) := ⟨some⟩ instance has_bot : has_bot (with_bot α) := ⟨none⟩ lemma none_eq_bot : (none : with_bot α) = (⊥ : with_bot α) := rfl lemma some_eq_coe (a : α) : (some a : with_bot α) = (↑a : with_bot α) := rfl theorem coe_eq_coe {a b : α} : (a : with_bot α) = b ↔ a = b := by rw [← option.some.inj_eq a b]; refl instance partial_order [partial_order α] : partial_order (with_bot α) := { le := λ o₁ o₂ : option α, ∀ a ∈ o₁, ∃ b ∈ o₂, a ≤ b, le_refl := λ o a ha, ⟨a, ha, le_refl _⟩, le_trans := λ o₁ o₂ o₃ h₁ h₂ a ha, let ⟨b, hb, ab⟩ := h₁ a ha, ⟨c, hc, bc⟩ := h₂ b hb in ⟨c, hc, le_trans ab bc⟩, le_antisymm := λ o₁ o₂ h₁ h₂, begin cases o₁ with a, { cases o₂ with b, {refl}, rcases h₂ b rfl with ⟨_, ⟨⟩, _⟩ }, { rcases h₁ a rfl with ⟨b, ⟨⟩, h₁'⟩, rcases h₂ b rfl with ⟨_, ⟨⟩, h₂'⟩, rw le_antisymm h₁' h₂' } end } instance order_bot [partial_order α] : order_bot (with_bot α) := { bot_le := λ a a' h, option.no_confusion h, ..with_bot.partial_order, ..with_bot.has_bot } @[simp] theorem coe_le_coe [partial_order α] {a b : α} : (a : with_bot α) ≤ b ↔ a ≤ b := ⟨λ h, by rcases h a rfl with ⟨_, ⟨⟩, h⟩; exact h, λ h a' e, option.some_inj.1 e ▸ ⟨b, rfl, h⟩⟩ @[simp] theorem some_le_some [partial_order α] {a b : α} : @has_le.le (with_bot α) _ (some a) (some b) ↔ a ≤ b := coe_le_coe theorem coe_le [partial_order α] {a b : α} : ∀ {o : option α}, b ∈ o → ((a : with_bot α) ≤ o ↔ a ≤ b) | _ rfl := coe_le_coe @[simp] theorem some_lt_some [partial_order α] {a b : α} : @has_lt.lt (with_bot α) _ (some a) (some b) ↔ a < b := (and_congr some_le_some (not_congr some_le_some)) .trans lt_iff_le_not_le.symm lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_bot α) < b ↔ a < b := some_lt_some lemma bot_lt_some [partial_order α] (a : α) : (⊥ : with_bot α) < some a := lt_of_le_of_ne bot_le (λ h, option.no_confusion h) lemma bot_lt_coe [partial_order α] (a : α) : (⊥ : with_bot α) < a := bot_lt_some a instance linear_order [linear_order α] : linear_order (with_bot α) := { le_total := λ o₁ o₂, begin cases o₁ with a, {exact or.inl bot_le}, cases o₂ with b, {exact or.inr bot_le}, simp [le_total] end, ..with_bot.partial_order } instance decidable_linear_order [decidable_linear_order α] : decidable_linear_order (with_bot α) := { decidable_le := λ a b, begin cases a with a, { exact is_true bot_le }, cases b with b, { exact is_false (mt (le_antisymm bot_le) (by simp)) }, { exact decidable_of_iff _ some_le_some } end, ..with_bot.linear_order } instance semilattice_sup [semilattice_sup α] : semilattice_sup_bot (with_bot α) := { sup := option.lift_or_get (⊔), le_sup_left := λ o₁ o₂ a ha, by cases ha; cases o₂; simp [option.lift_or_get], le_sup_right := λ o₁ o₂ a ha, by cases ha; cases o₁; simp [option.lift_or_get], sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases o₁ with b; cases o₂ with c; cases ha, { exact h₂ a rfl }, { exact h₁ a rfl }, { rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩, simp at h₂, exact ⟨d, rfl, sup_le h₁' h₂⟩ } end, ..with_bot.order_bot } instance semilattice_inf [semilattice_inf α] : semilattice_inf_bot (with_bot α) := { inf := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊓ b)), inf_le_left := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, inf_le_left⟩ end, inf_le_right := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, inf_le_right⟩ end, le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases ha, rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩, rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩, exact ⟨_, rfl, le_inf ab ac⟩ end, ..with_bot.order_bot } instance lattice [lattice α] : lattice (with_bot α) := { ..with_bot.semilattice_sup, ..with_bot.semilattice_inf } theorem lattice_eq_DLO [decidable_linear_order α] : lattice.lattice_of_decidable_linear_order = @with_bot.lattice α _ := lattice.ext $ λ x y, iff.rfl theorem sup_eq_max [decidable_linear_order α] (x y : with_bot α) : x ⊔ y = max x y := by rw [← sup_eq_max, lattice_eq_DLO] theorem inf_eq_min [decidable_linear_order α] (x y : with_bot α) : x ⊓ y = min x y := by rw [← inf_eq_min, lattice_eq_DLO] instance order_top [order_top α] : order_top (with_bot α) := { top := some ⊤, le_top := λ o a ha, by cases ha; exact ⟨_, rfl, le_top⟩, ..with_bot.partial_order } instance bounded_lattice [bounded_lattice α] : bounded_lattice (with_bot α) := { ..with_bot.lattice, ..with_bot.order_top, ..with_bot.order_bot } lemma well_founded_lt [partial_order α] (h : well_founded ((<) : α → α → Prop)) : well_founded ((<) : with_bot α → with_bot α → Prop) := have acc_bot : acc ((<) : with_bot α → with_bot α → Prop) ⊥ := acc.intro _ (λ a ha, (not_le_of_gt ha bot_le).elim), ⟨λ a, option.rec_on a acc_bot (λ a, acc.intro _ (λ b, option.rec_on b (λ _, acc_bot) (λ b, well_founded.induction h b (show ∀ b : α, (∀ c, c < b → (c : with_bot α) < a → acc ((<) : with_bot α → with_bot α → Prop) c) → (b : with_bot α) < a → acc ((<) : with_bot α → with_bot α → Prop) b, from λ b ih hba, acc.intro _ (λ c, option.rec_on c (λ _, acc_bot) (λ c hc, ih _ (some_lt_some.1 hc) (lt_trans hc hba)))))))⟩ instance densely_ordered [partial_order α] [densely_ordered α] [no_bot_order α] : densely_ordered (with_bot α) := ⟨ assume a b, match a, b with | a, none := assume h : a < ⊥, (not_lt_bot h).elim | none, some b := assume h, let ⟨a, ha⟩ := no_bot b in ⟨a, bot_lt_coe a, coe_lt_coe.2 ha⟩ | some a, some b := assume h, let ⟨a, ha₁, ha₂⟩ := dense (coe_lt_coe.1 h) in ⟨a, coe_lt_coe.2 ha₁, coe_lt_coe.2 ha₂⟩ end⟩ end with_bot --TODO(Mario): Construct using order dual on with_bot def with_top (α : Type*) := option α namespace with_top variable {α : Type u} open lattice instance : has_coe_t α (with_top α) := ⟨some⟩ instance has_top : has_top (with_top α) := ⟨none⟩ lemma none_eq_top : (none : with_top α) = (⊤ : with_top α) := rfl lemma some_eq_coe (a : α) : (some a : with_top α) = (↑a : with_top α) := rfl theorem coe_eq_coe {a b : α} : (a : with_top α) = b ↔ a = b := by rw [← option.some.inj_eq a b]; refl @[simp] theorem top_ne_coe [partial_order α] {a : α} : ⊤ ≠ (a : with_top α) . @[simp] theorem coe_ne_top [partial_order α] {a : α} : (a : with_top α) ≠ ⊤ . instance partial_order [partial_order α] : partial_order (with_top α) := { le := λ o₁ o₂ : option α, ∀ b ∈ o₂, ∃ a ∈ o₁, a ≤ b, le_refl := λ o a ha, ⟨a, ha, le_refl _⟩, le_trans := λ o₁ o₂ o₃ h₁ h₂ c hc, let ⟨b, hb, bc⟩ := h₂ c hc, ⟨a, ha, ab⟩ := h₁ b hb in ⟨a, ha, le_trans ab bc⟩, le_antisymm := λ o₁ o₂ h₁ h₂, begin cases o₂ with b, { cases o₁ with a, {refl}, rcases h₂ a rfl with ⟨_, ⟨⟩, _⟩ }, { rcases h₁ b rfl with ⟨a, ⟨⟩, h₁'⟩, rcases h₂ a rfl with ⟨_, ⟨⟩, h₂'⟩, rw le_antisymm h₁' h₂' } end } instance order_top [partial_order α] : order_top (with_top α) := { le_top := λ a a' h, option.no_confusion h, ..with_top.partial_order, .. with_top.has_top } @[simp] theorem coe_le_coe [partial_order α] {a b : α} : (a : with_top α) ≤ b ↔ a ≤ b := ⟨λ h, by rcases h b rfl with ⟨_, ⟨⟩, h⟩; exact h, λ h a' e, option.some_inj.1 e ▸ ⟨a, rfl, h⟩⟩ @[simp] theorem some_le_some [partial_order α] {a b : α} : @has_le.le (with_top α) _ (some a) (some b) ↔ a ≤ b := coe_le_coe theorem le_coe [partial_order α] {a b : α} : ∀ {o : option α}, a ∈ o → (@has_le.le (with_top α) _ o b ↔ a ≤ b) | _ rfl := coe_le_coe theorem le_coe_iff [partial_order α] (b : α) : ∀(x : with_top α), x ≤ b ↔ (∃a:α, x = a ∧ a ≤ b) | (some a) := by simp [some_eq_coe, coe_eq_coe] | none := by simp [none_eq_top] theorem coe_le_iff [partial_order α] (a : α) : ∀(x : with_top α), ↑a ≤ x ↔ (∀b:α, x = ↑b → a ≤ b) | (some b) := by simp [some_eq_coe, coe_eq_coe] | none := by simp [none_eq_top] theorem lt_iff_exists_coe [partial_order α] : ∀(a b : with_top α), a < b ↔ (∃p:α, a = p ∧ ↑p < b) | (some a) b := by simp [some_eq_coe, coe_eq_coe] | none b := by simp [none_eq_top] @[simp] theorem some_lt_some [partial_order α] {a b : α} : @has_lt.lt (with_top α) _ (some a) (some b) ↔ a < b := (and_congr some_le_some (not_congr some_le_some)) .trans lt_iff_le_not_le.symm lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_top α) < b ↔ a < b := some_lt_some lemma coe_lt_top [partial_order α] (a : α) : (a : with_top α) < ⊤ := lt_of_le_of_ne le_top (λ h, option.no_confusion h) lemma not_top_le_coe [partial_order α] (a : α) : ¬ (⊤:with_top α) ≤ ↑a := assume h, (lt_irrefl ⊤ (lt_of_le_of_lt h (coe_lt_top a))).elim instance linear_order [linear_order α] : linear_order (with_top α) := { le_total := λ o₁ o₂, begin cases o₁ with a, {exact or.inr le_top}, cases o₂ with b, {exact or.inl le_top}, simp [le_total] end, ..with_top.partial_order } instance decidable_linear_order [decidable_linear_order α] : decidable_linear_order (with_top α) := { decidable_le := λ a b, begin cases b with b, { exact is_true le_top }, cases a with a, { exact is_false (mt (le_antisymm le_top) (by simp)) }, { exact decidable_of_iff _ some_le_some } end, ..with_top.linear_order } instance semilattice_inf [semilattice_inf α] : semilattice_inf_top (with_top α) := { inf := option.lift_or_get (⊓), inf_le_left := λ o₁ o₂ a ha, by cases ha; cases o₂; simp [option.lift_or_get], inf_le_right := λ o₁ o₂ a ha, by cases ha; cases o₁; simp [option.lift_or_get], le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases o₂ with b; cases o₃ with c; cases ha, { exact h₂ a rfl }, { exact h₁ a rfl }, { rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩, simp at h₂, exact ⟨d, rfl, le_inf h₁' h₂⟩ } end, ..with_top.order_top } lemma coe_inf [semilattice_inf α] (a b : α) : ((a ⊓ b : α) : with_top α) = a ⊓ b := rfl instance semilattice_sup [semilattice_sup α] : semilattice_sup_top (with_top α) := { sup := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊔ b)), le_sup_left := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, le_sup_left⟩ end, le_sup_right := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, le_sup_right⟩ end, sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases ha, rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩, rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩, exact ⟨_, rfl, sup_le ab ac⟩ end, ..with_top.order_top } lemma coe_sup [semilattice_sup α] (a b : α) : ((a ⊔ b : α) : with_top α) = a ⊔ b := rfl instance lattice [lattice α] : lattice (with_top α) := { ..with_top.semilattice_sup, ..with_top.semilattice_inf } theorem lattice_eq_DLO [decidable_linear_order α] : lattice.lattice_of_decidable_linear_order = @with_top.lattice α _ := lattice.ext $ λ x y, iff.rfl theorem sup_eq_max [decidable_linear_order α] (x y : with_top α) : x ⊔ y = max x y := by rw [← sup_eq_max, lattice_eq_DLO] theorem inf_eq_min [decidable_linear_order α] (x y : with_top α) : x ⊓ y = min x y := by rw [← inf_eq_min, lattice_eq_DLO] instance order_bot [order_bot α] : order_bot (with_top α) := { bot := some ⊥, bot_le := λ o a ha, by cases ha; exact ⟨_, rfl, bot_le⟩, ..with_top.partial_order } instance bounded_lattice [bounded_lattice α] : bounded_lattice (with_top α) := { ..with_top.lattice, ..with_top.order_top, ..with_top.order_bot } lemma well_founded_lt {α : Type*} [partial_order α] (h : well_founded ((<) : α → α → Prop)) : well_founded ((<) : with_top α → with_top α → Prop) := have acc_some : ∀ a : α, acc ((<) : with_top α → with_top α → Prop) (some a) := λ a, acc.intro _ (well_founded.induction h a (show ∀ b, (∀ c, c < b → ∀ d : with_top α, d < some c → acc (<) d) → ∀ y : with_top α, y < some b → acc (<) y, from λ b ih c, option.rec_on c (λ hc, (not_lt_of_ge lattice.le_top hc).elim) (λ c hc, acc.intro _ (ih _ (some_lt_some.1 hc))))), ⟨λ a, option.rec_on a (acc.intro _ (λ y, option.rec_on y (λ h, (lt_irrefl _ h).elim) (λ _ _, acc_some _))) acc_some⟩ instance densely_ordered [partial_order α] [densely_ordered α] [no_top_order α] : densely_ordered (with_top α) := ⟨ assume a b, match a, b with | none, a := assume h : ⊤ < a, (not_top_lt h).elim | some a, none := assume h, let ⟨b, hb⟩ := no_top a in ⟨b, coe_lt_coe.2 hb, coe_lt_top b⟩ | some a, some b := assume h, let ⟨a, ha₁, ha₂⟩ := dense (coe_lt_coe.1 h) in ⟨a, coe_lt_coe.2 ha₁, coe_lt_coe.2 ha₂⟩ end⟩ end with_top namespace order_dual open lattice variable (α : Type*) instance [has_bot α] : has_top (order_dual α) := ⟨(⊥ : α)⟩ instance [has_top α] : has_bot (order_dual α) := ⟨(⊤ : α)⟩ instance [order_bot α] : order_top (order_dual α) := { le_top := @bot_le α _, .. order_dual.partial_order α, .. order_dual.lattice.has_top α } instance [order_top α] : order_bot (order_dual α) := { bot_le := @le_top α _, .. order_dual.partial_order α, .. order_dual.lattice.has_bot α } instance [semilattice_inf_bot α] : semilattice_sup_top (order_dual α) := { .. order_dual.lattice.semilattice_sup α, .. order_dual.lattice.order_top α } instance [semilattice_inf_top α] : semilattice_sup_bot (order_dual α) := { .. order_dual.lattice.semilattice_sup α, .. order_dual.lattice.order_bot α } instance [semilattice_sup_bot α] : semilattice_inf_top (order_dual α) := { .. order_dual.lattice.semilattice_inf α, .. order_dual.lattice.order_top α } instance [semilattice_sup_top α] : semilattice_inf_bot (order_dual α) := { .. order_dual.lattice.semilattice_inf α, .. order_dual.lattice.order_bot α } instance [bounded_lattice α] : bounded_lattice (order_dual α) := { .. order_dual.lattice.lattice α, .. order_dual.lattice.order_top α, .. order_dual.lattice.order_bot α } end order_dual
43fd2afd1ada3eda8ac479266f437179301e04e7
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/algebra/order/group.lean
2bccb2931576c689987116f73d94a1b69859699f
[ "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
46,495
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import algebra.order.monoid import order.order_dual import algebra.abs /-! # Ordered groups This file develops the basics of ordered groups. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. -/ set_option old_structure_cmd true open function universe u variable {α : Type u} @[to_additive] instance group.covariant_class_le.to_contravariant_class_le [group α] [has_le α] [covariant_class α α (*) (≤)] : contravariant_class α α (*) (≤) := group.covconv @[to_additive] instance group.swap.covariant_class_le.to_contravariant_class_le [group α] [has_le α] [covariant_class α α (swap (*)) (≤)] : contravariant_class α α (swap (*)) (≤) := { elim := λ a b c bc, calc b = b * a * a⁻¹ : eq_mul_inv_of_mul_eq rfl ... ≤ c * a * a⁻¹ : mul_le_mul_right' bc a⁻¹ ... = c : mul_inv_eq_of_eq_mul rfl } @[to_additive] instance group.covariant_class_lt.to_contravariant_class_lt [group α] [has_lt α] [covariant_class α α (*) (<)] : contravariant_class α α (*) (<) := { elim := λ a b c bc, calc b = a⁻¹ * (a * b) : eq_inv_mul_of_mul_eq rfl ... < a⁻¹ * (a * c) : mul_lt_mul_left' bc a⁻¹ ... = c : inv_mul_cancel_left a c } @[to_additive] instance group.swap.covariant_class_lt.to_contravariant_class_lt [group α] [has_lt α] [covariant_class α α (swap (*)) (<)] : contravariant_class α α (swap (*)) (<) := { elim := λ a b c bc, calc b = b * a * a⁻¹ : eq_mul_inv_of_mul_eq rfl ... < c * a * a⁻¹ : mul_lt_mul_right' bc a⁻¹ ... = c : mul_inv_eq_of_eq_mul rfl } /-- An ordered additive commutative group is an additive commutative group with a partial order in which addition is strictly monotone. -/ @[protect_proj, ancestor add_comm_group partial_order] class ordered_add_comm_group (α : Type u) extends add_comm_group α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) /-- An ordered commutative group is an commutative group with a partial order in which multiplication is strictly monotone. -/ @[protect_proj, ancestor comm_group partial_order] class ordered_comm_group (α : Type u) extends comm_group α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) attribute [to_additive] ordered_comm_group @[to_additive] instance ordered_comm_group.to_covariant_class_left_le (α : Type u) [ordered_comm_group α] : covariant_class α α (*) (≤) := { elim := λ a b c bc, ordered_comm_group.mul_le_mul_left b c bc a } /--The units of an ordered commutative monoid form an ordered commutative group. -/ @[to_additive] instance units.ordered_comm_group [ordered_comm_monoid α] : ordered_comm_group (units α) := { mul_le_mul_left := λ a b h c, (mul_le_mul_left' (h : (a : α) ≤ b) _ : (c : α) * a ≤ c * b), .. units.partial_order, .. units.comm_group } @[priority 100, to_additive] -- see Note [lower instance priority] instance ordered_comm_group.to_ordered_cancel_comm_monoid (α : Type u) [s : ordered_comm_group α] : ordered_cancel_comm_monoid α := { mul_left_cancel := λ a b c, (mul_right_inj a).mp, le_of_mul_le_mul_left := λ a b c, (mul_le_mul_iff_left a).mp, ..s } @[priority 100, to_additive] instance ordered_comm_group.has_exists_mul_of_le (α : Type u) [ordered_comm_group α] : has_exists_mul_of_le α := ⟨λ a b hab, ⟨b * a⁻¹, (mul_inv_cancel_comm_assoc a b).symm⟩⟩ @[to_additive] instance [h : has_inv α] : has_inv (order_dual α) := h @[to_additive] instance [h : has_div α] : has_div (order_dual α) := h @[to_additive] instance [h : div_inv_monoid α] : div_inv_monoid (order_dual α) := h @[to_additive] instance [h : group α] : group (order_dual α) := h @[to_additive] instance [h : comm_group α] : comm_group (order_dual α) := h @[to_additive] instance [ordered_comm_group α] : ordered_comm_group (order_dual α) := { .. order_dual.ordered_comm_monoid, .. order_dual.group } section group variables [group α] section typeclasses_left_le variables [has_le α] [covariant_class α α (*) (≤)] {a b c d : α} /-- Uses `left` co(ntra)variant. -/ @[simp, to_additive left.neg_nonpos_iff] lemma left.inv_le_one_iff : a⁻¹ ≤ 1 ↔ 1 ≤ a := by { rw [← mul_le_mul_iff_left a], simp } /-- Uses `left` co(ntra)variant. -/ @[simp, to_additive left.nonneg_neg_iff] lemma left.one_le_inv_iff : 1 ≤ a⁻¹ ↔ a ≤ 1 := by { rw [← mul_le_mul_iff_left a], simp } @[simp, to_additive] lemma le_inv_mul_iff_mul_le : b ≤ a⁻¹ * c ↔ a * b ≤ c := by { rw ← mul_le_mul_iff_left a, simp } @[simp, to_additive] lemma inv_mul_le_iff_le_mul : b⁻¹ * a ≤ c ↔ a ≤ b * c := by rw [← mul_le_mul_iff_left b, mul_inv_cancel_left] @[to_additive neg_le_iff_add_nonneg'] lemma inv_le_iff_one_le_mul' : a⁻¹ ≤ b ↔ 1 ≤ a * b := (mul_le_mul_iff_left a).symm.trans $ by rw mul_inv_self @[to_additive] lemma le_inv_iff_mul_le_one_left : a ≤ b⁻¹ ↔ b * a ≤ 1 := (mul_le_mul_iff_left b).symm.trans $ by rw mul_inv_self @[to_additive] lemma le_inv_mul_iff_le : 1 ≤ b⁻¹ * a ↔ b ≤ a := by rw [← mul_le_mul_iff_left b, mul_one, mul_inv_cancel_left] @[to_additive] lemma inv_mul_le_one_iff : a⁻¹ * b ≤ 1 ↔ b ≤ a := trans (inv_mul_le_iff_le_mul) $ by rw mul_one end typeclasses_left_le section typeclasses_left_lt variables [has_lt α] [covariant_class α α (*) (<)] {a b c : α} /-- Uses `left` co(ntra)variant. -/ @[simp, to_additive left.neg_pos_iff] lemma left.one_lt_inv_iff : 1 < a⁻¹ ↔ a < 1 := by rw [← mul_lt_mul_iff_left a, mul_inv_self, mul_one] /-- Uses `left` co(ntra)variant. -/ @[simp, to_additive left.neg_neg_iff] lemma left.inv_lt_one_iff : a⁻¹ < 1 ↔ 1 < a := by rw [← mul_lt_mul_iff_left a, mul_inv_self, mul_one] @[simp, to_additive] lemma lt_inv_mul_iff_mul_lt : b < a⁻¹ * c ↔ a * b < c := by { rw [← mul_lt_mul_iff_left a], simp } @[simp, to_additive] lemma inv_mul_lt_iff_lt_mul : b⁻¹ * a < c ↔ a < b * c := by rw [← mul_lt_mul_iff_left b, mul_inv_cancel_left] @[to_additive] lemma inv_lt_iff_one_lt_mul' : a⁻¹ < b ↔ 1 < a * b := (mul_lt_mul_iff_left a).symm.trans $ by rw mul_inv_self @[to_additive] lemma lt_inv_iff_mul_lt_one' : a < b⁻¹ ↔ b * a < 1 := (mul_lt_mul_iff_left b).symm.trans $ by rw mul_inv_self @[to_additive] lemma lt_inv_mul_iff_lt : 1 < b⁻¹ * a ↔ b < a := by rw [← mul_lt_mul_iff_left b, mul_one, mul_inv_cancel_left] @[to_additive] lemma inv_mul_lt_one_iff : a⁻¹ * b < 1 ↔ b < a := trans (inv_mul_lt_iff_lt_mul) $ by rw mul_one end typeclasses_left_lt section typeclasses_right_le variables [has_le α] [covariant_class α α (swap (*)) (≤)] {a b c : α} /-- Uses `right` co(ntra)variant. -/ @[simp, to_additive right.neg_nonpos_iff] lemma right.inv_le_one_iff : a⁻¹ ≤ 1 ↔ 1 ≤ a := by { rw [← mul_le_mul_iff_right a], simp } /-- Uses `right` co(ntra)variant. -/ @[simp, to_additive right.nonneg_neg_iff] lemma right.one_le_inv_iff : 1 ≤ a⁻¹ ↔ a ≤ 1 := by { rw [← mul_le_mul_iff_right a], simp } @[to_additive neg_le_iff_add_nonneg] lemma inv_le_iff_one_le_mul : a⁻¹ ≤ b ↔ 1 ≤ b * a := (mul_le_mul_iff_right a).symm.trans $ by rw inv_mul_self @[to_additive] lemma le_inv_iff_mul_le_one_right : a ≤ b⁻¹ ↔ a * b ≤ 1 := (mul_le_mul_iff_right b).symm.trans $ by rw inv_mul_self @[simp, to_additive] lemma mul_inv_le_iff_le_mul : a * b⁻¹ ≤ c ↔ a ≤ c * b := (mul_le_mul_iff_right b).symm.trans $ by rw inv_mul_cancel_right @[simp, to_additive] lemma le_mul_inv_iff_mul_le : c ≤ a * b⁻¹ ↔ c * b ≤ a := (mul_le_mul_iff_right b).symm.trans $ by rw inv_mul_cancel_right @[simp, to_additive] lemma mul_inv_le_one_iff_le : a * b⁻¹ ≤ 1 ↔ a ≤ b := mul_inv_le_iff_le_mul.trans $ by rw one_mul @[to_additive] lemma le_mul_inv_iff_le : 1 ≤ a * b⁻¹ ↔ b ≤ a := by rw [← mul_le_mul_iff_right b, one_mul, inv_mul_cancel_right] @[to_additive] lemma mul_inv_le_one_iff : b * a⁻¹ ≤ 1 ↔ b ≤ a := trans (mul_inv_le_iff_le_mul) $ by rw one_mul end typeclasses_right_le section typeclasses_right_lt variables [has_lt α] [covariant_class α α (swap (*)) (<)] {a b c : α} /-- Uses `right` co(ntra)variant. -/ @[simp, to_additive right.neg_neg_iff] lemma right.inv_lt_one_iff : a⁻¹ < 1 ↔ 1 < a := by rw [← mul_lt_mul_iff_right a, inv_mul_self, one_mul] /-- Uses `right` co(ntra)variant. -/ @[simp, to_additive right.neg_pos_iff] lemma right.one_lt_inv_iff : 1 < a⁻¹ ↔ a < 1 := by rw [← mul_lt_mul_iff_right a, inv_mul_self, one_mul] @[to_additive] lemma inv_lt_iff_one_lt_mul : a⁻¹ < b ↔ 1 < b * a := (mul_lt_mul_iff_right a).symm.trans $ by rw inv_mul_self @[to_additive] lemma lt_inv_iff_mul_lt_one : a < b⁻¹ ↔ a * b < 1 := (mul_lt_mul_iff_right b).symm.trans $ by rw inv_mul_self @[simp, to_additive] lemma mul_inv_lt_iff_lt_mul : a * b⁻¹ < c ↔ a < c * b := by rw [← mul_lt_mul_iff_right b, inv_mul_cancel_right] @[simp, to_additive] lemma lt_mul_inv_iff_mul_lt : c < a * b⁻¹ ↔ c * b < a := (mul_lt_mul_iff_right b).symm.trans $ by rw inv_mul_cancel_right @[simp, to_additive] lemma inv_mul_lt_one_iff_lt : a * b⁻¹ < 1 ↔ a < b := by rw [← mul_lt_mul_iff_right b, inv_mul_cancel_right, one_mul] @[to_additive] lemma lt_mul_inv_iff_lt : 1 < a * b⁻¹ ↔ b < a := by rw [← mul_lt_mul_iff_right b, one_mul, inv_mul_cancel_right] @[to_additive] lemma mul_inv_lt_one_iff : b * a⁻¹ < 1 ↔ b < a := trans (mul_inv_lt_iff_lt_mul) $ by rw one_mul end typeclasses_right_lt section typeclasses_left_right_le variables [has_le α] [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] {a b c d : α} @[simp, to_additive] lemma inv_le_inv_iff : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := by { rw [← mul_le_mul_iff_left a, ← mul_le_mul_iff_right b], simp } alias neg_le_neg_iff ↔ le_of_neg_le_neg _ section variable (α) /-- `x ↦ x⁻¹` as an order-reversing equivalence. -/ @[to_additive "`x ↦ -x` as an order-reversing equivalence.", simps] def order_iso.inv : α ≃o order_dual α := { to_equiv := (equiv.inv α).trans order_dual.to_dual, map_rel_iff' := λ a b, @inv_le_inv_iff α _ _ _ _ _ _ } end @[to_additive neg_le] lemma inv_le' : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := (order_iso.inv α).symm_apply_le alias inv_le' ↔ inv_le_of_inv_le' _ attribute [to_additive neg_le_of_neg_le] inv_le_of_inv_le' @[to_additive le_neg] lemma le_inv' : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := (order_iso.inv α).le_symm_apply @[to_additive] lemma mul_inv_le_inv_mul_iff : a * b⁻¹ ≤ d⁻¹ * c ↔ d * a ≤ c * b := by rw [← mul_le_mul_iff_left d, ← mul_le_mul_iff_right b, mul_inv_cancel_left, mul_assoc, inv_mul_cancel_right] @[simp, to_additive] lemma div_le_self_iff (a : α) {b : α} : a / b ≤ a ↔ 1 ≤ b := by simp [div_eq_mul_inv] alias sub_le_self_iff ↔ _ sub_le_self end typeclasses_left_right_le section typeclasses_left_right_lt variables [has_lt α] [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)] {a b c d : α} @[simp, to_additive] lemma inv_lt_inv_iff : a⁻¹ < b⁻¹ ↔ b < a := by { rw [← mul_lt_mul_iff_left a, ← mul_lt_mul_iff_right b], simp } @[to_additive neg_lt] lemma inv_lt' : a⁻¹ < b ↔ b⁻¹ < a := by rw [← inv_lt_inv_iff, inv_inv] @[to_additive lt_neg] lemma lt_inv' : a < b⁻¹ ↔ b < a⁻¹ := by rw [← inv_lt_inv_iff, inv_inv] alias lt_inv' ↔ lt_inv_of_lt_inv _ attribute [to_additive] lt_inv_of_lt_inv alias inv_lt' ↔ inv_lt_of_inv_lt' _ attribute [to_additive neg_lt_of_neg_lt] inv_lt_of_inv_lt' @[to_additive] lemma mul_inv_lt_inv_mul_iff : a * b⁻¹ < d⁻¹ * c ↔ d * a < c * b := by rw [← mul_lt_mul_iff_left d, ← mul_lt_mul_iff_right b, mul_inv_cancel_left, mul_assoc, inv_mul_cancel_right] @[simp, to_additive] lemma div_lt_self_iff (a : α) {b : α} : a / b < a ↔ 1 < b := by simp [div_eq_mul_inv] alias sub_lt_self_iff ↔ _ sub_lt_self end typeclasses_left_right_lt section pre_order variable [preorder α] section left_le variables [covariant_class α α (*) (≤)] {a : α} @[to_additive] lemma left.inv_le_self (h : 1 ≤ a) : a⁻¹ ≤ a := le_trans (left.inv_le_one_iff.mpr h) h alias left.neg_le_self ← neg_le_self @[to_additive] lemma left.self_le_inv (h : a ≤ 1) : a ≤ a⁻¹ := le_trans h (left.one_le_inv_iff.mpr h) end left_le section left_lt variables [covariant_class α α (*) (<)] {a : α} @[to_additive] lemma left.inv_lt_self (h : 1 < a) : a⁻¹ < a := (left.inv_lt_one_iff.mpr h).trans h alias left.neg_lt_self ← neg_lt_self @[to_additive] lemma left.self_lt_inv (h : a < 1) : a < a⁻¹ := lt_trans h (left.one_lt_inv_iff.mpr h) end left_lt section right_le variables [covariant_class α α (swap (*)) (≤)] {a : α} @[to_additive] lemma right.inv_le_self (h : 1 ≤ a) : a⁻¹ ≤ a := le_trans (right.inv_le_one_iff.mpr h) h @[to_additive] lemma right.self_le_inv (h : a ≤ 1) : a ≤ a⁻¹ := le_trans h (right.one_le_inv_iff.mpr h) end right_le section right_lt variables [covariant_class α α (swap (*)) (<)] {a : α} @[to_additive] lemma right.inv_lt_self (h : 1 < a) : a⁻¹ < a := (right.inv_lt_one_iff.mpr h).trans h @[to_additive] lemma right.self_lt_inv (h : a < 1) : a < a⁻¹ := lt_trans h (right.one_lt_inv_iff.mpr h) end right_lt end pre_order end group section comm_group variables [comm_group α] section has_le variables [has_le α] [covariant_class α α (*) (≤)] {a b c d : α} @[to_additive] lemma inv_mul_le_iff_le_mul' : c⁻¹ * a ≤ b ↔ a ≤ b * c := by rw [inv_mul_le_iff_le_mul, mul_comm] @[simp, to_additive] lemma mul_inv_le_iff_le_mul' : a * b⁻¹ ≤ c ↔ a ≤ b * c := by rw [← inv_mul_le_iff_le_mul, mul_comm] @[to_additive add_neg_le_add_neg_iff] lemma mul_inv_le_mul_inv_iff' : a * b⁻¹ ≤ c * d⁻¹ ↔ a * d ≤ c * b := by rw [mul_comm c, mul_inv_le_inv_mul_iff, mul_comm] end has_le section has_lt variables [has_lt α] [covariant_class α α (*) (<)] {a b c d : α} @[to_additive] lemma inv_mul_lt_iff_lt_mul' : c⁻¹ * a < b ↔ a < b * c := by rw [inv_mul_lt_iff_lt_mul, mul_comm] @[simp, to_additive] lemma mul_inv_lt_iff_le_mul' : a * b⁻¹ < c ↔ a < b * c := by rw [← inv_mul_lt_iff_lt_mul, mul_comm] @[to_additive add_neg_lt_add_neg_iff] lemma mul_inv_lt_mul_inv_iff' : a * b⁻¹ < c * d⁻¹ ↔ a * d < c * b := by rw [mul_comm c, mul_inv_lt_inv_mul_iff, mul_comm] end has_lt end comm_group alias le_inv' ↔ le_inv_of_le_inv _ attribute [to_additive] le_inv_of_le_inv alias left.inv_le_one_iff ↔ one_le_of_inv_le_one _ attribute [to_additive] one_le_of_inv_le_one alias left.one_le_inv_iff ↔ le_one_of_one_le_inv _ attribute [to_additive nonpos_of_neg_nonneg] le_one_of_one_le_inv alias inv_lt_inv_iff ↔ lt_of_inv_lt_inv _ attribute [to_additive] lt_of_inv_lt_inv alias left.inv_lt_one_iff ↔ one_lt_of_inv_lt_one _ attribute [to_additive] one_lt_of_inv_lt_one alias left.inv_lt_one_iff ← inv_lt_one_iff_one_lt attribute [to_additive] inv_lt_one_iff_one_lt alias left.inv_lt_one_iff ← inv_lt_one' attribute [to_additive neg_lt_zero] inv_lt_one' alias left.one_lt_inv_iff ↔ inv_of_one_lt_inv _ attribute [to_additive neg_of_neg_pos] inv_of_one_lt_inv alias left.one_lt_inv_iff ↔ _ one_lt_inv_of_inv attribute [to_additive neg_pos_of_neg] one_lt_inv_of_inv alias le_inv_mul_iff_mul_le ↔ mul_le_of_le_inv_mul _ attribute [to_additive] mul_le_of_le_inv_mul alias le_inv_mul_iff_mul_le ↔ _ le_inv_mul_of_mul_le attribute [to_additive] le_inv_mul_of_mul_le alias inv_mul_le_iff_le_mul ↔ _ inv_mul_le_of_le_mul attribute [to_additive] inv_mul_le_iff_le_mul alias lt_inv_mul_iff_mul_lt ↔ mul_lt_of_lt_inv_mul _ attribute [to_additive] mul_lt_of_lt_inv_mul alias lt_inv_mul_iff_mul_lt ↔ _ lt_inv_mul_of_mul_lt attribute [to_additive] lt_inv_mul_of_mul_lt alias inv_mul_lt_iff_lt_mul ↔ lt_mul_of_inv_mul_lt inv_mul_lt_of_lt_mul attribute [to_additive] lt_mul_of_inv_mul_lt attribute [to_additive] inv_mul_lt_of_lt_mul alias lt_mul_of_inv_mul_lt ← lt_mul_of_inv_mul_lt_left attribute [to_additive] lt_mul_of_inv_mul_lt_left alias left.inv_le_one_iff ← inv_le_one' attribute [to_additive neg_nonpos] inv_le_one' alias left.one_le_inv_iff ← one_le_inv' attribute [to_additive neg_nonneg] one_le_inv' alias left.one_lt_inv_iff ← one_lt_inv' attribute [to_additive neg_pos] one_lt_inv' alias mul_lt_mul_left' ← ordered_comm_group.mul_lt_mul_left' attribute [to_additive ordered_add_comm_group.add_lt_add_left] ordered_comm_group.mul_lt_mul_left' alias le_of_mul_le_mul_left' ← ordered_comm_group.le_of_mul_le_mul_left attribute [to_additive ordered_add_comm_group.le_of_add_le_add_left] ordered_comm_group.le_of_mul_le_mul_left alias lt_of_mul_lt_mul_left' ← ordered_comm_group.lt_of_mul_lt_mul_left attribute [to_additive ordered_add_comm_group.lt_of_add_lt_add_left] ordered_comm_group.lt_of_mul_lt_mul_left /-- Pullback an `ordered_comm_group` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.ordered_add_comm_group "Pullback an `ordered_add_comm_group` under an injective map."] def function.injective.ordered_comm_group [ordered_comm_group α] {β : Type*} [has_one β] [has_mul β] [has_inv β] [has_div β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) : ordered_comm_group β := { ..partial_order.lift f hf, ..hf.ordered_comm_monoid f one mul, ..hf.comm_group f one mul inv div } /- Most of the lemmas that are primed in this section appear in ordered_field. -/ /- I (DT) did not try to minimise the assumptions. -/ section group variables [group α] [has_le α] section right variables [covariant_class α α (swap (*)) (≤)] {a b c d : α} @[simp, to_additive] lemma div_le_div_iff_right (c : α) : a / c ≤ b / c ↔ a ≤ b := by simpa only [div_eq_mul_inv] using mul_le_mul_iff_right _ @[to_additive sub_le_sub_right] lemma div_le_div_right' (h : a ≤ b) (c : α) : a / c ≤ b / c := (div_le_div_iff_right c).2 h @[simp, to_additive sub_nonneg] lemma one_le_div' : 1 ≤ a / b ↔ b ≤ a := by rw [← mul_le_mul_iff_right b, one_mul, div_eq_mul_inv, inv_mul_cancel_right] alias sub_nonneg ↔ le_of_sub_nonneg sub_nonneg_of_le @[simp, to_additive sub_nonpos] lemma div_le_one' : a / b ≤ 1 ↔ a ≤ b := by rw [← mul_le_mul_iff_right b, one_mul, div_eq_mul_inv, inv_mul_cancel_right] alias sub_nonpos ↔ le_of_sub_nonpos sub_nonpos_of_le @[to_additive] lemma le_div_iff_mul_le : a ≤ c / b ↔ a * b ≤ c := by rw [← mul_le_mul_iff_right b, div_eq_mul_inv, inv_mul_cancel_right] alias le_sub_iff_add_le ↔ add_le_of_le_sub_right le_sub_right_of_add_le @[to_additive] lemma div_le_iff_le_mul : a / c ≤ b ↔ a ≤ b * c := by rw [← mul_le_mul_iff_right c, div_eq_mul_inv, inv_mul_cancel_right] /-- `equiv.mul_right` as an `order_iso`. See also `order_embedding.mul_right`. -/ @[to_additive "`equiv.add_right` as an `order_iso`. See also `order_embedding.add_right`.", simps to_equiv apply {simp_rhs := tt}] def order_iso.mul_right (a : α) : α ≃o α := { map_rel_iff' := λ _ _, mul_le_mul_iff_right a, to_equiv := equiv.mul_right a } @[simp, to_additive] lemma order_iso.mul_right_symm (a : α) : (order_iso.mul_right a).symm = order_iso.mul_right a⁻¹ := by { ext x, refl } end right section left variables [covariant_class α α (*) (≤)] /-- `equiv.mul_left` as an `order_iso`. See also `order_embedding.mul_left`. -/ @[to_additive "`equiv.add_left` as an `order_iso`. See also `order_embedding.add_left`.", simps to_equiv apply {simp_rhs := tt}] def order_iso.mul_left (a : α) : α ≃o α := { map_rel_iff' := λ _ _, mul_le_mul_iff_left a, to_equiv := equiv.mul_left a } @[simp, to_additive] lemma order_iso.mul_left_symm (a : α) : (order_iso.mul_left a).symm = order_iso.mul_left a⁻¹ := by { ext x, refl } variables [covariant_class α α (swap (*)) (≤)] {a b c : α} @[simp, to_additive] lemma div_le_div_iff_left (a : α) : a / b ≤ a / c ↔ c ≤ b := by rw [div_eq_mul_inv, div_eq_mul_inv, ← mul_le_mul_iff_left a⁻¹, inv_mul_cancel_left, inv_mul_cancel_left, inv_le_inv_iff] @[to_additive sub_le_sub_left] lemma div_le_div_left' (h : a ≤ b) (c : α) : c / b ≤ c / a := (div_le_div_iff_left c).2 h end left end group section comm_group variables [comm_group α] section has_le variables [has_le α] [covariant_class α α (*) (≤)] {a b c d : α} @[to_additive sub_le_sub_iff] lemma div_le_div_iff' : a / b ≤ c / d ↔ a * d ≤ c * b := by simpa only [div_eq_mul_inv] using mul_inv_le_mul_inv_iff' @[to_additive] lemma le_div_iff_mul_le' : b ≤ c / a ↔ a * b ≤ c := by rw [le_div_iff_mul_le, mul_comm] alias le_sub_iff_add_le' ↔ add_le_of_le_sub_left le_sub_left_of_add_le @[to_additive] lemma div_le_iff_le_mul' : a / b ≤ c ↔ a ≤ b * c := by rw [div_le_iff_le_mul, mul_comm] alias sub_le_iff_le_add' ↔ le_add_of_sub_left_le sub_left_le_of_le_add @[simp, to_additive] lemma inv_le_div_iff_le_mul : b⁻¹ ≤ a / c ↔ c ≤ a * b := le_div_iff_mul_le.trans inv_mul_le_iff_le_mul' @[to_additive] lemma inv_le_div_iff_le_mul' : a⁻¹ ≤ b / c ↔ c ≤ a * b := by rw [inv_le_div_iff_le_mul, mul_comm] @[to_additive sub_le] lemma div_le'' : a / b ≤ c ↔ a / c ≤ b := div_le_iff_le_mul'.trans div_le_iff_le_mul.symm @[to_additive le_sub] lemma le_div'' : a ≤ b / c ↔ c ≤ b / a := le_div_iff_mul_le'.trans le_div_iff_mul_le.symm end has_le section preorder variables [preorder α] [covariant_class α α (*) (≤)] {a b c d : α} @[to_additive sub_le_sub] lemma div_le_div'' (hab : a ≤ b) (hcd : c ≤ d) : a / d ≤ b / c := begin rw [div_eq_mul_inv, div_eq_mul_inv, mul_comm b, mul_inv_le_inv_mul_iff, mul_comm], exact mul_le_mul' hab hcd end end preorder end comm_group /- Most of the lemmas that are primed in this section appear in ordered_field. -/ /- I (DT) did not try to minimise the assumptions. -/ section group variables [group α] [has_lt α] section right variables [covariant_class α α (swap (*)) (<)] {a b c d : α} @[simp, to_additive] lemma div_lt_div_iff_right (c : α) : a / c < b / c ↔ a < b := by simpa only [div_eq_mul_inv] using mul_lt_mul_iff_right _ @[to_additive sub_lt_sub_right] lemma div_lt_div_right' (h : a < b) (c : α) : a / c < b / c := (div_lt_div_iff_right c).2 h @[simp, to_additive sub_pos] lemma one_lt_div' : 1 < a / b ↔ b < a := by rw [← mul_lt_mul_iff_right b, one_mul, div_eq_mul_inv, inv_mul_cancel_right] alias sub_pos ↔ lt_of_sub_pos sub_pos_of_lt @[simp, to_additive sub_neg] lemma div_lt_one' : a / b < 1 ↔ a < b := by rw [← mul_lt_mul_iff_right b, one_mul, div_eq_mul_inv, inv_mul_cancel_right] alias sub_neg ↔ lt_of_sub_neg sub_neg_of_lt alias sub_neg ← sub_lt_zero @[to_additive] lemma lt_div_iff_mul_lt : a < c / b ↔ a * b < c := by rw [← mul_lt_mul_iff_right b, div_eq_mul_inv, inv_mul_cancel_right] alias lt_sub_iff_add_lt ↔ add_lt_of_lt_sub_right lt_sub_right_of_add_lt @[to_additive] lemma div_lt_iff_lt_mul : a / c < b ↔ a < b * c := by rw [← mul_lt_mul_iff_right c, div_eq_mul_inv, inv_mul_cancel_right] alias sub_lt_iff_lt_add ↔ lt_add_of_sub_right_lt sub_right_lt_of_lt_add end right section left variables [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)] {a b c : α} @[simp, to_additive] lemma div_lt_div_iff_left (a : α) : a / b < a / c ↔ c < b := by rw [div_eq_mul_inv, div_eq_mul_inv, ← mul_lt_mul_iff_left a⁻¹, inv_mul_cancel_left, inv_mul_cancel_left, inv_lt_inv_iff] @[simp, to_additive] lemma inv_lt_div_iff_lt_mul : a⁻¹ < b / c ↔ c < a * b := by rw [div_eq_mul_inv, lt_mul_inv_iff_mul_lt, inv_mul_lt_iff_lt_mul] @[to_additive sub_lt_sub_left] lemma div_lt_div_left' (h : a < b) (c : α) : c / b < c / a := (div_lt_div_iff_left c).2 h end left end group section comm_group variables [comm_group α] section has_lt variables [has_lt α] [covariant_class α α (*) (<)] {a b c d : α} @[to_additive sub_lt_sub_iff] lemma div_lt_div_iff' : a / b < c / d ↔ a * d < c * b := by simpa only [div_eq_mul_inv] using mul_inv_lt_mul_inv_iff' @[to_additive] lemma lt_div_iff_mul_lt' : b < c / a ↔ a * b < c := by rw [lt_div_iff_mul_lt, mul_comm] alias lt_sub_iff_add_lt' ↔ add_lt_of_lt_sub_left lt_sub_left_of_add_lt @[to_additive] lemma div_lt_iff_lt_mul' : a / b < c ↔ a < b * c := by rw [div_lt_iff_lt_mul, mul_comm] alias sub_lt_iff_lt_add' ↔ lt_add_of_sub_left_lt sub_left_lt_of_lt_add @[to_additive] lemma inv_lt_div_iff_lt_mul' : b⁻¹ < a / c ↔ c < a * b := lt_div_iff_mul_lt.trans inv_mul_lt_iff_lt_mul' @[to_additive sub_lt] lemma div_lt'' : a / b < c ↔ a / c < b := div_lt_iff_lt_mul'.trans div_lt_iff_lt_mul.symm @[to_additive lt_sub] lemma lt_div'' : a < b / c ↔ c < b / a := lt_div_iff_mul_lt'.trans lt_div_iff_mul_lt.symm end has_lt section preorder variables [preorder α] [covariant_class α α (*) (<)] {a b c d : α} @[to_additive sub_lt_sub] lemma div_lt_div'' (hab : a < b) (hcd : c < d) : a / d < b / c := begin rw [div_eq_mul_inv, div_eq_mul_inv, mul_comm b, mul_inv_lt_inv_mul_iff, mul_comm], exact mul_lt_mul_of_lt_of_lt hab hcd end end preorder end comm_group section linear_order variables [group α] [linear_order α] [covariant_class α α (*) (≤)] section variable_names variables {a b c : α} @[to_additive] lemma le_of_forall_one_lt_lt_mul (h : ∀ ε : α, 1 < ε → a < b * ε) : a ≤ b := le_of_not_lt (λ h₁, lt_irrefl a (by simpa using (h _ (lt_inv_mul_iff_lt.mpr h₁)))) @[to_additive] lemma le_iff_forall_one_lt_lt_mul : a ≤ b ↔ ∀ ε, 1 < ε → a < b * ε := ⟨λ h ε, lt_mul_of_le_of_one_lt h, le_of_forall_one_lt_lt_mul⟩ /- I (DT) introduced this lemma to prove (the additive version `sub_le_sub_flip` of) `div_le_div_flip` below. Now I wonder what is the point of either of these lemmas... -/ @[to_additive] lemma div_le_inv_mul_iff [covariant_class α α (swap (*)) (≤)] : a / b ≤ a⁻¹ * b ↔ a ≤ b := begin rw [div_eq_mul_inv, mul_inv_le_inv_mul_iff], exact ⟨λ h, not_lt.mp (λ k, not_lt.mpr h (mul_lt_mul''' k k)), λ h, mul_le_mul' h h⟩, end /- What is the point of this lemma? See comment about `div_le_inv_mul_iff` above. -/ @[simp, to_additive] lemma div_le_div_flip {α : Type*} [comm_group α] [linear_order α] [covariant_class α α (*) (≤)] {a b : α}: a / b ≤ b / a ↔ a ≤ b := begin rw [div_eq_mul_inv b, mul_comm], exact div_le_inv_mul_iff, end @[simp, to_additive] lemma max_one_div_max_inv_one_eq_self (a : α) : max a 1 / max a⁻¹ 1 = a := by { rcases le_total a 1 with h|h; simp [h] } alias max_zero_sub_max_neg_zero_eq_self ← max_zero_sub_eq_self end variable_names section densely_ordered variables [densely_ordered α] {a b c : α} @[to_additive] lemma le_of_forall_one_lt_le_mul (h : ∀ ε : α, 1 < ε → a ≤ b * ε) : a ≤ b := le_of_forall_le_of_dense $ λ c hc, calc a ≤ b * (b⁻¹ * c) : h _ (lt_inv_mul_iff_lt.mpr hc) ... = c : mul_inv_cancel_left b c @[to_additive] lemma le_of_forall_lt_one_mul_le (h : ∀ ε < 1, a * ε ≤ b) : a ≤ b := @le_of_forall_one_lt_le_mul (order_dual α) _ _ _ _ _ _ h @[to_additive] lemma le_of_forall_one_lt_div_le (h : ∀ ε : α, 1 < ε → a / ε ≤ b) : a ≤ b := le_of_forall_lt_one_mul_le $ λ ε ε1, by simpa only [div_eq_mul_inv, inv_inv] using h ε⁻¹ (left.one_lt_inv_iff.2 ε1) @[to_additive] lemma le_iff_forall_one_lt_le_mul : a ≤ b ↔ ∀ ε, 1 < ε → a ≤ b * ε := ⟨λ h ε ε_pos, le_mul_of_le_of_one_le h ε_pos.le, le_of_forall_one_lt_le_mul⟩ @[to_additive] lemma le_iff_forall_lt_one_mul_le : a ≤ b ↔ ∀ ε < 1, a * ε ≤ b := @le_iff_forall_one_lt_le_mul (order_dual α) _ _ _ _ _ _ end densely_ordered end linear_order /-! ### Linearly ordered commutative groups -/ /-- A linearly ordered additive commutative group is an additive commutative group with a linear order in which addition is monotone. -/ @[protect_proj, ancestor ordered_add_comm_group linear_order] class linear_ordered_add_comm_group (α : Type u) extends ordered_add_comm_group α, linear_order α /-- A linearly ordered commutative monoid with an additively absorbing `⊤` element. Instances should include number systems with an infinite element adjoined.` -/ @[protect_proj, ancestor linear_ordered_add_comm_monoid_with_top sub_neg_monoid nontrivial] class linear_ordered_add_comm_group_with_top (α : Type*) extends linear_ordered_add_comm_monoid_with_top α, sub_neg_monoid α, nontrivial α := (neg_top : - (⊤ : α) = ⊤) (add_neg_cancel : ∀ a:α, a ≠ ⊤ → a + (- a) = 0) /-- A linearly ordered commutative group is a commutative group with a linear order in which multiplication is monotone. -/ @[protect_proj, ancestor ordered_comm_group linear_order, to_additive] class linear_ordered_comm_group (α : Type u) extends ordered_comm_group α, linear_order α @[to_additive] instance [linear_ordered_comm_group α] : linear_ordered_comm_group (order_dual α) := { .. order_dual.ordered_comm_group, .. order_dual.linear_order α } section linear_ordered_comm_group variables [linear_ordered_comm_group α] {a b c : α} @[priority 100, to_additive] -- see Note [lower instance priority] instance linear_ordered_comm_group.to_linear_ordered_cancel_comm_monoid : linear_ordered_cancel_comm_monoid α := { le_of_mul_le_mul_left := λ x y z, le_of_mul_le_mul_left', mul_left_cancel := λ x y z, mul_left_cancel, ..‹linear_ordered_comm_group α› } /-- Pullback a `linear_ordered_comm_group` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.linear_ordered_add_comm_group "Pullback a `linear_ordered_add_comm_group` under an injective map."] def function.injective.linear_ordered_comm_group {β : Type*} [has_one β] [has_mul β] [has_inv β] [has_div β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) : linear_ordered_comm_group β := { ..linear_order.lift f hf, ..hf.ordered_comm_group f one mul inv div } @[to_additive linear_ordered_add_comm_group.add_lt_add_left] lemma linear_ordered_comm_group.mul_lt_mul_left' (a b : α) (h : a < b) (c : α) : c * a < c * b := mul_lt_mul_left' h c @[to_additive min_neg_neg] lemma min_inv_inv' (a b : α) : min (a⁻¹) (b⁻¹) = (max a b)⁻¹ := eq.symm $ @monotone.map_max α (order_dual α) _ _ has_inv.inv a b $ λ a b, inv_le_inv_iff.mpr @[to_additive max_neg_neg] lemma max_inv_inv' (a b : α) : max (a⁻¹) (b⁻¹) = (min a b)⁻¹ := eq.symm $ @monotone.map_min α (order_dual α) _ _ has_inv.inv a b $ λ a b, inv_le_inv_iff.mpr @[to_additive min_sub_sub_right] lemma min_div_div_right' (a b c : α) : min (a / c) (b / c) = min a b / c := by simpa only [div_eq_mul_inv] using min_mul_mul_right a b (c⁻¹) @[to_additive max_sub_sub_right] lemma max_div_div_right' (a b c : α) : max (a / c) (b / c) = max a b / c := by simpa only [div_eq_mul_inv] using max_mul_mul_right a b (c⁻¹) @[to_additive min_sub_sub_left] lemma min_div_div_left' (a b c : α) : min (a / b) (a / c) = a / max b c := by simp only [div_eq_mul_inv, min_mul_mul_left, min_inv_inv'] @[to_additive max_sub_sub_left] lemma max_div_div_left' (a b c : α) : max (a / b) (a / c) = a / min b c := by simp only [div_eq_mul_inv, max_mul_mul_left, max_inv_inv'] @[to_additive eq_zero_of_neg_eq] lemma eq_one_of_inv_eq' (h : a⁻¹ = a) : a = 1 := match lt_trichotomy a 1 with | or.inl h₁ := have 1 < a, from h ▸ one_lt_inv_of_inv h₁, absurd h₁ this.asymm | or.inr (or.inl h₁) := h₁ | or.inr (or.inr h₁) := have a < 1, from h ▸ inv_lt_one'.mpr h₁, absurd h₁ this.asymm end @[to_additive exists_zero_lt] lemma exists_one_lt' [nontrivial α] : ∃ (a:α), 1 < a := begin obtain ⟨y, hy⟩ := decidable.exists_ne (1 : α), cases hy.lt_or_lt, { exact ⟨y⁻¹, one_lt_inv'.mpr h⟩ }, { exact ⟨y, h⟩ } end @[priority 100, to_additive] -- see Note [lower instance priority] instance linear_ordered_comm_group.to_no_top_order [nontrivial α] : no_top_order α := ⟨ begin obtain ⟨y, hy⟩ : ∃ (a:α), 1 < a := exists_one_lt', exact λ a, ⟨a * y, lt_mul_of_one_lt_right' a hy⟩ end ⟩ @[priority 100, to_additive] -- see Note [lower instance priority] instance linear_ordered_comm_group.to_no_bot_order [nontrivial α] : no_bot_order α := ⟨ begin obtain ⟨y, hy⟩ : ∃ (a:α), 1 < a := exists_one_lt', exact λ a, ⟨a / y, (div_lt_self_iff a).mpr hy⟩ end ⟩ end linear_ordered_comm_group section covariant_add_le section has_neg /-- `abs a` is the absolute value of `a`. -/ @[to_additive, priority 100] -- see Note [lower instance priority] instance has_inv_lattice_has_abs [has_inv α] [lattice α] : has_abs (α) := ⟨λa, a ⊔ (a⁻¹)⟩ lemma abs_eq_sup_neg {α : Type*} [has_neg α] [lattice α] (a : α) : abs a = a ⊔ (-a) := rfl variables [has_neg α] [linear_order α] {a b: α} lemma abs_eq_max_neg : abs a = max a (-a) := rfl lemma abs_choice (x : α) : |x| = x ∨ |x| = -x := max_choice _ _ lemma abs_le' : |a| ≤ b ↔ a ≤ b ∧ -a ≤ b := max_le_iff lemma le_abs : a ≤ |b| ↔ a ≤ b ∨ a ≤ -b := le_max_iff lemma le_abs_self (a : α) : a ≤ |a| := le_max_left _ _ lemma neg_le_abs_self (a : α) : -a ≤ |a| := le_max_right _ _ lemma lt_abs : a < |b| ↔ a < b ∨ a < -b := lt_max_iff theorem abs_le_abs (h₀ : a ≤ b) (h₁ : -a ≤ b) : |a| ≤ |b| := (abs_le'.2 ⟨h₀, h₁⟩).trans (le_abs_self b) lemma abs_by_cases (P : α → Prop) {a : α} (h1 : P a) (h2 : P (-a)) : P (|a|) := sup_ind _ _ h1 h2 end has_neg section add_group variables [add_group α] [linear_order α] @[simp] lemma abs_neg (a : α) : | -a| = |a| := begin rw [abs_eq_max_neg, max_comm, neg_neg, abs_eq_max_neg] end lemma eq_or_eq_neg_of_abs_eq {a b : α} (h : |a| = b) : a = b ∨ a = -b := by simpa only [← h, eq_comm, eq_neg_iff_eq_neg] using abs_choice a lemma abs_eq_abs {a b : α} : |a| = |b| ↔ a = b ∨ a = -b := begin refine ⟨λ h, _, λ h, _⟩, { obtain rfl | rfl := eq_or_eq_neg_of_abs_eq h; simpa only [neg_eq_iff_neg_eq, neg_inj, or.comm, @eq_comm _ (-b)] using abs_choice b }, { cases h; simp only [h, abs_neg] }, end lemma abs_sub_comm (a b : α) : |a - b| = |b - a| := calc |a - b| = | - (b - a)| : congr_arg _ (neg_sub b a).symm ... = |b - a| : abs_neg (b - a) variables [covariant_class α α (+) (≤)] {a b c : α} lemma abs_of_nonneg (h : 0 ≤ a) : |a| = a := max_eq_left $ (neg_nonpos.2 h).trans h lemma abs_of_pos (h : 0 < a) : |a| = a := abs_of_nonneg h.le lemma abs_of_nonpos (h : a ≤ 0) : |a| = -a := max_eq_right $ h.trans (neg_nonneg.2 h) lemma abs_of_neg (h : a < 0) : |a| = -a := abs_of_nonpos h.le @[simp] lemma abs_zero : |0| = (0:α) := abs_of_nonneg le_rfl @[simp] lemma abs_pos : 0 < |a| ↔ a ≠ 0 := begin rcases lt_trichotomy a 0 with (ha|rfl|ha), { simp [abs_of_neg ha, neg_pos, ha.ne, ha] }, { simp }, { simp [abs_of_pos ha, ha, ha.ne.symm] } end lemma abs_pos_of_pos (h : 0 < a) : 0 < |a| := abs_pos.2 h.ne.symm lemma abs_pos_of_neg (h : a < 0) : 0 < |a| := abs_pos.2 h.ne lemma neg_abs_le_self (a : α) : -|a| ≤ a := begin cases le_total 0 a with h h, { calc -|a| = - a : congr_arg (has_neg.neg) (abs_of_nonneg h) ... ≤ 0 : neg_nonpos.mpr h ... ≤ a : h }, { calc -|a| = - - a : congr_arg (has_neg.neg) (abs_of_nonpos h) ... ≤ a : (neg_neg a).le } end lemma abs_nonneg (a : α) : 0 ≤ |a| := (le_total 0 a).elim (λ h, h.trans (le_abs_self a)) (λ h, (neg_nonneg.2 h).trans $ neg_le_abs_self a) @[simp] lemma abs_abs (a : α) : | |a| | = |a| := abs_of_nonneg $ abs_nonneg a @[simp] lemma abs_eq_zero : |a| = 0 ↔ a = 0 := decidable.not_iff_not.1 $ ne_comm.trans $ (abs_nonneg a).lt_iff_ne.symm.trans abs_pos @[simp] lemma abs_nonpos_iff {a : α} : |a| ≤ 0 ↔ a = 0 := (abs_nonneg a).le_iff_eq.trans abs_eq_zero variable [covariant_class α α (swap (+)) (≤)] lemma abs_lt : |a| < b ↔ - b < a ∧ a < b := max_lt_iff.trans $ and.comm.trans $ by rw [neg_lt] lemma neg_lt_of_abs_lt (h : |a| < b) : -b < a := (abs_lt.mp h).1 lemma lt_of_abs_lt (h : |a| < b) : a < b := (abs_lt.mp h).2 lemma max_sub_min_eq_abs' (a b : α) : max a b - min a b = |a - b| := begin cases le_total a b with ab ba, { rw [max_eq_right ab, min_eq_left ab, abs_of_nonpos, neg_sub], rwa sub_nonpos }, { rw [max_eq_left ba, min_eq_right ba, abs_of_nonneg], rwa sub_nonneg } end lemma max_sub_min_eq_abs (a b : α) : max a b - min a b = |b - a| := by { rw abs_sub_comm, exact max_sub_min_eq_abs' _ _ } end add_group end covariant_add_le section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] {a b c d : α} lemma abs_le : |a| ≤ b ↔ - b ≤ a ∧ a ≤ b := by rw [abs_le', and.comm, neg_le] lemma neg_le_of_abs_le (h : |a| ≤ b) : -b ≤ a := (abs_le.mp h).1 lemma le_of_abs_le (h : |a| ≤ b) : a ≤ b := (abs_le.mp h).2 /-- The **triangle inequality** in `linear_ordered_add_comm_group`s. -/ lemma abs_add (a b : α) : |a + b| ≤ |a| + |b| := abs_le.2 ⟨(neg_add (|a|) (|b|)).symm ▸ add_le_add (neg_le.2 $ neg_le_abs_self _) (neg_le.2 $ neg_le_abs_self _), add_le_add (le_abs_self _) (le_abs_self _)⟩ theorem abs_sub (a b : α) : |a - b| ≤ |a| + |b| := by { rw [sub_eq_add_neg, ←abs_neg b], exact abs_add a _ } lemma abs_sub_le_iff : |a - b| ≤ c ↔ a - b ≤ c ∧ b - a ≤ c := by rw [abs_le, neg_le_sub_iff_le_add, sub_le_iff_le_add', and_comm, sub_le_iff_le_add'] lemma abs_sub_lt_iff : |a - b| < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add', sub_lt_iff_lt_add', and_comm, sub_lt_iff_lt_add'] lemma sub_le_of_abs_sub_le_left (h : |a - b| ≤ c) : b - c ≤ a := sub_le.1 $ (abs_sub_le_iff.1 h).2 lemma sub_le_of_abs_sub_le_right (h : |a - b| ≤ c) : a - c ≤ b := sub_le_of_abs_sub_le_left (abs_sub_comm a b ▸ h) lemma sub_lt_of_abs_sub_lt_left (h : |a - b| < c) : b - c < a := sub_lt.1 $ (abs_sub_lt_iff.1 h).2 lemma sub_lt_of_abs_sub_lt_right (h : |a - b| < c) : a - c < b := sub_lt_of_abs_sub_lt_left (abs_sub_comm a b ▸ h) lemma abs_sub_abs_le_abs_sub (a b : α) : |a| - |b| ≤ |a - b| := sub_le_iff_le_add.2 $ calc |a| = |a - b + b| : by rw [sub_add_cancel] ... ≤ |a - b| + |b| : abs_add _ _ lemma abs_abs_sub_abs_le_abs_sub (a b : α) : | |a| - |b| | ≤ |a - b| := abs_sub_le_iff.2 ⟨abs_sub_abs_le_abs_sub _ _, by rw abs_sub_comm; apply abs_sub_abs_le_abs_sub⟩ lemma abs_eq (hb : 0 ≤ b) : |a| = b ↔ a = b ∨ a = -b := begin refine ⟨eq_or_eq_neg_of_abs_eq, _⟩, rintro (rfl|rfl); simp only [abs_neg, abs_of_nonneg hb] end lemma abs_le_max_abs_abs (hab : a ≤ b) (hbc : b ≤ c) : |b| ≤ max (|a|) (|c|) := abs_le'.2 ⟨by simp [hbc.trans (le_abs_self c)], by simp [(neg_le_neg_iff.mpr hab).trans (neg_le_abs_self a)]⟩ lemma eq_of_abs_sub_eq_zero {a b : α} (h : |a - b| = 0) : a = b := sub_eq_zero.1 $ abs_eq_zero.1 h lemma abs_sub_le (a b c : α) : |a - c| ≤ |a - b| + |b - c| := calc |a - c| = |a - b + (b - c)| : by rw [sub_add_sub_cancel] ... ≤ |a - b| + |b - c| : abs_add _ _ lemma abs_add_three (a b c : α) : |a + b + c| ≤ |a| + |b| + |c| := (abs_add _ _).trans (add_le_add_right (abs_add _ _) _) lemma dist_bdd_within_interval {a b lb ub : α} (hal : lb ≤ a) (hau : a ≤ ub) (hbl : lb ≤ b) (hbu : b ≤ ub) : |a - b| ≤ ub - lb := abs_sub_le_iff.2 ⟨sub_le_sub hau hbl, sub_le_sub hbu hal⟩ lemma eq_of_abs_sub_nonpos (h : |a - b| ≤ 0) : a = b := eq_of_abs_sub_eq_zero (le_antisymm h (abs_nonneg (a - b))) lemma max_sub_max_le_max (a b c d : α) : max a b - max c d ≤ max (a - c) (b - d) := begin simp only [sub_le_iff_le_add, max_le_iff], split, calc a = a - c + c : (sub_add_cancel a c).symm ... ≤ max (a - c) (b - d) + max c d : add_le_add (le_max_left _ _) (le_max_left _ _), calc b = b - d + d : (sub_add_cancel b d).symm ... ≤ max (a - c) (b - d) + max c d : add_le_add (le_max_right _ _) (le_max_right _ _) end lemma abs_max_sub_max_le_max (a b c d : α) : |max a b - max c d| ≤ max (|a - c|) (|b - d|) := begin refine abs_sub_le_iff.2 ⟨_, _⟩, { exact (max_sub_max_le_max _ _ _ _).trans (max_le_max (le_abs_self _) (le_abs_self _)) }, { rw [abs_sub_comm a c, abs_sub_comm b d], exact (max_sub_max_le_max _ _ _ _).trans (max_le_max (le_abs_self _) (le_abs_self _)) } end lemma abs_min_sub_min_le_max (a b c d : α) : |min a b - min c d| ≤ max (|a - c|) (|b - d|) := by simpa only [max_neg_neg, neg_sub_neg, abs_sub_comm] using abs_max_sub_max_le_max (-a) (-b) (-c) (-d) lemma abs_max_sub_max_le_abs (a b c : α) : |max a c - max b c| ≤ |a - b| := by simpa only [sub_self, abs_zero, max_eq_left (abs_nonneg _)] using abs_max_sub_max_le_max a c b c instance with_top.linear_ordered_add_comm_group_with_top : linear_ordered_add_comm_group_with_top (with_top α) := { neg := option.map (λ a : α, -a), neg_top := @option.map_none _ _ (λ a : α, -a), add_neg_cancel := begin rintro (a | a) ha, { exact (ha rfl).elim }, { exact with_top.coe_add.symm.trans (with_top.coe_eq_coe.2 (add_neg_self a)) } end, .. with_top.linear_ordered_add_comm_monoid_with_top, .. option.nontrivial } end linear_ordered_add_comm_group namespace add_comm_group /-- A collection of elements in an `add_comm_group` designated as "non-negative". This is useful for constructing an `ordered_add_commm_group` by choosing a positive cone in an exisiting `add_comm_group`. -/ @[nolint has_inhabited_instance] structure positive_cone (α : Type*) [add_comm_group α] := (nonneg : α → Prop) (pos : α → Prop := λ a, nonneg a ∧ ¬ nonneg (-a)) (pos_iff : ∀ a, pos a ↔ nonneg a ∧ ¬ nonneg (-a) . order_laws_tac) (zero_nonneg : nonneg 0) (add_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a + b)) (nonneg_antisymm : ∀ {a}, nonneg a → nonneg (-a) → a = 0) /-- A positive cone in an `add_comm_group` induces a linear order if for every `a`, either `a` or `-a` is non-negative. -/ @[nolint has_inhabited_instance] structure total_positive_cone (α : Type*) [add_comm_group α] extends positive_cone α := (nonneg_decidable : decidable_pred nonneg) (nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a)) /-- Forget that a `total_positive_cone` is total. -/ add_decl_doc total_positive_cone.to_positive_cone end add_comm_group namespace ordered_add_comm_group open add_comm_group /-- Construct an `ordered_add_comm_group` by designating a positive cone in an existing `add_comm_group`. -/ def mk_of_positive_cone {α : Type*} [add_comm_group α] (C : positive_cone α) : ordered_add_comm_group α := { le := λ a b, C.nonneg (b - a), lt := λ a b, C.pos (b - a), lt_iff_le_not_le := λ a b, by simp; rw [C.pos_iff]; simp, le_refl := λ a, by simp [C.zero_nonneg], le_trans := λ a b c nab nbc, by simp [-sub_eq_add_neg]; rw ← sub_add_sub_cancel; exact C.add_nonneg nbc nab, le_antisymm := λ a b nab nba, eq_of_sub_eq_zero $ C.nonneg_antisymm nba (by rw neg_sub; exact nab), add_le_add_left := λ a b nab c, by simpa [(≤), preorder.le] using nab, ..‹add_comm_group α› } end ordered_add_comm_group namespace linear_ordered_add_comm_group open add_comm_group /-- Construct a `linear_ordered_add_comm_group` by designating a positive cone in an existing `add_comm_group` such that for every `a`, either `a` or `-a` is non-negative. -/ def mk_of_positive_cone {α : Type*} [add_comm_group α] (C : total_positive_cone α) : linear_ordered_add_comm_group α := { le_total := λ a b, by { convert C.nonneg_total (b - a), change C.nonneg _ = _, congr, simp, }, decidable_le := λ a b, C.nonneg_decidable _, ..ordered_add_comm_group.mk_of_positive_cone C.to_positive_cone } end linear_ordered_add_comm_group namespace prod variables {G H : Type*} @[to_additive] instance [ordered_comm_group G] [ordered_comm_group H] : ordered_comm_group (G × H) := { .. prod.comm_group, .. prod.partial_order G H, .. prod.ordered_cancel_comm_monoid } end prod section type_tags instance [ordered_add_comm_group α] : ordered_comm_group (multiplicative α) := { ..multiplicative.comm_group, ..multiplicative.ordered_comm_monoid } instance [ordered_comm_group α] : ordered_add_comm_group (additive α) := { ..additive.add_comm_group, ..additive.ordered_add_comm_monoid } instance [linear_ordered_add_comm_group α] : linear_ordered_comm_group (multiplicative α) := { ..multiplicative.linear_order, ..multiplicative.ordered_comm_group } instance [linear_ordered_comm_group α] : linear_ordered_add_comm_group (additive α) := { ..additive.linear_order, ..additive.ordered_add_comm_group } end type_tags section norm_num_lemmas /- The following lemmas are stated so that the `norm_num` tactic can use them with the expected signatures. -/ variables [ordered_comm_group α] {a b : α} @[to_additive neg_le_neg] lemma inv_le_inv' : a ≤ b → b⁻¹ ≤ a⁻¹ := inv_le_inv_iff.mpr @[to_additive neg_lt_neg] lemma inv_lt_inv' : a < b → b⁻¹ < a⁻¹ := inv_lt_inv_iff.mpr /- The additive version is also a `linarith` lemma. -/ @[to_additive] theorem inv_lt_one_of_one_lt : 1 < a → a⁻¹ < 1 := inv_lt_one_iff_one_lt.mpr /- The additive version is also a `linarith` lemma. -/ @[to_additive] lemma inv_le_one_of_one_le : 1 ≤ a → a⁻¹ ≤ 1 := inv_le_one'.mpr @[to_additive neg_nonneg_of_nonpos] lemma one_le_inv_of_le_one : a ≤ 1 → 1 ≤ a⁻¹ := one_le_inv'.mpr end norm_num_lemmas
5471df55d4824433c6912fe1f0d7bcc39690accb
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/doc/examples/NFM2022/nfm11.lean
183588ff900f01b2dceaf4e2054bfcbe69eeff90
[ "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
308
lean
/- Structures -/ structure Point where x : Int := 0 y : Int := 0 deriving Repr #eval Point.x (Point.mk 10 20) -- 10 #eval { x := 10, y := 20 : Point } def p : Point := { y := 20 } #eval p.x #eval p.y #eval { p with x := 5 } -- { x := 5, y := 20 } structure Point3D extends Point where z : Int
e3fe75d6a3a6755569711786e571f111c5314c78
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/algebra/field.lean
9306ec2badeaa4746a0973f8b211e354bcd5f630
[ "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
18,401
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis, Leonardo de Moura Structures with multiplicative and additive components, including division rings and fields. The development is modeled after Isabelle's library. -/ prelude import init.algebra.ring universe u /- Make sure instances defined in this file have lower priority than the ones defined for concrete structures -/ set_option default_priority 100 set_option old_structure_cmd true class division_ring (α : Type u) extends ring α, has_inv α, zero_ne_one_class α := (mul_inv_cancel : ∀ {a : α}, a ≠ 0 → a * a⁻¹ = 1) (inv_mul_cancel : ∀ {a : α}, a ≠ 0 → a⁻¹ * a = 1) variable {α : Type u} section division_ring variables [division_ring α] protected definition algebra.div (a b : α) : α := a * b⁻¹ instance division_ring_has_div [division_ring α] : has_div α := ⟨algebra.div⟩ lemma division_def (a b : α) : a / b = a * b⁻¹ := rfl @[simp] lemma mul_inv_cancel {a : α} (h : a ≠ 0) : a * a⁻¹ = 1 := division_ring.mul_inv_cancel h @[simp] lemma inv_mul_cancel {a : α} (h : a ≠ 0) : a⁻¹ * a = 1 := division_ring.inv_mul_cancel h @[simp] lemma one_div_eq_inv (a : α) : 1 / a = a⁻¹ := one_mul a⁻¹ lemma inv_eq_one_div (a : α) : a⁻¹ = 1 / a := by simp local attribute [simp] division_def mul_comm mul_assoc mul_left_comm mul_inv_cancel inv_mul_cancel lemma div_eq_mul_one_div (a b : α) : a / b = a * (1 / b) := by simp lemma mul_one_div_cancel {a : α} (h : a ≠ 0) : a * (1 / a) = 1 := by simp [h] lemma one_div_mul_cancel {a : α} (h : a ≠ 0) : (1 / a) * a = 1 := by simp [h] lemma div_self {a : α} (h : a ≠ 0) : a / a = 1 := by simp [h] lemma one_div_one : 1 / 1 = (1:α) := div_self (ne.symm zero_ne_one) lemma mul_div_assoc (a b c : α) : (a * b) / c = a * (b / c) := by simp lemma one_div_ne_zero {a : α} (h : a ≠ 0) : 1 / a ≠ 0 := assume : 1 / a = 0, have 0 = (1:α), from eq.symm (by rw [← mul_one_div_cancel h, this, mul_zero]), absurd this zero_ne_one lemma one_inv_eq : 1⁻¹ = (1:α) := calc 1⁻¹ = 1 * 1⁻¹ : by rw [one_mul] ... = (1:α) : by simp local attribute [simp] one_inv_eq lemma div_one (a : α) : a / 1 = a := by simp lemma zero_div (a : α) : 0 / a = 0 := by simp -- note: integral domain has a "mul_ne_zero". α commutative division ring is an integral -- domain, but let's not define that class for now. lemma division_ring.mul_ne_zero {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := assume : a * b = 0, have a * 1 = 0, by rw [← mul_one_div_cancel hb, ← mul_assoc, this, zero_mul], have a = 0, by rwa mul_one at this, absurd this ha lemma mul_ne_zero_comm {a b : α} (h : a * b ≠ 0) : b * a ≠ 0 := have h₁ : a ≠ 0, from ne_zero_of_mul_ne_zero_right h, have h₂ : b ≠ 0, from ne_zero_of_mul_ne_zero_left h, division_ring.mul_ne_zero h₂ h₁ lemma eq_one_div_of_mul_eq_one {a b : α} (h : a * b = 1) : b = 1 / a := have a ≠ 0, from assume : a = 0, have 0 = (1:α), by rwa [this, zero_mul] at h, absurd this zero_ne_one, have b = (1 / a) * a * b, by rw [one_div_mul_cancel this, one_mul], show b = 1 / a, by rwa [mul_assoc, h, mul_one] at this lemma eq_one_div_of_mul_eq_one_left {a b : α} (h : b * a = 1) : b = 1 / a := have a ≠ 0, from assume : a = 0, have 0 = (1:α), by rwa [this, mul_zero] at h, absurd this zero_ne_one, by rw [← h, mul_div_assoc, div_self this, mul_one] lemma division_ring.one_div_mul_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (1 / b) = 1 / (b * a) := have (b * a) * ((1 / a) * (1 / b)) = 1, by rw [mul_assoc, ← mul_assoc a, mul_one_div_cancel ha, one_mul, mul_one_div_cancel hb], eq_one_div_of_mul_eq_one this lemma one_div_neg_one_eq_neg_one : (1:α) / (-1) = -1 := have (-1) * (-1) = (1:α), by rw [neg_mul_neg, one_mul], eq.symm (eq_one_div_of_mul_eq_one this) lemma division_ring.one_div_neg_eq_neg_one_div {a : α} (h : a ≠ 0) : 1 / (- a) = - (1 / a) := have -1 ≠ (0:α), from (assume : -1 = 0, absurd (eq.symm (calc 1 = -(-1) : (neg_neg (1:α)).symm ... = -0 : by rw this ... = (0:α) : neg_zero)) zero_ne_one), calc 1 / (- a) = 1 / ((-1) * a) : by rw neg_eq_neg_one_mul ... = (1 / a) * (1 / (- 1)) : by rw (division_ring.one_div_mul_one_div h this) ... = (1 / a) * (-1) : by rw one_div_neg_one_eq_neg_one ... = - (1 / a) : by rw [mul_neg_eq_neg_mul_symm, mul_one] lemma div_neg_eq_neg_div {a : α} (b : α) (ha : a ≠ 0) : b / (- a) = - (b / a) := calc b / (- a) = b * (1 / (- a)) : by rw [← inv_eq_one_div, division_def] ... = b * -(1 / a) : by rw (division_ring.one_div_neg_eq_neg_one_div ha) ... = -(b * (1 / a)) : by rw neg_mul_eq_mul_neg ... = - (b * a⁻¹) : by rw inv_eq_one_div lemma neg_div (a b : α) : (-b) / a = - (b / a) := by rw [neg_eq_neg_one_mul, mul_div_assoc, ← neg_eq_neg_one_mul] lemma division_ring.neg_div_neg_eq (a : α) {b : α} (hb : b ≠ 0) : (-a) / (-b) = a / b := by rw [(div_neg_eq_neg_div _ hb), neg_div, neg_neg] lemma division_ring.one_div_one_div {a : α} (h : a ≠ 0) : 1 / (1 / a) = a := eq.symm (eq_one_div_of_mul_eq_one_left (mul_one_div_cancel h)) lemma division_ring.eq_of_one_div_eq_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) (h : 1 / a = 1 / b) : a = b := by rw [← division_ring.one_div_one_div ha, h, (division_ring.one_div_one_div hb)] lemma mul_inv_eq {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (b * a)⁻¹ = a⁻¹ * b⁻¹ := eq.symm $ calc a⁻¹ * b⁻¹ = (1 / a) * (1 / b) : by simp ... = (1 / (b * a)) : division_ring.one_div_mul_one_div ha hb ... = (b * a)⁻¹ : by simp lemma mul_div_cancel (a : α) {b : α} (hb : b ≠ 0) : a * b / b = a := by simp [hb] lemma div_mul_cancel (a : α) {b : α} (hb : b ≠ 0) : a / b * b = a := by simp [hb] lemma div_add_div_same (a b c : α) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma div_sub_div_same (a b c : α) : (a / c) - (b / c) = (a - b) / c := by rw [sub_eq_add_neg, ← neg_div, div_add_div_same, sub_eq_add_neg] lemma one_div_mul_add_mul_one_div_eq_one_div_add_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (a + b) * (1 / b) = 1 / a + 1 / b := by rw [(left_distrib (1 / a)), (one_div_mul_cancel ha), right_distrib, one_mul, mul_assoc, (mul_one_div_cancel hb), mul_one, add_comm] lemma one_div_mul_sub_mul_one_div_eq_one_div_add_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (b - a) * (1 / b) = 1 / a - 1 / b := by rw [(mul_sub_left_distrib (1 / a)), (one_div_mul_cancel ha), mul_sub_right_distrib, one_mul, mul_assoc, (mul_one_div_cancel hb), mul_one] lemma div_eq_one_iff_eq (a : α) {b : α} (hb : b ≠ 0) : a / b = 1 ↔ a = b := iff.intro (assume : a / b = 1, calc a = a / b * b : by simp [hb] ... = 1 * b : by rw this ... = b : by simp) (assume : a = b, by simp [this, hb]) lemma eq_of_div_eq_one (a : α) {b : α} (Hb : b ≠ 0) : a / b = 1 → a = b := iff.mp $ div_eq_one_iff_eq a Hb lemma eq_div_iff_mul_eq (a b : α) {c : α} (hc : c ≠ 0) : a = b / c ↔ a * c = b := iff.intro (assume : a = b / c, by rw [this, (div_mul_cancel _ hc)]) (assume : a * c = b, by rw [← this, mul_div_cancel _ hc]) lemma eq_div_of_mul_eq (a b : α) {c : α} (hc : c ≠ 0) : a * c = b → a = b / c := iff.mpr $ eq_div_iff_mul_eq a b hc lemma mul_eq_of_eq_div (a b: α) {c : α} (hc : c ≠ 0) : a = b / c → a * c = b := iff.mp $ eq_div_iff_mul_eq a b hc lemma add_div_eq_mul_add_div (a b : α) {c : α} (hc : c ≠ 0) : a + b / c = (a * c + b) / c := have (a + b / c) * c = a * c + b, by rw [right_distrib, (div_mul_cancel _ hc)], (iff.mpr (eq_div_iff_mul_eq _ _ hc)) this lemma mul_mul_div (a : α) {c : α} (hc : c ≠ 0) : a = a * c * (1 / c) := by simp [hc] end division_ring class field (α : Type u) extends division_ring α, comm_ring α section field variable [field α] lemma field.one_div_mul_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (1 / b) = 1 / (a * b) := by rw [(division_ring.one_div_mul_one_div ha hb), mul_comm b] lemma field.div_mul_right {a b : α} (hb : b ≠ 0) (h : a * b ≠ 0) : a / (a * b) = 1 / b := have a ≠ 0, from ne_zero_of_mul_ne_zero_right h, eq.symm (calc 1 / b = a * ((1 / a) * (1 / b)) : by rw [← mul_assoc, mul_one_div_cancel this, one_mul] ... = a * (1 / (b * a)) : by rw (division_ring.one_div_mul_one_div this hb) ... = a * (a * b)⁻¹ : by rw [inv_eq_one_div, mul_comm a b]) lemma field.div_mul_left {a b : α} (ha : a ≠ 0) (h : a * b ≠ 0) : b / (a * b) = 1 / a := have b * a ≠ 0, from mul_ne_zero_comm h, by rw [mul_comm a, (field.div_mul_right ha this)] lemma mul_div_cancel_left {a : α} (b : α) (ha : a ≠ 0) : a * b / a = b := by rw [mul_comm a, (mul_div_cancel _ ha)] lemma mul_div_cancel' (a : α) {b : α} (hb : b ≠ 0) : b * (a / b) = a := by rw [mul_comm, (div_mul_cancel _ hb)] lemma one_div_add_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : 1 / a + 1 / b = (a + b) / (a * b) := have a * b ≠ 0, from (division_ring.mul_ne_zero ha hb), by rw [add_comm, ← field.div_mul_left ha this, ← field.div_mul_right hb this, division_def, division_def, division_def, ← right_distrib] lemma field.div_mul_div (a : α) {b : α} (c : α) {d : α} (hb : b ≠ 0) (hd : d ≠ 0) : (a / b) * (c / d) = (a * c) / (b * d) := begin simp [division_def], rw [mul_inv_eq hd hb, mul_comm d⁻¹] end lemma mul_div_mul_left (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : (c * a) / (c * b) = a / b := by rw [← field.div_mul_div _ _ hc hb, div_self hc, one_mul] lemma mul_div_mul_right (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : (a * c) / (b * c) = a / b := by rw [mul_comm a, mul_comm b, mul_div_mul_left _ hb hc] lemma div_mul_eq_mul_div (a b c : α) : (b / c) * a = (b * a) / c := by simp [division_def] lemma field.div_mul_eq_mul_div_comm (a b : α) {c : α} (hc : c ≠ 0) : (b / c) * a = b * (a / c) := by rw [div_mul_eq_mul_div, ← one_mul c, ← field.div_mul_div _ _ (ne.symm zero_ne_one) hc, div_one, one_mul] lemma div_add_div (a : α) {b : α} (c : α) {d : α} (hb : b ≠ 0) (hd : d ≠ 0) : (a / b) + (c / d) = ((a * d) + (b * c)) / (b * d) := by rw [← mul_div_mul_right _ hb hd, ← mul_div_mul_left _ hd hb, div_add_div_same] lemma div_sub_div (a : α) {b : α} (c : α) {d : α} (hb : b ≠ 0) (hd : d ≠ 0) : (a / b) - (c / d) = ((a * d) - (b * c)) / (b * d) := begin simp [sub_eq_add_neg], rw [neg_eq_neg_one_mul, ← mul_div_assoc, div_add_div _ _ hb hd, ← mul_assoc, mul_comm b, mul_assoc, ← neg_eq_neg_one_mul] end lemma mul_eq_mul_of_div_eq_div (a : α) {b : α} (c : α) {d : α} (hb : b ≠ 0) (hd : d ≠ 0) (h : a / b = c / d) : a * d = c * b := by rw [← mul_one (a*d), mul_assoc, mul_comm d, ← mul_assoc, ← div_self hb, ← field.div_mul_eq_mul_div_comm _ _ hb, h, div_mul_eq_mul_div, div_mul_cancel _ hd] lemma field.one_div_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : 1 / (a / b) = b / a := have (a / b) * (b / a) = 1, from calc (a / b) * (b / a) = (a * b) / (b * a) : field.div_mul_div _ _ hb ha ... = (a * b) / (a * b) : by rw mul_comm ... = 1 : div_self (division_ring.mul_ne_zero ha hb), eq.symm (eq_one_div_of_mul_eq_one this) lemma field.div_div_eq_mul_div (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : a / (b / c) = (a * c) / b := by rw [div_eq_mul_one_div, field.one_div_div hb hc, ← mul_div_assoc] lemma field.div_div_eq_div_mul (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : (a / b) / c = a / (b * c) := by rw [div_eq_mul_one_div, field.div_mul_div _ _ hb hc, mul_one] lemma field.div_div_div_div_eq (a : α) {b c d : α} (hb : b ≠ 0) (hc : c ≠ 0) (hd : d ≠ 0) : (a / b) / (c / d) = (a * d) / (b * c) := by rw [field.div_div_eq_mul_div _ hc hd, div_mul_eq_mul_div, field.div_div_eq_div_mul _ hb hc] lemma field.div_mul_eq_div_mul_one_div (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : a / (b * c) = (a / b) * (1 / c) := by rw [← field.div_div_eq_div_mul _ hb hc, ← div_eq_mul_one_div] lemma eq_of_mul_eq_mul_of_nonzero_left {a b c : α} (h : a ≠ 0) (h₂ : a * b = a * c) : b = c := by rw [← one_mul b, ← div_self h, div_mul_eq_mul_div, h₂, mul_div_cancel_left _ h] lemma eq_of_mul_eq_mul_of_nonzero_right {a b c : α} (h : c ≠ 0) (h2 : a * c = b * c) : a = b := by rw [← mul_one a, ← div_self h, ← mul_div_assoc, h2, mul_div_cancel _ h] end field class discrete_field (α : Type u) extends field α := (has_decidable_eq : decidable_eq α) (inv_zero : inv zero = zero) attribute [instance] discrete_field.has_decidable_eq section discrete_field variable [discrete_field α] -- many of the lemmas in discrete_field are the same as lemmas in field or division ring, -- but with fewer hypotheses since 0⁻¹ = 0 and equality is decidable. lemma discrete_field.eq_zero_or_eq_zero_of_mul_eq_zero (a b : α) (h : a * b = 0) : a = 0 ∨ b = 0 := decidable.by_cases (assume : a = 0, or.inl this) (assume : a ≠ 0, or.inr (by rw [← one_mul b, ← inv_mul_cancel this, mul_assoc, h, mul_zero])) instance discrete_field.to_integral_domain [s : discrete_field α] : integral_domain α := {s with eq_zero_or_eq_zero_of_mul_eq_zero := discrete_field.eq_zero_or_eq_zero_of_mul_eq_zero} lemma inv_zero : 0⁻¹ = (0:α) := discrete_field.inv_zero α lemma one_div_zero : 1 / 0 = (0:α) := calc 1 / 0 = (1:α) * 0⁻¹ : by rw division_def ... = 1 * 0 : by rw inv_zero ... = (0:α) : by rw mul_zero lemma div_zero (a : α) : a / 0 = 0 := by rw [div_eq_mul_one_div, one_div_zero, mul_zero] lemma ne_zero_of_one_div_ne_zero {a : α} (h : 1 / a ≠ 0) : a ≠ 0 := assume ha : a = 0, begin rw [ha, one_div_zero] at h, contradiction end lemma eq_zero_of_one_div_eq_zero {a : α} (h : 1 / a = 0) : a = 0 := decidable.by_cases (assume ha, ha) (assume ha, false.elim ((one_div_ne_zero ha) h)) lemma one_div_mul_one_div' (a b : α) : (1 / a) * (1 / b) = 1 / (b * a) := decidable.by_cases (assume : a = 0, by rw [this, div_zero, zero_mul, mul_zero, div_zero]) (assume ha : a ≠ 0, decidable.by_cases (assume : b = 0, by rw [this, div_zero, mul_zero, zero_mul, div_zero]) (assume : b ≠ 0, division_ring.one_div_mul_one_div ha this)) lemma one_div_neg_eq_neg_one_div (a : α) : 1 / (- a) = - (1 / a) := decidable.by_cases (assume : a = 0, by rw [this, neg_zero, div_zero, neg_zero]) (assume : a ≠ 0, division_ring.one_div_neg_eq_neg_one_div this) lemma neg_div_neg_eq (a b : α) : (-a) / (-b) = a / b := decidable.by_cases (assume hb : b = 0, by rw [hb, neg_zero, div_zero, div_zero]) (assume hb : b ≠ 0, division_ring.neg_div_neg_eq _ hb) lemma one_div_one_div (a : α) : 1 / (1 / a) = a := decidable.by_cases (assume ha : a = 0, by rw [ha, div_zero, div_zero]) (assume ha : a ≠ 0, division_ring.one_div_one_div ha) lemma eq_of_one_div_eq_one_div {a b : α} (h : 1 / a = 1 / b) : a = b := decidable.by_cases (assume ha : a = 0, have hb : b = 0, from eq_zero_of_one_div_eq_zero (by rw [← h, ha, div_zero]), hb.symm ▸ ha) (assume ha : a ≠ 0, have hb : b ≠ 0, from ne_zero_of_one_div_ne_zero (h ▸ (one_div_ne_zero ha)), division_ring.eq_of_one_div_eq_one_div ha hb h) lemma mul_inv' (a b : α) : (b * a)⁻¹ = a⁻¹ * b⁻¹ := decidable.by_cases (assume ha : a = 0, by rw [ha, mul_zero, inv_zero, zero_mul]) (assume ha : a ≠ 0, decidable.by_cases (assume hb : b = 0, by rw [hb, zero_mul, inv_zero, mul_zero]) (assume hb : b ≠ 0, mul_inv_eq ha hb)) -- the following are specifically for fields lemma one_div_mul_one_div (a b : α) : (1 / a) * (1 / b) = 1 / (a * b) := by rw [one_div_mul_one_div', mul_comm b] lemma div_mul_right {a : α} (b : α) (ha : a ≠ 0) : a / (a * b) = 1 / b := decidable.by_cases (assume hb : b = 0, by rw [hb, mul_zero, div_zero, div_zero]) (assume hb : b ≠ 0, field.div_mul_right hb (mul_ne_zero ha hb)) lemma div_mul_left (a : α) {b : α} (hb : b ≠ 0) : b / (a * b) = 1 / a := by rw [mul_comm a, div_mul_right _ hb] lemma div_mul_div (a b c d : α) : (a / b) * (c / d) = (a * c) / (b * d) := decidable.by_cases (assume hb : b = 0, by rw [hb, div_zero, zero_mul, zero_mul, div_zero]) (assume hb : b ≠ 0, decidable.by_cases (assume hd : d = 0, by rw [hd, div_zero, mul_zero, mul_zero, div_zero]) (assume hd : d ≠ 0, field.div_mul_div _ _ hb hd)) lemma mul_div_mul_left' (a b : α) {c : α} (hc : c ≠ 0) : (c * a) / (c * b) = a / b := decidable.by_cases (assume hb : b = 0, by rw [hb, mul_zero, div_zero, div_zero]) (assume hb : b ≠ 0, mul_div_mul_left _ hb hc) lemma mul_div_mul_right' (a b : α) {c : α} (hc : c ≠ 0) : (a * c) / (b * c) = a / b := by rw [mul_comm a, mul_comm b, (mul_div_mul_left' _ _ hc)] lemma div_mul_eq_mul_div_comm (a b c : α) : (b / c) * a = b * (a / c) := decidable.by_cases (assume hc : c = 0, by rw [hc, div_zero, zero_mul, div_zero, mul_zero]) (assume hc : c ≠ 0, field.div_mul_eq_mul_div_comm _ _ hc) lemma one_div_div (a b : α) : 1 / (a / b) = b / a := decidable.by_cases (assume ha : a = 0, by rw [ha, zero_div, div_zero, div_zero]) (assume ha : a ≠ 0, decidable.by_cases (assume hb : b = 0, by rw [hb, div_zero, zero_div, div_zero]) (assume hb : b ≠ 0, field.one_div_div ha hb)) lemma div_div_eq_mul_div (a b c : α) : a / (b / c) = (a * c) / b := by rw [div_eq_mul_one_div, one_div_div, ← mul_div_assoc] lemma div_div_eq_div_mul (a b c : α) : (a / b) / c = a / (b * c) := by rw [div_eq_mul_one_div, div_mul_div, mul_one] lemma div_div_div_div_eq (a b c d : α) : (a / b) / (c / d) = (a * d) / (b * c) := by rw [div_div_eq_mul_div, div_mul_eq_mul_div, div_div_eq_div_mul] lemma div_helper {a : α} (b : α) (h : a ≠ 0) : (1 / (a * b)) * a = 1 / b := by rw [div_mul_eq_mul_div, one_mul, div_mul_right _ h] lemma div_mul_eq_div_mul_one_div (a b c : α) : a / (b * c) = (a / b) * (1 / c) := by rw [← div_div_eq_div_mul, ← div_eq_mul_one_div] end discrete_field
ecdb91faeb9e66b6bd6ad5174e48ed40c6a8f4fe
9cb9db9d79fad57d80ca53543dc07efb7c4f3838
/src/system_of_complexes/basic.lean
6770785a0057456b0cbc8353ffda47b25cae5933
[]
no_license
mr-infty/lean-liquid
3ff89d1f66244b434654c59bdbd6b77cb7de0109
a8db559073d2101173775ccbd85729d3a4f1ed4d
refs/heads/master
1,678,465,145,334
1,614,565,310,000
1,614,565,310,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,475
lean
import algebra.homology.chain_complex import normed_group.NormedGroup import algebra.ordered_group import facts import tactic.gptf universe variables v u noncomputable theory open opposite category_theory open_locale nnreal /-! # Systems of complexes of normed abelian groups In this file we define systems of complexes of normed abelian groups, along the lines of Definition 9.3 of [Analytic]. ## Main declarations * `system_of_complexes`: a system of complexes of normed abelian groups. * `is_bounded_exact`: an exactness criterion for such systems, requiring a suitable interplay between the norms and the algebraic properties of the system. * `admissible`: such a system is *admissible* if all maps that occur in the system are norm-nonincreasing. -/ -- TODO: at some point we can abstract the following definition over `NormedGroup` and `ℝ≥0`. -- But I don't think that is relevant for this project. /-- A system of complexes of normed abelian groups, indexed by `ℝ≥0`. See also Definition 9.3 of [Analytic]. Implementation detail: `cochain_complex` assumes that the complex is indexed by `ℤ`, whereas we are interested in complexes indexed by `ℕ`. We therefore set all objects indexed by negative integers to `0`, in our use case. -/ @[derive category_theory.category] def system_of_complexes : Type* := ℝ≥0ᵒᵖ ⥤ (cochain_complex NormedGroup) variables {M M' N : system_of_complexes.{u}} (f : M ⟶ M') (g : M' ⟶ N) instance : has_coe_to_fun system_of_complexes := ⟨λ C, ℝ≥0 → ℤ → NormedGroup, λ C c i, (C.obj $ op c).X i⟩ /-- `f.apply c i` is application of the natural transformation `f`: $f_c^i : M_c^i ⟶ N_c^i$. -/ def category_theory.has_hom.hom.apply (f : M ⟶ N) {c : ℝ≥0} {i : ℤ} : M c i ⟶ N c i := (f.app (op c)).f i instance hom_to_fun : has_coe_to_fun (M ⟶ N) := ⟨λ f, Π {c : ℝ≥0} {i : ℤ}, M c i → N c i, λ f {c} {i} x, f.apply x⟩ lemma system_of_complexes.map_sub (f : M ⟶ N) {c i} (m m' : M c i) : f (m-m') = f m - f m' := normed_group_hom.map_sub _ _ _ /-- `f.apply c i` is application of the natural isomorphism `f`: $f_c^i : M_c^i ≅ N_c^i$. -/ def category_theory.iso.apply (f : M ≅ N) {c : ℝ≥0} {i : ℤ} : M c i ≅ N c i := pi.iso_app (differential_object.iso_app $ f.app $ op c) i namespace system_of_complexes variables (C C₁ C₂ : system_of_complexes.{u}) /-- `res` is the restriction map `C c' i ⟶ C c i` for a system of complexes `C`, and nonnegative reals `c ≤ c'`. -/ def res {C : system_of_complexes} {c' c : ℝ≥0} {i : ℤ} [h : fact (c ≤ c')] : C c' i ⟶ C c i := (C.map (hom_of_le h).op).f i variables {c₁ c₂ c₃ : ℝ≥0} (i : ℤ) @[simp] lemma res_comp_res (h₁ : fact (c₂ ≤ c₁)) (h₂ : fact (c₃ ≤ c₂)) : @res C _ _ i h₁ ≫ @res C _ _ i h₂ = @res C _ _ i (le_trans h₂ h₁) := begin have := (category_theory.functor.map_comp C (hom_of_le h₁).op (hom_of_le h₂).op), rw [← op_comp] at this, delta res, erw this, refl, end @[simp] lemma res_res (h₁ : fact (c₂ ≤ c₁)) (h₂ : fact (c₃ ≤ c₂)) (x : C c₁ i) : @res C _ _ i h₂ (@res C _ _ i h₁ x) = @res C _ _ i (le_trans h₂ h₁) x := by { rw ← (C.res_comp_res i h₁ h₂), refl } /-- `C.d` is the differential `C c i ⟶ C c (i+1)` for a system of complexes `C`. -/ def d {C : system_of_complexes} {c : ℝ≥0} {i : ℤ} : C c i ⟶ C c (i+1) := (C.obj $ op c).d i lemma d_comp_d (c : ℝ≥0) (i : ℤ) : (d : C c i ⟶ C c (i+1)) ≫ d = 0 := (C.obj $ op c).d_squared _ @[simp] lemma d_d (c : ℝ≥0) (i : ℤ) (x : C c i) : d (d x) = 0 := show ((d : C c i ⟶ C c (i+1)) ≫ d) x = 0, by { rw d_comp_d, refl } lemma d_comp_res (h : fact (c₂ ≤ c₁)) : @d C c₁ i ≫ @res C _ _ _ h = @res C _ _ i _ ≫ @d C c₂ i := homological_complex.comm_at (C.map (hom_of_le h).op) i lemma d_res (h : fact (c₂ ≤ c₁)) (x) : @d C c₂ i (@res C _ _ i _ x) = @res C _ _ _ h (@d C c₁ i x) := show (@res C _ _ i _ ≫ @d C c₂ i) x = (@d C c₁ i ≫ @res C _ _ _ h) x, by rw d_comp_res section iso variables (ϕ : M ≅ N) (c : ℝ≥0) (i) lemma apply_hom_eq_hom_apply : (ϕ.apply.hom : M c i ⟶ N c i) = ϕ.hom.apply := rfl lemma apply_inv_eq_inv_apply : (ϕ.apply.inv : N c i ⟶ M c i) = ϕ.inv.apply := rfl @[simp] lemma hom_apply_comp_inv_apply : (ϕ.hom.apply : M c i ⟶ N c i) ≫ ϕ.inv.apply = 𝟙 _ := by rw [← apply_hom_eq_hom_apply, ← apply_inv_eq_inv_apply, iso.hom_inv_id] @[simp] lemma inv_apply_comp_hom_apply : (ϕ.inv.apply : N c i ⟶ M c i) ≫ ϕ.hom.apply = 𝟙 _ := by rw [← apply_hom_eq_hom_apply, ← apply_inv_eq_inv_apply, iso.inv_hom_id] @[simp] lemma inv_apply_hom_apply (x : M c i) : ϕ.inv.apply (ϕ.hom.apply x) = x := show ((ϕ.hom.apply : M c i ⟶ N c i) ≫ ϕ.inv.apply) x = x, by simp only [hom_apply_comp_inv_apply, coe_id, id.def] @[simp] lemma hom_apply_inv_apply (x : N c i) : ϕ.hom (ϕ.inv x) = x := show ((ϕ.inv.apply : N c i ⟶ M c i) ≫ ϕ.hom.apply) x = x, by simp only [inv_apply_comp_hom_apply, coe_id, id.def] end iso /-- Convenience definition: The identity morphism of an object in the system of complexes when it is given by different indices that are not definitionally equal. -/ def congr {c c' : ℝ≥0} {i i' : ℤ} (hc : c = c') (hi : i = i') : C c i ⟶ C c' i' := eq_to_hom $ by { subst hc, subst hi } variables (M M' N) lemma d_apply (f : M ⟶ N) {c : ℝ≥0} {i : ℤ} (m : M c i) : d (f m) = f (d m) := begin have h : ((M.obj (op c)).d i ≫ (f.app (op c)).f (i + 1)) m = (f.app (op c)).f (i + 1) ((M.obj (op c)).d i m), { exact coe_comp ((M.obj (op c)).d i) ((f.app (op c)).f (i + 1)) m }, rwa [homological_complex.comm_at (f.app (op c)) i] at h, end lemma res_comp_apply (f : M ⟶ N) (c c' : ℝ≥0) [h : fact (c ≤ c')] (i : ℤ) : @res M c' c i _ ≫ f.apply = f.apply ≫ res := begin have step1 := f.naturality (hom_of_le h).op, have step2 := congr_arg differential_object.hom.f step1, exact congr_fun step2 i end lemma res_apply (f : M ⟶ N) (c c' : ℝ≥0) [h : fact (c ≤ c')] {i : ℤ} (m : M c' i) : @res N c' c _ _ (f m) = f (res m) := begin show (f.apply ≫ (@res N c' c _ _)) m = (@res M c' c _ _ ≫ (f.apply)) m, rw res_comp_apply end /-- A system of complexes is *admissible* if all the differentials and restriction maps are norm-nonincreasing. See Definition 9.3 of [Analytic]. -/ structure admissible (C : system_of_complexes) : Prop := (d_norm_noninc : ∀ c i, (d : C c i ⟶ C c (i+1)).norm_noninc) (res_norm_noninc : ∀ c' c i h, (@res C c' c i h).norm_noninc) /-- `is_bounded_exact k K m c₀` is a predicate on systems of complexes. A system of complexes `C` is `(k,K)`-exact in degrees `≤ m` for `c ≥ c₀`* if the following condition is satisfied: For all `c ≥ c₀` and all `x : C (k * c) i` with `i ≤ m` there is some `y : C c (i-1)` (which is defined to be `0` when `i = 0`) such that `∥(C.res x) - (C.d y)∥ ≤ K * ∥C.d x∥`. See Definition 9.3 of [Analytic] (which coalesces the roles of `k` and `K`). Implementation details: * Because our chain complexes are indexed by `ℤ` instead of `ℕ`, and we make sure that objects indexed by negative integers are `0`, we automatically take care of the parenthetical condition about `i = 0`. * The original text bounds `i` as `i ≤ m`, and then requires `y : C c (i-1)`. We change this to `i < m` and `y : C c i`, because this has better definitional properties. (This is a hack around an inconvenience known as dependent type theory hell.) -/ def is_bounded_exact (k K : ℝ≥0) (m : ℤ) [hk : fact (1 ≤ k)] (c₀ : ℝ≥0) : Prop := ∀ c ≥ c₀, ∀ i < m, ∀ x : C (k * c) (i+1), ∃ y : C c i, ∥res x - d y∥ ≤ K * ∥d x∥ /-- Weak version of `is_bounded_exact`. -/ def is_weak_bounded_exact (k K : ℝ≥0) (m : ℤ) [hk : fact (1 ≤ k)] (c₀ : ℝ≥0) : Prop := ∀ c ≥ c₀, ∀ i < m, ∀ x : C (k * c) (i+1), ∀ ε > 0, ∃ y : C c i, ∥res x - d y∥ ≤ K * ∥d x∥ + ε namespace is_weak_bounded_exact variables {C C₁ C₂} variables {k k' K K' : ℝ≥0} {m m' : ℤ} {c₀ c₀' : ℝ≥0} [fact (1 ≤ k)] [fact (1 ≤ k')] lemma of_le (hC : C.is_weak_bounded_exact k K m c₀) (hC_adm : C.admissible) (hk : k ≤ k') (hK : K ≤ K') (hm : m' ≤ m) (hc₀ : c₀ ≤ c₀') : C.is_weak_bounded_exact k' K' m' c₀' := begin intros c hc i hi x ε ε_pos, haveI : fact (k ≤ k') := hk, obtain ⟨y, hy⟩ := hC c (hc₀.trans hc) i (lt_of_lt_of_le hi hm) (res x) ε ε_pos, use y, simp only [res_res] at hy, refine le_trans hy _, rw d_res, apply add_le_add_right, exact mul_le_mul hK (hC_adm.res_norm_noninc _ _ _ _ (d x)) (norm_nonneg _) ((zero_le K).trans hK) end end is_weak_bounded_exact namespace is_bounded_exact variables {C C₁ C₂} variables {k k' K K' : ℝ≥0} {m m' : ℤ} {c₀ c₀' : ℝ≥0} [fact (1 ≤ k)] [fact (1 ≤ k')] lemma of_le (hC : C.is_bounded_exact k K m c₀) (hC_adm : C.admissible) (hk : k ≤ k') (hK : K ≤ K') (hm : m' ≤ m) (hc₀ : c₀ ≤ c₀') : C.is_bounded_exact k' K' m' c₀' := begin intros c hc i hi x, haveI : fact (k ≤ k') := hk, obtain ⟨y, hy⟩ := hC c (hc₀.trans hc) i (lt_of_lt_of_le hi hm) (res x), use y, simp only [res_res] at hy, refine le_trans hy _, rw d_res, exact mul_le_mul hK (hC_adm.res_norm_noninc _ _ _ _ (d x)) (norm_nonneg _) ((zero_le K).trans hK) end lemma of_iso (h : C₁.is_bounded_exact k K m c₀) (f : C₁ ≅ C₂) (hf : ∀ c i, (f.hom.apply : C₁ c i ⟶ C₂ c i).is_strict) : C₂.is_bounded_exact k K m c₀ := begin intros c hc i hi x, obtain ⟨y, hy⟩ := h c hc i hi (f.inv.apply x), refine ⟨f.hom y, _⟩, calc ∥res x - d (f.hom y)∥ = ∥res x - f.hom (d y)∥ : by rw d_apply ... = ∥f.hom (f.inv (res x)) - f.hom (d y)∥ : by rw hom_apply_inv_apply ... = ∥f.hom (f.inv (res x) - d y)∥ : by congr ; exact (f.hom.apply.map_sub _ _).symm ... = ∥f.inv (res x) - d y∥ : hf _ _ _ ... = ∥res (f.inv x) - d y∥ : by rw res_apply ... ≤ K * ∥d (f.inv x)∥ : hy ... = K * ∥d x∥ : congr_arg _ _, calc ∥d (f.inv x)∥ = ∥f.inv (d x)∥ : by rw d_apply ... = ∥f.hom (f.inv (d x))∥ : (hf _ _ _).symm ... = ∥d x∥ : by rw hom_apply_inv_apply end lemma of_shift {k K : ℝ≥0} {m : ℤ} [hk : fact (1 ≤ k)] {c₀ : ℝ≥0} (H : ∀ c ≥ c₀, ∀ i < m - 1, ∀ x : C (k * c) (i + 1 + 1), ∃ y : C c (i + 1), ∥res x - d y∥ ≤ K * ∥d x∥) : C.is_bounded_exact k K m c₀ := begin intros c hc i hi x, specialize H c hc (i-1) (by linarith), rw [sub_add_cancel] at H, exact H x, end end is_bounded_exact namespace is_weak_bounded_exact variables {C C₁ C₂} variables {k k' K K' : ℝ≥0} {m m' : ℤ} {c₀ c₀' : ℝ≥0} [fact (1 ≤ k)] [fact (1 ≤ k')] lemma to_exact (hC : C.is_weak_bounded_exact k K m c₀) {δ : ℝ≥0} (hδ : 0 < δ) (H : ∀ c ≥ c₀, ∀ i < m - 1, ∀ x : C (k * c) (i + 1 + 1), d x = 0 → ∃ y : C c (i + 1), res x = d y) : C.is_bounded_exact k (K + δ) m c₀ := begin apply is_bounded_exact.of_shift, intros c hc i hi x, by_cases hdx : d x = 0, { rcases H c hc i hi x hdx with ⟨y, hy⟩, exact ⟨y, by simp [hy, hdx]⟩ }, { have : ((K + δ : ℝ≥0) : ℝ) * ∥d x∥ = K * ∥d x∥ + δ * ∥d x∥, apply_mod_cast add_mul, simp_rw this, apply hC c hc _ _ x (δ*∥d x∥) (mul_pos (by exact_mod_cast hδ) $ norm_pos_iff.mpr hdx), linarith }, end end is_weak_bounded_exact section quotient open normed_group_hom variables {M M'} /-- The quotient of a system of complexes. -/ def is_quotient (f : M ⟶ M') : Prop := ∀ c i, normed_group_hom.is_quotient (f.apply : M c i ⟶ M' c i) -- The next three lemmas restate lemmas about normed_group_hom.is_quotient in terms of the coercion -- of `M ⟶ M'` to functions. lemma is_quotient.surjective {f : M ⟶ M'} (h : is_quotient f) {c i} (m' : M' c i) : ∃ m : M c i, f m = m' := (h c i).surjective m' lemma is_quotient.norm_lift {f : M ⟶ M'} (h : is_quotient f) {ε : ℝ} (hε : 0 < ε) {c i} (n : M' c i) : ∃ (m : M c i), f m = n ∧ ∥m∥ < ∥n∥ + ε := quotient_norm_lift (h c i) hε n lemma is_quotient.norm_le {f : M ⟶ M'} (h : is_quotient f) {c i} (m : M c i) : ∥f m∥ ≤ ∥m∥ := quotient_norm_le (h c i) _ /-- The quotient of an admissible system of complexes is admissible. -/ lemma admissible_of_quotient {f : M ⟶ M'} (hquot : is_quotient f) (hadm : M.admissible) : M'.admissible := begin split, { intros c i m', refine le_of_forall_pos_le_add _, intros ε hε, obtain ⟨m, hm : f m = m' ∧ ∥m∥ < ∥m'∥ + ε⟩ := hquot.norm_lift hε m', rw [← hm.1, d_apply], calc ∥f (d m)∥ ≤ ∥d m∥ : hquot.norm_le _ ... ≤ ∥m∥ : hadm.d_norm_noninc _ _ m ... ≤ ∥m'∥ + ε : le_of_lt hm.2 ... = ∥f m∥ + ε : by rw [hm.1] }, { intros c' c i hc m', letI h := hc, refine le_of_forall_pos_le_add _, intros ε hε, obtain ⟨m, hm⟩ := hquot.norm_lift hε m', rw [← hm.1, res_apply], calc ∥f (res m)∥ ≤ ∥res m∥ : hquot.norm_le _ ... ≤ ∥m∥ : hadm.res_norm_noninc c' c _ hc m ... ≤ ∥m'∥ + ε : le_of_lt hm.2 ... = ∥f m∥ + ε : by rw [hm.1] } end end quotient end system_of_complexes -- #lint- only unused_arguments def_lemma doc_blame
7c9060507264491d2f3e1bc00539df8b717573d9
78269ad0b3c342b20786f60690708b6e328132b0
/src/library_dev/data/num/basic.lean
d85e199789547bbfcd8e4773bd5ceff81d566705
[]
no_license
dselsam/library_dev
e74f46010fee9c7b66eaa704654cad0fcd2eefca
1b4e34e7fb067ea5211714d6d3ecef5132fc8218
refs/heads/master
1,610,372,841,675
1,497,014,421,000
1,497,014,421,000
86,526,137
0
0
null
1,490,752,133,000
1,490,752,132,000
null
UTF-8
Lean
false
false
11,274
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro Binary representation of integers using inductive types. Note: Unlike in Coq, where this representation is preferred because of the reliance on kernel reduction, in Lean this representation is discouraged in favor of the "Peano" natural numbers `nat`, and the purpose of this collection of theorems is to show the equivalence of the different approaches. -/ import data.pnat ...data.bool data.vector data.bitvec universe u inductive pos_num : Type | one : pos_num | bit1 : pos_num → pos_num | bit0 : pos_num → pos_num instance : has_one pos_num := ⟨pos_num.one⟩ instance : decidable_eq pos_num := by tactic.mk_dec_eq_instance inductive num : Type | zero : num | pos : pos_num → num instance : has_zero num := ⟨num.zero⟩ instance : has_one num := ⟨num.pos 1⟩ instance : has_coe pos_num num := ⟨num.pos⟩ instance : decidable_eq num := by tactic.mk_dec_eq_instance -- Representation of integers using trichotomy around zero inductive znum : Type | zero : znum | pos : pos_num → znum | neg : pos_num → znum instance : has_zero znum := ⟨znum.zero⟩ instance : has_one znum := ⟨znum.pos 1⟩ instance : has_coe pos_num znum := ⟨znum.pos⟩ instance : decidable_eq znum := by tactic.mk_dec_eq_instance -- Alternative representation of integers using a sign bit at the end inductive nzsnum : Type | one : bool → nzsnum | bit : bool → nzsnum → nzsnum inductive snum : Type | zero : bool → snum | nz : nzsnum → snum instance : has_coe nzsnum snum := ⟨snum.nz⟩ instance : has_zero snum := ⟨snum.zero ff⟩ instance : has_one nzsnum := ⟨nzsnum.one tt⟩ instance : has_one snum := ⟨snum.nz 1⟩ instance : decidable_eq nzsnum := by tactic.mk_dec_eq_instance instance : decidable_eq snum := by tactic.mk_dec_eq_instance namespace pos_num def succ : pos_num → pos_num | 1 := bit0 one | (bit1 n) := bit0 (succ n) | (bit0 n) := bit1 n def is_one : pos_num → bool | 1 := tt | _ := ff protected def add : pos_num → pos_num → pos_num | 1 b := succ b | a 1 := succ a | (bit0 a) (bit0 b) := bit0 (add a b) | (bit1 a) (bit1 b) := bit0 (succ (add a b)) | (bit0 a) (bit1 b) := bit1 (add a b) | (bit1 a) (bit0 b) := bit1 (add a b) instance : has_add pos_num := ⟨pos_num.add⟩ def pred' : pos_num → option pos_num | 1 := none | (bit0 n) := some (option.cases_on (pred' n) 1 bit1) | (bit1 n) := bit0 n def pred (a : pos_num) : pos_num := (pred' a).get_or_else 1 def size : pos_num → pos_num | 1 := 1 | (bit0 n) := succ (size n) | (bit1 n) := succ (size n) protected def mul (a : pos_num) : pos_num → pos_num | 1 := a | (bit0 b) := bit0 (mul b) | (bit1 b) := bit0 (mul b) + a instance : has_mul pos_num := ⟨pos_num.mul⟩ def of_nat_succ : ℕ → pos_num | 0 := 1 | (nat.succ n) := succ (of_nat_succ n) def of_nat (n : ℕ) : pos_num := of_nat_succ (nat.pred n) open ordering def cmp : pos_num → pos_num → ordering | 1 1 := eq | _ 1 := gt | 1 _ := lt | (bit0 a) (bit0 b) := cmp a b | (bit0 a) (bit1 b) := ordering.cases_on (cmp a b) lt lt gt | (bit1 a) (bit0 b) := ordering.cases_on (cmp a b) lt gt gt | (bit1 a) (bit1 b) := cmp a b instance : has_ordering pos_num := ⟨cmp⟩ def psub : pos_num → pos_num → option pos_num | 1 b := none | a 1 := pred' a | (bit0 a) (bit0 b) := bit0 <$> psub a b | (bit0 a) (bit1 b) := bit1 <$> psub a b | (bit1 a) (bit0 b) := bit1 <$> psub a b | (bit1 a) (bit1 b) := bit0 <$> psub a b protected def sub (a b : pos_num) : pos_num := (psub a b).get_or_else 1 instance : has_sub pos_num := ⟨pos_num.sub⟩ end pos_num section variables {α : Type u} [has_zero α] [has_one α] [has_add α] def cast_pos_num : pos_num → α | 1 := 1 | (pos_num.bit0 a) := bit0 (cast_pos_num a) | (pos_num.bit1 a) := bit1 (cast_pos_num a) def cast_num : num → α | 0 := 0 | (num.pos p) := cast_pos_num p instance pos_num_coe : has_coe pos_num α := ⟨cast_pos_num⟩ instance num_nat_coe : has_coe num α := ⟨cast_num⟩ end namespace nat def of_pos_num : pos_num → nat := cast_pos_num def of_num : num → nat := cast_num end nat instance : has_lt pos_num := ⟨λa b, (a : ℕ) < b⟩ instance : has_le pos_num := ⟨λa b, (a : ℕ) ≤ b⟩ instance : has_lt num := ⟨λa b, (a : ℕ) < b⟩ instance : has_le num := ⟨λa b, (a : ℕ) ≤ b⟩ namespace num open pos_num def succ' : num → pos_num | 0 := 1 | (pos p) := succ p def succ (n : num) : num := pos (succ' n) def of_nat : nat → num | 0 := 0 | (nat.succ n) := succ (of_nat n) instance nat_num_coe : has_coe nat num := ⟨of_nat⟩ protected def add : num → num → num | 0 a := a | b 0 := b | (pos a) (pos b) := pos (a + b) instance : has_add num := ⟨num.add⟩ def pred : num → num | 0 := 0 | (pos p) := option.cases_on (pred' p) 0 pos protected def bit0 : num → num | 0 := 0 | (pos n) := pos (pos_num.bit0 n) protected def bit1 : num → num | 0 := 1 | (pos n) := pos (pos_num.bit1 n) def size : num → num | 0 := 0 | (pos n) := pos (pos_num.size n) protected def mul : num → num → num | 0 _ := 0 | _ 0 := 0 | (pos a) (pos b) := pos (a * b) instance : has_mul num := ⟨num.mul⟩ open ordering def cmp : num → num → ordering | 0 0 := eq | _ 0 := gt | 0 _ := lt | (pos a) (pos b) := pos_num.cmp a b instance : has_ordering num := ⟨cmp⟩ protected def sub : num → num → num | a 0 := a | 0 b := b | (pos a) (pos b) := option.cases_on (psub a b) 0 pos instance : has_sub num := ⟨num.sub⟩ def to_znum : num → znum | 0 := 0 | (pos a) := znum.pos a instance coe_znum : has_coe num znum := ⟨to_znum⟩ end num namespace znum open pos_num def succ : znum → znum | 0 := 1 | (pos a) := pos (pos_num.succ a) | (neg a) := option.cases_on (pos_num.pred' a) 0 neg def pred : znum → znum | 0 := neg 1 | (pos a) := option.cases_on (pos_num.pred' a) 0 pos | (neg a) := neg (pos_num.succ a) def zneg : znum → znum | 0 := 0 | (pos a) := neg a | (neg a) := pos a instance : has_neg znum := ⟨zneg⟩ protected def add : znum → znum → znum | 0 a := a | b 0 := b | (pos a) (pos b) := pos (a + b) | (pos a) (neg b) := option.cases_on (psub a b) (option.cases_on (psub b a) 0 neg) pos | (neg a) (pos b) := option.cases_on (psub a b) (option.cases_on (psub b a) 0 pos) neg | (neg a) (neg b) := neg (a + b) instance : has_add znum := ⟨znum.add⟩ protected def sub (a b : znum) : znum := a + zneg b instance : has_sub znum := ⟨znum.sub⟩ protected def mul : znum → znum → znum | 0 a := 0 | b 0 := 0 | (pos a) (pos b) := pos (a * b) | (pos a) (neg b) := neg (a * b) | (neg a) (pos b) := neg (a * b) | (neg a) (neg b) := pos (a * b) instance : has_mul znum := ⟨znum.mul⟩ end znum namespace int def of_znum : znum → ℤ | 0 := 0 | (znum.pos a) := a | (znum.neg a) := -[1+ option.cases_on (pos_num.pred' a) 0 nat.of_pos_num] instance znum_coe : has_coe znum ℤ := ⟨of_znum⟩ end int instance : has_lt znum := ⟨λa b, (a : ℤ) < b⟩ instance : has_le znum := ⟨λa b, (a : ℤ) ≤ b⟩ /- The snum representation uses a bit string, essentially a list of 0 (ff) and 1 (tt) bits, and the negation of the MSB is sign-extended to all higher bits. -/ namespace nzsnum notation a :: b := bit a b def sign : nzsnum → bool | (one b) := bnot b | (b :: p) := sign p @[pattern] def not : nzsnum → nzsnum | (one b) := one (bnot b) | (b :: p) := bnot b :: not p prefix ~ := not def bit0 : nzsnum → nzsnum := bit ff def bit1 : nzsnum → nzsnum := bit tt def head : nzsnum → bool | (one b) := b | (b :: p) := b def tail : nzsnum → snum | (one b) := snum.zero (bnot b) | (b :: p) := p end nzsnum namespace snum open nzsnum def sign : snum → bool | (zero z) := z | (nz p) := p.sign @[pattern] def not : snum → snum | (zero z) := zero (bnot z) | (nz p) := ~p prefix ~ := not @[pattern] def bit : bool → snum → snum | b (zero z) := if b = z then zero b else one b | b (nz p) := p.bit b notation a :: b := bit a b def bit0 : snum → snum := bit ff def bit1 : snum → snum := bit tt theorem bit_zero (b) : b :: zero b = zero b := by cases b; refl theorem bit_one (b) : b :: zero (bnot b) = one b := by cases b; refl end snum namespace nzsnum open snum def drec' {C : snum → Sort u} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p : nzsnum, C p | (one b) := by rw -bit_one; exact s b (snum.zero (bnot b)) (z (bnot b)) | (bit b p) := s b p (drec' p) end nzsnum namespace snum open nzsnum def head : snum → bool | (zero z) := z | (nz p) := p.head def tail : snum → snum | (zero z) := zero z | (nz p) := p.tail def drec' {C : snum → Sort u} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p, C p | (zero b) := z b | (nz p) := p.drec' z s def rec' {α} (z : bool → α) (s : bool → snum → α → α) : snum → α := drec' z s def bits : snum → Π n, vector bool n | p 0 := [] | p (n+1) := head p :: bits (tail p) n def test_bit : nat → snum → bool | 0 p := head p | (n+1) p := test_bit n (tail p) def succ : snum → snum := rec' (λ b, cond b 0 1) (λb p succp, cond b (ff :: succp) (tt :: p)) def pred : snum → snum := rec' (λ b, cond b (~1) ~0) (λb p predp, cond b (ff :: p) (tt :: predp)) protected def neg (n : snum) : snum := succ ~n instance : has_neg snum := ⟨snum.neg⟩ -- First bit is 0 or 1 (tt), second bit is 0 or -1 (tt) def czadd : bool → bool → snum → snum | ff ff p := p | ff tt p := pred p | tt ff p := succ p | tt tt p := p def cadd : snum → snum → bool → snum := rec' (λ a p c, czadd c a p) $ λa p IH, rec' (λb c, czadd c b (a :: p)) $ λb q _ c, bitvec.xor3 a b c :: IH q (bitvec.carry a b c) protected def add (a b : snum) : snum := cadd a b ff instance : has_add snum := ⟨snum.add⟩ protected def sub (a b : snum) : snum := a + -b instance : has_sub snum := ⟨snum.sub⟩ protected def mul (a : snum) : snum → snum := rec' (λ b, cond b (-a) 0) $ λb q IH, cond b (bit0 IH + a) (bit0 IH) instance : has_mul snum := ⟨snum.mul⟩ end snum namespace int def of_snum : snum → ℤ := snum.rec' (λ a, cond a (-1) 0) (λa p IH, cond a (bit1 IH) (bit0 IH)) instance snum_coe : has_coe snum ℤ := ⟨of_snum⟩ end int instance : has_lt snum := ⟨λa b, (a : ℤ) < b⟩ instance : has_le snum := ⟨λa b, (a : ℤ) ≤ b⟩
30d0ca06978cea9df499a7bbae83cac743f2082a
26ac254ecb57ffcb886ff709cf018390161a9225
/src/tactic/squeeze.lean
6f43d0c9e0faf676461bdbecdbbbede21411ee28
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
13,240
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import control.traversable.basic import tactic.simpa open interactive interactive.types lean.parser private meta def loc.to_string_aux : option name → string | none := "⊢" | (some x) := to_string x /-- pretty print a `loc` -/ meta def loc.to_string : loc → string | (loc.ns []) := "" | (loc.ns [none]) := "" | (loc.ns ls) := string.join $ list.intersperse " " (" at" :: ls.map loc.to_string_aux) | loc.wildcard := " at *" /-- shift `pos` `n` columns to the left -/ meta def pos.move_left (p : pos) (n : ℕ) : pos := { line := p.line, column := p.column - n } namespace tactic open list /-- parse structure instance of the shape `{ field1 := value1, .. , field2 := value2 }` -/ meta def struct_inst : lean.parser pexpr := do tk "{", ls ← sep_by (skip_info (tk ",")) ( sum.inl <$> (tk ".." *> texpr) <|> sum.inr <$> (prod.mk <$> ident <* tk ":=" <*> texpr)), tk "}", let (srcs,fields) := partition_map id ls, let (names,values) := unzip fields, pure $ pexpr.mk_structure_instance { field_names := names, field_values := values, sources := srcs } /-- pretty print structure instance -/ meta def struct.to_tactic_format (e : pexpr) : tactic format := do r ← e.get_structure_instance_info, fs ← mzip_with (λ n v, do v ← to_expr v >>= pp, pure $ format!"{n} := {v}" ) r.field_names r.field_values, let ss := r.sources.map (λ s, format!" .. {s}"), let x : format := format.join $ list.intersperse ", " (fs ++ ss), pure format!" {{{x}}" /-- Attribute containing a table that accumulates multiple `squeeze_simp` suggestions -/ @[user_attribute] private meta def squeeze_loc_attr : user_attribute unit (option (list (pos × string × list simp_arg_type × string))) := { name := `_squeeze_loc, parser := fail "this attribute should not be used", descr := "table to accumulate multiple `squeeze_simp` suggestions" } /-- dummy declaration used as target of `squeeze_loc` attribute -/ def squeeze_loc_attr_carrier := () run_cmd squeeze_loc_attr.set ``squeeze_loc_attr_carrier none tt /-- Emit a suggestion to the user. If inside a `squeeze_scope` block, the suggestions emitted through `mk_suggestion` will be aggregated so that every tactic that makes a suggestion can consider multiple execution of the same invocation. If `at_pos` is true, make the suggestion at `p` instead of the current position. -/ meta def mk_suggestion (p : pos) (pre post : string) (args : list simp_arg_type) (at_pos := ff) : tactic unit := do xs ← squeeze_loc_attr.get_param ``squeeze_loc_attr_carrier, match xs with | none := do args ← to_line_wrap_format <$> args.mmap pp, if at_pos then @scope_trace _ p.line p.column $ λ _, _root_.trace sformat!"{pre}{args}{post}" (pure () : tactic unit) else trace sformat!"{pre}{args}{post}" | some xs := do squeeze_loc_attr.set ``squeeze_loc_attr_carrier ((p,pre,args,post) :: xs) ff end local postfix `?`:9001 := optional /-- translate a `pexpr` into a `simp` configuration -/ meta def parse_config : option pexpr → tactic (simp_config_ext × format) | none := pure ({}, "") | (some cfg) := do e ← to_expr ``(%%cfg : simp_config_ext), fmt ← has_to_tactic_format.to_tactic_format cfg, prod.mk <$> eval_expr simp_config_ext e <*> struct.to_tactic_format cfg /-- translate a `pexpr` into a `dsimp` configuration -/ meta def parse_dsimp_config : option pexpr → tactic (dsimp_config × format) | none := pure ({}, "") | (some cfg) := do e ← to_expr ``(%%cfg : simp_config_ext), fmt ← has_to_tactic_format.to_tactic_format cfg, prod.mk <$> eval_expr dsimp_config e <*> struct.to_tactic_format cfg /-- `same_result proof tac` runs tactic `tac` and checks if the proof produced by `tac` is equivalent to `proof`. -/ meta def same_result (pr : proof_state) (tac : tactic unit) : tactic bool := do s ← get_proof_state_after tac, pure $ some pr = s private meta def filter_simp_set_aux (tac : bool → list simp_arg_type → tactic unit) (args : list simp_arg_type) (pr : proof_state) : list simp_arg_type → list simp_arg_type → list simp_arg_type → tactic (list simp_arg_type × list simp_arg_type) | [] ys ds := pure (ys.reverse, ds.reverse) | (x :: xs) ys ds := do b ← same_result pr (tac tt (args ++ xs ++ ys)), if b then filter_simp_set_aux xs ys (x:: ds) else filter_simp_set_aux xs (x :: ys) ds declare_trace squeeze.deleted /-- `filter_simp_set g call_simp user_args simp_args` returns `args'` such that, when calling `call_simp tt /- only -/ args'` on the goal `g` (`g` is a meta var) we end up in the same state as if we had called `call_simp ff (user_args ++ simp_args)` and removing any one element of `args'` changes the resulting proof. -/ meta def filter_simp_set (tac : bool → list simp_arg_type → tactic unit) (user_args simp_args : list simp_arg_type) : tactic (list simp_arg_type) := do some s ← get_proof_state_after (tac ff (user_args ++ simp_args)), (simp_args', _) ← filter_simp_set_aux tac user_args s simp_args [] [], (user_args', ds) ← filter_simp_set_aux tac simp_args' s user_args [] [], when (is_trace_enabled_for `squeeze.deleted = tt ∧ ¬ ds.empty) trace!"deleting provided arguments {ds}", pure (user_args' ++ simp_args') /-- make a `simp_arg_type` that references the name given as an argument -/ meta def name.to_simp_args (n : name) : tactic simp_arg_type := do e ← resolve_name' n, pure $ simp_arg_type.expr e /-- tactic combinator to create a `simp`-like tactic that minimizes its argument list. * `no_dflt`: did the user use the `only` keyword? * `args`: list of `simp` arguments * `tac`: how to invoke the underlying `simp` tactic -/ meta def squeeze_simp_core (no_dflt : bool) (args : list simp_arg_type) (tac : Π (no_dflt : bool) (args : list simp_arg_type), tactic unit) (mk_suggestion : list simp_arg_type → tactic unit) : tactic unit := do v ← target >>= mk_meta_var, g ← retrieve $ do { g ← main_goal, tac no_dflt args, instantiate_mvars g }, let vs := g.list_constant, vs ← vs.mfilter is_simp_lemma, vs ← vs.mmap strip_prefix, vs ← vs.to_list.mmap name.to_simp_args, with_local_goals' [v] (filter_simp_set tac args vs) >>= mk_suggestion, tac no_dflt args namespace interactive attribute [derive decidable_eq] simp_arg_type /-- combinator meant to aggregate the suggestions issued by multiple calls of `squeeze_simp` (due, for instance, to `;`). Can be used as: ```lean example {α β} (xs ys : list α) (f : α → β) : (xs ++ ys.tail).map f = xs.map f ∧ (xs.tail.map f).length = xs.length := begin have : xs = ys, admit, squeeze_scope { split; squeeze_simp, -- `squeeze_simp` is run twice, the first one requires -- `list.map_append` and the second one `[list.length_map, list.length_tail]` -- prints only one message and combine the suggestions: -- > Try this: simp only [list.length_map, list.length_tail, list.map_append] squeeze_simp [this] -- `squeeze_simp` is run only once -- prints: -- > Try this: simp only [this] }, end ``` -/ meta def squeeze_scope (tac : itactic) : tactic unit := do none ← squeeze_loc_attr.get_param ``squeeze_loc_attr_carrier | pure (), squeeze_loc_attr.set ``squeeze_loc_attr_carrier (some []) ff, finally tac $ do some xs ← squeeze_loc_attr.get_param ``squeeze_loc_attr_carrier | fail "invalid state", let m := native.rb_lmap.of_list xs, squeeze_loc_attr.set ``squeeze_loc_attr_carrier none ff, m.to_list.reverse.mmap' $ λ ⟨p,suggs⟩, do { let ⟨pre,_,post⟩ := suggs.head, let suggs : list (list simp_arg_type) := suggs.map $ prod.fst ∘ prod.snd, mk_suggestion p pre post (suggs.foldl list.union []) tt, pure () } /-- `squeeze_simp`, `squeeze_simpa` and `squeeze_dsimp` perform the same task with the difference that `squeeze_simp` relates to `simp` while `squeeze_simpa` relates to `simpa` and `squeeze_dsimp` relates to `dsimp`. The following applies to `squeeze_simp`, `squeeze_simpa` and `squeeze_dsimp`. `squeeze_simp` behaves like `simp` (including all its arguments) and prints a `simp only` invocation to skip the search through the `simp` lemma list. For instance, the following is easily solved with `simp`: ```lean example : 0 + 1 = 1 + 0 := by simp ``` To guide the proof search and speed it up, we may replace `simp` with `squeeze_simp`: ```lean example : 0 + 1 = 1 + 0 := by squeeze_simp -- prints: -- Try this: simp only [add_zero, eq_self_iff_true, zero_add] ``` `squeeze_simp` suggests a replacement which we can use instead of `squeeze_simp`. ```lean example : 0 + 1 = 1 + 0 := by simp only [add_zero, eq_self_iff_true, zero_add] ``` `squeeze_simp only` prints nothing as it already skips the `simp` list. This tactic is useful for speeding up the compilation of a complete file. Steps: 1. search and replace ` simp` with ` squeeze_simp` (the space helps avoid the replacement of `simp` in `@[simp]`) throughout the file. 2. Starting at the beginning of the file, go to each printout in turn, copy the suggestion in place of `squeeze_simp`. 3. after all the suggestions were applied, search and replace `squeeze_simp` with `simp` to remove the occurrences of `squeeze_simp` that did not produce a suggestion. Known limitation(s): * in cases where `squeeze_simp` is used after a `;` (e.g. `cases x; squeeze_simp`), `squeeze_simp` will produce as many suggestions as the number of goals it is applied to. It is likely that none of the suggestion is a good replacement but they can all be combined by concatenating their list of lemmas. `squeeze_scope` can be used to combine the suggestions: `by squeeze_scope { cases x; squeeze_simp }` -/ meta def squeeze_simp (key : parse cur_pos) (use_iota_eqn : parse (tk "!")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (locat : parse location) (cfg : parse struct_inst?) : tactic unit := do (cfg',c) ← parse_config cfg, squeeze_simp_core no_dflt hs (λ l_no_dft l_args, simp use_iota_eqn l_no_dft l_args attr_names locat cfg') (λ args, let use_iota_eqn := if use_iota_eqn.is_some then "!" else "", attrs := if attr_names.empty then "" else string.join (list.intersperse " " (" with" :: attr_names.map to_string)), loc := loc.to_string locat in mk_suggestion (key.move_left 1) sformat!"Try this: simp{use_iota_eqn} only " sformat!"{attrs}{loc}{c}" args) /-- see `squeeze_simp` -/ meta def squeeze_simpa (key : parse cur_pos) (use_iota_eqn : parse (tk "!")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (tgt : parse (tk "using" *> texpr)?) (cfg : parse struct_inst?) : tactic unit := do (cfg',c) ← parse_config cfg, tgt' ← traverse (λ t, do t ← to_expr t >>= pp, pure format!" using {t}") tgt, squeeze_simp_core no_dflt hs (λ l_no_dft l_args, simpa use_iota_eqn l_no_dft l_args attr_names tgt cfg') (λ args, let use_iota_eqn := if use_iota_eqn.is_some then "!" else "", attrs := if attr_names.empty then "" else string.join (list.intersperse " " (" with" :: attr_names.map to_string)), tgt' := tgt'.get_or_else "" in mk_suggestion (key.move_left 1) sformat!"Try this: simpa{use_iota_eqn} only " sformat!"{attrs}{tgt'}{c}" args) /-- `squeeze_dsimp` behaves like `dsimp` (including all its arguments) and prints a `dsimp only` invocation to skip the search through the `simp` lemma list. See the doc string of `squeeze_simp` for examples. -/ meta def squeeze_dsimp (key : parse cur_pos) (use_iota_eqn : parse (tk "!")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (locat : parse location) (cfg : parse struct_inst?) : tactic unit := do (cfg',c) ← parse_dsimp_config cfg, squeeze_simp_core no_dflt hs (λ l_no_dft l_args, dsimp l_no_dft l_args attr_names locat cfg') (λ args, let use_iota_eqn := if use_iota_eqn.is_some then "!" else "", attrs := if attr_names.empty then "" else string.join (list.intersperse " " (" with" :: attr_names.map to_string)), loc := loc.to_string locat in mk_suggestion (key.move_left 1) sformat!"Try this: dsimp{use_iota_eqn} only " sformat!"{attrs}{loc}{c}" args) end interactive end tactic open tactic.interactive add_tactic_doc { name := "squeeze_simp / squeeze_simpa / squeeze_dsimp / squeeze_scope", category := doc_category.tactic, decl_names := [``squeeze_simp, ``squeeze_dsimp, ``squeeze_simpa, ``squeeze_scope], tags := ["simplification", "Try this"], inherit_description_from := ``squeeze_simp }
f9b59a96c9e4664e02ca297a28376b1e6333820c
8eeb99d0fdf8125f5d39a0ce8631653f588ee817
/src/algebra/ordered_ring.lean
6e7dc0ccb238b31b4ab517a972b87883456424a7
[ "Apache-2.0" ]
permissive
jesse-michael-han/mathlib
a15c58378846011b003669354cbab7062b893cfe
fa6312e4dc971985e6b7708d99a5bc3062485c89
refs/heads/master
1,625,200,760,912
1,602,081,753,000
1,602,081,753,000
181,787,230
0
0
null
1,555,460,682,000
1,555,460,682,000
null
UTF-8
Lean
false
false
43,044
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import algebra.ordered_group set_option old_structure_cmd true universe u variable {α : Type u} /-- An `ordered_semiring α` is a semiring `α` with a partial order such that multiplication with a positive number and addition are monotone. -/ @[protect_proj] class ordered_semiring (α : Type u) extends semiring α, ordered_cancel_add_comm_monoid α := (zero_lt_one : 0 < (1 : α)) (mul_lt_mul_of_pos_left : ∀ a b c : α, a < b → 0 < c → c * a < c * b) (mul_lt_mul_of_pos_right : ∀ a b c : α, a < b → 0 < c → a * c < b * c) section ordered_semiring variables [ordered_semiring α] {a b c d : α} lemma zero_lt_one : 0 < (1:α) := ordered_semiring.zero_lt_one lemma zero_le_one : 0 ≤ (1:α) := zero_lt_one.le lemma zero_lt_two : 0 < (2:α) := add_pos zero_lt_one zero_lt_one @[field_simps] lemma two_ne_zero : (2:α) ≠ 0 := ne.symm (ne_of_lt zero_lt_two) lemma one_lt_two : 1 < (2:α) := calc (2:α) = 1+1 : one_add_one_eq_two ... > 1+0 : add_lt_add_left zero_lt_one _ ... = 1 : add_zero 1 lemma one_le_two : 1 ≤ (2:α) := one_lt_two.le lemma zero_lt_four : 0 < (4:α) := add_pos zero_lt_two zero_lt_two lemma mul_lt_mul_of_pos_left (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b := ordered_semiring.mul_lt_mul_of_pos_left a b c h₁ h₂ lemma mul_lt_mul_of_pos_right (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c := ordered_semiring.mul_lt_mul_of_pos_right a b c h₁ h₂ lemma mul_le_mul_of_nonneg_left (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b := begin cases classical.em (b ≤ a), { simp [h.antisymm h₁] }, cases classical.em (c ≤ 0), { simp [h_1.antisymm h₂] }, exact (mul_lt_mul_of_pos_left (h₁.lt_of_not_le h) (h₂.lt_of_not_le h_1)).le, end lemma mul_le_mul_of_nonneg_right (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c := begin cases classical.em (b ≤ a), { simp [h.antisymm h₁] }, cases classical.em (c ≤ 0), { simp [h_1.antisymm h₂] }, exact (mul_lt_mul_of_pos_right (h₁.lt_of_not_le h) (h₂.lt_of_not_le h_1)).le, end -- TODO: there are four variations, depending on which variables we assume to be nonneg lemma mul_le_mul (hac : a ≤ c) (hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) : a * b ≤ c * d := calc a * b ≤ c * b : mul_le_mul_of_nonneg_right hac nn_b ... ≤ c * d : mul_le_mul_of_nonneg_left hbd nn_c lemma mul_nonneg (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := have h : 0 * b ≤ a * b, from mul_le_mul_of_nonneg_right ha hb, by rwa [zero_mul] at h lemma mul_nonpos_of_nonneg_of_nonpos (ha : 0 ≤ a) (hb : b ≤ 0) : a * b ≤ 0 := have h : a * b ≤ a * 0, from mul_le_mul_of_nonneg_left hb ha, by rwa mul_zero at h lemma mul_nonpos_of_nonpos_of_nonneg (ha : a ≤ 0) (hb : 0 ≤ b) : a * b ≤ 0 := have h : a * b ≤ 0 * b, from mul_le_mul_of_nonneg_right ha hb, by rwa zero_mul at h lemma mul_lt_mul (hac : a < c) (hbd : b ≤ d) (pos_b : 0 < b) (nn_c : 0 ≤ c) : a * b < c * d := calc a * b < c * b : mul_lt_mul_of_pos_right hac pos_b ... ≤ c * d : mul_le_mul_of_nonneg_left hbd nn_c lemma mul_lt_mul' (h1 : a ≤ c) (h2 : b < d) (h3 : 0 ≤ b) (h4 : 0 < c) : a * b < c * d := calc a * b ≤ c * b : mul_le_mul_of_nonneg_right h1 h3 ... < c * d : mul_lt_mul_of_pos_left h2 h4 lemma mul_pos (ha : 0 < a) (hb : 0 < b) : 0 < a * b := have h : 0 * b < a * b, from mul_lt_mul_of_pos_right ha hb, by rwa zero_mul at h lemma mul_neg_of_pos_of_neg (ha : 0 < a) (hb : b < 0) : a * b < 0 := have h : a * b < a * 0, from mul_lt_mul_of_pos_left hb ha, by rwa mul_zero at h lemma mul_neg_of_neg_of_pos (ha : a < 0) (hb : 0 < b) : a * b < 0 := have h : a * b < 0 * b, from mul_lt_mul_of_pos_right ha hb, by rwa zero_mul at h lemma mul_self_le_mul_self (h1 : 0 ≤ a) (h2 : a ≤ b) : a * a ≤ b * b := mul_le_mul h2 h2 h1 $ h1.trans h2 lemma mul_self_lt_mul_self (h1 : 0 ≤ a) (h2 : a < b) : a * a < b * b := mul_lt_mul' h2.le h2 h1 $ h1.trans_lt h2 lemma mul_lt_mul'' (h1 : a < c) (h2 : b < d) (h3 : 0 ≤ a) (h4 : 0 ≤ b) : a * b < c * d := (lt_or_eq_of_le h4).elim (λ b0, mul_lt_mul h1 h2.le b0 $ h3.trans h1.le) (λ b0, by rw [← b0, mul_zero]; exact mul_pos (h3.trans_lt h1) (h4.trans_lt h2)) lemma le_mul_of_one_le_right (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ b * a := suffices b * 1 ≤ b * a, by rwa mul_one at this, mul_le_mul_of_nonneg_left h hb lemma le_mul_of_one_le_left (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ a * b := suffices 1 * b ≤ a * b, by rwa one_mul at this, mul_le_mul_of_nonneg_right h hb lemma bit1_pos (h : 0 ≤ a) : 0 < bit1 a := lt_add_of_le_of_pos (add_nonneg h h) zero_lt_one lemma bit1_pos' (h : 0 < a) : 0 < bit1 a := bit1_pos h.le lemma lt_add_one (a : α) : a < a + 1 := lt_add_of_le_of_pos le_rfl zero_lt_one lemma lt_one_add (a : α) : a < 1 + a := by { rw [add_comm], apply lt_add_one } lemma one_lt_mul (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := (one_mul (1 : α)) ▸ mul_lt_mul' ha hb zero_le_one (zero_lt_one.trans_le ha) lemma mul_le_one (ha : a ≤ 1) (hb' : 0 ≤ b) (hb : b ≤ 1) : a * b ≤ 1 := begin rw ← one_mul (1 : α), apply mul_le_mul; {assumption <|> apply zero_le_one} end lemma one_lt_mul_of_le_of_lt (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := calc 1 = 1 * 1 : by rw one_mul ... < a * b : mul_lt_mul' ha hb zero_le_one (zero_lt_one.trans_le ha) lemma one_lt_mul_of_lt_of_le (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := calc 1 = 1 * 1 : by rw one_mul ... < a * b : mul_lt_mul ha hb zero_lt_one $ zero_le_one.trans ha.le lemma mul_le_of_le_one_right (ha : 0 ≤ a) (hb1 : b ≤ 1) : a * b ≤ a := calc a * b ≤ a * 1 : mul_le_mul_of_nonneg_left hb1 ha ... = a : mul_one a lemma mul_le_of_le_one_left (hb : 0 ≤ b) (ha1 : a ≤ 1) : a * b ≤ b := calc a * b ≤ 1 * b : mul_le_mul ha1 le_rfl hb zero_le_one ... = b : one_mul b lemma mul_lt_one_of_nonneg_of_lt_one_left (ha0 : 0 ≤ a) (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := calc a * b ≤ a : mul_le_of_le_one_right ha0 hb ... < 1 : ha lemma mul_lt_one_of_nonneg_of_lt_one_right (ha : a ≤ 1) (hb0 : 0 ≤ b) (hb : b < 1) : a * b < 1 := calc a * b ≤ b : mul_le_of_le_one_left hb0 ha ... < 1 : hb end ordered_semiring /-- A `linear_ordered_semiring α` is a semiring `α` with a linear order such that multiplication with a positive number and addition are monotone. -/ @[protect_proj] class linear_ordered_semiring (α : Type u) extends ordered_semiring α, linear_order α section linear_ordered_semiring variables [linear_ordered_semiring α] {a b c d : α} lemma lt_of_mul_lt_mul_left (h : c * a < c * b) (hc : 0 ≤ c) : a < b := lt_of_not_ge (assume h1 : b ≤ a, have h2 : c * b ≤ c * a, from mul_le_mul_of_nonneg_left h1 hc, h2.not_lt h) lemma lt_of_mul_lt_mul_right (h : a * c < b * c) (hc : 0 ≤ c) : a < b := lt_of_not_ge (assume h1 : b ≤ a, have h2 : b * c ≤ a * c, from mul_le_mul_of_nonneg_right h1 hc, h2.not_lt h) lemma le_of_mul_le_mul_left (h : c * a ≤ c * b) (hc : 0 < c) : a ≤ b := le_of_not_gt (assume h1 : b < a, have h2 : c * b < c * a, from mul_lt_mul_of_pos_left h1 hc, h2.not_le h) lemma le_of_mul_le_mul_right (h : a * c ≤ b * c) (hc : 0 < c) : a ≤ b := le_of_not_gt (assume h1 : b < a, have h2 : b * c < a * c, from mul_lt_mul_of_pos_right h1 hc, h2.not_le h) lemma pos_and_pos_or_neg_and_neg_of_mul_pos (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, _⟩, contrapose! hab, exact mul_nonpos_of_nonneg_of_nonpos ha.le hab }, { rw [zero_mul] at hab, exact hab.false.elim }, { refine or.inr ⟨ha, _⟩, contrapose! hab, exact mul_nonpos_of_nonpos_of_nonneg ha.le hab } end lemma nonneg_and_nonneg_or_nonpos_and_nonpos_of_mul_nnonneg (hab : 0 ≤ a * b) : (0 ≤ a ∧ 0 ≤ b) ∨ (a ≤ 0 ∧ b ≤ 0) := begin contrapose! hab, rcases lt_trichotomy 0 a with (ha|rfl|ha), exacts [mul_neg_of_pos_of_neg ha (hab.1 ha.le), ((hab.1 le_rfl).asymm (hab.2 le_rfl)).elim, mul_neg_of_neg_of_pos ha (hab.2 ha.le)] end lemma pos_of_mul_pos_left (h : 0 < a * b) (ha : 0 ≤ a) : 0 < b := ((pos_and_pos_or_neg_and_neg_of_mul_pos h).resolve_right $ λ h, h.1.not_le ha).2 lemma pos_of_mul_pos_right (h : 0 < a * b) (hb : 0 ≤ b) : 0 < a := ((pos_and_pos_or_neg_and_neg_of_mul_pos h).resolve_right $ λ h, h.2.not_le hb).1 lemma nonneg_of_mul_nonneg_left (h : 0 ≤ a * b) (h1 : 0 < a) : 0 ≤ b := le_of_not_gt (assume h2 : b < 0, (mul_neg_of_pos_of_neg h1 h2).not_le h) lemma nonneg_of_mul_nonneg_right (h : 0 ≤ a * b) (h1 : 0 < b) : 0 ≤ a := le_of_not_gt (assume h2 : a < 0, (mul_neg_of_neg_of_pos h2 h1).not_le h) lemma neg_of_mul_neg_left (h : a * b < 0) (h1 : 0 ≤ a) : b < 0 := lt_of_not_ge (assume h2 : b ≥ 0, (mul_nonneg h1 h2).not_lt h) lemma neg_of_mul_neg_right (h : a * b < 0) (h1 : 0 ≤ b) : a < 0 := lt_of_not_ge (assume h2 : a ≥ 0, (mul_nonneg h2 h1).not_lt h) lemma nonpos_of_mul_nonpos_left (h : a * b ≤ 0) (h1 : 0 < a) : b ≤ 0 := le_of_not_gt (assume h2 : b > 0, (mul_pos h1 h2).not_le h) lemma nonpos_of_mul_nonpos_right (h : a * b ≤ 0) (h1 : 0 < b) : a ≤ 0 := le_of_not_gt (assume h2 : a > 0, (mul_pos h2 h1).not_le h) @[simp] lemma mul_le_mul_left (h : 0 < c) : c * a ≤ c * b ↔ a ≤ b := ⟨λ h', le_of_mul_le_mul_left h' h, λ h', mul_le_mul_of_nonneg_left h' h.le⟩ @[simp] lemma mul_le_mul_right (h : 0 < c) : a * c ≤ b * c ↔ a ≤ b := ⟨λ h', le_of_mul_le_mul_right h' h, λ h', mul_le_mul_of_nonneg_right h' h.le⟩ @[simp] lemma mul_lt_mul_left (h : 0 < c) : c * a < c * b ↔ a < b := ⟨lt_imp_lt_of_le_imp_le $ λ h', mul_le_mul_of_nonneg_left h' h.le, λ h', mul_lt_mul_of_pos_left h' h⟩ @[simp] lemma mul_lt_mul_right (h : 0 < c) : a * c < b * c ↔ a < b := ⟨lt_imp_lt_of_le_imp_le $ λ h', mul_le_mul_of_nonneg_right h' h.le, λ h', mul_lt_mul_of_pos_right h' h⟩ @[simp] lemma zero_le_mul_left (h : 0 < c) : 0 ≤ c * b ↔ 0 ≤ b := by { convert mul_le_mul_left h, simp } @[simp] lemma zero_le_mul_right (h : 0 < c) : 0 ≤ b * c ↔ 0 ≤ b := by { convert mul_le_mul_right h, simp } @[simp] lemma zero_lt_mul_left (h : 0 < c) : 0 < c * b ↔ 0 < b := by { convert mul_lt_mul_left h, simp } @[simp] lemma zero_lt_mul_right (h : 0 < c) : 0 < b * c ↔ 0 < b := by { convert mul_lt_mul_right h, simp } @[simp] lemma bit0_le_bit0 : bit0 a ≤ bit0 b ↔ a ≤ b := by rw [bit0, bit0, ← two_mul, ← two_mul, mul_le_mul_left zero_lt_two] @[simp] lemma bit0_lt_bit0 : bit0 a < bit0 b ↔ a < b := by rw [bit0, bit0, ← two_mul, ← two_mul, mul_lt_mul_left zero_lt_two] @[simp] lemma bit1_le_bit1 : bit1 a ≤ bit1 b ↔ a ≤ b := (add_le_add_iff_right 1).trans bit0_le_bit0 @[simp] lemma bit1_lt_bit1 : bit1 a < bit1 b ↔ a < b := (add_lt_add_iff_right 1).trans bit0_lt_bit0 @[simp] lemma one_le_bit1 : (1 : α) ≤ bit1 a ↔ 0 ≤ a := by rw [bit1, le_add_iff_nonneg_left, bit0, ← two_mul, zero_le_mul_left zero_lt_two] @[simp] lemma one_lt_bit1 : (1 : α) < bit1 a ↔ 0 < a := by rw [bit1, lt_add_iff_pos_left, bit0, ← two_mul, zero_lt_mul_left zero_lt_two] @[simp] lemma zero_le_bit0 : (0 : α) ≤ bit0 a ↔ 0 ≤ a := by rw [bit0, ← two_mul, zero_le_mul_left zero_lt_two] @[simp] lemma zero_lt_bit0 : (0 : α) < bit0 a ↔ 0 < a := by rw [bit0, ← two_mul, zero_lt_mul_left zero_lt_two] lemma le_mul_iff_one_le_left (hb : 0 < b) : b ≤ a * b ↔ 1 ≤ a := suffices 1 * b ≤ a * b ↔ 1 ≤ a, by rwa one_mul at this, mul_le_mul_right hb lemma lt_mul_iff_one_lt_left (hb : 0 < b) : b < a * b ↔ 1 < a := suffices 1 * b < a * b ↔ 1 < a, by rwa one_mul at this, mul_lt_mul_right hb lemma le_mul_iff_one_le_right (hb : 0 < b) : b ≤ b * a ↔ 1 ≤ a := suffices b * 1 ≤ b * a ↔ 1 ≤ a, by rwa mul_one at this, mul_le_mul_left hb lemma lt_mul_iff_one_lt_right (hb : 0 < b) : b < b * a ↔ 1 < a := suffices b * 1 < b * a ↔ 1 < a, by rwa mul_one at this, mul_lt_mul_left hb lemma lt_mul_of_one_lt_right (hb : 0 < b) : 1 < a → b < b * a := (lt_mul_iff_one_lt_right hb).2 theorem mul_nonneg_iff_right_nonneg_of_pos (h : 0 < a) : 0 ≤ b * a ↔ 0 ≤ b := ⟨assume : 0 ≤ b * a, nonneg_of_mul_nonneg_right this h, assume : 0 ≤ b, mul_nonneg this h.le⟩ lemma mul_le_iff_le_one_left (hb : 0 < b) : a * b ≤ b ↔ a ≤ 1 := ⟨ λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_left hb).2 h.not_lt), λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_left hb).1 h.not_lt) ⟩ lemma mul_lt_iff_lt_one_left (hb : 0 < b) : a * b < b ↔ a < 1 := ⟨ λ h, lt_of_not_ge (mt (le_mul_iff_one_le_left hb).2 h.not_le), λ h, lt_of_not_ge (mt (le_mul_iff_one_le_left hb).1 h.not_le) ⟩ lemma mul_le_iff_le_one_right (hb : 0 < b) : b * a ≤ b ↔ a ≤ 1 := ⟨ λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_right hb).2 h.not_lt), λ h, le_of_not_lt (mt (lt_mul_iff_one_lt_right hb).1 h.not_lt) ⟩ lemma mul_lt_iff_lt_one_right (hb : 0 < b) : b * a < b ↔ a < 1 := ⟨ λ h, lt_of_not_ge (mt (le_mul_iff_one_le_right hb).2 h.not_le), λ h, lt_of_not_ge (mt (le_mul_iff_one_le_right hb).1 h.not_le) ⟩ lemma nonpos_of_mul_nonneg_left (h : 0 ≤ a * b) (hb : b < 0) : a ≤ 0 := le_of_not_gt (λ ha, absurd h (mul_neg_of_pos_of_neg ha hb).not_le) lemma nonpos_of_mul_nonneg_right (h : 0 ≤ a * b) (ha : a < 0) : b ≤ 0 := le_of_not_gt (λ hb, absurd h (mul_neg_of_neg_of_pos ha hb).not_le) lemma neg_of_mul_pos_left (h : 0 < a * b) (hb : b ≤ 0) : a < 0 := lt_of_not_ge (λ ha, absurd h (mul_nonpos_of_nonneg_of_nonpos ha hb).not_lt) lemma neg_of_mul_pos_right (h : 0 < a * b) (ha : a ≤ 0) : b < 0 := lt_of_not_ge (λ hb, absurd h (mul_nonpos_of_nonpos_of_nonneg ha hb).not_lt) @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_nontrivial {α : Type*} [linear_ordered_semiring α] : nontrivial α := { exists_pair_ne := ⟨0, 1, ne_of_lt zero_lt_one⟩ } /- TODO This theorem ought to be written in the context of `nontrivial` linearly ordered (additive) commutative monoids rather than linearly ordered rings; however, the former concept does not currently exist in mathlib. -/ @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_no_top_order {α : Type*} [linear_ordered_semiring α] : no_top_order α := ⟨assume a, ⟨a + 1, lt_add_of_pos_right _ zero_lt_one⟩⟩ end linear_ordered_semiring section mono variables {β : Type*} [linear_ordered_semiring α] [preorder β] {f g : β → α} {a : α} lemma monotone_mul_left_of_nonneg (ha : 0 ≤ a) : monotone (λ x, a*x) := assume b c b_le_c, mul_le_mul_of_nonneg_left b_le_c ha lemma monotone_mul_right_of_nonneg (ha : 0 ≤ a) : monotone (λ x, x*a) := assume b c b_le_c, mul_le_mul_of_nonneg_right b_le_c ha lemma monotone.mul_const (hf : monotone f) (ha : 0 ≤ a) : monotone (λ x, (f x) * a) := (monotone_mul_right_of_nonneg ha).comp hf lemma monotone.const_mul (hf : monotone f) (ha : 0 ≤ a) : monotone (λ x, a * (f x)) := (monotone_mul_left_of_nonneg ha).comp hf lemma monotone.mul (hf : monotone f) (hg : monotone g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 ≤ g x) : monotone (λ x, f x * g x) := λ x y h, mul_le_mul (hf h) (hg h) (hg0 x) (hf0 y) lemma strict_mono_mul_left_of_pos (ha : 0 < a) : strict_mono (λ x, a * x) := assume b c b_lt_c, (mul_lt_mul_left ha).2 b_lt_c lemma strict_mono_mul_right_of_pos (ha : 0 < a) : strict_mono (λ x, x * a) := assume b c b_lt_c, (mul_lt_mul_right ha).2 b_lt_c lemma strict_mono.mul_const (hf : strict_mono f) (ha : 0 < a) : strict_mono (λ x, (f x) * a) := (strict_mono_mul_right_of_pos ha).comp hf lemma strict_mono.const_mul (hf : strict_mono f) (ha : 0 < a) : strict_mono (λ x, a * (f x)) := (strict_mono_mul_left_of_pos ha).comp hf lemma strict_mono.mul_monotone (hf : strict_mono f) (hg : monotone g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 < g x) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul (hf h) (hg h.le) (hg0 x) (hf0 y) lemma monotone.mul_strict_mono (hf : monotone f) (hg : strict_mono g) (hf0 : ∀ x, 0 < f x) (hg0 : ∀ x, 0 ≤ g x) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul' (hf h.le) (hg h) (hg0 x) (hf0 y) lemma strict_mono.mul (hf : strict_mono f) (hg : strict_mono g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 ≤ g x) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul'' (hf h) (hg h) (hf0 x) (hg0 x) end mono /-- A `decidable_linear_ordered_semiring α` is a semiring `α` with a decidable linear order such that multiplication with a positive number and addition are monotone. -/ @[protect_proj] class decidable_linear_ordered_semiring (α : Type u) extends linear_ordered_semiring α, decidable_linear_order α section decidable_linear_ordered_semiring variables [decidable_linear_ordered_semiring α] {a b c : α} @[simp] lemma decidable.mul_le_mul_left (h : 0 < c) : c * a ≤ c * b ↔ a ≤ b := decidable.le_iff_le_iff_lt_iff_lt.2 $ mul_lt_mul_left h @[simp] lemma decidable.mul_le_mul_right (h : 0 < c) : a * c ≤ b * c ↔ a ≤ b := decidable.le_iff_le_iff_lt_iff_lt.2 $ mul_lt_mul_right h end decidable_linear_ordered_semiring /-- An `ordered_ring α` is a ring `α` with a partial order such that multiplication with a positive number and addition are monotone. -/ @[protect_proj] class ordered_ring (α : Type u) extends ring α, ordered_add_comm_group α, nontrivial α := (zero_lt_one : zero < one) (mul_pos : ∀ a b : α, 0 < a → 0 < b → 0 < a * b) section ordered_ring variables [ordered_ring α] {a b c : α} lemma ordered_ring.mul_nonneg (a b : α) (h₁ : 0 ≤ a) (h₂ : 0 ≤ b) : 0 ≤ a * b := begin cases classical.em (a ≤ 0), { simp [le_antisymm h h₁] }, cases classical.em (b ≤ 0), { simp [le_antisymm h_1 h₂] }, exact (le_not_le_of_lt (ordered_ring.mul_pos a b (h₁.lt_of_not_le h) (h₂.lt_of_not_le h_1))).left, end lemma ordered_ring.mul_le_mul_of_nonneg_left (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b := have 0 ≤ b - a, from sub_nonneg_of_le h₁, have 0 ≤ c * (b - a), from ordered_ring.mul_nonneg c (b - a) h₂ this, begin rw mul_sub_left_distrib at this, apply le_of_sub_nonneg this end lemma ordered_ring.mul_le_mul_of_nonneg_right (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c := have 0 ≤ b - a, from sub_nonneg_of_le h₁, have 0 ≤ (b - a) * c, from ordered_ring.mul_nonneg (b - a) c this h₂, begin rw mul_sub_right_distrib at this, apply le_of_sub_nonneg this end lemma ordered_ring.mul_lt_mul_of_pos_left (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b := have 0 < b - a, from sub_pos_of_lt h₁, have 0 < c * (b - a), from ordered_ring.mul_pos c (b - a) h₂ this, begin rw mul_sub_left_distrib at this, apply lt_of_sub_pos this end lemma ordered_ring.mul_lt_mul_of_pos_right (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c := have 0 < b - a, from sub_pos_of_lt h₁, have 0 < (b - a) * c, from ordered_ring.mul_pos (b - a) c this h₂, begin rw mul_sub_right_distrib at this, apply lt_of_sub_pos this end @[priority 100] -- see Note [lower instance priority] instance ordered_ring.to_ordered_semiring : ordered_semiring α := { mul_zero := mul_zero, zero_mul := zero_mul, add_left_cancel := @add_left_cancel α _, add_right_cancel := @add_right_cancel α _, le_of_add_le_add_left := @le_of_add_le_add_left α _, mul_lt_mul_of_pos_left := @ordered_ring.mul_lt_mul_of_pos_left α _, mul_lt_mul_of_pos_right := @ordered_ring.mul_lt_mul_of_pos_right α _, ..‹ordered_ring α› } lemma mul_le_mul_of_nonpos_left {a b c : α} (h : b ≤ a) (hc : c ≤ 0) : c * a ≤ c * b := have -c ≥ 0, from neg_nonneg_of_nonpos hc, have -c * b ≤ -c * a, from mul_le_mul_of_nonneg_left h this, have -(c * b) ≤ -(c * a), by rwa [← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul] at this, le_of_neg_le_neg this lemma mul_le_mul_of_nonpos_right {a b c : α} (h : b ≤ a) (hc : c ≤ 0) : a * c ≤ b * c := have -c ≥ 0, from neg_nonneg_of_nonpos hc, have b * -c ≤ a * -c, from mul_le_mul_of_nonneg_right h this, have -(b * c) ≤ -(a * c), by rwa [← neg_mul_eq_mul_neg, ← neg_mul_eq_mul_neg] at this, le_of_neg_le_neg this lemma mul_nonneg_of_nonpos_of_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b := have 0 * b ≤ a * b, from mul_le_mul_of_nonpos_right ha hb, by rwa zero_mul at this lemma mul_lt_mul_of_neg_left {a b c : α} (h : b < a) (hc : c < 0) : c * a < c * b := have -c > 0, from neg_pos_of_neg hc, have -c * b < -c * a, from mul_lt_mul_of_pos_left h this, have -(c * b) < -(c * a), by rwa [← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul] at this, lt_of_neg_lt_neg this lemma mul_lt_mul_of_neg_right {a b c : α} (h : b < a) (hc : c < 0) : a * c < b * c := have -c > 0, from neg_pos_of_neg hc, have b * -c < a * -c, from mul_lt_mul_of_pos_right h this, have -(b * c) < -(a * c), by rwa [← neg_mul_eq_mul_neg, ← neg_mul_eq_mul_neg] at this, lt_of_neg_lt_neg this lemma mul_pos_of_neg_of_neg {a b : α} (ha : a < 0) (hb : b < 0) : 0 < a * b := have 0 * b < a * b, from mul_lt_mul_of_neg_right ha hb, by rwa zero_mul at this end ordered_ring /-- A `linear_ordered_ring α` is a ring `α` with a linear order such that multiplication with a positive number and addition are monotone. -/ @[protect_proj] class linear_ordered_ring (α : Type u) extends ordered_ring α, linear_order α section linear_ordered_ring variables [linear_ordered_ring α] {a b c : α} @[priority 100] -- see Note [lower instance priority] instance linear_ordered_ring.to_linear_ordered_semiring : linear_ordered_semiring α := { mul_zero := mul_zero, zero_mul := zero_mul, add_left_cancel := @add_left_cancel α _, add_right_cancel := @add_right_cancel α _, le_of_add_le_add_left := @le_of_add_le_add_left α _, mul_lt_mul_of_pos_left := @mul_lt_mul_of_pos_left α _, mul_lt_mul_of_pos_right := @mul_lt_mul_of_pos_right α _, le_total := linear_ordered_ring.le_total, ..‹linear_ordered_ring α› } @[priority 100] -- see Note [lower instance priority] instance linear_ordered_ring.to_domain : domain α := { eq_zero_or_eq_zero_of_mul_eq_zero := begin intros a b hab, contrapose! hab, cases (lt_or_gt_of_ne hab.1) with ha ha; cases (lt_or_gt_of_ne hab.2) with hb hb, exacts [(mul_pos_of_neg_of_neg ha hb).ne.symm, (mul_neg_of_neg_of_pos ha hb).ne, (mul_neg_of_pos_of_neg ha hb).ne, (mul_pos ha hb).ne.symm] end, .. ‹linear_ordered_ring α› } lemma mul_pos_iff : 0 < a * b ↔ 0 < a ∧ 0 < b ∨ a < 0 ∧ b < 0 := ⟨pos_and_pos_or_neg_and_neg_of_mul_pos, λ h, h.elim (and_imp.2 mul_pos) (and_imp.2 mul_pos_of_neg_of_neg)⟩ lemma mul_neg_iff : a * b < 0 ↔ 0 < a ∧ b < 0 ∨ a < 0 ∧ 0 < b := by rw [← neg_pos, neg_mul_eq_mul_neg, mul_pos_iff, neg_pos, neg_lt_zero] lemma mul_nonneg_iff : 0 ≤ a * b ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 := ⟨nonneg_and_nonneg_or_nonpos_and_nonpos_of_mul_nnonneg, λ h, h.elim (and_imp.2 mul_nonneg) (and_imp.2 mul_nonneg_of_nonpos_of_nonpos)⟩ lemma mul_nonpos_iff : a * b ≤ 0 ↔ 0 ≤ a ∧ b ≤ 0 ∨ a ≤ 0 ∧ 0 ≤ b := by rw [← neg_nonneg, neg_mul_eq_mul_neg, mul_nonneg_iff, neg_nonneg, neg_nonpos] lemma mul_self_nonneg (a : α) : 0 ≤ a * a := or.elim (le_total 0 a) (assume h : a ≥ 0, mul_nonneg h h) (assume h : a ≤ 0, mul_nonneg_of_nonpos_of_nonpos h h) lemma gt_of_mul_lt_mul_neg_left (h : c * a < c * b) (hc : c ≤ 0) : b < a := have nhc : 0 ≤ -c, from neg_nonneg_of_nonpos hc, have h2 : -(c * b) < -(c * a), from neg_lt_neg h, have h3 : (-c) * b < (-c) * a, from calc (-c) * b = - (c * b) : by rewrite neg_mul_eq_neg_mul ... < -(c * a) : h2 ... = (-c) * a : by rewrite neg_mul_eq_neg_mul, lt_of_mul_lt_mul_left h3 nhc lemma neg_one_lt_zero : -1 < (0:α) := begin have this := neg_lt_neg (@zero_lt_one α _), rwa neg_zero at this end lemma le_of_mul_le_of_one_le {a b c : α} (h : a * c ≤ b) (hb : 0 ≤ b) (hc : 1 ≤ c) : a ≤ b := have h' : a * c ≤ b * c, from calc a * c ≤ b : h ... = b * 1 : by rewrite mul_one ... ≤ b * c : mul_le_mul_of_nonneg_left hc hb, le_of_mul_le_mul_right h' (zero_lt_one.trans_le hc) lemma nonneg_le_nonneg_of_squares_le {a b : α} (hb : 0 ≤ b) (h : a * a ≤ b * b) : a ≤ b := le_of_not_gt (λhab, (mul_self_lt_mul_self hb hab).not_le h) lemma mul_self_le_mul_self_iff {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a ≤ b ↔ a * a ≤ b * b := ⟨mul_self_le_mul_self h1, nonneg_le_nonneg_of_squares_le h2⟩ lemma mul_self_lt_mul_self_iff {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a < b ↔ a * a < b * b := iff.trans (lt_iff_not_ge _ _) $ iff.trans (not_iff_not_of_iff $ mul_self_le_mul_self_iff h2 h1) $ iff.symm (lt_iff_not_ge _ _) /- TODO This theorem ought to be written in the context of `nontrivial` linearly ordered (additive) commutative groups rather than linearly ordered rings; however, the former concept does not currently exist in mathlib. -/ @[priority 100] -- see Note [lower instance priority] instance linear_ordered_ring.to_no_bot_order : no_bot_order α := ⟨assume a, ⟨a - 1, sub_lt_iff_lt_add.mpr $ lt_add_of_pos_right _ zero_lt_one⟩⟩ @[simp] lemma mul_le_mul_left_of_neg {a b c : α} (h : c < 0) : c * a ≤ c * b ↔ b ≤ a := ⟨le_imp_le_of_lt_imp_lt $ λ h', mul_lt_mul_of_neg_left h' h, λ h', mul_le_mul_of_nonpos_left h' h.le⟩ @[simp] lemma mul_le_mul_right_of_neg {a b c : α} (h : c < 0) : a * c ≤ b * c ↔ b ≤ a := ⟨le_imp_le_of_lt_imp_lt $ λ h', mul_lt_mul_of_neg_right h' h, λ h', mul_le_mul_of_nonpos_right h' h.le⟩ @[simp] lemma mul_lt_mul_left_of_neg {a b c : α} (h : c < 0) : c * a < c * b ↔ b < a := lt_iff_lt_of_le_iff_le (mul_le_mul_left_of_neg h) @[simp] lemma mul_lt_mul_right_of_neg {a b c : α} (h : c < 0) : a * c < b * c ↔ b < a := lt_iff_lt_of_le_iff_le (mul_le_mul_right_of_neg h) lemma sub_one_lt (a : α) : a - 1 < a := sub_lt_iff_lt_add.2 (lt_add_one a) lemma mul_self_pos {a : α} (ha : a ≠ 0) : 0 < a * a := by rcases lt_trichotomy a 0 with h|h|h; [exact mul_pos_of_neg_of_neg h h, exact (ha h).elim, exact mul_pos h h] lemma mul_self_le_mul_self_of_le_of_neg_le {x y : α} (h₁ : x ≤ y) (h₂ : -x ≤ y) : x * x ≤ y * y := begin cases le_total 0 x, { exact mul_self_le_mul_self h h₁ }, { rw ← neg_mul_neg, exact mul_self_le_mul_self (neg_nonneg_of_nonpos h) h₂ } end lemma nonneg_of_mul_nonpos_left {a b : α} (h : a * b ≤ 0) (hb : b < 0) : 0 ≤ a := le_of_not_gt (λ ha, absurd h (mul_pos_of_neg_of_neg ha hb).not_le) lemma nonneg_of_mul_nonpos_right {a b : α} (h : a * b ≤ 0) (ha : a < 0) : 0 ≤ b := le_of_not_gt (λ hb, absurd h (mul_pos_of_neg_of_neg ha hb).not_le) lemma pos_of_mul_neg_left {a b : α} (h : a * b < 0) (hb : b ≤ 0) : 0 < a := lt_of_not_ge (λ ha, absurd h (mul_nonneg_of_nonpos_of_nonpos ha hb).not_lt) lemma pos_of_mul_neg_right {a b : α} (h : a * b < 0) (ha : a ≤ 0) : 0 < b := lt_of_not_ge (λ hb, absurd h (mul_nonneg_of_nonpos_of_nonpos ha hb).not_lt) /- The sum of two squares is zero iff both elements are zero. -/ lemma mul_self_add_mul_self_eq_zero {x y : α} : x * x + y * y = 0 ↔ x = 0 ∧ y = 0 := begin split; intro h, swap, { rcases h with ⟨rfl, rfl⟩, simp }, have : y * y ≤ 0, { rw [← h], apply le_add_of_nonneg_left (mul_self_nonneg x) }, have : y * y = 0 := le_antisymm this (mul_self_nonneg y), have hx : x = 0, { rwa [this, add_zero, mul_self_eq_zero] at h }, rw mul_self_eq_zero at this, split; assumption end end linear_ordered_ring /-- A `linear_ordered_comm_ring α` is a commutative ring `α` with a linear order such that multiplication with a positive number and addition are monotone. -/ @[protect_proj] class linear_ordered_comm_ring (α : Type u) extends linear_ordered_ring α, comm_monoid α @[priority 100] -- see Note [lower instance priority] instance linear_ordered_comm_ring.to_integral_domain [s: linear_ordered_comm_ring α] : integral_domain α := { ..s, .. linear_ordered_ring.to_domain } /-- A `decidable_linear_ordered_comm_ring α` is a commutative ring `α` with a decidable linear order such that multiplication with a positive number and addition are monotone. -/ @[protect_proj] class decidable_linear_ordered_comm_ring (α : Type u) extends linear_ordered_comm_ring α, decidable_linear_ordered_add_comm_group α @[priority 100] -- see Note [lower instance priority] instance decidable_linear_ordered_comm_ring.to_decidable_linear_ordered_semiring [d : decidable_linear_ordered_comm_ring α] : decidable_linear_ordered_semiring α := let s : linear_ordered_semiring α := @linear_ordered_ring.to_linear_ordered_semiring α _ in { zero_mul := @linear_ordered_semiring.zero_mul α s, mul_zero := @linear_ordered_semiring.mul_zero α s, add_left_cancel := @linear_ordered_semiring.add_left_cancel α s, add_right_cancel := @linear_ordered_semiring.add_right_cancel α s, le_of_add_le_add_left := @linear_ordered_semiring.le_of_add_le_add_left α s, mul_lt_mul_of_pos_left := @linear_ordered_semiring.mul_lt_mul_of_pos_left α s, mul_lt_mul_of_pos_right := @linear_ordered_semiring.mul_lt_mul_of_pos_right α s, ..d } section decidable_linear_ordered_comm_ring variables [decidable_linear_ordered_comm_ring α] {a b c : α} lemma abs_mul (a b : α) : abs (a * b) = abs a * abs b := or.elim (le_total 0 a) (assume h1 : 0 ≤ a, or.elim (le_total 0 b) (assume h2 : 0 ≤ b, calc abs (a * b) = a * b : abs_of_nonneg (mul_nonneg h1 h2) ... = abs a * b : by rw (abs_of_nonneg h1) ... = abs a * abs b : by rw (abs_of_nonneg h2)) (assume h2 : b ≤ 0, calc abs (a * b) = -(a * b) : abs_of_nonpos (mul_nonpos_of_nonneg_of_nonpos h1 h2) ... = a * -b : by rw neg_mul_eq_mul_neg ... = abs a * -b : by rw (abs_of_nonneg h1) ... = abs a * abs b : by rw (abs_of_nonpos h2))) (assume h1 : a ≤ 0, or.elim (le_total 0 b) (assume h2 : 0 ≤ b, calc abs (a * b) = -(a * b) : abs_of_nonpos (mul_nonpos_of_nonpos_of_nonneg h1 h2) ... = -a * b : by rw neg_mul_eq_neg_mul ... = abs a * b : by rw (abs_of_nonpos h1) ... = abs a * abs b : by rw (abs_of_nonneg h2)) (assume h2 : b ≤ 0, calc abs (a * b) = a * b : abs_of_nonneg (mul_nonneg_of_nonpos_of_nonpos h1 h2) ... = -a * -b : by rw neg_mul_neg ... = abs a * -b : by rw (abs_of_nonpos h1) ... = abs a * abs b : by rw (abs_of_nonpos h2))) lemma abs_mul_abs_self (a : α) : abs a * abs a = a * a := abs_by_cases (λ x, x * x = a * a) rfl (neg_mul_neg a a) lemma abs_mul_self (a : α) : abs (a * a) = a * a := by rw [abs_mul, abs_mul_abs_self] lemma sub_le_of_abs_sub_le_left (h : abs (a - b) ≤ c) : b - c ≤ a := if hz : 0 ≤ a - b then (calc a ≥ b : le_of_sub_nonneg hz ... ≥ b - c : sub_le_self _ $ (abs_nonneg _).trans h) else have habs : b - a ≤ c, by rwa [abs_of_neg (lt_of_not_ge hz), neg_sub] at h, have habs' : b ≤ c + a, from le_add_of_sub_right_le habs, sub_left_le_of_le_add habs' lemma sub_le_of_abs_sub_le_right (h : abs (a - b) ≤ c) : a - c ≤ b := sub_le_of_abs_sub_le_left (abs_sub a b ▸ h) lemma sub_lt_of_abs_sub_lt_left (h : abs (a - b) < c) : b - c < a := if hz : 0 ≤ a - b then (calc a ≥ b : le_of_sub_nonneg hz ... > b - c : sub_lt_self _ ((abs_nonneg _).trans_lt h)) else have habs : b - a < c, by rwa [abs_of_neg (lt_of_not_ge hz), neg_sub] at h, have habs' : b < c + a, from lt_add_of_sub_right_lt habs, sub_left_lt_of_lt_add habs' lemma sub_lt_of_abs_sub_lt_right (h : abs (a - b) < c) : a - c < b := sub_lt_of_abs_sub_lt_left (abs_sub a b ▸ h) lemma abs_sub_square (a b : α) : abs (a - b) * abs (a - b) = a * a + b * b - (1 + 1) * a * b := begin rw abs_mul_abs_self, simp [left_distrib, right_distrib, add_assoc, add_comm, add_left_comm, mul_comm, sub_eq_add_neg] end lemma eq_zero_of_mul_self_add_mul_self_eq_zero (h : a * a + b * b = 0) : a = 0 := have a * a ≤ (0 : α), from calc a * a ≤ a * a + b * b : le_add_of_nonneg_right (mul_self_nonneg b) ... = 0 : h, eq_zero_of_mul_self_eq_zero (le_antisymm this (mul_self_nonneg a)) lemma abs_abs_sub_abs_le_abs_sub (a b : α) : abs (abs a - abs b) ≤ abs (a - b) := begin apply nonneg_le_nonneg_of_squares_le, apply abs_nonneg, iterate {rw abs_sub_square}, iterate {rw abs_mul_abs_self}, apply sub_le_sub_left, iterate {rw mul_assoc}, apply mul_le_mul_of_nonneg_left, { rw [← abs_mul], apply le_abs_self }, { exact (add_pos (@zero_lt_one α _) zero_lt_one).le } end -- The proof doesn't need commutativity but we have no `decidable_linear_ordered_ring` @[simp] lemma abs_two : abs (2:α) = 2 := abs_of_pos zero_lt_two end decidable_linear_ordered_comm_ring /-- Extend `nonneg_add_comm_group` to support ordered rings specified by their nonnegative elements -/ class nonneg_ring (α : Type*) extends ring α, nonneg_add_comm_group α, nontrivial α := (one_pos : pos 1) (mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b)) (mul_pos : ∀ {a b}, pos a → pos b → pos (a * b)) /-- Extend `nonneg_add_comm_group` to support linearly ordered rings specified by their nonnegative elements -/ class linear_nonneg_ring (α : Type*) extends domain α, nonneg_add_comm_group α := (one_pos : pos 1) (mul_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a * b)) (nonneg_total : ∀ a, nonneg a ∨ nonneg (-a)) namespace nonneg_ring open nonneg_add_comm_group variable [nonneg_ring α] @[priority 100] -- see Note [lower instance priority] instance to_ordered_ring : ordered_ring α := { zero_lt_one := begin dsimp [(<), preorder.lt, partial_order.lt], convert one_pos, exact sub_zero _, end, mul_pos := λ a b, by simp [pos_def.symm]; exact mul_pos, ..‹nonneg_ring α›, ..(infer_instance : ordered_add_comm_group α) } /-- `to_linear_nonneg_ring` shows that a `nonneg_ring` with a total order is a `domain`, hence a `linear_nonneg_ring`. -/ def to_linear_nonneg_ring (nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a)) : linear_nonneg_ring α := { nonneg_total := nonneg_total, eq_zero_or_eq_zero_of_mul_eq_zero := suffices ∀ {a} b : α, nonneg a → a * b = 0 → a = 0 ∨ b = 0, from λ a b, (nonneg_total a).elim (this b) (λ na, by simpa using this b na), suffices ∀ {a b : α}, nonneg a → nonneg b → a * b = 0 → a = 0 ∨ b = 0, from λ a b na, (nonneg_total b).elim (this na) (λ nb, by simpa using this na nb), λ a b na nb z, classical.by_cases (λ nna : nonneg (-a), or.inl (nonneg_antisymm na nna)) (λ pa, classical.by_cases (λ nnb : nonneg (-b), or.inr (nonneg_antisymm nb nnb)) (λ pb, absurd z $ ne_of_gt $ pos_def.1 $ mul_pos ((pos_iff _).2 ⟨na, pa⟩) ((pos_iff _).2 ⟨nb, pb⟩))), ..‹nonneg_ring α› } end nonneg_ring namespace linear_nonneg_ring open nonneg_add_comm_group variable [linear_nonneg_ring α] @[priority 100] -- see Note [lower instance priority] instance to_nonneg_ring : nonneg_ring α := { mul_pos := λ a b pa pb, let ⟨a1, a2⟩ := (pos_iff a).1 pa, ⟨b1, b2⟩ := (pos_iff b).1 pb in have ab : nonneg (a * b), from mul_nonneg a1 b1, (pos_iff _).2 ⟨ab, λ hn, have a * b = 0, from nonneg_antisymm ab hn, (eq_zero_or_eq_zero_of_mul_eq_zero _ _ this).elim (ne_of_gt (pos_def.1 pa)) (ne_of_gt (pos_def.1 pb))⟩, ..‹linear_nonneg_ring α› } @[priority 100] -- see Note [lower instance priority] instance to_linear_order : linear_order α := { le_total := nonneg_total_iff.1 nonneg_total, ..‹linear_nonneg_ring α›, ..(infer_instance : ordered_add_comm_group α) } @[priority 100] -- see Note [lower instance priority] instance to_linear_ordered_ring : linear_ordered_ring α := { mul_pos := by simp [pos_def.symm]; exact @nonneg_ring.mul_pos _ _, zero_lt_one := lt_of_not_ge $ λ (h : nonneg (0 - 1)), begin rw [zero_sub] at h, have := mul_nonneg h h, simp at this, exact zero_ne_one (nonneg_antisymm this h).symm end, ..‹linear_nonneg_ring α›, ..(infer_instance : ordered_add_comm_group α), ..(infer_instance : linear_order α) } /-- Convert a `linear_nonneg_ring` with a commutative multiplication and decidable non-negativity into a `decidable_linear_ordered_comm_ring` -/ def to_decidable_linear_ordered_comm_ring [decidable_pred (@nonneg α _)] [comm : @is_commutative α (*)] : decidable_linear_ordered_comm_ring α := { decidable_le := by apply_instance, decidable_lt := by apply_instance, mul_comm := is_commutative.comm, ..@linear_nonneg_ring.to_linear_ordered_ring _ _ } end linear_nonneg_ring /-- A canonically ordered commutative semiring is an ordered, commutative semiring in which `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other ordered groups. -/ class canonically_ordered_comm_semiring (α : Type*) extends canonically_ordered_add_monoid α, comm_semiring α, nontrivial α := (eq_zero_or_eq_zero_of_mul_eq_zero : ∀ a b : α, a * b = 0 → a = 0 ∨ b = 0) namespace canonically_ordered_semiring variables [canonically_ordered_comm_semiring α] {a b : α} open canonically_ordered_add_monoid (le_iff_exists_add) @[priority 100] -- see Note [lower instance priority] instance canonically_ordered_comm_semiring.to_no_zero_divisors : no_zero_divisors α := ⟨canonically_ordered_comm_semiring.eq_zero_or_eq_zero_of_mul_eq_zero⟩ lemma mul_le_mul {a b c d : α} (hab : a ≤ b) (hcd : c ≤ d) : a * c ≤ b * d := begin rcases (le_iff_exists_add _ _).1 hab with ⟨b, rfl⟩, rcases (le_iff_exists_add _ _).1 hcd with ⟨d, rfl⟩, suffices : a * c ≤ a * c + (a * d + b * c + b * d), by simpa [mul_add, add_mul, _root_.add_assoc], exact (le_iff_exists_add _ _).2 ⟨_, rfl⟩ end lemma mul_le_mul_left' {b c : α} (h : b ≤ c) (a : α) : a * b ≤ a * c := mul_le_mul (le_refl a) h lemma mul_le_mul_right' {b c : α} (h : b ≤ c) (a : α) : b * a ≤ c * a := mul_le_mul h (le_refl a) /-- A version of `zero_lt_one : 0 < 1` for a `canonically_ordered_comm_semiring`. -/ lemma zero_lt_one : (0:α) < 1 := (zero_le 1).lt_of_ne zero_ne_one lemma mul_pos : 0 < a * b ↔ (0 < a) ∧ (0 < b) := by simp only [zero_lt_iff_ne_zero, ne.def, mul_eq_zero, not_or_distrib] end canonically_ordered_semiring namespace with_top instance [nonempty α] : nontrivial (with_top α) := option.nontrivial variable [decidable_eq α] section has_mul variables [has_zero α] [has_mul α] instance : mul_zero_class (with_top α) := { zero := 0, mul := λm n, if m = 0 ∨ n = 0 then 0 else m.bind (λa, n.bind $ λb, ↑(a * b)), zero_mul := assume a, if_pos $ or.inl rfl, mul_zero := assume a, if_pos $ or.inr rfl } lemma mul_def {a b : with_top α} : a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl @[simp] lemma mul_top {a : with_top α} (h : a ≠ 0) : a * ⊤ = ⊤ := by cases a; simp [mul_def, h]; refl @[simp] lemma top_mul {a : with_top α} (h : a ≠ 0) : ⊤ * a = ⊤ := by cases a; simp [mul_def, h]; refl @[simp] lemma top_mul_top : (⊤ * ⊤ : with_top α) = ⊤ := top_mul top_ne_zero end has_mul section mul_zero_class variables [mul_zero_class α] @[norm_cast] lemma coe_mul {a b : α} : (↑(a * b) : with_top α) = a * b := decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha, decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb, by { simp [*, mul_def], refl } lemma mul_coe {b : α} (hb : b ≠ 0) : ∀{a : with_top α}, a * b = a.bind (λa:α, ↑(a * b)) | none := show (if (⊤:with_top α) = 0 ∨ (b:with_top α) = 0 then 0 else ⊤ : with_top α) = ⊤, by simp [hb] | (some a) := show ↑a * ↑b = ↑(a * b), from coe_mul.symm @[simp] lemma mul_eq_top_iff {a b : with_top α} : a * b = ⊤ ↔ (a ≠ 0 ∧ b = ⊤) ∨ (a = ⊤ ∧ b ≠ 0) := begin cases a; cases b; simp only [none_eq_top, some_eq_coe], { simp [← coe_mul] }, { suffices : ⊤ * (b : with_top α) = ⊤ ↔ b ≠ 0, by simpa, by_cases hb : b = 0; simp [hb] }, { suffices : (a : with_top α) * ⊤ = ⊤ ↔ a ≠ 0, by simpa, by_cases ha : a = 0; simp [ha] }, { simp [← coe_mul] } end end mul_zero_class section no_zero_divisors variables [mul_zero_class α] [no_zero_divisors α] instance : no_zero_divisors (with_top α) := ⟨λ a b, by cases a; cases b; dsimp [mul_def]; split_ifs; simp [*, none_eq_top, some_eq_coe, mul_eq_zero] at *⟩ end no_zero_divisors variables [canonically_ordered_comm_semiring α] private lemma comm (a b : with_top α) : a * b = b * a := begin by_cases ha : a = 0, { simp [ha] }, by_cases hb : b = 0, { simp [hb] }, simp [ha, hb, mul_def, option.bind_comm a b, mul_comm] end private lemma distrib' (a b c : with_top α) : (a + b) * c = a * c + b * c := begin cases c, { show (a + b) * ⊤ = a * ⊤ + b * ⊤, by_cases ha : a = 0; simp [ha] }, { show (a + b) * c = a * c + b * c, by_cases hc : c = 0, { simp [hc] }, simp [mul_coe hc], cases a; cases b, repeat { refl <|> exact congr_arg some (add_mul _ _ _) } } end private lemma assoc (a b c : with_top α) : (a * b) * c = a * (b * c) := begin cases a, { by_cases hb : b = 0; by_cases hc : c = 0; simp [*, none_eq_top] }, cases b, { by_cases ha : a = 0; by_cases hc : c = 0; simp [*, none_eq_top, some_eq_coe] }, cases c, { by_cases ha : a = 0; by_cases hb : b = 0; simp [*, none_eq_top, some_eq_coe] }, simp [some_eq_coe, coe_mul.symm, mul_assoc] end private lemma one_mul' : ∀a : with_top α, 1 * a = a | none := show ((1:α) : with_top α) * ⊤ = ⊤, by simp [-with_top.coe_one] | (some a) := show ((1:α) : with_top α) * a = a, by simp [coe_mul.symm, -with_top.coe_one] instance : canonically_ordered_comm_semiring (with_top α) := { one := (1 : α), right_distrib := distrib', left_distrib := assume a b c, by rw [comm, distrib', comm b, comm c]; refl, mul_assoc := assoc, mul_comm := comm, one_mul := one_mul', mul_one := assume a, by rw [comm, one_mul'], .. with_top.add_comm_monoid, .. with_top.mul_zero_class, .. with_top.canonically_ordered_add_monoid, .. with_top.no_zero_divisors, .. with_top.nontrivial } end with_top
8a046557e57d0dfb5d9a9d9f9a3ff15d499051d6
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/cases_induction_fresh.lean
7c27773163fadf7fc5c1d4066acd23ca33a9eaf3
[ "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
646
lean
example (p q r s: Prop): p ∧ q → r ∧ s → s ∧ q := begin intros h1 h2, cases h1, cases h2, trace_state, constructor; assumption end example (p q r s: Prop): p ∧ q → r ∧ s → s ∧ q := begin intros a a_1, cases a, cases a_1, trace_state, constructor; assumption end #print "------------" example (p q r s: Prop): p ∧ q → r ∧ s → s ∧ q := begin intros h1 h2, induction h1, induction h2, trace_state, constructor; assumption end example (p q r s: Prop): p ∧ q → r ∧ s → s ∧ q := begin intros a a_1, induction a, induction a_1, trace_state, constructor; assumption end
8789c40429a04b0da0194844d04244da774ef6b4
c2b0bd5aa4b3f4fb8a887fab57c5fc08be03aa01
/applicative.lean
0a26ef3160b820405403d389d9619e1ce74311a0
[]
no_license
cipher1024/lambda-calc
f72aa6dedea96fc7831d1898f3150a18baefdf0b
327eaa12588018c01b2205f3e4e0048de1d97afd
refs/heads/master
1,625,672,854,392
1,507,233,523,000
1,507,233,523,000
99,383,225
4
0
null
null
null
null
UTF-8
Lean
false
false
3,302
lean
universes u u₀ u₁ u₂ u₃ structure identity (α : Type u) : Type u := (run : α) structure const (α : Type u) (β : Type u₀) : Type u := (run : α) structure compose (f : Type u₁ → Type u₀) (g : Type u₂ → Type u₁) (α : Type u₂) : Type u₀ := (run : f (g α)) instance : monad identity := { bind := λ α β x f, f x.run , pure := λ α, identity.mk , id_map := λ α ⟨ x ⟩, rfl , bind_assoc := λ α β γ ⟨ x ⟩ f g, rfl , pure_bind := λ α β x f, rfl } instance {f : Type u₁ → Type u₀} {g : Type u₂ → Type u₁} [has_pure f] [has_pure g] : has_pure (compose f g) := { pure := λ α x, ⟨ pure $ pure x ⟩ } instance {f : Type u₁ → Type u₀} {g : Type u₂ → Type u₁} [has_pure f] [has_seq f] [has_seq g] : has_seq (compose f g) := { seq := λ α β ⟨ f ⟩ ⟨ x ⟩, ⟨ pure has_seq.seq <*> f <*> x ⟩ } section applicative open applicative has_map functor lemma id_map' {f : Type u → Type u₀} [functor f] : ∀ {α : Type u}, map id = (id : f α → f α) := by { introv, apply funext, intro, apply id_map } lemma pure_seq_eq_map' {f : Type u → Type u₀} [applicative f] : ∀ {α β : Type u} (g : α → β), (has_seq.seq $ has_pure.pure _ g) = (map g : f α → f β) := by { introv, apply funext, intro, apply pure_seq_eq_map } instance applicative_comp {f : Type u₁ → Type u₀} {g : Type u₂ → Type u₁} [applicative f] [applicative g] : applicative (compose f g) := { pure := @pure _ _ , seq := @has_seq.seq _ _ , seq_assoc := begin introv, cases x with x, cases h with h₀, cases g_1 with h₁, simp [pure,has_pure.pure,has_seq.seq,compose.has_seq._match_2,compose.has_seq._match_1], apply congr_arg, simp [seq_assoc], simp [pure_seq_eq_map'], simp [seq_pure,map_pure], repeat { rw ← map_comp }, unfold function.comp, tactic.congr, simp [seq_assoc,pure_seq_eq_map], end , seq_pure := begin introv, cases g_1 with h, simp [pure,has_pure.pure,has_seq.seq], simp [compose.has_seq._match_2,compose.has_seq._match_1], simp [pure_seq_eq_map,seq_pure,map_pure], rw ← map_comp, simp [function.comp,seq_pure,pure_seq_eq_map'], end , map_pure := begin introv, simp [pure,has_pure.pure,has_seq.seq,compose.has_seq._match_2,compose.has_seq._match_1], simp [pure_seq_eq_map,map_pure], end , pure_seq_eq_map := begin introv, cases x with x, simp, end , id_map := begin introv, cases x with x, simp [has_seq.seq,pure,has_pure.pure,compose.has_seq._match_2,compose.has_seq._match_1], simp [pure_seq_eq_map,map_pure,pure_seq_eq_map',id_map'], end } instance {α} : functor (const α) := { map := λ β γ f x, ⟨ _, x.run ⟩ , id_map := by { introv, cases x, simp, } , map_comp := by { introv, cases x, simp } } instance {α} [monoid α] : applicative (const α) := { map := @has_map.map _ _ , seq := λ α β f x, ⟨ _, f.run * x.run ⟩ , pure := λ α x, ⟨ _, 1 ⟩ , id_map := @functor.id_map _ _ , seq_pure := by { introv, cases g, simp [has_map.map] } , map_pure := by { introv, simp [has_map.map] } , pure_seq_eq_map := by { introv, simp [has_map.map] } , seq_assoc := by { introv, cases g, simp [has_map.map] } } end applicative
1305640dcfbdb1e6da13669e07560426760cd9ce
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/hott/eq1.hlean
420bb851e100f45d864f88341baa7d3a4f5896f6
[ "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
534
hlean
open nat inductive vector (A : Type) : nat → Type := nil {} : vector A zero, cons : Π {n}, A → vector A n → vector A (succ n) infixr `::` := vector.cons definition swap {A : Type} : Π {n}, vector A (succ (succ n)) → vector A (succ (succ n)), swap (a :: b :: vs) := b :: a :: vs -- Remark: in the current approach for HoTT, the equation -- swap (a :: b :: v) = b :: a :: v -- holds definitionally only when the index is a closed term. example (a b : num) (v : vector num 5) : swap (a :: b :: v) = b :: a :: v := rfl
3cbbb96d93f092c2eb880dc5ec228a32b8c6a335
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/algebra/field.lean
20c062c2a107ab0cf984641b4c3af09d0ecd96f5
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,315
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis, Leonardo de Moura Structures with multiplicative and additive components, including division rings and fields. The development is modeled after Isabelle's library. -/ prelude import init.algebra.ring universe variables u /- Make sure instances defined in this file have lower priority than the ones defined for concrete structures -/ set_option default_priority 100 class division_ring (α : Type u) extends ring α, has_inv α, zero_ne_one_class α := (mul_inv_cancel : ∀ {a : α}, a ≠ 0 → a * a⁻¹ = 1) (inv_mul_cancel : ∀ {a : α}, a ≠ 0 → a⁻¹ * a = 1) variable {α : Type u} section division_ring variables [division_ring α] protected definition algebra.div (a b : α) : α := a * b⁻¹ instance division_ring_has_div [division_ring α] : has_div α := ⟨algebra.div⟩ lemma division_def (a b : α) : a / b = a * b⁻¹ := rfl @[simp] lemma mul_inv_cancel {a : α} (h : a ≠ 0) : a * a⁻¹ = 1 := division_ring.mul_inv_cancel h @[simp] lemma inv_mul_cancel {a : α} (h : a ≠ 0) : a⁻¹ * a = 1 := division_ring.inv_mul_cancel h @[simp] lemma one_div_eq_inv (a : α) : 1 / a = a⁻¹ := one_mul a⁻¹ lemma inv_eq_one_div (a : α) : a⁻¹ = 1 / a := by simp local attribute [simp] division_def mul_comm mul_assoc mul_left_comm mul_inv_cancel inv_mul_cancel lemma div_eq_mul_one_div (a b : α) : a / b = a * (1 / b) := by simp lemma mul_one_div_cancel {a : α} (h : a ≠ 0) : a * (1 / a) = 1 := by simp [h] lemma one_div_mul_cancel {a : α} (h : a ≠ 0) : (1 / a) * a = 1 := by simp [h] lemma div_self {a : α} (h : a ≠ 0) : a / a = 1 := by simp [h] lemma one_div_one : 1 / 1 = (1:α) := div_self (ne.symm zero_ne_one) lemma mul_div_assoc (a b c : α) : (a * b) / c = a * (b / c) := by simp lemma one_div_ne_zero {a : α} (h : a ≠ 0) : 1 / a ≠ 0 := suppose 1 / a = 0, have 0 = (1:α), from eq.symm (by rw [-(mul_one_div_cancel h), this, mul_zero]), absurd this zero_ne_one lemma one_inv_eq : 1⁻¹ = (1:α) := suffices 1 * 1⁻¹ = (1:α), begin simp at this, assumption end, mul_inv_cancel (ne.symm (@zero_ne_one α _)) local attribute [simp] one_inv_eq lemma div_one (a : α) : a / 1 = a := by simp lemma zero_div (a : α) : 0 / a = 0 := by simp -- note: integral domain has a "mul_ne_zero". α commutative division ring is an integral -- domain, but let's not define that class for now. lemma division_ring.mul_ne_zero {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := suppose a * b = 0, have a * 1 = 0, by rw [-(mul_one_div_cancel hb), -mul_assoc, this, zero_mul], have a = 0, by rwa mul_one at this, absurd this ha lemma mul_ne_zero_comm {a b : α} (h : a * b ≠ 0) : b * a ≠ 0 := have h₁ : a ≠ 0, from ne_zero_of_mul_ne_zero_right h, have h₂ : b ≠ 0, from ne_zero_of_mul_ne_zero_left h, division_ring.mul_ne_zero h₂ h₁ lemma eq_one_div_of_mul_eq_one {a b : α} (h : a * b = 1) : b = 1 / a := have a ≠ 0, from suppose a = 0, have 0 = (1:α), by rwa [this, zero_mul] at h, absurd this zero_ne_one, have b = (1 / a) * a * b, by rw [one_div_mul_cancel this, one_mul], show b = 1 / a, by rwa [mul_assoc, h, mul_one] at this lemma eq_one_div_of_mul_eq_one_left {a b : α} (h : b * a = 1) : b = 1 / a := have a ≠ 0, from suppose a = 0, have 0 = (1:α), by rwa [this, mul_zero] at h, absurd this zero_ne_one, by rw [-h, mul_div_assoc, div_self this, mul_one] lemma division_ring.one_div_mul_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (1 / b) = 1 / (b * a) := have (b * a) * ((1 / a) * (1 / b)) = 1, by rw [mul_assoc, -(mul_assoc a), mul_one_div_cancel ha, one_mul, mul_one_div_cancel hb], eq_one_div_of_mul_eq_one this lemma one_div_neg_one_eq_neg_one : (1:α) / (-1) = -1 := have (-1) * (-1) = (1:α), by rw [neg_mul_neg, one_mul], eq.symm (eq_one_div_of_mul_eq_one this) lemma division_ring.one_div_neg_eq_neg_one_div {a : α} (h : a ≠ 0) : 1 / (- a) = - (1 / a) := have -1 ≠ (0:α), from (suppose -1 = 0, absurd (eq.symm (calc 1 = -(-1) : (neg_neg (1:α))^.symm ... = -0 : by rw this ... = (0:α) : neg_zero)) zero_ne_one), calc 1 / (- a) = 1 / ((-1) * a) : by rw neg_eq_neg_one_mul ... = (1 / a) * (1 / (- 1)) : by rw (division_ring.one_div_mul_one_div h this) ... = (1 / a) * (-1) : by rw one_div_neg_one_eq_neg_one ... = - (1 / a) : by rw [mul_neg_eq_neg_mul_symm, mul_one] lemma div_neg_eq_neg_div {a : α} (b : α) (ha : a ≠ 0) : b / (- a) = - (b / a) := calc b / (- a) = b * (1 / (- a)) : by rw [-inv_eq_one_div, division_def] ... = b * -(1 / a) : by rw (division_ring.one_div_neg_eq_neg_one_div ha) ... = -(b * (1 / a)) : by rw neg_mul_eq_mul_neg ... = - (b * a⁻¹) : by rw inv_eq_one_div lemma neg_div (a b : α) : (-b) / a = - (b / a) := by rw [neg_eq_neg_one_mul, mul_div_assoc, -neg_eq_neg_one_mul] lemma division_ring.neg_div_neg_eq (a : α) {b : α} (hb : b ≠ 0) : (-a) / (-b) = a / b := by rw [(div_neg_eq_neg_div _ hb), neg_div, neg_neg] lemma division_ring.one_div_one_div {a : α} (h : a ≠ 0) : 1 / (1 / a) = a := eq.symm (eq_one_div_of_mul_eq_one_left (mul_one_div_cancel h)) lemma division_ring.eq_of_one_div_eq_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) (h : 1 / a = 1 / b) : a = b := by rw [-(division_ring.one_div_one_div ha), h, (division_ring.one_div_one_div hb)] lemma mul_inv_eq {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (b * a)⁻¹ = a⁻¹ * b⁻¹ := eq.symm $ calc a⁻¹ * b⁻¹ = (1 / a) * (1 / b) : by simp ... = (1 / (b * a)) : division_ring.one_div_mul_one_div ha hb ... = (b * a)⁻¹ : by simp lemma mul_div_cancel (a : α) {b : α} (hb : b ≠ 0) : a * b / b = a := by simp [hb] lemma div_mul_cancel (a : α) {b : α} (hb : b ≠ 0) : a / b * b = a := by simp [hb] lemma div_add_div_same (a b c : α) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma div_sub_div_same (a b c : α) : (a / c) - (b / c) = (a - b) / c := by rw [sub_eq_add_neg, -neg_div, div_add_div_same, sub_eq_add_neg] lemma one_div_mul_add_mul_one_div_eq_one_div_add_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (a + b) * (1 / b) = 1 / a + 1 / b := by rw [(left_distrib (1 / a)), (one_div_mul_cancel ha), right_distrib, one_mul, mul_assoc, (mul_one_div_cancel hb), mul_one, add_comm] lemma one_div_mul_sub_mul_one_div_eq_one_div_add_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (b - a) * (1 / b) = 1 / a - 1 / b := by rw [(mul_sub_left_distrib (1 / a)), (one_div_mul_cancel ha), mul_sub_right_distrib, one_mul, mul_assoc, (mul_one_div_cancel hb), mul_one] lemma div_eq_one_iff_eq (a : α) {b : α} (hb : b ≠ 0) : a / b = 1 ↔ a = b := iff.intro (suppose a / b = 1, calc a = a / b * b : by simp [hb] ... = 1 * b : by rw this ... = b : by simp) (suppose a = b, by simp [this, hb]) lemma eq_of_div_eq_one (a : α) {b : α} (Hb : b ≠ 0) : a / b = 1 → a = b := iff.mp $ div_eq_one_iff_eq a Hb lemma eq_div_iff_mul_eq (a b : α) {c : α} (hc : c ≠ 0) : a = b / c ↔ a * c = b := iff.intro (suppose a = b / c, by rw [this, (div_mul_cancel _ hc)]) (suppose a * c = b, by rw [-this, mul_div_cancel _ hc]) lemma eq_div_of_mul_eq (a b : α) {c : α} (hc : c ≠ 0) : a * c = b → a = b / c := iff.mpr $ eq_div_iff_mul_eq a b hc lemma mul_eq_of_eq_div (a b: α) {c : α} (hc : c ≠ 0) : a = b / c → a * c = b := iff.mp $ eq_div_iff_mul_eq a b hc lemma add_div_eq_mul_add_div (a b : α) {c : α} (hc : c ≠ 0) : a + b / c = (a * c + b) / c := have (a + b / c) * c = a * c + b, by rw [right_distrib, (div_mul_cancel _ hc)], (iff.mpr (eq_div_iff_mul_eq _ _ hc)) this lemma mul_mul_div (a : α) {c : α} (hc : c ≠ 0) : a = a * c * (1 / c) := by simp [hc] end division_ring class field (α : Type u) extends division_ring α, comm_ring α section field variable [field α] lemma field.one_div_mul_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (1 / b) = 1 / (a * b) := by rw [(division_ring.one_div_mul_one_div ha hb), mul_comm b] lemma field.div_mul_right {a b : α} (hb : b ≠ 0) (h : a * b ≠ 0) : a / (a * b) = 1 / b := have a ≠ 0, from ne_zero_of_mul_ne_zero_right h, eq.symm (calc 1 / b = a * ((1 / a) * (1 / b)) : by rw [-mul_assoc, mul_one_div_cancel this, one_mul] ... = a * (1 / (b * a)) : by rw (division_ring.one_div_mul_one_div this hb) ... = a * (a * b)⁻¹ : by rw [inv_eq_one_div, mul_comm a b]) lemma field.div_mul_left {a b : α} (ha : a ≠ 0) (h : a * b ≠ 0) : b / (a * b) = 1 / a := have b * a ≠ 0, from mul_ne_zero_comm h, by rw [mul_comm a, (field.div_mul_right ha this)] lemma mul_div_cancel_left {a : α} (b : α) (ha : a ≠ 0) : a * b / a = b := by rw [mul_comm a, (mul_div_cancel _ ha)] lemma mul_div_cancel' (a : α) {b : α} (hb : b ≠ 0) : b * (a / b) = a := by rw [mul_comm, (div_mul_cancel _ hb)] lemma one_div_add_one_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : 1 / a + 1 / b = (a + b) / (a * b) := have a * b ≠ 0, from (division_ring.mul_ne_zero ha hb), by rw [add_comm, -(field.div_mul_left ha this), -(field.div_mul_right hb this), division_def, division_def, division_def, -right_distrib] lemma field.div_mul_div (a : α) {b : α} (c : α) {d : α} (hb : b ≠ 0) (hd : d ≠ 0) : (a / b) * (c / d) = (a * c) / (b * d) := begin simp [division_def], rw [mul_inv_eq hd hb, mul_comm d⁻¹] end lemma mul_div_mul_left (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : (c * a) / (c * b) = a / b := by rw [-(field.div_mul_div _ _ hc hb), div_self hc, one_mul] lemma mul_div_mul_right (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : (a * c) / (b * c) = a / b := by rw [mul_comm a, mul_comm b, mul_div_mul_left _ hb hc] lemma div_mul_eq_mul_div (a b c : α) : (b / c) * a = (b * a) / c := by simp [division_def] lemma field.div_mul_eq_mul_div_comm (a b : α) {c : α} (hc : c ≠ 0) : (b / c) * a = b * (a / c) := by rw [div_mul_eq_mul_div, -(one_mul c), -(field.div_mul_div _ _ (ne.symm zero_ne_one) hc), div_one, one_mul] lemma div_add_div (a : α) {b : α} (c : α) {d : α} (hb : b ≠ 0) (hd : d ≠ 0) : (a / b) + (c / d) = ((a * d) + (b * c)) / (b * d) := by rw [-(mul_div_mul_right _ hb hd), -(mul_div_mul_left _ hd hb), div_add_div_same] lemma div_sub_div (a : α) {b : α} (c : α) {d : α} (hb : b ≠ 0) (hd : d ≠ 0) : (a / b) - (c / d) = ((a * d) - (b * c)) / (b * d) := begin simp [sub_eq_add_neg], rw [neg_eq_neg_one_mul, -mul_div_assoc, div_add_div _ _ hb hd, -mul_assoc, mul_comm b, mul_assoc, -neg_eq_neg_one_mul] end lemma mul_eq_mul_of_div_eq_div (a : α) {b : α} (c : α) {d : α} (hb : b ≠ 0) (hd : d ≠ 0) (h : a / b = c / d) : a * d = c * b := by rw [-(mul_one (a*d)), mul_assoc, (mul_comm d), -mul_assoc, -(div_self hb), -(field.div_mul_eq_mul_div_comm _ _ hb), h, div_mul_eq_mul_div, div_mul_cancel _ hd] lemma field.one_div_div {a b : α} (ha : a ≠ 0) (hb : b ≠ 0) : 1 / (a / b) = b / a := have (a / b) * (b / a) = 1, from calc (a / b) * (b / a) = (a * b) / (b * a) : field.div_mul_div _ _ hb ha ... = (a * b) / (a * b) : by rw mul_comm ... = 1 : div_self (division_ring.mul_ne_zero ha hb), eq.symm (eq_one_div_of_mul_eq_one this) lemma field.div_div_eq_mul_div (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : a / (b / c) = (a * c) / b := by rw [div_eq_mul_one_div, field.one_div_div hb hc, -mul_div_assoc] lemma field.div_div_eq_div_mul (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : (a / b) / c = a / (b * c) := by rw [div_eq_mul_one_div, field.div_mul_div _ _ hb hc, mul_one] lemma field.div_div_div_div_eq (a : α) {b c d : α} (hb : b ≠ 0) (hc : c ≠ 0) (hd : d ≠ 0) : (a / b) / (c / d) = (a * d) / (b * c) := by rw [field.div_div_eq_mul_div _ hc hd, div_mul_eq_mul_div, field.div_div_eq_div_mul _ hb hc] lemma field.div_mul_eq_div_mul_one_div (a : α) {b c : α} (hb : b ≠ 0) (hc : c ≠ 0) : a / (b * c) = (a / b) * (1 / c) := by rw [-(field.div_div_eq_div_mul _ hb hc), -div_eq_mul_one_div] lemma eq_of_mul_eq_mul_of_nonzero_left {a b c : α} (h : a ≠ 0) (h₂ : a * b = a * c) : b = c := by rw [-one_mul b, -div_self h, div_mul_eq_mul_div, h₂, mul_div_cancel_left _ h] lemma eq_of_mul_eq_mul_of_nonzero_right {a b c : α} (h : c ≠ 0) (h2 : a * c = b * c) : a = b := by rw [-mul_one a, -div_self h, -mul_div_assoc, h2, mul_div_cancel _ h] end field class discrete_field (α : Type u) extends field α := (has_decidable_eq : decidable_eq α) (inv_zero : inv zero = zero) attribute [instance] discrete_field.has_decidable_eq section discrete_field variable [discrete_field α] -- many of the lemmas in discrete_field are the same as lemmas in field or division ring, -- but with fewer hypotheses since 0⁻¹ = 0 and equality is decidable. lemma discrete_field.eq_zero_or_eq_zero_of_mul_eq_zero (a b : α) (h : a * b = 0) : a = 0 ∨ b = 0 := decidable.by_cases (suppose a = 0, or.inl this) (suppose a ≠ 0, or.inr (by rw [-(one_mul b), -(inv_mul_cancel this), mul_assoc, h, mul_zero])) instance discrete_field.to_integral_domain [s : discrete_field α] : integral_domain α := {s with eq_zero_or_eq_zero_of_mul_eq_zero := discrete_field.eq_zero_or_eq_zero_of_mul_eq_zero} lemma inv_zero : 0⁻¹ = (0:α) := discrete_field.inv_zero α lemma one_div_zero : 1 / 0 = (0:α) := calc 1 / 0 = (1:α) * 0⁻¹ : by rw division_def ... = 1 * 0 : by rw inv_zero ... = (0:α) : by rw mul_zero lemma div_zero (a : α) : a / 0 = 0 := by rw [div_eq_mul_one_div, one_div_zero, mul_zero] lemma ne_zero_of_one_div_ne_zero {a : α} (h : 1 / a ≠ 0) : a ≠ 0 := assume ha : a = 0, begin rw [ha, one_div_zero] at h, contradiction end lemma eq_zero_of_one_div_eq_zero {a : α} (h : 1 / a = 0) : a = 0 := decidable.by_cases (assume ha, ha) (assume ha, false.elim ((one_div_ne_zero ha) h)) lemma one_div_mul_one_div' (a b : α) : (1 / a) * (1 / b) = 1 / (b * a) := decidable.by_cases (suppose a = 0, by rw [this, div_zero, zero_mul, mul_zero, div_zero]) (assume ha : a ≠ 0, decidable.by_cases (suppose b = 0, by rw [this, div_zero, mul_zero, zero_mul, div_zero]) (suppose b ≠ 0, division_ring.one_div_mul_one_div ha this)) lemma one_div_neg_eq_neg_one_div (a : α) : 1 / (- a) = - (1 / a) := decidable.by_cases (suppose a = 0, by rw [this, neg_zero, div_zero, neg_zero]) (suppose a ≠ 0, division_ring.one_div_neg_eq_neg_one_div this) lemma neg_div_neg_eq (a b : α) : (-a) / (-b) = a / b := decidable.by_cases (assume hb : b = 0, by rw [hb, neg_zero, div_zero, div_zero]) (assume hb : b ≠ 0, division_ring.neg_div_neg_eq _ hb) lemma one_div_one_div (a : α) : 1 / (1 / a) = a := decidable.by_cases (assume ha : a = 0, by rw [ha, div_zero, div_zero]) (assume ha : a ≠ 0, division_ring.one_div_one_div ha) lemma eq_of_one_div_eq_one_div {a b : α} (h : 1 / a = 1 / b) : a = b := decidable.by_cases (assume ha : a = 0, have hb : b = 0, from eq_zero_of_one_div_eq_zero (by rw [-h, ha, div_zero]), hb^.symm ▸ ha) (assume ha : a ≠ 0, have hb : b ≠ 0, from ne_zero_of_one_div_ne_zero (h ▸ (one_div_ne_zero ha)), division_ring.eq_of_one_div_eq_one_div ha hb h) lemma mul_inv' (a b : α) : (b * a)⁻¹ = a⁻¹ * b⁻¹ := decidable.by_cases (assume ha : a = 0, by rw [ha, mul_zero, inv_zero, zero_mul]) (assume ha : a ≠ 0, decidable.by_cases (assume hb : b = 0, by rw [hb, zero_mul, inv_zero, mul_zero]) (assume hb : b ≠ 0, mul_inv_eq ha hb)) -- the following are specifically for fields lemma one_div_mul_one_div (a b : α) : (1 / a) * (1 / b) = 1 / (a * b) := by rw [one_div_mul_one_div', mul_comm b] lemma div_mul_right {a : α} (b : α) (ha : a ≠ 0) : a / (a * b) = 1 / b := decidable.by_cases (assume hb : b = 0, by rw [hb, mul_zero, div_zero, div_zero]) (assume hb : b ≠ 0, field.div_mul_right hb (mul_ne_zero ha hb)) lemma div_mul_left (a : α) {b : α} (hb : b ≠ 0) : b / (a * b) = 1 / a := by rw [mul_comm a, div_mul_right _ hb] lemma div_mul_div (a b c d : α) : (a / b) * (c / d) = (a * c) / (b * d) := decidable.by_cases (assume hb : b = 0, by rw [hb, div_zero, zero_mul, zero_mul, div_zero]) (assume hb : b ≠ 0, decidable.by_cases (assume hd : d = 0, by rw [hd, div_zero, mul_zero, mul_zero, div_zero]) (assume hd : d ≠ 0, field.div_mul_div _ _ hb hd)) lemma mul_div_mul_left' (a b : α) {c : α} (hc : c ≠ 0) : (c * a) / (c * b) = a / b := decidable.by_cases (assume hb : b = 0, by rw [hb, mul_zero, div_zero, div_zero]) (assume hb : b ≠ 0, mul_div_mul_left _ hb hc) lemma mul_div_mul_right' (a b : α) {c : α} (hc : c ≠ 0) : (a * c) / (b * c) = a / b := by rw [mul_comm a, mul_comm b, (mul_div_mul_left' _ _ hc)] lemma div_mul_eq_mul_div_comm (a b c : α) : (b / c) * a = b * (a / c) := decidable.by_cases (assume hc : c = 0, by rw [hc, div_zero, zero_mul, div_zero, mul_zero]) (assume hc : c ≠ 0, field.div_mul_eq_mul_div_comm _ _ hc) lemma one_div_div (a b : α) : 1 / (a / b) = b / a := decidable.by_cases (assume ha : a = 0, by rw [ha, zero_div, div_zero, div_zero]) (assume ha : a ≠ 0, decidable.by_cases (assume hb : b = 0, by rw [hb, div_zero, zero_div, div_zero]) (assume hb : b ≠ 0, field.one_div_div ha hb)) lemma div_div_eq_mul_div (a b c : α) : a / (b / c) = (a * c) / b := by rw [div_eq_mul_one_div, one_div_div, -mul_div_assoc] lemma div_div_eq_div_mul (a b c : α) : (a / b) / c = a / (b * c) := by rw [div_eq_mul_one_div, div_mul_div, mul_one] lemma div_div_div_div_eq (a b c d : α) : (a / b) / (c / d) = (a * d) / (b * c) := by rw [div_div_eq_mul_div, div_mul_eq_mul_div, div_div_eq_div_mul] lemma div_helper {a : α} (b : α) (h : a ≠ 0) : (1 / (a * b)) * a = 1 / b := by rw [div_mul_eq_mul_div, one_mul, div_mul_right _ h] lemma div_mul_eq_div_mul_one_div (a b c : α) : a / (b * c) = (a / b) * (1 / c) := by rw [-div_div_eq_div_mul, -div_eq_mul_one_div] end discrete_field
4db3f01b5d7fbc7741092e57a305fb55ba4b272a
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/convex/basic_auto.lean
a4635554b995416ae5c7f20eeb8f62fade01125c
[]
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
52,396
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.set.intervals.ord_connected import Mathlib.data.set.intervals.image_preimage import Mathlib.data.complex.module import Mathlib.linear_algebra.affine_space.affine_map import Mathlib.algebra.module.ordered import Mathlib.PostPort universes u u_1 v v' w x u' namespace Mathlib /-! # Convex sets and functions on real vector spaces In a real vector space, we define the following objects and properties. * `segment x y` is the closed segment joining `x` and `y`. * A set `s` is `convex` if for any two points `x y ∈ s` it includes `segment x y`; * A function `f : E → β` is `convex_on` a set `s` if `s` is itself a convex set, and for any two points `x y ∈ s` the segment joining `(x, f x)` to `(y, f y)` is (non-strictly) above the graph of `f`; equivalently, `convex_on f s` means that the epigraph `{p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2}` is a convex set; * Center mass of a finite set of points with prescribed weights. * Convex hull of a set `s` is the minimal convex set that includes `s`. * Standard simplex `std_simplex ι [fintype ι]` is the intersection of the positive quadrant with the hyperplane `s.sum = 1` in the space `ι → ℝ`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex, and prove Jensen's inequality. Note: To define convexity for functions `f : E → β`, we need `β` to be an ordered vector space, defined using the instance `ordered_semimodule ℝ β`. ## Notations We use the following local notations: * `I = Icc (0:ℝ) 1`; * `[x, y] = segment x y`. They are defined using `local notation`, so they are not available outside of this file. -/ /-! ### Segment -/ /-- Segments in a vector space. -/ def segment {E : Type u} [add_comm_group E] [vector_space ℝ E] (x : E) (y : E) : set E := set_of fun (z : E) => ∃ (a : ℝ), ∃ (b : ℝ), ∃ (ha : 0 ≤ a), ∃ (hb : 0 ≤ b), ∃ (hab : a + b = 1), a • x + b • y = z theorem segment_symm {E : Type u} [add_comm_group E] [vector_space ℝ E] (x : E) (y : E) : segment x y = segment y x := sorry theorem left_mem_segment {E : Type u} [add_comm_group E] [vector_space ℝ E] (x : E) (y : E) : x ∈ segment x y := sorry theorem right_mem_segment {E : Type u} [add_comm_group E] [vector_space ℝ E] (x : E) (y : E) : y ∈ segment x y := segment_symm y x ▸ left_mem_segment y x theorem segment_same {E : Type u} [add_comm_group E] [vector_space ℝ E] (x : E) : segment x x = singleton x := sorry theorem segment_eq_image {E : Type u} [add_comm_group E] [vector_space ℝ E] (x : E) (y : E) : segment x y = (fun (θ : ℝ) => (1 - θ) • x + θ • y) '' set.Icc 0 1 := sorry theorem segment_eq_image' {E : Type u} [add_comm_group E] [vector_space ℝ E] (x : E) (y : E) : segment x y = (fun (θ : ℝ) => x + θ • (y - x)) '' set.Icc 0 1 := sorry theorem segment_eq_image₂ {E : Type u} [add_comm_group E] [vector_space ℝ E] (x : E) (y : E) : segment x y = (fun (p : ℝ × ℝ) => prod.fst p • x + prod.snd p • y) '' set_of fun (p : ℝ × ℝ) => 0 ≤ prod.fst p ∧ 0 ≤ prod.snd p ∧ prod.fst p + prod.snd p = 1 := sorry theorem segment_eq_Icc {a : ℝ} {b : ℝ} (h : a ≤ b) : segment a b = set.Icc a b := sorry theorem segment_eq_Icc' (a : ℝ) (b : ℝ) : segment a b = set.Icc (min a b) (max a b) := sorry theorem segment_eq_interval (a : ℝ) (b : ℝ) : segment a b = set.interval a b := segment_eq_Icc' a b theorem mem_segment_translate {E : Type u} [add_comm_group E] [vector_space ℝ E] (a : E) {x : E} {b : E} {c : E} : a + x ∈ segment (a + b) (a + c) ↔ x ∈ segment b c := sorry theorem segment_translate_preimage {E : Type u} [add_comm_group E] [vector_space ℝ E] (a : E) (b : E) (c : E) : (fun (x : E) => a + x) ⁻¹' segment (a + b) (a + c) = segment b c := set.ext fun (x : E) => mem_segment_translate a theorem segment_translate_image {E : Type u} [add_comm_group E] [vector_space ℝ E] (a : E) (b : E) (c : E) : (fun (x : E) => a + x) '' segment b c = segment (a + b) (a + c) := Eq.subst (segment_translate_preimage a b c) (set.image_preimage_eq (segment (a + b) (a + c))) (add_left_surjective a) /-! ### Convexity of sets -/ /-- Convexity of sets. -/ def convex {E : Type u} [add_comm_group E] [vector_space ℝ E] (s : set E) := ∀ {x y : E}, x ∈ s → y ∈ s → ∀ {a b : ℝ}, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s theorem convex_iff_forall_pos {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} : convex s ↔ ∀ {x y : E}, x ∈ s → y ∈ s → ∀ {a b : ℝ}, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := sorry theorem convex_iff_segment_subset {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} : convex s ↔ ∀ {x y : E}, x ∈ s → y ∈ s → segment x y ⊆ s := sorry theorem convex.segment_subset {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (h : convex s) {x : E} {y : E} (hx : x ∈ s) (hy : y ∈ s) : segment x y ⊆ s := iff.mp convex_iff_segment_subset h x y hx hy /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ theorem convex_iff_pointwise_add_subset {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} : convex s ↔ ∀ {a b : ℝ}, 0 ≤ a → 0 ≤ b → a + b = 1 → a • s + b • s ⊆ s := sorry /-- Alternative definition of set convexity, using division. -/ theorem convex_iff_div {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} : convex s ↔ ∀ {x y : E}, x ∈ s → y ∈ s → ∀ {a b : ℝ}, 0 ≤ a → 0 ≤ b → 0 < a + b → (a / (a + b)) • x + (b / (a + b)) • y ∈ s := sorry /-! ### Examples of convex sets -/ theorem convex_empty {E : Type u} [add_comm_group E] [vector_space ℝ E] : convex ∅ := sorry theorem convex_singleton {E : Type u} [add_comm_group E] [vector_space ℝ E] (c : E) : convex (singleton c) := sorry theorem convex_univ {E : Type u} [add_comm_group E] [vector_space ℝ E] : convex set.univ := fun (_x _x_1 : E) (_x : _x ∈ set.univ) (_x : _x_1 ∈ set.univ) (_x _x_2 : ℝ) (_x_3 : 0 ≤ _x) (_x_4 : 0 ≤ _x_2) (_x : _x + _x_2 = 1) => trivial theorem convex.inter {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : set E} (hs : convex s) (ht : convex t) : convex (s ∩ t) := fun (x y : E) (hx : x ∈ s ∩ t) (hy : y ∈ s ∩ t) (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) => { left := hs (and.left hx) (and.left hy) ha hb hab, right := ht (and.right hx) (and.right hy) ha hb hab } theorem convex_sInter {E : Type u} [add_comm_group E] [vector_space ℝ E] {S : set (set E)} (h : ∀ (s : set E), s ∈ S → convex s) : convex (⋂₀S) := fun (x y : E) (hx : x ∈ ⋂₀S) (hy : y ∈ ⋂₀S) (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) (s : set E) (hs : s ∈ S) => h s hs (hx s hs) (hy s hs) ha hb hab theorem convex_Inter {E : Type u} [add_comm_group E] [vector_space ℝ E] {ι : Sort u_1} {s : ι → set E} (h : ∀ (i : ι), convex (s i)) : convex (set.Inter fun (i : ι) => s i) := Eq.subst (set.sInter_range s) convex_sInter (iff.mpr set.forall_range_iff h) theorem convex.prod {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {s : set E} {t : set F} (hs : convex s) (ht : convex t) : convex (set.prod s t) := sorry theorem convex.combo_to_vadd {E : Type u} [add_comm_group E] [vector_space ℝ E] {a : ℝ} {b : ℝ} {x : E} {y : E} (h : a + b = 1) : a • x + b • y = b • (y - x) + x := sorry /-- Applying an affine map to an affine combination of two points yields an affine combination of the images. -/ theorem convex.combo_affine_apply {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {a : ℝ} {b : ℝ} {x : E} {y : E} {f : affine_map ℝ E F} (h : a + b = 1) : coe_fn f (a • x + b • y) = a • coe_fn f x + b • coe_fn f y := sorry /-- The preimage of a convex set under an affine map is convex. -/ theorem convex.affine_preimage {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] (f : affine_map ℝ E F) {s : set F} (hs : convex s) : convex (⇑f ⁻¹' s) := sorry /-- The image of a convex set under an affine map is convex. -/ theorem convex.affine_image {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] (f : affine_map ℝ E F) {s : set E} (hs : convex s) : convex (⇑f '' s) := sorry theorem convex.linear_image {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {s : set E} (hs : convex s) (f : linear_map ℝ E F) : convex (⇑f '' s) := convex.affine_image (linear_map.to_affine_map f) hs theorem convex.is_linear_image {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {s : set E} (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (f '' s) := convex.linear_image hs (is_linear_map.mk' f hf) theorem convex.linear_preimage {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {s : set F} (hs : convex s) (f : linear_map ℝ E F) : convex (⇑f ⁻¹' s) := convex.affine_preimage (linear_map.to_affine_map f) hs theorem convex.is_linear_preimage {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {s : set F} (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (f ⁻¹' s) := convex.linear_preimage hs (is_linear_map.mk' f hf) theorem convex.neg {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (hs : convex s) : convex ((fun (z : E) => -z) '' s) := convex.is_linear_image hs is_linear_map.is_linear_map_neg theorem convex.neg_preimage {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (hs : convex s) : convex ((fun (z : E) => -z) ⁻¹' s) := convex.is_linear_preimage hs is_linear_map.is_linear_map_neg theorem convex.smul {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (c : ℝ) (hs : convex s) : convex (c • s) := convex.linear_image hs (coe_fn (linear_map.lsmul ℝ E) c) theorem convex.smul_preimage {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (c : ℝ) (hs : convex s) : convex ((fun (z : E) => c • z) ⁻¹' s) := convex.linear_preimage hs (coe_fn (linear_map.lsmul ℝ E) c) theorem convex.add {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : set E} (hs : convex s) (ht : convex t) : convex (s + t) := eq.mpr (id (Eq._oldrec (Eq.refl (convex (s + t))) (Eq.symm set.add_image_prod))) (convex.is_linear_image (convex.prod hs ht) is_linear_map.is_linear_map_add) theorem convex.sub {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : set E} (hs : convex s) (ht : convex t) : convex ((fun (x : E × E) => prod.fst x - prod.snd x) '' set.prod s t) := convex.is_linear_image (convex.prod hs ht) is_linear_map.is_linear_map_sub theorem convex.translate {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (hs : convex s) (z : E) : convex ((fun (x : E) => z + x) '' s) := convex.affine_image (affine_map.const ℝ E z +ᵥ affine_map.id ℝ E) hs /-- The translation of a convex set is also convex. -/ theorem convex.translate_preimage_right {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (hs : convex s) (a : E) : convex ((fun (z : E) => a + z) ⁻¹' s) := convex.affine_preimage (affine_map.const ℝ E a +ᵥ affine_map.id ℝ E) hs /-- The translation of a convex set is also convex. -/ theorem convex.translate_preimage_left {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (hs : convex s) (a : E) : convex ((fun (z : E) => z + a) ⁻¹' s) := sorry theorem convex.affinity {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (hs : convex s) (z : E) (c : ℝ) : convex ((fun (x : E) => z + c • x) '' s) := convex.affine_image (affine_map.const ℝ E z +ᵥ c • affine_map.id ℝ E) hs theorem real.convex_iff_ord_connected {s : set ℝ} : convex s ↔ set.ord_connected s := sorry theorem convex.ord_connected {s : set ℝ} : convex s → set.ord_connected s := iff.mp real.convex_iff_ord_connected theorem convex_Iio (r : ℝ) : convex (set.Iio r) := set.ord_connected.convex set.ord_connected_Iio theorem convex_Ioi (r : ℝ) : convex (set.Ioi r) := set.ord_connected.convex set.ord_connected_Ioi theorem convex_Iic (r : ℝ) : convex (set.Iic r) := set.ord_connected.convex set.ord_connected_Iic theorem convex_Ici (r : ℝ) : convex (set.Ici r) := set.ord_connected.convex set.ord_connected_Ici theorem convex_Ioo (r : ℝ) (s : ℝ) : convex (set.Ioo r s) := set.ord_connected.convex set.ord_connected_Ioo theorem convex_Ico (r : ℝ) (s : ℝ) : convex (set.Ico r s) := set.ord_connected.convex set.ord_connected_Ico theorem convex_Ioc (r : ℝ) (s : ℝ) : convex (set.Ioc r s) := set.ord_connected.convex set.ord_connected_Ioc theorem convex_Icc (r : ℝ) (s : ℝ) : convex (set.Icc r s) := set.ord_connected.convex set.ord_connected_Icc theorem convex_interval (r : ℝ) (s : ℝ) : convex (set.interval r s) := set.ord_connected.convex set.ord_connected_interval theorem convex_segment {E : Type u} [add_comm_group E] [vector_space ℝ E] (a : E) (b : E) : convex (segment a b) := sorry theorem convex_halfspace_lt {E : Type u} [add_comm_group E] [vector_space ℝ E] {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex (set_of fun (w : E) => f w < r) := convex.is_linear_preimage (convex_Iio r) h theorem convex_halfspace_le {E : Type u} [add_comm_group E] [vector_space ℝ E] {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex (set_of fun (w : E) => f w ≤ r) := convex.is_linear_preimage (convex_Iic r) h theorem convex_halfspace_gt {E : Type u} [add_comm_group E] [vector_space ℝ E] {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex (set_of fun (w : E) => r < f w) := convex.is_linear_preimage (convex_Ioi r) h theorem convex_halfspace_ge {E : Type u} [add_comm_group E] [vector_space ℝ E] {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex (set_of fun (w : E) => r ≤ f w) := convex.is_linear_preimage (convex_Ici r) h theorem convex_hyperplane {E : Type u} [add_comm_group E] [vector_space ℝ E] {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex (set_of fun (w : E) => f w = r) := id (eq.mpr (id (Eq._oldrec (Eq.refl (convex (f ⁻¹' set_of fun (p : ℝ) => p = r))) set.set_of_eq_eq_singleton)) (convex.is_linear_preimage (convex_singleton r) h)) theorem convex_halfspace_re_lt (r : ℝ) : convex (set_of fun (c : ℂ) => complex.re c < r) := convex_halfspace_lt (is_linear_map.mk complex.add_re complex.smul_re) r theorem convex_halfspace_re_le (r : ℝ) : convex (set_of fun (c : ℂ) => complex.re c ≤ r) := convex_halfspace_le (is_linear_map.mk complex.add_re complex.smul_re) r theorem convex_halfspace_re_gt (r : ℝ) : convex (set_of fun (c : ℂ) => r < complex.re c) := convex_halfspace_gt (is_linear_map.mk complex.add_re complex.smul_re) r theorem convex_halfspace_re_lge (r : ℝ) : convex (set_of fun (c : ℂ) => r ≤ complex.re c) := convex_halfspace_ge (is_linear_map.mk complex.add_re complex.smul_re) r theorem convex_halfspace_im_lt (r : ℝ) : convex (set_of fun (c : ℂ) => complex.im c < r) := convex_halfspace_lt (is_linear_map.mk complex.add_im complex.smul_im) r theorem convex_halfspace_im_le (r : ℝ) : convex (set_of fun (c : ℂ) => complex.im c ≤ r) := convex_halfspace_le (is_linear_map.mk complex.add_im complex.smul_im) r theorem convex_halfspace_im_gt (r : ℝ) : convex (set_of fun (c : ℂ) => r < complex.im c) := convex_halfspace_gt (is_linear_map.mk complex.add_im complex.smul_im) r theorem convex_halfspace_im_lge (r : ℝ) : convex (set_of fun (c : ℂ) => r ≤ complex.im c) := convex_halfspace_ge (is_linear_map.mk complex.add_im complex.smul_im) r /-! ### Convex combinations in intervals -/ theorem convex.combo_self {α : Type v'} [linear_ordered_field α] (a : α) {x : α} {y : α} (h : x + y = 1) : a = x * a + y * a := sorry /-- If `x` is in an `Ioo`, it can be expressed as a convex combination of the endpoints. -/ theorem convex.mem_Ioo {α : Type v'} [linear_ordered_field α] {a : α} {b : α} {x : α} (h : a < b) : x ∈ set.Ioo a b ↔ ∃ (x_a : α), ∃ (x_b : α), 0 < x_a ∧ 0 < x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := sorry /-- If `x` is in an `Ioc`, it can be expressed as a convex combination of the endpoints. -/ theorem convex.mem_Ioc {α : Type v'} [linear_ordered_field α] {a : α} {b : α} {x : α} (h : a < b) : x ∈ set.Ioc a b ↔ ∃ (x_a : α), ∃ (x_b : α), 0 ≤ x_a ∧ 0 < x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := sorry /-- If `x` is in an `Ico`, it can be expressed as a convex combination of the endpoints. -/ theorem convex.mem_Ico {α : Type v'} [linear_ordered_field α] {a : α} {b : α} {x : α} (h : a < b) : x ∈ set.Ico a b ↔ ∃ (x_a : α), ∃ (x_b : α), 0 < x_a ∧ 0 ≤ x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := sorry /-- If `x` is in an `Icc`, it can be expressed as a convex combination of the endpoints. -/ theorem convex.mem_Icc {α : Type v'} [linear_ordered_field α] {a : α} {b : α} {x : α} (h : a ≤ b) : x ∈ set.Icc a b ↔ ∃ (x_a : α), ∃ (x_b : α), 0 ≤ x_a ∧ 0 ≤ x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := sorry theorem submodule.convex {E : Type u} [add_comm_group E] [vector_space ℝ E] (K : submodule ℝ E) : convex ↑K := id fun (x y : E) (ᾰ : x ∈ ↑K) (ᾰ_1 : y ∈ ↑K) (a b : ℝ) (ᾰ_2 : 0 ≤ a) (ᾰ_3 : 0 ≤ b) (ᾰ_4 : a + b = 1) => submodule.add_mem K (submodule.smul_mem K a ᾰ) (submodule.smul_mem K b ᾰ_1) theorem subspace.convex {E : Type u} [add_comm_group E] [vector_space ℝ E] (K : subspace ℝ E) : convex ↑K := submodule.convex K /-! ### Convex and concave functions -/ /-- Convexity of functions -/ def convex_on {E : Type u} [add_comm_group E] [vector_space ℝ E] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] (s : set E) (f : E → β) := convex s ∧ ∀ {x y : E}, x ∈ s → y ∈ s → ∀ {a b : ℝ}, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y /-- Concavity of functions -/ def concave_on {E : Type u} [add_comm_group E] [vector_space ℝ E] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] (s : set E) (f : E → β) := convex s ∧ ∀ {x y : E}, x ∈ s → y ∈ s → ∀ {a b : ℝ}, 0 ≤ a → 0 ≤ b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y) /-- A function `f` is concave iff `-f` is convex. -/ @[simp] theorem neg_convex_on_iff {E : Type u} [add_comm_group E] [vector_space ℝ E] {γ : Type u_1} [ordered_add_comm_group γ] [semimodule ℝ γ] (s : set E) (f : E → γ) : convex_on s (-f) ↔ concave_on s f := sorry /-- A function `f` is concave iff `-f` is convex. -/ @[simp] theorem neg_concave_on_iff {E : Type u} [add_comm_group E] [vector_space ℝ E] {γ : Type u_1} [ordered_add_comm_group γ] [semimodule ℝ γ] (s : set E) (f : E → γ) : concave_on s (-f) ↔ convex_on s f := eq.mpr (id (Eq._oldrec (Eq.refl (concave_on s (-f) ↔ convex_on s f)) (Eq.symm (propext (neg_convex_on_iff s (-f)))))) (eq.mpr (id (Eq._oldrec (Eq.refl (convex_on s ( --f) ↔ convex_on s f)) (neg_neg f))) (iff.refl (convex_on s f))) theorem convex_on_id {s : set ℝ} (hs : convex s) : convex_on s id := sorry theorem concave_on_id {s : set ℝ} (hs : convex s) : concave_on s id := sorry theorem convex_on_const {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] (c : β) (hs : convex s) : convex_on s fun (x : E) => c := sorry theorem concave_on_const {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] (c : β) (hs : convex s) : concave_on s fun (x : E) => c := convex_on_const c hs theorem convex_on_iff_div {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} : convex_on s f ↔ convex s ∧ ∀ {x y : E}, x ∈ s → y ∈ s → ∀ {a b : ℝ}, 0 ≤ a → 0 ≤ b → 0 < a + b → f ((a / (a + b)) • x + (b / (a + b)) • y) ≤ (a / (a + b)) • f x + (b / (a + b)) • f y := sorry theorem concave_on_iff_div {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} : concave_on s f ↔ convex s ∧ ∀ {x y : E}, x ∈ s → y ∈ s → ∀ {a b : ℝ}, 0 ≤ a → 0 ≤ b → 0 < a + b → (a / (a + b)) • f x + (b / (a + b)) • f y ≤ f ((a / (a + b)) • x + (b / (a + b)) • y) := convex_on_iff_div /-- For a function on a convex set in a linear ordered space, in order to prove that it is convex it suffices to verify the inequality `f (a • x + b • y) ≤ a • f x + b • f y` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ theorem linear_order.convex_on_of_lt {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} [linear_order E] (hs : convex s) (hf : ∀ {x y : E}, x ∈ s → y ∈ s → x < y → ∀ {a b : ℝ}, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y) : convex_on s f := sorry /-- For a function on a convex set in a linear ordered space, in order to prove that it is concave it suffices to verify the inequality `a • f x + b • f y ≤ f (a • x + b • y)` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ theorem linear_order.concave_on_of_lt {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} [linear_order E] (hs : convex s) (hf : ∀ {x y : E}, x ∈ s → y ∈ s → x < y → ∀ {a b : ℝ}, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y)) : concave_on s f := linear_order.convex_on_of_lt hs hf /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is convex on `D`. This way of proving convexity of a function is used in the proof of convexity of a function with a monotone derivative. -/ theorem convex_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) : convex_on s f := sorry /-- For a function `f` defined on a subset `D` of `ℝ`, if `f` is convex on `D`, then for any three points `x<y<z`, the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`. -/ theorem convex_on.slope_mono_adjacent {s : set ℝ} {f : ℝ → ℝ} (hf : convex_on s f) {x : ℝ} {y : ℝ} {z : ℝ} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f y - f x) / (y - x) ≤ (f z - f y) / (z - y) := sorry /-- For a function `f` defined on a convex subset `D` of `ℝ`, `f` is convex on `D` iff for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`. -/ theorem convex_on_real_iff_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} : convex_on s f ↔ ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y) := { mp := convex_on.slope_mono_adjacent, mpr := convex_on_real_of_slope_mono_adjacent hs } /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is concave on `D`. -/ theorem concave_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) : concave_on s f := sorry /-- For a function `f` defined on a subset `D` of `ℝ`, if `f` is concave on `D`, then for any three points `x<y<z`, the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`. -/ theorem concave_on.slope_mono_adjacent {s : set ℝ} {f : ℝ → ℝ} (hf : concave_on s f) {x : ℝ} {y : ℝ} {z : ℝ} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f z - f y) / (z - y) ≤ (f y - f x) / (y - x) := sorry /-- For a function `f` defined on a convex subset `D` of `ℝ`, `f` is concave on `D` iff for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`. -/ theorem concave_on_real_iff_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} : concave_on s f ↔ ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x) := { mp := concave_on.slope_mono_adjacent, mpr := concave_on_real_of_slope_mono_adjacent hs } theorem convex_on.subset {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {t : set E} {f : E → β} (h_convex_on : convex_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : convex_on s f := { left := h_convex, right := fun (x y : E) (hx : x ∈ s) (hy : y ∈ s) => and.right h_convex_on x y (h_subset hx) (h_subset hy) } theorem concave_on.subset {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {t : set E} {f : E → β} (h_concave_on : concave_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : concave_on s f := convex_on.subset h_concave_on h_subset h_convex theorem convex_on.add {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} {g : E → β} (hf : convex_on s f) (hg : convex_on s g) : convex_on s fun (x : E) => f x + g x := sorry theorem concave_on.add {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} {g : E → β} (hf : concave_on s f) (hg : concave_on s g) : concave_on s fun (x : E) => f x + g x := convex_on.add hf hg theorem convex_on.smul {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] [ordered_semimodule ℝ β] {f : E → β} {c : ℝ} (hc : 0 ≤ c) (hf : convex_on s f) : convex_on s fun (x : E) => c • f x := sorry theorem concave_on.smul {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] [ordered_semimodule ℝ β] {f : E → β} {c : ℝ} (hc : 0 ≤ c) (hf : concave_on s f) : concave_on s fun (x : E) => c • f x := convex_on.smul hc hf /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ theorem convex_on.le_on_segment' {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} {x : E} {y : E} {a : ℝ} {b : ℝ} (hf : convex_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : f (a • x + b • y) ≤ max (f x) (f y) := sorry /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ theorem concave_on.le_on_segment' {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} {x : E} {y : E} {a : ℝ} {b : ℝ} (hf : concave_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : min (f x) (f y) ≤ f (a • x + b • y) := convex_on.le_on_segment' hf hx hy ha hb hab /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ theorem convex_on.le_on_segment {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) {x : E} {y : E} {z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ segment x y) : f z ≤ max (f x) (f y) := sorry /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ theorem concave_on.le_on_segment {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) {x : E} {y : E} {z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ segment x y) : min (f x) (f y) ≤ f z := convex_on.le_on_segment hf hx hy hz theorem convex_on.convex_le {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] [ordered_semimodule ℝ β] {f : E → β} (hf : convex_on s f) (r : β) : convex (has_sep.sep (fun (x : E) => f x ≤ r) s) := sorry theorem concave_on.concave_le {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] [ordered_semimodule ℝ β] {f : E → β} (hf : concave_on s f) (r : β) : convex (has_sep.sep (fun (x : E) => r ≤ f x) s) := convex_on.convex_le hf r theorem convex_on.convex_lt {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [ordered_cancel_add_comm_monoid γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) (r : γ) : convex (has_sep.sep (fun (x : E) => f x < r) s) := sorry theorem concave_on.convex_lt {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [ordered_cancel_add_comm_monoid γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) (r : γ) : convex (has_sep.sep (fun (x : E) => r < f x) s) := convex_on.convex_lt hf r theorem convex_on.convex_epigraph {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) : convex (set_of fun (p : E × γ) => prod.fst p ∈ s ∧ f (prod.fst p) ≤ prod.snd p) := sorry theorem concave_on.convex_hypograph {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) : convex (set_of fun (p : E × γ) => prod.fst p ∈ s ∧ prod.snd p ≤ f (prod.fst p)) := convex_on.convex_epigraph hf theorem convex_on_iff_convex_epigraph {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} : convex_on s f ↔ convex (set_of fun (p : E × γ) => prod.fst p ∈ s ∧ f (prod.fst p) ≤ prod.snd p) := sorry theorem concave_on_iff_convex_hypograph {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {γ : Type u_1} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} : concave_on s f ↔ convex (set_of fun (p : E × γ) => prod.fst p ∈ s ∧ prod.snd p ≤ f (prod.fst p)) := convex_on_iff_convex_epigraph /-- If a function is convex on `s`, it remains convex when precomposed by an affine map. -/ theorem convex_on.comp_affine_map {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : F → β} (g : affine_map ℝ E F) {s : set F} (hf : convex_on s f) : convex_on (⇑g ⁻¹' s) (f ∘ ⇑g) := sorry /-- If a function is concave on `s`, it remains concave when precomposed by an affine map. -/ theorem concave_on.comp_affine_map {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : F → β} (g : affine_map ℝ E F) {s : set F} (hf : concave_on s f) : concave_on (⇑g ⁻¹' s) (f ∘ ⇑g) := convex_on.comp_affine_map g hf /-- If `g` is convex on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ theorem convex_on.comp_linear_map {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {g : F → β} {s : set F} (hg : convex_on s g) (f : linear_map ℝ E F) : convex_on (⇑f ⁻¹' s) (g ∘ ⇑f) := convex_on.comp_affine_map (linear_map.to_affine_map f) hg /-- If `g` is concave on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ theorem concave_on.comp_linear_map {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {g : F → β} {s : set F} (hg : concave_on s g) (f : linear_map ℝ E F) : concave_on (⇑f ⁻¹' s) (g ∘ ⇑f) := concave_on.comp_affine_map (linear_map.to_affine_map f) hg /-- If a function is convex on `s`, it remains convex after a translation. -/ theorem convex_on.translate_right {E : Type u} [add_comm_group E] [vector_space ℝ E] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} {s : set E} {a : E} (hf : convex_on s f) : convex_on ((fun (z : E) => a + z) ⁻¹' s) (f ∘ fun (z : E) => a + z) := convex_on.comp_affine_map (affine_map.const ℝ E a +ᵥ affine_map.id ℝ E) hf /-- If a function is concave on `s`, it remains concave after a translation. -/ theorem concave_on.translate_right {E : Type u} [add_comm_group E] [vector_space ℝ E] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} {s : set E} {a : E} (hf : concave_on s f) : concave_on ((fun (z : E) => a + z) ⁻¹' s) (f ∘ fun (z : E) => a + z) := concave_on.comp_affine_map (affine_map.const ℝ E a +ᵥ affine_map.id ℝ E) hf /-- If a function is convex on `s`, it remains convex after a translation. -/ theorem convex_on.translate_left {E : Type u} [add_comm_group E] [vector_space ℝ E] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} {s : set E} {a : E} (hf : convex_on s f) : convex_on ((fun (z : E) => a + z) ⁻¹' s) (f ∘ fun (z : E) => z + a) := sorry /-- If a function is concave on `s`, it remains concave after a translation. -/ theorem concave_on.translate_left {E : Type u} [add_comm_group E] [vector_space ℝ E] {β : Type u_1} [ordered_add_comm_monoid β] [semimodule ℝ β] {f : E → β} {s : set E} {a : E} (hf : concave_on s f) : concave_on ((fun (z : E) => a + z) ⁻¹' s) (f ∘ fun (z : E) => z + a) := sorry /-! ### Center of mass -/ /-- Center of mass of a finite collection of points with prescribed weights. Note that we require neither `0 ≤ w i` nor `∑ w = 1`. -/ def finset.center_mass {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] (t : finset ι) (w : ι → ℝ) (z : ι → E) : E := (finset.sum t fun (i : ι) => w i)⁻¹ • finset.sum t fun (i : ι) => w i • z i theorem finset.center_mass_empty {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] (w : ι → ℝ) (z : ι → E) : finset.center_mass ∅ w z = 0 := sorry theorem finset.center_mass_pair {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] (i : ι) (j : ι) (w : ι → ℝ) (z : ι → E) (hne : i ≠ j) : finset.center_mass (insert i (singleton j)) w z = (w i / (w i + w j)) • z i + (w j / (w i + w j)) • z j := sorry theorem finset.center_mass_insert {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] (i : ι) (t : finset ι) {w : ι → ℝ} (z : ι → E) (ha : ¬i ∈ t) (hw : (finset.sum t fun (j : ι) => w j) ≠ 0) : finset.center_mass (insert i t) w z = (w i / (w i + finset.sum t fun (j : ι) => w j)) • z i + ((finset.sum t fun (j : ι) => w j) / (w i + finset.sum t fun (j : ι) => w j)) • finset.center_mass t w z := sorry theorem finset.center_mass_singleton {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] (i : ι) {w : ι → ℝ} (z : ι → E) (hw : w i ≠ 0) : finset.center_mass (singleton i) w z = z i := sorry theorem finset.center_mass_eq_of_sum_1 {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] (t : finset ι) {w : ι → ℝ} (z : ι → E) (hw : (finset.sum t fun (i : ι) => w i) = 1) : finset.center_mass t w z = finset.sum t fun (i : ι) => w i • z i := sorry theorem finset.center_mass_smul {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] (c : ℝ) (t : finset ι) {w : ι → ℝ} (z : ι → E) : (finset.center_mass t w fun (i : ι) => c • z i) = c • finset.center_mass t w z := sorry /-- A convex combination of two centers of mass is a center of mass as well. This version deals with two different index types. -/ theorem finset.center_mass_segment' {E : Type u} {ι : Type w} {ι' : Type x} [add_comm_group E] [vector_space ℝ E] (s : finset ι) (t : finset ι') (ws : ι → ℝ) (zs : ι → E) (wt : ι' → ℝ) (zt : ι' → E) (hws : (finset.sum s fun (i : ι) => ws i) = 1) (hwt : (finset.sum t fun (i : ι') => wt i) = 1) (a : ℝ) (b : ℝ) (hab : a + b = 1) : a • finset.center_mass s ws zs + b • finset.center_mass t wt zt = finset.center_mass (finset.map function.embedding.inl s ∪ finset.map function.embedding.inr t) (sum.elim (fun (i : ι) => a * ws i) fun (j : ι') => b * wt j) (sum.elim zs zt) := sorry /-- A convex combination of two centers of mass is a center of mass as well. This version works if two centers of mass share the set of original points. -/ theorem finset.center_mass_segment {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] (s : finset ι) (w₁ : ι → ℝ) (w₂ : ι → ℝ) (z : ι → E) (hw₁ : (finset.sum s fun (i : ι) => w₁ i) = 1) (hw₂ : (finset.sum s fun (i : ι) => w₂ i) = 1) (a : ℝ) (b : ℝ) (hab : a + b = 1) : a • finset.center_mass s w₁ z + b • finset.center_mass s w₂ z = finset.center_mass s (fun (i : ι) => a * w₁ i + b * w₂ i) z := sorry theorem finset.center_mass_ite_eq {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] (i : ι) (t : finset ι) (z : ι → E) (hi : i ∈ t) : finset.center_mass t (fun (j : ι) => ite (i = j) 1 0) z = z i := sorry theorem finset.center_mass_subset {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] {t : finset ι} {w : ι → ℝ} (z : ι → E) {t' : finset ι} (ht : t ⊆ t') (h : ∀ (i : ι), i ∈ t' → ¬i ∈ t → w i = 0) : finset.center_mass t w z = finset.center_mass t' w z := sorry theorem finset.center_mass_filter_ne_zero {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] {t : finset ι} {w : ι → ℝ} (z : ι → E) : finset.center_mass (finset.filter (fun (i : ι) => w i ≠ 0) t) w z = finset.center_mass t w z := sorry /-- The center of mass of a finite subset of a convex set belongs to the set provided that all weights are non-negative, and the total weight is positive. -/ theorem convex.center_mass_mem {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : finset ι} {w : ι → ℝ} {z : ι → E} (hs : convex s) : (∀ (i : ι), i ∈ t → 0 ≤ w i) → (0 < finset.sum t fun (i : ι) => w i) → (∀ (i : ι), i ∈ t → z i ∈ s) → finset.center_mass t w z ∈ s := sorry theorem convex.sum_mem {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : finset ι} {w : ι → ℝ} {z : ι → E} (hs : convex s) (h₀ : ∀ (i : ι), i ∈ t → 0 ≤ w i) (h₁ : (finset.sum t fun (i : ι) => w i) = 1) (hz : ∀ (i : ι), i ∈ t → z i ∈ s) : (finset.sum t fun (i : ι) => w i • z i) ∈ s := sorry theorem convex_iff_sum_mem {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} : convex s ↔ ∀ (t : finset E) (w : E → ℝ), (∀ (i : E), i ∈ t → 0 ≤ w i) → (finset.sum t fun (i : E) => w i) = 1 → (∀ (x : E), x ∈ t → x ∈ s) → (finset.sum t fun (x : E) => w x • x) ∈ s := sorry /-- Jensen's inequality, `finset.center_mass` version. -/ theorem convex_on.map_center_mass_le {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : finset ι} {w : ι → ℝ} {z : ι → E} {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ (i : ι), i ∈ t → 0 ≤ w i) (hpos : 0 < finset.sum t fun (i : ι) => w i) (hmem : ∀ (i : ι), i ∈ t → z i ∈ s) : f (finset.center_mass t w z) ≤ finset.center_mass t w (f ∘ z) := sorry /-- Jensen's inequality, `finset.sum` version. -/ theorem convex_on.map_sum_le {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : finset ι} {w : ι → ℝ} {z : ι → E} {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ (i : ι), i ∈ t → 0 ≤ w i) (h₁ : (finset.sum t fun (i : ι) => w i) = 1) (hmem : ∀ (i : ι), i ∈ t → z i ∈ s) : f (finset.sum t fun (i : ι) => w i • z i) ≤ finset.sum t fun (i : ι) => w i * f (z i) := sorry /-- If a function `f` is convex on `s` takes value `y` at the center of mass of some points `z i ∈ s`, then for some `i` we have `y ≤ f (z i)`. -/ theorem convex_on.exists_ge_of_center_mass {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : finset ι} {w : ι → ℝ} {z : ι → E} {f : E → ℝ} (h : convex_on s f) (hw₀ : ∀ (i : ι), i ∈ t → 0 ≤ w i) (hws : 0 < finset.sum t fun (i : ι) => w i) (hz : ∀ (i : ι), i ∈ t → z i ∈ s) : ∃ (i : ι), ∃ (H : i ∈ t), f (finset.center_mass t w z) ≤ f (z i) := sorry /-! ### Convex hull -/ /-- The convex hull of a set `s` is the minimal convex set that includes `s`. -/ def convex_hull {E : Type u} [add_comm_group E] [vector_space ℝ E] (s : set E) : set E := set.Inter fun (t : set E) => set.Inter fun (hst : s ⊆ t) => set.Inter fun (ht : convex t) => t theorem subset_convex_hull {E : Type u} [add_comm_group E] [vector_space ℝ E] (s : set E) : s ⊆ convex_hull s := set.subset_Inter fun (t : set E) => set.subset_Inter fun (hst : s ⊆ t) => set.subset_Inter fun (ht : convex t) => hst theorem convex_convex_hull {E : Type u} [add_comm_group E] [vector_space ℝ E] (s : set E) : convex (convex_hull s) := convex_Inter fun (t : set E) => convex_Inter fun (ht : s ⊆ t) => convex_Inter id theorem convex_hull_min {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : set E} (hst : s ⊆ t) (ht : convex t) : convex_hull s ⊆ t := set.Inter_subset_of_subset t (set.Inter_subset_of_subset hst (set.Inter_subset (fun (ht : convex t) => t) ht)) theorem convex_hull_mono {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {t : set E} (hst : s ⊆ t) : convex_hull s ⊆ convex_hull t := convex_hull_min (set.subset.trans hst (subset_convex_hull t)) (convex_convex_hull t) theorem convex.convex_hull_eq {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (hs : convex s) : convex_hull s = s := set.subset.antisymm (convex_hull_min (set.subset.refl s) hs) (subset_convex_hull s) @[simp] theorem convex_hull_singleton {E : Type u} [add_comm_group E] [vector_space ℝ E] {x : E} : convex_hull (singleton x) = singleton x := convex.convex_hull_eq (convex_singleton x) theorem is_linear_map.image_convex_hull {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {s : set E} {f : E → F} (hf : is_linear_map ℝ f) : f '' convex_hull s = convex_hull (f '' s) := sorry theorem linear_map.image_convex_hull {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {s : set E} (f : linear_map ℝ E F) : ⇑f '' convex_hull s = convex_hull (⇑f '' s) := is_linear_map.image_convex_hull (linear_map.is_linear f) theorem finset.center_mass_mem_convex_hull {E : Type u} {ι : Type w} [add_comm_group E] [vector_space ℝ E] {s : set E} (t : finset ι) {w : ι → ℝ} (hw₀ : ∀ (i : ι), i ∈ t → 0 ≤ w i) (hws : 0 < finset.sum t fun (i : ι) => w i) {z : ι → E} (hz : ∀ (i : ι), i ∈ t → z i ∈ s) : finset.center_mass t w z ∈ convex_hull s := convex.center_mass_mem (convex_convex_hull s) hw₀ hws fun (i : ι) (hi : i ∈ t) => subset_convex_hull s (hz i hi) -- TODO : Do we need other versions of the next lemma? /-- Convex hull of `s` is equal to the set of all centers of masses of `finset`s `t`, `z '' t ⊆ s`. This version allows finsets in any type in any universe. -/ theorem convex_hull_eq {E : Type u} [add_comm_group E] [vector_space ℝ E] (s : set E) : convex_hull s = set_of fun (x : E) => ∃ (ι : Type u'), ∃ (t : finset ι), ∃ (w : ι → ℝ), ∃ (z : ι → E), ∃ (hw₀ : ∀ (i : ι), i ∈ t → 0 ≤ w i), ∃ (hw₁ : (finset.sum t fun (i : ι) => w i) = 1), ∃ (hz : ∀ (i : ι), i ∈ t → z i ∈ s), finset.center_mass t w z = x := sorry /-- Maximum principle for convex functions. If a function `f` is convex on the convex hull of `s`, then `f` can't have a maximum on `convex_hull s` outside of `s`. -/ theorem convex_on.exists_ge_of_mem_convex_hull {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} {f : E → ℝ} (hf : convex_on (convex_hull s) f) {x : E} (hx : x ∈ convex_hull s) : ∃ (y : E), ∃ (H : y ∈ s), f x ≤ f y := sorry theorem finset.convex_hull_eq {E : Type u} [add_comm_group E] [vector_space ℝ E] (s : finset E) : convex_hull ↑s = set_of fun (x : E) => ∃ (w : E → ℝ), ∃ (hw₀ : ∀ (y : E), y ∈ s → 0 ≤ w y), ∃ (hw₁ : (finset.sum s fun (y : E) => w y) = 1), finset.center_mass s w id = x := sorry theorem set.finite.convex_hull_eq {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (hs : set.finite s) : convex_hull s = set_of fun (x : E) => ∃ (w : E → ℝ), ∃ (hw₀ : ∀ (y : E), y ∈ s → 0 ≤ w y), ∃ (hw₁ : (finset.sum (set.finite.to_finset hs) fun (y : E) => w y) = 1), finset.center_mass (set.finite.to_finset hs) w id = x := sorry theorem convex_hull_eq_union_convex_hull_finite_subsets {E : Type u} [add_comm_group E] [vector_space ℝ E] (s : set E) : convex_hull s = set.Union fun (t : finset E) => set.Union fun (w : ↑t ⊆ s) => convex_hull ↑t := sorry theorem is_linear_map.convex_hull_image {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] {f : E → F} (hf : is_linear_map ℝ f) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := sorry theorem linear_map.convex_hull_image {E : Type u} {F : Type v} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] (f : linear_map ℝ E F) (s : set E) : convex_hull (⇑f '' s) = ⇑f '' convex_hull s := is_linear_map.convex_hull_image (linear_map.is_linear f) s /-! ### Simplex -/ /-- The standard simplex in the space of functions `ι → ℝ` is the set of vectors with non-negative coordinates with total sum `1`. -/ def std_simplex (ι : Type u_1) [fintype ι] : set (ι → ℝ) := set_of fun (f : ι → ℝ) => (∀ (x : ι), 0 ≤ f x) ∧ (finset.sum finset.univ fun (x : ι) => f x) = 1 theorem std_simplex_eq_inter (ι : Type w) [fintype ι] : std_simplex ι = (set.Inter fun (x : ι) => set_of fun (f : ι → ℝ) => 0 ≤ f x) ∩ set_of fun (f : ι → ℝ) => (finset.sum finset.univ fun (x : ι) => f x) = 1 := sorry theorem convex_std_simplex (ι : Type w) [fintype ι] : convex (std_simplex ι) := sorry theorem ite_eq_mem_std_simplex {ι : Type w} [fintype ι] (i : ι) : (fun (j : ι) => ite (i = j) 1 0) ∈ std_simplex ι := sorry /-- `std_simplex ι` is the convex hull of the canonical basis in `ι → ℝ`. -/ theorem convex_hull_basis_eq_std_simplex {ι : Type w} [fintype ι] : convex_hull (set.range fun (i j : ι) => ite (i = j) 1 0) = std_simplex ι := sorry /-- The convex hull of a finite set is the image of the standard simplex in `s → ℝ` under the linear map sending each function `w` to `∑ x in s, w x • x`. Since we have no sums over finite sets, we use sum over `@finset.univ _ hs.fintype`. The map is defined in terms of operations on `(s → ℝ) →ₗ[ℝ] ℝ` so that later we will not need to prove that this map is linear. -/ theorem set.finite.convex_hull_eq_image {E : Type u} [add_comm_group E] [vector_space ℝ E] {s : set E} (hs : set.finite s) : convex_hull s = ⇑(finset.sum finset.univ fun (x : ↥s) => linear_map.smul_right (linear_map.proj x) (subtype.val x)) '' std_simplex ↥s := sorry /-- All values of a function `f ∈ std_simplex ι` belong to `[0, 1]`. -/ theorem mem_Icc_of_mem_std_simplex {ι : Type w} [fintype ι] {f : ι → ℝ} (hf : f ∈ std_simplex ι) (x : ι) : f x ∈ set.Icc 0 1 := { left := and.left hf x, right := and.right hf ▸ finset.single_le_sum (fun (y : ι) (hy : y ∈ finset.univ) => and.left hf y) (finset.mem_univ x) } end Mathlib
c7d3e08cbd7657d7cdf98ecfa77ffdf41323ca22
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Lean/Elab/Inductive.lean
79bbedd5dd6ecf97583837c17adc4fc8659df09f
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,991
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.ReplaceLevel import Lean.Util.ReplaceExpr import Lean.Util.CollectLevelParams import Lean.Util.Constructions import Lean.Elab.Command import Lean.Elab.CollectFVars import Lean.Elab.DefView import Lean.Elab.DeclUtil import Lean.Elab.Deriving.Basic namespace Lean.Elab.Command open Meta builtin_initialize registerTraceClass `Elab.inductive def checkValidInductiveModifier [Monad m] [MonadError m] (modifiers : Modifiers) : m Unit := do if modifiers.isNoncomputable then throwError "invalid use of 'noncomputable' in inductive declaration" if modifiers.isPartial then throwError "invalid use of 'partial' in inductive declaration" unless modifiers.attrs.size == 0 || (modifiers.attrs.size == 1 && modifiers.attrs[0].name == `class) do throwError "invalid use of attributes in inductive declaration" def checkValidCtorModifier [Monad m] [MonadError m] (modifiers : Modifiers) : m Unit := do if modifiers.isNoncomputable then throwError "invalid use of 'noncomputable' in constructor declaration" if modifiers.isPartial then throwError "invalid use of 'partial' in constructor declaration" if modifiers.isUnsafe then throwError "invalid use of 'unsafe' in constructor declaration" if modifiers.attrs.size != 0 then throwError "invalid use of attributes in constructor declaration" structure CtorView where ref : Syntax modifiers : Modifiers inferMod : Bool -- true if `{}` is used in the constructor declaration declName : Name binders : Syntax type? : Option Syntax deriving Inhabited structure InductiveView where ref : Syntax modifiers : Modifiers shortDeclName : Name declName : Name levelNames : List Name binders : Syntax type? : Option Syntax ctors : Array CtorView derivingClasses : Array DerivingClassView deriving Inhabited structure ElabHeaderResult where view : InductiveView lctx : LocalContext localInsts : LocalInstances params : Array Expr type : Expr deriving Inhabited private partial def elabHeaderAux (views : Array InductiveView) (i : Nat) (acc : Array ElabHeaderResult) : TermElabM (Array ElabHeaderResult) := do if h : i < views.size then let view := views.get ⟨i, h⟩ let acc ← Term.withAutoBoundImplicitLocal <| Term.elabBinders view.binders.getArgs (catchAutoBoundImplicit := true) fun params => do match view.type? with | none => let u ← mkFreshLevelMVar let type := mkSort u let params ← Term.addAutoBoundImplicits params pure <| acc.push { lctx := (← getLCtx), localInsts := (← getLocalInstances), params := params, type := type, view := view } | some typeStx => Term.elabTypeWithAutoBoundImplicit typeStx fun type => do unless (← isTypeFormerType type) do throwErrorAt typeStx "invalid inductive type, resultant type is not a sort" let params ← Term.addAutoBoundImplicits params pure <| acc.push { lctx := (← getLCtx), localInsts := (← getLocalInstances), params := params, type := type, view := view } elabHeaderAux views (i+1) acc else pure acc private def checkNumParams (rs : Array ElabHeaderResult) : TermElabM Nat := do let numParams := rs[0].params.size for r in rs do unless r.params.size == numParams do throwErrorAt r.view.ref "invalid inductive type, number of parameters mismatch in mutually inductive datatypes" pure numParams private def checkUnsafe (rs : Array ElabHeaderResult) : TermElabM Unit := do let isUnsafe := rs[0].view.modifiers.isUnsafe for r in rs do unless r.view.modifiers.isUnsafe == isUnsafe do throwErrorAt r.view.ref "invalid inductive type, cannot mix unsafe and safe declarations in a mutually inductive datatypes" private def checkLevelNames (views : Array InductiveView) : TermElabM Unit := do if views.size > 1 then let levelNames := views[0].levelNames for view in views do unless view.levelNames == levelNames do throwErrorAt view.ref "invalid inductive type, universe parameters mismatch in mutually inductive datatypes" private def mkTypeFor (r : ElabHeaderResult) : TermElabM Expr := do withLCtx r.lctx r.localInsts do mkForallFVars r.params r.type private def throwUnexpectedInductiveType {α} : TermElabM α := throwError "unexpected inductive resulting type" -- Given `e` of the form `forall As, B`, return `B`. -- It assumes `B` doesn't depend on `As`. private def getResultingType (e : Expr) : TermElabM Expr := forallTelescopeReducing e fun _ r => pure r private def eqvFirstTypeResult (firstType type : Expr) : MetaM Bool := forallTelescopeReducing firstType fun _ firstTypeResult => isDefEq firstTypeResult type -- Auxiliary function for checking whether the types in mutually inductive declaration are compatible. private partial def checkParamsAndResultType (type firstType : Expr) (numParams : Nat) : TermElabM Unit := do try forallTelescopeCompatible type firstType numParams fun _ type firstType => forallTelescopeReducing type fun _ type => forallTelescopeReducing firstType fun _ firstType => do match type with | Expr.sort _ _ => unless (← isDefEq firstType type) do throwError! "resulting universe mismatch, given{indentExpr type}\nexpected type{indentExpr firstType}" | _ => throwError "unexpected inductive resulting type" catch | Exception.error ref msg => throw (Exception.error ref m!"invalid mutually inductive types, {msg}") | ex => throw ex -- Auxiliary function for checking whether the types in mutually inductive declaration are compatible. private def checkHeader (r : ElabHeaderResult) (numParams : Nat) (firstType? : Option Expr) : TermElabM Expr := do let type ← mkTypeFor r match firstType? with | none => pure type | some firstType => withRef r.view.ref $ checkParamsAndResultType type firstType numParams pure firstType -- Auxiliary function for checking whether the types in mutually inductive declaration are compatible. private partial def checkHeaders (rs : Array ElabHeaderResult) (numParams : Nat) (i : Nat) (firstType? : Option Expr) : TermElabM Unit := do if i < rs.size then let type ← checkHeader rs[i] numParams firstType? checkHeaders rs numParams (i+1) type private def elabHeader (views : Array InductiveView) : TermElabM (Array ElabHeaderResult) := do let rs ← elabHeaderAux views 0 #[] if rs.size > 1 then checkUnsafe rs let numParams ← checkNumParams rs checkHeaders rs numParams 0 none pure rs /- Create a local declaration for each inductive type in `rs`, and execute `x params indFVars`, where `params` are the inductive type parameters and `indFVars` are the new local declarations. We use the the local context/instances and parameters of rs[0]. Note that this method is executed after we executed `checkHeaders` and established all parameters are compatible. -/ private partial def withInductiveLocalDecls {α} (rs : Array ElabHeaderResult) (x : Array Expr → Array Expr → TermElabM α) : TermElabM α := do let namesAndTypes ← rs.mapM fun r => do let type ← mkTypeFor r pure (r.view.shortDeclName, type) let r0 := rs[0] let params := r0.params withLCtx r0.lctx r0.localInsts $ withRef r0.view.ref do let rec loop (i : Nat) (indFVars : Array Expr) := do if h : i < namesAndTypes.size then let (id, type) := namesAndTypes.get ⟨i, h⟩ withLocalDeclD id type fun indFVar => loop (i+1) (indFVars.push indFVar) else x params indFVars loop 0 #[] private def isInductiveFamily (numParams : Nat) (indFVar : Expr) : TermElabM Bool := do let indFVarType ← inferType indFVar forallTelescopeReducing indFVarType fun xs _ => pure $ xs.size > numParams /- Elaborate constructor types. Remark: we check whether the resulting type is correct, but we do not check for: - Positivity (it is a rare failure, and the kernel already checks for it). - Universe constraints (the kernel checks for it). -/ private def elabCtors (indFVar : Expr) (params : Array Expr) (r : ElabHeaderResult) : TermElabM (List Constructor) := withRef r.view.ref do let indFamily ← isInductiveFamily params.size indFVar r.view.ctors.toList.mapM fun ctorView => Term.elabBinders ctorView.binders.getArgs fun ctorParams => withRef ctorView.ref do let type ← match ctorView.type? with | none => if indFamily then throwError "constructor resulting type must be specified in inductive family declaration" pure (mkAppN indFVar params) | some ctorType => let type ← Term.elabTerm ctorType none let resultingType ← getResultingType type unless resultingType.getAppFn == indFVar do throwError! "unexpected constructor resulting type{indentExpr resultingType}" unless (← isType resultingType) do throwError! "unexpected constructor resulting type, type expected{indentExpr resultingType}" let args := resultingType.getAppArgs for i in [:params.size] do let param := params[i] let arg := args[i] unless (← isDefEq param arg) do throwError! "inductive datatype parameter mismatch{indentExpr arg}\nexpected{indentExpr param}" pure type let type ← mkForallFVars ctorParams type let type ← mkForallFVars params type pure { name := ctorView.declName, type := type } /- Convert universe metavariables occurring in the `indTypes` into new parameters. Remark: if the resulting inductive datatype has universe metavariables, we will fix it later using `inferResultingUniverse`. -/ private def levelMVarToParamAux (indTypes : List InductiveType) : StateRefT Nat TermElabM (List InductiveType) := indTypes.mapM fun indType => do let type ← Term.levelMVarToParam' indType.type let ctors ← indType.ctors.mapM fun ctor => do let ctorType ← Term.levelMVarToParam' ctor.type pure { ctor with type := ctorType } pure { indType with ctors := ctors, type := type } private def levelMVarToParam (indTypes : List InductiveType) : TermElabM (List InductiveType) := (levelMVarToParamAux indTypes).run' 1 private def getResultingUniverse : List InductiveType → TermElabM Level | [] => throwError "unexpected empty inductive declaration" | indType :: _ => do let r ← getResultingType indType.type match r with | Expr.sort u _ => pure u | _ => throwError "unexpected inductive type resulting type" def tmpIndParam := mkLevelParam `_tmp_ind_univ_param /-- Return true if `u` is of the form `?m + k`. Return false if `u` does not contain universe metavariables. Throw exception otherwise. -/ def shouldInferResultUniverse (u : Level) : TermElabM Bool := do let u ← instantiateLevelMVars u if u.hasMVar then match u.getLevelOffset with | Level.mvar mvarId _ => do Term.assignLevelMVar mvarId tmpIndParam pure true | _ => throwError! "cannot infer resulting universe level of inductive datatype, given level contains metavariables {mkSort u}, provide universe explicitly" else pure false /- Auxiliary function for `updateResultingUniverse` `accLevelAtCtor u r rOffset us` add `u` components to `us` if they are not already there and it is different from the resulting universe level `r+rOffset`. If `u` is a `max`, then its components are recursively processed. If `u` is a `succ` and `rOffset > 0`, we process the `u`s child using `rOffset-1`. This method is used to infer the resulting universe level of an inductive datatype. -/ def accLevelAtCtor : Level → Level → Nat → Array Level → TermElabM (Array Level) | Level.max u v _, r, rOffset, us => do let us ← accLevelAtCtor u r rOffset us; accLevelAtCtor v r rOffset us | Level.imax u v _, r, rOffset, us => do let us ← accLevelAtCtor u r rOffset us; accLevelAtCtor v r rOffset us | Level.zero _, _, _, us => pure us | Level.succ u _, r, rOffset+1, us => accLevelAtCtor u r rOffset us | u, r, rOffset, us => if rOffset == 0 && u == r then pure us else if r.occurs u then throwError! "failed to compute resulting universe level of inductive datatype, provide universe explicitly" else if rOffset > 0 then throwError! "failed to compute resulting universe level of inductive datatype, provide universe explicitly" else if us.contains u then pure us else pure (us.push u) /- Auxiliary function for `updateResultingUniverse` -/ private partial def collectUniversesFromCtorTypeAux (r : Level) (rOffset : Nat) : Nat → Expr → Array Level → TermElabM (Array Level) | 0, Expr.forallE n d b c, us => do let u ← getLevel d let u ← instantiateLevelMVars u let us ← accLevelAtCtor u r rOffset us withLocalDecl n c.binderInfo d fun x => let e := b.instantiate1 x collectUniversesFromCtorTypeAux r rOffset 0 e us | i+1, Expr.forallE n d b c, us => do withLocalDecl n c.binderInfo d fun x => let e := b.instantiate1 x collectUniversesFromCtorTypeAux r rOffset i e us | _, _, us => pure us /- Auxiliary function for `updateResultingUniverse` -/ private partial def collectUniversesFromCtorType (r : Level) (rOffset : Nat) (ctorType : Expr) (numParams : Nat) (us : Array Level) : TermElabM (Array Level) := collectUniversesFromCtorTypeAux r rOffset numParams ctorType us /- Auxiliary function for `updateResultingUniverse` -/ private partial def collectUniverses (r : Level) (rOffset : Nat) (numParams : Nat) (indTypes : List InductiveType) : TermElabM (Array Level) := indTypes.foldlM (init := #[]) fun us indType => indType.ctors.foldlM (init := us) fun us ctor => collectUniversesFromCtorType r rOffset ctor.type numParams us def mkResultUniverse (us : Array Level) (rOffset : Nat) : Level := if us.isEmpty && rOffset == 0 then levelOne else let r := Level.mkNaryMax us.toList if rOffset == 0 && !r.isZero && !r.isNeverZero then (mkLevelMax r levelOne).normalize else r.normalize private def updateResultingUniverse (numParams : Nat) (indTypes : List InductiveType) : TermElabM (List InductiveType) := do let r ← getResultingUniverse indTypes let rOffset : Nat := r.getOffset let r : Level := r.getLevelOffset unless r.isParam do throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly" let us ← collectUniverses r rOffset numParams indTypes trace[Elab.inductive]! "updateResultingUniverse us: {us}, r: {r}, rOffset: {rOffset}" let rNew := mkResultUniverse us rOffset let updateLevel (e : Expr) : Expr := e.replaceLevel fun u => if u == tmpIndParam then some rNew else none return indTypes.map fun indType => let type := updateLevel indType.type; let ctors := indType.ctors.map fun ctor => { ctor with type := updateLevel ctor.type }; { indType with type := type, ctors := ctors } builtin_initialize registerOption `bootstrap.inductiveCheckResultingUniverse { defValue := true, group := "bootstrap", descr := "by default the `inductive/structure commands report an error if the resulting universe is not zero, but may be zero for some universe parameters. Reason: unless this type is a subsingleton, it is hardly what the user wants since it can only eliminate into `Prop`. In the `Init` package, we define subsingletons, and we use this option to disable the check. This option may be deleted in the future after we improve the validator" } def getCheckResultingUniverseOption (opts : Options) : Bool := opts.get `bootstrap.inductiveCheckResultingUniverse true def checkResultingUniverse (u : Level) : TermElabM Unit := do if getCheckResultingUniverseOption (← getOptions) then let u ← instantiateLevelMVars u if !u.isZero && !u.isNeverZero then throwError! "invalid universe polymorphic type, the resultant universe is not Prop (i.e., 0), but it may be Prop for some parameter values (solution: use 'u+1' or 'max 1 u'{indentD u}" private def checkResultingUniverses (indTypes : List InductiveType) : TermElabM Unit := do checkResultingUniverse (← getResultingUniverse indTypes) private def collectUsed (indTypes : List InductiveType) : StateRefT CollectFVars.State TermElabM Unit := do indTypes.forM fun indType => do Term.collectUsedFVars indType.type indType.ctors.forM fun ctor => Term.collectUsedFVars ctor.type private def removeUnused (vars : Array Expr) (indTypes : List InductiveType) : TermElabM (LocalContext × LocalInstances × Array Expr) := do let (_, used) ← (collectUsed indTypes).run {} Term.removeUnused vars used private def withUsed {α} (vars : Array Expr) (indTypes : List InductiveType) (k : Array Expr → TermElabM α) : TermElabM α := do let (lctx, localInsts, vars) ← removeUnused vars indTypes withLCtx lctx localInsts $ k vars private def updateParams (vars : Array Expr) (indTypes : List InductiveType) : TermElabM (List InductiveType) := indTypes.mapM fun indType => do let type ← mkForallFVars vars indType.type let ctors ← indType.ctors.mapM fun ctor => do let ctorType ← mkForallFVars vars ctor.type pure { ctor with type := ctorType } pure { indType with type := type, ctors := ctors } private def collectLevelParamsInInductive (indTypes : List InductiveType) : Array Name := let usedParams := indTypes.foldl (init := {}) fun (usedParams : CollectLevelParams.State) indType => let usedParams := collectLevelParams usedParams indType.type; indType.ctors.foldl (init := usedParams) fun (usedParams : CollectLevelParams.State) ctor => collectLevelParams usedParams ctor.type usedParams.params private def mkIndFVar2Const (views : Array InductiveView) (indFVars : Array Expr) (levelNames : List Name) : ExprMap Expr := let levelParams := levelNames.map mkLevelParam; views.size.fold (init := {}) fun i (m : ExprMap Expr) => let view := views[i] let indFVar := indFVars[i] m.insert indFVar (mkConst view.declName levelParams) /- Remark: `numVars <= numParams`. `numVars` is the number of context `variables` used in the inductive declaration, and `numParams` is `numVars` + number of explicit parameters provided in the declaration. -/ private def replaceIndFVarsWithConsts (views : Array InductiveView) (indFVars : Array Expr) (levelNames : List Name) (numVars : Nat) (numParams : Nat) (indTypes : List InductiveType) : TermElabM (List InductiveType) := let indFVar2Const := mkIndFVar2Const views indFVars levelNames indTypes.mapM fun indType => do let ctors ← indType.ctors.mapM fun ctor => do let type ← forallBoundedTelescope ctor.type numParams fun params type => do let type := type.replace fun e => if !e.isFVar then none else match indFVar2Const.find? e with | none => none | some c => mkAppN c (params.extract 0 numVars) mkForallFVars params type pure { ctor with type := type } pure { indType with ctors := ctors } abbrev Ctor2InferMod := Std.HashMap Name Bool private def mkCtor2InferMod (views : Array InductiveView) : Ctor2InferMod := views.foldl (init := {}) fun m view => view.ctors.foldl (init := m) fun m ctorView => m.insert ctorView.declName ctorView.inferMod private def applyInferMod (views : Array InductiveView) (numParams : Nat) (indTypes : List InductiveType) : List InductiveType := let ctor2InferMod := mkCtor2InferMod views indTypes.map fun indType => let ctors := indType.ctors.map fun ctor => let inferMod := ctor2InferMod.find! ctor.name -- true if `{}` was used let ctorType := ctor.type.inferImplicit numParams !inferMod { ctor with type := ctorType } { indType with ctors := ctors } private def mkAuxConstructions (views : Array InductiveView) : TermElabM Unit := do let env ← getEnv let hasEq := env.contains `Eq let hasHEq := env.contains `HEq let hasUnit := env.contains `PUnit let hasProd := env.contains `Prod for view in views do let n := view.declName mkRecOn n if hasUnit then mkCasesOn n if hasUnit && hasEq && hasHEq then mkNoConfusion n if hasUnit && hasProd then mkBelow n if hasUnit && hasProd then mkIBelow n for view in views do let n := view.declName; if hasUnit && hasProd then mkBRecOn n if hasUnit && hasProd then mkBInductionOn n private def mkInductiveDecl (vars : Array Expr) (views : Array InductiveView) : TermElabM Unit := do let view0 := views[0] let scopeLevelNames ← Term.getLevelNames checkLevelNames views let allUserLevelNames := view0.levelNames let isUnsafe := view0.modifiers.isUnsafe withRef view0.ref <| Term.withLevelNames allUserLevelNames do let rs ← elabHeader views withInductiveLocalDecls rs fun params indFVars => do let numExplicitParams := params.size let indTypes ← views.size.foldM (init := []) fun i (indTypes : List InductiveType) => do let indFVar := indFVars[i] let r := rs[i] let type ← mkForallFVars params r.type let ctors ← elabCtors indFVar params r let indType := { name := r.view.declName, type := type, ctors := ctors : InductiveType } pure (indType :: indTypes) let indTypes := indTypes.reverse Term.synthesizeSyntheticMVarsNoPostponing let u ← getResultingUniverse indTypes let inferLevel ← shouldInferResultUniverse u withUsed vars indTypes fun vars => do let numVars := vars.size let numParams := numVars + numExplicitParams let indTypes ← updateParams vars indTypes let indTypes ← levelMVarToParam indTypes let indTypes ← if inferLevel then updateResultingUniverse numParams indTypes else checkResultingUniverses indTypes; pure indTypes let usedLevelNames := collectLevelParamsInInductive indTypes match sortDeclLevelParams scopeLevelNames allUserLevelNames usedLevelNames with | Except.error msg => throwError msg | Except.ok levelParams => do let indTypes ← replaceIndFVarsWithConsts views indFVars levelParams numVars numParams indTypes let indTypes := applyInferMod views numParams indTypes let decl := Declaration.inductDecl levelParams numParams indTypes isUnsafe Term.ensureNoUnassignedMVars decl addDecl decl mkAuxConstructions views -- We need to invoke `applyAttributes` because `class` is implemented as an attribute. for view in views do Term.applyAttributesAt view.declName view.modifiers.attrs AttributeApplicationTime.afterTypeChecking private def applyDerivingHandlers (views : Array InductiveView) : CommandElabM Unit := do let mut processed : NameSet := {} for view in views do for classView in view.derivingClasses do let className := classView.className unless processed.contains className do processed := processed.insert className let mut declNames := #[] for view in views do if view.derivingClasses.any fun classView => classView.className == className then declNames := declNames.push view.declName classView.applyHandlers declNames def elabInductiveViews (views : Array InductiveView) : CommandElabM Unit := do let view0 := views[0] let ref := view0.ref runTermElabM view0.declName fun vars => withRef ref do mkInductiveDecl vars views applyDerivingHandlers views end Lean.Elab.Command
ae2e58c022d7b98cf7a9adc554b576089080d027
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/group_power/ring.lean
80e3dd7a3d76d345f36275359c2c8a3bd05d828a
[ "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
7,936
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis -/ import algebra.group_power.basic import algebra.group_with_zero.commute import algebra.hom.ring import algebra.ring.commute import algebra.group_with_zero.divisibility import algebra.ring.divisibility import data.nat.order.basic /-! # Power operations on monoids with zero, semirings, and rings > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides additional lemmas about the natural power operator on rings and semirings. Further lemmas about ordered semirings and rings can be found in `algebra.group_power.lemmas`. -/ variables {R S M : Type*} section monoid_with_zero variables [monoid_with_zero M] lemma zero_pow : ∀ {n : ℕ}, 0 < n → (0 : M) ^ n = 0 | (n+1) _ := by rw [pow_succ, zero_mul] @[simp] lemma zero_pow' : ∀ n : ℕ, n ≠ 0 → (0 : M) ^ n = 0 | 0 h := absurd rfl h | (k+1) h := by { rw [pow_succ], exact zero_mul _ } lemma zero_pow_eq (n : ℕ) : (0 : M)^n = if n = 0 then 1 else 0 := begin split_ifs with h, { rw [h, pow_zero], }, { rw [zero_pow (nat.pos_of_ne_zero h)] }, end lemma pow_eq_zero_of_le {x : M} {n m : ℕ} (hn : n ≤ m) (hx : x^n = 0) : x^m = 0 := by rw [← tsub_add_cancel_of_le hn, pow_add, hx, mul_zero] theorem pow_eq_zero [no_zero_divisors M] {x : M} {n : ℕ} (H : x^n = 0) : x = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one x, H, mul_zero] }, { rw pow_succ at H, exact or.cases_on (mul_eq_zero.1 H) id ih } end @[simp] lemma pow_eq_zero_iff [no_zero_divisors M] {a : M} {n : ℕ} (hn : 0 < n) : a ^ n = 0 ↔ a = 0 := begin refine ⟨pow_eq_zero, _⟩, rintros rfl, exact zero_pow hn, end lemma pow_eq_zero_iff' [no_zero_divisors M] [nontrivial M] {a : M} {n : ℕ} : a ^ n = 0 ↔ a = 0 ∧ n ≠ 0 := by cases (zero_le n).eq_or_gt; simp [*, ne_of_gt] lemma pow_ne_zero_iff [no_zero_divisors M] {a : M} {n : ℕ} (hn : 0 < n) : a ^ n ≠ 0 ↔ a ≠ 0 := (pow_eq_zero_iff hn).not lemma ne_zero_pow {a : M} {n : ℕ} (hn : n ≠ 0) : a ^ n ≠ 0 → a ≠ 0 := by { contrapose!, rintro rfl, exact zero_pow' n hn } @[field_simps] theorem pow_ne_zero [no_zero_divisors M] {a : M} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h instance ne_zero.pow [no_zero_divisors M] {x : M} [ne_zero x] {n : ℕ} : ne_zero (x ^ n) := ⟨pow_ne_zero n ne_zero.out⟩ theorem sq_eq_zero_iff [no_zero_divisors M] {a : M} : a ^ 2 = 0 ↔ a = 0 := pow_eq_zero_iff two_pos @[simp] lemma zero_pow_eq_zero [nontrivial M] {n : ℕ} : (0 : M) ^ n = 0 ↔ 0 < n := begin split; intro h, { rw [pos_iff_ne_zero], rintro rfl, simpa using h }, { exact zero_pow' n h.ne.symm } end lemma ring.inverse_pow (r : M) : ∀ (n : ℕ), ring.inverse r ^ n = ring.inverse (r ^ n) | 0 := by rw [pow_zero, pow_zero, ring.inverse_one] | (n + 1) := by rw [pow_succ, pow_succ', ring.mul_inverse_rev' ((commute.refl r).pow_left n), ring.inverse_pow] end monoid_with_zero section comm_monoid_with_zero variables [comm_monoid_with_zero M] {n : ℕ} (hn : 0 < n) include M hn /-- We define `x ↦ x^n` (for positive `n : ℕ`) as a `monoid_with_zero_hom` -/ def pow_monoid_with_zero_hom : M →*₀ M := { map_zero' := zero_pow hn, ..pow_monoid_hom n } @[simp] lemma coe_pow_monoid_with_zero_hom : (pow_monoid_with_zero_hom hn : M → M) = (^ n) := rfl @[simp] lemma pow_monoid_with_zero_hom_apply (a : M) : pow_monoid_with_zero_hom hn a = a ^ n := rfl end comm_monoid_with_zero lemma pow_dvd_pow_iff [cancel_comm_monoid_with_zero R] {x : R} {n m : ℕ} (h0 : x ≠ 0) (h1 : ¬ is_unit x) : x ^ n ∣ x ^ m ↔ n ≤ m := begin split, { intro h, rw [← not_lt], intro hmn, apply h1, have : x ^ m * x ∣ x ^ m * 1, { rw [← pow_succ', mul_one], exact (pow_dvd_pow _ (nat.succ_le_of_lt hmn)).trans h }, rwa [mul_dvd_mul_iff_left, ← is_unit_iff_dvd_one] at this, apply pow_ne_zero m h0 }, { apply pow_dvd_pow } end section semiring variables [semiring R] [semiring S] protected lemma ring_hom.map_pow (f : R →+* S) (a) : ∀ n : ℕ, f (a ^ n) = (f a) ^ n := map_pow f a lemma min_pow_dvd_add {n m : ℕ} {a b c : R} (ha : c ^ n ∣ a) (hb : c ^ m ∣ b) : c ^ (min n m) ∣ a + b := begin replace ha := (pow_dvd_pow c (min_le_left n m)).trans ha, replace hb := (pow_dvd_pow c (min_le_right n m)).trans hb, exact dvd_add ha hb end end semiring section comm_semiring variables [comm_semiring R] lemma add_sq (a b : R) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by simp only [sq, add_mul_self_eq] lemma add_sq' (a b : R) : (a + b) ^ 2 = a ^ 2 + b ^ 2 + 2 * a * b := by rw [add_sq, add_assoc, add_comm _ (b ^ 2), add_assoc] alias add_sq ← add_pow_two end comm_semiring section has_distrib_neg variables [monoid R] [has_distrib_neg R] variables (R) theorem neg_one_pow_eq_or : ∀ n : ℕ, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl (pow_zero _) | (n+1) := (neg_one_pow_eq_or n).swap.imp (λ h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (λ h, by rw [pow_succ, h, mul_one]) variables {R} theorem neg_pow (a : R) (n : ℕ) : (- a) ^ n = (-1) ^ n * a ^ n := (neg_one_mul a) ▸ (commute.neg_one_left a).mul_pow n @[simp] theorem neg_pow_bit0 (a : R) (n : ℕ) : (- a) ^ (bit0 n) = a ^ (bit0 n) := by rw [pow_bit0', neg_mul_neg, pow_bit0'] @[simp] theorem neg_pow_bit1 (a : R) (n : ℕ) : (- a) ^ (bit1 n) = - a ^ (bit1 n) := by simp only [bit1, pow_succ, neg_pow_bit0, neg_mul_eq_neg_mul] @[simp] lemma neg_sq (a : R) : (-a) ^ 2 = a ^ 2 := by simp [sq] @[simp] lemma neg_one_sq : (-1 : R) ^ 2 = 1 := by rw [neg_sq, one_pow] alias neg_sq ← neg_pow_two alias neg_one_sq ← neg_one_pow_two end has_distrib_neg section ring variables [ring R] {a b : R} protected lemma commute.sq_sub_sq (h : commute a b) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by rw [sq, sq, h.mul_self_sub_mul_self_eq] @[simp] lemma neg_one_pow_mul_eq_zero_iff {n : ℕ} {r : R} : (-1)^n * r = 0 ↔ r = 0 := by rcases neg_one_pow_eq_or R n; simp [h] @[simp] lemma mul_neg_one_pow_eq_zero_iff {n : ℕ} {r : R} : r * (-1)^n = 0 ↔ r = 0 := by rcases neg_one_pow_eq_or R n; simp [h] variables [no_zero_divisors R] protected lemma commute.sq_eq_sq_iff_eq_or_eq_neg (h : commute a b) : a ^ 2 = b ^ 2 ↔ a = b ∨ a = -b := by rw [←sub_eq_zero, h.sq_sub_sq, mul_eq_zero, add_eq_zero_iff_eq_neg, sub_eq_zero, or_comm] @[simp] lemma sq_eq_one_iff : a^2 = 1 ↔ a = 1 ∨ a = -1 := by rw [←(commute.one_right a).sq_eq_sq_iff_eq_or_eq_neg, one_pow] lemma sq_ne_one_iff : a^2 ≠ 1 ↔ a ≠ 1 ∧ a ≠ -1 := sq_eq_one_iff.not.trans not_or_distrib end ring section comm_ring variables [comm_ring R] lemma sq_sub_sq (a b : R) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := (commute.all a b).sq_sub_sq alias sq_sub_sq ← pow_two_sub_pow_two lemma sub_sq (a b : R) : (a - b) ^ 2 = a ^ 2 - 2 * a * b + b ^ 2 := by rw [sub_eq_add_neg, add_sq, neg_sq, mul_neg, ← sub_eq_add_neg] alias sub_sq ← sub_pow_two lemma sub_sq' (a b : R) : (a - b) ^ 2 = a ^ 2 + b ^ 2 - 2 * a * b := by rw [sub_eq_add_neg, add_sq', neg_sq, mul_neg, ← sub_eq_add_neg] variables [no_zero_divisors R] {a b : R} lemma sq_eq_sq_iff_eq_or_eq_neg : a ^ 2 = b ^ 2 ↔ a = b ∨ a = -b := (commute.all a b).sq_eq_sq_iff_eq_or_eq_neg lemma eq_or_eq_neg_of_sq_eq_sq (a b : R) : a ^ 2 = b ^ 2 → a = b ∨ a = -b := sq_eq_sq_iff_eq_or_eq_neg.1 /- Copies of the above comm_ring lemmas for `units R`. -/ namespace units protected lemma sq_eq_sq_iff_eq_or_eq_neg {a b : Rˣ} : a ^ 2 = b ^ 2 ↔ a = b ∨ a = -b := by simp_rw [ext_iff, coe_pow, sq_eq_sq_iff_eq_or_eq_neg, units.coe_neg] protected lemma eq_or_eq_neg_of_sq_eq_sq (a b : Rˣ) (h : a ^ 2 = b ^ 2) : a = b ∨ a = -b := units.sq_eq_sq_iff_eq_or_eq_neg.1 h end units end comm_ring
7beab3d134117223c770d343b24714beeda0035e
4e3bf8e2b29061457a887ac8889e88fa5aa0e34c
/lean/love11_logical_foundations_of_mathematics_demo.lean
7933383438e2afebc779880e3c1abb3d2a9ca34d
[]
no_license
mukeshtiwari/logical_verification_2019
9f964c067a71f65eb8884743273fbeef99e6503d
16f62717f55ed5b7b87e03ae0134791a9bef9b9a
refs/heads/master
1,619,158,844,208
1,585,139,500,000
1,585,139,500,000
249,906,380
0
0
null
1,585,118,728,000
1,585,118,727,000
null
UTF-8
Lean
false
false
9,025
lean
/- LoVe Demo 11: Logical Foundations of Mathematics -/ import .love05_inductive_predicates_demo_master namespace LoVe set_option pp.beta true /- Type Universes -/ #check @and.intro #check ∀a b : Prop, a → b → a ∧ b #check Prop #check ℕ #check Type #check Type 1 #check Type 2 -- ... universe variables u v #check Type u #check Sort 0 #check Sort 1 #check Sort 2 #check Sort u #check Type* /- Impredicativity -/ section impredicativity variables (α : Type u) (β : Type v) #check α → β #check Πa : Type, a → a #check ∀a : Prop, a → a end impredicativity /- Proof Irrelevance -/ #check proof_irrel lemma proof_irrel {a : Prop} (h₁ h₂ : a) : h₁ = h₂ := by refl /- Large Elimination -/ inductive square_roots (sq : ℤ) : Type u | intro (z : ℤ) (h : sq = z * z) : square_roots -- `square_roots sq` contains all square roots of a number `sq`. -- * It is empty if `sq` is not square. -- * It has one element if `sq = 0`. -- * It has two elements if `sq > 0` is square. def two_as_root : square_roots 4 := square_roots.intro 2 (by refl) def neg_two_as_root : square_roots 4 := square_roots.intro (-2) (by refl) def large_elim {sq : ℤ} {α : Sort v} : square_roots sq → (Π(z : ℤ) (h : sq = z * z), α) → α | (square_roots.intro z h) f := f z h #eval large_elim two_as_root (λz _, z) -- result: 2 #eval large_elim neg_two_as_root (λz _, z) -- result: -2 example : two_as_root ≠ neg_two_as_root := begin have hne : large_elim two_as_root (λz _, z) ≠ large_elim neg_two_as_root (λz _, z) := by intro h; cases h, intro hr, apply hne, rw hr end -- Above, we could also have used `cases hr`. example (sq : ℤ) (r : square_roots sq) : sq ≥ 0 := begin apply large_elim r, intros z hz, rw hz, apply mul_self_nonneg end -- Above, we could also have used `cases r`. /- Small Elimination -/ inductive square_number (sq : ℤ) : Prop | intro (z : ℤ) (h : sq = z * z) : square_number -- only difference: `Prop` instead of `Type u` lemma four_is_square : square_number 4 := square_number.intro 2 (by refl) lemma four_is_square' : square_number 4 := square_number.intro (-2) (by refl) example : four_is_square = four_is_square' := by apply proof_irrel -- The "no confusion" principle does not hold for inductive predicates, -- only for inductive types. -- If we could define a large eliminator, this would lead to a contradiction. def large_elim' {sq : ℤ} {α : Sort v} : square_number sq → (∀(z : ℤ) (h : sq = z * z), α) → α | (square_number.intro z h) f := f z h -- induction tactic failed, recursor -- 'LoVe.square_number.dcases_on' can only eliminate into Prop -- We cannot extract the information that `four_is_square` was -- constructed using `2` and `four_is_square'` was constructed -- using `-2`. def small_elim {sq : ℤ} {α : Prop} : square_number sq → (∀(z : ℤ) (h : sq = z * z), α) → α | (square_number.intro z h) f := f z h -- It is called a small eliminator because it eliminates only -- into `Prop`, whereas a large eliminator can eliminate into -- an arbitrary large type universe `Sort v`. example (sq : ℤ) (h : square_number sq) : sq ≥ 0 := begin apply small_elim h, intros z hz, rw hz, apply mul_self_nonneg end /- Axiom of Choice -/ #print nonempty /- inductive nonempty (α : Sort u) : Prop | intro (val : α) : nonempty -/ lemma nat.nonempty : nonempty ℕ := nonempty.intro 0 #print classical.choice -- classical.choice is noncomputable: #eval classical.choice nat.nonempty #reduce classical.choice nat.nonempty -- definitions using it need to be marked as noncomputable: def some_natural_number := classical.choice nat.nonempty noncomputable def some_natural_number' := classical.choice nat.nonempty -- Law of excluded middle: #check classical.em -- excluded middle #check classical.by_contradiction local attribute [instance, priority 0] classical.prop_decidable -- This enables the tactic `by_cases` to be used on any proposition. example (a : Prop) : a ∨ ¬ a := begin by_cases h : a, { exact or.intro_left (¬ a) h }, { exact or.intro_right a h } end -- `by_cases` makes a case distinction on the truth of a proposition, -- whereas `cases` makes a case distinction on an inductive predicate/datatype. -- Hilbert choice: classical.some variables (p : ℕ → Prop) (h : ∃n : ℕ, p n) #check classical.some h #check classical.some_spec h -- In contrast to `cases` this can also be used in definitions. -- Set-theoretic axiom of choice: #check @classical.axiom_of_choice example (x : list ℕ) (h : ∀y : list ℕ, ∃z : list ℕ, [0] ++ z = x ++ y) : x.head = 0 := begin choose z h using h, have hx : x = [0] ++ z [] := by simp [h []], simp [hx] end /- Subtype Example: Full Binary Trees -/ #check btree #check is_full #check mirror #check is_full_mirror #check mirror_mirror def full_btree (α : Type) : Type := {t : btree α // is_full t} /- This is syntactic sugar for: def full_btree (α : Type) := @subtype (btree α) is_full -/ #check subtype -- To define elements of this subtype, -- we need to provide a btree and a proof that it is full: def some_full_btree : full_btree ℕ := subtype.mk empty is_full.empty #check subtype.mk def another_full_btree : full_btree ℕ := subtype.mk (node 6 empty empty) begin apply is_full.node, apply is_full.empty, apply is_full.empty, refl end #reduce subtype.val another_full_btree -- returns: node 6 empty empty #check subtype.property another_full_btree -- returns: is_full (subtype.val another_full_btree) def full_btree.mirror {α : Type} (t : full_btree α) : full_btree α := subtype.mk (mirror (subtype.val t)) begin apply is_full_mirror, apply subtype.property t end #reduce subtype.val (full_btree.mirror another_full_btree) lemma full_btree.mirror_mirror {α : Type} (t : full_btree α) : (full_btree.mirror (full_btree.mirror t)) = t := begin apply subtype.eq, simp [full_btree.mirror], apply mirror_mirror end #check subtype.eq /- Subtype Example: Vectors -/ #check vector def vector (α : Type u) (n : ℕ) : Type u := {l : list α // l.length = n} def some_vector : vector ℤ 3 := subtype.mk ([1, 2, 3]) (by refl) def vector.neg {n : ℕ} (v : vector ℤ n) : vector ℤ n := subtype.mk (list.map int.neg (subtype.val v)) begin rw list.length_map, exact subtype.property v end lemma int.neg_comp_neg : int.neg ∘ int.neg = id := begin apply funext, apply neg_neg end lemma vector.neg_neg (n : ℕ) (v : vector ℤ n) : vector.neg (vector.neg v) = v := begin apply subtype.eq, simp [vector.neg], rw [int.neg_comp_neg, list.map_id] end /- Quotient Types -/ -- `quotient` for equivalence relations #check quotient #print setoid def base : Type := sorry def e : base → base → Prop := sorry instance base.setoid : setoid base := { r := e, iseqv := sorry } def my_quotient : Type := quotient base.setoid variables (a : base) #check quotient.mk a #check ⟦a⟧ #check @quotient.sound base base.setoid #check @quotient.exact base base.setoid variables (q q' : my_quotient) (β : Type) (f : base → β) (g : base → base → β) #check quotient.lift f #check quotient.lift₂ g #check quotient.induction_on #check quotient.induction_on₂ #check quotient.induction_on₃ /- Integers as a Quotient -/ /- Basic idea: `(p, n)` represents `p - n` -/ instance myℤ.rel : setoid (ℕ × ℕ) := { r := λa b, a.1 + b.2 = b.1 + a.2, iseqv := begin apply and.intro, { intro a, refl }, apply and.intro, { intros a b h, rw h }, { intros a b c eq_ab eq_bc, apply eq_of_add_eq_add_right, calc (a.1 + c.2) + b.2 = (a.1 + b.2) + c.2 : by ac_refl ... = a.2 + (b.1 + c.2) : by rw [eq_ab]; ac_refl ... = (c.1 + a.2) + b.2 : by rw [eq_bc]; ac_refl } end } #print equivalence def myℤ : Type := quotient myℤ.rel lemma rel_iff (a b : ℕ × ℕ) : a ≈ b ↔ a.1 + b.2 = b.1 + a.2 := by refl def zero : myℤ := ⟦(0, 0)⟧ example (n : ℕ) : zero = ⟦(n, n)⟧ := begin rw zero, apply quotient.sound, rw [rel_iff], simp end #check quotient.lift₂ def add : myℤ → myℤ → myℤ := quotient.lift₂ (λa b : ℕ × ℕ, ⟦(a.1 + b.1, a.2 + b.2)⟧) begin intros a₁ b₁ a₂ b₂ ha hb, apply quotient.sound, rw [rel_iff] at ha hb ⊢, calc (a₁.1 + b₁.1) + (a₂.2 + b₂.2) = (a₁.1 + a₂.2) + (b₁.1 + b₂.2) : by ac_refl ... = (a₂.1 + a₁.2) + (b₂.1 + b₁.2) : by rw [ha, hb] ... = (a₂.1 + b₂.1) + (a₁.2 + b₁.2) : by ac_refl end lemma add_mk (ap an bp bn : ℕ) : add ⟦(ap, an)⟧ ⟦(bp, bn)⟧ = ⟦(ap + bp, an + bn)⟧ := by refl lemma add_zero (i : myℤ) : add zero i = i := begin apply quotient.induction_on i, intro p, rw zero, cases p, rw add_mk, apply quotient.sound, simp end end LoVe
cc0447b6679dc06c7f72c9aecfbd8c0386b49d74
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/eisenstein_criterion.lean
78fc40f281f19efb88c7a4bf962175ee50a6d313
[ "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
5,743
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.nat.cast.with_top import ring_theory.prime import ring_theory.polynomial.content import ring_theory.ideal.quotient_operations /-! # Eisenstein's criterion > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A proof of a slight generalisation of Eisenstein's criterion for the irreducibility of a polynomial over an integral domain. -/ open polynomial ideal.quotient variables {R : Type*} [comm_ring R] namespace polynomial open_locale polynomial namespace eisenstein_criterion_aux /- Section for auxiliary lemmas used in the proof of `irreducible_of_eisenstein_criterion`-/ lemma map_eq_C_mul_X_pow_of_forall_coeff_mem {f : R[X]} {P : ideal R} (hfP : ∀ (n : ℕ), ↑n < f.degree → f.coeff n ∈ P) : map (mk P) f = C ((mk P) f.leading_coeff) * X ^ f.nat_degree := polynomial.ext (λ n, begin by_cases hf0 : f = 0, { simp [hf0], }, rcases lt_trichotomy ↑n (degree f) with h | h | h, { erw [coeff_map, eq_zero_iff_mem.2 (hfP n h), coeff_C_mul, coeff_X_pow, if_neg, mul_zero], rintro rfl, exact not_lt_of_ge degree_le_nat_degree h }, { have : nat_degree f = n, from nat_degree_eq_of_degree_eq_some h.symm, rw [coeff_C_mul, coeff_X_pow, if_pos this.symm, mul_one, leading_coeff, this, coeff_map] }, { rw [coeff_eq_zero_of_degree_lt, coeff_eq_zero_of_degree_lt], { refine lt_of_le_of_lt (degree_C_mul_X_pow_le _ _) _, rwa ← degree_eq_nat_degree hf0 }, { exact lt_of_le_of_lt (degree_map_le _ _) h } } end) lemma le_nat_degree_of_map_eq_mul_X_pow {n : ℕ} {P : ideal R} (hP : P.is_prime) {q : R[X]} {c : polynomial (R ⧸ P)} (hq : map (mk P) q = c * X ^ n) (hc0 : c.degree = 0) : n ≤ q.nat_degree := with_bot.coe_le_coe.1 (calc ↑n = degree (q.map (mk P)) : by rw [hq, degree_mul, hc0, zero_add, degree_pow, degree_X, nsmul_one, nat.cast_with_bot] ... ≤ degree q : degree_map_le _ _ ... ≤ nat_degree q : degree_le_nat_degree) lemma eval_zero_mem_ideal_of_eq_mul_X_pow {n : ℕ} {P : ideal R} {q : R[X]} {c : polynomial (R ⧸ P)} (hq : map (mk P) q = c * X ^ n) (hn0 : 0 < n) : eval 0 q ∈ P := by rw [← coeff_zero_eq_eval_zero, ← eq_zero_iff_mem, ← coeff_map, coeff_zero_eq_eval_zero, hq, eval_mul, eval_pow, eval_X, zero_pow hn0, mul_zero] lemma is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit {p q : R[X]} (hu : ∀ (x : R), C x ∣ p * q → is_unit x) (hpm : p.nat_degree = 0) : is_unit p := begin rw [eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpm), is_unit_C], refine hu _ _, rw [← eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpm)], exact dvd_mul_right _ _ end end eisenstein_criterion_aux open eisenstein_criterion_aux variables [is_domain R] /-- If `f` is a non constant polynomial with coefficients in `R`, and `P` is a prime ideal in `R`, then if every coefficient in `R` except the leading coefficient is in `P`, and the trailing coefficient is not in `P^2` and no non units in `R` divide `f`, then `f` is irreducible. -/ theorem irreducible_of_eisenstein_criterion {f : R[X]} {P : ideal R} (hP : P.is_prime) (hfl : f.leading_coeff ∉ P) (hfP : ∀ n : ℕ, ↑n < degree f → f.coeff n ∈ P) (hfd0 : 0 < degree f) (h0 : f.coeff 0 ∉ P^2) (hu : f.is_primitive) : irreducible f := have hf0 : f ≠ 0, from λ _, by simp only [*, not_true, submodule.zero_mem, coeff_zero] at *, have hf : f.map (mk P) = C (mk P (leading_coeff f)) * X ^ nat_degree f, from map_eq_C_mul_X_pow_of_forall_coeff_mem hfP, have hfd0 : 0 < f.nat_degree, from with_bot.coe_lt_coe.1 (lt_of_lt_of_le hfd0 degree_le_nat_degree), ⟨mt degree_eq_zero_of_is_unit (λ h, by simp only [*, lt_irrefl] at *), begin rintros p q rfl, rw [polynomial.map_mul] at hf, rcases mul_eq_mul_prime_pow (show prime (X : polynomial (R ⧸ P)), from monic_X.prime_of_degree_eq_one degree_X) hf with ⟨m, n, b, c, hmnd, hbc, hp, hq⟩, have hmn : 0 < m → 0 < n → false, { assume hm0 hn0, refine h0 _, rw [coeff_zero_eq_eval_zero, eval_mul, sq], exact ideal.mul_mem_mul (eval_zero_mem_ideal_of_eq_mul_X_pow hp hm0) (eval_zero_mem_ideal_of_eq_mul_X_pow hq hn0) }, have hpql0 : (mk P) (p * q).leading_coeff ≠ 0, { rwa [ne.def, eq_zero_iff_mem] }, have hp0 : p ≠ 0, from λ h, by simp only [*, zero_mul, eq_self_iff_true, not_true, ne.def] at *, have hq0 : q ≠ 0, from λ h, by simp only [*, eq_self_iff_true, not_true, ne.def, mul_zero] at *, have hbc0 : degree b = 0 ∧ degree c = 0, { apply_fun degree at hbc, rwa [degree_C hpql0, degree_mul, eq_comm, nat.with_bot.add_eq_zero_iff] at hbc }, have hmp : m ≤ nat_degree p, from le_nat_degree_of_map_eq_mul_X_pow hP hp hbc0.1, have hnq : n ≤ nat_degree q, from le_nat_degree_of_map_eq_mul_X_pow hP hq hbc0.2, have hpmqn : p.nat_degree = m ∧ q.nat_degree = n, { rw [nat_degree_mul hp0 hq0] at hmnd, clear_except hmnd hmp hnq, contrapose hmnd, apply ne_of_lt, rw not_and_distrib at hmnd, cases hmnd, { exact add_lt_add_of_lt_of_le (lt_of_le_of_ne hmp (ne.symm hmnd)) hnq }, { exact add_lt_add_of_le_of_lt hmp (lt_of_le_of_ne hnq (ne.symm hmnd)) } }, obtain rfl | rfl : m = 0 ∨ n = 0, { rwa [pos_iff_ne_zero, pos_iff_ne_zero, imp_false, not_not, ← or_iff_not_imp_left] at hmn }, { exact or.inl (is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit hu hpmqn.1) }, { exact or.inr (is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit (by simpa only [mul_comm] using hu) hpmqn.2) } end⟩ end polynomial
0c218abff5cb3cfa8b0e414440a4434626d5b8c6
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/group_theory/subgroup.lean
77a9fccc8fe1d56232b6d806ad535a53d6332fd6
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
28,051
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, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro, Michael Howes -/ import group_theory.submonoid open set function variables {α : Type*} {β : Type*} {a a₁ a₂ b c: α} section group variables [group α] [add_group β] @[to_additive] lemma injective_mul {a : α} : injective ((*) a) := assume a₁ a₂ h, have a⁻¹ * a * a₁ = a⁻¹ * a * a₂, by rw [mul_assoc, mul_assoc, h], by rwa [inv_mul_self, one_mul, one_mul] at this /-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/ class is_add_subgroup (s : set β) extends is_add_submonoid s : Prop := (neg_mem {a} : a ∈ s → -a ∈ s) /-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/ @[to_additive is_add_subgroup] class is_subgroup (s : set α) extends is_submonoid s : Prop := (inv_mem {a} : a ∈ s → a⁻¹ ∈ s) instance additive.is_add_subgroup (s : set α) [is_subgroup s] : @is_add_subgroup (additive α) _ s := ⟨@is_subgroup.inv_mem _ _ _ _⟩ theorem additive.is_add_subgroup_iff {s : set α} : @is_add_subgroup (additive α) _ s ↔ is_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_subgroup.mk α _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by resetI; apply_instance⟩ instance multiplicative.is_subgroup (s : set β) [is_add_subgroup s] : @is_subgroup (multiplicative β) _ s := ⟨@is_add_subgroup.neg_mem _ _ _ _⟩ theorem multiplicative.is_subgroup_iff {s : set β} : @is_subgroup (multiplicative β) _ s ↔ is_add_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_add_subgroup.mk β _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by resetI; apply_instance⟩ @[to_additive add_group] instance subtype.group {s : set α} [is_subgroup s] : group s := by subtype_instance @[to_additive add_comm_group] instance subtype.comm_group {α : Type*} [comm_group α] {s : set α} [is_subgroup s] : comm_group s := by subtype_instance @[simp, to_additive] lemma is_subgroup.coe_inv {s : set α} [is_subgroup s] (a : s) : ((a⁻¹ : s) : α) = a⁻¹ := rfl @[simp] lemma is_subgroup.coe_gpow {s : set α} [is_subgroup s] (a : s) (n : ℤ) : ((a ^ n : s) : α) = a ^ n := by induction n; simp [is_submonoid.coe_pow a] @[simp] lemma is_add_subgroup.gsmul_coe {β : Type*} [add_group β] {s : set β} [is_add_subgroup s] (a : s) (n : ℤ) : ((gsmul n a : s) : β) = gsmul n a := by induction n; simp [is_add_submonoid.smul_coe a] attribute [to_additive gsmul_coe] is_subgroup.coe_gpow @[to_additive of_add_neg] theorem is_subgroup.of_div (s : set α) (one_mem : (1:α) ∈ s) (div_mem : ∀{a b:α}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s) : is_subgroup s := have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from assume a ha, have 1 * a⁻¹ ∈ s, from div_mem one_mem ha, by simpa, { inv_mem := inv_mem, mul_mem := assume a b ha hb, have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb), by simpa, one_mem := one_mem } theorem is_add_subgroup.of_sub (s : set β) (zero_mem : (0:β) ∈ s) (sub_mem : ∀{a b:β}, a ∈ s → b ∈ s → a - b ∈ s) : is_add_subgroup s := is_add_subgroup.of_add_neg s zero_mem (λ x y hx hy, sub_mem hx hy) @[to_additive] instance is_subgroup.inter (s₁ s₂ : set α) [is_subgroup s₁] [is_subgroup s₂] : is_subgroup (s₁ ∩ s₂) := { inv_mem := λ x hx, ⟨is_subgroup.inv_mem hx.1, is_subgroup.inv_mem hx.2⟩, ..is_submonoid.inter s₁ s₂ } @[to_additive is_add_subgroup_Union_of_directed] lemma is_subgroup_Union_of_directed {ι : Type*} [hι : nonempty ι] (s : ι → set α) [∀ i, is_subgroup (s i)] (directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) : is_subgroup (⋃i, s i) := { inv_mem := λ a ha, let ⟨i, hi⟩ := set.mem_Union.1 ha in set.mem_Union.2 ⟨i, is_subgroup.inv_mem hi⟩, to_is_submonoid := is_submonoid_Union_of_directed s directed } def gpowers (x : α) : set α := set.range ((^) x : ℤ → α) def gmultiples (x : β) : set β := set.range (λ i, gsmul i x) attribute [to_additive gmultiples] gpowers instance gpowers.is_subgroup (x : α) : is_subgroup (gpowers x) := { one_mem := ⟨(0:ℤ), by simp⟩, mul_mem := assume x₁ x₂ ⟨i₁, h₁⟩ ⟨i₂, h₂⟩, ⟨i₁ + i₂, by simp [gpow_add, *]⟩, inv_mem := assume x₀ ⟨i, h⟩, ⟨-i, by simp [h.symm]⟩ } instance gmultiples.is_add_subgroup (x : β) : is_add_subgroup (gmultiples x) := multiplicative.is_subgroup_iff.1 $ gpowers.is_subgroup _ attribute [to_additive is_add_subgroup] gpowers.is_subgroup lemma is_subgroup.gpow_mem {a : α} {s : set α} [is_subgroup s] (h : a ∈ s) : ∀{i:ℤ}, a ^ i ∈ s | (n : ℕ) := is_submonoid.pow_mem h | -[1+ n] := is_subgroup.inv_mem (is_submonoid.pow_mem h) lemma is_add_subgroup.gsmul_mem {a : β} {s : set β} [is_add_subgroup s] : a ∈ s → ∀{i:ℤ}, gsmul i a ∈ s := @is_subgroup.gpow_mem (multiplicative β) _ _ _ _ lemma gpowers_subset {a : α} {s : set α} [is_subgroup s] (h : a ∈ s) : gpowers a ⊆ s := λ x hx, match x, hx with _, ⟨i, rfl⟩ := is_subgroup.gpow_mem h end lemma gmultiples_subset {a : β} {s : set β} [is_add_subgroup s] (h : a ∈ s) : gmultiples a ⊆ s := @gpowers_subset (multiplicative β) _ _ _ _ h attribute [to_additive gmultiples_subset] gpowers_subset lemma mem_gpowers {a : α} : a ∈ gpowers a := ⟨1, by simp⟩ lemma mem_gmultiples {a : β} : a ∈ gmultiples a := ⟨1, by simp⟩ attribute [to_additive mem_gmultiples] mem_gpowers end group namespace is_subgroup open is_submonoid variables [group α] (s : set α) [is_subgroup s] @[to_additive] lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s := ⟨λ h, by simpa using inv_mem h, inv_mem⟩ @[to_additive] lemma mul_mem_cancel_left (h : a ∈ s) : b * a ∈ s ↔ b ∈ s := ⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩ @[to_additive] lemma mul_mem_cancel_right (h : a ∈ s) : a * b ∈ s ↔ b ∈ s := ⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩ end is_subgroup theorem is_add_subgroup.sub_mem {α} [add_group α] (s : set α) [is_add_subgroup s] (a b : α) (ha : a ∈ s) (hb : b ∈ s) : a - b ∈ s := is_add_submonoid.add_mem ha (is_add_subgroup.neg_mem hb) class normal_add_subgroup [add_group α] (s : set α) extends is_add_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : α, g + n - g ∈ s) @[to_additive normal_add_subgroup] class normal_subgroup [group α] (s : set α) extends is_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : α, g * n * g⁻¹ ∈ s) @[to_additive normal_add_subgroup_of_add_comm_group] lemma normal_subgroup_of_comm_group [comm_group α] (s : set α) [hs : is_subgroup s] : normal_subgroup s := { normal := λ n hn g, by rwa [mul_right_comm, mul_right_inv, one_mul], ..hs } instance additive.normal_add_subgroup [group α] (s : set α) [normal_subgroup s] : @normal_add_subgroup (additive α) _ s := ⟨@normal_subgroup.normal _ _ _ _⟩ theorem additive.normal_add_subgroup_iff [group α] {s : set α} : @normal_add_subgroup (additive α) _ s ↔ normal_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_subgroup.mk α _ _ (additive.is_add_subgroup_iff.1 h₁) @h₂, λ h, by resetI; apply_instance⟩ instance multiplicative.normal_subgroup [add_group α] (s : set α) [normal_add_subgroup s] : @normal_subgroup (multiplicative α) _ s := ⟨@normal_add_subgroup.normal _ _ _ _⟩ theorem multiplicative.normal_subgroup_iff [add_group α] {s : set α} : @normal_subgroup (multiplicative α) _ s ↔ normal_add_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_add_subgroup.mk α _ _ (multiplicative.is_subgroup_iff.1 h₁) @h₂, λ h, by resetI; apply_instance⟩ namespace is_subgroup variable [group α] -- Normal subgroup properties @[to_additive] lemma mem_norm_comm {s : set α} [normal_subgroup s] {a b : α} (hab : a * b ∈ s) : b * a ∈ s := have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from normal_subgroup.normal (a * b) hab a⁻¹, by simp at h; exact h @[to_additive] lemma mem_norm_comm_iff {s : set α} [normal_subgroup s] {a b : α} : a * b ∈ s ↔ b * a ∈ s := ⟨mem_norm_comm, mem_norm_comm⟩ /-- The trivial subgroup -/ @[to_additive] def trivial (α : Type*) [group α] : set α := {1} @[simp, to_additive] lemma mem_trivial [group α] {g : α} : g ∈ trivial α ↔ g = 1 := mem_singleton_iff @[to_additive] instance trivial_normal : normal_subgroup (trivial α) := by refine {..}; simp [trivial] {contextual := tt} @[to_additive] lemma eq_trivial_iff {H : set α} [is_subgroup H] : H = trivial α ↔ (∀ x ∈ H, x = (1 : α)) := by simp only [set.ext_iff, is_subgroup.mem_trivial]; exact ⟨λ h x, (h x).1, λ h x, ⟨h x, λ hx, hx.symm ▸ is_submonoid.one_mem H⟩⟩ @[to_additive] instance univ_subgroup : normal_subgroup (@univ α) := by refine {..}; simp @[to_additive add_center] def center (α : Type*) [group α] : set α := {z | ∀ g, g * z = z * g} @[to_additive mem_add_center] lemma mem_center {a : α} : a ∈ center α ↔ ∀g, g * a = a * g := iff.rfl @[to_additive add_center_normal] instance center_normal : normal_subgroup (center α) := { one_mem := by simp [center], mul_mem := assume a b ha hb g, by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc], inv_mem := assume a ha g, calc g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g] ... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp, normal := assume n ha g h, calc h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc] ... = g * g⁻¹ * n * h : by rw ha h; simp ... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] } @[to_additive add_normalizer] def normalizer (s : set α) : set α := {g : α | ∀ n, n ∈ s ↔ g * n * g⁻¹ ∈ s} @[to_additive normalizer_is_add_subgroup] instance normalizer_is_subgroup (s : set α) [is_subgroup s] : is_subgroup (normalizer s) := { one_mem := by simp [normalizer], mul_mem := λ a b (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) (hb : ∀ n, n ∈ s ↔ b * n * b⁻¹ ∈ s) n, by rw [mul_inv_rev, ← mul_assoc, mul_assoc a, mul_assoc a, ← ha, ← hb], inv_mem := λ a (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) n, by rw [ha (a⁻¹ * n * a⁻¹⁻¹)]; simp [mul_assoc] } @[to_additive subset_add_normalizer] lemma subset_normalizer (s : set α) [is_subgroup s] : s ⊆ normalizer s := λ g hg n, by rw [is_subgroup.mul_mem_cancel_left _ ((is_subgroup.inv_mem_iff _).2 hg), is_subgroup.mul_mem_cancel_right _ hg] /-- Every subgroup is a normal subgroup of its normalizer -/ @[to_additive add_normal_in_add_normalizer] instance normal_in_normalizer (s : set α) [is_subgroup s] : normal_subgroup (subtype.val ⁻¹' s : set (normalizer s)) := { one_mem := show (1 : α) ∈ s, from is_submonoid.one_mem _, mul_mem := λ a b ha hb, show (a * b : α) ∈ s, from is_submonoid.mul_mem ha hb, inv_mem := λ a ha, show (a⁻¹ : α) ∈ s, from is_subgroup.inv_mem ha, normal := λ a ha ⟨m, hm⟩, (hm a).1 ha } end is_subgroup -- Homomorphism subgroups namespace is_group_hom open is_submonoid is_subgroup open is_mul_hom (map_mul) variables [group α] [group β] @[to_additive] def ker (f : α → β) [is_group_hom f] : set α := preimage f (trivial β) @[to_additive] lemma mem_ker (f : α → β) [is_group_hom f] {x : α} : x ∈ ker f ↔ f x = 1 := mem_trivial @[to_additive] lemma one_ker_inv (f : α → β) [is_group_hom f] {a b : α} (h : f (a * b⁻¹) = 1) : f a = f b := begin rw [map_mul f, map_inv f] at h, rw [←inv_inv (f b), eq_inv_of_mul_eq_one h] end @[to_additive] lemma one_ker_inv' (f : α → β) [is_group_hom f] {a b : α} (h : f (a⁻¹ * b) = 1) : f a = f b := begin rw [map_mul f, map_inv f] at h, apply eq_of_inv_eq_inv, rw eq_inv_of_mul_eq_one h end @[to_additive] lemma inv_ker_one (f : α → β) [is_group_hom f] {a b : α} (h : f a = f b) : f (a * b⁻¹) = 1 := have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv], by rwa [←map_inv f, ←map_mul f] at this @[to_additive] lemma inv_ker_one' (f : α → β) [is_group_hom f] {a b : α} (h : f a = f b) : f (a⁻¹ * b) = 1 := have (f a)⁻¹ * f b = 1, by rw [h, mul_left_inv], by rwa [←map_inv f, ←map_mul f] at this @[to_additive] lemma one_iff_ker_inv (f : α → β) [is_group_hom f] (a b : α) : f a = f b ↔ f (a * b⁻¹) = 1 := ⟨inv_ker_one f, one_ker_inv f⟩ @[to_additive] lemma one_iff_ker_inv' (f : α → β) [is_group_hom f] (a b : α) : f a = f b ↔ f (a⁻¹ * b) = 1 := ⟨inv_ker_one' f, one_ker_inv' f⟩ @[to_additive] lemma inv_iff_ker (f : α → β) [w : is_group_hom f] (a b : α) : f a = f b ↔ a * b⁻¹ ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv _ _ _ @[to_additive] lemma inv_iff_ker' (f : α → β) [w : is_group_hom f] (a b : α) : f a = f b ↔ a⁻¹ * b ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv' _ _ _ @[to_additive image_add_subgroup] instance image_subgroup (f : α → β) [is_group_hom f] (s : set α) [is_subgroup s] : is_subgroup (f '' s) := { mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩, ⟨b₁ * b₂, mul_mem hb₁ hb₂, by simp [eq₁, eq₂, map_mul f]⟩, one_mem := ⟨1, one_mem s, map_one f⟩, inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, inv_mem hb, by rw map_inv f; simp *⟩ } @[to_additive range_add_subgroup] instance range_subgroup (f : α → β) [is_group_hom f] : is_subgroup (set.range f) := @set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ local attribute [simp] one_mem inv_mem mul_mem normal_subgroup.normal @[to_additive] instance preimage (f : α → β) [is_group_hom f] (s : set β) [is_subgroup s] : is_subgroup (f ⁻¹' s) := by refine {..}; simp [map_mul f, map_one f, map_inv f, @inv_mem β _ s] {contextual:=tt} @[to_additive] instance preimage_normal (f : α → β) [is_group_hom f] (s : set β) [normal_subgroup s] : normal_subgroup (f ⁻¹' s) := ⟨by simp [map_mul f, map_inv f] {contextual:=tt}⟩ @[to_additive] instance normal_subgroup_ker (f : α → β) [is_group_hom f] : normal_subgroup (ker f) := is_group_hom.preimage_normal f (trivial β) @[to_additive] lemma inj_of_trivial_ker (f : α → β) [is_group_hom f] (h : ker f = trivial α) : function.injective f := begin intros a₁ a₂ hfa, simp [ext_iff, ker, is_subgroup.trivial] at h, have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact inv_ker_one f hfa, rw [eq_inv_of_mul_eq_one ha, inv_inv a₂] end @[to_additive] lemma trivial_ker_of_inj (f : α → β) [is_group_hom f] (h : function.injective f) : ker f = trivial α := set.ext $ assume x, iff.intro (assume hx, suffices f x = f 1, by simpa using h this, by simp [map_one f]; rwa [mem_ker] at hx) (by simp [mem_ker, is_group_hom.map_one f] {contextual := tt}) @[to_additive] lemma inj_iff_trivial_ker (f : α → β) [is_group_hom f] : function.injective f ↔ ker f = trivial α := ⟨trivial_ker_of_inj f, inj_of_trivial_ker f⟩ @[to_additive] lemma trivial_ker_iff_eq_one (f : α → β) [is_group_hom f] : ker f = trivial α ↔ ∀ x, f x = 1 → x = 1 := by rw set.ext_iff; simp [ker]; exact ⟨λ h x hx, (h x).1 hx, λ h x, ⟨h x, λ hx, by rw [hx, map_one f]⟩⟩ end is_group_hom @[to_additive is_add_group_hom] instance subtype_val.is_group_hom [group α] {s : set α} [is_subgroup s] : is_group_hom (subtype.val : s → α) := { ..subtype_val.is_monoid_hom } @[to_additive is_add_group_hom] instance coe.is_group_hom [group α] {s : set α} [is_subgroup s] : is_group_hom (coe : s → α) := { ..subtype_val.is_monoid_hom } @[to_additive is_add_group_hom] instance subtype_mk.is_group_hom [group α] [group β] {s : set α} [is_subgroup s] (f : β → α) [is_group_hom f] (h : ∀ x, f x ∈ s) : is_group_hom (λ x, (⟨f x, h x⟩ : s)) := { ..subtype_mk.is_monoid_hom f h } @[to_additive is_add_group_hom] instance set_inclusion.is_group_hom [group α] {s t : set α} [is_subgroup s] [is_subgroup t] (h : s ⊆ t) : is_group_hom (set.inclusion h) := subtype_mk.is_group_hom _ _ namespace add_group variables [add_group α] inductive in_closure (s : set α) : α → Prop | basic {a : α} : a ∈ s → in_closure a | zero : in_closure 0 | neg {a : α} : in_closure a → in_closure (-a) | add {a b : α} : in_closure a → in_closure b → in_closure (a + b) end add_group namespace group open is_submonoid is_subgroup variables [group α] {s : set α} @[to_additive] inductive in_closure (s : set α) : α → Prop | basic {a : α} : a ∈ s → in_closure a | one : in_closure 1 | inv {a : α} : in_closure a → in_closure a⁻¹ | mul {a b : α} : in_closure a → in_closure b → in_closure (a * b) /-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ @[to_additive] def closure (s : set α) : set α := {a | in_closure s a } @[to_additive] lemma mem_closure {a : α} : a ∈ s → a ∈ closure s := in_closure.basic @[to_additive is_add_subgroup] instance closure.is_subgroup (s : set α) : is_subgroup (closure s) := { one_mem := in_closure.one s, mul_mem := assume a b, in_closure.mul, inv_mem := assume a, in_closure.inv } @[to_additive] theorem subset_closure {s : set α} : s ⊆ closure s := λ a, mem_closure @[to_additive] theorem closure_subset {s t : set α} [is_subgroup t] (h : s ⊆ t) : closure s ⊆ t := assume a ha, by induction ha; simp [h _, *, one_mem, mul_mem, inv_mem_iff] @[to_additive] lemma closure_subset_iff (s t : set α) [is_subgroup t] : closure s ⊆ t ↔ s ⊆ t := ⟨assume h b ha, h (mem_closure ha), assume h b ha, closure_subset h ha⟩ @[to_additive] theorem closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_subset $ set.subset.trans h subset_closure @[simp, to_additive closure_add_subgroup] lemma closure_subgroup (s : set α) [is_subgroup s] : closure s = s := set.subset.antisymm (closure_subset $ set.subset.refl s) subset_closure @[to_additive] theorem exists_list_of_mem_closure {s : set α} {a : α} (h : a ∈ closure s) : (∃l:list α, (∀x∈l, x ∈ s ∨ x⁻¹ ∈ s) ∧ l.prod = a) := in_closure.rec_on h (λ x hxs, ⟨[x], list.forall_mem_singleton.2 $ or.inl hxs, one_mul _⟩) ⟨[], list.forall_mem_nil _, rfl⟩ (λ x _ ⟨L, HL1, HL2⟩, ⟨L.reverse.map has_inv.inv, λ x hx, let ⟨y, hy1, hy2⟩ := list.exists_of_mem_map hx in hy2 ▸ or.imp id (by rw [inv_inv]; exact id) (HL1 _ $ list.mem_reverse.1 hy1).symm, HL2 ▸ list.rec_on L one_inv.symm (λ hd tl ih, by rw [list.reverse_cons, list.map_append, list.prod_append, ih, list.map_singleton, list.prod_cons, list.prod_nil, mul_one, list.prod_cons, mul_inv_rev])⟩) (λ x y hx hy ⟨L1, HL1, HL2⟩ ⟨L2, HL3, HL4⟩, ⟨L1 ++ L2, list.forall_mem_append.2 ⟨HL1, HL3⟩, by rw [list.prod_append, HL2, HL4]⟩) @[to_additive] lemma image_closure [group β] (f : α → β) [is_group_hom f] (s : set α) : f '' closure s = closure (f '' s) := le_antisymm begin rintros _ ⟨x, hx, rfl⟩, apply in_closure.rec_on hx; intros, { solve_by_elim [subset_closure, set.mem_image_of_mem] }, { rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem }, { rw [is_group_hom.map_inv f], apply is_subgroup.inv_mem, assumption }, { rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] } end (closure_subset $ set.image_subset _ subset_closure) @[to_additive] theorem mclosure_subset {s : set α} : monoid.closure s ⊆ closure s := monoid.closure_subset $ subset_closure @[to_additive] theorem mclosure_inv_subset {s : set α} : monoid.closure (has_inv.inv ⁻¹' s) ⊆ closure s := monoid.closure_subset $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx) @[to_additive] theorem closure_eq_mclosure {s : set α} : closure s = monoid.closure (s ∪ has_inv.inv ⁻¹' s) := set.subset.antisymm (@closure_subset _ _ _ (monoid.closure (s ∪ has_inv.inv ⁻¹' s)) { inv_mem := λ x hx, monoid.in_closure.rec_on hx (λ x hx, or.cases_on hx (λ hx, monoid.subset_closure $ or.inr $ show x⁻¹⁻¹ ∈ s, from (inv_inv x).symm ▸ hx) (λ hx, monoid.subset_closure $ or.inl hx)) ((@one_inv α _).symm ▸ is_submonoid.one_mem _) (λ x y hx hy ihx ihy, (mul_inv_rev x y).symm ▸ is_submonoid.mul_mem ihy ihx) } (set.subset.trans (set.subset_union_left _ _) monoid.subset_closure)) (monoid.closure_subset $ set.union_subset subset_closure $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx)) @[to_additive] theorem mem_closure_union_iff {α : Type*} [comm_group α] {s t : set α} {x : α} : x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x := begin simp only [closure_eq_mclosure, monoid.mem_closure_union_iff, exists_prop, preimage_union], split, { rintro ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, rfl⟩, refine ⟨_, ⟨_, hys, _, hzs, rfl⟩, _, ⟨_, hyt, _, hzt, rfl⟩, _⟩, rw [mul_assoc, mul_assoc, mul_left_comm zs], refl }, { rintro ⟨_, ⟨ys, hys, zs, hzs, rfl⟩, _, ⟨yt, hyt, zt, hzt, rfl⟩, rfl⟩, refine ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, _⟩, rw [mul_assoc, mul_assoc, mul_left_comm yt], refl } end @[to_additive gmultiples_eq_closure] theorem gpowers_eq_closure {a : α} : gpowers a = closure {a} := subset.antisymm (gpowers_subset $ mem_closure $ by simp) (closure_subset $ by simp [mem_gpowers]) end group namespace is_subgroup variable [group α] @[to_additive] lemma trivial_eq_closure : trivial α = group.closure ∅ := subset.antisymm (by simp [set.subset_def, is_submonoid.one_mem]) (group.closure_subset $ by simp) end is_subgroup /-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. -/ namespace group variables {s : set α} [group α] /-- Given an element a, conjugates a is the set of conjugates. -/ def conjugates (a : α) : set α := {b | is_conj a b} lemma mem_conjugates_self {a : α} : a ∈ conjugates a := is_conj_refl _ /-- 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 α) : set α := ⋃ a ∈ s, conjugates a lemma mem_conjugates_of_set_iff {x : α} : x ∈ conjugates_of_set s ↔ ∃ a : α, a ∈ s ∧ is_conj a x := set.mem_bUnion_iff theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s := λ (x : α) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩ theorem conjugates_of_set_mono {s t : set α} (h : s ⊆ t) : conjugates_of_set s ⊆ conjugates_of_set t := set.bUnion_subset_bUnion_left h lemma conjugates_subset {t : set α} [normal_subgroup t] {a : α} (h : a ∈ t) : conjugates a ⊆ t := λ x ⟨c,w⟩, begin have H := normal_subgroup.normal a h c, rwa ←w, end theorem conjugates_of_set_subset {s t : set α} [normal_subgroup t] (h : s ⊆ t) : conjugates_of_set s ⊆ t := set.bUnion_subset (λ x H, conjugates_subset (h H)) /-- The set of conjugates of s is closed under conjugation. -/ lemma conj_mem_conjugates_of_set {x c : α} : x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) := λ H, begin rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩, exact mem_conjugates_of_set_iff.2 ⟨a, h₁, is_conj_trans h₂ ⟨c,rfl⟩⟩, end /-- 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 α) : set α := 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 /-- The normal closure of a set is a subgroup. -/ instance normal_closure.is_subgroup (s : set α) : is_subgroup (normal_closure s) := closure.is_subgroup (conjugates_of_set s) /-- The normal closure of s is a normal subgroup. -/ instance normal_closure.is_normal : normal_subgroup (normal_closure s) := ⟨ λ n h g, begin induction h with x hx x hx ihx x y hx hy ihx ihy, {exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx))}, {simpa using (normal_closure.is_subgroup s).one_mem}, {rw ←conj_inv, exact (is_subgroup.inv_mem ihx)}, {rw ←conj_mul, exact (is_submonoid.mul_mem ihx ihy)}, end ⟩ /-- The normal closure of s is the smallest normal subgroup containing s. -/ theorem normal_closure_subset {s t : set α} [normal_subgroup t] (h : s ⊆ t) : normal_closure s ⊆ t := λ a w, begin induction w with x hx x hx ihx x y hx hy ihx ihy, {exact (conjugates_of_set_subset h $ hx)}, {exact is_submonoid.one_mem t}, {exact is_subgroup.inv_mem ihx}, {exact is_submonoid.mul_mem ihx ihy} end lemma normal_closure_subset_iff {s t : set α} [normal_subgroup t] : s ⊆ t ↔ normal_closure s ⊆ t := ⟨normal_closure_subset, set.subset.trans (subset_normal_closure)⟩ theorem normal_closure_mono {s t : set α} : s ⊆ t → normal_closure s ⊆ normal_closure t := λ h, normal_closure_subset (set.subset.trans h (subset_normal_closure)) end group section simple_group class simple_group (α : Type*) [group α] : Prop := (simple : ∀ (N : set α) [normal_subgroup N], N = is_subgroup.trivial α ∨ N = set.univ) class simple_add_group (α : Type*) [add_group α] : Prop := (simple : ∀ (N : set α) [normal_add_subgroup N], N = is_add_subgroup.trivial α ∨ N = set.univ) attribute [to_additive simple_add_group] simple_group theorem additive.simple_add_group_iff [group α] : simple_add_group (additive α) ↔ simple_group α := ⟨λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.2 h)⟩, λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.1 h)⟩⟩ instance additive.simple_add_group [group α] [simple_group α] : simple_add_group (additive α) := additive.simple_add_group_iff.2 (by apply_instance) theorem multiplicative.simple_group_iff [add_group α] : simple_group (multiplicative α) ↔ simple_add_group α := ⟨λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.2 h)⟩, λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.1 h)⟩⟩ instance multiplicative.simple_group [add_group α] [simple_add_group α] : simple_group (multiplicative α) := multiplicative.simple_group_iff.2 (by apply_instance) lemma simple_group_of_surjective [group α] [group β] [simple_group α] (f : α → β) [is_group_hom f] (hf : function.surjective f) : simple_group β := ⟨λ H iH, have normal_subgroup (f ⁻¹' H), by resetI; apply_instance, begin resetI, cases simple_group.simple (f ⁻¹' H) with h h, { refine or.inl (is_subgroup.eq_trivial_iff.2 (λ x hx, _)), cases hf x with y hy, rw ← hy at hx, rw [← hy, is_subgroup.eq_trivial_iff.1 h y hx, is_group_hom.map_one f] }, { refine or.inr (set.eq_univ_of_forall (λ x, _)), cases hf x with y hy, rw set.eq_univ_iff_forall at h, rw ← hy, exact h y } end⟩ lemma simple_add_group_of_surjective [add_group α] [add_group β] [simple_add_group α] (f : α → β) [is_add_group_hom f] (hf : function.surjective f) : simple_add_group β := multiplicative.simple_group_iff.1 (@simple_group_of_surjective (multiplicative α) (multiplicative β) _ _ _ f _ hf) attribute [to_additive simple_add_group_of_surjective] simple_group_of_surjective end simple_group
b1bdd5bde8d1ca63d67f9dcec6d6767854b26a61
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/stage0/src/Init/Data/Nat/Basic.lean
ebf7439bf3619eefecca1f315524445d850bf797
[ "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
14,354
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import Init.Core universes u namespace Nat set_option bootstrap.gen_matcher_code false in @[extern "lean_nat_sub"] protected def sub : (@& Nat) → (@& Nat) → Nat | a, 0 => a | a, b+1 => pred (Nat.sub a b) instance : Sub Nat := ⟨Nat.sub⟩ @[specialize] def foldAux {α : Type u} (f : Nat → α → α) (s : Nat) : Nat → α → α | 0, a => a | succ n, a => foldAux f s n (f (s - (succ n)) a) @[inline] def fold {α : Type u} (f : Nat → α → α) (n : Nat) (init : α) : α := foldAux f n n init @[inline] def foldRev {α : Type u} (f : Nat → α → α) (n : Nat) (init : α) : α := let rec @[specialize] loop | 0, a => a | succ n, a => loop n (f n a) loop n init @[specialize] def anyAux (f : Nat → Bool) (s : Nat) : Nat → Bool | 0 => false | succ n => f (s - (succ n)) || anyAux f s n /- `any f n = true` iff there is `i in [0, n-1]` s.t. `f i = true` -/ @[inline] def any (f : Nat → Bool) (n : Nat) : Bool := anyAux f n n @[inline] def all (f : Nat → Bool) (n : Nat) : Bool := !any (fun i => !f i) n @[inline] def repeat {α : Type u} (f : α → α) (n : Nat) (a : α) : α := let rec @[specialize] loop | 0, a => a | succ n, a => loop n (f a) loop n a /- Nat.add theorems -/ protected theorem zeroAdd : ∀ (n : Nat), 0 + n = n | 0 => rfl | n+1 => congrArg succ (Nat.zeroAdd n) theorem succAdd : ∀ (n m : Nat), (succ n) + m = succ (n + m) | n, 0 => rfl | n, m+1 => congrArg succ (succAdd n m) theorem addSucc (n m : Nat) : n + succ m = succ (n + m) := rfl protected theorem addZero (n : Nat) : n + 0 = n := rfl theorem addOne (n : Nat) : n + 1 = succ n := rfl theorem succEqAddOne (n : Nat) : succ n = n + 1 := rfl protected theorem addComm : ∀ (n m : Nat), n + m = m + n | n, 0 => Eq.symm (Nat.zeroAdd n) | n, m+1 => by have succ (n + m) = succ (m + n) by apply congrArg; apply Nat.addComm rw [succAdd m n] apply this protected theorem addAssoc : ∀ (n m k : Nat), (n + m) + k = n + (m + k) | n, m, 0 => rfl | n, m, succ k => congrArg succ (Nat.addAssoc n m k) protected theorem addLeftComm (n m k : Nat) : n + (m + k) = m + (n + k) := by rw [← Nat.addAssoc, Nat.addComm n m, Nat.addAssoc] exact rfl protected theorem addRightComm (n m k : Nat) : (n + m) + k = (n + k) + m := by rw [Nat.addAssoc, Nat.addComm m k, ← Nat.addAssoc] exact rfl protected theorem addLeftCancel : ∀ {n m k : Nat}, n + m = n + k → m = k | 0, m, k, h => Nat.zeroAdd m ▸ Nat.zeroAdd k ▸ h | succ n, m, k, h => have n+m = n+k from have succ (n + m) = succ (n + k) from succAdd n m ▸ succAdd n k ▸ h Nat.noConfusion this id Nat.addLeftCancel this protected theorem addRightCancel {n m k : Nat} (h : n + m = k + m) : n = k := have m + n = m + k from Nat.addComm n m ▸ Nat.addComm k m ▸ h Nat.addLeftCancel this /- Nat.mul theorems -/ protected theorem mulZero (n : Nat) : n * 0 = 0 := rfl theorem mulSucc (n m : Nat) : n * succ m = n * m + n := rfl protected theorem zeroMul : ∀ (n : Nat), 0 * n = 0 | 0 => rfl | succ n => mulSucc 0 n ▸ (Nat.zeroMul n).symm ▸ rfl theorem succMul : ∀ (n m : Nat), (succ n) * m = (n * m) + m | n, 0 => rfl | n, succ m => by have succ (n * m + m + n) = succ (n * m + n + m) from congrArg succ (Nat.addRightComm ..) rw [mulSucc n m, mulSucc (succ n) m, succMul n m] assumption protected theorem mulComm : ∀ (n m : Nat), n * m = m * n | n, 0 => (Nat.zeroMul n).symm ▸ (Nat.mulZero n).symm ▸ rfl | n, succ m => (mulSucc n m).symm ▸ (succMul m n).symm ▸ (Nat.mulComm n m).symm ▸ rfl protected theorem mulOne : ∀ (n : Nat), n * 1 = n := Nat.zeroAdd protected theorem oneMul (n : Nat) : 1 * n = n := Nat.mulComm n 1 ▸ Nat.mulOne n protected theorem leftDistrib : ∀ (n m k : Nat), n * (m + k) = n * m + n * k | 0, m, k => (Nat.zeroMul (m + k)).symm ▸ (Nat.zeroMul m).symm ▸ (Nat.zeroMul k).symm ▸ rfl | succ n, m, k => have h₁ : succ n * (m + k) = n * (m + k) + (m + k) from succMul .. have h₂ : n * (m + k) + (m + k) = (n * m + n * k) + (m + k) from Nat.leftDistrib n m k ▸ rfl have h₃ : (n * m + n * k) + (m + k) = n * m + (n * k + (m + k)) from Nat.addAssoc .. have h₄ : n * m + (n * k + (m + k)) = n * m + (m + (n * k + k)) from congrArg (fun x => n*m + x) (Nat.addLeftComm ..) have h₅ : n * m + (m + (n * k + k)) = (n * m + m) + (n * k + k) from (Nat.addAssoc ..).symm have h₆ : (n * m + m) + (n * k + k) = (n * m + m) + succ n * k from succMul n k ▸ rfl have h₇ : (n * m + m) + succ n * k = succ n * m + succ n * k from succMul n m ▸ rfl (((((h₁.trans h₂).trans h₃).trans h₄).trans h₅).trans h₆).trans h₇ protected theorem rightDistrib (n m k : Nat) : (n + m) * k = n * k + m * k := have h₁ : (n + m) * k = k * (n + m) from Nat.mulComm .. have h₂ : k * (n + m) = k * n + k * m from Nat.leftDistrib .. have h₃ : k * n + k * m = n * k + k * m from Nat.mulComm n k ▸ rfl have h₄ : n * k + k * m = n * k + m * k from Nat.mulComm m k ▸ rfl ((h₁.trans h₂).trans h₃).trans h₄ protected theorem mulAssoc : ∀ (n m k : Nat), (n * m) * k = n * (m * k) | n, m, 0 => rfl | n, m, succ k => have h₁ : n * m * succ k = n * m * (k + 1) from rfl have h₂ : n * m * (k + 1) = (n * m * k) + n * m * 1 from Nat.leftDistrib .. have h₃ : (n * m * k) + n * m * 1 = (n * m * k) + n * m by rw [Nat.mulOne (n*m)]; exact rfl have h₄ : (n * m * k) + n * m = (n * (m * k)) + n * m by rw [Nat.mulAssoc n m k]; exact rfl have h₅ : (n * (m * k)) + n * m = n * (m * k + m) from (Nat.leftDistrib n (m*k) m).symm have h₆ : n * (m * k + m) = n * (m * succ k) from Nat.mulSucc m k ▸ rfl ((((h₁.trans h₂).trans h₃).trans h₄).trans h₅).trans h₆ /- Inequalities -/ theorem succLtSucc {n m : Nat} : n < m → succ n < succ m := succLeSucc theorem ltSuccOfLe {n m : Nat} : n ≤ m → n < succ m := succLeSucc protected theorem subZero (n : Nat) : n - 0 = n := rfl theorem succSubSuccEqSub (n m : Nat) : succ n - succ m = n - m := by induction m | zero => exact rfl | succ m ih => apply congrArg pred ih theorem notSuccLeSelf (n : Nat) : ¬succ n ≤ n := by induction n | zero => intro h; apply notSuccLeZero 0 h | succ n ih => intro h; exact ih (leOfSuccLeSucc h) protected theorem ltIrrefl (n : Nat) : ¬n < n := notSuccLeSelf n theorem predLe : ∀ (n : Nat), pred n ≤ n | zero => rfl | succ n => leSucc _ theorem predLt : ∀ {n : Nat}, n ≠ 0 → pred n < n | zero, h => absurd rfl h | succ n, h => ltSuccOfLe (Nat.leRefl _) theorem subLe (n m : Nat) : n - m ≤ n := by induction m | zero => exact Nat.leRefl (n - 0) | succ m ih => apply Nat.leTrans (predLe (n - m)) ih theorem subLt : ∀ {n m : Nat}, 0 < n → 0 < m → n - m < n | 0, m, h1, h2 => absurd h1 (Nat.ltIrrefl 0) | n+1, 0, h1, h2 => absurd h2 (Nat.ltIrrefl 0) | n+1, m+1, h1, h2 => Eq.symm (succSubSuccEqSub n m) ▸ show n - m < succ n from ltSuccOfLe (subLe n m) protected theorem ltOfLtOfLe {n m k : Nat} : n < m → m ≤ k → n < k := Nat.leTrans protected theorem ltOfLtOfEq {n m k : Nat} : n < m → m = k → n < k := fun h₁ h₂ => h₂ ▸ h₁ protected theorem leOfEq {n m : Nat} (p : n = m) : n ≤ m := p ▸ Nat.leRefl n theorem leOfSuccLe {n m : Nat} (h : succ n ≤ m) : n ≤ m := Nat.leTrans (leSucc n) h protected theorem leOfLt {n m : Nat} (h : n < m) : n ≤ m := leOfSuccLe h def lt.step {n m : Nat} : n < m → n < succ m := leStep def succPos := zeroLtSucc theorem eqZeroOrPos : ∀ (n : Nat), n = 0 ∨ n > 0 | 0 => Or.inl rfl | n+1 => Or.inr (succPos _) protected theorem ltOfLeOfLt {n m k : Nat} (h₁ : n ≤ m) : m < k → n < k := Nat.leTrans (succLeSucc h₁) def lt.base (n : Nat) : n < succ n := Nat.leRefl (succ n) theorem ltSuccSelf (n : Nat) : n < succ n := lt.base n protected theorem leTotal (m n : Nat) : m ≤ n ∨ n ≤ m := match Nat.ltOrGe m n with | Or.inl h => Or.inl (Nat.leOfLt h) | Or.inr h => Or.inr h protected theorem ltOfLeAndNe {m n : Nat} (h₁ : m ≤ n) (h₂ : m ≠ n) : m < n := match Nat.eqOrLtOfLe h₁ with | Or.inl h => absurd h h₂ | Or.inr h => h theorem eqZeroOfLeZero {n : Nat} (h : n ≤ 0) : n = 0 := Nat.leAntisymm h (zeroLe _) theorem ltOfSuccLt {n m : Nat} : succ n < m → n < m := leOfSuccLe theorem ltOfSuccLtSucc {n m : Nat} : succ n < succ m → n < m := leOfSuccLeSucc theorem ltOfSuccLe {n m : Nat} (h : succ n ≤ m) : n < m := h theorem succLeOfLt {n m : Nat} (h : n < m) : succ n ≤ m := h theorem ltOrEqOrLeSucc {m n : Nat} (h : m ≤ succ n) : m ≤ n ∨ m = succ n := Decidable.byCases (fun (h' : m = succ n) => Or.inr h') (fun (h' : m ≠ succ n) => have m < succ n from Nat.ltOfLeAndNe h h' have succ m ≤ succ n from succLeOfLt this Or.inl (leOfSuccLeSucc this)) theorem leAddRight : ∀ (n k : Nat), n ≤ n + k | n, 0 => Nat.leRefl n | n, k+1 => leSuccOfLe (leAddRight n k) theorem leAddLeft (n m : Nat): n ≤ m + n := Nat.addComm n m ▸ leAddRight n m theorem le.dest : ∀ {n m : Nat}, n ≤ m → Exists (fun k => n + k = m) | zero, zero, h => ⟨0, rfl⟩ | zero, succ n, h => ⟨succ n, Nat.addComm 0 (succ n) ▸ rfl⟩ | succ n, zero, h => Bool.noConfusion h | succ n, succ m, h => have n ≤ m from h have Exists (fun k => n + k = m) from dest this match this with | ⟨k, h⟩ => ⟨k, show succ n + k = succ m from ((succAdd n k).symm ▸ h ▸ rfl)⟩ theorem le.intro {n m k : Nat} (h : n + k = m) : n ≤ m := h ▸ leAddRight n k protected theorem notLeOfGt {n m : Nat} (h : n > m) : ¬ n ≤ m := fun h₁ => match Nat.ltOrGe n m with | Or.inl h₂ => absurd (Nat.ltTrans h h₂) (Nat.ltIrrefl _) | Or.inr h₂ => have Heq : n = m from Nat.leAntisymm h₁ h₂ absurd (@Eq.subst _ _ _ _ Heq h) (Nat.ltIrrefl m) theorem gtOfNotLe {n m : Nat} (h : ¬ n ≤ m) : n > m := match Nat.ltOrGe m n with | Or.inl h₁ => h₁ | Or.inr h₁ => absurd h₁ h protected theorem addLeAddLeft {n m : Nat} (h : n ≤ m) (k : Nat) : k + n ≤ k + m := match le.dest h with | ⟨w, hw⟩ => have h₁ : k + n + w = k + (n + w) from Nat.addAssoc .. have h₂ : k + (n + w) = k + m from congrArg _ hw le.intro $ h₁.trans h₂ protected theorem addLeAddRight {n m : Nat} (h : n ≤ m) (k : Nat) : n + k ≤ m + k := by rw [Nat.addComm n k, Nat.addComm m k] apply Nat.addLeAddLeft assumption protected theorem addLtAddLeft {n m : Nat} (h : n < m) (k : Nat) : k + n < k + m := ltOfSuccLe (addSucc k n ▸ Nat.addLeAddLeft (succLeOfLt h) k) protected theorem addLtAddRight {n m : Nat} (h : n < m) (k : Nat) : n + k < m + k := Nat.addComm k m ▸ Nat.addComm k n ▸ Nat.addLtAddLeft h k protected theorem zeroLtOne : 0 < (1:Nat) := zeroLtSucc 0 theorem addLeAdd {a b c d : Nat} (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d := Nat.leTrans (Nat.addLeAddRight h₁ c) (Nat.addLeAddLeft h₂ b) theorem addLtAdd {a b c d : Nat} (h₁ : a < b) (h₂ : c < d) : a + c < b + d := Nat.ltTrans (Nat.addLtAddRight h₁ c) (Nat.addLtAddLeft h₂ b) /- Basic theorems for comparing numerals -/ theorem natZeroEqZero : Nat.zero = 0 := rfl protected theorem oneNeZero : 1 ≠ (0 : Nat) := fun h => Nat.noConfusion h protected theorem zeroNeOne : 0 ≠ (1 : Nat) := fun h => Nat.noConfusion h theorem succNeZero (n : Nat) : succ n ≠ 0 := fun h => Nat.noConfusion h /- mul + order -/ theorem mulLeMulLeft {n m : Nat} (k : Nat) (h : n ≤ m) : k * n ≤ k * m := match le.dest h with | ⟨l, hl⟩ => have k * n + k * l = k * m from Nat.leftDistrib k n l ▸ hl.symm ▸ rfl le.intro this theorem mulLeMulRight {n m : Nat} (k : Nat) (h : n ≤ m) : n * k ≤ m * k := Nat.mulComm k m ▸ Nat.mulComm k n ▸ mulLeMulLeft k h protected theorem mulLeMul {n₁ m₁ n₂ m₂ : Nat} (h₁ : n₁ ≤ n₂) (h₂ : m₁ ≤ m₂) : n₁ * m₁ ≤ n₂ * m₂ := Nat.leTrans (mulLeMulRight _ h₁) (mulLeMulLeft _ h₂) protected theorem mulLtMulOfPosLeft {n m k : Nat} (h : n < m) (hk : k > 0) : k * n < k * m := Nat.ltOfLtOfLe (Nat.addLtAddLeft hk _) (Nat.mulSucc k n ▸ Nat.mulLeMulLeft k (succLeOfLt h)) protected theorem mulLtMulOfPosRight {n m k : Nat} (h : n < m) (hk : k > 0) : n * k < m * k := Nat.mulComm k m ▸ Nat.mulComm k n ▸ Nat.mulLtMulOfPosLeft h hk protected theorem mulPos {n m : Nat} (ha : n > 0) (hb : m > 0) : n * m > 0 := have h : 0 * m < n * m from Nat.mulLtMulOfPosRight ha hb Nat.zeroMul m ▸ h /- power -/ theorem powSucc (n m : Nat) : n^(succ m) = n^m * n := rfl theorem powZero (n : Nat) : n^0 = 1 := rfl theorem powLePowOfLeLeft {n m : Nat} (h : n ≤ m) : ∀ (i : Nat), n^i ≤ m^i | 0 => Nat.leRefl _ | succ i => Nat.mulLeMul (powLePowOfLeLeft h i) h theorem powLePowOfLeRight {n : Nat} (hx : n > 0) {i : Nat} : ∀ {j}, i ≤ j → n^i ≤ n^j | 0, h => have i = 0 from eqZeroOfLeZero h this.symm ▸ Nat.leRefl _ | succ j, h => match ltOrEqOrLeSucc h with | Or.inl h => show n^i ≤ n^j * n from have n^i * 1 ≤ n^j * n from Nat.mulLeMul (powLePowOfLeRight hx h) hx Nat.mulOne (n^i) ▸ this | Or.inr h => h.symm ▸ Nat.leRefl _ theorem posPowOfPos {n : Nat} (m : Nat) (h : 0 < n) : 0 < n^m := powLePowOfLeRight h (Nat.zeroLe _) /- min/max -/ protected def min (n m : Nat) : Nat := if n ≤ m then n else m protected def max (n m : Nat) : Nat := if n ≤ m then m else n end Nat namespace Prod @[inline] def foldI {α : Type u} (f : Nat → α → α) (i : Nat × Nat) (a : α) : α := Nat.foldAux f i.2 (i.2 - i.1) a @[inline] def anyI (f : Nat → Bool) (i : Nat × Nat) : Bool := Nat.anyAux f i.2 (i.2 - i.1) @[inline] def allI (f : Nat → Bool) (i : Nat × Nat) : Bool := Nat.anyAux (fun a => !f a) i.2 (i.2 - i.1) end Prod
6af54dbb2a6874c8188e7b909e792305438f9abc
c6ff7f82fbfbc4ed06a4edc8adf7239c6aba6945
/src/ring_theory/algebra_tower.lean
fb3c6c57e7e31db759d5b2af3fe253159e412a7a
[ "Apache-2.0" ]
permissive
jpablo/mathlib
f14c9042506cce59176d3d22b457f190e78938a3
fd453cf1569b8f9396476287b046d216e92224e4
refs/heads/master
1,690,443,975,886
1,631,043,219,000
1,631,043,219,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,493
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.algebra.restrict_scalars import algebra.algebra.tower import algebra.invertible import linear_algebra.basis import ring_theory.adjoin.basic import ring_theory.polynomial.tower /-! # Towers of algebras We set up the basic theory of algebra towers. An algebra tower A/S/R is expressed by having instances of `algebra A S`, `algebra R S`, `algebra R A` and `is_scalar_tower R S A`, the later asserting the compatibility condition `(r • s) • a = r • (s • a)`. In `field_theory/tower.lean` we use this to prove the tower law for finite extensions, that if `R` and `S` are both fields, then `[A:R] = [A:S] [S:A]`. In this file we prepare the main lemma: if `{bi | i ∈ I}` is an `R`-basis of `S` and `{cj | j ∈ J}` is a `S`-basis of `A`, then `{bi cj | i ∈ I, j ∈ J}` is an `R`-basis of `A`. This statement does not require the base rings to be a field, so we also generalize the lemma to rings in this file. -/ open_locale pointwise universes u v w u₁ variables (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) namespace is_scalar_tower section semiring variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] [algebra R A] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] variables (R S A B) /-- Suppose that `R -> S -> A` is a tower of algebras. If an element `r : R` is invertible in `S`, then it is invertible in `A`. -/ def invertible.algebra_tower (r : R) [invertible (algebra_map R S r)] : invertible (algebra_map R A r) := invertible.copy (invertible.map (algebra_map S A : S →* A) (algebra_map R S r)) (algebra_map R A r) (by rw [ring_hom.coe_monoid_hom, is_scalar_tower.algebra_map_apply R S A]) /-- A natural number that is invertible when coerced to `R` is also invertible when coerced to any `R`-algebra. -/ def invertible_algebra_coe_nat (n : ℕ) [inv : invertible (n : R)] : invertible (n : A) := by { haveI : invertible (algebra_map ℕ R n) := inv, exact invertible.algebra_tower ℕ R A n } end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [comm_semiring B] variables [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B] end comm_semiring end is_scalar_tower namespace algebra theorem adjoin_algebra_map' {R : Type u} {S : Type v} {A : Type w} [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] (s : set S) : adjoin R (algebra_map S (restrict_scalars R S A) '' s) = (adjoin R s).map ((algebra.of_id S (restrict_scalars R S A)).restrict_scalars R) := le_antisymm (adjoin_le $ set.image_subset_iff.2 $ λ y hy, ⟨y, subset_adjoin hy, rfl⟩) (subalgebra.map_le.2 $ adjoin_le $ λ y hy, subset_adjoin ⟨y, hy, rfl⟩) theorem adjoin_algebra_map (R : Type u) (S : Type v) (A : Type w) [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (s : set S) : adjoin R (algebra_map S A '' s) = subalgebra.map (adjoin R s) (is_scalar_tower.to_alg_hom R S A) := le_antisymm (adjoin_le $ set.image_subset_iff.2 $ λ y hy, ⟨y, subset_adjoin hy, rfl⟩) (subalgebra.map_le.2 $ adjoin_le $ λ y hy, subset_adjoin ⟨y, hy, rfl⟩) lemma adjoin_res (C D E : Type*) [comm_semiring C] [comm_semiring D] [comm_semiring E] [algebra C D] [algebra C E] [algebra D E] [is_scalar_tower C D E] (S : set E) : (algebra.adjoin D S).res C = ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)).under (algebra.adjoin ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)) S) := begin suffices : set.range (algebra_map D E) = set.range (algebra_map ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)) E), { ext x, change x ∈ subsemiring.closure (_ ∪ S) ↔ x ∈ subsemiring.closure (_ ∪ S), rw this }, ext x, split, { rintros ⟨y, hy⟩, exact ⟨⟨algebra_map D E y, ⟨y, ⟨algebra.mem_top, rfl⟩⟩⟩, hy⟩ }, { rintros ⟨⟨y, ⟨z, ⟨h0, h1⟩⟩⟩, h2⟩, exact ⟨z, eq.trans h1 h2⟩ }, end lemma adjoin_res_eq_adjoin_res (C D E F : Type*) [comm_semiring C] [comm_semiring D] [comm_semiring E] [comm_semiring F] [algebra C D] [algebra C E] [algebra C F] [algebra D F] [algebra E F] [is_scalar_tower C D F] [is_scalar_tower C E F] {S : set D} {T : set E} (hS : algebra.adjoin C S = ⊤) (hT : algebra.adjoin C T = ⊤) : (algebra.adjoin E (algebra_map D F '' S)).res C = (algebra.adjoin D (algebra_map E F '' T)).res C := by { rw [adjoin_res, adjoin_res, ←hS, ←hT, ←algebra.adjoin_image, ←algebra.adjoin_image, ←alg_hom.coe_to_ring_hom, ←alg_hom.coe_to_ring_hom, is_scalar_tower.coe_to_alg_hom, is_scalar_tower.coe_to_alg_hom, ←adjoin_union_eq_under, ←adjoin_union_eq_under, set.union_comm] } end algebra section open_locale classical lemma algebra.fg_trans' {R S A : Type*} [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (hRS : (⊤ : subalgebra R S).fg) (hSA : (⊤ : subalgebra S A).fg) : (⊤ : subalgebra R A).fg := let ⟨s, hs⟩ := hRS, ⟨t, ht⟩ := hSA in ⟨s.image (algebra_map S A) ∪ t, by rw [finset.coe_union, finset.coe_image, algebra.adjoin_union_eq_under, algebra.adjoin_algebra_map, hs, algebra.map_top, is_scalar_tower.range_under_adjoin, ht, subalgebra.res_top]⟩ end section algebra_map_coeffs variables {R} (A) {ι M : Type*} [comm_semiring R] [semiring A] [add_comm_monoid M] variables [algebra R A] [module A M] [module R M] [is_scalar_tower R A M] variables (b : basis ι R M) (h : function.bijective (algebra_map R A)) /-- If `R` and `A` have a bijective `algebra_map R A` and act identically on `M`, then a basis for `M` as `R`-module is also a basis for `M` as `R'`-module. -/ @[simps] noncomputable def basis.algebra_map_coeffs : basis ι A M := b.map_coeffs (ring_equiv.of_bijective _ h) (λ c x, by simp) lemma basis.algebra_map_coeffs_apply (i : ι) : b.algebra_map_coeffs A h i = b i := b.map_coeffs_apply _ _ _ @[simp] lemma basis.coe_algebra_map_coeffs : (b.algebra_map_coeffs A h : ι → M) = b := b.coe_map_coeffs _ _ end algebra_map_coeffs section ring open finsupp open_locale big_operators classical universes v₁ w₁ variables {R S A} variables [comm_ring R] [ring S] [add_comm_group A] variables [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] theorem linear_independent_smul {ι : Type v₁} {b : ι → S} {ι' : Type w₁} {c : ι' → A} (hb : linear_independent R b) (hc : linear_independent S c) : linear_independent R (λ p : ι × ι', b p.1 • c p.2) := begin rw linear_independent_iff' at hb hc, rw linear_independent_iff'', rintros s g hg hsg ⟨i, k⟩, by_cases hik : (i, k) ∈ s, { have h1 : ∑ i in (s.image prod.fst).product (s.image prod.snd), g i • b i.1 • c i.2 = 0, { rw ← hsg, exact (finset.sum_subset finset.subset_product $ λ p _ hp, show g p • b p.1 • c p.2 = 0, by rw [hg p hp, zero_smul]).symm }, rw [finset.sum_product, finset.sum_comm] at h1, simp_rw [← smul_assoc, ← finset.sum_smul] at h1, exact hb _ _ (hc _ _ h1 k (finset.mem_image_of_mem _ hik)) i (finset.mem_image_of_mem _ hik) }, exact hg _ hik end /-- `basis.smul (b : basis ι R S) (c : basis ι S A)` is the `R`-basis on `A` where the `(i, j)`th basis vector is `b i • c j`. -/ noncomputable def basis.smul {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) : basis (ι × ι') R A := basis.of_repr ((c.repr.restrict_scalars R) ≪≫ₗ ((finsupp.lcongr (equiv.refl _) b.repr) ≪≫ₗ ((finsupp_prod_lequiv R).symm ≪≫ₗ ((finsupp.lcongr (equiv.prod_comm ι' ι) (linear_equiv.refl _ _)))))) @[simp] theorem basis.smul_repr {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) (x ij): (b.smul c).repr x ij = b.repr (c.repr x ij.2) ij.1 := by simp [basis.smul] theorem basis.smul_repr_mk {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) (x i j): (b.smul c).repr x (i, j) = b.repr (c.repr x j) i := b.smul_repr c x (i, j) @[simp] theorem basis.smul_apply {ι : Type v₁} {ι' : Type w₁} (b : basis ι R S) (c : basis ι' S A) (ij) : (b.smul c) ij = b ij.1 • c ij.2 := begin obtain ⟨i, j⟩ := ij, rw basis.apply_eq_iff, ext ⟨i', j'⟩, rw [basis.smul_repr, linear_equiv.map_smul, basis.repr_self, finsupp.smul_apply, finsupp.single_apply], dsimp only, split_ifs with hi, { simp [hi, finsupp.single_apply] }, { simp [hi] }, end end ring section artin_tate variables (C : Type*) variables [comm_ring A] [comm_ring B] [comm_ring C] variables [algebra A B] [algebra B C] [algebra A C] [is_scalar_tower A B C] open finset submodule open_locale classical lemma exists_subalgebra_of_fg (hAC : (⊤ : subalgebra A C).fg) (hBC : (⊤ : submodule B C).fg) : ∃ B₀ : subalgebra A B, B₀.fg ∧ (⊤ : submodule B₀ C).fg := begin cases hAC with x hx, cases hBC with y hy, have := hy, simp_rw [eq_top_iff', mem_span_finset] at this, choose f hf, let s : finset B := (finset.product (x ∪ (y * y)) y).image (function.uncurry f), have hsx : ∀ (xi ∈ x) (yj ∈ y), f xi yj ∈ s := λ xi hxi yj hyj, show function.uncurry f (xi, yj) ∈ s, from mem_image_of_mem _ $ mem_product.2 ⟨mem_union_left _ hxi, hyj⟩, have hsy : ∀ (yi yj yk ∈ y), f (yi * yj) yk ∈ s := λ yi yj yk hyi hyj hyk, show function.uncurry f (yi * yj, yk) ∈ s, from mem_image_of_mem _ $ mem_product.2 ⟨mem_union_right _ $ finset.mul_mem_mul hyi hyj, hyk⟩, have hxy : ∀ xi ∈ x, xi ∈ span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) := λ xi hxi, hf xi ▸ sum_mem _ (λ yj hyj, smul_mem (span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C)) ⟨f xi yj, algebra.subset_adjoin $ hsx xi hxi yj hyj⟩ (subset_span $ mem_insert_of_mem hyj)), have hyy : span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) * span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) ≤ span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C), { rw [span_mul_span, span_le, coe_insert], rintros _ ⟨yi, yj, rfl | hyi, rfl | hyj, rfl⟩, { rw mul_one, exact subset_span (set.mem_insert _ _) }, { rw one_mul, exact subset_span (set.mem_insert_of_mem _ hyj) }, { rw mul_one, exact subset_span (set.mem_insert_of_mem _ hyi) }, { rw ← hf (yi * yj), exact set_like.mem_coe.2 (sum_mem _ $ λ yk hyk, smul_mem (span (algebra.adjoin A (↑s : set B)) (insert 1 ↑y : set C)) ⟨f (yi * yj) yk, algebra.subset_adjoin $ hsy yi yj yk hyi hyj hyk⟩ (subset_span $ set.mem_insert_of_mem _ hyk : yk ∈ _)) } }, refine ⟨algebra.adjoin A (↑s : set B), subalgebra.fg_adjoin_finset _, insert 1 y, _⟩, refine restrict_scalars_injective A _ _ _, rw [restrict_scalars_top, eq_top_iff, ← algebra.top_to_submodule, ← hx, algebra.adjoin_eq_span, span_le], refine λ r hr, submonoid.closure_induction hr (λ c hc, hxy c hc) (subset_span $ mem_insert_self _ _) (λ p q hp hq, hyy $ submodule.mul_mem_mul hp hq) end /-- Artin--Tate lemma: if A ⊆ B ⊆ C is a chain of subrings of commutative rings, and A is noetherian, and C is algebra-finite over A, and C is module-finite over B, then B is algebra-finite over A. References: Atiyah--Macdonald Proposition 7.8; Stacks 00IS; Altman--Kleiman 16.17. -/ theorem fg_of_fg_of_fg [is_noetherian_ring A] (hAC : (⊤ : subalgebra A C).fg) (hBC : (⊤ : submodule B C).fg) (hBCi : function.injective (algebra_map B C)) : (⊤ : subalgebra A B).fg := let ⟨B₀, hAB₀, hB₀C⟩ := exists_subalgebra_of_fg A B C hAC hBC in algebra.fg_trans' (B₀.fg_top.2 hAB₀) $ subalgebra.fg_of_submodule_fg $ have is_noetherian_ring B₀, from is_noetherian_ring_of_fg hAB₀, have is_noetherian B₀ C, by exactI is_noetherian_of_fg_of_noetherian' hB₀C, by exactI fg_of_injective (is_scalar_tower.to_alg_hom B₀ B C).to_linear_map (linear_map.ker_eq_bot.2 hBCi) end artin_tate section alg_hom_tower variables {A} {C D : Type*} [comm_semiring A] [comm_semiring C] [comm_semiring D] [algebra A C] [algebra A D] variables (f : C →ₐ[A] D) (B) [comm_semiring B] [algebra A B] [algebra B C] [is_scalar_tower A B C] /-- Restrict the domain of an `alg_hom`. -/ def alg_hom.restrict_domain : B →ₐ[A] D := f.comp (is_scalar_tower.to_alg_hom A B C) /-- Extend the scalars of an `alg_hom`. -/ def alg_hom.extend_scalars : @alg_hom B C D _ _ _ _ (f.restrict_domain B).to_ring_hom.to_algebra := { commutes' := λ _, rfl .. f } variables {B} /-- `alg_hom`s from the top of a tower are equivalent to a pair of `alg_hom`s. -/ def alg_hom_equiv_sigma : (C →ₐ[A] D) ≃ Σ (f : B →ₐ[A] D), @alg_hom B C D _ _ _ _ f.to_ring_hom.to_algebra := { to_fun := λ f, ⟨f.restrict_domain B, f.extend_scalars B⟩, inv_fun := λ fg, let alg := fg.1.to_ring_hom.to_algebra in by exactI fg.2.restrict_scalars A, left_inv := λ f, by { dsimp only, ext, refl }, right_inv := begin rintros ⟨⟨f, _, _, _, _, _⟩, g, _, _, _, _, hg⟩, have : f = λ x, g (algebra_map B C x) := by { ext, exact (hg x).symm }, subst this, refl, end } end alg_hom_tower
773248e44ae83023263b7c5fd26dcde68cb8b207
9338c56dfd6ceacc3e5e63e32a7918cfec5d5c69
/src/sheaves/locally_ringed_space.lean
32e9a5db34986a6442c7e51e3b42c9b030450eab
[]
no_license
Project-Reykjavik/lean-scheme
7322eefce504898ba33737970be89dc751108e2b
6d3ec18fecfd174b79d0ce5c85a783f326dd50f6
refs/heads/master
1,669,426,172,632
1,578,284,588,000
1,578,284,588,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,146
lean
/- Locally ringed spaces. https://stacks.math.columbia.edu/tag/01HA -/ import topology.basic import ring_theory.localization import sheaves.presheaf_maps import sheaves.presheaf_of_rings_maps import sheaves.presheaf_of_rings import sheaves.sheaf_of_rings import sheaves.stalk_of_rings universes u v w open topological_space -- Locally ringed spaces. section locally_ringed_spaces structure locally_ringed_space (X : Type u) [topological_space X] := (O : sheaf_of_rings X) (Hstalks : ∀ x, is_local_ring (stalk_of_rings O.F x)) instance locally_ringed_space.local_ring (X : Type u) [topological_space X] (OX : locally_ringed_space X) (x : X) : local_ring (stalk_of_rings OX.O.F x) := local_of_is_local_ring $ OX.Hstalks x -- Morphism of locally ringed spaces. structure morphism {X : Type u} {Y : Type v} [topological_space X] [topological_space Y] (OX : locally_ringed_space X) (OY : locally_ringed_space Y) := (f : X → Y) (Hf : continuous f) (fO : presheaf_of_rings.fmap Hf OX.O.F OY.O.F) (Hstalks : ∀ x s, is_unit (presheaf_of_rings.fmap.induced OX.O.F OY.O.F fO x s) → is_unit s) infix `⟶`:80 := morphism section morphism variables {A : Type u} [topological_space A] variables {B : Type v} [topological_space B] variables {C : Type w} [topological_space C] variable {OA : locally_ringed_space A} variable {OB : locally_ringed_space B} variable {OC : locally_ringed_space C} def comp (F : morphism OA OB) (G : morphism OB OC) : morphism OA OC := { f := G.f ∘ F.f, Hf := continuous.comp G.Hf F.Hf, fO := presheaf.fmap.comp F.fO G.fO, Hstalks := begin intros x s H, apply G.Hstalks, apply F.Hstalks, convert H, unfold presheaf.fmap.comp, unfold presheaf_of_rings.fmap.induced, unfold presheaf.fmap.induced, dsimp, apply quotient.induction_on s, rintros ⟨U, HxU, s⟩; simp, use [opens.comap F.Hf (opens.comap G.Hf U), HxU]; dsimp, use [(λ x Hx, Hx), (λ x Hx, Hx)], refl, end } infixl `⊚`:80 := comp def locally_ringed_space.id (OX : locally_ringed_space A) : morphism OX OX := { f := id, Hf := continuous_id, fO := presheaf.fmap.id OX.O.F.to_presheaf, Hstalks := begin intros x a, unfold presheaf.fmap.id, unfold presheaf_of_rings.fmap.induced, unfold presheaf.fmap.induced, dsimp, apply quotient.induction_on a, rintros ⟨U, HxU, s⟩; simp, intros H, have HU : opens.comap continuous_id U = U, { unfold opens.comap, apply subtype.eq; dsimp, refl, }, convert H, { use HU.symm, }, { dsimp only [subtype.coe_mk], have Hid := OX.O.F.to_presheaf.Hid' U s, rw ←Hid, congr, { exact HU.symm, }, { exact Hid.symm, }, }, end } structure iso (OX : locally_ringed_space A) (OY : locally_ringed_space B) := (mor : OX ⟶ OY) (inv : OY ⟶ OX) (mor_inv_id : mor ⊚ inv = locally_ringed_space.id OX) (inv_mor_id : inv ⊚ mor = locally_ringed_space.id OY) infix `≅`:80 := λ OX OY, nonempty (iso OX OY) end morphism end locally_ringed_spaces
e435892f347484c072c47473e18df68900802ea9
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/project_1_a_decrire/lean-scheme-submission/src/sheaves/presheaf_maps.lean
22cfa56df5a8847c27a483a75dd3e7f5cd0707c2
[]
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
3,802
lean
/- Continuous maps and presheaves. https://stacks.math.columbia.edu/tag/008C -/ import to_mathlib.opens import sheaves.presheaf import sheaves.stalk universes u v w open topological_space variables {α : Type u} [topological_space α] variables {β : Type v} [topological_space β] variables {f : α → β} (Hf : continuous f) -- f induces a functor PSh(α) ⟶ PSh(β). namespace presheaf section pushforward def pushforward (F : presheaf α) : presheaf β := { F := λ U, F (opens.comap Hf U), res := λ U V HVU, F.res (opens.comap Hf U) (opens.comap Hf V) (opens.comap_mono Hf V U HVU), Hid := λ U, F.Hid (opens.comap Hf U), Hcomp := λ U V W HWV HVU, F.Hcomp (opens.comap Hf U) (opens.comap Hf V) (opens.comap Hf W) (opens.comap_mono Hf W V HWV) (opens.comap_mono Hf V U HVU), } def pushforward.morphism (F G : presheaf α) (φ : F ⟶ G) : pushforward Hf F ⟶ pushforward Hf G := { map := λ U, φ.map (opens.comap Hf U), commutes := λ U V HVU, φ.commutes (opens.comap Hf U) (opens.comap Hf V) (opens.comap_mono Hf V U HVU), } end pushforward -- f induces a functor PSh(β) ⟶ PSh(α). Simplified to the case when f is 'nice'. section pullback variable (Hf' : ∀ (U : opens α), is_open (f '' U)) def pullback (F : presheaf β) : presheaf α := { F := λ U, F (opens.map Hf' U), res := λ U V HVU, F.res (opens.map Hf' U) (opens.map Hf' V) (opens.map_mono Hf' V U HVU), Hid := λ U, F.Hid (opens.map Hf' U), Hcomp := λ U V W HWV HVU, F.Hcomp (opens.map Hf' U) (opens.map Hf' V) (opens.map Hf' W) (opens.map_mono Hf' W V HWV) (opens.map_mono Hf' V U HVU), } def pullback.morphism (F G : presheaf β) (φ : F ⟶ G) : pullback Hf' F ⟶ pullback Hf' G := { map := λ U, φ.map (opens.map Hf' U), commutes := λ U V HVU, φ.commutes (opens.map Hf' U) (opens.map Hf' V) (opens.map_mono Hf' V U HVU), } end pullback -- f induces a `map` from a presheaf on β to a presheaf on α. structure fmap (F : presheaf α) (G : presheaf β) := (map : ∀ (U), G U → F (opens.comap Hf U)) (commutes : ∀ (U V) (HVU : V ⊆ U), (map V) ∘ (G.res U V HVU) = (F.res (opens.comap Hf U) (opens.comap Hf V) (opens.comap_mono Hf V U HVU)) ∘ (map U)) namespace fmap variables {γ : Type w} [topological_space γ] variables {g : β → γ} {Hg : continuous g} variable {Hf} def comp {F : presheaf α} {G : presheaf β} {H : presheaf γ} (f_ : fmap Hf F G) (g_ : fmap Hg G H) : fmap (continuous.comp Hg Hf) F H := { map := λ U, (f_.map (opens.comap Hg U)) ∘ (g_.map U), commutes := begin intros U V HVU, rw function.comp.assoc _ _ (H.res _ _ _), rw g_.commutes, rw ←function.comp.assoc _ _ (g_.map _), rw f_.commutes, refl, end, } def id (F : presheaf α) : fmap continuous_id F F := { map := λ U, begin have HUU : opens.comap continuous_id U ⊆ U, intros x Hx, dsimp [opens.comap] at Hx, exact Hx, exact (F.res U (opens.comap continuous_id U) HUU), end, commutes := begin intros U V HUV, iterate 2 { rw ←F.Hcomp, }, end, } -- Induced map on stalks. def induced (F : presheaf α) (G : presheaf β) (f' : fmap Hf F G) (x : α) : stalk G (f x) → stalk F x := begin intros Us, let g : stalk.elem G (f x) → stalk F x := λ Us, (⟦⟨opens.comap Hf Us.U, Us.HxU, f'.map Us.U Us.s⟩⟧), apply quotient.lift_on Us g, rintros a b ⟨V, HxV, HVaU, HVbU, Hres⟩, apply quotient.sound, use [opens.comap Hf V, HxV], use [set.preimage_mono HVaU, set.preimage_mono HVbU], have Ha := congr_fun (f'.commutes a.U V HVaU) a.s, have Hb := congr_fun (f'.commutes b.U V HVbU) b.s, dsimp only [function.comp] at *, erw [←Ha, ←Hb, Hres], end end fmap end presheaf
3b1c260abbb6560bcd3d7db13043c0e153b6c215
78269ad0b3c342b20786f60690708b6e328132b0
/src/library_dev/topology/topological_space.lean
37b455f56a93d61bd9d56a15a7611d61ab2c5b77
[]
no_license
dselsam/library_dev
e74f46010fee9c7b66eaa704654cad0fcd2eefca
1b4e34e7fb067ea5211714d6d3ecef5132fc8218
refs/heads/master
1,610,372,841,675
1,497,014,421,000
1,497,014,421,000
86,526,137
0
0
null
1,490,752,133,000
1,490,752,132,000
null
UTF-8
Lean
false
false
25,793
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Theory of topological spaces. -/ import ..algebra.lattice.filter open set filter lattice universes u v w structure topological_space (α : Type u) := (open' : set α → Prop) (open_univ : open' univ) (open_inter : ∀s t, open' s → open' t → open' (s ∩ t)) (open_sUnion : ∀s, (∀t∈s, open' t) → open' (⋃₀ s)) attribute [class] topological_space section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a a₁ a₂ : α} {s s₁ s₂ : set α} lemma topological_space_eq {f g : topological_space α} (h' : f^.open' = g^.open') : f = g := begin cases f with a, cases g with b, assert h : a = b, assumption, clear h', subst h end section variables [t : topological_space α] include t /- open -/ def open' (s : set α) : Prop := topological_space.open' t s @[simp] lemma open_univ : open' (univ : set α) := topological_space.open_univ t lemma open_inter (h₁ : open' s₁) (h₂ : open' s₂) : open' (s₁ ∩ s₂) := topological_space.open_inter t s₁ s₂ h₁ h₂ lemma open_sUnion {s : set (set α)} (h : ∀t ∈ s, open' t) : open' (⋃₀ s) := topological_space.open_sUnion t s h end variables [topological_space α] lemma open_Union {f : ι → set α} (h : ∀i, open' (f i)) : open' (⋃i, f i) := open_sUnion $ take t ⟨i, (heq : t = f i)⟩, heq^.symm ▸ h i @[simp] lemma open_empty : open' (∅ : set α) := have open' (⋃₀ ∅ : set α), from open_sUnion (take a, false.elim), by simp at this; assumption /- closed -/ def closed (s : set α) : Prop := open' (-s) @[simp] lemma closed_empty : closed (∅ : set α) := by simp [closed] @[simp] lemma closed_univ : closed (univ : set α) := by simp [closed] lemma closed_union : closed s₁ → closed s₂ → closed (s₁ ∪ s₂) := by simp [closed]; exact open_inter lemma closed_sInter {s : set (set α)} : (∀t ∈ s, closed t) → closed (⋂₀ s) := by simp [closed, compl_sInter]; exact take h, open_Union $ take t, open_Union $ take ht, h t ht lemma closed_Inter {f : ι → set α} (h : ∀i, closed (f i)) : closed (⋂i, f i ) := closed_sInter $ take t ⟨i, (heq : t = f i)⟩, heq^.symm ▸ h i @[simp] lemma closed_compl_iff_open {s : set α} : open' (-s) ↔ closed s := by refl @[simp] lemma open_compl_iff_closed {s : set α} : closed (-s) ↔ open' s := by rw [-closed_compl_iff_open, compl_compl] lemma open_diff {s t : set α} (h₁ : open' s) (h₂ : closed t) : open' (s - t) := open_inter h₁ $ closed_compl_iff_open^.mpr h₂ /- interior -/ def interior (s : set α) : set α := ⋃₀ {t | open' t ∧ t ⊆ s} @[simp] lemma open_interior {s : set α} : open' (interior s) := open_sUnion $ take t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ take t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : open' t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma interior_eq_of_open {s : set α} (h : 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 ↔ open' s := ⟨take h, h ▸ open_interior, interior_eq_of_open⟩ lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : open' s) : s ⊆ interior t ↔ s ⊆ t := ⟨take h, subset.trans h interior_subset, take 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) open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := interior_eq_of_open open_empty @[simp] lemma interior_univ : interior (univ : set α) = univ := interior_eq_of_open open_univ @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := interior_eq_of_open open_interior @[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) $ by simp [open_inter]) lemma interior_union_closed_of_interior_empty {s t : set α} (h₁ : closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from take x ⟨u, ⟨(hu₁ : open' u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u - s ⊆ t, from take x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u - s ⊆ interior t, by simp [subset_interior_iff_subset_of_open, this, open_diff hu₁ h₁], have u - s ⊆ ∅, by rw [h₂] at this; assumption, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this open_interior) (interior_mono $ subset_union_left _ _) /- closure -/ def closure (s : set α) : set α := ⋂₀ {t | closed t ∧ s ⊆ t} @[simp] lemma closed_closure {s : set α} : closed (closure s) := closed_sInter $ take t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ take t ⟨h₁, h₂⟩, h₂ lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma closure_eq_of_closed {s : set α} (h : closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma closure_eq_iff_closed {s : set α} : closure s = s ↔ closed s := ⟨take h, h ▸ closed_closure, closure_eq_of_closed⟩ lemma closure_subset_iff_subset_of_closed {s t : set α} (h₁ : closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, take h, closure_minimal h h₁⟩ lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) closed_closure @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := closure_eq_of_closed closed_empty @[simp] lemma closure_univ : closure (univ : set α) = univ := closure_eq_of_closed closed_univ @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := closure_eq_of_closed closed_closure @[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) $ by simp [closed_union]) (union_subset (closure_mono $ subset_union_left _ _) (closure_mono $ subset_union_right _ _)) 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 simp [interior, closure], rw [compl_sUnion, compl_image_set_of], simp [neg_subset_neg_iff_subset] end @[simp] lemma interior_compl_eq {s : set α} : interior (- s) = - closure s := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl_eq {s : set α} : closure (- s) = - interior s := by simp [closure_eq_compl_interior_compl] /- neighbourhood filter -/ def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ open' s}, principal s) lemma nhds_sets {a : α} : (nhds a)^.sets = {s | ∃t⊆s, open' t ∧ a ∈ t} := calc (nhds a)^.sets = (⋃s∈{s : set α| a ∈ s ∧ open' s}, (principal s)^.sets) : infi_sets_eq' begin simp, exact take x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨_, ⟨open_inter hx₁ hy₁, ⟨hx₂, hy₂⟩⟩, ⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩ end ⟨univ, by simp⟩ ... = {s | ∃t⊆s, open' t ∧ a ∈ t} : le_antisymm (supr_le $ take i, supr_le $ take ⟨hi₁, hi₂⟩ t ht, ⟨i, ht, hi₂, hi₁⟩) (take t ⟨i, hi₁, hi₂, hi₃⟩, begin simp; exact ⟨i, hi₂, hi₁, hi₃⟩ end) lemma map_nhds {a : α} {f : α → β} : map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ open' s}, principal (image f s)) := calc map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ open' s}, map f (principal s)) : map_binfi_eq begin simp, exact take x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨_, ⟨open_inter hx₁ hy₁, ⟨hx₂, hy₂⟩⟩, ⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩ end ⟨univ, by simp⟩ ... = _ : by simp lemma mem_nhds_sets_iff {a : α} {s : set α} : s ∈ (nhds a)^.sets ↔ ∃t⊆s, open' t ∧ a ∈ t := by simp [nhds_sets] lemma mem_nhds_sets {a : α} {s : set α} (hs : open' s) (ha : a ∈ s) : s ∈ (nhds a)^.sets := by simp [nhds_sets]; exact ⟨s, hs, subset.refl _, ha⟩ lemma return_le_nhds : return ≤ (nhds : α → filter α) := take a, le_infi $ take s, le_infi $ take ⟨h₁, _⟩, principal_mono^.mpr $ by simp [h₁] @[simp] lemma nhds_neq_bot {a : α} : nhds a ≠ ⊥ := suppose nhds a = ⊥, have return a = (⊥ : filter α), from lattice.bot_unique $ this ▸ return_le_nhds a, return_neq_bot this lemma interior_eq_nhds {s : set α} : interior s = {a | nhds a ≤ principal s} := set.ext $ by simp [interior, nhds_sets] lemma open_iff_nhds {s : set α} : open' s ↔ (∀a∈s, nhds a ≤ principal s) := calc open' s ↔ interior s = s : by rw [interior_eq_iff_open] ... ↔ s ⊆ interior s : ⟨take h, by simph [subset.refl], subset.antisymm interior_subset⟩ ... ↔ (∀a∈s, nhds a ≤ principal s) : by rw [interior_eq_nhds]; refl lemma closure_eq_nhds {s : set α} : closure s = {a | nhds a ⊓ principal s ≠ ⊥} := calc closure s = - interior (- s) : closure_eq_compl_interior_compl ... = {a | ¬ nhds a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl ... = {a | nhds a ⊓ principal s ≠ ⊥} : set.ext $ take a, not_congr (inf_eq_bot_iff_le_compl (show principal s ⊔ principal (-s) = ⊤, by simp [principal_univ]) (by simp))^.symm lemma closed_iff_nhds {s : set α} : closed s ↔ (∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s) := calc closed s ↔ closure s = s : by rw [closure_eq_iff_closed] ... ↔ closure s ⊆ s : ⟨take h, by simph [subset.refl], take h, subset.antisymm h subset_closure⟩ ... ↔ (∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite def locally_finite (f : β → set α) := ∀x:α, ∃t∈(nhds x)^.sets, finite {i | f i ∩ t ≠ ∅ } theorem not_eq_empty_iff_exists {s : set α} : ¬ (s = ∅) ↔ ∃ x, x ∈ s := ⟨exists_mem_of_ne_empty, take ⟨x, (hx : x ∈ s)⟩ h_eq, by rw [h_eq] at hx; assumption⟩ lemma closed_Union_of_locally_finite {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, closed (f i)) : closed (⋃i, f i) := open_iff_nhds^.mpr $ take a, assume h : a ∉ (⋃i, f i), have ∀i, a ∈ -f i, from take i hi, by simp at h; exact h ⟨i, hi⟩, have ∀i, - f i ∈ (nhds a).sets, by rw [nhds_sets]; exact take i, ⟨- f i, subset.refl _, h₂ i, this i⟩, let ⟨t, h_sets, (h_fin : finite {i | f i ∩ t ≠ ∅ })⟩ := h₁ a in calc nhds a ≤ principal (t ∩ (⋂ i∈{i | f i ∩ t ≠ ∅ }, - f i)) : begin simp, apply @filter.inter_mem_sets _ (nhds a) _ _ h_sets, apply @filter.Inter_mem_sets _ _ (nhds a) _ _ h_fin, exact take i h, this i end ... ≤ principal (- ⋃i, f i) : begin simp, intro x, simp [not_eq_empty_iff_exists], exact take ⟨xt, ht⟩ i xfi, ht i ⟨x, xt, xfi⟩ xfi end end locally_finite section compact def compact (s : set α) := ∀{f}, f ≠ ⊥ → f ≤ principal s → ∃a∈s, f ⊓ nhds a ≠ ⊥ lemma compact_adherence_nhdset {s t : set α} {f : filter α} (hs : compact s) (hf₂ : f ≤ principal s) (ht₁ : open' t) (ht₂ : ∀a∈s, nhds a ⊓ f ≠ ⊥ → a ∈ t) : t ∈ f.sets := classical.by_cases mem_sets_of_neq_bot $ suppose f ⊓ principal (- t) ≠ ⊥, let ⟨a, ha, (hfa : f ⊓ principal (-t) ⊓ nhds a ≠ ⊥)⟩ := hs this $ inf_le_left_of_le hf₂ in have a ∈ t, from ht₂ a ha $ neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_left, have nhds a ⊓ principal (-t) ≠ ⊥, from neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_right, have ∀s∈(nhds a ⊓ principal (-t)).sets, s ≠ ∅, from forall_sets_neq_empty_iff_neq_bot.mpr this, have false, from this _ ⟨t, mem_nhds_sets ht₁ ‹a ∈ t ›, -t, subset.refl _, subset.refl _⟩ (by simp), by contradiction lemma compact_iff_ultrafilter_le_nhds {s : set α} : compact s ↔ (∀f, ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a) := ⟨assume hs : compact s, take f hf hfs, let ⟨a, ha, h⟩ := hs hf.left hfs in ⟨a, ha, le_of_ultrafilter hf h⟩, assume hs : (∀f, ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a), take f hf hfs, let ⟨a, ha, (h : ultrafilter_of f ≤ nhds a)⟩ := hs (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs) in have ultrafilter_of f ⊓ nhds a ≠ ⊥, by simp [inf_of_le_left, h]; exact (ultrafilter_ultrafilter_of hf).left, ⟨a, ha, neq_bot_of_le_neq_bot this (inf_le_inf ultrafilter_of_le (le_refl _))⟩⟩ lemma finite_subcover_of_compact {s : set α} {c : set (set α)} (hs : compact s) (hc₁ : ∀t∈c, open' t) (hc₂ : s ⊆ ⋃₀ c) : ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c' := classical.by_contradiction $ take h, have h : ∀{c'}, c' ⊆ c → finite c' → ¬ s ⊆ ⋃₀ c', from take c' h₁ h₂ h₃, h ⟨c', h₁, h₂, h₃⟩, let f : filter α := (⨅c':{c' : set (set α) // c' ⊆ c ∧ finite c'}, principal (s - ⋃₀ c')), ⟨a, ha⟩ := @exists_mem_of_ne_empty α s (take h', h (empty_subset _) finite.empty $ h'.symm ▸ empty_subset _) in have f ≠ ⊥, from infi_neq_bot_of_directed ⟨a⟩ (take ⟨c₁, hc₁, hc'₁⟩ ⟨c₂, hc₂, hc'₂⟩, ⟨⟨c₁ ∪ c₂, union_subset hc₁ hc₂, finite_union hc'₁ hc'₂⟩, principal_mono.mpr $ diff_right_antimono $ sUnion_mono $ subset_union_left _ _, principal_mono.mpr $ diff_right_antimono $ sUnion_mono $ subset_union_right _ _⟩) (take ⟨c', hc'₁, hc'₂⟩, by simp [diff_neq_empty]; exact h hc'₁ hc'₂), have f ≤ principal s, from infi_le_of_le ⟨∅, empty_subset _, finite.empty⟩ $ show principal (s - ⋃₀∅) ≤ principal s, by simp; exact subset.refl s, let ⟨a, ha, (h : f ⊓ nhds a ≠ ⊥)⟩ := hs ‹f ≠ ⊥› this, ⟨t, ht₁, (ht₂ : a ∈ t)⟩ := hc₂ ha in have f ≤ principal (-t), from infi_le_of_le ⟨{t}, by simp [ht₁], finite_insert finite.empty⟩ $ principal_mono.mpr $ show s - ⋃₀{t} ⊆ - t, begin simp; exact take x ⟨_, hnt⟩, hnt end, have closed (- t), from closed_compl_iff_open.mp $ by simp; exact hc₁ t ht₁, have a ∈ - t, from closed_iff_nhds.mp this _ $ neq_bot_of_le_neq_bot h $ le_inf inf_le_right (inf_le_left_of_le $ ‹f ≤ principal (- t)›), this ‹a ∈ t› end compact section separation class t1_space (α : Type u) [topological_space α] := (t1 : ∀x, closed ({x} : set α)) class t2_space (α : Type u) [topological_space α] := (t2 : ∀x y, x ≠ y → ∃u v : set α, open' u ∧ open' v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅) lemma eq_of_nhds_neq_bot [ht : t2_space α] {x y : α} (h : nhds x ⊓ nhds y ≠ ⊥) : x = y := classical.by_contradiction $ suppose x ≠ y, let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 _ x y this in have h₁ : u ∈ (nhds x ⊓ nhds y).sets, from @mem_inf_sets_of_left α (nhds x) (nhds y) _ $ mem_nhds_sets hu hx, have h₂ : v ∈ (nhds x ⊓ nhds y).sets, from @mem_inf_sets_of_right α (nhds x) (nhds y) _ $ mem_nhds_sets hv hy, have u ∩ v ∈ (nhds x ⊓ nhds y).sets, from @inter_mem_sets α (nhds x ⊓ nhds y) _ _ h₁ h₂, h $ empty_in_sets_eq_bot.mp $ huv ▸ this end separation end topological_space namespace topological_space variables {α : Type u} inductive generate_open (g : set (set α)) : set α → Prop | basic : ∀s∈g, generate_open s | univ : generate_open univ | inter : ∀s t, generate_open s → generate_open t → generate_open (s ∩ t) | sUnion : ∀k, (∀s∈k, generate_open s) → generate_open (⋃₀ k) def generate_from (g : set (set α)) : topological_space α := { topological_space . open' := generate_open g, open_univ := generate_open.univ g, open_inter := generate_open.inter, open_sUnion := generate_open.sUnion } lemma nhds_generate_from {g : set (set α)} {a : α} : @nhds α (generate_from g) a = (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) := le_antisymm (infi_le_infi $ take s, infi_le_infi_const $ take ⟨as, sg⟩, ⟨as, generate_open.basic _ sg⟩) (le_infi $ take s, le_infi $ take ⟨as, hs⟩, have ∀s, generate_open g s → a ∈ s → (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) ≤ principal s, begin intros s hs, induction hs, case generate_open.basic s hs { exact take as, infi_le_of_le s $ infi_le _ ⟨as, hs⟩ }, case generate_open.univ { rw [principal_univ], exact take _, le_top }, case generate_open.inter s t hs' ht' hs ht { exact take ⟨has, hat⟩, calc _ ≤ principal s ⊓ principal t : le_inf (hs has) (ht hat) ... = _ : by simp }, case generate_open.sUnion k hk' hk { intro h, simp at h, revert h, exact take ⟨t, hat, htk⟩, calc _ ≤ principal t : hk t htk hat ... ≤ _ : begin simp; exact subset_sUnion_of_mem htk end }, end, this s hs as) end topological_space section constructions variables {α : Type u} {β : Type v} instance : weak_order (topological_space α) := { weak_order . le := λt s, t^.open' ≤ s^.open', le_antisymm := take t s h₁ h₂, topological_space_eq $ le_antisymm h₁ h₂, le_refl := take t, le_refl t^.open', le_trans := take a b c h₁ h₂, @le_trans _ _ a^.open' b^.open' c^.open' h₁ h₂ } instance : has_Inf (topological_space α) := ⟨take (tt : set (topological_space α)), { topological_space . open' := λs, ∀t∈tt, topological_space.open' t s, open_univ := take t h, t^.open_univ, open_inter := take s₁ s₂ h₁ h₂ t ht, t^.open_inter s₁ s₂ (h₁ t ht) (h₂ t ht), open_sUnion := take s h t ht, t^.open_sUnion _ $ take s' hss', h _ hss' _ ht }⟩ private lemma Inf_le {tt : set (topological_space α)} {t : topological_space α} (h : t ∈ tt) : Inf tt ≤ t := take s hs, hs t h private lemma le_Inf {tt : set (topological_space α)} {t : topological_space α} (h : ∀t'∈tt, t ≤ t') : t ≤ Inf tt := take s hs t' ht', h t' ht' s hs def topological_space.induced {α : Type u} {β : Type v} (f : α → β) (t : topological_space β) : topological_space α := { topological_space . open' := λs, ∃s', t^.open' s' ∧ s = vimage f s', open_univ := ⟨univ, by simp; exact t^.open_univ⟩, open_inter := take s₁ s₂ ⟨s'₁, hs₁, eq₁⟩ ⟨s'₂, hs₂, eq₂⟩, ⟨s'₁ ∩ s'₂, by simp [eq₁, eq₂]; exact t^.open_inter _ _ hs₁ hs₂⟩, open_sUnion := take s h, begin simp [classical.skolem] at h, cases h with f hf, apply exists.intro (⋃(x : set α) (h : x ∈ s), f x h), simp [sUnion_eq_Union, (λx h, (hf x h)^.right^.symm)], exact (@open_Union β _ t _ $ take i, show open' (⋃h, f i h), from @open_Union β _ t _ $ take h, (hf i h)^.left) end } def topological_space.coinduced {α : Type u} {β : Type v} (f : α → β) (t : topological_space α) : topological_space β := { topological_space . open' := λs, t^.open' (vimage f s), open_univ := by simp; exact t^.open_univ, open_inter := take s₁ s₂ h₁ h₂, by simp; exact t^.open_inter _ _ h₁ h₂, open_sUnion := take s h, by rw [vimage_sUnion]; exact (@open_Union _ _ t _ $ take i, show open' (⋃ (H : i ∈ s), vimage f i), from @open_Union _ _ t _ $ take hi, h i hi) } instance : has_inf (topological_space α) := ⟨take t₁ t₂ : topological_space α, { topological_space . open' := λs, t₁.open' s ∧ t₂.open' s, open_univ := ⟨t₁^.open_univ, t₂^.open_univ⟩, open_inter := take s₁ s₂ ⟨h₁₁, h₁₂⟩ ⟨h₂₁, h₂₂⟩, ⟨t₁.open_inter s₁ s₂ h₁₁ h₂₁, t₂.open_inter s₁ s₂ h₁₂ h₂₂⟩, open_sUnion := take s h, ⟨t₁.open_sUnion _ $ take t ht, (h t ht).left, t₂.open_sUnion _ $ take t ht, (h t ht).right⟩ }⟩ instance : has_top (topological_space α) := ⟨{topological_space . open' := λs, true, open_univ := trivial, open_inter := take a b ha hb, trivial, open_sUnion := take s h, trivial }⟩ instance {α : Type u} : complete_lattice (topological_space α) := { topological_space.weak_order with sup := λa b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := take a b, le_Inf $ take x, assume h : a ≤ x ∧ b ≤ x, h^.left, le_sup_right := take a b, le_Inf $ take x, assume h : a ≤ x ∧ b ≤ x, h^.right, sup_le := take a b c h₁ h₂, Inf_le $ show c ∈ {x | a ≤ x ∧ b ≤ x}, from ⟨h₁, h₂⟩, inf := (⊓), le_inf := take a b h h₁ h₂ s hs, ⟨h₁ s hs, h₂ s hs⟩, inf_le_left := take a b s ⟨h₁, h₂⟩, h₁, inf_le_right := take a b s ⟨h₁, h₂⟩, h₂, top := ⊤, le_top := take a t ht, trivial, bot := Inf univ, bot_le := take a, Inf_le $ mem_univ a, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := take s f h, le_Inf $ take t ht, ht _ h, Sup_le := take s f h, Inf_le $ take t ht, h _ ht, Inf := Inf, le_Inf := take s a, le_Inf, Inf_le := take s a, Inf_le } instance inhabited_topological_space {α : Type u} : inhabited (topological_space α) := ⟨⊤⟩ lemma t2_space_top : @t2_space α ⊤ := ⟨take x y hxy, ⟨{x}, {y}, trivial, trivial, mem_insert _ _, mem_insert _ _, eq_empty_of_forall_not_mem $ by intros z hz; simp at hz; cc⟩⟩ lemma le_of_nhds_le_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₂ x ≤ @nhds α t₁ x) : t₁ ≤ t₂ := take s, show @open' α t₁ s → @open' α t₂ s, begin simp [open_iff_nhds]; exact take hs a ha, h _ $ hs _ ha end lemma eq_of_nhds_eq_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₂ x = @nhds α t₁ x) : t₁ = t₂ := le_antisymm (le_of_nhds_le_nhds $ take x, le_of_eq $ h x) (le_of_nhds_le_nhds $ take x, le_of_eq $ (h x).symm) instance : topological_space empty := ⊤ instance : topological_space unit := ⊤ instance : topological_space bool := ⊤ instance : topological_space ℕ := ⊤ instance : topological_space ℤ := ⊤ instance sierpinski_space : topological_space Prop := topological_space.generate_from {{true}} instance {p : α → Prop} [t : topological_space α] : topological_space (subtype p) := topological_space.induced subtype.val t instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α × β) := topological_space.induced prod.fst t₁ ⊔ topological_space.induced prod.snd t₂ instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α ⊕ β) := topological_space.coinduced sum.inl t₁ ⊓ topological_space.coinduced sum.inr t₂ instance {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (sigma β) := ⨅a, topological_space.coinduced (sigma.mk a) (t₂ a) instance topological_space_Pi {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (Πa, β a) := ⨆a, topological_space.induced (λf, f a) (t₂ a) section open topological_space lemma generate_from_le {t : topological_space α} { g : set (set α) } (h : ∀s∈g, open' s) : generate_from g ≤ t := take s (hs : generate_open g s), generate_open.rec_on hs h open_univ (take s t _ _ hs ht, open_inter hs ht) (take k _ hk, open_sUnion hk) lemma supr_eq_generate_from {ι : Sort w} { g : ι → topological_space α } : supr g = generate_from (⋃i, {s | (g i).open' s}) := le_antisymm (supr_le $ take i s open_s, generate_open.basic _ $ by simp; exact ⟨i, open_s⟩) (generate_from_le $ take s, begin simp, exact take ⟨i, open_s⟩, have g i ≤ supr g, from le_supr _ _, this s open_s end) lemma sup_eq_generate_from { g₁ g₂ : topological_space α } : g₁ ⊔ g₂ = generate_from {s | g₁.open' s ∨ g₂.open' s} := le_antisymm (sup_le (take s, generate_open.basic _ ∘ or.inl) (take s, generate_open.basic _ ∘ or.inr)) (generate_from_le $ take s hs, have h₁ : g₁ ≤ g₁ ⊔ g₂, from le_sup_left, have h₂ : g₂ ≤ g₁ ⊔ g₂, from le_sup_right, or.rec_on hs (h₁ s) (h₂ s)) lemma nhds_mono {t₁ t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) : @nhds α t₂ a ≤ @nhds α t₁ a := infi_le_infi $ take s, infi_le_infi2 $ take ⟨ha, hs⟩, ⟨⟨ha, h _ hs⟩, le_refl _⟩ lemma nhds_supr {ι : Sort w} {t : ι → topological_space α} {a : α} : @nhds α (supr t) a = (⨅i, @nhds α (t i) a) := le_antisymm (le_infi $ take i, nhds_mono $ le_supr _ _) begin rw [supr_eq_generate_from, nhds_generate_from], simp, exact (le_infi $ take s, le_infi $ take ⟨⟨i, hi⟩, hs⟩, infi_le_of_le i $ le_principal_iff.mpr $ @mem_nhds_sets α (t i) _ _ hi hs) end end end constructions
91fb7a20e01af2789ab9ff923c0902e842566169
7cef822f3b952965621309e88eadf618da0c8ae9
/src/order/pilex.lean
90244be72264c49752c42c5684e36a88adfef3dd
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
3,966
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 algebra.order_functions tactic.tauto algebra.pi_instances variables {ι : Type*} {β : ι → Type*} (r : ι → ι → Prop) (s : Π {i}, β i → β i → Prop) def pi.lex (x y : Π i, β i) : Prop := ∃ i, (∀ j, r j i → x j = y j) ∧ s (x i) (y i) def pilex (α : Type*) (β : α → Type*) : Type* := Π a, β a instance [has_lt ι] [∀ a, has_lt (β a)] : has_lt (pilex ι β) := { lt := pi.lex (<) (λ _, (<)) } set_option eqn_compiler.zeta true instance [linear_order ι] [∀ a, partial_order (β a)] : partial_order (pilex ι β) := let I := classical.DLO ι in have lt_not_symm : ∀ {x y : pilex ι β}, ¬ (x < y ∧ y < x), from λ x y ⟨⟨i, hi⟩, ⟨j, hj⟩⟩, begin rcases lt_trichotomy i j with hij | hij | hji, { exact lt_irrefl (x i) (by simpa [hj.1 _ hij] using hi.2) }, { exact not_le_of_gt hj.2 (hij ▸ le_of_lt hi.2) }, { exact lt_irrefl (x j) (by simpa [hi.1 _ hji] using hj.2) }, end, { le := λ x y, x < y ∨ x = y, le_refl := λ _, or.inr rfl, le_antisymm := λ x y hxy hyx, hxy.elim (λ hxy, hyx.elim (λ hyx, false.elim (lt_not_symm ⟨hxy, hyx⟩)) eq.symm) id, le_trans := λ x y z hxy hyz, hxy.elim (λ ⟨i, hi⟩, hyz.elim (λ ⟨j, hj⟩, or.inl ⟨by exactI min i j, by resetI; exact λ k hk, by rw [hi.1 _ (lt_min_iff.1 hk).1, hj.1 _ (lt_min_iff.1 hk).2], by resetI; exact (le_total i j).elim (λ hij, by rw [min_eq_left hij]; exact lt_of_lt_of_le hi.2 ((lt_or_eq_of_le hij).elim (λ h, le_of_eq (hj.1 _ h)) (λ h, h.symm ▸ le_of_lt hj.2))) (λ hji, by rw [min_eq_right hji]; exact lt_of_le_of_lt ((lt_or_eq_of_le hji).elim (λ h, le_of_eq (hi.1 _ h)) (λ h, h.symm ▸ le_of_lt hi.2)) hj.2)⟩) (λ hyz, hyz ▸ hxy)) (λ hxy, hxy.symm ▸ hyz), lt_iff_le_not_le := λ x y, show x < y ↔ (x < y ∨ x = y) ∧ ¬ (y < x ∨ y = x), from ⟨λ ⟨i, hi⟩, ⟨or.inl ⟨i, hi⟩, λ h, h.elim (λ ⟨j, hj⟩, begin rcases lt_trichotomy i j with hij | hij | hji, { exact lt_irrefl (x i) (by simpa [hj.1 _ hij] using hi.2) }, { exact not_le_of_gt hj.2 (hij ▸ le_of_lt hi.2) }, { exact lt_irrefl (x j) (by simpa [hi.1 _ hji] using hj.2) }, end) (λ hyx, lt_irrefl (x i) (by simpa [hyx] using hi.2))⟩, by tauto⟩, ..pilex.has_lt } instance [linear_order ι] (wf : well_founded ((<) : ι → ι → Prop)) [∀ a, linear_order (β a)] : linear_order (pilex ι β) := { le_total := λ x y, by classical; exact or_iff_not_imp_left.2 (λ hxy, begin have := not_or_distrib.1 hxy, let i : ι := well_founded.min wf _ (classical.not_forall.1 (this.2 ∘ funext)), have hjiyx : ∀ j < i, y j = x j, { assume j, rw [eq_comm, ← not_imp_not], exact λ h, well_founded.not_lt_min wf _ _ h }, refine or.inl ⟨i, hjiyx, _⟩, { refine lt_of_not_ge (λ hyx, _), exact this.1 ⟨i, (λ j hj, (hjiyx j hj).symm), lt_of_le_of_ne hyx (well_founded.min_mem _ {i | x i ≠ y i} _)⟩ } end), ..pilex.partial_order } instance [linear_order ι] [∀ a, ordered_comm_group (β a)] : ordered_comm_group (pilex ι β) := { add_le_add_left := λ x y hxy z, hxy.elim (λ ⟨i, hi⟩, or.inl ⟨i, λ j hji, show z j + x j = z j + y j, by rw [hi.1 j hji], add_lt_add_left hi.2 _⟩) (λ hxy, hxy ▸ le_refl _), add_lt_add_left := λ x y ⟨i, hi⟩ z, ⟨i, λ j hji, show z j + x j = z j + y j, by rw [hi.1 j hji], add_lt_add_left hi.2 _⟩, ..pilex.partial_order, ..pi.add_comm_group }
31a406411d134beaffaa313267a8c80940a263c2
5d166a16ae129621cb54ca9dde86c275d7d2b483
/library/init/meta/smt/congruence_closure.lean
3777874a36a28280bea2a89f6a1b65abaeaab7d6
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
5,353
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.interactive_base init.meta.tactic init.meta.set_get_option_tactics structure cc_config := /- If tt, congruence closure will treat implicit instance arguments as constants. -/ (ignore_instances : bool := tt) /- If tt, congruence closure modulo AC. -/ (ac : bool := tt) /- If ho_fns is (some fns), then full (and more expensive) support for higher-order functions is *only* considered for the functions in fns and local functions. The performance overhead is described in the paper "Congruence Closure in Intensional Type Theory". If ho_fns is none, then full support is provided for *all* constants. -/ (ho_fns : option (list name) := none) /- If true, then use excluded middle -/ (em : bool := tt) /- Congruence closure state -/ meta constant cc_state : Type meta constant cc_state.mk_core : cc_config → cc_state /- Create a congruence closure state object using the hypotheses in the current goal. -/ meta constant cc_state.mk_using_hs_core : cc_config → tactic cc_state meta constant cc_state.next : cc_state → expr → expr meta constant cc_state.roots_core : cc_state → bool → list expr meta constant cc_state.root : cc_state → expr → expr meta constant cc_state.mt : cc_state → expr → nat meta constant cc_state.gmt : cc_state → nat meta constant cc_state.inc_gmt : cc_state → cc_state meta constant cc_state.is_cg_root : cc_state → expr → bool meta constant cc_state.pp_eqc : cc_state → expr → tactic format meta constant cc_state.pp_core : cc_state → bool → tactic format meta constant cc_state.internalize : cc_state → expr → tactic cc_state meta constant cc_state.add : cc_state → expr → tactic cc_state meta constant cc_state.is_eqv : cc_state → expr → expr → tactic bool meta constant cc_state.is_not_eqv : cc_state → expr → expr → tactic bool meta constant cc_state.eqv_proof : cc_state → expr → expr → tactic expr meta constant cc_state.inconsistent : cc_state → bool /- (proof_for cc e) constructs a proof for e if it is equivalent to true in cc_state -/ meta constant cc_state.proof_for : cc_state → expr → tactic expr /- (refutation_for cc e) constructs a proof for (not e) if it is equivalent to false in cc_state -/ meta constant cc_state.refutation_for : cc_state → expr → tactic expr /- If the given state is inconsistent, return a proof for false. Otherwise fail. -/ meta constant cc_state.proof_for_false : cc_state → tactic expr namespace cc_state meta def mk : cc_state := cc_state.mk_core {} meta def mk_using_hs : tactic cc_state := cc_state.mk_using_hs_core {} meta def roots (s : cc_state) : list expr := cc_state.roots_core s tt meta instance : has_to_tactic_format cc_state := ⟨λ s, cc_state.pp_core s tt⟩ meta def eqc_of_core (s : cc_state) : expr → expr → list expr → list expr | e f r := let n := s.next e in if n = f then e::r else eqc_of_core n f (e::r) meta def eqc_of (s : cc_state) (e : expr) : list expr := s.eqc_of_core e e [] meta def in_singlenton_eqc (s : cc_state) (e : expr) : bool := s.next e = e meta def eqc_size (s : cc_state) (e : expr) : nat := (s.eqc_of e).length meta def fold_eqc_core {α} (s : cc_state) (f : α → expr → α) (first : expr) : expr → α → α | c a := let new_a := f a c, next := s.next c in if next =ₐ first then new_a else fold_eqc_core next new_a meta def fold_eqc {α} (s : cc_state) (e : expr) (a : α) (f : α → expr → α) : α := fold_eqc_core s f e e a meta def mfold_eqc {α} {m : Type → Type} [monad m] (s : cc_state) (e : expr) (a : α) (f : α → expr → m α) : m α := fold_eqc s e (return a) (λ act e, do a ← act, f a e) end cc_state open tactic meta def tactic.cc_core (cfg : cc_config) : tactic unit := do intros, s ← cc_state.mk_using_hs_core cfg, t ← target, s ← s.internalize t, if s.inconsistent then do { pr ← s.proof_for_false, mk_app `false.elim [t, pr] >>= exact} else do { tr ← return $ expr.const `true [], b ← s.is_eqv t tr, if b then do { pr ← s.eqv_proof t tr, mk_app `of_eq_true [pr] >>= exact } else do { dbg ← get_bool_option `trace.cc.failure ff, if dbg then do { ccf ← pp s, fail format!"cc tactic failed, equivalence classes: \n{ccf}" } else do { fail "cc tactic failed" } } } meta def tactic.cc : tactic unit := tactic.cc_core {} meta def tactic.cc_dbg_core (cfg : cc_config) : tactic unit := save_options $ set_bool_option `trace.cc.failure tt >> tactic.cc_core cfg meta def tactic.cc_dbg : tactic unit := tactic.cc_dbg_core {} meta def tactic.ac_refl : tactic unit := do (lhs, rhs) ← target >>= match_eq, s ← return $ cc_state.mk, s ← s.internalize lhs, s ← s.internalize rhs, b ← s.is_eqv lhs rhs, if b then do { s.eqv_proof lhs rhs >>= exact } else do { fail "ac_refl failed" }
80c3ed75ebe704235a8a624431d01dc3a9dae93b
36938939954e91f23dec66a02728db08a7acfcf9
/lean/deps/galois_stdlib/src/galois/data/array.lean
17f19f04cc24b5aa17e4d053ffba4318c182042e
[ "Apache-2.0" ]
permissive
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
4,912
lean
-- Additional definitions for arrays import .array.lex_order import ..algebra.order import ..data.nat.basic import ..data.list import ..logic namespace galois open array namespace array universe u /- rev_list -/ section rev_list variables {n : ℕ} {α : Type u} {a : array n α} theorem rev_list_reverse_aux : ∀ i (h : i ≤ n) (t : list α), (a.iterate_aux (λ _, (::)) i h []).reverse_core t = a.rev_iterate_aux (λ _, (::)) i h t | 0 h t := rfl | (i+1) h t := rev_list_reverse_aux i _ _ @[simp] theorem rev_list_reverse : a.rev_list.reverse = a.to_list := rev_list_reverse_aux _ _ _ @[simp] theorem to_list_reverse : a.to_list.reverse = a.rev_list := by rw [←rev_list_reverse, list.reverse_reverse] end rev_list /- length -/ section length variables {n : ℕ} {α : Type u} theorem rev_list_length_aux (a : array n α) (i h) : (a.iterate_aux (λ _, (::)) i h []).length = i := by induction i; simp [*, d_array.iterate_aux] @[simp] theorem rev_list_length (a : array n α) : a.rev_list.length = n := rev_list_length_aux a _ _ @[simp] theorem to_list_length (a : array n α) : a.to_list.length = n := by rw[←rev_list_reverse, list.length_reverse, rev_list_length] end length /- nth -/ section nth variables {n : ℕ} {α : Type u} {a : array n α} theorem to_list_nth_le_aux (i : ℕ) (ih : i < n) : ∀ j {jh t h'}, (∀ k tl, j + k = i → list.nth_le t k tl = a.read ⟨i, ih⟩) → (a.rev_iterate_aux (λ _, (::)) j jh t).nth_le i h' = a.read ⟨i, ih⟩ | 0 _ _ _ al := al i _ $ zero_add _ | (j+1) jh t h' al := to_list_nth_le_aux j $ λ k tl hjk, show list.nth_le (a.read ⟨j, jh⟩ :: t) k tl = a.read ⟨i, ih⟩, from match k, hjk, tl with | 0, e, tl := match i, e, ih with ._, rfl, _ := rfl end | k'+1, _, tl := by simp[list.nth_le]; exact al _ _ (by simp [*]) end theorem to_list_nth_le (i : ℕ) (h h') : list.nth_le a.to_list i h' = a.read ⟨i, h⟩ := to_list_nth_le_aux _ _ _ (λ k tl, absurd tl k.not_lt_zero) @[simp] theorem to_list_nth_le' (a : array n α) (i : fin n) (h') : list.nth_le a.to_list i.1 h' = a.read i := by cases i; apply to_list_nth_le end nth /- to_array -/ section to_array variables {n : ℕ} {α : Type u} @[simp] theorem to_list_to_array (a : array n α) : list.to_array a.to_list == a := heq_of_heq_of_eq (@@eq.drec_on (λ m (e : a.to_list.length = m), (d_array.mk (λ v, a.to_list.nth_le v.1 v.2)) == (@d_array.mk m (λ _, α) $ λ v, a.to_list.nth_le v.1 $ e.symm ▸ v.2)) (array.to_list_length a) heq.rfl) $ d_array.ext $ λ ⟨i, h⟩, to_list_nth_le i h _ end to_array section push_back variables {n : ℕ} {α : Type u} {v : α} {a : array n α} lemma push_back_rev_list_aux : ∀ i h h', d_array.iterate_aux (a.push_back v) (λ _, (::)) i h [] = d_array.iterate_aux a (λ _, (::)) i h' [] | 0 h h' := rfl | (i+1) h h' := begin simp [d_array.iterate_aux], refine ⟨_, push_back_rev_list_aux _ _ _⟩, dsimp [array.read, d_array.read, array.push_back], rw [dif_neg], refl, exact ne_of_lt h', end @[simp] theorem push_back_rev_list : (a.push_back v).rev_list = v :: a.rev_list := begin unfold array.push_back array.rev_list array.foldl array.iterate d_array.iterate, dsimp [d_array.iterate_aux, array.read, d_array.read, array.push_back], rw [dif_pos (eq.refl n)], apply congr_arg, apply push_back_rev_list_aux end @[simp] theorem push_back_to_list : (a.push_back v).to_list = a.to_list ++ [v] := by rw [←rev_list_reverse, ←rev_list_reverse, push_back_rev_list, list.reverse_cons] end push_back /- Simplification rule for reading from an array constructed by push_back with a less-than test. -/ theorem read_push_back_lt_iff {α} {n:ℕ} (a : array n α) (x:α) (i : fin (n+1)) : read (push_back a x) i = if lt:i.val < n then read a ⟨i.val,lt⟩ else x := begin cases i with i i_lt_plus_1, have i_le := nat.le_of_succ_le_succ i_lt_plus_1, dsimp [push_back, array.read, d_array.read], cases (decide (i = n)), case decidable.is_true : is_eq { simp [is_eq], }, case decidable.is_false : is_neq { have i_lt := lt_of_le_of_ne i_le is_neq, simp [is_neq, i_lt], }, end /-- Lemma needed for stating read_slice below -/ theorem read_slice.len (s:ℕ) {e n:ℕ} (e_le_n : e ≤ n) (i:fin (e - s)) : s + i.val < n := calc s + i.val = i.val + s : add_comm _ _ ... < e : nat.add_lt_of_lt_sub_right i.is_lt ... ≤ n : e_le_n /- Simplify read (slice ...) -/ theorem read_slice {n:ℕ} {α} (a:array n α) (s e :ℕ) (s_le_e : s ≤ e) (e_le_n : e ≤ n) (i:fin (e - s)) : read (slice a s e s_le_e e_le_n) i = read a ⟨s + i.val, read_slice.len s e_le_n i⟩ := begin cases a with f, cases i, simp [array.read, d_array.read, slice], end /-- Empty buffer converted to array is same as array.nil -/ @[simp] theorem to_list_nil {α} : to_list (@nil α) = [] := eq.refl _ end array end galois
878583f8b3fd2382203b23ee2040c083a5589385
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/bool/all_any.lean
2b7b3bbe292a7544feec8d380b316aae87a491fe
[ "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
1,620
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.list.basic /-! # Boolean quantifiers This proves a few properties about `list.all` and `list.any`, which are the `bool` universal and existential quantifiers. Their definitions are in core Lean. -/ variables {α : Type*} {p : α → Prop} [decidable_pred p] {l : list α} {a : α} namespace list @[simp] theorem all_nil (p : α → bool) : all [] p = tt := rfl @[simp] theorem all_cons (p : α → bool) (a : α) (l : list α) : all (a::l) p = (p a && all l p) := rfl theorem all_iff_forall {p : α → bool} : all l p ↔ ∀ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_true rfl (forall_mem_nil _) }, simp only [all_cons, band_coe_iff, ih, forall_mem_cons] end theorem all_iff_forall_prop : all l (λ a, p a) ↔ ∀ a ∈ l, p a := by simp only [all_iff_forall, bool.of_to_bool_iff] @[simp] theorem any_nil (p : α → bool) : any [] p = ff := rfl @[simp] theorem any_cons (p : α → bool) (a : α) (l : list α) : any (a :: l) p = (p a || any l p) := rfl theorem any_iff_exists {p : α → bool} : any l p ↔ ∃ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_false bool.not_ff (not_exists_mem_nil _) }, simp only [any_cons, bor_coe_iff, ih, exists_mem_cons_iff] end theorem any_iff_exists_prop : any l (λ a, p a) ↔ ∃ a ∈ l, p a := by simp [any_iff_exists] theorem any_of_mem {p : α → bool} (h₁ : a ∈ l) (h₂ : p a) : any l p := any_iff_exists.2 ⟨_, h₁, h₂⟩ end list
5046f4a13f2193ed11775a7c96897e1059236655
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/cubic_discriminant.lean
ae9d98ff2d568022b02e54af365905d88c61fa50
[ "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
11,419
lean
/- Copyright (c) 2022 David Kurniadi Angdinata. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Kurniadi Angdinata -/ import field_theory.splitting_field /-! # Cubics and discriminants This file defines cubic polynomials over a semiring and their discriminants over a splitting field. ## Main definitions * `cubic`: the structure representing a cubic polynomial. * `disc`: the discriminant of a cubic polynomial. ## Main statements * `disc_ne_zero_iff_roots_nodup`: the cubic discriminant is not equal to zero if and only if the cubic has no duplicate roots. ## References * https://en.wikipedia.org/wiki/Cubic_equation * https://en.wikipedia.org/wiki/Discriminant ## Tags cubic, discriminant, polynomial, root -/ noncomputable theory /-- The structure representing a cubic polynomial. -/ @[ext] structure cubic (R : Type*) := (a b c d : R) namespace cubic open cubic polynomial open_locale polynomial variables {R S F K : Type*} instance [inhabited R] : inhabited (cubic R) := ⟨⟨default, default, default, default⟩⟩ instance [has_zero R] : has_zero (cubic R) := ⟨⟨0, 0, 0, 0⟩⟩ section basic variables {P : cubic R} [semiring R] /-- Convert a cubic polynomial to a polynomial. -/ def to_poly (P : cubic R) : R[X] := C P.a * X ^ 3 + C P.b * X ^ 2 + C P.c * X + C P.d /-! ### Coefficients -/ section coeff private lemma coeffs : (∀ n > 3, P.to_poly.coeff n = 0) ∧ P.to_poly.coeff 3 = P.a ∧ P.to_poly.coeff 2 = P.b ∧ P.to_poly.coeff 1 = P.c ∧ P.to_poly.coeff 0 = P.d := begin simp only [to_poly, coeff_add, coeff_C, coeff_C_mul_X, coeff_C_mul_X_pow], norm_num, intros n hn, repeat { rw [if_neg] }, any_goals { linarith only [hn] }, repeat { rw [zero_add] } end @[simp] lemma coeff_gt_three (n : ℕ) (hn : 3 < n) : P.to_poly.coeff n = 0 := coeffs.1 n hn @[simp] lemma coeff_three : P.to_poly.coeff 3 = P.a := coeffs.2.1 @[simp] lemma coeff_two : P.to_poly.coeff 2 = P.b := coeffs.2.2.1 @[simp] lemma coeff_one : P.to_poly.coeff 1 = P.c := coeffs.2.2.2.1 @[simp] lemma coeff_zero : P.to_poly.coeff 0 = P.d := coeffs.2.2.2.2 lemma a_of_eq {Q : cubic R} (h : P.to_poly = Q.to_poly) : P.a = Q.a := by rw [← coeff_three, h, coeff_three] lemma b_of_eq {Q : cubic R} (h : P.to_poly = Q.to_poly) : P.b = Q.b := by rw [← coeff_two, h, coeff_two] lemma c_of_eq {Q : cubic R} (h : P.to_poly = Q.to_poly) : P.c = Q.c := by rw [← coeff_one, h, coeff_one] lemma d_of_eq {Q : cubic R} (h : P.to_poly = Q.to_poly) : P.d = Q.d := by rw [← coeff_zero, h, coeff_zero] @[simp] lemma to_poly_injective (P Q : cubic R) : P.to_poly = Q.to_poly ↔ P = Q := ⟨λ h, cubic.ext _ _ (a_of_eq h) (b_of_eq h) (c_of_eq h) (d_of_eq h), congr_arg _⟩ @[simp] lemma of_a_eq_zero (ha : P.a = 0) : P.to_poly = C P.b * X ^ 2 + C P.c * X + C P.d := by rw [to_poly, C_eq_zero.mpr ha, zero_mul, zero_add] @[simp] lemma of_a_b_eq_zero (ha : P.a = 0) (hb : P.b = 0) : P.to_poly = C P.c * X + C P.d := by rw [of_a_eq_zero ha, C_eq_zero.mpr hb, zero_mul, zero_add] @[simp] lemma of_a_b_c_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) : P.to_poly = C P.d := by rw [of_a_b_eq_zero ha hb, C_eq_zero.mpr hc, zero_mul, zero_add] @[simp] lemma of_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) (hd : P.d = 0) : P.to_poly = 0 := by rw [of_a_b_c_eq_zero ha hb hc, C_eq_zero.mpr hd] @[simp] lemma zero : (0 : cubic R).to_poly = 0 := of_zero rfl rfl rfl rfl @[simp] lemma eq_zero_iff : P.to_poly = 0 ↔ P = 0 := by rw [← zero, to_poly_injective] lemma ne_zero (h0 : ¬P.a = 0 ∨ ¬P.b = 0 ∨ ¬P.c = 0 ∨ ¬P.d = 0) : P.to_poly ≠ 0 := by { contrapose! h0, rw [eq_zero_iff.mp h0], exact ⟨rfl, rfl, rfl, rfl⟩ } lemma ne_zero_of_a_ne_zero (ha : P.a ≠ 0) : P.to_poly ≠ 0 := (or_imp_distrib.mp ne_zero).1 ha lemma ne_zero_of_b_ne_zero (hb : P.b ≠ 0) : P.to_poly ≠ 0 := (or_imp_distrib.mp (or_imp_distrib.mp ne_zero).2).1 hb lemma ne_zero_of_c_ne_zero (hc : P.c ≠ 0) : P.to_poly ≠ 0 := (or_imp_distrib.mp (or_imp_distrib.mp (or_imp_distrib.mp ne_zero).2).2).1 hc lemma ne_zero_of_d_ne_zero (hd : P.d ≠ 0) : P.to_poly ≠ 0 := (or_imp_distrib.mp (or_imp_distrib.mp (or_imp_distrib.mp ne_zero).2).2).2 hd end coeff /-! ### Degrees -/ section degree /-- The equivalence between cubic polynomials and polynomials of degree at most three. -/ @[simps] def equiv : cubic R ≃ {p : R[X] // p.degree ≤ 3} := { to_fun := λ P, ⟨P.to_poly, degree_cubic_le⟩, inv_fun := λ f, ⟨coeff f 3, coeff f 2, coeff f 1, coeff f 0⟩, left_inv := λ P, by ext; simp only [subtype.coe_mk, coeffs], right_inv := λ f, begin ext (_ | _ | _ | _ | n); simp only [subtype.coe_mk, coeffs], have h3 : 3 < n + 4 := by linarith only, rw [coeff_gt_three _ h3, (degree_le_iff_coeff_zero (f : R[X]) 3).mp f.2 _ $ with_bot.coe_lt_coe.mpr h3] end } lemma degree (ha : P.a ≠ 0) : P.to_poly.degree = 3 := degree_cubic ha lemma degree_of_a_eq_zero (ha : P.a = 0) (hb : P.b ≠ 0) : P.to_poly.degree = 2 := by rw [of_a_eq_zero ha, degree_quadratic hb] lemma degree_of_a_b_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c ≠ 0) : P.to_poly.degree = 1 := by rw [of_a_b_eq_zero ha hb, degree_linear hc] lemma degree_of_a_b_c_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) (hd : P.d ≠ 0) : P.to_poly.degree = 0 := by rw [of_a_b_c_eq_zero ha hb hc, degree_C hd] lemma degree_of_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) (hd : P.d = 0) : P.to_poly.degree = ⊥ := by rw [of_zero ha hb hc hd, degree_zero] lemma leading_coeff (ha : P.a ≠ 0) : P.to_poly.leading_coeff = P.a := leading_coeff_cubic ha lemma leading_coeff_of_a_eq_zero (ha : P.a = 0) (hb : P.b ≠ 0) : P.to_poly.leading_coeff = P.b := by rw [of_a_eq_zero ha, leading_coeff_quadratic hb] lemma leading_coeff_of_a_b_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c ≠ 0) : P.to_poly.leading_coeff = P.c := by rw [of_a_b_eq_zero ha hb, leading_coeff_linear hc] lemma leading_coeff_of_a_b_c_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) : P.to_poly.leading_coeff = P.d := by rw [of_a_b_c_eq_zero ha hb hc, leading_coeff_C] end degree /-! ### Map across a homomorphism -/ section map variables [semiring S] {φ : R →+* S} /-- Map a cubic polynomial across a semiring homomorphism. -/ def map (φ : R →+* S) (P : cubic R) : cubic S := ⟨φ P.a, φ P.b, φ P.c, φ P.d⟩ lemma map_to_poly : (map φ P).to_poly = polynomial.map φ P.to_poly := by simp only [map, to_poly, map_C, map_X, polynomial.map_add, polynomial.map_mul, polynomial.map_pow] end map end basic section roots open multiset /-! ### Roots over an extension -/ section extension variables {P : cubic R} [comm_ring R] [comm_ring S] {φ : R →+* S} /-- The roots of a cubic polynomial. -/ def roots [is_domain R] (P : cubic R) : multiset R := P.to_poly.roots lemma map_roots [is_domain S] : (map φ P).roots = (polynomial.map φ P.to_poly).roots := by rw [roots, map_to_poly] theorem mem_roots_iff [is_domain R] (h0 : P.to_poly ≠ 0) (x : R) : x ∈ P.roots ↔ P.a * x ^ 3 + P.b * x ^ 2 + P.c * x + P.d = 0 := begin rw [roots, mem_roots h0, is_root, to_poly], simp only [eval_C, eval_X, eval_add, eval_mul, eval_pow] end theorem card_roots_le [is_domain R] [decidable_eq R] : P.roots.to_finset.card ≤ 3 := begin apply (to_finset_card_le P.to_poly.roots).trans, by_cases hP : P.to_poly = 0, { exact (card_roots' P.to_poly).trans (by { rw [hP, nat_degree_zero], exact zero_le 3 }) }, { exact with_bot.coe_le_coe.1 ((card_roots hP).trans degree_cubic_le) } end end extension variables {P : cubic F} [field F] [field K] {φ : F →+* K} {x y z : K} /-! ### Roots over a splitting field -/ section split theorem splits_iff_card_roots (ha : P.a ≠ 0) : splits φ P.to_poly ↔ (map φ P).roots.card = 3 := begin replace ha : (map φ P).a ≠ 0 := (_root_.map_ne_zero φ).mpr ha, nth_rewrite_lhs 0 [← ring_hom.id_comp φ], rw [roots, ← splits_map_iff, ← map_to_poly, splits_iff_card_roots, ← ((degree_eq_iff_nat_degree_eq $ ne_zero_of_a_ne_zero ha).mp $ degree ha : _ = 3)] end theorem splits_iff_roots_eq_three (ha : P.a ≠ 0) : splits φ P.to_poly ↔ ∃ x y z : K, (map φ P).roots = {x, y, z} := by rw [splits_iff_card_roots ha, card_eq_three] theorem eq_prod_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : (map φ P).to_poly = C (φ P.a) * (X - C x) * (X - C y) * (X - C z) := begin rw [map_to_poly, eq_prod_roots_of_splits $ (splits_iff_roots_eq_three ha).mpr $ exists.intro x $ exists.intro y $ exists.intro z h3, leading_coeff ha, ← map_roots, h3], change C (φ P.a) * ((X - C x) ::ₘ (X - C y) ::ₘ {X - C z}).prod = _, rw [prod_cons, prod_cons, prod_singleton, mul_assoc, mul_assoc] end theorem eq_sum_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : map φ P = ⟨φ P.a, φ P.a * -(x + y + z), φ P.a * (x * y + x * z + y * z), φ P.a * -(x * y * z)⟩ := begin apply_fun to_poly, any_goals { exact λ P Q, (to_poly_injective P Q).mp }, rw [eq_prod_three_roots ha h3, to_poly], simp only [C_neg, C_add, C_mul], ring1 end theorem b_eq_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : φ P.b = φ P.a * -(x + y + z) := by injection eq_sum_three_roots ha h3 theorem c_eq_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : φ P.c = φ P.a * (x * y + x * z + y * z) := by injection eq_sum_three_roots ha h3 theorem d_eq_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : φ P.d = φ P.a * -(x * y * z) := by injection eq_sum_three_roots ha h3 end split /-! ### Discriminant over a splitting field -/ section discriminant /-- The discriminant of a cubic polynomial. -/ def disc {R : Type*} [ring R] (P : cubic R) : R := P.b ^ 2 * P.c ^ 2 - 4 * P.a * P.c ^ 3 - 4 * P.b ^ 3 * P.d - 27 * P.a ^ 2 * P.d ^ 2 + 18 * P.a * P.b * P.c * P.d theorem disc_eq_prod_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : φ P.disc = (φ P.a * φ P.a * (x - y) * (x - z) * (y - z)) ^ 2 := begin simp only [disc, ring_hom.map_add, ring_hom.map_sub, ring_hom.map_mul, map_pow], simp only [ring_hom.map_one, map_bit0, map_bit1], rw [b_eq_three_roots ha h3, c_eq_three_roots ha h3, d_eq_three_roots ha h3], ring1 end theorem disc_ne_zero_iff_roots_ne (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : P.disc ≠ 0 ↔ x ≠ y ∧ x ≠ z ∧ y ≠ z := begin rw [←_root_.map_ne_zero φ, disc_eq_prod_three_roots ha h3, pow_two], simp_rw [mul_ne_zero_iff, sub_ne_zero, _root_.map_ne_zero, and_self, and_iff_right ha, and_assoc], end theorem disc_ne_zero_iff_roots_nodup (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : P.disc ≠ 0 ↔ (map φ P).roots.nodup := begin rw [disc_ne_zero_iff_roots_ne ha h3, h3], change _ ↔ (x ::ₘ y ::ₘ {z}).nodup, rw [nodup_cons, nodup_cons, mem_cons, mem_singleton, mem_singleton], simp only [nodup_singleton], tautology end theorem card_roots_of_disc_ne_zero [decidable_eq K] (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) (hd : P.disc ≠ 0) : (map φ P).roots.to_finset.card = 3 := begin rw [to_finset_card_of_nodup $ (disc_ne_zero_iff_roots_nodup ha h3).mp hd, ← splits_iff_card_roots ha, splits_iff_roots_eq_three ha], exact ⟨x, ⟨y, ⟨z, h3⟩⟩⟩ end end discriminant end roots end cubic
44855e6f24798f3c5ac9e267acb552a662b2f61a
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/bornology/basic.lean
c3aeef4234909753cd03196337436b29416efad7
[ "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
10,135
lean
/- Copyright (c) 2022 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import order.filter.cofinite /-! # Basic theory of bornology We develop the basic theory of bornologies. Instead of axiomatizing bounded sets and defining bornologies in terms of those, we recognize that the cobounded sets form a filter and define a bornology as a filter of cobounded sets which contains the cofinite filter. This allows us to make use of the extensive library for filters, but we also provide the relevant connecting results for bounded sets. The specification of a bornology in terms of the cobounded filter is equivalent to the standard one (e.g., see [Bourbaki, *Topological Vector Spaces*][bourbaki1987], **covering bornology**, now often called simply **bornology**) in terms of bounded sets (see `bornology.of_bounded`, `is_bounded.union`, `is_bounded.subset`), except that we do not allow the empty bornology (that is, we require that *some* set must be bounded; equivalently, `∅` is bounded). In the literature the cobounded filter is generally referred to as the *filter at infinity*. ## Main definitions - `bornology α`: a class consisting of `cobounded : filter α` and a proof that this filter contains the `cofinite` filter. - `bornology.is_cobounded`: the predicate that a set is a member of the `cobounded α` filter. For `s : set α`, one should prefer `bornology.is_cobounded s` over `s ∈ cobounded α`. - `bornology.is_bounded`: the predicate that states a set is bounded (i.e., the complement of a cobounded set). One should prefer `bornology.is_bounded s` over `sᶜ ∈ cobounded α`. - `bounded_space α`: a class extending `bornology α` with the condition `bornology.is_bounded (set.univ : set α)` Although use of `cobounded α` is discouraged for indicating the (co)boundedness of individual sets, it is intended for regular use as a filter on `α`. -/ open set filter variables {ι α β : Type*} /-- A **bornology** on a type `α` is a filter of cobounded sets which contains the cofinite filter. Such spaces are equivalently specified by their bounded sets, see `bornology.of_bounded` and `bornology.ext_iff_is_bounded`-/ @[ext] class bornology (α : Type*) := (cobounded [] : filter α) (le_cofinite [] : cobounded ≤ cofinite) /-- A constructor for bornologies by specifying the bounded sets, and showing that they satisfy the appropriate conditions. -/ @[simps] def bornology.of_bounded {α : Type*} (B : set (set α)) (empty_mem : ∅ ∈ B) (subset_mem : ∀ s₁ ∈ B, ∀ s₂ : set α, s₂ ⊆ s₁ → s₂ ∈ B) (union_mem : ∀ s₁ s₂ ∈ B, s₁ ∪ s₂ ∈ B) (singleton_mem : ∀ x, {x} ∈ B) : bornology α := { cobounded := { sets := {s : set α | sᶜ ∈ B}, univ_sets := by rwa ←compl_univ at empty_mem, sets_of_superset := λ x y hx hy, subset_mem xᶜ hx yᶜ (compl_subset_compl.mpr hy), inter_sets := λ x y hx hy, by simpa [compl_inter] using union_mem xᶜ hx yᶜ hy, }, le_cofinite := begin rw le_cofinite_iff_compl_singleton_mem, intros x, change {x}ᶜᶜ ∈ B, rw compl_compl, exact singleton_mem x end } /-- A constructor for bornologies by specifying the bounded sets, and showing that they satisfy the appropriate conditions. -/ @[simps] def bornology.of_bounded' {α : Type*} (B : set (set α)) (empty_mem : ∅ ∈ B) (subset_mem : ∀ s₁ ∈ B, ∀ s₂ : set α, s₂ ⊆ s₁ → s₂ ∈ B) (union_mem : ∀ s₁ s₂ ∈ B, s₁ ∪ s₂ ∈ B) (sUnion_univ : ⋃₀ B = univ) : bornology α := bornology.of_bounded B empty_mem subset_mem union_mem $ λ x, begin rw sUnion_eq_univ_iff at sUnion_univ, rcases sUnion_univ x with ⟨s, hs, hxs⟩, exact subset_mem s hs {x} (singleton_subset_iff.mpr hxs) end namespace bornology section variables [bornology α] {s t : set α} {x : α} /-- `is_cobounded` is the predicate that `s` is in the filter of cobounded sets in the ambient bornology on `α` -/ def is_cobounded (s : set α) : Prop := s ∈ cobounded α /-- `is_bounded` is the predicate that `s` is bounded relative to the ambient bornology on `α`. -/ def is_bounded (s : set α) : Prop := is_cobounded sᶜ lemma is_cobounded_def {s : set α} : is_cobounded s ↔ s ∈ cobounded α := iff.rfl lemma is_bounded_def {s : set α} : is_bounded s ↔ sᶜ ∈ cobounded α := iff.rfl @[simp] lemma is_bounded_compl_iff : is_bounded sᶜ ↔ is_cobounded s := by rw [is_bounded_def, is_cobounded_def, compl_compl] @[simp] lemma is_cobounded_compl_iff : is_cobounded sᶜ ↔ is_bounded s := iff.rfl alias is_bounded_compl_iff ↔ bornology.is_bounded.of_compl bornology.is_cobounded.compl alias is_cobounded_compl_iff ↔ bornology.is_cobounded.of_compl bornology.is_bounded.compl @[simp] lemma is_bounded_empty : is_bounded (∅ : set α) := by { rw [is_bounded_def, compl_empty], exact univ_mem} @[simp] lemma is_bounded_singleton : is_bounded ({x} : set α) := by {rw [is_bounded_def], exact le_cofinite _ (finite_singleton x).compl_mem_cofinite} @[simp] lemma is_cobounded_univ : is_cobounded (univ : set α) := univ_mem @[simp] lemma is_cobounded_inter : is_cobounded (s ∩ t) ↔ is_cobounded s ∧ is_cobounded t := inter_mem_iff lemma is_cobounded.inter (hs : is_cobounded s) (ht : is_cobounded t) : is_cobounded (s ∩ t) := is_cobounded_inter.2 ⟨hs, ht⟩ @[simp] lemma is_bounded_union : is_bounded (s ∪ t) ↔ is_bounded s ∧ is_bounded t := by simp only [← is_cobounded_compl_iff, compl_union, is_cobounded_inter] lemma is_bounded.union (hs : is_bounded s) (ht : is_bounded t) : is_bounded (s ∪ t) := is_bounded_union.2 ⟨hs, ht⟩ lemma is_cobounded.superset (hs : is_cobounded s) (ht : s ⊆ t) : is_cobounded t := mem_of_superset hs ht lemma is_bounded.subset (ht : is_bounded t) (hs : s ⊆ t) : is_bounded s := ht.superset (compl_subset_compl.mpr hs) @[simp] lemma sUnion_bounded_univ : (⋃₀ {s : set α | is_bounded s}) = univ := sUnion_eq_univ_iff.2 $ λ a, ⟨{a}, is_bounded_singleton, mem_singleton a⟩ lemma comap_cobounded_le_iff [bornology β] {f : α → β} : (cobounded β).comap f ≤ cobounded α ↔ ∀ ⦃s⦄, is_bounded s → is_bounded (f '' s) := begin refine ⟨λ h s hs, _, λ h t ht, ⟨(f '' tᶜ)ᶜ, h $ is_cobounded.compl ht, compl_subset_comm.1 $ subset_preimage_image _ _⟩⟩, obtain ⟨t, ht, hts⟩ := h hs.compl, rw [subset_compl_comm, ←preimage_compl] at hts, exact (is_cobounded.compl ht).subset ((image_subset f hts).trans $ image_preimage_subset _ _), end end lemma ext_iff' {t t' : bornology α} : t = t' ↔ ∀ s, (@cobounded α t).sets s ↔ (@cobounded α t').sets s := (ext_iff _ _).trans filter.ext_iff lemma ext_iff_is_bounded {t t' : bornology α} : t = t' ↔ ∀ s, @is_bounded α t s ↔ @is_bounded α t' s := ⟨λ h s, h ▸ iff.rfl, λ h, by { ext, simpa only [is_bounded_def, compl_compl] using h sᶜ, }⟩ variables {s : set α} lemma is_cobounded_of_bounded_iff (B : set (set α)) {empty_mem subset_mem union_mem sUnion_univ} : @is_cobounded _ (of_bounded B empty_mem subset_mem union_mem sUnion_univ) s ↔ sᶜ ∈ B := iff.rfl lemma is_bounded_of_bounded_iff (B : set (set α)) {empty_mem subset_mem union_mem sUnion_univ} : @is_bounded _ (of_bounded B empty_mem subset_mem union_mem sUnion_univ) s ↔ s ∈ B := by rw [is_bounded_def, ←filter.mem_sets, of_bounded_cobounded_sets, set.mem_set_of_eq, compl_compl] variables [bornology α] lemma is_cobounded_bInter {s : set ι} {f : ι → set α} (hs : finite s) : is_cobounded (⋂ i ∈ s, f i) ↔ ∀ i ∈ s, is_cobounded (f i) := bInter_mem hs @[simp] lemma is_cobounded_bInter_finset (s : finset ι) {f : ι → set α} : is_cobounded (⋂ i ∈ s, f i) ↔ ∀ i ∈ s, is_cobounded (f i) := bInter_finset_mem s @[simp] lemma is_cobounded_Inter [fintype ι] {f : ι → set α} : is_cobounded (⋂ i, f i) ↔ ∀ i, is_cobounded (f i) := Inter_mem lemma is_cobounded_sInter {S : set (set α)} (hs : finite S) : is_cobounded (⋂₀ S) ↔ ∀ s ∈ S, is_cobounded s := sInter_mem hs lemma is_bounded_bUnion {s : set ι} {f : ι → set α} (hs : finite s) : is_bounded (⋃ i ∈ s, f i) ↔ ∀ i ∈ s, is_bounded (f i) := by simp only [← is_cobounded_compl_iff, compl_Union, is_cobounded_bInter hs] lemma is_bounded_bUnion_finset (s : finset ι) {f : ι → set α} : is_bounded (⋃ i ∈ s, f i) ↔ ∀ i ∈ s, is_bounded (f i) := is_bounded_bUnion s.finite_to_set lemma is_bounded_sUnion {S : set (set α)} (hs : finite S) : is_bounded (⋃₀ S) ↔ (∀ s ∈ S, is_bounded s) := by rw [sUnion_eq_bUnion, is_bounded_bUnion hs] @[simp] lemma is_bounded_Union [fintype ι] {s : ι → set α} : is_bounded (⋃ i, s i) ↔ ∀ i, is_bounded (s i) := by rw [← sUnion_range, is_bounded_sUnion (finite_range s), forall_range_iff] end bornology open bornology lemma set.finite.is_bounded [bornology α] {s : set α} (hs : s.finite) : is_bounded s := bornology.le_cofinite α hs.compl_mem_cofinite instance : bornology punit := ⟨⊥, bot_le⟩ /-- The cofinite filter as a bornology -/ @[reducible] def bornology.cofinite : bornology α := { cobounded := cofinite, le_cofinite := le_rfl } /-- A space with a `bornology` is a **bounded space** if `set.univ : set α` is bounded. -/ class bounded_space (α : Type*) [bornology α] : Prop := (bounded_univ : bornology.is_bounded (univ : set α)) namespace bornology variables [bornology α] lemma is_bounded_univ : is_bounded (univ : set α) ↔ bounded_space α := ⟨λ h, ⟨h⟩, λ h, h.1⟩ lemma cobounded_eq_bot_iff : cobounded α = ⊥ ↔ bounded_space α := by rw [← is_bounded_univ, is_bounded_def, compl_univ, empty_mem_iff_bot] variables [bounded_space α] lemma is_bounded.all (s : set α) : is_bounded s := bounded_space.bounded_univ.subset s.subset_univ lemma is_cobounded.all (s : set α) : is_cobounded s := compl_compl s ▸ is_bounded.all sᶜ variable (α) @[simp] lemma cobounded_eq_bot : cobounded α = ⊥ := cobounded_eq_bot_iff.2 ‹_› end bornology
5ef9ad0a494698d5cf03405bc158b716047c29ff
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/free_module/rank.lean
f4457a08efbbe1e71173bdf1985c94a5960489d9
[ "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,959
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import linear_algebra.free_module.basic import linear_algebra.finsupp_vector_space /-! # Rank of free modules This is a basic API for the rank of free modules. -/ universes u v w variables (R : Type u) (M : Type v) (N : Type w) open_locale tensor_product direct_sum big_operators cardinal open cardinal namespace module.free section ring variables [ring R] [strong_rank_condition R] variables [add_comm_group M] [module R M] [module.free R M] variables [add_comm_group N] [module R N] [module.free R N] /-- The rank of a free module `M` over `R` is the cardinality of `choose_basis_index R M`. -/ lemma rank_eq_card_choose_basis_index : module.rank R M = #(choose_basis_index R M) := (choose_basis R M).mk_eq_dim''.symm /-- The rank of `(ι →₀ R)` is `(# ι).lift`. -/ @[simp] lemma rank_finsupp {ι : Type v} : module.rank R (ι →₀ R) = (# ι).lift := by simpa [lift_id', lift_umax] using (basis.of_repr (linear_equiv.refl _ (ι →₀ R))).mk_eq_dim.symm /-- If `R` and `ι` lie in the same universe, the rank of `(ι →₀ R)` is `# ι`. -/ lemma rank_finsupp' {ι : Type u} : module.rank R (ι →₀ R) = # ι := by simp /-- The rank of `M × N` is `(module.rank R M).lift + (module.rank R N).lift`. -/ @[simp] lemma rank_prod : module.rank R (M × N) = lift.{w v} (module.rank R M) + lift.{v w} (module.rank R N) := by simpa [rank_eq_card_choose_basis_index R M, rank_eq_card_choose_basis_index R N, lift_umax, lift_umax'] using ((choose_basis R M).prod (choose_basis R N)).mk_eq_dim.symm /-- If `M` and `N` lie in the same universe, the rank of `M × N` is `(module.rank R M) + (module.rank R N)`. -/ lemma rank_prod' (N : Type v) [add_comm_group N] [module R N] [module.free R N] : module.rank R (M × N) = (module.rank R M) + (module.rank R N) := by simp /-- The rank of the direct sum is the sum of the ranks. -/ @[simp] lemma rank_direct_sum {ι : Type v} (M : ι → Type w) [Π (i : ι), add_comm_group (M i)] [Π (i : ι), module R (M i)] [Π (i : ι), module.free R (M i)] : module.rank R (⨁ i, M i) = cardinal.sum (λ i, module.rank R (M i)) := begin let B := λ i, choose_basis R (M i), let b : basis _ R (⨁ i, M i) := dfinsupp.basis (λ i, B i), simp [← b.mk_eq_dim'', λ i, (B i).mk_eq_dim''], end /-- The rank of a finite product is the sum of the ranks. -/ @[simp] lemma rank_pi_fintype {ι : Type v} [fintype ι] {M : ι → Type w} [Π (i : ι), add_comm_group (M i)] [Π (i : ι), module R (M i)] [Π (i : ι), module.free R (M i)] : module.rank R (Π i, M i) = cardinal.sum (λ i, module.rank R (M i)) := by { rw [← (direct_sum.linear_equiv_fun_on_fintype _ _ M).dim_eq, rank_direct_sum] } /-- If `m` and `n` are `fintype`, the rank of `m × n` matrices is `(# m).lift * (# n).lift`. -/ @[simp] lemma rank_matrix (m : Type v) (n : Type w) [fintype m] [fintype n] : module.rank R (matrix m n R) = (lift.{(max v w u) v} (# m)) * (lift.{(max v w u) w} (# n)) := begin have h := (matrix.std_basis R m n).mk_eq_dim, rw [← lift_lift.{(max v w u) (max v w)}, lift_inj] at h, simpa using h.symm, end /-- If `m` and `n` are `fintype` that lie in the same universe, the rank of `m × n` matrices is `(# n * # m).lift`. -/ @[simp] lemma rank_matrix' (m n : Type v) [fintype m] [fintype n] : module.rank R (matrix m n R) = (# m * # n).lift := by rw [rank_matrix, lift_mul, lift_umax] /-- If `m` and `n` are `fintype` that lie in the same universe as `R`, the rank of `m × n` matrices is `# m * # n`. -/ @[simp] lemma rank_matrix'' (m n : Type u) [fintype m] [fintype n] : module.rank R (matrix m n R) = # m * # n := by simp end ring section comm_ring variables [comm_ring R] [strong_rank_condition R] variables [add_comm_group M] [module R M] [module.free R M] variables [add_comm_group N] [module R N] [module.free R N] /-- The rank of `M ⊗[R] N` is `(module.rank R M).lift * (module.rank R N).lift`. -/ @[simp] lemma rank_tensor_product : module.rank R (M ⊗[R] N) = lift.{w v} (module.rank R M) * lift.{v w} (module.rank R N) := begin let ιM := choose_basis_index R M, let ιN := choose_basis_index R N, have h₁ := linear_equiv.lift_dim_eq (tensor_product.congr (repr R M) (repr R N)), let b : basis (ιM × ιN) R (_ →₀ R) := finsupp.basis_single_one, rw [linear_equiv.dim_eq (finsupp_tensor_finsupp' R ιM ιN), ← b.mk_eq_dim, mk_prod] at h₁, rw [lift_inj.1 h₁, rank_eq_card_choose_basis_index R M, rank_eq_card_choose_basis_index R N], end /-- If `M` and `N` lie in the same universe, the rank of `M ⊗[R] N` is `(module.rank R M) * (module.rank R N)`. -/ lemma rank_tensor_product' (N : Type v) [add_comm_group N] [module R N] [module.free R N] : module.rank R (M ⊗[R] N) = (module.rank R M) * (module.rank R N) := by simp end comm_ring end module.free
1230745f0b2d64c9a2e1686f7ed7992a2a8e2ba7
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/t11.lean
f0e5ef7cfe7e45e825753e485d8e765a5bb7388f
[ "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
390
lean
prelude constant A : Type.{1} definition bool : Type.{1} := Type.{0} constant Exists (P : A → bool) : bool notation `exists` binders `, ` b:(scoped b, Exists b) := b notation `∃` binders `, ` b:(scoped b, Exists b) := b constant p : A → bool constant q : A → A → bool check exists x : A, p x check ∃ x y : A, q x y notation `{` binder `|` b:scoped `}` := b check {x : A | x}
da58b3b9b9dff9d66fe2f15cc437144da1f8d4c3
82e44445c70db0f03e30d7be725775f122d72f3e
/src/algebra/lie/basic.lean
bc0159871f634ebad9896beb63dd095d200472f8
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
26,620
lean
/- Copyright (c) 2019 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import data.bracket import algebra.algebra.basic import tactic.noncomm_ring /-! # Lie algebras This file defines Lie rings and Lie algebras over a commutative ring together with their modules, morphisms and equivalences, as well as various lemmas to make these definitions usable. ## Main definitions * `lie_ring` * `lie_algebra` * `lie_ring_module` * `lie_module` * `lie_hom` * `lie_equiv` * `lie_module_hom` * `lie_module_equiv` ## Notation Working over a fixed commutative ring `R`, we introduce the notations: * `L →ₗ⁅R⁆ L'` for a morphism of Lie algebras, * `L ≃ₗ⁅R⁆ L'` for an equivalence of Lie algebras, * `M →ₗ⁅R,L⁆ N` for a morphism of Lie algebra modules `M`, `N` over a Lie algebra `L`, * `M ≃ₗ⁅R,L⁆ N` for an equivalence of Lie algebra modules `M`, `N` over a Lie algebra `L`. ## Implementation notes Lie algebras are defined as modules with a compatible Lie ring structure and thus, like modules, are partially unbundled. ## References * [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 1--3*](bourbaki1975) ## Tags lie bracket, jacobi identity, lie ring, lie algebra, lie module -/ universes u v w w₁ w₂ /-- A Lie ring is an additive group with compatible product, known as the bracket, satisfying the Jacobi identity. -/ @[protect_proj] class lie_ring (L : Type v) extends add_comm_group L, has_bracket L L := (add_lie : ∀ (x y z : L), ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆) (lie_add : ∀ (x y z : L), ⁅x, y + z⁆ = ⁅x, y⁆ + ⁅x, z⁆) (lie_self : ∀ (x : L), ⁅x, x⁆ = 0) (leibniz_lie : ∀ (x y z : L), ⁅x, ⁅y, z⁆⁆ = ⁅⁅x, y⁆, z⁆ + ⁅y, ⁅x, z⁆⁆) /-- A Lie algebra is a module with compatible product, known as the bracket, satisfying the Jacobi identity. Forgetting the scalar multiplication, every Lie algebra is a Lie ring. -/ @[protect_proj] class lie_algebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] extends module R L := (lie_smul : ∀ (t : R) (x y : L), ⁅x, t • y⁆ = t • ⁅x, y⁆) /-- A Lie ring module is an additive group, together with an additive action of a Lie ring on this group, such that the Lie bracket acts as the commutator of endomorphisms. (For representations of Lie *algebras* see `lie_module`.) -/ @[protect_proj] class lie_ring_module (L : Type v) (M : Type w) [lie_ring L] [add_comm_group M] extends has_bracket L M := (add_lie : ∀ (x y : L) (m : M), ⁅x + y, m⁆ = ⁅x, m⁆ + ⁅y, m⁆) (lie_add : ∀ (x : L) (m n : M), ⁅x, m + n⁆ = ⁅x, m⁆ + ⁅x, n⁆) (leibniz_lie : ∀ (x y : L) (m : M), ⁅x, ⁅y, m⁆⁆ = ⁅⁅x, y⁆, m⁆ + ⁅y, ⁅x, m⁆⁆) /-- A Lie module is a module over a commutative ring, together with a linear action of a Lie algebra on this module, such that the Lie bracket acts as the commutator of endomorphisms. -/ @[protect_proj] class lie_module (R : Type u) (L : Type v) (M : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] [lie_ring_module L M] := (smul_lie : ∀ (t : R) (x : L) (m : M), ⁅t • x, m⁆ = t • ⁅x, m⁆) (lie_smul : ∀ (t : R) (x : L) (m : M), ⁅x, t • m⁆ = t • ⁅x, m⁆) section basic_properties variables {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} variables [comm_ring R] [lie_ring L] [lie_algebra R L] variables [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] variables [add_comm_group N] [module R N] [lie_ring_module L N] [lie_module R L N] variables (t : R) (x y z : L) (m n : M) @[simp] lemma add_lie : ⁅x + y, m⁆ = ⁅x, m⁆ + ⁅y, m⁆ := lie_ring_module.add_lie x y m @[simp] lemma lie_add : ⁅x, m + n⁆ = ⁅x, m⁆ + ⁅x, n⁆ := lie_ring_module.lie_add x m n @[simp] lemma smul_lie : ⁅t • x, m⁆ = t • ⁅x, m⁆ := lie_module.smul_lie t x m @[simp] lemma lie_smul : ⁅x, t • m⁆ = t • ⁅x, m⁆ := lie_module.lie_smul t x m lemma leibniz_lie : ⁅x, ⁅y, m⁆⁆ = ⁅⁅x, y⁆, m⁆ + ⁅y, ⁅x, m⁆⁆ := lie_ring_module.leibniz_lie x y m @[simp] lemma lie_zero : ⁅x, 0⁆ = (0 : M) := (add_monoid_hom.mk' _ (lie_add x)).map_zero @[simp] lemma zero_lie : ⁅(0 : L), m⁆ = 0 := (add_monoid_hom.mk' (λ (x : L), ⁅x, m⁆) (λ x y, add_lie x y m)).map_zero @[simp] lemma lie_self : ⁅x, x⁆ = 0 := lie_ring.lie_self x instance lie_ring_self_module : lie_ring_module L L := { ..(infer_instance : lie_ring L) } @[simp] lemma lie_skew : -⁅y, x⁆ = ⁅x, y⁆ := have h : ⁅x + y, x⁆ + ⁅x + y, y⁆ = 0, { rw ← lie_add, apply lie_self, }, by simpa [neg_eq_iff_add_eq_zero] using h /-- Every Lie algebra is a module over itself. -/ instance lie_algebra_self_module : lie_module R L L := { smul_lie := λ t x m, by rw [←lie_skew, ←lie_skew x m, lie_algebra.lie_smul, smul_neg], lie_smul := by apply lie_algebra.lie_smul, } @[simp] lemma neg_lie : ⁅-x, m⁆ = -⁅x, m⁆ := by { rw [←sub_eq_zero, sub_neg_eq_add, ←add_lie], simp, } @[simp] lemma lie_neg : ⁅x, -m⁆ = -⁅x, m⁆ := by { rw [←sub_eq_zero, sub_neg_eq_add, ←lie_add], simp, } @[simp] lemma sub_lie : ⁅x - y, m⁆ = ⁅x, m⁆ - ⁅y, m⁆ := by simp [sub_eq_add_neg] @[simp] lemma lie_sub : ⁅x, m - n⁆ = ⁅x, m⁆ - ⁅x, n⁆ := by simp [sub_eq_add_neg] @[simp] lemma nsmul_lie (n : ℕ) : ⁅n • x, m⁆ = n • ⁅x, m⁆ := add_monoid_hom.map_nsmul ⟨λ (x : L), ⁅x, m⁆, zero_lie m, λ _ _, add_lie _ _ _⟩ _ _ @[simp] lemma lie_nsmul (n : ℕ) : ⁅x, n • m⁆ = n • ⁅x, m⁆ := add_monoid_hom.map_nsmul ⟨λ (m : M), ⁅x, m⁆, lie_zero x, λ _ _, lie_add _ _ _⟩ _ _ @[simp] lemma gsmul_lie (a : ℤ) : ⁅a • x, m⁆ = a • ⁅x, m⁆ := add_monoid_hom.map_gsmul ⟨λ (x : L), ⁅x, m⁆, zero_lie m, λ _ _, add_lie _ _ _⟩ _ _ @[simp] lemma lie_gsmul (a : ℤ) : ⁅x, a • m⁆ = a • ⁅x, m⁆ := add_monoid_hom.map_gsmul ⟨λ (m : M), ⁅x, m⁆, lie_zero x, λ _ _, lie_add _ _ _⟩ _ _ @[simp] lemma lie_lie : ⁅⁅x, y⁆, m⁆ = ⁅x, ⁅y, m⁆⁆ - ⁅y, ⁅x, m⁆⁆ := by rw [leibniz_lie, add_sub_cancel] lemma lie_jacobi : ⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0 := by { rw [← neg_neg ⁅x, y⁆, lie_neg z, lie_skew y x, ← lie_skew, lie_lie], abel, } instance lie_ring.int_lie_algebra : lie_algebra ℤ L := { lie_smul := λ n x y, lie_gsmul x y n, } instance : lie_ring_module L (M →ₗ[R] N) := { bracket := λ x f, { to_fun := λ m, ⁅x, f m⁆ - f ⁅x, m⁆, map_add' := λ m n, by { simp only [lie_add, linear_map.map_add], abel, }, map_smul' := λ t m, by simp only [smul_sub, linear_map.map_smul, lie_smul], }, add_lie := λ x y f, by { ext n, simp only [add_lie, linear_map.coe_mk, linear_map.add_apply, linear_map.map_add], abel, }, lie_add := λ x f g, by { ext n, simp only [linear_map.coe_mk, lie_add, linear_map.add_apply], abel, }, leibniz_lie := λ x y f, by { ext n, simp only [lie_lie, linear_map.coe_mk, linear_map.map_sub, linear_map.add_apply, lie_sub], abel, }, } @[simp] lemma lie_hom.lie_apply (f : M →ₗ[R] N) (x : L) (m : M) : ⁅x, f⁆ m = ⁅x, f m⁆ - f ⁅x, m⁆ := rfl instance : lie_module R L (M →ₗ[R] N) := { smul_lie := λ t x f, by { ext n, simp only [smul_sub, smul_lie, linear_map.smul_apply, lie_hom.lie_apply, linear_map.map_smul], }, lie_smul := λ t x f, by { ext n, simp only [smul_sub, linear_map.smul_apply, lie_hom.lie_apply, lie_smul], }, } end basic_properties set_option old_structure_cmd true /-- A morphism of Lie algebras is a linear map respecting the bracket operations. -/ structure lie_hom (R : Type u) (L : Type v) (L' : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] extends L →ₗ[R] L' := (map_lie' : ∀ {x y : L}, to_fun ⁅x, y⁆ = ⁅to_fun x, to_fun y⁆) attribute [nolint doc_blame] lie_hom.to_linear_map notation L ` →ₗ⁅`:25 R:25 `⁆ `:0 L':0 := lie_hom R L L' namespace lie_hom variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] instance : has_coe (L₁ →ₗ⁅R⁆ L₂) (L₁ →ₗ[R] L₂) := ⟨lie_hom.to_linear_map⟩ /-- see Note [function coercion] -/ instance : has_coe_to_fun (L₁ →ₗ⁅R⁆ L₂) := ⟨_, lie_hom.to_fun⟩ initialize_simps_projections lie_hom (to_fun → apply) @[simp, norm_cast] lemma coe_to_linear_map (f : L₁ →ₗ⁅R⁆ L₂) : ((f : L₁ →ₗ[R] L₂) : L₁ → L₂) = f := rfl @[simp] lemma to_fun_eq_coe (f : L₁ →ₗ⁅R⁆ L₂) : f.to_fun = ⇑f := rfl @[simp] lemma map_smul (f : L₁ →ₗ⁅R⁆ L₂) (c : R) (x : L₁) : f (c • x) = c • f x := linear_map.map_smul (f : L₁ →ₗ[R] L₂) c x @[simp] lemma map_add (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : f (x + y) = (f x) + (f y) := linear_map.map_add (f : L₁ →ₗ[R] L₂) x y @[simp] lemma map_sub (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : f (x - y) = (f x) - (f y) := linear_map.map_sub (f : L₁ →ₗ[R] L₂) x y @[simp] lemma map_neg (f : L₁ →ₗ⁅R⁆ L₂) (x : L₁) : f (-x) = -(f x) := linear_map.map_neg (f : L₁ →ₗ[R] L₂) x @[simp] lemma map_lie (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : f ⁅x, y⁆ = ⁅f x, f y⁆ := lie_hom.map_lie' f @[simp] lemma map_zero (f : L₁ →ₗ⁅R⁆ L₂) : f 0 = 0 := (f : L₁ →ₗ[R] L₂).map_zero /-- The identity map is a morphism of Lie algebras. -/ def id : L₁ →ₗ⁅R⁆ L₁ := { map_lie' := λ x y, rfl, .. (linear_map.id : L₁ →ₗ[R] L₁) } @[simp] lemma coe_id : ((id : L₁ →ₗ⁅R⁆ L₁) : L₁ → L₁) = _root_.id := rfl lemma id_apply (x : L₁) : (id : L₁ →ₗ⁅R⁆ L₁) x = x := rfl /-- The constant 0 map is a Lie algebra morphism. -/ instance : has_zero (L₁ →ₗ⁅R⁆ L₂) := ⟨{ map_lie' := by simp, ..(0 : L₁ →ₗ[R] L₂)}⟩ @[norm_cast, simp] lemma coe_zero : ((0 : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) = 0 := rfl lemma zero_apply (x : L₁) : (0 : L₁ →ₗ⁅R⁆ L₂) x = 0 := rfl /-- The identity map is a Lie algebra morphism. -/ instance : has_one (L₁ →ₗ⁅R⁆ L₁) := ⟨id⟩ @[simp] lemma coe_one : ((1 : (L₁ →ₗ⁅R⁆ L₁)) : L₁ → L₁) = _root_.id := rfl lemma one_apply (x : L₁) : (1 : (L₁ →ₗ⁅R⁆ L₁)) x = x := rfl instance : inhabited (L₁ →ₗ⁅R⁆ L₂) := ⟨0⟩ lemma coe_injective : @function.injective (L₁ →ₗ⁅R⁆ L₂) (L₁ → L₂) coe_fn := by rintro ⟨f, _⟩ ⟨g, _⟩ ⟨h⟩; congr @[ext] lemma ext {f g : L₁ →ₗ⁅R⁆ L₂} (h : ∀ x, f x = g x) : f = g := coe_injective $ funext h lemma ext_iff {f g : L₁ →ₗ⁅R⁆ L₂} : f = g ↔ ∀ x, f x = g x := ⟨by { rintro rfl x, refl }, ext⟩ lemma congr_fun {f g : L₁ →ₗ⁅R⁆ L₂} (h : f = g) (x : L₁) : f x = g x := h ▸ rfl @[simp] lemma mk_coe (f : L₁ →ₗ⁅R⁆ L₂) (h₁ h₂ h₃) : (⟨f, h₁, h₂, h₃⟩ : L₁ →ₗ⁅R⁆ L₂) = f := by { ext, refl, } @[simp] lemma coe_mk (f : L₁ → L₂) (h₁ h₂ h₃) : ((⟨f, h₁, h₂, h₃⟩ : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) = f := rfl @[norm_cast, simp] lemma coe_linear_mk (f : L₁ →ₗ[R] L₂) (h₁ h₂ h₃) : ((⟨f, h₁, h₂, h₃⟩ : L₁ →ₗ⁅R⁆ L₂) : L₁ →ₗ[R] L₂) = ⟨f, h₁, h₂⟩ := by { ext, refl, } /-- The composition of morphisms is a morphism. -/ def comp (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) : L₁ →ₗ⁅R⁆ L₃ := { map_lie' := λ x y, by { change f (g ⁅x, y⁆) = ⁅f (g x), f (g y)⁆, rw [map_lie, map_lie], }, ..linear_map.comp f.to_linear_map g.to_linear_map } lemma comp_apply (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) (x : L₁) : f.comp g x = f (g x) := rfl @[norm_cast, simp] lemma coe_comp (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) : (f.comp g : L₁ → L₃) = f ∘ g := rfl @[norm_cast, simp] lemma coe_linear_map_comp (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) : (f.comp g : L₁ →ₗ[R] L₃) = (f : L₂ →ₗ[R] L₃).comp (g : L₁ →ₗ[R] L₂) := rfl @[simp] lemma comp_id (f : L₁ →ₗ⁅R⁆ L₂) : f.comp (id : L₁ →ₗ⁅R⁆ L₁) = f := by { ext, refl, } @[simp] lemma id_comp (f : L₁ →ₗ⁅R⁆ L₂) : (id : L₂ →ₗ⁅R⁆ L₂).comp f = f := by { ext, refl, } /-- The inverse of a bijective morphism is a morphism. -/ def inverse (f : L₁ →ₗ⁅R⁆ L₂) (g : L₂ → L₁) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : L₂ →ₗ⁅R⁆ L₁ := { map_lie' := λ x y, calc g ⁅x, y⁆ = g ⁅f (g x), f (g y)⁆ : by { conv_lhs { rw [←h₂ x, ←h₂ y], }, } ... = g (f ⁅g x, g y⁆) : by rw map_lie ... = ⁅g x, g y⁆ : (h₁ _), ..linear_map.inverse f.to_linear_map g h₁ h₂ } end lie_hom /-- An equivalence of Lie algebras is a morphism which is also a linear equivalence. We could instead define an equivalence to be a morphism which is also a (plain) equivalence. However it is more convenient to define via linear equivalence to get `.to_linear_equiv` for free. -/ structure lie_equiv (R : Type u) (L : Type v) (L' : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] extends L →ₗ⁅R⁆ L', L ≃ₗ[R] L' attribute [nolint doc_blame] lie_equiv.to_lie_hom attribute [nolint doc_blame] lie_equiv.to_linear_equiv notation L ` ≃ₗ⁅`:50 R `⁆ ` L' := lie_equiv R L L' namespace lie_equiv variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] instance has_coe_to_lie_hom : has_coe (L₁ ≃ₗ⁅R⁆ L₂) (L₁ →ₗ⁅R⁆ L₂) := ⟨to_lie_hom⟩ instance has_coe_to_linear_equiv : has_coe (L₁ ≃ₗ⁅R⁆ L₂) (L₁ ≃ₗ[R] L₂) := ⟨to_linear_equiv⟩ /-- see Note [function coercion] -/ instance : has_coe_to_fun (L₁ ≃ₗ⁅R⁆ L₂) := ⟨_, to_fun⟩ @[simp, norm_cast] lemma coe_to_lie_equiv (e : L₁ ≃ₗ⁅R⁆ L₂) : ((e : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) = e := rfl @[simp, norm_cast] lemma coe_to_linear_equiv (e : L₁ ≃ₗ⁅R⁆ L₂) : ((e : L₁ ≃ₗ[R] L₂) : L₁ → L₂) = e := rfl instance : has_one (L₁ ≃ₗ⁅R⁆ L₁) := ⟨{ map_lie' := λ x y, by { change ((1 : L₁→ₗ[R] L₁) ⁅x, y⁆) = ⁅(1 : L₁→ₗ[R] L₁) x, (1 : L₁→ₗ[R] L₁) y⁆, simp, }, ..(1 : L₁ ≃ₗ[R] L₁)}⟩ @[simp] lemma one_apply (x : L₁) : (1 : (L₁ ≃ₗ⁅R⁆ L₁)) x = x := rfl instance : inhabited (L₁ ≃ₗ⁅R⁆ L₁) := ⟨1⟩ /-- Lie algebra equivalences are reflexive. -/ @[refl] def refl : L₁ ≃ₗ⁅R⁆ L₁ := 1 @[simp] lemma refl_apply (x : L₁) : (refl : L₁ ≃ₗ⁅R⁆ L₁) x = x := rfl /-- Lie algebra equivalences are symmetric. -/ @[symm] def symm (e : L₁ ≃ₗ⁅R⁆ L₂) : L₂ ≃ₗ⁅R⁆ L₁ := { ..lie_hom.inverse e.to_lie_hom e.inv_fun e.left_inv e.right_inv, ..e.to_linear_equiv.symm } @[simp] lemma symm_symm (e : L₁ ≃ₗ⁅R⁆ L₂) : e.symm.symm = e := by { cases e, refl, } @[simp] lemma apply_symm_apply (e : L₁ ≃ₗ⁅R⁆ L₂) : ∀ x, e (e.symm x) = x := e.to_linear_equiv.apply_symm_apply @[simp] lemma symm_apply_apply (e : L₁ ≃ₗ⁅R⁆ L₂) : ∀ x, e.symm (e x) = x := e.to_linear_equiv.symm_apply_apply /-- Lie algebra equivalences are transitive. -/ @[trans] def trans (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) : L₁ ≃ₗ⁅R⁆ L₃ := { ..lie_hom.comp e₂.to_lie_hom e₁.to_lie_hom, ..linear_equiv.trans e₁.to_linear_equiv e₂.to_linear_equiv } @[simp] lemma trans_apply (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) (x : L₁) : (e₁.trans e₂) x = e₂ (e₁ x) := rfl @[simp] lemma symm_trans_apply (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) (x : L₃) : (e₁.trans e₂).symm x = e₁.symm (e₂.symm x) := rfl lemma bijective (e : L₁ ≃ₗ⁅R⁆ L₂) : function.bijective ((e : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) := e.to_linear_equiv.bijective lemma injective (e : L₁ ≃ₗ⁅R⁆ L₂) : function.injective ((e : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) := e.to_linear_equiv.injective lemma surjective (e : L₁ ≃ₗ⁅R⁆ L₂) : function.surjective ((e : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) := e.to_linear_equiv.surjective end lie_equiv section lie_module_morphisms variables (R : Type u) (L : Type v) (M : Type w) (N : Type w₁) (P : Type w₂) variables [comm_ring R] [lie_ring L] [lie_algebra R L] variables [add_comm_group M] [add_comm_group N] [add_comm_group P] variables [module R M] [module R N] [module R P] variables [lie_ring_module L M] [lie_ring_module L N] [lie_ring_module L P] variables [lie_module R L M] [lie_module R L N] [lie_module R L P] set_option old_structure_cmd true /-- A morphism of Lie algebra modules is a linear map which commutes with the action of the Lie algebra. -/ structure lie_module_hom extends M →ₗ[R] N := (map_lie' : ∀ {x : L} {m : M}, to_fun ⁅x, m⁆ = ⁅x, to_fun m⁆) attribute [nolint doc_blame] lie_module_hom.to_linear_map notation M ` →ₗ⁅`:25 R,L:25 `⁆ `:0 N:0 := lie_module_hom R L M N namespace lie_module_hom variables {R L M N P} instance : has_coe (M →ₗ⁅R,L⁆ N) (M →ₗ[R] N) := ⟨lie_module_hom.to_linear_map⟩ /-- see Note [function coercion] -/ instance : has_coe_to_fun (M →ₗ⁅R,L⁆ N) := ⟨_, lie_module_hom.to_fun⟩ @[simp, norm_cast] lemma coe_to_linear_map (f : M →ₗ⁅R,L⁆ N) : ((f : M →ₗ[R] N) : M → N) = f := rfl @[simp] lemma map_smul (f : M →ₗ⁅R,L⁆ N) (c : R) (x : M) : f (c • x) = c • f x := linear_map.map_smul (f : M →ₗ[R] N) c x @[simp] lemma map_add (f : M →ₗ⁅R,L⁆ N) (x y : M) : f (x + y) = (f x) + (f y) := linear_map.map_add (f : M →ₗ[R] N) x y @[simp] lemma map_sub (f : M →ₗ⁅R,L⁆ N) (x y : M) : f (x - y) = (f x) - (f y) := linear_map.map_sub (f : M →ₗ[R] N) x y @[simp] lemma map_neg (f : M →ₗ⁅R,L⁆ N) (x : M) : f (-x) = -(f x) := linear_map.map_neg (f : M →ₗ[R] N) x @[simp] lemma map_lie (f : M →ₗ⁅R,L⁆ N) (x : L) (m : M) : f ⁅x, m⁆ = ⁅x, f m⁆ := lie_module_hom.map_lie' f lemma map_lie₂ (f : M →ₗ⁅R,L⁆ N →ₗ[R] P) (x : L) (m : M) (n : N) : ⁅x, f m n⁆ = f ⁅x, m⁆ n + f m ⁅x, n⁆ := by simp only [sub_add_cancel, map_lie, lie_hom.lie_apply] @[simp] lemma map_zero (f : M →ₗ⁅R,L⁆ N) : f 0 = 0 := linear_map.map_zero (f : M →ₗ[R] N) /-- The constant 0 map is a Lie module morphism. -/ instance : has_zero (M →ₗ⁅R,L⁆ N) := ⟨{ map_lie' := by simp, ..(0 : M →ₗ[R] N) }⟩ @[norm_cast, simp] lemma coe_zero : ((0 : M →ₗ⁅R,L⁆ N) : M → N) = 0 := rfl lemma zero_apply (m : M) : (0 : M →ₗ⁅R,L⁆ N) m = 0 := rfl /-- The identity map is a Lie module morphism. -/ instance : has_one (M →ₗ⁅R,L⁆ M) := ⟨{ map_lie' := by simp, ..(1 : M →ₗ[R] M) }⟩ instance : inhabited (M →ₗ⁅R,L⁆ N) := ⟨0⟩ lemma coe_injective : @function.injective (M →ₗ⁅R,L⁆ N) (M → N) coe_fn := by { rintros ⟨f, _⟩ ⟨g, _⟩ ⟨h⟩, congr, } @[ext] lemma ext {f g : M →ₗ⁅R,L⁆ N} (h : ∀ m, f m = g m) : f = g := coe_injective $ funext h lemma ext_iff {f g : M →ₗ⁅R,L⁆ N} : f = g ↔ ∀ m, f m = g m := ⟨by { rintro rfl m, refl, }, ext⟩ @[simp] lemma mk_coe (f : M →ₗ⁅R,L⁆ N) (h₁ h₂ h₃) : (⟨f, h₁, h₂, h₃⟩ : M →ₗ⁅R,L⁆ N) = f := by { ext, refl, } @[simp] lemma coe_mk (f : M → N) (h₁ h₂ h₃) : ((⟨f, h₁, h₂, h₃⟩ : M →ₗ⁅R,L⁆ N) : M → N) = f := rfl @[norm_cast, simp] lemma coe_linear_mk (f : M →ₗ[R] N) (h₁ h₂ h₃) : ((⟨f, h₁, h₂, h₃⟩ : M →ₗ⁅R,L⁆ N) : M →ₗ[R] N) = ⟨f, h₁, h₂⟩ := by { ext, refl, } /-- The composition of Lie module morphisms is a morphism. -/ def comp (f : N →ₗ⁅R,L⁆ P) (g : M →ₗ⁅R,L⁆ N) : M →ₗ⁅R,L⁆ P := { map_lie' := λ x m, by { change f (g ⁅x, m⁆) = ⁅x, f (g m)⁆, rw [map_lie, map_lie], }, ..linear_map.comp f.to_linear_map g.to_linear_map } lemma comp_apply (f : N →ₗ⁅R,L⁆ P) (g : M →ₗ⁅R,L⁆ N) (m : M) : f.comp g m = f (g m) := rfl @[norm_cast, simp] lemma coe_comp (f : N →ₗ⁅R,L⁆ P) (g : M →ₗ⁅R,L⁆ N) : (f.comp g : M → P) = f ∘ g := rfl @[norm_cast, simp] lemma coe_linear_map_comp (f : N →ₗ⁅R,L⁆ P) (g : M →ₗ⁅R,L⁆ N) : (f.comp g : M →ₗ[R] P) = (f : N →ₗ[R] P).comp (g : M →ₗ[R] N) := rfl /-- The inverse of a bijective morphism of Lie modules is a morphism of Lie modules. -/ def inverse (f : M →ₗ⁅R,L⁆ N) (g : N → M) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : N →ₗ⁅R,L⁆ M := { map_lie' := λ x n, calc g ⁅x, n⁆ = g ⁅x, f (g n)⁆ : by rw h₂ ... = g (f ⁅x, g n⁆) : by rw map_lie ... = ⁅x, g n⁆ : (h₁ _), ..linear_map.inverse f.to_linear_map g h₁ h₂ } instance : has_add (M →ₗ⁅R,L⁆ N) := { add := λ f g, { map_lie' := by simp, ..((f : M →ₗ[R] N) + (g : M →ₗ[R] N)) }, } instance : has_sub (M →ₗ⁅R,L⁆ N) := { sub := λ f g, { map_lie' := by simp, ..((f : M →ₗ[R] N) - (g : M →ₗ[R] N)) }, } instance : has_neg (M →ₗ⁅R,L⁆ N) := { neg := λ f, { map_lie' := by simp, ..(-(f : (M →ₗ[R] N))) }, } @[norm_cast, simp] lemma coe_add (f g : M →ₗ⁅R,L⁆ N) : ⇑(f + g) = f + g := rfl lemma add_apply (f g : M →ₗ⁅R,L⁆ N) (m : M) : (f + g) m = f m + g m := rfl @[norm_cast, simp] lemma coe_sub (f g : M →ₗ⁅R,L⁆ N) : ⇑(f - g) = f - g := rfl lemma sub_apply (f g : M →ₗ⁅R,L⁆ N) (m : M) : (f - g) m = f m - g m := rfl @[norm_cast, simp] lemma coe_neg (f : M →ₗ⁅R,L⁆ N) : ⇑(-f) = -f := rfl lemma neg_apply (f : M →ₗ⁅R,L⁆ N) (m : M) : (-f) m = -(f m) := rfl instance : add_comm_group (M →ₗ⁅R,L⁆ N) := { zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, nsmul := λ n f, { map_lie' := λ x m, by simp, ..(n • (f : M →ₗ[R] N)) }, nsmul_zero' := λ f, by { ext, simp, }, nsmul_succ' := λ n f, by { ext, simp [nat.succ_eq_one_add, add_nsmul], }, ..(coe_injective.add_comm_group _ coe_zero coe_add coe_neg coe_sub : add_comm_group (M →ₗ⁅R,L⁆ N)) } instance : has_scalar R (M →ₗ⁅R,L⁆ N) := { smul := λ t f, { map_lie' := by simp, ..(t • (f : M →ₗ[R] N)) }, } @[norm_cast, simp] lemma coe_smul (t : R) (f : M →ₗ⁅R,L⁆ N) : ⇑(t • f) = t • f := rfl lemma smul_apply (t : R) (f : M →ₗ⁅R,L⁆ N) (m : M) : (t • f) m = t • (f m) := rfl instance : module R (M →ₗ⁅R,L⁆ N) := function.injective.module R ⟨to_fun, rfl, coe_add⟩ coe_injective coe_smul end lie_module_hom /-- An equivalence of Lie algebra modules is a linear equivalence which is also a morphism of Lie algebra modules. -/ structure lie_module_equiv extends M ≃ₗ[R] N, M →ₗ⁅R,L⁆ N, M ≃ N attribute [nolint doc_blame] lie_module_equiv.to_equiv attribute [nolint doc_blame] lie_module_equiv.to_lie_module_hom attribute [nolint doc_blame] lie_module_equiv.to_linear_equiv notation M ` ≃ₗ⁅`:25 R,L:25 `⁆ `:0 N:0 := lie_module_equiv R L M N namespace lie_module_equiv variables {R L M N P} instance has_coe_to_equiv : has_coe (M ≃ₗ⁅R,L⁆ N) (M ≃ N) := ⟨to_equiv⟩ instance has_coe_to_lie_module_hom : has_coe (M ≃ₗ⁅R,L⁆ N) (M →ₗ⁅R,L⁆ N) := ⟨to_lie_module_hom⟩ instance has_coe_to_linear_equiv : has_coe (M ≃ₗ⁅R,L⁆ N) (M ≃ₗ[R] N) := ⟨to_linear_equiv⟩ /-- see Note [function coercion] -/ instance : has_coe_to_fun (M ≃ₗ⁅R,L⁆ N) := ⟨_, to_fun⟩ @[simp] lemma coe_mk (f : M → N) (h₁ h₂ F h₃ h₄ h₅) : ((⟨f, h₁, h₂, F, h₃, h₄, h₅⟩ : M ≃ₗ⁅R,L⁆ N) : M → N) = f := rfl @[simp, norm_cast] lemma coe_to_lie_module_hom (e : M ≃ₗ⁅R,L⁆ N) : ((e : M →ₗ⁅R,L⁆ N) : M → N) = e := rfl @[simp, norm_cast] lemma coe_to_linear_equiv (e : M ≃ₗ⁅R,L⁆ N) : ((e : M ≃ₗ[R] N) : M → N) = e := rfl lemma to_equiv_injective : function.injective (to_equiv : (M ≃ₗ⁅R,L⁆ N) → M ≃ N) := λ ⟨_, _, _, _, _, _, _⟩ ⟨_, _, _, _, _, _, _⟩ h, lie_module_equiv.mk.inj_eq.mpr (equiv.mk.inj h) @[ext] lemma ext (e₁ e₂ : M ≃ₗ⁅R,L⁆ N) (h : ∀ m, e₁ m = e₂ m) : e₁ = e₂ := to_equiv_injective (equiv.ext h) instance : has_one (M ≃ₗ⁅R,L⁆ M) := ⟨{ map_lie' := λ x m, rfl, ..(1 : M ≃ₗ[R] M) }⟩ @[simp] lemma one_apply (m : M) : (1 : (M ≃ₗ⁅R,L⁆ M)) m = m := rfl instance : inhabited (M ≃ₗ⁅R,L⁆ M) := ⟨1⟩ /-- Lie module equivalences are reflexive. -/ @[refl] def refl : M ≃ₗ⁅R,L⁆ M := 1 @[simp] lemma refl_apply (m : M) : (refl : M ≃ₗ⁅R,L⁆ M) m = m := rfl /-- Lie module equivalences are syemmtric. -/ @[symm] def symm (e : M ≃ₗ⁅R,L⁆ N) : N ≃ₗ⁅R,L⁆ M := { ..lie_module_hom.inverse e.to_lie_module_hom e.inv_fun e.left_inv e.right_inv, ..(e : M ≃ₗ[R] N).symm } @[simp] lemma symm_symm (e : M ≃ₗ⁅R,L⁆ N) : e.symm.symm = e := by { cases e, refl, } @[simp] lemma apply_symm_apply (e : M ≃ₗ⁅R,L⁆ N) : ∀ x, e (e.symm x) = x := e.to_linear_equiv.apply_symm_apply @[simp] lemma symm_apply_apply (e : M ≃ₗ⁅R,L⁆ N) : ∀ x, e.symm (e x) = x := e.to_linear_equiv.symm_apply_apply /-- Lie module equivalences are transitive. -/ @[trans] def trans (e₁ : M ≃ₗ⁅R,L⁆ N) (e₂ : N ≃ₗ⁅R,L⁆ P) : M ≃ₗ⁅R,L⁆ P := { ..lie_module_hom.comp e₂.to_lie_module_hom e₁.to_lie_module_hom, ..linear_equiv.trans e₁.to_linear_equiv e₂.to_linear_equiv } @[simp] lemma trans_apply (e₁ : M ≃ₗ⁅R,L⁆ N) (e₂ : N ≃ₗ⁅R,L⁆ P) (m : M) : (e₁.trans e₂) m = e₂ (e₁ m) := rfl @[simp] lemma symm_trans_apply (e₁ : M ≃ₗ⁅R,L⁆ N) (e₂ : N ≃ₗ⁅R,L⁆ P) (p : P) : (e₁.trans e₂).symm p = e₁.symm (e₂.symm p) := rfl end lie_module_equiv end lie_module_morphisms
42409c6a7a12489586e4f8d30fb8f74ce2aeff58
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/algebra/star/self_adjoint.lean
ded683b24709057f70a866f20b811d5cbda04303
[ "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
13,789
lean
/- Copyright (c) 2021 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import algebra.star.basic import group_theory.subgroup.basic /-! # Self-adjoint, skew-adjoint and normal elements of a star additive group This file defines `self_adjoint R` (resp. `skew_adjoint R`), where `R` is a star additive group, as the additive subgroup containing the elements that satisfy `star x = x` (resp. `star x = -x`). This includes, for instance, (skew-)Hermitian operators on Hilbert spaces. We also define `is_star_normal R`, a `Prop` that states that an element `x` satisfies `star x * x = x * star x`. ## Implementation notes * When `R` is a `star_module R₂ R`, then `self_adjoint R` has a natural `module (self_adjoint R₂) (self_adjoint R)` structure. However, doing this literally would be undesirable since in the main case of interest (`R₂ = ℂ`) we want `module ℝ (self_adjoint R)` and not `module (self_adjoint ℂ) (self_adjoint R)`. We solve this issue by adding the typeclass `[has_trivial_star R₃]`, of which `ℝ` is an instance (registered in `data/real/basic`), and then add a `[module R₃ (self_adjoint R)]` instance whenever we have `[module R₃ R] [has_trivial_star R₃]`. (Another approach would have been to define `[star_invariant_scalars R₃ R]` to express the fact that `star (x • v) = x • star v`, but this typeclass would have the disadvantage of taking two type arguments.) ## TODO * Define `λ z x, z * x * star z` (i.e. conjugation by `z`) as a monoid action of `R` on `R` (similar to the existing `conj_act` for groups), and then state the fact that `self_adjoint R` is invariant under it. -/ variables {R A : Type*} /-- An element is self-adjoint if it is equal to its star. -/ def is_self_adjoint [has_star R] (x : R) : Prop := star x = x /-- An element of a star monoid is normal if it commutes with its adjoint. -/ class is_star_normal [has_mul R] [has_star R] (x : R) : Prop := (star_comm_self : commute (star x) x) export is_star_normal (star_comm_self) lemma star_comm_self' [has_mul R] [has_star R] (x : R) [is_star_normal x] : (star x) * x = x * star x := is_star_normal.star_comm_self namespace is_self_adjoint lemma star_eq [has_star R] {x : R} (hx : is_self_adjoint x) : star x = x := hx lemma _root_.is_self_adjoint_iff [has_star R] {x : R} : is_self_adjoint x ↔ star x = x := iff.rfl section add_group variables [add_group R] [star_add_monoid R] variables (R) lemma _root_.is_self_adjoint_zero : is_self_adjoint (0 : R) := star_zero R variables {R} lemma add {x y : R} (hx : is_self_adjoint x) (hy : is_self_adjoint y) : is_self_adjoint (x + y) := by simp only [is_self_adjoint_iff, star_add, hx.star_eq, hy.star_eq] lemma neg {x : R} (hx : is_self_adjoint x) : is_self_adjoint (-x) := by simp only [is_self_adjoint_iff, star_neg, hx.star_eq] lemma sub {x y : R} (hx : is_self_adjoint x) (hy : is_self_adjoint y) : is_self_adjoint (x - y) := by simp only [is_self_adjoint_iff, star_sub, hx.star_eq, hy.star_eq] lemma bit0 {x : R} (hx : is_self_adjoint x) : is_self_adjoint (bit0 x) := by simp only [is_self_adjoint_iff, star_bit0, hx.star_eq] end add_group section non_unital_semiring variables [non_unital_semiring R] [star_ring R] lemma conjugate {x : R} (hx : is_self_adjoint x) (z : R) : is_self_adjoint (z * x * star z) := by simp only [is_self_adjoint_iff, star_mul, star_star, mul_assoc, hx.star_eq] lemma conjugate' {x : R} (hx : is_self_adjoint x) (z : R) : is_self_adjoint (star z * x * z) := by simp only [is_self_adjoint_iff, star_mul, star_star, mul_assoc, hx.star_eq] lemma is_star_normal {x : R} (hx : is_self_adjoint x) : is_star_normal x := ⟨by simp only [hx.star_eq]⟩ end non_unital_semiring section ring variables [ring R] [star_ring R] variables (R) lemma _root_.is_self_adjoint_one : is_self_adjoint (1 : R) := star_one R variables {R} lemma bit1 {x : R} (hx : is_self_adjoint x) : is_self_adjoint (bit1 x) := by simp only [is_self_adjoint_iff, star_bit1, hx.star_eq] lemma pow {x : R} (hx : is_self_adjoint x) (n : ℕ) : is_self_adjoint (x ^ n):= by simp only [is_self_adjoint_iff, star_pow, hx.star_eq] end ring section non_unital_comm_ring variables [non_unital_comm_ring R] [star_ring R] lemma mul {x y : R} (hx : is_self_adjoint x) (hy : is_self_adjoint y) : is_self_adjoint (x * y) := by simp only [is_self_adjoint_iff, star_mul', hx.star_eq, hy.star_eq] end non_unital_comm_ring section field variables [field R] [star_ring R] lemma inv {x : R} (hx : is_self_adjoint x) : is_self_adjoint x⁻¹ := by simp only [is_self_adjoint_iff, star_inv', hx.star_eq] lemma div {x y : R} (hx : is_self_adjoint x) (hy : is_self_adjoint y) : is_self_adjoint (x / y) := by simp only [is_self_adjoint_iff, star_div', hx.star_eq, hy.star_eq] lemma zpow {x : R} (hx : is_self_adjoint x) (n : ℤ) : is_self_adjoint (x ^ n):= by simp only [is_self_adjoint_iff, star_zpow₀, hx.star_eq] end field section has_smul variables [has_star R] [has_trivial_star R] [add_group A] [star_add_monoid A] lemma smul [has_smul R A] [star_module R A] (r : R) {x : A} (hx : is_self_adjoint x) : is_self_adjoint (r • x) := by simp only [is_self_adjoint_iff, star_smul, star_trivial, hx.star_eq] end has_smul end is_self_adjoint variables (R) /-- The self-adjoint elements of a star additive group, as an additive subgroup. -/ def self_adjoint [add_group R] [star_add_monoid R] : add_subgroup R := { carrier := {x | is_self_adjoint x}, zero_mem' := star_zero R, add_mem' := λ _ _ hx, hx.add, neg_mem' := λ _ hx, hx.neg } /-- The skew-adjoint elements of a star additive group, as an additive subgroup. -/ def skew_adjoint [add_comm_group R] [star_add_monoid R] : add_subgroup R := { carrier := {x | star x = -x}, zero_mem' := show star (0 : R) = -0, by simp only [star_zero, neg_zero], add_mem' := λ x y (hx : star x = -x) (hy : star y = -y), show star (x + y) = -(x + y), by rw [star_add x y, hx, hy, neg_add], neg_mem' := λ x (hx : star x = -x), show star (-x) = (- -x), by simp only [hx, star_neg] } variables {R} namespace self_adjoint section add_group variables [add_group R] [star_add_monoid R] lemma mem_iff {x : R} : x ∈ self_adjoint R ↔ star x = x := by { rw [←add_subgroup.mem_carrier], exact iff.rfl } @[simp, norm_cast] lemma star_coe_eq {x : self_adjoint R} : star (x : R) = x := x.prop instance : inhabited (self_adjoint R) := ⟨0⟩ end add_group section ring variables [ring R] [star_ring R] instance : has_one (self_adjoint R) := ⟨⟨1, is_self_adjoint_one R⟩⟩ @[simp, norm_cast] lemma coe_one : ↑(1 : self_adjoint R) = (1 : R) := rfl instance [nontrivial R] : nontrivial (self_adjoint R) := ⟨⟨0, 1, subtype.ne_of_val_ne zero_ne_one⟩⟩ instance : has_nat_cast (self_adjoint R) := ⟨λ n, ⟨n, nat.rec_on n (by simp [zero_mem]) (λ k hk, (@nat.cast_succ R _ k).symm ▸ add_mem hk (is_self_adjoint_one R))⟩⟩ instance : has_int_cast (self_adjoint R) := ⟨λ n, ⟨n, begin cases n; simp [show ↑n ∈ self_adjoint R, from (n : self_adjoint R).2], refine add_mem (is_self_adjoint_one R).neg (n : self_adjoint R).2.neg, end ⟩ ⟩ instance : has_pow (self_adjoint R) ℕ := ⟨λ x n, ⟨(x : R) ^ n, x.prop.pow n⟩⟩ @[simp, norm_cast] lemma coe_pow (x : self_adjoint R) (n : ℕ) : ↑(x ^ n) = (x : R) ^ n := rfl end ring section non_unital_comm_ring variables [non_unital_comm_ring R] [star_ring R] instance : has_mul (self_adjoint R) := ⟨λ x y, ⟨(x : R) * y, x.prop.mul y.prop⟩⟩ @[simp, norm_cast] lemma coe_mul (x y : self_adjoint R) : ↑(x * y) = (x : R) * y := rfl end non_unital_comm_ring section comm_ring variables [comm_ring R] [star_ring R] instance : comm_ring (self_adjoint R) := function.injective.comm_ring _ subtype.coe_injective (self_adjoint R).coe_zero coe_one (self_adjoint R).coe_add coe_mul (self_adjoint R).coe_neg (self_adjoint R).coe_sub (self_adjoint R).coe_nsmul (self_adjoint R).coe_zsmul coe_pow (λ _, rfl) (λ _, rfl) end comm_ring section field variables [field R] [star_ring R] instance : has_inv (self_adjoint R) := { inv := λ x, ⟨(x.val)⁻¹, x.prop.inv⟩ } @[simp, norm_cast] lemma coe_inv (x : self_adjoint R) : ↑(x⁻¹) = (x : R)⁻¹ := rfl instance : has_div (self_adjoint R) := { div := λ x y, ⟨x / y, x.prop.div y.prop⟩ } @[simp, norm_cast] lemma coe_div (x y : self_adjoint R) : ↑(x / y) = (x / y : R) := rfl instance : has_pow (self_adjoint R) ℤ := { pow := λ x z, ⟨x ^ z, x.prop.zpow z⟩ } @[simp, norm_cast] lemma coe_zpow (x : self_adjoint R) (z : ℤ) : ↑(x ^ z) = (x : R) ^ z := rfl lemma rat_cast_mem : ∀ (x : ℚ), (x : R) ∈ self_adjoint R | ⟨a, b, h1, h2⟩ := by rw [mem_iff, rat.cast_mk', star_mul', star_inv', star_nat_cast, star_int_cast] instance : has_rat_cast (self_adjoint R) := ⟨λ n, ⟨n, rat_cast_mem n⟩⟩ @[simp, norm_cast] lemma coe_rat_cast (x : ℚ) : ↑(x : self_adjoint R) = (x : R) := rfl instance has_qsmul : has_smul ℚ (self_adjoint R) := ⟨λ a x, ⟨a • x, by rw rat.smul_def; exact (rat_cast_mem a).mul x.prop⟩⟩ @[simp, norm_cast] lemma coe_rat_smul (x : self_adjoint R) (a : ℚ) : ↑(a • x) = a • (x : R) := rfl instance : field (self_adjoint R) := function.injective.field _ subtype.coe_injective (self_adjoint R).coe_zero coe_one (self_adjoint R).coe_add coe_mul (self_adjoint R).coe_neg (self_adjoint R).coe_sub coe_inv coe_div (self_adjoint R).coe_nsmul (self_adjoint R).coe_zsmul coe_rat_smul coe_pow coe_zpow (λ _, rfl) (λ _, rfl) coe_rat_cast end field section has_smul variables [has_star R] [has_trivial_star R] [add_group A] [star_add_monoid A] instance [has_smul R A] [star_module R A] : has_smul R (self_adjoint A) := ⟨λ r x, ⟨r • x, x.prop.smul r⟩⟩ @[simp, norm_cast] lemma coe_smul [has_smul R A] [star_module R A] (r : R) (x : self_adjoint A) : ↑(r • x) = r • (x : A) := rfl instance [monoid R] [mul_action R A] [star_module R A] : mul_action R (self_adjoint A) := function.injective.mul_action coe subtype.coe_injective coe_smul instance [monoid R] [distrib_mul_action R A] [star_module R A] : distrib_mul_action R (self_adjoint A) := function.injective.distrib_mul_action (self_adjoint A).subtype subtype.coe_injective coe_smul end has_smul section module variables [has_star R] [has_trivial_star R] [add_comm_group A] [star_add_monoid A] instance [semiring R] [module R A] [star_module R A] : module R (self_adjoint A) := function.injective.module R (self_adjoint A).subtype subtype.coe_injective coe_smul end module end self_adjoint namespace skew_adjoint section add_group variables [add_comm_group R] [star_add_monoid R] lemma mem_iff {x : R} : x ∈ skew_adjoint R ↔ star x = -x := by { rw [←add_subgroup.mem_carrier], exact iff.rfl } @[simp, norm_cast] lemma star_coe_eq {x : skew_adjoint R} : star (x : R) = -x := x.prop instance : inhabited (skew_adjoint R) := ⟨0⟩ lemma bit0_mem {x : R} (hx : x ∈ skew_adjoint R) : bit0 x ∈ skew_adjoint R := by rw [mem_iff, star_bit0, mem_iff.mp hx, bit0, bit0, neg_add] end add_group section ring variables [ring R] [star_ring R] lemma conjugate {x : R} (hx : x ∈ skew_adjoint R) (z : R) : z * x * star z ∈ skew_adjoint R := by simp only [mem_iff, star_mul, star_star, mem_iff.mp hx, neg_mul, mul_neg, mul_assoc] lemma conjugate' {x : R} (hx : x ∈ skew_adjoint R) (z : R) : star z * x * z ∈ skew_adjoint R := by simp only [mem_iff, star_mul, star_star, mem_iff.mp hx, neg_mul, mul_neg, mul_assoc] lemma is_star_normal_of_mem {x : R} (hx : x ∈ skew_adjoint R) : is_star_normal x := ⟨by { simp only [mem_iff] at hx, simp only [hx, commute.neg_left] }⟩ instance (x : skew_adjoint R) : is_star_normal (x : R) := is_star_normal_of_mem (set_like.coe_mem _) end ring section has_smul variables [has_star R] [has_trivial_star R] [add_comm_group A] [star_add_monoid A] lemma smul_mem [monoid R] [distrib_mul_action R A] [star_module R A] (r : R) {x : A} (h : x ∈ skew_adjoint A) : r • x ∈ skew_adjoint A := by rw [mem_iff, star_smul, star_trivial, mem_iff.mp h, smul_neg r] instance [monoid R] [distrib_mul_action R A] [star_module R A] : has_smul R (skew_adjoint A) := ⟨λ r x, ⟨r • x, smul_mem r x.prop⟩⟩ @[simp, norm_cast] lemma coe_smul [monoid R] [distrib_mul_action R A] [star_module R A] (r : R) (x : skew_adjoint A) : ↑(r • x) = r • (x : A) := rfl instance [monoid R] [distrib_mul_action R A] [star_module R A] : distrib_mul_action R (skew_adjoint A) := function.injective.distrib_mul_action (skew_adjoint A).subtype subtype.coe_injective coe_smul instance [semiring R] [module R A] [star_module R A] : module R (skew_adjoint A) := function.injective.module R (skew_adjoint A).subtype subtype.coe_injective coe_smul end has_smul end skew_adjoint instance is_star_normal_zero [semiring R] [star_ring R] : is_star_normal (0 : R) := ⟨by simp only [star_comm_self, star_zero]⟩ instance is_star_normal_one [monoid R] [star_semigroup R] : is_star_normal (1 : R) := ⟨by simp only [star_comm_self, star_one]⟩ instance is_star_normal_star_self [monoid R] [star_semigroup R] {x : R} [is_star_normal x] : is_star_normal (star x) := ⟨show star (star x) * (star x) = (star x) * star (star x), by rw [star_star, star_comm_self']⟩ @[priority 100] -- see Note [lower instance priority] instance has_trivial_star.is_star_normal [monoid R] [star_semigroup R] [has_trivial_star R] {x : R} : is_star_normal x := ⟨by rw [star_trivial]⟩ @[priority 100] -- see Note [lower instance priority] instance comm_monoid.is_star_normal [comm_monoid R] [star_semigroup R] {x : R} : is_star_normal x := ⟨mul_comm _ _⟩
dcc0bb0fd40a8a094ef41ba60f82eca786222076
26ac254ecb57ffcb886ff709cf018390161a9225
/src/topology/algebra/group.lean
abc1dbe62ad8318a7182def4e47e578942267716
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
20,773
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot Theory of topological groups. -/ import order.filter.pointwise import group_theory.quotient_group import topology.algebra.monoid import topology.homeomorph open classical set filter topological_space open_locale classical topological_space universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section topological_group section prio set_option default_priority 100 -- see Note [default priority] /-- A topological (additive) group is a group in which the addition and negation operations are continuous. -/ class topological_add_group (α : Type u) [topological_space α] [add_group α] extends topological_add_monoid α : Prop := (continuous_neg : continuous (λa:α, -a)) /-- A topological group is a group in which the multiplication and inversion operations are continuous. -/ @[to_additive topological_add_group] class topological_group (α : Type*) [topological_space α] [group α] extends topological_monoid α : Prop := (continuous_inv : continuous (λa:α, a⁻¹)) end prio variables [topological_space α] [group α] @[to_additive] lemma continuous_inv [topological_group α] : continuous (λx:α, x⁻¹) := topological_group.continuous_inv @[to_additive] lemma continuous.inv [topological_group α] [topological_space β] {f : β → α} (hf : continuous f) : continuous (λx, (f x)⁻¹) := continuous_inv.comp hf @[to_additive] lemma continuous_on_inv [topological_group α] {s : set α} : continuous_on (λx:α, x⁻¹) s := continuous_inv.continuous_on @[to_additive] lemma continuous_on.inv [topological_group α] [topological_space β] {f : β → α} {s : set β} (hf : continuous_on f s) : continuous_on (λx, (f x)⁻¹) s := continuous_inv.comp_continuous_on hf @[to_additive] lemma tendsto_inv {α : Type*} [group α] [topological_space α] [topological_group α] (a : α) : tendsto (λ x, x⁻¹) (nhds a) (nhds (a⁻¹)) := continuous_inv.tendsto a /-- If a function converges to a value in a multiplicative topological group, then its inverse converges to the inverse of this value. For the version in normed fields assuming additionally that the limit is nonzero, use `tendsto.inv'`. -/ @[to_additive] lemma filter.tendsto.inv [topological_group α] {f : β → α} {x : filter β} {a : α} (hf : tendsto f x (𝓝 a)) : tendsto (λx, (f x)⁻¹) x (𝓝 a⁻¹) := tendsto.comp (continuous_iff_continuous_at.mp topological_group.continuous_inv a) hf @[to_additive] lemma continuous_at.inv [topological_group α] [topological_space β] {f : β → α} {x : β} (hf : continuous_at f x) : continuous_at (λx, (f x)⁻¹) x := hf.inv @[to_additive] lemma continuous_within_at.inv [topological_group α] [topological_space β] {f : β → α} {s : set β} {x : β} (hf : continuous_within_at f s x) : continuous_within_at (λx, (f x)⁻¹) s x := hf.inv @[to_additive topological_add_group] instance [topological_group α] [topological_space β] [group β] [topological_group β] : topological_group (α × β) := { continuous_inv := continuous_fst.inv.prod_mk continuous_snd.inv } attribute [instance] prod.topological_add_group @[to_additive] protected def homeomorph.mul_left [topological_group α] (a : α) : α ≃ₜ α := { continuous_to_fun := continuous_const.mul continuous_id, continuous_inv_fun := continuous_const.mul continuous_id, .. equiv.mul_left a } @[to_additive] lemma is_open_map_mul_left [topological_group α] (a : α) : is_open_map (λ x, a * x) := (homeomorph.mul_left a).is_open_map @[to_additive] lemma is_closed_map_mul_left [topological_group α] (a : α) : is_closed_map (λ x, a * x) := (homeomorph.mul_left a).is_closed_map @[to_additive] protected def homeomorph.mul_right {α : Type*} [topological_space α] [group α] [topological_group α] (a : α) : α ≃ₜ α := { continuous_to_fun := continuous_id.mul continuous_const, continuous_inv_fun := continuous_id.mul continuous_const, .. equiv.mul_right a } @[to_additive] lemma is_open_map_mul_right [topological_group α] (a : α) : is_open_map (λ x, x * a) := (homeomorph.mul_right a).is_open_map @[to_additive] lemma is_closed_map_mul_right [topological_group α] (a : α) : is_closed_map (λ x, x * a) := (homeomorph.mul_right a).is_closed_map @[to_additive] protected def homeomorph.inv (α : Type*) [topological_space α] [group α] [topological_group α] : α ≃ₜ α := { continuous_to_fun := continuous_inv, continuous_inv_fun := continuous_inv, .. equiv.inv α } @[to_additive exists_nhds_half] lemma exists_nhds_split [topological_group α] {s : set α} (hs : s ∈ 𝓝 (1 : α)) : ∃ V ∈ 𝓝 (1 : α), ∀ v w ∈ V, v * w ∈ s := begin have : ((λa:α×α, a.1 * a.2) ⁻¹' s) ∈ 𝓝 ((1, 1) : α × α) := tendsto_mul (by simpa using hs), rw nhds_prod_eq at this, rcases mem_prod_iff.1 this with ⟨V₁, H₁, V₂, H₂, H⟩, exact ⟨V₁ ∩ V₂, inter_mem_sets H₁ H₂, assume v w ⟨hv, _⟩ ⟨_, hw⟩, @H (v, w) ⟨hv, hw⟩⟩ end @[to_additive exists_nhds_half_neg] lemma exists_nhds_split_inv [topological_group α] {s : set α} (hs : s ∈ 𝓝 (1 : α)) : ∃ V ∈ 𝓝 (1 : α), ∀ v w ∈ V, v * w⁻¹ ∈ s := begin have : tendsto (λa:α×α, a.1 * (a.2)⁻¹) ((𝓝 (1:α)).prod (𝓝 (1:α))) (𝓝 1), { simpa using (@tendsto_fst α α (𝓝 1) (𝓝 1)).mul tendsto_snd.inv }, have : ((λa:α×α, a.1 * (a.2)⁻¹) ⁻¹' s) ∈ (𝓝 (1:α)).prod (𝓝 (1:α)) := this (by simpa using hs), rcases mem_prod_iff.1 this with ⟨V₁, H₁, V₂, H₂, H⟩, exact ⟨V₁ ∩ V₂, inter_mem_sets H₁ H₂, assume v w ⟨hv, _⟩ ⟨_, hw⟩, @H (v, w) ⟨hv, hw⟩⟩ end @[to_additive exists_nhds_quarter] lemma exists_nhds_split4 [topological_group α] {u : set α} (hu : u ∈ 𝓝 (1 : α)) : ∃ V ∈ 𝓝 (1 : α), ∀ {v w s t}, v ∈ V → w ∈ V → s ∈ V → t ∈ V → v * w * s * t ∈ u := begin rcases exists_nhds_split hu with ⟨W, W_nhd, h⟩, rcases exists_nhds_split W_nhd with ⟨V, V_nhd, h'⟩, existsi [V, V_nhd], intros v w s t v_in w_in s_in t_in, simpa [mul_assoc] using h _ _ (h' v w v_in w_in) (h' s t s_in t_in) end section variable (α) @[to_additive] lemma nhds_one_symm [topological_group α] : comap (λr:α, r⁻¹) (𝓝 (1 : α)) = 𝓝 (1 : α) := begin have lim : tendsto (λr:α, r⁻¹) (𝓝 1) (𝓝 1), { simpa using (@tendsto_id α (𝓝 1)).inv }, refine comap_eq_of_inverse _ _ lim lim, { funext x, simp }, end end @[to_additive] lemma nhds_translation_mul_inv [topological_group α] (x : α) : comap (λy:α, y * x⁻¹) (𝓝 1) = 𝓝 x := begin refine comap_eq_of_inverse (λy:α, y * x) _ _ _, { funext x; simp }, { suffices : tendsto (λy:α, y * x⁻¹) (𝓝 x) (𝓝 (x * x⁻¹)), { simpa }, exact tendsto_id.mul tendsto_const_nhds }, { suffices : tendsto (λy:α, y * x) (𝓝 1) (𝓝 (1 * x)), { simpa }, exact tendsto_id.mul tendsto_const_nhds } end @[to_additive] lemma topological_group.ext {G : Type*} [group G] {t t' : topological_space G} (tg : @topological_group G t _) (tg' : @topological_group G t' _) (h : @nhds G t 1 = @nhds G t' 1) : t = t' := eq_of_nhds_eq_nhds $ λ x, by rw [← @nhds_translation_mul_inv G t _ _ x , ← @nhds_translation_mul_inv G t' _ _ x , ← h] end topological_group section quotient_topological_group variables [topological_space α] [group α] [topological_group α] (N : set α) [normal_subgroup N] @[to_additive] instance {α : Type u} [group α] [topological_space α] (N : set α) [normal_subgroup N] : topological_space (quotient_group.quotient N) := by dunfold quotient_group.quotient; apply_instance open quotient_group @[to_additive quotient_add_group_saturate] lemma quotient_group_saturate {α : Type u} [group α] (N : set α) [normal_subgroup N] (s : set α) : (coe : α → quotient N) ⁻¹' ((coe : α → quotient N) '' s) = (⋃ x : N, (λ y, y*x.1) '' s) := begin ext x, simp only [mem_preimage, mem_image, mem_Union, quotient_group.eq], split, { exact assume ⟨a, a_in, h⟩, ⟨⟨_, h⟩, a, a_in, mul_inv_cancel_left _ _⟩ }, { exact assume ⟨⟨i, hi⟩, a, ha, eq⟩, ⟨a, ha, by simp only [eq.symm, (mul_assoc _ _ _).symm, inv_mul_cancel_left, hi]⟩ } end @[to_additive] lemma quotient_group.open_coe : is_open_map (coe : α → quotient N) := begin intros s s_op, change is_open ((coe : α → quotient N) ⁻¹' (coe '' s)), rw quotient_group_saturate N s, apply is_open_Union, rintro ⟨n, _⟩, exact is_open_map_mul_right n s s_op end @[to_additive topological_add_group_quotient] instance topological_group_quotient : topological_group (quotient N) := { continuous_mul := begin have cont : continuous ((coe : α → quotient N) ∘ (λ (p : α × α), p.fst * p.snd)) := continuous_quot_mk.comp continuous_mul, have quot : quotient_map (λ p : α × α, ((p.1:quotient N), (p.2:quotient N))), { apply is_open_map.to_quotient_map, { exact is_open_map.prod (quotient_group.open_coe N) (quotient_group.open_coe N) }, { exact (continuous_quot_mk.comp continuous_fst).prod_mk (continuous_quot_mk.comp continuous_snd) }, { rintro ⟨⟨x⟩, ⟨y⟩⟩, exact ⟨(x, y), rfl⟩ } }, exact (quotient_map.continuous_iff quot).2 cont, end, continuous_inv := begin apply continuous_quotient_lift, change continuous ((coe : α → quotient N) ∘ (λ (a : α), a⁻¹)), exact continuous_quot_mk.comp continuous_inv end } attribute [instance] topological_add_group_quotient end quotient_topological_group section topological_add_group variables [topological_space α] [add_group α] lemma continuous.sub [topological_add_group α] [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λx, f x - g x) := by simp [sub_eq_add_neg]; exact hf.add hg.neg lemma continuous_sub [topological_add_group α] : continuous (λp:α×α, p.1 - p.2) := continuous_fst.sub continuous_snd lemma continuous_on.sub [topological_add_group α] [topological_space β] {f : β → α} {g : β → α} {s : set β} (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λx, f x - g x) s := continuous_sub.comp_continuous_on (hf.prod hg) lemma filter.tendsto.sub [topological_add_group α] {f : β → α} {g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) : tendsto (λx, f x - g x) x (𝓝 (a - b)) := by simp [sub_eq_add_neg]; exact hf.add hg.neg lemma nhds_translation [topological_add_group α] (x : α) : comap (λy:α, y - x) (𝓝 0) = 𝓝 x := nhds_translation_add_neg x end topological_add_group section prio set_option default_priority 100 -- see Note [default priority] /-- additive group with a neighbourhood around 0. Only used to construct a topology and uniform space. This is currently only available for commutative groups, but it can be extended to non-commutative groups too. -/ class add_group_with_zero_nhd (α : Type u) extends add_comm_group α := (Z [] : filter α) (zero_Z : pure 0 ≤ Z) (sub_Z : tendsto (λp:α×α, p.1 - p.2) (Z.prod Z) Z) end prio namespace add_group_with_zero_nhd variables (α) [add_group_with_zero_nhd α] local notation `Z` := add_group_with_zero_nhd.Z @[priority 100] -- see Note [lower instance priority] instance : topological_space α := topological_space.mk_of_nhds $ λa, map (λx, x + a) (Z α) variables {α} lemma neg_Z : tendsto (λa:α, - a) (Z α) (Z α) := have tendsto (λa, (0:α)) (Z α) (Z α), by refine le_trans (assume h, _) zero_Z; simp [univ_mem_sets'] {contextual := tt}, have tendsto (λa:α, 0 - a) (Z α) (Z α), from sub_Z.comp (tendsto.prod_mk this tendsto_id), by simpa lemma add_Z : tendsto (λp:α×α, p.1 + p.2) ((Z α).prod (Z α)) (Z α) := suffices tendsto (λp:α×α, p.1 - -p.2) ((Z α).prod (Z α)) (Z α), by simpa [sub_eq_add_neg], sub_Z.comp (tendsto.prod_mk tendsto_fst (neg_Z.comp tendsto_snd)) lemma exists_Z_half {s : set α} (hs : s ∈ Z α) : ∃ V ∈ Z α, ∀ v w ∈ V, v + w ∈ s := begin have : ((λa:α×α, a.1 + a.2) ⁻¹' s) ∈ (Z α).prod (Z α) := add_Z (by simpa using hs), rcases mem_prod_iff.1 this with ⟨V₁, H₁, V₂, H₂, H⟩, exact ⟨V₁ ∩ V₂, inter_mem_sets H₁ H₂, assume v w ⟨hv, _⟩ ⟨_, hw⟩, @H (v, w) ⟨hv, hw⟩⟩ end lemma nhds_eq (a : α) : 𝓝 a = map (λx, x + a) (Z α) := topological_space.nhds_mk_of_nhds _ _ (assume a, calc pure a = map (λx, x + a) (pure 0) : by simp ... ≤ _ : map_mono zero_Z) (assume b s hs, let ⟨t, ht, eqt⟩ := exists_Z_half hs in have t0 : (0:α) ∈ t, by simpa using zero_Z ht, begin refine ⟨(λx:α, x + b) '' t, image_mem_map ht, _, _⟩, { refine set.image_subset_iff.2 (assume b hbt, _), simpa using eqt 0 b t0 hbt }, { rintros _ ⟨c, hb, rfl⟩, refine (Z α).sets_of_superset ht (assume x hxt, _), simpa [add_assoc] using eqt _ _ hxt hb } end) lemma nhds_zero_eq_Z : 𝓝 0 = Z α := by simp [nhds_eq]; exact filter.map_id @[priority 100] -- see Note [lower instance priority] instance : topological_add_monoid α := ⟨ continuous_iff_continuous_at.2 $ assume ⟨a, b⟩, begin rw [continuous_at, nhds_prod_eq, nhds_eq, nhds_eq, nhds_eq, filter.prod_map_map_eq, tendsto_map'_iff], suffices : tendsto ((λx:α, (a + b) + x) ∘ (λp:α×α,p.1 + p.2)) (filter.prod (Z α) (Z α)) (map (λx:α, (a + b) + x) (Z α)), { simpa [(∘), add_comm, add_left_comm] }, exact tendsto_map.comp add_Z end⟩ @[priority 100] -- see Note [lower instance priority] instance : topological_add_group α := ⟨continuous_iff_continuous_at.2 $ assume a, begin rw [continuous_at, nhds_eq, nhds_eq, tendsto_map'_iff], suffices : tendsto ((λx:α, x - a) ∘ (λx:α, -x)) (Z α) (map (λx:α, x - a) (Z α)), { simpa [(∘), add_comm, sub_eq_add_neg] using this }, exact tendsto_map.comp neg_Z end⟩ end add_group_with_zero_nhd section filter_mul section variables [topological_space α] [group α] [topological_group α] @[to_additive] lemma is_open_mul_left {s t : set α} : is_open t → is_open (s * t) := λ ht, begin have : ∀a, is_open ((λ (x : α), a * x) '' t), assume a, apply is_open_map_mul_left, exact ht, rw ← Union_mul_left_image, exact is_open_Union (λa, is_open_Union $ λha, this _), end @[to_additive] lemma is_open_mul_right {s t : set α} : is_open s → is_open (s * t) := λ hs, begin have : ∀a, is_open ((λ (x : α), x * a) '' s), assume a, apply is_open_map_mul_right, exact hs, rw ← Union_mul_right_image, exact is_open_Union (λa, is_open_Union $ λha, this _), end variables (α) lemma topological_group.t1_space (h : @is_closed α _ {1}) : t1_space α := ⟨assume x, by { convert is_closed_map_mul_right x _ h, simp }⟩ lemma topological_group.regular_space [t1_space α] : regular_space α := ⟨assume s a hs ha, let f := λ p : α × α, p.1 * (p.2)⁻¹ in have hf : continuous f := continuous_mul.comp (continuous_fst.prod_mk (continuous_inv.comp continuous_snd)), -- a ∈ -s implies f (a, 1) ∈ -s, and so (a, 1) ∈ f⁻¹' (-s); -- and so can find t₁ t₂ open such that a ∈ t₁ × t₂ ⊆ f⁻¹' (-s) let ⟨t₁, t₂, ht₁, ht₂, a_mem_t₁, one_mem_t₂, t_subset⟩ := is_open_prod_iff.1 (hf _ (is_open_compl_iff.2 hs)) a (1:α) (by simpa [f]) in begin use s * t₂, use is_open_mul_left ht₂, use λ x hx, ⟨x, 1, hx, one_mem_t₂, mul_one _⟩, apply inf_principal_eq_bot, rw mem_nhds_sets_iff, refine ⟨t₁, _, ht₁, a_mem_t₁⟩, rintros x hx ⟨y, z, hy, hz, yz⟩, have : x * z⁻¹ ∈ sᶜ := (prod_subset_iff.1 t_subset) x hx z hz, have : x * z⁻¹ ∈ s, rw ← yz, simpa, contradiction end⟩ local attribute [instance] topological_group.regular_space lemma topological_group.t2_space [t1_space α] : t2_space α := regular_space.t2_space α end section /-! Some results about an open set containing the product of two sets in a topological group. -/ variables [topological_space α] [group α] [topological_group α] /-- Given a open neighborhood `U` of `1` there is a open neighborhood `V` of `1` such that `VV ⊆ U`. -/ @[to_additive "Given a open neighborhood `U` of `0` there is a open neighborhood `V` of `0` such that `V + V ⊆ U`."] lemma one_open_separated_mul {U : set α} (h1U : is_open U) (h2U : (1 : α) ∈ U) : ∃ V : set α, is_open V ∧ (1 : α) ∈ V ∧ V * V ⊆ U := begin rcases exists_nhds_square (continuous_mul U h1U) (by simp only [mem_preimage, one_mul, h2U] : ((1 : α), (1 : α)) ∈ (λ p : α × α, p.1 * p.2) ⁻¹' U) with ⟨V, h1V, h2V, h3V⟩, refine ⟨V, h1V, h2V, _⟩, rwa [← image_subset_iff, image_mul_prod] at h3V end /-- Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `1` such that `KV ⊆ U`. -/ @[to_additive "Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `0` such that `K + V ⊆ U`."] lemma compact_open_separated_mul {K U : set α} (hK : is_compact K) (hU : is_open U) (hKU : K ⊆ U) : ∃ V : set α, is_open V ∧ (1 : α) ∈ V ∧ K * V ⊆ U := begin let W : α → set α := λ x, (λ y, x * y) ⁻¹' U, have h1W : ∀ x, is_open (W x) := λ x, continuous_mul_left x U hU, have h2W : ∀ x ∈ K, (1 : α) ∈ W x := λ x hx, by simp only [mem_preimage, mul_one, hKU hx], choose V hV using λ x : K, one_open_separated_mul (h1W x) (h2W x.1 x.2), let X : K → set α := λ x, (λ y, (x : α)⁻¹ * y) ⁻¹' (V x), cases hK.elim_finite_subcover X (λ x, continuous_mul_left x⁻¹ (V x) (hV x).1) _ with t ht, swap, { intros x hx, rw [mem_Union], use ⟨x, hx⟩, rw [mem_preimage], convert (hV _).2.1, simp only [mul_left_inv, subtype.coe_mk] }, refine ⟨⋂ x ∈ t, V x, is_open_bInter (finite_mem_finset _) (λ x hx, (hV x).1), _, _⟩, { simp only [mem_Inter], intros x hx, exact (hV x).2.1 }, rintro _ ⟨x, y, hx, hy, rfl⟩, simp only [mem_Inter] at hy, have := ht hx, simp only [mem_Union, mem_preimage] at this, rcases this with ⟨z, h1z, h2z⟩, have : (z : α)⁻¹ * x * y ∈ W z := (hV z).2.2 (mul_mem_mul h2z (hy z h1z)), rw [mem_preimage] at this, convert this using 1, simp only [mul_assoc, mul_inv_cancel_left] end /-- A compact set is covered by finitely many left multiplicative translates of a set with non-empty interior. -/ @[to_additive "A compact set is covered by finitely many left additive translates of a set with non-empty interior."] lemma compact_covered_by_mul_left_translates {K V : set α} (hK : is_compact K) (hV : (interior V).nonempty) : ∃ t : finset α, K ⊆ ⋃ g ∈ t, (λ h, g * h) ⁻¹' V := begin cases hV with g₀ hg₀, rcases is_compact.elim_finite_subcover hK (λ x : α, interior $ (λ h, x * h) ⁻¹' V) _ _ with ⟨t, ht⟩, { refine ⟨t, subset.trans ht _⟩, apply Union_subset_Union, intro g, apply Union_subset_Union, intro hg, apply interior_subset }, { intro g, apply is_open_interior }, { intros g hg, rw [mem_Union], use g₀ * g⁻¹, apply preimage_interior_subset_interior_preimage, exact continuous_const.mul continuous_id, rwa [mem_preimage, inv_mul_cancel_right] } end end section variables [topological_space α] [comm_group α] [topological_group α] @[to_additive] lemma nhds_mul (x y : α) : 𝓝 (x * y) = 𝓝 x * 𝓝 y := filter_eq $ set.ext $ assume s, begin rw [← nhds_translation_mul_inv x, ← nhds_translation_mul_inv y, ← nhds_translation_mul_inv (x*y)], split, { rintros ⟨t, ht, ts⟩, rcases exists_nhds_split ht with ⟨V, V_mem, h⟩, refine ⟨(λa, a * x⁻¹) ⁻¹' V, (λa, a * y⁻¹) ⁻¹' V, ⟨V, V_mem, subset.refl _⟩, ⟨V, V_mem, subset.refl _⟩, _⟩, rintros a ⟨v, w, v_mem, w_mem, rfl⟩, apply ts, simpa [mul_comm, mul_assoc, mul_left_comm] using h (v * x⁻¹) (w * y⁻¹) v_mem w_mem }, { rintros ⟨a, c, ⟨b, hb, ba⟩, ⟨d, hd, dc⟩, ac⟩, refine ⟨b ∩ d, inter_mem_sets hb hd, assume v, _⟩, simp only [preimage_subset_iff, mul_inv_rev, mem_preimage] at *, rintros ⟨vb, vd⟩, refine ac ⟨v * y⁻¹, y, _, _, _⟩, { rw ← mul_assoc _ _ _ at vb, exact ba _ vb }, { apply dc y, rw mul_right_inv, exact mem_of_nhds hd }, { simp only [inv_mul_cancel_right] } } end @[to_additive] lemma nhds_is_mul_hom : is_mul_hom (λx:α, 𝓝 x) := ⟨λ_ _, nhds_mul _ _⟩ end end filter_mul
357866f06b9139f8d81d22d8b36a50eeef3aec2a
0cdd90d2d613d48e567989a6bb0c5808ed4ad727
/src/happy_number.lean
647722e04bb6102b0a77d0a7b425819bda34e5fd
[ "MIT" ]
permissive
SnobbyDragon/happynumbers
e749077a6aed5b6158f168293466130c729ddbcc
e71d1c05d461a27db7dfbaa34561ad7c1e141836
refs/heads/master
1,669,489,237,904
1,596,426,948,000
1,596,426,948,000
275,297,492
0
0
null
null
null
null
UTF-8
Lean
false
false
14,067
lean
import tactic import data.nat.digits import logic.basic import data.list namespace happynumber section definitions -- Perfect digital invariant (a.k.a. happy function) -- the sum of the pth (p > 0) power of digits of a natural number n in a base b > 1 def happyfunction (p : ℕ) (b : ℕ) : ℕ → ℕ | n := ((digits b n).map (λ d, d^p)).sum -- gets the ith iteration of the happy function w/base b, power p on natural number n def happyfunction' (p : ℕ) (b : ℕ) (n : ℕ) : ℕ → ℕ | 0 := n | (i+1) := happyfunction p b (happyfunction' (i)) -- Happy number -- a number n is b-happy iff the happy function on n w/base b, power 2 eventually equals 1 def happy (b : ℕ) (n : ℕ) : Prop := ∃ (j : ℕ), happyfunction' 2 b n j = 1 -- height of a b-happy number n is the number of iterations of the happy function to reach 1 def happyheight (b n i : ℕ) (H : happyfunction' 2 b n i ≠ 1 ∧ happyfunction' 2 b n i.succ = 1) : ℕ := i.succ --Sad number -- a number n is b-sad iff all iterations of the happy function on n w/base b, power 2 are not equal to 1 def sad (b : ℕ) (n : ℕ): Prop := ∀ (j : ℕ), happyfunction' 2 b n j ≠ 1 end definitions -- one iteration of happy function @[simp] lemma happyfunction'_one_eq_happyfunction (p b n : ℕ) : happyfunction' p b n 1 = happyfunction p b n := begin unfold happyfunction', end -- commutative composition but not really bc it's technically the same function lemma happyfunction'_comm (p b n i : ℕ) : happyfunction p b (happyfunction' p b n i) = happyfunction' p b (happyfunction p b n) i := begin rw <- happyfunction', induction i with k nk, unfold happyfunction', unfold happyfunction' at *, rw nk, end -- happy = not sad lemma happy_not_sad (b n : ℕ) : happy b n ↔ ¬(sad b n) := begin split, intros H S, cases H with k Hk, specialize S k, contradiction, intros S, unfold sad at S, --rw <- not_exists at S, simp at S, cases S with k Sk, use k, exact Sk, end -- every natural number n is either happy or sad (but not both) lemma happy_or_sad (b: ℕ) : ∀ (n : ℕ), happy b n ∨ sad b n := begin intros n, rw happy_not_sad, finish, end -- happy function on 1 equals 1 for any p > 0 and b > 1 lemma happyfunction_one (p : ℕ) (b : ℕ) : (b > 1) → happyfunction p b 1 = 1 := begin intros h, unfold happyfunction, have H : digits b 1 = [1], apply digits_of_lt, linarith, exact h, rw H, simp, end -- junk input. b = 0 lemma junk_zero_one (p : ℕ) : happyfunction p 0 1 = 1 := begin unfold happyfunction, have H : digits 0 1 = [1], refl, rw H, simp, end -- junk input. b = 1 lemma junk_one_one (p : ℕ) : happyfunction p 1 1 = 1 := begin unfold happyfunction, have H : digits 1 1 = [1], refl, rw H, simp, end -- just to not deal with b > 1 hypothesis @[simp] lemma happyfunction_one' (p : ℕ) (b : ℕ) : happyfunction p b 1 = 1 := begin have H : (b = 0) ∨ (1 = b) ∨ (1 < b), cases b, left, refl, right, have H := nat.succ_pos b, have H' : 1 ≤ b.succ, linarith, rw le_iff_eq_or_lt at H', exact H', cases H, rw H, exact junk_zero_one p, cases H, rw <- H, exact junk_one_one p, exact happyfunction_one p b H, end -- any iteration of the happy function on 1 equals 1 @[simp] lemma happyfunction'_one (p : ℕ) (b : ℕ) : ∀ (i : ℕ), happyfunction' p b 1 i = 1 := begin intros i, induction i with k ik, unfold happyfunction', unfold happyfunction', rw ik, exact happyfunction_one' p b, end -- for base 10, after reaching 1, will stay 1 lemma ten_happily_ever_after (n : ℕ) (j: ℕ) : happyfunction' 2 10 n j = 1 → ∀ (i : ℕ), (j ≤ i) → happyfunction' 2 10 n i = 1 := begin intros H i hi, rw le_iff_exists_add at hi, cases hi with c hc, induction c with k ck generalizing i, { simp at hc, rw hc, exact H, }, { rw nat.add_succ at hc, rw hc, unfold happyfunction', specialize ck (j+k), simp at ck, rw ck, --unfold happyfunction, simp, }, end -- for base b, after reaching 1, will stay 1 lemma happily_ever_after (b : ℕ) (n : ℕ) (j: ℕ) : happyfunction' 2 b n j = 1 → ∀ i, (j ≤ i) → happyfunction' 2 b n i = 1 := begin intros H i hi, rw le_iff_exists_add at hi, cases hi with c hc, induction c with k ck generalizing i, { simp at hc, rw hc, exact H, }, { rw nat.add_succ at hc, rw hc, unfold happyfunction', specialize ck (j+k), simp at ck, rw ck, --exact happyfunction_one' 2 b, simp, }, end -- 1 is 10-happy lemma ten_happy_one : happy 10 1 := begin use 0, unfold happyfunction', end -- 7 is 10-happy lemma ten_happy_seven : happy 10 7 := begin use 5, unfold happyfunction', unfold happyfunction, norm_num, end -- happy function on 0 equals 0 @[simp] lemma happyfunction_zero (p : ℕ) (b : ℕ) : happyfunction p b 0 = 0 := begin unfold happyfunction, simp, end -- any iteration of the happy function on 0 equals 0 @[simp] lemma happyfunction'_zero (p : ℕ) (b : ℕ) : ∀ (i : ℕ), happyfunction' p b 0 i = 0 := begin intros i, induction i with k ik, unfold happyfunction', unfold happyfunction', rw ik, --unfold happyfunction, simp, end -- 0 is 10-sad lemma ten_sad_zero : sad 10 0 := begin intros i hi, --rw happyfunction'_zero 2 10 i at hi, --linarith, simp at hi, exact hi, end -- for b=10, multiplying by 10 (adding a zero) won't make a difference lemma ten_happyfunction_eq_times_ten (n : ℕ) : happyfunction 2 10 n = happyfunction 2 10 (10*n) := begin cases n, norm_num, have H : digits 10 (10*n.succ) = 0 :: digits 10 n.succ, have h₁ : 10*n.succ = 0 + 10*n.succ, simp, rw h₁, apply digits_add, linarith, linarith, right, exact nat.succ_pos n, unfold happyfunction, rw H, norm_num, end lemma ten_happyfunction'_eq_times_ten (n : ℕ) : ∀ (i : ℕ), (0 < i) → happyfunction' 2 10 n i = happyfunction' 2 10 (10*n) i := begin intros i i_pos, cases i, exfalso, linarith, unfold happyfunction', rw happyfunction'_comm, rw happyfunction'_comm 2 10 (10*n), rw ten_happyfunction_eq_times_ten, end -- if n is 10-happy, then n*10 is 10-happy lemma ten_happy_times_ten (n : ℕ) : happy 10 n → happy 10 (10*n) := begin intros H, cases H with j Hj, cases j, use 1, rw happyfunction'_one_eq_happyfunction, unfold happyfunction' at Hj, rw Hj, rw <- ten_happyfunction_eq_times_ten, simp, use j.succ, rw <- ten_happyfunction'_eq_times_ten n j.succ (nat.succ_pos'), exact Hj, end -- 10^m is always a 10-happy number lemma ten_happy_pow_ten (m : ℕ) : happy 10 (10^m) := begin use 1, unfold happyfunction', induction m with k mk, rw nat.pow_zero 10, exact happyfunction_one' 2 10, rw nat.pow_succ, rw mul_comm, rw <- ten_happyfunction_eq_times_ten (10^k), exact mk, end -- for b=10, permuting the digits won't make a difference lemma ten_happyfunction_eq_permute_digits (p : ℕ) (n n' : ℕ) (P : (digits 10 n) ~ (digits 10 n')) : happyfunction p 10 n = happyfunction p 10 n' := begin unfold happyfunction, have h := list.perm.map (λ (d : ℕ), d^p) P, have h' := list.perm.sum_eq h, exact h', end lemma ten_happyfunction'_eq_permute_digits (p i : ℕ) (n n' : ℕ) (P : (digits 10 n) ~ (digits 10 n')) : happyfunction' p 10 n i = happyfunction' p 10 n' i := begin sorry end -- permuting a 10-happy number's digits will result in another happy number lemma ten_happy_permute_ten_happy (n : ℕ) (H : happy 10 n) (n' : ℕ) (P : (digits 10 n) ~ (digits 10 n')) : happy 10 n' := begin sorry end -- references "A Set of Eight Numbers" - Arthur Porges -- https://oeis.org/A003621/a003621.pdf section mainTheorem -- the base 10 digits of a number are all at most 9 -- Thanks Kevin Buzzard for digits_lt_base :) lemma ten_digits_le_9 (n : ℕ) : ∀ (d ∈ (digits 10 n)), d ≤ 9 := begin intros d H, have H' := digits_lt_base (by linarith) H, linarith, end lemma sum_list_le_len_mul_ge (l : list ℕ) (m : ℕ) : (∀ n ∈ l, n ≤ m) → l.sum ≤ m*l.length := begin intros h, induction l, refl, rw [list.sum_cons, list.length_cons, mul_add, mul_one, add_comm], apply add_le_add, apply l_ih, intros n in_t_tl, specialize h n, apply h, right, exact in_t_tl, exact h l_hd (list.mem_cons_self l_hd l_tl), end lemma ne_zero_iff_digits_ne_nil (b n : ℕ) : n ≠ 0 ↔ digits b n ≠ list.nil := begin split, { intros hn hd, cases n, { contradiction }, { have h := of_digits_digits b n.succ, rw hd at h, contradiction, }, }, { contrapose!, intros h, rw h, exact digits_zero b, }, end lemma ne_zero_iff_digits_len_ne_zero (b n : ℕ) : n ≠ 0 ↔ (digits b n).length ≠ 0 := begin rw [ne_zero_iff_digits_ne_nil b n, not_iff_not], symmetry', exact list.length_eq_zero, end lemma digits_ge_base_pow_len (b m : ℕ) : m ≠ 0 → m ≥ (b + 2) ^ ((digits (b + 2) m).length - 1) := begin apply nat.strong_induction_on m, clear m, intros n IH npos, unfold digits at IH ⊢, cases n, { contradiction, }, { rw [digits_aux_def (b+2) (by linarith) (n.succ), list.length_cons], specialize IH ((n.succ)/(b+2)) (nat.div_lt_self' n b), cases nat.lt_or_ge n.succ (b+2), { rw [nat.div_eq_of_lt h, digits_aux_zero, list.length], exact nat.succ_pos n }, { have geb : (n.succ / (b + 2)) ≥ 1 := nat.div_pos h (by linarith), specialize IH (by linarith [geb]), rw nat.succ_sub_one, have IH' := nat.mul_le_mul_left (b+2) IH, rw [nat.mul_comm, <- nat.pow_succ, nat.succ_eq_add_one] at IH', rw nat.add_comm ((digits_aux (b + 2) _ (n.succ / (b + 2))).length - 1) at IH', rw <- nat.add_sub_assoc at IH', { rw nat.add_sub_cancel_left 1 (digits_aux (b + 2) _ (n.succ / (b + 2))).length at IH', have IH'' := nat.div_mul_le_self n.succ (b+2), rw mul_comm at IH', exact le_trans IH' IH'' }, { change 0 < (digits_aux (b + 2) _ (n.succ / (b + 2))).length, rw nat.pos_iff_ne_zero, rw [<- digits, <- ne_zero_iff_digits_len_ne_zero], linarith [geb] } }, rwa nat.pos_iff_ne_zero } end lemma ten_digits_ge_base_pow_len (n : ℕ) : n ≠ 0 → n ≥ 10 ^ ((digits 10 n).length - 1) := begin exact digits_ge_base_pow_len 8 n, end lemma digits_one_less (b m : ℕ) : m > 0 → (digits (b+2) m).length = (digits (b+2) (m/(b+2))).length + 1 := begin intros hm, unfold digits, conv_lhs { rw digits_aux_def (b+2) (by linarith) m hm }, rw list.length_cons, end -- happyfunction n is less than 81*(number of digits in n) lemma ten_happyfunction_le (n : ℕ) : happyfunction 2 10 n ≤ 81*(digits 10 n).length := begin unfold happyfunction, have hdsq : ∀ dsq ∈ (list.map (λ (d : ℕ), d ^ 2) (digits 10 n)), dsq ≤ 81, intros dsq dsqin, rw list.mem_map at dsqin, cases dsqin with d' hd', cases hd' with dh sqh, have dle := ten_digits_le_9 n d' dh, rw <- sqh, exact nat.pow_le_pow_of_le_left dle 2, have hle := sum_list_le_len_mul_ge (list.map (λ (d : ℕ), d ^ 2) (digits 10 n)) 81 hdsq, rw list.length_map (λ (d : ℕ), d ^ 2) (digits 10 n) at hle, exact hle, end lemma helper_lt (a : ℕ) : 81*(a + 4) < 10^(a + 3) := begin induction a with k ak, { norm_num }, { repeat { rw nat.succ_add }, rw nat.mul_succ, rw nat.pow_succ, linarith, }, end -- happy function on a 4 or more digit number will result in a smaller number lemma ge_four_digits_dec (n : ℕ) : 4 ≤ (digits 10 n).length → happyfunction 2 10 n < n := begin intros hdig, have npos : n ≠ 0, rw ne_zero_iff_digits_len_ne_zero 10, linarith, have hge := digits_ge_base_pow_len 8 n npos, norm_num at hge, have hle := ten_happyfunction_le n, set R := (digits 10 n).length with ←h, suffices : 81*R < 10^(R-1), { linarith }, rw le_iff_exists_add at hdig, cases hdig with c hc, rw hc, norm_num, rw [add_comm 4 c, add_comm 3 c], exact helper_lt c, end -- happy function on a 4 or more digit number will never increase the number of digits lemma ge_four_digits_le_digits_len (n : ℕ) : 4 ≤ (digits 10 n).length → (digits 10 (happyfunction 2 10 n)).length ≤ (digits 10 n).length := begin intros h, have dec := ge_four_digits_dec n h, exact le_digits_len_le 10 (happyfunction 2 10 n) n (by linarith [dec]), end -- eventually, happyfunction on n is less than 4 digits long lemma eventually_lt_four_digits (n : ℕ) : ∃ (a : ℕ), (digits 10 (happyfunction' 2 10 n a)).length < 4 := begin sorry end -- eventually, happyfunction on n is less than or equal to 162 lemma eventually_le (n : ℕ) : ∃ (a : ℕ), happyfunction' 2 10 n a ≤ 162 := begin sorry end def K : set ℕ := {4, 16, 37, 58, 89, 145, 42, 20} lemma K_closed_under_happyfunction (n : ℕ) (H : n ∈ K) : happyfunction 2 10 n ∈ K := begin iterate 7 { cases H, rw H, unfold happyfunction, norm_num, right, simp }, have H' := set.eq_of_mem_singleton H, rw H', unfold happyfunction, norm_num, left, refl, end lemma K_closed_under_happyfunction' (n : ℕ) (H : n ∈ K) : ∀ (i : ℕ), happyfunction' 2 10 n i ∈ K := begin intros i, induction i with k ik, unfold happyfunction', exact H, unfold happyfunction', exact K_closed_under_happyfunction (happyfunction' 2 10 n k) ik, end -- 10-Sad numbers > 0 always end in the 8 number cycle {4, 16, 37, 58, 89, 145, 42, 20} lemma ten_sad_eightnumcycle (n : ℕ) (H : sad 10 n) : ∃ (j : ℕ), ∀ (i : ℕ), (j ≤ i) → (happyfunction' 2 10 n i) ∈ K := begin sorry end theorem ten_happyfunction_convergence (A : ℕ) : ∃ (n > 0), ∀ (r ≥ n), (happyfunction' 2 10 A r = 1 ∨ happyfunction' 2 10 A r ∈ K) := begin have H := happy_or_sad 10 A, cases H, cases H with n Hn, use n, split, sorry end end mainTheorem #print happyfunction #eval happyfunction 2 10 999 #print happyfunction' #eval happyfunction' 2 10 7 5 #print happy #reduce happy 10 7 #print sad #reduce sad 10 4 end happynumber
46f0607f82b46da99924d612bdbab56b3ed9c7ad
4bddde0d06fbd53be6f23d7f5899998e8f63410b
/src/tactic/iconfig/struct.lean
5cd71aa78070bf3efc7d3b81b103546198c87dcd
[]
no_license
khoek/libiconfig
4816290a5862af14b07683b3d2663e8e62832ef4
6f55c50bc5d852d26ee5ee4c5b52b2cda2a852e5
refs/heads/master
1,586,109,683,212
1,559,567,916,000
1,559,567,916,000
157,085,466
0
1
null
1,559,567,917,000
1,541,945,134,000
Lean
UTF-8
Lean
false
false
1,135
lean
import .types namespace iconfig open tactic cfgopt meta def is_valid_config (n : name) : tactic bool := (to_expr $ pexpr.mk_structure_instance ⟨some n, [], [], []⟩) >> return tt <|> return ff meta def assert_valid_config (n : name) : tactic unit := do r ← is_valid_config n, if r then skip else fail format!"config '{n}' cannot be instantiated as '{{}'" meta def resolve_field (e : environment) (struct : name) (field : name) : tactic expr := do e ← mk_const (struct.append field) >>= infer_type, match e with | expr.pi _ _ _ t := pure t | _ := fail "read invalid expr" end meta def get_struct_types (e : environment) (struct : name) : tactic (list (name × type)) := do assert_valid_config struct, e.structure_fields struct >>= list.mmap (λ s, do r ← type.from_expr <$> resolve_field e struct s, r ← r, return (s, r)) meta def mk_config (struct : name) (α : Type) [reflected α] (fields : list (name × value)) : tactic α := (to_expr $ pexpr.mk_structure_instance ⟨some struct, fields.map prod.fst, fields.map (λ s, pexpr.of_expr s.2.to_expr), []⟩) >>= eval_expr α end iconfig
7a03d8dc443822f4968c71218d774b9caab0587f
7c92a46ce39266c13607ecdef7f228688f237182
/src/Spa/space.lean
80b05f4a19439e7021bef73dbced87189ec6238a
[ "Apache-2.0" ]
permissive
asym57/lean-perfectoid-spaces
3217d01f6ddc0d13e9fb68651749469750420767
359187b429f254a946218af4411d45f08705c83e
refs/heads/master
1,609,457,937,251
1,577,542,616,000
1,577,542,675,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,662
lean
import continuous_valuations import Huber_pair /-! # The adic spectrum as a topological space In this file we define a structure (`rational_open_data`) that will parameterise a basis for the topology on the adic spectrum of a Huber pair. -/ open_locale classical local attribute [instance] set.pointwise_mul_comm_semiring local attribute [instance] set.pointwise_mul_action local postfix `⁺` : 66 := λ A : Huber_pair, A.plus variables {Γ₀ : Type*} [linear_ordered_comm_group_with_zero Γ₀] -- We reserve the name `Spa` (with upper case `S`) for the bundled adic spectrum (`adic_space.lean`) /-- The space underlying the adic spectrum of a Huber pair (A,A⁺) consists of all the equivalence classes of valuations that are continuous and whose value on the ring A⁺ is ≤ 1. [Wedhorn, Def 7.23]. -/ definition spa (A : Huber_pair) : Type := {v : Spv A // v.is_continuous ∧ ∀ r : A⁺, v (algebra_map A r) ≤ 1} /--The equivalence class of a valuation is contained in spa if and only if the valuation is continuous and its values on the ring A⁺ are ≤ 1, since these properties are constant on equivalence classes.-/ lemma mk_mem_spa {A : Huber_pair} {v : valuation A Γ₀} : Spv.mk v ∈ {v : Spv A | v.is_continuous ∧ ∀ r : A⁺, v (algebra_map A r) ≤ 1} ↔ v.is_continuous ∧ ∀ r : A⁺, v (algebra_map A r) ≤ 1 := begin apply and_congr, { exact (Spv.out_mk v).is_continuous_iff, }, { apply forall_congr, intro r, simpa using (Spv.out_mk v) (algebra_map A r) 1, } end namespace spa open set algebra variables {A : Huber_pair} /-- The coercion from the adic spectrum of a Huber pair to the ambient valuation spectrum.-/ instance : has_coe (spa A) (Spv A) := ⟨subtype.val⟩ @[ext] lemma ext (v₁ v₂ : spa A) (h : (Spv.out ↑v₁).is_equiv (Spv.out (↑v₂ : Spv A))) : v₁ = v₂ := subtype.val_injective $ Spv.ext _ _ h lemma ext_iff {v₁ v₂ : spa A} : v₁ = v₂ ↔ ((Spv.out ↑v₁).is_equiv (Spv.out (↑v₂ : Spv A))) := by rw [subtype.coe_ext, Spv.ext_iff] /-- The value monoid of a random representative valuation of a point in the adic spectrum. -/ abbreviation out_Γ₀ (v : spa A) := Spv.out_Γ₀ (v : Spv A) /-- A valuation in the adic spectrum is continuous. -/ lemma is_continuous (v : spa A) : Spv.is_continuous (v : Spv A) := v.property.left /-- The valuation of an integral element is at most 1. -/ lemma map_plus (v : spa A) (a : (A⁺)) : v (algebra_map A a) ≤ 1 := v.property.right a /-- The valuation of a unit of the ring of integral elements is 1. -/ @[simp] lemma map_unit (v : spa A) (u : units (A⁺)) : v ((algebra_map A : (A⁺) → A) u) = 1 := begin have h₁ := map_plus v u, have h₂ := map_plus v (u⁻¹ : _), have := actual_ordered_comm_monoid.mul_eq_one_iff_of_le_one' h₁ h₂, apply (this.mp _).left, erw ← valuation.map_mul, rw ← is_ring_hom.map_mul (algebra_map A : (A⁺) → A), simp only [units.mul_inv, algebra.map_one, valuation.map_one] end -- We are now going to setup the topology on `spa A`. -- A basis of the topology is indexed by the following data: /--A rational open subset of `spa A` is indexed by: * an element s of A, and * a finite set T ⊆ A that generates an open ideal in A. In the literature, these sets are commonly denoted by D(T,s).-/ structure rational_open_data (A : Huber_pair) := (s : A) (T : set A) [Tfin : fintype T] (Hopen : is_open ((ideal.span T) : set A)) namespace rational_open_data variables (r : rational_open_data A) attribute [instance] Tfin @[ext] lemma 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 /--The subset of the adic spectrum associated with the data for a rational open subset. In the literature, these sets are commonly denoted by D(T,s).-/ def open_set (r : rational_open_data A) : set (spa A) := {v : spa A | (∀ t ∈ r.T, (v t ≤ v r.s)) ∧ (v r.s ≠ 0)} variable (A) /--The rational open subset covering the entire adic spectrum.-/ def univ : rational_open_data A := { s := 1, T := {1}, Hopen := by { rw ideal.span_singleton_one, exact is_open_univ } } variable {A} @[simp] lemma univ_s : (univ A).s = 1 := rfl @[simp] lemma univ_T : (univ A).T = {1} := rfl @[simp] lemma univ_open_set : (univ A).open_set = set.univ := begin rw eq_univ_iff_forall, intros v, split, { intros t ht, erw mem_singleton_iff at ht, rw [ht, univ_s], }, { erw [univ_s, Spv.map_one], exact one_ne_zero } end /--The rational open subset D(T,s) is the same as D(T ∪ {s}, s).-/ noncomputable def insert_s (r : rational_open_data A) : rational_open_data A := { s := r.s, T := insert r.s r.T, Hopen := submodule.is_open_of_open_submodule ⟨ideal.span (r.T), r.Hopen, ideal.span_mono $ set.subset_insert _ _⟩ } @[simp] lemma insert_s_s (r : rational_open_data A) : (insert_s r).s = r.s := rfl @[simp] lemma insert_s_T (r : rational_open_data A) : (insert_s r).T = insert r.s r.T := rfl @[simp] lemma insert_s_open_set (r : rational_open_data A) : (insert_s r).open_set = r.open_set := begin ext v, split; rintros ⟨h₁, h₂⟩; split; try { exact h₂ }; intros t ht, { apply h₁ t, exact mem_insert_of_mem _ ht }, { cases ht, { rw [ht, insert_s_s], }, { exact h₁ t ht } }, end lemma mem_insert_s (r : rational_open_data A) : r.s ∈ (insert_s r).T := by {left, refl} /-- Auxilliary definition for the intersection of two rational open sets.-/ 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 := set.pointwise_mul_fintype _ _, Hopen := begin rcases Huber_ring.exists_pod_subset _ (mem_nhds_sets r1.Hopen $ ideal.zero_mem $ ideal.span r1.T) with ⟨A₀, _, _, _, ⟨_, emb, I, fg, top⟩, hI⟩, dsimp only at hI, resetI, rw is_ideal_adic_iff at top, cases top.2 (algebra_map A ⁻¹' ↑(ideal.span r2.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 _ (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, { haveI : is_ring_hom (algebra.to_fun A : A₀ → A) := algebra.is_ring_hom, apply is_ring_hom.map_zero }, exact (mem_nhds_sets r2.Hopen $ ideal.zero_mem $ ideal.span r2.T) } end } /--The intersection of two rational open sets is a rational open set.-/ 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) @[simp] lemma inter_s (r1 r2 : rational_open_data A) : (r1.inter r2).s = r1.s * r2.s := rfl @[simp] lemma inter_T (r1 r2 : rational_open_data A) : (r1.inter r2).T = (insert r1.s r1.T) * (insert r2.s r2.T) := rfl lemma inter_open_set (r1 r2 : rational_open_data A) : (inter r1 r2).open_set = r1.open_set ∩ r2.open_set := begin rw [← insert_s_open_set r1, ← insert_s_open_set r2], apply le_antisymm, { rintros v ⟨hv, hs⟩, have vmuls : v (r1.s * r2.s) = v r1.s * v r2.s := valuation.map_mul _ _ _, have hs₁ : v r1.s ≠ 0 := λ H, by simpa [-coe_fn_coe_base, vmuls, H] using hs, have hs₂ : v r2.s ≠ 0 := λ H, by simpa [-coe_fn_coe_base, vmuls, H] using hs, split; split; try { assumption }; intros t ht, { suffices H : v t * v r2.s ≤ v r1.s * v r2.s, { simpa [hs₂, mul_assoc, -coe_fn_coe_base] using linear_ordered_structure.mul_le_mul_right H (group_with_zero.mk₀ _ hs₂)⁻¹, }, { simpa using hv (t * r2.s) ⟨t, ht, r2.s, mem_insert_s r2, rfl⟩, } }, { suffices H : v r1.s * v t ≤ v r1.s * v r2.s, { simpa [hs₁, mul_assoc, -coe_fn_coe_base] using linear_ordered_structure.mul_le_mul_left H (group_with_zero.mk₀ _ hs₁)⁻¹, }, { simpa using hv (r1.s * t) ⟨r1.s, mem_insert_s r1, t, ht, rfl⟩, } } }, { rintros v ⟨⟨hv₁, hs₁⟩, ⟨hv₂, hs₂⟩⟩, split, { rintros t ⟨t₁, ht₁, t₂, ht₂, rfl⟩, convert le_trans (linear_ordered_structure.mul_le_mul_right (hv₁ t₁ ht₁) _) (linear_ordered_structure.mul_le_mul_left (hv₂ t₂ ht₂) _); apply valuation.map_mul }, { assume eq_zero, simp at eq_zero, tauto }, } end lemma inter_symm (r1 r2 : rational_open_data A) : r1.inter r2 = r2.inter r1 := ext (mul_comm _ _) (mul_comm _ _) end rational_open_data variable (A) /--The basis for the topology on the adic spectrum, consisting of rational open sets.-/ def rational_basis := {U : set (spa A) | ∃ r : rational_open_data A, U = r.open_set} /--The topology on the adic spectrum, generated by rational open sets.-/ instance : topological_space (spa A) := topological_space.generate_from (rational_basis A) variable {A} /--The rational open sets form a basis for the topology on the adic spectrum.-/ lemma rational_basis.is_basis : topological_space.is_topological_basis (rational_basis A) := begin refine ⟨_, _, rfl⟩, { rintros _ ⟨r₁, rfl⟩ _ ⟨r₂, rfl⟩ x hx, refine ⟨_, ⟨_, (rational_open_data.inter_open_set r₁ r₂).symm⟩, hx, subset.refl _⟩, }, { apply subset.antisymm (subset_univ _) (subset_sUnion_of_mem _), exact ⟨_, rational_open_data.univ_open_set.symm⟩ } end end spa
f82074435eb398a123c16ab9a0e86e2de2bfe270
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world03/level06.lean
04bf09bc2b74e5aff7a28a03766b9e11ab3374a9
[]
no_license
abdelq/natural-number-game
a1b5b8f1d52625a7addcefc97c966d3f06a48263
bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2
refs/heads/master
1,668,606,478,691
1,594,175,058,000
1,594,175,058,000
278,673,209
0
1
null
null
null
null
UTF-8
Lean
false
false
202
lean
lemma succ_mul (a b : mynat) : succ a * b = a * b + b := begin induction b with h hd, repeat {rw mul_zero}, refl, rw mul_succ, rw mul_succ, rw hd, rw add_succ, rw add_succ, rw add_right_comm, refl, end
8b7028a152175f36c52b9947b5efc79193dbe87f
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/period_after_eqns.lean
6d3985368db7637a7b0d7a9317260a5ffc6d492f
[ "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
55
lean
def f : nat → nat | 0 := 1 | (a+1) := 1 . #check 10
34c82ae8d77923d37aa391f20bf0353db2d11f46
367134ba5a65885e863bdc4507601606690974c1
/src/set_theory/game/short.lean
b579e1f5bd5218bb66091e305453cffe1dd6c79b
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
8,720
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import set_theory.game import data.fintype.basic /-! # Short games A combinatorial game is `short` [Conway, ch.9][conway2001] if it has only finitely many positions. In particular, this means there is a finite set of moves at every point. We prove that the order relations `≤` and `<`, and the equivalence relation `≈`, are decidable on short games, although unfortunately in practice `dec_trivial` doesn't seem to be able to prove anything using these instances. -/ universes u namespace pgame /-- A short game is a game with a finite set of moves at every turn. -/ inductive short : pgame.{u} → Type (u+1) | mk : Π {α β : Type u} {L : α → pgame.{u}} {R : β → pgame.{u}} (sL : ∀ i : α, short (L i)) (sR : ∀ j : β, short (R j)) [fintype α] [fintype β], short ⟨α, β, L, R⟩ instance subsingleton_short : Π (x : pgame), subsingleton (short x) | (mk xl xr xL xR) := ⟨λ a b, begin cases a, cases b, congr, { funext, apply @subsingleton.elim _ (subsingleton_short (xL x)) }, { funext, apply @subsingleton.elim _ (subsingleton_short (xR x)) }, end⟩ using_well_founded { dec_tac := pgame_wf_tac } /-- A synonym for `short.mk` that specifies the pgame in an implicit argument. -/ def short.mk' {x : pgame} [fintype x.left_moves] [fintype x.right_moves] (sL : ∀ i : x.left_moves, short (x.move_left i)) (sR : ∀ j : x.right_moves, short (x.move_right j)) : short x := by unfreezingI { cases x, dsimp at * }; exact short.mk sL sR attribute [class] short /-- Extracting the `fintype` instance for the indexing type for Left's moves in a short game. This is an unindexed typeclass, so it can't be made a global instance. -/ def fintype_left {α β : Type u} {L : α → pgame.{u}} {R : β → pgame.{u}} [S : short ⟨α, β, L, R⟩] : fintype α := by { casesI S with _ _ _ _ _ _ F _, exact F } local attribute [instance] fintype_left instance fintype_left_moves (x : pgame) [S : short x] : fintype (x.left_moves) := by { casesI x, dsimp, apply_instance } /-- Extracting the `fintype` instance for the indexing type for Right's moves in a short game. This is an unindexed typeclass, so it can't be made a global instance. -/ def fintype_right {α β : Type u} {L : α → pgame.{u}} {R : β → pgame.{u}} [S : short ⟨α, β, L, R⟩] : fintype β := by { casesI S with _ _ _ _ _ _ _ F, exact F } local attribute [instance] fintype_right instance fintype_right_moves (x : pgame) [S : short x] : fintype (x.right_moves) := by { casesI x, dsimp, apply_instance } instance move_left_short (x : pgame) [S : short x] (i : x.left_moves) : short (x.move_left i) := by { casesI S with _ _ _ _ L _ _ _, apply L } /-- Extracting the `short` instance for a move by Left. This would be a dangerous instance potentially introducing new metavariables in typeclass search, so we only make it an instance locally. -/ def move_left_short' {xl xr} (xL xR) [S : short (mk xl xr xL xR)] (i : xl) : short (xL i) := by { casesI S with _ _ _ _ L _ _ _, apply L } local attribute [instance] move_left_short' instance move_right_short (x : pgame) [S : short x] (j : x.right_moves) : short (x.move_right j) := by { casesI S with _ _ _ _ _ R _ _, apply R } /-- Extracting the `short` instance for a move by Right. This would be a dangerous instance potentially introducing new metavariables in typeclass search, so we only make it an instance locally. -/ def move_right_short' {xl xr} (xL xR) [S : short (mk xl xr xL xR)] (j : xr) : short (xR j) := by { casesI S with _ _ _ _ _ R _ _, apply R } local attribute [instance] move_right_short' instance short.of_pempty {xL} {xR} : short (mk pempty pempty xL xR) := short.mk (λ i, pempty.elim i) (λ j, pempty.elim j) instance short_0 : short 0 := short.mk (λ i, by cases i) (λ j, by cases j) instance short_1 : short 1 := short.mk (λ i, begin cases i, apply_instance, end) (λ j, by cases j) /-- Evidence that every `pgame` in a list is `short`. -/ inductive list_short : list pgame.{u} → Type (u+1) | nil : list_short [] | cons : Π (hd : pgame.{u}) [short hd] (tl : list pgame.{u}) [list_short tl], list_short (hd :: tl) attribute [class] list_short attribute [instance] list_short.nil list_short.cons instance list_short_nth_le : Π (L : list pgame.{u}) [list_short L] (i : fin (list.length L)), short (list.nth_le L i i.is_lt) | [] _ n := begin exfalso, rcases n with ⟨_, ⟨⟩⟩, end | (hd :: tl) (@list_short.cons _ S _ _) ⟨0, _⟩ := S | (hd :: tl) (@list_short.cons _ _ _ S) ⟨n+1, h⟩ := @list_short_nth_le tl S ⟨n, (add_lt_add_iff_right 1).mp h⟩ instance short_of_lists : Π (L R : list pgame) [list_short L] [list_short R], short (pgame.of_lists L R) | L R _ _ := by { resetI, apply short.mk, { intros, apply_instance }, { intros, apply pgame.list_short_nth_le /- where does the subtype.val come from? -/ } } /-- If `x` is a short game, and `y` is a relabelling of `x`, then `y` is also short. -/ def short_of_relabelling : Π {x y : pgame.{u}} (R : relabelling x y) (S : short x), short y | x y ⟨L, R, rL, rR⟩ S := begin resetI, haveI := (fintype.of_equiv _ L), haveI := (fintype.of_equiv _ R), exact short.mk' (λ i, by { rw ←(L.right_inv i), apply short_of_relabelling (rL (L.symm i)) infer_instance, }) (λ j, short_of_relabelling (rR j) infer_instance) end /-- If `x` has no left move or right moves, it is (very!) short. -/ def short_of_equiv_empty {x : pgame.{u}} (el : x.left_moves ≃ pempty) (er : x.right_moves ≃ pempty) : short x := short_of_relabelling (relabel_relabelling el er).symm short.of_pempty instance short_neg : Π (x : pgame.{u}) [short x], short (-x) | (mk xl xr xL xR) _ := begin resetI, apply short.mk, { rintro i, apply short_neg _, apply_instance, }, { rintro j, apply short_neg _, apply_instance, } end using_well_founded { dec_tac := pgame_wf_tac } instance short_add : Π (x y : pgame.{u}) [short x] [short y], short (x + y) | (mk xl xr xL xR) (mk yl yr yL yR) _ _ := begin resetI, apply short.mk, { rintro ⟨i⟩, { apply short_add, }, { change short (mk xl xr xL xR + yL i), apply short_add, } }, { rintro ⟨j⟩, { apply short_add, }, { change short (mk xl xr xL xR + yR j), apply short_add, } }, end using_well_founded { dec_tac := pgame_wf_tac } instance short_nat : Π n : ℕ, short n | 0 := pgame.short_0 | (n+1) := @pgame.short_add _ _ (short_nat n) pgame.short_1 instance short_bit0 (x : pgame.{u}) [short x] : short (bit0 x) := by { dsimp [bit0], apply_instance } instance short_bit1 (x : pgame.{u}) [short x] : short (bit1 x) := by { dsimp [bit1], apply_instance } /-- Auxiliary construction of decidability instances. We build `decidable (x ≤ y)` and `decidable (x < y)` in a simultaneous induction. Instances for the two projections separately are provided below. -/ def le_lt_decidable : Π (x y : pgame.{u}) [short x] [short y], decidable (x ≤ y) × decidable (x < y) | (mk xl xr xL xR) (mk yl yr yL yR) shortx shorty := begin resetI, split, { apply @and.decidable _ _ _ _, { apply @fintype.decidable_forall_fintype xl _ _ (by apply_instance), intro i, apply (@le_lt_decidable _ _ _ _).2; apply_instance, }, { apply @fintype.decidable_forall_fintype yr _ _ (by apply_instance), intro i, apply (@le_lt_decidable _ _ _ _).2; apply_instance, }, }, { apply @or.decidable _ _ _ _, { apply @fintype.decidable_exists_fintype yl _ _ (by apply_instance), intro i, apply (@le_lt_decidable _ _ _ _).1; apply_instance, }, { apply @fintype.decidable_exists_fintype xr _ _ (by apply_instance), intro i, apply (@le_lt_decidable _ _ _ _).1; apply_instance, }, }, end using_well_founded { dec_tac := pgame_wf_tac } instance le_decidable (x y : pgame.{u}) [short x] [short y] : decidable (x ≤ y) := (le_lt_decidable x y).1 instance lt_decidable (x y : pgame.{u}) [short x] [short y] : decidable (x < y) := (le_lt_decidable x y).2 instance equiv_decidable (x y : pgame.{u}) [short x] [short y] : decidable (x ≈ y) := and.decidable example : short 0 := by apply_instance example : short 1 := by apply_instance example : short 2 := by apply_instance example : short (-2) := by apply_instance example : short (of_lists [0] [1]) := by apply_instance example : short (of_lists [-2, -1] [1]) := by apply_instance example : short (0 + 0) := by apply_instance example : decidable ((1 : pgame) ≤ 1) := by apply_instance example : (0 : pgame) ≤ 0 := dec_trivial example : (1 : pgame) ≤ 1 := dec_trivial end pgame
b40ede235f815e52f29015a1f2a30e4485f231d3
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/algebra/monoid_algebra/grading.lean
dc5b3b1092c129c66094bbdca4864d1ec38618be
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,849
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.monoid_algebra.to_direct_sum import linear_algebra.finsupp /-! # Internal grading of an `add_monoid_algebra` In this file, we show that an `add_monoid_algebra` has an internal direct sum structure. ## Main results * `add_monoid_algebra.grade R i`: the `i`th grade of an `add_monoid_algebra R ι` * `add_monoid_algebra.equiv_grade`: the equivalence between an `add_monoid_algebra` and the direct sum of its grades. * `add_monoid_algebra.grade.is_internal`: propositionally, the statement that `add_monoid_algebra.grade` defines an internal graded structure. -/ noncomputable theory namespace add_monoid_algebra variables {ι : Type*} {R : Type*} section variables (R) /-- The submodule corresponding to each grade. -/ abbreviation grade [comm_semiring R] (i : ι) : submodule R (add_monoid_algebra R ι) := (finsupp.lsingle i).range end variables {R} [decidable_eq ι] [add_monoid ι] [comm_semiring R] open_locale direct_sum instance grade.gmonoid : direct_sum.gmonoid (λ i : ι, grade R i) := direct_sum.gmonoid.of_submodules (grade R) ⟨1, rfl⟩ $ λ i j, begin rintros ⟨-, a, rfl⟩ ⟨-, b, rfl⟩, exact ⟨_, single_mul_single.symm⟩, end /-- The canonical grade decomposition. -/ def to_grades : add_monoid_algebra R ι →ₐ[R] ⨁ i : ι, grade R i := add_monoid_algebra.lift _ _ _ { to_fun := λ i, direct_sum.of (λ i : ι, grade R i) i.to_add ⟨_, 1, rfl⟩, map_one' := rfl, map_mul' := λ i j, begin rw [direct_sum.of_mul_of, to_add_mul], congr, refine eq.trans _ single_mul_single.symm, rw one_mul, refl, end } @[simp] lemma to_grades_single (i : ι) (r : R) : to_grades (finsupp.single i r) = direct_sum.of (λ i : ι, grade R i) i ⟨finsupp.single _ _, r, rfl⟩ := begin refine (add_monoid_algebra.lift_single _ _ _).trans _, refine (direct_sum.of_smul _ _ _ _).symm.trans _, dsimp, congr' 1, ext : 1, refine (finsupp.smul_single _ _ _).trans _, rw [smul_eq_mul, mul_one], refl end @[simp] lemma to_grades_coe {i : ι} (x : grade R i) : to_grades ↑x = direct_sum.of (λ i, grade R i) i x := begin obtain ⟨-, x, rfl⟩ := x, exact to_grades_single _ _, end /-- The canonical recombination of grades. -/ def of_grades : (⨁ i : ι, grade R i) →ₐ[R] add_monoid_algebra R ι := direct_sum.to_algebra R _ (λ i : ι, submodule.subtype _) rfl (λ _ _ _ _, rfl) (λ r, rfl) @[simp] lemma of_grades_of (i : ι) (x : grade R i) : of_grades (direct_sum.of (λ i, grade R i) i x) = x := direct_sum.to_add_monoid_of _ _ _ @[simp] lemma of_grades_comp_to_grades : of_grades.comp to_grades = alg_hom.id R (add_monoid_algebra R ι) := begin ext : 2, dsimp, rw [to_grades_single, of_grades_of, subtype.coe_mk], end @[simp] lemma of_grades_to_grades (x : add_monoid_algebra R ι) : of_grades x.to_grades = x := alg_hom.congr_fun of_grades_comp_to_grades x @[simp] lemma to_grades_comp_of_grades : to_grades.comp of_grades = alg_hom.id R (⨁ i : ι, grade R i) := begin ext : 2, dsimp [direct_sum.lof_eq_of], rw [of_grades_of, to_grades_coe], end @[simp] lemma to_grades_of_grades (g : ⨁ i : ι, grade R i) : (of_grades g).to_grades = g := alg_hom.congr_fun to_grades_comp_of_grades g /-- An `add_monoid_algebra R ι` is equivalent as an algebra to the direct sum of its grades. -/ @[simps] def equiv_grades : add_monoid_algebra R ι ≃ₐ[R] ⨁ i : ι, grade R i := alg_equiv.of_alg_hom _ _ to_grades_comp_of_grades of_grades_comp_to_grades /-- `add_monoid_algebra.grades` describe an internally graded algebra -/ lemma grade.is_internal : direct_sum.submodule_is_internal (grade R : ι → submodule R _) := equiv_grades.symm.bijective end add_monoid_algebra
d58fe8824a823aa89eadf391edebe8b89fed4138
78269ad0b3c342b20786f60690708b6e328132b0
/src/library_dev/algebra/lattice/basic_experiment.lean
e4dc791b3d8f278f19aa37e146177d041acbc2b8
[]
no_license
dselsam/library_dev
e74f46010fee9c7b66eaa704654cad0fcd2eefca
1b4e34e7fb067ea5211714d6d3ecef5132fc8218
refs/heads/master
1,610,372,841,675
1,497,014,421,000
1,497,014,421,000
86,526,137
0
0
null
1,490,752,133,000
1,490,752,132,000
null
UTF-8
Lean
false
false
9,490
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Defines the inf/sup (semi)-lattice with optionally top/bot type class hierarchy. -/ import ..order ...tools.auto.finish open auto set_option old_structure_cmd true universes u v w -- TODO: move this eventually, if we decide to use them attribute [ematch] le_trans lt_of_le_of_lt lt_of_lt_of_le lt_trans section variable {α : Type u} -- TODO: this seems crazy, but it also seems to work reasonably well @[ematch] lemma le_antisymm' [weak_order α] : ∀ {a b : α}, (: a ≤ b :) → b ≤ a → a = b := weak_order.le_antisymm end /- TODO: automatic construction of dual definitions / theorems -/ namespace lattice reserve infixl ` ⊓ `:70 reserve infixl ` ⊔ `:65 class has_top (α : Type u) := (top : α) class has_bot (α : Type u) := (bot : α) class has_sup (α : Type u) := (sup : α → α → α) class has_inf (α : Type u) := (inf : α → α → α) class has_imp (α : Type u) := (imp : α → α → α) /- Better name -/ def imp {α : Type u} [has_imp α] : α → α → α := has_imp.imp infix ⊔ := has_sup.sup infix ⊓ := has_inf.inf notation `⊤` := has_top.top _ notation `⊥` := has_bot.bot _ class order_top (α : Type u) extends has_top α, weak_order α := (le_top : ∀ a : α, a ≤ ⊤) section order_top variables {α : Type u} [order_top α] {a : α} @[simp] lemma le_top : a ≤ ⊤ := order_top.le_top a lemma top_unique (h : ⊤ ≤ a) : a = ⊤ := le_antisymm le_top h -- TODO: delete in favor of the next? lemma eq_top_iff : a = ⊤ ↔ ⊤ ≤ a := ⟨take eq, eq^.symm ▸ le_refl ⊤, top_unique⟩ @[simp] lemma top_le_iff : ⊤ ≤ a ↔ a = ⊤ := ⟨top_unique, λ h, h.symm ▸ le_refl ⊤⟩ end order_top class order_bot (α : Type u) extends has_bot α, weak_order α := (bot_le : ∀ a : α, ⊥ ≤ a) section order_bot variables {α : Type u} [order_bot α] {a : α} @[simp] lemma bot_le : ⊥ ≤ a := order_bot.bot_le a lemma bot_unique (h : a ≤ ⊥) : a = ⊥ := le_antisymm h bot_le -- TODO: delete? lemma eq_bot_iff : a = ⊥ ↔ a ≤ ⊥ := ⟨take eq, eq^.symm ▸ le_refl ⊥, bot_unique⟩ @[simp] lemma le_bot_iff : a ≤ ⊥ ↔ a = ⊥ := ⟨bot_unique, take h, h.symm ▸ le_refl ⊥⟩ lemma neq_bot_of_le_neq_bot {a b : α} (hb : b ≠ ⊥) (hab : b ≤ a) : a ≠ ⊥ := take ha, hb $ bot_unique $ ha ▸ hab end order_bot class semilattice_sup (α : Type u) extends has_sup α, weak_order α := (le_sup_left : ∀ a b : α, a ≤ a ⊔ b) (le_sup_right : ∀ a b : α, b ≤ a ⊔ b) (sup_le : ∀ a b c : α, a ≤ c → b ≤ c → a ⊔ b ≤ c) section semilattice_sup variables {α : Type u} [semilattice_sup α] {a b c d : α} lemma le_sup_left : a ≤ a ⊔ b := semilattice_sup.le_sup_left a b @[ematch] lemma le_sup_left' : a ≤ (: a ⊔ b :) := semilattice_sup.le_sup_left a b lemma le_sup_right : b ≤ a ⊔ b := semilattice_sup.le_sup_right a b @[ematch] lemma le_sup_right' : b ≤ (: a ⊔ b :) := semilattice_sup.le_sup_right a b lemma le_sup_left_of_le (h : c ≤ a) : c ≤ a ⊔ b := by finish lemma le_sup_right_of_le (h : c ≤ b) : c ≤ a ⊔ b := by finish lemma sup_le : a ≤ c → b ≤ c → a ⊔ b ≤ c := semilattice_sup.sup_le a b c @[simp] lemma sup_le_iff : a ⊔ b ≤ c ↔ a ≤ c ∧ b ≤ c := ⟨assume h : a ⊔ b ≤ c, ⟨le_trans le_sup_left h, le_trans le_sup_right h⟩, take ⟨h₁, h₂⟩, sup_le h₁ h₂⟩ -- TODO: if we just write le_antisymm, Lean doesn't know which ≤ we want to use -- Can we do anything about that? lemma sup_of_le_left (h : b ≤ a) : a ⊔ b = a := by apply @le_antisymm α _ ; finish lemma sup_of_le_right (h : a ≤ b) : a ⊔ b = b := by apply @le_antisymm α _ ; finish lemma sup_le_sup (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊔ c ≤ b ⊔ d := by finish lemma le_of_sup_eq (h : a ⊔ b = b) : a ≤ b := by finish @[simp] lemma sup_idem : a ⊔ a = a := by apply @le_antisymm α _ ; finish lemma sup_comm : a ⊔ b = b ⊔ a := by apply @le_antisymm α _ ; finish -- -- TODO(Jeremy): stopped here -- instance semilattice_sup_to_is_commutative [semilattice_sup α] : is_commutative α (⊔) := ⟨@sup_comm _ _⟩ lemma sup_assoc : a ⊔ b ⊔ c = a ⊔ (b ⊔ c) := le_antisymm (sup_le (sup_le le_sup_left (le_sup_right_of_le le_sup_left)) (le_sup_right_of_le le_sup_right)) (sup_le (le_sup_left_of_le le_sup_left) (sup_le (le_sup_left_of_le le_sup_right) le_sup_right)) instance semilattice_sup_to_is_associative [semilattice_sup α] : is_associative α (⊔) := ⟨@sup_assoc _ _⟩ end semilattice_sup class semilattice_inf (α : Type u) extends has_inf α, weak_order α := (inf_le_left : ∀ a b : α, a ⊓ b ≤ a) (inf_le_right : ∀ a b : α, a ⊓ b ≤ b) (le_inf : ∀ a b c : α, a ≤ b → a ≤ c → a ≤ b ⊓ c) section semilattice_inf variables {α : Type u} [semilattice_inf α] {a b c d : α} lemma inf_le_left : a ⊓ b ≤ a := semilattice_inf.inf_le_left a b lemma inf_le_right : a ⊓ b ≤ b := semilattice_inf.inf_le_right a b lemma le_inf : a ≤ b → a ≤ c → a ≤ b ⊓ c := semilattice_inf.le_inf a b c lemma inf_le_left_of_le (h : a ≤ c) : a ⊓ b ≤ c := le_trans inf_le_left h lemma inf_le_right_of_le (h : b ≤ c) : a ⊓ b ≤ c := le_trans inf_le_right h lemma le_inf_iff : a ≤ b ⊓ c ↔ a ≤ b ∧ a ≤ c := ⟨assume h : a ≤ b ⊓ c, ⟨le_trans h inf_le_left, le_trans h inf_le_right⟩, take ⟨h₁, h₂⟩, le_inf h₁ h₂⟩ lemma inf_of_le_left (h : a ≤ b) : a ⊓ b = a := le_antisymm inf_le_left (le_inf (le_refl _) h) lemma inf_of_le_right (h : b ≤ a) : a ⊓ b = b := le_antisymm inf_le_right (le_inf h (le_refl _)) lemma inf_le_inf (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊓ c ≤ b ⊓ d := le_inf (inf_le_left_of_le h₁) (inf_le_right_of_le h₂) lemma le_of_inf_eq (h : a ⊓ b = a) : a ≤ b := h ▸ inf_le_right @[simp] lemma inf_idem : a ⊓ a = a := inf_of_le_left (le_refl _) lemma inf_comm : a ⊓ b = b ⊓ a := have ∀{a b : α}, a ⊓ b ≤ b ⊓ a, from take a b, le_inf inf_le_right inf_le_left, le_antisymm this this instance semilattice_inf_to_is_commutative [semilattice_inf α] : is_commutative α (⊓) := ⟨@inf_comm _ _⟩ lemma inf_assoc : a ⊓ b ⊓ c = a ⊓ (b ⊓ c) := le_antisymm (le_inf (inf_le_left_of_le inf_le_left) (le_inf (inf_le_left_of_le inf_le_right) inf_le_right)) (le_inf (le_inf inf_le_left (inf_le_right_of_le inf_le_left)) (inf_le_right_of_le inf_le_right)) instance semilattice_inf_to_is_associative [semilattice_inf α] : is_associative α (⊓) := ⟨@inf_assoc _ _⟩ end semilattice_inf class semilattice_sup_top (α : Type u) extends order_top α, semilattice_sup α section semilattice_sup_top variables {α : Type u} [semilattice_sup_top α] {a : α} @[simp] lemma top_sup_eq : ⊤ ⊔ a = ⊤ := sup_of_le_left le_top @[simp] lemma sup_top_eq : a ⊔ ⊤ = ⊤ := sup_of_le_right le_top end semilattice_sup_top class semilattice_sup_bot (α : Type u) extends order_bot α, semilattice_sup α section semilattice_sup_bot variables {α : Type u} [semilattice_sup_bot α] {a b : α} @[simp] lemma bot_sup_eq : ⊥ ⊔ a = a := sup_of_le_right bot_le @[simp] lemma sup_bot_eq : a ⊔ ⊥ = a := sup_of_le_left bot_le @[simp] lemma sup_eq_bot_iff : a ⊔ b = ⊥ ↔ (a = ⊥ ∧ b = ⊥) := by rw [eq_bot_iff, sup_le_iff]; simp end semilattice_sup_bot class semilattice_inf_top (α : Type u) extends order_top α, semilattice_inf α section semilattice_inf_top variables {α : Type u} [semilattice_inf_top α] {a b : α} @[simp] lemma top_inf_eq : ⊤ ⊓ a = a := inf_of_le_right le_top @[simp] lemma inf_top_eq : a ⊓ ⊤ = a := inf_of_le_left le_top @[simp] lemma inf_eq_top_iff : a ⊓ b = ⊤ ↔ (a = ⊤ ∧ b = ⊤) := by rw [eq_top_iff, le_inf_iff]; simp end semilattice_inf_top class semilattice_inf_bot (α : Type u) extends order_bot α, semilattice_inf α section semilattice_inf_bot variables {α : Type u} [semilattice_inf_bot α] {a : α} @[simp] lemma bot_inf_eq : ⊥ ⊓ a = ⊥ := inf_of_le_left bot_le @[simp] lemma inf_bot_eq : a ⊓ ⊥ = ⊥ := inf_of_le_right bot_le end semilattice_inf_bot /- Lattices -/ class lattice (α : Type u) extends semilattice_sup α, semilattice_inf α section lattice variables {α : Type u} [lattice α] {a b c d : α} /- Distributivity laws -/ /- TODO: better names? -/ lemma sup_inf_le : a ⊔ (b ⊓ c) ≤ (a ⊔ b) ⊓ (a ⊔ c) := sup_le (le_inf le_sup_left le_sup_left) $ le_inf (inf_le_left_of_le le_sup_right) (inf_le_right_of_le le_sup_right) lemma le_inf_sup : (a ⊓ b) ⊔ (a ⊓ c) ≤ a ⊓ (b ⊔ c) := le_inf (sup_le inf_le_left inf_le_left) $ sup_le (le_sup_left_of_le inf_le_right) (le_sup_right_of_le inf_le_right) lemma inf_sup_self : a ⊓ (a ⊔ b) = a := le_antisymm inf_le_left (le_inf (le_refl a) le_sup_left) lemma sup_inf_self : a ⊔ (a ⊓ b) = a := le_antisymm (sup_le (le_refl a) inf_le_left) le_sup_left end lattice /- Lattices derived from linear orders -/ instance lattice_of_decidable_linear_order {α : Type u} [o : decidable_linear_order α] : lattice α := { o with sup := max, le_sup_left := le_max_left, le_sup_right := le_max_right, sup_le := take a b c, max_le, inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := take a b c, le_min } end lattice
b3f192cf1b5f8105583fb45adb8669d3a0db0e2f
4fa161becb8ce7378a709f5992a594764699e268
/test/nth_rewrite.lean
257d4347f179787aa29df1f2bd4d496a9909b78f
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
3,169
lean
/- Copyright (c) 2018 Keeley Hoek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Keeley Hoek, Scott Morrison -/ import tactic.nth_rewrite import init_.data.nat.lemmas import data.vector structure F := (a : ℕ) (v : vector ℕ a) (p : v.val = []) example (f : F) : f.v.val = [] := begin nth_rewrite 0 [f.p], end structure cat := (O : Type) (H : O → O → Type) (i : Π o : O, H o o) (c : Π {X Y Z : O} (f : H X Y) (g : H Y Z), H X Z) (li : Π {X Y : O} (f : H X Y), c (i X) f = f) (ri : Π {X Y : O} (f : H X Y), c f (i Y) = f) (a : Π {W X Y Z : O} (f : H W X) (g : H X Y) (h : H Y Z), c (c f g) h = c f (c g h)) open tactic example (C : cat) (W X Y Z : C.O) (f : C.H X Y) (g : C.H W X) (h k : C.H Y Z) : C.c (C.c g f) h = C.c g (C.c f h) := begin nth_rewrite 0 [C.a], end example (C : cat) (X Y : C.O) (f : C.H X Y) : C.c f (C.i Y) = f := begin nth_rewrite 0 [C.ri], end -- The next two examples fail when using the kabstract backend. axiom foo : [1] = [2] example : [[1], [1], [1]] = [[1], [2], [1]] := begin nth_rewrite_lhs 1 [foo], end axiom foo' : [6] = [7] axiom bar' : [[5],[5]] = [[6],[6]] example : [[7],[6]] = [[5],[5]] := begin nth_rewrite_lhs 0 foo', nth_rewrite_rhs 0 bar', nth_rewrite_lhs 0 ←foo', nth_rewrite_lhs 0 ←foo', end axiom wowzer : (3, 3) = (5, 2) axiom kachow (n : ℕ) : (4, n) = (5, n) axiom pchew (n : ℕ) : (n, 5) = (5, n) axiom smash (n m : ℕ) : (n, m) = (1, 1) example : [(3, 3), (5, 9), (5, 9)] = [(4, 5), (3, 6), (1, 1)] := begin nth_rewrite_lhs 0 wowzer, nth_rewrite_lhs 2 ←pchew, nth_rewrite_rhs 0 pchew, nth_rewrite_rhs 0 smash, nth_rewrite_rhs 1 smash, nth_rewrite_rhs 2 smash, nth_rewrite_lhs 0 smash, nth_rewrite_lhs 1 smash, nth_rewrite_lhs 2 smash, end example (a b c : ℕ) : c + a + b = a + c + b := begin nth_rewrite_rhs 1 add_comm, end -- With the `kabstract` backend, we only find one rewrite, even though there are obviously two. -- The problem is that `(a + b) + c` matches directly, so the WHOLE THING gets replaced with a -- metavariable, per the `kabstract` strategy. This is devastating to the search, since we cannot -- see inside this metavariable. -- I still think it's fixable. Because all applications have an order, I'm pretty sure we can't -- miss any rewrites if we also look inside every thing we chunk-up into a metavariable as well. -- In almost every case this will bring up no results (with the exception of situations like this -- one), so there should be essentially no change in complexity. example (x y : Prop) (h₁ : x ↔ y) (h₂ : x ↔ x ∧ x) : x ∧ x ↔ x := begin nth_rewrite_rhs 1 [h₁] at h₂, nth_rewrite_rhs 0 [← h₁] at h₂, nth_rewrite_rhs 0 h₂, end example (x y : ℕ) (h₁ : x = y) (h₂ : x = x + x) : x + x = x := begin nth_rewrite_rhs 1 [h₁] at h₂, nth_rewrite_rhs 0 [← h₁] at h₂, nth_rewrite_rhs 0 h₂, end example (x y z : ℕ) (h1 : x = y) (h2 : y = z) : x + x + x + y = y + y + x + x := begin nth_rewrite 2 [h1, h2], -- h2 is not used nth_rewrite 2 [h2], repeat { rw h1 }, repeat { rw h2 }, end
759f745bc167f06145fce64101cff1a86fb00c94
54d7e71c3616d331b2ec3845d31deb08f3ff1dea
/tests/lean/run/infix_paren.lean
0b71264bf161c7f15f902e9faeb8f4ae88a6913f
[ "Apache-2.0" ]
permissive
pachugupta/lean
6f3305c4292288311cc4ab4550060b17d49ffb1d
0d02136a09ac4cf27b5c88361750e38e1f485a1a
refs/heads/master
1,611,110,653,606
1,493,130,117,000
1,493,167,649,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
555
lean
open list #eval filter (< 10) [20, 5, 10, 3, 2, 14, 1] #eval qsort (<) [20, 5, 10, 3, 2, 14, 1] #eval foldl (+) 0 [1, 2, 3] example : foldl (+) 0 [3, 4, 1] = 8 := rfl example : foldl (*) 2 [3, 4, 1] = 24 := rfl #check (+) 1 2 example : (+) 1 2 = 3 := rfl example : (*) 3 4 = 12 := rfl example : (++) [1,2] [3,4] = [1,2,3,4] := rfl example : (++) [1,2] [3,4] = [1,2] ++ [3,4] := rfl /- (-) is rejected since we have prefix notation for - We cannot write (::) since we use (: t :) for annotating patterns for ematching. We have to write ( :: ) -/
90ebef3ad03825e60bc79be0c861c957a5230eb7
2bafba05c98c1107866b39609d15e849a4ca2bb8
/src/week_5/Part_B_topology.lean
c50829548ce989076dd9788f9fc46c2dfadfae11
[ "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
5,478
lean
import topology.subset_properties variables (X Y : Type) [topological_space X] [topological_space Y] (f : X → Y) open filter set open_locale filter -- for 𝓟 open_locale topological_space -- for 𝓝 /- ## Neighbourhood filters If `α` is a topological space and `a : α` then `𝓝 a` is the following filter on `α`: `X ∈ 𝓝 a` if and only if `X` contains an open neighbourhood of `a`, or equivalently if `a` is in the interior of `X`. You should think of `𝓝 a` as the "generalised subset" of `X` corresponding to an infinitesimally small open neighbourhood of `a`. Let's use the API for interior and closure, and check that `𝓝 a` is a filter. Useful stuff from the topological space API: `interior_univ : interior univ = univ` `mem_univ x : x ∈ univ` `interior_mono : s ⊆ t → interior s ⊆ interior t` and guess what this is called: `??? : interior (s ∩ t) = interior s ∩ interior t` If you don't know how to #check your guess, ask me! -/ variables {α : Type*} [topological_space α] open set -- neighbourhood filter 𝓝 a on α example (a : α): filter α := { sets := {X : set α | a ∈ interior X}, univ_sets := begin sorry, end, sets_of_superset := begin sorry, end, inter_sets := begin sorry, end } /- ## Cluster points. A cluster point `a : α` of a filter `F : filter α` on a topological space should be thought of as a point in the closure of the "generalised set" corresponding to `F`. Here's the formal definition. A cluster point of a filter `F : filter α` (also known as an accumulation point or a limit point) is `x : α` such that `𝓝 x ⊓ F ≠ ⊥`. The picture is that the intersection of the generalised set `F` and the infinitesimal open neighbourhood `𝓝 x` of `x` is nonempty, or in other words that `x` is in some kind of "closure" of `F`. Let's go through the notation more carefully though, to try and figure out what it means. Recall that the order on filters is upside-down, so `𝓝 x ⊓ F` means the filter generated by `F` and the neighbourhoods of `x`, and `⊥` is the filter which contains every subset. So this boils down to saying that there do not exist sets `A ∈ 𝓝 x` and `B ∈ F` such that `A ∩ B = ∅`, or, in other words, every element of the filter intersects every neighbourhood of `x`. To give an example, if `S` is any subset of `α` then the cluster points of `𝓟 S` are just the points `x` such that any open set containing `x` meets `S`, or equivalently that `x` is in the closure of `S`. The below lemma is called `cluster_pt.mono` in mathlib. The picture is that if `F` and `G` are generalised subsets of a topological space and `F ⊆ G`, then `closure F ⊆ closure G`. You can prove it by using `cluster_pt.mono` of course, but why not give a direct proof yourself? Start by rewriting `cluster_pt_iff`, which translates down the definition of `cluster_pt` to a purely set-theoretic one not involving `⊓`. -/ example {x : α} {F G : filter α} (hxF : cluster_pt x F) (hFG : F ≤ G) : cluster_pt x G := begin sorry, end /- ## Compactness The actual definition of `is_compact` in mathlib: A subset `S` of a topological space `α` is *compact* if for every filter `F ≠ ⊥` such that `S ∈ F`, there exists `a : α` such that every set of `F` also meets every neighborhood of `a`. In pictures -- `S` is compact iff the closure of every non-empty generalised subset `F ⊆ S` contains an element of `S`. I'm not too sure this picture helps much (at least, it doesn't help me too much). Somehow noncompactness results in generalised subsets which are "on the boundary of `S`" but which don't intersect a small neighbourhood of any elements of `S`. Perhaps it's best not to worry about this exotic definition right now -- or perhaps one of you can explain it to me. Of course it's equivalent to the usual definition of compactness, but we won't prove this. So here is another proof that a closed subset of a compact space is compact. As before, we prove the more general assertion that if `α` is any topological space then the intersection of a compact subset of `α` and a closed subset of `α` is a compact subset of `α`. Here's the actual definition in mathlib: `def is_compact (s : set α) := ∀ ⦃f⦄ [ne_bot f], f ≤ 𝓟 s → ∃a∈s, cluster_pt a f` Note that `ne_bot f` is in square brackets, which means that the type class inference system is supposed to supply it. We will explicitly add this fact into the type class inference system with `haveI` below. Now here's a suggestion for a proof. Say `S` is compact and `C` is closed. Say `F` is a non-bot filter with `F ≤ 𝓟 (S ∩ C)`. First show that by compactness of `S`, we can find a cluster point `a` for `F` in `S`. Now show that this cluster point is in `C` too, because `C` is closed. You might find `is_closed.closure_eq : is_closed C → closure C = C` useful, and also `mem_closure_iff_cluster_pt : a ∈ closure S ↔ cluster_pt a (𝓟 S)` -/ lemma closed_of_compact (S : set X) (hS : is_compact S) (C : set X) (hC : is_closed C) : is_compact (S ∩ C) := begin -- assume `F` is a filter which is not `⊥`, and `≤` is the principal -- filter on `S ∩ C` (i.e. which contains `S ∩ C`). We need to find a -- cluster point for `F` which is in `S ∩ C`. intros F hnF hFSC, -- Let's tell the type class inference system about `hnf : f.ne_bot` haveI := hnF, -- see if you can take it from here. sorry, end
bab527c0289ff4524a5ab03165244f5cdaa25f7a
737dc4b96c97368cb66b925eeea3ab633ec3d702
/stage0/src/Lean/Meta/Match/Match.lean
d02354efee755bdeb043f3e2b718ae81ebf75c5e
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
41,153
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.CollectLevelParams import Lean.Util.Recognizers import Lean.Compiler.ExternAttr import Lean.Meta.Check import Lean.Meta.Closure import Lean.Meta.Tactic.Cases import Lean.Meta.Tactic.Contradiction import Lean.Meta.GeneralizeTelescope import Lean.Meta.Match.Basic import Lean.Meta.Match.MVarRenaming import Lean.Meta.Match.CaseValues namespace Lean.Meta.Match /- The number of patterns in each AltLHS must be equal to majors.length -/ private def checkNumPatterns (majors : Array Expr) (lhss : List AltLHS) : MetaM Unit := do let num := majors.size if lhss.any fun lhs => lhs.patterns.length != num then throwError "incorrect number of patterns" /- Given a list of `AltLHS`, create a minor premise for each one, convert them into `Alt`, and then execute `k` -/ private def withAlts {α} (motive : Expr) (lhss : List AltLHS) (k : List Alt → Array (Expr × Nat) → MetaM α) : MetaM α := loop lhss [] #[] where mkMinorType (xs : Array Expr) (lhs : AltLHS) : MetaM Expr := withExistingLocalDecls lhs.fvarDecls do let args ← lhs.patterns.toArray.mapM (Pattern.toExpr · (annotate := true)) let minorType := mkAppN motive args mkForallFVars xs minorType loop (lhss : List AltLHS) (alts : List Alt) (minors : Array (Expr × Nat)) : MetaM α := do match lhss with | [] => k alts.reverse minors | lhs::lhss => let xs := lhs.fvarDecls.toArray.map LocalDecl.toExpr let minorType ← mkMinorType xs lhs let (minorType, minorNumParams) := if !xs.isEmpty then (minorType, xs.size) else (mkSimpleThunkType minorType, 1) let idx := alts.length let minorName := (`h).appendIndexAfter (idx+1) trace[Meta.Match.debug] "minor premise {minorName} : {minorType}" withLocalDeclD minorName minorType fun minor => do let rhs := if xs.isEmpty then mkApp minor (mkConst `Unit.unit) else mkAppN minor xs let minors := minors.push (minor, minorNumParams) let fvarDecls ← lhs.fvarDecls.mapM instantiateLocalDeclMVars let alts := { ref := lhs.ref, idx := idx, rhs := rhs, fvarDecls := fvarDecls, patterns := lhs.patterns : Alt } :: alts loop lhss alts minors def assignGoalOf (p : Problem) (e : Expr) : MetaM Unit := withGoalOf p (assignExprMVar p.mvarId e) structure State where used : Std.HashSet Nat := {} -- used alternatives counterExamples : List (List Example) := [] /-- Return true if the given (sub-)problem has been solved. -/ private def isDone (p : Problem) : Bool := p.vars.isEmpty /-- Return true if the next element on the `p.vars` list is a variable. -/ private def isNextVar (p : Problem) : Bool := match p.vars with | Expr.fvar _ _ :: _ => true | _ => false private def hasAsPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.as _ _ :: _ => true | _ => false private def hasCtorPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.ctor _ _ _ _ :: _ => true | _ => false private def hasValPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.val _ :: _ => true | _ => false private def hasNatValPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.val v :: _ => v.isNatLit | _ => false private def hasVarPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.var _ :: _ => true | _ => false private def hasArrayLitPattern (p : Problem) : Bool := p.alts.any fun alt => match alt.patterns with | Pattern.arrayLit _ _ :: _ => true | _ => false private def isVariableTransition (p : Problem) : Bool := p.alts.all fun alt => match alt.patterns with | Pattern.inaccessible _ :: _ => true | Pattern.var _ :: _ => true | _ => false private def isConstructorTransition (p : Problem) : Bool := (hasCtorPattern p || p.alts.isEmpty) && p.alts.all fun alt => match alt.patterns with | Pattern.ctor _ _ _ _ :: _ => true | Pattern.var _ :: _ => true | Pattern.inaccessible _ :: _ => true | _ => false private def isValueTransition (p : Problem) : Bool := hasVarPattern p && hasValPattern p && p.alts.all fun alt => match alt.patterns with | Pattern.val _ :: _ => true | Pattern.var _ :: _ => true | _ => false private def isArrayLitTransition (p : Problem) : Bool := hasArrayLitPattern p && hasVarPattern p && p.alts.all fun alt => match alt.patterns with | Pattern.arrayLit _ _ :: _ => true | Pattern.var _ :: _ => true | _ => false private def isNatValueTransition (p : Problem) : Bool := hasNatValPattern p && (!isNextVar p || p.alts.any fun alt => match alt.patterns with | Pattern.ctor _ _ _ _ :: _ => true | Pattern.inaccessible _ :: _ => true | _ => false) private def processSkipInaccessible (p : Problem) : Problem := match p.vars with | [] => unreachable! | x :: xs => do let alts := p.alts.map fun alt => match alt.patterns with | Pattern.inaccessible _ :: ps => { alt with patterns := ps } | _ => unreachable! { p with alts := alts, vars := xs } private def processLeaf (p : Problem) : StateRefT State MetaM Unit := match p.alts with | [] => do /- TODO: allow users to configure which tactic is used to close leaves. -/ unless (← contradictionCore p.mvarId {}) do trace[Meta.Match.match] "missing alternative" admit p.mvarId modify fun s => { s with counterExamples := p.examples :: s.counterExamples } | alt :: _ => do -- TODO: check whether we have unassigned metavars in rhs liftM $ assignGoalOf p alt.rhs modify fun s => { s with used := s.used.insert alt.idx } private def processAsPattern (p : Problem) : MetaM Problem := match p.vars with | [] => unreachable! | x :: xs => withGoalOf p do let alts ← p.alts.mapM fun alt => match alt.patterns with | Pattern.as fvarId p :: ps => /- We used to use `checkAndReplaceFVarId` here, but `x` and `fvarId` may have different types when dependent types are beind used. Let's consider the repro for issue #471 ``` inductive vec : Nat → Type | nil : vec 0 | cons : Int → vec n → vec n.succ def vec_len : vec n → Nat | vec.nil => 0 | x@(vec.cons h t) => vec_len t + 1 ``` we reach the state ``` [Meta.Match.match] remaining variables: [x✝:(vec n✝)] alternatives: [n:(Nat), x:(vec (Nat.succ n)), h:(Int), t:(vec n)] |- [x@(vec.cons n h t)] => h_1 n x h t [x✝:(vec n✝)] |- [x✝] => h_2 n✝ x✝ ``` The variables `x✝:(vec n✝)` and `x:(vec (Nat.succ n))` have different types, but we perform the substitution anyway, because we claim the "discrepancy" will be corrected after we process the pattern `(vec.cons n h t)`. The right-hand-side is temporarily type incorrect, but we claim this is fine because it will be type correct again after we the pattern `(vec.cons n h t)`. TODO: try to find a cleaner solution. -/ { alt with patterns := p :: ps }.replaceFVarId fvarId x | _ => pure alt pure { p with alts := alts } private def processVariable (p : Problem) : MetaM Problem := match p.vars with | [] => unreachable! | x :: xs => withGoalOf p do let alts ← p.alts.mapM fun alt => match alt.patterns with | Pattern.inaccessible _ :: ps => pure { alt with patterns := ps } | Pattern.var fvarId :: ps => { alt with patterns := ps }.checkAndReplaceFVarId fvarId x | _ => unreachable! pure { p with alts := alts, vars := xs } private def throwInductiveTypeExpected {α} (e : Expr) : MetaM α := do let t ← inferType e throwError "failed to compile pattern matching, inductive type expected{indentExpr e}\nhas type{indentExpr t}" private def inLocalDecls (localDecls : List LocalDecl) (fvarId : FVarId) : Bool := localDecls.any fun d => d.fvarId == fvarId namespace Unify structure Context where altFVarDecls : List LocalDecl structure State where fvarSubst : FVarSubst := {} abbrev M := ReaderT Context $ StateRefT State MetaM def isAltVar (fvarId : FVarId) : M Bool := do return inLocalDecls (← read).altFVarDecls fvarId def expandIfVar (e : Expr) : M Expr := do match e with | Expr.fvar _ _ => return (← get).fvarSubst.apply e | _ => return e def occurs (fvarId : FVarId) (v : Expr) : Bool := Option.isSome $ v.find? fun e => match e with | Expr.fvar fvarId' _ => fvarId == fvarId' | _=> false def assign (fvarId : FVarId) (v : Expr) : M Bool := do if occurs fvarId v then trace[Meta.Match.unify] "assign occurs check failed, {mkFVar fvarId} := {v}" pure false else let ctx ← read if (← isAltVar fvarId) then trace[Meta.Match.unify] "{mkFVar fvarId} := {v}" modify fun s => { s with fvarSubst := s.fvarSubst.insert fvarId v } pure true else trace[Meta.Match.unify] "assign failed variable is not local, {mkFVar fvarId} := {v}" pure false partial def unify (a : Expr) (b : Expr) : M Bool := do trace[Meta.Match.unify] "{a} =?= {b}" if (← isDefEq a b) then pure true else let a' ← whnfD (← expandIfVar a) let b' ← whnfD (← expandIfVar b) if a != a' || b != b' then unify a' b' else match a, b with | Expr.fvar aFvarId _, Expr.fvar bFVarId _ => assign aFvarId b <||> assign bFVarId a | Expr.fvar aFvarId _, b => assign aFvarId b | a, Expr.fvar bFVarId _ => assign bFVarId a | Expr.app aFn aArg _, Expr.app bFn bArg _ => unify aFn bFn <&&> unify aArg bArg | _, _ => pure false end Unify private def unify? (altFVarDecls : List LocalDecl) (a b : Expr) : MetaM (Option FVarSubst) := do let a ← instantiateMVars a let b ← instantiateMVars b let (b, s) ← Unify.unify a b { altFVarDecls := altFVarDecls} |>.run {} if b then pure s.fvarSubst else trace[Meta.Match.unify] "failed to unify {a} =?= {b}" pure none private def expandVarIntoCtor? (alt : Alt) (fvarId : FVarId) (ctorName : Name) : MetaM (Option Alt) := withExistingLocalDecls alt.fvarDecls do let env ← getEnv let ldecl ← getLocalDecl fvarId let expectedType ← inferType (mkFVar fvarId) let expectedType ← whnfD expectedType let (ctorLevels, ctorParams) ← getInductiveUniverseAndParams expectedType let ctor := mkAppN (mkConst ctorName ctorLevels) ctorParams let ctorType ← inferType ctor forallTelescopeReducing ctorType fun ctorFields resultType => do let ctor := mkAppN ctor ctorFields let alt := alt.replaceFVarId fvarId ctor let ctorFieldDecls ← ctorFields.mapM fun ctorField => getLocalDecl ctorField.fvarId! let newAltDecls := ctorFieldDecls.toList ++ alt.fvarDecls let subst? ← unify? newAltDecls resultType expectedType match subst? with | none => pure none | some subst => let newAltDecls := newAltDecls.filter fun d => !subst.contains d.fvarId -- remove declarations that were assigned let newAltDecls := newAltDecls.map fun d => d.applyFVarSubst subst -- apply substitution to remaining declaration types let patterns := alt.patterns.map fun p => p.applyFVarSubst subst let rhs := subst.apply alt.rhs let ctorFieldPatterns := ctorFields.toList.map fun ctorField => match subst.get ctorField.fvarId! with | e@(Expr.fvar fvarId _) => if inLocalDecls newAltDecls fvarId then Pattern.var fvarId else Pattern.inaccessible e | e => Pattern.inaccessible e pure $ some { alt with fvarDecls := newAltDecls, rhs := rhs, patterns := ctorFieldPatterns ++ patterns } private def getInductiveVal? (x : Expr) : MetaM (Option InductiveVal) := do let xType ← inferType x let xType ← whnfD xType match xType.getAppFn with | Expr.const constName _ _ => let cinfo ← getConstInfo constName match cinfo with | ConstantInfo.inductInfo val => pure (some val) | _ => pure none | _ => pure none private def hasRecursiveType (x : Expr) : MetaM Bool := do match (← getInductiveVal? x) with | some val => pure val.isRec | _ => pure false /- Given `alt` s.t. the next pattern is an inaccessible pattern `e`, try to normalize `e` into a constructor application. If it is not a constructor, throw an error. Otherwise, if it is a constructor application of `ctorName`, update the next patterns with the fields of the constructor. Otherwise, return none. -/ def processInaccessibleAsCtor (alt : Alt) (ctorName : Name) : MetaM (Option Alt) := do let env ← getEnv match alt.patterns with | p@(Pattern.inaccessible e) :: ps => trace[Meta.Match.match] "inaccessible in ctor step {e}" withExistingLocalDecls alt.fvarDecls do -- Try to push inaccessible annotations. let e ← whnfD e match e.constructorApp? env with | some (ctorVal, ctorArgs) => if ctorVal.name == ctorName then let fields := ctorArgs.extract ctorVal.numParams ctorArgs.size let fields := fields.toList.map Pattern.inaccessible pure $ some { alt with patterns := fields ++ ps } else pure none | _ => throwErrorAt alt.ref "dependent match elimination failed, inaccessible pattern found{indentD p.toMessageData}\nconstructor expected" | _ => unreachable! private def hasNonTrivialExample (p : Problem) : Bool := p.examples.any fun | Example.underscore => false | _ => true private def throwCasesException (p : Problem) (ex : Exception) : MetaM α := do match ex with | Exception.error ref msg => let exampleMsg := if hasNonTrivialExample p then m!" after processing{indentD <| examplesToMessageData p.examples}" else "" throw <| Exception.error ref <| m!"{msg}{exampleMsg}\n" ++ "the dependent pattern matcher can solve the following kinds of equations\n" ++ "- <var> = <term> and <term> = <var>\n" ++ "- <term> = <term> where the terms are definitionally equal\n" ++ "- <constructor> = <constructor>, examples: List.cons x xs = List.cons y ys, and List.cons x xs = List.nil" | _ => throw ex private def processConstructor (p : Problem) : MetaM (Array Problem) := do trace[Meta.Match.match] "constructor step" let env ← getEnv match p.vars with | [] => unreachable! | x :: xs => do let subgoals? ← commitWhenSome? do let subgoals ← try cases p.mvarId x.fvarId! catch ex => if p.alts.isEmpty then /- If we have no alternatives and dependent pattern matching fails, then a "missing cases" error is bettern than a "stuck" error message. -/ return none else throwCasesException p ex if subgoals.isEmpty then /- Easy case: we have solved problem `p` since there are no subgoals -/ pure (some #[]) else if !p.alts.isEmpty then pure (some subgoals) else do let isRec ← withGoalOf p $ hasRecursiveType x /- If there are no alternatives and the type of the current variable is recursive, we do NOT consider a constructor-transition to avoid nontermination. TODO: implement a more general approach if this is not sufficient in practice -/ if isRec then pure none else pure (some subgoals) match subgoals? with | none => pure #[{ p with vars := xs }] | some subgoals => subgoals.mapM fun subgoal => withMVarContext subgoal.mvarId do let subst := subgoal.subst let fields := subgoal.fields.toList let newVars := fields ++ xs let newVars := newVars.map fun x => x.applyFVarSubst subst let subex := Example.ctor subgoal.ctorName $ fields.map fun field => match field with | Expr.fvar fvarId _ => Example.var fvarId | _ => Example.underscore -- This case can happen due to dependent elimination let examples := p.examples.map $ Example.replaceFVarId x.fvarId! subex let examples := examples.map $ Example.applyFVarSubst subst let newAlts := p.alts.filter fun alt => match alt.patterns with | Pattern.ctor n _ _ _ :: _ => n == subgoal.ctorName | Pattern.var _ :: _ => true | Pattern.inaccessible _ :: _ => true | _ => false let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst let newAlts ← newAlts.filterMapM fun alt => match alt.patterns with | Pattern.ctor _ _ _ fields :: ps => pure $ some { alt with patterns := fields ++ ps } | Pattern.var fvarId :: ps => expandVarIntoCtor? { alt with patterns := ps } fvarId subgoal.ctorName | Pattern.inaccessible _ :: _ => processInaccessibleAsCtor alt subgoal.ctorName | _ => unreachable! pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples } private def processNonVariable (p : Problem) : MetaM Problem := match p.vars with | [] => unreachable! | x :: xs => withGoalOf p do let x ← whnfD x let env ← getEnv match x.constructorApp? env with | some (ctorVal, xArgs) => let alts ← p.alts.filterMapM fun alt => match alt.patterns with | Pattern.ctor n _ _ fields :: ps => if n != ctorVal.name then pure none else pure $ some { alt with patterns := fields ++ ps } | Pattern.inaccessible _ :: _ => processInaccessibleAsCtor alt ctorVal.name | p :: _ => throwError "failed to compile pattern matching, inaccessible pattern or constructor expected{indentD p.toMessageData}" | _ => unreachable! let xFields := xArgs.extract ctorVal.numParams xArgs.size pure { p with alts := alts, vars := xFields.toList ++ xs } | none => let alts ← p.alts.filterMapM fun alt => match alt.patterns with | Pattern.inaccessible e :: ps => do if (← isDefEq x e) then pure $ some { alt with patterns := ps } else pure none | p :: _ => throwError "failed to compile pattern matching, unexpected pattern{indentD p.toMessageData}\ndiscriminant{indentExpr x}" | _ => unreachable! pure { p with alts := alts, vars := xs } private def collectValues (p : Problem) : Array Expr := p.alts.foldl (init := #[]) fun values alt => match alt.patterns with | Pattern.val v :: _ => if values.contains v then values else values.push v | _ => values private def isFirstPatternVar (alt : Alt) : Bool := match alt.patterns with | Pattern.var _ :: _ => true | _ => false private def processValue (p : Problem) : MetaM (Array Problem) := do trace[Meta.Match.match] "value step" match p.vars with | [] => unreachable! | x :: xs => do let values := collectValues p let subgoals ← caseValues p.mvarId x.fvarId! values subgoals.mapIdxM fun i subgoal => do if h : i.val < values.size then let value := values.get ⟨i, h⟩ -- (x = value) branch let subst := subgoal.subst let examples := p.examples.map $ Example.replaceFVarId x.fvarId! (Example.val value) let examples := examples.map $ Example.applyFVarSubst subst let newAlts := p.alts.filter fun alt => match alt.patterns with | Pattern.val v :: _ => v == value | Pattern.var _ :: _ => true | _ => false let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst let newAlts := newAlts.map fun alt => match alt.patterns with | Pattern.val _ :: ps => { alt with patterns := ps } | Pattern.var fvarId :: ps => let alt := { alt with patterns := ps } alt.replaceFVarId fvarId value | _ => unreachable! let newVars := xs.map fun x => x.applyFVarSubst subst pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples } else -- else branch for value let newAlts := p.alts.filter isFirstPatternVar pure { p with mvarId := subgoal.mvarId, alts := newAlts, vars := x::xs } private def collectArraySizes (p : Problem) : Array Nat := p.alts.foldl (init := #[]) fun sizes alt => match alt.patterns with | Pattern.arrayLit _ ps :: _ => let sz := ps.length; if sizes.contains sz then sizes else sizes.push sz | _ => sizes private def expandVarIntoArrayLit (alt : Alt) (fvarId : FVarId) (arrayElemType : Expr) (arraySize : Nat) : MetaM Alt := withExistingLocalDecls alt.fvarDecls do let fvarDecl ← getLocalDecl fvarId let varNamePrefix := fvarDecl.userName let rec loop | n+1, newVars => withLocalDeclD (varNamePrefix.appendIndexAfter (n+1)) arrayElemType fun x => loop n (newVars.push x) | 0, newVars => do let arrayLit ← mkArrayLit arrayElemType newVars.toList let alt := alt.replaceFVarId fvarId arrayLit let newDecls ← newVars.toList.mapM fun newVar => getLocalDecl newVar.fvarId! let newPatterns := newVars.toList.map fun newVar => Pattern.var newVar.fvarId! pure { alt with fvarDecls := newDecls ++ alt.fvarDecls, patterns := newPatterns ++ alt.patterns } loop arraySize #[] private def processArrayLit (p : Problem) : MetaM (Array Problem) := do trace[Meta.Match.match] "array literal step" match p.vars with | [] => unreachable! | x :: xs => do let sizes := collectArraySizes p let subgoals ← caseArraySizes p.mvarId x.fvarId! sizes subgoals.mapIdxM fun i subgoal => do if h : i.val < sizes.size then let size := sizes.get! i let subst := subgoal.subst let elems := subgoal.elems.toList let newVars := elems.map mkFVar ++ xs let newVars := newVars.map fun x => x.applyFVarSubst subst let subex := Example.arrayLit <| elems.map Example.var let examples := p.examples.map <| Example.replaceFVarId x.fvarId! subex let examples := examples.map <| Example.applyFVarSubst subst let newAlts := p.alts.filter fun alt => match alt.patterns with | Pattern.arrayLit _ ps :: _ => ps.length == size | Pattern.var _ :: _ => true | _ => false let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst let newAlts ← newAlts.mapM fun alt => match alt.patterns with | Pattern.arrayLit _ pats :: ps => pure { alt with patterns := pats ++ ps } | Pattern.var fvarId :: ps => do let α ← getArrayArgType <| subst.apply x expandVarIntoArrayLit { alt with patterns := ps } fvarId α size | _ => unreachable! pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples } else do -- else branch let newAlts := p.alts.filter isFirstPatternVar pure { p with mvarId := subgoal.mvarId, alts := newAlts, vars := x::xs } private def expandNatValuePattern (p : Problem) : Problem := do let alts := p.alts.map fun alt => match alt.patterns with | Pattern.val (Expr.lit (Literal.natVal 0) _) :: ps => { alt with patterns := Pattern.ctor `Nat.zero [] [] [] :: ps } | Pattern.val (Expr.lit (Literal.natVal (n+1)) _) :: ps => { alt with patterns := Pattern.ctor `Nat.succ [] [] [Pattern.val (mkRawNatLit n)] :: ps } | _ => alt { p with alts := alts } private def traceStep (msg : String) : StateRefT State MetaM Unit := do trace[Meta.Match.match] "{msg} step" private def traceState (p : Problem) : MetaM Unit := withGoalOf p (traceM `Meta.Match.match p.toMessageData) private def throwNonSupported (p : Problem) : MetaM Unit := withGoalOf p do let msg ← p.toMessageData throwError "failed to compile pattern matching, stuck at{indentD msg}" def isCurrVarInductive (p : Problem) : MetaM Bool := do match p.vars with | [] => pure false | x::_ => withGoalOf p do let val? ← getInductiveVal? x pure val?.isSome private def checkNextPatternTypes (p : Problem) : MetaM Unit := do match p.vars with | [] => return () | x::_ => withGoalOf p do for alt in p.alts do withRef alt.ref do match alt.patterns with | [] => pure () | p::_ => let e ← p.toExpr let xType ← inferType x let eType ← inferType e unless (← isDefEq xType eType) do throwError "pattern{indentExpr e}\n{← mkHasTypeButIsExpectedMsg eType xType}" private def List.moveToFront [Inhabited α] (as : List α) (i : Nat) : List α := let rec loop : (as : List α) → (i : Nat) → α × List α | [], _ => unreachable! | a::as, 0 => (a, as) | a::as, i+1 => let (b, bs) := loop as i (b, a::bs) let (b, bs) := loop as i b :: bs /-- Move variable `#i` to the beginning of the to-do list `p.vars`. -/ private def moveToFront (p : Problem) (i : Nat) : Problem := do if i == 0 then p else if h : i < p.vars.length then let x := p.vars.get i h return { p with vars := List.moveToFront p.vars i alts := p.alts.map fun alt => { alt with patterns := List.moveToFront alt.patterns i } } else p private partial def process (p : Problem) : StateRefT State MetaM Unit := search 0 where /- If `p.vars` is empty, then we are done. Otherwise, we process `p.vars[0]`. -/ tryToProcess (p : Problem) : StateRefT State MetaM Unit := withIncRecDepth do traceState p let isInductive ← liftM $ isCurrVarInductive p if isDone p then processLeaf p else if hasAsPattern p then traceStep ("as-pattern") let p ← processAsPattern p process p else if isNatValueTransition p then traceStep ("nat value to constructor") process (expandNatValuePattern p) else if !isNextVar p then traceStep ("non variable") let p ← processNonVariable p process p else if isInductive && isConstructorTransition p then let ps ← processConstructor p ps.forM process else if isVariableTransition p then traceStep ("variable") let p ← processVariable p process p else if isValueTransition p then let ps ← processValue p ps.forM process else if isArrayLitTransition p then let ps ← processArrayLit p ps.forM process else if hasNatValPattern p then -- This branch is reachable when `p`, for example, is just values without an else-alternative. -- We added it just to get better error messages. traceStep ("nat value to constructor") process (expandNatValuePattern p) else checkNextPatternTypes p throwNonSupported p /- Return `true` if `type` does not depend on the first `i` elements in `xs` -/ checkVarDeps (xs : List Expr) (i : Nat) (type : Expr) : MetaM Bool := do match i, xs with | 0, _ => return true | _, [] => unreachable! | i+1, x::xs => if x.isFVar then if (← dependsOn type x.fvarId!) then return false checkVarDeps xs i type /-- Auxiliary method for `search`. Find next variable to "try". `i` is the position of the variable s.t. `tryToProcess` failed. We only consider variables that do not depend on other variables at `p.vars`. -/ findNext (i : Nat) : MetaM (Option Nat) := do if h : i < p.vars.length then let x := p.vars.get i h if (← checkVarDeps p.vars i (← inferType x)) then return i else findNext (i+1) else return none /-- Auxiliary method for trying the next variable to process. It moves variable `#i` to the front of the to-do list and invokes `tryToProcess`. Note that for non-dependent elimination, variable `#0` always work. If variable `#i` fails, we use `findNext` to select the next variable to try. Remark: the "missing cases" error is not considered a failure. -/ search (i : Nat) : StateRefT State MetaM Unit := do let s₁ ← getThe Meta.State let s₂ ← get let p' := moveToFront p i try tryToProcess p' catch ex => match (← withGoalOf p <| findNext (i+1)) with | none => throw ex | some j => trace[Meta.Match.match] "failed with #{i}, trying #{j}{indentD ex.toMessageData}" set s₁; set s₂; search j private def getUElimPos? (matcherLevels : List Level) (uElim : Level) : MetaM (Option Nat) := if uElim == levelZero then return none else match matcherLevels.toArray.indexOf? uElim with | none => throwError "dependent match elimination failed, universe level not found" | some pos => return some pos.val /- See comment at `mkMatcher` before `mkAuxDefinition` -/ register_builtin_option bootstrap.genMatcherCode : Bool := { defValue := true group := "bootstrap" descr := "disable code generation for auxiliary matcher function" } builtin_initialize matcherExt : EnvExtension (Std.PHashMap (Expr × Bool) Name) ← registerEnvExtension (pure {}) /- Similar to `mkAuxDefinition`, but uses the cache `matcherExt`. It also returns an Boolean that indicates whether a new matcher function was added to the environment or not. -/ def mkMatcherAuxDefinition (name : Name) (type : Expr) (value : Expr) : MetaM (Expr × (Option $ MatcherInfo → MetaM Unit)) := do trace[Meta.debug] "{name} : {type} := {value}" let compile := bootstrap.genMatcherCode.get (← getOptions) let result ← Closure.mkValueTypeClosure type value (zeta := false) let env ← getEnv let mkMatcherConst name := mkAppN (mkConst name result.levelArgs.toList) result.exprArgs match (matcherExt.getState env).find? (result.value, compile) with | some nameNew => (mkMatcherConst nameNew, none) | none => let decl := Declaration.defnDecl { name := name, levelParams := result.levelParams.toList, type := result.type, value := result.value, hints := ReducibilityHints.regular (getMaxHeight env result.value + 1), safety := if env.hasUnsafe result.type || env.hasUnsafe result.value then DefinitionSafety.unsafe else DefinitionSafety.safe } trace[Meta.debug] "{name} : {result.type} := {result.value}" let addMatcher : MatcherInfo → MetaM Unit := fun mi => do addDecl decl if compile then compileDecl decl modifyEnv fun env => matcherExt.modifyState env fun s => s.insert (result.value, compile) name addMatcherInfo name mi setInlineAttribute name (mkMatcherConst name, some addMatcher) structure MkMatcherInput where matcherName : Name matchType : Expr numDiscrs : Nat lhss : List Match.AltLHS /- Create a dependent matcher for `matchType` where `matchType` is of the form `(a_1 : A_1) -> (a_2 : A_2[a_1]) -> ... -> (a_n : A_n[a_1, a_2, ... a_{n-1}]) -> B[a_1, ..., a_n]` where `n = numDiscrs`, and the `lhss` are the left-hand-sides of the `match`-expression alternatives. Each `AltLHS` has a list of local declarations and a list of patterns. The number of patterns must be the same in each `AltLHS`. The generated matcher has the structure described at `MatcherInfo`. The motive argument is of the form `(motive : (a_1 : A_1) -> (a_2 : A_2[a_1]) -> ... -> (a_n : A_n[a_1, a_2, ... a_{n-1}]) -> Sort v)` where `v` is a universe parameter or 0 if `B[a_1, ..., a_n]` is a proposition. -/ def mkMatcher (input : MkMatcherInput) : MetaM MatcherResult := let ⟨matcherName, matchType, numDiscrs, lhss⟩ := input forallBoundedTelescope matchType numDiscrs fun majors matchTypeBody => do checkNumPatterns majors lhss /- We generate an matcher that can eliminate using different motives with different universe levels. `uElim` is the universe level the caller wants to eliminate to. If it is not levelZero, we create a matcher that can eliminate in any universe level. This is useful for implementing `MatcherApp.addArg` because it may have to change the universe level. -/ let uElim ← getLevel matchTypeBody let uElimGen ← if uElim == levelZero then pure levelZero else mkFreshLevelMVar let motiveType ← mkForallFVars majors (mkSort uElimGen) withLocalDeclD `motive motiveType fun motive => do trace[Meta.Match.debug] "motiveType: {motiveType}" let mvarType := mkAppN motive majors trace[Meta.Match.debug] "target: {mvarType}" withAlts motive lhss fun alts minors => do let mvar ← mkFreshExprMVar mvarType let examples := majors.toList.map fun major => Example.var major.fvarId! let (_, s) ← (process { mvarId := mvar.mvarId!, vars := majors.toList, alts := alts, examples := examples }).run {} let args := #[motive] ++ majors ++ minors.map Prod.fst let type ← mkForallFVars args mvarType let val ← mkLambdaFVars args mvar trace[Meta.Match.debug] "matcher value: {val}\ntype: {type}" /- The option `bootstrap.gen_matcher_code` is a helper hack. It is useful, for example, for compiling `src/Init/Data/Int`. It is needed because the compiler uses `Int.decLt` for generating code for `Int.casesOn` applications, but `Int.casesOn` is used to give the reference implementation for ``` @[extern "lean_int_neg"] def neg (n : @& Int) : Int := match n with | ofNat n => negOfNat n | negSucc n => succ n ``` which is defined **before** `Int.decLt` -/ let (matcher, addMatcher) ← mkMatcherAuxDefinition matcherName type val trace[Meta.Match.debug] "matcher levels: {matcher.getAppFn.constLevels!}, uElim: {uElimGen}" let uElimPos? ← getUElimPos? matcher.getAppFn.constLevels! uElimGen discard <| isLevelDefEq uElimGen uElim let addMatcher := match addMatcher with | some addMatcher => { numParams := matcher.getAppNumArgs numDiscrs, altNumParams := minors.map Prod.snd, uElimPos? } |> addMatcher | none => () trace[Meta.Match.debug] "matcher: {matcher}" let unusedAltIdxs := lhss.length.fold (init := []) fun i r => if s.used.contains i then r else i::r pure { matcher, counterExamples := s.counterExamples, unusedAltIdxs := unusedAltIdxs.reverse, addMatcher } def getMkMatcherInputInContext (matcherApp : MatcherApp) : MetaM MkMatcherInput := do let matcherName := matcherApp.matcherName let some matcherInfo ← getMatcherInfo? matcherName | throwError "not a matcher: {matcherName}" let matcherConst ← getConstInfo matcherName let matcherType ← instantiateForall matcherConst.type $ matcherApp.params ++ #[matcherApp.motive] let matchType ← do let u := if let some idx := matcherInfo.uElimPos? then mkLevelParam matcherConst.levelParams.toArray[idx] else levelZero forallBoundedTelescope matcherType (some matcherInfo.numDiscrs) fun discrs t => do mkForallFVars discrs (mkConst ``PUnit [u]) let matcherType ← instantiateForall matcherType matcherApp.discrs let lhss := Array.toList $ ←forallBoundedTelescope matcherType (some matcherApp.alts.size) fun alts _ => alts.mapM fun alt => do let ty ← inferType alt forallTelescope ty fun xs body => do let xs ← xs.filterM fun x => dependsOn body x.fvarId! body.withApp fun f args => do let ctx ← getLCtx let localDecls := xs.map ctx.getFVar! let patterns ← args.mapM Match.toPattern return { ref := Syntax.missing fvarDecls := localDecls.toList patterns := patterns.toList : Match.AltLHS } return { matcherName, matchType, numDiscrs := matcherApp.discrs.size, lhss } def withMkMatcherInput (matcherName : Name) (k : MkMatcherInput → MetaM α) : MetaM α := do let some matcherInfo ← getMatcherInfo? matcherName | throwError "not a matcher: {matcherName}" let matcherConst ← getConstInfo matcherName forallBoundedTelescope matcherConst.type (some matcherInfo.arity) fun xs t => do let matcherApp ← mkConstWithLevelParams matcherConst.name let matcherApp := mkAppN matcherApp xs let some matcherApp ← matchMatcherApp? matcherApp | throwError "not a matcher app: {matcherApp}" let mkMatcherInput ← getMkMatcherInputInContext matcherApp k mkMatcherInput end Match /- Auxiliary function for MatcherApp.addArg -/ private partial def updateAlts (typeNew : Expr) (altNumParams : Array Nat) (alts : Array Expr) (i : Nat) : MetaM (Array Nat × Array Expr) := do if h : i < alts.size then let alt := alts.get ⟨i, h⟩ let numParams := altNumParams[i] let typeNew ← whnfD typeNew match typeNew with | Expr.forallE n d b _ => let alt ← forallBoundedTelescope d (some numParams) fun xs d => do let alt ← try instantiateLambda alt xs catch _ => throwError "unexpected matcher application, insufficient number of parameters in alternative" forallBoundedTelescope d (some 1) fun x d => do let alt ← mkLambdaFVars x alt -- x is the new argument we are adding to the alternative let alt ← mkLambdaFVars xs alt pure alt updateAlts (b.instantiate1 alt) (altNumParams.set! i (numParams+1)) (alts.set ⟨i, h⟩ alt) (i+1) | _ => throwError "unexpected type at MatcherApp.addArg" else pure (altNumParams, alts) /- Given - matcherApp `match_i As (fun xs => motive[xs]) discrs (fun ys_1 => (alt_1 : motive (C_1[ys_1])) ... (fun ys_n => (alt_n : motive (C_n[ys_n]) remaining`, and - expression `e : B[discrs]`, Construct the term `match_i As (fun xs => B[xs] -> motive[xs]) discrs (fun ys_1 (y : B[C_1[ys_1]]) => alt_1) ... (fun ys_n (y : B[C_n[ys_n]]) => alt_n) e remaining`, and We use `kabstract` to abstract the discriminants from `B[discrs]`. This method assumes - the `matcherApp.motive` is a lambda abstraction where `xs.size == discrs.size` - each alternative is a lambda abstraction where `ys_i.size == matcherApp.altNumParams[i]` -/ def MatcherApp.addArg (matcherApp : MatcherApp) (e : Expr) : MetaM MatcherApp := lambdaTelescope matcherApp.motive fun motiveArgs motiveBody => do unless motiveArgs.size == matcherApp.discrs.size do -- This error can only happen if someone implemented a transformation that rewrites the motive created by `mkMatcher`. throwError "unexpected matcher application, motive must be lambda expression with #{matcherApp.discrs.size} arguments" let eType ← inferType e let eTypeAbst ← matcherApp.discrs.size.foldRevM (init := eType) fun i eTypeAbst => do let motiveArg := motiveArgs[i] let discr := matcherApp.discrs[i] let eTypeAbst ← kabstract eTypeAbst discr pure $ eTypeAbst.instantiate1 motiveArg let motiveBody ← mkArrow eTypeAbst motiveBody let matcherLevels ← match matcherApp.uElimPos? with | none => pure matcherApp.matcherLevels | some pos => let uElim ← getLevel motiveBody pure $ matcherApp.matcherLevels.set! pos uElim let motive ← mkLambdaFVars motiveArgs motiveBody -- Construct `aux` `match_i As (fun xs => B[xs] → motive[xs]) discrs`, and infer its type `auxType`. -- We use `auxType` to infer the type `B[C_i[ys_i]]` of the new argument in each alternative. let aux := mkAppN (mkConst matcherApp.matcherName matcherLevels.toList) matcherApp.params let aux := mkApp aux motive let aux := mkAppN aux matcherApp.discrs check aux unless (← isTypeCorrect aux) do throwError "failed to add argument to matcher application, type error when constructing the new motive" let auxType ← inferType aux let (altNumParams, alts) ← updateAlts auxType matcherApp.altNumParams matcherApp.alts 0 pure { matcherApp with matcherLevels := matcherLevels, motive := motive, alts := alts, altNumParams := altNumParams, remaining := #[e] ++ matcherApp.remaining } builtin_initialize registerTraceClass `Meta.Match.match registerTraceClass `Meta.Match.debug registerTraceClass `Meta.Match.unify end Lean.Meta
5b226cad552d0a51b85e8e50f194d78ee0b65560
f00cc9c04d77f9621aa57d1406d35c522c3ff82c
/library/init/core.lean
ad2c2698b95fbe4b1c4d52f3934ac8b7e944423b
[ "Apache-2.0" ]
permissive
shonfeder/lean
444c66a74676d74fb3ef682d88cd0f5c1bf928a5
24d5a1592d80cefe86552d96410c51bb07e6d411
refs/heads/master
1,619,338,440,905
1,512,842,340,000
1,512,842,340,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,331
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura notation, basic datatypes and type classes -/ prelude notation `Prop` := Sort 0 notation f ` $ `:1 a:0 := f a /- Logical operations and relations -/ reserve prefix `¬`:40 reserve prefix `~`:40 reserve infixr ` ∧ `:35 reserve infixr ` /\ `:35 reserve infixr ` \/ `:30 reserve infixr ` ∨ `:30 reserve infix ` <-> `:20 reserve infix ` ↔ `:20 reserve infix ` = `:50 reserve infix ` == `:50 reserve infix ` ≠ `:50 reserve infix ` ≈ `:50 reserve infix ` ~ `:50 reserve infix ` ≡ `:50 reserve infixl ` ⬝ `:75 reserve infixr ` ▸ `:75 reserve infixr ` ▹ `:75 /- types and type constructors -/ reserve infixr ` ⊕ `:30 reserve infixr ` × `:35 /- arithmetic operations -/ reserve infixl ` + `:65 reserve infixl ` - `:65 reserve infixl ` * `:70 reserve infixl ` / `:70 reserve infixl ` % `:70 reserve prefix `-`:100 reserve infix ` ^ `:80 reserve infixr ` ∘ `:90 -- input with \comp reserve infix ` <= `:50 reserve infix ` ≤ `:50 reserve infix ` < `:50 reserve infix ` >= `:50 reserve infix ` ≥ `:50 reserve infix ` > `:50 /- boolean operations -/ reserve infixl ` && `:70 reserve infixl ` || `:65 /- set operations -/ reserve infix ` ∈ `:50 reserve infix ` ∉ `:50 reserve infixl ` ∩ `:70 reserve infixl ` ∪ `:65 reserve infix ` ⊆ `:50 reserve infix ` ⊇ `:50 reserve infix ` ⊂ `:50 reserve infix ` ⊃ `:50 reserve infix ` \ `:70 /- other symbols -/ reserve infix ` ∣ `:50 reserve infixl ` ++ `:65 reserve infixr ` :: `:67 reserve infixl `; `:1 universes u v w /- The kernel definitional equality test (t =?= s) has special support for id_delta applications. It implements the following rules 1) (id_delta t) =?= t 2) t =?= (id_delta t) 3) (id_delta t) =?= s IF (unfold_of t) =?= s 4) t =?= id_delta s IF t =?= (unfold_of s) This is mechanism for controlling the delta reduction (aka unfolding) used in the kernel. We use id_delta applications to address performance problems when type checking lemmas generated by the equation compiler. -/ @[inline] def id_delta {α : Sort u} (a : α) : α := a /-- Gadget for optional parameter support. -/ @[reducible] def opt_param (α : Sort u) (default : α) : Sort u := α /-- Gadget for marking output parameters in type classes. -/ @[reducible] def inout_param (α : Sort u) : Sort u := α notation `inout`:1024 a:0 := inout_param a inductive punit : Sort u | star : punit inductive unit : Type | star : unit /-- Gadget for defining thunks, thunk parameters have special treatment. Example: given def f (s : string) (t : thunk nat) : nat an application f "hello" 10 is converted into f "hello" (λ _, 10) -/ @[reducible] def thunk (α : Type u) : Type u := unit → α inductive true : Prop | intro : true inductive false : Prop inductive empty : Type def not (a : Prop) := a → false prefix `¬` := not inductive eq {α : Sort u} (a : α) : α → Prop | refl : eq a /- Initialize the quotient module, which effectively adds the following definitions: constant quot {α : Sort u} (r : α → α → Prop) : Sort u constant quot.mk {α : Sort u} (r : α → α → Prop) (a : α) : quot r constant quot.lift {α : Sort u} {r : α → α → Prop} {β : Sort v} (f : α → β) : (∀ a b : α, r a b → eq (f a) (f b)) → quot r → β constant quot.ind {α : Sort u} {r : α → α → Prop} {β : quot r → Prop} : (∀ a : α, β (quot.mk r a)) → ∀ q : quot r, β q -/ init_quotient inductive heq {α : Sort u} (a : α) : Π {β : Sort u}, β → Prop | refl : heq a structure prod (α : Type u) (β : Type v) := (fst : α) (snd : β) /-- Similar to `prod`, but α and β can be propositions. We use this type internally to automatically generate the brec_on recursor. -/ structure pprod (α : Sort u) (β : Sort v) := (fst : α) (snd : β) structure and (a b : Prop) : Prop := intro :: (left : a) (right : b) def and.elim_left {a b : Prop} (h : and a b) : a := h.1 def and.elim_right {a b : Prop} (h : and a b) : b := h.2 /- eq basic support -/ infix = := eq attribute [refl] eq.refl @[pattern] def rfl {α : Sort u} {a : α} : a = a := eq.refl a @[elab_as_eliminator, subst] lemma eq.subst {α : Sort u} {P : α → Prop} {a b : α} (h₁ : a = b) (h₂ : P a) : P b := eq.rec h₂ h₁ notation h1 ▸ h2 := eq.subst h1 h2 @[trans] lemma eq.trans {α : Sort u} {a b c : α} (h₁ : a = b) (h₂ : b = c) : a = c := h₂ ▸ h₁ @[symm] lemma eq.symm {α : Sort u} {a b : α} (h : a = b) : b = a := h ▸ rfl infix == := heq @[pattern] def heq.rfl {α : Sort u} {a : α} : a == a := heq.refl a lemma eq_of_heq {α : Sort u} {a a' : α} (h : a == a') : a = a' := have ∀ (α' : Sort u) (a' : α') (h₁ : @heq α a α' a') (h₂ : α = α'), (eq.rec_on h₂ a : α') = a', from λ (α' : Sort u) (a' : α') (h₁ : @heq α a α' a'), heq.rec_on h₁ (λ h₂ : α = α, rfl), show (eq.rec_on (eq.refl α) a : α) = a', from this α a' h (eq.refl α) /- The following four lemmas could not be automatically generated when the structures were declared, so we prove them manually here. -/ lemma prod.mk.inj {α : Type u} {β : Type v} {x₁ : α} {y₁ : β} {x₂ : α} {y₂ : β} : (x₁, y₁) = (x₂, y₂) → and (x₁ = x₂) (y₁ = y₂) := λ h, prod.no_confusion h (λ h₁ h₂, ⟨h₁, h₂⟩) lemma prod.mk.inj_arrow {α : Type u} {β : Type v} {x₁ : α} {y₁ : β} {x₂ : α} {y₂ : β} : (x₁, y₁) = (x₂, y₂) → Π ⦃P : Sort w⦄, (x₁ = x₂ → y₁ = y₂ → P) → P := λ h₁ _ h₂, prod.no_confusion h₁ h₂ lemma pprod.mk.inj {α : Sort u} {β : Sort v} {x₁ : α} {y₁ : β} {x₂ : α} {y₂ : β} : pprod.mk x₁ y₁ = pprod.mk x₂ y₂ → and (x₁ = x₂) (y₁ = y₂) := λ h, pprod.no_confusion h (λ h₁ h₂, ⟨h₁, h₂⟩) lemma pprod.mk.inj_arrow {α : Type u} {β : Type v} {x₁ : α} {y₁ : β} {x₂ : α} {y₂ : β} : (x₁, y₁) = (x₂, y₂) → Π ⦃P : Sort w⦄, (x₁ = x₂ → y₁ = y₂ → P) → P := λ h₁ _ h₂, prod.no_confusion h₁ h₂ inductive sum (α : Type u) (β : Type v) | inl {} (val : α) : sum | inr {} (val : β) : sum inductive psum (α : Sort u) (β : Sort v) | inl {} (val : α) : psum | inr {} (val : β) : psum inductive or (a b : Prop) : Prop | inl {} (h : a) : or | inr {} (h : b) : or def or.intro_left {a : Prop} (b : Prop) (ha : a) : or a b := or.inl ha def or.intro_right (a : Prop) {b : Prop} (hb : b) : or a b := or.inr hb structure sigma {α : Type u} (β : α → Type v) := mk :: (fst : α) (snd : β fst) structure psigma {α : Sort u} (β : α → Sort v) := mk :: (fst : α) (snd : β fst) inductive bool : Type | ff : bool | tt : bool /- Remark: subtype must take a Sort instead of Type because of the axiom strong_indefinite_description. -/ structure subtype {α : Sort u} (p : α → Prop) := (val : α) (property : p val) attribute [pp_using_anonymous_constructor] sigma psigma subtype pprod and class inductive decidable (p : Prop) | is_false (h : ¬p) : decidable | is_true (h : p) : decidable @[reducible] def decidable_pred {α : Sort u} (r : α → Prop) := Π (a : α), decidable (r a) @[reducible] def decidable_rel {α : Sort u} (r : α → α → Prop) := Π (a b : α), decidable (r a b) @[reducible] def decidable_eq (α : Sort u) := decidable_rel (@eq α) inductive option (α : Type u) | none {} : option | some (val : α) : option export option (none some) export bool (ff tt) inductive list (T : Type u) | nil {} : list | cons (hd : T) (tl : list) : list notation h :: t := list.cons h t notation `[` l:(foldr `, ` (h t, list.cons h t) list.nil `]`) := l inductive nat | zero : nat | succ (n : nat) : nat structure unification_constraint := {α : Type u} (lhs : α) (rhs : α) infix ` ≟ `:50 := unification_constraint.mk infix ` =?= `:50 := unification_constraint.mk structure unification_hint := (pattern : unification_constraint) (constraints : list unification_constraint) /- Declare builtin and reserved notation -/ class has_zero (α : Type u) := (zero : α) class has_one (α : Type u) := (one : α) class has_add (α : Type u) := (add : α → α → α) class has_mul (α : Type u) := (mul : α → α → α) class has_inv (α : Type u) := (inv : α → α) class has_neg (α : Type u) := (neg : α → α) class has_sub (α : Type u) := (sub : α → α → α) class has_div (α : Type u) := (div : α → α → α) class has_dvd (α : Type u) := (dvd : α → α → Prop) class has_mod (α : Type u) := (mod : α → α → α) class has_le (α : Type u) := (le : α → α → Prop) class has_lt (α : Type u) := (lt : α → α → Prop) class has_append (α : Type u) := (append : α → α → α) class has_andthen (α : Type u) (β : Type v) (σ : inout Type w) := (andthen : α → β → σ) class has_union (α : Type u) := (union : α → α → α) class has_inter (α : Type u) := (inter : α → α → α) class has_sdiff (α : Type u) := (sdiff : α → α → α) class has_equiv (α : Sort u) := (equiv : α → α → Prop) class has_subset (α : Type u) := (subset : α → α → Prop) class has_ssubset (α : Type u) := (ssubset : α → α → Prop) /- Type classes has_emptyc and has_insert are used to implement polymorphic notation for collections. Example: {a, b, c}. -/ class has_emptyc (α : Type u) := (emptyc : α) class has_insert (α : inout Type u) (γ : Type v) := (insert : α → γ → γ) /- Type class used to implement the notation { a ∈ c | p a } -/ class has_sep (α : inout Type u) (γ : Type v) := (sep : (α → Prop) → γ → γ) /- Type class for set-like membership -/ class has_mem (α : inout Type u) (γ : Type v) := (mem : α → γ → Prop) def andthen {α : Type u} {β : Type v} {σ : Type w} [has_andthen α β σ] : α → β → σ := has_andthen.andthen σ infix ∈ := has_mem.mem notation a ∉ s := ¬ has_mem.mem a s infix + := has_add.add infix * := has_mul.mul infix - := has_sub.sub infix / := has_div.div infix ∣ := has_dvd.dvd infix % := has_mod.mod prefix - := has_neg.neg infix <= := has_le.le infix ≤ := has_le.le infix < := has_lt.lt infix ++ := has_append.append infix ; := andthen notation `∅` := has_emptyc.emptyc _ infix ∪ := has_union.union infix ∩ := has_inter.inter infix ⊆ := has_subset.subset infix ⊂ := has_ssubset.ssubset infix \ := has_sdiff.sdiff infix ≈ := has_equiv.equiv export has_append (append) @[reducible] def ge {α : Type u} [has_le α] (a b : α) : Prop := has_le.le b a @[reducible] def gt {α : Type u} [has_lt α] (a b : α) : Prop := has_lt.lt b a infix >= := ge infix ≥ := ge infix > := gt @[reducible] def superset {α : Type u} [has_subset α] (a b : α) : Prop := has_subset.subset b a @[reducible] def ssuperset {α : Type u} [has_ssubset α] (a b : α) : Prop := has_ssubset.ssubset b a infix ⊇ := superset infix ⊃ := ssuperset def bit0 {α : Type u} [s : has_add α] (a : α) : α := a + a def bit1 {α : Type u} [s₁ : has_one α] [s₂ : has_add α] (a : α) : α := (bit0 a) + 1 attribute [pattern] has_zero.zero has_one.one bit0 bit1 has_add.add has_neg.neg def insert {α : Type u} {γ : Type v} [has_insert α γ] : α → γ → γ := has_insert.insert /- The empty collection -/ def singleton {α : Type u} {γ : Type v} [has_emptyc γ] [has_insert α γ] (a : α) : γ := has_insert.insert a ∅ /- nat basic instances -/ namespace nat protected def add : nat → nat → nat | a zero := a | a (succ b) := succ (add a b) /- We mark the following definitions as pattern to make sure they can be used in recursive equations, and reduced by the equation compiler. -/ attribute [pattern] nat.add nat.add._main end nat instance : has_zero nat := ⟨nat.zero⟩ instance : has_one nat := ⟨nat.succ (nat.zero)⟩ instance : has_add nat := ⟨nat.add⟩ def std.priority.default : nat := 1000 def std.priority.max : nat := 0xFFFFFFFF namespace nat protected def prio := std.priority.default + 100 end nat /- Global declarations of right binding strength If a module reassigns these, it will be incompatible with other modules that adhere to these conventions. When hovering over a symbol, use "C-c C-k" to see how to input it. -/ def std.prec.max : nat := 1024 -- the strength of application, identifiers, (, [, etc. def std.prec.arrow : nat := 25 /- The next def is "max + 10". It can be used e.g. for postfix operations that should be stronger than application. -/ def std.prec.max_plus : nat := std.prec.max + 10 reserve postfix `⁻¹`:std.prec.max_plus -- input with \sy or \-1 or \inv postfix ⁻¹ := has_inv.inv notation α × β := prod α β -- notation for n-ary tuples /- sizeof -/ class has_sizeof (α : Sort u) := (sizeof : α → nat) def sizeof {α : Sort u} [s : has_sizeof α] : α → nat := has_sizeof.sizeof /- Declare sizeof instances and lemmas for types declared before has_sizeof. From now on, the inductive compiler will automatically generate sizeof instances and lemmas. -/ /- Every type `α` has a default has_sizeof instance that just returns 0 for every element of `α` -/ protected def default.sizeof (α : Sort u) : α → nat | a := 0 instance default_has_sizeof (α : Sort u) : has_sizeof α := ⟨default.sizeof α⟩ protected def nat.sizeof : nat → nat | n := n instance : has_sizeof nat := ⟨nat.sizeof⟩ protected def prod.sizeof {α : Type u} {β : Type v} [has_sizeof α] [has_sizeof β] : (prod α β) → nat | ⟨a, b⟩ := 1 + sizeof a + sizeof b instance (α : Type u) (β : Type v) [has_sizeof α] [has_sizeof β] : has_sizeof (prod α β) := ⟨prod.sizeof⟩ protected def sum.sizeof {α : Type u} {β : Type v} [has_sizeof α] [has_sizeof β] : (sum α β) → nat | (sum.inl a) := 1 + sizeof a | (sum.inr b) := 1 + sizeof b instance (α : Type u) (β : Type v) [has_sizeof α] [has_sizeof β] : has_sizeof (sum α β) := ⟨sum.sizeof⟩ protected def psum.sizeof {α : Type u} {β : Type v} [has_sizeof α] [has_sizeof β] : (psum α β) → nat | (psum.inl a) := 1 + sizeof a | (psum.inr b) := 1 + sizeof b instance (α : Type u) (β : Type v) [has_sizeof α] [has_sizeof β] : has_sizeof (psum α β) := ⟨psum.sizeof⟩ protected def sigma.sizeof {α : Type u} {β : α → Type v} [has_sizeof α] [∀ a, has_sizeof (β a)] : sigma β → nat | ⟨a, b⟩ := 1 + sizeof a + sizeof b instance (α : Type u) (β : α → Type v) [has_sizeof α] [∀ a, has_sizeof (β a)] : has_sizeof (sigma β) := ⟨sigma.sizeof⟩ protected def psigma.sizeof {α : Type u} {β : α → Type v} [has_sizeof α] [∀ a, has_sizeof (β a)] : psigma β → nat | ⟨a, b⟩ := 1 + sizeof a + sizeof b instance (α : Type u) (β : α → Type v) [has_sizeof α] [∀ a, has_sizeof (β a)] : has_sizeof (psigma β) := ⟨psigma.sizeof⟩ protected def unit.sizeof : unit → nat | u := 1 instance : has_sizeof unit := ⟨unit.sizeof⟩ protected def punit.sizeof : punit → nat | u := 1 instance : has_sizeof punit := ⟨punit.sizeof⟩ protected def bool.sizeof : bool → nat | b := 1 instance : has_sizeof bool := ⟨bool.sizeof⟩ protected def option.sizeof {α : Type u} [has_sizeof α] : option α → nat | none := 1 | (some a) := 1 + sizeof a instance (α : Type u) [has_sizeof α] : has_sizeof (option α) := ⟨option.sizeof⟩ protected def list.sizeof {α : Type u} [has_sizeof α] : list α → nat | list.nil := 1 | (list.cons a l) := 1 + sizeof a + list.sizeof l instance (α : Type u) [has_sizeof α] : has_sizeof (list α) := ⟨list.sizeof⟩ protected def subtype.sizeof {α : Type u} [has_sizeof α] {p : α → Prop} : subtype p → nat | ⟨a, _⟩ := sizeof a instance {α : Type u} [has_sizeof α] (p : α → Prop) : has_sizeof (subtype p) := ⟨subtype.sizeof⟩ lemma nat_add_zero (n : nat) : n + 0 = n := rfl /- Combinator calculus -/ namespace combinator universes u₁ u₂ u₃ def I {α : Type u₁} (a : α) := a def K {α : Type u₁} {β : Type u₂} (a : α) (b : β) := a def S {α : Type u₁} {β : Type u₂} {γ : Type u₃} (x : α → β → γ) (y : α → β) (z : α) := x z (y z) end combinator /-- Auxiliary datatype for #[ ... ] notation. #[1, 2, 3, 4] is notation for bin_tree.node (bin_tree.node (bin_tree.leaf 1) (bin_tree.leaf 2)) (bin_tree.node (bin_tree.leaf 3) (bin_tree.leaf 4)) We use this notation to input long sequences without exhausting the system stack space. Later, we define a coercion from `bin_tree` into `list`. -/ inductive bin_tree (α : Type u) | empty {} : bin_tree | leaf (val : α) : bin_tree | node (left right : bin_tree) : bin_tree attribute [elab_simple] bin_tree.node bin_tree.leaf /- Basic unification hints -/ @[unify] def add_succ_defeq_succ_add_hint (x y z : nat) : unification_hint := { pattern := x + nat.succ y ≟ nat.succ z, constraints := [z ≟ x + y] }
e951dc82a95e5826e21a98d99332716d80a5fd62
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/matrix/nondegenerate.lean
9c76a186f06802fe6e3f6871cefda3224641ac53
[ "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,646
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 data.matrix.basic import linear_algebra.matrix.determinant import linear_algebra.matrix.adjugate /-! # Matrices associated with non-degenerate bilinear forms > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions * `matrix.nondegenerate A`: the proposition that when interpreted as a bilinear form, the matrix `A` is nondegenerate. -/ namespace matrix variables {m R A : Type*} [fintype m] [comm_ring R] /-- A matrix `M` is nondegenerate if for all `v ≠ 0`, there is a `w ≠ 0` with `w ⬝ M ⬝ v ≠ 0`. -/ def nondegenerate (M : matrix m m R) := ∀ v, (∀ w, matrix.dot_product v (mul_vec M w) = 0) → v = 0 /-- If `M` is nondegenerate and `w ⬝ M ⬝ v = 0` for all `w`, then `v = 0`. -/ lemma nondegenerate.eq_zero_of_ortho {M : matrix m m R} (hM : nondegenerate M) {v : m → R} (hv : ∀ w, matrix.dot_product v (mul_vec M w) = 0) : v = 0 := hM v hv /-- If `M` is nondegenerate and `v ≠ 0`, then there is some `w` such that `w ⬝ M ⬝ v ≠ 0`. -/ lemma nondegenerate.exists_not_ortho_of_ne_zero {M : matrix m m R} (hM : nondegenerate M) {v : m → R} (hv : v ≠ 0) : ∃ w, matrix.dot_product v (mul_vec M w) ≠ 0 := not_forall.mp (mt hM.eq_zero_of_ortho hv) variables [comm_ring A] [is_domain A] /-- If `M` has a nonzero determinant, then `M` as a bilinear form on `n → A` is nondegenerate. See also `bilin_form.nondegenerate_of_det_ne_zero'` and `bilin_form.nondegenerate_of_det_ne_zero`. -/ theorem nondegenerate_of_det_ne_zero [decidable_eq m] {M : matrix m m A} (hM : M.det ≠ 0) : nondegenerate M := begin intros v hv, ext i, specialize hv (M.cramer (pi.single i 1)), refine (mul_eq_zero.mp _).resolve_right hM, convert hv, simp only [mul_vec_cramer M (pi.single i 1), dot_product, pi.smul_apply, smul_eq_mul], rw [finset.sum_eq_single i, pi.single_eq_same, mul_one], { intros j _ hj, simp [hj] }, { intros, have := finset.mem_univ i, contradiction } end theorem eq_zero_of_vec_mul_eq_zero [decidable_eq m] {M : matrix m m A} (hM : M.det ≠ 0) {v : m → A} (hv : M.vec_mul v = 0) : v = 0 := (nondegenerate_of_det_ne_zero hM).eq_zero_of_ortho (λ w, by rw [dot_product_mul_vec, hv, zero_dot_product]) theorem eq_zero_of_mul_vec_eq_zero [decidable_eq m] {M : matrix m m A} (hM : M.det ≠ 0) {v : m → A} (hv : M.mul_vec v = 0) : v = 0 := eq_zero_of_vec_mul_eq_zero (by rwa det_transpose) ((vec_mul_transpose M v).trans hv) end matrix
ba89882abc718f2e9f7bd0f5f1cfabcd7b817e19
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/complex/schwarz.lean
4e72bd775073aee74d2dbb40626dc9a3e02c3fd0
[ "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
10,032
lean
/- Copyright (c) 2022 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.abs_max import analysis.complex.removable_singularity /-! # Schwarz lemma In this file we prove several versions of the Schwarz lemma. * `complex.norm_deriv_le_div_of_maps_to_ball`, `complex.abs_deriv_le_div_of_maps_to_ball`: if `f : ℂ → E` sends an open disk with center `c` and a positive radius `R₁` to an open ball with center `f c` and radius `R₂`, then the absolute value of the derivative of `f` at `c` is at most the ratio `R₂ / R₁`; * `complex.dist_le_div_mul_dist_of_maps_to_ball`: if `f : ℂ → E` sends an open disk with center `c` and radius `R₁` to an open disk with center `f c` and radius `R₂`, then for any `z` in the former disk we have `dist (f z) (f c) ≤ (R₂ / R₁) * dist z c`; * `complex.abs_deriv_le_one_of_maps_to_ball`: if `f : ℂ → ℂ` sends an open disk of positive radius to itself and the center of this disk to itself, then the absolute value of the derivative of `f` at the center of this disk is at most `1`; * `complex.dist_le_dist_of_maps_to_ball`: if `f : ℂ → ℂ` sends an open disk to itself and the center `c` of this disk to itself, then for any point `z` of this disk we have `dist (f z) c ≤ dist z c`; * `complex.abs_le_abs_of_maps_to_ball`: if `f : ℂ → ℂ` sends an open disk with center `0` to itself, then for any point `z` of this disk we have `abs (f z) ≤ abs z`. ## Implementation notes We prove some versions of the Schwarz lemma for a map `f : ℂ → E` taking values in any normed space over complex numbers. ## TODO * Prove that these inequalities are strict unless `f` is an affine map. * Prove that any diffeomorphism of the unit disk to itself is a Möbius map. ## Tags Schwarz lemma -/ open metric set function filter topological_space open_locale topology namespace complex section space variables {E : Type*} [normed_add_comm_group E] [normed_space ℂ E] {R R₁ R₂ : ℝ} {f : ℂ → E} {c z z₀ : ℂ} /-- An auxiliary lemma for `complex.norm_dslope_le_div_of_maps_to_ball`. -/ lemma schwarz_aux {f : ℂ → ℂ} (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (hz : z ∈ ball c R₁) : ‖dslope f c z‖ ≤ R₂ / R₁ := begin have hR₁ : 0 < R₁, from nonempty_ball.1 ⟨z, hz⟩, suffices : ∀ᶠ r in 𝓝[<] R₁, ‖dslope f c z‖ ≤ R₂ / r, { refine ge_of_tendsto _ this, exact (tendsto_const_nhds.div tendsto_id hR₁.ne').mono_left nhds_within_le_nhds }, rw mem_ball at hz, filter_upwards [Ioo_mem_nhds_within_Iio ⟨hz, le_rfl⟩] with r hr, have hr₀ : 0 < r, from dist_nonneg.trans_lt hr.1, replace hd : diff_cont_on_cl ℂ (dslope f c) (ball c r), { refine differentiable_on.diff_cont_on_cl _, rw closure_ball c hr₀.ne', exact ((differentiable_on_dslope $ ball_mem_nhds _ hR₁).mpr hd).mono (closed_ball_subset_ball hr.2) }, refine norm_le_of_forall_mem_frontier_norm_le bounded_ball hd _ _, { rw frontier_ball c hr₀.ne', intros z hz, have hz' : z ≠ c, from ne_of_mem_sphere hz hr₀.ne', rw [dslope_of_ne _ hz', slope_def_module, norm_smul, norm_inv, mem_sphere_iff_norm.1 hz, ← div_eq_inv_mul, div_le_div_right hr₀, ← dist_eq_norm], exact le_of_lt (h_maps (mem_ball.2 (by { rw mem_sphere.1 hz, exact hr.2 }))) }, { rw [closure_ball c hr₀.ne', mem_closed_ball], exact hr.1.le } end /-- Two cases of the **Schwarz Lemma** (derivative and distance), merged together. -/ lemma norm_dslope_le_div_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (hz : z ∈ ball c R₁) : ‖dslope f c z‖ ≤ R₂ / R₁ := begin have hR₁ : 0 < R₁, from nonempty_ball.1 ⟨z, hz⟩, have hR₂ : 0 < R₂, from nonempty_ball.1 ⟨f z, h_maps hz⟩, cases eq_or_ne (dslope f c z) 0 with hc hc, { rw [hc, norm_zero], exact div_nonneg hR₂.le hR₁.le }, rcases exists_dual_vector ℂ _ hc with ⟨g, hg, hgf⟩, have hg' : ‖g‖₊ = 1, from nnreal.eq hg, have hg₀ : ‖g‖₊ ≠ 0, by simpa only [hg'] using one_ne_zero, calc ‖dslope f c z‖ = ‖dslope (g ∘ f) c z‖ : begin rw [g.dslope_comp, hgf, is_R_or_C.norm_of_real, abs_norm], exact λ _, hd.differentiable_at (ball_mem_nhds _ hR₁) end ... ≤ R₂ / R₁ : begin refine schwarz_aux (g.differentiable.comp_differentiable_on hd) (maps_to.comp _ h_maps) hz, simpa only [hg', nnreal.coe_one, one_mul] using g.lipschitz.maps_to_ball hg₀ (f c) R₂ end end /-- Equality case in the **Schwarz Lemma**: in the setup of `norm_dslope_le_div_of_maps_to_ball`, if `‖dslope f c z₀‖ = R₂ / R₁` holds at a point in the ball then the map `f` is affine. -/ lemma affine_of_maps_to_ball_of_exists_norm_dslope_eq_div [complete_space E] [strict_convex_space ℝ E] (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : set.maps_to f (ball c R₁) (ball (f c) R₂)) (h_z₀ : z₀ ∈ ball c R₁) (h_eq : ‖dslope f c z₀‖ = R₂ / R₁) : set.eq_on f (λ z, f c + (z - c) • dslope f c z₀) (ball c R₁) := begin set g := dslope f c, rintro z hz, by_cases z = c, { simp [h] }, have h_R₁ : 0 < R₁ := nonempty_ball.mp ⟨_, h_z₀⟩, have g_le_div : ∀ z ∈ ball c R₁, ‖g z‖ ≤ R₂ / R₁, from λ z hz, norm_dslope_le_div_of_maps_to_ball hd h_maps hz, have g_max : is_max_on (norm ∘ g) (ball c R₁) z₀, from is_max_on_iff.mpr (λ z hz, by simpa [h_eq] using g_le_div z hz), have g_diff : differentiable_on ℂ g (ball c R₁), from (differentiable_on_dslope (is_open_ball.mem_nhds (mem_ball_self h_R₁))).mpr hd, have : g z = g z₀ := eq_on_of_is_preconnected_of_is_max_on_norm (convex_ball c R₁).is_preconnected is_open_ball g_diff h_z₀ g_max hz, simp [← this] end lemma affine_of_maps_to_ball_of_exists_norm_dslope_eq_div' [complete_space E] [strict_convex_space ℝ E] (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : set.maps_to f (ball c R₁) (ball (f c) R₂)) (h_z₀ : ∃ z₀ ∈ ball c R₁, ‖dslope f c z₀‖ = R₂ / R₁) : ∃ C : E, ‖C‖ = R₂ / R₁ ∧ set.eq_on f (λ z, f c + (z - c) • C) (ball c R₁) := let ⟨z₀, h_z₀, h_eq⟩ := h_z₀ in ⟨dslope f c z₀, h_eq, affine_of_maps_to_ball_of_exists_norm_dslope_eq_div hd h_maps h_z₀ h_eq⟩ /-- The **Schwarz Lemma**: if `f : ℂ → E` sends an open disk with center `c` and a positive radius `R₁` to an open ball with center `f c` and radius `R₂`, then the absolute value of the derivative of `f` at `c` is at most the ratio `R₂ / R₁`. -/ lemma norm_deriv_le_div_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (h₀ : 0 < R₁) : ‖deriv f c‖ ≤ R₂ / R₁ := by simpa only [dslope_same] using norm_dslope_le_div_of_maps_to_ball hd h_maps (mem_ball_self h₀) /-- The **Schwarz Lemma**: if `f : ℂ → E` sends an open disk with center `c` and radius `R₁` to an open ball with center `f c` and radius `R₂`, then for any `z` in the former disk we have `dist (f z) (f c) ≤ (R₂ / R₁) * dist z c`. -/ lemma dist_le_div_mul_dist_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (hz : z ∈ ball c R₁) : dist (f z) (f c) ≤ (R₂ / R₁) * dist z c := begin rcases eq_or_ne z c with rfl|hne, { simp only [dist_self, mul_zero] }, simpa only [dslope_of_ne _ hne, slope_def_module, norm_smul, norm_inv, ← div_eq_inv_mul, ← dist_eq_norm, div_le_iff (dist_pos.2 hne)] using norm_dslope_le_div_of_maps_to_ball hd h_maps hz end end space variables {f : ℂ → ℂ} {c z : ℂ} {R R₁ R₂ : ℝ} /-- The **Schwarz Lemma**: if `f : ℂ → ℂ` sends an open disk with center `c` and a positive radius `R₁` to an open disk with center `f c` and radius `R₂`, then the absolute value of the derivative of `f` at `c` is at most the ratio `R₂ / R₁`. -/ lemma abs_deriv_le_div_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) R₂)) (h₀ : 0 < R₁) : abs (deriv f c) ≤ R₂ / R₁ := norm_deriv_le_div_of_maps_to_ball hd h_maps h₀ /-- The **Schwarz Lemma**: if `f : ℂ → ℂ` sends an open disk of positive radius to itself and the center of this disk to itself, then the absolute value of the derivative of `f` at the center of this disk is at most `1`. -/ lemma abs_deriv_le_one_of_maps_to_ball (hd : differentiable_on ℂ f (ball c R)) (h_maps : maps_to f (ball c R) (ball c R)) (hc : f c = c) (h₀ : 0 < R) : abs (deriv f c) ≤ 1 := (norm_deriv_le_div_of_maps_to_ball hd (by rwa hc) h₀).trans_eq (div_self h₀.ne') /-- The **Schwarz Lemma**: if `f : ℂ → ℂ` sends an open disk to itself and the center `c` of this disk to itself, then for any point `z` of this disk we have `dist (f z) c ≤ dist z c`. -/ lemma dist_le_dist_of_maps_to_ball_self (hd : differentiable_on ℂ f (ball c R)) (h_maps : maps_to f (ball c R) (ball c R)) (hc : f c = c) (hz : z ∈ ball c R) : dist (f z) c ≤ dist z c := have hR : 0 < R, from nonempty_ball.1 ⟨z, hz⟩, by simpa only [hc, div_self hR.ne', one_mul] using dist_le_div_mul_dist_of_maps_to_ball hd (by rwa hc) hz /-- The **Schwarz Lemma**: if `f : ℂ → ℂ` sends an open disk with center `0` to itself, the for any point `z` of this disk we have `abs (f z) ≤ abs z`. -/ lemma abs_le_abs_of_maps_to_ball_self (hd : differentiable_on ℂ f (ball 0 R)) (h_maps : maps_to f (ball 0 R) (ball 0 R)) (h₀ : f 0 = 0) (hz : abs z < R) : abs (f z) ≤ abs z := begin replace hz : z ∈ ball (0 : ℂ) R, from mem_ball_zero_iff.2 hz, simpa only [dist_zero_right] using dist_le_dist_of_maps_to_ball_self hd h_maps h₀ hz end end complex
91dd48af3381cadf5b7131630840af072e1e280a
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/order/iterate.lean
f45b9053df2da37ab817243cf8b46b2ef00389f7
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,383
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 logic.function.iterate import data.nat.basic /-! # Inequalities on iterates In this file we prove some inequalities comparing `f^[n] x` and `g^[n] x` where `f` and `g` are two self-maps that commute with each other. Current selection of inequalities is motivated by formalization of the rotation number of a circle homeomorphism. -/ variables {α : Type*} namespace monotone variables [preorder α] {f : α → α} {x y : ℕ → α} lemma seq_le_seq (hf : monotone f) (n : ℕ) (h₀ : x 0 ≤ y 0) (hx : ∀ k < n, x (k + 1) ≤ f (x k)) (hy : ∀ k < n, f (y k) ≤ y (k + 1)) : x n ≤ y n := begin induction n with n ihn, { exact h₀ }, { refine (hx _ n.lt_succ_self).trans ((hf $ ihn _ _).trans (hy _ n.lt_succ_self)), exact λ k hk, hx _ (hk.trans n.lt_succ_self), exact λ k hk, hy _ (hk.trans n.lt_succ_self) } end lemma seq_pos_lt_seq_of_lt_of_le (hf : monotone f) {n : ℕ} (hn : 0 < n) (h₀ : x 0 ≤ y 0) (hx : ∀ k < n, x (k + 1) < f (x k)) (hy : ∀ k < n, f (y k) ≤ y (k + 1)) : x n < y n := begin induction n with n ihn, { exact hn.false.elim }, suffices : x n ≤ y n, from (hx n n.lt_succ_self).trans_le ((hf this).trans $ hy n n.lt_succ_self), cases n, { exact h₀ }, refine (ihn n.zero_lt_succ (λ k hk, hx _ _) (λ k hk, hy _ _)).le; exact hk.trans n.succ.lt_succ_self end lemma seq_pos_lt_seq_of_le_of_lt (hf : monotone f) {n : ℕ} (hn : 0 < n) (h₀ : x 0 ≤ y 0) (hx : ∀ k < n, x (k + 1) ≤ f (x k)) (hy : ∀ k < n, f (y k) < y (k + 1)) : x n < y n := hf.dual.seq_pos_lt_seq_of_lt_of_le hn h₀ hy hx lemma seq_lt_seq_of_lt_of_le (hf : monotone f) (n : ℕ) (h₀ : x 0 < y 0) (hx : ∀ k < n, x (k + 1) < f (x k)) (hy : ∀ k < n, f (y k) ≤ y (k + 1)) : x n < y n := by { cases n, exacts [h₀, hf.seq_pos_lt_seq_of_lt_of_le n.zero_lt_succ h₀.le hx hy] } lemma seq_lt_seq_of_le_of_lt (hf : monotone f) (n : ℕ) (h₀ : x 0 < y 0) (hx : ∀ k < n, x (k + 1) ≤ f (x k)) (hy : ∀ k < n, f (y k) < y (k + 1)) : x n < y n := hf.dual.seq_lt_seq_of_lt_of_le n h₀ hy hx end monotone namespace function section preorder variables [preorder α] {f : α → α} lemma id_le_iterate_of_id_le (h : id ≤ f) : ∀ n, id ≤ (f^[n]) | 0 := by { rw function.iterate_zero, exact le_rfl } | (n + 1) := λ x, begin rw function.iterate_succ_apply', exact (id_le_iterate_of_id_le n x).trans (h _), end lemma iterate_le_id_of_le_id (h : f ≤ id) : ∀ n, (f^[n]) ≤ id := @id_le_iterate_of_id_le (order_dual α) _ f h lemma iterate_le_iterate_of_id_le (h : id ≤ f) {m n : ℕ} (hmn : m ≤ n) : f^[m] ≤ (f^[n]) := begin rw [←nat.add_sub_cancel' hmn, add_comm, function.iterate_add], exact λ x, id_le_iterate_of_id_le h _ _, end lemma iterate_le_iterate_of_le_id (h : f ≤ id) {m n : ℕ} (hmn : m ≤ n) : f^[n] ≤ (f^[m]) := @iterate_le_iterate_of_id_le (order_dual α) _ f h m n hmn end preorder namespace commute section preorder variables [preorder α] {f g : α → α} lemma iterate_le_of_map_le (h : commute f g) (hf : monotone f) (hg : monotone g) {x} (hx : f x ≤ g x) (n : ℕ) : f^[n] x ≤ (g^[n]) x := by refine hf.seq_le_seq n _ (λ k hk, _) (λ k hk, _); simp [iterate_succ' f, h.iterate_right _ _, hg.iterate _ hx] lemma iterate_pos_lt_of_map_lt (h : commute f g) (hf : monotone f) (hg : strict_mono g) {x} (hx : f x < g x) {n} (hn : 0 < n) : f^[n] x < (g^[n]) x := by refine hf.seq_pos_lt_seq_of_le_of_lt hn _ (λ k hk, _) (λ k hk, _); simp [iterate_succ' f, h.iterate_right _ _, hg.iterate _ hx] lemma iterate_pos_lt_of_map_lt' (h : commute f g) (hf : strict_mono f) (hg : monotone g) {x} (hx : f x < g x) {n} (hn : 0 < n) : f^[n] x < (g^[n]) x := @iterate_pos_lt_of_map_lt (order_dual α) _ g f h.symm hg.dual hf.dual x hx n hn end preorder variables [linear_order α] {f g : α → α} lemma iterate_pos_lt_iff_map_lt (h : commute f g) (hf : monotone f) (hg : strict_mono g) {x n} (hn : 0 < n) : f^[n] x < (g^[n]) x ↔ f x < g x := begin rcases lt_trichotomy (f x) (g x) with H|H|H, { simp only [*, iterate_pos_lt_of_map_lt] }, { simp only [*, h.iterate_eq_of_map_eq, lt_irrefl] }, { simp only [lt_asymm H, lt_asymm (h.symm.iterate_pos_lt_of_map_lt' hg hf H hn)] } end lemma iterate_pos_lt_iff_map_lt' (h : commute f g) (hf : strict_mono f) (hg : monotone g) {x n} (hn : 0 < n) : f^[n] x < (g^[n]) x ↔ f x < g x := @iterate_pos_lt_iff_map_lt (order_dual α) _ _ _ h.symm hg.dual hf.dual x n hn lemma iterate_pos_le_iff_map_le (h : commute f g) (hf : monotone f) (hg : strict_mono g) {x n} (hn : 0 < n) : f^[n] x ≤ (g^[n]) x ↔ f x ≤ g x := by simpa only [not_lt] using not_congr (h.symm.iterate_pos_lt_iff_map_lt' hg hf hn) lemma iterate_pos_le_iff_map_le' (h : commute f g) (hf : strict_mono f) (hg : monotone g) {x n} (hn : 0 < n) : f^[n] x ≤ (g^[n]) x ↔ f x ≤ g x := by simpa only [not_lt] using not_congr (h.symm.iterate_pos_lt_iff_map_lt hg hf hn) lemma iterate_pos_eq_iff_map_eq (h : commute f g) (hf : monotone f) (hg : strict_mono g) {x n} (hn : 0 < n) : f^[n] x = (g^[n]) x ↔ f x = g x := by simp only [le_antisymm_iff, h.iterate_pos_le_iff_map_le hf hg hn, h.symm.iterate_pos_le_iff_map_le' hg hf hn] end commute end function namespace monotone open function section variables {β : Type*} [preorder β] {f : α → α} {g : β → β} {h : α → β} lemma le_iterate_comp_of_le (hg : monotone g) (H : ∀ x, h (f x) ≤ g (h x)) (n : ℕ) (x : α) : h (f^[n] x) ≤ (g^[n] (h x)) := by refine hg.seq_le_seq n _ (λ k hk, _) (λ k hk, _); simp [iterate_succ', H _] lemma iterate_comp_le_of_le (hg : monotone g) (H : ∀ x, g (h x) ≤ h (f x)) (n : ℕ) (x : α) : g^[n] (h x) ≤ h (f^[n] x) := hg.dual.le_iterate_comp_of_le H n x end variables [preorder α] {f g : α → α} /-- If `f ≤ g` and `f` is monotone, then `f^[n] ≤ g^[n]`. -/ lemma iterate_le_of_le (hf : monotone f) (h : f ≤ g) (n : ℕ) : f^[n] ≤ (g^[n]) := hf.iterate_comp_le_of_le h n /-- If `f ≤ g` and `f` is monotone, then `f^[n] ≤ g^[n]`. -/ lemma iterate_ge_of_ge (hg : monotone g) (h : f ≤ g) (n : ℕ) : f^[n] ≤ (g^[n]) := hg.dual.iterate_le_of_le h n end monotone
2da2421e97f3eb50dd55d49672e793ecf4c540f6
fbf512ee44de430e3d3c5869751ad95325c938d7
/tactic.lean
0e6c9f35781b1efbbeedfe82c50be4121035ebf7
[]
no_license
skbaek/clausify
4858c9005fb86a5e410bfcaa77524f82d955f655
d09b071bdcce7577c3fffacd0893b776285b1590
refs/heads/master
1,588,360,590,818
1,553,553,880,000
1,553,553,880,000
177,644,542
0
0
null
null
null
null
UTF-8
Lean
false
false
715
lean
open expr tactic meta def unifies_with_and (x : expr) : tactic (expr × expr) := do mv1 ← mk_meta_var `(Prop), mv2 ← mk_meta_var `(Prop), tactic.unify x `(%%mv1 ∧ %%mv2), return (mv1,mv2) meta def split_ands_core : list expr → tactic unit | [] := skip | (x::xs) := try (do (px,qx) ← infer_type x >>= unifies_with_and, np ← get_unused_name, nq ← get_unused_name, assertv np px (app (app (app `(@and.elim_left).to_expr px) qx) x), assertv nq qx (app (app (app `(@and.elim_right).to_expr px) qx) x)) >> split_ands_core xs meta def split_ands : tactic unit := local_context >>= split_ands_core meta def intro_fresh : tactic expr := do n ← get_unused_name, intro n
5222ff45510d559f3290ea1fe02147028eae3399
46125763b4dbf50619e8846a1371029346f4c3db
/src/linear_algebra/tensor_product.lean
0fc5732514bf36402c0206d2235ff5e82a639ce1
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
16,024
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro Tensor product of modules over commutative rings. -/ import group_theory.free_abelian_group import linear_algebra.direct_sum_module variables {R : Type*} [comm_ring R] variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q] variables [module R M] [module R N] [module R P] [module R Q] include R set_option class.instance_max_depth 100 namespace linear_map variables (R) def mk₂ (f : M → N → P) (H1 : ∀ m₁ m₂ n, f (m₁ + m₂) n = f m₁ n + f m₂ n) (H2 : ∀ (c:R) m n, f (c • m) n = c • f m n) (H3 : ∀ m n₁ n₂, f m (n₁ + n₂) = f m n₁ + f m n₂) (H4 : ∀ (c:R) m n, f m (c • n) = c • f m n) : M →ₗ N →ₗ P := ⟨λ m, ⟨f m, H3 m, λ c, H4 c m⟩, λ m₁ m₂, linear_map.ext $ H1 m₁ m₂, λ c m, linear_map.ext $ H2 c m⟩ variables {R} @[simp] theorem mk₂_apply (f : M → N → P) {H1 H2 H3 H4} (m : M) (n : N) : (mk₂ R f H1 H2 H3 H4 : M →ₗ[R] N →ₗ P) m n = f m n := rfl variables (f : M →ₗ[R] N →ₗ[R] P) theorem ext₂ {f g : M →ₗ[R] N →ₗ[R] P} (H : ∀ m n, f m n = g m n) : f = g := linear_map.ext (λ m, linear_map.ext $ λ n, H m n) def flip : N →ₗ M →ₗ P := mk₂ R (λ n m, f m n) (λ n₁ n₂ m, (f m).map_add _ _) (λ c n m, (f m).map_smul _ _) (λ n m₁ m₂, by rw f.map_add; refl) (λ c n m, by rw f.map_smul; refl) @[simp] theorem flip_apply (m : M) (n : N) : flip f n m = f m n := rfl variables {R} theorem flip_inj {f g : M →ₗ[R] N →ₗ P} (H : flip f = flip g) : f = g := ext₂ $ λ m n, show flip f n m = flip g n m, by rw H variables (R M N P) def lflip : (M →ₗ[R] N →ₗ P) →ₗ[R] N →ₗ M →ₗ P := ⟨flip, λ _ _, rfl, λ _ _, rfl⟩ variables {R M N P} @[simp] theorem lflip_apply (m : M) (n : N) : lflip R M N P f n m = f m n := rfl theorem map_zero₂ (y) : f 0 y = 0 := (flip f y).map_zero theorem map_neg₂ (x y) : f (-x) y = -f x y := (flip f y).map_neg _ theorem map_add₂ (x₁ x₂ y) : f (x₁ + x₂) y = f x₁ y + f x₂ y := (flip f y).map_add _ _ theorem map_smul₂ (r:R) (x y) : f (r • x) y = r • f x y := (flip f y).map_smul _ _ variables (R P) def lcomp (f : M →ₗ[R] N) : (N →ₗ P) →ₗ M →ₗ P := flip $ (flip id).comp f variables {R P} @[simp] theorem lcomp_apply (f : M →ₗ[R] N) (g : N →ₗ P) (x : M) : lcomp R P f g x = g (f x) := rfl variables (R M N P) def llcomp : (N →ₗ[R] P) →ₗ[R] (M →ₗ[R] N) →ₗ M →ₗ P := flip ⟨lcomp R P, λ f f', ext₂ $ λ g x, g.map_add _ _, λ c f, ext₂ $ λ g x, g.map_smul _ _⟩ variables {R M N P} section @[simp] theorem llcomp_apply (f : N →ₗ[R] P) (g : M →ₗ[R] N) (x : M) : llcomp R M N P f g x = f (g x) := rfl end def compl₂ (g : Q →ₗ N) : M →ₗ Q →ₗ P := (lcomp R _ g).comp f @[simp] theorem compl₂_apply (g : Q →ₗ[R] N) (m : M) (q : Q) : f.compl₂ g m q = f m (g q) := rfl def compr₂ (g : P →ₗ Q) : M →ₗ N →ₗ Q := linear_map.comp (llcomp R N P Q g) f @[simp] theorem compr₂_apply (g : P →ₗ[R] Q) (m : M) (n : N) : f.compr₂ g m n = g (f m n) := rfl variables (R M) def lsmul : R →ₗ M →ₗ M := mk₂ R (•) add_smul (λ _ _ _, mul_smul _ _ _) smul_add (λ r s m, by simp only [smul_smul, smul_eq_mul, mul_comm]) variables {R M} @[simp] theorem lsmul_apply (r : R) (m : M) : lsmul R M r m = r • m := rfl end linear_map variables (M N) namespace tensor_product section open free_abelian_group variables (R) def relators : set (free_abelian_group (M × N)) := add_group.closure { x : free_abelian_group (M × N) | (∃ (m₁ m₂ : M) (n : N), x = of (m₁, n) + of (m₂, n) - of (m₁ + m₂, n)) ∨ (∃ (m : M) (n₁ n₂ : N), x = of (m, n₁) + of (m, n₂) - of (m, n₁ + n₂)) ∨ (∃ (r : R) (m : M) (n : N), x = of (r • m, n) - of (m, r • n)) } end namespace relators instance : normal_add_subgroup (relators R M N) := by unfold relators; apply normal_add_subgroup_of_add_comm_group end relators end tensor_product variables (R) def tensor_product : Type* := quotient_add_group.quotient (tensor_product.relators R M N) variables {R} localized "infix ` ⊗ `:100 := tensor_product _" in tensor_product localized "notation M ` ⊗[`:100 R `] ` N:100 := tensor_product R M N" in tensor_product namespace tensor_product section module local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup instance : add_comm_group (M ⊗[R] N) := quotient_add_group.add_comm_group _ instance : inhabited (M ⊗[R] N) := ⟨0⟩ instance quotient.mk.is_add_group_hom : is_add_group_hom (quotient.mk : free_abelian_group (M × N) → M ⊗ N) := quotient_add_group.is_add_group_hom _ variables (R) {M N} def tmul (m : M) (n : N) : M ⊗[R] N := quotient_add_group.mk $ free_abelian_group.of (m, n) variables {R} infix ` ⊗ₜ `:100 := tmul _ notation x ` ⊗ₜ[`:100 R `] ` y := tmul R x y lemma add_tmul (m₁ m₂ : M) (n : N) : (m₁ + m₂) ⊗ₜ n = m₁ ⊗ₜ n + m₂ ⊗ₜ[R] n := eq.symm $ sub_eq_zero.1 $ eq.symm $ quotient.sound $ add_group.in_closure.basic $ or.inl $ ⟨m₁, m₂, n, rfl⟩ lemma tmul_add (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ + n₂) = m ⊗ₜ n₁ + m ⊗ₜ[R] n₂ := eq.symm $ sub_eq_zero.1 $ eq.symm $ quotient.sound $ add_group.in_closure.basic $ or.inr $ or.inl $ ⟨m, n₁, n₂, rfl⟩ lemma smul_tmul (r : R) (m : M) (n : N) : (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) := sub_eq_zero.1 $ eq.symm $ quotient.sound $ add_group.in_closure.basic $ or.inr $ or.inr $ ⟨r, m, n, rfl⟩ local attribute [instance] quotient_add_group.is_add_group_hom_quotient_lift def smul.aux (r : R) : free_abelian_group (M × N) → M ⊗[R] N := free_abelian_group.lift (λ (y : M × N), (r • y.1) ⊗ₜ y.2) instance (r : R) : is_add_group_hom (smul.aux r : _ → M ⊗ N) := by unfold smul.aux; apply_instance instance : has_scalar R (M ⊗ N) := ⟨λ r, quotient_add_group.lift _ (smul.aux r) $ λ x hx, begin refine (is_add_group_hom.mem_ker (smul.aux r : _ → M ⊗ N)).1 (add_group.closure_subset _ hx), clear hx x, rintro x (⟨m₁, m₂, n, rfl⟩ | ⟨m, n₁, n₂, rfl⟩ | ⟨q, m, n, rfl⟩); simp only [smul.aux, is_add_group_hom.mem_ker, -sub_eq_add_neg, sub_self, add_tmul, tmul_add, smul_tmul, smul_add, smul_smul, mul_comm, free_abelian_group.lift.of, free_abelian_group.lift.add, free_abelian_group.lift.sub] end⟩ instance smul.is_add_group_hom (r : R) : is_add_group_hom ((•) r : M ⊗[R] N → M ⊗[R] N) := by unfold has_scalar.smul; apply_instance protected theorem smul_add (r : R) (x y : M ⊗[R] N) : r • (x + y) = r • x + r • y := is_add_hom.map_add _ _ _ instance : module R (M ⊗ N) := module.of_core { smul := (•), smul_add := tensor_product.smul_add, add_smul := begin intros r s x, apply quotient_add_group.induction_on' x, intro z, symmetry, refine @free_abelian_group.lift.unique _ _ _ _ _ (is_add_group_hom.mk' $ λ p q, _) _ z, { simp [tensor_product.smul_add, add_comm, add_left_comm] }, rintro ⟨m, n⟩, change (r • m) ⊗ₜ n + (s • m) ⊗ₜ n = ((r + s) • m) ⊗ₜ n, rw [add_smul, add_tmul] end, mul_smul := begin intros r s x, apply quotient_add_group.induction_on' x, intro z, symmetry, refine @free_abelian_group.lift.unique _ _ _ _ _ (is_add_group_hom.mk' $ λ p q, _) _ z, { simp [tensor_product.smul_add] }, rintro ⟨m, n⟩, change r • s • (m ⊗ₜ n) = ((r * s) • m) ⊗ₜ n, rw mul_smul, refl end, one_smul := λ x, quotient.induction_on x $ λ _, eq.symm $ free_abelian_group.lift.unique _ _ $ λ ⟨p, q⟩, by rw one_smul; refl } @[simp] lemma tmul_smul (r : R) (x : M) (y : N) : x ⊗ₜ (r • y) = r • (x ⊗ₜ[R] y) := (smul_tmul _ _ _).symm variables (R M N) def mk : M →ₗ N →ₗ M ⊗ N := linear_map.mk₂ R (⊗ₜ) add_tmul (λ c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul variables {R M N} @[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m ⊗ₜ n := rfl lemma zero_tmul (n : N) : (0 ⊗ₜ[R] n : M ⊗ N) = 0 := (mk R M N).map_zero₂ _ lemma tmul_zero (m : M) : (m ⊗ₜ[R] 0 : M ⊗ N) = 0 := (mk R M N _).map_zero lemma neg_tmul (m : M) (n : N) : (-m) ⊗ₜ n = -(m ⊗ₜ[R] n) := (mk R M N).map_neg₂ _ _ lemma tmul_neg (m : M) (n : N) : m ⊗ₜ (-n) = -(m ⊗ₜ[R] n) := (mk R M N _).map_neg _ end module local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup @[elab_as_eliminator] protected theorem induction_on {C : (M ⊗[R] N) → Prop} (z : M ⊗[R] N) (C0 : C 0) (C1 : ∀ x y, C $ x ⊗ₜ[R] y) (Cp : ∀ x y, C x → C y → C (x + y)) : C z := quotient.induction_on z $ λ x, free_abelian_group.induction_on x C0 (λ ⟨p, q⟩, C1 p q) (λ ⟨p, q⟩ _, show C (-(p ⊗ₜ q)), by rw ← neg_tmul; from C1 (-p) q) (λ _ _, Cp _ _) section UMP variables {M N P Q} variables (f : M →ₗ[R] N →ₗ[R] P) local attribute [instance] free_abelian_group.lift.is_add_group_hom def lift_aux : (M ⊗[R] N) → P := quotient_add_group.lift _ (free_abelian_group.lift $ λ z, f z.1 z.2) $ λ x hx, begin refine (is_add_group_hom.mem_ker _).1 (add_group.closure_subset _ hx), clear hx x, rintro x (⟨m₁, m₂, n, rfl⟩ | ⟨m, n₁, n₂, rfl⟩ | ⟨q, m, n, rfl⟩); simp [is_add_group_hom.mem_ker, -sub_eq_add_neg, f.map_add, f.map_add₂, f.map_smul, f.map_smul₂, sub_self], end variable {f} local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup @[simp] lemma lift_aux.add (x y) : lift_aux f (x + y) = lift_aux f x + lift_aux f y := quotient.induction_on₂ x y $ λ m n, free_abelian_group.lift.add _ _ _ @[simp] lemma lift_aux.smul (r:R) (x) : lift_aux f (r • x) = r • lift_aux f x := tensor_product.induction_on _ _ x (smul_zero _).symm (λ p q, by rw [← tmul_smul]; simp [lift_aux, tmul]) (λ p q ih1 ih2, by simp [@smul_add _ _ _ _ _ _ p _, lift_aux.add, ih1, ih2, smul_add]) variable (f) def lift : M ⊗ N →ₗ P := { to_fun := lift_aux f, add := lift_aux.add, smul := lift_aux.smul } variable {f} @[simp] lemma lift.tmul (x y) : lift f (x ⊗ₜ y) = f x y := zero_add _ @[simp] lemma lift.tmul' (x y) : (lift f).1 (x ⊗ₜ y) = f x y := lift.tmul _ _ theorem lift.unique {g : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = f x y) : g = lift f := linear_map.ext $ λ z, begin apply quotient_add_group.induction_on' z, intro z, refine @free_abelian_group.lift.unique _ _ _ _ _ (is_add_group_hom.mk' $ λ p q, _) _ z, { simp [g.2] }, exact λ ⟨m, n⟩, H m n end theorem lift_mk : lift (mk R M N) = linear_map.id := eq.symm $ lift.unique $ λ x y, rfl theorem lift_compr₂ (g : P →ₗ Q) : lift (f.compr₂ g) = g.comp (lift f) := eq.symm $ lift.unique $ λ x y, by simp theorem lift_mk_compr₂ (f : M ⊗ N →ₗ P) : lift ((mk R M N).compr₂ f) = f := by rw [lift_compr₂, lift_mk, linear_map.comp_id] theorem ext {g h : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h := by rw ← lift_mk_compr₂ h; exact lift.unique H theorem mk_compr₂_inj {g h : M ⊗ N →ₗ P} (H : (mk R M N).compr₂ g = (mk R M N).compr₂ h) : g = h := by rw [← lift_mk_compr₂ g, H, lift_mk_compr₂] example : M → N → (M → N → P) → P := λ m, flip $ λ f, f m variables (R M N P) def uncurry : (M →ₗ N →ₗ[R] P) →ₗ M ⊗ N →ₗ P := linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp linear_map.id.flip variables {R M N P} @[simp] theorem uncurry_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) : uncurry R M N P f (m ⊗ₜ n) = f m n := by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl variables (R M N P) def lift.equiv : (M →ₗ N →ₗ P) ≃ₗ (M ⊗ N →ₗ P) := { inv_fun := λ f, (mk R M N).compr₂ f, left_inv := λ f, linear_map.ext₂ $ λ m n, lift.tmul _ _, right_inv := λ f, ext $ λ m n, lift.tmul _ _, .. uncurry R M N P } def lcurry : (M ⊗[R] N →ₗ[R] P) →ₗ[R] M →ₗ[R] N →ₗ[R] P := (lift.equiv R M N P).symm variables {R M N P} @[simp] theorem lcurry_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) : lcurry R M N P f m n = f (m ⊗ₜ n) := rfl def curry (f : M ⊗ N →ₗ P) : M →ₗ N →ₗ P := lcurry R M N P f @[simp] theorem curry_apply (f : M ⊗ N →ₗ[R] P) (m : M) (n : N) : curry f m n = f (m ⊗ₜ n) := rfl end UMP variables {M N} /-- The base ring is a left identity for the tensor product of modules, up to linear equivalence. -/ protected def lid : R ⊗ M ≃ₗ M := linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1) (linear_map.ext $ λ _, by simp) (ext $ λ r m, by simp; rw [← tmul_smul, ← smul_tmul, smul_eq_mul, mul_one]) /-- The tensor product of modules is commutative, up to linear equivalence. -/ protected def comm : M ⊗ N ≃ₗ N ⊗ M := linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip) (ext $ λ m n, rfl) (ext $ λ m n, rfl) /-- The base ring is a right identity for the tensor product of modules, up to linear equivalence. -/ protected def rid : M ⊗ R ≃ₗ M := linear_equiv.trans tensor_product.comm tensor_product.lid open linear_map protected def assoc : (M ⊗[R] N) ⊗[R] P ≃ₗ[R] M ⊗[R] (N ⊗[R] P) := begin refine linear_equiv.of_linear (lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _) (lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _) (mk_compr₂_inj $ linear_map.ext $ λ m, ext $ λ n p, _) (mk_compr₂_inj $ flip_inj $ linear_map.ext $ λ p, ext $ λ m n, _); repeat { rw lift.tmul <|> rw compr₂_apply <|> rw comp_apply <|> rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|> rw uncurry_apply <|> rw curry_apply <|> rw id_apply } end def map (f : M →ₗ[R] P) (g : N →ₗ Q) : M ⊗ N →ₗ P ⊗ Q := lift $ comp (compl₂ (mk _ _ _) g) f @[simp] theorem map_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (m : M) (n : N) : map f g (m ⊗ₜ n) = f m ⊗ₜ g n := rfl def congr (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) : M ⊗ N ≃ₗ[R] P ⊗ Q := linear_equiv.of_linear (map f g) (map f.symm g.symm) (ext $ λ m n, by simp; simp only [linear_equiv.apply_symm_apply]) (ext $ λ m n, by simp; simp only [linear_equiv.symm_apply_apply]) variables (ι₁ : Type*) (ι₂ : Type*) variables [decidable_eq ι₁] [decidable_eq ι₂] variables (M₁ : ι₁ → Type*) (M₂ : ι₂ → Type*) variables [Π i₁, add_comm_group (M₁ i₁)] [Π i₂, add_comm_group (M₂ i₂)] variables [Π i₁, module R (M₁ i₁)] [Π i₂, module R (M₂ i₂)] def direct_sum : direct_sum ι₁ M₁ ⊗[R] direct_sum ι₂ M₂ ≃ₗ[R] direct_sum (ι₁ × ι₂) (λ i, M₁ i.1 ⊗[R] M₂ i.2) := begin refine linear_equiv.of_linear (lift $ direct_sum.to_module R _ _ $ λ i₁, flip $ direct_sum.to_module R _ _ $ λ i₂, flip $ curry $ direct_sum.lof R (ι₁ × ι₂) (λ i, M₁ i.1 ⊗[R] M₂ i.2) (i₁, i₂)) (direct_sum.to_module R _ _ $ λ i, map (direct_sum.lof R _ _ _) (direct_sum.lof R _ _ _)) (linear_map.ext $ direct_sum.to_module.ext _ $ λ i, mk_compr₂_inj $ linear_map.ext $ λ x₁, linear_map.ext $ λ x₂, _) (mk_compr₂_inj $ linear_map.ext $ direct_sum.to_module.ext _ $ λ i₁, linear_map.ext $ λ x₁, linear_map.ext $ direct_sum.to_module.ext _ $ λ i₂, linear_map.ext $ λ x₂, _); repeat { rw compr₂_apply <|> rw comp_apply <|> rw id_apply <|> rw mk_apply <|> rw direct_sum.to_module_lof <|> rw map_tmul <|> rw lift.tmul <|> rw flip_apply <|> rw curry_apply }, cases i; refl end end tensor_product
d9227b012b200c6ff6ad88a46ee15d5a4971a0ea
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/exterior_algebra.lean
dde36fd297534e28567fc3b4eb5c97b7cbe2e00f
[ "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
10,038
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhangir Azerbayev, Adam Topaz, Eric Wieser -/ import algebra.ring_quot import linear_algebra.tensor_algebra import linear_algebra.alternating import group_theory.perm.sign /-! # Exterior Algebras We construct the exterior algebra of a module `M` over a commutative semiring `R`. ## Notation The exterior algebra of the `R`-module `M` is denoted as `exterior_algebra R M`. It is endowed with the structure of an `R`-algebra. Given a linear morphism `f : M → A` from a module `M` to another `R`-algebra `A`, such that `cond : ∀ m : M, f m * f m = 0`, there is a (unique) lift of `f` to an `R`-algebra morphism, which is denoted `exterior_algebra.lift R f cond`. The canonical linear map `M → exterior_algebra R M` is denoted `exterior_algebra.ι R`. ## Theorems The main theorems proved ensure that `exterior_algebra R M` satisfies the universal property of the exterior algebra. 1. `ι_comp_lift` is fact that the composition of `ι R` with `lift R f cond` agrees with `f`. 2. `lift_unique` ensures the uniqueness of `lift R f cond` with respect to 1. ## Definitions * `ι_multi` is the `alternating_map` corresponding to the wedge product of `ι R m` terms. ## Implementation details The exterior algebra of `M` is constructed as a quotient of the tensor algebra, as follows. 1. We define a relation `exterior_algebra.rel R M` on `tensor_algebra R M`. This is the smallest relation which identifies squares of elements of `M` with `0`. 2. The exterior algebra is the quotient of the tensor algebra by this relation. -/ universes u1 u2 u3 variables (R : Type u1) [comm_semiring R] variables (M : Type u2) [add_comm_monoid M] [module R M] namespace exterior_algebra open tensor_algebra /-- `rel` relates each `ι m * ι m`, for `m : M`, with `0`. The exterior algebra of `M` is defined as the quotient modulo this relation. -/ inductive rel : tensor_algebra R M → tensor_algebra R M → Prop | of (m : M) : rel ((ι R m) * (ι R m)) 0 end exterior_algebra /-- The exterior algebra of an `R`-module `M`. -/ @[derive [inhabited, semiring, algebra R]] def exterior_algebra := ring_quot (exterior_algebra.rel R M) namespace exterior_algebra variables {M} instance {S : Type u3} [comm_ring S] [module S M] : ring (exterior_algebra S M) := ring_quot.ring (exterior_algebra.rel S M) /-- The canonical linear map `M →ₗ[R] exterior_algebra R M`. -/ def ι : M →ₗ[R] exterior_algebra R M := (ring_quot.mk_alg_hom R _).to_linear_map.comp (tensor_algebra.ι R) variables {R} /-- As well as being linear, `ι m` squares to zero -/ @[simp] theorem ι_sq_zero (m : M) : (ι R m) * (ι R m) = 0 := begin erw [←alg_hom.map_mul, ring_quot.mk_alg_hom_rel R (rel.of m), alg_hom.map_zero _], end variables {A : Type*} [semiring A] [algebra R A] @[simp] theorem comp_ι_sq_zero (g : exterior_algebra R M →ₐ[R] A) (m : M) : g (ι R m) * g (ι R m) = 0 := by rw [←alg_hom.map_mul, ι_sq_zero, alg_hom.map_zero] variables (R) /-- Given a linear map `f : M →ₗ[R] A` into an `R`-algebra `A`, which satisfies the condition: `cond : ∀ m : M, f m * f m = 0`, this is the canonical lift of `f` to a morphism of `R`-algebras from `exterior_algebra R M` to `A`. -/ @[simps symm_apply] def lift : {f : M →ₗ[R] A // ∀ m, f m * f m = 0} ≃ (exterior_algebra R M →ₐ[R] A) := { to_fun := λ f, ring_quot.lift_alg_hom R ⟨tensor_algebra.lift R (f : M →ₗ[R] A), λ x y (h : rel R M x y), by { induction h, rw [alg_hom.map_zero, alg_hom.map_mul, tensor_algebra.lift_ι_apply, f.prop] }⟩, inv_fun := λ F, ⟨F.to_linear_map.comp (ι R), λ m, by rw [ linear_map.comp_apply, alg_hom.to_linear_map_apply, comp_ι_sq_zero]⟩, left_inv := λ f, by { ext, simp [ι] }, right_inv := λ F, by { ext, simp [ι] } } @[simp] theorem ι_comp_lift (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = 0) : (lift R ⟨f, cond⟩).to_linear_map.comp (ι R) = f := (subtype.mk_eq_mk.mp $ (lift R).symm_apply_apply ⟨f, cond⟩) @[simp] theorem lift_ι_apply (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = 0) (x) : lift R ⟨f, cond⟩ (ι R x) = f x := (linear_map.ext_iff.mp $ ι_comp_lift R f cond) x @[simp] theorem lift_unique (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = 0) (g : exterior_algebra R M →ₐ[R] A) : g.to_linear_map.comp (ι R) = f ↔ g = lift R ⟨f, cond⟩ := begin convert (lift R).symm_apply_eq, rw lift_symm_apply, simp only, end attribute [irreducible] ι lift -- Marking `exterior_algebra` irreducible makes our `ring` instances inaccessible. -- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/algebra.2Esemiring_to_ring.20breaks.20semimodule.20typeclass.20lookup/near/212580241 -- For now, we avoid this by not marking it irreducible. variables {R M} @[simp] theorem lift_comp_ι (g : exterior_algebra R M →ₐ[R] A) : lift R ⟨g.to_linear_map.comp (ι R), comp_ι_sq_zero _⟩ = g := begin convert (lift R).apply_symm_apply g, rw lift_symm_apply, refl, end /-- See note [partially-applied ext lemmas]. -/ @[ext] theorem hom_ext {f g : exterior_algebra R M →ₐ[R] A} (h : f.to_linear_map.comp (ι R) = g.to_linear_map.comp (ι R)) : f = g := begin apply (lift R).symm.injective, rw [lift_symm_apply, lift_symm_apply], simp only [h], end /-- If `C` holds for the `algebra_map` of `r : R` into `exterior_algebra R M`, the `ι` of `x : M`, and is preserved under addition and muliplication, then it holds for all of `exterior_algebra R M`. -/ -- This proof closely follows `tensor_algebra.induction` @[elab_as_eliminator] lemma induction {C : exterior_algebra R M → Prop} (h_grade0 : ∀ r, C (algebra_map R (exterior_algebra R M) r)) (h_grade1 : ∀ x, C (ι R x)) (h_mul : ∀ a b, C a → C b → C (a * b)) (h_add : ∀ a b, C a → C b → C (a + b)) (a : exterior_algebra R M) : C a := begin -- the arguments are enough to construct a subalgebra, and a mapping into it from M let s : subalgebra R (exterior_algebra R M) := { carrier := C, mul_mem' := h_mul, add_mem' := h_add, algebra_map_mem' := h_grade0, }, let of : { f : M →ₗ[R] s // ∀ m, f m * f m = 0 } := ⟨(ι R).cod_restrict s.to_submodule h_grade1, λ m, subtype.eq $ ι_sq_zero m ⟩, -- the mapping through the subalgebra is the identity have of_id : alg_hom.id R (exterior_algebra R M) = s.val.comp (lift R of), { ext, simp [of], }, -- finding a proof is finding an element of the subalgebra convert subtype.prop (lift R of a), exact alg_hom.congr_fun of_id a, end /-- The left-inverse of `algebra_map`. -/ def algebra_map_inv : exterior_algebra R M →ₐ[R] R := exterior_algebra.lift R ⟨(0 : M →ₗ[R] R), λ m, by simp⟩ lemma algebra_map_left_inverse : function.left_inverse algebra_map_inv (algebra_map R $ exterior_algebra R M) := λ x, by simp [algebra_map_inv] /-- The left-inverse of `ι`. As an implementation detail, we implement this using `triv_sq_zero_ext` which has a suitable algebra structure. -/ def ι_inv : exterior_algebra R M →ₗ[R] M := (triv_sq_zero_ext.snd_hom R M).comp (lift R ⟨triv_sq_zero_ext.inr_hom R M, λ m, triv_sq_zero_ext.inr_mul_inr R _ m m⟩).to_linear_map lemma ι_left_inverse : function.left_inverse ι_inv (ι R : M → exterior_algebra R M) := λ x, by simp [ι_inv] @[simp] lemma ι_add_mul_swap (x y : M) : ι R x * ι R y + ι R y * ι R x = 0 := calc _ = ι R (x + y) * ι R (x + y) : by simp [mul_add, add_mul] ... = _ : ι_sq_zero _ lemma ι_mul_prod_list {n : ℕ} (f : fin n → M) (i : fin n) : (ι R $ f i) * (list.of_fn $ λ i, ι R $ f i).prod = 0 := begin induction n with n hn, { exact i.elim0, }, { rw [list.of_fn_succ, list.prod_cons, ←mul_assoc], by_cases h : i = 0, { rw [h, ι_sq_zero, zero_mul], }, { replace hn := congr_arg ((*) $ ι R $ f 0) (hn (λ i, f $ fin.succ i) (i.pred h)), simp only at hn, rw [fin.succ_pred, ←mul_assoc, mul_zero] at hn, refine (eq_zero_iff_eq_zero_of_add_eq_zero _).mp hn, rw [← add_mul, ι_add_mul_swap, zero_mul], } } end variables (R) /-- The product of `n` terms of the form `ι R m` is an alternating map. This is a special case of `multilinear_map.mk_pi_algebra_fin` -/ def ι_multi (n : ℕ) : alternating_map R M (exterior_algebra R M) (fin n) := let F := (multilinear_map.mk_pi_algebra_fin R n (exterior_algebra R M)).comp_linear_map (λ i, ι R) in { map_eq_zero_of_eq' := λ f x y hfxy hxy, begin rw [multilinear_map.comp_linear_map_apply, multilinear_map.mk_pi_algebra_fin_apply], wlog h : x < y := lt_or_gt_of_ne hxy using x y, clear hxy, induction n with n hn generalizing x y, { exact x.elim0, }, { rw [list.of_fn_succ, list.prod_cons], by_cases hx : x = 0, -- one of the repeated terms is on the left { rw hx at hfxy h, rw [hfxy, ←fin.succ_pred y (ne_of_lt h).symm], exact ι_mul_prod_list (f ∘ fin.succ) _, }, -- ignore the left-most term and induct on the remaining ones, decrementing indices { convert mul_zero _, refine hn (λ i, f $ fin.succ i) (x.pred hx) (y.pred (ne_of_lt $ lt_of_le_of_lt x.zero_le h).symm) (fin.pred_lt_pred_iff.mpr h) _, simp only [fin.succ_pred], exact hfxy, } } end, to_fun := F, ..F} variables {R} lemma ι_multi_apply {n : ℕ} (v : fin n → M) : ι_multi R n v = (list.of_fn $ λ i, ι R (v i)).prod := rfl end exterior_algebra namespace tensor_algebra variables {R M} /-- The canonical image of the `tensor_algebra` in the `exterior_algebra`, which maps `tensor_algebra.ι R x` to `exterior_algebra.ι R x`. -/ def to_exterior : tensor_algebra R M →ₐ[R] exterior_algebra R M := tensor_algebra.lift R (exterior_algebra.ι R) @[simp] lemma to_exterior_ι (m : M) : (tensor_algebra.ι R m).to_exterior = exterior_algebra.ι R m := by simp [to_exterior] end tensor_algebra
341fe3f1d658480e59f2d83963449abe9da54d53
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/probability/process/adapted.lean
a483a31db3c44f2a3f8f1801e523c93a2673c201
[ "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
9,032
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying, Rémy Degenne -/ import probability.process.filtration import topology.instances.discrete /-! # Adapted and progressively measurable processes > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines some standard definition from the theory of stochastic processes including filtrations and stopping times. These definitions are used to model the amount of information at a specific time and are the first step in formalizing stochastic processes. ## Main definitions * `measure_theory.adapted`: a sequence of functions `u` is said to be adapted to a filtration `f` if at each point in time `i`, `u i` is `f i`-strongly measurable * `measure_theory.prog_measurable`: a sequence of functions `u` is said to be progressively measurable with respect to a filtration `f` if at each point in time `i`, `u` restricted to `set.Iic i × Ω` is strongly measurable with respect to the product `measurable_space` structure where the σ-algebra used for `Ω` is `f i`. ## Main results * `adapted.prog_measurable_of_continuous`: a continuous adapted process is progressively measurable. ## Tags adapted, progressively measurable -/ open filter order topological_space open_locale classical measure_theory nnreal ennreal topology big_operators namespace measure_theory variables {Ω β ι : Type*} {m : measurable_space Ω} [topological_space β] [preorder ι] {u v : ι → Ω → β} {f : filtration ι m} /-- A sequence of functions `u` is adapted to a filtration `f` if for all `i`, `u i` is `f i`-measurable. -/ def adapted (f : filtration ι m) (u : ι → Ω → β) : Prop := ∀ i : ι, strongly_measurable[f i] (u i) namespace adapted @[protected, to_additive] lemma mul [has_mul β] [has_continuous_mul β] (hu : adapted f u) (hv : adapted f v) : adapted f (u * v) := λ i, (hu i).mul (hv i) @[protected, to_additive] lemma div [has_div β] [has_continuous_div β] (hu : adapted f u) (hv : adapted f v) : adapted f (u / v) := λ i, (hu i).div (hv i) @[protected, to_additive] lemma inv [group β] [topological_group β] (hu : adapted f u) : adapted f u⁻¹ := λ i, (hu i).inv @[protected] lemma smul [has_smul ℝ β] [has_continuous_smul ℝ β] (c : ℝ) (hu : adapted f u) : adapted f (c • u) := λ i, (hu i).const_smul c @[protected] lemma strongly_measurable {i : ι} (hf : adapted f u) : strongly_measurable[m] (u i) := (hf i).mono (f.le i) lemma strongly_measurable_le {i j : ι} (hf : adapted f u) (hij : i ≤ j) : strongly_measurable[f j] (u i) := (hf i).mono (f.mono hij) end adapted lemma adapted_const (f : filtration ι m) (x : β) : adapted f (λ _ _, x) := λ i, strongly_measurable_const variable (β) lemma adapted_zero [has_zero β] (f : filtration ι m) : adapted f (0 : ι → Ω → β) := λ i, @strongly_measurable_zero Ω β (f i) _ _ variable {β} lemma filtration.adapted_natural [metrizable_space β] [mβ : measurable_space β] [borel_space β] {u : ι → Ω → β} (hum : ∀ i, strongly_measurable[m] (u i)) : adapted (filtration.natural u hum) u := begin assume i, refine strongly_measurable.mono _ (le_supr₂_of_le i (le_refl i) le_rfl), rw strongly_measurable_iff_measurable_separable, exact ⟨measurable_iff_comap_le.2 le_rfl, (hum i).is_separable_range⟩ end /-- Progressively measurable process. A sequence of functions `u` is said to be progressively measurable with respect to a filtration `f` if at each point in time `i`, `u` restricted to `set.Iic i × Ω` is measurable with respect to the product `measurable_space` structure where the σ-algebra used for `Ω` is `f i`. The usual definition uses the interval `[0,i]`, which we replace by `set.Iic i`. We recover the usual definition for index types `ℝ≥0` or `ℕ`. -/ def prog_measurable [measurable_space ι] (f : filtration ι m) (u : ι → Ω → β) : Prop := ∀ i, strongly_measurable[subtype.measurable_space.prod (f i)] (λ p : set.Iic i × Ω, u p.1 p.2) lemma prog_measurable_const [measurable_space ι] (f : filtration ι m) (b : β) : prog_measurable f ((λ _ _, b) : ι → Ω → β) := λ i, @strongly_measurable_const _ _ (subtype.measurable_space.prod (f i)) _ _ namespace prog_measurable variables [measurable_space ι] protected lemma adapted (h : prog_measurable f u) : adapted f u := begin intro i, have : u i = (λ p : set.Iic i × Ω, u p.1 p.2) ∘ (λ x, (⟨i, set.mem_Iic.mpr le_rfl⟩, x)) := rfl, rw this, exact (h i).comp_measurable measurable_prod_mk_left, end protected lemma comp {t : ι → Ω → ι} [topological_space ι] [borel_space ι] [metrizable_space ι] (h : prog_measurable f u) (ht : prog_measurable f t) (ht_le : ∀ i ω, t i ω ≤ i) : prog_measurable f (λ i ω, u (t i ω) ω) := begin intro i, have : (λ p : ↥(set.Iic i) × Ω, u (t (p.fst : ι) p.snd) p.snd) = (λ p : ↥(set.Iic i) × Ω, u (p.fst : ι) p.snd) ∘ (λ p : ↥(set.Iic i) × Ω, (⟨t (p.fst : ι) p.snd, set.mem_Iic.mpr ((ht_le _ _).trans p.fst.prop)⟩, p.snd)) := rfl, rw this, exact (h i).comp_measurable ((ht i).measurable.subtype_mk.prod_mk measurable_snd), end section arithmetic @[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β] (hu : prog_measurable f u) (hv : prog_measurable f v) : prog_measurable f (λ i ω, u i ω * v i ω) := λ i, (hu i).mul (hv i) @[to_additive] protected lemma finset_prod' {γ} [comm_monoid β] [has_continuous_mul β] {U : γ → ι → Ω → β} {s : finset γ} (h : ∀ c ∈ s, prog_measurable f (U c)) : prog_measurable f (∏ c in s, U c) := finset.prod_induction U (prog_measurable f) (λ _ _, prog_measurable.mul) (prog_measurable_const _ 1) h @[to_additive] protected lemma finset_prod {γ} [comm_monoid β] [has_continuous_mul β] {U : γ → ι → Ω → β} {s : finset γ} (h : ∀ c ∈ s, prog_measurable f (U c)) : prog_measurable f (λ i a, ∏ c in s, U c i a) := by { convert prog_measurable.finset_prod' h, ext i a, simp only [finset.prod_apply], } @[to_additive] protected lemma inv [group β] [topological_group β] (hu : prog_measurable f u) : prog_measurable f (λ i ω, (u i ω)⁻¹) := λ i, (hu i).inv @[to_additive] protected lemma div [group β] [topological_group β] (hu : prog_measurable f u) (hv : prog_measurable f v) : prog_measurable f (λ i ω, u i ω / v i ω) := λ i, (hu i).div (hv i) end arithmetic end prog_measurable lemma prog_measurable_of_tendsto' {γ} [measurable_space ι] [pseudo_metrizable_space β] (fltr : filter γ) [fltr.ne_bot] [fltr.is_countably_generated] {U : γ → ι → Ω → β} (h : ∀ l, prog_measurable f (U l)) (h_tendsto : tendsto U fltr (𝓝 u)) : prog_measurable f u := begin assume i, apply @strongly_measurable_of_tendsto (set.Iic i × Ω) β γ (measurable_space.prod _ (f i)) _ _ fltr _ _ _ _ (λ l, h l i), rw tendsto_pi_nhds at h_tendsto ⊢, intro x, specialize h_tendsto x.fst, rw tendsto_nhds at h_tendsto ⊢, exact λ s hs h_mem, h_tendsto {g | g x.snd ∈ s} (hs.preimage (continuous_apply x.snd)) h_mem, end lemma prog_measurable_of_tendsto [measurable_space ι] [pseudo_metrizable_space β] {U : ℕ → ι → Ω → β} (h : ∀ l, prog_measurable f (U l)) (h_tendsto : tendsto U at_top (𝓝 u)) : prog_measurable f u := prog_measurable_of_tendsto' at_top h h_tendsto /-- A continuous and adapted process is progressively measurable. -/ theorem adapted.prog_measurable_of_continuous [topological_space ι] [metrizable_space ι] [second_countable_topology ι] [measurable_space ι] [opens_measurable_space ι] [pseudo_metrizable_space β] (h : adapted f u) (hu_cont : ∀ ω, continuous (λ i, u i ω)) : prog_measurable f u := λ i, @strongly_measurable_uncurry_of_continuous_of_strongly_measurable _ _ (set.Iic i) _ _ _ _ _ _ _ (f i) _ (λ ω, (hu_cont ω).comp continuous_induced_dom) (λ j, (h j).mono (f.mono j.prop)) /-- For filtrations indexed by a discrete order, `adapted` and `prog_measurable` are equivalent. This lemma provides `adapted f u → prog_measurable f u`. See `prog_measurable.adapted` for the reverse direction, which is true more generally. -/ lemma adapted.prog_measurable_of_discrete [topological_space ι] [discrete_topology ι] [second_countable_topology ι] [measurable_space ι] [opens_measurable_space ι] [pseudo_metrizable_space β] (h : adapted f u) : prog_measurable f u := h.prog_measurable_of_continuous (λ _, continuous_of_discrete_topology) -- this dot notation will make more sense once we have a more general definition for predictable lemma predictable.adapted {f : filtration ℕ m} {u : ℕ → Ω → β} (hu : adapted f (λ n, u (n + 1))) (hu0 : strongly_measurable[f 0] (u 0)) : adapted f u := λ n, match n with | 0 := hu0 | n + 1 := (hu n).mono (f.mono n.le_succ) end end measure_theory
bff3b74389e301848cdeb1cefd3d0569c96c037c
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/logic/relation.lean
d27e6bff4b721e008a52549a8677d860c3139719
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
13,192
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 Transitive reflexive as well as reflexive closure of relations. -/ import tactic.basic variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} namespace relation section comp variables {r : α → β → Prop} {p : β → γ → Prop} {q : γ → δ → Prop} def comp (r : α → β → Prop) (p : β → γ → Prop) (a : α) (c : γ) : Prop := ∃b, r a b ∧ p b c local infixr ` ∘r ` : 80 := relation.comp lemma comp_eq : r ∘r (=) = r := funext $ assume a, funext $ assume b, propext $ iff.intro (assume ⟨c, h, eq⟩, eq ▸ h) (assume h, ⟨b, h, rfl⟩) lemma eq_comp : (=) ∘r r = r := funext $ assume a, funext $ assume b, propext $ iff.intro (assume ⟨c, eq, h⟩, eq.symm ▸ h) (assume h, ⟨a, rfl, h⟩) lemma iff_comp {r : Prop → α → Prop} : (↔) ∘r r = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, eq_comp] lemma comp_iff {r : α → Prop → Prop} : r ∘r (↔) = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, comp_eq] lemma comp_assoc : (r ∘r p) ∘r q = r ∘r p ∘r q := begin funext a d, apply propext, split, exact assume ⟨c, ⟨b, hab, hbc⟩, hcd⟩, ⟨b, hab, c, hbc, hcd⟩, exact assume ⟨b, hab, c, hbc, hcd⟩, ⟨c, ⟨b, hab, hbc⟩, hcd⟩ end lemma flip_comp : flip (r ∘r p) = (flip p) ∘r (flip r) := begin funext c a, apply propext, split, exact assume ⟨b, hab, hbc⟩, ⟨b, hbc, hab⟩, exact assume ⟨b, hbc, hab⟩, ⟨b, hab, hbc⟩ end end comp protected def map (r : α → β → Prop) (f : α → γ) (g : β → δ) : γ → δ → Prop := λc d, ∃a b, r a b ∧ f a = c ∧ g b = d variables {r : α → α → Prop} {a b c d : α} /-- `refl_trans_gen r`: reflexive transitive closure of `r` -/ inductive refl_trans_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_trans_gen a | tail {b c} : refl_trans_gen b → r b c → refl_trans_gen c attribute [refl] refl_trans_gen.refl mk_iff_of_inductive_prop relation.refl_trans_gen relation.refl_trans_gen.cases_tail_iff /-- `refl_gen r`: reflexive closure of `r` -/ inductive refl_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_gen a | single {b} : r a b → refl_gen b mk_iff_of_inductive_prop relation.refl_gen relation.refl_gen_iff /-- `trans_gen r`: transitive closure of `r` -/ inductive trans_gen (r : α → α → Prop) (a : α) : α → Prop | single {b} : r a b → trans_gen b | tail {b c} : trans_gen b → r b c → trans_gen c mk_iff_of_inductive_prop relation.trans_gen relation.trans_gen_iff attribute [refl] refl_gen.refl lemma refl_gen.to_refl_trans_gen : ∀{a b}, refl_gen r a b → refl_trans_gen r a b | a _ refl_gen.refl := by refl | a b (refl_gen.single h) := refl_trans_gen.tail refl_trans_gen.refl h namespace refl_trans_gen @[trans] lemma trans (hab : refl_trans_gen r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma single (hab : r a b) : refl_trans_gen r a b := refl.tail hab lemma head (hab : r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { exact refl.tail hab }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma symmetric (h : symmetric r) : symmetric (refl_trans_gen r) := begin intros x y h, induction h with z w a b c, { refl }, { apply relation.refl_trans_gen.head (h b) c } end lemma cases_tail : refl_trans_gen r a b → b = a ∨ (∃c, refl_trans_gen r a c ∧ r c b) := (cases_tail_iff r a b).1 lemma head_induction_on {P : ∀(a:α), refl_trans_gen r a b → Prop} {a : α} (h : refl_trans_gen r a b) (refl : P b refl) (head : ∀{a c} (h' : r a c) (h : refl_trans_gen r c b), P c h → P a (h.head h')) : P a h := begin induction h generalizing P, case refl_trans_gen.refl { exact refl }, case refl_trans_gen.tail : b c hab hbc ih { apply ih, show P b _, from head hbc _ refl, show ∀a a', r a a' → refl_trans_gen r a' b → P a' _ → P a _, from assume a a' hab hbc, head hab _ } end lemma trans_induction_on {P : ∀{a b : α}, refl_trans_gen r a b → Prop} {a b : α} (h : refl_trans_gen r a b) (ih₁ : ∀a, @P a a refl) (ih₂ : ∀{a b} (h : r a b), P (single h)) (ih₃ : ∀{a b c} (h₁ : refl_trans_gen r a b) (h₂ : refl_trans_gen r b c), P h₁ → P h₂ → P (h₁.trans h₂)) : P h := begin induction h, case refl_trans_gen.refl { exact ih₁ a }, case refl_trans_gen.tail : b c hab hbc ih { exact ih₃ hab (single hbc) ih (ih₂ hbc) } end lemma cases_head (h : refl_trans_gen r a b) : a = b ∨ (∃c, r a c ∧ refl_trans_gen r c b) := begin induction h using relation.refl_trans_gen.head_induction_on, { left, refl }, { right, existsi _, split; assumption } end lemma cases_head_iff : refl_trans_gen r a b ↔ a = b ∨ (∃c, r a c ∧ refl_trans_gen r c b) := begin split, { exact cases_head }, { assume h, rcases h with rfl | ⟨c, hac, hcb⟩, { refl }, { exact head hac hcb } } end lemma total_of_right_unique (U : relator.right_unique r) (ab : refl_trans_gen r a b) (ac : refl_trans_gen r a c) : refl_trans_gen r b c ∨ refl_trans_gen r c b := begin induction ab with b d ab bd IH, { exact or.inl ac }, { rcases IH with IH | IH, { rcases cases_head IH with rfl | ⟨e, be, ec⟩, { exact or.inr (single bd) }, { cases U bd be, exact or.inl ec } }, { exact or.inr (IH.tail bd) } } end end refl_trans_gen namespace trans_gen lemma to_refl {a b} (h : trans_gen r a b) : refl_trans_gen r a b := begin induction h with b h b c _ bc ab, exact refl_trans_gen.single h, exact refl_trans_gen.tail ab bc end @[trans] lemma trans_left (hab : trans_gen r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := begin induction hbc, case refl_trans_gen.refl : c hab { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end @[trans] lemma trans (hab : trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := trans_left hab hbc.to_refl lemma head' (hab : r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := trans_left (single hab) hbc lemma tail' (hab : refl_trans_gen r a b) (hbc : r b c) : trans_gen r a c := begin induction hab generalizing c, case refl_trans_gen.refl : c hac { exact single hac }, case refl_trans_gen.tail : d b hab hdb IH { exact tail (IH hdb) hbc } end @[trans] lemma trans_right (hab : refl_trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := begin induction hbc, case trans_gen.single : c hbc { exact tail' hab hbc }, case trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma head (hab : r a b) (hbc : trans_gen r b c) : trans_gen r a c := head' hab hbc.to_refl lemma tail'_iff : trans_gen r a c ↔ ∃ b, refl_trans_gen r a b ∧ r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, tail' hab hbc⟩, cases h with _ hac b _ hab hbc, { exact ⟨_, by refl, hac⟩ }, { exact ⟨_, hab.to_refl, hbc⟩ } end lemma head'_iff : trans_gen r a c ↔ ∃ b, r a b ∧ refl_trans_gen r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, head' hab hbc⟩, induction h, case trans_gen.single : c hac { exact ⟨_, hac, by refl⟩ }, case trans_gen.tail : b c hab hbc IH { rcases IH with ⟨d, had, hdb⟩, exact ⟨_, had, hdb.tail hbc⟩ } end end trans_gen section refl_trans_gen open refl_trans_gen lemma refl_trans_gen_iff_eq (h : ∀b, ¬ r a b) : refl_trans_gen r a b ↔ b = a := by rw [cases_head_iff]; simp [h, eq_comm] lemma refl_trans_gen_iff_eq_or_trans_gen : refl_trans_gen r a b ↔ b = a ∨ trans_gen r a b := begin refine ⟨λ h, _, λ h, _⟩, { cases h with c _ hac hcb, { exact or.inl rfl }, { exact or.inr (trans_gen.tail' hac hcb) } }, { rcases h with rfl | h, {refl}, {exact h.to_refl} } end lemma refl_trans_gen_lift {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀a b, r a b → p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := hab.trans_induction_on (assume a, refl) (assume a b, refl_trans_gen.single ∘ h _ _) (assume a b c _ _, trans) lemma refl_trans_gen_mono {p : α → α → Prop} : (∀a b, r a b → p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen_lift id lemma refl_trans_gen_eq_self (refl : reflexive r) (trans : transitive r) : refl_trans_gen r = r := funext $ λ a, funext $ λ b, propext $ ⟨λ h, begin induction h with b c h₁ h₂ IH, {apply refl}, exact trans IH h₂, end, single⟩ lemma reflexive_refl_trans_gen : reflexive (refl_trans_gen r) := assume a, refl lemma transitive_refl_trans_gen : transitive (refl_trans_gen r) := assume a b c, trans lemma refl_trans_gen_idem : refl_trans_gen (refl_trans_gen r) = refl_trans_gen r := refl_trans_gen_eq_self reflexive_refl_trans_gen transitive_refl_trans_gen lemma refl_trans_gen_lift' {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀a b, r a b → refl_trans_gen p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := by simpa [refl_trans_gen_idem] using refl_trans_gen_lift f h hab lemma refl_trans_gen_closed {p : α → α → Prop} : (∀ a b, r a b → refl_trans_gen p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen_lift' id end refl_trans_gen def join (r : α → α → Prop) : α → α → Prop := λa b, ∃c, r a c ∧ r b c section join open refl_trans_gen refl_gen lemma church_rosser (h : ∀a b c, r a b → r a c → ∃d, refl_gen r b d ∧ refl_trans_gen r c d) (hab : refl_trans_gen r a b) (hac : refl_trans_gen r a c) : join (refl_trans_gen r) b c := begin induction hab, case refl_trans_gen.refl { exact ⟨c, hac, refl⟩ }, case refl_trans_gen.tail : d e had hde ih { clear hac had a, rcases ih with ⟨b, hdb, hcb⟩, have : ∃a, refl_trans_gen r e a ∧ refl_gen r b a, { clear hcb, induction hdb, case refl_trans_gen.refl { exact ⟨e, refl, refl_gen.single hde⟩ }, case refl_trans_gen.tail : f b hdf hfb ih { rcases ih with ⟨a, hea, hfa⟩, cases hfa with _ hfa, { exact ⟨b, hea.tail hfb, refl_gen.refl⟩ }, { rcases h _ _ _ hfb hfa with ⟨c, hbc, hac⟩, exact ⟨c, hea.trans hac, hbc⟩ } } }, rcases this with ⟨a, hea, hba⟩, cases hba with _ hba, { exact ⟨b, hea, hcb⟩ }, { exact ⟨a, hea, hcb.tail hba⟩ } } end lemma join_of_single (h : reflexive r) (hab : r a b) : join r a b := ⟨b, hab, h b⟩ lemma symmetric_join : symmetric (join r) := assume a b ⟨c, hac, hcb⟩, ⟨c, hcb, hac⟩ lemma reflexive_join (h : reflexive r) : reflexive (join r) := assume a, ⟨a, h a, h a⟩ lemma transitive_join (ht : transitive r) (h : ∀a b c, r a b → r a c → join r b c) : transitive (join r) := assume a b c ⟨x, hax, hbx⟩ ⟨y, hby, hcy⟩, let ⟨z, hxz, hyz⟩ := h b x y hbx hby in ⟨z, ht hax hxz, ht hcy hyz⟩ lemma equivalence_join (hr : reflexive r) (ht : transitive r) (h : ∀a b c, r a b → r a c → join r b c) : equivalence (join r) := ⟨reflexive_join hr, symmetric_join, transitive_join ht h⟩ lemma equivalence_join_refl_trans_gen (h : ∀a b c, r a b → r a c → ∃d, refl_gen r b d ∧ refl_trans_gen r c d) : equivalence (join (refl_trans_gen r)) := equivalence_join reflexive_refl_trans_gen transitive_refl_trans_gen (assume a b c, church_rosser h) lemma join_of_equivalence {r' : α → α → Prop} (hr : equivalence r) (h : ∀a b, r' a b → r a b) : join r' a b → r a b | ⟨c, hac, hbc⟩ := hr.2.2 (h _ _ hac) (hr.2.1 $ h _ _ hbc) lemma refl_trans_gen_of_transitive_reflexive {r' : α → α → Prop} (hr : reflexive r) (ht : transitive r) (h : ∀a b, r' a b → r a b) (h' : refl_trans_gen r' a b) : r a b := begin induction h' with b c hab hbc ih, { exact hr _ }, { exact ht ih (h _ _ hbc) } end lemma refl_trans_gen_of_equivalence {r' : α → α → Prop} (hr : equivalence r) : (∀a b, r' a b → r a b) → refl_trans_gen r' a b → r a b := refl_trans_gen_of_transitive_reflexive hr.1 hr.2.2 end join section eqv_gen lemma eqv_gen_iff_of_equivalence (h : equivalence r) : eqv_gen r a b ↔ r a b := iff.intro begin assume h, induction h, case eqv_gen.rel { assumption }, case eqv_gen.refl { exact h.1 _ }, case eqv_gen.symm { apply h.2.1, assumption }, case eqv_gen.trans : a b c _ _ hab hbc { exact h.2.2 hab hbc } end (eqv_gen.rel a b) lemma eqv_gen_mono {r p : α → α → Prop} (hrp : ∀a b, r a b → p a b) (h : eqv_gen r a b) : eqv_gen p a b := begin induction h, case eqv_gen.rel : a b h { exact eqv_gen.rel _ _ (hrp _ _ h) }, case eqv_gen.refl : { exact eqv_gen.refl _ }, case eqv_gen.symm : a b h ih { exact eqv_gen.symm _ _ ih }, case eqv_gen.trans : a b c ih1 ih2 hab hbc { exact eqv_gen.trans _ _ _ hab hbc } end end eqv_gen end relation
39fed535b8ab068a48e3b822a2c434293723985c
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/big_operators/intervals.lean
ac3699ea592de502902d547d6eeee33fb44d42d5
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
10,487
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import algebra.big_operators.basic import algebra.module.basic import data.nat.interval import tactic.linarith /-! # Results about big operators over intervals We prove results about big operators over intervals (mostly the `ℕ`-valued `Ico m n`). -/ universes u v w open_locale big_operators nat namespace finset section generic variables {α : Type u} {β : Type v} {γ : Type w} {s₂ s₁ s : finset α} {a : α} {g f : α → β} variables [comm_monoid β] @[to_additive] lemma prod_Ico_add' [ordered_cancel_add_comm_monoid α] [has_exists_add_of_le α] [locally_finite_order α] (f : α → β) (a b c : α) : (∏ x in Ico a b, f (x + c)) = (∏ x in Ico (a + c) (b + c), f x) := by { rw [← map_add_right_Ico, prod_map], refl } @[to_additive] lemma prod_Ico_add [ordered_cancel_add_comm_monoid α] [has_exists_add_of_le α] [locally_finite_order α] (f : α → β) (a b c : α) : (∏ x in Ico a b, f (c + x)) = (∏ x in Ico (a + c) (b + c), f x) := begin convert prod_Ico_add' f a b c, simp_rw add_comm, end lemma sum_Ico_succ_top {δ : Type*} [add_comm_monoid δ] {a b : ℕ} (hab : a ≤ b) (f : ℕ → δ) : (∑ k in Ico a (b + 1), f k) = (∑ k in Ico a b, f k) + f b := by rw [nat.Ico_succ_right_eq_insert_Ico hab, sum_insert right_not_mem_Ico, add_comm] @[to_additive] lemma prod_Ico_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → β) : (∏ k in Ico a (b + 1), f k) = (∏ k in Ico a b, f k) * f b := @sum_Ico_succ_top (additive β) _ _ _ hab _ lemma sum_eq_sum_Ico_succ_bot {δ : Type*} [add_comm_monoid δ] {a b : ℕ} (hab : a < b) (f : ℕ → δ) : (∑ k in Ico a b, f k) = f a + (∑ k in Ico (a + 1) b, f k) := have ha : a ∉ Ico (a + 1) b, by simp, by rw [← sum_insert ha, nat.Ico_insert_succ_left hab] @[to_additive] lemma prod_eq_prod_Ico_succ_bot {a b : ℕ} (hab : a < b) (f : ℕ → β) : (∏ k in Ico a b, f k) = f a * (∏ k in Ico (a + 1) b, f k) := @sum_eq_sum_Ico_succ_bot (additive β) _ _ _ hab _ @[to_additive] lemma prod_Ico_consecutive (f : ℕ → β) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : (∏ i in Ico m n, f i) * (∏ i in Ico n k, f i) = (∏ i in Ico m k, f i) := Ico_union_Ico_eq_Ico hmn hnk ▸ eq.symm $ prod_union $ Ico_disjoint_Ico_consecutive m n k @[to_additive] lemma prod_Ioc_consecutive (f : ℕ → β) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : (∏ i in Ioc m n, f i) * (∏ i in Ioc n k, f i) = (∏ i in Ioc m k, f i) := begin rw [← Ioc_union_Ioc_eq_Ioc hmn hnk, prod_union], apply disjoint_left.2 (λ x hx h'x, _), exact lt_irrefl _ ((mem_Ioc.1 h'x).1.trans_le (mem_Ioc.1 hx).2), end @[to_additive] lemma prod_Ioc_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → β) : (∏ k in Ioc a (b + 1), f k) = (∏ k in Ioc a b, f k) * f (b + 1) := by rw [← prod_Ioc_consecutive _ hab (nat.le_succ b), nat.Ioc_succ_singleton, prod_singleton] @[to_additive] lemma prod_range_mul_prod_Ico (f : ℕ → β) {m n : ℕ} (h : m ≤ n) : (∏ k in range m, f k) * (∏ k in Ico m n, f k) = (∏ k in range n, f k) := nat.Ico_zero_eq_range ▸ nat.Ico_zero_eq_range ▸ prod_Ico_consecutive f m.zero_le h @[to_additive] lemma prod_Ico_eq_mul_inv {δ : Type*} [comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (∏ k in Ico m n, f k) = (∏ k in range n, f k) * (∏ k in range m, f k)⁻¹ := eq_mul_inv_iff_mul_eq.2 $ by rw [mul_comm]; exact prod_range_mul_prod_Ico f h @[to_additive] lemma prod_Ico_eq_div {δ : Type*} [comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (∏ k in Ico m n, f k) = (∏ k in range n, f k) / (∏ k in range m, f k) := by simpa only [div_eq_mul_inv] using prod_Ico_eq_mul_inv f h @[to_additive] lemma prod_range_sub_prod_range {α : Type*} [comm_group α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : (∏ k in range m, f k) / (∏ k in range n, f k) = ∏ k in (range m).filter (λ k, n ≤ k), f k := begin rw [← prod_Ico_eq_div f hnm], congr, apply finset.ext, simp only [mem_Ico, mem_filter, mem_range, *], tauto, end /-- The two ways of summing over `(i,j)` in the range `a<=i<=j<b` are equal. -/ lemma sum_Ico_Ico_comm {M : Type*} [add_comm_monoid M] (a b : ℕ) (f : ℕ → ℕ → M) : ∑ i in finset.Ico a b, ∑ j in finset.Ico i b, f i j = ∑ j in finset.Ico a b, ∑ i in finset.Ico a (j+1), f i j := begin rw [finset.sum_sigma', finset.sum_sigma'], refine finset.sum_bij' (λ (x : Σ (i : ℕ), ℕ) _, (⟨x.2, x.1⟩ : Σ (i : ℕ), ℕ)) _ (λ _ _, rfl) (λ (x : Σ (i : ℕ), ℕ) _, (⟨x.2, x.1⟩ : Σ (i : ℕ), ℕ)) _ (by rintro ⟨⟩ _; refl) (by rintro ⟨⟩ _; refl); simp only [finset.mem_Ico, sigma.forall, finset.mem_sigma]; rintros a b ⟨⟨h₁,h₂⟩, ⟨h₃, h₄⟩⟩; refine ⟨⟨_, _⟩, ⟨_, _⟩⟩; linarith end @[to_additive] lemma prod_Ico_eq_prod_range (f : ℕ → β) (m n : ℕ) : (∏ k in Ico m n, f k) = (∏ k in range (n - m), f (m + k)) := begin by_cases h : m ≤ n, { rw [←nat.Ico_zero_eq_range, prod_Ico_add, zero_add, tsub_add_cancel_of_le h] }, { replace h : n ≤ m := le_of_not_ge h, rw [Ico_eq_empty_of_le h, tsub_eq_zero_iff_le.mpr h, range_zero, prod_empty, prod_empty] } end lemma prod_Ico_reflect (f : ℕ → β) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : ∏ j in Ico k m, f (n - j) = ∏ j in Ico (n + 1 - m) (n + 1 - k), f j := begin have : ∀ i < m, i ≤ n, { intros i hi, exact (add_le_add_iff_right 1).1 (le_trans (nat.lt_iff_add_one_le.1 hi) h) }, cases lt_or_le k m with hkm hkm, { rw [← nat.Ico_image_const_sub_eq_Ico (this _ hkm)], refine (prod_image _).symm, simp only [mem_Ico], rintros i ⟨ki, im⟩ j ⟨kj, jm⟩ Hij, rw [← tsub_tsub_cancel_of_le (this _ im), Hij, tsub_tsub_cancel_of_le (this _ jm)] }, { simp [Ico_eq_empty_of_le, tsub_le_tsub_left, hkm] } end lemma sum_Ico_reflect {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : ∑ j in Ico k m, f (n - j) = ∑ j in Ico (n + 1 - m) (n + 1 - k), f j := @prod_Ico_reflect (multiplicative δ) _ f k m n h lemma prod_range_reflect (f : ℕ → β) (n : ℕ) : ∏ j in range n, f (n - 1 - j) = ∏ j in range n, f j := begin cases n, { simp }, { simp only [←nat.Ico_zero_eq_range, nat.succ_sub_succ_eq_sub, tsub_zero], rw prod_Ico_reflect _ _ le_rfl, simp } end lemma sum_range_reflect {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (n : ℕ) : ∑ j in range n, f (n - 1 - j) = ∑ j in range n, f j := @prod_range_reflect (multiplicative δ) _ f n @[simp] lemma prod_Ico_id_eq_factorial : ∀ n : ℕ, ∏ x in Ico 1 (n + 1), x = n! | 0 := rfl | (n+1) := by rw [prod_Ico_succ_top $ nat.succ_le_succ $ zero_le n, nat.factorial_succ, prod_Ico_id_eq_factorial n, nat.succ_eq_add_one, mul_comm] @[simp] lemma prod_range_add_one_eq_factorial : ∀ n : ℕ, ∏ x in range n, (x+1) = n! | 0 := rfl | (n+1) := by simp [finset.range_succ, prod_range_add_one_eq_factorial n] section gauss_sum /-- Gauss' summation formula -/ lemma sum_range_id_mul_two (n : ℕ) : (∑ i in range n, i) * 2 = n * (n - 1) := calc (∑ i in range n, i) * 2 = (∑ i in range n, i) + (∑ i in range n, (n - 1 - i)) : by rw [sum_range_reflect (λ i, i) n, mul_two] ... = ∑ i in range n, (i + (n - 1 - i)) : sum_add_distrib.symm ... = ∑ i in range n, (n - 1) : sum_congr rfl $ λ i hi, add_tsub_cancel_of_le $ nat.le_pred_of_lt $ mem_range.1 hi ... = n * (n - 1) : by rw [sum_const, card_range, nat.nsmul_eq_mul] /-- Gauss' summation formula -/ lemma sum_range_id (n : ℕ) : (∑ i in range n, i) = (n * (n - 1)) / 2 := by rw [← sum_range_id_mul_two n, nat.mul_div_cancel]; exact dec_trivial end gauss_sum end generic section nat variable {β : Type*} variables (f g : ℕ → β) {m n : ℕ} section group variable [comm_group β] @[to_additive] lemma prod_range_succ_div_prod : (∏ i in range (n+1), f i) / ∏ i in range n, f i = f n := div_eq_iff_eq_mul'.mpr $ prod_range_succ f n @[to_additive] lemma prod_range_succ_div_top : (∏ i in range (n+1), f i) / f n = ∏ i in range n, f i := div_eq_iff_eq_mul.mpr $ prod_range_succ f n @[to_additive] lemma prod_Ico_div_bot (hmn : m < n) : (∏ i in Ico m n, f i) / f m = ∏ i in Ico (m+1) n, f i := div_eq_iff_eq_mul'.mpr $ prod_eq_prod_Ico_succ_bot hmn _ @[to_additive] lemma prod_Ico_succ_div_top (hmn : m ≤ n) : (∏ i in Ico m (n+1), f i) / f n = ∏ i in Ico m n, f i := div_eq_iff_eq_mul.mpr $ prod_Ico_succ_top hmn _ end group end nat section module variables {R M : Type*} [ring R] [add_comm_group M] [module R M] (f : ℕ → R) (g : ℕ → M) {m n : ℕ} open finset -- The partial sum of `g`, starting from zero local notation `G ` n:80 := ∑ i in range n, g i /-- **Summation by parts**, also known as **Abel's lemma** or an **Abel transformation** -/ theorem sum_Ico_by_parts (hmn : m < n) : ∑ i in Ico m n, f i • g i = f (n-1) • G n - f m • G m - ∑ i in Ico m (n-1), (f (i+1) - f i) • G (i+1) := begin have h₁ : ∑ i in Ico (m+1) n, (f i • G i) = ∑ i in Ico m (n-1), (f (i+1) • G (i+1)), { conv in n { rw ←nat.sub_add_cancel (nat.one_le_of_lt hmn) }, rw ←sum_Ico_add' }, have h₂ : ∑ i in Ico (m+1) n, (f i • G (i+1)) = ∑ i in Ico m (n-1), (f i • G (i+1)) + f (n-1) • G n - f m • G (m+1) := by rw [←sum_Ico_sub_bot _ hmn, ←sum_Ico_succ_sub_top _ (nat.le_pred_of_lt hmn), nat.sub_add_cancel (pos_of_gt hmn), sub_add_cancel], rw sum_eq_sum_Ico_succ_bot hmn, conv { for (f _ • g _) [2] { rw ← sum_range_succ_sub_sum g } }, simp_rw [smul_sub, sum_sub_distrib, h₂, h₁], conv_lhs { congr, skip, rw [←add_sub, add_comm, ←add_sub, ←sum_sub_distrib] }, have : ∀ i, f i • G (i+1) - f (i+1) • G (i+1) = -((f (i+1) - f i) • G (i+1)), { intro i, rw sub_smul, abel }, simp_rw [this, sum_neg_distrib, sum_range_succ, smul_add], abel, end variable (n) /-- **Summation by parts** for ranges -/ lemma sum_range_by_parts : ∑ i in range n, (f i • g i) = f (n-1) • G n - ∑ i in range (n-1), (f (i+1) - f i) • G (i+1) := begin by_cases hn : n = 0, { simp [hn], }, { rw [range_eq_Ico, sum_Ico_by_parts f g (nat.pos_of_ne_zero hn), sum_range_zero, smul_zero, sub_zero, range_eq_Ico] }, end end module end finset
a28aeb983a1e7d92a0ab6bf2a0a516e55d3e7c23
367134ba5a65885e863bdc4507601606690974c1
/src/data/nat/with_bot.lean
7c369ce1d50c2108265a04fe53d404ab28fda870
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
1,854
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.nat.basic import algebra.ordered_group /-! # `with_bot ℕ` Lemmas about the type of natural numbers with a bottom element adjoined. -/ namespace nat lemma with_bot.add_eq_zero_iff : ∀ {n m : with_bot ℕ}, n + m = 0 ↔ n = 0 ∧ m = 0 | none m := iff_of_false dec_trivial (λ h, absurd h.1 dec_trivial) | n none := iff_of_false (by cases n; exact dec_trivial) (λ h, absurd h.2 dec_trivial) | (some n) (some m) := show (n + m : with_bot ℕ) = (0 : ℕ) ↔ (n : with_bot ℕ) = (0 : ℕ) ∧ (m : with_bot ℕ) = (0 : ℕ), by rw [← with_bot.coe_add, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, add_eq_zero_iff' (nat.zero_le _) (nat.zero_le _)] lemma with_bot.add_eq_one_iff : ∀ {n m : with_bot ℕ}, n + m = 1 ↔ (n = 0 ∧ m = 1) ∨ (n = 1 ∧ m = 0) | none none := dec_trivial | none (some m) := dec_trivial | (some n) none := iff_of_false dec_trivial (λ h, h.elim (λ h, absurd h.2 dec_trivial) (λ h, absurd h.2 dec_trivial)) | (some n) (some 0) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp | (some n) (some (m + 1)) := by erw [with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe, with_bot.coe_eq_coe]; simp [nat.add_succ, nat.succ_inj', nat.succ_ne_zero] @[simp] lemma with_bot.coe_nonneg {n : ℕ} : 0 ≤ (n : with_bot ℕ) := by rw [← with_bot.coe_zero, with_bot.coe_le_coe]; exact nat.zero_le _ @[simp] lemma with_bot.lt_zero_iff (n : with_bot ℕ) : n < 0 ↔ n = ⊥ := option.cases_on n dec_trivial (λ n, iff_of_false (by simp [with_bot.some_eq_coe]) (λ h, option.no_confusion h)) end nat
9bda8bede1ae4f25cdc1b8e00b2dee33db8d3501
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/deprecated/submonoid_auto.lean
5a3a0a336fbf89f68f7b32b93a4d36c61d8b7122
[]
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,862
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, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.group_theory.submonoid.basic import Mathlib.algebra.big_operators.basic import Mathlib.PostPort universes u_2 l u_1 namespace Mathlib /-! # Submonoids This file defines unbundled multiplicative and additive submonoids (deprecated). For bundled form see `group_theory/submonoid`. We some results about images and preimages of submonoids under monoid homomorphisms. These theorems use unbundled monoid homomorphisms (also deprecated). There are also theorems about the submonoids generated by an element or a subset of a monoid, defined inductively. ## Implementation notes Unbundled submonoids will slowly be removed from mathlib. ## Tags submonoid, submonoids, is_submonoid -/ /-- `s` is an additive submonoid: a set containing 0 and closed under addition. -/ class is_add_submonoid {A : Type u_2} [add_monoid A] (s : set A) where zero_mem : 0 ∈ s add_mem : ∀ {a b : A}, a ∈ s → b ∈ s → a + b ∈ s /-- `s` is a submonoid: a set containing 1 and closed under multiplication. -/ class is_submonoid {M : Type u_1} [monoid M] (s : set M) where one_mem : 1 ∈ s mul_mem : ∀ {a b : M}, a ∈ s → b ∈ s → a * b ∈ s theorem additive.is_add_submonoid {M : Type u_1} [monoid M] (s : set M) [is_submonoid s] : is_add_submonoid s := is_submonoid.dcases_on _inst_3 fun (one_mem : 1 ∈ s) (mul_mem : ∀ {a b : M}, a ∈ s → b ∈ s → a * b ∈ s) => idRhs (is_add_submonoid s) (is_add_submonoid.mk one_mem mul_mem) theorem additive.is_add_submonoid_iff {M : Type u_1} [monoid M] {s : set M} : is_add_submonoid s ↔ is_submonoid s := sorry theorem multiplicative.is_submonoid {A : Type u_2} [add_monoid A] (s : set A) [is_add_submonoid s] : is_submonoid s := is_add_submonoid.dcases_on _inst_3 fun (zero_mem : 0 ∈ s) (add_mem : ∀ {a b : A}, a ∈ s → b ∈ s → a + b ∈ s) => idRhs (is_submonoid s) (is_submonoid.mk zero_mem add_mem) theorem multiplicative.is_submonoid_iff {A : Type u_2} [add_monoid A] {s : set A} : is_submonoid s ↔ is_add_submonoid s := sorry /-- The intersection of two submonoids of a monoid `M` is a submonoid of `M`. -/ protected instance is_submonoid.inter {M : Type u_1} [monoid M] (s₁ : set M) (s₂ : set M) [is_submonoid s₁] [is_submonoid s₂] : is_submonoid (s₁ ∩ s₂) := is_submonoid.mk { left := is_submonoid.one_mem, right := is_submonoid.one_mem } fun (x y : M) (hx : x ∈ s₁ ∩ s₂) (hy : y ∈ s₁ ∩ s₂) => { left := is_submonoid.mul_mem (and.left hx) (and.left hy), right := is_submonoid.mul_mem (and.right hx) (and.right hy) } /-- The intersection of an indexed set of submonoids of a monoid `M` is a submonoid of `M`. -/ protected instance is_add_submonoid.Inter {M : Type u_1} [add_monoid M] {ι : Sort u_2} (s : ι → set M) [h : ∀ (y : ι), is_add_submonoid (s y)] : is_add_submonoid (set.Inter s) := is_add_submonoid.mk (iff.mpr set.mem_Inter fun (y : ι) => is_add_submonoid.zero_mem) fun (x₁ x₂ : M) (h₁ : x₁ ∈ set.Inter s) (h₂ : x₂ ∈ set.Inter s) => iff.mpr set.mem_Inter fun (y : ι) => is_add_submonoid.add_mem (iff.mp set.mem_Inter h₁ y) (iff.mp set.mem_Inter h₂ y) /-- The union of an indexed, directed, nonempty set of submonoids of a monoid `M` is a submonoid of `M`. -/ theorem is_add_submonoid_Union_of_directed {M : Type u_1} [add_monoid M] {ι : Type u_2} [hι : Nonempty ι] (s : ι → set M) [∀ (i : ι), is_add_submonoid (s i)] (directed : ∀ (i j : ι), ∃ (k : ι), s i ⊆ s k ∧ s j ⊆ s k) : is_add_submonoid (set.Union fun (i : ι) => s i) := sorry /-- The set of natural number powers `1, x, x², ...` of an element `x` of a monoid. -/ /-- The set of natural number multiples `0, x, 2x, ...` of an element `x` of an `add_monoid`. -/ def powers {M : Type u_1} [monoid M] (x : M) : set M := set_of fun (y : M) => ∃ (n : ℕ), x ^ n = y def multiples {A : Type u_2} [add_monoid A] (x : A) : set A := set_of fun (y : A) => ∃ (n : ℕ), n •ℕ x = y /-- 1 is in the set of natural number powers of an element of a monoid. -/ theorem powers.one_mem {M : Type u_1} [monoid M] {x : M} : 1 ∈ powers x := Exists.intro 0 (pow_zero x) /-- 0 is in the set of natural number multiples of an element of an `add_monoid`. -/ theorem multiples.zero_mem {A : Type u_2} [add_monoid A] {x : A} : 0 ∈ multiples x := Exists.intro 0 (zero_nsmul x) /-- An element of a monoid is in the set of that element's natural number powers. -/ theorem powers.self_mem {M : Type u_1} [monoid M] {x : M} : x ∈ powers x := Exists.intro 1 (pow_one x) /-- An element of an `add_monoid` is in the set of that element's natural number multiples. -/ theorem multiples.self_mem {A : Type u_2} [add_monoid A] {x : A} : x ∈ multiples x := Exists.intro 1 (one_nsmul x) /-- The set of natural number powers of an element of a monoid is closed under multiplication. -/ theorem powers.mul_mem {M : Type u_1} [monoid M] {x : M} {y : M} {z : M} : y ∈ powers x → z ∈ powers x → y * z ∈ powers x := sorry /-- The set of natural number multiples of an element of an `add_monoid` is closed under addition. -/ theorem multiples.add_mem {A : Type u_2} [add_monoid A] {x : A} {y : A} {z : A} : y ∈ multiples x → z ∈ multiples x → y + z ∈ multiples x := powers.mul_mem /-- The set of natural number powers of an element of a monoid `M` is a submonoid of `M`. -/ protected instance multiples.is_add_submonoid {M : Type u_1} [add_monoid M] (x : M) : is_add_submonoid (multiples x) := is_add_submonoid.mk multiples.zero_mem fun (y z : M) => multiples.add_mem /-- A monoid is a submonoid of itself. -/ protected instance univ.is_submonoid {M : Type u_1} [monoid M] : is_submonoid set.univ := is_submonoid.mk (eq.mpr (id (propext ((fun {α : Type u_1} (x : α) => iff_true_intro (set.mem_univ x)) 1))) trivial) (eq.mpr (id (Eq.trans (Eq.trans (forall_congr_eq fun (a : M) => forall_congr_eq fun (b : M) => Eq.trans (imp_congr_eq (propext ((fun {α : Type u_1} (x : α) => iff_true_intro (set.mem_univ x)) a)) (Eq.trans (imp_congr_eq (propext ((fun {α : Type u_1} (x : α) => iff_true_intro (set.mem_univ x)) b)) (propext ((fun {α : Type u_1} (x : α) => iff_true_intro (set.mem_univ x)) (a * b)))) (propext (forall_prop_of_true True.intro)))) (propext (forall_prop_of_true True.intro))) (propext (forall_const M))) (propext (forall_const M)))) trivial) /-- The preimage of a submonoid under a monoid hom is a submonoid of the domain. -/ protected instance preimage.is_add_submonoid {M : Type u_1} [add_monoid M] {N : Type u_2} [add_monoid N] (f : M → N) [is_add_monoid_hom f] (s : set N) [is_add_submonoid s] : is_add_submonoid (f ⁻¹' s) := is_add_submonoid.mk ((fun (this : f 0 ∈ s) => this) (eq.mpr (id (Eq._oldrec (Eq.refl (f 0 ∈ s)) (is_add_monoid_hom.map_zero f))) is_add_submonoid.zero_mem)) fun (a b : M) (ha : f a ∈ s) (hb : f b ∈ s) => (fun (this : f (a + b) ∈ s) => this) (eq.mpr (id (Eq._oldrec (Eq.refl (f (a + b) ∈ s)) (is_add_monoid_hom.map_add f a b))) (is_add_submonoid.add_mem ha hb)) /-- The image of a submonoid under a monoid hom is a submonoid of the codomain. -/ instance image.is_add_submonoid {M : Type u_1} [add_monoid M] {γ : Type u_2} [add_monoid γ] (f : M → γ) [is_add_monoid_hom f] (s : set M) [is_add_submonoid s] : is_add_submonoid (f '' s) := sorry /-- The image of a monoid hom is a submonoid of the codomain. -/ protected instance range.is_add_submonoid {M : Type u_1} [add_monoid M] {γ : Type u_2} [add_monoid γ] (f : M → γ) [is_add_monoid_hom f] : is_add_submonoid (set.range f) := eq.mpr (id (Eq._oldrec (Eq.refl (is_add_submonoid (set.range f))) (Eq.symm set.image_univ))) (image.is_add_submonoid f set.univ) /-- Submonoids are closed under natural powers. -/ theorem is_submonoid.pow_mem {M : Type u_1} [monoid M] {s : set M} {a : M} [is_submonoid s] (h : a ∈ s) {n : ℕ} : a ^ n ∈ s := sorry /-- An `add_submonoid` is closed under multiplication by naturals. -/ theorem is_add_submonoid.smul_mem {A : Type u_2} [add_monoid A] {t : set A} {a : A} [is_add_submonoid t] (h : a ∈ t) {n : ℕ} : n •ℕ a ∈ t := is_submonoid.pow_mem /-- The set of natural number powers of an element of a submonoid is a subset of the submonoid. -/ theorem is_submonoid.power_subset {M : Type u_1} [monoid M] {s : set M} {a : M} [is_submonoid s] (h : a ∈ s) : powers a ⊆ s := sorry /-- The set of natural number multiples of an element of an `add_submonoid` is a subset of the `add_submonoid`. -/ theorem is_add_submonoid.multiple_subset {A : Type u_2} [add_monoid A] {t : set A} {a : A} [is_add_submonoid t] : a ∈ t → multiples a ⊆ t := is_submonoid.power_subset namespace is_submonoid /-- The product of a list of elements of a submonoid is an element of the submonoid. -/ theorem list_prod_mem {M : Type u_1} [monoid M] {s : set M} [is_submonoid s] {l : List M} : (∀ (x : M), x ∈ l → x ∈ s) → list.prod l ∈ s := sorry /-- The product of a multiset of elements of a submonoid of a `comm_monoid` is an element of the submonoid. -/ theorem Mathlib.is_add_submonoid.multiset_sum_mem {M : Type u_1} [add_comm_monoid M] (s : set M) [is_add_submonoid s] (m : multiset M) : (∀ (a : M), a ∈ m → a ∈ s) → multiset.sum m ∈ s := sorry /-- The product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is an element of the submonoid. -/ theorem finset_prod_mem {M : Type u_1} {A : Type u_2} [comm_monoid M] (s : set M) [is_submonoid s] (f : A → M) (t : finset A) : (∀ (b : A), b ∈ t → f b ∈ s) → (finset.prod t fun (b : A) => f b) ∈ s := sorry end is_submonoid -- TODO: modify `subtype_instance` to produce this definition, then use it here -- and for `subtype.group` /-- Submonoids are themselves monoids. -/ def subtype.add_monoid {M : Type u_1} [add_monoid M] {s : set M} [is_add_submonoid s] : add_monoid ↥s := add_monoid.mk (fun (x y : ↥s) => { val := ↑x + ↑y, property := sorry }) sorry { val := 0, property := is_add_submonoid.zero_mem } sorry sorry /-- Submonoids of commutative monoids are themselves commutative monoids. -/ def subtype.add_comm_monoid {M : Type u_1} [add_comm_monoid M] {s : set M} [is_add_submonoid s] : add_comm_monoid ↥s := add_comm_monoid.mk add_monoid.add sorry add_monoid.zero sorry sorry sorry /-- Submonoids inherit the 1 of the monoid. -/ @[simp] theorem is_submonoid.coe_one {M : Type u_1} [monoid M] {s : set M} [is_submonoid s] : ↑1 = 1 := rfl /-- Submonoids inherit the multiplication of the monoid. -/ @[simp] theorem is_add_submonoid.coe_add {M : Type u_1} [add_monoid M] {s : set M} [is_add_submonoid s] (a : ↥s) (b : ↥s) : ↑(a + b) = ↑a + ↑b := rfl /-- Submonoids inherit the exponentiation by naturals of the monoid. -/ @[simp] theorem is_submonoid.coe_pow {M : Type u_1} [monoid M] {s : set M} [is_submonoid s] (a : ↥s) (n : ℕ) : ↑(a ^ n) = ↑a ^ n := sorry /-- An `add_submonoid` inherits the multiplication by naturals of the `add_monoid`. -/ @[simp] theorem is_add_submonoid.smul_coe {A : Type u_1} [add_monoid A] {s : set A} [is_add_submonoid s] (a : ↥s) (n : ℕ) : ↑(n •ℕ a) = n •ℕ ↑a := sorry /-- The natural injection from a submonoid into the monoid is a monoid hom. -/ protected instance subtype_val.is_add_monoid_hom {M : Type u_1} [add_monoid M] {s : set M} [is_add_submonoid s] : is_add_monoid_hom subtype.val := is_add_monoid_hom.mk rfl /-- The natural injection from a submonoid into the monoid is a monoid hom. -/ protected instance coe.is_add_monoid_hom {M : Type u_1} [add_monoid M] {s : set M} [is_add_submonoid s] : is_add_monoid_hom coe := subtype_val.is_add_monoid_hom /-- Given a monoid hom `f : γ → M` whose image is contained in a submonoid `s`, the induced map from `γ` to `s` is a monoid hom. -/ protected instance subtype_mk.is_add_monoid_hom {M : Type u_1} [add_monoid M] {s : set M} {γ : Type u_2} [add_monoid γ] [is_add_submonoid s] (f : γ → M) [is_add_monoid_hom f] (h : ∀ (x : γ), f x ∈ s) : is_add_monoid_hom fun (x : γ) => { val := f x, property := h x } := is_add_monoid_hom.mk (subtype.eq (is_add_monoid_hom.map_zero f)) /-- Given two submonoids `s` and `t` such that `s ⊆ t`, the natural injection from `s` into `t` is a monoid hom. -/ protected instance set_inclusion.is_monoid_hom {M : Type u_1} [monoid M] {s : set M} (t : set M) [is_submonoid s] [is_submonoid t] (h : s ⊆ t) : is_monoid_hom (set.inclusion h) := subtype_mk.is_monoid_hom (fun (x : ↥s) => ↑x) fun (x : ↥s) => set.inclusion._proof_1 h x namespace add_monoid /-- The inductively defined membership predicate for the submonoid generated by a subset of a monoid. -/ inductive in_closure {A : Type u_2} [add_monoid A] (s : set A) : A → Prop where | basic : ∀ {a : A}, a ∈ s → in_closure s a | zero : in_closure s 0 | add : ∀ {a b : A}, in_closure s a → in_closure s b → in_closure s (a + b) end add_monoid namespace monoid /-- The inductively defined membership predicate for the `add_submonoid` generated by a subset of an add_monoid. -/ inductive in_closure {M : Type u_1} [monoid M] (s : set M) : M → Prop where | basic : ∀ {a : M}, a ∈ s → in_closure s a | one : in_closure s 1 | mul : ∀ {a b : M}, in_closure s a → in_closure s b → in_closure s (a * b) /-- The inductively defined submonoid generated by a subset of a monoid. -/ def Mathlib.add_monoid.closure {M : Type u_1} [add_monoid M] (s : set M) : set M := set_of fun (a : M) => add_monoid.in_closure s a protected instance Mathlib.add_monoid.closure.is_add_submonoid {M : Type u_1} [add_monoid M] (s : set M) : is_add_submonoid (add_monoid.closure s) := is_add_submonoid.mk add_monoid.in_closure.zero fun (a b : M) => add_monoid.in_closure.add /-- A subset of a monoid is contained in the submonoid it generates. -/ theorem Mathlib.add_monoid.subset_closure {M : Type u_1} [add_monoid M] {s : set M} : s ⊆ add_monoid.closure s := fun (a : M) => add_monoid.in_closure.basic /-- The submonoid generated by a set is contained in any submonoid that contains the set. -/ theorem Mathlib.add_monoid.closure_subset {M : Type u_1} [add_monoid M] {s : set M} {t : set M} [is_add_submonoid t] (h : s ⊆ t) : add_monoid.closure s ⊆ t := sorry /-- Given subsets `t` and `s` of a monoid `M`, if `s ⊆ t`, the submonoid of `M` generated by `s` is contained in the submonoid generated by `t`. -/ theorem Mathlib.add_monoid.closure_mono {M : Type u_1} [add_monoid M] {s : set M} {t : set M} (h : s ⊆ t) : add_monoid.closure s ⊆ add_monoid.closure t := add_monoid.closure_subset (set.subset.trans h add_monoid.subset_closure) /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ theorem closure_singleton {M : Type u_1} [monoid M] {x : M} : closure (singleton x) = powers x := set.eq_of_subset_of_subset (closure_subset (iff.mpr set.singleton_subset_iff powers.self_mem)) (is_submonoid.power_subset (iff.mp set.singleton_subset_iff subset_closure)) /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set under the monoid hom. -/ theorem Mathlib.add_monoid.image_closure {M : Type u_1} [add_monoid M] {A : Type u_2} [add_monoid A] (f : M → A) [is_add_monoid_hom f] (s : set M) : f '' add_monoid.closure s = add_monoid.closure (f '' s) := sorry /-- Given an element `a` of the submonoid of a monoid `M` generated by a set `s`, there exists a list of elements of `s` whose product is `a`. -/ theorem Mathlib.add_monoid.exists_list_of_mem_closure {M : Type u_1} [add_monoid M] {s : set M} {a : M} (h : a ∈ add_monoid.closure s) : ∃ (l : List M), (∀ (x : M), x ∈ l → x ∈ s) ∧ list.sum l = a := sorry /-- Given sets `s, t` of a commutative monoid `M`, `x ∈ M` is in the submonoid of `M` generated by `s ∪ t` iff there exists an element of the submonoid generated by `s` and an element of the submonoid generated by `t` whose product is `x`. -/ theorem Mathlib.add_monoid.mem_closure_union_iff {M : Type u_1} [add_comm_monoid M] {s : set M} {t : set M} {x : M} : x ∈ add_monoid.closure (s ∪ t) ↔ ∃ (y : M), ∃ (H : y ∈ add_monoid.closure s), ∃ (z : M), ∃ (H : z ∈ add_monoid.closure t), y + z = x := sorry end monoid /-- Create a bundled submonoid from a set `s` and `[is_submonoid s]`. -/ def add_submonoid.of {M : Type u_1} [add_monoid M] (s : set M) [h : is_add_submonoid s] : add_submonoid M := add_submonoid.mk s is_add_submonoid.zero_mem is_add_submonoid.add_mem protected instance submonoid.is_submonoid {M : Type u_1} [monoid M] (S : submonoid M) : is_submonoid ↑S := is_submonoid.mk (submonoid.one_mem' S) (submonoid.mul_mem' S) end Mathlib
54353682049064044c729e0be6b214211456e909
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/normed/group/SemiNormedGroup/kernels.lean
0dc24df7e08d082df71d182e6effaf366994c380
[ "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
13,247
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca, Johan Commelin, Scott Morrison -/ import analysis.normed.group.SemiNormedGroup import analysis.normed.group.quotient import category_theory.limits.shapes.kernels /-! # Kernels and cokernels in SemiNormedGroup₁ and SemiNormedGroup We show that `SemiNormedGroup₁` has cokernels (for which of course the `cokernel.π f` maps are norm non-increasing), as well as the easier result that `SemiNormedGroup` has cokernels. We also show that `SemiNormedGroup` has kernels. So far, I don't see a way to state nicely what we really want: `SemiNormedGroup` has cokernels, and `cokernel.π f` is norm non-increasing. The problem is that the limits API doesn't promise you any particular model of the cokernel, and in `SemiNormedGroup` one can always take a cokernel and rescale its norm (and hence making `cokernel.π f` arbitrarily large in norm), obtaining another categorical cokernel. -/ open category_theory category_theory.limits universe u namespace SemiNormedGroup₁ noncomputable theory /-- Auxiliary definition for `has_cokernels SemiNormedGroup₁`. -/ def cokernel_cocone {X Y : SemiNormedGroup₁.{u}} (f : X ⟶ Y) : cofork f 0 := cofork.of_π (@SemiNormedGroup₁.mk_hom _ (SemiNormedGroup.of (Y ⧸ (normed_add_group_hom.range f.1))) f.1.range.normed_mk (normed_add_group_hom.is_quotient_quotient _).norm_le) begin ext, simp only [comp_apply, limits.zero_comp, normed_add_group_hom.zero_apply, SemiNormedGroup₁.mk_hom_apply, SemiNormedGroup₁.zero_apply, ←normed_add_group_hom.mem_ker, f.1.range.ker_normed_mk, f.1.mem_range], use x, refl, end /-- Auxiliary definition for `has_cokernels SemiNormedGroup₁`. -/ def cokernel_lift {X Y : SemiNormedGroup₁.{u}} (f : X ⟶ Y) (s : cokernel_cofork f) : (cokernel_cocone f).X ⟶ s.X := begin fsplit, -- The lift itself: { apply normed_add_group_hom.lift _ s.π.1, rintro _ ⟨b, rfl⟩, change (f ≫ s.π) b = 0, simp, }, -- The lift has norm at most one: exact normed_add_group_hom.lift_norm_noninc _ _ _ s.π.2, end instance : has_cokernels SemiNormedGroup₁.{u} := { has_colimit := λ X Y f, has_colimit.mk { cocone := cokernel_cocone f, is_colimit := is_colimit_aux _ (cokernel_lift f) (λ s, begin ext, apply normed_add_group_hom.lift_mk f.1.range, rintro _ ⟨b, rfl⟩, change (f ≫ s.π) b = 0, simp, end) (λ s m w, subtype.eq (normed_add_group_hom.lift_unique f.1.range _ _ _ (congr_arg subtype.val w : _))), } } -- Sanity check example : has_cokernels SemiNormedGroup₁ := by apply_instance end SemiNormedGroup₁ namespace SemiNormedGroup section equalizers_and_kernels /-- The equalizer cone for a parallel pair of morphisms of seminormed groups. -/ def fork {V W : SemiNormedGroup.{u}} (f g : V ⟶ W) : fork f g := @fork.of_ι _ _ _ _ _ _ (of (f - g).ker) (normed_add_group_hom.incl (f - g).ker) $ begin ext v, have : v.1 ∈ (f - g).ker := v.2, simpa only [normed_add_group_hom.incl_apply, pi.zero_apply, coe_comp, normed_add_group_hom.coe_zero, subtype.val_eq_coe, normed_add_group_hom.mem_ker, normed_add_group_hom.coe_sub, pi.sub_apply, sub_eq_zero] using this end instance has_limit_parallel_pair {V W : SemiNormedGroup.{u}} (f g : V ⟶ W) : has_limit (parallel_pair f g) := { exists_limit := nonempty.intro { cone := fork f g, is_limit := fork.is_limit.mk _ (λ c, normed_add_group_hom.ker.lift (fork.ι c) _ $ show normed_add_group_hom.comp_hom (f - g) c.ι = 0, by { rw [add_monoid_hom.map_sub, add_monoid_hom.sub_apply, sub_eq_zero], exact c.condition }) (λ c, normed_add_group_hom.ker.incl_comp_lift _ _ _) (λ c g h, by { ext x, dsimp, rw ← h, refl }) } } instance : limits.has_equalizers.{u (u+1)} SemiNormedGroup := @has_equalizers_of_has_limit_parallel_pair SemiNormedGroup _ $ λ V W f g, SemiNormedGroup.has_limit_parallel_pair f g end equalizers_and_kernels section cokernel -- PROJECT: can we reuse the work to construct cokernels in `SemiNormedGroup₁` here? -- I don't see a way to do this that is less work than just repeating the relevant parts. /-- Auxiliary definition for `has_cokernels SemiNormedGroup`. -/ def cokernel_cocone {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : cofork f 0 := @cofork.of_π _ _ _ _ _ _ (SemiNormedGroup.of (Y ⧸ (normed_add_group_hom.range f))) f.range.normed_mk begin ext, simp only [comp_apply, limits.zero_comp, normed_add_group_hom.zero_apply, ←normed_add_group_hom.mem_ker, f.range.ker_normed_mk, f.mem_range, exists_apply_eq_apply], end /-- Auxiliary definition for `has_cokernels SemiNormedGroup`. -/ def cokernel_lift {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) (s : cokernel_cofork f) : (cokernel_cocone f).X ⟶ s.X := normed_add_group_hom.lift _ s.π begin rintro _ ⟨b, rfl⟩, change (f ≫ s.π) b = 0, simp, end /-- Auxiliary definition for `has_cokernels SemiNormedGroup`. -/ def is_colimit_cokernel_cocone {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : is_colimit (cokernel_cocone f) := is_colimit_aux _ (cokernel_lift f) (λ s, begin ext, apply normed_add_group_hom.lift_mk f.range, rintro _ ⟨b, rfl⟩, change (f ≫ s.π) b = 0, simp, end) (λ s m w, normed_add_group_hom.lift_unique f.range _ _ _ w) instance : has_cokernels SemiNormedGroup.{u} := { has_colimit := λ X Y f, has_colimit.mk { cocone := cokernel_cocone f, is_colimit := is_colimit_cokernel_cocone f } } -- Sanity check example : has_cokernels SemiNormedGroup := by apply_instance section explicit_cokernel /-- An explicit choice of cokernel, which has good properties with respect to the norm. -/ def explicit_cokernel {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : SemiNormedGroup.{u} := (cokernel_cocone f).X /-- Descend to the explicit cokernel. -/ def explicit_cokernel_desc {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} (w : f ≫ g = 0) : explicit_cokernel f ⟶ Z := (is_colimit_cokernel_cocone f).desc (cofork.of_π g (by simp [w])) /-- The projection from `Y` to the explicit cokernel of `X ⟶ Y`. -/ def explicit_cokernel_π {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : Y ⟶ explicit_cokernel f := (cokernel_cocone f).ι.app walking_parallel_pair.one lemma explicit_cokernel_π_surjective {X Y : SemiNormedGroup.{u}} {f : X ⟶ Y} : function.surjective (explicit_cokernel_π f) := surjective_quot_mk _ @[simp, reassoc] lemma comp_explicit_cokernel_π {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : f ≫ explicit_cokernel_π f = 0 := begin convert (cokernel_cocone f).w walking_parallel_pair_hom.left, simp, end @[simp] lemma explicit_cokernel_π_apply_dom_eq_zero {X Y : SemiNormedGroup.{u}} {f : X ⟶ Y} (x : X) : (explicit_cokernel_π f) (f x) = 0 := show (f ≫ (explicit_cokernel_π f)) x = 0, by { rw [comp_explicit_cokernel_π], refl } @[simp, reassoc] lemma explicit_cokernel_π_desc {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} (w : f ≫ g = 0) : explicit_cokernel_π f ≫ explicit_cokernel_desc w = g := (is_colimit_cokernel_cocone f).fac _ _ @[simp] lemma explicit_cokernel_π_desc_apply {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} {cond : f ≫ g = 0} (x : Y) : explicit_cokernel_desc cond (explicit_cokernel_π f x) = g x := show (explicit_cokernel_π f ≫ explicit_cokernel_desc cond) x = g x, by rw explicit_cokernel_π_desc lemma explicit_cokernel_desc_unique {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} (w : f ≫ g = 0) (e : explicit_cokernel f ⟶ Z) (he : explicit_cokernel_π f ≫ e = g) : e = explicit_cokernel_desc w := begin apply (is_colimit_cokernel_cocone f).uniq (cofork.of_π g (by simp [w])), rintro (_|_), { convert w.symm, simp }, { exact he } end lemma explicit_cokernel_desc_comp_eq_desc {X Y Z W : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} {h : Z ⟶ W} {cond : f ≫ g = 0} : explicit_cokernel_desc cond ≫ h = explicit_cokernel_desc (show f ≫ (g ≫ h) = 0, by rw [← category_theory.category.assoc, cond, limits.zero_comp]) := begin refine explicit_cokernel_desc_unique _ _ _, rw [← category_theory.category.assoc, explicit_cokernel_π_desc] end @[simp] lemma explicit_cokernel_desc_zero {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} : explicit_cokernel_desc (show f ≫ (0 : Y ⟶ Z) = 0, from category_theory.limits.comp_zero) = 0 := eq.symm $ explicit_cokernel_desc_unique _ _ category_theory.limits.comp_zero @[ext] lemma explicit_cokernel_hom_ext {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} (e₁ e₂ : explicit_cokernel f ⟶ Z) (h : explicit_cokernel_π f ≫ e₁ = explicit_cokernel_π f ≫ e₂) : e₁ = e₂ := begin let g : Y ⟶ Z := explicit_cokernel_π f ≫ e₂, have w : f ≫ g = 0, by simp, have : e₂ = explicit_cokernel_desc w, { apply explicit_cokernel_desc_unique, refl }, rw this, apply explicit_cokernel_desc_unique, exact h, end instance explicit_cokernel_π.epi {X Y : SemiNormedGroup.{u}} {f : X ⟶ Y} : epi (explicit_cokernel_π f) := begin constructor, intros Z g h H, ext x, obtain ⟨x, hx⟩ := explicit_cokernel_π_surjective (explicit_cokernel_π f x), change (explicit_cokernel_π f ≫ g) _ = _, rw [H] end lemma is_quotient_explicit_cokernel_π {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : normed_add_group_hom.is_quotient (explicit_cokernel_π f) := normed_add_group_hom.is_quotient_quotient _ lemma norm_noninc_explicit_cokernel_π {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : (explicit_cokernel_π f).norm_noninc := (is_quotient_explicit_cokernel_π f).norm_le open_locale nnreal lemma explicit_cokernel_desc_norm_le_of_norm_le {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} (w : f ≫ g = 0) (c : ℝ≥0) (h : ‖ g ‖ ≤ c) : ‖ explicit_cokernel_desc w ‖ ≤ c := normed_add_group_hom.lift_norm_le _ _ _ h lemma explicit_cokernel_desc_norm_noninc {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} {cond : f ≫ g = 0} (hg : g.norm_noninc) : (explicit_cokernel_desc cond).norm_noninc := begin refine normed_add_group_hom.norm_noninc.norm_noninc_iff_norm_le_one.2 _, rw [← nnreal.coe_one], exact explicit_cokernel_desc_norm_le_of_norm_le cond 1 (normed_add_group_hom.norm_noninc.norm_noninc_iff_norm_le_one.1 hg) end lemma explicit_cokernel_desc_comp_eq_zero {X Y Z W : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} {h : Z ⟶ W} (cond : f ≫ g = 0) (cond2 : g ≫ h = 0) : explicit_cokernel_desc cond ≫ h = 0 := begin rw [← cancel_epi (explicit_cokernel_π f), ← category.assoc, explicit_cokernel_π_desc], simp [cond2] end lemma explicit_cokernel_desc_norm_le {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} (w : f ≫ g = 0) : ‖ explicit_cokernel_desc w ‖ ≤ ‖ g ‖ := explicit_cokernel_desc_norm_le_of_norm_le w ‖ g ‖₊ le_rfl /-- The explicit cokernel is isomorphic to the usual cokernel. -/ def explicit_cokernel_iso {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : explicit_cokernel f ≅ cokernel f := (is_colimit_cokernel_cocone f).cocone_point_unique_up_to_iso (colimit.is_colimit _) @[simp] lemma explicit_cokernel_iso_hom_π {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : explicit_cokernel_π f ≫ (explicit_cokernel_iso f).hom = cokernel.π _ := by simp [explicit_cokernel_π, explicit_cokernel_iso, is_colimit.cocone_point_unique_up_to_iso] @[simp] lemma explicit_cokernel_iso_inv_π {X Y : SemiNormedGroup.{u}} (f : X ⟶ Y) : cokernel.π f ≫ (explicit_cokernel_iso f).inv = explicit_cokernel_π f := by simp [explicit_cokernel_π, explicit_cokernel_iso] @[simp] lemma explicit_cokernel_iso_hom_desc {X Y Z : SemiNormedGroup.{u}} {f : X ⟶ Y} {g : Y ⟶ Z} (w : f ≫ g = 0) : (explicit_cokernel_iso f).hom ≫ cokernel.desc f g w = explicit_cokernel_desc w := begin ext1, simp [explicit_cokernel_desc, explicit_cokernel_π, explicit_cokernel_iso, is_colimit.cocone_point_unique_up_to_iso], end /-- A special case of `category_theory.limits.cokernel.map` adapted to `explicit_cokernel`. -/ noncomputable def explicit_cokernel.map {A B C D : SemiNormedGroup.{u}} {fab : A ⟶ B} {fbd : B ⟶ D} {fac : A ⟶ C} {fcd : C ⟶ D} (h : fab ≫ fbd = fac ≫ fcd) : explicit_cokernel fab ⟶ explicit_cokernel fcd := @explicit_cokernel_desc _ _ _ fab (fbd ≫ explicit_cokernel_π _) $ by simp [reassoc_of h] /-- A special case of `category_theory.limits.cokernel.map_desc` adapted to `explicit_cokernel`. -/ lemma explicit_coker.map_desc {A B C D B' D' : SemiNormedGroup.{u}} {fab : A ⟶ B} {fbd : B ⟶ D} {fac : A ⟶ C} {fcd : C ⟶ D} {h : fab ≫ fbd = fac ≫ fcd} {fbb' : B ⟶ B'} {fdd' : D ⟶ D'} {condb : fab ≫ fbb' = 0} {condd : fcd ≫ fdd' = 0} {g : B' ⟶ D'} (h' : fbb' ≫ g = fbd ≫ fdd'): explicit_cokernel_desc condb ≫ g = explicit_cokernel.map h ≫ explicit_cokernel_desc condd := begin delta explicit_cokernel.map, simp [← cancel_epi (explicit_cokernel_π fab), category.assoc, explicit_cokernel_π_desc, h'] end end explicit_cokernel end cokernel end SemiNormedGroup
b870fcaf31fdd752825e10f654c05ab7de67ab4b
a4673261e60b025e2c8c825dfa4ab9108246c32e
/src/Lean/Util/FoldConsts.lean
315119046510562fc5c66fff3192cc95eb7c1444
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,695
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.Expr import Lean.Environment namespace Lean namespace Expr namespace FoldConstsImpl abbrev cacheSize : USize := 8192 structure State := (visitedTerms : Array Expr) -- Remark: cache based on pointer address. Our "unsafe" implementation relies on the fact that `()` is not a valid Expr (visitedConsts : NameHashSet) -- cache based on structural equality abbrev FoldM := StateM State @[inline] unsafe def visited (e : Expr) (size : USize) : FoldM Bool := do let s ← get let h := ptrAddrUnsafe e let i := h % size let k := s.visitedTerms.uget i lcProof if ptrAddrUnsafe k == h then pure true else do modify $ fun s => { s with visitedTerms := s.visitedTerms.uset i e lcProof } pure false @[specialize] unsafe def fold {α : Type} (f : Name → α → α) (size : USize) (e : Expr) (acc : α) : FoldM α := let rec visit (e : Expr) (acc : α) : FoldM α := do if (← visited e size) then pure acc else match e with | Expr.forallE _ d b _ => visit b (← visit d acc) | Expr.lam _ d b _ => visit b (← visit d acc) | Expr.mdata _ b _ => visit b acc | Expr.letE _ t v b _ => visit b (← visit v (← visit t acc)) | Expr.app f a _ => visit a (← visit f acc) | Expr.proj _ _ b _ => visit b acc | Expr.const c _ _ => let s ← get if s.visitedConsts.contains c then pure acc else do modify fun s => { s with visitedConsts := s.visitedConsts.insert c }; pure $ f c acc | _ => pure acc visit e acc unsafe def initCache : State := { visitedTerms := mkArray cacheSize.toNat (cast lcProof ()), visitedConsts := {} } @[inline] unsafe def foldUnsafe {α : Type} (e : Expr) (init : α) (f : Name → α → α) : α := (fold f cacheSize e init).run' initCache end FoldConstsImpl /-- Apply `f` to every constant occurring in `e` once. -/ @[implementedBy FoldConstsImpl.foldUnsafe] constant foldConsts {α : Type} (e : Expr) (init : α) (f : Name → α → α) : α := init def getUsedConstants (e : Expr) : Array Name := e.foldConsts #[] fun c cs => cs.push c end Expr def getMaxHeight (env : Environment) (e : Expr) : UInt32 := e.foldConsts 0 $ fun constName max => match env.find? constName with | ConstantInfo.defnInfo val => match val.hints with | ReducibilityHints.regular h => if h > max then h else max | _ => max | _ => max end Lean
f4420e32a199fd9ee5bedfce40aef8e9053f1d7c
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/order/filter/cofinite.lean
466f7615f94404522834ea0883e12d1b49e2c0e8
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
2,289
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jeremy Avigad, Yury Kudryashov -/ import order.filter.at_top_bot /-! # The cofinite filter In this file we define `cofinite`: the filter of sets with finite complement and prove its basic properties. In particular, we prove that for `ℕ` it is equal to `at_top`. ## TODO Define filters for other cardinalities of the complement. -/ open set open_locale classical namespace filter variables {α : Type*} /-- The cofinite filter is the filter of subsets whose complements are finite. -/ def cofinite : filter α := { sets := {s | finite sᶜ}, univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq], sets_of_superset := assume s t (hs : finite sᶜ) (st: s ⊆ t), hs.subset $ compl_subset_compl.2 st, inter_sets := assume s t (hs : finite sᶜ) (ht : finite (tᶜ)), by simp only [compl_inter, finite.union, ht, hs, mem_set_of_eq] } @[simp] lemma mem_cofinite {s : set α} : s ∈ (@cofinite α) ↔ finite sᶜ := iff.rfl lemma cofinite_ne_bot [infinite α] : @cofinite α ≠ ⊥ := mt empty_in_sets_eq_bot.mpr $ by { simp only [mem_cofinite, compl_empty], exact infinite_univ } lemma frequently_cofinite_iff_infinite {p : α → Prop} : (∃ᶠ x in cofinite, p x) ↔ set.infinite {x | p x} := by simp only [filter.frequently, filter.eventually, mem_cofinite, compl_set_of, not_not, set.infinite] end filter open filter lemma set.infinite_iff_frequently_cofinite {α : Type*} {s : set α} : set.infinite s ↔ (∃ᶠ x in cofinite, x ∈ s) := frequently_cofinite_iff_infinite.symm /-- For natural numbers the filters `cofinite` and `at_top` coincide. -/ lemma nat.cofinite_eq_at_top : @cofinite ℕ = at_top := begin ext s, simp only [mem_cofinite, mem_at_top_sets], split, { assume hs, use (hs.to_finset.sup id) + 1, assume b hb, by_contradiction hbs, have := hs.to_finset.subset_range_sup_succ (finite.mem_to_finset.2 hbs), exact not_lt_of_le hb (finset.mem_range.1 this) }, { rintros ⟨N, hN⟩, apply (finite_lt_nat N).subset, assume n hn, change n < N, exact lt_of_not_ge (λ hn', hn $ hN n hn') } end
07d0827458ee65b48669e51aab1285b0f6e8c0f9
8b9f17008684d796c8022dab552e42f0cb6fb347
/library/logic/axioms/classical.lean
77353544453416990f756d6325d410f34037cc97
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
281
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: logic.axioms.classical Author: Jeremy Avigad -/ import logic.axioms.prop_complete logic.axioms.hilbert import logic.axioms.prop_decidable
a8d131151ed6850d6b6aef3f6543b2bbb6631772
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/cc_constructors.lean
a1f370febc43490635fdbaa75877edd6c18132e6
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,826
lean
open tactic example (a b : nat) (s t : list nat) : a::s = b::t → a ≠ b → false := by cc example (a b : nat) (s t : list nat) : a::s = b::t → t ≠ s → false := by cc example (a c b : nat) (s t : list nat) : a::s = b::t → a ≠ c → c = b → false := by cc example (a c b : nat) (s t : list nat) : a::a::s = a::b::t → a ≠ c → c = b → false := by cc example (a b : nat) (s t r : list nat) : a::s = r → r = b::t → a ≠ b → false := by cc example (a b : nat) (s t r : list nat) : a::s = r → r = b::t → a = b := by cc example (a b : nat) (s t r : list nat) : list.cons a = list.cons b → a = b := begin intro h1, /- In the current implementation, cc does not "complete" partially applied constructor applications. So, the following one should fail. -/ try {cc}, /- Complete it manually. TODO(Leo): we can automate it for inhabited types. -/ have h := congr_fun h1 [], cc end inductive foo | mk1 : nat → nat → foo | mk2 : nat → nat → foo example (a b : nat) : foo.mk1 a = foo.mk2 b → false := begin intro h1, /- In the current implementation, cc does not "complete" partially applied constructor applications. So, the following one should fail. -/ try {cc}, have h := congr_fun h1 0, cc end universe variables u inductive Vec (α : Type u) : nat → Type (max 1 u) | nil : Vec 0 | cons : ∀ {n}, α → Vec n → Vec (nat.succ n) example (α : Type u) (a b c d : α) (n : nat) (s t : Vec α n) : Vec.cons a s = Vec.cons b t → a ≠ b → false := by cc example (α : Type u) (a b c d : α) (n : nat) (s t : Vec α n) : Vec.cons a s = Vec.cons b t → t ≠ s → false := by cc example (α : Type u) (a b c d : α) (n : nat) (s t : Vec α n) : Vec.cons a (Vec.cons a s) = Vec.cons a (Vec.cons b t) → b ≠ c → c = a → false := by cc
853478a818a3429663761d206c9decb0bdd19baf
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Parser/Level.lean
c49cb80a69b06f86865b1329cb27b5715b1a9d6d
[ "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
1,038
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Parser.Extra namespace Lean namespace Parser builtin_initialize registerBuiltinParserAttribute `builtinLevelParser `level @[inline] def levelParser (rbp : Nat := 0) : Parser := categoryParser `level rbp namespace Level @[builtinLevelParser] def paren := leading_parser "(" >> levelParser >> ")" @[builtinLevelParser] def max := leading_parser nonReservedSymbol "max" true >> many1 (ppSpace >> levelParser maxPrec) @[builtinLevelParser] def imax := leading_parser nonReservedSymbol "imax" true >> many1 (ppSpace >> levelParser maxPrec) @[builtinLevelParser] def hole := leading_parser "_" @[builtinLevelParser] def num := checkPrec maxPrec >> numLit @[builtinLevelParser] def ident := checkPrec maxPrec >> Parser.ident @[builtinLevelParser] def addLit := trailing_parser:65 " + " >> numLit end Level end Parser end Lean
8f8ef61821c96bd8edec3c20c1b044f958aaf21d
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/analysis/special_functions/pow.lean
1c459f3ae90373721638a9b5c2c804e50a6fbae6
[ "Apache-2.0" ]
permissive
LibertasSpZ/mathlib
b9fcd46625eb940611adb5e719a4b554138dade6
33f7870a49d7cc06d2f3036e22543e6ec5046e68
refs/heads/master
1,672,066,539,347
1,602,429,158,000
1,602,429,158,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
52,593
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel -/ import analysis.special_functions.trigonometric import analysis.calculus.extend_deriv /-! # Power function on `ℂ`, `ℝ`, `ℝ≥0`, and `ennreal` We construct the power functions `x ^ y` where * `x` and `y` are complex numbers, * or `x` and `y` are real numbers, * or `x` is a nonnegative real number and `y` is a real number; * or `x` is a number from `[0, +∞]` (a.k.a. `ennreal`) and `y` is a real number. We also prove basic properties of these functions. -/ noncomputable theory open_locale classical real topological_space nnreal namespace complex /-- The complex power function `x^y`, given by `x^y = exp(y log x)` (where `log` is the principal determination of the logarithm), unless `x = 0` where one sets `0^0 = 1` and `0^y = 0` for `y ≠ 0`. -/ noncomputable def cpow (x y : ℂ) : ℂ := if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) noncomputable instance : has_pow ℂ ℂ := ⟨cpow⟩ @[simp] lemma cpow_eq_pow (x y : ℂ) : cpow x y = x ^ y := rfl lemma cpow_def (x y : ℂ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := rfl @[simp] lemma cpow_zero (x : ℂ) : x ^ (0 : ℂ) = 1 := by simp [cpow_def] @[simp] lemma cpow_eq_zero_iff (x y : ℂ) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [cpow_def], split_ifs; simp [*, exp_ne_zero] } @[simp] lemma zero_cpow {x : ℂ} (h : x ≠ 0) : (0 : ℂ) ^ x = 0 := by simp [cpow_def, *] @[simp] lemma cpow_one (x : ℂ) : x ^ (1 : ℂ) = x := if hx : x = 0 then by simp [hx, cpow_def] else by rw [cpow_def, if_neg (one_ne_zero : (1 : ℂ) ≠ 0), if_neg hx, mul_one, exp_log hx] @[simp] lemma one_cpow (x : ℂ) : (1 : ℂ) ^ x = 1 := by rw cpow_def; split_ifs; simp [one_ne_zero, *] at * lemma cpow_add {x : ℂ} (y z : ℂ) (hx : x ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := by simp [cpow_def]; split_ifs; simp [*, exp_add, mul_add] at * lemma cpow_mul {x y : ℂ} (z : ℂ) (h₁ : -π < (log x * y).im) (h₂ : (log x * y).im ≤ π) : x ^ (y * z) = (x ^ y) ^ z := begin simp [cpow_def], split_ifs; simp [*, exp_ne_zero, log_exp h₁ h₂, mul_assoc] at * end lemma cpow_neg (x y : ℂ) : x ^ -y = (x ^ y)⁻¹ := by simp [cpow_def]; split_ifs; simp [exp_neg] lemma cpow_neg_one (x : ℂ) : x ^ (-1 : ℂ) = x⁻¹ := by simpa using cpow_neg x 1 @[simp] lemma cpow_nat_cast (x : ℂ) : ∀ (n : ℕ), x ^ (n : ℂ) = x ^ n | 0 := by simp | (n + 1) := if hx : x = 0 then by simp only [hx, pow_succ, complex.zero_cpow (nat.cast_ne_zero.2 (nat.succ_ne_zero _)), zero_mul] else by simp [cpow_add, hx, pow_add, cpow_nat_cast n] @[simp] lemma cpow_int_cast (x : ℂ) : ∀ (n : ℤ), x ^ (n : ℂ) = x ^ n | (n : ℕ) := by simp; refl | -[1+ n] := by rw fpow_neg_succ_of_nat; simp only [int.neg_succ_of_nat_coe, int.cast_neg, complex.cpow_neg, inv_eq_one_div, int.cast_coe_nat, cpow_nat_cast] lemma cpow_nat_inv_pow (x : ℂ) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℂ)) ^ n = x := have (log x * (↑n)⁻¹).im = (log x).im / n, by rw [div_eq_mul_inv, ← of_real_nat_cast, ← of_real_inv, mul_im, of_real_re, of_real_im]; simp, have h : -π < (log x * (↑n)⁻¹).im ∧ (log x * (↑n)⁻¹).im ≤ π, from (le_total (log x).im 0).elim (λ h, ⟨calc -π < (log x).im : by simp [log, neg_pi_lt_arg] ... ≤ ((log x).im * 1) / n : (le_div_iff (nat.cast_pos.2 hn : (0 : ℝ) < _)).mpr (mul_le_mul_of_nonpos_left (by rw ← nat.cast_one; exact nat.cast_le.2 hn) h) ... = (log x * (↑n)⁻¹).im : by simp [this], this.symm ▸ le_trans (div_nonpos_of_nonpos_of_nonneg h n.cast_nonneg) (le_of_lt real.pi_pos)⟩) (λ h, ⟨this.symm ▸ lt_of_lt_of_le (neg_neg_of_pos real.pi_pos) (div_nonneg h n.cast_nonneg), calc (log x * (↑n)⁻¹).im = (1 * (log x).im) / n : by simp [this] ... ≤ (log x).im : (div_le_iff' (nat.cast_pos.2 hn : (0 : ℝ) < _)).mpr (mul_le_mul_of_nonneg_right (by rw ← nat.cast_one; exact nat.cast_le.2 hn) h) ... ≤ _ : by simp [log, arg_le_pi]⟩), by rw [← cpow_nat_cast, ← cpow_mul _ h.1 h.2, inv_mul_cancel (show (n : ℂ) ≠ 0, from nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)), cpow_one] end complex namespace real /-- The real power function `x^y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp(y log x)`. For `x = 0`, one sets `0^0=1` and `0^y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (πy)`. -/ noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re noncomputable instance : has_pow ℝ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl lemma rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl lemma rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := by simp only [rpow_def, complex.cpow_def]; split_ifs; simp [*, (complex.of_real_log hx).symm, -complex.of_real_mul, (complex.of_real_mul _ _).symm, complex.exp_of_real_re] at * lemma rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)] lemma exp_mul (x y : ℝ) : exp (x * y) = (exp x) ^ y := by rw [rpow_def_of_pos (exp_pos _), log_exp] lemma rpow_eq_zero_iff_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [rpow_def_of_nonneg hx], split_ifs; simp [*, exp_ne_zero] } open_locale real lemma rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) := begin rw [rpow_def, complex.cpow_def, if_neg], have : complex.log x * y = ↑(log(-x) * y) + ↑(y * π) * complex.I, simp only [complex.log, abs_of_neg hx, complex.arg_of_real_of_neg hx, complex.abs_of_real, complex.of_real_mul], ring, { rw [this, complex.exp_add_mul_I, ← complex.of_real_exp, ← complex.of_real_cos, ← complex.of_real_sin, mul_add, ← complex.of_real_mul, ← mul_assoc, ← complex.of_real_mul, complex.add_re, complex.of_real_re, complex.mul_re, complex.I_re, complex.of_real_im, real.log_neg_eq_log], ring }, { rw complex.of_real_eq_zero, exact ne_of_lt hx } end lemma rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) * cos (y * π) := by split_ifs; simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _ lemma rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y := by rw rpow_def_of_pos hx; apply exp_pos lemma abs_rpow_le_abs_rpow (x y : ℝ) : abs (x ^ y) ≤ abs (x) ^ y := abs_le_of_le_of_neg_le begin cases lt_trichotomy 0 x, { rw abs_of_pos h }, cases h, { simp [h.symm] }, rw [rpow_def_of_neg h, rpow_def_of_pos (abs_pos_of_neg h), log_abs], calc exp (log x * y) * cos (y * π) ≤ exp (log x * y) * 1 : mul_le_mul_of_nonneg_left (cos_le_one _) (le_of_lt $ exp_pos _) ... = _ : mul_one _ end begin cases lt_trichotomy 0 x, { rw abs_of_pos h, have : 0 < x^y := rpow_pos_of_pos h _, linarith }, cases h, { simp only [h.symm, abs_zero, rpow_def_of_nonneg], split_ifs, repeat {norm_num} }, rw [rpow_def_of_neg h, rpow_def_of_pos (abs_pos_of_neg h), log_abs], calc -(exp (log x * y) * cos (y * π)) = exp (log x * y) * (-cos (y * π)) : by ring ... ≤ exp (log x * y) * 1 : mul_le_mul_of_nonneg_left (neg_le.2 $ neg_one_le_cos _) (le_of_lt $ exp_pos _) ... = exp (log x * y) : mul_one _ end end real namespace complex lemma of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) := by simp [real.rpow_def_of_nonneg hx, complex.cpow_def]; split_ifs; simp [complex.of_real_log hx] @[simp] lemma abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ (y : ℂ)) = x.abs ^ y := begin rw [real.rpow_def_of_nonneg (abs_nonneg _), complex.cpow_def], split_ifs; simp [*, abs_of_nonneg (le_of_lt (real.exp_pos _)), complex.log, complex.exp_add, add_mul, mul_right_comm _ I, exp_mul_I, abs_cos_add_sin_mul_I, (complex.of_real_mul _ _).symm, -complex.of_real_mul] at * end @[simp] lemma abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (n⁻¹ : ℂ)) = x.abs ^ (n⁻¹ : ℝ) := by rw ← abs_cpow_real; simp [-abs_cpow_real] end complex namespace real variables {x y z : ℝ} @[simp] lemma rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def] @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 := by simp [rpow_def, *] @[simp] lemma rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def] @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def] lemma zero_rpow_le_one (x : ℝ) : (0 : ℝ) ^ x ≤ 1 := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma zero_rpow_nonneg (x : ℝ) : 0 ≤ (0 : ℝ) ^ x := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y := by rw [rpow_def_of_nonneg hx]; split_ifs; simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)] lemma rpow_add {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := by simp only [rpow_def_of_pos hx, mul_add, exp_add] lemma rpow_add' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := begin rcases le_iff_eq_or_lt.1 hx with H|pos, { simp only [← H, h, rpow_eq_zero_iff_of_nonneg, true_and, zero_rpow, eq_self_iff_true, ne.def, not_false_iff, zero_eq_mul], by_contradiction F, push_neg at F, apply h, simp [F] }, { exact rpow_add pos _ _ } end /-- For `0 ≤ x`, the only problematic case in the equality `x ^ y * x ^ z = x ^ (y + z)` is for `x = 0` and `y + z = 0`, where the right hand side is `1` while the left hand side can vanish. The inequality is always true, though, and given in this lemma. -/ lemma le_rpow_add {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ y * x ^ z ≤ x ^ (y + z) := begin rcases le_iff_eq_or_lt.1 hx with H|pos, { by_cases h : y + z = 0, { simp only [H.symm, h, rpow_zero], calc (0 : ℝ) ^ y * 0 ^ z ≤ 1 * 1 : mul_le_mul (zero_rpow_le_one y) (zero_rpow_le_one z) (zero_rpow_nonneg z) zero_le_one ... = 1 : by simp }, { simp [rpow_add', ← H, h] } }, { simp [rpow_add pos] } end lemma rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by rw [← complex.of_real_inj, complex.of_real_cpow (rpow_nonneg_of_nonneg hx _), complex.of_real_cpow hx, complex.of_real_mul, complex.cpow_mul, complex.of_real_cpow hx]; simp only [(complex.of_real_mul _ _).symm, (complex.of_real_log hx).symm, complex.of_real_im, neg_lt_zero, pi_pos, le_of_lt pi_pos] lemma rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := by simp only [rpow_def_of_nonneg hx]; split_ifs; simp [*, exp_neg] at * lemma rpow_sub {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg, rpow_add hx, rpow_neg (le_of_lt hx), div_eq_mul_inv] lemma rpow_sub' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg, rpow_add' hx h, rpow_neg hx, div_eq_mul_inv] @[simp] lemma rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, (complex.of_real_pow _ _).symm, complex.cpow_nat_cast, complex.of_real_nat_cast, complex.of_real_re] @[simp] lemma rpow_int_cast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, (complex.of_real_fpow _ _).symm, complex.cpow_int_cast, complex.of_real_int_cast, complex.of_real_re] lemma rpow_neg_one (x : ℝ) : x ^ (-1 : ℝ) = x⁻¹ := begin suffices H : x ^ ((-1 : ℤ) : ℝ) = x⁻¹, by exact_mod_cast H, simp only [rpow_int_cast, fpow_one, fpow_neg], end lemma mul_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x*y)^z = x^z * y^z := begin iterate 3 { rw real.rpow_def_of_nonneg }, split_ifs; simp * at *, { have hx : 0 < x, cases lt_or_eq_of_le h with h₂ h₂, exact h₂, exfalso, apply h_2, exact eq.symm h₂, have hy : 0 < y, cases lt_or_eq_of_le h₁ with h₂ h₂, exact h₂, exfalso, apply h_3, exact eq.symm h₂, rw [log_mul (ne_of_gt hx) (ne_of_gt hy), add_mul, exp_add]}, { exact h₁}, { exact h}, { exact mul_nonneg h h₁}, end lemma inv_rpow (hx : 0 ≤ x) (y : ℝ) : (x⁻¹)^y = (x^y)⁻¹ := begin by_cases hy0 : y = 0, { simp [*] }, by_cases hx0 : x = 0, { simp [*] }, simp only [real.rpow_def_of_nonneg hx, real.rpow_def_of_nonneg (inv_nonneg.2 hx), if_false, hx0, mt inv_eq_zero.1 hx0, log_inv, ← neg_mul_eq_neg_mul, exp_neg] end lemma div_rpow (hx : 0 ≤ x) (hy : 0 ≤ y) (z : ℝ) : (x / y) ^ z = x^z / y^z := by simp only [div_eq_mul_inv, mul_rpow hx (inv_nonneg.2 hy), inv_rpow hy] lemma rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x^z < y^z := begin rw le_iff_eq_or_lt at hx, cases hx, { rw [← hx, zero_rpow (ne_of_gt hz)], exact rpow_pos_of_pos (by rwa ← hx at hxy) _ }, rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp], exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz end lemma rpow_le_rpow {x y z: ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := begin rcases eq_or_lt_of_le h₁ with rfl|h₁', { refl }, rcases eq_or_lt_of_le h₂ with rfl|h₂', { simp }, exact le_of_lt (rpow_lt_rpow h h₁' h₂') end lemma rpow_lt_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := ⟨lt_imp_lt_of_le_imp_le $ λ h, rpow_le_rpow hy h (le_of_lt hz), λ h, rpow_lt_rpow hx h hz⟩ lemma rpow_le_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := le_iff_le_iff_lt_iff_lt.2 $ rpow_lt_rpow_iff hy hx hz lemma rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x^y < x^z := begin repeat {rw [rpow_def_of_pos (lt_trans zero_lt_one hx)]}, rw exp_lt_exp, exact mul_lt_mul_of_pos_left hyz (log_pos hx), end lemma rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)]}, rw exp_le_exp, exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx), end lemma rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_lt_exp, exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1), end lemma rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_le_exp, exact mul_le_mul_of_nonpos_left hyz (log_nonpos (le_of_lt hx0) hx1), end lemma rpow_lt_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x < 1) (hz : 0 < z) : x^z < 1 := by { rw ← one_rpow z, exact rpow_lt_rpow hx1 hx2 hz } lemma rpow_le_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := by { rw ← one_rpow z, exact rpow_le_rpow hx1 hx2 hz } lemma rpow_lt_one_of_one_lt_of_neg {x z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := by { convert rpow_lt_rpow_of_exponent_lt hx hz, exact (rpow_zero x).symm } lemma rpow_le_one_of_one_le_of_nonpos {x z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := by { convert rpow_le_rpow_of_exponent_le hx hz, exact (rpow_zero x).symm } lemma one_lt_rpow {x z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := by { rw ← one_rpow z, exact rpow_lt_rpow zero_le_one hx hz } lemma one_le_rpow {x z : ℝ} (hx : 1 ≤ x) (hz : 0 ≤ z) : 1 ≤ x^z := by { rw ← one_rpow z, exact rpow_le_rpow zero_le_one hx hz } lemma one_lt_rpow_of_pos_of_lt_one_of_neg (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := by { convert rpow_lt_rpow_of_exponent_gt hx1 hx2 hz, exact (rpow_zero x).symm } lemma one_le_rpow_of_pos_of_le_one_of_nonpos (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := by { convert rpow_le_rpow_of_exponent_ge hx1 hx2 hz, exact (rpow_zero x).symm } lemma rpow_lt_one_iff_of_pos (hx : 0 < x) : x ^ y < 1 ↔ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := by rw [rpow_def_of_pos hx, exp_lt_one_iff, mul_neg_iff, log_pos_iff hx, log_neg_iff hx] lemma rpow_lt_one_iff (hx : 0 ≤ x) : x ^ y < 1 ↔ x = 0 ∧ y ≠ 0 ∨ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, zero_lt_one] }, { simp [rpow_lt_one_iff_of_pos hx, hx.ne.symm] } end lemma one_lt_rpow_iff_of_pos (hx : 0 < x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ x < 1 ∧ y < 0 := by rw [rpow_def_of_pos hx, one_lt_exp_iff, mul_pos_iff, log_pos_iff hx, log_neg_iff hx] lemma one_lt_rpow_iff (hx : 0 ≤ x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ 0 < x ∧ x < 1 ∧ y < 0 := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, (@zero_lt_one ℝ _).not_lt] }, { simp [one_lt_rpow_iff_of_pos hx, hx] } end lemma rpow_le_one_iff_of_pos (hx : 0 < x) : x ^ y ≤ 1 ↔ 1 ≤ x ∧ y ≤ 0 ∨ x ≤ 1 ∧ 0 ≤ y := by rw [rpow_def_of_pos hx, exp_le_one_iff, mul_nonpos_iff, log_nonneg_iff hx, log_nonpos_iff hx] lemma pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ n) ^ (n⁻¹ : ℝ) = x := have hn0 : (n : ℝ) ≠ 0, by simpa [nat.pos_iff_ne_zero] using hn, by rw [← rpow_nat_cast, ← rpow_mul hx, mul_inv_cancel hn0, rpow_one] lemma rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℝ)) ^ n = x := have hn0 : (n : ℝ) ≠ 0, by simpa [nat.pos_iff_ne_zero] using hn, by rw [← rpow_nat_cast, ← rpow_mul hx, inv_mul_cancel hn0, rpow_one] section prove_rpow_is_continuous lemma continuous_rpow_aux1 : continuous (λp : {p:ℝ×ℝ // 0 < p.1}, p.val.1 ^ p.val.2) := suffices h : continuous (λ p : {p:ℝ×ℝ // 0 < p.1 }, exp (log p.val.1 * p.val.2)), by { convert h, ext p, rw rpow_def_of_pos p.2 }, continuous_exp.comp $ (show continuous ((λp:{p:ℝ//0 < p}, log (p.val)) ∘ (λp:{p:ℝ×ℝ//0<p.fst}, ⟨p.val.1, p.2⟩)), from continuous_log'.comp $ continuous_subtype_mk _ $ continuous_fst.comp continuous_subtype_val).mul (continuous_snd.comp $ continuous_subtype_val.comp continuous_id) lemma continuous_rpow_aux2 : continuous (λ p : {p:ℝ×ℝ // p.1 < 0}, p.val.1 ^ p.val.2) := suffices h : continuous (λp:{p:ℝ×ℝ // p.1 < 0}, exp (log (-p.val.1) * p.val.2) * cos (p.val.2 * π)), by { convert h, ext p, rw [rpow_def_of_neg p.2, log_neg_eq_log] }, (continuous_exp.comp $ (show continuous $ (λp:{p:ℝ//0<p}, log (p.val))∘(λp:{p:ℝ×ℝ//p.1<0}, ⟨-p.val.1, neg_pos_of_neg p.2⟩), from continuous_log'.comp $ continuous_subtype_mk _ $ continuous_neg.comp $ continuous_fst.comp continuous_subtype_val).mul (continuous_snd.comp $ continuous_subtype_val.comp continuous_id)).mul (continuous_cos.comp $ (continuous_snd.comp $ continuous_subtype_val.comp continuous_id).mul continuous_const) lemma continuous_at_rpow_of_ne_zero (hx : x ≠ 0) (y : ℝ) : continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) := begin cases lt_trichotomy 0 x, exact continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_rpow_aux1 _ h) (mem_nhds_sets (by { convert (is_open_lt' (0:ℝ)).prod is_open_univ, ext, finish }) h), cases h, { exact absurd h.symm hx }, exact continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_rpow_aux2 _ h) (mem_nhds_sets (by { convert (is_open_gt' (0:ℝ)).prod is_open_univ, ext, finish }) h) end lemma continuous_rpow_aux3 : continuous (λ p : {p:ℝ×ℝ // 0 < p.2}, p.val.1 ^ p.val.2) := continuous_iff_continuous_at.2 $ λ ⟨(x₀, y₀), hy₀⟩, begin by_cases hx₀ : x₀ = 0, { simp only [continuous_at, hx₀, zero_rpow (ne_of_gt hy₀), metric.tendsto_nhds_nhds], assume ε ε0, rcases exists_pos_rat_lt (half_pos hy₀) with ⟨q, q_pos, q_lt⟩, let q := (q:ℝ), replace q_pos : 0 < q := rat.cast_pos.2 q_pos, let δ := min (min q (ε ^ (1 / q))) (1/2), have δ0 : 0 < δ := lt_min (lt_min q_pos (rpow_pos_of_pos ε0 _)) (by norm_num), have : δ ≤ q := le_trans (min_le_left _ _) (min_le_left _ _), have : δ ≤ ε ^ (1 / q) := le_trans (min_le_left _ _) (min_le_right _ _), have : δ < 1 := lt_of_le_of_lt (min_le_right _ _) (by norm_num), use δ, use δ0, rintros ⟨⟨x, y⟩, hy⟩, simp only [subtype.dist_eq, real.dist_eq, prod.dist_eq, sub_zero, subtype.coe_mk], assume h, rw max_lt_iff at h, cases h with xδ yy₀, have qy : q < y, calc q < y₀ / 2 : q_lt ... = y₀ - y₀ / 2 : (sub_half _).symm ... ≤ y₀ - δ : by linarith ... < y : sub_lt_of_abs_sub_lt_left yy₀, calc abs(x^y) ≤ abs(x)^y : abs_rpow_le_abs_rpow _ _ ... < δ ^ y : rpow_lt_rpow (abs_nonneg _) xδ hy ... < δ ^ q : by { refine rpow_lt_rpow_of_exponent_gt _ _ _, repeat {linarith} } ... ≤ (ε ^ (1 / q)) ^ q : by { refine rpow_le_rpow _ _ _, repeat {linarith} } ... = ε : by { rw [← rpow_mul, div_mul_cancel, rpow_one], exact ne_of_gt q_pos, linarith }}, { exact (continuous_within_at_iff_continuous_at_restrict (λp:ℝ×ℝ, p.1^p.2) _).1 (continuous_at_rpow_of_ne_zero hx₀ _).continuous_within_at } end lemma continuous_at_rpow_of_pos (hy : 0 < y) (x : ℝ) : continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) := continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_rpow_aux3 _ hy) (mem_nhds_sets (by { convert is_open_univ.prod (is_open_lt' (0:ℝ)), ext, finish }) hy) lemma continuous_at_rpow {x y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (λp:ℝ×ℝ, p.1^p.2) (x, y) := by { cases h, exact continuous_at_rpow_of_ne_zero h _, exact continuous_at_rpow_of_pos h x } variables {α : Type*} [topological_space α] {f g : α → ℝ} /-- `real.rpow` is continuous at all points except for the lower half of the y-axis. In other words, the function `λp:ℝ×ℝ, p.1^p.2` is continuous at `(x, y)` if `x ≠ 0` or `y > 0`. Multiple forms of the claim is provided in the current section. -/ lemma continuous_rpow (h : ∀a, f a ≠ 0 ∨ 0 < g a) (hf : continuous f) (hg : continuous g): continuous (λa:α, (f a) ^ (g a)) := continuous_iff_continuous_at.2 $ λ a, begin show continuous_at ((λp:ℝ×ℝ, p.1^p.2) ∘ (λa, (f a, g a))) a, refine continuous_at.comp _ (continuous_iff_continuous_at.1 (hf.prod_mk hg) _), { replace h := h a, cases h, { exact continuous_at_rpow_of_ne_zero h _ }, { exact continuous_at_rpow_of_pos h _ }}, end lemma continuous_rpow_of_ne_zero (h : ∀a, f a ≠ 0) (hf : continuous f) (hg : continuous g): continuous (λa:α, (f a) ^ (g a)) := continuous_rpow (λa, or.inl $ h a) hf hg lemma continuous_rpow_of_pos (h : ∀a, 0 < g a) (hf : continuous f) (hg : continuous g): continuous (λa:α, (f a) ^ (g a)) := continuous_rpow (λa, or.inr $ h a) hf hg end prove_rpow_is_continuous section prove_rpow_is_differentiable lemma has_deriv_at_rpow_of_pos {x : ℝ} (h : 0 < x) (p : ℝ) : has_deriv_at (λ x, x^p) (p * x^(p-1)) x := begin have : has_deriv_at (λ x, exp (log x * p)) (p * x^(p-1)) x, { convert (has_deriv_at_exp _).comp x ((has_deriv_at_log (ne_of_gt h)).mul_const p) using 1, field_simp [rpow_def_of_pos h, mul_sub, exp_sub, exp_log h, ne_of_gt h], ring }, apply this.congr_of_eventually_eq, have : set.Ioi (0 : ℝ) ∈ 𝓝 x := mem_nhds_sets is_open_Ioi h, exact filter.eventually_of_mem this (λ y hy, rpow_def_of_pos hy _) end lemma has_deriv_at_rpow_of_neg {x : ℝ} (h : x < 0) (p : ℝ) : has_deriv_at (λ x, x^p) (p * x^(p-1)) x := begin have : has_deriv_at (λ x, exp (log x * p) * cos (p * π)) (p * x^(p-1)) x, { convert ((has_deriv_at_exp _).comp x ((has_deriv_at_log (ne_of_lt h)).mul_const p)).mul_const _ using 1, field_simp [rpow_def_of_neg h, mul_sub, exp_sub, sub_mul, cos_sub, exp_log_of_neg h, ne_of_lt h], ring }, apply this.congr_of_eventually_eq, have : set.Iio (0 : ℝ) ∈ 𝓝 x := mem_nhds_sets is_open_Iio h, exact filter.eventually_of_mem this (λ y hy, rpow_def_of_neg hy _) end lemma has_deriv_at_rpow {x : ℝ} (h : x ≠ 0) (p : ℝ) : has_deriv_at (λ x, x^p) (p * x^(p-1)) x := begin rcases lt_trichotomy x 0 with H|H|H, { exact has_deriv_at_rpow_of_neg H p }, { exact (h H).elim }, { exact has_deriv_at_rpow_of_pos H p }, end lemma has_deriv_at_rpow_zero_of_one_le {p : ℝ} (h : 1 ≤ p) : has_deriv_at (λ x, x^p) (p * (0 : ℝ)^(p-1)) 0 := begin apply has_deriv_at_of_has_deriv_at_of_ne (λ x hx, has_deriv_at_rpow hx p), { exact (continuous_rpow_of_pos (λ _, (lt_of_lt_of_le zero_lt_one h)) continuous_id continuous_const).continuous_at }, { rcases le_iff_eq_or_lt.1 h with rfl|h, { simp [continuous_const.continuous_at] }, { exact (continuous_const.mul (continuous_rpow_of_pos (λ _, sub_pos_of_lt h) continuous_id continuous_const)).continuous_at } } end lemma has_deriv_at_rpow_of_one_le (x : ℝ) {p : ℝ} (h : 1 ≤ p) : has_deriv_at (λ x, x^p) (p * x^(p-1)) x := begin by_cases hx : x = 0, { rw hx, exact has_deriv_at_rpow_zero_of_one_le h }, { exact has_deriv_at_rpow hx p } end end prove_rpow_is_differentiable section sqrt lemma sqrt_eq_rpow : sqrt = λx:ℝ, x ^ (1/(2:ℝ)) := begin funext, by_cases h : 0 ≤ x, { rw [← mul_self_inj_of_nonneg, mul_self_sqrt h, ← pow_two, ← rpow_nat_cast, ← rpow_mul h], norm_num, exact sqrt_nonneg _, exact rpow_nonneg_of_nonneg h _ }, { replace h : x < 0 := lt_of_not_ge h, have : 1 / (2:ℝ) * π = π / (2:ℝ), ring, rw [sqrt_eq_zero_of_nonpos (le_of_lt h), rpow_def_of_neg h, this, cos_pi_div_two, mul_zero] } end lemma continuous_sqrt : continuous sqrt := by rw sqrt_eq_rpow; exact continuous_rpow_of_pos (λa, by norm_num) continuous_id continuous_const end sqrt end real section differentiability open real variables {f : ℝ → ℝ} {x f' : ℝ} {s : set ℝ} (p : ℝ) /- Differentiability statements for the power of a function, when the function does not vanish and the exponent is arbitrary-/ lemma has_deriv_within_at.rpow (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (λ y, (f y)^p) (f' * p * (f x)^(p-1)) s x := begin convert (has_deriv_at_rpow hx p).comp_has_deriv_within_at x hf using 1, ring end lemma has_deriv_at.rpow (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (λ y, (f y)^p) (f' * p * (f x)^(p-1)) x := begin rw ← has_deriv_within_at_univ at *, exact hf.rpow p hx end lemma differentiable_within_at.rpow (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (λx, (f x)^p) s x := (hf.has_deriv_within_at.rpow p hx).differentiable_within_at @[simp] lemma differentiable_at.rpow (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (λx, (f x)^p) x := (hf.has_deriv_at.rpow p hx).differentiable_at lemma differentiable_on.rpow (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λx, (f x)^p) s := λx h, (hf x h).rpow p (hx x h) @[simp] lemma differentiable.rpow (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) : differentiable ℝ (λx, (f x)^p) := λx, (hf x).rpow p (hx x) lemma deriv_within_rpow (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, (f x)^p) s x = (deriv_within f s x) * p * (f x)^(p-1) := (hf.has_deriv_within_at.rpow p hx).deriv_within hxs @[simp] lemma deriv_rpow (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (λx, (f x)^p) x = (deriv f x) * p * (f x)^(p-1) := (hf.has_deriv_at.rpow p hx).deriv /- Differentiability statements for the power of a function, when the function may vanish but the exponent is at least one. -/ variable {p} lemma has_deriv_within_at.rpow_of_one_le (hf : has_deriv_within_at f f' s x) (hp : 1 ≤ p) : has_deriv_within_at (λ y, (f y)^p) (f' * p * (f x)^(p-1)) s x := begin convert (has_deriv_at_rpow_of_one_le (f x) hp).comp_has_deriv_within_at x hf using 1, ring end lemma has_deriv_at.rpow_of_one_le (hf : has_deriv_at f f' x) (hp : 1 ≤ p) : has_deriv_at (λ y, (f y)^p) (f' * p * (f x)^(p-1)) x := begin rw ← has_deriv_within_at_univ at *, exact hf.rpow_of_one_le hp end lemma differentiable_within_at.rpow_of_one_le (hf : differentiable_within_at ℝ f s x) (hp : 1 ≤ p) : differentiable_within_at ℝ (λx, (f x)^p) s x := (hf.has_deriv_within_at.rpow_of_one_le hp).differentiable_within_at @[simp] lemma differentiable_at.rpow_of_one_le (hf : differentiable_at ℝ f x) (hp : 1 ≤ p) : differentiable_at ℝ (λx, (f x)^p) x := (hf.has_deriv_at.rpow_of_one_le hp).differentiable_at lemma differentiable_on.rpow_of_one_le (hf : differentiable_on ℝ f s) (hp : 1 ≤ p) : differentiable_on ℝ (λx, (f x)^p) s := λx h, (hf x h).rpow_of_one_le hp @[simp] lemma differentiable.rpow_of_one_le (hf : differentiable ℝ f) (hp : 1 ≤ p) : differentiable ℝ (λx, (f x)^p) := λx, (hf x).rpow_of_one_le hp lemma deriv_within_rpow_of_one_le (hf : differentiable_within_at ℝ f s x) (hp : 1 ≤ p) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, (f x)^p) s x = (deriv_within f s x) * p * (f x)^(p-1) := (hf.has_deriv_within_at.rpow_of_one_le hp).deriv_within hxs @[simp] lemma deriv_rpow_of_one_le (hf : differentiable_at ℝ f x) (hp : 1 ≤ p) : deriv (λx, (f x)^p) x = (deriv f x) * p * (f x)^(p-1) := (hf.has_deriv_at.rpow_of_one_le hp).deriv /- Differentiability statements for the square root of a function, when the function does not vanish -/ lemma has_deriv_within_at.sqrt (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (λ y, sqrt (f y)) (f' / (2 * sqrt (f x))) s x := begin simp only [sqrt_eq_rpow], convert hf.rpow (1/2) hx, rcases lt_trichotomy (f x) 0 with H|H|H, { have A : (f x)^((1:ℝ)/2) = 0, { rw rpow_def_of_neg H, have : cos (1/2 * π) = 0, by { convert cos_pi_div_two using 2, ring }, rw [this], simp }, have B : f x ^ ((1:ℝ) / 2 - 1) = 0, { rw rpow_def_of_neg H, have : cos (π/2 - π) = 0, by simp [cos_sub], have : cos (((1:ℝ)/2 - 1) * π) = 0, by { convert this using 2, ring }, rw this, simp }, rw [A, B], simp }, { exact (hx H).elim }, { have A : 0 < (f x)^((1:ℝ)/2) := rpow_pos_of_pos H _, have B : (f x) ^ (-(1:ℝ)) = (f x)^(-((1:ℝ)/2)) * (f x)^(-((1:ℝ)/2)), { rw [← rpow_add H], congr, norm_num }, rw [sub_eq_add_neg, rpow_add H, B, rpow_neg (le_of_lt H)], field_simp [hx, ne_of_gt A], ring } end lemma has_deriv_at.sqrt (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (λ y, sqrt (f y)) (f' / (2 * sqrt(f x))) x := begin rw ← has_deriv_within_at_univ at *, exact hf.sqrt hx end lemma differentiable_within_at.sqrt (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (λx, sqrt (f x)) s x := (hf.has_deriv_within_at.sqrt hx).differentiable_within_at @[simp] lemma differentiable_at.sqrt (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (λx, sqrt (f x)) x := (hf.has_deriv_at.sqrt hx).differentiable_at lemma differentiable_on.sqrt (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λx, sqrt (f x)) s := λx h, (hf x h).sqrt (hx x h) @[simp] lemma differentiable.sqrt (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) : differentiable ℝ (λx, sqrt (f x)) := λx, (hf x).sqrt (hx x) lemma deriv_within_sqrt (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, sqrt (f x)) s x = (deriv_within f s x) / (2 * sqrt (f x)) := (hf.has_deriv_within_at.sqrt hx).deriv_within hxs @[simp] lemma deriv_sqrt (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (λx, sqrt (f x)) x = (deriv f x) / (2 * sqrt (f x)) := (hf.has_deriv_at.sqrt hx).deriv end differentiability namespace nnreal /-- The nonnegative real power function `x^y`, defined for `x : ℝ≥0` and `y : ℝ ` as the restriction of the real power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0 = 1` and `0 ^ y = 0` for `y ≠ 0`. -/ noncomputable def rpow (x : ℝ≥0) (y : ℝ) : ℝ≥0 := ⟨(x : ℝ) ^ y, real.rpow_nonneg_of_nonneg x.2 y⟩ noncomputable instance : has_pow ℝ≥0 ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ℝ≥0) (y : ℝ) : rpow x y = x ^ y := rfl @[simp, norm_cast] lemma coe_rpow (x : ℝ≥0) (y : ℝ) : ((x ^ y : ℝ≥0) : ℝ) = (x : ℝ) ^ y := rfl @[simp] lemma rpow_zero (x : ℝ≥0) : x ^ (0 : ℝ) = 1 := nnreal.eq $ real.rpow_zero _ @[simp] lemma rpow_eq_zero_iff {x : ℝ≥0} {y : ℝ} : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := begin rw [← nnreal.coe_eq, coe_rpow, ← nnreal.coe_eq_zero], exact real.rpow_eq_zero_iff_of_nonneg x.2 end @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ≥0) ^ x = 0 := nnreal.eq $ real.zero_rpow h @[simp] lemma rpow_one (x : ℝ≥0) : x ^ (1 : ℝ) = x := nnreal.eq $ real.rpow_one _ @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ≥0) ^ x = 1 := nnreal.eq $ real.one_rpow _ lemma rpow_add {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add (zero_lt_iff_ne_zero.2 hx) _ _ lemma rpow_add' (x : ℝ≥0) {y z : ℝ} (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := nnreal.eq $ real.rpow_add' x.2 h lemma rpow_mul (x : ℝ≥0) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := nnreal.eq $ real.rpow_mul x.2 y z lemma rpow_neg (x : ℝ≥0) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := nnreal.eq $ real.rpow_neg x.2 _ lemma rpow_neg_one (x : ℝ≥0) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_sub {x : ℝ≥0} (hx : x ≠ 0) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub (zero_lt_iff_ne_zero.2 hx) y z lemma rpow_sub' (x : ℝ≥0) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := nnreal.eq $ real.rpow_sub' x.2 h lemma inv_rpow (x : ℝ≥0) (y : ℝ) : (x⁻¹) ^ y = (x ^ y)⁻¹ := nnreal.eq $ real.inv_rpow x.2 y lemma div_rpow (x y : ℝ≥0) (z : ℝ) : (x / y) ^ z = x ^ z / y ^ z := nnreal.eq $ real.div_rpow x.2 y.2 z @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ≥0) (n : ℕ) : x ^ (n : ℝ) = x ^ n := nnreal.eq $ by simpa only [coe_rpow, coe_pow] using real.rpow_nat_cast x n lemma mul_rpow {x y : ℝ≥0} {z : ℝ} : (x*y)^z = x^z * y^z := nnreal.eq $ real.mul_rpow x.2 y.2 lemma rpow_le_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := real.rpow_le_rpow x.2 h₁ h₂ lemma rpow_lt_rpow {x y : ℝ≥0} {z: ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := real.rpow_lt_rpow x.2 h₁ h₂ lemma rpow_lt_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := real.rpow_lt_rpow_iff x.2 y.2 hz lemma rpow_le_rpow_iff {x y : ℝ≥0} {z : ℝ} (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := real.rpow_le_rpow_iff x.2 y.2 hz lemma rpow_lt_rpow_of_exponent_lt {x : ℝ≥0} {y z : ℝ} (hx : 1 < x) (hyz : y < z) : x^y < x^z := real.rpow_lt_rpow_of_exponent_lt hx hyz lemma rpow_le_rpow_of_exponent_le {x : ℝ≥0} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_le hx hyz lemma rpow_lt_rpow_of_exponent_gt {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := real.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz lemma rpow_le_rpow_of_exponent_ge {x : ℝ≥0} {y z : ℝ} (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := real.rpow_le_rpow_of_exponent_ge hx0 hx1 hyz lemma rpow_lt_one {x : ℝ≥0} {z : ℝ} (hx : 0 ≤ x) (hx1 : x < 1) (hz : 0 < z) : x^z < 1 := real.rpow_lt_one hx hx1 hz lemma rpow_le_one {x : ℝ≥0} {z : ℝ} (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := real.rpow_le_one x.2 hx2 hz lemma rpow_lt_one_of_one_lt_of_neg {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := real.rpow_lt_one_of_one_lt_of_neg hx hz lemma rpow_le_one_of_one_le_of_nonpos {x : ℝ≥0} {z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := real.rpow_le_one_of_one_le_of_nonpos hx hz lemma one_lt_rpow {x : ℝ≥0} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := real.one_lt_rpow hx hz lemma one_le_rpow {x : ℝ≥0} {z : ℝ} (h : 1 ≤ x) (h₁ : 0 ≤ z) : 1 ≤ x^z := real.one_le_rpow h h₁ lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := real.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz lemma one_le_rpow_of_pos_of_le_one_of_nonpos {x : ℝ≥0} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := real.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 hz lemma pow_nat_rpow_nat_inv (x : ℝ≥0) {n : ℕ} (hn : 0 < n) : (x ^ n) ^ (n⁻¹ : ℝ) = x := by { rw [← nnreal.coe_eq, coe_rpow, nnreal.coe_pow], exact real.pow_nat_rpow_nat_inv x.2 hn } lemma rpow_nat_inv_pow_nat (x : ℝ≥0) {n : ℕ} (hn : 0 < n) : (x ^ (n⁻¹ : ℝ)) ^ n = x := by { rw [← nnreal.coe_eq, nnreal.coe_pow, coe_rpow], exact real.rpow_nat_inv_pow_nat x.2 hn } lemma continuous_at_rpow {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 < y) : continuous_at (λp:ℝ≥0×ℝ, p.1^p.2) (x, y) := begin have : (λp:ℝ≥0×ℝ, p.1^p.2) = nnreal.of_real ∘ (λp:ℝ×ℝ, p.1^p.2) ∘ (λp:ℝ≥0 × ℝ, (p.1.1, p.2)), { ext p, rw [coe_rpow, nnreal.coe_of_real _ (real.rpow_nonneg_of_nonneg p.1.2 _)], refl }, rw this, refine nnreal.continuous_of_real.continuous_at.comp (continuous_at.comp _ _), { apply real.continuous_at_rpow, simp at h, rw ← (nnreal.coe_eq_zero x) at h, exact h }, { exact ((continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd).continuous_at } end end nnreal open filter lemma filter.tendsto.nnrpow {α : Type*} {f : filter α} {u : α → ℝ≥0} {v : α → ℝ} {x : ℝ≥0} {y : ℝ} (hx : tendsto u f (𝓝 x)) (hy : tendsto v f (𝓝 y)) (h : x ≠ 0 ∨ 0 < y) : tendsto (λ a, (u a) ^ (v a)) f (𝓝 (x ^ y)) := tendsto.comp (nnreal.continuous_at_rpow h) (hx.prod_mk_nhds hy) namespace nnreal lemma continuous_at_rpow_const {x : ℝ≥0} {y : ℝ} (h : x ≠ 0 ∨ 0 ≤ y) : continuous_at (λ z, z^y) x := h.elim (λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inl h)) $ λ h, h.eq_or_lt.elim (λ h, h ▸ by simp only [rpow_zero, continuous_at_const]) (λ h, tendsto_id.nnrpow tendsto_const_nhds (or.inr h)) lemma continuous_rpow_const {y : ℝ} (h : 0 ≤ y) : continuous (λ x : ℝ≥0, x^y) := continuous_iff_continuous_at.2 $ λ x, continuous_at_rpow_const (or.inr h) end nnreal namespace ennreal /-- The real power function `x^y` on extended nonnegative reals, defined for `x : ennreal` and `y : ℝ` as the restriction of the real power function if `0 < x < ⊤`, and with the natural values for `0` and `⊤` (i.e., `0 ^ x = 0` for `x > 0`, `1` for `x = 0` and `⊤` for `x < 0`, and `⊤ ^ x = 1 / 0 ^ x`). -/ noncomputable def rpow : ennreal → ℝ → ennreal | (some x) y := if x = 0 ∧ y < 0 then ⊤ else (x ^ y : ℝ≥0) | none y := if 0 < y then ⊤ else if y = 0 then 1 else 0 noncomputable instance : has_pow ennreal ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x : ennreal) (y : ℝ) : rpow x y = x ^ y := rfl @[simp] lemma rpow_zero {x : ennreal} : x ^ (0 : ℝ) = 1 := by cases x; { dsimp only [(^), rpow], simp [lt_irrefl] } lemma top_rpow_def (y : ℝ) : (⊤ : ennreal) ^ y = if 0 < y then ⊤ else if y = 0 then 1 else 0 := rfl @[simp] lemma top_rpow_of_pos {y : ℝ} (h : 0 < y) : (⊤ : ennreal) ^ y = ⊤ := by simp [top_rpow_def, h] @[simp] lemma top_rpow_of_neg {y : ℝ} (h : y < 0) : (⊤ : ennreal) ^ y = 0 := by simp [top_rpow_def, asymm h, ne_of_lt h] @[simp] lemma zero_rpow_of_pos {y : ℝ} (h : 0 < y) : (0 : ennreal) ^ y = 0 := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, asymm h, ne_of_gt h], end @[simp] lemma zero_rpow_of_neg {y : ℝ} (h : y < 0) : (0 : ennreal) ^ y = ⊤ := begin rw [← ennreal.coe_zero, ← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h, ne_of_gt h], end lemma zero_rpow_def (y : ℝ) : (0 : ennreal) ^ y = if 0 < y then 0 else if y = 0 then 1 else ⊤ := begin rcases lt_trichotomy 0 y with H|rfl|H, { simp [H, ne_of_gt, zero_rpow_of_pos, lt_irrefl] }, { simp [lt_irrefl] }, { simp [H, asymm H, ne_of_lt, zero_rpow_of_neg] } end @[norm_cast] lemma coe_rpow_of_ne_zero {x : ℝ≥0} (h : x ≠ 0) (y : ℝ) : (x : ennreal) ^ y = (x ^ y : ℝ≥0) := begin rw [← ennreal.some_eq_coe], dsimp only [(^), rpow], simp [h] end @[norm_cast] lemma coe_rpow_of_nonneg (x : ℝ≥0) {y : ℝ} (h : 0 ≤ y) : (x : ennreal) ^ y = (x ^ y : ℝ≥0) := begin by_cases hx : x = 0, { rcases le_iff_eq_or_lt.1 h with H|H, { simp [hx, H.symm] }, { simp [hx, zero_rpow_of_pos H, nnreal.zero_rpow (ne_of_gt H)] } }, { exact coe_rpow_of_ne_zero hx _ } end @[simp] lemma rpow_one (x : ennreal) : x ^ (1 : ℝ) = x := by cases x; dsimp only [(^), rpow]; simp [zero_lt_one, not_lt_of_le zero_le_one] @[simp] lemma one_rpow (x : ℝ) : (1 : ennreal) ^ x = 1 := by { rw [← coe_one, coe_rpow_of_ne_zero one_ne_zero], simp } @[simp] lemma rpow_eq_zero_iff {x : ennreal} {y : ℝ} : x ^ y = 0 ↔ (x = 0 ∧ 0 < y) ∨ (x = ⊤ ∧ y < 0) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end @[simp] lemma rpow_eq_top_iff {x : ennreal} {y : ℝ} : x ^ y = ⊤ ↔ (x = 0 ∧ y < 0) ∨ (x = ⊤ ∧ 0 < y) := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [H, top_rpow_of_neg, top_rpow_of_pos, le_of_lt] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, H, zero_rpow_of_neg, zero_rpow_of_pos, le_of_lt] }, { simp [coe_rpow_of_ne_zero h, h] } } end lemma rpow_add {x : ennreal} (y z : ℝ) (hx : x ≠ 0) (h'x : x ≠ ⊤) : x ^ (y + z) = x ^ y * x ^ z := begin cases x, { exact (h'x rfl).elim }, have : x ≠ 0 := λ h, by simpa [h] using hx, simp [coe_rpow_of_ne_zero this, nnreal.rpow_add this] end lemma rpow_neg (x : ennreal) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := begin cases x, { rcases lt_trichotomy y 0 with H|H|H; simp [top_rpow_of_pos, top_rpow_of_neg, H, neg_pos.mpr] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with H|H|H; simp [h, zero_rpow_of_pos, zero_rpow_of_neg, H, neg_pos.mpr] }, { have A : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, ← coe_inv A, nnreal.rpow_neg] } } end lemma rpow_neg_one (x : ennreal) : x ^ (-1 : ℝ) = x ⁻¹ := by simp [rpow_neg] lemma rpow_mul (x : ennreal) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [h, Hy, Hz, zero_rpow_of_neg, zero_rpow_of_pos, top_rpow_of_neg, top_rpow_of_pos, mul_pos_of_neg_of_neg, mul_neg_of_neg_of_pos, mul_neg_of_pos_of_neg] }, { have : x ^ y ≠ 0, by simp [h], simp [coe_rpow_of_ne_zero h, coe_rpow_of_ne_zero this, nnreal.rpow_mul] } } end @[simp, norm_cast] lemma rpow_nat_cast (x : ennreal) (n : ℕ) : x ^ (n : ℝ) = x ^ n := begin cases x, { cases n; simp [top_rpow_of_pos (nat.cast_add_one_pos _), top_pow (nat.succ_pos _)] }, { simp [coe_rpow_of_nonneg _ (nat.cast_nonneg n)] } end @[norm_cast] lemma coe_mul_rpow (x y : ℝ≥0) (z : ℝ) : ((x : ennreal) * y) ^ z = x^z * y^z := begin rcases lt_trichotomy z 0 with H|H|H, { by_cases hx : x = 0; by_cases hy : y = 0, { simp [hx, hy, zero_rpow_of_neg, H] }, { have : (y : ennreal) ^ z ≠ 0, by simp [rpow_eq_zero_iff, hy], simp [hx, hy, zero_rpow_of_neg, H, with_top.top_mul this] }, { have : (x : ennreal) ^ z ≠ 0, by simp [rpow_eq_zero_iff, hx], simp [hx, hy, zero_rpow_of_neg H, with_top.mul_top this] }, { rw [← coe_mul, coe_rpow_of_ne_zero, nnreal.mul_rpow, coe_mul, coe_rpow_of_ne_zero hx, coe_rpow_of_ne_zero hy], simp [hx, hy] } }, { simp [H] }, { by_cases hx : x = 0; by_cases hy : y = 0, { simp [hx, hy, zero_rpow_of_pos, H] }, { have : (y : ennreal) ^ z ≠ 0, by simp [rpow_eq_zero_iff, hy], simp [hx, hy, zero_rpow_of_pos H, with_top.top_mul this] }, { have : (x : ennreal) ^ z ≠ 0, by simp [rpow_eq_zero_iff, hx], simp [hx, hy, zero_rpow_of_pos H, with_top.mul_top this] }, { rw [← coe_mul, coe_rpow_of_ne_zero, nnreal.mul_rpow, coe_mul, coe_rpow_of_ne_zero hx, coe_rpow_of_ne_zero hy], simp [hx, hy] } }, end lemma mul_rpow_of_ne_top {x y : ennreal} (hx : x ≠ ⊤) (hy : y ≠ ⊤) (z : ℝ) : (x * y) ^ z = x^z * y^z := begin lift x to ℝ≥0 using hx, lift y to ℝ≥0 using hy, exact coe_mul_rpow x y z end lemma mul_rpow_of_ne_zero {x y : ennreal} (hx : x ≠ 0) (hy : y ≠ 0) (z : ℝ) : (x * y) ^ z = x ^ z * y ^ z := begin rcases lt_trichotomy z 0 with H|H|H, { cases x; cases y, { simp [hx, hy, top_rpow_of_neg, H] }, { have : y ≠ 0, by simpa using hy, simp [hx, hy, top_rpow_of_neg, H, rpow_eq_zero_iff, this] }, { have : x ≠ 0, by simpa using hx, simp [hx, hy, top_rpow_of_neg, H, rpow_eq_zero_iff, this] }, { have hx' : x ≠ 0, by simpa using hx, have hy' : y ≠ 0, by simpa using hy, simp only [some_eq_coe], rw [← coe_mul, coe_rpow_of_ne_zero, nnreal.mul_rpow, coe_mul, coe_rpow_of_ne_zero hx', coe_rpow_of_ne_zero hy'], simp [hx', hy'] } }, { simp [H] }, { cases x; cases y, { simp [hx, hy, top_rpow_of_pos, H] }, { have : y ≠ 0, by simpa using hy, simp [hx, hy, top_rpow_of_pos, H, rpow_eq_zero_iff, this] }, { have : x ≠ 0, by simpa using hx, simp [hx, hy, top_rpow_of_pos, H, rpow_eq_zero_iff, this] }, { have hx' : x ≠ 0, by simpa using hx, have hy' : y ≠ 0, by simpa using hy, simp only [some_eq_coe], rw [← coe_mul, coe_rpow_of_ne_zero, nnreal.mul_rpow, coe_mul, coe_rpow_of_ne_zero hx', coe_rpow_of_ne_zero hy'], simp [hx', hy'] } } end lemma mul_rpow_of_nonneg (x y : ennreal) {z : ℝ} (hz : 0 ≤ z) : (x * y) ^ z = x ^ z * y ^ z := begin rcases le_iff_eq_or_lt.1 hz with H|H, { simp [← H] }, by_cases h : x = 0 ∨ y = 0, { cases h; simp [h, zero_rpow_of_pos H] }, push_neg at h, exact mul_rpow_of_ne_zero h.1 h.2 z end lemma rpow_le_rpow {x y : ennreal} {z : ℝ} (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := begin rcases le_iff_eq_or_lt.1 h₂ with H|H, { simp [← H, le_refl] }, cases y, { simp [top_rpow_of_pos H] }, cases x, { exact (not_top_le_coe h₁).elim }, simp at h₁, simp [coe_rpow_of_nonneg _ h₂, nnreal.rpow_le_rpow h₁ h₂] end lemma rpow_lt_rpow {x y : ennreal} {z : ℝ} (h₁ : x < y) (h₂ : 0 < z) : x^z < y^z := begin cases x, { exact (not_top_lt h₁).elim }, cases y, { simp [top_rpow_of_pos h₂, coe_rpow_of_nonneg _ (le_of_lt h₂)] }, simp at h₁, simp [coe_rpow_of_nonneg _ (le_of_lt h₂), nnreal.rpow_lt_rpow h₁ h₂] end lemma rpow_lt_rpow_of_exponent_lt {x : ennreal} {y z : ℝ} (hx : 1 < x) (hx' : x ≠ ⊤) (hyz : y < z) : x^y < x^z := begin lift x to ℝ≥0 using hx', rw [one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_rpow_of_exponent_lt hx hyz] end lemma rpow_le_rpow_of_exponent_le {x : ennreal} {y z : ℝ} (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin cases x, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, top_rpow_of_neg, top_rpow_of_pos, le_refl]; linarith }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_rpow_of_exponent_le hx hyz] } end lemma rpow_lt_rpow_of_exponent_gt {x : ennreal} {y z : ℝ} (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx1 le_top), simp at hx0 hx1, simp [coe_rpow_of_ne_zero (ne_of_gt hx0), nnreal.rpow_lt_rpow_of_exponent_gt hx0 hx1 hyz] end lemma rpow_le_rpow_of_exponent_ge {x : ennreal} {y z : ℝ} (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx1 coe_lt_top), by_cases h : x = 0, { rcases lt_trichotomy y 0 with Hy|Hy|Hy; rcases lt_trichotomy z 0 with Hz|Hz|Hz; simp [Hy, Hz, h, zero_rpow_of_neg, zero_rpow_of_pos, le_refl]; linarith }, { simp at hx1, simp [coe_rpow_of_ne_zero h, nnreal.rpow_le_rpow_of_exponent_ge (bot_lt_iff_ne_bot.mpr h) hx1 hyz] } end lemma rpow_lt_one {x : ennreal} {z : ℝ} (hx : x < 1) (hz : 0 < z) : x^z < 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx le_top), simp only [coe_lt_one_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.rpow_lt_one (zero_le x) hx hz], end lemma rpow_le_one {x : ennreal} {z : ℝ} (hx : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx coe_lt_top), simp only [coe_le_one_iff] at hx, simp [coe_rpow_of_nonneg _ hz, nnreal.rpow_le_one hx hz], end lemma rpow_lt_one_of_one_lt_of_neg {x : ennreal} {z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := begin cases x, { simp [top_rpow_of_neg hz, ennreal.zero_lt_one] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_trans zero_lt_one hx)), nnreal.rpow_lt_one_of_one_lt_of_neg hx hz] }, end lemma rpow_le_one_of_one_le_of_neg {x : ennreal} {z : ℝ} (hx : 1 ≤ x) (hz : z < 0) : x^z ≤ 1 := begin cases x, { simp [top_rpow_of_neg hz, ennreal.zero_lt_one] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hx)), nnreal.rpow_le_one_of_one_le_of_nonpos hx (le_of_lt hz)] }, end lemma one_lt_rpow {x : ennreal} {z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [some_eq_coe, one_lt_coe_iff] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_lt_rpow hx hz] } end lemma one_le_rpow {x : ennreal} {z : ℝ} (hx : 1 ≤ x) (hz : 0 < z) : 1 ≤ x^z := begin cases x, { simp [top_rpow_of_pos hz] }, { simp only [one_le_coe_iff, some_eq_coe] at hx, simp [coe_rpow_of_nonneg _ (le_of_lt hz), nnreal.one_le_rpow hx (le_of_lt hz)] }, end lemma one_lt_rpow_of_pos_of_lt_one_of_neg {x : ennreal} {z : ℝ} (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_lt_of_le hx2 le_top), simp only [coe_lt_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_lt_rpow_of_pos_of_lt_one_of_neg hx1 hx2 hz], end lemma one_le_rpow_of_pos_of_le_one_of_neg {x : ennreal} {z : ℝ} (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z < 0) : 1 ≤ x^z := begin lift x to ℝ≥0 using ne_of_lt (lt_of_le_of_lt hx2 coe_lt_top), simp only [coe_le_one_iff, coe_pos] at ⊢ hx1 hx2, simp [coe_rpow_of_ne_zero (ne_of_gt hx1), nnreal.one_le_rpow_of_pos_of_le_one_of_nonpos hx1 hx2 (le_of_lt hz)], end lemma to_real_rpow (x : ennreal) (z : ℝ) : (x.to_real) ^ z = (x ^ z).to_real := begin rcases lt_trichotomy z 0 with H|H|H, { cases x, { simp [H, ne_of_lt] }, by_cases hx : x = 0, { simp [hx, H, ne_of_lt] }, { simp [coe_rpow_of_ne_zero hx] } }, { simp [H] }, { cases x, { simp [H, ne_of_gt] }, simp [coe_rpow_of_nonneg _ (le_of_lt H)] } end end ennreal
8e5fc39ec0371df4850792511c8aa1a5c11e9f68
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/mvar3.lean
026ad0ece54435298dfdabe727fcc510e6ce7bb4
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,864
lean
import Lean.MetavarContext open Lean def mkLambdaTest (mctx : MetavarContext) (ngen : NameGenerator) (lctx : LocalContext) (xs : Array Expr) (e : Expr) : Except MetavarContext.MkBinding.Exception (MetavarContext × NameGenerator × Expr) := match MetavarContext.mkLambda xs e false true lctx { mctx := mctx, ngen := ngen } with | EStateM.Result.ok e s => Except.ok (s.mctx, s.ngen, e) | EStateM.Result.error e s => Except.error e def check (b : Bool) : IO Unit := «unless» b (throw $ IO.userError "error") def f := mkConst `f def g := mkConst `g def a := mkConst `a def b := mkConst `b def c := mkConst `c def b0 := mkBVar 0 def b1 := mkBVar 1 def b2 := mkBVar 2 def u := mkLevelParam `u def typeE := mkSort levelOne def natE := mkConst `Nat def boolE := mkConst `Bool def vecE := mkConst `Vec [levelZero] def α := mkFVar `α def x := mkFVar `x def y := mkFVar `y def z := mkFVar `z def w := mkFVar `w def m1 := mkMVar `m1 def m2 := mkMVar `m2 def m3 := mkMVar `m3 def bi := BinderInfo.default def arrow (d b : Expr) := mkForall `_ bi d b def lctx1 : LocalContext := {} def lctx2 := lctx1.mkLocalDecl `α `α typeE def lctx3 := lctx2.mkLocalDecl `x `x m1 def lctx4 := lctx3.mkLocalDecl `y `y (arrow natE m2) def mctx1 : MetavarContext := {} def mctx2 := mctx1.addExprMVarDecl `m1 `m1 lctx2 #[] typeE def mctx3 := mctx2.addExprMVarDecl `m2 `m2 lctx3 #[] natE def mctx4 := mctx3.addExprMVarDecl `m3 `m3 lctx3 #[] natE def mctx4' := mctx3.addExprMVarDecl `m3 `m3 lctx3 #[] natE MetavarKind.syntheticOpaque def R1 := match mkLambdaTest mctx4 {namePrefix := `n} lctx4 #[α, x, y] $ mkAppN f #[m3, x] with | Except.ok s => s | Except.error e => panic! (toString e) def e1 := R1.2.2 def mctx5 := R1.1 def sortNames (xs : List Name) : List Name := (xs.toArray.qsort Name.lt).toList def sortNamePairs {α} [Inhabited α] (xs : List (Name × α)) : List (Name × α) := (xs.toArray.qsort (fun a b => Name.lt a.1 b.1)).toList #eval toString $ sortNames $ mctx5.decls.toList.map Prod.fst #eval toString $ sortNamePairs $ mctx5.eAssignment.toList #eval e1 #eval check (!e1.hasFVar) def R2 := match mkLambdaTest mctx4' {namePrefix := `n} lctx4 #[α, x, y] $ mkAppN f #[m3, y] with | Except.ok s => s | Except.error e => panic! (toString e) def e2 := R2.2.2 def mctx6 := R2.1 #eval toString $ sortNames $ mctx6.decls.toList.map Prod.fst #eval toString $ sortNamePairs $ mctx6.eAssignment.toList -- ?n.2 was delayed assigned because ?m.3 is synthetic #eval toString $ sortNames $ mctx6.dAssignment.toList.map Prod.fst #eval e2 #print "assigning ?m1 and ?n.1" def R3 := let mctx := mctx6.assignExpr `m3 x; let mctx := mctx.assignExpr (Name.mkNum `n 1) (mkLambda `_ bi typeE natE); -- ?n.2 is instantiated because we have the delayed assignment `?n.2 α x := ?m1` (mctx.instantiateMVars e2) def e3 := R3.1 def mctx7 := R3.2 #eval e3
4f2b9dff44b663be034a373fd0eb11cee49795ae
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/data/polynomial/eval.lean
605388058e22d1e311383b94c6d0f49f9592d1fd
[ "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
19,021
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.induction import data.polynomial.degree.basic import deprecated.ring /-! # Theory of univariate polynomials The main defs here are `eval₂`, `eval`, and `map`. We give several lemmas about their interaction with each other and with module operations. -/ noncomputable theory open finsupp finset add_monoid_algebra open_locale big_operators namespace polynomial universes u v w y variables {R : Type u} {S : Type v} {T : Type w} {ι : Type y} {a b : R} {m n : ℕ} section semiring variables [semiring R] {p q r : polynomial R} section variables [semiring S] variables (f : R →+* S) (x : S) /-- Evaluate a polynomial `p` given a ring hom `f` from the scalar ring to the target and a value `x` for the variable in the target -/ def eval₂ (p : polynomial R) : S := p.sum (λ e a, f a * x ^ e) lemma eval₂_eq_sum {f : R →+* S} {x : S} : p.eval₂ f x = p.sum (λ e a, f a * x ^ e) := rfl @[simp] lemma eval₂_zero : (0 : polynomial R).eval₂ f x = 0 := finsupp.sum_zero_index @[simp] lemma eval₂_C : (C a).eval₂ f x = f a := (sum_single_index $ by rw [f.map_zero, zero_mul]).trans $ by simp [pow_zero, mul_one] @[simp] lemma eval₂_X : X.eval₂ f x = x := (sum_single_index $ by rw [f.map_zero, zero_mul]).trans $ by rw [f.map_one, one_mul, pow_one] @[simp] lemma eval₂_monomial {n : ℕ} {r : R} : (monomial n r).eval₂ f x = (f r) * x^n := begin apply sum_single_index, simp, end @[simp] lemma eval₂_X_pow {n : ℕ} : (X^n).eval₂ f x = x^n := begin rw ←monomial_one_eq_X_pow, convert eval₂_monomial f x, simp, end @[simp] lemma eval₂_add : (p + q).eval₂ f x = p.eval₂ f x + q.eval₂ f x := finsupp.sum_add_index (λ _, by rw [f.map_zero, zero_mul]) (λ _ _ _, by rw [f.map_add, add_mul]) @[simp] lemma eval₂_one : (1 : polynomial R).eval₂ f x = 1 := by rw [← C_1, eval₂_C, f.map_one] @[simp] lemma eval₂_bit0 : (bit0 p).eval₂ f x = bit0 (p.eval₂ f x) := by rw [bit0, eval₂_add, bit0] @[simp] lemma eval₂_bit1 : (bit1 p).eval₂ f x = bit1 (p.eval₂ f x) := by rw [bit1, eval₂_add, eval₂_bit0, eval₂_one, bit1] @[simp] lemma eval₂_smul (g : R →+* S) (p : polynomial R) (x : S) {s : R} : eval₂ g x (s • p) = g s • eval₂ g x p := begin simp only [eval₂, sum_smul_index, forall_const, zero_mul, g.map_zero, g.map_mul, mul_assoc], -- Why doesn't `rw [←finsupp.mul_sum]` work? convert (@finsupp.mul_sum _ _ _ _ _ (g s) p (λ i a, (g a * x ^ i))).symm, end instance eval₂.is_add_monoid_hom : is_add_monoid_hom (eval₂ f x) := { map_zero := eval₂_zero _ _, map_add := λ _ _, eval₂_add _ _ } @[simp] lemma eval₂_nat_cast (n : ℕ) : (n : polynomial R).eval₂ f x = n := nat.rec_on n rfl $ λ n ih, by rw [n.cast_succ, eval₂_add, ih, eval₂_one, n.cast_succ] variables [semiring T] lemma eval₂_sum (p : polynomial T) (g : ℕ → T → polynomial R) (x : S) : (p.sum g).eval₂ f x = p.sum (λ n a, (g n a).eval₂ f x) := finsupp.sum_sum_index (by simp [is_add_monoid_hom.map_zero f]) (by intros; simp [right_distrib, is_add_monoid_hom.map_add f]) lemma eval₂_finset_sum (s : finset ι) (g : ι → polynomial R) (x : S) : (∑ i in s, g i).eval₂ f x = ∑ i in s, (g i).eval₂ f x := begin classical, induction s using finset.induction with p hp s hs, simp, rw [sum_insert, eval₂_add, hs, sum_insert]; assumption, end lemma eval₂_mul_noncomm (hf : ∀ b a, a * f b = f b * a) : (p * q).eval₂ f x = p.eval₂ f x * q.eval₂ f x := begin have f_zero : ∀ (a : ℕ), f 0 * x ^ a = 0, { intro, simp }, have f_add : ∀ (a : ℕ) (b₁ b₂ : R), f (b₁ + b₂) * x ^ a = f b₁ * x ^ a + f b₂ * x ^ a, { intros, rw [f.map_add, add_mul] }, simp_rw [eval₂, add_monoid_algebra.mul_def, finsupp.sum_mul _ p, finsupp.mul_sum _ q], rw sum_sum_index; try { assumption }, apply sum_congr rfl, assume i hi, dsimp only, rw sum_sum_index; try { assumption }, apply sum_congr rfl, assume j hj, dsimp only, rw [sum_single_index, f.map_mul, pow_add], { rw [mul_assoc, ←mul_assoc _ (x ^ i), ← hf _ (x ^ i), mul_assoc, mul_assoc] }, { apply f_zero } end lemma eval₂_list_prod_noncomm (ps : list (polynomial R)) (hf : ∀ b a, a * f b = f b * a): ps.prod.eval₂ f x = (ps.map (polynomial.eval₂ f x)).prod := begin induction ps, { simp }, { simp [eval₂_mul_noncomm _ _ hf, ps_ih] {contextual := tt} } end /-- `eval₂` as a `ring_hom` for noncommutative rings -/ def eval₂_ring_hom_noncomm (f : R →+* S) (hf : ∀ b a, a * f b = f b * a) (x : S) : polynomial R →+* S := { to_fun := eval₂ f x, map_add' := λ _ _, eval₂_add _ _, map_zero' := eval₂_zero _ _, map_mul' := λ _ _, eval₂_mul_noncomm _ _ hf, map_one' := eval₂_one _ _ } end /-! We next prove that eval₂ is multiplicative as long as target ring is commutative (even if the source ring is not). -/ section eval₂ variables [comm_semiring S] variables (f : R →+* S) (x : S) @[simp] lemma eval₂_mul : (p * q).eval₂ f x = p.eval₂ f x * q.eval₂ f x := begin apply eval₂_mul_noncomm, simp [mul_comm] end lemma eval₂_mul_eq_zero_of_left (q : polynomial R) (hp : p.eval₂ f x = 0) : (p * q).eval₂ f x = 0 := begin rw eval₂_mul f x, exact mul_eq_zero_of_left hp (q.eval₂ f x) end lemma eval₂_mul_eq_zero_of_right (p : polynomial R) (hq : q.eval₂ f x = 0) : (p * q).eval₂ f x = 0 := begin rw eval₂_mul f x, exact mul_eq_zero_of_right (p.eval₂ f x) hq end instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f x) := ⟨eval₂_zero _ _, eval₂_one _ _, λ _ _, eval₂_add _ _, λ _ _, eval₂_mul _ _⟩ /-- `eval₂` as a `ring_hom` -/ def eval₂_ring_hom (f : R →+* S) (x) : polynomial R →+* S := ring_hom.of (eval₂ f x) @[simp] lemma coe_eval₂_ring_hom (f : R →+* S) (x) : ⇑(eval₂_ring_hom f x) = eval₂ f x := rfl lemma eval₂_pow (n : ℕ) : (p ^ n).eval₂ f x = p.eval₂ f x ^ n := (eval₂_ring_hom _ _).map_pow _ _ end eval₂ section eval variables {x : R} /-- `eval x p` is the evaluation of the polynomial `p` at `x` -/ def eval : R → polynomial R → R := eval₂ (ring_hom.id _) lemma eval_eq_sum : p.eval x = sum p (λ e a, a * x ^ e) := rfl @[simp] lemma eval_C : (C a).eval x = a := eval₂_C _ _ @[simp] lemma eval_nat_cast {n : ℕ} : (n : polynomial R).eval x = n := by simp only [←C_eq_nat_cast, eval_C] @[simp] lemma eval_X : X.eval x = x := eval₂_X _ _ @[simp] lemma eval_monomial {n a} : (monomial n a).eval x = a * x^n := eval₂_monomial _ _ @[simp] lemma eval_zero : (0 : polynomial R).eval x = 0 := eval₂_zero _ _ @[simp] lemma eval_add : (p + q).eval x = p.eval x + q.eval x := eval₂_add _ _ @[simp] lemma eval_one : (1 : polynomial R).eval x = 1 := eval₂_one _ _ @[simp] lemma eval_bit0 : (bit0 p).eval x = bit0 (p.eval x) := eval₂_bit0 _ _ @[simp] lemma eval_bit1 : (bit1 p).eval x = bit1 (p.eval x) := eval₂_bit1 _ _ @[simp] lemma eval_smul (p : polynomial R) (x : R) {s : R} : (s • p).eval x = s • p.eval x := eval₂_smul (ring_hom.id _) _ _ lemma eval_sum (p : polynomial R) (f : ℕ → R → polynomial R) (x : R) : (p.sum f).eval x = p.sum (λ n a, (f n a).eval x) := eval₂_sum _ _ _ _ /-- `is_root p x` implies `x` is a root of `p`. The evaluation of `p` at `x` is zero -/ def is_root (p : polynomial R) (a : R) : Prop := p.eval a = 0 instance [decidable_eq R] : decidable (is_root p a) := by unfold is_root; apply_instance @[simp] lemma is_root.def : is_root p a ↔ p.eval a = 0 := iff.rfl lemma coeff_zero_eq_eval_zero (p : polynomial R) : coeff p 0 = p.eval 0 := calc coeff p 0 = coeff p 0 * 0 ^ 0 : by simp ... = p.eval 0 : eq.symm $ finset.sum_eq_single _ (λ b _ hb, by simp [zero_pow (nat.pos_of_ne_zero hb)]) (by simp) lemma zero_is_root_of_coeff_zero_eq_zero {p : polynomial R} (hp : p.coeff 0 = 0) : is_root p 0 := by rwa coeff_zero_eq_eval_zero at hp end eval section comp /-- The composition of polynomials as a polynomial. -/ def comp (p q : polynomial R) : polynomial R := p.eval₂ C q lemma comp_eq_sum_left : p.comp q = p.sum (λ e a, C a * q ^ e) := rfl @[simp] lemma comp_X : p.comp X = p := begin refine ext (λ n, _), rw [comp, eval₂], conv in (C _ * _) { rw ← single_eq_C_mul_X }, congr, convert finsupp.sum_single _, end @[simp] lemma X_comp : X.comp p = p := eval₂_X _ _ @[simp] lemma comp_C : p.comp (C a) = C (p.eval a) := begin dsimp [comp, eval₂, eval, finsupp.sum], rw [← p.support.sum_hom (@C R _)], apply finset.sum_congr rfl; simp end @[simp] lemma C_comp : (C a).comp p = C a := eval₂_C _ _ @[simp] lemma comp_zero : p.comp (0 : polynomial R) = C (p.eval 0) := by rw [← C_0, comp_C] @[simp] lemma zero_comp : comp (0 : polynomial R) p = 0 := by rw [← C_0, C_comp] @[simp] lemma comp_one : p.comp 1 = C (p.eval 1) := by rw [← C_1, comp_C] @[simp] lemma one_comp : comp (1 : polynomial R) p = 1 := by rw [← C_1, C_comp] @[simp] lemma add_comp : (p + q).comp r = p.comp r + q.comp r := eval₂_add _ _ end comp section map variables [semiring S] variables (f : R →+* S) /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : polynomial R → polynomial S := eval₂ (C.comp f) X instance is_semiring_hom_C_f : is_semiring_hom (C ∘ f) := is_semiring_hom.comp _ _ @[simp] lemma map_C : (C a).map f = C (f a) := eval₂_C _ _ @[simp] lemma map_X : X.map f = X := eval₂_X _ _ @[simp] lemma map_monomial {n a} : (monomial n a).map f = monomial n (f a) := begin dsimp only [map], rw [eval₂_monomial, single_eq_C_mul_X], refl, end @[simp] lemma map_zero : (0 : polynomial R).map f = 0 := eval₂_zero _ _ @[simp] lemma map_add : (p + q).map f = p.map f + q.map f := eval₂_add _ _ @[simp] lemma map_one : (1 : polynomial R).map f = 1 := eval₂_one _ _ @[simp] theorem map_nat_cast (n : ℕ) : (n : polynomial R).map f = n := nat.rec_on n rfl $ λ n ih, by rw [n.cast_succ, map_add, ih, map_one, n.cast_succ] @[simp] lemma coeff_map (n : ℕ) : coeff (p.map f) n = f (coeff p n) := begin rw [map, eval₂, coeff_sum], conv_rhs { rw [← sum_C_mul_X_eq p, coeff_sum, finsupp.sum, ← p.support.sum_hom f], }, refine finset.sum_congr rfl (λ x hx, _), simp [function.comp, coeff_C_mul_X, f.map_mul], split_ifs; simp [is_semiring_hom.map_zero f], end lemma map_map [semiring T] (g : S →+* T) (p : polynomial R) : (p.map f).map g = p.map (g.comp f) := ext (by simp [coeff_map]) @[simp] lemma map_id : p.map (ring_hom.id _) = p := by simp [polynomial.ext_iff, coeff_map] lemma eval₂_eq_eval_map {x : S} : p.eval₂ f x = (p.map f).eval x := begin apply polynomial.induction_on' p, { intros p q hp hq, simp [hp, hq], }, { intros n r, simp, } end lemma map_injective (hf : function.injective f): function.injective (map f) := λ p q h, ext $ λ m, hf $ by rw [← coeff_map f, ← coeff_map f, h] variables {f} lemma map_monic_eq_zero_iff (hp : p.monic) : p.map f = 0 ↔ ∀ x, f x = 0 := ⟨ λ hfp x, calc f x = f x * f p.leading_coeff : by simp [hp] ... = f x * (p.map f).coeff p.nat_degree : by { congr, apply (coeff_map _ _).symm } ... = 0 : by simp [hfp], λ h, ext (λ n, trans (coeff_map f n) (h _)) ⟩ lemma map_monic_ne_zero (hp : p.monic) [nontrivial S] : p.map f ≠ 0 := λ h, f.map_one_ne_zero ((map_monic_eq_zero_iff hp).mp h _) variables (f) open is_semiring_hom -- If the rings were commutative, we could prove this just using `eval₂_mul`. -- TODO this proof is just a hack job on the proof of `eval₂_mul`, -- using that `X` is central. It should probably be golfed! @[simp] lemma map_mul : (p * q).map f = p.map f * q.map f := begin dunfold map, dunfold eval₂, rw [add_monoid_algebra.mul_def, finsupp.sum_mul _ p], simp only [finsupp.mul_sum _ q], rw [sum_sum_index], { apply sum_congr rfl, assume i hi, dsimp only, rw [sum_sum_index], { apply sum_congr rfl, assume j hj, dsimp only, rw [sum_single_index, (C.comp f).map_mul, pow_add], { simp [←mul_assoc], conv_lhs { rw ←@X_pow_mul_assoc _ _ _ _ i }, }, { simp, } }, { intro, simp, }, { intros, simp [add_mul], } }, { intro, simp, }, { intros, simp [add_mul], } end instance map.is_semiring_hom : is_semiring_hom (map f) := { map_zero := eval₂_zero _ _, map_one := eval₂_one _ _, map_add := λ _ _, eval₂_add _ _, map_mul := λ _ _, map_mul f, } lemma map_list_prod (L : list (polynomial R)) : L.prod.map f = (L.map $ map f).prod := eq.symm $ list.prod_hom _ (monoid_hom.of (map f)) @[simp] lemma map_pow (n : ℕ) : (p ^ n).map f = p.map f ^ n := is_monoid_hom.map_pow (map f) _ _ lemma mem_map_range {p : polynomial S} : p ∈ set.range (map f) ↔ ∀ n, p.coeff n ∈ (set.range f) := begin split, { rintro ⟨p, rfl⟩ n, rw coeff_map, exact set.mem_range_self _ }, { intro h, rw p.as_sum, apply is_add_submonoid.finset_sum_mem, intros i hi, rcases h i with ⟨c, hc⟩, use [C c * X^i], rw [map_mul, map_C, hc, map_pow, map_X] } end lemma eval₂_map [semiring T] (g : S →+* T) (x : T) : (p.map f).eval₂ g x = p.eval₂ (g.comp f) x := begin convert finsupp.sum_map_range_index _, { change map f p = map_range f _ p, ext, rw map_range_apply, exact coeff_map f a, }, { exact f.map_zero, }, { intro a, simp only [ring_hom.map_zero, zero_mul], }, end lemma eval_map (x : S) : (p.map f).eval x = p.eval₂ f x := eval₂_map f (ring_hom.id _) x end map /-! After having set up the basic theory of `eval₂`, `eval`, `comp`, and `map`, we make `eval₂` irreducible. Perhaps we can make the others irreducible too? -/ attribute [irreducible] polynomial.eval₂ section hom_eval₂ -- TODO: Here we need commutativity in both `S` and `T`? variables [comm_semiring S] [comm_semiring T] variables (f : R →+* S) (g : S →+* T) (p) lemma hom_eval₂ (x : S) : g (p.eval₂ f x) = p.eval₂ (g.comp f) (g x) := begin apply polynomial.induction_on p; clear p, { intros a, rw [eval₂_C, eval₂_C], refl, }, { intros p q hp hq, simp only [hp, hq, eval₂_add, g.map_add] }, { intros n a ih, simp only [eval₂_mul, eval₂_C, eval₂_X_pow, g.map_mul, g.map_pow], refl, } end end hom_eval₂ end semiring section comm_semiring section eval variables [comm_semiring R] {p q : polynomial R} {x : R} @[simp] lemma eval_mul : (p * q).eval x = p.eval x * q.eval x := eval₂_mul _ _ instance eval.is_semiring_hom : is_semiring_hom (eval x) := eval₂.is_semiring_hom _ _ @[simp] lemma eval_pow (n : ℕ) : (p ^ n).eval x = p.eval x ^ n := eval₂_pow _ _ _ lemma eval₂_hom [comm_semiring S] (f : R →+* S) (x : R) : p.eval₂ f (f x) = f (p.eval x) := (ring_hom.comp_id f) ▸ (hom_eval₂ p (ring_hom.id R) f x).symm lemma root_mul_left_of_is_root (p : polynomial R) {q : polynomial R} : is_root q a → is_root (p * q) a := λ H, by rw [is_root, eval_mul, is_root.def.1 H, mul_zero] lemma root_mul_right_of_is_root {p : polynomial R} (q : polynomial R) : is_root p a → is_root (p * q) a := λ H, by rw [is_root, eval_mul, is_root.def.1 H, zero_mul] /-- Polynomial evaluation commutes with finset.prod -/ lemma eval_prod {ι : Type*} (s : finset ι) (p : ι → polynomial R) (x : R) : eval x (∏ j in s, p j) = ∏ j in s, eval x (p j) := begin classical, apply finset.induction_on s, { simp only [finset.prod_empty, eval_one] }, { intros j s hj hpj, have h0 : ∏ i in insert j s, eval x (p i) = (eval x (p j)) * ∏ i in s, eval x (p i), { apply finset.prod_insert hj }, rw [h0, ← hpj, finset.prod_insert hj, eval_mul] }, end end eval section map variables [comm_semiring R] [comm_semiring S] (f : R →+* S) lemma map_multiset_prod (m : multiset (polynomial R)) : m.prod.map f = (m.map $ map f).prod := eq.symm $ multiset.prod_hom _ (monoid_hom.of (map f)) lemma map_prod {ι : Type*} (g : ι → polynomial R) (s : finset ι) : (∏ i in s, g i).map f = ∏ i in s, (g i).map f := eq.symm $ prod_hom _ _ lemma map_sum {ι : Type*} (g : ι → polynomial R) (s : finset ι) : (∑ i in s, g i).map f = ∑ i in s, (g i).map f := eq.symm $ sum_hom _ _ lemma support_map_subset (p : polynomial R) : (map f p).support ⊆ p.support := begin intros x, simp only [mem_support_iff], contrapose!, change p.coeff x = 0 → (map f p).coeff x = 0, rw coeff_map, intro hx, rw hx, exact ring_hom.map_zero f, end lemma map_comp (p q : polynomial R) : map f (p.comp q) = (map f p).comp (map f q) := polynomial.induction_on p (by simp) (by simp {contextual := tt}) (by simp [pow_succ', ← mul_assoc, polynomial.comp] {contextual := tt}) end map end comm_semiring section ring variables [ring R] {p q : polynomial R} -- @[simp] -- lemma C_eq_int_cast (n : ℤ) : C ↑n = (n : polynomial R) := -- (C : R →+* _).map_int_cast n lemma C_neg : C (-a) = -C a := ring_hom.map_neg C a lemma C_sub : C (a - b) = C a - C b := ring_hom.map_sub C a b instance map.is_ring_hom {S} [ring S] (f : R →+* S) : is_ring_hom (map f) := by apply is_ring_hom.of_semiring @[simp] lemma map_sub {S} [comm_ring S] (f : R →+* S) : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _ @[simp] lemma map_neg {S} [comm_ring S] (f : R →+* S) : (-p).map f = -(p.map f) := is_ring_hom.map_neg _ @[simp] lemma eval_int_cast {n : ℤ} {x : R} : (n : polynomial R).eval x = n := by simp only [←C_eq_int_cast, eval_C] @[simp] lemma eval₂_neg {S} [ring S] (f : R →+* S) {x : S} : (-p).eval₂ f x = -p.eval₂ f x := by rw [eq_neg_iff_add_eq_zero, ←eval₂_add, add_left_neg, eval₂_zero] @[simp] lemma eval₂_sub {S} [ring S] (f : R →+* S) {x : S} : (p - q).eval₂ f x = p.eval₂ f x - q.eval₂ f x := by rw [sub_eq_add_neg, eval₂_add, eval₂_neg, sub_eq_add_neg] @[simp] lemma eval_neg (p : polynomial R) (x : R) : (-p).eval x = -p.eval x := eval₂_neg _ @[simp] lemma eval_sub (p q : polynomial R) (x : R) : (p - q).eval x = p.eval x - q.eval x := eval₂_sub _ lemma root_X_sub_C : is_root (X - C a) b ↔ a = b := by rw [is_root.def, eval_sub, eval_X, eval_C, sub_eq_zero_iff_eq, eq_comm] end ring section comm_ring variables [comm_ring R] {p q : polynomial R} instance eval₂.is_ring_hom {S} [comm_ring S] (f : R →+* S) {x : S} : is_ring_hom (eval₂ f x) := by apply is_ring_hom.of_semiring instance eval.is_ring_hom {x : R} : is_ring_hom (eval x) := eval₂.is_ring_hom _ lemma eval₂_endomorphism_algebra_map {M : Type w} [add_comm_group M] [module R M] (f : M →ₗ[R] M) (v : M) (p : polynomial R) : p.eval₂ (algebra_map R (M →ₗ[R] M)) f v = p.sum (λ n b, b • (f ^ n) v) := begin dunfold polynomial.eval₂ finsupp.sum, exact (finset.sum_hom p.support (λ h : M →ₗ[R] M, h v)).symm end end comm_ring end polynomial
f32f6365bc7ec79af835bdd75f238484cdc7ee2d
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/field/basic.lean
0beeca085d3fb9664c7c08bf6b9444f0694ff9d0
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
13,378
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis, Leonardo de Moura, Johannes Hölzl, Mario Carneiro -/ import algebra.field.defs import algebra.group_with_zero.units.lemmas import algebra.hom.ring import algebra.ring.inj_surj /-! # Lemmas about division (semi)rings and (semi)fields > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ open function order_dual set set_option old_structure_cmd true universe u variables {α β K : Type*} section division_semiring variables [division_semiring α] {a b c : α} lemma add_div (a b c : α) : (a + b) / c = a / c + b / c := by simp_rw [div_eq_mul_inv, add_mul] @[field_simps] lemma div_add_div_same (a b c : α) : a / c + b / c = (a + b) / c := (add_div _ _ _).symm lemma same_add_div (h : b ≠ 0) : (b + a) / b = 1 + a / b := by rw [←div_self h, add_div] lemma div_add_same (h : b ≠ 0) : (a + b) / b = a / b + 1 := by rw [←div_self h, add_div] lemma one_add_div (h : b ≠ 0 ) : 1 + a / b = (b + a) / b := (same_add_div h).symm lemma div_add_one (h : b ≠ 0) : a / b + 1 = (a + b) / b := (div_add_same h).symm lemma one_div_mul_add_mul_one_div_eq_one_div_add_one_div (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (a + b) * (1 / b) = 1 / a + 1 / b := by rw [mul_add, one_div_mul_cancel ha, add_mul, one_mul, mul_assoc, mul_one_div_cancel hb, mul_one, add_comm] lemma add_div_eq_mul_add_div (a b : α) (hc : c ≠ 0) : a + b / c = (a * c + b) / c := (eq_div_iff_mul_eq hc).2 $ by rw [right_distrib, (div_mul_cancel _ hc)] @[field_simps] lemma add_div' (a b c : α) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by rw [add_div, mul_div_cancel _ hc] @[field_simps] lemma div_add' (a b c : α) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] end division_semiring section division_monoid variables [division_monoid K] [has_distrib_neg K] {a b : K} lemma one_div_neg_one_eq_neg_one : (1:K) / (-1) = -1 := have (-1) * (-1) = (1:K), by rw [neg_mul_neg, one_mul], eq.symm (eq_one_div_of_mul_eq_one_right this) lemma one_div_neg_eq_neg_one_div (a : K) : 1 / (- a) = - (1 / a) := calc 1 / (- a) = 1 / ((-1) * a) : by rw neg_eq_neg_one_mul ... = (1 / a) * (1 / (- 1)) : by rw one_div_mul_one_div_rev ... = (1 / a) * (-1) : by rw one_div_neg_one_eq_neg_one ... = - (1 / a) : by rw [mul_neg, mul_one] lemma div_neg_eq_neg_div (a b : K) : b / (- a) = - (b / a) := calc b / (- a) = b * (1 / (- a)) : by rw [← inv_eq_one_div, division_def] ... = b * -(1 / a) : by rw one_div_neg_eq_neg_one_div ... = -(b * (1 / a)) : by rw neg_mul_eq_mul_neg ... = - (b / a) : by rw mul_one_div lemma neg_div (a b : K) : (-b) / a = - (b / a) := by rw [neg_eq_neg_one_mul, mul_div_assoc, ← neg_eq_neg_one_mul] @[field_simps] lemma neg_div' (a b : K) : - (b / a) = (-b) / a := by simp [neg_div] lemma neg_div_neg_eq (a b : K) : (-a) / (-b) = a / b := by rw [div_neg_eq_neg_div, neg_div, neg_neg] lemma neg_inv : - a⁻¹ = (- a)⁻¹ := by rw [inv_eq_one_div, inv_eq_one_div, div_neg_eq_neg_div] lemma div_neg (a : K) : a / -b = -(a / b) := by rw [← div_neg_eq_neg_div] lemma inv_neg : (-a)⁻¹ = -(a⁻¹) := by rw neg_inv end division_monoid section division_ring variables [division_ring K] {a b : K} @[simp] lemma div_neg_self {a : K} (h : a ≠ 0) : a / -a = -1 := by rw [div_neg_eq_neg_div, div_self h] @[simp] lemma neg_div_self {a : K} (h : a ≠ 0) : (-a) / a = -1 := by rw [neg_div, div_self h] lemma div_sub_div_same (a b c : K) : (a / c) - (b / c) = (a - b) / c := by rw [sub_eq_add_neg, ← neg_div, div_add_div_same, sub_eq_add_neg] lemma same_sub_div {a b : K} (h : b ≠ 0) : (b - a) / b = 1 - a / b := by simpa only [← @div_self _ _ b h] using (div_sub_div_same b a b).symm lemma one_sub_div {a b : K} (h : b ≠ 0) : 1 - a / b = (b - a) / b := (same_sub_div h).symm lemma div_sub_same {a b : K} (h : b ≠ 0) : (a - b) / b = a / b - 1 := by simpa only [← @div_self _ _ b h] using (div_sub_div_same a b b).symm lemma div_sub_one {a b : K} (h : b ≠ 0) : a / b - 1 = (a - b) / b := (div_sub_same h).symm lemma sub_div (a b c : K) : (a - b) / c = a / c - b / c := (div_sub_div_same _ _ _).symm /-- See `inv_sub_inv` for the more convenient version when `K` is commutative. -/ lemma inv_sub_inv' {a b : K} (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ - b⁻¹ = a⁻¹ * (b - a) * b⁻¹ := by rw [mul_sub, sub_mul, mul_inv_cancel_right₀ hb, inv_mul_cancel ha, one_mul] lemma one_div_mul_sub_mul_one_div_eq_one_div_add_one_div (ha : a ≠ 0) (hb : b ≠ 0) : (1 / a) * (b - a) * (1 / b) = 1 / a - 1 / b := by rw [(mul_sub_left_distrib (1 / a)), (one_div_mul_cancel ha), mul_sub_right_distrib, one_mul, mul_assoc, (mul_one_div_cancel hb), mul_one] @[priority 100] -- see Note [lower instance priority] instance division_ring.is_domain : is_domain K := no_zero_divisors.to_is_domain _ end division_ring section semifield variables [semifield α] {a b c d : α} lemma div_add_div (a : α) (c : α) (hb : b ≠ 0) (hd : d ≠ 0) : (a / b) + (c / d) = ((a * d) + (b * c)) / (b * d) := by rw [← mul_div_mul_right _ b hd, ← mul_div_mul_left c d hb, div_add_div_same] lemma one_div_add_one_div (ha : a ≠ 0) (hb : b ≠ 0) : 1 / a + 1 / b = (a + b) / (a * b) := by rw [div_add_div _ _ ha hb, one_mul, mul_one, add_comm] lemma inv_add_inv (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ + b⁻¹ = (a + b) / (a * b) := by rw [inv_eq_one_div, inv_eq_one_div, one_div_add_one_div ha hb] end semifield section field variable [field K] local attribute [simp] mul_assoc mul_comm mul_left_comm @[field_simps] lemma div_sub_div (a : K) {b : K} (c : K) {d : K} (hb : b ≠ 0) (hd : d ≠ 0) : (a / b) - (c / d) = ((a * d) - (b * c)) / (b * d) := begin simp [sub_eq_add_neg], rw [neg_eq_neg_one_mul, ← mul_div_assoc, div_add_div _ _ hb hd, ← mul_assoc, mul_comm b, mul_assoc, ← neg_eq_neg_one_mul] end lemma inv_sub_inv {a b : K} (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ - b⁻¹ = (b - a) / (a * b) := by rw [inv_eq_one_div, inv_eq_one_div, div_sub_div _ _ ha hb, one_mul, mul_one] @[field_simps] lemma sub_div' (a b c : K) (hc : c ≠ 0) : b - a / c = (b * c - a) / c := by simpa using div_sub_div b a one_ne_zero hc @[field_simps] lemma div_sub' (a b c : K) (hc : c ≠ 0) : a / c - b = (a - c * b) / c := by simpa using div_sub_div a b hc one_ne_zero @[priority 100] -- see Note [lower instance priority] instance field.is_domain : is_domain K := { ..division_ring.is_domain } end field namespace ring_hom protected lemma injective [division_ring α] [semiring β] [nontrivial β] (f : α →+* β) : injective f := (injective_iff_map_eq_zero f).2 $ λ x, (map_eq_zero f).1 end ring_hom section noncomputable_defs variables {R : Type*} [nontrivial R] /-- Constructs a `division_ring` structure on a `ring` consisting only of units and 0. -/ noncomputable def division_ring_of_is_unit_or_eq_zero [hR : ring R] (h : ∀ (a : R), is_unit a ∨ a = 0) : division_ring R := { .. (group_with_zero_of_is_unit_or_eq_zero h), .. hR } /-- Constructs a `field` structure on a `comm_ring` consisting only of units and 0. See note [reducible non-instances]. -/ @[reducible] noncomputable def field_of_is_unit_or_eq_zero [hR : comm_ring R] (h : ∀ (a : R), is_unit a ∨ a = 0) : field R := { .. (group_with_zero_of_is_unit_or_eq_zero h), .. hR } end noncomputable_defs /-- Pullback a `division_semiring` along an injective function. -/ @[reducible] -- See note [reducible non-instances] protected def function.injective.division_semiring [division_semiring β] [has_zero α] [has_mul α] [has_add α] [has_one α] [has_inv α] [has_div α] [has_smul ℕ α] [has_pow α ℕ] [has_pow α ℤ] [has_nat_cast α] (f : α → β) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) : division_semiring α := { .. hf.group_with_zero f zero one mul inv div npow zpow, .. hf.semiring f zero one add mul nsmul npow nat_cast } /-- Pullback a `division_ring` along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.division_ring [division_ring K] {K'} [has_zero K'] [has_one K'] [has_add K'] [has_mul K'] [has_neg K'] [has_sub K'] [has_inv K'] [has_div K'] [has_smul ℕ K'] [has_smul ℤ K'] [has_smul ℚ K'] [has_pow K' ℕ] [has_pow K' ℤ] [has_nat_cast K'] [has_int_cast K'] [has_rat_cast K'] (f : K' → K) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) (qsmul : ∀ x (n : ℚ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) (rat_cast : ∀ n : ℚ, f n = n) : division_ring K' := { rat_cast := coe, rat_cast_mk := λ a b h1 h2, hf (by erw [rat_cast, mul, inv, int_cast, nat_cast]; exact division_ring.rat_cast_mk a b h1 h2), qsmul := (•), qsmul_eq_mul' := λ a x, hf (by erw [qsmul, mul, rat.smul_def, rat_cast]), .. hf.group_with_zero f zero one mul inv div npow zpow, .. hf.ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast } /-- Pullback a `field` along an injective function. -/ @[reducible] -- See note [reducible non-instances] protected def function.injective.semifield [semifield β] [has_zero α] [has_mul α] [has_add α] [has_one α] [has_inv α] [has_div α] [has_smul ℕ α] [has_pow α ℕ] [has_pow α ℤ] [has_nat_cast α] (f : α → β) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) : semifield α := { .. hf.comm_group_with_zero f zero one mul inv div npow zpow, .. hf.comm_semiring f zero one add mul nsmul npow nat_cast } /-- Pullback a `field` along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.field [field K] {K'} [has_zero K'] [has_mul K'] [has_add K'] [has_neg K'] [has_sub K'] [has_one K'] [has_inv K'] [has_div K'] [has_smul ℕ K'] [has_smul ℤ K'] [has_smul ℚ K'] [has_pow K' ℕ] [has_pow K' ℤ] [has_nat_cast K'] [has_int_cast K'] [has_rat_cast K'] (f : K' → K) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) (qsmul : ∀ x (n : ℚ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) (rat_cast : ∀ n : ℚ, f n = n) : field K' := { rat_cast := coe, rat_cast_mk := λ a b h1 h2, hf (by erw [rat_cast, mul, inv, int_cast, nat_cast]; exact division_ring.rat_cast_mk a b h1 h2), qsmul := (•), qsmul_eq_mul' := λ a x, hf (by erw [qsmul, mul, rat.smul_def, rat_cast]), .. hf.comm_group_with_zero f zero one mul inv div npow zpow, .. hf.comm_ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast } /-! ### Order dual -/ instance [h : has_rat_cast α] : has_rat_cast αᵒᵈ := h instance [h : division_semiring α] : division_semiring αᵒᵈ := h instance [h : division_ring α] : division_ring αᵒᵈ := h instance [h : semifield α] : semifield αᵒᵈ := h instance [h : field α] : field αᵒᵈ := h @[simp] lemma to_dual_rat_cast [has_rat_cast α] (n : ℚ) : to_dual (n : α) = n := rfl @[simp] lemma of_dual_rat_cast [has_rat_cast α] (n : ℚ) : (of_dual n : α) = n := rfl /-! ### Lexicographic order -/ instance [h : has_rat_cast α] : has_rat_cast (lex α) := h instance [h : division_semiring α] : division_semiring (lex α) := h instance [h : division_ring α] : division_ring (lex α) := h instance [h : semifield α] : semifield (lex α) := h instance [h : field α] : field (lex α) := h @[simp] lemma to_lex_rat_cast [has_rat_cast α] (n : ℚ) : to_lex (n : α) = n := rfl @[simp] lemma of_lex_rat_cast [has_rat_cast α] (n : ℚ) : (of_lex n : α) = n := rfl
53e696de53944597788bc636cdf2ed262454b4f8
618003631150032a5676f229d13a079ac875ff77
/src/algebra/default.lean
ca5733cb22ae5c19cc6c3e2cdaa07392b9a20c80
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
43
lean
import algebra.group import algebra.module
cc6417da0b726df338c58480d994725c87a841f6
367134ba5a65885e863bdc4507601606690974c1
/src/data/nat/parity.lean
bce772492a353f5fe042ab94a457a093e749349f
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
8,217
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Benjamin Davidson The `even` and `odd` predicates on the natural numbers. -/ import data.nat.modeq namespace nat variables {m n : ℕ} @[simp] theorem mod_two_ne_one : ¬ n % 2 = 1 ↔ n % 2 = 0 := by cases mod_two_eq_zero_or_one n with h h; simp [h] @[simp] theorem mod_two_ne_zero : ¬ n % 2 = 0 ↔ n % 2 = 1 := by cases mod_two_eq_zero_or_one n with h h; simp [h] theorem even_iff : even n ↔ n % 2 = 0 := ⟨λ ⟨m, hm⟩, by simp [hm], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [h])⟩⟩ theorem odd_iff : odd n ↔ n % 2 = 1 := ⟨λ ⟨m, hm⟩, by norm_num [hm, add_mod], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by rw [h, add_comm])⟩⟩ lemma not_even_iff : ¬ even n ↔ n % 2 = 1 := by rw [even_iff, mod_two_ne_zero] lemma not_odd_iff : ¬ odd n ↔ n % 2 = 0 := by rw [odd_iff, mod_two_ne_one] lemma even_iff_not_odd : even n ↔ ¬ odd n := by rw [not_odd_iff, even_iff] @[simp] lemma odd_iff_not_even : odd n ↔ ¬ even n := by rw [not_even_iff, odd_iff] lemma is_compl_even_odd : is_compl {n : ℕ | even n} {n | odd n} := by simp [← set.compl_set_of, is_compl_compl] lemma even_or_odd (n : ℕ) : even n ∨ odd n := or.imp_right (odd_iff_not_even.2) (em (even n)) lemma even_or_odd' (n : ℕ) : ∃ k, n = 2 * k ∨ n = 2 * k + 1 := by simpa only [exists_or_distrib, ← odd, ← even] using even_or_odd n lemma even_xor_odd (n : ℕ) : xor (even n) (odd n) := begin cases (even_or_odd n) with h, { exact or.inl ⟨h, (even_iff_not_odd.mp h)⟩ }, { exact or.inr ⟨h, (odd_iff_not_even.mp h)⟩ }, end lemma even_xor_odd' (n : ℕ) : ∃ k, xor (n = 2 * k) (n = 2 * k + 1) := begin rcases (even_or_odd n) with ⟨k, h⟩ | ⟨k, h⟩; use k, { simpa only [xor, h, true_and, eq_self_iff_true, not_true, or_false, and_false] using (succ_ne_self (2*k)).symm }, { simp only [xor, h, add_right_eq_self, false_or, eq_self_iff_true, not_true, not_false_iff, one_ne_zero, and_self] }, end lemma odd_gt_zero (h : odd n) : 0 < n := by { obtain ⟨k, hk⟩ := h, rw hk, exact succ_pos', } instance : decidable_pred (even : ℕ → Prop) := λ n, decidable_of_decidable_of_iff (by apply_instance) even_iff.symm instance decidable_pred_odd : decidable_pred (odd : ℕ → Prop) := λ n, decidable_of_decidable_of_iff (by apply_instance) odd_iff_not_even.symm mk_simp_attribute parity_simps "Simp attribute for lemmas about `even`" @[simp] theorem even_zero : even 0 := ⟨0, dec_trivial⟩ @[simp] theorem not_even_one : ¬ even 1 := by rw even_iff; apply one_ne_zero @[simp] theorem even_bit0 (n : ℕ) : even (bit0 n) := ⟨n, by rw [bit0, two_mul]⟩ @[parity_simps] theorem even_add : even (m + n) ↔ (even m ↔ even n) := begin cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂], { exact @modeq.modeq_add _ _ 0 _ 0 h₁ h₂ }, { exact @modeq.modeq_add _ _ 0 _ 1 h₁ h₂ }, { exact @modeq.modeq_add _ _ 1 _ 0 h₁ h₂ }, exact @modeq.modeq_add _ _ 1 _ 1 h₁ h₂ end theorem even.add_even (hm : even m) (hn : even n) : even (m + n) := even_add.2 $ by simp only [*] theorem even_add' : even (m + n) ↔ (odd m ↔ odd n) := by rw [even_add, even_iff_not_odd, even_iff_not_odd, not_iff_not] theorem odd.add_odd (hm : odd m) (hn : odd n) : even (m + n) := even_add'.2 $ by simp only [*] @[simp] theorem not_even_bit1 (n : ℕ) : ¬ even (bit1 n) := by simp [bit1] with parity_simps lemma two_not_dvd_two_mul_add_one (n : ℕ) : ¬(2 ∣ 2 * n + 1) := by convert not_even_bit1 n; exact two_mul n lemma two_not_dvd_two_mul_sub_one : Π {n} (w : 0 < n), ¬(2 ∣ 2 * n - 1) | (n + 1) _ := two_not_dvd_two_mul_add_one n @[parity_simps] theorem even_sub (h : n ≤ m) : even (m - n) ↔ (even m ↔ even n) := begin conv { to_rhs, rw [←nat.sub_add_cancel h, even_add] }, by_cases h : even n; simp [h] end theorem even.sub_even (hm : even m) (hn : even n) : even (m - n) := (le_total n m).elim (λ h, by simp only [even_sub h, *]) (λ h, by simp only [sub_eq_zero_of_le h, even_zero]) theorem even_sub' (h : n ≤ m) : even (m - n) ↔ (odd m ↔ odd n) := by rw [even_sub h, even_iff_not_odd, even_iff_not_odd, not_iff_not] theorem odd.sub_odd (hm : odd m) (hn : odd n) : even (m - n) := (le_total n m).elim (λ h, by simp only [even_sub' h, *]) (λ h, by simp only [sub_eq_zero_of_le h, even_zero]) @[parity_simps] theorem even_succ : even (succ n) ↔ ¬ even n := by rw [succ_eq_add_one, even_add]; simp [not_even_one] @[parity_simps] theorem even_mul : even (m * n) ↔ even m ∨ even n := begin cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂], { exact @modeq.modeq_mul _ _ 0 _ 0 h₁ h₂ }, { exact @modeq.modeq_mul _ _ 0 _ 1 h₁ h₂ }, { exact @modeq.modeq_mul _ _ 1 _ 0 h₁ h₂ }, exact @modeq.modeq_mul _ _ 1 _ 1 h₁ h₂ end theorem odd_mul : odd (m * n) ↔ odd m ∧ odd n := by simp [not_or_distrib] with parity_simps theorem even.mul_left (hm : even m) (n) : even (m * n) := even_mul.mpr $ or.inl hm theorem even.mul_right (m) (hn : even n) : even (m * n) := even_mul.mpr $ or.inr hn theorem odd.mul (hm : odd m) (hn : odd n) : odd (m * n) := odd_mul.mpr ⟨hm, hn⟩ theorem odd.of_mul_left (h : odd (m * n)) : odd m := (odd_mul.mp h).1 theorem odd.of_mul_right (h : odd (m * n)) : odd n := (odd_mul.mp h).2 /-- If `m` and `n` are natural numbers, then the natural number `m^n` is even if and only if `m` is even and `n` is positive. -/ @[parity_simps] theorem even_pow : even (m^n) ↔ even m ∧ n ≠ 0 := by { induction n with n ih; simp [*, pow_succ', even_mul], tauto } theorem even_div : even (m / n) ↔ m % (2 * n) / n = 0 := by rw [even_iff_two_dvd, dvd_iff_mod_eq_zero, nat.div_mod_eq_mod_mul_div, mul_comm] @[parity_simps] theorem odd_add : odd (m + n) ↔ (odd m ↔ even n) := begin by_contra hnot, rw [not_iff, ← even_iff_not_odd, even_add, odd_iff_not_even, ← not_iff] at hnot, exact (iff_not_self _).mp hnot, end theorem odd.add_even (hm : odd m) (hn : even n) : odd (m + n) := odd_add.2 $ by simp only [*] theorem odd_add' : odd (m + n) ↔ (odd n ↔ even m) := by rw [add_comm, odd_add] theorem even.add_odd (hm : even m) (hn : odd n) : odd (m + n) := odd_add'.2 $ by simp only [*] @[parity_simps] theorem odd_sub (h : n ≤ m) : odd (m - n) ↔ (odd m ↔ even n) := begin by_contra hnot, rw [not_iff, ← even_iff_not_odd, even_sub h, odd_iff_not_even, ← not_iff] at hnot, exact (iff_not_self _).mp hnot, end theorem odd.sub_even (h : n ≤ m) (hm : odd m) (hn : even n) : odd (m - n) := (odd_sub h).mpr (iff_of_true hm hn) theorem odd_sub' (h : n ≤ m) : odd (m - n) ↔ (odd n ↔ even m) := begin by_contra hnot, rw [not_iff, ← even_iff_not_odd, even_sub h, odd_iff_not_even, ← not_iff, @iff.comm _ (even n)] at hnot, exact (iff_not_self _).mp hnot, end theorem even.sub_odd (h : n ≤ m) (hm : even m) (hn : odd n) : odd (m - n) := (odd_sub' h).mpr (iff_of_true hn hm) lemma even_mul_succ_self (n : ℕ) : even (n * (n + 1)) := begin rw even_mul, convert n.even_or_odd, simp with parity_simps end variables {R : Type*} [ring R] theorem neg_one_pow_eq_one_iff_even (h1 : (-1 : R) ≠ 1) : (-1 : R) ^ n = 1 ↔ even n := ⟨λ h, n.mod_two_eq_zero_or_one.elim (dvd_iff_mod_eq_zero _ _).2 (λ hn, by rw [neg_one_pow_eq_pow_mod_two, hn, pow_one] at h; exact (h1 h).elim), λ ⟨m, hm⟩, by rw [neg_one_pow_eq_pow_mod_two, hm]; simp⟩ @[simp] theorem neg_one_pow_two : (-1 : R) ^ 2 = 1 := by simp theorem neg_one_pow_of_even : even n → (-1 : R) ^ n = 1 := by { rintro ⟨c, rfl⟩, simp [pow_mul] } theorem neg_one_pow_of_odd : odd n → (-1 : R) ^ n = -1 := by { rintro ⟨c, rfl⟩, simp [pow_add, pow_mul] } -- Here are examples of how `parity_simps` can be used with `nat`. example (m n : ℕ) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) := by simp [*, (dec_trivial : ¬ 2 = 0)] with parity_simps example : ¬ even 25394535 := by simp end nat
1e855d971e5cced71d99962b7563f960169298a2
94e33a31faa76775069b071adea97e86e218a8ee
/src/measure_theory/measure/haar_lebesgue.lean
e6cf45bc7797c6f0962b0f6e5577aeb87de062f6
[ "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
40,061
lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Sébastien Gouëzel -/ import measure_theory.measure.lebesgue import measure_theory.measure.haar import linear_algebra.finite_dimensional import analysis.normed_space.pointwise import measure_theory.group.pointwise /-! # Relationship between the Haar and Lebesgue measures We prove that the Haar measure and Lebesgue measure are equal on `ℝ` and on `ℝ^ι`, in `measure_theory.add_haar_measure_eq_volume` and `measure_theory.add_haar_measure_eq_volume_pi`. We deduce basic properties of any Haar measure on a finite dimensional real vector space: * `map_linear_map_add_haar_eq_smul_add_haar`: a linear map rescales the Haar measure by the absolute value of its determinant. * `add_haar_preimage_linear_map` : when `f` is a linear map with nonzero determinant, the measure of `f ⁻¹' s` is the measure of `s` multiplied by the absolute value of the inverse of the determinant of `f`. * `add_haar_image_linear_map` : when `f` is a linear map, the measure of `f '' s` is the measure of `s` multiplied by the absolute value of the determinant of `f`. * `add_haar_submodule` : a strict submodule has measure `0`. * `add_haar_smul` : the measure of `r • s` is `|r| ^ dim * μ s`. * `add_haar_ball`: the measure of `ball x r` is `r ^ dim * μ (ball 0 1)`. * `add_haar_closed_ball`: the measure of `closed_ball x r` is `r ^ dim * μ (ball 0 1)`. * `add_haar_sphere`: spheres have zero measure. We also show that a Lebesgue density point `x` of a set `s` (with respect to closed balls) has density one for the rescaled copies `{x} + r • t` of a given set `t` with positive measure, in `tendsto_add_haar_inter_smul_one_of_density_one`. In particular, `s` intersects `{x} + r • t` for small `r`, see `eventually_nonempty_inter_smul_of_density_one`. -/ open topological_space set filter metric open_locale ennreal pointwise topological_space /-- The interval `[0,1]` as a compact set with non-empty interior. -/ def topological_space.positive_compacts.Icc01 : positive_compacts ℝ := { carrier := Icc 0 1, compact' := is_compact_Icc, interior_nonempty' := by simp_rw [interior_Icc, nonempty_Ioo, zero_lt_one] } universe u /-- The set `[0,1]^ι` as a compact set with non-empty interior. -/ def topological_space.positive_compacts.pi_Icc01 (ι : Type*) [fintype ι] : positive_compacts (ι → ℝ) := { carrier := pi univ (λ i, Icc 0 1), compact' := is_compact_univ_pi (λ i, is_compact_Icc), interior_nonempty' := by simp only [interior_pi_set, set.to_finite, interior_Icc, univ_pi_nonempty_iff, nonempty_Ioo, implies_true_iff, zero_lt_one] } namespace measure_theory open measure topological_space.positive_compacts finite_dimensional /-! ### The Lebesgue measure is a Haar measure on `ℝ` and on `ℝ^ι`. -/ /-- The Haar measure equals the Lebesgue measure on `ℝ`. -/ lemma add_haar_measure_eq_volume : add_haar_measure Icc01 = volume := by { convert (add_haar_measure_unique volume Icc01).symm, simp [Icc01] } instance : is_add_haar_measure (volume : measure ℝ) := by { rw ← add_haar_measure_eq_volume, apply_instance } /-- The Haar measure equals the Lebesgue measure on `ℝ^ι`. -/ lemma add_haar_measure_eq_volume_pi (ι : Type*) [fintype ι] : add_haar_measure (pi_Icc01 ι) = volume := begin convert (add_haar_measure_unique volume (pi_Icc01 ι)).symm, simp only [pi_Icc01, volume_pi_pi (λ i, Icc (0 : ℝ) 1), positive_compacts.coe_mk, compacts.coe_mk, finset.prod_const_one, ennreal.of_real_one, real.volume_Icc, one_smul, sub_zero], end instance is_add_haar_measure_volume_pi (ι : Type*) [fintype ι] : is_add_haar_measure (volume : measure (ι → ℝ)) := by { rw ← add_haar_measure_eq_volume_pi, apply_instance } namespace measure /-! ### Strict subspaces have zero measure -/ /-- If a set is disjoint of its translates by infinitely many bounded vectors, then it has measure zero. This auxiliary lemma proves this assuming additionally that the set is bounded. -/ lemma add_haar_eq_zero_of_disjoint_translates_aux {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] {s : set E} (u : ℕ → E) (sb : bounded s) (hu : bounded (range u)) (hs : pairwise (disjoint on (λ n, {u n} + s))) (h's : measurable_set s) : μ s = 0 := begin by_contra h, apply lt_irrefl ∞, calc ∞ = ∑' (n : ℕ), μ s : (ennreal.tsum_const_eq_top_of_ne_zero h).symm ... = ∑' (n : ℕ), μ ({u n} + s) : by { congr' 1, ext1 n, simp only [image_add_left, measure_preimage_add, singleton_add] } ... = μ (⋃ n, {u n} + s) : by rw measure_Union hs (λ n, by simpa only [image_add_left, singleton_add] using measurable_id.const_add _ h's) ... = μ (range u + s) : by rw [← Union_add, Union_singleton_eq_range] ... < ∞ : bounded.measure_lt_top (hu.add sb) end /-- If a set is disjoint of its translates by infinitely many bounded vectors, then it has measure zero. -/ lemma add_haar_eq_zero_of_disjoint_translates {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] {s : set E} (u : ℕ → E) (hu : bounded (range u)) (hs : pairwise (disjoint on (λ n, {u n} + s))) (h's : measurable_set s) : μ s = 0 := begin suffices H : ∀ R, μ (s ∩ closed_ball 0 R) = 0, { apply le_antisymm _ (zero_le _), calc μ s ≤ ∑' (n : ℕ), μ (s ∩ closed_ball 0 n) : by { conv_lhs { rw ← Union_inter_closed_ball_nat s 0 }, exact measure_Union_le _ } ... = 0 : by simp only [H, tsum_zero] }, assume R, apply add_haar_eq_zero_of_disjoint_translates_aux μ u (bounded.mono (inter_subset_right _ _) bounded_closed_ball) hu _ (h's.inter (measurable_set_closed_ball)), apply pairwise_disjoint.mono hs (λ n, _), exact add_subset_add (subset.refl _) (inter_subset_left _ _) end /-- A strict vector subspace has measure zero. -/ lemma add_haar_submodule {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] (s : submodule ℝ E) (hs : s ≠ ⊤) : μ s = 0 := begin obtain ⟨x, hx⟩ : ∃ x, x ∉ s, by simpa only [submodule.eq_top_iff', not_exists, ne.def, not_forall] using hs, obtain ⟨c, cpos, cone⟩ : ∃ (c : ℝ), 0 < c ∧ c < 1 := ⟨1/2, by norm_num, by norm_num⟩, have A : bounded (range (λ (n : ℕ), (c ^ n) • x)), { have : tendsto (λ (n : ℕ), (c ^ n) • x) at_top (𝓝 ((0 : ℝ) • x)) := (tendsto_pow_at_top_nhds_0_of_lt_1 cpos.le cone).smul_const x, exact bounded_range_of_tendsto _ this }, apply add_haar_eq_zero_of_disjoint_translates μ _ A _ (submodule.closed_of_finite_dimensional s).measurable_set, assume m n hmn, simp only [function.on_fun, image_add_left, singleton_add, disjoint_left, mem_preimage, set_like.mem_coe], assume y hym hyn, have A : (c ^ n - c ^ m) • x ∈ s, { convert s.sub_mem hym hyn, simp only [sub_smul, neg_sub_neg, add_sub_add_right_eq_sub] }, have H : c ^ n - c ^ m ≠ 0, by simpa only [sub_eq_zero, ne.def] using (strict_anti_pow cpos cone).injective.ne hmn.symm, have : x ∈ s, { convert s.smul_mem (c ^ n - c ^ m)⁻¹ A, rw [smul_smul, inv_mul_cancel H, one_smul] }, exact hx this end /-- A strict affine subspace has measure zero. -/ lemma add_haar_affine_subspace {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] (s : affine_subspace ℝ E) (hs : s ≠ ⊤) : μ s = 0 := begin rcases s.eq_bot_or_nonempty with rfl|hne, { rw [affine_subspace.bot_coe, measure_empty] }, rw [ne.def, ← affine_subspace.direction_eq_top_iff_of_nonempty hne] at hs, rcases hne with ⟨x, hx : x ∈ s⟩, simpa only [affine_subspace.coe_direction_eq_vsub_set_right hx, vsub_eq_sub, sub_eq_add_neg, image_add_right, neg_neg, measure_preimage_add_right] using add_haar_submodule μ s.direction hs end /-! ### Applying a linear map rescales Haar measure by the determinant We first prove this on `ι → ℝ`, using that this is already known for the product Lebesgue measure (thanks to matrices computations). Then, we extend this to any finite-dimensional real vector space by using a linear equiv with a space of the form `ι → ℝ`, and arguing that such a linear equiv maps Haar measure to Haar measure. -/ lemma map_linear_map_add_haar_pi_eq_smul_add_haar {ι : Type*} [fintype ι] {f : (ι → ℝ) →ₗ[ℝ] (ι → ℝ)} (hf : f.det ≠ 0) (μ : measure (ι → ℝ)) [is_add_haar_measure μ] : measure.map f μ = ennreal.of_real (abs (f.det)⁻¹) • μ := begin /- We have already proved the result for the Lebesgue product measure, using matrices. We deduce it for any Haar measure by uniqueness (up to scalar multiplication). -/ have := add_haar_measure_unique μ (pi_Icc01 ι), rw [this, add_haar_measure_eq_volume_pi, measure.map_smul, real.map_linear_map_volume_pi_eq_smul_volume_pi hf, smul_comm], end lemma map_linear_map_add_haar_eq_smul_add_haar {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] {f : E →ₗ[ℝ] E} (hf : f.det ≠ 0) : measure.map f μ = ennreal.of_real (abs (f.det)⁻¹) • μ := begin -- we reduce to the case of `E = ι → ℝ`, for which we have already proved the result using -- matrices in `map_linear_map_add_haar_pi_eq_smul_add_haar`. let ι := fin (finrank ℝ E), haveI : finite_dimensional ℝ (ι → ℝ) := by apply_instance, have : finrank ℝ E = finrank ℝ (ι → ℝ), by simp, have e : E ≃ₗ[ℝ] ι → ℝ := linear_equiv.of_finrank_eq E (ι → ℝ) this, -- next line is to avoid `g` getting reduced by `simp`. obtain ⟨g, hg⟩ : ∃ g, g = (e : E →ₗ[ℝ] (ι → ℝ)).comp (f.comp (e.symm : (ι → ℝ) →ₗ[ℝ] E)) := ⟨_, rfl⟩, have gdet : g.det = f.det, by { rw [hg], exact linear_map.det_conj f e }, rw ← gdet at hf ⊢, have fg : f = (e.symm : (ι → ℝ) →ₗ[ℝ] E).comp (g.comp (e : E →ₗ[ℝ] (ι → ℝ))), { ext x, simp only [linear_equiv.coe_coe, function.comp_app, linear_map.coe_comp, linear_equiv.symm_apply_apply, hg] }, simp only [fg, linear_equiv.coe_coe, linear_map.coe_comp], have Ce : continuous e := (e : E →ₗ[ℝ] (ι → ℝ)).continuous_of_finite_dimensional, have Cg : continuous g := linear_map.continuous_of_finite_dimensional g, have Cesymm : continuous e.symm := (e.symm : (ι → ℝ) →ₗ[ℝ] E).continuous_of_finite_dimensional, rw [← map_map Cesymm.measurable (Cg.comp Ce).measurable, ← map_map Cg.measurable Ce.measurable], haveI : is_add_haar_measure (map e μ) := is_add_haar_measure_map μ e.to_add_equiv Ce Cesymm, have ecomp : (e.symm) ∘ e = id, by { ext x, simp only [id.def, function.comp_app, linear_equiv.symm_apply_apply] }, rw [map_linear_map_add_haar_pi_eq_smul_add_haar hf (map e μ), measure.map_smul, map_map Cesymm.measurable Ce.measurable, ecomp, measure.map_id] end /-- The preimage of a set `s` under a linear map `f` with nonzero determinant has measure equal to `μ s` times the absolute value of the inverse of the determinant of `f`. -/ @[simp] lemma add_haar_preimage_linear_map {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] {f : E →ₗ[ℝ] E} (hf : f.det ≠ 0) (s : set E) : μ (f ⁻¹' s) = ennreal.of_real (abs (f.det)⁻¹) * μ s := calc μ (f ⁻¹' s) = measure.map f μ s : ((f.equiv_of_det_ne_zero hf).to_continuous_linear_equiv.to_homeomorph .to_measurable_equiv.map_apply s).symm ... = ennreal.of_real (abs (f.det)⁻¹) * μ s : by { rw map_linear_map_add_haar_eq_smul_add_haar μ hf, refl } /-- The preimage of a set `s` under a continuous linear map `f` with nonzero determinant has measure equal to `μ s` times the absolute value of the inverse of the determinant of `f`. -/ @[simp] lemma add_haar_preimage_continuous_linear_map {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] {f : E →L[ℝ] E} (hf : linear_map.det (f : E →ₗ[ℝ] E) ≠ 0) (s : set E) : μ (f ⁻¹' s) = ennreal.of_real (abs (linear_map.det (f : E →ₗ[ℝ] E))⁻¹) * μ s := add_haar_preimage_linear_map μ hf s /-- The preimage of a set `s` under a linear equiv `f` has measure equal to `μ s` times the absolute value of the inverse of the determinant of `f`. -/ @[simp] lemma add_haar_preimage_linear_equiv {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] (f : E ≃ₗ[ℝ] E) (s : set E) : μ (f ⁻¹' s) = ennreal.of_real (abs (f.symm : E →ₗ[ℝ] E).det) * μ s := begin have A : (f : E →ₗ[ℝ] E).det ≠ 0 := (linear_equiv.is_unit_det' f).ne_zero, convert add_haar_preimage_linear_map μ A s, simp only [linear_equiv.det_coe_symm] end /-- The preimage of a set `s` under a continuous linear equiv `f` has measure equal to `μ s` times the absolute value of the inverse of the determinant of `f`. -/ @[simp] lemma add_haar_preimage_continuous_linear_equiv {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] (f : E ≃L[ℝ] E) (s : set E) : μ (f ⁻¹' s) = ennreal.of_real (abs (f.symm : E →ₗ[ℝ] E).det) * μ s := add_haar_preimage_linear_equiv μ _ s /-- The image of a set `s` under a linear map `f` has measure equal to `μ s` times the absolute value of the determinant of `f`. -/ @[simp] lemma add_haar_image_linear_map {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] (f : E →ₗ[ℝ] E) (s : set E) : μ (f '' s) = ennreal.of_real (abs f.det) * μ s := begin rcases ne_or_eq f.det 0 with hf|hf, { let g := (f.equiv_of_det_ne_zero hf).to_continuous_linear_equiv, change μ (g '' s) = _, rw [continuous_linear_equiv.image_eq_preimage g s, add_haar_preimage_continuous_linear_equiv], congr, ext x, simp only [linear_equiv.coe_to_continuous_linear_equiv, linear_equiv.of_is_unit_det_apply, linear_equiv.coe_coe, continuous_linear_equiv.symm_symm], }, { simp only [hf, zero_mul, ennreal.of_real_zero, abs_zero], have : μ f.range = 0 := add_haar_submodule μ _ (linear_map.range_lt_top_of_det_eq_zero hf).ne, exact le_antisymm (le_trans (measure_mono (image_subset_range _ _)) this.le) (zero_le _) } end /-- The image of a set `s` under a continuous linear map `f` has measure equal to `μ s` times the absolute value of the determinant of `f`. -/ @[simp] lemma add_haar_image_continuous_linear_map {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] (f : E →L[ℝ] E) (s : set E) : μ (f '' s) = ennreal.of_real (abs (f : E →ₗ[ℝ] E).det) * μ s := add_haar_image_linear_map μ _ s /-- The image of a set `s` under a continuous linear equiv `f` has measure equal to `μ s` times the absolute value of the determinant of `f`. -/ @[simp] lemma add_haar_image_continuous_linear_equiv {E : Type*} [normed_group E] [normed_space ℝ E] [measurable_space E] [borel_space E] [finite_dimensional ℝ E] (μ : measure E) [is_add_haar_measure μ] (f : E ≃L[ℝ] E) (s : set E) : μ (f '' s) = ennreal.of_real (abs (f : E →ₗ[ℝ] E).det) * μ s := μ.add_haar_image_linear_map (f : E →ₗ[ℝ] E) s /-! ### Basic properties of Haar measures on real vector spaces -/ variables {E : Type*} [normed_group E] [measurable_space E] [normed_space ℝ E] [finite_dimensional ℝ E] [borel_space E] (μ : measure E) [is_add_haar_measure μ] lemma map_add_haar_smul {r : ℝ} (hr : r ≠ 0) : measure.map ((•) r) μ = ennreal.of_real (abs (r ^ (finrank ℝ E))⁻¹) • μ := begin let f : E →ₗ[ℝ] E := r • 1, change measure.map f μ = _, have hf : f.det ≠ 0, { simp only [mul_one, linear_map.det_smul, ne.def, monoid_hom.map_one], assume h, exact hr (pow_eq_zero h) }, simp only [map_linear_map_add_haar_eq_smul_add_haar μ hf, mul_one, linear_map.det_smul, monoid_hom.map_one], end @[simp] lemma add_haar_preimage_smul {r : ℝ} (hr : r ≠ 0) (s : set E) : μ (((•) r) ⁻¹' s) = ennreal.of_real (abs (r ^ (finrank ℝ E))⁻¹) * μ s := calc μ (((•) r) ⁻¹' s) = measure.map ((•) r) μ s : ((homeomorph.smul (is_unit_iff_ne_zero.2 hr).unit).to_measurable_equiv.map_apply s).symm ... = ennreal.of_real (abs (r^(finrank ℝ E))⁻¹) * μ s : by { rw map_add_haar_smul μ hr, refl } /-- Rescaling a set by a factor `r` multiplies its measure by `abs (r ^ dim)`. -/ @[simp] lemma add_haar_smul (r : ℝ) (s : set E) : μ (r • s) = ennreal.of_real (abs (r ^ (finrank ℝ E))) * μ s := begin rcases ne_or_eq r 0 with h|rfl, { rw [← preimage_smul_inv₀ h, add_haar_preimage_smul μ (inv_ne_zero h), inv_pow, inv_inv] }, rcases eq_empty_or_nonempty s with rfl|hs, { simp only [measure_empty, mul_zero, smul_set_empty] }, rw [zero_smul_set hs, ← singleton_zero], by_cases h : finrank ℝ E = 0, { haveI : subsingleton E := finrank_zero_iff.1 h, simp only [h, one_mul, ennreal.of_real_one, abs_one, subsingleton.eq_univ_of_nonempty hs, pow_zero, subsingleton.eq_univ_of_nonempty (singleton_nonempty (0 : E))] }, { haveI : nontrivial E := nontrivial_of_finrank_pos (bot_lt_iff_ne_bot.2 h), simp only [h, zero_mul, ennreal.of_real_zero, abs_zero, ne.def, not_false_iff, zero_pow', measure_singleton] } end @[simp] lemma add_haar_image_homothety (x : E) (r : ℝ) (s : set E) : μ (affine_map.homothety x r '' s) = ennreal.of_real (abs (r ^ (finrank ℝ E))) * μ s := calc μ (affine_map.homothety x r '' s) = μ ((λ y, y + x) '' (r • ((λ y, y + (-x)) '' s))) : by { simp only [← image_smul, image_image, ← sub_eq_add_neg], refl } ... = ennreal.of_real (abs (r ^ (finrank ℝ E))) * μ s : by simp only [image_add_right, measure_preimage_add_right, add_haar_smul] /-! We don't need to state `map_add_haar_neg` here, because it has already been proved for general Haar measures on general commutative groups. -/ /-! ### Measure of balls -/ lemma add_haar_ball_center {E : Type*} [normed_group E] [measurable_space E] [borel_space E] (μ : measure E) [is_add_haar_measure μ] (x : E) (r : ℝ) : μ (ball x r) = μ (ball (0 : E) r) := begin have : ball (0 : E) r = ((+) x) ⁻¹' (ball x r), by simp [preimage_add_ball], rw [this, measure_preimage_add] end lemma add_haar_closed_ball_center {E : Type*} [normed_group E] [measurable_space E] [borel_space E] (μ : measure E) [is_add_haar_measure μ] (x : E) (r : ℝ) : μ (closed_ball x r) = μ (closed_ball (0 : E) r) := begin have : closed_ball (0 : E) r = ((+) x) ⁻¹' (closed_ball x r), by simp [preimage_add_closed_ball], rw [this, measure_preimage_add] end lemma add_haar_ball_mul_of_pos (x : E) {r : ℝ} (hr : 0 < r) (s : ℝ) : μ (ball x (r * s)) = ennreal.of_real (r ^ (finrank ℝ E)) * μ (ball 0 s) := begin have : ball (0 : E) (r * s) = r • ball 0 s, by simp only [smul_ball hr.ne' (0 : E) s, real.norm_eq_abs, abs_of_nonneg hr.le, smul_zero], simp only [this, add_haar_smul, abs_of_nonneg hr.le, add_haar_ball_center, abs_pow], end lemma add_haar_ball_of_pos (x : E) {r : ℝ} (hr : 0 < r) : μ (ball x r) = ennreal.of_real (r ^ (finrank ℝ E)) * μ (ball 0 1) := by rw [← add_haar_ball_mul_of_pos μ x hr, mul_one] lemma add_haar_ball_mul [nontrivial E] (x : E) {r : ℝ} (hr : 0 ≤ r) (s : ℝ) : μ (ball x (r * s)) = ennreal.of_real (r ^ (finrank ℝ E)) * μ (ball 0 s) := begin rcases has_le.le.eq_or_lt hr with h|h, { simp only [← h, zero_pow finrank_pos, measure_empty, zero_mul, ennreal.of_real_zero, ball_zero] }, { exact add_haar_ball_mul_of_pos μ x h s } end lemma add_haar_ball [nontrivial E] (x : E) {r : ℝ} (hr : 0 ≤ r) : μ (ball x r) = ennreal.of_real (r ^ (finrank ℝ E)) * μ (ball 0 1) := by rw [← add_haar_ball_mul μ x hr, mul_one] lemma add_haar_closed_ball_mul_of_pos (x : E) {r : ℝ} (hr : 0 < r) (s : ℝ) : μ (closed_ball x (r * s)) = ennreal.of_real (r ^ (finrank ℝ E)) * μ (closed_ball 0 s) := begin have : closed_ball (0 : E) (r * s) = r • closed_ball 0 s, by simp [smul_closed_ball' hr.ne' (0 : E), abs_of_nonneg hr.le], simp only [this, add_haar_smul, abs_of_nonneg hr.le, add_haar_closed_ball_center, abs_pow], end lemma add_haar_closed_ball_mul (x : E) {r : ℝ} (hr : 0 ≤ r) {s : ℝ} (hs : 0 ≤ s) : μ (closed_ball x (r * s)) = ennreal.of_real (r ^ (finrank ℝ E)) * μ (closed_ball 0 s) := begin have : closed_ball (0 : E) (r * s) = r • closed_ball 0 s, by simp [smul_closed_ball r (0 : E) hs, abs_of_nonneg hr], simp only [this, add_haar_smul, abs_of_nonneg hr, add_haar_closed_ball_center, abs_pow], end /-- The measure of a closed ball can be expressed in terms of the measure of the closed unit ball. Use instead `add_haar_closed_ball`, which uses the measure of the open unit ball as a standard form. -/ lemma add_haar_closed_ball' (x : E) {r : ℝ} (hr : 0 ≤ r) : μ (closed_ball x r) = ennreal.of_real (r ^ (finrank ℝ E)) * μ (closed_ball 0 1) := by rw [← add_haar_closed_ball_mul μ x hr zero_le_one, mul_one] lemma add_haar_closed_unit_ball_eq_add_haar_unit_ball : μ (closed_ball (0 : E) 1) = μ (ball 0 1) := begin apply le_antisymm _ (measure_mono ball_subset_closed_ball), have A : tendsto (λ (r : ℝ), ennreal.of_real (r ^ (finrank ℝ E)) * μ (closed_ball (0 : E) 1)) (𝓝[<] 1) (𝓝 (ennreal.of_real (1 ^ (finrank ℝ E)) * μ (closed_ball (0 : E) 1))), { refine ennreal.tendsto.mul _ (by simp) tendsto_const_nhds (by simp), exact ennreal.tendsto_of_real ((tendsto_id'.2 nhds_within_le_nhds).pow _) }, simp only [one_pow, one_mul, ennreal.of_real_one] at A, refine le_of_tendsto A _, refine mem_nhds_within_Iio_iff_exists_Ioo_subset.2 ⟨(0 : ℝ), by simp, λ r hr, _⟩, dsimp, rw ← add_haar_closed_ball' μ (0 : E) hr.1.le, exact measure_mono (closed_ball_subset_ball hr.2) end lemma add_haar_closed_ball (x : E) {r : ℝ} (hr : 0 ≤ r) : μ (closed_ball x r) = ennreal.of_real (r ^ (finrank ℝ E)) * μ (ball 0 1) := by rw [add_haar_closed_ball' μ x hr, add_haar_closed_unit_ball_eq_add_haar_unit_ball] lemma add_haar_sphere_of_ne_zero (x : E) {r : ℝ} (hr : r ≠ 0) : μ (sphere x r) = 0 := begin rcases hr.lt_or_lt with h|h, { simp only [empty_diff, measure_empty, ← closed_ball_diff_ball, closed_ball_eq_empty.2 h] }, { rw [← closed_ball_diff_ball, measure_diff ball_subset_closed_ball measurable_set_ball measure_ball_lt_top.ne, add_haar_ball_of_pos μ _ h, add_haar_closed_ball μ _ h.le, tsub_self]; apply_instance } end lemma add_haar_sphere [nontrivial E] (x : E) (r : ℝ) : μ (sphere x r) = 0 := begin rcases eq_or_ne r 0 with rfl|h, { rw [sphere_zero, measure_singleton] }, { exact add_haar_sphere_of_ne_zero μ x h } end lemma add_haar_singleton_add_smul_div_singleton_add_smul {r : ℝ} (hr : r ≠ 0) (x y : E) (s t : set E) : μ ({x} + r • s) / μ ({y} + r • t) = μ s / μ t := calc μ ({x} + r • s) / μ ({y} + r • t) = ennreal.of_real (|r| ^ finrank ℝ E) * μ s * (ennreal.of_real (|r| ^ finrank ℝ E) * μ t)⁻¹ : by simp only [div_eq_mul_inv, add_haar_smul, image_add_left, measure_preimage_add, abs_pow, singleton_add] ... = ennreal.of_real (|r| ^ finrank ℝ E) * (ennreal.of_real (|r| ^ finrank ℝ E))⁻¹ * (μ s * (μ t)⁻¹) : begin rw ennreal.mul_inv, { ring }, { simp only [pow_pos (abs_pos.mpr hr), ennreal.of_real_eq_zero, not_le, ne.def, true_or] }, { simp only [ennreal.of_real_ne_top, true_or, ne.def, not_false_iff] }, end ... = μ s / μ t : begin rw [ennreal.mul_inv_cancel, one_mul, div_eq_mul_inv], { simp only [pow_pos (abs_pos.mpr hr), ennreal.of_real_eq_zero, not_le, ne.def], }, { simp only [ennreal.of_real_ne_top, ne.def, not_false_iff] } end /-! ### Density points Besicovitch covering theorem ensures that, for any locally finite measure on a finite-dimensional real vector space, almost every point of a set `s` is a density point, i.e., `μ (s ∩ closed_ball x r) / μ (closed_ball x r)` tends to `1` as `r` tends to `0` (see `besicovitch.ae_tendsto_measure_inter_div`). When `μ` is a Haar measure, one can deduce the same property for any rescaling sequence of sets, of the form `{x} + r • t` where `t` is a set with positive finite measure, instead of the sequence of closed balls. We argue first for the dual property, i.e., if `s` has density `0` at `x`, then `μ (s ∩ ({x} + r • t)) / μ ({x} + r • t)` tends to `0`. First when `t` is contained in the ball of radius `1`, in `tendsto_add_haar_inter_smul_zero_of_density_zero_aux1`, (by arguing by inclusion). Then when `t` is bounded, reducing to the previous one by rescaling, in `tendsto_add_haar_inter_smul_zero_of_density_zero_aux2`. Then for a general set `t`, by cutting it into a bounded part and a part with small measure, in `tendsto_add_haar_inter_smul_zero_of_density_zero`. Going to the complement, one obtains the desired property at points of density `1`, first when `s` is measurable in `tendsto_add_haar_inter_smul_one_of_density_one_aux`, and then without this assumption in `tendsto_add_haar_inter_smul_one_of_density_one` by applying the previous lemma to the measurable hull `to_measurable μ s` -/ lemma tendsto_add_haar_inter_smul_zero_of_density_zero_aux1 (s : set E) (x : E) (h : tendsto (λ r, μ (s ∩ closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 0)) (t : set E) (u : set E) (h'u : μ u ≠ 0) (t_bound : t ⊆ closed_ball 0 1) : tendsto (λ (r : ℝ), μ (s ∩ ({x} + r • t)) / μ ({x} + r • u)) (𝓝[>] 0) (𝓝 0) := begin have A : tendsto (λ (r : ℝ), μ (s ∩ ({x} + r • t)) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 0), { apply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds h (eventually_of_forall (λ b, zero_le _)), filter_upwards [self_mem_nhds_within], rintros r (rpos : 0 < r), apply ennreal.mul_le_mul (measure_mono (inter_subset_inter_right _ _)) le_rfl, assume y hy, have : y - x ∈ r • closed_ball (0 : E) 1, { apply smul_set_mono t_bound, simpa [neg_add_eq_sub] using hy }, simpa only [smul_closed_ball _ _ zero_le_one, real.norm_of_nonneg rpos.le, mem_closed_ball_iff_norm, mul_one, sub_zero, smul_zero] }, have B : tendsto (λ (r : ℝ), μ (closed_ball x r) / μ ({x} + r • u)) (𝓝[>] 0) (𝓝 (μ (closed_ball x 1) / μ ({x} + u))), { apply tendsto_const_nhds.congr' _, filter_upwards [self_mem_nhds_within], rintros r (rpos : 0 < r), have : closed_ball x r = {x} + r • closed_ball 0 1, by simp only [smul_closed_ball, real.norm_of_nonneg rpos.le, zero_le_one, add_zero, mul_one, singleton_add_closed_ball, smul_zero], simp only [this, add_haar_singleton_add_smul_div_singleton_add_smul μ rpos.ne'], simp only [add_haar_closed_ball_center, image_add_left, measure_preimage_add, singleton_add] }, have C : tendsto (λ (r : ℝ), (μ (s ∩ ({x} + r • t)) / μ (closed_ball x r)) * (μ (closed_ball x r) / μ ({x} + r • u))) (𝓝[>] 0) (𝓝 (0 * (μ (closed_ball x 1) / μ ({x} + u)))), { apply ennreal.tendsto.mul A _ B (or.inr ennreal.zero_ne_top), simp only [ennreal.div_eq_top, h'u, measure_closed_ball_lt_top.ne, false_or, image_add_left, eq_self_iff_true, not_true, ne.def, not_false_iff, measure_preimage_add, singleton_add, and_false, false_and] }, simp only [zero_mul] at C, apply C.congr' _, filter_upwards [self_mem_nhds_within], rintros r (rpos : 0 < r), calc μ (s ∩ ({x} + r • t)) / μ (closed_ball x r) * (μ (closed_ball x r) / μ ({x} + r • u)) = (μ (closed_ball x r) * (μ (closed_ball x r))⁻¹) * (μ (s ∩ ({x} + r • t)) / μ ({x} + r • u)) : by { simp only [div_eq_mul_inv], ring } ... = μ (s ∩ ({x} + r • t)) / μ ({x} + r • u) : by rw [ennreal.mul_inv_cancel (measure_closed_ball_pos μ x rpos).ne' measure_closed_ball_lt_top.ne, one_mul], end lemma tendsto_add_haar_inter_smul_zero_of_density_zero_aux2 (s : set E) (x : E) (h : tendsto (λ r, μ (s ∩ closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 0)) (t : set E) (u : set E) (h'u : μ u ≠ 0) (R : ℝ) (Rpos : 0 < R) (t_bound : t ⊆ closed_ball 0 R) : tendsto (λ (r : ℝ), μ (s ∩ ({x} + r • t)) / μ ({x} + r • u)) (𝓝[>] 0) (𝓝 0) := begin set t' := R⁻¹ • t with ht', set u' := R⁻¹ • u with hu', have A : tendsto (λ (r : ℝ), μ (s ∩ ({x} + r • t')) / μ ({x} + r • u')) (𝓝[>] 0) (𝓝 0), { apply tendsto_add_haar_inter_smul_zero_of_density_zero_aux1 μ s x h t' u', { simp only [h'u, (pow_pos Rpos _).ne', abs_nonpos_iff, add_haar_smul, not_false_iff, ennreal.of_real_eq_zero, inv_eq_zero, inv_pow, ne.def, or_self, mul_eq_zero] }, { convert smul_set_mono t_bound, rw [smul_closed_ball _ _ Rpos.le, smul_zero, real.norm_of_nonneg (inv_nonneg.2 Rpos.le), inv_mul_cancel Rpos.ne'] } }, have B : tendsto (λ (r : ℝ), R * r) (𝓝[>] 0) (𝓝[>] (R * 0)), { apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within, { exact (tendsto_const_nhds.mul tendsto_id).mono_left nhds_within_le_nhds }, { filter_upwards [self_mem_nhds_within], assume r rpos, rw mul_zero, exact mul_pos Rpos rpos } }, rw mul_zero at B, apply (A.comp B).congr' _, filter_upwards [self_mem_nhds_within], rintros r (rpos : 0 < r), have T : (R * r) • t' = r • t, by rw [mul_comm, ht', smul_smul, mul_assoc, mul_inv_cancel Rpos.ne', mul_one], have U : (R * r) • u' = r • u, by rw [mul_comm, hu', smul_smul, mul_assoc, mul_inv_cancel Rpos.ne', mul_one], dsimp, rw [T, U], end /-- Consider a point `x` at which a set `s` has density zero, with respect to closed balls. Then it also has density zero with respect to any measurable set `t`: the proportion of points in `s` belonging to a rescaled copy `{x} + r • t` of `t` tends to zero as `r` tends to zero. -/ lemma tendsto_add_haar_inter_smul_zero_of_density_zero (s : set E) (x : E) (h : tendsto (λ r, μ (s ∩ closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 0)) (t : set E) (ht : measurable_set t) (h''t : μ t ≠ ∞) : tendsto (λ (r : ℝ), μ (s ∩ ({x} + r • t)) / μ ({x} + r • t)) (𝓝[>] 0) (𝓝 0) := begin refine tendsto_order.2 ⟨λ a' ha', (ennreal.not_lt_zero ha').elim, λ ε (εpos : 0 < ε), _⟩, rcases eq_or_ne (μ t) 0 with h't|h't, { apply eventually_of_forall (λ r, _), suffices H : μ (s ∩ ({x} + r • t)) = 0, by { rw H, simpa only [ennreal.zero_div] using εpos }, apply le_antisymm _ (zero_le _), calc μ (s ∩ ({x} + r • t)) ≤ μ ({x} + r • t) : measure_mono (inter_subset_right _ _) ... = 0 : by simp only [h't, add_haar_smul, image_add_left, measure_preimage_add, singleton_add, mul_zero] }, obtain ⟨n, npos, hn⟩ : ∃ (n : ℕ), 0 < n ∧ μ (t \ closed_ball 0 n) < (ε / 2) * μ t, { have A : tendsto (λ (n : ℕ), μ (t \ closed_ball 0 n)) at_top (𝓝 (μ (⋂ (n : ℕ), t \ closed_ball 0 n))), { have N : ∃ (n : ℕ), μ (t \ closed_ball 0 n) ≠ ∞ := ⟨0, ((measure_mono (diff_subset t _)).trans_lt h''t.lt_top).ne⟩, refine tendsto_measure_Inter (λ n, ht.diff measurable_set_closed_ball) (λ m n hmn, _) N, exact diff_subset_diff subset.rfl (closed_ball_subset_closed_ball (nat.cast_le.2 hmn)) }, have : (⋂ (n : ℕ), t \ closed_ball 0 n) = ∅, by simp_rw [diff_eq, ← inter_Inter, Inter_eq_compl_Union_compl, compl_compl, Union_closed_ball_nat, compl_univ, inter_empty], simp only [this, measure_empty] at A, have I : 0 < (ε / 2) * μ t := ennreal.mul_pos (ennreal.half_pos εpos.ne').ne' h't, exact (eventually.and (Ioi_mem_at_top 0) ((tendsto_order.1 A).2 _ I)).exists }, have L : tendsto (λ (r : ℝ), μ (s ∩ ({x} + r • (t ∩ closed_ball 0 n))) / μ ({x} + r • t)) (𝓝[>] 0) (𝓝 0) := tendsto_add_haar_inter_smul_zero_of_density_zero_aux2 μ s x h _ t h't n (nat.cast_pos.2 npos) (inter_subset_right _ _), filter_upwards [(tendsto_order.1 L).2 _ (ennreal.half_pos εpos.ne'), self_mem_nhds_within], rintros r hr (rpos : 0 < r), have I : μ (s ∩ ({x} + r • t)) ≤ μ (s ∩ ({x} + r • (t ∩ closed_ball 0 n))) + μ ({x} + r • (t \ closed_ball 0 n)) := calc μ (s ∩ ({x} + r • t)) = μ ((s ∩ ({x} + r • (t ∩ closed_ball 0 n))) ∪ (s ∩ ({x} + r • (t \ closed_ball 0 n)))) : by rw [← inter_union_distrib_left, ← add_union, ← smul_set_union, inter_union_diff] ... ≤ μ (s ∩ ({x} + r • (t ∩ closed_ball 0 n))) + μ (s ∩ ({x} + r • (t \ closed_ball 0 n))) : measure_union_le _ _ ... ≤ μ (s ∩ ({x} + r • (t ∩ closed_ball 0 n))) + μ ({x} + r • (t \ closed_ball 0 n)) : add_le_add le_rfl (measure_mono (inter_subset_right _ _)), calc μ (s ∩ ({x} + r • t)) / μ ({x} + r • t) ≤ (μ (s ∩ ({x} + r • (t ∩ closed_ball 0 n))) + μ ({x} + r • (t \ closed_ball 0 n))) / μ ({x} + r • t) : ennreal.mul_le_mul I le_rfl ... < ε / 2 + ε / 2 : begin rw ennreal.add_div, apply ennreal.add_lt_add hr _, rwa [add_haar_singleton_add_smul_div_singleton_add_smul μ rpos.ne', ennreal.div_lt_iff (or.inl h't) (or.inl h''t)], end ... = ε : ennreal.add_halves _ end lemma tendsto_add_haar_inter_smul_one_of_density_one_aux (s : set E) (hs : measurable_set s) (x : E) (h : tendsto (λ r, μ (s ∩ closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 1)) (t : set E) (ht : measurable_set t) (h't : μ t ≠ 0) (h''t : μ t ≠ ∞) : tendsto (λ (r : ℝ), μ (s ∩ ({x} + r • t)) / μ ({x} + r • t)) (𝓝[>] 0) (𝓝 1) := begin have I : ∀ u v, μ u ≠ 0 → μ u ≠ ∞ → measurable_set v → μ u / μ u - μ (vᶜ ∩ u) / μ u = μ (v ∩ u) / μ u, { assume u v uzero utop vmeas, simp_rw [div_eq_mul_inv], rw ← ennreal.sub_mul, swap, { simp only [uzero, ennreal.inv_eq_top, implies_true_iff, ne.def, not_false_iff] }, congr' 1, apply ennreal.sub_eq_of_add_eq (ne_top_of_le_ne_top utop (measure_mono (inter_subset_right _ _))), rw [inter_comm _ u, inter_comm _ u], exact measure_inter_add_diff u vmeas }, have L : tendsto (λ r, μ (sᶜ ∩ closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 0), { have A : tendsto (λ r, μ (closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 1), { apply tendsto_const_nhds.congr' _, filter_upwards [self_mem_nhds_within], assume r hr, rw [div_eq_mul_inv, ennreal.mul_inv_cancel], { exact (measure_closed_ball_pos μ _ hr).ne' }, { exact measure_closed_ball_lt_top.ne } }, have B := ennreal.tendsto.sub A h (or.inl ennreal.one_ne_top), simp only [tsub_self] at B, apply B.congr' _, filter_upwards [self_mem_nhds_within], rintros r (rpos : 0 < r), convert I (closed_ball x r) sᶜ (measure_closed_ball_pos μ _ rpos).ne' (measure_closed_ball_lt_top).ne hs.compl, rw compl_compl }, have L' : tendsto (λ (r : ℝ), μ (sᶜ ∩ ({x} + r • t)) / μ ({x} + r • t)) (𝓝[>] 0) (𝓝 0) := tendsto_add_haar_inter_smul_zero_of_density_zero μ sᶜ x L t ht h''t, have L'' : tendsto (λ (r : ℝ), μ ({x} + r • t) / μ ({x} + r • t)) (𝓝[>] 0) (𝓝 1), { apply tendsto_const_nhds.congr' _, filter_upwards [self_mem_nhds_within], rintros r (rpos : 0 < r), rw [add_haar_singleton_add_smul_div_singleton_add_smul μ rpos.ne', ennreal.div_self h't h''t] }, have := ennreal.tendsto.sub L'' L' (or.inl ennreal.one_ne_top), simp only [tsub_zero] at this, apply this.congr' _, filter_upwards [self_mem_nhds_within], rintros r (rpos : 0 < r), refine I ({x} + r • t) s _ _ hs, { simp only [h't, abs_of_nonneg rpos.le, pow_pos rpos, add_haar_smul, image_add_left, ennreal.of_real_eq_zero, not_le, or_false, ne.def, measure_preimage_add, abs_pow, singleton_add, mul_eq_zero] }, { simp only [h''t, ennreal.of_real_ne_top, add_haar_smul, image_add_left, with_top.mul_eq_top_iff, ne.def, not_false_iff, measure_preimage_add, singleton_add, and_false, false_and, or_self] } end /-- Consider a point `x` at which a set `s` has density one, with respect to closed balls (i.e., a Lebesgue density point of `s`). Then `s` has also density one at `x` with respect to any measurable set `t`: the proportion of points in `s` belonging to a rescaled copy `{x} + r • t` of `t` tends to one as `r` tends to zero. -/ lemma tendsto_add_haar_inter_smul_one_of_density_one (s : set E) (x : E) (h : tendsto (λ r, μ (s ∩ closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 1)) (t : set E) (ht : measurable_set t) (h't : μ t ≠ 0) (h''t : μ t ≠ ∞) : tendsto (λ (r : ℝ), μ (s ∩ ({x} + r • t)) / μ ({x} + r • t)) (𝓝[>] 0) (𝓝 1) := begin have : tendsto (λ (r : ℝ), μ (to_measurable μ s ∩ ({x} + r • t)) / μ ({x} + r • t)) (𝓝[>] 0) (𝓝 1), { apply tendsto_add_haar_inter_smul_one_of_density_one_aux μ _ (measurable_set_to_measurable _ _) _ _ t ht h't h''t, apply tendsto_of_tendsto_of_tendsto_of_le_of_le' h tendsto_const_nhds, { apply eventually_of_forall (λ r, _), apply ennreal.mul_le_mul _ le_rfl, exact measure_mono (inter_subset_inter_left _ (subset_to_measurable _ _)) }, { filter_upwards [self_mem_nhds_within], rintros r (rpos : 0 < r), apply ennreal.div_le_of_le_mul, rw one_mul, exact measure_mono (inter_subset_right _ _) } }, apply this.congr (λ r, _), congr' 1, apply measure_to_measurable_inter_of_sigma_finite, simp only [image_add_left, singleton_add], apply (continuous_add_left (-x)).measurable (ht.const_smul₀ r) end /-- Consider a point `x` at which a set `s` has density one, with respect to closed balls (i.e., a Lebesgue density point of `s`). Then `s` intersects the rescaled copies `{x} + r • t` of a given set `t` with positive measure, for any small enough `r`. -/ lemma eventually_nonempty_inter_smul_of_density_one (s : set E) (x : E) (h : tendsto (λ r, μ (s ∩ closed_ball x r) / μ (closed_ball x r)) (𝓝[>] 0) (𝓝 1)) (t : set E) (ht : measurable_set t) (h't : μ t ≠ 0) : ∀ᶠ r in 𝓝[>] (0 : ℝ), (s ∩ ({x} + r • t)).nonempty := begin obtain ⟨t', t'_meas, t't, t'pos, t'top⟩ : ∃ t', measurable_set t' ∧ t' ⊆ t ∧ 0 < μ t' ∧ μ t' < ⊤ := exists_subset_measure_lt_top ht h't.bot_lt, filter_upwards [(tendsto_order.1 (tendsto_add_haar_inter_smul_one_of_density_one μ s x h t' t'_meas t'pos.ne' t'top.ne)).1 0 ennreal.zero_lt_one], assume r hr, have : μ (s ∩ ({x} + r • t')) ≠ 0 := λ h', by simpa only [ennreal.not_lt_zero, ennreal.zero_div, h'] using hr, have : (s ∩ ({x} + r • t')).nonempty := nonempty_of_measure_ne_zero this, apply this.mono (inter_subset_inter subset.rfl _), exact add_subset_add subset.rfl (smul_set_mono t't), end end measure end measure_theory
9ac7418c73011ce0fc073553eebf289d5c6447e8
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/instance.lean
96ca2cef1e0fa113cd57e49adfba0b8158637445
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
371
lean
variable {α : Type} def isPrefix (xs : list α) (ys : list α) : Prop := ∃ (t : list α), xs ++ t = ys instance list_has_le : has_le (list α) := ⟨isPrefix⟩ lemma list.isPrefixRelf : ∀ (xs : list α), xs ≤ xs := assume xs, ⟨[], by simp⟩ section local attribute [instance] list_has_le lemma foo (xs : list α): xs ≤ xs := ⟨[], by simp⟩ end
740e0b280ce7435c3b831fd8e20c1230900998f2
91b8df3b248df89472cc0b753fbe2bac750aefea
/experiments/lean/src/ddl/binary/default.lean
a51d2d4141b77a3a0c70420559c9caea591ad460
[ "Apache-2.0", "LicenseRef-scancode-unknown-license-reference" ]
permissive
yeslogic/fathom
eabe5c4112d3b4d5ec9096a57bb502254ddbdf15
3960a9466150d392c2cb103c5cb5fcffa0200814
refs/heads/main
1,685,349,769,736
1,675,998,621,000
1,675,998,621,000
28,993,871
214
11
Apache-2.0
1,694,044,276,000
1,420,764,938,000
Rust
UTF-8
Lean
false
false
149
lean
import ddl.binary.basic import ddl.binary.formation import ddl.binary.kinding import ddl.binary.monad import ddl.binary.repr import ddl.binary.subst
2376ebf6ff569b0197764dd0819894298da1d7b5
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/big_operators/nat_antidiagonal.lean
3a9b8428ab5d395ea9bd497634c3d6e90224182f
[ "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,852
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.nat_antidiagonal import algebra.big_operators.basic /-! # Big operators for `nat_antidiagonal` This file contains theorems relevant to big operators over `finset.nat.antidiagonal`. -/ open_locale big_operators variables {M N : Type*} [comm_monoid M] [add_comm_monoid N] namespace finset namespace nat lemma prod_antidiagonal_succ {n : ℕ} {f : ℕ × ℕ → M} : ∏ p in antidiagonal (n + 1), f p = f (0, n + 1) * ∏ p in antidiagonal n, f (p.1 + 1, p.2) := begin rw [antidiagonal_succ, prod_insert, prod_map], refl, intro con, rcases mem_map.1 con with ⟨⟨a,b⟩, ⟨h1, h2⟩⟩, simp only [prod.mk.inj_iff, function.embedding.coe_prod_map, prod.map_mk] at h2, apply nat.succ_ne_zero a h2.1, end lemma sum_antidiagonal_succ {n : ℕ} {f : ℕ × ℕ → N} : ∑ p in antidiagonal (n + 1), f p = f (0, n + 1) + ∑ p in antidiagonal n, f (p.1 + 1, p.2) := @prod_antidiagonal_succ (multiplicative N) _ _ _ @[to_additive] lemma prod_antidiagonal_swap {n : ℕ} {f : ℕ × ℕ → M} : ∏ p in antidiagonal n, f p.swap = ∏ p in antidiagonal n, f p := by { nth_rewrite 1 ← map_swap_antidiagonal, rw [prod_map], refl } lemma prod_antidiagonal_succ' {n : ℕ} {f : ℕ × ℕ → M} : ∏ p in antidiagonal (n + 1), f p = f (n + 1, 0) * ∏ p in antidiagonal n, f (p.1, p.2 + 1) := begin rw [← prod_antidiagonal_swap, prod_antidiagonal_succ, ← prod_antidiagonal_swap], refl end lemma sum_antidiagonal_succ' {n : ℕ} {f : ℕ × ℕ → N} : ∑ p in antidiagonal (n + 1), f p = f (n + 1, 0) + ∑ p in antidiagonal n, f (p.1, p.2 + 1) := @prod_antidiagonal_succ' (multiplicative N) _ _ _ @[to_additive] lemma prod_antidiagonal_subst {n : ℕ} {f : ℕ × ℕ → ℕ → M} : ∏ p in antidiagonal n, f p n = ∏ p in antidiagonal n, f p (p.1 + p.2) := prod_congr rfl $ λ p hp, by rw [nat.mem_antidiagonal.1 hp] @[to_additive] lemma prod_antidiagonal_eq_prod_range_succ_mk {M : Type*} [comm_monoid M] (f : ℕ × ℕ → M) (n : ℕ) : ∏ ij in finset.nat.antidiagonal n, f ij = ∏ k in range n.succ, f (k, n - k) := begin convert prod_map _ ⟨λ i, (i, n - i), λ x y h, (prod.mk.inj h).1⟩ _, refl, end /-- This lemma matches more generally than `finset.nat.prod_antidiagonal_eq_prod_range_succ_mk` when using `rw ←`. -/ @[to_additive "This lemma matches more generally than `finset.nat.sum_antidiagonal_eq_sum_range_succ_mk` when using `rw ←`."] lemma prod_antidiagonal_eq_prod_range_succ {M : Type*} [comm_monoid M] (f : ℕ → ℕ → M) (n : ℕ) : ∏ ij in finset.nat.antidiagonal n, f ij.1 ij.2 = ∏ k in range n.succ, f k (n - k) := prod_antidiagonal_eq_prod_range_succ_mk _ _ end nat end finset
5908e7637ae6b111de1358bbb77e7aac5643f23c
675b8263050a5d74b89ceab381ac81ce70535688
/src/data/real/nnreal.lean
815793f13a9a6a2d135c3a65f37adf0924a4b7d2
[ "Apache-2.0" ]
permissive
vozor/mathlib
5921f55235ff60c05f4a48a90d616ea167068adf
f7e728ad8a6ebf90291df2a4d2f9255a6576b529
refs/heads/master
1,675,607,702,231
1,609,023,279,000
1,609,023,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
29,057
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.linear_ordered_comm_group_with_zero import algebra.big_operators.ring import data.real.basic import data.indicator_function /-! # Nonnegative real numbers In this file we define `nnreal` (notation: `ℝ≥0`) to be the type of non-negative real numbers, a.k.a. the interval `[0, ∞)`. We also define the following operations and structures on `ℝ≥0`: * the order on `ℝ≥0` is the restriction of the order on `ℝ`; these relations define a conditionally complete linear order with a bottom element, `conditionally_complete_linear_order_bot`; * `a + b` and `a * b` are the restrictions of addition and multiplication of real numbers to `ℝ≥0`; these operations together with `0 = ⟨0, _⟩` and `1 = ⟨1, _⟩` turn `ℝ≥0` into a linear ordered archimedean commutative semifield; we have no typeclass for this in `mathlib` yet, so we define the following instances instead: - `linear_ordered_semiring ℝ≥0`; - `comm_semiring ℝ≥0`; - `canonically_ordered_comm_semiring ℝ≥0`; - `linear_ordered_comm_group_with_zero ℝ≥0`; - `archimedean ℝ≥0`. * `nnreal.of_real x` is defined as `⟨max x 0, _⟩`, i.e. `↑(nnreal.of_real x) = x` when `0 ≤ x` and `↑(nnreal.of_real x) = 0` otherwise. We also define an instance `can_lift ℝ ℝ≥0`. This instance can be used by the `lift` tactic to replace `x : ℝ` and `hx : 0 ≤ x` in the proof context with `x : ℝ≥0` while replacing all occurences of `x` with `↑x`. This tactic also works for a function `f : α → ℝ` with a hypothesis `hf : ∀ x, 0 ≤ f x`. ## Notations This file defines `ℝ≥0` as a localized notation for `nnreal`. -/ noncomputable theory open_locale classical big_operators /-- Nonnegative real numbers. -/ def nnreal := {r : ℝ // 0 ≤ r} localized "notation ` ℝ≥0 ` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : ℝ≥0) : n.val = n := rfl instance : can_lift ℝ ℝ≥0 := { coe := coe, cond := λ r, 0 ≤ r, prf := λ x hx, ⟨⟨x, hx⟩, rfl⟩ } protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff /-- Reinterpret a real number `r` as a non-negative real number. Returns `0` if `r < 0`. -/ protected def of_real (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma coe_of_real (r : ℝ) (hr : 0 ≤ r) : (nnreal.of_real r : ℝ) = r := max_eq_left hr lemma le_coe_of_real (r : ℝ) : r ≤ nnreal.of_real r := le_max_left r 0 lemma coe_nonneg (r : ℝ≥0) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl instance : has_zero ℝ≥0 := ⟨⟨0, le_refl 0⟩⟩ instance : has_one ℝ≥0 := ⟨⟨1, zero_le_one⟩⟩ instance : has_add ℝ≥0 := ⟨λa b, ⟨a + b, add_nonneg a.2 b.2⟩⟩ instance : has_sub ℝ≥0 := ⟨λa b, nnreal.of_real (a - b)⟩ instance : has_mul ℝ≥0 := ⟨λa b, ⟨a * b, mul_nonneg a.2 b.2⟩⟩ instance : has_inv ℝ≥0 := ⟨λa, ⟨(a.1)⁻¹, inv_nonneg.2 a.2⟩⟩ instance : has_le ℝ≥0 := ⟨λ r s, (r:ℝ) ≤ s⟩ instance : has_bot ℝ≥0 := ⟨0⟩ instance : inhabited ℝ≥0 := ⟨0⟩ protected lemma injective_coe : function.injective (coe : ℝ≥0 → ℝ) := subtype.coe_injective @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := nnreal.injective_coe.eq_iff @[simp, norm_cast] protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl @[simp, norm_cast] protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl @[simp, norm_cast] protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl @[simp, norm_cast] protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl @[simp, norm_cast] protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] -- TODO: setup semifield! @[simp] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by norm_cast lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast instance : comm_semiring ℝ≥0 := begin refine { zero := 0, add := (+), one := 1, mul := (*), ..}; { intros; apply nnreal.eq; simp [mul_comm, mul_assoc, add_comm_monoid.add, left_distrib, right_distrib, add_comm_monoid.zero, add_comm, add_left_comm] } end /-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/ def to_real_hom : ℝ≥0 →+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl instance : comm_group_with_zero ℝ≥0 := { exists_pair_ne := ⟨0, 1, assume h, zero_ne_one $ nnreal.eq_iff.2 h⟩, inv_zero := nnreal.eq $ show (0⁻¹ : ℝ) = 0, from inv_zero, mul_inv_cancel := assume x h, nnreal.eq $ mul_inv_cancel $ ne_iff.2 h, .. (by apply_instance : has_inv ℝ≥0), .. (_ : comm_semiring ℝ≥0), .. (_ : semiring ℝ≥0) } @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (λ x, f x) a := (to_real_hom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[simp, norm_cast] protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[simp, norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := to_real_hom.map_pow r n @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = ∑ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ @[norm_cast] lemma nsmul_coe (r : ℝ≥0) (n : ℕ) : ↑(n •ℕ r) = n •ℕ (r:ℝ) := to_real_hom.to_add_monoid_hom.map_nsmul _ _ @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := to_real_hom.map_nat_cast n instance : linear_order ℝ≥0 := linear_order.lift (coe : ℝ≥0 → ℝ) nnreal.injective_coe @[simp, norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma of_real_mono : monotone nnreal.of_real := λ x y h, max_le_max h (le_refl 0) @[simp] lemma of_real_coe {r : ℝ≥0} : nnreal.of_real r = r := nnreal.eq $ max_eq_left r.2 /-- `nnreal.of_real` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ protected def gi : galois_insertion nnreal.of_real coe := galois_insertion.monotone_intro nnreal.coe_mono nnreal.of_real_mono le_coe_of_real (λ _, of_real_coe) instance : order_bot ℝ≥0 := { bot := ⊥, bot_le := assume ⟨a, h⟩, h, .. nnreal.linear_order } instance : canonically_ordered_add_monoid ℝ≥0 := { add_le_add_left := assume a b h c, @add_le_add_left ℝ _ a b h c, lt_of_add_lt_add_left := assume a b c, @lt_of_add_lt_add_left ℝ _ a b c, le_iff_exists_add := assume ⟨a, ha⟩ ⟨b, hb⟩, iff.intro (assume h : a ≤ b, ⟨⟨b - a, le_sub_iff_add_le.2 $ by simp [h]⟩, nnreal.eq $ show b = a + (b - a), by rw [add_sub_cancel'_right]⟩) (assume ⟨⟨c, hc⟩, eq⟩, eq.symm ▸ show a ≤ a + c, from (le_add_iff_nonneg_right a).2 hc), ..nnreal.comm_semiring, ..nnreal.order_bot, ..nnreal.linear_order } instance : distrib_lattice ℝ≥0 := by apply_instance instance : semilattice_inf_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : semilattice_sup_bot ℝ≥0 := { .. nnreal.order_bot, .. nnreal.distrib_lattice } instance : linear_ordered_semiring ℝ≥0 := { add_left_cancel := assume a b c h, nnreal.eq $ @add_left_cancel ℝ _ a b c (nnreal.eq_iff.2 h), add_right_cancel := assume a b c h, nnreal.eq $ @add_right_cancel ℝ _ a b c (nnreal.eq_iff.2 h), le_of_add_le_add_left := assume a b c, @le_of_add_le_add_left ℝ _ a b c, mul_lt_mul_of_pos_left := assume a b c, @mul_lt_mul_of_pos_left ℝ _ a b c, mul_lt_mul_of_pos_right := assume a b c, @mul_lt_mul_of_pos_right ℝ _ a b c, zero_le_one := @zero_le_one ℝ _, exists_pair_ne := ⟨0, 1, ne_of_lt (@zero_lt_one ℝ _ _)⟩, .. nnreal.linear_order, .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring, } instance : linear_ordered_comm_group_with_zero ℝ≥0 := { mul_le_mul_left := assume a b h c, mul_le_mul (le_refl c) h (zero_le a) (zero_le c), zero_le_one := zero_le 1, .. nnreal.linear_ordered_semiring, .. nnreal.comm_group_with_zero } instance : canonically_ordered_comm_semiring ℝ≥0 := { .. nnreal.canonically_ordered_add_monoid, .. nnreal.comm_semiring, .. (show no_zero_divisors ℝ≥0, by apply_instance), .. nnreal.comm_group_with_zero } instance : densely_ordered ℝ≥0 := ⟨assume a b (h : (a : ℝ) < b), let ⟨c, hac, hcb⟩ := exists_between h in ⟨⟨c, le_trans a.property $ le_of_lt $ hac⟩, hac, hcb⟩⟩ instance : no_top_order ℝ≥0 := ⟨assume a, let ⟨b, hb⟩ := no_top (a:ℝ) in ⟨⟨b, le_trans a.property $ le_of_lt $ hb⟩, hb⟩⟩ lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : ℝ≥0 → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨nnreal.of_real b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_left_of_le $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : ℝ≥0 → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : ℝ≥0 → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Sup_empty] }, rcases h with ⟨⟨b, hb⟩, hbs⟩, by_cases h' : bdd_above s, { exact le_cSup_of_le (bdd_above_coe.2 h') (set.mem_image_of_mem _ hbs) hb }, { rw [real.Sup_of_not_bdd_above], rwa [bdd_above_coe] } end⟩⟩ instance : has_Inf ℝ≥0 := ⟨λs, ⟨Inf ((coe : ℝ≥0 → ℝ) '' s), begin cases s.eq_empty_or_nonempty with h h, { simp [h, set.image_empty, real.Inf_empty] }, exact le_cInf (h.image _) (assume r ⟨q, _, eq⟩, eq ▸ q.2) end⟩⟩ lemma coe_Sup (s : set ℝ≥0) : (↑(Sup s) : ℝ) = Sup ((coe : ℝ≥0 → ℝ) '' s) := rfl lemma coe_Inf (s : set ℝ≥0) : (↑(Inf s) : ℝ) = Inf ((coe : ℝ≥0 → ℝ) '' s) := rfl instance : conditionally_complete_linear_order_bot ℝ≥0 := { Sup := Sup, Inf := Inf, le_cSup := assume s a hs ha, le_cSup (bdd_above_coe.2 hs) (set.mem_image_of_mem _ ha), cSup_le := assume s a hs h,show Sup ((coe : ℝ≥0 → ℝ) '' s) ≤ a, from cSup_le (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cInf_le := assume s a _ has, cInf_le (bdd_below_coe s) (set.mem_image_of_mem _ has), le_cInf := assume s a hs h, show (↑a : ℝ) ≤ Inf ((coe : ℝ≥0 → ℝ) '' s), from le_cInf (by simp [hs]) $ assume r ⟨b, hb, eq⟩, eq ▸ h hb, cSup_empty := nnreal.eq $ by simp [coe_Sup, real.Sup_empty]; refl, decidable_le := begin assume x y, apply classical.dec end, .. nnreal.linear_ordered_semiring, .. lattice_of_linear_order, .. nnreal.order_bot } instance : archimedean ℝ≥0 := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (n •ℕ y : ℝ≥0), by simp [*, -nsmul_eq_mul, nsmul_coe]⟩ ⟩ lemma le_of_forall_epsilon_le {a b : ℝ≥0} (h : ∀ε, 0 < ε → a ≤ b + ε) : a ≤ b := le_of_forall_le_of_dense $ assume x hxb, begin rcases le_iff_exists_add.1 (le_of_lt hxb) with ⟨ε, rfl⟩, exact h _ ((lt_add_iff_pos_right b).1 hxb) end lemma lt_iff_exists_rat_btwn (a b : ℝ≥0) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < nnreal.of_real q ∧ nnreal.of_real q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [coe_of_real _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : ℝ≥0) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := begin cases le_total b c with h h, { simp [sup_eq_max, max_eq_right h, max_eq_right (mul_le_mul_of_nonneg_left h (zero_le a))] }, { simp [sup_eq_max, max_eq_left h, max_eq_left (mul_le_mul_of_nonneg_left h (zero_le a))] }, end lemma mul_finset_sup {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : r * s.sup f = s.sup (λa, r * f a) := begin refine s.induction_on _ _, { simp [bot_eq_zero] }, { assume a s has ih, simp [has, ih, mul_sup], } end @[simp, norm_cast] lemma coe_max (x y : ℝ≥0) : ((max x y : ℝ≥0) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, norm_cast] lemma coe_min (x y : ℝ≥0) : ((min x y : ℝ≥0) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : ℝ≥0} : 0 ≤ (q : ℝ) := q.2 @[simp] lemma of_real_zero : nnreal.of_real 0 = 0 := by simp [nnreal.of_real]; refl @[simp] lemma of_real_one : nnreal.of_real 1 = 1 := by simp [nnreal.of_real, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma of_real_pos {r : ℝ} : 0 < nnreal.of_real r ↔ 0 < r := by simp [nnreal.of_real, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma of_real_eq_zero {r : ℝ} : nnreal.of_real r = 0 ↔ r ≤ 0 := by simpa [-of_real_pos] using (not_iff_not.2 (@of_real_pos r)) lemma of_real_of_nonpos {r : ℝ} : r ≤ 0 → nnreal.of_real r = 0 := of_real_eq_zero.2 @[simp] lemma of_real_le_of_real_iff {r p : ℝ} (hp : 0 ≤ p) : nnreal.of_real r ≤ nnreal.of_real p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, nnreal.of_real, hp] @[simp] lemma of_real_lt_of_real_iff' {r p : ℝ} : nnreal.of_real r < nnreal.of_real p ↔ r < p ∧ 0 < p := by simp [nnreal.coe_lt_coe.symm, nnreal.of_real, lt_irrefl] lemma of_real_lt_of_real_iff {r p : ℝ} (h : 0 < p) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans (and_iff_left h) lemma of_real_lt_of_real_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : nnreal.of_real r < nnreal.of_real p ↔ r < p := of_real_lt_of_real_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma of_real_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real (r + p) = nnreal.of_real r + nnreal.of_real p := nnreal.eq $ by simp [nnreal.of_real, hr, hp, add_nonneg] lemma of_real_add_of_real {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : nnreal.of_real r + nnreal.of_real p = nnreal.of_real (r + p) := (of_real_add hr hp).symm lemma of_real_le_of_real {r p : ℝ} (h : r ≤ p) : nnreal.of_real r ≤ nnreal.of_real p := nnreal.of_real_mono h lemma of_real_add_le {r p : ℝ} : nnreal.of_real (r + p) ≤ nnreal.of_real r + nnreal.of_real p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma of_real_le_iff_le_coe {r : ℝ} {p : ℝ≥0} : nnreal.of_real r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : ℝ≥0} {p : ℝ} (hp : 0 ≤ p) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma le_of_real_iff_coe_le' {r : ℝ≥0} {p : ℝ} (hr : 0 < r) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := (le_or_lt 0 p).elim le_of_real_iff_coe_le $ λ hp, by simp only [(hp.trans_le r.coe_nonneg).not_le, of_real_eq_zero.2 hp.le, hr.not_le] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : ℝ≥0} (ha : 0 ≤ r) : nnreal.of_real r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, nnreal.coe_of_real r ha] lemma lt_of_real_iff_coe_lt {r : ℝ≥0} {p : ℝ} : r < nnreal.of_real p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, nnreal.coe_of_real p h] }, { rw [of_real_eq_zero.2 h], split, intro, have := not_lt_of_le (zero_le r), contradiction, intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (coe_nonneg _) rp), contradiction } end end of_real section mul lemma mul_eq_mul_left {a b c : ℝ≥0} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := begin rw [← nnreal.eq_iff, ← nnreal.eq_iff, nnreal.coe_mul, nnreal.coe_mul], split, { exact mul_left_cancel' (mt (@nnreal.eq_iff a 0).1 h) }, { assume h, rw [h] } end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : nnreal.of_real (p * q) = nnreal.of_real p * nnreal.of_real q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, simp [nnreal.of_real, hp, hq, max_eq_left, mul_nonneg] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [of_real_eq_zero.2 hq, of_real_eq_zero.2 hpq, mul_zero] } end end mul section sub lemma sub_def {r p : ℝ≥0} : r - p = nnreal.of_real (r - p) := rfl lemma sub_eq_zero {r p : ℝ≥0} (h : r ≤ p) : r - p = 0 := nnreal.eq $ max_eq_right $ sub_le_iff_le_add.2 $ by simpa [nnreal.coe_le_coe] using h @[simp] lemma sub_self {r : ℝ≥0} : r - r = 0 := sub_eq_zero $ le_refl r @[simp] lemma sub_zero {r : ℝ≥0} : r - 0 = r := by rw [sub_def, nnreal.coe_zero, sub_zero, nnreal.of_real_coe] lemma sub_pos {r p : ℝ≥0} : 0 < r - p ↔ p < r := of_real_pos.trans $ sub_pos.trans $ nnreal.coe_lt_coe protected lemma sub_lt_self {r p : ℝ≥0} : 0 < r → 0 < p → r - p < r := assume hr hp, begin cases le_total r p, { rwa [sub_eq_zero h] }, { rw [← nnreal.coe_lt_coe, nnreal.coe_sub h], exact sub_lt_self _ hp } end @[simp] lemma sub_le_iff_le_add {r p q : ℝ≥0} : r - p ≤ q ↔ r ≤ q + p := match le_total p r with | or.inl h := by rw [← nnreal.coe_le_coe, ← nnreal.coe_le_coe, nnreal.coe_sub h, nnreal.coe_add, sub_le_iff_le_add] | or.inr h := have r ≤ p + q, from le_add_right h, by simpa [nnreal.coe_le_coe, nnreal.coe_le_coe, sub_eq_zero h, add_comm] end @[simp] lemma sub_le_self {r p : ℝ≥0} : r - p ≤ r := sub_le_iff_le_add.2 $ le_add_right $ le_refl r lemma add_sub_cancel {r p : ℝ≥0} : (p + r) - r = p := nnreal.eq $ by rw [nnreal.coe_sub, nnreal.coe_add, add_sub_cancel]; exact le_add_left (le_refl _) lemma add_sub_cancel' {r p : ℝ≥0} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] @[simp] lemma sub_add_cancel_of_le {a b : ℝ≥0} (h : b ≤ a) : (a - b) + b = a := nnreal.eq $ by rw [nnreal.coe_add, nnreal.coe_sub h, sub_add_cancel] lemma sub_sub_cancel_of_le {r p : ℝ≥0} (h : r ≤ p) : p - (p - r) = r := by rw [nnreal.sub_def, nnreal.sub_def, nnreal.coe_of_real _ $ sub_nonneg.2 h, sub_sub_cancel, nnreal.of_real_coe] lemma lt_sub_iff_add_lt {p q r : ℝ≥0} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : ℝ≥0) : ℝ) = (q : ℝ) - (r : ℝ) := nnreal.coe_sub (le_of_lt (sub_pos.1 (lt_of_le_of_lt (zero_le _) H))), rwa [← nnreal.coe_lt_coe, this, lt_sub_iff_add_lt, ← nnreal.coe_add] at H }, { assume H, have : r ≤ q := le_trans (le_add_left (le_refl _)) (le_of_lt H), rwa [← nnreal.coe_lt_coe, nnreal.coe_sub this, lt_sub_iff_add_lt, ← nnreal.coe_add] } end lemma sub_lt_iff_lt_add {a b c : ℝ≥0} (h : b ≤ a) : a - b < c ↔ a < b + c := by simp only [←nnreal.coe_lt_coe, nnreal.coe_sub h, nnreal.coe_add, sub_lt_iff_lt_add'] lemma sub_eq_iff_eq_add {a b c : ℝ≥0} (h : b ≤ a) : a - b = c ↔ a = c + b := by rw [←nnreal.eq_iff, nnreal.coe_sub h, ←nnreal.eq_iff, nnreal.coe_add, sub_eq_iff_eq_add] end sub section inv lemma div_def {r p : ℝ≥0} : r / p = r * p⁻¹ := rfl lemma sum_div {ι} (s : finset ι) (f : ι → ℝ≥0) (b : ℝ≥0) : (∑ i in s, f i) / b = ∑ i in s, (f i / b) := by simp only [nnreal.div_def, finset.sum_mul] @[simp] lemma inv_zero : (0 : ℝ≥0)⁻¹ = 0 := nnreal.eq inv_zero @[simp] lemma inv_eq_zero {r : ℝ≥0} : (r : ℝ≥0)⁻¹ = 0 ↔ r = 0 := inv_eq_zero @[simp] lemma inv_pos {r : ℝ≥0} : 0 < r⁻¹ ↔ 0 < r := by simp [zero_lt_iff_ne_zero] lemma div_pos {r p : ℝ≥0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := mul_pos hr (inv_pos.2 hp) @[simp] lemma inv_one : (1:ℝ≥0)⁻¹ = 1 := nnreal.eq $ inv_one @[simp] lemma div_one {r : ℝ≥0} : r / 1 = r := by rw [div_def, inv_one, mul_one] protected lemma mul_inv {r p : ℝ≥0} : (r * p)⁻¹ = p⁻¹ * r⁻¹ := nnreal.eq $ mul_inv_rev' _ _ protected lemma inv_pow {r : ℝ≥0} {n : ℕ} : (r^n)⁻¹ = (r⁻¹)^n := nnreal.eq $ by { push_cast, exact (inv_pow' _ _).symm } @[simp] lemma inv_mul_cancel {r : ℝ≥0} (h : r ≠ 0) : r⁻¹ * r = 1 := nnreal.eq $ inv_mul_cancel $ mt (@nnreal.eq_iff r 0).1 h @[simp] lemma mul_inv_cancel {r : ℝ≥0} (h : r ≠ 0) : r * r⁻¹ = 1 := by rw [mul_comm, inv_mul_cancel h] @[simp] lemma div_self {r : ℝ≥0} (h : r ≠ 0) : r / r = 1 := mul_inv_cancel h lemma div_self_le (r : ℝ≥0) : r / r ≤ 1 := if h : r = 0 then by simp [h] else by rw [div_self h] @[simp] lemma mul_div_cancel {r p : ℝ≥0} (h : p ≠ 0) : r * p / p = r := by rw [div_def, mul_assoc, mul_inv_cancel h, mul_one] @[simp] lemma mul_div_cancel' {r p : ℝ≥0} (h : r ≠ 0) : r * (p / r) = p := by rw [mul_comm, div_mul_cancel _ h] @[simp] lemma inv_inv {r : ℝ≥0} : r⁻¹⁻¹ = r := nnreal.eq (inv_inv' _) @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (zero_lt_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← @mul_le_mul_left _ _ a _ r this, ← mul_assoc, mul_inv_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := by rw [div_def, mul_comm, ← mul_le_iff_le_inv hr, mul_comm] lemma div_le_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r := @div_le_iff ℝ _ a r b $ zero_lt_iff_ne_zero.2 hr lemma le_of_forall_lt_one_mul_lt {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := zero_lt_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := eq.symm $ right_distrib a b (c⁻¹) lemma half_pos {a : ℝ≥0} (h : 0 < a) : 0 < a / 2 := div_pos h zero_lt_two lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := nnreal.eq (add_halves a) lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := by rw [← nnreal.coe_lt_coe, nnreal.coe_div]; exact half_lt_self (bot_lt_iff_ne_bot.2 h) lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := by simpa [div_def] using half_lt_self zero_ne_one.symm lemma div_lt_iff {a b c : ℝ≥0} (hc : c ≠ 0) : b / c < a ↔ b < a * c := begin rw [← nnreal.coe_lt_coe, ← nnreal.coe_lt_coe, nnreal.coe_div, nnreal.coe_mul], exact div_lt_iff (zero_lt_iff_ne_zero.mpr hc) end lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] theorem div_pow {a b : ℝ≥0} (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := div_pow _ _ _ @[field_simps] lemma mul_div_assoc' (a b c : ℝ≥0) : a * (b / c) = (a * b) / c := by rw [div_def, div_def, mul_assoc] @[field_simps] lemma div_add_div (a : ℝ≥0) {b : ℝ≥0} (c : ℝ≥0) {d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := begin rw ← nnreal.eq_iff, simp only [nnreal.coe_add, nnreal.coe_div, nnreal.coe_mul], exact div_add_div _ _ (coe_ne_zero.2 hb) (coe_ne_zero.2 hd) end @[field_simps] lemma inv_eq_one_div (a : ℝ≥0) : a⁻¹ = 1/a := by rw [div_def, one_mul] @[field_simps] lemma div_mul_eq_mul_div (a b c : ℝ≥0) : (a / b) * c = (a * c) / b := by { rw [div_def, div_def], ac_refl } @[field_simps] lemma add_div' (a b c : ℝ≥0) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc @[field_simps] lemma div_add' (a b c : ℝ≥0) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma one_div (a : ℝ≥0) : 1 / a = a⁻¹ := one_mul a⁻¹ lemma one_div_div (a b : ℝ≥0) : 1 / (a / b) = b / a := by { rw ← nnreal.eq_iff, simp [one_div_div] } lemma div_eq_mul_one_div (a b : ℝ≥0) : a / b = a * (1 / b) := by rw [div_def, div_def, one_mul] @[field_simps] lemma div_div_eq_mul_div (a b c : ℝ≥0) : a / (b / c) = (a * c) / b := by { rw ← nnreal.eq_iff, simp [div_div_eq_mul_div] } @[field_simps] lemma div_div_eq_div_mul (a b c : ℝ≥0) : (a / b) / c = a / (b * c) := by { rw ← nnreal.eq_iff, simp [div_div_eq_div_mul] } @[field_simps] lemma div_eq_div_iff {a b c d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := div_eq_div_iff hb hd @[field_simps] lemma div_eq_iff {a b c : ℝ≥0} (hb : b ≠ 0) : a / b = c ↔ a = c * b := by simpa using @div_eq_div_iff a b c 1 hb one_ne_zero @[field_simps] lemma eq_div_iff {a b c : ℝ≥0} (hb : b ≠ 0) : c = a / b ↔ c * b = a := by simpa using @div_eq_div_iff c 1 a b one_ne_zero hb lemma of_real_inv {x : ℝ} : nnreal.of_real x⁻¹ = (nnreal.of_real x)⁻¹ := begin by_cases hx : 0 ≤ x, { nth_rewrite 0 ← coe_of_real x hx, rw [←nnreal.coe_inv, of_real_coe], }, { have hx' := le_of_not_ge hx, rw [of_real_eq_zero.mpr hx', inv_zero, of_real_eq_zero.mpr (inv_nonpos.mpr hx')], }, end lemma of_real_div {x y : ℝ} (hx : 0 ≤ x) : nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y := by rw [div_def, ←of_real_inv, ←of_real_mul hx, div_eq_mul_inv] lemma of_real_div' {x y : ℝ} (hy : 0 ≤ y) : nnreal.of_real (x / y) = nnreal.of_real x / nnreal.of_real y := by rw [div_def, ←of_real_inv, mul_comm, ←@of_real_mul y⁻¹ _ (by simp [hy]), mul_comm, div_eq_mul_inv] end inv section pow theorem pow_eq_zero {a : ℝ≥0} {n : ℕ} (h : a^n = 0) : a = 0 := begin rw ← nnreal.eq_iff, rw [← nnreal.eq_iff, coe_pow] at h, exact pow_eq_zero h end @[field_simps] theorem pow_ne_zero {a : ℝ≥0} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h end pow @[simp] lemma abs_eq (x : ℝ≥0) : abs (x : ℝ) = x := abs_of_nonneg x.property end nnreal /-- The absolute value on `ℝ` as a map to `ℝ≥0`. -/ @[pp_nodot] def real.nnabs (x : ℝ) : ℝ≥0 := ⟨abs x, abs_nonneg x⟩ @[norm_cast, simp] lemma nnreal.coe_nnabs (x : ℝ) : (real.nnabs x : ℝ) = abs x := by simp [real.nnabs]
306daa3f75ddf0a3bfb69b8b9826cf0a65be982c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/witt_vector/discrete_valuation_ring.lean
f44bfbc19e7a62beed7a1df38829b420c43b561f
[ "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
6,211
lean
/- Copyright (c) 2022 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Heather Macbeth, Johan Commelin -/ import ring_theory.witt_vector.domain import ring_theory.witt_vector.mul_coeff import ring_theory.discrete_valuation_ring.basic import tactic.linear_combination /-! # Witt vectors over a perfect ring > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file establishes that Witt vectors over a perfect field are a discrete valuation ring. When `k` is a perfect ring, a nonzero `a : 𝕎 k` can be written as `p^m * b` for some `m : ℕ` and `b : 𝕎 k` with nonzero 0th coefficient. When `k` is also a field, this `b` can be chosen to be a unit of `𝕎 k`. ## Main declarations * `witt_vector.exists_eq_pow_p_mul`: the existence of this element `b` over a perfect ring * `witt_vector.exists_eq_pow_p_mul'`: the existence of this unit `b` over a perfect field * `witt_vector.discrete_valuation_ring`: `𝕎 k` is a discrete valuation ring if `k` is a perfect field -/ noncomputable theory namespace witt_vector variables {p : ℕ} [hp : fact p.prime] include hp local notation `𝕎` := witt_vector p section comm_ring variables {k : Type*} [comm_ring k] [char_p k p] /-- This is the `n+1`st coefficient of our inverse. -/ def succ_nth_val_units (n : ℕ) (a : units k) (A : 𝕎 k) (bs : fin (n+1) → k) : k := - ↑(a⁻¹ ^ (p^(n+1))) * (A.coeff (n + 1) * ↑(a⁻¹ ^ (p^(n+1))) + nth_remainder p n (truncate_fun (n+1) A) bs) /-- Recursively defines the sequence of coefficients for the inverse to a Witt vector whose first entry is a unit. -/ noncomputable def inverse_coeff (a : units k) (A : 𝕎 k) : ℕ → k | 0 := ↑a⁻¹ | (n + 1) := succ_nth_val_units n a A (λ i, inverse_coeff i.val) using_well_founded { dec_tac := `[apply fin.is_lt] } /-- Upgrade a Witt vector `A` whose first entry `A.coeff 0` is a unit to be, itself, a unit in `𝕎 k`. -/ def mk_unit {a : units k} {A : 𝕎 k} (hA : A.coeff 0 = a) : units (𝕎 k) := units.mk_of_mul_eq_one A (witt_vector.mk p (inverse_coeff a A)) begin ext n, induction n with n ih, { simp [witt_vector.mul_coeff_zero, inverse_coeff, hA] }, let H_coeff := A.coeff (n + 1) * ↑(a⁻¹ ^ p ^ (n + 1)) + nth_remainder p n (truncate_fun (n + 1) A) (λ (i : fin (n + 1)), inverse_coeff a A i), have H := units.mul_inv (a ^ p ^ (n + 1)), linear_combination -H_coeff*H with { normalize := ff }, have ha : (a:k) ^ (p ^ (n + 1)) = ↑(a ^ (p ^ (n + 1))) := by norm_cast, have ha_inv : (↑(a⁻¹):k) ^ (p ^ (n + 1)) = ↑(a ^ (p ^ (n + 1)))⁻¹ := by exact_mod_cast inv_pow _ _, simp only [nth_remainder_spec, inverse_coeff, succ_nth_val_units, hA, fin.val_eq_coe, one_coeff_eq_of_pos, nat.succ_pos', H_coeff, ha_inv, ha, inv_pow], ring!, end @[simp] lemma coe_mk_unit {a : units k} {A : 𝕎 k} (hA : A.coeff 0 = a) : (mk_unit hA : 𝕎 k) = A := rfl end comm_ring section field variables {k : Type*} [field k] [char_p k p] lemma is_unit_of_coeff_zero_ne_zero (x : 𝕎 k) (hx : x.coeff 0 ≠ 0) : is_unit x := begin let y : kˣ := units.mk0 (x.coeff 0) hx, have hy : x.coeff 0 = y := rfl, exact (mk_unit hy).is_unit end variables (p) lemma irreducible : irreducible (p : 𝕎 k) := begin have hp : ¬ is_unit (p : 𝕎 k), { intro hp, simpa only [constant_coeff_apply, coeff_p_zero, not_is_unit_zero] using (constant_coeff : witt_vector p k →+* _).is_unit_map hp, }, refine ⟨hp, λ a b hab, _⟩, obtain ⟨ha0, hb0⟩ : a ≠ 0 ∧ b ≠ 0, { rw ← mul_ne_zero_iff, intro h, rw h at hab, exact p_nonzero p k hab }, obtain ⟨m, a, ha, rfl⟩ := verschiebung_nonzero ha0, obtain ⟨n, b, hb, rfl⟩ := verschiebung_nonzero hb0, cases m, { exact or.inl (is_unit_of_coeff_zero_ne_zero a ha) }, cases n, { exact or.inr (is_unit_of_coeff_zero_ne_zero b hb) }, rw iterate_verschiebung_mul at hab, apply_fun (λ x, coeff x 1) at hab, simp only [coeff_p_one, nat.add_succ, add_comm _ n, function.iterate_succ', function.comp_app, verschiebung_coeff_add_one, verschiebung_coeff_zero] at hab, exact (one_ne_zero hab).elim end end field section perfect_ring variables {k : Type*} [comm_ring k] [char_p k p] [perfect_ring k p] lemma exists_eq_pow_p_mul (a : 𝕎 k) (ha : a ≠ 0) : ∃ (m : ℕ) (b : 𝕎 k), b.coeff 0 ≠ 0 ∧ a = p ^ m * b := begin obtain ⟨m, c, hc, hcm⟩ := witt_vector.verschiebung_nonzero ha, obtain ⟨b, rfl⟩ := (frobenius_bijective p k).surjective.iterate m c, rw witt_vector.iterate_frobenius_coeff at hc, have := congr_fun (witt_vector.verschiebung_frobenius_comm.comp_iterate m) b, simp only [function.comp_app] at this, rw ← this at hcm, refine ⟨m, b, _, _⟩, { contrapose! hc, have : 0 < p ^ m := pow_pos (nat.prime.pos (fact.out _)) _, simp [hc, zero_pow this] }, { rw ← mul_left_iterate (p : 𝕎 k) m, convert hcm, ext1 x, rw [mul_comm, ← witt_vector.verschiebung_frobenius x] }, end end perfect_ring section perfect_field variables {k : Type*} [field k] [char_p k p] [perfect_ring k p] lemma exists_eq_pow_p_mul' (a : 𝕎 k) (ha : a ≠ 0) : ∃ (m : ℕ) (b : units (𝕎 k)), a = p ^ m * b := begin obtain ⟨m, b, h₁, h₂⟩ := exists_eq_pow_p_mul a ha, let b₀ := units.mk0 (b.coeff 0) h₁, have hb₀ : b.coeff 0 = b₀ := rfl, exact ⟨m, mk_unit hb₀, h₂⟩, end /- Note: The following lemma should be an instance, but it seems to cause some exponential blowups in certain typeclass resolution problems. See the following Lean4 issue as well as the zulip discussion linked there: https://github.com/leanprover/lean4/issues/1102 -/ /-- The ring of Witt Vectors of a perfect field of positive characteristic is a DVR. -/ lemma discrete_valuation_ring : discrete_valuation_ring (𝕎 k) := discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization begin refine ⟨p, irreducible p, λ x hx, _⟩, obtain ⟨n, b, hb⟩ := exists_eq_pow_p_mul' x hx, exact ⟨n, b, hb.symm⟩, end end perfect_field end witt_vector
6893cb036a97e8feb3a824fabdee67a307eac3e9
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/tests/lean/run/n4.lean
6e2e9fb04159cb4142b9a022a4e375a75c42f0b8
[ "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
440
lean
prelude definition Prop : Type.{1} := Type.{0} section variable N : Type.{1} variables a b c : N variable and : Prop → Prop → Prop local infixr `∧`:35 := and variable le : N → N → Prop variable lt : N → N → Prop precedence `≤`:50 precedence `<`:50 local infixl ≤ := le local infixl < := lt check a ≤ b definition T : Prop := a ≤ b check T end check T (* print(get_env():find("T"):value()) *)
81a2b042c5dfbac48c047ad11b2229869bf63183
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/series/tempLevel03.lean
32c6c3b05c8d36d18c8e54ce6f7cd71bfb1061eb
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,573
lean
import game.series.tempLevel02 variable X : Type --hide namespace xena /- Idea 03: comparison test -/ /- Lemma Suppose $0 ≤ a_n ≤ b_n$ for all n ∈ ℕ. If $∑ b_n$ converges then $∑ a_n$ converges and $∑ a_n ≤ ∑ b_n$. -/ theorem comparison_test (a b : ℕ → ℝ) (h1 : ∀ (n : ℕ), 0 ≤ a n) (h2 : ∀ (n : ℕ), a n ≤ b n) : series_converges b → series_converges a := begin --introduce hyp that series over b converges, with sum S intro hyp, -- we will use the following fact later -- our converging sequence of partials must be bounded have fact := bounded_if_convergent (seq_partials_over b) hyp, cases hyp with S hypS, --show that partial sums over a are all positive have fact1: ∀ k : ℕ, (0 ≤ kth_partial_sum a k), unfold kth_partial_sum, -- just making finset explicit intro k, have fact2: ∀ x ∈ (finset.range (k + 1)), 0 ≤ a x, intros x hx, specialize h1 x, exact h1, apply finset.sum_nonneg, exact fact2, --show that nth partial sum over a ≤ nth partial sum over b have fact3: ∀ k : ℕ, kth_partial_sum a k ≤ kth_partial_sum b k, intro k, have fact4: ∀ x ∈ (finset.range (k + 1)), a x ≤ b x, intros x hx, specialize h2 x, exact h2, apply finset.sum_le_sum, exact fact4, -- Although we know that our series over b converges with sum S -- we use the fact that the sequence of sums must be bounded -- by *some* T (i.e. we don't use the fact that S is the supremum) cases fact with T hypT, -- hypT is stated in terms of `abs` -- so our partial sums over b are bounded above have fact5: ∀ k : ℕ, kth_partial_sum b k ≤ T, unfold seq_partials_over at hypT, intro K, have fact6: kth_partial_sum b K ≤ |kth_partial_sum b K|, exact le_abs_self (kth_partial_sum b K), have := hypT K, linarith, -- so our partial sums over a are bounded above have fact7 : ∀ k : ℕ, kth_partial_sum a k ≤ T, intro k, specialize fact3 k, specialize fact5 k, linarith, -- bdd_mono_converges is the relevant theorem now. -- but it uses absolute values, so we will need to -- do a little more work using some facts proved above refine bdd_mono_converges (seq_partials_over a) _ _, unfold seq_partials_over, use T, intro m, let fact8 := fact7 m, refine max_le (fact7 m) _, have fact9 := fact1 m, linarith, unfold is_monotone, left, intro n, unfold seq_partials_over, let fact10 := finset.sum_range_succ a (n+1), have fact11 := h1 (n + 1), unfold kth_partial_sum, linarith, end end xena
b7c07195634b95e37e72fa34f963185e66656db6
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/polyhedral_lattice/Hom.lean
250cae3e815044435d7f3b02f753cac65bbab5aa
[]
no_license
Ja1941/lean-liquid
fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc
8e80ed0cbdf5145d6814e833a674eaf05a1495c1
refs/heads/master
1,689,437,983,362
1,628,362,719,000
1,628,362,719,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,272
lean
import polyhedral_lattice.pseudo_normed_group import polyhedral_lattice.int import polyhedral_lattice.category import pseudo_normed_group.category /-! # Category-theoretic Hom(Λ, M) If Λ is a polyhedral lattice then Hom(Λ, -) is a functor from profinitely filtered pseudo-normed groups equipped with T⁻¹ to itself. Furthermore, if Λ = ℤ then this functor is isomorphic to the identity functor. -/ noncomputable theory universe variables u open_locale nnreal -- enable the notation `ℝ≥0` for the nonnegative real numbers. open ProFiltPseuNormGrpWithTinv (of) /-- The additive group homomorphisms `Λ →+ M` as an object of `ProFiltPseuNormGrpWithTinv r'`. -/ def polyhedral_lattice.Hom {r' : ℝ≥0} [fact (0 < r')] (Λ M : Type*) [polyhedral_lattice Λ] [profinitely_filtered_pseudo_normed_group_with_Tinv r' M] : ProFiltPseuNormGrpWithTinv r' := of r' (Λ →+ M) namespace PolyhedralLattice open opposite pseudo_normed_group polyhedral_lattice profinitely_filtered_pseudo_normed_group open category_theory variables {r' : ℝ≥0} [fact (0 < r')] /-- Like `polyhedral_lattice.Hom` but functorial in the first entry. Unfortunately, this means that the entries are now swapped. So `(PolyhedralLattice.Hom M).obj (op Λ) = Λ →+ M`. -/ @[simps] def Hom (M : ProFiltPseuNormGrpWithTinv r') : PolyhedralLatticeᵒᵖ ⥤ (ProFiltPseuNormGrpWithTinv r') := { obj := λ Λ, (Hom Λ.unop M), map := λ Λ₁ Λ₂ f, { to_fun := λ g, g.comp f.unop.to_add_monoid_hom, map_zero' := add_monoid_hom.zero_comp _, map_add' := λ g₁ g₂, add_monoid_hom.add_comp _ _ _, strict' := λ c g hg c' l hl, hg ((f.unop.strict_nnnorm _).trans hl), continuous' := λ c, begin rw [add_monoid_hom.continuous_iff], intro l, haveI : fact (∥f.unop l∥₊ ≤ ∥l∥₊) := ⟨f.unop.strict_nnnorm l⟩, have aux := (continuous_apply (f.unop l)).comp (add_monoid_hom.incl_continuous Λ₁.unop r' M c), rwa (embedding_cast_le (c * ∥f.unop l∥₊) (c * ∥l∥₊)).continuous_iff at aux end, map_Tinv' := λ g, by { ext l, refl } }, map_id' := λ Λ, by { ext, refl }, map_comp' := by { intros, ext, refl } } end PolyhedralLattice namespace polyhedral_lattice /-! In the remainder of the file, we show that `Hom(ℤ, M)` is isomorphic to `M`. -/ open pseudo_normed_group profinitely_filtered_pseudo_normed_group_with_Tinv_hom variables {r' : ℝ≥0} [fact (0 < r')] variables (M : ProFiltPseuNormGrpWithTinv.{u} r') /-- `HomZ_map` as an equiv. -/ @[simps] def HomZ_map_equiv : Hom ℤ M ≃+ M := { to_fun := add_monoid_hom.eval 1, inv_fun := (smul_add_hom ℤ M).flip, map_add' := add_monoid_hom.map_add _, left_inv := λ f, by { ext, exact one_smul _ _ }, right_inv := λ x, one_smul _ _ } lemma HomZ_map_equiv_strict (c : ℝ≥0) (f : (Hom ℤ M)) : f ∈ filtration (Hom ℤ M) c ↔ (HomZ_map_equiv M) f ∈ filtration M c := begin split, { intro hf, simpa only [mul_one] using hf int.one_mem_filtration }, { intros hx c₁ n hn, rw [semi_normed_group.mem_filtration_iff] at hn, have aux := pseudo_normed_group.int_smul_mem_filtration n _ c hx, rw [nnreal.coe_nat_abs] at aux, rw [← (HomZ_map_equiv M).symm_apply_apply f, HomZ_map_equiv_symm_apply, add_monoid_hom.flip_apply, smul_add_hom_apply, mul_comm], exact pseudo_normed_group.filtration_mono (mul_le_mul_right' hn c) aux } end lemma HomZ_map_equiv_ctu (c : ℝ≥0) : continuous (level (HomZ_map_equiv M) (λ c x , (HomZ_map_equiv_strict M c x).1) c) := begin haveI : fact (c * ∥(1:ℤ)∥₊ ≤ c) := ⟨by rw [nnnorm_one, mul_one]⟩, have aux := add_monoid_hom.incl_continuous ℤ r' M c, have aux2 := (continuous_apply 1).comp aux, rwa (profinitely_filtered_pseudo_normed_group.embedding_cast_le (c * ∥(1:ℤ)∥₊) c).continuous_iff at aux2 end /-- The isomorphism `Hom ℤ M ≅ M` for `M` a `profinitely_filtered_pseudo_normed_group_with_Tinv`. -/ noncomputable def HomZ_iso : Hom ℤ M ≅ M := ProFiltPseuNormGrpWithTinv.iso_of_equiv_of_strict' (HomZ_map_equiv M) (HomZ_map_equiv_strict M) (HomZ_map_equiv_ctu M) $ λ x, by { simp only [add_monoid_hom.eval_apply_apply, HomZ_map_equiv_apply], refl } end polyhedral_lattice #lint-
b92d66a40a1e66d4e5dcef2fec41a5d6854cd32e
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/category/FinVect/limits.lean
e27d7c05aa3c239001bbd87355cb3b7a1e969ced
[ "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
2,850
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 algebra.category.FinVect import algebra.category.Module.limits import algebra.category.Module.products import algebra.category.Module.epi_mono import category_theory.limits.creates import category_theory.limits.shapes.finite_limits import category_theory.limits.constructions.limits_of_products_and_equalizers /-! # `forget₂ (FinVect K) (Module K)` creates all finite limits. And hence `FinVect K` has all finite limits. ## Future work After generalising `FinVect` to allow the ring and the module to live in different universes, generalize this construction so we can take limits over smaller diagrams, as is done for the other algebraic categories. -/ noncomputable theory universes v u open category_theory open category_theory.limits namespace FinVect variables {J : Type} [small_category J] [fin_category J] variables {k : Type v} [field k] instance {J : Type} [fintype J] (Z : J → Module.{v} k) [∀ j, finite_dimensional k (Z j)] : finite_dimensional k (∏ λ j, Z j : Module.{v} k) := begin haveI : finite_dimensional k (Module.of k (Π j, Z j)), { dsimp, apply_instance, }, exact finite_dimensional.of_injective (Module.pi_iso_pi _).hom ((Module.mono_iff_injective _).1 (by apply_instance)), end /-- Finite limits of finite finite dimensional vectors spaces are finite dimensional, because we can realise them as subobjects of a finite product. -/ instance (F : J ⥤ FinVect k) : finite_dimensional k (limit (F ⋙ forget₂ (FinVect k) (Module.{v} k)) : Module.{v} k) := begin haveI : ∀ j, finite_dimensional k ((F ⋙ forget₂ (FinVect k) (Module.{v} k)).obj j), { intro j, change finite_dimensional k (F.obj j), apply_instance, }, exact finite_dimensional.of_injective (limit_subobject_product (F ⋙ forget₂ (FinVect k) (Module.{v} k))) ((Module.mono_iff_injective _).1 (by apply_instance)), end /-- The forgetful functor from `FinVect k` to `Module k` creates all finite limits. -/ def forget₂_creates_limit (F : J ⥤ FinVect k) : creates_limit F (forget₂ (FinVect k) (Module.{v} k)) := creates_limit_of_fully_faithful_of_iso ⟨(limit (F ⋙ forget₂ (FinVect k) (Module.{v} k)) : Module.{v} k), by apply_instance⟩ (iso.refl _) instance : creates_limits_of_shape J (forget₂ (FinVect k) (Module.{v} k)) := { creates_limit := λ F, forget₂_creates_limit F, } instance : has_finite_limits (FinVect k) := { out := λ J _ _, by exactI has_limits_of_shape_of_has_limits_of_shape_creates_limits_of_shape (forget₂ (FinVect k) (Module.{v} k)), } instance : preserves_finite_limits (forget₂ (FinVect k) (Module.{v} k)) := { preserves_finite_limits := λ J _ _, by exactI infer_instance, } end FinVect
2072bfe7507922aeab41153d8803336df70866d5
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/library/data/list/default.lean
521bd8a30309ca0a552a37bfb59ad53de67ca0ad
[ "Apache-2.0" ]
permissive
respu/lean
6582d19a2f2838a28ecd2b3c6f81c32d07b5341d
8c76419c60b63d0d9f7bc04ebb0b99812d0ec654
refs/heads/master
1,610,882,451,231
1,427,747,084,000
1,427,747,429,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
186
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Jeremy Avigad import data.list.basic
0fb3d7c4aba418f5274d165fcd9e0f90f9444ce4
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/sums/basic.lean
5f93ad854be9d349c1dc991f0cb62641e490bfde
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
5,031
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.eq_to_hom /-# Disjoint unions of categories, functors, and natural transformations. -/ namespace category_theory universes v₁ u₁ -- declare the `v`'s first; see `category_theory.category` for an explanation open sum section variables (C : Type u₁) [category.{v₁} C] (D : Type u₁) [category.{v₁} D] /-- `sum C D` gives the direct sum of two categories. -/ instance sum : category.{v₁} (C ⊕ D) := { hom := λ X Y, match X, Y with | inl X, inl Y := X ⟶ Y | inl X, inr Y := pempty | inr X, inl Y := pempty | inr X, inr Y := X ⟶ Y end, id := λ X, match X with | inl X := 𝟙 X | inr X := 𝟙 X end, comp := λ X Y Z f g, match X, Y, Z, f, g with | inl X, inl Y, inl Z, f, g := f ≫ g | inr X, inr Y, inr Z, f, g := f ≫ g end } @[simp] lemma sum_comp_inl {P Q R : C} (f : (inl P : C ⊕ D) ⟶ inl Q) (g : inl Q ⟶ inl R) : f ≫ g = (f : P ⟶ Q) ≫ (g : Q ⟶ R) := rfl @[simp] lemma sum_comp_inr {P Q R : D} (f : (inr P : C ⊕ D) ⟶ inr Q) (g : inr Q ⟶ inr R) : f ≫ g = (f : P ⟶ Q) ≫ (g : Q ⟶ R) := rfl end namespace sum variables (C : Type u₁) [category.{v₁} C] (D : Type u₁) [category.{v₁} D] /-- `inl_` is the functor `X ↦ inl X`. -/ -- Unfortunate naming here, suggestions welcome. @[simps] def inl_ : C ⥤ C ⊕ D := { obj := λ X, inl X, map := λ X Y f, f } /-- `inr_` is the functor `X ↦ inr X`. -/ @[simps] def inr_ : D ⥤ C ⊕ D := { obj := λ X, inr X, map := λ X Y f, f } /-- The functor exchanging two direct summand categories. -/ def swap : C ⊕ D ⥤ D ⊕ C := { obj := λ X, match X with | inl X := inr X | inr X := inl X end, map := λ X Y f, match X, Y, f with | inl X, inl Y, f := f | inr X, inr Y, f := f end } @[simp] lemma swap_obj_inl (X : C) : (swap C D).obj (inl X) = inr X := rfl @[simp] lemma swap_obj_inr (X : D) : (swap C D).obj (inr X) = inl X := rfl @[simp] lemma swap_map_inl {X Y : C} {f : inl X ⟶ inl Y} : (swap C D).map f = f := rfl @[simp] lemma swap_map_inr {X Y : D} {f : inr X ⟶ inr Y} : (swap C D).map f = f := rfl namespace swap /-- `swap` gives an equivalence between `C ⊕ D` and `D ⊕ C`. -/ def equivalence : C ⊕ D ≌ D ⊕ C := equivalence.mk (swap C D) (swap D C) (nat_iso.of_components (λ X, eq_to_iso (by { cases X; refl })) (by tidy)) (nat_iso.of_components (λ X, eq_to_iso (by { cases X; refl })) (by tidy)) instance is_equivalence : is_equivalence (swap C D) := (by apply_instance : is_equivalence (equivalence C D).functor) /-- The double swap on `C ⊕ D` is naturally isomorphic to the identity functor. -/ def symmetry : swap C D ⋙ swap D C ≅ 𝟭 (C ⊕ D) := (equivalence C D).unit_iso.symm end swap end sum variables {A : Type u₁} [category.{v₁} A] {B : Type u₁} [category.{v₁} B] {C : Type u₁} [category.{v₁} C] {D : Type u₁} [category.{v₁} D] namespace functor /-- The sum of two functors. -/ def sum (F : A ⥤ B) (G : C ⥤ D) : A ⊕ C ⥤ B ⊕ D := { obj := λ X, match X with | inl X := inl (F.obj X) | inr X := inr (G.obj X) end, map := λ X Y f, match X, Y, f with | inl X, inl Y, f := F.map f | inr X, inr Y, f := G.map f end, map_id' := λ X, begin cases X; unfold_aux, erw F.map_id, refl, erw G.map_id, refl end, map_comp' := λ X Y Z f g, match X, Y, Z, f, g with | inl X, inl Y, inl Z, f, g := by { unfold_aux, erw F.map_comp, refl } | inr X, inr Y, inr Z, f, g := by { unfold_aux, erw G.map_comp, refl } end } @[simp] lemma sum_obj_inl (F : A ⥤ B) (G : C ⥤ D) (a : A) : (F.sum G).obj (inl a) = inl (F.obj a) := rfl @[simp] lemma sum_obj_inr (F : A ⥤ B) (G : C ⥤ D) (c : C) : (F.sum G).obj (inr c) = inr (G.obj c) := rfl @[simp] lemma sum_map_inl (F : A ⥤ B) (G : C ⥤ D) {a a' : A} (f : inl a ⟶ inl a') : (F.sum G).map f = F.map f := rfl @[simp] lemma sum_map_inr (F : A ⥤ B) (G : C ⥤ D) {c c' : C} (f : inr c ⟶ inr c') : (F.sum G).map f = G.map f := rfl end functor namespace nat_trans /-- The sum of two natural transformations. -/ def sum {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟶ G) (β : H ⟶ I) : F.sum H ⟶ G.sum I := { app := λ X, match X with | inl X := α.app X | inr X := β.app X end, naturality' := λ X Y f, match X, Y, f with | inl X, inl Y, f := begin unfold_aux, erw α.naturality, refl, end | inr X, inr Y, f := begin unfold_aux, erw β.naturality, refl, end end } @[simp] lemma sum_app_inl {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟶ G) (β : H ⟶ I) (a : A) : (sum α β).app (inl a) = α.app a := rfl @[simp] lemma sum_app_inr {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟶ G) (β : H ⟶ I) (c : C) : (sum α β).app (inr c) = β.app c := rfl end nat_trans end category_theory