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
44924f18da55c8d7962161bc2db18f73bc57be54
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/eq21.lean
00afa15c69d7f1c8395aaf4864024109aceee54a
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
429
lean
inductive formula := | eqf : nat → nat → formula | impf : formula → formula → formula namespace formula definition denote : formula → Prop | denote (eqf n1 n2) := n1 = n2 | denote (impf f1 f2) := denote f1 → denote f2 theorem denote_eqf (n1 n2 : nat) : denote (eqf n1 n2) = (n1 = n2) := rfl theorem denote_impf (f1 f2 : formula) : denote (impf f1 f2) = (denote f1 → denote f2) := rfl end formula
e55b1475bdd2e63555ca8e2aed36382ae45af56e
2c096fdfecf64e46ea7bc6ce5521f142b5926864
/src/Lean/Server/InfoUtils.lean
273a8aecab8cc0f8d83263acc8e154e72acecefe
[ "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
Kha/lean4
1005785d2c8797ae266a303968848e5f6ce2fe87
b99e11346948023cd6c29d248cd8f3e3fb3474cf
refs/heads/master
1,693,355,498,027
1,669,080,461,000
1,669,113,138,000
184,748,176
0
0
Apache-2.0
1,665,995,520,000
1,556,884,930,000
Lean
UTF-8
Lean
false
false
14,704
lean
/- Copyright (c) 2021 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.PrettyPrinter namespace Lean.Elab /-- Visit nodes, passing in a surrounding context (the innermost one) and accumulating results on the way back up. -/ partial def InfoTree.visitM [Monad m] (preNode : ContextInfo → Info → (children : PersistentArray InfoTree) → m Unit := fun _ _ _ => pure ()) (postNode : ContextInfo → Info → (children : PersistentArray InfoTree) → List (Option α) → m α) : InfoTree → m (Option α) := go none where go | _, context ctx t => go ctx t | some ctx, node i cs => do preNode ctx i cs let as ← cs.toList.mapM (go <| i.updateContext? ctx) postNode ctx i cs as | none, node .. => panic! "unexpected context-free info tree node" | _, hole .. => pure none /-- `InfoTree.visitM` specialized to `Unit` return type -/ def InfoTree.visitM' [Monad m] (preNode : ContextInfo → Info → (children : PersistentArray InfoTree) → m Unit := fun _ _ _ => pure ()) (postNode : ContextInfo → Info → (children : PersistentArray InfoTree) → m Unit := fun _ _ _ => pure ()) (t : InfoTree) : m Unit := t.visitM preNode (fun ci i cs _ => postNode ci i cs) |> discard /-- Visit nodes bottom-up, passing in a surrounding context (the innermost one) and the union of nested results (empty at leaves). -/ def InfoTree.collectNodesBottomUp (p : ContextInfo → Info → PersistentArray InfoTree → List α → List α) (i : InfoTree) : List α := i.visitM (m := Id) (postNode := fun ci i cs as => p ci i cs (as.filterMap id).join) |>.getD [] /-- For every branch of the `InfoTree`, find the deepest node in that branch for which `p` returns `some _` and return the union of all such nodes. The visitor `p` is given a node together with its innermost surrounding `ContextInfo`. -/ partial def InfoTree.deepestNodes (p : ContextInfo → Info → PersistentArray InfoTree → Option α) (infoTree : InfoTree) : List α := infoTree.collectNodesBottomUp fun ctx i cs rs => if rs.isEmpty then match p ctx i cs with | some r => [r] | none => [] else rs partial def InfoTree.foldInfo (f : ContextInfo → Info → α → α) (init : α) : InfoTree → α := go none init where go ctx? a | context ctx t => go ctx a t | node i ts => let a := match ctx? with | none => a | some ctx => f ctx i a ts.foldl (init := a) (go <| i.updateContext? ctx?) | _ => a def Info.isTerm : Info → Bool | ofTermInfo _ => true | _ => false def Info.isCompletion : Info → Bool | ofCompletionInfo .. => true | _ => false def InfoTree.getCompletionInfos (infoTree : InfoTree) : Array (ContextInfo × CompletionInfo) := infoTree.foldInfo (init := #[]) fun ctx info result => match info with | Info.ofCompletionInfo info => result.push (ctx, info) | _ => result def Info.stx : Info → Syntax | ofTacticInfo i => i.stx | ofTermInfo i => i.stx | ofCommandInfo i => i.stx | ofMacroExpansionInfo i => i.stx | ofOptionInfo i => i.stx | ofFieldInfo i => i.stx | ofCompletionInfo i => i.stx | ofCustomInfo i => i.stx | ofUserWidgetInfo i => i.stx | ofFVarAliasInfo _ => .missing | ofFieldRedeclInfo i => i.stx def Info.lctx : Info → LocalContext | Info.ofTermInfo i => i.lctx | Info.ofFieldInfo i => i.lctx | _ => LocalContext.empty def Info.pos? (i : Info) : Option String.Pos := i.stx.getPos? (canonicalOnly := true) def Info.tailPos? (i : Info) : Option String.Pos := i.stx.getTailPos? (canonicalOnly := true) def Info.range? (i : Info) : Option String.Range := i.stx.getRange? (canonicalOnly := true) def Info.contains (i : Info) (pos : String.Pos) (includeStop := false) : Bool := i.range?.any (·.contains pos includeStop) def Info.size? (i : Info) : Option String.Pos := do let pos ← i.pos? let tailPos ← i.tailPos? return tailPos - pos -- `Info` without position information are considered to have "infinite" size def Info.isSmaller (i₁ i₂ : Info) : Bool := match i₁.size?, i₂.pos? with | some sz₁, some sz₂ => sz₁ < sz₂ | some _, none => true | _, _ => false def Info.occursBefore? (i : Info) (hoverPos : String.Pos) : Option String.Pos := do let tailPos ← i.tailPos? guard (tailPos ≤ hoverPos) return hoverPos - tailPos def Info.occursInside? (i : Info) (hoverPos : String.Pos) : Option String.Pos := do let headPos ← i.pos? let tailPos ← i.tailPos? guard (headPos ≤ hoverPos && hoverPos < tailPos) return hoverPos - headPos def InfoTree.smallestInfo? (p : Info → Bool) (t : InfoTree) : Option (ContextInfo × Info) := let ts := t.deepestNodes fun ctx i _ => if p i then some (ctx, i) else none let infos := ts.map fun (ci, i) => let diff := i.tailPos?.get! - i.pos?.get! (diff, ci, i) infos.toArray.getMax? (fun a b => a.1 > b.1) |>.map fun (_, ci, i) => (ci, i) /-- Find an info node, if any, which should be shown on hover/cursor at position `hoverPos`. -/ partial def InfoTree.hoverableInfoAt? (t : InfoTree) (hoverPos : String.Pos) (includeStop := false) (omitAppFns := false) : Option (ContextInfo × Info) := Id.run do let results := t.visitM (m := Id) (postNode := fun ctx info _ results => do let mut results := results.bind (·.getD []) if omitAppFns && info.stx.isOfKind ``Parser.Term.app && info.stx[0].isIdent then results := results.filter (·.2.2.stx != info.stx[0]) unless results.isEmpty do return results -- prefer innermost results /- Remark: we skip `info` nodes associated with the `nullKind` and `withAnnotateState` because they are used by tactics (e.g., `rewrite`) to control which goal is displayed in the info views. See issue #1403 -/ if info.stx.isOfKind nullKind || info.toElabInfo?.any (·.elaborator == `Lean.Elab.Tactic.evalWithAnnotateState) then return results unless (info matches .ofFieldInfo _ | .ofOptionInfo _ || info.toElabInfo?.isSome) && info.contains hoverPos includeStop do return results let r := info.range?.get! let priority := ( -- prefer results directly *after* the hover position (only matters for `includeStop = true`; see #767) if r.stop == hoverPos then 0 else 1, -- relying on the info tree structure is _not_ sufficient for choosing the smallest surrounding node: -- `⟨x⟩` expands to an application of a canonical syntax with the span of the anonymous constructor to `x`, -- i.e. there are two info tree siblings whose spans are not disjoint and we should choose the smaller node -- surrounding the cursor Int.negOfNat (r.stop - r.start).byteIdx, -- prefer results for constants over variables (which overlap at declaration names) if info matches .ofTermInfo { expr := .fvar .., .. } then 0 else 1) [(priority, ctx, info)]) |>.getD [] -- sort results by lexicographical priority let maxPrio? := let _ := @lexOrd let _ := @leOfOrd.{0} let _ := @maxOfLe results.map (·.1) |>.maximum? let res? := results.find? (·.1 == maxPrio?) |>.map (·.2) if let some (_, i) := res? then if let .ofTermInfo ti := i then if ti.expr.isSyntheticSorry then return none return res? def Info.type? (i : Info) : MetaM (Option Expr) := match i with | Info.ofTermInfo ti => Meta.inferType ti.expr | Info.ofFieldInfo fi => Meta.inferType fi.val | _ => return none def Info.docString? (i : Info) : MetaM (Option String) := do let env ← getEnv match i with | Info.ofTermInfo ti => if let some n := ti.expr.constName? then return ← findDocString? env n | .ofFieldInfo fi => return ← findDocString? env fi.projName | .ofOptionInfo oi => if let some doc ← findDocString? env oi.declName then return doc if let some decl := (← getOptionDecls).find? oi.optionName then return decl.descr return none | _ => pure () if let some ei := i.toElabInfo? then return ← findDocString? env ei.stx.getKind <||> findDocString? env ei.elaborator return none /-- Construct a hover popup, if any, from an info node in a context.-/ def Info.fmtHover? (ci : ContextInfo) (i : Info) : IO (Option Format) := do ci.runMetaM i.lctx do let mut fmts := #[] let modFmt ← try let (termFmt, modFmt) ← fmtTermAndModule? if let some f := termFmt then fmts := fmts.push f pure modFmt catch _ => pure none if let some m ← i.docString? then fmts := fmts.push m if let some f := modFmt then fmts := fmts.push f if fmts.isEmpty then return none else return f!"\n***\n".joinSep fmts.toList where fmtModule? (decl : Name) : MetaM (Option Format) := do let some mod ← findModuleOf? decl | return none return some f!"*import {mod}*" fmtTermAndModule? : MetaM (Option Format × Option Format) := do match i with | Info.ofTermInfo ti => let e ← instantiateMVars ti.expr if e.isSort then -- Types of sorts are funny to look at in widgets, but ultimately not very helpful return (none, none) let tp ← instantiateMVars (← Meta.inferType e) let tpFmt ← Meta.ppExpr tp if e.isConst then -- Recall that `ppExpr` adds a `@` if the constant has implicit arguments, and it is quite distracting let eFmt ← withOptions (pp.fullNames.set · true |> (pp.universes.set · true)) <| PrettyPrinter.ppConst e return (some f!"```lean\n{eFmt} : {tpFmt}\n```", ← fmtModule? e.constName!) else let eFmt ← Meta.ppExpr e -- Try not to show too scary internals let showTerm := if let .fvar _ := e then if let some ldecl := (← getLCtx).findFVar? e then !ldecl.userName.hasMacroScopes else false else isAtomicFormat eFmt let fmt := if showTerm then f!"{eFmt} : {tpFmt}" else tpFmt return (some f!"```lean\n{fmt}\n```", none) | Info.ofFieldInfo fi => let tp ← Meta.inferType fi.val let tpFmt ← Meta.ppExpr tp return (some f!"```lean\n{fi.fieldName} : {tpFmt}\n```", none) | _ => return (none, none) isAtomicFormat : Format → Bool | Std.Format.text _ => true | Std.Format.group f _ => isAtomicFormat f | Std.Format.nest _ f => isAtomicFormat f | Std.Format.tag _ f => isAtomicFormat f | _ => false structure GoalsAtResult where ctxInfo : ContextInfo tacticInfo : TacticInfo useAfter : Bool /-- Whether the tactic info is further indented than the hover position. -/ indented : Bool -- for overlapping goals, only keep those of the highest reported priority priority : Nat /-- Try to retrieve `TacticInfo` for `hoverPos`. We retrieve all `TacticInfo` nodes s.t. `hoverPos` is inside the node's range plus trailing whitespace. We usually prefer the innermost such nodes so that for composite tactics such as `induction`, we show the nested proofs' states. However, if `hoverPos` is after the tactic, we prefer nodes that are not indented relative to it, meaning that e.g. at `|` in ```lean have := by exact foo | ``` we show the (final, see below) state of `have`, not `exact`. Moreover, we instruct the LSP server to use the state after tactic execution if - the hover position is after the info's start position *and* - there is no nested tactic info after the hover position (tactic combinators should decide for themselves where to show intermediate states by calling `withTacticInfoContext`) -/ partial def InfoTree.goalsAt? (text : FileMap) (t : InfoTree) (hoverPos : String.Pos) : List GoalsAtResult := let gs := t.collectNodesBottomUp fun ctx i cs gs => Id.run do if let Info.ofTacticInfo ti := i then if let (some pos, some tailPos) := (i.pos?, i.tailPos?) then let trailSize := i.stx.getTrailingSize -- show info at EOF even if strictly outside token + trail let atEOF := tailPos.byteIdx + trailSize == text.source.endPos.byteIdx -- include at least one trailing character (see also `priority` below) if pos ≤ hoverPos ∧ (hoverPos.byteIdx < tailPos.byteIdx + max 1 trailSize || atEOF) then -- overwrite bottom-up results according to "innermost" heuristics documented above if gs.isEmpty || hoverPos ≥ tailPos && gs.all (·.indented) then return [{ ctxInfo := ctx tacticInfo := ti useAfter := hoverPos > pos && !cs.any (hasNestedTactic pos tailPos) indented := (text.toPosition pos).column > (text.toPosition hoverPos).column -- use goals just before cursor as fall-back only -- thus for `(by foo)`, placing the cursor after `foo` shows its state as long -- as there is no state on `)` priority := if hoverPos.byteIdx == tailPos.byteIdx + trailSize then 0 else 1 }] return gs let maxPrio? := gs.map (·.priority) |>.maximum? gs.filter (some ·.priority == maxPrio?) where hasNestedTactic (pos tailPos) : InfoTree → Bool | InfoTree.node i@(Info.ofTacticInfo _) cs => Id.run do if let `(by $_) := i.stx then return false -- ignore term-nested proofs such as in `simp [show p by ...]` if let (some pos', some tailPos') := (i.pos?, i.tailPos?) then -- ignore preceding nested infos -- ignore nested infos of the same tactic, e.g. from expansion if tailPos' > hoverPos && (pos', tailPos') != (pos, tailPos) then return true cs.any (hasNestedTactic pos tailPos) | InfoTree.node (Info.ofMacroExpansionInfo _) cs => cs.any (hasNestedTactic pos tailPos) | _ => false partial def InfoTree.termGoalAt? (t : InfoTree) (hoverPos : String.Pos) : Option (ContextInfo × Info) := -- In the case `f a b`, where `f` is an identifier, the term goal at `f` should be the goal for the full application `f a b`. hoverableInfoAt? t hoverPos (includeStop := true) (omitAppFns := true) partial def InfoTree.hasSorry : InfoTree → IO Bool := go none where go ci? | .context ci t => go ci t | .node i cs => if let (some ci, .ofTermInfo ti) := (ci?, i) then do let expr ← ti.runMetaM ci (instantiateMVars ti.expr) return expr.hasSorry -- we assume that `cs` are subterms of `ti.expr` and -- thus do not have to be checked as well else cs.anyM (go ci?) | _ => return false end Lean.Elab
17343fc14b84bd0c33ae7f6aee2b27df144a5fcd
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/tests/lean/inst.lean
7073510e5a1239487904f89c82315e44a32b6022
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
495
lean
import Lean.Expr open Lean def tst : IO Unit := do let f := mkConst `f; let x := mkBVar 0; let y := mkBVar 1; let t := mkApp (mkApp (mkApp f x) y) x; let a := mkConst `a; let b := mkApp f (mkConst `b); let c := mkConst `c; IO.println t; IO.println (t.instantiate #[a, b]); IO.println (t.instantiateRange 0 2 #[a, b]); IO.println (t.instantiateRange 2 4 #[c, c, a, b, c]); IO.println (t.instantiateRev #[a, b]); IO.println (t.instantiate #[a]); IO.println (t.instantiate1 a); pure () #eval tst
cfec20427f722ada8c122110f1eb386887e26299
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/inlineProjInstIssue.lean
264f099c7ab2a874a9f7f9d87e74226f37a49f96
[ "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
345
lean
class NameableType (τ : Type) where name : String instance : NameableType String where name := "String" instance [inst : NameableType α] : NameableType (Array α) where name := if inst.name.contains ' ' then s!"Array ({inst.name})" else s!"Array {inst.name}" def foo : String := NameableType.name (Array String)
1a0ac95bc67b092bc08849fe9d4a04ec4ee0e942
1d265c7dd8cb3d0e1d645a19fd6157a2084c3921
/src/lessons/lesson20.lean
221d211858c47f7999a490d883e28475807a0638
[ "MIT" ]
permissive
hanzhi713/lean-proofs
de432372f220d302be09b5ca4227f8986567e4fd
4d8356a878645b9ba7cb036f87737f3f1e68ede5
refs/heads/master
1,585,580,245,658
1,553,646,623,000
1,553,646,623,000
151,342,188
0
1
null
null
null
null
UTF-8
Lean
false
false
5,864
lean
namespace mynat inductive mynat : Type | zero : mynat | succ : mynat → mynat def zero := mynat.zero def one := mynat.succ zero def two := mynat.succ one def three := mynat.succ (mynat.succ (mynat.succ zero)) #reduce one #reduce two #reduce three /- Unary functions -/ -- identity function on mynat def id_mynat (n: mynat) := n -- constant zero function def zero_mynat (n: mynat) := zero -- predecessor function def pred (n : mynat) := match n with | mynat.zero := zero | mynat.succ n' := n' end #reduce pred three #reduce pred zero /-mynat.succ (admynat.succ (add_mynat n m)mynat.succ (add_mynat n m)mynat.succ (add_mynat n m)mynat.succ (add_mynat n m)mynat.succ (add_mynat n m)mynat.succ (add_mynat n m)mynat.succ (add_mynat n m)d_mynat n m) There are two new and important concepts here. The first is a new kind of matching. The second is that we define the predecessor of zero to be zero. That's a bit odd. Look at the matching. The first pattern is familar. The second, though, it interesting. If we get here, we know n isn't zero, so it must by the successor of the next smaller mynat. Here we give that next smaller value the name n'. And of course that's the number we want to return as precessor! -/ /- Now let's see binary operations and recursive functions. To define a recursive function we have a new syntax/ -/ def add_mynat: mynat → mynat → mynat | mynat.zero m := m | (mynat.succ n') m := mynat.succ (add_mynat n' m) #reduce add_mynat three two /- Syntax notes: use explicit function type syntax. Omit any :=. Define how the function works by cases. Each case defines what the function does for the specific kinds of arguments used in the case. Omit commas between arguments All cases may be covered -/ -- It works! #reduce add_mynat one two #reduce add_mynat three three /- EXERCISES: (1) We just implemented addition as the recursive (iterated) application of the successor function. Now you are to implement multiplication as iterated addition. (2) Implement exponentiation as iterated multiplication. (3) Take this pattern one step further. What function did you implement? How would you write it in regular math notation? -/ def mul_mynat: mynat → mynat → mynat | mynat.zero m := zero | (mynat.succ n') m := add_mynat m (mul_mynat n' m) #reduce mul_mynat three two #reduce mul_mynat three three def exp_mynat: mynat → mynat → mynat | n mynat.zero := mynat.succ mynat.zero | n (mynat.succ m) := mul_mynat n (exp_mynat n m) #reduce exp_mynat three two #reduce exp_mynat three three #reduce exp_mynat zero zero example : exp_mynat three two = mul_mynat three three := rfl /- We can easily prove that for all m : ℕ, add_mynat 0 m = m, because the definition of add_mynat has a matching pattern (the first one), which explains exactly how to reduce a term with first argument zero. -/ theorem zero_left_id: ∀ m : mynat, add_mynat mynat.zero m = m := begin intro m, -- apply rfl, simp [add_mynat], end /- We just asked Lean to simplify the goal using the "simplication rules" specified by the two cases in the definition of add_mynat. The result is m = m, which Lean takes care of with an automated application of rfl. -/ /- Unfortunately, we don't have such a simplification rule when the zero is on the right! For that we need a whole new proof strategy: proof by induction. -/ theorem zero_right_id : ∀ m : mynat, add_mynat m mynat.zero = m := begin intro m, induction m with m' h, -- base case apply rfl, --simp [add_mynat], -- inductive case simp [add_mynat], assumption, end lemma add_n_succ_m : ∀ n m : mynat, add_mynat n (mynat.succ m) = mynat.succ (add_mynat n m) := begin intros n m, induction n with n h, trivial, rw add_mynat, rw add_mynat, rw h, end /- Property verification: our addition operation is commutative! -/ example : ∀ m n : mynat, add_mynat m n = add_mynat n m := begin intros m n, -- by induction on m induction m with m h, --base case: m = zero rw add_mynat, rw zero_right_id, -- inductive case: -- if true for m then true for succ m rw add_mynat, rw add_n_succ_m, -- rewrite using induction hypothesis! rw h, end /- Proof by induction is proof by case analysis on the *constructors* for values of a given type, with the huge added benefit of an induction hypothesis. So, as an example, if we show that some predicate involving a natural number, n, is true no matter what *constructor* was used to "build" n, then we've' shown the predicate to be true for *all* (∀) values of n, because there are no n other than those that can be built by the given constructors. Proving a proposition is true for all constructors is thus tantamount to showing that it is true for all values, even if there are infinitely many. Let's think about the two constructors for values of type mynat. First, there is the zero constructor. That's the "base case". Second, there's the succ constructor. From a value, n, it constructs a value, succ n. Now if we prove the following two cases, we're done: (1) the predicate in question is true when n is zero (2) if the predicate is true for any n, then it is true for succ n. The reason we're done is that there are no other possibilities for n. The set of values of an inductively defined type is defined to be exactly the set of values that can be built by using the available constructors any *finite* number of times, and that there are no other values of the given type. -/ /- EXERCISE: try this proof using cases instead of induction. Cases also does case analysis on the constructors. Why does simple case analysis fail? -/ /- EXERCISES: To Come Shortly -/ end mynat
d1d32d742765edba815692127f9857a19f5657b9
d5ecf6c46a2f605470a4a7724909dc4b9e7350e0
/tactic/norm_num.lean
fcc69761961484e3e6ea2b4db8e4cfc341dd31b7
[ "Apache-2.0" ]
permissive
MonoidMusician/mathlib
41f79df478987a636b735c338396813d2e8e44c4
72234ef1a050eea3a2197c23aeb345fc13c08ff3
refs/heads/master
1,583,672,205,771
1,522,892,143,000
1,522,892,143,000
128,144,032
0
0
Apache-2.0
1,522,892,144,000
1,522,890,892,000
Lean
UTF-8
Lean
false
false
11,936
lean
/- Copyright (c) 2017 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Mario Carneiro Evaluating arithmetic expressions including *, +, -, ^, ≤ -/ import algebra.group_power data.rat tactic.interactive universes u v w open tactic namespace expr protected meta def to_pos_rat : expr → option ℚ | `(%%e₁ / %%e₂) := do m ← e₁.to_nat, n ← e₂.to_nat, some (rat.mk m n) | e := do n ← e.to_nat, return (rat.of_int n) protected meta def to_rat : expr → option ℚ | `(has_neg.neg %%e) := do q ← e.to_pos_rat, some (-q) | e := e.to_pos_rat protected meta def of_rat (α : expr) : ℚ → tactic expr | ⟨(n:ℕ), d, h, c⟩ := do e₁ ← expr.of_nat α n, if d = 1 then return e₁ else do e₂ ← expr.of_nat α d, tactic.mk_app ``has_div.div [e₁, e₂] | ⟨-[1+n], d, h, c⟩ := do e₁ ← expr.of_nat α (n+1), e ← (if d = 1 then return e₁ else do e₂ ← expr.of_nat α d, tactic.mk_app ``has_div.div [e₁, e₂]), tactic.mk_app ``has_neg.neg [e] end expr namespace norm_num variable {α : Type u} theorem bit0_zero [add_group α] : bit0 (0 : α) = 0 := add_zero _ theorem bit1_zero [add_group α] [has_one α] : bit1 (0 : α) = 1 := by rw [bit1, bit0_zero, zero_add] lemma pow_bit0_helper [monoid α] (a t : α) (b : ℕ) (h : a ^ b = t) : a ^ bit0 b = t * t := by simp [pow_bit0, h] lemma pow_bit1_helper [monoid α] (a t : α) (b : ℕ) (h : a ^ b = t) : a ^ bit1 b = t * t * a := by simp [pow_bit1, h] lemma lt_add_of_pos_helper [ordered_cancel_comm_monoid α] (a b c : α) (h : a + b = c) (h₂ : 0 < b) : a < c := h ▸ (lt_add_iff_pos_right _).2 h₂ lemma nat_div_helper (a b q r : ℕ) (h : r + q * b = a) (h₂ : r < b) : a / b = q := by rw [← h, nat.add_mul_div_right _ _ (lt_of_le_of_lt (nat.zero_le _) h₂), nat.div_eq_of_lt h₂, zero_add] lemma int_div_helper (a b q r : ℤ) (h : r + q * b = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a / b = q := by rw [← h, int.add_mul_div_right _ _ (ne_of_gt (lt_of_le_of_lt h₁ h₂)), int.div_eq_zero_of_lt h₁ h₂, zero_add] lemma nat_mod_helper (a b q r : ℕ) (h : r + q * b = a) (h₂ : r < b) : a % b = r := by rw [← h, nat.add_mul_mod_self_right, nat.mod_eq_of_lt h₂] lemma int_mod_helper (a b q r : ℤ) (h : r + q * b = a) (h₁ : 0 ≤ r) (h₂ : r < b) : a % b = r := by rw [← h, int.add_mul_mod_self, int.mod_eq_of_lt h₁ h₂] meta def eval_pow (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr) | `(monoid.pow %%e₁ 0) := do p ← mk_app ``pow_zero [e₁], a ← infer_type e₁, o ← mk_app ``has_one.one [a], return (o, p) | `(monoid.pow %%e₁ 1) := do p ← mk_app ``pow_one [e₁], return (e₁, p) | `(monoid.pow %%e₁ (bit0 %%e₂)) := do e ← mk_app ``monoid.pow [e₁, e₂], (e', p) ← simp e, p' ← mk_app ``norm_num.pow_bit0_helper [e₁, e', e₂, p], e'' ← to_expr ``(%%e' * %%e'), return (e'', p') | `(monoid.pow %%e₁ (bit1 %%e₂)) := do e ← mk_app ``monoid.pow [e₁, e₂], (e', p) ← simp e, p' ← mk_app ``norm_num.pow_bit1_helper [e₁, e', e₂, p], e'' ← to_expr ``(%%e' * %%e' * %%e₁), return (e'', p') | `(nat.pow %%e₁ %%e₂) := do p₁ ← mk_app ``nat.pow_eq_pow [e₁, e₂], e ← mk_app ``monoid.pow [e₁, e₂], (e', p₂) ← simp e, p ← mk_eq_trans p₁ p₂, return (e', p) | _ := failed meta def prove_pos : instance_cache → expr → tactic (instance_cache × expr) | c `(has_one.one _) := do (c, p) ← c.mk_app ``zero_lt_one [], return (c, p) | c `(bit0 %%e) := do (c, p) ← prove_pos c e, (c, p) ← c.mk_app ``bit0_pos [e, p], return (c, p) | c `(bit1 %%e) := do (c, p) ← prove_pos c e, (c, p) ← c.mk_app ``bit1_pos' [e, p], return (c, p) | c `(%%e₁ / %%e₂) := do (c, p₁) ← prove_pos c e₁, (c, p₂) ← prove_pos c e₂, (c, p) ← c.mk_app ``div_pos_of_pos_of_pos [e₁, e₂, p₁, p₂], return (c, p) | c e := failed meta def prove_lt (simp : expr → tactic (expr × expr)) : instance_cache → expr → expr → tactic (instance_cache × expr) | c `(- %%e₁) `(- %%e₂) := do (c, p) ← prove_lt c e₁ e₂, (c, p) ← c.mk_app ``neg_lt_neg [e₁, e₂, p], return (c, p) | c `(- %%e₁) `(has_zero.zero _) := do (c, p) ← prove_pos c e₁, (c, p) ← c.mk_app ``neg_neg_of_pos [e₁, p], return (c, p) | c `(- %%e₁) e₂ := do (c, p₁) ← prove_pos c e₁, (c, me₁) ← c.mk_app ``has_neg.neg [e₁], (c, p₁) ← c.mk_app ``neg_neg_of_pos [e₁, p₁], (c, p₂) ← prove_pos c e₂, (c, z) ← c.mk_app ``has_zero.zero [], (c, p) ← c.mk_app ``lt_trans [me₁, z, e₂, p₁, p₂], return (c, p) | c `(has_zero.zero _) e₂ := prove_pos c e₂ | c e₁ e₂ := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, d ← expr.of_rat c.α (n₂ - n₁), (c, e₃) ← c.mk_app ``has_add.add [e₁, d], (e₂', p) ← norm_num e₃, guard (e₂' =ₐ e₂), (c, p') ← prove_pos c d, (c, p) ← c.mk_app ``norm_num.lt_add_of_pos_helper [e₁, d, e₂, p, p'], return (c, p) private meta def true_intro (p : expr) : tactic (expr × expr) := prod.mk <$> mk_const `true <*> mk_app ``eq_true_intro [p] private meta def false_intro (p : expr) : tactic (expr × expr) := prod.mk <$> mk_const `false <*> mk_app ``eq_false_intro [p] meta def eval_ineq (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr) | `(%%e₁ < %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ < n₂ then do (_, p) ← prove_lt simp c e₁ e₂, true_intro p else do (c, p) ← if n₁ = n₂ then c.mk_app ``lt_irrefl [e₁] else (do (c, p') ← prove_lt simp c e₂ e₁, c.mk_app ``not_lt_of_gt [e₁, e₂, p']), false_intro p | `(%%e₁ ≤ %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ ≤ n₂ then do (c, p) ← if n₁ = n₂ then c.mk_app ``le_refl [e₁] else (do (c, p') ← prove_lt simp c e₁ e₂, c.mk_app ``le_of_lt [e₁, e₂, p']), true_intro p else do (c, p) ← prove_lt simp c e₂ e₁, (c, p) ← c.mk_app ``not_le_of_gt [e₁, e₂, p], false_intro p | `(%%e₁ = %%e₂) := do n₁ ← e₁.to_rat, n₂ ← e₂.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ < n₂ then do (c, p) ← prove_lt simp c e₁ e₂, (c, p) ← c.mk_app ``ne_of_lt [e₁, e₂, p], false_intro p else if n₂ < n₁ then do (c, p) ← prove_lt simp c e₂ e₁, (c, p) ← c.mk_app ``ne_of_gt [e₁, e₂, p], false_intro p else mk_eq_refl e₁ >>= true_intro | `(%%e₁ > %%e₂) := mk_app ``has_lt.lt [e₂, e₁] >>= simp | `(%%e₁ ≥ %%e₂) := mk_app ``has_le.le [e₂, e₁] >>= simp | `(%%e₁ ≠ %%e₂) := do e ← mk_app ``eq [e₁, e₂], mk_app ``not [e] >>= simp | _ := failed meta def eval_div_ext (simp : expr → tactic (expr × expr)) : expr → tactic (expr × expr) | `(has_inv.inv %%e) := do c ← infer_type e >>= mk_instance_cache, (c, p₁) ← c.mk_app ``inv_eq_one_div [e], (c, o) ← c.mk_app ``has_one.one [], (c, e') ← c.mk_app ``has_div.div [o, e], (do (e'', p₂) ← simp e', p ← mk_eq_trans p₁ p₂, return (e'', p)) <|> return (e', p₁) | `(%%e₁ / %%e₂) := do α ← infer_type e₁, c ← mk_instance_cache α, match α with | `(nat) := do n₁ ← e₁.to_nat, n₂ ← e₂.to_nat, q ← expr.of_nat α (n₁ / n₂), r ← expr.of_nat α (n₁ % n₂), (c, e₃) ← c.mk_app ``has_mul.mul [q, e₂], (c, e₃) ← c.mk_app ``has_add.add [r, e₃], (e₁', p) ← norm_num e₃, guard (e₁' =ₐ e₁), (c, p') ← prove_lt simp c r e₂, p ← mk_app ``norm_num.nat_div_helper [e₁, e₂, q, r, p, p'], return (q, p) | `(int) := match e₂ with | `(- %%e₂') := do (c, p₁) ← c.mk_app ``int.div_neg [e₁, e₂'], (c, e) ← c.mk_app ``has_div.div [e₁, e₂'], (c, e) ← c.mk_app ``has_neg.neg [e], (e', p₂) ← simp e, p ← mk_eq_trans p₁ p₂, return (e', p) | _ := do n₁ ← e₁.to_int, n₂ ← e₂.to_int, q ← expr.of_rat α $ rat.of_int (n₁ / n₂), r ← expr.of_rat α $ rat.of_int (n₁ % n₂), (c, e₃) ← c.mk_app ``has_mul.mul [q, e₂], (c, e₃) ← c.mk_app ``has_add.add [r, e₃], (e₁', p) ← norm_num e₃, guard (e₁' =ₐ e₁), (c, r0) ← c.mk_app ``has_zero.zero [], (c, r0) ← c.mk_app ``has_le.le [r0, r], (_, p₁) ← simp r0, p₁ ← mk_app ``of_eq_true [p₁], (c, p₂) ← prove_lt simp c r e₂, p ← mk_app ``norm_num.int_div_helper [e₁, e₂, q, r, p, p₁, p₂], return (q, p) end | _ := failed end | `(%%e₁ % %%e₂) := do α ← infer_type e₁, c ← mk_instance_cache α, match α with | `(nat) := do n₁ ← e₁.to_nat, n₂ ← e₂.to_nat, q ← expr.of_nat α (n₁ / n₂), r ← expr.of_nat α (n₁ % n₂), (c, e₃) ← c.mk_app ``has_mul.mul [q, e₂], (c, e₃) ← c.mk_app ``has_add.add [r, e₃], (e₁', p) ← norm_num e₃, guard (e₁' =ₐ e₁), (c, p') ← prove_lt simp c r e₂, p ← mk_app ``norm_num.nat_mod_helper [e₁, e₂, q, r, p, p'], return (r, p) | `(int) := match e₂ with | `(- %%e₂') := do (c, p₁) ← c.mk_app ``int.mod_neg [e₁, e₂'], (c, e) ← c.mk_app ``has_mod.mod [e₁, e₂'], (e', p₂) ← simp e, p ← mk_eq_trans p₁ p₂, return (e', p) | _ := do n₁ ← e₁.to_int, n₂ ← e₂.to_int, q ← expr.of_rat α $ rat.of_int (n₁ / n₂), r ← expr.of_rat α $ rat.of_int (n₁ % n₂), (c, e₃) ← c.mk_app ``has_mul.mul [q, e₂], (c, e₃) ← c.mk_app ``has_add.add [r, e₃], (e₁', p) ← norm_num e₃, guard (e₁' =ₐ e₁), (c, r0) ← c.mk_app ``has_zero.zero [], (c, r0) ← c.mk_app ``has_le.le [r0, r], (_, p₁) ← simp r0, p₁ ← mk_app ``of_eq_true [p₁], (c, p₂) ← prove_lt simp c r e₂, p ← mk_app ``norm_num.int_mod_helper [e₁, e₂, q, r, p, p₁, p₂], return (r, p) end | _ := failed end | _ := failed meta def derive1 (simp : expr → tactic (expr × expr)) (e : expr) : tactic (expr × expr) := norm_num e <|> eval_div_ext simp e <|> eval_pow simp e <|> eval_ineq simp e meta def derive : expr → tactic (expr × expr) | e := do (_, e', pr) ← ext_simplify_core () {} simp_lemmas.mk (λ _, failed) (λ _ _ _ _ _, failed) (λ _ _ _ _ e, do (new_e, pr) ← derive1 derive e, guard (¬ new_e =ₐ e), return ((), new_e, some pr, tt)) `eq e, return (e', pr) end norm_num namespace tactic.interactive open norm_num interactive interactive.types /-- Basic version of `norm_num` that does not call `simp`. -/ meta def norm_num1 (loc : parse location) : tactic unit := do ns ← loc.get_locals, tt ← tactic.replace_at derive ns loc.include_goal | fail "norm_num failed to simplify", when loc.include_goal $ try tactic.triv, when (¬ ns.empty) $ try tactic.contradiction /-- Normalize numerical expressions. Supports the operations `+` `-` `*` `/` `^` `<` `≤` over ordered fields (or other appropriate classes), as well as `-` `/` `%` over `ℤ` and `ℕ`. -/ meta def norm_num (loc : parse location) : tactic unit := let t := orelse' (norm_num1 loc) $ simp_core {} failed ff [] [] loc in t >> repeat t meta def apply_normed (x : parse texpr) : tactic unit := do x₁ ← to_expr x, (x₂,_) ← derive x₁, tactic.exact x₂ end tactic.interactive
c4ee5fcae9187f88165eb3d454b38bd24a341335
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/topology/dense_embedding.lean
f252c4035448443416d4d5be508914097ee79842
[ "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
13,576
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import topology.separation /-! # Dense embeddings This file defines three properties of functions: * `dense_range f` means `f` has dense image; * `dense_inducing i` means `i` is also `inducing`; * `dense_embedding e` means `e` is also an `embedding`. The main theorem `continuous_extend` gives a criterion for a function `f : X → Z` to a regular (T₃) space Z to extend along a dense embedding `i : X → Y` to a continuous function `g : Y → Z`. Actually `i` only has to be `dense_inducing` (not necessarily injective). -/ noncomputable theory open set filter open_locale classical topological_space filter variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} section dense_range variables [topological_space β] [topological_space γ] (f : α → β) (g : β → γ) /-- `f : α → β` has dense range if its range (image) is a dense subset of β. -/ def dense_range := ∀ x, x ∈ closure (range f) variables {f} lemma dense_range_iff_closure_range : dense_range f ↔ closure (range f) = univ := eq_univ_iff_forall.symm lemma dense_range.closure_range (h : dense_range f) : closure (range f) = univ := eq_univ_iff_forall.mpr h lemma dense_range.comp (hg : dense_range g) (hf : dense_range f) (cg : continuous g) : dense_range (g ∘ f) := begin have : g '' (closure $ range f) ⊆ closure (g '' range f), from image_closure_subset_closure_image cg, have : closure (g '' closure (range f)) ⊆ closure (g '' range f), by simpa [closure_closure] using (closure_mono this), intro c, rw range_comp, apply this, rw [hf.closure_range, image_univ], exact hg c end /-- If `f : α → β` has dense range and `β` contains some element, then `α` must too. -/ def dense_range.inhabited (df : dense_range f) (b : β) : inhabited α := ⟨classical.choice $ by simpa only [univ_inter, range_nonempty_iff_nonempty] using mem_closure_iff.1 (df b) _ is_open_univ trivial⟩ lemma dense_range.nonempty (hf : dense_range f) : nonempty α ↔ nonempty β := ⟨nonempty.map f, λ ⟨b⟩, @nonempty_of_inhabited _ (hf.inhabited b)⟩ lemma dense_range.prod {ι : Type*} {κ : Type*} {f : ι → β} {g : κ → γ} (hf : dense_range f) (hg : dense_range g) : dense_range (λ p : ι × κ, (f p.1, g p.2)) := have closure (range $ λ p : ι×κ, (f p.1, g p.2)) = set.prod (closure $ range f) (closure $ range g), by rw [←closure_prod_eq, prod_range_range_eq], assume ⟨b, d⟩, this.symm ▸ mem_prod.2 ⟨hf _, hg _⟩ end dense_range /-- `i : α → β` is "dense inducing" if it has dense range and the topology on `α` is the one induced by `i` from the topology on `β`. -/ structure dense_inducing [topological_space α] [topological_space β] (i : α → β) extends inducing i : Prop := (dense : dense_range i) namespace dense_inducing variables [topological_space α] [topological_space β] variables {i : α → β} (di : dense_inducing i) lemma nhds_eq_comap (di : dense_inducing i) : ∀ a : α, 𝓝 a = comap i (𝓝 $ i a) := di.to_inducing.nhds_eq_comap protected lemma continuous (di : dense_inducing i) : continuous i := di.to_inducing.continuous lemma closure_range : closure (range i) = univ := di.dense.closure_range lemma self_sub_closure_image_preimage_of_open {s : set β} (di : dense_inducing i) : is_open s → s ⊆ closure (i '' (i ⁻¹' s)) := begin intros s_op b b_in_s, rw [image_preimage_eq_inter_range, mem_closure_iff], intros U U_op b_in, rw ←inter_assoc, exact (dense_iff_inter_open.1 di.closure_range) _ (is_open_inter U_op s_op) ⟨b, b_in, b_in_s⟩ end lemma closure_image_nhds_of_nhds {s : set α} {a : α} (di : dense_inducing i) : s ∈ 𝓝 a → closure (i '' s) ∈ 𝓝 (i a) := begin rw [di.nhds_eq_comap a, mem_comap_sets], intro h, rcases h with ⟨t, t_nhd, sub⟩, rw mem_nhds_sets_iff at t_nhd, rcases t_nhd with ⟨U, U_sub, ⟨U_op, e_a_in_U⟩⟩, have := calc i ⁻¹' U ⊆ i⁻¹' t : preimage_mono U_sub ... ⊆ s : sub, have := calc U ⊆ closure (i '' (i ⁻¹' U)) : self_sub_closure_image_preimage_of_open di U_op ... ⊆ closure (i '' s) : closure_mono (image_subset i this), have U_nhd : U ∈ 𝓝 (i a) := mem_nhds_sets U_op e_a_in_U, exact (𝓝 (i a)).sets_of_superset U_nhd this end /-- The product of two dense inducings is a dense inducing -/ protected lemma prod [topological_space γ] [topological_space δ] {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_inducing e₁) (de₂ : dense_inducing e₂) : dense_inducing (λ(p : α × γ), (e₁ p.1, e₂ p.2)) := { induced := (de₁.to_inducing.prod_mk de₂.to_inducing).induced, dense := de₁.dense.prod de₂.dense } variables [topological_space δ] {f : γ → α} {g : γ → δ} {h : δ → β} /-- γ -f→ α g↓ ↓e δ -h→ β -/ lemma tendsto_comap_nhds_nhds {d : δ} {a : α} (di : dense_inducing i) (H : tendsto h (𝓝 d) (𝓝 (i a))) (comm : h ∘ g = i ∘ f) : tendsto f (comap g (𝓝 d)) (𝓝 a) := begin have lim1 : map g (comap g (𝓝 d)) ≤ 𝓝 d := map_comap_le, replace lim1 : map h (map g (comap g (𝓝 d))) ≤ map h (𝓝 d) := map_mono lim1, rw [filter.map_map, comm, ← filter.map_map, map_le_iff_le_comap] at lim1, have lim2 : comap i (map h (𝓝 d)) ≤ comap i (𝓝 (i a)) := comap_mono H, rw ← di.nhds_eq_comap at lim2, exact le_trans lim1 lim2, end protected lemma nhds_inf_ne_bot (di : dense_inducing i) {b : β} : 𝓝 b ⊓ 𝓟 (range i) ≠ ⊥ := begin convert di.dense b, simp [closure_eq_cluster_pts, cluster_pt] end lemma comap_nhds_ne_bot (di : dense_inducing i) {b : β} : comap i (𝓝 b) ≠ ⊥ := comap_ne_bot $ λ s hs, let ⟨_, ⟨ha, a, rfl⟩⟩ := mem_closure_iff_nhds.1 (di.dense b) s hs in ⟨a, ha⟩ variables [topological_space γ] /-- If `i : α → β` is a dense inducing, then any function `f : α → γ` "extends" to a function `g = extend di f : β → γ`. If `γ` is Hausdorff and `f` has a continuous extension, then `g` is the unique such extension. In general, `g` might not be continuous or even extend `f`. -/ def extend (di : dense_inducing i) (f : α → γ) (b : β) : γ := @@lim _ ⟨f (di.dense.inhabited b).default⟩ (comap i (𝓝 b)) f lemma extend_eq [t2_space γ] {b : β} {c : γ} {f : α → γ} (hf : tendsto f (comap i (𝓝 b)) (𝓝 c)) : di.extend f b = c := hf.lim_eq di.comap_nhds_ne_bot lemma extend_e_eq [t2_space γ] {f : α → γ} (a : α) (hf : continuous_at f a) : di.extend f (i a) = f a := extend_eq _ $ di.nhds_eq_comap a ▸ hf lemma extend_eq_of_cont [t2_space γ] {f : α → γ} (hf : continuous f) (a : α) : di.extend f (i a) = f a := di.extend_e_eq a (continuous_iff_continuous_at.1 hf a) lemma continuous_at_extend [regular_space γ] {b : β} {f : α → γ} (di : dense_inducing i) (hf : ∀ᶠ x in 𝓝 b, ∃c, tendsto f (comap i $ 𝓝 x) (𝓝 c)) : continuous_at (di.extend f) b := begin set φ := di.extend f, suffices : ∀ V' ∈ 𝓝 (φ b), is_closed V' → φ ⁻¹' V' ∈ 𝓝 b, by simpa [continuous_at, (closed_nhds_basis _).tendsto_right_iff], intros V' V'_in V'_closed, set V₁ := {x | tendsto f (comap i $ 𝓝 x) (𝓝 $ φ x)}, have V₁_in : V₁ ∈ 𝓝 b, { filter_upwards [hf], rintros x ⟨c, hc⟩, change tendsto f (comap i (𝓝 x)) (𝓝 (φ x)), convert hc, exact di.extend_eq hc }, obtain ⟨V₂, V₂_in, V₂_op, hV₂⟩ : ∃ V₂ ∈ 𝓝 b, is_open V₂ ∧ ∀ x ∈ i ⁻¹' V₂, f x ∈ V', { simpa [and_assoc] using ((nhds_basis_opens' b).comap i).tendsto_left_iff.mp (mem_of_nhds V₁_in : b ∈ V₁) V' V'_in }, suffices : ∀ x ∈ V₁ ∩ V₂, φ x ∈ V', { filter_upwards [inter_mem_sets V₁_in V₂_in], exact this }, rintros x ⟨x_in₁, x_in₂⟩, have hV₂x : V₂ ∈ 𝓝 x := mem_nhds_sets V₂_op x_in₂, apply mem_of_closed_of_tendsto (comap_nhds_ne_bot di) x_in₁ V'_closed, use V₂, tauto, end lemma continuous_extend [regular_space γ] {f : α → γ} (di : dense_inducing i) (hf : ∀b, ∃c, tendsto f (comap i (𝓝 b)) (𝓝 c)) : continuous (di.extend f) := continuous_iff_continuous_at.mpr $ assume b, di.continuous_at_extend $ univ_mem_sets' hf lemma mk' (i : α → β) (c : continuous i) (dense : ∀x, x ∈ closure (range i)) (H : ∀ (a:α) s ∈ 𝓝 a, ∃t ∈ 𝓝 (i a), ∀ b, i b ∈ t → b ∈ s) : dense_inducing i := { induced := (induced_iff_nhds_eq i).2 $ λ a, le_antisymm (tendsto_iff_comap.1 $ c.tendsto _) (by simpa [le_def] using H a), dense := dense } end dense_inducing /-- A dense embedding is an embedding with dense image. -/ structure dense_embedding [topological_space α] [topological_space β] (e : α → β) extends dense_inducing e : Prop := (inj : function.injective e) theorem dense_embedding.mk' [topological_space α] [topological_space β] (e : α → β) (c : continuous e) (dense : ∀x, x ∈ closure (range e)) (inj : function.injective e) (H : ∀ (a:α) s ∈ 𝓝 a, ∃t ∈ 𝓝 (e a), ∀ b, e b ∈ t → b ∈ s) : dense_embedding e := { inj := inj, ..dense_inducing.mk' e c dense H} namespace dense_embedding variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] variables {e : α → β} (de : dense_embedding e) lemma inj_iff {x y} : e x = e y ↔ x = y := de.inj.eq_iff lemma to_embedding : embedding e := { induced := de.induced, inj := de.inj } /-- The product of two dense embeddings is a dense embedding -/ protected lemma prod {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁) (de₂ : dense_embedding e₂) : dense_embedding (λ(p : α × γ), (e₁ p.1, e₂ p.2)) := { inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨de₁.inj h₁, de₂.inj h₂⟩, ..dense_inducing.prod de₁.to_dense_inducing de₂.to_dense_inducing } /-- The dense embedding of a subtype inside its closure. -/ def subtype_emb {α : Type*} (p : α → Prop) (e : α → β) (x : {x // p x}) : {x // x ∈ closure (e '' {x | p x})} := ⟨e x, subset_closure $ mem_image_of_mem e x.prop⟩ protected lemma subtype (p : α → Prop) : dense_embedding (subtype_emb p e) := { dense_embedding . dense := assume ⟨x, hx⟩, closure_subtype.mpr $ have (λ (x : {x // p x}), e x) = e ∘ coe, from rfl, begin rw ← image_univ, simp [(image_comp _ _ _).symm, (∘), subtype_emb, -image_univ], rw [this, image_comp, subtype.coe_image], simp, assumption end, inj := assume ⟨x, hx⟩ ⟨y, hy⟩ h, subtype.eq $ de.inj $ @@congr_arg subtype.val h, induced := (induced_iff_nhds_eq _).2 (assume ⟨x, hx⟩, by simp [subtype_emb, nhds_subtype_eq_comap, de.to_inducing.nhds_eq_comap, comap_comap, (∘)]) } end dense_embedding lemma is_closed_property [topological_space β] {e : α → β} {p : β → Prop} (he : dense_range e) (hp : is_closed {x | p x}) (h : ∀a, p (e a)) : ∀b, p b := have univ ⊆ {b | p b}, from calc univ = closure (range e) : he.closure_range.symm ... ⊆ closure {b | p b} : closure_mono $ range_subset_iff.mpr h ... = _ : hp.closure_eq, assume b, this trivial lemma is_closed_property2 [topological_space β] {e : α → β} {p : β → β → Prop} (he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) : ∀b₁ b₂, p b₁ b₂ := have ∀q:β×β, p q.1 q.2, from is_closed_property (he.prod he) hp $ λ _, h _ _, assume b₁ b₂, this ⟨b₁, b₂⟩ lemma is_closed_property3 [topological_space β] {e : α → β} {p : β → β → β → Prop} (he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2}) (h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) : ∀b₁ b₂ b₃, p b₁ b₂ b₃ := have ∀q:β×β×β, p q.1 q.2.1 q.2.2, from is_closed_property (he.prod $ he.prod he) hp $ λ _, h _ _ _, assume b₁ b₂ b₃, this ⟨b₁, b₂, b₃⟩ @[elab_as_eliminator] lemma dense_range.induction_on [topological_space β] {e : α → β} (he : dense_range e) {p : β → Prop} (b₀ : β) (hp : is_closed {b | p b}) (ih : ∀a:α, p $ e a) : p b₀ := is_closed_property he hp ih b₀ @[elab_as_eliminator] lemma dense_range.induction_on₂ [topological_space β] {e : α → β} {p : β → β → Prop} (he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) (b₁ b₂ : β) : p b₁ b₂ := is_closed_property2 he hp h _ _ @[elab_as_eliminator] lemma dense_range.induction_on₃ [topological_space β] {e : α → β} {p : β → β → β → Prop} (he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2}) (h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) (b₁ b₂ b₃ : β) : p b₁ b₂ b₃ := is_closed_property3 he hp h _ _ _ section variables [topological_space β] [topological_space γ] [t2_space γ] variables {f : α → β} /-- Two continuous functions to a t2-space that agree on the dense range of a function are equal. -/ lemma dense_range.equalizer (hfd : dense_range f) {g h : β → γ} (hg : continuous g) (hh : continuous h) (H : g ∘ f = h ∘ f) : g = h := funext $ λ y, hfd.induction_on y (is_closed_eq hg hh) $ congr_fun H end
a37fb972db7ce8a0d7783de7f78b7ef0eb1dbd39
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/zeta_values.lean
296f70123d5593634f6f3b1777a2648ca255b992
[ "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
16,722
lean
/- Copyright (c) 2022 David Loeffler. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Loeffler -/ import number_theory.bernoulli_polynomials import measure_theory.integral.interval_integral import analysis.fourier.add_circle import analysis.p_series /-! # Critical values of the Riemann zeta function > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove formulae for the critical values of `ζ(s)`, and more generally of Hurwitz zeta functions, in terms of Bernoulli polynomials. ## Main results: * `has_sum_zeta_nat`: the final formula for zeta values, $$\zeta(2k) = \frac{(-1)^{(k + 1)} 2 ^ {2k - 1} \pi^{2k} B_{2 k}}{(2 k)!}.$$ * `has_sum_zeta_two` and `has_sum_zeta_four`: special cases given explicitly. * `has_sum_one_div_nat_pow_mul_cos`: a formula for the sum `∑ (n : ℕ), cos (2 π i n x) / n ^ k` as an explicit multiple of `Bₖ(x)`, for any `x ∈ [0, 1]` and `k ≥ 2` even. * `has_sum_one_div_nat_pow_mul_sin`: a formula for the sum `∑ (n : ℕ), sin (2 π i n x) / n ^ k` as an explicit multiple of `Bₖ(x)`, for any `x ∈ [0, 1]` and `k ≥ 3` odd. -/ noncomputable theory open_locale nat real interval open complex measure_theory set interval_integral local notation `𝕌` := unit_add_circle local attribute [instance] real.fact_zero_lt_one section bernoulli_fun_props /-! Simple properties of the Bernoulli polynomial, as a function `ℝ → ℝ`. -/ /-- The function `x ↦ Bₖ(x) : ℝ → ℝ`. -/ def bernoulli_fun (k : ℕ) (x : ℝ) : ℝ := (polynomial.map (algebra_map ℚ ℝ) (polynomial.bernoulli k)).eval x lemma bernoulli_fun_eval_zero (k : ℕ) : bernoulli_fun k 0 = bernoulli k := by rw [bernoulli_fun, polynomial.eval_zero_map, polynomial.bernoulli_eval_zero, eq_rat_cast] lemma bernoulli_fun_endpoints_eq_of_ne_one {k : ℕ} (hk : k ≠ 1) : bernoulli_fun k 1 = bernoulli_fun k 0 := by rw [bernoulli_fun_eval_zero, bernoulli_fun, polynomial.eval_one_map, polynomial.bernoulli_eval_one, bernoulli_eq_bernoulli'_of_ne_one hk, eq_rat_cast] lemma bernoulli_fun_eval_one (k : ℕ) : bernoulli_fun k 1 = bernoulli_fun k 0 + ite (k = 1) 1 0 := begin rw [bernoulli_fun, bernoulli_fun_eval_zero, polynomial.eval_one_map, polynomial.bernoulli_eval_one], split_ifs, { rw [h, bernoulli_one, bernoulli'_one, eq_rat_cast], push_cast, ring }, { rw [bernoulli_eq_bernoulli'_of_ne_one h, add_zero, eq_rat_cast], } end lemma has_deriv_at_bernoulli_fun (k : ℕ) (x : ℝ) : has_deriv_at (bernoulli_fun k) (k * bernoulli_fun (k - 1) x) x := begin convert ((polynomial.bernoulli k).map $ algebra_map ℚ ℝ).has_deriv_at x using 1, simp only [bernoulli_fun, polynomial.derivative_map, polynomial.derivative_bernoulli k, polynomial.map_mul, polynomial.map_nat_cast, polynomial.eval_mul, polynomial.eval_nat_cast], end lemma antideriv_bernoulli_fun (k : ℕ) (x : ℝ) : has_deriv_at (λ x, (bernoulli_fun (k + 1) x) / (k + 1)) (bernoulli_fun k x) x := begin convert (has_deriv_at_bernoulli_fun (k + 1) x).div_const _, field_simp [nat.cast_add_one_ne_zero k], ring, end lemma integral_bernoulli_fun_eq_zero {k : ℕ} (hk : k ≠ 0) : ∫ (x : ℝ) in 0..1, bernoulli_fun k x = 0 := begin rw integral_eq_sub_of_has_deriv_at (λ x hx, antideriv_bernoulli_fun k x) ((polynomial.continuous _).interval_integrable _ _), dsimp only, rw bernoulli_fun_eval_one, split_ifs, { exfalso, exact hk (nat.succ_inj'.mp h), }, { simp }, end end bernoulli_fun_props section bernoulli_fourier_coeffs /-! Compute the Fourier coefficients of the Bernoulli functions via integration by parts. -/ /-- The `n`-th Fourier coefficient of the `k`-th Bernoulli function on the interval `[0, 1]`. -/ def bernoulli_fourier_coeff (k : ℕ) (n : ℤ) : ℂ := fourier_coeff_on zero_lt_one (λ x, bernoulli_fun k x) n /-- Recurrence relation (in `k`) for the `n`-th Fourier coefficient of `Bₖ`. -/ lemma bernoulli_fourier_coeff_recurrence (k : ℕ) {n : ℤ} (hn : n ≠ 0) : bernoulli_fourier_coeff k n = 1 / ((-2) * π * I * n) * (ite (k = 1) 1 0 - k * bernoulli_fourier_coeff (k - 1) n) := begin unfold bernoulli_fourier_coeff, rw [fourier_coeff_on_of_has_deriv_at zero_lt_one hn (λ x hx, (has_deriv_at_bernoulli_fun k x).of_real_comp) ((continuous_of_real.comp $ continuous_const.mul $ polynomial.continuous _).interval_integrable _ _)], dsimp only, simp_rw [of_real_one, of_real_zero, sub_zero, one_mul], rw [quotient_add_group.coe_zero, fourier_eval_zero, one_mul, ←of_real_sub, bernoulli_fun_eval_one, add_sub_cancel'], congr' 2, { split_ifs, all_goals { simp only [of_real_one, of_real_zero, one_mul]}, }, { simp_rw [of_real_mul, of_real_nat_cast, fourier_coeff_on.const_mul] }, end /-- The Fourier coefficients of `B₀(x) = 1`. -/ lemma bernoulli_zero_fourier_coeff {n : ℤ} (hn : n ≠ 0) : bernoulli_fourier_coeff 0 n = 0 := by simpa using bernoulli_fourier_coeff_recurrence 0 hn /-- The `0`-th Fourier coefficient of `Bₖ(x)`. -/ lemma bernoulli_fourier_coeff_zero {k : ℕ} (hk : k ≠ 0) : bernoulli_fourier_coeff k 0 = 0 := by simp_rw [bernoulli_fourier_coeff, fourier_coeff_on_eq_integral, neg_zero, fourier_zero, sub_zero, div_one, one_smul, interval_integral.integral_of_real, integral_bernoulli_fun_eq_zero hk, of_real_zero] lemma bernoulli_fourier_coeff_eq {k : ℕ} (hk : k ≠ 0) (n : ℤ) : bernoulli_fourier_coeff k n = - k! / (2 * π * I * n) ^ k := begin rcases eq_or_ne n 0 with rfl|hn, { rw [bernoulli_fourier_coeff_zero hk, int.cast_zero, mul_zero, zero_pow' _ hk, div_zero] }, refine nat.le_induction _ (λ k hk h'k, _) k (nat.one_le_iff_ne_zero.mpr hk), { rw bernoulli_fourier_coeff_recurrence 1 hn, simp only [nat.cast_one, tsub_self, neg_mul, one_mul, eq_self_iff_true, if_true, nat.factorial_one, pow_one, inv_I, mul_neg], rw [bernoulli_zero_fourier_coeff hn, sub_zero, mul_one, div_neg, neg_div], }, { rw [bernoulli_fourier_coeff_recurrence (k + 1) hn, nat.add_sub_cancel k 1], split_ifs, { exfalso, exact (ne_of_gt (nat.lt_succ_iff.mpr hk)) h,}, { rw [h'k, nat.factorial_succ, zero_sub, nat.cast_mul, pow_add, pow_one, neg_div, mul_neg, mul_neg, mul_neg, neg_neg, neg_mul, neg_mul, neg_mul, div_neg], field_simp [int.cast_ne_zero.mpr hn, I_ne_zero], ring_nf, } } end end bernoulli_fourier_coeffs section bernoulli_periodized /-! In this section we use the above evaluations of the Fourier coefficients of Bernoulli polynomials, together with the theorem `has_pointwise_sum_fourier_series_of_summable` from Fourier theory, to obtain an explicit formula for `∑ (n:ℤ), 1 / n ^ k * fourier n x`. -/ /-- The Bernoulli polynomial, extended from `[0, 1)` to the unit circle. -/ def periodized_bernoulli (k : ℕ) : 𝕌 → ℝ := add_circle.lift_Ico 1 0 (bernoulli_fun k) lemma periodized_bernoulli.continuous {k : ℕ} (hk : k ≠ 1) : continuous (periodized_bernoulli k) := add_circle.lift_Ico_zero_continuous (by exact_mod_cast (bernoulli_fun_endpoints_eq_of_ne_one hk).symm) (polynomial.continuous _).continuous_on lemma fourier_coeff_bernoulli_eq {k : ℕ} (hk : k ≠ 0) (n : ℤ) : fourier_coeff (coe ∘ periodized_bernoulli k : 𝕌 → ℂ) n = -k! / (2 * π * I * n) ^ k := begin have : (coe ∘ periodized_bernoulli k : 𝕌 → ℂ) = add_circle.lift_Ico 1 0 (coe ∘ bernoulli_fun k), { ext1 x, refl }, rw [this, fourier_coeff_lift_Ico_eq], simpa only [zero_add] using bernoulli_fourier_coeff_eq hk n, end lemma summable_bernoulli_fourier {k : ℕ} (hk : 2 ≤ k) : summable (λ n, -k! / (2 * π * I * n) ^ k : ℤ → ℂ) := begin have : ∀ (n : ℤ), -(k! : ℂ) / (2 * π * I * n) ^ k = (-k! / (2 * π * I) ^ k) * (1 / n ^ k), { intro n, rw [mul_one_div, div_div, ←mul_pow], }, simp_rw this, apply summable.mul_left, rw ←summable_norm_iff, have : (λ (x : ℤ), ‖1 / (x:ℂ) ^ k‖) = (λ (x : ℤ), |1 / (x:ℝ) ^ k|), { ext1 x, rw [norm_eq_abs, ←complex.abs_of_real], congr' 1, norm_cast }, simp_rw this, rw [summable_abs_iff], exact real.summable_one_div_int_pow.mpr (one_lt_two.trans_le hk), end lemma has_sum_one_div_pow_mul_fourier_mul_bernoulli_fun {k : ℕ} (hk : 2 ≤ k) {x : ℝ} (hx : x ∈ Icc (0:ℝ) 1) : has_sum (λ n:ℤ, 1 / (n:ℂ) ^ k * fourier n (x : 𝕌)) (-(2 * π * I) ^ k / k! * bernoulli_fun k x) := begin -- first show it suffices to prove result for `Ico 0 1` suffices : ∀ {y : ℝ}, y ∈ Ico (0:ℝ) 1 → has_sum _ _, { rw [←Ico_insert_right (zero_le_one' ℝ), mem_insert_iff, or.comm] at hx, rcases hx with hx | rfl, { exact this hx }, { convert this (left_mem_Ico.mpr zero_lt_one) using 1, { rw [add_circle.coe_period, quotient_add_group.coe_zero], }, { rw bernoulli_fun_endpoints_eq_of_ne_one (by linarith : k ≠ 1) } } }, intros y hy, let B : C(𝕌, ℂ) := continuous_map.mk (coe ∘ periodized_bernoulli k) (continuous_of_real.comp (periodized_bernoulli.continuous (by linarith))), have step1 : ∀ (n:ℤ), fourier_coeff B n = -k! / (2 * π * I * n) ^ k, { rw continuous_map.coe_mk, exact fourier_coeff_bernoulli_eq (by linarith : k ≠ 0) }, have step2 := has_pointwise_sum_fourier_series_of_summable ((summable_bernoulli_fourier hk).congr (λ n, (step1 n).symm)) y, simp_rw step1 at step2, convert step2.mul_left ((-(2 * ↑π * I) ^ k) / (k! : ℂ)) using 2, ext1 n, rw [smul_eq_mul, ←mul_assoc, mul_div, mul_neg, div_mul_cancel, neg_neg, mul_pow _ ↑n, ←div_div, div_self], { rw [ne.def, pow_eq_zero_iff', not_and_distrib], exact or.inl two_pi_I_ne_zero, }, { exact nat.cast_ne_zero.mpr (nat.factorial_ne_zero _), }, { rw [continuous_map.coe_mk, function.comp_app, of_real_inj, periodized_bernoulli, add_circle.lift_Ico_coe_apply (by rwa zero_add)] }, end end bernoulli_periodized section cleanup /- This section is just reformulating the results in a nicer form. -/ lemma has_sum_one_div_nat_pow_mul_fourier {k : ℕ} (hk : 2 ≤ k) {x : ℝ} (hx : x ∈ Icc (0:ℝ) 1) : has_sum (λ n:ℕ, 1 / (n:ℂ) ^ k * (fourier n (x : 𝕌) + (-1) ^ k * fourier (-n) (x : 𝕌))) (-(2 * π * I) ^ k / k! * bernoulli_fun k x) := begin convert (has_sum_one_div_pow_mul_fourier_mul_bernoulli_fun hk hx).sum_nat_of_sum_int, { ext1 n, rw [int.cast_neg, mul_add, ←mul_assoc], conv_rhs { rw [neg_eq_neg_one_mul, mul_pow, ←div_div] }, congr' 2, rw [div_mul_eq_mul_div₀, one_mul], congr' 1, rw [eq_div_iff, ←mul_pow, ←neg_eq_neg_one_mul, neg_neg, one_pow], apply pow_ne_zero, rw neg_ne_zero, exact one_ne_zero, }, { rw [int.cast_zero, zero_pow (by linarith : 0 < k), div_zero, zero_mul, add_zero] }, end lemma has_sum_one_div_nat_pow_mul_cos {k : ℕ} (hk : k ≠ 0) {x : ℝ} (hx : x ∈ Icc (0:ℝ) 1) : has_sum (λ n:ℕ, 1 / (n:ℝ) ^ (2 * k) * real.cos (2 * π * n * x)) ((-1) ^ (k + 1) * (2 * π) ^ (2 * k) / 2 / (2 * k)! * (polynomial.map (algebra_map ℚ ℝ) (polynomial.bernoulli (2 * k))).eval x) := begin have : has_sum (λ n:ℕ, 1 / (n:ℂ) ^ (2 * k) * (fourier n (x : 𝕌) + fourier (-n) (x : 𝕌))) ((-1) ^ (k + 1) * (2 * π) ^ (2 * k) / (2 * k)! * bernoulli_fun (2 * k) x), { convert (has_sum_one_div_nat_pow_mul_fourier (by linarith [nat.one_le_iff_ne_zero.mpr hk] : 2 ≤ 2 * k) hx), { ext1 n, rw [pow_mul (-1 : ℂ),neg_one_sq, one_pow, one_mul], }, { rw [pow_add, pow_one], conv_rhs { rw [mul_pow], congr, congr, skip, rw [pow_mul, I_sq] }, ring, } }, convert ((has_sum_iff _ _).mp (this.div_const 2)).1, { ext1 n, convert (of_real_re _).symm, rw of_real_mul,rw ←mul_div, congr, { rw [of_real_div, of_real_one, of_real_pow], refl, }, { rw [of_real_cos, of_real_mul, fourier_coe_apply, fourier_coe_apply, cos, of_real_one, div_one, div_one, of_real_mul, of_real_mul, of_real_bit0, of_real_one, int.cast_neg, int.cast_coe_nat, of_real_nat_cast], congr' 3, { ring }, { ring }, }, }, { convert (of_real_re _).symm, rw [of_real_mul, of_real_div, of_real_div, of_real_mul, of_real_pow, of_real_pow, of_real_neg, of_real_nat_cast, of_real_mul, of_real_bit0, of_real_one], ring }, end lemma has_sum_one_div_nat_pow_mul_sin {k : ℕ} (hk : k ≠ 0) {x : ℝ} (hx : x ∈ Icc (0:ℝ) 1) : has_sum (λ n:ℕ, 1 / (n:ℝ) ^ (2 * k + 1) * real.sin (2 * π * n * x)) ((-1) ^ (k + 1) * (2 * π) ^ (2 * k + 1) / 2 / (2 * k + 1)! * (polynomial.map (algebra_map ℚ ℝ) (polynomial.bernoulli (2 * k + 1))).eval x) := begin have : has_sum (λ n:ℕ, 1 / (n:ℂ) ^ (2 * k + 1) * (fourier n (x : 𝕌) - fourier (-n) (x : 𝕌))) ((-1)^(k + 1) * I * (2 * π)^(2 * k + 1) / (2 * k + 1)! * bernoulli_fun (2 * k + 1) x), { convert (has_sum_one_div_nat_pow_mul_fourier (by linarith [nat.one_le_iff_ne_zero.mpr hk] : 2 ≤ 2 * k + 1) hx), { ext1 n, rw [pow_add (-1: ℂ), pow_mul (-1 : ℂ), neg_one_sq, one_pow, one_mul, pow_one, ←neg_eq_neg_one_mul, ←sub_eq_add_neg], }, { rw [pow_add, pow_one], conv_rhs { rw [mul_pow], congr, congr, skip, rw [pow_add, pow_one, pow_mul, I_sq] }, ring, }, }, convert ((has_sum_iff _ _).mp (this.div_const (2 * I))).1, { ext1 n, convert (of_real_re _).symm, rw of_real_mul,rw ←mul_div, congr, { rw [of_real_div, of_real_one, of_real_pow], refl, }, { rw [of_real_sin, of_real_mul, fourier_coe_apply, fourier_coe_apply, sin, of_real_one, div_one, div_one, of_real_mul, of_real_mul, of_real_bit0, of_real_one, int.cast_neg, int.cast_coe_nat, of_real_nat_cast, ←div_div, div_I, div_mul_eq_mul_div₀, ←neg_div, ←neg_mul, neg_sub], congr' 4, { ring, }, { ring }, }, }, { convert (of_real_re _).symm, rw [of_real_mul, of_real_div, of_real_div, of_real_mul, of_real_pow, of_real_pow, of_real_neg, of_real_nat_cast, of_real_mul, of_real_bit0, of_real_one, ←div_div, div_I, div_mul_eq_mul_div₀], have : ∀ (α β γ δ : ℂ), α * I * β / γ * δ * I = I ^ 2 * α * β / γ * δ := by { intros, ring }, rw [this, I_sq], ring, }, end lemma has_sum_zeta_nat {k : ℕ} (hk : k ≠ 0) : has_sum (λ n:ℕ, 1 / (n:ℝ) ^ (2 * k)) ((-1) ^ (k + 1) * 2 ^ (2 * k - 1) * π ^ (2 * k) * bernoulli (2 * k) / (2 * k)!) := begin convert has_sum_one_div_nat_pow_mul_cos hk (left_mem_Icc.mpr zero_le_one), { ext1 n, rw [mul_zero, real.cos_zero, mul_one], }, rw [polynomial.eval_zero_map, polynomial.bernoulli_eval_zero, eq_rat_cast], have : (2:ℝ) ^ (2 * k - 1) = (2:ℝ) ^ (2 * k) / 2, { rw eq_div_iff (two_ne_zero' ℝ), conv_lhs { congr, skip, rw ←pow_one (2:ℝ) }, rw [←pow_add, nat.sub_add_cancel], linarith [nat.one_le_iff_ne_zero.mpr hk], }, rw [this, mul_pow], ring, end end cleanup section examples lemma has_sum_zeta_two : has_sum (λ n:ℕ, 1 / (n : ℝ) ^ 2) (π ^ 2 / 6) := begin convert has_sum_zeta_nat one_ne_zero using 1, rw mul_one, rw [bernoulli_eq_bernoulli'_of_ne_one (by dec_trivial : 2 ≠ 1), bernoulli'_two], norm_num, field_simp, ring, end lemma has_sum_zeta_four : has_sum (λ n:ℕ, 1 / (n : ℝ) ^ 4) (π ^ 4 / 90) := begin convert has_sum_zeta_nat two_ne_zero using 1, norm_num, rw [bernoulli_eq_bernoulli'_of_ne_one, bernoulli'_four], norm_num, field_simp, ring, dec_trivial, end lemma polynomial.bernoulli_three_eval_one_quarter : (polynomial.bernoulli 3).eval (1 / 4) = 3 / 64 := begin simp_rw [polynomial.bernoulli, finset.sum_range_succ, polynomial.eval_add, polynomial.eval_monomial], rw [finset.sum_range_zero, polynomial.eval_zero, zero_add, bernoulli_one], rw [bernoulli_eq_bernoulli'_of_ne_one zero_ne_one, bernoulli'_zero, bernoulli_eq_bernoulli'_of_ne_one (by dec_trivial : 2 ≠ 1), bernoulli'_two, bernoulli_eq_bernoulli'_of_ne_one (by dec_trivial : 3 ≠ 1), bernoulli'_three], norm_num, end /-- Explicit formula for `L(χ, 3)`, where `χ` is the unique nontrivial Dirichlet character modulo 4. -/ lemma has_sum_L_function_mod_four_eval_three : has_sum (λ n:ℕ, (1 / (n:ℝ) ^ 3 * real.sin (π * n / 2))) (π ^ 3 / 32) := begin convert has_sum_one_div_nat_pow_mul_sin one_ne_zero (_ : 1 / 4 ∈ Icc (0:ℝ) 1), { ext1 n, norm_num, left, congr' 1, ring, }, { have : (1 / 4 : ℝ) = (algebra_map ℚ ℝ) (1 / 4 : ℚ), by norm_num, rw [this, mul_pow, polynomial.eval_map, polynomial.eval₂_at_apply, (by dec_trivial : 2 * 1 + 1 = 3), polynomial.bernoulli_three_eval_one_quarter], norm_num, field_simp, ring }, { rw mem_Icc, split, linarith, linarith, }, end end examples
25c20687ac891ea58c46af7498a28a625e233d57
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/omega/term_auto.lean
0701362413ae9945eccb6200c935259695efc838
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,022
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.omega.coeffs import Mathlib.PostPort namespace Mathlib /- Normalized linear integer arithmetic terms. -/ namespace omega /-- Shadow syntax of normalized terms. The first element represents the constant term and the list represents the coefficients. -/ def term := ℤ × List ℤ namespace term /-- Evaluate a term using the valuation v. -/ @[simp] def val (v : ℕ → ℤ) : term → ℤ := sorry @[simp] def neg : term → term := sorry @[simp] def add : term → term → term := sorry @[simp] def sub : term → term → term := sorry @[simp] def mul (i : ℤ) : term → term := sorry @[simp] def div (i : ℤ) : term → term := sorry theorem val_neg {v : ℕ → ℤ} {t : term} : val v (neg t) = -val v t := sorry @[simp] theorem val_sub {v : ℕ → ℤ} {t1 : term} {t2 : term} : val v (sub t1 t2) = val v t1 - val v t2 := sorry @[simp] theorem val_add {v : ℕ → ℤ} {t1 : term} {t2 : term} : val v (add t1 t2) = val v t1 + val v t2 := sorry @[simp] theorem val_mul {v : ℕ → ℤ} {i : ℤ} {t : term} : val v (mul i t) = i * val v t := sorry theorem val_div {v : ℕ → ℤ} {i : ℤ} {b : ℤ} {as : List ℤ} : i ∣ b → (∀ (x : ℤ), x ∈ as → i ∣ x) → val v (div i (b, as)) = val v (b, as) / i := sorry /-- Fresh de Brujin index not used by any variable ocurring in the term -/ def fresh_index (t : term) : ℕ := list.length (prod.snd t) def to_string (t : term) : string := list.foldr (fun (_x : ℕ × ℤ) => sorry) (to_string (prod.fst t)) (list.enum (prod.snd t)) protected instance has_to_string : has_to_string term := has_to_string.mk to_string end term /-- Fresh de Brujin index not used by any variable ocurring in the list of terms -/ def terms.fresh_index : List term → ℕ := sorry end Mathlib
600ffdfcb8a202bccb8cfb4d4d1beb565b4ec893
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/module/submodule/basic.lean
106569d968336d42e7c821a350532f9bc5ab1ebe
[ "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
16,225
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro -/ import algebra.module.linear_map import algebra.module.equiv import group_theory.group_action.sub_mul_action /-! # Submodules of a module In this file we define * `submodule R M` : a subset of a `module` `M` that contains zero and is closed with respect to addition and scalar multiplication. * `subspace k M` : an abbreviation for `submodule` assuming that `k` is a `field`. ## Tags submodule, subspace, linear map -/ open function open_locale big_operators universes u'' u' u v w variables {G : Type u''} {S : Type u'} {R : Type u} {M : Type v} {ι : Type w} set_option old_structure_cmd true /-- A submodule of a module is one which is closed under vector operations. This is a sufficient condition for the subset of vectors in the submodule to themselves form a module. -/ structure submodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [module R M] extends add_submonoid M, sub_mul_action R M : Type v. /-- Reinterpret a `submodule` as an `add_submonoid`. -/ add_decl_doc submodule.to_add_submonoid /-- Reinterpret a `submodule` as an `sub_mul_action`. -/ add_decl_doc submodule.to_sub_mul_action namespace submodule variables [semiring R] [add_comm_monoid M] [module R M] instance : set_like (submodule R M) M := { coe := submodule.carrier, coe_injective' := λ p q h, by cases p; cases q; congr' } instance : add_submonoid_class (submodule R M) M := { zero_mem := zero_mem', add_mem := add_mem' } @[simp] theorem mem_to_add_submonoid (p : submodule R M) (x : M) : x ∈ p.to_add_submonoid ↔ x ∈ p := iff.rfl variables {p q : submodule R M} @[simp] lemma mem_mk {S : set M} {x : M} (h₁ h₂ h₃) : x ∈ (⟨S, h₁, h₂, h₃⟩ : submodule R M) ↔ x ∈ S := iff.rfl @[simp] lemma coe_set_mk (S : set M) (h₁ h₂ h₃) : ((⟨S, h₁, h₂, h₃⟩ : submodule R M) : set M) = S := rfl @[simp] lemma mk_le_mk {S S' : set M} (h₁ h₂ h₃ h₁' h₂' h₃') : (⟨S, h₁, h₂, h₃⟩ : submodule R M) ≤ (⟨S', h₁', h₂', h₃'⟩ : submodule R M) ↔ S ⊆ S' := iff.rfl @[ext] theorem ext (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := set_like.ext h /-- Copy of a submodule with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (p : submodule R M) (s : set M) (hs : s = ↑p) : submodule R M := { carrier := s, zero_mem' := hs.symm ▸ p.zero_mem', add_mem' := hs.symm ▸ p.add_mem', smul_mem' := hs.symm ▸ p.smul_mem' } @[simp] lemma coe_copy (S : submodule R M) (s : set M) (hs : s = ↑S) : (S.copy s hs : set M) = s := rfl lemma copy_eq (S : submodule R M) (s : set M) (hs : s = ↑S) : S.copy s hs = S := set_like.coe_injective hs theorem to_add_submonoid_injective : injective (to_add_submonoid : submodule R M → add_submonoid M) := λ p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h) @[simp] theorem to_add_submonoid_eq : p.to_add_submonoid = q.to_add_submonoid ↔ p = q := to_add_submonoid_injective.eq_iff @[mono] lemma to_add_submonoid_strict_mono : strict_mono (to_add_submonoid : submodule R M → add_submonoid M) := λ _ _, id lemma to_add_submonoid_le : p.to_add_submonoid ≤ q.to_add_submonoid ↔ p ≤ q := iff.rfl @[mono] lemma to_add_submonoid_mono : monotone (to_add_submonoid : submodule R M → add_submonoid M) := to_add_submonoid_strict_mono.monotone @[simp] theorem coe_to_add_submonoid (p : submodule R M) : (p.to_add_submonoid : set M) = p := rfl theorem to_sub_mul_action_injective : injective (to_sub_mul_action : submodule R M → sub_mul_action R M) := λ p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h) @[simp] theorem to_sub_mul_action_eq : p.to_sub_mul_action = q.to_sub_mul_action ↔ p = q := to_sub_mul_action_injective.eq_iff @[mono] lemma to_sub_mul_action_strict_mono : strict_mono (to_sub_mul_action : submodule R M → sub_mul_action R M) := λ _ _, id @[mono] lemma to_sub_mul_action_mono : monotone (to_sub_mul_action : submodule R M → sub_mul_action R M) := to_sub_mul_action_strict_mono.monotone @[simp] theorem coe_to_sub_mul_action (p : submodule R M) : (p.to_sub_mul_action : set M) = p := rfl end submodule namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] -- We can infer the module structure implicitly from the bundled submodule, -- rather than via typeclass resolution. variables {module_M : module R M} variables {p q : submodule R M} variables {r : R} {x y : M} variables (p) @[simp] lemma mem_carrier : x ∈ p.carrier ↔ x ∈ (p : set M) := iff.rfl @[simp] protected lemma zero_mem : (0 : M) ∈ p := zero_mem _ protected lemma add_mem (h₁ : x ∈ p) (h₂ : y ∈ p) : x + y ∈ p := add_mem h₁ h₂ lemma smul_mem (r : R) (h : x ∈ p) : r • x ∈ p := p.smul_mem' r h lemma smul_of_tower_mem [has_smul S R] [has_smul S M] [is_scalar_tower S R M] (r : S) (h : x ∈ p) : r • x ∈ p := p.to_sub_mul_action.smul_of_tower_mem r h protected lemma sum_mem {t : finset ι} {f : ι → M} : (∀c∈t, f c ∈ p) → (∑ i in t, f i) ∈ p := sum_mem lemma sum_smul_mem {t : finset ι} {f : ι → M} (r : ι → R) (hyp : ∀ c ∈ t, f c ∈ p) : (∑ i in t, r i • f i) ∈ p := sum_mem (λ i hi, smul_mem _ _ (hyp i hi)) @[simp] lemma smul_mem_iff' [group G] [mul_action G M] [has_smul G R] [is_scalar_tower G R M] (g : G) : g • x ∈ p ↔ x ∈ p := p.to_sub_mul_action.smul_mem_iff' g instance : has_add p := ⟨λx y, ⟨x.1 + y.1, add_mem x.2 y.2⟩⟩ instance : has_zero p := ⟨⟨0, zero_mem _⟩⟩ instance : inhabited p := ⟨0⟩ instance [has_smul S R] [has_smul S M] [is_scalar_tower S R M] : has_smul S p := ⟨λ c x, ⟨c • x.1, smul_of_tower_mem _ c x.2⟩⟩ instance [has_smul S R] [has_smul S M] [is_scalar_tower S R M] : is_scalar_tower S R p := p.to_sub_mul_action.is_scalar_tower instance [has_smul S R] [has_smul S M] [is_scalar_tower S R M] [has_smul Sᵐᵒᵖ R] [has_smul Sᵐᵒᵖ M] [is_scalar_tower Sᵐᵒᵖ R M] [is_central_scalar S M] : is_central_scalar S p := p.to_sub_mul_action.is_central_scalar protected lemma nonempty : (p : set M).nonempty := ⟨0, p.zero_mem⟩ @[simp] lemma mk_eq_zero {x} (h : x ∈ p) : (⟨x, h⟩ : p) = 0 ↔ x = 0 := subtype.ext_iff_val variables {p} @[simp, norm_cast] lemma coe_eq_zero {x : p} : (x : M) = 0 ↔ x = 0 := (set_like.coe_eq_coe : (x : M) = (0 : p) ↔ x = 0) @[simp, norm_cast] lemma coe_add (x y : p) : (↑(x + y) : M) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : p) : M) = 0 := rfl @[norm_cast] lemma coe_smul (r : R) (x : p) : ((r • x : p) : M) = r • ↑x := rfl @[simp, norm_cast] lemma coe_smul_of_tower [has_smul S R] [has_smul S M] [is_scalar_tower S R M] (r : S) (x : p) : ((r • x : p) : M) = r • ↑x := rfl @[simp, norm_cast] lemma coe_mk (x : M) (hx : x ∈ p) : ((⟨x, hx⟩ : p) : M) = x := rfl @[simp] lemma coe_mem (x : p) : (x : M) ∈ p := x.2 variables (p) instance : add_comm_monoid p := { add := (+), zero := 0, .. p.to_add_submonoid.to_add_comm_monoid } instance module' [semiring S] [has_smul S R] [module S M] [is_scalar_tower S R M] : module S p := by refine {smul := (•), ..p.to_sub_mul_action.mul_action', ..}; { intros, apply set_coe.ext, simp [smul_add, add_smul, mul_smul] } instance : module R p := p.module' instance no_zero_smul_divisors [no_zero_smul_divisors R M] : no_zero_smul_divisors R p := ⟨λ c x h, have c = 0 ∨ (x : M) = 0, from eq_zero_or_eq_zero_of_smul_eq_zero (congr_arg coe h), this.imp_right (@subtype.ext_iff _ _ x 0).mpr⟩ /-- Embedding of a submodule `p` to the ambient space `M`. -/ protected def subtype : p →ₗ[R] M := by refine {to_fun := coe, ..}; simp [coe_smul] theorem subtype_apply (x : p) : p.subtype x = x := rfl @[simp] lemma coe_subtype : ((submodule.subtype p) : p → M) = coe := rfl lemma injective_subtype : injective p.subtype := subtype.coe_injective /-- Note the `add_submonoid` version of this lemma is called `add_submonoid.coe_finset_sum`. -/ @[simp] lemma coe_sum (x : ι → p) (s : finset ι) : ↑(∑ i in s, x i) = ∑ i in s, (x i : M) := p.subtype.map_sum section restrict_scalars variables (S) [semiring S] [module S M] [module R M] [has_smul S R] [is_scalar_tower S R M] /-- `V.restrict_scalars S` is the `S`-submodule of the `S`-module given by restriction of scalars, corresponding to `V`, an `R`-submodule of the original `R`-module. -/ def restrict_scalars (V : submodule R M) : submodule S M := { carrier := V, zero_mem' := V.zero_mem, smul_mem' := λ c m h, V.smul_of_tower_mem c h, add_mem' := λ x y hx hy, V.add_mem hx hy } @[simp] lemma coe_restrict_scalars (V : submodule R M) : (V.restrict_scalars S : set M) = V := rfl @[simp] lemma restrict_scalars_mem (V : submodule R M) (m : M) : m ∈ V.restrict_scalars S ↔ m ∈ V := iff.refl _ @[simp] lemma restrict_scalars_self (V : submodule R M) : V.restrict_scalars R = V := set_like.coe_injective rfl variables (R S M) lemma restrict_scalars_injective : function.injective (restrict_scalars S : submodule R M → submodule S M) := λ V₁ V₂ h, ext $ set.ext_iff.1 (set_like.ext'_iff.1 h : _) @[simp] lemma restrict_scalars_inj {V₁ V₂ : submodule R M} : restrict_scalars S V₁ = restrict_scalars S V₂ ↔ V₁ = V₂ := (restrict_scalars_injective S _ _).eq_iff /-- Even though `p.restrict_scalars S` has type `submodule S M`, it is still an `R`-module. -/ instance restrict_scalars.orig_module (p : submodule R M) : module R (p.restrict_scalars S) := (by apply_instance : module R p) instance (p : submodule R M) : is_scalar_tower S R (p.restrict_scalars S) := { smul_assoc := λ r s x, subtype.ext $ smul_assoc r s (x : M) } /-- `restrict_scalars S` is an embedding of the lattice of `R`-submodules into the lattice of `S`-submodules. -/ @[simps] def restrict_scalars_embedding : submodule R M ↪o submodule S M := { to_fun := restrict_scalars S, inj' := restrict_scalars_injective S R M, map_rel_iff' := λ p q, by simp [set_like.le_def] } /-- Turning `p : submodule R M` into an `S`-submodule gives the same module structure as turning it into a type and adding a module structure. -/ @[simps {simp_rhs := tt}] def restrict_scalars_equiv (p : submodule R M) : p.restrict_scalars S ≃ₗ[R] p := { to_fun := id, inv_fun := id, map_smul' := λ c x, rfl, .. add_equiv.refl p } end restrict_scalars end add_comm_monoid section add_comm_group variables [ring R] [add_comm_group M] variables {module_M : module R M} variables (p p' : submodule R M) variables {r : R} {x y : M} instance [module R M] : add_subgroup_class (submodule R M) M := { neg_mem := λ p x, p.to_sub_mul_action.neg_mem, .. submodule.add_submonoid_class } protected lemma neg_mem (hx : x ∈ p) : -x ∈ p := neg_mem hx /-- Reinterpret a submodule as an additive subgroup. -/ def to_add_subgroup : add_subgroup M := { neg_mem' := λ _, p.neg_mem , .. p.to_add_submonoid } @[simp] lemma coe_to_add_subgroup : (p.to_add_subgroup : set M) = p := rfl @[simp] lemma mem_to_add_subgroup : x ∈ p.to_add_subgroup ↔ x ∈ p := iff.rfl include module_M theorem to_add_subgroup_injective : injective (to_add_subgroup : submodule R M → add_subgroup M) | p q h := set_like.ext (set_like.ext_iff.1 h : _) @[simp] theorem to_add_subgroup_eq : p.to_add_subgroup = p'.to_add_subgroup ↔ p = p' := to_add_subgroup_injective.eq_iff @[mono] lemma to_add_subgroup_strict_mono : strict_mono (to_add_subgroup : submodule R M → add_subgroup M) := λ _ _, id lemma to_add_subgroup_le : p.to_add_subgroup ≤ p'.to_add_subgroup ↔ p ≤ p' := iff.rfl @[mono] lemma to_add_subgroup_mono : monotone (to_add_subgroup : submodule R M → add_subgroup M) := to_add_subgroup_strict_mono.monotone omit module_M protected lemma sub_mem : x ∈ p → y ∈ p → x - y ∈ p := sub_mem protected lemma neg_mem_iff : -x ∈ p ↔ x ∈ p := neg_mem_iff protected lemma add_mem_iff_left : y ∈ p → (x + y ∈ p ↔ x ∈ p) := add_mem_cancel_right protected lemma add_mem_iff_right : x ∈ p → (x + y ∈ p ↔ y ∈ p) := add_mem_cancel_left protected lemma coe_neg (x : p) : ((-x : p) : M) = -x := add_subgroup_class.coe_neg _ protected lemma coe_sub (x y : p) : (↑(x - y) : M) = ↑x - ↑y := add_subgroup_class.coe_sub _ _ lemma sub_mem_iff_left (hy : y ∈ p) : (x - y) ∈ p ↔ x ∈ p := by rw [sub_eq_add_neg, p.add_mem_iff_left (p.neg_mem hy)] lemma sub_mem_iff_right (hx : x ∈ p) : (x - y) ∈ p ↔ y ∈ p := by rw [sub_eq_add_neg, p.add_mem_iff_right hx, p.neg_mem_iff] instance : add_comm_group p := { add := (+), zero := 0, neg := has_neg.neg, ..p.to_add_subgroup.to_add_comm_group } end add_comm_group section is_domain variables [ring R] [is_domain R] variables [add_comm_group M] [module R M] {b : ι → M} lemma not_mem_of_ortho {x : M} {N : submodule R M} (ortho : ∀ (c : R) (y ∈ N), c • x + y = (0 : M) → c = 0) : x ∉ N := by { intro hx, simpa using ortho (-1) x hx } lemma ne_zero_of_ortho {x : M} {N : submodule R M} (ortho : ∀ (c : R) (y ∈ N), c • x + y = (0 : M) → c = 0) : x ≠ 0 := mt (λ h, show x ∈ N, from h.symm ▸ N.zero_mem) (not_mem_of_ortho ortho) end is_domain section ordered_monoid variables [semiring R] /-- A submodule of an `ordered_add_comm_monoid` is an `ordered_add_comm_monoid`. -/ instance to_ordered_add_comm_monoid {M} [ordered_add_comm_monoid M] [module R M] (S : submodule R M) : ordered_add_comm_monoid S := subtype.coe_injective.ordered_add_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submodule of a `linear_ordered_add_comm_monoid` is a `linear_ordered_add_comm_monoid`. -/ instance to_linear_ordered_add_comm_monoid {M} [linear_ordered_add_comm_monoid M] [module R M] (S : submodule R M) : linear_ordered_add_comm_monoid S := subtype.coe_injective.linear_ordered_add_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A submodule of an `ordered_cancel_add_comm_monoid` is an `ordered_cancel_add_comm_monoid`. -/ instance to_ordered_cancel_add_comm_monoid {M} [ordered_cancel_add_comm_monoid M] [module R M] (S : submodule R M) : ordered_cancel_add_comm_monoid S := subtype.coe_injective.ordered_cancel_add_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submodule of a `linear_ordered_cancel_add_comm_monoid` is a `linear_ordered_cancel_add_comm_monoid`. -/ instance to_linear_ordered_cancel_add_comm_monoid {M} [linear_ordered_cancel_add_comm_monoid M] [module R M] (S : submodule R M) : linear_ordered_cancel_add_comm_monoid S := subtype.coe_injective.linear_ordered_cancel_add_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) end ordered_monoid section ordered_group variables [ring R] /-- A submodule of an `ordered_add_comm_group` is an `ordered_add_comm_group`. -/ instance to_ordered_add_comm_group {M} [ordered_add_comm_group M] [module R M] (S : submodule R M) : ordered_add_comm_group S := subtype.coe_injective.ordered_add_comm_group coe rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A submodule of a `linear_ordered_add_comm_group` is a `linear_ordered_add_comm_group`. -/ instance to_linear_ordered_add_comm_group {M} [linear_ordered_add_comm_group M] [module R M] (S : submodule R M) : linear_ordered_add_comm_group S := subtype.coe_injective.linear_ordered_add_comm_group coe rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) end ordered_group end submodule namespace submodule variables [division_ring S] [semiring R] [add_comm_monoid M] [module R M] variables [has_smul S R] [module S M] [is_scalar_tower S R M] variables (p : submodule R M) {s : S} {x y : M} theorem smul_mem_iff (s0 : s ≠ 0) : s • x ∈ p ↔ x ∈ p := p.to_sub_mul_action.smul_mem_iff s0 end submodule /-- Subspace of a vector space. Defined to equal `submodule`. -/ abbreviation subspace (R : Type u) (M : Type v) [field R] [add_comm_group M] [module R M] := submodule R M
d76c7fb596a26bfc2e09d2d20045b755b4955669
9cb9db9d79fad57d80ca53543dc07efb7c4f3838
/src/pseudo_normed_group/breen_deligne.lean
9ad0592bd56ad04bd094c8a483ca1630b179ca46
[]
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
4,111
lean
import pseudo_normed_group.basic import pseudo_normed_group.category import breen_deligne.suitable import for_mathlib.add_monoid_hom noncomputable theory local attribute [instance] type_pow open_locale nnreal big_operators matrix namespace breen_deligne namespace basic_universal_map variables {m n : ℕ} (f : basic_universal_map m n) variables (M : Type*) section pseudo_normed_group variables [pseudo_normed_group M] open add_monoid_hom pseudo_normed_group -- TODO: make this definition readable. /-- `f.eval_png` is the group homomorphism `(M^m) →+ (M^n)` obtained by matrix multiplication with the matrix `f`. Implementation detail: We currently cannot multiply a matrix with `ℤ`-coefficients with a vector with coefficients in a `ℤ`-module. Hence we write out the definition of the homomorphism in a slightly convoluted way. See the lemma `eval_png_apply` for a readable formula. -/ def eval_png : (M^m) →+ (M^n) := mk_to_pi $ λ j, mk_from_pi $ λ i, const_smul_hom _ $ f j i lemma eval_png_apply (x : M^m) : f.eval_png M x = λ j, ∑ i, f j i • (x i) := begin ext j, simp only [eval_png, coe_mk_from_pi, add_monoid_hom.apply_apply, mk_to_pi_apply, add_monoid_hom.to_fun_eq_coe, fintype.sum_apply, function.comp_app, coe_gsmul, @mk_from_pi_apply M _ (fin m) _ (λ _, M) _ _ x, const_smul_hom_apply] end @[simp] lemma eval_png_zero : (0 : basic_universal_map m n).eval_png M = 0 := by { ext, simp only [eval_png_apply, zero_smul, finset.sum_const_zero, matrix.zero_apply], refl } lemma eval_png_mem_filtration : (f.eval_png M) ∈ filtration ((M^m) →+ (M^n)) (finset.univ.sup $ λ i, ∑ j, (f i j).nat_abs) := begin apply mk_to_pi_mem_filtration, intro j, refine filtration_mono (finset.le_sup (finset.mem_univ j)) (mk_from_pi_mem_filtration _ _), intros i, exact const_smul_hom_int_mem_filtration _ _ le_rfl end lemma eval_png_mem_filtration' (c₁ c₂ : ℝ≥0) [h : f.suitable c₁ c₂] (x : M^m) (hx : x ∈ filtration (M^m) c₁) : (f.eval_png M x) ∈ filtration (M^n) c₂ := filtration_mono (f.sup_mul_le c₁ c₂) (f.eval_png_mem_filtration M hx) /-- `f.eval_png₀ M` is the group homomorphism `(M^m) →+ (M^n)` obtained by matrix multiplication with the matrix `f`, but restricted to `(filtration M c₁)^m → (filtration M c₂)^n`. -/ def eval_png₀ (c₁ c₂ : ℝ≥0) [h : f.suitable c₁ c₂] (x : (filtration M c₁ : Type*)^m) : (filtration M c₂ : Type*)^n := λ j, (⟨f.eval_png M (pow_incl x) j, eval_png_mem_filtration' f M c₁ c₂ _ (λ i, (x i).2) j⟩ : (filtration M c₂ : Type*)) lemma eval_png_comp {l m n} (g : basic_universal_map m n) (f : basic_universal_map l m) : (g.comp f).eval_png M = (g.eval_png M).comp (f.eval_png M) := begin ext x j, simp only [eval_png_apply, function.comp_app, coe_comp, basic_universal_map.comp, matrix.mul_apply, finset.smul_sum, finset.sum_smul, mul_smul], rw finset.sum_comm end end pseudo_normed_group section profinitely_filtered_pseudo_normed_group open pseudo_normed_group variables [profinitely_filtered_pseudo_normed_group M] lemma pfpng_ctu'_eval_png : pfpng_ctu' (f.eval_png M) := begin have : (f.eval_png M : M^m → M^n) = ∑ i, λ x j, f j i • (x i), { ext x j, rw [f.eval_png_apply M x, finset.sum_apply, finset.sum_apply] }, rw this, refine pfpng_ctu'_sum _ _ _ _, { rintro i -, refine pfpng_ctu'_of_pfpng_ctu i (λ (x : M) j, f j i • x) _, intro j, exact pfpng_ctu_smul_int _ _ }, { rintro i - c₁, let C : ℝ≥0 := finset.univ.sup (λ j, (f j i).nat_abs), refine ⟨C * c₁, _⟩, intros x j, have := add_monoid_hom.const_smul_hom_int_mem_filtration (f j i) _ le_rfl (x i).2, apply filtration_mono (mul_le_mul' _ le_rfl) this, exact finset.le_sup (finset.mem_univ j) } end lemma eval_png₀_continuous (c₁ c₂ : ℝ≥0) [f.suitable c₁ c₂] : continuous (f.eval_png₀ M c₁ c₂) := f.pfpng_ctu'_eval_png M _ $ λ x, rfl end profinitely_filtered_pseudo_normed_group end basic_universal_map end breen_deligne #lint- only unused_arguments def_lemma doc_blame
71f8ace728ffd90fa197f306d308e30bbd13e0da
7920bb65c187f9e7065f47e49b3b60ebe8cfe2c1
/src/part1.lean
9c3688df828e08a762c8b2079df4b9b8d0e7ccba
[]
no_license
ADedecker/grifone-3-16
3357441687acd8e1d8aa0f3eaeab76d69bd46b62
d7cb5a58695203dea6a27fb69067e08eb6a82bad
refs/heads/master
1,679,431,665,551
1,616,077,475,000
1,616,077,475,000
348,148,279
0
0
null
null
null
null
UTF-8
Lean
false
false
1,848
lean
import linear_algebra import utils namespace linear_map variables {E K : Type*} [field K] [add_comm_group E] [vector_space K E] (f : E →ₗ[K] E) local notation `k` := f.ker_it local notation `j` := f.range_it def ker_it (n : ℕ) := (f^n).ker lemma mem_ker_it_succ_iff (n : ℕ) (x : E) : x ∈ k (n+1) ↔ f x ∈ k n := by rw [ker_it, mem_ker, ker_it, mem_ker, pow_succ']; refl def range_it (n : ℕ) := (f^n).range lemma range_it_succ_eq' (n : ℕ) : j (n+1) = (j n).map f := by rw [range_it, range_it, range, range, pow_succ, mul_eq_comp, submodule.map_comp] lemma range_it_succ_eq (n : ℕ) : j (n+1) = f.range.map (f^n) := by rw [range_it, range, range, pow_succ', mul_eq_comp, submodule.map_comp] lemma ker_it_mono : monotone k := monotone_of_monotone_nat begin intros n x hx, rw [ker_it, mem_ker] at ⊢ hx, simp [hx, pow_succ] end lemma range_it_antimono : ∀ ⦃x y⦄, x ≤ y → j y ≤ j x := antimono_of_antimono_nat (λ n x ⟨x', _, hx'⟩, ⟨f x', trivial, by simpa using hx'⟩) lemma q1_1 {m : ℕ} (hm : j (m+1) = j m) : ∀ p, j (m+p) = j m | 0 := rfl | (p+1) := le_antisymm (f.range_it_antimono $ le_add_right $ le_refl m) begin intros x hx, rwa [← hm, range_it_succ_eq', ← q1_1 p, ← range_it_succ_eq'] at hx end lemma q1_2 {m : ℕ} (hm : k (m+1) = k m) : ∀ p, k (m+p) = k m | 0 := rfl | (p+1) := le_antisymm begin intros x hx, rwa [← add_assoc, mem_ker_it_succ_iff, q1_2 p, ← mem_ker_it_succ_iff, hm] at hx, end (f.ker_it_mono $ le_add_right $ le_refl m) lemma q1_1' {m n : ℕ} (hm : j (m+1) = j m) (hmn : m ≤ n) : j n = j m := begin convert f.q1_1 hm (n-m), rw nat.add_sub_cancel' hmn end lemma q1_2' {m n : ℕ} (hm : k (m+1) = k m) (hmn : m ≤ n) : k n = k m := begin convert f.q1_2 hm (n-m), rw nat.add_sub_cancel' hmn end end linear_map
c2046a18bd0a75aa9b9a83fcba398d04a1fe0970
a6b711a4e8db20755026231f7ed529a9014b2b6d
/05_Disjunction/00_intro.lean
46ea4e07e4eddc9d3b469a0cdb4ca06eaa19d6b8
[]
no_license
chaseboettner/cs-dm-1
b67d4a7e86f56bce59d2af115503769749d423b2
80b35f2957ffaa45b8b7a4479a3570a2d6eb4db0
refs/heads/master
1,585,367,603,488
1,536,235,675,000
1,536,235,675,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
507
lean
-- UNDER CONSTRUCTION section variables P Q R X Y Z: Prop variable pfP : P variable pfQ : Q variable pfR : R /- There's not enough information on the right hand side of the := to infer the value of R, so type inference can't be used, so R must be given explicitly. -/ theorem pfPorR : P ∨ R := or.intro_left R pfP /- EXERCISE: Explain why P doesn't need to be given explicitly to the right of the :=. -/ /- P ----- (∨-intro-left) P ∨ Q Q ----- (∨-intro-right) P ∨ Q -/
2a3efc65e84e6d2247d815ba337a984207554593
f7c63613a4933f66368ef2802a50c417a46cddfc
/tests/lean/run/1813.lean
e5b265dfabdd3baa084656419da83840001199d9
[ "Apache-2.0" ]
permissive
avigad/lean
28cef71cd8c4eae53f342c87f81ca78d7cc75874
5410178203ab5ae854b5804586ace074ffd63aae
refs/heads/master
1,608,801,379,340
1,504,894,459,000
1,505,174,163,000
22,361,408
0
0
null
null
null
null
UTF-8
Lean
false
false
523
lean
open tactic example {A B : Type} (f : A -> B) a b c (h1 : f a = b) (h2 : f a = c) : false := begin rw h1 at *, guard_hyp h1 := f a = b, guard_hyp h2 := b = c, admit end example {A B : Type} (f : A -> B) a b c (h1 : f a = b) (h2 : f a = c) : false := begin rw [id h1] at *, guard_hyp h1 := f a = b, guard_hyp h2 := b = c, admit end example {A B : Type} (f : A -> B) a b c (h1 : f a = b) (h2 : f a = c) : false := begin rw [id id h1] at *, guard_hyp h1 := f a = b, guard_hyp h2 := b = c, admit end
ad68946d0a81b8ba871c07289706ecacacf332a7
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/legendre_symbol/jacobi_symbol.lean
9bbec60ab8148ebd131ea7324d30b0ca8bd7f472
[ "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
17,540
lean
/- Copyright (c) 2022 Michael Stoll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Stoll -/ import number_theory.legendre_symbol.quadratic_reciprocity /-! # The Jacobi Symbol We define the Jacobi symbol and prove its main properties. ## Main definitions We define the Jacobi symbol, `jacobi_sym a b`, for integers `a` and natural numbers `b` as the product over the prime factors `p` of `b` of the Legendre symbols `legendre_sym p a`. This agrees with the mathematical definition when `b` is odd. The prime factors are obtained via `nat.factors`. Since `nat.factors 0 = []`, this implies in particular that `jacobi_sym a 0 = 1` for all `a`. ## Main statements We prove the main properties of the Jacobi symbol, including the following. * Multiplicativity in both arguments (`jacobi_sym.mul_left`, `jacobi_sym.mul_right`) * The value of the symbol is `1` or `-1` when the arguments are coprime (`jacobi_sym.eq_one_or_neg_one`) * The symbol vanishes if and only if `b ≠ 0` and the arguments are not coprime (`jacobi_sym.eq_zero_iff`) * If the symbol has the value `-1`, then `a : zmod b` is not a square (`zmod.nonsquare_of_jacobi_sym_eq_neg_one`); the converse holds when `b = p` is a prime (`zmod.nonsquare_iff_jacobi_sym_eq_neg_one`); in particular, in this case `a` is a square mod `p` when the symbol has the value `1` (`zmod.is_square_of_jacobi_sym_eq_one`). * Quadratic reciprocity (`jacobi_sym.quadratic_reciprocity`, `jacobi_sym.quadratic_reciprocity_one_mod_four`, `jacobi_sym.quadratic_reciprocity_three_mod_four`) * The supplementary laws for `a = -1`, `a = 2`, `a = -2` (`jacobi_sym.at_neg_one`, `jacobi_sym.at_two`, `jacobi_sym.at_neg_two`) * The symbol depends on `a` only via its residue class mod `b` (`jacobi_sym.mod_left`) and on `b` only via its residue class mod `4*a` (`jacobi_sym.mod_right`) ## Notations We define the notation `J(a | b)` for `jacobi_sym a b`, localized to `number_theory_symbols`. ## Tags Jacobi symbol, quadratic reciprocity -/ section jacobi /-! ### Definition of the Jacobi symbol We define the Jacobi symbol $\Bigl(\frac{a}{b}\Bigr)$ for integers `a` and natural numbers `b` as the product of the Legendre symbols $\Bigl(\frac{a}{p}\Bigr)$, where `p` runs through the prime divisors (with multiplicity) of `b`, as provided by `b.factors`. This agrees with the Jacobi symbol when `b` is odd and gives less meaningful values when it is not (e.g., the symbol is `1` when `b = 0`). This is called `jacobi_sym a b`. We define localized notation (locale `number_theory_symbols`) `J(a | b)` for the Jacobi symbol `jacobi_sym a b`. -/ open nat zmod /-- The Jacobi symbol of `a` and `b` -/ -- Since we need the fact that the factors are prime, we use `list.pmap`. def jacobi_sym (a : ℤ) (b : ℕ) : ℤ := (b.factors.pmap (λ p pp, @legendre_sym p ⟨pp⟩ a) (λ p pf, prime_of_mem_factors pf)).prod -- Notation for the Jacobi symbol. localized "notation `J(` a ` | ` b `)` := jacobi_sym a b" in number_theory_symbols /-! ### Properties of the Jacobi symbol -/ namespace jacobi_sym /-- The symbol `J(a | 0)` has the value `1`. -/ @[simp] lemma zero_right (a : ℤ) : J(a | 0) = 1 := by simp only [jacobi_sym, factors_zero, list.prod_nil, list.pmap] /-- The symbol `J(a | 1)` has the value `1`. -/ @[simp] lemma one_right (a : ℤ) : J(a | 1) = 1 := by simp only [jacobi_sym, factors_one, list.prod_nil, list.pmap] /-- The Legendre symbol `legendre_sym p a` with an integer `a` and a prime number `p` is the same as the Jacobi symbol `J(a | p)`. -/ lemma _root_.legendre_sym.to_jacobi_sym (p : ℕ) [fp : fact p.prime] (a : ℤ) : legendre_sym p a = J(a | p) := by simp only [jacobi_sym, factors_prime fp.1, list.prod_cons, list.prod_nil, mul_one, list.pmap] /-- The Jacobi symbol is multiplicative in its second argument. -/ lemma mul_right' (a : ℤ) {b₁ b₂ : ℕ} (hb₁ : b₁ ≠ 0) (hb₂ : b₂ ≠ 0) : J(a | b₁ * b₂) = J(a | b₁) * J(a | b₂) := begin rw [jacobi_sym, ((perm_factors_mul hb₁ hb₂).pmap _).prod_eq, list.pmap_append, list.prod_append], exacts [rfl, λ p hp, (list.mem_append.mp hp).elim prime_of_mem_factors prime_of_mem_factors], end /-- The Jacobi symbol is multiplicative in its second argument. -/ lemma mul_right (a : ℤ) (b₁ b₂ : ℕ) [ne_zero b₁] [ne_zero b₂] : J(a | b₁ * b₂) = J(a | b₁) * J(a | b₂) := mul_right' a (ne_zero.ne b₁) (ne_zero.ne b₂) /-- The Jacobi symbol takes only the values `0`, `1` and `-1`. -/ lemma trichotomy (a : ℤ) (b : ℕ) : J(a | b) = 0 ∨ J(a | b) = 1 ∨ J(a | b) = -1 := ((@sign_type.cast_hom ℤ _ _).to_monoid_hom.mrange.copy {0, 1, -1} $ by {rw set.pair_comm, exact (sign_type.range_eq sign_type.cast_hom).symm}).list_prod_mem begin intros _ ha', rcases list.mem_pmap.mp ha' with ⟨p, hp, rfl⟩, haveI : fact p.prime := ⟨prime_of_mem_factors hp⟩, exact quadratic_char_is_quadratic (zmod p) a, end /-- The symbol `J(1 | b)` has the value `1`. -/ @[simp] lemma one_left (b : ℕ) : J(1 | b) = 1 := list.prod_eq_one (λ z hz, let ⟨p, hp, he⟩ := list.mem_pmap.1 hz in by rw [← he, legendre_sym.at_one]) /-- The Jacobi symbol is multiplicative in its first argument. -/ lemma mul_left (a₁ a₂ : ℤ) (b : ℕ) : J(a₁ * a₂ | b) = J(a₁ | b) * J(a₂ | b) := by { simp_rw [jacobi_sym, list.pmap_eq_map_attach, legendre_sym.mul], exact list.prod_map_mul } /-- The symbol `J(a | b)` vanishes iff `a` and `b` are not coprime (assuming `b ≠ 0`). -/ lemma eq_zero_iff_not_coprime {a : ℤ} {b : ℕ} [ne_zero b] : J(a | b) = 0 ↔ a.gcd b ≠ 1 := list.prod_eq_zero_iff.trans begin rw [list.mem_pmap, int.gcd_eq_nat_abs, ne, prime.not_coprime_iff_dvd], simp_rw [legendre_sym.eq_zero_iff, int_coe_zmod_eq_zero_iff_dvd, mem_factors (ne_zero.ne b), ← int.coe_nat_dvd_left, int.coe_nat_dvd, exists_prop, and_assoc, and_comm], end /-- The symbol `J(a | b)` is nonzero when `a` and `b` are coprime. -/ protected lemma ne_zero {a : ℤ} {b : ℕ} (h : a.gcd b = 1) : J(a | b) ≠ 0 := begin casesI eq_zero_or_ne_zero b with hb, { rw [hb, zero_right], exact one_ne_zero }, { contrapose! h, exact eq_zero_iff_not_coprime.1 h }, end /-- The symbol `J(a | b)` vanishes if and only if `b ≠ 0` and `a` and `b` are not coprime. -/ lemma eq_zero_iff {a : ℤ} {b : ℕ} : J(a | b) = 0 ↔ b ≠ 0 ∧ a.gcd b ≠ 1 := ⟨λ h, begin casesI eq_or_ne b 0 with hb hb, { rw [hb, zero_right] at h, cases h }, exact ⟨hb, mt jacobi_sym.ne_zero $ not_not.2 h⟩, end, λ ⟨hb, h⟩, by { rw ← ne_zero_iff at hb, exactI eq_zero_iff_not_coprime.2 h }⟩ /-- The symbol `J(0 | b)` vanishes when `b > 1`. -/ lemma zero_left {b : ℕ} (hb : 1 < b) : J(0 | b) = 0 := (@eq_zero_iff_not_coprime 0 b ⟨ne_zero_of_lt hb⟩).mpr $ by { rw [int.gcd_zero_left, int.nat_abs_of_nat], exact hb.ne' } /-- The symbol `J(a | b)` takes the value `1` or `-1` if `a` and `b` are coprime. -/ lemma eq_one_or_neg_one {a : ℤ} {b : ℕ} (h : a.gcd b = 1) : J(a | b) = 1 ∨ J(a | b) = -1 := (trichotomy a b).resolve_left $ jacobi_sym.ne_zero h /-- We have that `J(a^e | b) = J(a | b)^e`. -/ lemma pow_left (a : ℤ) (e b : ℕ) : J(a ^ e | b) = J(a | b) ^ e := nat.rec_on e (by rw [pow_zero, pow_zero, one_left]) $ λ _ ih, by rw [pow_succ, pow_succ, mul_left, ih] /-- We have that `J(a | b^e) = J(a | b)^e`. -/ lemma pow_right (a : ℤ) (b e : ℕ) : J(a | b ^ e) = J(a | b) ^ e := begin induction e with e ih, { rw [pow_zero, pow_zero, one_right], }, { casesI eq_zero_or_ne_zero b with hb, { rw [hb, zero_pow (succ_pos e), zero_right, one_pow], }, { rw [pow_succ, pow_succ, mul_right, ih], } } end /-- The square of `J(a | b)` is `1` when `a` and `b` are coprime. -/ lemma sq_one {a : ℤ} {b : ℕ} (h : a.gcd b = 1) : J(a | b) ^ 2 = 1 := by cases eq_one_or_neg_one h with h₁ h₁; rw h₁; refl /-- The symbol `J(a^2 | b)` is `1` when `a` and `b` are coprime. -/ lemma sq_one' {a : ℤ} {b : ℕ} (h : a.gcd b = 1) : J(a ^ 2 | b) = 1 := by rw [pow_left, sq_one h] /-- The symbol `J(a | b)` depends only on `a` mod `b`. -/ lemma mod_left (a : ℤ) (b : ℕ) : J(a | b) = J(a % b | b) := congr_arg list.prod $ list.pmap_congr _ begin rintro p hp _ _, conv_rhs { rw [legendre_sym.mod, int.mod_mod_of_dvd _ (int.coe_nat_dvd.2 $ dvd_of_mem_factors hp), ← legendre_sym.mod] }, end /-- The symbol `J(a | b)` depends only on `a` mod `b`. -/ lemma mod_left' {a₁ a₂ : ℤ} {b : ℕ} (h : a₁ % b = a₂ % b) : J(a₁ | b) = J(a₂ | b) := by rw [mod_left, h, ← mod_left] end jacobi_sym namespace zmod open jacobi_sym /-- If `J(a | b)` is `-1`, then `a` is not a square modulo `b`. -/ lemma nonsquare_of_jacobi_sym_eq_neg_one {a : ℤ} {b : ℕ} (h : J(a | b) = -1) : ¬ is_square (a : zmod b) := λ ⟨r, ha⟩, begin rw [← r.coe_val_min_abs, ← int.cast_mul, int_coe_eq_int_coe_iff', ← sq] at ha, apply (by norm_num : ¬ (0 : ℤ) ≤ -1), rw [← h, mod_left, ha, ← mod_left, pow_left], apply sq_nonneg, end /-- If `p` is prime, then `J(a | p)` is `-1` iff `a` is not a square modulo `p`. -/ lemma nonsquare_iff_jacobi_sym_eq_neg_one {a : ℤ} {p : ℕ} [fact p.prime] : J(a | p) = -1 ↔ ¬ is_square (a : zmod p) := by { rw [← legendre_sym.to_jacobi_sym], exact legendre_sym.eq_neg_one_iff p } /-- If `p` is prime and `J(a | p) = 1`, then `a` is q square mod `p`. -/ lemma is_square_of_jacobi_sym_eq_one {a : ℤ} {p : ℕ} [fact p.prime] (h : J(a | p) = 1) : is_square (a : zmod p) := not_not.mp $ by { rw [← nonsquare_iff_jacobi_sym_eq_neg_one, h], dec_trivial } end zmod /-! ### Values at `-1`, `2` and `-2` -/ namespace jacobi_sym /-- If `χ` is a multiplicative function such that `J(a | p) = χ p` for all odd primes `p`, then `J(a | b)` equals `χ b` for all odd natural numbers `b`. -/ lemma value_at (a : ℤ) {R : Type*} [comm_semiring R] (χ : R →* ℤ) (hp : ∀ (p : ℕ) (pp : p.prime) (h2 : p ≠ 2), @legendre_sym p ⟨pp⟩ a = χ p) {b : ℕ} (hb : odd b) : J(a | b) = χ b := begin conv_rhs { rw [← prod_factors hb.pos.ne', cast_list_prod, χ.map_list_prod] }, rw [jacobi_sym, list.map_map, ← list.pmap_eq_map nat.prime _ _ (λ _, prime_of_mem_factors)], congr' 1, apply list.pmap_congr, exact λ p h pp _, hp p pp (hb.ne_two_of_dvd_nat $ dvd_of_mem_factors h) end /-- If `b` is odd, then `J(-1 | b)` is given by `χ₄ b`. -/ lemma at_neg_one {b : ℕ} (hb : odd b) : J(-1 | b) = χ₄ b := value_at (-1) χ₄ (λ p pp, @legendre_sym.at_neg_one p ⟨pp⟩) hb /-- If `b` is odd, then `J(-a | b) = χ₄ b * J(a | b)`. -/ protected lemma neg (a : ℤ) {b : ℕ} (hb : odd b) : J(-a | b) = χ₄ b * J(a | b) := by rw [neg_eq_neg_one_mul, mul_left, at_neg_one hb] /-- If `b` is odd, then `J(2 | b)` is given by `χ₈ b`. -/ lemma at_two {b : ℕ} (hb : odd b) : J(2 | b) = χ₈ b := value_at 2 χ₈ (λ p pp, @legendre_sym.at_two p ⟨pp⟩) hb /-- If `b` is odd, then `J(-2 | b)` is given by `χ₈' b`. -/ lemma at_neg_two {b : ℕ} (hb : odd b) : J(-2 | b) = χ₈' b := value_at (-2) χ₈' (λ p pp, @legendre_sym.at_neg_two p ⟨pp⟩) hb end jacobi_sym /-! ### Quadratic Reciprocity -/ /-- The bi-multiplicative map giving the sign in the Law of Quadratic Reciprocity -/ def qr_sign (m n : ℕ) : ℤ := J(χ₄ m | n) namespace qr_sign /-- We can express `qr_sign m n` as a power of `-1` when `m` and `n` are odd. -/ lemma neg_one_pow {m n : ℕ} (hm : odd m) (hn : odd n) : qr_sign m n = (-1) ^ ((m / 2) * (n / 2)) := begin rw [qr_sign, pow_mul, ← χ₄_eq_neg_one_pow (odd_iff.mp hm)], cases odd_mod_four_iff.mp (odd_iff.mp hm) with h h, { rw [χ₄_nat_one_mod_four h, jacobi_sym.one_left, one_pow], }, { rw [χ₄_nat_three_mod_four h, ← χ₄_eq_neg_one_pow (odd_iff.mp hn), jacobi_sym.at_neg_one hn], } end /-- When `m` and `n` are odd, then the square of `qr_sign m n` is `1`. -/ lemma sq_eq_one {m n : ℕ} (hm : odd m) (hn : odd n) : (qr_sign m n) ^ 2 = 1 := by rw [neg_one_pow hm hn, ← pow_mul, mul_comm, pow_mul, neg_one_sq, one_pow] /-- `qr_sign` is multiplicative in the first argument. -/ lemma mul_left (m₁ m₂ n : ℕ) : qr_sign (m₁ * m₂) n = qr_sign m₁ n * qr_sign m₂ n := by simp_rw [qr_sign, nat.cast_mul, map_mul, jacobi_sym.mul_left] /-- `qr_sign` is multiplicative in the second argument. -/ lemma mul_right (m n₁ n₂ : ℕ) [ne_zero n₁] [ne_zero n₂] : qr_sign m (n₁ * n₂) = qr_sign m n₁ * qr_sign m n₂ := jacobi_sym.mul_right (χ₄ m) n₁ n₂ /-- `qr_sign` is symmetric when both arguments are odd. -/ protected lemma symm {m n : ℕ} (hm : odd m) (hn : odd n) : qr_sign m n = qr_sign n m := by rw [neg_one_pow hm hn, neg_one_pow hn hm, mul_comm (m / 2)] /-- We can move `qr_sign m n` from one side of an equality to the other when `m` and `n` are odd. -/ lemma eq_iff_eq {m n : ℕ} (hm : odd m) (hn : odd n) (x y : ℤ) : qr_sign m n * x = y ↔ x = qr_sign m n * y := by refine ⟨λ h', let h := h'.symm in _, λ h, _⟩; rw [h, ← mul_assoc, ← pow_two, sq_eq_one hm hn, one_mul] end qr_sign namespace jacobi_sym /-- The Law of Quadratic Reciprocity for the Jacobi symbol, version with `qr_sign` -/ lemma quadratic_reciprocity' {a b : ℕ} (ha : odd a) (hb : odd b) : J(a | b) = qr_sign b a * J(b | a) := begin -- define the right hand side for fixed `a` as a `ℕ →* ℤ` let rhs : ℕ → ℕ →* ℤ := λ a, { to_fun := λ x, qr_sign x a * J(x | a), map_one' := by { convert ← mul_one _, symmetry, all_goals { apply one_left } }, map_mul' := λ x y, by rw [qr_sign.mul_left, nat.cast_mul, mul_left, mul_mul_mul_comm] }, have rhs_apply : ∀ (a b : ℕ), rhs a b = qr_sign b a * J(b | a) := λ a b, rfl, refine value_at a (rhs a) (λ p pp hp, eq.symm _) hb, have hpo := pp.eq_two_or_odd'.resolve_left hp, rw [@legendre_sym.to_jacobi_sym p ⟨pp⟩, rhs_apply, nat.cast_id, qr_sign.eq_iff_eq hpo ha, qr_sign.symm hpo ha], refine value_at p (rhs p) (λ q pq hq, _) ha, have hqo := pq.eq_two_or_odd'.resolve_left hq, rw [rhs_apply, nat.cast_id, ← @legendre_sym.to_jacobi_sym p ⟨pp⟩, qr_sign.symm hqo hpo, qr_sign.neg_one_pow hpo hqo, @legendre_sym.quadratic_reciprocity' p q ⟨pp⟩ ⟨pq⟩ hp hq], end /-- The Law of Quadratic Reciprocity for the Jacobi symbol -/ lemma quadratic_reciprocity {a b : ℕ} (ha : odd a) (hb : odd b) : J(a | b) = (-1) ^ ((a / 2) * (b / 2)) * J(b | a) := by rw [← qr_sign.neg_one_pow ha hb, qr_sign.symm ha hb, quadratic_reciprocity' ha hb] /-- The Law of Quadratic Reciprocity for the Jacobi symbol: if `a` and `b` are natural numbers with `a % 4 = 1` and `b` odd, then `J(a | b) = J(b | a)`. -/ theorem quadratic_reciprocity_one_mod_four {a b : ℕ} (ha : a % 4 = 1) (hb : odd b) : J(a | b) = J(b | a) := by rw [quadratic_reciprocity (odd_iff.mpr (odd_of_mod_four_eq_one ha)) hb, pow_mul, neg_one_pow_div_two_of_one_mod_four ha, one_pow, one_mul] /-- The Law of Quadratic Reciprocity for the Jacobi symbol: if `a` and `b` are natural numbers with `a` odd and `b % 4 = 1`, then `J(a | b) = J(b | a)`. -/ theorem quadratic_reciprocity_one_mod_four' {a b : ℕ} (ha : odd a) (hb : b % 4 = 1) : J(a | b) = J(b | a) := (quadratic_reciprocity_one_mod_four hb ha).symm /-- The Law of Quadratic Reciprocityfor the Jacobi symbol: if `a` and `b` are natural numbers both congruent to `3` mod `4`, then `J(a | b) = -J(b | a)`. -/ theorem quadratic_reciprocity_three_mod_four {a b : ℕ} (ha : a % 4 = 3) (hb : b % 4 = 3) : J(a | b) = - J(b | a) := let nop := @neg_one_pow_div_two_of_three_mod_four in begin rw [quadratic_reciprocity, pow_mul, nop ha, nop hb, neg_one_mul]; rwa [odd_iff, odd_of_mod_four_eq_three], end /-- The Jacobi symbol `J(a | b)` depends only on `b` mod `4*a` (version for `a : ℕ`). -/ lemma mod_right' (a : ℕ) {b : ℕ} (hb : odd b) : J(a | b) = J(a | b % (4 * a)) := begin rcases eq_or_ne a 0 with rfl | ha₀, { rw [mul_zero, mod_zero], }, have hb' : odd (b % (4 * a)) := hb.mod_even (even.mul_right (by norm_num) _), rcases exists_eq_pow_mul_and_not_dvd ha₀ 2 (by norm_num) with ⟨e, a', ha₁', ha₂⟩, have ha₁ := odd_iff.mpr (two_dvd_ne_zero.mp ha₁'), nth_rewrite 1 [ha₂], nth_rewrite 0 [ha₂], rw [nat.cast_mul, mul_left, mul_left, quadratic_reciprocity' ha₁ hb, quadratic_reciprocity' ha₁ hb', nat.cast_pow, pow_left, pow_left, nat.cast_two, at_two hb, at_two hb'], congr' 1, swap, congr' 1, { simp_rw [qr_sign], rw [χ₄_nat_mod_four, χ₄_nat_mod_four (b % (4 * a)), mod_mod_of_dvd b (dvd_mul_right 4 a) ] }, { rw [mod_left ↑(b % _), mod_left b, int.coe_nat_mod, int.mod_mod_of_dvd b], simp only [ha₂, nat.cast_mul, ← mul_assoc], exact dvd_mul_left a' _, }, cases e, { refl }, { rw [χ₈_nat_mod_eight, χ₈_nat_mod_eight (b % (4 * a)), mod_mod_of_dvd b], use 2 ^ e * a', rw [ha₂, pow_succ], ring, } end /-- The Jacobi symbol `J(a | b)` depends only on `b` mod `4*a`. -/ lemma mod_right (a : ℤ) {b : ℕ} (hb : odd b) : J(a | b) = J(a | b % (4 * a.nat_abs)) := begin cases int.nat_abs_eq a with ha ha; nth_rewrite 1 [ha]; nth_rewrite 0 [ha], { exact mod_right' a.nat_abs hb, }, { have hb' : odd (b % (4 * a.nat_abs)) := hb.mod_even (even.mul_right (by norm_num) _), rw [jacobi_sym.neg _ hb, jacobi_sym.neg _ hb', mod_right' _ hb, χ₄_nat_mod_four, χ₄_nat_mod_four (b % (4 * _)), mod_mod_of_dvd b (dvd_mul_right 4 _)], } end end jacobi_sym end jacobi
5c64e78d5be303a6a4709e4263f7edfa9c4e934e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/subtype_instance.lean
5160037818792d837f67e36a5f044a840c44de1b
[ "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,434
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Provides a `subtype_instance` tactic which builds instances for algebraic substructures (sub-groups, sub-rings...). -/ import tactic.basic open tactic expr name list namespace tactic setup_tactic_parser open tactic.interactive (get_current_field refine_struct) /-- makes the substructure axiom name from field name, by postfacing with `_mem`-/ def mk_mem_name (sub : name) : name → name | (mk_string n _) := mk_string (n ++ "_mem") sub | n := n meta def derive_field_subtype : tactic unit := do field ← get_current_field, b ← target >>= is_prop, if b then do `[simp [subtype.ext_iff_val], dsimp [set.coe_eq_subtype]], intros, applyc field; assumption else do s ← find_local ``(set _), `(set %%α) ← infer_type s, e ← mk_const field, expl_arity ← get_expl_arity $ e α, xs ← (iota expl_arity).mmap $ λ _, intro1, args ← xs.mmap $ λ x, mk_app `subtype.val [x], hyps ← xs.mmap $ λ x, mk_app `subtype.property [x], val ← mk_app field args, subname ← local_context >>= list.mfirst (λ h, do (expr.const n _, args) ← get_app_fn_args <$> infer_type h, is_def_eq s args.ilast reducible, return n), mem_field ← resolve_constant $ mk_mem_name subname field, val_mem ← mk_app mem_field hyps, `(coe_sort %%s) <- target >>= instantiate_mvars, tactic.refine ``(@subtype.mk _ %%s %%val %%val_mem) namespace interactive /-- builds instances for algebraic substructures Example: ```lean variables {α : Type*} [monoid α] {s : set α} class is_submonoid (s : set α) : Prop := (one_mem : (1:α) ∈ s) (mul_mem {a b} : a ∈ s → b ∈ s → a * b ∈ s) instance subtype.monoid {s : set α} [is_submonoid s] : monoid s := by subtype_instance ``` -/ meta def subtype_instance := do t ← target, let cl := t.get_app_fn.const_name, src ← find_ancestors cl t.app_arg, let inst := pexpr.mk_structure_instance { struct := cl, field_values := [], field_names := [], sources := src.map to_pexpr }, refine_struct inst ; derive_field_subtype add_tactic_doc { name := "subtype_instance", category := doc_category.tactic, decl_names := [``subtype_instance], tags := ["type class", "structures"] } end interactive end tactic
90e8732016237b17b51f93f9b45896b9c4c21563
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/restrict_bug.lean
252a109db2a3f7e322b47a592a7e6f93630418a5
[ "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
157
lean
axiom all {A : Type}: list A → (A → Prop) → Prop variable {A : Type} variable {R : A → A → Prop} set_option pp.all true check ∀ a l, all l (R a)
0c9b51be3d4e1ef6f1f0feb6587c9b125a9b7e3c
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/category/Compactum.lean
68850283ad17d72dcc0946b2fb9b5bdf2b0e581d
[ "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
17,822
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.monad.types import category_theory.monad.limits import category_theory.equivalence import topology.category.CompHaus import topology.category.Profinite import data.set.constructions /-! # Compacta and Compact Hausdorff Spaces Recall that, given a monad `M` on `Type*`, an *algebra* for `M` consists of the following data: - A type `X : Type*` - A "structure" map `M X → X`. This data must also satisfy a distributivity and unit axiom, and algebras for `M` form a category in an evident way. See the file `category_theory.monad.algebra` for a general version, as well as the following link. https://ncatlab.org/nlab/show/monad This file proves the equivalence between the category of *compact Hausdorff topological spaces* and the category of algebras for the *ultrafilter monad*. ## Notation: Here are the main objects introduced in this file. - `Compactum` is the type of compacta, which we define as algebras for the ultrafilter monad. - `Compactum_to_CompHaus` is the functor `Compactum ⥤ CompHaus`. Here `CompHaus` is the usual category of compact Hausdorff spaces. - `Compactum_to_CompHaus.is_equivalence` is a term of type `is_equivalence Compactum_to_CompHaus`. The proof of this equivalence is a bit technical. But the idea is quite simply that the structure map `ultrafilter X → X` for an algebra `X` of the ultrafilter monad should be considered as the map sending an ultrafilter to its limit in `X`. The topology on `X` is then defined by mimicking the characterization of open sets in terms of ultrafilters. Any `X : Compactum` is endowed with a coercion to `Type*`, as well as the following instances: - `topological_space X`. - `compact_space X`. - `t2_space X`. Any morphism `f : X ⟶ Y` of is endowed with a coercion to a function `X → Y`, which is shown to be continuous in `continuous_of_hom`. The function `Compactum.of_topological_space` can be used to construct a `Compactum` from a topological space which satisfies `compact_space` and `t2_space`. We also add wrappers around structures which already exist. Here are the main ones, all in the `Compactum` namespace: - `forget : Compactum ⥤ Type*` is the forgetful functor, which induces a `concrete_category` instance for `Compactum`. - `free : Type* ⥤ Compactum` is the left adjoint to `forget`, and the adjunction is in `adj`. - `str : ultrafilter X → X` is the structure map for `X : Compactum`. The notation `X.str` is preferred. - `join : ultrafilter (ultrafilter X) → ultrafilter X` is the monadic join for `X : Compactum`. Again, the notation `X.join` is preferred. - `incl : X → ultrafilter X` is the unit for `X : Compactum`. The notation `X.incl` is preferred. ## References - E. Manes, Algebraic Theories, Graduate Texts in Mathematics 26, Springer-Verlag, 1976. - https://ncatlab.org/nlab/show/ultrafilter -/ universe u open category_theory filter ultrafilter topological_space category_theory.limits has_finite_inter open_locale classical topological_space local notation `β` := of_type_monad ultrafilter /-- The type `Compactum` of Compacta, defined as algebras for the ultrafilter monad. -/ @[derive [category, inhabited]] def Compactum := monad.algebra β namespace Compactum /-- The forgetful functor to Type* -/ @[derive [creates_limits,faithful]] def forget : Compactum ⥤ Type* := monad.forget _ /-- The "free" Compactum functor. -/ def free : Type* ⥤ Compactum := monad.free _ /-- The adjunction between `free` and `forget`. -/ def adj : free ⊣ forget := monad.adj _ -- Basic instances instance : concrete_category Compactum := { forget := forget } instance : has_coe_to_sort Compactum Type* := ⟨forget.obj⟩ instance {X Y : Compactum} : has_coe_to_fun (X ⟶ Y) (λ f, X → Y) := ⟨λ f, f.f⟩ instance : has_limits Compactum := has_limits_of_has_limits_creates_limits forget /-- The structure map for a compactum, essentially sending an ultrafilter to its limit. -/ def str (X : Compactum) : ultrafilter X → X := X.a /-- The monadic join. -/ def join (X : Compactum) : ultrafilter (ultrafilter X) → ultrafilter X := β .μ.app _ /-- The inclusion of `X` into `ultrafilter X`. -/ def incl (X : Compactum) : X → ultrafilter X := β .η.app _ @[simp] lemma str_incl (X : Compactum) (x : X) : X.str (X.incl x) = x := begin change (β .η.app _ ≫ X.a) _ = _, rw monad.algebra.unit, refl, end @[simp] lemma str_hom_commute (X Y : Compactum) (f : X ⟶ Y) (xs : ultrafilter X) : f (X.str xs) = Y.str (map f xs) := begin change (X.a ≫ f.f) _ = _, rw ←f.h, refl, end @[simp] lemma join_distrib (X : Compactum) (uux : ultrafilter (ultrafilter X)) : X.str (X.join uux) = X.str (map X.str uux) := begin change (β .μ.app _ ≫ X.a) _ = _, rw monad.algebra.assoc, refl, end instance {X : Compactum} : topological_space X := { is_open := λ U, ∀ (F : ultrafilter X), X.str F ∈ U → U ∈ F, is_open_univ := λ _ _, filter.univ_sets _, is_open_inter := λ S T h3 h4 h5 h6, filter.inter_sets _ (h3 _ h6.1) (h4 _ h6.2), is_open_sUnion := λ S h1 F ⟨T,hT,h2⟩, mem_of_superset (h1 T hT _ h2) (set.subset_sUnion_of_mem hT) } theorem is_closed_iff {X : Compactum} (S : set X) : is_closed S ↔ (∀ F : ultrafilter X, S ∈ F → X.str F ∈ S) := begin rw ← is_open_compl_iff, split, { intros cond F h, by_contradiction c, specialize cond F c, rw compl_mem_iff_not_mem at cond, contradiction }, { intros h1 F h2, specialize h1 F, cases F.mem_or_compl_mem S, exacts [absurd (h1 h) h2, h] } end instance {X : Compactum} : compact_space X := begin constructor, rw is_compact_iff_ultrafilter_le_nhds, intros F h, refine ⟨X.str F, by tauto, _⟩, rw le_nhds_iff, intros S h1 h2, exact h2 F h1 end /-- A local definition used only in the proofs. -/ private def basic {X : Compactum} (A : set X) : set (ultrafilter X) := {F | A ∈ F} /-- A local definition used only in the proofs. -/ private def cl {X : Compactum} (A : set X) : set X := X.str '' (basic A) private lemma basic_inter {X : Compactum} (A B : set X) : basic (A ∩ B) = basic A ∩ basic B := begin ext G, split, { intro hG, split; filter_upwards [hG] with _, exacts [and.left, and.right] }, { rintros ⟨h1, h2⟩, exact inter_mem h1 h2 } end private lemma subset_cl {X : Compactum} (A : set X) : A ⊆ cl A := λ a ha, ⟨X.incl a, ha,by simp⟩ private theorem cl_cl {X : Compactum} (A : set X) : cl (cl A) ⊆ cl A := begin rintros _ ⟨F,hF,rfl⟩, -- Notation to be used in this proof. let fsu := finset (set (ultrafilter X)), let ssu := set (set (ultrafilter X)), let ι : fsu → ssu := coe, let C0 : ssu := {Z | ∃ B ∈ F, X.str ⁻¹' B = Z}, let AA := {G : ultrafilter X | A ∈ G}, let C1 := insert AA C0, let C2 := finite_inter_closure C1, -- C0 is closed under intersections. have claim1 : ∀ B C ∈ C0, B ∩ C ∈ C0, { rintros B ⟨Q,hQ,rfl⟩ C ⟨R,hR,rfl⟩, use Q ∩ R, simp only [and_true, eq_self_iff_true, set.preimage_inter, subtype.val_eq_coe], exact inter_sets _ hQ hR }, -- All sets in C0 are nonempty. have claim2 : ∀ B ∈ C0, set.nonempty B, { rintros B ⟨Q,hQ,rfl⟩, obtain ⟨q⟩ := filter.nonempty_of_mem hQ, use X.incl q, simpa, }, -- The intersection of AA with every set in C0 is nonempty. have claim3 : ∀ B ∈ C0, (AA ∩ B).nonempty, { rintros B ⟨Q,hQ,rfl⟩, have : (Q ∩ cl A).nonempty := filter.nonempty_of_mem (inter_mem hQ hF), rcases this with ⟨q,hq1,P,hq2,hq3⟩, refine ⟨P,hq2,_⟩, rw ←hq3 at hq1, simpa }, -- Suffices to show that the intersection of any finite subcollection of C1 is nonempty. suffices : ∀ (T : fsu), ι T ⊆ C1 → (⋂₀ ι T).nonempty, { obtain ⟨G, h1⟩ := exists_ultrafilter_of_finite_inter_nonempty _ this, use X.join G, have : G.map X.str = F := ultrafilter.coe_le_coe.1 (λ S hS, h1 (or.inr ⟨S, hS, rfl⟩)), rw [join_distrib, this], exact ⟨h1 (or.inl rfl), rfl⟩ }, -- C2 is closed under finite intersections (by construction!). have claim4 := finite_inter_closure_has_finite_inter C1, -- C0 is closed under finite intersections by claim1. have claim5 : has_finite_inter C0 := ⟨⟨_, univ_mem, set.preimage_univ⟩, claim1⟩, -- Every element of C2 is nonempty. have claim6 : ∀ P ∈ C2, (P : set (ultrafilter X)).nonempty, { suffices : ∀ P ∈ C2, P ∈ C0 ∨ ∃ Q ∈ C0, P = AA ∩ Q, { intros P hP, cases this P hP, { exact claim2 _ h }, { rcases h with ⟨Q, hQ, rfl⟩, exact claim3 _ hQ } }, intros P hP, exact claim5.finite_inter_closure_insert _ hP }, intros T hT, -- Suffices to show that the intersection of the T's is contained in C2. suffices : ⋂₀ ι T ∈ C2, by exact claim6 _ this, -- Finish apply claim4.finite_inter_mem, intros t ht, exact finite_inter_closure.basic (@hT t ht), end lemma is_closed_cl {X : Compactum} (A : set X) : is_closed (cl A) := begin rw is_closed_iff, intros F hF, exact cl_cl _ ⟨F, hF, rfl⟩, end lemma str_eq_of_le_nhds {X : Compactum} (F : ultrafilter X) (x : X) : ↑F ≤ 𝓝 x → X.str F = x := begin -- Notation to be used in this proof. let fsu := finset (set (ultrafilter X)), let ssu := set (set (ultrafilter X)), let ι : fsu → ssu := coe, let T0 : ssu := { S | ∃ A ∈ F, S = basic A }, let AA := (X.str ⁻¹' {x}), let T1 := insert AA T0, let T2 := finite_inter_closure T1, intro cond, -- If F contains a closed set A, then x is contained in A. have claim1 : ∀ (A : set X), is_closed A → A ∈ F → x ∈ A, { intros A hA h, by_contradiction H, rw le_nhds_iff at cond, specialize cond Aᶜ H hA.is_open_compl, rw [ultrafilter.mem_coe, ultrafilter.compl_mem_iff_not_mem] at cond, contradiction }, -- If A ∈ F, then x ∈ cl A. have claim2 : ∀ (A : set X), A ∈ F → x ∈ cl A, { intros A hA, exact claim1 (cl A) (is_closed_cl A) (mem_of_superset hA (subset_cl A)) }, -- T0 is closed under intersections. have claim3 : ∀ (S1 S2 ∈ T0), S1 ∩ S2 ∈ T0, { rintros S1 ⟨S1, hS1, rfl⟩ S2 ⟨S2, hS2, rfl⟩, exact ⟨S1 ∩ S2, inter_mem hS1 hS2, by simp [basic_inter]⟩ }, -- For every S ∈ T0, the intersection AA ∩ S is nonempty. have claim4 : ∀ (S ∈ T0), (AA ∩ S).nonempty, { rintros S ⟨S, hS, rfl⟩, rcases claim2 _ hS with ⟨G, hG, hG2⟩, exact ⟨G, hG2, hG⟩ }, -- Every element of T0 is nonempty. have claim5 : ∀ (S ∈ T0), set.nonempty S, { rintros S ⟨S, hS, rfl⟩, exact ⟨F, hS⟩ }, -- Every element of T2 is nonempty. have claim6 : ∀ (S ∈ T2), set.nonempty S, { suffices : ∀ S ∈ T2, S ∈ T0 ∨ ∃ Q ∈ T0, S = AA ∩ Q, { intros S hS, cases this _ hS with h h, { exact claim5 S h }, { rcases h with ⟨Q, hQ, rfl⟩, exact claim4 Q hQ } }, intros S hS, apply finite_inter_closure_insert, { split, { use set.univ, refine ⟨filter.univ_sets _, _⟩, ext, refine ⟨_, by tauto⟩, { intro, apply filter.univ_sets, } }, { exact claim3 } }, { exact hS} }, -- It suffices to show that the intersection of any finite subset of T1 is nonempty. suffices : ∀ (F : fsu), ↑F ⊆ T1 → (⋂₀ ι F).nonempty, { obtain ⟨G,h1⟩ := ultrafilter.exists_ultrafilter_of_finite_inter_nonempty _ this, have c1 : X.join G = F := ultrafilter.coe_le_coe.1 (λ P hP, h1 (or.inr ⟨P, hP, rfl⟩)), have c2 : G.map X.str = X.incl x, { refine ultrafilter.coe_le_coe.1 (λ P hP, _), apply mem_of_superset (h1 (or.inl rfl)), rintros x ⟨rfl⟩, exact hP }, simp [←c1, c2] }, -- Finish... intros T hT, refine claim6 _ (finite_inter_mem (finite_inter_closure_has_finite_inter _) _ _), intros t ht, exact finite_inter_closure.basic (@hT t ht) end lemma le_nhds_of_str_eq {X : Compactum} (F : ultrafilter X) (x : X) : X.str F = x → ↑F ≤ 𝓝 x := λ h, le_nhds_iff.mpr (λ s hx hs, hs _ $ by rwa h) -- All the hard work above boils down to this t2_space instance. instance {X : Compactum} : t2_space X := begin rw t2_iff_ultrafilter, intros _ _ F hx hy, rw [← str_eq_of_le_nhds _ _ hx, ← str_eq_of_le_nhds _ _ hy] end /-- The structure map of a compactum actually computes limits. -/ lemma Lim_eq_str {X : Compactum} (F : ultrafilter X) : F.Lim = X.str F := begin rw [ultrafilter.Lim_eq_iff_le_nhds, le_nhds_iff], tauto, end lemma cl_eq_closure {X : Compactum} (A : set X) : cl A = closure A := begin ext, rw mem_closure_iff_ultrafilter, split, { rintro ⟨F, h1, h2⟩, exact ⟨F, h1, le_nhds_of_str_eq _ _ h2⟩ }, { rintro ⟨F, h1, h2⟩, exact ⟨F, h1, str_eq_of_le_nhds _ _ h2⟩ } end /-- Any morphism of compacta is continuous. -/ lemma continuous_of_hom {X Y : Compactum} (f : X ⟶ Y) : continuous f := begin rw continuous_iff_ultrafilter, intros x _ h, rw [tendsto, ← coe_map], apply le_nhds_of_str_eq, rw [← str_hom_commute, str_eq_of_le_nhds _ x h] end /-- Given any compact Hausdorff space, we construct a Compactum. -/ noncomputable def of_topological_space (X : Type*) [topological_space X] [compact_space X] [t2_space X] : Compactum := { A := X, a := ultrafilter.Lim, unit' := by {ext x, exact Lim_eq (pure_le_nhds _) }, assoc' := begin ext FF, change ultrafilter (ultrafilter X) at FF, set x := (ultrafilter.map ultrafilter.Lim FF).Lim with c1, have c2 : ∀ (U : set X) (F : ultrafilter X), F.Lim ∈ U → is_open U → U ∈ F, { intros U F h1 hU, exact c1 ▸ is_open_iff_ultrafilter.mp hU _ h1 _ (ultrafilter.le_nhds_Lim _) }, have c3 : ↑(ultrafilter.map ultrafilter.Lim FF) ≤ 𝓝 x, { rw le_nhds_iff, intros U hx hU, exact mem_coe.2 (c2 _ _ (by rwa ← c1) hU) }, have c4 : ∀ (U : set X), x ∈ U → is_open U → { G : ultrafilter X | U ∈ G } ∈ FF, { intros U hx hU, suffices : ultrafilter.Lim ⁻¹' U ∈ FF, { apply mem_of_superset this, intros P hP, exact c2 U P hP hU }, exact @c3 U (is_open.mem_nhds hU hx) }, apply Lim_eq, rw le_nhds_iff, exact c4, end } /-- Any continuous map between Compacta is a morphism of compacta. -/ def hom_of_continuous {X Y : Compactum} (f : X → Y) (cont : continuous f) : X ⟶ Y := { f := f, h' := begin rw continuous_iff_ultrafilter at cont, ext (F : ultrafilter X), specialize cont (X.str F) F (le_nhds_of_str_eq F (X.str F) rfl), have := str_eq_of_le_nhds (ultrafilter.map f F) _ cont, simpa only [←this, types_comp_apply, of_type_functor_map], end } end Compactum /-- The functor functor from Compactum to CompHaus. -/ def Compactum_to_CompHaus : Compactum ⥤ CompHaus := { obj := λ X, { to_Top := { α := X } }, map := λ X Y f, { to_fun := f, continuous_to_fun := Compactum.continuous_of_hom _ }} namespace Compactum_to_CompHaus /-- The functor Compactum_to_CompHaus is full. -/ def full : full Compactum_to_CompHaus.{u} := { preimage := λ X Y f, Compactum.hom_of_continuous f.1 f.2 } /-- The functor Compactum_to_CompHaus is faithful. -/ lemma faithful : faithful Compactum_to_CompHaus := {} /-- This definition is used to prove essential surjectivity of Compactum_to_CompHaus. -/ def iso_of_topological_space {D : CompHaus} : Compactum_to_CompHaus.obj (Compactum.of_topological_space D) ≅ D := { hom := { to_fun := id, continuous_to_fun := continuous_def.2 $ λ _ h, by {rw is_open_iff_ultrafilter' at h, exact h} }, inv := { to_fun := id, continuous_to_fun := continuous_def.2 $ λ _ h1, by {rw is_open_iff_ultrafilter', intros _ h2, exact h1 _ h2} } } /-- The functor Compactum_to_CompHaus is essentially surjective. -/ lemma ess_surj : ess_surj Compactum_to_CompHaus := { mem_ess_image := λ X, ⟨Compactum.of_topological_space X, ⟨iso_of_topological_space⟩⟩ } /-- The functor Compactum_to_CompHaus is an equivalence of categories. -/ noncomputable instance is_equivalence : is_equivalence Compactum_to_CompHaus := begin apply equivalence.of_fully_faithfully_ess_surj _, exact Compactum_to_CompHaus.full, exact Compactum_to_CompHaus.faithful, exact Compactum_to_CompHaus.ess_surj, end end Compactum_to_CompHaus /-- The forgetful functors of `Compactum` and `CompHaus` are compatible via `Compactum_to_CompHaus`. -/ def Compactum_to_CompHaus_comp_forget : Compactum_to_CompHaus ⋙ category_theory.forget CompHaus ≅ Compactum.forget := nat_iso.of_components (λ X, eq_to_iso rfl) $ by { intros X Y f, dsimp, simpa } /- TODO: `forget CompHaus` is monadic, as it is isomorphic to the composition of an equivalence with the monadic functor `forget Compactum`. Once we have the API to transfer monadicity of functors along such isomorphisms, the instance `creates_limits (forget CompHaus)` can be deduced from this monadicity. -/ noncomputable instance CompHaus.forget_creates_limits : creates_limits (forget CompHaus) := begin let e : forget CompHaus ≅ Compactum_to_CompHaus.inv ⋙ Compactum.forget := _ ≪≫ iso_whisker_left _ Compactum_to_CompHaus_comp_forget, swap, refine _ ≪≫ functor.associator _ _ _, refine (functor.left_unitor _).symm ≪≫ _, refine iso_whisker_right _ _, exact Compactum_to_CompHaus.as_equivalence.symm.unit_iso, exact creates_limits_of_nat_iso e.symm, end noncomputable instance Profinite.forget_creates_limits : creates_limits (forget Profinite) := begin change creates_limits (Profinite_to_CompHaus ⋙ forget _), apply_instance, end
9f4028cda4a6e29ecc5a3dc0990dbdce9a9764e7
bf532e3e865883a676110e756f800e0ddeb465be
/category/basic.lean
12b64227d15fb60a7137f9fdc6faac67ba57e208
[ "Apache-2.0" ]
permissive
aqjune/mathlib
da42a97d9e6670d2efaa7d2aa53ed3585dafc289
f7977ff5a6bcf7e5c54eec908364ceb40dafc795
refs/heads/master
1,631,213,225,595
1,521,089,840,000
1,521,089,840,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,242
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 Extends the theory on functors, applicatives and monads. -/ universes u v w x y variables {α β γ : Type u} section applicative variables {f : Type u → Type v} [applicative f] lemma pure_seq_eq_map : ∀ {α β : Type u} (g : α → β) (x : f α), pure g <*> x = g <$> x := @applicative.pure_seq_eq_map f _ end applicative section monad variables {m : Type u → Type v} [monad m] lemma map_bind (x : m α) {g : α → m β} {f : β → γ} : f <$> (x >>= g) = (x >>= λa, f <$> g a) := by simp [monad.bind_assoc, (∘), (monad.bind_pure_comp_eq_map _ _ _).symm] lemma seq_bind_eq (x : m α) {g : β → m γ} {f : α → β} : (f <$> x) >>= g = (x >>= g ∘ f) := show bind (f <$> x) g = bind x (g ∘ f), by rw [←monad.bind_pure_comp_eq_map, monad.bind_assoc]; simp [monad.pure_bind] lemma seq_eq_bind_map {x : m α} {f : m (α → β)} : f <*> x = (f >>= (<$> x)) := (monad.bind_map_eq_seq m f x).symm lemma bind_assoc : ∀ {α β γ : Type u} (x : m α) (f : α → m β) (g : β → m γ), x >>= f >>= g = x >>= λ x, f x >>= g := @monad.bind_assoc m _ end monad
f151e822d54bdfb18e154cc7becd600144d65736
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/strInterpolation.lean
5f51880ea478f4c10c63ea8a995c994b8953b966
[ "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
372
lean
#eval s!"hello {1+1}" def tst (x : Nat) : IO Unit := do IO.println s!"x: {x}" IO.println s!"x+1: {x+1}" #eval tst 10 #eval s!"{1}+{1}" #eval s!"\{{1+1}}" #eval s!"a{1}" def g (x : Nat) : StateRefT Nat IO Nat := do modify (· + x) get def ex : StateRefT Nat IO Unit := do IO.println s!">> hello {(←g 1)}" IO.println s!">> world {(←g 1)}" pure () #eval ex.run' 0
0e22cd3a5f7fa8b51268eafe6b2a388c43930941
3863d2564418bccb1859e057bf5a4ef240e75fd7
/hott/homotopy/complex_hopf.hlean
038695919f9528ce165ec3dd7b69e672b003a629
[ "Apache-2.0" ]
permissive
JacobGross/lean
118bbb067ff4d4af48a266face2c7eb9868fa91c
eb26087df940c54337cb807b4bc6d345d1fc1085
refs/heads/master
1,582,735,011,532
1,462,557,826,000
1,462,557,826,000
46,451,196
0
0
null
1,462,557,826,000
1,447,885,161,000
C++
UTF-8
Lean
false
false
1,104
hlean
/- Copyright (c) 2016 Ulrik Buchholtz and Egbert Rijke. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ulrik Buchholtz, Egbert Rijke The H-space structure on S¹ and the complex Hopf fibration (the standard one). -/ import .hopf .circle open eq equiv is_equiv circle is_conn trunc is_trunc sphere_index sphere susp namespace hopf definition circle_h_space [instance] : h_space S¹ := ⦃ h_space, one := base, mul := circle_mul, one_mul := circle_base_mul, mul_one := circle_mul_base ⦄ definition circle_assoc (x y z : S¹) : (x * y) * z = x * (y * z) := begin induction x, { reflexivity }, { apply eq_pathover, induction y, { exact natural_square_tr (λa : S¹, ap (λb : S¹, b * z) (circle_mul_base a)) loop }, { apply is_prop.elimo, apply is_trunc_square } } end open sphere.ops function definition complex_hopf : S 3 → S 2 := begin intro x, apply @sigma.pr1 (susp S¹) (hopf S¹), apply inv (hopf.total S¹), apply inv (join.spheres 1 1), exact x end end hopf
0db7fa7d05898634991f6482920f9991e7d41393
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/limits/cones.lean
d08eed316f623ca817b6123b6d7629dbd5769be3
[ "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
30,953
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.functor.const import category_theory.discrete_category import category_theory.yoneda import category_theory.functor.reflects_isomorphisms /-! # Cones and cocones We define `cone F`, a cone over a functor `F`, and `F.cones : Cᵒᵖ ⥤ Type`, the functor associating to `X` the cones over `F` with cone point `X`. A cone `c` is defined by specifying its cone point `c.X` and a natural transformation `c.π` from the constant `c.X` valued functor to `F`. We provide `c.w f : c.π.app j ≫ F.map f = c.π.app j'` for any `f : j ⟶ j'` as a wrapper for `c.π.naturality f` avoiding unneeded identity morphisms. We define `c.extend f`, where `c : cone F` and `f : Y ⟶ c.X` for some other `Y`, which replaces the cone point by `Y` and inserts `f` into each of the components of the cone. Similarly we have `c.whisker F` producing a `cone (E ⋙ F)` We define morphisms of cones, and the category of cones. We define `cone.postcompose α : cone F ⥤ cone G` for `α` a natural transformation `F ⟶ G`. And, of course, we dualise all this to cocones as well. -/ -- morphism levels before object levels. See note [category_theory universes]. universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ open category_theory variables {J : Type u₁} [category.{v₁} J] variables {K : Type u₂} [category.{v₂} K] variables {C : Type u₃} [category.{v₃} C] variables {D : Type u₄} [category.{v₄} D] open category_theory open category_theory.category open category_theory.functor open opposite namespace category_theory namespace functor variables {J C} (F : J ⥤ C) /-- `F.cones` is the functor assigning to an object `X` the type of natural transformations from the constant functor with value `X` to `F`. An object representing this functor is a limit of `F`. -/ @[simps] def cones : Cᵒᵖ ⥤ Type (max u₁ v₃) := (const J).op ⋙ yoneda.obj F /-- `F.cocones` is the functor assigning to an object `X` the type of natural transformations from `F` to the constant functor with value `X`. An object corepresenting this functor is a colimit of `F`. -/ @[simps] def cocones : C ⥤ Type (max u₁ v₃) := const J ⋙ coyoneda.obj (op F) end functor section variables (J C) /-- Functorially associated to each functor `J ⥤ C`, we have the `C`-presheaf consisting of cones with a given cone point. -/ @[simps] def cones : (J ⥤ C) ⥤ (Cᵒᵖ ⥤ Type (max u₁ v₃)) := { obj := functor.cones, map := λ F G f, whisker_left (const J).op (yoneda.map f) } /-- Contravariantly associated to each functor `J ⥤ C`, we have the `C`-copresheaf consisting of cocones with a given cocone point. -/ @[simps] def cocones : (J ⥤ C)ᵒᵖ ⥤ (C ⥤ Type (max u₁ v₃)) := { obj := λ F, functor.cocones (unop F), map := λ F G f, whisker_left (const J) (coyoneda.map f) } end namespace limits section local attribute [tidy] tactic.discrete_cases /-- A `c : cone F` is: * an object `c.X` and * a natural transformation `c.π : c.X ⟶ F` from the constant `c.X` functor to `F`. `cone F` is equivalent, via `cone.equiv` below, to `Σ X, F.cones.obj X`. -/ structure cone (F : J ⥤ C) := (X : C) (π : (const J).obj X ⟶ F) instance inhabited_cone (F : discrete punit ⥤ C) : inhabited (cone F) := ⟨{ X := F.obj ⟨⟨⟩⟩, π := { app := λ ⟨⟨⟩⟩, 𝟙 _, }, }⟩ @[simp, reassoc] lemma cone.w {F : J ⥤ C} (c : cone F) {j j' : J} (f : j ⟶ j') : c.π.app j ≫ F.map f = c.π.app j' := by { rw ← c.π.naturality f, apply id_comp } /-- A `c : cocone F` is * an object `c.X` and * a natural transformation `c.ι : F ⟶ c.X` from `F` to the constant `c.X` functor. `cocone F` is equivalent, via `cone.equiv` below, to `Σ X, F.cocones.obj X`. -/ structure cocone (F : J ⥤ C) := (X : C) (ι : F ⟶ (const J).obj X) instance inhabited_cocone (F : discrete punit ⥤ C) : inhabited (cocone F) := ⟨{ X := F.obj ⟨⟨⟩⟩, ι := { app := λ ⟨⟨⟩⟩, 𝟙 _, }, }⟩ @[simp, reassoc] lemma cocone.w {F : J ⥤ C} (c : cocone F) {j j' : J} (f : j ⟶ j') : F.map f ≫ c.ι.app j' = c.ι.app j := by { rw c.ι.naturality f, apply comp_id } end variables {F : J ⥤ C} namespace cone /-- The isomorphism between a cone on `F` and an element of the functor `F.cones`. -/ @[simps] def equiv (F : J ⥤ C) : cone F ≅ Σ X, F.cones.obj X := { hom := λ c, ⟨op c.X, c.π⟩, inv := λ c, { X := c.1.unop, π := c.2 }, hom_inv_id' := by { ext1, cases x, refl }, inv_hom_id' := by { ext1, cases x, refl } } /-- A map to the vertex of a cone naturally induces a cone by composition. -/ @[simps] def extensions (c : cone F) : yoneda.obj c.X ⋙ ulift_functor.{u₁} ⟶ F.cones := { app := λ X f, (const J).map f.down ≫ c.π } /-- A map to the vertex of a cone induces a cone by composition. -/ @[simps] def extend (c : cone F) {X : C} (f : X ⟶ c.X) : cone F := { X := X, π := c.extensions.app (op X) ⟨f⟩ } /-- Whisker a cone by precomposition of a functor. -/ @[simps] def whisker (E : K ⥤ J) (c : cone F) : cone (E ⋙ F) := { X := c.X, π := whisker_left E c.π } end cone namespace cocone /-- The isomorphism between a cocone on `F` and an element of the functor `F.cocones`. -/ def equiv (F : J ⥤ C) : cocone F ≅ Σ X, F.cocones.obj X := { hom := λ c, ⟨c.X, c.ι⟩, inv := λ c, { X := c.1, ι := c.2 }, hom_inv_id' := by { ext1, cases x, refl }, inv_hom_id' := by { ext1, cases x, refl } } /-- A map from the vertex of a cocone naturally induces a cocone by composition. -/ @[simps] def extensions (c : cocone F) : coyoneda.obj (op c.X) ⋙ ulift_functor.{u₁} ⟶ F.cocones := { app := λ X f, c.ι ≫ (const J).map f.down } /-- A map from the vertex of a cocone induces a cocone by composition. -/ @[simps] def extend (c : cocone F) {X : C} (f : c.X ⟶ X) : cocone F := { X := X, ι := c.extensions.app X ⟨f⟩ } /-- Whisker a cocone by precomposition of a functor. See `whiskering` for a functorial version. -/ @[simps] def whisker (E : K ⥤ J) (c : cocone F) : cocone (E ⋙ F) := { X := c.X, ι := whisker_left E c.ι } end cocone /-- A cone morphism between two cones for the same diagram is a morphism of the cone points which commutes with the cone legs. -/ @[ext] structure cone_morphism (A B : cone F) := (hom : A.X ⟶ B.X) (w' : ∀ j : J, hom ≫ B.π.app j = A.π.app j . obviously) restate_axiom cone_morphism.w' attribute [simp, reassoc] cone_morphism.w instance inhabited_cone_morphism (A : cone F) : inhabited (cone_morphism A A) := ⟨{ hom := 𝟙 _ }⟩ /-- The category of cones on a given diagram. -/ @[simps] instance cone.category : category (cone F) := { hom := λ A B, cone_morphism A B, comp := λ X Y Z f g, { hom := f.hom ≫ g.hom }, id := λ B, { hom := 𝟙 B.X } } namespace cones /-- To give an isomorphism between cones, it suffices to give an isomorphism between their vertices which commutes with the cone maps. -/ @[ext, simps] def ext {c c' : cone F} (φ : c.X ≅ c'.X) (w : ∀ j, c.π.app j = φ.hom ≫ c'.π.app j) : c ≅ c' := { hom := { hom := φ.hom }, inv := { hom := φ.inv, w' := λ j, φ.inv_comp_eq.mpr (w j) } } /-- Given a cone morphism whose object part is an isomorphism, produce an isomorphism of cones. -/ lemma cone_iso_of_hom_iso {K : J ⥤ C} {c d : cone K} (f : c ⟶ d) [i : is_iso f.hom] : is_iso f := ⟨⟨{ hom := inv f.hom, w' := λ j, (as_iso f.hom).inv_comp_eq.2 (f.w j).symm }, by tidy⟩⟩ /-- Functorially postcompose a cone for `F` by a natural transformation `F ⟶ G` to give a cone for `G`. -/ @[simps] def postcompose {G : J ⥤ C} (α : F ⟶ G) : cone F ⥤ cone G := { obj := λ c, { X := c.X, π := c.π ≫ α }, map := λ c₁ c₂ f, { hom := f.hom } } /-- Postcomposing a cone by the composite natural transformation `α ≫ β` is the same as postcomposing by `α` and then by `β`. -/ @[simps] def postcompose_comp {G H : J ⥤ C} (α : F ⟶ G) (β : G ⟶ H) : postcompose (α ≫ β) ≅ postcompose α ⋙ postcompose β := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy) /-- Postcomposing by the identity does not change the cone up to isomorphism. -/ @[simps] def postcompose_id : postcompose (𝟙 F) ≅ 𝟭 (cone F) := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy) /-- If `F` and `G` are naturally isomorphic functors, then they have equivalent categories of cones. -/ @[simps] def postcompose_equivalence {G : J ⥤ C} (α : F ≅ G) : cone F ≌ cone G := { functor := postcompose α.hom, inverse := postcompose α.inv, unit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy) } /-- Whiskering on the left by `E : K ⥤ J` gives a functor from `cone F` to `cone (E ⋙ F)`. -/ @[simps] def whiskering (E : K ⥤ J) : cone F ⥤ cone (E ⋙ F) := { obj := λ c, c.whisker E, map := λ c c' f, { hom := f.hom } } /-- Whiskering by an equivalence gives an equivalence between categories of cones. -/ @[simps] def whiskering_equivalence (e : K ≌ J) : cone F ≌ cone (e.functor ⋙ F) := { functor := whiskering e.functor, inverse := whiskering e.inverse ⋙ postcompose (e.inv_fun_id_assoc F).hom, unit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (begin intro k, dsimp, -- See library note [dsimp, simp] simpa [e.counit_app_functor] using s.w (e.unit_inv.app k), end)) (by tidy), } /-- The categories of cones over `F` and `G` are equivalent if `F` and `G` are naturally isomorphic (possibly after changing the indexing category by an equivalence). -/ @[simps functor inverse unit_iso counit_iso] def equivalence_of_reindexing {G : K ⥤ C} (e : K ≌ J) (α : e.functor ⋙ F ≅ G) : cone F ≌ cone G := (whiskering_equivalence e).trans (postcompose_equivalence α) section variable (F) /-- Forget the cone structure and obtain just the cone point. -/ @[simps] def forget : cone F ⥤ C := { obj := λ t, t.X, map := λ s t f, f.hom } variables (G : C ⥤ D) /-- A functor `G : C ⥤ D` sends cones over `F` to cones over `F ⋙ G` functorially. -/ @[simps] def functoriality : cone F ⥤ cone (F ⋙ G) := { obj := λ A, { X := G.obj A.X, π := { app := λ j, G.map (A.π.app j), naturality' := by intros; erw ←G.map_comp; tidy } }, map := λ X Y f, { hom := G.map f.hom, w' := λ j, by simp [-cone_morphism.w, ←f.w j] } } instance functoriality_full [full G] [faithful G] : full (functoriality F G) := { preimage := λ X Y t, { hom := G.preimage t.hom, w' := λ j, G.map_injective (by simpa using t.w j) } } instance functoriality_faithful [faithful G] : faithful (cones.functoriality F G) := { map_injective' := λ X Y f g e, by { ext1, injection e, apply G.map_injective h_1 } } /-- If `e : C ≌ D` is an equivalence of categories, then `functoriality F e.functor` induces an equivalence between cones over `F` and cones over `F ⋙ e.functor`. -/ @[simps] def functoriality_equivalence (e : C ≌ D) : cone F ≌ cone (F ⋙ e.functor) := let f : (F ⋙ e.functor) ⋙ e.inverse ≅ F := functor.associator _ _ _ ≪≫ iso_whisker_left _ (e.unit_iso).symm ≪≫ functor.right_unitor _ in { functor := functoriality F e.functor, inverse := (functoriality (F ⋙ e.functor) e.inverse) ⋙ (postcompose_equivalence f).functor, unit_iso := nat_iso.of_components (λ c, cones.ext (e.unit_iso.app _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ c, cones.ext (e.counit_iso.app _) (by tidy)) (by tidy), } /-- If `F` reflects isomorphisms, then `cones.functoriality F` reflects isomorphisms as well. -/ instance reflects_cone_isomorphism (F : C ⥤ D) [reflects_isomorphisms F] (K : J ⥤ C) : reflects_isomorphisms (cones.functoriality K F) := begin constructor, introsI, haveI : is_iso (F.map f.hom) := (cones.forget (K ⋙ F)).map_is_iso ((cones.functoriality K F).map f), haveI := reflects_isomorphisms.reflects F f.hom, apply cone_iso_of_hom_iso end end end cones /-- A cocone morphism between two cocones for the same diagram is a morphism of the cocone points which commutes with the cocone legs. -/ @[ext] structure cocone_morphism (A B : cocone F) := (hom : A.X ⟶ B.X) (w' : ∀ j : J, A.ι.app j ≫ hom = B.ι.app j . obviously) instance inhabited_cocone_morphism (A : cocone F) : inhabited (cocone_morphism A A) := ⟨{ hom := 𝟙 _ }⟩ restate_axiom cocone_morphism.w' attribute [simp, reassoc] cocone_morphism.w @[simps] instance cocone.category : category (cocone F) := { hom := λ A B, cocone_morphism A B, comp := λ _ _ _ f g, { hom := f.hom ≫ g.hom }, id := λ B, { hom := 𝟙 B.X } } namespace cocones /-- To give an isomorphism between cocones, it suffices to give an isomorphism between their vertices which commutes with the cocone maps. -/ @[ext, simps] def ext {c c' : cocone F} (φ : c.X ≅ c'.X) (w : ∀ j, c.ι.app j ≫ φ.hom = c'.ι.app j) : c ≅ c' := { hom := { hom := φ.hom }, inv := { hom := φ.inv, w' := λ j, φ.comp_inv_eq.mpr (w j).symm } } /-- Given a cocone morphism whose object part is an isomorphism, produce an isomorphism of cocones. -/ lemma cocone_iso_of_hom_iso {K : J ⥤ C} {c d : cocone K} (f : c ⟶ d) [i : is_iso f.hom] : is_iso f := ⟨⟨{ hom := inv f.hom, w' := λ j, (as_iso f.hom).comp_inv_eq.2 (f.w j).symm }, by tidy⟩⟩ /-- Functorially precompose a cocone for `F` by a natural transformation `G ⟶ F` to give a cocone for `G`. -/ @[simps] def precompose {G : J ⥤ C} (α : G ⟶ F) : cocone F ⥤ cocone G := { obj := λ c, { X := c.X, ι := α ≫ c.ι }, map := λ c₁ c₂ f, { hom := f.hom } } /-- Precomposing a cocone by the composite natural transformation `α ≫ β` is the same as precomposing by `β` and then by `α`. -/ def precompose_comp {G H : J ⥤ C} (α : F ⟶ G) (β : G ⟶ H) : precompose (α ≫ β) ≅ precompose β ⋙ precompose α := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy) /-- Precomposing by the identity does not change the cocone up to isomorphism. -/ def precompose_id : precompose (𝟙 F) ≅ 𝟭 (cocone F) := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy) /-- If `F` and `G` are naturally isomorphic functors, then they have equivalent categories of cocones. -/ @[simps] def precompose_equivalence {G : J ⥤ C} (α : G ≅ F) : cocone F ≌ cocone G := { functor := precompose α.hom, inverse := precompose α.inv, unit_iso := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy) } /-- Whiskering on the left by `E : K ⥤ J` gives a functor from `cocone F` to `cocone (E ⋙ F)`. -/ @[simps] def whiskering (E : K ⥤ J) : cocone F ⥤ cocone (E ⋙ F) := { obj := λ c, c.whisker E, map := λ c c' f, { hom := f.hom, } } /-- Whiskering by an equivalence gives an equivalence between categories of cones. -/ @[simps] def whiskering_equivalence (e : K ≌ J) : cocone F ≌ cocone (e.functor ⋙ F) := { functor := whiskering e.functor, inverse := whiskering e.inverse ⋙ precompose ((functor.left_unitor F).inv ≫ (whisker_right (e.counit_iso).inv F) ≫ (functor.associator _ _ _).inv), unit_iso := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (begin intro k, dsimp, simpa [e.counit_inv_app_functor k] using s.w (e.unit.app k), end)) (by tidy), } /-- The categories of cocones over `F` and `G` are equivalent if `F` and `G` are naturally isomorphic (possibly after changing the indexing category by an equivalence). -/ @[simps functor_obj] def equivalence_of_reindexing {G : K ⥤ C} (e : K ≌ J) (α : e.functor ⋙ F ≅ G) : cocone F ≌ cocone G := (whiskering_equivalence e).trans (precompose_equivalence α.symm) section variable (F) /-- Forget the cocone structure and obtain just the cocone point. -/ @[simps] def forget : cocone F ⥤ C := { obj := λ t, t.X, map := λ s t f, f.hom } variables (G : C ⥤ D) /-- A functor `G : C ⥤ D` sends cocones over `F` to cocones over `F ⋙ G` functorially. -/ @[simps] def functoriality : cocone F ⥤ cocone (F ⋙ G) := { obj := λ A, { X := G.obj A.X, ι := { app := λ j, G.map (A.ι.app j), naturality' := by intros; erw ←G.map_comp; tidy } }, map := λ _ _ f, { hom := G.map f.hom, w' := by intros; rw [←functor.map_comp, cocone_morphism.w] } } instance functoriality_full [full G] [faithful G] : full (functoriality F G) := { preimage := λ X Y t, { hom := G.preimage t.hom, w' := λ j, G.map_injective (by simpa using t.w j) } } instance functoriality_faithful [faithful G] : faithful (functoriality F G) := { map_injective' := λ X Y f g e, by { ext1, injection e, apply G.map_injective h_1 } } /-- If `e : C ≌ D` is an equivalence of categories, then `functoriality F e.functor` induces an equivalence between cocones over `F` and cocones over `F ⋙ e.functor`. -/ @[simps] def functoriality_equivalence (e : C ≌ D) : cocone F ≌ cocone (F ⋙ e.functor) := let f : (F ⋙ e.functor) ⋙ e.inverse ≅ F := functor.associator _ _ _ ≪≫ iso_whisker_left _ (e.unit_iso).symm ≪≫ functor.right_unitor _ in { functor := functoriality F e.functor, inverse := (functoriality (F ⋙ e.functor) e.inverse) ⋙ (precompose_equivalence f.symm).functor, unit_iso := nat_iso.of_components (λ c, cocones.ext (e.unit_iso.app _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ c, cocones.ext (e.counit_iso.app _) begin -- Unfortunately this doesn't work by `tidy`. -- In this configuration `simp` reaches a dead-end and needs help. intros j, dsimp, simp only [←equivalence.counit_inv_app_functor, iso.inv_hom_id_app, map_comp, equivalence.fun_inv_map, assoc, id_comp, iso.inv_hom_id_app_assoc], dsimp, simp, -- See note [dsimp, simp]. end) (λ c c' f, by { ext, dsimp, simp, dsimp, simp, }), } /-- If `F` reflects isomorphisms, then `cocones.functoriality F` reflects isomorphisms as well. -/ instance reflects_cocone_isomorphism (F : C ⥤ D) [reflects_isomorphisms F] (K : J ⥤ C) : reflects_isomorphisms (cocones.functoriality K F) := begin constructor, introsI, haveI : is_iso (F.map f.hom) := (cocones.forget (K ⋙ F)).map_is_iso ((cocones.functoriality K F).map f), haveI := reflects_isomorphisms.reflects F f.hom, apply cocone_iso_of_hom_iso end end end cocones end limits namespace functor variables {F : J ⥤ C} {G : J ⥤ C} (H : C ⥤ D) open category_theory.limits /-- The image of a cone in C under a functor G : C ⥤ D is a cone in D. -/ @[simps] def map_cone (c : cone F) : cone (F ⋙ H) := (cones.functoriality F H).obj c /-- The image of a cocone in C under a functor G : C ⥤ D is a cocone in D. -/ @[simps] def map_cocone (c : cocone F) : cocone (F ⋙ H) := (cocones.functoriality F H).obj c /-- Given a cone morphism `c ⟶ c'`, construct a cone morphism on the mapped cones functorially. -/ def map_cone_morphism {c c' : cone F} (f : c ⟶ c') : H.map_cone c ⟶ H.map_cone c' := (cones.functoriality F H).map f /-- Given a cocone morphism `c ⟶ c'`, construct a cocone morphism on the mapped cocones functorially. -/ def map_cocone_morphism {c c' : cocone F} (f : c ⟶ c') : H.map_cocone c ⟶ H.map_cocone c' := (cocones.functoriality F H).map f /-- If `H` is an equivalence, we invert `H.map_cone` and get a cone for `F` from a cone for `F ⋙ H`.-/ def map_cone_inv [is_equivalence H] (c : cone (F ⋙ H)) : cone F := (limits.cones.functoriality_equivalence F (as_equivalence H)).inverse.obj c /-- `map_cone` is the left inverse to `map_cone_inv`. -/ def map_cone_map_cone_inv {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : cone (F ⋙ H)) : map_cone H (map_cone_inv H c) ≅ c := (limits.cones.functoriality_equivalence F (as_equivalence H)).counit_iso.app c /-- `map_cone` is the right inverse to `map_cone_inv`. -/ def map_cone_inv_map_cone {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : cone F) : map_cone_inv H (map_cone H c) ≅ c := (limits.cones.functoriality_equivalence F (as_equivalence H)).unit_iso.symm.app c /-- If `H` is an equivalence, we invert `H.map_cone` and get a cone for `F` from a cone for `F ⋙ H`.-/ def map_cocone_inv [is_equivalence H] (c : cocone (F ⋙ H)) : cocone F := (limits.cocones.functoriality_equivalence F (as_equivalence H)).inverse.obj c /-- `map_cocone` is the left inverse to `map_cocone_inv`. -/ def map_cocone_map_cocone_inv {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : cocone (F ⋙ H)) : map_cocone H (map_cocone_inv H c) ≅ c := (limits.cocones.functoriality_equivalence F (as_equivalence H)).counit_iso.app c /-- `map_cocone` is the right inverse to `map_cocone_inv`. -/ def map_cocone_inv_map_cocone {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : cocone F) : map_cocone_inv H (map_cocone H c) ≅ c := (limits.cocones.functoriality_equivalence F (as_equivalence H)).unit_iso.symm.app c /-- `functoriality F _ ⋙ postcompose (whisker_left F _)` simplifies to `functoriality F _`. -/ @[simps] def functoriality_comp_postcompose {H H' : C ⥤ D} (α : H ≅ H') : cones.functoriality F H ⋙ cones.postcompose (whisker_left F α.hom) ≅ cones.functoriality F H' := nat_iso.of_components (λ c, cones.ext (α.app _) (by tidy)) (by tidy) /-- For `F : J ⥤ C`, given a cone `c : cone F`, and a natural isomorphism `α : H ≅ H'` for functors `H H' : C ⥤ D`, the postcomposition of the cone `H.map_cone` using the isomorphism `α` is isomorphic to the cone `H'.map_cone`. -/ @[simps] def postcompose_whisker_left_map_cone {H H' : C ⥤ D} (α : H ≅ H') (c : cone F) : (cones.postcompose (whisker_left F α.hom : _)).obj (H.map_cone c) ≅ H'.map_cone c := (functoriality_comp_postcompose α).app c /-- `map_cone` commutes with `postcompose`. In particular, for `F : J ⥤ C`, given a cone `c : cone F`, a natural transformation `α : F ⟶ G` and a functor `H : C ⥤ D`, we have two obvious ways of producing a cone over `G ⋙ H`, and they are both isomorphic. -/ @[simps] def map_cone_postcompose {α : F ⟶ G} {c} : H.map_cone ((cones.postcompose α).obj c) ≅ (cones.postcompose (whisker_right α H : _)).obj (H.map_cone c) := cones.ext (iso.refl _) (by tidy) /-- `map_cone` commutes with `postcompose_equivalence` -/ @[simps] def map_cone_postcompose_equivalence_functor {α : F ≅ G} {c} : H.map_cone ((cones.postcompose_equivalence α).functor.obj c) ≅ (cones.postcompose_equivalence (iso_whisker_right α H : _)).functor.obj (H.map_cone c) := cones.ext (iso.refl _) (by tidy) /-- `functoriality F _ ⋙ precompose (whisker_left F _)` simplifies to `functoriality F _`. -/ @[simps] def functoriality_comp_precompose {H H' : C ⥤ D} (α : H ≅ H') : cocones.functoriality F H ⋙ cocones.precompose (whisker_left F α.inv) ≅ cocones.functoriality F H' := nat_iso.of_components (λ c, cocones.ext (α.app _) (by tidy)) (by tidy) /-- For `F : J ⥤ C`, given a cocone `c : cocone F`, and a natural isomorphism `α : H ≅ H'` for functors `H H' : C ⥤ D`, the precomposition of the cocone `H.map_cocone` using the isomorphism `α` is isomorphic to the cocone `H'.map_cocone`. -/ @[simps] def precompose_whisker_left_map_cocone {H H' : C ⥤ D} (α : H ≅ H') (c : cocone F) : (cocones.precompose (whisker_left F α.inv : _)).obj (H.map_cocone c) ≅ H'.map_cocone c := (functoriality_comp_precompose α).app c /-- `map_cocone` commutes with `precompose`. In particular, for `F : J ⥤ C`, given a cocone `c : cocone F`, a natural transformation `α : F ⟶ G` and a functor `H : C ⥤ D`, we have two obvious ways of producing a cocone over `G ⋙ H`, and they are both isomorphic. -/ @[simps] def map_cocone_precompose {α : F ⟶ G} {c} : H.map_cocone ((cocones.precompose α).obj c) ≅ (cocones.precompose (whisker_right α H : _)).obj (H.map_cocone c) := cocones.ext (iso.refl _) (by tidy) /-- `map_cocone` commutes with `precompose_equivalence` -/ @[simps] def map_cocone_precompose_equivalence_functor {α : F ≅ G} {c} : H.map_cocone ((cocones.precompose_equivalence α).functor.obj c) ≅ (cocones.precompose_equivalence (iso_whisker_right α H : _)).functor.obj (H.map_cocone c) := cocones.ext (iso.refl _) (by tidy) /-- `map_cone` commutes with `whisker` -/ @[simps] def map_cone_whisker {E : K ⥤ J} {c : cone F} : H.map_cone (c.whisker E) ≅ (H.map_cone c).whisker E := cones.ext (iso.refl _) (by tidy) /-- `map_cocone` commutes with `whisker` -/ @[simps] def map_cocone_whisker {E : K ⥤ J} {c : cocone F} : H.map_cocone (c.whisker E) ≅ (H.map_cocone c).whisker E := cocones.ext (iso.refl _) (by tidy) end functor end category_theory namespace category_theory.limits section variables {F : J ⥤ C} /-- Change a `cocone F` into a `cone F.op`. -/ @[simps] def cocone.op (c : cocone F) : cone F.op := { X := op c.X, π := nat_trans.op c.ι } /-- Change a `cone F` into a `cocone F.op`. -/ @[simps] def cone.op (c : cone F) : cocone F.op := { X := op c.X, ι := nat_trans.op c.π } /-- Change a `cocone F.op` into a `cone F`. -/ @[simps] def cocone.unop (c : cocone F.op) : cone F := { X := unop c.X, π := nat_trans.remove_op c.ι } /-- Change a `cone F.op` into a `cocone F`. -/ @[simps] def cone.unop (c : cone F.op) : cocone F := { X := unop c.X, ι := nat_trans.remove_op c.π } variables (F) /-- The category of cocones on `F` is equivalent to the opposite category of the category of cones on the opposite of `F`. -/ def cocone_equivalence_op_cone_op : cocone F ≌ (cone F.op)ᵒᵖ := { functor := { obj := λ c, op (cocone.op c), map := λ X Y f, quiver.hom.op { hom := f.hom.op, w' := λ j, by { apply quiver.hom.unop_inj, dsimp, simp, }, } }, inverse := { obj := λ c, cone.unop (unop c), map := λ X Y f, { hom := f.unop.hom.unop, w' := λ j, by { apply quiver.hom.op_inj, dsimp, simp, }, } }, unit_iso := nat_iso.of_components (λ c, cocones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ c, by { induction c using opposite.rec, dsimp, apply iso.op, exact cones.ext (iso.refl _) (by tidy), }) (λ X Y f, quiver.hom.unop_inj (cone_morphism.ext _ _ (by { dsimp, simp }))), functor_unit_iso_comp' := λ c, begin apply quiver.hom.unop_inj, ext, dsimp, simp, end } attribute [simps] cocone_equivalence_op_cone_op end section variables {F : J ⥤ Cᵒᵖ} /-- Change a cocone on `F.left_op : Jᵒᵖ ⥤ C` to a cocone on `F : J ⥤ Cᵒᵖ`. -/ -- Here and below we only automatically generate the `@[simp]` lemma for the `X` field, -- as we can write a simpler `rfl` lemma for the components of the natural transformation by hand. @[simps {rhs_md := semireducible, simp_rhs := tt}] def cone_of_cocone_left_op (c : cocone F.left_op) : cone F := { X := op c.X, π := nat_trans.remove_left_op c.ι } /-- Change a cone on `F : J ⥤ Cᵒᵖ` to a cocone on `F.left_op : Jᵒᵖ ⥤ C`. -/ @[simps {rhs_md := semireducible, simp_rhs := tt}] def cocone_left_op_of_cone (c : cone F) : cocone (F.left_op) := { X := unop c.X, ι := nat_trans.left_op c.π } /-- Change a cone on `F.left_op : Jᵒᵖ ⥤ C` to a cocone on `F : J ⥤ Cᵒᵖ`. -/ /- When trying use `@[simps]` to generate the `ι_app` field of this definition, `@[simps]` tries to reduce the RHS using `expr.dsimp` and `expr.simp`, but for some reason the expression is not being simplified properly. -/ @[simps X] def cocone_of_cone_left_op (c : cone F.left_op) : cocone F := { X := op c.X, ι := nat_trans.remove_left_op c.π } @[simp] lemma cocone_of_cone_left_op_ι_app (c : cone F.left_op) (j) : (cocone_of_cone_left_op c).ι.app j = (c.π.app (op j)).op := by { dsimp only [cocone_of_cone_left_op], simp } /-- Change a cocone on `F : J ⥤ Cᵒᵖ` to a cone on `F.left_op : Jᵒᵖ ⥤ C`. -/ @[simps {rhs_md := semireducible, simp_rhs := tt}] def cone_left_op_of_cocone (c : cocone F) : cone (F.left_op) := { X := unop c.X, π := nat_trans.left_op c.ι } end section variables {F : Jᵒᵖ ⥤ C} /-- Change a cocone on `F.right_op : J ⥤ Cᵒᵖ` to a cone on `F : Jᵒᵖ ⥤ C`. -/ @[simps] def cone_of_cocone_right_op (c : cocone F.right_op) : cone F := { X := unop c.X, π := nat_trans.remove_right_op c.ι } /-- Change a cone on `F : Jᵒᵖ ⥤ C` to a cocone on `F.right_op : Jᵒᵖ ⥤ C`. -/ @[simps] def cocone_right_op_of_cone (c : cone F) : cocone (F.right_op) := { X := op c.X, ι := nat_trans.right_op c.π } /-- Change a cone on `F.right_op : J ⥤ Cᵒᵖ` to a cocone on `F : Jᵒᵖ ⥤ C`. -/ @[simps] def cocone_of_cone_right_op (c : cone F.right_op) : cocone F := { X := unop c.X, ι := nat_trans.remove_right_op c.π } /-- Change a cocone on `F : Jᵒᵖ ⥤ C` to a cone on `F.right_op : J ⥤ Cᵒᵖ`. -/ @[simps] def cone_right_op_of_cocone (c : cocone F) : cone (F.right_op) := { X := op c.X, π := nat_trans.right_op c.ι } end section variables {F : Jᵒᵖ ⥤ Cᵒᵖ} /-- Change a cocone on `F.unop : J ⥤ C` into a cone on `F : Jᵒᵖ ⥤ Cᵒᵖ`. -/ @[simps] def cone_of_cocone_unop (c : cocone F.unop) : cone F := { X := op c.X, π := nat_trans.remove_unop c.ι } /-- Change a cone on `F : Jᵒᵖ ⥤ Cᵒᵖ` into a cocone on `F.unop : J ⥤ C`. -/ @[simps] def cocone_unop_of_cone (c : cone F) : cocone F.unop := { X := unop c.X, ι := nat_trans.unop c.π } /-- Change a cone on `F.unop : J ⥤ C` into a cocone on `F : Jᵒᵖ ⥤ Cᵒᵖ`. -/ @[simps] def cocone_of_cone_unop (c : cone F.unop) : cocone F := { X := op c.X, ι := nat_trans.remove_unop c.π } /-- Change a cocone on `F : Jᵒᵖ ⥤ Cᵒᵖ` into a cone on `F.unop : J ⥤ C`. -/ @[simps] def cone_unop_of_cocone (c : cocone F) : cone F.unop := { X := unop c.X, π := nat_trans.unop c.ι } end end category_theory.limits namespace category_theory.functor open category_theory.limits variables {F : J ⥤ C} section variables (G : C ⥤ D) /-- The opposite cocone of the image of a cone is the image of the opposite cocone. -/ @[simps {rhs_md := semireducible}] def map_cone_op (t : cone F) : (G.map_cone t).op ≅ (G.op.map_cocone t.op) := cocones.ext (iso.refl _) (by tidy) /-- The opposite cone of the image of a cocone is the image of the opposite cone. -/ @[simps {rhs_md := semireducible}] def map_cocone_op {t : cocone F} : (G.map_cocone t).op ≅ (G.op.map_cone t.op) := cones.ext (iso.refl _) (by tidy) end end category_theory.functor
12f8450a976d1ca07a9d34d007d6b970045f8323
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/tactic/squeeze.lean
635dee7b98a0a18709c8de7f5dd2d276cfae9d79
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
3,609
lean
import meta.rb_map import tactic.basic import category.traversable open interactive interactive.types lean.parser meta def loc.to_string_aux : option name → string | none := "⊢" | (some x) := to_string x 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 *" namespace tactic namespace interactive meta def arg.to_tactic_format : simp_arg_type → tactic format | (simp_arg_type.expr e) := i_to_expr_no_subgoals e >>= pp | simp_arg_type.all_hyps := pure "*" | (simp_arg_type.except n) := pure format!"-{n}" open list meta def record_lit : 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 } meta def rec.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}}" local postfix `?`:9001 := optional 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 <*> rec.to_tactic_format cfg meta def squeeze_simp (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 record_lit?) : tactic unit := do g ← main_goal, (cfg',c) ← parse_config cfg, hs' ← hs.mmap arg.to_tactic_format, simp use_iota_eqn no_dflt hs attr_names locat cfg', g ← instantiate_mvars g, let vs := g.list_constant, vs ← vs.mfilter (succeeds ∘ has_attribute `simp), let use_iota_eqn := if use_iota_eqn.is_some then "!" else "", let attrs := if attr_names.empty then "" else string.join (list.intersperse " " (" with" :: attr_names.map to_string)), let loc := loc.to_string locat, let args := hs' ++ vs.to_list.map to_fmt, trace format!"simp{use_iota_eqn} only {args}{attrs}{loc}{c}" meta def squeeze_simpa (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 record_lit?) : tactic unit := do g ← main_goal, (cfg',c) ← parse_config cfg, tgt' ← traverse (λ t, do t ← to_expr t >>= pp, pure format!" using {t}") tgt, simpa use_iota_eqn no_dflt hs attr_names tgt cfg', g ← instantiate_mvars g, let vs := g.list_constant, vs ← vs.mfilter (succeeds ∘ has_attribute `simp), let use_iota_eqn := if use_iota_eqn.is_some then "!" else "", let attrs := if attr_names.empty then "" else string.join (list.intersperse " " (" with" :: attr_names.map to_string)), let tgt' := tgt'.get_or_else "", hs ← hs.mmap arg.to_tactic_format, let args := hs ++ vs.to_list.map to_fmt, trace format!"simpa{use_iota_eqn} only {args}{attrs}{tgt'}{c}" end interactive end tactic
1c9c189db59cf465f84000ee15f07e7274ad8974
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/normed_space/dual.lean
e2b6c94a46c829b08bb3c241b78ccab532fb67bf
[ "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,336
lean
/- Copyright (c) 2020 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.normed_space.hahn_banach.extension import analysis.normed_space.is_R_or_C import analysis.locally_convex.polar /-! # The topological dual of a normed space In this file we define the topological dual `normed_space.dual` of a normed space, and the continuous linear map `normed_space.inclusion_in_double_dual` from a normed space into its double dual. For base field `𝕜 = ℝ` or `𝕜 = ℂ`, this map is actually an isometric embedding; we provide a version `normed_space.inclusion_in_double_dual_li` of the map which is of type a bundled linear isometric embedding, `E →ₗᵢ[𝕜] (dual 𝕜 (dual 𝕜 E))`. Since a lot of elementary properties don't require `eq_of_dist_eq_zero` we start setting up the theory for `seminormed_add_comm_group` and we specialize to `normed_add_comm_group` when needed. ## Main definitions * `inclusion_in_double_dual` and `inclusion_in_double_dual_li` are the inclusion of a normed space in its double dual, considered as a bounded linear map and as a linear isometry, respectively. * `polar 𝕜 s` is the subset of `dual 𝕜 E` consisting of those functionals `x'` for which `‖x' z‖ ≤ 1` for every `z ∈ s`. ## Tags dual -/ noncomputable theory open_locale classical topology universes u v namespace normed_space section general variables (𝕜 : Type*) [nontrivially_normed_field 𝕜] variables (E : Type*) [seminormed_add_comm_group E] [normed_space 𝕜 E] variables (F : Type*) [normed_add_comm_group F] [normed_space 𝕜 F] /-- The topological dual of a seminormed space `E`. -/ @[derive [inhabited, seminormed_add_comm_group, normed_space 𝕜]] def dual := E →L[𝕜] 𝕜 instance : continuous_linear_map_class (dual 𝕜 E) 𝕜 E 𝕜 := continuous_linear_map.continuous_semilinear_map_class instance : has_coe_to_fun (dual 𝕜 E) (λ _, E → 𝕜) := continuous_linear_map.to_fun instance : normed_add_comm_group (dual 𝕜 F) := continuous_linear_map.to_normed_add_comm_group instance [finite_dimensional 𝕜 E] : finite_dimensional 𝕜 (dual 𝕜 E) := continuous_linear_map.finite_dimensional /-- The inclusion of a normed space in its double (topological) dual, considered as a bounded linear map. -/ def inclusion_in_double_dual : E →L[𝕜] (dual 𝕜 (dual 𝕜 E)) := continuous_linear_map.apply 𝕜 𝕜 @[simp] lemma dual_def (x : E) (f : dual 𝕜 E) : inclusion_in_double_dual 𝕜 E x f = f x := rfl lemma inclusion_in_double_dual_norm_eq : ‖inclusion_in_double_dual 𝕜 E‖ = ‖(continuous_linear_map.id 𝕜 (dual 𝕜 E))‖ := continuous_linear_map.op_norm_flip _ lemma inclusion_in_double_dual_norm_le : ‖inclusion_in_double_dual 𝕜 E‖ ≤ 1 := by { rw inclusion_in_double_dual_norm_eq, exact continuous_linear_map.norm_id_le } lemma double_dual_bound (x : E) : ‖(inclusion_in_double_dual 𝕜 E) x‖ ≤ ‖x‖ := by simpa using continuous_linear_map.le_of_op_norm_le _ (inclusion_in_double_dual_norm_le 𝕜 E) x /-- The dual pairing as a bilinear form. -/ def dual_pairing : (dual 𝕜 E) →ₗ[𝕜] E →ₗ[𝕜] 𝕜 := continuous_linear_map.coe_lm 𝕜 @[simp] lemma dual_pairing_apply {v : dual 𝕜 E} {x : E} : dual_pairing 𝕜 E v x = v x := rfl lemma dual_pairing_separating_left : (dual_pairing 𝕜 E).separating_left := begin rw [linear_map.separating_left_iff_ker_eq_bot, linear_map.ker_eq_bot], exact continuous_linear_map.coe_injective, end end general section bidual_isometry variables (𝕜 : Type v) [is_R_or_C 𝕜] {E : Type u} [normed_add_comm_group E] [normed_space 𝕜 E] /-- If one controls the norm of every `f x`, then one controls the norm of `x`. Compare `continuous_linear_map.op_norm_le_bound`. -/ lemma norm_le_dual_bound (x : E) {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ (f : dual 𝕜 E), ‖f x‖ ≤ M * ‖f‖) : ‖x‖ ≤ M := begin classical, by_cases h : x = 0, { simp only [h, hMp, norm_zero] }, { obtain ⟨f, hf₁, hfx⟩ : ∃ f : E →L[𝕜] 𝕜, ‖f‖ = 1 ∧ f x = ‖x‖ := exists_dual_vector 𝕜 x h, calc ‖x‖ = ‖(‖x‖ : 𝕜)‖ : is_R_or_C.norm_coe_norm.symm ... = ‖f x‖ : by rw hfx ... ≤ M * ‖f‖ : hM f ... = M : by rw [hf₁, mul_one] } end lemma eq_zero_of_forall_dual_eq_zero {x : E} (h : ∀ f : dual 𝕜 E, f x = (0 : 𝕜)) : x = 0 := norm_le_zero_iff.mp (norm_le_dual_bound 𝕜 x le_rfl (λ f, by simp [h f])) lemma eq_zero_iff_forall_dual_eq_zero (x : E) : x = 0 ↔ ∀ g : dual 𝕜 E, g x = 0 := ⟨λ hx, by simp [hx], λ h, eq_zero_of_forall_dual_eq_zero 𝕜 h⟩ /-- See also `geometric_hahn_banach_point_point`. -/ lemma eq_iff_forall_dual_eq {x y : E} : x = y ↔ ∀ g : dual 𝕜 E, g x = g y := begin rw [← sub_eq_zero, eq_zero_iff_forall_dual_eq_zero 𝕜 (x - y)], simp [sub_eq_zero], end /-- The inclusion of a normed space in its double dual is an isometry onto its image.-/ def inclusion_in_double_dual_li : E →ₗᵢ[𝕜] (dual 𝕜 (dual 𝕜 E)) := { norm_map' := begin intros x, apply le_antisymm, { exact double_dual_bound 𝕜 E x }, rw continuous_linear_map.norm_def, refine le_cInf continuous_linear_map.bounds_nonempty _, rintros c ⟨hc1, hc2⟩, exact norm_le_dual_bound 𝕜 x hc1 hc2 end, .. inclusion_in_double_dual 𝕜 E } end bidual_isometry section polar_sets open metric set normed_space /-- Given a subset `s` in a normed space `E` (over a field `𝕜`), the polar `polar 𝕜 s` is the subset of `dual 𝕜 E` consisting of those functionals which evaluate to something of norm at most one at all points `z ∈ s`. -/ def polar (𝕜 : Type*) [nontrivially_normed_field 𝕜] {E : Type*} [seminormed_add_comm_group E] [normed_space 𝕜 E] : set E → set (dual 𝕜 E) := (dual_pairing 𝕜 E).flip.polar variables (𝕜 : Type*) [nontrivially_normed_field 𝕜] variables {E : Type*} [seminormed_add_comm_group E] [normed_space 𝕜 E] lemma mem_polar_iff {x' : dual 𝕜 E} (s : set E) : x' ∈ polar 𝕜 s ↔ ∀ z ∈ s, ‖x' z‖ ≤ 1 := iff.rfl @[simp] lemma polar_univ : polar 𝕜 (univ : set E) = {(0 : dual 𝕜 E)} := (dual_pairing 𝕜 E).flip.polar_univ (linear_map.flip_separating_right.mpr (dual_pairing_separating_left 𝕜 E)) lemma is_closed_polar (s : set E) : is_closed (polar 𝕜 s) := begin dunfold normed_space.polar, simp only [linear_map.polar_eq_Inter, linear_map.flip_apply], refine is_closed_bInter (λ z hz, _), exact is_closed_Iic.preimage (continuous_linear_map.apply 𝕜 𝕜 z).continuous.norm end @[simp] lemma polar_closure (s : set E) : polar 𝕜 (closure s) = polar 𝕜 s := ((dual_pairing 𝕜 E).flip.polar_antitone subset_closure).antisymm $ (dual_pairing 𝕜 E).flip.polar_gc.l_le $ closure_minimal ((dual_pairing 𝕜 E).flip.polar_gc.le_u_l s) $ by simpa [linear_map.flip_flip] using (is_closed_polar _ _).preimage (inclusion_in_double_dual 𝕜 E).continuous variables {𝕜} /-- If `x'` is a dual element such that the norms `‖x' z‖` are bounded for `z ∈ s`, then a small scalar multiple of `x'` is in `polar 𝕜 s`. -/ lemma smul_mem_polar {s : set E} {x' : dual 𝕜 E} {c : 𝕜} (hc : ∀ z, z ∈ s → ‖ x' z ‖ ≤ ‖c‖) : c⁻¹ • x' ∈ polar 𝕜 s := begin by_cases c_zero : c = 0, { simp only [c_zero, inv_zero, zero_smul], exact (dual_pairing 𝕜 E).flip.zero_mem_polar _ }, have eq : ∀ z, ‖ c⁻¹ • (x' z) ‖ = ‖ c⁻¹ ‖ * ‖ x' z ‖ := λ z, norm_smul c⁻¹ _, have le : ∀ z, z ∈ s → ‖ c⁻¹ • (x' z) ‖ ≤ ‖ c⁻¹ ‖ * ‖ c ‖, { intros z hzs, rw eq z, apply mul_le_mul (le_of_eq rfl) (hc z hzs) (norm_nonneg _) (norm_nonneg _), }, have cancel : ‖ c⁻¹ ‖ * ‖ c ‖ = 1, by simp only [c_zero, norm_eq_zero, ne.def, not_false_iff, inv_mul_cancel, norm_inv], rwa cancel at le, end lemma polar_ball_subset_closed_ball_div {c : 𝕜} (hc : 1 < ‖c‖) {r : ℝ} (hr : 0 < r) : polar 𝕜 (ball (0 : E) r) ⊆ closed_ball (0 : dual 𝕜 E) (‖c‖ / r) := begin intros x' hx', rw mem_polar_iff at hx', simp only [polar, mem_set_of_eq, mem_closed_ball_zero_iff, mem_ball_zero_iff] at *, have hcr : 0 < ‖c‖ / r, from div_pos (zero_lt_one.trans hc) hr, refine continuous_linear_map.op_norm_le_of_shell hr hcr.le hc (λ x h₁ h₂, _), calc ‖x' x‖ ≤ 1 : hx' _ h₂ ... ≤ (‖c‖ / r) * ‖x‖ : (inv_pos_le_iff_one_le_mul' hcr).1 (by rwa inv_div) end variables (𝕜) lemma closed_ball_inv_subset_polar_closed_ball {r : ℝ} : closed_ball (0 : dual 𝕜 E) r⁻¹ ⊆ polar 𝕜 (closed_ball (0 : E) r) := λ x' hx' x hx, calc ‖x' x‖ ≤ ‖x'‖ * ‖x‖ : x'.le_op_norm x ... ≤ r⁻¹ * r : mul_le_mul (mem_closed_ball_zero_iff.1 hx') (mem_closed_ball_zero_iff.1 hx) (norm_nonneg _) (dist_nonneg.trans hx') ... = r / r : inv_mul_eq_div _ _ ... ≤ 1 : div_self_le_one r /-- The `polar` of closed ball in a normed space `E` is the closed ball of the dual with inverse radius. -/ lemma polar_closed_ball {𝕜 E : Type*} [is_R_or_C 𝕜] [normed_add_comm_group E] [normed_space 𝕜 E] {r : ℝ} (hr : 0 < r) : polar 𝕜 (closed_ball (0 : E) r) = closed_ball (0 : dual 𝕜 E) r⁻¹ := begin refine subset.antisymm _ (closed_ball_inv_subset_polar_closed_ball _), intros x' h, simp only [mem_closed_ball_zero_iff], refine continuous_linear_map.op_norm_le_of_ball hr (inv_nonneg.mpr hr.le) (λ z hz, _), simpa only [one_div] using linear_map.bound_of_ball_bound' hr 1 x'.to_linear_map h z end /-- Given a neighborhood `s` of the origin in a normed space `E`, the dual norms of all elements of the polar `polar 𝕜 s` are bounded by a constant. -/ lemma bounded_polar_of_mem_nhds_zero {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : bounded (polar 𝕜 s) := begin obtain ⟨a, ha⟩ : ∃ a : 𝕜, 1 < ‖a‖ := normed_field.exists_one_lt_norm 𝕜, obtain ⟨r, r_pos, r_ball⟩ : ∃ (r : ℝ) (hr : 0 < r), ball 0 r ⊆ s := metric.mem_nhds_iff.1 s_nhd, exact bounded_closed_ball.mono (((dual_pairing 𝕜 E).flip.polar_antitone r_ball).trans $ polar_ball_subset_closed_ball_div ha r_pos) end end polar_sets end normed_space
6529e41a114d639cac6e683561279151903675aa
94e33a31faa76775069b071adea97e86e218a8ee
/src/number_theory/padics/padic_numbers.lean
e4ce9e5011d0f5c20f747eae8969279e49db27dc
[ "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
42,044
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import analysis.normed_space.basic import number_theory.padics.padic_norm /-! # p-adic numbers This file defines the p-adic numbers (rationals) `ℚ_p` as the completion of `ℚ` with respect to the p-adic norm. We show that the p-adic norm on ℚ extends to `ℚ_p`, that `ℚ` is embedded in `ℚ_p`, and that `ℚ_p` is Cauchy complete. ## Important definitions * `padic` : the type of p-adic numbers * `padic_norm_e` : the rational valued p-adic norm on `ℚ_p` * `padic.add_valuation` : the additive `p`-adic valuation on `ℚ_p`, with values in `with_top ℤ`. ## Notation We introduce the notation `ℚ_[p]` for the p-adic numbers. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact (prime p)]` as a type class argument. We use the same concrete Cauchy sequence construction that is used to construct ℝ. `ℚ_p` inherits a field structure from this construction. The extension of the norm on ℚ to `ℚ_p` is *not* analogous to extending the absolute value to ℝ, and hence the proof that `ℚ_p` is complete is different from the proof that ℝ is complete. A small special-purpose simplification tactic, `padic_index_simp`, is used to manipulate sequence indices in the proof that the norm extends. `padic_norm_e` is the rational-valued p-adic norm on `ℚ_p`. To instantiate `ℚ_p` as a normed field, we must cast this into a ℝ-valued norm. The `ℝ`-valued norm, using notation `∥ ∥` from normed spaces, is the canonical representation of this norm. `simp` prefers `padic_norm` to `padic_norm_e` when possible. Since `padic_norm_e` and `∥ ∥` have different types, `simp` does not rewrite one to the other. Coercions from `ℚ` to `ℚ_p` are set up to work with the `norm_cast` tactic. ## References * [F. Q. Gouvêa, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, norm, valuation, cauchy, completion, p-adic completion -/ noncomputable theory open_locale classical open nat multiplicity padic_norm cau_seq cau_seq.completion metric /-- The type of Cauchy sequences of rationals with respect to the p-adic norm. -/ @[reducible] def padic_seq (p : ℕ) := cau_seq _ (padic_norm p) namespace padic_seq section variables {p : ℕ} [fact p.prime] /-- The p-adic norm of the entries of a nonzero Cauchy sequence of rationals is eventually constant. -/ lemma stationary {f : cau_seq ℚ (padic_norm p)} (hf : ¬ f ≈ 0) : ∃ N, ∀ m n, N ≤ m → N ≤ n → padic_norm p (f n) = padic_norm p (f m) := have ∃ ε > 0, ∃ N1, ∀ j ≥ N1, ε ≤ padic_norm p (f j), from cau_seq.abv_pos_of_not_lim_zero $ not_lim_zero_of_not_congr_zero hf, let ⟨ε, hε, N1, hN1⟩ := this, ⟨N2, hN2⟩ := cau_seq.cauchy₂ f hε in ⟨ max N1 N2, λ n m hn hm, have padic_norm p (f n - f m) < ε, from hN2 _ (max_le_iff.1 hn).2 _ (max_le_iff.1 hm).2, have padic_norm p (f n - f m) < padic_norm p (f n), from lt_of_lt_of_le this $ hN1 _ (max_le_iff.1 hn).1, have padic_norm p (f n - f m) < max (padic_norm p (f n)) (padic_norm p (f m)), from lt_max_iff.2 (or.inl this), begin by_contradiction hne, rw ←padic_norm.neg p (f m) at hne, have hnam := add_eq_max_of_ne p hne, rw [padic_norm.neg, max_comm] at hnam, rw [←hnam, sub_eq_add_neg, add_comm] at this, apply _root_.lt_irrefl _ this end ⟩ /-- For all n ≥ stationary_point f hf, the p-adic norm of f n is the same. -/ def stationary_point {f : padic_seq p} (hf : ¬ f ≈ 0) : ℕ := classical.some $ stationary hf lemma stationary_point_spec {f : padic_seq p} (hf : ¬ f ≈ 0) : ∀ {m n}, stationary_point hf ≤ m → stationary_point hf ≤ n → padic_norm p (f n) = padic_norm p (f m) := classical.some_spec $ stationary hf /-- Since the norm of the entries of a Cauchy sequence is eventually stationary, we can lift the norm to sequences. -/ def norm (f : padic_seq p) : ℚ := if hf : f ≈ 0 then 0 else padic_norm p (f (stationary_point hf)) lemma norm_zero_iff (f : padic_seq p) : f.norm = 0 ↔ f ≈ 0 := begin constructor, { intro h, by_contradiction hf, unfold norm at h, split_ifs at h, apply hf, intros ε hε, existsi stationary_point hf, intros j hj, have heq := stationary_point_spec hf le_rfl hj, simpa [h, heq] }, { intro h, simp [norm, h] } end end section embedding open cau_seq variables {p : ℕ} [fact p.prime] lemma equiv_zero_of_val_eq_of_equiv_zero {f g : padic_seq p} (h : ∀ k, padic_norm p (f k) = padic_norm p (g k)) (hf : f ≈ 0) : g ≈ 0 := λ ε hε, let ⟨i, hi⟩ := hf _ hε in ⟨i, λ j hj, by simpa [h] using hi _ hj⟩ lemma norm_nonzero_of_not_equiv_zero {f : padic_seq p} (hf : ¬ f ≈ 0) : f.norm ≠ 0 := hf ∘ f.norm_zero_iff.1 lemma norm_eq_norm_app_of_nonzero {f : padic_seq p} (hf : ¬ f ≈ 0) : ∃ k, f.norm = padic_norm p k ∧ k ≠ 0 := have heq : f.norm = padic_norm p (f $ stationary_point hf), by simp [norm, hf], ⟨f $ stationary_point hf, heq, λ h, norm_nonzero_of_not_equiv_zero hf (by simpa [h] using heq)⟩ lemma not_lim_zero_const_of_nonzero {q : ℚ} (hq : q ≠ 0) : ¬ lim_zero (const (padic_norm p) q) := λ h', hq $ const_lim_zero.1 h' lemma not_equiv_zero_const_of_nonzero {q : ℚ} (hq : q ≠ 0) : ¬ (const (padic_norm p) q) ≈ 0 := λ h : lim_zero (const (padic_norm p) q - 0), not_lim_zero_const_of_nonzero hq $ by simpa using h lemma norm_nonneg (f : padic_seq p) : 0 ≤ f.norm := if hf : f ≈ 0 then by simp [hf, norm] else by simp [norm, hf, padic_norm.nonneg] /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_left_left {f : padic_seq p} (hf : ¬ f ≈ 0) (v2 v3 : ℕ) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max (stationary_point hf) (max v2 v3))) := begin apply stationary_point_spec hf, { apply le_max_left }, { exact le_rfl } end /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_left {f : padic_seq p} (hf : ¬ f ≈ 0) (v1 v3 : ℕ) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max v1 (max (stationary_point hf) v3))) := begin apply stationary_point_spec hf, { apply le_trans, { apply le_max_left _ v3 }, { apply le_max_right } }, { exact le_rfl } end /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_right {f : padic_seq p} (hf : ¬ f ≈ 0) (v1 v2 : ℕ) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max v1 (max v2 (stationary_point hf)))) := begin apply stationary_point_spec hf, { apply le_trans, { apply le_max_right v2 }, { apply le_max_right } }, { exact le_rfl } end end embedding section valuation open cau_seq variables {p : ℕ} [fact p.prime] /-! ### Valuation on `padic_seq` -/ /-- The `p`-adic valuation on `ℚ` lifts to `padic_seq p`. `valuation f` is defined to be the valuation of the (`ℚ`-valued) stationary point of `f`. -/ def valuation (f : padic_seq p) : ℤ := if hf : f ≈ 0 then 0 else padic_val_rat p (f (stationary_point hf)) lemma norm_eq_pow_val {f : padic_seq p} (hf : ¬ f ≈ 0) : f.norm = p^(-f.valuation : ℤ) := begin rw [norm, valuation, dif_neg hf, dif_neg hf, padic_norm, if_neg], intro H, apply cau_seq.not_lim_zero_of_not_congr_zero hf, intros ε hε, use (stationary_point hf), intros n hn, rw stationary_point_spec hf le_rfl hn, simpa [H] using hε, end lemma val_eq_iff_norm_eq {f g : padic_seq p} (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) : f.valuation = g.valuation ↔ f.norm = g.norm := begin rw [norm_eq_pow_val hf, norm_eq_pow_val hg, ← neg_inj, zpow_inj], { exact_mod_cast (fact.out p.prime).pos }, { exact_mod_cast (fact.out p.prime).ne_one }, end end valuation end padic_seq section open padic_seq private meta def index_simp_core (hh hf hg : expr) (at_ : interactive.loc := interactive.loc.ns [none]) : tactic unit := do [v1, v2, v3] ← [hh, hf, hg].mmap (λ n, tactic.mk_app ``stationary_point [n] <|> return n), e1 ← tactic.mk_app ``lift_index_left_left [hh, v2, v3] <|> return `(true), e2 ← tactic.mk_app ``lift_index_left [hf, v1, v3] <|> return `(true), e3 ← tactic.mk_app ``lift_index_right [hg, v1, v2] <|> return `(true), sl ← [e1, e2, e3].mfoldl (λ s e, simp_lemmas.add s e) simp_lemmas.mk, when at_.include_goal (tactic.simp_target sl >> tactic.skip), hs ← at_.get_locals, hs.mmap' (tactic.simp_hyp sl []) /-- This is a special-purpose tactic that lifts padic_norm (f (stationary_point f)) to padic_norm (f (max _ _ _)). -/ meta def tactic.interactive.padic_index_simp (l : interactive.parse interactive.types.pexpr_list) (at_ : interactive.parse interactive.types.location) : tactic unit := do [h, f, g] ← l.mmap tactic.i_to_expr, index_simp_core h f g at_ end namespace padic_seq section embedding open cau_seq variables {p : ℕ} [hp : fact p.prime] include hp lemma norm_mul (f g : padic_seq p) : (f * g).norm = f.norm * g.norm := if hf : f ≈ 0 then have hg : f * g ≈ 0, from mul_equiv_zero' _ hf, by simp only [hf, hg, norm, dif_pos, zero_mul] else if hg : g ≈ 0 then have hf : f * g ≈ 0, from mul_equiv_zero _ hg, by simp only [hf, hg, norm, dif_pos, mul_zero] else have hfg : ¬ f * g ≈ 0, by apply mul_not_equiv_zero; assumption, begin unfold norm, split_ifs, padic_index_simp [hfg, hf, hg], apply padic_norm.mul end lemma eq_zero_iff_equiv_zero (f : padic_seq p) : mk f = 0 ↔ f ≈ 0 := mk_eq lemma ne_zero_iff_nequiv_zero (f : padic_seq p) : mk f ≠ 0 ↔ ¬ f ≈ 0 := not_iff_not.2 (eq_zero_iff_equiv_zero _) lemma norm_const (q : ℚ) : norm (const (padic_norm p) q) = padic_norm p q := if hq : q = 0 then have (const (padic_norm p) q) ≈ 0, by simp [hq]; apply setoid.refl (const (padic_norm p) 0), by subst hq; simp [norm, this] else have ¬ (const (padic_norm p) q) ≈ 0, from not_equiv_zero_const_of_nonzero hq, by simp [norm, this] lemma norm_values_discrete (a : padic_seq p) (ha : ¬ a ≈ 0) : (∃ (z : ℤ), a.norm = ↑p ^ (-z)) := let ⟨k, hk, hk'⟩ := norm_eq_norm_app_of_nonzero ha in by simpa [hk] using padic_norm.values_discrete p hk' lemma norm_one : norm (1 : padic_seq p) = 1 := have h1 : ¬ (1 : padic_seq p) ≈ 0, from one_not_equiv_zero _, by simp [h1, norm, hp.1.one_lt] private lemma norm_eq_of_equiv_aux {f g : padic_seq p} (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) (hfg : f ≈ g) (h : padic_norm p (f (stationary_point hf)) ≠ padic_norm p (g (stationary_point hg))) (hlt : padic_norm p (g (stationary_point hg)) < padic_norm p (f (stationary_point hf))) : false := begin have hpn : 0 < padic_norm p (f (stationary_point hf)) - padic_norm p (g (stationary_point hg)), from sub_pos_of_lt hlt, cases hfg _ hpn with N hN, let i := max N (max (stationary_point hf) (stationary_point hg)), have hi : N ≤ i, from le_max_left _ _, have hN' := hN _ hi, padic_index_simp [N, hf, hg] at hN' h hlt, have hpne : padic_norm p (f i) ≠ padic_norm p (-(g i)), by rwa [ ←padic_norm.neg p (g i)] at h, let hpnem := add_eq_max_of_ne p hpne, have hpeq : padic_norm p ((f - g) i) = max (padic_norm p (f i)) (padic_norm p (g i)), { rwa padic_norm.neg at hpnem }, rw [hpeq, max_eq_left_of_lt hlt] at hN', have : padic_norm p (f i) < padic_norm p (f i), { apply lt_of_lt_of_le hN', apply sub_le_self, apply padic_norm.nonneg }, exact lt_irrefl _ this end private lemma norm_eq_of_equiv {f g : padic_seq p} (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) (hfg : f ≈ g) : padic_norm p (f (stationary_point hf)) = padic_norm p (g (stationary_point hg)) := begin by_contradiction h, cases (decidable.em (padic_norm p (g (stationary_point hg)) < padic_norm p (f (stationary_point hf)))) with hlt hnlt, { exact norm_eq_of_equiv_aux hf hg hfg h hlt }, { apply norm_eq_of_equiv_aux hg hf (setoid.symm hfg) (ne.symm h), apply lt_of_le_of_ne, apply le_of_not_gt hnlt, apply h } end theorem norm_equiv {f g : padic_seq p} (hfg : f ≈ g) : f.norm = g.norm := if hf : f ≈ 0 then have hg : g ≈ 0, from setoid.trans (setoid.symm hfg) hf, by simp [norm, hf, hg] else have hg : ¬ g ≈ 0, from hf ∘ setoid.trans hfg, by unfold norm; split_ifs; exact norm_eq_of_equiv hf hg hfg private lemma norm_nonarchimedean_aux {f g : padic_seq p} (hfg : ¬ f + g ≈ 0) (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) : (f + g).norm ≤ max (f.norm) (g.norm) := begin unfold norm, split_ifs, padic_index_simp [hfg, hf, hg], apply padic_norm.nonarchimedean end theorem norm_nonarchimedean (f g : padic_seq p) : (f + g).norm ≤ max (f.norm) (g.norm) := if hfg : f + g ≈ 0 then have 0 ≤ max (f.norm) (g.norm), from le_max_of_le_left (norm_nonneg _), by simpa only [hfg, norm, ne.def, le_max_iff, cau_seq.add_apply, not_true, dif_pos] else if hf : f ≈ 0 then have hfg' : f + g ≈ g, { change lim_zero (f - 0) at hf, show lim_zero (f + g - g), by simpa only [sub_zero, add_sub_cancel] using hf }, have hcfg : (f + g).norm = g.norm, from norm_equiv hfg', have hcl : f.norm = 0, from (norm_zero_iff f).2 hf, have max (f.norm) (g.norm) = g.norm, by rw hcl; exact max_eq_right (norm_nonneg _), by rw [this, hcfg] else if hg : g ≈ 0 then have hfg' : f + g ≈ f, { change lim_zero (g - 0) at hg, show lim_zero (f + g - f), by simpa only [add_sub_cancel', sub_zero] using hg }, have hcfg : (f + g).norm = f.norm, from norm_equiv hfg', have hcl : g.norm = 0, from (norm_zero_iff g).2 hg, have max (f.norm) (g.norm) = f.norm, by rw hcl; exact max_eq_left (norm_nonneg _), by rw [this, hcfg] else norm_nonarchimedean_aux hfg hf hg lemma norm_eq {f g : padic_seq p} (h : ∀ k, padic_norm p (f k) = padic_norm p (g k)) : f.norm = g.norm := if hf : f ≈ 0 then have hg : g ≈ 0, from equiv_zero_of_val_eq_of_equiv_zero h hf, by simp only [hf, hg, norm, dif_pos] else have hg : ¬ g ≈ 0, from λ hg, hf $ equiv_zero_of_val_eq_of_equiv_zero (by simp only [h, forall_const, eq_self_iff_true]) hg, begin simp only [hg, hf, norm, dif_neg, not_false_iff], let i := max (stationary_point hf) (stationary_point hg), have hpf : padic_norm p (f (stationary_point hf)) = padic_norm p (f i), { apply stationary_point_spec, apply le_max_left, exact le_rfl }, have hpg : padic_norm p (g (stationary_point hg)) = padic_norm p (g i), { apply stationary_point_spec, apply le_max_right, exact le_rfl }, rw [hpf, hpg, h] end lemma norm_neg (a : padic_seq p) : (-a).norm = a.norm := norm_eq $ by simp lemma norm_eq_of_add_equiv_zero {f g : padic_seq p} (h : f + g ≈ 0) : f.norm = g.norm := have lim_zero (f + g - 0), from h, have f ≈ -g, from show lim_zero (f - (-g)), by simpa only [sub_zero, sub_neg_eq_add], have f.norm = (-g).norm, from norm_equiv this, by simpa only [norm_neg] using this lemma add_eq_max_of_ne {f g : padic_seq p} (hfgne : f.norm ≠ g.norm) : (f + g).norm = max f.norm g.norm := have hfg : ¬f + g ≈ 0, from mt norm_eq_of_add_equiv_zero hfgne, if hf : f ≈ 0 then have lim_zero (f - 0), from hf, have f + g ≈ g, from show lim_zero ((f + g) - g), by simpa only [sub_zero, add_sub_cancel], have h1 : (f+g).norm = g.norm, from norm_equiv this, have h2 : f.norm = 0, from (norm_zero_iff _).2 hf, by rw [h1, h2]; rw max_eq_right (norm_nonneg _) else if hg : g ≈ 0 then have lim_zero (g - 0), from hg, have f + g ≈ f, from show lim_zero ((f + g) - f), by rw [add_sub_cancel']; simpa only [sub_zero], have h1 : (f+g).norm = f.norm, from norm_equiv this, have h2 : g.norm = 0, from (norm_zero_iff _).2 hg, by rw [h1, h2]; rw max_eq_left (norm_nonneg _) else begin unfold norm at ⊢ hfgne, split_ifs at ⊢ hfgne, padic_index_simp [hfg, hf, hg] at ⊢ hfgne, exact padic_norm.add_eq_max_of_ne p hfgne end end embedding end padic_seq /-- The p-adic numbers `Q_[p]` are the Cauchy completion of `ℚ` with respect to the p-adic norm. -/ def padic (p : ℕ) [fact p.prime] := @cau_seq.completion.Cauchy _ _ _ _ (padic_norm p) _ notation `ℚ_[` p `]` := padic p namespace padic section completion variables {p : ℕ} [fact p.prime] /-- The discrete field structure on `ℚ_p` is inherited from the Cauchy completion construction. -/ instance field : field (ℚ_[p]) := cau_seq.completion.field instance : inhabited ℚ_[p] := ⟨0⟩ -- short circuits instance : has_zero ℚ_[p] := by apply_instance instance : has_one ℚ_[p] := by apply_instance instance : has_add ℚ_[p] := by apply_instance instance : has_mul ℚ_[p] := by apply_instance instance : has_sub ℚ_[p] := by apply_instance instance : has_neg ℚ_[p] := by apply_instance instance : has_div ℚ_[p] := by apply_instance instance : add_comm_group ℚ_[p] := by apply_instance instance : comm_ring ℚ_[p] := by apply_instance /-- Builds the equivalence class of a Cauchy sequence of rationals. -/ def mk : padic_seq p → ℚ_[p] := quotient.mk end completion section completion variables (p : ℕ) [fact p.prime] lemma mk_eq {f g : padic_seq p} : mk f = mk g ↔ f ≈ g := quotient.eq /-- Embeds the rational numbers in the p-adic numbers. -/ def of_rat : ℚ → ℚ_[p] := cau_seq.completion.of_rat @[simp] lemma of_rat_add : ∀ (x y : ℚ), of_rat p (x + y) = of_rat p x + of_rat p y := cau_seq.completion.of_rat_add @[simp] lemma of_rat_neg : ∀ (x : ℚ), of_rat p (-x) = -of_rat p x := cau_seq.completion.of_rat_neg @[simp] lemma of_rat_mul : ∀ (x y : ℚ), of_rat p (x * y) = of_rat p x * of_rat p y := cau_seq.completion.of_rat_mul @[simp] lemma of_rat_sub : ∀ (x y : ℚ), of_rat p (x - y) = of_rat p x - of_rat p y := cau_seq.completion.of_rat_sub @[simp] lemma of_rat_div : ∀ (x y : ℚ), of_rat p (x / y) = of_rat p x / of_rat p y := cau_seq.completion.of_rat_div @[simp] lemma of_rat_one : of_rat p 1 = 1 := rfl @[simp] lemma of_rat_zero : of_rat p 0 = 0 := rfl lemma cast_eq_of_rat_of_nat (n : ℕ) : (↑n : ℚ_[p]) = of_rat p n := begin induction n with n ih, { refl }, { simpa using ih } end lemma cast_eq_of_rat_of_int (n : ℤ) : ↑n = of_rat p n := by induction n; simp [cast_eq_of_rat_of_nat] lemma cast_eq_of_rat : ∀ (q : ℚ), (↑q : ℚ_[p]) = of_rat p q | ⟨n, d, h1, h2⟩ := show ↑n / ↑d = _, from have (⟨n, d, h1, h2⟩ : ℚ) = rat.mk n d, from rat.num_denom', by simp [this, rat.mk_eq_div, of_rat_div, cast_eq_of_rat_of_int, cast_eq_of_rat_of_nat] @[norm_cast] lemma coe_add : ∀ {x y : ℚ}, (↑(x + y) : ℚ_[p]) = ↑x + ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_neg : ∀ {x : ℚ}, (↑(-x) : ℚ_[p]) = -↑x := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_mul : ∀ {x y : ℚ}, (↑(x * y) : ℚ_[p]) = ↑x * ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_sub : ∀ {x y : ℚ}, (↑(x - y) : ℚ_[p]) = ↑x - ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_div : ∀ {x y : ℚ}, (↑(x / y) : ℚ_[p]) = ↑x / ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_one : (↑1 : ℚ_[p]) = 1 := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_zero : (↑0 : ℚ_[p]) = 0 := rfl lemma const_equiv {q r : ℚ} : const (padic_norm p) q ≈ const (padic_norm p) r ↔ q = r := ⟨ λ heq : lim_zero (const (padic_norm p) (q - r)), eq_of_sub_eq_zero $ const_lim_zero.1 heq, λ heq, by rw heq; apply setoid.refl _ ⟩ lemma of_rat_eq {q r : ℚ} : of_rat p q = of_rat p r ↔ q = r := ⟨(const_equiv p).1 ∘ quotient.eq.1, λ h, by rw h⟩ @[norm_cast] lemma coe_inj {q r : ℚ} : (↑q : ℚ_[p]) = ↑r ↔ q = r := by simp [cast_eq_of_rat, of_rat_eq] instance : char_zero ℚ_[p] := ⟨λ m n, by { rw ← rat.cast_coe_nat, norm_cast, exact id }⟩ end completion end padic /-- The rational-valued p-adic norm on `ℚ_p` is lifted from the norm on Cauchy sequences. The canonical form of this function is the normed space instance, with notation `∥ ∥`. -/ def padic_norm_e {p : ℕ} [hp : fact p.prime] : ℚ_[p] → ℚ := quotient.lift padic_seq.norm $ @padic_seq.norm_equiv _ _ namespace padic_norm_e section embedding open padic_seq variables {p : ℕ} [fact p.prime] lemma defn (f : padic_seq p) {ε : ℚ} (hε : 0 < ε) : ∃ N, ∀ i ≥ N, padic_norm_e (⟦f⟧ - f i) < ε := begin simp only [padic.cast_eq_of_rat], change ∃ N, ∀ i ≥ N, (f - const _ (f i)).norm < ε, by_contra' h, cases cauchy₂ f hε with N hN, rcases h N with ⟨i, hi, hge⟩, have hne : ¬ (f - const (padic_norm p) (f i)) ≈ 0, { intro h, unfold padic_seq.norm at hge; split_ifs at hge, exact not_lt_of_ge hge hε }, unfold padic_seq.norm at hge; split_ifs at hge, apply not_le_of_gt _ hge, cases em (N ≤ stationary_point hne) with hgen hngen, { apply hN _ hgen _ hi }, { have := stationary_point_spec hne le_rfl (le_of_not_le hngen), rw ←this, exact hN _ le_rfl _ hi }, end protected lemma nonneg (q : ℚ_[p]) : 0 ≤ padic_norm_e q := quotient.induction_on q $ norm_nonneg lemma zero_def : (0 : ℚ_[p]) = ⟦0⟧ := rfl lemma zero_iff (q : ℚ_[p]) : padic_norm_e q = 0 ↔ q = 0 := quotient.induction_on q $ by simpa only [zero_def, quotient.eq] using norm_zero_iff @[simp] protected lemma zero : padic_norm_e (0 : ℚ_[p]) = 0 := (zero_iff _).2 rfl /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`∥ ∥`). -/ @[simp] protected lemma one' : padic_norm_e (1 : ℚ_[p]) = 1 := norm_one @[simp] protected lemma neg (q : ℚ_[p]) : padic_norm_e (-q) = padic_norm_e q := quotient.induction_on q $ norm_neg /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`∥ ∥`). -/ theorem nonarchimedean' (q r : ℚ_[p]) : padic_norm_e (q + r) ≤ max (padic_norm_e q) (padic_norm_e r) := quotient.induction_on₂ q r $ norm_nonarchimedean /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`∥ ∥`). -/ theorem add_eq_max_of_ne' {q r : ℚ_[p]} : padic_norm_e q ≠ padic_norm_e r → padic_norm_e (q + r) = max (padic_norm_e q) (padic_norm_e r) := quotient.induction_on₂ q r $ λ _ _, padic_seq.add_eq_max_of_ne lemma triangle_ineq (x y z : ℚ_[p]) : padic_norm_e (x - z) ≤ padic_norm_e (x - y) + padic_norm_e (y - z) := calc padic_norm_e (x - z) = padic_norm_e ((x - y) + (y - z)) : by rw sub_add_sub_cancel ... ≤ max (padic_norm_e (x - y)) (padic_norm_e (y - z)) : padic_norm_e.nonarchimedean' _ _ ... ≤ padic_norm_e (x - y) + padic_norm_e (y - z) : max_le_add_of_nonneg (padic_norm_e.nonneg _) (padic_norm_e.nonneg _) protected lemma add (q r : ℚ_[p]) : padic_norm_e (q + r) ≤ (padic_norm_e q) + (padic_norm_e r) := calc padic_norm_e (q + r) ≤ max (padic_norm_e q) (padic_norm_e r) : nonarchimedean' _ _ ... ≤ (padic_norm_e q) + (padic_norm_e r) : max_le_add_of_nonneg (padic_norm_e.nonneg _) (padic_norm_e.nonneg _) protected lemma mul' (q r : ℚ_[p]) : padic_norm_e (q * r) = (padic_norm_e q) * (padic_norm_e r) := quotient.induction_on₂ q r $ norm_mul instance : is_absolute_value (@padic_norm_e p _) := { abv_nonneg := padic_norm_e.nonneg, abv_eq_zero := zero_iff, abv_add := padic_norm_e.add, abv_mul := padic_norm_e.mul' } @[simp] lemma eq_padic_norm' (q : ℚ) : padic_norm_e (padic.of_rat p q) = padic_norm p q := norm_const _ protected theorem image' {q : ℚ_[p]} : q ≠ 0 → ∃ n : ℤ, padic_norm_e q = p ^ (-n) := quotient.induction_on q $ λ f hf, have ¬ f ≈ 0, from (ne_zero_iff_nequiv_zero f).1 hf, norm_values_discrete f this lemma sub_rev (q r : ℚ_[p]) : padic_norm_e (q - r) = padic_norm_e (r - q) := by rw ←(padic_norm_e.neg); simp end embedding end padic_norm_e namespace padic section complete open padic_seq padic theorem rat_dense' {p : ℕ} [fact p.prime] (q : ℚ_[p]) {ε : ℚ} (hε : 0 < ε) : ∃ r : ℚ, padic_norm_e (q - r) < ε := quotient.induction_on q $ λ q', have ∃ N, ∀ m n ≥ N, padic_norm p (q' m - q' n) < ε, from cauchy₂ _ hε, let ⟨N, hN⟩ := this in ⟨q' N, begin simp only [padic.cast_eq_of_rat], change padic_seq.norm (q' - const _ (q' N)) < ε, cases decidable.em ((q' - const (padic_norm p) (q' N)) ≈ 0) with heq hne', { simpa only [heq, padic_seq.norm, dif_pos] }, { simp only [padic_seq.norm, dif_neg hne'], change padic_norm p (q' _ - q' _) < ε, have := stationary_point_spec hne', cases decidable.em (stationary_point hne' ≤ N) with hle hle, { have := eq.symm (this le_rfl hle), simp only [const_apply, sub_apply, padic_norm.zero, sub_self] at this, simpa only [this] }, { exact hN _ (lt_of_not_ge hle).le _ le_rfl } } end⟩ variables {p : ℕ} [fact p.prime] (f : cau_seq _ (@padic_norm_e p _)) open classical private lemma div_nat_pos (n : ℕ) : 0 < (1 / ((n + 1): ℚ)) := div_pos zero_lt_one (by exact_mod_cast succ_pos _) /-- `lim_seq f`, for `f` a Cauchy sequence of `p`-adic numbers, is a sequence of rationals with the same limit point as `f`. -/ def lim_seq : ℕ → ℚ := λ n, classical.some (rat_dense' (f n) (div_nat_pos n)) lemma exi_rat_seq_conv {ε : ℚ} (hε : 0 < ε) : ∃ N, ∀ i ≥ N, padic_norm_e (f i - ((lim_seq f) i : ℚ_[p])) < ε := begin refine (exists_nat_gt (1/ε)).imp (λ N hN i hi, _), have h := classical.some_spec (rat_dense' (f i) (div_nat_pos i)), refine lt_of_lt_of_le h ((div_le_iff' $ by exact_mod_cast succ_pos _).mpr _), rw right_distrib, apply le_add_of_le_of_nonneg, { exact (div_le_iff hε).mp (le_trans (le_of_lt hN) (by exact_mod_cast hi)) }, { apply le_of_lt, simpa } end lemma exi_rat_seq_conv_cauchy : is_cau_seq (padic_norm p) (lim_seq f) := assume ε hε, have hε3 : 0 < ε / 3, from div_pos hε (by norm_num), let ⟨N, hN⟩ := exi_rat_seq_conv f hε3, ⟨N2, hN2⟩ := f.cauchy₂ hε3 in begin existsi max N N2, intros j hj, suffices : padic_norm_e ((↑(lim_seq f j) - f (max N N2)) + (f (max N N2) - lim_seq f (max N N2))) < ε, { ring_nf at this ⊢, rw [← padic_norm_e.eq_padic_norm', ← padic.cast_eq_of_rat], exact_mod_cast this }, { apply lt_of_le_of_lt, { apply padic_norm_e.add }, { have : (3 : ℚ) ≠ 0, by norm_num, have : ε = ε / 3 + ε / 3 + ε / 3, { field_simp [this], simp only [bit0, bit1, mul_add, mul_one] }, rw this, apply add_lt_add, { suffices : padic_norm_e ((↑(lim_seq f j) - f j) + (f j - f (max N N2))) < ε / 3 + ε / 3, by simpa only [sub_add_sub_cancel], apply lt_of_le_of_lt, { apply padic_norm_e.add }, { apply add_lt_add, { rw [padic_norm_e.sub_rev], apply_mod_cast hN, exact le_of_max_le_left hj }, { exact hN2 _ (le_of_max_le_right hj) _ (le_max_right _ _) } } }, { apply_mod_cast hN, apply le_max_left }}} end private def lim' : padic_seq p := ⟨_, exi_rat_seq_conv_cauchy f⟩ private def lim : ℚ_[p] := ⟦lim' f⟧ theorem complete' : ∃ q : ℚ_[p], ∀ ε > 0, ∃ N, ∀ i ≥ N, padic_norm_e (q - f i) < ε := ⟨ lim f, λ ε hε, let ⟨N, hN⟩ := exi_rat_seq_conv f (show 0 < ε / 2, from div_pos hε (by norm_num)), ⟨N2, hN2⟩ := padic_norm_e.defn (lim' f) (show 0 < ε / 2, from div_pos hε (by norm_num)) in begin existsi max N N2, intros i hi, suffices : padic_norm_e ((lim f - lim' f i) + (lim' f i - f i)) < ε, { ring_nf at this; exact this }, { apply lt_of_le_of_lt, { apply padic_norm_e.add }, { have : ε = ε / 2 + ε / 2, by rw ←(add_self_div_two ε); simp, rw this, apply add_lt_add, { apply hN2, exact le_of_max_le_right hi }, { rw_mod_cast [padic_norm_e.sub_rev], apply hN, exact le_of_max_le_left hi }}} end ⟩ end complete section normed_space variables (p : ℕ) [fact p.prime] instance : has_dist ℚ_[p] := ⟨λ x y, padic_norm_e (x - y)⟩ instance : metric_space ℚ_[p] := { dist_self := by simp [dist], dist := dist, dist_comm := λ x y, by unfold dist; rw ←padic_norm_e.neg (x - y); simp, dist_triangle := begin intros, unfold dist, exact_mod_cast padic_norm_e.triangle_ineq _ _ _, end, eq_of_dist_eq_zero := begin unfold dist, intros _ _ h, apply eq_of_sub_eq_zero, apply (padic_norm_e.zero_iff _).1, exact_mod_cast h end } instance : has_norm ℚ_[p] := ⟨λ x, padic_norm_e x⟩ instance : normed_field ℚ_[p] := { dist_eq := λ _ _, rfl, norm_mul' := by simp [has_norm.norm, padic_norm_e.mul'], norm := norm, .. padic.field, .. padic.metric_space p } instance is_absolute_value : is_absolute_value (λ a : ℚ_[p], ∥a∥) := { abv_nonneg := norm_nonneg, abv_eq_zero := λ _, norm_eq_zero, abv_add := norm_add_le, abv_mul := by simp [has_norm.norm, padic_norm_e.mul'] } theorem rat_dense {p : ℕ} {hp : fact p.prime} (q : ℚ_[p]) {ε : ℝ} (hε : 0 < ε) : ∃ r : ℚ, ∥q - r∥ < ε := let ⟨ε', hε'l, hε'r⟩ := exists_rat_btwn hε, ⟨r, hr⟩ := rat_dense' q (by simpa using hε'l) in ⟨r, lt_trans (by simpa [has_norm.norm] using hr) hε'r⟩ end normed_space end padic namespace padic_norm_e section normed_space variables {p : ℕ} [hp : fact p.prime] include hp @[simp] protected lemma mul (q r : ℚ_[p]) : ∥q * r∥ = ∥q∥ * ∥r∥ := by simp [has_norm.norm, padic_norm_e.mul'] protected lemma is_norm (q : ℚ_[p]) : ↑(padic_norm_e q) = ∥q∥ := rfl theorem nonarchimedean (q r : ℚ_[p]) : ∥q + r∥ ≤ max (∥q∥) (∥r∥) := begin unfold has_norm.norm, exact_mod_cast nonarchimedean' _ _ end theorem add_eq_max_of_ne {q r : ℚ_[p]} (h : ∥q∥ ≠ ∥r∥) : ∥q+r∥ = max (∥q∥) (∥r∥) := begin unfold has_norm.norm, apply_mod_cast add_eq_max_of_ne', intro h', apply h, unfold has_norm.norm, exact_mod_cast h' end @[simp] lemma eq_padic_norm (q : ℚ) : ∥(↑q : ℚ_[p])∥ = padic_norm p q := begin unfold has_norm.norm, rw [← padic_norm_e.eq_padic_norm', ← padic.cast_eq_of_rat] end @[simp] lemma norm_p : ∥(p : ℚ_[p])∥ = p⁻¹ := begin have p₀ : p ≠ 0 := hp.1.ne_zero, have p₁ : p ≠ 1 := hp.1.ne_one, simp [p₀, p₁, norm, padic_norm, padic_val_rat, padic_val_int, zpow_neg, padic.cast_eq_of_rat_of_nat], end lemma norm_p_lt_one : ∥(p : ℚ_[p])∥ < 1 := begin rw norm_p, apply inv_lt_one, exact_mod_cast hp.1.one_lt end @[simp] lemma norm_p_pow (n : ℤ) : ∥(p^n : ℚ_[p])∥ = p^-n := by rw [norm_zpow, norm_p]; field_simp instance : nondiscrete_normed_field ℚ_[p] := { non_trivial := ⟨p⁻¹, begin rw [norm_inv, norm_p, inv_inv], exact_mod_cast hp.1.one_lt end⟩, .. padic.normed_field p } protected theorem image {q : ℚ_[p]} : q ≠ 0 → ∃ n : ℤ, ∥q∥ = ↑((↑p : ℚ) ^ (-n)) := quotient.induction_on q $ λ f hf, have ¬ f ≈ 0, from (padic_seq.ne_zero_iff_nequiv_zero f).1 hf, let ⟨n, hn⟩ := padic_seq.norm_values_discrete f this in ⟨n, congr_arg coe hn⟩ protected lemma is_rat (q : ℚ_[p]) : ∃ q' : ℚ, ∥q∥ = ↑q' := if h : q = 0 then ⟨0, by simp [h]⟩ else let ⟨n, hn⟩ := padic_norm_e.image h in ⟨_, hn⟩ /--`rat_norm q`, for a `p`-adic number `q` is the `p`-adic norm of `q`, as rational number. The lemma `padic_norm_e.eq_rat_norm` asserts `∥q∥ = rat_norm q`. -/ def rat_norm (q : ℚ_[p]) : ℚ := classical.some (padic_norm_e.is_rat q) lemma eq_rat_norm (q : ℚ_[p]) : ∥q∥ = rat_norm q := classical.some_spec (padic_norm_e.is_rat q) theorem norm_rat_le_one : ∀ {q : ℚ} (hq : ¬ p ∣ q.denom), ∥(q : ℚ_[p])∥ ≤ 1 | ⟨n, d, hn, hd⟩ := λ hq : ¬ p ∣ d, if hnz : n = 0 then have (⟨n, d, hn, hd⟩ : ℚ) = 0, from rat.zero_iff_num_zero.mpr hnz, by norm_num [this] else begin have hnz' : { rat . num := n, denom := d, pos := hn, cop := hd } ≠ 0, from mt rat.zero_iff_num_zero.1 hnz, rw [padic_norm_e.eq_padic_norm], norm_cast, rw [padic_norm.eq_zpow_of_nonzero p hnz', padic_val_rat, neg_sub, padic_val_nat.eq_zero_of_not_dvd hq], norm_cast, rw [zero_sub, zpow_neg, zpow_coe_nat], apply inv_le_one, { norm_cast, apply one_le_pow, exact hp.1.pos, }, end theorem norm_int_le_one (z : ℤ) : ∥(z : ℚ_[p])∥ ≤ 1 := suffices ∥((z : ℚ) : ℚ_[p])∥ ≤ 1, by simpa, norm_rat_le_one $ by simp [hp.1.ne_one] lemma norm_int_lt_one_iff_dvd (k : ℤ) : ∥(k : ℚ_[p])∥ < 1 ↔ ↑p ∣ k := begin split, { intro h, contrapose! h, apply le_of_eq, rw eq_comm, calc ∥(k : ℚ_[p])∥ = ∥((k : ℚ) : ℚ_[p])∥ : by { norm_cast } ... = padic_norm p k : padic_norm_e.eq_padic_norm _ ... = 1 : _, rw padic_norm, split_ifs with H, { exfalso, apply h, norm_cast at H, rw H, apply dvd_zero }, { norm_cast at H ⊢, convert zpow_zero _, rw [neg_eq_zero, padic_val_rat.of_int], norm_cast, apply padic_val_int.eq_zero_of_not_dvd h, } }, { rintro ⟨x, rfl⟩, push_cast, rw padic_norm_e.mul, calc _ ≤ ∥(p : ℚ_[p])∥ * 1 : mul_le_mul le_rfl (by simpa using norm_int_le_one _) (norm_nonneg _) (norm_nonneg _) ... < 1 : _, { rw [mul_one, padic_norm_e.norm_p], apply inv_lt_one, exact_mod_cast hp.1.one_lt }, }, end lemma norm_int_le_pow_iff_dvd (k : ℤ) (n : ℕ) : ∥(k : ℚ_[p])∥ ≤ ((↑p)^(-n : ℤ)) ↔ ↑(p^n) ∣ k := begin have : (p : ℝ) ^ (-n : ℤ) = ↑((p ^ (-n : ℤ) : ℚ)), {simp}, rw [show (k : ℚ_[p]) = ((k : ℚ) : ℚ_[p]), by norm_cast, eq_padic_norm, this], norm_cast, rw padic_norm.dvd_iff_norm_le, end lemma eq_of_norm_add_lt_right {p : ℕ} {hp : fact p.prime} {z1 z2 : ℚ_[p]} (h : ∥z1 + z2∥ < ∥z2∥) : ∥z1∥ = ∥z2∥ := by_contradiction $ λ hne, not_lt_of_ge (by rw padic_norm_e.add_eq_max_of_ne hne; apply le_max_right) h lemma eq_of_norm_add_lt_left {p : ℕ} {hp : fact p.prime} {z1 z2 : ℚ_[p]} (h : ∥z1 + z2∥ < ∥z1∥) : ∥z1∥ = ∥z2∥ := by_contradiction $ λ hne, not_lt_of_ge (by rw padic_norm_e.add_eq_max_of_ne hne; apply le_max_left) h end normed_space end padic_norm_e namespace padic variables {p : ℕ} [hp_prime : fact p.prime] include hp_prime set_option eqn_compiler.zeta true instance complete : cau_seq.is_complete ℚ_[p] norm := begin split, intro f, have cau_seq_norm_e : is_cau_seq padic_norm_e f, { intros ε hε, let h := is_cau f ε (by exact_mod_cast hε), unfold norm at h, apply_mod_cast h }, cases padic.complete' ⟨f, cau_seq_norm_e⟩ with q hq, existsi q, intros ε hε, cases exists_rat_btwn hε with ε' hε', norm_cast at hε', cases hq ε' hε'.1 with N hN, existsi N, intros i hi, let h := hN i hi, unfold norm, rw_mod_cast [cau_seq.sub_apply, padic_norm_e.sub_rev], refine lt_trans _ hε'.2, exact_mod_cast hN i hi end lemma padic_norm_e_lim_le {f : cau_seq ℚ_[p] norm} {a : ℝ} (ha : 0 < a) (hf : ∀ i, ∥f i∥ ≤ a) : ∥f.lim∥ ≤ a := let ⟨N, hN⟩ := setoid.symm (cau_seq.equiv_lim f) _ ha in calc ∥f.lim∥ = ∥f.lim - f N + f N∥ : by simp ... ≤ max (∥f.lim - f N∥) (∥f N∥) : padic_norm_e.nonarchimedean _ _ ... ≤ a : max_le (le_of_lt (hN _ le_rfl)) (hf _) open filter set instance : complete_space ℚ_[p] := begin apply complete_of_cauchy_seq_tendsto, intros u hu, let c : cau_seq ℚ_[p] norm := ⟨u, metric.cauchy_seq_iff'.mp hu⟩, refine ⟨c.lim, λ s h, _⟩, rcases metric.mem_nhds_iff.1 h with ⟨ε, ε0, hε⟩, have := c.equiv_lim ε ε0, simp only [mem_map, mem_at_top_sets, mem_set_of_eq], exact this.imp (λ N hN n hn, hε (hN n hn)) end /-! ### Valuation on `ℚ_[p]` -/ /-- `padic.valuation` lifts the p-adic valuation on rationals to `ℚ_[p]`. -/ def valuation : ℚ_[p] → ℤ := quotient.lift (@padic_seq.valuation p _) (λ f g h, begin by_cases hf : f ≈ 0, { have hg : g ≈ 0, from setoid.trans (setoid.symm h) hf, simp [hf, hg, padic_seq.valuation] }, { have hg : ¬ g ≈ 0, from (λ hg, hf (setoid.trans h hg)), rw padic_seq.val_eq_iff_norm_eq hf hg, exact padic_seq.norm_equiv h }, end) @[simp] lemma valuation_zero : valuation (0 : ℚ_[p]) = 0 := dif_pos ((const_equiv p).2 rfl) @[simp] lemma valuation_one : valuation (1 : ℚ_[p]) = 0 := begin change dite (cau_seq.const (padic_norm p) 1 ≈ _) _ _ = _, have h : ¬ cau_seq.const (padic_norm p) 1 ≈ 0, { assume H, erw const_equiv p at H, exact one_ne_zero H }, rw dif_neg h, simp, end lemma norm_eq_pow_val {x : ℚ_[p]} : x ≠ 0 → ∥x∥ = p^(-x.valuation) := begin apply quotient.induction_on' x, clear x, intros f hf, change (padic_seq.norm _ : ℝ) = (p : ℝ) ^ -padic_seq.valuation _, rw padic_seq.norm_eq_pow_val, change ↑((p : ℚ) ^ -padic_seq.valuation f) = (p : ℝ) ^ -padic_seq.valuation f, { rw rat.cast_zpow, congr' 1, norm_cast }, { apply cau_seq.not_lim_zero_of_not_congr_zero, contrapose! hf, apply quotient.sound, simpa using hf, } end @[simp] lemma valuation_p : valuation (p : ℚ_[p]) = 1 := begin have h : (1 : ℝ) < p := by exact_mod_cast (fact.out p.prime).one_lt, refine neg_injective ((zpow_strict_mono h).injective $ (norm_eq_pow_val _).symm.trans _), { exact_mod_cast (fact.out p.prime).ne_zero }, { simp } end lemma valuation_map_add {x y : ℚ_[p]} (hxy : x + y ≠ 0) : min (valuation x) (valuation y) ≤ valuation (x + y) := begin by_cases hx : x = 0, { rw [hx, zero_add], exact min_le_right _ _ }, { by_cases hy : y = 0, { rw [hy, add_zero], exact min_le_left _ _ }, { have h_norm : ∥x + y∥ ≤ (max ∥x∥ ∥y∥) := padic_norm_e.nonarchimedean x y, have hp_one : (1 : ℝ) < p, { rw [← nat.cast_one, nat.cast_lt], exact nat.prime.one_lt hp_prime.elim, }, rw [norm_eq_pow_val hx, norm_eq_pow_val hy, norm_eq_pow_val hxy] at h_norm, exact min_le_of_zpow_le_max hp_one h_norm }} end @[simp] lemma valuation_map_mul {x y : ℚ_[p]} (hx : x ≠ 0) (hy : y ≠ 0) : valuation (x * y) = valuation x + valuation y := begin have h_norm : ∥x * y∥ = ∥x∥ * ∥y∥ := norm_mul x y, have hp_ne_one : (p : ℝ) ≠ 1, { rw [← nat.cast_one, ne.def, nat.cast_inj], exact nat.prime.ne_one hp_prime.elim, }, have hp_pos : (0 : ℝ) < p, { rw [← nat.cast_zero, nat.cast_lt], exact nat.prime.pos hp_prime.elim }, rw [norm_eq_pow_val hx, norm_eq_pow_val hy, norm_eq_pow_val (mul_ne_zero hx hy), ← zpow_add₀ (ne_of_gt hp_pos), zpow_inj hp_pos hp_ne_one, ← neg_add, neg_inj] at h_norm, exact h_norm, end /-- The additive p-adic valuation on `ℚ_p`, with values in `with_top ℤ`. -/ def add_valuation_def : ℚ_[p] → (with_top ℤ) := λ x, if x = 0 then ⊤ else x.valuation @[simp] lemma add_valuation.map_zero : add_valuation_def (0 : ℚ_[p]) = ⊤ := by simp only [add_valuation_def, if_pos (eq.refl _)] @[simp] lemma add_valuation.map_one : add_valuation_def (1 : ℚ_[p]) = 0 := by simp only [add_valuation_def, if_neg (one_ne_zero), valuation_one, with_top.coe_zero] lemma add_valuation.map_mul (x y : ℚ_[p]) : add_valuation_def (x * y) = add_valuation_def x + add_valuation_def y := begin simp only [add_valuation_def], by_cases hx : x = 0, { rw [hx, if_pos (eq.refl _), zero_mul, if_pos (eq.refl _), with_top.top_add] }, { by_cases hy : y = 0, { rw [hy, if_pos (eq.refl _), mul_zero, if_pos (eq.refl _), with_top.add_top] }, { rw [if_neg hx, if_neg hy, if_neg (mul_ne_zero hx hy), ← with_top.coe_add, with_top.coe_eq_coe, valuation_map_mul hx hy] }} end lemma add_valuation.map_add (x y : ℚ_[p]) : min (add_valuation_def x) (add_valuation_def y) ≤ add_valuation_def (x + y) := begin simp only [add_valuation_def], by_cases hxy : x + y = 0, { rw [hxy, if_pos (eq.refl _)], exact le_top, }, { by_cases hx : x = 0, { simp only [hx, if_pos (eq.refl _), min_eq_right, le_top, zero_add, le_refl] }, { by_cases hy : y = 0, { simp only [hy, if_pos (eq.refl _), min_eq_left, le_top, add_zero, le_refl], }, { rw [if_neg hx, if_neg hy, if_neg hxy, ← with_top.coe_min, with_top.coe_le_coe], exact valuation_map_add hxy }}} end /-- The additive `p`-adic valuation on `ℚ_p`, as an `add_valuation`. -/ def add_valuation : add_valuation ℚ_[p] (with_top ℤ) := add_valuation.of add_valuation_def add_valuation.map_zero add_valuation.map_one add_valuation.map_add add_valuation.map_mul @[simp] lemma add_valuation.apply {x : ℚ_[p]} (hx : x ≠ 0) : x.add_valuation = x.valuation := by simp only [add_valuation, add_valuation.of_apply, add_valuation_def, if_neg hx] section norm_le_iff /-! ### Various characterizations of open unit balls -/ lemma norm_le_pow_iff_norm_lt_pow_add_one (x : ℚ_[p]) (n : ℤ) : ∥x∥ ≤ p ^ n ↔ ∥x∥ < p ^ (n + 1) := begin have aux : ∀ n : ℤ, 0 < (p ^ n : ℝ), { apply nat.zpow_pos_of_pos, exact hp_prime.1.pos }, by_cases hx0 : x = 0, { simp [hx0, norm_zero, aux, le_of_lt (aux _)], }, rw norm_eq_pow_val hx0, have h1p : 1 < (p : ℝ), { exact_mod_cast hp_prime.1.one_lt }, have H := zpow_strict_mono h1p, rw [H.le_iff_le, H.lt_iff_lt, int.lt_add_one_iff], end lemma norm_lt_pow_iff_norm_le_pow_sub_one (x : ℚ_[p]) (n : ℤ) : ∥x∥ < p ^ n ↔ ∥x∥ ≤ p ^ (n - 1) := by rw [norm_le_pow_iff_norm_lt_pow_add_one, sub_add_cancel] end norm_le_iff end padic
097d2a7bb316430ba3d7c825c96a3d6ddf573447
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/affine_space/matrix.lean
fd7eb1c68674943d0e38fdcc2d28a7e17070f3bf
[ "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,825
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import linear_algebra.affine_space.basis import linear_algebra.determinant /-! # Matrix results for barycentric co-ordinates > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Results about the matrix of barycentric co-ordinates for a family of points in an affine space, with respect to some affine basis. -/ open_locale affine big_operators matrix open set universes u₁ u₂ u₃ u₄ variables {ι : Type u₁} {k : Type u₂} {V : Type u₃} {P : Type u₄} variables [add_comm_group V] [affine_space V P] namespace affine_basis section ring variables [ring k] [module k V] (b : affine_basis ι k P) /-- Given an affine basis `p`, and a family of points `q : ι' → P`, this is the matrix whose rows are the barycentric coordinates of `q` with respect to `p`. It is an affine equivalent of `basis.to_matrix`. -/ noncomputable def to_matrix {ι' : Type*} (q : ι' → P) : matrix ι' ι k := λ i j, b.coord j (q i) @[simp] lemma to_matrix_apply {ι' : Type*} (q : ι' → P) (i : ι') (j : ι) : b.to_matrix q i j = b.coord j (q i) := rfl @[simp] lemma to_matrix_self [decidable_eq ι] : b.to_matrix b = (1 : matrix ι ι k) := begin ext i j, rw [to_matrix_apply, coord_apply, matrix.one_eq_pi_single, pi.single_apply], end variables {ι' : Type*} [fintype ι'] [fintype ι] (b₂ : affine_basis ι k P) lemma to_matrix_row_sum_one {ι' : Type*} (q : ι' → P) (i : ι') : ∑ j, b.to_matrix q i j = 1 := by simp /-- Given a family of points `p : ι' → P` and an affine basis `b`, if the matrix whose rows are the coordinates of `p` with respect `b` has a right inverse, then `p` is affine independent. -/ lemma affine_independent_of_to_matrix_right_inv [decidable_eq ι'] (p : ι' → P) {A : matrix ι ι' k} (hA : (b.to_matrix p) ⬝ A = 1) : affine_independent k p := begin rw affine_independent_iff_eq_of_fintype_affine_combination_eq, intros w₁ w₂ hw₁ hw₂ hweq, have hweq' : (b.to_matrix p).vec_mul w₁ = (b.to_matrix p).vec_mul w₂, { ext j, change ∑ i, (w₁ i) • (b.coord j (p i)) = ∑ i, (w₂ i) • (b.coord j (p i)), rw [← finset.univ.affine_combination_eq_linear_combination _ _ hw₁, ← finset.univ.affine_combination_eq_linear_combination _ _ hw₂, ← finset.univ.map_affine_combination p w₁ hw₁, ← finset.univ.map_affine_combination p w₂ hw₂, hweq], }, replace hweq' := congr_arg (λ w, A.vec_mul w) hweq', simpa only [matrix.vec_mul_vec_mul, ← matrix.mul_eq_mul, hA, matrix.vec_mul_one] using hweq', end /-- Given a family of points `p : ι' → P` and an affine basis `b`, if the matrix whose rows are the coordinates of `p` with respect `b` has a left inverse, then `p` spans the entire space. -/ lemma affine_span_eq_top_of_to_matrix_left_inv [decidable_eq ι] [nontrivial k] (p : ι' → P) {A : matrix ι ι' k} (hA : A ⬝ b.to_matrix p = 1) : affine_span k (range p) = ⊤ := begin suffices : ∀ i, b i ∈ affine_span k (range p), { rw [eq_top_iff, ← b.tot, affine_span_le], rintros q ⟨i, rfl⟩, exact this i, }, intros i, have hAi : ∑ j, A i j = 1, { calc ∑ j, A i j = ∑ j, (A i j) * ∑ l, b.to_matrix p j l : by simp ... = ∑ j, ∑ l, (A i j) * b.to_matrix p j l : by simp_rw finset.mul_sum ... = ∑ l, ∑ j, (A i j) * b.to_matrix p j l : by rw finset.sum_comm ... = ∑ l, (A ⬝ b.to_matrix p) i l : rfl ... = 1 : by simp [hA, matrix.one_apply, finset.filter_eq], }, have hbi : b i = finset.univ.affine_combination k p (A i), { apply b.ext_elem, intros j, rw [b.coord_apply, finset.univ.map_affine_combination _ _ hAi, finset.univ.affine_combination_eq_linear_combination _ _ hAi], change _ = (A ⬝ b.to_matrix p) i j, simp_rw [hA, matrix.one_apply, @eq_comm _ i j] }, rw hbi, exact affine_combination_mem_affine_span hAi p, end /-- A change of basis formula for barycentric coordinates. See also `affine_basis.to_matrix_inv_mul_affine_basis_to_matrix`. -/ @[simp] lemma to_matrix_vec_mul_coords (x : P) : (b.to_matrix b₂).vec_mul (b₂.coords x) = b.coords x := begin ext j, change _ = b.coord j x, conv_rhs { rw ← b₂.affine_combination_coord_eq_self x, }, rw finset.map_affine_combination _ _ _ (b₂.sum_coord_apply_eq_one x), simp [matrix.vec_mul, matrix.dot_product, to_matrix_apply, coords], end variables [decidable_eq ι] lemma to_matrix_mul_to_matrix : (b.to_matrix b₂) ⬝ (b₂.to_matrix b) = 1 := begin ext l m, change (b₂.to_matrix b).vec_mul (b.coords (b₂ l)) m = _, rw [to_matrix_vec_mul_coords, coords_apply, ← to_matrix_apply, to_matrix_self], end lemma is_unit_to_matrix : is_unit (b.to_matrix b₂) := ⟨{ val := b.to_matrix b₂, inv := b₂.to_matrix b, val_inv := b.to_matrix_mul_to_matrix b₂, inv_val := b₂.to_matrix_mul_to_matrix b, }, rfl⟩ lemma is_unit_to_matrix_iff [nontrivial k] (p : ι → P) : is_unit (b.to_matrix p) ↔ affine_independent k p ∧ affine_span k (range p) = ⊤ := begin split, { rintros ⟨⟨B, A, hA, hA'⟩, (rfl : B = b.to_matrix p)⟩, rw matrix.mul_eq_mul at hA hA', exact ⟨b.affine_independent_of_to_matrix_right_inv p hA, b.affine_span_eq_top_of_to_matrix_left_inv p hA'⟩, }, { rintros ⟨h_tot, h_ind⟩, let b' : affine_basis ι k P := ⟨p, h_tot, h_ind⟩, change is_unit (b.to_matrix b'), exact b.is_unit_to_matrix b', }, end end ring section comm_ring variables [comm_ring k] [module k V] [decidable_eq ι] [fintype ι] variables (b b₂ : affine_basis ι k P) /-- A change of basis formula for barycentric coordinates. See also `affine_basis.to_matrix_vec_mul_coords`. -/ @[simp] lemma to_matrix_inv_vec_mul_to_matrix (x : P) : (b.to_matrix b₂)⁻¹.vec_mul (b.coords x) = b₂.coords x := begin have hu := b.is_unit_to_matrix b₂, rw matrix.is_unit_iff_is_unit_det at hu, rw [← b.to_matrix_vec_mul_coords b₂, matrix.vec_mul_vec_mul, matrix.mul_nonsing_inv _ hu, matrix.vec_mul_one], end /-- If we fix a background affine basis `b`, then for any other basis `b₂`, we can characterise the barycentric coordinates provided by `b₂` in terms of determinants relative to `b`. -/ lemma det_smul_coords_eq_cramer_coords (x : P) : (b.to_matrix b₂).det • b₂.coords x = (b.to_matrix b₂)ᵀ.cramer (b.coords x) := begin have hu := b.is_unit_to_matrix b₂, rw matrix.is_unit_iff_is_unit_det at hu, rw [← b.to_matrix_inv_vec_mul_to_matrix, matrix.det_smul_inv_vec_mul_eq_cramer_transpose _ _ hu], end end comm_ring end affine_basis
a1b6a53565b92e086c5b8627cbf7c8769812c421
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/matrix/basic.lean
673c7a813acd1913d5790ed1658ee7250f868b3a
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
39,244
lean
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin -/ import algebra.big_operators.pi import algebra.module.pi import algebra.big_operators.ring import data.fintype.card /-! # Matrices -/ universes u u' v w open_locale big_operators /-- `matrix m n` is the type of matrices whose rows are indexed by the fintype `m` and whose columns are indexed by the fintype `n`. -/ @[nolint unused_arguments] def matrix (m : Type u) (n : Type u') [fintype m] [fintype n] (α : Type v) : Type (max u u' v) := m → n → α variables {l m n o : Type*} [fintype l] [fintype m] [fintype n] [fintype o] variables {α : Type v} namespace matrix section ext variables {M N : matrix m n α} theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N := ⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩ @[ext] theorem ext : (∀ i j, M i j = N i j) → M = N := ext_iff.mp end ext /-- `M.map f` is the matrix obtained by applying `f` to each entry of the matrix `M`. -/ def map (M : matrix m n α) {β : Type w} (f : α → β) : matrix m n β := λ i j, f (M i j) @[simp] lemma map_apply {M : matrix m n α} {β : Type w} {f : α → β} {i : m} {j : n} : M.map f i j = f (M i j) := rfl /-- The transpose of a matrix. -/ def transpose (M : matrix m n α) : matrix n m α | x y := M y x localized "postfix `ᵀ`:1500 := matrix.transpose" in matrix /-- `matrix.col u` is the column matrix whose entries are given by `u`. -/ def col (w : m → α) : matrix m unit α | x y := w x /-- `matrix.row u` is the row matrix whose entries are given by `u`. -/ def row (v : n → α) : matrix unit n α | x y := v y instance [inhabited α] : inhabited (matrix m n α) := pi.inhabited _ instance [has_add α] : has_add (matrix m n α) := pi.has_add instance [add_semigroup α] : add_semigroup (matrix m n α) := pi.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (matrix m n α) := pi.add_comm_semigroup instance [has_zero α] : has_zero (matrix m n α) := pi.has_zero instance [add_monoid α] : add_monoid (matrix m n α) := pi.add_monoid instance [add_comm_monoid α] : add_comm_monoid (matrix m n α) := pi.add_comm_monoid instance [has_neg α] : has_neg (matrix m n α) := pi.has_neg instance [add_group α] : add_group (matrix m n α) := pi.add_group instance [add_comm_group α] : add_comm_group (matrix m n α) := pi.add_comm_group @[simp] theorem zero_apply [has_zero α] (i j) : (0 : matrix m n α) i j = 0 := rfl @[simp] theorem neg_apply [has_neg α] (M : matrix m n α) (i j) : (- M) i j = - M i j := rfl @[simp] theorem add_apply [has_add α] (M N : matrix m n α) (i j) : (M + N) i j = M i j + N i j := rfl @[simp] lemma map_zero [has_zero α] {β : Type w} [has_zero β] {f : α → β} (h : f 0 = 0) : (0 : matrix m n α).map f = 0 := by { ext, simp [h], } lemma map_add [add_monoid α] {β : Type w} [add_monoid β] (f : α →+ β) (M N : matrix m n α) : (M + N).map f = M.map f + N.map f := by { ext, simp, } lemma map_sub [add_group α] {β : Type w} [add_group β] (f : α →+ β) (M N : matrix m n α) : (M - N).map f = M.map f - N.map f := by { ext, simp } lemma subsingleton_of_empty_left (hm : ¬ nonempty m) : subsingleton (matrix m n α) := ⟨λ M N, by { ext, contrapose! hm, use i }⟩ lemma subsingleton_of_empty_right (hn : ¬ nonempty n) : subsingleton (matrix m n α) := ⟨λ M N, by { ext, contrapose! hn, use j }⟩ end matrix /-- The `add_monoid_hom` between spaces of matrices induced by an `add_monoid_hom` between their coefficients. -/ def add_monoid_hom.map_matrix [add_monoid α] {β : Type w} [add_monoid β] (f : α →+ β) : matrix m n α →+ matrix m n β := { to_fun := λ M, M.map f, map_zero' := by simp, map_add' := matrix.map_add f, } @[simp] lemma add_monoid_hom.map_matrix_apply [add_monoid α] {β : Type w} [add_monoid β] (f : α →+ β) (M : matrix m n α) : f.map_matrix M = M.map f := rfl open_locale matrix namespace matrix section diagonal variables [decidable_eq n] /-- `diagonal d` is the square matrix such that `(diagonal d) i i = d i` and `(diagonal d) i j = 0` if `i ≠ j`. -/ def diagonal [has_zero α] (d : n → α) : matrix n n α := λ i j, if i = j then d i else 0 @[simp] theorem diagonal_apply_eq [has_zero α] {d : n → α} (i : n) : (diagonal d) i i = d i := by simp [diagonal] @[simp] theorem diagonal_apply_ne [has_zero α] {d : n → α} {i j : n} (h : i ≠ j) : (diagonal d) i j = 0 := by simp [diagonal, h] theorem diagonal_apply_ne' [has_zero α] {d : n → α} {i j : n} (h : j ≠ i) : (diagonal d) i j = 0 := diagonal_apply_ne h.symm @[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 := by simp [diagonal]; refl @[simp] lemma diagonal_transpose [has_zero α] (v : n → α) : (diagonal v)ᵀ = diagonal v := begin ext i j, by_cases h : i = j, { simp [h, transpose] }, { simp [h, transpose, diagonal_apply_ne' h] } end @[simp] theorem diagonal_add [add_monoid α] (d₁ d₂ : n → α) : diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) := by ext i j; by_cases h : i = j; simp [h] @[simp] lemma diagonal_map {β : Type w} [has_zero α] [has_zero β] {f : α → β} (h : f 0 = 0) {d : n → α} : (diagonal d).map f = diagonal (λ m, f (d m)) := by { ext, simp only [diagonal, map_apply], split_ifs; simp [h], } section one variables [has_zero α] [has_one α] instance : has_one (matrix n n α) := ⟨diagonal (λ _, 1)⟩ @[simp] theorem diagonal_one : (diagonal (λ _, 1) : matrix n n α) = 1 := rfl theorem one_apply {i j} : (1 : matrix n n α) i j = if i = j then 1 else 0 := rfl @[simp] theorem one_apply_eq (i) : (1 : matrix n n α) i i = 1 := diagonal_apply_eq i @[simp] theorem one_apply_ne {i j} : i ≠ j → (1 : matrix n n α) i j = 0 := diagonal_apply_ne theorem one_apply_ne' {i j} : j ≠ i → (1 : matrix n n α) i j = 0 := diagonal_apply_ne' @[simp] lemma one_map {β : Type w} [has_zero β] [has_one β] {f : α → β} (h₀ : f 0 = 0) (h₁ : f 1 = 1) : (1 : matrix n n α).map f = (1 : matrix n n β) := by { ext, simp only [one_apply, map_apply], split_ifs; simp [h₀, h₁], } end one section numeral @[simp] lemma bit0_apply [has_add α] (M : matrix m m α) (i : m) (j : m) : (bit0 M) i j = bit0 (M i j) := rfl variables [add_monoid α] [has_one α] lemma bit1_apply (M : matrix n n α) (i : n) (j : n) : (bit1 M) i j = if i = j then bit1 (M i j) else bit0 (M i j) := by dsimp [bit1]; by_cases h : i = j; simp [h] @[simp] lemma bit1_apply_eq (M : matrix n n α) (i : n) : (bit1 M) i i = bit1 (M i i) := by simp [bit1_apply] @[simp] lemma bit1_apply_ne (M : matrix n n α) {i j : n} (h : i ≠ j) : (bit1 M) i j = bit0 (M i j) := by simp [bit1_apply, h] end numeral end diagonal section dot_product /-- `dot_product v w` is the sum of the entrywise products `v i * w i` -/ def dot_product [has_mul α] [add_comm_monoid α] (v w : m → α) : α := ∑ i, v i * w i lemma dot_product_assoc [semiring α] (u : m → α) (v : m → n → α) (w : n → α) : dot_product (λ j, dot_product u (λ i, v i j)) w = dot_product u (λ i, dot_product (v i) w) := by simpa [dot_product, finset.mul_sum, finset.sum_mul, mul_assoc] using finset.sum_comm lemma dot_product_comm [comm_semiring α] (v w : m → α) : dot_product v w = dot_product w v := by simp_rw [dot_product, mul_comm] @[simp] lemma dot_product_punit [add_comm_monoid α] [has_mul α] (v w : punit → α) : dot_product v w = v ⟨⟩ * w ⟨⟩ := by simp [dot_product] @[simp] lemma dot_product_zero [semiring α] (v : m → α) : dot_product v 0 = 0 := by simp [dot_product] @[simp] lemma dot_product_zero' [semiring α] (v : m → α) : dot_product v (λ _, 0) = 0 := dot_product_zero v @[simp] lemma zero_dot_product [semiring α] (v : m → α) : dot_product 0 v = 0 := by simp [dot_product] @[simp] lemma zero_dot_product' [semiring α] (v : m → α) : dot_product (λ _, (0 : α)) v = 0 := zero_dot_product v @[simp] lemma add_dot_product [semiring α] (u v w : m → α) : dot_product (u + v) w = dot_product u w + dot_product v w := by simp [dot_product, add_mul, finset.sum_add_distrib] @[simp] lemma dot_product_add [semiring α] (u v w : m → α) : dot_product u (v + w) = dot_product u v + dot_product u w := by simp [dot_product, mul_add, finset.sum_add_distrib] @[simp] lemma diagonal_dot_product [decidable_eq m] [semiring α] (v w : m → α) (i : m) : dot_product (diagonal v i) w = v i * w i := have ∀ j ≠ i, diagonal v i j * w j = 0 := λ j hij, by simp [diagonal_apply_ne' hij], by convert finset.sum_eq_single i (λ j _, this j) _; simp @[simp] lemma dot_product_diagonal [decidable_eq m] [semiring α] (v w : m → α) (i : m) : dot_product v (diagonal w i) = v i * w i := have ∀ j ≠ i, v j * diagonal w i j = 0 := λ j hij, by simp [diagonal_apply_ne' hij], by convert finset.sum_eq_single i (λ j _, this j) _; simp @[simp] lemma dot_product_diagonal' [decidable_eq m] [semiring α] (v w : m → α) (i : m) : dot_product v (λ j, diagonal w j i) = v i * w i := have ∀ j ≠ i, v j * diagonal w j i = 0 := λ j hij, by simp [diagonal_apply_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _; simp @[simp] lemma neg_dot_product [ring α] (v w : m → α) : dot_product (-v) w = - dot_product v w := by simp [dot_product] @[simp] lemma dot_product_neg [ring α] (v w : m → α) : dot_product v (-w) = - dot_product v w := by simp [dot_product] @[simp] lemma smul_dot_product [semiring α] (x : α) (v w : m → α) : dot_product (x • v) w = x * dot_product v w := by simp [dot_product, finset.mul_sum, mul_assoc] @[simp] lemma dot_product_smul [comm_semiring α] (x : α) (v w : m → α) : dot_product v (x • w) = x * dot_product v w := by simp [dot_product, finset.mul_sum, mul_assoc, mul_comm, mul_left_comm] end dot_product /-- `M ⬝ N` is the usual product of matrices `M` and `N`, i.e. we have that `(M ⬝ N) i k` is the dot product of the `i`-th row of `M` by the `k`-th column of `Ǹ`. -/ protected def mul [has_mul α] [add_comm_monoid α] (M : matrix l m α) (N : matrix m n α) : matrix l n α := λ i k, dot_product (λ j, M i j) (λ j, N j k) localized "infixl ` ⬝ `:75 := matrix.mul" in matrix theorem mul_apply [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = ∑ j, M i j * N j k := rfl instance [has_mul α] [add_comm_monoid α] : has_mul (matrix n n α) := ⟨matrix.mul⟩ @[simp] theorem mul_eq_mul [has_mul α] [add_comm_monoid α] (M N : matrix n n α) : M * N = M ⬝ N := rfl theorem mul_apply' [has_mul α] [add_comm_monoid α] {M N : matrix n n α} {i k} : (M ⬝ N) i k = dot_product (λ j, M i j) (λ j, N j k) := rfl section semigroup variables [semiring α] protected theorem mul_assoc (L : matrix l m α) (M : matrix m n α) (N : matrix n o α) : (L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) := by { ext, apply dot_product_assoc } instance : semigroup (matrix n n α) := { mul_assoc := matrix.mul_assoc, ..matrix.has_mul } end semigroup @[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) : -diagonal d = diagonal (λ i, -d i) := by ext i j; by_cases i = j; simp [h] section semiring variables [semiring α] @[simp] protected theorem mul_zero (M : matrix m n α) : M ⬝ (0 : matrix n o α) = 0 := by { ext i j, apply dot_product_zero } @[simp] protected theorem zero_mul (M : matrix m n α) : (0 : matrix l m α) ⬝ M = 0 := by { ext i j, apply zero_dot_product } protected theorem mul_add (L : matrix m n α) (M N : matrix n o α) : L ⬝ (M + N) = L ⬝ M + L ⬝ N := by { ext i j, apply dot_product_add } protected theorem add_mul (L M : matrix l m α) (N : matrix m n α) : (L + M) ⬝ N = L ⬝ N + M ⬝ N := by { ext i j, apply add_dot_product } @[simp] theorem diagonal_mul [decidable_eq m] (d : m → α) (M : matrix m n α) (i j) : (diagonal d).mul M i j = d i * M i j := diagonal_dot_product _ _ _ @[simp] theorem mul_diagonal [decidable_eq n] (d : n → α) (M : matrix m n α) (i j) : (M ⬝ diagonal d) i j = M i j * d j := by { rw ← diagonal_transpose, apply dot_product_diagonal } @[simp] protected theorem one_mul [decidable_eq m] (M : matrix m n α) : (1 : matrix m m α) ⬝ M = M := by ext i j; rw [← diagonal_one, diagonal_mul, one_mul] @[simp] protected theorem mul_one [decidable_eq n] (M : matrix m n α) : M ⬝ (1 : matrix n n α) = M := by ext i j; rw [← diagonal_one, mul_diagonal, mul_one] instance [decidable_eq n] : monoid (matrix n n α) := { one_mul := matrix.one_mul, mul_one := matrix.mul_one, ..matrix.has_one, ..matrix.semigroup } instance [decidable_eq n] : semiring (matrix n n α) := { mul_zero := matrix.mul_zero, zero_mul := matrix.zero_mul, left_distrib := matrix.mul_add, right_distrib := matrix.add_mul, ..matrix.add_comm_monoid, ..matrix.monoid } @[simp] theorem diagonal_mul_diagonal [decidable_eq n] (d₁ d₂ : n → α) : (diagonal d₁) ⬝ (diagonal d₂) = diagonal (λ i, d₁ i * d₂ i) := by ext i j; by_cases i = j; simp [h] theorem diagonal_mul_diagonal' [decidable_eq n] (d₁ d₂ : n → α) : diagonal d₁ * diagonal d₂ = diagonal (λ i, d₁ i * d₂ i) := diagonal_mul_diagonal _ _ lemma map_mul {L : matrix m n α} {M : matrix n o α} {β : Type w} [semiring β] {f : α →+* β} : (L ⬝ M).map f = L.map f ⬝ M.map f := by { ext, simp [mul_apply, ring_hom.map_sum], } lemma is_add_monoid_hom_mul_left (M : matrix l m α) : is_add_monoid_hom (λ x : matrix m n α, M ⬝ x) := { to_is_add_hom := ⟨matrix.mul_add _⟩, map_zero := matrix.mul_zero _ } lemma is_add_monoid_hom_mul_right (M : matrix m n α) : is_add_monoid_hom (λ x : matrix l m α, x ⬝ M) := { to_is_add_hom := ⟨λ _ _, matrix.add_mul _ _ _⟩, map_zero := matrix.zero_mul _ } protected lemma sum_mul {β : Type*} (s : finset β) (f : β → matrix l m α) (M : matrix m n α) : (∑ a in s, f a) ⬝ M = ∑ a in s, f a ⬝ M := (@finset.sum_hom _ _ _ _ _ s f (λ x, x ⬝ M) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_right l _ _ _ _ _ _ _ M) : _)).symm protected lemma mul_sum {β : Type*} (s : finset β) (f : β → matrix m n α) (M : matrix l m α) : M ⬝ ∑ a in s, f a = ∑ a in s, M ⬝ f a := (@finset.sum_hom _ _ _ _ _ s f (λ x, M ⬝ x) /- This line does not type-check without `id` and `: _`. Lean did not recognize that two different `add_monoid` instances were def-eq -/ (id (@is_add_monoid_hom_mul_left _ _ n _ _ _ _ _ M) : _)).symm @[simp] lemma row_mul_col_apply (v w : m → α) (i j) : (row v ⬝ col w) i j = dot_product v w := rfl end semiring end matrix /-- The `ring_hom` between spaces of square matrices induced by a `ring_hom` between their coefficients. -/ def ring_hom.map_matrix [decidable_eq m] [semiring α] {β : Type w} [semiring β] (f : α →+* β) : matrix m m α →+* matrix m m β := { to_fun := λ M, M.map f, map_one' := by simp, map_mul' := λ L M, matrix.map_mul, ..(f.to_add_monoid_hom).map_matrix } @[simp] lemma ring_hom.map_matrix_apply [decidable_eq m] [semiring α] {β : Type w} [semiring β] (f : α →+* β) (M : matrix m m α) : f.map_matrix M = M.map f := rfl open_locale matrix namespace matrix section ring variables [ring α] @[simp] theorem neg_mul (M : matrix m n α) (N : matrix n o α) : (-M) ⬝ N = -(M ⬝ N) := by { ext, apply neg_dot_product } @[simp] theorem mul_neg (M : matrix m n α) (N : matrix n o α) : M ⬝ (-N) = -(M ⬝ N) := by { ext, apply dot_product_neg } protected theorem sub_mul (M M' : matrix m n α) (N : matrix n o α) : (M - M') ⬝ N = M ⬝ N - M' ⬝ N := by rw [sub_eq_add_neg, matrix.add_mul, neg_mul, sub_eq_add_neg] protected theorem mul_sub (M : matrix m n α) (N N' : matrix n o α) : M ⬝ (N - N') = M ⬝ N - M ⬝ N' := by rw [sub_eq_add_neg, matrix.mul_add, mul_neg, sub_eq_add_neg] end ring instance [decidable_eq n] [ring α] : ring (matrix n n α) := { ..matrix.semiring, ..matrix.add_comm_group } instance [semiring α] : has_scalar α (matrix m n α) := pi.has_scalar instance {β : Type w} [semiring α] [add_comm_monoid β] [semimodule α β] : semimodule α (matrix m n β) := pi.semimodule _ _ _ @[simp] lemma smul_apply [semiring α] (a : α) (A : matrix m n α) (i : m) (j : n) : (a • A) i j = a * A i j := rfl section semiring variables [semiring α] lemma smul_eq_diagonal_mul [decidable_eq m] (M : matrix m n α) (a : α) : a • M = diagonal (λ _, a) ⬝ M := by { ext, simp } @[simp] lemma smul_mul (M : matrix m n α) (a : α) (N : matrix n l α) : (a • M) ⬝ N = a • M ⬝ N := by { ext, apply smul_dot_product } @[simp] lemma mul_mul_left (M : matrix m n α) (N : matrix n o α) (a : α) : (λ i j, a * M i j) ⬝ N = a • (M ⬝ N) := begin simp only [←smul_apply], simp, end /-- The ring homomorphism `α →+* matrix n n α` sending `a` to the diagonal matrix with `a` on the diagonal. -/ def scalar (n : Type u) [decidable_eq n] [fintype n] : α →+* matrix n n α := { to_fun := λ a, a • 1, map_zero' := by simp, map_add' := by { intros, ext, simp [add_mul], }, map_one' := by simp, map_mul' := by { intros, ext, simp [mul_assoc], }, } section scalar variable [decidable_eq n] @[simp] lemma coe_scalar : (scalar n : α → matrix n n α) = λ a, a • 1 := rfl lemma scalar_apply_eq (a : α) (i : n) : scalar n a i i = a := by simp only [coe_scalar, mul_one, one_apply_eq, smul_apply] lemma scalar_apply_ne (a : α) (i j : n) (h : i ≠ j) : scalar n a i j = 0 := by simp only [h, coe_scalar, one_apply_ne, ne.def, not_false_iff, smul_apply, mul_zero] lemma scalar_inj [nonempty n] {r s : α} : scalar n r = scalar n s ↔ r = s := begin split, { intro h, inhabit n, rw [← scalar_apply_eq r (arbitrary n), ← scalar_apply_eq s (arbitrary n), h] }, { rintro rfl, refl } end end scalar end semiring section comm_semiring variables [comm_semiring α] lemma smul_eq_mul_diagonal [decidable_eq n] (M : matrix m n α) (a : α) : a • M = M ⬝ diagonal (λ _, a) := by { ext, simp [mul_comm] } @[simp] lemma mul_smul (M : matrix m n α) (a : α) (N : matrix n l α) : M ⬝ (a • N) = a • M ⬝ N := by { ext, apply dot_product_smul } @[simp] lemma mul_mul_right (M : matrix m n α) (N : matrix n o α) (a : α) : M ⬝ (λ i j, a * N i j) = a • (M ⬝ N) := begin simp only [←smul_apply], simp, end lemma scalar.commute [decidable_eq n] (r : α) (M : matrix n n α) : commute (scalar n r) M := by simp [commute, semiconj_by] end comm_semiring section semiring variables [semiring α] /-- For two vectors `w` and `v`, `vec_mul_vec w v i j` is defined to be `w i * v j`. Put another way, `vec_mul_vec w v` is exactly `col w ⬝ row v`. -/ def vec_mul_vec (w : m → α) (v : n → α) : matrix m n α | x y := w x * v y /-- `mul_vec M v` is the matrix-vector product of `M` and `v`, where `v` is seen as a column matrix. Put another way, `mul_vec M v` is the vector whose entries are those of `M ⬝ col v` (see `col_mul_vec`). -/ def mul_vec (M : matrix m n α) (v : n → α) : m → α | i := dot_product (λ j, M i j) v /-- `vec_mul v M` is the vector-matrix product of `v` and `M`, where `v` is seen as a row matrix. Put another way, `vec_mul v M` is the vector whose entries are those of `row v ⬝ M` (see `row_vec_mul`). -/ def vec_mul (v : m → α) (M : matrix m n α) : n → α | j := dot_product v (λ i, M i j) instance mul_vec.is_add_monoid_hom_left (v : n → α) : is_add_monoid_hom (λM:matrix m n α, mul_vec M v) := { map_zero := by ext; simp [mul_vec]; refl, map_add := begin intros x y, ext m, apply add_dot_product end } lemma mul_vec_diagonal [decidable_eq m] (v w : m → α) (x : m) : mul_vec (diagonal v) w x = v x * w x := diagonal_dot_product v w x lemma vec_mul_diagonal [decidable_eq m] (v w : m → α) (x : m) : vec_mul v (diagonal w) x = v x * w x := dot_product_diagonal' v w x @[simp] lemma mul_vec_one [decidable_eq m] (v : m → α) : mul_vec 1 v = v := by { ext, rw [←diagonal_one, mul_vec_diagonal, one_mul] } @[simp] lemma vec_mul_one [decidable_eq m] (v : m → α) : vec_mul v 1 = v := by { ext, rw [←diagonal_one, vec_mul_diagonal, mul_one] } @[simp] lemma mul_vec_zero (A : matrix m n α) : mul_vec A 0 = 0 := by { ext, simp [mul_vec] } @[simp] lemma vec_mul_zero (A : matrix m n α) : vec_mul 0 A = 0 := by { ext, simp [vec_mul] } @[simp] lemma vec_mul_vec_mul (v : m → α) (M : matrix m n α) (N : matrix n o α) : vec_mul (vec_mul v M) N = vec_mul v (M ⬝ N) := by { ext, apply dot_product_assoc } @[simp] lemma mul_vec_mul_vec (v : o → α) (M : matrix m n α) (N : matrix n o α) : mul_vec M (mul_vec N v) = mul_vec (M ⬝ N) v := by { ext, symmetry, apply dot_product_assoc } lemma vec_mul_vec_eq (w : m → α) (v : n → α) : vec_mul_vec w v = (col w) ⬝ (row v) := by { ext i j, simp [vec_mul_vec, mul_apply], refl } variables [decidable_eq m] [decidable_eq n] /-- `std_basis_matrix i j a` is the matrix with `a` in the `i`-th row, `j`-th column, and zeroes elsewhere. -/ def std_basis_matrix (i : m) (j : n) (a : α) : matrix m n α := (λ i' j', if i' = i ∧ j' = j then a else 0) @[simp] lemma smul_std_basis_matrix (i : m) (j : n) (a b : α) : b • std_basis_matrix i j a = std_basis_matrix i j (b • a) := by { unfold std_basis_matrix, ext, simp } @[simp] lemma std_basis_matrix_zero (i : m) (j : n) : std_basis_matrix i j (0 : α) = 0 := by { unfold std_basis_matrix, ext, simp } lemma std_basis_matrix_add (i : m) (j : n) (a b : α) : std_basis_matrix i j (a + b) = std_basis_matrix i j a + std_basis_matrix i j b := begin unfold std_basis_matrix, ext, split_ifs with h; simp [h], end lemma matrix_eq_sum_std_basis (x : matrix n m α) : x = ∑ (i : n) (j : m), std_basis_matrix i j (x i j) := begin ext, iterate 2 {rw finset.sum_apply}, rw ← finset.sum_subset, swap 4, exact {i}, { norm_num [std_basis_matrix] }, { simp }, intros y _ hyi, norm_num, convert finset.sum_const_zero, ext, norm_num [std_basis_matrix], rw if_neg, contrapose! hyi, simp [hyi] end -- TODO: tie this up with the `basis` machinery of linear algebra -- this is not completely trivial because we are indexing by two types, instead of one -- TODO: add `std_basis_vec` lemma std_basis_eq_basis_mul_basis (i : m) (j : n) : std_basis_matrix i j 1 = vec_mul_vec (λ i', ite (i = i') 1 0) (λ j', ite (j = j') 1 0) := begin ext, norm_num [std_basis_matrix, vec_mul_vec], split_ifs; tauto, end @[elab_as_eliminator] protected lemma induction_on' {X : Type*} [semiring X] {M : matrix n n X → Prop} (m : matrix n n X) (h_zero : M 0) (h_add : ∀p q, M p → M q → M (p + q)) (h_std_basis : ∀ i j x, M (std_basis_matrix i j x)) : M m := begin rw [matrix_eq_sum_std_basis m, ← finset.sum_product'], apply finset.sum_induction _ _ h_add h_zero, { intros, apply h_std_basis, } end @[elab_as_eliminator] protected lemma induction_on [nonempty n] {X : Type*} [semiring X] {M : matrix n n X → Prop} (m : matrix n n X) (h_add : ∀p q, M p → M q → M (p + q)) (h_std_basis : ∀ i j x, M (std_basis_matrix i j x)) : M m := matrix.induction_on' m begin have i : n := classical.choice (by assumption), simpa using h_std_basis i i 0, end h_add h_std_basis end semiring section ring variables [ring α] lemma neg_vec_mul (v : m → α) (A : matrix m n α) : vec_mul (-v) A = - vec_mul v A := by { ext, apply neg_dot_product } lemma vec_mul_neg (v : m → α) (A : matrix m n α) : vec_mul v (-A) = - vec_mul v A := by { ext, apply dot_product_neg } lemma neg_mul_vec (v : n → α) (A : matrix m n α) : mul_vec (-A) v = - mul_vec A v := by { ext, apply neg_dot_product } lemma mul_vec_neg (v : n → α) (A : matrix m n α) : mul_vec A (-v) = - mul_vec A v := by { ext, apply dot_product_neg } lemma smul_mul_vec_assoc (A : matrix n n α) (b : n → α) (a : α) : (a • A).mul_vec b = a • (A.mul_vec b) := begin ext i, change dot_product ((a • A) i) b = _, simp only [mul_vec, smul_eq_mul, pi.smul_apply, smul_dot_product], end end ring section transpose open_locale matrix /-- Tell `simp` what the entries are in a transposed matrix. Compare with `mul_apply`, `diagonal_apply_eq`, etc. -/ @[simp] lemma transpose_apply (M : matrix m n α) (i j) : M.transpose j i = M i j := rfl @[simp] lemma transpose_transpose (M : matrix m n α) : Mᵀᵀ = M := by ext; refl @[simp] lemma transpose_zero [has_zero α] : (0 : matrix m n α)ᵀ = 0 := by ext i j; refl @[simp] lemma transpose_one [decidable_eq n] [has_zero α] [has_one α] : (1 : matrix n n α)ᵀ = 1 := begin ext i j, unfold has_one.one transpose, by_cases i = j, { simp only [h, diagonal_apply_eq] }, { simp only [diagonal_apply_ne h, diagonal_apply_ne (λ p, h (symm p))] } end @[simp] lemma transpose_add [has_add α] (M : matrix m n α) (N : matrix m n α) : (M + N)ᵀ = Mᵀ + Nᵀ := by { ext i j, simp } @[simp] lemma transpose_sub [add_group α] (M : matrix m n α) (N : matrix m n α) : (M - N)ᵀ = Mᵀ - Nᵀ := by { ext i j, simp } @[simp] lemma transpose_mul [comm_semiring α] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᵀ = Nᵀ ⬝ Mᵀ := begin ext i j, apply dot_product_comm end @[simp] lemma transpose_smul [semiring α] (c : α) (M : matrix m n α) : (c • M)ᵀ = c • Mᵀ := by { ext i j, refl } @[simp] lemma transpose_neg [has_neg α] (M : matrix m n α) : (- M)ᵀ = - Mᵀ := by ext i j; refl lemma transpose_map {β : Type w} {f : α → β} {M : matrix m n α} : Mᵀ.map f = (M.map f)ᵀ := by { ext, refl } end transpose /-- `M.minor row col` is the matrix obtained by reindexing the rows and the lines of `M`, such that `M.minor row col i j = M (row i) (col j)`. Note that the total number of row/colums doesn't have to be preserved. -/ def minor (A : matrix m n α) (row : l → m) (col : o → n) : matrix l o α := λ i j, A (row i) (col j) /-- The left `n × l` part of a `n × (l+r)` matrix. -/ @[reducible] def sub_left {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin l) α := minor A id (fin.cast_add r) /-- The right `n × r` part of a `n × (l+r)` matrix. -/ @[reducible] def sub_right {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin r) α := minor A id (fin.nat_add l) /-- The top `u × n` part of a `(u+d) × n` matrix. -/ @[reducible] def sub_up {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin u) (fin n) α := minor A (fin.cast_add d) id /-- The bottom `d × n` part of a `(u+d) × n` matrix. -/ @[reducible] def sub_down {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin d) (fin n) α := minor A (fin.nat_add u) id /-- The top-right `u × r` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_up_right {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin r) α := sub_up (sub_right A) /-- The bottom-right `d × r` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_down_right {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin r) α := sub_down (sub_right A) /-- The top-left `u × l` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_up_left {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin (l)) α := sub_up (sub_left A) /-- The bottom-left `d × l` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_down_left {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin (l)) α := sub_down (sub_left A) section row_col /-! ### `row_col` section Simplification lemmas for `matrix.row` and `matrix.col`. -/ open_locale matrix @[simp] lemma col_add [semiring α] (v w : m → α) : col (v + w) = col v + col w := by { ext, refl } @[simp] lemma col_smul [semiring α] (x : α) (v : m → α) : col (x • v) = x • col v := by { ext, refl } @[simp] lemma row_add [semiring α] (v w : m → α) : row (v + w) = row v + row w := by { ext, refl } @[simp] lemma row_smul [semiring α] (x : α) (v : m → α) : row (x • v) = x • row v := by { ext, refl } @[simp] lemma col_apply (v : m → α) (i j) : matrix.col v i j = v i := rfl @[simp] lemma row_apply (v : m → α) (i j) : matrix.row v i j = v j := rfl @[simp] lemma transpose_col (v : m → α) : (matrix.col v).transpose = matrix.row v := by {ext, refl} @[simp] lemma transpose_row (v : m → α) : (matrix.row v).transpose = matrix.col v := by {ext, refl} lemma row_vec_mul [semiring α] (M : matrix m n α) (v : m → α) : matrix.row (matrix.vec_mul v M) = matrix.row v ⬝ M := by {ext, refl} lemma col_vec_mul [semiring α] (M : matrix m n α) (v : m → α) : matrix.col (matrix.vec_mul v M) = (matrix.row v ⬝ M)ᵀ := by {ext, refl} lemma col_mul_vec [semiring α] (M : matrix m n α) (v : n → α) : matrix.col (matrix.mul_vec M v) = M ⬝ matrix.col v := by {ext, refl} lemma row_mul_vec [semiring α] (M : matrix m n α) (v : n → α) : matrix.row (matrix.mul_vec M v) = (M ⬝ matrix.col v)ᵀ := by {ext, refl} end row_col section update /-- Update, i.e. replace the `i`th row of matrix `A` with the values in `b`. -/ def update_row [decidable_eq n] (M : matrix n m α) (i : n) (b : m → α) : matrix n m α := function.update M i b /-- Update, i.e. replace the `j`th column of matrix `A` with the values in `b`. -/ def update_column [decidable_eq m] (M : matrix n m α) (j : m) (b : n → α) : matrix n m α := λ i, function.update (M i) j (b i) variables {M : matrix n m α} {i : n} {j : m} {b : m → α} {c : n → α} @[simp] lemma update_row_self [decidable_eq n] : update_row M i b i = b := function.update_same i b M @[simp] lemma update_column_self [decidable_eq m] : update_column M j c i j = c i := function.update_same j (c i) (M i) @[simp] lemma update_row_ne [decidable_eq n] {i' : n} (i_ne : i' ≠ i) : update_row M i b i' = M i' := function.update_noteq i_ne b M @[simp] lemma update_column_ne [decidable_eq m] {j' : m} (j_ne : j' ≠ j) : update_column M j c i j' = M i j' := function.update_noteq j_ne (c i) (M i) lemma update_row_apply [decidable_eq n] {i' : n} : update_row M i b i' j = if i' = i then b j else M i' j := begin by_cases i' = i, { rw [h, update_row_self, if_pos rfl] }, { rwa [update_row_ne h, if_neg h] } end lemma update_column_apply [decidable_eq m] {j' : m} : update_column M j c i j' = if j' = j then c i else M i j' := begin by_cases j' = j, { rw [h, update_column_self, if_pos rfl] }, { rwa [update_column_ne h, if_neg h] } end lemma update_row_transpose [decidable_eq m] : update_row Mᵀ j c = (update_column M j c)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end lemma update_column_transpose [decidable_eq n] : update_column Mᵀ i b = (update_row M i b)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end end update section block_matrices /-- We can form a single large matrix by flattening smaller 'block' matrices of compatible dimensions. -/ def from_blocks (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : matrix (n ⊕ o) (l ⊕ m) α := sum.elim (λ i, sum.elim (A i) (B i)) (λ i, sum.elim (C i) (D i)) @[simp] lemma from_blocks_apply₁₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : n) (j : l) : from_blocks A B C D (sum.inl i) (sum.inl j) = A i j := rfl @[simp] lemma from_blocks_apply₁₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : n) (j : m) : from_blocks A B C D (sum.inl i) (sum.inr j) = B i j := rfl @[simp] lemma from_blocks_apply₂₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : o) (j : l) : from_blocks A B C D (sum.inr i) (sum.inl j) = C i j := rfl @[simp] lemma from_blocks_apply₂₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (i : o) (j : m) : from_blocks A B C D (sum.inr i) (sum.inr j) = D i j := rfl /-- Given a matrix whose row and column indexes are sum types, we can extract the correspnding "top left" submatrix. -/ def to_blocks₁₁ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix n l α := λ i j, M (sum.inl i) (sum.inl j) /-- Given a matrix whose row and column indexes are sum types, we can extract the correspnding "top right" submatrix. -/ def to_blocks₁₂ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix n m α := λ i j, M (sum.inl i) (sum.inr j) /-- Given a matrix whose row and column indexes are sum types, we can extract the correspnding "bottom left" submatrix. -/ def to_blocks₂₁ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix o l α := λ i j, M (sum.inr i) (sum.inl j) /-- Given a matrix whose row and column indexes are sum types, we can extract the correspnding "bottom right" submatrix. -/ def to_blocks₂₂ (M : matrix (n ⊕ o) (l ⊕ m) α) : matrix o m α := λ i j, M (sum.inr i) (sum.inr j) lemma from_blocks_to_blocks (M : matrix (n ⊕ o) (l ⊕ m) α) : from_blocks M.to_blocks₁₁ M.to_blocks₁₂ M.to_blocks₂₁ M.to_blocks₂₂ = M := begin ext i j, rcases i; rcases j; refl, end @[simp] lemma to_blocks_from_blocks₁₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₁₁ = A := rfl @[simp] lemma to_blocks_from_blocks₁₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₁₂ = B := rfl @[simp] lemma to_blocks_from_blocks₂₁ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₂₁ = C := rfl @[simp] lemma to_blocks_from_blocks₂₂ (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D).to_blocks₂₂ = D := rfl lemma from_blocks_transpose (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : (from_blocks A B C D)ᵀ = from_blocks Aᵀ Cᵀ Bᵀ Dᵀ := begin ext i j, rcases i; rcases j; simp [from_blocks], end variables [semiring α] lemma from_blocks_smul (x : α) (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) : x • (from_blocks A B C D) = from_blocks (x • A) (x • B) (x • C) (x • D) := begin ext i j, rcases i; rcases j; simp [from_blocks], end lemma from_blocks_add (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (A' : matrix n l α) (B' : matrix n m α) (C' : matrix o l α) (D' : matrix o m α) : (from_blocks A B C D) + (from_blocks A' B' C' D') = from_blocks (A + A') (B + B') (C + C') (D + D') := begin ext i j, rcases i; rcases j; refl, end lemma from_blocks_multiply {p q : Type*} [fintype p] [fintype q] (A : matrix n l α) (B : matrix n m α) (C : matrix o l α) (D : matrix o m α) (A' : matrix l p α) (B' : matrix l q α) (C' : matrix m p α) (D' : matrix m q α) : (from_blocks A B C D) ⬝ (from_blocks A' B' C' D') = from_blocks (A ⬝ A' + B ⬝ C') (A ⬝ B' + B ⬝ D') (C ⬝ A' + D ⬝ C') (C ⬝ B' + D ⬝ D') := begin ext i j, rcases i; rcases j; simp only [from_blocks, mul_apply, fintype.sum_sum_type, sum.elim_inl, sum.elim_inr, pi.add_apply], end variables [decidable_eq l] [decidable_eq m] @[simp] lemma from_blocks_diagonal (d₁ : l → α) (d₂ : m → α) : from_blocks (diagonal d₁) 0 0 (diagonal d₂) = diagonal (sum.elim d₁ d₂) := begin ext i j, rcases i; rcases j; simp [diagonal], end @[simp] lemma from_blocks_one : from_blocks (1 : matrix l l α) 0 0 (1 : matrix m m α) = 1 := by { ext i j, rcases i; rcases j; simp [one_apply] } end block_matrices section block_diagonal variables (M N : o → matrix m n α) [decidable_eq o] section has_zero variables [has_zero α] /-- `matrix.block_diagonal M` turns `M : o → matrix m n α'` into a `m × o`-by`n × o` block matrix which has the entries of `M` along the diagonal and zero elsewhere. -/ def block_diagonal : matrix (m × o) (n × o) α | ⟨i, k⟩ ⟨j, k'⟩ := if k = k' then M k i j else 0 lemma block_diagonal_apply (ik jk) : block_diagonal M ik jk = if ik.2 = jk.2 then M ik.2 ik.1 jk.1 else 0 := by { cases ik, cases jk, refl } @[simp] lemma block_diagonal_apply_eq (i j k) : block_diagonal M (i, k) (j, k) = M k i j := if_pos rfl lemma block_diagonal_apply_ne (i j) {k k'} (h : k ≠ k') : block_diagonal M (i, k) (j, k') = 0 := if_neg h @[simp] lemma block_diagonal_transpose : (block_diagonal M)ᵀ = (block_diagonal (λ k, (M k)ᵀ)) := begin ext, simp only [transpose_apply, block_diagonal_apply, eq_comm], split_ifs with h, { rw h }, { refl } end @[simp] lemma block_diagonal_zero : block_diagonal (0 : o → matrix m n α) = 0 := by { ext, simp [block_diagonal_apply] } @[simp] lemma block_diagonal_diagonal [decidable_eq m] (d : o → m → α) : (block_diagonal (λ k, diagonal (d k))) = diagonal (λ ik, d ik.2 ik.1) := begin ext ⟨i, k⟩ ⟨j, k'⟩, simp only [block_diagonal_apply, diagonal], split_ifs; finish end @[simp] lemma block_diagonal_one [decidable_eq m] [has_one α] : (block_diagonal (1 : o → matrix m m α)) = 1 := show (block_diagonal (λ (_ : o), diagonal (λ (_ : m), (1 : α)))) = diagonal (λ _, 1), by rw [block_diagonal_diagonal] end has_zero @[simp] lemma block_diagonal_add [add_monoid α] : block_diagonal (M + N) = block_diagonal M + block_diagonal N := begin ext, simp only [block_diagonal_apply, add_apply], split_ifs; simp end @[simp] lemma block_diagonal_neg [add_group α] : block_diagonal (-M) = - block_diagonal M := begin ext, simp only [block_diagonal_apply, neg_apply], split_ifs; simp end @[simp] lemma block_diagonal_sub [add_group α] : block_diagonal (M - N) = block_diagonal M - block_diagonal N := by simp [sub_eq_add_neg] @[simp] lemma block_diagonal_mul {p : Type*} [fintype p] [semiring α] (N : o → matrix n p α) : block_diagonal (λ k, M k ⬝ N k) = block_diagonal M ⬝ block_diagonal N := begin ext ⟨i, k⟩ ⟨j, k'⟩, simp only [block_diagonal_apply, mul_apply, ← finset.univ_product_univ, finset.sum_product], split_ifs with h; simp [h] end @[simp] lemma block_diagonal_smul {R : Type*} [semiring R] [add_comm_monoid α] [semimodule R α] (x : R) : block_diagonal (x • M) = x • block_diagonal M := by { ext, simp only [block_diagonal_apply, pi.smul_apply, smul_apply], split_ifs; simp } end block_diagonal end matrix namespace ring_hom variables {β : Type*} [semiring α] [semiring β] lemma map_matrix_mul (M : matrix m n α) (N : matrix n o α) (i : m) (j : o) (f : α →+* β) : f (matrix.mul M N i j) = matrix.mul (λ i j, f (M i j)) (λ i j, f (N i j)) i j := by simp [matrix.mul_apply, ring_hom.map_sum] end ring_hom
dbb2e9a238f3725339cc5d5235cb51d562a7fe28
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/unknownTactic.lean
01ad345ecc2255629cb93e33e80a56467314227a
[ "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
271
lean
theorem ex1 (x : Nat) : x = x → x = x := by intro aexact (rfl) #print "---" theorem ex2 (x : Nat) : x = x → x = x := have : x = x := by foo fun h => h #print "---" theorem ex3 (x : Nat) : x = x → x = x := have : x = x := by foo (aaa bbb) fun h => h
b55bd6b083fbf109f1c806d6248c9c0cbf336496
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/calc.lean
337d1e0b62612b8f16c41dda130b049d0123784c
[ "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
356
lean
namespace foo constant le : nat → nat → Prop axiom le_trans {a b c : nat} : le a b → le b c → le a c attribute [trans] le_trans infix `<<`:50 := le end foo namespace foo theorem T {a b c d : nat} : a << b → b << c → c << d → a << d := assume H1 H2 H3, calc a << b : H1 ... << c : H2 ... << d : H3 end foo
848c1513307bef7f3bd62ff88be1311043dde7fd
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/group_with_zero/units/lemmas.lean
68ced6b72bfc91a6f58f982c4eaf36c4319ba368
[ "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
7,460
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.group_with_zero.commute import algebra.hom.units import group_theory.group_action.units /-! # Further lemmas about units in a `monoid_with_zero` or a `group_with_zero`. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ variables {α M₀ G₀ M₀' G₀' F F' : Type*} variables [monoid_with_zero M₀] section group_with_zero variables [group_with_zero G₀] {a b c : G₀} @[simp] lemma div_self (h : a ≠ 0) : a / a = 1 := h.is_unit.div_self lemma eq_mul_inv_iff_mul_eq₀ (hc : c ≠ 0) : a = b * c⁻¹ ↔ a * c = b := hc.is_unit.eq_mul_inv_iff_mul_eq lemma eq_inv_mul_iff_mul_eq₀ (hb : b ≠ 0) : a = b⁻¹ * c ↔ b * a = c := hb.is_unit.eq_inv_mul_iff_mul_eq lemma inv_mul_eq_iff_eq_mul₀ (ha : a ≠ 0) : a⁻¹ * b = c ↔ b = a * c := ha.is_unit.inv_mul_eq_iff_eq_mul lemma mul_inv_eq_iff_eq_mul₀ (hb : b ≠ 0) : a * b⁻¹ = c ↔ a = c * b := hb.is_unit.mul_inv_eq_iff_eq_mul lemma mul_inv_eq_one₀ (hb : b ≠ 0) : a * b⁻¹ = 1 ↔ a = b := hb.is_unit.mul_inv_eq_one lemma inv_mul_eq_one₀ (ha : a ≠ 0) : a⁻¹ * b = 1 ↔ a = b := ha.is_unit.inv_mul_eq_one lemma mul_eq_one_iff_eq_inv₀ (hb : b ≠ 0) : a * b = 1 ↔ a = b⁻¹ := hb.is_unit.mul_eq_one_iff_eq_inv lemma mul_eq_one_iff_inv_eq₀ (ha : a ≠ 0) : a * b = 1 ↔ a⁻¹ = b := ha.is_unit.mul_eq_one_iff_inv_eq @[simp] lemma div_mul_cancel (a : G₀) (h : b ≠ 0) : a / b * b = a := h.is_unit.div_mul_cancel _ @[simp] lemma mul_div_cancel (a : G₀) (h : b ≠ 0) : a * b / b = a := h.is_unit.mul_div_cancel _ lemma mul_one_div_cancel (h : a ≠ 0) : a * (1 / a) = 1 := h.is_unit.mul_one_div_cancel lemma one_div_mul_cancel (h : a ≠ 0) : (1 / a) * a = 1 := h.is_unit.one_div_mul_cancel lemma div_left_inj' (hc : c ≠ 0) : a / c = b / c ↔ a = b := hc.is_unit.div_left_inj @[field_simps] lemma div_eq_iff (hb : b ≠ 0) : a / b = c ↔ a = c * b := hb.is_unit.div_eq_iff @[field_simps] lemma eq_div_iff (hb : b ≠ 0) : c = a / b ↔ c * b = a := hb.is_unit.eq_div_iff lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a := hb.is_unit.div_eq_iff.trans eq_comm lemma eq_div_iff_mul_eq (hc : c ≠ 0) : a = b / c ↔ a * c = b := hc.is_unit.eq_div_iff lemma div_eq_of_eq_mul (hb : b ≠ 0) : a = c * b → a / b = c := hb.is_unit.div_eq_of_eq_mul lemma eq_div_of_mul_eq (hc : c ≠ 0) : a * c = b → a = b / c := hc.is_unit.eq_div_of_mul_eq lemma div_eq_one_iff_eq (hb : b ≠ 0) : a / b = 1 ↔ a = b := hb.is_unit.div_eq_one_iff_eq lemma div_mul_left (hb : b ≠ 0) : b / (a * b) = 1 / a := hb.is_unit.div_mul_left lemma mul_div_mul_right (a b : G₀) (hc : c ≠ 0) : (a * c) / (b * c) = a / b := hc.is_unit.mul_div_mul_right _ _ lemma mul_mul_div (a : G₀) (hb : b ≠ 0) : a = a * b * (1 / b) := (hb.is_unit.mul_mul_div _).symm lemma div_div_div_cancel_right (a : G₀) (hc : c ≠ 0) : (a / c) / (b / c) = a / b := by rw [div_div_eq_mul_div, div_mul_cancel _ hc] lemma div_mul_div_cancel (a : G₀) (hc : c ≠ 0) : (a / c) * (c / b) = a / b := by rw [← mul_div_assoc, div_mul_cancel _ hc] lemma div_mul_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a / b * b = a := classical.by_cases (λ hb : b = 0, by simp [*]) (div_mul_cancel a) lemma mul_div_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a * b / b = a := classical.by_cases (λ hb : b = 0, by simp [*]) (mul_div_cancel a) @[simp] theorem divp_mk0 (a : G₀) {b : G₀} (hb : b ≠ 0) : a /ₚ units.mk0 b hb = a / b := divp_eq_div _ _ end group_with_zero section comm_group_with_zero -- comm variables [comm_group_with_zero G₀] {a b c d : G₀} lemma div_mul_right (b : G₀) (ha : a ≠ 0) : a / (a * b) = 1 / b := ha.is_unit.div_mul_right _ lemma mul_div_cancel_left_of_imp {a b : G₀} (h : a = 0 → b = 0) : a * b / a = b := by rw [mul_comm, mul_div_cancel_of_imp h] lemma mul_div_cancel_left (b : G₀) (ha : a ≠ 0) : a * b / a = b := ha.is_unit.mul_div_cancel_left _ lemma mul_div_cancel_of_imp' {a b : G₀} (h : b = 0 → a = 0) : b * (a / b) = a := by rw [mul_comm, div_mul_cancel_of_imp h] lemma mul_div_cancel' (a : G₀) (hb : b ≠ 0) : b * (a / b) = a := hb.is_unit.mul_div_cancel' _ lemma mul_div_mul_left (a b : G₀) (hc : c ≠ 0) : (c * a) / (c * b) = a / b := hc.is_unit.mul_div_mul_left _ _ lemma mul_eq_mul_of_div_eq_div (a : G₀) {b : G₀} (c : G₀) {d : G₀} (hb : b ≠ 0) (hd : d ≠ 0) (h : a / b = c / d) : a * d = c * b := by rw [←mul_one a, ←div_self hb, ←mul_comm_div, h, div_mul_eq_mul_div, div_mul_cancel _ hd] @[field_simps] lemma div_eq_div_iff (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := hb.is_unit.div_eq_div_iff hd.is_unit lemma div_div_cancel' (ha : a ≠ 0) : a / (a / b) = b := ha.is_unit.div_div_cancel lemma div_helper (b : G₀) (h : a ≠ 0) : 1 / (a * b) * a = 1 / b := by rw [div_mul_eq_mul_div, one_mul, div_mul_right _ h] end comm_group_with_zero section monoid_with_zero variables [group_with_zero G₀] [nontrivial M₀] [monoid_with_zero M₀'] [monoid_with_zero_hom_class F G₀ M₀] [monoid_with_zero_hom_class F' G₀ M₀'] (f : F) {a : G₀} include M₀ lemma map_ne_zero : f a ≠ 0 ↔ a ≠ 0 := ⟨λ hfa ha, hfa $ ha.symm ▸ map_zero f, λ ha, ((is_unit.mk0 a ha).map f).ne_zero⟩ @[simp] lemma map_eq_zero : f a = 0 ↔ a = 0 := not_iff_not.1 (map_ne_zero f) omit M₀ include M₀' lemma eq_on_inv₀ (f g : F') (h : f a = g a) : f a⁻¹ = g a⁻¹ := begin rcases eq_or_ne a 0 with rfl|ha, { rw [inv_zero, map_zero, map_zero] }, { exact (is_unit.mk0 a ha).eq_on_inv f g h } end end monoid_with_zero section group_with_zero variables [group_with_zero G₀] [group_with_zero G₀'] [monoid_with_zero_hom_class F G₀ G₀'] (f : F) (a b : G₀) include G₀' /-- A monoid homomorphism between groups with zeros sending `0` to `0` sends `a⁻¹` to `(f a)⁻¹`. -/ @[simp] lemma map_inv₀ : f a⁻¹ = (f a)⁻¹ := begin by_cases h : a = 0, by simp [h], apply eq_inv_of_mul_eq_one_left, rw [← map_mul, inv_mul_cancel h, map_one] end @[simp] lemma map_div₀ : f (a / b) = f a / f b := map_div' f (map_inv₀ f) a b end group_with_zero /-- We define the inverse as a `monoid_with_zero_hom` by extending the inverse map by zero on non-units. -/ noncomputable def monoid_with_zero.inverse {M : Type*} [comm_monoid_with_zero M] : M →*₀ M := { to_fun := ring.inverse, map_zero' := ring.inverse_zero _, map_one' := ring.inverse_one _, map_mul' := λ x y, (ring.mul_inverse_rev x y).trans (mul_comm _ _) } @[simp] lemma monoid_with_zero.coe_inverse {M : Type*} [comm_monoid_with_zero M] : (monoid_with_zero.inverse : M → M) = ring.inverse := rfl @[simp] lemma monoid_with_zero.inverse_apply {M : Type*} [comm_monoid_with_zero M] (a : M) : monoid_with_zero.inverse a = ring.inverse a := rfl /-- Inversion on a commutative group with zero, considered as a monoid with zero homomorphism. -/ def inv_monoid_with_zero_hom {G₀ : Type*} [comm_group_with_zero G₀] : G₀ →*₀ G₀ := { map_zero' := inv_zero, ..inv_monoid_hom } namespace units variables [group_with_zero G₀] variables {a b : G₀} @[simp] lemma smul_mk0 {α : Type*} [has_smul G₀ α] {g : G₀} (hg : g ≠ 0) (a : α) : (mk0 g hg) • a = g • a := rfl end units
faa43b477e1724440388404b18ac8dff9641ebb3
bb31430994044506fa42fd667e2d556327e18dfe
/src/linear_algebra/projection.lean
0607c59f7ad110d90ec019a840c336223ccc4a0b
[ "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
16,752
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import linear_algebra.quotient import linear_algebra.prod /-! # Projection to a subspace In this file we define * `linear_proj_of_is_compl (p q : submodule R E) (h : is_compl p q)`: the projection of a module `E` to a submodule `p` along its complement `q`; it is the unique linear map `f : E → p` such that `f x = x` for `x ∈ p` and `f x = 0` for `x ∈ q`. * `is_compl_equiv_proj p`: equivalence between submodules `q` such that `is_compl p q` and projections `f : E → p`, `∀ x ∈ p, f x = x`. We also provide some lemmas justifying correctness of our definitions. ## Tags projection, complement subspace -/ section ring variables {R : Type*} [ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {G : Type*} [add_comm_group G] [module R G] (p q : submodule R E) variables {S : Type*} [semiring S] {M : Type*} [add_comm_monoid M] [module S M] (m : submodule S M) noncomputable theory namespace linear_map variable {p} open submodule lemma ker_id_sub_eq_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : ker (id - p.subtype.comp f) = p := begin ext x, simp only [comp_apply, mem_ker, subtype_apply, sub_apply, id_apply, sub_eq_zero], exact ⟨λ h, h.symm ▸ submodule.coe_mem _, λ hx, by erw [hf ⟨x, hx⟩, subtype.coe_mk]⟩ end lemma range_eq_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : range f = ⊤ := range_eq_top.2 $ λ x, ⟨x, hf x⟩ lemma is_compl_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : is_compl p f.ker := begin split, { rw disjoint_iff_inf_le, rintros x ⟨hpx, hfx⟩, erw [set_like.mem_coe, mem_ker, hf ⟨x, hpx⟩, mk_eq_zero] at hfx, simp only [hfx, set_like.mem_coe, zero_mem] }, { rw codisjoint_iff_le_sup, intros x hx, rw [mem_sup'], refine ⟨f x, ⟨x - f x, _⟩, add_sub_cancel'_right _ _⟩, rw [mem_ker, linear_map.map_sub, hf, sub_self] } end end linear_map namespace submodule open linear_map /-- If `q` is a complement of `p`, then `M/p ≃ q`. -/ def quotient_equiv_of_is_compl (h : is_compl p q) : (E ⧸ p) ≃ₗ[R] q := linear_equiv.symm $ linear_equiv.of_bijective (p.mkq.comp q.subtype) ⟨by rw [← ker_eq_bot, ker_comp, ker_mkq, disjoint_iff_comap_eq_bot.1 h.symm.disjoint], by rw [← range_eq_top, range_comp, range_subtype, map_mkq_eq_top, h.sup_eq_top]⟩ @[simp] lemma quotient_equiv_of_is_compl_symm_apply (h : is_compl p q) (x : q) : (quotient_equiv_of_is_compl p q h).symm x = quotient.mk x := rfl @[simp] lemma quotient_equiv_of_is_compl_apply_mk_coe (h : is_compl p q) (x : q) : quotient_equiv_of_is_compl p q h (quotient.mk x) = x := (quotient_equiv_of_is_compl p q h).apply_symm_apply x @[simp] lemma mk_quotient_equiv_of_is_compl_apply (h : is_compl p q) (x : E ⧸ p) : (quotient.mk (quotient_equiv_of_is_compl p q h x) : E ⧸ p) = x := (quotient_equiv_of_is_compl p q h).symm_apply_apply x /-- If `q` is a complement of `p`, then `p × q` is isomorphic to `E`. It is the unique linear map `f : E → p` such that `f x = x` for `x ∈ p` and `f x = 0` for `x ∈ q`. -/ def prod_equiv_of_is_compl (h : is_compl p q) : (p × q) ≃ₗ[R] E := begin apply linear_equiv.of_bijective (p.subtype.coprod q.subtype), split, { rw [← ker_eq_bot, ker_coprod_of_disjoint_range, ker_subtype, ker_subtype, prod_bot], rw [range_subtype, range_subtype], exact h.1 }, { rw [← range_eq_top, ← sup_eq_range, h.sup_eq_top] } end @[simp] lemma coe_prod_equiv_of_is_compl (h : is_compl p q) : (prod_equiv_of_is_compl p q h : (p × q) →ₗ[R] E) = p.subtype.coprod q.subtype := rfl @[simp] lemma coe_prod_equiv_of_is_compl' (h : is_compl p q) (x : p × q) : prod_equiv_of_is_compl p q h x = x.1 + x.2 := rfl @[simp] lemma prod_equiv_of_is_compl_symm_apply_left (h : is_compl p q) (x : p) : (prod_equiv_of_is_compl p q h).symm x = (x, 0) := (prod_equiv_of_is_compl p q h).symm_apply_eq.2 $ by simp @[simp] lemma prod_equiv_of_is_compl_symm_apply_right (h : is_compl p q) (x : q) : (prod_equiv_of_is_compl p q h).symm x = (0, x) := (prod_equiv_of_is_compl p q h).symm_apply_eq.2 $ by simp @[simp] lemma prod_equiv_of_is_compl_symm_apply_fst_eq_zero (h : is_compl p q) {x : E} : ((prod_equiv_of_is_compl p q h).symm x).1 = 0 ↔ x ∈ q := begin conv_rhs { rw [← (prod_equiv_of_is_compl p q h).apply_symm_apply x] }, rw [coe_prod_equiv_of_is_compl', submodule.add_mem_iff_left _ (submodule.coe_mem _), mem_right_iff_eq_zero_of_disjoint h.disjoint] end @[simp] lemma prod_equiv_of_is_compl_symm_apply_snd_eq_zero (h : is_compl p q) {x : E} : ((prod_equiv_of_is_compl p q h).symm x).2 = 0 ↔ x ∈ p := begin conv_rhs { rw [← (prod_equiv_of_is_compl p q h).apply_symm_apply x] }, rw [coe_prod_equiv_of_is_compl', submodule.add_mem_iff_right _ (submodule.coe_mem _), mem_left_iff_eq_zero_of_disjoint h.disjoint] end @[simp] lemma prod_comm_trans_prod_equiv_of_is_compl (h : is_compl p q) : linear_equiv.prod_comm R q p ≪≫ₗ prod_equiv_of_is_compl p q h = prod_equiv_of_is_compl q p h.symm := linear_equiv.ext $ λ _, add_comm _ _ /-- Projection to a submodule along its complement. -/ def linear_proj_of_is_compl (h : is_compl p q) : E →ₗ[R] p := (linear_map.fst R p q) ∘ₗ ↑(prod_equiv_of_is_compl p q h).symm variables {p q} @[simp] lemma linear_proj_of_is_compl_apply_left (h : is_compl p q) (x : p) : linear_proj_of_is_compl p q h x = x := by simp [linear_proj_of_is_compl] @[simp] lemma linear_proj_of_is_compl_range (h : is_compl p q) : (linear_proj_of_is_compl p q h).range = ⊤ := range_eq_of_proj (linear_proj_of_is_compl_apply_left h) @[simp] lemma linear_proj_of_is_compl_apply_eq_zero_iff (h : is_compl p q) {x : E} : linear_proj_of_is_compl p q h x = 0 ↔ x ∈ q:= by simp [linear_proj_of_is_compl] lemma linear_proj_of_is_compl_apply_right' (h : is_compl p q) (x : E) (hx : x ∈ q) : linear_proj_of_is_compl p q h x = 0 := (linear_proj_of_is_compl_apply_eq_zero_iff h).2 hx @[simp] lemma linear_proj_of_is_compl_apply_right (h : is_compl p q) (x : q) : linear_proj_of_is_compl p q h x = 0 := linear_proj_of_is_compl_apply_right' h x x.2 @[simp] lemma linear_proj_of_is_compl_ker (h : is_compl p q) : (linear_proj_of_is_compl p q h).ker = q := ext $ λ x, mem_ker.trans (linear_proj_of_is_compl_apply_eq_zero_iff h) lemma linear_proj_of_is_compl_comp_subtype (h : is_compl p q) : (linear_proj_of_is_compl p q h).comp p.subtype = id := linear_map.ext $ linear_proj_of_is_compl_apply_left h lemma linear_proj_of_is_compl_idempotent (h : is_compl p q) (x : E) : linear_proj_of_is_compl p q h (linear_proj_of_is_compl p q h x) = linear_proj_of_is_compl p q h x := linear_proj_of_is_compl_apply_left h _ lemma exists_unique_add_of_is_compl_prod (hc : is_compl p q) (x : E) : ∃! (u : p × q), (u.fst : E) + u.snd = x := (prod_equiv_of_is_compl _ _ hc).to_equiv.bijective.exists_unique _ lemma exists_unique_add_of_is_compl (hc : is_compl p q) (x : E) : ∃ (u : p) (v : q), ((u : E) + v = x ∧ ∀ (r : p) (s : q), (r : E) + s = x → r = u ∧ s = v) := let ⟨u, hu₁, hu₂⟩ := exists_unique_add_of_is_compl_prod hc x in ⟨u.1, u.2, hu₁, λ r s hrs, prod.eq_iff_fst_eq_snd_eq.1 (hu₂ ⟨r, s⟩ hrs)⟩ lemma linear_proj_add_linear_proj_of_is_compl_eq_self (hpq : is_compl p q) (x : E) : (p.linear_proj_of_is_compl q hpq x + q.linear_proj_of_is_compl p hpq.symm x : E) = x := begin dunfold linear_proj_of_is_compl, rw ←prod_comm_trans_prod_equiv_of_is_compl _ _ hpq, exact (prod_equiv_of_is_compl _ _ hpq).apply_symm_apply x, end end submodule namespace linear_map open submodule /-- Given linear maps `φ` and `ψ` from complement submodules, `of_is_compl` is the induced linear map over the entire module. -/ def of_is_compl {p q : submodule R E} (h : is_compl p q) (φ : p →ₗ[R] F) (ψ : q →ₗ[R] F) : E →ₗ[R] F := (linear_map.coprod φ ψ) ∘ₗ ↑(submodule.prod_equiv_of_is_compl _ _ h).symm variables {p q} @[simp] lemma of_is_compl_left_apply (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (u : p) : of_is_compl h φ ψ (u : E) = φ u := by simp [of_is_compl] @[simp] lemma of_is_compl_right_apply (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (v : q) : of_is_compl h φ ψ (v : E) = ψ v := by simp [of_is_compl] lemma of_is_compl_eq (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} {χ : E →ₗ[R] F} (hφ : ∀ u, φ u = χ u) (hψ : ∀ u, ψ u = χ u) : of_is_compl h φ ψ = χ := begin ext x, obtain ⟨_, _, rfl, _⟩ := exists_unique_add_of_is_compl h x, simp [of_is_compl, hφ, hψ] end lemma of_is_compl_eq' (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} {χ : E →ₗ[R] F} (hφ : φ = χ.comp p.subtype) (hψ : ψ = χ.comp q.subtype) : of_is_compl h φ ψ = χ := of_is_compl_eq h (λ _, hφ.symm ▸ rfl) (λ _, hψ.symm ▸ rfl) @[simp] lemma of_is_compl_zero (h : is_compl p q) : (of_is_compl h 0 0 : E →ₗ[R] F) = 0 := of_is_compl_eq _ (λ _, rfl) (λ _, rfl) @[simp] lemma of_is_compl_add (h : is_compl p q) {φ₁ φ₂ : p →ₗ[R] F} {ψ₁ ψ₂ : q →ₗ[R] F} : of_is_compl h (φ₁ + φ₂) (ψ₁ + ψ₂) = of_is_compl h φ₁ ψ₁ + of_is_compl h φ₂ ψ₂ := of_is_compl_eq _ (by simp) (by simp) @[simp] lemma of_is_compl_smul {R : Type*} [comm_ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {p q : submodule R E} (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (c : R) : of_is_compl h (c • φ) (c • ψ) = c • of_is_compl h φ ψ := of_is_compl_eq _ (by simp) (by simp) section variables {R₁ : Type*} [comm_ring R₁] [module R₁ E] [module R₁ F] /-- The linear map from `(p →ₗ[R₁] F) × (q →ₗ[R₁] F)` to `E →ₗ[R₁] F`. -/ def of_is_compl_prod {p q : submodule R₁ E} (h : is_compl p q) : ((p →ₗ[R₁] F) × (q →ₗ[R₁] F)) →ₗ[R₁] (E →ₗ[R₁] F) := { to_fun := λ φ, of_is_compl h φ.1 φ.2, map_add' := by { intros φ ψ, rw [prod.snd_add, prod.fst_add, of_is_compl_add] }, map_smul' := by { intros c φ, simp [prod.smul_snd, prod.smul_fst, of_is_compl_smul] } } @[simp] lemma of_is_compl_prod_apply {p q : submodule R₁ E} (h : is_compl p q) (φ : (p →ₗ[R₁] F) × (q →ₗ[R₁] F)) : of_is_compl_prod h φ = of_is_compl h φ.1 φ.2 := rfl /-- The natural linear equivalence between `(p →ₗ[R₁] F) × (q →ₗ[R₁] F)` and `E →ₗ[R₁] F`. -/ def of_is_compl_prod_equiv {p q : submodule R₁ E} (h : is_compl p q) : ((p →ₗ[R₁] F) × (q →ₗ[R₁] F)) ≃ₗ[R₁] (E →ₗ[R₁] F) := { inv_fun := λ φ, ⟨φ.dom_restrict p, φ.dom_restrict q⟩, left_inv := begin intros φ, ext, { exact of_is_compl_left_apply h x }, { exact of_is_compl_right_apply h x } end, right_inv := begin intro φ, ext, obtain ⟨a, b, hab, _⟩ := exists_unique_add_of_is_compl h x, rw [← hab], simp, end, .. of_is_compl_prod h } end @[simp] lemma linear_proj_of_is_compl_of_proj (f : E →ₗ[R] p) (hf : ∀ x : p, f x = x) : p.linear_proj_of_is_compl f.ker (is_compl_of_proj hf) = f := begin ext x, have : x ∈ p ⊔ f.ker, { simp only [(is_compl_of_proj hf).sup_eq_top, mem_top] }, rcases mem_sup'.1 this with ⟨x, y, rfl⟩, simp [hf] end /-- If `f : E →ₗ[R] F` and `g : E →ₗ[R] G` are two surjective linear maps and their kernels are complement of each other, then `x ↦ (f x, g x)` defines a linear equivalence `E ≃ₗ[R] F × G`. -/ def equiv_prod_of_surjective_of_is_compl (f : E →ₗ[R] F) (g : E →ₗ[R] G) (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : E ≃ₗ[R] F × G := linear_equiv.of_bijective (f.prod g) ⟨by simp [← ker_eq_bot, hfg.inf_eq_bot], by { rw [←range_eq_top], simp [range_prod_eq hfg.sup_eq_top, *] }⟩ @[simp] lemma coe_equiv_prod_of_surjective_of_is_compl {f : E →ₗ[R] F} {g : E →ₗ[R] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : (equiv_prod_of_surjective_of_is_compl f g hf hg hfg : E →ₗ[R] F × G) = f.prod g := rfl @[simp] lemma equiv_prod_of_surjective_of_is_compl_apply {f : E →ₗ[R] F} {g : E →ₗ[R] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) (x : E): equiv_prod_of_surjective_of_is_compl f g hf hg hfg x = (f x, g x) := rfl end linear_map namespace submodule open linear_map /-- Equivalence between submodules `q` such that `is_compl p q` and linear maps `f : E →ₗ[R] p` such that `∀ x : p, f x = x`. -/ def is_compl_equiv_proj : {q // is_compl p q} ≃ {f : E →ₗ[R] p // ∀ x : p, f x = x} := { to_fun := λ q, ⟨linear_proj_of_is_compl p q q.2, linear_proj_of_is_compl_apply_left q.2⟩, inv_fun := λ f, ⟨(f : E →ₗ[R] p).ker, is_compl_of_proj f.2⟩, left_inv := λ ⟨q, hq⟩, by simp only [linear_proj_of_is_compl_ker, subtype.coe_mk], right_inv := λ ⟨f, hf⟩, subtype.eq $ f.linear_proj_of_is_compl_of_proj hf } @[simp] lemma coe_is_compl_equiv_proj_apply (q : {q // is_compl p q}) : (p.is_compl_equiv_proj q : E →ₗ[R] p) = linear_proj_of_is_compl p q q.2 := rfl @[simp] lemma coe_is_compl_equiv_proj_symm_apply (f : {f : E →ₗ[R] p // ∀ x : p, f x = x}) : (p.is_compl_equiv_proj.symm f : submodule R E) = (f : E →ₗ[R] p).ker := rfl end submodule namespace linear_map open submodule /-- A linear endomorphism of a module `E` is a projection onto a submodule `p` if it sends every element of `E` to `p` and fixes every element of `p`. The definition allow more generally any `fun_like` type and not just linear maps, so that it can be used for example with `continuous_linear_map` or `matrix`. -/ structure is_proj {F : Type*} [fun_like F M (λ _, M)] (f : F) : Prop := (map_mem : ∀ x, f x ∈ m) (map_id : ∀ x ∈ m, f x = x) lemma is_proj_iff_idempotent (f : M →ₗ[S] M) : (∃ p : submodule S M, is_proj p f) ↔ f ∘ₗ f = f := begin split, { intro h, obtain ⟨p, hp⟩ := h, ext, rw comp_apply, exact hp.map_id (f x) (hp.map_mem x), }, { intro h, use f.range, split, { intro x, exact mem_range_self f x, }, { intros x hx, obtain ⟨y, hy⟩ := mem_range.1 hx, rw [←hy, ←comp_apply, h], }, }, end namespace is_proj variables {p m} /-- Restriction of the codomain of a projection of onto a subspace `p` to `p` instead of the whole space. -/ def cod_restrict {f : M →ₗ[S] M} (h : is_proj m f) : M →ₗ[S] m := f.cod_restrict m h.map_mem @[simp] lemma cod_restrict_apply {f : M →ₗ[S] M} (h : is_proj m f) (x : M) : ↑(h.cod_restrict x) = f x := f.cod_restrict_apply m x @[simp] lemma cod_restrict_apply_cod {f : M →ₗ[S] M} (h : is_proj m f) (x : m) : h.cod_restrict x = x := by {ext, rw [cod_restrict_apply], exact h.map_id x x.2} lemma cod_restrict_ker {f : M →ₗ[S] M} (h : is_proj m f) : h.cod_restrict.ker = f.ker := f.ker_cod_restrict m _ lemma is_compl {f : E →ₗ[R] E} (h : is_proj p f) : is_compl p f.ker := by { rw ←cod_restrict_ker, exact is_compl_of_proj h.cod_restrict_apply_cod, } lemma eq_conj_prod_map' {f : E →ₗ[R] E} (h : is_proj p f) : f = (p.prod_equiv_of_is_compl f.ker h.is_compl).to_linear_map ∘ₗ prod_map id 0 ∘ₗ (p.prod_equiv_of_is_compl f.ker h.is_compl).symm.to_linear_map := begin refine (linear_map.cancel_right (p.prod_equiv_of_is_compl f.ker h.is_compl).surjective).1 _, ext, { simp only [coe_comp, linear_equiv.coe_to_linear_map, coe_inl, function.comp_app, linear_equiv.of_top_apply, linear_equiv.of_injective_apply, coprod_apply, submodule.coe_subtype, coe_zero, add_zero, prod_equiv_of_is_compl_symm_apply_left, prod_map_apply, id_coe, id.def, zero_apply, coe_prod_equiv_of_is_compl', h.map_id x x.2], }, {simp only [coe_comp, linear_equiv.coe_to_linear_map, coe_inr, function.comp_app, linear_equiv.of_top_apply, linear_equiv.of_injective_apply, coprod_apply, submodule.coe_subtype, coe_zero, zero_add, map_coe_ker, prod_equiv_of_is_compl_symm_apply_right, prod_map_apply, id_coe, id.def, zero_apply, coe_prod_equiv_of_is_compl'], } end end is_proj end linear_map end ring section comm_ring namespace linear_map variables {R : Type*} [comm_ring R] {E : Type*} [add_comm_group E] [module R E] {p : submodule R E} lemma is_proj.eq_conj_prod_map {f : E →ₗ[R] E} (h : is_proj p f) : f = (p.prod_equiv_of_is_compl f.ker h.is_compl).conj (prod_map id 0) := by {rw linear_equiv.conj_apply, exact h.eq_conj_prod_map'} end linear_map end comm_ring
c37b897681d343208473c15912bfcc106a428a96
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/doNotation2.lean
de460b63ee24dbba2e1ab2cd4b1c2445d3b0424f
[ "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
365
lean
new_frontend def f (x : Nat) : IO Nat := do IO.println "hello world" let aux (y : Nat) (z : Nat) : IO Nat := do IO.println "aux started" IO.println ("y: " ++ toString y ++ ", z: " ++ toString z) pure (x+y) aux x (x + 1) -- It is part of the application since it is indented aux x (x -- parentheses use `withoutPosition` -1) aux x x; aux x x #eval f 10
cabf6df81c462153f4ae5d299e81a077f52cc621
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/simpStar.lean
abdc7b707b0a9d54db64f2a357b9d42702cda4d2
[ "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
506
lean
opaque f (x y : Nat) : Nat opaque g (x : Nat) : Nat theorem ex1 (x : Nat) (h₁ : f x x = g x) (h₂ : g x = x) : f x (f x x) = x := by fail_if_success simp simp [*] theorem ex2 (x : Nat) (h₁ : f x x = g x) (h₂ : g x = x) : f x (f x x) = x := by simp [*] axiom g_ax (x : Nat) : g x = 0 theorem ex3 (x y : Nat) (h₁ : f x x = g x) (h₂ : f x x < 5) : f x x + f x x = 0 := by simp [*] at * trace_state have aux₁ : f x x = g x := h₁ have aux₂ : g x < 5 := h₂ simp [g_ax]
10b650a0e190de4e4a6f2f80193a1995c1903902
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/number_theory/quadratic_reciprocity.lean
c53fa07f341478ffebda67c9f0e4ec9f5047f431
[ "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
24,987
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 field_theory.finite.basic import data.zmod.basic import data.nat.parity /-! # Quadratic reciprocity. This file contains results about quadratic residues modulo a prime number. The main results are the law of quadratic reciprocity, `quadratic_reciprocity`, as well as the interpretations in terms of existence of square roots depending on the congruence mod 4, `exists_sq_eq_prime_iff_of_mod_four_eq_one`, and `exists_sq_eq_prime_iff_of_mod_four_eq_three`. Also proven are conditions for `-1` and `2` to be a square modulo a prime, `exists_sq_eq_neg_one_iff_mod_four_ne_three` and `exists_sq_eq_two_iff` ## Implementation notes The proof of quadratic reciprocity implemented uses Gauss' lemma and Eisenstein's lemma -/ open function finset nat finite_field zmod open_locale big_operators nat namespace zmod variables (p q : ℕ) [fact p.prime] [fact q.prime] /-- Euler's Criterion: A unit `x` of `zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion_units (x : units (zmod p)) : (∃ y : units (zmod p), y ^ 2 = x) ↔ x ^ (p / 2) = 1 := begin cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd, { substI p, refine iff_of_true ⟨1, _⟩ _; apply subsingleton.elim }, obtain ⟨g, hg⟩ := is_cyclic.exists_generator (units (zmod p)), obtain ⟨n, hn⟩ : x ∈ submonoid.powers g, { rw mem_powers_iff_mem_gpowers, apply hg }, split, { rintro ⟨y, rfl⟩, rw [← pow_mul, two_mul_odd_div_two hp_odd, units_pow_card_sub_one_eq_one], }, { subst x, assume h, have key : 2 * (p / 2) ∣ n * (p / 2), { rw [← pow_mul] at h, rw [two_mul_odd_div_two hp_odd, ← card_units, ← order_of_eq_card_of_forall_mem_gpowers hg], apply order_of_dvd_of_pow_eq_one h }, have : 0 < p / 2 := nat.div_pos (fact.out (1 < p)) dec_trivial, obtain ⟨m, rfl⟩ := dvd_of_mul_dvd_mul_right this key, refine ⟨g ^ m, _⟩, rw [mul_comm, pow_mul], }, end /-- Euler's Criterion: a nonzero `a : zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion {a : zmod p} (ha : a ≠ 0) : (∃ y : zmod p, y ^ 2 = a) ↔ a ^ (p / 2) = 1 := begin apply (iff_congr _ (by simp [units.ext_iff])).mp (euler_criterion_units p (units.mk0 a ha)), simp only [units.ext_iff, sq, units.coe_mk0, units.coe_mul], split, { rintro ⟨y, hy⟩, exact ⟨y, hy⟩ }, { rintro ⟨y, rfl⟩, have hy : y ≠ 0, { rintro rfl, simpa [zero_pow] using ha, }, refine ⟨units.mk0 y hy, _⟩, simp, } end lemma exists_sq_eq_neg_one_iff_mod_four_ne_three : (∃ y : zmod p, y ^ 2 = -1) ↔ p % 4 ≠ 3 := begin cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd, { substI p, exact dec_trivial }, haveI := fact.mk hp_odd, have neg_one_ne_zero : (-1 : zmod p) ≠ 0, from mt neg_eq_zero.1 one_ne_zero, rw [euler_criterion p neg_one_ne_zero, neg_one_pow_eq_pow_mod_two], cases mod_two_eq_zero_or_one (p / 2) with p_half_even p_half_odd, { rw [p_half_even, pow_zero, eq_self_iff_true, true_iff], contrapose! p_half_even with hp, rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp], exact dec_trivial }, { rw [p_half_odd, pow_one, iff_false_intro (ne_neg_self p one_ne_zero).symm, false_iff, not_not], rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl] at p_half_odd, rw [← nat.mod_mul_left_mod _ 2, show 2 * 2 = 4, from rfl] at hp_odd, have hp : p % 4 < 4, from nat.mod_lt _ dec_trivial, revert hp hp_odd p_half_odd, generalize : p % 4 = k, dec_trivial! } end lemma pow_div_two_eq_neg_one_or_one {a : zmod p} (ha : a ≠ 0) : a ^ (p / 2) = 1 ∨ a ^ (p / 2) = -1 := begin cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd, { substI p, revert a ha, exact dec_trivial }, rw [← mul_self_eq_one_iff, ← pow_add, ← two_mul, two_mul_odd_div_two hp_odd], exact pow_card_sub_one_eq_one ha end /-- **Wilson's Lemma**: the product of `1`, ..., `p-1` is `-1` modulo `p`. -/ @[simp] lemma wilsons_lemma : ((p - 1)! : zmod p) = -1 := begin refine calc ((p - 1)! : zmod p) = (∏ x in Ico 1 (succ (p - 1)), x) : by rw [← finset.prod_Ico_id_eq_factorial, prod_nat_cast] ... = (∏ x : units (zmod p), x) : _ ... = -1 : by simp_rw [← units.coe_hom_apply, ← (units.coe_hom (zmod p)).map_prod, prod_univ_units_id_eq_neg_one, units.coe_hom_apply, units.coe_neg, units.coe_one], have hp : 0 < p := (fact.out p.prime).pos, symmetry, refine prod_bij (λ a _, (a : zmod p).val) _ _ _ _, { intros a ha, rw [Ico.mem, ← nat.succ_sub hp, nat.succ_sub_one], split, { apply nat.pos_of_ne_zero, rw ← @val_zero p, assume h, apply units.ne_zero a (val_injective p h) }, { exact val_lt _ } }, { intros a ha, simp only [cast_id, nat_cast_val], }, { intros _ _ _ _ h, rw units.ext_iff, exact val_injective p h }, { intros b hb, rw [Ico.mem, nat.succ_le_iff, ← succ_sub hp, succ_sub_one, pos_iff_ne_zero] at hb, refine ⟨units.mk0 b _, finset.mem_univ _, _⟩, { assume h, apply hb.1, apply_fun val at h, simpa only [val_cast_of_lt hb.right, val_zero] using h }, { simp only [val_cast_of_lt hb.right, units.coe_mk0], } } end @[simp] lemma prod_Ico_one_prime : (∏ x in Ico 1 p, (x : zmod p)) = -1 := begin conv in (Ico 1 p) { rw [← succ_sub_one p, succ_sub (fact.out p.prime).pos] }, rw [← prod_nat_cast, finset.prod_Ico_id_eq_factorial, wilsons_lemma] end end zmod /-- The image of the map sending a non zero natural number `x ≤ p / 2` to the absolute value of the element of interger in the interval `(-p/2, p/2]` congruent to `a * x` mod p is the set of non zero natural numbers `x` such that `x ≤ p / 2` -/ lemma Ico_map_val_min_abs_nat_abs_eq_Ico_map_id (p : ℕ) [hp : fact p.prime] (a : zmod p) (hap : a ≠ 0) : (Ico 1 (p / 2).succ).1.map (λ x, (a * x).val_min_abs.nat_abs) = (Ico 1 (p / 2).succ).1.map (λ a, a) := begin have he : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x ≠ 0 ∧ x ≤ p / 2, by simp [nat.lt_succ_iff, nat.succ_le_iff, pos_iff_ne_zero] {contextual := tt}, have hep : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x < p, from λ x hx, lt_of_le_of_lt (he hx).2 (nat.div_lt_self hp.1.pos dec_trivial), have hpe : ∀ {x}, x ∈ Ico 1 (p / 2).succ → ¬ p ∣ x, from λ x hx hpx, not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero (he hx).1) hpx) (hep hx), have hmem : ∀ (x : ℕ) (hx : x ∈ Ico 1 (p / 2).succ), (a * x : zmod p).val_min_abs.nat_abs ∈ Ico 1 (p / 2).succ, { assume x hx, simp [hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hx, lt_succ_iff, succ_le_iff, pos_iff_ne_zero, nat_abs_val_min_abs_le _], }, have hsurj : ∀ (b : ℕ) (hb : b ∈ Ico 1 (p / 2).succ), ∃ x ∈ Ico 1 (p / 2).succ, b = (a * x : zmod p).val_min_abs.nat_abs, { assume b hb, refine ⟨(b / a : zmod p).val_min_abs.nat_abs, Ico.mem.mpr ⟨_, _⟩, _⟩, { apply nat.pos_of_ne_zero, simp only [div_eq_mul_inv, hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hb, not_false_iff, val_min_abs_eq_zero, inv_eq_zero, int.nat_abs_eq_zero, ne.def, mul_eq_zero, or_self] }, { apply lt_succ_of_le, apply nat_abs_val_min_abs_le }, { rw nat_cast_nat_abs_val_min_abs, split_ifs, { erw [mul_div_cancel' _ hap, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (Ico.mem.1 hb).2), int.nat_abs_of_nat], }, { erw [mul_neg_eq_neg_mul_symm, mul_div_cancel' _ hap, nat_abs_val_min_abs_neg, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (Ico.mem.1 hb).2), int.nat_abs_of_nat] } } }, exact multiset.map_eq_map_of_bij_of_nodup _ _ (finset.nodup _) (finset.nodup _) (λ x _, (a * x : zmod p).val_min_abs.nat_abs) hmem (λ _ _, rfl) (inj_on_of_surj_on_of_card_le _ hmem hsurj (le_refl _)) hsurj end private lemma gauss_lemma_aux₁ (p : ℕ) [fact p.prime] [fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) * (p / 2)! : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! := calc (a ^ (p / 2) * (p / 2)! : zmod p) = (∏ x in Ico 1 (p / 2).succ, a * x) : by rw [prod_mul_distrib, ← prod_nat_cast, ← prod_nat_cast, prod_Ico_id_eq_factorial, prod_const, Ico.card, succ_sub_one]; simp ... = (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val) : by simp ... = (∏ x in Ico 1 (p / 2).succ, (if (a * x : zmod p).val ≤ p / 2 then 1 else -1) * (a * x : zmod p).val_min_abs.nat_abs) : prod_congr rfl $ λ _ _, begin simp only [nat_cast_nat_abs_val_min_abs], split_ifs; simp end ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : have (∏ x in Ico 1 (p / 2).succ, if (a * x : zmod p).val ≤ p / 2 then (1 : zmod p) else -1) = (∏ x in (Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2), -1), from prod_bij_ne_one (λ x _ _, x) (λ x, by split_ifs; simp * at * {contextual := tt}) (λ _ _ _ _ _ _, id) (λ b h _, ⟨b, by simp [-not_le, *] at *⟩) (by intros; split_ifs at *; simp * at *), by rw [prod_mul_distrib, this]; simp ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! : by rw [← prod_nat_cast, finset.prod_eq_multiset_prod, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.prod_eq_multiset_prod, prod_Ico_id_eq_factorial] private lemma gauss_lemma_aux₂ (p : ℕ) [hp : fact p.prime] [fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := (mul_left_inj' (show ((p / 2)! : zmod p) ≠ 0, by rw [ne.def, char_p.cast_eq_zero_iff (zmod p) p, hp.1.dvd_factorial, not_le]; exact nat.div_lt_self hp.1.pos dec_trivial)).1 $ by simpa using gauss_lemma_aux₁ p hap private lemma eisenstein_lemma_aux₁ (p : ℕ) [fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card + ∑ x in Ico 1 (p / 2).succ, x + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) := have hp2 : (p : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 hp2.1, calc ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((∑ x in Ico 1 (p / 2).succ, ((a * x) % p + p * ((a * x) / p)) : ℕ) : zmod 2) : by simp only [mod_add_div] ... = (∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) : by simp only [val_nat_cast]; simp [sum_add_distrib, mul_sum.symm, nat.cast_add, nat.cast_mul, nat.cast_sum, hp2] ... = _ : congr_arg2 (+) (calc ((∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) : zmod 2) = ∑ x in Ico 1 (p / 2).succ, ((((a * x : zmod p).val_min_abs + (if (a * x : zmod p).val ≤ p / 2 then 0 else p)) : ℤ) : zmod 2) : by simp only [(val_eq_ite_val_min_abs _).symm]; simp [nat.cast_sum] ... = ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card + ((∑ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : ℕ) : by { simp [ite_cast, add_comm, sum_add_distrib, finset.sum_ite, hp2, nat.cast_sum], } ... = _ : by rw [finset.sum_eq_multiset_sum, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.sum_eq_multiset_sum]; simp [nat.cast_sum]) rfl private lemma eisenstein_lemma_aux₂ (p : ℕ) [fact p.prime] [fact (p % 2 = 1)] {a : ℕ} (ha2 : a % 2 = 1) (hap : (a : zmod p) ≠ 0) : ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card ≡ ∑ x in Ico 1 (p / 2).succ, (x * a) / p [MOD 2] := have ha2 : (a : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 ha2, (eq_iff_modeq_nat 2).1 $ sub_eq_zero.1 $ by simpa [add_left_comm, sub_eq_add_neg, finset.mul_sum.symm, mul_comm, ha2, nat.cast_sum, add_neg_eq_iff_eq_add.symm, neg_eq_self_mod_two, add_assoc] using eq.symm (eisenstein_lemma_aux₁ p hap) lemma div_eq_filter_card {a b c : ℕ} (hb0 : 0 < b) (hc : a / b ≤ c) : a / b = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card := calc a / b = (Ico 1 (a / b).succ).card : by simp ... = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card : congr_arg _ $ finset.ext $ λ x, have x * b ≤ a → x ≤ c, from λ h, le_trans (by rwa [le_div_iff_mul_le _ _ hb0]) hc, by simp [lt_succ_iff, le_div_iff_mul_le _ _ hb0]; tauto /-- The given sum is the number of integer points in the triangle formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)` -/ private lemma sum_Ico_eq_card_lt {p q : ℕ} : ∑ a in Ico 1 (p / 2).succ, (a * q) / p = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)).card := if hp0 : p = 0 then by simp [hp0, finset.ext_iff] else calc ∑ a in Ico 1 (p / 2).succ, (a * q) / p = ∑ a in Ico 1 (p / 2).succ, ((Ico 1 (q / 2).succ).filter (λ x, x * p ≤ a * q)).card : finset.sum_congr rfl $ λ x hx, div_eq_filter_card (nat.pos_of_ne_zero hp0) (calc x * q / p ≤ (p / 2) * q / p : nat.div_le_div_right (mul_le_mul_of_nonneg_right (le_of_lt_succ $ by finish) (nat.zero_le _)) ... ≤ _ : nat.div_mul_div_le_div _ _ _) ... = _ : by rw [← card_sigma]; exact card_congr (λ a _, ⟨a.1, a.2⟩) (by simp only [mem_filter, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, heq_iff_eq, forall_true_iff] {contextual := tt}) (λ ⟨b₁, b₂⟩ h, ⟨⟨b₁, b₂⟩, by revert h; simp only [mem_filter, eq_self_iff_true, exists_prop_of_true, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}⟩) /-- Each of the sums in this lemma is the cardinality of the set integer points in each of the two triangles formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)`. Adding them gives the number of points in the rectangle. -/ private lemma sum_mul_div_add_sum_mul_div_eq_mul (p q : ℕ) [hp : fact p.prime] (hq0 : (q : zmod p) ≠ 0) : ∑ a in Ico 1 (p / 2).succ, (a * q) / p + ∑ a in Ico 1 (q / 2).succ, (a * p) / q = (p / 2) * (q / 2) := begin have hswap : (((Ico 1 (q / 2).succ).product (Ico 1 (p / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * q ≤ x.1 * p)).card = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)).card := card_congr (λ x _, prod.swap x) (λ ⟨_, _⟩, by simp only [mem_filter, and_self, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, prod.swap_prod_mk, forall_true_iff] {contextual := tt}) (λ ⟨x₁, x₂⟩ h, ⟨⟨x₂, x₁⟩, by revert h; simp only [mem_filter, eq_self_iff_true, and_self, exists_prop_of_true, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}⟩), have hdisj : disjoint (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)) (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)), { apply disjoint_filter.2 (λ x hx hpq hqp, _), have hxp : x.1 < p, from lt_of_le_of_lt (show x.1 ≤ p / 2, by simp only [*, lt_succ_iff, Ico.mem, mem_product] at *; tauto) (nat.div_lt_self hp.1.pos dec_trivial), have : (x.1 : zmod p) = 0, { simpa [hq0] using congr_arg (coe : ℕ → zmod p) (le_antisymm hpq hqp) }, apply_fun zmod.val at this, rw [val_cast_of_lt hxp, val_zero] at this, simpa only [this, nonpos_iff_eq_zero, Ico.mem, one_ne_zero, false_and, mem_product] using hx }, have hunion : ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q) ∪ ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p) = ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)), from finset.ext (λ x, by have := le_total (x.2 * p) (x.1 * q); simp only [mem_union, mem_filter, Ico.mem, mem_product]; tauto), rw [sum_Ico_eq_card_lt, sum_Ico_eq_card_lt, hswap, ← card_disjoint_union hdisj, hunion, card_product], simp only [Ico.card, nat.sub_zero, succ_sub_succ_eq_sub] end variables (p q : ℕ) [fact p.prime] [fact q.prime] namespace zmod /-- The Legendre symbol of `a` and `p` is an integer defined as * `0` if `a` is `0` modulo `p`; * `1` if `a ^ (p / 2)` is `1` modulo `p` (by `euler_criterion` this is equivalent to “`a` is a square modulo `p`”); * `-1` otherwise. -/ def legendre_sym (a p : ℕ) : ℤ := if (a : zmod p) = 0 then 0 else if (a : zmod p) ^ (p / 2) = 1 then 1 else -1 lemma legendre_sym_eq_pow (a p : ℕ) [hp : fact p.prime] : (legendre_sym a p : zmod p) = (a ^ (p / 2)) := begin rw legendre_sym, by_cases ha : (a : zmod p) = 0, { simp only [if_pos, ha, zero_pow (nat.div_pos (hp.1.two_le) (succ_pos 1)), int.cast_zero] }, cases hp.1.eq_two_or_odd with hp2 hp_odd, { substI p, generalize : (a : (zmod 2)) = b, revert b, dec_trivial, }, { haveI := fact.mk hp_odd, rw if_neg ha, have : (-1 : zmod p) ≠ 1, from (ne_neg_self p one_ne_zero).symm, cases pow_div_two_eq_neg_one_or_one p ha with h h, { rw [if_pos h, h, int.cast_one], }, { rw [h, if_neg this, int.cast_neg, int.cast_one], } } end lemma legendre_sym_eq_one_or_neg_one (a p : ℕ) (ha : (a : zmod p) ≠ 0) : legendre_sym a p = -1 ∨ legendre_sym a p = 1 := by unfold legendre_sym; split_ifs; simp only [*, eq_self_iff_true, or_true, true_or] at * lemma legendre_sym_eq_zero_iff (a p : ℕ) : legendre_sym a p = 0 ↔ (a : zmod p) = 0 := begin split, { classical, contrapose, assume ha, cases legendre_sym_eq_one_or_neg_one a p ha with h h, all_goals { rw h, norm_num } }, { assume ha, rw [legendre_sym, if_pos ha] } end /-- Gauss' lemma. The legendre symbol can be computed by considering the number of naturals less than `p/2` such that `(a * x) % p > p / 2` -/ lemma gauss_lemma {a : ℕ} [fact (p % 2 = 1)] (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1) ^ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := have (legendre_sym a p : zmod p) = (((-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card : ℤ) : zmod p), by rw [legendre_sym_eq_pow, gauss_lemma_aux₂ p ha0]; simp, begin cases legendre_sym_eq_one_or_neg_one a p ha0; cases neg_one_pow_eq_or ℤ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card; simp [*, ne_neg_self p one_ne_zero, (ne_neg_self p one_ne_zero).symm] at * end lemma legendre_sym_eq_one_iff {a : ℕ} (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = 1 ↔ (∃ b : zmod p, b ^ 2 = a) := begin rw [euler_criterion p ha0, legendre_sym, if_neg ha0], split_ifs, { simp only [h, eq_self_iff_true] }, finish -- this is quite slow. I'm actually surprised that it can close the goal at all! end lemma eisenstein_lemma [fact (p % 2 = 1)] {a : ℕ} (ha1 : a % 2 = 1) (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1)^∑ x in Ico 1 (p / 2).succ, (x * a) / p := by rw [neg_one_pow_eq_pow_mod_two, gauss_lemma p ha0, neg_one_pow_eq_pow_mod_two, show _ = _, from eisenstein_lemma_aux₂ p ha1 ha0] /-- **Quadratic reciprocity theorem** -/ theorem quadratic_reciprocity [hp1 : fact (p % 2 = 1)] [hq1 : fact (q % 2 = 1)] (hpq : p ≠ q) : legendre_sym p q * legendre_sym q p = (-1) ^ ((p / 2) * (q / 2)) := have hpq0 : (p : zmod q) ≠ 0, from prime_ne_zero q p hpq.symm, have hqp0 : (q : zmod p) ≠ 0, from prime_ne_zero p q hpq, by rw [eisenstein_lemma q hp1.1 hpq0, eisenstein_lemma p hq1.1 hqp0, ← pow_add, sum_mul_div_add_sum_mul_div_eq_mul q p hpq0, mul_comm] -- move this local attribute [instance] lemma fact_prime_two : fact (nat.prime 2) := ⟨nat.prime_two⟩ lemma legendre_sym_two [hp1 : fact (p % 2 = 1)] : legendre_sym 2 p = (-1) ^ (p / 4 + p / 2) := have hp2 : p ≠ 2, from mt (congr_arg (% 2)) (by simpa using hp1.1), have hp22 : p / 2 / 2 = _ := div_eq_filter_card (show 0 < 2, from dec_trivial) (nat.div_le_self (p / 2) 2), have hcard : (Ico 1 (p / 2).succ).card = p / 2, by simp, have hx2 : ∀ x ∈ Ico 1 (p / 2).succ, (2 * x : zmod p).val = 2 * x, from λ x hx, have h2xp : 2 * x < p, from calc 2 * x ≤ 2 * (p / 2) : mul_le_mul_of_nonneg_left (le_of_lt_succ $ by finish) dec_trivial ... < _ : by conv_rhs {rw [← div_add_mod p 2, hp1.1]}; exact lt_succ_self _, by rw [← nat.cast_two, ← nat.cast_mul, val_cast_of_lt h2xp], have hdisj : disjoint ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)), from disjoint_filter.2 (λ x hx, by simp [hx2 _ hx, mul_comm]), have hunion : ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ∪ ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)) = Ico 1 (p / 2).succ, begin rw [filter_union_right], conv_rhs {rw [← @filter_true _ (Ico 1 (p / 2).succ)]}, exact filter_congr (λ x hx, by simp [hx2 _ hx, lt_or_le, mul_comm]) end, begin rw [gauss_lemma p (prime_ne_zero p 2 hp2), neg_one_pow_eq_pow_mod_two, @neg_one_pow_eq_pow_mod_two _ _ (p / 4 + p / 2)], refine congr_arg2 _ rfl ((eq_iff_modeq_nat 2).1 _), rw [show 4 = 2 * 2, from rfl, ← nat.div_div_eq_div_mul, hp22, nat.cast_add, ← sub_eq_iff_eq_add', sub_eq_add_neg, neg_eq_self_mod_two, ← nat.cast_add, ← card_disjoint_union hdisj, hunion, hcard] end lemma exists_sq_eq_two_iff [hp1 : fact (p % 2 = 1)] : (∃ a : zmod p, a ^ 2 = 2) ↔ p % 8 = 1 ∨ p % 8 = 7 := have hp2 : ((2 : ℕ) : zmod p) ≠ 0, from prime_ne_zero p 2 (λ h, by simpa [h] using hp1.1), have hpm4 : p % 4 = p % 8 % 4, from (nat.mod_mul_left_mod p 2 4).symm, have hpm2 : p % 2 = p % 8 % 2, from (nat.mod_mul_left_mod p 4 2).symm, begin rw [show (2 : zmod p) = (2 : ℕ), by simp, ← legendre_sym_eq_one_iff p hp2, legendre_sym_two p, neg_one_pow_eq_one_iff_even (show (-1 : ℤ) ≠ 1, from dec_trivial), even_add, even_div, even_div], have := nat.mod_lt p (show 0 < 8, from dec_trivial), resetI, rw fact_iff at hp1, revert this hp1, erw [hpm4, hpm2], generalize hm : p % 8 = m, unfreezingI {clear_dependent p}, dec_trivial!, end lemma exists_sq_eq_prime_iff_of_mod_four_eq_one (hp1 : p % 4 = 1) [hq1 : fact (q % 2 = 1)] : (∃ a : zmod p, a ^ 2 = q) ↔ ∃ b : zmod q, b ^ 2 = p := if hpq : p = q then by substI hpq else have h1 : ((p / 2) * (q / 2)) % 2 = 0, from (dvd_iff_mod_eq_zero _ _).1 (dvd_mul_of_dvd_left ((dvd_iff_mod_eq_zero _ _).2 $ by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp1]; refl) _), begin haveI hp_odd : fact (p % 2 = 1) := ⟨odd_of_mod_four_eq_one hp1⟩, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hqp0, if_neg hpq0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction, end lemma exists_sq_eq_prime_iff_of_mod_four_eq_three (hp3 : p % 4 = 3) (hq3 : q % 4 = 3) (hpq : p ≠ q) : (∃ a : zmod p, a ^ 2 = q) ↔ ¬∃ b : zmod q, b ^ 2 = p := have h1 : ((p / 2) * (q / 2)) % 2 = 1, from nat.odd_mul_odd (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp3]; refl) (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hq3]; refl), begin haveI hp_odd : fact (p % 2 = 1) := ⟨odd_of_mod_four_eq_three hp3⟩, haveI hq_odd : fact (q % 2 = 1) := ⟨odd_of_mod_four_eq_three hq3⟩, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hpq0, if_neg hqp0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction end end zmod
d0a0df6204cf275696c8114b43edc1fced5be391
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/app_builder_tac1.lean
64d91ca732f4e5403b65edd6dbb6677a657901d0
[ "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
520
lean
open tactic list nat name set_option trace.app_builder true set_option pp.all true meta_definition mk_ite (c a b : expr) : tactic expr := mk_mapp `ite [some c, none, none, some a, some b] example (a b : nat) : nat := by do a ← get_local `a, b ← get_local `b, mk_app `add [a, b] >>= trace, mk_app `mul [a, b] >>= trace, mk_app `sub [a, b] >>= trace, c ← mk_app `eq [a, b], trace c, mk_ite c a b >>= trace, mk_ite c b a >>= trace, assumption, return ()
d45e9a205ca47d315de507353d47117f96f64353
5c4b17dae42fab1d4f493f3b52977bffa54fefea
/4.6.1.lean
10886c00e73687156c80b736f358ab1f12ca5153
[]
no_license
hyponymous/theorem-proving-in-lean-solutions
9214cb45cc87347862fd17dfdea79fdf24b9df92
a95320ae81c90c1b15da04574602cd378794400d
refs/heads/master
1,585,777,733,214
1,541,039,359,000
1,541,039,359,000
153,676,525
2
0
null
null
null
null
UTF-8
Lean
false
false
1,036
lean
variables (α : Type) (p q : α → Prop) example : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := iff.intro (assume h : (∀ x, p x ∧ q x), show (∀ x, p x) ∧ (∀ x, q x), from and.intro (assume z : α, (h z).left) (assume z : α, (h z).right)) (assume h : (∀ x, p x) ∧ (∀ x, q x), show (∀ x, p x ∧ q x), from assume z : α, and.intro (h.left z) (h.right z)) example : (∀ x, p x → q x) → (∀ x, p x) → (∀ x, q x) := assume h1 : (∀ x, p x → q x), show (∀ x, p x) → (∀ x, q x), from assume h2 : (∀ x, p x), show (∀ x, q x), from assume z : α, (h1 z) (h2 z) example : (∀ x, p x) ∨ (∀ x, q x) → ∀ x, p x ∨ q x := assume h : (∀ x, p x) ∨ (∀ x, q x), show ∀ x, p x ∨ q x, from assume z : α, or.elim h (assume hl : ∀ x, p x, or.inl (hl z)) (assume hr : ∀ x, q x, or.inr (hr z))
85706764f5011d05aada7ce31e1e1ed23ed6f36f
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/lamexp.lean
69a4587f58ae4c754d0554b20d00fa969732f219
[ "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,768
lean
inductive ty | base : string → ty | arr : ty → ty → ty namespace ty instance : decidable_eq ty := by tactic.mk_dec_eq_instance instance : inhabited ty := ⟨ty.base "o"⟩ end ty @[reducible] def ctx := list ty inductive lamexp_core : ctx → ty → Type | var : Π {Γ : ctx} {t} (n : ℕ), list.nth Γ n = some t → lamexp_core Γ t | fv : Π {Γ}, string → Π t : ty, lamexp_core Γ t | con : Π {Γ}, string → Π t : ty, lamexp_core Γ t | app : Π {Γ} {t s}, lamexp_core Γ (ty.arr t s) → lamexp_core Γ t → lamexp_core Γ s | abs : Π {Γ : ctx} {t s}, lamexp_core (t::Γ) s → lamexp_core Γ (ty.arr t s) namespace lamexp_core -- instance {Γ t} : decidable_eq (lamexp_core Γ t) := by tactic.mk_dec_eq_instance instance {Γ t} : inhabited (lamexp_core Γ t) := ⟨lamexp_core.con "c" t⟩ end lamexp_core @[reducible] def lamexp := lamexp_core [] namespace lamexp lemma nth_append {α} {ys : list α} : Π {n xs}, list.nth xs n ≠ none → list.nth (xs ++ ys) n = list.nth xs n | 0 [] h := by contradiction | (n+1) [] h := by contradiction | 0 (x::xs) h := rfl | (n+1) (x::xs) h := @nth_append n xs h def weaken_core : ∀ {Γ Δ t}, lamexp_core Γ t → lamexp_core (Γ ++ Δ) t | Γ Δ .(t) (@lamexp_core.var .(Γ) t n h) := lamexp_core.var n $ begin rw nth_append, assumption, rw h, intro, contradiction end | Γ Δ .(t) (lamexp_core.fv n t) := lamexp_core.fv n t | Γ Δ .(t) (lamexp_core.con n t) := lamexp_core.con n t | Γ Δ .(s) (@lamexp_core.app .(Γ) t s a b) := lamexp_core.app (weaken_core a) (weaken_core b) | Γ Δ (ty.arr .(t) .(s)) (@lamexp_core.abs .(Γ) t s a) := lamexp_core.abs $ cast (by simp) (weaken_core a) def weaken {Γ t} : lamexp t → lamexp_core Γ t := weaken_core end lamexp
3a1594454aabeac993ab77d570f6e43e68bb9abd
cc060cf567f81c404a13ee79bf21f2e720fa6db0
/lean/20171217-false.lean
0314ec37cad4be380344ef58f04d9a2e22d69b1d
[ "Apache-2.0" ]
permissive
semorrison/proof
cf0a8c6957153bdb206fd5d5a762a75958a82bca
5ee398aa239a379a431190edbb6022b1a0aa2c70
refs/heads/master
1,610,414,502,842
1,518,696,851,000
1,518,696,851,000
78,375,937
2
1
null
null
null
null
UTF-8
Lean
false
false
160
lean
lemma main : false := begin have helper : (∃ x : ℕ, true) → false := λ ⟨x, h⟩, by apply _fun_match; assumption, exact helper ⟨0, ⟨⟩⟩, end
d2eae8b960eb256d83919268adcdc7c3115822aa
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/analysis/normed_space/finite_dimension.lean
ffa6612043a0d8e636a8ddb5569e1491d8e58706
[ "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
12,834
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.normed_space.operator_norm linear_algebra.finite_dimensional tactic.omega /-! # Finite dimensional normed spaces over complete fields Over a complete nondiscrete field, in finite dimension, all norms are equivalent and all linear maps are continuous. Moreover, a finite-dimensional subspace is always complete and closed. ## Main results: * `linear_map.continuous_of_finite_dimensional` : a linear map on a finite-dimensional space over a complete field is continuous. * `finite_dimensional.complete` : a finite-dimensional space over a complete field is complete. This is not registered as an instance, as the field would be an unknown metavariable in typeclass resolution. * `submodule.closed_of_finite_dimensional` : a finite-dimensional subspace over a complete field is closed * `finite_dimensional.proper` : a finite-dimensional space over a proper field is proper. This is not registered as an instance, as the field would be an unknown metavariable in typeclass resolution. It is however registered as an instance for `𝕜 = ℝ` and `𝕜 = ℂ`. As properness implies completeness, there is no need to also register `finite_dimensional.complete` on `ℝ` or `ℂ`. ## Implementation notes The fact that all norms are equivalent is not written explicitly, as it would mean having two norms on a single space, which is not the way type classes work. However, if one has a finite-dimensional vector space `E` with a norm, and a copy `E'` of this type with another norm, then the identities from `E` to `E'` and from `E'`to `E` are continuous thanks to `linear_map.continuous_of_finite_dimensional`. This gives the desired norm equivalence. -/ universes u v w x open set finite_dimensional open_locale classical -- To get a reasonable compile time for `continuous_equiv_fun_basis`, typeclass inference needs -- to be guided. local attribute [instance, priority 10000] pi.module normed_space.to_module submodule.add_comm_group submodule.module linear_map.finite_dimensional_range Pi.complete nondiscrete_normed_field.to_normed_field set_option class.instance_max_depth 100 /-- A linear map on `ι → 𝕜` (where `ι` is a fintype) is continuous -/ lemma linear_map.continuous_on_pi {ι : Type w} [fintype ι] {𝕜 : Type u} [normed_field 𝕜] {E : Type v} [add_comm_group E] [vector_space 𝕜 E] [topological_space E] [topological_add_group E] [topological_vector_space 𝕜 E] (f : (ι → 𝕜) →ₗ[𝕜] E) : continuous f := begin -- for the proof, write `f` in the standard basis, and use that each coordinate is a continuous -- function. have : (f : (ι → 𝕜) → E) = (λx, finset.sum finset.univ (λi:ι, x i • (f (λj, if i = j then 1 else 0)))), by { ext x, exact f.pi_apply_eq_sum_univ x }, rw this, refine continuous_finset_sum _ (λi hi, _), exact (continuous_apply i).smul continuous_const end section complete_field variables {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {F : Type w} [normed_group F] [normed_space 𝕜 F] {F' : Type x} [add_comm_group F'] [vector_space 𝕜 F'] [topological_space F'] [topological_add_group F'] [topological_vector_space 𝕜 F'] [complete_space 𝕜] set_option class.instance_max_depth 150 /-- In finite dimension over a complete field, the canonical identification (in terms of a basis) with `𝕜^n` together with its sup norm is continuous. This is the nontrivial part in the fact that all norms are equivalent in finite dimension. Do not use this statement as its formulation is awkward (in terms of the dimension `n`, as the proof is done by induction over `n`) and it is superceded by the fact that every linear map on a finite-dimensional space is continuous, in `linear_map.continuous_of_finite_dimensional`. -/ lemma continuous_equiv_fun_basis {n : ℕ} {ι : Type v} [fintype ι] (ξ : ι → E) (hn : fintype.card ι = n) (hξ : is_basis 𝕜 ξ) : continuous (equiv_fun_basis hξ) := begin unfreezeI, induction n with n IH generalizing ι E, { apply linear_map.continuous_of_bound _ 0 (λx, _), have : equiv_fun_basis hξ x = 0, by { ext i, exact (fintype.card_eq_zero_iff.1 hn i).elim }, change ∥equiv_fun_basis hξ x∥ ≤ 0 * ∥x∥, rw this, simp [norm_nonneg] }, { haveI : finite_dimensional 𝕜 E := of_finite_basis hξ, -- first step: thanks to the inductive assumption, any n-dimensional subspace is equivalent -- to a standard space of dimension n, hence it is complete and therefore closed. have H₁ : ∀s : submodule 𝕜 E, findim 𝕜 s = n → is_closed (s : set E), { assume s s_dim, rcases exists_is_basis_finite 𝕜 s with ⟨b, b_basis, b_finite⟩, letI : fintype b := finite.fintype b_finite, have U : uniform_embedding (equiv_fun_basis b_basis).symm.to_equiv, { have : fintype.card b = n, by { rw ← s_dim, exact (findim_eq_card_basis b_basis).symm }, have : continuous (equiv_fun_basis b_basis) := IH (subtype.val : b → s) this b_basis, exact (equiv_fun_basis b_basis).symm.uniform_embedding (linear_map.continuous_on_pi _) this }, have : is_complete (s : set E), from complete_space_coe_iff_is_complete.1 ((complete_space_congr U).1 (by apply_instance)), exact is_closed_of_is_complete this }, -- second step: any linear form is continuous, as its kernel is closed by the first step have H₂ : ∀f : E →ₗ[𝕜] 𝕜, continuous f, { assume f, have : findim 𝕜 f.ker = n ∨ findim 𝕜 f.ker = n.succ, { have Z := f.findim_range_add_findim_ker, rw [findim_eq_card_basis hξ, hn] at Z, have : findim 𝕜 f.range = 0 ∨ findim 𝕜 f.range = 1, { have I : ∀(k : ℕ), k ≤ 1 ↔ k = 0 ∨ k = 1, by omega manual, have : findim 𝕜 f.range ≤ findim 𝕜 𝕜 := submodule.findim_le _, rwa [findim_of_field, I] at this }, cases this, { rw this at Z, right, simpa using Z }, { left, rw [this, add_comm, nat.add_one] at Z, exact nat.succ_inj Z } }, have : is_closed (f.ker : set E), { cases this, { exact H₁ _ this }, { have : f.ker = ⊤, by { apply eq_top_of_findim_eq, rw [findim_eq_card_basis hξ, hn, this] }, simp [this] } }, exact linear_map.continuous_iff_is_closed_ker.2 this }, -- third step: applying the continuity to the linear form corresponding to a coefficient in the -- basis decomposition, deduce that all such coefficients are controlled in terms of the norm have : ∀i:ι, ∃C, 0 ≤ C ∧ ∀(x:E), ∥equiv_fun_basis hξ x i∥ ≤ C * ∥x∥, { assume i, let f : E →ₗ[𝕜] 𝕜 := (linear_map.proj i).comp (equiv_fun_basis hξ), let f' : E →L[𝕜] 𝕜 := { cont := H₂ f, ..f }, exact ⟨∥f'∥, norm_nonneg _, λx, continuous_linear_map.le_op_norm f' x⟩ }, -- fourth step: combine the bound on each coefficient to get a global bound and the continuity choose C0 hC0 using this, let C := finset.sum finset.univ C0, have C_nonneg : 0 ≤ C := finset.sum_nonneg (λi hi, (hC0 i).1), have C0_le : ∀i, C0 i ≤ C := λi, finset.single_le_sum (λj hj, (hC0 j).1) (finset.mem_univ _), apply linear_map.continuous_of_bound _ C (λx, _), rw pi_norm_le_iff, { exact λi, le_trans ((hC0 i).2 x) (mul_le_mul_of_nonneg_right (C0_le i) (norm_nonneg _)) }, { exact mul_nonneg C_nonneg (norm_nonneg _) } } end /-- Any linear map on a finite dimensional space over a complete field is continuous. -/ theorem linear_map.continuous_of_finite_dimensional [finite_dimensional 𝕜 E] (f : E →ₗ[𝕜] F') : continuous f := begin -- for the proof, go to a model vector space `b → 𝕜` thanks to `continuous_equiv_fun_basis`, and -- argue that all linear maps there are continuous. rcases exists_is_basis_finite 𝕜 E with ⟨b, b_basis, b_finite⟩, letI : fintype b := finite.fintype b_finite, have A : continuous (equiv_fun_basis b_basis) := continuous_equiv_fun_basis _ rfl b_basis, have B : continuous (f.comp ((equiv_fun_basis b_basis).symm : (b → 𝕜) →ₗ[𝕜] E)) := linear_map.continuous_on_pi _, have : continuous ((f.comp ((equiv_fun_basis b_basis).symm : (b → 𝕜) →ₗ[𝕜] E)) ∘ (equiv_fun_basis b_basis)) := B.comp A, convert this, ext x, simp only [linear_equiv.coe_apply, function.comp_app, coe_fn_coe_base, linear_map.comp_apply], rw linear_equiv.symm_apply_apply end /-- The continuous linear map induced by a linear map on a finite dimensional space -/ def linear_map.to_continuous_linear_map [finite_dimensional 𝕜 E] (f : E →ₗ[𝕜] F') : E →L[𝕜] F' := { cont := f.continuous_of_finite_dimensional, ..f } /-- The continuous linear equivalence induced by a linear equivalence on a finite dimensional space. -/ def linear_equiv.to_continuous_linear_equiv [finite_dimensional 𝕜 E] (e : E ≃ₗ[𝕜] F) : E ≃L[𝕜] F := { continuous_to_fun := e.to_linear_map.continuous_of_finite_dimensional, continuous_inv_fun := begin haveI : finite_dimensional 𝕜 F := e.finite_dimensional, exact e.symm.to_linear_map.continuous_of_finite_dimensional end, ..e } /-- Any finite-dimensional vector space over a complete field is complete. We do not register this as an instance to avoid an instance loop when trying to prove the completeness of `𝕜`, and the search for `𝕜` as an unknown metavariable. Declare the instance explicitly when needed. -/ variables (𝕜 E) lemma finite_dimensional.complete [finite_dimensional 𝕜 E] : complete_space E := begin rcases exists_is_basis_finite 𝕜 E with ⟨b, b_basis, b_finite⟩, letI : fintype b := finite.fintype b_finite, have : uniform_embedding (equiv_fun_basis b_basis).symm := linear_equiv.uniform_embedding _ (linear_map.continuous_of_finite_dimensional _) (linear_map.continuous_of_finite_dimensional _), change uniform_embedding (equiv_fun_basis b_basis).symm.to_equiv at this, exact (complete_space_congr this).1 (by apply_instance) end variables {𝕜 E} /-- A finite-dimensional subspace is complete. -/ lemma submodule.complete_of_finite_dimensional (s : submodule 𝕜 E) [finite_dimensional 𝕜 s] : is_complete (s : set E) := begin haveI : complete_space s := finite_dimensional.complete 𝕜 s, have : is_complete (range (subtype.val : s → E)), { rw [← image_univ, is_complete_image_iff], { exact complete_univ }, { exact isometry_subtype_val.uniform_embedding } }, rwa subtype.val_range at this end /-- A finite-dimensional subspace is closed. -/ lemma submodule.closed_of_finite_dimensional (s : submodule 𝕜 E) [finite_dimensional 𝕜 s] : is_closed (s : set E) := is_closed_of_is_complete s.complete_of_finite_dimensional end complete_field section proper_field variables (𝕜 : Type u) [nondiscrete_normed_field 𝕜] (E : Type v) [normed_group E] [normed_space 𝕜 E] [proper_space 𝕜] /-- Any finite-dimensional vector space over a proper field is proper. We do not register this as an instance to avoid an instance loop when trying to prove the properness of `𝕜`, and the search for `𝕜` as an unknown metavariable. Declare the instance explicitly when needed. -/ lemma finite_dimensional.proper [finite_dimensional 𝕜 E] : proper_space E := begin rcases exists_is_basis_finite 𝕜 E with ⟨b, b_basis, b_finite⟩, letI : fintype b := finite.fintype b_finite, let e := equiv_fun_basis b_basis, let f : E →L[𝕜] (b → 𝕜) := { cont := linear_map.continuous_of_finite_dimensional _, ..e.to_linear_map }, refine metric.proper_image_of_proper e.symm (linear_map.continuous_of_finite_dimensional _) _ (∥f∥) (λx y, _), { exact equiv.range_eq_univ e.symm.to_equiv }, { have A : e (e.symm x) = x := linear_equiv.apply_symm_apply _ _, have B : e (e.symm y) = y := linear_equiv.apply_symm_apply _ _, conv_lhs { rw [← A, ← B] }, change dist (f (e.symm x)) (f (e.symm y)) ≤ ∥f∥ * dist (e.symm x) (e.symm y), exact f.lipschitz.dist_le_mul _ _ } end end proper_field /- Over the real numbers, we can register the previous statement as an instance as it will not cause problems in instance resolution since the properness of `ℝ` is already known. -/ instance finite_dimensional.proper_real (E : Type u) [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] : proper_space E := finite_dimensional.proper ℝ E attribute [instance, priority 900] finite_dimensional.proper_real
caf6a9d34ed3cb9d435ceddf59493f40ad856702
26ac254ecb57ffcb886ff709cf018390161a9225
/src/order/filter/bases.lean
bbacfc16afea719bc478a3b8abb156b758da90cb
[ "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
31,112
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury Kudryashov, Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import order.filter.at_top_bot import data.set.countable /-! # Filter bases A filter basis `B : filter_basis α` on a type `α` is a nonempty collection of sets of `α` such that the intersection of two elements of this collection contains some element of the collection. Compared to filters, filter bases do not require that any set containing an element of `B` belongs to `B`. A filter basis `B` can be used to construct `B.filter : filter α` such that a set belongs to `B.filter` if and only if it contains an element of `B`. Given an indexing type `ι`, a predicate `p : ι → Prop`, and a map `s : ι → set α`, the proposition `h : filter.is_basis p s` makes sure the range of `s` bounded by `p` (ie. `s '' set_of p`) defines a filter basis `h.filter_basis`. If one already has a filter `l` on `α`, `filter.has_basis l p s` (where `p : ι → Prop` and `s : ι → set α` as above) means that a set belongs to `l` if and only if it contains some `s i` with `p i`. It implies `h : filter.is_basis p s`, and `l = h.filter_basis.filter`. The point of this definition is that checking statements involving elements of `l` often reduces to checking them on the basis elements. This file also introduces more restricted classes of bases, involving monotonicity or countability. In particular, for `l : filter α`, `l.is_countably_generated` means there is a countable set of sets which generates `s`. This is reformulated in term of bases, and consequences are derived. ## Main statements * `has_basis.mem_iff`, `has_basis.mem_of_superset`, `has_basis.mem_of_mem` : restate `t ∈ f` in terms of a basis; * `basis_sets` : all sets of a filter form a basis; * `has_basis.inf`, `has_basis.inf_principal`, `has_basis.prod`, `has_basis.prod_self`, `has_basis.map`, `has_basis.comap` : combinators to construct filters of `l ⊓ l'`, `l ⊓ 𝓟 t`, `l.prod l'`, `l.prod l`, `l.map f`, `l.comap f` respectively; * `has_basis.le_iff`, `has_basis.ge_iff`, has_basis.le_basis_iff` : restate `l ≤ l'` in terms of bases. * `has_basis.tendsto_right_iff`, `has_basis.tendsto_left_iff`, `has_basis.tendsto_iff` : restate `tendsto f l l'` in terms of bases. * `is_countably_generated_iff_exists_antimono_basis` : proves a filter is countably generated if and only if it admis a basis parametrized by a decreasing sequence of sets indexed by `ℕ`. * `tendsto_iff_seq_tendsto ` : an abstract version of "sequentially continuous implies continuous". ## Implementation notes As with `Union`/`bUnion`/`sUnion`, there are three different approaches to filter bases: * `has_basis l s`, `s : set (set α)`; * `has_basis l s`, `s : ι → set α`; * `has_basis l p s`, `p : ι → Prop`, `s : ι → set α`. We use the latter one because, e.g., `𝓝 x` in an `emetric_space` or in a `metric_space` has a basis of this form. The other two can be emulated using `s = id` or `p = λ _, true`. With this approach sometimes one needs to `simp` the statement provided by the `has_basis` machinery, e.g., `simp only [exists_prop, true_and]` or `simp only [forall_const]` can help with the case `p = λ _, true`. -/ open set filter open_locale filter variables {α : Type*} {β : Type*} {γ : Type*} {ι : Type*} {ι' : Type*} /-- A filter basis `B` on a type `α` is a nonempty collection of sets of `α` such that the intersection of two elements of this collection contains some element of the collection. -/ structure filter_basis (α : Type*) := (sets : set (set α)) (nonempty : sets.nonempty) (inter_sets {x y} : x ∈ sets → y ∈ sets → ∃ z ∈ sets, z ⊆ x ∩ y) /-- If `B` is a filter basis on `α`, and `U` a subset of `α` then we can write `U ∈ B` as on paper. -/ @[reducible] instance {α : Type*}: has_mem (set α) (filter_basis α) := ⟨λ U B, U ∈ B.sets⟩ -- For illustration purposes, the filter basis defining (at_top : filter ℕ) instance : inhabited (filter_basis ℕ) := ⟨{ sets := range Ici, nonempty := ⟨Ici 0, mem_range_self 0⟩, inter_sets := begin rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, refine ⟨Ici (max n m), mem_range_self _, _⟩, rintros p p_in, split ; rw mem_Ici at *, exact le_of_max_le_left p_in, exact le_of_max_le_right p_in, end }⟩ /-- `is_basis p s` means the image of `s` bounded by `p` is a filter basis. -/ protected structure filter.is_basis (p : ι → Prop) (s : ι → set α) : Prop := (nonempty : ∃ i, p i) (inter : ∀ {i j}, p i → p j → ∃ k, p k ∧ s k ⊆ s i ∩ s j) namespace filter namespace is_basis /-- Constructs a filter basis from an indexed family of sets satisfying `is_basis`. -/ protected def filter_basis {p : ι → Prop} {s : ι → set α} (h : is_basis p s) : filter_basis α := { sets := s '' set_of p, nonempty := let ⟨i, hi⟩ := h.nonempty in ⟨s i, mem_image_of_mem s hi⟩, inter_sets := by { rintros _ _ ⟨i, hi, rfl⟩ ⟨j, hj, rfl⟩, rcases h.inter hi hj with ⟨k, hk, hk'⟩, exact ⟨_, mem_image_of_mem s hk, hk'⟩ } } variables {p : ι → Prop} {s : ι → set α} (h : is_basis p s) lemma mem_filter_basis_iff {U : set α} : U ∈ h.filter_basis ↔ ∃ i, p i ∧ s i = U := iff.rfl end is_basis end filter namespace filter_basis /-- The filter associated to a filter basis. -/ protected def filter (B : filter_basis α) : filter α := { sets := {s | ∃ t ∈ B, t ⊆ s}, univ_sets := let ⟨s, s_in⟩ := B.nonempty in ⟨s, s_in, s.subset_univ⟩, sets_of_superset := λ x y ⟨s, s_in, h⟩ hxy, ⟨s, s_in, set.subset.trans h hxy⟩, inter_sets := λ x y ⟨s, s_in, hs⟩ ⟨t, t_in, ht⟩, let ⟨u, u_in, u_sub⟩ := B.inter_sets s_in t_in in ⟨u, u_in, set.subset.trans u_sub $ set.inter_subset_inter hs ht⟩ } lemma mem_filter_iff (B : filter_basis α) {U : set α} : U ∈ B.filter ↔ ∃ s ∈ B, s ⊆ U := iff.rfl lemma mem_filter_of_mem (B : filter_basis α) {U : set α} : U ∈ B → U ∈ B.filter:= λ U_in, ⟨U, U_in, subset.refl _⟩ lemma eq_infi_principal (B : filter_basis α) : B.filter = ⨅ s : B.sets, 𝓟 s := begin ext U, rw [mem_filter_iff, mem_infi], { simp }, { rintros ⟨U, U_in⟩ ⟨V, V_in⟩, rcases B.inter_sets U_in V_in with ⟨W, W_in, W_sub⟩, use [W, W_in], finish }, cases B.nonempty with U U_in, exact ⟨⟨U, U_in⟩⟩, end protected lemma generate (B : filter_basis α) : generate B.sets = B.filter := begin apply le_antisymm, { intros U U_in, rcases B.mem_filter_iff.mp U_in with ⟨V, V_in, h⟩, exact generate_sets.superset (generate_sets.basic V_in) h }, { rw sets_iff_generate, apply mem_filter_of_mem } end end filter_basis namespace filter namespace is_basis variables {p : ι → Prop} {s : ι → set α} /-- Constructs a filter from an indexed family of sets satisfying `is_basis`. -/ protected def filter (h : is_basis p s) : filter α := h.filter_basis.filter protected lemma mem_filter_iff (h : is_basis p s) {U : set α} : U ∈ h.filter ↔ ∃ i, p i ∧ s i ⊆ U := begin erw [h.filter_basis.mem_filter_iff], simp only [mem_filter_basis_iff h, exists_prop], split, { rintros ⟨_, ⟨i, pi, rfl⟩, h⟩, tauto }, { tauto } end lemma filter_eq_generate (h : is_basis p s) : h.filter = generate {U | ∃ i, p i ∧ s i = U} := by erw h.filter_basis.generate ; refl end is_basis /-- We say that a filter `l` has a basis `s : ι → set α` bounded by `p : ι → Prop`, if `t ∈ l` if and only if `t` includes `s i` for some `i` such that `p i`. -/ protected structure has_basis (l : filter α) (p : ι → Prop) (s : ι → set α) : Prop := (mem_iff' : ∀ (t : set α), t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t) section same_type variables {l l' : filter α} {p : ι → Prop} {s : ι → set α} {t : set α} {i : ι} {p' : ι' → Prop} {s' : ι' → set α} {i' : ι'} lemma has_basis_generate (s : set (set α)) : (generate s).has_basis (λ t, finite t ∧ t ⊆ s) (λ t, ⋂₀ t) := ⟨begin intro U, rw mem_generate_iff, apply exists_congr, tauto end⟩ /-- The smallest filter basis containing a given collection of sets. -/ def filter_basis.of_sets (s : set (set α)) : filter_basis α := { sets := sInter '' { t | finite t ∧ t ⊆ s}, nonempty := ⟨univ, ∅, ⟨⟨finite_empty, empty_subset s⟩, sInter_empty⟩⟩, inter_sets := begin rintros _ _ ⟨a, ⟨fina, suba⟩, rfl⟩ ⟨b, ⟨finb, subb⟩, rfl⟩, exact ⟨⋂₀ (a ∪ b), mem_image_of_mem _ ⟨fina.union finb, union_subset suba subb⟩, by rw sInter_union⟩, end } /-- Definition of `has_basis` unfolded with implicit set argument. -/ lemma has_basis.mem_iff (hl : l.has_basis p s) : t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t := hl.mem_iff' t protected lemma is_basis.has_basis (h : is_basis p s) : has_basis h.filter p s := ⟨λ t, by simp only [h.mem_filter_iff, exists_prop]⟩ lemma has_basis.mem_of_superset (hl : l.has_basis p s) (hi : p i) (ht : s i ⊆ t) : t ∈ l := (hl.mem_iff).2 ⟨i, hi, ht⟩ lemma has_basis.mem_of_mem (hl : l.has_basis p s) (hi : p i) : s i ∈ l := hl.mem_of_superset hi $ subset.refl _ lemma has_basis.is_basis (h : l.has_basis p s) : is_basis p s := { nonempty := let ⟨i, hi, H⟩ := h.mem_iff.mp univ_mem_sets in ⟨i, hi⟩, inter := λ i j hi hj, by simpa [h.mem_iff] using l.inter_sets (h.mem_of_mem hi) (h.mem_of_mem hj) } lemma has_basis.filter_eq (h : l.has_basis p s) : h.is_basis.filter = l := by { ext U, simp [h.mem_iff, is_basis.mem_filter_iff] } lemma has_basis.eq_generate (h : l.has_basis p s) : l = generate { U | ∃ i, p i ∧ s i = U } := by rw [← h.is_basis.filter_eq_generate, h.filter_eq] lemma generate_eq_generate_inter (s : set (set α)) : generate s = generate (sInter '' { t | finite t ∧ t ⊆ s}) := by erw [(filter_basis.of_sets s).generate, ← (has_basis_generate s).filter_eq] ; refl lemma of_sets_filter_eq_generate (s : set (set α)) : (filter_basis.of_sets s).filter = generate s := by rw [← (filter_basis.of_sets s).generate, generate_eq_generate_inter s] ; refl lemma has_basis.eventually_iff (hl : l.has_basis p s) {q : α → Prop} : (∀ᶠ x in l, q x) ↔ ∃ i, p i ∧ ∀ ⦃x⦄, x ∈ s i → q x := by simpa using hl.mem_iff lemma has_basis.forall_nonempty_iff_ne_bot (hl : l.has_basis p s) : (∀ {i}, p i → (s i).nonempty) ↔ ne_bot l := ⟨λ H, forall_sets_nonempty_iff_ne_bot.1 $ λ s hs, let ⟨i, hi, his⟩ := hl.mem_iff.1 hs in (H hi).mono his, λ H i hi, H.nonempty_of_mem (hl.mem_of_mem hi)⟩ lemma basis_sets (l : filter α) : l.has_basis (λ s : set α, s ∈ l) id := ⟨λ t, exists_sets_subset_iff.symm⟩ lemma has_basis_self {l : filter α} {P : set α → Prop} : has_basis l (λ s, s ∈ l ∧ P s) id ↔ ∀ t, (t ∈ l ↔ ∃ r ∈ l, P r ∧ r ⊆ t) := begin split, { rintros ⟨h⟩ t, convert h t, ext s, tauto, }, { intro h, constructor, intro t, convert h t, ext s, tauto } end lemma at_top_basis [nonempty α] [semilattice_sup α] : (@at_top α _).has_basis (λ _, true) Ici := ⟨λ t, by simpa only [exists_prop, true_and] using @mem_at_top_sets α _ _ t⟩ lemma at_top_basis' [semilattice_sup α] (a : α) : (@at_top α _).has_basis (λ x, a ≤ x) Ici := ⟨λ t, (@at_top_basis α ⟨a⟩ _).mem_iff.trans ⟨λ ⟨x, _, hx⟩, ⟨x ⊔ a, le_sup_right, λ y hy, hx (le_trans le_sup_left hy)⟩, λ ⟨x, _, hx⟩, ⟨x, trivial, hx⟩⟩⟩ theorem has_basis.ge_iff (hl' : l'.has_basis p' s') : l ≤ l' ↔ ∀ i', p' i' → s' i' ∈ l := ⟨λ h i' hi', h $ hl'.mem_of_mem hi', λ h s hs, let ⟨i', hi', hs⟩ := hl'.mem_iff.1 hs in mem_sets_of_superset (h _ hi') hs⟩ theorem has_basis.le_iff (hl : l.has_basis p s) : l ≤ l' ↔ ∀ t ∈ l', ∃ i (hi : p i), s i ⊆ t := by simp only [le_def, hl.mem_iff] theorem has_basis.le_basis_iff (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : l ≤ l' ↔ ∀ i', p' i' → ∃ i (hi : p i), s i ⊆ s' i' := by simp only [hl'.ge_iff, hl.mem_iff] lemma has_basis.inf (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊓ l').has_basis (λ i : ι × ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∩ s' i.2) := ⟨begin intro t, simp only [mem_inf_sets, exists_prop, hl.mem_iff, hl'.mem_iff], split, { rintros ⟨t, ⟨i, hi, ht⟩, t', ⟨i', hi', ht'⟩, H⟩, use [(i, i'), ⟨hi, hi'⟩, subset.trans (inter_subset_inter ht ht') H] }, { rintros ⟨⟨i, i'⟩, ⟨hi, hi'⟩, H⟩, use [s i, i, hi, subset.refl _, s' i', i', hi', subset.refl _, H] } end⟩ lemma has_basis.inf_principal (hl : l.has_basis p s) (s' : set α) : (l ⊓ 𝓟 s').has_basis p (λ i, s i ∩ s') := ⟨λ t, by simp only [mem_inf_principal, hl.mem_iff, subset_def, mem_set_of_eq, mem_inter_iff, and_imp]⟩ lemma has_basis.eq_binfi (h : l.has_basis p s) : l = ⨅ i (_ : p i), 𝓟 (s i) := eq_binfi_of_mem_sets_iff_exists_mem $ λ t, by simp only [h.mem_iff, mem_principal_sets] lemma has_basis.eq_infi (h : l.has_basis (λ _, true) s) : l = ⨅ i, 𝓟 (s i) := by simpa only [infi_true] using h.eq_binfi @[nolint ge_or_gt] -- see Note [nolint_ge] lemma has_basis_infi_principal {s : ι → set α} (h : directed (≥) s) (ne : nonempty ι) : (⨅ i, 𝓟 (s i)).has_basis (λ _, true) s := ⟨begin refine λ t, (mem_infi (h.mono_comp _ _) ne t).trans $ by simp only [exists_prop, true_and, mem_principal_sets], exact λ _ _, principal_mono.2 end⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma has_basis_binfi_principal {s : β → set α} {S : set β} (h : directed_on (s ⁻¹'o (≥)) S) (ne : S.nonempty) : (⨅ i ∈ S, 𝓟 (s i)).has_basis (λ i, i ∈ S) s := ⟨begin refine λ t, (mem_binfi _ ne).trans $ by simp only [mem_principal_sets], rw [directed_on_iff_directed, ← directed_comp, (∘)] at h ⊢, apply h.mono_comp _ _, exact λ _ _, principal_mono.2 end⟩ lemma has_basis.map (f : α → β) (hl : l.has_basis p s) : (l.map f).has_basis p (λ i, f '' (s i)) := ⟨λ t, by simp only [mem_map, image_subset_iff, hl.mem_iff, preimage]⟩ lemma has_basis.comap (f : β → α) (hl : l.has_basis p s) : (l.comap f).has_basis p (λ i, f ⁻¹' (s i)) := ⟨begin intro t, simp only [mem_comap_sets, exists_prop, hl.mem_iff], split, { rintros ⟨t', ⟨i, hi, ht'⟩, H⟩, exact ⟨i, hi, subset.trans (preimage_mono ht') H⟩ }, { rintros ⟨i, hi, H⟩, exact ⟨s i, ⟨i, hi, subset.refl _⟩, H⟩ } end⟩ lemma comap_has_basis (f : α → β) (l : filter β) : has_basis (comap f l) (λ s : set β, s ∈ l) (λ s, f ⁻¹' s) := ⟨λ t, mem_comap_sets⟩ lemma has_basis.prod_self (hl : l.has_basis p s) : (l.prod l).has_basis p (λ i, (s i).prod (s i)) := ⟨begin intro t, apply mem_prod_iff.trans, split, { rintros ⟨t₁, ht₁, t₂, ht₂, H⟩, rcases hl.mem_iff.1 (inter_mem_sets ht₁ ht₂) with ⟨i, hi, ht⟩, exact ⟨i, hi, λ p ⟨hp₁, hp₂⟩, H ⟨(ht hp₁).1, (ht hp₂).2⟩⟩ }, { rintros ⟨i, hi, H⟩, exact ⟨s i, hl.mem_of_mem hi, s i, hl.mem_of_mem hi, H⟩ } end⟩ lemma has_basis.exists_iff (hl : l.has_basis p s) {P : set α → Prop} (mono : ∀ ⦃s t⦄, s ⊆ t → P t → P s) : (∃ s ∈ l, P s) ↔ ∃ (i) (hi : p i), P (s i) := ⟨λ ⟨s, hs, hP⟩, let ⟨i, hi, his⟩ := hl.mem_iff.1 hs in ⟨i, hi, mono his hP⟩, λ ⟨i, hi, hP⟩, ⟨s i, hl.mem_of_mem hi, hP⟩⟩ lemma has_basis.forall_iff (hl : l.has_basis p s) {P : set α → Prop} (mono : ∀ ⦃s t⦄, s ⊆ t → P s → P t) : (∀ s ∈ l, P s) ↔ ∀ i, p i → P (s i) := ⟨λ H i hi, H (s i) $ hl.mem_of_mem hi, λ H s hs, let ⟨i, hi, his⟩ := hl.mem_iff.1 hs in mono his (H i hi)⟩ lemma has_basis.sInter_sets (h : has_basis l p s) : ⋂₀ l.sets = ⋂ i ∈ set_of p, s i := begin ext x, suffices : (∀ t ∈ l, x ∈ t) ↔ ∀ i, p i → x ∈ s i, by simpa only [mem_Inter, mem_set_of_eq, mem_sInter], simp_rw h.mem_iff, split, { intros h i hi, exact h (s i) ⟨i, hi, subset.refl _⟩ }, { rintros h _ ⟨i, hi, sub⟩, exact sub (h i hi) }, end variables [preorder ι] (l p s) /-- `is_antimono_basis p s` means the image of `s` bounded by `p` is a filter basis such that `s` is decreasing and `p` is increasing, ie `i ≤ j → p i → p j`. -/ structure is_antimono_basis extends is_basis p s : Prop := (decreasing : ∀ {i j}, p i → p j → i ≤ j → s j ⊆ s i) (mono : monotone p) /-- We say that a filter `l` has a antimono basis `s : ι → set α` bounded by `p : ι → Prop`, if `t ∈ l` if and only if `t` includes `s i` for some `i` such that `p i`, and `s` is decreasing and `p` is increasing, ie `i ≤ j → p i → p j`. -/ structure has_antimono_basis [preorder ι] (l : filter α) (p : ι → Prop) (s : ι → set α) extends has_basis l p s : Prop := (decreasing : ∀ {i j}, p i → p j → i ≤ j → s j ⊆ s i) (mono : monotone p) end same_type section two_types variables {la : filter α} {pa : ι → Prop} {sa : ι → set α} {lb : filter β} {pb : ι' → Prop} {sb : ι' → set β} {f : α → β} lemma has_basis.tendsto_left_iff (hla : la.has_basis pa sa) : tendsto f la lb ↔ ∀ t ∈ lb, ∃ i (hi : pa i), ∀ x ∈ sa i, f x ∈ t := by { simp only [tendsto, (hla.map f).le_iff, image_subset_iff], refl } lemma has_basis.tendsto_right_iff (hlb : lb.has_basis pb sb) : tendsto f la lb ↔ ∀ i (hi : pb i), ∀ᶠ x in la, f x ∈ sb i := by simp only [tendsto, hlb.ge_iff, mem_map, filter.eventually] lemma has_basis.tendsto_iff (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : tendsto f la lb ↔ ∀ ib (hib : pb ib), ∃ ia (hia : pa ia), ∀ x ∈ sa ia, f x ∈ sb ib := by simp [hlb.tendsto_right_iff, hla.eventually_iff] lemma tendsto.basis_left (H : tendsto f la lb) (hla : la.has_basis pa sa) : ∀ t ∈ lb, ∃ i (hi : pa i), ∀ x ∈ sa i, f x ∈ t := hla.tendsto_left_iff.1 H lemma tendsto.basis_right (H : tendsto f la lb) (hlb : lb.has_basis pb sb) : ∀ i (hi : pb i), ∀ᶠ x in la, f x ∈ sb i := hlb.tendsto_right_iff.1 H lemma tendsto.basis_both (H : tendsto f la lb) (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : ∀ ib (hib : pb ib), ∃ ia (hia : pa ia), ∀ x ∈ sa ia, f x ∈ sb ib := (hla.tendsto_iff hlb).1 H lemma has_basis.prod (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : (la.prod lb).has_basis (λ i : ι × ι', pa i.1 ∧ pb i.2) (λ i, (sa i.1).prod (sb i.2)) := (hla.comap prod.fst).inf (hlb.comap prod.snd) lemma has_basis.prod' {la : filter α} {lb : filter β} {ι : Type*} {p : ι → Prop} {sa : ι → set α} {sb : ι → set β} (hla : la.has_basis p sa) (hlb : lb.has_basis p sb) (h_dir : ∀ {i j}, p i → p j → ∃ k, p k ∧ sa k ⊆ sa i ∧ sb k ⊆ sb j) : (la.prod lb).has_basis p (λ i, (sa i).prod (sb i)) := ⟨begin intros t, rw mem_prod_iff, split, { rintros ⟨u, u_in, v, v_in, huv⟩, rcases hla.mem_iff.mp u_in with ⟨i, hi, si⟩, rcases hlb.mem_iff.mp v_in with ⟨j, hj, sj⟩, rcases h_dir hi hj with ⟨k, hk, ki, kj⟩, use [k, hk], calc (sa k).prod (sb k) ⊆ (sa i).prod (sb j) : set.prod_mono ki kj ... ⊆ u.prod v : set.prod_mono si sj ... ⊆ t : huv, }, { rintro ⟨i, hi, h⟩, exact ⟨sa i, hla.mem_of_mem hi, sb i, hlb.mem_of_mem hi, h⟩ }, end⟩ lemma has_antimono_basis.tendsto [semilattice_sup ι] [nonempty ι] {l : filter α} {p : ι → Prop} {s : ι → set α} (hl : l.has_antimono_basis p s) {φ : ι → α} (h : ∀ i : ι, φ i ∈ s i) : tendsto φ at_top l := begin rw hl.to_has_basis.tendsto_right_iff, intros i hi, rw eventually_at_top, exact ⟨i, λ j hij, hl.decreasing hi (hl.mono hij hi) hij (h j)⟩, end end two_types /-- `is_countably_generated f` means `f = generate s` for some countable `s`. -/ def is_countably_generated (f : filter α) : Prop := ∃ s : set (set α), countable s ∧ f = generate s /-- `is_countable_basis p s` means the image of `s` bounded by `p` is a countable filter basis. -/ structure is_countable_basis (p : ι → Prop) (s : ι → set α) extends is_basis p s : Prop := (countable : countable $ set_of p) /-- We say that a filter `l` has a countable basis `s : ι → set α` bounded by `p : ι → Prop`, if `t ∈ l` if and only if `t` includes `s i` for some `i` such that `p i`, and the set defined by `p` is countable. -/ structure has_countable_basis (l : filter α) (p : ι → Prop) (s : ι → set α) extends has_basis l p s : Prop := (countable : countable $ set_of p) /-- A countable filter basis `B` on a type `α` is a nonempty countable collection of sets of `α` such that the intersection of two elements of this collection contains some element of the collection. -/ structure countable_filter_basis (α : Type*) extends filter_basis α := (countable : countable sets) -- For illustration purposes, the countable filter basis defining (at_top : filter ℕ) instance nat.inhabited_countable_filter_basis : inhabited (countable_filter_basis ℕ) := ⟨{ countable := countable_range (λ n, Ici n), ..(default $ filter_basis ℕ),}⟩ lemma antimono_seq_of_seq (s : ℕ → set α) : ∃ t : ℕ → set α, (∀ i j, i ≤ j → t j ⊆ t i) ∧ (⨅ i, 𝓟 $ s i) = ⨅ i, 𝓟 (t i) := begin use λ n, ⋂ m ≤ n, s m, split, { intros i j hij a, simp, intros h i' hi'i, apply h, transitivity; assumption }, apply le_antisymm; rw le_infi_iff; intro i, { rw le_principal_iff, apply Inter_mem_sets (finite_le_nat _), intros j hji, rw ← le_principal_iff, apply infi_le_of_le j _, apply le_refl _ }, { apply infi_le_of_le i _, rw principal_mono, intro a, simp, intro h, apply h, refl }, end lemma countable_binfi_eq_infi_seq [complete_lattice α] {B : set ι} (Bcbl : countable B) (Bne : B.nonempty) (f : ι → α) : ∃ (x : ℕ → ι), (⨅ t ∈ B, f t) = ⨅ i, f (x i) := begin rw countable_iff_exists_surjective_to_subtype Bne at Bcbl, rcases Bcbl with ⟨g, gsurj⟩, rw infi_subtype', use (λ n, g n), apply le_antisymm; rw le_infi_iff, { intro i, apply infi_le_of_le (g i) _, apply le_refl _ }, { intros a, rcases gsurj a with i, apply infi_le_of_le i _, subst h, apply le_refl _ } end lemma countable_binfi_eq_infi_seq' [complete_lattice α] {B : set ι} (Bcbl : countable B) (f : ι → α) {i₀ : ι} (h : f i₀ = ⊤) : ∃ (x : ℕ → ι), (⨅ t ∈ B, f t) = ⨅ i, f (x i) := begin cases B.eq_empty_or_nonempty with hB Bnonempty, { rw [hB, infi_emptyset], use λ n, i₀, simp [h] }, { exact countable_binfi_eq_infi_seq Bcbl Bnonempty f } end lemma countable_binfi_principal_eq_seq_infi {B : set (set α)} (Bcbl : countable B) : ∃ (x : ℕ → set α), (⨅ t ∈ B, 𝓟 t) = ⨅ i, 𝓟 (x i) := countable_binfi_eq_infi_seq' Bcbl 𝓟 principal_univ namespace is_countably_generated /-- A set generating a countably generated filter. -/ def generating_set {f : filter α} (h : is_countably_generated f) := classical.some h lemma countable_generating_set {f : filter α} (h : is_countably_generated f) : countable h.generating_set := (classical.some_spec h).1 lemma eq_generate {f : filter α} (h : is_countably_generated f) : f = generate h.generating_set := (classical.some_spec h).2 /-- A countable filter basis for a countably generated filter. -/ def countable_filter_basis {l : filter α} (h : is_countably_generated l) : countable_filter_basis α := { countable := (countable_set_of_finite_subset h.countable_generating_set).image _, ..filter_basis.of_sets (h.generating_set) } lemma filter_basis_filter {l : filter α} (h : is_countably_generated l) : h.countable_filter_basis.to_filter_basis.filter = l := begin conv_rhs { rw h.eq_generate }, apply of_sets_filter_eq_generate, end lemma has_countable_basis {l : filter α} (h : is_countably_generated l) : l.has_countable_basis (λ t, finite t ∧ t ⊆ h.generating_set) (λ t, ⋂₀ t) := ⟨by convert has_basis_generate _ ; exact h.eq_generate, countable_set_of_finite_subset h.countable_generating_set⟩ lemma exists_countable_infi_principal {f : filter α} (h : f.is_countably_generated) : ∃ s : set (set α), countable s ∧ f = ⨅ t ∈ s, 𝓟 t := begin let B := h.countable_filter_basis, use [B.sets, B.countable], rw ← h.filter_basis_filter, rw B.to_filter_basis.eq_infi_principal, rw infi_subtype'' end lemma exists_seq {f : filter α} (cblb : f.is_countably_generated) : ∃ x : ℕ → set α, f = ⨅ i, 𝓟 (x i) := begin rcases cblb.exists_countable_infi_principal with ⟨B, Bcbl, rfl⟩, exact countable_binfi_principal_eq_seq_infi Bcbl, end lemma exists_antimono_seq {f : filter α} (cblb : f.is_countably_generated) : ∃ x : ℕ → set α, (∀ i j, i ≤ j → x j ⊆ x i) ∧ f = ⨅ i, 𝓟 (x i) := begin rcases cblb.exists_seq with ⟨x', hx'⟩, let x := λ n, ⋂ m ≤ n, x' m, use x, split, { intros i j hij a, simp [x], intros h i' hi'i, apply h, transitivity; assumption }, subst hx', apply le_antisymm; rw le_infi_iff; intro i, { rw le_principal_iff, apply Inter_mem_sets (finite_le_nat _), intros j hji, rw ← le_principal_iff, apply infi_le_of_le j _, apply le_refl _ }, { apply infi_le_of_le i _, rw principal_mono, intro a, simp [x], intro h, apply h, refl }, end lemma has_antimono_basis {f : filter α} (h : f.is_countably_generated) : ∃ x : ℕ → set α, f.has_antimono_basis (λ _, true) x := begin rcases h.exists_antimono_seq with ⟨x, x_dec, rfl⟩, refine ⟨x, has_basis_infi_principal _ ⟨0⟩, _, monotone_const⟩, exacts [directed_of_sup x_dec, λ i j _ _, x_dec i j] end end is_countably_generated lemma is_countably_generated_seq (x : ℕ → set α) : is_countably_generated (⨅ i, 𝓟 $ x i) := begin rcases antimono_seq_of_seq x with ⟨y, am, h⟩, rw h, use [range y, countable_range _], rw (has_basis_infi_principal _ _).eq_generate, { simp [range] }, { exact directed_of_sup am }, { use 0 }, end lemma is_countably_generated_of_seq {f : filter α} (h : ∃ x : ℕ → set α, f = ⨅ i, 𝓟 $ x i) : f.is_countably_generated := let ⟨x, h⟩ := h in by rw h ; apply is_countably_generated_seq lemma is_countably_generated_binfi_principal {B : set $ set α} (h : countable B) : is_countably_generated (⨅ (s ∈ B), 𝓟 s) := is_countably_generated_of_seq (countable_binfi_principal_eq_seq_infi h) lemma is_countably_generated_iff_exists_antimono_basis {f : filter α} : is_countably_generated f ↔ ∃ x : ℕ → set α, f.has_antimono_basis (λ _, true) x := begin split, { intro h, exact h.has_antimono_basis }, { rintros ⟨x, h⟩, rw h.to_has_basis.eq_infi, exact is_countably_generated_seq x }, end namespace is_countably_generated lemma exists_antimono_seq' {f : filter α} (cblb : f.is_countably_generated) : ∃ x : ℕ → set α, (∀ i j, i ≤ j → x j ⊆ x i) ∧ ∀ {s}, (s ∈ f ↔ ∃ i, x i ⊆ s) := let ⟨x, hx⟩ := is_countably_generated_iff_exists_antimono_basis.mp cblb in ⟨x, λ i j, hx.decreasing trivial trivial, λ s, by simp [hx.to_has_basis.mem_iff]⟩ protected lemma comap {l : filter β} (h : l.is_countably_generated) (f : α → β) : (comap f l).is_countably_generated := begin rcases h.exists_seq with ⟨x, hx⟩, apply is_countably_generated_of_seq, use λ i, f ⁻¹' x i, calc comap f l = comap f (⨅ i, 𝓟 (x i)) : by rw hx ... = (⨅ i, comap f $ 𝓟 $ x i) : comap_infi ... = (⨅ i, 𝓟 $ f ⁻¹' x i) : by simp_rw comap_principal, end /-- An abstract version of continuity of sequentially continuous functions on metric spaces: if a filter `k` is countably generated then `tendsto f k l` iff for every sequence `u` converging to `k`, `f ∘ u` tends to `l`. -/ lemma tendsto_iff_seq_tendsto {f : α → β} {k : filter α} {l : filter β} (hcb : k.is_countably_generated) : tendsto f k l ↔ (∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) := suffices (∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) → tendsto f k l, from ⟨by intros; apply tendsto.comp; assumption, by assumption⟩, begin rcases hcb.exists_antimono_seq with ⟨g, gmon, gbasis⟩, have gbasis : ∀ A, A ∈ k ↔ ∃ i, g i ⊆ A, { intro A, subst gbasis, rw mem_infi, { simp only [set.mem_Union, iff_self, filter.mem_principal_sets] }, { exact directed_of_sup (λ i j h, principal_mono.mpr $ gmon _ _ h) }, { apply_instance } }, classical, contrapose, simp only [not_forall, not_imp, not_exists, subset_def, @tendsto_def _ _ f, gbasis], rintro ⟨B, hBl, hfBk⟩, choose x h using hfBk, use x, split, { simp only [tendsto_at_top', gbasis], rintros A ⟨i, hgiA⟩, use i, refine (λ j hj, hgiA $ gmon _ _ hj _), simp only [h] }, { simp only [tendsto_at_top', (∘), not_forall, not_exists], use [B, hBl], intro i, use [i, (le_refl _)], apply (h i).right }, end lemma tendsto_of_seq_tendsto {f : α → β} {k : filter α} {l : filter β} (hcb : k.is_countably_generated) : (∀ x : ℕ → α, tendsto x at_top k → tendsto (f ∘ x) at_top l) → tendsto f k l := hcb.tendsto_iff_seq_tendsto.2 lemma subseq_tendsto {f : filter α} (hf : is_countably_generated f) {u : ℕ → α} (hx : ne_bot (f ⊓ map u at_top)) : ∃ (θ : ℕ → ℕ), (strict_mono θ) ∧ (tendsto (u ∘ θ) at_top f) := begin rcases hf.has_antimono_basis with ⟨B, h⟩, have : ∀ N, ∃ n ≥ N, u n ∈ B N, from λ N, filter.inf_map_at_top_ne_bot_iff.mp hx _ (h.to_has_basis.mem_of_mem trivial) N, choose φ hφ using this, cases forall_and_distrib.mp hφ with φ_ge φ_in, have lim_uφ : tendsto (u ∘ φ) at_top f, from h.tendsto φ_in, have lim_φ : tendsto φ at_top at_top, from (tendsto_at_top_mono φ_ge tendsto_id), obtain ⟨ψ, hψ, hψφ⟩ : ∃ ψ : ℕ → ℕ, strict_mono ψ ∧ strict_mono (φ ∘ ψ), from strict_mono_subseq_of_tendsto_at_top lim_φ, exact ⟨φ ∘ ψ, hψφ, lim_uφ.comp $ strict_mono_tendsto_at_top hψ⟩, end end is_countably_generated -- TODO : prove this for a encodable type lemma is_countably_generated_at_top_finset_nat : (at_top : filter $ finset ℕ).is_countably_generated := begin apply is_countably_generated_of_seq, use λ N, Ici (finset.range N), apply eq_infi_of_mem_sets_iff_exists_mem, assume s, rw mem_at_top_sets, refine ⟨_, λ ⟨N, hN⟩, ⟨finset.range N, hN⟩⟩, rintros ⟨t, ht⟩, rcases mem_at_top_sets.1 (tendsto_finset_range (mem_at_top t)) with ⟨N, hN⟩, simp only [preimage, mem_set_of_eq] at hN, exact ⟨N, mem_principal_sets.2 $ λ t' ht', ht t' $ le_trans (hN _ $ le_refl N) ht'⟩ end end filter
22228b805b2464589824899b1e252d72f59ea18e
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/set_like/fintype.lean
92783be767416b2536168710b97e3170f9832987
[ "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
910
lean
/- Copyright (c) 2021 . All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.set_like.basic import data.fintype.basic /-! # Set-like fintype This file contains a fintype instance for set-like objects such as subgroups. If `set_like A B` and `fintype B` then `fintype A`. -/ namespace set_like /-- TODO: It should be possible to obtain a computable version of this for most set_like objects. If we add those instances, we should remove this one. -/ @[nolint dangerous_instance, instance, priority 100] noncomputable instance {A B : Type*} [fintype B] [set_like A B] : fintype A := fintype.of_injective coe set_like.coe_injective @[nolint dangerous_instance, priority 100] -- See note [lower instance priority] instance {A B : Type*} [finite B] [set_like A B] : finite A := finite.of_injective coe set_like.coe_injective end set_like
1deac0597c381ee486612dac86b918e39cf32bab
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/polynomial/tower.lean
8dc8313ae783ed5d7fba3d775f5b735f12dc440c
[ "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,363
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yuyang Zhao -/ import algebra.algebra.tower import data.polynomial.algebra_map /-! # Algebra towers for polynomial > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file proves some basic results about the algebra tower structure for the type `R[X]`. This structure itself is provided elsewhere as `polynomial.is_scalar_tower` When you update this file, you can also try to make a corresponding update in `ring_theory.mv_polynomial.tower`. -/ open_locale polynomial variables (R A B : Type*) namespace polynomial section semiring variables [comm_semiring R] [comm_semiring A] [semiring B] variables [algebra R A] [algebra A B] [algebra R B] variables [is_scalar_tower R A B] variables {R B} @[simp] theorem aeval_map_algebra_map (x : B) (p : R[X]) : aeval x (map (algebra_map R A) p) = aeval x p := by rw [aeval_def, aeval_def, eval₂_map, is_scalar_tower.algebra_map_eq R A B] end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [semiring B] variables [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B] variables {R A} lemma aeval_algebra_map_apply (x : A) (p : R[X]) : aeval (algebra_map A B x) p = algebra_map A B (aeval x p) := by rw [aeval_def, aeval_def, hom_eval₂, ←is_scalar_tower.algebra_map_eq] @[simp] lemma aeval_algebra_map_eq_zero_iff [no_zero_smul_divisors A B] [nontrivial B] (x : A) (p : R[X]) : aeval (algebra_map A B x) p = 0 ↔ aeval x p = 0 := by rw [aeval_algebra_map_apply, algebra.algebra_map_eq_smul_one, smul_eq_zero, iff_false_intro (one_ne_zero' B), or_false] variables {B} lemma aeval_algebra_map_eq_zero_iff_of_injective {x : A} {p : R[X]} (h : function.injective (algebra_map A B)) : aeval (algebra_map A B x) p = 0 ↔ aeval x p = 0 := by rw [aeval_algebra_map_apply, ← (algebra_map A B).map_zero, h.eq_iff] end comm_semiring end polynomial namespace subalgebra open polynomial section comm_semiring variables {R A} [comm_semiring R] [comm_semiring A] [algebra R A] @[simp] lemma aeval_coe (S : subalgebra R A) (x : S) (p : R[X]) : aeval (x : A) p = aeval x p := aeval_algebra_map_apply A x p end comm_semiring end subalgebra
c5e6c2504230e74c6407b4a599e93db1ef18d0c8
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/data/list/basic.lean
e7cd3009bdb54a8674b10fa7792e6a9f59923ddd
[ "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
8,090
lean
---------------------------------------------------------------------------------------------------- --- Copyright (c) 2014 Parikshit Khanna. All rights reserved. --- Released under Apache 2.0 license as described in the file LICENSE. --- Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura ---------------------------------------------------------------------------------------------------- import logic tools.helper_tactics tools.tactic data.nat.basic -- Theory list -- =========== -- -- Basic properties of lists. open eq.ops helper_tactics nat inductive list (T : Type) : Type := nil {} : list T, cons : T → list T → list T namespace list notation h :: t := cons h t notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l variable {T : Type} -- Concat -- ------ definition append (s t : list T) : list T := rec t (λx l u, x::u) s notation l₁ ++ l₂ := append l₁ l₂ theorem append.nil_left (t : list T) : nil ++ t = t theorem append.cons (x : T) (s t : list T) : x::s ++ t = x::(s ++ t) theorem append.nil_right (t : list T) : t ++ nil = t := induction_on t rfl (λx l H, H ▸ rfl) theorem append.assoc (s t u : list T) : s ++ t ++ u = s ++ (t ++ u) := induction_on s rfl (λx l H, H ▸ rfl) -- Length -- ------ definition length : list T → nat := rec 0 (λx l m, succ m) theorem length.nil : length (@nil T) = 0 theorem length.cons (x : T) (t : list T) : length (x::t) = succ (length t) theorem length.append (s t : list T) : length (s ++ t) = length s + length t := induction_on s (!add.zero_left⁻¹) (λx s H, !add.succ_left⁻¹ ▸ H ▸ rfl) -- add_rewrite length_nil length_cons -- Append -- ------ definition concat (x : T) : list T → list T := rec [x] (λy l l', y::l') theorem concat.nil (x : T) : concat x nil = [x] theorem concat.cons (x y : T) (l : list T) : concat x (y::l) = y::(concat x l) theorem concat.eq_append (x : T) (l : list T) : concat x l = l ++ [x] -- add_rewrite append_nil append_cons -- Reverse -- ------- definition reverse : list T → list T := rec nil (λx l r, r ++ [x]) theorem reverse.nil : reverse (@nil T) = nil theorem reverse.cons (x : T) (l : list T) : reverse (x::l) = concat x (reverse l) theorem reverse.singleton (x : T) : reverse [x] = [x] theorem reverse.append (s t : list T) : reverse (s ++ t) = (reverse t) ++ (reverse s) := induction_on s (!append.nil_right⁻¹) (λx s H, calc reverse (x::s ++ t) = reverse t ++ reverse s ++ [x] : {H} ... = reverse t ++ (reverse s ++ [x]) : !append.assoc) theorem reverse.reverse (l : list T) : reverse (reverse l) = l := induction_on l rfl (λx l' H, H ▸ !reverse.append) theorem concat.eq_reverse_cons (x : T) (l : list T) : concat x l = reverse (x :: reverse l) := induction_on l rfl (λy l' H, calc concat x (y::l') = (y::l') ++ [x] : !concat.eq_append ... = reverse (reverse (y::l')) ++ [x] : {!reverse.reverse⁻¹}) -- Head and tail -- ------------- definition head (x : T) : list T → T := rec x (λx l h, x) theorem head.nil (x : T) : head x nil = x theorem head.cons (x x' : T) (t : list T) : head x' (x::t) = x theorem head.concat {s : list T} (t : list T) (x : T) : s ≠ nil → (head x (s ++ t) = head x s) := cases_on s (take H : nil ≠ nil, absurd rfl H) (take x s, take H : x::s ≠ nil, calc head x (x::s ++ t) = head x (x::(s ++ t)) : {!append.cons} ... = x : !head.cons ... = head x (x::s) : !head.cons⁻¹) definition tail : list T → list T := rec nil (λx l b, l) theorem tail.nil : tail (@nil T) = nil theorem tail.cons (x : T) (l : list T) : tail (x::l) = l theorem cons_head_tail {l : list T} (x : T) : l ≠ nil → (head x l)::(tail l) = l := cases_on l (assume H : nil ≠ nil, absurd rfl H) (take x l, assume H : x::l ≠ nil, rfl) -- List membership -- --------------- definition mem (x : T) : list T → Prop := rec false (λy l H, x = y ∨ H) notation e ∈ s := mem e s theorem mem.nil (x : T) : x ∈ nil ↔ false := iff.rfl theorem mem.cons (x y : T) (l : list T) : x ∈ y::l ↔ (x = y ∨ x ∈ l) := iff.rfl theorem mem.concat_imp_or {x : T} {s t : list T} : x ∈ s ++ t → x ∈ s ∨ x ∈ t := induction_on s or.inr (take y s, assume IH : x ∈ s ++ t → x ∈ s ∨ x ∈ t, assume H1 : x ∈ y::s ++ t, have H2 : x = y ∨ x ∈ s ++ t, from H1, have H3 : x = y ∨ x ∈ s ∨ x ∈ t, from or.imp_or_right H2 IH, iff.elim_right or.assoc H3) theorem mem.or_imp_concat {x : T} {s t : list T} : x ∈ s ∨ x ∈ t → x ∈ s ++ t := induction_on s (take H, or.elim H false_elim (assume H, H)) (take y s, assume IH : x ∈ s ∨ x ∈ t → x ∈ s ++ t, assume H : x ∈ y::s ∨ x ∈ t, or.elim H (assume H1, or.elim H1 (take H2 : x = y, or.inl H2) (take H2 : x ∈ s, or.inr (IH (or.inl H2)))) (assume H1 : x ∈ t, or.inr (IH (or.inr H1)))) theorem mem.concat (x : T) (s t : list T) : x ∈ s ++ t ↔ x ∈ s ∨ x ∈ t := iff.intro mem.concat_imp_or mem.or_imp_concat theorem mem.split {x : T} {l : list T} : x ∈ l → ∃s t : list T, l = s ++ (x::t) := induction_on l (take H : x ∈ nil, false_elim (iff.elim_left !mem.nil H)) (take y l, assume IH : x ∈ l → ∃s t : list T, l = s ++ (x::t), assume H : x ∈ y::l, or.elim H (assume H1 : x = y, exists_intro nil (!exists_intro (H1 ▸ rfl))) (assume H1 : x ∈ l, obtain s (H2 : ∃t : list T, l = s ++ (x::t)), from IH H1, obtain t (H3 : l = s ++ (x::t)), from H2, have H4 : y :: l = (y::s) ++ (x::t), from H3 ▸ rfl, !exists_intro (!exists_intro H4))) definition mem.is_decidable [instance] (H : decidable_eq T) (x : T) (l : list T) : decidable (x ∈ l) := rec_on l (decidable.inr (iff.false_elim !mem.nil)) (take (h : T) (l : list T) (iH : decidable (x ∈ l)), show decidable (x ∈ h::l), from decidable.rec_on iH (assume Hp : x ∈ l, decidable.rec_on (H x h) (assume Heq : x = h, decidable.inl (or.inl Heq)) (assume Hne : x ≠ h, decidable.inl (or.inr Hp))) (assume Hn : ¬x ∈ l, decidable.rec_on (H x h) (assume Heq : x = h, decidable.inl (or.inl Heq)) (assume Hne : x ≠ h, have H1 : ¬(x = h ∨ x ∈ l), from assume H2 : x = h ∨ x ∈ l, or.elim H2 (assume Heq, absurd Heq Hne) (assume Hp, absurd Hp Hn), have H2 : ¬x ∈ h::l, from iff.elim_right (iff.flip_sign !mem.cons) H1, decidable.inr H2))) -- Find -- ---- section variable [H : decidable_eq T] include H definition find (x : T) : list T → nat := rec 0 (λy l b, if x = y then 0 else succ b) theorem find.nil (x : T) : find x nil = 0 theorem find.cons (x y : T) (l : list T) : find x (y::l) = if x = y then 0 else succ (find x l) theorem find.not_mem {l : list T} {x : T} : ¬x ∈ l → find x l = length l := rec_on l (assume P₁ : ¬x ∈ nil, rfl) (take y l, assume iH : ¬x ∈ l → find x l = length l, assume P₁ : ¬x ∈ y::l, have P₂ : ¬(x = y ∨ x ∈ l), from iff.elim_right (iff.flip_sign !mem.cons) P₁, have P₃ : ¬x = y ∧ ¬x ∈ l, from (iff.elim_left not_or P₂), calc find x (y::l) = if x = y then 0 else succ (find x l) : !find.cons ... = succ (find x l) : if_neg (and.elim_left P₃) ... = succ (length l) : {iH (and.elim_right P₃)} ... = length (y::l) : !length.cons⁻¹) end -- nth element -- ----------- definition nth (x : T) (l : list T) (n : nat) : T := nat.rec (λl, head x l) (λm f l, f (tail l)) n l theorem nth.zero (x : T) (l : list T) : nth x l 0 = head x l theorem nth.succ (x : T) (l : list T) (n : nat) : nth x l (succ n) = nth x (tail l) n end list
0ffe8df5ec8834fe349c8ec4ca7fe058a5c2c7b5
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch3/ex0104.lean
ae35412277790d7e40673376b3cf5a8cf834d7c9
[]
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
117
lean
constant Proof : Prop → Type constant modus_ponens : Π p q : Prop, (Proof p → Proof q) → Proof (implies p q)
02de878e3d3e9da2568503bf40a649ee31f5bbe5
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/rel.lean
1c1b0f2186a251ec1edbb8c26719bf7220f18794
[ "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,748
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import order.complete_lattice import order.galois_connection /-! # Relations > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines bundled relations. A relation between `α` and `β` is a function `α → β → Prop`. Relations are also known as set-valued functions, or partial multifunctions. ## Main declarations * `rel α β`: Relation between `α` and `β`. * `rel.inv`: `r.inv` is the `rel β α` obtained by swapping the arguments of `r`. * `rel.dom`: Domain of a relation. `x ∈ r.dom` iff there exists `y` such that `r x y`. * `rel.codom`: Codomain, aka range, of a relation. `y ∈ r.codom` iff there exists `x` such that `r x y`. * `rel.comp`: Relation composition. Note that the arguments order follows the `category_theory/` one, so `r.comp s x z ↔ ∃ y, r x y ∧ s y z`. * `rel.image`: Image of a set under a relation. `r.image s` is the set of `f x` over all `x ∈ s`. * `rel.preimage`: Preimage of a set under a relation. Note that `r.preimage = r.inv.image`. * `rel.core`: Core of a set. For `s : set β`, `r.core s` is the set of `x : α` such that all `y` related to `x` are in `s`. * `rel.restrict_domain`: Domain-restriction of a relation to a subtype. * `function.graph`: Graph of a function as a relation. -/ variables {α β γ : Type*} /-- A relation on `α` and `β`, aka a set-valued function, aka a partial multifunction -/ @[derive complete_lattice, derive inhabited] def rel (α β : Type*) := α → β → Prop namespace rel variables {δ : Type*} (r : rel α β) /-- The inverse relation : `r.inv x y ↔ r y x`. Note that this is *not* a groupoid inverse. -/ def inv : rel β α := flip r lemma inv_def (x : α) (y : β) : r.inv y x ↔ r x y := iff.rfl lemma inv_inv : inv (inv r) = r := by { ext x y, reflexivity } /-- Domain of a relation -/ def dom := {x | ∃ y, r x y} lemma dom_mono {r s : rel α β} (h : r ≤ s) : dom r ⊆ dom s := λ a ⟨b, hx⟩, ⟨b, h a b hx⟩ /-- Codomain aka range of a relation -/ def codom := {y | ∃ x, r x y} lemma codom_inv : r.inv.codom = r.dom := by { ext x y, reflexivity } lemma dom_inv : r.inv.dom = r.codom := by { ext x y, reflexivity} /-- Composition of relation; note that it follows the `category_theory/` order of arguments. -/ def comp (r : rel α β) (s : rel β γ) : rel α γ := λ x z, ∃ y, r x y ∧ s y z local infixr (name := rel.comp) ` ∘ ` := rel.comp lemma comp_assoc (r : rel α β) (s : rel β γ) (t : rel γ δ) : (r ∘ s) ∘ t = r ∘ s ∘ t := begin unfold comp, ext x w, split, { rintros ⟨z, ⟨y, rxy, syz⟩, tzw⟩, exact ⟨y, rxy, z, syz, tzw⟩ }, rintros ⟨y, rxy, z, syz, tzw⟩, exact ⟨z, ⟨y, rxy, syz⟩, tzw⟩ end @[simp] lemma comp_right_id (r : rel α β) : r ∘ @eq β = r := by { unfold comp, ext y, simp } @[simp] lemma comp_left_id (r : rel α β) : @eq α ∘ r = r := by { unfold comp, ext x, simp } lemma inv_id : inv (@eq α) = @eq α := by { ext x y, split; apply eq.symm } lemma inv_comp (r : rel α β) (s : rel β γ) : inv (r ∘ s) = inv s ∘ inv r := by { ext x z, simp [comp, inv, flip, and.comm] } /-- Image of a set under a relation -/ def image (s : set α) : set β := {y | ∃ x ∈ s, r x y} lemma mem_image (y : β) (s : set α) : y ∈ image r s ↔ ∃ x ∈ s, r x y := iff.rfl lemma image_subset : ((⊆) ⇒ (⊆)) r.image r.image := λ s t h y ⟨x, xs, rxy⟩, ⟨x, h xs, rxy⟩ lemma image_mono : monotone r.image := r.image_subset lemma image_inter (s t : set α) : r.image (s ∩ t) ⊆ r.image s ∩ r.image t := r.image_mono.map_inf_le s t lemma image_union (s t : set α) : r.image (s ∪ t) = r.image s ∪ r.image t := le_antisymm (λ y ⟨x, xst, rxy⟩, xst.elim (λ xs, or.inl ⟨x, ⟨xs, rxy⟩⟩) (λ xt, or.inr ⟨x, ⟨xt, rxy⟩⟩)) (r.image_mono.le_map_sup s t) @[simp] lemma image_id (s : set α) : image (@eq α) s = s := by { ext x, simp [mem_image] } lemma image_comp (s : rel β γ) (t : set α) : image (r ∘ s) t = image s (image r t) := begin ext z, simp only [mem_image], split, { rintros ⟨x, xt, y, rxy, syz⟩, exact ⟨y, ⟨x, xt, rxy⟩, syz⟩ }, rintros ⟨y, ⟨x, xt, rxy⟩, syz⟩, exact ⟨x, xt, y, rxy, syz⟩ end lemma image_univ : r.image set.univ = r.codom := by { ext y, simp [mem_image, codom] } /-- Preimage of a set under a relation `r`. Same as the image of `s` under `r.inv` -/ def preimage (s : set β) : set α := r.inv.image s lemma mem_preimage (x : α) (s : set β) : x ∈ r.preimage s ↔ ∃ y ∈ s, r x y := iff.rfl lemma preimage_def (s : set β) : preimage r s = {x | ∃ y ∈ s, r x y} := set.ext $ λ x, mem_preimage _ _ _ lemma preimage_mono {s t : set β} (h : s ⊆ t) : r.preimage s ⊆ r.preimage t := image_mono _ h lemma preimage_inter (s t : set β) : r.preimage (s ∩ t) ⊆ r.preimage s ∩ r.preimage t := image_inter _ s t lemma preimage_union (s t : set β) : r.preimage (s ∪ t) = r.preimage s ∪ r.preimage t := image_union _ s t lemma preimage_id (s : set α) : preimage (@eq α) s = s := by simp only [preimage, inv_id, image_id] lemma preimage_comp (s : rel β γ) (t : set γ) : preimage (r ∘ s) t = preimage r (preimage s t) := by simp only [preimage, inv_comp, image_comp] lemma preimage_univ : r.preimage set.univ = r.dom := by { rw [preimage, image_univ, codom_inv] } /-- Core of a set `s : set β` w.r.t `r : rel α β` is the set of `x : α` that are related *only* to elements of `s`. Other generalization of `function.preimage`. -/ def core (s : set β) := {x | ∀ y, r x y → y ∈ s} lemma mem_core (x : α) (s : set β) : x ∈ r.core s ↔ ∀ y, r x y → y ∈ s := iff.rfl lemma core_subset : ((⊆) ⇒ (⊆)) r.core r.core := λ s t h x h' y rxy, h (h' y rxy) lemma core_mono : monotone r.core := r.core_subset lemma core_inter (s t : set β) : r.core (s ∩ t) = r.core s ∩ r.core t := set.ext (by simp [mem_core, imp_and_distrib, forall_and_distrib]) lemma core_union (s t : set β) : r.core s ∪ r.core t ⊆ r.core (s ∪ t) := r.core_mono.le_map_sup s t @[simp] lemma core_univ : r.core set.univ = set.univ := set.ext (by simp [mem_core]) lemma core_id (s : set α) : core (@eq α) s = s := by simp [core] lemma core_comp (s : rel β γ) (t : set γ) : core (r ∘ s) t = core r (core s t) := begin ext x, simp [core, comp], split, { exact λ h y rxy z, h z y rxy }, { exact λ h z y rzy, h y rzy z } end /-- Restrict the domain of a relation to a subtype. -/ def restrict_domain (s : set α) : rel {x // x ∈ s} β := λ x y, r x.val y theorem image_subset_iff (s : set α) (t : set β) : image r s ⊆ t ↔ s ⊆ core r t := iff.intro (λ h x xs y rxy, h ⟨x, xs, rxy⟩) (λ h y ⟨x, xs, rxy⟩, h xs y rxy) theorem image_core_gc : galois_connection r.image r.core := image_subset_iff _ end rel namespace function /-- The graph of a function as a relation. -/ def graph (f : α → β) : rel α β := λ x y, f x = y end function namespace set -- TODO: if image were defined with bounded quantification in corelib, the next two would -- be definitional lemma image_eq (f : α → β) (s : set α) : f '' s = (function.graph f).image s := by simp [set.image, function.graph, rel.image] lemma preimage_eq (f : α → β) (s : set β) : f ⁻¹' s = (function.graph f).preimage s := by simp [set.preimage, function.graph, rel.preimage, rel.inv, flip, rel.image] lemma preimage_eq_core (f : α → β) (s : set β) : f ⁻¹' s = (function.graph f).core s := by simp [set.preimage, function.graph, rel.core] end set
c9f0cb1a6c55a2767c619947a2437eda09e402c6
4727251e0cd73359b15b664c3170e5d754078599
/src/field_theory/finiteness.lean
b8ca2c16da3a9dbdf4a1d158c7489e03d2cdf965
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,996
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 ring_theory.finiteness import linear_algebra.dimension /-! # A module over a division ring is noetherian if and only if it is finite. -/ universes u v open_locale classical cardinal open cardinal submodule module function namespace is_noetherian variables {K : Type u} {V : Type v} [division_ring K] [add_comm_group V] [module K V] /-- A module over a division ring is noetherian if and only if its dimension (as a cardinal) is strictly less than the first infinite cardinal `ω`. -/ lemma iff_dim_lt_omega : is_noetherian K V ↔ module.rank K V < ω := begin let b := basis.of_vector_space K V, rw [← b.mk_eq_dim'', lt_omega_iff_finite], split, { introI, exact finite_of_linear_independent (basis.of_vector_space_index.linear_independent K V) }, { assume hbfinite, refine @is_noetherian_of_linear_equiv K (⊤ : submodule K V) V _ _ _ _ _ (linear_equiv.of_top _ rfl) (id _), refine is_noetherian_of_fg_of_noetherian _ ⟨set.finite.to_finset hbfinite, _⟩, rw [set.finite.coe_to_finset, ← b.span_eq, basis.coe_of_vector_space, subtype.range_coe] } end variables (K V) /-- The dimension of a noetherian module over a division ring, as a cardinal, is strictly less than the first infinite cardinal `ω`. -/ lemma dim_lt_omega : ∀ [is_noetherian K V], module.rank K V < ω := is_noetherian.iff_dim_lt_omega.1 variables {K V} /-- In a noetherian module over a division ring, all bases are indexed by a finite type. -/ noncomputable def fintype_basis_index {ι : Type*} [is_noetherian K V] (b : basis ι K V) : fintype ι := b.fintype_index_of_dim_lt_omega (dim_lt_omega K V) /-- In a noetherian module over a division ring, `basis.of_vector_space` is indexed by a finite type. -/ noncomputable instance [is_noetherian K V] : fintype (basis.of_vector_space_index K V) := fintype_basis_index (basis.of_vector_space K V) /-- In a noetherian module over a division ring, if a basis is indexed by a set, that set is finite. -/ lemma finite_basis_index {ι : Type*} {s : set ι} [is_noetherian K V] (b : basis s K V) : s.finite := b.finite_index_of_dim_lt_omega (dim_lt_omega K V) variables (K V) /-- In a noetherian module over a division ring, there exists a finite basis. This is the indexing `finset`. -/ noncomputable def finset_basis_index [is_noetherian K V] : finset V := (finite_basis_index (basis.of_vector_space K V)).to_finset @[simp] lemma coe_finset_basis_index [is_noetherian K V] : (↑(finset_basis_index K V) : set V) = basis.of_vector_space_index K V := set.finite.coe_to_finset _ @[simp] lemma coe_sort_finset_basis_index [is_noetherian K V] : ((finset_basis_index K V) : Type*) = basis.of_vector_space_index K V := set.finite.coe_sort_to_finset _ /-- In a noetherian module over a division ring, there exists a finite basis. This is indexed by the `finset` `finite_dimensional.finset_basis_index`. This is in contrast to the result `finite_basis_index (basis.of_vector_space K V)`, which provides a set and a `set.finite`. -/ noncomputable def finset_basis [is_noetherian K V] : basis (finset_basis_index K V) K V := (basis.of_vector_space K V).reindex (by simp) @[simp] lemma range_finset_basis [is_noetherian K V] : set.range (finset_basis K V) = basis.of_vector_space_index K V := by rw [finset_basis, basis.range_reindex, basis.range_of_vector_space] variables {K V} /-- A module over a division ring is noetherian if and only if it is finitely generated. -/ lemma iff_fg : is_noetherian K V ↔ module.finite K V := begin split, { introI h, exact ⟨⟨finset_basis_index K V, by { convert (finset_basis K V).span_eq, simp }⟩⟩ }, { rintros ⟨s, hs⟩, rw [is_noetherian.iff_dim_lt_omega, ← dim_top, ← hs], exact lt_of_le_of_lt (dim_span_le _) (lt_omega_iff_finite.2 (set.finite_mem_finset s)) } end end is_noetherian
0bf8b7b190b2f8c21e639964d07b9768476faf22
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/decAuxBug.lean
25e3b814858f9866d38ad901d5f84d90e1bb1242
[ "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
98
lean
section variable (A : Type) (B : A → Prop) def foo := PSigma B def bar : Type := PSigma B end
b59505dc7bc43b90053589d6cebecf555dbf8ca6
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/typeclass_metas_internal_goals4.lean
361d8a5e0f997e1bfdaa8c09420341cd75e568dc
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
649
lean
class Foo (α β γ : Type) := (u:Unit) class Bar (α β γ : Type) := (u:Unit) class Top := (u:Unit) instance FooBarToTop (α β γ : Type) [Foo α β γ] [Bar α β γ] : Top := {u:=()} instance Foo₁ (β γ : Type) : Foo Unit β γ := {u:=()} instance Foo₂ (α γ : Type) : Foo α Unit γ := {u:=()} instance Foo₃ (α β : Type) : Foo α β Unit := {u:=()} instance Foo₁₂ (γ : Type) : Foo Unit Nat γ := {u:=()} instance Foo₂₃ (α : Type) : Foo α Unit Nat := {u:=()} instance Foo₃₁ (β : Type) : Foo Nat β Unit := {u:=()} instance Bar0 : Bar Unit Int (List Int) := {u:=()} new_frontend set_option pp.all true #synth Top
8189b48440d7c88d8cc5ba921b6aac4f4c952987
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/category_theory/abelian/basic.lean
7b3c32128285ae2b8f828e288171f15fb1e592a6
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,261
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.constructions.pullbacks import category_theory.limits.shapes.biproducts import category_theory.limits.shapes.images import category_theory.abelian.non_preadditive /-! # Abelian categories This file contains the definition and basic properties of abelian categories. There are many definitions of abelian category. Our definition is as follows: A category is called abelian if it is preadditive, has a finite products, kernels and cokernels, and if every monomorphism and epimorphism is normal. It should be noted that if we also assume coproducts, then preadditivity is actually a consequence of the other properties, as we show in `non_preadditive_abelian.lean`. However, this fact is of little practical relevance, since essentially all interesting abelian categories come with a preadditive structure. In this way, by requiring preadditivity, we allow the user to pass in the preadditive structure the specific category they are working with has natively. ## Main definitions * `abelian` is the type class indicating that a category is abelian. It extends `preadditive`. * `abelian.image f` is `kernel (cokernel.π f)`, and * `abelian.coimage f` is `cokernel (kernel.ι f)`. ## Main results * In an abelian category, mono + epi = iso. * If `f : X ⟶ Y`, then the map `factor_thru_image f : X ⟶ image f` is an epimorphism, and the map `factor_thru_coimage f : coimage f ⟶ Y` is a monomorphism. * Factoring through the image and coimage is a strong epi-mono factorisation. This means that * every abelian category has images. We instantiated this in such a way that `abelian.image f` is definitionally equal to `limits.image f`, and * there is a canonical isomorphism `coimage_iso_image : coimage f ≅ image f` such that `coimage.π f ≫ (coimage_iso_image f).hom ≫ image.ι f = f`. The lemma stating this is called `full_image_factorisation`. * Every epimorphism is a cokernel of its kernel. Every monomorphism is a kernel of its cokernel. * The pullback of an epimorphism is an epimorphism. The pushout of a monomorphism is a monomorphism. (This is not to be confused with the fact that the pullback of a monomorphism is a monomorphism, which is true in any category). ## Implementation notes The typeclass `abelian` does not extend `non_preadditive_abelian`, to avoid having to deal with comparing the two `has_zero_morphisms` instances (one from `preadditive` in `abelian`, and the other a field of `non_preadditive_abelian`). As a consequence, at the beginning of this file we trivially build a `non_preadditive_abelian` instance from an `abelian` instance, and use this to restate a number of theorems, in each case just reusing the proof from `non_preadditive_abelian.lean`. We don't show this yet, but abelian categories are finitely complete and finitely cocomplete. However, the limits we can construct at this level of generality will most likely be less nice than the ones that can be created in specific applications. For this reason, we adopt the following convention: * If the statement of a theorem involves limits, the existence of these limits should be made an explicit typeclass parameter. * If a limit only appears in a proof, but not in the statement of a theorem, the limit should not be a typeclass parameter, but instead be created using `abelian.has_pullbacks` or a similar definition. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] * [P. Aluffi, *Algebra: Chaper 0*][aluffi2016] -/ noncomputable theory open category_theory open category_theory.preadditive open category_theory.limits universes v u namespace category_theory variables {C : Type u} [category.{v} C] variables (C) /-- A (preadditive) category `C` is called abelian if it has all finite products, all kernels and cokernels, and if every monomorphism is the kernel of some morphism and every epimorphism is the cokernel of some morphism. (This definition implies the existence of zero objects: finite products give a terminal object, and in a preadditive category any terminal object is a zero object.) -/ class abelian extends preadditive C := [has_finite_products : has_finite_products C] [has_kernels : has_kernels C] [has_cokernels : has_cokernels C] (normal_mono : Π {X Y : C} (f : X ⟶ Y) [mono f], normal_mono f) (normal_epi : Π {X Y : C} (f : X ⟶ Y) [epi f], normal_epi f) attribute [instance, priority 100] abelian.has_finite_products attribute [instance, priority 100] abelian.has_kernels abelian.has_cokernels end category_theory open category_theory namespace category_theory.abelian variables {C : Type u} [category.{v} C] [abelian C] /-- An abelian category has finite biproducts. -/ lemma has_finite_biproducts : has_finite_biproducts C := limits.has_finite_biproducts.of_has_finite_products section to_non_preadditive_abelian local attribute [instance] has_finite_biproducts /-- Every abelian category is, in particular, `non_preadditive_abelian`. -/ def non_preadditive_abelian : non_preadditive_abelian C := { ..‹abelian C› } end to_non_preadditive_abelian section strong local attribute [instance] abelian.normal_epi /-- In an abelian category, every epimorphism is strong. -/ lemma strong_epi_of_epi {P Q : C} (f : P ⟶ Q) [epi f] : strong_epi f := by apply_instance end strong section mono_epi_iso variables {X Y : C} (f : X ⟶ Y) local attribute [instance] strong_epi_of_epi /-- In an abelian category, a monomorphism which is also an epimorphism is an isomorphism. -/ lemma is_iso_of_mono_of_epi [mono f] [epi f] : is_iso f := is_iso_of_mono_of_strong_epi _ end mono_epi_iso section factor local attribute [instance] non_preadditive_abelian variables {P Q : C} (f : P ⟶ Q) section lemma mono_of_zero_kernel (R : C) (l : is_limit (kernel_fork.of_ι (0 : R ⟶ P) (show 0 ≫ f = 0, by simp))) : mono f := non_preadditive_abelian.mono_of_zero_kernel _ _ l lemma mono_of_kernel_ι_eq_zero (h : kernel.ι f = 0) : mono f := mono_of_kernel_zero h lemma epi_of_zero_cokernel (R : C) (l : is_colimit (cokernel_cofork.of_π (0 : Q ⟶ R) (show f ≫ 0 = 0, by simp))) : epi f := non_preadditive_abelian.epi_of_zero_cokernel _ _ l lemma epi_of_cokernel_π_eq_zero (h : cokernel.π f = 0) : epi f := begin apply epi_of_zero_cokernel _ (cokernel f), simp_rw ←h, exact is_colimit.of_iso_colimit (colimit.is_colimit (parallel_pair f 0)) (iso_of_π _) end end namespace images /-- The kernel of the cokernel of `f` is called the image of `f`. -/ protected abbreviation image : C := kernel (cokernel.π f) /-- The inclusion of the image into the codomain. -/ protected abbreviation image.ι : images.image f ⟶ Q := kernel.ι (cokernel.π f) /-- There is a canonical epimorphism `p : P ⟶ image f` for every `f`. -/ protected abbreviation factor_thru_image : P ⟶ images.image f := kernel.lift (cokernel.π f) f $ cokernel.condition f /-- `f` factors through its image via the canonical morphism `p`. -/ @[simp, reassoc] protected lemma image.fac : images.factor_thru_image f ≫ image.ι f = f := kernel.lift_ι _ _ _ /-- The map `p : P ⟶ image f` is an epimorphism -/ instance : epi (images.factor_thru_image f) := show epi (non_preadditive_abelian.factor_thru_image f), by apply_instance section variables {f} lemma image_ι_comp_eq_zero {R : C} {g : Q ⟶ R} (h : f ≫ g = 0) : images.image.ι f ≫ g = 0 := zero_of_epi_comp (images.factor_thru_image f) $ by simp [h] end instance mono_factor_thru_image [mono f] : mono (images.factor_thru_image f) := mono_of_mono_fac $ image.fac f instance is_iso_factor_thru_image [mono f] : is_iso (images.factor_thru_image f) := is_iso_of_mono_of_epi _ /-- Factoring through the image is a strong epi-mono factorisation. -/ @[simps] def image_strong_epi_mono_factorisation : strong_epi_mono_factorisation f := { I := images.image f, m := image.ι f, m_mono := by apply_instance, e := images.factor_thru_image f, e_strong_epi := strong_epi_of_epi _ } end images namespace coimages /-- The cokernel of the kernel of `f` is called the coimage of `f`. -/ protected abbreviation coimage : C := cokernel (kernel.ι f) /-- The projection onto the coimage. -/ protected abbreviation coimage.π : P ⟶ coimages.coimage f := cokernel.π (kernel.ι f) /-- There is a canonical monomorphism `i : coimage f ⟶ Q`. -/ protected abbreviation factor_thru_coimage : coimages.coimage f ⟶ Q := cokernel.desc (kernel.ι f) f $ kernel.condition f /-- `f` factors through its coimage via the canonical morphism `p`. -/ protected lemma coimage.fac : coimage.π f ≫ coimages.factor_thru_coimage f = f := cokernel.π_desc _ _ _ /-- The canonical morphism `i : coimage f ⟶ Q` is a monomorphism -/ instance : mono (coimages.factor_thru_coimage f) := show mono (non_preadditive_abelian.factor_thru_coimage f), by apply_instance instance epi_factor_thru_coimage [epi f] : epi (coimages.factor_thru_coimage f) := epi_of_epi_fac $ coimage.fac f instance is_iso_factor_thru_coimage [epi f] : is_iso (coimages.factor_thru_coimage f) := is_iso_of_mono_of_epi _ /-- Factoring through the coimage is a strong epi-mono factorisation. -/ @[simps] def coimage_strong_epi_mono_factorisation : strong_epi_mono_factorisation f := { I := coimages.coimage f, m := coimages.factor_thru_coimage f, m_mono := by apply_instance, e := coimage.π f, e_strong_epi := strong_epi_of_epi _ } end coimages end factor section has_strong_epi_mono_factorisations /-- An abelian category has strong epi-mono factorisations. -/ @[priority 100] instance : has_strong_epi_mono_factorisations C := has_strong_epi_mono_factorisations.mk $ λ X Y f, images.image_strong_epi_mono_factorisation f /- In particular, this means that it has well-behaved images. -/ example : has_images C := by apply_instance example : has_image_maps C := by apply_instance end has_strong_epi_mono_factorisations section images variables {X Y : C} (f : X ⟶ Y) /-- There is a canonical isomorphism between the coimage and the image of a morphism. -/ abbreviation coimage_iso_image : coimages.coimage f ≅ images.image f := is_image.iso_ext (coimages.coimage_strong_epi_mono_factorisation f).to_mono_is_image (images.image_strong_epi_mono_factorisation f).to_mono_is_image /-- There is a canonical isomorphism between the abelian image and the categorical image of a morphism. -/ abbreviation image_iso_image : images.image f ≅ image f := is_image.iso_ext (images.image_strong_epi_mono_factorisation f).to_mono_is_image (image.is_image f) /-- There is a canonical isomorphism between the abelian coimage and the categorical image of a morphism. -/ abbreviation coimage_iso_image' : coimages.coimage f ≅ image f := is_image.iso_ext (coimages.coimage_strong_epi_mono_factorisation f).to_mono_is_image (image.is_image f) lemma full_image_factorisation : coimages.coimage.π f ≫ (coimage_iso_image f).hom ≫ images.image.ι f = f := by rw [limits.is_image.iso_ext_hom, ←images.image_strong_epi_mono_factorisation_to_mono_factorisation_m, is_image.lift_fac, coimages.coimage_strong_epi_mono_factorisation_to_mono_factorisation_m, coimages.coimage.fac] end images section cokernel_of_kernel variables {X Y : C} {f : X ⟶ Y} local attribute [instance] non_preadditive_abelian /-- In an abelian category, an epi is the cokernel of its kernel. More precisely: If `f` is an epimorphism and `s` is some limit kernel cone on `f`, then `f` is a cokernel of `fork.ι s`. -/ def epi_is_cokernel_of_kernel [epi f] (s : fork f 0) (h : is_limit s) : is_colimit (cokernel_cofork.of_π f (kernel_fork.condition s)) := non_preadditive_abelian.epi_is_cokernel_of_kernel s h /-- In an abelian category, a mono is the kernel of its cokernel. More precisely: If `f` is a monomorphism and `s` is some colimit cokernel cocone on `f`, then `f` is a kernel of `cofork.π s`. -/ def mono_is_kernel_of_cokernel [mono f] (s : cofork f 0) (h : is_colimit s) : is_limit (kernel_fork.of_ι f (cokernel_cofork.condition s)) := non_preadditive_abelian.mono_is_kernel_of_cokernel s h end cokernel_of_kernel section local attribute [instance] preadditive.has_equalizers_of_has_kernels /-- Any abelian category has pullbacks -/ lemma has_pullbacks : has_pullbacks C := has_pullbacks_of_has_binary_products_of_has_equalizers C end section local attribute [instance] preadditive.has_coequalizers_of_has_cokernels local attribute [instance] has_binary_biproducts.of_has_binary_products /-- Any abelian category has pushouts -/ lemma has_pushouts : has_pushouts C := has_pushouts_of_has_binary_coproducts_of_has_coequalizers C end namespace pullback_to_biproduct_is_kernel variables [limits.has_pullbacks C] {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) local attribute [instance] has_binary_biproducts.of_has_binary_products /-! This section contains a slightly technical result about pullbacks and biproducts. We will need it in the proof that the pullback of an epimorphism is an epimorpism. -/ /-- The canonical map `pullback f g ⟶ X ⊞ Y` -/ abbreviation pullback_to_biproduct : pullback f g ⟶ X ⊞ Y := biprod.lift pullback.fst pullback.snd /-- The canonical map `pullback f g ⟶ X ⊞ Y` induces a kernel cone on the map `biproduct X Y ⟶ Z` induced by `f` and `g`. A slightly more intuitive way to think of this may be that it induces an equalizer fork on the maps induced by `(f, 0)` and `(0, g)`. -/ abbreviation pullback_to_biproduct_fork : kernel_fork (biprod.desc f (-g)) := kernel_fork.of_ι (pullback_to_biproduct f g) $ by rw [biprod.lift_desc, comp_neg, pullback.condition, add_right_neg] local attribute [irreducible] has_limit_cospan_of_has_limit_pair_of_has_limit_parallel_pair /-- The canonical map `pullback f g ⟶ X ⊞ Y` is a kernel of the map induced by `(f, -g)`. -/ def is_limit_pullback_to_biproduct : is_limit (pullback_to_biproduct_fork f g) := fork.is_limit.mk _ (λ s, pullback.lift (fork.ι s ≫ biprod.fst) (fork.ι s ≫ biprod.snd) $ sub_eq_zero.1 $ by rw [category.assoc, category.assoc, ←comp_sub, sub_eq_add_neg, ←comp_neg, ←biprod.desc_eq, kernel_fork.condition s]) (λ s, begin ext; rw [fork.ι_of_ι, category.assoc], { rw [biprod.lift_fst, pullback.lift_fst] }, { rw [biprod.lift_snd, pullback.lift_snd] } end) (λ s m h, by ext; simp [fork.ι_eq_app_zero, ←h walking_parallel_pair.zero]) end pullback_to_biproduct_is_kernel namespace biproduct_to_pushout_is_cokernel variables [limits.has_pushouts C] {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) local attribute [instance] has_binary_biproducts.of_has_binary_products /-- The canonical map `Y ⊞ Z ⟶ pushout f g` -/ abbreviation biproduct_to_pushout : Y ⊞ Z ⟶ pushout f g := biprod.desc pushout.inl pushout.inr /-- The canonical map `Y ⊞ Z ⟶ pushout f g` induces a cokernel cofork on the map `X ⟶ Y ⊞ Z` induced by `f` and `-g`. -/ abbreviation biproduct_to_pushout_cofork : cokernel_cofork (biprod.lift f (-g)) := cokernel_cofork.of_π (biproduct_to_pushout f g) $ by rw [biprod.lift_desc, neg_comp, pushout.condition, add_right_neg] /-- The cofork induced by the canonical map `Y ⊞ Z ⟶ pushout f g` is in fact a colimit cokernel cofork. -/ def is_colimit_biproduct_to_pushout : is_colimit (biproduct_to_pushout_cofork f g) := cofork.is_colimit.mk _ (λ s, pushout.desc (biprod.inl ≫ cofork.π s) (biprod.inr ≫ cofork.π s) $ sub_eq_zero.1 $ by rw [←category.assoc, ←category.assoc, ←sub_comp, sub_eq_add_neg, ←neg_comp, ←biprod.lift_eq, cofork.condition s, zero_comp]) (λ s, by ext; simp) (λ s m h, by ext; simp [cofork.π_eq_app_one, ←h walking_parallel_pair.one] ) end biproduct_to_pushout_is_cokernel section epi_pullback variables [limits.has_pullbacks C] {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) local attribute [instance] has_binary_biproducts.of_has_binary_products /-- In an abelian category, the pullback of an epimorphism is an epimorphism. Proof from [aluffi2016, IX.2.3], cf. [borceux-vol2, 1.7.6] -/ instance epi_pullback_of_epi_f [epi f] : epi (pullback.snd : pullback f g ⟶ Y) := -- It will suffice to consider some morphism e : Y ⟶ R such that -- pullback.snd ≫ e = 0 and show that e = 0. epi_of_cancel_zero _ $ λ R e h, begin -- Consider the morphism u := (0, e) : X ⊞ Y⟶ R. let u := biprod.desc (0 : X ⟶ R) e, -- The composite pullback f g ⟶ X ⊞ Y ⟶ R is zero by assumption. have hu : pullback_to_biproduct_is_kernel.pullback_to_biproduct f g ≫ u = 0 := by simpa, -- pullback_to_biproduct f g is a kernel of (f, -g), so (f, -g) is a -- cokernel of pullback_to_biproduct f g have := epi_is_cokernel_of_kernel _ (pullback_to_biproduct_is_kernel.is_limit_pullback_to_biproduct f g), -- We use this fact to obtain a factorization of u through (f, -g) via some d : Z ⟶ R. obtain ⟨d, hd⟩ := cokernel_cofork.is_colimit.desc' this u hu, change Z ⟶ R at d, change biprod.desc f (-g) ≫ d = u at hd, -- But then f ≫ d = 0: have : f ≫ d = 0, calc f ≫ d = (biprod.inl ≫ biprod.desc f (-g)) ≫ d : by rw biprod.inl_desc ... = biprod.inl ≫ u : by rw [category.assoc, hd] ... = 0 : biprod.inl_desc _ _, -- But f is an epimorphism, so d = 0... have : d = 0 := (cancel_epi f).1 (by simpa), -- ...or, in other words, e = 0. calc e = biprod.inr ≫ u : by rw biprod.inr_desc ... = biprod.inr ≫ biprod.desc f (-g) ≫ d : by rw ←hd ... = biprod.inr ≫ biprod.desc f (-g) ≫ 0 : by rw this ... = (biprod.inr ≫ biprod.desc f (-g)) ≫ 0 : by rw ←category.assoc ... = 0 : has_zero_morphisms.comp_zero _ _ end /-- In an abelian category, the pullback of an epimorphism is an epimorphism. -/ instance epi_pullback_of_epi_g [epi g] : epi (pullback.fst : pullback f g ⟶ X) := -- It will suffice to consider some morphism e : X ⟶ R such that -- pullback.fst ≫ e = 0 and show that e = 0. epi_of_cancel_zero _ $ λ R e h, begin -- Consider the morphism u := (e, 0) : X ⊞ Y ⟶ R. let u := biprod.desc e (0 : Y ⟶ R), -- The composite pullback f g ⟶ X ⊞ Y ⟶ R is zero by assumption. have hu : pullback_to_biproduct_is_kernel.pullback_to_biproduct f g ≫ u = 0 := by simpa, -- pullback_to_biproduct f g is a kernel of (f, -g), so (f, -g) is a -- cokernel of pullback_to_biproduct f g have := epi_is_cokernel_of_kernel _ (pullback_to_biproduct_is_kernel.is_limit_pullback_to_biproduct f g), -- We use this fact to obtain a factorization of u through (f, -g) via some d : Z ⟶ R. obtain ⟨d, hd⟩ := cokernel_cofork.is_colimit.desc' this u hu, change Z ⟶ R at d, change biprod.desc f (-g) ≫ d = u at hd, -- But then (-g) ≫ d = 0: have : (-g) ≫ d = 0, calc (-g) ≫ d = (biprod.inr ≫ biprod.desc f (-g)) ≫ d : by rw biprod.inr_desc ... = biprod.inr ≫ u : by rw [category.assoc, hd] ... = 0 : biprod.inr_desc _ _, -- But g is an epimorphism, thus so is -g, so d = 0... have : d = 0 := (cancel_epi (-g)).1 (by simpa), -- ...or, in other words, e = 0. calc e = biprod.inl ≫ u : by rw biprod.inl_desc ... = biprod.inl ≫ biprod.desc f (-g) ≫ d : by rw ←hd ... = biprod.inl ≫ biprod.desc f (-g) ≫ 0 : by rw this ... = (biprod.inl ≫ biprod.desc f (-g)) ≫ 0 : by rw ←category.assoc ... = 0 : has_zero_morphisms.comp_zero _ _ end end epi_pullback section mono_pushout variables [limits.has_pushouts C] {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) local attribute [instance] has_binary_biproducts.of_has_binary_products instance mono_pushout_of_mono_f [mono f] : mono (pushout.inr : Z ⟶ pushout f g) := mono_of_cancel_zero _ $ λ R e h, begin let u := biprod.lift (0 : R ⟶ Y) e, have hu : u ≫ biproduct_to_pushout_is_cokernel.biproduct_to_pushout f g = 0 := by simpa, have := mono_is_kernel_of_cokernel _ (biproduct_to_pushout_is_cokernel.is_colimit_biproduct_to_pushout f g), obtain ⟨d, hd⟩ := kernel_fork.is_limit.lift' this u hu, change R ⟶ X at d, change d ≫ biprod.lift f (-g) = u at hd, have : d ≫ f = 0, calc d ≫ f = d ≫ biprod.lift f (-g) ≫ biprod.fst : by rw biprod.lift_fst ... = u ≫ biprod.fst : by rw [←category.assoc, hd] ... = 0 : biprod.lift_fst _ _, have : d = 0 := (cancel_mono f).1 (by simpa), calc e = u ≫ biprod.snd : by rw biprod.lift_snd ... = (d ≫ biprod.lift f (-g)) ≫ biprod.snd : by rw ←hd ... = (0 ≫ biprod.lift f (-g)) ≫ biprod.snd : by rw this ... = 0 ≫ biprod.lift f (-g) ≫ biprod.snd : by rw category.assoc ... = 0 : zero_comp end instance mono_pushout_of_mono_g [mono g] : mono (pushout.inl : Y ⟶ pushout f g) := mono_of_cancel_zero _ $ λ R e h, begin let u := biprod.lift e (0 : R ⟶ Z), have hu : u ≫ biproduct_to_pushout_is_cokernel.biproduct_to_pushout f g = 0 := by simpa, have := mono_is_kernel_of_cokernel _ (biproduct_to_pushout_is_cokernel.is_colimit_biproduct_to_pushout f g), obtain ⟨d, hd⟩ := kernel_fork.is_limit.lift' this u hu, change R ⟶ X at d, change d ≫ biprod.lift f (-g) = u at hd, have : d ≫ (-g) = 0, calc d ≫ (-g) = d ≫ biprod.lift f (-g) ≫ biprod.snd : by rw biprod.lift_snd ... = u ≫ biprod.snd : by rw [←category.assoc, hd] ... = 0 : biprod.lift_snd _ _, have : d = 0 := (cancel_mono (-g)).1 (by simpa), calc e = u ≫ biprod.fst : by rw biprod.lift_fst ... = (d ≫ biprod.lift f (-g)) ≫ biprod.fst : by rw ←hd ... = (0 ≫ biprod.lift f (-g)) ≫ biprod.fst : by rw this ... = 0 ≫ biprod.lift f (-g) ≫ biprod.fst : by rw category.assoc ... = 0 : zero_comp end end mono_pushout end category_theory.abelian namespace category_theory.non_preadditive_abelian variables (C : Type u) [category.{v} C] [non_preadditive_abelian C] /-- Every non_preadditive_abelian category can be promoted to an abelian category. -/ def abelian : abelian C := { has_finite_products := by apply_instance, /- We need the `convert`s here because the instances we have are slightly different from the instances we need: `has_kernels` depends on an instance of `has_zero_morphisms`. In the case of `non_preadditive_abelian`, this instance is an explicit argument. However, in the case of `abelian`, the `has_zero_morphisms` instance is derived from `preadditive`. So we need to transform an instance of "has kernels with non_preadditive_abelian.has_zero_morphisms" to an instance of "has kernels with non_preadditive_abelian.preadditive.has_zero_morphisms". Luckily, we have a `subsingleton` instance for `has_zero_morphisms`, so `convert` can immediately close the goal it creates for the two instances of `has_zero_morphisms`, and the proof is complete. -/ has_kernels := by convert (by apply_instance : limits.has_kernels C), has_cokernels := by convert (by apply_instance : limits.has_cokernels C), normal_mono := by { introsI, convert normal_mono f }, normal_epi := by { introsI, convert normal_epi f }, ..non_preadditive_abelian.preadditive } end category_theory.non_preadditive_abelian
82dd1055be4a608fa478dda59c44c63ddc39372a
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-numbertheory-102.lean
23bdf862896774664f0114940f10f13844c30d80
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
241
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import data.nat.basic import data.real.basic example : (2 ^ 8) % 5 = 1 := begin norm_num, end
6671e297784349038799e41edbc76318f30414d9
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/order/ideal.lean
116992c97c4d913d6c129a4c576612fedd6a51e1
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,955
lean
/- Copyright (c) 2020 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn -/ import order.basic import data.equiv.encodable.basic import order.atoms /-! # Order ideals, cofinal sets, and the Rasiowa–Sikorski lemma ## Main definitions Throughout this file, `P` is at least a preorder, but some sections require more structure, such as a bottom element, a top element, or a join-semilattice structure. - `order.ideal P`: the type of nonempty, upward directed, and downward closed subsets of `P`. Dual to the notion of a filter on a preorder. - `order.is_ideal P`: a predicate for when a `set P` is an ideal. - `order.ideal.principal p`: the principal ideal generated by `p : P`. - `order.ideal.is_proper P`: a predicate for proper ideals. Dual to the notion of a proper filter. - `order.ideal.is_maximal`: a predicate for maximal ideals. Dual to the notion of an ultrafilter. - `order.cofinal P`: the type of subsets of `P` containing arbitrarily large elements. Dual to the notion of 'dense set' used in forcing. - `order.ideal_of_cofinals p 𝒟`, where `p : P`, and `𝒟` is a countable family of cofinal subsets of P: an ideal in `P` which contains `p` and intersects every set in `𝒟`. (This a form of the Rasiowa–Sikorski lemma.) ## References - <https://en.wikipedia.org/wiki/Ideal_(order_theory)> - <https://en.wikipedia.org/wiki/Cofinal_(mathematics)> - <https://en.wikipedia.org/wiki/Rasiowa%E2%80%93Sikorski_lemma> Note that for the Rasiowa–Sikorski lemma, Wikipedia uses the opposite ordering on `P`, in line with most presentations of forcing. ## Tags ideal, cofinal, dense, countable, generic -/ namespace order variables {P : Type*} /-- An ideal on a preorder `P` is a subset of `P` that is - nonempty - upward directed (any pair of elements in the ideal has an upper bound in the ideal) - downward closed (any element less than an element of the ideal is in the ideal). -/ structure ideal (P) [preorder P] := (carrier : set P) (nonempty : carrier.nonempty) (directed : directed_on (≤) carrier) (mem_of_le : ∀ {x y : P}, x ≤ y → y ∈ carrier → x ∈ carrier) /-- A subset of a preorder `P` is an ideal if it is - nonempty - upward directed (any pair of elements in the ideal has an upper bound in the ideal) - downward closed (any element less than an element of the ideal is in the ideal). -/ @[mk_iff] structure is_ideal {P} [preorder P] (I : set P) : Prop := (nonempty : I.nonempty) (directed : directed_on (≤) I) (mem_of_le : ∀ {x y : P}, x ≤ y → y ∈ I → x ∈ I) /-- Create an element of type `order.ideal` from a set satisfying the predicate `order.is_ideal`. -/ def is_ideal.to_ideal [preorder P] {I : set P} (h : is_ideal I) : ideal P := ⟨I, h.1, h.2, h.3⟩ namespace ideal section preorder variables [preorder P] {x : P} {I J : ideal P} /-- The smallest ideal containing a given element. -/ def principal (p : P) : ideal P := { carrier := { x | x ≤ p }, nonempty := ⟨p, le_refl _⟩, directed := λ x hx y hy, ⟨p, le_refl _, hx, hy⟩, mem_of_le := λ x y hxy hy, le_trans hxy hy, } instance [inhabited P] : inhabited (ideal P) := ⟨ideal.principal $ default P⟩ /-- An ideal of `P` can be viewed as a subset of `P`. -/ instance : has_coe (ideal P) (set P) := ⟨carrier⟩ /-- For the notation `x ∈ I`. -/ instance : has_mem P (ideal P) := ⟨λ x I, x ∈ (I : set P)⟩ /-- Two ideals are equal when their underlying sets are equal. -/ @[ext] lemma ext : ∀ (I J : ideal P), (I : set P) = J → I = J | ⟨_, _, _, _⟩ ⟨_, _, _, _⟩ rfl := rfl @[simp, norm_cast] lemma ext_set_eq {I J : ideal P} : (I : set P) = J ↔ I = J := ⟨by ext, congr_arg _⟩ lemma ext'_iff {I J : ideal P} : I = J ↔ (I : set P) = J := ext_set_eq.symm lemma is_ideal (I : ideal P) : is_ideal (I : set P) := ⟨I.2, I.3, I.4⟩ /-- The partial ordering by subset inclusion, inherited from `set P`. -/ instance : partial_order (ideal P) := partial_order.lift coe ext @[trans] lemma mem_of_mem_of_le : x ∈ I → I ≤ J → x ∈ J := @set.mem_of_mem_of_subset P x I J @[simp] lemma principal_le_iff : principal x ≤ I ↔ x ∈ I := ⟨λ (h : ∀ {y}, y ≤ x → y ∈ I), h (le_refl x), λ h_mem y (h_le : y ≤ x), I.mem_of_le h_le h_mem⟩ /-- A proper ideal is one that is not the whole set. Note that the whole set might not be an ideal. -/ @[mk_iff] class is_proper (I : ideal P) : Prop := (ne_univ : (I : set P) ≠ set.univ) lemma is_proper_of_not_mem {I : ideal P} {p : P} (nmem : p ∉ I) : is_proper I := ⟨λ hp, begin change p ∉ ↑I at nmem, rw hp at nmem, exact nmem (set.mem_univ p), end⟩ /-- An ideal is maximal if it is maximal in the collection of proper ideals. Note that we cannot use the `is_coatom` class because `P` might not have a `top` element. -/ @[mk_iff] class is_maximal (I : ideal P) extends is_proper I : Prop := (maximal_proper : ∀ J : ideal P, I < J → J.carrier = set.univ) end preorder section order_bot variables [order_bot P] {I : ideal P} /-- A specific witness of `I.nonempty` when `P` has a bottom element. -/ @[simp] lemma bot_mem : ⊥ ∈ I := I.mem_of_le bot_le I.nonempty.some_mem /-- There is a bottom ideal when `P` has a bottom element. -/ instance : order_bot (ideal P) := { bot := principal ⊥, bot_le := by simp, .. ideal.partial_order } end order_bot section order_top variables [order_top P] /-- There is a top ideal when `P` has a top element. -/ instance : order_top (ideal P) := { top := principal ⊤, le_top := λ I x h, le_top, .. ideal.partial_order } @[simp] lemma top_carrier : (⊤ : ideal P).carrier = set.univ := set.univ_subset_iff.1 (λ p _, le_top) @[simp] lemma top_coe : ((⊤ : ideal P) : set P) = set.univ := top_carrier lemma top_of_mem_top {I : ideal P} (mem_top : ⊤ ∈ I) : I = ⊤ := begin ext, change x ∈ I.carrier ↔ x ∈ (⊤ : ideal P).carrier, split, { simp [top_carrier] }, { exact λ _, I.mem_of_le le_top mem_top } end lemma is_proper_of_ne_top {I : ideal P} (ne_top : I ≠ ⊤) : is_proper I := is_proper_of_not_mem (λ h, ne_top (top_of_mem_top h)) lemma is_proper.ne_top {I : ideal P} (hI : is_proper I) : I ≠ ⊤ := begin intro h, rw [ext'_iff, top_coe] at h, apply hI.ne_univ, assumption, end lemma _root_.is_coatom.is_proper {I : ideal P} (hI : is_coatom I) : is_proper I := is_proper_of_ne_top hI.1 lemma is_proper_iff_ne_top {I : ideal P} : is_proper I ↔ I ≠ ⊤ := ⟨λ h, h.ne_top, λ h, is_proper_of_ne_top h⟩ lemma is_maximal.is_coatom {I : ideal P} (h : is_maximal I) : is_coatom I := ⟨is_maximal.to_is_proper.ne_top, λ J _, by { rw [ext'_iff, top_coe], exact is_maximal.maximal_proper J ‹_› }⟩ lemma is_maximal.is_coatom' {I : ideal P} [is_maximal I] : is_coatom I := is_maximal.is_coatom ‹_› lemma _root_.is_coatom.is_maximal {I : ideal P} (hI : is_coatom I) : is_maximal I := { maximal_proper := λ _ _, by simp [hI.2 _ ‹_›], ..is_coatom.is_proper ‹_› } lemma is_maximal_iff_is_coatom {I : ideal P} : is_maximal I ↔ is_coatom I := ⟨λ h, h.is_coatom, λ h, h.is_maximal⟩ end order_top section semilattice_sup variables [semilattice_sup P] {x y : P} {I : ideal P} /-- A specific witness of `I.directed` when `P` has joins. -/ lemma sup_mem (x y ∈ I) : x ⊔ y ∈ I := let ⟨z, h_mem, hx, hy⟩ := I.directed x ‹_› y ‹_› in I.mem_of_le (sup_le hx hy) h_mem @[simp] lemma sup_mem_iff : x ⊔ y ∈ I ↔ x ∈ I ∧ y ∈ I := ⟨λ h, ⟨I.mem_of_le le_sup_left h, I.mem_of_le le_sup_right h⟩, λ h, sup_mem x y h.left h.right⟩ end semilattice_sup section semilattice_sup_bot variables [semilattice_sup_bot P] (I J K : ideal P) /-- The intersection of two ideals is an ideal, when `P` has joins and a bottom. -/ def inf (I J : ideal P) : ideal P := { carrier := I ∩ J, nonempty := ⟨⊥, bot_mem, bot_mem⟩, directed := λ x ⟨_, _⟩ y ⟨_, _⟩, ⟨x ⊔ y, ⟨sup_mem x y ‹_› ‹_›, sup_mem x y ‹_› ‹_›⟩, by simp⟩, mem_of_le := λ x y h ⟨_, _⟩, ⟨mem_of_le I h ‹_›, mem_of_le J h ‹_›⟩ } /-- There is a smallest ideal containing two ideals, when `P` has joins and a bottom. -/ def sup (I J : ideal P) : ideal P := { carrier := {x | ∃ (i ∈ I) (j ∈ J), x ≤ i ⊔ j}, nonempty := ⟨⊥, ⊥, bot_mem, ⊥, bot_mem, bot_le⟩, directed := λ x ⟨xi, _, xj, _, _⟩ y ⟨yi, _, yj, _, _⟩, ⟨x ⊔ y, ⟨xi ⊔ yi, sup_mem xi yi ‹_› ‹_›, xj ⊔ yj, sup_mem xj yj ‹_› ‹_›, sup_le (calc x ≤ xi ⊔ xj : ‹_› ... ≤ (xi ⊔ yi) ⊔ (xj ⊔ yj) : sup_le_sup le_sup_left le_sup_left) (calc y ≤ yi ⊔ yj : ‹_› ... ≤ (xi ⊔ yi) ⊔ (xj ⊔ yj) : sup_le_sup le_sup_right le_sup_right)⟩, le_sup_left, le_sup_right⟩, mem_of_le := λ x y _ ⟨yi, _, yj, _, _⟩, ⟨yi, ‹_›, yj, ‹_›, le_trans ‹x ≤ y› ‹_›⟩ } lemma sup_le : I ≤ K → J ≤ K → sup I J ≤ K := λ hIK hJK x ⟨i, hiI, j, hjJ, hxij⟩, K.mem_of_le hxij $ sup_mem i j (mem_of_mem_of_le hiI hIK) (mem_of_mem_of_le hjJ hJK) instance : lattice (ideal P) := { sup := sup, le_sup_left := λ I J (i ∈ I), ⟨i, ‹_›, ⊥, bot_mem, by simp only [sup_bot_eq]⟩, le_sup_right := λ I J (j ∈ J), ⟨⊥, bot_mem, j, ‹_›, by simp only [bot_sup_eq]⟩, sup_le := sup_le, inf := inf, inf_le_left := λ I J, set.inter_subset_left I J, inf_le_right := λ I J, set.inter_subset_right I J, le_inf := λ I J K, set.subset_inter, .. ideal.partial_order } @[simp] lemma mem_inf {x : P} : x ∈ I ⊓ J ↔ x ∈ I ∧ x ∈ J := iff_of_eq rfl @[simp] lemma mem_sup {x : P} : x ∈ I ⊔ J ↔ ∃ (i ∈ I) (j ∈ J), x ≤ i ⊔ j := iff_of_eq rfl end semilattice_sup_bot end ideal /-- For a preorder `P`, `cofinal P` is the type of subsets of `P` containing arbitrarily large elements. They are the dense sets in the topology whose open sets are terminal segments. -/ structure cofinal (P) [preorder P] := (carrier : set P) (mem_gt : ∀ x : P, ∃ y ∈ carrier, x ≤ y) namespace cofinal variables [preorder P] instance : inhabited (cofinal P) := ⟨{ carrier := set.univ, mem_gt := λ x, ⟨x, trivial, le_refl _⟩ }⟩ instance : has_mem P (cofinal P) := ⟨λ x D, x ∈ D.carrier⟩ variables (D : cofinal P) (x : P) /-- A (noncomputable) element of a cofinal set lying above a given element. -/ noncomputable def above : P := classical.some $ D.mem_gt x lemma above_mem : D.above x ∈ D := exists.elim (classical.some_spec $ D.mem_gt x) $ λ a _, a lemma le_above : x ≤ D.above x := exists.elim (classical.some_spec $ D.mem_gt x) $ λ _ b, b end cofinal section ideal_of_cofinals variables [preorder P] (p : P) {ι : Type*} [encodable ι] (𝒟 : ι → cofinal P) /-- Given a starting point, and a countable family of cofinal sets, this is an increasing sequence that intersects each cofinal set. -/ noncomputable def sequence_of_cofinals : ℕ → P | 0 := p | (n+1) := match encodable.decode ι n with | none := sequence_of_cofinals n | some i := (𝒟 i).above (sequence_of_cofinals n) end lemma sequence_of_cofinals.monotone : monotone (sequence_of_cofinals p 𝒟) := by { apply monotone_of_monotone_nat, intros n, dunfold sequence_of_cofinals, cases encodable.decode ι n, { refl }, { apply cofinal.le_above }, } lemma sequence_of_cofinals.encode_mem (i : ι) : sequence_of_cofinals p 𝒟 (encodable.encode i + 1) ∈ 𝒟 i := by { dunfold sequence_of_cofinals, rw encodable.encodek, apply cofinal.above_mem, } /-- Given an element `p : P` and a family `𝒟` of cofinal subsets of a preorder `P`, indexed by a countable type, `ideal_of_cofinals p 𝒟` is an ideal in `P` which - contains `p`, according to `mem_ideal_of_cofinals p 𝒟`, and - intersects every set in `𝒟`, according to `cofinal_meets_ideal_of_cofinals p 𝒟`. This proves the Rasiowa–Sikorski lemma. -/ def ideal_of_cofinals : ideal P := { carrier := { x : P | ∃ n, x ≤ sequence_of_cofinals p 𝒟 n }, nonempty := ⟨p, 0, le_refl _⟩, directed := λ x ⟨n, hn⟩ y ⟨m, hm⟩, ⟨_, ⟨max n m, le_refl _⟩, le_trans hn $ sequence_of_cofinals.monotone p 𝒟 (le_max_left _ _), le_trans hm $ sequence_of_cofinals.monotone p 𝒟 (le_max_right _ _) ⟩, mem_of_le := λ x y hxy ⟨n, hn⟩, ⟨n, le_trans hxy hn⟩, } lemma mem_ideal_of_cofinals : p ∈ ideal_of_cofinals p 𝒟 := ⟨0, le_refl _⟩ /-- `ideal_of_cofinals p 𝒟` is `𝒟`-generic. -/ lemma cofinal_meets_ideal_of_cofinals (i : ι) : ∃ x : P, x ∈ 𝒟 i ∧ x ∈ ideal_of_cofinals p 𝒟 := ⟨_, sequence_of_cofinals.encode_mem p 𝒟 i, _, le_refl _⟩ end ideal_of_cofinals end order
41bb75b992c4d34e7210525d7b396f8c4961ae7e
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/tprod.lean
58a2b3596b533061c6a490f040d7cbba3fbc15a7
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
5,913
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import data.list.nodup /-! # Finite products of types This file defines the product of types over a list. For `l : list ι` and `α : ι → Type*` we define `list.tprod α l = l.foldr (λ i β, α i × β) punit`. This type should not be used if `Π i, α i` or `Π i ∈ l, α i` can be used instead (in the last expression, we could also replace the list `l` by a set or a finset). This type is used as an intermediary between binary products and finitary products. The application of this type is finitary product measures, but it could be used in any construction/theorem that is easier to define/prove on binary products than on finitary products. * Once we have the construction on binary products (like binary product measures in `measure_theory.prod`), we can easily define a finitary version on the type `tprod l α` by iterating. Properties can also be easily extended from the binary case to the finitary case by iterating. * Then we can use the equivalence `list.tprod.pi_equiv_tprod` below (or enhanced versions of it, like a `measurable_equiv` for product measures) to get the construction on `Π i : ι, α i`, at least when assuming `[fintype ι] [encodable ι]` (using `encodable.sorted_univ`). Using `local attribute [instance] fintype.encodable` we can get rid of the argument `[encodable ι]`. ## Main definitions * We have the equivalence `tprod.pi_equiv_tprod : (Π i, α i) ≃ tprod α l` if `l` contains every element of `ι` exactly once. * The product of sets is `set.tprod : (Π i, set (α i)) → set (tprod α l)`. -/ open list function variables {ι : Type*} {α : ι → Type*} {i j : ι} {l : list ι} {f : Π i, α i} namespace list variable (α) /-- The product of a family of types over a list. -/ def tprod (l : list ι) : Type* := l.foldr (λ i β, α i × β) punit variable {α} namespace tprod open list /-- Turning a function `f : Π i, α i` into an element of the iterated product `tprod α l`. -/ protected def mk : ∀ (l : list ι) (f : Π i, α i), tprod α l | [] := λ f, punit.star | (i :: is) := λ f, (f i, mk is f) instance [∀ i, inhabited (α i)] : inhabited (tprod α l) := ⟨tprod.mk l (λ _, default)⟩ @[simp] lemma fst_mk (i : ι) (l : list ι) (f : Π i, α i) : (tprod.mk (i::l) f).1 = f i := rfl @[simp] lemma snd_mk (i : ι) (l : list ι) (f : Π i, α i) : (tprod.mk (i::l) f).2 = tprod.mk l f := rfl variables [decidable_eq ι] /-- Given an element of the iterated product `l.prod α`, take a projection into direction `i`. If `i` appears multiple times in `l`, this chooses the first component in direction `i`. -/ protected def elim : ∀ {l : list ι} (v : tprod α l) {i : ι} (hi : i ∈ l), α i | (i :: is) v j hj := if hji : j = i then by { subst hji, exact v.1 } else elim v.2 (hj.resolve_left hji) @[simp] lemma elim_self (v : tprod α (i :: l)) : v.elim (l.mem_cons_self i) = v.1 := by simp [tprod.elim] @[simp] lemma elim_of_ne (hj : j ∈ i :: l) (hji : j ≠ i) (v : tprod α (i :: l)) : v.elim hj = tprod.elim v.2 (hj.resolve_left hji) := by simp [tprod.elim, hji] @[simp] lemma elim_of_mem (hl : (i :: l).nodup) (hj : j ∈ l) (v : tprod α (i :: l)) : v.elim (mem_cons_of_mem _ hj) = tprod.elim v.2 hj := by { apply elim_of_ne, rintro rfl, exact not_mem_of_nodup_cons hl hj } lemma elim_mk : ∀ (l : list ι) (f : Π i, α i) {i : ι} (hi : i ∈ l), (tprod.mk l f).elim hi = f i | (i :: is) f j hj := begin by_cases hji : j = i, { subst hji, simp }, { rw [elim_of_ne _ hji, snd_mk, elim_mk] } end @[ext] lemma ext : ∀ {l : list ι} (hl : l.nodup) {v w : tprod α l} (hvw : ∀ i (hi : i ∈ l), v.elim hi = w.elim hi), v = w | [] hl v w hvw := punit.ext | (i :: is) hl v w hvw := begin ext, rw [← elim_self v, hvw, elim_self], refine ext (nodup_cons.mp hl).2 (λ j hj, _), rw [← elim_of_mem hl, hvw, elim_of_mem hl] end /-- A version of `tprod.elim` when `l` contains all elements. In this case we get a function into `Π i, α i`. -/ @[simp] protected def elim' (h : ∀ i, i ∈ l) (v : tprod α l) (i : ι) : α i := v.elim (h i) lemma mk_elim (hnd : l.nodup) (h : ∀ i, i ∈ l) (v : tprod α l) : tprod.mk l (v.elim' h) = v := tprod.ext hnd (λ i hi, by simp [elim_mk]) /-- Pi-types are equivalent to iterated products. -/ def pi_equiv_tprod (hnd : l.nodup) (h : ∀ i, i ∈ l) : (Π i, α i) ≃ tprod α l := ⟨tprod.mk l, tprod.elim' h, λ f, funext $ λ i, elim_mk l f (h i), mk_elim hnd h⟩ end tprod end list namespace set open list /-- A product of sets in `tprod α l`. -/ @[simp] protected def tprod : ∀ (l : list ι) (t : Π i, set (α i)), set (tprod α l) | [] t := univ | (i :: is) t := t i ×ˢ tprod is t lemma mk_preimage_tprod : ∀ (l : list ι) (t : Π i, set (α i)), tprod.mk l ⁻¹' set.tprod l t = {i | i ∈ l}.pi t | [] t := by simp [set.tprod] | (i :: l) t := begin ext f, have : f ∈ tprod.mk l ⁻¹' set.tprod l t ↔ f ∈ {x | x ∈ l}.pi t, { rw [mk_preimage_tprod l t] }, change tprod.mk l f ∈ set.tprod l t ↔ ∀ (i : ι), i ∈ l → f i ∈ t i at this, /- `simp [set.tprod, tprod.mk, this]` can close this goal but is slow. -/ rw [set.tprod, tprod.mk, mem_preimage, mem_pi, prod_mk_mem_set_prod_eq], simp_rw [mem_set_of_eq, mem_cons_iff], rw [forall_eq_or_imp, and.congr_right_iff], exact λ _, this end lemma elim_preimage_pi [decidable_eq ι] {l : list ι} (hnd : l.nodup) (h : ∀ i, i ∈ l) (t : Π i, set (α i)) : tprod.elim' h ⁻¹' pi univ t = set.tprod l t := begin have : { i | i ∈ l} = univ, { ext i, simp [h] }, rw [← this, ← mk_preimage_tprod, preimage_preimage], convert preimage_id, simp [tprod.mk_elim hnd h, id_def] end end set
cee1fcc0d62f7a576bf57e5323b853906fe264eb
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/control/fix.lean
cc257a5177d54ce21e3c5601e4f7f178835b43ce
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
3,314
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import data.nat.upto import data.stream.basic import data.pfun /-! # Fixed point This module defines a generic `fix` operator for defining recursive computations that are not necessarily well-founded or productive. An instance is defined for `roption`. ## Main definition * class `has_fix` * `roption.fix` -/ universes u v open_locale classical variables {α : Type*} {β : α → Type*} /-- `has_fix α` gives us a way to calculate the fixed point of function of type `α → α`. -/ class has_fix (α : Type*) := (fix : (α → α) → α) namespace roption open roption nat nat.upto section basic variables (f : (Π a, roption $ β a) → (Π a, roption $ β a)) /-- A series of successive, finite approximation of the fixed point of `f`, defined by `approx f n = f^[n] ⊥`. The limit of this chain is the fixed point of `f`. -/ def fix.approx : stream $ Π a, roption $ β a | 0 := ⊥ | (nat.succ i) := f (fix.approx i) /-- loop body for finding the fixed point of `f` -/ def fix_aux {p : ℕ → Prop} (i : nat.upto p) (g : Π j : nat.upto p, i < j → Π a, roption $ β a) : Π a, roption $ β a := f $ λ x : α, assert (¬p (i.val)) $ λ h : ¬ p (i.val), g (i.succ h) (nat.lt_succ_self _) x /-- The least fixed point of `f`. If `f` is a continuous function (according to complete partial orders), it satisfies the equations: 1. `fix f = f (fix f)` (is a fixed point) 2. `∀ X, f X ≤ X → fix f ≤ X` (least fixed point) -/ protected def fix (x : α) : roption $ β x := roption.assert (∃ i, (fix.approx f i x).dom) $ λ h, well_founded.fix.{1} (nat.upto.wf h) (fix_aux f) nat.upto.zero x protected lemma fix_def {x : α} (h' : ∃ i, (fix.approx f i x).dom) : roption.fix f x = fix.approx f (nat.succ $ nat.find h') x := begin let p := λ (i : ℕ), (fix.approx f i x).dom, have : p (nat.find h') := nat.find_spec h', generalize hk : nat.find h' = k, replace hk : nat.find h' = k + (@upto.zero p).val := hk, rw hk at this, revert hk, dsimp [roption.fix], rw assert_pos h', revert this, generalize : upto.zero = z, intros, suffices : ∀ x', well_founded.fix (fix._proof_1 f x h') (fix_aux f) z x' = fix.approx f (succ k) x', from this _, induction k generalizing z; intro, { rw [fix.approx,well_founded.fix_eq,fix_aux], congr, ext : 1, rw assert_neg, refl, rw nat.zero_add at this, simpa only [not_not, subtype.val_eq_coe] }, { rw [fix.approx,well_founded.fix_eq,fix_aux], congr, ext : 1, have hh : ¬(fix.approx f (z.val) x).dom, { apply nat.find_min h', rw [hk,nat.succ_add,← nat.add_succ], apply nat.lt_of_succ_le, apply nat.le_add_left }, rw succ_add_eq_succ_add at this hk, rw [assert_pos hh, k_ih (upto.succ z hh) this hk] } end lemma fix_def' {x : α} (h' : ¬ ∃ i, (fix.approx f i x).dom) : roption.fix f x = none := by dsimp [roption.fix]; rw assert_neg h' end basic end roption namespace roption instance : has_fix (roption α) := ⟨λ f, roption.fix (λ x u, f (x u)) ()⟩ end roption open sigma namespace pi instance roption.has_fix {β} : has_fix (α → roption β) := ⟨roption.fix⟩ end pi
9f896e1c1ed7eb96895631b11f8c99894a86bb55
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/combinatorics/young_diagram.lean
7886783d1f48ad7a37f9679d521a4216b8fe1a90
[ "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
4,643
lean
/- Copyright (c) 2022 Jake Levinson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jake Levinson -/ import order.upper_lower /-! # Young diagrams A Young diagram is a finite set of up-left justified boxes: ```text □□□□□ □□□ □□□ □ ``` This Young diagram corresponds to the [5, 3, 3, 1] partition of 12. We represent it as a lower set in `ℕ × ℕ` in the product partial order. We write `(i, j) ∈ μ` to say that `(i, j)` (in matrix coordinates) is in the Young diagram `μ`. ## Main definitions - `young_diagram` : Young diagrams - `young_diagram.card` : the number of cells in a Young diagram (its *cardinality*) - `young_diagram.distrib_lattice` : a distributive lattice instance for Young diagrams ordered by containment, with `(⊥ : young_diagram)` the empty diagram. ## Notation In "English notation", a Young diagram is drawn so that (i1, j1) ≤ (i2, j2) means (i1, j1) is weakly up-and-left of (i2, j2). This terminology is used below, e.g. in `young_diagram.up_left_mem`. ## Tags Young diagram ## References <https://en.wikipedia.org/wiki/Young_tableau> -/ /-- A Young diagram is a finite collection of cells on the `ℕ × ℕ` grid such that whenever a cell is present, so are all the ones above and to the left of it. Like matrices, an `(i, j)` cell is a cell in row `i` and column `j`, where rows are enumerated downward and columns rightward. Young diagrams are modeled as finite sets in `ℕ × ℕ` that are lower sets with respect to the standard order on products. -/ @[ext] structure young_diagram := (cells : finset (ℕ × ℕ)) (is_lower_set : is_lower_set (cells : set (ℕ × ℕ))) namespace young_diagram instance : set_like young_diagram (ℕ × ℕ) := { coe := coe young_diagram.cells, coe_injective' := λ μ ν h, by { rwa [young_diagram.ext_iff, ← finset.coe_inj] } } @[simp] lemma mem_cells {μ : young_diagram} (c : ℕ × ℕ) : c ∈ μ.cells ↔ c ∈ μ := iff.rfl /-- In "English notation", a Young diagram is drawn so that (i1, j1) ≤ (i2, j2) means (i1, j1) is weakly up-and-left of (i2, j2). -/ lemma up_left_mem (μ : young_diagram) {i1 i2 j1 j2 : ℕ} (hi : i1 ≤ i2) (hj : j1 ≤ j2) (hcell : (i2, j2) ∈ μ) : (i1, j1) ∈ μ := μ.is_lower_set (prod.mk_le_mk.mpr ⟨hi, hj⟩) hcell section distrib_lattice @[simp] lemma cells_subset_iff {μ ν : young_diagram} : μ.cells ⊆ ν.cells ↔ μ ≤ ν := iff.rfl @[simp] lemma cells_ssubset_iff {μ ν : young_diagram} : μ.cells ⊂ ν.cells ↔ μ < ν := iff.rfl instance : has_sup young_diagram := { sup := λ μ ν, { cells := μ.cells ∪ ν.cells, is_lower_set := by { rw finset.coe_union, exact μ.is_lower_set.union ν.is_lower_set } } } @[simp] lemma cells_sup (μ ν : young_diagram) : (μ ⊔ ν).cells = μ.cells ∪ ν.cells := rfl @[simp, norm_cast] lemma coe_sup (μ ν : young_diagram) : ↑(μ ⊔ ν) = (μ ∪ ν : set (ℕ × ℕ)) := finset.coe_union _ _ @[simp] lemma mem_sup {μ ν : young_diagram} {x : ℕ × ℕ} : x ∈ (μ ⊔ ν) ↔ x ∈ μ ∨ x ∈ ν := finset.mem_union instance : has_inf young_diagram := { inf := λ μ ν, { cells := μ.cells ∩ ν.cells, is_lower_set := by { rw finset.coe_inter, exact μ.is_lower_set.inter ν.is_lower_set } } } @[simp] lemma cells_inf (μ ν : young_diagram) : (μ ⊓ ν).cells = μ.cells ∩ ν.cells := rfl @[simp, norm_cast] lemma coe_inf (μ ν : young_diagram) : ↑(μ ⊓ ν) = (μ ∩ ν : set (ℕ × ℕ)) := finset.coe_inter _ _ @[simp] lemma mem_inf {μ ν : young_diagram} {x : ℕ × ℕ} : x ∈ (μ ⊓ ν) ↔ x ∈ μ ∧ x ∈ ν := finset.mem_inter /-- The empty Young diagram is (⊥ : young_diagram). -/ instance : order_bot young_diagram := { bot := { cells := ∅, is_lower_set := λ _ _ _, false.elim }, bot_le := λ _ _, false.elim } @[simp] lemma cells_bot : (⊥ : young_diagram).cells = ∅ := rfl @[simp, norm_cast] lemma coe_bot : ↑(⊥ : young_diagram) = (∅ : set (ℕ × ℕ)) := rfl @[simp] lemma not_mem_bot (x : ℕ × ℕ) : x ∉ (⊥ : young_diagram) := finset.not_mem_empty x instance : inhabited young_diagram := ⟨⊥⟩ instance : distrib_lattice young_diagram := function.injective.distrib_lattice young_diagram.cells (λ μ ν h, by rwa young_diagram.ext_iff) (λ _ _, rfl) (λ _ _, rfl) end distrib_lattice /-- Cardinality of a Young diagram -/ @[reducible] protected def card (μ : young_diagram) : ℕ := μ.cells.card end young_diagram
18589d4988b3abd4febb23c38a9d29590fb28372
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/finset/preimage.lean
d8f7c34a28b100e01a71ebf7df946b0fe5561b63
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
4,253
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.set.finite import algebra.big_operators.basic /-! # Preimage of a `finset` under an injective map. -/ open set function open_locale big_operators universes u v w x variables {α : Type u} {β : Type v} {ι : Sort w} {γ : Type x} namespace finset section preimage /-- Preimage of `s : finset β` under a map `f` injective of `f ⁻¹' s` as a `finset`. -/ noncomputable def preimage (s : finset β) (f : α → β) (hf : set.inj_on f (f ⁻¹' ↑s)) : finset α := (s.finite_to_set.preimage hf).to_finset @[simp] lemma mem_preimage {f : α → β} {s : finset β} {hf : set.inj_on f (f ⁻¹' ↑s)} {x : α} : x ∈ preimage s f hf ↔ f x ∈ s := set.finite.mem_to_finset @[simp, norm_cast] lemma coe_preimage {f : α → β} (s : finset β) (hf : set.inj_on f (f ⁻¹' ↑s)) : (↑(preimage s f hf) : set α) = f ⁻¹' ↑s := set.finite.coe_to_finset _ lemma monotone_preimage {f : α → β} (h : injective f) : monotone (λ s, preimage s f (h.inj_on _)) := λ s t hst x hx, mem_preimage.2 (hst $ mem_preimage.1 hx) lemma image_subset_iff_subset_preimage [decidable_eq β] {f : α → β} {s : finset α} {t : finset β} (hf : set.inj_on f (f ⁻¹' ↑t)) : s.image f ⊆ t ↔ s ⊆ t.preimage f hf := image_subset_iff.trans $ by simp only [subset_iff, mem_preimage] lemma map_subset_iff_subset_preimage {f : α ↪ β} {s : finset α} {t : finset β} : s.map f ⊆ t ↔ s ⊆ t.preimage f (f.injective.inj_on _) := by classical; rw [map_eq_image, image_subset_iff_subset_preimage] lemma image_preimage [decidable_eq β] (f : α → β) (s : finset β) [Π x, decidable (x ∈ set.range f)] (hf : set.inj_on f (f ⁻¹' ↑s)) : image f (preimage s f hf) = s.filter (λ x, x ∈ set.range f) := finset.coe_inj.1 $ by simp only [coe_image, coe_preimage, coe_filter, set.image_preimage_eq_inter_range, set.sep_mem_eq] lemma image_preimage_of_bij [decidable_eq β] (f : α → β) (s : finset β) (hf : set.bij_on f (f ⁻¹' ↑s) ↑s) : image f (preimage s f hf.inj_on) = s := finset.coe_inj.1 $ by simpa using hf.image_eq lemma sigma_preimage_mk {β : α → Type*} [decidable_eq α] (s : finset (Σ a, β a)) (t : finset α) : t.sigma (λ a, s.preimage (sigma.mk a) $ sigma_mk_injective.inj_on _) = s.filter (λ a, a.1 ∈ t) := by { ext x, simp [and_comm] } lemma sigma_preimage_mk_of_subset {β : α → Type*} [decidable_eq α] (s : finset (Σ a, β a)) {t : finset α} (ht : s.image sigma.fst ⊆ t) : t.sigma (λ a, s.preimage (sigma.mk a) $ sigma_mk_injective.inj_on _) = s := by rw [sigma_preimage_mk, filter_true_of_mem $ image_subset_iff.1 ht] lemma sigma_image_fst_preimage_mk {β : α → Type*} [decidable_eq α] (s : finset (Σ a, β a)) : (s.image sigma.fst).sigma (λ a, s.preimage (sigma.mk a) $ sigma_mk_injective.inj_on _) = s := s.sigma_preimage_mk_of_subset (subset.refl _) end preimage @[to_additive] lemma prod_preimage' [comm_monoid β] (f : α → γ) [decidable_pred $ λ x, x ∈ set.range f] (s : finset γ) (hf : set.inj_on f (f ⁻¹' ↑s)) (g : γ → β) : ∏ x in s.preimage f hf, g (f x) = ∏ x in s.filter (λ x, x ∈ set.range f), g x := by haveI := classical.dec_eq γ; calc ∏ x in preimage s f hf, g (f x) = ∏ x in image f (preimage s f hf), g x : eq.symm $ prod_image $ by simpa only [mem_preimage, inj_on] using hf ... = ∏ x in s.filter (λ x, x ∈ set.range f), g x : by rw [image_preimage] @[to_additive] lemma prod_preimage [comm_monoid β] (f : α → γ) (s : finset γ) (hf : set.inj_on f (f ⁻¹' ↑s)) (g : γ → β) (hg : ∀ x ∈ s, x ∉ set.range f → g x = 1) : ∏ x in s.preimage f hf, g (f x) = ∏ x in s, g x := by { classical, rw [prod_preimage', prod_filter_of_ne], exact λ x hx, not.imp_symm (hg x hx) } @[to_additive] lemma prod_preimage_of_bij [comm_monoid β] (f : α → γ) (s : finset γ) (hf : set.bij_on f (f ⁻¹' ↑s) ↑s) (g : γ → β) : ∏ x in s.preimage f hf.inj_on, g (f x) = ∏ x in s, g x := prod_preimage _ _ hf.inj_on g $ λ x hxs hxf, (hxf $ hf.subset_range hxs).elim end finset
4e4fd53d2e508af0cac0be1dc07bc6dc9319bddd
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Elab/Tactic/Rewrite.lean
55de56b798b193c3079563a07710665dfb2e149c
[ "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
3,144
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Tactic.Rewrite import Lean.Meta.Tactic.Replace import Lean.Elab.Tactic.Location import Lean.Elab.Tactic.Config namespace Lean.Elab.Tactic open Meta def rewriteTarget (stx : Syntax) (symm : Bool) (config : Rewrite.Config) : TacticM Unit := do Term.withSynthesize <| withMainContext do let e ← elabTerm stx none true let r ← (← getMainGoal).rewrite (← getMainTarget) e symm (config := config) let mvarId' ← (← getMainGoal).replaceTargetEq r.eNew r.eqProof replaceMainGoal (mvarId' :: r.mvarIds) def rewriteLocalDecl (stx : Syntax) (symm : Bool) (fvarId : FVarId) (config : Rewrite.Config) : TacticM Unit := do Term.withSynthesize <| withMainContext do let e ← elabTerm stx none true let localDecl ← fvarId.getDecl let rwResult ← (← getMainGoal).rewrite localDecl.type e symm (config := config) let replaceResult ← (← getMainGoal).replaceLocalDecl fvarId rwResult.eNew rwResult.eqProof replaceMainGoal (replaceResult.mvarId :: rwResult.mvarIds) def withRWRulesSeq (token : Syntax) (rwRulesSeqStx : Syntax) (x : (symm : Bool) → (term : Syntax) → TacticM Unit) : TacticM Unit := do let lbrak := rwRulesSeqStx[0] let rules := rwRulesSeqStx[1].getArgs -- show initial state up to (incl.) `[` withTacticInfoContext (mkNullNode #[token, lbrak]) (pure ()) let numRules := (rules.size + 1) / 2 for i in [:numRules] do let rule := rules[i * 2]! let sep := rules.getD (i * 2 + 1) Syntax.missing -- show rule state up to (incl.) next `,` withTacticInfoContext (mkNullNode #[rule, sep]) do -- show errors on rule withRef rule do let symm := !rule[0].isNone let term := rule[1] let processId (id : Syntax) : TacticM Unit := do -- Try to get equation theorems for `id` first let declName ← try resolveGlobalConstNoOverload id catch _ => return (← x symm term) let some eqThms ← getEqnsFor? declName (nonRec := true) | x symm term let rec go : List Name → TacticM Unit | [] => throwError "failed to rewrite using equation theorems for '{declName}'" | eqThm::eqThms => (x symm (mkIdentFrom id eqThm)) <|> go eqThms go eqThms.toList discard <| Term.addTermInfo id (← mkConstWithFreshMVarLevels declName) (lctx? := ← getLCtx) match term with | `($id:ident) => processId id | `(@$id:ident) => processId id | _ => x symm term declare_config_elab elabRewriteConfig Rewrite.Config @[builtin_tactic Lean.Parser.Tactic.rewriteSeq] def evalRewriteSeq : Tactic := fun stx => do let cfg ← elabRewriteConfig stx[1] let loc := expandOptLocation stx[3] withRWRulesSeq stx[0] stx[2] fun symm term => do withLocation loc (rewriteLocalDecl term symm · cfg) (rewriteTarget term symm cfg) (throwTacticEx `rewrite · "did not find instance of the pattern in the current goal") end Lean.Elab.Tactic
b304af7324100f4adda2d0067bfe24eb17c5b5f5
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/group/units.lean
f492b62332cd4a6a9c74737dc134ba0179a1cb47
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,540
lean
/- Copyright (c) 2017 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johannes Hölzl, Chris Hughes, Jens Wagemaker -/ import algebra.group.basic import logic.nontrivial /-! # Units (i.e., invertible elements) of a multiplicative monoid -/ universe u variable {α : Type u} /-- Units of a monoid, bundled version. An element of a `monoid` is a unit if it has a two-sided inverse. This version bundles the inverse element so that it can be computed. For a predicate see `is_unit`. -/ structure units (α : Type u) [monoid α] := (val : α) (inv : α) (val_inv : val * inv = 1) (inv_val : inv * val = 1) /-- Units of an add_monoid, bundled version. An element of an add_monoid is a unit if it has a two-sided additive inverse. This version bundles the inverse element so that it can be computed. For a predicate see `is_add_unit`. -/ structure add_units (α : Type u) [add_monoid α] := (val : α) (neg : α) (val_neg : val + neg = 0) (neg_val : neg + val = 0) attribute [to_additive add_units] units section has_elem @[to_additive] lemma unique_has_one {α : Type*} [unique α] [has_one α] : default α = 1 := unique.default_eq 1 end has_elem namespace units variables [monoid α] @[to_additive] instance : has_coe (units α) α := ⟨val⟩ @[to_additive] instance : has_inv (units α) := ⟨λ u, ⟨u.2, u.1, u.4, u.3⟩⟩ /-- See Note [custom simps projection] -/ @[to_additive /-" See Note [custom simps projection] "-/] def simps.coe (u : units α) : α := u /-- See Note [custom simps projection] -/ @[to_additive /-" See Note [custom simps projection] "-/] def simps.coe_inv (u : units α) : α := ↑(u⁻¹) initialize_simps_projections units (val → coe as_prefix, inv → coe_inv as_prefix) initialize_simps_projections add_units (val → coe as_prefix, neg → coe_neg as_prefix) @[simp, to_additive] lemma coe_mk (a : α) (b h₁ h₂) : ↑(units.mk a b h₁ h₂) = a := rfl @[ext, to_additive] theorem ext : function.injective (coe : units α → α) | ⟨v, i₁, vi₁, iv₁⟩ ⟨v', i₂, vi₂, iv₂⟩ e := by change v = v' at e; subst v'; congr; simpa only [iv₂, vi₁, one_mul, mul_one] using mul_assoc i₂ v i₁ @[norm_cast, to_additive] theorem eq_iff {a b : units α} : (a : α) = b ↔ a = b := ext.eq_iff @[to_additive] theorem ext_iff {a b : units α} : a = b ↔ (a : α) = b := eq_iff.symm @[to_additive] instance [decidable_eq α] : decidable_eq (units α) := λ a b, decidable_of_iff' _ ext_iff @[simp, to_additive] theorem mk_coe (u : units α) (y h₁ h₂) : mk (u : α) y h₁ h₂ = u := ext rfl /-- Copy a unit, adjusting definition equalities. -/ @[to_additive /-"Copy an `add_unit`, adjusting definitional equalities."-/, simps] def copy (u : units α) (val : α) (hv : val = u) (inv : α) (hi : inv = ↑(u⁻¹)) : units α := { val := val, inv := inv, inv_val := hv.symm ▸ hi.symm ▸ u.inv_val, val_inv := hv.symm ▸ hi.symm ▸ u.val_inv } @[to_additive] lemma copy_eq (u : units α) (val hv inv hi) : u.copy val hv inv hi = u := ext hv /-- Units of a monoid form a group. -/ @[to_additive] instance : group (units α) := { mul := λ u₁ u₂, ⟨u₁.val * u₂.val, u₂.inv * u₁.inv, by rw [mul_assoc, ← mul_assoc u₂.val, val_inv, one_mul, val_inv], by rw [mul_assoc, ← mul_assoc u₁.inv, inv_val, one_mul, inv_val]⟩, one := ⟨1, 1, one_mul 1, one_mul 1⟩, mul_one := λ u, ext $ mul_one u, one_mul := λ u, ext $ one_mul u, mul_assoc := λ u₁ u₂ u₃, ext $ mul_assoc u₁ u₂ u₃, inv := has_inv.inv, mul_left_inv := λ u, ext u.inv_val } variables (a b : units α) {c : units α} @[simp, norm_cast, to_additive] lemma coe_mul : (↑(a * b) : α) = a * b := rfl @[simp, norm_cast, to_additive] lemma coe_one : ((1 : units α) : α) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_eq_one {a : units α} : (a : α) = 1 ↔ a = 1 := by rw [←units.coe_one, eq_iff] @[simp, to_additive] lemma inv_mk (x y : α) (h₁ h₂) : (mk x y h₁ h₂)⁻¹ = mk y x h₂ h₁ := rfl @[simp, to_additive] lemma val_eq_coe : a.val = (↑a : α) := rfl @[simp, to_additive] lemma inv_eq_coe_inv : a.inv = ((a⁻¹ : units α) : α) := rfl @[simp, to_additive] lemma inv_mul : (↑a⁻¹ * a : α) = 1 := inv_val _ @[simp, to_additive] lemma mul_inv : (a * ↑a⁻¹ : α) = 1 := val_inv _ @[to_additive] lemma inv_mul_of_eq {u : units α} {a : α} (h : ↑u = a) : ↑u⁻¹ * a = 1 := by { rw [←h, u.inv_mul], } @[to_additive] lemma mul_inv_of_eq {u : units α} {a : α} (h : ↑u = a) : a * ↑u⁻¹ = 1 := by { rw [←h, u.mul_inv], } @[simp, to_additive] lemma mul_inv_cancel_left (a : units α) (b : α) : (a:α) * (↑a⁻¹ * b) = b := by rw [← mul_assoc, mul_inv, one_mul] @[simp, to_additive] lemma inv_mul_cancel_left (a : units α) (b : α) : (↑a⁻¹:α) * (a * b) = b := by rw [← mul_assoc, inv_mul, one_mul] @[simp, to_additive] lemma mul_inv_cancel_right (a : α) (b : units α) : a * b * ↑b⁻¹ = a := by rw [mul_assoc, mul_inv, mul_one] @[simp, to_additive] lemma inv_mul_cancel_right (a : α) (b : units α) : a * ↑b⁻¹ * b = a := by rw [mul_assoc, inv_mul, mul_one] @[to_additive] instance : inhabited (units α) := ⟨1⟩ @[to_additive] instance {α} [comm_monoid α] : comm_group (units α) := { mul_comm := λ u₁ u₂, ext $ mul_comm _ _, ..units.group } @[to_additive] instance [has_repr α] : has_repr (units α) := ⟨repr ∘ val⟩ @[simp, to_additive] theorem mul_right_inj (a : units α) {b c : α} : (a:α) * b = a * c ↔ b = c := ⟨λ h, by simpa only [inv_mul_cancel_left] using congr_arg ((*) ↑(a⁻¹ : units α)) h, congr_arg _⟩ @[simp, to_additive] theorem mul_left_inj (a : units α) {b c : α} : b * a = c * a ↔ b = c := ⟨λ h, by simpa only [mul_inv_cancel_right] using congr_arg (* ↑(a⁻¹ : units α)) h, congr_arg _⟩ @[to_additive] theorem eq_mul_inv_iff_mul_eq {a b : α} : a = b * ↑c⁻¹ ↔ a * c = b := ⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩ @[to_additive] theorem eq_inv_mul_iff_mul_eq {a c : α} : a = ↑b⁻¹ * c ↔ ↑b * a = c := ⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩ @[to_additive] theorem inv_mul_eq_iff_eq_mul {b c : α} : ↑a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩ @[to_additive] theorem mul_inv_eq_iff_eq_mul {a c : α} : a * ↑b⁻¹ = c ↔ a = c * b := ⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩ lemma inv_eq_of_mul_eq_one {u : units α} {a : α} (h : ↑u * a = 1) : ↑u⁻¹ = a := calc ↑u⁻¹ = ↑u⁻¹ * 1 : by rw mul_one ... = ↑u⁻¹ * ↑u * a : by rw [←h, ←mul_assoc] ... = a : by rw [u.inv_mul, one_mul] lemma inv_unique {u₁ u₂ : units α} (h : (↑u₁ : α) = ↑u₂) : (↑u₁⁻¹ : α) = ↑u₂⁻¹ := inv_eq_of_mul_eq_one $ by rw [h, u₂.mul_inv] end units /-- For `a, b` in a `comm_monoid` such that `a * b = 1`, makes a unit out of `a`. -/ @[to_additive "For `a, b` in an `add_comm_monoid` such that `a + b = 0`, makes an add_unit out of `a`."] def units.mk_of_mul_eq_one [comm_monoid α] (a b : α) (hab : a * b = 1) : units α := ⟨a, b, hab, (mul_comm b a).trans hab⟩ @[simp, to_additive] lemma units.coe_mk_of_mul_eq_one [comm_monoid α] {a b : α} (h : a * b = 1) : (units.mk_of_mul_eq_one a b h : α) = a := rfl section monoid variables [monoid α] {a b c : α} /-- Partial division. It is defined when the second argument is invertible, and unlike the division operator in `division_ring` it is not totalized at zero. -/ def divp (a : α) (u) : α := a * (u⁻¹ : units α) infix ` /ₚ `:70 := divp @[simp] theorem divp_self (u : units α) : (u : α) /ₚ u = 1 := units.mul_inv _ @[simp] theorem divp_one (a : α) : a /ₚ 1 = a := mul_one _ theorem divp_assoc (a b : α) (u : units α) : a * b /ₚ u = a * (b /ₚ u) := mul_assoc _ _ _ @[simp] theorem divp_inv (u : units α) : a /ₚ u⁻¹ = a * u := rfl @[simp] theorem divp_mul_cancel (a : α) (u : units α) : a /ₚ u * u = a := (mul_assoc _ _ _).trans $ by rw [units.inv_mul, mul_one] @[simp] theorem mul_divp_cancel (a : α) (u : units α) : (a * u) /ₚ u = a := (mul_assoc _ _ _).trans $ by rw [units.mul_inv, mul_one] @[simp] theorem divp_left_inj (u : units α) {a b : α} : a /ₚ u = b /ₚ u ↔ a = b := units.mul_left_inj _ theorem divp_divp_eq_divp_mul (x : α) (u₁ u₂ : units α) : (x /ₚ u₁) /ₚ u₂ = x /ₚ (u₂ * u₁) := by simp only [divp, mul_inv_rev, units.coe_mul, mul_assoc] theorem divp_eq_iff_mul_eq {x : α} {u : units α} {y : α} : x /ₚ u = y ↔ y * u = x := u.mul_left_inj.symm.trans $ by rw [divp_mul_cancel]; exact ⟨eq.symm, eq.symm⟩ theorem divp_eq_one_iff_eq {a : α} {u : units α} : a /ₚ u = 1 ↔ a = u := (units.mul_left_inj u).symm.trans $ by rw [divp_mul_cancel, one_mul] @[simp] theorem one_divp (u : units α) : 1 /ₚ u = ↑u⁻¹ := one_mul _ end monoid section comm_monoid variables [comm_monoid α] theorem divp_eq_divp_iff {x y : α} {ux uy : units α} : x /ₚ ux = y /ₚ uy ↔ x * uy = y * ux := by rw [divp_eq_iff_mul_eq, mul_comm, ← divp_assoc, divp_eq_iff_mul_eq, mul_comm y ux] theorem divp_mul_divp (x y : α) (ux uy : units α) : (x /ₚ ux) * (y /ₚ uy) = (x * y) /ₚ (ux * uy) := by rw [← divp_divp_eq_divp_mul, divp_assoc, mul_comm x, divp_assoc, mul_comm] end comm_monoid /-! # `is_unit` predicate In this file we define the `is_unit` predicate on a `monoid`, and prove a few basic properties. For the bundled version see `units`. See also `prime`, `associated`, and `irreducible` in `algebra/associated`. -/ section is_unit variables {M : Type*} {N : Type*} /-- An element `a : M` of a monoid is a unit if it has a two-sided inverse. The actual definition says that `a` is equal to some `u : units M`, where `units M` is a bundled version of `is_unit`. -/ @[to_additive is_add_unit "An element `a : M` of an add_monoid is an `add_unit` if it has a two-sided additive inverse. The actual definition says that `a` is equal to some `u : add_units M`, where `add_units M` is a bundled version of `is_add_unit`."] def is_unit [monoid M] (a : M) : Prop := ∃ u : units M, (u : M) = a @[nontriviality] lemma is_unit_of_subsingleton [monoid M] [subsingleton M] (a : M) : is_unit a := ⟨⟨a, a, subsingleton.elim _ _, subsingleton.elim _ _⟩, rfl⟩ instance [monoid M] [subsingleton M] : unique (units M) := { default := 1, uniq := λ a, units.coe_eq_one.mp $ subsingleton.elim (a : M) 1 } @[simp, to_additive is_add_unit_add_unit] protected lemma units.is_unit [monoid M] (u : units M) : is_unit (u : M) := ⟨u, rfl⟩ @[simp, to_additive is_add_unit_zero] theorem is_unit_one [monoid M] : is_unit (1:M) := ⟨1, rfl⟩ @[to_additive is_add_unit_of_add_eq_zero] theorem is_unit_of_mul_eq_one [comm_monoid M] (a b : M) (h : a * b = 1) : is_unit a := ⟨units.mk_of_mul_eq_one a b h, rfl⟩ @[to_additive is_add_unit.exists_neg] theorem is_unit.exists_right_inv [monoid M] {a : M} (h : is_unit a) : ∃ b, a * b = 1 := by { rcases h with ⟨⟨a, b, hab, _⟩, rfl⟩, exact ⟨b, hab⟩ } @[to_additive is_add_unit.exists_neg'] theorem is_unit.exists_left_inv [monoid M] {a : M} (h : is_unit a) : ∃ b, b * a = 1 := by { rcases h with ⟨⟨a, b, _, hba⟩, rfl⟩, exact ⟨b, hba⟩ } @[to_additive is_add_unit_iff_exists_neg] theorem is_unit_iff_exists_inv [comm_monoid M] {a : M} : is_unit a ↔ ∃ b, a * b = 1 := ⟨λ h, h.exists_right_inv, λ ⟨b, hab⟩, is_unit_of_mul_eq_one _ b hab⟩ @[to_additive is_add_unit_iff_exists_neg'] theorem is_unit_iff_exists_inv' [comm_monoid M] {a : M} : is_unit a ↔ ∃ b, b * a = 1 := by simp [is_unit_iff_exists_inv, mul_comm] @[to_additive] lemma is_unit.mul [monoid M] {x y : M} : is_unit x → is_unit y → is_unit (x * y) := by { rintros ⟨x, rfl⟩ ⟨y, rfl⟩, exact ⟨x * y, units.coe_mul _ _⟩ } /-- Multiplication by a `u : units M` on the right doesn't affect `is_unit`. -/ @[simp, to_additive is_add_unit_add_add_units "Addition of a `u : add_units M` on the right doesn't affect `is_add_unit`."] theorem units.is_unit_mul_units [monoid M] (a : M) (u : units M) : is_unit (a * u) ↔ is_unit a := iff.intro (assume ⟨v, hv⟩, have is_unit (a * ↑u * ↑u⁻¹), by existsi v * u⁻¹; rw [←hv, units.coe_mul], by rwa [mul_assoc, units.mul_inv, mul_one] at this) (λ v, v.mul u.is_unit) /-- Multiplication by a `u : units M` on the left doesn't affect `is_unit`. -/ @[simp, to_additive is_add_unit_add_units_add "Addition of a `u : add_units M` on the left doesn't affect `is_add_unit`."] theorem units.is_unit_units_mul {M : Type*} [monoid M] (u : units M) (a : M) : is_unit (↑u * a) ↔ is_unit a := iff.intro (assume ⟨v, hv⟩, have is_unit (↑u⁻¹ * (↑u * a)), by existsi u⁻¹ * v; rw [←hv, units.coe_mul], by rwa [←mul_assoc, units.inv_mul, one_mul] at this) u.is_unit.mul @[to_additive is_add_unit_of_add_is_add_unit_left] theorem is_unit_of_mul_is_unit_left [comm_monoid M] {x y : M} (hu : is_unit (x * y)) : is_unit x := let ⟨z, hz⟩ := is_unit_iff_exists_inv.1 hu in is_unit_iff_exists_inv.2 ⟨y * z, by rwa ← mul_assoc⟩ @[to_additive] theorem is_unit_of_mul_is_unit_right [comm_monoid M] {x y : M} (hu : is_unit (x * y)) : is_unit y := @is_unit_of_mul_is_unit_left _ _ y x $ by rwa mul_comm @[simp] lemma is_unit.mul_iff [comm_monoid M] {x y : M} : is_unit (x * y) ↔ is_unit x ∧ is_unit y := ⟨λ h, ⟨is_unit_of_mul_is_unit_left h, is_unit_of_mul_is_unit_right h⟩, λ h, is_unit.mul h.1 h.2⟩ @[to_additive] theorem is_unit.mul_right_inj [monoid M] {a b c : M} (ha : is_unit a) : a * b = a * c ↔ b = c := by cases ha with a ha; rw [←ha, units.mul_right_inj] @[to_additive] theorem is_unit.mul_left_inj [monoid M] {a b c : M} (ha : is_unit a) : b * a = c * a ↔ b = c := by cases ha with a ha; rw [←ha, units.mul_left_inj] /-- The element of the group of units, corresponding to an element of a monoid which is a unit. -/ noncomputable def is_unit.unit [monoid M] {a : M} (h : is_unit a) : units M := (classical.some h).copy a (classical.some_spec h).symm _ rfl lemma is_unit.unit_spec [monoid M] {a : M} (h : is_unit a) : ↑h.unit = a := rfl lemma is_unit.coe_inv_mul [monoid M] {a : M} (h : is_unit a) : ↑(h.unit)⁻¹ * a = 1 := units.mul_inv _ lemma is_unit.mul_coe_inv [monoid M] {a : M} (h : is_unit a) : a * ↑(h.unit)⁻¹ = 1 := begin convert units.mul_inv _, simp [h.unit_spec] end end is_unit section noncomputable_defs variables {M : Type*} /-- Constructs a `group` structure on a `monoid` consisting only of units. -/ noncomputable def group_of_is_unit [hM : monoid M] (h : ∀ (a : M), is_unit a) : group M := { inv := λ a, ↑((h a).unit)⁻¹, mul_left_inv := λ a, by { change ↑((h a).unit)⁻¹ * a = 1, rw [units.inv_mul_eq_iff_eq_mul, (h a).unit_spec, mul_one] }, .. hM } /-- Constructs a `comm_group` structure on a `comm_monoid` consisting only of units. -/ noncomputable def comm_group_of_is_unit [hM : comm_monoid M] (h : ∀ (a : M), is_unit a) : comm_group M := { inv := λ a, ↑((h a).unit)⁻¹, mul_left_inv := λ a, by { change ↑((h a).unit)⁻¹ * a = 1, rw [units.inv_mul_eq_iff_eq_mul, (h a).unit_spec, mul_one] }, .. hM } end noncomputable_defs
13e6a4f2e0aef95c1482814caea276bda3304af5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/adjunction/reflective.lean
3febb0a8d88f7e10e4af8f975b4b0c60eff63522
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
7,937
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.adjunction.fully_faithful import category_theory.functor.reflects_isomorphisms import category_theory.epi_mono /-! # Reflective functors Basic properties of reflective functors, especially those relating to their essential image. Note properties of reflective functors relating to limits and colimits are included in `category_theory.monad.limits`. -/ universes v₁ v₂ v₃ u₁ u₂ u₃ noncomputable theory namespace category_theory open category adjunction variables {C : Type u₁} {D : Type u₂} {E : Type u₃} variables [category.{v₁} C] [category.{v₂} D] [category.{v₃} E] /-- A functor is *reflective*, or *a reflective inclusion*, if it is fully faithful and right adjoint. -/ class reflective (R : D ⥤ C) extends is_right_adjoint R, full R, faithful R. variables {i : D ⥤ C} /-- For a reflective functor `i` (with left adjoint `L`), with unit `η`, we have `η_iL = iL η`. -/ -- TODO: This holds more generally for idempotent adjunctions, not just reflective adjunctions. lemma unit_obj_eq_map_unit [reflective i] (X : C) : (of_right_adjoint i).unit.app (i.obj ((left_adjoint i).obj X)) = i.map ((left_adjoint i).map ((of_right_adjoint i).unit.app X)) := begin rw [←cancel_mono (i.map ((of_right_adjoint i).counit.app ((left_adjoint i).obj X))), ←i.map_comp], simp, end /-- When restricted to objects in `D` given by `i : D ⥤ C`, the unit is an isomorphism. In other words, `η_iX` is an isomorphism for any `X` in `D`. More generally this applies to objects essentially in the reflective subcategory, see `functor.ess_image.unit_iso`. -/ instance is_iso_unit_obj [reflective i] {B : D} : is_iso ((of_right_adjoint i).unit.app (i.obj B)) := begin have : (of_right_adjoint i).unit.app (i.obj B) = inv (i.map ((of_right_adjoint i).counit.app B)), { rw ← comp_hom_eq_id, apply (of_right_adjoint i).right_triangle_components }, rw this, exact is_iso.inv_is_iso, end /-- If `A` is essentially in the image of a reflective functor `i`, then `η_A` is an isomorphism. This gives that the "witness" for `A` being in the essential image can instead be given as the reflection of `A`, with the isomorphism as `η_A`. (For any `B` in the reflective subcategory, we automatically have that `ε_B` is an iso.) -/ lemma functor.ess_image.unit_is_iso [reflective i] {A : C} (h : A ∈ i.ess_image) : is_iso ((of_right_adjoint i).unit.app A) := begin suffices : (of_right_adjoint i).unit.app A = h.get_iso.inv ≫ (of_right_adjoint i).unit.app (i.obj h.witness) ≫ (left_adjoint i ⋙ i).map h.get_iso.hom, { rw this, apply_instance }, rw ← nat_trans.naturality, simp, end /-- If `η_A` is an isomorphism, then `A` is in the essential image of `i`. -/ lemma mem_ess_image_of_unit_is_iso [is_right_adjoint i] (A : C) [is_iso ((of_right_adjoint i).unit.app A)] : A ∈ i.ess_image := ⟨(left_adjoint i).obj A, ⟨(as_iso ((of_right_adjoint i).unit.app A)).symm⟩⟩ /-- If `η_A` is a split monomorphism, then `A` is in the reflective subcategory. -/ lemma mem_ess_image_of_unit_is_split_mono [reflective i] {A : C} [is_split_mono ((of_right_adjoint i).unit.app A)] : A ∈ i.ess_image := begin let η : 𝟭 C ⟶ left_adjoint i ⋙ i := (of_right_adjoint i).unit, haveI : is_iso (η.app (i.obj ((left_adjoint i).obj A))) := (i.obj_mem_ess_image _).unit_is_iso, have : epi (η.app A), { apply epi_of_epi (retraction (η.app A)) _, rw (show retraction _ ≫ η.app A = _, from η.naturality (retraction (η.app A))), apply epi_comp (η.app (i.obj ((left_adjoint i).obj A))) }, resetI, haveI := is_iso_of_epi_of_is_split_mono (η.app A), exact mem_ess_image_of_unit_is_iso A, end /-- Composition of reflective functors. -/ instance reflective.comp (F : C ⥤ D) (G : D ⥤ E) [Fr : reflective F] [Gr : reflective G] : reflective (F ⋙ G) := { to_faithful := faithful.comp F G, } /-- (Implementation) Auxiliary definition for `unit_comp_partial_bijective`. -/ def unit_comp_partial_bijective_aux [reflective i] (A : C) (B : D) : (A ⟶ i.obj B) ≃ (i.obj ((left_adjoint i).obj A) ⟶ i.obj B) := ((adjunction.of_right_adjoint i).hom_equiv _ _).symm.trans (equiv_of_fully_faithful i) /-- The description of the inverse of the bijection `unit_comp_partial_bijective_aux`. -/ lemma unit_comp_partial_bijective_aux_symm_apply [reflective i] {A : C} {B : D} (f : i.obj ((left_adjoint i).obj A) ⟶ i.obj B) : (unit_comp_partial_bijective_aux _ _).symm f = (of_right_adjoint i).unit.app A ≫ f := by simp [unit_comp_partial_bijective_aux] /-- If `i` has a reflector `L`, then the function `(i.obj (L.obj A) ⟶ B) → (A ⟶ B)` given by precomposing with `η.app A` is a bijection provided `B` is in the essential image of `i`. That is, the function `λ (f : i.obj (L.obj A) ⟶ B), η.app A ≫ f` is bijective, as long as `B` is in the essential image of `i`. This definition gives an equivalence: the key property that the inverse can be described nicely is shown in `unit_comp_partial_bijective_symm_apply`. This establishes there is a natural bijection `(A ⟶ B) ≃ (i.obj (L.obj A) ⟶ B)`. In other words, from the point of view of objects in `D`, `A` and `i.obj (L.obj A)` look the same: specifically that `η.app A` is an isomorphism. -/ def unit_comp_partial_bijective [reflective i] (A : C) {B : C} (hB : B ∈ i.ess_image) : (A ⟶ B) ≃ (i.obj ((left_adjoint i).obj A) ⟶ B) := calc (A ⟶ B) ≃ (A ⟶ i.obj hB.witness) : iso.hom_congr (iso.refl _) hB.get_iso.symm ... ≃ (i.obj _ ⟶ i.obj hB.witness) : unit_comp_partial_bijective_aux _ _ ... ≃ (i.obj ((left_adjoint i).obj A) ⟶ B) : iso.hom_congr (iso.refl _) hB.get_iso @[simp] lemma unit_comp_partial_bijective_symm_apply [reflective i] (A : C) {B : C} (hB : B ∈ i.ess_image) (f) : (unit_comp_partial_bijective A hB).symm f = (of_right_adjoint i).unit.app A ≫ f := by simp [unit_comp_partial_bijective, unit_comp_partial_bijective_aux_symm_apply] lemma unit_comp_partial_bijective_symm_natural [reflective i] (A : C) {B B' : C} (h : B ⟶ B') (hB : B ∈ i.ess_image) (hB' : B' ∈ i.ess_image) (f : i.obj ((left_adjoint i).obj A) ⟶ B) : (unit_comp_partial_bijective A hB').symm (f ≫ h) = (unit_comp_partial_bijective A hB).symm f ≫ h := by simp lemma unit_comp_partial_bijective_natural [reflective i] (A : C) {B B' : C} (h : B ⟶ B') (hB : B ∈ i.ess_image) (hB' : B' ∈ i.ess_image) (f : A ⟶ B) : (unit_comp_partial_bijective A hB') (f ≫ h) = unit_comp_partial_bijective A hB f ≫ h := by rw [←equiv.eq_symm_apply, unit_comp_partial_bijective_symm_natural A h, equiv.symm_apply_apply] /-- If `i : D ⥤ C` is reflective, the inverse functor of `i ≌ F.ess_image` can be explicitly defined by the reflector. -/ @[simps] def equiv_ess_image_of_reflective [reflective i] : D ≌ i.ess_image_subcategory := { functor := i.to_ess_image, inverse := i.ess_image_inclusion ⋙ (left_adjoint i : _), unit_iso := nat_iso.of_components (λ X, (as_iso $ (of_right_adjoint i).counit.app X).symm) (by { intros X Y f, dsimp, simp only [is_iso.eq_inv_comp, is_iso.comp_inv_eq, category.assoc], exact ((of_right_adjoint i).counit.naturality _).symm }), counit_iso := nat_iso.of_components (λ X, by { refine (iso.symm $ as_iso _), exact (of_right_adjoint i).unit.app X.obj, apply_with (is_iso_of_reflects_iso _ i.ess_image_inclusion) { instances := ff }, exact functor.ess_image.unit_is_iso X.property }) (by { intros X Y f, dsimp, rw [is_iso.comp_inv_eq, assoc], have h := ((of_right_adjoint i).unit.naturality f).symm, rw [functor.id_map] at h, erw [← h, is_iso.inv_hom_id_assoc, functor.comp_map] }) } end category_theory
80a26493fd267f976b18ffce6fd192572761c7a2
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/path_connected.lean
9a13c1b205d627a7b6dbe03d7be43fe66534c4c6
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
40,951
lean
/- Copyright (c) 2020 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import topology.unit_interval import topology.algebra.ordered.proj_Icc import topology.continuous_function.basic /-! # Path connectedness ## Main definitions In the file the unit interval `[0, 1]` in `ℝ` is denoted by `I`, and `X` is a topological space. * `path (x y : X)` is the type of paths from `x` to `y`, i.e., continuous maps from `I` to `X` mapping `0` to `x` and `1` to `y`. * `path.map` is the image of a path under a continuous map. * `joined (x y : X)` means there is a path between `x` and `y`. * `joined.some_path (h : joined x y)` selects some path between two points `x` and `y`. * `path_component (x : X)` is the set of points joined to `x`. * `path_connected_space X` is a predicate class asserting that `X` is non-empty and every two points of `X` are joined. Then there are corresponding relative notions for `F : set X`. * `joined_in F (x y : X)` means there is a path `γ` joining `x` to `y` with values in `F`. * `joined_in.some_path (h : joined_in F x y)` selects a path from `x` to `y` inside `F`. * `path_component_in F (x : X)` is the set of points joined to `x` in `F`. * `is_path_connected F` asserts that `F` is non-empty and every two points of `F` are joined in `F`. * `loc_path_connected_space X` is a predicate class asserting that `X` is locally path-connected: each point has a basis of path-connected neighborhoods (we do *not* ask these to be open). ## Main theorems * `joined` and `joined_in F` are transitive relations. One can link the absolute and relative version in two directions, using `(univ : set X)` or the subtype `↥F`. * `path_connected_space_iff_univ : path_connected_space X ↔ is_path_connected (univ : set X)` * `is_path_connected_iff_path_connected_space : is_path_connected F ↔ path_connected_space ↥F` For locally path connected spaces, we have * `path_connected_space_iff_connected_space : path_connected_space X ↔ connected_space X` * `is_connected_iff_is_path_connected (U_op : is_open U) : is_path_connected U ↔ is_connected U` ## Implementation notes By default, all paths have `I` as their source and `X` as their target, but there is an operation `set.Icc_extend` that will extend any continuous map `γ : I → X` into a continuous map `Icc_extend zero_le_one γ : ℝ → X` that is constant before `0` and after `1`. This is used to define `path.extend` that turns `γ : path x y` into a continuous map `γ.extend : ℝ → X` whose restriction to `I` is the original `γ`, and is equal to `x` on `(-∞, 0]` and to `y` on `[1, +∞)`. -/ noncomputable theory open_locale classical topological_space filter unit_interval open filter set function unit_interval variables {X Y : Type*} [topological_space X] [topological_space Y] {x y z : X} {ι : Type*} /-! ### Paths -/ /-- Continuous path connecting two points `x` and `y` in a topological space -/ @[nolint has_inhabited_instance] structure path (x y : X) extends C(I, X) := (source' : to_fun 0 = x) (target' : to_fun 1 = y) instance : has_coe_to_fun (path x y) (λ _, I → X) := ⟨λ p, p.to_fun⟩ @[ext] protected lemma path.ext : ∀ {γ₁ γ₂ : path x y}, (γ₁ : I → X) = γ₂ → γ₁ = γ₂ | ⟨⟨x, h11⟩, h12, h13⟩ ⟨⟨.(x), h21⟩, h22, h23⟩ rfl := rfl namespace path @[simp] lemma coe_mk (f : I → X) (h₁ h₂ h₃) : ⇑(mk ⟨f, h₁⟩ h₂ h₃ : path x y) = f := rfl variable (γ : path x y) @[continuity] protected lemma continuous : continuous γ := γ.continuous_to_fun @[simp] protected lemma source : γ 0 = x := γ.source' @[simp] protected lemma target : γ 1 = y := γ.target' /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply : I → X := γ initialize_simps_projections path (to_continuous_map_to_fun → simps.apply, -to_continuous_map) @[simp] lemma coe_to_continuous_map : ⇑γ.to_continuous_map = γ := rfl /-- Any function `φ : Π (a : α), path (x a) (y a)` can be seen as a function `α × I → X`. -/ instance has_uncurry_path {X α : Type*} [topological_space X] {x y : α → X} : has_uncurry (Π (a : α), path (x a) (y a)) (α × I) X := ⟨λ φ p, φ p.1 p.2⟩ /-- The constant path from a point to itself -/ @[refl, simps] def refl (x : X) : path x x := { to_fun := λ t, x, continuous_to_fun := continuous_const, source' := rfl, target' := rfl } @[simp] lemma refl_range {a : X} : range (path.refl a) = {a} := by simp [path.refl, has_coe_to_fun.coe, coe_fn] /-- The reverse of a path from `x` to `y`, as a path from `y` to `x` -/ @[symm, simps] def symm (γ : path x y) : path y x := { to_fun := γ ∘ σ, continuous_to_fun := by continuity, source' := by simpa [-path.target] using γ.target, target' := by simpa [-path.source] using γ.source } @[simp] lemma symm_symm {γ : path x y} : γ.symm.symm = γ := by { ext, simp } @[simp] lemma refl_symm {a : X} : (path.refl a).symm = path.refl a := by { ext, refl } @[simp] lemma symm_range {a b : X} (γ : path a b) : range γ.symm = range γ := begin ext x, simp only [mem_range, path.symm, has_coe_to_fun.coe, coe_fn, unit_interval.symm, set_coe.exists, comp_app, subtype.coe_mk, subtype.val_eq_coe], split; rintros ⟨y, hy, hxy⟩; refine ⟨1-y, mem_iff_one_sub_mem.mp hy, _⟩; convert hxy, simp end /-- A continuous map extending a path to `ℝ`, constant before `0` and after `1`. -/ def extend : ℝ → X := Icc_extend zero_le_one γ /-- See Note [continuity lemma statement]. -/ lemma _root_.continuous.path_extend {γ : Y → path x y} {f : Y → ℝ} (hγ : continuous ↿γ) (hf : continuous f) : continuous (λ t, (γ t).extend (f t)) := continuous.Icc_extend hγ hf /-- A useful special case of `continuous.path_extend`. -/ @[continuity] lemma continuous_extend : continuous γ.extend := γ.continuous.Icc_extend' lemma _root_.filter.tendsto.path_extend {X Y : Type*} [topological_space X] [topological_space Y] {l r : Y → X} {y : Y} {l₁ : filter ℝ} {l₂ : filter X} {γ : ∀ y, path (l y) (r y)} (hγ : tendsto ↿γ (𝓝 y ×ᶠ l₁.map (proj_Icc 0 1 zero_le_one)) l₂) : tendsto ↿(λ x, (γ x).extend) (𝓝 y ×ᶠ l₁) l₂ := filter.tendsto.Icc_extend _ hγ lemma _root_.continuous_at.path_extend {g : Y → ℝ} {l r : Y → X} (γ : ∀ y, path (l y) (r y)) {y : Y} (hγ : continuous_at ↿γ (y, proj_Icc 0 1 zero_le_one (g y))) (hg : continuous_at g y) : continuous_at (λ i, (γ i).extend (g i)) y := hγ.Icc_extend (λ x, γ x) hg @[simp] lemma extend_extends {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t : ℝ} (ht : t ∈ (Icc 0 1 : set ℝ)) : γ.extend t = γ ⟨t, ht⟩ := Icc_extend_of_mem _ γ ht lemma extend_zero : γ.extend 0 = x := by simp lemma extend_one : γ.extend 1 = y := by simp @[simp] lemma extend_extends' {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t : (Icc 0 1 : set ℝ)) : γ.extend t = γ t := Icc_extend_coe _ γ t @[simp] lemma extend_range {X : Type*} [topological_space X] {a b : X} (γ : path a b) : range γ.extend = range γ := Icc_extend_range _ γ lemma extend_of_le_zero {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t : ℝ} (ht : t ≤ 0) : γ.extend t = a := (Icc_extend_of_le_left _ _ ht).trans γ.source lemma extend_of_one_le {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t : ℝ} (ht : 1 ≤ t) : γ.extend t = b := (Icc_extend_of_right_le _ _ ht).trans γ.target @[simp] lemma refl_extend {X : Type*} [topological_space X] {a : X} : (path.refl a).extend = λ _, a := rfl /-- The path obtained from a map defined on `ℝ` by restriction to the unit interval. -/ def of_line {f : ℝ → X} (hf : continuous_on f I) (h₀ : f 0 = x) (h₁ : f 1 = y) : path x y := { to_fun := f ∘ coe, continuous_to_fun := hf.comp_continuous continuous_subtype_coe subtype.prop, source' := h₀, target' := h₁ } lemma of_line_mem {f : ℝ → X} (hf : continuous_on f I) (h₀ : f 0 = x) (h₁ : f 1 = y) : ∀ t, of_line hf h₀ h₁ t ∈ f '' I := λ ⟨t, t_in⟩, ⟨t, t_in, rfl⟩ local attribute [simp] Iic_def /-- Concatenation of two paths from `x` to `y` and from `y` to `z`, putting the first path on `[0, 1/2]` and the second one on `[1/2, 1]`. -/ @[trans] def trans (γ : path x y) (γ' : path y z) : path x z := { to_fun := (λ t : ℝ, if t ≤ 1/2 then γ.extend (2*t) else γ'.extend (2*t-1)) ∘ coe, continuous_to_fun := begin refine (continuous.if_le _ _ continuous_id continuous_const (by norm_num)).comp continuous_subtype_coe, -- TODO: the following are provable by `continuity` but it is too slow exacts [γ.continuous_extend.comp (continuous_const.mul continuous_id), γ'.continuous_extend.comp ((continuous_const.mul continuous_id).sub continuous_const)] end, source' := by norm_num, target' := by norm_num } lemma trans_apply (γ : path x y) (γ' : path y z) (t : I) : (γ.trans γ') t = if h : (t : ℝ) ≤ 1/2 then γ ⟨2 * t, (mul_pos_mem_iff zero_lt_two).2 ⟨t.2.1, h⟩⟩ else γ' ⟨2 * t - 1, two_mul_sub_one_mem_iff.2 ⟨(not_le.1 h).le, t.2.2⟩⟩ := show ite _ _ _ = _, by split_ifs; rw extend_extends @[simp] lemma trans_symm (γ : path x y) (γ' : path y z) : (γ.trans γ').symm = γ'.symm.trans γ.symm := begin ext t, simp only [trans_apply, ← one_div, symm_apply, not_le, comp_app], split_ifs with h h₁ h₂ h₃ h₄; rw [coe_symm_eq] at h, { have ht : (t : ℝ) = 1/2, { linarith [unit_interval.nonneg t, unit_interval.le_one t] }, norm_num [ht] }, { refine congr_arg _ (subtype.ext _), norm_num [sub_sub_assoc_swap, mul_sub] }, { refine congr_arg _ (subtype.ext _), have h : 2 - 2 * (t : ℝ) - 1 = 1 - 2 * t, by linarith, norm_num [mul_sub, h] }, { exfalso, linarith [unit_interval.nonneg t, unit_interval.le_one t] } end @[simp] lemma refl_trans_refl {X : Type*} [topological_space X] {a : X} : (path.refl a).trans (path.refl a) = path.refl a := begin ext, simp only [path.trans, if_t_t, one_div, path.refl_extend], refl end lemma trans_range {X : Type*} [topological_space X] {a b c : X} (γ₁ : path a b) (γ₂ : path b c) : range (γ₁.trans γ₂) = range γ₁ ∪ range γ₂ := begin rw path.trans, apply eq_of_subset_of_subset, { rintros x ⟨⟨t, ht0, ht1⟩, hxt⟩, by_cases h : t ≤ 1/2, { left, use [2*t, ⟨by linarith, by linarith⟩], rw ← γ₁.extend_extends, unfold_coes at hxt, simp only [h, comp_app, if_true] at hxt, exact hxt }, { right, use [2*t-1, ⟨by linarith, by linarith⟩], rw ← γ₂.extend_extends, unfold_coes at hxt, simp only [h, comp_app, if_false] at hxt, exact hxt } }, { rintros x (⟨⟨t, ht0, ht1⟩, hxt⟩ | ⟨⟨t, ht0, ht1⟩, hxt⟩), { use ⟨t/2, ⟨by linarith, by linarith⟩⟩, unfold_coes, have : t/2 ≤ 1/2 := by linarith, simp only [this, comp_app, if_true], ring_nf, rwa γ₁.extend_extends }, { by_cases h : t = 0, { use ⟨1/2, ⟨by linarith, by linarith⟩⟩, unfold_coes, simp only [h, comp_app, if_true, le_refl, mul_one_div_cancel (@two_ne_zero ℝ _ _)], rw γ₁.extend_one, rwa [← γ₂.extend_extends, h, γ₂.extend_zero] at hxt }, { use ⟨(t+1)/2, ⟨by linarith, by linarith⟩⟩, unfold_coes, change t ≠ 0 at h, have ht0 := lt_of_le_of_ne ht0 h.symm, have : ¬ (t+1)/2 ≤ 1/2 := by {rw not_le, linarith}, simp only [comp_app, if_false, this], ring_nf, rwa γ₂.extend_extends } } } end /-- Image of a path from `x` to `y` by a continuous map -/ def map (γ : path x y) {Y : Type*} [topological_space Y] {f : X → Y} (h : continuous f) : path (f x) (f y) := { to_fun := f ∘ γ, continuous_to_fun := by continuity, source' := by simp, target' := by simp } @[simp] lemma map_coe (γ : path x y) {Y : Type*} [topological_space Y] {f : X → Y} (h : continuous f) : (γ.map h : I → Y) = f ∘ γ := by { ext t, refl } @[simp] lemma map_symm (γ : path x y) {Y : Type*} [topological_space Y] {f : X → Y} (h : continuous f) : (γ.map h).symm = γ.symm.map h := rfl @[simp] lemma map_trans (γ : path x y) (γ' : path y z) {Y : Type*} [topological_space Y] {f : X → Y} (h : continuous f) : (γ.trans γ').map h = (γ.map h).trans (γ'.map h) := by { ext t, rw [trans_apply, map_coe, comp_app, trans_apply], split_ifs; refl } @[simp] lemma map_id (γ : path x y) : γ.map continuous_id = γ := by { ext, refl } @[simp] lemma map_map (γ : path x y) {Y : Type*} [topological_space Y] {Z : Type*} [topological_space Z] {f : X → Y} (hf : continuous f) {g : Y → Z} (hg : continuous g) : (γ.map hf).map hg = γ.map (hg.comp hf) := by { ext, refl } /-- Casting a path from `x` to `y` to a path from `x'` to `y'` when `x' = x` and `y' = y` -/ def cast (γ : path x y) {x' y'} (hx : x' = x) (hy : y' = y) : path x' y' := { to_fun := γ, continuous_to_fun := γ.continuous, source' := by simp [hx], target' := by simp [hy] } @[simp] lemma symm_cast {X : Type*} [topological_space X] {a₁ a₂ b₁ b₂ : X} (γ : path a₂ b₂) (ha : a₁ = a₂) (hb : b₁ = b₂) : (γ.cast ha hb).symm = (γ.symm).cast hb ha := rfl @[simp] lemma trans_cast {X : Type*} [topological_space X] {a₁ a₂ b₁ b₂ c₁ c₂ : X} (γ : path a₂ b₂) (γ' : path b₂ c₂) (ha : a₁ = a₂) (hb : b₁ = b₂) (hc : c₁ = c₂) : (γ.cast ha hb).trans (γ'.cast hb hc) = (γ.trans γ').cast ha hc := rfl @[simp] lemma cast_coe (γ : path x y) {x' y'} (hx : x' = x) (hy : y' = y) : (γ.cast hx hy : I → X) = γ := rfl @[continuity] lemma symm_continuous_family {X ι : Type*} [topological_space X] [topological_space ι] {a b : ι → X} (γ : Π (t : ι), path (a t) (b t)) (h : continuous ↿γ) : continuous ↿(λ t, (γ t).symm) := h.comp (continuous_id.prod_map continuous_symm) @[continuity] lemma continuous_uncurry_extend_of_continuous_family {X ι : Type*} [topological_space X] [topological_space ι] {a b : ι → X} (γ : Π (t : ι), path (a t) (b t)) (h : continuous ↿γ) : continuous ↿(λ t, (γ t).extend) := h.comp (continuous_id.prod_map continuous_proj_Icc) @[continuity] lemma trans_continuous_family {X ι : Type*} [topological_space X] [topological_space ι] {a b c : ι → X} (γ₁ : Π (t : ι), path (a t) (b t)) (h₁ : continuous ↿γ₁) (γ₂ : Π (t : ι), path (b t) (c t)) (h₂ : continuous ↿γ₂) : continuous ↿(λ t, (γ₁ t).trans (γ₂ t)) := begin have h₁' := path.continuous_uncurry_extend_of_continuous_family γ₁ h₁, have h₂' := path.continuous_uncurry_extend_of_continuous_family γ₂ h₂, simp only [has_uncurry.uncurry, has_coe_to_fun.coe, coe_fn, path.trans, (∘)], refine continuous.if_le _ _ (continuous_subtype_coe.comp continuous_snd) continuous_const _, { change continuous ((λ p : ι × ℝ, (γ₁ p.1).extend p.2) ∘ (prod.map id (λ x, 2*x : I → ℝ))), exact h₁'.comp (continuous_id.prod_map $ continuous_const.mul continuous_subtype_coe) }, { change continuous ((λ p : ι × ℝ, (γ₂ p.1).extend p.2) ∘ (prod.map id (λ x, 2*x - 1 : I → ℝ))), exact h₂'.comp (continuous_id.prod_map $ (continuous_const.mul continuous_subtype_coe).sub continuous_const) }, { rintros st hst, simp [hst, mul_inv_cancel (@two_ne_zero ℝ _ _)] } end /-! #### Product of paths -/ section prod variables {a₁ a₂ a₃ : X} {b₁ b₂ b₃ : Y} /-- Given a path in `X` and a path in `Y`, we can take their pointwise product to get a path in `X × Y`. -/ protected def prod (γ₁ : path a₁ a₂) (γ₂ : path b₁ b₂) : path (a₁, b₁) (a₂, b₂) := { to_continuous_map := continuous_map.prod_mk γ₁.to_continuous_map γ₂.to_continuous_map, source' := by simp, target' := by simp, } @[simp] lemma prod_coe_fn (γ₁ : path a₁ a₂) (γ₂ : path b₁ b₂) : (coe_fn (γ₁.prod γ₂)) = λ t, (γ₁ t, γ₂ t) := rfl /-- Path composition commutes with products -/ lemma trans_prod_eq_prod_trans (γ₁ : path a₁ a₂) (δ₁ : path a₂ a₃) (γ₂ : path b₁ b₂) (δ₂ : path b₂ b₃) : (γ₁.prod γ₂).trans (δ₁.prod δ₂) = (γ₁.trans δ₁).prod (γ₂.trans δ₂) := begin ext t; unfold path.trans; simp only [path.coe_mk, path.prod_coe_fn, function.comp_app]; split_ifs; refl, end end prod section pi variables {χ : ι → Type*} [∀ i, topological_space (χ i)] {as bs cs : Π i, χ i} /-- Given a family of paths, one in each Xᵢ, we take their pointwise product to get a path in Π i, Xᵢ. -/ protected def pi (γ : Π i, path (as i) (bs i)) : path as bs := { to_continuous_map := continuous_map.pi (λ i, (γ i).to_continuous_map), source' := by simp, target' := by simp, } @[simp] lemma pi_coe_fn (γ : Π i, path (as i) (bs i)) : (coe_fn (path.pi γ)) = λ t i, γ i t := rfl /-- Path composition commutes with products -/ lemma trans_pi_eq_pi_trans (γ₀ : Π i, path (as i) (bs i)) (γ₁ : Π i, path (bs i) (cs i)) : (path.pi γ₀).trans (path.pi γ₁) = path.pi (λ i, (γ₀ i).trans (γ₁ i)) := begin ext t i, unfold path.trans, simp only [path.coe_mk, function.comp_app, pi_coe_fn], split_ifs; refl, end end pi /-! #### Pointwise multiplication/addition of two paths in a topological (additive) group -/ /-- Pointwise multiplication of paths in a topological group. The additive version is probably more useful. -/ @[to_additive "Pointwise addition of paths in a topological additive group."] protected def mul [has_mul X] [has_continuous_mul X] {a₁ b₁ a₂ b₂ : X} (γ₁ : path a₁ b₁) (γ₂ : path a₂ b₂) : path (a₁ * a₂) (b₁ * b₂) := (γ₁.prod γ₂).map continuous_mul @[to_additive] protected lemma mul_apply [has_mul X] [has_continuous_mul X] {a₁ b₁ a₂ b₂ : X} (γ₁ : path a₁ b₁) (γ₂ : path a₂ b₂) (t : unit_interval) : (γ₁.mul γ₂) t = γ₁ t * γ₂ t := rfl /-! #### Truncating a path -/ /-- `γ.truncate t₀ t₁` is the path which follows the path `γ` on the time interval `[t₀, t₁]` and stays still otherwise. -/ def truncate {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t₀ t₁ : ℝ) : path (γ.extend $ min t₀ t₁) (γ.extend t₁) := { to_fun := λ s, γ.extend (min (max s t₀) t₁), continuous_to_fun := γ.continuous_extend.comp ((continuous_subtype_coe.max continuous_const).min continuous_const), source' := begin simp only [min_def, max_def], norm_cast, split_ifs with h₁ h₂ h₃ h₄, { simp [γ.extend_of_le_zero h₁] }, { congr, linarith }, { have h₄ : t₁ ≤ 0 := le_of_lt (by simpa using h₂), simp [γ.extend_of_le_zero h₄, γ.extend_of_le_zero h₁] }, all_goals { refl } end, target' := begin simp only [min_def, max_def], norm_cast, split_ifs with h₁ h₂ h₃, { simp [γ.extend_of_one_le h₂] }, { refl }, { have h₄ : 1 ≤ t₀ := le_of_lt (by simpa using h₁), simp [γ.extend_of_one_le h₄, γ.extend_of_one_le (h₄.trans h₃)] }, { refl } end } /-- `γ.truncate_of_le t₀ t₁ h`, where `h : t₀ ≤ t₁` is `γ.truncate t₀ t₁` casted as a path from `γ.extend t₀` to `γ.extend t₁`. -/ def truncate_of_le {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t₀ t₁ : ℝ} (h : t₀ ≤ t₁) : path (γ.extend t₀) (γ.extend t₁) := (γ.truncate t₀ t₁).cast (by rw min_eq_left h) rfl lemma truncate_range {X : Type*} [topological_space X] {a b : X} (γ : path a b) {t₀ t₁ : ℝ} : range (γ.truncate t₀ t₁) ⊆ range γ := begin rw ← γ.extend_range, simp only [range_subset_iff, set_coe.exists, set_coe.forall], intros x hx, simp only [has_coe_to_fun.coe, coe_fn, path.truncate, mem_range_self] end /-- For a path `γ`, `γ.truncate` gives a "continuous family of paths", by which we mean the uncurried function which maps `(t₀, t₁, s)` to `γ.truncate t₀ t₁ s` is continuous. -/ @[continuity] lemma truncate_continuous_family {X : Type*} [topological_space X] {a b : X} (γ : path a b) : continuous (λ x, γ.truncate x.1 x.2.1 x.2.2 : ℝ × ℝ × I → X) := γ.continuous_extend.comp (((continuous_subtype_coe.comp (continuous_snd.comp continuous_snd)).max continuous_fst).min (continuous_fst.comp continuous_snd)) /- TODO : When `continuity` gets quicker, change the proof back to : `begin` `simp only [has_coe_to_fun.coe, coe_fn, path.truncate],` `continuity,` `exact continuous_subtype_coe` `end` -/ @[continuity] lemma truncate_const_continuous_family {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t : ℝ) : continuous ↿(γ.truncate t) := have key : continuous (λ x, (t, x) : ℝ × I → ℝ × ℝ × I) := continuous_const.prod_mk continuous_id, by convert γ.truncate_continuous_family.comp key @[simp] lemma truncate_self {X : Type*} [topological_space X] {a b : X} (γ : path a b) (t : ℝ) : γ.truncate t t = (path.refl $ γ.extend t).cast (by rw min_self) rfl := begin ext x, rw cast_coe, simp only [truncate, has_coe_to_fun.coe, coe_fn, refl, min_def, max_def], split_ifs with h₁ h₂; congr, exact le_antisymm ‹_› ‹_› end @[simp] lemma truncate_zero_zero {X : Type*} [topological_space X] {a b : X} (γ : path a b) : γ.truncate 0 0 = (path.refl a).cast (by rw [min_self, γ.extend_zero]) γ.extend_zero := by convert γ.truncate_self 0; exact γ.extend_zero.symm @[simp] lemma truncate_one_one {X : Type*} [topological_space X] {a b : X} (γ : path a b) : γ.truncate 1 1 = (path.refl b).cast (by rw [min_self, γ.extend_one]) γ.extend_one := by convert γ.truncate_self 1; exact γ.extend_one.symm @[simp] lemma truncate_zero_one {X : Type*} [topological_space X] {a b : X} (γ : path a b) : γ.truncate 0 1 = γ.cast (by simp [zero_le_one, extend_zero]) (by simp) := begin ext x, rw cast_coe, have : ↑x ∈ (Icc 0 1 : set ℝ) := x.2, rw [truncate, coe_mk, max_eq_left this.1, min_eq_left this.2, extend_extends'] end /-! #### Reparametrising a path -/ /-- Given a path `γ` and a function `f : I → I` where `f 0 = 0` and `f 1 = 1`, `γ.reparam f` is the path defined by `γ ∘ f`. -/ def reparam (γ : path x y) (f : I → I) (hfcont : continuous f) (hf₀ : f 0 = 0) (hf₁ : f 1 = 1) : path x y := { to_fun := γ ∘ f, continuous_to_fun := by continuity, source' := by simp [hf₀], target' := by simp [hf₁] } @[simp] lemma coe_to_fun (γ : path x y) {f : I → I} (hfcont : continuous f) (hf₀ : f 0 = 0) (hf₁ : f 1 = 1) : ⇑(γ.reparam f hfcont hf₀ hf₁) = γ ∘ f := rfl @[simp] lemma reparam_id (γ : path x y) : γ.reparam id continuous_id rfl rfl = γ := by { ext, refl } lemma range_reparam (γ : path x y) {f : I → I} (hfcont : continuous f) (hf₀ : f 0 = 0) (hf₁ : f 1 = 1) : range ⇑(γ.reparam f hfcont hf₀ hf₁) = range γ := begin change range (γ ∘ f) = range γ, have : range f = univ, { rw range_iff_surjective, intro t, have h₁ : continuous (Icc_extend (@zero_le_one ℝ _) f), { continuity }, have := intermediate_value_Icc (@zero_le_one ℝ _) h₁.continuous_on, { rw [Icc_extend_left, Icc_extend_right] at this, change Icc (f 0) (f 1) ⊆ _ at this, rw [hf₀, hf₁] at this, rcases this t.2 with ⟨w, hw₁, hw₂⟩, rw Icc_extend_of_mem _ _ hw₁ at hw₂, use [⟨w, hw₁⟩, hw₂] } }, rw [range_comp, this, image_univ], end lemma refl_reparam {f : I → I} (hfcont : continuous f) (hf₀ : f 0 = 0) (hf₁ : f 1 = 1) : (refl x).reparam f hfcont hf₀ hf₁ = refl x := begin ext, simp, end end path /-! ### Being joined by a path -/ /-- The relation "being joined by a path". This is an equivalence relation. -/ def joined (x y : X) : Prop := nonempty (path x y) @[refl] lemma joined.refl (x : X) : joined x x := ⟨path.refl x⟩ /-- When two points are joined, choose some path from `x` to `y`. -/ def joined.some_path (h : joined x y) : path x y := nonempty.some h @[symm] lemma joined.symm {x y : X} (h : joined x y) : joined y x := ⟨h.some_path.symm⟩ @[trans] lemma joined.trans {x y z : X} (hxy : joined x y) (hyz : joined y z) : joined x z := ⟨hxy.some_path.trans hyz.some_path⟩ variables (X) /-- The setoid corresponding the equivalence relation of being joined by a continuous path. -/ def path_setoid : setoid X := { r := joined, iseqv := mk_equivalence _ joined.refl (λ x y, joined.symm) (λ x y z, joined.trans) } /-- The quotient type of points of a topological space modulo being joined by a continuous path. -/ def zeroth_homotopy := quotient (path_setoid X) instance : inhabited (zeroth_homotopy ℝ) := ⟨@quotient.mk ℝ (path_setoid ℝ) 0⟩ variables {X} /-! ### Being joined by a path inside a set -/ /-- The relation "being joined by a path in `F`". Not quite an equivalence relation since it's not reflexive for points that do not belong to `F`. -/ def joined_in (F : set X) (x y : X) : Prop := ∃ γ : path x y, ∀ t, γ t ∈ F variables {F : set X} lemma joined_in.mem (h : joined_in F x y) : x ∈ F ∧ y ∈ F := begin rcases h with ⟨γ, γ_in⟩, have : γ 0 ∈ F ∧ γ 1 ∈ F, by { split; apply γ_in }, simpa using this end lemma joined_in.source_mem (h : joined_in F x y) : x ∈ F := h.mem.1 lemma joined_in.target_mem (h : joined_in F x y) : y ∈ F := h.mem.2 /-- When `x` and `y` are joined in `F`, choose a path from `x` to `y` inside `F` -/ def joined_in.some_path (h : joined_in F x y) : path x y := classical.some h lemma joined_in.some_path_mem (h : joined_in F x y) (t : I) : h.some_path t ∈ F := classical.some_spec h t /-- If `x` and `y` are joined in the set `F`, then they are joined in the subtype `F`. -/ lemma joined_in.joined_subtype (h : joined_in F x y) : joined (⟨x, h.source_mem⟩ : F) (⟨y, h.target_mem⟩ : F) := ⟨{ to_fun := λ t, ⟨h.some_path t, h.some_path_mem t⟩, continuous_to_fun := by continuity, source' := by simp, target' := by simp }⟩ lemma joined_in.of_line {f : ℝ → X} (hf : continuous_on f I) (h₀ : f 0 = x) (h₁ : f 1 = y) (hF : f '' I ⊆ F) : joined_in F x y := ⟨path.of_line hf h₀ h₁, λ t, hF $ path.of_line_mem hf h₀ h₁ t⟩ lemma joined_in.joined (h : joined_in F x y) : joined x y := ⟨h.some_path⟩ lemma joined_in_iff_joined (x_in : x ∈ F) (y_in : y ∈ F) : joined_in F x y ↔ joined (⟨x, x_in⟩ : F) (⟨y, y_in⟩ : F) := ⟨λ h, h.joined_subtype, λ h, ⟨h.some_path.map continuous_subtype_coe, by simp⟩⟩ @[simp] lemma joined_in_univ : joined_in univ x y ↔ joined x y := by simp [joined_in, joined, exists_true_iff_nonempty] lemma joined_in.mono {U V : set X} (h : joined_in U x y) (hUV : U ⊆ V) : joined_in V x y := ⟨h.some_path, λ t, hUV (h.some_path_mem t)⟩ lemma joined_in.refl (h : x ∈ F) : joined_in F x x := ⟨path.refl x, λ t, h⟩ @[symm] lemma joined_in.symm (h : joined_in F x y) : joined_in F y x := begin cases h.mem with hx hy, simp [joined_in_iff_joined, *] at *, exact h.symm end lemma joined_in.trans (hxy : joined_in F x y) (hyz : joined_in F y z) : joined_in F x z := begin cases hxy.mem with hx hy, cases hyz.mem with hx hy, simp [joined_in_iff_joined, *] at *, exact hxy.trans hyz end /-! ### Path component -/ /-- The path component of `x` is the set of points that can be joined to `x`. -/ def path_component (x : X) := {y | joined x y} @[simp] lemma mem_path_component_self (x : X) : x ∈ path_component x := joined.refl x @[simp] lemma path_component.nonempty (x : X) : (path_component x).nonempty := ⟨x, mem_path_component_self x⟩ lemma mem_path_component_of_mem (h : x ∈ path_component y) : y ∈ path_component x := joined.symm h lemma path_component_symm : x ∈ path_component y ↔ y ∈ path_component x := ⟨λ h, mem_path_component_of_mem h, λ h, mem_path_component_of_mem h⟩ lemma path_component_congr (h : x ∈ path_component y) : path_component x = path_component y := begin ext z, split, { intro h', rw path_component_symm, exact (h.trans h').symm }, { intro h', rw path_component_symm at h' ⊢, exact h'.trans h }, end lemma path_component_subset_component (x : X) : path_component x ⊆ connected_component x := λ y h, (is_connected_range h.some_path.continuous).subset_connected_component ⟨0, by simp⟩ ⟨1, by simp⟩ /-- The path component of `x` in `F` is the set of points that can be joined to `x` in `F`. -/ def path_component_in (x : X) (F : set X) := {y | joined_in F x y} @[simp] lemma path_component_in_univ (x : X) : path_component_in x univ = path_component x := by simp [path_component_in, path_component, joined_in, joined, exists_true_iff_nonempty] lemma joined.mem_path_component (hyz : joined y z) (hxy : y ∈ path_component x) : z ∈ path_component x := hxy.trans hyz /-! ### Path connected sets -/ /-- A set `F` is path connected if it contains a point that can be joined to all other in `F`. -/ def is_path_connected (F : set X) : Prop := ∃ x ∈ F, ∀ {y}, y ∈ F → joined_in F x y lemma is_path_connected_iff_eq : is_path_connected F ↔ ∃ x ∈ F, path_component_in x F = F := begin split ; rintros ⟨x, x_in, h⟩ ; use [x, x_in], { ext y, exact ⟨λ hy, hy.mem.2, h⟩ }, { intros y y_in, rwa ← h at y_in }, end lemma is_path_connected.joined_in (h : is_path_connected F) : ∀ x y ∈ F, joined_in F x y := λ x x_in x y_in, let ⟨b, b_in, hb⟩ := h in (hb x_in).symm.trans (hb y_in) lemma is_path_connected_iff : is_path_connected F ↔ F.nonempty ∧ ∀ x y ∈ F, joined_in F x y := ⟨λ h, ⟨let ⟨b, b_in, hb⟩ := h in ⟨b, b_in⟩, h.joined_in⟩, λ ⟨⟨b, b_in⟩, h⟩, ⟨b, b_in, λ x x_in, h b b_in x x_in⟩⟩ lemma is_path_connected.image {Y : Type*} [topological_space Y] (hF : is_path_connected F) {f : X → Y} (hf : continuous f) : is_path_connected (f '' F) := begin rcases hF with ⟨x, x_in, hx⟩, use [f x, mem_image_of_mem f x_in], rintros _ ⟨y, y_in, rfl⟩, exact ⟨(hx y_in).some_path.map hf, λ t, ⟨_, (hx y_in).some_path_mem t, rfl⟩⟩, end lemma is_path_connected.mem_path_component (h : is_path_connected F) (x_in : x ∈ F) (y_in : y ∈ F) : y ∈ path_component x := (h.joined_in x x_in y y_in).joined lemma is_path_connected.subset_path_component (h : is_path_connected F) (x_in : x ∈ F) : F ⊆ path_component x := λ y y_in, h.mem_path_component x_in y_in lemma is_path_connected.union {U V : set X} (hU : is_path_connected U) (hV : is_path_connected V) (hUV : (U ∩ V).nonempty) : is_path_connected (U ∪ V) := begin rcases hUV with ⟨x, xU, xV⟩, use [x, or.inl xU], rintros y (yU | yV), { exact (hU.joined_in x xU y yU).mono (subset_union_left U V) }, { exact (hV.joined_in x xV y yV).mono (subset_union_right U V) }, end /-- If a set `W` is path-connected, then it is also path-connected when seen as a set in a smaller ambient type `U` (when `U` contains `W`). -/ lemma is_path_connected.preimage_coe {U W : set X} (hW : is_path_connected W) (hWU : W ⊆ U) : is_path_connected ((coe : U → X) ⁻¹' W) := begin rcases hW with ⟨x, x_in, hx⟩, use [⟨x, hWU x_in⟩, by simp [x_in]], rintros ⟨y, hyU⟩ hyW, exact ⟨(hx hyW).joined_subtype.some_path.map (continuous_inclusion hWU), by simp⟩ end lemma is_path_connected.exists_path_through_family {X : Type*} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n+1) → X) (hp : ∀ i, p i ∈ s) : ∃ γ : path (p 0) (p n), (range γ ⊆ s) ∧ (∀ i, p i ∈ range γ) := begin let p' : ℕ → X := λ k, if h : k < n+1 then p ⟨k, h⟩ else p ⟨0, n.zero_lt_succ⟩, obtain ⟨γ, hγ⟩ : ∃ (γ : path (p' 0) (p' n)), (∀ i ≤ n, p' i ∈ range γ) ∧ range γ ⊆ s, { have hp' : ∀ i ≤ n, p' i ∈ s, { intros i hi, simp [p', nat.lt_succ_of_le hi, hp] }, clear_value p', clear hp p, induction n with n hn, { use path.refl (p' 0), { split, { rintros i hi, rw nat.le_zero_iff.mp hi, exact ⟨0, rfl⟩ }, { rw range_subset_iff, rintros x, exact hp' 0 le_rfl } } }, { rcases hn (λ i hi, hp' i $ nat.le_succ_of_le hi) with ⟨γ₀, hγ₀⟩, rcases h.joined_in (p' n) (hp' n n.le_succ) (p' $ n+1) (hp' (n+1) $ le_rfl) with ⟨γ₁, hγ₁⟩, let γ : path (p' 0) (p' $ n+1) := γ₀.trans γ₁, use γ, have range_eq : range γ = range γ₀ ∪ range γ₁ := γ₀.trans_range γ₁, split, { rintros i hi, by_cases hi' : i ≤ n, { rw range_eq, left, exact hγ₀.1 i hi' }, { rw [not_le, ← nat.succ_le_iff] at hi', have : i = n.succ := by linarith, rw this, use 1, exact γ.target } }, { rw range_eq, apply union_subset hγ₀.2, rw range_subset_iff, exact hγ₁ } } }, have hpp' : ∀ k < n+1, p k = p' k, { intros k hk, simp only [p', hk, dif_pos], congr, ext, rw fin.coe_coe_of_lt hk, norm_cast }, use γ.cast (hpp' 0 n.zero_lt_succ) (hpp' n n.lt_succ_self), simp only [γ.cast_coe], refine and.intro hγ.2 _, rintros ⟨i, hi⟩, convert hγ.1 i (nat.le_of_lt_succ hi), rw ← hpp' i hi, congr, ext, rw fin.coe_coe_of_lt hi, norm_cast end lemma is_path_connected.exists_path_through_family' {X : Type*} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n+1) → X) (hp : ∀ i, p i ∈ s) : ∃ (γ : path (p 0) (p n)) (t : fin (n + 1) → I), (∀ t, γ t ∈ s) ∧ ∀ i, γ (t i) = p i := begin rcases h.exists_path_through_family p hp with ⟨γ, hγ⟩, rcases hγ with ⟨h₁, h₂⟩, simp only [range, mem_set_of_eq] at h₂, rw range_subset_iff at h₁, choose! t ht using h₂, exact ⟨γ, t, h₁, ht⟩ end /-! ### Path connected spaces -/ /-- A topological space is path-connected if it is non-empty and every two points can be joined by a continuous path. -/ class path_connected_space (X : Type*) [topological_space X] : Prop := (nonempty : nonempty X) (joined : ∀ x y : X, joined x y) attribute [instance, priority 50] path_connected_space.nonempty lemma path_connected_space_iff_zeroth_homotopy : path_connected_space X ↔ nonempty (zeroth_homotopy X) ∧ subsingleton (zeroth_homotopy X) := begin letI := path_setoid X, split, { introI h, refine ⟨(nonempty_quotient_iff _).mpr h.1, ⟨_⟩⟩, rintros ⟨x⟩ ⟨y⟩, exact quotient.sound (path_connected_space.joined x y) }, { unfold zeroth_homotopy, rintros ⟨h, h'⟩, resetI, exact ⟨(nonempty_quotient_iff _).mp h, λ x y, quotient.exact $ subsingleton.elim ⟦x⟧ ⟦y⟧⟩ }, end namespace path_connected_space variables [path_connected_space X] /-- Use path-connectedness to build a path between two points. -/ def some_path (x y : X) : path x y := nonempty.some (joined x y) end path_connected_space lemma is_path_connected_iff_path_connected_space : is_path_connected F ↔ path_connected_space F := begin rw is_path_connected_iff, split, { rintro ⟨⟨x, x_in⟩, h⟩, refine ⟨⟨⟨x, x_in⟩⟩, _⟩, rintros ⟨y, y_in⟩ ⟨z, z_in⟩, have H := h y y_in z z_in, rwa joined_in_iff_joined y_in z_in at H }, { rintros ⟨⟨x, x_in⟩, H⟩, refine ⟨⟨x, x_in⟩, λ y y_in z z_in, _⟩, rw joined_in_iff_joined y_in z_in, apply H } end lemma path_connected_space_iff_univ : path_connected_space X ↔ is_path_connected (univ : set X) := begin split, { introI h, inhabit X, refine ⟨default, mem_univ _, _⟩, simpa using path_connected_space.joined default }, { intro h, have h' := h.joined_in, cases h with x h, exact ⟨⟨x⟩, by simpa using h'⟩ }, end lemma path_connected_space_iff_eq : path_connected_space X ↔ ∃ x : X, path_component x = univ := by simp [path_connected_space_iff_univ, is_path_connected_iff_eq] @[priority 100] -- see Note [lower instance priority] instance path_connected_space.connected_space [path_connected_space X] : connected_space X := begin rw connected_space_iff_connected_component, rcases is_path_connected_iff_eq.mp (path_connected_space_iff_univ.mp ‹_›) with ⟨x, x_in, hx⟩, use x, rw ← univ_subset_iff, exact (by simpa using hx : path_component x = univ) ▸ path_component_subset_component x end lemma is_path_connected.is_connected (hF : is_path_connected F) : is_connected F := begin rw is_connected_iff_connected_space, rw is_path_connected_iff_path_connected_space at hF, exact @path_connected_space.connected_space _ _ hF end namespace path_connected_space variables [path_connected_space X] lemma exists_path_through_family {n : ℕ} (p : fin (n+1) → X) : ∃ γ : path (p 0) (p n), (∀ i, p i ∈ range γ) := begin have : is_path_connected (univ : set X) := path_connected_space_iff_univ.mp (by apply_instance), rcases this.exists_path_through_family p (λ i, true.intro) with ⟨γ, -, h⟩, exact ⟨γ, h⟩ end lemma exists_path_through_family' {n : ℕ} (p : fin (n+1) → X) : ∃ (γ : path (p 0) (p n)) (t : fin (n + 1) → I), ∀ i, γ (t i) = p i := begin have : is_path_connected (univ : set X) := path_connected_space_iff_univ.mp (by apply_instance), rcases this.exists_path_through_family' p (λ i, true.intro) with ⟨γ, t, -, h⟩, exact ⟨γ, t, h⟩ end end path_connected_space /-! ### Locally path connected spaces -/ /-- A topological space is locally path connected, at every point, path connected neighborhoods form a neighborhood basis. -/ class loc_path_connected_space (X : Type*) [topological_space X] : Prop := (path_connected_basis : ∀ x : X, (𝓝 x).has_basis (λ s : set X, s ∈ 𝓝 x ∧ is_path_connected s) id) export loc_path_connected_space (path_connected_basis) lemma loc_path_connected_of_bases {p : ι → Prop} {s : X → ι → set X} (h : ∀ x, (𝓝 x).has_basis p (s x)) (h' : ∀ x i, p i → is_path_connected (s x i)) : loc_path_connected_space X := begin constructor, intro x, apply (h x).to_has_basis, { intros i pi, exact ⟨s x i, ⟨(h x).mem_of_mem pi, h' x i pi⟩, by refl⟩ }, { rintros U ⟨U_in, hU⟩, rcases (h x).mem_iff.mp U_in with ⟨i, pi, hi⟩, tauto } end lemma path_connected_space_iff_connected_space [loc_path_connected_space X] : path_connected_space X ↔ connected_space X := begin split, { introI h, apply_instance }, { introI hX, rw path_connected_space_iff_eq, use (classical.arbitrary X), refine eq_univ_of_nonempty_clopen (by simp) ⟨_, _⟩, { rw is_open_iff_mem_nhds, intros y y_in, rcases (path_connected_basis y).ex_mem with ⟨U, ⟨U_in, hU⟩⟩, apply mem_of_superset U_in, rw ← path_component_congr y_in, exact hU.subset_path_component (mem_of_mem_nhds U_in) }, { rw is_closed_iff_nhds, intros y H, rcases (path_connected_basis y).ex_mem with ⟨U, ⟨U_in, hU⟩⟩, rcases H U U_in with ⟨z, hz, hz'⟩, exact ((hU.joined_in z hz y $ mem_of_mem_nhds U_in).joined.mem_path_component hz') } }, end lemma path_connected_subset_basis [loc_path_connected_space X] {U : set X} (h : is_open U) (hx : x ∈ U) : (𝓝 x).has_basis (λ s : set X, s ∈ 𝓝 x ∧ is_path_connected s ∧ s ⊆ U) id := (path_connected_basis x).has_basis_self_subset (is_open.mem_nhds h hx) lemma loc_path_connected_of_is_open [loc_path_connected_space X] {U : set X} (h : is_open U) : loc_path_connected_space U := ⟨begin rintros ⟨x, x_in⟩, rw nhds_subtype_eq_comap, constructor, intros V, rw (has_basis.comap (coe : U → X) (path_connected_subset_basis h x_in)).mem_iff, split, { rintros ⟨W, ⟨W_in, hW, hWU⟩, hWV⟩, exact ⟨coe ⁻¹' W, ⟨⟨preimage_mem_comap W_in, hW.preimage_coe hWU⟩, hWV⟩⟩ }, { rintros ⟨W, ⟨W_in, hW⟩, hWV⟩, refine ⟨coe '' W, ⟨filter.image_coe_mem_of_mem_comap (is_open.mem_nhds h x_in) W_in, hW.image continuous_subtype_coe, subtype.coe_image_subset U W⟩, _⟩, rintros x ⟨y, ⟨y_in, hy⟩⟩, rw ← subtype.coe_injective hy, tauto }, end⟩ lemma is_open.is_connected_iff_is_path_connected [loc_path_connected_space X] {U : set X} (U_op : is_open U) : is_path_connected U ↔ is_connected U := begin rw [is_connected_iff_connected_space, is_path_connected_iff_path_connected_space], haveI := loc_path_connected_of_is_open U_op, exact path_connected_space_iff_connected_space end
c4721a01fa7940e1c10219de339f21867401b55e
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/preadditive/injective.lean
0f4b579d247b3ce21a1e332b95b12a7d54a98399
[ "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
10,584
lean
/- Copyright (c) 2022 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang, Kevin Buzzard -/ import algebra.homology.exact import category_theory.types import category_theory.preadditive.projective import category_theory.limits.shapes.biproducts /-! # Injective objects and categories with enough injectives An object `J` is injective iff every morphism into `J` can be obtained by extending a monomorphism. -/ noncomputable theory open category_theory open category_theory.limits open opposite universes v v₁ v₂ u₁ u₂ namespace category_theory variables {C : Type u₁} [category.{v₁} C] /-- An object `J` is injective iff every morphism into `J` can be obtained by extending a monomorphism. -/ class injective (J : C) : Prop := (factors : ∀ {X Y : C} (g : X ⟶ J) (f : X ⟶ Y) [mono f], ∃ h : Y ⟶ J, f ≫ h = g) section /-- An injective presentation of an object `X` consists of a monomorphism `f : X ⟶ J` to some injective object `J`. -/ @[nolint has_nonempty_instance] structure injective_presentation (X : C) := (J : C) (injective : injective J . tactic.apply_instance) (f : X ⟶ J) (mono : mono f . tactic.apply_instance) variables (C) /-- A category "has enough injectives" if every object has an injective presentation, i.e. if for every object `X` there is an injective object `J` and a monomorphism `X ↪ J`. -/ class enough_injectives : Prop := (presentation : ∀ (X : C), nonempty (injective_presentation X)) end namespace injective /-- Let `J` be injective and `g` a morphism into `J`, then `g` can be factored through any monomorphism. -/ def factor_thru {J X Y : C} [injective J] (g : X ⟶ J) (f : X ⟶ Y) [mono f] : Y ⟶ J := (injective.factors g f).some @[simp] lemma comp_factor_thru {J X Y : C} [injective J] (g : X ⟶ J) (f : X ⟶ Y) [mono f] : f ≫ factor_thru g f = g := (injective.factors g f).some_spec section open_locale zero_object instance zero_injective [has_zero_object C] [has_zero_morphisms C] : injective (0 : C) := { factors := λ X Y g f mono, ⟨0, by ext⟩ } end lemma of_iso {P Q : C} (i : P ≅ Q) (hP : injective P) : injective Q := { factors := λ X Y g f mono, begin obtain ⟨h, h_eq⟩ := @injective.factors C _ P _ _ _ (g ≫ i.inv) f mono, refine ⟨h ≫ i.hom, _⟩, rw [←category.assoc, h_eq, category.assoc, iso.inv_hom_id, category.comp_id], end } lemma iso_iff {P Q : C} (i : P ≅ Q) : injective P ↔ injective Q := ⟨of_iso i, of_iso i.symm⟩ /-- The axiom of choice says that every nonempty type is an injective object in `Type`. -/ instance (X : Type u₁) [nonempty X] : injective X := { factors := λ Y Z g f mono, ⟨λ z, by classical; exact if h : z ∈ set.range f then g (classical.some h) else nonempty.some infer_instance, begin ext y, change dite _ _ _ = _, split_ifs, { rw mono_iff_injective at mono, rw mono (classical.some_spec h) }, { exact false.elim (h ⟨y, rfl⟩) }, end⟩ } instance Type.enough_injectives : enough_injectives (Type u₁) := { presentation := λ X, nonempty.intro { J := with_bot X, injective := infer_instance, f := option.some, mono := by { rw [mono_iff_injective], exact option.some_injective X, } } } instance {P Q : C} [has_binary_product P Q] [injective P] [injective Q] : injective (P ⨯ Q) := { factors := λ X Y g f mono, begin resetI, use limits.prod.lift (factor_thru (g ≫ limits.prod.fst) f) (factor_thru (g ≫ limits.prod.snd) f), simp only [prod.comp_lift, comp_factor_thru], ext, { simp only [prod.lift_fst] }, { simp only [prod.lift_snd] }, end } instance {β : Type v} (c : β → C) [has_product c] [∀ b, injective (c b)] : injective (∏ c) := { factors := λ X Y g f mono, begin resetI, refine ⟨pi.lift (λ b, factor_thru (g ≫ (pi.π c _)) f), _⟩, ext ⟨j⟩, simp only [category.assoc, limit.lift_π, fan.mk_π_app, comp_factor_thru], end } instance {P Q : C} [has_zero_morphisms C] [has_binary_biproduct P Q] [injective P] [injective Q] : injective (P ⊞ Q) := { factors := λ X Y g f mono, begin resetI, refine ⟨biprod.lift (factor_thru (g ≫ biprod.fst) f) (factor_thru (g ≫ biprod.snd) f), _⟩, ext, { simp only [category.assoc, biprod.lift_fst, comp_factor_thru] }, { simp only [category.assoc, biprod.lift_snd, comp_factor_thru] }, end } instance {β : Type v} (c : β → C) [has_zero_morphisms C] [has_biproduct c] [∀ b, injective (c b)] : injective (⨁ c) := { factors := λ X Y g f mono, begin resetI, refine ⟨biproduct.lift (λ b, factor_thru (g ≫ biproduct.π _ _) f), _⟩, ext, simp only [category.assoc, biproduct.lift_π, comp_factor_thru], end } instance {P : Cᵒᵖ} [projective P] : injective (unop P) := { factors := λ X Y g f mono, by exactI ⟨(@projective.factor_thru Cᵒᵖ _ P _ _ _ g.op f.op _).unop, quiver.hom.op_inj (by simp)⟩ } instance {J : Cᵒᵖ} [injective J] : projective (unop J) := { factors := λ E X f e he, by exactI ⟨(@factor_thru Cᵒᵖ _ J _ _ _ f.op e.op _).unop, quiver.hom.op_inj (by simp)⟩ } instance {J : C} [injective J] : projective (op J) := { factors := λ E X f e epi, by exactI ⟨(@factor_thru C _ J _ _ _ f.unop e.unop _).op, quiver.hom.unop_inj (by simp)⟩ } instance {P : C} [projective P] : injective (op P) := { factors := λ X Y g f mono, by exactI ⟨(@projective.factor_thru C _ P _ _ _ g.unop f.unop _).op, quiver.hom.unop_inj (by simp)⟩ } lemma injective_iff_projective_op {J : C} : injective J ↔ projective (op J) := ⟨λ h, by exactI infer_instance, λ h, show injective (unop (op J)), by exactI infer_instance⟩ lemma projective_iff_injective_op {P : C} : projective P ↔ injective (op P) := ⟨λ h, by exactI infer_instance, λ h, show projective (unop (op P)), by exactI infer_instance⟩ lemma injective_iff_preserves_epimorphisms_yoneda_obj (J : C) : injective J ↔ (yoneda.obj J).preserves_epimorphisms := begin rw [injective_iff_projective_op, projective.projective_iff_preserves_epimorphisms_coyoneda_obj], exact functor.preserves_epimorphisms.iso_iff (coyoneda.obj_op_op _) end section adjunction open category_theory.functor variables {D : Type u₂} [category.{v₂} D] variables {L : C ⥤ D} {R : D ⥤ C} [preserves_monomorphisms L] lemma injective_of_adjoint (adj : L ⊣ R) (J : D) [injective J] : injective $ R.obj J := ⟨λ A A' g f im, by exactI ⟨adj.hom_equiv _ _ (factor_thru ((adj.hom_equiv A J).symm g) (L.map f)), (adj.hom_equiv _ _).symm.injective (by simp)⟩⟩ end adjunction section preadditive variables [preadditive C] lemma injective_iff_preserves_epimorphisms_preadditive_yoneda_obj (J : C) : injective J ↔ (preadditive_yoneda.obj J).preserves_epimorphisms := begin rw injective_iff_preserves_epimorphisms_yoneda_obj, refine ⟨λ (h : (preadditive_yoneda.obj J ⋙ (forget _)).preserves_epimorphisms), _, _⟩, { exactI functor.preserves_epimorphisms_of_preserves_of_reflects (preadditive_yoneda.obj J) (forget _) }, { introI, exact (infer_instance : (preadditive_yoneda.obj J ⋙ forget _).preserves_epimorphisms) } end lemma injective_iff_preserves_epimorphisms_preadditive_yoneda_obj' (J : C) : injective J ↔ (preadditive_yoneda_obj J).preserves_epimorphisms := begin rw injective_iff_preserves_epimorphisms_yoneda_obj, refine ⟨λ (h : (preadditive_yoneda_obj J ⋙ (forget _)).preserves_epimorphisms), _, _⟩, { exactI functor.preserves_epimorphisms_of_preserves_of_reflects (preadditive_yoneda_obj J) (forget _) }, { introI, exact (infer_instance : (preadditive_yoneda_obj J ⋙ forget _).preserves_epimorphisms) } end end preadditive section enough_injectives variable [enough_injectives C] /-- `injective.under X` provides an arbitrarily chosen injective object equipped with an monomorphism `injective.ι : X ⟶ injective.under X`. -/ def under (X : C) : C := (enough_injectives.presentation X).some.J instance injective_under (X : C) : injective (under X) := (enough_injectives.presentation X).some.injective /-- The monomorphism `injective.ι : X ⟶ injective.under X` from the arbitrarily chosen injective object under `X`. -/ def ι (X : C) : X ⟶ under X := (enough_injectives.presentation X).some.f instance ι_mono (X : C) : mono (ι X) := (enough_injectives.presentation X).some.mono section variables [has_zero_morphisms C] {X Y : C} (f : X ⟶ Y) [has_cokernel f] /-- When `C` has enough injectives, the object `injective.syzygies f` is an arbitrarily chosen injective object under `cokernel f`. -/ @[derive injective] def syzygies : C := under (cokernel f) /-- When `C` has enough injective, `injective.d f : Y ⟶ syzygies f` is the composition `cokernel.π f ≫ ι (cokernel f)`. (When `C` is abelian, we have `exact f (injective.d f)`.) -/ abbreviation d : Y ⟶ syzygies f := cokernel.π f ≫ ι (cokernel f) end end enough_injectives instance [enough_injectives C] : enough_projectives Cᵒᵖ := ⟨λ X, ⟨⟨_, infer_instance, (injective.ι (unop X)).op, infer_instance⟩⟩⟩ instance [enough_projectives C] : enough_injectives Cᵒᵖ := ⟨λ X, ⟨⟨_, infer_instance, (projective.π (unop X)).op, infer_instance⟩⟩⟩ lemma enough_projectives_of_enough_injectives_op [enough_injectives Cᵒᵖ] : enough_projectives C := ⟨λ X, ⟨⟨_, infer_instance, (injective.ι (op X)).unop, infer_instance⟩⟩⟩ lemma enough_injectives_of_enough_projectives_op [enough_projectives Cᵒᵖ] : enough_injectives C := ⟨λ X, ⟨⟨_, infer_instance, (projective.π (op X)).unop, infer_instance⟩⟩⟩ open injective section variables [has_zero_morphisms C] [has_images Cᵒᵖ] [has_equalizers Cᵒᵖ] /-- Given a pair of exact morphism `f : Q ⟶ R` and `g : R ⟶ S` and a map `h : R ⟶ J` to an injective object `J` such that `f ≫ h = 0`, then `g` descents to a map `S ⟶ J`. See below: ``` Q --- f --> R --- g --> S | | h v J ``` -/ def exact.desc {J Q R S : C} [injective J] (h : R ⟶ J) (f : Q ⟶ R) (g : R ⟶ S) (hgf : exact g.op f.op) (w : f ≫ h = 0) : S ⟶ J := (exact.lift h.op g.op f.op hgf (congr_arg quiver.hom.op w)).unop @[simp] lemma exact.comp_desc {J Q R S : C} [injective J] (h : R ⟶ J) (f : Q ⟶ R) (g : R ⟶ S) (hgf : exact g.op f.op) (w : f ≫ h = 0) : g ≫ exact.desc h f g hgf w = h := by convert congr_arg quiver.hom.unop (exact.lift_comp h.op g.op f.op hgf (congr_arg quiver.hom.op w)) end end injective end category_theory
9007a5e3e31d0413ea76d90d378b2569965b6cbf
ce89339993655da64b6ccb555c837ce6c10f9ef4
/bluejam/chap4_exercise1.lean
1337a16aded0d4c6c2dd2cb580497c39e664a0fc
[]
no_license
zeptometer/LearnLean
ef32dc36a22119f18d843f548d0bb42f907bff5d
bb84d5dbe521127ba134d4dbf9559b294a80b9f7
refs/heads/master
1,625,710,824,322
1,601,382,570,000
1,601,382,570,000
195,228,870
2
0
null
null
null
null
UTF-8
Lean
false
false
1,363
lean
variables (α : Type) (p q : α → Prop) example : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := iff.intro ( assume h: ∀ x, (p x ∧ q x), have ∀ x, p x, from ( assume x: α, show p x, from (h x).left ), have ∀ x, q x, from ( assume x: α , show q x, from (h x).right ), and.intro ‹ ∀ x, p x › ‹ ∀ x, q x › ) ( assume h: (∀ x, p x) ∧ (∀ x, q x), assume y: α, have p y, from h.left y, have q y, from h.right y, show p y ∧ q y, from and.intro ‹ p y › ‹ q y › ) example : (∀ x, p x → q x) → (∀ x, p x) → (∀ x, q x) := assume h: (∀ x, p x → q x), assume hp: (∀ x, p x), assume y: α, show q y, from (h y) (hp y) example : (∀ x, p x) ∨ (∀ x, q x) → ∀ x, p x ∨ q x := assume h: (∀ x, p x) ∨ (∀ x, q x), or.elim h ( assume hl: ∀ x, p x, assume y: α, have p y, from hl y, or.intro_left (q y) this ) ( assume hr: ∀ x, q x, assume y: α, have q y, from hr y, or.intro_right (p y) this )
63c25dcde5a4e0a8210783c955d28e0041a43bb9
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/tactic/monotonicity/interactive.lean
35893af4fc997c06b940fc6d4f5f857bd18dc613
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
21,467
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 tactic.monotonicity.basic import category.basic import category.traversable import category.traversable.derive import data.dlist import logic.basic import tactic.core variables {a b c p : Prop} namespace tactic.interactive open lean lean.parser interactive open interactive.types open tactic local postfix `?`:9001 := optional local postfix *:9001 := many meta inductive mono_function (elab : bool := tt) | non_assoc : expr elab → list (expr elab) → list (expr elab) → mono_function | assoc : expr elab → option (expr elab) → option (expr elab) → mono_function | assoc_comm : expr elab → expr elab → mono_function meta instance : decidable_eq mono_function := by mk_dec_eq_instance meta def mono_function.to_tactic_format : mono_function → tactic format | (mono_function.non_assoc fn xs ys) := do fn' ← pp fn, xs' ← mmap pp xs, ys' ← mmap pp ys, return format!"{fn'} {xs'} _ {ys'}" | (mono_function.assoc fn xs ys) := do fn' ← pp fn, xs' ← pp xs, ys' ← pp ys, return format!"{fn'} {xs'} _ {ys'}" | (mono_function.assoc_comm fn xs) := do fn' ← pp fn, xs' ← pp xs, return format!"{fn'} _ {xs'}" meta instance has_to_tactic_format_mono_function : has_to_tactic_format mono_function := { to_tactic_format := mono_function.to_tactic_format } @[derive traversable] meta structure ac_mono_ctx' (rel : Type) := (to_rel : rel) (function : mono_function) (left right rel_def : expr) @[reducible] meta def ac_mono_ctx := ac_mono_ctx' (option (expr → expr → expr)) @[reducible] meta def ac_mono_ctx_ne := ac_mono_ctx' (expr → expr → expr) meta def ac_mono_ctx.to_tactic_format (ctx : ac_mono_ctx) : tactic format := do fn ← pp ctx.function, l ← pp ctx.left, r ← pp ctx.right, rel ← pp ctx.rel_def, return format!"{{ function := {fn}\n, left := {l}\n, right := {r}\n, rel_def := {rel} }" meta instance has_to_tactic_format_mono_ctx : has_to_tactic_format ac_mono_ctx := { to_tactic_format := ac_mono_ctx.to_tactic_format } meta def as_goal (e : expr) (tac : tactic unit) : tactic unit := do gs ← get_goals, set_goals [e], tac, set_goals gs open list (hiding map) functor dlist section config parameter opt : mono_cfg parameter asms : list expr meta def unify_with_instance (e : expr) : tactic unit := as_goal e $ apply_instance <|> apply_opt_param <|> apply_auto_param <|> tactic.solve_by_elim { assumptions := pure asms } <|> reflexivity <|> applyc ``id <|> return () private meta def match_rule_head (p : expr) : list expr → expr → expr → tactic expr | vs e t := (unify t p >> mmap' unify_with_instance vs >> instantiate_mvars e) <|> do (expr.pi _ _ d b) ← return t | failed, v ← mk_meta_var d, match_rule_head (v::vs) (expr.app e v) (b.instantiate_var v) meta def pi_head : expr → tactic expr | (expr.pi n _ t b) := do v ← mk_meta_var t, pi_head (b.instantiate_var v) | e := return e meta def delete_expr (e : expr) : list expr → tactic (option (list expr)) | [] := return none | (x :: xs) := (compare opt e x >> return (some xs)) <|> (map (cons x) <$> delete_expr xs) meta def match_ac' : list expr → list expr → tactic (list expr × list expr × list expr) | es (x :: xs) := do es' ← delete_expr x es, match es' with | (some es') := do (c,l,r) ← match_ac' es' xs, return (x::c,l,r) | none := do (c,l,r) ← match_ac' es xs, return (c,l,x::r) end | es [] := do return ([],es,[]) meta def match_ac (unif : bool) (l : list expr) (r : list expr) : tactic (list expr × list expr × list expr) := do (s',l',r') ← match_ac' l r, s' ← mmap instantiate_mvars s', l' ← mmap instantiate_mvars l', r' ← mmap instantiate_mvars r', return (s',l',r') meta def match_prefix : list expr → list expr → tactic (list expr × list expr × list expr) | (x :: xs) (y :: ys) := (do compare opt x y, prod.map ((::) x) id <$> match_prefix xs ys) <|> return ([],x :: xs,y :: ys) | xs ys := return ([],xs,ys) /-- `(prefix,left,right,suffix) ← match_assoc unif l r` finds the longest prefix and suffix common to `l` and `r` and returns them along with the differences -/ meta def match_assoc (l : list expr) (r : list expr) : tactic (list expr × list expr × list expr × list expr) := do (pre,l₁,r₁) ← match_prefix l r, (suf,l₂,r₂) ← match_prefix (reverse l₁) (reverse r₁), return (pre,reverse l₂,reverse r₂,reverse suf) meta def check_ac : expr → tactic (bool × bool × option (expr × expr × expr) × expr) | (expr.app (expr.app f x) y) := do t ← infer_type x, a ← try_core $ to_expr ``(is_associative %%t %%f) >>= mk_instance, c ← try_core $ to_expr ``(is_commutative %%t %%f) >>= mk_instance, i ← try_core (do v ← mk_meta_var t, l_inst_p ← to_expr ``(is_left_id %%t %%f %%v), r_inst_p ← to_expr ``(is_right_id %%t %%f %%v), l_v ← mk_meta_var l_inst_p, r_v ← mk_meta_var r_inst_p , l_id ← mk_mapp `is_left_id.left_id [some t,f,v,some l_v], mk_instance l_inst_p >>= unify l_v, r_id ← mk_mapp `is_right_id.right_id [none,f,v,some r_v], mk_instance r_inst_p >>= unify r_v, v' ← instantiate_mvars v, return (l_id,r_id,v')), return (a.is_some,c.is_some,i,f) | _ := return (ff,ff,none,expr.var 1) meta def parse_assoc_chain' (f : expr) : expr → tactic (dlist expr) | e := (do (expr.app (expr.app f' x) y) ← return e, is_def_eq f f', (++) <$> parse_assoc_chain' x <*> parse_assoc_chain' y) <|> return (singleton e) meta def parse_assoc_chain (f : expr) : expr → tactic (list expr) := map dlist.to_list ∘ parse_assoc_chain' f meta def fold_assoc (op : expr) : option (expr × expr × expr) → list expr → option (expr × list expr) | _ (x::xs) := some (foldl (expr.app ∘ expr.app op) x xs, []) | none [] := none | (some (l_id,r_id,x₀)) [] := some (x₀,[l_id,r_id]) meta def fold_assoc1 (op : expr) : list expr → option expr | (x::xs) := some $ foldl (expr.app ∘ expr.app op) x xs | [] := none meta def same_function_aux : list expr → list expr → expr → expr → tactic (expr × list expr × list expr) | xs₀ xs₁ (expr.app f₀ a₀) (expr.app f₁ a₁) := same_function_aux (a₀ :: xs₀) (a₁ :: xs₁) f₀ f₁ | xs₀ xs₁ e₀ e₁ := is_def_eq e₀ e₁ >> return (e₀,xs₀,xs₁) meta def same_function : expr → expr → tactic (expr × list expr × list expr) := same_function_aux [] [] meta def parse_ac_mono_function (l r : expr) : tactic (expr × expr × list expr × mono_function) := do (full_f,ls,rs) ← same_function l r, (a,c,i,f) ← check_ac l, if a then if c then do (s,ls,rs) ← monad.join (match_ac tt <$> parse_assoc_chain f l <*> parse_assoc_chain f r), (l',l_id) ← fold_assoc f i ls, (r',r_id) ← fold_assoc f i rs, s' ← fold_assoc1 f s, return (l',r',l_id ++ r_id,mono_function.assoc_comm f s') else do -- a ∧ ¬ c (pre,ls,rs,suff) ← monad.join (match_assoc <$> parse_assoc_chain f l <*> parse_assoc_chain f r), (l',l_id) ← fold_assoc f i ls, (r',r_id) ← fold_assoc f i rs, let pre' := fold_assoc1 f pre, let suff' := fold_assoc1 f suff, return (l',r',l_id ++ r_id,mono_function.assoc f pre' suff') else do -- ¬ a (xs₀,x₀,x₁,xs₁) ← find_one_difference opt ls rs, return (x₀,x₁,[],mono_function.non_assoc full_f xs₀ xs₁) meta def parse_ac_mono_function' (l r : pexpr) := do l' ← to_expr l, r' ← to_expr r, parse_ac_mono_function l' r' meta def ac_monotonicity_goal : expr → tactic (expr × expr × list expr × ac_mono_ctx) | `(%%e₀ → %%e₁) := do (l,r,id_rs,f) ← parse_ac_mono_function e₀ e₁, t₀ ← infer_type e₀, t₁ ← infer_type e₁, rel_def ← to_expr ``(λ x₀ x₁, (x₀ : %%t₀) → (x₁ : %%t₁)), return (e₀, e₁, id_rs, { function := f , left := l, right := r , to_rel := some $ expr.pi `x binder_info.default , rel_def := rel_def }) | `(%%e₀ = %%e₁) := do (l,r,id_rs,f) ← parse_ac_mono_function e₀ e₁, t₀ ← infer_type e₀, t₁ ← infer_type e₁, rel_def ← to_expr ``(λ x₀ x₁, (x₀ : %%t₀) = (x₁ : %%t₁)), return (e₀, e₁, id_rs, { function := f , left := l, right := r , to_rel := none , rel_def := rel_def }) | (expr.app (expr.app rel e₀) e₁) := do (l,r,id_rs,f) ← parse_ac_mono_function e₀ e₁, return (e₀, e₁, id_rs, { function := f , left := l, right := r , to_rel := expr.app ∘ expr.app rel , rel_def := rel }) | _ := fail "invalid monotonicity goal" meta def bin_op_left (f : expr) : option expr → expr → expr | none e := e | (some e₀) e₁ := f.mk_app [e₀,e₁] meta def bin_op (f a b : expr) : expr := f.mk_app [a,b] meta def bin_op_right (f : expr) : expr → option expr → expr | e none := e | e₀ (some e₁) := f.mk_app [e₀,e₁] meta def mk_fun_app : mono_function → expr → expr | (mono_function.non_assoc f x y) z := f.mk_app (x ++ z :: y) | (mono_function.assoc f x y) z := bin_op_left f x (bin_op_right f z y) | (mono_function.assoc_comm f x) z := f.mk_app [z,x] meta inductive mono_law /- `assoc (l₀,r₀) (r₁,l₁)` gives first how to find rules to prove x+(y₀+z) R x+(y₁+z); if that fails, helps prove (x+y₀)+z R (x+y₁)+z -/ | assoc : expr × expr → expr × expr → mono_law /- `congr r` gives the rule to prove `x = y → f x = f y` -/ | congr : expr → mono_law | other : expr → mono_law meta def mono_law.to_tactic_format : mono_law → tactic format | (mono_law.other e) := do e ← pp e, return format!"other {e}" | (mono_law.congr r) := do e ← pp r, return format!"congr {e}" | (mono_law.assoc (x₀,x₁) (y₀,y₁)) := do x₀ ← pp x₀, x₁ ← pp x₁, y₀ ← pp y₀, y₁ ← pp y₁, return format!"assoc {x₀}; {x₁} | {y₀}; {y₁}" meta instance has_to_tactic_format_mono_law : has_to_tactic_format mono_law := { to_tactic_format := mono_law.to_tactic_format } meta def mk_rel (ctx : ac_mono_ctx_ne) (f : expr → expr) : expr := ctx.to_rel (f ctx.left) (f ctx.right) meta def mk_congr_args (fn : expr) (xs₀ xs₁ : list expr) (l r : expr) : tactic expr := do p ← mk_app `eq [fn.mk_app $ xs₀ ++ l :: xs₁,fn.mk_app $ xs₀ ++ r :: xs₁], prod.snd <$> solve_aux p (do iterate_exactly (xs₁.length) (applyc `congr_fun), applyc `congr_arg) meta def mk_congr_law (ctx : ac_mono_ctx) : tactic expr := match ctx.function with | (mono_function.assoc f x₀ x₁) := if (x₀ <|> x₁).is_some then mk_congr_args f x₀.to_monad x₁.to_monad ctx.left ctx.right else failed | (mono_function.assoc_comm f x₀) := mk_congr_args f [x₀] [] ctx.left ctx.right | (mono_function.non_assoc f x₀ x₁) := mk_congr_args f x₀ x₁ ctx.left ctx.right end meta def mk_pattern (ctx : ac_mono_ctx) : tactic mono_law := match (sequence ctx : option (ac_mono_ctx' _)) with | (some ctx) := match ctx.function with | (mono_function.assoc f (some x) (some y)) := return $ mono_law.assoc ( mk_rel ctx (λ i, bin_op f x (bin_op f i y)) , mk_rel ctx (λ i, bin_op f i y)) ( mk_rel ctx (λ i, bin_op f (bin_op f x i) y) , mk_rel ctx (λ i, bin_op f x i)) | (mono_function.assoc f (some x) none) := return $ mono_law.other $ mk_rel ctx (λ e, mk_fun_app ctx.function e) | (mono_function.assoc f none (some y)) := return $ mono_law.other $ mk_rel ctx (λ e, mk_fun_app ctx.function e) | (mono_function.assoc f none none) := none | _ := return $ mono_law.other $ mk_rel ctx (λ e, mk_fun_app ctx.function e) end | none := mono_law.congr <$> mk_congr_law ctx end meta def match_rule (pat : expr) (r : name) : tactic expr := do r' ← mk_const r, t ← infer_type r', match_rule_head pat [] r' t meta def find_lemma (pat : expr) : list name → tactic (list expr) | [] := return [] | (r :: rs) := do (cons <$> match_rule pat r <|> pure id) <*> find_lemma rs meta def match_chaining_rules (ls : list name) (x₀ x₁ : expr) : tactic (list expr) := do x' ← to_expr ``(%%x₁ → %%x₀), r₀ ← find_lemma x' ls, r₁ ← find_lemma x₁ ls, return (expr.app <$> r₀ <*> r₁) meta def find_rule (ls : list name) : mono_law → tactic (list expr) | (mono_law.assoc (x₀,x₁) (y₀,y₁)) := (match_chaining_rules ls x₀ x₁) <|> (match_chaining_rules ls y₀ y₁) | (mono_law.congr r) := return [r] | (mono_law.other p) := find_lemma p ls universes u v lemma apply_rel {α : Sort u} (R : α → α → Sort v) {x y : α} (x' y' : α) (h : R x y) (hx : x = x') (hy : y = y') : R x' y' := by { rw [← hx,← hy], apply h } meta def ac_refine (e : expr) : tactic unit := refine ``(eq.mp _ %%e) ; ac_refl meta def one_line (e : expr) : tactic format := do lbl ← pp e, asm ← infer_type e >>= pp, return format!"\t{asm}\n" meta def side_conditions (e : expr) : tactic format := do let vs := e.list_meta_vars, ts ← mmap one_line vs.tail, let r := e.get_app_fn.const_name, return format!"{r}:\n{format.join ts}" open monad /-- tactic-facing function, similar to `interactive.tactic.generalize` with the exception that meta variables -/ meta def generalize' (h : name) (v : expr) (x : name) : tactic (expr × expr) := do tgt ← target, t ← infer_type v, tgt' ← do { ⟨tgt', _⟩ ← solve_aux tgt (tactic.generalize v x >> target), to_expr ``(λ y : %%t, Π x, y = x → %%(tgt'.binding_body.lift_vars 0 1)) } <|> to_expr ``(λ y : %%t, Π x, %%v = x → %%tgt), t ← head_beta (tgt' v) >>= assert h, swap, r ← mk_eq_refl v, solve1 $ tactic.exact (t v r), prod.mk <$> tactic.intro x <*> tactic.intro h private meta def hide_meta_vars (tac : list expr → tactic unit) : tactic unit := focus1 $ do tgt ← target >>= instantiate_mvars, tactic.change tgt, ctx ← local_context, let vs := tgt.list_meta_vars, vs' ← mmap (λ v, do h ← get_unused_name `h, x ← get_unused_name `x, prod.snd <$> generalize' h v x) vs, tac ctx; vs'.mmap' (try ∘ tactic.subst) meta def hide_meta_vars' (tac : itactic) : itactic := hide_meta_vars $ λ _, tac end config meta def solve_mvar (v : expr) (tac : tactic unit) : tactic unit := do gs ← get_goals, set_goals [v], target >>= instantiate_mvars >>= tactic.change, tac, done, set_goals $ gs def list.minimum_on {α β} [decidable_linear_order β] (f : α → β) : list α → list α | [] := [] | (x :: xs) := prod.snd $ xs.foldl (λ ⟨k,a⟩ b, let k' := f b in if k < k' then (k,a) else if k' < k then (k', [b]) else (k,b :: a)) (f x, [x]) open format mono_selection meta def best_match {β} (xs : list expr) (tac : expr → tactic β) : tactic unit := do t ← target, xs ← xs.mmap (λ x, try_core $ prod.mk x <$> solve_aux t (tac x >> get_goals)), let xs := xs.filter_map id, let r := list.minimum_on (list.length ∘ prod.fst ∘ prod.snd) xs, match r with | [(_,gs,pr)] := tactic.exact pr >> set_goals gs | [] := fail "no good match found" | _ := do lmms ← r.mmap (λ ⟨l,gs,_⟩, do ts ← gs.mmap infer_type, msg ← ts.mmap pp, pure $ foldl compose "\n\n" (list.intersperse "\n" $ to_fmt l.get_app_fn.const_name :: msg)), let msg := foldl compose "" lmms, fail format!"ambiguous match: {msg}\n\nTip: try asserting a side condition to distinguish between the lemmas" end meta def mono_aux (dir : parse side) (cfg : mono_cfg := { mono_cfg . }) : tactic unit := do t ← target >>= instantiate_mvars, ns ← get_monotonicity_lemmas t dir, asms ← local_context, rs ← find_lemma asms t ns, focus1 $ () <$ best_match rs (λ law, tactic.refine $ to_pexpr law) /-- - `mono` applies a monotonicity rule. - `mono*` applies monotonicity rules repetitively. - `mono with x ≤ y` or `mono with [0 ≤ x,0 ≤ y]` creates an assertion for the listed propositions. Those help to select the right monotonicity rule. - `mono left` or `mono right` is useful when proving strict orderings: for `x + y < w + z` could be broken down into either - left: `x ≤ w` and `y < z` or - right: `x < w` and `y ≤ z` -/ meta def mono (many : parse (tk "*")?) (dir : parse side) (hyps : parse $ tk "with" *> pexpr_list_or_texpr <|> pure []) (cfg : mono_cfg := { mono_cfg . }) : tactic unit := do hyps ← hyps.mmap (λ p, to_expr p >>= mk_meta_var), hyps.mmap' (λ pr, do h ← get_unused_name `h, note h none pr), if many.is_some then repeat $ mono_aux dir cfg else mono_aux dir cfg, gs ← get_goals, set_goals $ hyps ++ gs /-- transforms a goal of the form `f x ≼ f y` into `x ≤ y` using lemmas marked as `monotonic`. Special care is taken when `f` is the repeated application of an associative operator and if the operator is commutative -/ meta def ac_mono_aux (cfg : mono_cfg := { mono_cfg . }) : tactic unit := hide_meta_vars $ λ asms, do try `[dunfold has_sub.sub algebra.sub], tgt ← target >>= instantiate_mvars, (l,r,id_rs,g) ← ac_monotonicity_goal cfg tgt <|> fail "monotonic context not found", ns ← get_monotonicity_lemmas tgt both, p ← mk_pattern g, rules ← find_rule asms ns p <|> fail "no applicable rules found", when (rules = []) (fail "no applicable rules found"), err ← format.join <$> mmap side_conditions rules, focus1 $ best_match rules (λ rule, do t₀ ← mk_meta_var `(Prop), v₀ ← mk_meta_var t₀, t₁ ← mk_meta_var `(Prop), v₁ ← mk_meta_var t₁, tactic.refine $ ``(apply_rel %%(g.rel_def) %%l %%r %%rule %%v₀ %%v₁), solve_mvar v₀ (try (any_of id_rs rewrite_target) >> ( done <|> refl <|> ac_refl <|> `[simp only [is_associative.assoc]]) ), solve_mvar v₁ (try (any_of id_rs rewrite_target) >> ( done <|> refl <|> ac_refl <|> `[simp only [is_associative.assoc]]) ), n ← num_goals, iterate_exactly (n-1) (try $ solve1 $ apply_instance <|> tactic.solve_by_elim {assumptions := pure asms})) open sum nat /-- (repeat_until_or_at_most n t u): repeat tactic `t` at most n times or until u succeeds -/ meta def repeat_until_or_at_most : nat → tactic unit → tactic unit → tactic unit | 0 t _ := fail "too many applications" | (succ n) t u := u <|> (t >> repeat_until_or_at_most n t u) meta def repeat_until : tactic unit → tactic unit → tactic unit := repeat_until_or_at_most 100000 @[derive _root_.has_reflect] inductive rep_arity : Type | one | exactly (n : ℕ) | many meta def repeat_or_not : rep_arity → tactic unit → option (tactic unit) → tactic unit | rep_arity.one tac none := tac | rep_arity.many tac none := repeat tac | (rep_arity.exactly n) tac none := iterate_exactly n tac | rep_arity.one tac (some until) := tac >> until | rep_arity.many tac (some until) := repeat_until tac until | (rep_arity.exactly n) tac (some until) := iterate_exactly n tac >> until meta def assert_or_rule : lean.parser (pexpr ⊕ pexpr) := (tk ":=" *> inl <$> texpr <|> (tk ":" *> inr <$> texpr)) meta def arity : lean.parser rep_arity := rep_arity.many <$ tk "*" <|> rep_arity.exactly <$> (tk "^" *> small_nat) <|> pure rep_arity.one /-- `ac_mono` reduces the `f x ⊑ f y`, for some relation `⊑` and a monotonic function `f` to `x ≺ y`. `ac_mono*` unwraps monotonic functions until it can't. `ac_mono^k`, for some literal number `k` applies monotonicity `k` times. `ac_mono h`, with `h` a hypothesis, unwraps monotonic functions and uses `h` to solve the remaining goal. Can be combined with * or ^k: `ac_mono* h` `ac_mono : p` asserts `p` and uses it to discharge the goal result unwrapping a series of monotonic functions. Can be combined with * or ^k: `ac_mono* : p` In the case where `f` is an associative or commutative operator, `ac_mono` will consider any possible permutation of its arguments and use the one the minimizes the difference between the left-hand side and the right-hand side. TODO(Simon): with `ac_mono h` and `ac_mono : p` split the remaining gaol if the provided rule does not solve it completely. -/ meta def ac_mono (rep : parse arity) : parse assert_or_rule? → opt_param mono_cfg { mono_cfg . } → tactic unit | none opt := focus1 $ repeat_or_not rep (ac_mono_aux opt) none | (some (inl h)) opt := do focus1 $ repeat_or_not rep (ac_mono_aux opt) (some $ done <|> to_expr h >>= ac_refine) | (some (inr t)) opt := do h ← i_to_expr t >>= assert `h, tactic.swap, focus1 $ repeat_or_not rep (ac_mono_aux opt) (some $ done <|> ac_refine h) attribute [mono] and.imp or.imp end tactic.interactive
df323b41f0c4e1028eead4fd2c8d1ff3d5c0cc4c
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/logic/embedding.lean
dd2380a6216c304f596843cac17c2e81814735de
[ "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
9,944
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.equiv.basic import data.sigma /-! # Injective functions -/ universes u v w x namespace function /-- `α ↪ β` is a bundled injective function. -/ structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj' : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ end function /-- Convert an `α ≃ β` to `α ↪ β`. -/ protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β := ⟨f, f.injective⟩ @[simp] theorem equiv.to_embedding_coe_fn {α : Sort u} {β : Sort v} (f : α ≃ β) : (f.to_embedding : α → β) = f := rfl namespace function namespace embedding @[ext] lemma ext {α β} {f g : embedding α β} (h : ∀ x, f x = g x) : f = g := by cases f; cases g; simpa using funext h lemma ext_iff {α β} {f g : embedding α β} : (∀ x, f x = g x) ↔ f = g := ⟨ext, λ h _, by rw h⟩ @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl theorem injective {α β} (f : α ↪ β) : injective f := f.inj' @[refl] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨g ∘ f, g.injective.comp f.injective⟩ @[simp] theorem refl_apply {α} (x : α) : embedding.refl α x = x := rfl @[simp] theorem trans_apply {α β γ} (f : α ↪ β) (g : β ↪ γ) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] lemma equiv_to_embedding_trans_symm_to_embedding {α β : Sort*} (e : α ≃ β) : function.embedding.trans (e.to_embedding) (e.symm.to_embedding) = function.embedding.refl _ := by { ext, simp, } @[simp] lemma equiv_symm_to_embedding_trans_to_embedding {α β : Sort*} (e : α ≃ β) : function.embedding.trans (e.symm.to_embedding) (e.to_embedding) = function.embedding.refl _ := by { ext, simp, } protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) /-- A right inverse `surj_inv` of a surjective function as an `embedding`. -/ protected noncomputable def of_surjective {α β} (f : β → α) (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ /-- Convert a surjective `embedding` to an `equiv` -/ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective f ⟨f.injective, hf⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ /-- Change the value of an embedding `f` at one point. If the prescribed image is already occupied by some `f a'`, then swap the values at these two points. -/ def set_value {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : α ↪ β := ⟨λ a', if a' = a then b else if f a' = b then f a else f a', begin intros x y h, dsimp at h, split_ifs at h; try { substI b }; try { simp only [f.injective.eq_iff] at * }; cc end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : set_value f a b a = b := by simp [set_value] /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.some_injective α⟩ /-- Embedding of a `subtype`. -/ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨subtype.val, λ _ _, subtype.ext_val⟩ /-- Choosing an element `b : β` gives an embedding of `punit` into `β`. -/ def punit {β : Sort*} (b : β) : punit ↪ β := ⟨λ _, b, by { rintros ⟨⟩ ⟨⟩ _, refl, }⟩ /-- Fixing an element `b : β` gives an embedding `α ↪ α × β`. -/ def sectl (α : Sort*) {β : Sort*} (b : β) : α ↪ α × β := ⟨λ a, (a, b), λ a a' h, congr_arg prod.fst h⟩ /-- Fixing an element `a : α` gives an embedding `β ↪ α × β`. -/ def sectr {α : Sort*} (a : α) (β : Sort*): β ↪ α × β := ⟨λ b, (a, b), λ b b' h, congr_arg prod.snd h⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.injective (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- If `e₁` and `e₂` are embeddings, then so is `prod.map e₁ e₂ : (a, b) ↦ (e₁ a, e₂ b)`. -/ def prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨prod.map e₁ e₂, e₁.injective.prod_map e₂.injective⟩ @[simp] lemma coe_prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(e₁.prod_map e₂) = prod.map e₁ e₂ := rfl section sum open sum /-- If `e₁` and `e₂` are embeddings, then so is `sum.map e₁ e₂`. -/ def sum_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨sum.map e₁ e₂, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.injective $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.injective $ inr.inj h end⟩ @[simp] theorem coe_sum_map {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(sum_map e₁ e₂) = sum.map e₁ e₂ := rfl /-- The embedding of `α` into the sum `α ⊕ β`. -/ def inl {α β : Type*} : α ↪ α ⊕ β := ⟨sum.inl, λ a b, sum.inl.inj⟩ /-- The embedding of `β` into the sum `α ⊕ β`. -/ def inr {α β : Type*} : β ↪ α ⊕ β := ⟨sum.inr, λ a b, sum.inr.inj⟩ end sum section sigma variables {α α' : Type*} {β : α → Type*} {β' : α' → Type*} /-- If `f : α ↪ α'` is an embedding and `g : Π a, β α ↪ β' (f α)` is a family of embeddings, then `sigma.map f g` is an embedding. -/ def sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : (Σ a, β a) ↪ Σ a', β' a' := ⟨sigma.map f (λ a, g a), f.injective.sigma_map (λ a, (g a).injective)⟩ @[simp] lemma coe_sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : ⇑(f.sigma_map g) = sigma.map f (λ a, g a) := rfl end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).injective (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.injective $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ protected def subtype_map {α β} {p : α → Prop} {q : β → Prop} (f : α ↪ β) (h : ∀{{x}}, p x → q (f x)) : {x : α // p x} ↪ {y : β // q y} := ⟨subtype.map f h, subtype.map_injective h f.2⟩ open set /-- `set.image` as an embedding `set α ↪ set β`. -/ protected def image {α β} (f : α ↪ β) : set α ↪ set β := ⟨image f, f.2.image_injective⟩ @[simp] lemma coe_image {α β} (f : α ↪ β) : ⇑f.image = image f := rfl end embedding end function namespace equiv @[simp] lemma refl_to_embedding {α : Type*} : (equiv.refl α).to_embedding = function.embedding.refl α := rfl @[simp] lemma trans_to_embedding {α β γ : Type*} (e : α ≃ β) (f : β ≃ γ) : (e.trans f).to_embedding = e.to_embedding.trans f.to_embedding := rfl end equiv namespace set /-- The injection map is an embedding between subsets. -/ def embedding_of_subset {α} (s t : set α) (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by congr; injection h⟩ @[simp] lemma embedding_of_subset_apply_mk {α} {s t : set α} (h : s ⊆ t) (x : α) (hx : x ∈ s) : embedding_of_subset s t h ⟨x, hx⟩ = ⟨x, h hx⟩ := rfl @[simp] lemma coe_embedding_of_subset_apply {α} {s t : set α} (h : s ⊆ t) (x : s) : (embedding_of_subset s t h x : α) = x := rfl end set /-- The embedding of a left cancellative semigroup into itself by left multiplication by a fixed element. -/ @[to_additive "The embedding of a left cancellative additive semigroup into itself by left translation by a fixed element."] def mul_left_embedding {G : Type u} [left_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, g * h, inj' := λ h h', (mul_right_inj g).mp, } @[simp] lemma mul_left_embedding_apply {G : Type u} [left_cancel_semigroup G] (g h : G) : mul_left_embedding g h = g * h := rfl /-- The embedding of a right cancellative semigroup into itself by right multiplication by a fixed element. -/ @[to_additive "The embedding of a right cancellative additive semigroup into itself by right translation by a fixed element."] def mul_right_embedding {G : Type u} [right_cancel_semigroup G] (g : G) : G ↪ G := { to_fun := λ h, h * g, inj' := λ h h', (mul_left_inj g).mp, } @[simp] lemma mul_right_embedding_apply {G : Type u} [right_cancel_semigroup G] (g h : G) : mul_right_embedding g h = h * g := rfl
c0f8c0655d9b7d21f7a5cb1c005a4f1da5f7f51c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/ring/defs.lean
7f28e7eb12f262f81c4ef9b1f9836f61a31d87d9
[ "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
16,731
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Yury Kudryashov, Neil Strickland -/ import algebra.group.basic import algebra.group_with_zero.defs import data.int.cast.defs /-! # Semirings and rings > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/655 > Any changes to this file require a corresponding PR to mathlib4. This file defines semirings, rings and domains. This is analogous to `algebra.group.defs` and `algebra.group.basic`, the difference being that the former is about `+` and `*` separately, while the present file is about their interaction. ## Main definitions * `distrib`: Typeclass for distributivity of multiplication over addition. * `has_distrib_neg`: Typeclass for commutativity of negation and multiplication. This is useful when dealing with multiplicative submonoids which are closed under negation without being closed under addition, for example `units`. * `(non_unital_)(non_assoc_)(semi)ring`: Typeclasses for possibly non-unital or non-associative rings and semirings. Some combinations are not defined yet because they haven't found use. ## Tags `semiring`, `comm_semiring`, `ring`, `comm_ring`, `domain`, `is_domain`, `nonzero`, `units` -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {R : Type x} set_option old_structure_cmd true open function /-! ### `distrib` class -/ /-- A typeclass stating that multiplication is left and right distributive over addition. -/ @[protect_proj, ancestor has_mul has_add] class distrib (R : Type*) extends has_mul R, has_add R := (left_distrib : ∀ a b c : R, a * (b + c) = a * b + a * c) (right_distrib : ∀ a b c : R, (a + b) * c = a * c + b * c) /-- A typeclass stating that multiplication is left distributive over addition. -/ @[protect_proj] class left_distrib_class (R : Type*) [has_mul R] [has_add R] := (left_distrib : ∀ a b c : R, a * (b + c) = a * b + a * c) /-- A typeclass stating that multiplication is right distributive over addition. -/ @[protect_proj] class right_distrib_class (R : Type*) [has_mul R] [has_add R] := (right_distrib : ∀ a b c : R, (a + b) * c = a * c + b * c) @[priority 100] -- see Note [lower instance priority] instance distrib.left_distrib_class (R : Type*) [distrib R] : left_distrib_class R := ⟨distrib.left_distrib⟩ @[priority 100] -- see Note [lower instance priority] instance distrib.right_distrib_class (R : Type*) [distrib R] : right_distrib_class R := ⟨distrib.right_distrib⟩ lemma left_distrib [has_mul R] [has_add R] [left_distrib_class R] (a b c : R) : a * (b + c) = a * b + a * c := left_distrib_class.left_distrib a b c alias left_distrib ← mul_add lemma right_distrib [has_mul R] [has_add R] [right_distrib_class R] (a b c : R) : (a + b) * c = a * c + b * c := right_distrib_class.right_distrib a b c alias right_distrib ← add_mul lemma distrib_three_right [has_mul R] [has_add R] [right_distrib_class R] (a b c d : R) : (a + b + c) * d = a * d + b * d + c * d := by simp [right_distrib] /-! ### Semirings -/ /-- A not-necessarily-unital, not-necessarily-associative semiring. -/ @[protect_proj, ancestor add_comm_monoid distrib mul_zero_class] class non_unital_non_assoc_semiring (α : Type u) extends add_comm_monoid α, distrib α, mul_zero_class α /-- An associative but not-necessarily unital semiring. -/ @[protect_proj, ancestor non_unital_non_assoc_semiring semigroup_with_zero] class non_unital_semiring (α : Type u) extends non_unital_non_assoc_semiring α, semigroup_with_zero α /-- A unital but not-necessarily-associative semiring. -/ @[protect_proj, ancestor non_unital_non_assoc_semiring mul_zero_one_class] class non_assoc_semiring (α : Type u) extends non_unital_non_assoc_semiring α, mul_zero_one_class α, add_comm_monoid_with_one α /-- A semiring is a type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative monoid (`monoid`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). The actual definition extends `monoid_with_zero` instead of `monoid` and `mul_zero_class`. -/ @[protect_proj, ancestor non_unital_semiring non_assoc_semiring monoid_with_zero] class semiring (α : Type u) extends non_unital_semiring α, non_assoc_semiring α, monoid_with_zero α section has_one_has_add variables [has_one α] [has_add α] lemma one_add_one_eq_two : 1 + 1 = (2 : α) := rfl end has_one_has_add section distrib_mul_one_class variables [has_add α] [mul_one_class α] lemma add_one_mul [right_distrib_class α] (a b : α) : (a + 1) * b = a * b + b := by rw [add_mul, one_mul] lemma mul_add_one [left_distrib_class α] (a b : α) : a * (b + 1) = a * b + a := by rw [mul_add, mul_one] lemma one_add_mul [right_distrib_class α] (a b : α) : (1 + a) * b = b + a * b := by rw [add_mul, one_mul] lemma mul_one_add [left_distrib_class α] (a b : α) : a * (1 + b) = a + a * b := by rw [mul_add, mul_one] theorem two_mul [right_distrib_class α] (n : α) : 2 * n = n + n := eq.trans (right_distrib 1 1 n) (by simp) theorem bit0_eq_two_mul [right_distrib_class α] (n : α) : bit0 n = 2 * n := (two_mul _).symm theorem mul_two [left_distrib_class α] (n : α) : n * 2 = n + n := (left_distrib n 1 1).trans (by simp) end distrib_mul_one_class section semiring variables [semiring α] @[to_additive] lemma mul_ite {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : a * (if P then b else c) = if P then a * b else a * c := by split_ifs; refl @[to_additive] lemma ite_mul {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : (if P then a else b) * c = if P then a * c else b * c := by split_ifs; refl -- We make `mul_ite` and `ite_mul` simp lemmas, -- but not `add_ite` or `ite_add`. -- The problem we're trying to avoid is dealing with -- summations of the form `∑ x in s, (f x + ite P 1 0)`, -- in which `add_ite` followed by `sum_ite` would needlessly slice up -- the `f x` terms according to whether `P` holds at `x`. -- There doesn't appear to be a corresponding difficulty so far with -- `mul_ite` and `ite_mul`. attribute [simp] mul_ite ite_mul @[simp] lemma mul_boole {α} [mul_zero_one_class α] (P : Prop) [decidable P] (a : α) : a * (if P then 1 else 0) = if P then a else 0 := by simp @[simp] lemma boole_mul {α} [mul_zero_one_class α] (P : Prop) [decidable P] (a : α) : (if P then 1 else 0) * a = if P then a else 0 := by simp lemma ite_mul_zero_left {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) : ite P (a * b) 0 = ite P a 0 * b := by { by_cases h : P; simp [h], } lemma ite_mul_zero_right {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) : ite P (a * b) 0 = a * ite P b 0 := by { by_cases h : P; simp [h], } lemma ite_and_mul_zero {α : Type*} [mul_zero_class α] (P Q : Prop) [decidable P] [decidable Q] (a b : α) : ite (P ∧ Q) (a * b) 0 = ite P a 0 * ite Q b 0 := by simp only [←ite_and, ite_mul, mul_ite, mul_zero, zero_mul, and_comm] end semiring /-- A non-unital commutative semiring is a `non_unital_semiring` with commutative multiplication. In other words, it is a type with the following structures: additive commutative monoid (`add_comm_monoid`), commutative semigroup (`comm_semigroup`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). -/ @[protect_proj, ancestor non_unital_semiring comm_semigroup] class non_unital_comm_semiring (α : Type u) extends non_unital_semiring α, comm_semigroup α /-- A commutative semiring is a `semiring` with commutative multiplication. In other words, it is a type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative commutative monoid (`comm_monoid`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). -/ @[protect_proj, ancestor semiring comm_monoid] class comm_semiring (α : Type u) extends semiring α, comm_monoid α @[priority 100] -- see Note [lower instance priority] instance comm_semiring.to_non_unital_comm_semiring [comm_semiring α] : non_unital_comm_semiring α := { .. comm_semiring.to_comm_monoid α, .. comm_semiring.to_semiring α } @[priority 100] -- see Note [lower instance priority] instance comm_semiring.to_comm_monoid_with_zero [comm_semiring α] : comm_monoid_with_zero α := { .. comm_semiring.to_comm_monoid α, .. comm_semiring.to_semiring α } section comm_semiring variables [comm_semiring α] {a b c : α} lemma add_mul_self_eq (a b : α) : (a + b) * (a + b) = a*a + 2*a*b + b*b := by simp only [two_mul, add_mul, mul_add, add_assoc, mul_comm b] end comm_semiring section has_distrib_neg /-- Typeclass for a negation operator that distributes across multiplication. This is useful for dealing with submonoids of a ring that contain `-1` without having to duplicate lemmas. -/ class has_distrib_neg (α : Type*) [has_mul α] extends has_involutive_neg α := (neg_mul : ∀ x y : α, -x * y = -(x * y)) (mul_neg : ∀ x y : α, x * -y = -(x * y)) section has_mul variables [has_mul α] [has_distrib_neg α] @[simp] lemma neg_mul (a b : α) : - a * b = - (a * b) := has_distrib_neg.neg_mul _ _ @[simp] lemma mul_neg (a b : α) : a * - b = - (a * b) := has_distrib_neg.mul_neg _ _ lemma neg_mul_neg (a b : α) : -a * -b = a * b := by simp lemma neg_mul_eq_neg_mul (a b : α) : -(a * b) = -a * b := (neg_mul _ _).symm lemma neg_mul_eq_mul_neg (a b : α) : -(a * b) = a * -b := (mul_neg _ _).symm lemma neg_mul_comm (a b : α) : -a * b = a * -b := by simp end has_mul section mul_one_class variables [mul_one_class α] [has_distrib_neg α] theorem neg_eq_neg_one_mul (a : α) : -a = -1 * a := by simp /-- An element of a ring multiplied by the additive inverse of one is the element's additive inverse. -/ lemma mul_neg_one (a : α) : a * -1 = -a := by simp /-- The additive inverse of one multiplied by an element of a ring is the element's additive inverse. -/ lemma neg_one_mul (a : α) : -1 * a = -a := by simp end mul_one_class section mul_zero_class variables [mul_zero_class α] [has_distrib_neg α] @[priority 100] instance mul_zero_class.neg_zero_class : neg_zero_class α := { neg_zero := by rw [←zero_mul (0 : α), ←neg_mul, mul_zero, mul_zero], ..mul_zero_class.to_has_zero α, ..has_distrib_neg.to_has_involutive_neg α } end mul_zero_class end has_distrib_neg /-! ### Rings -/ /-- A not-necessarily-unital, not-necessarily-associative ring. -/ @[protect_proj, ancestor add_comm_group non_unital_non_assoc_semiring] class non_unital_non_assoc_ring (α : Type u) extends add_comm_group α, non_unital_non_assoc_semiring α -- We defer the instance `non_unital_non_assoc_ring.to_has_distrib_neg` to `algebra.ring.basic` -- as it relies on the lemma `eq_neg_of_add_eq_zero_left`. /-- An associative but not-necessarily unital ring. -/ @[protect_proj, ancestor non_unital_non_assoc_ring non_unital_semiring] class non_unital_ring (α : Type*) extends non_unital_non_assoc_ring α, non_unital_semiring α /-- A unital but not-necessarily-associative ring. -/ @[protect_proj, ancestor non_unital_non_assoc_ring non_assoc_semiring] class non_assoc_ring (α : Type*) extends non_unital_non_assoc_ring α, non_assoc_semiring α, add_group_with_one α /-- A ring is a type with the following structures: additive commutative group (`add_comm_group`), multiplicative monoid (`monoid`), and distributive laws (`distrib`). Equivalently, a ring is a `semiring` with a negation operation making it an additive group. -/ @[protect_proj, ancestor add_comm_group monoid distrib] class ring (α : Type u) extends add_comm_group_with_one α, monoid α, distrib α section non_unital_non_assoc_ring variables [non_unital_non_assoc_ring α] @[priority 100] instance non_unital_non_assoc_ring.to_has_distrib_neg : has_distrib_neg α := { neg := has_neg.neg, neg_neg := neg_neg, neg_mul := λ a b, eq_neg_of_add_eq_zero_left $ by rw [←right_distrib, add_left_neg, zero_mul], mul_neg := λ a b, eq_neg_of_add_eq_zero_left $ by rw [←left_distrib, add_left_neg, mul_zero] } lemma mul_sub_left_distrib (a b c : α) : a * (b - c) = a * b - a * c := by simpa only [sub_eq_add_neg, neg_mul_eq_mul_neg] using mul_add a b (-c) alias mul_sub_left_distrib ← mul_sub lemma mul_sub_right_distrib (a b c : α) : (a - b) * c = a * c - b * c := by simpa only [sub_eq_add_neg, neg_mul_eq_neg_mul] using add_mul a (-b) c alias mul_sub_right_distrib ← sub_mul variables {a b c d e : α} /-- An iff statement following from right distributivity in rings and the definition of subtraction. -/ theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp [add_comm] ... ↔ a * e + c - b * e = d : iff.intro (λ h, begin rw h, simp end) (λ h, begin rw ← h, simp end) ... ↔ (a - b) * e + c = d : begin simp [sub_mul, sub_add_eq_add_sub] end /-- A simplification of one side of an equation exploiting right distributivity in rings and the definition of subtraction. -/ theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d := assume h, calc (a - b) * e + c = (a * e + c) - b * e : begin simp [sub_mul, sub_add_eq_add_sub] end ... = d : begin rw h, simp [@add_sub_cancel α] end end non_unital_non_assoc_ring section non_assoc_ring variables [non_assoc_ring α] lemma sub_one_mul (a b : α) : (a - 1) * b = a * b - b := by rw [sub_mul, one_mul] lemma mul_sub_one (a b : α) : a * (b - 1) = a * b - a := by rw [mul_sub, mul_one] lemma one_sub_mul (a b : α) : (1 - a) * b = b - a * b := by rw [sub_mul, one_mul] lemma mul_one_sub (a b : α) : a * (1 - b) = a - a * b := by rw [mul_sub, mul_one] end non_assoc_ring section ring variables [ring α] {a b c d e : α} /- A (unital, associative) ring is a not-necessarily-unital ring -/ @[priority 100] -- see Note [lower instance priority] instance ring.to_non_unital_ring : non_unital_ring α := { zero_mul := λ a, add_left_cancel $ show 0 * a + 0 * a = 0 * a + 0, by rw [← add_mul, zero_add, add_zero], mul_zero := λ a, add_left_cancel $ show a * 0 + a * 0 = a * 0 + 0, by rw [← mul_add, add_zero, add_zero], ..‹ring α› } /- A (unital, associative) ring is a not-necessarily-associative ring -/ @[priority 100] -- see Note [lower instance priority] instance ring.to_non_assoc_ring : non_assoc_ring α := { zero_mul := λ a, add_left_cancel $ show 0 * a + 0 * a = 0 * a + 0, by rw [← add_mul, zero_add, add_zero], mul_zero := λ a, add_left_cancel $ show a * 0 + a * 0 = a * 0 + 0, by rw [← mul_add, add_zero, add_zero], ..‹ring α› } /- The instance from `ring` to `semiring` happens often in linear algebra, for which all the basic definitions are given in terms of semirings, but many applications use rings or fields. We increase a little bit its priority above 100 to try it quickly, but remaining below the default 1000 so that more specific instances are tried first. -/ @[priority 200] instance ring.to_semiring : semiring α := { ..‹ring α›, .. ring.to_non_unital_ring } end ring /-- A non-unital commutative ring is a `non_unital_ring` with commutative multiplication. -/ @[protect_proj, ancestor non_unital_ring comm_semigroup] class non_unital_comm_ring (α : Type u) extends non_unital_ring α, comm_semigroup α @[priority 100] -- see Note [lower instance priority] instance non_unital_comm_ring.to_non_unital_comm_semiring [s : non_unital_comm_ring α] : non_unital_comm_semiring α := { ..s } /-- A commutative ring is a `ring` with commutative multiplication. -/ @[protect_proj, ancestor ring comm_semigroup] class comm_ring (α : Type u) extends ring α, comm_monoid α @[priority 100] -- see Note [lower instance priority] instance comm_ring.to_comm_semiring [s : comm_ring α] : comm_semiring α := { mul_zero := mul_zero, zero_mul := zero_mul, ..s } @[priority 100] -- see Note [lower instance priority] instance comm_ring.to_non_unital_comm_ring [s : comm_ring α] : non_unital_comm_ring α := { mul_zero := mul_zero, zero_mul := zero_mul, ..s } /-- A domain is a nontrivial ring with no zero divisors, i.e. satisfying the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. This is implemented as a mixin for `ring α`. To obtain an integral domain use `[comm_ring α] [is_domain α]`. -/ @[protect_proj, ancestor no_zero_divisors nontrivial] class is_domain (α : Type u) [ring α] extends no_zero_divisors α, nontrivial α : Prop
bb32d45e0667f86aff21174d1a2c2cc100258847
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/holeErrors.lean
e6969e64608d264cf5b240dd8df9a864b43c4d39
[ "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
176
lean
-- def f1.{u} := id.{u} def f2 : _ := id def f3.{u} := let x := id.{u}; x def f4 (x) := x def f5 (x : _) := x def f6 := fun x => x def f7.{u} := let rec x := id.{u}; 10
8479b138a3221f0817f4903213d473f1c7ef5286
b2e508d02500f1512e1618150413e6be69d9db10
/src/measure_theory/measure_space.lean
0b44bde0f148bb7eb127a34c728d08c1b8811b7a
[ "Apache-2.0" ]
permissive
callum-sutton/mathlib
c3788f90216e9cd43eeffcb9f8c9f959b3b01771
afd623825a3ac6bfbcc675a9b023edad3f069e89
refs/heads/master
1,591,371,888,053
1,560,990,690,000
1,560,990,690,000
192,476,045
0
0
Apache-2.0
1,568,941,843,000
1,560,837,965,000
Lean
UTF-8
Lean
false
false
35,104
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 Measure spaces -- measures Measures are restricted to a measurable space (associated by the type class `measurable_space`). This allows us to prove equalities between measures by restricting to a generating set of the measurable space. On the other hand, the `μ.measure s` projection (i.e. the measure of `s` on the measure space `μ`) is the _outer_ measure generated by `μ`. This gives us a unrestricted monotonicity rule and it is somehow well-behaved on non-measurable sets. This allows us for the `lebesgue` measure space to have the `borel` measurable space, but still be a complete measure. -/ import data.set.lattice data.set.finite import topology.instances.ennreal measure_theory.outer_measure noncomputable theory open classical set lattice filter finset function local attribute [instance] prop_decidable universes u v w x namespace measure_theory section of_measurable parameters {α : Type*} [measurable_space α] parameters (m : Π (s : set α), is_measurable s → ennreal) parameters (m0 : m ∅ is_measurable.empty = 0) include m0 /-- Measure projection which is ∞ for non-measurable sets. `measure'` is mainly used to derive the outer measure, for the main `measure` projection. -/ def measure' (s : set α) : ennreal := ⨅ h : is_measurable s, m s h lemma measure'_eq {s} (h : is_measurable s) : measure' s = m s h := by simp [measure', h] lemma measure'_empty : measure' ∅ = 0 := (measure'_eq is_measurable.empty).trans m0 lemma measure'_Union_nat {f : ℕ → set α} (hm : ∀i, is_measurable (f i)) (mU : m (⋃i, f i) (is_measurable.Union hm) = (∑i, m (f i) (hm i))) : measure' (⋃i, f i) = (∑i, measure' (f i)) := (measure'_eq _).trans $ mU.trans $ by congr; funext i; rw measure'_eq /-- outer measure of a measure -/ def outer_measure' : outer_measure α := outer_measure.of_function measure' measure'_empty lemma measure'_Union_le_tsum_nat' (mU : ∀ {f : ℕ → set α} (hm : ∀i, is_measurable (f i)), m (⋃i, f i) (is_measurable.Union hm) ≤ (∑i, m (f i) (hm i))) (s : ℕ → set α) : measure' (⋃i, s i) ≤ (∑i, measure' (s i)) := begin by_cases h : ∀i, is_measurable (s i), { rw [measure'_eq _ _ (is_measurable.Union h), congr_arg tsum _], {apply mU h}, funext i, apply measure'_eq _ _ (h i) }, { cases not_forall.1 h with i hi, exact le_trans (le_infi $ λ h, hi.elim h) (ennreal.le_tsum i) } end parameter (mU : ∀ {f : ℕ → set α} (hm : ∀i, is_measurable (f i)), pairwise (disjoint on f) → m (⋃i, f i) (is_measurable.Union hm) = (∑i, m (f i) (hm i))) include mU lemma measure'_Union {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) (hm : ∀i, is_measurable (f i)) : measure' (⋃i, f i) = (∑i, measure' (f i)) := begin rw [encodable.Union_decode2, outer_measure.Union_aux], { exact measure'_Union_nat _ _ (λ n, encodable.Union_decode2_cases is_measurable.empty hm) (mU _ (measurable_space.Union_decode2_disjoint_on hd)) }, { apply measure'_empty }, end lemma measure'_union {s₁ s₂ : set α} (hd : disjoint s₁ s₂) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : measure' (s₁ ∪ s₂) = measure' s₁ + measure' s₂ := begin rw [union_eq_Union, measure'_Union _ _ @mU (pairwise_disjoint_on_bool.2 hd) (bool.forall_bool.2 ⟨h₂, h₁⟩), tsum_fintype], change _+_ = _, simp end lemma measure'_mono {s₁ s₂ : set α} (h₁ : is_measurable s₁) (hs : s₁ ⊆ s₂) : measure' s₁ ≤ measure' s₂ := le_infi $ λ h₂, begin have := measure'_union _ _ @mU disjoint_diff h₁ (h₂.diff h₁), rw union_diff_cancel hs at this, rw ← measure'_eq m m0 _, exact le_iff_exists_add.2 ⟨_, this⟩ end lemma measure'_Union_le_tsum_nat : ∀ (s : ℕ → set α), measure' (⋃i, s i) ≤ (∑i, measure' (s i)) := measure'_Union_le_tsum_nat' $ λ f h, begin simp [Union_disjointed.symm] {single_pass := tt}, rw [mU (is_measurable.disjointed h) disjoint_disjointed], refine ennreal.tsum_le_tsum (λ i, _), rw [← measure'_eq m m0, ← measure'_eq m m0], exact measure'_mono _ _ @mU (is_measurable.disjointed h _) (inter_subset_left _ _) end lemma outer_measure'_eq {s : set α} (hs : is_measurable s) : outer_measure' s = m s hs := by rw ← measure'_eq m m0 hs; exact (le_antisymm (outer_measure.of_function_le _ _ _) $ le_infi $ λ f, le_infi $ λ hf, le_trans (measure'_mono _ _ @mU hs hf) $ measure'_Union_le_tsum_nat _ _ @mU _) lemma outer_measure'_eq_measure' {s : set α} (hs : is_measurable s) : outer_measure' s = measure' s := by rw [measure'_eq m m0 hs, outer_measure'_eq m m0 @mU hs] end of_measurable namespace outer_measure variables {α : Type*} [measurable_space α] (m : outer_measure α) def trim : outer_measure α := outer_measure' (λ s _, m s) m.empty theorem trim_ge : m ≤ m.trim := λ s, le_infi $ λ f, le_infi $ λ hs, le_trans (m.mono hs) $ le_trans (m.Union_nat f) $ ennreal.tsum_le_tsum $ λ i, le_infi $ λ hf, le_refl _ theorem trim_eq {s : set α} (hs : is_measurable s) : m.trim s = m s := le_antisymm (le_trans (of_function_le _ _ _) (infi_le _ hs)) (trim_ge _ _) theorem trim_congr {m₁ m₂ : outer_measure α} (H : ∀ {s : set α}, is_measurable s → m₁ s = m₂ s) : m₁.trim = m₂.trim := by unfold trim; congr; funext s hs; exact H hs theorem trim_le_trim {m₁ m₂ : outer_measure α} (H : m₁ ≤ m₂) : m₁.trim ≤ m₂.trim := λ s, infi_le_infi $ λ f, infi_le_infi $ λ hs, ennreal.tsum_le_tsum $ λ b, infi_le_infi $ λ hf, H _ theorem le_trim_iff {m₁ m₂ : outer_measure α} : m₁ ≤ m₂.trim ↔ ∀ s, is_measurable s → m₁ s ≤ m₂ s := le_of_function.trans $ forall_congr $ λ s, le_infi_iff theorem trim_eq_infi (s : set α) : m.trim s = ⨅ t (st : s ⊆ t) (ht : is_measurable t), m t := begin refine le_antisymm (le_infi $ λ t, le_infi $ λ st, le_infi $ λ ht, _) (le_infi $ λ f, le_infi $ λ hf, _), { rw ← trim_eq m ht, exact (trim m).mono st }, { by_cases h : ∀i, is_measurable (f i), { refine infi_le_of_le _ (infi_le_of_le hf $ infi_le_of_le (is_measurable.Union h) _), rw congr_arg tsum _, {exact m.Union_nat _}, funext i, exact measure'_eq _ _ (h i) }, { cases not_forall.1 h with i hi, exact le_trans (le_infi $ λ h, hi.elim h) (ennreal.le_tsum i) } } end theorem trim_eq_infi' (s : set α) : m.trim s = ⨅ t : {t // s ⊆ t ∧ is_measurable t}, m t.1 := by simp [infi_subtype, infi_and, trim_eq_infi] theorem trim_trim (m : outer_measure α) : m.trim.trim = m.trim := le_antisymm (le_trim_iff.2 $ λ s hs, by simp [trim_eq _ hs, le_refl]) (trim_ge _) theorem trim_zero : (0 : outer_measure α).trim = 0 := ext $ λ s, le_antisymm (le_trans ((trim 0).mono (subset_univ s)) $ le_of_eq $ trim_eq _ is_measurable.univ) (zero_le _) theorem trim_add (m₁ m₂ : outer_measure α) : (m₁ + m₂).trim = m₁.trim + m₂.trim := ext $ λ s, begin simp [trim_eq_infi'], rw ennreal.infi_add_infi, rintro ⟨t₁, st₁, ht₁⟩ ⟨t₂, st₂, ht₂⟩, exact ⟨⟨_, subset_inter_iff.2 ⟨st₁, st₂⟩, ht₁.inter ht₂⟩, add_le_add' (m₁.mono' (inter_subset_left _ _)) (m₂.mono' (inter_subset_right _ _))⟩, end theorem trim_sum_ge {ι} (m : ι → outer_measure α) : sum (λ i, (m i).trim) ≤ (sum m).trim := λ s, by simp [trim_eq_infi]; exact λ t st ht, ennreal.tsum_le_tsum (λ i, infi_le_of_le t $ infi_le_of_le st $ infi_le _ ht) end outer_measure structure measure (α : Type*) [measurable_space α] extends outer_measure α := (m_Union {f : ℕ → set α} : (∀i, is_measurable (f i)) → pairwise (disjoint on f) → measure_of (⋃i, f i) = (∑i, measure_of (f i))) (trimmed : to_outer_measure.trim = to_outer_measure) /-- Measure projections for a measure space. For measurable sets this returns the measure assigned by the `measure_of` field in `measure`. But we can extend this to _all_ sets, but using the outer measure. This gives us monotonicity and subadditivity for all sets. -/ instance measure.has_coe_to_fun {α} [measurable_space α] : has_coe_to_fun (measure α) := ⟨λ _, set α → ennreal, λ m, m.to_outer_measure⟩ namespace measure def of_measurable {α} [measurable_space α] (m : Π (s : set α), is_measurable s → ennreal) (m0 : m ∅ is_measurable.empty = 0) (mU : ∀ {f : ℕ → set α} (h : ∀i, is_measurable (f i)), pairwise (disjoint on f) → m (⋃i, f i) (is_measurable.Union h) = (∑i, m (f i) (h i))) : measure α := { m_Union := λ f hf hd, show outer_measure' m m0 (Union f) = ∑ i, outer_measure' m m0 (f i), begin rw [outer_measure'_eq m m0 @mU, mU hf hd], congr, funext n, rw outer_measure'_eq m m0 @mU end, trimmed := show (outer_measure' m m0).trim = outer_measure' m m0, begin unfold outer_measure.trim, congr, funext s hs, exact outer_measure'_eq m m0 @mU hs end, ..outer_measure' m m0 } lemma of_measurable_apply {α} [measurable_space α] {m : Π (s : set α), is_measurable s → ennreal} {m0 : m ∅ is_measurable.empty = 0} {mU : ∀ {f : ℕ → set α} (h : ∀i, is_measurable (f i)), pairwise (disjoint on f) → m (⋃i, f i) (is_measurable.Union h) = (∑i, m (f i) (h i))} (s : set α) (hs : is_measurable s) : of_measurable m m0 @mU s = m s hs := outer_measure'_eq m m0 @mU hs @[extensionality] lemma ext {α} [measurable_space α] : ∀ {μ₁ μ₂ : measure α}, (∀s, is_measurable s → μ₁ s = μ₂ s) → μ₁ = μ₂ | ⟨m₁, u₁, h₁⟩ ⟨m₂, u₂, h₂⟩ h := by congr; rw [← h₁, ← h₂]; exact outer_measure.trim_congr h end measure section variables {α : Type*} {β : Type*} [measurable_space α] {μ μ₁ μ₂ : measure α} {s s₁ s₂ : set α} @[simp] lemma to_outer_measure_apply (s) : μ.to_outer_measure s = μ s := rfl lemma measure_eq_trim (s) : μ s = μ.to_outer_measure.trim s := by rw μ.trimmed; refl lemma measure_eq_infi (s) : μ s = ⨅ t (st : s ⊆ t) (ht : is_measurable t), μ t := by rw [measure_eq_trim, outer_measure.trim_eq_infi]; refl lemma measure_eq_outer_measure' : μ s = outer_measure' (λ s _, μ s) μ.empty s := measure_eq_trim _ lemma to_outer_measure_eq_outer_measure' : μ.to_outer_measure = outer_measure' (λ s _, μ s) μ.empty := μ.trimmed.symm lemma measure_eq_measure' (hs : is_measurable s) : μ s = measure' (λ s _, μ s) μ.empty s := by rw [measure_eq_outer_measure', outer_measure'_eq_measure' (λ s _, μ s) _ μ.m_Union hs] @[simp] lemma measure_empty : μ ∅ = 0 := μ.empty lemma measure_mono (h : s₁ ⊆ s₂) : μ s₁ ≤ μ s₂ := μ.mono h lemma measure_mono_null (h : s₁ ⊆ s₂) (h₂ : μ s₂ = 0) : μ s₁ = 0 := by rw [← le_zero_iff_eq, ← h₂]; exact measure_mono h lemma exists_is_measurable_superset_of_measure_eq_zero {s : set α} (h : μ s = 0) : ∃t, s ⊆ t ∧ is_measurable t ∧ μ t = 0 := begin rw [measure_eq_infi] at h, have h := (infi_eq_bot _).1 h, choose t ht using show ∀n:ℕ, ∃t, s ⊆ t ∧ is_measurable t ∧ μ t < n⁻¹, { assume n, have : (0 : ennreal) < n⁻¹ := (zero_lt_iff_ne_zero.2 $ ennreal.inv_ne_zero.2 $ ennreal.nat_ne_top _), rcases h _ this with ⟨t, ht⟩, use [t], simpa [(>), infi_lt_iff, -add_comm] using ht }, refine ⟨⋂n, t n, subset_Inter (λn, (ht n).1), is_measurable.Inter (λn, (ht n).2.1), _⟩, refine eq_of_le_of_forall_le_of_dense bot_le (assume r hr, _), rcases ennreal.exists_inv_nat_lt (ne_of_gt hr) with ⟨n, hn⟩, calc μ (⋂n, t n) ≤ μ (t n) : measure_mono (Inter_subset _ _) ... ≤ n⁻¹ : le_of_lt (ht n).2.2 ... ≤ r : le_of_lt hn end theorem measure_Union_le {β} [encodable β] (s : β → set α) : μ (⋃i, s i) ≤ (∑i, μ (s i)) := μ.to_outer_measure.Union _ lemma measure_Union_null {β} [encodable β] {s : β → set α} : (∀ i, μ (s i) = 0) → μ (⋃i, s i) = 0 := μ.to_outer_measure.Union_null theorem measure_union_le (s₁ s₂ : set α) : μ (s₁ ∪ s₂) ≤ μ s₁ + μ s₂ := μ.to_outer_measure.union _ _ lemma measure_union_null {s₁ s₂ : set α} : μ s₁ = 0 → μ s₂ = 0 → μ (s₁ ∪ s₂) = 0 := μ.to_outer_measure.union_null lemma measure_Union {β} [encodable β] {f : β → set α} (hn : pairwise (disjoint on f)) (h : ∀i, is_measurable (f i)) : μ (⋃i, f i) = (∑i, μ (f i)) := by rw [measure_eq_measure' (is_measurable.Union h), measure'_Union (λ s _, μ s) _ μ.m_Union hn h]; simp [measure_eq_measure', h] lemma measure_union (hd : disjoint s₁ s₂) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ := by rw [measure_eq_measure' (h₁.union h₂), measure'_union (λ s _, μ s) _ μ.m_Union hd h₁ h₂]; simp [measure_eq_measure', h₁, h₂] lemma measure_bUnion {s : set β} {f : β → set α} (hs : countable s) (hd : pairwise_on s (disjoint on f)) (h : ∀b∈s, is_measurable (f b)) : μ (⋃b∈s, f b) = ∑p:s, μ (f p.1) := begin haveI := hs.to_encodable, rw [← measure_Union, bUnion_eq_Union], { rintro ⟨i, hi⟩ ⟨j, hj⟩ ij x ⟨h₁, h₂⟩, exact hd i hi j hj (mt subtype.eq' ij:_) ⟨h₁, h₂⟩ }, { simpa } end lemma measure_sUnion {S : set (set α)} (hs : countable S) (hd : pairwise_on S disjoint) (h : ∀s∈S, is_measurable s) : μ (⋃₀ S) = ∑s:S, μ s.1 := by rw [sUnion_eq_bUnion, measure_bUnion hs hd h] lemma measure_diff {s₁ s₂ : set α} (h : s₂ ⊆ s₁) (h₁ : is_measurable s₁) (h₂ : is_measurable s₂) (h_fin : μ s₂ < ⊤) : μ (s₁ \ s₂) = μ s₁ - μ s₂ := begin refine (ennreal.add_sub_self' h_fin).symm.trans _, rw [← measure_union disjoint_diff h₂ (h₁.diff h₂), union_diff_cancel h] end lemma measure_Union_eq_supr_nat {s : ℕ → set α} (h : ∀i, is_measurable (s i)) (hs : monotone s) : μ (⋃i, s i) = (⨆i, μ (s i)) := begin refine le_antisymm _ (supr_le $ λ i, measure_mono $ subset_Union _ _), rw [← Union_disjointed, measure_Union disjoint_disjointed (is_measurable.disjointed h), ennreal.tsum_eq_supr_nat], refine supr_le (λ n, _), cases n, {apply zero_le _}, suffices : sum (finset.range n.succ) (λ i, μ (disjointed s i)) = μ (s n), { rw this, exact le_supr _ n }, rw [← Union_disjointed_of_mono hs, measure_Union, tsum_eq_sum], { apply sum_congr rfl, intros i hi, simp [finset.mem_range.1 hi] }, { intros i hi, simp [mt finset.mem_range.2 hi] }, { rintro i j ij x ⟨⟨_, ⟨_, rfl⟩, h₁⟩, ⟨_, ⟨_, rfl⟩, h₂⟩⟩, exact disjoint_disjointed i j ij ⟨h₁, h₂⟩ }, { intro i, by_cases h' : i < n.succ; simp [h', is_measurable.empty], apply is_measurable.disjointed h } end lemma measure_Inter_eq_infi_nat {s : ℕ → set α} (h : ∀i, is_measurable (s i)) (hs : ∀i j, i ≤ j → s j ⊆ s i) (hfin : ∃i, μ (s i) < ⊤) : μ (⋂i, s i) = (⨅i, μ (s i)) := begin rcases hfin with ⟨k, hk⟩, rw [← ennreal.sub_sub_cancel (by exact hk) (infi_le _ k), ennreal.sub_infi, ← ennreal.sub_sub_cancel (by exact hk) (measure_mono (Inter_subset _ k)), ← measure_diff (Inter_subset _ k) (h k) (is_measurable.Inter h) (lt_of_le_of_lt (measure_mono (Inter_subset _ k)) hk), diff_Inter_left, measure_Union_eq_supr_nat], { congr, funext i, cases le_total k i with ik ik, { exact measure_diff (hs _ _ ik) (h k) (h i) (lt_of_le_of_lt (measure_mono (hs _ _ ik)) hk) }, { rw [diff_eq_empty.2 (hs _ _ ik), measure_empty, ennreal.sub_eq_zero_of_le (measure_mono (hs _ _ ik))] } }, { exact λ i, (h k).diff (h i) }, { exact λ i j ij, diff_subset_diff_right (hs _ _ ij) } end lemma measure_eq_inter_diff {μ : measure α} {s t : set α} (hs : is_measurable s) (ht : is_measurable t) : μ s = μ (s ∩ t) + μ (s \ t) := have hd : disjoint (s ∩ t) (s \ t) := assume a ⟨⟨_, hs⟩, _, hns⟩, hns hs , by rw [← measure_union hd (hs.inter ht) (hs.diff ht), inter_union_diff s t] lemma tendsto_measure_Union {μ : measure α} {s : ℕ → set α} (hs : ∀n, is_measurable (s n)) (hm : monotone s) : tendsto (μ ∘ s) at_top (nhds (μ (⋃n, s n))) := begin rw measure_Union_eq_supr_nat hs hm, exact tendsto_at_top_supr_nat (μ ∘ s) (assume n m hnm, measure_mono $ hm $ hnm) end lemma tendsto_measure_Inter {μ : measure α} {s : ℕ → set α} (hs : ∀n, is_measurable (s n)) (hm : ∀n m, n ≤ m → s m ⊆ s n) (hf : ∃i, μ (s i) < ⊤): tendsto (μ ∘ s) at_top (nhds (μ (⋂n, s n))) := begin rw measure_Inter_eq_infi_nat hs hm hf, exact tendsto_at_top_infi_nat (μ ∘ s) (assume n m hnm, measure_mono $ hm _ _ $ hnm), end end def outer_measure.to_measure {α} (m : outer_measure α) [ms : measurable_space α] (h : ms ≤ m.caratheodory) : measure α := measure.of_measurable (λ s _, m s) m.empty (λ f hf hd, m.Union_eq_of_caratheodory (λ i, h _ (hf i)) hd) lemma le_to_outer_measure_caratheodory {α} [ms : measurable_space α] (μ : measure α) : ms ≤ μ.to_outer_measure.caratheodory := begin assume s hs, rw to_outer_measure_eq_outer_measure', refine outer_measure.caratheodory_is_measurable (λ t, le_infi $ λ ht, _), rw [← measure_eq_measure' (ht.inter hs), ← measure_eq_measure' (ht.diff hs), ← measure_union _ (ht.inter hs) (ht.diff hs), inter_union_diff], exact le_refl _, exact λ x ⟨⟨_, h₁⟩, _, h₂⟩, h₂ h₁ end lemma to_measure_to_outer_measure {α} (m : outer_measure α) [ms : measurable_space α] (h : ms ≤ m.caratheodory) : (m.to_measure h).to_outer_measure = m.trim := rfl @[simp] lemma to_measure_apply {α} (m : outer_measure α) [ms : measurable_space α] (h : ms ≤ m.caratheodory) {s : set α} (hs : is_measurable s) : m.to_measure h s = m s := m.trim_eq hs lemma to_outer_measure_to_measure {α : Type*} [ms : measurable_space α] {μ : measure α} : μ.to_outer_measure.to_measure (le_to_outer_measure_caratheodory _) = μ := measure.ext $ λ s, μ.to_outer_measure.trim_eq namespace measure variables {α : Type*} {β : Type*} {γ : Type*} [measurable_space α] [measurable_space β] [measurable_space γ] instance : has_zero (measure α) := ⟨{ to_outer_measure := 0, m_Union := λ f hf hd, tsum_zero.symm, trimmed := outer_measure.trim_zero }⟩ @[simp] theorem zero_to_outer_measure : (0 : measure α).to_outer_measure = 0 := rfl @[simp] theorem zero_apply (s : set α) : (0 : measure α) s = 0 := rfl instance : inhabited (measure α) := ⟨0⟩ instance : has_add (measure α) := ⟨λμ₁ μ₂, { to_outer_measure := μ₁.to_outer_measure + μ₂.to_outer_measure, m_Union := λs hs hd, show μ₁ (⋃ i, s i) + μ₂ (⋃ i, s i) = ∑ i, μ₁ (s i) + μ₂ (s i), by rw [ennreal.tsum_add, measure_Union hd hs, measure_Union hd hs], trimmed := by rw [outer_measure.trim_add, μ₁.trimmed, μ₂.trimmed] }⟩ @[simp] theorem add_to_outer_measure (μ₁ μ₂ : measure α) : (μ₁ + μ₂).to_outer_measure = μ₁.to_outer_measure + μ₂.to_outer_measure := rfl @[simp] theorem add_apply (μ₁ μ₂ : measure α) (s : set α) : (μ₁ + μ₂) s = μ₁ s + μ₂ s := rfl instance : add_comm_monoid (measure α) := { zero := 0, add := (+), add_assoc := assume a b c, ext $ assume s hs, add_assoc _ _ _, add_comm := assume a b, ext $ assume s hs, add_comm _ _, zero_add := assume a, ext $ assume s hs, zero_add _, add_zero := assume a, ext $ assume s hs, add_zero _ } instance : partial_order (measure α) := { le := λm₁ m₂, ∀ s, is_measurable s → m₁ s ≤ m₂ s, le_refl := assume m s hs, le_refl _, le_trans := assume m₁ m₂ m₃ h₁ h₂ s hs, le_trans (h₁ s hs) (h₂ s hs), le_antisymm := assume m₁ m₂ h₁ h₂, ext $ assume s hs, le_antisymm (h₁ s hs) (h₂ s hs) } theorem le_iff {μ₁ μ₂ : measure α} : μ₁ ≤ μ₂ ↔ ∀ s, is_measurable s → μ₁ s ≤ μ₂ s := iff.rfl theorem to_outer_measure_le {μ₁ μ₂ : measure α} : μ₁.to_outer_measure ≤ μ₂.to_outer_measure ↔ μ₁ ≤ μ₂ := by rw [← μ₂.trimmed, outer_measure.le_trim_iff]; refl theorem le_iff' {μ₁ μ₂ : measure α} : μ₁ ≤ μ₂ ↔ ∀ s, μ₁ s ≤ μ₂ s := to_outer_measure_le.symm section variables {m : set (measure α)} {μ : measure α} lemma Inf_caratheodory (s : set α) (hs : is_measurable s) : (Inf (measure.to_outer_measure '' m)).caratheodory.is_measurable s := begin rw [outer_measure.Inf_eq_of_function_Inf_gen], refine outer_measure.caratheodory_is_measurable (assume t, _), by_cases ht : t = ∅, { simp [ht] }, simp only [outer_measure.Inf_gen_nonempty1 _ _ ht, le_infi_iff, ball_image_iff, to_outer_measure_apply, measure_eq_infi t], assume μ hμ u htu hu, have hm : ∀{s t}, s ⊆ t → outer_measure.Inf_gen (to_outer_measure '' m) s ≤ μ t, { assume s t hst, rw [outer_measure.Inf_gen_nonempty2 _ _ (mem_image_of_mem _ hμ)], refine infi_le_of_le (μ.to_outer_measure) (infi_le_of_le (mem_image_of_mem _ hμ) _), rw [to_outer_measure_apply], refine measure_mono hst }, rw [measure_eq_inter_diff hu hs], refine add_le_add' (hm $ inter_subset_inter_left _ htu) (hm $ diff_subset_diff_left htu) end instance : has_Inf (measure α) := ⟨λm, (Inf (to_outer_measure '' m)).to_measure $ Inf_caratheodory⟩ lemma Inf_apply {m : set (measure α)} {s : set α} (hs : is_measurable s) : Inf m s = Inf (to_outer_measure '' m) s := to_measure_apply _ _ hs private lemma Inf_le (h : μ ∈ m) : Inf m ≤ μ := have Inf (to_outer_measure '' m) ≤ μ.to_outer_measure := Inf_le (mem_image_of_mem _ h), assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s private lemma le_Inf (h : ∀μ' ∈ m, μ ≤ μ') : μ ≤ Inf m := have μ.to_outer_measure ≤ Inf (to_outer_measure '' m) := le_Inf $ ball_image_of_ball $ assume μ hμ, to_outer_measure_le.2 $ h _ hμ, assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s instance : has_Sup (measure α) := ⟨λs, Inf {μ' | ∀μ∈s, μ ≤ μ' }⟩ private lemma le_Sup (h : μ ∈ m) : μ ≤ Sup m := le_Inf $ assume μ' h', h' _ h private lemma Sup_le (h : ∀μ' ∈ m, μ' ≤ μ) : Sup m ≤ μ := Inf_le h instance : order_bot (measure α) := { bot := 0, bot_le := assume a s hs, bot_le, .. measure.partial_order } instance : order_top (measure α) := { top := (⊤ : outer_measure α).to_measure (by rw [outer_measure.top_caratheodory]; exact le_top), le_top := assume a s hs, by by_cases s = ∅; simp [h, to_measure_apply ⊤ _ hs, outer_measure.top_apply], .. measure.partial_order } instance : complete_lattice (measure α) := { Inf := Inf, Sup := Sup, inf := λa b, Inf {a, b}, sup := λa b, Sup {a, b}, le_Sup := assume s μ h, le_Sup h, Sup_le := assume s μ h, Sup_le h, Inf_le := assume s μ h, Inf_le h, le_Inf := assume s μ h, le_Inf h, le_sup_left := assume a b, le_Sup $ by simp, le_sup_right := assume a b, le_Sup $ by simp, sup_le := assume a b c hac hbc, Sup_le $ by simp [*, or_imp_distrib] {contextual := tt}, inf_le_left := assume a b, Inf_le $ by simp, inf_le_right := assume a b, Inf_le $ by simp, le_inf := assume a b c hac hbc, le_Inf $ by simp [*, or_imp_distrib] {contextual := tt}, .. measure.partial_order, .. measure.lattice.order_top, .. measure.lattice.order_bot } end def map (f : α → β) (μ : measure α) : measure β := if hf : measurable f then (μ.to_outer_measure.map f).to_measure $ λ s hs t, le_to_outer_measure_caratheodory μ _ (hf _ hs) (f ⁻¹' t) else 0 variables {μ ν : measure α} @[simp] theorem map_apply {f : α → β} (hf : measurable f) {s : set β} (hs : is_measurable s) : (map f μ : measure β) s = μ (f ⁻¹' s) := by rw [map, dif_pos hf, to_measure_apply _ _ hs]; refl @[simp] lemma map_id : map id μ = μ := ext $ λ s, map_apply measurable_id lemma map_map {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) : map g (map f μ) = map (g ∘ f) μ := ext $ λ s hs, by simp [hf, hg, hs, hg.preimage hs, hg.comp hf]; rw ← preimage_comp /-- The dirac measure. -/ def dirac (a : α) : measure α := (outer_measure.dirac a).to_measure (by simp) @[simp] lemma dirac_apply (a : α) {s : set α} (hs : is_measurable s) : (dirac a : measure α) s = ⨆ h : a ∈ s, 1 := to_measure_apply _ _ hs /-- Sum of an indexed family of measures. -/ def sum {ι : Type*} (f : ι → measure α) : measure α := (outer_measure.sum (λ i, (f i).to_outer_measure)).to_measure $ le_trans (by exact le_infi (λ i, le_to_outer_measure_caratheodory _)) (outer_measure.le_sum_caratheodory _) /-- Counting measure on any measurable space. -/ def count : measure α := sum dirac @[class] def is_complete {α} {_:measurable_space α} (μ : measure α) : Prop := ∀ s, μ s = 0 → is_measurable s /-- The "almost everywhere" filter of co-null sets. -/ def a_e (μ : measure α) : filter α := { sets := {s | μ (-s) = 0}, univ_sets := by simp [measure_empty], inter_sets := λ s t hs ht, by simp [compl_inter]; exact measure_union_null hs ht, sets_of_superset := λ s t hs hst, measure_mono_null (set.compl_subset_compl.2 hst) hs } lemma mem_a_e_iff (s : set α) : s ∈ μ.a_e.sets ↔ μ (- s) = 0 := iff.refl _ end measure end measure_theory section is_complete open measure_theory variables {α : Type*} [measurable_space α] (μ : measure α) def is_null_measurable (s : set α) : Prop := ∃ t z, s = t ∪ z ∧ is_measurable t ∧ μ z = 0 theorem is_null_measurable_iff {μ : measure α} {s : set α} : is_null_measurable μ s ↔ ∃ t, t ⊆ s ∧ is_measurable t ∧ μ (s \ t) = 0 := begin split, { rintro ⟨t, z, rfl, ht, hz⟩, refine ⟨t, set.subset_union_left _ _, ht, measure_mono_null _ hz⟩, simp [union_diff_left, diff_subset] }, { rintro ⟨t, st, ht, hz⟩, exact ⟨t, _, (union_diff_cancel st).symm, ht, hz⟩ } end theorem is_null_measurable_measure_eq {μ : measure α} {s t : set α} (st : t ⊆ s) (hz : μ (s \ t) = 0) : μ s = μ t := begin refine le_antisymm _ (measure_mono st), have := measure_union_le t (s \ t), rw [union_diff_cancel st, hz] at this, simpa end theorem is_measurable.is_null_measurable {s : set α} (hs : is_measurable s) : is_null_measurable μ s := ⟨s, ∅, by simp, hs, μ.empty⟩ theorem is_null_measurable_of_complete [c : μ.is_complete] {s : set α} : is_null_measurable μ s ↔ is_measurable s := ⟨by rintro ⟨t, z, rfl, ht, hz⟩; exact is_measurable.union ht (c _ hz), λ h, h.is_null_measurable _⟩ variables {μ} theorem is_null_measurable.union_null {s z : set α} (hs : is_null_measurable μ s) (hz : μ z = 0) : is_null_measurable μ (s ∪ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, exact ⟨t, z' ∪ z, set.union_assoc _ _ _, ht, le_zero_iff_eq.1 (le_trans (measure_union_le _ _) $ by simp [hz, hz'])⟩ end theorem null_is_null_measurable {z : set α} (hz : μ z = 0) : is_null_measurable μ z := by simpa using (is_measurable.empty.is_null_measurable _).union_null hz theorem is_null_measurable.Union_nat {s : ℕ → set α} (hs : ∀ i, is_null_measurable μ (s i)) : is_null_measurable μ (Union s) := begin choose t ht using assume i, is_null_measurable_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, refine is_null_measurable_iff.2 ⟨Union t, Union_subset_Union st, is_measurable.Union ht, measure_mono_null _ (measure_Union_null hz)⟩, rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (λ i, by rw ← diff_subset_iff) end theorem is_measurable.diff_null {s z : set α} (hs : is_measurable s) (hz : μ z = 0) : is_null_measurable μ (s \ z) := begin rw measure_eq_infi at hz, choose f hf using show ∀ q : {q:ℚ//q>0}, ∃ t:set α, z ⊆ t ∧ is_measurable t ∧ μ t < (nnreal.of_real q.1 : ennreal), { rintro ⟨ε, ε0⟩, have : 0 < (nnreal.of_real ε : ennreal), { simpa using ε0 }, rw ← hz at this, simpa [infi_lt_iff] }, refine is_null_measurable_iff.2 ⟨s \ Inter f, diff_subset_diff_right (subset_Inter (λ i, (hf i).1)), hs.diff (is_measurable.Inter (λ i, (hf i).2.1)), measure_mono_null _ (le_zero_iff_eq.1 $ le_of_not_lt $ λ h, _)⟩, { exact Inter f }, { rw [diff_subset_iff, diff_union_self], exact subset.trans (diff_subset _ _) (subset_union_left _ _) }, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨ε, ε0', ε0, h⟩, simp at ε0, apply not_le_of_lt (lt_trans (hf ⟨ε, ε0⟩).2.2 h), exact measure_mono (Inter_subset _ _) end theorem is_null_measurable.diff_null {s z : set α} (hs : is_null_measurable μ s) (hz : μ z = 0) : is_null_measurable μ (s \ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, rw [set.union_diff_distrib], exact (ht.diff_null hz).union_null (measure_mono_null (diff_subset _ _) hz') end theorem is_null_measurable.compl {s : set α} (hs : is_null_measurable μ s) : is_null_measurable μ (-s) := begin rcases hs with ⟨t, z, rfl, ht, hz⟩, rw compl_union, exact ht.compl.diff_null hz end def null_measurable {α : Type u} [measurable_space α] (μ : measure α) : measurable_space α := { is_measurable := is_null_measurable μ, is_measurable_empty := is_measurable.empty.is_null_measurable _, is_measurable_compl := λ s hs, hs.compl, is_measurable_Union := λ f, is_null_measurable.Union_nat } def completion {α : Type u} [measurable_space α] (μ : measure α) : @measure_theory.measure α (null_measurable μ) := { to_outer_measure := μ.to_outer_measure, m_Union := λ s hs hd, show μ (Union s) = ∑ i, μ (s i), begin choose t ht using assume i, is_null_measurable_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, rw is_null_measurable_measure_eq (Union_subset_Union st), { rw measure_Union _ ht, { congr, funext i, exact (is_null_measurable_measure_eq (st i) (hz i)).symm }, { rintro i j ij x ⟨h₁, h₂⟩, exact hd i j ij ⟨st i h₁, st j h₂⟩ } }, { refine measure_mono_null _ (measure_Union_null hz), rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (λ i, by rw ← diff_subset_iff) } end, trimmed := begin letI := null_measurable μ, refine le_antisymm (λ s, _) (outer_measure.trim_ge _), rw outer_measure.trim_eq_infi, dsimp, clear _inst, rw measure_eq_infi s, exact infi_le_infi (λ t, infi_le_infi $ λ st, infi_le_infi2 $ λ ht, ⟨ht.is_null_measurable _, le_refl _⟩) end } instance completion.is_complete {α : Type u} [measurable_space α] (μ : measure α) : (completion μ).is_complete := λ z hz, null_is_null_measurable hz end is_complete namespace measure_theory /-- A measure space is a measurable space equipped with a measure, referred to as `volume`. -/ class measure_space (α : Type*) extends measurable_space α := (μ {} : measure α) section measure_space variables {α : Type*} [measure_space α] {s₁ s₂ : set α} open measure_space def volume : set α → ennreal := @μ α _ @[simp] lemma volume_empty : volume (∅ : set α) = 0 := μ.empty lemma volume_mono : s₁ ⊆ s₂ → volume s₁ ≤ volume s₂ := measure_mono lemma volume_mono_null : s₁ ⊆ s₂ → volume s₂ = 0 → volume s₁ = 0 := measure_mono_null theorem volume_Union_le {β} [encodable β] : ∀ (s : β → set α), volume (⋃i, s i) ≤ (∑i, volume (s i)) := measure_Union_le lemma volume_Union_null {β} [encodable β] {s : β → set α} : (∀ i, volume (s i) = 0) → volume (⋃i, s i) = 0 := measure_Union_null theorem volume_union_le : ∀ (s₁ s₂ : set α), volume (s₁ ∪ s₂) ≤ volume s₁ + volume s₂ := measure_union_le lemma volume_union_null : volume s₁ = 0 → volume s₂ = 0 → volume (s₁ ∪ s₂) = 0 := measure_union_null lemma volume_Union {β} [encodable β] {f : β → set α} : pairwise (disjoint on f) → (∀i, is_measurable (f i)) → volume (⋃i, f i) = (∑i, volume (f i)) := measure_Union lemma volume_union : disjoint s₁ s₂ → is_measurable s₁ → is_measurable s₂ → volume (s₁ ∪ s₂) = volume s₁ + volume s₂ := measure_union lemma volume_bUnion {β} {s : set β} {f : β → set α} : countable s → pairwise_on s (disjoint on f) → (∀b∈s, is_measurable (f b)) → volume (⋃b∈s, f b) = ∑p:s, volume (f p.1) := measure_bUnion lemma volume_sUnion {S : set (set α)} : countable S → pairwise_on S disjoint → (∀s∈S, is_measurable s) → volume (⋃₀ S) = ∑s:S, volume s.1 := measure_sUnion lemma volume_bUnion_finset {β} {s : finset β} {f : β → set α} (hd : pairwise_on ↑s (disjoint on f)) (hm : ∀b∈s, is_measurable (f b)) : volume (⋃b∈s, f b) = s.sum (λp, volume (f p)) := show volume (⋃b∈(↑s : set β), f b) = s.sum (λp, volume (f p)), begin rw [volume_bUnion (countable_finite (finset.finite_to_set s)) hd hm, tsum_eq_sum], { show s.attach.sum (λb:(↑s : set β), volume (f b)) = s.sum (λb, volume (f b)), exact @finset.sum_attach _ _ s _ (λb, volume (f b)) }, simp end lemma volume_diff : s₂ ⊆ s₁ → is_measurable s₁ → is_measurable s₂ → volume s₂ < ⊤ → volume (s₁ \ s₂) = volume s₁ - volume s₂ := measure_diff /-- `∀ₘ a:α, p a` states that the property `p` is almost everywhere true in the measure space associated with `α`. This means that the measure of the complementary of `p` is `0`. In a probability measure, the measure of `p` is `1`, when `p` is measurable. -/ def all_ae (p : α → Prop) : Prop := { a | p a } ∈ (@measure_space.μ α _).a_e notation `∀ₘ` binders `, ` r:(scoped P, all_ae P) := r lemma all_ae_congr {p q : α → Prop} (h : ∀ₘ a, p a ↔ q a) : (∀ₘ a, p a) ↔ (∀ₘ a, q a) := iff.intro (assume h', by filter_upwards [h, h'] assume a hpq hp, hpq.1 hp) (assume h', by filter_upwards [h, h'] assume a hpq hq, hpq.2 hq) lemma all_ae_iff {p : α → Prop} : (∀ₘ a, p a) ↔ volume { a | ¬ p a } = 0 := iff.refl _ lemma all_ae_of_all {p : α → Prop} : (∀a, p a) → ∀ₘ a, p a := assume h, by {rw all_ae_iff, convert volume_empty, simp only [h, not_true], reflexivity} lemma all_ae_all_iff {ι : Type*} [encodable ι] {p : α → ι → Prop} : (∀ₘ a, ∀i, p a i) ↔ (∀i, ∀ₘ a, p a i):= begin refine iff.intro (assume h i, _) (assume h, _), { filter_upwards [h] assume a ha, ha i }, { have h := measure_Union_null h, rw [← compl_Inter] at h, filter_upwards [h] assume a, mem_Inter.1 } end end measure_space end measure_theory
63257b2f047686fc7731c2b1b36e79d410ec9eaf
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/module/submodule/basic.lean
1ffe24a81fd9e5083a27a8eadf6d84aaeb25c6ac
[ "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
17,534
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro -/ import algebra.module.linear_map import algebra.module.equiv import group_theory.group_action.sub_mul_action /-! # Submodules of a module In this file we define * `submodule R M` : a subset of a `module` `M` that contains zero and is closed with respect to addition and scalar multiplication. * `subspace k M` : an abbreviation for `submodule` assuming that `k` is a `field`. ## Tags submodule, subspace, linear map -/ open function open_locale big_operators universes u'' u' u v w variables {G : Type u''} {S : Type u'} {R : Type u} {M : Type v} {ι : Type w} set_option old_structure_cmd true /-- `submodule_class S R M` says `S` is a type of submodules `s ≤ M`. -/ class submodule_class (S : Type*) (R M : out_param $ Type*) [add_zero_class M] [has_smul R M] [set_like S M] [add_submonoid_class S M] extends smul_mem_class S R M /-- A submodule of a module is one which is closed under vector operations. This is a sufficient condition for the subset of vectors in the submodule to themselves form a module. -/ structure submodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [module R M] extends add_submonoid M, sub_mul_action R M : Type v. /-- Reinterpret a `submodule` as an `add_submonoid`. -/ add_decl_doc submodule.to_add_submonoid /-- Reinterpret a `submodule` as an `sub_mul_action`. -/ add_decl_doc submodule.to_sub_mul_action namespace submodule variables [semiring R] [add_comm_monoid M] [module R M] instance : set_like (submodule R M) M := { coe := submodule.carrier, coe_injective' := λ p q h, by cases p; cases q; congr' } instance : add_submonoid_class (submodule R M) M := { zero_mem := zero_mem', add_mem := add_mem' } instance : submodule_class (submodule R M) R M := { smul_mem := smul_mem' } @[simp] theorem mem_to_add_submonoid (p : submodule R M) (x : M) : x ∈ p.to_add_submonoid ↔ x ∈ p := iff.rfl variables {p q : submodule R M} @[simp] lemma mem_mk {S : set M} {x : M} (h₁ h₂ h₃) : x ∈ (⟨S, h₁, h₂, h₃⟩ : submodule R M) ↔ x ∈ S := iff.rfl @[simp] lemma coe_set_mk (S : set M) (h₁ h₂ h₃) : ((⟨S, h₁, h₂, h₃⟩ : submodule R M) : set M) = S := rfl @[simp] lemma mk_le_mk {S S' : set M} (h₁ h₂ h₃ h₁' h₂' h₃') : (⟨S, h₁, h₂, h₃⟩ : submodule R M) ≤ (⟨S', h₁', h₂', h₃'⟩ : submodule R M) ↔ S ⊆ S' := iff.rfl @[ext] theorem ext (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := set_like.ext h /-- Copy of a submodule with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (p : submodule R M) (s : set M) (hs : s = ↑p) : submodule R M := { carrier := s, zero_mem' := hs.symm ▸ p.zero_mem', add_mem' := λ _ _, hs.symm ▸ p.add_mem', smul_mem' := hs.symm ▸ p.smul_mem' } @[simp] lemma coe_copy (S : submodule R M) (s : set M) (hs : s = ↑S) : (S.copy s hs : set M) = s := rfl lemma copy_eq (S : submodule R M) (s : set M) (hs : s = ↑S) : S.copy s hs = S := set_like.coe_injective hs theorem to_add_submonoid_injective : injective (to_add_submonoid : submodule R M → add_submonoid M) := λ p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h) @[simp] theorem to_add_submonoid_eq : p.to_add_submonoid = q.to_add_submonoid ↔ p = q := to_add_submonoid_injective.eq_iff @[mono] lemma to_add_submonoid_strict_mono : strict_mono (to_add_submonoid : submodule R M → add_submonoid M) := λ _ _, id lemma to_add_submonoid_le : p.to_add_submonoid ≤ q.to_add_submonoid ↔ p ≤ q := iff.rfl @[mono] lemma to_add_submonoid_mono : monotone (to_add_submonoid : submodule R M → add_submonoid M) := to_add_submonoid_strict_mono.monotone @[simp] theorem coe_to_add_submonoid (p : submodule R M) : (p.to_add_submonoid : set M) = p := rfl theorem to_sub_mul_action_injective : injective (to_sub_mul_action : submodule R M → sub_mul_action R M) := λ p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h) @[simp] theorem to_sub_mul_action_eq : p.to_sub_mul_action = q.to_sub_mul_action ↔ p = q := to_sub_mul_action_injective.eq_iff @[mono] lemma to_sub_mul_action_strict_mono : strict_mono (to_sub_mul_action : submodule R M → sub_mul_action R M) := λ _ _, id @[mono] lemma to_sub_mul_action_mono : monotone (to_sub_mul_action : submodule R M → sub_mul_action R M) := to_sub_mul_action_strict_mono.monotone @[simp] theorem coe_to_sub_mul_action (p : submodule R M) : (p.to_sub_mul_action : set M) = p := rfl end submodule namespace submodule_class variables [semiring R] [add_comm_monoid M] [module R M] {A : Type*} [set_like A M] [add_submonoid_class A M] [hA : submodule_class A R M] (S' : A) include hA /-- A submodule of a `module` is a `module`. -/ @[priority 75] -- Prefer subclasses of `module` over `submodule_class`. instance to_module : module R S' := subtype.coe_injective.module R (add_submonoid_class.subtype S') (set_like.coe_smul S') /-- The natural `R`-linear map from a submodule of an `R`-module `M` to `M`. -/ protected def subtype : S' →ₗ[R] M := ⟨coe, λ _ _, rfl, λ _ _, rfl⟩ @[simp] protected theorem coe_subtype : (submodule_class.subtype S' : S' → M) = coe := rfl end submodule_class namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] -- We can infer the module structure implicitly from the bundled submodule, -- rather than via typeclass resolution. variables {module_M : module R M} variables {p q : submodule R M} variables {r : R} {x y : M} variables (p) @[simp] lemma mem_carrier : x ∈ p.carrier ↔ x ∈ (p : set M) := iff.rfl @[simp] protected lemma zero_mem : (0 : M) ∈ p := zero_mem _ protected lemma add_mem (h₁ : x ∈ p) (h₂ : y ∈ p) : x + y ∈ p := add_mem h₁ h₂ lemma smul_mem (r : R) (h : x ∈ p) : r • x ∈ p := p.smul_mem' r h lemma smul_of_tower_mem [has_smul S R] [has_smul S M] [is_scalar_tower S R M] (r : S) (h : x ∈ p) : r • x ∈ p := p.to_sub_mul_action.smul_of_tower_mem r h protected lemma sum_mem {t : finset ι} {f : ι → M} : (∀c∈t, f c ∈ p) → (∑ i in t, f i) ∈ p := sum_mem lemma sum_smul_mem {t : finset ι} {f : ι → M} (r : ι → R) (hyp : ∀ c ∈ t, f c ∈ p) : (∑ i in t, r i • f i) ∈ p := sum_mem (λ i hi, smul_mem _ _ (hyp i hi)) @[simp] lemma smul_mem_iff' [group G] [mul_action G M] [has_smul G R] [is_scalar_tower G R M] (g : G) : g • x ∈ p ↔ x ∈ p := p.to_sub_mul_action.smul_mem_iff' g instance : has_add p := ⟨λx y, ⟨x.1 + y.1, add_mem x.2 y.2⟩⟩ instance : has_zero p := ⟨⟨0, zero_mem _⟩⟩ instance : inhabited p := ⟨0⟩ instance [has_smul S R] [has_smul S M] [is_scalar_tower S R M] : has_smul S p := ⟨λ c x, ⟨c • x.1, smul_of_tower_mem _ c x.2⟩⟩ instance [has_smul S R] [has_smul S M] [is_scalar_tower S R M] : is_scalar_tower S R p := p.to_sub_mul_action.is_scalar_tower instance is_scalar_tower' {S' : Type*} [has_smul S R] [has_smul S M] [has_smul S' R] [has_smul S' M] [has_smul S S'] [is_scalar_tower S' R M] [is_scalar_tower S S' M] [is_scalar_tower S R M] : is_scalar_tower S S' p := p.to_sub_mul_action.is_scalar_tower' instance [has_smul S R] [has_smul S M] [is_scalar_tower S R M] [has_smul Sᵐᵒᵖ R] [has_smul Sᵐᵒᵖ M] [is_scalar_tower Sᵐᵒᵖ R M] [is_central_scalar S M] : is_central_scalar S p := p.to_sub_mul_action.is_central_scalar protected lemma nonempty : (p : set M).nonempty := ⟨0, p.zero_mem⟩ @[simp] lemma mk_eq_zero {x} (h : x ∈ p) : (⟨x, h⟩ : p) = 0 ↔ x = 0 := subtype.ext_iff_val variables {p} @[simp, norm_cast] lemma coe_eq_zero {x : p} : (x : M) = 0 ↔ x = 0 := (set_like.coe_eq_coe : (x : M) = (0 : p) ↔ x = 0) @[simp, norm_cast] lemma coe_add (x y : p) : (↑(x + y) : M) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : p) : M) = 0 := rfl @[norm_cast] lemma coe_smul (r : R) (x : p) : ((r • x : p) : M) = r • ↑x := rfl @[simp, norm_cast] lemma coe_smul_of_tower [has_smul S R] [has_smul S M] [is_scalar_tower S R M] (r : S) (x : p) : ((r • x : p) : M) = r • ↑x := rfl @[simp, norm_cast] lemma coe_mk (x : M) (hx : x ∈ p) : ((⟨x, hx⟩ : p) : M) = x := rfl @[simp] lemma coe_mem (x : p) : (x : M) ∈ p := x.2 variables (p) instance : add_comm_monoid p := { add := (+), zero := 0, .. p.to_add_submonoid.to_add_comm_monoid } instance module' [semiring S] [has_smul S R] [module S M] [is_scalar_tower S R M] : module S p := by refine {smul := (•), ..p.to_sub_mul_action.mul_action', ..}; { intros, apply set_coe.ext, simp [smul_add, add_smul, mul_smul] } instance : module R p := p.module' instance no_zero_smul_divisors [no_zero_smul_divisors R M] : no_zero_smul_divisors R p := ⟨λ c x h, have c = 0 ∨ (x : M) = 0, from eq_zero_or_eq_zero_of_smul_eq_zero (congr_arg coe h), this.imp_right (@subtype.ext_iff _ _ x 0).mpr⟩ /-- Embedding of a submodule `p` to the ambient space `M`. -/ protected def subtype : p →ₗ[R] M := by refine {to_fun := coe, ..}; simp [coe_smul] theorem subtype_apply (x : p) : p.subtype x = x := rfl @[simp] lemma coe_subtype : ((submodule.subtype p) : p → M) = coe := rfl lemma injective_subtype : injective p.subtype := subtype.coe_injective /-- Note the `add_submonoid` version of this lemma is called `add_submonoid.coe_finset_sum`. -/ @[simp] lemma coe_sum (x : ι → p) (s : finset ι) : ↑(∑ i in s, x i) = ∑ i in s, (x i : M) := map_sum p.subtype _ _ section restrict_scalars variables (S) [semiring S] [module S M] [module R M] [has_smul S R] [is_scalar_tower S R M] /-- `V.restrict_scalars S` is the `S`-submodule of the `S`-module given by restriction of scalars, corresponding to `V`, an `R`-submodule of the original `R`-module. -/ def restrict_scalars (V : submodule R M) : submodule S M := { carrier := V, zero_mem' := V.zero_mem, smul_mem' := λ c m h, V.smul_of_tower_mem c h, add_mem' := λ x y hx hy, V.add_mem hx hy } @[simp] lemma coe_restrict_scalars (V : submodule R M) : (V.restrict_scalars S : set M) = V := rfl @[simp] lemma restrict_scalars_mem (V : submodule R M) (m : M) : m ∈ V.restrict_scalars S ↔ m ∈ V := iff.refl _ @[simp] lemma restrict_scalars_self (V : submodule R M) : V.restrict_scalars R = V := set_like.coe_injective rfl variables (R S M) lemma restrict_scalars_injective : function.injective (restrict_scalars S : submodule R M → submodule S M) := λ V₁ V₂ h, ext $ set.ext_iff.1 (set_like.ext'_iff.1 h : _) @[simp] lemma restrict_scalars_inj {V₁ V₂ : submodule R M} : restrict_scalars S V₁ = restrict_scalars S V₂ ↔ V₁ = V₂ := (restrict_scalars_injective S _ _).eq_iff /-- Even though `p.restrict_scalars S` has type `submodule S M`, it is still an `R`-module. -/ instance restrict_scalars.orig_module (p : submodule R M) : module R (p.restrict_scalars S) := (by apply_instance : module R p) instance (p : submodule R M) : is_scalar_tower S R (p.restrict_scalars S) := { smul_assoc := λ r s x, subtype.ext $ smul_assoc r s (x : M) } /-- `restrict_scalars S` is an embedding of the lattice of `R`-submodules into the lattice of `S`-submodules. -/ @[simps] def restrict_scalars_embedding : submodule R M ↪o submodule S M := { to_fun := restrict_scalars S, inj' := restrict_scalars_injective S R M, map_rel_iff' := λ p q, by simp [set_like.le_def] } /-- Turning `p : submodule R M` into an `S`-submodule gives the same module structure as turning it into a type and adding a module structure. -/ @[simps {simp_rhs := tt}] def restrict_scalars_equiv (p : submodule R M) : p.restrict_scalars S ≃ₗ[R] p := { to_fun := id, inv_fun := id, map_smul' := λ c x, rfl, .. add_equiv.refl p } end restrict_scalars end add_comm_monoid section add_comm_group variables [ring R] [add_comm_group M] variables {module_M : module R M} variables (p p' : submodule R M) variables {r : R} {x y : M} instance [module R M] : add_subgroup_class (submodule R M) M := { neg_mem := λ p x, p.to_sub_mul_action.neg_mem, .. submodule.add_submonoid_class } protected lemma neg_mem (hx : x ∈ p) : -x ∈ p := neg_mem hx /-- Reinterpret a submodule as an additive subgroup. -/ def to_add_subgroup : add_subgroup M := { neg_mem' := λ _, p.neg_mem , .. p.to_add_submonoid } @[simp] lemma coe_to_add_subgroup : (p.to_add_subgroup : set M) = p := rfl @[simp] lemma mem_to_add_subgroup : x ∈ p.to_add_subgroup ↔ x ∈ p := iff.rfl include module_M theorem to_add_subgroup_injective : injective (to_add_subgroup : submodule R M → add_subgroup M) | p q h := set_like.ext (set_like.ext_iff.1 h : _) @[simp] theorem to_add_subgroup_eq : p.to_add_subgroup = p'.to_add_subgroup ↔ p = p' := to_add_subgroup_injective.eq_iff @[mono] lemma to_add_subgroup_strict_mono : strict_mono (to_add_subgroup : submodule R M → add_subgroup M) := λ _ _, id lemma to_add_subgroup_le : p.to_add_subgroup ≤ p'.to_add_subgroup ↔ p ≤ p' := iff.rfl @[mono] lemma to_add_subgroup_mono : monotone (to_add_subgroup : submodule R M → add_subgroup M) := to_add_subgroup_strict_mono.monotone omit module_M protected lemma sub_mem : x ∈ p → y ∈ p → x - y ∈ p := sub_mem protected lemma neg_mem_iff : -x ∈ p ↔ x ∈ p := neg_mem_iff protected lemma add_mem_iff_left : y ∈ p → (x + y ∈ p ↔ x ∈ p) := add_mem_cancel_right protected lemma add_mem_iff_right : x ∈ p → (x + y ∈ p ↔ y ∈ p) := add_mem_cancel_left protected lemma coe_neg (x : p) : ((-x : p) : M) = -x := add_subgroup_class.coe_neg _ protected lemma coe_sub (x y : p) : (↑(x - y) : M) = ↑x - ↑y := add_subgroup_class.coe_sub _ _ lemma sub_mem_iff_left (hy : y ∈ p) : (x - y) ∈ p ↔ x ∈ p := by rw [sub_eq_add_neg, p.add_mem_iff_left (p.neg_mem hy)] lemma sub_mem_iff_right (hx : x ∈ p) : (x - y) ∈ p ↔ y ∈ p := by rw [sub_eq_add_neg, p.add_mem_iff_right hx, p.neg_mem_iff] instance : add_comm_group p := { add := (+), zero := 0, neg := has_neg.neg, ..p.to_add_subgroup.to_add_comm_group } end add_comm_group section is_domain variables [ring R] [is_domain R] variables [add_comm_group M] [module R M] {b : ι → M} lemma not_mem_of_ortho {x : M} {N : submodule R M} (ortho : ∀ (c : R) (y ∈ N), c • x + y = (0 : M) → c = 0) : x ∉ N := by { intro hx, simpa using ortho (-1) x hx } lemma ne_zero_of_ortho {x : M} {N : submodule R M} (ortho : ∀ (c : R) (y ∈ N), c • x + y = (0 : M) → c = 0) : x ≠ 0 := mt (λ h, show x ∈ N, from h.symm ▸ N.zero_mem) (not_mem_of_ortho ortho) end is_domain section ordered_monoid variables [semiring R] /-- A submodule of an `ordered_add_comm_monoid` is an `ordered_add_comm_monoid`. -/ instance to_ordered_add_comm_monoid {M} [ordered_add_comm_monoid M] [module R M] (S : submodule R M) : ordered_add_comm_monoid S := subtype.coe_injective.ordered_add_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submodule of a `linear_ordered_add_comm_monoid` is a `linear_ordered_add_comm_monoid`. -/ instance to_linear_ordered_add_comm_monoid {M} [linear_ordered_add_comm_monoid M] [module R M] (S : submodule R M) : linear_ordered_add_comm_monoid S := subtype.coe_injective.linear_ordered_add_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A submodule of an `ordered_cancel_add_comm_monoid` is an `ordered_cancel_add_comm_monoid`. -/ instance to_ordered_cancel_add_comm_monoid {M} [ordered_cancel_add_comm_monoid M] [module R M] (S : submodule R M) : ordered_cancel_add_comm_monoid S := subtype.coe_injective.ordered_cancel_add_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) /-- A submodule of a `linear_ordered_cancel_add_comm_monoid` is a `linear_ordered_cancel_add_comm_monoid`. -/ instance to_linear_ordered_cancel_add_comm_monoid {M} [linear_ordered_cancel_add_comm_monoid M] [module R M] (S : submodule R M) : linear_ordered_cancel_add_comm_monoid S := subtype.coe_injective.linear_ordered_cancel_add_comm_monoid coe rfl (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) end ordered_monoid section ordered_group variables [ring R] /-- A submodule of an `ordered_add_comm_group` is an `ordered_add_comm_group`. -/ instance to_ordered_add_comm_group {M} [ordered_add_comm_group M] [module R M] (S : submodule R M) : ordered_add_comm_group S := subtype.coe_injective.ordered_add_comm_group coe rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A submodule of a `linear_ordered_add_comm_group` is a `linear_ordered_add_comm_group`. -/ instance to_linear_ordered_add_comm_group {M} [linear_ordered_add_comm_group M] [module R M] (S : submodule R M) : linear_ordered_add_comm_group S := subtype.coe_injective.linear_ordered_add_comm_group coe rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) end ordered_group end submodule namespace submodule variables [division_ring S] [semiring R] [add_comm_monoid M] [module R M] variables [has_smul S R] [module S M] [is_scalar_tower S R M] variables (p : submodule R M) {s : S} {x y : M} theorem smul_mem_iff (s0 : s ≠ 0) : s • x ∈ p ↔ x ∈ p := p.to_sub_mul_action.smul_mem_iff s0 end submodule /-- Subspace of a vector space. Defined to equal `submodule`. -/ abbreviation subspace (R : Type u) (M : Type v) [division_ring R] [add_comm_group M] [module R M] := submodule R M
e1c6353ab23f4d9ed61055c826dfafa7adb11caf
32317185abf7e7c963f4c67c190aec61af6b3628
/library/data/nat/bigops.lean
6784624bf6cbc1d30f0e93ae63a4ca957f736747
[ "Apache-2.0" ]
permissive
Andrew-Zipperer-unorganized/lean
198a2317f21198cd8d26e7085e484b86277f17f7
dcb35008e1474a0abebe632b1dced120e5f8c009
refs/heads/master
1,622,526,520,945
1,453,576,559,000
1,454,612,842,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,407
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Finite sums and products over intervals of natural numbers. -/ import data.nat.order algebra.group_bigops algebra.interval namespace nat /- sums -/ section add_monoid variables {A : Type} [add_monoid A] definition sum_up_to (n : ℕ) (f : ℕ → A) : A := nat.rec_on n 0 (λ n a, a + f n) notation `∑` binders ` < ` n `, ` r:(scoped f, sum_up_to n f) := r proposition sum_up_to_zero (f : ℕ → A) : (∑ i < 0, f i) = 0 := rfl proposition sum_up_to_succ (n : ℕ) (f : ℕ → A) : (∑ i < succ n, f i) = (∑ i < n, f i) + f n := rfl proposition sum_up_to_one (f : ℕ → A) : (∑ i < 1, f i) = f 0 := zero_add (f 0) definition sum_range (m n : ℕ) (f : ℕ → A) : A := sum_up_to (succ n - m) (λ i, f (i + m)) notation `∑` binders `=` m `...` n `, ` r:(scoped f, sum_range m n f) := r proposition sum_range_def (m n : ℕ) (f : ℕ → A) : (∑ i = m...n, f i) = (∑ i < (succ n - m), f (i + m)) := rfl proposition sum_range_self (m : ℕ) (f : ℕ → A) : (∑ i = m...m, f i) = f m := by rewrite [↑sum_range, succ_sub !le.refl, nat.sub_self, sum_up_to_one, zero_add] proposition sum_range_succ {m n : ℕ} (f : ℕ → A) (H : m ≤ succ n) : (∑ i = m...succ n, f i) = (∑ i = m...n, f i) + f (succ n) := by rewrite [↑sum_range, succ_sub H, sum_up_to_succ, nat.sub_add_cancel H] proposition sum_up_to_succ_eq_sum_range_zero (n : ℕ) (f : ℕ → A) : (∑ i < succ n, f i) = (∑ i = 0...n, f i) := rfl end add_monoid section finset variables {A : Type} [add_comm_monoid A] open finset proposition sum_up_to_eq_Sum_upto (n : ℕ) (f : ℕ → A) : (∑ i < n, f i) = (∑ i ∈ upto n, f i) := begin induction n with n ih, {exact rfl}, have H : upto n ∩ '{n} = ∅, from inter_eq_empty (take x, suppose x ∈ upto n, have x < n, from lt_of_mem_upto this, suppose x ∈ '{n}, have x = n, using this, by rewrite -mem_singleton_iff; apply this, have n < n, from eq.subst this `x < n`, show false, from !lt.irrefl this), rewrite [sum_up_to_succ, ih, upto_succ, Sum_union _ H, Sum_singleton] end end finset section set variables {A : Type} [add_comm_monoid A] open set interval proposition sum_range_eq_sum_interval_aux (m n : ℕ) (f : ℕ → A) : (∑ i = m...m+n, f i) = (∑ i ∈ '[m, m + n], f i) := begin induction n with n ih, {rewrite [nat.add_zero, sum_range_self, Icc_self, Sum_singleton]}, have H : m ≤ succ (m + n), from le_of_lt (lt_of_le_of_lt !le_add_right !lt_succ_self), have H' : '[m, m + n] ∩ '{succ (m + n)} = ∅, from eq_empty_of_forall_not_mem (take x, assume H1, have x = succ (m + n), from eq_of_mem_singleton (and.right H1), have succ (m + n) ≤ m + n, from eq.subst this (and.right (and.left H1)), show false, from not_lt_of_ge this !lt_succ_self), rewrite [add_succ, sum_range_succ f H, Icc_eq_Icc_union_Ioc !le_add_right !le_succ, nat.Ioc_eq_Icc_succ, Icc_self, Sum_union f H', Sum_singleton, ih] end proposition sum_range_eq_sum_interval {m n : ℕ} (f : ℕ → A) (H : m ≤ n) : (∑ i = m...n, f i) = (∑ i ∈ '[m, n], f i) := have n = m + (n - m), by rewrite [add.comm, nat.sub_add_cancel H], using this, by rewrite this; apply sum_range_eq_sum_interval_aux proposition sum_range_offset (m n : ℕ) (f : ℕ → A) : (∑ i = m...m+n, f i) = (∑ i = 0...n, f (m + i)) := have bij_on (add m) ('[0, n]) ('[m, m+n]), from !nat.bij_on_add_Icc_zero, by+ rewrite [-zero_add n at {2}, *sum_range_eq_sum_interval_aux, Sum_eq_of_bij_on f this, zero_add] end set /- products -/ section monoid variables {A : Type} [monoid A] definition prod_up_to (n : ℕ) (f : ℕ → A) : A := nat.rec_on n 1 (λ n a, a * f n) notation `∏` binders ` < ` n `, ` r:(scoped f, prod_up_to n f) := r proposition prod_up_to_zero (f : ℕ → A) : (∏ i < 0, f i) = 1 := rfl proposition prod_up_to_succ (n : ℕ) (f : ℕ → A) : (∏ i < succ n, f i) = (∏ i < n, f i) * f n := rfl proposition prod_up_to_one (f : ℕ → A) : (∏ i < 1, f i) = f 0 := one_mul (f 0) definition prod_range (m n : ℕ) (f : ℕ → A) : A := prod_up_to (succ n - m) (λ i, f (i + m)) notation `∏` binders `=` m `...` n `, ` r:(scoped f, prod_range m n f) := r proposition prod_range_def (m n : ℕ) (f : ℕ → A) : (∏ i = m...n, f i) = (∏ i < (succ n - m), f (i + m)) := rfl proposition prod_range_self (m : ℕ) (f : ℕ → A) : (∏ i = m...m, f i) = f m := by rewrite [↑prod_range, succ_sub !le.refl, nat.sub_self, prod_up_to_one, zero_add] proposition prod_range_succ {m n : ℕ} (f : ℕ → A) (H : m ≤ succ n) : (∏ i = m...succ n, f i) = (∏ i = m...n, f i) * f (succ n) := by rewrite [↑prod_range, succ_sub H, prod_up_to_succ, nat.sub_add_cancel H] proposition prod_up_to_succ_eq_prod_range_zero (n : ℕ) (f : ℕ → A) : (∏ i < succ n, f i) = (∏ i = 0...n, f i) := rfl end monoid section finset variables {A : Type} [comm_monoid A] open finset proposition prod_up_to_eq_Prod_upto (n : ℕ) (f : ℕ → A) : (∏ i < n, f i) = (∏ i ∈ upto n, f i) := begin induction n with n ih, {exact rfl}, have H : upto n ∩ '{n} = ∅, from inter_eq_empty (take x, suppose x ∈ upto n, have x < n, from lt_of_mem_upto this, suppose x ∈ '{n}, have x = n, using this, by rewrite -mem_singleton_iff; apply this, have n < n, from eq.subst this `x < n`, show false, from !lt.irrefl this), rewrite [prod_up_to_succ, ih, upto_succ, Prod_union _ H, Prod_singleton] end end finset section set variables {A : Type} [comm_monoid A] open set interval proposition prod_range_eq_prod_interval_aux (m n : ℕ) (f : ℕ → A) : (∏ i = m...m+n, f i) = (∏ i ∈ '[m, m + n], f i) := begin induction n with n ih, {rewrite [nat.add_zero, prod_range_self, Icc_self, Prod_singleton]}, have H : m ≤ succ (m + n), from le_of_lt (lt_of_le_of_lt !le_add_right !lt_succ_self), have H' : '[m, m + n] ∩ '{succ (m + n)} = ∅, from eq_empty_of_forall_not_mem (take x, assume H1, have x = succ (m + n), from eq_of_mem_singleton (and.right H1), have succ (m + n) ≤ m + n, from eq.subst this (and.right (and.left H1)), show false, from not_lt_of_ge this !lt_succ_self), rewrite [add_succ, prod_range_succ f H, Icc_eq_Icc_union_Ioc !le_add_right !le_succ, nat.Ioc_eq_Icc_succ, Icc_self, Prod_union f H', Prod_singleton, ih] end proposition prod_range_eq_prod_interval {m n : ℕ} (f : ℕ → A) (H : m ≤ n) : (∏ i = m...n, f i) = (∏ i ∈ '[m, n], f i) := have n = m + (n - m), by rewrite [add.comm, nat.sub_add_cancel H], using this, by rewrite this; apply prod_range_eq_prod_interval_aux proposition prod_range_offset (m n : ℕ) (f : ℕ → A) : (∏ i = m...m+n, f i) = (∏ i = 0...n, f (m + i)) := have bij_on (add m) ('[0, n]) ('[m, m+n]), from !nat.bij_on_add_Icc_zero, by+ rewrite [-zero_add n at {2}, *prod_range_eq_prod_interval_aux, Prod_eq_of_bij_on f this, zero_add] end set end nat
86d4f5a3a276adf6e90793952ad790d09f5640f2
b70447c014d9e71cf619ebc9f539b262c19c2e0b
/hott/types/eq.hlean
1519ec50b5d87c44b7890b63de69dd0b918e10f5
[ "Apache-2.0" ]
permissive
ia0/lean2
c20d8da69657f94b1d161f9590a4c635f8dc87f3
d86284da630acb78fa5dc3b0b106153c50ffccd0
refs/heads/master
1,611,399,322,751
1,495,751,007,000
1,495,751,007,000
93,104,167
0
0
null
1,496,355,488,000
1,496,355,487,000
null
UTF-8
Lean
false
false
21,849
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Partially ported from Coq HoTT Theorems about path types (identity types) -/ import types.sigma open eq sigma sigma.ops equiv is_equiv is_trunc namespace eq /- Path spaces -/ section variables {A B : Type} {a a₁ a₂ a₃ a₄ a' : A} {b b1 b2 : B} {f g : A → B} {h : B → A} {p p' p'' : a₁ = a₂} /- The path spaces of a path space are not, of course, determined; they are just the higher-dimensional structure of the original space. -/ /- some lemmas about whiskering or other higher paths -/ definition whisker_left_con_right (p : a₁ = a₂) {q q' q'' : a₂ = a₃} (r : q = q') (s : q' = q'') : whisker_left p (r ⬝ s) = whisker_left p r ⬝ whisker_left p s := begin induction p, induction r, induction s, reflexivity end definition whisker_right_con_right (q : a₂ = a₃) (r : p = p') (s : p' = p'') : whisker_right q (r ⬝ s) = whisker_right q r ⬝ whisker_right q s := begin induction q, induction r, induction s, reflexivity end definition whisker_left_con_left (p : a₁ = a₂) (p' : a₂ = a₃) {q q' : a₃ = a₄} (r : q = q') : whisker_left (p ⬝ p') r = !con.assoc ⬝ whisker_left p (whisker_left p' r) ⬝ !con.assoc' := begin induction p', induction p, induction r, induction q, reflexivity end definition whisker_right_con_left {p p' : a₁ = a₂} (q : a₂ = a₃) (q' : a₃ = a₄) (r : p = p') : whisker_right (q ⬝ q') r = !con.assoc' ⬝ whisker_right q' (whisker_right q r) ⬝ !con.assoc := begin induction q', induction q, induction r, induction p, reflexivity end definition whisker_left_inv_left (p : a₂ = a₁) {q q' : a₂ = a₃} (r : q = q') : !con_inv_cancel_left⁻¹ ⬝ whisker_left p (whisker_left p⁻¹ r) ⬝ !con_inv_cancel_left = r := begin induction p, induction r, induction q, reflexivity end definition whisker_left_inv (p : a₁ = a₂) {q q' : a₂ = a₃} (r : q = q') : whisker_left p r⁻¹ = (whisker_left p r)⁻¹ := by induction r; reflexivity definition whisker_right_inv {p p' : a₁ = a₂} (q : a₂ = a₃) (r : p = p') : whisker_right q r⁻¹ = (whisker_right q r)⁻¹ := by induction r; reflexivity definition ap_eq_apd10 [unfold 5] {B : A → Type} {f g : Πa, B a} (p : f = g) (a : A) : ap (λh, h a) p = apd10 p a := by induction p; reflexivity definition inverse2_right_inv (r : p = p') : r ◾ inverse2 r ⬝ con.right_inv p' = con.right_inv p := by induction r;induction p;reflexivity definition inverse2_left_inv (r : p = p') : inverse2 r ◾ r ⬝ con.left_inv p' = con.left_inv p := by induction r;induction p;reflexivity definition ap_con_right_inv (f : A → B) (p : a₁ = a₂) : ap_con f p p⁻¹ ⬝ whisker_left _ (ap_inv f p) ⬝ con.right_inv (ap f p) = ap (ap f) (con.right_inv p) := by induction p;reflexivity definition ap_con_left_inv (f : A → B) (p : a₁ = a₂) : ap_con f p⁻¹ p ⬝ whisker_right _ (ap_inv f p) ⬝ con.left_inv (ap f p) = ap (ap f) (con.left_inv p) := by induction p;reflexivity definition idp_con_whisker_left {q q' : a₂ = a₃} (r : q = q') : !idp_con⁻¹ ⬝ whisker_left idp r = r ⬝ !idp_con⁻¹ := by induction r;induction q;reflexivity definition whisker_left_idp_con {q q' : a₂ = a₃} (r : q = q') : whisker_left idp r ⬝ !idp_con = !idp_con ⬝ r := by induction r;induction q;reflexivity definition idp_con_idp {p : a = a} (q : p = idp) : idp_con p ⬝ q = ap (λp, idp ⬝ p) q := by cases q;reflexivity definition ap_is_constant [unfold 8] {A B : Type} {f : A → B} {b : B} (p : Πx, f x = b) {x y : A} (q : x = y) : ap f q = p x ⬝ (p y)⁻¹ := by induction q;exact !con.right_inv⁻¹ definition inv2_inv {p q : a = a'} (r : p = q) : inverse2 r⁻¹ = (inverse2 r)⁻¹ := by induction r;reflexivity definition inv2_con {p p' p'' : a = a'} (r : p = p') (r' : p' = p'') : inverse2 (r ⬝ r') = inverse2 r ⬝ inverse2 r' := by induction r';induction r;reflexivity definition con2_inv {p₁ q₁ : a₁ = a₂} {p₂ q₂ : a₂ = a₃} (r₁ : p₁ = q₁) (r₂ : p₂ = q₂) : (r₁ ◾ r₂)⁻¹ = r₁⁻¹ ◾ r₂⁻¹ := by induction r₁;induction r₂;reflexivity definition eq_con_inv_of_con_eq_whisker_left {A : Type} {a a₂ a₃ : A} {p : a = a₂} {q q' : a₂ = a₃} {r : a = a₃} (s' : q = q') (s : p ⬝ q' = r) : eq_con_inv_of_con_eq (whisker_left p s' ⬝ s) = eq_con_inv_of_con_eq s ⬝ whisker_left r (inverse2 s')⁻¹ := by induction s';induction q;induction s;reflexivity definition right_inv_eq_idp {A : Type} {a : A} {p : a = a} (r : p = idpath a) : con.right_inv p = r ◾ inverse2 r := by cases r;reflexivity /- Transporting in path spaces. There are potentially a lot of these lemmas, so we adopt a uniform naming scheme: - `l` means the left endpoint varies - `r` means the right endpoint varies - `F` means application of a function to that (varying) endpoint. -/ definition eq_transport_l (p : a₁ = a₂) (q : a₁ = a₃) : transport (λx, x = a₃) p q = p⁻¹ ⬝ q := by induction p; induction q; reflexivity definition eq_transport_r (p : a₂ = a₃) (q : a₁ = a₂) : transport (λx, a₁ = x) p q = q ⬝ p := by induction p; induction q; reflexivity definition eq_transport_lr (p : a₁ = a₂) (q : a₁ = a₁) : transport (λx, x = x) p q = p⁻¹ ⬝ q ⬝ p := by induction p; rewrite [▸*,idp_con] definition eq_transport_Fl (p : a₁ = a₂) (q : f a₁ = b) : transport (λx, f x = b) p q = (ap f p)⁻¹ ⬝ q := by induction p; induction q; reflexivity definition eq_transport_Fr (p : a₁ = a₂) (q : b = f a₁) : transport (λx, b = f x) p q = q ⬝ (ap f p) := by induction p; reflexivity definition eq_transport_FlFr (p : a₁ = a₂) (q : f a₁ = g a₁) : transport (λx, f x = g x) p q = (ap f p)⁻¹ ⬝ q ⬝ (ap g p) := by induction p; rewrite [▸*,idp_con] definition eq_transport_FlFr_D {B : A → Type} {f g : Πa, B a} (p : a₁ = a₂) (q : f a₁ = g a₁) : transport (λx, f x = g x) p q = (apdt f p)⁻¹ ⬝ ap (transport B p) q ⬝ (apdt g p) := by induction p; rewrite [▸*,idp_con,ap_id] definition eq_transport_FFlr (p : a₁ = a₂) (q : h (f a₁) = a₁) : transport (λx, h (f x) = x) p q = (ap h (ap f p))⁻¹ ⬝ q ⬝ p := by induction p; rewrite [▸*,idp_con] definition eq_transport_lFFr (p : a₁ = a₂) (q : a₁ = h (f a₁)) : transport (λx, x = h (f x)) p q = p⁻¹ ⬝ q ⬝ (ap h (ap f p)) := by induction p; rewrite [▸*,idp_con] /- Pathovers -/ -- In the comment we give the fibration of the pathover -- we should probably try to do everything just with pathover_eq (defined in cubical.square), -- the following definitions may be removed in future. definition eq_pathover_l (p : a₁ = a₂) (q : a₁ = a₃) : q =[p] p⁻¹ ⬝ q := /-(λx, x = a₃)-/ by induction p; induction q; exact idpo definition eq_pathover_r (p : a₂ = a₃) (q : a₁ = a₂) : q =[p] q ⬝ p := /-(λx, a₁ = x)-/ by induction p; induction q; exact idpo definition eq_pathover_lr (p : a₁ = a₂) (q : a₁ = a₁) : q =[p] p⁻¹ ⬝ q ⬝ p := /-(λx, x = x)-/ by induction p; rewrite [▸*,idp_con]; exact idpo definition eq_pathover_Fl (p : a₁ = a₂) (q : f a₁ = b) : q =[p] (ap f p)⁻¹ ⬝ q := /-(λx, f x = b)-/ by induction p; induction q; exact idpo definition eq_pathover_Fl' (p : a₁ = a₂) (q : f a₂ = b) : (ap f p) ⬝ q =[p] q := /-(λx, f x = b)-/ by induction p; induction q; exact idpo definition eq_pathover_Fr (p : a₁ = a₂) (q : b = f a₁) : q =[p] q ⬝ (ap f p) := /-(λx, b = f x)-/ by induction p; exact idpo definition eq_pathover_FlFr (p : a₁ = a₂) (q : f a₁ = g a₁) : q =[p] (ap f p)⁻¹ ⬝ q ⬝ (ap g p) := /-(λx, f x = g x)-/ by induction p; rewrite [▸*,idp_con]; exact idpo definition eq_pathover_FlFr_D {B : A → Type} {f g : Πa, B a} (p : a₁ = a₂) (q : f a₁ = g a₁) : q =[p] (apdt f p)⁻¹ ⬝ ap (transport B p) q ⬝ (apdt g p) := /-(λx, f x = g x)-/ by induction p; rewrite [▸*,idp_con,ap_id];exact idpo definition eq_pathover_FFlr (p : a₁ = a₂) (q : h (f a₁) = a₁) : q =[p] (ap h (ap f p))⁻¹ ⬝ q ⬝ p := /-(λx, h (f x) = x)-/ by induction p; rewrite [▸*,idp_con];exact idpo definition eq_pathover_lFFr (p : a₁ = a₂) (q : a₁ = h (f a₁)) : q =[p] p⁻¹ ⬝ q ⬝ (ap h (ap f p)) := /-(λx, x = h (f x))-/ by induction p; rewrite [▸*,idp_con];exact idpo definition eq_pathover_r_idp (p : a₁ = a₂) : idp =[p] p := /-(λx, a₁ = x)-/ by induction p; exact idpo definition eq_pathover_l_idp (p : a₁ = a₂) : idp =[p] p⁻¹ := /-(λx, x = a₁)-/ by induction p; exact idpo definition eq_pathover_l_idp' (p : a₁ = a₂) : idp =[p⁻¹] p := /-(λx, x = a₂)-/ by induction p; exact idpo -- The Functorial action of paths is [ap]. /- Equivalences between path spaces -/ /- [is_equiv_ap] is in init.equiv -/ definition equiv_ap [constructor] (f : A → B) [H : is_equiv f] (a₁ a₂ : A) : (a₁ = a₂) ≃ (f a₁ = f a₂) := equiv.mk (ap f) _ /- Path operations are equivalences -/ definition is_equiv_eq_inverse [constructor] (a₁ a₂ : A) : is_equiv (inverse : a₁ = a₂ → a₂ = a₁) := is_equiv.mk inverse inverse inv_inv inv_inv (λp, eq.rec_on p idp) local attribute is_equiv_eq_inverse [instance] definition eq_equiv_eq_symm [constructor] (a₁ a₂ : A) : (a₁ = a₂) ≃ (a₂ = a₁) := equiv.mk inverse _ definition is_equiv_concat_left [constructor] [instance] (p : a₁ = a₂) (a₃ : A) : is_equiv (concat p : a₂ = a₃ → a₁ = a₃) := is_equiv.mk (concat p) (concat p⁻¹) (con_inv_cancel_left p) (inv_con_cancel_left p) abstract (λq, by induction p;induction q;reflexivity) end local attribute is_equiv_concat_left [instance] definition equiv_eq_closed_left [constructor] (a₃ : A) (p : a₁ = a₂) : (a₁ = a₃) ≃ (a₂ = a₃) := equiv.mk (concat p⁻¹) _ definition is_equiv_concat_right [constructor] [instance] (p : a₂ = a₃) (a₁ : A) : is_equiv (λq : a₁ = a₂, q ⬝ p) := is_equiv.mk (λq, q ⬝ p) (λq, q ⬝ p⁻¹) (λq, inv_con_cancel_right q p) (λq, con_inv_cancel_right q p) (λq, by induction p;induction q;reflexivity) local attribute is_equiv_concat_right [instance] definition equiv_eq_closed_right [constructor] (a₁ : A) (p : a₂ = a₃) : (a₁ = a₂) ≃ (a₁ = a₃) := equiv.mk (λq, q ⬝ p) _ definition eq_equiv_eq_closed [constructor] (p : a₁ = a₂) (q : a₃ = a₄) : (a₁ = a₃) ≃ (a₂ = a₄) := equiv.trans (equiv_eq_closed_left a₃ p) (equiv_eq_closed_right a₂ q) definition loop_equiv_eq_closed [constructor] {A : Type} {a a' : A} (p : a = a') : (a = a) ≃ (a' = a') := eq_equiv_eq_closed p p definition is_equiv_whisker_left [constructor] (p : a₁ = a₂) (q r : a₂ = a₃) : is_equiv (whisker_left p : q = r → p ⬝ q = p ⬝ r) := begin fapply adjointify, {intro s, apply (!cancel_left s)}, {intro s, apply concat, {apply whisker_left_con_right}, apply concat, rotate_left 1, apply (whisker_left_inv_left p s), apply concat2, {apply concat, {apply whisker_left_con_right}, apply concat2, {induction p, induction q, reflexivity}, {reflexivity}}, {induction p, induction r, reflexivity}}, {intro s, induction s, induction q, induction p, reflexivity} end definition eq_equiv_con_eq_con_left [constructor] (p : a₁ = a₂) (q r : a₂ = a₃) : (q = r) ≃ (p ⬝ q = p ⬝ r) := equiv.mk _ !is_equiv_whisker_left definition is_equiv_whisker_right [constructor] {p q : a₁ = a₂} (r : a₂ = a₃) : is_equiv (λs, whisker_right r s : p = q → p ⬝ r = q ⬝ r) := begin fapply adjointify, {intro s, apply (!cancel_right s)}, {intro s, induction r, cases s, induction q, reflexivity}, {intro s, induction s, induction r, induction p, reflexivity} end definition eq_equiv_con_eq_con_right [constructor] (p q : a₁ = a₂) (r : a₂ = a₃) : (p = q) ≃ (p ⬝ r = q ⬝ r) := equiv.mk _ !is_equiv_whisker_right /- The following proofs can be simplified a bit by concatenating previous equivalences. However, these proofs have the advantage that the inverse is definitionally equal to what we would expect -/ definition is_equiv_con_eq_of_eq_inv_con [constructor] (p : a₁ = a₃) (q : a₂ = a₃) (r : a₂ = a₁) : is_equiv (con_eq_of_eq_inv_con : p = r⁻¹ ⬝ q → r ⬝ p = q) := begin fapply adjointify, { apply eq_inv_con_of_con_eq}, { intro s, induction r, rewrite [↑[con_eq_of_eq_inv_con,eq_inv_con_of_con_eq], con.assoc,con.assoc,con.left_inv,▸*,-con.assoc,con.right_inv,▸* at *,idp_con s]}, { intro s, induction r, rewrite [↑[con_eq_of_eq_inv_con,eq_inv_con_of_con_eq], con.assoc,con.assoc,con.right_inv,▸*,-con.assoc,con.left_inv,▸* at *,idp_con s] }, end definition eq_inv_con_equiv_con_eq [constructor] (p : a₁ = a₃) (q : a₂ = a₃) (r : a₂ = a₁) : (p = r⁻¹ ⬝ q) ≃ (r ⬝ p = q) := equiv.mk _ !is_equiv_con_eq_of_eq_inv_con definition is_equiv_con_eq_of_eq_con_inv [constructor] (p : a₁ = a₃) (q : a₂ = a₃) (r : a₂ = a₁) : is_equiv (con_eq_of_eq_con_inv : r = q ⬝ p⁻¹ → r ⬝ p = q) := begin fapply adjointify, { apply eq_con_inv_of_con_eq}, { intro s, induction p, rewrite [↑[con_eq_of_eq_con_inv,eq_con_inv_of_con_eq]]}, { intro s, induction p, rewrite [↑[con_eq_of_eq_con_inv,eq_con_inv_of_con_eq]] }, end definition eq_con_inv_equiv_con_eq [constructor] (p : a₁ = a₃) (q : a₂ = a₃) (r : a₂ = a₁) : (r = q ⬝ p⁻¹) ≃ (r ⬝ p = q) := equiv.mk _ !is_equiv_con_eq_of_eq_con_inv definition is_equiv_inv_con_eq_of_eq_con [constructor] (p : a₁ = a₃) (q : a₂ = a₃) (r : a₁ = a₂) : is_equiv (inv_con_eq_of_eq_con : p = r ⬝ q → r⁻¹ ⬝ p = q) := begin fapply adjointify, { apply eq_con_of_inv_con_eq}, { intro s, induction r, rewrite [↑[inv_con_eq_of_eq_con,eq_con_of_inv_con_eq], con.assoc,con.assoc,con.left_inv,▸*,-con.assoc,con.right_inv,▸* at *,idp_con s]}, { intro s, induction r, rewrite [↑[inv_con_eq_of_eq_con,eq_con_of_inv_con_eq], con.assoc,con.assoc,con.right_inv,▸*,-con.assoc,con.left_inv,▸* at *,idp_con s] }, end definition eq_con_equiv_inv_con_eq [constructor] (p : a₁ = a₃) (q : a₂ = a₃) (r : a₁ = a₂) : (p = r ⬝ q) ≃ (r⁻¹ ⬝ p = q) := equiv.mk _ !is_equiv_inv_con_eq_of_eq_con definition is_equiv_con_inv_eq_of_eq_con [constructor] (p : a₃ = a₁) (q : a₂ = a₃) (r : a₂ = a₁) : is_equiv (con_inv_eq_of_eq_con : r = q ⬝ p → r ⬝ p⁻¹ = q) := begin fapply adjointify, { apply eq_con_of_con_inv_eq}, { intro s, induction p, rewrite [↑[con_inv_eq_of_eq_con,eq_con_of_con_inv_eq]]}, { intro s, induction p, rewrite [↑[con_inv_eq_of_eq_con,eq_con_of_con_inv_eq]] }, end definition eq_con_equiv_con_inv_eq (p : a₃ = a₁) (q : a₂ = a₃) (r : a₂ = a₁) : (r = q ⬝ p) ≃ (r ⬝ p⁻¹ = q) := equiv.mk _ !is_equiv_con_inv_eq_of_eq_con local attribute is_equiv_inv_con_eq_of_eq_con is_equiv_con_inv_eq_of_eq_con is_equiv_con_eq_of_eq_con_inv is_equiv_con_eq_of_eq_inv_con [instance] definition is_equiv_eq_con_of_inv_con_eq (p : a₁ = a₃) (q : a₂ = a₃) (r : a₂ = a₁) : is_equiv (eq_con_of_inv_con_eq : r⁻¹ ⬝ q = p → q = r ⬝ p) := is_equiv_inv inv_con_eq_of_eq_con definition is_equiv_eq_con_of_con_inv_eq (p : a₁ = a₃) (q : a₂ = a₃) (r : a₂ = a₁) : is_equiv (eq_con_of_con_inv_eq : q ⬝ p⁻¹ = r → q = r ⬝ p) := is_equiv_inv con_inv_eq_of_eq_con definition is_equiv_eq_con_inv_of_con_eq (p : a₁ = a₃) (q : a₂ = a₃) (r : a₂ = a₁) : is_equiv (eq_con_inv_of_con_eq : r ⬝ p = q → r = q ⬝ p⁻¹) := is_equiv_inv con_eq_of_eq_con_inv definition is_equiv_eq_inv_con_of_con_eq (p : a₁ = a₃) (q : a₂ = a₃) (r : a₂ = a₁) : is_equiv (eq_inv_con_of_con_eq : r ⬝ p = q → p = r⁻¹ ⬝ q) := is_equiv_inv con_eq_of_eq_inv_con definition is_equiv_con_inv_eq_idp [constructor] (p q : a₁ = a₂) : is_equiv (con_inv_eq_idp : p = q → p ⬝ q⁻¹ = idp) := begin fapply adjointify, { apply eq_of_con_inv_eq_idp}, { intro s, induction q, esimp at *, cases s, reflexivity}, { intro s, induction s, induction p, reflexivity}, end definition is_equiv_inv_con_eq_idp [constructor] (p q : a₁ = a₂) : is_equiv (inv_con_eq_idp : p = q → q⁻¹ ⬝ p = idp) := begin fapply adjointify, { apply eq_of_inv_con_eq_idp}, { intro s, induction q, esimp [eq_of_inv_con_eq_idp] at *, eapply is_equiv_rect (eq_equiv_con_eq_con_left idp p idp), clear s, intro s, cases s, reflexivity}, { intro s, induction s, induction p, reflexivity}, end definition eq_equiv_con_inv_eq_idp [constructor] (p q : a₁ = a₂) : (p = q) ≃ (p ⬝ q⁻¹ = idp) := equiv.mk _ !is_equiv_con_inv_eq_idp definition eq_equiv_inv_con_eq_idp [constructor] (p q : a₁ = a₂) : (p = q) ≃ (q⁻¹ ⬝ p = idp) := equiv.mk _ !is_equiv_inv_con_eq_idp /- Pathover Equivalences -/ definition eq_pathover_equiv_l (p : a₁ = a₂) (q : a₁ = a₃) (r : a₂ = a₃) : q =[p] r ≃ q = p ⬝ r := /-(λx, x = a₃)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition eq_pathover_equiv_r (p : a₂ = a₃) (q : a₁ = a₂) (r : a₁ = a₃) : q =[p] r ≃ q ⬝ p = r := /-(λx, a₁ = x)-/ by induction p; apply pathover_idp definition eq_pathover_equiv_lr (p : a₁ = a₂) (q : a₁ = a₁) (r : a₂ = a₂) : q =[p] r ≃ q ⬝ p = p ⬝ r := /-(λx, x = x)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition eq_pathover_equiv_Fl (p : a₁ = a₂) (q : f a₁ = b) (r : f a₂ = b) : q =[p] r ≃ q = ap f p ⬝ r := /-(λx, f x = b)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition eq_pathover_equiv_Fr (p : a₁ = a₂) (q : b = f a₁) (r : b = f a₂) : q =[p] r ≃ q ⬝ ap f p = r := /-(λx, b = f x)-/ by induction p; apply pathover_idp definition eq_pathover_equiv_FlFr (p : a₁ = a₂) (q : f a₁ = g a₁) (r : f a₂ = g a₂) : q =[p] r ≃ q ⬝ ap g p = ap f p ⬝ r := /-(λx, f x = g x)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition eq_pathover_equiv_FFlr (p : a₁ = a₂) (q : h (f a₁) = a₁) (r : h (f a₂) = a₂) : q =[p] r ≃ q ⬝ p = ap h (ap f p) ⬝ r := /-(λx, h (f x) = x)-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ definition eq_pathover_equiv_lFFr (p : a₁ = a₂) (q : a₁ = h (f a₁)) (r : a₂ = h (f a₂)) : q =[p] r ≃ q ⬝ ap h (ap f p) = p ⬝ r := /-(λx, x = h (f x))-/ by induction p; exact !pathover_idp ⬝e !equiv_eq_closed_right !idp_con⁻¹ -- a lot of this library still needs to be ported from Coq HoTT -- the behavior of equality in other types is described in the corresponding type files -- encode decode method open is_trunc definition encode_decode_method' (a₀ a : A) (code : A → Type) (c₀ : code a₀) (decode : Π(a : A) (c : code a), a₀ = a) (encode_decode : Π(a : A) (c : code a), c₀ =[decode a c] c) (decode_encode : decode a₀ c₀ = idp) : (a₀ = a) ≃ code a := begin fapply equiv.MK, { intro p, exact p ▸ c₀}, { apply decode}, { intro c, apply tr_eq_of_pathover, apply encode_decode}, { intro p, induction p, apply decode_encode}, end end section parameters {A : Type} (a₀ : A) (code : A → Type) (H : is_contr (Σa, code a)) (p : (center (Σa, code a)).1 = a₀) include p protected definition encode {a : A} (q : a₀ = a) : code a := (p ⬝ q) ▸ (center (Σa, code a)).2 protected definition decode' {a : A} (c : code a) : a₀ = a := (is_prop.elim ⟨a₀, encode idp⟩ ⟨a, c⟩)..1 protected definition decode {a : A} (c : code a) : a₀ = a := (decode' (encode idp))⁻¹ ⬝ decode' c definition total_space_method (a : A) : (a₀ = a) ≃ code a := begin fapply equiv.MK, { exact encode}, { exact decode}, { intro c, unfold [encode, decode, decode'], induction p, esimp, rewrite [is_prop_elim_self,▸*,+idp_con], apply tr_eq_of_pathover, eapply @sigma.rec_on _ _ (λx, x.2 =[(is_prop.elim ⟨x.1, x.2⟩ ⟨a, c⟩)..1] c) (center (sigma code)), intro a c, apply eq_pr2}, { intro q, induction q, esimp, apply con.left_inv, }, end end definition encode_decode_method {A : Type} (a₀ a : A) (code : A → Type) (c₀ : code a₀) (decode : Π(a : A) (c : code a), a₀ = a) (encode_decode : Π(a : A) (c : code a), c₀ =[decode a c] c) : (a₀ = a) ≃ code a := begin fapply total_space_method, { fapply @is_contr.mk, { exact ⟨a₀, c₀⟩}, { intro p, fapply sigma_eq, apply decode, exact p.2, apply encode_decode}}, { reflexivity} end end eq
e2ee5827ab26527ed3d6d4fbb0a8439a6e82f99a
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/algebra/lie/solvable.lean
c86d78d44197e0024c485fd45396d4b198ca43ad
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
14,024
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.ideal_operations import algebra.lie.abelian import order.preorder_hom /-! # Solvable Lie algebras Like groups, Lie algebras admit a natural concept of solvability. We define this here via the derived series and prove some related results. We also define the radical of a Lie algebra and prove that it is solvable when the Lie algebra is Noetherian. ## Main definitions * `lie_algebra.derived_series_of_ideal` * `lie_algebra.derived_series` * `lie_algebra.is_solvable` * `lie_algebra.is_solvable_add` * `lie_algebra.radical` * `lie_algebra.radical_is_solvable` * `lie_algebra.derived_length_of_ideal` * `lie_algebra.derived_length` * `lie_algebra.derived_abelian_of_ideal` ## Tags lie algebra, derived series, derived length, solvable, radical -/ universes u v w w₁ w₂ variables (R : Type u) (L : Type v) (M : Type w) {L' : Type w₁} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] variables (I J : lie_ideal R L) {f : L' →ₗ⁅R⁆ L} namespace lie_algebra /-- A generalisation of the derived series of a Lie algebra, whose zeroth term is a specified ideal. It can be more convenient to work with this generalisation when considering the derived series of an ideal since it provides a type-theoretic expression of the fact that the terms of the ideal's derived series are also ideals of the enclosing algebra. See also `lie_ideal.derived_series_eq_derived_series_of_ideal_comap` and `lie_ideal.derived_series_eq_derived_series_of_ideal_map` below. -/ def derived_series_of_ideal (k : ℕ) : lie_ideal R L → lie_ideal R L := (λ I, ⁅I, I⁆)^[k] @[simp] lemma derived_series_of_ideal_zero : derived_series_of_ideal R L 0 I = I := rfl @[simp] lemma derived_series_of_ideal_succ (k : ℕ) : derived_series_of_ideal R L (k + 1) I = ⁅derived_series_of_ideal R L k I, derived_series_of_ideal R L k I⁆ := function.iterate_succ_apply' (λ I, ⁅I, I⁆) k I /-- The derived series of Lie ideals of a Lie algebra. -/ abbreviation derived_series (k : ℕ) : lie_ideal R L := derived_series_of_ideal R L k ⊤ lemma derived_series_def (k : ℕ) : derived_series R L k = derived_series_of_ideal R L k ⊤ := rfl variables {R L} local notation `D` := derived_series_of_ideal R L lemma derived_series_of_ideal_add (k l : ℕ) : D (k + l) I = D k (D l I) := begin induction k with k ih, { rw [zero_add, derived_series_of_ideal_zero], }, { rw [nat.succ_add k l, derived_series_of_ideal_succ, derived_series_of_ideal_succ, ih], }, end lemma derived_series_of_ideal_le {I J : lie_ideal R L} {k l : ℕ} (h₁ : I ≤ J) (h₂ : l ≤ k) : D k I ≤ D l J := begin revert l, induction k with k ih; intros l h₂, { rw nat.le_zero_iff at h₂, rw [h₂, derived_series_of_ideal_zero], exact h₁, }, { have h : l = k.succ ∨ l ≤ k, by rwa [le_iff_eq_or_lt, nat.lt_succ_iff] at h₂, cases h, { rw [h, derived_series_of_ideal_succ, derived_series_of_ideal_succ], exact lie_submodule.mono_lie _ _ _ _ (ih (le_refl k)) (ih (le_refl k)), }, { rw derived_series_of_ideal_succ, exact le_trans (lie_submodule.lie_le_left _ _) (ih h), }, }, end lemma derived_series_of_ideal_succ_le (k : ℕ) : D (k + 1) I ≤ D k I := derived_series_of_ideal_le (le_refl I) k.le_succ lemma derived_series_of_ideal_le_self (k : ℕ) : D k I ≤ I := derived_series_of_ideal_le (le_refl I) (zero_le k) lemma derived_series_of_ideal_mono {I J : lie_ideal R L} (h : I ≤ J) (k : ℕ) : D k I ≤ D k J := derived_series_of_ideal_le h (le_refl k) lemma derived_series_of_ideal_antimono {k l : ℕ} (h : l ≤ k) : D k I ≤ D l I := derived_series_of_ideal_le (le_refl I) h lemma derived_series_of_ideal_add_le_add (J : lie_ideal R L) (k l : ℕ) : D (k + l) (I + J) ≤ (D k I) + (D l J) := begin let D₁ : lie_ideal R L →ₘ lie_ideal R L := { to_fun := λ I, ⁅I, I⁆, monotone' := λ I J h, lie_submodule.mono_lie I J I J h h, }, have h₁ : ∀ (I J : lie_ideal R L), D₁ (I ⊔ J) ≤ (D₁ I) ⊔ J, { simp [lie_submodule.lie_le_right, lie_submodule.lie_le_left, le_sup_right_of_le], }, rw ← D₁.iterate_sup_le_sup_iff at h₁, exact h₁ k l I J, end lemma derived_series_of_bot_eq_bot (k : ℕ) : derived_series_of_ideal R L k ⊥ = ⊥ := by { rw eq_bot_iff, exact derived_series_of_ideal_le_self ⊥ k, } lemma abelian_iff_derived_one_eq_bot : is_lie_abelian I ↔ derived_series_of_ideal R L 1 I = ⊥ := by rw [derived_series_of_ideal_succ, derived_series_of_ideal_zero, lie_submodule.lie_abelian_iff_lie_self_eq_bot] lemma abelian_iff_derived_succ_eq_bot (I : lie_ideal R L) (k : ℕ) : is_lie_abelian (derived_series_of_ideal R L k I) ↔ derived_series_of_ideal R L (k + 1) I = ⊥ := by rw [add_comm, derived_series_of_ideal_add I 1 k, abelian_iff_derived_one_eq_bot] end lie_algebra namespace lie_ideal open lie_algebra variables {R L} lemma derived_series_eq_derived_series_of_ideal_comap (k : ℕ) : derived_series R I k = (derived_series_of_ideal R L k I).comap I.incl := begin induction k with k ih, { simp only [derived_series_def, comap_incl_self, derived_series_of_ideal_zero], }, { simp only [derived_series_def, derived_series_of_ideal_succ] at ⊢ ih, rw ih, exact comap_bracket_incl_of_le I (derived_series_of_ideal_le_self I k) (derived_series_of_ideal_le_self I k), }, end lemma derived_series_eq_derived_series_of_ideal_map (k : ℕ) : (derived_series R I k).map I.incl = derived_series_of_ideal R L k I := by { rw [derived_series_eq_derived_series_of_ideal_comap, map_comap_incl, inf_eq_right], apply derived_series_of_ideal_le_self, } lemma derived_series_eq_bot_iff (k : ℕ) : derived_series R I k = ⊥ ↔ derived_series_of_ideal R L k I = ⊥ := by rw [← derived_series_eq_derived_series_of_ideal_map, map_eq_bot_iff, ker_incl, eq_bot_iff] lemma derived_series_add_eq_bot {k l : ℕ} {I J : lie_ideal R L} (hI : derived_series R I k = ⊥) (hJ : derived_series R J l = ⊥) : derived_series R ↥(I + J) (k + l) = ⊥ := begin rw lie_ideal.derived_series_eq_bot_iff at hI hJ ⊢, rw ← le_bot_iff, let D := derived_series_of_ideal R L, change D k I = ⊥ at hI, change D l J = ⊥ at hJ, calc D (k + l) (I + J) ≤ (D k I) + (D l J) : derived_series_of_ideal_add_le_add I J k l ... ≤ ⊥ : by { rw [hI, hJ], simp, }, end lemma derived_series_map_le (k : ℕ) : (derived_series R L' k).map f ≤ derived_series R L k := begin induction k with k ih, { simp only [derived_series_def, derived_series_of_ideal_zero, le_top], }, { simp only [derived_series_def, derived_series_of_ideal_succ] at ih ⊢, exact le_trans (map_bracket_le f) (lie_submodule.mono_lie _ _ _ _ ih ih), }, end lemma derived_series_map_eq (k : ℕ) (h : function.surjective f) : (derived_series R L' k).map f = derived_series R L k := begin induction k with k ih, { change (⊤ : lie_ideal R L').map f = ⊤, rw ←f.ideal_range_eq_map, exact f.ideal_range_eq_top_of_surjective h, }, { simp only [derived_series_def, map_bracket_eq f h, ih, derived_series_of_ideal_succ], }, end end lie_ideal namespace lie_algebra /-- A Lie algebra is solvable if its derived series reaches 0 (in a finite number of steps). -/ class is_solvable : Prop := (solvable : ∃ k, derived_series R L k = ⊥) instance is_solvable_bot : is_solvable R ↥(⊥ : lie_ideal R L) := ⟨⟨0, @subsingleton.elim _ lie_ideal.subsingleton_of_bot _ ⊥⟩⟩ instance is_solvable_add {I J : lie_ideal R L} [hI : is_solvable R I] [hJ : is_solvable R J] : is_solvable R ↥(I + J) := begin tactic.unfreeze_local_instances, obtain ⟨k, hk⟩ := hI, obtain ⟨l, hl⟩ := hJ, exact ⟨⟨k+l, lie_ideal.derived_series_add_eq_bot hk hl⟩⟩, end end lie_algebra variables {R L} namespace function open lie_algebra lemma injective.lie_algebra_is_solvable [h₁ : is_solvable R L] (h₂ : injective f) : is_solvable R L' := begin tactic.unfreeze_local_instances, obtain ⟨k, hk⟩ := h₁, use k, apply lie_ideal.bot_of_map_eq_bot h₂, rw [eq_bot_iff, ← hk], apply lie_ideal.derived_series_map_le, end lemma surjective.lie_algebra_is_solvable [h₁ : is_solvable R L'] (h₂ : surjective f) : is_solvable R L := begin tactic.unfreeze_local_instances, obtain ⟨k, hk⟩ := h₁, use k, rw [← lie_ideal.derived_series_map_eq k h₂, hk], simp only [lie_ideal.map_eq_bot_iff, bot_le], end end function lemma lie_hom.is_solvable_range (f : L' →ₗ⁅R⁆ L) [h : lie_algebra.is_solvable R L'] : lie_algebra.is_solvable R f.range := f.surjective_range_restrict.lie_algebra_is_solvable namespace lie_algebra lemma solvable_iff_equiv_solvable (e : L' ≃ₗ⁅R⁆ L) : is_solvable R L' ↔ is_solvable R L := begin split; introsI h, { exact e.symm.injective.lie_algebra_is_solvable, }, { exact e.injective.lie_algebra_is_solvable, }, end lemma le_solvable_ideal_solvable {I J : lie_ideal R L} (h₁ : I ≤ J) (h₂ : is_solvable R J) : is_solvable R I := (lie_ideal.hom_of_le_injective h₁).lie_algebra_is_solvable variables (R L) @[priority 100] instance of_abelian_is_solvable [is_lie_abelian L] : is_solvable R L := begin use 1, rw [← abelian_iff_derived_one_eq_bot, lie_abelian_iff_equiv_lie_abelian lie_ideal.top_equiv_self], apply_instance, end /-- The (solvable) radical of Lie algebra is the `Sup` of all solvable ideals. -/ def radical := Sup { I : lie_ideal R L | is_solvable R I } /-- The radical of a Noetherian Lie algebra is solvable. -/ instance radical_is_solvable [is_noetherian R L] : is_solvable R (radical R L) := begin have hwf := lie_submodule.well_founded_of_noetherian R L L, rw ← complete_lattice.is_sup_closed_compact_iff_well_founded at hwf, refine hwf { I : lie_ideal R L | is_solvable R I } _ _, { use ⊥, exact lie_algebra.is_solvable_bot R L, }, { intros I J hI hJ, apply lie_algebra.is_solvable_add R L; [exact hI, exact hJ], }, end /-- The `→` direction of this lemma is actually true without the `is_noetherian` assumption. -/ lemma lie_ideal.solvable_iff_le_radical [is_noetherian R L] (I : lie_ideal R L) : is_solvable R I ↔ I ≤ radical R L := begin split; intros h, { exact le_Sup h, }, { apply le_solvable_ideal_solvable h, apply_instance, }, end lemma center_le_radical : center R L ≤ radical R L := have h : is_solvable R (center R L), { apply_instance, }, le_Sup h /-- Given a solvable Lie ideal `I` with derived series `I = D₀ ≥ D₁ ≥ ⋯ ≥ Dₖ = ⊥`, this is the natural number `k` (the number of inclusions). For a non-solvable ideal, the value is 0. -/ noncomputable def derived_length_of_ideal (I : lie_ideal R L) : ℕ := Inf {k | derived_series_of_ideal R L k I = ⊥} /-- The derived length of a Lie algebra is the derived length of its 'top' Lie ideal. See also `lie_algebra.derived_length_eq_derived_length_of_ideal`. -/ noncomputable abbreviation derived_length : ℕ := derived_length_of_ideal R L ⊤ lemma derived_series_of_derived_length_succ (I : lie_ideal R L) (k : ℕ) : derived_length_of_ideal R L I = k + 1 ↔ is_lie_abelian (derived_series_of_ideal R L k I) ∧ derived_series_of_ideal R L k I ≠ ⊥ := begin rw abelian_iff_derived_succ_eq_bot, let s := {k | derived_series_of_ideal R L k I = ⊥}, change Inf s = k + 1 ↔ k + 1 ∈ s ∧ k ∉ s, have hs : ∀ (k₁ k₂ : ℕ), k₁ ≤ k₂ → k₁ ∈ s → k₂ ∈ s, { intros k₁ k₂ h₁₂ h₁, suffices : derived_series_of_ideal R L k₂ I ≤ ⊥, { exact eq_bot_iff.mpr this, }, change derived_series_of_ideal R L k₁ I = ⊥ at h₁, rw ← h₁, exact derived_series_of_ideal_antimono I h₁₂, }, exact nat.Inf_upward_closed_eq_succ_iff hs k, end lemma derived_length_eq_derived_length_of_ideal (I : lie_ideal R L) : derived_length R I = derived_length_of_ideal R L I := begin let s₁ := {k | derived_series R I k = ⊥}, let s₂ := {k | derived_series_of_ideal R L k I = ⊥}, change Inf s₁ = Inf s₂, congr, ext k, exact I.derived_series_eq_bot_iff k, end variables {R L} /-- Given a solvable Lie ideal `I` with derived series `I = D₀ ≥ D₁ ≥ ⋯ ≥ Dₖ = ⊥`, this is the `k-1`th term in the derived series (and is therefore an Abelian ideal contained in `I`). For a non-solvable ideal, this is the zero ideal, `⊥`. -/ noncomputable def derived_abelian_of_ideal (I : lie_ideal R L) : lie_ideal R L := match derived_length_of_ideal R L I with | 0 := ⊥ | k + 1 := derived_series_of_ideal R L k I end lemma abelian_derived_abelian_of_ideal (I : lie_ideal R L) : is_lie_abelian (derived_abelian_of_ideal I) := begin dunfold derived_abelian_of_ideal, cases h : derived_length_of_ideal R L I with k, { exact is_lie_abelian_bot R L, }, { rw derived_series_of_derived_length_succ at h, exact h.1, }, end lemma derived_length_zero (I : lie_ideal R L) [hI : is_solvable R I] : derived_length_of_ideal R L I = 0 ↔ I = ⊥ := begin let s := {k | derived_series_of_ideal R L k I = ⊥}, change Inf s = 0 ↔ _, have hne : s ≠ ∅, { rw set.ne_empty_iff_nonempty, tactic.unfreeze_local_instances, obtain ⟨k, hk⟩ := hI, use k, rw [derived_series_def, lie_ideal.derived_series_eq_bot_iff] at hk, exact hk, }, simp [hne], end lemma abelian_of_solvable_ideal_eq_bot_iff (I : lie_ideal R L) [h : is_solvable R I] : derived_abelian_of_ideal I = ⊥ ↔ I = ⊥ := begin dunfold derived_abelian_of_ideal, cases h : derived_length_of_ideal R L I with k, { rw derived_length_zero at h, rw h, refl, }, { obtain ⟨h₁, h₂⟩ := (derived_series_of_derived_length_succ R L I k).mp h, have h₃ : I ≠ ⊥, { intros contra, apply h₂, rw contra, apply derived_series_of_bot_eq_bot, }, change derived_series_of_ideal R L k I = ⊥ ↔ I = ⊥, split; contradiction, }, end end lie_algebra
92980efbd57a1bdbd22bd6833f48133c29367633
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Init/Data/Array/Mem.lean
1c6ec08e958a905fa044c27c4f4b034268c1e286
[ "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
2,193
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.Array.Basic import Init.Data.Nat.Linear import Init.Data.List.BasicAux theorem List.sizeOf_get_lt [SizeOf α] (as : List α) (i : Fin as.length) : sizeOf (as.get i) < sizeOf as := by match as, i with | [], i => apply Fin.elim0 i | a::as, ⟨0, _⟩ => simp_arith [get] | a::as, ⟨i+1, h⟩ => simp [get] have h : i < as.length := Nat.lt_of_succ_lt_succ h have ih := sizeOf_get_lt as ⟨i, h⟩ exact Nat.lt_of_lt_of_le ih (Nat.le_add_left ..) namespace Array instance [DecidableEq α] : Membership α (Array α) where mem a as := as.contains a theorem sizeOf_get_lt [SizeOf α] (as : Array α) (i : Fin as.size) : sizeOf (as.get i) < sizeOf as := by cases as; rename_i as simp [get] have ih := List.sizeOf_get_lt as i exact Nat.lt_trans ih (by simp_arith) theorem sizeOf_lt_of_mem [DecidableEq α] [SizeOf α] {as : Array α} (h : a ∈ as) : sizeOf a < sizeOf as := by simp [Membership.mem, contains, any, Id.run, BEq.beq, anyM] at h let rec aux (j : Nat) (h : anyM.loop (m := Id) (fun b => decide (a = b)) as as.size (Nat.le_refl ..) j = true) : sizeOf a < sizeOf as := by unfold anyM.loop at h split at h · simp [Bind.bind, pure] at h; split at h next he => subst a; apply sizeOf_get_lt next => have ih := aux (j+1) h; assumption · contradiction apply aux 0 h termination_by aux j _ => as.size - j @[simp] theorem sizeOf_get [SizeOf α] (as : Array α) (i : Fin as.size) : sizeOf (as.get i) < sizeOf as := by cases as simp [get] apply Nat.lt_trans (List.sizeOf_get ..) simp_arith /-- This tactic, added to the `decreasing_trivial` toolbox, proves that `sizeOf arr[i] < sizeOf arr`, which is useful for well founded recursions over a nested inductive like `inductive T | mk : Array T → T`. -/ macro "array_get_dec" : tactic => `(tactic| first | apply sizeOf_get | apply Nat.lt_trans (sizeOf_get ..); simp_arith) macro_rules | `(tactic| decreasing_trivial) => `(tactic| array_get_dec) end Array
c46121fa1117eeb63c605788b441bb59a3ad47fa
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_423.lean
1990833eb9b345c442fe437fc6da3a92f6e5df7b
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
632
lean
import data.real.basic variables (f g : ℝ → ℝ) -- BEGIN def fn_even (f : ℝ → ℝ) : Prop := ∀ x, f x = f (-x) def fn_odd (f : ℝ → ℝ) : Prop := ∀ x, f x = - f (-x) example (ef : fn_even f) (eg : fn_even g) : fn_even (λ x, f x + g x) := begin intro x, calc (λ x, f x + g x) x = f x + g x : rfl ... = f (-x) + g (-x) : by rw [ef, eg] end example (of : fn_odd f) (og : fn_odd g) : fn_even (λ x, f x * g x) := sorry example (ef : fn_even f) (og : fn_odd g) : fn_odd (λ x, f x * g x) := sorry example (ef : fn_even f) (og : fn_odd g) : fn_even (λ x, f (g x)) := sorry -- END
335774aa5ed69bd7c774f4ef25dabbaaf08ea64f
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/linear_algebra/free_module/rank.lean
d23cf5a7782d599b01dcf3aeb2f5ab5859d3ecfa
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,612
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, ← add] 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 u} (M : ι → Type v) [Π (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''.symm, ← cardinal.sum_mk, λ i, (B i).mk_eq_dim''], end 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, mul] 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
3d7687acbe1f8cc675088f81bfeab06c36b8b091
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/analysis/normed_space/multilinear.lean
da9426bf6c04710761798de629d58f90cf67301d
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
72,042
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.normed_space.operator_norm import topology.algebra.multilinear /-! # Operator norm on the space of continuous multilinear maps When `f` is a continuous multilinear map in finitely many variables, we define its norm `∥f∥` as the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. We show that it is indeed a norm, and prove its basic properties. ## Main results Let `f` be a multilinear map in finitely many variables. * `exists_bound_of_continuous` asserts that, if `f` is continuous, then there exists `C > 0` with `∥f m∥ ≤ C * ∏ i, ∥m i∥` for all `m`. * `continuous_of_bound`, conversely, asserts that this bound implies continuity. * `mk_continuous` constructs the associated continuous multilinear map. Let `f` be a continuous multilinear map in finitely many variables. * `∥f∥` is its norm, i.e., the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. * `le_op_norm f m` asserts the fundamental inequality `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥`. * `norm_image_sub_le f m₁ m₂` gives a control of the difference `f m₁ - f m₂` in terms of `∥f∥` and `∥m₁ - m₂∥`. We also register isomorphisms corresponding to currying or uncurrying variables, transforming a continuous multilinear function `f` in `n+1` variables into a continuous linear function taking values in continuous multilinear functions in `n` variables, and also into a continuous multilinear function in `n` variables taking values in continuous linear functions. These operations are called `f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and `f.uncurry_right`). They induce continuous linear equivalences between spaces of continuous multilinear functions in `n+1` variables and spaces of continuous linear functions into continuous multilinear functions in `n` variables (resp. continuous multilinear functions in `n` variables taking values in continuous linear functions), called respectively `continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`. ## Implementation notes We mostly follow the API (and the proofs) of `operator_norm.lean`, with the additional complexity that we should deal with multilinear maps in several variables. The currying/uncurrying constructions are based on those in `multilinear.lean`. From the mathematical point of view, all the results follow from the results on operator norm in one variable, by applying them to one variable after the other through currying. However, this is only well defined when there is an order on the variables (for instance on `fin n`) although the final result is independent of the order. While everything could be done following this approach, it turns out that direct proofs are easier and more efficient. -/ noncomputable theory open_locale classical big_operators open finset metric local attribute [instance, priority 1001] add_comm_group.to_add_comm_monoid normed_group.to_add_comm_group normed_space.to_module -- hack to speed up simp when dealing with complicated types local attribute [-instance] unique.subsingleton pi.subsingleton /-! ### Type variables We use the following type variables in this file: * `𝕜` : a `nondiscrete_normed_field`; * `ι`, `ι'` : finite index types with decidable equality; * `E`, `E₁` : families of normed vector spaces over `𝕜` indexed by `i : ι`; * `E'` : a family of normed vector spaces over `𝕜` indexed by `i' : ι'`; * `Ei` : a family of normed vector spaces over `𝕜` indexed by `i : fin (nat.succ n)`; * `G`, `G'` : normed vector spaces over `𝕜`. -/ universes u v v' wE wE₁ wE' wEi wG wG' variables {𝕜 : Type u} {ι : Type v} {ι' : Type v'} {n : ℕ} {E : ι → Type wE} {E₁ : ι → Type wE₁} {E' : ι' → Type wE'} {Ei : fin n.succ → Type wEi} {G : Type wG} {G' : Type wG'} [decidable_eq ι] [fintype ι] [decidable_eq ι'] [fintype ι'] [nondiscrete_normed_field 𝕜] [Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)] [Π i, normed_group (E₁ i)] [Π i, normed_space 𝕜 (E₁ i)] [Π i, normed_group (E' i)] [Π i, normed_space 𝕜 (E' i)] [Π i, normed_group (Ei i)] [Π i, normed_space 𝕜 (Ei i)] [normed_group G] [normed_space 𝕜 G] [normed_group G'] [normed_space 𝕜 G'] /-! ### Continuity properties of multilinear maps We relate continuity of multilinear maps to the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, in both directions. Along the way, we prove useful bounds on the difference `∥f m₁ - f m₂∥`. -/ namespace multilinear_map variable (f : multilinear_map 𝕜 E G) /-- If a multilinear map in finitely many variables on normed spaces satisfies the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥` on a shell `ε i / ∥c i∥ < ∥m i∥ < ε i` for some positive numbers `ε i` and elements `c i : 𝕜`, `1 < ∥c i∥`, then it satisfies this inequality for all `m`. -/ lemma bound_of_shell {ε : ι → ℝ} {C : ℝ} (hε : ∀ i, 0 < ε i) {c : ι → 𝕜} (hc : ∀ i, 1 < ∥c i∥) (hf : ∀ m : Π i, E i, (∀ i, ε i / ∥c i∥ ≤ ∥m i∥) → (∀ i, ∥m i∥ < ε i) → ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m : Π i, E i) : ∥f m∥ ≤ C * ∏ i, ∥m i∥ := begin rcases em (∃ i, m i = 0) with ⟨i, hi⟩|hm; [skip, push_neg at hm], { simp [f.map_coord_zero i hi, prod_eq_zero (mem_univ i), hi] }, choose δ hδ0 hδm_lt hle_δm hδinv using λ i, rescale_to_shell (hc i) (hε i) (hm i), have hδ0 : 0 < ∏ i, ∥δ i∥, from prod_pos (λ i _, norm_pos_iff.2 (hδ0 i)), simpa [map_smul_univ, norm_smul, prod_mul_distrib, mul_left_comm C, mul_le_mul_left hδ0] using hf (λ i, δ i • m i) hle_δm hδm_lt, end /-- If a multilinear map in finitely many variables on normed spaces is continuous, then it satisfies the inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, for some `C` which can be chosen to be positive. -/ theorem exists_bound_of_continuous (hf : continuous f) : ∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) := begin by_cases hι : nonempty ι, swap, { refine ⟨∥f 0∥ + 1, add_pos_of_nonneg_of_pos (norm_nonneg _) zero_lt_one, λ m, _⟩, obtain rfl : m = 0, from funext (λ i, (hι ⟨i⟩).elim), simp [univ_eq_empty.2 hι, zero_le_one] }, resetI, obtain ⟨ε : ℝ, ε0 : 0 < ε, hε : ∀ m : Π i, E i, ∥m - 0∥ < ε → ∥f m - f 0∥ < 1⟩ := normed_group.tendsto_nhds_nhds.1 (hf.tendsto 0) 1 zero_lt_one, simp only [sub_zero, f.map_zero] at hε, rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, have : 0 < (∥c∥ / ε) ^ fintype.card ι, from pow_pos (div_pos (zero_lt_one.trans hc) ε0) _, refine ⟨_, this, _⟩, refine f.bound_of_shell (λ _, ε0) (λ _, hc) (λ m hcm hm, _), refine (hε m ((pi_norm_lt_iff ε0).2 hm)).le.trans _, rw [← div_le_iff' this, one_div, ← inv_pow', inv_div, fintype.card, ← prod_const], exact prod_le_prod (λ _ _, div_nonneg ε0.le (norm_nonneg _)) (λ i _, hcm i) end /-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂` using the multilinearity. Here, we give a precise but hard to use version. See `norm_image_sub_le_of_bound` for a less precise but more usable version. The bound reads `∥f m - f m'∥ ≤ C * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`, where the other terms in the sum are the same products where `1` is replaced by any `i`. -/ lemma norm_image_sub_le_of_bound' {C : ℝ} (hC : 0 ≤ C) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E i) : ∥f m₁ - f m₂∥ ≤ C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ := begin have A : ∀(s : finset ι), ∥f m₁ - f (s.piecewise m₂ m₁)∥ ≤ C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥, { refine finset.induction (by simp) _, assume i s his Hrec, have I : ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥ ≤ C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥, { have A : ((insert i s).piecewise m₂ m₁) = function.update (s.piecewise m₂ m₁) i (m₂ i) := s.piecewise_insert _ _ _, have B : s.piecewise m₂ m₁ = function.update (s.piecewise m₂ m₁) i (m₁ i), { ext j, by_cases h : j = i, { rw h, simp [his] }, { simp [h] } }, rw [B, A, ← f.map_sub], apply le_trans (H _) (mul_le_mul_of_nonneg_left _ hC), refine prod_le_prod (λj hj, norm_nonneg _) (λj hj, _), by_cases h : j = i, { rw h, simp }, { by_cases h' : j ∈ s; simp [h', h, le_refl] } }, calc ∥f m₁ - f ((insert i s).piecewise m₂ m₁)∥ ≤ ∥f m₁ - f (s.piecewise m₂ m₁)∥ + ∥f (s.piecewise m₂ m₁) - f ((insert i s).piecewise m₂ m₁)∥ : by { rw [← dist_eq_norm, ← dist_eq_norm, ← dist_eq_norm], exact dist_triangle _ _ _ } ... ≤ (C * ∑ i in s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥) + C * ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ : add_le_add Hrec I ... = C * ∑ i in insert i s, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ : by simp [his, add_comm, left_distrib] }, convert A univ, simp end /-- If `f` satisfies a boundedness property around `0`, one can deduce a bound on `f m₁ - f m₂` using the multilinearity. Here, we give a usable but not very precise version. See `norm_image_sub_le_of_bound'` for a more precise but less usable version. The bound is `∥f m - f m'∥ ≤ C * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`. -/ lemma norm_image_sub_le_of_bound {C : ℝ} (hC : 0 ≤ C) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (m₁ m₂ : Πi, E i) : ∥f m₁ - f m₂∥ ≤ C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ := begin have A : ∀ (i : ι), ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥) ≤ ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1), { assume i, calc ∏ j, (if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥) ≤ ∏ j : ι, function.update (λ j, max ∥m₁∥ ∥m₂∥) i (∥m₁ - m₂∥) j : begin apply prod_le_prod, { assume j hj, by_cases h : j = i; simp [h, norm_nonneg] }, { assume j hj, by_cases h : j = i, { rw h, simp, exact norm_le_pi_norm (m₁ - m₂) i }, { simp [h, max_le_max, norm_le_pi_norm] } } end ... = ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) : by { rw prod_update_of_mem (finset.mem_univ _), simp [card_univ_diff] } }, calc ∥f m₁ - f m₂∥ ≤ C * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ : f.norm_image_sub_le_of_bound' hC H m₁ m₂ ... ≤ C * ∑ i, ∥m₁ - m₂∥ * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) : mul_le_mul_of_nonneg_left (sum_le_sum (λi hi, A i)) hC ... = C * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ : by { rw [sum_const, card_univ, nsmul_eq_mul], ring } end /-- If a multilinear map satisfies an inequality `∥f m∥ ≤ C * ∏ i, ∥m i∥`, then it is continuous. -/ theorem continuous_of_bound (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : continuous f := begin let D := max C 1, have D_pos : 0 ≤ D := le_trans zero_le_one (le_max_right _ _), replace H : ∀ m, ∥f m∥ ≤ D * ∏ i, ∥m i∥, { assume m, apply le_trans (H m) (mul_le_mul_of_nonneg_right (le_max_left _ _) _), exact prod_nonneg (λ(i : ι) hi, norm_nonneg (m i)) }, refine continuous_iff_continuous_at.2 (λm, _), refine continuous_at_of_locally_lipschitz zero_lt_one (D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1)) (λm' h', _), rw [dist_eq_norm, dist_eq_norm], have : 0 ≤ (max ∥m'∥ ∥m∥), by simp, have : (max ∥m'∥ ∥m∥) ≤ ∥m∥ + 1, by simp [zero_le_one, norm_le_of_mem_closed_ball (le_of_lt h'), -add_comm], calc ∥f m' - f m∥ ≤ D * (fintype.card ι) * (max ∥m'∥ ∥m∥) ^ (fintype.card ι - 1) * ∥m' - m∥ : f.norm_image_sub_le_of_bound D_pos H m' m ... ≤ D * (fintype.card ι) * (∥m∥ + 1) ^ (fintype.card ι - 1) * ∥m' - m∥ : by apply_rules [mul_le_mul_of_nonneg_right, mul_le_mul_of_nonneg_left, mul_nonneg, norm_nonneg, nat.cast_nonneg, pow_le_pow_of_le_left] end /-- Constructing a continuous multilinear map from a multilinear map satisfying a boundedness condition. -/ def mk_continuous (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : continuous_multilinear_map 𝕜 E G := { cont := f.continuous_of_bound C H, ..f } @[simp] lemma coe_mk_continuous (C : ℝ) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ⇑(f.mk_continuous C H) = f := rfl /-- Given a multilinear map in `n` variables, if one restricts it to `k` variables putting `z` on the other coordinates, then the resulting restricted function satisfies an inequality `∥f.restr v∥ ≤ C * ∥z∥^(n-k) * Π ∥v i∥` if the original function satisfies `∥f v∥ ≤ C * Π ∥v i∥`. -/ lemma restr_norm_le {k n : ℕ} (f : (multilinear_map 𝕜 (λ i : fin n, G) G' : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) {C : ℝ} (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) (v : fin k → G) : ∥f.restr s hk z v∥ ≤ C * ∥z∥ ^ (n - k) * ∏ i, ∥v i∥ := begin rw [mul_right_comm, mul_assoc], convert H _ using 2, simp only [apply_dite norm, fintype.prod_dite, prod_const (∥z∥), finset.card_univ, fintype.card_of_subtype sᶜ (λ x, mem_compl), card_compl, fintype.card_fin, hk, mk_coe, ← (s.order_iso_of_fin hk).symm.bijective.prod_comp (λ x, ∥v x∥)], refl end end multilinear_map /-! ### Continuous multilinear maps We define the norm `∥f∥` of a continuous multilinear map `f` in finitely many variables as the smallest number such that `∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥` for all `m`. We show that this defines a normed space structure on `continuous_multilinear_map 𝕜 E G`. -/ namespace continuous_multilinear_map variables (c : 𝕜) (f g : continuous_multilinear_map 𝕜 E G) (m : Πi, E i) theorem bound : ∃ (C : ℝ), 0 < C ∧ (∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) := f.to_multilinear_map.exists_bound_of_continuous f.2 open real /-- The operator norm of a continuous multilinear map is the inf of all its bounds. -/ def op_norm := Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} instance has_op_norm : has_norm (continuous_multilinear_map 𝕜 E G) := ⟨op_norm⟩ lemma norm_def : ∥f∥ = Inf {c | 0 ≤ (c : ℝ) ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := rfl -- So that invocations of `real.Inf_le` make sense: we show that the set of -- bounds is nonempty and bounded below. lemma bounds_nonempty {f : continuous_multilinear_map 𝕜 E G} : ∃ c, c ∈ {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩ lemma bounds_bdd_below {f : continuous_multilinear_map 𝕜 E G} : bdd_below {c | 0 ≤ c ∧ ∀ m, ∥f m∥ ≤ c * ∏ i, ∥m i∥} := ⟨0, λ _ ⟨hn, _⟩, hn⟩ lemma op_norm_nonneg : 0 ≤ ∥f∥ := lb_le_Inf _ bounds_nonempty (λ _ ⟨hx, _⟩, hx) /-- The fundamental property of the operator norm of a continuous multilinear map: `∥f m∥` is bounded by `∥f∥` times the product of the `∥m i∥`. -/ theorem le_op_norm : ∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ := begin have A : 0 ≤ ∏ i, ∥m i∥ := prod_nonneg (λj hj, norm_nonneg _), cases A.eq_or_lt with h hlt, { rcases prod_eq_zero_iff.1 h.symm with ⟨i, _, hi⟩, rw norm_eq_zero at hi, have : f m = 0 := f.map_coord_zero i hi, rw [this, norm_zero], exact mul_nonneg (op_norm_nonneg f) A }, { rw [← div_le_iff hlt], apply (le_Inf _ bounds_nonempty bounds_bdd_below).2, rintro c ⟨_, hc⟩, rw [div_le_iff hlt], apply hc } end theorem le_of_op_norm_le {C : ℝ} (h : ∥f∥ ≤ C) : ∥f m∥ ≤ C * ∏ i, ∥m i∥ := (f.le_op_norm m).trans $ mul_le_mul_of_nonneg_right h (prod_nonneg $ λ i _, norm_nonneg (m i)) lemma ratio_le_op_norm : ∥f m∥ / ∏ i, ∥m i∥ ≤ ∥f∥ := div_le_of_nonneg_of_le_mul (prod_nonneg $ λ i _, norm_nonneg _) (op_norm_nonneg _) (f.le_op_norm m) /-- The image of the unit ball under a continuous multilinear map is bounded. -/ lemma unit_le_op_norm (h : ∥m∥ ≤ 1) : ∥f m∥ ≤ ∥f∥ := calc ∥f m∥ ≤ ∥f∥ * ∏ i, ∥m i∥ : f.le_op_norm m ... ≤ ∥f∥ * ∏ i : ι, 1 : mul_le_mul_of_nonneg_left (prod_le_prod (λi hi, norm_nonneg _) (λi hi, le_trans (norm_le_pi_norm _ _) h)) (op_norm_nonneg f) ... = ∥f∥ : by simp /-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/ lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ m, ∥f m∥ ≤ M * ∏ i, ∥m i∥) : ∥f∥ ≤ M := Inf_le _ bounds_bdd_below ⟨hMp, hM⟩ /-- The operator norm satisfies the triangle inequality. -/ theorem op_norm_add_le : ∥f + g∥ ≤ ∥f∥ + ∥g∥ := Inf_le _ bounds_bdd_below ⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw add_mul, exact norm_add_le_of_le (le_op_norm _ _) (le_op_norm _ _) }⟩ /-- A continuous linear map is zero iff its norm vanishes. -/ theorem op_norm_zero_iff : ∥f∥ = 0 ↔ f = 0 := begin split, { assume h, ext m, simpa [h] using f.le_op_norm m }, { rintro rfl, apply le_antisymm (op_norm_le_bound 0 le_rfl (λm, _)) (op_norm_nonneg _), simp } end variables {𝕜' : Type*} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜' 𝕜] [normed_space 𝕜' G] [is_scalar_tower 𝕜' 𝕜 G] lemma op_norm_smul_le (c : 𝕜') : ∥c • f∥ ≤ ∥c∥ * ∥f∥ := (c • f).op_norm_le_bound (mul_nonneg (norm_nonneg _) (op_norm_nonneg _)) begin intro m, erw [norm_smul, mul_assoc], exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _) end lemma op_norm_neg : ∥-f∥ = ∥f∥ := by { rw norm_def, apply congr_arg, ext, simp } /-- Continuous multilinear maps themselves form a normed space with respect to the operator norm. -/ instance to_normed_group : normed_group (continuous_multilinear_map 𝕜 E G) := normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_add_le, op_norm_neg⟩ instance to_normed_space : normed_space 𝕜' (continuous_multilinear_map 𝕜 E G) := ⟨λ c f, f.op_norm_smul_le c⟩ lemma op_norm_prod (f : continuous_multilinear_map 𝕜 E G) (g : continuous_multilinear_map 𝕜 E G') : ∥f.prod g∥ = max (∥f∥) (∥g∥) := le_antisymm (op_norm_le_bound _ (norm_nonneg (f, g)) (λ m, have H : 0 ≤ ∏ i, ∥m i∥, from prod_nonneg $ λ _ _, norm_nonneg _, by simpa only [prod_apply, prod.norm_def, max_mul_of_nonneg, H] using max_le_max (f.le_op_norm m) (g.le_op_norm m))) $ max_le (f.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_left _ _).trans ((f.prod g).le_op_norm _)) (g.op_norm_le_bound (norm_nonneg _) $ λ m, (le_max_right _ _).trans ((f.prod g).le_op_norm _)) lemma norm_pi {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_group (E' i')] [Π i', normed_space 𝕜 (E' i')] (f : Π i', continuous_multilinear_map 𝕜 E (E' i')) : ∥pi f∥ = ∥f∥ := begin apply le_antisymm, { refine (op_norm_le_bound _ (norm_nonneg f) (λ m, _)), dsimp, rw pi_norm_le_iff, exacts [λ i, (f i).le_of_op_norm_le m (norm_le_pi_norm f i), mul_nonneg (norm_nonneg f) (prod_nonneg $ λ _ _, norm_nonneg _)] }, { refine (pi_norm_le_iff (norm_nonneg _)).2 (λ i, _), refine (op_norm_le_bound _ (norm_nonneg _) (λ m, _)), refine le_trans _ ((pi f).le_op_norm m), convert norm_le_pi_norm (λ j, f j m) i } end section variables (𝕜 E E' G G') /-- `continuous_multilinear_map.prod` as a `linear_isometry_equiv`. -/ def prodL : (continuous_multilinear_map 𝕜 E G) × (continuous_multilinear_map 𝕜 E G') ≃ₗᵢ[𝕜] continuous_multilinear_map 𝕜 E (G × G') := { to_fun := λ f, f.1.prod f.2, inv_fun := λ f, ((continuous_linear_map.fst 𝕜 G G').comp_continuous_multilinear_map f, (continuous_linear_map.snd 𝕜 G G').comp_continuous_multilinear_map f), map_add' := λ f g, rfl, map_smul' := λ c f, rfl, left_inv := λ f, by ext; refl, right_inv := λ f, by ext; refl, norm_map' := λ f, op_norm_prod f.1 f.2 } /-- `continuous_multilinear_map.pi` as a `linear_isometry_equiv`. -/ def piₗᵢ {ι' : Type v'} [fintype ι'] {E' : ι' → Type wE'} [Π i', normed_group (E' i')] [Π i', normed_space 𝕜 (E' i')] : @linear_isometry_equiv 𝕜 (Π i', continuous_multilinear_map 𝕜 E (E' i')) (continuous_multilinear_map 𝕜 E (Π i, E' i)) _ _ _ (@pi.module ι' _ 𝕜 _ _ (λ i', infer_instance)) _ := { to_fun := pi, map_add' := λ f g, rfl, map_smul' := λ c f, rfl, inv_fun := λ f i, (@continuous_linear_map.proj 𝕜 _ _ E' _ _ _ i).comp_continuous_multilinear_map f, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl }, norm_map' := norm_pi } end section restrict_scalars variables [Π i, normed_space 𝕜' (E i)] [∀ i, is_scalar_tower 𝕜' 𝕜 (E i)] @[simp] lemma norm_restrict_scalars : ∥f.restrict_scalars 𝕜'∥ = ∥f∥ := by simp only [norm_def, coe_restrict_scalars] variable (𝕜') /-- `continuous_multilinear_map.restrict_scalars` as a `continuous_multilinear_map`. -/ def restrict_scalars_linear : continuous_multilinear_map 𝕜 E G →L[𝕜'] continuous_multilinear_map 𝕜' E G := linear_map.mk_continuous { to_fun := restrict_scalars 𝕜', map_add' := λ m₁ m₂, rfl, map_smul' := λ c m, rfl } 1 $ λ f, by simp variable {𝕜'} lemma continuous_restrict_scalars : continuous (restrict_scalars 𝕜' : continuous_multilinear_map 𝕜 E G → continuous_multilinear_map 𝕜' E G) := (restrict_scalars_linear 𝕜').continuous end restrict_scalars /-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, precise version. For a less precise but more usable version, see `norm_image_sub_le`. The bound reads `∥f m - f m'∥ ≤ ∥f∥ * ∥m 1 - m' 1∥ * max ∥m 2∥ ∥m' 2∥ * max ∥m 3∥ ∥m' 3∥ * ... * max ∥m n∥ ∥m' n∥ + ...`, where the other terms in the sum are the same products where `1` is replaced by any `i`.-/ lemma norm_image_sub_le' (m₁ m₂ : Πi, E i) : ∥f m₁ - f m₂∥ ≤ ∥f∥ * ∑ i, ∏ j, if j = i then ∥m₁ i - m₂ i∥ else max ∥m₁ j∥ ∥m₂ j∥ := f.to_multilinear_map.norm_image_sub_le_of_bound' (norm_nonneg _) f.le_op_norm _ _ /-- The difference `f m₁ - f m₂` is controlled in terms of `∥f∥` and `∥m₁ - m₂∥`, less precise version. For a more precise but less usable version, see `norm_image_sub_le'`. The bound is `∥f m - f m'∥ ≤ ∥f∥ * card ι * ∥m - m'∥ * (max ∥m∥ ∥m'∥) ^ (card ι - 1)`.-/ lemma norm_image_sub_le (m₁ m₂ : Πi, E i) : ∥f m₁ - f m₂∥ ≤ ∥f∥ * (fintype.card ι) * (max ∥m₁∥ ∥m₂∥) ^ (fintype.card ι - 1) * ∥m₁ - m₂∥ := f.to_multilinear_map.norm_image_sub_le_of_bound (norm_nonneg _) f.le_op_norm _ _ /-- Applying a multilinear map to a vector is continuous in both coordinates. -/ lemma continuous_eval : continuous (λ p : continuous_multilinear_map 𝕜 E G × Π i, E i, p.1 p.2) := begin apply continuous_iff_continuous_at.2 (λp, _), apply continuous_at_of_locally_lipschitz zero_lt_one ((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) + ∏ i, ∥p.2 i∥) (λq hq, _), have : 0 ≤ (max ∥q.2∥ ∥p.2∥), by simp, have : 0 ≤ ∥p∥ + 1, by simp [le_trans zero_le_one], have A : ∥q∥ ≤ ∥p∥ + 1 := norm_le_of_mem_closed_ball (le_of_lt hq), have : (max ∥q.2∥ ∥p.2∥) ≤ ∥p∥ + 1 := le_trans (max_le_max (norm_snd_le q) (norm_snd_le p)) (by simp [A, -add_comm, zero_le_one]), have : ∀ (i : ι), i ∈ univ → 0 ≤ ∥p.2 i∥ := λ i hi, norm_nonneg _, calc dist (q.1 q.2) (p.1 p.2) ≤ dist (q.1 q.2) (q.1 p.2) + dist (q.1 p.2) (p.1 p.2) : dist_triangle _ _ _ ... = ∥q.1 q.2 - q.1 p.2∥ + ∥q.1 p.2 - p.1 p.2∥ : by rw [dist_eq_norm, dist_eq_norm] ... ≤ ∥q.1∥ * (fintype.card ι) * (max ∥q.2∥ ∥p.2∥) ^ (fintype.card ι - 1) * ∥q.2 - p.2∥ + ∥q.1 - p.1∥ * ∏ i, ∥p.2 i∥ : add_le_add (norm_image_sub_le _ _ _) ((q.1 - p.1).le_op_norm p.2) ... ≤ (∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) * ∥q - p∥ + ∥q - p∥ * ∏ i, ∥p.2 i∥ : by apply_rules [add_le_add, mul_le_mul, le_refl, le_trans (norm_fst_le q) A, nat.cast_nonneg, mul_nonneg, pow_le_pow_of_le_left, pow_nonneg, norm_snd_le (q - p), norm_nonneg, norm_fst_le (q - p), prod_nonneg] ... = ((∥p∥ + 1) * (fintype.card ι) * (∥p∥ + 1) ^ (fintype.card ι - 1) + (∏ i, ∥p.2 i∥)) * dist q p : by { rw dist_eq_norm, ring } end lemma continuous_eval_left (m : Π i, E i) : continuous (λ p : continuous_multilinear_map 𝕜 E G, p m) := continuous_eval.comp (continuous_id.prod_mk continuous_const) lemma has_sum_eval {α : Type*} {p : α → continuous_multilinear_map 𝕜 E G} {q : continuous_multilinear_map 𝕜 E G} (h : has_sum p q) (m : Π i, E i) : has_sum (λ a, p a m) (q m) := begin dsimp [has_sum] at h ⊢, convert ((continuous_eval_left m).tendsto _).comp h, ext s, simp end open_locale topological_space open filter /-- If the target space is complete, the space of continuous multilinear maps with its norm is also complete. The proof is essentially the same as for the space of continuous linear maps (modulo the addition of `finset.prod` where needed. The duplication could be avoided by deducing the linear case from the multilinear case via a currying isomorphism. However, this would mess up imports, and it is more satisfactory to have the simplest case as a standalone proof. -/ instance [complete_space G] : complete_space (continuous_multilinear_map 𝕜 E G) := begin have nonneg : ∀ (v : Π i, E i), 0 ≤ ∏ i, ∥v i∥ := λ v, finset.prod_nonneg (λ i hi, norm_nonneg _), -- We show that every Cauchy sequence converges. refine metric.complete_of_cauchy_seq_tendsto (λ f hf, _), -- We now expand out the definition of a Cauchy sequence, rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, -- and establish that the evaluation at any point `v : Π i, E i` is Cauchy. have cau : ∀ v, cauchy_seq (λ n, f n v), { assume v, apply cauchy_seq_iff_le_tendsto_0.2 ⟨λ n, b n * ∏ i, ∥v i∥, λ n, _, _, _⟩, { exact mul_nonneg (b0 n) (nonneg v) }, { assume n m N hn hm, rw dist_eq_norm, apply le_trans ((f n - f m).le_op_norm v) _, exact mul_le_mul_of_nonneg_right (b_bound n m N hn hm) (nonneg v) }, { simpa using b_lim.mul tendsto_const_nhds } }, -- We assemble the limits points of those Cauchy sequences -- (which exist as `G` is complete) -- into a function which we call `F`. choose F hF using λv, cauchy_seq_tendsto_of_complete (cau v), -- Next, we show that this `F` is multilinear, let Fmult : multilinear_map 𝕜 E G := { to_fun := F, map_add' := λ v i x y, begin have A := hF (function.update v i (x + y)), have B := (hF (function.update v i x)).add (hF (function.update v i y)), simp at A B, exact tendsto_nhds_unique A B end, map_smul' := λ v i c x, begin have A := hF (function.update v i (c • x)), have B := filter.tendsto.smul (@tendsto_const_nhds _ ℕ _ c _) (hF (function.update v i x)), simp at A B, exact tendsto_nhds_unique A B end }, -- and that `F` has norm at most `(b 0 + ∥f 0∥)`. have Fnorm : ∀ v, ∥F v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥, { assume v, have A : ∀ n, ∥f n v∥ ≤ (b 0 + ∥f 0∥) * ∏ i, ∥v i∥, { assume n, apply le_trans ((f n).le_op_norm _) _, apply mul_le_mul_of_nonneg_right _ (nonneg v), calc ∥f n∥ = ∥(f n - f 0) + f 0∥ : by { congr' 1, abel } ... ≤ ∥f n - f 0∥ + ∥f 0∥ : norm_add_le _ _ ... ≤ b 0 + ∥f 0∥ : begin apply add_le_add_right, simpa [dist_eq_norm] using b_bound n 0 0 (zero_le _) (zero_le _) end }, exact le_of_tendsto (hF v).norm (eventually_of_forall A) }, -- Thus `F` is continuous, and we propose that as the limit point of our original Cauchy sequence. let Fcont := Fmult.mk_continuous _ Fnorm, use Fcont, -- Our last task is to establish convergence to `F` in norm. have : ∀ n, ∥f n - Fcont∥ ≤ b n, { assume n, apply op_norm_le_bound _ (b0 n) (λ v, _), have A : ∀ᶠ m in at_top, ∥(f n - f m) v∥ ≤ b n * ∏ i, ∥v i∥, { refine eventually_at_top.2 ⟨n, λ m hm, _⟩, apply le_trans ((f n - f m).le_op_norm _) _, exact mul_le_mul_of_nonneg_right (b_bound n m n (le_refl _) hm) (nonneg v) }, have B : tendsto (λ m, ∥(f n - f m) v∥) at_top (𝓝 (∥(f n - Fcont) v∥)) := tendsto.norm (tendsto_const_nhds.sub (hF v)), exact le_of_tendsto B A }, erw tendsto_iff_norm_tendsto_zero, exact squeeze_zero (λ n, norm_nonneg _) this b_lim, end end continuous_multilinear_map /-- If a continuous multilinear map is constructed from a multilinear map via the constructor `mk_continuous`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma multilinear_map.mk_continuous_norm_le (f : multilinear_map 𝕜 E G) {C : ℝ} (hC : 0 ≤ C) (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ∥f.mk_continuous C H∥ ≤ C := continuous_multilinear_map.op_norm_le_bound _ hC (λm, H m) /-- If a continuous multilinear map is constructed from a multilinear map via the constructor `mk_continuous`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma multilinear_map.mk_continuous_norm_le' (f : multilinear_map 𝕜 E G) {C : ℝ} (H : ∀ m, ∥f m∥ ≤ C * ∏ i, ∥m i∥) : ∥f.mk_continuous C H∥ ≤ max C 0 := continuous_multilinear_map.op_norm_le_bound _ (le_max_right _ _) $ λ m, (H m).trans $ mul_le_mul_of_nonneg_right (le_max_left _ _) (prod_nonneg $ λ _ _, norm_nonneg _) namespace continuous_multilinear_map /-- Given a continuous multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset `s` of `k` of these variables, one gets a new continuous multilinear map on `fin k` by varying these variables, and fixing the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/ def restr {k n : ℕ} (f : (G [×n]→L[𝕜] G' : _)) (s : finset (fin n)) (hk : s.card = k) (z : G) : G [×k]→L[𝕜] G' := (f.to_multilinear_map.restr s hk z).mk_continuous (∥f∥ * ∥z∥^(n-k)) $ λ v, multilinear_map.restr_norm_le _ _ _ _ f.le_op_norm _ lemma norm_restr {k n : ℕ} (f : G [×n]→L[𝕜] G') (s : finset (fin n)) (hk : s.card = k) (z : G) : ∥f.restr s hk z∥ ≤ ∥f∥ * ∥z∥ ^ (n - k) := begin apply multilinear_map.mk_continuous_norm_le, exact mul_nonneg (norm_nonneg _) (pow_nonneg (norm_nonneg _) _) end section variables (𝕜 ι) (A : Type*) [normed_comm_ring A] [normed_algebra 𝕜 A] /-- The continuous multilinear map on `A^ι`, where `A` is a normed commutative algebra over `𝕜`, associating to `m` the product of all the `m i`. See also `continuous_multilinear_map.mk_pi_algebra_fin`. -/ protected def mk_pi_algebra : continuous_multilinear_map 𝕜 (λ i : ι, A) A := @multilinear_map.mk_continuous 𝕜 ι (λ i : ι, A) A _ _ _ _ _ _ _ (multilinear_map.mk_pi_algebra 𝕜 ι A) (if nonempty ι then 1 else ∥(1 : A)∥) $ begin intro m, by_cases hι : nonempty ι, { resetI, simp [hι, norm_prod_le' univ univ_nonempty] }, { simp [eq_empty_of_not_nonempty hι univ, hι] } end variables {A 𝕜 ι} @[simp] lemma mk_pi_algebra_apply (m : ι → A) : continuous_multilinear_map.mk_pi_algebra 𝕜 ι A m = ∏ i, m i := rfl lemma norm_mk_pi_algebra_le [nonempty ι] : ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ 1 := calc ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ if nonempty ι then 1 else ∥(1 : A)∥ : multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _ ... = _ : if_pos ‹_› lemma norm_mk_pi_algebra_of_empty (h : ¬nonempty ι) : ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = ∥(1 : A)∥ := begin apply le_antisymm, calc ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ ≤ if nonempty ι then 1 else ∥(1 : A)∥ : multilinear_map.mk_continuous_norm_le _ (by split_ifs; simp [zero_le_one]) _ ... = ∥(1 : A)∥ : if_neg ‹_›, convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance], simp [eq_empty_of_not_nonempty h univ] end @[simp] lemma norm_mk_pi_algebra [norm_one_class A] : ∥continuous_multilinear_map.mk_pi_algebra 𝕜 ι A∥ = 1 := begin by_cases hι : nonempty ι, { resetI, refine le_antisymm norm_mk_pi_algebra_le _, convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance], simp }, { simp [norm_mk_pi_algebra_of_empty hι] } end end section variables (𝕜 n) (A : Type*) [normed_ring A] [normed_algebra 𝕜 A] /-- The continuous multilinear map on `A^n`, where `A` is a normed algebra over `𝕜`, associating to `m` the product of all the `m i`. See also: `multilinear_map.mk_pi_algebra`. -/ protected def mk_pi_algebra_fin : continuous_multilinear_map 𝕜 (λ i : fin n, A) A := @multilinear_map.mk_continuous 𝕜 (fin n) (λ i : fin n, A) A _ _ _ _ _ _ _ (multilinear_map.mk_pi_algebra_fin 𝕜 n A) (nat.cases_on n ∥(1 : A)∥ (λ _, 1)) $ begin intro m, cases n, { simp }, { have : @list.of_fn A n.succ m ≠ [] := by simp, simpa [← fin.prod_of_fn] using list.norm_prod_le' this } end variables {A 𝕜 n} @[simp] lemma mk_pi_algebra_fin_apply (m : fin n → A) : continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A m = (list.of_fn m).prod := rfl lemma norm_mk_pi_algebra_fin_succ_le : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n.succ A∥ ≤ 1 := multilinear_map.mk_continuous_norm_le _ zero_le_one _ lemma norm_mk_pi_algebra_fin_le_of_pos (hn : 0 < n) : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A∥ ≤ 1 := by cases n; [exact hn.false.elim, exact norm_mk_pi_algebra_fin_succ_le] lemma norm_mk_pi_algebra_fin_zero : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 0 A∥ = ∥(1 : A)∥ := begin refine le_antisymm (multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _) _, convert ratio_le_op_norm _ (λ _, 1); [simp, apply_instance] end @[simp] lemma norm_mk_pi_algebra_fin [norm_one_class A] : ∥continuous_multilinear_map.mk_pi_algebra_fin 𝕜 n A∥ = 1 := begin cases n, { simp [norm_mk_pi_algebra_fin_zero] }, { refine le_antisymm norm_mk_pi_algebra_fin_succ_le _, convert ratio_le_op_norm _ (λ _, 1); [skip, apply_instance], simp } end end variables (𝕜 ι) /-- The canonical continuous multilinear map on `𝕜^ι`, associating to `m` the product of all the `m i` (multiplied by a fixed reference element `z` in the target module) -/ protected def mk_pi_field (z : G) : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G := @multilinear_map.mk_continuous 𝕜 ι (λ(i : ι), 𝕜) G _ _ _ _ _ _ _ (multilinear_map.mk_pi_ring 𝕜 ι z) (∥z∥) (λ m, by simp only [multilinear_map.mk_pi_ring_apply, norm_smul, normed_field.norm_prod, mul_comm]) variables {𝕜 ι} @[simp] lemma mk_pi_field_apply (z : G) (m : ι → 𝕜) : (continuous_multilinear_map.mk_pi_field 𝕜 ι z : (ι → 𝕜) → G) m = (∏ i, m i) • z := rfl lemma mk_pi_field_apply_one_eq_self (f : continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) : continuous_multilinear_map.mk_pi_field 𝕜 ι (f (λi, 1)) = f := to_multilinear_map_inj f.to_multilinear_map.mk_pi_ring_apply_one_eq_self variables (𝕜 ι G) /-- Continuous multilinear maps on `𝕜^n` with values in `G` are in bijection with `G`, as such a continuous multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a linear equivalence in `continuous_multilinear_map.pi_field_equiv_aux`. The continuous linear equivalence is `continuous_multilinear_map.pi_field_equiv`. -/ protected def pi_field_equiv_aux : G ≃ₗ[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) := { to_fun := λ z, continuous_multilinear_map.mk_pi_field 𝕜 ι z, inv_fun := λ f, f (λi, 1), map_add' := λ z z', by { ext m, simp [smul_add] }, map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] }, left_inv := λ z, by simp, right_inv := λ f, f.mk_pi_field_apply_one_eq_self } /-- Continuous multilinear maps on `𝕜^n` with values in `G` are in bijection with `G`, as such a continuous multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a continuous linear equivalence in `continuous_multilinear_map.pi_field_equiv`. -/ protected def pi_field_equiv : G ≃L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : ι), 𝕜) G) := { continuous_to_fun := begin refine (continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι G).to_linear_map.continuous_of_bound (1 : ℝ) (λz, _), rw one_mul, change ∥continuous_multilinear_map.mk_pi_field 𝕜 ι z∥ ≤ ∥z∥, exact multilinear_map.mk_continuous_norm_le _ (norm_nonneg _) _ end, continuous_inv_fun := begin refine (continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι G).symm.to_linear_map.continuous_of_bound (1 : ℝ) (λf, _), rw one_mul, change ∥f (λi, 1)∥ ≤ ∥f∥, apply @continuous_multilinear_map.unit_le_op_norm 𝕜 ι (λ (i : ι), 𝕜) G _ _ _ _ _ _ _ f, simp [pi_norm_le_iff zero_le_one, le_refl] end, .. continuous_multilinear_map.pi_field_equiv_aux 𝕜 ι G } end continuous_multilinear_map namespace continuous_linear_map lemma norm_comp_continuous_multilinear_map_le (g : G →L[𝕜] G') (f : continuous_multilinear_map 𝕜 E G) : ∥g.comp_continuous_multilinear_map f∥ ≤ ∥g∥ * ∥f∥ := continuous_multilinear_map.op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) $ λ m, calc ∥g (f m)∥ ≤ ∥g∥ * (∥f∥ * ∏ i, ∥m i∥) : g.le_op_norm_of_le $ f.le_op_norm _ ... = _ : (mul_assoc _ _ _).symm /-- `continuous_linear_map.comp_continuous_multilinear_map` as a bundled continuous bilinear map. -/ def comp_continuous_multilinear_mapL : (G →L[𝕜] G') →L[𝕜] continuous_multilinear_map 𝕜 E G →L[𝕜] continuous_multilinear_map 𝕜 E G' := linear_map.mk_continuous₂ (linear_map.mk₂ 𝕜 comp_continuous_multilinear_map (λ f₁ f₂ g, rfl) (λ c f g, rfl) (λ f g₁ g₂, by { ext1, apply f.map_add }) (λ c f g, by { ext1, simp })) 1 $ λ f g, by { rw one_mul, exact f.norm_comp_continuous_multilinear_map_le g } /-- Flip arguments in `f : G →L[𝕜] continuous_multilinear_map 𝕜 E G'` to get `continuous_multilinear_map 𝕜 E (G →L[𝕜] G')` -/ def flip_multilinear (f : G →L[𝕜] continuous_multilinear_map 𝕜 E G') : continuous_multilinear_map 𝕜 E (G →L[𝕜] G') := multilinear_map.mk_continuous { to_fun := λ m, linear_map.mk_continuous { to_fun := λ x, f x m, map_add' := λ x y, by simp only [map_add, continuous_multilinear_map.add_apply], map_smul' := λ c x, by simp only [continuous_multilinear_map.smul_apply, map_smul]} (∥f∥ * ∏ i, ∥m i∥) $ λ x, by { rw mul_right_comm, exact (f x).le_of_op_norm_le _ (f.le_op_norm x) }, map_add' := λ m i x y, by { ext1, simp only [add_apply, continuous_multilinear_map.map_add, linear_map.coe_mk, linear_map.mk_continuous_apply]}, map_smul' := λ m i c x, by { ext1, simp only [coe_smul', continuous_multilinear_map.map_smul, linear_map.coe_mk, linear_map.mk_continuous_apply, pi.smul_apply]} } ∥f∥ $ λ m, linear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg f) (prod_nonneg $ λ i hi, norm_nonneg (m i))) _ end continuous_linear_map open continuous_multilinear_map namespace multilinear_map /-- Given a map `f : G →ₗ[𝕜] multilinear_map 𝕜 E G'` and an estimate `H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥`, construct a continuous linear map from `G` to `continuous_multilinear_map 𝕜 E G'`. In order to lift, e.g., a map `f : (multilinear_map 𝕜 E G) →ₗ[𝕜] multilinear_map 𝕜 E' G'` to a map `(continuous_multilinear_map 𝕜 E G) →L[𝕜] continuous_multilinear_map 𝕜 E' G'`, one can apply this construction to `f.comp continuous_multilinear_map.to_multilinear_map_linear` which is a linear map from `continuous_multilinear_map 𝕜 E G` to `multilinear_map 𝕜 E' G'`. -/ def mk_continuous_linear (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ) (H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥) : G →L[𝕜] continuous_multilinear_map 𝕜 E G' := linear_map.mk_continuous { to_fun := λ x, (f x).mk_continuous (C * ∥x∥) $ H x, map_add' := λ x y, by { ext1, simp }, map_smul' := λ c x, by { ext1, simp } } (max C 0) $ λ x, ((f x).mk_continuous_norm_le' _).trans_eq $ by rw [max_mul_of_nonneg _ _ (norm_nonneg x), zero_mul] lemma mk_continuous_linear_norm_le' (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') (C : ℝ) (H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥) : ∥mk_continuous_linear f C H∥ ≤ max C 0 := begin dunfold mk_continuous_linear, exact linear_map.mk_continuous_norm_le _ (le_max_right _ _) _ end lemma mk_continuous_linear_norm_le (f : G →ₗ[𝕜] multilinear_map 𝕜 E G') {C : ℝ} (hC : 0 ≤ C) (H : ∀ x m, ∥f x m∥ ≤ C * ∥x∥ * ∏ i, ∥m i∥) : ∥mk_continuous_linear f C H∥ ≤ C := (mk_continuous_linear_norm_le' f C H).trans_eq (max_eq_left hC) /-- Given a map `f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)` and an estimate `H : ∀ m m', ∥f m m'∥ ≤ C * ∏ i, ∥m i∥ * ∏ i, ∥m' i∥`, upgrade all `multilinear_map`s in the type to `continuous_multilinear_map`s. -/ def mk_continuous_multilinear (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ) (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) : continuous_multilinear_map 𝕜 E (continuous_multilinear_map 𝕜 E' G) := mk_continuous { to_fun := λ m, mk_continuous (f m) (C * ∏ i, ∥m i∥) $ H m, map_add' := λ m i x y, by { ext1, simp }, map_smul' := λ m i c x, by { ext1, simp } } (max C 0) $ λ m, ((f m).mk_continuous_norm_le' _).trans_eq $ by { rw [max_mul_of_nonneg, zero_mul], exact prod_nonneg (λ _ _, norm_nonneg _) } @[simp] lemma mk_continuous_multilinear_apply (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) {C : ℝ} (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) (m : Π i, E i) : ⇑(mk_continuous_multilinear f C H m) = f m := rfl lemma mk_continuous_multilinear_norm_le' (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) (C : ℝ) (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) : ∥mk_continuous_multilinear f C H∥ ≤ max C 0 := begin dunfold mk_continuous_multilinear, exact mk_continuous_norm_le _ (le_max_right _ _) _ end lemma mk_continuous_multilinear_norm_le (f : multilinear_map 𝕜 E (multilinear_map 𝕜 E' G)) {C : ℝ} (hC : 0 ≤ C) (H : ∀ m₁ m₂, ∥f m₁ m₂∥ ≤ C * (∏ i, ∥m₁ i∥) * ∏ i, ∥m₂ i∥) : ∥mk_continuous_multilinear f C H∥ ≤ C := (mk_continuous_multilinear_norm_le' f C H).trans_eq (max_eq_left hC) end multilinear_map namespace continuous_multilinear_map lemma norm_comp_continuous_linear_le (g : continuous_multilinear_map 𝕜 E₁ G) (f : Π i, E i →L[𝕜] E₁ i) : ∥g.comp_continuous_linear_map f∥ ≤ ∥g∥ * ∏ i, ∥f i∥ := op_norm_le_bound _ (mul_nonneg (norm_nonneg _) $ prod_nonneg $ λ i hi, norm_nonneg _) $ λ m, calc ∥g (λ i, f i (m i))∥ ≤ ∥g∥ * ∏ i, ∥f i (m i)∥ : g.le_op_norm _ ... ≤ ∥g∥ * ∏ i, (∥f i∥ * ∥m i∥) : mul_le_mul_of_nonneg_left (prod_le_prod (λ _ _, norm_nonneg _) (λ i hi, (f i).le_op_norm (m i))) (norm_nonneg g) ... = (∥g∥ * ∏ i, ∥f i∥) * ∏ i, ∥m i∥ : by rw [prod_mul_distrib, mul_assoc] /-- `continuous_multilinear_map.comp_continuous_linear_map` as a bundled continuous linear map. This implementation fixes `f : Π i, E i →L[𝕜] E₁ i`. TODO: Actually, the map is multilinear in `f` but an attempt to formalize this failed because of issues with class instances. -/ def comp_continuous_linear_mapL (f : Π i, E i →L[𝕜] E₁ i) : continuous_multilinear_map 𝕜 E₁ G →L[𝕜] continuous_multilinear_map 𝕜 E G := linear_map.mk_continuous { to_fun := λ g, g.comp_continuous_linear_map f, map_add' := λ g₁ g₂, rfl, map_smul' := λ c g, rfl } (∏ i, ∥f i∥) $ λ g, (norm_comp_continuous_linear_le _ _).trans_eq (mul_comm _ _) @[simp] lemma comp_continuous_linear_mapL_apply (g : continuous_multilinear_map 𝕜 E₁ G) (f : Π i, E i →L[𝕜] E₁ i) : comp_continuous_linear_mapL f g = g.comp_continuous_linear_map f := rfl lemma norm_comp_continuous_linear_mapL_le (f : Π i, E i →L[𝕜] E₁ i) : ∥@comp_continuous_linear_mapL 𝕜 ι E E₁ G _ _ _ _ _ _ _ _ _ f∥ ≤ (∏ i, ∥f i∥) := linear_map.mk_continuous_norm_le _ (prod_nonneg $ λ i _, norm_nonneg _) _ end continuous_multilinear_map section currying /-! ### Currying We associate to a continuous multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two curried functions, named `f.curry_left` (which is a continuous linear map on `E 0` taking values in continuous multilinear maps in `n` variables) and `f.curry_right` (which is a continuous multilinear map in `n` variables taking values in continuous linear maps on `E (last n)`). The inverse operations are called `uncurry_left` and `uncurry_right`. We also register continuous linear equiv versions of these correspondences, in `continuous_multilinear_curry_left_equiv` and `continuous_multilinear_curry_right_equiv`. -/ open fin function lemma continuous_linear_map.norm_map_tail_le (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) : ∥f (m 0) (tail m)∥ ≤ ∥f∥ * ∏ i, ∥m i∥ := calc ∥f (m 0) (tail m)∥ ≤ ∥f (m 0)∥ * ∏ i, ∥(tail m) i∥ : (f (m 0)).le_op_norm _ ... ≤ (∥f∥ * ∥m 0∥) * ∏ i, ∥(tail m) i∥ : mul_le_mul_of_nonneg_right (f.le_op_norm _) (prod_nonneg (λi hi, norm_nonneg _)) ... = ∥f∥ * (∥m 0∥ * ∏ i, ∥(tail m) i∥) : by ring ... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_succ, refl } lemma continuous_multilinear_map.norm_map_init_le (f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) (m : Πi, Ei i) : ∥f (init m) (m (last n))∥ ≤ ∥f∥ * ∏ i, ∥m i∥ := calc ∥f (init m) (m (last n))∥ ≤ ∥f (init m)∥ * ∥m (last n)∥ : (f (init m)).le_op_norm _ ... ≤ (∥f∥ * (∏ i, ∥(init m) i∥)) * ∥m (last n)∥ : mul_le_mul_of_nonneg_right (f.le_op_norm _) (norm_nonneg _) ... = ∥f∥ * ((∏ i, ∥(init m) i∥) * ∥m (last n)∥) : mul_assoc _ _ _ ... = ∥f∥ * ∏ i, ∥m i∥ : by { rw prod_univ_cast_succ, refl } lemma continuous_multilinear_map.norm_map_cons_le (f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) : ∥f (cons x m)∥ ≤ ∥f∥ * ∥x∥ * ∏ i, ∥m i∥ := calc ∥f (cons x m)∥ ≤ ∥f∥ * ∏ i, ∥cons x m i∥ : f.le_op_norm _ ... = (∥f∥ * ∥x∥) * ∏ i, ∥m i∥ : by { rw prod_univ_succ, simp [mul_assoc] } lemma continuous_multilinear_map.norm_map_snoc_le (f : continuous_multilinear_map 𝕜 Ei G) (m : Π(i : fin n), Ei i.cast_succ) (x : Ei (last n)) : ∥f (snoc m x)∥ ≤ ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ := calc ∥f (snoc m x)∥ ≤ ∥f∥ * ∏ i, ∥snoc m x i∥ : f.le_op_norm _ ... = ∥f∥ * (∏ i, ∥m i∥) * ∥x∥ : by { rw prod_univ_cast_succ, simp [mul_assoc] } /-! #### Left currying -/ /-- Given a continuous linear map `f` from `E 0` to continuous multilinear maps on `n` variables, construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (m 0) (tail m)`-/ def continuous_linear_map.uncurry_left (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) : continuous_multilinear_map 𝕜 Ei G := (@linear_map.uncurry_left 𝕜 n Ei G _ _ _ _ _ (continuous_multilinear_map.to_multilinear_map_linear.comp f.to_linear_map)).mk_continuous (∥f∥) (λm, continuous_linear_map.norm_map_tail_le f m) @[simp] lemma continuous_linear_map.uncurry_left_apply (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) (m : Πi, Ei i) : f.uncurry_left m = f (m 0) (tail m) := rfl /-- Given a continuous multilinear map `f` in `n+1` variables, split the first variable to obtain a continuous linear map into continuous multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/ def continuous_multilinear_map.curry_left (f : continuous_multilinear_map 𝕜 Ei G) : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G) := linear_map.mk_continuous { -- define a linear map into `n` continuous multilinear maps from an `n+1` continuous multilinear -- map to_fun := λx, (f.to_multilinear_map.curry_left x).mk_continuous (∥f∥ * ∥x∥) (f.norm_map_cons_le x), map_add' := λx y, by { ext m, exact f.cons_add m x y }, map_smul' := λc x, by { ext m, exact f.cons_smul m c x } } -- then register its continuity thanks to its boundedness properties. (∥f∥) (λx, multilinear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _) @[simp] lemma continuous_multilinear_map.curry_left_apply (f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (m : Π(i : fin n), Ei i.succ) : f.curry_left x m = f (cons x m) := rfl @[simp] lemma continuous_linear_map.curry_uncurry_left (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) : f.uncurry_left.curry_left = f := begin ext m x, simp only [tail_cons, continuous_linear_map.uncurry_left_apply, continuous_multilinear_map.curry_left_apply], rw cons_zero end @[simp] lemma continuous_multilinear_map.uncurry_curry_left (f : continuous_multilinear_map 𝕜 Ei G) : f.curry_left.uncurry_left = f := continuous_multilinear_map.to_multilinear_map_inj $ f.to_multilinear_map.uncurry_curry_left variables (𝕜 Ei G) /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), E i` is canonically isomorphic to the space of continuous linear maps from `E 0` to the space of continuous multilinear maps on `Π(i : fin n), E i.succ `, by separating the first variable. We register this isomorphism in `continuous_multilinear_curry_left_equiv 𝕜 E E₂`. The algebraic version (without topology) is given in `multilinear_curry_left_equiv 𝕜 E E₂`. The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_left_equiv : (Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) ≃ₗᵢ[𝕜] (continuous_multilinear_map 𝕜 Ei G) := linear_isometry_equiv.of_bounds { to_fun := continuous_linear_map.uncurry_left, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, refl }, inv_fun := continuous_multilinear_map.curry_left, left_inv := continuous_linear_map.curry_uncurry_left, right_inv := continuous_multilinear_map.uncurry_curry_left } (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) (λ f, linear_map.mk_continuous_norm_le _ (norm_nonneg f) _) variables {𝕜 Ei G} @[simp] lemma continuous_multilinear_curry_left_equiv_apply (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.succ) G)) (v : Π i, Ei i) : continuous_multilinear_curry_left_equiv 𝕜 Ei G f v = f (v 0) (tail v) := rfl @[simp] lemma continuous_multilinear_curry_left_equiv_symm_apply (f : continuous_multilinear_map 𝕜 Ei G) (x : Ei 0) (v : Π i : fin n, Ei i.succ) : (continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm f x v = f (cons x v) := rfl @[simp] lemma continuous_multilinear_map.curry_left_norm (f : continuous_multilinear_map 𝕜 Ei G) : ∥f.curry_left∥ = ∥f∥ := (continuous_multilinear_curry_left_equiv 𝕜 Ei G).symm.norm_map f @[simp] lemma continuous_linear_map.uncurry_left_norm (f : Ei 0 →L[𝕜] (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.succ) G)) : ∥f.uncurry_left∥ = ∥f∥ := (continuous_multilinear_curry_left_equiv 𝕜 Ei G).norm_map f /-! #### Right currying -/ /-- Given a continuous linear map `f` from continuous multilinear maps on `n` variables to continuous linear maps on `E 0`, construct the corresponding continuous multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`. -/ def continuous_multilinear_map.uncurry_right (f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) : continuous_multilinear_map 𝕜 Ei G := let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →ₗ[𝕜] G) := { to_fun := λ m, (f m).to_linear_map, map_add' := λ m i x y, by { simp, refl }, map_smul' := λ m i c x, by { simp, refl } } in (@multilinear_map.uncurry_right 𝕜 n Ei G _ _ _ _ _ f').mk_continuous (∥f∥) (λm, f.norm_map_init_le m) @[simp] lemma continuous_multilinear_map.uncurry_right_apply (f : continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) (m : Πi, Ei i) : f.uncurry_right m = f (init m) (m (last n)) := rfl /-- Given a continuous multilinear map `f` in `n+1` variables, split the last variable to obtain a continuous multilinear map in `n` variables into continuous linear maps, given by `m ↦ (x ↦ f (snoc m x))`. -/ def continuous_multilinear_map.curry_right (f : continuous_multilinear_map 𝕜 Ei G) : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G) := let f' : multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G) := { to_fun := λm, (f.to_multilinear_map.curry_right m).mk_continuous (∥f∥ * ∏ i, ∥m i∥) $ λx, f.norm_map_snoc_le m x, map_add' := λ m i x y, by { simp, refl }, map_smul' := λ m i c x, by { simp, refl } } in f'.mk_continuous (∥f∥) (λm, linear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (prod_nonneg (λj hj, norm_nonneg _))) _) @[simp] lemma continuous_multilinear_map.curry_right_apply (f : continuous_multilinear_map 𝕜 Ei G) (m : Π i : fin n, Ei i.cast_succ) (x : Ei (last n)) : f.curry_right m x = f (snoc m x) := rfl @[simp] lemma continuous_multilinear_map.curry_uncurry_right (f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) : f.uncurry_right.curry_right = f := begin ext m x, simp only [snoc_last, continuous_multilinear_map.curry_right_apply, continuous_multilinear_map.uncurry_right_apply], rw init_snoc end @[simp] lemma continuous_multilinear_map.uncurry_curry_right (f : continuous_multilinear_map 𝕜 Ei G) : f.curry_right.uncurry_right = f := by { ext m, simp } variables (𝕜 Ei G) /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), Ei i` is canonically isomorphic to the space of continuous multilinear maps on `Π(i : fin n), Ei i.cast_succ` with values in the space of continuous linear maps on `Ei (last n)`, by separating the last variable. We register this isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv 𝕜 Ei G`. The algebraic version (without topology) is given in `multilinear_curry_right_equiv 𝕜 Ei G`. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_right_equiv : (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) ≃ₗᵢ[𝕜] (continuous_multilinear_map 𝕜 Ei G) := linear_isometry_equiv.of_bounds { to_fun := continuous_multilinear_map.uncurry_right, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, refl }, inv_fun := continuous_multilinear_map.curry_right, left_inv := continuous_multilinear_map.curry_uncurry_right, right_inv := continuous_multilinear_map.uncurry_curry_right } (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) variables (n G') /-- The space of continuous multilinear maps on `Π(i : fin (n+1)), G` is canonically isomorphic to the space of continuous multilinear maps on `Π(i : fin n), G` with values in the space of continuous linear maps on `G`, by separating the last variable. We register this isomorphism as a continuous linear equiv in `continuous_multilinear_curry_right_equiv' 𝕜 n G G'`. For a version allowing dependent types, see `continuous_multilinear_curry_right_equiv`. When there are no dependent types, use the primed version as it helps Lean a lot for unification. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_right_equiv' : (G [×n]→L[𝕜] (G →L[𝕜] G')) ≃ₗᵢ[𝕜] (G [×n.succ]→L[𝕜] G') := continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin n.succ), G) G' variables {n 𝕜 G Ei G'} @[simp] lemma continuous_multilinear_curry_right_equiv_apply (f : (continuous_multilinear_map 𝕜 (λ(i : fin n), Ei i.cast_succ) (Ei (last n) →L[𝕜] G))) (v : Π i, Ei i) : (continuous_multilinear_curry_right_equiv 𝕜 Ei G) f v = f (init v) (v (last n)) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply (f : continuous_multilinear_map 𝕜 Ei G) (v : Π (i : fin n), Ei i.cast_succ) (x : Ei (last n)) : (continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm f v x = f (snoc v x) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_apply' (f : G [×n]→L[𝕜] (G →L[𝕜] G')) (v : Π (i : fin n.succ), G) : continuous_multilinear_curry_right_equiv' 𝕜 n G G' f v = f (init v) (v (last n)) := rfl @[simp] lemma continuous_multilinear_curry_right_equiv_symm_apply' (f : G [×n.succ]→L[𝕜] G') (v : Π (i : fin n), G) (x : G) : (continuous_multilinear_curry_right_equiv' 𝕜 n G G').symm f v x = f (snoc v x) := rfl @[simp] lemma continuous_multilinear_map.curry_right_norm (f : continuous_multilinear_map 𝕜 Ei G) : ∥f.curry_right∥ = ∥f∥ := (continuous_multilinear_curry_right_equiv 𝕜 Ei G).symm.norm_map f @[simp] lemma continuous_multilinear_map.uncurry_right_norm (f : continuous_multilinear_map 𝕜 (λ i : fin n, Ei i.cast_succ) (Ei (last n) →L[𝕜] G)) : ∥f.uncurry_right∥ = ∥f∥ := (continuous_multilinear_curry_right_equiv 𝕜 Ei G).norm_map f /-! #### Currying with `0` variables The space of multilinear maps with `0` variables is trivial: such a multilinear map is just an arbitrary constant (note that multilinear maps in `0` variables need not map `0` to `0`!). Therefore, the space of continuous multilinear maps on `(fin 0) → G` with values in `E₂` is isomorphic (and even isometric) to `E₂`. As this is the zeroth step in the construction of iterated derivatives, we register this isomorphism. -/ section local attribute [instance] unique.subsingleton variables {𝕜 G G'} /-- Associating to a continuous multilinear map in `0` variables the unique value it takes. -/ def continuous_multilinear_map.uncurry0 (f : continuous_multilinear_map 𝕜 (λ (i : fin 0), G) G') : G' := f 0 variables (𝕜 G) /-- Associating to an element `x` of a vector space `E₂` the continuous multilinear map in `0` variables taking the (unique) value `x` -/ def continuous_multilinear_map.curry0 (x : G') : G [×0]→L[𝕜] G' := { to_fun := λm, x, map_add' := λ m i, fin.elim0 i, map_smul' := λ m i, fin.elim0 i, cont := continuous_const } variable {G} @[simp] lemma continuous_multilinear_map.curry0_apply (x : G') (m : (fin 0) → G) : continuous_multilinear_map.curry0 𝕜 G x m = x := rfl variable {𝕜} @[simp] lemma continuous_multilinear_map.uncurry0_apply (f : G [×0]→L[𝕜] G') : f.uncurry0 = f 0 := rfl @[simp] lemma continuous_multilinear_map.apply_zero_curry0 (f : G [×0]→L[𝕜] G') {x : fin 0 → G} : continuous_multilinear_map.curry0 𝕜 G (f x) = f := by { ext m, simp [(subsingleton.elim _ _ : x = m)] } lemma continuous_multilinear_map.uncurry0_curry0 (f : G [×0]→L[𝕜] G') : continuous_multilinear_map.curry0 𝕜 G (f.uncurry0) = f := by simp variables (𝕜 G) @[simp] lemma continuous_multilinear_map.curry0_uncurry0 (x : G') : (continuous_multilinear_map.curry0 𝕜 G x).uncurry0 = x := rfl @[simp] lemma continuous_multilinear_map.curry0_norm (x : G') : ∥continuous_multilinear_map.curry0 𝕜 G x∥ = ∥x∥ := begin apply le_antisymm, { exact continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm, by simp) }, { simpa using (continuous_multilinear_map.curry0 𝕜 G x).le_op_norm 0 } end variables {𝕜 G} @[simp] lemma continuous_multilinear_map.fin0_apply_norm (f : G [×0]→L[𝕜] G') {x : fin 0 → G} : ∥f x∥ = ∥f∥ := begin have : x = 0 := subsingleton.elim _ _, subst this, refine le_antisymm (by simpa using f.le_op_norm 0) _, have : ∥continuous_multilinear_map.curry0 𝕜 G (f.uncurry0)∥ ≤ ∥f.uncurry0∥ := continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λm, by simp [-continuous_multilinear_map.apply_zero_curry0]), simpa end lemma continuous_multilinear_map.uncurry0_norm (f : G [×0]→L[𝕜] G') : ∥f.uncurry0∥ = ∥f∥ := by simp variables (𝕜 G G') /-- The continuous linear isomorphism between elements of a normed space, and continuous multilinear maps in `0` variables with values in this normed space. The direct and inverse maps are `uncurry0` and `curry0`. Use these unless you need the full framework of linear isometric equivs. -/ def continuous_multilinear_curry_fin0 : (G [×0]→L[𝕜] G') ≃ₗᵢ[𝕜] G' := { to_fun := λf, continuous_multilinear_map.uncurry0 f, inv_fun := λf, continuous_multilinear_map.curry0 𝕜 G f, map_add' := λf g, rfl, map_smul' := λc f, rfl, left_inv := continuous_multilinear_map.uncurry0_curry0, right_inv := continuous_multilinear_map.curry0_uncurry0 𝕜 G, norm_map' := continuous_multilinear_map.uncurry0_norm } variables {𝕜 G G'} @[simp] lemma continuous_multilinear_curry_fin0_apply (f : G [×0]→L[𝕜] G') : continuous_multilinear_curry_fin0 𝕜 G G' f = f 0 := rfl @[simp] lemma continuous_multilinear_curry_fin0_symm_apply (x : G') (v : (fin 0) → G) : (continuous_multilinear_curry_fin0 𝕜 G G').symm x v = x := rfl end /-! #### With 1 variable -/ variables (𝕜 G G') /-- Continuous multilinear maps from `G^1` to `G'` are isomorphic with continuous linear maps from `G` to `G'`. -/ def continuous_multilinear_curry_fin1 : (G [×1]→L[𝕜] G') ≃ₗᵢ[𝕜] (G →L[𝕜] G') := (continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin 1), G) G').symm.trans (continuous_multilinear_curry_fin0 𝕜 G (G →L[𝕜] G')) variables {𝕜 G G'} @[simp] lemma continuous_multilinear_curry_fin1_apply (f : G [×1]→L[𝕜] G') (x : G) : continuous_multilinear_curry_fin1 𝕜 G G' f x = f (fin.snoc 0 x) := rfl @[simp] lemma continuous_multilinear_curry_fin1_symm_apply (f : G →L[𝕜] G') (v : (fin 1) → G) : (continuous_multilinear_curry_fin1 𝕜 G G').symm f v = f (v 0) := rfl namespace continuous_multilinear_map variables (𝕜 G G') /-- An equivalence of the index set defines a linear isometric equivalence between the spaces of multilinear maps. -/ def dom_dom_congr (σ : ι ≃ ι') : continuous_multilinear_map 𝕜 (λ _ : ι, G) G' ≃ₗᵢ[𝕜] continuous_multilinear_map 𝕜 (λ _ : ι', G) G' := linear_isometry_equiv.of_bounds { to_fun := λ f, (multilinear_map.dom_dom_congr σ f.to_multilinear_map).mk_continuous ∥f∥ $ λ m, (f.le_op_norm (λ i, m (σ i))).trans_eq $ by rw [← σ.prod_comp], inv_fun := λ f, (multilinear_map.dom_dom_congr σ.symm f.to_multilinear_map).mk_continuous ∥f∥ $ λ m, (f.le_op_norm (λ i, m (σ.symm i))).trans_eq $ by rw [← σ.symm.prod_comp], left_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.symm_apply_apply], right_inv := λ f, ext $ λ m, congr_arg f $ by simp only [σ.apply_symm_apply], map_add' := λ f g, rfl, map_smul' := λ c f, rfl } (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) variables {𝕜 G G'} section variable [decidable_eq (ι ⊕ ι')] /-- A continuous multilinear map with variables indexed by `ι ⊕ ι'` defines a continuous multilinear map with variables indexed by `ι` taking values in the space of continuous multilinear maps with variables indexed by `ι'`. -/ def curry_sum (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G') : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') := multilinear_map.mk_continuous_multilinear (multilinear_map.curry_sum f.to_multilinear_map) (∥f∥) $ λ m m', by simpa [fintype.prod_sum_type, mul_assoc] using f.le_op_norm (sum.elim m m') @[simp] lemma curry_sum_apply (f : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G') (m : ι → G) (m' : ι' → G) : f.curry_sum m m' = f (sum.elim m m') := rfl /-- A continuous multilinear map with variables indexed by `ι` taking values in the space of continuous multilinear maps with variables indexed by `ι'` defines a continuous multilinear map with variables indexed by `ι ⊕ ι'`. -/ def uncurry_sum (f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G')) : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' := multilinear_map.mk_continuous (to_multilinear_map_linear.comp_multilinear_map f.to_multilinear_map).uncurry_sum (∥f∥) $ λ m, by simpa [fintype.prod_sum_type, mul_assoc] using (f (m ∘ sum.inl)).le_of_op_norm_le (m ∘ sum.inr) (f.le_op_norm _) @[simp] lemma uncurry_sum_apply (f : continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G')) (m : ι ⊕ ι' → G) : f.uncurry_sum m = f (m ∘ sum.inl) (m ∘ sum.inr) := rfl variables (𝕜 ι ι' G G') /-- Linear isometric equivalence between the space of continuous multilinear maps with variables indexed by `ι ⊕ ι'` and the space of continuous multilinear maps with variables indexed by `ι` taking values in the space of continuous multilinear maps with variables indexed by `ι'`. The forward and inverse functions are `continuous_multilinear_map.curry_sum` and `continuous_multilinear_map.uncurry_sum`. Use this definition only if you need some properties of `linear_isometry_equiv`. -/ def curry_sum_equiv : continuous_multilinear_map 𝕜 (λ x : ι ⊕ ι', G) G' ≃ₗᵢ[𝕜] continuous_multilinear_map 𝕜 (λ x : ι, G) (continuous_multilinear_map 𝕜 (λ x : ι', G) G') := linear_isometry_equiv.of_bounds { to_fun := curry_sum, inv_fun := uncurry_sum, map_add' := λ f g, by { ext, refl }, map_smul' := λ c f, by { ext, refl }, left_inv := λ f, by { ext m, exact congr_arg f (sum.elim_comp_inl_inr m) }, right_inv := λ f, by { ext m₁ m₂, change f _ _ = f _ _, rw [sum.elim_comp_inl, sum.elim_comp_inr] } } (λ f, multilinear_map.mk_continuous_multilinear_norm_le _ (norm_nonneg f) _) (λ f, multilinear_map.mk_continuous_norm_le _ (norm_nonneg f) _) end section variables (𝕜 G G') {k l : ℕ} {s : finset (fin n)} [decidable_pred (s : set (fin n))] /-- If `s : finset (fin n)` is a finite set of cardinality `k` and its complement has cardinality `l`, then the space of continuous multilinear maps `G [×n]→L[𝕜] G'` of `n` variables is isomorphic to the space of continuous multilinear maps `G [×k]→L[𝕜] G [×l]→L[𝕜] G'` of `k` variables taking values in the space of continuous multilinear maps of `l` variables. -/ def curry_fin_finset {k l n : ℕ} {s : finset (fin n)} [decidable_pred (s : set (fin n))] (hk : s.card = k) (hl : sᶜ.card = l) : (G [×n]→L[𝕜] G') ≃ₗᵢ[𝕜] (G [×k]→L[𝕜] G [×l]→L[𝕜] G') := (dom_dom_congr 𝕜 G G' (fin_sum_equiv_of_finset hk hl).symm).trans (curry_sum_equiv 𝕜 (fin k) (fin l) G G') variables {𝕜 G G'} @[simp] lemma curry_fin_finset_apply (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×n]→L[𝕜] G') (mk : fin k → G) (ml : fin l → G) : curry_fin_finset 𝕜 G G' hk hl f mk ml = f (λ i, sum.elim mk ml ((fin_sum_equiv_of_finset hk hl).symm i)) := rfl @[simp] lemma curry_fin_finset_symm_apply (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (m : fin n → G) : (curry_fin_finset 𝕜 G G' hk hl).symm f m = f (λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inl i)) (λ i, m $ fin_sum_equiv_of_finset hk hl (sum.inr i)) := rfl @[simp] lemma curry_fin_finset_symm_apply_piecewise_const (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x y : G) : (curry_fin_finset 𝕜 G G' hk hl).symm f (s.piecewise (λ _, x) (λ _, y)) = f (λ _, x) (λ _, y) := multilinear_map.curry_fin_finset_symm_apply_piecewise_const hk hl _ x y @[simp] lemma curry_fin_finset_symm_apply_const (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×k]→L[𝕜] G [×l]→L[𝕜] G') (x : G) : (curry_fin_finset 𝕜 G G' hk hl).symm f (λ _, x) = f (λ _, x) (λ _, x) := rfl @[simp] lemma curry_fin_finset_apply_const (hk : s.card = k) (hl : sᶜ.card = l) (f : G [×n]→L[𝕜] G') (x y : G) : curry_fin_finset 𝕜 G G' hk hl f (λ _, x) (λ _, y) = f (s.piecewise (λ _, x) (λ _, y)) := begin refine (curry_fin_finset_symm_apply_piecewise_const hk hl _ _ _).symm.trans _, -- `rw` fails rw linear_isometry_equiv.symm_apply_apply end end end continuous_multilinear_map end currying
04c046baba54fc5472d3604f9ea0712b2ab3005c
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/data/finsupp/basic.lean
8eadf22566b3ea49b3175bbc3259f0683aa9753d
[ "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
73,834
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, Scott Morrison -/ import algebra.group.pi import algebra.big_operators.order import algebra.module.basic import data.fintype.card import data.finset.preimage import data.multiset.antidiagonal /-! # Type of functions with finite support For any type `α` and a type `β` with zero, we define the type `finsupp α β` of finitely supported functions from `α` to `β`, i.e. the functions which are zero everywhere on `α` except on a finite set. We write this in infix notation as `α →₀ β`. Functions with finite support provide the basis for the following concrete instances: * `ℕ →₀ α`: Polynomials (where `α` is a ring) * `(σ →₀ ℕ) →₀ α`: Multivariate Polynomials (again `α` is a ring, and `σ` are variable names) * `α →₀ ℕ`: Multisets * `α →₀ ℤ`: Abelian groups freely generated by `α` * `β →₀ α`: Linear combinations over `β` where `α` is the scalar ring Most of the theory assumes that the range is a commutative monoid. This gives us the big sum operator as a powerful way to construct `finsupp` elements. A general piece of advice is to not use `α →₀ β` directly, as the type class setup might not be a good fit. Defining a copy and selecting the instances that are best suited for the application works better. ## Implementation notes This file is a `noncomputable theory` and uses classical logic throughout. ## Notation This file defines `α →₀ β` as notation for `finsupp α β`. -/ noncomputable theory open_locale classical big_operators open finset variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Type*} {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} /-- `finsupp α β`, denoted `α →₀ β`, is the type of functions `f : α → β` such that `f x = 0` for all but finitely many `x`. -/ structure finsupp (α : Type*) (β : Type*) [has_zero β] := (support : finset α) (to_fun : α → β) (mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0) infixr ` →₀ `:25 := finsupp namespace finsupp /-! ### Basic declarations about `finsupp` -/ section basic variable [has_zero β] instance : has_coe_to_fun (α →₀ β) := ⟨λ_, α → β, to_fun⟩ instance : has_zero (α →₀ β) := ⟨⟨∅, (λ_, 0), λ _, ⟨false.elim, λ H, H rfl⟩⟩⟩ @[simp] lemma zero_apply {a : α} : (0 : α →₀ β) a = 0 := rfl @[simp] lemma support_zero : (0 : α →₀ β).support = ∅ := rfl instance : inhabited (α →₀ β) := ⟨0⟩ @[simp] lemma mem_support_iff {f : α →₀ β} : ∀{a:α}, a ∈ f.support ↔ f a ≠ 0 := f.mem_support_to_fun lemma not_mem_support_iff {f : α →₀ β} {a} : a ∉ f.support ↔ f a = 0 := not_iff_comm.1 mem_support_iff.symm @[ext] lemma ext : ∀{f g : α →₀ β}, (∀a, f a = g a) → f = g | ⟨s, f, hf⟩ ⟨t, g, hg⟩ h := begin have : f = g, { funext a, exact h a }, subst this, have : s = t, { ext a, exact (hf a).trans (hg a).symm }, subst this end lemma ext_iff {f g : α →₀ β} : f = g ↔ (∀a:α, f a = g a) := ⟨by rintros rfl a; refl, ext⟩ @[simp] lemma support_eq_empty {f : α →₀ β} : f.support = ∅ ↔ f = 0 := ⟨assume h, ext $ assume a, by_contradiction $ λ H, (finset.ext_iff.1 h a).1 $ mem_support_iff.2 H, by rintro rfl; refl⟩ instance finsupp.decidable_eq [decidable_eq α] [decidable_eq β] : decidable_eq (α →₀ β) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀a∈f.support, f a = g a)) ⟨assume ⟨h₁, h₂⟩, ext $ assume a, if h : a ∈ f.support then h₂ a h else have hf : f a = 0, by rwa [mem_support_iff, not_not] at h, have hg : g a = 0, by rwa [h₁, mem_support_iff, not_not] at h, by rw [hf, hg], by rintro rfl; exact ⟨rfl, λ _ _, rfl⟩⟩ lemma finite_supp (f : α →₀ β) : set.finite {a | f a ≠ 0} := ⟨fintype.of_finset f.support (λ _, mem_support_iff)⟩ lemma support_subset_iff {s : set α} {f : α →₀ β} : ↑f.support ⊆ s ↔ (∀a∉s, f a = 0) := by simp only [set.subset_def, mem_coe, mem_support_iff]; exact forall_congr (assume a, not_imp_comm) /-- Given `fintype α`, `equiv_fun_on_fintype` is the `equiv` between `α →₀ β` and `α → β`. (All functions on a finite type are finitely supported.) -/ def equiv_fun_on_fintype [fintype α] : (α →₀ β) ≃ (α → β) := ⟨λf a, f a, λf, mk (finset.univ.filter $ λa, f a ≠ 0) f (by simp only [true_and, finset.mem_univ, iff_self, finset.mem_filter, finset.filter_congr_decidable, forall_true_iff]), begin intro f, ext a, refl end, begin intro f, ext a, refl end⟩ end basic /-! ### Declarations about `single` -/ section single variables [has_zero β] {a a' : α} {b : β} /-- `single a b` is the finitely supported function which has value `b` at `a` and zero otherwise. -/ def single (a : α) (b : β) : α →₀ β := ⟨if b = 0 then ∅ else {a}, λ a', if a = a' then b else 0, λ a', begin by_cases hb : b = 0; by_cases a = a'; simp only [hb, h, if_pos, if_false, mem_singleton], { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨λ _, hb, λ _, rfl⟩ }, { exact ⟨λ H _, h H.symm, λ H, (H rfl).elim⟩ } end⟩ lemma single_apply : (single a b : α →₀ β) a' = if a = a' then b else 0 := rfl @[simp] lemma single_eq_same : (single a b : α →₀ β) a = b := if_pos rfl @[simp] lemma single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ β) a' = 0 := if_neg h @[simp] lemma single_zero : (single a 0 : α →₀ β) = 0 := ext $ assume a', begin by_cases h : a = a', { rw [h, single_eq_same, zero_apply] }, { rw [single_eq_of_ne h, zero_apply] } end lemma support_single_ne_zero (hb : b ≠ 0) : (single a b).support = {a} := if_neg hb lemma support_single_subset : (single a b).support ⊆ {a} := show ite _ _ _ ⊆ _, by split_ifs; [exact empty_subset _, exact subset.refl _] lemma single_injective (a : α) : function.injective (single a : β → α →₀ β) := assume b₁ b₂ eq, have (single a b₁ : α →₀ β) a = (single a b₂ : α →₀ β) a, by rw eq, by rwa [single_eq_same, single_eq_same] at this lemma single_eq_single_iff (a₁ a₂ : α) (b₁ b₂ : β) : single a₁ b₁ = single a₂ b₂ ↔ ((a₁ = a₂ ∧ b₁ = b₂) ∨ (b₁ = 0 ∧ b₂ = 0)) := begin split, { assume eq, by_cases a₁ = a₂, { refine or.inl ⟨h, _⟩, rwa [h, (single_injective a₂).eq_iff] at eq }, { rw [ext_iff] at eq, have h₁ := eq a₁, have h₂ := eq a₂, simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (ne.symm h)] at h₁ h₂, exact or.inr ⟨h₁, h₂.symm⟩ } }, { rintros (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩), { refl }, { rw [single_zero, single_zero] } } end lemma single_left_inj (h : b ≠ 0) : single a b = single a' b ↔ a = a' := ⟨λ H, by simpa only [h, single_eq_single_iff, and_false, or_false, eq_self_iff_true, and_true] using H, λ H, by rw [H]⟩ lemma single_eq_zero : single a b = 0 ↔ b = 0 := ⟨λ h, by { rw ext_iff at h, simpa only [single_eq_same, zero_apply] using h a }, λ h, by rw [h, single_zero]⟩ lemma single_swap {α β : Type*} [has_zero β] (a₁ a₂ : α) (b : β) : (single a₁ b : α → β) a₂ = (single a₂ b : α → β) a₁ := by simp only [single_apply]; ac_refl lemma unique_single [unique α] (x : α →₀ β) : x = single (default α) (x (default α)) := by ext i; simp only [unique.eq_default i, single_eq_same] @[simp] lemma unique_single_eq_iff [unique α] {b' : β} : single a b = single a' b' ↔ b = b' := begin rw [single_eq_single_iff], split, { rintros (⟨_, rfl⟩ | ⟨rfl, rfl⟩); refl }, { intro h, left, exact ⟨subsingleton.elim _ _, h⟩ } end end single /-! ### Declarations about `on_finset` -/ section on_finset variables [has_zero β] /-- `on_finset s f hf` is the finsupp function representing `f` restricted to the finset `s`. The function needs to be `0` outside of `s`. Use this when the set needs to be filtered anyways, otherwise a better set representation is often available. -/ def on_finset (s : finset α) (f : α → β) (hf : ∀a, f a ≠ 0 → a ∈ s) : α →₀ β := ⟨s.filter (λa, f a ≠ 0), f, assume a, classical.by_cases (assume h : f a = 0, by rw mem_filter; exact ⟨and.right, λ H, (H h).elim⟩) (assume h : f a ≠ 0, by rw mem_filter; simp only [iff_true_intro h, hf a h, true_and])⟩ @[simp] lemma on_finset_apply {s : finset α} {f : α → β} {hf a} : (on_finset s f hf : α →₀ β) a = f a := rfl @[simp] lemma support_on_finset_subset {s : finset α} {f : α → β} {hf} : (on_finset s f hf).support ⊆ s := filter_subset _ lemma mem_support_on_finset {s : finset α} {f : α → β} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) {a : α} : a ∈ (finsupp.on_finset s f hf).support ↔ f a ≠ 0 := by simp [finsupp.mem_support_iff, finsupp.on_finset_apply] lemma support_on_finset {s : finset α} {f : α → β} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) : (finsupp.on_finset s f hf).support = s.filter (λ a, f a ≠ 0) := begin ext a, rw [mem_support_on_finset, finset.mem_filter], specialize hf a, finish end end on_finset /-! ### Declarations about `map_range` -/ section map_range variables [has_zero β₁] [has_zero β₂] /-- The composition of `f : β₁ → β₂` and `g : α →₀ β₁` is `map_range f hf g : α →₀ β₂`, well-defined when `f 0 = 0`. -/ def map_range (f : β₁ → β₂) (hf : f 0 = 0) (g : α →₀ β₁) : α →₀ β₂ := on_finset g.support (f ∘ g) $ assume a, by rw [mem_support_iff, not_imp_not]; exact λ H, (congr_arg f H).trans hf @[simp] lemma map_range_apply {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {a : α} : map_range f hf g a = f (g a) := rfl @[simp] lemma map_range_zero {f : β₁ → β₂} {hf : f 0 = 0} : map_range f hf (0 : α →₀ β₁) = 0 := ext $ λ a, by simp only [hf, zero_apply, map_range_apply] lemma support_map_range {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} : (map_range f hf g).support ⊆ g.support := support_on_finset_subset @[simp] lemma map_range_single {f : β₁ → β₂} {hf : f 0 = 0} {a : α} {b : β₁} : map_range f hf (single a b) = single a (f b) := ext $ λ a', show f (ite _ _ _) = ite _ _ _, by split_ifs; [refl, exact hf] end map_range /-! ### Declarations about `emb_domain` -/ section emb_domain variables [has_zero β] /-- Given `f : α₁ ↪ α₂` and `v : α₁ →₀ β`, `emb_domain f v : α₂ →₀ β` is the finitely supported function whose value at `f a : α₂` is `v a`. For a `b : α₂` outside the range of `f`, it is zero. -/ def emb_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : α₂ →₀ β := begin refine ⟨v.support.map f, λa₂, if h : a₂ ∈ v.support.map f then v (v.support.choose (λa₁, f a₁ = a₂) _) else 0, _⟩, { rcases finset.mem_map.1 h with ⟨a, ha, rfl⟩, exact exists_unique.intro a ⟨ha, rfl⟩ (assume b ⟨_, hb⟩, f.injective hb) }, { assume a₂, split_ifs, { simp only [h, true_iff, ne.def], rw [← not_mem_support_iff, not_not], apply finset.choose_mem }, { simp only [h, ne.def, ne_self_iff_false] } } end lemma support_emb_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : (emb_domain f v).support = v.support.map f := rfl lemma emb_domain_zero (f : α₁ ↪ α₂) : (emb_domain f 0 : α₂ →₀ β) = 0 := rfl lemma emb_domain_apply (f : α₁ ↪ α₂) (v : α₁ →₀ β) (a : α₁) : emb_domain f v (f a) = v a := begin change dite _ _ _ = _, split_ifs; rw [finset.mem_map' f] at h, { refine congr_arg (v : α₁ → β) (f.inj' _), exact finset.choose_property (λa₁, f a₁ = f a) _ _ }, { exact (not_mem_support_iff.1 h).symm } end lemma emb_domain_notin_range (f : α₁ ↪ α₂) (v : α₁ →₀ β) (a : α₂) (h : a ∉ set.range f) : emb_domain f v a = 0 := begin refine dif_neg (mt (assume h, _) h), rcases finset.mem_map.1 h with ⟨a, h, rfl⟩, exact set.mem_range_self a end lemma emb_domain_inj {f : α₁ ↪ α₂} {l₁ l₂ : α₁ →₀ β} : emb_domain f l₁ = emb_domain f l₂ ↔ l₁ = l₂ := ⟨λ h, ext $ λ a, by simpa only [emb_domain_apply] using ext_iff.1 h (f a), λ h, by rw h⟩ lemma emb_domain_map_range {β₁ β₂ : Type*} [has_zero β₁] [has_zero β₂] (f : α₁ ↪ α₂) (g : β₁ → β₂) (p : α₁ →₀ β₁) (hg : g 0 = 0) : emb_domain f (map_range g hg p) = map_range g hg (emb_domain f p) := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a', rfl⟩, rw [map_range_apply, emb_domain_apply, emb_domain_apply, map_range_apply] }, { rw [map_range_apply, emb_domain_notin_range, emb_domain_notin_range, ← hg]; assumption } end lemma single_of_emb_domain_single (l : α₁ →₀ β) (f : α₁ ↪ α₂) (a : α₂) (b : β) (hb : b ≠ 0) (h : l.emb_domain f = single a b) : ∃ x, l = single x b ∧ f x = a := begin have h_map_support : finset.map f (l.support) = {a}, by rw [←support_emb_domain, h, support_single_ne_zero hb]; refl, have ha : a ∈ finset.map f (l.support), by simp only [h_map_support, finset.mem_singleton], rcases finset.mem_map.1 ha with ⟨c, hc₁, hc₂⟩, use c, split, { ext d, rw [← emb_domain_apply f l, h], by_cases h_cases : c = d, { simp only [eq.symm h_cases, hc₂, single_eq_same] }, { rw [single_apply, single_apply, if_neg, if_neg h_cases], by_contra hfd, exact h_cases (f.injective (hc₂.trans hfd)) } }, { exact hc₂ } end end emb_domain /-! ### Declarations about `zip_with` -/ section zip_with variables [has_zero β] [has_zero β₁] [has_zero β₂] /-- `zip_with f hf g₁ g₂` is the finitely supported function satisfying `zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, and it is well-defined when `f 0 0 = 0`. -/ def zip_with (f : β₁ → β₂ → β) (hf : f 0 0 = 0) (g₁ : α →₀ β₁) (g₂ : α →₀ β₂) : (α →₀ β) := on_finset (g₁.support ∪ g₂.support) (λa, f (g₁ a) (g₂ a)) $ λ a H, begin simp only [mem_union, mem_support_iff, ne], rw [← not_and_distrib], rintro ⟨h₁, h₂⟩, rw [h₁, h₂] at H, exact H hf end @[simp] lemma zip_with_apply {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} {a : α} : zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a) := rfl lemma support_zip_with {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := support_on_finset_subset end zip_with /-! ### Declarations about `erase` -/ section erase /-- `erase a f` is the finitely supported function equal to `f` except at `a` where it is equal to `0`. -/ def erase [has_zero β] (a : α) (f : α →₀ β) : α →₀ β := ⟨f.support.erase a, (λa', if a' = a then 0 else f a'), assume a', by rw [mem_erase, mem_support_iff]; split_ifs; [exact ⟨λ H _, H.1 h, λ H, (H rfl).elim⟩, exact and_iff_right h]⟩ @[simp] lemma support_erase [has_zero β] {a : α} {f : α →₀ β} : (f.erase a).support = f.support.erase a := rfl @[simp] lemma erase_same [has_zero β] {a : α} {f : α →₀ β} : (f.erase a) a = 0 := if_pos rfl @[simp] lemma erase_ne [has_zero β] {a a' : α} {f : α →₀ β} (h : a' ≠ a) : (f.erase a) a' = f a' := if_neg h @[simp] lemma erase_single [has_zero β] {a : α} {b : β} : (erase a (single a b)) = 0 := begin ext s, by_cases hs : s = a, { rw [hs, erase_same], refl }, { rw [erase_ne hs], exact single_eq_of_ne (ne.symm hs) } end lemma erase_single_ne [has_zero β] {a a' : α} {b : β} (h : a ≠ a') : (erase a (single a' b)) = single a' b := begin ext s, by_cases hs : s = a, { rw [hs, erase_same, single_eq_of_ne (h.symm)] }, { rw [erase_ne hs] } end end erase /-! ### Declarations about `sum` and `prod` In most of this section, the domain `β` is assumed to be an `add_monoid`. -/ -- [to_additive sum] for finsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/ def sum [has_zero β] [add_comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := ∑ a in f.support, g a (f a) /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive] def prod [has_zero β] [comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := ∏ a in f.support, g a (f a) @[to_additive] lemma prod_of_support_subset [has_zero β] [comm_monoid γ] (f : α →₀ β) {s : finset α} (hs : f.support ⊆ s) (g : α → β → γ) (h : ∀ i ∈ s, g i 0 = 1) : f.prod g = ∏ x in s, g x (f x) := finset.prod_subset hs $ λ x hxs hx, h x hxs ▸ congr_arg (g x) $ not_mem_support_iff.1 hx @[to_additive] lemma prod_fintype [fintype α] [has_zero β] [comm_monoid γ] (f : α →₀ β) (g : α → β → γ) (h : ∀ i, g i 0 = 1) : f.prod g = ∏ i, g i (f i) := f.prod_of_support_subset (subset_univ _) g (λ x _, h x) @[simp, to_additive] lemma prod_single_index [has_zero β] [comm_monoid γ] {a : α} {b : β} {h : α → β → γ} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := calc (single a b).prod h = ∏ x in {a}, h x (single a b x) : prod_of_support_subset _ support_single_subset h $ λ x hx, (mem_singleton.1 hx).symm ▸ h_zero ... = h a b : by simp @[to_additive] lemma prod_map_range_index [has_zero β₁] [has_zero β₂] [comm_monoid γ] {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {h : α → β₂ → γ} (h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) := finset.prod_subset support_map_range $ λ _ _ H, by rw [not_mem_support_iff.1 H, h0] @[to_additive] lemma prod_zero_index [add_comm_monoid β] [comm_monoid γ] {h : α → β → γ} : (0 : α →₀ β).prod h = 1 := rfl @[to_additive] lemma prod_comm {α' : Type*} [has_zero β] {β' : Type*} [has_zero β'] (f : α →₀ β) (g : α' →₀ β') [comm_monoid γ] (h : α → β → α' → β' → γ) : f.prod (λ x v, g.prod (λ x' v', h x v x' v')) = g.prod (λ x' v', f.prod (λ x v, h x v x' v')) := begin dsimp [finsupp.prod], rw finset.prod_comm, end @[simp, to_additive] lemma prod_ite_eq [has_zero β] [comm_monoid γ] (f : α →₀ β) (a : α) (b : α → β → γ) : f.prod (λ x v, ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq, } /-- A restatement of `prod_ite_eq` with the equality test reversed. -/ @[simp, to_additive "A restatement of `sum_ite_eq` with the equality test reversed."] lemma prod_ite_eq' [has_zero β] [comm_monoid γ] (f : α →₀ β) (a : α) (b : α → β → γ) : f.prod (λ x v, ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq', } @[simp] lemma prod_pow [fintype α] [comm_monoid γ] (f : α →₀ ℕ) (g : α → γ) : f.prod (λ a b, g a ^ b) = ∏ a, g a ^ (f a) := begin apply prod_subset (finset.subset_univ _), intros a _ ha, simp only [finsupp.not_mem_support_iff.mp ha, pow_zero] end /-- If `g` maps a second argument of 0 to 0, summing it over the result of `on_finset` is the same as summing it over the original `finset`. -/ lemma on_finset_sum [has_zero β] [add_comm_monoid γ] {s : finset α} {f : α → β} {g : α → β → γ} (hf : ∀a, f a ≠ 0 → a ∈ s) (hg : ∀ a, g a 0 = 0) : (on_finset s f hf).sum g = ∑ a in s, g a (f a) := begin refine finset.sum_subset support_on_finset_subset _, intros x hx hxs, rw not_mem_support_iff.1 hxs, exact hg _ end section add_monoid variables [add_monoid β] instance : has_add (α →₀ β) := ⟨zip_with (+) (add_zero 0)⟩ @[simp] lemma add_apply {g₁ g₂ : α →₀ β} {a : α} : (g₁ + g₂) a = g₁ a + g₂ a := rfl lemma support_add {g₁ g₂ : α →₀ β} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with lemma support_add_eq {g₁ g₂ : α →₀ β} (h : disjoint g₁.support g₂.support) : (g₁ + g₂).support = g₁.support ∪ g₂.support := le_antisymm support_zip_with $ assume a ha, (finset.mem_union.1 ha).elim (assume ha, have a ∉ g₂.support, from disjoint_left.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, add_zero]) (assume ha, have a ∉ g₁.support, from disjoint_right.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, zero_add]) @[simp] lemma single_add {a : α} {b₁ b₂ : β} : single a (b₁ + b₂) = single a b₁ + single a b₂ := ext $ assume a', begin by_cases h : a = a', { rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] } end instance : add_monoid (α →₀ β) := { add_monoid . zero := 0, add := (+), add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _, zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _, add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ } /-- `finsupp.single` as an `add_monoid_hom`. -/ @[simps] def single_add_hom (a : α) : β →+ α →₀ β := ⟨single a, single_zero, λ _ _, single_add⟩ /-- Evaluation of a function `f : α →₀ β` at a point as an additive monoid homomorphism. -/ def eval_add_hom (a : α) : (α →₀ β) →+ β := ⟨λ g, g a, zero_apply, λ _ _, add_apply⟩ @[simp] lemma eval_add_hom_apply (a : α) (g : α →₀ β) : eval_add_hom a g = g a := rfl lemma single_add_erase {a : α} {f : α →₀ β} : single a (f a) + f.erase a = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, add_zero] else by simp only [add_apply, single_eq_of_ne h, zero_add, erase_ne (ne.symm h)] lemma erase_add_single {a : α} {f : α →₀ β} : f.erase a + single a (f a) = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, zero_add] else by simp only [add_apply, single_eq_of_ne h, add_zero, erase_ne (ne.symm h)] @[simp] lemma erase_add (a : α) (f f' : α →₀ β) : erase a (f + f') = erase a f + erase a f' := begin ext s, by_cases hs : s = a, { rw [hs, add_apply, erase_same, erase_same, erase_same, add_zero] }, rw [add_apply, erase_ne hs, erase_ne hs, erase_ne hs, add_apply], end @[elab_as_eliminator] protected theorem induction {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (single a b + f)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma induction₂ {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (f + single a b)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end @[simp] lemma add_closure_Union_range_single : add_submonoid.closure (⋃ a : α, set.range (single a : β → α →₀ β)) = ⊤ := top_unique $ λ x hx, finsupp.induction x (add_submonoid.zero_mem _) $ λ a b f ha hb hf, add_submonoid.add_mem _ (add_submonoid.subset_closure $ set.mem_Union.2 ⟨a, set.mem_range_self _⟩) hf @[ext] lemma add_hom_ext [add_monoid γ] ⦃f g : (α →₀ β) →+ γ⦄ (H : ∀ x y, f (single x y) = g (single x y)) : f = g := begin refine add_monoid_hom.eq_of_eq_on_mdense add_closure_Union_range_single (λ f hf, _), simp only [set.mem_Union, set.mem_range] at hf, rcases hf with ⟨x, y, rfl⟩, apply H end lemma map_range_add [add_monoid β₁] [add_monoid β₂] {f : β₁ → β₂} {hf : f 0 = 0} (hf' : ∀ x y, f (x + y) = f x + f y) (v₁ v₂ : α →₀ β₁) : map_range f hf (v₁ + v₂) = map_range f hf v₁ + map_range f hf v₂ := ext $ λ a, by simp only [hf', add_apply, map_range_apply] end add_monoid section nat_sub instance nat_sub : has_sub (α →₀ ℕ) := ⟨zip_with (λ m n, m - n) (nat.sub_zero 0)⟩ @[simp] lemma nat_sub_apply {g₁ g₂ : α →₀ ℕ} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma single_sub {a : α} {n₁ n₂ : ℕ} : single a (n₁ - n₂) = single a n₁ - single a n₂ := begin ext f, by_cases h : (a = f), { rw [h, nat_sub_apply, single_eq_same, single_eq_same, single_eq_same] }, rw [nat_sub_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h] end -- These next two lemmas are used in developing -- the partial derivative on `mv_polynomial`. lemma sub_single_one_add {a : α} {u u' : α →₀ ℕ} (h : u a ≠ 0) : u - single a 1 + u' = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u a), { contradiction }, { simp }, }, { simp [h], } end lemma add_sub_single_one {a : α} {u u' : α →₀ ℕ} (h : u' a ≠ 0) : u + (u' - single a 1) = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u' a), { contradiction }, { simp }, }, { simp [h], } end end nat_sub instance [add_comm_monoid β] : add_comm_monoid (α →₀ β) := { add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _, .. finsupp.add_monoid } instance [add_group β] : add_group (α →₀ β) := { neg := map_range (has_neg.neg) neg_zero, add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _, .. finsupp.add_monoid } lemma single_multiset_sum [add_comm_monoid β] (s : multiset β) (a : α) : single a s.sum = (s.map (single a)).sum := multiset.induction_on s single_zero $ λ a s ih, by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons] lemma single_finset_sum [add_comm_monoid β] (s : finset γ) (f : γ → β) (a : α) : single a (∑ b in s, f b) = ∑ b in s, single a (f b) := begin transitivity, apply single_multiset_sum, rw [multiset.map_map], refl end lemma single_sum [has_zero γ] [add_comm_monoid β] (s : δ →₀ γ) (f : δ → γ → β) (a : α) : single a (s.sum f) = s.sum (λd c, single a (f d c)) := single_finset_sum _ _ _ @[to_additive] lemma prod_neg_index [add_group β] [comm_monoid γ] {g : α →₀ β} {h : α → β → γ} (h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) := prod_map_range_index h0 @[simp] lemma neg_apply [add_group β] {g : α →₀ β} {a : α} : (- g) a = - g a := rfl @[simp] lemma sub_apply [add_group β] {g₁ g₂ : α →₀ β} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma support_neg [add_group β] {f : α →₀ β} : support (-f) = support f := finset.subset.antisymm support_map_range (calc support f = support (- (- f)) : congr_arg support (neg_neg _).symm ... ⊆ support (- f) : support_map_range) instance [add_comm_group β] : add_comm_group (α →₀ β) := { add_comm := add_comm, ..finsupp.add_group } @[simp] lemma sum_apply [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {a₂ : α} : (f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) := (eval_add_hom a₂ : (α →₀ β) →+ _).map_sum _ _ lemma support_sum [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → (α →₀ β)} : (f.sum g).support ⊆ f.support.bind (λa, (g a (f a)).support) := have ∀a₁ : α, f.sum (λ (a : α₁) (b : β₁), (g a b) a₁) ≠ 0 → (∃ (a : α₁), f a ≠ 0 ∧ ¬ (g a (f a)) a₁ = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, mem_support_iff.mp ha, ne⟩, by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply, exists_prop] using this @[simp] lemma sum_zero [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} : f.sum (λa b, (0 : γ)) = 0 := finset.sum_const_zero @[simp] lemma sum_add [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} {h₁ h₂ : α → β → γ} : f.sum (λa b, h₁ a b + h₂ a b) = f.sum h₁ + f.sum h₂ := finset.sum_add_distrib @[simp] lemma sum_neg [add_comm_monoid β] [add_comm_group γ] {f : α →₀ β} {h : α → β → γ} : f.sum (λa b, - h a b) = - f.sum h := f.support.sum_hom (@has_neg.neg γ _) @[simp] lemma sum_sub [add_comm_monoid β] [add_comm_group γ] {f : α →₀ β} {h₁ h₂ : α → β → γ} : f.sum (λa b, h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ := by rw [sub_eq_add_neg, ←sum_neg, ←sum_add]; refl @[to_additive] lemma prod_add_index [add_comm_monoid β] [comm_monoid γ] {f g : α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := have hf : f.prod h = ∏ a in f.support ∪ g.support, h a (f a), from f.prod_of_support_subset (subset_union_left _ _) _ $ λ a ha, h_zero a, have hg : g.prod h = ∏ a in f.support ∪ g.support, h a (g a), from g.prod_of_support_subset (subset_union_right _ _) _ $ λ a ha, h_zero a, have hfg : (f + g).prod h = ∏ a in f.support ∪ g.support, h a ((f + g) a), from (f + g).prod_of_support_subset support_add _ $ λ a ha, h_zero a, by simp only [*, add_apply, prod_mul_distrib] /-- The canonical isomorphism between families of additive monoid homomorphisms `α → (β →+ γ)` and monoid homomorphisms `(α →₀ β) →+ γ`. -/ def lift_add_hom [add_comm_monoid β] [add_comm_monoid γ] : (α → β →+ γ) ≃+ ((α →₀ β) →+ γ) := { to_fun := λ F, { to_fun := λ f, f.sum (λ x, F x), map_zero' := finset.sum_empty, map_add' := λ _ _, sum_add_index (λ x, (F x).map_zero) (λ x, (F x).map_add) }, inv_fun := λ F x, F.comp $ single_add_hom x, left_inv := λ F, by { ext, simp }, right_inv := λ F, by { ext, simp }, map_add' := λ F G, by { ext, simp } } @[simp] lemma lift_add_hom_apply [add_comm_monoid β] [add_comm_monoid γ] (F : α → β →+ γ) (f : α →₀ β) : lift_add_hom F f = f.sum (λ x, F x) := rfl @[simp] lemma lift_add_hom_symm_apply [add_comm_monoid β] [add_comm_monoid γ] (F : (α →₀ β) →+ γ) (x : α) : lift_add_hom.symm F x = F.comp (single_add_hom x) := rfl @[simp] lemma lift_add_hom_single_add_hom [add_comm_monoid β] : lift_add_hom (single_add_hom : α → β →+ α →₀ β) = add_monoid_hom.id _ := lift_add_hom.to_equiv.apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma sum_single [add_comm_monoid β] (f : α →₀ β) : f.sum single = f := add_monoid_hom.congr_fun lift_add_hom_single_add_hom f lemma sum_sub_index [add_comm_group β] [add_comm_group γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := (lift_add_hom (λ a, add_monoid_hom.of_map_sub (h a) (h_sub a))).map_sub f g @[to_additive] lemma prod_finset_sum_index [add_comm_monoid β] [comm_monoid γ] {s : finset ι} {g : ι → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : ∏ i in s, (g i).prod h = (∑ i in s, g i).prod h := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, sum_insert has, prod_add_index h_zero h_add] @[to_additive] lemma prod_sum_index [add_comm_monoid β₁] [add_comm_monoid β] [comm_monoid γ] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f.sum g).prod h = f.prod (λa b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm lemma multiset_sum_sum_index [add_comm_monoid β] [add_comm_monoid γ] (f : multiset (α →₀ β)) (h : α → β → γ) (h₀ : ∀a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : β), h a (b₁ + b₂) = h a b₁ + h a b₂) : (f.sum.sum h) = (f.map $ λg:α →₀ β, g.sum h).sum := multiset.induction_on f rfl $ assume a s ih, by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index h₀ h₁, ih] lemma multiset_map_sum [has_zero β] {f : α →₀ β} {m : γ → δ} {h : α → β → multiset γ} : multiset.map m (f.sum h) = f.sum (λa b, (h a b).map m) := (f.support.sum_hom _).symm lemma multiset_sum_sum [has_zero β] [add_comm_monoid γ] {f : α →₀ β} {h : α → β → multiset γ} : multiset.sum (f.sum h) = f.sum (λa b, multiset.sum (h a b)) := (f.support.sum_hom multiset.sum).symm section map_range variables [add_comm_monoid β₁] [add_comm_monoid β₂] (f : β₁ →+ β₂) /-- Composition with a fixed additive homomorphism is itself an additive homomorphism on functions. -/ def map_range.add_monoid_hom : (α →₀ β₁) →+ (α →₀ β₂) := { to_fun := (map_range f f.map_zero : (α →₀ β₁) → (α →₀ β₂)), map_zero' := map_range_zero, map_add' := λ a b, map_range_add f.map_add _ _ } lemma map_range_multiset_sum (m : multiset (α →₀ β₁)) : map_range f f.map_zero m.sum = (m.map $ λx, map_range f f.map_zero x).sum := (m.sum_hom (map_range.add_monoid_hom f)).symm lemma map_range_finset_sum {ι : Type*} (s : finset ι) (g : ι → (α →₀ β₁)) : map_range f f.map_zero (∑ x in s, g x) = ∑ x in s, map_range f f.map_zero (g x) := by rw [finset.sum.equations._eqn_1, map_range_multiset_sum, multiset.map_map]; refl end map_range /-! ### Declarations about `map_domain` -/ section map_domain variables [add_comm_monoid β] {v v₁ v₂ : α →₀ β} /-- Given `f : α₁ → α₂` and `v : α₁ →₀ β`, `map_domain f v : α₂ →₀ β` is the finitely supported function whose value at `a : α₂` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α₁ → α₂) (v : α₁ →₀ β) : α₂ →₀ β := v.sum $ λa, single (f a) lemma map_domain_apply {f : α₁ → α₂} (hf : function.injective f) (x : α₁ →₀ β) (a : α₁) : map_domain f x (f a) = x a := begin rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same], { assume b _ hba, exact single_eq_of_ne (hf.ne hba) }, { simp only [(∉), (≠), not_not, mem_support_iff], assume h, rw [h, single_zero], refl } end lemma map_domain_notin_range {f : α₁ → α₂} (x : α₁ →₀ β) (a : α₂) (h : a ∉ set.range f) : map_domain f x a = 0 := begin rw [map_domain, sum_apply, sum], exact finset.sum_eq_zero (assume a' h', single_eq_of_ne $ assume eq, h $ eq ▸ set.mem_range_self _) end lemma map_domain_id : map_domain id v = v := sum_single _ lemma map_domain_comp {f : α → α₁} {g : α₁ → α₂} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := begin refine ((sum_sum_index _ _).trans _).symm, { intros, exact single_zero }, { intros, exact single_add }, refine sum_congr rfl (λ _ _, sum_single_index _), { exact single_zero } end lemma map_domain_single {f : α → α₁} {a : α} {b : β} : map_domain f (single a b) = single (f a) b := sum_single_index single_zero @[simp] lemma map_domain_zero {f : α → α₂} : map_domain f 0 = (0 : α₂ →₀ β) := sum_zero_index lemma map_domain_congr {f g : α → α₂} (h : ∀x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ λ _ H, by simp only [h _ H] lemma map_domain_add {f : α → α₂} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ := sum_add_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_finset_sum {f : α → α₂} {s : finset ι} {v : ι → α →₀ β} : map_domain f (∑ i in s, v i) = ∑ i in s, map_domain f (v i) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_sum [has_zero β₁] {f : α → α₂} {s : α →₀ β₁} {v : α → β₁ → α →₀ β} : map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_support {f : α → α₂} {s : α →₀ β} : (s.map_domain f).support ⊆ s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bind_mono $ assume a ha, support_single_subset) $ by rw [finset.bind_singleton]; exact subset.refl _ @[to_additive] lemma prod_map_domain_index [comm_monoid γ] {f : α → α₂} {s : α →₀ β} {h : α₂ → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (s.map_domain f).prod h = s.prod (λa b, h (f a) b) := (prod_sum_index h_zero h_add).trans $ prod_congr rfl $ λ _ _, prod_single_index (h_zero _) lemma emb_domain_eq_map_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : emb_domain f v = map_domain f v := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a, rfl⟩, rw [map_domain_apply f.injective, emb_domain_apply] }, { rw [map_domain_notin_range, emb_domain_notin_range]; assumption } end lemma map_domain_injective {f : α₁ → α₂} (hf : function.injective f) : function.injective (map_domain f : (α₁ →₀ β) → (α₂ →₀ β)) := begin assume v₁ v₂ eq, ext a, have : map_domain f v₁ (f a) = map_domain f v₂ (f a), { rw eq }, rwa [map_domain_apply hf, map_domain_apply hf] at this, end end map_domain /-! ### Declarations about `comap_domain` -/ section comap_domain /-- Given `f : α₁ → α₂`, `l : α₂ →₀ γ` and a proof `hf` that `f` is injective on the preimage of `l.support`, `comap_domain f l hf` is the finitely supported function from `α₁` to `γ` given by composing `l` with `f`. -/ def comap_domain {α₁ α₂ γ : Type*} [has_zero γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.inj_on f (f ⁻¹' ↑l.support)) : α₁ →₀ γ := { support := l.support.preimage f hf, to_fun := (λ a, l (f a)), mem_support_to_fun := begin intros a, simp only [finset.mem_def.symm, finset.mem_preimage], exact l.mem_support_to_fun (f a), end } @[simp] lemma comap_domain_apply {α₁ α₂ γ : Type*} [has_zero γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.inj_on f (f ⁻¹' ↑l.support)) (a : α₁) : comap_domain f l hf a = l (f a) := rfl lemma sum_comap_domain {α₁ α₂ β γ : Type*} [has_zero β] [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ β) (g : α₂ → β → γ) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : (comap_domain f l hf.inj_on).sum (g ∘ f) = l.sum g := begin simp [sum], simp [comap_domain, finset.sum_preimage_of_bij f _ _ (λ (x : α₂), g x (l x))] end lemma eq_zero_of_comap_domain_eq_zero {α₁ α₂ γ : Type*} [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : comap_domain f l hf.inj_on = 0 → l = 0 := begin rw [← support_eq_empty, ← support_eq_empty, comap_domain], simp only [finset.ext_iff, finset.not_mem_empty, iff_false, mem_preimage], assume h a ha, cases hf.2.2 ha with b hb, exact h b (hb.2.symm ▸ ha) end lemma map_domain_comap_domain {α₁ α₂ γ : Type*} [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : function.injective f) (hl : ↑l.support ⊆ set.range f): map_domain f (comap_domain f l (hf.inj_on _)) = l := begin ext a, by_cases h_cases: a ∈ set.range f, { rcases set.mem_range.1 h_cases with ⟨b, hb⟩, rw [hb.symm, map_domain_apply hf, comap_domain_apply] }, { rw map_domain_notin_range _ _ h_cases, by_contra h_contr, apply h_cases (hl $ finset.mem_coe.2 $ mem_support_iff.2 $ λ h, h_contr h.symm) } end end comap_domain /-! ### Declarations about `filter` -/ section filter section has_zero variables [has_zero β] (p : α → Prop) (f : α →₀ β) /-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : α → Prop) (f : α →₀ β) : α →₀ β := on_finset f.support (λa, if p a then f a else 0) $ λ a H, mem_support_iff.2 $ λ h, by rw [h, if_t_t] at H; exact H rfl @[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := if_pos h @[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 := if_neg h @[simp] lemma support_filter : (f.filter p).support = f.support.filter p := finset.ext $ assume a, if H : p a then by simp only [mem_support_iff, filter_apply_pos _ _ H, mem_filter, H, and_true] else by simp only [mem_support_iff, filter_apply_neg _ _ H, mem_filter, H, and_false, ne.def, ne_self_iff_false] lemma filter_zero : (0 : α →₀ β).filter p = 0 := by rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty] @[simp] lemma filter_single_of_pos {a : α} {b : β} (h : p a) : (single a b).filter p = single a b := ext $ λ x, begin by_cases h' : p x, { simp only [h', filter_apply_pos] }, { simp only [h', filter_apply_neg, not_false_iff], rw single_eq_of_ne, rintro rfl, exact h' h } end @[simp] lemma filter_single_of_neg {a : α} {b : β} (h : ¬ p a) : (single a b).filter p = 0 := ext $ λ x, begin by_cases h' : p x, { simp only [h', filter_apply_pos, zero_apply], rw single_eq_of_ne, rintro rfl, exact h h' }, { simp only [h', finsupp.zero_apply, not_false_iff, filter_apply_neg] } end end has_zero lemma filter_pos_add_filter_neg [add_monoid β] (f : α →₀ β) (p : α → Prop) : f.filter p + f.filter (λa, ¬ p a) = f := ext $ assume a, if H : p a then by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_not, add_zero] else by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_false_iff, zero_add] end filter /-! ### Declarations about `frange` -/ section frange variables [has_zero β] /-- `frange f` is the image of `f` on the support of `f`. -/ def frange (f : α →₀ β) : finset β := finset.image f f.support theorem mem_frange {f : α →₀ β} {y : β} : y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y := finset.mem_image.trans ⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩, λ ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩ theorem zero_not_mem_frange {f : α →₀ β} : (0:β) ∉ f.frange := λ H, (mem_frange.1 H).1 rfl theorem frange_single {x : α} {y : β} : frange (single x y) ⊆ {y} := λ r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 ▸ (by rw single_apply at ht2 ⊢; split_ifs at ht2 ⊢; [exact finset.mem_singleton_self _, cc]) end frange /-! ### Declarations about `subtype_domain` -/ section subtype_domain variables {α' : Type*} [has_zero δ] {p : α → Prop} section zero variables [has_zero β] {v v' : α' →₀ β} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain (p : α → Prop) (f : α →₀ β) : (subtype p →₀ β) := ⟨f.support.subtype p, f ∘ subtype.val, λ a, by simp only [mem_subtype, mem_support_iff]⟩ @[simp] lemma support_subtype_domain {f : α →₀ β} : (subtype_domain p f).support = f.support.subtype p := rfl @[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ β} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ β) = 0 := rfl @[to_additive] lemma prod_subtype_domain_index [comm_monoid γ] {v : α →₀ β} {h : α → β → γ} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λa b, h a.1 b) = v.prod h := prod_bij (λp _, p.val) (λ _, mem_subtype.1) (λ _ _, rfl) (λ _ _ _ _, subtype.eq) (λ b hb, ⟨⟨b, hp b hb⟩, mem_subtype.2 hb, rfl⟩) end zero section monoid variables [add_monoid β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_add {v v' : α →₀ β} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ _, rfl instance subtype_domain.is_add_monoid_hom : is_add_monoid_hom (subtype_domain p : (α →₀ β) → subtype p →₀ β) := { map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero } @[simp] lemma filter_add {v v' : α →₀ β} : (v + v').filter p = v.filter p + v'.filter p := ext $ λ a, begin by_cases p a, { simp only [h, filter_apply_pos, add_apply] }, { simp only [h, add_zero, add_apply, not_false_iff, filter_apply_neg] } end instance filter.is_add_monoid_hom (p : α → Prop) : is_add_monoid_hom (filter p : (α →₀ β) → (α →₀ β)) := { map_zero := filter_zero p, map_add := λ x y, filter_add } end monoid section comm_monoid variables [add_comm_monoid β] lemma subtype_domain_sum {s : finset γ} {h : γ → α →₀ β} : (∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p := eq.symm (s.sum_hom _) lemma subtype_domain_finsupp_sum {s : γ →₀ δ} {h : γ → δ → α →₀ β} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum lemma filter_sum (s : finset γ) (f : γ → α →₀ β) : (∑ a in s, f a).filter p = ∑ a in s, filter p (f a) := (s.sum_hom (filter p)).symm end comm_monoid section group variables [add_group β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_neg {v : α →₀ β} : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ _, rfl @[simp] lemma subtype_domain_sub {v v' : α →₀ β} : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ _, rfl end group end subtype_domain /-! ### Declarations relating `finsupp` to `multiset` -/ section multiset /-- Given `f : α →₀ ℕ`, `f.to_multiset` is the multiset with multiplicities given by the values of `f` on the elements of `α`. -/ def to_multiset (f : α →₀ ℕ) : multiset α := f.sum (λa n, n •ℕ {a}) lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 := rfl lemma to_multiset_add (m n : α →₀ ℕ) : (m + n).to_multiset = m.to_multiset + n.to_multiset := sum_add_index (assume a, zero_nsmul _) (assume a b₁ b₂, add_nsmul _ _ _) lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (single a n) = n •ℕ {a} := by rw [to_multiset, sum_single_index]; apply zero_nsmul instance is_add_monoid_hom.to_multiset : is_add_monoid_hom (to_multiset : _ → multiset α) := { map_zero := to_multiset_zero, map_add := to_multiset_add } lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λa, id) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.card_zero, sum_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.card_add, ih, sum_add_index, to_multiset_single, sum_single_index, multiset.card_smul, multiset.singleton_eq_singleton, multiset.card_singleton, mul_one]; intros; refl } end lemma to_multiset_map (f : α →₀ ℕ) (g : α → β) : f.to_multiset.map g = (f.map_domain g).to_multiset := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single, to_multiset_single, to_multiset_add, to_multiset_single, is_add_monoid_hom.map_nsmul (multiset.map g)], refl } end lemma prod_to_multiset [comm_monoid α] (f : α →₀ ℕ) : f.to_multiset.prod = f.prod (λa n, a ^ n) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index, finsupp.prod_single_index, multiset.prod_smul, multiset.singleton_eq_singleton, multiset.prod_singleton], { exact pow_zero a }, { exact pow_zero }, { exact pow_add } } end lemma to_finset_to_multiset (f : α →₀ ℕ) : f.to_multiset.to_finset = f.support := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.to_finset_zero, support_zero] }, { assume a n f ha hn ih, rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq, support_single_ne_zero hn, multiset.to_finset_nsmul _ _ hn, multiset.singleton_eq_singleton, multiset.to_finset_cons, multiset.to_finset_zero], refl, refine disjoint.mono_left support_single_subset _, rwa [finset.singleton_disjoint] } end @[simp] lemma count_to_multiset (f : α →₀ ℕ) (a : α) : f.to_multiset.count a = f a := calc f.to_multiset.count a = f.sum (λx n, (n •ℕ {x} : multiset α).count a) : (f.support.sum_hom $ multiset.count a).symm ... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_smul] ... = f.sum (λx n, n * (x :: 0 : multiset α).count a) : rfl ... = f a * (a :: 0 : multiset α).count a : sum_eq_single _ (λ a' _ H, by simp only [multiset.count_cons_of_ne (ne.symm H), multiset.count_zero, mul_zero]) (λ H, by simp only [not_mem_support_iff.1 H, zero_mul]) ... = f a : by simp only [multiset.count_singleton, mul_one] /-- Given `m : multiset α`, `of_multiset m` is the finitely supported function from `α` to `ℕ` given by the multiplicities of the elements of `α` in `m`. -/ def of_multiset (m : multiset α) : α →₀ ℕ := on_finset m.to_finset (λa, m.count a) $ λ a H, multiset.mem_to_finset.2 $ by_contradiction (mt multiset.count_eq_zero.2 H) @[simp] lemma of_multiset_apply (m : multiset α) (a : α) : of_multiset m a = m.count a := rfl /-- `equiv_multiset` defines an `equiv` between finitely supported functions from `α` to `ℕ` and multisets on `α`. -/ def equiv_multiset : (α →₀ ℕ) ≃ (multiset α) := ⟨ to_multiset, of_multiset, assume f, finsupp.ext $ λ a, by rw [of_multiset_apply, count_to_multiset], assume m, multiset.ext.2 $ λ a, by rw [count_to_multiset, of_multiset_apply] ⟩ lemma mem_support_multiset_sum [add_comm_monoid β] {s : multiset (α →₀ β)} (a : α) : a ∈ s.sum.support → ∃f∈s, a ∈ (f : α →₀ β).support := multiset.induction_on s false.elim begin assume f s ih ha, by_cases a ∈ f.support, { exact ⟨f, multiset.mem_cons_self _ _, h⟩ }, { simp only [multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha, rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩, exact ⟨f', multiset.mem_cons_of_mem h₀, h₁⟩ } end lemma mem_support_finset_sum [add_comm_monoid β] {s : finset γ} {h : γ → α →₀ β} (a : α) (ha : a ∈ (∑ c in s, h c).support) : ∃c∈s, a ∈ (h c).support := let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in ⟨c, hc, eq.symm ▸ hfa⟩ lemma mem_support_single [has_zero β] (a a' : α) (b : β) : a ∈ (single a' b).support ↔ a = a' ∧ b ≠ 0 := ⟨λ H : (a ∈ ite _ _ _), if h : b = 0 then by rw if_pos h at H; exact H.elim else ⟨by rw if_neg h at H; exact mem_singleton.1 H, h⟩, λ ⟨h1, h2⟩, show a ∈ ite _ _ _, by rw [if_neg h2]; exact mem_singleton.2 h1⟩ @[simp] lemma mem_to_multiset (f : α →₀ ℕ) (i : α) : i ∈ f.to_multiset ↔ i ∈ f.support := by rw [← multiset.count_ne_zero, finsupp.count_to_multiset, finsupp.mem_support_iff] end multiset /-! ### Declarations about `curry` and `uncurry` -/ section curry_uncurry /-- Given a finitely supported function `f` from a product type `α × β` to `γ`, `curry f` is the "curried" finitely supported function from `α` to the type of finitely supported functions from `β` to `γ`. -/ protected def curry [add_comm_monoid γ] (f : (α × β) →₀ γ) : α →₀ (β →₀ γ) := f.sum $ λp c, single p.1 (single p.2 c) lemma sum_curry_index [add_comm_monoid γ] [add_comm_monoid δ] (f : (α × β) →₀ γ) (g : α → β → γ → δ) (hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) : f.curry.sum (λa f, f.sum (g a)) = f.sum (λp c, g p.1 p.2 c) := begin rw [finsupp.curry], transitivity, { exact sum_sum_index (assume a, sum_zero_index) (assume a b₀ b₁, sum_add_index (assume a, hg₀ _ _) (assume c d₀ d₁, hg₁ _ _ _ _)) }, congr, funext p c, transitivity, { exact sum_single_index sum_zero_index }, exact sum_single_index (hg₀ _ _) end /-- Given a finitely supported function `f` from `α` to the type of finitely supported functions from `β` to `γ`, `uncurry f` is the "uncurried" finitely supported function from `α × β` to `γ`. -/ protected def uncurry [add_comm_monoid γ] (f : α →₀ (β →₀ γ)) : (α × β) →₀ γ := f.sum $ λa g, g.sum $ λb c, single (a, b) c /-- `finsupp_prod_equiv` defines the `equiv` between `((α × β) →₀ γ)` and `(α →₀ (β →₀ γ))` given by currying and uncurrying. -/ def finsupp_prod_equiv [add_comm_monoid γ] : ((α × β) →₀ γ) ≃ (α →₀ (β →₀ γ)) := by refine ⟨finsupp.curry, finsupp.uncurry, λ f, _, λ f, _⟩; simp only [ finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] lemma filter_curry [add_comm_monoid β] (f : α₁ × α₂ →₀ β) (p : α₁ → Prop) : (f.filter (λa:α₁×α₂, p a.1)).curry = f.curry.filter p := begin rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, @filter_sum _ (α₂ →₀ β) _ p _ f.support _], rw [support_filter, sum_filter], refine finset.sum_congr rfl _, rintros ⟨a₁, a₂⟩ ha, dsimp only, split_ifs, { rw [filter_apply_pos, filter_single_of_pos]; exact h }, { rwa [filter_single_of_neg] } end lemma support_curry [add_comm_monoid β] (f : α₁ × α₂ →₀ β) : f.curry.support ⊆ f.support.image prod.fst := begin rw ← finset.bind_singleton, refine finset.subset.trans support_sum _, refine finset.bind_mono (assume a _, support_single_subset) end end curry_uncurry section variables [group γ] [mul_action γ α] [add_comm_monoid β] /-- Scalar multiplication by a group element g, given by precomposition with the action of g⁻¹ on the domain. -/ def comap_has_scalar : has_scalar γ (α →₀ β) := { smul := λ g f, f.comap_domain (λ a, g⁻¹ • a) (λ a a' m m' h, by simpa [←mul_smul] using (congr_arg (λ a, g • a) h)) } local attribute [instance] comap_has_scalar /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is multiplicative in g. -/ def comap_mul_action : mul_action γ (α →₀ β) := { one_smul := λ f, by { ext, dsimp [(•)], simp, }, mul_smul := λ g g' f, by { ext, dsimp [(•)], simp [mul_smul], }, } local attribute [instance] comap_mul_action /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is additive in the second argument. -/ def comap_distrib_mul_action : distrib_mul_action γ (α →₀ β) := { smul_zero := λ g, by { ext, dsimp [(•)], simp, }, smul_add := λ g f f', by { ext, dsimp [(•)], simp, }, } /-- Scalar multiplication by a group element on finitely supported functions on a group, given by precomposition with the action of g⁻¹. -/ def comap_distrib_mul_action_self : distrib_mul_action γ (γ →₀ β) := @finsupp.comap_distrib_mul_action γ β γ _ (mul_action.regular γ) _ @[simp] lemma comap_smul_single (g : γ) (a : α) (b : β) : g • single a b = single (g • a) b := begin ext a', dsimp [(•)], by_cases h : g • a = a', { subst h, simp [←mul_smul], }, { simp [single_eq_of_ne h], rw [single_eq_of_ne], rintro rfl, simpa [←mul_smul] using h, } end @[simp] lemma comap_smul_apply (g : γ) (f : α →₀ β) (a : α) : (g • f) a = f (g⁻¹ • a) := rfl end section instance [semiring γ] [add_comm_monoid β] [semimodule γ β] : has_scalar γ (α →₀ β) := ⟨λa v, v.map_range ((•) a) (smul_zero _)⟩ variables (α β) @[simp] lemma smul_apply' {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {a : α} {b : γ} {v : α →₀ β} : (b • v) a = b • (v a) := rfl instance [semiring γ] [add_comm_monoid β] [semimodule γ β] : semimodule γ (α →₀ β) := { smul := (•), smul_add := λ a x y, ext $ λ _, smul_add _ _ _, add_smul := λ a x y, ext $ λ _, add_smul _ _ _, one_smul := λ x, ext $ λ _, one_smul _ _, mul_smul := λ r s x, ext $ λ _, mul_smul _ _ _, zero_smul := λ x, ext $ λ _, zero_smul _ _, smul_zero := λ x, ext $ λ _, smul_zero _ } variables {α β} (γ) /-- Evaluation at point as a linear map. This version assumes that the codomain is a semimodule over some semiring. See also `leval`. -/ def leval' [semiring γ] [add_comm_monoid β] [semimodule γ β] (a : α) : (α →₀ β) →ₗ[γ] β := ⟨λ g, g a, λ _ _, add_apply, λ _ _, rfl⟩ @[simp] lemma coe_leval' [semiring γ] [add_comm_monoid β] [semimodule γ β] (a : α) (g : α →₀ β) : leval' γ a g = g a := rfl variable {γ} /-- Evaluation at point as a linear map. This version assumes that the codomain is a semiring. -/ def leval [semiring β] (a : α) : (α →₀ β) →ₗ[β] β := leval' β a @[simp] lemma coe_leval [semiring β] (a : α) (g : α →₀ β) : leval a g = g a := rfl lemma support_smul {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {b : γ} {g : α →₀ β} : (b • g).support ⊆ g.support := λ a, by simp only [smul_apply', mem_support_iff, ne.def]; exact mt (λ h, h.symm ▸ smul_zero _) section variables {p : α → Prop} @[simp] lemma filter_smul {R : semiring γ} [add_comm_monoid β] [semimodule γ β] {b : γ} {v : α →₀ β} : (b • v).filter p = b • v.filter p := ext $ λ a, begin by_cases p a, { simp only [h, smul_apply', filter_apply_pos] }, { simp only [h, smul_apply', not_false_iff, filter_apply_neg, smul_zero] } end end lemma map_domain_smul {α'} {R : semiring γ} [add_comm_monoid β] [semimodule γ β] {f : α → α'} (b : γ) (v : α →₀ β) : map_domain f (b • v) = b • map_domain f v := begin change map_domain f (map_range _ _ _) = map_range _ _ _, apply finsupp.induction v, { simp only [map_domain_zero, map_range_zero] }, intros a b v' hv₁ hv₂ IH, rw [map_range_add, map_domain_add, IH, map_domain_add, map_range_add, map_range_single, map_domain_single, map_domain_single, map_range_single]; apply smul_add end @[simp] lemma smul_single {R : semiring γ} [add_comm_monoid β] [semimodule γ β] (c : γ) (a : α) (b : β) : c • finsupp.single a b = finsupp.single a (c • b) := ext $ λ a', by by_cases a = a'; [{ subst h, simp only [smul_apply', single_eq_same] }, simp only [h, smul_apply', ne.def, not_false_iff, single_eq_of_ne, smul_zero]] @[simp] lemma smul_single' {R : semiring γ} (c : γ) (a : α) (b : γ) : c • finsupp.single a b = finsupp.single a (c * b) := smul_single _ _ _ lemma smul_single_one [semiring β] (a : α) (b : β) : b • single a 1 = single a b := by rw [smul_single, smul_eq_mul, mul_one] /-- Two `R`-linear maps from `finsupp X R` which agree on `single x 1` agree everywhere. -/ @[ext] lemma hom_ext [semiring β] [add_comm_monoid γ] [semimodule β γ] ⦃φ ψ : (α →₀ β) →ₗ[β] γ⦄ (h : ∀ a : α, φ (single a 1) = ψ (single a 1)) : φ = ψ := linear_map.to_add_monoid_hom_injective $ add_hom_ext $ λ x y, by simp only [← smul_single_one x y, linear_map.to_add_monoid_hom_coe, linear_map.map_smul, h] end @[simp] lemma smul_apply [semiring β] {a : α} {b : β} {v : α →₀ β} : (b • v) a = b • (v a) := rfl lemma sum_smul_index [semiring β] [add_comm_monoid γ] {g : α →₀ β} {b : β} {h : α → β → γ} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) := finsupp.sum_map_range_index h0 lemma sum_smul_index' [semiring β] [add_comm_monoid γ] [semimodule β γ] [add_comm_monoid δ] {g : α →₀ γ} {b : β} {h : α → γ → δ} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi c, h i (b • c)) := finsupp.sum_map_range_index h0 section variables [semiring β] [semiring γ] lemma sum_mul (b : γ) (s : α →₀ β) {f : α → β → γ} : (s.sum f) * b = s.sum (λ a c, (f a c) * b) := by simp only [finsupp.sum, finset.sum_mul] lemma mul_sum (b : γ) (s : α →₀ β) {f : α → β → γ} : b * (s.sum f) = s.sum (λ a c, b * (f a c)) := by simp only [finsupp.sum, finset.mul_sum] protected lemma eq_zero_of_zero_eq_one (zero_eq_one : (0 : β) = 1) (l : α →₀ β) : l = 0 := by ext i; simp only [eq_zero_of_zero_eq_one zero_eq_one (l i), finsupp.zero_apply] end /-- Given an `add_comm_monoid β` and `s : set α`, `restrict_support_equiv s β` is the `equiv` between the subtype of finitely supported functions with support contained in `s` and the type of finitely supported functions from `s`. -/ def restrict_support_equiv (s : set α) (β : Type*) [add_comm_monoid β] : {f : α →₀ β // ↑f.support ⊆ s } ≃ (s →₀ β):= begin refine ⟨λf, subtype_domain (λx, x ∈ s) f.1, λ f, ⟨f.map_domain subtype.val, _⟩, _, _⟩, { refine set.subset.trans (finset.coe_subset.2 map_domain_support) _, rw [finset.coe_image, set.image_subset_iff], exact assume x hx, x.2 }, { rintros ⟨f, hf⟩, apply subtype.eq, ext a, dsimp only, refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s → α), _) (assume h, _), { rcases h with ⟨x, rfl⟩, rw [map_domain_apply subtype.val_injective, subtype_domain_apply] }, { convert map_domain_notin_range _ _ h, rw [← not_mem_support_iff], refine mt _ h, exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } }, { assume f, ext ⟨a, ha⟩, dsimp only, rw [subtype_domain_apply, map_domain_apply subtype.val_injective] } end /-- Given `add_comm_monoid β` and `e : α₁ ≃ α₂`, `dom_congr e` is the corresponding `equiv` between `α₁ →₀ β` and `α₂ →₀ β`. -/ protected def dom_congr [add_comm_monoid β] (e : α₁ ≃ α₂) : (α₁ →₀ β) ≃+ (α₂ →₀ β) := { to_fun := map_domain e, inv_fun := map_domain e.symm, left_inv := begin assume v, simp only [map_domain_comp.symm, (∘), equiv.symm_apply_apply], exact map_domain_id end, right_inv := begin assume v, simp only [map_domain_comp.symm, (∘), equiv.apply_symm_apply], exact map_domain_id end, map_add' := λ a b, map_domain_add, } /-! ### Declarations about sigma types -/ section sigma variables {αs : ι → Type*} [has_zero β] (l : (Σ i, αs i) →₀ β) /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β` and an index element `i : ι`, `split l i` is the `i`th component of `l`, a finitely supported function from `as i` to `β`. -/ def split (i : ι) : αs i →₀ β := l.comap_domain (sigma.mk i) (λ x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2) lemma split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ := begin dunfold split, rw comap_domain_apply end /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β`, `split_support l` is the finset of indices in `ι` that appear in the support of `l`. -/ def split_support : finset ι := l.support.image sigma.fst lemma mem_split_support_iff_nonzero (i : ι) : i ∈ split_support l ↔ split l i ≠ 0 := begin rw [split_support, mem_image, ne.def, ← support_eq_empty, ← ne.def, ← finset.nonempty_iff_ne_empty, split, comap_domain, finset.nonempty], simp only [exists_prop, finset.mem_preimage, exists_and_distrib_right, exists_eq_right, mem_support_iff, sigma.exists, ne.def] end /-- Given `l`, a finitely supported function from the sigma type `Σ i, αs i` to `β` and an `ι`-indexed family `g` of functions from `(αs i →₀ β)` to `γ`, `split_comp` defines a finitely supported function from the index type `ι` to `γ` given by composing `g i` with `split l i`. -/ def split_comp [has_zero γ] (g : Π i, (αs i →₀ β) → γ) (hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ γ := { support := split_support l, to_fun := λ i, g i (split l i), mem_support_to_fun := begin intros i, rw [mem_split_support_iff_nonzero, not_iff_not, hg], end } lemma sigma_support : l.support = l.split_support.sigma (λ i, (l.split i).support) := by simp only [finset.ext_iff, split_support, split, comap_domain, mem_image, mem_preimage, sigma.forall, mem_sigma]; tauto lemma sigma_sum [add_comm_monoid γ] (f : (Σ (i : ι), αs i) → β → γ) : l.sum f = ∑ i in split_support l, (split l i).sum (λ (a : αs i) b, f ⟨i, a⟩ b) := by simp only [sum, sigma_support, sum_sigma, split_apply] end sigma end finsupp /-! ### Declarations relating `multiset` to `finsupp` -/ namespace multiset /-- Given a multiset `s`, `s.to_finsupp` returns the finitely supported function on `ℕ` given by the multiplicities of the elements of `s`. -/ def to_finsupp (s : multiset α) : α →₀ ℕ := { support := s.to_finset, to_fun := λ a, s.count a, mem_support_to_fun := λ a, begin rw mem_to_finset, convert not_iff_not_of_iff (count_eq_zero.symm), rw not_not end } @[simp] lemma to_finsupp_support (s : multiset α) : s.to_finsupp.support = s.to_finset := rfl @[simp] lemma to_finsupp_apply (s : multiset α) (a : α) : s.to_finsupp a = s.count a := rfl @[simp] lemma to_finsupp_zero : to_finsupp (0 : multiset α) = 0 := finsupp.ext $ λ a, count_zero a @[simp] lemma to_finsupp_add (s t : multiset α) : to_finsupp (s + t) = to_finsupp s + to_finsupp t := finsupp.ext $ λ a, count_add a s t lemma to_finsupp_singleton (a : α) : to_finsupp {a} = finsupp.single a 1 := finsupp.ext $ λ b, if h : a = b then by rw [to_finsupp_apply, finsupp.single_apply, h, if_pos rfl, singleton_eq_singleton, count_singleton] else begin rw [to_finsupp_apply, finsupp.single_apply, if_neg h, count_eq_zero, singleton_eq_singleton, mem_singleton], rintro rfl, exact h rfl end namespace to_finsupp instance : is_add_monoid_hom (to_finsupp : multiset α → α →₀ ℕ) := { map_zero := to_finsupp_zero, map_add := to_finsupp_add } end to_finsupp @[simp] lemma to_finsupp_to_multiset (s : multiset α) : s.to_finsupp.to_multiset = s := ext.2 $ λ a, by rw [finsupp.count_to_multiset, to_finsupp_apply] end multiset /-! ### Declarations about order(ed) instances on `finsupp` -/ namespace finsupp variables {σ : Type*} instance [preorder α] [has_zero α] : preorder (σ →₀ α) := { le := λ f g, ∀ s, f s ≤ g s, le_refl := λ f s, le_refl _, le_trans := λ f g h Hfg Hgh s, le_trans (Hfg s) (Hgh s) } instance [partial_order α] [has_zero α] : partial_order (σ →₀ α) := { le_antisymm := λ f g hfg hgf, ext $ λ s, le_antisymm (hfg s) (hgf s), .. finsupp.preorder } instance [ordered_cancel_add_comm_monoid α] : add_left_cancel_semigroup (σ →₀ α) := { add_left_cancel := λ a b c h, ext $ λ s, by { rw ext_iff at h, exact add_left_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_add_comm_monoid α] : add_right_cancel_semigroup (σ →₀ α) := { add_right_cancel := λ a b c h, ext $ λ s, by { rw ext_iff at h, exact add_right_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_add_comm_monoid α] : ordered_cancel_add_comm_monoid (σ →₀ α) := { add_le_add_left := λ a b h c s, add_le_add_left (h s) (c s), le_of_add_le_add_left := λ a b c h s, le_of_add_le_add_left (h s), .. finsupp.add_comm_monoid, .. finsupp.partial_order, .. finsupp.add_left_cancel_semigroup, .. finsupp.add_right_cancel_semigroup } lemma le_iff [canonically_ordered_add_monoid α] (f g : σ →₀ α) : f ≤ g ↔ ∀ s ∈ f.support, f s ≤ g s := ⟨λ h s hs, h s, λ h s, if H : s ∈ f.support then h s H else (not_mem_support_iff.1 H).symm ▸ zero_le (g s)⟩ @[simp] lemma add_eq_zero_iff [canonically_ordered_add_monoid α] (f g : σ →₀ α) : f + g = 0 ↔ f = 0 ∧ g = 0 := begin split, { assume h, split, all_goals { ext s, suffices H : f s + g s = 0, { rw add_eq_zero_iff at H, cases H, assumption }, show (f + g) s = 0, rw h, refl } }, { rintro ⟨rfl, rfl⟩, rw add_zero } end attribute [simp] to_multiset_zero to_multiset_add @[simp] lemma to_multiset_to_finsupp (f : σ →₀ ℕ) : f.to_multiset.to_finsupp = f := ext $ λ s, by rw [multiset.to_finsupp_apply, count_to_multiset] lemma to_multiset_strict_mono : strict_mono (@to_multiset σ) := λ m n h, begin rw lt_iff_le_and_ne at h ⊢, cases h with h₁ h₂, split, { rw multiset.le_iff_count, intro s, erw [count_to_multiset m s, count_to_multiset], exact h₁ s }, { intro H, apply h₂, replace H := congr_arg multiset.to_finsupp H, simpa only [to_multiset_to_finsupp] using H } end lemma sum_id_lt_of_lt (m n : σ →₀ ℕ) (h : m < n) : m.sum (λ _, id) < n.sum (λ _, id) := begin rw [← card_to_multiset, ← card_to_multiset], apply multiset.card_lt_of_lt, exact to_multiset_strict_mono h end variable (σ) /-- The order on `σ →₀ ℕ` is well-founded.-/ lemma lt_wf : well_founded (@has_lt.lt (σ →₀ ℕ) _) := subrelation.wf (sum_id_lt_of_lt) $ inv_image.wf _ nat.lt_wf instance decidable_le : decidable_rel (@has_le.le (σ →₀ ℕ) _) := λ m n, by rw le_iff; apply_instance variable {σ} /-- The `finsupp` counterpart of `multiset.antidiagonal`: the antidiagonal of `s : σ →₀ ℕ` consists of all pairs `(t₁, t₂) : (σ →₀ ℕ) × (σ →₀ ℕ)` such that `t₁ + t₂ = s`. The finitely supported function `antidiagonal s` is equal to the multiplicities of these pairs. -/ def antidiagonal (f : σ →₀ ℕ) : ((σ →₀ ℕ) × (σ →₀ ℕ)) →₀ ℕ := (f.to_multiset.antidiagonal.map (prod.map multiset.to_finsupp multiset.to_finsupp)).to_finsupp lemma mem_antidiagonal_support {f : σ →₀ ℕ} {p : (σ →₀ ℕ) × (σ →₀ ℕ)} : p ∈ (antidiagonal f).support ↔ p.1 + p.2 = f := begin erw [multiset.mem_to_finset, multiset.mem_map], split, { rintros ⟨⟨a, b⟩, h, rfl⟩, rw multiset.mem_antidiagonal at h, simpa only [to_multiset_to_finsupp, multiset.to_finsupp_add] using congr_arg multiset.to_finsupp h}, { intro h, refine ⟨⟨p.1.to_multiset, p.2.to_multiset⟩, _, _⟩, { simpa only [multiset.mem_antidiagonal, to_multiset_add] using congr_arg to_multiset h}, { rw [prod.map, to_multiset_to_finsupp, to_multiset_to_finsupp, prod.mk.eta] } } end @[simp] lemma antidiagonal_zero : antidiagonal (0 : σ →₀ ℕ) = single (0,0) 1 := by rw [← multiset.to_finsupp_singleton]; refl lemma swap_mem_antidiagonal_support {n : σ →₀ ℕ} {f} (hf : f ∈ (antidiagonal n).support) : f.swap ∈ (antidiagonal n).support := by simpa only [mem_antidiagonal_support, add_comm, prod.swap] using hf /-- Let `n : σ →₀ ℕ` be a finitely supported function. The set of `m : σ →₀ ℕ` that are coordinatewise less than or equal to `n`, is a finite set. -/ lemma finite_le_nat (n : σ →₀ ℕ) : set.finite {m | m ≤ n} := begin let I := {i // i ∈ n.support}, let k : ℕ := ∑ i in n.support, n i, let f : (σ →₀ ℕ) → (I → fin (k + 1)) := λ m i, m i, have hf : ∀ m ≤ n, ∀ i, (f m i : ℕ) = m i, { intros m hm i, apply fin.coe_coe_of_lt, calc m i ≤ n i : hm i ... < k + 1 : nat.lt_succ_iff.mpr (single_le_sum (λ _ _, nat.zero_le _) i.2) }, have f_im : set.finite (f '' {m | m ≤ n}) := set.finite.of_fintype _, suffices f_inj : set.inj_on f {m | m ≤ n}, { exact set.finite_of_finite_image f_inj f_im }, intros m₁ h₁ m₂ h₂ h, ext i, by_cases hi : i ∈ n.support, { replace h := congr_fun h ⟨i, hi⟩, rwa [fin.ext_iff, hf m₁ h₁, hf m₂ h₂] at h }, { rw not_mem_support_iff at hi, specialize h₁ i, specialize h₂ i, rw [hi, nat.le_zero_iff] at h₁ h₂, rw [h₁, h₂] } end /-- Let `n : σ →₀ ℕ` be a finitely supported function. The set of `m : σ →₀ ℕ` that are coordinatewise less than or equal to `n`, but not equal to `n` everywhere, is a finite set. -/ lemma finite_lt_nat (n : σ →₀ ℕ) : set.finite {m | m < n} := (finite_le_nat n).subset $ λ m, le_of_lt end finsupp
827ccf058f68858d2ffd54655760c43ddf6827d7
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/category/Module/filtered_colimits.lean
39cf4e67f18cf92846cdb8d9f2fb668c36f3ec73
[ "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
7,268
lean
/- Copyright (c) 2021 Justus Springer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justus Springer -/ import algebra.category.Group.filtered_colimits import algebra.category.Module.basic /-! # The forgetful functor from `R`-modules preserves filtered colimits. Forgetful functors from algebraic categories usually don't preserve colimits. However, they tend to preserve _filtered_ colimits. In this file, we start with a ring `R`, a small filtered category `J` and a functor `F : J ⥤ Module R`. We show that the colimit of `F ⋙ forget₂ (Module R) AddCommGroup` (in `AddCommGroup`) carries the structure of an `R`-module, thereby showing that the forgetful functor `forget₂ (Module R) AddCommGroup` preserves filtered colimits. In particular, this implies that `forget (Module R)` preserves filtered colimits. -/ universes u v noncomputable theory open_locale classical open category_theory open category_theory.limits open category_theory.is_filtered (renaming max → max') -- avoid name collision with `_root_.max`. open AddMon.filtered_colimits (colimit_zero_eq colimit_add_mk_eq) namespace Module.filtered_colimits section -- We use parameters here, mainly so we can have the abbreviations `M` and `M.mk` below, without -- passing around `F` all the time. parameters {R : Type u} [ring R] {J : Type v} [small_category J] [is_filtered J] parameters (F : J ⥤ Module.{max v u} R) /-- The colimit of `F ⋙ forget₂ (Module R) AddCommGroup` in the category `AddCommGroup`. In the following, we will show that this has the structure of an `R`-module. -/ abbreviation M : AddCommGroup := AddCommGroup.filtered_colimits.colimit (F ⋙ forget₂ (Module R) AddCommGroup.{max v u}) /-- The canonical projection into the colimit, as a quotient type. -/ abbreviation M.mk : (Σ j, F.obj j) → M := quot.mk (types.quot.rel (F ⋙ forget (Module R))) lemma M.mk_eq (x y : Σ j, F.obj j) (h : ∃ (k : J) (f : x.1 ⟶ k) (g : y.1 ⟶ k), F.map f x.2 = F.map g y.2) : M.mk x = M.mk y := quot.eqv_gen_sound (types.filtered_colimit.eqv_gen_quot_rel_of_rel (F ⋙ forget (Module R)) x y h) /-- The "unlifted" version of scalar multiplication in the colimit. -/ def colimit_smul_aux (r : R) (x : Σ j, F.obj j) : M := M.mk ⟨x.1, r • x.2⟩ lemma colimit_smul_aux_eq_of_rel (r : R) (x y : Σ j, F.obj j) (h : types.filtered_colimit.rel (F ⋙ forget (Module R)) x y) : colimit_smul_aux r x = colimit_smul_aux r y := begin apply M.mk_eq, obtain ⟨k, f, g, hfg⟩ := h, use [k, f, g], simp only [category_theory.functor.comp_map, forget_map_eq_coe] at hfg, rw [linear_map.map_smul, linear_map.map_smul, hfg], end /-- Scalar multiplication in the colimit. See also `colimit_smul_aux`. -/ instance colimit_has_scalar : has_scalar R M := { smul := λ r x, begin refine quot.lift (colimit_smul_aux F r) _ x, intros x y h, apply colimit_smul_aux_eq_of_rel, apply types.filtered_colimit.rel_of_quot_rel, exact h, end } @[simp] lemma colimit_smul_mk_eq (r : R) (x : Σ j, F.obj j) : r • M.mk x = M.mk ⟨x.1, r • x.2⟩ := rfl instance colimit_module : module R M := { one_smul := λ x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, erw [colimit_smul_mk_eq F 1 ⟨j, x⟩, one_smul], refl, end, mul_smul := λ r s x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, erw [colimit_smul_mk_eq F (r * s) ⟨j, x⟩, colimit_smul_mk_eq F s ⟨j, x⟩, colimit_smul_mk_eq F r ⟨j, _⟩, mul_smul], end, smul_add := λ r x y, begin apply quot.induction_on₂ x y, clear x y, intros x y, cases x with i x, cases y with j y, erw [colimit_add_mk_eq _ ⟨i, x⟩ ⟨j, y⟩ (max' i j) (left_to_max i j) (right_to_max i j), colimit_smul_mk_eq, smul_add, colimit_smul_mk_eq, colimit_smul_mk_eq, colimit_add_mk_eq _ ⟨i, _⟩ ⟨j, _⟩ (max' i j) (left_to_max i j) (right_to_max i j), linear_map.map_smul, linear_map.map_smul], refl, end, smul_zero := λ r, begin erw [colimit_zero_eq _ (is_filtered.nonempty.some : J), colimit_smul_mk_eq, smul_zero], refl, end, zero_smul := λ x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, erw [colimit_smul_mk_eq, zero_smul, colimit_zero_eq _ j], refl, end, add_smul := λ r s x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, erw [colimit_smul_mk_eq, add_smul, colimit_smul_mk_eq, colimit_smul_mk_eq, colimit_add_mk_eq _ ⟨j, _⟩ ⟨j, _⟩ j (𝟙 j) (𝟙 j), category_theory.functor.map_id, id_apply, id_apply], refl, end } /-- The bundled `R`-module giving the filtered colimit of a diagram. -/ def colimit : Module R := Module.of R M /-- The linear map from a given `R`-module in the diagram to the colimit module. -/ def cocone_morphism (j : J) : F.obj j ⟶ colimit := { map_smul' := λ r x, begin erw colimit_smul_mk_eq F r ⟨j, x⟩, refl, end, .. (AddCommGroup.filtered_colimits.colimit_cocone (F ⋙ forget₂ (Module R) AddCommGroup.{max v u})).ι.app j } /-- The cocone over the proposed colimit module. -/ def colimit_cocone : cocone F := { X := colimit, ι := { app := cocone_morphism, naturality' := λ j j' f, linear_map.coe_injective ((types.colimit_cocone (F ⋙ forget (Module R))).ι.naturality f) } } /-- Given a cocone `t` of `F`, the induced monoid linear map from the colimit to the cocone point. We already know that this is a morphism between additive groups. The only thing left to see is that it is a linear map, i.e. preserves scalar multiplication. -/ def colimit_desc (t : cocone F) : colimit ⟶ t.X := { map_smul' := λ r x, begin apply quot.induction_on x, clear x, intro x, cases x with j x, erw colimit_smul_mk_eq, exact linear_map.map_smul (t.ι.app j) r x, end, .. (AddCommGroup.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ (Module R) AddCommGroup.{max v u})).desc ((forget₂ (Module R) AddCommGroup.{max v u}).map_cocone t) } /-- The proposed colimit cocone is a colimit in `Module R`. -/ def colimit_cocone_is_colimit : is_colimit colimit_cocone := { desc := colimit_desc, fac' := λ t j, linear_map.coe_injective $ (types.colimit_cocone_is_colimit (F ⋙ forget (Module R))).fac ((forget (Module R)).map_cocone t) j, uniq' := λ t m h, linear_map.coe_injective $ (types.colimit_cocone_is_colimit (F ⋙ forget (Module R))).uniq ((forget (Module R)).map_cocone t) m ((λ j, funext $ λ x, linear_map.congr_fun (h j) x)) } instance forget₂_AddCommGroup_preserves_filtered_colimits : preserves_filtered_colimits (forget₂ (Module R) AddCommGroup.{u}) := { preserves_filtered_colimits := λ J _ _, by exactI { preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone (colimit_cocone_is_colimit F) (AddCommGroup.filtered_colimits.colimit_cocone_is_colimit (F ⋙ forget₂ (Module.{u} R) AddCommGroup.{u})) } } instance forget_preserves_filtered_colimits : preserves_filtered_colimits (forget (Module.{u} R)) := limits.comp_preserves_filtered_colimits (forget₂ (Module R) AddCommGroup) (forget AddCommGroup) end end Module.filtered_colimits
ae0eb4b6454bf9df39ef54fed8ccb245eb1486ec
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/test/examples.lean
773d1a76e7d23c72bacf084a556b138ab1a72abb
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
5,815
lean
import tactic data.stream.basic data.set.basic data.finset data.multiset category.traversable.derive open tactic universe u variable {α : Type} example (s t u : set ℕ) (h : s ⊆ t ∩ u) (h' : u ⊆ s) : u ⊆ s → true := begin dunfold has_subset.subset has_inter.inter at *, -- trace_state, intro1, triv end example (s t u : set ℕ) (h : s ⊆ t ∩ u) (h' : u ⊆ s) : u ⊆ s → true := begin delta has_subset.subset has_inter.inter at *, -- trace_state, intro1, triv end example (x y z : ℕ) (h'' : true) (h : 0 + y = x) (h' : 0 + y = z) : x = z + 0 := begin simp at *, -- trace_state, rw [←h, ←h'] end example (x y z : ℕ) (h'' : true) (h : 0 + y = x) (h' : 0 + y = z) : x = z + 0 := begin simp at *, simp [h] at h', simp [*] end def my_id (x : α) := x def my_id_def (x : α) : my_id x = x := rfl example (x y z : ℕ) (h'' : true) (h : 0 + my_id y = x) (h' : 0 + y = z) : x = z + 0 := begin simp [my_id_def] at *, simp [h] at h', simp [*] end @[simp] theorem mem_set_of {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl -- TODO: write a tactic to unfold specific instances of generic notation? theorem subset_def {s t : set α} : (s ⊆ t) = ∀ x, x ∈ s → x ∈ t := rfl theorem union_def {s₁ s₂ : set α} : s₁ ∪ s₂ = {a | a ∈ s₁ ∨ a ∈ s₂} := rfl theorem inter_def {s₁ s₂ : set α} : s₁ ∩ s₂ = {a | a ∈ s₁ ∧ a ∈ s₂} := rfl theorem union_subset {s t r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r := begin dsimp [subset_def, union_def] at *, intros x h, cases h; back_chaining_using_hs end theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := begin dsimp [subset_def, inter_def] at *, intros x h, split; back_chaining_using_hs end /- extensionality -/ example : true := begin have : ∀ (s₀ s₁ : set ℤ), s₀ = s₁, { intros, ext1, guard_target x ∈ s₀ ↔ x ∈ s₁, admit }, have : ∀ (s₀ s₁ : finset ℕ), s₀ = s₁, { intros, ext1, guard_target a ∈ s₀ ↔ a ∈ s₁, admit }, have : ∀ (s₀ s₁ : multiset ℕ), s₀ = s₁, { intros, ext1, guard_target multiset.count a s₀ = multiset.count a s₁, admit }, have : ∀ (s₀ s₁ : list ℕ), s₀ = s₁, { intros, ext1, guard_target list.nth s₀ n = list.nth s₁ n, admit }, have : ∀ (s₀ s₁ : stream ℕ), s₀ = s₁, { intros, ext1, guard_target stream.nth n s₀ = stream.nth n s₁, admit }, have : ∀ n (s₀ s₁ : array n ℕ), s₀ = s₁, { intros, ext1, guard_target array.read s₀ i = array.read s₁ i, admit }, trivial end /- choice -/ example (h : ∀n m : ℕ, ∃i j, m = n + i ∨ m + j = n) : true := begin choose i j h using h, guard_hyp i := ℕ → ℕ → ℕ, guard_hyp j := ℕ → ℕ → ℕ, guard_hyp h := ∀ (n m : ℕ), m = n + i n m ∨ m + j n m = n, trivial end example (h : ∀n m : ℕ, ∃i, ∀n:ℕ, ∃j, m = n + i ∨ m + j = n) : true := begin choose i j h using h, guard_hyp i := ℕ → ℕ → ℕ, guard_hyp j := ℕ → ℕ → ℕ → ℕ, guard_hyp h := ∀ (n m k : ℕ), m = k + i n m ∨ m + j n m k = k, trivial end -- Test `simp only [exists_prop]` gets applied after choosing. -- Because of this simp, we need a non-rfl goal example (h : ∀ n, ∃ k ≥ 0, n = k) : ∀ x : ℕ, 1 = 1 := begin choose u hu using h, guard_hyp hu := ∀ n, u n ≥ 0 ∧ n = u n, intro, refl end /- refine_struct -/ section refine_struct variables {α} [_inst : monoid α] include _inst example : true := begin have : group α, { refine_struct { .._inst }, guard_tags _field inv group, admit, guard_tags _field mul_left_inv group, admit, }, trivial end end refine_struct meta example : true := begin success_if_fail { let := compact_relation }, trivial end import_private compact_relation from tactic.coinduction meta example : true := begin let := compact_relation, trivial end meta example : true := begin success_if_fail { let := elim_gen_sum_aux }, trivial end import_private elim_gen_sum_aux meta example : true := begin let := elim_gen_sum_aux, trivial end /- traversable -/ open tactic.interactive run_cmd do lawful_traversable_derive_handler' `test ``(is_lawful_traversable) ``list -- the above creates local instances of `traversable` and `is_lawful_traversable` -- for `list` -- do not put in instances because they are not universe polymorphic @[derive [traversable, is_lawful_traversable]] structure my_struct (α : Type) := (y : ℤ) @[derive [traversable, is_lawful_traversable]] inductive either (α : Type u) | left : α → ℤ → either | right : α → either @[derive [traversable, is_lawful_traversable]] structure my_struct2 (α : Type u) : Type u := (x : α) (y : ℤ) (η : list α) (k : list (list α)) @[derive [traversable, is_lawful_traversable]] inductive rec_data3 (α : Type u) : Type u | nil : rec_data3 | cons : ℕ → α → rec_data3 → rec_data3 → rec_data3 @[derive traversable] meta structure meta_struct (α : Type u) : Type u := (x : α) (y : ℤ) (z : list α) (k : list (list α)) (w : expr) /- tests of has_sep on finset -/ example {α} (s : finset α) (p : α → Prop) [decidable_pred p] : {x ∈ s | p x} = s.filter p := by simp example {α} (s : finset α) (p : α → Prop) [decidable_pred p] : {x ∈ s | p x} = @finset.filter α p (λ _, classical.prop_decidable _) s := by simp section open_locale classical example {α} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := by simp example (n m k : ℕ) : {x ∈ finset.range n | x < m ∨ x < k } = {x ∈ finset.range n | x < m } ∪ {x ∈ finset.range n | x < k } := by simp [finset.filter_or] end
5332c94a7be302c8eea00ed1b85b7eb37f654fef
bdd56e6eb0f467437e368d613de75299495d4054
/src/data/dvector.lean
06f128328204a40a381db4e91eb0f3bbc9c53a80
[]
no_license
truong111000/formalabstracts
49a04c268ccee136e48e24e9d5dcb6fedea4b53e
93a89a5c05c6fbc23eb9b914b60dcc353e609cd2
refs/heads/master
1,589,551,767,824
1,555,708,723,000
1,555,708,723,000
182,326,292
0
0
null
1,555,708,332,000
1,555,708,331,000
null
UTF-8
Lean
false
false
10,613
lean
import tactic.tidy data.pnat universes u v w @[derive decidable_eq]inductive dvector (α : Type u) : ℕ → Type u | nil {} : dvector 0 | cons : ∀{n} (x : α) (xs : dvector n), dvector (n+1) @[derive decidable_eq]inductive dfin : ℕ → Type | fz {n} : dfin (n+1) | fs {n} : dfin n → dfin (n+1) namespace dfin @[simp] def to_nat : ∀ {n}, dfin n → ℕ | _ fz := 0 | _ (fs n) := n.to_nat + 1 theorem to_nat_lt : ∀ {n} (x : dfin n), x.to_nat < n | _ fz := nat.succ_pos _ | _ (fs n) := nat.succ_lt_succ (to_nat_lt n) def to_fin : ∀ {n}, dfin n → fin n | 0 x := by {cases x} | (n+1) x := ⟨to_nat x, by apply to_nat_lt⟩ @[extensionality] theorem to_nat_inj {n} {x y : dfin n} (e : x.to_nat = y.to_nat) : x = y := by induction x; cases y; injection e with e; [refl, rw x_ih e] def raise : ∀ {n}, dfin n → dfin (n+1) | _ fz := fz | _ (fs n) := fs n.raise def elim0 {α : Sort*} : dfin 0 → α. def cast_le : ∀ {m n}, n ≤ m → dfin n → dfin m | 0 n h x := false.elim (by cases h; cases x) | (m+1) _ _ fz := fz | (m+1) _ h (@fs n s) := fs (s.cast_le (nat.le_of_succ_le_succ h)) @[simp] theorem cast_le_to_nat : ∀ {m n} (h : n ≤ m) (x : dfin n), (x.cast_le h).to_nat = x.to_nat | 0 n h x := false.elim (by cases h; cases x) | (m+1) _ _ fz := rfl | (m+1) _ h (@fs n s) := congr_arg (+1) (cast_le_to_nat _ _) theorem cast_le_rfl {n} (x : dfin n) : x.cast_le (le_refl _) = x := to_nat_inj (by simp) def last : ∀ n, dfin (n+1) | 0 := fz | (n+1) := fs (last n) def of_nat : ∀ n, ℕ → option (dfin n) | 0 m := none | (n+1) 0 := some fz | (n+1) (m+1) := fs <$> of_nat n m def of_nat_lt : ∀ {n} m, m < n → dfin n | 0 m h := (nat.not_lt_zero _ h).elim | (n+1) 0 _ := fz | (n+1) (m+1) h := fs (of_nat_lt m (nat.lt_of_succ_lt_succ h)) @[simp] theorem of_nat_lt_to_nat : ∀ {n m} (h : m < n), (of_nat_lt m h).to_nat = m | 0 m h := (nat.not_lt_zero _ h).elim | (n+1) 0 _ := rfl | (n+1) (m+1) h := congr_arg (+1) (of_nat_lt_to_nat (nat.lt_of_succ_lt_succ h)) def of_fin : ∀{n}, fin n → dfin n | n ⟨val, h⟩ := of_nat_lt val h meta instance reflect : ∀ n, has_reflect (dfin n) | _ fz := `(fz) | _ (fs n) := `(fs).subst (reflect _ n) def fz' : Π{n : ℕ+}, dfin n | ⟨0, h⟩ := false.elim (lt_irrefl 0 h) | ⟨k+1, h⟩ := fz end dfin meta def tactic.interactive.to_dfin (m : ℕ) : tactic unit := do n ← do { `(dfin %%n) ← tactic.target | return (m+1), tactic.eval_expr ℕ n }, m ← dfin.of_nat n m, tactic.exact (reflect m) namespace dfin instance has_zero_dfin {n} : has_zero $ dfin (n+1) := ⟨fz⟩ instance has_one_dfin : ∀ {n}, has_one (dfin (nat.succ n)) | 0 := ⟨fz⟩ | (n+1) := ⟨fs fz⟩ instance has_zero_dfin_pnat {n : ℕ+} : has_zero (dfin n) := ⟨fz'⟩ instance has_one_dfin_pnat : Π{n : ℕ+}, has_one (dfin n) | ⟨0, h⟩ := false.elim (lt_irrefl 0 h) | ⟨1, h⟩ := ⟨fz⟩ | ⟨n+2, h⟩ := ⟨fs fz⟩ instance has_add_dfin {n} : has_add (dfin(n)) := ⟨λ x y, of_fin $ (to_fin x) + (to_fin y)⟩ def max : ∀{n}, dfin n → dfin n → dfin n | 0 x y := by cases x | (n+1) (fz) y := y | (n+1) x (fz) := x | (n+1) (fs k₁) (fs k₂) := fs $ max k₁ k₂ end dfin local notation h :: t := dvector.cons h t local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l namespace dvector variables {α : Type u} {β : Type v} {γ : Type w} {n : ℕ} @[simp] protected def zero_eq : ∀(xs : dvector α 0), xs = [] | [] := rfl @[simp] protected def concat : ∀{n : ℕ} (xs : dvector α n) (x : α), dvector α (n+1) | _ [] x' := [x'] | _ (x::xs) x' := x::concat xs x' @[simp] protected def nth : ∀{n : ℕ} (xs : dvector α n) (m : ℕ) (h : m < n), α | _ [] m h := by exfalso; exact nat.not_lt_zero m h | _ (x::xs) 0 h := x | _ (x::xs) (m+1) h := nth xs m (lt_of_add_lt_add_right h) @[reducible, simp] protected def last {n : ℕ} (xs : dvector α (n+1)) : α := xs.nth n (by {repeat{constructor}}) protected def nth' {n : ℕ} (xs : dvector α n) (m : fin n) : α := xs.nth m.1 m.2 protected def nth'' : ∀ {n : ℕ} (xs : dvector α n) (m : dfin n), α | _ (x::xs) dfin.fz := x | _ (x::xs) (dfin.fs (m)) := nth'' xs m protected def mem : ∀{n : ℕ} (x : α) (xs : dvector α n), Prop | _ x [] := false | _ x (x'::xs) := x = x' ∨ mem x xs instance {n : ℕ} : has_mem α (dvector α n) := ⟨dvector.mem⟩ protected def to_set : ∀{n : ℕ}, dvector α n → set α := λ n xs, λ a, dvector.mem a xs protected def pmem : ∀{n : ℕ} (x : α) (xs : dvector α n), Type | _ x [] := empty | _ x (x'::xs) := psum (x = x') (pmem x xs) protected def mem_of_pmem : ∀{n : ℕ} {x : α} {xs : dvector α n} (hx : xs.pmem x), x ∈ xs | _ x [] hx := by cases hx | _ x (x'::xs) hx := by cases hx;[exact or.inl hx, exact or.inr (mem_of_pmem hx)] @[simp] protected def map (f : α → β) : ∀{n : ℕ}, dvector α n → dvector β n | _ [] := [] | _ (x::xs) := f x :: map xs @[simp] protected lemma map_id : ∀{n : ℕ} (xs : dvector α n), xs.map (λx, x) = xs | _ [] := rfl | _ (x::xs) := by dsimp; simp* @[simp] protected lemma map_congr_pmem {f g : α → β} : ∀{n : ℕ} {xs : dvector α n} (h : ∀x, xs.pmem x → f x = g x), xs.map f = xs.map g | _ [] h := rfl | _ (x::xs) h := begin dsimp, congr' 1, exact h x (psum.inl rfl), apply map_congr_pmem, intros x hx, apply h, right, exact hx end @[simp] protected lemma map_congr_mem {f g : α → β} {n : ℕ} {xs : dvector α n} (h : ∀x, x ∈ xs → f x = g x) : xs.map f = xs.map g := dvector.map_congr_pmem $ λx hx, h x $ dvector.mem_of_pmem hx @[simp] protected lemma map_congr {f g : α → β} (h : ∀x, f x = g x) : ∀{n : ℕ} (xs : dvector α n), xs.map f = xs.map g | _ [] := rfl | _ (x::xs) := by dsimp; simp* @[simp] protected lemma map_map (g : β → γ) (f : α → β): ∀{n : ℕ} (xs : dvector α n), (xs.map f).map g = xs.map (λx, g (f x)) | _ [] := rfl | _ (x::xs) := by dsimp; simp* protected lemma map_inj {f : α → β} (hf : ∀{{x x'}}, f x = f x' → x = x') {n : ℕ} {xs xs' : dvector α n} (h : xs.map f = xs'.map f) : xs = xs' := begin induction xs; cases xs', refl, simp at h, congr;[apply hf, apply xs_ih]; simp [h] end @[simp] protected lemma map_concat (f : α → β) : ∀{n : ℕ} (xs : dvector α n) (x : α), (xs.concat x).map f = (xs.map f).concat (f x) | _ [] x' := by refl | _ (x::xs) x' := by dsimp; congr' 1; exact map_concat xs x' @[simp] protected lemma map_nth (f : α → β) : ∀{n : ℕ} (xs : dvector α n) (m : ℕ) (h : m < n), (xs.map f).nth m h = f (xs.nth m h) | _ [] m h := by exfalso; exact nat.not_lt_zero m h | _ (x::xs) 0 h := by refl | _ (x::xs) (m+1) h := by exact map_nth xs m _ protected lemma concat_nth : ∀{n : ℕ} (xs : dvector α n) (x : α) (m : ℕ) (h' : m < n+1) (h : m < n), (xs.concat x).nth m h' = xs.nth m h | _ [] x' m h' h := by exfalso; exact nat.not_lt_zero m h | _ (x::xs) x' 0 h' h := by refl | _ (x::xs) x' (m+1) h' h := by dsimp; exact concat_nth xs x' m _ _ @[simp] protected lemma concat_nth_last : ∀{n : ℕ} (xs : dvector α n) (x : α) (h : n < n+1), (xs.concat x).nth n h = x | _ [] x' h := by refl | _ (x::xs) x' h := by dsimp; exact concat_nth_last xs x' _ @[simp] protected lemma concat_nth_last' : ∀{n : ℕ} (xs : dvector α n) (x : α) (h : n < n+1), (xs.concat x).last = x := by apply dvector.concat_nth_last @[simp] protected def append : ∀{n m : ℕ} (xs : dvector α n) (xs' : dvector α m), dvector α (m+n) | _ _ [] xs := xs | _ _ (x'::xs) xs' := x'::append xs xs' @[simp]protected def insert : ∀{n : ℕ} (x : α) (k : ℕ) (xs : dvector α n), dvector α (n+1) | n x 0 xs := (x::xs) | 0 x k xs := (x::xs) | (n+1) x (k+1) (y::ys) := (y::insert x k ys) @[simp] protected lemma insert_at_zero : ∀{n : ℕ} (x : α) (xs : dvector α n), dvector.insert x 0 xs = (x::xs) := by {intros, induction n; refl} -- why doesn't {intros, refl} work? @[simp] protected lemma insert_nth : ∀{n : ℕ} (x : α) (k : ℕ) (xs : dvector α n) (h : k < n+1), (dvector.insert x k xs).nth k h = x | 0 x k xs h := by {cases h, refl, exfalso, apply nat.not_lt_zero, exact h_a} | n x 0 xs h := by {induction n, refl, simp*} | (n+1) x (k+1) (y::ys) h := by simp* protected lemma insert_cons {n k} {x y : α} {v : dvector α n} : (x::(v.insert y k)) = (x::v).insert y (k+1) := by {induction v, refl, simp*} /- Given a proof that n ≤ m, return the nth initial segment of -/ @[simp]protected def trunc : ∀ (n) {m : ℕ} (h : n ≤ m) (xs : dvector α m), dvector α n | 0 0 _ xs := [] | 0 (m+1) _ xs := [] | (n+1) 0 _ xs := by {exfalso, cases _x} | (n+1) (m+1) h (x::xs) := (x::@trunc n m (by simp at h; exact h) xs) @[simp]protected lemma trunc_n_n {n : ℕ} {h : n ≤ n} {v : dvector α n} : dvector.trunc n h v = v := by {induction v, refl, solve_by_elim} @[simp]protected lemma trunc_0_n {n : ℕ} {h : 0 ≤ n} {v : dvector α n} : dvector.trunc 0 h v = [] := by {induction v, refl, simp} @[simp]protected lemma trunc_nth {n m l: ℕ} {h : n ≤ m} {h' : l < n} {v : dvector α m} : (v.trunc n h).nth l h' = v.nth l (lt_of_lt_of_le h' h) := begin induction m generalizing n l, have : n = 0, by cases h; simp, subst this, cases h', cases n; cases l, {cases h'}, {cases h'}, {cases v, refl}, cases v, simp only [m_ih, dvector.nth, dvector.trunc] end protected lemma nth_irrel1 : ∀{n k : ℕ} {h : k < n + 1} {h' : k < n + 1 + 1} (v : dvector α (n+1)) (x : α), (x :: (v.trunc n (nat.le_succ n))).nth k h = (x::v).nth k h' := by {intros, apply @dvector.trunc_nth _ _ _ _ (by {simp, exact dec_trivial}) h (x::v)} protected def cast {n m} (p : n = m) : dvector α n → dvector α m := by subst p; exact id @[simp] protected lemma cast_irrel {n m} {p p' : n = m} {v : dvector α n} : v.cast p = v.cast p' := by refl @[simp] protected lemma cast_rfl {n m} {p : n = m} {q : m = n} {v : dvector α n} : (v.cast p).cast q = v := by {subst p, refl} protected lemma cast_hrfl {n m} {p : n = m} {v : dvector α n} : v.cast p == v := by subst p; refl @[simp] protected lemma cast_trans {n m o} {p : n = m} {q : m = o} {v : dvector α n} : (v.cast p).cast q = v.cast (trans p q) := by subst p; subst q; refl @[simp] protected def remove_mth : ∀ {n : ℕ} (m : ℕ) (xs : dvector α (n+1)) , dvector α (n) | 0 _ _ := dvector.nil | n 0 (dvector.cons y ys) := ys | (n+1) (k+1) (dvector.cons y ys) := dvector.cons y (remove_mth k ys) end dvector
7246ea8a8685037dd268148ec9f4da47b1c61426
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/algebra/ring/pi.lean
6d41616e06a7771f7ddc85032d0ebba40939323f
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
3,001
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import tactic.pi_instances import algebra.group.pi import algebra.ring.basic /-! # Pi instances for ring This file defines instances for ring, semiring and related structures on Pi Types -/ namespace pi universes u v w variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) instance mul_zero_class [Π i, mul_zero_class $ f i] : mul_zero_class (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field instance distrib [Π i, distrib $ f i] : distrib (Π i : I, f i) := by refine_struct { add := (+), mul := (*), .. }; tactic.pi_instance_derive_field instance semiring [∀ i, semiring $ f i] : semiring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), .. }; tactic.pi_instance_derive_field instance comm_semiring [∀ i, comm_semiring $ f i] : comm_semiring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), .. }; tactic.pi_instance_derive_field instance ring [∀ i, ring $ f i] : ring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), neg := has_neg.neg, .. }; tactic.pi_instance_derive_field instance comm_ring [∀ i, comm_ring $ f i] : comm_ring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), neg := has_neg.neg, .. }; tactic.pi_instance_derive_field /-- A family of ring homomorphisms `f a : γ →+* β a` defines a ring homomorphism `pi.ring_hom f : γ →+* Π a, β a` given by `pi.ring_hom f x b = f b x`. -/ protected def ring_hom {α : Type u} {β : α → Type v} [R : Π a : α, semiring (β a)] {γ : Type w} [semiring γ] (f : Π a : α, γ →+* β a) : γ →+* Π a, β a := { to_fun := λ x b, f b x, map_add' := λ x y, funext $ λ z, (f z).map_add x y, map_mul' := λ x y, funext $ λ z, (f z).map_mul x y, map_one' := funext $ λ z, (f z).map_one, map_zero' := funext $ λ z, (f z).map_zero } @[simp] lemma ring_hom_apply {α : Type u} {β : α → Type v} [R : Π a : α, semiring (β a)] {γ : Type w} [semiring γ] (f : Π a : α, γ →+* β a) (g) (a) : pi.ring_hom f g a = f a g := rfl end pi section ring_hom variable {I : Type*} -- The indexing type variable (f : I → Type*) -- The family of types already equipped with instances variables [Π i, semiring (f i)] /-- Evaluation of functions into an indexed collection of monoids at a point is a monoid homomorphism. -/ def ring_hom.apply (i : I) : (Π i, f i) →+* f i := { ..(monoid_hom.apply f i), ..(add_monoid_hom.apply f i) } @[simp] lemma ring_hom.apply_apply (i : I) (g : Π i, f i) : (ring_hom.apply f i) g = g i := rfl end ring_hom
22e78bb21ff9d3cd6f0e9151ba459c7bc23d70da
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/calculus/local_extr.lean
4cdcfeacb43e70de0899b887468d20f10baaec90
[ "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
20,797
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.calculus.deriv.polynomial import topology.algebra.order.extend_from import topology.algebra.polynomial /-! # Local extrema of smooth functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions In a real normed space `E` we define `pos_tangent_cone_at (s : set E) (x : E)`. This would be the same as `tangent_cone_at ℝ≥0 s x` if we had a theory of normed semifields. This set is used in the proof of Fermat's Theorem (see below), and can be used to formalize [Lagrange multipliers](https://en.wikipedia.org/wiki/Lagrange_multiplier) and/or [Karush–Kuhn–Tucker conditions](https://en.wikipedia.org/wiki/Karush–Kuhn–Tucker_conditions). ## Main statements For each theorem name listed below, we also prove similar theorems for `min`, `extr` (if applicable)`, and `(f)deriv` instead of `has_fderiv`. * `is_local_max_on.has_fderiv_within_at_nonpos` : `f' y ≤ 0` whenever `a` is a local maximum of `f` on `s`, `f` has derivative `f'` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`. * `is_local_max_on.has_fderiv_within_at_eq_zero` : In the settings of the previous theorem, if both `y` and `-y` belong to the positive tangent cone, then `f' y = 0`. * `is_local_max.has_fderiv_at_eq_zero` : [Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points)), the derivative of a differentiable function at a local extremum point equals zero. * `exists_has_deriv_at_eq_zero` : [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem): given a function `f` continuous on `[a, b]` and differentiable on `(a, b)`, there exists `c ∈ (a, b)` such that `f' c = 0`. ## Implementation notes For each mathematical fact we prove several versions of its formalization: * for maxima and minima; * using `has_fderiv*`/`has_deriv*` or `fderiv*`/`deriv*`. For the `fderiv*`/`deriv*` versions we omit the differentiability condition whenever it is possible due to the fact that `fderiv` and `deriv` are defined to be zero for non-differentiable functions. ## References * [Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points)); * [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem); * [Tangent cone](https://en.wikipedia.org/wiki/Tangent_cone); ## Tags local extremum, Fermat's Theorem, Rolle's Theorem -/ universes u v open filter set open_locale topology classical polynomial section module variables {E : Type u} [normed_add_comm_group E] [normed_space ℝ E] {f : E → ℝ} {a : E} {f' : E →L[ℝ] ℝ} /-- "Positive" tangent cone to `s` at `x`; the only difference from `tangent_cone_at` is that we require `c n → ∞` instead of `‖c n‖ → ∞`. One can think about `pos_tangent_cone_at` as `tangent_cone_at nnreal` but we have no theory of normed semifields yet. -/ def pos_tangent_cone_at (s : set E) (x : E) : set E := {y : E | ∃(c : ℕ → ℝ) (d : ℕ → E), (∀ᶠ n in at_top, x + d n ∈ s) ∧ (tendsto c at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))} lemma pos_tangent_cone_at_mono : monotone (λ s, pos_tangent_cone_at s a) := begin rintros s t hst y ⟨c, d, hd, hc, hcd⟩, exact ⟨c, d, mem_of_superset hd $ λ h hn, hst hn, hc, hcd⟩ end lemma mem_pos_tangent_cone_at_of_segment_subset {s : set E} {x y : E} (h : segment ℝ x y ⊆ s) : y - x ∈ pos_tangent_cone_at s x := begin let c := λn:ℕ, (2:ℝ)^n, let d := λn:ℕ, (c n)⁻¹ • (y-x), refine ⟨c, d, filter.univ_mem' (λn, h _), tendsto_pow_at_top_at_top_of_one_lt one_lt_two, _⟩, show x + d n ∈ segment ℝ x y, { rw segment_eq_image', refine ⟨(c n)⁻¹, ⟨_, _⟩, rfl⟩, exacts [inv_nonneg.2 (pow_nonneg zero_le_two _), inv_le_one (one_le_pow_of_one_le one_le_two _)] }, show tendsto (λ n, c n • d n) at_top (𝓝 (y - x)), { convert tendsto_const_nhds, ext n, simp only [d, smul_smul], rw [mul_inv_cancel, one_smul], exact pow_ne_zero _ two_ne_zero } end lemma mem_pos_tangent_cone_at_of_segment_subset' {s : set E} {x y : E} (h : segment ℝ x (x + y) ⊆ s) : y ∈ pos_tangent_cone_at s x := by simpa only [add_sub_cancel'] using mem_pos_tangent_cone_at_of_segment_subset h lemma pos_tangent_cone_at_univ : pos_tangent_cone_at univ a = univ := eq_univ_of_forall $ λ x, mem_pos_tangent_cone_at_of_segment_subset' (subset_univ _) /-- If `f` has a local max on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.has_fderiv_within_at_nonpos {s : set E} (h : is_local_max_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : f' y ≤ 0 := begin rcases hy with ⟨c, d, hd, hc, hcd⟩, have hc' : tendsto (λ n, ‖c n‖) at_top at_top, from tendsto_at_top_mono (λ n, le_abs_self _) hc, refine le_of_tendsto (hf.lim at_top hd hc' hcd) _, replace hd : tendsto (λ n, a + d n) at_top (𝓝[s] (a + 0)), from tendsto_inf.2 ⟨tendsto_const_nhds.add (tangent_cone_at.lim_zero _ hc' hcd), by rwa tendsto_principal⟩, rw [add_zero] at hd, replace h : ∀ᶠ n in at_top, f (a + d n) ≤ f a, from mem_map.1 (hd h), replace hc : ∀ᶠ n in at_top, 0 ≤ c n, from mem_map.1 (hc (mem_at_top (0:ℝ))), filter_upwards [h, hc], simp only [smul_eq_mul, mem_preimage, subset_def], assume n hnf hn, exact mul_nonpos_of_nonneg_of_nonpos hn (sub_nonpos.2 hnf) end /-- If `f` has a local max on `s` at `a` and `y` belongs to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.fderiv_within_nonpos {s : set E} (h : is_local_max_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y ≤ 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_nonpos hf.has_fderiv_within_at hy else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.has_fderiv_within_at_eq_zero {s : set E} (h : is_local_max_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : f' y = 0 := le_antisymm (h.has_fderiv_within_at_nonpos hf hy) $ by simpa using h.has_fderiv_within_at_nonpos hf hy' /-- If `f` has a local max on `s` at `a` and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y = 0`. -/ lemma is_local_max_on.fderiv_within_eq_zero {s : set E} (h : is_local_max_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y = 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_eq_zero hf.has_fderiv_within_at hy hy' else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- If `f` has a local min on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`, then `0 ≤ f' y`. -/ lemma is_local_min_on.has_fderiv_within_at_nonneg {s : set E} (h : is_local_min_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : 0 ≤ f' y := by simpa using h.neg.has_fderiv_within_at_nonpos hf.neg hy /-- If `f` has a local min on `s` at `a` and `y` belongs to the positive tangent cone of `s` at `a`, then `0 ≤ f' y`. -/ lemma is_local_min_on.fderiv_within_nonneg {s : set E} (h : is_local_min_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : (0:ℝ) ≤ (fderiv_within ℝ f s a : E → ℝ) y := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_nonneg hf.has_fderiv_within_at hy else by { rw [fderiv_within_zero_of_not_differentiable_within_at hf], refl } /-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_min_on.has_fderiv_within_at_eq_zero {s : set E} (h : is_local_min_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : f' y = 0 := by simpa using h.neg.has_fderiv_within_at_eq_zero hf.neg hy hy' /-- If `f` has a local min on `s` at `a` and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y = 0`. -/ lemma is_local_min_on.fderiv_within_eq_zero {s : set E} (h : is_local_min_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y = 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_eq_zero hf.has_fderiv_within_at hy hy' else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- **Fermat's Theorem**: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.has_fderiv_at_eq_zero (h : is_local_min f a) (hf : has_fderiv_at f f' a) : f' = 0 := begin ext y, apply (h.on univ).has_fderiv_within_at_eq_zero hf.has_fderiv_within_at; rw pos_tangent_cone_at_univ; apply mem_univ end /-- **Fermat's Theorem**: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.fderiv_eq_zero (h : is_local_min f a) : fderiv ℝ f a = 0 := if hf : differentiable_at ℝ f a then h.has_fderiv_at_eq_zero hf.has_fderiv_at else fderiv_zero_of_not_differentiable_at hf /-- **Fermat's Theorem**: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.has_fderiv_at_eq_zero (h : is_local_max f a) (hf : has_fderiv_at f f' a) : f' = 0 := neg_eq_zero.1 $ h.neg.has_fderiv_at_eq_zero hf.neg /-- **Fermat's Theorem**: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.fderiv_eq_zero (h : is_local_max f a) : fderiv ℝ f a = 0 := if hf : differentiable_at ℝ f a then h.has_fderiv_at_eq_zero hf.has_fderiv_at else fderiv_zero_of_not_differentiable_at hf /-- **Fermat's Theorem**: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.has_fderiv_at_eq_zero (h : is_local_extr f a) : has_fderiv_at f f' a → f' = 0 := h.elim is_local_min.has_fderiv_at_eq_zero is_local_max.has_fderiv_at_eq_zero /-- **Fermat's Theorem**: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.fderiv_eq_zero (h : is_local_extr f a) : fderiv ℝ f a = 0 := h.elim is_local_min.fderiv_eq_zero is_local_max.fderiv_eq_zero end module section real variables {f : ℝ → ℝ} {f' : ℝ} {a b : ℝ} /-- **Fermat's Theorem**: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.has_deriv_at_eq_zero (h : is_local_min f a) (hf : has_deriv_at f f' a) : f' = 0 := by simpa using continuous_linear_map.ext_iff.1 (h.has_fderiv_at_eq_zero (has_deriv_at_iff_has_fderiv_at.1 hf)) 1 /-- **Fermat's Theorem**: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.deriv_eq_zero (h : is_local_min f a) : deriv f a = 0 := if hf : differentiable_at ℝ f a then h.has_deriv_at_eq_zero hf.has_deriv_at else deriv_zero_of_not_differentiable_at hf /-- **Fermat's Theorem**: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.has_deriv_at_eq_zero (h : is_local_max f a) (hf : has_deriv_at f f' a) : f' = 0 := neg_eq_zero.1 $ h.neg.has_deriv_at_eq_zero hf.neg /-- **Fermat's Theorem**: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.deriv_eq_zero (h : is_local_max f a) : deriv f a = 0 := if hf : differentiable_at ℝ f a then h.has_deriv_at_eq_zero hf.has_deriv_at else deriv_zero_of_not_differentiable_at hf /-- **Fermat's Theorem**: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.has_deriv_at_eq_zero (h : is_local_extr f a) : has_deriv_at f f' a → f' = 0 := h.elim is_local_min.has_deriv_at_eq_zero is_local_max.has_deriv_at_eq_zero /-- **Fermat's Theorem**: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.deriv_eq_zero (h : is_local_extr f a) : deriv f a = 0 := h.elim is_local_min.deriv_eq_zero is_local_max.deriv_eq_zero end real section Rolle variables (f f' : ℝ → ℝ) {a b : ℝ} /-- A continuous function on a closed interval with `f a = f b` takes either its maximum or its minimum value at a point in the interior of the interval. -/ lemma exists_Ioo_extr_on_Icc (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, is_extr_on f (Icc a b) c := begin have ne : (Icc a b).nonempty, from nonempty_Icc.2 (le_of_lt hab), -- Consider absolute min and max points obtain ⟨c, cmem, cle⟩ : ∃ c ∈ Icc a b, ∀ x ∈ Icc a b, f c ≤ f x, from is_compact_Icc.exists_forall_le ne hfc, obtain ⟨C, Cmem, Cge⟩ : ∃ C ∈ Icc a b, ∀ x ∈ Icc a b, f x ≤ f C, from is_compact_Icc.exists_forall_ge ne hfc, by_cases hc : f c = f a, { by_cases hC : f C = f a, { have : ∀ x ∈ Icc a b, f x = f a, from λ x hx, le_antisymm (hC ▸ Cge x hx) (hc ▸ cle x hx), -- `f` is a constant, so we can take any point in `Ioo a b` rcases exists_between hab with ⟨c', hc'⟩, refine ⟨c', hc', or.inl _⟩, assume x hx, rw [mem_set_of_eq, this x hx, ← hC], exact Cge c' ⟨le_of_lt hc'.1, le_of_lt hc'.2⟩ }, { refine ⟨C, ⟨lt_of_le_of_ne Cmem.1 $ mt _ hC, lt_of_le_of_ne Cmem.2 $ mt _ hC⟩, or.inr Cge⟩, exacts [λ h, by rw h, λ h, by rw [h, hfI]] } }, { refine ⟨c, ⟨lt_of_le_of_ne cmem.1 $ mt _ hc, lt_of_le_of_ne cmem.2 $ mt _ hc⟩, or.inl cle⟩, exacts [λ h, by rw h, λ h, by rw [h, hfI]] } end /-- A continuous function on a closed interval with `f a = f b` has a local extremum at some point of the corresponding open interval. -/ lemma exists_local_extr_Ioo (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, is_local_extr f c := let ⟨c, cmem, hc⟩ := exists_Ioo_extr_on_Icc f hab hfc hfI in ⟨c, cmem, hc.is_local_extr $ Icc_mem_nhds cmem.1 cmem.2⟩ /-- **Rolle's Theorem** `has_deriv_at` version -/ lemma exists_has_deriv_at_eq_zero (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) : ∃ c ∈ Ioo a b, f' c = 0 := let ⟨c, cmem, hc⟩ := exists_local_extr_Ioo f hab hfc hfI in ⟨c, cmem, hc.has_deriv_at_eq_zero $ hff' c cmem⟩ /-- **Rolle's Theorem** `deriv` version -/ lemma exists_deriv_eq_zero (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, deriv f c = 0 := let ⟨c, cmem, hc⟩ := exists_local_extr_Ioo f hab hfc hfI in ⟨c, cmem, hc.deriv_eq_zero⟩ variables {f f'} {l : ℝ} /-- **Rolle's Theorem**, a version for a function on an open interval: if `f` has derivative `f'` on `(a, b)` and has the same limit `l` at `𝓝[>] a` and `𝓝[<] b`, then `f' c = 0` for some `c ∈ (a, b)`. -/ lemma exists_has_deriv_at_eq_zero' (hab : a < b) (hfa : tendsto f (𝓝[>] a) (𝓝 l)) (hfb : tendsto f (𝓝[<] b) (𝓝 l)) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) : ∃ c ∈ Ioo a b, f' c = 0 := begin have : continuous_on f (Ioo a b) := λ x hx, (hff' x hx).continuous_at.continuous_within_at, have hcont := continuous_on_Icc_extend_from_Ioo hab.ne this hfa hfb, obtain ⟨c, hc, hcextr⟩ : ∃ c ∈ Ioo a b, is_local_extr (extend_from (Ioo a b) f) c, { apply exists_local_extr_Ioo _ hab hcont, rw eq_lim_at_right_extend_from_Ioo hab hfb, exact eq_lim_at_left_extend_from_Ioo hab hfa }, use [c, hc], apply (hcextr.congr _).has_deriv_at_eq_zero (hff' c hc), rw eventually_eq_iff_exists_mem, exact ⟨Ioo a b, Ioo_mem_nhds hc.1 hc.2, extend_from_extends this⟩ end /-- **Rolle's Theorem**, a version for a function on an open interval: if `f` has the same limit `l` at `𝓝[>] a` and `𝓝[<] b`, then `deriv f c = 0` for some `c ∈ (a, b)`. This version does not require differentiability of `f` because we define `deriv f c = 0` whenever `f` is not differentiable at `c`. -/ lemma exists_deriv_eq_zero' (hab : a < b) (hfa : tendsto f (𝓝[>] a) (𝓝 l)) (hfb : tendsto f (𝓝[<] b) (𝓝 l)) : ∃ c ∈ Ioo a b, deriv f c = 0 := classical.by_cases (assume h : ∀ x ∈ Ioo a b, differentiable_at ℝ f x, show ∃ c ∈ Ioo a b, deriv f c = 0, from exists_has_deriv_at_eq_zero' hab hfa hfb (λ x hx, (h x hx).has_deriv_at)) (assume h : ¬∀ x ∈ Ioo a b, differentiable_at ℝ f x, have h : ∃ x, x ∈ Ioo a b ∧ ¬differentiable_at ℝ f x, by { push_neg at h, exact h }, let ⟨c, hc, hcdiff⟩ := h in ⟨c, hc, deriv_zero_of_not_differentiable_at hcdiff⟩) end Rolle namespace polynomial open_locale big_operators /-- The number of roots of a real polynomial `p` is at most the number of roots of its derivative that are not roots of `p` plus one. -/ lemma card_roots_to_finset_le_card_roots_derivative_diff_roots_succ (p : ℝ[X]) : p.roots.to_finset.card ≤ (p.derivative.roots.to_finset \ p.roots.to_finset).card + 1 := begin cases eq_or_ne p.derivative 0 with hp' hp', { rw [eq_C_of_derivative_eq_zero hp', roots_C, multiset.to_finset_zero, finset.card_empty], exact zero_le _ }, have hp : p ≠ 0, from ne_of_apply_ne derivative (by rwa [derivative_zero]), refine finset.card_le_diff_of_interleaved (λ x hx y hy hxy hxy', _), rw [multiset.mem_to_finset, mem_roots hp] at hx hy, obtain ⟨z, hz1, hz2⟩ := exists_deriv_eq_zero (λ x : ℝ, eval x p) hxy p.continuous_on (hx.trans hy.symm), refine ⟨z, _, hz1⟩, rwa [multiset.mem_to_finset, mem_roots hp', is_root, ← p.deriv] end /-- The number of roots of a real polynomial is at most the number of roots of its derivative plus one. -/ lemma card_roots_to_finset_le_derivative (p : ℝ[X]) : p.roots.to_finset.card ≤ p.derivative.roots.to_finset.card + 1 := p.card_roots_to_finset_le_card_roots_derivative_diff_roots_succ.trans $ add_le_add_right (finset.card_mono $ finset.sdiff_subset _ _) _ /-- The number of roots of a real polynomial (counted with multiplicities) is at most the number of roots of its derivative (counted with multiplicities) plus one. -/ lemma card_roots_le_derivative (p : ℝ[X]) : p.roots.card ≤ p.derivative.roots.card + 1 := calc p.roots.card = ∑ x in p.roots.to_finset, p.roots.count x : (multiset.to_finset_sum_count_eq _).symm ... = ∑ x in p.roots.to_finset, (p.roots.count x - 1 + 1) : eq.symm $ finset.sum_congr rfl $ λ x hx, tsub_add_cancel_of_le $ nat.succ_le_iff.2 $ multiset.count_pos.2 $ multiset.mem_to_finset.1 hx ... = ∑ x in p.roots.to_finset, (p.root_multiplicity x - 1) + p.roots.to_finset.card : by simp only [finset.sum_add_distrib, finset.card_eq_sum_ones, count_roots] ... ≤ ∑ x in p.roots.to_finset, p.derivative.root_multiplicity x + ((p.derivative.roots.to_finset \ p.roots.to_finset).card + 1) : add_le_add (finset.sum_le_sum $ λ x hx, root_multiplicity_sub_one_le_derivative_root_multiplicity _ _) p.card_roots_to_finset_le_card_roots_derivative_diff_roots_succ ... ≤ ∑ x in p.roots.to_finset, p.derivative.roots.count x + (∑ x in p.derivative.roots.to_finset \ p.roots.to_finset, p.derivative.roots.count x + 1) : begin simp only [← count_roots], refine add_le_add_left (add_le_add_right ((finset.card_eq_sum_ones _).trans_le _) _) _, refine finset.sum_le_sum (λ x hx, nat.succ_le_iff.2 $ _), rw [multiset.count_pos, ← multiset.mem_to_finset], exact (finset.mem_sdiff.1 hx).1 end ... = p.derivative.roots.card + 1 : begin rw [← add_assoc, ← finset.sum_union finset.disjoint_sdiff, finset.union_sdiff_self_eq_union, ← multiset.to_finset_sum_count_eq, ← finset.sum_subset (finset.subset_union_right _ _)], intros x hx₁ hx₂, simpa only [multiset.mem_to_finset, multiset.count_eq_zero] using hx₂ end /-- The number of real roots of a polynomial is at most the number of roots of its derivative plus one. -/ lemma card_root_set_le_derivative {F : Type*} [comm_ring F] [algebra F ℝ] (p : F[X]) : fintype.card (p.root_set ℝ) ≤ fintype.card (p.derivative.root_set ℝ) + 1 := by simpa only [root_set_def, finset.coe_sort_coe, fintype.card_coe, derivative_map] using card_roots_to_finset_le_derivative (p.map (algebra_map F ℝ)) end polynomial
a484c193b8e5371f227377df80bad643bd69fad2
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/algebra_tower.lean
0a0515b1a5f51ebb49bc118f3b48d8f65eda1b45
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
9,962
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.algebra.tower import Mathlib.algebra.invertible import Mathlib.linear_algebra.basis import Mathlib.ring_theory.adjoin import Mathlib.PostPort universes u v w u₁ u_1 u_2 u_3 u_4 v₁ w₁ namespace Mathlib /-! # 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. -/ namespace is_scalar_tower protected instance polynomial (R : Type u) {S : Type v} {A : Type w} [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] : is_scalar_tower R S (polynomial A) := of_algebra_map_eq fun (x : R) => congr_arg (⇑polynomial.C) (algebra_map_apply R S A x) theorem aeval_apply (R : Type u) (S : Type v) (A : Type w) [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (x : A) (p : polynomial R) : coe_fn (polynomial.aeval x) p = coe_fn (polynomial.aeval x) (polynomial.map (algebra_map R S) p) := sorry /-- 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 : Type u) (S : Type v) (A : Type w) [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (r : R) [invertible (coe_fn (algebra_map R S) r)] : invertible (coe_fn (algebra_map R A) r) := invertible.copy (invertible.map (↑(algebra_map S A)) (coe_fn (algebra_map R S) r)) (coe_fn (algebra_map R A) r) sorry /-- A natural number that is invertible when coerced to `R` is also invertible when coerced to any `R`-algebra. -/ def invertible_algebra_coe_nat (R : Type u) (A : Type w) [comm_semiring R] [semiring A] [algebra R A] (n : ℕ) [inv : invertible ↑n] : invertible ↑n := invertible.algebra_tower ℕ R A n theorem algebra_map_aeval (R : Type u) (A : Type w) (B : Type u₁) [comm_semiring R] [comm_semiring A] [comm_semiring B] [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B] (x : A) (p : polynomial R) : coe_fn (algebra_map A B) (coe_fn (polynomial.aeval x) p) = coe_fn (polynomial.aeval (coe_fn (algebra_map A B) x)) p := sorry theorem aeval_eq_zero_of_aeval_algebra_map_eq_zero (R : Type u) (A : Type w) (B : Type u₁) [comm_semiring R] [comm_semiring A] [comm_semiring B] [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B] {x : A} {p : polynomial R} (h : function.injective ⇑(algebra_map A B)) (hp : coe_fn (polynomial.aeval (coe_fn (algebra_map A B) x)) p = 0) : coe_fn (polynomial.aeval x) p = 0 := sorry theorem aeval_eq_zero_of_aeval_algebra_map_eq_zero_field {R : Type u_1} {A : Type u_2} {B : Type u_3} [comm_semiring R] [field A] [comm_semiring B] [nontrivial B] [algebra R A] [algebra R B] [algebra A B] [is_scalar_tower R A B] {x : A} {p : polynomial R} (h : coe_fn (polynomial.aeval (coe_fn (algebra_map A B) x)) p = 0) : coe_fn (polynomial.aeval x) p = 0 := aeval_eq_zero_of_aeval_algebra_map_eq_zero R A B (ring_hom.injective (algebra_map A B)) h 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 (comap R S A)) '' s) = subalgebra.map (adjoin R s) (to_comap R S A) := sorry 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) := sorry theorem adjoin_res (C : Type u_1) (D : Type u_2) (E : Type u_3) [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) : subalgebra.res C (adjoin D S) = subalgebra.under (subalgebra.map ⊤ (is_scalar_tower.to_alg_hom C D E)) (adjoin (↥(subalgebra.map ⊤ (is_scalar_tower.to_alg_hom C D E))) S) := sorry theorem adjoin_res_eq_adjoin_res (C : Type u_1) (D : Type u_2) (E : Type u_3) (F : Type u_4) [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 : adjoin C S = ⊤) (hT : adjoin C T = ⊤) : subalgebra.res C (adjoin E (⇑(algebra_map D F) '' S)) = subalgebra.res C (adjoin D (⇑(algebra_map E F) '' T)) := sorry end algebra namespace subalgebra @[simp] theorem aeval_coe (R : Type u) {A : Type w} [comm_semiring R] [comm_semiring A] [algebra R A] {S : subalgebra R A} {x : ↥S} {p : polynomial R} : coe_fn (polynomial.aeval ↑x) p = ↑(coe_fn (polynomial.aeval x) p) := Eq.symm (is_scalar_tower.algebra_map_aeval R (↥S) A x p) end subalgebra theorem algebra.fg_trans' {R : Type u_1} {S : Type u_2} {A : Type u_3} [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.fg ⊤) (hSA : subalgebra.fg ⊤) : subalgebra.fg ⊤ := sorry theorem linear_independent_smul {R : Type u} {S : Type v} {A : Type w} [comm_ring R] [ring S] [add_comm_group A] [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] {ι : Type v₁} {b : ι → S} {ι' : Type w₁} {c : ι' → A} (hb : linear_independent R b) (hc : linear_independent S c) : linear_independent R fun (p : ι × ι') => b (prod.fst p) • c (prod.snd p) := sorry theorem is_basis.smul {R : Type u} {S : Type v} {A : Type w} [comm_ring R] [ring S] [add_comm_group A] [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] {ι : Type v₁} {b : ι → S} {ι' : Type w₁} {c : ι' → A} (hb : is_basis R b) (hc : is_basis S c) : is_basis R fun (p : ι × ι') => b (prod.fst p) • c (prod.snd p) := sorry theorem is_basis.smul_repr {R : Type u} {S : Type v} {A : Type w} [comm_ring R] [ring S] [add_comm_group A] [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] {ι : Type u_1} {ι' : Type u_2} {b : ι → S} {c : ι' → A} (hb : is_basis R b) (hc : is_basis S c) (x : A) (ij : ι × ι') : coe_fn (coe_fn (is_basis.repr (is_basis.smul hb hc)) x) ij = coe_fn (coe_fn (is_basis.repr hb) (coe_fn (coe_fn (is_basis.repr hc) x) (prod.snd ij))) (prod.fst ij) := sorry theorem is_basis.smul_repr_mk {R : Type u} {S : Type v} {A : Type w} [comm_ring R] [ring S] [add_comm_group A] [algebra R S] [module S A] [module R A] [is_scalar_tower R S A] {ι : Type u_1} {ι' : Type u_2} {b : ι → S} {c : ι' → A} (hb : is_basis R b) (hc : is_basis S c) (x : A) (i : ι) (j : ι') : coe_fn (coe_fn (is_basis.repr (is_basis.smul hb hc)) x) (i, j) = coe_fn (coe_fn (is_basis.repr hb) (coe_fn (coe_fn (is_basis.repr hc) x) j)) i := sorry theorem exists_subalgebra_of_fg (A : Type w) (B : Type u₁) (C : Type u_1) [comm_ring A] [comm_ring B] [comm_ring C] [algebra A B] [algebra B C] [algebra A C] [is_scalar_tower A B C] (hAC : subalgebra.fg ⊤) (hBC : submodule.fg ⊤) : ∃ (B₀ : subalgebra A B), subalgebra.fg B₀ ∧ submodule.fg ⊤ := sorry /-- 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 (A : Type w) (B : Type u₁) (C : Type u_1) [comm_ring A] [comm_ring B] [comm_ring C] [algebra A B] [algebra B C] [algebra A C] [is_scalar_tower A B C] [is_noetherian_ring A] (hAC : subalgebra.fg ⊤) (hBC : submodule.fg ⊤) (hBCi : function.injective ⇑(algebra_map B C)) : subalgebra.fg ⊤ := sorry /-- Restrict the domain of an `alg_hom`. -/ def alg_hom.restrict_domain {A : Type w} (B : Type u₁) {C : Type u_1} {D : Type u_2} [comm_semiring A] [comm_semiring C] [comm_semiring D] [algebra A C] [algebra A D] (f : alg_hom A C D) [comm_semiring B] [algebra A B] [algebra B C] [is_scalar_tower A B C] : alg_hom A B D := alg_hom.comp f (is_scalar_tower.to_alg_hom A B C) /-- Extend the scalars of an `alg_hom`. -/ def alg_hom.extend_scalars {A : Type w} (B : Type u₁) {C : Type u_1} {D : Type u_2} [comm_semiring A] [comm_semiring C] [comm_semiring D] [algebra A C] [algebra A D] (f : alg_hom A C D) [comm_semiring B] [algebra A B] [algebra B C] [is_scalar_tower A B C] : alg_hom B C D := alg_hom.mk (alg_hom.to_fun f) sorry sorry sorry sorry sorry /-- `alg_hom`s from the top of a tower are equivalent to a pair of `alg_hom`s. -/ def alg_hom_equiv_sigma {A : Type w} {B : Type u₁} {C : Type u_1} {D : Type u_2} [comm_semiring A] [comm_semiring C] [comm_semiring D] [algebra A C] [algebra A D] [comm_semiring B] [algebra A B] [algebra B C] [is_scalar_tower A B C] : alg_hom A C D ≃ sigma fun (f : alg_hom A B D) => alg_hom B C D := equiv.mk (fun (f : alg_hom A C D) => sigma.mk (alg_hom.restrict_domain B f) (alg_hom.extend_scalars B f)) (fun (fg : sigma fun (f : alg_hom A B D) => alg_hom B C D) => is_scalar_tower.restrict_base A (sigma.snd fg)) sorry sorry
449231be22eef4e507223ad4468f001420161688
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/hom/units.lean
c6e5cc49872881a96c5ad6d7b953835b12439ae9
[ "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
13,935
lean
/- Copyright (c) 2018 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Chris Hughes, Kevin Buzzard -/ import algebra.hom.group import algebra.group.units /-! # Monoid homomorphisms and units > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file allows to lift monoid homomorphisms to group homomorphisms of their units subgroups. It also contains unrelated results about `units` that depend on `monoid_hom`. ## Main declarations * `units.map`: Turn an homomorphism from `α` to `β` monoids into an homomorphism from `αˣ` to `βˣ`. * `monoid_hom.to_hom_units`: Turn an homomorphism from a group `α` to `β` into an homomorphism from `α` to `βˣ`. ## TODO The results that don't mention homomorphisms should be proved (earlier?) in a different file and be used to golf the basic `group` lemmas. -/ open function universes u v w @[to_additive] lemma group.is_unit {G} [group G] (g : G) : is_unit g := ⟨⟨g, g⁻¹, mul_inv_self g, inv_mul_self g⟩, rfl⟩ section monoid_hom_class /-- If two homomorphisms from a division monoid to a monoid are equal at a unit `x`, then they are equal at `x⁻¹`. -/ @[to_additive "If two homomorphisms from a subtraction monoid to an additive monoid are equal at an additive unit `x`, then they are equal at `-x`."] lemma is_unit.eq_on_inv {F G N} [division_monoid G] [monoid N] [monoid_hom_class F G N] {x : G} (hx : is_unit x) (f g : F) (h : f x = g x) : f x⁻¹ = g x⁻¹ := left_inv_eq_right_inv (map_mul_eq_one f hx.inv_mul_cancel) $ h.symm ▸ map_mul_eq_one g $ hx.mul_inv_cancel /-- If two homomorphism from a group to a monoid are equal at `x`, then they are equal at `x⁻¹`. -/ @[to_additive "If two homomorphism from an additive group to an additive monoid are equal at `x`, then they are equal at `-x`." ] lemma eq_on_inv {F G M} [group G] [monoid M] [monoid_hom_class F G M] (f g : F) {x : G} (h : f x = g x) : f x⁻¹ = g x⁻¹ := (group.is_unit x).eq_on_inv f g h end monoid_hom_class namespace units variables {α : Type*} {M : Type u} {N : Type v} {P : Type w} [monoid M] [monoid N] [monoid P] /-- The group homomorphism on units induced by a `monoid_hom`. -/ @[to_additive "The `add_group` homomorphism on `add_unit`s induced by an `add_monoid_hom`."] def map (f : M →* N) : Mˣ →* Nˣ := monoid_hom.mk' (λ u, ⟨f u.val, f u.inv, by rw [← f.map_mul, u.val_inv, f.map_one], by rw [← f.map_mul, u.inv_val, f.map_one]⟩) (λ x y, ext (f.map_mul x y)) @[simp, to_additive] lemma coe_map (f : M →* N) (x : Mˣ) : ↑(map f x) = f x := rfl @[simp, to_additive] lemma coe_map_inv (f : M →* N) (u : Mˣ) : ↑(map f u)⁻¹ = f ↑u⁻¹ := rfl @[simp, to_additive] lemma map_comp (f : M →* N) (g : N →* P) : map (g.comp f) = (map g).comp (map f) := rfl variables (M) @[simp, to_additive] lemma map_id : map (monoid_hom.id M) = monoid_hom.id Mˣ := by ext; refl /-- Coercion `Mˣ → M` as a monoid homomorphism. -/ @[to_additive "Coercion `add_units M → M` as an add_monoid homomorphism."] def coe_hom : Mˣ →* M := ⟨coe, coe_one, coe_mul⟩ variable {M} @[simp, to_additive] lemma coe_hom_apply (x : Mˣ) : coe_hom M x = ↑x := rfl @[simp, norm_cast, to_additive] lemma coe_pow (u : Mˣ) (n : ℕ) : ((u ^ n : Mˣ) : M) = u ^ n := (units.coe_hom M).map_pow u n section division_monoid variables [division_monoid α] @[simp, norm_cast, to_additive] lemma coe_div : ∀ u₁ u₂ : αˣ, ↑(u₁ / u₂) = (u₁ / u₂ : α) := (units.coe_hom α).map_div @[simp, norm_cast, to_additive] lemma coe_zpow : ∀ (u : αˣ) (n : ℤ), ((u ^ n : αˣ) : α) = u ^ n := (units.coe_hom α).map_zpow @[field_simps] lemma _root_.divp_eq_div (a : α) (u : αˣ) : a /ₚ u = a / u := by rw [div_eq_mul_inv, divp, u.coe_inv] @[simp, to_additive] lemma _root_.map_units_inv {F : Type*} [monoid_hom_class F M α] (f : F) (u : units M) : f ↑u⁻¹ = (f u)⁻¹ := ((f : M →* α).comp (units.coe_hom M)).map_inv u end division_monoid /-- If a map `g : M → Nˣ` agrees with a homomorphism `f : M →* N`, then this map is a monoid homomorphism too. -/ @[to_additive "If a map `g : M → add_units N` agrees with a homomorphism `f : M →+ N`, then this map is an add_monoid homomorphism too."] def lift_right (f : M →* N) (g : M → Nˣ) (h : ∀ x, ↑(g x) = f x) : M →* Nˣ := { to_fun := g, map_one' := units.ext $ (h 1).symm ▸ f.map_one, map_mul' := λ x y, units.ext $ by simp only [h, coe_mul, f.map_mul] } @[simp, to_additive] lemma coe_lift_right {f : M →* N} {g : M → Nˣ} (h : ∀ x, ↑(g x) = f x) (x) : (lift_right f g h x : N) = f x := h x @[simp, to_additive] lemma mul_lift_right_inv {f : M →* N} {g : M → Nˣ} (h : ∀ x, ↑(g x) = f x) (x) : f x * ↑(lift_right f g h x)⁻¹ = 1 := by rw [units.mul_inv_eq_iff_eq_mul, one_mul, coe_lift_right] @[simp, to_additive] lemma lift_right_inv_mul {f : M →* N} {g : M → Nˣ} (h : ∀ x, ↑(g x) = f x) (x) : ↑(lift_right f g h x)⁻¹ * f x = 1 := by rw [units.inv_mul_eq_iff_eq_mul, mul_one, coe_lift_right] end units namespace monoid_hom /-- If `f` is a homomorphism from a group `G` to a monoid `M`, then its image lies in the units of `M`, and `f.to_hom_units` is the corresponding monoid homomorphism from `G` to `Mˣ`. -/ @[to_additive "If `f` is a homomorphism from an additive group `G` to an additive monoid `M`, then its image lies in the `add_units` of `M`, and `f.to_hom_units` is the corresponding homomorphism from `G` to `add_units M`."] def to_hom_units {G M : Type*} [group G] [monoid M] (f : G →* M) : G →* Mˣ := units.lift_right f (λ g, ⟨f g, f g⁻¹, map_mul_eq_one f (mul_inv_self _), map_mul_eq_one f (inv_mul_self _)⟩) (λ g, rfl) @[simp, to_additive] lemma coe_to_hom_units {G M : Type*} [group G] [monoid M] (f : G →* M) (g : G) : (f.to_hom_units g : M) = f g := rfl end monoid_hom namespace is_unit variables {F G α M N : Type*} section monoid variables [monoid M] [monoid N] @[to_additive] lemma map [monoid_hom_class F M N] (f : F) {x : M} (h : is_unit x) : is_unit (f x) := by rcases h with ⟨y, rfl⟩; exact (units.map (f : M →* N) y).is_unit @[to_additive] lemma of_left_inverse [monoid_hom_class F M N] [monoid_hom_class G N M] {f : F} {x : M} (g : G) (hfg : function.left_inverse g f) (h : is_unit (f x)) : is_unit x := by simpa only [hfg x] using h.map g @[to_additive] lemma _root_.is_unit_map_of_left_inverse [monoid_hom_class F M N] [monoid_hom_class G N M] {f : F} {x : M} (g : G) (hfg : function.left_inverse g f) : is_unit (f x) ↔ is_unit x := ⟨of_left_inverse g hfg, map _⟩ /-- If a homomorphism `f : M →* N` sends each element to an `is_unit`, then it can be lifted to `f : M →* Nˣ`. See also `units.lift_right` for a computable version. -/ @[to_additive "If a homomorphism `f : M →+ N` sends each element to an `is_add_unit`, then it can be lifted to `f : M →+ add_units N`. See also `add_units.lift_right` for a computable version."] noncomputable def lift_right (f : M →* N) (hf : ∀ x, is_unit (f x)) : M →* Nˣ := units.lift_right f (λ x, (hf x).unit) $ λ x, rfl @[to_additive] lemma coe_lift_right (f : M →* N) (hf : ∀ x, is_unit (f x)) (x) : (is_unit.lift_right f hf x : N) = f x := rfl @[simp, to_additive] lemma mul_lift_right_inv (f : M →* N) (h : ∀ x, is_unit (f x)) (x) : f x * ↑(is_unit.lift_right f h x)⁻¹ = 1 := units.mul_lift_right_inv (λ y, rfl) x @[simp, to_additive] lemma lift_right_inv_mul (f : M →* N) (h : ∀ x, is_unit (f x)) (x) : ↑(is_unit.lift_right f h x)⁻¹ * f x = 1 := units.lift_right_inv_mul (λ y, rfl) x end monoid section division_monoid variables [division_monoid α] {a b c : α} /-- The element of the group of units, corresponding to an element of a monoid which is a unit. As opposed to `is_unit.unit`, the inverse is computable and comes from the inversion on `α`. This is useful to transfer properties of inversion in `units α` to `α`. See also `to_units`. -/ @[to_additive "The element of the additive group of additive units, corresponding to an element of an additive monoid which is an additive unit. As opposed to `is_add_unit.add_unit`, the negation is computable and comes from the negation on `α`. This is useful to transfer properties of negation in `add_units α` to `α`. See also `to_add_units`.", simps] def unit' (h : is_unit a) : αˣ := ⟨a, a⁻¹, h.mul_inv_cancel, h.inv_mul_cancel⟩ @[simp, to_additive] protected lemma mul_inv_cancel_left (h : is_unit a) : ∀ b, a * (a⁻¹ * b) = b := h.unit'.mul_inv_cancel_left @[simp, to_additive] protected lemma inv_mul_cancel_left (h : is_unit a) : ∀ b, a⁻¹ * (a * b) = b := h.unit'.inv_mul_cancel_left @[simp, to_additive] protected lemma mul_inv_cancel_right (h : is_unit b) (a : α) : a * b * b⁻¹ = a := h.unit'.mul_inv_cancel_right _ @[simp, to_additive] protected lemma inv_mul_cancel_right (h : is_unit b) (a : α) : a * b⁻¹ * b = a := h.unit'.inv_mul_cancel_right _ @[to_additive] protected lemma div_self (h : is_unit a) : a / a = 1 := by rw [div_eq_mul_inv, h.mul_inv_cancel] @[to_additive] protected lemma eq_mul_inv_iff_mul_eq (h : is_unit c) : a = b * c⁻¹ ↔ a * c = b := h.unit'.eq_mul_inv_iff_mul_eq @[to_additive] protected lemma eq_inv_mul_iff_mul_eq (h : is_unit b) : a = b⁻¹ * c ↔ b * a = c := h.unit'.eq_inv_mul_iff_mul_eq @[to_additive] protected lemma inv_mul_eq_iff_eq_mul (h : is_unit a) : a⁻¹ * b = c ↔ b = a * c := h.unit'.inv_mul_eq_iff_eq_mul @[to_additive] protected lemma mul_inv_eq_iff_eq_mul (h : is_unit b) : a * b⁻¹ = c ↔ a = c * b := h.unit'.mul_inv_eq_iff_eq_mul @[to_additive] protected lemma mul_inv_eq_one (h : is_unit b) : a * b⁻¹ = 1 ↔ a = b := @units.mul_inv_eq_one _ _ h.unit' _ @[to_additive] protected lemma inv_mul_eq_one (h : is_unit a) : a⁻¹ * b = 1 ↔ a = b := @units.inv_mul_eq_one _ _ h.unit' _ @[to_additive] protected lemma mul_eq_one_iff_eq_inv (h : is_unit b) : a * b = 1 ↔ a = b⁻¹ := @units.mul_eq_one_iff_eq_inv _ _ h.unit' _ @[to_additive] protected lemma mul_eq_one_iff_inv_eq (h : is_unit a) : a * b = 1 ↔ a⁻¹ = b := @units.mul_eq_one_iff_inv_eq _ _ h.unit' _ @[simp, to_additive] protected lemma div_mul_cancel (h : is_unit b) (a : α) : a / b * b = a := by rw [div_eq_mul_inv, h.inv_mul_cancel_right] @[simp, to_additive] protected lemma mul_div_cancel (h : is_unit b) (a : α) : a * b / b = a := by rw [div_eq_mul_inv, h.mul_inv_cancel_right] @[to_additive] protected lemma mul_one_div_cancel (h : is_unit a) : a * (1 / a) = 1 := by simp [h] @[to_additive] protected lemma one_div_mul_cancel (h : is_unit a) : (1 / a) * a = 1 := by simp [h] @[to_additive] lemma inv : is_unit a → is_unit a⁻¹ := by { rintro ⟨u, rfl⟩, rw ←units.coe_inv, exact units.is_unit _ } @[to_additive] lemma div (ha : is_unit a) (hb : is_unit b) : is_unit (a / b) := by { rw div_eq_mul_inv, exact ha.mul hb.inv } @[to_additive] protected lemma div_left_inj (h : is_unit c) : a / c = b / c ↔ a = b := by { simp_rw div_eq_mul_inv, exact units.mul_left_inj h.inv.unit' } @[to_additive] protected lemma div_eq_iff (h : is_unit b) : a / b = c ↔ a = c * b := by rw [div_eq_mul_inv, h.mul_inv_eq_iff_eq_mul] @[to_additive] protected lemma eq_div_iff (h : is_unit c) : a = b / c ↔ a * c = b := by rw [div_eq_mul_inv, h.eq_mul_inv_iff_mul_eq] @[to_additive] protected lemma div_eq_of_eq_mul (h : is_unit b) : a = c * b → a / b = c := h.div_eq_iff.2 @[to_additive] protected lemma eq_div_of_mul_eq (h : is_unit c) : a * c = b → a = b / c := h.eq_div_iff.2 @[to_additive] protected lemma div_eq_one_iff_eq (h : is_unit b) : a / b = 1 ↔ a = b := ⟨eq_of_div_eq_one, λ hab, hab.symm ▸ h.div_self⟩ /-- The `group` version of this lemma is `div_mul_cancel'''` -/ @[to_additive "The `add_group` version of this lemma is `sub_add_cancel''`"] protected lemma div_mul_left (h : is_unit b) : b / (a * b) = 1 / a := by rw [div_eq_mul_inv, mul_inv_rev, h.mul_inv_cancel_left, one_div] @[to_additive] protected lemma mul_div_mul_right (h : is_unit c) (a b : α) : (a * c) / (b * c) = a / b := by simp only [div_eq_mul_inv, mul_inv_rev, mul_assoc, h.mul_inv_cancel_left] @[to_additive] protected lemma mul_mul_div (a : α) (h : is_unit b) : a * b * (1 / b) = a := by simp [h] end division_monoid section division_comm_monoid variables [division_comm_monoid α] {a b c d : α} @[to_additive] protected lemma div_mul_right (h : is_unit a) (b : α) : a / (a * b) = 1 / b := by rw [mul_comm, h.div_mul_left] @[to_additive] protected lemma mul_div_cancel_left (h : is_unit a) (b : α) : a * b / a = b := by rw [mul_comm, h.mul_div_cancel] @[to_additive] protected lemma mul_div_cancel' (h : is_unit a) (b : α) : a * (b / a) = b := by rw [mul_comm, h.div_mul_cancel] @[to_additive] protected lemma mul_div_mul_left (h : is_unit c) (a b : α) : (c * a) / (c * b) = a / b := by rw [mul_comm c, mul_comm c, h.mul_div_mul_right] @[to_additive] protected lemma mul_eq_mul_of_div_eq_div (hb : is_unit b) (hd : is_unit d) (a c : α) (h : a / b = c / d) : a * d = c * b := by rw [←mul_one a, ←hb.div_self, ←mul_comm_div, h, div_mul_eq_mul_div, hd.div_mul_cancel] @[to_additive] protected lemma div_eq_div_iff (hb : is_unit b) (hd : is_unit d) : a / b = c / d ↔ a * d = c * b := by rw [←(hb.mul hd).mul_left_inj, ←mul_assoc, hb.div_mul_cancel, ←mul_assoc, mul_right_comm, hd.div_mul_cancel] @[to_additive] protected lemma div_div_cancel (h : is_unit a) : a / (a / b) = b := by rw [div_div_eq_mul_div, h.mul_div_cancel_left] @[to_additive] protected lemma div_div_cancel_left (h : is_unit a) : a / b / a = b⁻¹ := by rw [div_eq_mul_inv, div_eq_mul_inv, mul_right_comm, h.mul_inv_cancel, one_mul] end division_comm_monoid end is_unit
21b3bfe59e604e9fcd682c84490aa53642781974
856e2e1615a12f95b551ed48fa5b03b245abba44
/src/data/real/nnreal.lean
4b31fa3900a7f045c277cd245bb9864aea8b1561
[ "Apache-2.0" ]
permissive
pimsp/mathlib
8b77e1ccfab21703ba8fbe65988c7de7765aa0e5
913318ca9d6979686996e8d9b5ebf7e74aae1c63
refs/heads/master
1,669,812,465,182
1,597,133,610,000
1,597,133,610,000
281,890,685
1
0
null
1,595,491,577,000
1,595,491,576,000
null
UTF-8
Lean
false
false
25,779
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import algebra.linear_ordered_comm_group_with_zero import algebra.big_operators.ring import data.finset.lattice import data.real.basic import data.indicator_function 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 : nnreal) : n.val = n := rfl instance : can_lift ℝ nnreal := { coe := coe, cond := λ r, r ≥ 0, 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 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 : nnreal) : (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⟩ @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := subtype.ext_iff_val.symm @[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 @[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 : decidable_linear_order ℝ≥0 := decidable_linear_order.lift (coe : ℝ≥0 → ℝ) subtype.val_injective @[norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl 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.decidable_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.decidable_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_lt_one := @zero_lt_one ℝ _, .. nnreal.decidable_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⟩ := dense 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 : nnreal → ℝ) '' 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 : nnreal → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ instance : has_Sup ℝ≥0 := ⟨λs, ⟨Sup ((coe : nnreal → ℝ) '' 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 : nnreal → ℝ) '' 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 nnreal) : (↑(Sup s) : ℝ) = Sup ((coe : nnreal → ℝ) '' s) := rfl lemma coe_Inf (s : set nnreal) : (↑(Inf s) : ℝ) = Inf ((coe : nnreal → ℝ) '' 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 : nnreal → ℝ) '' 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 : nnreal → ℝ) '' 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_decidable_linear_order, .. nnreal.order_bot } instance : archimedean nnreal := ⟨ assume x y pos_y, let ⟨n, hr⟩ := archimedean.arch (x:ℝ) (pos_y : (0 : ℝ) < y) in ⟨n, show (x:ℝ) ≤ (n •ℕ y : nnreal), by simp [*, -nsmul_eq_mul, nsmul_coe]⟩ ⟩ lemma le_of_forall_epsilon_le {a b : nnreal} (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 : nnreal) : 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 : (⊥ : nnreal) = 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 : nnreal) : ((max x y : nnreal) : ℝ) = max (x : ℝ) (y : ℝ) := by { delta max, split_ifs; refl } @[simp, norm_cast] lemma coe_min (x y : nnreal) : ((min x y : nnreal) : ℝ) = min (x : ℝ) (y : ℝ) := by { delta min, split_ifs; refl } section of_real @[simp] lemma zero_le_coe {q : nnreal} : 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 : nnreal} : nnreal.of_real r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_of_real_iff_coe_le {r : nnreal} {p : ℝ} (hp : p ≥ 0) : r ≤ nnreal.of_real p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, nnreal.coe_of_real p hp] lemma of_real_lt_iff_lt_coe {r : ℝ} {p : nnreal} (ha : r ≥ 0) : 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 : nnreal} {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 : nnreal} (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, have := max_eq_left (mul_nonneg hp hq), simpa [nnreal.of_real, hp, hq, max_eq_left] }, { 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 @[field_simps] theorem mul_ne_zero' {a b : nnreal} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 := mul_ne_zero h₁ h₂ 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 : nnreal} : 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 : nnreal} : 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 : nnreal} : (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 : nnreal} : (r + p) - r = p := by rw [add_comm, add_sub_cancel] @[simp] lemma sub_add_cancel_of_le {a b : nnreal} (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 : nnreal} : p < q - r ↔ p + r < q := begin split, { assume H, have : (((q - r) : nnreal) : ℝ) = (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 end sub section inv lemma div_def {r p : nnreal} : 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 : nnreal)⁻¹ = 0 := nnreal.eq inv_zero @[simp] lemma inv_eq_zero {r : nnreal} : (r : nnreal)⁻¹ = 0 ↔ r = 0 := inv_eq_zero @[simp] lemma inv_pos {r : nnreal} : 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_eq_inv (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 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 end nnreal
f47d179868639e01de7ec8af75f95884119f1237
4fa161becb8ce7378a709f5992a594764699e268
/src/category_theory/equivalence.lean
978002281148fb63d1b55bfeaa435c088461b6ab
[ "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
14,752
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.fully_faithful import category_theory.whiskering import tactic.slice namespace category_theory open category_theory.functor nat_iso category universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation /-- We define an equivalence as a (half)-adjoint equivalence, a pair of functors with a unit and counit which are natural isomorphisms and the triangle law `Fη ≫ εF = 1`, or in other words the composite `F ⟶ FGF ⟶ F` is the identity. The triangle equation is written as a family of equalities between morphisms, it is more complicated if we write it as an equality of natural transformations, because then we would have to insert natural transformations like `F ⟶ F1`. -/ structure equivalence (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] := mk' :: (functor : C ⥤ D) (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ functor ⋙ inverse) (counit_iso : inverse ⋙ functor ≅ 𝟭 D) (functor_unit_iso_comp' : ∀(X : C), functor.map ((unit_iso.hom : 𝟭 C ⟶ functor ⋙ inverse).app X) ≫ counit_iso.hom.app (functor.obj X) = 𝟙 (functor.obj X) . obviously) restate_axiom equivalence.functor_unit_iso_comp' infixr ` ≌ `:10 := equivalence variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] namespace equivalence @[simp] def unit (e : C ≌ D) : 𝟭 C ⟶ e.functor ⋙ e.inverse := e.unit_iso.hom @[simp] def counit (e : C ≌ D) : e.inverse ⋙ e.functor ⟶ 𝟭 D := e.counit_iso.hom @[simp] def unit_inv (e : C ≌ D) : e.functor ⋙ e.inverse ⟶ 𝟭 C := e.unit_iso.inv @[simp] def counit_inv (e : C ≌ D) : 𝟭 D ⟶ e.inverse ⋙ e.functor := e.counit_iso.inv lemma unit_def (e : C ≌ D) : e.unit_iso.hom = e.unit := rfl lemma counit_def (e : C ≌ D) : e.counit_iso.hom = e.counit := rfl lemma unit_inv_def (e : C ≌ D) : e.unit_iso.inv = e.unit_inv := rfl lemma counit_inv_def (e : C ≌ D) : e.counit_iso.inv = e.counit_inv := rfl @[simp] lemma functor_unit_comp (e : C ≌ D) (X : C) : e.functor.map (e.unit_iso.hom.app X) ≫ e.counit_iso.hom.app (e.functor.obj X) = 𝟙 (e.functor.obj X) := e.functor_unit_iso_comp X @[simp] lemma counit_inv_functor_comp (e : C ≌ D) (X : C) : e.counit_iso.inv.app (e.functor.obj X) ≫ e.functor.map (e.unit_iso.inv.app X) = 𝟙 (e.functor.obj X) := begin erw [iso.inv_eq_inv (e.functor.map_iso (e.unit_iso.app X) ≪≫ e.counit_iso.app (e.functor.obj X)) (iso.refl _)], exact e.functor_unit_comp X end lemma functor_unit (e : C ≌ D) (X : C) : e.functor.map (e.unit.app X) = e.counit_inv.app (e.functor.obj X) := by { erw [←iso.comp_hom_eq_id (e.counit_iso.app _), functor_unit_comp], refl } lemma counit_functor (e : C ≌ D) (X : C) : e.counit.app (e.functor.obj X) = e.functor.map (e.unit_inv.app X) := by { erw [←iso.hom_comp_eq_id (e.functor.map_iso (e.unit_iso.app X)), functor_unit_comp], refl } /-- The other triangle equality. The proof follows the following proof in Globular: http://globular.science/1905.001 -/ @[simp] lemma unit_inverse_comp (e : C ≌ D) (Y : D) : e.unit_iso.hom.app (e.inverse.obj Y) ≫ e.inverse.map (e.counit_iso.hom.app Y) = 𝟙 (e.inverse.obj Y) := begin rw [←id_comp (e.inverse.map _), ←map_id e.inverse, ←counit_inv_functor_comp, map_comp, ←iso.hom_inv_id_assoc (e.unit_iso.app _) (e.inverse.map (e.functor.map _)), app_hom, app_inv, unit_def, unit_inv_def], slice_lhs 2 3 { erw [e.unit.naturality] }, slice_lhs 1 2 { erw [e.unit.naturality] }, slice_lhs 4 4 { rw [←iso.hom_inv_id_assoc (e.inverse.map_iso (e.counit_iso.app _)) (e.unit_inv.app _)] }, slice_lhs 3 4 { erw [←map_comp e.inverse, e.counit.naturality], erw [(e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 2 3 { erw [←map_comp e.inverse, e.counit_iso.inv.naturality, map_comp] }, slice_lhs 3 4 { erw [e.unit_inv.naturality] }, slice_lhs 4 5 { erw [←map_comp (e.functor ⋙ e.inverse), (e.unit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 3 4 { erw [←e.unit_inv.naturality] }, slice_lhs 2 3 { erw [←map_comp e.inverse, ←e.counit_iso.inv.naturality, (e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp, (e.unit_iso.app _).hom_inv_id], refl end @[simp] lemma inverse_counit_inv_comp (e : C ≌ D) (Y : D) : e.inverse.map (e.counit_iso.inv.app Y) ≫ e.unit_iso.inv.app (e.inverse.obj Y) = 𝟙 (e.inverse.obj Y) := begin erw [iso.inv_eq_inv (e.unit_iso.app (e.inverse.obj Y) ≪≫ e.inverse.map_iso (e.counit_iso.app Y)) (iso.refl _)], exact e.unit_inverse_comp Y end lemma unit_inverse (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) = e.inverse.map (e.counit_inv.app Y) := by { erw [←iso.comp_hom_eq_id (e.inverse.map_iso (e.counit_iso.app Y)), unit_inverse_comp], refl } lemma inverse_counit (e : C ≌ D) (Y : D) : e.inverse.map (e.counit.app Y) = e.unit_inv.app (e.inverse.obj Y) := by { erw [←iso.hom_comp_eq_id (e.unit_iso.app _), unit_inverse_comp], refl } @[simp] lemma fun_inv_map (e : C ≌ D) (X Y : D) (f : X ⟶ Y) : e.functor.map (e.inverse.map f) = e.counit.app X ≫ f ≫ e.counit_inv.app Y := (nat_iso.naturality_2 (e.counit_iso) f).symm @[simp] lemma inv_fun_map (e : C ≌ D) (X Y : C) (f : X ⟶ Y) : e.inverse.map (e.functor.map f) = e.unit_inv.app X ≫ f ≫ e.unit.app Y := (nat_iso.naturality_1 (e.unit_iso) f).symm section -- In this section we convert an arbitrary equivalence to a half-adjoint equivalence. variables {F : C ⥤ D} {G : D ⥤ C} (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) def adjointify_η : 𝟭 C ≅ F ⋙ G := calc 𝟭 C ≅ F ⋙ G : η ... ≅ F ⋙ (𝟭 D ⋙ G) : iso_whisker_left F (left_unitor G).symm ... ≅ F ⋙ ((G ⋙ F) ⋙ G) : iso_whisker_left F (iso_whisker_right ε.symm G) ... ≅ F ⋙ (G ⋙ (F ⋙ G)) : iso_whisker_left F (associator G F G) ... ≅ (F ⋙ G) ⋙ (F ⋙ G) : (associator F G (F ⋙ G)).symm ... ≅ 𝟭 C ⋙ (F ⋙ G) : iso_whisker_right η.symm (F ⋙ G) ... ≅ F ⋙ G : left_unitor (F ⋙ G) lemma adjointify_η_ε (X : C) : F.map ((adjointify_η η ε).hom.app X) ≫ ε.hom.app (F.obj X) = 𝟙 (F.obj X) := begin dsimp [adjointify_η], simp, have := ε.hom.naturality (F.map (η.inv.app X)), dsimp at this, rw [this], clear this, rw [←assoc _ _ (F.map _)], have := ε.hom.naturality (ε.inv.app $ F.obj X), dsimp at this, rw [this], clear this, have := (ε.app $ F.obj X).hom_inv_id, dsimp at this, rw [this], clear this, rw [id_comp], have := (F.map_iso $ η.app X).hom_inv_id, dsimp at this, rw [this] end end protected definition mk (F : C ⥤ D) (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : C ≌ D := ⟨F, G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ @[refl, simps] def refl : C ≌ C := ⟨𝟭 C, 𝟭 C, iso.refl _, iso.refl _, λ X, category.id_comp _⟩ @[symm, simps] def symm (e : C ≌ D) : D ≌ C := ⟨e.inverse, e.functor, e.counit_iso.symm, e.unit_iso.symm, e.inverse_counit_inv_comp⟩ variables {E : Type u₃} [category.{v₃} E] @[trans] def trans (e : C ≌ D) (f : D ≌ E) : C ≌ E := begin apply equivalence.mk (e.functor ⋙ f.functor) (f.inverse ⋙ e.inverse), { refine iso.trans e.unit_iso _, exact iso_whisker_left e.functor (iso_whisker_right f.unit_iso e.inverse) }, { refine iso.trans _ f.counit_iso, exact iso_whisker_left f.inverse (iso_whisker_right e.counit_iso f.functor) } end def fun_inv_id_assoc (e : C ≌ D) (F : C ⥤ E) : e.functor ⋙ e.inverse ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.unit_iso.symm F ≪≫ F.left_unitor @[simp] lemma fun_inv_id_assoc_hom_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).hom.app X = F.map (e.unit_inv.app X) := by { dsimp [fun_inv_id_assoc], tidy } @[simp] lemma fun_inv_id_assoc_inv_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).inv.app X = F.map (e.unit.app X) := by { dsimp [fun_inv_id_assoc], tidy } def inv_fun_id_assoc (e : C ≌ D) (F : D ⥤ E) : e.inverse ⋙ e.functor ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.counit_iso F ≪≫ F.left_unitor @[simp] lemma inv_fun_id_assoc_hom_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).hom.app X = F.map (e.counit.app X) := by { dsimp [inv_fun_id_assoc], tidy } @[simp] lemma inv_fun_id_assoc_inv_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).inv.app X = F.map (e.counit_inv.app X) := by { dsimp [inv_fun_id_assoc], tidy } section -- There's of course a monoid structure on `C ≌ C`, -- but let's not encourage using it. -- The power structure is nevertheless useful. /-- Powers of an auto-equivalence. -/ def pow (e : C ≌ C) : ℤ → (C ≌ C) | (int.of_nat 0) := equivalence.refl | (int.of_nat 1) := e | (int.of_nat (n+2)) := e.trans (pow (int.of_nat (n+1))) | (int.neg_succ_of_nat 0) := e.symm | (int.neg_succ_of_nat (n+1)) := e.symm.trans (pow (int.neg_succ_of_nat n)) instance : has_pow (C ≌ C) ℤ := ⟨pow⟩ @[simp] lemma pow_zero (e : C ≌ C) : e^(0 : ℤ) = equivalence.refl := rfl @[simp] lemma pow_one (e : C ≌ C) : e^(1 : ℤ) = e := rfl @[simp] lemma pow_minus_one (e : C ≌ C) : e^(-1 : ℤ) = e.symm := rfl -- TODO as necessary, add the natural isomorphisms `(e^a).trans e^b ≅ e^(a+b)`. -- At this point, we haven't even defined the category of equivalences. end end equivalence /-- A functor that is part of a (half) adjoint equivalence -/ class is_equivalence (F : C ⥤ D) := mk' :: (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ F ⋙ inverse) (counit_iso : inverse ⋙ F ≅ 𝟭 D) (functor_unit_iso_comp' : ∀ (X : C), F.map ((unit_iso.hom : 𝟭 C ⟶ F ⋙ inverse).app X) ≫ counit_iso.hom.app (F.obj X) = 𝟙 (F.obj X) . obviously) restate_axiom is_equivalence.functor_unit_iso_comp' namespace is_equivalence instance of_equivalence (F : C ≌ D) : is_equivalence F.functor := { ..F } instance of_equivalence_inverse (F : C ≌ D) : is_equivalence F.inverse := is_equivalence.of_equivalence F.symm open equivalence protected definition mk {F : C ⥤ D} (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : is_equivalence F := ⟨G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ end is_equivalence namespace functor def as_equivalence (F : C ⥤ D) [is_equivalence F] : C ≌ D := ⟨F, is_equivalence.inverse F, is_equivalence.unit_iso, is_equivalence.counit_iso, is_equivalence.functor_unit_iso_comp⟩ instance is_equivalence_refl : is_equivalence (𝟭 C) := is_equivalence.of_equivalence equivalence.refl def inv (F : C ⥤ D) [is_equivalence F] : D ⥤ C := is_equivalence.inverse F instance is_equivalence_inv (F : C ⥤ D) [is_equivalence F] : is_equivalence F.inv := is_equivalence.of_equivalence F.as_equivalence.symm def fun_inv_id (F : C ⥤ D) [is_equivalence F] : F ⋙ F.inv ≅ 𝟭 C := is_equivalence.unit_iso.symm def inv_fun_id (F : C ⥤ D) [is_equivalence F] : F.inv ⋙ F ≅ 𝟭 D := is_equivalence.counit_iso variables {E : Type u₃} [category.{v₃} E] instance is_equivalence_trans (F : C ⥤ D) (G : D ⥤ E) [is_equivalence F] [is_equivalence G] : is_equivalence (F ⋙ G) := is_equivalence.of_equivalence (equivalence.trans (as_equivalence F) (as_equivalence G)) end functor namespace is_equivalence @[simp] lemma fun_inv_map (F : C ⥤ D) [is_equivalence F] (X Y : D) (f : X ⟶ Y) : F.map (F.inv.map f) = F.inv_fun_id.hom.app X ≫ f ≫ F.inv_fun_id.inv.app Y := begin erw [nat_iso.naturality_2], refl end @[simp] lemma inv_fun_map (F : C ⥤ D) [is_equivalence F] (X Y : C) (f : X ⟶ Y) : F.inv.map (F.map f) = F.fun_inv_id.hom.app X ≫ f ≫ F.fun_inv_id.inv.app Y := begin erw [nat_iso.naturality_2], refl end -- We should probably restate many of the lemmas about `equivalence` for `is_equivalence`, -- but these are the only ones I need for now. @[simp] lemma functor_unit_comp (E : C ⥤ D) [is_equivalence E] (Y) : E.map (is_equivalence.unit_iso.hom.app Y) ≫ is_equivalence.counit_iso.hom.app (E.obj Y) = 𝟙 _ := equivalence.functor_unit_comp (E.as_equivalence) Y @[simp] lemma counit_inv_functor_comp (E : C ⥤ D) [is_equivalence E] (Y) : is_equivalence.counit_iso.inv.app (E.obj Y) ≫ E.map (is_equivalence.unit_iso.inv.app Y) = 𝟙 _ := eq_of_inv_eq_inv (functor_unit_comp _ _) end is_equivalence class ess_surj (F : C ⥤ D) := (obj_preimage (d : D) : C) (iso' (d : D) : F.obj (obj_preimage d) ≅ d . obviously) restate_axiom ess_surj.iso' namespace functor def obj_preimage (F : C ⥤ D) [ess_surj F] (d : D) : C := ess_surj.obj_preimage.{v₁ v₂} F d def fun_obj_preimage_iso (F : C ⥤ D) [ess_surj F] (d : D) : F.obj (F.obj_preimage d) ≅ d := ess_surj.iso d end functor namespace equivalence def ess_surj_of_equivalence (F : C ⥤ D) [is_equivalence F] : ess_surj F := ⟨ λ Y : D, F.inv.obj Y, λ Y : D, (F.inv_fun_id.app Y) ⟩ @[priority 100] -- see Note [lower instance priority] instance faithful_of_equivalence (F : C ⥤ D) [is_equivalence F] : faithful F := { map_injective' := λ X Y f g w, begin have p := congr_arg (@category_theory.functor.map _ _ _ _ F.inv _ _) w, simpa only [cancel_epi, cancel_mono, is_equivalence.inv_fun_map] using p end }. @[priority 100] -- see Note [lower instance priority] instance full_of_equivalence (F : C ⥤ D) [is_equivalence F] : full F := { preimage := λ X Y f, F.fun_inv_id.inv.app X ≫ F.inv.map f ≫ F.fun_inv_id.hom.app Y, witness' := λ X Y f, F.inv.map_injective (by simpa only [is_equivalence.inv_fun_map, assoc, hom_inv_id_app_assoc, hom_inv_id_app] using comp_id _) } @[simp] private def equivalence_inverse (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : D ⥤ C := { obj := λ X, F.obj_preimage X, map := λ X Y f, F.preimage ((F.fun_obj_preimage_iso X).hom ≫ f ≫ (F.fun_obj_preimage_iso Y).inv), map_id' := λ X, begin apply F.map_injective, tidy end, map_comp' := λ X Y Z f g, by apply F.map_injective; simp } def equivalence_of_fully_faithfully_ess_surj (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : is_equivalence F := is_equivalence.mk (equivalence_inverse F) (nat_iso.of_components (λ X, (preimage_iso $ F.fun_obj_preimage_iso $ F.obj X).symm) (λ X Y f, by { apply F.map_injective, obviously })) (nat_iso.of_components (λ Y, F.fun_obj_preimage_iso Y) (by obviously)) end equivalence end category_theory
21815ae881490a80436eca42ebbddaf5ce40de4b
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/data/finsupp/basic.lean
0b375d281d6606f95aaa8920ee96271b407dbf6c
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
106,131
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, Scott Morrison -/ import data.finset.preimage import algebra.indicator_function import algebra.group_action_hom /-! # Type of functions with finite support For any type `α` and a type `M` with zero, we define the type `finsupp α M` (notation: `α →₀ M`) of finitely supported functions from `α` to `M`, i.e. the functions which are zero everywhere on `α` except on a finite set. Functions with finite support are used (at least) in the following parts of the library: * `monoid_algebra R M` and `add_monoid_algebra R M` are defined as `M →₀ R`; * polynomials and multivariate polynomials are defined as `add_monoid_algebra`s, hence they use `finsupp` under the hood; * the linear combination of a family of vectors `v i` with coefficients `f i` (as used, e.g., to define linearly independent family `linear_independent`) is defined as a map `finsupp.total : (ι → M) → (ι →₀ R) →ₗ[R] M`. Some other constructions are naturally equivalent to `α →₀ M` with some `α` and `M` but are defined in a different way in the library: * `multiset α ≃+ α →₀ ℕ`; * `free_abelian_group α ≃+ α →₀ ℤ`. Most of the theory assumes that the range is a commutative additive monoid. This gives us the big sum operator as a powerful way to construct `finsupp` elements. Many constructions based on `α →₀ M` use `semireducible` type tags to avoid reusing unwanted type instances. E.g., `monoid_algebra`, `add_monoid_algebra`, and types based on these two have non-pointwise multiplication. ## Notations This file adds `α →₀ M` as a global notation for `finsupp α M`. We also use the following convention for `Type*` variables in this file * `α`, `β`, `γ`: types with no additional structure that appear as the first argument to `finsupp` somewhere in the statement; * `ι` : an auxiliary index type; * `M`, `M'`, `N`, `P`: types with `has_zero` or `(add_)(comm_)monoid` structure; `M` is also used for a (semi)module over a (semi)ring. * `G`, `H`: groups (commutative or not, multiplicative or additive); * `R`, `S`: (semi)rings. ## TODO * This file is currently ~2K lines long, so possibly it should be splitted into smaller chunks; * Add the list of definitions and important lemmas to the module docstring. ## Implementation notes This file is a `noncomputable theory` and uses classical logic throughout. ## Notation This file defines `α →₀ β` as notation for `finsupp α β`. -/ noncomputable theory open_locale classical big_operators open finset variables {α β γ ι M M' N P G H R S : Type*} /-- `finsupp α M`, denoted `α →₀ M`, is the type of functions `f : α → M` such that `f x = 0` for all but finitely many `x`. -/ structure finsupp (α : Type*) (M : Type*) [has_zero M] := (support : finset α) (to_fun : α → M) (mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0) infixr ` →₀ `:25 := finsupp namespace finsupp /-! ### Basic declarations about `finsupp` -/ section basic variable [has_zero M] instance : has_coe_to_fun (α →₀ M) := ⟨λ _, α → M, to_fun⟩ @[simp] lemma coe_mk (f : α → M) (s : finset α) (h : ∀ a, a ∈ s ↔ f a ≠ 0) : ⇑(⟨s, f, h⟩ : α →₀ M) = f := rfl instance : has_zero (α →₀ M) := ⟨⟨∅, 0, λ _, ⟨false.elim, λ H, H rfl⟩⟩⟩ @[simp] lemma coe_zero : ⇑(0 : α →₀ M) = 0 := rfl lemma zero_apply {a : α} : (0 : α →₀ M) a = 0 := rfl @[simp] lemma support_zero : (0 : α →₀ M).support = ∅ := rfl instance : inhabited (α →₀ M) := ⟨0⟩ @[simp] lemma mem_support_iff {f : α →₀ M} : ∀{a:α}, a ∈ f.support ↔ f a ≠ 0 := f.mem_support_to_fun @[simp, norm_cast] lemma fun_support_eq (f : α →₀ M) : function.support f = f.support := set.ext $ λ x, mem_support_iff.symm lemma not_mem_support_iff {f : α →₀ M} {a} : a ∉ f.support ↔ f a = 0 := not_iff_comm.1 mem_support_iff.symm lemma coe_fn_injective : @function.injective (α →₀ M) (α → M) coe_fn | ⟨s, f, hf⟩ ⟨t, g, hg⟩ h := begin change f = g at h, subst h, have : s = t, { ext a, exact (hf a).trans (hg a).symm }, subst this end @[simp, norm_cast] lemma coe_fn_inj {f g : α →₀ M} : (f : α → M) = g ↔ f = g := coe_fn_injective.eq_iff @[simp, norm_cast] lemma coe_eq_zero {f : α →₀ M} : (f : α → M) = 0 ↔ f = 0 := by rw [← coe_zero, coe_fn_inj] @[ext] lemma ext {f g : α →₀ M} (h : ∀a, f a = g a) : f = g := coe_fn_injective (funext h) lemma ext_iff {f g : α →₀ M} : f = g ↔ (∀a:α, f a = g a) := ⟨by rintros rfl a; refl, ext⟩ lemma ext_iff' {f g : α →₀ M} : f = g ↔ f.support = g.support ∧ ∀ x ∈ f.support, f x = g x := ⟨λ h, h ▸ ⟨rfl, λ _ _, rfl⟩, λ ⟨h₁, h₂⟩, ext $ λ a, if h : a ∈ f.support then h₂ a h else have hf : f a = 0, from not_mem_support_iff.1 h, have hg : g a = 0, by rwa [h₁, not_mem_support_iff] at h, by rw [hf, hg]⟩ lemma congr_fun {f g : α →₀ M} (h : f = g) (a : α) : f a = g a := congr_fun (congr_arg finsupp.to_fun h) a @[simp] lemma support_eq_empty {f : α →₀ M} : f.support = ∅ ↔ f = 0 := by exact_mod_cast @function.support_eq_empty_iff _ _ _ f lemma support_nonempty_iff {f : α →₀ M} : f.support.nonempty ↔ f ≠ 0 := by simp only [finsupp.support_eq_empty, finset.nonempty_iff_ne_empty, ne.def] lemma nonzero_iff_exists {f : α →₀ M} : f ≠ 0 ↔ ∃ a : α, f a ≠ 0 := by simp [← finsupp.support_eq_empty, finset.eq_empty_iff_forall_not_mem] lemma card_support_eq_zero {f : α →₀ M} : card f.support = 0 ↔ f = 0 := by simp instance [decidable_eq α] [decidable_eq M] : decidable_eq (α →₀ M) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀a∈f.support, f a = g a)) ext_iff'.symm lemma finite_support (f : α →₀ M) : set.finite (function.support f) := f.fun_support_eq.symm ▸ f.support.finite_to_set lemma support_subset_iff {s : set α} {f : α →₀ M} : ↑f.support ⊆ s ↔ (∀a∉s, f a = 0) := by simp only [set.subset_def, mem_coe, mem_support_iff]; exact forall_congr (assume a, not_imp_comm) /-- Given `fintype α`, `equiv_fun_on_fintype` is the `equiv` between `α →₀ β` and `α → β`. (All functions on a finite type are finitely supported.) -/ @[simps] def equiv_fun_on_fintype [fintype α] : (α →₀ M) ≃ (α → M) := ⟨λf a, f a, λf, mk (finset.univ.filter $ λa, f a ≠ 0) f (by simp only [true_and, finset.mem_univ, iff_self, finset.mem_filter, finset.filter_congr_decidable, forall_true_iff]), begin intro f, ext a, refl end, begin intro f, ext a, refl end⟩ @[simp] lemma equiv_fun_on_fintype_symm_coe {α} [fintype α] (f : α →₀ M) : equiv_fun_on_fintype.symm f = f := by { ext, simp [equiv_fun_on_fintype], } end basic /-! ### Declarations about `single` -/ section single variables [has_zero M] {a a' : α} {b : M} /-- `single a b` is the finitely supported function which has value `b` at `a` and zero otherwise. -/ def single (a : α) (b : M) : α →₀ M := ⟨if b = 0 then ∅ else {a}, λ a', if a = a' then b else 0, λ a', begin by_cases hb : b = 0; by_cases a = a'; simp only [hb, h, if_pos, if_false, mem_singleton], { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨λ _, hb, λ _, rfl⟩ }, { exact ⟨λ H _, h H.symm, λ H, (H rfl).elim⟩ } end⟩ lemma single_apply [decidable (a = a')] : single a b a' = if a = a' then b else 0 := by convert rfl lemma single_eq_indicator : ⇑(single a b) = set.indicator {a} (λ _, b) := by { ext, simp [single_apply, set.indicator, @eq_comm _ a] } @[simp] lemma single_eq_same : (single a b : α →₀ M) a = b := if_pos rfl @[simp] lemma single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ M) a' = 0 := if_neg h lemma single_eq_update : ⇑(single a b) = function.update 0 a b := by rw [single_eq_indicator, ← set.piecewise_eq_indicator, set.piecewise_singleton] lemma single_eq_pi_single : ⇑(single a b) = pi.single a b := single_eq_update @[simp] lemma single_zero : (single a 0 : α →₀ M) = 0 := coe_fn_injective $ by simpa only [single_eq_update, coe_zero] using function.update_eq_self a (0 : α → M) lemma single_of_single_apply (a a' : α) (b : M) : single a ((single a' b) a) = single a' (single a' b) a := begin rw [single_apply, single_apply], ext, split_ifs, { rw h, }, { rw [zero_apply, single_apply, if_t_t], }, end lemma support_single_ne_zero (hb : b ≠ 0) : (single a b).support = {a} := if_neg hb lemma support_single_subset : (single a b).support ⊆ {a} := show ite _ _ _ ⊆ _, by split_ifs; [exact empty_subset _, exact subset.refl _] lemma single_apply_mem (x) : single a b x ∈ ({0, b} : set M) := by rcases em (a = x) with (rfl|hx); [simp, simp [single_eq_of_ne hx]] lemma range_single_subset : set.range (single a b) ⊆ {0, b} := set.range_subset_iff.2 single_apply_mem /-- `finsupp.single a b` is injective in `b`. For the statement that it is injective in `a`, see `finsupp.single_left_injective` -/ lemma single_injective (a : α) : function.injective (single a : M → α →₀ M) := assume b₁ b₂ eq, have (single a b₁ : α →₀ M) a = (single a b₂ : α →₀ M) a, by rw eq, by rwa [single_eq_same, single_eq_same] at this lemma single_apply_eq_zero {a x : α} {b : M} : single a b x = 0 ↔ (x = a → b = 0) := by simp [single_eq_indicator] lemma mem_support_single (a a' : α) (b : M) : a ∈ (single a' b).support ↔ a = a' ∧ b ≠ 0 := by simp [single_apply_eq_zero, not_or_distrib] lemma eq_single_iff {f : α →₀ M} {a b} : f = single a b ↔ f.support ⊆ {a} ∧ f a = b := begin refine ⟨λ h, h.symm ▸ ⟨support_single_subset, single_eq_same⟩, _⟩, rintro ⟨h, rfl⟩, ext x, by_cases hx : a = x; simp only [hx, single_eq_same, single_eq_of_ne, ne.def, not_false_iff], exact not_mem_support_iff.1 (mt (λ hx, (mem_singleton.1 (h hx)).symm) hx) end lemma single_eq_single_iff (a₁ a₂ : α) (b₁ b₂ : M) : single a₁ b₁ = single a₂ b₂ ↔ ((a₁ = a₂ ∧ b₁ = b₂) ∨ (b₁ = 0 ∧ b₂ = 0)) := begin split, { assume eq, by_cases a₁ = a₂, { refine or.inl ⟨h, _⟩, rwa [h, (single_injective a₂).eq_iff] at eq }, { rw [ext_iff] at eq, have h₁ := eq a₁, have h₂ := eq a₂, simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (ne.symm h)] at h₁ h₂, exact or.inr ⟨h₁, h₂.symm⟩ } }, { rintros (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩), { refl }, { rw [single_zero, single_zero] } } end /-- `finsupp.single a b` is injective in `a`. For the statement that it is injective in `b`, see `finsupp.single_injective` -/ lemma single_left_injective (h : b ≠ 0) : function.injective (λ a : α, single a b) := λ a a' H, (((single_eq_single_iff _ _ _ _).mp H).resolve_right $ λ hb, h hb.1).left lemma single_left_inj (h : b ≠ 0) : single a b = single a' b ↔ a = a' := (single_left_injective h).eq_iff lemma support_single_ne_bot (i : α) (h : b ≠ 0) : (single i b).support ≠ ⊥ := begin have : i ∈ (single i b).support := by simpa using h, intro H, simpa [H] end lemma support_single_disjoint {b' : M} (hb : b ≠ 0) (hb' : b' ≠ 0) {i j : α} : disjoint (single i b).support (single j b').support ↔ i ≠ j := by simpa [support_single_ne_zero, hb, hb'] using ne_comm @[simp] lemma single_eq_zero : single a b = 0 ↔ b = 0 := by simp [ext_iff, single_eq_indicator] lemma single_swap (a₁ a₂ : α) (b : M) : single a₁ b a₂ = single a₂ b a₁ := by simp only [single_apply]; ac_refl instance [nonempty α] [nontrivial M] : nontrivial (α →₀ M) := begin inhabit α, rcases exists_ne (0 : M) with ⟨x, hx⟩, exact nontrivial_of_ne (single (default α) x) 0 (mt single_eq_zero.1 hx) end lemma unique_single [unique α] (x : α →₀ M) : x = single (default α) (x (default α)) := ext $ unique.forall_iff.2 single_eq_same.symm lemma unique_ext [unique α] {f g : α →₀ M} (h : f (default α) = g (default α)) : f = g := ext $ λ a, by rwa [unique.eq_default a] lemma unique_ext_iff [unique α] {f g : α →₀ M} : f = g ↔ f (default α) = g (default α) := ⟨λ h, h ▸ rfl, unique_ext⟩ @[simp] lemma unique_single_eq_iff [unique α] {b' : M} : single a b = single a' b' ↔ b = b' := by rw [unique_ext_iff, unique.eq_default a, unique.eq_default a', single_eq_same, single_eq_same] lemma support_eq_singleton {f : α →₀ M} {a : α} : f.support = {a} ↔ f a ≠ 0 ∧ f = single a (f a) := ⟨λ h, ⟨mem_support_iff.1 $ h.symm ▸ finset.mem_singleton_self a, eq_single_iff.2 ⟨subset_of_eq h, rfl⟩⟩, λ h, h.2.symm ▸ support_single_ne_zero h.1⟩ lemma support_eq_singleton' {f : α →₀ M} {a : α} : f.support = {a} ↔ ∃ b ≠ 0, f = single a b := ⟨λ h, let h := support_eq_singleton.1 h in ⟨_, h.1, h.2⟩, λ ⟨b, hb, hf⟩, hf.symm ▸ support_single_ne_zero hb⟩ lemma card_support_eq_one {f : α →₀ M} : card f.support = 1 ↔ ∃ a, f a ≠ 0 ∧ f = single a (f a) := by simp only [card_eq_one, support_eq_singleton] lemma card_support_eq_one' {f : α →₀ M} : card f.support = 1 ↔ ∃ a (b ≠ 0), f = single a b := by simp only [card_eq_one, support_eq_singleton'] @[simp] lemma equiv_fun_on_fintype_single [fintype α] (x : α) (m : M) : (@finsupp.equiv_fun_on_fintype α M _ _) (finsupp.single x m) = pi.single x m := by { ext, simp [finsupp.single_eq_pi_single, finsupp.equiv_fun_on_fintype], } @[simp] lemma equiv_fun_on_fintype_symm_single [fintype α] (x : α) (m : M) : (@finsupp.equiv_fun_on_fintype α M _ _).symm (pi.single x m) = finsupp.single x m := by { ext, simp [finsupp.single_eq_pi_single, finsupp.equiv_fun_on_fintype], } end single /-! ### Declarations about `on_finset` -/ section on_finset variables [has_zero M] /-- `on_finset s f hf` is the finsupp function representing `f` restricted to the finset `s`. The function needs to be `0` outside of `s`. Use this when the set needs to be filtered anyways, otherwise a better set representation is often available. -/ def on_finset (s : finset α) (f : α → M) (hf : ∀a, f a ≠ 0 → a ∈ s) : α →₀ M := ⟨s.filter (λa, f a ≠ 0), f, by simpa⟩ @[simp] lemma on_finset_apply {s : finset α} {f : α → M} {hf a} : (on_finset s f hf : α →₀ M) a = f a := rfl @[simp] lemma support_on_finset_subset {s : finset α} {f : α → M} {hf} : (on_finset s f hf).support ⊆ s := filter_subset _ _ @[simp] lemma mem_support_on_finset {s : finset α} {f : α → M} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) {a : α} : a ∈ (finsupp.on_finset s f hf).support ↔ f a ≠ 0 := by rw [finsupp.mem_support_iff, finsupp.on_finset_apply] lemma support_on_finset {s : finset α} {f : α → M} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) : (finsupp.on_finset s f hf).support = s.filter (λ a, f a ≠ 0) := rfl end on_finset section of_support_finite variables [has_zero M] /-- The natural `finsupp` induced by the function `f` given that it has finite support. -/ noncomputable def of_support_finite (f : α → M) (hf : (function.support f).finite) : α →₀ M := { support := hf.to_finset, to_fun := f, mem_support_to_fun := λ _, hf.mem_to_finset } lemma of_support_finite_coe {f : α → M} {hf : (function.support f).finite} : (of_support_finite f hf : α → M) = f := rfl instance : can_lift (α → M) (α →₀ M) := { coe := coe_fn, cond := λ f, (function.support f).finite, prf := λ f hf, ⟨of_support_finite f hf, rfl⟩ } end of_support_finite /-! ### Declarations about `map_range` -/ section map_range variables [has_zero M] [has_zero N] [has_zero P] /-- The composition of `f : M → N` and `g : α →₀ M` is `map_range f hf g : α →₀ N`, well-defined when `f 0 = 0`. This preserves the structure on `f`, and exists in various bundled forms for when `f` is itself bundled: * `finsupp.map_range.equiv` * `finsupp.map_range.zero_hom` * `finsupp.map_range.add_monoid_hom` * `finsupp.map_range.add_equiv` * `finsupp.map_range.linear_map` * `finsupp.map_range.linear_equiv` -/ def map_range (f : M → N) (hf : f 0 = 0) (g : α →₀ M) : α →₀ N := on_finset g.support (f ∘ g) $ assume a, by rw [mem_support_iff, not_imp_not]; exact λ H, (congr_arg f H).trans hf @[simp] lemma map_range_apply {f : M → N} {hf : f 0 = 0} {g : α →₀ M} {a : α} : map_range f hf g a = f (g a) := rfl @[simp] lemma map_range_zero {f : M → N} {hf : f 0 = 0} : map_range f hf (0 : α →₀ M) = 0 := ext $ λ a, by simp only [hf, zero_apply, map_range_apply] @[simp] lemma map_range_id (g : α →₀ M) : map_range id rfl g = g := ext $ λ _, rfl lemma map_range_comp (f : N → P) (hf : f 0 = 0) (f₂ : M → N) (hf₂ : f₂ 0 = 0) (h : (f ∘ f₂) 0 = 0) (g : α →₀ M) : map_range (f ∘ f₂) h g = map_range f hf (map_range f₂ hf₂ g) := ext $ λ _, rfl lemma support_map_range {f : M → N} {hf : f 0 = 0} {g : α →₀ M} : (map_range f hf g).support ⊆ g.support := support_on_finset_subset @[simp] lemma map_range_single {f : M → N} {hf : f 0 = 0} {a : α} {b : M} : map_range f hf (single a b) = single a (f b) := ext $ λ a', show f (ite _ _ _) = ite _ _ _, by split_ifs; [refl, exact hf] end map_range /-! ### Declarations about `emb_domain` -/ section emb_domain variables [has_zero M] [has_zero N] /-- Given `f : α ↪ β` and `v : α →₀ M`, `emb_domain f v : β →₀ M` is the finitely supported function whose value at `f a : β` is `v a`. For a `b : β` outside the range of `f`, it is zero. -/ def emb_domain (f : α ↪ β) (v : α →₀ M) : β →₀ M := begin refine ⟨v.support.map f, λa₂, if h : a₂ ∈ v.support.map f then v (v.support.choose (λa₁, f a₁ = a₂) _) else 0, _⟩, { rcases finset.mem_map.1 h with ⟨a, ha, rfl⟩, exact exists_unique.intro a ⟨ha, rfl⟩ (assume b ⟨_, hb⟩, f.injective hb) }, { assume a₂, split_ifs, { simp only [h, true_iff, ne.def], rw [← not_mem_support_iff, not_not], apply finset.choose_mem }, { simp only [h, ne.def, ne_self_iff_false] } } end @[simp] lemma support_emb_domain (f : α ↪ β) (v : α →₀ M) : (emb_domain f v).support = v.support.map f := rfl @[simp] lemma emb_domain_zero (f : α ↪ β) : (emb_domain f 0 : β →₀ M) = 0 := rfl @[simp] lemma emb_domain_apply (f : α ↪ β) (v : α →₀ M) (a : α) : emb_domain f v (f a) = v a := begin change dite _ _ _ = _, split_ifs; rw [finset.mem_map' f] at h, { refine congr_arg (v : α → M) (f.inj' _), exact finset.choose_property (λa₁, f a₁ = f a) _ _ }, { exact (not_mem_support_iff.1 h).symm } end lemma emb_domain_notin_range (f : α ↪ β) (v : α →₀ M) (a : β) (h : a ∉ set.range f) : emb_domain f v a = 0 := begin refine dif_neg (mt (assume h, _) h), rcases finset.mem_map.1 h with ⟨a, h, rfl⟩, exact set.mem_range_self a end lemma emb_domain_injective (f : α ↪ β) : function.injective (emb_domain f : (α →₀ M) → (β →₀ M)) := λ l₁ l₂ h, ext $ λ a, by simpa only [emb_domain_apply] using ext_iff.1 h (f a) @[simp] lemma emb_domain_inj {f : α ↪ β} {l₁ l₂ : α →₀ M} : emb_domain f l₁ = emb_domain f l₂ ↔ l₁ = l₂ := (emb_domain_injective f).eq_iff @[simp] lemma emb_domain_eq_zero {f : α ↪ β} {l : α →₀ M} : emb_domain f l = 0 ↔ l = 0 := (emb_domain_injective f).eq_iff' $ emb_domain_zero f lemma emb_domain_map_range (f : α ↪ β) (g : M → N) (p : α →₀ M) (hg : g 0 = 0) : emb_domain f (map_range g hg p) = map_range g hg (emb_domain f p) := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a', rfl⟩, rw [map_range_apply, emb_domain_apply, emb_domain_apply, map_range_apply] }, { rw [map_range_apply, emb_domain_notin_range, emb_domain_notin_range, ← hg]; assumption } end lemma single_of_emb_domain_single (l : α →₀ M) (f : α ↪ β) (a : β) (b : M) (hb : b ≠ 0) (h : l.emb_domain f = single a b) : ∃ x, l = single x b ∧ f x = a := begin have h_map_support : finset.map f (l.support) = {a}, by rw [←support_emb_domain, h, support_single_ne_zero hb]; refl, have ha : a ∈ finset.map f (l.support), by simp only [h_map_support, finset.mem_singleton], rcases finset.mem_map.1 ha with ⟨c, hc₁, hc₂⟩, use c, split, { ext d, rw [← emb_domain_apply f l, h], by_cases h_cases : c = d, { simp only [eq.symm h_cases, hc₂, single_eq_same] }, { rw [single_apply, single_apply, if_neg, if_neg h_cases], by_contra hfd, exact h_cases (f.injective (hc₂.trans hfd)) } }, { exact hc₂ } end @[simp] lemma emb_domain_single (f : α ↪ β) (a : α) (m : M) : emb_domain f (single a m) = single (f a) m := begin ext b, by_cases h : b ∈ set.range f, { rcases h with ⟨a', rfl⟩, simp [single_apply], }, { simp only [emb_domain_notin_range, h, single_apply, not_false_iff], rw if_neg, rintro rfl, simpa using h, }, end end emb_domain /-! ### Declarations about `zip_with` -/ section zip_with variables [has_zero M] [has_zero N] [has_zero P] /-- `zip_with f hf g₁ g₂` is the finitely supported function satisfying `zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, and it is well-defined when `f 0 0 = 0`. -/ def zip_with (f : M → N → P) (hf : f 0 0 = 0) (g₁ : α →₀ M) (g₂ : α →₀ N) : (α →₀ P) := on_finset (g₁.support ∪ g₂.support) (λa, f (g₁ a) (g₂ a)) $ λ a H, begin simp only [mem_union, mem_support_iff, ne], rw [← not_and_distrib], rintro ⟨h₁, h₂⟩, rw [h₁, h₂] at H, exact H hf end @[simp] lemma zip_with_apply {f : M → N → P} {hf : f 0 0 = 0} {g₁ : α →₀ M} {g₂ : α →₀ N} {a : α} : zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a) := rfl lemma support_zip_with [D : decidable_eq α] {f : M → N → P} {hf : f 0 0 = 0} {g₁ : α →₀ M} {g₂ : α →₀ N} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := by rw subsingleton.elim D; exact support_on_finset_subset end zip_with /-! ### Declarations about `erase` -/ section erase variables [has_zero M] /-- `erase a f` is the finitely supported function equal to `f` except at `a` where it is equal to `0`. -/ def erase (a : α) (f : α →₀ M) : α →₀ M := ⟨f.support.erase a, (λa', if a' = a then 0 else f a'), assume a', by rw [mem_erase, mem_support_iff]; split_ifs; [exact ⟨λ H _, H.1 h, λ H, (H rfl).elim⟩, exact and_iff_right h]⟩ @[simp] lemma support_erase {a : α} {f : α →₀ M} : (f.erase a).support = f.support.erase a := rfl @[simp] lemma erase_same {a : α} {f : α →₀ M} : (f.erase a) a = 0 := if_pos rfl @[simp] lemma erase_ne {a a' : α} {f : α →₀ M} (h : a' ≠ a) : (f.erase a) a' = f a' := if_neg h @[simp] lemma erase_single {a : α} {b : M} : (erase a (single a b)) = 0 := begin ext s, by_cases hs : s = a, { rw [hs, erase_same], refl }, { rw [erase_ne hs], exact single_eq_of_ne (ne.symm hs) } end lemma erase_single_ne {a a' : α} {b : M} (h : a ≠ a') : (erase a (single a' b)) = single a' b := begin ext s, by_cases hs : s = a, { rw [hs, erase_same, single_eq_of_ne (h.symm)] }, { rw [erase_ne hs] } end @[simp] lemma erase_zero (a : α) : erase a (0 : α →₀ M) = 0 := by rw [← support_eq_empty, support_erase, support_zero, erase_empty] end erase /-! ### Declarations about `sum` and `prod` In most of this section, the domain `β` is assumed to be an `add_monoid`. -/ section sum_prod -- [to_additive sum] for finsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/ def sum [has_zero M] [add_comm_monoid N] (f : α →₀ M) (g : α → M → N) : N := ∑ a in f.support, g a (f a) /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive] def prod [has_zero M] [comm_monoid N] (f : α →₀ M) (g : α → M → N) : N := ∏ a in f.support, g a (f a) variables [has_zero M] [has_zero M'] [comm_monoid N] @[to_additive] lemma prod_of_support_subset (f : α →₀ M) {s : finset α} (hs : f.support ⊆ s) (g : α → M → N) (h : ∀ i ∈ s, g i 0 = 1) : f.prod g = ∏ x in s, g x (f x) := finset.prod_subset hs $ λ x hxs hx, h x hxs ▸ congr_arg (g x) $ not_mem_support_iff.1 hx @[to_additive] lemma prod_fintype [fintype α] (f : α →₀ M) (g : α → M → N) (h : ∀ i, g i 0 = 1) : f.prod g = ∏ i, g i (f i) := f.prod_of_support_subset (subset_univ _) g (λ x _, h x) @[simp, to_additive] lemma prod_single_index {a : α} {b : M} {h : α → M → N} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := calc (single a b).prod h = ∏ x in {a}, h x (single a b x) : prod_of_support_subset _ support_single_subset h $ λ x hx, (mem_singleton.1 hx).symm ▸ h_zero ... = h a b : by simp @[to_additive] lemma prod_map_range_index {f : M → M'} {hf : f 0 = 0} {g : α →₀ M} {h : α → M' → N} (h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) := finset.prod_subset support_map_range $ λ _ _ H, by rw [not_mem_support_iff.1 H, h0] @[simp, to_additive] lemma prod_zero_index {h : α → M → N} : (0 : α →₀ M).prod h = 1 := rfl @[to_additive] lemma prod_comm (f : α →₀ M) (g : β →₀ M') (h : α → M → β → M' → N) : f.prod (λ x v, g.prod (λ x' v', h x v x' v')) = g.prod (λ x' v', f.prod (λ x v, h x v x' v')) := finset.prod_comm @[simp, to_additive] lemma prod_ite_eq [decidable_eq α] (f : α →₀ M) (a : α) (b : α → M → N) : f.prod (λ x v, ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq, } @[simp] lemma sum_ite_self_eq [decidable_eq α] {N : Type*} [add_comm_monoid N] (f : α →₀ N) (a : α) : f.sum (λ x v, ite (a = x) v 0) = f a := by { convert f.sum_ite_eq a (λ x, id), simp [ite_eq_right_iff.2 eq.symm] } /-- A restatement of `prod_ite_eq` with the equality test reversed. -/ @[simp, to_additive "A restatement of `sum_ite_eq` with the equality test reversed."] lemma prod_ite_eq' [decidable_eq α] (f : α →₀ M) (a : α) (b : α → M → N) : f.prod (λ x v, ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq', } @[simp] lemma sum_ite_self_eq' [decidable_eq α] {N : Type*} [add_comm_monoid N] (f : α →₀ N) (a : α) : f.sum (λ x v, ite (x = a) v 0) = f a := by { convert f.sum_ite_eq' a (λ x, id), simp [ite_eq_right_iff.2 eq.symm] } @[simp] lemma prod_pow [fintype α] (f : α →₀ ℕ) (g : α → N) : f.prod (λ a b, g a ^ b) = ∏ a, g a ^ (f a) := f.prod_fintype _ $ λ a, pow_zero _ /-- If `g` maps a second argument of 0 to 1, then multiplying it over the result of `on_finset` is the same as multiplying it over the original `finset`. -/ @[to_additive "If `g` maps a second argument of 0 to 0, summing it over the result of `on_finset` is the same as summing it over the original `finset`."] lemma on_finset_prod {s : finset α} {f : α → M} {g : α → M → N} (hf : ∀a, f a ≠ 0 → a ∈ s) (hg : ∀ a, g a 0 = 1) : (on_finset s f hf).prod g = ∏ a in s, g a (f a) := finset.prod_subset support_on_finset_subset $ by simp [*] { contextual := tt } @[to_additive] lemma _root_.submonoid.finsupp_prod_mem (S : submonoid N) (f : α →₀ M) (g : α → M → N) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ S) : f.prod g ∈ S := S.prod_mem $ λ i hi, h _ (finsupp.mem_support_iff.mp hi) end sum_prod /-! ### Additive monoid structure on `α →₀ M` -/ section add_zero_class variables [add_zero_class M] instance : has_add (α →₀ M) := ⟨zip_with (+) (add_zero 0)⟩ @[simp] lemma coe_add (f g : α →₀ M) : ⇑(f + g) = f + g := rfl lemma add_apply (g₁ g₂ : α →₀ M) (a : α) : (g₁ + g₂) a = g₁ a + g₂ a := rfl lemma support_add [decidable_eq α] {g₁ g₂ : α →₀ M} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with lemma support_add_eq [decidable_eq α] {g₁ g₂ : α →₀ M} (h : disjoint g₁.support g₂.support) : (g₁ + g₂).support = g₁.support ∪ g₂.support := le_antisymm support_zip_with $ assume a ha, (finset.mem_union.1 ha).elim (assume ha, have a ∉ g₂.support, from disjoint_left.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, add_zero]) (assume ha, have a ∉ g₁.support, from disjoint_right.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, zero_add]) @[simp] lemma single_add {a : α} {b₁ b₂ : M} : single a (b₁ + b₂) = single a b₁ + single a b₂ := ext $ assume a', begin by_cases h : a = a', { rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] } end instance : add_zero_class (α →₀ M) := { zero := 0, add := (+), zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _, add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ } /-- `finsupp.single` as an `add_monoid_hom`. See `finsupp.lsingle` for the stronger version as a linear map. -/ @[simps] def single_add_hom (a : α) : M →+ α →₀ M := ⟨single a, single_zero, λ _ _, single_add⟩ /-- Evaluation of a function `f : α →₀ M` at a point as an additive monoid homomorphism. See `finsupp.lapply` for the stronger version as a linear map. -/ @[simps apply] def apply_add_hom (a : α) : (α →₀ M) →+ M := ⟨λ g, g a, zero_apply, λ _ _, add_apply _ _ _⟩ lemma single_add_erase (a : α) (f : α →₀ M) : single a (f a) + f.erase a = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, add_zero] else by simp only [add_apply, single_eq_of_ne h, zero_add, erase_ne (ne.symm h)] lemma erase_add_single (a : α) (f : α →₀ M) : f.erase a + single a (f a) = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, zero_add] else by simp only [add_apply, single_eq_of_ne h, add_zero, erase_ne (ne.symm h)] @[simp] lemma erase_add (a : α) (f f' : α →₀ M) : erase a (f + f') = erase a f + erase a f' := begin ext s, by_cases hs : s = a, { rw [hs, add_apply, erase_same, erase_same, erase_same, add_zero] }, rw [add_apply, erase_ne hs, erase_ne hs, erase_ne hs, add_apply], end @[elab_as_eliminator] protected theorem induction {p : (α →₀ M) → Prop} (f : α →₀ M) (h0 : p 0) (ha : ∀a b (f : α →₀ M), a ∉ f.support → b ≠ 0 → p f → p (single a b + f)) : p f := suffices ∀s (f : α →₀ M), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma induction₂ {p : (α →₀ M) → Prop} (f : α →₀ M) (h0 : p 0) (ha : ∀a b (f : α →₀ M), a ∉ f.support → b ≠ 0 → p f → p (f + single a b)) : p f := suffices ∀s (f : α →₀ M), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma induction_linear {p : (α →₀ M) → Prop} (f : α →₀ M) (h0 : p 0) (hadd : ∀ f g : α →₀ M, p f → p g → p (f + g)) (hsingle : ∀ a b, p (single a b)) : p f := induction₂ f h0 (λ a b f _ _ w, hadd _ _ w (hsingle _ _)) @[simp] lemma add_closure_Union_range_single : add_submonoid.closure (⋃ a : α, set.range (single a : M → α →₀ M)) = ⊤ := top_unique $ λ x hx, finsupp.induction x (add_submonoid.zero_mem _) $ λ a b f ha hb hf, add_submonoid.add_mem _ (add_submonoid.subset_closure $ set.mem_Union.2 ⟨a, set.mem_range_self _⟩) hf /-- If two additive homomorphisms from `α →₀ M` are equal on each `single a b`, then they are equal. -/ lemma add_hom_ext [add_zero_class N] ⦃f g : (α →₀ M) →+ N⦄ (H : ∀ x y, f (single x y) = g (single x y)) : f = g := begin refine add_monoid_hom.eq_of_eq_on_mdense add_closure_Union_range_single (λ f hf, _), simp only [set.mem_Union, set.mem_range] at hf, rcases hf with ⟨x, y, rfl⟩, apply H end /-- If two additive homomorphisms from `α →₀ M` are equal on each `single a b`, then they are equal. We formulate this using equality of `add_monoid_hom`s so that `ext` tactic can apply a type-specific extensionality lemma after this one. E.g., if the fiber `M` is `ℕ` or `ℤ`, then it suffices to verify `f (single a 1) = g (single a 1)`. -/ @[ext] lemma add_hom_ext' [add_zero_class N] ⦃f g : (α →₀ M) →+ N⦄ (H : ∀ x, f.comp (single_add_hom x) = g.comp (single_add_hom x)) : f = g := add_hom_ext $ λ x, add_monoid_hom.congr_fun (H x) lemma mul_hom_ext [mul_one_class N] ⦃f g : multiplicative (α →₀ M) →* N⦄ (H : ∀ x y, f (multiplicative.of_add $ single x y) = g (multiplicative.of_add $ single x y)) : f = g := monoid_hom.ext $ add_monoid_hom.congr_fun $ @add_hom_ext α M (additive N) _ _ f.to_additive'' g.to_additive'' H @[ext] lemma mul_hom_ext' [mul_one_class N] {f g : multiplicative (α →₀ M) →* N} (H : ∀ x, f.comp (single_add_hom x).to_multiplicative = g.comp (single_add_hom x).to_multiplicative) : f = g := mul_hom_ext $ λ x, monoid_hom.congr_fun (H x) lemma map_range_add [add_zero_class N] {f : M → N} {hf : f 0 = 0} (hf' : ∀ x y, f (x + y) = f x + f y) (v₁ v₂ : α →₀ M) : map_range f hf (v₁ + v₂) = map_range f hf v₁ + map_range f hf v₂ := ext $ λ a, by simp only [hf', add_apply, map_range_apply] /-- Bundle `emb_domain f` as an additive map from `α →₀ M` to `β →₀ M`. -/ @[simps] def emb_domain.add_monoid_hom (f : α ↪ β) : (α →₀ M) →+ (β →₀ M) := { to_fun := λ v, emb_domain f v, map_zero' := by simp, map_add' := λ v w, begin ext b, by_cases h : b ∈ set.range f, { rcases h with ⟨a, rfl⟩, simp, }, { simp [emb_domain_notin_range, h], }, end, } @[simp] lemma emb_domain_add (f : α ↪ β) (v w : α →₀ M) : emb_domain f (v + w) = emb_domain f v + emb_domain f w := (emb_domain.add_monoid_hom f).map_add v w end add_zero_class section add_monoid variables [add_monoid M] instance : add_monoid (α →₀ M) := { add_monoid . zero := 0, add := (+), add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _, nsmul := λ n v, v.map_range ((•) n) (nsmul_zero _), nsmul_zero' := λ v, by { ext i, simp }, nsmul_succ' := λ n v, by { ext i, simp [nat.succ_eq_one_add, add_nsmul] }, .. finsupp.add_zero_class } end add_monoid end finsupp @[to_additive] lemma mul_equiv.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P] (h : N ≃* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ @[to_additive] lemma monoid_hom.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P] (h : N →* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ lemma ring_hom.map_finsupp_sum [has_zero M] [semiring R] [semiring S] (h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.sum g) = f.sum (λ a b, h (g a b)) := h.map_sum _ _ lemma ring_hom.map_finsupp_prod [has_zero M] [comm_semiring R] [comm_semiring S] (h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ @[to_additive] lemma monoid_hom.coe_finsupp_prod [has_zero β] [monoid N] [comm_monoid P] (f : α →₀ β) (g : α → β → N →* P) : ⇑(f.prod g) = f.prod (λ i fi, g i fi) := monoid_hom.coe_prod _ _ @[simp, to_additive] lemma monoid_hom.finsupp_prod_apply [has_zero β] [monoid N] [comm_monoid P] (f : α →₀ β) (g : α → β → N →* P) (x : N) : f.prod g x = f.prod (λ i fi, g i fi x) := monoid_hom.finset_prod_apply _ _ _ namespace finsupp section nat_sub instance nat_sub : has_sub (α →₀ ℕ) := ⟨zip_with (λ m n, m - n) (nat.sub_zero 0)⟩ @[simp] lemma coe_nat_sub (g₁ g₂ : α →₀ ℕ) : ⇑(g₁ - g₂) = g₁ - g₂ := rfl lemma nat_sub_apply (g₁ g₂ : α →₀ ℕ) (a : α) : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma single_nat_sub {a : α} {n₁ n₂ : ℕ} : single a (n₁ - n₂) = single a n₁ - single a n₂ := begin ext f, by_cases h : (a = f), { rw [h, nat_sub_apply, single_eq_same, single_eq_same, single_eq_same] }, rw [nat_sub_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h] end -- These next two lemmas are used in developing -- the partial derivative on `mv_polynomial`. lemma sub_single_one_add {a : α} {u u' : α →₀ ℕ} (h : u a ≠ 0) : u - single a 1 + u' = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u a), { contradiction }, { simp }, }, { simp [h], } end lemma add_sub_single_one {a : α} {u u' : α →₀ ℕ} (h : u' a ≠ 0) : u + (u' - single a 1) = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u' a), { contradiction }, { simp }, }, { simp [h], } end @[simp] lemma nat_zero_sub (f : α →₀ ℕ) : 0 - f = 0 := ext $ λ x, nat.zero_sub _ @[simp] lemma nat_sub_self (f : α →₀ ℕ) : f - f = 0 := ext $ λ x, nat.sub_self _ end nat_sub instance [add_comm_monoid M] : add_comm_monoid (α →₀ M) := { add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _, .. finsupp.add_monoid } instance [add_group G] : has_sub (α →₀ G) := ⟨zip_with has_sub.sub (sub_zero _)⟩ instance [add_group G] : add_group (α →₀ G) := { neg := map_range (has_neg.neg) neg_zero, sub := has_sub.sub, sub_eq_add_neg := λ x y, ext (λ i, sub_eq_add_neg _ _), add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _, gsmul := λ n v, v.map_range ((•) n) (gsmul_zero _), gsmul_zero' := λ v, by { ext i, simp }, gsmul_succ' := λ n v, by { ext i, simp [nat.succ_eq_one_add, add_gsmul] }, gsmul_neg' := λ n v, by { ext i, simp only [nat.succ_eq_add_one, map_range_apply, gsmul_neg_succ_of_nat, int.coe_nat_succ, neg_inj, add_gsmul, add_nsmul, one_gsmul, gsmul_coe_nat, one_nsmul] }, .. finsupp.add_monoid } instance [add_comm_group G] : add_comm_group (α →₀ G) := { add_comm := add_comm, ..finsupp.add_group } lemma single_multiset_sum [add_comm_monoid M] (s : multiset M) (a : α) : single a s.sum = (s.map (single a)).sum := multiset.induction_on s single_zero $ λ a s ih, by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons] lemma single_finset_sum [add_comm_monoid M] (s : finset ι) (f : ι → M) (a : α) : single a (∑ b in s, f b) = ∑ b in s, single a (f b) := begin transitivity, apply single_multiset_sum, rw [multiset.map_map], refl end lemma single_sum [has_zero M] [add_comm_monoid N] (s : ι →₀ M) (f : ι → M → N) (a : α) : single a (s.sum f) = s.sum (λd c, single a (f d c)) := single_finset_sum _ _ _ @[to_additive] lemma prod_neg_index [add_group G] [comm_monoid M] {g : α →₀ G} {h : α → G → M} (h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) := prod_map_range_index h0 @[simp] lemma coe_neg [add_group G] (g : α →₀ G) : ⇑(-g) = -g := rfl lemma neg_apply [add_group G] (g : α →₀ G) (a : α) : (- g) a = - g a := rfl @[simp] lemma coe_sub [add_group G] (g₁ g₂ : α →₀ G) : ⇑(g₁ - g₂) = g₁ - g₂ := rfl lemma sub_apply [add_group G] (g₁ g₂ : α →₀ G) (a : α) : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma support_neg [add_group G] {f : α →₀ G} : support (-f) = support f := finset.subset.antisymm support_map_range (calc support f = support (- (- f)) : congr_arg support (neg_neg _).symm ... ⊆ support (- f) : support_map_range) @[simp] lemma sum_apply [has_zero M] [add_comm_monoid N] {f : α →₀ M} {g : α → M → β →₀ N} {a₂ : β} : (f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) := (apply_add_hom a₂ : (β →₀ N) →+ _).map_sum _ _ lemma support_sum [decidable_eq β] [has_zero M] [add_comm_monoid N] {f : α →₀ M} {g : α → M → (β →₀ N)} : (f.sum g).support ⊆ f.support.bUnion (λa, (g a (f a)).support) := have ∀ c, f.sum (λ a b, g a b c) ≠ 0 → (∃ a, f a ≠ 0 ∧ ¬ (g a (f a)) c = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, mem_support_iff.mp ha, ne⟩, by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bUnion, sum_apply, exists_prop] @[simp] lemma sum_zero [has_zero M] [add_comm_monoid N] {f : α →₀ M} : f.sum (λa b, (0 : N)) = 0 := finset.sum_const_zero @[simp, to_additive] lemma prod_mul [has_zero M] [comm_monoid N] {f : α →₀ M} {h₁ h₂ : α → M → N} : f.prod (λa b, h₁ a b * h₂ a b) = f.prod h₁ * f.prod h₂ := finset.prod_mul_distrib @[simp, to_additive] lemma prod_inv [has_zero M] [comm_group G] {f : α →₀ M} {h : α → M → G} : f.prod (λa b, (h a b)⁻¹) = (f.prod h)⁻¹ := (((monoid_hom.id G)⁻¹).map_prod _ _).symm @[simp] lemma sum_sub [has_zero M] [add_comm_group G] {f : α →₀ M} {h₁ h₂ : α → M → G} : f.sum (λa b, h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ := finset.sum_sub_distrib @[to_additive] lemma prod_add_index [add_comm_monoid M] [comm_monoid N] {f g : α →₀ M} {h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := have hf : f.prod h = ∏ a in f.support ∪ g.support, h a (f a), from f.prod_of_support_subset (subset_union_left _ _) _ $ λ a ha, h_zero a, have hg : g.prod h = ∏ a in f.support ∪ g.support, h a (g a), from g.prod_of_support_subset (subset_union_right _ _) _ $ λ a ha, h_zero a, have hfg : (f + g).prod h = ∏ a in f.support ∪ g.support, h a ((f + g) a), from (f + g).prod_of_support_subset support_add _ $ λ a ha, h_zero a, by simp only [*, add_apply, prod_mul_distrib] @[simp] lemma sum_add_index' [add_comm_monoid M] [add_comm_monoid N] {f g : α →₀ M} (h : α → M →+ N) : (f + g).sum (λ x, h x) = f.sum (λ x, h x) + g.sum (λ x, h x) := sum_add_index (λ a, (h a).map_zero) (λ a, (h a).map_add) @[simp] lemma prod_add_index' [add_comm_monoid M] [comm_monoid N] {f g : α →₀ M} (h : α → multiplicative M →* N) : (f + g).prod (λ a b, h a (multiplicative.of_add b)) = f.prod (λ a b, h a (multiplicative.of_add b)) * g.prod (λ a b, h a (multiplicative.of_add b)) := prod_add_index (λ a, (h a).map_one) (λ a, (h a).map_mul) /-- The canonical isomorphism between families of additive monoid homomorphisms `α → (M →+ N)` and monoid homomorphisms `(α →₀ M) →+ N`. -/ def lift_add_hom [add_comm_monoid M] [add_comm_monoid N] : (α → M →+ N) ≃+ ((α →₀ M) →+ N) := { to_fun := λ F, { to_fun := λ f, f.sum (λ x, F x), map_zero' := finset.sum_empty, map_add' := λ _ _, sum_add_index (λ x, (F x).map_zero) (λ x, (F x).map_add) }, inv_fun := λ F x, F.comp $ single_add_hom x, left_inv := λ F, by { ext, simp }, right_inv := λ F, by { ext, simp }, map_add' := λ F G, by { ext, simp } } @[simp] lemma lift_add_hom_apply [add_comm_monoid M] [add_comm_monoid N] (F : α → M →+ N) (f : α →₀ M) : lift_add_hom F f = f.sum (λ x, F x) := rfl @[simp] lemma lift_add_hom_symm_apply [add_comm_monoid M] [add_comm_monoid N] (F : (α →₀ M) →+ N) (x : α) : lift_add_hom.symm F x = F.comp (single_add_hom x) := rfl lemma lift_add_hom_symm_apply_apply [add_comm_monoid M] [add_comm_monoid N] (F : (α →₀ M) →+ N) (x : α) (y : M) : lift_add_hom.symm F x y = F (single x y) := rfl @[simp] lemma lift_add_hom_single_add_hom [add_comm_monoid M] : lift_add_hom (single_add_hom : α → M →+ α →₀ M) = add_monoid_hom.id _ := lift_add_hom.to_equiv.apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma sum_single [add_comm_monoid M] (f : α →₀ M) : f.sum single = f := add_monoid_hom.congr_fun lift_add_hom_single_add_hom f @[simp] lemma lift_add_hom_apply_single [add_comm_monoid M] [add_comm_monoid N] (f : α → M →+ N) (a : α) (b : M) : lift_add_hom f (single a b) = f a b := sum_single_index (f a).map_zero @[simp] lemma lift_add_hom_comp_single [add_comm_monoid M] [add_comm_monoid N] (f : α → M →+ N) (a : α) : (lift_add_hom f).comp (single_add_hom a) = f a := add_monoid_hom.ext $ λ b, lift_add_hom_apply_single f a b lemma comp_lift_add_hom [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] (g : N →+ P) (f : α → M →+ N) : g.comp (lift_add_hom f) = lift_add_hom (λ a, g.comp (f a)) := lift_add_hom.symm_apply_eq.1 $ funext $ λ a, by rw [lift_add_hom_symm_apply, add_monoid_hom.comp_assoc, lift_add_hom_comp_single] lemma sum_sub_index [add_comm_group β] [add_comm_group γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := (lift_add_hom (λ a, add_monoid_hom.of_map_sub (h a) (h_sub a))).map_sub f g @[to_additive] lemma prod_emb_domain [has_zero M] [comm_monoid N] {v : α →₀ M} {f : α ↪ β} {g : β → M → N} : (v.emb_domain f).prod g = v.prod (λ a b, g (f a) b) := begin rw [prod, prod, support_emb_domain, finset.prod_map], simp_rw emb_domain_apply, end @[to_additive] lemma prod_finset_sum_index [add_comm_monoid M] [comm_monoid N] {s : finset ι} {g : ι → α →₀ M} {h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : ∏ i in s, (g i).prod h = (∑ i in s, g i).prod h := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, sum_insert has, prod_add_index h_zero h_add] @[to_additive] lemma prod_sum_index [add_comm_monoid M] [add_comm_monoid N] [comm_monoid P] {f : α →₀ M} {g : α → M → β →₀ N} {h : β → N → P} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f.sum g).prod h = f.prod (λa b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm lemma multiset_sum_sum_index [add_comm_monoid M] [add_comm_monoid N] (f : multiset (α →₀ M)) (h : α → M → N) (h₀ : ∀a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : M), h a (b₁ + b₂) = h a b₁ + h a b₂) : (f.sum.sum h) = (f.map $ λg:α →₀ M, g.sum h).sum := multiset.induction_on f rfl $ assume a s ih, by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index h₀ h₁, ih] lemma support_sum_eq_bUnion {α : Type*} {ι : Type*} {M : Type*} [add_comm_monoid M] {g : ι → α →₀ M} (s : finset ι) (h : ∀ i₁ i₂, i₁ ≠ i₂ → disjoint (g i₁).support (g i₂).support) : (∑ i in s, g i).support = s.bUnion (λ i, (g i).support) := begin apply finset.induction_on s, { simp }, { intros i s hi, simp only [hi, sum_insert, not_false_iff, bUnion_insert], intro hs, rw [finsupp.support_add_eq, hs], rw [hs], intros x hx, simp only [mem_bUnion, exists_prop, inf_eq_inter, ne.def, mem_inter] at hx, obtain ⟨hxi, j, hj, hxj⟩ := hx, have hn : i ≠ j := λ H, hi (H.symm ▸ hj), apply h _ _ hn, simp [hxi, hxj] } end lemma multiset_map_sum [has_zero M] {f : α →₀ M} {m : β → γ} {h : α → M → multiset β} : multiset.map m (f.sum h) = f.sum (λa b, (h a b).map m) := (multiset.map_add_monoid_hom m).map_sum _ f.support lemma multiset_sum_sum [has_zero M] [add_comm_monoid N] {f : α →₀ M} {h : α → M → multiset N} : multiset.sum (f.sum h) = f.sum (λa b, multiset.sum (h a b)) := (multiset.sum_add_monoid_hom : multiset N →+ N).map_sum _ f.support section map_range section equiv variables [has_zero M] [has_zero N] [has_zero P] /-- `finsupp.map_range` as an equiv. -/ @[simps apply] def map_range.equiv (f : M ≃ N) (hf : f 0 = 0) (hf' : f.symm 0 = 0) : (α →₀ M) ≃ (α →₀ N) := { to_fun := (map_range f hf : (α →₀ M) → (α →₀ N)), inv_fun := (map_range f.symm hf' : (α →₀ N) → (α →₀ M)), left_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw equiv.symm_comp_self, { exact map_range_id _ }, { refl }, end, right_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw equiv.self_comp_symm, { exact map_range_id _ }, { refl }, end } @[simp] lemma map_range.equiv_refl : map_range.equiv (equiv.refl M) rfl rfl = equiv.refl (α →₀ M) := equiv.ext map_range_id lemma map_range.equiv_trans (f : M ≃ N) (hf : f 0 = 0) (hf') (f₂ : N ≃ P) (hf₂ : f₂ 0 = 0) (hf₂') : (map_range.equiv (f.trans f₂) (by rw [equiv.trans_apply, hf, hf₂]) (by rw [equiv.symm_trans_apply, hf₂', hf']) : (α →₀ _) ≃ _) = (map_range.equiv f hf hf').trans (map_range.equiv f₂ hf₂ hf₂') := equiv.ext $ map_range_comp _ _ _ _ _ @[simp] lemma map_range.equiv_symm (f : M ≃ N) (hf hf') : ((map_range.equiv f hf hf').symm : (α →₀ _) ≃ _) = map_range.equiv f.symm hf' hf := equiv.ext $ λ x, rfl end equiv section zero_hom variables [has_zero M] [has_zero N] [has_zero P] /-- Composition with a fixed zero-preserving homomorphism is itself an zero-preserving homomorphism on functions. -/ @[simps] def map_range.zero_hom (f : zero_hom M N) : zero_hom (α →₀ M) (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), map_zero' := map_range_zero } @[simp] lemma map_range.zero_hom_id : map_range.zero_hom (zero_hom.id M) = zero_hom.id (α →₀ M) := zero_hom.ext map_range_id lemma map_range.zero_hom_comp (f : zero_hom N P) (f₂ : zero_hom M N) : (map_range.zero_hom (f.comp f₂) : zero_hom (α →₀ _) _) = (map_range.zero_hom f).comp (map_range.zero_hom f₂) := zero_hom.ext $ map_range_comp _ _ _ _ _ end zero_hom section add_monoid_hom variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] /-- Composition with a fixed additive homomorphism is itself an additive homomorphism on functions. -/ @[simps] def map_range.add_monoid_hom (f : M →+ N) : (α →₀ M) →+ (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), map_zero' := map_range_zero, map_add' := λ a b, map_range_add f.map_add _ _ } @[simp] lemma map_range.add_monoid_hom_id : map_range.add_monoid_hom (add_monoid_hom.id M) = add_monoid_hom.id (α →₀ M) := add_monoid_hom.ext map_range_id lemma map_range.add_monoid_hom_comp (f : N →+ P) (f₂ : M →+ N) : (map_range.add_monoid_hom (f.comp f₂) : (α →₀ _) →+ _) = (map_range.add_monoid_hom f).comp (map_range.add_monoid_hom f₂) := add_monoid_hom.ext $ map_range_comp _ _ _ _ _ @[simp] lemma map_range.add_monoid_hom_to_zero_hom (f : M →+ N) : (map_range.add_monoid_hom f).to_zero_hom = (map_range.zero_hom f.to_zero_hom : zero_hom (α →₀ _) _) := zero_hom.ext $ λ _, rfl lemma map_range_multiset_sum (f : M →+ N) (m : multiset (α →₀ M)) : map_range f f.map_zero m.sum = (m.map $ λx, map_range f f.map_zero x).sum := (map_range.add_monoid_hom f : (α →₀ _) →+ _).map_multiset_sum _ lemma map_range_finset_sum (f : M →+ N) (s : finset ι) (g : ι → (α →₀ M)) : map_range f f.map_zero (∑ x in s, g x) = ∑ x in s, map_range f f.map_zero (g x) := (map_range.add_monoid_hom f : (α →₀ _) →+ _).map_sum _ _ /-- `finsupp.map_range.add_monoid_hom` as an equiv. -/ @[simps apply] def map_range.add_equiv (f : M ≃+ N) : (α →₀ M) ≃+ (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), inv_fun := (map_range f.symm f.symm.map_zero : (α →₀ N) → (α →₀ M)), left_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw add_equiv.symm_comp_self, { exact map_range_id _ }, { refl }, end, right_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw add_equiv.self_comp_symm, { exact map_range_id _ }, { refl }, end, ..(map_range.add_monoid_hom f.to_add_monoid_hom) } @[simp] lemma map_range.add_equiv_refl : map_range.add_equiv (add_equiv.refl M) = add_equiv.refl (α →₀ M) := add_equiv.ext map_range_id lemma map_range.add_equiv_trans (f : M ≃+ N) (f₂ : N ≃+ P) : (map_range.add_equiv (f.trans f₂) : (α →₀ _) ≃+ _) = (map_range.add_equiv f).trans (map_range.add_equiv f₂) := add_equiv.ext $ map_range_comp _ _ _ _ _ @[simp] lemma map_range.add_equiv_symm (f : M ≃+ N) : ((map_range.add_equiv f).symm : (α →₀ _) ≃+ _) = map_range.add_equiv f.symm := add_equiv.ext $ λ x, rfl @[simp] lemma map_range.add_equiv_to_add_monoid_hom (f : M ≃+ N) : (map_range.add_equiv f : (α →₀ _) ≃+ _).to_add_monoid_hom = (map_range.add_monoid_hom f.to_add_monoid_hom : (α →₀ _) →+ _) := add_monoid_hom.ext $ λ _, rfl @[simp] lemma map_range.add_equiv_to_equiv (f : M ≃+ N) : (map_range.add_equiv f).to_equiv = (map_range.equiv f.to_equiv f.map_zero f.symm.map_zero : (α →₀ _) ≃ _) := equiv.ext $ λ _, rfl end add_monoid_hom end map_range /-! ### Declarations about `map_domain` -/ section map_domain variables [add_comm_monoid M] {v v₁ v₂ : α →₀ M} /-- Given `f : α → β` and `v : α →₀ M`, `map_domain f v : β →₀ M` is the finitely supported function whose value at `a : β` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α → β) (v : α →₀ M) : β →₀ M := v.sum $ λa, single (f a) lemma map_domain_apply {f : α → β} (hf : function.injective f) (x : α →₀ M) (a : α) : map_domain f x (f a) = x a := begin rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same], { assume b _ hba, exact single_eq_of_ne (hf.ne hba) }, { assume h, rw [not_mem_support_iff.1 h, single_zero, zero_apply] } end lemma map_domain_notin_range {f : α → β} (x : α →₀ M) (a : β) (h : a ∉ set.range f) : map_domain f x a = 0 := begin rw [map_domain, sum_apply, sum], exact finset.sum_eq_zero (assume a' h', single_eq_of_ne $ assume eq, h $ eq ▸ set.mem_range_self _) end @[simp] lemma map_domain_id : map_domain id v = v := sum_single _ lemma map_domain_comp {f : α → β} {g : β → γ} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := begin refine ((sum_sum_index _ _).trans _).symm, { intros, exact single_zero }, { intros, exact single_add }, refine sum_congr rfl (λ _ _, sum_single_index _), { exact single_zero } end @[simp] lemma map_domain_single {f : α → β} {a : α} {b : M} : map_domain f (single a b) = single (f a) b := sum_single_index single_zero @[simp] lemma map_domain_zero {f : α → β} : map_domain f (0 : α →₀ M) = (0 : β →₀ M) := sum_zero_index lemma map_domain_congr {f g : α → β} (h : ∀x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ λ _ H, by simp only [h _ H] lemma map_domain_add {f : α → β} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ := sum_add_index (λ _, single_zero) (λ _ _ _, single_add) @[simp] lemma map_domain_equiv_apply {f : α ≃ β} (x : α →₀ M) (a : β) : map_domain f x a = x (f.symm a) := begin conv_lhs { rw ←f.apply_symm_apply a }, exact map_domain_apply f.injective _ _, end /-- `finsupp.map_domain` is an `add_monoid_hom`. -/ @[simps] def map_domain.add_monoid_hom (f : α → β) : (α →₀ M) →+ (β →₀ M) := { to_fun := map_domain f, map_zero' := map_domain_zero, map_add' := λ _ _, map_domain_add} @[simp] lemma map_domain.add_monoid_hom_id : map_domain.add_monoid_hom id = add_monoid_hom.id (α →₀ M) := add_monoid_hom.ext $ λ _, map_domain_id lemma map_domain.add_monoid_hom_comp (f : β → γ) (g : α → β) : (map_domain.add_monoid_hom (f ∘ g) : (α →₀ M) →+ (γ →₀ M)) = (map_domain.add_monoid_hom f).comp (map_domain.add_monoid_hom g) := add_monoid_hom.ext $ λ _, map_domain_comp lemma map_domain_finset_sum {f : α → β} {s : finset ι} {v : ι → α →₀ M} : map_domain f (∑ i in s, v i) = ∑ i in s, map_domain f (v i) := (map_domain.add_monoid_hom f : (α →₀ M) →+ β →₀ M).map_sum _ _ lemma map_domain_sum [has_zero N] {f : α → β} {s : α →₀ N} {v : α → N → α →₀ M} : map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) := (map_domain.add_monoid_hom f : (α →₀ M) →+ β →₀ M).map_finsupp_sum _ _ lemma map_domain_support [decidable_eq β] {f : α → β} {s : α →₀ M} : (s.map_domain f).support ⊆ s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bUnion_mono $ assume a ha, support_single_subset) $ by rw [finset.bUnion_singleton]; exact subset.refl _ @[to_additive] lemma prod_map_domain_index [comm_monoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (h_zero : ∀b, h b 0 = 1) (h_add : ∀b m₁ m₂, h b (m₁ + m₂) = h b m₁ * h b m₂) : (map_domain f s).prod h = s.prod (λa m, h (f a) m) := (prod_sum_index h_zero h_add).trans $ prod_congr rfl $ λ _ _, prod_single_index (h_zero _) /-- A version of `sum_map_domain_index` that takes a bundled `add_monoid_hom`, rather than separate linearity hypotheses. -/ -- Note that in `prod_map_domain_index`, `M` is still an additive monoid, -- so there is no analogous version in terms of `monoid_hom`. @[simp] lemma sum_map_domain_index_add_monoid_hom [add_comm_monoid N] {f : α → β} {s : α →₀ M} (h : β → M →+ N) : (map_domain f s).sum (λ b m, h b m) = s.sum (λ a m, h (f a) m) := @sum_map_domain_index _ _ _ _ _ _ _ _ (λ b m, h b m) (λ b, (h b).map_zero) (λ b m₁ m₂, (h b).map_add _ _) lemma emb_domain_eq_map_domain (f : α ↪ β) (v : α →₀ M) : emb_domain f v = map_domain f v := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a, rfl⟩, rw [map_domain_apply f.injective, emb_domain_apply] }, { rw [map_domain_notin_range, emb_domain_notin_range]; assumption } end @[to_additive] lemma prod_map_domain_index_inj [comm_monoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (hf : function.injective f) : (s.map_domain f).prod h = s.prod (λa b, h (f a) b) := by rw [←function.embedding.coe_fn_mk f hf, ←emb_domain_eq_map_domain, prod_emb_domain] lemma map_domain_injective {f : α → β} (hf : function.injective f) : function.injective (map_domain f : (α →₀ M) → (β →₀ M)) := begin assume v₁ v₂ eq, ext a, have : map_domain f v₁ (f a) = map_domain f v₂ (f a), { rw eq }, rwa [map_domain_apply hf, map_domain_apply hf] at this, end lemma map_domain.add_monoid_hom_comp_map_range [add_comm_monoid N] (f : α → β) (g : M →+ N) : (map_domain.add_monoid_hom f).comp (map_range.add_monoid_hom g) = (map_range.add_monoid_hom g).comp (map_domain.add_monoid_hom f) := by { ext, simp } /-- When `g` preserves addition, `map_range` and `map_domain` commute. -/ lemma map_domain_map_range [add_comm_monoid N] (f : α → β) (v : α →₀ M) (g : M → N) (h0 : g 0 = 0) (hadd : ∀ x y, g (x + y) = g x + g y) : map_domain f (map_range g h0 v) = map_range g h0 (map_domain f v) := let g' : M →+ N := { to_fun := g, map_zero' := h0, map_add' := hadd} in add_monoid_hom.congr_fun (map_domain.add_monoid_hom_comp_map_range f g') v end map_domain /-! ### Declarations about `comap_domain` -/ section comap_domain /-- Given `f : α → β`, `l : β →₀ M` and a proof `hf` that `f` is injective on the preimage of `l.support`, `comap_domain f l hf` is the finitely supported function from `α` to `M` given by composing `l` with `f`. -/ def comap_domain [has_zero M] (f : α → β) (l : β →₀ M) (hf : set.inj_on f (f ⁻¹' ↑l.support)) : α →₀ M := { support := l.support.preimage f hf, to_fun := (λ a, l (f a)), mem_support_to_fun := begin intros a, simp only [finset.mem_def.symm, finset.mem_preimage], exact l.mem_support_to_fun (f a), end } @[simp] lemma comap_domain_apply [has_zero M] (f : α → β) (l : β →₀ M) (hf : set.inj_on f (f ⁻¹' ↑l.support)) (a : α) : comap_domain f l hf a = l (f a) := rfl lemma sum_comap_domain [has_zero M] [add_comm_monoid N] (f : α → β) (l : β →₀ M) (g : β → M → N) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : (comap_domain f l hf.inj_on).sum (g ∘ f) = l.sum g := begin simp only [sum, comap_domain_apply, (∘)], simp [comap_domain, finset.sum_preimage_of_bij f _ _ (λ x, g x (l x))], end lemma eq_zero_of_comap_domain_eq_zero [add_comm_monoid M] (f : α → β) (l : β →₀ M) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : comap_domain f l hf.inj_on = 0 → l = 0 := begin rw [← support_eq_empty, ← support_eq_empty, comap_domain], simp only [finset.ext_iff, finset.not_mem_empty, iff_false, mem_preimage], assume h a ha, cases hf.2.2 ha with b hb, exact h b (hb.2.symm ▸ ha) end lemma map_domain_comap_domain [add_comm_monoid M] (f : α → β) (l : β →₀ M) (hf : function.injective f) (hl : ↑l.support ⊆ set.range f): map_domain f (comap_domain f l (hf.inj_on _)) = l := begin ext a, by_cases h_cases: a ∈ set.range f, { rcases set.mem_range.1 h_cases with ⟨b, hb⟩, rw [hb.symm, map_domain_apply hf, comap_domain_apply] }, { rw map_domain_notin_range _ _ h_cases, by_contra h_contr, apply h_cases (hl $ finset.mem_coe.2 $ mem_support_iff.2 $ λ h, h_contr h.symm) } end end comap_domain section option /-- Restrict a finitely supported function on `option α` to a finitely supported function on `α`. -/ def some [has_zero M] (f : option α →₀ M) : α →₀ M := f.comap_domain option.some (λ _, by simp) @[simp] lemma some_apply [has_zero M] (f : option α →₀ M) (a : α) : f.some a = f (option.some a) := rfl @[simp] lemma some_zero [has_zero M] : (0 : option α →₀ M).some = 0 := by { ext, simp, } @[simp] lemma some_add [add_comm_monoid M] (f g : option α →₀ M) : (f + g).some = f.some + g.some := by { ext, simp, } @[simp] lemma some_single_none [has_zero M] (m : M) : (single none m : option α →₀ M).some = 0 := by { ext, simp, } @[simp] lemma some_single_some [has_zero M] (a : α) (m : M) : (single (option.some a) m : option α →₀ M).some = single a m := by { ext b, simp [single_apply], } @[to_additive] lemma prod_option_index [add_comm_monoid M] [comm_monoid N] (f : option α →₀ M) (b : option α → M → N) (h_zero : ∀ o, b o 0 = 1) (h_add : ∀ o m₁ m₂, b o (m₁ + m₂) = b o m₁ * b o m₂) : f.prod b = b none (f none) * f.some.prod (λ a, b (option.some a)) := begin apply induction_linear f, { simp [h_zero], }, { intros f₁ f₂ h₁ h₂, rw [finsupp.prod_add_index, h₁, h₂, some_add, finsupp.prod_add_index], simp only [h_add, pi.add_apply, finsupp.coe_add], rw mul_mul_mul_comm, all_goals { simp [h_zero, h_add], }, }, { rintros (_|a) m; simp [h_zero, h_add], } end lemma sum_option_index_smul [semiring R] [add_comm_monoid M] [module R M] (f : option α →₀ R) (b : option α → M) : f.sum (λ o r, r • b o) = f none • b none + f.some.sum (λ a r, r • b (option.some a)) := f.sum_option_index _ (λ _, zero_smul _ _) (λ _ _ _, add_smul _ _ _) end option /-! ### Declarations about `equiv_congr_left` -/ section equiv_congr_left variable [has_zero M] /-- Given `f : α ≃ β`, we can map `l : α →₀ M` to `equiv_map_domain f l : β →₀ M` (computably) by mapping the support forwards and the function backwards. -/ def equiv_map_domain (f : α ≃ β) (l : α →₀ M) : β →₀ M := { support := l.support.map f.to_embedding, to_fun := λ a, l (f.symm a), mem_support_to_fun := λ a, by simp only [finset.mem_map_equiv, mem_support_to_fun]; refl } @[simp] lemma equiv_map_domain_apply (f : α ≃ β) (l : α →₀ M) (b : β) : equiv_map_domain f l b = l (f.symm b) := rfl lemma equiv_map_domain_symm_apply (f : α ≃ β) (l : β →₀ M) (a : α) : equiv_map_domain f.symm l a = l (f a) := rfl @[simp] lemma equiv_map_domain_refl (l : α →₀ M) : equiv_map_domain (equiv.refl _) l = l := by ext x; refl lemma equiv_map_domain_refl' : equiv_map_domain (equiv.refl _) = @id (α →₀ M) := by ext x; refl lemma equiv_map_domain_trans (f : α ≃ β) (g : β ≃ γ) (l : α →₀ M) : equiv_map_domain (f.trans g) l = equiv_map_domain g (equiv_map_domain f l) := by ext x; refl lemma equiv_map_domain_trans' (f : α ≃ β) (g : β ≃ γ) : @equiv_map_domain _ _ M _ (f.trans g) = equiv_map_domain g ∘ equiv_map_domain f := by ext x; refl @[simp] lemma equiv_map_domain_single (f : α ≃ β) (a : α) (b : M) : equiv_map_domain f (single a b) = single (f a) b := by ext x; simp only [single_apply, equiv.apply_eq_iff_eq_symm_apply, equiv_map_domain_apply]; congr @[simp] lemma equiv_map_domain_zero {f : α ≃ β} : equiv_map_domain f (0 : α →₀ M) = (0 : β →₀ M) := by ext x; simp only [equiv_map_domain_apply, coe_zero, pi.zero_apply] lemma equiv_map_domain_eq_map_domain {M} [add_comm_monoid M] (f : α ≃ β) (l : α →₀ M) : equiv_map_domain f l = map_domain f l := by ext x; simp [map_domain_equiv_apply] /-- Given `f : α ≃ β`, the finitely supported function spaces are also in bijection: `(α →₀ M) ≃ (β →₀ M)`. This is the finitely-supported version of `equiv.Pi_congr_left`. -/ def equiv_congr_left (f : α ≃ β) : (α →₀ M) ≃ (β →₀ M) := by refine ⟨equiv_map_domain f, equiv_map_domain f.symm, λ f, _, λ f, _⟩; ext x; simp only [equiv_map_domain_apply, equiv.symm_symm, equiv.symm_apply_apply, equiv.apply_symm_apply] @[simp] lemma equiv_congr_left_apply (f : α ≃ β) (l : α →₀ M) : equiv_congr_left f l = equiv_map_domain f l := rfl @[simp] lemma equiv_congr_left_symm (f : α ≃ β) : (@equiv_congr_left _ _ M _ f).symm = equiv_congr_left f.symm := rfl end equiv_congr_left /-! ### Declarations about `filter` -/ section filter section has_zero variables [has_zero M] (p : α → Prop) (f : α →₀ M) /-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : α → Prop) (f : α →₀ M) : α →₀ M := { to_fun := λ a, if p a then f a else 0, support := f.support.filter (λ a, p a), mem_support_to_fun := λ a, by split_ifs; { simp only [h, mem_filter, mem_support_iff], tauto } } lemma filter_apply (a : α) [D : decidable (p a)] : f.filter p a = if p a then f a else 0 := by rw subsingleton.elim D; refl lemma filter_eq_indicator : ⇑(f.filter p) = set.indicator {x | p x} f := rfl @[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := if_pos h @[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 := if_neg h @[simp] lemma support_filter [D : decidable_pred p] : (f.filter p).support = f.support.filter p := by rw subsingleton.elim D; refl lemma filter_zero : (0 : α →₀ M).filter p = 0 := by rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty] @[simp] lemma filter_single_of_pos {a : α} {b : M} (h : p a) : (single a b).filter p = single a b := coe_fn_injective $ by simp [filter_eq_indicator, set.subset_def, mem_support_single, h] @[simp] lemma filter_single_of_neg {a : α} {b : M} (h : ¬ p a) : (single a b).filter p = 0 := ext $ by simp [filter_eq_indicator, single_apply_eq_zero, @imp.swap (p _), h] end has_zero lemma filter_pos_add_filter_neg [add_zero_class M] (f : α →₀ M) (p : α → Prop) : f.filter p + f.filter (λa, ¬ p a) = f := coe_fn_injective $ set.indicator_self_add_compl {x | p x} f end filter /-! ### Declarations about `frange` -/ section frange variables [has_zero M] /-- `frange f` is the image of `f` on the support of `f`. -/ def frange (f : α →₀ M) : finset M := finset.image f f.support theorem mem_frange {f : α →₀ M} {y : M} : y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y := finset.mem_image.trans ⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩, λ ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩ theorem zero_not_mem_frange {f : α →₀ M} : (0:M) ∉ f.frange := λ H, (mem_frange.1 H).1 rfl theorem frange_single {x : α} {y : M} : frange (single x y) ⊆ {y} := λ r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 ▸ (by rw single_apply at ht2 ⊢; split_ifs at ht2 ⊢; [exact finset.mem_singleton_self _, cc]) end frange /-! ### Declarations about `subtype_domain` -/ section subtype_domain section zero variables [has_zero M] {p : α → Prop} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain (p : α → Prop) (f : α →₀ M) : (subtype p →₀ M) := ⟨f.support.subtype p, f ∘ coe, λ a, by simp only [mem_subtype, mem_support_iff]⟩ @[simp] lemma support_subtype_domain [D : decidable_pred p] {f : α →₀ M} : (subtype_domain p f).support = f.support.subtype p := by rw subsingleton.elim D; refl @[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ M} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ M) = 0 := rfl lemma subtype_domain_eq_zero_iff' {f : α →₀ M} : f.subtype_domain p = 0 ↔ ∀ x, p x → f x = 0 := by simp_rw [← support_eq_empty, support_subtype_domain, subtype_eq_empty, not_mem_support_iff] lemma subtype_domain_eq_zero_iff {f : α →₀ M} (hf : ∀ x ∈ f.support , p x) : f.subtype_domain p = 0 ↔ f = 0 := subtype_domain_eq_zero_iff'.trans ⟨λ H, ext $ λ x, if hx : p x then H x hx else not_mem_support_iff.1 $ mt (hf x) hx, λ H x _, by simp [H]⟩ @[to_additive] lemma prod_subtype_domain_index [comm_monoid N] {v : α →₀ M} {h : α → M → N} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λa b, h a b) = v.prod h := prod_bij (λp _, p.val) (λ _, mem_subtype.1) (λ _ _, rfl) (λ _ _ _ _, subtype.eq) (λ b hb, ⟨⟨b, hp b hb⟩, mem_subtype.2 hb, rfl⟩) end zero section add_zero_class variables [add_zero_class M] {p : α → Prop} {v v' : α →₀ M} @[simp] lemma subtype_domain_add {v v' : α →₀ M} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ _, rfl /-- `subtype_domain` but as an `add_monoid_hom`. -/ def subtype_domain_add_monoid_hom : (α →₀ M) →+ subtype p →₀ M := { to_fun := subtype_domain p, map_zero' := subtype_domain_zero, map_add' := λ _ _, subtype_domain_add } /-- `finsupp.filter` as an `add_monoid_hom`. -/ def filter_add_hom (p : α → Prop) : (α →₀ M) →+ (α →₀ M) := { to_fun := filter p, map_zero' := filter_zero p, map_add' := λ f g, coe_fn_injective $ set.indicator_add {x | p x} f g } @[simp] lemma filter_add {v v' : α →₀ M} : (v + v').filter p = v.filter p + v'.filter p := (filter_add_hom p).map_add v v' end add_zero_class section comm_monoid variables [add_comm_monoid M] {p : α → Prop} lemma subtype_domain_sum {s : finset ι} {h : ι → α →₀ M} : (∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p := (subtype_domain_add_monoid_hom : _ →+ subtype p →₀ M).map_sum _ s lemma subtype_domain_finsupp_sum [has_zero N] {s : β →₀ N} {h : β → N → α →₀ M} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum lemma filter_sum (s : finset ι) (f : ι → α →₀ M) : (∑ a in s, f a).filter p = ∑ a in s, filter p (f a) := (filter_add_hom p : (α →₀ M) →+ _).map_sum f s lemma filter_eq_sum (p : α → Prop) [D : decidable_pred p] (f : α →₀ M) : f.filter p = ∑ i in f.support.filter p, single i (f i) := (f.filter p).sum_single.symm.trans $ finset.sum_congr (by rw subsingleton.elim D; refl) $ λ x hx, by rw [filter_apply_pos _ _ (mem_filter.1 hx).2] end comm_monoid section group variables [add_group G] {p : α → Prop} {v v' : α →₀ G} @[simp] lemma subtype_domain_neg : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ _, rfl @[simp] lemma subtype_domain_sub : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ _, rfl @[simp] lemma single_neg {a : α} {b : G} : single a (-b) = -single a b := (single_add_hom a : G →+ _).map_neg b @[simp] lemma single_sub {a : α} {b₁ b₂ : G} : single a (b₁ - b₂) = single a b₁ - single a b₂ := (single_add_hom a : G →+ _).map_sub b₁ b₂ end group end subtype_domain /-! ### Declarations relating `finsupp` to `multiset` -/ section multiset /-- Given `f : α →₀ ℕ`, `f.to_multiset` is the multiset with multiplicities given by the values of `f` on the elements of `α`. We define this function as an `add_equiv`. -/ def to_multiset : (α →₀ ℕ) ≃+ multiset α := { to_fun := λ f, f.sum (λa n, n • {a}), inv_fun := λ s, ⟨s.to_finset, λ a, s.count a, λ a, by simp⟩, left_inv := λ f, ext $ λ a, by { simp only [sum, multiset.count_sum', multiset.count_singleton, mul_boole, coe_mk, multiset.mem_to_finset, iff_self, not_not, mem_support_iff, ite_eq_left_iff, ne.def, multiset.count_eq_zero, multiset.count_nsmul, finset.sum_ite_eq, ite_not], exact eq.symm }, right_inv := λ s, by simp only [sum, coe_mk, multiset.to_finset_sum_count_nsmul_eq], map_add' := λ f g, sum_add_index (λ a, zero_nsmul _) (λ a, add_nsmul _) } lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 := rfl lemma to_multiset_add (m n : α →₀ ℕ) : (m + n).to_multiset = m.to_multiset + n.to_multiset := to_multiset.map_add m n lemma to_multiset_apply (f : α →₀ ℕ) : f.to_multiset = f.sum (λ a n, n • {a}) := rfl @[simp] lemma to_multiset_symm_apply (s : multiset α) (x : α) : finsupp.to_multiset.symm s x = s.count x := rfl @[simp] lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (single a n) = n • {a} := by rw [to_multiset_apply, sum_single_index]; apply zero_nsmul lemma to_multiset_sum {ι : Type*} {f : ι → α →₀ ℕ} (s : finset ι) : finsupp.to_multiset (∑ i in s, f i) = ∑ i in s, finsupp.to_multiset (f i) := add_equiv.map_sum _ _ _ lemma to_multiset_sum_single {ι : Type*} (s : finset ι) (n : ℕ) : finsupp.to_multiset (∑ i in s, single i n) = n • s.val := by simp_rw [to_multiset_sum, finsupp.to_multiset_single, sum_nsmul, sum_multiset_singleton] lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λa, id) := by simp [to_multiset_apply, add_monoid_hom.map_finsupp_sum, function.id_def] lemma to_multiset_map (f : α →₀ ℕ) (g : α → β) : f.to_multiset.map g = (f.map_domain g).to_multiset := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single, to_multiset_single, to_multiset_add, to_multiset_single, ← multiset.coe_map_add_monoid_hom, (multiset.map_add_monoid_hom g).map_nsmul], refl } end @[simp] lemma prod_to_multiset [comm_monoid M] (f : M →₀ ℕ) : f.to_multiset.prod = f.prod (λa n, a ^ n) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index, finsupp.prod_single_index, multiset.prod_nsmul, multiset.prod_singleton], { exact pow_zero a }, { exact pow_zero }, { exact pow_add } } end @[simp] lemma to_finset_to_multiset [decidable_eq α] (f : α →₀ ℕ) : f.to_multiset.to_finset = f.support := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.to_finset_zero, support_zero] }, { assume a n f ha hn ih, rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq, support_single_ne_zero hn, multiset.to_finset_nsmul _ _ hn, multiset.to_finset_singleton], refine disjoint.mono_left support_single_subset _, rwa [finset.singleton_disjoint] } end @[simp] lemma count_to_multiset [decidable_eq α] (f : α →₀ ℕ) (a : α) : f.to_multiset.count a = f a := calc f.to_multiset.count a = f.sum (λx n, (n • {x} : multiset α).count a) : (multiset.count_add_monoid_hom a).map_sum _ f.support ... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_nsmul] ... = f a * ({a} : multiset α).count a : sum_eq_single _ (λ a' _ H, by simp only [multiset.count_singleton, if_false, H.symm, mul_zero]) (λ H, by simp only [not_mem_support_iff.1 H, zero_mul]) ... = f a : by rw [multiset.count_singleton_self, mul_one] lemma mem_support_multiset_sum [add_comm_monoid M] {s : multiset (α →₀ M)} (a : α) : a ∈ s.sum.support → ∃f∈s, a ∈ (f : α →₀ M).support := multiset.induction_on s false.elim begin assume f s ih ha, by_cases a ∈ f.support, { exact ⟨f, multiset.mem_cons_self _ _, h⟩ }, { simp only [multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha, rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩, exact ⟨f', multiset.mem_cons_of_mem h₀, h₁⟩ } end lemma mem_support_finset_sum [add_comm_monoid M] {s : finset ι} {h : ι → α →₀ M} (a : α) (ha : a ∈ (∑ c in s, h c).support) : ∃ c ∈ s, a ∈ (h c).support := let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in ⟨c, hc, eq.symm ▸ hfa⟩ @[simp] lemma mem_to_multiset (f : α →₀ ℕ) (i : α) : i ∈ f.to_multiset ↔ i ∈ f.support := by rw [← multiset.count_ne_zero, finsupp.count_to_multiset, finsupp.mem_support_iff] end multiset /-! ### Declarations about `curry` and `uncurry` -/ section curry_uncurry variables [add_comm_monoid M] [add_comm_monoid N] /-- Given a finitely supported function `f` from a product type `α × β` to `γ`, `curry f` is the "curried" finitely supported function from `α` to the type of finitely supported functions from `β` to `γ`. -/ protected def curry (f : (α × β) →₀ M) : α →₀ (β →₀ M) := f.sum $ λp c, single p.1 (single p.2 c) @[simp] lemma curry_apply (f : (α × β) →₀ M) (x : α) (y : β) : f.curry x y = f (x, y) := begin have : ∀ (b : α × β), single b.fst (single b.snd (f b)) x y = if b = (x, y) then f b else 0, { rintros ⟨b₁, b₂⟩, simp [single_apply, ite_apply, prod.ext_iff, ite_and], split_ifs; simp [single_apply, *] }, rw [finsupp.curry, sum_apply, sum_apply, finsupp.sum, finset.sum_eq_single, this, if_pos rfl], { intros b hb b_ne, rw [this b, if_neg b_ne] }, { intros hxy, rw [this (x, y), if_pos rfl, not_mem_support_iff.mp hxy] } end lemma sum_curry_index (f : (α × β) →₀ M) (g : α → β → M → N) (hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) : f.curry.sum (λa f, f.sum (g a)) = f.sum (λp c, g p.1 p.2 c) := begin rw [finsupp.curry], transitivity, { exact sum_sum_index (assume a, sum_zero_index) (assume a b₀ b₁, sum_add_index (assume a, hg₀ _ _) (assume c d₀ d₁, hg₁ _ _ _ _)) }, congr, funext p c, transitivity, { exact sum_single_index sum_zero_index }, exact sum_single_index (hg₀ _ _) end /-- Given a finitely supported function `f` from `α` to the type of finitely supported functions from `β` to `M`, `uncurry f` is the "uncurried" finitely supported function from `α × β` to `M`. -/ protected def uncurry (f : α →₀ (β →₀ M)) : (α × β) →₀ M := f.sum $ λa g, g.sum $ λb c, single (a, b) c /-- `finsupp_prod_equiv` defines the `equiv` between `((α × β) →₀ M)` and `(α →₀ (β →₀ M))` given by currying and uncurrying. -/ def finsupp_prod_equiv : ((α × β) →₀ M) ≃ (α →₀ (β →₀ M)) := by refine ⟨finsupp.curry, finsupp.uncurry, λ f, _, λ f, _⟩; simp only [ finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] lemma filter_curry (f : α × β →₀ M) (p : α → Prop) : (f.filter (λa:α×β, p a.1)).curry = f.curry.filter p := begin rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, filter_sum, support_filter, sum_filter], refine finset.sum_congr rfl _, rintros ⟨a₁, a₂⟩ ha, dsimp only, split_ifs, { rw [filter_apply_pos, filter_single_of_pos]; exact h }, { rwa [filter_single_of_neg] } end lemma support_curry [decidable_eq α] (f : α × β →₀ M) : f.curry.support ⊆ f.support.image prod.fst := begin rw ← finset.bUnion_singleton, refine finset.subset.trans support_sum _, refine finset.bUnion_mono (assume a _, support_single_subset) end end curry_uncurry section sum /-- `finsupp.sum_elim f g` maps `inl x` to `f x` and `inr y` to `g y`. -/ def sum_elim {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) : α ⊕ β →₀ γ := on_finset ((f.support.map ⟨_, sum.inl_injective⟩) ∪ g.support.map ⟨_, sum.inr_injective⟩) (sum.elim f g) (λ ab h, by { cases ab with a b; simp only [sum.elim_inl, sum.elim_inr] at h; simpa }) @[simp] lemma coe_sum_elim {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) : ⇑(sum_elim f g) = sum.elim f g := rfl lemma sum_elim_apply {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : α ⊕ β) : sum_elim f g x = sum.elim f g x := rfl lemma sum_elim_inl {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : α) : sum_elim f g (sum.inl x) = f x := rfl lemma sum_elim_inr {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : β) : sum_elim f g (sum.inr x) = g x := rfl /-- The equivalence between `(α ⊕ β) →₀ γ` and `(α →₀ γ) × (β →₀ γ)`. This is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/ @[simps apply symm_apply] def sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] : ((α ⊕ β) →₀ γ) ≃ (α →₀ γ) × (β →₀ γ) := { to_fun := λ f, ⟨f.comap_domain sum.inl (sum.inl_injective.inj_on _), f.comap_domain sum.inr (sum.inr_injective.inj_on _)⟩, inv_fun := λ fg, sum_elim fg.1 fg.2, left_inv := λ f, by { ext ab, cases ab with a b; simp }, right_inv := λ fg, by { ext; simp } } lemma fst_sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] (f : (α ⊕ β) →₀ γ) (x : α) : (sum_finsupp_equiv_prod_finsupp f).1 x = f (sum.inl x) := rfl lemma snd_sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] (f : (α ⊕ β) →₀ γ) (y : β) : (sum_finsupp_equiv_prod_finsupp f).2 y = f (sum.inr y) := rfl lemma sum_finsupp_equiv_prod_finsupp_symm_inl {α β γ : Type*} [has_zero γ] (fg : (α →₀ γ) × (β →₀ γ)) (x : α) : (sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x := rfl lemma sum_finsupp_equiv_prod_finsupp_symm_inr {α β γ : Type*} [has_zero γ] (fg : (α →₀ γ) × (β →₀ γ)) (y : β) : (sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y := rfl variables [add_monoid M] /-- The additive equivalence between `(α ⊕ β) →₀ M` and `(α →₀ M) × (β →₀ M)`. This is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/ @[simps apply symm_apply] def sum_finsupp_add_equiv_prod_finsupp {α β : Type*} : ((α ⊕ β) →₀ M) ≃+ (α →₀ M) × (β →₀ M) := { map_add' := by { intros, ext; simp only [equiv.to_fun_as_coe, prod.fst_add, prod.snd_add, add_apply, snd_sum_finsupp_equiv_prod_finsupp, fst_sum_finsupp_equiv_prod_finsupp] }, .. sum_finsupp_equiv_prod_finsupp } lemma fst_sum_finsupp_add_equiv_prod_finsupp {α β : Type*} (f : (α ⊕ β) →₀ M) (x : α) : (sum_finsupp_add_equiv_prod_finsupp f).1 x = f (sum.inl x) := rfl lemma snd_sum_finsupp_add_equiv_prod_finsupp {α β : Type*} (f : (α ⊕ β) →₀ M) (y : β) : (sum_finsupp_add_equiv_prod_finsupp f).2 y = f (sum.inr y) := rfl lemma sum_finsupp_add_equiv_prod_finsupp_symm_inl {α β : Type*} (fg : (α →₀ M) × (β →₀ M)) (x : α) : (sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x := rfl lemma sum_finsupp_add_equiv_prod_finsupp_symm_inr {α β : Type*} (fg : (α →₀ M) × (β →₀ M)) (y : β) : (sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y := rfl end sum section variables [group G] [mul_action G α] [add_comm_monoid M] /-- Scalar multiplication by a group element g, given by precomposition with the action of g⁻¹ on the domain. -/ def comap_has_scalar : has_scalar G (α →₀ M) := { smul := λ g f, f.comap_domain (λ a, g⁻¹ • a) (λ a a' m m' h, by simpa [←mul_smul] using (congr_arg (λ a, g • a) h)) } local attribute [instance] comap_has_scalar /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is multiplicative in g. -/ def comap_mul_action : mul_action G (α →₀ M) := { one_smul := λ f, by { ext, dsimp [(•)], simp, }, mul_smul := λ g g' f, by { ext, dsimp [(•)], simp [mul_smul], }, } local attribute [instance] comap_mul_action /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is additive in the second argument. -/ def comap_distrib_mul_action : distrib_mul_action G (α →₀ M) := { smul_zero := λ g, by { ext, dsimp [(•)], simp, }, smul_add := λ g f f', by { ext, dsimp [(•)], simp, }, } /-- Scalar multiplication by a group element on finitely supported functions on a group, given by precomposition with the action of g⁻¹. -/ def comap_distrib_mul_action_self : distrib_mul_action G (G →₀ M) := @finsupp.comap_distrib_mul_action G M G _ (monoid.to_mul_action G) _ @[simp] lemma comap_smul_single (g : G) (a : α) (b : M) : g • single a b = single (g • a) b := begin ext a', dsimp [(•)], by_cases h : g • a = a', { subst h, simp [←mul_smul], }, { simp [single_eq_of_ne h], rw [single_eq_of_ne], rintro rfl, simpa [←mul_smul] using h, } end @[simp] lemma comap_smul_apply (g : G) (f : α →₀ M) (a : α) : (g • f) a = f (g⁻¹ • a) := rfl end section instance [monoid R] [add_monoid M] [distrib_mul_action R M] : has_scalar R (α →₀ M) := ⟨λa v, v.map_range ((•) a) (smul_zero _)⟩ /-! Throughout this section, some `monoid` and `semiring` arguments are specified with `{}` instead of `[]`. See note [implicit instance arguments]. -/ @[simp] lemma coe_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] (b : R) (v : α →₀ M) : ⇑(b • v) = b • v := rfl lemma smul_apply {_ : monoid R} [add_monoid M] [distrib_mul_action R M] (b : R) (v : α →₀ M) (a : α) : (b • v) a = b • (v a) := rfl lemma _root_.is_smul_regular.finsupp {_ : monoid R} [add_monoid M] [distrib_mul_action R M] {k : R} (hk : is_smul_regular M k) : is_smul_regular (α →₀ M) k := λ _ _ h, ext $ λ i, hk (congr_fun h i) instance [monoid R] [nonempty α] [add_monoid M] [distrib_mul_action R M] [has_faithful_scalar R M] : has_faithful_scalar R (α →₀ M) := { eq_of_smul_eq_smul := λ r₁ r₂ h, let ⟨a⟩ := ‹nonempty α› in eq_of_smul_eq_smul $ λ m : M, by simpa using congr_fun (h (single a m)) a } variables (α M) instance [monoid R] [add_monoid M] [distrib_mul_action R M] : distrib_mul_action R (α →₀ M) := { smul := (•), smul_add := λ a x y, ext $ λ _, smul_add _ _ _, one_smul := λ x, ext $ λ _, one_smul _ _, mul_smul := λ r s x, ext $ λ _, mul_smul _ _ _, smul_zero := λ x, ext $ λ _, smul_zero _ } instance [monoid R] [monoid S] [add_monoid M] [distrib_mul_action R M] [distrib_mul_action S M] [has_scalar R S] [is_scalar_tower R S M] : is_scalar_tower R S (α →₀ M) := { smul_assoc := λ r s a, ext $ λ _, smul_assoc _ _ _ } instance [monoid R] [monoid S] [add_monoid M] [distrib_mul_action R M] [distrib_mul_action S M] [smul_comm_class R S M] : smul_comm_class R S (α →₀ M) := { smul_comm := λ r s a, ext $ λ _, smul_comm _ _ _ } instance [semiring R] [add_comm_monoid M] [module R M] : module R (α →₀ M) := { smul := (•), zero_smul := λ x, ext $ λ _, zero_smul _ _, add_smul := λ a x y, ext $ λ _, add_smul _ _ _, .. finsupp.distrib_mul_action α M } variables {α M} {R} lemma support_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] {b : R} {g : α →₀ M} : (b • g).support ⊆ g.support := λ a, by { simp only [smul_apply, mem_support_iff, ne.def], exact mt (λ h, h.symm ▸ smul_zero _) } section variables {p : α → Prop} @[simp] lemma filter_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] {b : R} {v : α →₀ M} : (b • v).filter p = b • v.filter p := coe_fn_injective $ set.indicator_smul {x | p x} b v end lemma map_domain_smul {_ : monoid R} [add_comm_monoid M] [distrib_mul_action R M] {f : α → β} (b : R) (v : α →₀ M) : map_domain f (b • v) = b • map_domain f v := begin change map_domain f (map_range _ _ _) = map_range _ _ _, apply finsupp.induction v, { simp only [map_domain_zero, map_range_zero] }, intros a b v' hv₁ hv₂ IH, rw [map_range_add, map_domain_add, IH, map_domain_add, map_range_add, map_range_single, map_domain_single, map_domain_single, map_range_single]; apply smul_add end @[simp] lemma smul_single {_ : monoid R} [add_monoid M] [distrib_mul_action R M] (c : R) (a : α) (b : M) : c • finsupp.single a b = finsupp.single a (c • b) := map_range_single @[simp] lemma smul_single' {_ : semiring R} (c : R) (a : α) (b : R) : c • finsupp.single a b = finsupp.single a (c * b) := smul_single _ _ _ lemma map_range_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] [add_monoid N] [distrib_mul_action R N] {f : M → N} {hf : f 0 = 0} (c : R) (v : α →₀ M) (hsmul : ∀ x, f (c • x) = c • f x) : map_range f hf (c • v) = c • map_range f hf v := begin erw ←map_range_comp, have : (f ∘ (•) c) = ((•) c ∘ f) := funext hsmul, simp_rw this, apply map_range_comp, rw [function.comp_apply, smul_zero, hf], end lemma smul_single_one [semiring R] (a : α) (b : R) : b • single a 1 = single a b := by rw [smul_single, smul_eq_mul, mul_one] end lemma sum_smul_index [semiring R] [add_comm_monoid M] {g : α →₀ R} {b : R} {h : α → R → M} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) := finsupp.sum_map_range_index h0 lemma sum_smul_index' [monoid R] [add_monoid M] [distrib_mul_action R M] [add_comm_monoid N] {g : α →₀ M} {b : R} {h : α → M → N} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi c, h i (b • c)) := finsupp.sum_map_range_index h0 /-- A version of `finsupp.sum_smul_index'` for bundled additive maps. -/ lemma sum_smul_index_add_monoid_hom [monoid R] [add_monoid M] [add_comm_monoid N] [distrib_mul_action R M] {g : α →₀ M} {b : R} {h : α → M →+ N} : (b • g).sum (λ a, h a) = g.sum (λ i c, h i (b • c)) := sum_map_range_index (λ i, (h i).map_zero) instance [semiring R] [add_comm_monoid M] [module R M] {ι : Type*} [no_zero_smul_divisors R M] : no_zero_smul_divisors R (ι →₀ M) := ⟨λ c f h, or_iff_not_imp_left.mpr (λ hc, finsupp.ext (λ i, (smul_eq_zero.mp (finsupp.ext_iff.mp h i)).resolve_left hc))⟩ section distrib_mul_action_hom variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid N] [distrib_mul_action R M] [distrib_mul_action R N] /-- `finsupp.single` as a `distrib_mul_action_hom`. See also `finsupp.lsingle` for the version as a linear map. -/ def distrib_mul_action_hom.single (a : α) : M →+[R] (α →₀ M) := { map_smul' := λ k m, by simp only [add_monoid_hom.to_fun_eq_coe, single_add_hom_apply, smul_single], .. single_add_hom a } lemma distrib_mul_action_hom_ext {f g : (α →₀ M) →+[R] N} (h : ∀ (a : α) (m : M), f (single a m) = g (single a m)) : f = g := distrib_mul_action_hom.to_add_monoid_hom_injective $ add_hom_ext h /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma distrib_mul_action_hom_ext' {f g : (α →₀ M) →+[R] N} (h : ∀ (a : α), f.comp (distrib_mul_action_hom.single a) = g.comp (distrib_mul_action_hom.single a)) : f = g := distrib_mul_action_hom_ext $ λ a, distrib_mul_action_hom.congr_fun (h a) end distrib_mul_action_hom section variables [has_zero R] /-- The `finsupp` version of `pi.unique`. -/ instance unique_of_right [subsingleton R] : unique (α →₀ R) := { uniq := λ l, ext $ λ i, subsingleton.elim _ _, .. finsupp.inhabited } /-- The `finsupp` version of `pi.unique_of_is_empty`. -/ instance unique_of_left [is_empty α] : unique (α →₀ R) := { uniq := λ l, ext is_empty_elim, .. finsupp.inhabited } end /-- Given an `add_comm_monoid M` and `s : set α`, `restrict_support_equiv s M` is the `equiv` between the subtype of finitely supported functions with support contained in `s` and the type of finitely supported functions from `s`. -/ def restrict_support_equiv (s : set α) (M : Type*) [add_comm_monoid M] : {f : α →₀ M // ↑f.support ⊆ s } ≃ (s →₀ M) := begin refine ⟨λf, subtype_domain (λx, x ∈ s) f.1, λ f, ⟨f.map_domain subtype.val, _⟩, _, _⟩, { refine set.subset.trans (finset.coe_subset.2 map_domain_support) _, rw [finset.coe_image, set.image_subset_iff], exact assume x hx, x.2 }, { rintros ⟨f, hf⟩, apply subtype.eq, ext a, dsimp only, refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s → α), _) (assume h, _), { rcases h with ⟨x, rfl⟩, rw [map_domain_apply subtype.val_injective, subtype_domain_apply] }, { convert map_domain_notin_range _ _ h, rw [← not_mem_support_iff], refine mt _ h, exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } }, { assume f, ext ⟨a, ha⟩, dsimp only, rw [subtype_domain_apply, map_domain_apply subtype.val_injective] } end /-- Given `add_comm_monoid M` and `e : α ≃ β`, `dom_congr e` is the corresponding `equiv` between `α →₀ M` and `β →₀ M`. This is `finsupp.equiv_congr_left` as an `add_equiv`. -/ @[simps apply] protected def dom_congr [add_comm_monoid M] (e : α ≃ β) : (α →₀ M) ≃+ (β →₀ M) := { to_fun := equiv_map_domain e, inv_fun := equiv_map_domain e.symm, left_inv := λ v, begin simp only [← equiv_map_domain_trans, equiv.trans_symm], exact equiv_map_domain_refl _ end, right_inv := begin assume v, simp only [← equiv_map_domain_trans, equiv.symm_trans], exact equiv_map_domain_refl _ end, map_add' := λ a b, by simp only [equiv_map_domain_eq_map_domain]; exact map_domain_add } @[simp] lemma dom_congr_refl [add_comm_monoid M] : finsupp.dom_congr (equiv.refl α) = add_equiv.refl (α →₀ M) := add_equiv.ext $ λ _, equiv_map_domain_refl _ @[simp] lemma dom_congr_symm [add_comm_monoid M] (e : α ≃ β) : (finsupp.dom_congr e).symm = (finsupp.dom_congr e.symm : (β →₀ M) ≃+ (α →₀ M)):= add_equiv.ext $ λ _, rfl @[simp] lemma dom_congr_trans [add_comm_monoid M] (e : α ≃ β) (f : β ≃ γ) : (finsupp.dom_congr e).trans (finsupp.dom_congr f) = (finsupp.dom_congr (e.trans f) : (α →₀ M) ≃+ _) := add_equiv.ext $ λ _, (equiv_map_domain_trans _ _ _).symm end finsupp namespace finsupp /-! ### Declarations about sigma types -/ section sigma variables {αs : ι → Type*} [has_zero M] (l : (Σ i, αs i) →₀ M) /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `M` and an index element `i : ι`, `split l i` is the `i`th component of `l`, a finitely supported function from `as i` to `M`. This is the `finsupp` version of `sigma.curry`. -/ def split (i : ι) : αs i →₀ M := l.comap_domain (sigma.mk i) (λ x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2) lemma split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ := begin dunfold split, rw comap_domain_apply end /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β`, `split_support l` is the finset of indices in `ι` that appear in the support of `l`. -/ def split_support : finset ι := l.support.image sigma.fst lemma mem_split_support_iff_nonzero (i : ι) : i ∈ split_support l ↔ split l i ≠ 0 := begin rw [split_support, mem_image, ne.def, ← support_eq_empty, ← ne.def, ← finset.nonempty_iff_ne_empty, split, comap_domain, finset.nonempty], simp only [exists_prop, finset.mem_preimage, exists_and_distrib_right, exists_eq_right, mem_support_iff, sigma.exists, ne.def] end /-- Given `l`, a finitely supported function from the sigma type `Σ i, αs i` to `β` and an `ι`-indexed family `g` of functions from `(αs i →₀ β)` to `γ`, `split_comp` defines a finitely supported function from the index type `ι` to `γ` given by composing `g i` with `split l i`. -/ def split_comp [has_zero N] (g : Π i, (αs i →₀ M) → N) (hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ N := { support := split_support l, to_fun := λ i, g i (split l i), mem_support_to_fun := begin intros i, rw [mem_split_support_iff_nonzero, not_iff_not, hg], end } lemma sigma_support : l.support = l.split_support.sigma (λ i, (l.split i).support) := by simp only [finset.ext_iff, split_support, split, comap_domain, mem_image, mem_preimage, sigma.forall, mem_sigma]; tauto lemma sigma_sum [add_comm_monoid N] (f : (Σ (i : ι), αs i) → M → N) : l.sum f = ∑ i in split_support l, (split l i).sum (λ (a : αs i) b, f ⟨i, a⟩ b) := by simp only [sum, sigma_support, sum_sigma, split_apply] variables {η : Type*} [fintype η] {ιs : η → Type*} [has_zero α] /-- On a `fintype η`, `finsupp.split` is an equivalence between `(Σ (j : η), ιs j) →₀ α` and `Π j, (ιs j →₀ α)`. This is the `finsupp` version of `equiv.Pi_curry`. -/ noncomputable def sigma_finsupp_equiv_pi_finsupp : ((Σ j, ιs j) →₀ α) ≃ Π j, (ιs j →₀ α) := { to_fun := split, inv_fun := λ f, on_finset (finset.univ.sigma (λ j, (f j).support)) (λ ji, f ji.1 ji.2) (λ g hg, finset.mem_sigma.mpr ⟨finset.mem_univ _, mem_support_iff.mpr hg⟩), left_inv := λ f, by { ext, simp [split] }, right_inv := λ f, by { ext, simp [split] } } @[simp] lemma sigma_finsupp_equiv_pi_finsupp_apply (f : (Σ j, ιs j) →₀ α) (j i) : sigma_finsupp_equiv_pi_finsupp f j i = f ⟨j, i⟩ := rfl /-- On a `fintype η`, `finsupp.split` is an additive equivalence between `(Σ (j : η), ιs j) →₀ α` and `Π j, (ιs j →₀ α)`. This is the `add_equiv` version of `finsupp.sigma_finsupp_equiv_pi_finsupp`. -/ noncomputable def sigma_finsupp_add_equiv_pi_finsupp {α : Type*} {ιs : η → Type*} [add_monoid α] : ((Σ j, ιs j) →₀ α) ≃+ Π j, (ιs j →₀ α) := { map_add' := λ f g, by { ext, simp }, .. sigma_finsupp_equiv_pi_finsupp } @[simp] lemma sigma_finsupp_add_equiv_pi_finsupp_apply {α : Type*} {ιs : η → Type*} [add_monoid α] (f : (Σ j, ιs j) →₀ α) (j i) : sigma_finsupp_add_equiv_pi_finsupp f j i = f ⟨j, i⟩ := rfl end sigma end finsupp /-! ### Declarations relating `multiset` to `finsupp` -/ namespace multiset /-- Given a multiset `s`, `s.to_finsupp` returns the finitely supported function on `ℕ` given by the multiplicities of the elements of `s`. -/ def to_finsupp : multiset α ≃+ (α →₀ ℕ) := finsupp.to_multiset.symm @[simp] lemma to_finsupp_support [D : decidable_eq α] (s : multiset α) : s.to_finsupp.support = s.to_finset := by rw subsingleton.elim D; refl @[simp] lemma to_finsupp_apply [D : decidable_eq α] (s : multiset α) (a : α) : to_finsupp s a = s.count a := by rw subsingleton.elim D; refl lemma to_finsupp_zero : to_finsupp (0 : multiset α) = 0 := add_equiv.map_zero _ lemma to_finsupp_add (s t : multiset α) : to_finsupp (s + t) = to_finsupp s + to_finsupp t := to_finsupp.map_add s t @[simp] lemma to_finsupp_singleton (a : α) : to_finsupp ({a} : multiset α) = finsupp.single a 1 := finsupp.to_multiset.symm_apply_eq.2 $ by simp @[simp] lemma to_finsupp_to_multiset (s : multiset α) : s.to_finsupp.to_multiset = s := finsupp.to_multiset.apply_symm_apply s lemma to_finsupp_eq_iff {s : multiset α} {f : α →₀ ℕ} : s.to_finsupp = f ↔ s = f.to_multiset := finsupp.to_multiset.symm_apply_eq end multiset @[simp] lemma finsupp.to_multiset_to_finsupp (f : α →₀ ℕ) : f.to_multiset.to_finsupp = f := finsupp.to_multiset.symm_apply_apply f /-! ### Declarations about order(ed) instances on `finsupp` -/ namespace finsupp instance [preorder M] [has_zero M] : preorder (α →₀ M) := { le := λ f g, ∀ s, f s ≤ g s, le_refl := λ f s, le_refl _, le_trans := λ f g h Hfg Hgh s, le_trans (Hfg s) (Hgh s) } instance [partial_order M] [has_zero M] : partial_order (α →₀ M) := { le_antisymm := λ f g hfg hgf, ext $ λ s, le_antisymm (hfg s) (hgf s), .. finsupp.preorder } instance [ordered_cancel_add_comm_monoid M] : ordered_cancel_add_comm_monoid (α →₀ M) := { add_le_add_left := λ a b h c s, add_le_add_left (h s) (c s), le_of_add_le_add_left := λ a b c h s, le_of_add_le_add_left (h s), add_left_cancel := λ a b c h, ext $ λ s, add_left_cancel (ext_iff.1 h s), .. finsupp.add_comm_monoid, .. finsupp.partial_order } lemma le_def [preorder M] [has_zero M] {f g : α →₀ M} : f ≤ g ↔ ∀ x, f x ≤ g x := iff.rfl lemma le_iff' [canonically_ordered_add_monoid M] (f g : α →₀ M) {t : finset α} (hf : f.support ⊆ t) : f ≤ g ↔ ∀ s ∈ t, f s ≤ g s := ⟨λ h s hs, h s, λ h s, if H : s ∈ f.support then h s (hf H) else (not_mem_support_iff.1 H).symm ▸ zero_le (g s)⟩ lemma le_iff [canonically_ordered_add_monoid M] (f g : α →₀ M) : f ≤ g ↔ ∀ s ∈ f.support, f s ≤ g s := le_iff' f g (subset.refl _) instance decidable_le [canonically_ordered_add_monoid M] [decidable_rel (@has_le.le M _)] : decidable_rel (@has_le.le (α →₀ M) _) := λ f g, decidable_of_iff _ (le_iff f g).symm @[simp] lemma single_le_iff [canonically_ordered_add_monoid M] {i : α} {x : M} {f : α →₀ M} : single i x ≤ f ↔ x ≤ f i := (le_iff' _ _ support_single_subset).trans $ by simp @[simp] lemma add_eq_zero_iff [canonically_ordered_add_monoid M] (f g : α →₀ M) : f + g = 0 ↔ f = 0 ∧ g = 0 := by simp [ext_iff, forall_and_distrib] /-- `finsupp.to_multiset` as an order isomorphism. -/ def order_iso_multiset : (α →₀ ℕ) ≃o multiset α := { to_equiv := to_multiset.to_equiv, map_rel_iff' := λ f g, by simp [multiset.le_iff_count, le_def] } @[simp] lemma coe_order_iso_multiset : ⇑(@order_iso_multiset α) = to_multiset := rfl @[simp] lemma coe_order_iso_multiset_symm : ⇑(@order_iso_multiset α).symm = multiset.to_finsupp := rfl lemma to_multiset_strict_mono : strict_mono (@to_multiset α) := order_iso_multiset.strict_mono lemma sum_id_lt_of_lt (m n : α →₀ ℕ) (h : m < n) : m.sum (λ _, id) < n.sum (λ _, id) := begin rw [← card_to_multiset, ← card_to_multiset], apply multiset.card_lt_of_lt, exact to_multiset_strict_mono h end variable (α) /-- The order on `σ →₀ ℕ` is well-founded.-/ lemma lt_wf : well_founded (@has_lt.lt (α →₀ ℕ) _) := subrelation.wf (sum_id_lt_of_lt) $ inv_image.wf _ nat.lt_wf variable {α} @[simp] lemma nat_add_sub_cancel (f g : α →₀ ℕ) : f + g - g = f := ext $ λ a, nat.add_sub_cancel _ _ @[simp] lemma nat_add_sub_cancel_left (f g : α →₀ ℕ) : f + g - f = g := ext $ λ a, nat.add_sub_cancel_left _ _ lemma nat_add_sub_of_le {f g : α →₀ ℕ} (h : f ≤ g) : f + (g - f) = g := ext $ λ a, nat.add_sub_of_le (h a) lemma nat_sub_add_cancel {f g : α →₀ ℕ} (h : f ≤ g) : g - f + f = g := ext $ λ a, nat.sub_add_cancel (h a) lemma nat_add_sub_assoc {f₁ f₂ : α →₀ ℕ} (h : f₁ ≤ f₂) (f₃ : α →₀ ℕ) : f₃ + f₂ - f₁ = f₃ + (f₂ - f₁) := ext $ λ a, nat.add_sub_assoc (h _) _ instance : canonically_ordered_add_monoid (α →₀ ℕ) := { bot := 0, bot_le := λ f s, zero_le (f s), le_iff_exists_add := λ f g, ⟨λ H, ⟨g - f, (nat_add_sub_of_le H).symm⟩, λ ⟨c, hc⟩, hc.symm ▸ λ x, by simp⟩, .. (infer_instance : ordered_add_comm_monoid (α →₀ ℕ)) } end finsupp namespace multiset lemma to_finsuppstrict_mono : strict_mono (@to_finsupp α) := finsupp.order_iso_multiset.symm.strict_mono end multiset
d94a1ec39fb120429721238f3df5b84ce536671f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/shapes/reflexive_auto.lean
beca4841e387814b85152c5f9916bada8c537080
[]
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
8,533
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.adjunction.default import Mathlib.category_theory.limits.shapes.equalizers import Mathlib.category_theory.limits.shapes.kernel_pair import Mathlib.PostPort universes v u l v₂ u₂ namespace Mathlib /-! # Reflexive coequalizers We define reflexive pairs as a pair of morphisms which have a common section. We say a category has reflexive coequalizers if it has coequalizers of all reflexive pairs. Reflexive coequalizers often enjoy nicer properties than general coequalizers, and feature heavily in some versions of the monadicity theorem. We also give some examples of reflexive pairs: for an adjunction `F ⊣ G` with counit `ε`, the pair `(FGε_B, ε_FGB)` is reflexive. If a pair `f,g` is a kernel pair for some morphism, then it is reflexive. # TODO * If `C` has binary coproducts and reflexive coequalizers, then it has all coequalizers. * If `T` is a monad on cocomplete category `C`, then `algebra T` is cocomplete iff it has reflexive coequalizers. * If `C` is locally cartesian closed and has reflexive coequalizers, then it has images: in fact regular epi (and hence strong epi) images. -/ namespace category_theory /-- The pair `f g : A ⟶ B` is reflexive if there is a morphism `B ⟶ A` which is a section for both. -/ class is_reflexive_pair {C : Type u} [category C] {A : C} {B : C} (f : A ⟶ B) (g : A ⟶ B) where common_section : ∃ (s : B ⟶ A), s ≫ f = 𝟙 ∧ s ≫ g = 𝟙 /-- The pair `f g : A ⟶ B` is coreflexive if there is a morphism `B ⟶ A` which is a retraction for both. -/ class is_coreflexive_pair {C : Type u} [category C] {A : C} {B : C} (f : A ⟶ B) (g : A ⟶ B) where common_retraction : ∃ (s : B ⟶ A), f ≫ s = 𝟙 ∧ g ≫ s = 𝟙 theorem is_reflexive_pair.mk' {C : Type u} [category C] {A : C} {B : C} {f : A ⟶ B} {g : A ⟶ B} (s : B ⟶ A) (sf : s ≫ f = 𝟙) (sg : s ≫ g = 𝟙) : is_reflexive_pair f g := is_reflexive_pair.mk (Exists.intro s { left := sf, right := sg }) theorem is_coreflexive_pair.mk' {C : Type u} [category C] {A : C} {B : C} {f : A ⟶ B} {g : A ⟶ B} (s : B ⟶ A) (fs : f ≫ s = 𝟙) (gs : g ≫ s = 𝟙) : is_coreflexive_pair f g := is_coreflexive_pair.mk (Exists.intro s { left := fs, right := gs }) /-- Get the common section for a reflexive pair. -/ def common_section {C : Type u} [category C] {A : C} {B : C} (f : A ⟶ B) (g : A ⟶ B) [is_reflexive_pair f g] : B ⟶ A := Exists.some (is_reflexive_pair.common_section f g) @[simp] theorem section_comp_left_assoc {C : Type u} [category C] {A : C} {B : C} (f : A ⟶ B) (g : A ⟶ B) [is_reflexive_pair f g] {X' : C} (f' : B ⟶ X') : common_section f g ≫ f ≫ f' = f' := sorry @[simp] theorem section_comp_right {C : Type u} [category C] {A : C} {B : C} (f : A ⟶ B) (g : A ⟶ B) [is_reflexive_pair f g] : common_section f g ≫ g = 𝟙 := and.right (Exists.some_spec (is_reflexive_pair.common_section f g)) /-- Get the common retraction for a coreflexive pair. -/ def common_retraction {C : Type u} [category C] {A : C} {B : C} (f : A ⟶ B) (g : A ⟶ B) [is_coreflexive_pair f g] : B ⟶ A := Exists.some (is_coreflexive_pair.common_retraction f g) @[simp] theorem left_comp_retraction_assoc {C : Type u} [category C] {A : C} {B : C} (f : A ⟶ B) (g : A ⟶ B) [is_coreflexive_pair f g] {X' : C} (f' : A ⟶ X') : f ≫ common_retraction f g ≫ f' = f' := sorry @[simp] theorem right_comp_retraction_assoc {C : Type u} [category C] {A : C} {B : C} (f : A ⟶ B) (g : A ⟶ B) [is_coreflexive_pair f g] {X' : C} (f' : A ⟶ X') : g ≫ common_retraction f g ≫ f' = f' := sorry /-- If `f,g` is a kernel pair for some morphism `q`, then it is reflexive. -/ theorem is_kernel_pair.is_reflexive_pair {C : Type u} [category C] {A : C} {B : C} {R : C} {f : R ⟶ A} {g : R ⟶ A} {q : A ⟶ B} (h : is_kernel_pair q f g) : is_reflexive_pair f g := is_reflexive_pair.mk' (subtype.val (is_kernel_pair.lift' h 𝟙 𝟙 rfl)) (and.left (subtype.property (is_kernel_pair.lift' h 𝟙 𝟙 rfl))) (and.right (subtype.property (is_kernel_pair.lift' h 𝟙 𝟙 rfl))) /-- If `f,g` is reflexive, then `g,f` is reflexive. -/ -- This shouldn't be an instance as it would instantly loop. theorem is_reflexive_pair.swap {C : Type u} [category C] {A : C} {B : C} {f : A ⟶ B} {g : A ⟶ B} [is_reflexive_pair f g] : is_reflexive_pair g f := is_reflexive_pair.mk' (common_section f g) (section_comp_right f g) (section_comp_left f g) /-- If `f,g` is coreflexive, then `g,f` is coreflexive. -/ -- This shouldn't be an instance as it would instantly loop. theorem is_coreflexive_pair.swap {C : Type u} [category C] {A : C} {B : C} {f : A ⟶ B} {g : A ⟶ B} [is_coreflexive_pair f g] : is_coreflexive_pair g f := is_coreflexive_pair.mk' (common_retraction f g) (right_comp_retraction f g) (left_comp_retraction f g) /-- For an adjunction `F ⊣ G` with counit `ε`, the pair `(FGε_B, ε_FGB)` is reflexive. -/ protected instance app.is_reflexive_pair {C : Type u} [category C] {D : Type u₂} [category D] {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) (B : D) : is_reflexive_pair (functor.map F (functor.map G (nat_trans.app (adjunction.counit adj) B))) (nat_trans.app (adjunction.counit adj) (functor.obj F (functor.obj G B))) := is_reflexive_pair.mk' (functor.map F (nat_trans.app (adjunction.unit adj) (functor.obj G B))) (eq.mpr (id (Eq._oldrec (Eq.refl (functor.map F (nat_trans.app (adjunction.unit adj) (functor.obj G B)) ≫ functor.map F (functor.map G (nat_trans.app (adjunction.counit adj) B)) = 𝟙)) (Eq.symm (functor.map_comp F (nat_trans.app (adjunction.unit adj) (functor.obj G B)) (functor.map G (nat_trans.app (adjunction.counit adj) B)))))) (eq.mpr (id (Eq._oldrec (Eq.refl (functor.map F (nat_trans.app (adjunction.unit adj) (functor.obj G B) ≫ functor.map G (nat_trans.app (adjunction.counit adj) B)) = 𝟙)) (adjunction.right_triangle_components adj))) (functor.map_id F (functor.obj G (functor.obj 𝟭 B))))) (adjunction.left_triangle_components adj) namespace limits /-- `C` has reflexive coequalizers if it has coequalizers for every reflexive pair. -/ class has_reflexive_coequalizers (C : Type u) [category C] where has_coeq : ∀ {A B : C} (f g : A ⟶ B) [_inst_3 : is_reflexive_pair f g], has_coequalizer f g /-- `C` has coreflexive equalizers if it has equalizers for every coreflexive pair. -/ class has_coreflexive_equalizers (C : Type u) [category C] where has_eq : ∀ {A B : C} (f g : A ⟶ B) [_inst_3 : is_coreflexive_pair f g], has_equalizer f g theorem has_coequalizer_of_common_section (C : Type u) [category C] [has_reflexive_coequalizers C] {A : C} {B : C} {f : A ⟶ B} {g : A ⟶ B} (r : B ⟶ A) (rf : r ≫ f = 𝟙) (rg : r ≫ g = 𝟙) : has_coequalizer f g := let _inst : is_reflexive_pair f g := is_reflexive_pair.mk' r rf rg; has_reflexive_coequalizers.has_coeq f g theorem has_equalizer_of_common_retraction (C : Type u) [category C] [has_coreflexive_equalizers C] {A : C} {B : C} {f : A ⟶ B} {g : A ⟶ B} (r : B ⟶ A) (fr : f ≫ r = 𝟙) (gr : g ≫ r = 𝟙) : has_equalizer f g := let _inst : is_coreflexive_pair f g := is_coreflexive_pair.mk' r fr gr; has_coreflexive_equalizers.has_eq f g /-- If `C` has coequalizers, then it has reflexive coequalizers. -/ protected instance has_reflexive_coequalizers_of_has_coequalizers (C : Type u) [category C] [has_coequalizers C] : has_reflexive_coequalizers C := has_reflexive_coequalizers.mk fun (A B : C) (f g : A ⟶ B) (i : is_reflexive_pair f g) => limits.has_colimit_of_has_colimits_of_shape (parallel_pair f g) /-- If `C` has equalizers, then it has coreflexive equalizers. -/ protected instance has_coreflexive_equalizers_of_has_equalizers (C : Type u) [category C] [has_equalizers C] : has_coreflexive_equalizers C := has_coreflexive_equalizers.mk fun (A B : C) (f g : A ⟶ B) (i : is_coreflexive_pair f g) => limits.has_limit_of_has_limits_of_shape (parallel_pair f g) end Mathlib
3eb8bcc53b1289e3d706cd9b1c9e71f2a72baf9d
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/analysis/normed_space/basic.lean
3dadf36205989b4eec162e83145ad0b3a0b262b5
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
40,458
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import algebra.pi_instances import linear_algebra.basic import topology.instances.nnreal topology.instances.complex import topology.algebra.module import topology.metric_space.lipschitz import topology.metric_space.antilipschitz /-! # Normed spaces -/ variables {α : Type*} {β : Type*} {γ : Type*} {ι : Type*} noncomputable theory open filter metric open_locale topological_space localized "notation f `→_{`:50 a `}`:0 b := filter.tendsto f (_root_.nhds a) (_root_.nhds b)" in filter /-- Auxiliary class, endowing a type `α` with a function `norm : α → ℝ`. This class is designed to be extended in more interesting classes specifying the properties of the norm. -/ class has_norm (α : Type*) := (norm : α → ℝ) export has_norm (norm) notation `∥`:1024 e:1 `∥`:1 := norm e section prio set_option default_priority 100 -- see Note [default priority] /-- A normed group is an additive group endowed with a norm for which `dist x y = ∥x - y∥` defines a metric space structure. -/ class normed_group (α : Type*) extends has_norm α, add_comm_group α, metric_space α := (dist_eq : ∀ x y, dist x y = norm (x - y)) end prio /-- Construct a normed group from a translation invariant distance -/ def normed_group.of_add_dist [has_norm α] [add_comm_group α] [metric_space α] (H1 : ∀ x:α, ∥x∥ = dist x 0) (H2 : ∀ x y z : α, dist x y ≤ dist (x + z) (y + z)) : normed_group α := { dist_eq := λ x y, begin rw H1, apply le_antisymm, { rw [sub_eq_add_neg, ← add_right_neg y], apply H2 }, { have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this } end } /-- Construct a normed group from a translation invariant distance -/ def normed_group.of_add_dist' [has_norm α] [add_comm_group α] [metric_space α] (H1 : ∀ x:α, ∥x∥ = dist x 0) (H2 : ∀ x y z : α, dist (x + z) (y + z) ≤ dist x y) : normed_group α := { dist_eq := λ x y, begin rw H1, apply le_antisymm, { have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this }, { rw [sub_eq_add_neg, ← add_right_neg y], apply H2 } end } /-- A normed group can be built from a norm that satisfies algebraic properties. This is formalised in this structure. -/ structure normed_group.core (α : Type*) [add_comm_group α] [has_norm α] : Prop := (norm_eq_zero_iff : ∀ x : α, ∥x∥ = 0 ↔ x = 0) (triangle : ∀ x y : α, ∥x + y∥ ≤ ∥x∥ + ∥y∥) (norm_neg : ∀ x : α, ∥-x∥ = ∥x∥) /-- Constructing a normed group from core properties of a norm, i.e., registering the distance and the metric space structure from the norm properties. -/ noncomputable def normed_group.of_core (α : Type*) [add_comm_group α] [has_norm α] (C : normed_group.core α) : normed_group α := { dist := λ x y, ∥x - y∥, dist_eq := assume x y, by refl, dist_self := assume x, (C.norm_eq_zero_iff (x - x)).mpr (show x - x = 0, by simp), eq_of_dist_eq_zero := assume x y h, show (x = y), from sub_eq_zero.mp $ (C.norm_eq_zero_iff (x - y)).mp h, dist_triangle := assume x y z, calc ∥x - z∥ = ∥x - y + (y - z)∥ : by simp [sub_eq_add_neg] ... ≤ ∥x - y∥ + ∥y - z∥ : C.triangle _ _, dist_comm := assume x y, calc ∥x - y∥ = ∥ -(y - x)∥ : by simp ... = ∥y - x∥ : by { rw [C.norm_neg] } } section normed_group variables [normed_group α] [normed_group β] lemma dist_eq_norm (g h : α) : dist g h = ∥g - h∥ := normed_group.dist_eq _ _ @[simp] lemma dist_zero_right (g : α) : dist g 0 = ∥g∥ := by rw [dist_eq_norm, sub_zero] lemma norm_sub_rev (g h : α) : ∥g - h∥ = ∥h - g∥ := by simpa only [dist_eq_norm] using dist_comm g h @[simp] lemma norm_neg (g : α) : ∥-g∥ = ∥g∥ := by simpa using norm_sub_rev 0 g @[simp] lemma dist_add_left (g h₁ h₂ : α) : dist (g + h₁) (g + h₂) = dist h₁ h₂ := by simp [dist_eq_norm] @[simp] lemma dist_add_right (g₁ g₂ h : α) : dist (g₁ + h) (g₂ + h) = dist g₁ g₂ := by simp [dist_eq_norm] @[simp] lemma dist_neg_neg (g h : α) : dist (-g) (-h) = dist g h := by simp only [dist_eq_norm, neg_sub_neg, norm_sub_rev] @[simp] lemma dist_sub_left (g h₁ h₂ : α) : dist (g - h₁) (g - h₂) = dist h₁ h₂ := by simp only [sub_eq_add_neg, dist_add_left, dist_neg_neg] @[simp] lemma dist_sub_right (g₁ g₂ h : α) : dist (g₁ - h) (g₂ - h) = dist g₁ g₂ := dist_add_right _ _ _ /-- Triangle inequality for the norm. -/ lemma norm_add_le (g h : α) : ∥g + h∥ ≤ ∥g∥ + ∥h∥ := by simpa [dist_eq_norm] using dist_triangle g 0 (-h) lemma norm_add_le_of_le {g₁ g₂ : α} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) : ∥g₁ + g₂∥ ≤ n₁ + n₂ := le_trans (norm_add_le g₁ g₂) (add_le_add H₁ H₂) lemma dist_add_add_le (g₁ g₂ h₁ h₂ : α) : dist (g₁ + g₂) (h₁ + h₂) ≤ dist g₁ h₁ + dist g₂ h₂ := by simpa only [dist_add_left, dist_add_right] using dist_triangle (g₁ + g₂) (h₁ + g₂) (h₁ + h₂) lemma dist_add_add_le_of_le {g₁ g₂ h₁ h₂ : α} {d₁ d₂ : ℝ} (H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) : dist (g₁ + g₂) (h₁ + h₂) ≤ d₁ + d₂ := le_trans (dist_add_add_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂) lemma dist_sub_sub_le (g₁ g₂ h₁ h₂ : α) : dist (g₁ - g₂) (h₁ - h₂) ≤ dist g₁ h₁ + dist g₂ h₂ := dist_neg_neg g₂ h₂ ▸ dist_add_add_le _ _ _ _ lemma dist_sub_sub_le_of_le {g₁ g₂ h₁ h₂ : α} {d₁ d₂ : ℝ} (H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) : dist (g₁ - g₂) (h₁ - h₂) ≤ d₁ + d₂ := le_trans (dist_sub_sub_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂) lemma abs_dist_sub_le_dist_add_add (g₁ g₂ h₁ h₂ : α) : abs (dist g₁ h₁ - dist g₂ h₂) ≤ dist (g₁ + g₂) (h₁ + h₂) := by simpa only [dist_add_left, dist_add_right, dist_comm h₂] using abs_dist_sub_le (g₁ + g₂) (h₁ + h₂) (h₁ + g₂) @[simp] lemma norm_nonneg (g : α) : 0 ≤ ∥g∥ := by { rw[←dist_zero_right], exact dist_nonneg } lemma norm_eq_zero {g : α} : ∥g∥ = 0 ↔ g = 0 := dist_zero_right g ▸ dist_eq_zero @[simp] lemma norm_zero : ∥(0:α)∥ = 0 := norm_eq_zero.2 rfl lemma norm_sum_le {β} : ∀(s : finset β) (f : β → α), ∥s.sum f∥ ≤ s.sum (λa, ∥ f a ∥) := finset.le_sum_of_subadditive norm norm_zero norm_add_le lemma norm_sum_le_of_le {β} (s : finset β) {f : β → α} {n : β → ℝ} (h : ∀ b ∈ s, ∥f b∥ ≤ n b) : ∥s.sum f∥ ≤ s.sum n := by { haveI := classical.dec_eq β, exact le_trans (norm_sum_le s f) (finset.sum_le_sum h) } lemma norm_pos_iff {g : α} : 0 < ∥ g ∥ ↔ g ≠ 0 := dist_zero_right g ▸ dist_pos lemma norm_le_zero_iff {g : α} : ∥g∥ ≤ 0 ↔ g = 0 := by { rw[←dist_zero_right], exact dist_le_zero } lemma norm_sub_le (g h : α) : ∥g - h∥ ≤ ∥g∥ + ∥h∥ := by simpa [dist_eq_norm] using dist_triangle g 0 h lemma norm_sub_le_of_le {g₁ g₂ : α} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) : ∥g₁ - g₂∥ ≤ n₁ + n₂ := le_trans (norm_sub_le g₁ g₂) (add_le_add H₁ H₂) lemma dist_le_norm_add_norm (g h : α) : dist g h ≤ ∥g∥ + ∥h∥ := by { rw dist_eq_norm, apply norm_sub_le } lemma abs_norm_sub_norm_le (g h : α) : abs(∥g∥ - ∥h∥) ≤ ∥g - h∥ := by simpa [dist_eq_norm] using abs_dist_sub_le g h 0 lemma norm_sub_norm_le (g h : α) : ∥g∥ - ∥h∥ ≤ ∥g - h∥ := le_trans (le_abs_self _) (abs_norm_sub_norm_le g h) lemma dist_norm_norm_le (g h : α) : dist ∥g∥ ∥h∥ ≤ ∥g - h∥ := abs_norm_sub_norm_le g h lemma ball_0_eq (ε : ℝ) : ball (0:α) ε = {x | ∥x∥ < ε} := set.ext $ assume a, by simp lemma norm_le_of_mem_closed_ball {g h : α} {r : ℝ} (H : h ∈ closed_ball g r) : ∥h∥ ≤ ∥g∥ + r := calc ∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right] ... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _ ... ≤ ∥g∥ + r : by { apply add_le_add_left, rw ← dist_eq_norm, exact H } lemma norm_lt_of_mem_ball {g h : α} {r : ℝ} (H : h ∈ ball g r) : ∥h∥ < ∥g∥ + r := calc ∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right] ... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _ ... < ∥g∥ + r : by { apply add_lt_add_left, rw ← dist_eq_norm, exact H } theorem normed_group.tendsto_nhds_zero {f : γ → α} {l : filter γ} : tendsto f l (𝓝 0) ↔ ∀ ε > 0, ∀ᶠ x in l, ∥ f x ∥ < ε := metric.tendsto_nhds.trans $ by simp only [dist_zero_right] section nnnorm /-- Version of the norm taking values in nonnegative reals. -/ def nnnorm (a : α) : nnreal := ⟨norm a, norm_nonneg a⟩ @[simp] lemma coe_nnnorm (a : α) : (nnnorm a : ℝ) = norm a := rfl lemma nndist_eq_nnnorm (a b : α) : nndist a b = nnnorm (a - b) := nnreal.eq $ dist_eq_norm _ _ lemma nnnorm_eq_zero {a : α} : nnnorm a = 0 ↔ a = 0 := by simp only [nnreal.eq_iff.symm, nnreal.coe_zero, coe_nnnorm, norm_eq_zero] @[simp] lemma nnnorm_zero : nnnorm (0 : α) = 0 := nnreal.eq norm_zero lemma nnnorm_add_le (g h : α) : nnnorm (g + h) ≤ nnnorm g + nnnorm h := nnreal.coe_le_coe.2 $ norm_add_le g h @[simp] lemma nnnorm_neg (g : α) : nnnorm (-g) = nnnorm g := nnreal.eq $ norm_neg g lemma nndist_nnnorm_nnnorm_le (g h : α) : nndist (nnnorm g) (nnnorm h) ≤ nnnorm (g - h) := nnreal.coe_le_coe.2 $ dist_norm_norm_le g h lemma of_real_norm_eq_coe_nnnorm (x : β) : ennreal.of_real ∥x∥ = (nnnorm x : ennreal) := ennreal.of_real_eq_coe_nnreal _ lemma edist_eq_coe_nnnorm_sub (x y : β) : edist x y = (nnnorm (x - y) : ennreal) := by rw [edist_dist, dist_eq_norm, of_real_norm_eq_coe_nnnorm] lemma edist_eq_coe_nnnorm (x : β) : edist x 0 = (nnnorm x : ennreal) := by rw [edist_eq_coe_nnnorm_sub, _root_.sub_zero] lemma nndist_add_add_le (g₁ g₂ h₁ h₂ : α) : nndist (g₁ + g₂) (h₁ + h₂) ≤ nndist g₁ h₁ + nndist g₂ h₂ := nnreal.coe_le_coe.2 $ dist_add_add_le g₁ g₂ h₁ h₂ lemma edist_add_add_le (g₁ g₂ h₁ h₂ : α) : edist (g₁ + g₂) (h₁ + h₂) ≤ edist g₁ h₁ + edist g₂ h₂ := by { simp only [edist_nndist], norm_cast, apply nndist_add_add_le } lemma nnnorm_sum_le {β} : ∀(s : finset β) (f : β → α), nnnorm (s.sum f) ≤ s.sum (λa, nnnorm (f a)) := finset.le_sum_of_subadditive nnnorm nnnorm_zero nnnorm_add_le end nnnorm lemma lipschitz_with.neg {α : Type*} [emetric_space α] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) : lipschitz_with K (λ x, -f x) := λ x y, by simpa only [edist_dist, dist_neg_neg] using hf x y lemma lipschitz_with.add {α : Type*} [emetric_space α] {Kf : nnreal} {f : α → β} (hf : lipschitz_with Kf f) {Kg : nnreal} {g : α → β} (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (λ x, f x + g x) := λ x y, calc edist (f x + g x) (f y + g y) ≤ edist (f x) (f y) + edist (g x) (g y) : edist_add_add_le _ _ _ _ ... ≤ Kf * edist x y + Kg * edist x y : add_le_add' (hf x y) (hg x y) ... = (Kf + Kg) * edist x y : (add_mul _ _ _).symm lemma lipschitz_with.sub {α : Type*} [emetric_space α] {Kf : nnreal} {f : α → β} (hf : lipschitz_with Kf f) {Kg : nnreal} {g : α → β} (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (λ x, f x - g x) := hf.add hg.neg lemma antilipschitz_with.add_lipschitz_with {α : Type*} [metric_space α] {Kf : nnreal} {f : α → β} (hf : antilipschitz_with Kf f) {Kg : nnreal} {g : α → β} (hg : lipschitz_with Kg g) (hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ (λ x, f x + g x) := begin refine antilipschitz_with.of_le_mul_dist (λ x y, _), rw [nnreal.coe_inv, ← div_eq_inv_mul'], apply le_div_of_mul_le (nnreal.coe_pos.2 $ nnreal.sub_pos.2 hK), rw [mul_comm, nnreal.coe_sub (le_of_lt hK), sub_mul], calc ↑Kf⁻¹ * dist x y - Kg * dist x y ≤ dist (f x) (f y) - dist (g x) (g y) : sub_le_sub (hf.mul_le_dist x y) (hg.dist_le_mul x y) ... ≤ _ : le_trans (le_abs_self _) (abs_dist_sub_le_dist_add_add _ _ _ _) end /-- A submodule of a normed group is also a normed group, with the restriction of the norm. As all instances can be inferred from the submodule `s`, they are put as implicit instead of typeclasses. -/ instance submodule.normed_group {𝕜 : Type*} {_ : ring 𝕜} {E : Type*} [normed_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) : normed_group s := { norm := λx, norm (x : E), dist_eq := λx y, dist_eq_norm (x : E) (y : E) } /-- normed group instance on the product of two normed groups, using the sup norm. -/ instance prod.normed_group : normed_group (α × β) := { norm := λx, max ∥x.1∥ ∥x.2∥, dist_eq := assume (x y : α × β), show max (dist x.1 y.1) (dist x.2 y.2) = (max ∥(x - y).1∥ ∥(x - y).2∥), by simp [dist_eq_norm] } lemma norm_fst_le (x : α × β) : ∥x.1∥ ≤ ∥x∥ := by simp [norm, le_max_left] lemma norm_snd_le (x : α × β) : ∥x.2∥ ≤ ∥x∥ := by simp [norm, le_max_right] lemma norm_prod_le_iff {x : α × β} {r : ℝ} : ∥x∥ ≤ r ↔ ∥x.1∥ ≤ r ∧ ∥x.2∥ ≤ r := max_le_iff /-- normed group instance on the product of finitely many normed groups, using the sup norm. -/ instance pi.normed_group {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] : normed_group (Πi, π i) := { norm := λf, ((finset.sup finset.univ (λ b, nnnorm (f b)) : nnreal) : ℝ), dist_eq := assume x y, congr_arg (coe : nnreal → ℝ) $ congr_arg (finset.sup finset.univ) $ funext $ assume a, show nndist (x a) (y a) = nnnorm (x a - y a), from nndist_eq_nnnorm _ _ } /-- The norm of an element in a product space is `≤ r` if and only if the norm of each component is. -/ lemma pi_norm_le_iff {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] {r : ℝ} (hr : 0 ≤ r) {x : Πi, π i} : ∥x∥ ≤ r ↔ ∀i, ∥x i∥ ≤ r := by { simp only [(dist_zero_right _).symm, dist_pi_le_iff hr], refl } lemma norm_le_pi_norm {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] (x : Πi, π i) (i : ι) : ∥x i∥ ≤ ∥x∥ := (pi_norm_le_iff (norm_nonneg x)).1 (le_refl _) i lemma tendsto_iff_norm_tendsto_zero {f : ι → β} {a : filter ι} {b : β} : tendsto f a (𝓝 b) ↔ tendsto (λ e, ∥ f e - b ∥) a (𝓝 0) := by rw tendsto_iff_dist_tendsto_zero ; simp only [(dist_eq_norm _ _).symm] lemma tendsto_zero_iff_norm_tendsto_zero {f : γ → β} {a : filter γ} : tendsto f a (𝓝 0) ↔ tendsto (λ e, ∥ f e ∥) a (𝓝 0) := have tendsto f a (𝓝 0) ↔ tendsto (λ e, ∥ f e - 0 ∥) a (𝓝 0) := tendsto_iff_norm_tendsto_zero, by simpa lemma lim_norm (x : α) : (λg:α, ∥g - x∥) →_{x} 0 := tendsto_iff_norm_tendsto_zero.1 (continuous_iff_continuous_at.1 continuous_id x) lemma lim_norm_zero : (λg:α, ∥g∥) →_{0} 0 := by simpa using lim_norm (0:α) lemma continuous_norm : continuous (λg:α, ∥g∥) := begin rw continuous_iff_continuous_at, intro x, rw [continuous_at, tendsto_iff_dist_tendsto_zero], exact squeeze_zero (λ t, abs_nonneg _) (λ t, abs_norm_sub_norm_le _ _) (lim_norm x) end lemma filter.tendsto.norm {β : Type*} {l : filter β} {f : β → α} {a : α} (h : tendsto f l (𝓝 a)) : tendsto (λ x, ∥f x∥) l (𝓝 ∥a∥) := tendsto.comp continuous_norm.continuous_at h lemma continuous_nnnorm : continuous (nnnorm : α → nnreal) := continuous_subtype_mk _ continuous_norm lemma filter.tendsto.nnnorm {β : Type*} {l : filter β} {f : β → α} {a : α} (h : tendsto f l (𝓝 a)) : tendsto (λ x, nnnorm (f x)) l (𝓝 (nnnorm a)) := tendsto.comp continuous_nnnorm.continuous_at h /-- If `∥y∥→∞`, then we can assume `y≠x` for any fixed `x`. -/ lemma eventually_ne_of_tendsto_norm_at_top {l : filter γ} {f : γ → α} (h : tendsto (λ y, ∥f y∥) l at_top) (x : α) : ∀ᶠ y in l, f y ≠ x := begin have : ∀ᶠ y in l, 1 + ∥x∥ ≤ ∥f y∥ := h (mem_at_top (1 + ∥x∥)), refine this.mono (λ y hy hxy, _), subst x, exact not_le_of_lt zero_lt_one (add_le_iff_nonpos_left.1 hy) end /-- A normed group is a uniform additive group, i.e., addition and subtraction are uniformly continuous. -/ @[priority 100] -- see Note [lower instance priority] instance normed_uniform_group : uniform_add_group α := begin refine ⟨metric.uniform_continuous_iff.2 $ assume ε hε, ⟨ε / 2, half_pos hε, assume a b h, _⟩⟩, rw [prod.dist_eq, max_lt_iff, dist_eq_norm, dist_eq_norm] at h, calc dist (a.1 - a.2) (b.1 - b.2) = ∥(a.1 - b.1) - (a.2 - b.2)∥ : by simp [dist_eq_norm, sub_eq_add_neg]; abel ... ≤ ∥a.1 - b.1∥ + ∥a.2 - b.2∥ : norm_sub_le _ _ ... < ε / 2 + ε / 2 : add_lt_add h.1 h.2 ... = ε : add_halves _ end @[priority 100] -- see Note [lower instance priority] instance normed_top_monoid : topological_add_monoid α := by apply_instance -- short-circuit type class inference @[priority 100] -- see Note [lower instance priority] instance normed_top_group : topological_add_group α := by apply_instance -- short-circuit type class inference end normed_group section normed_ring section prio set_option default_priority 100 -- see Note [default priority] /-- A normed ring is a ring endowed with a norm which satisfies the inequality `∥x y∥ ≤ ∥x∥ ∥y∥`. -/ class normed_ring (α : Type*) extends has_norm α, ring α, metric_space α := (dist_eq : ∀ x y, dist x y = norm (x - y)) (norm_mul : ∀ a b, norm (a * b) ≤ norm a * norm b) end prio @[priority 100] -- see Note [lower instance priority] instance normed_ring.to_normed_group [β : normed_ring α] : normed_group α := { ..β } lemma norm_mul_le {α : Type*} [normed_ring α] (a b : α) : (∥a*b∥) ≤ (∥a∥) * (∥b∥) := normed_ring.norm_mul _ _ lemma norm_pow_le {α : Type*} [normed_ring α] (a : α) : ∀ {n : ℕ}, 0 < n → ∥a^n∥ ≤ ∥a∥^n | 1 h := by simp | (n+2) h := le_trans (norm_mul_le a (a^(n+1))) (mul_le_mul (le_refl _) (norm_pow_le (nat.succ_pos _)) (norm_nonneg _) (norm_nonneg _)) /-- Normed ring structure on the product of two normed rings, using the sup norm. -/ instance prod.normed_ring [normed_ring α] [normed_ring β] : normed_ring (α × β) := { norm_mul := assume x y, calc ∥x * y∥ = ∥(x.1*y.1, x.2*y.2)∥ : rfl ... = (max ∥x.1*y.1∥ ∥x.2*y.2∥) : rfl ... ≤ (max (∥x.1∥*∥y.1∥) (∥x.2∥*∥y.2∥)) : max_le_max (norm_mul_le (x.1) (y.1)) (norm_mul_le (x.2) (y.2)) ... = (max (∥x.1∥*∥y.1∥) (∥y.2∥*∥x.2∥)) : by simp[mul_comm] ... ≤ (max (∥x.1∥) (∥x.2∥)) * (max (∥y.2∥) (∥y.1∥)) : by { apply max_mul_mul_le_max_mul_max; simp [norm_nonneg] } ... = (max (∥x.1∥) (∥x.2∥)) * (max (∥y.1∥) (∥y.2∥)) : by simp[max_comm] ... = (∥x∥*∥y∥) : rfl, ..prod.normed_group } end normed_ring @[priority 100] -- see Note [lower instance priority] instance normed_ring_top_monoid [normed_ring α] : topological_monoid α := ⟨ continuous_iff_continuous_at.2 $ λ x, tendsto_iff_norm_tendsto_zero.2 $ have ∀ e : α × α, e.fst * e.snd - x.fst * x.snd = e.fst * e.snd - e.fst * x.snd + (e.fst * x.snd - x.fst * x.snd), by intro; rw sub_add_sub_cancel, begin apply squeeze_zero, { intro, apply norm_nonneg }, { simp only [this], intro, apply norm_add_le }, { rw ←zero_add (0 : ℝ), apply tendsto.add, { apply squeeze_zero, { intro, apply norm_nonneg }, { intro t, show ∥t.fst * t.snd - t.fst * x.snd∥ ≤ ∥t.fst∥ * ∥t.snd - x.snd∥, rw ←mul_sub, apply norm_mul_le }, { rw ←mul_zero (∥x.fst∥), apply tendsto.mul, { apply continuous_iff_continuous_at.1, apply continuous_norm.comp continuous_fst }, { apply tendsto_iff_norm_tendsto_zero.1, apply continuous_iff_continuous_at.1, apply continuous_snd }}}, { apply squeeze_zero, { intro, apply norm_nonneg }, { intro t, show ∥t.fst * x.snd - x.fst * x.snd∥ ≤ ∥t.fst - x.fst∥ * ∥x.snd∥, rw ←sub_mul, apply norm_mul_le }, { rw ←zero_mul (∥x.snd∥), apply tendsto.mul, { apply tendsto_iff_norm_tendsto_zero.1, apply continuous_iff_continuous_at.1, apply continuous_fst }, { apply tendsto_const_nhds }}}} end ⟩ /-- A normed ring is a topological ring. -/ @[priority 100] -- see Note [lower instance priority] instance normed_top_ring [normed_ring α] : topological_ring α := ⟨ continuous_iff_continuous_at.2 $ λ x, tendsto_iff_norm_tendsto_zero.2 $ have ∀ e : α, -e - -x = -(e - x), by intro; simp, by simp only [this, norm_neg]; apply lim_norm ⟩ section prio set_option default_priority 100 -- see Note [default priority] /-- A normed field is a field with a norm satisfying ∥x y∥ = ∥x∥ ∥y∥. -/ class normed_field (α : Type*) extends has_norm α, field α, metric_space α := (dist_eq : ∀ x y, dist x y = norm (x - y)) (norm_mul' : ∀ a b, norm (a * b) = norm a * norm b) /-- A nondiscrete normed field is a normed field in which there is an element of norm different from `0` and `1`. This makes it possible to bring any element arbitrarily close to `0` by multiplication by the powers of any element, and thus to relate algebra and topology. -/ class nondiscrete_normed_field (α : Type*) extends normed_field α := (non_trivial : ∃x:α, 1<∥x∥) end prio @[priority 100] -- see Note [lower instance priority] instance normed_field.to_normed_ring [i : normed_field α] : normed_ring α := { norm_mul := by finish [i.norm_mul'], ..i } namespace normed_field @[simp] lemma norm_one {α : Type*} [normed_field α] : ∥(1 : α)∥ = 1 := have ∥(1 : α)∥ * ∥(1 : α)∥ = ∥(1 : α)∥ * 1, by calc ∥(1 : α)∥ * ∥(1 : α)∥ = ∥(1 : α) * (1 : α)∥ : by rw normed_field.norm_mul' ... = ∥(1 : α)∥ * 1 : by simp, eq_of_mul_eq_mul_left (ne_of_gt (norm_pos_iff.2 (by simp))) this @[simp] lemma norm_mul [normed_field α] (a b : α) : ∥a * b∥ = ∥a∥ * ∥b∥ := normed_field.norm_mul' a b instance normed_field.is_monoid_hom_norm [normed_field α] : is_monoid_hom (norm : α → ℝ) := { map_one := norm_one, map_mul := norm_mul } @[simp] lemma norm_pow [normed_field α] (a : α) : ∀ (n : ℕ), ∥a^n∥ = ∥a∥^n := is_monoid_hom.map_pow norm a @[simp] lemma norm_prod {β : Type*} [normed_field α] (s : finset β) (f : β → α) : ∥s.prod f∥ = s.prod (λb, ∥f b∥) := eq.symm (s.prod_hom norm) @[simp] lemma norm_div {α : Type*} [normed_field α] (a b : α) : ∥a/b∥ = ∥a∥/∥b∥ := begin classical, by_cases hb : b = 0, {simp [hb]}, apply eq_div_of_mul_eq, { apply ne_of_gt, apply norm_pos_iff.mpr hb }, { rw [←normed_field.norm_mul, div_mul_cancel _ hb] } end @[simp] lemma norm_inv {α : Type*} [normed_field α] (a : α) : ∥a⁻¹∥ = ∥a∥⁻¹ := by simp only [inv_eq_one_div, norm_div, norm_one] @[simp] lemma norm_fpow {α : Type*} [normed_field α] (a : α) : ∀n : ℤ, ∥a^n∥ = ∥a∥^n | (n : ℕ) := norm_pow a n | -[1+ n] := by simp [fpow_neg_succ_of_nat] lemma exists_one_lt_norm (α : Type*) [i : nondiscrete_normed_field α] : ∃x : α, 1 < ∥x∥ := i.non_trivial lemma exists_norm_lt_one (α : Type*) [nondiscrete_normed_field α] : ∃x : α, 0 < ∥x∥ ∧ ∥x∥ < 1 := begin rcases exists_one_lt_norm α with ⟨y, hy⟩, refine ⟨y⁻¹, _, _⟩, { simp only [inv_eq_zero, ne.def, norm_pos_iff], assume h, rw ← norm_eq_zero at h, rw h at hy, exact lt_irrefl _ (lt_trans zero_lt_one hy) }, { simp [inv_lt_one hy] } end lemma exists_lt_norm (α : Type*) [nondiscrete_normed_field α] (r : ℝ) : ∃ x : α, r < ∥x∥ := let ⟨w, hw⟩ := exists_one_lt_norm α in let ⟨n, hn⟩ := pow_unbounded_of_one_lt r hw in ⟨w^n, by rwa norm_pow⟩ lemma exists_norm_lt (α : Type*) [nondiscrete_normed_field α] {r : ℝ} (hr : 0 < r) : ∃ x : α, 0 < ∥x∥ ∧ ∥x∥ < r := let ⟨w, hw⟩ := exists_one_lt_norm α in let ⟨n, hle, hlt⟩ := exists_int_pow_near' hr hw in ⟨w^n, by { rw norm_fpow; exact fpow_pos_of_pos (lt_trans zero_lt_one hw) _}, by rwa norm_fpow⟩ lemma punctured_nhds_ne_bot {α : Type*} [nondiscrete_normed_field α] (x : α) : nhds_within x (-{x}) ≠ ⊥ := begin rw [← mem_closure_iff_nhds_within_ne_bot, metric.mem_closure_iff], rintros ε ε0, rcases normed_field.exists_norm_lt α ε0 with ⟨b, hb0, hbε⟩, refine ⟨x + b, mt (set.mem_singleton_iff.trans add_right_eq_self).1 $ norm_pos_iff.1 hb0, _⟩, rwa [dist_comm, dist_eq_norm, add_sub_cancel'], end lemma tendsto_inv [normed_field α] {r : α} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (𝓝 r) (𝓝 r⁻¹) := begin refine (nhds_basis_closed_ball.tendsto_iff nhds_basis_closed_ball).2 (λε εpos, _), let δ := min (ε/2 * ∥r∥^2) (∥r∥/2), have norm_r_pos : 0 < ∥r∥ := norm_pos_iff.mpr r0, have A : 0 < ε / 2 * ∥r∥ ^ 2 := mul_pos' (half_pos εpos) (pow_pos norm_r_pos 2), have δpos : 0 < δ, by simp [half_pos norm_r_pos, A], refine ⟨δ, δpos, λ x hx, _⟩, have rx : ∥r∥/2 ≤ ∥x∥ := calc ∥r∥/2 = ∥r∥ - ∥r∥/2 : by ring ... ≤ ∥r∥ - ∥r - x∥ : begin apply sub_le_sub (le_refl _), rw [← dist_eq_norm, dist_comm], exact le_trans hx (min_le_right _ _) end ... ≤ ∥r - (r - x)∥ : norm_sub_norm_le r (r - x) ... = ∥x∥ : by simp [sub_sub_cancel], have norm_x_pos : 0 < ∥x∥ := lt_of_lt_of_le (half_pos norm_r_pos) rx, have : x⁻¹ - r⁻¹ = (r - x) * x⁻¹ * r⁻¹, by rw [sub_mul, sub_mul, mul_inv_cancel (norm_pos_iff.mp norm_x_pos), one_mul, mul_comm, ← mul_assoc, inv_mul_cancel r0, one_mul], calc dist x⁻¹ r⁻¹ = ∥x⁻¹ - r⁻¹∥ : dist_eq_norm _ _ ... ≤ ∥r-x∥ * ∥x∥⁻¹ * ∥r∥⁻¹ : by rw [this, norm_mul, norm_mul, norm_inv, norm_inv] ... ≤ (ε/2 * ∥r∥^2) * (2 * ∥r∥⁻¹) * (∥r∥⁻¹) : begin apply_rules [mul_le_mul, inv_nonneg.2, le_of_lt A, norm_nonneg, inv_nonneg.2, mul_nonneg, (inv_le_inv norm_x_pos norm_r_pos).2, le_refl], show ∥r - x∥ ≤ ε / 2 * ∥r∥ ^ 2, by { rw [← dist_eq_norm, dist_comm], exact le_trans hx (min_le_left _ _) }, show ∥x∥⁻¹ ≤ 2 * ∥r∥⁻¹, { convert (inv_le_inv norm_x_pos (half_pos norm_r_pos)).2 rx, rw [inv_div, div_eq_inv_mul', mul_comm] }, show (0 : ℝ) ≤ 2, by norm_num end ... = ε * (∥r∥ * ∥r∥⁻¹)^2 : by { generalize : ∥r∥⁻¹ = u, ring } ... = ε : by { rw [mul_inv_cancel (ne.symm (ne_of_lt norm_r_pos))], simp } end lemma continuous_on_inv [normed_field α] : continuous_on (λ(x:α), x⁻¹) {x | x ≠ 0} := begin assume x hx, apply continuous_at.continuous_within_at, exact (tendsto_inv hx) end instance : normed_field ℝ := { norm := λ x, abs x, dist_eq := assume x y, rfl, norm_mul' := abs_mul } instance : nondiscrete_normed_field ℝ := { non_trivial := ⟨2, by { unfold norm, rw abs_of_nonneg; norm_num }⟩ } end normed_field /-- If a function converges to a nonzero value, its inverse converges to the inverse of this value. We use the name `tendsto.inv'` as `tendsto.inv` is already used in multiplicative topological groups. -/ lemma filter.tendsto.inv' [normed_field α] {l : filter β} {f : β → α} {y : α} (hy : y ≠ 0) (h : tendsto f l (𝓝 y)) : tendsto (λx, (f x)⁻¹) l (𝓝 y⁻¹) := (normed_field.tendsto_inv hy).comp h lemma filter.tendsto.div [normed_field α] {l : filter β} {f g : β → α} {x y : α} (hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) (hy : y ≠ 0) : tendsto (λa, f a / g a) l (𝓝 (x / y)) := hf.mul (hg.inv' hy) lemma real.norm_eq_abs (r : ℝ) : norm r = abs r := rfl @[simp] lemma norm_norm [normed_group α] (x : α) : ∥∥x∥∥ = ∥x∥ := by rw [real.norm_eq_abs, abs_of_nonneg (norm_nonneg _)] @[simp] lemma nnnorm_norm [normed_group α] (a : α) : nnnorm ∥a∥ = nnnorm a := by simp only [nnnorm, norm_norm] instance : normed_ring ℤ := { norm := λ n, ∥(n : ℝ)∥, norm_mul := λ m n, le_of_eq $ by simp only [norm, int.cast_mul, abs_mul], dist_eq := λ m n, by simp only [int.dist_eq, norm, int.cast_sub] } @[norm_cast] lemma int.norm_cast_real (m : ℤ) : ∥(m : ℝ)∥ = ∥m∥ := rfl instance : normed_field ℚ := { norm := λ r, ∥(r : ℝ)∥, norm_mul' := λ r₁ r₂, by simp only [norm, rat.cast_mul, abs_mul], dist_eq := λ r₁ r₂, by simp only [rat.dist_eq, norm, rat.cast_sub] } instance : nondiscrete_normed_field ℚ := { non_trivial := ⟨2, by { unfold norm, rw abs_of_nonneg; norm_num }⟩ } @[norm_cast, simp] lemma rat.norm_cast_real (r : ℚ) : ∥(r : ℝ)∥ = ∥r∥ := rfl @[norm_cast, simp] lemma int.norm_cast_rat (m : ℤ) : ∥(m : ℚ)∥ = ∥m∥ := by rw [← rat.norm_cast_real, ← int.norm_cast_real]; congr' 1; norm_cast section normed_space section prio set_option default_priority 100 -- see Note [default priority] -- see Note[vector space definition] for why we extend `module`. /-- A normed space over a normed field is a vector space endowed with a norm which satisfies the equality `∥c • x∥ = ∥c∥ ∥x∥`. -/ class normed_space (α : Type*) (β : Type*) [normed_field α] [normed_group β] extends module α β := (norm_smul : ∀ (a:α) (b:β), norm (a • b) = has_norm.norm a * norm b) end prio variables [normed_field α] [normed_group β] instance normed_field.to_normed_space : normed_space α α := { norm_smul := normed_field.norm_mul } set_option class.instance_max_depth 43 lemma norm_smul [normed_space α β] (s : α) (x : β) : ∥s • x∥ = ∥s∥ * ∥x∥ := normed_space.norm_smul s x lemma dist_smul [normed_space α β] (s : α) (x y : β) : dist (s • x) (s • y) = ∥s∥ * dist x y := by simp only [dist_eq_norm, (norm_smul _ _).symm, smul_sub] lemma nnnorm_smul [normed_space α β] (s : α) (x : β) : nnnorm (s • x) = nnnorm s * nnnorm x := nnreal.eq $ norm_smul s x lemma nndist_smul [normed_space α β] (s : α) (x y : β) : nndist (s • x) (s • y) = nnnorm s * nndist x y := nnreal.eq $ dist_smul s x y variables {E : Type*} {F : Type*} [normed_group E] [normed_space α E] [normed_group F] [normed_space α F] @[priority 100] -- see Note [lower instance priority] instance normed_space.topological_vector_space : topological_vector_space α E := begin refine { continuous_smul := continuous_iff_continuous_at.2 $ λ p, tendsto_iff_norm_tendsto_zero.2 _ }, refine squeeze_zero (λ _, norm_nonneg _) _ _, { exact λ q, ∥q.1 - p.1∥ * ∥q.2∥ + ∥p.1∥ * ∥q.2 - p.2∥ }, { intro q, rw [← sub_add_sub_cancel, ← norm_smul, ← norm_smul, smul_sub, sub_smul], exact norm_add_le _ _ }, { conv { congr, skip, skip, congr, rw [← zero_add (0:ℝ)], congr, rw [← zero_mul ∥p.2∥], skip, rw [← mul_zero ∥p.1∥] }, exact ((tendsto_iff_norm_tendsto_zero.1 (continuous_fst.tendsto p)).mul (continuous_snd.tendsto p).norm).add (tendsto_const_nhds.mul (tendsto_iff_norm_tendsto_zero.1 (continuous_snd.tendsto p))) } end /-- In a normed space over a nondiscrete normed field, only `⊤` submodule has a nonempty interior. See also `submodule.eq_top_of_nonempty_interior'` for a `topological_module` version. -/ lemma submodule.eq_top_of_nonempty_interior {α E : Type*} [nondiscrete_normed_field α] [normed_group E] [normed_space α E] (s : submodule α E) (hs : (interior (s:set E)).nonempty) : s = ⊤ := begin refine s.eq_top_of_nonempty_interior' _ hs, simp only [is_unit_iff_ne_zero, @ne.def α, set.mem_singleton_iff.symm], exact normed_field.punctured_nhds_ne_bot _ end open normed_field /-- If there is a scalar `c` with `∥c∥>1`, then any element can be moved by scalar multiplication to any shell of width `∥c∥`. Also recap information on the norm of the rescaling element that shows up in applications. -/ lemma rescale_to_shell {c : α} (hc : 1 < ∥c∥) {ε : ℝ} (εpos : 0 < ε) {x : E} (hx : x ≠ 0) : ∃d:α, d ≠ 0 ∧ ∥d • x∥ ≤ ε ∧ (ε/∥c∥ ≤ ∥d • x∥) ∧ (∥d∥⁻¹ ≤ ε⁻¹ * ∥c∥ * ∥x∥) := begin have xεpos : 0 < ∥x∥/ε := div_pos_of_pos_of_pos (norm_pos_iff.2 hx) εpos, rcases exists_int_pow_near xεpos hc with ⟨n, hn⟩, have cpos : 0 < ∥c∥ := lt_trans (zero_lt_one : (0 :ℝ) < 1) hc, have cnpos : 0 < ∥c^(n+1)∥ := by { rw norm_fpow, exact lt_trans xεpos hn.2 }, refine ⟨(c^(n+1))⁻¹, _, _, _, _⟩, show (c ^ (n + 1))⁻¹ ≠ 0, by rwa [ne.def, inv_eq_zero, ← ne.def, ← norm_pos_iff], show ∥(c ^ (n + 1))⁻¹ • x∥ ≤ ε, { rw [norm_smul, norm_inv, ← div_eq_inv_mul', div_le_iff cnpos, mul_comm, norm_fpow], exact (div_le_iff εpos).1 (le_of_lt (hn.2)) }, show ε / ∥c∥ ≤ ∥(c ^ (n + 1))⁻¹ • x∥, { rw [div_le_iff cpos, norm_smul, norm_inv, norm_fpow, fpow_add (ne_of_gt cpos), fpow_one, mul_inv', mul_comm, ← mul_assoc, ← mul_assoc, mul_inv_cancel (ne_of_gt cpos), one_mul, ← div_eq_inv_mul', le_div_iff (fpow_pos_of_pos cpos _), mul_comm], exact (le_div_iff εpos).1 hn.1 }, show ∥(c ^ (n + 1))⁻¹∥⁻¹ ≤ ε⁻¹ * ∥c∥ * ∥x∥, { have : ε⁻¹ * ∥c∥ * ∥x∥ = ε⁻¹ * ∥x∥ * ∥c∥, by ring, rw [norm_inv, inv_inv', norm_fpow, fpow_add (ne_of_gt cpos), fpow_one, this, ← div_eq_inv_mul'], exact mul_le_mul_of_nonneg_right hn.1 (norm_nonneg _) } end /-- The product of two normed spaces is a normed space, with the sup norm. -/ instance : normed_space α (E × F) := { norm_smul := begin intros s x, cases x with x₁ x₂, change max (∥s • x₁∥) (∥s • x₂∥) = ∥s∥ * max (∥x₁∥) (∥x₂∥), rw [norm_smul, norm_smul, ← mul_max_of_nonneg _ _ (norm_nonneg _)] end, add_smul := λ r x y, prod.ext (add_smul _ _ _) (add_smul _ _ _), smul_add := λ r x y, prod.ext (smul_add _ _ _) (smul_add _ _ _), ..prod.normed_group, ..prod.module } /-- The product of finitely many normed spaces is a normed space, with the sup norm. -/ instance pi.normed_space {E : ι → Type*} [fintype ι] [∀i, normed_group (E i)] [∀i, normed_space α (E i)] : normed_space α (Πi, E i) := { norm_smul := λ a f, show (↑(finset.sup finset.univ (λ (b : ι), nnnorm (a • f b))) : ℝ) = nnnorm a * ↑(finset.sup finset.univ (λ (b : ι), nnnorm (f b))), by simp only [(nnreal.coe_mul _ _).symm, nnreal.mul_finset_sup, nnnorm_smul] } /-- A subspace of a normed space is also a normed space, with the restriction of the norm. -/ instance submodule.normed_space {𝕜 : Type*} [normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] (s : submodule 𝕜 E) : normed_space 𝕜 s := { norm_smul := λc x, norm_smul c (x : E) } end normed_space section normed_algebra section prio set_option default_priority 100 -- see Note [default priority] /-- A normed algebra `𝕜'` over `𝕜` is an algebra endowed with a norm for which the embedding of `𝕜` in `𝕜'` is an isometry. -/ class normed_algebra (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜'] extends algebra 𝕜 𝕜' := (norm_algebra_map_eq : ∀x:𝕜, ∥algebra_map 𝕜 𝕜' x∥ = ∥x∥) end prio @[simp] lemma norm_algebra_map_eq {𝕜 : Type*} (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜'] [h : normed_algebra 𝕜 𝕜'] (x : 𝕜) : ∥algebra_map 𝕜 𝕜' x∥ = ∥x∥ := normed_algebra.norm_algebra_map_eq _ end normed_algebra section restrict_scalars set_option class.instance_max_depth 40 variables (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] {E : Type*} [normed_group E] [normed_space 𝕜' E] /-- `𝕜`-normed space structure induced by a `𝕜'`-normed space structure when `𝕜'` is a normed algebra over `𝕜`. Not registered as an instance as `𝕜'` can not be inferred. -/ def normed_space.restrict_scalars : normed_space 𝕜 E := { norm_smul := λc x, begin change ∥(algebra_map 𝕜 𝕜' c) • x∥ = ∥c∥ * ∥x∥, simp [norm_smul] end, ..module.restrict_scalars 𝕜 𝕜' E } end restrict_scalars section summable open_locale classical open finset filter variables [normed_group α] @[nolint ge_or_gt] -- see Note [nolint_ge] lemma cauchy_seq_finset_iff_vanishing_norm {f : ι → α} : cauchy_seq (λ s : finset ι, s.sum f) ↔ ∀ε > 0, ∃s:finset ι, ∀t, disjoint t s → ∥ t.sum f ∥ < ε := begin simp only [cauchy_seq_finset_iff_vanishing, metric.mem_nhds_iff, exists_imp_distrib], split, { assume h ε hε, refine h {x | ∥x∥ < ε} ε hε _, rw [ball_0_eq ε] }, { assume h s ε hε hs, rcases h ε hε with ⟨t, ht⟩, refine ⟨t, assume u hu, hs _⟩, rw [ball_0_eq], exact ht u hu } end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma summable_iff_vanishing_norm [complete_space α] {f : ι → α} : summable f ↔ ∀ε > 0, ∃s:finset ι, ∀t, disjoint t s → ∥ t.sum f ∥ < ε := by rw [summable_iff_cauchy_seq_finset, cauchy_seq_finset_iff_vanishing_norm] lemma cauchy_seq_finset_of_norm_bounded {f : ι → α} (g : ι → ℝ) (hg : summable g) (h : ∀i, ∥f i∥ ≤ g i) : cauchy_seq (λ s : finset ι, s.sum f) := cauchy_seq_finset_iff_vanishing_norm.2 $ assume ε hε, let ⟨s, hs⟩ := summable_iff_vanishing_norm.1 hg ε hε in ⟨s, assume t ht, have ∥t.sum g∥ < ε := hs t ht, have nn : 0 ≤ t.sum g := finset.sum_nonneg (assume a _, le_trans (norm_nonneg _) (h a)), lt_of_le_of_lt (norm_sum_le_of_le t (λ i _, h i)) $ by rwa [real.norm_eq_abs, abs_of_nonneg nn] at this⟩ lemma cauchy_seq_finset_of_summable_norm {f : ι → α} (hf : summable (λa, ∥f a∥)) : cauchy_seq (λ s : finset ι, s.sum f) := cauchy_seq_finset_of_norm_bounded _ hf (assume i, le_refl _) /-- If a function `f` is summable in norm, and along some sequence of finsets exhausting the space its sum is converging to a limit `a`, then this holds along all finsets, i.e., `f` is summable with sum `a`. -/ lemma has_sum_of_subseq_of_summable {f : ι → α} (hf : summable (λa, ∥f a∥)) {s : β → finset ι} {p : filter β} (hp : p ≠ ⊥) (hs : tendsto s p at_top) {a : α} (ha : tendsto (λ b, (s b).sum f) p (𝓝 a)) : has_sum f a := tendsto_nhds_of_cauchy_seq_of_subseq (cauchy_seq_finset_of_summable_norm hf) hp hs ha /-- If `∑ i, ∥f i∥` is summable, then `∥(∑ i, f i)∥ ≤ (∑ i, ∥f i∥)`. Note that we do not assume that `∑ i, f i` is summable, and it might not be the case if `α` is not a complete space. -/ lemma norm_tsum_le_tsum_norm {f : ι → α} (hf : summable (λi, ∥f i∥)) : ∥(∑i, f i)∥ ≤ (∑ i, ∥f i∥) := begin by_cases h : summable f, { have h₁ : tendsto (λs:finset ι, ∥s.sum f∥) at_top (𝓝 ∥(∑ i, f i)∥) := (continuous_norm.tendsto _).comp h.has_sum, have h₂ : tendsto (λs:finset ι, s.sum (λi, ∥f i∥)) at_top (𝓝 (∑ i, ∥f i∥)) := hf.has_sum, exact le_of_tendsto_of_tendsto' at_top_ne_bot h₁ h₂ (assume s, norm_sum_le _ _) }, { rw tsum_eq_zero_of_not_summable h, simp [tsum_nonneg] } end variable [complete_space α] lemma summable_of_norm_bounded {f : ι → α} (g : ι → ℝ) (hg : summable g) (h : ∀i, ∥f i∥ ≤ g i) : summable f := by { rw summable_iff_cauchy_seq_finset, exact cauchy_seq_finset_of_norm_bounded g hg h } lemma summable_of_nnnorm_bounded {f : ι → α} (g : ι → nnreal) (hg : summable g) (h : ∀i, nnnorm (f i) ≤ g i) : summable f := summable_of_norm_bounded (λ i, (g i : ℝ)) (nnreal.summable_coe.2 hg) (λ i, by exact_mod_cast h i) lemma summable_of_summable_norm {f : ι → α} (hf : summable (λa, ∥f a∥)) : summable f := summable_of_norm_bounded _ hf (assume i, le_refl _) lemma summable_of_summable_nnnorm {f : ι → α} (hf : summable (λa, nnnorm (f a))) : summable f := summable_of_nnnorm_bounded _ hf (assume i, le_refl _) end summable
b2d7986e59b2c681308a0a08727899a780f45fb5
05b503addd423dd68145d68b8cde5cd595d74365
/src/algebra/field.lean
ee6e32642e3355cf2526ae4f01996ee04b010bcf
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,061
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import logic.basic algebra.ring algebra.group_with_zero open set universe u variables {α : Type u} @[priority 100] -- see Note [lower instance priority] instance division_ring.to_domain [s : division_ring α] : domain α := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, classical.by_contradiction $ λ hn, division_ring.mul_ne_zero (mt or.inl hn) (mt or.inr hn) h ..s } /-- Every division ring is a `group_with_zero`. -/ @[priority 10] -- see Note [lower instance priority] instance division_ring.to_group_with_zero {K : Type*} [division_ring K] : group_with_zero K := { zero_mul := _, mul_zero := _, .. ‹division_ring K› } /-- Every field is a `comm_group_with_zero`. -/ @[priority 100] -- see Note [lower instance priority] instance field.to_comm_group_with_zero {K : Type*} [field K] : comm_group_with_zero K := { .. (_ : group_with_zero K), .. ‹field K› } @[simp] theorem inv_one [division_ring α] : (1⁻¹ : α) = 1 := by rw [inv_eq_one_div, one_div_one] attribute [simp] inv_inv' section division_ring variables [s : division_ring α] {a b c : α} include s attribute [simp] div_one zero_div div_self lemma neg_inv : - a⁻¹ = (- a)⁻¹ := by rw [inv_eq_one_div, inv_eq_one_div, div_neg_eq_neg_div] lemma add_div (a b c : α) : (a + b) / c = a / c + b / c := (div_add_div_same _ _ _).symm lemma sub_div (a b c : α) : (a - b) / c = a / c - b / c := (div_sub_div_same _ _ _).symm lemma division_ring.inv_inj : a⁻¹ = b⁻¹ ↔ a = b := inv_inj'' _ _ lemma division_ring.inv_eq_iff : a⁻¹ = b ↔ b⁻¹ = a := inv_eq_iff lemma div_neg (a : α) : a / -b = -(a / b) := by rw [← div_neg_eq_neg_div] end division_ring @[priority 100] -- see Note [lower instance priority] instance field.to_integral_domain [F : field α] : integral_domain α := { ..F, ..division_ring.to_domain } section variables [field α] {a b c d : α} lemma inv_add_inv {a b : α} (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] lemma inv_sub_inv {a b : α} (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] lemma add_div' (a b c : α) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := by simpa using div_add_div b a one_ne_zero hc lemma sub_div' (a b c : α) (hc : c ≠ 0) : b - a / c = (b * c - a) / c := by simpa using div_sub_div b a one_ne_zero hc lemma div_add' (a b c : α) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := by rwa [add_comm, add_div', add_comm] lemma div_sub' (a b c : α) (hc : c ≠ 0) : a / c - b = (a - c * b) / c := by simpa using div_sub_div a b hc one_ne_zero end namespace ring_hom section variables {β : Type*} [division_ring α] [division_ring β] (f : α →+* β) {x y : α} lemma map_ne_zero : f x ≠ 0 ↔ x ≠ 0 := ⟨mt $ λ h, h.symm ▸ f.map_zero, λ x0 h, one_ne_zero $ by rw [← f.map_one, ← mul_inv_cancel x0, f.map_mul, h, zero_mul]⟩ lemma map_eq_zero : f x = 0 ↔ x = 0 := by haveI := classical.dec; exact not_iff_not.1 f.map_ne_zero lemma map_inv : f x⁻¹ = (f x)⁻¹ := begin classical, by_cases h : x = 0, by simp [h], apply (domain.mul_left_inj (f.map_ne_zero.2 h)).1, rw [mul_inv_cancel (f.map_ne_zero.2 h), ← f.map_mul, mul_inv_cancel h, f.map_one] end lemma map_div : f (x / y) = f x / f y := (f.map_mul _ _).trans $ congr_arg _ $ f.map_inv lemma injective : function.injective f := f.injective_iff.2 (λ a ha, classical.by_contradiction $ λ ha0, by simpa [ha, f.map_mul, f.map_one, zero_ne_one] using congr_arg f (mul_inv_cancel ha0)) end end ring_hom namespace is_ring_hom open ring_hom (of) section variables {β : Type*} [division_ring α] [division_ring β] variables (f : α → β) [is_ring_hom f] {x y : α} lemma map_ne_zero : f x ≠ 0 ↔ x ≠ 0 := (of f).map_ne_zero lemma map_eq_zero : f x = 0 ↔ x = 0 := (of f).map_eq_zero lemma map_inv : f x⁻¹ = (f x)⁻¹ := (of f).map_inv lemma map_div : f (x / y) = f x / f y := (of f).map_div lemma injective : function.injective f := (of f).injective end end is_ring_hom section field_simp mk_simp_attribute field_simps "The simpset `field_simps` is used by the tactic `field_simp` to reduce an expression in a field to an expression of the form `n / d` where `n` and `d` are division-free." lemma mul_div_assoc' {α : Type*} [division_ring α] (a b c : α) : a * (b / c) = (a * b) / c := by simp [mul_div_assoc] lemma neg_div' {α : Type*} [division_ring α] (a b : α) : - (b / a) = (-b) / a := by simp [neg_div] attribute [field_simps] div_add_div_same inv_eq_one_div div_mul_eq_mul_div div_add' add_div' div_div_eq_div_mul mul_div_assoc' div_eq_div_iff div_eq_iff eq_div_iff mul_ne_zero' div_div_eq_mul_div neg_div' two_ne_zero div_sub_div div_sub' sub_div' end field_simp
29db49c4d8bd067e948f4578c6085982f26148da
2c096fdfecf64e46ea7bc6ce5521f142b5926864
/src/Lean/Data/JsonRpc.lean
2121b62419dae371faa72d5b7bf07fa3d6073ca3
[ "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
Kha/lean4
1005785d2c8797ae266a303968848e5f6ce2fe87
b99e11346948023cd6c29d248cd8f3e3fb3474cf
refs/heads/master
1,693,355,498,027
1,669,080,461,000
1,669,113,138,000
184,748,176
0
0
Apache-2.0
1,665,995,520,000
1,556,884,930,000
Lean
UTF-8
Lean
false
false
13,639
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 Lean.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.JsonRpc open Json /-- In JSON-RPC, each request from the client editor to the language server comes with a request id so that the corresponding response can be identified or cancelled. -/ inductive RequestID where | str (s : String) | num (n : JsonNumber) | null deriving Inhabited, BEq, Ord instance : OfNat RequestID n := ⟨RequestID.num n⟩ instance : ToString RequestID where toString | RequestID.str s => s!"\"{s}\"" | RequestID.num n => toString n | RequestID.null => "null" /-- Error codes defined by [JSON-RPC](https://www.jsonrpc.org/specification#error_object) and [LSP](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#errorCodes). -/ inductive ErrorCode where /-- Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text. -/ | parseError /-- The JSON sent is not a valid Request object. -/ | invalidRequest /-- The method does not exist / is not available. -/ | methodNotFound /-- Invalid method parameter(s). -/ | invalidParams /-- Internal JSON-RPC error. -/ | internalError /-- Error code indicating that a server received a notification or request before the server has received the `initialize` request. -/ | serverNotInitialized | unknownErrorCode -- LSP-specific codes below. /-- The server detected that the content of a document got modified outside normal conditions. A server should NOT send this error code if it detects a content change in it unprocessed messages. The result even computed on an older state might still be useful for the client. If a client decides that a result is not of any use anymore the client should cancel the request. -/ | contentModified /-- The client has canceled a request and a server as detected the cancel. -/ | requestCancelled -- Lean-specific codes below. | rpcNeedsReconnect | workerExited | workerCrashed deriving Inhabited, BEq instance : FromJson ErrorCode := ⟨fun | num (-32700 : Int) => return ErrorCode.parseError | num (-32600 : Int) => return ErrorCode.invalidRequest | num (-32601 : Int) => return ErrorCode.methodNotFound | num (-32602 : Int) => return ErrorCode.invalidParams | num (-32603 : Int) => return ErrorCode.internalError | num (-32002 : Int) => return ErrorCode.serverNotInitialized | num (-32001 : Int) => return ErrorCode.unknownErrorCode | num (-32801 : Int) => return ErrorCode.contentModified | num (-32800 : Int) => return ErrorCode.requestCancelled | num (-32900 : Int) => return ErrorCode.rpcNeedsReconnect | num (-32901 : Int) => return ErrorCode.workerExited | num (-32902 : Int) => return ErrorCode.workerCrashed | _ => throw "expected error code"⟩ instance : ToJson ErrorCode := ⟨fun | ErrorCode.parseError => (-32700 : Int) | ErrorCode.invalidRequest => (-32600 : Int) | ErrorCode.methodNotFound => (-32601 : Int) | ErrorCode.invalidParams => (-32602 : Int) | ErrorCode.internalError => (-32603 : Int) | ErrorCode.serverNotInitialized => (-32002 : Int) | ErrorCode.unknownErrorCode => (-32001 : Int) | ErrorCode.contentModified => (-32801 : Int) | ErrorCode.requestCancelled => (-32800 : Int) | ErrorCode.rpcNeedsReconnect => (-32900 : Int) | ErrorCode.workerExited => (-32901 : Int) | ErrorCode.workerCrashed => (-32902 : Int)⟩ /-- A JSON-RPC message. Uses separate constructors for notifications and errors because client and server behavior is expected to be wildly different for both. -/ inductive Message where /-- A request message to describe a request between the client and the server. Every processed request must send a response back to the sender of the request. -/ | request (id : RequestID) (method : String) (params? : Option Structured) /-- A notification message. A processed notification message must not send a response back. They work like events. -/ | notification (method : String) (params? : Option Structured) /-- A Response Message sent as a result of a request. -/ | response (id : RequestID) (result : Json) /-- A non-successful response. -/ | responseError (id : RequestID) (code : ErrorCode) (message : String) (data? : Option Json) def Batch := Array Message /-- Generic version of `Message.request`. A request message to describe a request between the client and the server. Every processed request must send a response back to the sender of the request. - [LSP](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#requestMessage) - [JSON-RPC](https://www.jsonrpc.org/specification#request_object) -/ structure Request (α : Type u) where id : RequestID method : String param : α deriving Inhabited, BEq instance [ToJson α] : Coe (Request α) Message := ⟨fun r => Message.request r.id r.method (toStructured? r.param).toOption⟩ /-- Generic version of `Message.notification`. A notification message. A processed notification message must not send a response back. They work like events. - [JSON-RPC](https://www.jsonrpc.org/specification#notification) - [LSP](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#notificationMessage). -/ structure Notification (α : Type u) where method : String param : α deriving Inhabited, BEq instance [ToJson α] : Coe (Notification α) Message := ⟨fun r => Message.notification r.method (toStructured? r.param).toOption⟩ /-- Generic version of `Message.response`. A Response Message sent as a result of a request. If a request doesn’t provide a result value the receiver of a request still needs to return a response message to conform to the JSON-RPC specification. The result property of the ResponseMessage should be set to null in this case to signal a successful request. References: - [JSON-RPC](https://www.jsonrpc.org/specification#response_object) - [LSP](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#responseMessage) -/ structure Response (α : Type u) where id : RequestID result : α deriving Inhabited, BEq instance [ToJson α] : Coe (Response α) Message := ⟨fun r => Message.response r.id (toJson r.result)⟩ /-- Generic version of `Message.responseError`. References: - [JSON-RPC](https://www.jsonrpc.org/specification#error_object) - [LSP](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#responseError). -/ structure ResponseError (α : Type u) where id : RequestID code : ErrorCode /-- A string providing a short description of the error. -/ message : String /-- A primitive or structured value that contains additional information about the error. Can be omitted. -/ data? : Option α := none deriving Inhabited, BEq instance [ToJson α] : Coe (ResponseError α) Message := ⟨fun r => Message.responseError r.id r.code r.message (r.data?.map toJson)⟩ instance : Coe String RequestID := ⟨RequestID.str⟩ instance : Coe JsonNumber RequestID := ⟨RequestID.num⟩ private def RequestID.lt : RequestID → RequestID → Bool | RequestID.str a, RequestID.str b => a < b | RequestID.num a, RequestID.num b => a < b | RequestID.null, RequestID.num _ => true | RequestID.null, RequestID.str _ => true | RequestID.num _, RequestID.str _ => true | _, _ /- str < *, num < null, null < null -/ => false private def RequestID.ltProp : LT RequestID := ⟨fun a b => RequestID.lt a b = true⟩ instance : LT RequestID := RequestID.ltProp instance (a b : RequestID) : Decidable (a < b) := inferInstanceAs (Decidable (RequestID.lt a b = true)) instance : FromJson RequestID := ⟨fun j => match j with | str s => return RequestID.str s | num n => return RequestID.num n | _ => throw "a request id needs to be a number or a string"⟩ instance : ToJson RequestID := ⟨fun rid => match rid with | RequestID.str s => s | RequestID.num n => num n | RequestID.null => null⟩ instance : ToJson 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?⟩ ]⟩ instance : FromJson Message where fromJson? j := do let "2.0" ← j.getObjVal? "jsonrpc" | throw "only version 2.0 of JSON RPC is supported" (do let id ← j.getObjValAs? RequestID "id" let method ← j.getObjValAs? String "method" let params? := j.getObjValAs? Structured "params" pure (Message.request id method params?.toOption)) <|> (do let method ← j.getObjValAs? String "method" let params? := j.getObjValAs? Structured "params" pure (Message.notification method params?.toOption)) <|> (do let id ← j.getObjValAs? RequestID "id" let result ← j.getObjVal? "result" pure (Message.response id result)) <|> (do let id ← j.getObjValAs? RequestID "id" let err ← j.getObjVal? "error" let code ← err.getObjValAs? ErrorCode "code" let message ← err.getObjValAs? String "message" let data? := err.getObjVal? "data" pure (Message.responseError id code message data?.toOption)) -- TODO(WN): temporary until we have deriving FromJson instance [FromJson α] : FromJson (Notification α) where fromJson? j := do let msg : Message ← fromJson? j if let Message.notification method params? := msg then let params := params? let param : α ← fromJson? (toJson params) pure $ ⟨method, param⟩ else throw "not a notfication" end Lean.JsonRpc namespace IO.FS.Stream open Lean open Lean.JsonRpc section def readMessage (h : FS.Stream) (nBytes : Nat) : IO Message := do let j ← h.readJson nBytes match fromJson? j with | Except.ok m => pure m | Except.error inner => throw $ userError s!"JSON '{j.compress}' did not have the format of a JSON-RPC message.\n{inner}" def readRequestAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α) [FromJson α] : IO (Request α) := do let m ← h.readMessage nBytes match m with | Message.request id method params? => if method = expectedMethod then let j := toJson params? match fromJson? j with | Except.ok v => pure ⟨id, expectedMethod, v⟩ | Except.error inner => throw $ userError s!"Unexpected param '{j.compress}' for method '{expectedMethod}'\n{inner}" else throw $ userError s!"Expected method '{expectedMethod}', got method '{method}'" | _ => throw $ userError s!"Expected JSON-RPC request, got: '{(toJson m).compress}'" def readNotificationAs (h : FS.Stream) (nBytes : Nat) (expectedMethod : String) (α) [FromJson α] : IO (Notification α) := do let m ← h.readMessage nBytes match m with | Message.notification method params? => if method = expectedMethod then let j := toJson params? match fromJson? j with | Except.ok v => pure ⟨expectedMethod, v⟩ | Except.error inner => throw $ userError s!"Unexpected param '{j.compress}' for method '{expectedMethod}'\n{inner}" else throw $ userError s!"Expected method '{expectedMethod}', got method '{method}'" | _ => throw $ userError s!"Expected JSON-RPC notification, got: '{(toJson m).compress}'" partial def readResponseAs (h : FS.Stream) (nBytes : Nat) (expectedID : RequestID) (α) [FromJson α] : IO (Response α) := do let m ← h.readMessage nBytes match m with | Message.response id result => if id == expectedID then match fromJson? result with | Except.ok v => pure ⟨expectedID, v⟩ | Except.error inner => throw $ userError s!"Unexpected result '{result.compress}'\n{inner}" else throw $ userError s!"Expected id {expectedID}, got id {id}" | Message.notification .. => readResponseAs h nBytes expectedID α | _ => throw $ userError s!"Expected JSON-RPC response, got: '{(toJson m).compress}'" end section variable [ToJson α] def writeMessage (h : FS.Stream) (m : Message) : IO Unit := h.writeJson (toJson m) def writeRequest (h : FS.Stream) (r : Request α) : IO Unit := h.writeMessage r def writeNotification (h : FS.Stream) (n : Notification α) : IO Unit := h.writeMessage n def writeResponse (h : FS.Stream) (r : Response α) : IO Unit := h.writeMessage r def writeResponseError (h : FS.Stream) (e : ResponseError Unit) : IO Unit := h.writeMessage (Message.responseError e.id e.code e.message none) def writeResponseErrorWithData (h : FS.Stream) (e : ResponseError α) : IO Unit := h.writeMessage e end end IO.FS.Stream
2a27bfa5de1a3c47bd71e654efcfc7559e9ffbca
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/tactic/mk_iff_of_inductive_prop.lean
cff6aba96cb5e135ac367b7ce3ce6af117f18dee
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
6,893
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 Generation function for iff rules for inductives, like for `list.chain`: ∀{α : Type*} (R : α → α → Prop) (a : α) (l : list α), chain R a l ↔ l = [] ∨ ∃{b : α} {l' : list α}, R a b ∧ chain R b l ∧ l = b :: l' -/ namespace tactic open tactic expr meta def mk_iff (e₀ : expr) (e₁ : expr) : expr := `(%%e₀ ↔ %%e₁) meta def select : ℕ → ℕ → tactic unit | 0 0 := skip | 0 (n + 1) := left >> skip | (m + 1) (n + 1) := right >> select m n | (n + 1) 0 := failure /-- `compact_relation bs as_ps`: Produce a relation of the form: R as := ∃ bs, Λ_i a_i = p_i[bs] This relation is user visible, so we compact it by removing each `b_j` where a `p_i = b_j`, and hence `a_i = b_j`. We need to take care when there are `p_i` and `p_j` with `p_i = p_j = b_k`. TODO: this is copied from Lean's `coinductive_predicates.lean`, export it there. -/ private meta def compact_relation : list expr → list (expr × expr) → list (option expr) × list (expr × expr) | [] ps := ([], ps) | (b :: bs) ps := match ps.span (λap:expr × expr, ¬ ap.2 =ₐ b) with | (_, []) := let (bs, ps) := compact_relation bs ps in (b::bs, ps) | (ps₁, list.cons (a, _) ps₂) := let i := a.instantiate_local b.local_uniq_name, (bs, ps) := compact_relation (bs.map i) ((ps₁ ++ ps₂).map (λ⟨a, p⟩, (a, i p))) in (none :: bs, ps) end meta def constr_to_prop (univs : list level) (g : list expr) (idxs : list expr) (c : name) : tactic ((list (option expr) × (expr ⊕ ℕ)) × expr) := do e ← get_env, decl ← get_decl c, some type' ← return $ decl.instantiate_type_univ_params univs, type ← drop_pis g type', (args, res) ← mk_local_pis type, let idxs_inst := res.get_app_args.drop g.length, let (bs, eqs) := compact_relation args (idxs.zip idxs_inst), let bs' := bs.filter_map id, eqs ← eqs.mmap (λ⟨idx, inst⟩, do let ty := idx.local_type, inst_ty ← infer_type inst, sort u ← infer_type ty, (is_def_eq ty inst_ty >> return ((const `eq [u] : expr) ty idx inst)) <|> return ((const `heq [u] : expr) ty idx inst_ty inst)), (n, r) ← match bs', eqs with | [], [] := return (sum.inr 0, mk_true) | _, [] := do let t : expr := bs'.ilast.local_type, sort l ← infer_type t, if l = level.zero then do r ← mk_exists_lst bs'.init t, return (sum.inl bs'.ilast, r) else do r ← mk_exists_lst bs' mk_true, return (sum.inr 0, r) | _, _ := do r ← mk_exists_lst bs' (mk_and_lst eqs), return (sum.inr eqs.length, r) end, return ((bs, n), r) private meta def to_cases (s : list $ list (option expr) × (expr ⊕ ℕ)) : tactic unit := do h ← intro1, i ← induction h, focus ((s.zip i).enum.map $ λ⟨p, (shape, t), _, vars, _⟩, do let si := (shape.zip vars).filter_map (λ⟨c, v⟩, c >>= λ _, some v), select p (s.length - 1), match t with | sum.inl e := do si.init.mmap' existsi, some v ← return $ vars.nth (shape.length - 1), exact v | sum.inr n := do si.mmap' existsi, iterate_exactly (n - 1) (split >> constructor >> skip) >> constructor >> skip end, done), done private def list_option_merge {α : Type*} {β : Type*} : list (option α) → list β → list (option β) | [] _ := [] | (none :: xs) ys := none :: list_option_merge xs ys | (some a :: xs) (y :: ys) := some y :: list_option_merge xs ys | (some a :: xs) [] := [] private meta def to_inductive (cs : list name) (gs : list expr) (s : list (list (option expr) × (expr ⊕ ℕ))) (h : expr) : tactic unit := match s.length with | 0 := induction h >> skip | (n + 1) := do r ← elim_gen_sum n h, focus ((cs.zip (r.zip s)).map $ λ⟨constr_name, h, bs, e⟩, do let n := (bs.filter_map id).length, match e with | sum.inl e := elim_gen_prod (n - 1) h [] >> skip | sum.inr 0 := do (hs, h) ← elim_gen_prod n h [], clear h | sum.inr (e + 1) := do (hs, h) ← elim_gen_prod n h [], (es, eq) ← elim_gen_prod e h [], let es := es ++ [eq], /- `es.mmap' subst`: fails when we have dependent equalities (heq). `subst will change the dependent hypotheses, so that the uniq local names in `es` are wrong afterwards. Instead we revert them and pull them out one by one -/ revert_lst es, es.mmap' (λ_, intro1 >>= subst) end, ctxt ← local_context, let gs := ctxt.take gs.length, let hs := (ctxt.reverse.take n).reverse, let m := gs.map some ++ list_option_merge bs hs, args ← m.mmap (λa, match a with some v := return v | none := mk_mvar end), c ← mk_const constr_name, exact (c.mk_app args), done), done end /-- `mk_iff_of_inductive_prop i r` makes a iff rule for the inductively defined proposition `i`. The new rule `r` has the shape `∀ps is, i as ↔ ⋁_j, ∃cs, is = cs`, where `ps` are the type parameters, `is` are the indices, `j` ranges over all possible constructors, the `cs` are the parameters for each constructors, the equalities `is = cs` are the instantiations for each constructor for each of the indices to the inductive type `i`. In each case, we remove constructor parameters (i.e. `cs`) when the corresponding equality would be just `c = i` for some index `i`. For example: `mk_iff_of_inductive_prop` on `list.chain` produces: ∀{α : Type*} (R : α → α → Prop) (a : α) (l : list α), chain R a l ↔ l = [] ∨ ∃{b : α} {l' : list α}, R a b ∧ chain R b l ∧ l = b :: l' -/ meta def mk_iff_of_inductive_prop (i : name) (r : name) : tactic unit := do e ← get_env, guard (e.is_inductive i), let constrs := e.constructors_of i, let params := e.inductive_num_params i, let indices := e.inductive_num_indices i, let rec := match e.recursor_of i with some rec := rec | none := i.append `rec end, decl ← get_decl i, let type := decl.type, let univ_names := decl.univ_params, let univs := univ_names.map level.param, /- we use these names for our universe parameters, maybe we should construct a copy of them using uniq_name -/ (g, `(Prop)) ← mk_local_pis type | fail "Inductive type is not a proposition", let lhs := (const i univs).mk_app g, shape_rhss ← constrs.mmap (constr_to_prop univs (g.take params) (g.drop params)), let shape := shape_rhss.map prod.fst, let rhss := shape_rhss.map prod.snd, add_theorem_by r univ_names ((mk_iff lhs (mk_or_lst rhss)).pis g) (do gs ← intro_lst (g.map local_pp_name), split, focus [to_cases shape, intro1 >>= to_inductive constrs (gs.take params) shape]), skip end tactic
1e44059248455bf5127e8714c17fe490bdf847e2
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/geometry/manifold/smooth_manifold_with_corners.lean
9aa609e9d202728b6fc5c0f898b1352bf5ef3f5b
[ "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
41,634
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.times_cont_diff import geometry.manifold.charted_space /-! # Smooth manifolds (possibly with boundary or corners) A smooth manifold is a manifold modelled on a normed vector space, or a subset like a half-space (to get manifolds with boundaries) for which the changes of coordinates are smooth maps. We define a model with corners as a map `I : H → E` embedding nicely the topological space `H` in the vector space `E` (or more precisely as a structure containing all the relevant properties). Given such a model with corners `I` on `(E, H)`, we define the groupoid of local homeomorphisms of `H` which are smooth when read in `E` (for any regularity `n : with_top ℕ`). With this groupoid at hand and the general machinery of charted spaces, we thus get the notion of `C^n` manifold with respect to any model with corners `I` on `(E, H)`. We also introduce a specific type class for `C^∞` manifolds as these are the most commonly used. ## Main definitions * `model_with_corners 𝕜 E H` : a structure containing informations on the way a space `H` embeds in a model vector space E over the field `𝕜`. This is all that is needed to define a smooth manifold with model space `H`, and model vector space `E`. * `model_with_corners_self 𝕜 E` : trivial model with corners structure on the space `E` embedded in itself by the identity. * `times_cont_diff_groupoid n I` : when `I` is a model with corners on `(𝕜, E, H)`, this is the groupoid of local homeos of `H` which are of class `C^n` over the normed field `𝕜`, when read in `E`. * `smooth_manifold_with_corners I M` : a type class saying that the charted space `M`, modelled on the space `H`, has `C^∞` changes of coordinates with respect to the model with corners `I` on `(𝕜, E, H)`. This type class is just a shortcut for `has_groupoid M (times_cont_diff_groupoid ∞ I)`. * `ext_chart_at I x`: in a smooth manifold with corners with the model `I` on `(E, H)`, the charts take values in `H`, but often we may want to use their `E`-valued version, obtained by composing the charts with `I`. Since the target is in general not open, we can not register them as local homeomorphisms, but we register them as local equivs. `ext_chart_at I x` is the canonical such local equiv around `x`. As specific examples of models with corners, we define (in the file `real_instances.lean`) * `model_with_corners_self ℝ (euclidean_space (fin n))` for the model space used to define `n`-dimensional real manifolds without boundary (with notation `𝓡 n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_half_space n)` for the model space used to define `n`-dimensional real manifolds with boundary (with notation `𝓡∂ n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_quadrant n)` for the model space used to define `n`-dimensional real manifolds with corners With these definitions at hand, to invoke an `n`-dimensional real manifold without boundary, one could use `variables {n : ℕ} {M : Type*} [topological_space M] [charted_space (euclidean_space (fin n)) M] [smooth_manifold_with_corners (𝓡 n) M]`. However, this is not the recommended way: a theorem proved using this assumption would not apply for instance to the tangent space of such a manifold, which is modelled on `(euclidean_space (fin n)) × (euclidean_space (fin n))` and not on `euclidean_space (fin (2 * n))`! In the same way, it would not apply to product manifolds, modelled on `(euclidean_space (fin n)) × (euclidean_space (fin m))`. The right invocation does not focus on one specific construction, but on all constructions sharing the right properties, like `variables {E : Type*} [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] {I : model_with_corners ℝ E E} [I.boundaryless] {M : Type*} [topological_space M] [charted_space E M] [smooth_manifold_with_corners I M]` Here, `I.boundaryless` is a typeclass property ensuring that there is no boundary (this is for instance the case for `model_with_corners_self`, or products of these). Note that one could consider as a natural assumption to only use the trivial model with corners `model_with_corners_self ℝ E`, but again in product manifolds the natural model with corners will not be this one but the product one (and they are not defeq as `(λp : E × F, (p.1, p.2))` is not defeq to the identity). So, it is important to use the above incantation to maximize the applicability of theorems. ## Implementation notes We want to talk about manifolds modelled on a vector space, but also on manifolds with boundary, modelled on a half space (or even manifolds with corners). For the latter examples, we still want to define smooth functions, tangent bundles, and so on. As smooth functions are well defined on vector spaces or subsets of these, one could take for model space a subtype of a vector space. With the drawback that the whole vector space itself (which is the most basic example) is not directly a subtype of itself: the inclusion of `univ : set E` in `set E` would show up in the definition, instead of `id`. A good abstraction covering both cases it to have a vector space `E` (with basic example the Euclidean space), a model space `H` (with basic example the upper half space), and an embedding of `H` into `E` (which can be the identity for `H = E`, or `subtype.val` for manifolds with corners). We say that the pair `(E, H)` with their embedding is a model with corners, and we encompass all the relevant properties (in particular the fact that the image of `H` in `E` should have unique differentials) in the definition of `model_with_corners`. We concentrate on `C^∞` manifolds: all the definitions work equally well for `C^n` manifolds, but later on it is a pain to carry all over the smoothness parameter, especially when one wants to deal with `C^k` functions as there would be additional conditions `k ≤ n` everywhere. Since one deals almost all the time with `C^∞` (or analytic) manifolds, this seems to be a reasonable choice that one could revisit later if needed. `C^k` manifolds are still available, but they should be called using `has_groupoid M (times_cont_diff_groupoid k I)` where `I` is the model with corners. I have considered using the model with corners `I` as a typeclass argument, possibly `out_param`, to get lighter notations later on, but it did not turn out right, as on `E × F` there are two natural model with corners, the trivial (identity) one, and the product one, and they are not defeq and one needs to indicate to Lean which one we want to use. This means that when talking on objects on manifolds one will most often need to specify the model with corners one is using. For instance, the tangent bundle will be `tangent_bundle I M` and the derivative will be `mfderiv I I' f`, instead of the more natural notations `tangent_bundle 𝕜 M` and `mfderiv 𝕜 f` (the field has to be explicit anyway, as some manifolds could be considered both as real and complex manifolds). -/ noncomputable theory universes u v w u' v' w' open set filter open_locale manifold filter topological_space localized "notation `∞` := (⊤ : with_top ℕ)" in manifold section model_with_corners /-! ### Models with corners. -/ /-- A structure containing informations on the way a space `H` embeds in a model vector space `E` over the field `𝕜`. This is all what is needed to define a smooth manifold with model space `H`, and model vector space `E`. -/ @[nolint has_inhabited_instance] structure model_with_corners (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] (H : Type*) [topological_space H] extends local_equiv H E := (source_eq : source = univ) (unique_diff' : unique_diff_on 𝕜 to_local_equiv.target) (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') (continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity') attribute [simp, mfld_simps] model_with_corners.source_eq /-- A vector space is a model with corners. -/ def model_with_corners_self (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] : model_with_corners 𝕜 E E := { to_local_equiv := local_equiv.refl E, source_eq := rfl, unique_diff' := unique_diff_on_univ, continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id } localized "notation `𝓘(` 𝕜 `, ` E `)` := model_with_corners_self 𝕜 E" in manifold localized "notation `𝓘(` 𝕜 `)` := model_with_corners_self 𝕜 𝕜" in manifold section variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) namespace model_with_corners instance : has_coe_to_fun (model_with_corners 𝕜 E H) := ⟨_, λ e, e.to_fun⟩ /-- The inverse to a model with corners, only registered as a local equiv. -/ protected def symm : local_equiv E H := I.to_local_equiv.symm /- Register a few lemmas to make sure that `simp` puts expressions in normal form -/ @[simp, mfld_simps] lemma to_local_equiv_coe : (I.to_local_equiv : H → E) = I := rfl @[simp, mfld_simps] lemma mk_coe (e : local_equiv H E) (a b c d) : ((model_with_corners.mk e a b c d : model_with_corners 𝕜 E H) : H → E) = (e : H → E) := rfl @[simp, mfld_simps] lemma to_local_equiv_coe_symm : (I.to_local_equiv.symm : E → H) = I.symm := rfl @[simp, mfld_simps] lemma mk_symm (e : local_equiv H E) (a b c d) : (model_with_corners.mk e a b c d : model_with_corners 𝕜 E H).symm = e.symm := rfl @[continuity] protected lemma continuous : continuous I := I.continuous_to_fun protected lemma continuous_at {x} : continuous_at I x := I.continuous.continuous_at protected lemma continuous_within_at {s x} : continuous_within_at I s x := I.continuous_at.continuous_within_at @[continuity] lemma continuous_symm : continuous I.symm := I.continuous_inv_fun lemma continuous_at_symm {x} : continuous_at I.symm x := I.continuous_symm.continuous_at lemma continuous_within_at_symm {s x} : continuous_within_at I.symm s x := I.continuous_symm.continuous_within_at @[simp, mfld_simps] lemma target_eq : I.target = range (I : H → E) := by { rw [← image_univ, ← I.source_eq], exact (I.to_local_equiv.image_source_eq_target).symm } protected lemma unique_diff : unique_diff_on 𝕜 (range I) := I.target_eq ▸ I.unique_diff' @[simp, mfld_simps] protected lemma left_inv (x : H) : I.symm (I x) = x := by { refine I.left_inv' _, simp } protected lemma left_inverse : function.left_inverse I.symm I := I.left_inv @[simp, mfld_simps] lemma symm_comp_self : I.symm ∘ I = id := I.left_inverse.comp_eq_id protected lemma right_inv_on : right_inv_on I.symm I (range I) := I.left_inverse.right_inv_on_range @[simp, mfld_simps] protected lemma right_inv {x : E} (hx : x ∈ range I) : I (I.symm x) = x := I.right_inv_on hx protected lemma image_eq (s : set H) : I '' s = I.symm ⁻¹' s ∩ range I := begin refine (I.to_local_equiv.image_eq_target_inter_inv_preimage _).trans _, { rw I.source_eq, exact subset_univ _ }, { rw [inter_comm, I.target_eq, I.to_local_equiv_coe_symm] } end protected lemma closed_embedding : closed_embedding I := I.left_inverse.closed_embedding I.continuous_symm I.continuous lemma closed_range : is_closed (range I) := I.closed_embedding.closed_range lemma map_nhds_eq (x : H) : map I (𝓝 x) = 𝓝[range I] (I x) := I.closed_embedding.to_embedding.map_nhds_eq x lemma image_mem_nhds_within {x : H} {s : set H} (hs : s ∈ 𝓝 x) : I '' s ∈ 𝓝[range I] (I x) := I.map_nhds_eq x ▸ image_mem_map hs lemma symm_map_nhds_within_range (x : H) : map I.symm (𝓝[range I] (I x)) = 𝓝 x := by rw [← I.map_nhds_eq, map_map, I.symm_comp_self, map_id] lemma unique_diff_preimage {s : set H} (hs : is_open s) : unique_diff_on 𝕜 (I.symm ⁻¹' s ∩ range I) := by { rw inter_comm, exact I.unique_diff.inter (hs.preimage I.continuous_inv_fun) } lemma unique_diff_preimage_source {β : Type*} [topological_space β] {e : local_homeomorph H β} : unique_diff_on 𝕜 (I.symm ⁻¹' (e.source) ∩ range I) := I.unique_diff_preimage e.open_source lemma unique_diff_at_image {x : H} : unique_diff_within_at 𝕜 (range I) (I x) := I.unique_diff _ (mem_range_self _) protected lemma locally_compact [locally_compact_space E] (I : model_with_corners 𝕜 E H) : locally_compact_space H := begin have : ∀ (x : H), (𝓝 x).has_basis (λ s, s ∈ 𝓝 (I x) ∧ is_compact s) (λ s, I.symm '' (s ∩ range ⇑I)), { intro x, rw ← I.symm_map_nhds_within_range, exact ((compact_basis_nhds (I x)).inf_principal _).map _ }, refine locally_compact_space_of_has_basis this _, rintro x s ⟨-, hsc⟩, exact (hsc.inter_right I.closed_range).image I.continuous_symm end open topological_space protected lemma second_countable_topology [second_countable_topology E] (I : model_with_corners 𝕜 E H) : second_countable_topology H := I.closed_embedding.to_embedding.second_countable_topology end model_with_corners section variables (𝕜 E) /-- In the trivial model with corners, the associated local equiv is the identity. -/ @[simp, mfld_simps] lemma model_with_corners_self_local_equiv : (𝓘(𝕜, E)).to_local_equiv = local_equiv.refl E := rfl @[simp, mfld_simps] lemma model_with_corners_self_coe : (𝓘(𝕜, E) : E → E) = id := rfl @[simp, mfld_simps] lemma model_with_corners_self_coe_symm : (𝓘(𝕜, E).symm : E → E) = id := rfl end end section model_with_corners_prod /-- Given two model_with_corners `I` on `(E, H)` and `I'` on `(E', H')`, we define the model with corners `I.prod I'` on `(E × E', model_prod H H')`. This appears in particular for the manifold structure on the tangent bundle to a manifold modelled on `(E, H)`: it will be modelled on `(E × E, H × E)`. See note [Manifold type tags] for explanation about `model_prod H H'` vs `H × H'`. -/ def model_with_corners.prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') : model_with_corners 𝕜 (E × E') (model_prod H H') := { to_fun := λ x, (I x.1, I' x.2), inv_fun := λ x, (I.symm x.1, I'.symm x.2), source := {x | x.1 ∈ I.source ∧ x.2 ∈ I'.source}, source_eq := by simp only [set_of_true] with mfld_simps, unique_diff' := I.unique_diff'.prod I'.unique_diff', continuous_to_fun := I.continuous_to_fun.prod_map I'.continuous_to_fun, continuous_inv_fun := I.continuous_inv_fun.prod_map I'.continuous_inv_fun, .. I.to_local_equiv.prod I'.to_local_equiv } /-- Given a finite family of `model_with_corners` `I i` on `(E i, H i)`, we define the model with corners `pi I` on `(Π i, E i, model_pi H)`. See note [Manifold type tags] for explanation about `model_pi H`. -/ def model_with_corners.pi {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {ι : Type v} [fintype ι] {E : ι → Type w} [Π i, normed_group (E i)] [Π i, normed_space 𝕜 (E i)] {H : ι → Type u'} [Π i, topological_space (H i)] (I : Π i, model_with_corners 𝕜 (E i) (H i)) : model_with_corners 𝕜 (Π i, E i) (model_pi H) := { to_local_equiv := local_equiv.pi (λ i, (I i).to_local_equiv), source_eq := by simp only [set.pi_univ] with mfld_simps, unique_diff' := unique_diff_on.pi ι E _ _ (λ i _, (I i).unique_diff'), continuous_to_fun := continuous_pi $ λ i, (I i).continuous.comp (continuous_apply i), continuous_inv_fun := continuous_pi $ λ i, (I i).continuous_symm.comp (continuous_apply i) } /-- Special case of product model with corners, which is trivial on the second factor. This shows up as the model to tangent bundles. -/ @[reducible] def model_with_corners.tangent {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) : model_with_corners 𝕜 (E × E) (model_prod H E) := I.prod (𝓘(𝕜, E)) variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {F : Type*} [normed_group F] [normed_space 𝕜 F] {F' : Type*} [normed_group F'] [normed_space 𝕜 F'] {H : Type*} [topological_space H] {H' : Type*} [topological_space H'] {G : Type*} [topological_space G] {G' : Type*} [topological_space G'] {I : model_with_corners 𝕜 E H} {J : model_with_corners 𝕜 F G} @[simp, mfld_simps] lemma model_with_corners_prod_to_local_equiv : (I.prod J).to_local_equiv = I.to_local_equiv.prod (J.to_local_equiv) := rfl @[simp, mfld_simps] lemma model_with_corners_prod_coe (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : (I.prod I' : _ × _ → _ × _) = prod.map I I' := rfl @[simp, mfld_simps] lemma model_with_corners_prod_coe_symm (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : ((I.prod I').symm : _ × _ → _ × _) = prod.map I.symm I'.symm := rfl end model_with_corners_prod section boundaryless /-- Property ensuring that the model with corners `I` defines manifolds without boundary. -/ class model_with_corners.boundaryless {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) : Prop := (range_eq_univ : range I = univ) /-- The trivial model with corners has no boundary -/ instance model_with_corners_self_boundaryless (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] : (model_with_corners_self 𝕜 E).boundaryless := ⟨by simp⟩ /-- If two model with corners are boundaryless, their product also is -/ instance model_with_corners.range_eq_univ_prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) [I.boundaryless] {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') [I'.boundaryless] : (I.prod I').boundaryless := begin split, dsimp [model_with_corners.prod, model_prod], rw [← prod_range_range_eq, model_with_corners.boundaryless.range_eq_univ, model_with_corners.boundaryless.range_eq_univ, univ_prod_univ] end end boundaryless section times_cont_diff_groupoid /-! ### Smooth functions on models with corners -/ variables {m n : with_top ℕ} {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] variable (n) /-- Given a model with corners `(E, H)`, we define the groupoid of `C^n` transformations of `H` as the maps that are `C^n` when read in `E` through `I`. -/ def times_cont_diff_groupoid : structure_groupoid H := pregroupoid.groupoid { property := λf s, times_cont_diff_on 𝕜 n (I ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I), comp := λf g u v hf hg hu hv huv, begin have : I ∘ (g ∘ f) ∘ I.symm = (I ∘ g ∘ I.symm) ∘ (I ∘ f ∘ I.symm), by { ext x, simp }, rw this, apply times_cont_diff_on.comp hg _, { rintros x ⟨hx1, hx2⟩, simp only with mfld_simps at ⊢ hx1, exact hx1.2 }, { refine hf.mono _, rintros x ⟨hx1, hx2⟩, exact ⟨hx1.1, hx2⟩ } end, id_mem := begin apply times_cont_diff_on.congr (times_cont_diff_id.times_cont_diff_on), rintros x ⟨hx1, hx2⟩, rcases mem_range.1 hx2 with ⟨y, hy⟩, rw ← hy, simp only with mfld_simps, end, locality := λf u hu H, begin apply times_cont_diff_on_of_locally_times_cont_diff_on, rintros y ⟨hy1, hy2⟩, rcases mem_range.1 hy2 with ⟨x, hx⟩, rw ← hx at ⊢ hy1, simp only with mfld_simps at ⊢ hy1, rcases H x hy1 with ⟨v, v_open, xv, hv⟩, have : ((I.symm ⁻¹' (u ∩ v)) ∩ (range I)) = ((I.symm ⁻¹' u) ∩ (range I) ∩ I.symm ⁻¹' v), { rw [preimage_inter, inter_assoc, inter_assoc], congr' 1, rw inter_comm }, rw this at hv, exact ⟨I.symm ⁻¹' v, v_open.preimage I.continuous_symm, by simpa, hv⟩ end, congr := λf g u hu fg hf, begin apply hf.congr, rintros y ⟨hy1, hy2⟩, rcases mem_range.1 hy2 with ⟨x, hx⟩, rw ← hx at ⊢ hy1, simp only with mfld_simps at ⊢ hy1, rw fg _ hy1 end } variable {n} /-- Inclusion of the groupoid of `C^n` local diffeos in the groupoid of `C^m` local diffeos when `m ≤ n` -/ lemma times_cont_diff_groupoid_le (h : m ≤ n) : times_cont_diff_groupoid n I ≤ times_cont_diff_groupoid m I := begin rw [times_cont_diff_groupoid, times_cont_diff_groupoid], apply groupoid_of_pregroupoid_le, assume f s hfs, exact times_cont_diff_on.of_le hfs h end /-- The groupoid of `0`-times continuously differentiable maps is just the groupoid of all local homeomorphisms -/ lemma times_cont_diff_groupoid_zero_eq : times_cont_diff_groupoid 0 I = continuous_groupoid H := begin apply le_antisymm le_top, assume u hu, -- we have to check that every local homeomorphism belongs to `times_cont_diff_groupoid 0 I`, -- by unfolding its definition change u ∈ times_cont_diff_groupoid 0 I, rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid], simp only [times_cont_diff_on_zero], split, { apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous) _ (subset_univ _), apply continuous_on.comp u.continuous_to_fun I.continuous_symm.continuous_on (inter_subset_left _ _) }, { apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous) _ (subset_univ _), apply continuous_on.comp u.continuous_inv_fun I.continuous_inv_fun.continuous_on (inter_subset_left _ _) }, end variable (n) /-- An identity local homeomorphism belongs to the `C^n` groupoid. -/ lemma of_set_mem_times_cont_diff_groupoid {s : set H} (hs : is_open s) : local_homeomorph.of_set s hs ∈ times_cont_diff_groupoid n I := begin rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid], suffices h : times_cont_diff_on 𝕜 n (I ∘ I.symm) (I.symm ⁻¹' s ∩ range I), by simp [h], have : times_cont_diff_on 𝕜 n id (univ : set E) := times_cont_diff_id.times_cont_diff_on, exact this.congr_mono (λ x hx, by simp [hx.2]) (subset_univ _) end /-- The composition of a local homeomorphism from `H` to `M` and its inverse belongs to the `C^n` groupoid. -/ lemma symm_trans_mem_times_cont_diff_groupoid (e : local_homeomorph M H) : e.symm.trans e ∈ times_cont_diff_groupoid n I := begin have : e.symm.trans e ≈ local_homeomorph.of_set e.target e.open_target := local_homeomorph.trans_symm_self _, exact structure_groupoid.eq_on_source _ (of_set_mem_times_cont_diff_groupoid n I e.open_target) this end variables {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] /-- The product of two smooth local homeomorphisms is smooth. -/ lemma times_cont_diff_groupoid_prod {I : model_with_corners 𝕜 E H} {I' : model_with_corners 𝕜 E' H'} {e : local_homeomorph H H} {e' : local_homeomorph H' H'} (he : e ∈ times_cont_diff_groupoid ⊤ I) (he' : e' ∈ times_cont_diff_groupoid ⊤ I') : e.prod e' ∈ times_cont_diff_groupoid ⊤ (I.prod I') := begin cases he with he he_symm, cases he' with he' he'_symm, simp only at he he_symm he' he'_symm, split; simp only [local_equiv.prod_source, local_homeomorph.prod_to_local_equiv], { have h3 := times_cont_diff_on.prod_map he he', rw [← I.image_eq, ← I'.image_eq, set.prod_image_image_eq] at h3, rw ← (I.prod I').image_eq, exact h3, }, { have h3 := times_cont_diff_on.prod_map he_symm he'_symm, rw [← I.image_eq, ← I'.image_eq, set.prod_image_image_eq] at h3, rw ← (I.prod I').image_eq, exact h3, } end /-- The `C^n` groupoid is closed under restriction. -/ instance : closed_under_restriction (times_cont_diff_groupoid n I) := (closed_under_restriction_iff_id_le _).mpr begin apply structure_groupoid.le_iff.mpr, rintros e ⟨s, hs, hes⟩, apply (times_cont_diff_groupoid n I).eq_on_source' _ _ _ hes, exact of_set_mem_times_cont_diff_groupoid n I hs, end end times_cont_diff_groupoid end model_with_corners section smooth_manifold_with_corners /-! ### Smooth manifolds with corners -/ /-- Typeclass defining smooth manifolds with corners with respect to a model with corners, over a field `𝕜` and with infinite smoothness to simplify typeclass search and statements later on. -/ @[ancestor has_groupoid] class smooth_manifold_with_corners {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] extends has_groupoid M (times_cont_diff_groupoid ∞ I) : Prop lemma smooth_manifold_with_corners.mk' {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] [gr : has_groupoid M (times_cont_diff_groupoid ∞ I)] : smooth_manifold_with_corners I M := { ..gr } lemma smooth_manifold_with_corners_of_times_cont_diff_on {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] (h : ∀ (e e' : local_homeomorph M H), e ∈ atlas H M → e' ∈ atlas H M → times_cont_diff_on 𝕜 ⊤ (I ∘ (e.symm ≫ₕ e') ∘ I.symm) (I.symm ⁻¹' (e.symm ≫ₕ e').source ∩ range I)) : smooth_manifold_with_corners I M := { compatible := begin haveI : has_groupoid M (times_cont_diff_groupoid ∞ I) := has_groupoid_of_pregroupoid _ h, apply structure_groupoid.compatible, end } /-- For any model with corners, the model space is a smooth manifold -/ instance model_space_smooth {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} : smooth_manifold_with_corners I H := { .. has_groupoid_model_space _ _ } end smooth_manifold_with_corners namespace smooth_manifold_with_corners /- We restate in the namespace `smooth_manifolds_with_corners` some lemmas that hold for general charted space with a structure groupoid, avoiding the need to specify the groupoid `times_cont_diff_groupoid ∞ I` explicitly. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] /-- The maximal atlas of `M` for the smooth manifold with corners structure corresponding to the model with corners `I`. -/ def maximal_atlas := (times_cont_diff_groupoid ∞ I).maximal_atlas M variable {M} lemma mem_maximal_atlas_of_mem_atlas [smooth_manifold_with_corners I M] {e : local_homeomorph M H} (he : e ∈ atlas H M) : e ∈ maximal_atlas I M := structure_groupoid.mem_maximal_atlas_of_mem_atlas _ he lemma chart_mem_maximal_atlas [smooth_manifold_with_corners I M] (x : M) : chart_at H x ∈ maximal_atlas I M := structure_groupoid.chart_mem_maximal_atlas _ x variable {I} lemma compatible_of_mem_maximal_atlas {e e' : local_homeomorph M H} (he : e ∈ maximal_atlas I M) (he' : e' ∈ maximal_atlas I M) : e.symm.trans e' ∈ times_cont_diff_groupoid ∞ I := structure_groupoid.compatible_of_mem_maximal_atlas he he' /-- The product of two smooth manifolds with corners is naturally a smooth manifold with corners. -/ instance prod {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} (M : Type*) [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] (M' : Type*) [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] : smooth_manifold_with_corners (I.prod I') (M×M') := { compatible := begin rintros f g ⟨f1, f2, hf1, hf2, rfl⟩ ⟨g1, g2, hg1, hg2, rfl⟩, rw [local_homeomorph.prod_symm, local_homeomorph.prod_trans], have h1 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I) hf1 hg1, have h2 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I') hf2 hg2, exact times_cont_diff_groupoid_prod h1 h2, end } end smooth_manifold_with_corners lemma local_homeomorph.singleton_smooth_manifold_with_corners {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] (e : local_homeomorph M H) (h : e.source = set.univ) : @smooth_manifold_with_corners 𝕜 _ E _ _ H _ I M _ (e.singleton_charted_space h) := @smooth_manifold_with_corners.mk' _ _ _ _ _ _ _ _ _ _ (id _) $ e.singleton_has_groupoid h (times_cont_diff_groupoid ∞ I) lemma open_embedding.singleton_smooth_manifold_with_corners {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [nonempty M] {f : M → H} (h : open_embedding f) : @smooth_manifold_with_corners 𝕜 _ E _ _ H _ I M _ h.singleton_charted_space := (h.to_local_homeomorph f).singleton_smooth_manifold_with_corners I (by simp) namespace topological_space.opens open topological_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] (s : opens M) instance : smooth_manifold_with_corners I s := { ..s.has_groupoid (times_cont_diff_groupoid ∞ I) } end topological_space.opens section extended_charts open_locale topological_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] (x : M) {s t : set M} /-! ### Extended charts In a smooth manifold with corners, the model space is the space `H`. However, we will also need to use extended charts taking values in the model vector space `E`. These extended charts are not `local_homeomorph` as the target is not open in `E` in general, but we can still register them as `local_equiv`. -/ /-- The preferred extended chart on a manifold with corners around a point `x`, from a neighborhood of `x` to the model vector space. -/ @[simp, mfld_simps] def ext_chart_at (x : M) : local_equiv M E := (chart_at H x).to_local_equiv.trans I.to_local_equiv lemma ext_chart_at_coe : ⇑(ext_chart_at I x) = I ∘ chart_at H x := rfl lemma ext_chart_at_coe_symm : ⇑(ext_chart_at I x).symm = (chart_at H x).symm ∘ I.symm := rfl lemma ext_chart_at_source : (ext_chart_at I x).source = (chart_at H x).source := by rw [ext_chart_at, local_equiv.trans_source, I.source_eq, preimage_univ, inter_univ] lemma ext_chart_at_open_source : is_open (ext_chart_at I x).source := by { rw ext_chart_at_source, exact (chart_at H x).open_source } lemma mem_ext_chart_source : x ∈ (ext_chart_at I x).source := by simp only [ext_chart_at_source, mem_chart_source] lemma ext_chart_at_to_inv : (ext_chart_at I x).symm ((ext_chart_at I x) x) = x := (ext_chart_at I x).left_inv (mem_ext_chart_source I x) lemma ext_chart_at_source_mem_nhds' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : (ext_chart_at I x).source ∈ 𝓝 x' := is_open.mem_nhds (ext_chart_at_open_source I x) h lemma ext_chart_at_source_mem_nhds : (ext_chart_at I x).source ∈ 𝓝 x := ext_chart_at_source_mem_nhds' I x (mem_ext_chart_source I x) lemma ext_chart_at_source_mem_nhds_within' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : (ext_chart_at I x).source ∈ 𝓝[s] x' := mem_nhds_within_of_mem_nhds (ext_chart_at_source_mem_nhds' I x h) lemma ext_chart_at_source_mem_nhds_within : (ext_chart_at I x).source ∈ 𝓝[s] x := mem_nhds_within_of_mem_nhds (ext_chart_at_source_mem_nhds I x) lemma ext_chart_at_continuous_on : continuous_on (ext_chart_at I x) (ext_chart_at I x).source := begin refine I.continuous.comp_continuous_on _, rw ext_chart_at_source, exact (chart_at H x).continuous_on end lemma ext_chart_at_continuous_at' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : continuous_at (ext_chart_at I x) x' := (ext_chart_at_continuous_on I x).continuous_at $ ext_chart_at_source_mem_nhds' I x h lemma ext_chart_at_continuous_at : continuous_at (ext_chart_at I x) x := ext_chart_at_continuous_at' _ _ (mem_ext_chart_source I x) lemma ext_chart_at_continuous_on_symm : continuous_on (ext_chart_at I x).symm (ext_chart_at I x).target := begin apply continuous_on.comp (chart_at H x).continuous_on_symm I.continuous_symm.continuous_on, simp [ext_chart_at, local_equiv.trans_target] end lemma ext_chart_at_map_nhds' {x y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x) (𝓝 y) = 𝓝[range I] (ext_chart_at I x y) := begin rw [ext_chart_at_coe, (∘), ← I.map_nhds_eq, ← (chart_at H x).map_nhds_eq, map_map], rwa ext_chart_at_source at hy end lemma ext_chart_at_map_nhds : map (ext_chart_at I x) (𝓝 x) = 𝓝[range I] (ext_chart_at I x x) := ext_chart_at_map_nhds' I $ mem_ext_chart_source I x lemma ext_chart_at_target_mem_nhds_within' {y : M} (hy : y ∈ (ext_chart_at I x).source) : (ext_chart_at I x).target ∈ 𝓝[range I] (ext_chart_at I x y) := begin rw [← local_equiv.image_source_eq_target, ← ext_chart_at_map_nhds' I hy], exact image_mem_map (ext_chart_at_source_mem_nhds' _ _ hy) end lemma ext_chart_at_target_mem_nhds_within : (ext_chart_at I x).target ∈ 𝓝[range I] (ext_chart_at I x x) := ext_chart_at_target_mem_nhds_within' I x (mem_ext_chart_source I x) lemma ext_chart_at_target_subset_range : (ext_chart_at I x).target ⊆ range I := by simp only with mfld_simps lemma nhds_within_ext_chart_target_eq' {y : M} (hy : y ∈ (ext_chart_at I x).source) : 𝓝[(ext_chart_at I x).target] (ext_chart_at I x y) = 𝓝[range I] (ext_chart_at I x y) := (nhds_within_mono _ (ext_chart_at_target_subset_range _ _)).antisymm $ nhds_within_le_of_mem (ext_chart_at_target_mem_nhds_within' _ _ hy) lemma nhds_within_ext_chart_target_eq : 𝓝[(ext_chart_at I x).target] ((ext_chart_at I x) x) = 𝓝[range I] ((ext_chart_at I x) x) := nhds_within_ext_chart_target_eq' I x (mem_ext_chart_source I x) lemma ext_chart_continuous_at_symm'' {y : E} (h : y ∈ (ext_chart_at I x).target) : continuous_at (ext_chart_at I x).symm y := continuous_at.comp ((chart_at H x).continuous_at_symm h.2) (I.continuous_symm.continuous_at) lemma ext_chart_continuous_at_symm' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : continuous_at (ext_chart_at I x).symm (ext_chart_at I x x') := ext_chart_continuous_at_symm'' I _ $ (ext_chart_at I x).map_source h lemma ext_chart_continuous_at_symm : continuous_at (ext_chart_at I x).symm ((ext_chart_at I x) x) := ext_chart_continuous_at_symm' I x (mem_ext_chart_source I x) lemma ext_chart_continuous_on_symm : continuous_on (ext_chart_at I x).symm (ext_chart_at I x).target := λ y hy, (ext_chart_continuous_at_symm'' _ _ hy).continuous_within_at lemma ext_chart_preimage_open_of_open' {s : set E} (hs : is_open s) : is_open ((ext_chart_at I x).source ∩ ext_chart_at I x ⁻¹' s) := (ext_chart_at_continuous_on I x).preimage_open_of_open (ext_chart_at_open_source _ _) hs lemma ext_chart_preimage_open_of_open {s : set E} (hs : is_open s) : is_open ((chart_at H x).source ∩ ext_chart_at I x ⁻¹' s) := by { rw ← ext_chart_at_source I, exact ext_chart_preimage_open_of_open' I x hs } lemma ext_chart_at_map_nhds_within_eq_image' {y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x) (𝓝[s] y) = 𝓝[ext_chart_at I x '' ((ext_chart_at I x).source ∩ s)] (ext_chart_at I x y) := by set e := ext_chart_at I x; calc map e (𝓝[s] y) = map e (𝓝[e.source ∩ s] y) : congr_arg (map e) (nhds_within_inter_of_mem (ext_chart_at_source_mem_nhds_within' I x hy)).symm ... = 𝓝[e '' (e.source ∩ s)] (e y) : ((ext_chart_at I x).left_inv_on.mono $ inter_subset_left _ _).map_nhds_within_eq ((ext_chart_at I x).left_inv hy) (ext_chart_continuous_at_symm' I x hy).continuous_within_at (ext_chart_at_continuous_at' I x hy).continuous_within_at lemma ext_chart_at_map_nhds_within_eq_image : map (ext_chart_at I x) (𝓝[s] x) = 𝓝[ext_chart_at I x '' ((ext_chart_at I x).source ∩ s)] (ext_chart_at I x x) := ext_chart_at_map_nhds_within_eq_image' I x (mem_ext_chart_source I x) lemma ext_chart_at_map_nhds_within' {y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x) (𝓝[s] y) = 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x y) := by rw [ext_chart_at_map_nhds_within_eq_image' I x hy, nhds_within_inter, ← nhds_within_ext_chart_target_eq' _ _ hy, ← nhds_within_inter, (ext_chart_at I x).image_source_inter_eq', inter_comm] lemma ext_chart_at_map_nhds_within : map (ext_chart_at I x) (𝓝[s] x) = 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x x) := ext_chart_at_map_nhds_within' I x (mem_ext_chart_source I x) lemma ext_chart_at_symm_map_nhds_within' {y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x).symm (𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x y)) = 𝓝[s] y := begin rw [← ext_chart_at_map_nhds_within' I x hy, map_map, map_congr, map_id], exact (ext_chart_at I x).left_inv_on.eq_on.eventually_eq_of_mem (ext_chart_at_source_mem_nhds_within' _ _ hy) end lemma ext_chart_at_symm_map_nhds_within_range' {y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x).symm (𝓝[range I] (ext_chart_at I x y)) = 𝓝 y := by rw [← nhds_within_univ, ← ext_chart_at_symm_map_nhds_within' I x hy, preimage_univ, univ_inter] lemma ext_chart_at_symm_map_nhds_within : map (ext_chart_at I x).symm (𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x x)) = 𝓝[s] x := ext_chart_at_symm_map_nhds_within' I x (mem_ext_chart_source I x) lemma ext_chart_at_symm_map_nhds_within_range : map (ext_chart_at I x).symm (𝓝[range I] (ext_chart_at I x x)) = 𝓝 x := ext_chart_at_symm_map_nhds_within_range' I x (mem_ext_chart_source I x) /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point in the source is a neighborhood of the preimage, within a set. -/ lemma ext_chart_preimage_mem_nhds_within' {x' : M} (h : x' ∈ (ext_chart_at I x).source) (ht : t ∈ 𝓝[s] x') : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x') := by rwa [← ext_chart_at_symm_map_nhds_within' I x h, mem_map] at ht /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of the base point is a neighborhood of the preimage, within a set. -/ lemma ext_chart_preimage_mem_nhds_within (ht : t ∈ 𝓝[s] x) : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := ext_chart_preimage_mem_nhds_within' I x (mem_ext_chart_source I x) ht /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point is a neighborhood of the preimage. -/ lemma ext_chart_preimage_mem_nhds (ht : t ∈ 𝓝 x) : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝 ((ext_chart_at I x) x) := begin apply (ext_chart_continuous_at_symm I x).preimage_mem_nhds, rwa (ext_chart_at I x).left_inv (mem_ext_chart_source _ _) end /-- Technical lemma to rewrite suitably the preimage of an intersection under an extended chart, to bring it into a convenient form to apply derivative lemmas. -/ lemma ext_chart_preimage_inter_eq : ((ext_chart_at I x).symm ⁻¹' (s ∩ t) ∩ range I) = ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ∩ ((ext_chart_at I x).symm ⁻¹' t) := by mfld_set_tac end extended_charts /-- In the case of the manifold structure on a vector space, the extended charts are just the identity.-/ lemma ext_chart_model_space_eq_id (𝕜 : Type*) [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] (x : E) : ext_chart_at (model_with_corners_self 𝕜 E) x = local_equiv.refl E := by simp only with mfld_simps
46791912ba653c3ed2ac5a9d329c9f8df4fc0fae
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/pi.lean
9d55eab4bb20a378b4ef482776afbefd364a2b60
[ "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
12,808
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov, Eric Wieser -/ import linear_algebra.basic /-! # Pi types of modules This file defines constructors for linear maps whose domains or codomains are pi types. It contains theorems relating these to each other, as well as to `linear_map.ker`. ## Main definitions - pi types in the codomain: - `linear_map.pi` - `linear_map.single` - pi types in the domain: - `linear_map.proj` - `linear_map.diag` -/ universes u v w x y z u' v' w' x' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x} {ι' : Type x'} open function submodule open_locale big_operators namespace linear_map universe i variables [semiring R] [add_comm_monoid M₂] [module R M₂] [add_comm_monoid M₃] [module R M₃] {φ : ι → Type i} [∀i, add_comm_monoid (φ i)] [∀i, module R (φ i)] /-- `pi` construction for linear functions. From a family of linear functions it produces a linear function into a family of modules. -/ def pi (f : Πi, M₂ →ₗ[R] φ i) : M₂ →ₗ[R] (Πi, φ i) := { to_fun := λ c i, f i c, map_add' := λ c d, funext $ λ i, (f i).map_add _ _, map_smul' := λ c d, funext $ λ i, (f i).map_smul _ _ } @[simp] lemma pi_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i : ι) : pi f c i = f i c := rfl lemma ker_pi (f : Πi, M₂ →ₗ[R] φ i) : ker (pi f) = (⨅i:ι, ker (f i)) := by ext c; simp [funext_iff]; refl lemma pi_eq_zero (f : Πi, M₂ →ₗ[R] φ i) : pi f = 0 ↔ (∀i, f i = 0) := by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩ lemma pi_zero : pi (λi, 0 : Πi, M₂ →ₗ[R] φ i) = 0 := by ext; refl lemma pi_comp (f : Πi, M₂ →ₗ[R] φ i) (g : M₃ →ₗ[R] M₂) : (pi f).comp g = pi (λi, (f i).comp g) := rfl /-- The projections from a family of modules are linear maps. Note: known here as `linear_map.proj`, this construction is in other categories called `eval`, for example `pi.eval_monoid_hom`, `pi.eval_ring_hom`. -/ def proj (i : ι) : (Πi, φ i) →ₗ[R] φ i := { to_fun := function.eval i, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } @[simp] lemma coe_proj (i : ι) : ⇑(proj i : (Πi, φ i) →ₗ[R] φ i) = function.eval i := rfl lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →ₗ[R] φ i) b = b i := rfl lemma proj_pi (f : Πi, M₂ →ₗ[R] φ i) (i : ι) : (proj i).comp (pi f) = f i := ext $ assume c, rfl lemma infi_ker_proj : (⨅i, ker (proj i) : submodule R (Πi, φ i)) = ⊥ := bot_unique $ set_like.le_def.2 $ assume a h, begin simp only [mem_infi, mem_ker, proj_apply] at h, exact (mem_bot _).2 (funext $ assume i, h i) end /-- Linear map between the function spaces `I → M₂` and `I → M₃`, induced by a linear map `f` between `M₂` and `M₃`. -/ @[simps] protected def comp_left (f : M₂ →ₗ[R] M₃) (I : Type*) : (I → M₂) →ₗ[R] (I → M₃) := { to_fun := λ h, f ∘ h, map_smul' := λ c h, by { ext x, exact f.map_smul' c (h x) }, .. f.to_add_monoid_hom.comp_left I } lemma apply_single [add_comm_monoid M] [module R M] [decidable_eq ι] (f : Π i, φ i →ₗ[R] M) (i j : ι) (x : φ i) : f j (pi.single i x j) = pi.single i (f i x) j := pi.apply_single (λ i, f i) (λ i, (f i).map_zero) _ _ _ /-- The `linear_map` version of `add_monoid_hom.single` and `pi.single`. -/ def single [decidable_eq ι] (i : ι) : φ i →ₗ[R] (Πi, φ i) := { to_fun := pi.single i, map_smul' := pi.single_smul i, .. add_monoid_hom.single φ i} @[simp] lemma coe_single [decidable_eq ι] (i : ι) : ⇑(single i : φ i →ₗ[R] (Π i, φ i)) = pi.single i := rfl variables (R φ) /-- The linear equivalence between linear functions on a finite product of modules and families of functions on these modules. See note [bundled maps over different rings]. -/ @[simps] def lsum (S) [add_comm_monoid M] [module R M] [fintype ι] [decidable_eq ι] [semiring S] [module S M] [smul_comm_class R S M] : (Π i, φ i →ₗ[R] M) ≃ₗ[S] ((Π i, φ i) →ₗ[R] M) := { to_fun := λ f, ∑ i : ι, (f i).comp (proj i), inv_fun := λ f i, f.comp (single i), map_add' := λ f g, by simp only [pi.add_apply, add_comp, finset.sum_add_distrib], map_smul' := λ c f, by simp only [pi.smul_apply, smul_comp, finset.smul_sum], left_inv := λ f, by { ext i x, simp [apply_single] }, right_inv := λ f, begin ext, suffices : f (∑ j, pi.single j (x j)) = f x, by simpa [apply_single], rw finset.univ_sum_single end } variables {R φ} section ext variables [fintype ι] [decidable_eq ι] [add_comm_monoid M] [module R M] {f g : (Π i, φ i) →ₗ[R] M} lemma pi_ext (h : ∀ i x, f (pi.single i x) = g (pi.single i x)) : f = g := to_add_monoid_hom_injective $ add_monoid_hom.functions_ext _ _ _ h lemma pi_ext_iff : f = g ↔ ∀ i x, f (pi.single i x) = g (pi.single i x) := ⟨λ h i x, h ▸ rfl, pi_ext⟩ /-- This is used as the ext lemma instead of `linear_map.pi_ext` for reasons explained in note [partially-applied ext lemmas]. -/ @[ext] lemma pi_ext' (h : ∀ i, f.comp (single i) = g.comp (single i)) : f = g := begin refine pi_ext (λ i x, _), convert linear_map.congr_fun (h i) x end lemma pi_ext'_iff : f = g ↔ ∀ i, f.comp (single i) = g.comp (single i) := ⟨λ h i, h ▸ rfl, pi_ext'⟩ end ext section variables (R φ) /-- If `I` and `J` are disjoint index sets, the product of the kernels of the `J`th projections of `φ` is linearly equivalent to the product over `I`. -/ def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)] (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) : (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i)) ≃ₗ[R] (Πi:I, φ i) := begin refine linear_equiv.of_linear (pi $ λi, (proj (i:ι)).comp (submodule.subtype _)) (cod_restrict _ (pi $ λi, if h : i ∈ I then proj (⟨i, h⟩ : I) else 0) _) _ _, { assume b, simp only [mem_infi, mem_ker, funext_iff, proj_apply, pi_apply], assume j hjJ, have : j ∉ I := assume hjI, hd ⟨hjI, hjJ⟩, rw [dif_neg this, zero_apply] }, { simp only [pi_comp, comp_assoc, subtype_comp_cod_restrict, proj_pi, dif_pos, subtype.coe_prop], ext b ⟨j, hj⟩, refl }, { ext1 ⟨b, hb⟩, apply subtype.ext, ext j, have hb : ∀i ∈ J, b i = 0, { simpa only [mem_infi, mem_ker, proj_apply] using (mem_infi _).1 hb }, simp only [comp_apply, pi_apply, id_apply, proj_apply, subtype_apply, cod_restrict_apply], split_ifs, { refl }, { exact (hb _ $ (hu trivial).resolve_left h).symm } } end end section variable [decidable_eq ι] /-- `diag i j` is the identity map if `i = j`. Otherwise it is the constant 0 map. -/ def diag (i j : ι) : φ i →ₗ[R] φ j := @function.update ι (λj, φ i →ₗ[R] φ j) _ 0 i id j lemma update_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i j : ι) (b : M₂ →ₗ[R] φ i) : (update f i b j) c = update (λi, f i c) i (b c) j := begin by_cases j = i, { rw [h, update_same, update_same] }, { rw [update_noteq h, update_noteq h] } end end end linear_map namespace submodule variables [semiring R] {φ : ι → Type*} [∀ i, add_comm_monoid (φ i)] [∀ i, module R (φ i)] open linear_map /-- A version of `set.pi` for submodules. Given an index set `I` and a family of submodules `p : Π i, submodule R (φ i)`, `pi I s` is the submodule of dependent functions `f : Π i, φ i` such that `f i` belongs to `p a` whenever `i ∈ I`. -/ def pi (I : set ι) (p : Π i, submodule R (φ i)) : submodule R (Π i, φ i) := { carrier := set.pi I (λ i, p i), zero_mem' := λ i hi, (p i).zero_mem, add_mem' := λ x y hx hy i hi, (p i).add_mem (hx i hi) (hy i hi), smul_mem' := λ c x hx i hi, (p i).smul_mem c (hx i hi) } variables {I : set ι} {p : Π i, submodule R (φ i)} {x : Π i, φ i} @[simp] lemma mem_pi : x ∈ pi I p ↔ ∀ i ∈ I, x i ∈ p i := iff.rfl @[simp, norm_cast] lemma coe_pi : (pi I p : set (Π i, φ i)) = set.pi I (λ i, p i) := rfl lemma binfi_comap_proj : (⨅ i ∈ I, comap (proj i) (p i)) = pi I p := by { ext x, simp } lemma infi_comap_proj : (⨅ i, comap (proj i) (p i)) = pi set.univ p := by { ext x, simp } lemma supr_map_single [decidable_eq ι] [fintype ι] : (⨆ i, map (linear_map.single i) (p i)) = pi set.univ p := begin refine (supr_le $ λ i, _).antisymm _, { rintro _ ⟨x, hx : x ∈ p i, rfl⟩ j -, rcases em (j = i) with rfl|hj; simp * }, { intros x hx, rw [← finset.univ_sum_single x], exact sum_mem_supr (λ i, mem_map_of_mem (hx i trivial)) } end end submodule namespace linear_equiv variables [semiring R] {φ ψ χ : ι → Type*} [∀ i, add_comm_monoid (φ i)] [∀ i, module R (φ i)] variables [∀ i, add_comm_monoid (ψ i)] [∀ i, module R (ψ i)] variables [∀ i, add_comm_monoid (χ i)] [∀ i, module R (χ i)] /-- Combine a family of linear equivalences into a linear equivalence of `pi`-types. This is `equiv.Pi_congr_right` as a `linear_equiv` -/ @[simps apply] def Pi_congr_right (e : Π i, φ i ≃ₗ[R] ψ i) : (Π i, φ i) ≃ₗ[R] (Π i, ψ i) := { to_fun := λ f i, e i (f i), inv_fun := λ f i, (e i).symm (f i), map_smul' := λ c f, by { ext, simp }, .. add_equiv.Pi_congr_right (λ j, (e j).to_add_equiv) } @[simp] lemma Pi_congr_right_refl : Pi_congr_right (λ j, refl R (φ j)) = refl _ _ := rfl @[simp] lemma Pi_congr_right_symm (e : Π i, φ i ≃ₗ[R] ψ i) : (Pi_congr_right e).symm = (Pi_congr_right $ λ i, (e i).symm) := rfl @[simp] lemma Pi_congr_right_trans (e : Π i, φ i ≃ₗ[R] ψ i) (f : Π i, ψ i ≃ₗ[R] χ i) : (Pi_congr_right e).trans (Pi_congr_right f) = (Pi_congr_right $ λ i, (e i).trans (f i)) := rfl variables (R φ) /-- Transport dependent functions through an equivalence of the base space. This is `equiv.Pi_congr_left'` as a `linear_equiv`. -/ @[simps {simp_rhs := tt}] def Pi_congr_left' (e : ι ≃ ι') : (Π i', φ i') ≃ₗ[R] (Π i, φ $ e.symm i) := { map_add' := λ x y, rfl, map_smul' := λ x y, rfl, .. equiv.Pi_congr_left' φ e } /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". This is `equiv.Pi_congr_left` as a `linear_equiv` -/ def Pi_congr_left (e : ι' ≃ ι) : (Π i', φ (e i')) ≃ₗ[R] (Π i, φ i) := (Pi_congr_left' R φ e.symm).symm variables (ι R M) (S : Type*) [fintype ι] [decidable_eq ι] [semiring S] [add_comm_monoid M] [module R M] [module S M] [smul_comm_class R S M] /-- Linear equivalence between linear functions `Rⁿ → M` and `Mⁿ`. The spaces `Rⁿ` and `Mⁿ` are represented as `ι → R` and `ι → M`, respectively, where `ι` is a finite type. This as an `S`-linear equivalence, under the assumption that `S` acts on `M` commuting with `R`. When `R` is commutative, we can take this to be the usual action with `S = R`. Otherwise, `S = ℕ` shows that the equivalence is additive. See note [bundled maps over different rings]. -/ def pi_ring : ((ι → R) →ₗ[R] M) ≃ₗ[S] (ι → M) := (linear_map.lsum R (λ i : ι, R) S).symm.trans (Pi_congr_right $ λ i, linear_map.ring_lmap_equiv_self R M S) variables {ι R M} @[simp] lemma pi_ring_apply (f : (ι → R) →ₗ[R] M) (i : ι) : pi_ring R M ι S f i = f (pi.single i 1) := rfl @[simp] lemma pi_ring_symm_apply (f : ι → M) (g : ι → R) : (pi_ring R M ι S).symm f g = ∑ i, g i • f i := by simp [pi_ring, linear_map.lsum] /-- `equiv.sum_arrow_equiv_prod_arrow` as a linear equivalence. -/ -- TODO additive version? def sum_arrow_lequiv_prod_arrow (α β R M : Type*) [semiring R] [add_comm_monoid M] [module R M] : ((α ⊕ β) → M) ≃ₗ[R] (α → M) × (β → M) := { map_add' := by { intros f g, ext; refl }, map_smul' := by { intros r f, ext; refl, }, .. equiv.sum_arrow_equiv_prod_arrow α β M, } @[simp] lemma sum_arrow_lequiv_prod_arrow_apply_fst {α β} (f : (α ⊕ β) → M) (a : α) : (sum_arrow_lequiv_prod_arrow α β R M f).1 a = f (sum.inl a) := rfl @[simp] lemma sum_arrow_lequiv_prod_arrow_apply_snd {α β} (f : (α ⊕ β) → M) (b : β) : (sum_arrow_lequiv_prod_arrow α β R M f).2 b = f (sum.inr b) := rfl @[simp] lemma sum_arrow_lequiv_prod_arrow_symm_apply_inl {α β} (f : α → M) (g : β → M) (a : α) : ((sum_arrow_lequiv_prod_arrow α β R M).symm (f, g)) (sum.inl a) = f a := rfl @[simp] lemma sum_arrow_lequiv_prod_arrow_symm_apply_inr {α β} (f : α → M) (g : β → M) (b : β) : ((sum_arrow_lequiv_prod_arrow α β R M).symm (f, g)) (sum.inr b) = g b := rfl end linear_equiv
b1b5ab65b0e983cc2d70bafb1bf2c66487c75935
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/extern.lean
84a3c515d012f0fa235baa5e2f7b0ba13b0d9925
[ "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
318
lean
new_frontend @[extern] def foo (x : Nat) := x @[extern "bla"] def foo2 (x : Nat) := x @[extern 2 "boo"] def foo3 (x : Nat) := x @[extern cpp "Lean::bla" llvm "lean_bla"] def foo4 (x : Bool) := x @[extern cpp inline "#1 && #2"] def foo5 (x y : Bool) := x @[extern cpp adhoc llvm "foo"] def foo6 (x y : Bool) := x
1b1d290dbff704d1b7d86143904ab696fe034b9e
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/hit/pushout.hlean
908a17e5233e4f7bc818996e2f8350deac956533
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
12,891
hlean
/- Copyright (c) 2015-16 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Ulrik Buchholtz Declaration and properties of the pushout -/ import .quotient types.sigma types.arrow_2 open quotient eq sum equiv is_trunc namespace pushout section parameters {TL BL TR : Type} (f : TL → BL) (g : TL → TR) local abbreviation A := BL + TR inductive pushout_rel : A → A → Type := | Rmk : Π(x : TL), pushout_rel (inl (f x)) (inr (g x)) open pushout_rel local abbreviation R := pushout_rel definition pushout : Type := quotient R -- TODO: define this in root namespace parameters {f g} definition inl (x : BL) : pushout := class_of R (inl x) definition inr (x : TR) : pushout := class_of R (inr x) definition glue (x : TL) : inl (f x) = inr (g x) := eq_of_rel pushout_rel (Rmk f g x) protected definition rec {P : pushout → Type} (Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x)) (y : pushout) : P y := begin induction y, { cases a, apply Pinl, apply Pinr}, { cases H, apply Pglue} end protected definition rec_on [reducible] {P : pushout → Type} (y : pushout) (Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x)) : P y := rec Pinl Pinr Pglue y theorem rec_glue {P : pushout → Type} (Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x)) (x : TL) : apd (rec Pinl Pinr Pglue) (glue x) = Pglue x := !rec_eq_of_rel protected definition elim {P : Type} (Pinl : BL → P) (Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) (y : pushout) : P := rec Pinl Pinr (λx, pathover_of_eq _ (Pglue x)) y protected definition elim_on [reducible] {P : Type} (y : pushout) (Pinl : BL → P) (Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) : P := elim Pinl Pinr Pglue y theorem elim_glue {P : Type} (Pinl : BL → P) (Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) (x : TL) : ap (elim Pinl Pinr Pglue) (glue x) = Pglue x := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (glue x)), rewrite [▸*,-apd_eq_pathover_of_eq_ap,↑pushout.elim,rec_glue], end protected definition elim_type (Pinl : BL → Type) (Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) : pushout → Type := quotient.elim_type (sum.rec Pinl Pinr) begin intro v v' r, induction r, apply Pglue end protected definition elim_type_on [reducible] (y : pushout) (Pinl : BL → Type) (Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) : Type := elim_type Pinl Pinr Pglue y theorem elim_type_glue (Pinl : BL → Type) (Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) (x : TL) : transport (elim_type Pinl Pinr Pglue) (glue x) = Pglue x := !elim_type_eq_of_rel_fn theorem elim_type_glue_inv (Pinl : BL → Type) (Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) (x : TL) : transport (elim_type Pinl Pinr Pglue) (glue x)⁻¹ = to_inv (Pglue x) := !elim_type_eq_of_rel_inv protected definition rec_prop {P : pushout → Type} [H : Πx, is_prop (P x)] (Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (y : pushout) := rec Pinl Pinr (λx, !is_prop.elimo) y protected definition elim_prop {P : Type} [H : is_prop P] (Pinl : BL → P) (Pinr : TR → P) (y : pushout) : P := elim Pinl Pinr (λa, !is_prop.elim) y end end pushout attribute pushout.inl pushout.inr [constructor] attribute pushout.rec pushout.elim [unfold 10] [recursor 10] attribute pushout.elim_type [unfold 9] attribute pushout.rec_on pushout.elim_on [unfold 7] attribute pushout.elim_type_on [unfold 6] open sigma namespace pushout variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR) /- The non-dependent universal property -/ definition pushout_arrow_equiv (C : Type) : (pushout f g → C) ≃ (Σ(i : BL → C) (j : TR → C), Πc, i (f c) = j (g c)) := begin fapply equiv.MK, { intro f, exact ⟨λx, f (inl x), λx, f (inr x), λx, ap f (glue x)⟩}, { intro v x, induction v with i w, induction w with j p, induction x, exact (i a), exact (j a), exact (p x)}, { intro v, induction v with i w, induction w with j p, esimp, apply ap (λp, ⟨i, j, p⟩), apply eq_of_homotopy, intro x, apply elim_glue}, { intro f, apply eq_of_homotopy, intro x, induction x: esimp, apply eq_pathover, apply hdeg_square, esimp, apply elim_glue}, end /- glue squares -/ protected definition glue_square {x x' : TL} (p : x = x') : square (glue x) (glue x') (ap inl (ap f p)) (ap inr (ap g p)) := by cases p; apply vrefl end pushout open function sigma.ops namespace pushout /- The flattening lemma -/ section universe variable u parameters {TL BL TR : Type} (f : TL → BL) (g : TL → TR) (Pinl : BL → Type.{u}) (Pinr : TR → Type.{u}) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) include Pglue local abbreviation A := BL + TR local abbreviation R : A → A → Type := pushout_rel f g local abbreviation P [unfold 5] := pushout.elim_type Pinl Pinr Pglue local abbreviation F : sigma (Pinl ∘ f) → sigma Pinl := λz, ⟨ f z.1 , z.2 ⟩ local abbreviation G : sigma (Pinl ∘ f) → sigma Pinr := λz, ⟨ g z.1 , Pglue z.1 z.2 ⟩ protected definition flattening : sigma P ≃ pushout F G := begin apply equiv.trans !quotient.flattening.flattening_lemma, fapply equiv.MK, { intro q, induction q with z z z' fr, { induction z with a p, induction a with x x, { exact inl ⟨x, p⟩ }, { exact inr ⟨x, p⟩ } }, { induction fr with a a' r p, induction r with x, exact glue ⟨x, p⟩ } }, { intro q, induction q with xp xp xp, { exact class_of _ ⟨sum.inl xp.1, xp.2⟩ }, { exact class_of _ ⟨sum.inr xp.1, xp.2⟩ }, { apply eq_of_rel, constructor } }, { intro q, induction q with xp xp xp: induction xp with x p, { apply ap inl, reflexivity }, { apply ap inr, reflexivity }, { unfold F, unfold G, apply eq_pathover, rewrite [ap_id,ap_compose' (quotient.elim _ _)], krewrite elim_glue, krewrite elim_eq_of_rel, apply hrefl } }, { intro q, induction q with z z z' fr, { induction z with a p, induction a with x x, { reflexivity }, { reflexivity } }, { induction fr with a a' r p, induction r with x, esimp, apply eq_pathover, rewrite [ap_id,ap_compose' (pushout.elim _ _ _)], krewrite elim_eq_of_rel, krewrite elim_glue, apply hrefl } } end end -- Commutativity of pushouts section variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR) protected definition transpose [constructor] : pushout f g → pushout g f := begin intro x, induction x, apply inr a, apply inl a, apply !glue⁻¹ end --TODO prove without krewrite? protected definition transpose_involutive (x : pushout f g) : pushout.transpose g f (pushout.transpose f g x) = x := begin induction x, apply idp, apply idp, apply eq_pathover, refine _ ⬝hp !ap_id⁻¹, refine !(ap_compose (pushout.transpose _ _)) ⬝ph _, esimp[pushout.transpose], krewrite [elim_glue, ap_inv, elim_glue, inv_inv], apply hrfl end protected definition symm : pushout f g ≃ pushout g f := begin fapply equiv.MK, do 2 exact !pushout.transpose, do 2 (intro x; apply pushout.transpose_involutive), end end -- Functoriality of pushouts section section lemmas variables {X : Type} {x₀ x₁ x₂ x₃ : X} (p : x₀ = x₁) (q : x₁ = x₂) (r : x₂ = x₃) private definition is_equiv_functor_lemma₁ : (r ⬝ ((p ⬝ q ⬝ r)⁻¹ ⬝ p)) = q⁻¹ := by cases p; cases r; cases q; reflexivity private definition is_equiv_functor_lemma₂ : (p ⬝ q ⬝ r)⁻¹ ⬝ (p ⬝ q) = r⁻¹ := by cases p; cases r; cases q; reflexivity end lemmas variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR) {TL' BL' TR' : Type} (f' : TL' → BL') (g' : TL' → TR') (tl : TL → TL') (bl : BL → BL') (tr : TR → TR') (fh : bl ∘ f ~ f' ∘ tl) (gh : tr ∘ g ~ g' ∘ tl) include fh gh protected definition functor [reducible] : pushout f g → pushout f' g' := begin intro x, induction x with a b z, { exact inl (bl a) }, { exact inr (tr b) }, { exact (ap inl (fh z)) ⬝ glue (tl z) ⬝ (ap inr (gh z)⁻¹) } end protected definition ap_functor_inl [reducible] {x x' : BL} (p : x = x') : ap (pushout.functor f g f' g' tl bl tr fh gh) (ap inl p) = ap inl (ap bl p) := by cases p; reflexivity protected definition ap_functor_inr [reducible] {x x' : TR} (p : x = x') : ap (pushout.functor f g f' g' tl bl tr fh gh) (ap inr p) = ap inr (ap tr p) := by cases p; reflexivity variables [ietl : is_equiv tl] [iebl : is_equiv bl] [ietr : is_equiv tr] include ietl iebl ietr open equiv is_equiv arrow protected definition is_equiv_functor [instance] : is_equiv (pushout.functor f g f' g' tl bl tr fh gh) := adjointify (pushout.functor f g f' g' tl bl tr fh gh) (pushout.functor f' g' f g tl⁻¹ bl⁻¹ tr⁻¹ (inv_commute_of_commute tl bl f f' fh) (inv_commute_of_commute tl tr g g' gh)) abstract begin intro x', induction x' with a' b' z', { apply ap inl, apply right_inv }, { apply ap inr, apply right_inv }, { apply eq_pathover, rewrite [ap_id,ap_compose' (pushout.functor f g f' g' tl bl tr fh gh)], krewrite elim_glue, rewrite [ap_inv,ap_con,ap_inv], krewrite [pushout.ap_functor_inr], rewrite ap_con, krewrite [pushout.ap_functor_inl,elim_glue], apply transpose, apply move_top_of_right, apply move_top_of_left', krewrite [-(ap_inv inl),-ap_con,-(ap_inv inr),-ap_con], apply move_top_of_right, apply move_top_of_left', krewrite [-ap_con,-(ap_inv inl),-ap_con], rewrite ap_bot_inv_commute_of_commute, apply eq_hconcat (ap02 inl (is_equiv_functor_lemma₁ (right_inv bl (f' z')) (ap f' (right_inv tl z')⁻¹) (fh (tl⁻¹ z'))⁻¹)), rewrite [ap_inv f',inv_inv], rewrite ap_bot_inv_commute_of_commute, refine hconcat_eq _ (ap02 inr (is_equiv_functor_lemma₁ (right_inv tr (g' z')) (ap g' (right_inv tl z')⁻¹) (gh (tl⁻¹ z'))⁻¹))⁻¹, rewrite [ap_inv g',inv_inv], apply pushout.glue_square } end end abstract begin intro x, induction x with a b z, { apply ap inl, apply left_inv }, { apply ap inr, apply left_inv }, { apply eq_pathover, rewrite [ap_id,ap_compose' (pushout.functor f' g' f g tl⁻¹ bl⁻¹ tr⁻¹ _ _) (pushout.functor f g f' g' tl bl tr _ _)], krewrite elim_glue, rewrite [ap_inv,ap_con,ap_inv], krewrite [pushout.ap_functor_inr], rewrite ap_con, krewrite [pushout.ap_functor_inl,elim_glue], apply transpose, apply move_top_of_right, apply move_top_of_left', krewrite [-(ap_inv inl),-ap_con,-(ap_inv inr),-ap_con], apply move_top_of_right, apply move_top_of_left', krewrite [-ap_con,-(ap_inv inl),-ap_con], rewrite inv_commute_of_commute_top, apply eq_hconcat (ap02 inl (is_equiv_functor_lemma₂ (ap bl⁻¹ (fh z))⁻¹ (left_inv bl (f z)) (ap f (left_inv tl z)⁻¹))), rewrite [ap_inv f,inv_inv], rewrite inv_commute_of_commute_top, refine hconcat_eq _ (ap02 inr (is_equiv_functor_lemma₂ (ap tr⁻¹ (gh z))⁻¹ (left_inv tr (g z)) (ap g (left_inv tl z)⁻¹)))⁻¹, rewrite [ap_inv g,inv_inv], apply pushout.glue_square } end end end /- version giving the equivalence -/ section variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR) {TL' BL' TR' : Type} (f' : TL' → BL') (g' : TL' → TR') (tl : TL ≃ TL') (bl : BL ≃ BL') (tr : TR ≃ TR') (fh : bl ∘ f ~ f' ∘ tl) (gh : tr ∘ g ~ g' ∘ tl) include fh gh protected definition equiv : pushout f g ≃ pushout f' g' := equiv.mk (pushout.functor f g f' g' tl bl tr fh gh) _ end end pushout
710d3139400c5661ad864a6e765bfe6125a5026f
63abd62053d479eae5abf4951554e1064a4c45b4
/src/linear_algebra/lagrange.lean
cf03e714c1cb0b9a69d6b3be9d92cc2ddbe1ed31
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
7,092
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kenny Lau. -/ import ring_theory.polynomial import algebra.big_operators.basic /-! # Lagrange interpolation ## Main definitions * `lagrange.basis s x` where `s : finset F` and `x : F`: the Lagrange basis polynomial that evaluates to `1` at `x` and `0` at other elements of `s`. * `lagrange.interpolate s f` where `s : finset F` and `f : s → F`: the Lagrange interpolant that evaluates to `f x` at `x` for `x ∈ s`. -/ noncomputable theory open_locale big_operators classical universe u namespace lagrange variables {F : Type u} [decidable_eq F] [field F] (s : finset F) variables {F' : Type u} [field F'] (s' : finset F') open polynomial /-- Lagrange basis polynomials that evaluate to 1 at `x` and 0 at other elements of `s`. -/ def basis (x : F) : polynomial F := ∏ y in s.erase x, C (x - y)⁻¹ * (X - C y) @[simp] theorem basis_empty (x : F) : basis ∅ x = 1 := rfl @[simp] theorem eval_basis_self (x : F) : (basis s x).eval x = 1 := begin rw [basis, ← (s.erase x).prod_hom (eval x), finset.prod_eq_one], intros y hy, simp_rw [eval_mul, eval_sub, eval_C, eval_X], exact inv_mul_cancel (sub_ne_zero_of_ne (finset.ne_of_mem_erase hy).symm) end @[simp] theorem eval_basis_ne (x y : F) (h1 : y ∈ s) (h2 : y ≠ x) : (basis s x).eval y = 0 := begin rw [basis, ← (s.erase x).prod_hom (eval y), finset.prod_eq_zero (finset.mem_erase.2 ⟨h2, h1⟩)], simp_rw [eval_mul, eval_sub, eval_C, eval_X, sub_self, mul_zero] end theorem eval_basis (x y : F) (h : y ∈ s) : (basis s x).eval y = if y = x then 1 else 0 := by { split_ifs with H, { subst H, apply eval_basis_self }, { exact eval_basis_ne s x y h H } } @[simp] theorem nat_degree_basis (x : F) (hx : x ∈ s) : (basis s x).nat_degree = s.card - 1 := begin unfold basis, generalize hsx : s.erase x = sx, have : x ∉ sx := hsx ▸ finset.not_mem_erase x s, rw [← finset.insert_erase hx, hsx, finset.card_insert_of_not_mem this, nat.add_sub_cancel], clear hx hsx s, revert this, apply sx.induction_on, { intros hx, rw [finset.prod_empty, nat_degree_one], refl }, { intros y s hys ih hx, rw [finset.mem_insert, not_or_distrib] at hx, have h1 : C (x - y)⁻¹ ≠ C 0 := λ h, hx.1 (eq_of_sub_eq_zero $ inv_eq_zero.1 $ C_inj.1 h), have h2 : X ^ 1 - C y ≠ 0 := by convert X_pow_sub_C_ne_zero zero_lt_one y, rw C_0 at h1, rw pow_one at h2, rw [finset.prod_insert hys, nat_degree_mul (mul_ne_zero h1 h2), ih hx.2, finset.card_insert_of_not_mem hys, nat_degree_mul h1 h2, nat_degree_C, zero_add, nat_degree, degree_X_sub_C, add_comm], refl, rw [ne, finset.prod_eq_zero_iff], rintro ⟨z, hzs, hz⟩, rw mul_eq_zero at hz, cases hz with hz hz, { rw [← C_0, C_inj, inv_eq_zero, sub_eq_zero] at hz, exact hx.2 (hz.symm ▸ hzs) }, { rw ← pow_one (X : polynomial F) at hz, exact X_pow_sub_C_ne_zero zero_lt_one _ hz } } end variables (f : (↑s : set F) → F) /-- Lagrange interpolation: given a finset `s` and a function `f : s → F`, `interpolate s f` is the unique polynomial of degree `< s.card` that takes value `f x` on all `x` in `s`. -/ def interpolate : polynomial F := ∑ x in s.attach, C (f x) * basis s x @[simp] theorem interpolate_empty (f) : interpolate (∅ : finset F) f = 0 := rfl @[simp] theorem eval_interpolate (x) (H : x ∈ s) : eval x (interpolate s f) = f ⟨x, H⟩ := begin rw [interpolate, ← finset.sum_hom _ (eval x), finset.sum_eq_single (⟨x, H⟩ : { x // x ∈ s })], { rw [eval_mul, eval_C, subtype.coe_mk, eval_basis_self, mul_one] }, { rintros ⟨y, hy⟩ _ hyx, rw [eval_mul, subtype.coe_mk, eval_basis_ne s y x H, mul_zero], { rintros rfl, exact hyx rfl } }, { intro h, exact absurd (finset.mem_attach _ _) h } end theorem degree_interpolate_lt : (interpolate s f).degree < s.card := if H : s = ∅ then by { subst H, rw [interpolate_empty, degree_zero], exact with_bot.bot_lt_coe _ } else lt_of_le_of_lt (degree_sum_le _ _) $ (finset.sup_lt_iff $ with_bot.bot_lt_coe s.card).2 $ λ b _, calc (C (f b) * basis s b).degree ≤ (C (f b)).degree + (basis s b).degree : degree_mul_le _ _ ... ≤ 0 + (basis s b).degree : add_le_add_right degree_C_le _ ... = (basis s b).degree : zero_add _ ... ≤ (basis s b).nat_degree : degree_le_nat_degree ... = (s.card - 1 : ℕ) : by { rw nat_degree_basis s b b.2 } ... < s.card : with_bot.coe_lt_coe.2 (nat.pred_lt $ mt finset.card_eq_zero.1 H) /-- Linear version of `interpolate`. -/ def linterpolate : ((↑s : set F) → F) →ₗ[F] polynomial F := { to_fun := interpolate s, map_add' := λ f g, by { simp_rw [interpolate, ← finset.sum_add_distrib, ← add_mul, ← C_add], refl }, map_smul' := λ c f, by { simp_rw [interpolate, finset.smul_sum, C_mul', smul_smul], refl } } @[simp] lemma interpolate_add (f g) : interpolate s (f + g) = interpolate s f + interpolate s g := (linterpolate s).map_add f g @[simp] lemma interpolate_zero : interpolate s 0 = 0 := (linterpolate s).map_zero @[simp] lemma interpolate_neg (f) : interpolate s (-f) = -interpolate s f := (linterpolate s).map_neg f @[simp] lemma interpolate_sub (f g) : interpolate s (f - g) = interpolate s f - interpolate s g := (linterpolate s).map_sub f g @[simp] lemma interpolate_smul (c : F) (f) : interpolate s (c • f) = c • interpolate s f := (linterpolate s).map_smul c f theorem eq_zero_of_eval_eq_zero {f : polynomial F'} (hf1 : f.degree < s'.card) (hf2 : ∀ x ∈ s', f.eval x = 0) : f = 0 := by_contradiction $ λ hf3, not_le_of_lt hf1 $ calc (s'.card : with_bot ℕ) ≤ f.roots.to_finset.card : with_bot.coe_le_coe.2 $ finset.card_le_of_subset $ λ x hx, (multiset.mem_to_finset).mpr $ (mem_roots hf3).2 $ hf2 x hx ... ≤ f.roots.card : with_bot.coe_le_coe.2 $ f.roots.to_finset_card_le ... ≤ f.degree : card_roots hf3 theorem eq_of_eval_eq {f g : polynomial F'} (hf : f.degree < s'.card) (hg : g.degree < s'.card) (hfg : ∀ x ∈ s', f.eval x = g.eval x) : f = g := eq_of_sub_eq_zero $ eq_zero_of_eval_eq_zero s' (lt_of_le_of_lt (degree_sub_le f g) $ max_lt hf hg) (λ x hx, by rw [eval_sub, hfg x hx, sub_self]) theorem eq_interpolate (f : polynomial F) (hf : f.degree < s.card) : interpolate s (λ x, f.eval x) = f := eq_of_eval_eq s (degree_interpolate_lt s _) hf $ λ x hx, eval_interpolate s _ x hx /-- Lagrange interpolation induces isomorphism between functions from `s` and polynomials of degree less than `s.card`. -/ def fun_equiv_degree_lt : degree_lt F s.card ≃ₗ[F] ((↑s : set F) → F) := { to_fun := λ f x, f.1.eval x, map_add' := λ f g, funext $ λ x, eval_add, map_smul' := λ c f, funext $ λ x, by { rw [pi.smul_apply, smul_eq_mul, ← @eval_C F c _ x, ← eval_mul, eval_C, C_mul'], refl }, inv_fun := λ f, ⟨interpolate s f, mem_degree_lt.2 $ degree_interpolate_lt s f⟩, left_inv := λ f, subtype.eq $ eq_interpolate s f $ mem_degree_lt.1 f.2, right_inv := λ f, funext $ λ ⟨x, hx⟩, eval_interpolate s f x hx } end lagrange
afda772d936d6bdfd1ef7d0b433553344ca1694f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/pairwise_auto.lean
b352f6f770a5d759bea9bda21d0b95427f328058
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
10,835
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.basic import Mathlib.PostPort universes u v namespace Mathlib namespace list /- pairwise relation (generalized no duplicate) -/ theorem pairwise_iff {α : Type u} (R : α → α → Prop) : ∀ (ᾰ : List α), pairwise R ᾰ ↔ ᾰ = [] ∨ Exists fun {a : α} => Exists fun {l : List α} => (∀ (a' : α), a' ∈ l → R a a') ∧ pairwise R l ∧ ᾰ = a :: l := sorry theorem rel_of_pairwise_cons {α : Type u} {R : α → α → Prop} {a : α} {l : List α} (p : pairwise R (a :: l)) {a' : α} : a' ∈ l → R a a' := and.left (iff.mp pairwise_cons p) theorem pairwise_of_pairwise_cons {α : Type u} {R : α → α → Prop} {a : α} {l : List α} (p : pairwise R (a :: l)) : pairwise R l := and.right (iff.mp pairwise_cons p) theorem pairwise.tail {α : Type u} {R : α → α → Prop} {l : List α} (p : pairwise R l) : pairwise R (tail l) := sorry theorem pairwise.imp_of_mem {α : Type u} {R : α → α → Prop} {S : α → α → Prop} {l : List α} (H : ∀ {a b : α}, a ∈ l → b ∈ l → R a b → S a b) (p : pairwise R l) : pairwise S l := sorry theorem pairwise.imp {α : Type u} {R : α → α → Prop} {S : α → α → Prop} (H : ∀ (a b : α), R a b → S a b) {l : List α} : pairwise R l → pairwise S l := pairwise.imp_of_mem fun (a b : α) (_x : a ∈ l) (_x : b ∈ l) => H a b theorem pairwise.and {α : Type u} {R : α → α → Prop} {S : α → α → Prop} {l : List α} : pairwise (fun (a b : α) => R a b ∧ S a b) l ↔ pairwise R l ∧ pairwise S l := sorry theorem pairwise.imp₂ {α : Type u} {R : α → α → Prop} {S : α → α → Prop} {T : α → α → Prop} (H : ∀ (a b : α), R a b → S a b → T a b) {l : List α} (hR : pairwise R l) (hS : pairwise S l) : pairwise T l := pairwise.imp (fun (a b : α) => And._oldrec (H a b)) (iff.mpr pairwise.and { left := hR, right := hS }) theorem pairwise.iff_of_mem {α : Type u} {R : α → α → Prop} {S : α → α → Prop} {l : List α} (H : ∀ {a b : α}, a ∈ l → b ∈ l → (R a b ↔ S a b)) : pairwise R l ↔ pairwise S l := { mp := pairwise.imp_of_mem fun (a b : α) (m : a ∈ l) (m' : b ∈ l) => iff.mp (H m m'), mpr := pairwise.imp_of_mem fun (a b : α) (m : a ∈ l) (m' : b ∈ l) => iff.mpr (H m m') } theorem pairwise.iff {α : Type u} {R : α → α → Prop} {S : α → α → Prop} (H : ∀ (a b : α), R a b ↔ S a b) {l : List α} : pairwise R l ↔ pairwise S l := pairwise.iff_of_mem fun (a b : α) (_x : a ∈ l) (_x : b ∈ l) => H a b theorem pairwise_of_forall {α : Type u} {R : α → α → Prop} {l : List α} (H : ∀ (x y : α), R x y) : pairwise R l := sorry theorem pairwise.and_mem {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R l ↔ pairwise (fun (x y : α) => x ∈ l ∧ y ∈ l ∧ R x y) l := sorry theorem pairwise.imp_mem {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R l ↔ pairwise (fun (x y : α) => x ∈ l → y ∈ l → R x y) l := sorry theorem pairwise_of_sublist {α : Type u} {R : α → α → Prop} {l₁ : List α} {l₂ : List α} : l₁ <+ l₂ → pairwise R l₂ → pairwise R l₁ := sorry theorem forall_of_forall_of_pairwise {α : Type u} {R : α → α → Prop} (H : symmetric R) {l : List α} (H₁ : ∀ (x : α), x ∈ l → R x x) (H₂ : pairwise R l) (x : α) : x ∈ l → ∀ (y : α), y ∈ l → R x y := sorry theorem forall_of_pairwise {α : Type u} {R : α → α → Prop} (H : symmetric R) {l : List α} (hl : pairwise R l) (a : α) : a ∈ l → ∀ (b : α), b ∈ l → a ≠ b → R a b := forall_of_forall_of_pairwise (fun (a b : α) (h : a ≠ b → R a b) (hne : b ≠ a) => H (h (ne.symm hne))) (fun (_x : α) (_x_1 : _x ∈ l) (h : _x ≠ _x) => false.elim (h rfl)) (pairwise.imp (fun (_x _x_1 : α) (h : R _x _x_1) (_x : _x ≠ _x_1) => h) hl) theorem pairwise_singleton {α : Type u} (R : α → α → Prop) (a : α) : pairwise R [a] := sorry theorem pairwise_pair {α : Type u} {R : α → α → Prop} {a : α} {b : α} : pairwise R [a, b] ↔ R a b := sorry theorem pairwise_append {α : Type u} {R : α → α → Prop} {l₁ : List α} {l₂ : List α} : pairwise R (l₁ ++ l₂) ↔ pairwise R l₁ ∧ pairwise R l₂ ∧ ∀ (x : α), x ∈ l₁ → ∀ (y : α), y ∈ l₂ → R x y := sorry theorem pairwise_append_comm {α : Type u} {R : α → α → Prop} (s : symmetric R) {l₁ : List α} {l₂ : List α} : pairwise R (l₁ ++ l₂) ↔ pairwise R (l₂ ++ l₁) := sorry theorem pairwise_middle {α : Type u} {R : α → α → Prop} (s : symmetric R) {a : α} {l₁ : List α} {l₂ : List α} : pairwise R (l₁ ++ a :: l₂) ↔ pairwise R (a :: (l₁ ++ l₂)) := sorry theorem pairwise_map {α : Type u} {β : Type v} {R : α → α → Prop} (f : β → α) {l : List β} : pairwise R (map f l) ↔ pairwise (fun (a b : β) => R (f a) (f b)) l := sorry theorem pairwise_of_pairwise_map {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → β) (H : ∀ (a b : α), S (f a) (f b) → R a b) {l : List α} (p : pairwise S (map f l)) : pairwise R l := pairwise.imp H (iff.mp (pairwise_map f) p) theorem pairwise_map_of_pairwise {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → β) (H : ∀ (a b : α), R a b → S (f a) (f b)) {l : List α} (p : pairwise R l) : pairwise S (map f l) := iff.mpr (pairwise_map f) (pairwise.imp H p) theorem pairwise_filter_map {α : Type u} {β : Type v} {R : α → α → Prop} (f : β → Option α) {l : List β} : pairwise R (filter_map f l) ↔ pairwise (fun (a a' : β) => ∀ (b : α), b ∈ f a → ∀ (b' : α), b' ∈ f a' → R b b') l := sorry theorem pairwise_filter_map_of_pairwise {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → Option β) (H : ∀ (a a' : α), R a a' → ∀ (b : β), b ∈ f a → ∀ (b' : β), b' ∈ f a' → S b b') {l : List α} (p : pairwise R l) : pairwise S (filter_map f l) := iff.mpr (pairwise_filter_map f) (pairwise.imp H p) theorem pairwise_filter {α : Type u} {R : α → α → Prop} (p : α → Prop) [decidable_pred p] {l : List α} : pairwise R (filter p l) ↔ pairwise (fun (x y : α) => p x → p y → R x y) l := sorry theorem pairwise_filter_of_pairwise {α : Type u} {R : α → α → Prop} (p : α → Prop) [decidable_pred p] {l : List α} : pairwise R l → pairwise R (filter p l) := pairwise_of_sublist (filter_sublist l) theorem pairwise_pmap {α : Type u} {β : Type v} {R : α → α → Prop} {p : β → Prop} {f : (b : β) → p b → α} {l : List β} (h : ∀ (x : β), x ∈ l → p x) : pairwise R (pmap f l h) ↔ pairwise (fun (b₁ b₂ : β) => ∀ (h₁ : p b₁) (h₂ : p b₂), R (f b₁ h₁) (f b₂ h₂)) l := sorry theorem pairwise.pmap {α : Type u} {β : Type v} {R : α → α → Prop} {l : List α} (hl : pairwise R l) {p : α → Prop} {f : (a : α) → p a → β} (h : ∀ (x : α), x ∈ l → p x) {S : β → β → Prop} (hS : ∀ {x : α} (hx : p x) {y : α} (hy : p y), R x y → S (f x hx) (f y hy)) : pairwise S (pmap f l h) := iff.mpr (pairwise_pmap h) (pairwise.imp_of_mem (fun (a b : α) (ᾰ : a ∈ l) (ᾰ_1 : b ∈ l) (ᾰ_2 : R a b) (h₁ : p a) (h₂ : p b) => hS h₁ h₂ ᾰ_2) hl) theorem pairwise_join {α : Type u} {R : α → α → Prop} {L : List (List α)} : pairwise R (join L) ↔ (∀ (l : List α), l ∈ L → pairwise R l) ∧ pairwise (fun (l₁ l₂ : List α) => ∀ (x : α), x ∈ l₁ → ∀ (y : α), y ∈ l₂ → R x y) L := sorry @[simp] theorem pairwise_reverse {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R (reverse l) ↔ pairwise (fun (x y : α) => R y x) l := sorry theorem pairwise_iff_nth_le {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R l ↔ ∀ (i j : ℕ) (h₁ : j < length l) (h₂ : i < j), R (nth_le l i (lt_trans h₂ h₁)) (nth_le l j h₁) := sorry theorem pairwise_sublists' {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R l → pairwise (lex (function.swap R)) (sublists' l) := sorry theorem pairwise_sublists {α : Type u} {R : α → α → Prop} {l : List α} (H : pairwise R l) : pairwise (fun (l₁ l₂ : List α) => lex R (reverse l₁) (reverse l₂)) (sublists l) := sorry /- pairwise reduct -/ @[simp] theorem pw_filter_nil {α : Type u} {R : α → α → Prop} [DecidableRel R] : pw_filter R [] = [] := rfl @[simp] theorem pw_filter_cons_of_pos {α : Type u} {R : α → α → Prop} [DecidableRel R] {a : α} {l : List α} (h : ∀ (b : α), b ∈ pw_filter R l → R a b) : pw_filter R (a :: l) = a :: pw_filter R l := if_pos h @[simp] theorem pw_filter_cons_of_neg {α : Type u} {R : α → α → Prop} [DecidableRel R] {a : α} {l : List α} (h : ¬∀ (b : α), b ∈ pw_filter R l → R a b) : pw_filter R (a :: l) = pw_filter R l := if_neg h theorem pw_filter_map {α : Type u} {β : Type v} {R : α → α → Prop} [DecidableRel R] (f : β → α) (l : List β) : pw_filter R (map f l) = map f (pw_filter (fun (x y : β) => R (f x) (f y)) l) := sorry theorem pw_filter_sublist {α : Type u} {R : α → α → Prop} [DecidableRel R] (l : List α) : pw_filter R l <+ l := sorry theorem pw_filter_subset {α : Type u} {R : α → α → Prop} [DecidableRel R] (l : List α) : pw_filter R l ⊆ l := sublist.subset (pw_filter_sublist l) theorem pairwise_pw_filter {α : Type u} {R : α → α → Prop} [DecidableRel R] (l : List α) : pairwise R (pw_filter R l) := sorry theorem pw_filter_eq_self {α : Type u} {R : α → α → Prop} [DecidableRel R] {l : List α} : pw_filter R l = l ↔ pairwise R l := sorry @[simp] theorem pw_filter_idempotent {α : Type u} {R : α → α → Prop} [DecidableRel R] {l : List α} : pw_filter R (pw_filter R l) = pw_filter R l := iff.mpr pw_filter_eq_self (pairwise_pw_filter l) theorem forall_mem_pw_filter {α : Type u} {R : α → α → Prop} [DecidableRel R] (neg_trans : ∀ {x y z : α}, R x z → R x y ∨ R y z) (a : α) (l : List α) : (∀ (b : α), b ∈ pw_filter R l → R a b) ↔ ∀ (b : α), b ∈ l → R a b := sorry end Mathlib
150538370de750475017c16f6a05bbef4cfa5f6c
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/analysis/calculus/local_extr.lean
2fcb5e3c6cd85afbdcf73e4fe328e74e04de2597
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,926
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.local_extr import analysis.calculus.deriv /-! # Local extrema of smooth functions ## Main definitions In a real normed space `E` we define `pos_tangent_cone_at (s : set E) (x : E)`. This would be the same as `tangent_cone_at ℝ≥0 s x` if we had a theory of normed semifields. This set is used in the proof of Fermat's Theorem (see below), and can be used to formalize [Lagrange multipliers](https://en.wikipedia.org/wiki/Lagrange_multiplier) and/or [Karush–Kuhn–Tucker conditions](https://en.wikipedia.org/wiki/Karush–Kuhn–Tucker_conditions). ## Main statements For each theorem name listed below, we also prove similar theorems for `min`, `extr` (if applicable)`, and `(f)deriv` instead of `has_fderiv`. * `is_local_max_on.has_fderiv_within_at_nonpos` : `f' y ≤ 0` whenever `a` is a local maximum of `f` on `s`, `f` has derivative `f'` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`. * `is_local_max_on.has_fderiv_within_at_eq_zero` : In the settings of the previous theorem, if both `y` and `-y` belong to the positive tangent cone, then `f' y = 0`. * `is_local_max.has_fderiv_at_eq_zero` : [Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points)), the derivative of a differentiable function at a local extremum point equals zero. * `exists_has_deriv_at_eq_zero` : [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem): given a function `f` continuous on `[a, b]` and differentiable on `(a, b)`, there exists `c ∈ (a, b)` such that `f' c = 0`. ## Implementation notes For each mathematical fact we prove several versions of its formalization: * for maxima and minima; * using `has_fderiv*`/`has_deriv*` or `fderiv*`/`deriv*`. For the `fderiv*`/`deriv*` versions we omit the differentiability condition whenever it is possible due to the fact that `fderiv` and `deriv` are defined to be zero for non-differentiable functions. ## References * [Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points)); * [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem); * [Tangent cone](https://en.wikipedia.org/wiki/Tangent_cone); ## Tags local extremum, Fermat's Theorem, Rolle's Theorem -/ universes u v open filter set open_locale topological_space classical section module variables {E : Type u} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {a : E} {f' : E →L[ℝ] ℝ} /-- "Positive" tangent cone to `s` at `x`; the only difference from `tangent_cone_at` is that we require `c n → ∞` instead of `∥c n∥ → ∞`. One can think about `pos_tangent_cone_at` as `tangent_cone_at nnreal` but we have no theory of normed semifields yet. -/ def pos_tangent_cone_at (s : set E) (x : E) : set E := {y : E | ∃(c : ℕ → ℝ) (d : ℕ → E), (∀ᶠ n in at_top, x + d n ∈ s) ∧ (tendsto c at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))} lemma pos_tangent_cone_at_mono : monotone (λ s, pos_tangent_cone_at s a) := begin rintros s t hst y ⟨c, d, hd, hc, hcd⟩, exact ⟨c, d, mem_sets_of_superset hd $ λ h hn, hst hn, hc, hcd⟩ end lemma mem_pos_tangent_cone_at_of_segment_subset {s : set E} {x y : E} (h : segment x y ⊆ s) : y - x ∈ pos_tangent_cone_at s x := begin let c := λn:ℕ, (2:ℝ)^n, let d := λn:ℕ, (c n)⁻¹ • (y-x), refine ⟨c, d, filter.univ_mem_sets' (λn, h _), tendsto_pow_at_top_at_top_of_one_lt one_lt_two, _⟩, show x + d n ∈ segment x y, { rw segment_eq_image', refine ⟨(c n)⁻¹, ⟨_, _⟩, rfl⟩, exacts [inv_nonneg.2 (pow_nonneg zero_le_two _), inv_le_one (one_le_pow_of_one_le one_le_two _)] }, show tendsto (λ n, c n • d n) at_top (𝓝 (y - x)), { convert tendsto_const_nhds, ext n, simp only [d, smul_smul], rw [mul_inv_cancel, one_smul], exact pow_ne_zero _ two_ne_zero } end lemma mem_pos_tangent_cone_at_of_segment_subset' {s : set E} {x y : E} (h : segment x (x + y) ⊆ s) : y ∈ pos_tangent_cone_at s x := by simpa only [add_sub_cancel'] using mem_pos_tangent_cone_at_of_segment_subset h lemma pos_tangent_cone_at_univ : pos_tangent_cone_at univ a = univ := eq_univ_of_forall $ λ x, mem_pos_tangent_cone_at_of_segment_subset' (subset_univ _) /-- If `f` has a local max on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.has_fderiv_within_at_nonpos {s : set E} (h : is_local_max_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : f' y ≤ 0 := begin rcases hy with ⟨c, d, hd, hc, hcd⟩, have hc' : tendsto (λ n, ∥c n∥) at_top at_top, from tendsto_at_top_mono (λ n, le_abs_self _) hc, refine le_of_tendsto (hf.lim at_top hd hc' hcd) _, replace hd : tendsto (λ n, a + d n) at_top (𝓝[s] (a + 0)), from tendsto_inf.2 ⟨tendsto_const_nhds.add (tangent_cone_at.lim_zero _ hc' hcd), by rwa tendsto_principal⟩, rw [add_zero] at hd, replace h : ∀ᶠ n in at_top, f (a + d n) ≤ f a, from mem_map.1 (hd h), replace hc : ∀ᶠ n in at_top, 0 ≤ c n, from mem_map.1 (hc (mem_at_top (0:ℝ))), filter_upwards [h, hc], simp only [smul_eq_mul, mem_preimage, subset_def], assume n hnf hn, exact mul_nonpos_of_nonneg_of_nonpos hn (sub_nonpos.2 hnf) end /-- If `f` has a local max on `s` at `a` and `y` belongs to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.fderiv_within_nonpos {s : set E} (h : is_local_max_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y ≤ 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_nonpos hf.has_fderiv_within_at hy else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.has_fderiv_within_at_eq_zero {s : set E} (h : is_local_max_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : f' y = 0 := le_antisymm (h.has_fderiv_within_at_nonpos hf hy) $ by simpa using h.has_fderiv_within_at_nonpos hf hy' /-- If `f` has a local max on `s` at `a` and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y = 0`. -/ lemma is_local_max_on.fderiv_within_eq_zero {s : set E} (h : is_local_max_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y = 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_eq_zero hf.has_fderiv_within_at hy hy' else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- If `f` has a local min on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`, then `0 ≤ f' y`. -/ lemma is_local_min_on.has_fderiv_within_at_nonneg {s : set E} (h : is_local_min_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : 0 ≤ f' y := by simpa using h.neg.has_fderiv_within_at_nonpos hf.neg hy /-- If `f` has a local min on `s` at `a` and `y` belongs to the positive tangent cone of `s` at `a`, then `0 ≤ f' y`. -/ lemma is_local_min_on.fderiv_within_nonneg {s : set E} (h : is_local_min_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : (0:ℝ) ≤ (fderiv_within ℝ f s a : E → ℝ) y := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_nonneg hf.has_fderiv_within_at hy else by { rw [fderiv_within_zero_of_not_differentiable_within_at hf], refl } /-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_min_on.has_fderiv_within_at_eq_zero {s : set E} (h : is_local_min_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : f' y = 0 := by simpa using h.neg.has_fderiv_within_at_eq_zero hf.neg hy hy' /-- If `f` has a local min on `s` at `a` and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y = 0`. -/ lemma is_local_min_on.fderiv_within_eq_zero {s : set E} (h : is_local_min_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y = 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_eq_zero hf.has_fderiv_within_at hy hy' else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.has_fderiv_at_eq_zero (h : is_local_min f a) (hf : has_fderiv_at f f' a) : f' = 0 := begin ext y, apply (h.on univ).has_fderiv_within_at_eq_zero hf.has_fderiv_within_at; rw pos_tangent_cone_at_univ; apply mem_univ end /-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.fderiv_eq_zero (h : is_local_min f a) : fderiv ℝ f a = 0 := if hf : differentiable_at ℝ f a then h.has_fderiv_at_eq_zero hf.has_fderiv_at else fderiv_zero_of_not_differentiable_at hf /-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.has_fderiv_at_eq_zero (h : is_local_max f a) (hf : has_fderiv_at f f' a) : f' = 0 := neg_eq_zero.1 $ h.neg.has_fderiv_at_eq_zero hf.neg /-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.fderiv_eq_zero (h : is_local_max f a) : fderiv ℝ f a = 0 := if hf : differentiable_at ℝ f a then h.has_fderiv_at_eq_zero hf.has_fderiv_at else fderiv_zero_of_not_differentiable_at hf /-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.has_fderiv_at_eq_zero (h : is_local_extr f a) : has_fderiv_at f f' a → f' = 0 := h.elim is_local_min.has_fderiv_at_eq_zero is_local_max.has_fderiv_at_eq_zero /-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.fderiv_eq_zero (h : is_local_extr f a) : fderiv ℝ f a = 0 := h.elim is_local_min.fderiv_eq_zero is_local_max.fderiv_eq_zero end module section real variables {f : ℝ → ℝ} {f' : ℝ} {a b : ℝ} /-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.has_deriv_at_eq_zero (h : is_local_min f a) (hf : has_deriv_at f f' a) : f' = 0 := by simpa using continuous_linear_map.ext_iff.1 (h.has_fderiv_at_eq_zero (has_deriv_at_iff_has_fderiv_at.1 hf)) 1 /-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.deriv_eq_zero (h : is_local_min f a) : deriv f a = 0 := if hf : differentiable_at ℝ f a then h.has_deriv_at_eq_zero hf.has_deriv_at else deriv_zero_of_not_differentiable_at hf /-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.has_deriv_at_eq_zero (h : is_local_max f a) (hf : has_deriv_at f f' a) : f' = 0 := neg_eq_zero.1 $ h.neg.has_deriv_at_eq_zero hf.neg /-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.deriv_eq_zero (h : is_local_max f a) : deriv f a = 0 := if hf : differentiable_at ℝ f a then h.has_deriv_at_eq_zero hf.has_deriv_at else deriv_zero_of_not_differentiable_at hf /-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.has_deriv_at_eq_zero (h : is_local_extr f a) : has_deriv_at f f' a → f' = 0 := h.elim is_local_min.has_deriv_at_eq_zero is_local_max.has_deriv_at_eq_zero /-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.deriv_eq_zero (h : is_local_extr f a) : deriv f a = 0 := h.elim is_local_min.deriv_eq_zero is_local_max.deriv_eq_zero end real section Rolle variables (f f' : ℝ → ℝ) {a b : ℝ} /-- A continuous function on a closed interval with `f a = f b` takes either its maximum or its minimum value at a point in the interior of the interval. -/ lemma exists_Ioo_extr_on_Icc (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, is_extr_on f (Icc a b) c := begin have ne : (Icc a b).nonempty, from nonempty_Icc.2 (le_of_lt hab), -- Consider absolute min and max points obtain ⟨c, cmem, cle⟩ : ∃ c ∈ Icc a b, ∀ x ∈ Icc a b, f c ≤ f x, from compact_Icc.exists_forall_le ne hfc, obtain ⟨C, Cmem, Cge⟩ : ∃ C ∈ Icc a b, ∀ x ∈ Icc a b, f x ≤ f C, from compact_Icc.exists_forall_ge ne hfc, by_cases hc : f c = f a, { by_cases hC : f C = f a, { have : ∀ x ∈ Icc a b, f x = f a, from λ x hx, le_antisymm (hC ▸ Cge x hx) (hc ▸ cle x hx), -- `f` is a constant, so we can take any point in `Ioo a b` rcases exists_between hab with ⟨c', hc'⟩, refine ⟨c', hc', or.inl _⟩, assume x hx, rw [mem_set_of_eq, this x hx, ← hC], exact Cge c' ⟨le_of_lt hc'.1, le_of_lt hc'.2⟩ }, { refine ⟨C, ⟨lt_of_le_of_ne Cmem.1 $ mt _ hC, lt_of_le_of_ne Cmem.2 $ mt _ hC⟩, or.inr Cge⟩, exacts [λ h, by rw h, λ h, by rw [h, hfI]] } }, { refine ⟨c, ⟨lt_of_le_of_ne cmem.1 $ mt _ hc, lt_of_le_of_ne cmem.2 $ mt _ hc⟩, or.inl cle⟩, exacts [λ h, by rw h, λ h, by rw [h, hfI]] } end /-- A continuous function on a closed interval with `f a = f b` has a local extremum at some point of the corresponding open interval. -/ lemma exists_local_extr_Ioo (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, is_local_extr f c := let ⟨c, cmem, hc⟩ := exists_Ioo_extr_on_Icc f hab hfc hfI in ⟨c, cmem, hc.is_local_extr $ Icc_mem_nhds cmem.1 cmem.2⟩ /-- Rolle's Theorem `has_deriv_at` version -/ lemma exists_has_deriv_at_eq_zero (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) : ∃ c ∈ Ioo a b, f' c = 0 := let ⟨c, cmem, hc⟩ := exists_local_extr_Ioo f hab hfc hfI in ⟨c, cmem, hc.has_deriv_at_eq_zero $ hff' c cmem⟩ /-- Rolle's Theorem `deriv` version -/ lemma exists_deriv_eq_zero (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, deriv f c = 0 := let ⟨c, cmem, hc⟩ := exists_local_extr_Ioo f hab hfc hfI in ⟨c, cmem, hc.deriv_eq_zero⟩ variables {f f'} {l : ℝ} /-- Rolle's Theorem, a version for a function on an open interval: if `f` has derivative `f'` on `(a, b)` and has the same limit `l` at `𝓝[Ioi a] a` and `𝓝[Iio b] b`, then `f' c = 0` for some `c ∈ (a, b)`. -/ lemma exists_has_deriv_at_eq_zero' (hab : a < b) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 l)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 l)) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) : ∃ c ∈ Ioo a b, f' c = 0 := begin have : continuous_on f (Ioo a b) := λ x hx, (hff' x hx).continuous_at.continuous_within_at, have hcont := continuous_on_Icc_extend_from_Ioo hab this hfa hfb, obtain ⟨c, hc, hcextr⟩ : ∃ c ∈ Ioo a b, is_local_extr (extend_from (Ioo a b) f) c, { apply exists_local_extr_Ioo _ hab hcont, rw eq_lim_at_right_extend_from_Ioo hab hfb, exact eq_lim_at_left_extend_from_Ioo hab hfa }, use [c, hc], apply (hcextr.congr _).has_deriv_at_eq_zero (hff' c hc), rw eventually_eq_iff_exists_mem, exact ⟨Ioo a b, Ioo_mem_nhds hc.1 hc.2, extend_from_extends this⟩ end /-- Rolle's Theorem, a version for a function on an open interval: if `f` has the same limit `l` at `𝓝[Ioi a] a` and `𝓝[Iio b] b`, then `deriv f c = 0` for some `c ∈ (a, b)`. This version does not require differentiability of `f` because we define `deriv f c = 0` whenever `f` is not differentiable at `c`. -/ lemma exists_deriv_eq_zero' (hab : a < b) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 l)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 l)) : ∃ c ∈ Ioo a b, deriv f c = 0 := classical.by_cases (assume h : ∀ x ∈ Ioo a b, differentiable_at ℝ f x, show ∃ c ∈ Ioo a b, deriv f c = 0, from exists_has_deriv_at_eq_zero' hab hfa hfb (λ x hx, (h x hx).has_deriv_at)) (assume h : ¬∀ x ∈ Ioo a b, differentiable_at ℝ f x, have h : ∃ x, x ∈ Ioo a b ∧ ¬differentiable_at ℝ f x, by { push_neg at h, exact h }, let ⟨c, hc, hcdiff⟩ := h in ⟨c, hc, deriv_zero_of_not_differentiable_at hcdiff⟩) end Rolle
b1ff6a51e9e626aeada2bede8951cbe545e85a4d
0d2e44896897eda703992595d71a0b19ed30b8a1
/uexp/tactic_paper_supplemental_material/monoid_cancellation/cancellation_solver_opt.lean
06e08f8920a1bf3600a7e24de465c39759e24fc8
[ "BSD-2-Clause" ]
permissive
wwombat/Cosette
a87312aabefdb53ea8b67c37731bd58c7485afb6
4c5dc6172e24d3546c9818ac1fad06f72fe1c991
refs/heads/master
1,619,479,568,051
1,520,292,502,000
1,520,292,502,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,041
lean
import .cancellation_solver local infix * := star open tactic expr namespace opt set_option eqn_compiler.max_steps 20000 meta def iter_right : tactic unit := do t ← target, match t with | `(%%a = o * %%b) := apply `(@plus_unit_c %%a %%b) | `((%%a * %%b) * %%c = %%d) := (apply `(@plus_assoc_c1 %%a %%b %%c %%d) >> iter_right) <|> (apply `(@plus_assoc_c2 %%a %%b %%c %%d) >> iter_right) | `((%%a * %%b) = (%%c * %%d)) := apply `(@plus_cancel %%a %%b %%c %%d) >> reflexivity | _ := failed end meta def iter_left : tactic unit := do t ← target, match t with | `(o * %%a = %%b) := apply `(@plus_unit_p %%a %%b) | `((%%a * %%b) * %%c = %%d) := (apply `(@plus_assoc_p1 %%a %%b %%c %%d) >> iter_left) <|> (apply `(@plus_assoc_p2 %%a %%b %%c %%d) >> iter_left) | _ := iter_right <|> applyc `plus_comm_p >> iter_right end meta def cancel := iter_left <|> applyc `plus_comm_p >> iter_left meta def solve := repeat $ reflexivity <|> cancel end opt
41c5b2e8ca6654f5e1ecbc504bd789e760acdda2
7cdf3413c097e5d36492d12cdd07030eb991d394
/src/game/world4/level7.lean
8d2e81c653c073c7b25a825328f7511bb261424a
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
817
lean
import game.world4.level6 -- hide namespace mynat -- hide /- # World 4 : Power World ## Level 7 : `pow_pow` -/ /- Boss level! What will the collectible be? -/ /- Lemma For all naturals $a$, $m$, $n$, we have $(a ^ m) ^ n = a ^ {mn}$. -/ lemma pow_pow (a m n : mynat) : (a ^ m) ^ n = a ^ (m * n) := begin [less_leaky] induction' n with t Ht, rw [mul_zero, pow_zero, pow_zero], refl, rw [pow_succ, Ht, mul_succ, pow_add], refl, end /- Apparently Lean can't find a collectible, even though you feel like you just finished power world so you must have proved *something*. What should the collectible for this level be called? -/ /- But what is this? It's one of those twists where there's another boss after the boss you thought was the final boss! Go to the next level! -/ end mynat -- hide
007fd8cb2067f1815ab94cd304bfe27fc7a43b80
46125763b4dbf50619e8846a1371029346f4c3db
/src/category_theory/limits/limits.lean
b48be9b3313abb810619f9cb3d5950323e9fd749
[ "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
36,825
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Mario Carneiro, Scott Morrison, Floris van Doorn -/ import category_theory.whiskering import category_theory.yoneda import category_theory.limits.cones import category_theory.eq_to_hom open category_theory category_theory.category category_theory.functor opposite namespace category_theory.limits universes v u u' u'' w -- declare the `v`'s first; see `category_theory.category` for an explanation -- See the notes at the top of cones.lean, explaining why we can't allow `J : Prop` here. variables {J K : Type v} [small_category J] [small_category K] variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 variables {F : J ⥤ C} /-- A cone `t` on `F` is a limit cone if each cone on `F` admits a unique cone morphism to `t`. -/ structure is_limit (t : cone F) := (lift : Π (s : cone F), s.X ⟶ t.X) (fac' : ∀ (s : cone F) (j : J), lift s ≫ t.π.app j = s.π.app j . obviously) (uniq' : ∀ (s : cone F) (m : s.X ⟶ t.X) (w : ∀ j : J, m ≫ t.π.app j = s.π.app j), m = lift s . obviously) restate_axiom is_limit.fac' attribute [simp] is_limit.fac restate_axiom is_limit.uniq' namespace is_limit instance subsingleton {t : cone F} : subsingleton (is_limit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ def lift_cone_morphism {t : cone F} (h : is_limit t) (s : cone F) : s ⟶ t := { hom := h.lift s } lemma uniq_cone_morphism {s t : cone F} (h : is_limit t) {f f' : s ⟶ t} : f = f' := have ∀ {g : s ⟶ t}, g = h.lift_cone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm def mk_cone_morphism {t : cone F} (lift : Π (s : cone F), s ⟶ t) (uniq' : ∀ (s : cone F) (m : s ⟶ t), m = lift s) : is_limit t := { lift := λ s, (lift s).hom, uniq' := λ s m w, have cone_morphism.mk m w = lift s, by apply uniq', congr_arg cone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique_up_to_iso {s t : cone F} (P : is_limit s) (Q : is_limit t) : s ≅ t := { hom := Q.lift_cone_morphism s, inv := P.lift_cone_morphism t, hom_inv_id' := P.uniq_cone_morphism, inv_hom_id' := Q.uniq_cone_morphism } def of_iso_limit {r t : cone F} (P : is_limit r) (i : r ≅ t) : is_limit t := is_limit.mk_cone_morphism (λ s, P.lift_cone_morphism s ≫ i.hom) (λ s m, by rw ←i.comp_inv_eq; apply P.uniq_cone_morphism) variables {t : cone F} lemma hom_lift (h : is_limit t) {W : C} (m : W ⟶ t.X) : m = h.lift { X := W, π := { app := λ b, m ≫ t.π.app b } } := h.uniq { X := W, π := { app := λ b, m ≫ t.π.app b } } m (λ b, rfl) /-- Two morphisms into a limit are equal if their compositions with each cone morphism are equal. -/ lemma hom_ext (h : is_limit t) {W : C} {f f' : W ⟶ t.X} (w : ∀ j, f ≫ t.π.app j = f' ≫ t.π.app j) : f = f' := by rw [h.hom_lift f, h.hom_lift f']; congr; exact funext w /-- The universal property of a limit cone: a map `W ⟶ X` is the same as a cone on `F` with vertex `W`. -/ def hom_iso (h : is_limit t) (W : C) : (W ⟶ t.X) ≅ ((const J).obj W ⟶ F) := { hom := λ f, (t.extend f).π, inv := λ π, h.lift { X := W, π := π }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_limit t) {W : C} (f : W ⟶ t.X) : (is_limit.hom_iso h W).hom f = (t.extend f).π := rfl /-- The limit of `F` represents the functor taking `W` to the set of cones on `F` with vertex `W`. -/ def nat_iso (h : is_limit t) : yoneda.obj t.X ≅ F.cones := nat_iso.of_components (λ W, is_limit.hom_iso h (unop W)) (by tidy). def hom_iso' (h : is_limit t) (W : C) : ((W ⟶ t.X) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j'} (f : j ⟶ j'), p j ≫ F.map f = p j' } := h.hom_iso W ≪≫ { hom := λ π, ⟨λ j, π.app j, λ j j' f, by convert ←(π.naturality f).symm; apply id_comp⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [id_comp], exact (p.2 f).symm end } } /-- If G : C → D is a faithful functor which sends t to a limit cone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cone F} {D : Type u'} [category.{v} D] (G : C ⥤ D) [faithful G] (ht : is_limit (G.map_cone t)) (lift : Π (s : cone F), s.X ⟶ t.X) (h : ∀ s, G.map (lift s) = ht.lift (G.map_cone s)) : is_limit t := { lift := lift, fac' := λ s j, by apply G.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } def iso_unique_cone_morphism {t : cone F} : is_limit t ≅ Π s, unique (s ⟶ t) := { hom := λ h s, { default := h.lift_cone_morphism s, uniq := λ _, h.uniq_cone_morphism }, inv := λ h, { lift := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cone_morphism.hom ((h s).uniq ⟨f, w⟩) } } namespace of_nat_iso variables {X : C} (h : yoneda.obj X ≅ F.cones) /-- If `F.cones` is represented by `X`, each morphism `f : Y ⟶ X` gives a cone with cone point `Y`. -/ def cone_of_hom {Y : C} (f : Y ⟶ X) : cone F := { X := Y, π := h.hom.app (op Y) f } /-- If `F.cones` is represented by `X`, each cone `s` gives a morphism `s.X ⟶ X`. -/ def hom_of_cone (s : cone F) : s.X ⟶ X := h.inv.app (op s.X) s.π @[simp] lemma cone_of_hom_of_cone (s : cone F) : cone_of_hom h (hom_of_cone h s) = s := begin dsimp [cone_of_hom, hom_of_cone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) (op s_X)) s_π, end @[simp] lemma hom_of_cone_of_hom {Y : C} (f : Y ⟶ X) : hom_of_cone h (cone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) (op Y)) f /-- If `F.cones` is represented by `X`, the cone corresponding to the identity morphism on `X` will be a limit cone. -/ def limit_cone : cone F := cone_of_hom h (𝟙 X) /-- If `F.cones` is represented by `X`, the cone corresponding to a morphism `f : Y ⟶ X` is the limit cone extended by `f`. -/ lemma cone_of_hom_fac {Y : C} (f : Y ⟶ X) : cone_of_hom h f = (limit_cone h).extend f := begin dsimp [cone_of_hom, limit_cone, cone.extend], congr, ext j, have t := congr_fun (h.hom.naturality f.op) (𝟙 X), dsimp at t, simp only [comp_id] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cones` is represented by `X`, any cone is the extension of the limit cone by the corresponding morphism. -/ lemma cone_fac (s : cone F) : (limit_cone h).extend (hom_of_cone h s) = s := begin rw ←cone_of_hom_of_cone h s, conv_lhs { simp only [hom_of_cone_of_hom] }, apply (cone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cones` is representable, then the cone corresponding to the identity morphism on the representing object is a limit cone. -/ def of_nat_iso {X : C} (h : yoneda.obj X ≅ F.cones) : is_limit (limit_cone h) := { lift := λ s, hom_of_cone h s, fac' := λ s j, begin have h := cone_fac h s, cases s, injection h with h₁ h₂, simp only [heq_iff_eq] at h₂, conv_rhs { rw ← h₂ }, refl, end, uniq' := λ s m w, begin rw ←hom_of_cone_of_hom h m, congr, rw cone_of_hom_fac, dsimp, cases s, congr, ext j, exact w j, end } end end is_limit /-- A cocone `t` on `F` is a colimit cocone if each cocone on `F` admits a unique cocone morphism from `t`. -/ structure is_colimit (t : cocone F) := (desc : Π (s : cocone F), t.X ⟶ s.X) (fac' : ∀ (s : cocone F) (j : J), t.ι.app j ≫ desc s = s.ι.app j . obviously) (uniq' : ∀ (s : cocone F) (m : t.X ⟶ s.X) (w : ∀ j : J, t.ι.app j ≫ m = s.ι.app j), m = desc s . obviously) restate_axiom is_colimit.fac' attribute [simp] is_colimit.fac restate_axiom is_colimit.uniq' namespace is_colimit instance subsingleton {t : cocone F} : subsingleton (is_colimit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ def desc_cocone_morphism {t : cocone F} (h : is_colimit t) (s : cocone F) : t ⟶ s := { hom := h.desc s } lemma uniq_cocone_morphism {s t : cocone F} (h : is_colimit t) {f f' : t ⟶ s} : f = f' := have ∀ {g : t ⟶ s}, g = h.desc_cocone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm def mk_cocone_morphism {t : cocone F} (desc : Π (s : cocone F), t ⟶ s) (uniq' : ∀ (s : cocone F) (m : t ⟶ s), m = desc s) : is_colimit t := { desc := λ s, (desc s).hom, uniq' := λ s m w, have cocone_morphism.mk m w = desc s, by apply uniq', congr_arg cocone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique_up_to_iso {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) : s ≅ t := { hom := P.desc_cocone_morphism t, inv := Q.desc_cocone_morphism s, hom_inv_id' := P.uniq_cocone_morphism, inv_hom_id' := Q.uniq_cocone_morphism } def of_iso_colimit {r t : cocone F} (P : is_colimit r) (i : r ≅ t) : is_colimit t := is_colimit.mk_cocone_morphism (λ s, i.inv ≫ P.desc_cocone_morphism s) (λ s m, by rw i.eq_inv_comp; apply P.uniq_cocone_morphism) variables {t : cocone F} lemma hom_desc (h : is_colimit t) {W : C} (m : t.X ⟶ W) : m = h.desc { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := by intros; erw [←assoc, t.ι.naturality, comp_id, comp_id] } } := h.uniq { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := _ } } m (λ b, rfl) /-- Two morphisms out of a colimit are equal if their compositions with each cocone morphism are equal. -/ lemma hom_ext (h : is_colimit t) {W : C} {f f' : t.X ⟶ W} (w : ∀ j, t.ι.app j ≫ f = t.ι.app j ≫ f') : f = f' := by rw [h.hom_desc f, h.hom_desc f']; congr; exact funext w /-- The universal property of a colimit cocone: a map `X ⟶ W` is the same as a cocone on `F` with vertex `W`. -/ def hom_iso (h : is_colimit t) (W : C) : (t.X ⟶ W) ≅ (F ⟶ (const J).obj W) := { hom := λ f, (t.extend f).ι, inv := λ ι, h.desc { X := W, ι := ι }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_colimit t) {W : C} (f : t.X ⟶ W) : (is_colimit.hom_iso h W).hom f = (t.extend f).ι := rfl /-- The colimit of `F` represents the functor taking `W` to the set of cocones on `F` with vertex `W`. -/ def nat_iso (h : is_colimit t) : coyoneda.obj (op t.X) ≅ F.cocones := nat_iso.of_components (is_colimit.hom_iso h) (by intros; ext; dsimp; rw ←assoc; refl) def hom_iso' (h : is_colimit t) (W : C) : ((t.X ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j' : J} (f : j ⟶ j'), F.map f ≫ p j' = p j } := h.hom_iso W ≪≫ { hom := λ ι, ⟨λ j, ι.app j, λ j j' f, by convert ←(ι.naturality f); apply comp_id⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [comp_id], exact (p.2 f) end } } /-- If G : C → D is a faithful functor which sends t to a colimit cocone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cocone F} {D : Type u'} [category.{v} D] (G : C ⥤ D) [faithful G] (ht : is_colimit (G.map_cocone t)) (desc : Π (s : cocone F), t.X ⟶ s.X) (h : ∀ s, G.map (desc s) = ht.desc (G.map_cocone s)) : is_colimit t := { desc := desc, fac' := λ s j, by apply G.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cocone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } def iso_unique_cocone_morphism {t : cocone F} : is_colimit t ≅ Π s, unique (t ⟶ s) := { hom := λ h s, { default := h.desc_cocone_morphism s, uniq := λ _, h.uniq_cocone_morphism }, inv := λ h, { desc := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cocone_morphism.hom ((h s).uniq ⟨f, w⟩) } } namespace of_nat_iso variables {X : C} (h : coyoneda.obj (op X) ≅ F.cocones) /-- If `F.cocones` is corepresented by `X`, each morphism `f : X ⟶ Y` gives a cocone with cone point `Y`. -/ def cocone_of_hom {Y : C} (f : X ⟶ Y) : cocone F := { X := Y, ι := h.hom.app Y f } /-- If `F.cocones` is corepresented by `X`, each cocone `s` gives a morphism `X ⟶ s.X`. -/ def hom_of_cocone (s : cocone F) : X ⟶ s.X := h.inv.app s.X s.ι @[simp] lemma cocone_of_hom_of_cocone (s : cocone F) : cocone_of_hom h (hom_of_cocone h s) = s := begin dsimp [cocone_of_hom, hom_of_cocone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) s_X) s_ι, end @[simp] lemma hom_of_cocone_of_hom {Y : C} (f : X ⟶ Y) : hom_of_cocone h (cocone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) Y) f /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to the identity morphism on `X` will be a colimit cocone. -/ def colimit_cocone : cocone F := cocone_of_hom h (𝟙 X) /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to a morphism `f : Y ⟶ X` is the colimit cocone extended by `f`. -/ lemma cocone_of_hom_fac {Y : C} (f : X ⟶ Y) : cocone_of_hom h f = (colimit_cocone h).extend f := begin dsimp [cocone_of_hom, colimit_cocone, cocone.extend], congr, ext j, have t := congr_fun (h.hom.naturality f) (𝟙 X), dsimp at t, simp only [id_comp] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cocones` is corepresented by `X`, any cocone is the extension of the colimit cocone by the corresponding morphism. -/ lemma cocone_fac (s : cocone F) : (colimit_cocone h).extend (hom_of_cocone h s) = s := begin rw ←cocone_of_hom_of_cocone h s, conv_lhs { simp only [hom_of_cocone_of_hom] }, apply (cocone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cocones` is corepresentable, then the cocone corresponding to the identity morphism on the representing object is a colimit cocone. -/ def of_nat_iso {X : C} (h : coyoneda.obj (op X) ≅ F.cocones) : is_colimit (colimit_cocone h) := { desc := λ s, hom_of_cocone h s, fac' := λ s j, begin have h := cocone_fac h s, cases s, injection h with h₁ h₂, simp only [heq_iff_eq] at h₂, conv_rhs { rw ← h₂ }, refl, end, uniq' := λ s m w, begin rw ←hom_of_cocone_of_hom h m, congr, rw cocone_of_hom_fac, dsimp, cases s, congr, ext j, exact w j, end } end end is_colimit section limit /-- `has_limit F` represents a particular chosen limit of the diagram `F`. -/ class has_limit (F : J ⥤ C) := (cone : cone F) (is_limit : is_limit cone) variables (J C) /-- `C` has limits of shape `J` if we have chosen a particular limit of every functor `F : J ⥤ C`. -/ class has_limits_of_shape := (has_limit : Π F : J ⥤ C, has_limit F) /-- `C` has all (small) limits if it has limits of every shape. -/ class has_limits := (has_limits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_limits_of_shape J C) variables {J C} @[priority 100] -- see Note [lower instance priority] instance has_limit_of_has_limits_of_shape {J : Type v} [small_category J] [H : has_limits_of_shape J C] (F : J ⥤ C) : has_limit F := has_limits_of_shape.has_limit F @[priority 100] -- see Note [lower instance priority] instance has_limits_of_shape_of_has_limits {J : Type v} [small_category J] [H : has_limits.{v} C] : has_limits_of_shape J C := has_limits.has_limits_of_shape C J /- Interface to the `has_limit` class. -/ def limit.cone (F : J ⥤ C) [has_limit F] : cone F := has_limit.cone F def limit (F : J ⥤ C) [has_limit F] := (limit.cone F).X def limit.π (F : J ⥤ C) [has_limit F] (j : J) : limit F ⟶ F.obj j := (limit.cone F).π.app j @[simp] lemma limit.cone_π {F : J ⥤ C} [has_limit F] (j : J) : (limit.cone F).π.app j = limit.π _ j := rfl @[simp] lemma limit.w (F : J ⥤ C) [has_limit F] {j j' : J} (f : j ⟶ j') : limit.π F j ≫ F.map f = limit.π F j' := (limit.cone F).w f def limit.is_limit (F : J ⥤ C) [has_limit F] : is_limit (limit.cone F) := has_limit.is_limit.{v} F def limit.lift (F : J ⥤ C) [has_limit F] (c : cone F) : c.X ⟶ limit F := (limit.is_limit F).lift c @[simp] lemma limit.is_limit_lift {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.is_limit F).lift c = limit.lift F c := rfl @[simp, reassoc] lemma limit.lift_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : limit.lift F c ≫ limit.π F j = c.π.app j := is_limit.fac _ c j def limit.cone_morphism {F : J ⥤ C} [has_limit F] (c : cone F) : cone_morphism c (limit.cone F) := (limit.is_limit F).lift_cone_morphism c @[simp] lemma limit.cone_morphism_hom {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.cone_morphism c).hom = limit.lift F c := rfl lemma limit.cone_morphism_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : (limit.cone_morphism c).hom ≫ limit.π F j = c.π.app j := by simp @[ext] lemma limit.hom_ext {F : J ⥤ C} [has_limit F] {X : C} {f f' : X ⟶ limit F} (w : ∀ j, f ≫ limit.π F j = f' ≫ limit.π F j) : f = f' := (limit.is_limit F).hom_ext w def limit.hom_iso (F : J ⥤ C) [has_limit F] (W : C) : (W ⟶ limit F) ≅ (F.cones.obj (op W)) := (limit.is_limit F).hom_iso W @[simp] lemma limit.hom_iso_hom (F : J ⥤ C) [has_limit F] {W : C} (f : W ⟶ limit F) : (limit.hom_iso F W).hom f = (const J).map f ≫ (limit.cone F).π := (limit.is_limit F).hom_iso_hom f def limit.hom_iso' (F : J ⥤ C) [has_limit F] (W : C) : ((W ⟶ limit F) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j' : J} (f : j ⟶ j'), p j ≫ F.map f = p j' } := (limit.is_limit F).hom_iso' W lemma limit.lift_extend {F : J ⥤ C} [has_limit F] (c : cone F) {X : C} (f : X ⟶ c.X) : limit.lift F (c.extend f) = f ≫ limit.lift F c := by obviously def has_limit_of_iso {F G : J ⥤ C} [has_limit F] (α : F ≅ G) : has_limit G := { cone := (cones.postcompose α.hom).obj (limit.cone F), is_limit := { lift := λ s, limit.lift F ((cones.postcompose α.inv).obj s), fac' := λ s j, begin rw [cones.postcompose_obj_π, nat_trans.comp_app, limit.cone_π, ←category.assoc, limit.lift_π], simp end, uniq' := λ s m w, begin apply limit.hom_ext, intro j, rw [limit.lift_π, cones.postcompose_obj_π, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_comp_inv], simpa using w j end } } /-- If a functor `G` has the same collection of cones as a functor `F` which has a limit, then `G` also has a limit. -/ -- See the construction of limits from products and equalizers -- for an example usage. def has_limit.of_cones_iso {J K : Type v} [small_category J] [small_category K] (F : J ⥤ C) (G : K ⥤ C) (h : F.cones ≅ G.cones) [has_limit F] : has_limit G := ⟨_, is_limit.of_nat_iso ((is_limit.nat_iso (limit.is_limit F)) ≪≫ h)⟩ section pre variables (F) [has_limit F] (E : K ⥤ J) [has_limit (E ⋙ F)] def limit.pre : limit F ⟶ limit (E ⋙ F) := limit.lift (E ⋙ F) { X := limit F, π := { app := λ k, limit.π F (E.obj k) } } @[simp] lemma limit.pre_π (k : K) : limit.pre F E ≫ limit.π (E ⋙ F) k = limit.π F (E.obj k) := by erw is_limit.fac @[simp] lemma limit.lift_pre (c : cone F) : limit.lift F c ≫ limit.pre F E = limit.lift (E ⋙ F) (c.whisker E) := by ext; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_limit (D ⋙ E ⋙ F)] @[simp] lemma limit.pre_pre : limit.pre F E ≫ limit.pre (E ⋙ F) D = limit.pre F (D ⋙ E) := by ext j; erw [assoc, limit.pre_π, limit.pre_π, limit.pre_π]; refl end pre section post variables {D : Type u'} [𝒟 : category.{v} D] include 𝒟 variables (F) [has_limit F] (G : C ⥤ D) [has_limit (F ⋙ G)] def limit.post : G.obj (limit F) ⟶ limit (F ⋙ G) := limit.lift (F ⋙ G) { X := G.obj (limit F), π := { app := λ j, G.map (limit.π F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cone.w, id_comp]; refl } } @[simp] lemma limit.post_π (j : J) : limit.post F G ≫ limit.π (F ⋙ G) j = G.map (limit.π F j) := by erw is_limit.fac @[simp] lemma limit.lift_post (c : cone F) : G.map (limit.lift F c) ≫ limit.post F G = limit.lift (F ⋙ G) (G.map_cone c) := by ext; rw [assoc, limit.post_π, ←G.map_comp, limit.lift_π, limit.lift_π]; refl @[simp] lemma limit.post_post {E : Type u''} [category.{v} E] (H : D ⥤ E) [has_limit ((F ⋙ G) ⋙ H)] : /- H G (limit F) ⟶ H (limit (F ⋙ G)) ⟶ limit ((F ⋙ G) ⋙ H) equals -/ /- H G (limit F) ⟶ limit (F ⋙ (G ⋙ H)) -/ H.map (limit.post F G) ≫ limit.post (F ⋙ G) H = limit.post F (G ⋙ H) := by ext; erw [assoc, limit.post_π, ←H.map_comp, limit.post_π, limit.post_π]; refl end post lemma limit.pre_post {D : Type u'} [category.{v} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_limit F] [has_limit (E ⋙ F)] [has_limit (F ⋙ G)] [has_limit ((E ⋙ F) ⋙ G)] : /- G (limit F) ⟶ G (limit (E ⋙ F)) ⟶ limit ((E ⋙ F) ⋙ G) vs -/ /- G (limit F) ⟶ limit F ⋙ G ⟶ limit (E ⋙ (F ⋙ G)) or -/ G.map (limit.pre F E) ≫ limit.post (E ⋙ F) G = limit.post F G ≫ limit.pre (F ⋙ G) E := by ext; erw [assoc, limit.post_π, ←G.map_comp, limit.pre_π, assoc, limit.pre_π, limit.post_π]; refl open category_theory.equivalence instance has_limit_equivalence_comp (e : K ≌ J) [has_limit F] : has_limit (e.functor ⋙ F) := { cone := cone.whisker e.functor (limit.cone F), is_limit := let e' := cones.postcompose (e.inv_fun_id_assoc F).hom in { lift := λ s, limit.lift F (e'.obj (cone.whisker e.inverse s)), fac' := λ s j, begin dsimp, rw [limit.lift_π], dsimp [e'], erw [inv_fun_id_assoc_hom_app, counit_functor, ←s.π.naturality, id_comp] end, uniq' := λ s m w, begin apply limit.hom_ext, intro j, erw [limit.lift_π, ←limit.w F (e.counit_iso.hom.app j)], slice_lhs 1 2 { erw [w (e.inverse.obj j)] }, simp end } } local attribute [elab_simple] inv_fun_id_assoc -- not entirely sure why this is needed def has_limit_of_equivalence_comp (e : K ≌ J) [has_limit (e.functor ⋙ F)] : has_limit F := begin haveI : has_limit (e.inverse ⋙ e.functor ⋙ F) := limits.has_limit_equivalence_comp e.symm, apply has_limit_of_iso (e.inv_fun_id_assoc F), end -- `has_limit_comp_equivalence` and `has_limit_of_comp_equivalence` -- are proved in `category_theory/adjunction/limits.lean`. section lim_functor variables [has_limits_of_shape J C] /-- `limit F` is functorial in `F`, when `C` has all limits of shape `J`. -/ def lim : (J ⥤ C) ⥤ C := { obj := λ F, limit F, map := λ F G α, limit.lift G { X := limit F, π := { app := λ j, limit.π F j ≫ α.app j, naturality' := λ j j' f, by erw [id_comp, assoc, ←α.naturality, ←assoc, limit.w] } }, map_comp' := λ F G H α β, by ext; erw [assoc, is_limit.fac, is_limit.fac, ←assoc, is_limit.fac, assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp, reassoc] lemma limit.map_π (j : J) : lim.map α ≫ limit.π G j = limit.π F j ≫ α.app j := by apply is_limit.fac @[simp] lemma limit.lift_map (c : cone F) : limit.lift F c ≫ lim.map α = limit.lift G ((cones.postcompose α).obj c) := by ext; rw [assoc, limit.map_π, ←assoc, limit.lift_π, limit.lift_π]; refl lemma limit.map_pre [has_limits_of_shape K C] (E : K ⥤ J) : lim.map α ≫ limit.pre G E = limit.pre F E ≫ lim.map (whisker_left E α) := by ext; rw [assoc, limit.pre_π, limit.map_π, assoc, limit.map_π, ←assoc, limit.pre_π]; refl lemma limit.map_pre' [has_limits_of_shape.{v} K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : limit.pre F E₂ = limit.pre F E₁ ≫ lim.map (whisker_right α F) := by ext1; simp [(category.assoc _ _ _ _).symm] lemma limit.id_pre (F : J ⥤ C) : limit.pre F (𝟭 _) = lim.map (functor.left_unitor F).inv := by tidy lemma limit.map_post {D : Type u'} [category.{v} D] [has_limits_of_shape J D] (H : C ⥤ D) : /- H (limit F) ⟶ H (limit G) ⟶ limit (G ⋙ H) vs H (limit F) ⟶ limit (F ⋙ H) ⟶ limit (G ⋙ H) -/ H.map (lim.map α) ≫ limit.post G H = limit.post F H ≫ lim.map (whisker_right α H) := begin ext, rw [assoc, limit.post_π, ←H.map_comp, limit.map_π, H.map_comp], rw [assoc, limit.map_π, ←assoc, limit.post_π], refl end def lim_yoneda : lim ⋙ yoneda ≅ category_theory.cones J C := nat_iso.of_components (λ F, nat_iso.of_components (λ W, limit.hom_iso F (unop W)) (by tidy)) (by tidy) end lim_functor def has_limits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_limits_of_shape J C] : has_limits_of_shape J' C := by { constructor, intro F, apply has_limit_of_equivalence_comp e, apply_instance } end limit section colimit /-- `has_colimit F` represents a particular chosen colimit of the diagram `F`. -/ class has_colimit (F : J ⥤ C) := (cocone : cocone F) (is_colimit : is_colimit cocone) variables (J C) /-- `C` has colimits of shape `J` if we have chosen a particular colimit of every functor `F : J ⥤ C`. -/ class has_colimits_of_shape := (has_colimit : Π F : J ⥤ C, has_colimit F) /-- `C` has all (small) colimits if it has colimits of every shape. -/ class has_colimits := (has_colimits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_colimits_of_shape J C) variables {J C} @[priority 100] -- see Note [lower instance priority] instance has_colimit_of_has_colimits_of_shape {J : Type v} [small_category J] [H : has_colimits_of_shape J C] (F : J ⥤ C) : has_colimit F := has_colimits_of_shape.has_colimit F @[priority 100] -- see Note [lower instance priority] instance has_colimits_of_shape_of_has_colimits {J : Type v} [small_category J] [H : has_colimits.{v} C] : has_colimits_of_shape J C := has_colimits.has_colimits_of_shape C J /- Interface to the `has_colimit` class. -/ def colimit.cocone (F : J ⥤ C) [has_colimit F] : cocone F := has_colimit.cocone F def colimit (F : J ⥤ C) [has_colimit F] := (colimit.cocone F).X def colimit.ι (F : J ⥤ C) [has_colimit F] (j : J) : F.obj j ⟶ colimit F := (colimit.cocone F).ι.app j @[simp] lemma colimit.cocone_ι {F : J ⥤ C} [has_colimit F] (j : J) : (colimit.cocone F).ι.app j = colimit.ι _ j := rfl @[simp] lemma colimit.w (F : J ⥤ C) [has_colimit F] {j j' : J} (f : j ⟶ j') : F.map f ≫ colimit.ι F j' = colimit.ι F j := (colimit.cocone F).w f def colimit.is_colimit (F : J ⥤ C) [has_colimit F] : is_colimit (colimit.cocone F) := has_colimit.is_colimit.{v} F def colimit.desc (F : J ⥤ C) [has_colimit F] (c : cocone F) : colimit F ⟶ c.X := (colimit.is_colimit F).desc c @[simp] lemma colimit.is_colimit_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.is_colimit F).desc c = colimit.desc F c := rfl /-- We have lots of lemmas describing how to simplify `colimit.ι F j ≫ _`, and combined with `colimit.ext` we rely on these lemmas for many calculations. However, since `category.assoc` is a `@[simp]` lemma, often expressions are right associated, and it's hard to apply these lemmas about `colimit.ι`. We thus use `reassoc` to define additional `@[simp]` lemmas, with an arbitrary extra morphism. (see `tactic/reassoc_axiom.lean`) -/ @[simp, reassoc] lemma colimit.ι_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ colimit.desc F c = c.ι.app j := is_colimit.fac _ c j def colimit.cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) : cocone_morphism (colimit.cocone F) c := (colimit.is_colimit F).desc_cocone_morphism c @[simp] lemma colimit.cocone_morphism_hom {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.cocone_morphism c).hom = colimit.desc F c := rfl lemma colimit.ι_cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ (colimit.cocone_morphism c).hom = c.ι.app j := by simp @[ext] lemma colimit.hom_ext {F : J ⥤ C} [has_colimit F] {X : C} {f f' : colimit F ⟶ X} (w : ∀ j, colimit.ι F j ≫ f = colimit.ι F j ≫ f') : f = f' := (colimit.is_colimit F).hom_ext w def colimit.hom_iso (F : J ⥤ C) [has_colimit F] (W : C) : (colimit F ⟶ W) ≅ (F.cocones.obj W) := (colimit.is_colimit F).hom_iso W @[simp] lemma colimit.hom_iso_hom (F : J ⥤ C) [has_colimit F] {W : C} (f : colimit F ⟶ W) : (colimit.hom_iso F W).hom f = (colimit.cocone F).ι ≫ (const J).map f := (colimit.is_colimit F).hom_iso_hom f def colimit.hom_iso' (F : J ⥤ C) [has_colimit F] (W : C) : ((colimit F ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j'} (f : j ⟶ j'), F.map f ≫ p j' = p j } := (colimit.is_colimit F).hom_iso' W lemma colimit.desc_extend (F : J ⥤ C) [has_colimit F] (c : cocone F) {X : C} (f : c.X ⟶ X) : colimit.desc F (c.extend f) = colimit.desc F c ≫ f := begin ext1, rw [←category.assoc], simp end def has_colimit_of_iso {F G : J ⥤ C} [has_colimit F] (α : G ≅ F) : has_colimit G := { cocone := (cocones.precompose α.hom).obj (colimit.cocone F), is_colimit := { desc := λ s, colimit.desc F ((cocones.precompose α.inv).obj s), fac' := λ s j, begin rw [cocones.precompose_obj_ι, nat_trans.comp_app, colimit.cocone_ι], rw [category.assoc, colimit.ι_desc, ←nat_iso.app_hom, ←iso.eq_inv_comp], refl end, uniq' := λ s m w, begin apply colimit.hom_ext, intro j, rw [colimit.ι_desc, cocones.precompose_obj_ι, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_inv_comp], simpa using w j end } } /-- If a functor `G` has the same collection of cocones as a functor `F` which has a colimit, then `G` also has a colimit. -/ def has_colimit.of_cocones_iso {J K : Type v} [small_category J] [small_category K] (F : J ⥤ C) (G : K ⥤ C) (h : F.cocones ≅ G.cocones) [has_colimit F] : has_colimit G := ⟨_, is_colimit.of_nat_iso ((is_colimit.nat_iso (colimit.is_colimit F)) ≪≫ h)⟩ section pre variables (F) [has_colimit F] (E : K ⥤ J) [has_colimit (E ⋙ F)] def colimit.pre : colimit (E ⋙ F) ⟶ colimit F := colimit.desc (E ⋙ F) { X := colimit F, ι := { app := λ k, colimit.ι F (E.obj k) } } @[simp, reassoc] lemma colimit.ι_pre (k : K) : colimit.ι (E ⋙ F) k ≫ colimit.pre F E = colimit.ι F (E.obj k) := by erw is_colimit.fac @[simp] lemma colimit.pre_desc (c : cocone F) : colimit.pre F E ≫ colimit.desc F c = colimit.desc (E ⋙ F) (c.whisker E) := by ext; rw [←assoc, colimit.ι_pre]; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_colimit (D ⋙ E ⋙ F)] @[simp] lemma colimit.pre_pre : colimit.pre (E ⋙ F) D ≫ colimit.pre F E = colimit.pre F (D ⋙ E) := begin ext j, rw [←assoc, colimit.ι_pre, colimit.ι_pre], letI : has_colimit ((D ⋙ E) ⋙ F) := show has_colimit (D ⋙ E ⋙ F), by apply_instance, exact (colimit.ι_pre F (D ⋙ E) j).symm end end pre section post variables {D : Type u'} [𝒟 : category.{v} D] include 𝒟 variables (F) [has_colimit F] (G : C ⥤ D) [has_colimit (F ⋙ G)] def colimit.post : colimit (F ⋙ G) ⟶ G.obj (colimit F) := colimit.desc (F ⋙ G) { X := G.obj (colimit F), ι := { app := λ j, G.map (colimit.ι F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cocone.w, comp_id]; refl } } @[simp, reassoc] lemma colimit.ι_post (j : J) : colimit.ι (F ⋙ G) j ≫ colimit.post F G = G.map (colimit.ι F j) := by erw is_colimit.fac @[simp] lemma colimit.post_desc (c : cocone F) : colimit.post F G ≫ G.map (colimit.desc F c) = colimit.desc (F ⋙ G) (G.map_cocone c) := by ext; rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_desc, colimit.ι_desc]; refl @[simp] lemma colimit.post_post {E : Type u''} [category.{v} E] (H : D ⥤ E) [has_colimit ((F ⋙ G) ⋙ H)] : /- H G (colimit F) ⟶ H (colimit (F ⋙ G)) ⟶ colimit ((F ⋙ G) ⋙ H) equals -/ /- H G (colimit F) ⟶ colimit (F ⋙ (G ⋙ H)) -/ colimit.post (F ⋙ G) H ≫ H.map (colimit.post F G) = colimit.post F (G ⋙ H) := begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colimit.ι_post], exact (colimit.ι_post F (G ⋙ H) j).symm end end post lemma colimit.pre_post {D : Type u'} [category.{v} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_colimit F] [has_colimit (E ⋙ F)] [has_colimit (F ⋙ G)] [has_colimit ((E ⋙ F) ⋙ G)] : /- G (colimit F) ⟶ G (colimit (E ⋙ F)) ⟶ colimit ((E ⋙ F) ⋙ G) vs -/ /- G (colimit F) ⟶ colimit F ⋙ G ⟶ colimit (E ⋙ (F ⋙ G)) or -/ colimit.post (E ⋙ F) G ≫ G.map (colimit.pre F E) = colimit.pre (F ⋙ G) E ≫ colimit.post F G := begin ext, rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_pre, ←assoc], letI : has_colimit (E ⋙ F ⋙ G) := show has_colimit ((E ⋙ F) ⋙ G), by apply_instance, erw [colimit.ι_pre (F ⋙ G) E j, colimit.ι_post] end open category_theory.equivalence instance has_colimit_equivalence_comp (e : K ≌ J) [has_colimit F] : has_colimit (e.functor ⋙ F) := { cocone := cocone.whisker e.functor (colimit.cocone F), is_colimit := let e' := cocones.precompose (e.inv_fun_id_assoc F).inv in { desc := λ s, colimit.desc F (e'.obj (cocone.whisker e.inverse s)), fac' := λ s j, begin dsimp, rw [colimit.ι_desc], dsimp [e'], erw [inv_fun_id_assoc_inv_app, ←functor_unit, s.ι.naturality, comp_id], refl end, uniq' := λ s m w, begin apply colimit.hom_ext, intro j, erw [colimit.ι_desc], have := w (e.inverse.obj j), simp at this, erw [←colimit.w F (e.counit_iso.hom.app j)] at this, erw [assoc, ←iso.eq_inv_comp (F.map_iso $ e.counit_iso.app j)] at this, erw [this], simp end } } def has_colimit_of_equivalence_comp (e : K ≌ J) [has_colimit (e.functor ⋙ F)] : has_colimit F := begin haveI : has_colimit (e.inverse ⋙ e.functor ⋙ F) := limits.has_colimit_equivalence_comp e.symm, apply has_colimit_of_iso (e.inv_fun_id_assoc F).symm, end section colim_functor variables [has_colimits_of_shape J C] /-- `colimit F` is functorial in `F`, when `C` has all colimits of shape `J`. -/ def colim : (J ⥤ C) ⥤ C := { obj := λ F, colimit F, map := λ F G α, colimit.desc F { X := colimit G, ι := { app := λ j, α.app j ≫ colimit.ι G j, naturality' := λ j j' f, by erw [comp_id, ←assoc, α.naturality, assoc, colimit.w] } }, map_comp' := λ F G H α β, by ext; erw [←assoc, is_colimit.fac, is_colimit.fac, assoc, is_colimit.fac, ←assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp, reassoc] lemma colimit.ι_map (j : J) : colimit.ι F j ≫ colim.map α = α.app j ≫ colimit.ι G j := by apply is_colimit.fac @[simp] lemma colimit.map_desc (c : cocone G) : colim.map α ≫ colimit.desc G c = colimit.desc F ((cocones.precompose α).obj c) := by ext; rw [←assoc, colimit.ι_map, assoc, colimit.ι_desc, colimit.ι_desc]; refl lemma colimit.pre_map [has_colimits_of_shape K C] (E : K ⥤ J) : colimit.pre F E ≫ colim.map α = colim.map (whisker_left E α) ≫ colimit.pre G E := by ext; rw [←assoc, colimit.ι_pre, colimit.ι_map, ←assoc, colimit.ι_map, assoc, colimit.ι_pre]; refl lemma colimit.pre_map' [has_colimits_of_shape.{v} K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : colimit.pre F E₁ = colim.map (whisker_right α F) ≫ colimit.pre F E₂ := by ext1; simp [(category.assoc _ _ _ _).symm] lemma colimit.pre_id (F : J ⥤ C) : colimit.pre F (𝟭 _) = colim.map (functor.left_unitor F).hom := by tidy lemma colimit.map_post {D : Type u'} [category.{v} D] [has_colimits_of_shape J D] (H : C ⥤ D) : /- H (colimit F) ⟶ H (colimit G) ⟶ colimit (G ⋙ H) vs H (colimit F) ⟶ colimit (F ⋙ H) ⟶ colimit (G ⋙ H) -/ colimit.post F H ≫ H.map (colim.map α) = colim.map (whisker_right α H) ≫ colimit.post G H:= begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colimit.ι_map, H.map_comp], rw [←assoc, colimit.ι_map, assoc, colimit.ι_post], refl end def colim_coyoneda : colim.op ⋙ coyoneda ≅ category_theory.cocones J C := nat_iso.of_components (λ F, nat_iso.of_components (colimit.hom_iso (unop F)) (by tidy)) (by tidy) end colim_functor def has_colimits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_colimits_of_shape J C] : has_colimits_of_shape J' C := by { constructor, intro F, apply has_colimit_of_equivalence_comp e, apply_instance } end colimit end category_theory.limits