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
f102ff88f7f952ae5632abe960732ebe66ed6d08
5e3548e65f2c037cb94cd5524c90c623fbd6d46a
/src_icannos_totilas/anneaux/cpge_anneaux_9.lean
3887a3fb9d42140114c919ce4a5abdb47ccea31c
[]
no_license
ahayat16/lean_exos
d4f08c30adb601a06511a71b5ffb4d22d12ef77f
682f2552d5b04a8c8eb9e4ab15f875a91b03845c
refs/heads/main
1,693,101,073,585
1,636,479,336,000
1,636,479,336,000
415,000,441
0
0
null
null
null
null
UTF-8
Lean
false
false
173
lean
import data.real.sqrt def E {X : Type} (a : X) (f : X β†’ ℝ) := f a theorem cpge_anneaux_9 {X : Type} : βˆ€ a : X, βˆƒ (g : (X β†’ ℝ) β†’+* ℝ), (E a) = g := sorry
f4902baf736bf73c624d7c2d8da6cc4fb1ed4361
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/prop819/completion.lean
3e4a6ee1e6b9c6c85e43a7f73c595d1e0dfdedff
[]
no_license
Ja1941/lean-liquid
fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc
8e80ed0cbdf5145d6814e833a674eaf05a1495c1
refs/heads/master
1,689,437,983,362
1,628,362,719,000
1,628,362,719,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,956
lean
import algebra.homology.additive import locally_constant.Vhat import normed_group.controlled_exactness import prop819.strict_complex_iso noncomputable theory universes u variables (C D : cochain_complex SemiNormedGroup.{u} β„•) open SemiNormedGroup /-- The completed cochain complex associated to C. -/ abbreviation cmpl := (Completion.map_homological_complex _).obj C open_locale nnreal lemma cmpl_exact_of_exact (Ξ΅ : ℝβ‰₯0) (hΞ΅ : 0 < Ξ΅) (cond : βˆ€ (n : β„•) (f : C.X (n+1)) (hf : C.d (n+1) (n+2) f = 0), βˆƒ g : C.X n, C.d _ _ g = f ∧ βˆ₯gβˆ₯β‚Š ≀ βˆ₯fβˆ₯β‚Š) (n : β„•) (f : (cmpl C).X (n+1)) (hf : (cmpl C).d _ (n+2) f = 0) : βˆƒ g : (cmpl C).X n, (cmpl C).d _ _ g = f ∧ βˆ₯gβˆ₯β‚Š ≀ (1 + Ξ΅) * βˆ₯fβˆ₯β‚Š := begin have := @controlled_exactness (C.X (n+1)) (C.X n) (C.X (n+2)) _ _ _ (C.d _ _) 1 zero_lt_one 1 (C.d _ _) _ _ Ξ΅ hΞ΅ f _, { rcases this with ⟨g,hg1,hg2⟩, exact ⟨g,hg1,hg2⟩ }, { intros g hg, erw add_monoid_hom.mem_ker at hg, specialize cond _ g hg, rcases cond with ⟨g',h1,h2⟩, refine ⟨g',h1,_⟩, simpa }, { rintros g ⟨g,rfl⟩, specialize cond (n+1) (C.d (n+1) (n+2) g) _, { change (C.d (n+1) (n+2) ≫ C.d (n+2) (n+3)) g = 0, simp }, rcases cond with ⟨g',h1,h2⟩, refine ⟨g',h1,_⟩, dsimp, simp, exact h2 }, { erw add_monoid_hom.mem_ker, exact hf }, end lemma injective_of_strict_iso (F : strict_iso C D) (cond : βˆ€ (f : C.X 0) (hf : C.d 0 1 f = 0), f = 0) (f : D.X 0) (hf : D.d 0 1 f = 0) : f = 0 := begin let FF := (homological_complex.eval _ _ 0).map_iso F.iso, have : f = (FF.inv ≫ FF.hom) f, by {rw FF.inv_hom_id, simp}, rw this, change FF.hom (FF.inv f) = 0, rw ←(show FF.hom 0 = 0, by simp), congr' 1, apply cond, change (F.iso.inv.f 0 ≫ C.d 0 1) f = 0, erw F.iso.inv.comm, change (F.iso.inv.f 1) _ = 0, erw hf, simp, end lemma exact_of_strict_iso (F : strict_iso C D) (Ξ΅ : ℝβ‰₯0) (hΞ΅ : 0 < Ξ΅) (cond : βˆ€ (n : β„•) (f : C.X (n+1)) (hf : C.d _ (n+2) f = 0), βˆƒ g : C.X n, C.d _ _ g = f ∧ βˆ₯gβˆ₯β‚Š ≀ (1+Ξ΅) * βˆ₯fβˆ₯β‚Š) (n : β„•) (f : D.X (n+1)) (hf : D.d _ (n+2) f = 0) : βˆƒ g : D.X n, D.d _ _ g = f ∧ βˆ₯gβˆ₯β‚Š ≀ (1 + Ξ΅) * βˆ₯fβˆ₯β‚Š := begin specialize cond n (((homological_complex.eval _ _ (n+1)).map_iso F.iso).inv f) _, { dsimp [homological_complex.eval], change (F.iso.inv.f (n+1) ≫ C.d (n+1) (n+2)) f = 0, rw F.iso.inv.comm, simp [hf] }, rcases cond with ⟨g, h1, h2⟩, refine ⟨((homological_complex.eval _ _ n).map_iso F.iso).hom g, _, _⟩, { dsimp [homological_complex.eval] at *, change (F.iso.hom.f _ ≫ D.d _ _) _ = _, rw F.iso.hom.comm, simp [h1], let FF := (homological_complex.eval _ _ (n+1)).map_iso F.iso, change (FF.inv ≫ FF.hom) f = f, rw FF.inv_hom_id, refl }, { rw SemiNormedGroup.strict_iso_inv at h2, rwa SemiNormedGroup.strict_iso_hom }, end
8ce1c312dac6d15dbd9533f9805421731b4a0426
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Elab/Quotation.lean
5b278dc4fdda553e1232b8d224d10f3328d0859c
[ "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
34,032
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich Elaboration of syntax quotations as terms and patterns (in `match_syntax`). See also `./Hygiene.lean` for the basic hygiene workings and data types. -/ import Lean.Syntax import Lean.ResolveName import Lean.Elab.Term import Lean.Elab.Quotation.Util import Lean.Elab.Quotation.Precheck import Lean.Elab.Syntax import Lean.Parser.Syntax namespace Lean.Elab.Term.Quotation open Lean.Parser.Term open Lean.Syntax open Meta open TSyntax.Compat -- TODO(gabriel): We heavily use `have` here to keep the local context clean. -- We should replace this by non-dependent lets in the future. /-- `C[$(e)]` ~> `let a := e; C[$a]`. Used in the implementation of antiquot splices. -/ private partial def floatOutAntiquotTerms (stx : Syntax) : StateT (Syntax β†’ TermElabM Syntax) TermElabM Syntax := if isAntiquots stx && !isEscapedAntiquot (getCanonicalAntiquot stx) then let e := getAntiquotTerm (getCanonicalAntiquot stx) if !e.isIdent || !e.getId.isAtomic then withFreshMacroScope do let a ← `(__stx_lift) modify (fun _ (stx : Syntax) => (`(let $a:ident := $e; $stx) : TermElabM Syntax)) let stx := if stx.isOfKind choiceKind then mkNullNode <| stx.getArgs.map (Β·.setArg 2 a) else stx.setArg 2 a return stx else return stx else if let Syntax.node i k args := stx then return Syntax.node i k (← args.mapM floatOutAntiquotTerms) else return stx private def getSepFromSplice (splice : Syntax) : String := if let Syntax.atom _ sep := getAntiquotSpliceSuffix splice then sep.dropRight 1 -- drop trailing * else unreachable! private def getSepStxFromSplice (splice : Syntax) : Syntax := Unhygienic.run do match getSepFromSplice splice with | "" => `(mkNullNode) -- sepByIdent uses the null node for separator-less enumerations | sep => `(mkAtom $(Syntax.mkStrLit sep)) partial def mkTuple : Array Syntax β†’ TermElabM Syntax | #[] => `(Unit.unit) | #[e] => return e | es => do let stx ← mkTuple (es.eraseIdx 0) `(Prod.mk $(es[0]!) $stx) def resolveSectionVariable (sectionVars : NameMap Name) (id : Name) : List (Name Γ— List String) := -- decode macro scopes from name before recursion let extractionResult := extractMacroScopes id let rec loop : Name β†’ List String β†’ List (Name Γ— List String) | id@(.str p s), projs => -- NOTE: we assume that macro scopes always belong to the projected constant, not the projections let id := { extractionResult with name := id }.review match sectionVars.find? id with | some newId => [(newId, projs)] | none => loop p (s::projs) | _, _ => [] loop extractionResult.name [] /-- Transform sequence of pushes and appends into acceptable code -/ def ArrayStxBuilder := Sum (Array Term) Term namespace ArrayStxBuilder def empty : ArrayStxBuilder := .inl #[] def build : ArrayStxBuilder β†’ Term | .inl elems => quote elems | .inr arr => arr def push (b : ArrayStxBuilder) (elem : Syntax) : ArrayStxBuilder := match b with | .inl elems => .inl <| elems.push elem | .inr arr => .inr <| mkCApp ``Array.push #[arr, elem] def append (b : ArrayStxBuilder) (arr : Syntax) (appendName := ``Array.append) : ArrayStxBuilder := .inr <| mkCApp appendName #[b.build, arr] def mkNode (b : ArrayStxBuilder) (k : SyntaxNodeKind) : TermElabM Term := do let k := quote k match b with | .inl #[a₁] => `(Syntax.node1 info $(k) $(a₁)) | .inl #[a₁, aβ‚‚] => `(Syntax.node2 info $(k) $(a₁) $(aβ‚‚)) | .inl #[a₁, aβ‚‚, a₃] => `(Syntax.node3 info $(k) $(a₁) $(aβ‚‚) $(a₃)) | .inl #[a₁, aβ‚‚, a₃, aβ‚„] => `(Syntax.node4 info $(k) $(a₁) $(aβ‚‚) $(a₃) $(aβ‚„)) | .inl #[a₁, aβ‚‚, a₃, aβ‚„, aβ‚…] => `(Syntax.node5 info $(k) $(a₁) $(aβ‚‚) $(a₃) $(aβ‚„) $(aβ‚…)) | .inl #[a₁, aβ‚‚, a₃, aβ‚„, aβ‚…, a₆] => `(Syntax.node6 info $(k) $(a₁) $(aβ‚‚) $(a₃) $(aβ‚„) $(aβ‚…) $(a₆)) | .inl #[a₁, aβ‚‚, a₃, aβ‚„, aβ‚…, a₆, a₇] => `(Syntax.node7 info $(k) $(a₁) $(aβ‚‚) $(a₃) $(aβ‚„) $(aβ‚…) $(a₆) $(a₇)) | .inl #[a₁, aβ‚‚, a₃, aβ‚„, aβ‚…, a₆, a₇, aβ‚ˆ] => `(Syntax.node8 info $(k) $(a₁) $(aβ‚‚) $(a₃) $(aβ‚„) $(aβ‚…) $(a₆) $(a₇) $(aβ‚ˆ)) | _ => `(Syntax.node info $(k) $(b.build)) end ArrayStxBuilder def tryAddSyntaxNodeKindInfo (stx : Syntax) (k : SyntaxNodeKind) : TermElabM Unit := do if (← getEnv).contains k then addTermInfo' stx (← mkConstWithFreshMVarLevels k) else -- HACK to support built in categories, which use a different naming convention let k := ``Lean.Parser.Category ++ k if (← getEnv).contains k then addTermInfo' stx (← mkConstWithFreshMVarLevels k) instance : Quote Syntax.Preresolved where quote | .namespace ns => Unhygienic.run ``(Syntax.Preresolved.namespace $(quote ns)) | .decl n fs => Unhygienic.run ``(Syntax.Preresolved.decl $(quote n) $(quote fs)) /-- Elaborate the content of a syntax quotation term -/ private partial def quoteSyntax : Syntax β†’ TermElabM Term | Syntax.ident _ rawVal val preresolved => do if !hygiene.get (← getOptions) then return ← `(Syntax.ident info $(quote rawVal) $(quote val) $(quote preresolved)) -- Add global scopes at compilation time (now), add macro scope at runtime (in the quotation). -- See the paper for details. let consts ← resolveGlobalName val -- extension of the paper algorithm: also store unique section variable names as top-level scopes -- so they can be captured and used inside the section, but not outside let sectionVars := resolveSectionVariable (← read).sectionVars val -- extension of the paper algorithm: resolve namespaces as well let namespaces ← resolveNamespaceCore (allowEmpty := true) val let preresolved := (consts ++ sectionVars).map (fun (n, projs) => Preresolved.decl n projs) ++ namespaces.map .namespace ++ preresolved let val := quote val -- `scp` is bound in stxQuot.expand `(Syntax.ident info $(quote rawVal) (addMacroScope mainModule $val scp) $(quote preresolved)) -- if antiquotation, insert contents as-is, else recurse | stx@(Syntax.node _ k _) => do if let some (k, _) := stx.antiquotKind? then if let some name := getAntiquotKindSpec? stx then tryAddSyntaxNodeKindInfo name k if isAntiquots stx && !isEscapedAntiquot (getCanonicalAntiquot stx) then let ks := antiquotKinds stx `(@TSyntax.raw $(quote <| ks.map (Β·.1)) $(getAntiquotTerm (getCanonicalAntiquot stx))) else if isTokenAntiquot stx && !isEscapedAntiquot stx then match stx[0] with | Syntax.atom _ val => `(Syntax.atom (SourceInfo.fromRef $(getAntiquotTerm stx) (canonical := true)) $(quote val)) | _ => throwErrorAt stx "expected token" else if isAntiquotSuffixSplice stx && !isEscapedAntiquot (getCanonicalAntiquot (getAntiquotSuffixSpliceInner stx)) then -- splices must occur in a `many` node throwErrorAt stx "unexpected antiquotation splice" else if isAntiquotSplice stx && !isEscapedAntiquot stx then throwErrorAt stx "unexpected antiquotation splice" else -- if escaped antiquotation, decrement by one escape level let stx := unescapeAntiquot stx let mut args := ArrayStxBuilder.empty let appendName := if (← getEnv).contains ``Array.append then ``Array.append else ``Array.appendCore for arg in stx.getArgs do if k == nullKind && isAntiquotSuffixSplice arg && !isEscapedAntiquot (getCanonicalAntiquot (getAntiquotSuffixSpliceInner arg)) then let antiquot := getAntiquotSuffixSpliceInner arg let ks := antiquotKinds antiquot |>.map (Β·.1) let val := getAntiquotTerm (getCanonicalAntiquot antiquot) args := args.append (appendName := appendName) <| ← match antiquotSuffixSplice? arg with | `optional => `(match Option.map (@TSyntax.raw $(quote ks)) $val:term with | some x => Array.empty.push x | none => Array.empty) | `many => `(@TSyntaxArray.raw $(quote ks) $val) | `sepBy => let sep := quote <| getSepFromSplice arg `(@TSepArray.elemsAndSeps $(quote ks) $sep $val) | k => throwErrorAt arg "invalid antiquotation suffix splice kind '{k}'" else if k == nullKind && isAntiquotSplice arg && !isEscapedAntiquot arg then let k := antiquotSpliceKind? arg let (arg, bindLets) ← floatOutAntiquotTerms arg |>.run pure let inner ← (getAntiquotSpliceContents arg).mapM quoteSyntax let ids ← getAntiquotationIds arg if ids.isEmpty then throwErrorAt stx "antiquotation splice must contain at least one antiquotation" let arr ← match k with | `optional => `(match $[$ids:ident],* with | $[some $ids:ident],* => $(quote inner) | $[_%$ids],* => Array.empty) | _ => let arr ← ids[:ids.size-1].foldrM (fun id arr => `(Array.zip $id:ident $arr)) ids.back `(Array.map (fun $(← mkTuple ids) => $(inner[0]!)) $arr) let arr ← if k == `sepBy then `(mkSepArray $arr $(getSepStxFromSplice arg)) else pure arr let arr ← bindLets arr args := args.append (appendName := appendName) arr else do let arg ← quoteSyntax arg args := args.push arg args.mkNode k | Syntax.atom _ val => `(Syntax.atom info $(quote val)) | Syntax.missing => throwUnsupportedSyntax def addNamedQuotInfo (stx : Syntax) (k : SyntaxNodeKind) : TermElabM SyntaxNodeKind := do if stx.getNumArgs == 3 && stx[0].isAtom then let s := stx[0].getAtomVal if s.length > 3 then if let (some l, some r) := (stx[0].getPos? true, stx[0].getTailPos? true) then -- HACK: The atom is the string "`(foo|", so chop off the edges. let name := stx[0].setInfo <| .synthetic ⟨l.1 + 2⟩ ⟨r.1 - 1⟩ (canonical := true) tryAddSyntaxNodeKindInfo name k pure k def getQuotKind (stx : Syntax) : TermElabM SyntaxNodeKind := do match stx.getKind with | ``Parser.Command.quot => addNamedQuotInfo stx `command | ``Parser.Term.quot => addNamedQuotInfo stx `term | ``Parser.Tactic.quot => addNamedQuotInfo stx `tactic | ``Parser.Tactic.quotSeq => addNamedQuotInfo stx `tactic.seq | .str kind "quot" => addNamedQuotInfo stx kind | ``dynamicQuot => match ← elabParserName stx[1] with | .parser n _ => return n | .category c => return c | k => throwError "unexpected quotation kind {k}" def mkSyntaxQuotation (stx : Syntax) (kind : Name) : TermElabM Syntax := do /- Syntax quotations are monadic values depending on the current macro scope. For efficiency, we bind the macro scope once for each quotation, then build the syntax tree in a completely pure computation depending on this binding. Note that regular function calls do not introduce a new macro scope (i.e. we preserve referential transparency), so we can refer to this same `scp` inside `quoteSyntax` by including it literally in a syntax quotation. -/ `(Bind.bind MonadRef.mkInfoFromRefPos (fun info => Bind.bind getCurrMacroScope (fun scp => Bind.bind getMainModule (fun mainModule => Pure.pure (@TSyntax.mk $(quote kind) $stx))))) /- NOTE: It may seem like the newly introduced binding `scp` may accidentally capture identifiers in an antiquotation introduced by `quoteSyntax`. However, note that the syntax quotation above enjoys the same hygiene guarantees as anywhere else in Lean; that is, we implement hygienic quotations by making use of the hygienic quotation support of the bootstrapped Lean compiler! Aside: While this might sound "dangerous", it is in fact less reliant on a "chain of trust" than other bootstrapping parts of Lean: because this implementation itself never uses `scp` (or any other identifier) both inside and outside quotations, it can actually correctly be compiled by an unhygienic (but otherwise correct) implementation of syntax quotations. As long as it is then compiled again with the resulting executable (i.e. up to stage 2), the result is a correct hygienic implementation. In this sense the implementation is "self-stabilizing". It was in fact originally compiled by an unhygienic prototype implementation. -/ def stxQuot.expand (stx : Syntax) : TermElabM Syntax := do let stx := if stx.getNumArgs == 1 then stx[0] else stx let kind ← getQuotKind stx let stx ← quoteSyntax stx.getQuotContent mkSyntaxQuotation stx kind macro "elab_stx_quot" kind:ident : command => `(@[builtin_term_elab $kind:ident] def elabQuot : TermElab := adaptExpander stxQuot.expand) elab_stx_quot Parser.Term.quot elab_stx_quot Parser.Tactic.quot elab_stx_quot Parser.Tactic.quotSeq elab_stx_quot Parser.Term.dynamicQuot elab_stx_quot Parser.Command.quot /-! # match -/ /-- an "alternative" of patterns plus right-hand side -/ private abbrev Alt := List Term Γ— Term /-- In a single match step, we match the first discriminant against the "head" of the first pattern of the first alternative. This datatype describes what kind of check this involves, which helps other patterns decide if they are covered by the same check and don't have to be checked again (see also `MatchResult`). -/ inductive HeadCheck where /-- match step that always succeeds: _, x, `($x), ... -/ | unconditional /-- match step based on kind and, optionally, arity of discriminant If `arity` is given, that number of new discriminants is introduced. `covered` patterns should then introduce the same number of new patterns. We actually check the arity at run time only in the case of `null` nodes since it should otherwise by implied by the node kind. without arity: `($x:k) with arity: any quotation without an antiquotation head pattern -/ | shape (k : List SyntaxNodeKind) (arity : Option Nat) /-- Match step that succeeds on `null` nodes of arity at least `numPrefix + numSuffix`, introducing discriminants for the first `numPrefix` children, one `null` node for those in between, and for the `numSuffix` last children. example: `([$x, $xs,*, $y]) is `slice 2 2` -/ | slice (numPrefix numSuffix : Nat) /-- other, complicated match step that will probably only cover identical patterns example: antiquotation splices `($[...]*) -/ | other (pat : Syntax) open HeadCheck /-- Describe whether a pattern is covered by a head check (induced by the pattern itself or a different pattern). -/ inductive MatchResult where /-- Pattern agrees with head check, remove and transform remaining alternative. If `exhaustive` is `false`, *also* include unchanged alternative in the "no" branch. -/ | covered (f : Alt β†’ TermElabM Alt) (exhaustive : Bool) /-- Pattern disagrees with head check, include in "no" branch only -/ | uncovered /-- Pattern is not quite sure yet; include unchanged in both branches -/ | undecided instance : Repr MatchResult where reprPrec | .covered _ e, _ => f!"covered _ {repr e}" | .uncovered, _ => "uncovered" | .undecided, _ => "undecided" open MatchResult /-- All necessary information on a pattern head. -/ structure HeadInfo where /-- check induced by the pattern -/ check : HeadCheck /-- compute compatibility of pattern with given head check -/ onMatch (taken : HeadCheck) : MatchResult /-- actually run the specified head check, with the discriminant bound to `__discr` -/ doMatch (yes : (newDiscrs : List Term) β†’ TermElabM Term) (no : TermElabM Term) : TermElabM Term /-- Adapt alternatives that do not introduce new discriminants in `doMatch`, but are covered by those that do so. -/ private def noOpMatchAdaptPats : HeadCheck β†’ Alt β†’ Alt | shape _ (some sz), (pats, rhs) => (List.replicate sz (Unhygienic.run `(_)) ++ pats, rhs) | slice p s, (pats, rhs) => (List.replicate (p + 1 + s) (Unhygienic.run `(_)) ++ pats, rhs) | _, alt => alt private def adaptRhs (fn : Term β†’ TermElabM Term) : Alt β†’ TermElabM Alt | (pats, rhs) => return (pats, ← fn rhs) private partial def getHeadInfo (alt : Alt) : TermElabM HeadInfo := let pat := alt.fst.head! let unconditionally rhsFn := pure { check := unconditional, doMatch := fun yes _ => yes [], onMatch := fun taken => covered (adaptRhs rhsFn ∘ noOpMatchAdaptPats taken) (taken matches unconditional) } -- quotation pattern if isQuot pat then do let quoted := getQuotContent pat if let some (k, _) := quoted.antiquotKind? then if let some name := getAntiquotKindSpec? quoted then tryAddSyntaxNodeKindInfo name k if quoted.isAtom || quoted.isOfKind `patternIgnore then -- We assume that atoms are uniquely determined by the node kind and never have to be checked unconditionally pure else if quoted.isTokenAntiquot then unconditionally (`(have $(quoted.getAntiquotTerm) := __discr; $(Β·))) else if isAntiquots quoted && !isEscapedAntiquot (getCanonicalAntiquot quoted) then -- quotation contains a single antiquotation let (ks, pseudoKinds) := antiquotKinds quoted |>.unzip let rhsFn := match getAntiquotTerm (getCanonicalAntiquot quoted) with | `(_) => pure | `($id:ident) => fun stx => `(have $id := @TSyntax.mk $(quote ks) __discr; $(stx)) | anti => fun _ => throwErrorAt anti "unsupported antiquotation kind in pattern" -- Antiquotation kinds like `$id:ident` influence the parser, but also need to be considered by -- `match` (but not by quotation terms). For example, `($id:ident) and `($e) are not -- distinguishable without checking the kind of the node to be captured. Note that some -- antiquotations like the latter one for terms do not correspond to any actual node kind, -- so we would only check for `ident` here. -- -- if stx.isOfKind `ident then -- let id := stx; let e := stx; ... -- else -- let e := stx; ... if (getCanonicalAntiquot quoted)[3].isNone || pseudoKinds.all id then unconditionally rhsFn else pure { check := shape ks none, onMatch := fun | taken@(shape ks' sz) => if ks' == ks then covered (adaptRhs rhsFn ∘ noOpMatchAdaptPats taken) (exhaustive := sz.isNone) else uncovered | _ => undecided, doMatch := fun yes no => do let cond ← ks.foldlM (fun cond k => `(or $cond (Syntax.isOfKind __discr $(quote k)))) (← `(false)) `(cond $cond $(← yes []) $(← no)), } else if isAntiquotSuffixSplice quoted then throwErrorAt quoted "unexpected antiquotation splice" else if isAntiquotSplice quoted then throwErrorAt quoted "unexpected antiquotation splice" else if quoted.getArgs.size == 1 && isAntiquotSuffixSplice quoted[0] then let inner := getAntiquotSuffixSpliceInner quoted[0] let ks := antiquotKinds inner |>.map (Β·.1) unconditionally <| match getAntiquotTerm (getCanonicalAntiquot inner) with | `(_) => pure | `($id:ident) => fun rhs => match antiquotSuffixSplice? quoted[0] with | `optional => `(have $id := Option.map (@TSyntax.mk $(quote ks)) (Syntax.getOptional? __discr); $rhs) | `many => `(have $id := @TSyntaxArray.mk $(quote ks) (Syntax.getArgs __discr); $rhs) | `sepBy => `(have $id := @TSepArray.mk $(quote ks) $(quote <| getSepFromSplice quoted[0]) (Syntax.getArgs __discr); $rhs) | k => throwErrorAt quoted "invalid antiquotation suffix splice kind '{k}'" | anti => fun _ => throwErrorAt anti "unsupported antiquotation kind in pattern" else if quoted.getArgs.size == 1 && isAntiquotSplice quoted[0] then pure { check := other pat, onMatch := fun | other pat' => if pat' == pat then covered pure (exhaustive := true) else undecided | _ => undecided, doMatch := fun yes no => do let splice := quoted[0] let k := antiquotSpliceKind? splice let contents := getAntiquotSpliceContents splice let ids ← getAntiquotationIds splice let yes ← yes [] let no ← no match k with | `optional => let nones := mkArray ids.size (← `(none)) `(let_delayed yes _ $ids* := $yes; if __discr.isNone then yes () $[ $nones]* else match __discr with | `($(mkNullNode contents)) => yes () $[ (some $ids)]* | _ => $no) | _ => let mut discrs ← `(Syntax.getArgs __discr) if k == `sepBy then discrs ← `(Array.getSepElems $discrs) let tuple ← mkTuple ids let mut yes := yes let resId ← match ids with | #[id] => pure id | _ => for id in ids do yes ← `(have $id := tuples.map (fun $tuple => $id); $yes) `(tuples) let contents := if contents.size == 1 then contents[0]! else mkNullNode contents -- We use `no_error_if_unused%` in auxiliary `match`-syntax to avoid spurious error messages, -- the outer `match` is checking for unused alternatives `(match ($(discrs).sequenceMap fun | `($contents) => no_error_if_unused% some $tuple | _ => no_error_if_unused% none) with | some $resId => $yes | none => $no) } else if let some idx := quoted.getArgs.findIdx? (fun arg => isAntiquotSuffixSplice arg || isAntiquotSplice arg) then do /- patterns of the form `match __discr, ... with | `(pat_0 ... pat_(idx-1) $[...]* pat_(idx+1) ...), ...` transform to ``` if __discr.getNumArgs >= $quoted.getNumArgs - 1 then match __discr[0], ..., __discr[idx-1], mkNullNode (__discr.getArgs.extract idx (__discr.getNumArgs - $numSuffix))), ..., __discr[quoted.getNumArgs - 1] with | `(pat_0), ... `(pat_(idx-1)), `($[...])*, `(pat_(idx+1)), ... ``` -/ let numSuffix := quoted.getNumArgs - 1 - idx pure { check := slice idx numSuffix onMatch := fun | slice p s => if p == idx && s == numSuffix then let argPats := quoted.getArgs.mapIdx fun i arg => let arg := if (i : Nat) == idx then mkNullNode #[arg] else arg Unhygienic.run `(`($(arg))) covered (fun (pats, rhs) => pure (argPats.toList ++ pats, rhs)) (exhaustive := true) else uncovered | _ => undecided doMatch := fun yes no => do let prefixDiscrs ← (List.range idx).mapM (`(Syntax.getArg __discr $(quote Β·))) let sliceDiscr ← `(mkNullNode (__discr.getArgs.extract $(quote idx) (Nat.sub __discr.getNumArgs $(quote numSuffix)))) let suffixDiscrs ← (List.range numSuffix).mapM fun i => `(Syntax.getArg __discr (Nat.sub __discr.getNumArgs $(quote (numSuffix - i)))) `(ite (GE.ge __discr.getNumArgs $(quote (quoted.getNumArgs - 1))) $(← yes (prefixDiscrs ++ sliceDiscr :: suffixDiscrs)) $(← no)) } else -- not an antiquotation, or an escaped antiquotation: match head shape let quoted := unescapeAntiquot quoted let kind := quoted.getKind let lit := isLitKind kind let argPats := quoted.getArgs.map fun arg => Unhygienic.run `(`($(arg))) pure { check := -- Atoms are matched only within literals because it would be a waste of time for keywords -- such as `if` and `then` and would blow up the generated code. -- See also `elabMatchSyntax` docstring below. if quoted.isIdent || lit then -- NOTE: We could make this case split more precise by refining `HeadCheck`, -- but matching on literals is quite rare. other quoted else shape [kind] argPats.size, onMatch := fun | other stx' => if quoted.isIdent || lit then if quoted == stx' then covered pure (exhaustive := true) else uncovered else undecided | shape ks sz => if ks == [kind] && sz == argPats.size then covered (fun (pats, rhs) => pure (argPats.toList ++ pats, rhs)) (exhaustive := true) else uncovered | _ => undecided, doMatch := fun yes no => do let (cond, newDiscrs) ← if lit then let cond ← `(Syntax.matchesLit __discr $(quote kind) $(quote (isLit? kind quoted).get!)) pure (cond, []) else let cond ← match kind with | `null => `(Syntax.matchesNull __discr $(quote argPats.size)) | `ident => `(Syntax.matchesIdent __discr $(quote quoted.getId)) | _ => `(Syntax.isOfKind __discr $(quote kind)) let newDiscrs ← (List.range argPats.size).mapM fun i => `(Syntax.getArg __discr $(quote i)) pure (cond, newDiscrs) `(ite (Eq $cond true) $(← yes newDiscrs) $(← no)) } else match pat with | `(_) => unconditionally pure | `($id:ident) => unconditionally (`(have $id := __discr; $(Β·))) | `($id:ident@$pat) => do let info ← getHeadInfo (pat::alt.1.tail!, alt.2) return { info with onMatch := fun taken => match info.onMatch taken with | covered f exh => covered (fun alt => f alt >>= adaptRhs (`(have $id := __discr; $(Β·)))) exh | r => r } | _ => throwErrorAt pat "match (syntax) : unexpected pattern kind {pat}" /-- Bind right-hand side to new `let_delayed` decl in order to prevent code duplication -/ private def deduplicate (floatedLetDecls : Array Syntax) : Alt β†’ TermElabM (Array Syntax Γ— Alt) -- NOTE: new macro scope so that introduced bindings do not collide | (pats, rhs) => do if let `($_:ident $[ $_:ident]*) := rhs then -- looks simple enough/created by this function, skip return (floatedLetDecls, (pats, rhs)) withFreshMacroScope do match (← getPatternsVars pats.toArray) with | #[] => -- no antiquotations => introduce Unit parameter to preserve evaluation order let rhs' ← `(rhs Unit.unit) pure (floatedLetDecls.push (← `(letDecl|rhs _ := $rhs)), (pats, rhs')) | vars => let rhs' ← `(rhs $vars*) pure (floatedLetDecls.push (← `(letDecl|rhs $vars:ident* := $rhs)), (pats, rhs')) private partial def compileStxMatch (discrs : List Term) (alts : List Alt) : TermElabM Syntax := do trace[Elab.match_syntax] "match {discrs} with {alts}" match discrs, alts with | [], ([], rhs)::_ => pure rhs -- nothing left to match | _, [] => logError "non-exhaustive 'match' (syntax)" pure Syntax.missing | discr::discrs, alt::alts => do let info ← getHeadInfo alt let alts := (info.onMatch info.check, alt) :: (← alts.mapM fun alt => return ((← getHeadInfo alt).onMatch info.check, alt)) let mut yesAlts := #[] let mut undecidedAlts := #[] let mut nonExhaustiveAlts := #[] let mut floatedLetDecls := #[] for (x, alt') in alts do let mut alt' := alt' trace[Elab.match_syntax.onMatch] "{alt'} ~> {repr x}" match x with | covered f exh => -- we can only factor out a common check if there are no undecided patterns in between; -- otherwise we would change the order of alternatives if undecidedAlts.isEmpty then yesAlts ← yesAlts.push <$> f (alt'.1.tail!, alt'.2) if !exh then nonExhaustiveAlts := nonExhaustiveAlts.push alt' else (floatedLetDecls, alt') ← deduplicate floatedLetDecls alt' undecidedAlts := undecidedAlts.push alt' nonExhaustiveAlts := nonExhaustiveAlts.push alt' | undecided => (floatedLetDecls, alt') ← deduplicate floatedLetDecls alt' undecidedAlts := undecidedAlts.push alt' nonExhaustiveAlts := nonExhaustiveAlts.push alt' | uncovered => nonExhaustiveAlts := nonExhaustiveAlts.push alt' let mut stx ← info.doMatch (yes := fun newDiscrs => do let mut yesAlts := yesAlts if !undecidedAlts.isEmpty then -- group undecided alternatives in a new default case `| discr2, ... => match discr, discr2, ... with ...` let vars ← discrs.mapM fun _ => withFreshMacroScope `(__discr) let pats := List.replicate newDiscrs.length (Unhygienic.run `(_)) ++ vars let alts ← undecidedAlts.mapM fun alt => `(matchAltExpr| | $(alt.1.toArray),* => no_error_if_unused% $(alt.2)) let rhs ← `(match __discr, $[$(vars.toArray):term],* with $alts:matchAlt*) yesAlts := yesAlts.push (pats, rhs) withFreshMacroScope $ compileStxMatch (newDiscrs ++ discrs) yesAlts.toList) (no := withFreshMacroScope $ compileStxMatch (discr::discrs) nonExhaustiveAlts.toList) for d in floatedLetDecls do stx ← `(let_delayed $d:letDecl; $stx) `(have __discr := $discr; $stx) | _, _ => unreachable! abbrev IdxSet := HashSet Nat private partial def hasNoErrorIfUnused : Syntax β†’ Bool | `(no_error_if_unused% $_) => true | `(clear% $_; $body) => hasNoErrorIfUnused body | _ => false /-- Given `rhss` the right-hand-sides of a `match`-syntax notation, We tag them with with fresh identifiers `alt_idx`. We use them to detect whether an alternative has been used or not. The result is a triple `(altIdxMap, ignoreIfUnused, rhssNew)` where - `altIdxMap` is a mapping from the `alt_idx` identifiers to right-hand-side indices. That is, the map contains the entry `alt_idx ↦ i` if `alt_idx` was used to mark `rhss[i]`. - `i ∈ ignoreIfUnused` if `rhss[i]` is marked with `no_error_if_unused%` - `rhssNew` is the updated array of right-hand-sides. -/ private def markRhss (rhss : Array Term) : TermElabM (NameMap Nat Γ— IdxSet Γ— Array Term) := do let mut altIdxMap : NameMap Nat := {} let mut ignoreIfUnused : IdxSet := {} let mut rhssNew := #[] for rhs in rhss do if hasNoErrorIfUnused rhs then ignoreIfUnused := ignoreIfUnused.insert rhssNew.size let (idx, rhs) ← withFreshMacroScope do let idx ← `(alt_idx) let rhs ← `(alt_idx $rhs) return (idx, rhs) altIdxMap := altIdxMap.insert idx.getId rhssNew.size rhssNew := rhssNew.push rhs return (altIdxMap, ignoreIfUnused, rhssNew) /-- Given the mapping `idxMap` built using `markRhss`, and `stx` the resulting syntax after expanding `match`-syntax, return the pair `(stxNew, usedSet)`, where `stxNew` is `stx` after removing the `alt_idx` markers in `idxMap`, and `i ∈ usedSet` if `stx` contains an `alt_idx` s.t. `alt_idx ↦ i` is in `idxMap`. That is, `usedSet` contains the index of the used match-syntax right-hand-sides. -/ private partial def findUsedAlts (stx : Syntax) (altIdxMap : NameMap Nat) : TermElabM (Syntax Γ— IdxSet) := do go stx |>.run {} where go (stx : Syntax) : StateRefT IdxSet TermElabM Syntax := do match stx with | `($id:ident $rhs:term) => if let some idx := altIdxMap.find? id.getId then modify fun s => s.insert idx return rhs | _ => pure () match stx with | .node info kind cs => return .node info kind (← cs.mapM go) | _ => return stx /-- Check whether `stx` has unused alternatives, and remove the auxiliary `alt_idx` markers from it (see `markRhss`). The parameter `alts` provides position information for alternatives. `altIdxMap` and `ignoreIfUnused` are the map and set built using `markRhss`. -/ private def checkUnusedAlts (stx : Syntax) (alts : Array Syntax) (altIdxMap : NameMap Nat) (ignoreIfUnused : IdxSet) : TermElabM Syntax := do let (stx, used) ← findUsedAlts stx altIdxMap for i in [:alts.size] do unless used.contains i || ignoreIfUnused.contains i do logErrorAt alts[i]! s!"redundant alternative #{i+1}" return stx def match_syntax.expand (stx : Syntax) : TermElabM Syntax := do match stx with | `(match $[$discrs:term],* with $[|%$alt $[$patss],* => $rhss]*) => do if !patss.any (Β·.any (fun | `($_@$pat) => pat.raw.isQuot | pat => pat.raw.isQuot)) then -- no quotations => fall back to regular `match` throwUnsupportedSyntax let (altIdxMap, ignoreIfUnused, rhss) ← markRhss rhss -- call `getQuotKind` on all the patterns, which adds -- term info for all `(foo| ...)` as a side effect patss.forM (Β·.forM fun ⟨pat⟩ => do if pat.isQuot then _ ← getQuotKind pat) let stx ← compileStxMatch discrs.toList (patss.map (Β·.toList) |>.zip rhss).toList let stx ← checkUnusedAlts stx alt altIdxMap ignoreIfUnused trace[Elab.match_syntax.result] "{stx}" return stx | _ => throwUnsupportedSyntax @[builtin_term_elab Β«matchΒ»] def elabMatchSyntax : TermElab := adaptExpander match_syntax.expand @[builtin_term_elab noErrorIfUnused] def elabNoErrorIfUnused : TermElab := fun stx expectedType? => match stx with | `(no_error_if_unused% $term) => elabTerm term expectedType? | _ => throwUnsupportedSyntax builtin_initialize registerTraceClass `Elab.match_syntax registerTraceClass `Elab.match_syntax.alt (inherited := true) registerTraceClass `Elab.match_syntax.result (inherited := true) end Lean.Elab.Term.Quotation
deec1cb58c3f1e773118588a778c2d78b3b5885e
94e33a31faa76775069b071adea97e86e218a8ee
/src/order/modular_lattice.lean
2be73b526f6ca2c82fbb5935c82a3f4717706c48
[ "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
14,749
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, YaΓ«l Dillies -/ import order.cover import order.lattice_intervals /-! # Modular Lattices This file defines (semi)modular lattices, a kind of lattice useful in algebra. For examples, look to the subobject lattices of abelian groups, submodules, and ideals, or consider any distributive lattice. ## Typeclasses We define (semi)modularity typeclasses as Prop-valued mixins. * `is_weak_upper_modular_lattice`: Weakly upper modular lattices. Lattice where `a βŠ” b` covers `a` and `b` if `a` and `b` both cover `a βŠ“ b`. * `is_weak_lower_modular_lattice`: Weakly lower modular lattices. Lattice where `a` and `b` cover `a βŠ“ b` if `a βŠ” b` covers both `a` and `b` * `is_upper_modular_lattice`: Upper modular lattices. Lattices where `a βŠ” b` covers `a` if `b` covers `a βŠ“ b`. * `is_lower_modular_lattice`: Lower modular lattices. Lattices where `a` covers `a βŠ“ b` if `a βŠ” b` covers `b`. - `is_modular_lattice`: Modular lattices. Lattices where `a ≀ c β†’ (a βŠ” b) βŠ“ c = a βŠ” (b βŠ“ c)`. We only require an inequality because the other direction holds in all lattices. ## Main Definitions - `inf_Icc_order_iso_Icc_sup` gives an order isomorphism between the intervals `[a βŠ“ b, a]` and `[b, a βŠ” b]`. This corresponds to the diamond (or second) isomorphism theorems of algebra. ## Main Results - `is_modular_lattice_iff_inf_sup_inf_assoc`: Modularity is equivalent to the `inf_sup_inf_assoc`: `(x βŠ“ z) βŠ” (y βŠ“ z) = ((x βŠ“ z) βŠ” y) βŠ“ z` - `distrib_lattice.is_modular_lattice`: Distributive lattices are modular. ## References * [Manfred Stern, *Semimodular lattices. {Theory} and applications*][stern2009] * [Wikipedia, *Modular Lattice*][https://en.wikipedia.org/wiki/Modular_lattice] ## TODO - Relate atoms and coatoms in modular lattices - Prove that a modular lattice is both upper and lower modular. -/ variable {Ξ± : Type*} /-- A weakly upper modular lattice is a lattice where `a βŠ” b` covers `a` and `b` if `a` and `b` both cover `a βŠ“ b`. -/ class is_weak_upper_modular_lattice (Ξ± : Type*) [lattice Ξ±] : Prop := (covby_sup_of_inf_covby_covby {a b : Ξ±} : a βŠ“ b β‹– a β†’ a βŠ“ b β‹– b β†’ a β‹– a βŠ” b) /-- A weakly lower modular lattice is a lattice where `a` and `b` cover `a βŠ“ b` if `a βŠ” b` covers both `a` and `b`. -/ class is_weak_lower_modular_lattice (Ξ± : Type*) [lattice Ξ±] : Prop := (inf_covby_of_covby_covby_sup {a b : Ξ±} : a β‹– a βŠ” b β†’ b β‹– a βŠ” b β†’ a βŠ“ b β‹– a) /-- An upper modular lattice, aka semimodular lattice, is a lattice where `a βŠ” b` covers `a` and `b` if either `a` or `b` covers `a βŠ“ b`. -/ class is_upper_modular_lattice (Ξ± : Type*) [lattice Ξ±] : Prop := (covby_sup_of_inf_covby {a b : Ξ±} : a βŠ“ b β‹– a β†’ b β‹– a βŠ” b) /-- A lower modular lattice is a lattice where `a` and `b` both cover `a βŠ“ b` if `a βŠ” b` covers either `a` or `b`. -/ class is_lower_modular_lattice (Ξ± : Type*) [lattice Ξ±] : Prop := (inf_covby_of_covby_sup {a b : Ξ±} : a β‹– a βŠ” b β†’ a βŠ“ b β‹– b) /-- A modular lattice is one with a limited associativity between `βŠ“` and `βŠ”`. -/ class is_modular_lattice (Ξ± : Type*) [lattice Ξ±] : Prop := (sup_inf_le_assoc_of_le : βˆ€ {x : Ξ±} (y : Ξ±) {z : Ξ±}, x ≀ z β†’ (x βŠ” y) βŠ“ z ≀ x βŠ” (y βŠ“ z)) section weak_upper_modular variables [lattice Ξ±] [is_weak_upper_modular_lattice Ξ±] {a b : Ξ±} lemma covby_sup_of_inf_covby_of_inf_covby_left : a βŠ“ b β‹– a β†’ a βŠ“ b β‹– b β†’ a β‹– a βŠ” b := is_weak_upper_modular_lattice.covby_sup_of_inf_covby_covby lemma covby_sup_of_inf_covby_of_inf_covby_right : a βŠ“ b β‹– a β†’ a βŠ“ b β‹– b β†’ b β‹– a βŠ” b := by { rw [inf_comm, sup_comm], exact Ξ» ha hb, covby_sup_of_inf_covby_of_inf_covby_left hb ha } alias covby_sup_of_inf_covby_of_inf_covby_left ← covby.sup_of_inf_of_inf_left alias covby_sup_of_inf_covby_of_inf_covby_right ← covby.sup_of_inf_of_inf_right instance : is_weak_lower_modular_lattice (order_dual Ξ±) := ⟨λ a b ha hb, (ha.of_dual.sup_of_inf_of_inf_left hb.of_dual).to_dual⟩ end weak_upper_modular section weak_lower_modular variables [lattice Ξ±] [is_weak_lower_modular_lattice Ξ±] {a b : Ξ±} lemma inf_covby_of_covby_sup_of_covby_sup_left : a β‹– a βŠ” b β†’ b β‹– a βŠ” b β†’ a βŠ“ b β‹– a := is_weak_lower_modular_lattice.inf_covby_of_covby_covby_sup lemma inf_covby_of_covby_sup_of_covby_sup_right : a β‹– a βŠ” b β†’ b β‹– a βŠ” b β†’ a βŠ“ b β‹– b := by { rw [sup_comm, inf_comm], exact Ξ» ha hb, inf_covby_of_covby_sup_of_covby_sup_left hb ha } alias inf_covby_of_covby_sup_of_covby_sup_left ← covby.inf_of_sup_of_sup_left alias inf_covby_of_covby_sup_of_covby_sup_right ← covby.inf_of_sup_of_sup_right instance : is_weak_upper_modular_lattice (order_dual Ξ±) := ⟨λ a b ha hb, (ha.of_dual.inf_of_sup_of_sup_left hb.of_dual).to_dual⟩ end weak_lower_modular section upper_modular variables [lattice Ξ±] [is_upper_modular_lattice Ξ±] {a b : Ξ±} lemma covby_sup_of_inf_covby_left : a βŠ“ b β‹– a β†’ b β‹– a βŠ” b := is_upper_modular_lattice.covby_sup_of_inf_covby lemma covby_sup_of_inf_covby_right : a βŠ“ b β‹– b β†’ a β‹– a βŠ” b := by { rw [sup_comm, inf_comm], exact covby_sup_of_inf_covby_left } alias covby_sup_of_inf_covby_left ← covby.sup_of_inf_left alias covby_sup_of_inf_covby_right ← covby.sup_of_inf_right @[priority 100] -- See note [lower instance priority] instance is_upper_modular_lattice.to_is_weak_upper_modular_lattice : is_weak_upper_modular_lattice Ξ± := ⟨λ a b _, covby.sup_of_inf_right⟩ instance : is_lower_modular_lattice (order_dual Ξ±) := ⟨λ a b h, h.of_dual.sup_of_inf_left.to_dual⟩ end upper_modular section lower_modular variables [lattice Ξ±] [is_lower_modular_lattice Ξ±] {a b : Ξ±} lemma inf_covby_of_covby_sup_left : a β‹– a βŠ” b β†’ a βŠ“ b β‹– b := is_lower_modular_lattice.inf_covby_of_covby_sup lemma inf_covby_of_covby_sup_right : b β‹– a βŠ” b β†’ a βŠ“ b β‹– a := by { rw [inf_comm, sup_comm], exact inf_covby_of_covby_sup_left } alias inf_covby_of_covby_sup_left ← covby.inf_of_sup_left alias inf_covby_of_covby_sup_right ← covby.inf_of_sup_right @[priority 100] -- See note [lower instance priority] instance is_lower_modular_lattice.to_is_weak_lower_modular_lattice : is_weak_lower_modular_lattice Ξ± := ⟨λ a b _, covby.inf_of_sup_right⟩ instance : is_upper_modular_lattice (order_dual Ξ±) := ⟨λ a b h, h.of_dual.inf_of_sup_left.to_dual⟩ end lower_modular section is_modular_lattice variables [lattice Ξ±] [is_modular_lattice Ξ±] theorem sup_inf_assoc_of_le {x : Ξ±} (y : Ξ±) {z : Ξ±} (h : x ≀ z) : (x βŠ” y) βŠ“ z = x βŠ” (y βŠ“ z) := le_antisymm (is_modular_lattice.sup_inf_le_assoc_of_le y h) (le_inf (sup_le_sup_left inf_le_left _) (sup_le h inf_le_right)) theorem is_modular_lattice.inf_sup_inf_assoc {x y z : Ξ±} : (x βŠ“ z) βŠ” (y βŠ“ z) = ((x βŠ“ z) βŠ” y) βŠ“ z := (sup_inf_assoc_of_le y inf_le_right).symm lemma inf_sup_assoc_of_le {x : Ξ±} (y : Ξ±) {z : Ξ±} (h : z ≀ x) : (x βŠ“ y) βŠ” z = x βŠ“ (y βŠ” z) := by rw [inf_comm, sup_comm, ← sup_inf_assoc_of_le y h, inf_comm, sup_comm] instance : is_modular_lattice Ξ±α΅’α΅ˆ := ⟨λ x y z xz, le_of_eq (by { rw [inf_comm, sup_comm, eq_comm, inf_comm, sup_comm], exact @sup_inf_assoc_of_le Ξ± _ _ _ y _ xz })⟩ variables {x y z : Ξ±} theorem is_modular_lattice.sup_inf_sup_assoc : (x βŠ” z) βŠ“ (y βŠ” z) = ((x βŠ” z) βŠ“ y) βŠ” z := @is_modular_lattice.inf_sup_inf_assoc Ξ±α΅’α΅ˆ _ _ _ _ _ theorem eq_of_le_of_inf_le_of_sup_le (hxy : x ≀ y) (hinf : y βŠ“ z ≀ x βŠ“ z) (hsup : y βŠ” z ≀ x βŠ” z) : x = y := le_antisymm hxy $ have h : y ≀ x βŠ” z, from calc y ≀ y βŠ” z : le_sup_left ... ≀ x βŠ” z : hsup, calc y ≀ (x βŠ” z) βŠ“ y : le_inf h le_rfl ... = x βŠ” (z βŠ“ y) : sup_inf_assoc_of_le _ hxy ... ≀ x βŠ” (z βŠ“ x) : sup_le_sup_left (by rw [inf_comm, @inf_comm _ _ z]; exact hinf) _ ... ≀ x : sup_le le_rfl inf_le_right theorem sup_lt_sup_of_lt_of_inf_le_inf (hxy : x < y) (hinf : y βŠ“ z ≀ x βŠ“ z) : x βŠ” z < y βŠ” z := lt_of_le_of_ne (sup_le_sup_right (le_of_lt hxy) _) (Ξ» hsup, ne_of_lt hxy $ eq_of_le_of_inf_le_of_sup_le (le_of_lt hxy) hinf (le_of_eq hsup.symm)) theorem inf_lt_inf_of_lt_of_sup_le_sup (hxy : x < y) (hinf : y βŠ” z ≀ x βŠ” z) : x βŠ“ z < y βŠ“ z := @sup_lt_sup_of_lt_of_inf_le_inf Ξ±α΅’α΅ˆ _ _ _ _ _ hxy hinf /-- A generalization of the theorem that if `N` is a submodule of `M` and `N` and `M / N` are both Artinian, then `M` is Artinian. -/ theorem well_founded_lt_exact_sequence {Ξ² Ξ³ : Type*} [partial_order Ξ²] [preorder Ξ³] (h₁ : well_founded ((<) : Ξ² β†’ Ξ² β†’ Prop)) (hβ‚‚ : well_founded ((<) : Ξ³ β†’ Ξ³ β†’ Prop)) (K : Ξ±) (f₁ : Ξ² β†’ Ξ±) (fβ‚‚ : Ξ± β†’ Ξ²) (g₁ : Ξ³ β†’ Ξ±) (gβ‚‚ : Ξ± β†’ Ξ³) (gci : galois_coinsertion f₁ fβ‚‚) (gi : galois_insertion gβ‚‚ g₁) (hf : βˆ€ a, f₁ (fβ‚‚ a) = a βŠ“ K) (hg : βˆ€ a, g₁ (gβ‚‚ a) = a βŠ” K) : well_founded ((<) : Ξ± β†’ Ξ± β†’ Prop) := subrelation.wf (Ξ» A B hAB, show prod.lex (<) (<) (fβ‚‚ A, gβ‚‚ A) (fβ‚‚ B, gβ‚‚ B), begin simp only [prod.lex_def, lt_iff_le_not_le, ← gci.l_le_l_iff, ← gi.u_le_u_iff, hf, hg, le_antisymm_iff], simp only [gci.l_le_l_iff, gi.u_le_u_iff, ← lt_iff_le_not_le, ← le_antisymm_iff], cases lt_or_eq_of_le (inf_le_inf_right K (le_of_lt hAB)) with h h, { exact or.inl h }, { exact or.inr ⟨h, sup_lt_sup_of_lt_of_inf_le_inf hAB (le_of_eq h.symm)⟩ } end) (inv_image.wf _ (prod.lex_wf h₁ hβ‚‚)) /-- A generalization of the theorem that if `N` is a submodule of `M` and `N` and `M / N` are both Noetherian, then `M` is Noetherian. -/ theorem well_founded_gt_exact_sequence {Ξ² Ξ³ : Type*} [preorder Ξ²] [partial_order Ξ³] (h₁ : well_founded ((>) : Ξ² β†’ Ξ² β†’ Prop)) (hβ‚‚ : well_founded ((>) : Ξ³ β†’ Ξ³ β†’ Prop)) (K : Ξ±) (f₁ : Ξ² β†’ Ξ±) (fβ‚‚ : Ξ± β†’ Ξ²) (g₁ : Ξ³ β†’ Ξ±) (gβ‚‚ : Ξ± β†’ Ξ³) (gci : galois_coinsertion f₁ fβ‚‚) (gi : galois_insertion gβ‚‚ g₁) (hf : βˆ€ a, f₁ (fβ‚‚ a) = a βŠ“ K) (hg : βˆ€ a, g₁ (gβ‚‚ a) = a βŠ” K) : well_founded ((>) : Ξ± β†’ Ξ± β†’ Prop) := @well_founded_lt_exact_sequence Ξ±α΅’α΅ˆ _ _ Ξ³α΅’α΅ˆ Ξ²α΅’α΅ˆ _ _ hβ‚‚ h₁ K g₁ gβ‚‚ f₁ fβ‚‚ gi.dual gci.dual hg hf /-- The diamond isomorphism between the intervals `[a βŠ“ b, a]` and `[b, a βŠ” b]` -/ def inf_Icc_order_iso_Icc_sup (a b : Ξ±) : set.Icc (a βŠ“ b) a ≃o set.Icc b (a βŠ” b) := { to_fun := Ξ» x, ⟨x βŠ” b, ⟨le_sup_right, sup_le_sup_right x.prop.2 b⟩⟩, inv_fun := Ξ» x, ⟨a βŠ“ x, ⟨inf_le_inf_left a x.prop.1, inf_le_left⟩⟩, left_inv := Ξ» x, subtype.ext (by { change a βŠ“ (↑x βŠ” b) = ↑x, rw [sup_comm, ← inf_sup_assoc_of_le _ x.prop.2, sup_eq_right.2 x.prop.1] }), right_inv := Ξ» x, subtype.ext (by { change a βŠ“ ↑x βŠ” b = ↑x, rw [inf_comm, inf_sup_assoc_of_le _ x.prop.1, inf_eq_left.2 x.prop.2] }), map_rel_iff' := Ξ» x y, begin simp only [subtype.mk_le_mk, equiv.coe_fn_mk, and_true, le_sup_right], rw [← subtype.coe_le_coe], refine ⟨λ h, _, Ξ» h, sup_le_sup_right h _⟩, rw [← sup_eq_right.2 x.prop.1, inf_sup_assoc_of_le _ x.prop.2, sup_comm, ← sup_eq_right.2 y.prop.1, inf_sup_assoc_of_le _ y.prop.2, @sup_comm _ _ b], exact inf_le_inf_left _ h end } end is_modular_lattice namespace is_compl variables [lattice Ξ±] [bounded_order Ξ±] [is_modular_lattice Ξ±] /-- The diamond isomorphism between the intervals `set.Iic a` and `set.Ici b`. -/ def Iic_order_iso_Ici {a b : Ξ±} (h : is_compl a b) : set.Iic a ≃o set.Ici b := (order_iso.set_congr (set.Iic a) (set.Icc (a βŠ“ b) a) (h.inf_eq_bot.symm β–Έ set.Icc_bot.symm)).trans $ (inf_Icc_order_iso_Icc_sup a b).trans (order_iso.set_congr (set.Icc b (a βŠ” b)) (set.Ici b) (h.sup_eq_top.symm β–Έ set.Icc_top)) end is_compl theorem is_modular_lattice_iff_inf_sup_inf_assoc [lattice Ξ±] : is_modular_lattice Ξ± ↔ βˆ€ (x y z : Ξ±), (x βŠ“ z) βŠ” (y βŠ“ z) = ((x βŠ“ z) βŠ” y) βŠ“ z := ⟨λ h, @is_modular_lattice.inf_sup_inf_assoc _ _ h, Ξ» h, ⟨λ x y z xz, by rw [← inf_eq_left.2 xz, h]⟩⟩ namespace distrib_lattice @[priority 100] instance [distrib_lattice Ξ±] : is_modular_lattice Ξ± := ⟨λ x y z xz, by rw [inf_sup_right, inf_eq_left.2 xz]⟩ end distrib_lattice theorem disjoint.disjoint_sup_right_of_disjoint_sup_left [lattice Ξ±] [order_bot Ξ±] [is_modular_lattice Ξ±] {a b c : Ξ±} (h : disjoint a b) (hsup : disjoint (a βŠ” b) c) : disjoint a (b βŠ” c) := begin rw [disjoint, ← h.eq_bot, sup_comm], apply le_inf inf_le_left, apply (inf_le_inf_right (c βŠ” b) le_sup_right).trans, rw [sup_comm, is_modular_lattice.sup_inf_sup_assoc, hsup.eq_bot, bot_sup_eq] end theorem disjoint.disjoint_sup_left_of_disjoint_sup_right [lattice Ξ±] [order_bot Ξ±] [is_modular_lattice Ξ±] {a b c : Ξ±} (h : disjoint b c) (hsup : disjoint a (b βŠ” c)) : disjoint (a βŠ” b) c := begin rw [disjoint.comm, sup_comm], apply disjoint.disjoint_sup_right_of_disjoint_sup_left h.symm, rwa [sup_comm, disjoint.comm] at hsup, end namespace is_modular_lattice variables [lattice Ξ±] [is_modular_lattice Ξ±] {a : Ξ±} instance is_modular_lattice_Iic : is_modular_lattice (set.Iic a) := ⟨λ x y z xz, (sup_inf_le_assoc_of_le (y : Ξ±) xz : (↑x βŠ” ↑y) βŠ“ ↑z ≀ ↑x βŠ” ↑y βŠ“ ↑z)⟩ instance is_modular_lattice_Ici : is_modular_lattice (set.Ici a) := ⟨λ x y z xz, (sup_inf_le_assoc_of_le (y : Ξ±) xz : (↑x βŠ” ↑y) βŠ“ ↑z ≀ ↑x βŠ” ↑y βŠ“ ↑z)⟩ section is_complemented variables [bounded_order Ξ±] [is_complemented Ξ±] instance is_complemented_Iic : is_complemented (set.Iic a) := ⟨λ ⟨x, hx⟩, let ⟨y, hy⟩ := exists_is_compl x in ⟨⟨y βŠ“ a, set.mem_Iic.2 inf_le_right⟩, begin split, { change x βŠ“ (y βŠ“ a) ≀ βŠ₯, -- improve lattice subtype API rw ← inf_assoc, exact le_trans inf_le_left hy.1 }, { change a ≀ x βŠ” (y βŠ“ a), -- improve lattice subtype API rw [← sup_inf_assoc_of_le _ (set.mem_Iic.1 hx), top_le_iff.1 hy.2, top_inf_eq] } end⟩⟩ instance is_complemented_Ici : is_complemented (set.Ici a) := ⟨λ ⟨x, hx⟩, let ⟨y, hy⟩ := exists_is_compl x in ⟨⟨y βŠ” a, set.mem_Ici.2 le_sup_right⟩, begin split, { change x βŠ“ (y βŠ” a) ≀ a, -- improve lattice subtype API rw [← inf_sup_assoc_of_le _ (set.mem_Ici.1 hx), le_bot_iff.1 hy.1, bot_sup_eq] }, { change ⊀ ≀ x βŠ” (y βŠ” a), -- improve lattice subtype API rw ← sup_assoc, exact le_trans hy.2 le_sup_left } end⟩⟩ end is_complemented end is_modular_lattice
0433ebec8641203ba80dc992cb2574f776f05b24
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/library/theories/group_theory/pgroup.lean
58d2a26c6d107c7a4f75507685d1f1beb17d4774
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
15,468
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ import theories.number_theory.primes data algebra.group algebra.group_power algebra.group_bigops import .cyclic .finsubg .hom .perm .action open nat fin list function subtype namespace group_theory section pgroup open finset fintype variables {G S : Type} [ambientG : group G] [deceqG : decidable_eq G] [finS : fintype S] [deceqS : decidable_eq S] include ambientG definition psubg (H : finset G) (p m : nat) : Prop := prime p ∧ card H = p^m include deceqG finS deceqS variables {H : finset G} [subgH : is_finsubg H] include subgH variables {hom : G β†’ perm S} [Hom : is_hom_class hom] include Hom open finset.partition lemma card_mod_eq_of_action_by_psubg {p : nat} : βˆ€ {m : nat}, psubg H p m β†’ (card S) % p = (card (fixed_points hom H)) % p | 0 := by rewrite [↑psubg, pow_zero]; intro Psubg; rewrite [finsubg_eq_singleton_one_of_card_one (and.right Psubg), fixed_points_of_one] | (succ m) := take Ppsubg, begin rewrite [@orbit_class_equation' G S ambientG _ finS deceqS hom Hom H subgH], apply add_mod_eq_of_dvd, apply dvd_Sum_of_dvd, intro s Psin, rewrite mem_sep_iff at Psin, cases Psin with Psinorbs Pcardne, esimp [orbits, equiv_classes, orbit_partition] at Psinorbs, rewrite mem_image_iff at Psinorbs, cases Psinorbs with a Pa, cases Pa with Pain Porb, substvars, cases Ppsubg with Pprime PcardH, assert Pdvd : card (orbit hom H a) ∣ p ^ (succ m), rewrite -PcardH, apply dvd_of_eq_mul (finset.card (stab hom H a)), apply orbit_stabilizer_theorem, apply or.elim (eq_one_or_dvd_of_dvd_prime_pow Pprime Pdvd), intro Pcardeq, contradiction, intro Ppdvd, exact Ppdvd end end pgroup section psubg_cosets open finset fintype variables {G : Type} [ambientG : group G] [finG : fintype G] [deceqG : decidable_eq G] include ambientG deceqG finG variables {H : finset G} [finsubgH : is_finsubg H] include finsubgH lemma card_psubg_cosets_mod_eq {p : nat} {m : nat} : psubg H p m β†’ (card (lcoset_type univ H)) % p = card (lcoset_type (normalizer H) H) % p := assume Psubg, by rewrite [-card_aol_fixed_points_eq_card_cosets]; exact card_mod_eq_of_action_by_psubg Psubg end psubg_cosets section cauchy lemma prodl_rotl_eq_one_of_prodl_eq_one {A B : Type} [gB : group B] {f : A β†’ B} : βˆ€ {l : list A}, Prodl l f = 1 β†’ Prodl (list.rotl l) f = 1 | nil := assume Peq, rfl | (a::l) := begin rewrite [rotl_cons, Prodl_cons f, Prodl_append _ _ f, Prodl_singleton], exact mul_eq_one_of_mul_eq_one end section rotl_peo variables {A : Type} [ambA : group A] include ambA variable [finA : fintype A] include finA variable (A) definition all_prodl_eq_one (n : nat) : list (list A) := map (Ξ» l, cons (Prodl l id)⁻¹ l) (all_lists_of_len n) variable {A} lemma prodl_eq_one_of_mem_all_prodl_eq_one {n : nat} {l : list A} : l ∈ all_prodl_eq_one A n β†’ Prodl l id = 1 := assume Plin, obtain l' Pl' Pl, from exists_of_mem_map Plin, by substvars; rewrite [Prodl_cons id _ l', mul.left_inv] lemma length_of_mem_all_prodl_eq_one {n : nat} {l : list A} : l ∈ all_prodl_eq_one A n β†’ length l = succ n := assume Plin, obtain l' Pl' Pl, from exists_of_mem_map Plin, begin substvars, rewrite [length_cons, length_mem_all_lists Pl'] end lemma nodup_all_prodl_eq_one {n : nat} : nodup (all_prodl_eq_one A n) := nodup_map (take l₁ lβ‚‚ Peq, tail_eq_of_cons_eq Peq) nodup_all_lists lemma all_prodl_eq_one_complete {n : nat} : βˆ€ {l : list A}, length l = succ n β†’ Prodl l id = 1 β†’ l ∈ all_prodl_eq_one A n | nil := assume Pleq, by contradiction | (a::l) := assume Pleq Pprod, begin rewrite length_cons at Pleq, rewrite (Prodl_cons id a l) at Pprod, rewrite [eq_inv_of_mul_eq_one Pprod], apply mem_map, apply mem_all_lists, apply succ.inj Pleq end open fintype lemma length_all_prodl_eq_one {n : nat} : length (@all_prodl_eq_one A _ _ n) = (card A)^n := eq.trans !length_map length_all_lists open fin definition prodseq {n : nat} (s : seq A n) : A := Prodl (upto n) s definition peo [reducible] {n : nat} (s : seq A n) := prodseq s = 1 definition constseq {n : nat} (s : seq A (succ n)) := βˆ€ i, s i = s !zero lemma prodseq_eq {n :nat} {s : seq A n} : prodseq s = Prodl (fun_to_list s) id := Prodl_map lemma prodseq_eq_pow_of_constseq {n : nat} (s : seq A (succ n)) : constseq s β†’ prodseq s = (s !zero) ^ succ n := assume Pc, assert Pcl : βˆ€ i, i ∈ upto (succ n) β†’ s i = s !zero, from take i, assume Pin, Pc i, by rewrite [↑prodseq, Prodl_eq_pow_of_const _ Pcl, fin.length_upto] lemma seq_eq_of_constseq_of_eq {n : nat} {s₁ sβ‚‚ : seq A (succ n)} : constseq s₁ β†’ constseq sβ‚‚ β†’ s₁ !zero = sβ‚‚ !zero β†’ s₁ = sβ‚‚ := assume Pc₁ Pcβ‚‚ Peq, funext take i, by rewrite [Pc₁ i, Pcβ‚‚ i, Peq] lemma peo_const_one : βˆ€ {n : nat}, peo (Ξ» i : fin n, (1 : A)) | 0 := rfl | (succ n) := let s := Ξ» i : fin (succ n), (1 : A) in assert Pconst : constseq s, from take i, rfl, calc prodseq s = (s !zero) ^ succ n : prodseq_eq_pow_of_constseq s Pconst ... = (1 : A) ^ succ n : rfl ... = 1 : one_pow variable [deceqA : decidable_eq A] include deceqA variable (A) definition peo_seq [reducible] (n : nat) := {s : seq A (succ n) | peo s} definition peo_seq_one (n : nat) : peo_seq A n := tag (Ξ» i : fin (succ n), (1 : A)) peo_const_one definition all_prodseq_eq_one (n : nat) : list (seq A (succ n)) := dmap (Ξ» l, length l = card (fin (succ n))) list_to_fun (all_prodl_eq_one A n) definition all_peo_seqs (n : nat) : list (peo_seq A n) := dmap peo tag (all_prodseq_eq_one A n) variable {A} lemma prodseq_eq_one_of_mem_all_prodseq_eq_one {n : nat} {s : seq A (succ n)} : s ∈ all_prodseq_eq_one A n β†’ prodseq s = 1 := assume Psin, obtain l Pex, from exists_of_mem_dmap Psin, obtain leq Pin Peq, from Pex, by rewrite [prodseq_eq, Peq, list_to_fun_to_list, prodl_eq_one_of_mem_all_prodl_eq_one Pin] lemma all_prodseq_eq_one_complete {n : nat} {s : seq A (succ n)} : prodseq s = 1 β†’ s ∈ all_prodseq_eq_one A n := assume Peq, assert Plin : map s (elems (fin (succ n))) ∈ all_prodl_eq_one A n, from begin apply all_prodl_eq_one_complete, rewrite [length_map], exact length_upto (succ n), rewrite prodseq_eq at Peq, exact Peq end, assert Psin : list_to_fun (map s (elems (fin (succ n)))) (length_map_of_fintype s) ∈ all_prodseq_eq_one A n, from mem_dmap _ Plin, by rewrite [fun_eq_list_to_fun_map s (length_map_of_fintype s)]; apply Psin lemma nodup_all_prodseq_eq_one {n : nat} : nodup (all_prodseq_eq_one A n) := dmap_nodup_of_dinj dinj_list_to_fun nodup_all_prodl_eq_one lemma rotl1_peo_of_peo {n : nat} {s : seq A n} : peo s β†’ peo (rotl_fun 1 s) := begin rewrite [↑peo, *prodseq_eq, seq_rotl_eq_list_rotl], apply prodl_rotl_eq_one_of_prodl_eq_one end section local attribute perm.f [coercion] lemma rotl_perm_peo_of_peo {n : nat} : βˆ€ {m} {s : seq A n}, peo s β†’ peo (rotl_perm A n m s) | 0 := begin rewrite [↑rotl_perm, rotl_seq_zero], intros, assumption end | (succ m) := take s, assert Pmul : rotl_perm A n (m + 1) s = rotl_fun 1 (rotl_perm A n m s), from calc s ∘ (rotl (m + 1)) = s ∘ ((rotl m) ∘ (rotl 1)) : rotl_compose ... = s ∘ (rotl m) ∘ (rotl 1) : compose.assoc, begin rewrite [-add_one, Pmul], intro P, exact rotl1_peo_of_peo (rotl_perm_peo_of_peo P) end end lemma nodup_all_peo_seqs {n : nat} : nodup (all_peo_seqs A n) := dmap_nodup_of_dinj (dinj_tag peo) nodup_all_prodseq_eq_one lemma all_peo_seqs_complete {n : nat} : βˆ€ s : peo_seq A n, s ∈ all_peo_seqs A n := take ps, subtype.destruct ps (take s, assume Ps, assert Pin : s ∈ all_prodseq_eq_one A n, from all_prodseq_eq_one_complete Ps, mem_dmap Ps Pin) lemma length_all_peo_seqs {n : nat} : length (all_peo_seqs A n) = (card A)^n := eq.trans (eq.trans (show length (all_peo_seqs A n) = length (all_prodseq_eq_one A n), from assert Pmap : map elt_of (all_peo_seqs A n) = all_prodseq_eq_one A n, from map_dmap_of_inv_of_pos (Ξ» s P, rfl) (Ξ» s, prodseq_eq_one_of_mem_all_prodseq_eq_one), by rewrite [-Pmap, length_map]) (show length (all_prodseq_eq_one A n) = length (all_prodl_eq_one A n), from assert Pmap : map fun_to_list (all_prodseq_eq_one A n) = all_prodl_eq_one A n, from map_dmap_of_inv_of_pos list_to_fun_to_list (Ξ» l Pin, by rewrite [length_of_mem_all_prodl_eq_one Pin, card_fin]), by rewrite [-Pmap, length_map])) length_all_prodl_eq_one definition peo_seq_is_fintype [instance] {n : nat} : fintype (peo_seq A n) := fintype.mk (all_peo_seqs A n) nodup_all_peo_seqs all_peo_seqs_complete lemma card_peo_seq {n : nat} : card (peo_seq A n) = (card A)^n := length_all_peo_seqs section variable (A) local attribute perm.f [coercion] definition rotl_peo_seq (n : nat) (m : nat) (s : peo_seq A n) : peo_seq A n := tag (rotl_perm A (succ n) m (elt_of s)) (rotl_perm_peo_of_peo (has_property s)) variable {A} end lemma rotl_peo_seq_zero {n : nat} : rotl_peo_seq A n 0 = id := funext take s, subtype.eq begin rewrite [↑rotl_peo_seq, ↑rotl_perm, rotl_seq_zero] end lemma rotl_peo_seq_id {n : nat} : rotl_peo_seq A n (succ n) = id := funext take s, subtype.eq begin rewrite [↑rotl_peo_seq, -rotl_perm_pow_eq, rotl_perm_pow_eq_one] end lemma rotl_peo_seq_compose {n i j : nat} : (rotl_peo_seq A n i) ∘ (rotl_peo_seq A n j) = rotl_peo_seq A n (j + i) := funext take s, subtype.eq begin rewrite [↑rotl_peo_seq, ↑rotl_perm, ↑rotl_fun, compose.assoc, rotl_compose] end lemma rotl_peo_seq_mod {n i : nat} : rotl_peo_seq A n i = rotl_peo_seq A n (i % succ n) := funext take s, subtype.eq begin rewrite [↑rotl_peo_seq, rotl_perm_mod] end lemma rotl_peo_seq_inj {n m : nat} : injective (rotl_peo_seq A n m) := take ps₁ psβ‚‚, subtype.destruct ps₁ (Ξ» s₁ P₁, subtype.destruct psβ‚‚ (Ξ» sβ‚‚ Pβ‚‚, assume Peq, tag_eq (rotl_fun_inj (dinj_tag peo _ _ Peq)))) variable (A) definition rotl_perm_ps [reducible] (n : nat) (m : fin (succ n)) : perm (peo_seq A n) := perm.mk (rotl_peo_seq A n m) rotl_peo_seq_inj variable {A} variable {n : nat} lemma rotl_perm_ps_eq {m : fin (succ n)} {s : peo_seq A n} : elt_of (perm.f (rotl_perm_ps A n m) s) = perm.f (rotl_perm A (succ n) m) (elt_of s) := rfl lemma rotl_perm_ps_eq_of_rotl_perm_eq {i j : fin (succ n)} : (rotl_perm A (succ n) i) = (rotl_perm A (succ n) j) β†’ (rotl_perm_ps A n i) = (rotl_perm_ps A n j) := assume Peq, eq_of_feq (funext take s, subtype.eq (by rewrite [*rotl_perm_ps_eq, Peq])) lemma rotl_perm_ps_hom (i j : fin (succ n)) : rotl_perm_ps A n (i+j) = (rotl_perm_ps A n i) * (rotl_perm_ps A n j) := eq_of_feq (begin rewrite [↑rotl_perm_ps, {val (i+j)}val_madd, add.comm, -rotl_peo_seq_mod, -rotl_peo_seq_compose] end) section local attribute group_of_add_group [instance] definition rotl_perm_ps_is_hom [instance] : is_hom_class (rotl_perm_ps A n) := is_hom_class.mk rotl_perm_ps_hom open finset lemma const_of_is_fixed_point {s : peo_seq A n} : is_fixed_point (rotl_perm_ps A n) univ s β†’ constseq (elt_of s) := assume Pfp, take i, begin rewrite [-(Pfp i !mem_univ) at {1}, rotl_perm_ps_eq, ↑rotl_perm, ↑rotl_fun, {i}mk_mod_eq at {2}, rotl_to_zero] end lemma const_of_rotl_fixed_point {s : peo_seq A n} : s ∈ fixed_points (rotl_perm_ps A n) univ β†’ constseq (elt_of s) := assume Psin, take i, begin apply const_of_is_fixed_point, exact is_fixed_point_of_mem_fixed_points Psin end lemma pow_eq_one_of_mem_fixed_points {s : peo_seq A n} : s ∈ fixed_points (rotl_perm_ps A n) univ β†’ (elt_of s !zero)^(succ n) = 1 := assume Psin, eq.trans (eq.symm (prodseq_eq_pow_of_constseq (elt_of s) (const_of_rotl_fixed_point Psin))) (has_property s) lemma peo_seq_one_is_fixed_point : is_fixed_point (rotl_perm_ps A n) univ (peo_seq_one A n) := take h, assume Pin, by esimp [rotl_perm_ps] lemma peo_seq_one_mem_fixed_points : peo_seq_one A n ∈ fixed_points (rotl_perm_ps A n) univ := mem_fixed_points_of_exists_of_is_fixed_point (exists.intro !zero !mem_univ) peo_seq_one_is_fixed_point lemma generator_of_prime_of_dvd_order {p : nat} : prime p β†’ p ∣ card A β†’ βˆƒ g : A, g β‰  1 ∧ g^p = 1 := assume Pprime Pdvd, let pp := nat.pred p, spp := nat.succ pp in assert Peq : spp = p, from succ_pred_prime Pprime, assert Ppsubg : psubg (@univ (fin spp) _) spp 1, from and.intro (eq.symm Peq β–Έ Pprime) (by rewrite [Peq, card_fin, pow_one]), have (pow_nat (card A) pp) % spp = (card (fixed_points (rotl_perm_ps A pp) univ)) % spp, by rewrite -card_peo_seq; apply card_mod_eq_of_action_by_psubg Ppsubg, have Pcardmod : (pow_nat (card A) pp) % p = (card (fixed_points (rotl_perm_ps A pp) univ)) % p, from Peq β–Έ this, have Pfpcardmod : (card (fixed_points (rotl_perm_ps A pp) univ)) % p = 0, from eq.trans (eq.symm Pcardmod) (mod_eq_zero_of_dvd (dvd_pow_of_dvd_of_pos Pdvd (pred_prime_pos Pprime))), have Pfpcardpos : card (fixed_points (rotl_perm_ps A pp) univ) > 0, from card_pos_of_mem peo_seq_one_mem_fixed_points, have Pfpcardgt1 : card (fixed_points (rotl_perm_ps A pp) univ) > 1, from gt_one_of_pos_of_prime_dvd Pprime Pfpcardpos Pfpcardmod, obtain s₁ sβ‚‚ Pin₁ Pinβ‚‚ Psnes, from exists_two_of_card_gt_one Pfpcardgt1, decidable.by_cases (Ξ» Pe₁ : elt_of s₁ !zero = 1, assert Pneβ‚‚ : elt_of sβ‚‚ !zero β‰  1, from assume Peβ‚‚, absurd (subtype.eq (seq_eq_of_constseq_of_eq (const_of_rotl_fixed_point Pin₁) (const_of_rotl_fixed_point Pinβ‚‚) (eq.trans Pe₁ (eq.symm Peβ‚‚)))) Psnes, exists.intro (elt_of sβ‚‚ !zero) (and.intro Pneβ‚‚ (Peq β–Έ pow_eq_one_of_mem_fixed_points Pinβ‚‚))) (Ξ» Pne, exists.intro (elt_of s₁ !zero) (and.intro Pne (Peq β–Έ pow_eq_one_of_mem_fixed_points Pin₁))) end theorem cauchy_theorem {p : nat} : prime p β†’ p ∣ card A β†’ βˆƒ g : A, order g = p := assume Pprime Pdvd, obtain g Pne Pgpow, from generator_of_prime_of_dvd_order Pprime Pdvd, assert Porder : order g ∣ p, from order_dvd_of_pow_eq_one Pgpow, or.elim (eq_one_or_eq_self_of_prime_of_dvd Pprime Porder) (Ξ» Pe, absurd (eq_one_of_order_eq_one Pe) Pne) (Ξ» Porderp, exists.intro g Porderp) end rotl_peo end cauchy section sylow open finset fintype variables {G : Type} [ambientG : group G] [finG : fintype G] [deceqG : decidable_eq G] include ambientG deceqG finG theorem first_sylow_theorem {p : nat} (Pp : prime p) : βˆ€ n, p^n ∣ card G β†’ βˆƒ (H : finset G) (finsubgH : is_finsubg H), card H = p^n | 0 := assume Pdvd, exists.intro ('{1}) (exists.intro one_is_finsubg (by rewrite [pow_zero])) | (succ n) := assume Pdvd, obtain H PfinsubgH PcardH, from first_sylow_theorem n (pow_dvd_of_pow_succ_dvd Pdvd), assert Ppsubg : psubg H p n, from and.intro Pp PcardH, assert Ppowsucc : p^(succ n) ∣ (card (lcoset_type univ H) * p^n), by rewrite [-PcardH, -(lagrange_theorem' !subset_univ)]; exact Pdvd, assert Ppdvd : p ∣ card (lcoset_type (normalizer H) H), from dvd_of_mod_eq_zero (by rewrite [-(card_psubg_cosets_mod_eq Ppsubg), -dvd_iff_mod_eq_zero]; exact dvd_of_pow_succ_dvd_mul_pow (pos_of_prime Pp) Ppowsucc), obtain J PJ, from cauchy_theorem Pp Ppdvd, exists.intro (fin_coset_Union (cyc J)) (exists.intro _ (by rewrite [pow_succ, -PcardH, -PJ]; apply card_Union_lcosets)) end sylow end group_theory
9136d593360fd868e449c9a7a63be5c443b2a623
e953c38599905267210b87fb5d82dcc3e52a4214
/hott/hit/circle.hlean
d8cfd91aab59d5a3549aae5e10154b4abdbc3553
[ "Apache-2.0" ]
permissive
c-cube/lean
563c1020bff98441c4f8ba60111fef6f6b46e31b
0fb52a9a139f720be418dafac35104468e293b66
refs/heads/master
1,610,753,294,113
1,440,451,356,000
1,440,499,588,000
41,748,334
0
0
null
1,441,122,656,000
1,441,122,656,000
null
UTF-8
Lean
false
false
9,965
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Declaration of the circle -/ import .sphere import types.bool types.int.hott types.equiv import algebra.fundamental_group algebra.hott open eq susp bool sphere_index is_equiv equiv equiv.ops is_trunc pi definition circle : Typeβ‚€ := sphere 1 namespace circle notation `SΒΉ` := circle definition base1 : circle := !north definition base2 : circle := !south definition seg1 : base1 = base2 := merid !north definition seg2 : base1 = base2 := merid !south definition base : circle := base1 definition loop : base = base := seg2 ⬝ seg1⁻¹ definition rec2 {P : circle β†’ Type} (Pb1 : P base1) (Pb2 : P base2) (Ps1 : Pb1 =[seg1] Pb2) (Ps2 : Pb1 =[seg2] Pb2) (x : circle) : P x := begin induction x with b, { exact Pb1}, { exact Pb2}, { esimp at *, induction b with y, { exact Ps1}, { exact Ps2}, { cases y}}, end definition rec2_on [reducible] {P : circle β†’ Type} (x : circle) (Pb1 : P base1) (Pb2 : P base2) (Ps1 : Pb1 =[seg1] Pb2) (Ps2 : Pb1 =[seg2] Pb2) : P x := circle.rec2 Pb1 Pb2 Ps1 Ps2 x theorem rec2_seg1 {P : circle β†’ Type} (Pb1 : P base1) (Pb2 : P base2) (Ps1 : Pb1 =[seg1] Pb2) (Ps2 : Pb1 =[seg2] Pb2) : apdo (rec2 Pb1 Pb2 Ps1 Ps2) seg1 = Ps1 := !rec_merid theorem rec2_seg2 {P : circle β†’ Type} (Pb1 : P base1) (Pb2 : P base2) (Ps1 : Pb1 =[seg1] Pb2) (Ps2 : Pb1 =[seg2] Pb2) : apdo (rec2 Pb1 Pb2 Ps1 Ps2) seg2 = Ps2 := !rec_merid definition elim2 {P : Type} (Pb1 Pb2 : P) (Ps1 Ps2 : Pb1 = Pb2) (x : circle) : P := rec2 Pb1 Pb2 (pathover_of_eq Ps1) (pathover_of_eq Ps2) x definition elim2_on [reducible] {P : Type} (x : circle) (Pb1 Pb2 : P) (Ps1 : Pb1 = Pb2) (Ps2 : Pb1 = Pb2) : P := elim2 Pb1 Pb2 Ps1 Ps2 x theorem elim2_seg1 {P : Type} (Pb1 Pb2 : P) (Ps1 : Pb1 = Pb2) (Ps2 : Pb1 = Pb2) : ap (elim2 Pb1 Pb2 Ps1 Ps2) seg1 = Ps1 := begin apply eq_of_fn_eq_fn_inv !(pathover_constant seg1), rewrite [β–Έ*,-apdo_eq_pathover_of_eq_ap,↑elim2,rec2_seg1], end theorem elim2_seg2 {P : Type} (Pb1 Pb2 : P) (Ps1 : Pb1 = Pb2) (Ps2 : Pb1 = Pb2) : ap (elim2 Pb1 Pb2 Ps1 Ps2) seg2 = Ps2 := begin apply eq_of_fn_eq_fn_inv !(pathover_constant seg2), rewrite [β–Έ*,-apdo_eq_pathover_of_eq_ap,↑elim2,rec2_seg2], end definition elim2_type (Pb1 Pb2 : Type) (Ps1 Ps2 : Pb1 ≃ Pb2) (x : circle) : Type := elim2 Pb1 Pb2 (ua Ps1) (ua Ps2) x definition elim2_type_on [reducible] (x : circle) (Pb1 Pb2 : Type) (Ps1 Ps2 : Pb1 ≃ Pb2) : Type := elim2_type Pb1 Pb2 Ps1 Ps2 x theorem elim2_type_seg1 (Pb1 Pb2 : Type) (Ps1 Ps2 : Pb1 ≃ Pb2) : transport (elim2_type Pb1 Pb2 Ps1 Ps2) seg1 = Ps1 := by rewrite [tr_eq_cast_ap_fn,↑elim2_type,elim2_seg1];apply cast_ua_fn theorem elim2_type_seg2 (Pb1 Pb2 : Type) (Ps1 Ps2 : Pb1 ≃ Pb2) : transport (elim2_type Pb1 Pb2 Ps1 Ps2) seg2 = Ps2 := by rewrite [tr_eq_cast_ap_fn,↑elim2_type,elim2_seg2];apply cast_ua_fn protected definition rec {P : circle β†’ Type} (Pbase : P base) (Ploop : Pbase =[loop] Pbase) (x : circle) : P x := begin fapply (rec2_on x), { exact Pbase}, { exact (transport P seg1 Pbase)}, { apply pathover_tr}, { apply pathover_tr_of_pathover, exact Ploop} end protected definition rec_on [reducible] {P : circle β†’ Type} (x : circle) (Pbase : P base) (Ploop : Pbase =[loop] Pbase) : P x := circle.rec Pbase Ploop x theorem rec_loop_helper {A : Type} (P : A β†’ Type) {x y z : A} {p : x = y} {p' : z = y} {u : P x} {v : P z} (q : u =[p ⬝ p'⁻¹] v) : pathover_tr_of_pathover q ⬝o !pathover_tr⁻¹ᡒ = q := by cases p'; cases q; exact idp definition con_refl {A : Type} {x y : A} (p : x = y) : p ⬝ refl _ = p := eq.rec_on p idp theorem rec_loop {P : circle β†’ Type} (Pbase : P base) (Ploop : Pbase =[loop] Pbase) : apdo (circle.rec Pbase Ploop) loop = Ploop := begin rewrite [↑loop,apdo_con,↑circle.rec,↑circle.rec2_on,↑base,rec2_seg2,apdo_inv,rec2_seg1], apply rec_loop_helper end protected definition elim {P : Type} (Pbase : P) (Ploop : Pbase = Pbase) (x : circle) : P := circle.rec Pbase (pathover_of_eq Ploop) x protected definition elim_on [reducible] {P : Type} (x : circle) (Pbase : P) (Ploop : Pbase = Pbase) : P := circle.elim Pbase Ploop x theorem elim_loop {P : Type} (Pbase : P) (Ploop : Pbase = Pbase) : ap (circle.elim Pbase Ploop) loop = Ploop := begin apply eq_of_fn_eq_fn_inv !(pathover_constant loop), rewrite [β–Έ*,-apdo_eq_pathover_of_eq_ap,↑circle.elim,rec_loop], end protected definition elim_type (Pbase : Type) (Ploop : Pbase ≃ Pbase) (x : circle) : Type := circle.elim Pbase (ua Ploop) x protected definition elim_type_on [reducible] (x : circle) (Pbase : Type) (Ploop : Pbase ≃ Pbase) : Type := circle.elim_type Pbase Ploop x theorem elim_type_loop (Pbase : Type) (Ploop : Pbase ≃ Pbase) : transport (circle.elim_type Pbase Ploop) loop = Ploop := by rewrite [tr_eq_cast_ap_fn,↑circle.elim_type,circle.elim_loop];apply cast_ua_fn theorem elim_type_loop_inv (Pbase : Type) (Ploop : Pbase ≃ Pbase) : transport (circle.elim_type Pbase Ploop) loop⁻¹ = to_inv Ploop := by rewrite [tr_inv_fn,↑to_inv]; apply inv_eq_inv; apply elim_type_loop end circle attribute circle.base1 circle.base2 circle.base [constructor] attribute circle.rec2 circle.elim2 [unfold 6] [recursor 6] attribute circle.elim2_type [unfold 5] attribute circle.rec2_on circle.elim2_on [unfold 2] attribute circle.elim2_type [unfold 1] attribute circle.rec circle.elim [unfold 4] [recursor 4] attribute circle.elim_type [unfold 3] attribute circle.rec_on circle.elim_on [unfold 2] attribute circle.elim_type_on [unfold 1] namespace circle definition pointed_circle [instance] [constructor] : pointed circle := pointed.mk base definition loop_neq_idp : loop β‰  idp := assume H : loop = idp, have H2 : Ξ {A : Type₁} {a : A} {p : a = a}, p = idp, from Ξ»A a p, calc p = ap (circle.elim a p) loop : elim_loop ... = ap (circle.elim a p) (refl base) : by rewrite H, absurd H2 eq_bnot_ne_idp definition nonidp (x : circle) : x = x := begin induction x, { exact loop}, { apply concato_eq, apply pathover_eq_lr, rewrite [con.left_inv,idp_con]} end definition nonidp_neq_idp : nonidp β‰  (Ξ»x, idp) := assume H : nonidp = Ξ»x, idp, have H2 : loop = idp, from apd10 H base, absurd H2 loop_neq_idp open int protected definition code (x : circle) : Typeβ‚€ := circle.elim_type_on x β„€ equiv_succ definition transport_code_loop (a : β„€) : transport circle.code loop a = succ a := ap10 !elim_type_loop a definition transport_code_loop_inv (a : β„€) : transport circle.code loop⁻¹ a = pred a := ap10 !elim_type_loop_inv a protected definition encode {x : circle} (p : base = x) : circle.code x := transport circle.code p (of_num 0) -- why is the explicit coercion needed here? protected definition decode {x : circle} : circle.code x β†’ base = x := begin induction x, { exact power loop}, { apply arrow_pathover_left, intro b, apply concato_eq, apply pathover_eq_r, rewrite [power_con,transport_code_loop]} end --remove this theorem after #484 theorem encode_decode {x : circle} : Ξ (a : circle.code x), circle.encode (circle.decode a) = a := begin unfold circle.decode, induction x, { intro a, esimp [base,base1], --simplify after #587 apply rec_nat_on a, { exact idp}, { intros n p, apply transport (Ξ»(y : base = base), transport circle.code y _ = _), apply power_con, rewrite [β–Έ*,con_tr, transport_code_loop, ↑[circle.encode,circle.code] at p], krewrite p}, { intros n p, apply transport (Ξ»(y : base = base), transport circle.code y _ = _), { exact !power_con_inv ⬝ ap (power loop) !neg_succ⁻¹}, rewrite [β–Έ*,@con_tr _ circle.code,transport_code_loop_inv, ↑[circle.encode] at p, p, -neg_succ]}}, { apply pathover_of_tr_eq, apply eq_of_homotopy, intro a, apply @is_hset.elim, esimp [circle.code,base,base1], exact _} --simplify after #587 end definition circle_eq_equiv [constructor] (x : circle) : (base = x) ≃ circle.code x := begin fapply equiv.MK, { exact circle.encode}, { exact circle.decode}, { exact circle.encode_decode}, { intro p, cases p, exact idp}, end definition base_eq_base_equiv [constructor] : base = base ≃ β„€ := circle_eq_equiv base definition decode_add (a b : β„€) : base_eq_base_equiv⁻¹ a ⬝ base_eq_base_equiv⁻¹ b = base_eq_base_equiv⁻¹ (a + b) := !power_con_power definition encode_con (p q : base = base) : circle.encode (p ⬝ q) = circle.encode p + circle.encode q := preserve_binary_of_inv_preserve base_eq_base_equiv concat add decode_add p q --the carrier of π₁(SΒΉ) is the set-truncation of base = base. open core algebra trunc equiv.ops definition fg_carrier_equiv_int : π₁(SΒΉ) ≃ β„€ := trunc_equiv_trunc 0 base_eq_base_equiv ⬝e !equiv_trunc⁻¹ᡉ definition con_comm_base (p q : base = base) : p ⬝ q = q ⬝ p := eq_of_fn_eq_fn base_eq_base_equiv (by esimp;rewrite [+encode_con,add.comm]) definition fundamental_group_of_circle : π₁(SΒΉ) = group_integers := begin apply (Group_eq fg_carrier_equiv_int), intros g h, induction g with g', induction h with h', apply encode_con, end definition eq_equiv_Z (x : SΒΉ) : x = x ≃ β„€ := begin induction x, { apply base_eq_base_equiv}, { apply equiv_pathover, intro p p' q, apply pathover_of_eq, let H := eq_of_square (square_of_pathover q), rewrite con_comm_base at H, let H' := cancel_left H, induction H', reflexivity} end end circle
515c11d77b02df91ae907bb4e3bfe1aed2557853
6e9cd8d58e550c481a3b45806bd34a3514c6b3e0
/src/group_theory/submonoid.lean
9e74647ead01c0b3071e347698811a0d5b58c228
[ "Apache-2.0" ]
permissive
sflicht/mathlib
220fd16e463928110e7b0a50bbed7b731979407f
1b2048d7195314a7e34e06770948ee00f0ac3545
refs/heads/master
1,665,934,056,043
1,591,373,803,000
1,591,373,803,000
269,815,267
0
0
Apache-2.0
1,591,402,068,000
1,591,402,067,000
null
UTF-8
Lean
false
false
31,941
lean
/- Copyright (c) 2018 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import algebra.big_operators import algebra.free_monoid import algebra.group.prod import data.equiv.mul_add /-! # Submonoids This file defines bundled multiplicative and additive submonoids. We prove submonoids of a monoid form a complete lattice, and results about images and preimages of submonoids under monoid homomorphisms. There are also theorems about the submonoids generated by an element or a subset of a monoid, defined as the infimum of the set of submonoids containing a given element/subset. ## Implementation notes Submonoid inclusion is denoted `≀` rather than `βŠ†`, although `∈` is defined as membership of a submonoid's underlying set. ## Tags submonoid, submonoids -/ variables {M : Type*} [monoid M] {s : set M} variables {A : Type*} [add_monoid A] {t : set A} /-- A submonoid of a monoid `M` is a subset containing 1 and closed under multiplication. -/ structure submonoid (M : Type*) [monoid M] := (carrier : set M) (one_mem' : (1 : M) ∈ carrier) (mul_mem' {a b} : a ∈ carrier β†’ b ∈ carrier β†’ a * b ∈ carrier) /-- An additive submonoid of an additive monoid `M` is a subset containing 0 and closed under addition. -/ structure add_submonoid (M : Type*) [add_monoid M] := (carrier : set M) (zero_mem' : (0 : M) ∈ carrier) (add_mem' {a b} : a ∈ carrier β†’ b ∈ carrier β†’ a + b ∈ carrier) attribute [to_additive add_submonoid] submonoid /-- Map from submonoids of monoid `M` to `add_submonoid`s of `additive M`. -/ def submonoid.to_add_submonoid {M : Type*} [monoid M] (S : submonoid M) : add_submonoid (additive M) := { carrier := S.carrier, zero_mem' := S.one_mem', add_mem' := S.mul_mem' } /-- Map from `add_submonoid`s of `additive M` to submonoids of `M`. -/ def submonoid.of_add_submonoid {M : Type*} [monoid M] (S : add_submonoid (additive M)) : submonoid M := { carrier := S.carrier, one_mem' := S.zero_mem', mul_mem' := S.add_mem' } /-- Map from `add_submonoid`s of `add_monoid M` to submonoids of `multiplicative M`. -/ def add_submonoid.to_submonoid {M : Type*} [add_monoid M] (S : add_submonoid M) : submonoid (multiplicative M) := { carrier := S.carrier, one_mem' := S.zero_mem', mul_mem' := S.add_mem' } /-- Map from submonoids of `multiplicative M` to `add_submonoid`s of `add_monoid M`. -/ def add_submonoid.of_submonoid {M : Type*} [add_monoid M] (S : submonoid (multiplicative M)) : add_submonoid M := { carrier := S.carrier, zero_mem' := S.one_mem', add_mem' := S.mul_mem' } /-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/ def submonoid.add_submonoid_equiv (M : Type*) [monoid M] : submonoid M ≃ add_submonoid (additive M) := { to_fun := submonoid.to_add_submonoid, inv_fun := submonoid.of_add_submonoid, left_inv := Ξ» x, by cases x; refl, right_inv := Ξ» x, by cases x; refl } namespace submonoid @[to_additive] instance : has_coe (submonoid M) (set M) := ⟨submonoid.carrier⟩ @[to_additive] instance : has_coe_to_sort (submonoid M) := ⟨Type*, Ξ» S, S.carrier⟩ @[to_additive] instance : has_mem M (submonoid M) := ⟨λ m S, m ∈ (S:set M)⟩ @[simp, to_additive] lemma mem_carrier {s : submonoid M} {x : M} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[simp, norm_cast, to_additive] lemma mem_coe {S : submonoid M} {m : M} : m ∈ (S : set M) ↔ m ∈ S := iff.rfl @[simp, norm_cast, to_additive] lemma coe_coe (s : submonoid M) : β†₯(s : set M) = s := rfl attribute [norm_cast] add_submonoid.mem_coe add_submonoid.coe_coe end submonoid @[to_additive] protected lemma submonoid.exists {s : submonoid M} {p : s β†’ Prop} : (βˆƒ x : s, p x) ↔ βˆƒ x ∈ s, p ⟨x, β€Ήx ∈ sβ€ΊβŸ© := set_coe.exists @[to_additive] protected lemma submonoid.forall {s : submonoid M} {p : s β†’ Prop} : (βˆ€ x : s, p x) ↔ βˆ€ x ∈ s, p ⟨x, β€Ήx ∈ sβ€ΊβŸ© := set_coe.forall namespace submonoid variables (S : submonoid M) /-- Two submonoids are equal if the underlying subsets are equal. -/ @[to_additive "Two `add_submonoid`s are equal if the underlying subsets are equal."] theorem ext' ⦃S T : submonoid M⦄ (h : (S : set M) = T) : S = T := by cases S; cases T; congr' /-- Two submonoids are equal if and only if the underlying subsets are equal. -/ @[to_additive "Two `add_submonoid`s are equal if and only if the underlying subsets are equal."] protected theorem ext'_iff {S T : submonoid M} : S = T ↔ (S : set M) = T := ⟨λ h, h β–Έ rfl, Ξ» h, ext' h⟩ /-- Two submonoids are equal if they have the same elements. -/ @[ext, to_additive "Two `add_submonoid`s are equal if they have the same elements."] theorem ext {S T : submonoid M} (h : βˆ€ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h attribute [ext] add_submonoid.ext /-- Copy a submonoid replacing `carrier` with a set that is equal to it. -/ @[to_additive "Copy an additive submonoid replacing `carrier` with a set that is equal to it."] def copy (S : submonoid M) (s : set M) (hs : s = S) : submonoid M := { carrier := s, one_mem' := hs.symm β–Έ S.one_mem', mul_mem' := hs.symm β–Έ S.mul_mem' } @[simp, to_additive] lemma coe_copy {S : submonoid M} {s : set M} (hs : s = S) : (S.copy s hs : set M) = s := rfl @[to_additive] lemma copy_eq {S : submonoid M} {s : set M} (hs : s = S) : S.copy s hs = S := ext' hs /-- A submonoid contains the monoid's 1. -/ @[to_additive "An `add_submonoid` contains the monoid's 0."] theorem one_mem : (1 : M) ∈ S := S.one_mem' /-- A submonoid is closed under multiplication. -/ @[to_additive "An `add_submonoid` is closed under addition."] theorem mul_mem {x y : M} : x ∈ S β†’ y ∈ S β†’ x * y ∈ S := submonoid.mul_mem' S /-- Product of a list of elements in a submonoid is in the submonoid. -/ @[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."] lemma list_prod_mem : βˆ€ {l : list M}, (βˆ€x ∈ l, x ∈ S) β†’ l.prod ∈ S | [] h := S.one_mem | (a::l) h := suffices a * l.prod ∈ S, by rwa [list.prod_cons], have a ∈ S ∧ (βˆ€ x ∈ l, x ∈ S), from list.forall_mem_cons.1 h, S.mul_mem this.1 (list_prod_mem this.2) /-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/ @[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is in the `add_submonoid`."] lemma multiset_prod_mem {M} [comm_monoid M] (S : submonoid M) (m : multiset M) : (βˆ€a ∈ m, a ∈ S) β†’ m.prod ∈ S := begin refine quotient.induction_on m (assume l hl, _), rw [multiset.quot_mk_to_coe, multiset.coe_prod], exact S.list_prod_mem hl end /-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the submonoid. -/ @[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is in the `add_submonoid`."] lemma prod_mem {M : Type*} [comm_monoid M] (S : submonoid M) {ΞΉ : Type*} {t : finset ΞΉ} {f : ΞΉ β†’ M} (h : βˆ€c ∈ t, f c ∈ S) : t.prod f ∈ S := S.multiset_prod_mem (t.1.map f) $ Ξ» x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix β–Έ h i hi lemma pow_mem {x : M} (hx : x ∈ S) : βˆ€ n:β„•, x^n ∈ S | 0 := S.one_mem | (n+1) := S.mul_mem hx (pow_mem n) /-- A submonoid of a monoid inherits a multiplication. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."] instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩ /-- A submonoid of a monoid inherits a 1. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."] instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩ @[simp, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl @[simp, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl @[simp, to_additive] lemma coe_eq_coe (x y : S) : (x : M) = y ↔ x = y := set_coe.ext_iff attribute [norm_cast] coe_eq_coe coe_mul coe_one attribute [norm_cast] add_submonoid.coe_eq_coe add_submonoid.coe_add add_submonoid.coe_zero /-- A submonoid of a monoid inherits a monoid structure. -/ @[to_additive to_add_monoid "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure."] instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S := by refine { mul := (*), one := 1, .. }; simp [mul_assoc, ← submonoid.coe_eq_coe] /-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/ @[to_additive to_add_comm_monoid "An `add_submonoid` of an `add_comm_monoid` is an `add_comm_monoid`."] instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S := { mul_comm := Ξ» _ _, subtype.eq $ mul_comm _ _, ..S.to_monoid} /-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/ @[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."] def subtype : S β†’* M := ⟨coe, rfl, Ξ» _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑S.subtype = coe := rfl @[to_additive] instance : has_le (submonoid M) := ⟨λ S T, βˆ€ ⦃x⦄, x ∈ S β†’ x ∈ T⟩ @[to_additive] lemma le_def {S T : submonoid M} : S ≀ T ↔ βˆ€ ⦃x : M⦄, x ∈ S β†’ x ∈ T := iff.rfl @[simp, norm_cast, to_additive] lemma coe_subset_coe {S T : submonoid M} : (S : set M) βŠ† T ↔ S ≀ T := iff.rfl @[to_additive] instance : partial_order (submonoid M) := { le := Ξ» S T, βˆ€ ⦃x⦄, x ∈ S β†’ x ∈ T, .. partial_order.lift (coe : submonoid M β†’ set M) ext' infer_instance } @[simp, norm_cast, to_additive] lemma coe_ssubset_coe {S T : submonoid M} : (S : set M) βŠ‚ T ↔ S < T := iff.rfl attribute [norm_cast] add_submonoid.coe_subset_coe add_submonoid.coe_ssubset_coe /-- The submonoid `M` of the monoid `M`. -/ @[to_additive "The `add_submonoid M` of the `add_monoid M`."] instance : has_top (submonoid M) := ⟨{ carrier := set.univ, one_mem' := set.mem_univ 1, mul_mem' := Ξ» _ _ _ _, set.mem_univ _ }⟩ /-- The trivial submonoid `{1}` of an monoid `M`. -/ @[to_additive "The trivial `add_submonoid` `{0}` of an `add_monoid` `M`."] instance : has_bot (submonoid M) := ⟨{ carrier := {1}, one_mem' := set.mem_singleton 1, mul_mem' := Ξ» a b ha hb, by { simp only [set.mem_singleton_iff] at *, rw [ha, hb, mul_one] }}⟩ @[to_additive] instance : inhabited (submonoid M) := ⟨βŠ₯⟩ @[simp, to_additive] lemma mem_bot {x : M} : x ∈ (βŠ₯ : submonoid M) ↔ x = 1 := set.mem_singleton_iff @[simp, to_additive] lemma mem_top (x : M) : x ∈ (⊀ : submonoid M) := set.mem_univ x @[simp, to_additive] lemma coe_top : ((⊀ : submonoid M) : set M) = set.univ := rfl @[simp, to_additive] lemma coe_bot : ((βŠ₯ : submonoid M) : set M) = {1} := rfl /-- The inf of two submonoids is their intersection. -/ @[to_additive "The inf of two `add_submonoid`s is their intersection."] instance : has_inf (submonoid M) := ⟨λ S₁ Sβ‚‚, { carrier := S₁ ∩ Sβ‚‚, one_mem' := ⟨S₁.one_mem, Sβ‚‚.one_mem⟩, mul_mem' := Ξ» _ _ ⟨hx, hx'⟩ ⟨hy, hy'⟩, ⟨S₁.mul_mem hx hy, Sβ‚‚.mul_mem hx' hy'⟩ }⟩ @[simp, to_additive] lemma coe_inf (p p' : submonoid M) : ((p βŠ“ p' : submonoid M) : set M) = p ∩ p' := rfl @[simp, to_additive] lemma mem_inf {p p' : submonoid M} {x : M} : x ∈ p βŠ“ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[to_additive] instance : has_Inf (submonoid M) := ⟨λ s, { carrier := β‹‚ t ∈ s, ↑t, one_mem' := set.mem_bInter $ Ξ» i h, i.one_mem, mul_mem' := Ξ» x y hx hy, set.mem_bInter $ Ξ» i h, i.mul_mem (by apply set.mem_bInter_iff.1 hx i h) (by apply set.mem_bInter_iff.1 hy i h) }⟩ @[simp, to_additive] lemma coe_Inf (S : set (submonoid M)) : ((Inf S : submonoid M) : set M) = β‹‚ s ∈ S, ↑s := rfl @[to_additive] lemma mem_Inf {S : set (submonoid M)} {x : M} : x ∈ Inf S ↔ βˆ€ p ∈ S, x ∈ p := set.mem_bInter_iff @[to_additive] lemma mem_infi {ΞΉ : Sort*} {S : ΞΉ β†’ submonoid M} {x : M} : (x ∈ β¨… i, S i) ↔ βˆ€ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, to_additive] lemma coe_infi {ΞΉ : Sort*} {S : ΞΉ β†’ submonoid M} : (↑(β¨… i, S i) : set M) = β‹‚ i, S i := by simp only [infi, coe_Inf, set.bInter_range] attribute [norm_cast] coe_Inf coe_infi /-- Submonoids of a monoid form a complete lattice. -/ @[to_additive "The `add_submonoid`s of an `add_monoid` form a complete lattice."] instance : complete_lattice (submonoid M) := { le := (≀), lt := (<), bot := (βŠ₯), bot_le := Ξ» S x hx, (mem_bot.1 hx).symm β–Έ S.one_mem, top := (⊀), le_top := Ξ» S x hx, mem_top x, inf := (βŠ“), Inf := has_Inf.Inf, le_inf := Ξ» a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := Ξ» a b x, and.left, inf_le_right := Ξ» a b x, and.right, .. complete_lattice_of_Inf (submonoid M) $ Ξ» s, is_glb.of_image (Ξ» S T, show (S : set M) ≀ T ↔ S ≀ T, from coe_subset_coe) is_glb_binfi } /-- The `submonoid` generated by a set. -/ @[to_additive "The `add_submonoid` generated by a set"] def closure (s : set M) : submonoid M := Inf {S | s βŠ† S} @[to_additive] lemma mem_closure {x : M} : x ∈ closure s ↔ βˆ€ S : submonoid M, s βŠ† S β†’ x ∈ S := mem_Inf /-- The submonoid generated by a set includes the set. -/ @[simp, to_additive "The `add_submonoid` generated by a set includes the set."] lemma subset_closure : s βŠ† closure s := Ξ» x hx, mem_closure.2 $ Ξ» S hS, hS hx variable {S} open set /-- A submonoid `S` includes `closure s` if and only if it includes `s`. -/ @[simp, to_additive "An additive submonoid `S` includes `closure s` if and only if it includes `s`"] lemma closure_le : closure s ≀ S ↔ s βŠ† S := ⟨subset.trans subset_closure, Ξ» h, Inf_le h⟩ /-- Submonoid closure of a set is monotone in its argument: if `s βŠ† t`, then `closure s ≀ closure t`. -/ @[to_additive "Additive submonoid closure of a set is monotone in its argument: if `s βŠ† t`, then `closure s ≀ closure t`"] lemma closure_mono ⦃s t : set M⦄ (h : s βŠ† t) : closure s ≀ closure t := closure_le.2 $ subset.trans h subset_closure @[to_additive] lemma closure_eq_of_le (h₁ : s βŠ† S) (hβ‚‚ : S ≀ closure s) : closure s = S := le_antisymm (closure_le.2 h₁) hβ‚‚ variable (S) /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p` holds for all elements of the closure of `s`. -/ @[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `s`, and is preserved under addition, then `p` holds for all elements of the additive closure of `s`."] lemma closure_induction {p : M β†’ Prop} {x} (h : x ∈ closure s) (Hs : βˆ€ x ∈ s, p x) (H1 : p 1) (Hmul : βˆ€ x y, p x β†’ p y β†’ p (x * y)) : p x := (@closure_le _ _ _ ⟨p, H1, Hmul⟩).2 Hs h attribute [elab_as_eliminator] submonoid.closure_induction add_submonoid.closure_induction variable (M) /-- `closure` forms a Galois insertion with the coercion to set. -/ @[to_additive "`closure` forms a Galois insertion with the coercion to set."] protected def gi : galois_insertion (@closure M _) coe := { choice := Ξ» s _, closure s, gc := Ξ» s t, closure_le, le_l_u := Ξ» s, subset_closure, choice_eq := Ξ» s h, rfl } variable {M} /-- Closure of a submonoid `S` equals `S`. -/ @[simp, to_additive "Additive closure of an additive submonoid `S` equals `S`"] lemma closure_eq : closure (S : set M) = S := (submonoid.gi M).l_u_eq S @[simp, to_additive] lemma closure_empty : closure (βˆ… : set M) = βŠ₯ := (submonoid.gi M).gc.l_bot @[simp, to_additive] lemma closure_univ : closure (univ : set M) = ⊀ := @coe_top M _ β–Έ closure_eq ⊀ @[to_additive] lemma closure_union (s t : set M) : closure (s βˆͺ t) = closure s βŠ” closure t := (submonoid.gi M).gc.l_sup @[to_additive] lemma closure_Union {ΞΉ} (s : ΞΉ β†’ set M) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (submonoid.gi M).gc.l_supr @[to_additive] lemma mem_supr_of_directed {ΞΉ} [hΞΉ : nonempty ΞΉ] {S : ΞΉ β†’ submonoid M} (hS : directed (≀) S) {x : M} : x ∈ (⨆ i, S i) ↔ βˆƒ i, x ∈ S i := begin refine ⟨_, Ξ» ⟨i, hi⟩, (le_def.1 $ le_supr S i) hi⟩, suffices : x ∈ closure (⋃ i, (S i : set M)) β†’ βˆƒ i, x ∈ S i, by simpa only [closure_Union, closure_eq (S _)] using this, refine (Ξ» hx, closure_induction hx (Ξ» _, mem_Union.1) _ _), { exact hΞΉ.elim (Ξ» i, ⟨i, (S i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hS i j with ⟨k, hki, hkj⟩, exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ } end @[to_additive] lemma coe_supr_of_directed {ΞΉ} [nonempty ΞΉ] {S : ΞΉ β†’ submonoid M} (hS : directed (≀) S) : ((⨆ i, S i : submonoid M) : set M) = ⋃ i, ↑(S i) := set.ext $ Ξ» x, by simp [mem_supr_of_directed hS] @[to_additive] lemma mem_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≀) S) {x : M} : x ∈ Sup S ↔ βˆƒ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, rw [Sup_eq_supr, supr_subtype', mem_supr_of_directed, subtype.exists], exact (directed_on_iff_directed _).1 hS end @[to_additive] lemma coe_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≀) S) : (↑(Sup S) : set M) = ⋃ s ∈ S, ↑s := set.ext $ Ξ» x, by simp [mem_Sup_of_directed_on Sne hS] variables {N : Type*} [monoid N] {P : Type*} [monoid P] /-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def comap (f : M β†’* N) (S : submonoid N) : submonoid M := { carrier := (f ⁻¹' S), one_mem' := show f 1 ∈ S, by rw f.map_one; exact S.one_mem, mul_mem' := Ξ» a b ha hb, show f (a * b) ∈ S, by rw f.map_mul; exact S.mul_mem ha hb } @[simp, to_additive] lemma coe_comap (S : submonoid N) (f : M β†’* N) : (S.comap f : set M) = f ⁻¹' S := rfl @[simp, to_additive] lemma mem_comap {S : submonoid N} {f : M β†’* N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl @[to_additive] lemma comap_comap (S : submonoid P) (g : N β†’* P) (f : M β†’* N) : (S.comap g).comap f = S.comap (g.comp f) := rfl /-- The image of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def map (f : M β†’* N) (S : submonoid M) : submonoid N := { carrier := (f '' S), one_mem' := ⟨1, S.one_mem, f.map_one⟩, mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy, by rw f.map_mul; refl⟩ end } @[simp, to_additive] lemma coe_map (f : M β†’* N) (S : submonoid M) : (S.map f : set N) = f '' S := rfl @[simp, to_additive] lemma mem_map {f : M β†’* N} {S : submonoid M} {y : N} : y ∈ S.map f ↔ βˆƒ x ∈ S, f x = y := mem_image_iff_bex @[to_additive] lemma map_map (g : N β†’* P) (f : M β†’* N) : (S.map f).map g = S.map (g.comp f) := ext' $ image_image _ _ _ @[to_additive] lemma map_le_iff_le_comap {f : M β†’* N} {S : submonoid M} {T : submonoid N} : S.map f ≀ T ↔ S ≀ T.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : M β†’* N) : galois_connection (map f) (comap f) := Ξ» S T, map_le_iff_le_comap @[to_additive] lemma map_sup (S T : submonoid M) (f : M β†’* N) : (S βŠ” T).map f = S.map f βŠ” T.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ΞΉ : Sort*} (f : M β†’* N) (s : ΞΉ β†’ submonoid M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_inf (S T : submonoid N) (f : M β†’* N) : (S βŠ“ T).comap f = S.comap f βŠ“ T.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ΞΉ : Sort*} (f : M β†’* N) (s : ΞΉ β†’ submonoid N) : (infi s).comap f = β¨… i, (s i).comap f := (gc_map_comap f).u_infi @[simp, to_additive] lemma map_bot (f : M β†’* N) : (βŠ₯ : submonoid M).map f = βŠ₯ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : M β†’* N) : (⊀ : submonoid N).comap f = ⊀ := (gc_map_comap f).u_top /-- Given `submonoid`s `s`, `t` of monoids `M`, `N` respectively, `s Γ— t` as a submonoid of `M Γ— N`. -/ @[to_additive prod "Given `add_submonoid`s `s`, `t` of `add_monoid`s `A`, `B` respectively, `s Γ— t` as an `add_submonoid` of `A Γ— B`."] def prod (s : submonoid M) (t : submonoid N) : submonoid (M Γ— N) := { carrier := (s : set M).prod t, one_mem' := ⟨s.one_mem, t.one_mem⟩, mul_mem' := Ξ» p q hp hq, ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ } @[to_additive coe_prod] lemma coe_prod (s : submonoid M) (t : submonoid N) : (s.prod t : set (M Γ— N)) = (s : set M).prod (t : set N) := rfl @[to_additive mem_prod] lemma mem_prod {s : submonoid M} {t : submonoid N} {p : M Γ— N} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[to_additive prod_mono] lemma prod_mono : ((≀) β‡’ (≀) β‡’ (≀)) (@prod M _ N _) (@prod M _ N _) := Ξ» s s' hs t t' ht, set.prod_mono hs ht @[to_additive prod_mono_right] lemma prod_mono_right (s : submonoid M) : monotone (Ξ» t : submonoid N, s.prod t) := prod_mono (le_refl s) @[to_additive prod_mono_left] lemma prod_mono_left (t : submonoid N) : monotone (Ξ» s : submonoid M, s.prod t) := Ξ» s₁ sβ‚‚ hs, prod_mono hs (le_refl t) @[to_additive prod_top] lemma prod_top (s : submonoid M) : s.prod (⊀ : submonoid N) = s.comap (monoid_hom.fst M N) := ext $ Ξ» x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (s : submonoid N) : (⊀ : submonoid M).prod s = s.comap (monoid_hom.snd M N) := ext $ Ξ» x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊀ : submonoid M).prod (⊀ : submonoid N) = ⊀ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (βŠ₯ : submonoid M).prod (βŠ₯ : submonoid N) = βŠ₯ := ext' $ by simp [coe_prod, prod.one_eq_mk] /-- Product of submonoids is isomorphic to their product as monoids. -/ @[to_additive prod_equiv "Product of additive submonoids is isomorphic to their product as additive monoids"] def prod_equiv (s : submonoid M) (t : submonoid N) : s.prod t ≃* s Γ— t := { map_mul' := Ξ» x y, rfl, .. equiv.set.prod ↑s ↑t } end submonoid namespace monoid_hom variables {N : Type*} {P : Type*} [monoid N] [monoid P] (S : submonoid M) open submonoid /-- The range of a monoid homomorphism is a submonoid. -/ @[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."] def mrange (f : M β†’* N) : submonoid N := (⊀ : submonoid M).map f @[simp, to_additive] lemma coe_mrange (f : M β†’* N) : (f.mrange : set N) = set.range f := set.image_univ @[simp, to_additive] lemma mem_mrange {f : M β†’* N} {y : N} : y ∈ f.mrange ↔ βˆƒ x, f x = y := by simp [mrange] @[to_additive] lemma map_mrange (g : N β†’* P) (f : M β†’* N) : f.mrange.map g = (g.comp f).mrange := (⊀ : submonoid M).map_map g f /-- Restriction of a monoid hom to a submonoid of the domain. -/ @[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."] def mrestrict {N : Type*} [monoid N] (f : M β†’* N) (S : submonoid M) : S β†’* N := f.comp S.subtype @[simp, to_additive] lemma mrestrict_apply {N : Type*} [monoid N] (f : M β†’* N) (x : S) : f.mrestrict S x = f x := rfl /-- Restriction of a monoid hom to a submonoid of the codomain. -/ @[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain."] def cod_mrestrict (f : M β†’* N) (S : submonoid N) (h : βˆ€ x, f x ∈ S) : M β†’* S := { to_fun := Ξ» n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := Ξ» x y, subtype.eq (f.map_mul x y) } /-- Restriction of a monoid hom to its range iterpreted as a submonoid. -/ @[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."] def mrange_restrict {N} [monoid N] (f : M β†’* N) : M β†’* f.mrange := f.cod_mrestrict f.mrange $ Ξ» x, ⟨x, submonoid.mem_top x, rfl⟩ @[simp, to_additive] lemma coe_mrange_restrict {N} [monoid N] (f : M β†’* N) (x : M) : (f.mrange_restrict x : N) = f x := rfl @[to_additive] lemma mrange_top_iff_surjective {N} [monoid N] {f : M β†’* N} : f.mrange = (⊀ : submonoid N) ↔ function.surjective f := submonoid.ext'_iff.trans $ iff.trans (by rw [coe_mrange, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid hom is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."] lemma mrange_top_of_surjective {N} [monoid N] (f : M β†’* N) (hf : function.surjective f) : f.mrange = (⊀ : submonoid N) := mrange_top_iff_surjective.2 hf /-- The submonoid of elements `x : M` such that `f x = g x` -/ @[to_additive "The additive submonoid of elements `x : M` such that `f x = g x`"] def eq_mlocus (f g : M β†’* N) : submonoid M := { carrier := {x | f x = g x}, one_mem' := by rw [set.mem_set_of_eq, f.map_one, g.map_one], mul_mem' := Ξ» x y (hx : _ = _) (hy : _ = _), by simp [*] } /-- If two monoid homomorphisms are equal on a set, then they are equal on its submonoid closure. -/ @[to_additive] lemma eq_on_mclosure {f g : M β†’* N} {s : set M} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≀ f.eq_mlocus g, from closure_le.2 h @[to_additive] lemma eq_of_eq_on_mtop {f g : M β†’* N} (h : set.eq_on f g (⊀ : submonoid M)) : f = g := ext $ Ξ» x, h trivial @[to_additive] lemma eq_of_eq_on_mdense {s : set M} (hs : closure s = ⊀) {f g : M β†’* N} (h : s.eq_on f g) : f = g := eq_of_eq_on_mtop $ hs β–Έ eq_on_mclosure h @[to_additive] lemma mclosure_preimage_le (f : M β†’* N) (s : set N) : closure (f ⁻¹' s) ≀ (closure s).comap f := closure_le.2 $ Ξ» x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set."] lemma map_mclosure (f : M β†’* N) (s : set M) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (mclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end monoid_hom namespace free_monoid variables {Ξ± : Type*} open submonoid @[to_additive] theorem closure_range_of : closure (set.range $ @of Ξ±) = ⊀ := eq_top_iff.2 $ Ξ» x hx, free_monoid.rec_on x (one_mem _) $ Ξ» x xs hxs, mul_mem _ (subset_closure $ set.mem_range_self _) hxs end free_monoid namespace submonoid variables {N : Type*} [monoid N] open monoid_hom /-- The monoid hom associated to an inclusion of submonoids. -/ @[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."] def inclusion {S T : submonoid M} (h : S ≀ T) : S β†’* T := S.subtype.cod_mrestrict _ (Ξ» x, h x.2) @[simp, to_additive] lemma range_subtype (s : submonoid M) : s.subtype.mrange = s := ext' $ (coe_mrange _).trans $ set.range_coe_subtype s lemma closure_singleton_eq (x : M) : closure ({x} : set M) = (powers_hom M x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨multiplicative.of_add 1, trivial, pow_one x⟩) $ Ξ» x ⟨n, _, hn⟩, hn β–Έ pow_mem _ (subset_closure $ set.mem_singleton _) _ /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ lemma mem_closure_singleton {x y : M} : y ∈ closure ({x} : set M) ↔ βˆƒ n:β„•, x^n=y := by rw [closure_singleton_eq, mem_mrange]; refl @[to_additive] lemma closure_eq_mrange (s : set M) : closure s = (free_monoid.lift (coe : s β†’ M)).mrange := by rw [mrange, ← free_monoid.closure_range_of, map_mclosure, ← set.range_comp, free_monoid.lift_comp_of, set.range_coe_subtype] @[to_additive] lemma exists_list_of_mem_closure {s : set M} {x : M} (hx : x ∈ closure s) : βˆƒ (l : list M) (hl : βˆ€ y ∈ l, y ∈ s), l.prod = x := begin rw [closure_eq_mrange, mem_mrange] at hx, rcases hx with ⟨l, hx⟩, exact ⟨list.map coe l, Ξ» y hy, let ⟨z, hz, hy⟩ := list.mem_map.1 hy in hy β–Έ z.2, hx⟩ end @[to_additive] lemma map_inl (s : submonoid M) : s.map (inl M N) = s.prod βŠ₯ := ext $ Ξ» p, ⟨λ ⟨x, hx, hp⟩, hp β–Έ ⟨hx, set.mem_singleton 1⟩, Ξ» ⟨hps, hp1⟩, ⟨p.1, hps, prod.ext rfl $ (set.eq_of_mem_singleton hp1).symm⟩⟩ @[to_additive] lemma map_inr (s : submonoid N) : s.map (inr M N) = prod βŠ₯ s := ext $ Ξ» p, ⟨λ ⟨x, hx, hp⟩, hp β–Έ ⟨set.mem_singleton 1, hx⟩, Ξ» ⟨hp1, hps⟩, ⟨p.2, hps, prod.ext (set.eq_of_mem_singleton hp1).symm rfl⟩⟩ @[to_additive] lemma range_inl : (inl M N).mrange = prod ⊀ βŠ₯ := map_inl ⊀ @[to_additive] lemma range_inr : (inr M N).mrange = prod βŠ₯ ⊀ := map_inr ⊀ @[to_additive] lemma range_inl' : (inl M N).mrange = comap (snd M N) βŠ₯ := range_inl.trans (top_prod _) @[to_additive] lemma range_inr' : (inr M N).mrange = comap (fst M N) βŠ₯ := range_inr.trans (prod_top _) @[simp, to_additive] lemma range_fst : (fst M N).mrange = ⊀ := (fst M N).mrange_top_of_surjective $ @prod.fst_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma range_snd : (snd M N).mrange = ⊀ := (snd M N).mrange_top_of_surjective $ @prod.snd_surjective _ _ ⟨1⟩ @[simp, to_additive prod_bot_sup_bot_prod] lemma prod_bot_sup_bot_prod (s : submonoid M) (t : submonoid N) : (s.prod βŠ₯) βŠ” (prod βŠ₯ t) = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) $ assume p hp, prod.fst_mul_snd p β–Έ mul_mem _ ((le_sup_left : s.prod βŠ₯ ≀ s.prod βŠ₯ βŠ” prod βŠ₯ t) ⟨hp.1, set.mem_singleton 1⟩) ((le_sup_right : prod βŠ₯ t ≀ s.prod βŠ₯ βŠ” prod βŠ₯ t) ⟨set.mem_singleton 1, hp.2⟩) @[simp, to_additive] lemma range_inl_sup_range_inr : (inl M N).mrange βŠ” (inr M N).mrange = ⊀ := by simp only [range_inl, range_inr, prod_bot_sup_bot_prod, top_prod_top] end submonoid namespace submonoid variables {N : Type*} [comm_monoid N] open monoid_hom @[to_additive] lemma sup_eq_range (s t : submonoid N) : s βŠ” t = (s.subtype.coprod t.subtype).mrange := by rw [mrange, ← range_inl_sup_range_inr, map_sup, map_mrange, coprod_comp_inl, map_mrange, coprod_comp_inr, range_subtype, range_subtype] @[to_additive] lemma mem_sup {s t : submonoid N} {x : N} : x ∈ s βŠ” t ↔ βˆƒ (y ∈ s) (z ∈ t), y * z = x := by simp only [sup_eq_range, mem_mrange, coprod_apply, prod.exists, submonoid.exists, coe_subtype, subtype.coe_mk] end submonoid namespace add_submonoid open set lemma smul_mem (S : add_submonoid A) {x : A} (hx : x ∈ S) : βˆ€ n : β„•, n β€’β„• x ∈ S | 0 := S.zero_mem | (n+1) := S.add_mem hx (smul_mem n) lemma closure_singleton_eq (x : A) : closure ({x} : set A) = (multiples_hom A x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨1, trivial, one_nsmul x⟩) $ Ξ» x ⟨n, _, hn⟩, hn β–Έ smul_mem _ (subset_closure $ set.mem_singleton _) _ /-- The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number multiples of the element. -/ lemma mem_closure_singleton {x y : A} : y ∈ closure ({x} : set A) ↔ βˆƒ n:β„•, n β€’β„• x = y := by rw [closure_singleton_eq, add_monoid_hom.mem_mrange]; refl end add_submonoid namespace mul_equiv variables {S T : submonoid M} /-- Makes the identity isomorphism from a proof two submonoids of a multiplicative monoid are equal. -/ @[to_additive add_submonoid_congr "Makes the identity additive isomorphism from a proof two submonoids of an additive monoid are equal."] def submonoid_congr (h : S = T) : S ≃* T := { map_mul' := Ξ» _ _, rfl, ..equiv.set_congr $ submonoid.ext'_iff.1 h } end mul_equiv
6dcc77d4c925b1dd5e255622dd42653e35a84342
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/smt_not_exists.lean
b010b910744d04ae06761ebc4fd86646f0789046
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
380
lean
universe variables u def ex (p q : nat β†’ nat β†’ Prop) (h : βˆƒ x, p x x ∧ q x x) : βˆƒ x y, p x y := begin [smt] by_contra, destruct h, smt_tactic.add_lemmas_from_facts, ematch end print ex lemma ex2 (p q : nat β†’ nat β†’ Prop) (h : βˆƒ x, p x x ∧ q x x) : βˆƒ x, p x x := begin [smt] by_contra, destruct h, smt_tactic.add_lemmas_from_facts, ematch end
10492013f53de3959bdb207bc07dfe4c11761c84
4727251e0cd73359b15b664c3170e5d754078599
/archive/imo/imo2006_q3.lean
b2c2b38fc99f6654c0229d6123cfa8a2aa7363c3
[ "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
5,403
lean
/- Copyright (c) 2021 Tian Chen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tian Chen -/ import analysis.special_functions.sqrt /-! # IMO 2006 Q3 Determine the least real number $M$ such that $$ \left| ab(a^2 - b^2) + bc(b^2 - c^2) + ca(c^2 - a^2) \right| ≀ M (a^2 + b^2 + c^2)^2 $$ for all real numbers $a$, $b$, $c$. ## Solution The answer is $M = \frac{9 \sqrt 2}{32}$. This is essentially a translation of the solution in https://web.evanchen.cc/exams/IMO-2006-notes.pdf. It involves making the substitution `x = a - b`, `y = b - c`, `z = c - a`, `s = a + b + c`. -/ open real /-- Replacing `x` and `y` with their average increases the left side. -/ lemma lhs_ineq {x y : ℝ} (hxy : 0 ≀ x * y) : 16 * x ^ 2 * y ^ 2 * (x + y) ^ 2 ≀ ((x + y) ^ 2) ^ 3 := begin conv_rhs { rw pow_succ' }, refine mul_le_mul_of_nonneg_right _ (sq_nonneg _), apply le_of_sub_nonneg, calc ((x + y) ^ 2) ^ 2 - 16 * x ^ 2 * y ^ 2 = (x - y) ^ 2 * ((x + y) ^ 2 + 4 * (x * y)) : by ring ... β‰₯ 0 : mul_nonneg (sq_nonneg _) $ add_nonneg (sq_nonneg _) $ mul_nonneg zero_lt_four.le hxy end lemma four_pow_four_pos : (0 : ℝ) < 4 ^ 4 := pow_pos zero_lt_four _ lemma mid_ineq {s t : ℝ} : s * t ^ 3 ≀ (3 * t + s) ^ 4 / 4 ^ 4 := (le_div_iff four_pow_four_pos).mpr $ le_of_sub_nonneg $ calc (3 * t + s) ^ 4 - s * t ^ 3 * 4 ^ 4 = (s - t) ^ 2 * ((s + 7 * t) ^ 2 + 2 * (4 * t) ^ 2) : by ring ... β‰₯ 0 : mul_nonneg (sq_nonneg _) $ add_nonneg (sq_nonneg _) $ mul_nonneg zero_le_two (sq_nonneg _) /-- Replacing `x` and `y` with their average decreases the right side. -/ lemma rhs_ineq {x y : ℝ} : 3 * (x + y) ^ 2 ≀ 2 * (x ^ 2 + y ^ 2 + (x + y) ^ 2) := le_of_sub_nonneg $ calc _ = (x - y) ^ 2 : by ring ... β‰₯ 0 : sq_nonneg _ lemma zero_lt_32 : (0 : ℝ) < 32 := by norm_num theorem subst_wlog {x y z s : ℝ} (hxy : 0 ≀ x * y) (hxyz : x + y + z = 0) : 32 * |x * y * z * s| ≀ sqrt 2 * (x^2 + y^2 + z^2 + s^2)^2 := have hz : (x + y)^2 = z^2 := neg_eq_of_add_eq_zero_right hxyz β–Έ (neg_sq _).symm, have hs : 0 ≀ 2 * s ^ 2 := mul_nonneg zero_le_two (sq_nonneg s), have this : _ := calc (2 * s^2) * (16 * x^2 * y^2 * (x + y)^2) ≀ (3 * (x + y)^2 + 2 * s^2)^4 / 4^4 : le_trans (mul_le_mul_of_nonneg_left (lhs_ineq hxy) hs) mid_ineq ... ≀ (2 * (x^2 + y^2 + (x + y)^2) + 2 * s^2)^4 / 4^4 : div_le_div_of_le four_pow_four_pos.le $ pow_le_pow_of_le_left (add_nonneg (mul_nonneg zero_lt_three.le (sq_nonneg _)) hs) (add_le_add_right rhs_ineq _) _, le_of_pow_le_pow _ (mul_nonneg (sqrt_nonneg _) (sq_nonneg _)) nat.succ_pos' $ calc (32 * |x * y * z * s|) ^ 2 = 32 * ((2 * s^2) * (16 * x^2 * y^2 * (x + y)^2)) : by rw [mul_pow, sq_abs, hz]; ring ... ≀ 32 * ((2 * (x^2 + y^2 + (x + y)^2) + 2 * s^2)^4 / 4^4) : mul_le_mul_of_nonneg_left this zero_lt_32.le ... = (sqrt 2 * (x^2 + y^2 + z^2 + s^2)^2)^2 : by rw [mul_pow, sq_sqrt zero_le_two, hz, ←pow_mul, ←mul_add, mul_pow, ←mul_comm_div, ←mul_assoc, show 32 / 4 ^ 4 * 2 ^ 4 = (2 : ℝ), by norm_num, show 2 * 2 = 4, by refl] /-- Proof that `M = 9 * sqrt 2 / 32` works with the substitution. -/ theorem subst_proof₁ (x y z s : ℝ) (hxyz : x + y + z = 0) : |x * y * z * s| ≀ sqrt 2 / 32 * (x^2 + y^2 + z^2 + s^2)^2 := begin wlog h' := mul_nonneg_of_three x y z using [x y z, y z x, z x y] tactic.skip, { rw [div_mul_eq_mul_div, le_div_iff' zero_lt_32], exact subst_wlog h' hxyz }, { intro h, rw [add_assoc, add_comm] at h, rw [mul_assoc x, mul_comm x, add_assoc (x^2), add_comm (x^2)], exact this h }, { intro h, rw [add_comm, ← add_assoc] at h, rw [mul_comm _ z, ← mul_assoc, add_comm _ (z^2), ← add_assoc], exact this h } end lemma lhs_identity (a b c : ℝ) : a * b * (a^2 - b^2) + b * c * (b^2 - c^2) + c * a * (c^2 - a^2) = (a - b) * (b - c) * (c - a) * -(a + b + c) := by ring theorem proof₁ {a b c : ℝ} : |a * b * (a^2 - b^2) + b * c * (b^2 - c^2) + c * a * (c^2 - a^2)| ≀ 9 * sqrt 2 / 32 * (a^2 + b^2 + c^2)^2 := calc _ = _ : congr_arg _ $ lhs_identity a b c ... ≀ _ : subst_proof₁ (a - b) (b - c) (c - a) (-(a + b + c)) (by ring) ... = _ : by ring theorem proofβ‚‚ (M : ℝ) (h : βˆ€ a b c : ℝ, |a * b * (a^2 - b^2) + b * c * (b^2 - c^2) + c * a * (c^2 - a^2)| ≀ M * (a^2 + b^2 + c^2)^2) : 9 * sqrt 2 / 32 ≀ M := begin have h₁ : βˆ€ x : ℝ, (2 - 3 * x - 2) * (2 - (2 + 3 * x)) * (2 + 3 * x - (2 - 3 * x)) * -(2 - 3 * x + 2 + (2 + 3 * x)) = -(18 ^ 2 * x ^ 2 * x), { intro, ring }, have hβ‚‚ : βˆ€ x : ℝ, (2 - 3 * x) ^ 2 + 2 ^ 2 + (2 + 3 * x) ^ 2 = 18 * x ^ 2 + 12, { intro, ring }, have := h (2 - 3 * sqrt 2) 2 (2 + 3 * sqrt 2), rw [lhs_identity, h₁, hβ‚‚, sq_sqrt zero_le_two, abs_neg, abs_eq_self.mpr, ← div_le_iff] at this, { convert this using 1, ring }, { apply pow_pos, norm_num }, { exact mul_nonneg (mul_nonneg (sq_nonneg _) zero_le_two) (sqrt_nonneg _) } end theorem imo2006_q3 (M : ℝ) : (βˆ€ a b c : ℝ, |a * b * (a^2 - b^2) + b * c * (b^2 - c^2) + c * a * (c^2 - a^2)| ≀ M * (a^2 + b^2 + c^2)^2) ↔ 9 * sqrt 2 / 32 ≀ M := ⟨proofβ‚‚ M, Ξ» h _ _ _, le_trans proof₁ $ mul_le_mul_of_nonneg_right h $ sq_nonneg _⟩
12a33d945df43962997371ff94fa146bceaf27ab
4727251e0cd73359b15b664c3170e5d754078599
/archive/imo/imo1962_q4.lean
42bf061c48fb144d15156703b475c79793a8b6bc
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
4,155
lean
/- Copyright (c) 2020 Kevin Lacker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Lacker, Heather Macbeth -/ import analysis.special_functions.trigonometric.complex /-! # IMO 1962 Q4 Solve the equation `cos x ^ 2 + cos (2 * x) ^ 2 + cos (3 * x) ^ 2 = 1`. Since Lean does not have a concept of "simplest form", we just express what is in fact the simplest form of the set of solutions, and then prove it equals the set of solutions. -/ open real open_locale real noncomputable theory def problem_equation (x : ℝ) : Prop := cos x ^ 2 + cos (2 * x) ^ 2 + cos (3 * x) ^ 2 = 1 def solution_set : set ℝ := { x : ℝ | βˆƒ k : β„€, x = (2 * ↑k + 1) * Ο€ / 4 ∨ x = (2 * ↑k + 1) * Ο€ / 6 } /- The key to solving this problem simply is that we can rewrite the equation as a product of terms, shown in `alt_formula`, being equal to zero. -/ def alt_formula (x : ℝ) : ℝ := cos x * (cos x ^ 2 - 1/2) * cos (3 * x) lemma cos_sum_equiv {x : ℝ} : (cos x ^ 2 + cos (2 * x) ^ 2 + cos (3 * x) ^ 2 - 1) / 4 = alt_formula x := begin simp only [real.cos_two_mul, cos_three_mul, alt_formula], ring end lemma alt_equiv {x : ℝ} : problem_equation x ↔ alt_formula x = 0 := begin rw [ problem_equation, ← cos_sum_equiv, div_eq_zero_iff, sub_eq_zero], norm_num, end lemma finding_zeros {x : ℝ} : alt_formula x = 0 ↔ cos x ^ 2 = 1/2 ∨ cos (3 * x) = 0 := begin simp only [alt_formula, mul_assoc, mul_eq_zero, sub_eq_zero], split, { rintro (h1|h2), { right, rw [cos_three_mul, h1], ring }, { exact h2 } }, { exact or.inr } end /- Now we can solve for `x` using basic-ish trigonometry. -/ lemma solve_cos2_half {x : ℝ} : cos x ^ 2 = 1/2 ↔ βˆƒ k : β„€, x = (2 * ↑k + 1) * Ο€ / 4 := begin rw cos_sq, simp only [add_right_eq_self, div_eq_zero_iff], norm_num, rw cos_eq_zero_iff, split; { rintro ⟨k, h⟩, use k, linarith }, end lemma solve_cos3x_0 {x : ℝ} : cos (3 * x) = 0 ↔ βˆƒ k : β„€, x = (2 * ↑k + 1) * Ο€ / 6 := begin rw cos_eq_zero_iff, refine exists_congr (Ξ» k, _), split; intro; linarith end /- The final theorem is now just gluing together our lemmas. -/ theorem imo1962_q4 {x : ℝ} : problem_equation x ↔ x ∈ solution_set := begin rw [alt_equiv, finding_zeros, solve_cos3x_0, solve_cos2_half], exact exists_or_distrib.symm end /- We now present a second solution. The key to this solution is that, when the identity is converted to an identity which is polynomial in `a` := `cos x`, it can be rewritten as a product of terms, `a ^ 2 * (2 * a ^ 2 - 1) * (4 * a ^ 2 - 3)`, being equal to zero. -/ /-- Someday, when there is a Grobner basis tactic, try to automate this proof. (A little tricky -- the ideals are not the same but their Jacobson radicals are.) -/ lemma formula {R : Type*} [comm_ring R] [is_domain R] [char_zero R] (a : R) : a ^ 2 + (2 * a ^ 2 - 1) ^ 2 + (4 * a ^ 3 - 3 * a) ^ 2 = 1 ↔ (2 * a ^ 2 - 1) * (4 * a ^ 3 - 3 * a) = 0 := calc a ^ 2 + (2 * a ^ 2 - 1) ^ 2 + (4 * a ^ 3 - 3 * a) ^ 2 = 1 ↔ a ^ 2 + (2 * a ^ 2 - 1) ^ 2 + (4 * a ^ 3 - 3 * a) ^ 2 - 1 = 0 : by rw ← sub_eq_zero ... ↔ 2 * a ^ 2 * (2 * a ^ 2 - 1) * (4 * a ^ 2 - 3) = 0 : by { split; intros h; convert h; ring } ... ↔ a * (2 * a ^ 2 - 1) * (4 * a ^ 2 - 3) = 0 : by simp [(by norm_num : (2:R) β‰  0)] ... ↔ (2 * a ^ 2 - 1) * (4 * a ^ 3 - 3 * a) = 0 : by { split; intros h; convert h using 1; ring } /- Again, we now can solve for `x` using basic-ish trigonometry. -/ lemma solve_cos2x_0 {x : ℝ} : cos (2 * x) = 0 ↔ βˆƒ k : β„€, x = (2 * ↑k + 1) * Ο€ / 4 := begin rw cos_eq_zero_iff, refine exists_congr (Ξ» k, _), split; intro; linarith end /- Again, the final theorem is now just gluing together our lemmas. -/ theorem imo1962_q4' {x : ℝ} : problem_equation x ↔ x ∈ solution_set := calc problem_equation x ↔ cos x ^ 2 + cos (2 * x) ^ 2 + cos (3 * x) ^ 2 = 1 : by refl ... ↔ cos (2 * x) = 0 ∨ cos (3 * x) = 0 : by simp [cos_two_mul, cos_three_mul, formula] ... ↔ x ∈ solution_set : by { rw [solve_cos2x_0, solve_cos3x_0, ← exists_or_distrib], refl }
fe10ae088776c845663183cebc9fc119ac338654
92b50235facfbc08dfe7f334827d47281471333b
/library/logic/cast.lean
764ce7311beb16332b8ee840dda4225fff4aec6a
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
7,733
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura Casts and heterogeneous equality. See also init.datatypes and init.logic. -/ import logic.eq logic.quantifiers open eq.ops section universe variable u variables {A B : Type.{u}} definition cast (H : A = B) (a : A) : B := eq.rec a H theorem cast_refl (a : A) : cast (eq.refl A) a = a := rfl theorem cast_proof_irrel (H₁ Hβ‚‚ : A = B) (a : A) : cast H₁ a = cast Hβ‚‚ a := rfl theorem cast_eq (H : A = A) (a : A) : cast H a = a := rfl end namespace heq universe variable u variables {A B C : Type.{u}} {a a' : A} {b b' : B} {c : C} definition type_eq (H : a == b) : A = B := heq.rec_on H (eq.refl A) theorem drec_on {C : Ξ  {B : Type} (b : B), a == b β†’ Type} (H₁ : a == b) (Hβ‚‚ : C a (refl a)) : C b H₁ := heq.rec (Ξ» H₁ : a == a, show C a H₁, from Hβ‚‚) H₁ H₁ theorem to_cast_eq (H : a == b) : cast (type_eq H) a = b := drec_on H !cast_eq end heq section universe variables u v variables {A A' B C : Type.{u}} {P P' : A β†’ Type.{v}} {a a' : A} {b : B} theorem hcongr_fun {f : Ξ  x, P x} {f' : Ξ  x, P' x} (a : A) (H₁ : f == f') (Hβ‚‚ : P = P') : f a == f' a := have aux : βˆ€ (f : Ξ  x, P x) (f' : Ξ  x, P x), f == f' β†’ f a == f' a, from take f f' H, heq.to_eq H β–Έ heq.refl (f a), (Hβ‚‚ β–Έ aux) f f' H₁ theorem hcongr {P' : A' β†’ Type} {f : Ξ  a, P a} {f' : Ξ  a', P' a'} {a : A} {a' : A'} (Hf : f == f') (HP : P == P') (Ha : a == a') : f a == f' a' := have H1 : βˆ€ (B P' : A β†’ Type) (f : Ξ  x, P x) (f' : Ξ  x, P' x), f == f' β†’ (Ξ»x, P x) == (Ξ»x, P' x) β†’ f a == f' a, from take P P' f f' Hf HB, hcongr_fun a Hf (heq.to_eq HB), have H2 : βˆ€ (B : A β†’ Type) (P' : A' β†’ Type) (f : Ξ  x, P x) (f' : Ξ  x, P' x), f == f' β†’ (Ξ»x, P x) == (Ξ»x, P' x) β†’ f a == f' a', from heq.subst Ha H1, H2 P P' f f' Hf HP theorem hcongr_arg (f : Ξ x, P x) {a b : A} (H : a = b) : f a == f b := H β–Έ (heq.refl (f a)) end section variables {A : Type} {B : A β†’ Type} {C : Ξ a, B a β†’ Type} {D : Ξ a b, C a b β†’ Type} variables {a a' : A} {b : B a} {b' : B a'} {c : C a b} {c' : C a' b'} theorem hcongr_arg2 (f : Ξ a b, C a b) (Ha : a = a') (Hb : b == b') : f a b == f a' b' := hcongr (hcongr_arg f Ha) (hcongr_arg C Ha) Hb theorem hcongr_arg3 (f : Ξ a b c, D a b c) (Ha : a = a') (Hb : b == b') (Hc : c == c') : f a b c == f a' b' c' := hcongr (hcongr_arg2 f Ha Hb) (hcongr_arg2 D Ha Hb) Hc end section universe variables u v variables {A A' B C : Type.{u}} {P P' : A β†’ Type.{v}} {a a' : A} {b : B} -- should H₁ be explicit (useful in e.g. hproof_irrel) theorem eq_rec_to_heq {H₁ : a = a'} {p : P a} {p' : P a'} (Hβ‚‚ : eq.rec_on H₁ p = p') : p == p' := calc p == eq.rec_on H₁ p : heq.symm (eq_rec_heq H₁ p) ... = p' : Hβ‚‚ theorem cast_to_heq {H₁ : A = B} (Hβ‚‚ : cast H₁ a = b) : a == b := eq_rec_to_heq Hβ‚‚ theorem hproof_irrel {a b : Prop} (H : a = b) (H₁ : a) (Hβ‚‚ : b) : H₁ == Hβ‚‚ := eq_rec_to_heq (proof_irrel (cast H H₁) Hβ‚‚) --TODO: generalize to eq.rec. This is a special case of rec_on_compose in eq.lean theorem cast_trans (Hab : A = B) (Hbc : B = C) (a : A) : cast Hbc (cast Hab a) = cast (Hab ⬝ Hbc) a := heq.to_eq (calc cast Hbc (cast Hab a) == cast Hab a : eq_rec_heq Hbc (cast Hab a) ... == a : eq_rec_heq Hab a ... == cast (Hab ⬝ Hbc) a : heq.symm (eq_rec_heq (Hab ⬝ Hbc) a)) theorem pi_eq (H : P = P') : (Ξ  x, P x) = (Ξ  x, P' x) := H β–Έ (eq.refl (Ξ  x, P x)) theorem rec_on_app (H : P = P') (f : Ξ  x, P x) (a : A) : eq.rec_on H f a == f a := have aux : βˆ€ H : P = P, eq.rec_on H f a == f a, from take H : P = P, heq.refl (eq.rec_on H f a), (H β–Έ aux) H theorem rec_on_pull (H : P = P') (f : Ξ  x, P x) (a : A) : eq.rec_on H f a = eq.rec_on (congr_fun H a) (f a) := heq.to_eq (calc eq.rec_on H f a == f a : rec_on_app H f a ... == eq.rec_on (congr_fun H a) (f a) : heq.symm (eq_rec_heq (congr_fun H a) (f a))) theorem cast_app (H : P = P') (f : Ξ  x, P x) (a : A) : cast (pi_eq H) f a == f a := have H₁ : βˆ€ (H : (Ξ  x, P x) = (Ξ  x, P x)), cast H f a == f a, from assume H, heq.of_eq (congr_fun (cast_eq H f) a), have Hβ‚‚ : βˆ€ (H : (Ξ  x, P x) = (Ξ  x, P' x)), cast H f a == f a, from H β–Έ H₁, Hβ‚‚ (pi_eq H) end -- function extensionality wrt heterogeneous equality theorem hfunext {A : Type} {B : A β†’ Type} {B' : A β†’ Type} {f : Ξ  x, B x} {g : Ξ  x, B' x} (H : βˆ€ a, f a == g a) : f == g := let HH : B = B' := (funext (Ξ» x, heq.type_eq (H x))) in cast_to_heq (funext (Ξ» a, heq.to_eq (heq.trans (cast_app HH f a) (H a)))) section variables {A : Type} {B : A β†’ Type} {C : Ξ a, B a β†’ Type} {D : Ξ a b, C a b β†’ Type} {E : Ξ a b c, D a b c β†’ Type} {F : Type} variables {a a' : A} {b : B a} {b' : B a'} {c : C a b} {c' : C a' b'} {d : D a b c} {d' : D a' b' c'} theorem hcongr_arg4 (f : Ξ a b c d, E a b c d) (Ha : a = a') (Hb : b == b') (Hc : c == c') (Hd : d == d') : f a b c d == f a' b' c' d' := hcongr (hcongr_arg3 f Ha Hb Hc) (hcongr_arg3 E Ha Hb Hc) Hd theorem dcongr_arg2 (f : Ξ a, B a β†’ F) (Ha : a = a') (Hb : eq.rec_on Ha b = b') : f a b = f a' b' := heq.to_eq (hcongr_arg2 f Ha (eq_rec_to_heq Hb)) theorem dcongr_arg3 (f : Ξ a b, C a b β†’ F) (Ha : a = a') (Hb : eq.rec_on Ha b = b') (Hc : cast (dcongr_arg2 C Ha Hb) c = c') : f a b c = f a' b' c' := heq.to_eq (hcongr_arg3 f Ha (eq_rec_to_heq Hb) (eq_rec_to_heq Hc)) theorem dcongr_arg4 (f : Ξ a b c, D a b c β†’ F) (Ha : a = a') (Hb : eq.rec_on Ha b = b') (Hc : cast (dcongr_arg2 C Ha Hb) c = c') (Hd : cast (dcongr_arg3 D Ha Hb Hc) d = d') : f a b c d = f a' b' c' d' := heq.to_eq (hcongr_arg4 f Ha (eq_rec_to_heq Hb) (eq_rec_to_heq Hc) (eq_rec_to_heq Hd)) -- mixed versions (we want them for example if C a' b' is a subsingleton, like a proposition. -- Then proving eq is easier than proving heq) theorem hdcongr_arg3 (f : Ξ a b, C a b β†’ F) (Ha : a = a') (Hb : b == b') (Hc : cast (heq.to_eq (hcongr_arg2 C Ha Hb)) c = c') : f a b c = f a' b' c' := heq.to_eq (hcongr_arg3 f Ha Hb (eq_rec_to_heq Hc)) theorem hhdcongr_arg4 (f : Ξ a b c, D a b c β†’ F) (Ha : a = a') (Hb : b == b') (Hc : c == c') (Hd : cast (dcongr_arg3 D Ha (!eq.rec_on_irrel_arg ⬝ heq.to_cast_eq Hb) (!eq.rec_on_irrel_arg ⬝ heq.to_cast_eq Hc)) d = d') : f a b c d = f a' b' c' d' := heq.to_eq (hcongr_arg4 f Ha Hb Hc (eq_rec_to_heq Hd)) theorem hddcongr_arg4 (f : Ξ a b c, D a b c β†’ F) (Ha : a = a') (Hb : b == b') (Hc : cast (heq.to_eq (hcongr_arg2 C Ha Hb)) c = c') (Hd : cast (hdcongr_arg3 D Ha Hb Hc) d = d') : f a b c d = f a' b' c' d' := heq.to_eq (hcongr_arg4 f Ha Hb (eq_rec_to_heq Hc) (eq_rec_to_heq Hd)) --Is a reasonable version of "hcongr2" provable without pi_ext and funext? --It looks like you need some ugly extra conditions -- theorem hcongr2' {A A' : Type} {B : A β†’ Type} {B' : A' β†’ Type} {C : Ξ a, B a β†’ Type} {C' : Ξ a, B' a β†’ Type} -- {f : Ξ  a b, C a b} {f' : Ξ  a' b', C' a' b'} {a : A} {a' : A'} {b : B a} {b' : B' a'} -- (HBB' : B == B') (HCC' : C == C') -- (Hff' : f == f') (Haa' : a == a') (Hbb' : b == b') : f a b == f' a' b' := -- hcongr (hcongr Hff' (sorry) Haa') (hcongr HCC' (sorry) Haa') Hbb' end
abfb877af8d18cd264f08dbbfad9fc68c316aaed
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/omega/int/preterm.lean
0a57c20926ace273d3105a5f969f1bf2b1c9e8d1
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
2,815
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Seul Baek -/ /- Linear integer arithmetic terms in pre-normalized form. -/ import tactic.omega.term namespace omega namespace int /-- The shadow syntax for arithmetic terms. All constants are reified to `cst` (e.g., `-5` is reified to `cst -5`) and all other atomic terms are reified to `exp` (e.g., `-5 * (gcd 14 -7)` is reified to `exp -5 \`(gcd 14 -7)`). `exp` accepts a coefficient of type `int` as its first argument because multiplication by constant is allowed by the omega test. -/ meta inductive exprterm : Type | cst : int β†’ exprterm | exp : int β†’ expr β†’ exprterm | add : exprterm β†’ exprterm β†’ exprterm /-- Similar to `exprterm`, except that all exprs are now replaced with de Brujin indices of type `nat`. This is akin to generalizing over the terms represented by the said exprs. -/ @[derive has_reflect, derive inhabited] inductive preterm : Type | cst : int β†’ preterm | var : int β†’ nat β†’ preterm | add : preterm β†’ preterm β†’ preterm localized "notation `&` k := omega.int.preterm.cst k" in omega.int localized "infix ` ** ` : 300 := omega.int.preterm.var" in omega.int localized "notation t `+*` s := omega.int.preterm.add t s" in omega.int namespace preterm /-- Preterm evaluation -/ @[simp] def val (v : nat β†’ int) : preterm β†’ int | (& i) := i | (i ** n) := if i = 1 then v n else if i = -1 then -(v n) else (v n) * i | (t1 +* t2) := t1.val + t2.val /-- Fresh de Brujin index not used by any variable in argument -/ def fresh_index : preterm β†’ nat | (& _) := 0 | (i ** n) := n + 1 | (t1 +* t2) := max t1.fresh_index t2.fresh_index @[simp] def add_one (t : preterm) : preterm := t +* (&1) def repr : preterm β†’ string | (& i) := i.repr | (i ** n) := i.repr ++ "*x" ++ n.repr | (t1 +* t2) := "(" ++ t1.repr ++ " + " ++ t2.repr ++ ")" end preterm open_locale list.func -- get notation for list.func.set /-- Return a term (which is in canonical form by definition) that is equivalent to the input preterm -/ @[simp] def canonize : preterm β†’ term | (& i) := ⟨i, []⟩ | (i ** n) := ⟨0, [] {n ↦ i}⟩ | (t1 +* t2) := term.add (canonize t1) (canonize t2) @[simp] lemma val_canonize {v : nat β†’ int} : βˆ€ {t : preterm}, (canonize t).val v = t.val v | (& i) := by simp only [preterm.val, add_zero, term.val, canonize, coeffs.val_nil] | (i ** n) := begin simp only [coeffs.val_set, canonize, preterm.val, zero_add, term.val], split_ifs with h1 h2, { simp only [one_mul, h1] }, { simp only [neg_mul_eq_neg_mul_symm, one_mul, h2] }, { rw mul_comm } end | (t +* s) := by simp only [canonize, val_canonize, term.val_add, preterm.val] end int end omega
e58ebef37c9b67b1d59e61a3055a8cecb62b8a46
9efb19082863e0d533e4a6ef27bf8d1854fce261
/sphinx/bai2.lean
17aa558dac1939c585b6c39b81e43ac27fa8e7db
[ "CC-BY-4.0" ]
permissive
mactiendinh/formalabstracts
da3b4761020a98eafdbb08a82494c287e460bce9
b8a51764d9ba32ba77bcd87a2344be36955b6292
refs/heads/master
1,584,615,451,722
1,528,359,866,000
1,528,359,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
584
lean
#check nat #check nat β†’ nat #check Ξ» x : nat , x #check Type def identity : βˆ€ x :β„• , β„• := Ξ» x : β„• , x #check identity def identity' (x :β„•) : β„• := x #check identity' theorem my_first_theorem (x:β„• ): identity x =x:= rfl #print and.left #print and.right #print and.intro theorem and_is_comulative {p q: Prop}: βˆ€ (h: p ∧ q), q ∧ p := Ξ» h: p ∧ q, and.intro (and.right h) (and.left h) set_option pp.all true #print and_is_comulative theorem T: 1 = 0 ∧ 2 < 3 β†’ 2 < 3 ∧ 1 = 0 := Ξ» h, and_is_comulative h #print T
8e2e42c02b0fb4245d9b9fc2ab7cc43f62505e4c
618003631150032a5676f229d13a079ac875ff77
/src/algebra/order_functions.lean
0263cd42d25a4df750781bd6bd0d417e8cee1e80
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
11,515
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.ordered_ring /-! # strictly monotone functions, max, min and abs This file proves basic properties about strictly monotone functions, maxima and minima on a `decidable_linear_order`, and the absolute value function on linearly ordered add_comm_groups, semirings and rings. ## Tags min, max, abs -/ universes u v variables {Ξ± : Type u} {Ξ² : Type v} attribute [simp] max_eq_left max_eq_right min_eq_left min_eq_right section variables [decidable_linear_order Ξ±] [decidable_linear_order Ξ²] {f : Ξ± β†’ Ξ²} {a b c d : Ξ±} -- translate from lattices to linear orders (sup β†’ max, inf β†’ min) @[simp] lemma le_min_iff : c ≀ min a b ↔ c ≀ a ∧ c ≀ b := le_inf_iff @[simp] lemma max_le_iff : max a b ≀ c ↔ a ≀ c ∧ b ≀ c := sup_le_iff lemma max_le_max : a ≀ c β†’ b ≀ d β†’ max a b ≀ max c d := sup_le_sup lemma min_le_min : a ≀ c β†’ b ≀ d β†’ min a b ≀ min c d := inf_le_inf lemma le_max_left_of_le : a ≀ b β†’ a ≀ max b c := le_sup_left_of_le lemma le_max_right_of_le : a ≀ c β†’ a ≀ max b c := le_sup_right_of_le lemma min_le_left_of_le : a ≀ c β†’ min a b ≀ c := inf_le_left_of_le lemma min_le_right_of_le : b ≀ c β†’ min a b ≀ c := inf_le_right_of_le lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right lemma min_le_max : min a b ≀ max a b := le_trans (min_le_left a b) (le_max_left a b) /-- An instance asserting that `max a a = a` -/ instance max_idem : is_idempotent Ξ± max := by apply_instance -- short-circuit type class inference /-- An instance asserting that `min a a = a` -/ instance min_idem : is_idempotent Ξ± min := by apply_instance -- short-circuit type class inference @[simp] lemma min_le_iff : min a b ≀ c ↔ a ≀ c ∨ b ≀ c := have a ≀ b β†’ (a ≀ c ∨ b ≀ c ↔ a ≀ c), from assume h, or_iff_left_of_imp $ le_trans h, have b ≀ a β†’ (a ≀ c ∨ b ≀ c ↔ b ≀ c), from assume h, or_iff_right_of_imp $ le_trans h, by cases le_total a b; simp * @[simp] lemma le_max_iff : a ≀ max b c ↔ a ≀ b ∨ a ≀ c := have b ≀ c β†’ (a ≀ b ∨ a ≀ c ↔ a ≀ c), from assume h, or_iff_right_of_imp $ assume h', le_trans h' h, have c ≀ b β†’ (a ≀ b ∨ a ≀ c ↔ a ≀ b), from assume h, or_iff_left_of_imp $ assume h', le_trans h' h, by cases le_total b c; simp * @[simp] lemma max_lt_iff : max a b < c ↔ (a < c ∧ b < c) := by rw [lt_iff_not_ge]; simp [(β‰₯), le_max_iff, not_or_distrib] @[simp] lemma lt_min_iff : a < min b c ↔ (a < b ∧ a < c) := by rw [lt_iff_not_ge]; simp [(β‰₯), min_le_iff, not_or_distrib] @[simp] lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c := by rw [lt_iff_not_ge]; simp [(β‰₯), max_le_iff, not_and_distrib] @[simp] lemma min_lt_iff : min a b < c ↔ a < c ∨ b < c := by rw [lt_iff_not_ge]; simp [(β‰₯), le_min_iff, not_and_distrib] lemma max_lt_max (h₁ : a < c) (hβ‚‚ : b < d) : max a b < max c d := by apply max_lt; simp [lt_max_iff, h₁, hβ‚‚] lemma min_lt_min (h₁ : a < c) (hβ‚‚ : b < d) : min a b < min c d := by apply lt_min; simp [min_lt_iff, h₁, hβ‚‚] theorem min_right_comm (a b c : Ξ±) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : Ξ±) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : Ξ±) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c lemma monotone.map_max (hf : monotone f) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp [h, hf h] lemma monotone.map_min (hf : monotone f) : f (min a b) = min (f a) (f b) := by cases le_total a b; simp [h, hf h] theorem min_choice (a b : Ξ±) : min a b = a ∨ min a b = b := by by_cases h : a ≀ b; simp [min, h] theorem max_choice (a b : Ξ±) : max a b = a ∨ max a b = b := by by_cases h : a ≀ b; simp [max, h] lemma le_of_max_le_left {a b c : Ξ±} (h : max a b ≀ c) : a ≀ c := le_trans (le_max_left _ _) h lemma le_of_max_le_right {a b c : Ξ±} (h : max a b ≀ c) : b ≀ c := le_trans (le_max_right _ _) h end lemma min_add {Ξ± : Type u} [decidable_linear_ordered_add_comm_group Ξ±] (a b c : Ξ±) : min a b + c = min (a + c) (b + c) := if hle : a ≀ b then have a - c ≀ b - c, from sub_le_sub hle (le_refl _), by simp * at * else have b - c ≀ a - c, from sub_le_sub (le_of_lt (lt_of_not_ge hle)) (le_refl _), by simp * at * lemma min_sub {Ξ± : Type u} [decidable_linear_ordered_add_comm_group Ξ±] (a b c : Ξ±) : min a b - c = min (a - c) (b - c) := by simp [min_add, sub_eq_add_neg] /- Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ lemma fn_min_add_fn_max [decidable_linear_order Ξ±] [add_comm_semigroup Ξ²] (f : Ξ± β†’ Ξ²) (n m : Ξ±) : f (min n m) + f (max n m) = f n + f m := by { cases le_total n m with h h; simp [h, add_comm] } lemma min_add_max [decidable_linear_order Ξ±] [add_comm_semigroup Ξ±] (n m : Ξ±) : min n m + max n m = n + m := fn_min_add_fn_max id n m lemma fn_min_mul_fn_max [decidable_linear_order Ξ±] [comm_semigroup Ξ²] (f : Ξ± β†’ Ξ²) (n m : Ξ±) : f (min n m) * f (max n m) = f n * f m := by { cases le_total n m with h h; simp [h, mul_comm] } lemma min_mul_max [decidable_linear_order Ξ±] [comm_semigroup Ξ±] (n m : Ξ±) : min n m * max n m = n * m := fn_min_mul_fn_max id n m section decidable_linear_ordered_add_comm_group variables [decidable_linear_ordered_add_comm_group Ξ±] {a b c : Ξ±} attribute [simp] abs_zero abs_neg lemma abs_add (a b : Ξ±) : abs (a + b) ≀ abs a + abs b := abs_add_le_abs_add_abs a b theorem abs_le : abs a ≀ b ↔ - b ≀ a ∧ a ≀ b := ⟨assume h, ⟨neg_le_of_neg_le $ le_trans (neg_le_abs_self _) h, le_trans (le_abs_self _) h⟩, assume ⟨h₁, hβ‚‚βŸ©, abs_le_of_le_of_neg_le hβ‚‚ $ neg_le_of_neg_le hβ‚βŸ© lemma abs_lt : abs a < b ↔ - b < a ∧ a < b := ⟨assume h, ⟨neg_lt_of_neg_lt $ lt_of_le_of_lt (neg_le_abs_self _) h, lt_of_le_of_lt (le_abs_self _) h⟩, assume ⟨h₁, hβ‚‚βŸ©, abs_lt_of_lt_of_neg_lt hβ‚‚ $ neg_lt_of_neg_lt hβ‚βŸ© lemma lt_abs : a < abs b ↔ a < b ∨ a < -b := lt_max_iff lemma abs_sub_le_iff : abs (a - b) ≀ c ↔ a - b ≀ c ∧ b - a ≀ c := by rw [abs_le, neg_le_sub_iff_le_add, @sub_le_iff_le_add' _ _ b, and_comm] lemma abs_sub_lt_iff : abs (a - b) < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add, @sub_lt_iff_lt_add' _ _ b, and_comm] lemma sub_abs_le_abs_sub (a b : Ξ±) : abs a - abs b ≀ abs (a - b) := abs_sub_abs_le_abs_sub a b lemma abs_abs_sub_le_abs_sub (a b : Ξ±) : abs (abs a - abs b) ≀ abs (a - b) := abs_sub_le_iff.2 ⟨sub_abs_le_abs_sub _ _, by rw abs_sub; apply sub_abs_le_abs_sub⟩ lemma abs_eq (hb : 0 ≀ b) : abs a = b ↔ a = b ∨ a = -b := iff.intro begin cases le_total a 0 with a_nonpos a_nonneg, { rw [abs_of_nonpos a_nonpos, neg_eq_iff_neg_eq, eq_comm], exact or.inr }, { rw [abs_of_nonneg a_nonneg, eq_comm], exact or.inl } end (by intro h; cases h; subst h; try { rw abs_neg }; exact abs_of_nonneg hb) @[simp] lemma abs_eq_zero : abs a = 0 ↔ a = 0 := ⟨eq_zero_of_abs_eq_zero, Ξ» e, e.symm β–Έ abs_zero⟩ lemma abs_pos_iff {a : Ξ±} : 0 < abs a ↔ a β‰  0 := ⟨λ h, mt abs_eq_zero.2 (ne_of_gt h), abs_pos_of_ne_zero⟩ @[simp] lemma abs_nonpos_iff {a : Ξ±} : abs a ≀ 0 ↔ a = 0 := by rw [← not_lt, abs_pos_iff, not_not] lemma abs_le_max_abs_abs (hab : a ≀ b) (hbc : b ≀ c) : abs b ≀ max (abs a) (abs c) := abs_le_of_le_of_neg_le (by simp [le_max_iff, le_trans hbc (le_abs_self c)]) (by simp [le_max_iff, le_trans (neg_le_neg hab) (neg_le_abs_self a)]) theorem abs_le_abs {Ξ± : Type*} [decidable_linear_ordered_add_comm_group Ξ±] {a b : Ξ±} (hβ‚€ : a ≀ b) (h₁ : -a ≀ b) : abs a ≀ abs b := calc abs a ≀ b : by { apply abs_le_of_le_of_neg_le; assumption } ... ≀ abs b : le_abs_self _ lemma min_le_add_of_nonneg_right {a b : Ξ±} (hb : b β‰₯ 0) : min a b ≀ a + b := calc min a b ≀ a : by apply min_le_left ... ≀ a + b : le_add_of_nonneg_right hb lemma min_le_add_of_nonneg_left {a b : Ξ±} (ha : a β‰₯ 0) : min a b ≀ a + b := calc min a b ≀ b : by apply min_le_right ... ≀ a + b : le_add_of_nonneg_left ha lemma max_le_add_of_nonneg {a b : Ξ±} (ha : a β‰₯ 0) (hb : b β‰₯ 0) : max a b ≀ a + b := max_le_iff.2 (by split; simpa) lemma max_zero_sub_eq_self (a : Ξ±) : max a 0 - max (-a) 0 = a := begin rcases le_total a 0, { rw [max_eq_right h, max_eq_left, zero_sub, neg_neg], { rwa [le_neg, neg_zero] } }, { rw [max_eq_left, max_eq_right, sub_zero], { rwa [neg_le, neg_zero] }, exact h } end lemma abs_max_sub_max_le_abs (a b c : Ξ±) : abs (max a c - max b c) ≀ abs (a - b) := begin simp only [max], split_ifs, { rw [sub_self, abs_zero], exact abs_nonneg _ }, { calc abs (c - b) = - (c - b) : abs_of_neg (sub_neg_of_lt (lt_of_not_ge h_1)) ... = b - c : neg_sub _ _ ... ≀ b - a : by { rw sub_le_sub_iff_left, exact h } ... = - (a - b) : by rw neg_sub ... ≀ abs (a - b) : neg_le_abs_self _ }, { calc abs (a - c) = a - c : abs_of_pos (sub_pos_of_lt (lt_of_not_ge h)) ... ≀ a - b : by { rw sub_le_sub_iff_left, exact h_1 } ... ≀ abs (a - b) : le_abs_self _ }, { refl } end lemma max_sub_min_eq_abs' (a b : Ξ±) : max a b - min a b = abs (a - b) := begin cases le_total a b with ab ba, { rw [max_eq_right ab, min_eq_left ab, abs_of_nonpos, neg_sub], rwa sub_nonpos }, { rw [max_eq_left ba, min_eq_right ba, abs_of_nonneg], exact sub_nonneg_of_le ba } end lemma max_sub_min_eq_abs (a b : Ξ±) : max a b - min a b = abs (b - a) := by { rw [abs_sub], exact max_sub_min_eq_abs' _ _ } end decidable_linear_ordered_add_comm_group section decidable_linear_ordered_semiring variables [decidable_linear_ordered_semiring Ξ±] {a b c d : Ξ±} lemma mul_max_of_nonneg (b c : Ξ±) (ha : 0 ≀ a) : a * max b c = max (a * b) (a * c) := (monotone_mul_left_of_nonneg ha).map_max lemma mul_min_of_nonneg (b c : Ξ±) (ha : 0 ≀ a) : a * min b c = min (a * b) (a * c) := (monotone_mul_left_of_nonneg ha).map_min lemma max_mul_of_nonneg (a b : Ξ±) (hc : 0 ≀ c) : max a b * c = max (a * c) (b * c) := (monotone_mul_right_of_nonneg hc).map_max lemma min_mul_of_nonneg (a b : Ξ±) (hc : 0 ≀ c) : min a b * c = min (a * c) (b * c) := (monotone_mul_right_of_nonneg hc).map_min end decidable_linear_ordered_semiring section decidable_linear_ordered_comm_ring variables [decidable_linear_ordered_comm_ring Ξ±] {a b c d : Ξ±} @[simp] lemma abs_one : abs (1 : Ξ±) = 1 := abs_of_pos zero_lt_one lemma max_mul_mul_le_max_mul_max (b c : Ξ±) (ha : 0 ≀ a) (hd: 0 ≀ d) : max (a * b) (d * c) ≀ max a c * max d b := have ba : b * a ≀ max d b * max c a, from mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)), have cd : c * d ≀ max a c * max b d, from mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)), max_le (by simpa [mul_comm, max_comm] using ba) (by simpa [mul_comm, max_comm] using cd) end decidable_linear_ordered_comm_ring
b3bd27030fd8be86d628242fba2714b9080703bd
5883d9218e6f144e20eee6ca1dab8529fa1a97c0
/src/exp/type.lean
5c08591d71dc621b99dda4e04c3abb03b1e1c9b4
[]
no_license
spl/alpha-conversion-is-easy
0d035bc570e52a6345d4890e4d0c9e3f9b8126c1
ed937fe85d8495daffd9412a5524c77b9fcda094
refs/heads/master
1,607,649,280,020
1,517,380,240,000
1,517,380,240,000
52,174,747
4
0
null
1,456,052,226,000
1,456,001,163,000
Lean
UTF-8
Lean
false
false
1,448
lean
/- This file contains the `exp` inductive data type. -/ import vname namespace acie ----------------------------------------------------------------- variables {V : Type} [decidable_eq V] -- Type of variable names variables {vs : Type β†’ Type} [vset vs V] -- Type of variable name sets /- `exp` is an inductive data type representing a lambda calculus expression language. The type is indexed by a finite set of variables that are free in the given expression. -/ inductive exp : vs V β†’ Type | var : Ξ  {X : vs V}, ν∈ X β†’ exp X -- variable | app : Ξ  {X : vs V}, exp X β†’ exp X β†’ exp X -- application | lam : Ξ  {X : vs V} {a : V}, exp (insert a X) β†’ exp X -- lambda abstraction namespace exp ------------------------------------------------------------------ -- A utility pattern for binding the name. @[pattern, reducible] def lam' (a : V) {X : vs V} : exp (insert a X) β†’ exp X := @exp.lam _ _ _ _ _ a private def repr [has_repr V] : βˆ€ (X : vs V), exp X β†’ string | X (var x) := has_repr.repr x | X (app f e) := "(" ++ repr _ f ++ " " ++ repr _ e ++ ")" | X (lam' a e) := "(Ξ»" ++ has_repr.repr a ++ "." ++ repr _ e ++ ")" instance has_repr [has_repr V] (X : vs V) : has_repr (exp X) := ⟨repr X⟩ end /- namespace -/ exp -------------------------------------------------------- end /- namespace -/ acie -------------------------------------------------------
ab5ce13308c006091ce4e192abdbb4a49d44c51f
4fa161becb8ce7378a709f5992a594764699e268
/src/measure_theory/simple_func_dense.lean
f76f986337c24bfdff82f3fdb610a89a631817fa
[ "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,957
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.l1_space /-! # Density of simple functions Show that each Borel measurable function can be approximated, both pointwise and in `LΒΉ` norm, by a sequence of simple functions. -/ noncomputable theory open set filter topological_space open_locale classical topological_space universes u v variables {Ξ± : Type u} {Ξ² : Type v} {ΞΉ : Type*} namespace measure_theory open ennreal nat metric variables [measure_space Ξ±] [normed_group Ξ²] [second_countable_topology Ξ²] [measurable_space Ξ²] [borel_space Ξ²] local infixr ` β†’β‚› `:25 := simple_func -- FIXME this monolithic proof used to cause a deterministic timeout with `-T50000` -- It no longer does, but it should still be broken up into a sequence of more manageable pieces. lemma simple_func_sequence_tendsto {f : Ξ± β†’ Ξ²} (hf : measurable f) : βˆƒ (F : β„• β†’ (Ξ± β†’β‚› Ξ²)), βˆ€ x : Ξ±, tendsto (Ξ» n, F n x) at_top (𝓝 (f x)) ∧ βˆ€ n, βˆ₯F n xβˆ₯ ≀ βˆ₯f xβˆ₯ + βˆ₯f xβˆ₯ := -- enumerate a countable dense subset {e k} of Ξ² let ⟨D, ⟨D_countable, D_dense⟩⟩ := @separable_space.exists_countable_closure_eq_univ Ξ² _ _ in let e := enumerate_countable D_countable 0 in let E := range e in have E_dense : closure E = univ := dense_of_subset_dense (subset_range_enumerate D_countable 0) D_dense, let A' (N k : β„•) : set Ξ± := f ⁻¹' (metric.ball (e k) (1 / (N+1 : ℝ)) \ metric.ball 0 (1 / (N+1 : ℝ))) in let A N := disjointed (A' N) in have is_measurable_A' : βˆ€ {N k}, is_measurable (A' N k) := Ξ» N k, hf.preimage $ is_measurable.inter is_measurable_ball $ is_measurable.compl is_measurable_ball, have is_measurable_A : βˆ€ {N k}, is_measurable (A N k) := Ξ» N, is_measurable.disjointed $ Ξ» k, is_measurable_A', have A_subset_A' : βˆ€ {N k x}, x ∈ A N k β†’ x ∈ A' N k := Ξ» N k, inter_subset_left _ _, have dist_ek_fx' : βˆ€ {x N k}, x ∈ A' N k β†’ (dist (e k) (f x) < 1 / (N+1 : ℝ)) := Ξ» x N k, by { rw [dist_comm], simpa using (Ξ» a b, a) }, have dist_ek_fx : βˆ€ {x N k}, x ∈ A N k β†’ (dist (e k) (f x) < 1 / (N+1 : ℝ)) := Ξ» x N k h, dist_ek_fx' (A_subset_A' h), have norm_fx' : βˆ€ {x N k}, x ∈ A' N k β†’ (1 / (N+1 : ℝ)) ≀ βˆ₯f xβˆ₯ := Ξ» x N k, by simp [ball_0_eq], have norm_fx : βˆ€ {x N k}, x ∈ A N k β†’ (1 / (N+1 : ℝ)) ≀ βˆ₯f xβˆ₯ := Ξ» x N k h, norm_fx' (A_subset_A' h), -- construct the desired sequence of simple functions let M N x := nat.find_greatest (Ξ» M, x ∈ ⋃ k ≀ N, (A M k)) N in let k N x := nat.find_greatest (Ξ» k, x ∈ A (M N x) k) N in let F N x := if x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k then e (k N x) else 0 in -- prove properties of the construction above have k_unique : βˆ€ {M k k' x}, x ∈ A M k ∧ x ∈ A M k' β†’ k = k' := Ξ» M k k' x h, begin by_contradiction k_ne_k', have NE : (A M k ∩ A M k').nonempty, from ⟨x, h⟩, have E : A M k ∩ A M k' = βˆ… := disjoint_disjointed' k k' k_ne_k', exact NE.ne_empty E, end, have x_mem_Union_k : βˆ€ {N x}, (x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k) β†’ x ∈ ⋃ k ≀ N, A (M N x) k := Ξ» N x h, @nat.find_greatest_spec (Ξ» M, x ∈ ⋃ k ≀ N, (A M k)) _ N ( let ⟨M, hM⟩ := mem_Union.1 (h) in let ⟨hM₁, hMβ‚‚βŸ© := mem_Union.1 hM in ⟨M, ⟨hM₁, hMβ‚‚βŸ©βŸ©), have x_mem_A : βˆ€ {N x}, (x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k) β†’ x ∈ A (M N x) (k N x) := Ξ» N x h, @nat.find_greatest_spec (Ξ» k, x ∈ A (M N x) k) _ N ( let ⟨k, hk⟩ := mem_Union.1 (x_mem_Union_k h) in let ⟨hk₁, hkβ‚‚βŸ© := mem_Union.1 hk in ⟨k, ⟨hk₁, hkβ‚‚βŸ©βŸ©), have x_mem_A' : βˆ€ {N x}, (x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k) β†’ x ∈ A' (M N x) (k N x) := Ξ» N x h, mem_of_subset_of_mem (inter_subset_left _ _) (x_mem_A h), -- prove that for all N, (F N) has finite range have F_finite : βˆ€ {N}, finite (range (F N)) := begin assume N, apply finite_range_ite, { rw range_comp, apply finite_image, exact finite_range_find_greatest }, { exact finite_range_const } end, -- prove that for all N, (F N) is a measurable function have F_measurable : βˆ€ {N}, measurable (F N) := begin assume N, refine measurable.if _ _ measurable_const, -- show `is_measurable {a : Ξ± | a ∈ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k}` { rw set_of_mem_eq, simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable_A] }, -- show `measurable (Ξ» (x : Ξ±), e (k N x))` apply measurable.comp measurable_from_nat, apply measurable_find_greatest, assume k' k'_le_N, by_cases k'_eq_0 : k' = 0, -- if k' = 0 have : {x | k N x = 0} = (-⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k) βˆͺ (⋃ (m ≀ N), A m 0 - ⋃ m' (hmm' : m < m') (hm'N : m' ≀ N) (k ≀ N), A m' k), { ext, split, { rw [mem_set_of_eq, mem_union_eq, or_iff_not_imp_left, mem_compl_eq, not_not_mem], assume k_eq_0 x_mem, simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff], refine ⟨M N x, ⟨nat.find_greatest_le, ⟨by { rw ← k_eq_0, exact x_mem_A x_mem} , _⟩⟩⟩, assume m hMm hmN k k_le_N, have := nat.find_greatest_is_greatest _ m ⟨hMm, hmN⟩, { simp only [not_exists, exists_prop, mem_Union, not_and] at this, exact this k k_le_N }, { exact ⟨M N x, ⟨nat.find_greatest_le, x_mem_Union_k x_mem⟩⟩ } }, { simp only [mem_set_of_eq, mem_union_eq, mem_compl_eq], by_cases x_mem : (x βˆ‰ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k), { intro, apply find_greatest_eq_zero, assume k k_le_N hx, have : x ∈ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k, { simp only [mem_Union], use [M N x, nat.find_greatest_le, k, k_le_N, hx] }, contradiction }, { rw not_not_mem at x_mem, assume h, cases h, contradiction, simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff] at h, rcases h with ⟨m, ⟨m_le_N, ⟨hx, hm⟩⟩⟩, by_cases m_lt_M : m < M N x, { have := hm (M N x) m_lt_M nat.find_greatest_le (k N x) nat.find_greatest_le, have := x_mem_A x_mem, contradiction }, rw not_lt at m_lt_M, by_cases m_gt_M : m > M N x, { have := nat.find_greatest_is_greatest _ m ⟨m_gt_M, m_le_N⟩, { have : x ∈ ⋃ k ≀ N, A m k, { exact mem_bUnion (nat.zero_le N) hx }, contradiction }, { exact ⟨m, m_le_N, mem_bUnion (nat.zero_le _) hx⟩ } }, rw not_lt at m_gt_M, have M_eq_m := le_antisymm m_lt_M m_gt_M, rw ← k'_eq_0, exact k_unique ⟨x_mem_A x_mem, by { rw [k'_eq_0, M_eq_m], exact hx }⟩ } } }, -- end of `have` rw [k'_eq_0, this], apply is_measurable.union, { apply is_measurable.compl, simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable_A] }, { simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable.diff, is_measurable_A] }, -- if k' β‰  0 have : {x | k N x = k'} = ⋃(m ≀ N), A m k' - ⋃m' (hmm' : m < m') (hm'N : m' ≀ N) (k ≀ N), A m' k, { ext, split, { rw [mem_set_of_eq], assume k_eq_k', have x_mem : x ∈ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k, { have := find_greatest_of_ne_zero k_eq_k' k'_eq_0, simp only [mem_Union], use [M N x, nat.find_greatest_le, k', k'_le_N, this] }, simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff], refine ⟨M N x, ⟨nat.find_greatest_le, ⟨by { rw ← k_eq_k', exact x_mem_A x_mem} , _⟩⟩⟩, assume m hMm hmN k k_le_N, have := nat.find_greatest_is_greatest _ m ⟨hMm, hmN⟩, { simp only [not_exists, exists_prop, mem_Union, not_and] at this, exact this k k_le_N }, exact ⟨M N x, ⟨nat.find_greatest_le, x_mem_Union_k x_mem⟩⟩ }, { simp only [mem_set_of_eq, mem_union_eq, mem_compl_eq], assume h, have x_mem : x ∈ ⋃ (M : β„•) (H : M ≀ N) (k : β„•) (H : k ≀ N), A M k, { simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff] at h, rcases h with ⟨m, hm, hx, _⟩, simp only [mem_Union], use [m, hm, k', k'_le_N, hx] }, simp only [not_exists, exists_prop, mem_Union, not_and, sub_eq_diff, mem_diff] at h, rcases h with ⟨m, ⟨m_le_N, ⟨hx, hm⟩⟩⟩, by_cases m_lt_M : m < M N x, { have := hm (M N x) m_lt_M nat.find_greatest_le (k N x) nat.find_greatest_le, have := x_mem_A x_mem, contradiction }, rw not_lt at m_lt_M, by_cases m_gt_M : m > M N x, { have := nat.find_greatest_is_greatest _ m ⟨m_gt_M, m_le_N⟩, have : x ∈ ⋃ k ≀ N, A m k := mem_bUnion k'_le_N hx, contradiction, { simp only [mem_Union], use [m, m_le_N, k', k'_le_N, hx] }}, rw not_lt at m_gt_M, have M_eq_m := le_antisymm m_lt_M m_gt_M, exact k_unique ⟨x_mem_A x_mem, by { rw M_eq_m, exact hx }⟩ } }, -- end of `have` rw this, simp [is_measurable.Union, is_measurable.Union_Prop, is_measurable.diff, is_measurable_A] end, -- start of proof ⟨λ N, ⟨F N, Ξ» x, measurable.preimage F_measurable is_measurable_singleton, F_finite⟩, -- The pointwise convergence part of the theorem Ξ» x, ⟨metric.tendsto_at_top.2 $ Ξ» Ξ΅ hΞ΅, classical.by_cases --first case : f x = 0 ( assume fx_eq_0 : f x = 0, have x_not_mem_A' : βˆ€ {M k}, x βˆ‰ A' M k := Ξ» M k, begin simp only [mem_preimage, fx_eq_0, metric.mem_ball, one_div_eq_inv, norm_zero, not_and, not_lt, add_comm, not_le, dist_zero_right, mem_diff], assume h, rw add_comm, exact inv_pos_of_nat end, have x_not_mem_A : βˆ€ {M k}, x βˆ‰ A M k := by { assume M k h, have := disjointed_subset h, exact absurd this x_not_mem_A' }, have F_eq_0 : βˆ€ {N}, F N x = 0 := Ξ» N, by simp [F, if_neg, mem_Union, x_not_mem_A], -- end of `have` ⟨0, Ξ» n hn, show dist (F n x) (f x) < Ξ΅, by {rw [fx_eq_0, F_eq_0, dist_self], exact hΞ΅}⟩ ) --second case : f x β‰  0 ( assume fx_ne_0 : f x β‰  0, let ⟨Nβ‚€, hN⟩ := exists_nat_one_div_lt (lt_min (norm_pos_iff.2 fx_ne_0) hΞ΅) in have norm_fx_gt : _ := (lt_min_iff.1 hN).1, have Ξ΅_gt : _ := (lt_min_iff.1 hN).2, have x_mem_Union_k_Nβ‚€ : x ∈ ⋃ k, A Nβ‚€ k := let ⟨k, hk⟩ := mem_closure_range_iff_nat.1 (by { rw E_dense, exact mem_univ (f x) }) Nβ‚€ in begin rw [Union_disjointed, mem_Union], use k, rw [mem_preimage], simp, rw [← one_div_eq_inv], exact ⟨hk, le_of_lt norm_fx_gt⟩ end, let ⟨kβ‚€, x_mem_A⟩ := mem_Union.1 x_mem_Union_k_Nβ‚€ in let n := max Nβ‚€ kβ‚€ in have x_mem_Union_Union : βˆ€ {N} (hN : n ≀ N), x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k := assume N hN, mem_Union.2 ⟨Nβ‚€, mem_Union.2 ⟨le_trans (le_max_left _ _) hN, mem_Union.2 ⟨kβ‚€, mem_Union.2 ⟨le_trans (le_max_right _ _) hN, x_mem_A⟩⟩⟩⟩, have FN_eq : βˆ€ {N} (hN : n ≀ N), F N x = e (k N x) := assume N hN, if_pos $ x_mem_Union_Union hN, -- start of proof ⟨n, assume N hN, have Nβ‚€_le_N : Nβ‚€ ≀ N := le_trans (le_max_left _ _) hN, have kβ‚€_le_N : kβ‚€ ≀ N := le_trans (le_max_right _ _) hN, show dist (F N x) (f x) < Ξ΅, from calc dist (F N x) (f x) = dist (e (k N x)) (f x) : by rw FN_eq hN ... < 1 / ((M N x : ℝ) + 1) : begin have := x_mem_A' (x_mem_Union_Union hN), rw [mem_preimage, mem_diff, metric.mem_ball, dist_comm] at this, exact this.1 end ... ≀ 1 / ((Nβ‚€ : ℝ) + 1) : @one_div_le_one_div_of_le _ _ ((Nβ‚€ : ℝ) + 1) ((M N x : ℝ) + 1) (nat.cast_add_one_pos Nβ‚€) (add_le_add_right (nat.cast_le.2 (nat.le_find_greatest Nβ‚€_le_N (mem_bUnion kβ‚€_le_N x_mem_A))) 1) ... < Ξ΅ : Ξ΅_gt ⟩ ), -- second part of the theorem assume N, show βˆ₯F N xβˆ₯ ≀ βˆ₯f xβˆ₯ + βˆ₯f xβˆ₯, from classical.by_cases ( assume h : x ∈ ⋃ M ≀ N, ⋃ k ≀ N, A M k, calc βˆ₯F N xβˆ₯ = dist (F N x) 0 : by simp ... = dist (e (k N x)) 0 : begin simp only [F], rw if_pos h end ... ≀ dist (e (k N x)) (f x) + dist (f x) 0 : dist_triangle _ _ _ ... = dist (e (k N x)) (f x) + βˆ₯f xβˆ₯ : by simp ... ≀ 1 / ((M N x : ℝ) + 1) + βˆ₯f xβˆ₯ : le_of_lt $ add_lt_add_right (dist_ek_fx (x_mem_A h)) _ ... ≀ βˆ₯f xβˆ₯ + βˆ₯f xβˆ₯ : add_le_add_right (norm_fx (x_mem_A h) ) _) ( assume h : x βˆ‰ ⋃ M ≀ N, ⋃ k ≀ N, A M k, have F_eq_0 : F N x = 0 := if_neg h, by { simp only [F_eq_0, norm_zero], exact add_nonneg (norm_nonneg _) (norm_nonneg _) } )⟩⟩ lemma simple_func_sequence_tendsto' {f : Ξ± β†’ Ξ²} (hfm : measurable f) (hfi : integrable f) : βˆƒ (F : β„• β†’ (Ξ± β†’β‚› Ξ²)), (βˆ€n, integrable (F n)) ∧ tendsto (Ξ» n, ∫⁻ x, nndist (F n x) (f x)) at_top (𝓝 0) := let ⟨F, hF⟩ := simple_func_sequence_tendsto hfm in let G : β„• β†’ Ξ± β†’ ennreal := Ξ»n x, nndist (F n x) (f x) in let g : Ξ± β†’ ennreal := Ξ»x, nnnorm (f x) + nnnorm (f x) + nnnorm (f x) in have hF_meas : βˆ€ n, measurable (G n) := Ξ» n, measurable.comp measurable_coe $ (F n).measurable.nndist hfm, have hg_meas : measurable g := measurable.comp measurable_coe $ measurable.add (measurable.add hfm.nnnorm hfm.nnnorm) hfm.nnnorm, have h_bound : βˆ€ n, βˆ€β‚˜ x, G n x ≀ g x := Ξ» n, ae_of_all _ $ Ξ» x, coe_le_coe.2 $ calc nndist (F n x) (f x) ≀ nndist (F n x) 0 + nndist 0 (f x) : nndist_triangle _ _ _ ... = nnnorm (F n x) + nnnorm (f x) : by simp [nndist_eq_nnnorm] ... ≀ nnnorm (f x) + nnnorm (f x) + nnnorm (f x) : by { simp [nnreal.coe_le_coe.symm, (hF x).2, add_comm] }, have h_finite : lintegral g < ⊀ := calc (∫⁻ x, nnnorm (f x) + nnnorm (f x) + nnnorm (f x)) = (∫⁻ x, nnnorm (f x)) + (∫⁻ x, nnnorm (f x)) + (∫⁻ x, nnnorm (f x)) : by { rw [lintegral_add, lintegral_nnnorm_add], exacts [hfm, hfm, hfm.ennnorm.add hfm.ennnorm, hfm.ennnorm] } ... < ⊀ : by { simp only [and_self, add_lt_top], exact hfi}, have h_lim : βˆ€β‚˜ x, tendsto (Ξ» n, G n x) at_top (𝓝 0) := ae_of_all _ $ Ξ» x, begin apply (@tendsto_coe β„• at_top (Ξ» n, nndist (F n x) (f x)) 0).2, apply (@nnreal.tendsto_coe β„• at_top (Ξ» n, nndist (F n x) (f x)) 0).1, apply tendsto_iff_dist_tendsto_zero.1 (hF x).1 end, begin use F, split, { assume n, exact calc (∫⁻ a, nnnorm (F n a)) ≀ ∫⁻ a, nnnorm (f a) + nnnorm (f a) : lintegral_mono (by { assume a, simp only [coe_add.symm, coe_le_coe], exact (hF a).2 n }) ... = (∫⁻ a, nnnorm (f a)) + (∫⁻ a, nnnorm (f a)) : lintegral_nnnorm_add hfm hfm ... < ⊀ : by simp only [add_lt_top, and_self]; exact hfi }, convert @tendsto_lintegral_of_dominated_convergence _ _ G (Ξ» a, 0) g hF_meas h_bound h_finite h_lim, simp only [lintegral_zero] end end measure_theory
46c062d6085ab86a83cbd3880498fe47f2175318
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/bernoulli.lean
b77fb4c73944868a15777fc3378c3e7b7d2438c5
[ "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
16,851
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Kevin Buzzard -/ import algebra.big_operators.nat_antidiagonal import algebra.geom_sum import data.fintype.big_operators import ring_theory.power_series.well_known import tactic.field_simp /-! # Bernoulli numbers The Bernoulli numbers are a sequence of rational numbers that frequently show up in number theory. ## Mathematical overview The Bernoulli numbers $(B_0, B_1, B_2, \ldots)=(1, -1/2, 1/6, 0, -1/30, \ldots)$ are a sequence of rational numbers. They show up in the formula for the sums of $k$th powers. They are related to the Taylor series expansions of $x/\tan(x)$ and of $\coth(x)$, and also show up in the values that the Riemann Zeta function takes both at both negative and positive integers (and hence in the theory of modular forms). For example, if $1 \leq n$ is even then $$\zeta(2n)=\sum_{t\geq1}t^{-2n}=(-1)^{n+1}\frac{(2\pi)^{2n}B_{2n}}{2(2n)!}.$$ Note however that this result is not yet formalised in Lean. The Bernoulli numbers can be formally defined using the power series $$\sum B_n\frac{t^n}{n!}=\frac{t}{1-e^{-t}}$$ although that happens to not be the definition in mathlib (this is an *implementation detail* and need not concern the mathematician). Note that $B_1=-1/2$, meaning that we are using the $B_n^-$ of [from Wikipedia](https://en.wikipedia.org/wiki/Bernoulli_number). ## Implementation detail The Bernoulli numbers are defined using well-founded induction, by the formula $$B_n=1-\sum_{k\lt n}\frac{\binom{n}{k}}{n-k+1}B_k.$$ This formula is true for all $n$ and in particular $B_0=1$. Note that this is the definition for positive Bernoulli numbers, which we call `bernoulli'`. The negative Bernoulli numbers are then defined as `bernoulli := (-1)^n * bernoulli'`. ## Main theorems `sum_bernoulli : βˆ‘ k in finset.range n, (n.choose k : β„š) * bernoulli k = 0` -/ open_locale nat big_operators open finset nat finset.nat power_series variables (A : Type*) [comm_ring A] [algebra β„š A] /-! ### Definitions -/ /-- The Bernoulli numbers: the $n$-th Bernoulli number $B_n$ is defined recursively via $$B_n = 1 - \sum_{k < n} \binom{n}{k}\frac{B_k}{n+1-k}$$ -/ def bernoulli' : β„• β†’ β„š := well_founded.fix lt_wf $ Ξ» n bernoulli', 1 - βˆ‘ k : fin n, n.choose k / (n - k + 1) * bernoulli' k k.2 lemma bernoulli'_def' (n : β„•) : bernoulli' n = 1 - βˆ‘ k : fin n, n.choose k / (n - k + 1) * bernoulli' k := well_founded.fix_eq _ _ _ lemma bernoulli'_def (n : β„•) : bernoulli' n = 1 - βˆ‘ k in range n, n.choose k / (n - k + 1) * bernoulli' k := by { rw [bernoulli'_def', ← fin.sum_univ_eq_sum_range], refl } lemma bernoulli'_spec (n : β„•) : βˆ‘ k in range n.succ, (n.choose (n - k) : β„š) / (n - k + 1) * bernoulli' k = 1 := begin rw [sum_range_succ_comm, bernoulli'_def n, tsub_self], conv in (n.choose (_ - _)) { rw choose_symm (mem_range.1 H).le }, simp only [one_mul, cast_one, sub_self, sub_add_cancel, choose_zero_right, zero_add, div_one], end lemma bernoulli'_spec' (n : β„•) : βˆ‘ k in antidiagonal n, ((k.1 + k.2).choose k.2 : β„š) / (k.2 + 1) * bernoulli' k.1 = 1 := begin refine ((sum_antidiagonal_eq_sum_range_succ_mk _ n).trans _).trans (bernoulli'_spec n), refine sum_congr rfl (Ξ» x hx, _), simp only [add_tsub_cancel_of_le, mem_range_succ_iff.mp hx, cast_sub], end /-! ### Examples -/ section examples @[simp] lemma bernoulli'_zero : bernoulli' 0 = 1 := by { rw bernoulli'_def, norm_num } @[simp] lemma bernoulli'_one : bernoulli' 1 = 1/2 := by { rw bernoulli'_def, norm_num } @[simp] lemma bernoulli'_two : bernoulli' 2 = 1/6 := by { rw bernoulli'_def, norm_num [sum_range_succ] } @[simp] lemma bernoulli'_three : bernoulli' 3 = 0 := by { rw bernoulli'_def, norm_num [sum_range_succ] } @[simp] lemma bernoulli'_four : bernoulli' 4 = -1/30 := have nat.choose 4 2 = 6 := dec_trivial, -- shrug by { rw bernoulli'_def, norm_num [sum_range_succ, this] } end examples @[simp] lemma sum_bernoulli' (n : β„•) : βˆ‘ k in range n, (n.choose k : β„š) * bernoulli' k = n := begin cases n, { simp }, suffices : (n + 1 : β„š) * βˆ‘ k in range n, ↑(n.choose k) / (n - k + 1) * bernoulli' k = βˆ‘ x in range n, ↑(n.succ.choose x) * bernoulli' x, { rw_mod_cast [sum_range_succ, bernoulli'_def, ← this, choose_succ_self_right], ring }, simp_rw [mul_sum, ← mul_assoc], refine sum_congr rfl (Ξ» k hk, _), congr', have : ((n - k : β„•) : β„š) + 1 β‰  0 := by apply_mod_cast succ_ne_zero, field_simp [← cast_sub (mem_range.1 hk).le, mul_comm], rw_mod_cast [tsub_add_eq_add_tsub (mem_range.1 hk).le, choose_mul_succ_eq], end /-- The exponential generating function for the Bernoulli numbers `bernoulli' n`. -/ def bernoulli'_power_series := mk $ Ξ» n, algebra_map β„š A (bernoulli' n / n!) theorem bernoulli'_power_series_mul_exp_sub_one : bernoulli'_power_series A * (exp A - 1) = X * exp A := begin ext n, -- constant coefficient is a special case cases n, { simp }, rw [bernoulli'_power_series, coeff_mul, mul_comm X, sum_antidiagonal_succ'], suffices : βˆ‘ p in antidiagonal n, (bernoulli' p.1 / p.1!) * ((p.2 + 1) * p.2!)⁻¹ = n!⁻¹, { simpa [ring_hom.map_sum] using congr_arg (algebra_map β„š A) this }, apply eq_inv_of_mul_eq_one_left, rw sum_mul, convert bernoulli'_spec' n using 1, apply sum_congr rfl, simp_rw [mem_antidiagonal], rintro ⟨i, j⟩ rfl, have : (j + 1 : β„š) β‰  0 := by exact_mod_cast succ_ne_zero j, have : (j + 1 : β„š) * j! * i! β‰  0 := by simpa [factorial_ne_zero], have := factorial_mul_factorial_dvd_factorial_add i j, field_simp [mul_comm _ (bernoulli' i), mul_assoc, add_choose], rw_mod_cast [mul_comm (j + 1), mul_div_assoc, ← mul_assoc], rw [cast_mul, cast_mul, mul_div_mul_right, cast_div_char_zero, cast_mul], assumption, rwa nat.cast_succ, end /-- Odd Bernoulli numbers (greater than 1) are zero. -/ theorem bernoulli'_odd_eq_zero {n : β„•} (h_odd : odd n) (hlt : 1 < n) : bernoulli' n = 0 := begin let B := mk (Ξ» n, bernoulli' n / n!), suffices : (B - eval_neg_hom B) * (exp β„š - 1) = X * (exp β„š - 1), { cases mul_eq_mul_right_iff.mp this; simp only [power_series.ext_iff, eval_neg_hom, coeff_X] at h, { apply eq_zero_of_neg_eq, specialize h n, split_ifs at h; simp [h_odd.neg_one_pow, factorial_ne_zero, *] at * }, { simpa using h 1 } }, have h : B * (exp β„š - 1) = X * exp β„š, { simpa [bernoulli'_power_series] using bernoulli'_power_series_mul_exp_sub_one β„š }, rw [sub_mul, h, mul_sub X, sub_right_inj, ← neg_sub, mul_neg, neg_eq_iff_eq_neg], suffices : eval_neg_hom (B * (exp β„š - 1)) * exp β„š = eval_neg_hom (X * exp β„š) * exp β„š, { simpa [mul_assoc, sub_mul, mul_comm (eval_neg_hom (exp β„š)), exp_mul_exp_neg_eq_one] }, congr', end /-- The Bernoulli numbers are defined to be `bernoulli'` with a parity sign. -/ def bernoulli (n : β„•) : β„š := (-1)^n * bernoulli' n lemma bernoulli'_eq_bernoulli (n : β„•) : bernoulli' n = (-1)^n * bernoulli n := by simp [bernoulli, ← mul_assoc, ← sq, ← pow_mul, mul_comm n 2, pow_mul] @[simp] lemma bernoulli_zero : bernoulli 0 = 1 := by simp [bernoulli] @[simp] lemma bernoulli_one : bernoulli 1 = -1/2 := by norm_num [bernoulli] theorem bernoulli_eq_bernoulli'_of_ne_one {n : β„•} (hn : n β‰  1) : bernoulli n = bernoulli' n := begin by_cases h0 : n = 0, { simp [h0] }, rw [bernoulli, neg_one_pow_eq_pow_mod_two], cases mod_two_eq_zero_or_one n, { simp [h] }, simp [bernoulli'_odd_eq_zero (odd_iff.mpr h) (one_lt_iff_ne_zero_and_ne_one.mpr ⟨h0, hn⟩)], end @[simp] theorem sum_bernoulli (n : β„•): βˆ‘ k in range n, (n.choose k : β„š) * bernoulli k = if n = 1 then 1 else 0 := begin cases n, { simp }, cases n, { simp }, suffices : βˆ‘ i in range n, ↑((n + 2).choose (i + 2)) * bernoulli (i + 2) = n / 2, { simp only [this, sum_range_succ', cast_succ, bernoulli_one, bernoulli_zero, choose_one_right, mul_one, choose_zero_right, cast_zero, if_false, zero_add, succ_succ_ne_one], ring }, have f := sum_bernoulli' n.succ.succ, simp_rw [sum_range_succ', bernoulli'_one, choose_one_right, cast_succ, ← eq_sub_iff_add_eq] at f, convert f, { funext x, rw bernoulli_eq_bernoulli'_of_ne_one (succ_ne_zero x ∘ succ.inj) }, { simp only [one_div, mul_one, bernoulli'_zero, cast_one, choose_zero_right, add_sub_cancel], ring }, end lemma bernoulli_spec' (n : β„•) : βˆ‘ k in antidiagonal n, ((k.1 + k.2).choose k.2 : β„š) / (k.2 + 1) * bernoulli k.1 = if n = 0 then 1 else 0 := begin cases n, { simp }, rw if_neg (succ_ne_zero _), -- algebra facts have h₁ : (1, n) ∈ antidiagonal n.succ := by simp [mem_antidiagonal, add_comm], have hβ‚‚ : (n : β„š) + 1 β‰  0 := by apply_mod_cast succ_ne_zero, have h₃ : (1 + n).choose n = n + 1 := by simp [add_comm], -- key equation: the corresponding fact for `bernoulli'` have H := bernoulli'_spec' n.succ, -- massage it to match the structure of the goal, then convert piece by piece rw sum_eq_add_sum_diff_singleton h₁ at H ⊒, apply add_eq_of_eq_sub', convert eq_sub_of_add_eq' H using 1, { refine sum_congr rfl (Ξ» p h, _), obtain ⟨h', h''⟩ : p ∈ _ ∧ p β‰  _ := by rwa [mem_sdiff, mem_singleton] at h, simp [bernoulli_eq_bernoulli'_of_ne_one ((not_congr (antidiagonal_congr h' h₁)).mp h'')] }, { field_simp [h₃], norm_num }, end /-- The exponential generating function for the Bernoulli numbers `bernoulli n`. -/ def bernoulli_power_series := mk $ Ξ» n, algebra_map β„š A (bernoulli n / n!) theorem bernoulli_power_series_mul_exp_sub_one : bernoulli_power_series A * (exp A - 1) = X := begin ext n, -- constant coefficient is a special case cases n, { simp }, simp only [bernoulli_power_series, coeff_mul, coeff_X, sum_antidiagonal_succ', one_div, coeff_mk, coeff_one, coeff_exp, linear_map.map_sub, factorial, if_pos, cast_succ, cast_one, cast_mul, sub_zero, ring_hom.map_one, add_eq_zero_iff, if_false, _root_.inv_one, zero_add, one_ne_zero, mul_zero, and_false, sub_self, ← ring_hom.map_mul, ← ring_hom.map_sum], cases n, { simp }, rw if_neg n.succ_succ_ne_one, have hfact : βˆ€ m, (m! : β„š) β‰  0 := Ξ» m, by exact_mod_cast factorial_ne_zero m, have hite2 : ite (n.succ = 0) 1 0 = (0 : β„š) := if_neg n.succ_ne_zero, rw [←map_zero (algebra_map β„š A), ←zero_div (n.succ! : β„š), ←hite2, ← bernoulli_spec', sum_div], refine congr_arg (algebra_map β„š A) (sum_congr rfl $ Ξ» x h, eq_div_of_mul_eq (hfact n.succ) _), rw mem_antidiagonal at h, have hj : (x.2 + 1 : β„š) β‰  0 := by exact_mod_cast succ_ne_zero _, field_simp [← h, mul_ne_zero hj (hfact x.2), hfact x.1, mul_comm _ (bernoulli x.1), mul_assoc, add_choose, cast_div_char_zero (factorial_mul_factorial_dvd_factorial_add _ _), nat.factorial_ne_zero, hj], cc, end section faulhaber /-- **Faulhaber's theorem** relating the **sum of of p-th powers** to the Bernoulli numbers: $$\sum_{k=0}^{n-1} k^p = \sum_{i=0}^p B_i\binom{p+1}{i}\frac{n^{p+1-i}}{p+1}.$$ See https://proofwiki.org/wiki/Faulhaber%27s_Formula and [orosi2018faulhaber] for the proof provided here. -/ theorem sum_range_pow (n p : β„•) : βˆ‘ k in range n, (k : β„š) ^ p = βˆ‘ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1) := begin have hne : βˆ€ m : β„•, (m! : β„š) β‰  0 := Ξ» m, by exact_mod_cast factorial_ne_zero m, -- compute the Cauchy product of two power series have h_cauchy : mk (Ξ» p, bernoulli p / p!) * mk (Ξ» q, coeff β„š (q + 1) (exp β„š ^ n)) = mk (Ξ» p, βˆ‘ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1)!), { ext q : 1, let f := Ξ» a b, bernoulli a / a! * coeff β„š (b + 1) (exp β„š ^ n), -- key step: use `power_series.coeff_mul` and then rewrite sums simp only [coeff_mul, coeff_mk, cast_mul, sum_antidiagonal_eq_sum_range_succ f], apply sum_congr rfl, simp_intros m h only [finset.mem_range], simp only [f, exp_pow_eq_rescale_exp, rescale, one_div, coeff_mk, ring_hom.coe_mk, coeff_exp, ring_hom.id_apply, cast_mul, algebra_map_rat_rat], -- manipulate factorials and binomial coefficients rw [choose_eq_factorial_div_factorial h.le, eq_comm, div_eq_iff (hne q.succ), succ_eq_add_one, mul_assoc _ _ ↑q.succ!, mul_comm _ ↑q.succ!, ← mul_assoc, div_mul_eq_mul_div, mul_comm (↑n ^ (q - m + 1)), ← mul_assoc _ _ (↑n ^ (q - m + 1)), ← one_div, mul_one_div, div_div, tsub_add_eq_add_tsub (le_of_lt_succ h), cast_div, cast_mul], { ring }, { exact factorial_mul_factorial_dvd_factorial h.le }, { simp [hne] } }, -- same as our goal except we pull out `p!` for convenience have hps : βˆ‘ k in range n, ↑k ^ p = (βˆ‘ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1)!) * p!, { suffices : mk (Ξ» p, βˆ‘ k in range n, ↑k ^ p * algebra_map β„š β„š p!⁻¹) = mk (Ξ» p, βˆ‘ i in range (p + 1), bernoulli i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1)!), { rw [← div_eq_iff (hne p), div_eq_mul_inv, sum_mul], rw power_series.ext_iff at this, simpa using this p }, -- the power series `exp β„š - 1` is non-zero, a fact we need in order to use `mul_right_inj'` have hexp : exp β„š - 1 β‰  0, { simp only [exp, power_series.ext_iff, ne, not_forall], use 1, simp }, have h_r : exp β„š ^ n - 1 = X * mk (Ξ» p, coeff β„š (p + 1) (exp β„š ^ n)), { have h_const : C β„š (constant_coeff β„š (exp β„š ^ n)) = 1 := by simp, rw [← h_const, sub_const_eq_X_mul_shift] }, -- key step: a chain of equalities of power series rw [← mul_right_inj' hexp, mul_comm, ← exp_pow_sum, geom_sum_mul, h_r, ← bernoulli_power_series_mul_exp_sub_one, bernoulli_power_series, mul_right_comm], simp [h_cauchy, mul_comm] }, -- massage `hps` into our goal rw [hps, sum_mul], refine sum_congr rfl (Ξ» x hx, _), field_simp [mul_right_comm _ ↑p!, ← mul_assoc _ _ ↑p!, cast_add_one_ne_zero, hne], end /-- Alternate form of **Faulhaber's theorem**, relating the sum of p-th powers to the Bernoulli numbers: $$\sum_{k=1}^{n} k^p = \sum_{i=0}^p (-1)^iB_i\binom{p+1}{i}\frac{n^{p+1-i}}{p+1}.$$ Deduced from `sum_range_pow`. -/ theorem sum_Ico_pow (n p : β„•) : βˆ‘ k in Ico 1 (n + 1), (k : β„š) ^ p = βˆ‘ i in range (p + 1), bernoulli' i * (p + 1).choose i * n ^ (p + 1 - i) / (p + 1) := begin rw ← nat.cast_succ, -- dispose of the trivial case cases p, { simp }, let f := Ξ» i, bernoulli i * p.succ.succ.choose i * n ^ (p.succ.succ - i) / p.succ.succ, let f' := Ξ» i, bernoulli' i * p.succ.succ.choose i * n ^ (p.succ.succ - i) / p.succ.succ, suffices : βˆ‘ k in Ico 1 n.succ, ↑k ^ p.succ = βˆ‘ i in range p.succ.succ, f' i, { convert this }, -- prove some algebraic facts that will make things easier for us later on have hle := nat.le_add_left 1 n, have hne : (p + 1 + 1 : β„š) β‰  0 := by exact_mod_cast succ_ne_zero p.succ, have h1 : βˆ€ r : β„š, r * (p + 1 + 1) * n ^ p.succ / (p + 1 + 1 : β„š) = r * n ^ p.succ := Ξ» r, by rw [mul_div_right_comm, mul_div_cancel _ hne], have h2 : f 1 + n ^ p.succ = 1 / 2 * n ^ p.succ, { simp_rw [f, bernoulli_one, choose_one_right, succ_sub_succ_eq_sub, cast_succ, tsub_zero, h1], ring }, have : βˆ‘ i in range p, bernoulli (i + 2) * (p + 2).choose (i + 2) * n ^ (p - i) / ↑(p + 2) = βˆ‘ i in range p, bernoulli' (i + 2) * (p + 2).choose (i + 2) * n ^ (p - i) / ↑(p + 2) := sum_congr rfl (Ξ» i h, by rw bernoulli_eq_bernoulli'_of_ne_one (succ_succ_ne_one i)), calc βˆ‘ k in Ico 1 n.succ, ↑k ^ p.succ -- replace sum over `Ico` with sum over `range` and simplify = βˆ‘ k in range n.succ, ↑k ^ p.succ : by simp [sum_Ico_eq_sub _ hle, succ_ne_zero] -- extract the last term of the sum ... = βˆ‘ k in range n, (k : β„š) ^ p.succ + n ^ p.succ : by rw sum_range_succ -- apply the key lemma, `sum_range_pow` ... = βˆ‘ i in range p.succ.succ, f i + n ^ p.succ : by simp [f, sum_range_pow] -- extract the first two terms of the sum ... = βˆ‘ i in range p, f i.succ.succ + f 1 + f 0 + n ^ p.succ : by simp_rw [sum_range_succ'] ... = βˆ‘ i in range p, f i.succ.succ + (f 1 + n ^ p.succ) + f 0 : by ring ... = βˆ‘ i in range p, f i.succ.succ + 1 / 2 * n ^ p.succ + f 0 : by rw h2 -- convert from `bernoulli` to `bernoulli'` ... = βˆ‘ i in range p, f' i.succ.succ + f' 1 + f' 0 : by { simp only [f, f'], simpa [h1, Ξ» i, show i + 2 = i + 1 + 1, from rfl] } -- rejoin the first two terms of the sum ... = βˆ‘ i in range p.succ.succ, f' i : by simp_rw [sum_range_succ'], end end faulhaber
4bf63422d54d60485be0d70bf0abc003c547f1c6
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/data/finset/basic.lean
dcb70dde64e5f7f96de1c5a38088d0d23f923587
[ "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
132,054
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro -/ import data.multiset.finset_ops import tactic.apply import tactic.monotonicity import tactic.nth_rewrite /-! # Finite sets Terms of type `finset Ξ±` are one way of talking about finite subsets of `Ξ±` in mathlib. Below, `finset Ξ±` is defined as a structure with 2 fields: 1. `val` is a `multiset Ξ±` of elements; 2. `nodup` is a proof that `val` has no duplicates. Finsets in Lean are constructive in that they have an underlying `list` that enumerates their elements. In particular, any function that uses the data of the underlying list cannot depend on its ordering. This is handled on the `multiset` level by multiset API, so in most cases one needn't worry about it explicitly. Finsets give a basic foundation for defining finite sums and products over types: 1. `βˆ‘ i in (s : finset Ξ±), f i`; 2. `∏ i in (s : finset Ξ±), f i`. Lean refers to these operations as `big_operator`s. More information can be found in `algebra.big_operators.basic`. Finsets are directly used to define fintypes in Lean. A `fintype Ξ±` instance for a type `Ξ±` consists of a universal `finset Ξ±` containing every term of `Ξ±`, called `univ`. See `data.fintype.basic`. There is also `univ'`, the noncomputable partner to `univ`, which is defined to be `Ξ±` as a finset if `Ξ±` is finite, and the empty finset otherwise. See `data.fintype.basic`. ## Main declarations ### Main definitions * `finset`: Defines a type for the finite subsets of `Ξ±`. Constructing a `finset` requires two pieces of data: `val`, a `multiset Ξ±` of elements, and `nodup`, a proof that `val` has no duplicates. * `finset.has_mem`: Defines membership `a ∈ (s : finset Ξ±)`. * `finset.has_coe`: Provides a coercion `s : finset Ξ±` to `s : set Ξ±`. * `finset.has_coe_to_sort`: Coerce `s : finset Ξ±` to the type of all `x ∈ s`. * `finset.induction_on`: Induction on finsets. To prove a proposition about an arbitrary `finset Ξ±`, it suffices to prove it for the empty finset, and to show that if it holds for some `finset Ξ±`, then it holds for the finset obtained by inserting a new element. * `finset.choose`: Given a proof `h` of existence and uniqueness of a certain element satisfying a predicate, `choose s h` returns the element of `s` satisfying that predicate. * `finset.card`: `card s : β„•` returns the cardinalilty of `s : finset Ξ±`. The API for `card`'s interaction with operations on finsets is extensive. TODO: The noncomputable sister `fincard` is about to be added into mathlib. ### Finset constructions * `singleton`: Denoted by `{a}`; the finset consisting of one element. * `finset.empty`: Denoted by `βˆ…`. The finset associated to any type consisting of no elements. * `finset.range`: For any `n : β„•`, `range n` is equal to `{0, 1, ... , n - 1} βŠ† β„•`. This convention is consistent with other languages and normalizes `card (range n) = n`. Beware, `n` is not in `range n`. * `finset.diag`: Given `s`, `diag s` is the set of pairs `(a, a)` with `a ∈ s`. See also `finset.off_diag`: Given a finite set `s`, the off-diagonal, `s.off_diag` is the set of pairs `(a, b)` with `a β‰  b` for `a, b ∈ s`. * `finset.attach`: Given `s : finset Ξ±`, `attach s` forms a finset of elements of the subtype `{a // a ∈ s}`; in other words, it attaches elements to a proof of membership in the set. ### Finsets from functions * `finset.image`: Given a function `f : Ξ± β†’ Ξ²`, `s.image f` is the image finset in `Ξ²`. * `finset.map`: Given an embedding `f : Ξ± β†ͺ Ξ²`, `s.map f` is the image finset in `Ξ²`. * `finset.filter`: Given a predicate `p : Ξ± β†’ Prop`, `s.filter p` is the finset consisting of those elements in `s` satisfying the predicate `p`. ### The lattice structure on subsets of finsets There is a natural lattice structure on the subsets of a set. In Lean, we use lattice notation to talk about things involving unions and intersections. See `order.lattice`. For the lattice structure on finsets, `βŠ₯` is called `bot` with `βŠ₯ = βˆ…` and `⊀` is called `top` with `⊀ = univ`. * `finset.subset`: Lots of API about lattices, otherwise behaves exactly as one would expect. * `finset.union`: Defines `s βˆͺ t` (or `s βŠ” t`) as the union of `s` and `t`. See `finset.bUnion` for finite unions. * `finset.inter`: Defines `s ∩ t` (or `s βŠ“ t`) as the intersection of `s` and `t`. TODO: `finset.bInter` for finite intersections. * `finset.disj_union`: Given a hypothesis `h` which states that finsets `s` and `t` are disjoint, `s.disj_union t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`; this does not require decidable equality on the type `Ξ±`. ### Operations on two or more finsets * `finset.insert` and `finset.cons`: For any `a : Ξ±`, `insert s a` returns `s βˆͺ {a}`. `cons s a h` returns the same except that it requires a hypothesis stating that `a` is not already in `s`. This does not require decidable equality on the type `Ξ±`. * `finset.union`: see "The lattice structure on subsets of finsets" * `finset.inter`: see "The lattice structure on subsets of finsets" * `finset.erase`: For any `a : Ξ±`, `erase s a` returns `s` with the element `a` removed. * `finset.sdiff`: Defines the set difference `s \ t` for finsets `s` and `t`. * `finset.prod`: Given finsets of `Ξ±` and `Ξ²`, defines finsets of `Ξ± Γ— Ξ²`. For arbitrary dependent products, see `data.finset.pi`. * `finset.sigma`: Given finsets of `Ξ±` and `Ξ²`, defines finsets of the dependent sum type `Ξ£ Ξ±, Ξ²` * `finset.bUnion`: Finite unions of finsets; given an indexing function `f : Ξ± β†’ finset Ξ²` and a `s : finset Ξ±`, `s.bUnion f` is the union of all finsets of the form `f a` for `a ∈ s`. * `finset.bInter`: TODO: Implemement finite intersections. ### Maps constructed using finsets * `finset.piecewise`: Given two functions `f`, `g`, `s.piecewise f g` is a function which is equal to `f` on `s` and `g` on the complement. ### Predicates on finsets * `disjoint`: defined via the lattice structure on finsets; two sets are disjoint if their intersection is empty. * `finset.nonempty`: A finset is nonempty if it has elements. This is equivalent to saying `s β‰  βˆ…`. TODO: Decide on the simp normal form. ### Equivalences between finsets * The `data.equiv` files describe a general type of equivalence, so look in there for any lemmas. There is some API for rewriting sums and products from `s` to `t` given that `s ≃ t`. TODO: examples ## Tags finite sets, finset -/ open multiset subtype nat function universes u variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} /-- `finset Ξ±` is the type of finite sets of elements of `Ξ±`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (Ξ± : Type*) := (val : multiset Ξ±) (nodup : nodup val) namespace finset theorem eq_of_veq : βˆ€ {s t : finset Ξ±}, s.1 = t.1 β†’ s = t | ⟨s, _⟩ ⟨t, _⟩ rfl := rfl @[simp] theorem val_inj {s t : finset Ξ±} : s.1 = t.1 ↔ s = t := ⟨eq_of_veq, congr_arg _⟩ @[simp] theorem erase_dup_eq_self [decidable_eq Ξ±] (s : finset Ξ±) : erase_dup s.1 = s.1 := s.2.erase_dup instance has_decidable_eq [decidable_eq Ξ±] : decidable_eq (finset Ξ±) | s₁ sβ‚‚ := decidable_of_iff _ val_inj /-! ### membership -/ instance : has_mem Ξ± (finset Ξ±) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : Ξ±} {s : finset Ξ±} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] theorem mem_mk {a : Ξ±} {s nd} : a ∈ @finset.mk Ξ± s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) : decidable (a ∈ s) := multiset.decidable_mem _ _ /-! ### set coercion -/ /-- Convert a finset to a set in the natural way. -/ instance : has_coe_t (finset Ξ±) (set Ξ±) := ⟨λ s, {x | x ∈ s}⟩ @[simp, norm_cast] lemma mem_coe {a : Ξ±} {s : finset Ξ±} : a ∈ (s : set Ξ±) ↔ a ∈ s := iff.rfl @[simp] lemma set_of_mem {Ξ±} {s : finset Ξ±} : {a | a ∈ s} = s := rfl @[simp] lemma coe_mem {s : finset Ξ±} (x : (s : set Ξ±)) : ↑x ∈ s := x.2 @[simp] lemma mk_coe {s : finset Ξ±} (x : (s : set Ξ±)) {h} : (⟨x, h⟩ : (s : set Ξ±)) = x := subtype.coe_eta _ _ instance decidable_mem' [decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) : decidable (a ∈ (s : set Ξ±)) := s.decidable_mem _ /-! ### extensionality -/ theorem ext_iff {s₁ sβ‚‚ : finset Ξ±} : s₁ = sβ‚‚ ↔ βˆ€ a, a ∈ s₁ ↔ a ∈ sβ‚‚ := val_inj.symm.trans $ nodup_ext s₁.2 sβ‚‚.2 @[ext] theorem ext {s₁ sβ‚‚ : finset Ξ±} : (βˆ€ a, a ∈ s₁ ↔ a ∈ sβ‚‚) β†’ s₁ = sβ‚‚ := ext_iff.2 @[simp, norm_cast] theorem coe_inj {s₁ sβ‚‚ : finset Ξ±} : (s₁ : set Ξ±) = sβ‚‚ ↔ s₁ = sβ‚‚ := set.ext_iff.trans ext_iff.symm lemma coe_injective {Ξ±} : injective (coe : finset Ξ± β†’ set Ξ±) := Ξ» s t, coe_inj.1 /-! ### type coercion -/ /-- Coercion from a finset to the corresponding subtype. -/ instance {Ξ± : Type u} : has_coe_to_sort (finset Ξ±) (Type u) := ⟨λ s, {x // x ∈ s}⟩ instance pi_finset_coe.can_lift (ΞΉ : Type*) (Ξ± : Ξ  i : ΞΉ, Type*) [ne : Ξ  i, nonempty (Ξ± i)] (s : finset ΞΉ) : can_lift (Ξ  i : s, Ξ± i) (Ξ  i, Ξ± i) := { coe := Ξ» f i, f i, .. pi_subtype.can_lift ΞΉ Ξ± (∈ s) } instance pi_finset_coe.can_lift' (ΞΉ Ξ± : Type*) [ne : nonempty Ξ±] (s : finset ΞΉ) : can_lift (s β†’ Ξ±) (ΞΉ β†’ Ξ±) := pi_finset_coe.can_lift ΞΉ (Ξ» _, Ξ±) s instance finset_coe.can_lift (s : finset Ξ±) : can_lift Ξ± s := { coe := coe, cond := Ξ» a, a ∈ s, prf := Ξ» a ha, ⟨⟨a, ha⟩, rfl⟩ } @[simp, norm_cast] lemma coe_sort_coe (s : finset Ξ±) : ((s : set Ξ±) : Sort*) = s := rfl /-! ### subset -/ instance : has_subset (finset Ξ±) := ⟨λ s₁ sβ‚‚, βˆ€ ⦃a⦄, a ∈ s₁ β†’ a ∈ sβ‚‚βŸ© theorem subset_def {s₁ sβ‚‚ : finset Ξ±} : s₁ βŠ† sβ‚‚ ↔ s₁.1 βŠ† sβ‚‚.1 := iff.rfl @[simp] theorem subset.refl (s : finset Ξ±) : s βŠ† s := subset.refl _ theorem subset_of_eq {s t : finset Ξ±} (h : s = t) : s βŠ† t := h β–Έ subset.refl _ theorem subset.trans {s₁ sβ‚‚ s₃ : finset Ξ±} : s₁ βŠ† sβ‚‚ β†’ sβ‚‚ βŠ† s₃ β†’ s₁ βŠ† s₃ := subset.trans theorem superset.trans {s₁ sβ‚‚ s₃ : finset Ξ±} : s₁ βŠ‡ sβ‚‚ β†’ sβ‚‚ βŠ‡ s₃ β†’ s₁ βŠ‡ s₃ := Ξ» h' h, subset.trans h h' -- TODO: these should be global attributes, but this will require fixing other files local attribute [trans] subset.trans superset.trans theorem mem_of_subset {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} : s₁ βŠ† sβ‚‚ β†’ a ∈ s₁ β†’ a ∈ sβ‚‚ := mem_of_subset theorem subset.antisymm {s₁ sβ‚‚ : finset Ξ±} (H₁ : s₁ βŠ† sβ‚‚) (Hβ‚‚ : sβ‚‚ βŠ† s₁) : s₁ = sβ‚‚ := ext $ Ξ» a, ⟨@H₁ a, @Hβ‚‚ a⟩ theorem subset_iff {s₁ sβ‚‚ : finset Ξ±} : s₁ βŠ† sβ‚‚ ↔ βˆ€ ⦃x⦄, x ∈ s₁ β†’ x ∈ sβ‚‚ := iff.rfl @[simp, norm_cast] theorem coe_subset {s₁ sβ‚‚ : finset Ξ±} : (s₁ : set Ξ±) βŠ† sβ‚‚ ↔ s₁ βŠ† sβ‚‚ := iff.rfl @[simp] theorem val_le_iff {s₁ sβ‚‚ : finset Ξ±} : s₁.1 ≀ sβ‚‚.1 ↔ s₁ βŠ† sβ‚‚ := le_iff_subset s₁.2 instance : has_ssubset (finset Ξ±) := ⟨λa b, a βŠ† b ∧ Β¬ b βŠ† a⟩ instance : partial_order (finset Ξ±) := { le := (βŠ†), lt := (βŠ‚), le_refl := subset.refl, le_trans := @subset.trans _, le_antisymm := @subset.antisymm _ } /-- Coercion to `set Ξ±` as an `order_embedding`. -/ def coe_emb : finset Ξ± β†ͺo set Ξ± := ⟨⟨coe, coe_injective⟩, Ξ» s t, coe_subset⟩ @[simp] lemma coe_coe_emb : ⇑(coe_emb : finset Ξ± β†ͺo set Ξ±) = coe := rfl theorem subset.antisymm_iff {s₁ sβ‚‚ : finset Ξ±} : s₁ = sβ‚‚ ↔ s₁ βŠ† sβ‚‚ ∧ sβ‚‚ βŠ† s₁ := le_antisymm_iff theorem not_subset (s t : finset Ξ±) : Β¬(s βŠ† t) ↔ βˆƒ x ∈ s, Β¬(x ∈ t) := by simp only [←finset.coe_subset, set.not_subset, exists_prop, finset.mem_coe] @[simp] theorem le_eq_subset : ((≀) : finset Ξ± β†’ finset Ξ± β†’ Prop) = (βŠ†) := rfl @[simp] theorem lt_eq_subset : ((<) : finset Ξ± β†’ finset Ξ± β†’ Prop) = (βŠ‚) := rfl theorem le_iff_subset {s₁ sβ‚‚ : finset Ξ±} : s₁ ≀ sβ‚‚ ↔ s₁ βŠ† sβ‚‚ := iff.rfl theorem lt_iff_ssubset {s₁ sβ‚‚ : finset Ξ±} : s₁ < sβ‚‚ ↔ s₁ βŠ‚ sβ‚‚ := iff.rfl @[simp, norm_cast] lemma coe_ssubset {s₁ sβ‚‚ : finset Ξ±} : (s₁ : set Ξ±) βŠ‚ sβ‚‚ ↔ s₁ βŠ‚ sβ‚‚ := show (s₁ : set Ξ±) βŠ‚ sβ‚‚ ↔ s₁ βŠ† sβ‚‚ ∧ Β¬sβ‚‚ βŠ† s₁, by simp only [set.ssubset_def, finset.coe_subset] @[simp] theorem val_lt_iff {s₁ sβ‚‚ : finset Ξ±} : s₁.1 < sβ‚‚.1 ↔ s₁ βŠ‚ sβ‚‚ := and_congr val_le_iff $ not_congr val_le_iff lemma ssubset_iff_subset_ne {s t : finset Ξ±} : s βŠ‚ t ↔ s βŠ† t ∧ s β‰  t := @lt_iff_le_and_ne _ _ s t theorem ssubset_iff_of_subset {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : s₁ βŠ‚ sβ‚‚ ↔ βˆƒ x ∈ sβ‚‚, x βˆ‰ s₁ := set.ssubset_iff_of_subset h lemma ssubset_of_ssubset_of_subset {s₁ sβ‚‚ s₃ : finset Ξ±} (hs₁sβ‚‚ : s₁ βŠ‚ sβ‚‚) (hsβ‚‚s₃ : sβ‚‚ βŠ† s₃) : s₁ βŠ‚ s₃ := set.ssubset_of_ssubset_of_subset hs₁sβ‚‚ hsβ‚‚s₃ lemma ssubset_of_subset_of_ssubset {s₁ sβ‚‚ s₃ : finset Ξ±} (hs₁sβ‚‚ : s₁ βŠ† sβ‚‚) (hsβ‚‚s₃ : sβ‚‚ βŠ‚ s₃) : s₁ βŠ‚ s₃ := set.ssubset_of_subset_of_ssubset hs₁sβ‚‚ hsβ‚‚s₃ lemma exists_of_ssubset {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ‚ sβ‚‚) : βˆƒ x ∈ sβ‚‚, x βˆ‰ s₁ := set.exists_of_ssubset h /-! ### Nonempty -/ /-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used in theorem assumptions instead of `βˆƒ x, x ∈ s` or `s β‰  βˆ…` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : finset Ξ±) : Prop := βˆƒ x:Ξ±, x ∈ s @[simp, norm_cast] lemma coe_nonempty {s : finset Ξ±} : (s:set Ξ±).nonempty ↔ s.nonempty := iff.rfl @[simp] lemma nonempty_coe_sort (s : finset Ξ±) : nonempty β†₯s ↔ s.nonempty := nonempty_subtype alias coe_nonempty ↔ _ finset.nonempty.to_set lemma nonempty.bex {s : finset Ξ±} (h : s.nonempty) : βˆƒ x:Ξ±, x ∈ s := h lemma nonempty.mono {s t : finset Ξ±} (hst : s βŠ† t) (hs : s.nonempty) : t.nonempty := set.nonempty.mono hst hs lemma nonempty.forall_const {s : finset Ξ±} (h : s.nonempty) {p : Prop} : (βˆ€ x ∈ s, p) ↔ p := let ⟨x, hx⟩ := h in ⟨λ h, h x hx, Ξ» h x hx, h⟩ /-! ### empty -/ /-- The empty finset -/ protected def empty : finset Ξ± := ⟨0, nodup_zero⟩ instance : has_emptyc (finset Ξ±) := ⟨finset.empty⟩ instance inhabited_finset : inhabited (finset Ξ±) := βŸ¨βˆ…βŸ© @[simp] theorem empty_val : (βˆ… : finset Ξ±).1 = 0 := rfl @[simp] theorem not_mem_empty (a : Ξ±) : a βˆ‰ (βˆ… : finset Ξ±) := id @[simp] theorem not_nonempty_empty : Β¬(βˆ… : finset Ξ±).nonempty := Ξ» ⟨x, hx⟩, not_mem_empty x hx @[simp] theorem mk_zero : (⟨0, nodup_zero⟩ : finset Ξ±) = βˆ… := rfl theorem ne_empty_of_mem {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : s β‰  βˆ… := Ξ» e, not_mem_empty a $ e β–Έ h theorem nonempty.ne_empty {s : finset Ξ±} (h : s.nonempty) : s β‰  βˆ… := exists.elim h $ Ξ» a, ne_empty_of_mem @[simp] theorem empty_subset (s : finset Ξ±) : βˆ… βŠ† s := zero_subset _ theorem eq_empty_of_forall_not_mem {s : finset Ξ±} (H : βˆ€x, x βˆ‰ s) : s = βˆ… := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset Ξ±} : s = βˆ… ↔ βˆ€ x, x βˆ‰ s := ⟨by rintro rfl x; exact id, Ξ» h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset Ξ±} : s.1 = 0 ↔ s = βˆ… := @val_inj _ s βˆ… theorem subset_empty {s : finset Ξ±} : s βŠ† βˆ… ↔ s = βˆ… := subset_zero.trans val_eq_zero @[simp] lemma not_ssubset_empty (s : finset Ξ±) : Β¬s βŠ‚ βˆ… := Ξ» h, let ⟨x, he, hs⟩ := exists_of_ssubset h in he theorem nonempty_of_ne_empty {s : finset Ξ±} (h : s β‰  βˆ…) : s.nonempty := exists_mem_of_ne_zero (mt val_eq_zero.1 h) theorem nonempty_iff_ne_empty {s : finset Ξ±} : s.nonempty ↔ s β‰  βˆ… := ⟨nonempty.ne_empty, nonempty_of_ne_empty⟩ @[simp] theorem not_nonempty_iff_eq_empty {s : finset Ξ±} : Β¬s.nonempty ↔ s = βˆ… := by { rw nonempty_iff_ne_empty, exact not_not, } theorem eq_empty_or_nonempty (s : finset Ξ±) : s = βˆ… ∨ s.nonempty := classical.by_cases or.inl (Ξ» h, or.inr (nonempty_of_ne_empty h)) @[simp, norm_cast] lemma coe_empty : ((βˆ… : finset Ξ±) : set Ξ±) = βˆ… := rfl @[simp, norm_cast] lemma coe_eq_empty {s : finset Ξ±} : (s : set Ξ±) = βˆ… ↔ s = βˆ… := by rw [← coe_empty, coe_inj] /-- A `finset` for an empty type is empty. -/ lemma eq_empty_of_is_empty [is_empty Ξ±] (s : finset Ξ±) : s = βˆ… := finset.eq_empty_of_forall_not_mem is_empty_elim /-! ### singleton -/ /-- `{a} : finset a` is the set `{a}` containing `a` and nothing else. This differs from `insert a βˆ…` in that it does not require a `decidable_eq` instance for `Ξ±`. -/ instance : has_singleton Ξ± (finset Ξ±) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩ @[simp] theorem singleton_val (a : Ξ±) : ({a} : finset Ξ±).1 = {a} := rfl @[simp] theorem mem_singleton {a b : Ξ±} : b ∈ ({a} : finset Ξ±) ↔ b = a := mem_singleton theorem not_mem_singleton {a b : Ξ±} : a βˆ‰ ({b} : finset Ξ±) ↔ a β‰  b := not_congr mem_singleton theorem mem_singleton_self (a : Ξ±) : a ∈ ({a} : finset Ξ±) := or.inl rfl lemma singleton_injective : injective (singleton : Ξ± β†’ finset Ξ±) := Ξ» a b h, mem_singleton.1 (h β–Έ mem_singleton_self _) theorem singleton_inj {a b : Ξ±} : ({a} : finset Ξ±) = {b} ↔ a = b := singleton_injective.eq_iff @[simp] theorem singleton_nonempty (a : Ξ±) : ({a} : finset Ξ±).nonempty := ⟨a, mem_singleton_self a⟩ @[simp] theorem singleton_ne_empty (a : Ξ±) : ({a} : finset Ξ±) β‰  βˆ… := (singleton_nonempty a).ne_empty @[simp, norm_cast] lemma coe_singleton (a : Ξ±) : (({a} : finset Ξ±) : set Ξ±) = {a} := by { ext, simp } @[simp, norm_cast] lemma coe_eq_singleton {Ξ± : Type*} {s : finset Ξ±} {a : Ξ±} : (s : set Ξ±) = {a} ↔ s = {a} := by rw [←finset.coe_singleton, finset.coe_inj] lemma eq_singleton_iff_unique_mem {s : finset Ξ±} {a : Ξ±} : s = {a} ↔ a ∈ s ∧ βˆ€ x ∈ s, x = a := begin split; intro t, rw t, refine ⟨finset.mem_singleton_self _, Ξ» _, finset.mem_singleton.1⟩, ext, rw finset.mem_singleton, refine ⟨t.right _, Ξ» r, r.symm β–Έ t.left⟩ end lemma eq_singleton_iff_nonempty_unique_mem {s : finset Ξ±} {a : Ξ±} : s = {a} ↔ s.nonempty ∧ βˆ€ x ∈ s, x = a := begin split, { intros h, subst h, simp, }, { rintros ⟨hne, h_uniq⟩, rw eq_singleton_iff_unique_mem, refine ⟨_, h_uniq⟩, rw ← h_uniq hne.some hne.some_spec, apply hne.some_spec, }, end lemma singleton_iff_unique_mem (s : finset Ξ±) : (βˆƒ a, s = {a}) ↔ βˆƒ! a, a ∈ s := by simp only [eq_singleton_iff_unique_mem, exists_unique] lemma singleton_subset_set_iff {s : set Ξ±} {a : Ξ±} : ↑({a} : finset Ξ±) βŠ† s ↔ a ∈ s := by rw [coe_singleton, set.singleton_subset_iff] @[simp] lemma singleton_subset_iff {s : finset Ξ±} {a : Ξ±} : {a} βŠ† s ↔ a ∈ s := singleton_subset_set_iff @[simp] lemma subset_singleton_iff {s : finset Ξ±} {a : Ξ±} : s βŠ† {a} ↔ s = βˆ… ∨ s = {a} := begin split, { intro hs, apply or.imp_right _ s.eq_empty_or_nonempty, rintro ⟨t, ht⟩, apply subset.antisymm hs, rwa [singleton_subset_iff, ←mem_singleton.1 (hs ht)] }, rintro (rfl | rfl), { exact empty_subset _ }, exact subset.refl _, end @[simp] lemma ssubset_singleton_iff {s : finset Ξ±} {a : Ξ±} : s βŠ‚ {a} ↔ s = βˆ… := by rw [←coe_ssubset, coe_singleton, set.ssubset_singleton_iff, coe_eq_empty] lemma eq_empty_of_ssubset_singleton {s : finset Ξ±} {x : Ξ±} (hs : s βŠ‚ {x}) : s = βˆ… := ssubset_singleton_iff.1 hs /-! ### cons -/ /-- `cons a s h` is the set `{a} βˆͺ s` containing `a` and the elements of `s`. It is the same as `insert a s` when it is defined, but unlike `insert a s` it does not require `decidable_eq Ξ±`, and the union is guaranteed to be disjoint. -/ def cons {Ξ±} (a : Ξ±) (s : finset Ξ±) (h : a βˆ‰ s) : finset Ξ± := ⟨a ::β‚˜ s.1, multiset.nodup_cons.2 ⟨h, s.2⟩⟩ @[simp] theorem mem_cons {a s h b} : b ∈ @cons Ξ± a s h ↔ b = a ∨ b ∈ s := by rcases s with ⟨⟨s⟩⟩; apply list.mem_cons_iff @[simp] theorem cons_val {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : (cons a s h).1 = a ::β‚˜ s.1 := rfl @[simp] theorem mk_cons {a : Ξ±} {s : multiset Ξ±} (h : (a ::β‚˜ s).nodup) : (⟨a ::β‚˜ s, h⟩ : finset Ξ±) = cons a ⟨s, (multiset.nodup_cons.1 h).2⟩ (multiset.nodup_cons.1 h).1 := rfl @[simp] theorem nonempty_cons {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : (cons a s h).nonempty := ⟨a, mem_cons.2 (or.inl rfl)⟩ @[simp] lemma nonempty_mk_coe : βˆ€ {l : list Ξ±} {hl}, (βŸ¨β†‘l, hl⟩ : finset Ξ±).nonempty ↔ l β‰  [] | [] hl := by simp | (a::l) hl := by simp [← multiset.cons_coe] @[simp] lemma coe_cons {a s h} : (@cons Ξ± a s h : set Ξ±) = insert a s := by { ext, simp } @[simp] lemma cons_subset_cons {a s hs t ht} : @cons Ξ± a s hs βŠ† cons a t ht ↔ s βŠ† t := by rwa [← coe_subset, coe_cons, coe_cons, set.insert_subset_insert_iff, coe_subset] /-! ### disjoint union -/ /-- `disj_union s t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`. It is the same as `s βˆͺ t`, but it does not require decidable equality on the type. The hypothesis ensures that the sets are disjoint. -/ def disj_union {Ξ±} (s t : finset Ξ±) (h : βˆ€ a ∈ s, a βˆ‰ t) : finset Ξ± := ⟨s.1 + t.1, multiset.nodup_add.2 ⟨s.2, t.2, h⟩⟩ @[simp] theorem mem_disj_union {Ξ± s t h a} : a ∈ @disj_union Ξ± s t h ↔ a ∈ s ∨ a ∈ t := by rcases s with ⟨⟨s⟩⟩; rcases t with ⟨⟨t⟩⟩; apply list.mem_append /-! ### insert -/ section decidable_eq variables [decidable_eq Ξ±] /-- `insert a s` is the set `{a} βˆͺ s` containing `a` and the elements of `s`. -/ instance : has_insert Ξ± (finset Ξ±) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩ theorem insert_def (a : Ξ±) (s : finset Ξ±) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl @[simp] theorem insert_val (a : Ξ±) (s : finset Ξ±) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : Ξ±) (s : finset Ξ±) : (insert a s).1 = erase_dup (a ::β‚˜ s.1) := by rw [erase_dup_cons, erase_dup_eq_self]; refl theorem insert_val_of_not_mem {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : (insert a s).1 = a ::β‚˜ s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] theorem mem_insert {a b : Ξ±} {s : finset Ξ±} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : Ξ±) (s : finset Ξ±) : a ∈ insert a s := mem_ndinsert_self a s.1 theorem mem_insert_of_mem {a b : Ξ±} {s : finset Ξ±} (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h theorem mem_of_mem_insert_of_ne {a b : Ξ±} {s : finset Ξ±} (h : b ∈ insert a s) : b β‰  a β†’ b ∈ s := (mem_insert.1 h).resolve_left @[simp] theorem cons_eq_insert {Ξ±} [decidable_eq Ξ±] (a s h) : @cons Ξ± a s h = insert a s := ext $ Ξ» a, by simp @[simp, norm_cast] lemma coe_insert (a : Ξ±) (s : finset Ξ±) : ↑(insert a s) = (insert a s : set Ξ±) := set.ext $ Ξ» x, by simp only [mem_coe, mem_insert, set.mem_insert_iff] lemma mem_insert_coe {s : finset Ξ±} {x y : Ξ±} : x ∈ insert y s ↔ x ∈ insert y (s : set Ξ±) := by simp instance : is_lawful_singleton Ξ± (finset Ξ±) := ⟨λ a, by { ext, simp }⟩ @[simp] theorem insert_eq_of_mem {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] theorem insert_singleton_self_eq (a : Ξ±) : ({a, a} : finset Ξ±) = {a} := insert_eq_of_mem $ mem_singleton_self _ theorem insert.comm (a b : Ξ±) (s : finset Ξ±) : insert a (insert b s) = insert b (insert a s) := ext $ Ξ» x, by simp only [mem_insert, or.left_comm] theorem insert_singleton_comm (a b : Ξ±) : ({a, b} : finset Ξ±) = {b, a} := begin ext, simp [or.comm] end @[simp] theorem insert_idem (a : Ξ±) (s : finset Ξ±) : insert a (insert a s) = insert a s := ext $ Ξ» x, by simp only [mem_insert, or.assoc.symm, or_self] @[simp] theorem insert_nonempty (a : Ξ±) (s : finset Ξ±) : (insert a s).nonempty := ⟨a, mem_insert_self a s⟩ @[simp] theorem insert_ne_empty (a : Ξ±) (s : finset Ξ±) : insert a s β‰  βˆ… := (insert_nonempty a s).ne_empty section /-! The universe annotation is required for the following instance, possibly this is a bug in Lean. See leanprover.zulipchat.com/#narrow/stream/113488-general/topic/strange.20error.20(universe.20issue.3F) -/ instance {Ξ± : Type u} [decidable_eq Ξ±] (i : Ξ±) (s : finset Ξ±) : nonempty.{u + 1} ((insert i s : finset Ξ±) : set Ξ±) := (finset.coe_nonempty.mpr (s.insert_nonempty i)).to_subtype end lemma ne_insert_of_not_mem (s t : finset Ξ±) {a : Ξ±} (h : a βˆ‰ s) : s β‰  insert a t := by { contrapose! h, simp [h] } theorem insert_subset {a : Ξ±} {s t : finset Ξ±} : insert a s βŠ† t ↔ a ∈ t ∧ s βŠ† t := by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib] theorem subset_insert (a : Ξ±) (s : finset Ξ±) : s βŠ† insert a s := Ξ» b, mem_insert_of_mem theorem insert_subset_insert (a : Ξ±) {s t : finset Ξ±} (h : s βŠ† t) : insert a s βŠ† insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma ssubset_iff {s t : finset Ξ±} : s βŠ‚ t ↔ (βˆƒa βˆ‰ s, insert a s βŠ† t) := by exact_mod_cast @set.ssubset_iff_insert Ξ± s t lemma ssubset_insert {s : finset Ξ±} {a : Ξ±} (h : a βˆ‰ s) : s βŠ‚ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[elab_as_eliminator] lemma cons_induction {Ξ± : Type*} {p : finset Ξ± β†’ Prop} (h₁ : p βˆ…) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : finset Ξ±} (h : a βˆ‰ s), p s β†’ p (cons a s h)) : βˆ€ s, p s | ⟨s, nd⟩ := multiset.induction_on s (Ξ» _, h₁) (Ξ» a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : cons a (finset.mk s _) m = ⟨a ::β‚˜ s, nd⟩)], { exact hβ‚‚ (by exact m) (IH nd') }, { rw [cons_val] } end) nd @[elab_as_eliminator] lemma cons_induction_on {Ξ± : Type*} {p : finset Ξ± β†’ Prop} (s : finset Ξ±) (h₁ : p βˆ…) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : finset Ξ±} (h : a βˆ‰ s), p s β†’ p (cons a s h)) : p s := cons_induction h₁ hβ‚‚ s @[elab_as_eliminator] protected theorem induction {Ξ± : Type*} {p : finset Ξ± β†’ Prop} [decidable_eq Ξ±] (h₁ : p βˆ…) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : finset Ξ±}, a βˆ‰ s β†’ p s β†’ p (insert a s)) : βˆ€ s, p s := cons_induction h₁ $ Ξ» a s ha, (s.cons_eq_insert a ha).symm β–Έ hβ‚‚ ha /-- To prove a proposition about an arbitrary `finset Ξ±`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset Ξ±`, then it holds for the `finset` obtained by inserting a new element. -/ @[elab_as_eliminator] protected theorem induction_on {Ξ± : Type*} {p : finset Ξ± β†’ Prop} [decidable_eq Ξ±] (s : finset Ξ±) (h₁ : p βˆ…) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : finset Ξ±}, a βˆ‰ s β†’ p s β†’ p (insert a s)) : p s := finset.induction h₁ hβ‚‚ s /-- To prove a proposition about `S : finset Ξ±`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset Ξ± βŠ† S`, then it holds for the `finset` obtained by inserting a new element of `S`. -/ @[elab_as_eliminator] theorem induction_on' {Ξ± : Type*} {p : finset Ξ± β†’ Prop} [decidable_eq Ξ±] (S : finset Ξ±) (h₁ : p βˆ…) (hβ‚‚ : βˆ€ {a s}, a ∈ S β†’ s βŠ† S β†’ a βˆ‰ s β†’ p s β†’ p (insert a s)) : p S := @finset.induction_on Ξ± (Ξ» T, T βŠ† S β†’ p T) _ S (Ξ» _, h₁) (Ξ» a s has hqs hs, let ⟨hS, sS⟩ := finset.insert_subset.1 hs in hβ‚‚ hS sS has (hqs sS)) (finset.subset.refl S) /-- To prove a proposition about a nonempty `s : finset Ξ±`, it suffices to show it holds for all singletons and that if it holds for `t : finset Ξ±`, then it also holds for the `finset` obtained by inserting an element in `t`. -/ @[elab_as_eliminator] lemma nonempty.cons_induction {Ξ± : Type*} {s : finset Ξ±} (hs : s.nonempty) {p : finset Ξ± β†’ Prop} (hβ‚€ : βˆ€ a, p {a}) (h₁ : βˆ€ ⦃a⦄ s (h : a βˆ‰ s), p s β†’ p (finset.cons a s h)) : p s := begin induction s using finset.cons_induction with a t ha h, { exact (not_nonempty_empty hs).elim, }, obtain rfl | ht := t.eq_empty_or_nonempty, { exact hβ‚€ a }, { exact h₁ t ha (h ht) } end /-- Inserting an element to a finite set is equivalent to the option type. -/ def subtype_insert_equiv_option {t : finset Ξ±} {x : Ξ±} (h : x βˆ‰ t) : {i // i ∈ insert x t} ≃ option {i // i ∈ t} := begin refine { to_fun := Ξ» y, if h : ↑y = x then none else some ⟨y, (mem_insert.mp y.2).resolve_left h⟩, inv_fun := Ξ» y, y.elim ⟨x, mem_insert_self _ _⟩ $ Ξ» z, ⟨z, mem_insert_of_mem z.2⟩, .. }, { intro y, by_cases h : ↑y = x, simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk], simp only [h, option.elim, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, { rintro (_|y), simp only [option.elim, dif_pos, subtype.coe_mk], have : ↑y β‰  x, { rintro ⟨⟩, exact h y.2 }, simp only [this, option.elim, subtype.eta, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, end /-! ### union -/ /-- `s βˆͺ t` is the set such that `a ∈ s βˆͺ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset Ξ±) := ⟨λ s₁ sβ‚‚, ⟨_, nodup_ndunion s₁.1 sβ‚‚.2⟩⟩ theorem union_val_nd (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).1 = ndunion s₁.1 sβ‚‚.1 := rfl @[simp] theorem union_val (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).1 = s₁.1 βˆͺ sβ‚‚.1 := ndunion_eq_union s₁.2 @[simp] theorem mem_union {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a ∈ s₁ βˆͺ sβ‚‚ ↔ a ∈ s₁ ∨ a ∈ sβ‚‚ := mem_ndunion @[simp] theorem disj_union_eq_union {Ξ±} [decidable_eq Ξ±] (s t h) : @disj_union Ξ± s t h = s βˆͺ t := ext $ Ξ» a, by simp theorem mem_union_left {a : Ξ±} {s₁ : finset Ξ±} (sβ‚‚ : finset Ξ±) (h : a ∈ s₁) : a ∈ s₁ βˆͺ sβ‚‚ := mem_union.2 $ or.inl h theorem mem_union_right {a : Ξ±} {sβ‚‚ : finset Ξ±} (s₁ : finset Ξ±) (h : a ∈ sβ‚‚) : a ∈ s₁ βˆͺ sβ‚‚ := mem_union.2 $ or.inr h theorem forall_mem_union {s₁ sβ‚‚ : finset Ξ±} {p : Ξ± β†’ Prop} : (βˆ€ ab ∈ (s₁ βˆͺ sβ‚‚), p ab) ↔ (βˆ€ a ∈ s₁, p a) ∧ (βˆ€ b ∈ sβ‚‚, p b) := ⟨λ h, ⟨λ a, h a ∘ mem_union_left _, Ξ» b, h b ∘ mem_union_right _⟩, Ξ» h ab hab, (mem_union.mp hab).elim (h.1 _) (h.2 _)⟩ theorem not_mem_union {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a βˆ‰ s₁ βˆͺ sβ‚‚ ↔ a βˆ‰ s₁ ∧ a βˆ‰ sβ‚‚ := by rw [mem_union, not_or_distrib] @[simp, norm_cast] lemma coe_union (s₁ sβ‚‚ : finset Ξ±) : ↑(s₁ βˆͺ sβ‚‚) = (s₁ βˆͺ sβ‚‚ : set Ξ±) := set.ext $ Ξ» x, mem_union theorem union_subset {s₁ sβ‚‚ s₃ : finset Ξ±} (h₁ : s₁ βŠ† s₃) (hβ‚‚ : sβ‚‚ βŠ† s₃) : s₁ βˆͺ sβ‚‚ βŠ† s₃ := val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 hβ‚‚βŸ©) theorem subset_union_left (s₁ sβ‚‚ : finset Ξ±) : s₁ βŠ† s₁ βˆͺ sβ‚‚ := Ξ» x, mem_union_left _ theorem subset_union_right (s₁ sβ‚‚ : finset Ξ±) : sβ‚‚ βŠ† s₁ βˆͺ sβ‚‚ := Ξ» x, mem_union_right _ lemma union_subset_union {s₁ t₁ sβ‚‚ tβ‚‚ : finset Ξ±} (h₁ : s₁ βŠ† t₁) (hβ‚‚ : sβ‚‚ βŠ† tβ‚‚) : s₁ βˆͺ sβ‚‚ βŠ† t₁ βˆͺ tβ‚‚ := by { intros x hx, rw finset.mem_union at hx ⊒, tauto } theorem union_comm (s₁ sβ‚‚ : finset Ξ±) : s₁ βˆͺ sβ‚‚ = sβ‚‚ βˆͺ s₁ := ext $ Ξ» x, by simp only [mem_union, or_comm] instance : is_commutative (finset Ξ±) (βˆͺ) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = s₁ βˆͺ (sβ‚‚ βˆͺ s₃) := ext $ Ξ» x, by simp only [mem_union, or_assoc] instance : is_associative (finset Ξ±) (βˆͺ) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset Ξ±) : s βˆͺ s = s := ext $ Ξ» _, mem_union.trans $ or_self _ instance : is_idempotent (finset Ξ±) (βˆͺ) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : s₁ βˆͺ (sβ‚‚ βˆͺ s₃) = sβ‚‚ βˆͺ (s₁ βˆͺ s₃) := ext $ Ξ» _, by simp only [mem_union, or.left_comm] theorem union_right_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = (s₁ βˆͺ s₃) βˆͺ sβ‚‚ := ext $ Ξ» x, by simp only [mem_union, or_assoc, or_comm (x ∈ sβ‚‚)] theorem union_self (s : finset Ξ±) : s βˆͺ s = s := union_idempotent s @[simp] theorem union_empty (s : finset Ξ±) : s βˆͺ βˆ… = s := ext $ Ξ» x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset Ξ±) : βˆ… βˆͺ s = s := ext $ Ξ» x, mem_union.trans $ false_or _ theorem insert_eq (a : Ξ±) (s : finset Ξ±) : insert a s = {a} βˆͺ s := rfl @[simp] theorem insert_union (a : Ξ±) (s t : finset Ξ±) : insert a s βˆͺ t = insert a (s βˆͺ t) := by simp only [insert_eq, union_assoc] @[simp] theorem union_insert (a : Ξ±) (s t : finset Ξ±) : s βˆͺ insert a t = insert a (s βˆͺ t) := by simp only [insert_eq, union_left_comm] theorem insert_union_distrib (a : Ξ±) (s t : finset Ξ±) : insert a (s βˆͺ t) = insert a s βˆͺ insert a t := by simp only [insert_union, union_insert, insert_idem] @[simp] lemma union_eq_left_iff_subset {s t : finset Ξ±} : s βˆͺ t = s ↔ t βŠ† s := begin split, { assume h, have : t βŠ† s βˆͺ t := subset_union_right _ _, rwa h at this }, { assume h, exact subset.antisymm (union_subset (subset.refl _) h) (subset_union_left _ _) } end @[simp] lemma left_eq_union_iff_subset {s t : finset Ξ±} : s = s βˆͺ t ↔ t βŠ† s := by rw [← union_eq_left_iff_subset, eq_comm] @[simp] lemma union_eq_right_iff_subset {s t : finset Ξ±} : t βˆͺ s = s ↔ t βŠ† s := by rw [union_comm, union_eq_left_iff_subset] @[simp] lemma right_eq_union_iff_subset {s t : finset Ξ±} : s = t βˆͺ s ↔ t βŠ† s := by rw [← union_eq_right_iff_subset, eq_comm] /-- To prove a relation on pairs of `finset X`, it suffices to show that it is * symmetric, * it holds when one of the `finset`s is empty, * it holds for pairs of singletons, * if it holds for `[a, c]` and for `[b, c]`, then it holds for `[a βˆͺ b, c]`. -/ lemma induction_on_union (P : finset Ξ± β†’ finset Ξ± β†’ Prop) (symm : βˆ€ {a b}, P a b β†’ P b a) (empty_right : βˆ€ {a}, P a βˆ…) (singletons : βˆ€ {a b}, P {a} {b}) (union_of : βˆ€ {a b c}, P a c β†’ P b c β†’ P (a βˆͺ b) c) : βˆ€ a b, P a b := begin intros a b, refine finset.induction_on b empty_right (Ξ» x s xs hi, symm _), rw finset.insert_eq, apply union_of _ (symm hi), refine finset.induction_on a empty_right (Ξ» a t ta hi, symm _), rw finset.insert_eq, exact union_of singletons (symm hi), end lemma exists_mem_subset_of_subset_bUnion_of_directed_on {Ξ± ΞΉ : Type*} {f : ΞΉ β†’ set Ξ±} {c : set ΞΉ} {a : ΞΉ} (hac : a ∈ c) (hc : directed_on (Ξ» i j, f i βŠ† f j) c) {s : finset Ξ±} (hs : (s : set Ξ±) βŠ† ⋃ i ∈ c, f i) : βˆƒ i ∈ c, (s : set Ξ±) βŠ† f i := begin classical, revert hs, apply s.induction_on, { intros, use [a, hac], simp }, { intros b t hbt htc hbtc, obtain ⟨i : ΞΉ , hic : i ∈ c, hti : (t : set Ξ±) βŠ† f i⟩ := htc (set.subset.trans (t.subset_insert b) hbtc), obtain ⟨j, hjc, hbj⟩ : βˆƒ j ∈ c, b ∈ f j, by simpa [set.mem_bUnion_iff] using hbtc (t.mem_insert_self b), rcases hc j hjc i hic with ⟨k, hkc, hk, hk'⟩, use [k, hkc], rw [coe_insert, set.insert_subset], exact ⟨hk hbj, trans hti hk'⟩ } end /-! ### inter -/ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset Ξ±) := ⟨λ s₁ sβ‚‚, ⟨_, nodup_ndinter sβ‚‚.1 s₁.2⟩⟩ -- TODO: some of these results may have simpler proofs, once there are enough results -- to obtain the `lattice` instance. theorem inter_val_nd (s₁ sβ‚‚ : finset Ξ±) : (s₁ ∩ sβ‚‚).1 = ndinter s₁.1 sβ‚‚.1 := rfl @[simp] theorem inter_val (s₁ sβ‚‚ : finset Ξ±) : (s₁ ∩ sβ‚‚).1 = s₁.1 ∩ sβ‚‚.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a ∈ s₁ ∩ sβ‚‚ ↔ a ∈ s₁ ∧ a ∈ sβ‚‚ := mem_ndinter theorem mem_of_mem_inter_left {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} (h : a ∈ s₁ ∩ sβ‚‚) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} (h : a ∈ s₁ ∩ sβ‚‚) : a ∈ sβ‚‚ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a ∈ s₁ β†’ a ∈ sβ‚‚ β†’ a ∈ s₁ ∩ sβ‚‚ := and_imp.1 mem_inter.2 theorem inter_subset_left (s₁ sβ‚‚ : finset Ξ±) : s₁ ∩ sβ‚‚ βŠ† s₁ := Ξ» a, mem_of_mem_inter_left theorem inter_subset_right (s₁ sβ‚‚ : finset Ξ±) : s₁ ∩ sβ‚‚ βŠ† sβ‚‚ := Ξ» a, mem_of_mem_inter_right theorem subset_inter {s₁ sβ‚‚ s₃ : finset Ξ±} : s₁ βŠ† sβ‚‚ β†’ s₁ βŠ† s₃ β†’ s₁ βŠ† sβ‚‚ ∩ s₃ := by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial @[simp, norm_cast] lemma coe_inter (s₁ sβ‚‚ : finset Ξ±) : ↑(s₁ ∩ sβ‚‚) = (s₁ ∩ sβ‚‚ : set Ξ±) := set.ext $ Ξ» _, mem_inter @[simp] theorem union_inter_cancel_left {s t : finset Ξ±} : (s βˆͺ t) ∩ s = s := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left] @[simp] theorem union_inter_cancel_right {s t : finset Ξ±} : (s βˆͺ t) ∩ t = t := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right] theorem inter_comm (s₁ sβ‚‚ : finset Ξ±) : s₁ ∩ sβ‚‚ = sβ‚‚ ∩ s₁ := ext $ Ξ» _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = s₁ ∩ (sβ‚‚ ∩ s₃) := ext $ Ξ» _, by simp only [mem_inter, and_assoc] theorem inter_left_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : s₁ ∩ (sβ‚‚ ∩ s₃) = sβ‚‚ ∩ (s₁ ∩ s₃) := ext $ Ξ» _, by simp only [mem_inter, and.left_comm] theorem inter_right_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = (s₁ ∩ s₃) ∩ sβ‚‚ := ext $ Ξ» _, by simp only [mem_inter, and.right_comm] @[simp] theorem inter_self (s : finset Ξ±) : s ∩ s = s := ext $ Ξ» _, mem_inter.trans $ and_self _ @[simp] theorem inter_empty (s : finset Ξ±) : s ∩ βˆ… = βˆ… := ext $ Ξ» _, mem_inter.trans $ and_false _ @[simp] theorem empty_inter (s : finset Ξ±) : βˆ… ∩ s = βˆ… := ext $ Ξ» _, mem_inter.trans $ false_and _ @[simp] lemma inter_union_self (s t : finset Ξ±) : s ∩ (t βˆͺ s) = s := by rw [inter_comm, union_inter_cancel_right] @[simp] theorem insert_inter_of_mem {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} (h : a ∈ sβ‚‚) : insert a s₁ ∩ sβ‚‚ = insert a (s₁ ∩ sβ‚‚) := ext $ Ξ» x, have x = a ∨ x ∈ sβ‚‚ ↔ x ∈ sβ‚‚, from or_iff_right_of_imp $ by rintro rfl; exact h, by simp only [mem_inter, mem_insert, or_and_distrib_left, this] @[simp] theorem inter_insert_of_mem {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} (h : a ∈ s₁) : s₁ ∩ insert a sβ‚‚ = insert a (s₁ ∩ sβ‚‚) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} (h : a βˆ‰ sβ‚‚) : insert a s₁ ∩ sβ‚‚ = s₁ ∩ sβ‚‚ := ext $ Ξ» x, have Β¬ (x = a ∧ x ∈ sβ‚‚), by rintro ⟨rfl, H⟩; exact h H, by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or] @[simp] theorem inter_insert_of_not_mem {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} (h : a βˆ‰ s₁) : s₁ ∩ insert a sβ‚‚ = s₁ ∩ sβ‚‚ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : Ξ±} {s : finset Ξ±} (H : a ∈ s) : {a} ∩ s = {a} := show insert a βˆ… ∩ s = insert a βˆ…, by rw [insert_inter_of_mem H, empty_inter] @[simp] theorem singleton_inter_of_not_mem {a : Ξ±} {s : finset Ξ±} (H : a βˆ‰ s) : {a} ∩ s = βˆ… := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h @[simp] theorem inter_singleton_of_mem {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : s ∩ {a} = {a} := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : s ∩ {a} = βˆ… := by rw [inter_comm, singleton_inter_of_not_mem h] @[mono] lemma inter_subset_inter {x y s t : finset Ξ±} (h : x βŠ† y) (h' : s βŠ† t) : x ∩ s βŠ† y ∩ t := begin intros a a_in, rw finset.mem_inter at a_in ⊒, exact ⟨h a_in.1, h' a_in.2⟩ end lemma inter_subset_inter_right {x y s : finset Ξ±} (h : x βŠ† y) : x ∩ s βŠ† y ∩ s := finset.inter_subset_inter h (finset.subset.refl _) lemma inter_subset_inter_left {x y s : finset Ξ±} (h : x βŠ† y) : s ∩ x βŠ† s ∩ y := finset.inter_subset_inter (finset.subset.refl _) h /-! ### lattice laws -/ instance : lattice (finset Ξ±) := { sup := (βˆͺ), sup_le := assume a b c, union_subset, le_sup_left := subset_union_left, le_sup_right := subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, ..finset.partial_order } @[simp] theorem sup_eq_union : ((βŠ”) : finset Ξ± β†’ finset Ξ± β†’ finset Ξ±) = (βˆͺ) := rfl @[simp] theorem inf_eq_inter : ((βŠ“) : finset Ξ± β†’ finset Ξ± β†’ finset Ξ±) = (∩) := rfl instance : semilattice_inf_bot (finset Ξ±) := { bot := βˆ…, bot_le := empty_subset, ..finset.lattice } @[simp] lemma bot_eq_empty : (βŠ₯ : finset Ξ±) = βˆ… := rfl instance {Ξ± : Type*} [decidable_eq Ξ±] : semilattice_sup_bot (finset Ξ±) := { ..finset.semilattice_inf_bot, ..finset.lattice } instance : distrib_lattice (finset Ξ±) := { le_sup_inf := assume a b c, show (a βˆͺ b) ∩ (a βˆͺ c) βŠ† a βˆͺ b ∩ c, by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt}; simp only [true_or, imp_true_iff, true_and, or_true], ..finset.lattice } theorem inter_distrib_left (s t u : finset Ξ±) : s ∩ (t βˆͺ u) = (s ∩ t) βˆͺ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : finset Ξ±) : (s βˆͺ t) ∩ u = (s ∩ u) βˆͺ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : finset Ξ±) : s βˆͺ (t ∩ u) = (s βˆͺ t) ∩ (s βˆͺ u) := sup_inf_left theorem union_distrib_right (s t u : finset Ξ±) : (s ∩ t) βˆͺ u = (s βˆͺ u) ∩ (t βˆͺ u) := sup_inf_right lemma union_eq_empty_iff (A B : finset Ξ±) : A βˆͺ B = βˆ… ↔ A = βˆ… ∧ B = βˆ… := sup_eq_bot_iff lemma union_subset_iff {s₁ sβ‚‚ s₃ : finset Ξ±} : s₁ βˆͺ sβ‚‚ βŠ† s₃ ↔ s₁ βŠ† s₃ ∧ sβ‚‚ βŠ† s₃ := (sup_le_iff : s₁ βŠ” sβ‚‚ ≀ s₃ ↔ s₁ ≀ s₃ ∧ sβ‚‚ ≀ s₃) lemma subset_inter_iff {s₁ sβ‚‚ s₃ : finset Ξ±} : s₁ βŠ† sβ‚‚ ∩ s₃ ↔ s₁ βŠ† sβ‚‚ ∧ s₁ βŠ† s₃ := (le_inf_iff : s₁ ≀ sβ‚‚ βŠ“ s₃ ↔ s₁ ≀ sβ‚‚ ∧ s₁ ≀ s₃) theorem inter_eq_left_iff_subset (s t : finset Ξ±) : s ∩ t = s ↔ s βŠ† t := (inf_eq_left : s βŠ“ t = s ↔ s ≀ t) theorem inter_eq_right_iff_subset (s t : finset Ξ±) : t ∩ s = s ↔ s βŠ† t := (inf_eq_right : t βŠ“ s = s ↔ s ≀ t) /-! ### erase -/ /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset Ξ±) (a : Ξ±) : finset Ξ± := ⟨_, nodup_erase_of_nodup a s.2⟩ @[simp] theorem erase_val (s : finset Ξ±) (a : Ξ±) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : Ξ±} {s : finset Ξ±} : a ∈ erase s b ↔ a β‰  b ∧ a ∈ s := mem_erase_iff_of_nodup s.2 theorem not_mem_erase (a : Ξ±) (s : finset Ξ±) : a βˆ‰ erase s a := mem_erase_of_nodup s.2 -- While this can be solved by `simp`, this lemma is eligible for `dsimp` @[nolint simp_nf, simp] theorem erase_empty (a : Ξ±) : erase βˆ… a = βˆ… := rfl @[simp] lemma erase_singleton (a : Ξ±) : ({a} : finset Ξ±).erase a = βˆ… := begin ext x, rw [mem_erase, mem_singleton, not_and_self], refl, end theorem ne_of_mem_erase {a b : Ξ±} {s : finset Ξ±} : b ∈ erase s a β†’ b β‰  a := by simp only [mem_erase]; exact and.left theorem mem_of_mem_erase {a b : Ξ±} {s : finset Ξ±} : b ∈ erase s a β†’ b ∈ s := mem_of_mem_erase theorem mem_erase_of_ne_of_mem {a b : Ξ±} {s : finset Ξ±} : a β‰  b β†’ a ∈ s β†’ a ∈ erase s b := by simp only [mem_erase]; exact and.intro /-- An element of `s` that is not an element of `erase s a` must be `a`. -/ lemma eq_of_mem_of_not_mem_erase {a b : Ξ±} {s : finset Ξ±} (hs : b ∈ s) (hsa : b βˆ‰ s.erase a) : b = a := begin rw [mem_erase, not_and] at hsa, exact not_imp_not.mp hsa hs end theorem erase_insert {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : erase (insert a s) a = s := ext $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or]; apply and_iff_right_of_imp; rintro H rfl; exact h H theorem insert_erase {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : insert a (erase s a) = s := ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and]; apply or_iff_right_of_imp; rintro rfl; exact h theorem erase_subset_erase (a : Ξ±) {s t : finset Ξ±} (h : s βŠ† t) : erase s a βŠ† erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : Ξ±) (s : finset Ξ±) : erase s a βŠ† s := erase_subset _ _ @[simp, norm_cast] lemma coe_erase (a : Ξ±) (s : finset Ξ±) : ↑(erase s a) = (s \ {a} : set Ξ±) := set.ext $ Ξ» _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl lemma erase_ssubset {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : s.erase a βŠ‚ s := calc s.erase a βŠ‚ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h @[simp] theorem erase_eq_of_not_mem {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h theorem subset_insert_iff {a : Ξ±} {s t : finset Ξ±} : s βŠ† insert a t ↔ erase s a βŠ† t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp]; exact forall_congr (Ξ» x, forall_swap) theorem erase_insert_subset (a : Ξ±) (s : finset Ξ±) : erase (insert a s) a βŠ† s := subset_insert_iff.1 $ subset.refl _ theorem insert_erase_subset (a : Ξ±) (s : finset Ξ±) : s βŠ† insert a (erase s a) := subset_insert_iff.2 $ subset.refl _ lemma erase_inj {x y : Ξ±} (s : finset Ξ±) (hx : x ∈ s) : s.erase x = s.erase y ↔ x = y := begin refine ⟨λ h, _, congr_arg _⟩, rw eq_of_mem_of_not_mem_erase hx, rw ←h, simp, end lemma erase_inj_on (s : finset Ξ±) : set.inj_on s.erase s := Ξ» _ _ _ _, (erase_inj s β€Ή_β€Ί).mp /-! ### sdiff -/ /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset Ξ±) := ⟨λs₁ sβ‚‚, ⟨s₁.1 - sβ‚‚.1, nodup_of_le tsub_le_self s₁.2⟩⟩ @[simp] lemma sdiff_val (s₁ sβ‚‚ : finset Ξ±) : (s₁ \ sβ‚‚).val = s₁.val - sβ‚‚.val := rfl @[simp] theorem mem_sdiff {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a ∈ s₁ \ sβ‚‚ ↔ a ∈ s₁ ∧ a βˆ‰ sβ‚‚ := mem_sub_of_nodup s₁.2 @[simp] theorem inter_sdiff_self (s₁ sβ‚‚ : finset Ξ±) : s₁ ∩ (sβ‚‚ \ s₁) = βˆ… := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h instance : generalized_boolean_algebra (finset Ξ±) := { sup_inf_sdiff := Ξ» x y, by { simp only [ext_iff, mem_union, mem_sdiff, inf_eq_inter, sup_eq_union, mem_inter], tauto }, inf_inf_sdiff := Ξ» x y, by { simp only [ext_iff, inter_sdiff_self, inter_empty, inter_assoc, false_iff, inf_eq_inter, not_mem_empty], tauto }, ..finset.has_sdiff, ..finset.distrib_lattice, ..finset.semilattice_inf_bot } lemma not_mem_sdiff_of_mem_right {a : Ξ±} {s t : finset Ξ±} (h : a ∈ t) : a βˆ‰ s \ t := by simp only [mem_sdiff, h, not_true, not_false_iff, and_false] theorem union_sdiff_of_subset {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : s₁ βˆͺ (sβ‚‚ \ s₁) = sβ‚‚ := sup_sdiff_of_le h theorem sdiff_union_of_subset {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : (sβ‚‚ \ s₁) βˆͺ s₁ = sβ‚‚ := (union_comm _ _).trans (union_sdiff_of_subset h) theorem inter_sdiff (s t u : finset Ξ±) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] } @[simp] theorem sdiff_inter_self (s₁ sβ‚‚ : finset Ξ±) : (sβ‚‚ \ s₁) ∩ s₁ = βˆ… := inf_sdiff_self_left @[simp] theorem sdiff_self (s₁ : finset Ξ±) : s₁ \ s₁ = βˆ… := sdiff_self theorem sdiff_inter_distrib_right (s₁ sβ‚‚ s₃ : finset Ξ±) : s₁ \ (sβ‚‚ ∩ s₃) = (s₁ \ sβ‚‚) βˆͺ (s₁ \ s₃) := sdiff_inf @[simp] theorem sdiff_inter_self_left (s₁ sβ‚‚ : finset Ξ±) : s₁ \ (s₁ ∩ sβ‚‚) = s₁ \ sβ‚‚ := sdiff_inf_self_left @[simp] theorem sdiff_inter_self_right (s₁ sβ‚‚ : finset Ξ±) : s₁ \ (sβ‚‚ ∩ s₁) = s₁ \ sβ‚‚ := sdiff_inf_self_right @[simp] theorem sdiff_empty {s₁ : finset Ξ±} : s₁ \ βˆ… = s₁ := sdiff_bot @[mono] theorem sdiff_subset_sdiff {s₁ sβ‚‚ t₁ tβ‚‚ : finset Ξ±} (h₁ : t₁ βŠ† tβ‚‚) (hβ‚‚ : sβ‚‚ βŠ† s₁) : t₁ \ s₁ βŠ† tβ‚‚ \ sβ‚‚ := sdiff_le_sdiff β€Ήt₁ ≀ tβ‚‚β€Ί β€Ήsβ‚‚ ≀ s₁› @[simp, norm_cast] lemma coe_sdiff (s₁ sβ‚‚ : finset Ξ±) : ↑(s₁ \ sβ‚‚) = (s₁ \ sβ‚‚ : set Ξ±) := set.ext $ Ξ» _, mem_sdiff @[simp] theorem union_sdiff_self_eq_union {s t : finset Ξ±} : s βˆͺ (t \ s) = s βˆͺ t := sup_sdiff_self_right @[simp] theorem sdiff_union_self_eq_union {s t : finset Ξ±} : (s \ t) βˆͺ t = s βˆͺ t := sup_sdiff_self_left lemma union_sdiff_symm {s t : finset Ξ±} : s βˆͺ (t \ s) = t βˆͺ (s \ t) := sup_sdiff_symm lemma sdiff_union_inter (s t : finset Ξ±) : (s \ t) βˆͺ (s ∩ t) = s := by { rw union_comm, exact sup_inf_sdiff _ _ } @[simp] lemma sdiff_idem (s t : finset Ξ±) : s \ t \ t = s \ t := sdiff_idem lemma sdiff_eq_empty_iff_subset {s t : finset Ξ±} : s \ t = βˆ… ↔ s βŠ† t := sdiff_eq_bot_iff @[simp] lemma empty_sdiff (s : finset Ξ±) : βˆ… \ s = βˆ… := bot_sdiff lemma insert_sdiff_of_not_mem (s : finset Ξ±) {t : finset Ξ±} {x : Ξ±} (h : x βˆ‰ t) : (insert x s) \ t = insert x (s \ t) := begin rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_not_mem s h end lemma insert_sdiff_of_mem (s : finset Ξ±) {t : finset Ξ±} {x : Ξ±} (h : x ∈ t) : (insert x s) \ t = s \ t := begin rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_mem s h end @[simp] lemma insert_sdiff_insert (s t : finset Ξ±) (x : Ξ±) : (insert x s) \ (insert x t) = s \ insert x t := insert_sdiff_of_mem _ (mem_insert_self _ _) lemma sdiff_insert_of_not_mem {s : finset Ξ±} {x : Ξ±} (h : x βˆ‰ s) (t : finset Ξ±) : s \ (insert x t) = s \ t := begin refine subset.antisymm (sdiff_subset_sdiff (subset.refl _) (subset_insert _ _)) (Ξ» y hy, _), simp only [mem_sdiff, mem_insert, not_or_distrib] at hy ⊒, exact ⟨hy.1, Ξ» hxy, h $ hxy β–Έ hy.1, hy.2⟩ end @[simp] lemma sdiff_subset (s t : finset Ξ±) : s \ t βŠ† s := show s \ t ≀ s, from sdiff_le lemma sdiff_ssubset {s t : finset Ξ±} (h : t βŠ† s) (ht : t.nonempty) : s \ t βŠ‚ s := sdiff_lt (le_iff_subset.2 h) ht.ne_empty lemma union_sdiff_distrib (s₁ sβ‚‚ t : finset Ξ±) : (s₁ βˆͺ sβ‚‚) \ t = s₁ \ t βˆͺ sβ‚‚ \ t := sup_sdiff lemma sdiff_union_distrib (s t₁ tβ‚‚ : finset Ξ±) : s \ (t₁ βˆͺ tβ‚‚) = (s \ t₁) ∩ (s \ tβ‚‚) := sdiff_sup lemma union_sdiff_self (s t : finset Ξ±) : (s βˆͺ t) \ t = s \ t := sup_sdiff_right_self lemma sdiff_singleton_eq_erase (a : Ξ±) (s : finset Ξ±) : s \ singleton a = erase s a := by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto } @[simp] lemma sdiff_singleton_not_mem_eq_self (s : finset Ξ±) {a : Ξ±} (ha : a βˆ‰ s) : s \ {a} = s := by simp only [sdiff_singleton_eq_erase, ha, erase_eq_of_not_mem, not_false_iff] lemma sdiff_sdiff_self_left (s t : finset Ξ±) : s \ (s \ t) = s ∩ t := sdiff_sdiff_right_self lemma sdiff_eq_sdiff_iff_inter_eq_inter {s t₁ tβ‚‚ : finset Ξ±} : s \ t₁ = s \ tβ‚‚ ↔ s ∩ t₁ = s ∩ tβ‚‚ := sdiff_eq_sdiff_iff_inf_eq_inf lemma union_eq_sdiff_union_sdiff_union_inter (s t : finset Ξ±) : s βˆͺ t = (s \ t) βˆͺ (t \ s) βˆͺ (s ∩ t) := sup_eq_sdiff_sup_sdiff_sup_inf end decidable_eq /-! ### attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset Ξ±) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ theorem sizeof_lt_sizeof_of_mem [has_sizeof Ξ±] {x : Ξ±} {s : finset Ξ±} (hx : x ∈ s) : sizeof x < sizeof s := by { cases s, dsimp [sizeof, has_sizeof.sizeof, finset.sizeof], apply lt_add_left, exact multiset.sizeof_lt_sizeof_of_mem hx } @[simp] theorem attach_val (s : finset Ξ±) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset Ξ±) : βˆ€ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (βˆ… : finset Ξ±) = βˆ… := rfl @[simp] lemma attach_nonempty_iff (s : finset Ξ±) : s.attach.nonempty ↔ s.nonempty := by simp [finset.nonempty] @[simp] lemma attach_eq_empty_iff (s : finset Ξ±) : s.attach = βˆ… ↔ s = βˆ… := by simpa [eq_empty_iff_forall_not_mem] /-! ### piecewise -/ section piecewise /-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its complement. -/ def piecewise {Ξ± : Type*} {Ξ΄ : Ξ± β†’ Sort*} (s : finset Ξ±) (f g : Ξ i, Ξ΄ i) [βˆ€j, decidable (j ∈ s)] : Ξ i, Ξ΄ i := Ξ»i, if i ∈ s then f i else g i variables {Ξ΄ : Ξ± β†’ Sort*} (s : finset Ξ±) (f g : Ξ i, Ξ΄ i) @[simp] lemma piecewise_insert_self [decidable_eq Ξ±] {j : Ξ±} [βˆ€i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] @[simp] lemma piecewise_empty [βˆ€i : Ξ±, decidable (i ∈ (βˆ… : finset Ξ±))] : piecewise βˆ… f g = g := by { ext i, simp [piecewise] } variable [βˆ€j, decidable (j ∈ s)] @[norm_cast] lemma piecewise_coe [βˆ€j, decidable (j ∈ (s : set Ξ±))] : (s : set Ξ±).piecewise f g = s.piecewise f g := by { ext, congr } @[simp, priority 980] lemma piecewise_eq_of_mem {i : Ξ±} (hi : i ∈ s) : s.piecewise f g i = f i := by simp [piecewise, hi] @[simp, priority 980] lemma piecewise_eq_of_not_mem {i : Ξ±} (hi : i βˆ‰ s) : s.piecewise f g i = g i := by simp [piecewise, hi] lemma piecewise_congr {f f' g g' : Ξ  i, Ξ΄ i} (hf : βˆ€ i ∈ s, f i = f' i) (hg : βˆ€ i βˆ‰ s, g i = g' i) : s.piecewise f g = s.piecewise f' g' := funext $ Ξ» i, if_ctx_congr iff.rfl (hf i) (hg i) @[simp, priority 990] lemma piecewise_insert_of_ne [decidable_eq Ξ±] {i j : Ξ±} [βˆ€i, decidable (i ∈ insert j s)] (h : i β‰  j) : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] lemma piecewise_insert [decidable_eq Ξ±] (j : Ξ±) [βˆ€i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = update (s.piecewise f g) j (f j) := begin classical, rw [← piecewise_coe, ← piecewise_coe, ← set.piecewise_insert, ← coe_insert j s], congr end lemma piecewise_cases {i} (p : Ξ΄ i β†’ Prop) (hf : p (f i)) (hg : p (g i)) : p (s.piecewise f g i) := by by_cases hi : i ∈ s; simpa [hi] lemma piecewise_mem_set_pi {Ξ΄ : Ξ± β†’ Type*} {t : set Ξ±} {t' : Ξ  i, set (Ξ΄ i)} {f g} (hf : f ∈ set.pi t t') (hg : g ∈ set.pi t t') : s.piecewise f g ∈ set.pi t t' := by { classical, rw ← piecewise_coe, exact set.piecewise_mem_pi ↑s hf hg } lemma piecewise_singleton [decidable_eq Ξ±] (i : Ξ±) : piecewise {i} f g = update g i (f i) := by rw [← insert_emptyc_eq, piecewise_insert, piecewise_empty] lemma piecewise_piecewise_of_subset_left {s t : finset Ξ±} [Ξ  i, decidable (i ∈ s)] [Ξ  i, decidable (i ∈ t)] (h : s βŠ† t) (f₁ fβ‚‚ g : Ξ  a, Ξ΄ a) : s.piecewise (t.piecewise f₁ fβ‚‚) g = s.piecewise f₁ g := s.piecewise_congr (Ξ» i hi, piecewise_eq_of_mem _ _ _ (h hi)) (Ξ» _ _, rfl) @[simp] lemma piecewise_idem_left (f₁ fβ‚‚ g : Ξ  a, Ξ΄ a) : s.piecewise (s.piecewise f₁ fβ‚‚) g = s.piecewise f₁ g := piecewise_piecewise_of_subset_left (subset.refl _) _ _ _ lemma piecewise_piecewise_of_subset_right {s t : finset Ξ±} [Ξ  i, decidable (i ∈ s)] [Ξ  i, decidable (i ∈ t)] (h : t βŠ† s) (f g₁ gβ‚‚ : Ξ  a, Ξ΄ a) : s.piecewise f (t.piecewise g₁ gβ‚‚) = s.piecewise f gβ‚‚ := s.piecewise_congr (Ξ» _ _, rfl) (Ξ» i hi, t.piecewise_eq_of_not_mem _ _ (mt (@h _) hi)) @[simp] lemma piecewise_idem_right (f g₁ gβ‚‚ : Ξ  a, Ξ΄ a) : s.piecewise f (s.piecewise g₁ gβ‚‚) = s.piecewise f gβ‚‚ := piecewise_piecewise_of_subset_right (subset.refl _) f g₁ gβ‚‚ lemma update_eq_piecewise {Ξ² : Type*} [decidable_eq Ξ±] (f : Ξ± β†’ Ξ²) (i : Ξ±) (v : Ξ²) : update f i v = piecewise (singleton i) (Ξ»j, v) f := (piecewise_singleton _ _ _).symm lemma update_piecewise [decidable_eq Ξ±] (i : Ξ±) (v : Ξ΄ i) : update (s.piecewise f g) i v = s.piecewise (update f i v) (update g i v) := begin ext j, rcases em (j = i) with (rfl|hj); by_cases hs : j ∈ s; simp * end lemma update_piecewise_of_mem [decidable_eq Ξ±] {i : Ξ±} (hi : i ∈ s) (v : Ξ΄ i) : update (s.piecewise f g) i v = s.piecewise (update f i v) g := begin rw update_piecewise, refine s.piecewise_congr (Ξ» _ _, rfl) (Ξ» j hj, update_noteq _ _ _), exact Ξ» h, hj (h.symm β–Έ hi) end lemma update_piecewise_of_not_mem [decidable_eq Ξ±] {i : Ξ±} (hi : i βˆ‰ s) (v : Ξ΄ i) : update (s.piecewise f g) i v = s.piecewise f (update g i v) := begin rw update_piecewise, refine s.piecewise_congr (Ξ» j hj, update_noteq _ _ _) (Ξ» _ _, rfl), exact Ξ» h, hi (h β–Έ hj) end lemma piecewise_le_of_le_of_le {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g h : Ξ  i, Ξ΄ i} (Hf : f ≀ h) (Hg : g ≀ h) : s.piecewise f g ≀ h := Ξ» x, piecewise_cases s f g (≀ h x) (Hf x) (Hg x) lemma le_piecewise_of_le_of_le {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g h : Ξ  i, Ξ΄ i} (Hf : h ≀ f) (Hg : h ≀ g) : h ≀ s.piecewise f g := Ξ» x, piecewise_cases s f g (Ξ» y, h x ≀ y) (Hf x) (Hg x) lemma piecewise_le_piecewise' {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g f' g' : Ξ  i, Ξ΄ i} (Hf : βˆ€ x ∈ s, f x ≀ f' x) (Hg : βˆ€ x βˆ‰ s, g x ≀ g' x) : s.piecewise f g ≀ s.piecewise f' g' := Ξ» x, by { by_cases hx : x ∈ s; simp [hx, *] } lemma piecewise_le_piecewise {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g f' g' : Ξ  i, Ξ΄ i} (Hf : f ≀ f') (Hg : g ≀ g') : s.piecewise f g ≀ s.piecewise f' g' := s.piecewise_le_piecewise' (Ξ» x _, Hf x) (Ξ» x _, Hg x) lemma piecewise_mem_Icc_of_mem_of_mem {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f f₁ g g₁ : Ξ  i, Ξ΄ i} (hf : f ∈ set.Icc f₁ g₁) (hg : g ∈ set.Icc f₁ g₁) : s.piecewise f g ∈ set.Icc f₁ g₁ := ⟨le_piecewise_of_le_of_le _ hf.1 hg.1, piecewise_le_of_le_of_le _ hf.2 hg.2⟩ lemma piecewise_mem_Icc {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g : Ξ  i, Ξ΄ i} (h : f ≀ g) : s.piecewise f g ∈ set.Icc f g := piecewise_mem_Icc_of_mem_of_mem _ (set.left_mem_Icc.2 h) (set.right_mem_Icc.2 h) lemma piecewise_mem_Icc' {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g : Ξ  i, Ξ΄ i} (h : g ≀ f) : s.piecewise f g ∈ set.Icc g f := piecewise_mem_Icc_of_mem_of_mem _ (set.right_mem_Icc.2 h) (set.left_mem_Icc.2 h) end piecewise section decidable_pi_exists variables {s : finset Ξ±} instance decidable_dforall_finset {p : Ξ a∈s, Prop} [hp : βˆ€a (h : a ∈ s), decidable (p a h)] : decidable (βˆ€a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {Ξ² : Ξ± β†’ Type*} [h : βˆ€a, decidable_eq (Ξ² a)] : decidable_eq (Ξ a∈s, Ξ² a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Ξ a∈s, Prop} [hp : βˆ€a (h : a ∈ s), decidable (p a h)] : decidable (βˆƒa (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /-! ### filter -/ section filter variables (p q : Ξ± β†’ Prop) [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (s : finset Ξ±) : finset Ξ± := ⟨_, nodup_filter p s.2⟩ @[simp] theorem filter_val (s : finset Ξ±) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem filter_subset (s : finset Ξ±) : s.filter p βŠ† s := filter_subset _ _ variable {p} @[simp] theorem mem_filter {s : finset Ξ±} {a : Ξ±} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter theorem filter_ssubset {s : finset Ξ±} : s.filter p βŠ‚ s ↔ βˆƒ x ∈ s, Β¬ p x := ⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (Ξ» hp, mem_filter.2 ⟨hs, hp⟩) hp⟩, Ξ» ⟨x, hs, hp⟩, ⟨s.filter_subset _, Ξ» h, hp (mem_filter.1 (h hs)).2⟩⟩ variable (p) theorem filter_filter (s : finset Ξ±) : (s.filter p).filter q = s.filter (Ξ»a, p a ∧ q a) := ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm] lemma filter_true {s : finset Ξ±} [h : decidable_pred (Ξ» _, true)] : @finset.filter Ξ± (Ξ» _, true) h s = s := by ext; simp @[simp] theorem filter_false {h} (s : finset Ξ±) : @filter Ξ± (Ξ»a, false) h s = βˆ… := ext $ assume a, by simp only [mem_filter, and_false]; refl variables {p q} /-- If all elements of a `finset` satisfy the predicate `p`, `s.filter p` is `s`. -/ @[simp] lemma filter_true_of_mem {s : finset Ξ±} (h : βˆ€ x ∈ s, p x) : s.filter p = s := ext $ Ξ» x, ⟨λ h, (mem_filter.1 h).1, Ξ» hx, mem_filter.2 ⟨hx, h x hx⟩⟩ /-- If all elements of a `finset` fail to satisfy the predicate `p`, `s.filter p` is `βˆ…`. -/ lemma filter_false_of_mem {s : finset Ξ±} (h : βˆ€ x ∈ s, Β¬ p x) : s.filter p = βˆ… := eq_empty_of_forall_not_mem (by simpa) lemma filter_congr {s : finset Ξ±} (H : βˆ€ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H variables (p q) lemma filter_empty : filter p βˆ… = βˆ… := subset_empty.1 $ filter_subset _ _ lemma filter_subset_filter {s t : finset Ξ±} (h : s βŠ† t) : s.filter p βŠ† t.filter p := assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩ lemma monotone_filter_left (p : Ξ± β†’ Prop) [decidable_pred p] : monotone (filter p) := Ξ» _ _, filter_subset_filter p lemma monotone_filter_right (s : finset Ξ±) ⦃p q : Ξ± β†’ Prop⦄ [decidable_pred p] [decidable_pred q] (h : p ≀ q) : s.filter p ≀ s.filter q := multiset.subset_of_le (multiset.monotone_filter_right s.val h) @[simp, norm_cast] lemma coe_filter (s : finset Ξ±) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set Ξ±) := set.ext $ Ξ» _, mem_filter theorem filter_singleton (a : Ξ±) : filter p (singleton a) = if p a then singleton a else βˆ… := by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } variable [decidable_eq Ξ±] theorem filter_union (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).filter p = s₁.filter p βˆͺ sβ‚‚.filter p := ext $ Ξ» _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_union_right (s : finset Ξ±) : s.filter p βˆͺ s.filter q = s.filter (Ξ»x, p x ∨ q x) := ext $ Ξ» x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm] lemma filter_mem_eq_inter {s t : finset Ξ±} [Ξ  i, decidable (i ∈ t)] : s.filter (Ξ» i, i ∈ t) = s ∩ t := ext $ Ξ» i, by rw [mem_filter, mem_inter] theorem filter_inter (s t : finset Ξ±) : filter p s ∩ t = filter p (s ∩ t) := by { ext, simp only [mem_inter, mem_filter, and.right_comm] } theorem inter_filter (s t : finset Ξ±) : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : Ξ±) (s : finset Ξ±) : filter p (insert a s) = if p a then insert a (filter p s) else filter p s := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_or [decidable_pred (Ξ» a, p a ∨ q a)] (s : finset Ξ±) : s.filter (Ξ» a, p a ∨ q a) = s.filter p βˆͺ s.filter q := ext $ Ξ» _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and [decidable_pred (Ξ» a, p a ∧ q a)] (s : finset Ξ±) : s.filter (Ξ» a, p a ∧ q a) = s.filter p ∩ s.filter q := ext $ Ξ» _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not [decidable_pred (Ξ» a, Β¬ p a)] (s : finset Ξ±) : s.filter (Ξ» a, Β¬ p a) = s \ s.filter p := ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using Ξ» a, and_congr_right $ Ξ» h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ sβ‚‚ : finset Ξ±) : s₁ \ sβ‚‚ = filter (βˆ‰ sβ‚‚) s₁ := ext $ Ξ» _, by simp only [mem_sdiff, mem_filter] theorem sdiff_eq_self (s₁ sβ‚‚ : finset Ξ±) : s₁ \ sβ‚‚ = s₁ ↔ s₁ ∩ sβ‚‚ βŠ† βˆ… := by { simp [subset.antisymm_iff], split; intro h, { transitivity' ((s₁ \ sβ‚‚) ∩ sβ‚‚), mono, simp }, { calc s₁ \ sβ‚‚ βŠ‡ s₁ \ (s₁ ∩ sβ‚‚) : by simp [(βŠ‡)] ... βŠ‡ s₁ \ βˆ… : by mono using [(βŠ‡)] ... βŠ‡ s₁ : by simp [(βŠ‡)] } } theorem filter_union_filter_neg_eq [decidable_pred (Ξ» a, Β¬ p a)] (s : finset Ξ±) : s.filter p βˆͺ s.filter (Ξ»a, Β¬ p a) = s := by simp only [filter_not, union_sdiff_of_subset (filter_subset p s)] theorem filter_inter_filter_neg_eq [decidable_pred (Ξ» a, Β¬ p a)] (s : finset Ξ±) : s.filter p ∩ s.filter (Ξ»a, Β¬ p a) = βˆ… := by simp only [filter_not, inter_sdiff_self] lemma subset_union_elim {s : finset Ξ±} {t₁ tβ‚‚ : set Ξ±} (h : ↑s βŠ† t₁ βˆͺ tβ‚‚) : βˆƒs₁ sβ‚‚ : finset Ξ±, s₁ βˆͺ sβ‚‚ = s ∧ ↑s₁ βŠ† t₁ ∧ ↑sβ‚‚ βŠ† tβ‚‚ \ t₁ := begin classical, refine ⟨s.filter (∈ t₁), s.filter (βˆ‰ t₁), _, _ , _⟩, { simp [filter_union_right, em] }, { intro x, simp }, { intro x, simp, intros hx hxβ‚‚, refine ⟨or.resolve_left (h hx) hxβ‚‚, hxβ‚‚βŸ© } end /- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/ @[simp] lemma filter_congr_decidable {Ξ±} (s : finset Ξ±) (p : Ξ± β†’ Prop) (h : decidable_pred p) [decidable_pred p] : @filter Ξ± p h s = s.filter p := by congr section classical open_locale classical /-- The following instance allows us to write `{x ∈ s | p x}` for `finset.filter p s`. Since the former notation requires us to define this for all propositions `p`, and `finset.filter` only works for decidable propositions, the notation `{x ∈ s | p x}` is only compatible with classical logic because it uses `classical.prop_decidable`. We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp` unfolds the notation `{x ∈ s | p x}` to `finset.filter p s`. If `p` happens to be decidable, the simp-lemma `finset.filter_congr_decidable` will make sure that `finset.filter` uses the right instance for decidability. -/ noncomputable instance {Ξ± : Type*} : has_sep Ξ± (finset Ξ±) := ⟨λ p x, x.filter p⟩ @[simp] lemma sep_def {Ξ± : Type*} (s : finset Ξ±) (p : Ξ± β†’ Prop) : {x ∈ s | p x} = s.filter p := rfl end classical /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ -- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter(eq b)`. lemma filter_eq [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) : s.filter (eq b) = ite (b ∈ s) {b} βˆ… := begin split_ifs, { ext, simp only [mem_filter, mem_singleton], exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩, }⟩ }, { ext, simp only [mem_filter, not_and, iff_false, not_mem_empty], rintros m ⟨e⟩, exact h m, } end /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ lemma filter_eq' [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) : s.filter (Ξ» a, a = b) = ite (b ∈ s) {b} βˆ… := trans (filter_congr (Ξ» _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b) lemma filter_ne [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) : s.filter (Ξ» a, b β‰  a) = s.erase b := by { ext, simp only [mem_filter, mem_erase, ne.def], tauto, } lemma filter_ne' [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) : s.filter (Ξ» a, a β‰  b) = s.erase b := trans (filter_congr (Ξ» _ _, ⟨ne.symm, ne.symm⟩)) (filter_ne s b) end filter /-! ### range -/ section range variables {n m l : β„•} /-- `range n` is the set of natural numbers less than `n`. -/ def range (n : β„•) : finset β„• := ⟨_, nodup_range n⟩ @[simp] theorem range_coe (n : β„•) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp] theorem range_zero : range 0 = βˆ… := rfl @[simp] theorem range_one : range 1 = {0} := rfl theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm theorem range_add_one : range (n + 1) = insert n (range n) := range_succ @[simp] theorem not_mem_range_self : n βˆ‰ range n := not_mem_range_self @[simp] theorem self_mem_range_succ (n : β„•) : n ∈ range (n + 1) := multiset.self_mem_range_succ n @[simp] theorem range_subset {n m} : range n βŠ† range m ↔ n ≀ m := range_subset theorem range_mono : monotone range := Ξ» _ _, range_subset.2 lemma mem_range_succ_iff {a b : β„•} : a ∈ finset.range b.succ ↔ a ≀ b := finset.mem_range.trans nat.lt_succ_iff lemma mem_range_le {n x : β„•} (hx : x ∈ range n) : x ≀ n := (mem_range.1 hx).le lemma mem_range_sub_ne_zero {n x : β„•} (hx : x ∈ range n) : n - x β‰  0 := ne_of_gt $ tsub_pos_of_lt $ mem_range.1 hx @[simp] lemma nonempty_range_iff : (range n).nonempty ↔ n β‰  0 := ⟨λ ⟨k, hk⟩, ((zero_le k).trans_lt $ mem_range.1 hk).ne', Ξ» h, ⟨0, mem_range.2 $ pos_iff_ne_zero.2 h⟩⟩ @[simp] lemma range_eq_empty_iff : range n = βˆ… ↔ n = 0 := by rw [← not_nonempty_iff_eq_empty, nonempty_range_iff, not_not] lemma nonempty_range_succ : (range $ n + 1).nonempty := nonempty_range_iff.2 n.succ_ne_zero end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : Ξ± β†’ Prop) : (βˆƒ x, x ∈ (βˆ… : finset Ξ±) ∧ p x) ↔ false := by simp only [not_mem_empty, false_and, exists_false] theorem exists_mem_insert [d : decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) (p : Ξ± β†’ Prop) : (βˆƒ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (βˆƒ x, x ∈ s ∧ p x) := by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem forall_mem_empty_iff (p : Ξ± β†’ Prop) : (βˆ€ x, x ∈ (βˆ… : finset Ξ±) β†’ p x) ↔ true := iff_true_intro $ Ξ» _, false.elim theorem forall_mem_insert [d : decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) (p : Ξ± β†’ Prop) : (βˆ€ x, x ∈ insert a s β†’ p x) ↔ p a ∧ (βˆ€ x, x ∈ s β†’ p x) := by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] end finset /-- Equivalence between the set of natural numbers which are `β‰₯ k` and `β„•`, given by `n β†’ n - k`. -/ def not_mem_range_equiv (k : β„•) : {n // n βˆ‰ range k} ≃ β„• := { to_fun := Ξ» i, i.1 - k, inv_fun := Ξ» j, ⟨j + k, by simp⟩, left_inv := begin assume j, rw subtype.ext_iff_val, apply tsub_add_cancel_of_le, simpa using j.2 end, right_inv := Ξ» j, add_tsub_cancel_right _ _ } @[simp] lemma coe_not_mem_range_equiv (k : β„•) : (not_mem_range_equiv k : {n // n βˆ‰ range k} β†’ β„•) = (Ξ» i, i - k) := rfl @[simp] lemma coe_not_mem_range_equiv_symm (k : β„•) : ((not_mem_range_equiv k).symm : β„• β†’ {n // n βˆ‰ range k}) = Ξ» j, ⟨j + k, by simp⟩ := rfl /-! ### erase_dup on list and multiset -/ namespace multiset variable [decidable_eq Ξ±] /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset Ξ±) : finset Ξ± := ⟨_, nodup_erase_dup s⟩ @[simp] theorem to_finset_val (s : multiset Ξ±) : s.to_finset.1 = s.erase_dup := rfl theorem to_finset_eq {s : multiset Ξ±} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 n.erase_dup.symm lemma nodup.to_finset_inj {l l' : multiset Ξ±} (hl : nodup l) (hl' : nodup l') (h : l.to_finset = l'.to_finset) : l = l' := by simpa [←to_finset_eq hl, ←to_finset_eq hl'] using h @[simp] theorem mem_to_finset {a : Ξ±} {s : multiset Ξ±} : a ∈ s.to_finset ↔ a ∈ s := mem_erase_dup @[simp] lemma to_finset_zero : to_finset (0 : multiset Ξ±) = βˆ… := rfl @[simp] lemma to_finset_cons (a : Ξ±) (s : multiset Ξ±) : to_finset (a ::β‚˜ s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons @[simp] lemma to_finset_singleton (a : Ξ±) : to_finset ({a} : multiset Ξ±) = {a} := by rw [singleton_eq_cons, to_finset_cons, to_finset_zero, is_lawful_singleton.insert_emptyc_eq] @[simp] lemma to_finset_add (s t : multiset Ξ±) : to_finset (s + t) = to_finset s βˆͺ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_nsmul (s : multiset Ξ±) : βˆ€(n : β„•) (hn : n β‰  0), (n β€’ s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, one_nsmul] }, { rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] } end @[simp] lemma to_finset_inter (s t : multiset Ξ±) : to_finset (s ∩ t) = to_finset s ∩ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_union (s t : multiset Ξ±) : (s βˆͺ t).to_finset = s.to_finset βˆͺ t.to_finset := by ext; simp theorem to_finset_eq_empty {m : multiset Ξ±} : m.to_finset = βˆ… ↔ m = 0 := finset.val_inj.symm.trans multiset.erase_dup_eq_zero @[simp] lemma to_finset_subset (m1 m2 : multiset Ξ±) : m1.to_finset βŠ† m2.to_finset ↔ m1 βŠ† m2 := by simp only [finset.subset_iff, multiset.subset_iff, multiset.mem_to_finset] end multiset namespace finset @[simp] lemma val_to_finset [decidable_eq Ξ±] (s : finset Ξ±) : s.val.to_finset = s := by { ext, rw [multiset.mem_to_finset, ←mem_def] } end finset namespace list variable [decidable_eq Ξ±] /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list Ξ±) : finset Ξ± := multiset.to_finset l @[simp] theorem to_finset_val (l : list Ξ±) : l.to_finset.1 = (l.erase_dup : multiset Ξ±) := rfl theorem to_finset_eq {l : list Ξ±} (n : nodup l) : @finset.mk Ξ± l n = l.to_finset := multiset.to_finset_eq n @[simp] theorem mem_to_finset {a : Ξ±} {l : list Ξ±} : a ∈ l.to_finset ↔ a ∈ l := mem_erase_dup @[simp] theorem to_finset_nil : to_finset (@nil Ξ±) = βˆ… := rfl @[simp] theorem to_finset_cons {a : Ξ±} {l : list Ξ±} : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h] lemma to_finset_surj_on : set.surj_on to_finset {l : list Ξ± | l.nodup} set.univ := begin rintro s -, cases s with t hl, induction t using quot.ind with l, refine ⟨l, hl, (to_finset_eq hl).symm⟩ end theorem to_finset_surjective : surjective (to_finset : list Ξ± β†’ finset Ξ±) := by { intro s, rcases to_finset_surj_on (set.mem_univ s) with ⟨l, -, hls⟩, exact ⟨l, hls⟩ } lemma to_finset_eq_iff_perm_erase_dup {l l' : list Ξ±} : l.to_finset = l'.to_finset ↔ l.erase_dup ~ l'.erase_dup := by simp [finset.ext_iff, perm_ext (nodup_erase_dup _) (nodup_erase_dup _)] lemma to_finset_eq_of_perm (l l' : list Ξ±) (h : l ~ l') : l.to_finset = l'.to_finset := to_finset_eq_iff_perm_erase_dup.mpr h.erase_dup lemma perm_of_nodup_nodup_to_finset_eq {l l' : list Ξ±} (hl : nodup l) (hl' : nodup l') (h : l.to_finset = l'.to_finset) : l ~ l' := begin rw ←multiset.coe_eq_coe, exact multiset.nodup.to_finset_inj hl hl' h end @[simp] lemma to_finset_append {l l' : list Ξ±} : to_finset (l ++ l') = l.to_finset βˆͺ l'.to_finset := begin induction l with hd tl hl, { simp }, { simp [hl] } end @[simp] lemma to_finset_reverse {l : list Ξ±} : to_finset l.reverse = l.to_finset := to_finset_eq_of_perm _ _ (reverse_perm l) end list namespace finset /-! ### map -/ section map open function /-- When `f` is an embedding of `Ξ±` in `Ξ²` and `s` is a finset in `Ξ±`, then `s.map f` is the image finset in `Ξ²`. The embedding condition guarantees that there are no duplicates in the image. -/ def map (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : finset Ξ² := ⟨s.1.map f, nodup_map f.2 s.2⟩ @[simp] theorem map_val (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : Ξ± β†ͺ Ξ²) : (βˆ… : finset Ξ±).map f = βˆ… := rfl variables {f : Ξ± β†ͺ Ξ²} {s : finset Ξ±} @[simp] theorem mem_map {b : Ξ²} : b ∈ s.map f ↔ βˆƒ a ∈ s, f a = b := mem_map.trans $ by simp only [exists_prop]; refl @[simp] theorem mem_map_equiv {f : Ξ± ≃ Ξ²} {b : Ξ²} : b ∈ s.map f.to_embedding ↔ f.symm b ∈ s := by { rw mem_map, exact ⟨by { rintro ⟨a, H, rfl⟩, simpa }, Ξ» h, ⟨_, h, by simp⟩⟩ } theorem mem_map' (f : Ξ± β†ͺ Ξ²) {a} {s : finset Ξ±} : f a ∈ s.map f ↔ a ∈ s := mem_map_of_injective f.2 theorem mem_map_of_mem (f : Ξ± β†ͺ Ξ²) {a} {s : finset Ξ±} : a ∈ s β†’ f a ∈ s.map f := (mem_map' _).2 lemma apply_coe_mem_map (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) (x : s) : f x ∈ s.map f := mem_map_of_mem f x.prop @[simp, norm_cast] theorem coe_map (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : (s.map f : set Ξ²) = f '' s := set.ext $ Ξ» x, mem_map.trans set.mem_image_iff_bex.symm theorem coe_map_subset_range (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : (s.map f : set Ξ²) βŠ† set.range f := calc ↑(s.map f) = f '' s : coe_map f s ... βŠ† set.range f : set.image_subset_range f ↑s theorem map_to_finset [decidable_eq Ξ±] [decidable_eq Ξ²] {s : multiset Ξ±} : s.to_finset.map f = (s.map f).to_finset := ext $ Ξ» _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset] @[simp] theorem map_refl : s.map (embedding.refl _) = s := ext $ Ξ» _, by simpa only [mem_map, exists_prop] using exists_eq_right @[simp] theorem map_cast_heq {Ξ± Ξ²} (h : Ξ± = Ξ²) (s : finset Ξ±) : s.map (equiv.cast h).to_embedding == s := by { subst h, simp } theorem map_map {g : Ξ² β†ͺ Ξ³} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp only [map_val, multiset.map_map]; refl @[simp] theorem map_subset_map {s₁ sβ‚‚ : finset Ξ±} : s₁.map f βŠ† sβ‚‚.map f ↔ s₁ βŠ† sβ‚‚ := ⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs, Ξ» h, by simp [subset_def, map_subset_map h]⟩ /-- Associate to an embedding `f` from `Ξ±` to `Ξ²` the order embedding that maps a finset to its image under `f`. -/ def map_embedding (f : Ξ± β†ͺ Ξ²) : finset Ξ± β†ͺo finset Ξ² := order_embedding.of_map_le_iff (map f) (Ξ» _ _, map_subset_map) @[simp] theorem map_inj {s₁ sβ‚‚ : finset Ξ±} : s₁.map f = sβ‚‚.map f ↔ s₁ = sβ‚‚ := (map_embedding f).injective.eq_iff @[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl theorem map_filter {p : Ξ² β†’ Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := eq_of_veq (map_filter _ _ _) theorem map_union [decidable_eq Ξ±] [decidable_eq Ξ²] {f : Ξ± β†ͺ Ξ²} (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).map f = s₁.map f βˆͺ sβ‚‚.map f := coe_injective $ by simp only [coe_map, coe_union, set.image_union] theorem map_inter [decidable_eq Ξ±] [decidable_eq Ξ²] {f : Ξ± β†ͺ Ξ²} (s₁ sβ‚‚ : finset Ξ±) : (s₁ ∩ sβ‚‚).map f = s₁.map f ∩ sβ‚‚.map f := coe_injective $ by simp only [coe_map, coe_inter, set.image_inter f.injective] @[simp] theorem map_singleton (f : Ξ± β†ͺ Ξ²) (a : Ξ±) : map f {a} = {f a} := coe_injective $ by simp only [coe_map, coe_singleton, set.image_singleton] @[simp] theorem map_insert [decidable_eq Ξ±] [decidable_eq Ξ²] (f : Ξ± β†ͺ Ξ²) (a : Ξ±) (s : finset Ξ±) : (insert a s).map f = insert (f a) (s.map f) := by simp only [insert_eq, map_union, map_singleton] @[simp] theorem map_eq_empty : s.map f = βˆ… ↔ s = βˆ… := ⟨λ h, eq_empty_of_forall_not_mem $ Ξ» a m, ne_empty_of_mem (mem_map_of_mem _ m) h, Ξ» e, e.symm β–Έ rfl⟩ lemma attach_map_val {s : finset Ξ±} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _ lemma nonempty.map (h : s.nonempty) (f : Ξ± β†ͺ Ξ²) : (s.map f).nonempty := let ⟨a, ha⟩ := h in ⟨f a, (mem_map' f).mpr ha⟩ end map lemma range_add_one' (n : β„•) : range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ.inj⟩) := by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n] /-! ### image -/ section image variables [decidable_eq Ξ²] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : Ξ± β†’ Ξ²) (s : finset Ξ±) : finset Ξ² := (s.1.map f).to_finset @[simp] theorem image_val (f : Ξ± β†’ Ξ²) (s : finset Ξ±) : (image f s).1 = (s.1.map f).erase_dup := rfl @[simp] theorem image_empty (f : Ξ± β†’ Ξ²) : (βˆ… : finset Ξ±).image f = βˆ… := rfl variables {f : Ξ± β†’ Ξ²} {s : finset Ξ±} @[simp] theorem mem_image {b : Ξ²} : b ∈ s.image f ↔ βˆƒ a ∈ s, f a = b := by simp only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop] theorem mem_image_of_mem (f : Ξ± β†’ Ξ²) {a} {s : finset Ξ±} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ instance [can_lift Ξ² Ξ±] : can_lift (finset Ξ²) (finset Ξ±) := { cond := Ξ» s, βˆ€ x ∈ s, can_lift.cond Ξ± x, coe := image can_lift.coe, prf := begin rintro ⟨⟨l⟩, hd : l.nodup⟩ hl, lift l to list Ξ± using hl, refine ⟨⟨l, list.nodup_of_nodup_map _ hd⟩, ext $ Ξ» a, _⟩, simp end } lemma _root_.function.injective.mem_finset_image {f : Ξ± β†’ Ξ²} (hf : function.injective f) {s : finset Ξ±} {x : Ξ±} : f x ∈ s.image f ↔ x ∈ s := begin refine ⟨λ h, _, finset.mem_image_of_mem f⟩, obtain ⟨y, hy, heq⟩ := mem_image.1 h, exact hf heq β–Έ hy, end lemma filter_mem_image_eq_image (f : Ξ± β†’ Ξ²) (s : finset Ξ±) (t : finset Ξ²) (h : βˆ€ x ∈ s, f x ∈ t) : t.filter (Ξ» y, y ∈ s.image f) = s.image f := by { ext, rw [mem_filter, mem_image], simp only [and_imp, exists_prop, and_iff_right_iff_imp, exists_imp_distrib], rintros x xel rfl, exact h _ xel } lemma fiber_nonempty_iff_mem_image (f : Ξ± β†’ Ξ²) (s : finset Ξ±) (y : Ξ²) : (s.filter (Ξ» x, f x = y)).nonempty ↔ y ∈ s.image f := by simp [finset.nonempty] @[simp, norm_cast] lemma coe_image {f : Ξ± β†’ Ξ²} : ↑(s.image f) = f '' ↑s := set.ext $ Ξ» _, mem_image.trans set.mem_image_iff_bex.symm lemma nonempty.image (h : s.nonempty) (f : Ξ± β†’ Ξ²) : (s.image f).nonempty := let ⟨a, ha⟩ := h in ⟨f a, mem_image_of_mem f ha⟩ @[simp] lemma nonempty.image_iff (f : Ξ± β†’ Ξ²) : (s.image f).nonempty ↔ s.nonempty := ⟨λ ⟨y, hy⟩, let ⟨x, hx, _⟩ := mem_image.mp hy in ⟨x, hx⟩, Ξ» h, h.image f⟩ theorem image_to_finset [decidable_eq Ξ±] {s : multiset Ξ±} : s.to_finset.image f = (s.map f).to_finset := ext $ Ξ» _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map] theorem image_val_of_inj_on (H : set.inj_on f s) : (image f s).1 = s.1.map f := (nodup_map_on H s.2).erase_dup @[simp] theorem image_id [decidable_eq Ξ±] : s.image id = s := ext $ Ξ» _, by simp only [mem_image, exists_prop, id, exists_eq_right] @[simp] theorem image_id' [decidable_eq Ξ±] : s.image (Ξ» x, x) = s := image_id theorem image_image [decidable_eq Ξ³] {g : Ξ² β†’ Ξ³} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map] theorem image_subset_image {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : s₁.image f βŠ† sβ‚‚.image f := by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset', multiset.map_subset_map h] theorem image_subset_iff {s : finset Ξ±} {t : finset Ξ²} {f : Ξ± β†’ Ξ²} : s.image f βŠ† t ↔ βˆ€ x ∈ s, f x ∈ t := calc s.image f βŠ† t ↔ f '' ↑s βŠ† ↑t : by norm_cast ... ↔ _ : set.image_subset_iff theorem image_mono (f : Ξ± β†’ Ξ²) : monotone (finset.image f) := Ξ» _ _, image_subset_image theorem coe_image_subset_range : ↑(s.image f) βŠ† set.range f := calc ↑(s.image f) = f '' ↑s : coe_image ... βŠ† set.range f : set.image_subset_range f ↑s theorem image_filter {p : Ξ² β†’ Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext $ Ξ» b, by simp only [mem_filter, mem_image, exists_prop]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩, by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem image_union [decidable_eq Ξ±] {f : Ξ± β†’ Ξ²} (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).image f = s₁.image f βˆͺ sβ‚‚.image f := ext $ Ξ» _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem image_inter [decidable_eq Ξ±] (s₁ sβ‚‚ : finset Ξ±) (hf : βˆ€x y, f x = f y β†’ x = y) : (s₁ ∩ sβ‚‚).image f = s₁.image f ∩ sβ‚‚.image f := ext $ by simp only [mem_image, exists_prop, mem_inter]; exact Ξ» b, ⟨λ ⟨a, ⟨m₁, mβ‚‚βŸ©, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, mβ‚‚, e⟩⟩, Ξ» ⟨⟨a, m₁, eβ‚βŸ©, ⟨a', mβ‚‚, eβ‚‚βŸ©βŸ©, ⟨a, ⟨m₁, hf _ _ (eβ‚‚.trans e₁.symm) β–Έ mβ‚‚βŸ©, eβ‚βŸ©βŸ©. @[simp] theorem image_singleton (f : Ξ± β†’ Ξ²) (a : Ξ±) : image f {a} = {f a} := ext $ Ξ» x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm @[simp] theorem image_insert [decidable_eq Ξ±] (f : Ξ± β†’ Ξ²) (a : Ξ±) (s : finset Ξ±) : (insert a s).image f = insert (f a) (s.image f) := by simp only [insert_eq, image_singleton, image_union] @[simp] lemma image_erase [decidable_eq Ξ±] {f : Ξ± β†’ Ξ²} (hf : injective f) (s : finset Ξ±) (a : Ξ±) : (s.erase a).image f = (s.image f).erase (f a) := begin ext b, simp only [mem_image, exists_prop, mem_erase], split, { rintro ⟨a', ⟨haa', ha'⟩, rfl⟩, exact ⟨hf.ne haa', a', ha', rfl⟩ }, { rintro ⟨h, a', ha', rfl⟩, exact ⟨a', ⟨ne_of_apply_ne _ h, ha'⟩, rfl⟩ } end @[simp] theorem image_eq_empty : s.image f = βˆ… ↔ s = βˆ… := ⟨λ h, eq_empty_of_forall_not_mem $ Ξ» a m, ne_empty_of_mem (mem_image_of_mem _ m) h, Ξ» e, e.symm β–Έ rfl⟩ lemma mem_range_iff_mem_finset_range_of_mod_eq' [decidable_eq Ξ±] {f : β„• β†’ Ξ±} {a : Ξ±} {n : β„•} (hn : 0 < n) (h : βˆ€i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (Ξ»i, f i) := begin split, { rintros ⟨i, hi⟩, simp only [mem_image, exists_prop, mem_range], exact ⟨i % n, nat.mod_lt i hn, (rfl.congr hi).mp (h i)⟩ }, { rintro h, simp only [mem_image, exists_prop, set.mem_range, mem_range] at *, rcases h with ⟨i, hi, ha⟩, use ⟨i, ha⟩ }, end lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq Ξ±] {f : β„€ β†’ Ξ±} {a : Ξ±} {n : β„•} (hn : 0 < n) (h : βˆ€i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (Ξ»i, f i) := suffices (βˆƒi, f (i % n) = a) ↔ βˆƒi, i < n ∧ f ↑i = a, by simpa [h], have hn' : 0 < (n : β„€), from int.coe_nat_lt.mpr hn, iff.intro (assume ⟨i, hi⟩, have 0 ≀ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'), ⟨int.to_nat (i % n), by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩) (assume ⟨i, hi, ha⟩, ⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩) @[simp] lemma attach_image_val [decidable_eq Ξ±] {s : finset Ξ±} : s.attach.image subtype.val = s := eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self] @[simp] lemma attach_image_coe [decidable_eq Ξ±] {s : finset Ξ±} : s.attach.image coe = s := finset.attach_image_val @[simp] lemma attach_insert [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (Ξ»x, ⟨x.1, mem_insert_of_mem x.2⟩)) := ext $ Ξ» ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx) (Ξ» h : x = a, Ξ» _, mem_insert.2 $ or.inl $ subtype.eq h) (Ξ» h : x ∈ s, Ξ» _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩), Ξ» _, finset.mem_attach _ _⟩ theorem map_eq_image (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : s.map f = s.image f := eq_of_veq (s.map f).2.erase_dup.symm lemma image_const {s : finset Ξ±} (h : s.nonempty) (b : Ξ²) : s.image (Ξ»a, b) = singleton b := ext $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, h.bex, true_and, mem_singleton, eq_comm] @[simp] lemma map_erase [decidable_eq Ξ±] (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) (a : Ξ±) : (s.erase a).map f = (s.map f).erase (f a) := by { simp_rw map_eq_image, exact s.image_erase f.2 a } /-- Because `finset.image` requires a `decidable_eq` instances for the target type, we can only construct a `functor finset` when working classically. -/ instance [Ξ  P, decidable P] : functor finset := { map := Ξ» Ξ± Ξ² f s, s.image f, } instance [Ξ  P, decidable P] : is_lawful_functor finset := { id_map := Ξ» Ξ± x, image_id, comp_map := Ξ» Ξ± Ξ² Ξ³ f g s, image_image.symm, } /-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose elements belong to `s`. -/ protected def subtype {Ξ±} (p : Ξ± β†’ Prop) [decidable_pred p] (s : finset Ξ±) : finset (subtype p) := (s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩, Ξ» x y H, subtype.eq $ subtype.mk.inj H⟩ @[simp] lemma mem_subtype {p : Ξ± β†’ Prop} [decidable_pred p] {s : finset Ξ±} : βˆ€{a : subtype p}, a ∈ s.subtype p ↔ (a : Ξ±) ∈ s | ⟨a, ha⟩ := by simp [finset.subtype, ha] lemma subtype_eq_empty {p : Ξ± β†’ Prop} [decidable_pred p] {s : finset Ξ±} : s.subtype p = βˆ… ↔ βˆ€ x, p x β†’ x βˆ‰ s := by simp [ext_iff, subtype.forall, subtype.coe_mk]; refl @[mono] lemma subtype_mono {p : Ξ± β†’ Prop} [decidable_pred p] : monotone (finset.subtype p) := Ξ» s t h x hx, mem_subtype.2 $ h $ mem_subtype.1 hx /-- `s.subtype p` converts back to `s.filter p` with `embedding.subtype`. -/ @[simp] lemma subtype_map (p : Ξ± β†’ Prop) [decidable_pred p] : (s.subtype p).map (embedding.subtype _) = s.filter p := begin ext x, simp [and_comm _ (_ = _), @and.left_comm _ (_ = _), and_comm (p x) (x ∈ s)] end /-- If all elements of a `finset` satisfy the predicate `p`, `s.subtype p` converts back to `s` with `embedding.subtype`. -/ lemma subtype_map_of_mem {p : Ξ± β†’ Prop} [decidable_pred p] (h : βˆ€ x ∈ s, p x) : (s.subtype p).map (embedding.subtype _) = s := by rw [subtype_map, filter_true_of_mem h] /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, all elements of the result have the property of the subtype. -/ lemma property_of_mem_map_subtype {p : Ξ± β†’ Prop} (s : finset {x // p x}) {a : Ξ±} (h : a ∈ s.map (embedding.subtype _)) : p a := begin rcases mem_map.1 h with ⟨x, hx, rfl⟩, exact x.2 end /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, the result does not contain any value that does not satisfy the property of the subtype. -/ lemma not_mem_map_subtype_of_not_property {p : Ξ± β†’ Prop} (s : finset {x // p x}) {a : Ξ±} (h : Β¬ p a) : a βˆ‰ (s.map (embedding.subtype _)) := mt s.property_of_mem_map_subtype h /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, the result is a subset of the set giving the subtype. -/ lemma map_subtype_subset {t : set Ξ±} (s : finset t) : ↑(s.map (embedding.subtype _)) βŠ† t := begin intros a ha, rw mem_coe at ha, convert property_of_mem_map_subtype s ha end lemma subset_image_iff {f : Ξ± β†’ Ξ²} {s : finset Ξ²} {t : set Ξ±} : ↑s βŠ† f '' t ↔ βˆƒs' : finset Ξ±, ↑s' βŠ† t ∧ s'.image f = s := begin split, swap, { rintro ⟨s, hs, rfl⟩, rw [coe_image], exact set.image_subset f hs }, intro h, letI : can_lift Ξ² t := ⟨f ∘ coe, Ξ» y, y ∈ f '' t, Ξ» y ⟨x, hxt, hy⟩, ⟨⟨x, hxt⟩, hy⟩⟩, lift s to finset t using h, refine ⟨s.map (embedding.subtype _), map_subtype_subset _, _⟩, ext y, simp end end image end finset theorem multiset.to_finset_map [decidable_eq Ξ±] [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) (m : multiset Ξ±) : (m.map f).to_finset = m.to_finset.image f := finset.val_inj.1 (multiset.erase_dup_map_erase_dup_eq _ _).symm namespace finset /-! ### card -/ section card /-- `card s` is the cardinality (number of elements) of `s`. -/ def card (s : finset Ξ±) : nat := s.1.card theorem card_def (s : finset Ξ±) : s.card = s.1.card := rfl @[simp] lemma card_mk {m nodup} : (⟨m, nodup⟩ : finset Ξ±).card = m.card := rfl @[simp] theorem card_empty : card (βˆ… : finset Ξ±) = 0 := rfl theorem card_le_of_subset {s t : finset Ξ±} : s βŠ† t β†’ card s ≀ card t := multiset.card_le_of_le ∘ val_le_iff.mpr @[simp] theorem card_eq_zero {s : finset Ξ±} : card s = 0 ↔ s = βˆ… := card_eq_zero.trans val_eq_zero theorem card_pos {s : finset Ξ±} : 0 < card s ↔ s.nonempty := pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm alias finset.card_pos ↔ _ finset.nonempty.card_pos theorem card_ne_zero_of_mem {s : finset Ξ±} {a : Ξ±} (h : a ∈ s) : card s β‰  0 := (not_congr card_eq_zero).2 (ne_empty_of_mem h) theorem card_eq_one {s : finset Ξ±} : s.card = 1 ↔ βˆƒ a, s = {a} := by cases s; simp only [multiset.card_eq_one, finset.card, ← val_inj, singleton_val] theorem card_le_one {s : finset Ξ±} : s.card ≀ 1 ↔ βˆ€ (a ∈ s) (b ∈ s), a = b := begin rcases s.eq_empty_or_nonempty with rfl|⟨x, hx⟩, { simp }, refine (nat.succ_le_of_lt (card_pos.2 ⟨x, hx⟩)).le_iff_eq.trans (card_eq_one.trans ⟨_, _⟩), { rintro ⟨y, rfl⟩, simp }, { exact Ξ» h, ⟨x, eq_singleton_iff_unique_mem.2 ⟨hx, Ξ» y hy, h _ hy _ hx⟩⟩ } end theorem card_le_one_iff {s : finset Ξ±} : s.card ≀ 1 ↔ βˆ€ {a b}, a ∈ s β†’ b ∈ s β†’ a = b := by { rw card_le_one, tauto } lemma card_le_one_iff_subset_singleton [nonempty Ξ±] {s : finset Ξ±} : s.card ≀ 1 ↔ βˆƒ (x : Ξ±), s βŠ† {x} := begin split, { assume H, by_cases h : βˆƒ x, x ∈ s, { rcases h with ⟨x, hx⟩, refine ⟨x, Ξ» y hy, _⟩, rw [card_le_one.1 H y hy x hx, mem_singleton] }, { push_neg at h, inhabit Ξ±, exact ⟨default Ξ±, Ξ» y hy, (h y hy).elim⟩ } }, { rintros ⟨x, hx⟩, rw ← card_singleton x, exact card_le_of_subset hx } end /-- A `finset` of a subsingleton type has cardinality at most one. -/ lemma card_le_one_of_subsingleton [subsingleton Ξ±] (s : finset Ξ±) : s.card ≀ 1 := finset.card_le_one_iff.2 $ Ξ» _ _ _ _, subsingleton.elim _ _ theorem one_lt_card {s : finset Ξ±} : 1 < s.card ↔ βˆƒ (a ∈ s) (b ∈ s), a β‰  b := by { rw ← not_iff_not, push_neg, exact card_le_one } lemma exists_ne_of_one_lt_card {s : finset Ξ±} (hs : 1 < s.card) (a : Ξ±) : βˆƒ b : Ξ±, b ∈ s ∧ b β‰  a := begin obtain ⟨x, hx, y, hy, hxy⟩ := finset.one_lt_card.mp hs, by_cases ha : y = a, { exact ⟨x, hx, ne_of_ne_of_eq hxy ha⟩ }, { exact ⟨y, hy, ha⟩ }, end lemma one_lt_card_iff {s : finset Ξ±} : 1 < s.card ↔ βˆƒ x y, (x ∈ s) ∧ (y ∈ s) ∧ x β‰  y := by { rw one_lt_card, simp only [exists_prop, exists_and_distrib_left] } @[simp] theorem card_cons {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : card (cons a s h) = card s + 1 := card_cons _ _ @[simp] theorem card_insert_of_not_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : card (insert a s) = card s + 1 := by rw [← cons_eq_insert _ _ h, card_cons] theorem card_insert_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : card (insert a s) = card s := by rw insert_eq_of_mem h theorem card_insert_le [decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) : card (insert a s) ≀ card s + 1 := by by_cases a ∈ s; [{rw [insert_eq_of_mem h], apply nat.le_add_right}, rw [card_insert_of_not_mem h]] @[simp] theorem card_singleton (a : Ξ±) : card ({a} : finset Ξ±) = 1 := card_singleton _ lemma card_singleton_inter [decidable_eq Ξ±] {x : Ξ±} {s : finset Ξ±} : ({x} ∩ s).card ≀ 1 := begin cases (finset.decidable_mem x s), { simp [finset.singleton_inter_of_not_mem h] }, { simp [finset.singleton_inter_of_mem h] }, end @[simp] theorem card_erase_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : a ∈ s β†’ card (erase s a) = pred (card s) := card_erase_of_mem theorem card_erase_lt_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : a ∈ s β†’ card (erase s a) < card s := card_erase_lt_of_mem theorem card_erase_le [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : card (erase s a) ≀ card s := card_erase_le theorem pred_card_le_card_erase [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : card s - 1 ≀ card (erase s a) := begin by_cases h : a ∈ s, { rw [card_erase_of_mem h], refl }, { rw [erase_eq_of_not_mem h], apply nat.sub_le } end /-- If `a ∈ s` is known, see also `finset.card_erase_of_mem` and `finset.erase_eq_of_not_mem`. -/ theorem card_erase_eq_ite [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : card (erase s a) = if a ∈ s then pred (card s) else card s := card_erase_eq_ite @[simp] theorem card_range (n : β„•) : card (range n) = n := card_range n @[simp] theorem card_attach {s : finset Ξ±} : card (attach s) = card s := multiset.card_attach end card end finset theorem multiset.to_finset_card_le [decidable_eq Ξ±] (m : multiset Ξ±) : m.to_finset.card ≀ m.card := card_le_of_le (erase_dup_le _) lemma list.card_to_finset [decidable_eq Ξ±] (l : list Ξ±) : finset.card l.to_finset = l.erase_dup.length := rfl theorem list.to_finset_card_le [decidable_eq Ξ±] (l : list Ξ±) : l.to_finset.card ≀ l.length := multiset.to_finset_card_le ⟦l⟧ namespace finset section card theorem card_image_le [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} : card (image f s) ≀ card s := by simpa only [card_map] using (s.1.map f).to_finset_card_le theorem card_image_of_inj_on [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} (H : set.inj_on f s) : card (image f s) = card s := by simp only [card, image_val_of_inj_on H, card_map] theorem inj_on_of_card_image_eq [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} (H : card (image f s) = card s) : set.inj_on f s := begin change (s.1.map f).erase_dup.card = s.1.card at H, have : (s.1.map f).erase_dup = s.1.map f, { apply multiset.eq_of_le_of_card_le, { apply multiset.erase_dup_le }, rw H, simp only [multiset.card_map] }, rw multiset.erase_dup_eq_self at this, apply inj_on_of_nodup_map this, end theorem card_image_eq_iff_inj_on [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} : (s.image f).card = s.card ↔ set.inj_on f s := ⟨inj_on_of_card_image_eq, card_image_of_inj_on⟩ theorem card_image_of_injective [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (s : finset Ξ±) (H : injective f) : card (image f s) = card s := card_image_of_inj_on $ Ξ» x _ y _ h, H h lemma fiber_card_ne_zero_iff_mem_image (s : finset Ξ±) (f : Ξ± β†’ Ξ²) [decidable_eq Ξ²] (y : Ξ²) : (s.filter (Ξ» x, f x = y)).card β‰  0 ↔ y ∈ s.image f := by { rw [←pos_iff_ne_zero, card_pos, fiber_nonempty_iff_mem_image] } @[simp] lemma card_map {Ξ± Ξ²} (f : Ξ± β†ͺ Ξ²) {s : finset Ξ±} : (s.map f).card = s.card := multiset.card_map _ _ @[simp] lemma card_subtype (p : Ξ± β†’ Prop) [decidable_pred p] (s : finset Ξ±) : (s.subtype p).card = (s.filter p).card := by simp [finset.subtype] lemma card_eq_of_bijective {s : finset Ξ±} {n : β„•} (f : βˆ€i, i < n β†’ Ξ±) (hf : βˆ€a∈s, βˆƒi, βˆƒh:i<n, f i h = a) (hf' : βˆ€i (h : i < n), f i h ∈ s) (f_inj : βˆ€i j (hi : i < n) (hj : j < n), f i hi = f j hj β†’ i = j) : card s = n := begin classical, have : βˆ€ (a : Ξ±), a ∈ s ↔ βˆƒi (hi : i ∈ range n), f i (mem_range.1 hi) = a, from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, assume ⟨i, hi, eq⟩, eq β–Έ hf' i (mem_range.1 hi)⟩, have : s = ((range n).attach.image $ Ξ»i, f i.1 (mem_range.1 i.2)), by simpa only [ext_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and], calc card s = card ((range n).attach.image $ Ξ»i, f i.1 (mem_range.1 i.2)) : by rw [this] ... = card ((range n).attach) : card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n end lemma card_eq_succ [decidable_eq Ξ±] {s : finset Ξ±} {n : β„•} : s.card = n + 1 ↔ (βˆƒa t, a βˆ‰ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have 0 < card s, from eq.symm β–Έ nat.zero_lt_succ _, let ⟨a, has⟩ := card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [eq, card_erase_of_mem has, pred_succ]⟩) (assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq β–Έ n_eq β–Έ card_insert_of_not_mem hat) theorem card_filter_le (s : finset Ξ±) (p : Ξ± β†’ Prop) [decidable_pred p] : card (s.filter p) ≀ card s := card_le_of_subset $ filter_subset _ _ theorem eq_of_subset_of_card_le {s t : finset Ξ±} (h : s βŠ† t) (hβ‚‚ : card t ≀ card s) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) hβ‚‚ lemma filter_card_eq {s : finset Ξ±} {p : Ξ± β†’ Prop} [decidable_pred p] (h : (s.filter p).card = s.card) (x : Ξ±) (hx : x ∈ s) : p x := begin rw [←eq_of_subset_of_card_le (s.filter_subset p) h.ge, mem_filter] at hx, exact hx.2, end lemma card_lt_card {s t : finset Ξ±} (h : s βŠ‚ t) : s.card < t.card := card_lt_of_lt (val_lt_iff.2 h) lemma card_le_card_of_inj_on {s : finset Ξ±} {t : finset Ξ²} (f : Ξ± β†’ Ξ²) (hf : βˆ€a∈s, f a ∈ t) (f_inj : βˆ€aβ‚βˆˆs, βˆ€aβ‚‚βˆˆs, f a₁ = f aβ‚‚ β†’ a₁ = aβ‚‚) : card s ≀ card t := begin classical, calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj] ... ≀ card t : card_le_of_subset $ image_subset_iff.2 hf end /-- If there are more pigeons than pigeonholes, then there are two pigeons in the same pigeonhole. -/ lemma exists_ne_map_eq_of_card_lt_of_maps_to {s : finset Ξ±} {t : finset Ξ²} (hc : t.card < s.card) {f : Ξ± β†’ Ξ²} (hf : βˆ€ a ∈ s, f a ∈ t) : βˆƒ (x ∈ s) (y ∈ s), x β‰  y ∧ f x = f y := begin classical, by_contra hz, push_neg at hz, refine hc.not_le (card_le_card_of_inj_on f hf _), intros x hx y hy, contrapose, exact hz x hx y hy, end lemma le_card_of_inj_on_range {n} {s : finset Ξ±} (f : β„• β†’ Ξ±) (hf : βˆ€i<n, f i ∈ s) (f_inj : βˆ€ (i<n) (j<n), f i = f j β†’ i = j) : n ≀ card s := calc n = card (range n) : (card_range n).symm ... ≀ card s : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simpa only [mem_range]) /-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to define an object on `s`. Then one can inductively define an object on all finsets, starting from the empty set and iterating. This can be used either to define data, or to prove properties. -/ def strong_induction {p : finset Ξ± β†’ Sort*} (H : βˆ€ s, (βˆ€ t βŠ‚ s, p t) β†’ p s) : βˆ€ (s : finset Ξ±), p s | s := H s (Ξ» t h, have card t < card s, from card_lt_card h, strong_induction t) using_well_founded {rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf card⟩]} lemma strong_induction_eq {p : finset Ξ± β†’ Sort*} (H : βˆ€ s, (βˆ€ t βŠ‚ s, p t) β†’ p s) (s : finset Ξ±) : strong_induction H s = H s (Ξ» t h, strong_induction H t) := by rw strong_induction /-- Analogue of `strong_induction` with order of arguments swapped. -/ @[elab_as_eliminator] def strong_induction_on {p : finset Ξ± β†’ Sort*} : βˆ€ (s : finset Ξ±), (βˆ€s, (βˆ€ t βŠ‚ s, p t) β†’ p s) β†’ p s := Ξ» s H, strong_induction H s lemma strong_induction_on_eq {p : finset Ξ± β†’ Sort*} (s : finset Ξ±) (H : βˆ€ s, (βˆ€ t βŠ‚ s, p t) β†’ p s) : s.strong_induction_on H = H s (Ξ» t h, t.strong_induction_on H) := by { dunfold strong_induction_on, rw strong_induction } @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq Ξ±] {p : finset Ξ± β†’ Prop} (s : finset Ξ±) (hβ‚€ : p βˆ…) (h₁ : βˆ€ a s, a βˆ‰ s β†’ (βˆ€ t βŠ† s, p t) β†’ p (insert a s)) : p s := finset.strong_induction_on s $ Ξ» s, finset.induction_on s (Ξ» _, hβ‚€) $ Ξ» a s n _ ih, h₁ a s n $ Ξ» t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) /-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than `n`, one knows how to define `p s`. Then one can inductively define `p s` for all finsets `s` of cardinality less than `n`, starting from finsets of card `n` and iterating. This can be used either to define data, or to prove properties. -/ def strong_downward_induction {p : finset Ξ± β†’ Sort*} {n : β„•} (H : βˆ€ t₁, (βˆ€ {tβ‚‚ : finset Ξ±}, tβ‚‚.card ≀ n β†’ t₁ βŠ‚ tβ‚‚ β†’ p tβ‚‚) β†’ t₁.card ≀ n β†’ p t₁) : βˆ€ (s : finset Ξ±), s.card ≀ n β†’ p s | s := H s (Ξ» t ht h, have n - card t < n - card s, from (tsub_lt_tsub_iff_left_of_le ht).2 (finset.card_lt_card h), strong_downward_induction t ht) using_well_founded {rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf (Ξ» (t : finset Ξ±), n - t.card)⟩]} lemma strong_downward_induction_eq {p : finset Ξ± β†’ Sort*} {n : β„•} (H : βˆ€ t₁, (βˆ€ {tβ‚‚ : finset Ξ±}, tβ‚‚.card ≀ n β†’ t₁ βŠ‚ tβ‚‚ β†’ p tβ‚‚) β†’ t₁.card ≀ n β†’ p t₁) (s : finset Ξ±) : strong_downward_induction H s = H s (Ξ» t ht hst, strong_downward_induction H t ht) := by rw strong_downward_induction /-- Analogue of `strong_downward_induction` with order of arguments swapped. -/ @[elab_as_eliminator] def strong_downward_induction_on {p : finset Ξ± β†’ Sort*} {n : β„•} : βˆ€ (s : finset Ξ±), (βˆ€ t₁, (βˆ€ {tβ‚‚ : finset Ξ±}, tβ‚‚.card ≀ n β†’ t₁ βŠ‚ tβ‚‚ β†’ p tβ‚‚) β†’ t₁.card ≀ n β†’ p t₁) β†’ s.card ≀ n β†’ p s := Ξ» s H, strong_downward_induction H s lemma strong_downward_induction_on_eq {p : finset Ξ± β†’ Sort*} (s : finset Ξ±) {n : β„•} (H : βˆ€ t₁, (βˆ€ {tβ‚‚ : finset Ξ±}, tβ‚‚.card ≀ n β†’ t₁ βŠ‚ tβ‚‚ β†’ p tβ‚‚) β†’ t₁.card ≀ n β†’ p t₁) : s.strong_downward_induction_on H = H s (Ξ» t ht h, t.strong_downward_induction_on H ht) := by { dunfold strong_downward_induction_on, rw strong_downward_induction } lemma card_congr {s : finset Ξ±} {t : finset Ξ²} (f : Ξ  a ∈ s, Ξ²) (h₁ : βˆ€ a ha, f a ha ∈ t) (hβ‚‚ : βˆ€ a b ha hb, f a ha = f b hb β†’ a = b) (h₃ : βˆ€ b ∈ t, βˆƒ a ha, f a ha = b) : s.card = t.card := by haveI := classical.prop_decidable; exact calc s.card = s.attach.card : card_attach.symm ... = (s.attach.image (Ξ» (a : {a // a ∈ s}), f a.1 a.2)).card : eq.symm (card_image_of_injective _ (Ξ» a b h, subtype.eq (hβ‚‚ _ _ _ _ h))) ... = t.card : congr_arg card (finset.ext $ Ξ» b, ⟨λ h, let ⟨a, ha₁, haβ‚‚βŸ© := mem_image.1 h in haβ‚‚ β–Έ h₁ _ _, Ξ» h, let ⟨a, ha₁, haβ‚‚βŸ© := h₃ b h in mem_image.2 ⟨⟨a, haβ‚βŸ©, by simp [haβ‚‚]⟩⟩) lemma card_union_add_card_inter [decidable_eq Ξ±] (s t : finset Ξ±) : (s βˆͺ t).card + (s ∩ t).card = s.card + t.card := finset.induction_on t (by simp) $ Ξ» a r har, by by_cases a ∈ s; simp *; cc lemma card_union_le [decidable_eq Ξ±] (s t : finset Ξ±) : (s βˆͺ t).card ≀ s.card + t.card := card_union_add_card_inter s t β–Έ nat.le_add_right _ _ lemma card_union_eq [decidable_eq Ξ±] {s t : finset Ξ±} (h : disjoint s t) : (s βˆͺ t).card = s.card + t.card := begin rw [← card_union_add_card_inter], convert (add_zero _).symm, rw [card_eq_zero], rwa [disjoint_iff] at h end lemma surj_on_of_inj_on_of_card_le {s : finset Ξ±} {t : finset Ξ²} (f : Ξ  a ∈ s, Ξ²) (hf : βˆ€ a ha, f a ha ∈ t) (hinj : βˆ€ a₁ aβ‚‚ ha₁ haβ‚‚, f a₁ ha₁ = f aβ‚‚ haβ‚‚ β†’ a₁ = aβ‚‚) (hst : card t ≀ card s) : (βˆ€ b ∈ t, βˆƒ a ha, b = f a ha) := by haveI := classical.dec_eq Ξ²; exact Ξ» b hb, have h : card (image (Ξ» (a : {a // a ∈ s}), f a a.prop) (attach s)) = card s, from @card_attach _ s β–Έ card_image_of_injective _ (Ξ» ⟨a₁, haβ‚βŸ© ⟨aβ‚‚, haβ‚‚βŸ© h, subtype.eq $ hinj _ _ _ _ h), have h₁ : image (Ξ» a : {a // a ∈ s}, f a a.prop) s.attach = t := eq_of_subset_of_card_le (Ξ» b h, let ⟨a, ha₁, haβ‚‚βŸ© := mem_image.1 h in haβ‚‚ β–Έ hf _ _) (by simp [hst, h]), begin rw ← h₁ at hb, rcases mem_image.1 hb with ⟨a, ha₁, haβ‚‚βŸ©, exact ⟨a, a.2, haβ‚‚.symm⟩, end open function lemma inj_on_of_surj_on_of_card_le {s : finset Ξ±} {t : finset Ξ²} (f : Ξ  a ∈ s, Ξ²) (hf : βˆ€ a ha, f a ha ∈ t) (hsurj : βˆ€ b ∈ t, βˆƒ a ha, b = f a ha) (hst : card s ≀ card t) ⦃a₁ a₂⦄ (ha₁ : a₁ ∈ s) (haβ‚‚ : aβ‚‚ ∈ s) (ha₁aβ‚‚: f a₁ ha₁ = f aβ‚‚ haβ‚‚) : a₁ = aβ‚‚ := by haveI : inhabited {x // x ∈ s} := ⟨⟨a₁, haβ‚βŸ©βŸ©; exact let f' : {x // x ∈ s} β†’ {x // x ∈ t} := Ξ» x, ⟨f x.1 x.2, hf x.1 x.2⟩ in let g : {x // x ∈ t} β†’ {x // x ∈ s} := @surj_inv _ _ f' (Ξ» x, let ⟨y, hy₁, hyβ‚‚βŸ© := hsurj x.1 x.2 in ⟨⟨y, hyβ‚βŸ©, subtype.eq hyβ‚‚.symm⟩) in have hg : injective g, from injective_surj_inv _, have hsg : surjective g, from Ξ» x, let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (Ξ» (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x) (Ξ» x _, show (g x) ∈ s.attach, from mem_attach _ _) (Ξ» x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in ⟨y, hy.snd.symm⟩, have hif : injective f', from (left_inverse_of_surjective_of_right_inverse hsg (right_inverse_surj_inv _)).injective, subtype.ext_iff_val.1 (@hif ⟨a₁, haβ‚βŸ© ⟨aβ‚‚, haβ‚‚βŸ© (subtype.eq ha₁aβ‚‚)) end card section to_list /-- Produce a list of the elements in the finite set using choice. -/ @[reducible] noncomputable def to_list (s : finset Ξ±) : list Ξ± := s.1.to_list lemma nodup_to_list (s : finset Ξ±) : s.to_list.nodup := by { rw [to_list, ←multiset.coe_nodup, multiset.coe_to_list], exact s.nodup } @[simp] lemma mem_to_list {a : Ξ±} (s : finset Ξ±) : a ∈ s.to_list ↔ a ∈ s := by { rw [to_list, ←multiset.mem_coe, multiset.coe_to_list], exact iff.rfl } @[simp] lemma length_to_list (s : finset Ξ±) : s.to_list.length = s.card := by { rw [to_list, ←multiset.coe_card, multiset.coe_to_list], refl } @[simp] lemma to_list_empty : (βˆ… : finset Ξ±).to_list = [] := by simp [to_list] @[simp, norm_cast] lemma coe_to_list (s : finset Ξ±) : (s.to_list : multiset Ξ±) = s.val := by { classical, ext, simp } @[simp] lemma to_list_to_finset [decidable_eq Ξ±] (s : finset Ξ±) : s.to_list.to_finset = s := by { ext, simp } lemma exists_list_nodup_eq [decidable_eq Ξ±] (s : finset Ξ±) : βˆƒ (l : list Ξ±), l.nodup ∧ l.to_finset = s := ⟨s.to_list, s.nodup_to_list, s.to_list_to_finset⟩ end to_list section bUnion /-! ### bUnion This section is about the bounded union of an indexed family `t : Ξ± β†’ finset Ξ²` of finite sets over a finite set `s : finset Ξ±`. -/ variables [decidable_eq Ξ²] {s : finset Ξ±} {t : Ξ± β†’ finset Ξ²} /-- `bUnion s t` is the union of `t x` over `x ∈ s`. (This was formerly `bind` due to the monad structure on types with `decidable_eq`.) -/ protected def bUnion (s : finset Ξ±) (t : Ξ± β†’ finset Ξ²) : finset Ξ² := (s.1.bind (Ξ» a, (t a).1)).to_finset @[simp] theorem bUnion_val (s : finset Ξ±) (t : Ξ± β†’ finset Ξ²) : (s.bUnion t).1 = (s.1.bind (Ξ» a, (t a).1)).erase_dup := rfl @[simp] theorem bUnion_empty : finset.bUnion βˆ… t = βˆ… := rfl @[simp] theorem mem_bUnion {b : Ξ²} : b ∈ s.bUnion t ↔ βˆƒa∈s, b ∈ t a := by simp only [mem_def, bUnion_val, mem_erase_dup, mem_bind, exists_prop] @[simp] theorem bUnion_insert [decidable_eq Ξ±] {a : Ξ±} : (insert a s).bUnion t = t a βˆͺ s.bUnion t := ext $ Ξ» x, by simp only [mem_bUnion, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext $ Ξ» x, by simp [or_and_distrib_right, exists_or_distrib] theorem bUnion_congr {s₁ sβ‚‚ : finset Ξ±} {t₁ tβ‚‚ : Ξ± β†’ finset Ξ²} (hs : s₁ = sβ‚‚) (ht : βˆ€ a ∈ s₁, t₁ a = tβ‚‚ a) : s₁.bUnion t₁ = sβ‚‚.bUnion tβ‚‚ := ext $ Ξ» x, by simp [hs, ht] { contextual := tt } @[simp] lemma singleton_bUnion {a : Ξ±} : finset.bUnion {a} t = t a := begin classical, rw [← insert_emptyc_eq, bUnion_insert, bUnion_empty, union_empty] end theorem bUnion_inter (s : finset Ξ±) (f : Ξ± β†’ finset Ξ²) (t : finset Ξ²) : s.bUnion f ∩ t = s.bUnion (Ξ» x, f x ∩ t) := begin ext x, simp only [mem_bUnion, mem_inter], tauto end theorem inter_bUnion (t : finset Ξ²) (s : finset Ξ±) (f : Ξ± β†’ finset Ξ²) : t ∩ s.bUnion f = s.bUnion (Ξ» x, t ∩ f x) := by rw [inter_comm, bUnion_inter]; simp [inter_comm] theorem image_bUnion [decidable_eq Ξ³] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} {t : Ξ² β†’ finset Ξ³} : (s.image f).bUnion t = s.bUnion (Ξ»a, t (f a)) := by haveI := classical.dec_eq Ξ±; exact finset.induction_on s rfl (Ξ» a s has ih, by simp only [image_insert, bUnion_insert, ih]) theorem bUnion_image [decidable_eq Ξ³] {s : finset Ξ±} {t : Ξ± β†’ finset Ξ²} {f : Ξ² β†’ Ξ³} : (s.bUnion t).image f = s.bUnion (Ξ»a, (t a).image f) := by haveI := classical.dec_eq Ξ±; exact finset.induction_on s rfl (Ξ» a s has ih, by simp only [bUnion_insert, image_union, ih]) lemma bUnion_bUnion [decidable_eq Ξ³] (s : finset Ξ±) (f : Ξ± β†’ finset Ξ²) (g : Ξ² β†’ finset Ξ³) : (s.bUnion f).bUnion g = s.bUnion (Ξ» a, (f a).bUnion g) := begin ext, simp only [finset.mem_bUnion, exists_prop], simp_rw [←exists_and_distrib_right, ←exists_and_distrib_left, and_assoc], rw exists_comm, end theorem bind_to_finset [decidable_eq Ξ±] (s : multiset Ξ±) (t : Ξ± β†’ multiset Ξ²) : (s.bind t).to_finset = s.to_finset.bUnion (Ξ»a, (t a).to_finset) := ext $ Ξ» x, by simp only [multiset.mem_to_finset, mem_bUnion, multiset.mem_bind, exists_prop] lemma bUnion_mono {t₁ tβ‚‚ : Ξ± β†’ finset Ξ²} (h : βˆ€a∈s, t₁ a βŠ† tβ‚‚ a) : s.bUnion t₁ βŠ† s.bUnion tβ‚‚ := have βˆ€b a, a ∈ s β†’ b ∈ t₁ a β†’ (βˆƒ (a : Ξ±), a ∈ s ∧ b ∈ tβ‚‚ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa only [subset_iff, mem_bUnion, exists_imp_distrib, and_imp, exists_prop] lemma bUnion_subset_bUnion_of_subset_left {Ξ± : Type*} {s₁ sβ‚‚ : finset Ξ±} (t : Ξ± β†’ finset Ξ²) (h : s₁ βŠ† sβ‚‚) : s₁.bUnion t βŠ† sβ‚‚.bUnion t := begin intro x, simp only [and_imp, mem_bUnion, exists_prop], exact Exists.imp (Ξ» a ha, ⟨h ha.1, ha.2⟩) end lemma subset_bUnion_of_mem {s : finset Ξ±} (u : Ξ± β†’ finset Ξ²) {x : Ξ±} (xs : x ∈ s) : u x βŠ† s.bUnion u := begin apply subset.trans _ (bUnion_subset_bUnion_of_subset_left u (singleton_subset_iff.2 xs)), exact subset_of_eq singleton_bUnion.symm, end @[simp] lemma bUnion_subset_iff_forall_subset {Ξ± Ξ² : Type*} [decidable_eq Ξ²] {s : finset Ξ±} {t : finset Ξ²} {f : Ξ± β†’ finset Ξ²} : s.bUnion f βŠ† t ↔ βˆ€ x ∈ s, f x βŠ† t := ⟨λ h x hx, (subset_bUnion_of_mem f hx).trans h, Ξ» h x hx, let ⟨a, ha₁, haβ‚‚βŸ© := mem_bUnion.mp hx in h _ ha₁ haβ‚‚βŸ© lemma bUnion_singleton {f : Ξ± β†’ Ξ²} : s.bUnion (Ξ»a, {f a}) = s.image f := ext $ Ξ» x, by simp only [mem_bUnion, mem_image, mem_singleton, eq_comm] @[simp] lemma bUnion_singleton_eq_self [decidable_eq Ξ±] : s.bUnion (singleton : Ξ± β†’ finset Ξ±) = s := by { rw bUnion_singleton, exact image_id } lemma bUnion_filter_eq_of_maps_to [decidable_eq Ξ±] {s : finset Ξ±} {t : finset Ξ²} {f : Ξ± β†’ Ξ²} (h : βˆ€ x ∈ s, f x ∈ t) : t.bUnion (Ξ»a, s.filter $ (Ξ»c, f c = a)) = s := ext $ Ξ» b, by simpa using h b lemma image_bUnion_filter_eq [decidable_eq Ξ±] (s : finset Ξ²) (g : Ξ² β†’ Ξ±) : (s.image g).bUnion (Ξ»a, s.filter $ (Ξ»c, g c = a)) = s := bUnion_filter_eq_of_maps_to (Ξ» x, mem_image_of_mem g) lemma erase_bUnion (f : Ξ± β†’ finset Ξ²) (s : finset Ξ±) (b : Ξ²) : (s.bUnion f).erase b = s.bUnion (Ξ» x, (f x).erase b) := by { ext, simp only [finset.mem_bUnion, iff_self, exists_and_distrib_left, finset.mem_erase] } @[simp] lemma bUnion_nonempty : (s.bUnion t).nonempty ↔ βˆƒ x ∈ s, (t x).nonempty := by simp [finset.nonempty, ← exists_and_distrib_left, @exists_swap Ξ±] lemma nonempty.bUnion (hs : s.nonempty) (ht : βˆ€ x ∈ s, (t x).nonempty) : (s.bUnion t).nonempty := bUnion_nonempty.2 $ hs.imp $ Ξ» x hx, ⟨hx, ht x hx⟩ end bUnion /-! ### prod -/ section prod variables {s : finset Ξ±} {t : finset Ξ²} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset Ξ±) (t : finset Ξ²) : finset (Ξ± Γ— Ξ²) := ⟨_, nodup_product s.2 t.2⟩ @[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] theorem mem_product {p : Ξ± Γ— Ξ²} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product theorem subset_product [decidable_eq Ξ±] [decidable_eq Ξ²] {s : finset (Ξ± Γ— Ξ²)} : s βŠ† (s.image prod.fst).product (s.image prod.snd) := Ξ» p hp, mem_product.2 ⟨mem_image_of_mem _ hp, mem_image_of_mem _ hp⟩ theorem product_eq_bUnion [decidable_eq Ξ±] [decidable_eq Ξ²] (s : finset Ξ±) (t : finset Ξ²) : s.product t = s.bUnion (Ξ»a, t.image $ Ξ»b, (a, b)) := ext $ Ξ» ⟨x, y⟩, by simp only [mem_product, mem_bUnion, mem_image, exists_prop, prod.mk.inj_iff, and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left] @[simp] lemma product_bUnion {Ξ² Ξ³ : Type*} [decidable_eq Ξ³] (s : finset Ξ±) (t : finset Ξ²) (f : Ξ± Γ— Ξ² β†’ finset Ξ³) : (s.product t).bUnion f = s.bUnion (Ξ» a, t.bUnion (Ξ» b, f (a, b))) := by { classical, simp_rw [product_eq_bUnion, bUnion_bUnion, image_bUnion] } @[simp] theorem card_product (s : finset Ξ±) (t : finset Ξ²) : card (s.product t) = card s * card t := multiset.card_product _ _ theorem filter_product (p : Ξ± β†’ Prop) (q : Ξ² β†’ Prop) [decidable_pred p] [decidable_pred q] : (s.product t).filter (Ξ» (x : Ξ± Γ— Ξ²), p x.1 ∧ q x.2) = (s.filter p).product (t.filter q) := by { ext ⟨a, b⟩, simp only [mem_filter, mem_product], finish, } lemma filter_product_card (s : finset Ξ±) (t : finset Ξ²) (p : Ξ± β†’ Prop) (q : Ξ² β†’ Prop) [decidable_pred p] [decidable_pred q] : ((s.product t).filter (Ξ» (x : Ξ± Γ— Ξ²), p x.1 ↔ q x.2)).card = (s.filter p).card * (t.filter q).card + (s.filter (not ∘ p)).card * (t.filter (not ∘ q)).card := begin classical, rw [← card_product, ← card_product, ← filter_product, ← filter_product, ← card_union_eq], { apply congr_arg, ext ⟨a, b⟩, simp only [filter_union_right, mem_filter, mem_product], split; intros; finish, }, { rw disjoint_iff, change _ ∩ _ = βˆ…, ext ⟨a, b⟩, rw mem_inter, finish, }, end lemma empty_product (t : finset Ξ²) : (βˆ… : finset Ξ±).product t = βˆ… := rfl lemma product_empty (s : finset Ξ±) : s.product (βˆ… : finset Ξ²) = βˆ… := eq_empty_of_forall_not_mem (Ξ» x h, (finset.mem_product.1 h).2) end prod /-! ### sigma -/ section sigma variables {Οƒ : Ξ± β†’ Type*} {s : finset Ξ±} {t : Ξ a, finset (Οƒ a)} /-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/ protected def sigma (s : finset Ξ±) (t : Ξ a, finset (Οƒ a)) : finset (Ξ£a, Οƒ a) := ⟨_, nodup_sigma s.2 (Ξ» a, (t a).2)⟩ @[simp] theorem mem_sigma {p : sigma Οƒ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma @[simp] theorem sigma_nonempty : (s.sigma t).nonempty ↔ βˆƒ x ∈ s, (t x).nonempty := by simp [finset.nonempty] @[simp] theorem sigma_eq_empty : s.sigma t = βˆ… ↔ βˆ€ x ∈ s, t x = βˆ… := by simp only [← not_nonempty_iff_eq_empty, sigma_nonempty, not_exists] @[mono] theorem sigma_mono {s₁ sβ‚‚ : finset Ξ±} {t₁ tβ‚‚ : Ξ a, finset (Οƒ a)} (H1 : s₁ βŠ† sβ‚‚) (H2 : βˆ€a, t₁ a βŠ† tβ‚‚ a) : s₁.sigma t₁ βŠ† sβ‚‚.sigma tβ‚‚ := Ξ» ⟨x, sx⟩ H, let ⟨H3, H4⟩ := mem_sigma.1 H in mem_sigma.2 ⟨H1 H3, H2 x H4⟩ theorem sigma_eq_bUnion [decidable_eq (Ξ£ a, Οƒ a)] (s : finset Ξ±) (t : Ξ a, finset (Οƒ a)) : s.sigma t = s.bUnion (Ξ»a, (t a).map $ embedding.sigma_mk a) := by { ext ⟨x, y⟩, simp [and.left_comm] } end sigma /-! ### disjoint -/ section disjoint variable [decidable_eq Ξ±] theorem disjoint_left {s t : finset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ s β†’ a βˆ‰ t := by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and, and_imp]; refl theorem disjoint_val {s t : finset Ξ±} : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left theorem disjoint_iff_inter_eq_empty {s t : finset Ξ±} : disjoint s t ↔ s ∩ t = βˆ… := disjoint_iff instance decidable_disjoint (U V : finset Ξ±) : decidable (disjoint U V) := decidable_of_decidable_of_iff (by apply_instance) eq_bot_iff theorem disjoint_right {s t : finset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ t β†’ a βˆ‰ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_iff_ne {s t : finset Ξ±} : disjoint s t ↔ βˆ€ a ∈ s, βˆ€ b ∈ t, a β‰  b := by simp only [disjoint_left, imp_not_comm, forall_eq'] lemma not_disjoint_iff {s t : finset Ξ±} : Β¬disjoint s t ↔ βˆƒ a, a ∈ s ∧ a ∈ t := not_forall.trans $ exists_congr $ Ξ» a, begin rw [finset.inf_eq_inter, finset.mem_inter], exact not_not, end theorem disjoint_of_subset_left {s t u : finset Ξ±} (h : s βŠ† u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (Ξ» x m₁, (disjoint_left.1 d) (h m₁)) theorem disjoint_of_subset_right {s t u : finset Ξ±} (h : t βŠ† u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (Ξ» x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset Ξ±) : disjoint βˆ… s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset Ξ±) : disjoint s βˆ… := disjoint_bot_right @[simp] theorem disjoint_singleton_left {s : finset Ξ±} {a : Ξ±} : disjoint (singleton a) s ↔ a βˆ‰ s := by simp only [disjoint_left, mem_singleton, forall_eq] @[simp] theorem disjoint_singleton_right {s : finset Ξ±} {a : Ξ±} : disjoint s (singleton a) ↔ a βˆ‰ s := disjoint.comm.trans disjoint_singleton_left @[simp] lemma disjoint_singleton {a b : Ξ±} : disjoint ({a} : finset Ξ±) {b} ↔ a β‰  b := by rw [disjoint_singleton_left, mem_singleton] @[simp] theorem disjoint_insert_left {a : Ξ±} {s t : finset Ξ±} : disjoint (insert a s) t ↔ a βˆ‰ t ∧ disjoint s t := by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem disjoint_insert_right {a : Ξ±} {s t : finset Ξ±} : disjoint s (insert a t) ↔ a βˆ‰ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] @[simp] theorem disjoint_union_left {s t u : finset Ξ±} : disjoint (s βˆͺ t) u ↔ disjoint s u ∧ disjoint t u := by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right {s t u : finset Ξ±} : disjoint s (t βˆͺ u) ↔ disjoint s t ∧ disjoint s u := by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib] lemma sdiff_disjoint {s t : finset Ξ±} : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2 lemma disjoint_sdiff {s t : finset Ξ±} : disjoint s (t \ s) := sdiff_disjoint.symm lemma disjoint_sdiff_inter (s t : finset Ξ±) : disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint lemma sdiff_eq_self_iff_disjoint {s t : finset Ξ±} : s \ t = s ↔ disjoint s t := by rw [sdiff_eq_self, subset_empty, disjoint_iff_inter_eq_empty] lemma sdiff_eq_self_of_disjoint {s t : finset Ξ±} (h : disjoint s t) : s \ t = s := sdiff_eq_self_iff_disjoint.2 h lemma disjoint_self_iff_empty (s : finset Ξ±) : disjoint s s ↔ s = βˆ… := disjoint_self lemma disjoint_bUnion_left {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ finset Ξ±) (t : finset Ξ±) : disjoint (s.bUnion f) t ↔ (βˆ€i∈s, disjoint (f i) t) := begin classical, refine s.induction _ _, { simp only [forall_mem_empty_iff, bUnion_empty, disjoint_empty_left] }, { assume i s his ih, simp only [disjoint_union_left, bUnion_insert, his, forall_mem_insert, ih] } end lemma disjoint_bUnion_right {ΞΉ : Type*} (s : finset Ξ±) (t : finset ΞΉ) (f : ΞΉ β†’ finset Ξ±) : disjoint s (t.bUnion f) ↔ (βˆ€i∈t, disjoint s (f i)) := by simpa only [disjoint.comm] using disjoint_bUnion_left t f s @[simp] theorem card_disjoint_union {s t : finset Ξ±} (h : disjoint s t) : card (s βˆͺ t) = card s + card t := by rw [← card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero] theorem card_sdiff {s t : finset Ξ±} (h : s βŠ† t) : card (t \ s) = card t - card s := suffices card (t \ s) = card ((t \ s) βˆͺ s) - card s, by rwa sdiff_union_of_subset h at this, by rw [card_disjoint_union sdiff_disjoint, add_tsub_cancel_right] lemma card_sdiff_add_card {s t : finset Ξ±} : (s \ t).card + t.card = (s βˆͺ t).card := by rw [← card_disjoint_union sdiff_disjoint, sdiff_union_self_eq_union] lemma disjoint_filter {s : finset Ξ±} {p q : Ξ± β†’ Prop} [decidable_pred p] [decidable_pred q] : disjoint (s.filter p) (s.filter q) ↔ (βˆ€ x ∈ s, p x β†’ Β¬ q x) := by split; simp [disjoint_left] {contextual := tt} lemma disjoint_filter_filter {s t : finset Ξ±} {p q : Ξ± β†’ Prop} [decidable_pred p] [decidable_pred q] : (disjoint s t) β†’ disjoint (s.filter p) (t.filter q) := disjoint.mono (filter_subset _ _) (filter_subset _ _) lemma disjoint_iff_disjoint_coe {Ξ± : Type*} {a b : finset Ξ±} [decidable_eq Ξ±] : disjoint a b ↔ disjoint (↑a : set Ξ±) (↑b : set Ξ±) := by { rw [finset.disjoint_left, set.disjoint_left], refl } lemma filter_card_add_filter_neg_card_eq_card {Ξ± : Type*} {s : finset Ξ±} (p : Ξ± β†’ Prop) [decidable_pred p] : (s.filter p).card + (s.filter (not ∘ p)).card = s.card := by { classical, simp [← card_union_eq, filter_union_filter_neg_eq, disjoint_filter], } end disjoint section self_prod variables (s : finset Ξ±) [decidable_eq Ξ±] /-- Given a finite set `s`, the diagonal, `s.diag` is the set of pairs of the form `(a, a)` for `a ∈ s`. -/ def diag := (s.product s).filter (Ξ» (a : Ξ± Γ— Ξ±), a.fst = a.snd) /-- Given a finite set `s`, the off-diagonal, `s.off_diag` is the set of pairs `(a, b)` with `a β‰  b` for `a, b ∈ s`. -/ def off_diag := (s.product s).filter (Ξ» (a : Ξ± Γ— Ξ±), a.fst β‰  a.snd) @[simp] lemma mem_diag (x : Ξ± Γ— Ξ±) : x ∈ s.diag ↔ x.1 ∈ s ∧ x.1 = x.2 := by { simp only [diag, mem_filter, mem_product], split; intros; finish, } @[simp] lemma mem_off_diag (x : Ξ± Γ— Ξ±) : x ∈ s.off_diag ↔ x.1 ∈ s ∧ x.2 ∈ s ∧ x.1 β‰  x.2 := by { simp only [off_diag, mem_filter, mem_product], split; intros; finish, } @[simp] lemma diag_card : (diag s).card = s.card := begin suffices : diag s = s.image (Ξ» a, (a, a)), { rw this, apply card_image_of_inj_on, finish, }, ext ⟨a₁, aβ‚‚βŸ©, rw mem_diag, split; intros; finish, end @[simp] lemma off_diag_card : (off_diag s).card = s.card * s.card - s.card := begin suffices : (diag s).card + (off_diag s).card = s.card * s.card, { nth_rewrite 2 ← s.diag_card, finish, }, rw ← card_product, apply filter_card_add_filter_neg_card_eq_card, end end self_prod /-- Given a set A and a set B inside it, we can shrink A to any appropriate size, and keep B inside it. -/ lemma exists_intermediate_set {A B : finset Ξ±} (i : β„•) (h₁ : i + card B ≀ card A) (hβ‚‚ : B βŠ† A) : βˆƒ (C : finset Ξ±), B βŠ† C ∧ C βŠ† A ∧ card C = i + card B := begin classical, rcases nat.le.dest h₁ with ⟨k, _⟩, clear h₁, induction k with k ih generalizing A, { exact ⟨A, hβ‚‚, subset.refl _, h.symm⟩ }, { have : (A \ B).nonempty, { rw [← card_pos, card_sdiff hβ‚‚, ← h, nat.add_right_comm, add_tsub_cancel_right, nat.add_succ], apply nat.succ_pos }, rcases this with ⟨a, ha⟩, have z : i + card B + k = card (erase A a), { rw [card_erase_of_mem, ← h, nat.add_succ, nat.pred_succ], rw mem_sdiff at ha, exact ha.1 }, rcases ih _ z with ⟨B', hB', B'subA', cards⟩, { exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ }, { rintros t th, apply mem_erase_of_ne_of_mem _ (hβ‚‚ th), rintro rfl, exact not_mem_sdiff_of_mem_right th ha } } end /-- We can shrink A to any smaller size. -/ lemma exists_smaller_set (A : finset Ξ±) (i : β„•) (h₁ : i ≀ card A) : βˆƒ (B : finset Ξ±), B βŠ† A ∧ card B = i := let ⟨B, _, x₁, xβ‚‚βŸ© := exists_intermediate_set i (by simpa) (empty_subset A) in ⟨B, x₁, xβ‚‚βŸ© /-- `finset.fin_range k` is the finset `{0, 1, ..., k-1}`, as a `finset (fin k)`. -/ def fin_range (k : β„•) : finset (fin k) := ⟨list.fin_range k, list.nodup_fin_range k⟩ @[simp] lemma fin_range_card {k : β„•} : (fin_range k).card = k := by simp [fin_range] @[simp] lemma mem_fin_range {k : β„•} (m : fin k) : m ∈ fin_range k := list.mem_fin_range m @[simp] lemma coe_fin_range (k : β„•) : (fin_range k : set (fin k)) = set.univ := set.eq_univ_of_forall mem_fin_range /-- Given a finset `s` of `β„•` contained in `{0,..., n-1}`, the corresponding finset in `fin n` is `s.attach_fin h` where `h` is a proof that all elements of `s` are less than `n`. -/ def attach_fin (s : finset β„•) {n : β„•} (h : βˆ€ m ∈ s, m < n) : finset (fin n) := ⟨s.1.pmap (Ξ» a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (Ξ» _ _ _ _, fin.veq_of_eq) s.2⟩ @[simp] lemma mem_attach_fin {n : β„•} {s : finset β„•} (h : βˆ€ m ∈ s, m < n) {a : fin n} : a ∈ s.attach_fin h ↔ (a : β„•) ∈ s := ⟨λ h, let ⟨b, hb₁, hbβ‚‚βŸ© := multiset.mem_pmap.1 h in hbβ‚‚ β–Έ hb₁, Ξ» h, multiset.mem_pmap.2 ⟨a, h, fin.eta _ _⟩⟩ @[simp] lemma card_attach_fin {n : β„•} (s : finset β„•) (h : βˆ€ m ∈ s, m < n) : (s.attach_fin h).card = s.card := multiset.card_pmap _ _ _ /-! ### choose -/ section choose variables (p : Ξ± β†’ Prop) [decidable_pred p] (l : finset Ξ±) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : (βˆƒ! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : βˆƒ! a, a ∈ l ∧ p a) : Ξ± := choose_x p l hp lemma choose_spec (hp : βˆƒ! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : βˆƒ! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : βˆƒ! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose theorem lt_wf {Ξ±} : well_founded (@has_lt.lt (finset Ξ±) _) := have H : subrelation (@has_lt.lt (finset Ξ±) _) (inv_image (<) card), from Ξ» x y hxy, card_lt_card hxy, subrelation.wf H $ inv_image.wf _ $ nat.lt_wf end finset namespace equiv /-- Given an equivalence `Ξ±` to `Ξ²`, produce an equivalence between `finset Ξ±` and `finset Ξ²`. -/ protected def finset_congr (e : Ξ± ≃ Ξ²) : finset Ξ± ≃ finset Ξ² := { to_fun := Ξ» s, s.map e.to_embedding, inv_fun := Ξ» s, s.map e.symm.to_embedding, left_inv := Ξ» s, by simp [finset.map_map], right_inv := Ξ» s, by simp [finset.map_map] } @[simp] lemma finset_congr_apply (e : Ξ± ≃ Ξ²) (s : finset Ξ±) : e.finset_congr s = s.map e.to_embedding := rfl @[simp] lemma finset_congr_refl : (equiv.refl Ξ±).finset_congr = equiv.refl _ := by { ext, simp } @[simp] lemma finset_congr_symm (e : Ξ± ≃ Ξ²) : e.finset_congr.symm = e.symm.finset_congr := rfl @[simp] lemma finset_congr_trans (e : Ξ± ≃ Ξ²) (e' : Ξ² ≃ Ξ³) : e.finset_congr.trans (e'.finset_congr) = (e.trans e').finset_congr := by { ext, simp [-finset.mem_map, -equiv.trans_to_embedding] } end equiv namespace multiset variable [decidable_eq Ξ±] theorem to_finset_card_of_nodup {l : multiset Ξ±} (h : l.nodup) : l.to_finset.card = l.card := congr_arg card $ multiset.erase_dup_eq_self.mpr h lemma disjoint_to_finset {m1 m2 : multiset Ξ±} : _root_.disjoint m1.to_finset m2.to_finset ↔ m1.disjoint m2 := begin rw finset.disjoint_iff_ne, split, { intro h, intros a ha1 ha2, rw ← multiset.mem_to_finset at ha1 ha2, exact h _ ha1 _ ha2 rfl }, { rintros h a ha b hb rfl, rw multiset.mem_to_finset at ha hb, exact h ha hb } end end multiset namespace list variable [decidable_eq Ξ±] theorem to_finset_card_of_nodup {l : list Ξ±} (h : l.nodup) : l.to_finset.card = l.length := multiset.to_finset_card_of_nodup h lemma disjoint_to_finset_iff_disjoint {l l' : list Ξ±} : _root_.disjoint l.to_finset l'.to_finset ↔ l.disjoint l' := multiset.disjoint_to_finset end list
ec81a9c624a0ee984a5913ec6ffd67846e96ed62
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Meta/DecLevel.lean
e938946ed21fca2067b5eaea0a166e8fbeecd973
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
2,746
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Basic import Lean.Meta.InferType namespace Lean.Meta structure DecLevelContext where /-- If `true`, then `decAux? ?m` returns a fresh metavariable `?n` s.t. `?m := ?n+1`. -/ canAssignMVars : Bool := true private partial def decAux? : Level β†’ ReaderT DecLevelContext MetaM (Option Level) | Level.zero _ => return none | Level.param _ _ => return none | Level.mvar mvarId _ => do let mctx ← getMCtx match mctx.getLevelAssignment? mvarId with | some u => decAux? u | none => if (← isReadOnlyLevelMVar mvarId) || !(← read).canAssignMVars then return none else let u ← mkFreshLevelMVar trace[Meta.isLevelDefEq.step] "decAux?, {mkLevelMVar mvarId} := {mkLevelSucc u}" assignLevelMVar mvarId (mkLevelSucc u) return u | Level.succ u _ => return u | u => let processMax (u v : Level) : ReaderT DecLevelContext MetaM (Option Level) := do /- Remark: this code uses the fact that `max (u+1) (v+1) = (max u v)+1`. `decAux? (max (u+1) (v+1)) := max (decAux? (u+1)) (decAux? (v+1))` However, we must *not* assign metavariables in the recursive calls since `max ?u 1` is not equivalent to `max ?v 0` where `?v` is a fresh metavariable, and `?u := ?v+1` -/ withReader (fun _ => { canAssignMVars := false }) do match (← decAux? u) with | none => return none | some u => do match (← decAux? v) with | none => return none | some v => return mkLevelMax' u v match u with | Level.max u v _ => processMax u v /- Remark: If `decAux? v` returns `some ...`, then `imax u v` is equivalent to `max u v`. -/ | Level.imax u v _ => processMax u v | _ => unreachable! def decLevel? (u : Level) : MetaM (Option Level) := do let mctx ← getMCtx match (← decAux? u |>.run {}) with | some v => return some v | none => do modify fun s => { s with mctx := mctx } return none def decLevel (u : Level) : MetaM Level := do match (← decLevel? u) with | some u => return u | none => throwError "invalid universe level, {u} is not greater than 0" /- This method is useful for inferring universe level parameters for function that take arguments such as `{Ξ± : Type u}`. Recall that `Type u` is `Sort (u+1)` in Lean. Thus, given `Ξ±`, we must infer its universe level, and then decrement 1 to obtain `u`. -/ def getDecLevel (type : Expr) : MetaM Level := do decLevel (← getLevel type) end Lean.Meta
92de246f46a09eaadc96c82a5a47fcbb68620a43
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/tactic/tfae.lean
85f54bc4cfab226b57389ddc75ae6434c7821608
[ "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
4,978
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Reid Barton, Simon Hudon -/ import data.list.tfae import tactic.scc /-! # The Following Are Equivalent (TFAE) This file provides the tactics `tfae_have` and `tfae_finish` for proving the pairwise equivalence of propositions in a set using various implications between them. -/ open expr tactic lean lean.parser namespace tactic open interactive interactive.types expr export list (tfae) namespace tfae @[derive has_reflect, derive inhabited] inductive arrow : Type | right : arrow | left_right : arrow | left : arrow meta def mk_implication : Ξ  (re : arrow) (e₁ eβ‚‚ : expr), pexpr | arrow.right e₁ eβ‚‚ := ``(%%e₁ β†’ %%eβ‚‚) | arrow.left_right e₁ eβ‚‚ := ``(%%e₁ ↔ %%eβ‚‚) | arrow.left e₁ eβ‚‚ := ``(%%eβ‚‚ β†’ %%e₁) meta def mk_name : Ξ  (re : arrow) (i₁ iβ‚‚ : nat), name | arrow.right i₁ iβ‚‚ := ("tfae_" ++ to_string i₁ ++ "_to_" ++ to_string iβ‚‚ : string) | arrow.left_right i₁ iβ‚‚ := ("tfae_" ++ to_string i₁ ++ "_iff_" ++ to_string iβ‚‚ : string) | arrow.left i₁ iβ‚‚ := ("tfae_" ++ to_string iβ‚‚ ++ "_to_" ++ to_string i₁ : string) end tfae namespace interactive open tactic.tfae list meta def parse_list : expr β†’ option (list expr) | `([]) := pure [] | `(%%e :: %%es) := (::) e <$> parse_list es | _ := none /-- In a goal of the form `tfae [aβ‚€, a₁, aβ‚‚]`, `tfae_have : i β†’ j` creates the assertion `aα΅’ β†’ aβ±Ό`. The other possible notations are `tfae_have : i ← j` and `tfae_have : i ↔ j`. The user can also provide a label for the assertion, as with `have`: `tfae_have h : i ↔ j`. -/ meta def tfae_have (h : parse $ optional ident <* tk ":") (i₁ : parse (with_desc "i" small_nat)) (re : parse (((tk "β†’" <|> tk "->") *> return arrow.right) <|> ((tk "↔" <|> tk "<->") *> return arrow.left_right) <|> ((tk "←" <|> tk "<-") *> return arrow.left))) (iβ‚‚ : parse (with_desc "j" small_nat)) : tactic unit := do `(tfae %%l) <- target, l ← parse_list l, e₁ ← list.nth l (i₁ - 1) <|> fail format!"index {i₁} is not between 1 and {l.length}", eβ‚‚ ← list.nth l (iβ‚‚ - 1) <|> fail format!"index {iβ‚‚} is not between 1 and {l.length}", type ← to_expr (tfae.mk_implication re e₁ eβ‚‚), let h := h.get_or_else (mk_name re i₁ iβ‚‚), tactic.assert h type, return () /-- Finds all implications and equivalences in the context to prove a goal of the form `tfae [...]`. -/ meta def tfae_finish : tactic unit := applyc ``tfae_nil <|> closure.with_new_closure (Ξ» cl, do impl_graph.mk_scc cl, `(tfae %%l) ← target, l ← parse_list l, (_,r,_) ← cl.root l.head, refine ``(tfae_of_forall %%r _ _), thm ← mk_const ``forall_mem_cons, l.mmap' (Ξ» e, do rewrite_target thm, split, (_,r',p) ← cl.root e, tactic.exact p ), applyc ``forall_mem_nil, pure ()) end interactive end tactic /-- The `tfae` tactic suite is a set of tactics that help with proving that certain propositions are equivalent. In `data/list/basic.lean` there is a section devoted to propositions of the form ```lean tfae [p1, p2, ..., pn] ``` where `p1`, `p2`, through, `pn` are terms of type `Prop`. This proposition asserts that all the `pi` are pairwise equivalent. There are results that allow to extract the equivalence of two propositions `pi` and `pj`. To prove a goal of the form `tfae [p1, p2, ..., pn]`, there are two tactics. The first tactic is `tfae_have`. As an argument it takes an expression of the form `i arrow j`, where `i` and `j` are two positive natural numbers, and `arrow` is an arrow such as `β†’`, `->`, `←`, `<-`, `↔`, or `<->`. The tactic `tfae_have : i arrow j` sets up a subgoal in which the user has to prove the equivalence (or implication) of `pi` and `pj`. The remaining tactic, `tfae_finish`, is a finishing tactic. It collects all implications and equivalences from the local context and computes their transitive closure to close the main goal. `tfae_have` and `tfae_finish` can be used together in a proof as follows: ```lean example (a b c d : Prop) : tfae [a,b,c,d] := begin tfae_have : 3 β†’ 1, { /- prove c β†’ a -/ }, tfae_have : 2 β†’ 3, { /- prove b β†’ c -/ }, tfae_have : 2 ← 1, { /- prove a β†’ b -/ }, tfae_have : 4 ↔ 2, { /- prove d ↔ b -/ }, -- a b c d : Prop, -- tfae_3_to_1 : c β†’ a, -- tfae_2_to_3 : b β†’ c, -- tfae_1_to_2 : a β†’ b, -- tfae_4_iff_2 : d ↔ b -- ⊒ tfae [a, b, c, d] tfae_finish, end ``` -/ add_tactic_doc { name := "tfae", category := doc_category.tactic, decl_names := [`tactic.interactive.tfae_have, `tactic.interactive.tfae_finish], tags := ["logic"], inherit_description_from := `tactic.interactive.tfae_finish }
f49af3bbfdc8cd74d78ca9999ebbc7c65fd3afe4
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/ring_theory/ideals.lean
9cbbbc33a52909c3552090e4e0edab4cd99b5695
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
18,912
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Mario Carneiro -/ import algebra.associated linear_algebra.basic order.zorn universes u v variables {Ξ± : Type u} {Ξ² : Type v} {a b : Ξ±} open set function lattice open_locale classical namespace ideal variables [comm_ring Ξ±] (I : ideal Ξ±) @[ext] lemma ext {I J : ideal Ξ±} (h : βˆ€ x, x ∈ I ↔ x ∈ J) : I = J := submodule.ext h theorem eq_top_of_unit_mem (x y : Ξ±) (hx : x ∈ I) (h : y * x = 1) : I = ⊀ := eq_top_iff.2 $ Ξ» z _, calc z = z * (y * x) : by simp [h] ... = (z * y) * x : eq.symm $ mul_assoc z y x ... ∈ I : I.mul_mem_left hx theorem eq_top_of_is_unit_mem {x} (hx : x ∈ I) (h : is_unit x) : I = ⊀ := let ⟨y, hy⟩ := is_unit_iff_exists_inv'.1 h in eq_top_of_unit_mem I x y hx hy theorem eq_top_iff_one : I = ⊀ ↔ (1:Ξ±) ∈ I := ⟨by rintro rfl; trivial, Ξ» h, eq_top_of_unit_mem _ _ 1 h (by simp)⟩ theorem ne_top_iff_one : I β‰  ⊀ ↔ (1:Ξ±) βˆ‰ I := not_congr I.eq_top_iff_one def span (s : set Ξ±) : ideal Ξ± := submodule.span Ξ± s lemma subset_span {s : set Ξ±} : s βŠ† span s := submodule.subset_span lemma span_le {s : set Ξ±} {I} : span s ≀ I ↔ s βŠ† I := submodule.span_le lemma span_mono {s t : set Ξ±} : s βŠ† t β†’ span s ≀ span t := submodule.span_mono @[simp] lemma span_eq : span (I : set Ξ±) = I := submodule.span_eq _ @[simp] lemma span_singleton_one : span ({1} : set Ξ±) = ⊀ := (eq_top_iff_one _).2 $ subset_span $ mem_singleton _ lemma mem_span_insert {s : set Ξ±} {x y} : x ∈ span (insert y s) ↔ βˆƒ a (z ∈ span s), x = a * y + z := submodule.mem_span_insert lemma mem_span_insert' {s : set Ξ±} {x y} : x ∈ span (insert y s) ↔ βˆƒa, x + a * y ∈ span s := submodule.mem_span_insert' lemma mem_span_singleton' {x y : Ξ±} : x ∈ span ({y} : set Ξ±) ↔ βˆƒ a, a * y = x := submodule.mem_span_singleton lemma mem_span_singleton {x y : Ξ±} : x ∈ span ({y} : set Ξ±) ↔ y ∣ x := mem_span_singleton'.trans $ exists_congr $ Ξ» _, by rw [eq_comm, mul_comm]; refl lemma span_singleton_le_span_singleton {x y : Ξ±} : span ({x} : set Ξ±) ≀ span ({y} : set Ξ±) ↔ y ∣ x := span_le.trans $ singleton_subset_iff.trans mem_span_singleton lemma span_eq_bot {s : set Ξ±} : span s = βŠ₯ ↔ βˆ€ x ∈ s, (x:Ξ±) = 0 := submodule.span_eq_bot lemma span_singleton_eq_bot {x} : span ({x} : set Ξ±) = βŠ₯ ↔ x = 0 := submodule.span_singleton_eq_bot lemma span_singleton_eq_top {x} : span ({x} : set Ξ±) = ⊀ ↔ is_unit x := by rw [is_unit_iff_dvd_one, ← span_singleton_le_span_singleton, span_singleton_one, eq_top_iff] @[class] def is_prime (I : ideal Ξ±) : Prop := I β‰  ⊀ ∧ βˆ€ {x y : Ξ±}, x * y ∈ I β†’ x ∈ I ∨ y ∈ I theorem is_prime.mem_or_mem {I : ideal Ξ±} (hI : I.is_prime) : βˆ€ {x y : Ξ±}, x * y ∈ I β†’ x ∈ I ∨ y ∈ I := hI.2 theorem is_prime.mem_or_mem_of_mul_eq_zero {I : ideal Ξ±} (hI : I.is_prime) {x y : Ξ±} (h : x * y = 0) : x ∈ I ∨ y ∈ I := hI.2 (h.symm β–Έ I.zero_mem) theorem is_prime.mem_of_pow_mem {I : ideal Ξ±} (hI : I.is_prime) {r : Ξ±} (n : β„•) (H : r^n ∈ I) : r ∈ I := begin induction n with n ih, { exact (mt (eq_top_iff_one _).2 hI.1).elim H }, exact or.cases_on (hI.mem_or_mem H) id ih end theorem zero_ne_one_of_proper {I : ideal Ξ±} (h : I β‰  ⊀) : (0:Ξ±) β‰  1 := Ξ» hz, I.ne_top_iff_one.1 h $ hz β–Έ I.zero_mem theorem span_singleton_prime {p : Ξ±} (hp : p β‰  0) : is_prime (span ({p} : set Ξ±)) ↔ prime p := by simp [is_prime, prime, span_singleton_eq_top, hp, mem_span_singleton] @[class] def is_maximal (I : ideal Ξ±) : Prop := I β‰  ⊀ ∧ βˆ€ J, I < J β†’ J = ⊀ theorem is_maximal_iff {I : ideal Ξ±} : I.is_maximal ↔ (1:Ξ±) βˆ‰ I ∧ βˆ€ (J : ideal Ξ±) x, I ≀ J β†’ x βˆ‰ I β†’ x ∈ J β†’ (1:Ξ±) ∈ J := and_congr I.ne_top_iff_one $ forall_congr $ Ξ» J, by rw [lt_iff_le_not_le]; exact ⟨λ H x h hx₁ hxβ‚‚, J.eq_top_iff_one.1 $ H ⟨h, not_subset.2 ⟨_, hxβ‚‚, hxβ‚βŸ©βŸ©, Ξ» H ⟨h₁, hβ‚‚βŸ©, let ⟨x, xJ, xI⟩ := not_subset.1 hβ‚‚ in J.eq_top_iff_one.2 $ H x h₁ xI xJ⟩ theorem is_maximal.eq_of_le {I J : ideal Ξ±} (hI : I.is_maximal) (hJ : J β‰  ⊀) (IJ : I ≀ J) : I = J := eq_iff_le_not_lt.2 ⟨IJ, Ξ» h, hJ (hI.2 _ h)⟩ theorem is_maximal.exists_inv {I : ideal Ξ±} (hI : I.is_maximal) {x} (hx : x βˆ‰ I) : βˆƒ y, y * x - 1 ∈ I := begin cases is_maximal_iff.1 hI with H₁ Hβ‚‚, rcases mem_span_insert'.1 (Hβ‚‚ (span (insert x I)) x (set.subset.trans (subset_insert _ _) subset_span) hx (subset_span (mem_insert _ _))) with ⟨y, hy⟩, rw [span_eq, ← neg_mem_iff, add_comm, neg_add', neg_mul_eq_neg_mul] at hy, exact ⟨-y, hy⟩ end theorem is_maximal.is_prime {I : ideal Ξ±} (H : I.is_maximal) : I.is_prime := ⟨H.1, Ξ» x y hxy, or_iff_not_imp_left.2 $ Ξ» hx, begin cases H.exists_inv hx with z hz, have := I.mul_mem_left hz, rw [mul_sub, mul_one, mul_comm, mul_assoc] at this, exact I.neg_mem_iff.1 ((I.add_mem_iff_right $ I.mul_mem_left hxy).1 this) end⟩ instance is_maximal.is_prime' (I : ideal Ξ±) : βˆ€ [H : I.is_maximal], I.is_prime := is_maximal.is_prime theorem exists_le_maximal (I : ideal Ξ±) (hI : I β‰  ⊀) : βˆƒ M : ideal Ξ±, M.is_maximal ∧ I ≀ M := begin rcases zorn.zorn_partial_orderβ‚€ { J : ideal Ξ± | J β‰  ⊀ } _ I hI with ⟨M, M0, IM, h⟩, { refine ⟨M, ⟨M0, Ξ» J hJ, by_contradiction $ Ξ» J0, _⟩, IM⟩, cases h J J0 (le_of_lt hJ), exact lt_irrefl _ hJ }, { intros S SC cC I IS, refine ⟨Sup S, Ξ» H, _, Ξ» _, le_Sup⟩, rcases submodule.mem_Sup_of_directed ((eq_top_iff_one _).1 H) I IS cC.directed_on with ⟨J, JS, J0⟩, exact SC JS ((eq_top_iff_one _).2 J0) } end def is_coprime (x y : Ξ±) : Prop := span ({x, y} : set Ξ±) = ⊀ theorem mem_span_pair {x y z : Ξ±} : z ∈ span (insert y {x} : set Ξ±) ↔ βˆƒ a b, a * x + b * y = z := begin simp only [mem_span_insert, mem_span_singleton', exists_prop], split, { rintros ⟨a, b, ⟨c, hc⟩, h⟩, exact ⟨c, a, by simp [h, hc]⟩ }, { rintro ⟨b, c, e⟩, exact ⟨c, b * x, ⟨b, rfl⟩, by simp [e.symm]⟩ } end theorem is_coprime_def {x y : Ξ±} : is_coprime x y ↔ βˆ€ z, βˆƒ a b, a * x + b * y = z := by simp [is_coprime, submodule.eq_top_iff', mem_span_pair] theorem is_coprime_self {x : Ξ±} : is_coprime x x ↔ is_unit x := by rw [← span_singleton_eq_top]; simp [is_coprime] lemma span_singleton_lt_span_singleton [integral_domain Ξ²] {x y : Ξ²} : span ({x} : set Ξ²) < span ({y} : set Ξ²) ↔ y β‰  0 ∧ βˆƒ d : Ξ², Β¬ is_unit d ∧ x = y * d := by rw [lt_iff_le_not_le, span_singleton_le_span_singleton, span_singleton_le_span_singleton, dvd_and_not_dvd_iff] def quotient (I : ideal Ξ±) := I.quotient namespace quotient variables {I} {x y : Ξ±} def mk (I : ideal Ξ±) (a : Ξ±) : I.quotient := submodule.quotient.mk a protected theorem eq : mk I x = mk I y ↔ x - y ∈ I := submodule.quotient.eq I instance (I : ideal Ξ±) : has_one I.quotient := ⟨mk I 1⟩ @[simp] lemma mk_one (I : ideal Ξ±) : mk I 1 = 1 := rfl instance (I : ideal Ξ±) : has_mul I.quotient := ⟨λ a b, quotient.lift_onβ‚‚' a b (Ξ» a b, mk I (a * b)) $ Ξ» a₁ aβ‚‚ b₁ bβ‚‚ h₁ hβ‚‚, quot.sound $ begin refine calc a₁ * aβ‚‚ - b₁ * bβ‚‚ = aβ‚‚ * (a₁ - b₁) + (aβ‚‚ - bβ‚‚) * b₁ : _ ... ∈ I : I.add_mem (I.mul_mem_left h₁) (I.mul_mem_right hβ‚‚), rw [mul_sub, sub_mul, sub_add_sub_cancel, mul_comm, mul_comm b₁] end⟩ @[simp] theorem mk_mul : mk I (x * y) = mk I x * mk I y := rfl instance (I : ideal Ξ±) : comm_ring I.quotient := { mul := (*), one := 1, mul_assoc := Ξ» a b c, quotient.induction_on₃' a b c $ Ξ» a b c, congr_arg (mk _) (mul_assoc a b c), mul_comm := Ξ» a b, quotient.induction_onβ‚‚' a b $ Ξ» a b, congr_arg (mk _) (mul_comm a b), one_mul := Ξ» a, quotient.induction_on' a $ Ξ» a, congr_arg (mk _) (one_mul a), mul_one := Ξ» a, quotient.induction_on' a $ Ξ» a, congr_arg (mk _) (mul_one a), left_distrib := Ξ» a b c, quotient.induction_on₃' a b c $ Ξ» a b c, congr_arg (mk _) (left_distrib a b c), right_distrib := Ξ» a b c, quotient.induction_on₃' a b c $ Ξ» a b c, congr_arg (mk _) (right_distrib a b c), ..submodule.quotient.add_comm_group I } instance is_ring_hom_mk (I : ideal Ξ±) : is_ring_hom (mk I) := ⟨rfl, Ξ» _ _, rfl, Ξ» _ _, rfl⟩ def map_mk (I J : ideal Ξ±) : ideal I.quotient := { carrier := mk I '' J, zero := ⟨0, J.zero_mem, rfl⟩, add := by rintro _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩; exact ⟨x + y, J.add_mem hx hy, rfl⟩, smul := by rintro ⟨c⟩ _ ⟨x, hx, rfl⟩; exact ⟨c * x, J.mul_mem_left hx, rfl⟩ } @[simp] lemma mk_zero (I : ideal Ξ±) : mk I 0 = 0 := rfl @[simp] lemma mk_add (I : ideal Ξ±) (a b : Ξ±) : mk I (a + b) = mk I a + mk I b := rfl @[simp] lemma mk_neg (I : ideal Ξ±) (a : Ξ±) : mk I (-a : Ξ±) = -mk I a := rfl @[simp] lemma mk_sub (I : ideal Ξ±) (a b : Ξ±) : mk I (a - b : Ξ±) = mk I a - mk I b := rfl @[simp] lemma mk_pow (I : ideal Ξ±) (a : Ξ±) (n : β„•) : mk I (a ^ n : Ξ±) = mk I a ^ n := by induction n; simp [*, pow_succ] lemma eq_zero_iff_mem {I : ideal Ξ±} : mk I a = 0 ↔ a ∈ I := by conv {to_rhs, rw ← sub_zero a }; exact quotient.eq' theorem zero_eq_one_iff {I : ideal Ξ±} : (0 : I.quotient) = 1 ↔ I = ⊀ := eq_comm.trans $ eq_zero_iff_mem.trans (eq_top_iff_one _).symm theorem zero_ne_one_iff {I : ideal Ξ±} : (0 : I.quotient) β‰  1 ↔ I β‰  ⊀ := not_congr zero_eq_one_iff protected def nonzero_comm_ring {I : ideal Ξ±} (hI : I β‰  ⊀) : nonzero_comm_ring I.quotient := { zero_ne_one := zero_ne_one_iff.2 hI, ..quotient.comm_ring I } instance (I : ideal Ξ±) [hI : I.is_prime] : integral_domain I.quotient := { eq_zero_or_eq_zero_of_mul_eq_zero := Ξ» a b, quotient.induction_onβ‚‚' a b $ Ξ» a b hab, (hI.mem_or_mem (eq_zero_iff_mem.1 hab)).elim (or.inl ∘ eq_zero_iff_mem.2) (or.inr ∘ eq_zero_iff_mem.2), ..quotient.nonzero_comm_ring hI.1 } lemma exists_inv {I : ideal Ξ±} [hI : I.is_maximal] : βˆ€ {a : I.quotient}, a β‰  0 β†’ βˆƒ b : I.quotient, a * b = 1 := begin rintro ⟨a⟩ h, cases hI.exists_inv (mt eq_zero_iff_mem.2 h) with b hb, rw [mul_comm] at hb, exact ⟨mk _ b, quot.sound hb⟩ end /-- quotient by maximal ideal is a field. def rather than instance, since users will have computable inverses in some applications -/ protected noncomputable def field (I : ideal Ξ±) [hI : I.is_maximal] : discrete_field I.quotient := { inv := Ξ» a, if ha : a = 0 then 0 else classical.some (exists_inv ha), mul_inv_cancel := Ξ» a (ha : a β‰  0), show a * dite _ _ _ = _, by rw dif_neg ha; exact classical.some_spec (exists_inv ha), inv_mul_cancel := Ξ» a (ha : a β‰  0), show dite _ _ _ * a = _, by rw [mul_comm, dif_neg ha]; exact classical.some_spec (exists_inv ha), inv_zero := dif_pos rfl, has_decidable_eq := classical.dec_eq _, ..quotient.integral_domain I } variable [comm_ring Ξ²] def lift (S : ideal Ξ±) (f : Ξ± β†’ Ξ²) [is_ring_hom f] (H : βˆ€ (a : Ξ±), a ∈ S β†’ f a = 0) : quotient S β†’ Ξ² := Ξ» x, quotient.lift_on' x f $ Ξ» (a b) (h : _ ∈ _), eq_of_sub_eq_zero (by simpa only [is_ring_hom.map_sub f] using H _ h) variables {S : ideal Ξ±} {f : Ξ± β†’ Ξ²} [is_ring_hom f] {H : βˆ€ (a : Ξ±), a ∈ S β†’ f a = 0} @[simp] lemma lift_mk : lift S f H (mk S a) = f a := rfl instance : is_ring_hom (lift S f H) := { map_one := by show lift S f H (mk S 1) = 1; simp [is_ring_hom.map_one f, - mk_one], map_add := Ξ» a₁ aβ‚‚, quotient.induction_onβ‚‚' a₁ aβ‚‚ $ Ξ» a₁ aβ‚‚, begin show lift S f H (mk S a₁ + mk S aβ‚‚) = lift S f H (mk S a₁) + lift S f H (mk S aβ‚‚), have := ideal.quotient.is_ring_hom_mk S, rw ← this.map_add, show lift S f H (mk S (a₁ + aβ‚‚)) = lift S f H (mk S a₁) + lift S f H (mk S aβ‚‚), simp only [lift_mk, is_ring_hom.map_add f], end, map_mul := Ξ» a₁ aβ‚‚, quotient.induction_onβ‚‚' a₁ aβ‚‚ $ Ξ» a₁ aβ‚‚, begin show lift S f H (mk S a₁ * mk S aβ‚‚) = lift S f H (mk S a₁) * lift S f H (mk S aβ‚‚), have := ideal.quotient.is_ring_hom_mk S, rw ← this.map_mul, show lift S f H (mk S (a₁ * aβ‚‚)) = lift S f H (mk S a₁) * lift S f H (mk S aβ‚‚), simp only [lift_mk, is_ring_hom.map_mul f], end } end quotient lemma eq_bot_or_top {K : Type u} [discrete_field K] (I : ideal K) : I = βŠ₯ ∨ I = ⊀ := begin rw classical.or_iff_not_imp_right, change _ β‰  _ β†’ _, rw ideal.ne_top_iff_one, intro h1, rw eq_bot_iff, intros r hr, by_cases H : r = 0, {simpa}, simpa [H, h1] using submodule.smul_mem I r⁻¹ hr, end lemma eq_bot_of_prime {K : Type u} [discrete_field K] (I : ideal K) [h : I.is_prime] : I = βŠ₯ := classical.or_iff_not_imp_right.mp I.eq_bot_or_top h.1 end ideal def nonunits (Ξ± : Type u) [monoid Ξ±] : set Ξ± := { a | Β¬is_unit a } @[simp] theorem mem_nonunits_iff [comm_monoid Ξ±] : a ∈ nonunits Ξ± ↔ Β¬ is_unit a := iff.rfl theorem mul_mem_nonunits_right [comm_monoid Ξ±] : b ∈ nonunits Ξ± β†’ a * b ∈ nonunits Ξ± := mt is_unit_of_mul_is_unit_right theorem mul_mem_nonunits_left [comm_monoid Ξ±] : a ∈ nonunits Ξ± β†’ a * b ∈ nonunits Ξ± := mt is_unit_of_mul_is_unit_left theorem zero_mem_nonunits [semiring Ξ±] : 0 ∈ nonunits Ξ± ↔ (0:Ξ±) β‰  1 := not_congr is_unit_zero_iff @[simp] theorem one_not_mem_nonunits [monoid Ξ±] : (1:Ξ±) βˆ‰ nonunits Ξ± := not_not_intro is_unit_one theorem coe_subset_nonunits [comm_ring Ξ±] {I : ideal Ξ±} (h : I β‰  ⊀) : (I : set Ξ±) βŠ† nonunits Ξ± := Ξ» x hx hu, h $ I.eq_top_of_is_unit_mem hx hu lemma exists_max_ideal_of_mem_nonunits [comm_ring Ξ±] (h : a ∈ nonunits Ξ±) : βˆƒ I : ideal Ξ±, I.is_maximal ∧ a ∈ I := begin have : ideal.span ({a} : set Ξ±) β‰  ⊀, { intro H, rw ideal.span_singleton_eq_top at H, contradiction }, rcases ideal.exists_le_maximal _ this with ⟨I, Imax, H⟩, use [I, Imax], apply H, apply ideal.subset_span, exact set.mem_singleton a end class local_ring (Ξ± : Type u) extends nonzero_comm_ring Ξ± := (is_local : βˆ€ (a : Ξ±), (is_unit a) ∨ (is_unit (1 - a))) namespace local_ring variable [local_ring Ξ±] instance : comm_ring Ξ± := by apply_instance lemma is_unit_or_is_unit_one_sub_self (a : Ξ±) : (is_unit a) ∨ (is_unit (1 - a)) := is_local a lemma is_unit_of_mem_nonunits_one_sub_self (a : Ξ±) (h : (1 - a) ∈ nonunits Ξ±) : is_unit a := or_iff_not_imp_right.1 (is_local a) h lemma is_unit_one_sub_self_of_mem_nonunits (a : Ξ±) (h : a ∈ nonunits Ξ±) : is_unit (1 - a) := or_iff_not_imp_left.1 (is_local a) h lemma nonunits_add {x y} (hx : x ∈ nonunits Ξ±) (hy : y ∈ nonunits Ξ±) : x + y ∈ nonunits Ξ± := begin rintros ⟨u, hu⟩, apply hy, suffices : is_unit ((↑u⁻¹ : Ξ±) * y), { rcases this with ⟨s, hs⟩, use u * s, convert congr_arg (Ξ» z, (u : Ξ±) * z) hs, rw ← mul_assoc, simp }, rw show (↑u⁻¹ * y) = (1 - ↑u⁻¹ * x), { rw eq_sub_iff_add_eq, replace hu := congr_arg (Ξ» z, (↑u⁻¹ : Ξ±) * z) hu, simpa [mul_add] using hu }, apply is_unit_one_sub_self_of_mem_nonunits, exact mul_mem_nonunits_right hx end variable (Ξ±) def nonunits_ideal : ideal Ξ± := { carrier := nonunits Ξ±, zero := zero_mem_nonunits.2 $ zero_ne_one, add := Ξ» x y hx hy, nonunits_add hx hy, smul := Ξ» a x, mul_mem_nonunits_right } instance nonunits_ideal.is_maximal : (nonunits_ideal Ξ±).is_maximal := begin rw ideal.is_maximal_iff, split, { intro h, apply h, exact is_unit_one }, { intros I x hI hx H, erw not_not at hx, rcases hx with ⟨u,rfl⟩, simpa using I.smul_mem ↑u⁻¹ H } end lemma max_ideal_unique : βˆƒ! I : ideal Ξ±, I.is_maximal := ⟨nonunits_ideal Ξ±, nonunits_ideal.is_maximal Ξ±, Ξ» I hI, hI.eq_of_le (nonunits_ideal.is_maximal Ξ±).1 $ Ξ» x hx, hI.1 ∘ I.eq_top_of_is_unit_mem hx⟩ variable {Ξ±} @[simp] lemma mem_nonunits_ideal (x) : x ∈ nonunits_ideal Ξ± ↔ x ∈ nonunits Ξ± := iff.rfl end local_ring def is_local_ring (Ξ± : Type u) [comm_ring Ξ±] : Prop := ((0:Ξ±) β‰  1) ∧ βˆ€ (a : Ξ±), (is_unit a) ∨ (is_unit (1 - a)) def local_of_is_local_ring [comm_ring Ξ±] (h : is_local_ring Ξ±) : local_ring Ξ± := { zero_ne_one := h.1, is_local := h.2, .. β€Ήcomm_ring Ξ±β€Ί } def local_of_unit_or_unit_one_sub [comm_ring Ξ±] (hnze : (0:Ξ±) β‰  1) (h : βˆ€ x : Ξ±, is_unit x ∨ is_unit (1 - x)) : local_ring Ξ± := local_of_is_local_ring ⟨hnze, h⟩ def local_of_nonunits_ideal [comm_ring Ξ±] (hnze : (0:Ξ±) β‰  1) (h : βˆ€ x y ∈ nonunits Ξ±, x + y ∈ nonunits Ξ±) : local_ring Ξ± := local_of_is_local_ring ⟨hnze, Ξ» x, or_iff_not_imp_left.mpr $ Ξ» hx, begin by_contra H, apply h _ _ hx H, simp [-sub_eq_add_neg, add_sub_cancel'_right] end⟩ def local_of_unique_max_ideal [comm_ring Ξ±] (h : βˆƒ! I : ideal Ξ±, I.is_maximal) : local_ring Ξ± := local_of_nonunits_ideal (let ⟨I, Imax, _⟩ := h in (Ξ» (H : 0 = 1), Imax.1 $ I.eq_top_iff_one.2 $ H β–Έ I.zero_mem)) $ Ξ» x y hx hy H, let ⟨I, Imax, Iuniq⟩ := h in let ⟨Ix, Ixmax, Hx⟩ := exists_max_ideal_of_mem_nonunits hx in let ⟨Iy, Iymax, Hy⟩ := exists_max_ideal_of_mem_nonunits hy in have xmemI : x ∈ I, from ((Iuniq Ix Ixmax) β–Έ Hx), have ymemI : y ∈ I, from ((Iuniq Iy Iymax) β–Έ Hy), Imax.1 $ I.eq_top_of_is_unit_mem (I.add_mem xmemI ymemI) H class is_local_ring_hom [comm_ring Ξ±] [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) extends is_ring_hom f : Prop := (map_nonunit : βˆ€ a, is_unit (f a) β†’ is_unit a) @[simp] lemma is_unit_of_map_unit [comm_ring Ξ±] [comm_ring Ξ²] (f : Ξ± β†’ Ξ²) [is_local_ring_hom f] (a) (h : is_unit (f a)) : is_unit a := is_local_ring_hom.map_nonunit a h section open local_ring variables [local_ring Ξ±] [local_ring Ξ²] variables (f : Ξ± β†’ Ξ²) [is_local_ring_hom f] lemma map_nonunit (a) (h : a ∈ nonunits_ideal Ξ±) : f a ∈ nonunits_ideal Ξ² := Ξ» H, h $ is_unit_of_map_unit f a H end namespace local_ring variables [local_ring Ξ±] [local_ring Ξ²] variable (Ξ±) def residue_field := (nonunits_ideal Ξ±).quotient namespace residue_field noncomputable instance : discrete_field (residue_field Ξ±) := ideal.quotient.field (nonunits_ideal Ξ±) variables {Ξ± Ξ²} noncomputable def map (f : Ξ± β†’ Ξ²) [is_local_ring_hom f] : residue_field Ξ± β†’ residue_field Ξ² := ideal.quotient.lift (nonunits_ideal Ξ±) (ideal.quotient.mk _ ∘ f) $ Ξ» a ha, begin erw ideal.quotient.eq_zero_iff_mem, exact map_nonunit f a ha end instance map.is_field_hom (f : Ξ± β†’ Ξ²) [is_local_ring_hom f] : is_field_hom (map f) := ideal.quotient.is_ring_hom end residue_field end local_ring namespace discrete_field variables [discrete_field Ξ±] instance : local_ring Ξ± := { is_local := Ξ» a, if h : a = 0 then or.inr (by rw [h, sub_zero]; exact is_unit_one) else or.inl $ is_unit_of_mul_one a a⁻¹ $ div_self h } end discrete_field
dbd990e1fb851417df7c14681d5a99c4caad9fbc
2c096fdfecf64e46ea7bc6ce5521f142b5926864
/src/Lean/Server/Requests.lean
43e1eacea00644a1c705d05b0e925a6b8c122cdc
[ "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
10,208
lean
/- Copyright (c) 2021 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki, Marc Huisinga -/ import Lean.DeclarationRange import Lean.Data.Json import Lean.Data.Lsp import Lean.Elab.Command import Lean.Server.FileSource import Lean.Server.FileWorker.Utils import Lean.Server.Rpc.Basic namespace Lean.Server structure RequestError where code : JsonRpc.ErrorCode message : String deriving Inhabited namespace RequestError open JsonRpc def fileChanged : RequestError := { code := ErrorCode.contentModified message := "File changed." } def methodNotFound (method : String) : RequestError := { code := ErrorCode.methodNotFound message := s!"No request handler found for '{method}'" } def invalidParams (message : String) : RequestError := {code := ErrorCode.invalidParams, message} def internalError (message : String) : RequestError := { code := ErrorCode.internalError, message } def ofException (e : Lean.Exception) : IO RequestError := return internalError (← e.toMessageData.toString) def ofIoError (e : IO.Error) : RequestError := internalError (toString e) def toLspResponseError (id : RequestID) (e : RequestError) : ResponseError Unit := { id := id code := e.code message := e.message } end RequestError def parseRequestParams (paramType : Type) [FromJson paramType] (params : Json) : Except RequestError paramType := fromJson? params |>.mapError fun inner => { code := JsonRpc.ErrorCode.parseError message := s!"Cannot parse request params: {params.compress}\n{inner}" } structure RequestContext where rpcSessions : RBMap UInt64 (IO.Ref FileWorker.RpcSession) compare srcSearchPath : SearchPath doc : FileWorker.EditableDocument hLog : IO.FS.Stream hOut : IO.FS.Stream initParams : Lsp.InitializeParams abbrev RequestTask Ξ± := Task (Except RequestError Ξ±) abbrev RequestT m := ReaderT RequestContext <| ExceptT RequestError m /-- Workers execute request handlers in this monad. -/ abbrev RequestM := ReaderT RequestContext <| EIO RequestError abbrev RequestTask.pure (a : Ξ±) : RequestTask Ξ± := .pure (.ok a) instance : MonadLift IO RequestM where monadLift x := do match ← x.toBaseIO with | .error e => throw <| RequestError.ofIoError e | .ok v => return v instance : MonadLift (EIO Exception) RequestM where monadLift x := do match ← x.toBaseIO with | .error e => throw <| ← RequestError.ofException e | .ok v => return v namespace RequestM open FileWorker open Snapshots def readDoc [Monad m] [MonadReaderOf RequestContext m] : m EditableDocument := do let rc ← readThe RequestContext return rc.doc def asTask (t : RequestM Ξ±) : RequestM (RequestTask Ξ±) := do let rc ← readThe RequestContext let t ← EIO.asTask <| t.run rc return t.map liftExcept def mapTask (t : Task Ξ±) (f : Ξ± β†’ RequestM Ξ²) : RequestM (RequestTask Ξ²) := do let rc ← readThe RequestContext let t ← EIO.mapTask (f Β· rc) t return t.map liftExcept def bindTask (t : Task Ξ±) (f : Ξ± β†’ RequestM (RequestTask Ξ²)) : RequestM (RequestTask Ξ²) := do let rc ← readThe RequestContext EIO.bindTask t (f Β· rc) def waitFindSnapAux (notFoundX : RequestM Ξ±) (x : Snapshot β†’ RequestM Ξ±) : Except ElabTaskError (Option Snapshot) β†’ RequestM Ξ± /- The elaboration task that we're waiting for may be aborted if the file contents change. In that case, we reply with the `fileChanged` error. Thanks to this, the server doesn't get bogged down in requests for an old state of the document. -/ | Except.error FileWorker.ElabTaskError.aborted => throwThe RequestError RequestError.fileChanged | Except.error (FileWorker.ElabTaskError.ioError e) => throw (RequestError.ofIoError e) | Except.ok none => notFoundX | Except.ok (some snap) => x snap /-- Create a task which waits for the first snapshot matching `p`, handles various errors, and if a matching snapshot was found executes `x` with it. If not found, the task executes `notFoundX`. -/ def withWaitFindSnap (doc : EditableDocument) (p : Snapshot β†’ Bool) (notFoundX : RequestM Ξ²) (x : Snapshot β†’ RequestM Ξ²) : RequestM (RequestTask Ξ²) := do let findTask := doc.cmdSnaps.waitFind? p mapTask findTask <| waitFindSnapAux notFoundX x /-- See `withWaitFindSnap`. -/ def bindWaitFindSnap (doc : EditableDocument) (p : Snapshot β†’ Bool) (notFoundX : RequestM (RequestTask Ξ²)) (x : Snapshot β†’ RequestM (RequestTask Ξ²)) : RequestM (RequestTask Ξ²) := do let findTask := doc.cmdSnaps.waitFind? p bindTask findTask <| waitFindSnapAux notFoundX x /-- Create a task which waits for the snapshot containing `lspPos` and executes `f` with it. If no such snapshot exists, the request fails with an error. -/ def withWaitFindSnapAtPos (lspPos : Lsp.Position) (f : Snapshots.Snapshot β†’ RequestM Ξ±) : RequestM (RequestTask Ξ±) := do let doc ← readDoc let pos := doc.meta.text.lspPosToUtf8Pos lspPos withWaitFindSnap doc (fun s => s.endPos >= pos) (notFoundX := throw ⟨.invalidParams, s!"no snapshot found at {lspPos}"⟩) (x := f) open Elab.Command in def runCommandElabM (snap : Snapshot) (c : RequestT CommandElabM Ξ±) : RequestM Ξ± := do let rc ← readThe RequestContext match ← snap.runCommandElabM rc.doc.meta (c.run rc) with | .ok v => return v | .error e => throw e def runCoreM (snap : Snapshot) (c : RequestT CoreM Ξ±) : RequestM Ξ± := do let rc ← readThe RequestContext match ← snap.runCoreM rc.doc.meta (c.run rc) with | .ok v => return v | .error e => throw e open Elab.Term in def runTermElabM (snap : Snapshot) (c : RequestT TermElabM Ξ±) : RequestM Ξ± := do let rc ← readThe RequestContext match ← snap.runTermElabM rc.doc.meta (c.run rc) with | .ok v => return v | .error e => throw e end RequestM /-! # The global request handlers table We maintain a global map of LSP request handlers. This allows user code such as plugins to register its own handlers, for example to support ITP functionality such as goal state visualization. For details of how to register one, see `registerLspRequestHandler`. -/ section HandlerTable open Lsp structure RequestHandler where fileSource : Json β†’ Except RequestError Lsp.DocumentUri handle : Json β†’ RequestM (RequestTask Json) builtin_initialize requestHandlers : IO.Ref (PersistentHashMap String RequestHandler) ← IO.mkRef {} /-- NB: This method may only be called in `initialize` blocks (user or builtin). A registration consists of: - a type of JSON-parsable request data `paramType` - a `FileSource` instance for it so the system knows where to route requests - a type of JSON-serializable response data `respType` - an actual `handler` which runs in the `RequestM` monad and is expected to produce an asynchronous `RequestTask` which does any waiting/computation A handler task may be cancelled at any time, so it should check the cancellation token when possible to handle this cooperatively. Any exceptions thrown in a request handler will be reported to the client as LSP error responses. -/ def registerLspRequestHandler (method : String) paramType [FromJson paramType] [FileSource paramType] respType [ToJson respType] (handler : paramType β†’ RequestM (RequestTask respType)) : IO Unit := do if !(← Lean.initializing) then throw <| IO.userError s!"Failed to register LSP request handler for '{method}': only possible during initialization" if (← requestHandlers.get).contains method then throw <| IO.userError s!"Failed to register LSP request handler for '{method}': already registered" let fileSource := fun j => parseRequestParams paramType j |>.map Lsp.fileSource let handle := fun j => do let params ← liftExcept <| parseRequestParams paramType j let t ← handler params pure <| t.map <| Except.map ToJson.toJson requestHandlers.modify fun rhs => rhs.insert method { fileSource, handle } def lookupLspRequestHandler (method : String) : IO (Option RequestHandler) := return (← requestHandlers.get).find? method /-- NB: This method may only be called in `initialize` blocks (user or builtin). Register another handler to invoke after the last one registered for a method. At least one handler for the method must have already been registered to perform chaining. For more details on the registration of a handler, see `registerLspRequestHandler`. -/ def chainLspRequestHandler (method : String) paramType [FromJson paramType] respType [FromJson respType] [ToJson respType] (handler : paramType β†’ RequestTask respType β†’ RequestM (RequestTask respType)) : IO Unit := do if !(← Lean.initializing) then throw <| IO.userError s!"Failed to chain LSP request handler for '{method}': only possible during initialization" if let some oldHandler ← lookupLspRequestHandler method then let handle := fun j => do let t ← oldHandler.handle j let t := t.map fun x => x.bind fun j => FromJson.fromJson? j |>.mapError fun e => .internalError s!"Failed to parse original LSP response for `{method}` when chaining: {e}" let params ← liftExcept <| parseRequestParams paramType j let t ← handler params t pure <| t.map <| Except.map ToJson.toJson requestHandlers.modify fun rhs => rhs.insert method {oldHandler with handle} else throw <| IO.userError s!"Failed to chain LSP request handler for '{method}': no initial handler registered" def routeLspRequest (method : String) (params : Json) : IO (Except RequestError DocumentUri) := do match (← lookupLspRequestHandler method) with | none => return Except.error <| RequestError.methodNotFound method | some rh => return rh.fileSource params def handleLspRequest (method : String) (params : Json) : RequestM (RequestTask Json) := do match (← lookupLspRequestHandler method) with | none => throw <| .internalError s!"request '{method}' routed through watchdog but unknown in worker; are both using the same plugins?" | some rh => rh.handle params end HandlerTable end Lean.Server
9752d27944f96c44ba6819cd83e883513b2e2fc8
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/hit/colimit.hlean
93a938e16785e3384e8548afc64ad89238603a5e
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
7,589
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Definition of general colimits and sequential colimits. -/ /- definition of a general colimit -/ open eq nat quotient sigma equiv equiv.ops is_trunc namespace colimit section parameters {I J : Type} (A : I β†’ Type) (dom cod : J β†’ I) (f : Ξ (j : J), A (dom j) β†’ A (cod j)) variables {i : I} (a : A i) (j : J) (b : A (dom j)) local abbreviation B := Ξ£(i : I), A i inductive colim_rel : B β†’ B β†’ Type := | Rmk : Ξ {j : J} (a : A (dom j)), colim_rel ⟨cod j, f j a⟩ ⟨dom j, a⟩ open colim_rel local abbreviation R := colim_rel -- TODO: define this in root namespace definition colimit : Type := quotient colim_rel definition incl : colimit := class_of R ⟨i, a⟩ abbreviation ΞΉ := @incl definition cglue : ΞΉ (f j b) = ΞΉ b := eq_of_rel colim_rel (Rmk f b) protected definition rec {P : colimit β†’ Type} (Pincl : Π⦃i : I⦄ (x : A i), P (ΞΉ x)) (Pglue : Ξ (j : J) (x : A (dom j)), Pincl (f j x) =[cglue j x] Pincl x) (y : colimit) : P y := begin fapply (quotient.rec_on y), { intro a, cases a, apply Pincl}, { intro a a' H, cases H, apply Pglue} end protected definition rec_on [reducible] {P : colimit β†’ Type} (y : colimit) (Pincl : Π⦃i : I⦄ (x : A i), P (ΞΉ x)) (Pglue : Ξ (j : J) (x : A (dom j)), Pincl (f j x) =[cglue j x] Pincl x) : P y := rec Pincl Pglue y theorem rec_cglue {P : colimit β†’ Type} (Pincl : Π⦃i : I⦄ (x : A i), P (ΞΉ x)) (Pglue : Ξ (j : J) (x : A (dom j)), Pincl (f j x) =[cglue j x] Pincl x) {j : J} (x : A (dom j)) : apdo (rec Pincl Pglue) (cglue j x) = Pglue j x := !rec_eq_of_rel protected definition elim {P : Type} (Pincl : Π⦃i : I⦄ (x : A i), P) (Pglue : Ξ (j : J) (x : A (dom j)), Pincl (f j x) = Pincl x) (y : colimit) : P := rec Pincl (Ξ»j a, pathover_of_eq (Pglue j a)) y protected definition elim_on [reducible] {P : Type} (y : colimit) (Pincl : Π⦃i : I⦄ (x : A i), P) (Pglue : Ξ (j : J) (x : A (dom j)), Pincl (f j x) = Pincl x) : P := elim Pincl Pglue y theorem elim_cglue {P : Type} (Pincl : Π⦃i : I⦄ (x : A i), P) (Pglue : Ξ (j : J) (x : A (dom j)), Pincl (f j x) = Pincl x) {j : J} (x : A (dom j)) : ap (elim Pincl Pglue) (cglue j x) = Pglue j x := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (cglue j x)), rewrite [β–Έ*,-apdo_eq_pathover_of_eq_ap,↑elim,rec_cglue], end protected definition elim_type (Pincl : Π⦃i : I⦄ (x : A i), Type) (Pglue : Ξ (j : J) (x : A (dom j)), Pincl (f j x) ≃ Pincl x) (y : colimit) : Type := elim Pincl (Ξ»j a, ua (Pglue j a)) y protected definition elim_type_on [reducible] (y : colimit) (Pincl : Π⦃i : I⦄ (x : A i), Type) (Pglue : Ξ (j : J) (x : A (dom j)), Pincl (f j x) ≃ Pincl x) : Type := elim_type Pincl Pglue y theorem elim_type_cglue (Pincl : Π⦃i : I⦄ (x : A i), Type) (Pglue : Ξ (j : J) (x : A (dom j)), Pincl (f j x) ≃ Pincl x) {j : J} (x : A (dom j)) : transport (elim_type Pincl Pglue) (cglue j x) = Pglue j x := by rewrite [tr_eq_cast_ap_fn,↑elim_type,elim_cglue];apply cast_ua_fn protected definition rec_hprop {P : colimit β†’ Type} [H : Ξ x, is_hprop (P x)] (Pincl : Π⦃i : I⦄ (x : A i), P (ΞΉ x)) (y : colimit) : P y := rec Pincl (Ξ»a b, !is_hprop.elimo) y protected definition elim_hprop {P : Type} [H : is_hprop P] (Pincl : Π⦃i : I⦄ (x : A i), P) (y : colimit) : P := elim Pincl (Ξ»a b, !is_hprop.elim) y end end colimit /- definition of a sequential colimit -/ namespace seq_colim section /- we define it directly in terms of quotients. An alternative definition could be definition seq_colim := colimit.colimit A id succ f -/ parameters {A : β„• β†’ Type} (f : Π⦃n⦄, A n β†’ A (succ n)) variables {n : β„•} (a : A n) local abbreviation B := Ξ£(n : β„•), A n inductive seq_rel : B β†’ B β†’ Type := | Rmk : Ξ {n : β„•} (a : A n), seq_rel ⟨succ n, f a⟩ ⟨n, a⟩ open seq_rel local abbreviation R := seq_rel -- TODO: define this in root namespace definition seq_colim : Type := quotient seq_rel definition inclusion : seq_colim := class_of R ⟨n, a⟩ abbreviation sΞΉ := @inclusion definition glue : sΞΉ (f a) = sΞΉ a := eq_of_rel seq_rel (Rmk f a) protected definition rec {P : seq_colim β†’ Type} (Pincl : Π⦃n : ℕ⦄ (a : A n), P (sΞΉ a)) (Pglue : Ξ (n : β„•) (a : A n), Pincl (f a) =[glue a] Pincl a) (aa : seq_colim) : P aa := begin fapply (quotient.rec_on aa), { intro a, cases a, apply Pincl}, { intro a a' H, cases H, apply Pglue} end protected definition rec_on [reducible] {P : seq_colim β†’ Type} (aa : seq_colim) (Pincl : Π⦃n : ℕ⦄ (a : A n), P (sΞΉ a)) (Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) =[glue a] Pincl a) : P aa := rec Pincl Pglue aa theorem rec_glue {P : seq_colim β†’ Type} (Pincl : Π⦃n : ℕ⦄ (a : A n), P (sΞΉ a)) (Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) =[glue a] Pincl a) {n : β„•} (a : A n) : apdo (rec Pincl Pglue) (glue a) = Pglue a := !rec_eq_of_rel protected definition elim {P : Type} (Pincl : Π⦃n : ℕ⦄ (a : A n), P) (Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) = Pincl a) : seq_colim β†’ P := rec Pincl (Ξ»n a, pathover_of_eq (Pglue a)) protected definition elim_on [reducible] {P : Type} (aa : seq_colim) (Pincl : Π⦃n : ℕ⦄ (a : A n), P) (Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) = Pincl a) : P := elim Pincl Pglue aa theorem elim_glue {P : Type} (Pincl : Π⦃n : ℕ⦄ (a : A n), P) (Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) = Pincl a) {n : β„•} (a : A n) : ap (elim Pincl Pglue) (glue a) = Pglue a := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (glue a)), rewrite [β–Έ*,-apdo_eq_pathover_of_eq_ap,↑elim,rec_glue], end protected definition elim_type (Pincl : Π⦃n : ℕ⦄ (a : A n), Type) (Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) ≃ Pincl a) : seq_colim β†’ Type := elim Pincl (Ξ»n a, ua (Pglue a)) protected definition elim_type_on [reducible] (aa : seq_colim) (Pincl : Π⦃n : ℕ⦄ (a : A n), Type) (Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) ≃ Pincl a) : Type := elim_type Pincl Pglue aa theorem elim_type_glue (Pincl : Π⦃n : ℕ⦄ (a : A n), Type) (Pglue : Π⦃n : ℕ⦄ (a : A n), Pincl (f a) ≃ Pincl a) {n : β„•} (a : A n) : transport (elim_type Pincl Pglue) (glue a) = Pglue a := by rewrite [tr_eq_cast_ap_fn,↑elim_type,elim_glue];apply cast_ua_fn protected definition rec_hprop {P : seq_colim β†’ Type} [H : Ξ x, is_hprop (P x)] (Pincl : Π⦃n : ℕ⦄ (a : A n), P (sΞΉ a)) (aa : seq_colim) : P aa := rec Pincl (Ξ»a b, !is_hprop.elimo) aa protected definition elim_hprop {P : Type} [H : is_hprop P] (Pincl : Π⦃n : ℕ⦄ (a : A n), P) : seq_colim β†’ P := elim Pincl (Ξ»a b, !is_hprop.elim) end end seq_colim attribute colimit.incl seq_colim.inclusion [constructor] attribute colimit.rec colimit.elim [unfold 10] [recursor 10] attribute colimit.elim_type [unfold 9] attribute colimit.rec_on colimit.elim_on [unfold 8] attribute colimit.elim_type_on [unfold 7] attribute seq_colim.rec seq_colim.elim [unfold 6] [recursor 6] attribute seq_colim.elim_type [unfold 5] attribute seq_colim.rec_on seq_colim.elim_on [unfold 4] attribute seq_colim.elim_type_on [unfold 3]
3a2c5196d291d2b8aee7ee72c19dcb1b2800f649
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/hott/homotopy/wedge.hlean
1c1bd4d7a7a34e432ae7e8679cc659a066774b56
[ "Apache-2.0" ]
permissive
tectronics/lean
ab977ba6be0fcd46047ddbb3c8e16e7c26710701
f38af35e0616f89c6e9d7e3eb1d48e47ee666efe
refs/heads/master
1,532,358,526,384
1,456,276,623,000
1,456,276,623,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,741
hlean
/- Copyright (c) 2016 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Ulrik Buchholtz The Wedge Sum of Two pType Types -/ import hit.pointed_pushout .connectedness open eq pushout pointed pType unit definition pwedge (A B : Type*) : Type* := ppushout (pconst punit A) (pconst punit B) namespace wedge -- TODO maybe find a cleaner proof protected definition unit (A : Type*) : A ≃* pwedge punit A := begin fapply pequiv_of_pmap, { fapply pmap.mk, intro a, apply pinr a, apply respect_pt }, { fapply is_equiv.adjointify, intro x, fapply pushout.elim_on x, exact Ξ» x, Point A, exact id, intro u, reflexivity, intro x, fapply pushout.rec_on x, intro u, cases u, esimp, apply (glue unit.star)⁻¹, intro a, reflexivity, intro u, cases u, esimp, apply eq_pathover, refine _ ⬝hp !ap_id⁻¹, fapply eq_hconcat, apply ap_compose inr, krewrite elim_glue, fapply eq_hconcat, apply ap_idp, apply square_of_eq, apply con.left_inv, intro a, reflexivity}, end end wedge open trunc is_trunc function homotopy namespace wedge_extension section -- The wedge connectivity lemma (Lemma 8.6.2) parameters {A B : Type*} (n m : trunc_index) [cA : is_conn n .+2 A] [cB : is_conn m .+2 B] (P : A β†’ B β†’ (m .+1 +2+ n .+1)-Type) (f : Ξ a : A, P a (Point B)) (g : Ξ b : B, P (Point A) b) (p : f (Point A) = g (Point B)) include cA cB private definition Q (a : A) : (n .+1)-Type := trunctype.mk (fiber (Ξ»s : (Ξ b : B, P a b), s (Point B)) (f a)) (is_conn.elim_general (P a) (f a)) private definition Q_sec : Ξ a : A, Q a := is_conn.elim Q (fiber.mk g p⁻¹) protected definition ext : Ξ (a : A)(b : B), P a b := Ξ»a, fiber.point (Q_sec a) protected definition Ξ²_left (a : A) : ext a (Point B) = f a := fiber.point_eq (Q_sec a) private definition coh_aux : Ξ£q : ext (Point A) = g, Ξ²_left (Point A) = ap (Ξ»s : (Ξ b : B, P (Point A) b), s (Point B)) q ⬝ p⁻¹ := equiv.to_fun (fiber.fiber_eq_equiv (Q_sec (Point A)) (fiber.mk g p⁻¹)) (is_conn.elim_Ξ² Q (fiber.mk g p⁻¹)) protected definition Ξ²_right (b : B) : ext (Point A) b = g b := apd10 (sigma.pr1 coh_aux) b private definition lem : Ξ²_left (Point A) = Ξ²_right (Point B) ⬝ p⁻¹ := begin unfold Ξ²_right, unfold Ξ²_left, krewrite (apd10_eq_ap_eval (sigma.pr1 coh_aux) (Point B)), exact sigma.pr2 coh_aux, end protected definition coh : (Ξ²_left (Point A))⁻¹ ⬝ Ξ²_right (Point B) = p := by rewrite [lem,con_inv,inv_inv,con.assoc,con.left_inv] end end wedge_extension
56ab182f6f9cd5d1eb0441dfb8106cab29548a22
46125763b4dbf50619e8846a1371029346f4c3db
/src/ring_theory/free_ring.lean
120aa7e3d664ef7df5ccf7185a6683ea32674ee3
[ "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
4,004
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Johan Commelin -/ import algebra.free_monoid group_theory.free_abelian_group data.equiv.algebra data.polynomial universes u v def free_ring (Ξ± : Type u) : Type u := free_abelian_group $ free_monoid Ξ± namespace free_ring variables (Ξ± : Type u) instance : ring (free_ring Ξ±) := free_abelian_group.ring _ instance : inhabited (free_ring Ξ±) := ⟨0⟩ variables {Ξ±} def of (x : Ξ±) : free_ring Ξ± := free_abelian_group.of [x] @[elab_as_eliminator] protected lemma induction_on {C : free_ring Ξ± β†’ Prop} (z : free_ring Ξ±) (hn1 : C (-1)) (hb : βˆ€ b, C (of b)) (ha : βˆ€ x y, C x β†’ C y β†’ C (x + y)) (hm : βˆ€ x y, C x β†’ C y β†’ C (x * y)) : C z := have hn : βˆ€ x, C x β†’ C (-x), from Ξ» x ih, neg_one_mul x β–Έ hm _ _ hn1 ih, have h1 : C 1, from neg_neg (1 : free_ring Ξ±) β–Έ hn _ hn1, free_abelian_group.induction_on z (add_left_neg (1 : free_ring Ξ±) β–Έ ha _ _ hn1 h1) (Ξ» m, list.rec_on m h1 $ Ξ» a m ih, hm _ _ (hb a) ih) (Ξ» m ih, hn _ ih) ha section lift variables {Ξ² : Type v} [ring Ξ²] (f : Ξ± β†’ Ξ²) def lift : free_ring Ξ± β†’ Ξ² := free_abelian_group.lift $ Ξ» L, (list.map f L).prod @[simp] lemma lift_zero : lift f 0 = 0 := rfl @[simp] lemma lift_one : lift f 1 = 1 := free_abelian_group.lift.of _ _ @[simp] lemma lift_of (x : Ξ±) : lift f (of x) = f x := (free_abelian_group.lift.of _ _).trans $ one_mul _ @[simp] lemma lift_add (x y) : lift f (x + y) = lift f x + lift f y := free_abelian_group.lift.add _ _ _ @[simp] lemma lift_neg (x) : lift f (-x) = -lift f x := free_abelian_group.lift.neg _ _ @[simp] lemma lift_sub (x y) : lift f (x - y) = lift f x - lift f y := free_abelian_group.lift.sub _ _ _ @[simp] lemma lift_mul (x y) : lift f (x * y) = lift f x * lift f y := begin refine free_abelian_group.induction_on y (mul_zero _).symm _ _ _, { intros L2, conv { to_lhs, dsimp only [(*), mul_zero_class.mul, semiring.mul, ring.mul, semigroup.mul] }, rw [free_abelian_group.lift.of, lift, free_abelian_group.lift.of], refine free_abelian_group.induction_on x (zero_mul _).symm _ _ _, { intros L1, iterate 3 { rw free_abelian_group.lift.of }, show list.prod (list.map f (_ ++ _)) = _, rw [list.map_append, list.prod_append] }, { intros L1 ih, iterate 3 { rw free_abelian_group.lift.neg }, rw [ih, neg_mul_eq_neg_mul] }, { intros x1 x2 ih1 ih2, iterate 3 { rw free_abelian_group.lift.add }, rw [ih1, ih2, add_mul] } }, { intros L2 ih, rw [mul_neg_eq_neg_mul_symm, lift_neg, lift_neg, mul_neg_eq_neg_mul_symm, ih] }, { intros y1 y2 ih1 ih2, rw [mul_add, lift_add, lift_add, mul_add, ih1, ih2] }, end instance : is_ring_hom (lift f) := { map_one := lift_one f, map_mul := lift_mul f, map_add := lift_add f } @[simp] lemma lift_pow (x) (n : β„•) : lift f (x ^ n) = lift f x ^ n := is_semiring_hom.map_pow _ x n @[simp] lemma lift_comp_of (f : free_ring Ξ± β†’ Ξ²) [is_ring_hom f] : lift (f ∘ of) = f := funext $ Ξ» x, free_ring.induction_on x (by rw [lift_neg, lift_one, is_ring_hom.map_neg f, is_ring_hom.map_one f]) (lift_of _) (Ξ» x y ihx ihy, by rw [lift_add, is_ring_hom.map_add f, ihx, ihy]) (Ξ» x y ihx ihy, by rw [lift_mul, is_ring_hom.map_mul f, ihx, ihy]) end lift variables {Ξ² : Type v} (f : Ξ± β†’ Ξ²) def map : free_ring Ξ± β†’ free_ring Ξ² := lift $ of ∘ f @[simp] lemma map_zero : map f 0 = 0 := rfl @[simp] lemma map_one : map f 1 = 1 := rfl @[simp] lemma map_of (x : Ξ±) : map f (of x) = of (f x) := lift_of _ _ @[simp] lemma map_add (x y) : map f (x + y) = map f x + map f y := lift_add _ _ _ @[simp] lemma map_neg (x) : map f (-x) = -map f x := lift_neg _ _ @[simp] lemma map_sub (x y) : map f (x - y) = map f x - map f y := lift_sub _ _ _ @[simp] lemma map_mul (x y) : map f (x * y) = map f x * map f y := lift_mul _ _ _ @[simp] lemma map_pow (x) (n : β„•) : map f (x ^ n) = (map f x) ^ n := lift_pow _ _ _ end free_ring
2552b32a9e2201b837d1636409247a49e1a233b0
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/ring_theory/euclidean_domain.lean
918ac5d5cf82ad7932037312ed066748f4227176
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,587
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Chris Hughes -/ import ring_theory.coprime import ring_theory.ideal.basic /-! # Lemmas about Euclidean domains Various about Euclidean domains are proved; all of them seem to be true more generally for principal ideal domains, so these lemmas should probably be reproved in more generality and this file perhaps removed? ## Tags euclidean domain -/ noncomputable theory open_locale classical open euclidean_domain set ideal -- TODO -- this should surely be proved for PIDs instead? theorem span_gcd {Ξ±} [euclidean_domain Ξ±] (x y : Ξ±) : span ({gcd x y} : set Ξ±) = span ({x, y} : set Ξ±) := begin apply le_antisymm, { refine span_le.2 (Ξ» x, _), simp only [set.mem_singleton_iff, set_like.mem_coe, mem_span_pair], rintro rfl, exact ⟨gcd_a x y, gcd_b x y, by simp [gcd_eq_gcd_ab, mul_comm]⟩ }, { assume z , simp [mem_span_singleton, euclidean_domain.gcd_dvd_left, mem_span_pair, @eq_comm _ _ z] {contextual := tt}, assume a b h, exact dvd_add (dvd_mul_of_dvd_right (gcd_dvd_left _ _) _) (dvd_mul_of_dvd_right (gcd_dvd_right _ _) _) } end -- this should be proved for PIDs? theorem gcd_is_unit_iff {Ξ±} [euclidean_domain Ξ±] {x y : Ξ±} : is_unit (gcd x y) ↔ is_coprime x y := ⟨λ h, let ⟨b, hb⟩ := is_unit_iff_exists_inv'.1 h in ⟨b * gcd_a x y, b * gcd_b x y, by rw [← hb, gcd_eq_gcd_ab, mul_comm x, mul_comm y, mul_add, mul_assoc, mul_assoc]⟩, Ξ» ⟨a, b, h⟩, is_unit_iff_dvd_one.2 $ h β–Έ dvd_add (dvd_mul_of_dvd_right (gcd_dvd_left x y) _) (dvd_mul_of_dvd_right (gcd_dvd_right x y) _)⟩ -- this should be proved for UFDs surely? theorem is_coprime_of_dvd {Ξ±} [euclidean_domain Ξ±] {x y : Ξ±} (z : Β¬ (x = 0 ∧ y = 0)) (H : βˆ€ z ∈ nonunits Ξ±, z β‰  0 β†’ z ∣ x β†’ Β¬ z ∣ y) : is_coprime x y := begin rw [← gcd_is_unit_iff], by_contra h, refine H _ h _ (gcd_dvd_left _ _) (gcd_dvd_right _ _), rwa [ne, euclidean_domain.gcd_eq_zero_iff] end -- this should be proved for UFDs surely? theorem dvd_or_coprime {Ξ±} [euclidean_domain Ξ±] (x y : Ξ±) (h : irreducible x) : x ∣ y ∨ is_coprime x y := begin refine or_iff_not_imp_left.2 (Ξ» h', _), apply is_coprime_of_dvd, { unfreezingI { rintro ⟨rfl, rfl⟩ }, simpa using h }, { unfreezingI { rintro z nu nz ⟨w, rfl⟩ dy }, refine h' (dvd_trans _ dy), simpa using mul_dvd_mul_left z (is_unit_iff_dvd_one.1 $ (of_irreducible_mul h).resolve_left nu) } end
4c9d6399ed24bab5b7c840f0529fb2db836d1641
bdc27058d2df65f1c05b3dd4ff23a30af9332a2b
/src/hilbert_props.lean
f5ca65ab5093c207203ed784dc4bd65dd26f2935
[]
no_license
bakerjd99/leanteach2020
19e3dda4167f7bb8785fe0e2fe762a9c53a9214d
2160909674f3aec475eabb51df73af7fa1d91c65
refs/heads/master
1,668,724,511,289
1,594,407,424,000
1,594,407,424,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,422
lean
import tactic import .hilbert noncomputable theory open_locale classical local infix ` ≃ `:55 := C -- \ equiv == equivalence/congruence local infix `⬝`:56 := Segment.mk -- \ cdot == center-dot ------------------------------- -- |#Instructions For Hilbert# | ------------------------------- -- Alex: I copied the instructions from the propositions.lean document -- and deleted the ones that didn't pertain to the Hilbert group /- 0. Read these instructions carefully. 1. I have tried my best to translate statements of the propositions into Lean. 2. In place of proofs, I have placed sorry's. 3. Do NOT add your proofs to this file. Create a new file called euclid_props.lean (and hilbert_props.lean) on CoCalc. Copy the statements there and add your proofs to it. 4. You might have to make minor changes to the statement to make it compatible with your syntax. Don't make too many changes though. 5. If you do make a change to the statement, then don't forget to add a note in your hilbert_props.lean file. 6. I am also mentioning missing definitions in this file. You need to define these in your hilbert.lean / euclid.lean source file so that the propositions given here make sense. 7. If you spot some obvious mistakes in the statement of the propositions given below then let me know. You can also ask me for clarifications. 8. This is still an incomplete list and I will keep adding more propositions in this file. -- VK -/ -- If two distinct lines intersect, then they do so in exactly one point. lemma single_intersection (l₁ lβ‚‚ : Line) : l₁ β‰  lβ‚‚ β†’ intersect_line l₁ lβ‚‚ β†’ βˆƒ! x : Point, lies_on_line x l₁ ∧ lies_on_line x lβ‚‚ := begin intros h₁ hβ‚‚, rw intersect_line at hβ‚‚, choose x hβ‚‚ using hβ‚‚, use x, tidy, symmetry, by_contradiction, have line_exists := line_exists x y a_2, tidy, have h₃ : l₁ = ⟨x, y, a_2⟩, { apply line_unique, assumption, assumption}, have hβ‚„ : lβ‚‚ = ⟨x, y, a_2⟩, { apply line_unique, assumption, assumption}, cc, end -- Through any point there exists at least two lines. lemma two_lines (p : Point) : βˆƒ l₁ lβ‚‚ : Line, l₁ β‰  lβ‚‚ ∧ lies_on_line p l₁ ∧ lies_on_line p lβ‚‚ := begin choose a b c h using no_line_on_three_points, by_cases ha : p β‰  a, by_cases hb : p β‰  b, by_cases hc : p β‰  c, set la : Line := ⟨p, a, ha⟩, set lb : Line := ⟨p, b, hb⟩, set lc : Line := ⟨p, c, hc⟩, by_cases hab : la β‰  lb, use la, use lb, have le := line_exists p a ha, split, assumption, split, exact le.1, have le := line_exists p b hb, exact le.1, simp at hab, by_cases hbc : lb β‰  lc, repeat {sorry}, end -- # Euclid's Proposition 1 --------------------------- -- To construct an equilateral triangle on a given -- finite straight line. theorem construct_equilateral (s : Segment) : βˆƒ t : Triangle, t.p₁ = s.p₁ ∧ t.pβ‚‚ = s.pβ‚‚ ∧ equilateral t := begin let a : Point := s.p₁, let b : Point := s.pβ‚‚, -- let c : Point := s.p₁ ⬝ c ≃ s.pβ‚‚ ⬝ c, -- , sorry, end -- # Euclid's Proposition 2 --------------------------- -- To place a straight line equal to a given straight -- line with one end at a given point. theorem segment_move (s : Segment) (h : s.p₁ β‰  s.pβ‚‚): βˆƒ s' : Segment, s ≃ s' ∧ s'.p₁ = s.p₁ := begin sorry, --let l : Line := line_of_points s.p₁ s.pβ‚‚ h, --let y : Line := lies_on_line s.p₁ y, --fapply segment_copy' s.p₁ s.pβ‚‚ l s.p₁ y, end -- Proposition 3. To cut off from the greater of two given unequal -- straight lines a straight line equal to the less. -- Assume sβ‚‚ < s₁ --theorem prop3 (s₁ sβ‚‚ : Segment) (h : s₁.p1 β‰  s₁.p2): -- βˆƒ x : Point, lies_on_segment x s₁ h ∧ s₁.p1⬝x ≃ sβ‚‚ := sorry -- Proposition 4. If two triangles have two sides equal to two sides -- respectively, and have the angles contained by the equal straight -- lines equal, then they also have the base equal to the base, the -- triangle equals the triangle, and the remaining angles equal the -- remaining angles respectively, namely those opposite the equal -- sides. --theorem prop4 (t₁ tβ‚‚ : Triangle) : -- t₁.a⬝t₁.b ≃ tβ‚‚.a⬝tβ‚‚.b -- β†’ t₁.a⬝t₁.c ≃ tβ‚‚.a⬝tβ‚‚.c -- β†’ (Angle.mk t₁.a {Ray . base:=t₁.a, extension:=t₁.b} {Ray . base:=t₁.a, extension:=t₁.b} rfl rfl) ≃ Angle.mk t₁.a {Ray . base:=t₁.a, extension:=t₁.b} {Ray . base:=t₁.a, extension:=t₁.b} rfl rfl -- β†’ congruent_triangles t₁ tβ‚‚ -- := sorry -- Proposition 5. In isosceles triangles the angles at the base equal -- one another, and, if the equal straight lines are produced further, -- then the angles under the base equal one another. ---------------------------------------------------------------------- -- Part 1 of Prop 5 theorem isosceles_triangle_base_angle (abc : Triangle) : let sides := sides_of_triangle abc in let angles := angles_of_triangle abc in sides.nth 0 ≃ sides.nth 1 -- a⬝b ≃ b⬝c β†’ angles.nth 0 ≃ angles.nth 2 := begin set a := abc.p₁, set b := abc.pβ‚‚, set c := abc.p₃, intros sides angles h, have x : b⬝a ≃ a⬝b, { apply segment_swap}, have ba_bc : b⬝a ≃ b⬝c, { apply C_segment_trans, apply C_segment_symm, exact x, exact h}, have bc_ba : b⬝c ≃ b⬝a, { apply C_segment_symm, exact ba_bc}, have abc_cba : (⟨a, b, c⟩ : Angle) ≃ ⟨c, b, a⟩, by symmetry, have cong_tri := congruent_triangle_SAS b a c b c a ba_bc bc_ba abc_cba, have angles_cong := angles_of_congruent_triangle ⟨b, a, c⟩ ⟨b, c, a⟩ cong_tri, exact angles_cong.2.1, end -- Proposition 6. -- If in a triangle two angles equal one another, then the sides opposite the equal angles also equal one another. -- TODO: Maybe this should be a Theorem? -- Corresponding angles in congruent triangles must be congruent -- May be needed -- ∧ t₁.α₁ ≃ tβ‚‚.α₁ -- ∧ t₁.Ξ±β‚‚ ≃ tβ‚‚.Ξ±β‚‚ -- ∧ t₁.α₃ ≃ tβ‚‚.α₃ -- # Proposition 6 ------------------ -- If in a triangle two angles equal one another, then the sides -- opposite the equal angles also equal one another. -- https://artofproblemsolving.com/wiki/index.php/Base_Angle_Theorem theorem sides_opposite_equal_angles_are_equal (abc : Triangle): let sides := (sides_of_triangle abc) in let angles := (angles_of_triangle abc) in angles.nth 0 ≃ angles.nth 2 β†’ sides.nth 0 ≃ sides.nth 1 := -- bac ≃ bca β†’ ab ≃ bc begin set a := abc.p₁, set b := abc.pβ‚‚, set c := abc.p₃, intros sides angles h, -- BAC === BCA or ACB -- sides: ba = ac -- angle: by_contradiction, -- assume Β¬ (ab ≃ bc) sorry, end #exit -- Proposition 7. -- -- Given two straight lines constructed from the ends of a straight line and meeting in a point, there cannot be constructed from the ends of the same straight line, and on the same side of it, two other straight lines meeting in another point and equal to the former two respectively, namely each equal to that from the same end. -- Proposition 8. -- If two triangles have the two sides equal to two sides respectively, and also have the base equal to the base, then they also have the angles equal which are contained by the equal straight lines. -- Proposition 9. -- To bisect a given rectilinear angle. theorem prop_9 (a : Angle): βˆƒ (p : Point), Angle.mk a.ext₁ a.base p ≃ Angle.mk p a.base a.extβ‚‚ -- Proposition 10. -- To bisect a given finite straight line. theorem prop_10 (s : Segment) : βˆƒ (p : Point), B s.p₁ p s.pβ‚‚ ∧ p₁⬝p ≃ p⬝pβ‚‚ -- Proposition 11. -- To draw a straight line at right angles to a given straight line from a given point on it. -- Proposition 12. -- To draw a straight line perpendicular to a given infinite straight line from a given point not on it. -- Proposition 13. -- If a straight line stands on a straight line, then it makes either two right angles or angles whose sum equals two right angles. -- Proposition 14. -- If with any straight line, and at a point on it, two straight lines not lying on the same side make the sum of the adjacent angles equal to two right angles, then the two straight lines are in a straight line with one another. -- Proposition 15. -- If two straight lines cut one another, then they make the vertical angles equal to one another. -- Corollary. If two straight lines cut one another, then they will make the angles at the point of section equal to four right angles. -- /- Proposition 16. In any triangle, if one of the sides is produced, then the exterior angle is greater than either of the interior and opposite angles. Proposition 17. In any triangle the sum of any two angles is less than two right angles. Proposition 18. In any triangle the angle opposite the greater side is greater. Proposition 19. In any triangle the side opposite the greater angle is greater. Proposition 20. In any triangle the sum of any two sides is greater than the remaining one. Proposition 21. If from the ends of one of the sides of a triangle two straight lines are constructed meeting within the triangle, then the sum of the straight lines so constructed is less than the sum of the remaining two sides of the triangle, but the constructed straight lines contain a greater angle than the angle contained by the remaining two sides. Proposition 22. To construct a triangle out of three straight lines which equal three given straight lines: thus it is necessary that the sum of any two of the straight lines should be greater than the remaining one. Proposition 23. To construct a rectilinear angle equal to a given rectilinear angle on a given straight line and at a point on it. Proposition 24. If two triangles have two sides equal to two sides respectively, but have one of the angles contained by the equal straight lines greater than the other, then they also have the base greater than the base. Proposition 25. If two triangles have two sides equal to two sides respectively, but have the base greater than the base, then they also have the one of the angles contained by the equal straight lines greater than the other. Proposition 26. If two triangles have two angles equal to two angles respectively, and one side equal to one side, namely, either the side adjoining the equal angles, or that opposite one of the equal angles, then the remaining sides equal the remaining sides and the remaining angle equals the remaining angle. -/ theorem prop26_part1 (t₁ tβ‚‚ : Triangle): t₁.α₁ ≃ tβ‚‚.α₁ β†’ t₁.Ξ±β‚‚ ≃ tβ‚‚.Ξ±β‚‚ β†’ t₁.s₁₂ ≃ tβ‚‚.s₁₂ β†’ congruent t₁ tβ‚‚ := begin let G : Point := sorry end /- Proposition 27. If a straight line falling on two straight lines makes the alternate angles equal to one another, then the straight lines are parallel to one another. Proposition 28. If a straight line falling on two straight lines makes the exterior angle equal to the interior and opposite angle on the same side, or the sum of the interior angles on the same side equal to two right angles, then the straight lines are parallel to one another. Proposition 29. A straight line falling on parallel straight lines makes the alternate angles equal to one another, the exterior angle equal to the interior and opposite angle, and the sum of the interior angles on the same side equal to two right angles. Proposition 30. Straight lines parallel to the same straight line are also parallel to one another. -/ theorem prop30 (l₁ lβ‚‚ l₃ : Line): parallel_lines l₁ lβ‚‚ β†’ parallel_lines l₁ l₃ β†’ parallel_lines lβ‚‚ l₃ /-Proposition 31. To draw a straight line through a given point parallel to a given straight line. -/ theorem prop31 (l : Line) (p: Point) (h: Β¬lies_on_line p l): βˆƒ (l' : Line), lies_on_line p l' ∧ parallel_lines l l' := begin /-split, split, let l' := Line, use l', choose l' using parallel_postulate,-/ sorry, end /-Proposition 32. In any triangle, if one of the sides is produced, then the exterior angle equals the sum of the two interior and opposite angles, and the sum of the three interior angles of the triangle equals two right angles. Proposition 33. Straight lines which join the ends of equal and parallel straight lines in the same directions are themselves equal and parallel. Proposition 34. In parallelogrammic areas the opposite sides and angles equal one another, and the diameter bisects the areas. Proposition 35. Parallelograms which are on the same base and in the same parallels equal one another. Proposition 36. Parallelograms which are on equal bases and in the same parallels equal one another. Proposition 37. Triangles which are on the same base and in the same parallels equal one another. Proposition 38. Triangles which are on equal bases and in the same parallels equal one another. Proposition 39. Equal triangles which are on the same base and on the same side are also in the same parallels. Proposition 40. Equal triangles which are on equal bases and on the same side are also in the same parallels. Proposition 41. If a parallelogram has the same base with a triangle and is in the same parallels, then the parallelogram is double the triangle. Proposition 42. To construct a parallelogram equal to a given triangle in a given rectilinear angle. Proposition 43. In any parallelogram the complements of the parallelograms about the diameter equal one another. Proposition 44. To a given straight line in a given rectilinear angle, to apply a parallelogram equal to a given triangle. Proposition 45. To construct a parallelogram equal to a given rectilinear figure in a given rectilinear angle. Proposition 46. To describe a square on a given straight line. Proposition 47. In right-angled triangles the square on the side opposite the right angle equals the sum of the squares on the sides containing the right angle. Proposition 48. If in a triangle the square on one of the sides equals the sum of the squares on the remaining two sides of the triangle, then the angle contained by the remaining two sides of the triangle is right. -/
d8ee34a3c6a9b6f7163701266e50e00ce3f5205a
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/archive/imo/imo2011_q3.lean
c3e5dfc5d83611a2c3eae1095a7e773b3b589375
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
2,406
lean
/- Copyright (c) 2021 David Renshaw. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Renshaw -/ import data.real.basic /-! # IMO 2011 Q3 Let f : ℝ β†’ ℝ be a function that satisfies f(x + y) ≀ y * f(x) + f(f(x)) for all x and y. Prove that f(x) = 0 for all x ≀ 0. # Solution Direct translation of the solution found in https://www.imo-official.org/problems/IMO2011SL.pdf -/ theorem imo2011_q3 (f : ℝ β†’ ℝ) (hf : βˆ€ x y, f (x + y) ≀ y * f x + f (f x)) : βˆ€ x ≀ 0, f x = 0 := begin -- reparameterize have hxt : βˆ€ x t, f t ≀ t * f x - x * f x + f (f x), { intros x t, calc f t = f (x + (t - x)) : by rw (add_eq_of_eq_sub' rfl) ... ≀ (t - x) * f x + f (f x) : hf x (t - x) ... = t * f x - x * f x + f (f x) : by rw sub_mul }, have h_ab_combined : βˆ€ a b, a * f a + b * f b ≀ 2 * f a * f b, { intros a b, linarith [hxt b (f a), hxt a (f b)] }, have h_f_nonneg_of_pos : βˆ€ a < 0, 0 ≀ f a, { intros a han, suffices : a * f a ≀ 0, from nonneg_of_mul_nonpos_right this han, exact add_le_iff_nonpos_left.mp (h_ab_combined a (2 * f a)) }, have h_f_nonpos : βˆ€ x, f x ≀ 0, { intros x, by_contra h_suppose_not, -- If we choose a small enough argument for f, then we get a contradiction. let s := (x * f x - f (f x)) / (f x), have hm : min 0 s - 1 < s := (sub_one_lt _).trans_le (min_le_right 0 s), have hml : min 0 s - 1 < 0 := (sub_one_lt _).trans_le (min_le_left 0 s), suffices : f (min 0 s - 1) < 0, from not_le.mpr this (h_f_nonneg_of_pos (min 0 s - 1) hml), have hp : 0 < f x := not_le.mp h_suppose_not, calc f (min 0 s - 1) ≀ (min 0 s - 1) * f x - x * f x + f (f x) : hxt x (min 0 s - 1) ... < s * f x - x * f x + f (f x) : by linarith [(mul_lt_mul_right hp).mpr hm] ... = 0 : by { rw [(eq_div_iff hp.ne.symm).mp rfl], linarith } }, have h_fx_zero_of_neg : βˆ€ x < 0, f x = 0, { intros x hxz, exact (h_f_nonpos x).antisymm (h_f_nonneg_of_pos x hxz) }, intros x hx, obtain (h_x_neg : x < 0) | (rfl : x = 0) := hx.lt_or_eq, { exact h_fx_zero_of_neg _ h_x_neg }, { suffices : 0 ≀ f 0, from le_antisymm (h_f_nonpos 0) this, have hno : f (-1) = 0 := h_fx_zero_of_neg (-1) neg_one_lt_zero, have hp := hxt (-1) (-1), rw hno at hp, linarith }, end
30a8ce55e721fbc67ef21b7c6e049c9d2464fd33
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/abelian/non_preadditive.lean
3b86071992c8c04dcf3c59e908629d65c41167ac
[ "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,252
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.shapes.finite_products import category_theory.limits.shapes.kernels import category_theory.limits.shapes.normal_mono.equalizers import category_theory.abelian.images import category_theory.preadditive /-! # Every non_preadditive_abelian category is preadditive In mathlib, we define an abelian category as a preadditive category with a zero object, kernels and cokernels, products and coproducts and in which every monomorphism and epimorphis is normal. While virtually every interesting abelian category has a natural preadditive structure (which is why it is included in the definition), preadditivity is not actually needed: Every category that has all of the other properties appearing in the definition of an abelian category admits a preadditive structure. This is the construction we carry out in this file. The proof proceeds in roughly five steps: 1. Prove some results (for example that all equalizers exist) that would be trivial if we already had the preadditive structure but are a bit of work without it. 2. Develop images and coimages to show that every monomorphism is the kernel of its cokernel. The results of the first two steps are also useful for the "normal" development of abelian categories, and will be used there. 3. For every object `A`, define a "subtraction" morphism `Οƒ : A β¨― A ⟢ A` and use it to define subtraction on morphisms as `f - g := prod.lift f g ≫ Οƒ`. 4. Prove a small number of identities about this subtraction from the definition of `Οƒ`. 5. From these identities, prove a large number of other identities that imply that defining `f + g := f - (0 - g)` indeed gives an abelian group structure on morphisms such that composition is bilinear. The construction is non-trivial and it is quite remarkable that this abelian group structure can be constructed purely from the existence of a few limits and colimits. Even more remarkably, since abelian categories admit exactly one preadditive structure (see `subsingleton_preadditive_of_has_binary_biproducts`), the construction manages to exactly reconstruct any natural preadditive structure the category may have. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ noncomputable theory open category_theory open category_theory.limits namespace category_theory section universes v u variables (C : Type u) [category.{v} C] /-- We call a category `non_preadditive_abelian` if it has a zero object, kernels, cokernels, binary products and coproducts, and every monomorphism and every epimorphism is normal. -/ class non_preadditive_abelian extends has_zero_morphisms C, normal_mono_category C, normal_epi_category C := [has_zero_object : has_zero_object C] [has_kernels : has_kernels C] [has_cokernels : has_cokernels C] [has_finite_products : has_finite_products C] [has_finite_coproducts : has_finite_coproducts C] set_option default_priority 100 attribute [instance] non_preadditive_abelian.has_zero_object attribute [instance] non_preadditive_abelian.has_kernels attribute [instance] non_preadditive_abelian.has_cokernels attribute [instance] non_preadditive_abelian.has_finite_products attribute [instance] non_preadditive_abelian.has_finite_coproducts end end category_theory open category_theory universes v u variables {C : Type u} [category.{v} C] [non_preadditive_abelian C] namespace category_theory.non_preadditive_abelian section factor variables {P Q : C} (f : P ⟢ Q) /-- The map `p : P ⟢ image f` is an epimorphism -/ instance : epi (abelian.factor_thru_image f) := let I := abelian.image f, p := abelian.factor_thru_image f, i := kernel.ΞΉ (cokernel.Ο€ f) in -- It will suffice to consider some g : I ⟢ R such that p ≫ g = 0 and show that g = 0. normal_mono_category.epi_of_zero_cancel _ $ Ξ» R (g : I ⟢ R) (hpg : p ≫ g = 0), begin -- Since C is abelian, u := ker g ≫ i is the kernel of some morphism h. let u := kernel.ΞΉ g ≫ i, haveI : mono u := mono_comp _ _, haveI hu := normal_mono_of_mono u, let h := hu.g, -- By hypothesis, p factors through the kernel of g via some t. obtain ⟨t, ht⟩ := kernel.lift' g p hpg, have fh : f ≫ h = 0, calc f ≫ h = (p ≫ i) ≫ h : (abelian.image.fac f).symm β–Έ rfl ... = ((t ≫ kernel.ΞΉ g) ≫ i) ≫ h : ht β–Έ rfl ... = t ≫ u ≫ h : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = t ≫ 0 : hu.w β–Έ rfl ... = 0 : has_zero_morphisms.comp_zero _ _, -- h factors through the cokernel of f via some l. obtain ⟨l, hl⟩ := cokernel.desc' f h fh, have hih : i ≫ h = 0, calc i ≫ h = i ≫ cokernel.Ο€ f ≫ l : hl β–Έ rfl ... = 0 ≫ l : by rw [←category.assoc, kernel.condition] ... = 0 : zero_comp, -- i factors through u = ker h via some s. obtain ⟨s, hs⟩ := normal_mono.lift' u i hih, have hs' : (s ≫ kernel.ΞΉ g) ≫ i = πŸ™ I ≫ i, by rw [category.assoc, hs, category.id_comp], haveI : epi (kernel.ΞΉ g) := epi_of_epi_fac ((cancel_mono _).1 hs'), -- ker g is an epimorphism, but ker g ≫ g = 0 = ker g ≫ 0, so g = 0 as required. exact zero_of_epi_comp _ (kernel.condition g) end instance is_iso_factor_thru_image [mono f] : is_iso (abelian.factor_thru_image f) := is_iso_of_mono_of_epi _ /-- The canonical morphism `i : coimage f ⟢ Q` is a monomorphism -/ instance : mono (abelian.factor_thru_coimage f) := let I := abelian.coimage f, i := abelian.factor_thru_coimage f, p := cokernel.Ο€ (kernel.ΞΉ f) in normal_epi_category.mono_of_cancel_zero _ $ Ξ» R (g : R ⟢ I) (hgi : g ≫ i = 0), begin -- Since C is abelian, u := p ≫ coker g is the cokernel of some morphism h. let u := p ≫ cokernel.Ο€ g, haveI : epi u := epi_comp _ _, haveI hu := normal_epi_of_epi u, let h := hu.g, -- By hypothesis, i factors through the cokernel of g via some t. obtain ⟨t, ht⟩ := cokernel.desc' g i hgi, have hf : h ≫ f = 0, calc h ≫ f = h ≫ (p ≫ i) : (abelian.coimage.fac f).symm β–Έ rfl ... = h ≫ (p ≫ (cokernel.Ο€ g ≫ t)) : ht β–Έ rfl ... = h ≫ u ≫ t : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = 0 ≫ t : by rw [←category.assoc, hu.w] ... = 0 : zero_comp, -- h factors through the kernel of f via some l. obtain ⟨l, hl⟩ := kernel.lift' f h hf, have hhp : h ≫ p = 0, calc h ≫ p = (l ≫ kernel.ΞΉ f) ≫ p : hl β–Έ rfl ... = l ≫ 0 : by rw [category.assoc, cokernel.condition] ... = 0 : comp_zero, -- p factors through u = coker h via some s. obtain ⟨s, hs⟩ := normal_epi.desc' u p hhp, have hs' : p ≫ cokernel.Ο€ g ≫ s = p ≫ πŸ™ I, by rw [←category.assoc, hs, category.comp_id], haveI : mono (cokernel.Ο€ g) := mono_of_mono_fac ((cancel_epi _).1 hs'), -- coker g is a monomorphism, but g ≫ coker g = 0 = 0 ≫ coker g, so g = 0 as required. exact zero_of_comp_mono _ (cokernel.condition g) end instance is_iso_factor_thru_coimage [epi f] : is_iso (abelian.factor_thru_coimage f) := is_iso_of_mono_of_epi _ end factor section cokernel_of_kernel variables {X Y : C} {f : X ⟢ Y} /-- In a `non_preadditive_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)) := is_cokernel.cokernel_iso _ _ (cokernel.of_iso_comp _ _ (limits.is_limit.cone_point_unique_up_to_iso (limit.is_limit _) h) (cone_morphism.w (limits.is_limit.unique_up_to_iso (limit.is_limit _) h).hom _)) (as_iso $ abelian.factor_thru_coimage f) (abelian.coimage.fac f) /-- In a `non_preadditive_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)) := is_kernel.iso_kernel _ _ (kernel.of_comp_iso _ _ (limits.is_colimit.cocone_point_unique_up_to_iso h (colimit.is_colimit _)) (cocone_morphism.w (limits.is_colimit.unique_up_to_iso h $ colimit.is_colimit _).hom _)) (as_iso $ abelian.factor_thru_image f) (abelian.image.fac f) end cokernel_of_kernel section /-- The composite `A ⟢ A β¨― A ⟢ cokernel (Ξ” A)`, where the first map is `(πŸ™ A, 0)` and the second map is the canonical projection into the cokernel. -/ abbreviation r (A : C) : A ⟢ cokernel (diag A) := prod.lift (πŸ™ A) 0 ≫ cokernel.Ο€ (diag A) instance mono_Ξ” {A : C} : mono (diag A) := mono_of_mono_fac $ prod.lift_fst _ _ instance mono_r {A : C} : mono (r A) := begin let hl : is_limit (kernel_fork.of_ΞΉ (diag A) (cokernel.condition (diag A))), { exact mono_is_kernel_of_cokernel _ (colimit.is_colimit _) }, apply normal_epi_category.mono_of_cancel_zero, intros Z x hx, have hxx : (x ≫ prod.lift (πŸ™ A) (0 : A ⟢ A)) ≫ cokernel.Ο€ (diag A) = 0, { rw [category.assoc, hx] }, obtain ⟨y, hy⟩ := kernel_fork.is_limit.lift' hl _ hxx, rw kernel_fork.ΞΉ_of_ΞΉ at hy, have hyy : y = 0, { erw [←category.comp_id y, ←limits.prod.lift_snd (πŸ™ A) (πŸ™ A), ←category.assoc, hy, category.assoc, prod.lift_snd, has_zero_morphisms.comp_zero] }, haveI : mono (prod.lift (πŸ™ A) (0 : A ⟢ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (πŸ™ A) (0 : A ⟢ A))).1, rw [←hy, hyy, zero_comp, zero_comp] end instance epi_r {A : C} : epi (r A) := begin have hlp : prod.lift (πŸ™ A) (0 : A ⟢ A) ≫ limits.prod.snd = 0 := prod.lift_snd _ _, let hp1 : is_limit (kernel_fork.of_ΞΉ (prod.lift (πŸ™ A) (0 : A ⟢ A)) hlp), { refine fork.is_limit.mk _ (Ξ» s, fork.ΞΉ s ≫ limits.prod.fst) _ _, { intro s, ext; simp, erw category.comp_id }, { intros s m h, haveI : mono (prod.lift (πŸ™ A) (0 : A ⟢ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (πŸ™ A) (0 : A ⟢ A))).1, convert h, ext; simp } }, let hp2 : is_colimit (cokernel_cofork.of_Ο€ (limits.prod.snd : A β¨― A ⟢ A) hlp), { exact epi_is_cokernel_of_kernel _ hp1 }, apply normal_mono_category.epi_of_zero_cancel, intros Z z hz, have h : prod.lift (πŸ™ A) (0 : A ⟢ A) ≫ cokernel.Ο€ (diag A) ≫ z = 0, { rw [←category.assoc, hz] }, obtain ⟨t, ht⟩ := cokernel_cofork.is_colimit.desc' hp2 _ h, rw cokernel_cofork.Ο€_of_Ο€ at ht, have htt : t = 0, { rw [←category.id_comp t], change πŸ™ A ≫ t = 0, rw [←limits.prod.lift_snd (πŸ™ A) (πŸ™ A), category.assoc, ht, ←category.assoc, cokernel.condition, zero_comp] }, apply (cancel_epi (cokernel.Ο€ (diag A))).1, rw [←ht, htt, comp_zero, comp_zero] end instance is_iso_r {A : C} : is_iso (r A) := is_iso_of_mono_of_epi _ /-- The composite `A β¨― A ⟢ cokernel (diag A) ⟢ A` given by the natural projection into the cokernel followed by the inverse of `r`. In the category of modules, using the normal kernels and cokernels, this map is equal to the map `(a, b) ↦ a - b`, hence the name `Οƒ` for "subtraction". -/ abbreviation Οƒ {A : C} : A β¨― A ⟢ A := cokernel.Ο€ (diag A) ≫ inv (r A) end @[simp, reassoc] lemma diag_Οƒ {X : C} : diag X ≫ Οƒ = 0 := by rw [cokernel.condition_assoc, zero_comp] @[simp, reassoc] lemma lift_Οƒ {X : C} : prod.lift (πŸ™ X) 0 ≫ Οƒ = πŸ™ X := by rw [←category.assoc, is_iso.hom_inv_id] @[reassoc] lemma lift_map {X Y : C} (f : X ⟢ Y) : prod.lift (πŸ™ X) 0 ≫ limits.prod.map f f = f ≫ prod.lift (πŸ™ Y) 0 := by simp /-- Οƒ is a cokernel of Ξ” X. -/ def is_colimit_Οƒ {X : C} : is_colimit (cokernel_cofork.of_Ο€ Οƒ diag_Οƒ) := cokernel.cokernel_iso _ Οƒ (as_iso (r X)).symm (by rw [iso.symm_hom, as_iso_inv]) /-- This is the key identity satisfied by `Οƒ`. -/ lemma Οƒ_comp {X Y : C} (f : X ⟢ Y) : Οƒ ≫ f = limits.prod.map f f ≫ Οƒ := begin obtain ⟨g, hg⟩ := cokernel_cofork.is_colimit.desc' is_colimit_Οƒ (limits.prod.map f f ≫ Οƒ) (by simp), suffices hfg : f = g, { rw [←hg, cofork.Ο€_of_Ο€, hfg] }, calc f = f ≫ prod.lift (πŸ™ Y) 0 ≫ Οƒ : by rw [lift_Οƒ, category.comp_id] ... = prod.lift (πŸ™ X) 0 ≫ limits.prod.map f f ≫ Οƒ : by rw lift_map_assoc ... = prod.lift (πŸ™ X) 0 ≫ Οƒ ≫ g : by rw [←hg, cokernel_cofork.Ο€_of_Ο€] ... = g : by rw [←category.assoc, lift_Οƒ, category.id_comp] end section /- We write `f - g` for `prod.lift f g ≫ Οƒ`. -/ /-- Subtraction of morphisms in a `non_preadditive_abelian` category. -/ def has_sub {X Y : C} : has_sub (X ⟢ Y) := ⟨λ f g, prod.lift f g ≫ ΟƒβŸ© local attribute [instance] has_sub /- We write `-f` for `0 - f`. -/ /-- Negation of morphisms in a `non_preadditive_abelian` category. -/ def has_neg {X Y : C} : has_neg (X ⟢ Y) := ⟨λ f, 0 - f⟩ local attribute [instance] has_neg /- We write `f + g` for `f - (-g)`. -/ /-- Addition of morphisms in a `non_preadditive_abelian` category. -/ def has_add {X Y : C} : has_add (X ⟢ Y) := ⟨λ f g, f - (-g)⟩ local attribute [instance] has_add lemma sub_def {X Y : C} (a b : X ⟢ Y) : a - b = prod.lift a b ≫ Οƒ := rfl lemma add_def {X Y : C} (a b : X ⟢ Y) : a + b = a - (-b) := rfl lemma neg_def {X Y : C} (a : X ⟢ Y) : -a = 0 - a := rfl lemma sub_zero {X Y : C} (a : X ⟢ Y) : a - 0 = a := begin rw sub_def, conv_lhs { congr, congr, rw ←category.comp_id a, skip, rw (show 0 = a ≫ (0 : Y ⟢ Y), by simp)}, rw [← prod.comp_lift, category.assoc, lift_Οƒ, category.comp_id] end lemma sub_self {X Y : C} (a : X ⟢ Y) : a - a = 0 := by rw [sub_def, ←category.comp_id a, ← prod.comp_lift, category.assoc, diag_Οƒ, comp_zero] lemma lift_sub_lift {X Y : C} (a b c d : X ⟢ Y) : prod.lift a b - prod.lift c d = prod.lift (a - c) (b - d) := begin simp only [sub_def], ext, { rw [category.assoc, Οƒ_comp, prod.lift_map_assoc, prod.lift_fst, prod.lift_fst, prod.lift_fst] }, { rw [category.assoc, Οƒ_comp, prod.lift_map_assoc, prod.lift_snd, prod.lift_snd, prod.lift_snd] } end lemma sub_sub_sub {X Y : C} (a b c d : X ⟢ Y) : (a - c) - (b - d) = (a - b) - (c - d) := begin rw [sub_def, ←lift_sub_lift, sub_def, category.assoc, Οƒ_comp, prod.lift_map_assoc], refl end lemma neg_sub {X Y : C} (a b : X ⟢ Y) : (-a) - b = (-b) - a := by conv_lhs { rw [neg_def, ←sub_zero b, sub_sub_sub, sub_zero, ←neg_def] } lemma neg_neg {X Y : C} (a : X ⟢ Y) : -(-a) = a := begin rw [neg_def, neg_def], conv_lhs { congr, rw ←sub_self a }, rw [sub_sub_sub, sub_zero, sub_self, sub_zero] end lemma add_comm {X Y : C} (a b : X ⟢ Y) : a + b = b + a := begin rw [add_def], conv_lhs { rw ←neg_neg a }, rw [neg_def, neg_def, neg_def, sub_sub_sub], conv_lhs {congr, skip, rw [←neg_def, neg_sub] }, rw [sub_sub_sub, add_def, ←neg_def, neg_neg b, neg_def] end lemma add_neg {X Y : C} (a b : X ⟢ Y) : a + (-b) = a - b := by rw [add_def, neg_neg] lemma add_neg_self {X Y : C} (a : X ⟢ Y) : a + (-a) = 0 := by rw [add_neg, sub_self] lemma neg_add_self {X Y : C} (a : X ⟢ Y) : (-a) + a = 0 := by rw [add_comm, add_neg_self] lemma neg_sub' {X Y : C} (a b : X ⟢ Y) : -(a - b) = (-a) + b := begin rw [neg_def, neg_def], conv_lhs { rw ←sub_self (0 : X ⟢ Y) }, rw [sub_sub_sub, add_def, neg_def] end lemma neg_add {X Y : C} (a b : X ⟢ Y) : -(a + b) = (-a) - b := by rw [add_def, neg_sub', add_neg] lemma sub_add {X Y : C} (a b c : X ⟢ Y) : (a - b) + c = a - (b - c) := by rw [add_def, neg_def, sub_sub_sub, sub_zero] lemma add_assoc {X Y : C} (a b c : X ⟢ Y) : (a + b) + c = a + (b + c) := begin conv_lhs { congr, rw add_def }, rw [sub_add, ←add_neg, neg_sub', neg_neg] end lemma add_zero {X Y : C} (a : X ⟢ Y) : a + 0 = a := by rw [add_def, neg_def, sub_self, sub_zero] lemma comp_sub {X Y Z : C} (f : X ⟢ Y) (g h : Y ⟢ Z) : f ≫ (g - h) = f ≫ g - f ≫ h := by rw [sub_def, ←category.assoc, prod.comp_lift, sub_def] lemma sub_comp {X Y Z : C} (f g : X ⟢ Y) (h : Y ⟢ Z) : (f - g) ≫ h = f ≫ h - g ≫ h := by rw [sub_def, category.assoc, Οƒ_comp, ←category.assoc, prod.lift_map, sub_def] lemma comp_add (X Y Z : C) (f : X ⟢ Y) (g h : Y ⟢ Z) : f ≫ (g + h) = f ≫ g + f ≫ h := by rw [add_def, comp_sub, neg_def, comp_sub, comp_zero, add_def, neg_def] lemma add_comp (X Y Z : C) (f g : X ⟢ Y) (h : Y ⟢ Z) : (f + g) ≫ h = f ≫ h + g ≫ h := by rw [add_def, sub_comp, neg_def, sub_comp, zero_comp, add_def, neg_def] /-- Every `non_preadditive_abelian` category is preadditive. -/ def preadditive : preadditive C := { hom_group := Ξ» X Y, { add := (+), add_assoc := add_assoc, zero := 0, zero_add := neg_neg, add_zero := add_zero, neg := Ξ» f, -f, add_left_neg := neg_add_self, add_comm := add_comm }, add_comp' := add_comp, comp_add' := comp_add } end end category_theory.non_preadditive_abelian
5b2c78130a5721001c5e971e3bdc8427cf2cbdd8
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/field_power.lean
e6cda2f7369e5d6b1ebacf868780ec1305fa1854
[ "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
5,613
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 Integer power operation on fields. -/ import algebra.group_with_zero_power import tactic.linarith universe u @[simp] lemma ring_hom.map_fpow {K L : Type*} [division_ring K] [division_ring L] (f : K β†’+* L) : βˆ€ (a : K) (n : β„€), f (a ^ n) = f a ^ n := f.to_monoid_hom.map_fpow f.map_zero @[simp] lemma neg_fpow_bit0 {K : Type*} [division_ring K] (x : K) (n : β„€) : (-x) ^ (bit0 n) = x ^ bit0 n := by rw [fpow_bit0', fpow_bit0', neg_mul_neg] @[simp] lemma neg_fpow_bit1 {K : Type*} [division_ring K] (x : K) (n : β„€) : (-x) ^ (bit1 n) = - x ^ bit1 n := by rw [fpow_bit1', fpow_bit1', neg_mul_neg, neg_mul_eq_mul_neg] section ordered_field_power open int variables {K : Type u} [linear_ordered_field K] lemma fpow_nonneg_of_nonneg {a : K} (ha : 0 ≀ a) : βˆ€ (z : β„€), 0 ≀ a ^ z | (of_nat n) := pow_nonneg ha _ | -[1+n] := inv_nonneg.2 $ pow_nonneg ha _ lemma fpow_pos_of_pos {a : K} (ha : 0 < a) : βˆ€ (z : β„€), 0 < a ^ z | (of_nat n) := pow_pos ha _ | -[1+n] := inv_pos.2 $ pow_pos ha _ lemma fpow_le_of_le {x : K} (hx : 1 ≀ x) {a b : β„€} (h : a ≀ b) : x ^ a ≀ x ^ b := begin induction a with a a; induction b with b b, { simp only [fpow_of_nat, of_nat_eq_coe], apply pow_le_pow hx, apply le_of_coe_nat_le_coe_nat h }, { apply absurd h, apply not_le_of_gt, exact lt_of_lt_of_le (neg_succ_lt_zero _) (of_nat_nonneg _) }, { simp only [fpow_neg_succ_of_nat, one_div], apply le_trans (inv_le_one _); apply one_le_pow_of_one_le hx }, { simp only [fpow_neg_succ_of_nat], apply (inv_le_inv _ _).2, { apply pow_le_pow hx, have : -(↑(a+1) : β„€) ≀ -(↑(b+1) : β„€), from h, have h' := le_of_neg_le_neg this, apply le_of_coe_nat_le_coe_nat h' }, repeat { apply pow_pos (lt_of_lt_of_le zero_lt_one hx) } } end lemma pow_le_max_of_min_le {x : K} (hx : 1 ≀ x) {a b c : β„€} (h : min a b ≀ c) : x ^ (-c) ≀ max (x ^ (-a)) (x ^ (-b)) := begin wlog hle : a ≀ b, have hnle : -b ≀ -a, from neg_le_neg hle, have hfle : x ^ (-b) ≀ x ^ (-a), from fpow_le_of_le hx hnle, have : x ^ (-c) ≀ x ^ (-a), { apply fpow_le_of_le hx, simpa only [min_eq_left hle, neg_le_neg_iff] using h }, simpa only [max_eq_left hfle] end lemma fpow_le_one_of_nonpos {p : K} (hp : 1 ≀ p) {z : β„€} (hz : z ≀ 0) : p ^ z ≀ 1 := calc p ^ z ≀ p ^ 0 : fpow_le_of_le hp hz ... = 1 : by simp lemma one_le_fpow_of_nonneg {p : K} (hp : 1 ≀ p) {z : β„€} (hz : 0 ≀ z) : 1 ≀ p ^ z := calc p ^ z β‰₯ p ^ 0 : fpow_le_of_le hp hz ... = 1 : by simp end ordered_field_power lemma one_lt_pow {K} [linear_ordered_semiring K] {p : K} (hp : 1 < p) : βˆ€ {n : β„•}, 1 ≀ n β†’ 1 < p ^ n | 1 h := by simp; assumption | (k+2) h := begin rw ←one_mul (1 : K), apply mul_lt_mul, { assumption }, { apply le_of_lt, simpa using one_lt_pow (nat.le_add_left 1 k)}, { apply zero_lt_one }, { apply le_of_lt (lt_trans zero_lt_one hp) } end section local attribute [semireducible] int.nonneg lemma one_lt_fpow {K} [linear_ordered_field K] {p : K} (hp : 1 < p) : βˆ€ z : β„€, 0 < z β†’ 1 < p ^ z | (int.of_nat n) h := one_lt_pow hp (nat.succ_le_of_lt (int.lt_of_coe_nat_lt_coe_nat h)) end section ordered variables {K : Type*} [linear_ordered_field K] lemma nat.fpow_pos_of_pos {p : β„•} (h : 0 < p) (n:β„€) : 0 < (p:K)^n := by { apply fpow_pos_of_pos, exact_mod_cast h } lemma nat.fpow_ne_zero_of_pos {p : β„•} (h : 0 < p) (n:β„€) : (p:K)^n β‰  0 := ne_of_gt (nat.fpow_pos_of_pos h n) lemma fpow_strict_mono {x : K} (hx : 1 < x) : strict_mono (Ξ» n:β„€, x ^ n) := Ξ» m n h, show x ^ m < x ^ n, begin have xpos : 0 < x := by linarith, have hβ‚€ : x β‰  0 := by linarith, have hxm : 0 < x^m := fpow_pos_of_pos xpos m, have hxmβ‚€ : x^m β‰  0 := ne_of_gt hxm, suffices : 1 < x^(n-m), { replace := mul_lt_mul_of_pos_right this hxm, simp [sub_eq_add_neg] at this, simpa [*, fpow_add, mul_assoc, fpow_neg, inv_mul_cancel], }, apply one_lt_fpow hx, linarith, end @[simp] lemma fpow_lt_iff_lt {x : K} (hx : 1 < x) {m n : β„€} : x ^ m < x ^ n ↔ m < n := (fpow_strict_mono hx).lt_iff_lt @[simp] lemma fpow_le_iff_le {x : K} (hx : 1 < x) {m n : β„€} : x ^ m ≀ x ^ n ↔ m ≀ n := (fpow_strict_mono hx).le_iff_le @[simp] lemma pos_div_pow_pos {a b : K} (ha : 0 < a) (hb : 0 < b) (k : β„•) : 0 < a/b^k := div_pos ha (pow_pos hb k) @[simp] lemma div_pow_le {a b : K} (ha : 0 < a) (hb : 1 ≀ b) (k : β„•) : a/b^k ≀ a := (div_le_iff $ pow_pos (lt_of_lt_of_le zero_lt_one hb) k).mpr (calc a = a * 1 : (mul_one a).symm ... ≀ a*b^k : (mul_le_mul_left ha).mpr $ one_le_pow_of_one_le hb _) lemma fpow_injective {x : K} (hβ‚€ : 0 < x) (h₁ : x β‰  1) : function.injective ((^) x : β„€ β†’ K) := begin intros m n h, rcases lt_trichotomy x 1 with H|rfl|H, { apply (fpow_strict_mono (one_lt_inv hβ‚€ H)).injective, show x⁻¹ ^ m = x⁻¹ ^ n, rw [← fpow_neg_one, ← fpow_mul, ← fpow_mul, mul_comm _ m, mul_comm _ n, fpow_mul, fpow_mul, h], }, { contradiction }, { exact (fpow_strict_mono H).injective h, }, end @[simp] lemma fpow_inj {x : K} (hβ‚€ : 0 < x) (h₁ : x β‰  1) {m n : β„€} : x ^ m = x ^ n ↔ m = n := (fpow_injective hβ‚€ h₁).eq_iff end ordered section variables {K : Type*} [field K] @[simp, norm_cast] theorem rat.cast_fpow [char_zero K] (q : β„š) (n : β„€) : ((q ^ n : β„š) : K) = q ^ n := (rat.cast_hom K).map_fpow q n end
a6bc787e2f82a12bf64d8dcb28a8a945d1686452
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/pfun.lean
60e1bb8a4aeca5ab8c4cd752eb64b35b68385e06
[ "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
22,620
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Jeremy Avigad -/ import data.set.basic data.equiv.basic data.rel logic.relator /-- `roption Ξ±` is the type of "partial values" of type `Ξ±`. It is similar to `option Ξ±` except the domain condition can be an arbitrary proposition, not necessarily decidable. -/ structure {u} roption (Ξ± : Type u) : Type u := (dom : Prop) (get : dom β†’ Ξ±) namespace roption variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} /-- Convert an `roption Ξ±` with a decidable domain to an option -/ def to_option (o : roption Ξ±) [decidable o.dom] : option Ξ± := if h : dom o then some (o.get h) else none /-- `roption` extensionality -/ theorem ext' : βˆ€ {o p : roption Ξ±} (H1 : o.dom ↔ p.dom) (H2 : βˆ€h₁ hβ‚‚, o.get h₁ = p.get hβ‚‚), o = p | ⟨od, o⟩ ⟨pd, p⟩ H1 H2 := have t : od = pd, from propext H1, by cases t; rw [show o = p, from funext $ Ξ»p, H2 p p] /-- `roption` eta expansion -/ @[simp] theorem eta : Ξ  (o : roption Ξ±), (⟨o.dom, Ξ» h, o.get h⟩ : roption Ξ±) = o | ⟨h, f⟩ := rfl /-- `a ∈ o` means that `o` is defined and equal to `a` -/ protected def mem (a : Ξ±) (o : roption Ξ±) : Prop := βˆƒ h, o.get h = a instance : has_mem Ξ± (roption Ξ±) := ⟨roption.mem⟩ theorem mem_eq (a : Ξ±) (o : roption Ξ±) : (a ∈ o) = (βˆƒ h, o.get h = a) := rfl theorem dom_iff_mem : βˆ€ {o : roption Ξ±}, o.dom ↔ βˆƒy, y ∈ o | ⟨p, f⟩ := ⟨λh, ⟨f h, h, rfl⟩, λ⟨_, h, rfl⟩, h⟩ theorem get_mem {o : roption Ξ±} (h) : get o h ∈ o := ⟨_, rfl⟩ /-- `roption` extensionality -/ theorem ext {o p : roption Ξ±} (H : βˆ€ a, a ∈ o ↔ a ∈ p) : o = p := ext' ⟨λ h, ((H _).1 ⟨h, rfl⟩).fst, Ξ» h, ((H _).2 ⟨h, rfl⟩).fst⟩ $ Ξ» a b, ((H _).2 ⟨_, rfl⟩).snd /-- The `none` value in `roption` has a `false` domain and an empty function. -/ def none : roption Ξ± := ⟨false, false.rec _⟩ instance : inhabited (roption Ξ±) := ⟨none⟩ @[simp] theorem not_mem_none (a : Ξ±) : a βˆ‰ @none Ξ± := Ξ» h, h.fst /-- The `some a` value in `roption` has a `true` domain and the function returns `a`. -/ def some (a : Ξ±) : roption Ξ± := ⟨true, Ξ»_, a⟩ theorem mem_unique : relator.left_unique ((∈) : Ξ± β†’ roption Ξ± β†’ Prop) | _ ⟨p, f⟩ _ ⟨h₁, rfl⟩ ⟨hβ‚‚, rfl⟩ := rfl theorem get_eq_of_mem {o : roption Ξ±} {a} (h : a ∈ o) (h') : get o h' = a := mem_unique ⟨_, rfl⟩ h @[simp] theorem get_some {a : Ξ±} (ha : (some a).dom) : get (some a) ha = a := rfl theorem mem_some (a : Ξ±) : a ∈ some a := ⟨trivial, rfl⟩ @[simp] theorem mem_some_iff {a b} : b ∈ (some a : roption Ξ±) ↔ b = a := ⟨λ⟨h, e⟩, e.symm, Ξ» e, ⟨trivial, e.symm⟩⟩ theorem eq_some_iff {a : Ξ±} {o : roption Ξ±} : o = some a ↔ a ∈ o := ⟨λ e, e.symm β–Έ mem_some _, Ξ» ⟨h, e⟩, e β–Έ ext' (iff_true_intro h) (Ξ» _ _, rfl)⟩ theorem eq_none_iff {o : roption Ξ±} : o = none ↔ βˆ€ a, a βˆ‰ o := ⟨λ e, e.symm β–Έ not_mem_none, Ξ» h, ext (by simpa [not_mem_none])⟩ theorem eq_none_iff' {o : roption Ξ±} : o = none ↔ Β¬ o.dom := ⟨λ e, e.symm β–Έ id, Ξ» h, eq_none_iff.2 (Ξ» a h', h h'.fst)⟩ lemma some_ne_none (x : Ξ±) : some x β‰  none := by { intro h, change none.dom, rw [← h], trivial } lemma ne_none_iff {o : roption Ξ±} : o β‰  none ↔ βˆƒx, o = some x := begin split, { rw [ne, eq_none_iff], intro h, push_neg at h, cases h with x hx, use x, rwa [eq_some_iff] }, { rintro ⟨x, rfl⟩, apply some_ne_none } end @[simp] lemma some_inj {a b : Ξ±} : roption.some a = some b ↔ a = b := function.injective.eq_iff (Ξ» a b h, congr_fun (eq_of_heq (roption.mk.inj h).2) trivial) @[simp] lemma some_get {a : roption Ξ±} (ha : a.dom) : roption.some (roption.get a ha) = a := eq.symm (eq_some_iff.2 ⟨ha, rfl⟩) lemma get_eq_iff_eq_some {a : roption Ξ±} {ha : a.dom} {b : Ξ±} : a.get ha = b ↔ a = some b := ⟨λ h, by simp [h.symm], Ξ» h, by simp [h]⟩ instance none_decidable : decidable (@none Ξ±).dom := decidable.false instance some_decidable (a : Ξ±) : decidable (some a).dom := decidable.true def get_or_else (a : roption Ξ±) [decidable a.dom] (d : Ξ±) := if ha : a.dom then a.get ha else d @[simp] lemma get_or_else_none (d : Ξ±) : get_or_else none d = d := dif_neg id @[simp] lemma get_or_else_some (a : Ξ±) (d : Ξ±) : get_or_else (some a) d = a := dif_pos trivial @[simp] theorem mem_to_option {o : roption Ξ±} [decidable o.dom] {a : Ξ±} : a ∈ to_option o ↔ a ∈ o := begin unfold to_option, by_cases h : o.dom; simp [h], { exact ⟨λ h, ⟨_, h⟩, Ξ» ⟨_, h⟩, h⟩ }, { exact mt Exists.fst h } end /-- Convert an `option Ξ±` into an `roption Ξ±` -/ def of_option : option Ξ± β†’ roption Ξ± | option.none := none | (option.some a) := some a @[simp] theorem mem_of_option {a : Ξ±} : βˆ€ {o : option Ξ±}, a ∈ of_option o ↔ a ∈ o | option.none := ⟨λ h, h.fst.elim, Ξ» h, option.no_confusion h⟩ | (option.some b) := ⟨λ h, congr_arg option.some h.snd, Ξ» h, ⟨trivial, option.some.inj h⟩⟩ @[simp] theorem of_option_dom {Ξ±} : βˆ€ (o : option Ξ±), (of_option o).dom ↔ o.is_some | option.none := by simp [of_option, none] | (option.some a) := by simp [of_option] theorem of_option_eq_get {Ξ±} (o : option Ξ±) : of_option o = ⟨_, @option.get _ o⟩ := roption.ext' (of_option_dom o) $ Ξ» h₁ hβ‚‚, by cases o; [cases h₁, refl] instance : has_coe (option Ξ±) (roption Ξ±) := ⟨of_option⟩ @[simp] theorem mem_coe {a : Ξ±} {o : option Ξ±} : a ∈ (o : roption Ξ±) ↔ a ∈ o := mem_of_option @[simp] theorem coe_none : (@option.none Ξ± : roption Ξ±) = none := rfl @[simp] theorem coe_some (a : Ξ±) : (option.some a : roption Ξ±) = some a := rfl @[elab_as_eliminator] protected lemma induction_on {P : roption Ξ± β†’ Prop} (a : roption Ξ±) (hnone : P none) (hsome : βˆ€ a : Ξ±, P (some a)) : P a := (classical.em a.dom).elim (Ξ» h, roption.some_get h β–Έ hsome _) (Ξ» h, (eq_none_iff'.2 h).symm β–Έ hnone) instance of_option_decidable : βˆ€ o : option Ξ±, decidable (of_option o).dom | option.none := roption.none_decidable | (option.some a) := roption.some_decidable a @[simp] theorem to_of_option (o : option Ξ±) : to_option (of_option o) = o := by cases o; refl @[simp] theorem of_to_option (o : roption Ξ±) [decidable o.dom] : of_option (to_option o) = o := ext $ Ξ» a, mem_of_option.trans mem_to_option noncomputable def equiv_option : roption Ξ± ≃ option Ξ± := by haveI := classical.dec; exact ⟨λ o, to_option o, of_option, Ξ» o, of_to_option o, Ξ» o, eq.trans (by dsimp; congr) (to_of_option o)⟩ /-- `assert p f` is a bind-like operation which appends an additional condition `p` to the domain and uses `f` to produce the value. -/ def assert (p : Prop) (f : p β†’ roption Ξ±) : roption Ξ± := βŸ¨βˆƒh : p, (f h).dom, Ξ»ha, (f ha.fst).get ha.snd⟩ /-- The bind operation has value `g (f.get)`, and is defined when all the parts are defined. -/ protected def bind (f : roption Ξ±) (g : Ξ± β†’ roption Ξ²) : roption Ξ² := assert (dom f) (Ξ»b, g (f.get b)) /-- The map operation for `roption` just maps the value and maintains the same domain. -/ def map (f : Ξ± β†’ Ξ²) (o : roption Ξ±) : roption Ξ² := ⟨o.dom, f ∘ o.get⟩ theorem mem_map (f : Ξ± β†’ Ξ²) {o : roption Ξ±} : βˆ€ {a}, a ∈ o β†’ f a ∈ map f o | _ ⟨h, rfl⟩ := ⟨_, rfl⟩ @[simp] theorem mem_map_iff (f : Ξ± β†’ Ξ²) {o : roption Ξ±} {b} : b ∈ map f o ↔ βˆƒ a ∈ o, f a = b := ⟨match b with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩, rfl⟩ end, Ξ» ⟨a, h₁, hβ‚‚βŸ©, hβ‚‚ β–Έ mem_map f hβ‚βŸ© @[simp] theorem map_none (f : Ξ± β†’ Ξ²) : map f none = none := eq_none_iff.2 $ Ξ» a, by simp @[simp] theorem map_some (f : Ξ± β†’ Ξ²) (a : Ξ±) : map f (some a) = some (f a) := eq_some_iff.2 $ mem_map f $ mem_some _ theorem mem_assert {p : Prop} {f : p β†’ roption Ξ±} : βˆ€ {a} (h : p), a ∈ f h β†’ a ∈ assert p f | _ _ ⟨h, rfl⟩ := ⟨⟨_, _⟩, rfl⟩ @[simp] theorem mem_assert_iff {p : Prop} {f : p β†’ roption Ξ±} {a} : a ∈ assert p f ↔ βˆƒ h : p, a ∈ f h := ⟨match a with _, ⟨h, rfl⟩ := ⟨_, ⟨_, rfl⟩⟩ end, Ξ» ⟨a, h⟩, mem_assert _ h⟩ theorem mem_bind {f : roption Ξ±} {g : Ξ± β†’ roption Ξ²} : βˆ€ {a b}, a ∈ f β†’ b ∈ g a β†’ b ∈ f.bind g | _ _ ⟨h, rfl⟩ ⟨hβ‚‚, rfl⟩ := ⟨⟨_, _⟩, rfl⟩ @[simp] theorem mem_bind_iff {f : roption Ξ±} {g : Ξ± β†’ roption Ξ²} {b} : b ∈ f.bind g ↔ βˆƒ a ∈ f, b ∈ g a := ⟨match b with _, ⟨⟨h₁, hβ‚‚βŸ©, rfl⟩ := ⟨_, ⟨_, rfl⟩, ⟨_, rfl⟩⟩ end, Ξ» ⟨a, h₁, hβ‚‚βŸ©, mem_bind h₁ hβ‚‚βŸ© @[simp] theorem bind_none (f : Ξ± β†’ roption Ξ²) : none.bind f = none := eq_none_iff.2 $ Ξ» a, by simp @[simp] theorem bind_some (a : Ξ±) (f : Ξ± β†’ roption Ξ²) : (some a).bind f = f a := ext $ by simp theorem bind_some_eq_map (f : Ξ± β†’ Ξ²) (x : roption Ξ±) : x.bind (some ∘ f) = map f x := ext $ by simp [eq_comm] theorem bind_assoc {Ξ³} (f : roption Ξ±) (g : Ξ± β†’ roption Ξ²) (k : Ξ² β†’ roption Ξ³) : (f.bind g).bind k = f.bind (Ξ» x, (g x).bind k) := ext $ Ξ» a, by simp; exact ⟨λ ⟨_, ⟨_, h₁, hβ‚‚βŸ©, hβ‚ƒβŸ©, ⟨_, h₁, _, hβ‚‚, hβ‚ƒβŸ©, Ξ» ⟨_, h₁, _, hβ‚‚, hβ‚ƒβŸ©, ⟨_, ⟨_, h₁, hβ‚‚βŸ©, hβ‚ƒβŸ©βŸ© @[simp] theorem bind_map {Ξ³} (f : Ξ± β†’ Ξ²) (x) (g : Ξ² β†’ roption Ξ³) : (map f x).bind g = x.bind (Ξ» y, g (f y)) := by rw [← bind_some_eq_map, bind_assoc]; simp @[simp] theorem map_bind {Ξ³} (f : Ξ± β†’ roption Ξ²) (x : roption Ξ±) (g : Ξ² β†’ Ξ³) : map g (x.bind f) = x.bind (Ξ» y, map g (f y)) := by rw [← bind_some_eq_map, bind_assoc]; simp [bind_some_eq_map] theorem map_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ²) (o : roption Ξ±) : map g (map f o) = map (g ∘ f) o := by rw [← bind_some_eq_map, bind_map, bind_some_eq_map] instance : monad roption := { pure := @some, map := @map, bind := @roption.bind } instance : is_lawful_monad roption := { bind_pure_comp_eq_map := @bind_some_eq_map, id_map := Ξ» Ξ² f, by cases f; refl, pure_bind := @bind_some, bind_assoc := @bind_assoc } theorem map_id' {f : Ξ± β†’ Ξ±} (H : βˆ€ (x : Ξ±), f x = x) (o) : map f o = o := by rw [show f = id, from funext H]; exact id_map o @[simp] theorem bind_some_right (x : roption Ξ±) : x.bind some = x := by rw [bind_some_eq_map]; simp [map_id'] @[simp] theorem ret_eq_some (a : Ξ±) : return a = some a := rfl @[simp] theorem map_eq_map {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (o : roption Ξ±) : f <$> o = map f o := rfl @[simp] theorem bind_eq_bind {Ξ± Ξ²} (f : roption Ξ±) (g : Ξ± β†’ roption Ξ²) : f >>= g = f.bind g := rfl instance : monad_fail roption := { fail := Ξ»_ _, none, ..roption.monad } /- `restrict p o h` replaces the domain of `o` with `p`, and is well defined when `p` implies `o` is defined. -/ def restrict (p : Prop) : βˆ€ (o : roption Ξ±), (p β†’ o.dom) β†’ roption Ξ± | ⟨d, f⟩ H := ⟨p, Ξ»h, f (H h)⟩ @[simp] theorem mem_restrict (p : Prop) (o : roption Ξ±) (h : p β†’ o.dom) (a : Ξ±) : a ∈ restrict p o h ↔ p ∧ a ∈ o := begin cases o, dsimp [restrict, mem_eq], split, { rintro ⟨hβ‚€, hβ‚βŸ©, exact ⟨hβ‚€, ⟨_, hβ‚βŸ©βŸ© }, rintro ⟨hβ‚€, h₁, hβ‚‚βŸ©, exact ⟨hβ‚€, hβ‚‚βŸ© end /-- `unwrap o` gets the value at `o`, ignoring the condition. (This function is unsound.) -/ meta def unwrap (o : roption Ξ±) : Ξ± := o.get undefined theorem assert_defined {p : Prop} {f : p β†’ roption Ξ±} : βˆ€ (h : p), (f h).dom β†’ (assert p f).dom := exists.intro theorem bind_defined {f : roption Ξ±} {g : Ξ± β†’ roption Ξ²} : βˆ€ (h : f.dom), (g (f.get h)).dom β†’ (f.bind g).dom := assert_defined @[simp] theorem bind_dom {f : roption Ξ±} {g : Ξ± β†’ roption Ξ²} : (f.bind g).dom ↔ βˆƒ h : f.dom, (g (f.get h)).dom := iff.rfl end roption /-- `pfun Ξ± Ξ²`, or `Ξ± β†’. Ξ²`, is the type of partial functions from `Ξ±` to `Ξ²`. It is defined as `Ξ± β†’ roption Ξ²`. -/ def pfun (Ξ± : Type*) (Ξ² : Type*) := Ξ± β†’ roption Ξ² infixr ` β†’. `:25 := pfun namespace pfun variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} instance : inhabited (Ξ± β†’. Ξ²) := ⟨λ a, roption.none⟩ /-- The domain of a partial function -/ def dom (f : Ξ± β†’. Ξ²) : set Ξ± := Ξ» a, (f a).dom theorem mem_dom (f : Ξ± β†’. Ξ²) (x : Ξ±) : x ∈ dom f ↔ βˆƒ y, y ∈ f x := by simp [dom, set.mem_def, roption.dom_iff_mem] theorem dom_eq (f : Ξ± β†’. Ξ²) : dom f = {x | βˆƒ y, y ∈ f x} := set.ext (mem_dom f) /-- Evaluate a partial function -/ def fn (f : Ξ± β†’. Ξ²) (x) (h : dom f x) : Ξ² := (f x).get h /-- Evaluate a partial function to return an `option` -/ def eval_opt (f : Ξ± β†’. Ξ²) [D : decidable_pred (dom f)] (x : Ξ±) : option Ξ² := @roption.to_option _ _ (D x) /-- Partial function extensionality -/ theorem ext' {f g : Ξ± β†’. Ξ²} (H1 : βˆ€ a, a ∈ dom f ↔ a ∈ dom g) (H2 : βˆ€ a p q, f.fn a p = g.fn a q) : f = g := funext $ Ξ» a, roption.ext' (H1 a) (H2 a) theorem ext {f g : Ξ± β†’. Ξ²} (H : βˆ€ a b, b ∈ f a ↔ b ∈ g a) : f = g := funext $ Ξ» a, roption.ext (H a) /-- Turn a partial function into a function out of a subtype -/ def as_subtype (f : Ξ± β†’. Ξ²) (s : {x // f.dom x}) : Ξ² := f.fn s.1 s.2 def equiv_subtype : (Ξ± β†’. Ξ²) ≃ (Ξ£ p : Ξ± β†’ Prop, subtype p β†’ Ξ²) := ⟨λ f, ⟨f.dom, as_subtype f⟩, Ξ» ⟨p, f⟩ x, ⟨p x, Ξ» h, f ⟨x, h⟩⟩, Ξ» f, funext $ Ξ» a, roption.eta _, Ξ» ⟨p, f⟩, by dsimp; congr; funext a; cases a; refl⟩ theorem as_subtype_eq_of_mem {f : Ξ± β†’. Ξ²} {x : Ξ±} {y : Ξ²} (fxy : y ∈ f x) (domx : x ∈ f.dom) : f.as_subtype ⟨x, domx⟩ = y := roption.mem_unique (roption.get_mem _) fxy /-- Turn a total function into a partial function -/ protected def lift (f : Ξ± β†’ Ξ²) : Ξ± β†’. Ξ² := Ξ» a, roption.some (f a) instance : has_coe (Ξ± β†’ Ξ²) (Ξ± β†’. Ξ²) := ⟨pfun.lift⟩ @[simp] theorem lift_eq_coe (f : Ξ± β†’ Ξ²) : pfun.lift f = f := rfl @[simp] theorem coe_val (f : Ξ± β†’ Ξ²) (a : Ξ±) : (f : Ξ± β†’. Ξ²) a = roption.some (f a) := rfl /-- The graph of a partial function is the set of pairs `(x, f x)` where `x` is in the domain of `f`. -/ def graph (f : Ξ± β†’. Ξ²) : set (Ξ± Γ— Ξ²) := {p | p.2 ∈ f p.1} def graph' (f : Ξ± β†’. Ξ²) : rel Ξ± Ξ² := Ξ» x y, y ∈ f x /-- The range of a partial function is the set of values `f x` where `x` is in the domain of `f`. -/ def ran (f : Ξ± β†’. Ξ²) : set Ξ² := {b | βˆƒa, b ∈ f a} /-- Restrict a partial function to a smaller domain. -/ def restrict (f : Ξ± β†’. Ξ²) {p : set Ξ±} (H : p βŠ† f.dom) : Ξ± β†’. Ξ² := Ξ» x, roption.restrict (p x) (f x) (@H x) @[simp] theorem mem_restrict {f : Ξ± β†’. Ξ²} {s : set Ξ±} (h : s βŠ† f.dom) (a : Ξ±) (b : Ξ²) : b ∈ restrict f h a ↔ a ∈ s ∧ b ∈ f a := by { simp [restrict], reflexivity } def res (f : Ξ± β†’ Ξ²) (s : set Ξ±) : Ξ± β†’. Ξ² := restrict (pfun.lift f) (set.subset_univ s) theorem mem_res (f : Ξ± β†’ Ξ²) (s : set Ξ±) (a : Ξ±) (b : Ξ²) : b ∈ res f s a ↔ (a ∈ s ∧ f a = b) := by { simp [res], split; {intro h, simp [h]} } theorem res_univ (f : Ξ± β†’ Ξ²) : pfun.res f set.univ = f := rfl theorem dom_iff_graph (f : Ξ± β†’. Ξ²) (x : Ξ±) : x ∈ f.dom ↔ βˆƒy, (x, y) ∈ f.graph := roption.dom_iff_mem theorem lift_graph {f : Ξ± β†’ Ξ²} {a b} : (a, b) ∈ (f : Ξ± β†’. Ξ²).graph ↔ f a = b := show (βˆƒ (h : true), f a = b) ↔ f a = b, by simp /-- The monad `pure` function, the total constant `x` function -/ protected def pure (x : Ξ²) : Ξ± β†’. Ξ² := Ξ»_, roption.some x /-- The monad `bind` function, pointwise `roption.bind` -/ def bind (f : Ξ± β†’. Ξ²) (g : Ξ² β†’ Ξ± β†’. Ξ³) : Ξ± β†’. Ξ³ := Ξ»a, roption.bind (f a) (Ξ»b, g b a) /-- The monad `map` function, pointwise `roption.map` -/ def map (f : Ξ² β†’ Ξ³) (g : Ξ± β†’. Ξ²) : Ξ± β†’. Ξ³ := Ξ»a, roption.map f (g a) instance : monad (pfun Ξ±) := { pure := @pfun.pure _, bind := @pfun.bind _, map := @pfun.map _ } instance : is_lawful_monad (pfun Ξ±) := { bind_pure_comp_eq_map := Ξ» Ξ² Ξ³ f x, funext $ Ξ» a, roption.bind_some_eq_map _ _, id_map := Ξ» Ξ² f, by funext a; dsimp [functor.map, pfun.map]; cases f a; refl, pure_bind := Ξ» Ξ² Ξ³ x f, funext $ Ξ» a, roption.bind_some.{u_1 u_2} _ (f x), bind_assoc := Ξ» Ξ² Ξ³ Ξ΄ f g k, funext $ Ξ» a, roption.bind_assoc (f a) (Ξ» b, g b a) (Ξ» b, k b a) } theorem pure_defined (p : set Ξ±) (x : Ξ²) : p βŠ† (@pfun.pure Ξ± _ x).dom := set.subset_univ p theorem bind_defined {Ξ± Ξ² Ξ³} (p : set Ξ±) {f : Ξ± β†’. Ξ²} {g : Ξ² β†’ Ξ± β†’. Ξ³} (H1 : p βŠ† f.dom) (H2 : βˆ€x, p βŠ† (g x).dom) : p βŠ† (f >>= g).dom := Ξ»a ha, (⟨H1 ha, H2 _ ha⟩ : (f >>= g).dom a) def fix (f : Ξ± β†’. Ξ² βŠ• Ξ±) : Ξ± β†’. Ξ² := Ξ» a, roption.assert (acc (Ξ» x y, sum.inr x ∈ f y) a) $ Ξ» h, @well_founded.fix_F _ (Ξ» x y, sum.inr x ∈ f y) _ (Ξ» a IH, roption.assert (f a).dom $ Ξ» hf, by cases e : (f a).get hf with b a'; [exact roption.some b, exact IH _ ⟨hf, e⟩]) a h theorem dom_of_mem_fix {f : Ξ± β†’. Ξ² βŠ• Ξ±} {a : Ξ±} {b : Ξ²} (h : b ∈ fix f a) : (f a).dom := let ⟨h₁, hβ‚‚βŸ© := roption.mem_assert_iff.1 h in by rw well_founded.fix_F_eq at hβ‚‚; exact hβ‚‚.fst.fst theorem mem_fix_iff {f : Ξ± β†’. Ξ² βŠ• Ξ±} {a : Ξ±} {b : Ξ²} : b ∈ fix f a ↔ sum.inl b ∈ f a ∨ βˆƒ a', sum.inr a' ∈ f a ∧ b ∈ fix f a' := ⟨λ h, let ⟨h₁, hβ‚‚βŸ© := roption.mem_assert_iff.1 h in begin rw well_founded.fix_F_eq at hβ‚‚, simp at hβ‚‚, cases hβ‚‚ with hβ‚‚ h₃, cases e : (f a).get hβ‚‚ with b' a'; simp [e] at h₃, { subst b', refine or.inl ⟨hβ‚‚, e⟩ }, { exact or.inr ⟨a', ⟨_, e⟩, roption.mem_assert _ hβ‚ƒβŸ© } end, Ξ» h, begin simp [fix], rcases h with ⟨h₁, hβ‚‚βŸ© | ⟨a', h, hβ‚ƒβŸ©, { refine ⟨⟨_, Ξ» y h', _⟩, _⟩, { injection roption.mem_unique ⟨h₁, hβ‚‚βŸ© h' }, { rw well_founded.fix_F_eq, simp [h₁, hβ‚‚] } }, { simp [fix] at h₃, cases h₃ with h₃ hβ‚„, refine ⟨⟨_, Ξ» y h', _⟩, _⟩, { injection roption.mem_unique h h' with e, exact e β–Έ h₃ }, { cases h with h₁ hβ‚‚, rw well_founded.fix_F_eq, simp [h₁, hβ‚‚, hβ‚„] } } end⟩ @[elab_as_eliminator] def fix_induction {f : Ξ± β†’. Ξ² βŠ• Ξ±} {b : Ξ²} {C : Ξ± β†’ Sort*} {a : Ξ±} (h : b ∈ fix f a) (H : βˆ€ a, b ∈ fix f a β†’ (βˆ€ a', b ∈ fix f a' β†’ sum.inr a' ∈ f a β†’ C a') β†’ C a) : C a := begin replace h := roption.mem_assert_iff.1 h, have := h.snd, revert this, induction h.fst with a ha IH, intro hβ‚‚, refine H a (roption.mem_assert_iff.2 ⟨⟨_, ha⟩, hβ‚‚βŸ©) (Ξ» a' ha' fa', _), have := (roption.mem_assert_iff.1 ha').snd, exact IH _ fa' ⟨ha _ fa', this⟩ this end end pfun namespace pfun variables {Ξ± : Type*} {Ξ² : Type*} (f : Ξ± β†’. Ξ²) def image (s : set Ξ±) : set Ξ² := rel.image f.graph' s lemma image_def (s : set Ξ±) : image f s = {y | βˆƒ x ∈ s, y ∈ f x} := rfl lemma mem_image (y : Ξ²) (s : set Ξ±) : y ∈ image f s ↔ βˆƒ x ∈ s, y ∈ f x := iff.rfl lemma image_mono {s t : set Ξ±} (h : s βŠ† t) : f.image s βŠ† f.image t := rel.image_mono _ h lemma image_inter (s t : set Ξ±) : f.image (s ∩ t) βŠ† f.image s ∩ f.image t := rel.image_inter _ s t lemma image_union (s t : set Ξ±) : f.image (s βˆͺ t) = f.image s βˆͺ f.image t := rel.image_union _ s t def preimage (s : set Ξ²) : set Ξ± := rel.preimage (Ξ» x y, y ∈ f x) s lemma preimage_def (s : set Ξ²) : preimage f s = {x | βˆƒ y ∈ s, y ∈ f x} := rfl lemma mem_preimage (s : set Ξ²) (x : Ξ±) : x ∈ preimage f s ↔ βˆƒ y ∈ s, y ∈ f x := iff.rfl lemma preimage_subset_dom (s : set Ξ²) : f.preimage s βŠ† f.dom := assume x ⟨y, ys, fxy⟩, roption.dom_iff_mem.mpr ⟨y, fxy⟩ lemma preimage_mono {s t : set Ξ²} (h : s βŠ† t) : f.preimage s βŠ† f.preimage t := rel.preimage_mono _ h lemma preimage_inter (s t : set Ξ²) : f.preimage (s ∩ t) βŠ† f.preimage s ∩ f.preimage t := rel.preimage_inter _ s t lemma preimage_union (s t : set Ξ²) : f.preimage (s βˆͺ t) = f.preimage s βˆͺ f.preimage t := rel.preimage_union _ s t lemma preimage_univ : f.preimage set.univ = f.dom := by ext; simp [mem_preimage, mem_dom] def core (s : set Ξ²) : set Ξ± := rel.core f.graph' s lemma core_def (s : set Ξ²) : core f s = {x | βˆ€ y, y ∈ f x β†’ y ∈ s} := rfl lemma mem_core (x : Ξ±) (s : set Ξ²) : x ∈ core f s ↔ (βˆ€ y, y ∈ f x β†’ y ∈ s) := iff.rfl lemma compl_dom_subset_core (s : set Ξ²) : -f.dom βŠ† f.core s := assume x hx y fxy, absurd ((mem_dom f x).mpr ⟨y, fxy⟩) hx lemma core_mono {s t : set Ξ²} (h : s βŠ† t) : f.core s βŠ† f.core t := rel.core_mono _ h lemma core_inter (s t : set Ξ²) : f.core (s ∩ t) = f.core s ∩ f.core t := rel.core_inter _ s t lemma mem_core_res (f : Ξ± β†’ Ξ²) (s : set Ξ±) (t : set Ξ²) (x : Ξ±) : x ∈ core (res f s) t ↔ (x ∈ s β†’ f x ∈ t) := begin simp [mem_core, mem_res], split, { intros h h', apply h _ h', reflexivity }, intros h y xs fxeq, rw ←fxeq, exact h xs end section open_locale classical lemma core_res (f : Ξ± β†’ Ξ²) (s : set Ξ±) (t : set Ξ²) : core (res f s) t = -s βˆͺ f ⁻¹' t := by { ext, rw mem_core_res, by_cases h : x ∈ s; simp [h] } end lemma core_restrict (f : Ξ± β†’ Ξ²) (s : set Ξ²) : core (f : Ξ± β†’. Ξ²) s = set.preimage f s := by ext x; simp [core_def] lemma preimage_subset_core (f : Ξ± β†’. Ξ²) (s : set Ξ²) : f.preimage s βŠ† f.core s := assume x ⟨y, ys, fxy⟩ y' fxy', have y = y', from roption.mem_unique fxy fxy', this β–Έ ys lemma preimage_eq (f : Ξ± β†’. Ξ²) (s : set Ξ²) : f.preimage s = f.core s ∩ f.dom := set.eq_of_subset_of_subset (set.subset_inter (preimage_subset_core f s) (preimage_subset_dom f s)) (assume x ⟨xcore, xdom⟩, let y := (f x).get xdom in have ys : y ∈ s, from xcore _ (roption.get_mem _), show x ∈ preimage f s, from ⟨(f x).get xdom, ys, roption.get_mem _⟩) lemma core_eq (f : Ξ± β†’. Ξ²) (s : set Ξ²) : f.core s = f.preimage s βˆͺ -f.dom := by rw [preimage_eq, set.union_distrib_right, set.union_comm (dom f), set.compl_union_self, set.inter_univ, set.union_eq_self_of_subset_right (compl_dom_subset_core f s)] lemma preimage_as_subtype (f : Ξ± β†’. Ξ²) (s : set Ξ²) : f.as_subtype ⁻¹' s = subtype.val ⁻¹' pfun.preimage f s := begin ext x, simp only [set.mem_preimage, set.mem_set_of_eq, pfun.as_subtype, pfun.mem_preimage], show pfun.fn f (x.val) _ ∈ s ↔ βˆƒ y ∈ s, y ∈ f (x.val), exact iff.intro (assume h, ⟨_, h, roption.get_mem _⟩) (assume ⟨y, ys, fxy⟩, have f.fn x.val x.property ∈ f x.val := roption.get_mem _, roption.mem_unique fxy this β–Έ ys) end end pfun
fe40d072cdb309a49ce188e6f97897c2ec46aa89
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/data/nat/lemmas.lean
3db34522d48107b856df6cc039ebba76485289ef
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
47,801
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad -/ prelude import init.data.nat.basic init.data.nat.div init.meta init.algebra.functions universes u namespace nat attribute [pre_smt] nat_zero_eq_zero protected lemma add_comm : βˆ€ n m : β„•, n + m = m + n | n 0 := eq.symm (nat.zero_add n) | n (m+1) := suffices succ (n + m) = succ (m + n), from eq.symm (succ_add m n) β–Έ this, congr_arg succ (add_comm n m) protected lemma add_assoc : βˆ€ n m k : β„•, (n + m) + k = n + (m + k) | n m 0 := rfl | n m (succ k) := by rw [add_succ, add_succ, add_assoc] protected lemma add_left_comm : βˆ€ (n m k : β„•), n + (m + k) = m + (n + k) := left_comm nat.add nat.add_comm nat.add_assoc protected lemma add_left_cancel : βˆ€ {n m k : β„•}, n + m = n + k β†’ m = k | 0 m k := by simp [nat.zero_add] {contextual := tt} | (succ n) m k := Ξ» h, have n+m = n+k, by { simp [succ_add] at h, assumption }, add_left_cancel this protected lemma add_right_cancel {n m k : β„•} (h : n + m = k + m) : n = k := have m + n = m + k, by rwa [nat.add_comm n m, nat.add_comm k m] at h, nat.add_left_cancel this lemma succ_ne_zero (n : β„•) : succ n β‰  0 := assume h, nat.no_confusion h lemma succ_ne_self : βˆ€ n : β„•, succ n β‰  n | 0 h := absurd h (nat.succ_ne_zero 0) | (n+1) h := succ_ne_self n (nat.no_confusion h (Ξ» h, h)) protected lemma one_ne_zero : 1 β‰  (0 : β„•) := assume h, nat.no_confusion h protected lemma zero_ne_one : 0 β‰  (1 : β„•) := assume h, nat.no_confusion h lemma eq_zero_of_add_eq_zero_right : βˆ€ {n m : β„•}, n + m = 0 β†’ n = 0 | 0 m := by simp [nat.zero_add] | (n+1) m := Ξ» h, begin exfalso, rw [add_one, succ_add] at h, apply succ_ne_zero _ h end lemma eq_zero_of_add_eq_zero_left {n m : β„•} (h : n + m = 0) : m = 0 := @eq_zero_of_add_eq_zero_right m n (nat.add_comm n m β–Έ h) @[simp] lemma pred_zero : pred 0 = 0 := rfl @[simp] lemma pred_succ (n : β„•) : pred (succ n) = n := rfl protected lemma mul_zero (n : β„•) : n * 0 = 0 := rfl lemma mul_succ (n m : β„•) : n * succ m = n * m + n := rfl protected theorem zero_mul : βˆ€ (n : β„•), 0 * n = 0 | 0 := rfl | (succ n) := by rw [mul_succ, zero_mul] private meta def sort_add := `[simp [nat.add_assoc, nat.add_comm, nat.add_left_comm]] lemma succ_mul : βˆ€ (n m : β„•), (succ n) * m = (n * m) + m | n 0 := rfl | n (succ m) := begin simp [mul_succ, add_succ, succ_mul n m], sort_add end protected lemma right_distrib : βˆ€ (n m k : β„•), (n + m) * k = n * k + m * k | n m 0 := rfl | n m (succ k) := begin simp [mul_succ, right_distrib n m k], sort_add end protected lemma left_distrib : βˆ€ (n m k : β„•), n * (m + k) = n * m + n * k | 0 m k := by simp [nat.zero_mul] | (succ n) m k := begin simp [succ_mul, left_distrib n m k], sort_add end protected lemma mul_comm : βˆ€ (n m : β„•), n * m = m * n | n 0 := by rw [nat.zero_mul, nat.mul_zero] | n (succ m) := by simp [mul_succ, succ_mul, mul_comm n m] protected lemma mul_assoc : βˆ€ (n m k : β„•), (n * m) * k = n * (m * k) | n m 0 := rfl | n m (succ k) := by simp [mul_succ, nat.left_distrib, mul_assoc n m k] protected lemma mul_one : βˆ€ (n : β„•), n * 1 = n := nat.zero_add protected lemma one_mul (n : β„•) : 1 * n = n := by rw [nat.mul_comm, nat.mul_one] /- properties of inequality -/ protected lemma le_of_eq {n m : β„•} (p : n = m) : n ≀ m := p β–Έ less_than_or_equal.refl lemma le_succ_of_le {n m : β„•} (h : n ≀ m) : n ≀ succ m := nat.le_trans h (le_succ m) lemma le_of_succ_le {n m : β„•} (h : succ n ≀ m) : n ≀ m := nat.le_trans (le_succ n) h protected lemma le_of_lt {n m : β„•} (h : n < m) : n ≀ m := le_of_succ_le h lemma lt.step {n m : β„•} : n < m β†’ n < succ m := less_than_or_equal.step lemma eq_zero_or_pos (n : β„•) : n = 0 ∨ 0 < n := by {cases n, exact or.inl rfl, exact or.inr (succ_pos _)} protected lemma pos_of_ne_zero {n : nat} : n β‰  0 β†’ 0 < n := or.resolve_left (eq_zero_or_pos n) protected lemma lt_trans {n m k : β„•} (h₁ : n < m) : m < k β†’ n < k := nat.le_trans (less_than_or_equal.step h₁) protected lemma lt_of_le_of_lt {n m k : β„•} (h₁ : n ≀ m) : m < k β†’ n < k := nat.le_trans (succ_le_succ h₁) lemma lt.base (n : β„•) : n < succ n := nat.le_refl (succ n) lemma lt_succ_self (n : β„•) : n < succ n := lt.base n protected lemma le_antisymm {n m : β„•} (h₁ : n ≀ m) : m ≀ n β†’ n = m := less_than_or_equal.cases_on h₁ (Ξ» a, rfl) (Ξ» a b c, absurd (nat.lt_of_le_of_lt b c) (nat.lt_irrefl n)) protected lemma lt_or_ge : βˆ€ (a b : β„•), a < b ∨ b ≀ a | a 0 := or.inr (zero_le a) | a (b+1) := match lt_or_ge a b with | or.inl h := or.inl (le_succ_of_le h) | or.inr h := match nat.eq_or_lt_of_le h with | or.inl h1 := or.inl (h1 β–Έ lt_succ_self b) | or.inr h1 := or.inr h1 end end protected lemma le_total {m n : β„•} : m ≀ n ∨ n ≀ m := or.imp_left nat.le_of_lt (nat.lt_or_ge m n) protected lemma lt_of_le_and_ne {m n : β„•} (h1 : m ≀ n) : m β‰  n β†’ m < n := or.resolve_right (or.swap (nat.eq_or_lt_of_le h1)) protected lemma lt_iff_le_not_le {m n : β„•} : m < n ↔ (m ≀ n ∧ Β¬ n ≀ m) := ⟨λ hmn, ⟨nat.le_of_lt hmn, Ξ» hnm, nat.lt_irrefl _ (nat.lt_of_le_of_lt hnm hmn)⟩, Ξ» ⟨hmn, hnm⟩, nat.lt_of_le_and_ne hmn (Ξ» heq, hnm (heq β–Έ nat.le_refl _))⟩ instance : linear_order β„• := { le := nat.less_than_or_equal, le_refl := @nat.le_refl, le_trans := @nat.le_trans, le_antisymm := @nat.le_antisymm, le_total := @nat.le_total, lt := nat.lt, lt_iff_le_not_le := @nat.lt_iff_le_not_le, decidable_lt := nat.decidable_lt, decidable_le := nat.decidable_le, decidable_eq := nat.decidable_eq } lemma eq_zero_of_le_zero {n : nat} (h : n ≀ 0) : n = 0 := le_antisymm h (zero_le _) lemma succ_lt_succ {a b : β„•} : a < b β†’ succ a < succ b := succ_le_succ lemma lt_of_succ_lt {a b : β„•} : succ a < b β†’ a < b := le_of_succ_le lemma lt_of_succ_lt_succ {a b : β„•} : succ a < succ b β†’ a < b := le_of_succ_le_succ lemma pred_lt_pred : βˆ€ {n m : β„•}, n β‰  0 β†’ n < m β†’ pred n < pred m | 0 _ h₁ h := absurd rfl h₁ | _ 0 h₁ h := absurd h (not_lt_zero _) | (succ n) (succ m) _ h := lt_of_succ_lt_succ h lemma lt_of_succ_le {a b : β„•} (h : succ a ≀ b) : a < b := h lemma succ_le_of_lt {a b : β„•} (h : a < b) : succ a ≀ b := h lemma le_add_right : βˆ€ (n k : β„•), n ≀ n + k | n 0 := nat.le_refl n | n (k+1) := le_succ_of_le (le_add_right n k) lemma le_add_left (n m : β„•): n ≀ m + n := nat.add_comm n m β–Έ le_add_right n m lemma le.dest : βˆ€ {n m : β„•}, n ≀ m β†’ βˆƒ k, n + k = m | n _ less_than_or_equal.refl := ⟨0, rfl⟩ | n _ (less_than_or_equal.step h) := match le.dest h with | ⟨w, hw⟩ := ⟨succ w, hw β–Έ add_succ n w⟩ end lemma le.intro {n m k : β„•} (h : n + k = m) : n ≀ m := h β–Έ le_add_right n k protected lemma add_le_add_left {n m : β„•} (h : n ≀ m) (k : β„•) : k + n ≀ k + m := match le.dest h with | ⟨w, hw⟩ := @le.intro _ _ w begin rw [nat.add_assoc, hw] end end protected lemma add_le_add_right {n m : β„•} (h : n ≀ m) (k : β„•) : n + k ≀ m + k := begin rw [nat.add_comm n k, nat.add_comm m k], apply nat.add_le_add_left h end protected lemma le_of_add_le_add_left {k n m : β„•} (h : k + n ≀ k + m) : n ≀ m := match le.dest h with | ⟨w, hw⟩ := @le.intro _ _ w begin rw [nat.add_assoc] at hw, apply nat.add_left_cancel hw end end protected lemma le_of_add_le_add_right {k n m : β„•} : n + k ≀ m + k β†’ n ≀ m := begin rw [nat.add_comm _ k, nat.add_comm _ k], apply nat.le_of_add_le_add_left end protected lemma add_le_add_iff_le_right (k n m : β„•) : n + k ≀ m + k ↔ n ≀ m := ⟨ nat.le_of_add_le_add_right , assume h, nat.add_le_add_right h _ ⟩ protected theorem lt_of_add_lt_add_left {k n m : β„•} (h : k + n < k + m) : n < m := let h' := nat.le_of_lt h in nat.lt_of_le_and_ne (nat.le_of_add_le_add_left h') (Ξ» heq, nat.lt_irrefl (k + m) begin rw heq at h, assumption end) protected lemma lt_of_add_lt_add_right {a b c : β„•} (h : a + b < c + b) : a < c := nat.lt_of_add_lt_add_left $ show b + a < b + c, by rwa [nat.add_comm b a, nat.add_comm b c] protected lemma add_lt_add_left {n m : β„•} (h : n < m) (k : β„•) : k + n < k + m := lt_of_succ_le (add_succ k n β–Έ nat.add_le_add_left (succ_le_of_lt h) k) protected lemma add_lt_add_right {n m : β„•} (h : n < m) (k : β„•) : n + k < m + k := nat.add_comm k m β–Έ nat.add_comm k n β–Έ nat.add_lt_add_left h k protected lemma lt_add_of_pos_right {n k : β„•} (h : 0 < k) : n < n + k := nat.add_lt_add_left h n protected lemma lt_add_of_pos_left {n k : β„•} (h : 0 < k) : n < k + n := by rw nat.add_comm; exact nat.lt_add_of_pos_right h protected lemma add_lt_add {a b c d : β„•} (h₁ : a < b) (hβ‚‚ : c < d) : a + c < b + d := lt_trans (nat.add_lt_add_right h₁ c) (nat.add_lt_add_left hβ‚‚ b) protected lemma add_le_add {a b c d : β„•} (h₁ : a ≀ b) (hβ‚‚ : c ≀ d) : a + c ≀ b + d := le_trans (nat.add_le_add_right h₁ c) (nat.add_le_add_left hβ‚‚ b) protected lemma zero_lt_one : 0 < (1:nat) := zero_lt_succ 0 lemma mul_le_mul_left {n m : β„•} (k : β„•) (h : n ≀ m) : k * n ≀ k * m := match le.dest h with | ⟨l, hl⟩ := have k * n + k * l = k * m, by rw [← nat.left_distrib, hl], le.intro this end lemma mul_le_mul_right {n m : β„•} (k : β„•) (h : n ≀ m) : n * k ≀ m * k := nat.mul_comm k m β–Έ nat.mul_comm k n β–Έ mul_le_mul_left k h protected lemma mul_lt_mul_of_pos_left {n m k : β„•} (h : n < m) (hk : 0 < k) : k * n < k * m := nat.lt_of_lt_of_le (nat.lt_add_of_pos_right hk) (mul_succ k n β–Έ nat.mul_le_mul_left k (succ_le_of_lt h)) protected lemma mul_lt_mul_of_pos_right {n m k : β„•} (h : n < m) (hk : 0 < k) : n * k < m * k := nat.mul_comm k m β–Έ nat.mul_comm k n β–Έ nat.mul_lt_mul_of_pos_left h hk protected lemma le_of_mul_le_mul_left {a b c : β„•} (h : c * a ≀ c * b) (hc : 0 < c) : a ≀ b := not_lt.1 (assume h1 : b < a, have h2 : c * b < c * a, from nat.mul_lt_mul_of_pos_left h1 hc, not_le_of_gt h2 h) lemma le_of_lt_succ {m n : nat} : m < succ n β†’ m ≀ n := le_of_succ_le_succ theorem eq_of_mul_eq_mul_left {m k n : β„•} (Hn : 0 < n) (H : n * m = n * k) : m = k := le_antisymm (nat.le_of_mul_le_mul_left (le_of_eq H) Hn) (nat.le_of_mul_le_mul_left (le_of_eq H.symm) Hn) /- sub properties -/ @[simp] protected lemma zero_sub : βˆ€ a : β„•, 0 - a = 0 | 0 := rfl | (a+1) := congr_arg pred (zero_sub a) lemma sub_lt_succ (a b : β„•) : a - b < succ a := lt_succ_of_le (sub_le a b) protected theorem sub_le_sub_right {n m : β„•} (h : n ≀ m) : βˆ€ k, n - k ≀ m - k | 0 := h | (succ z) := pred_le_pred (sub_le_sub_right z) /- bit0/bit1 properties -/ protected lemma bit1_eq_succ_bit0 (n : β„•) : bit1 n = succ (bit0 n) := rfl protected lemma bit1_succ_eq (n : β„•) : bit1 (succ n) = succ (succ (bit1 n)) := eq.trans (nat.bit1_eq_succ_bit0 (succ n)) (congr_arg succ (nat.bit0_succ_eq n)) protected lemma bit1_ne_one : βˆ€ {n : β„•}, n β‰  0 β†’ bit1 n β‰  1 | 0 h h1 := absurd rfl h | (n+1) h h1 := nat.no_confusion h1 (Ξ» h2, absurd h2 (succ_ne_zero _)) protected lemma bit0_ne_one : βˆ€ n : β„•, bit0 n β‰  1 | 0 h := absurd h (ne.symm nat.one_ne_zero) | (n+1) h := have h1 : succ (succ (n + n)) = 1, from succ_add n n β–Έ h, nat.no_confusion h1 (Ξ» h2, absurd h2 (succ_ne_zero (n + n))) protected lemma add_self_ne_one : βˆ€ (n : β„•), n + n β‰  1 | 0 h := nat.no_confusion h | (n+1) h := have h1 : succ (succ (n + n)) = 1, from succ_add n n β–Έ h, nat.no_confusion h1 (Ξ» h2, absurd h2 (nat.succ_ne_zero (n + n))) protected lemma bit1_ne_bit0 : βˆ€ (n m : β„•), bit1 n β‰  bit0 m | 0 m h := absurd h (ne.symm (nat.add_self_ne_one m)) | (n+1) 0 h := have h1 : succ (bit0 (succ n)) = 0, from h, absurd h1 (nat.succ_ne_zero _) | (n+1) (m+1) h := have h1 : succ (succ (bit1 n)) = succ (succ (bit0 m)), from nat.bit0_succ_eq m β–Έ nat.bit1_succ_eq n β–Έ h, have h2 : bit1 n = bit0 m, from nat.no_confusion h1 (Ξ» h2', nat.no_confusion h2' (Ξ» h2'', h2'')), absurd h2 (bit1_ne_bit0 n m) protected lemma bit0_ne_bit1 : βˆ€ (n m : β„•), bit0 n β‰  bit1 m := Ξ» n m : nat, ne.symm (nat.bit1_ne_bit0 m n) protected lemma bit0_inj : βˆ€ {n m : β„•}, bit0 n = bit0 m β†’ n = m | 0 0 h := rfl | 0 (m+1) h := by contradiction | (n+1) 0 h := by contradiction | (n+1) (m+1) h := have succ (succ (n + n)) = succ (succ (m + m)), by { unfold bit0 at h, simp [add_one, add_succ, succ_add] at h, have aux : n + n = m + m := h, rw aux }, have n + n = m + m, by iterate { injection this with this }, have n = m, from bit0_inj this, by rw this protected lemma bit1_inj : βˆ€ {n m : β„•}, bit1 n = bit1 m β†’ n = m := Ξ» n m h, have succ (bit0 n) = succ (bit0 m), begin simp [nat.bit1_eq_succ_bit0] at h, rw h end, have bit0 n = bit0 m, by injection this, nat.bit0_inj this protected lemma bit0_ne {n m : β„•} : n β‰  m β†’ bit0 n β‰  bit0 m := Ξ» h₁ hβ‚‚, absurd (nat.bit0_inj hβ‚‚) h₁ protected lemma bit1_ne {n m : β„•} : n β‰  m β†’ bit1 n β‰  bit1 m := Ξ» h₁ hβ‚‚, absurd (nat.bit1_inj hβ‚‚) h₁ protected lemma zero_ne_bit0 {n : β„•} : n β‰  0 β†’ 0 β‰  bit0 n := Ξ» h, ne.symm (nat.bit0_ne_zero h) protected lemma zero_ne_bit1 (n : β„•) : 0 β‰  bit1 n := ne.symm (nat.bit1_ne_zero n) protected lemma one_ne_bit0 (n : β„•) : 1 β‰  bit0 n := ne.symm (nat.bit0_ne_one n) protected lemma one_ne_bit1 {n : β„•} : n β‰  0 β†’ 1 β‰  bit1 n := Ξ» h, ne.symm (nat.bit1_ne_one h) protected lemma one_lt_bit1 : βˆ€ {n : nat}, n β‰  0 β†’ 1 < bit1 n | 0 h := by contradiction | (succ n) h := begin rw nat.bit1_succ_eq, apply succ_lt_succ, apply zero_lt_succ end protected lemma one_lt_bit0 : βˆ€ {n : nat}, n β‰  0 β†’ 1 < bit0 n | 0 h := by contradiction | (succ n) h := begin rw nat.bit0_succ_eq, apply succ_lt_succ, apply zero_lt_succ end protected lemma bit0_lt {n m : nat} (h : n < m) : bit0 n < bit0 m := nat.add_lt_add h h protected lemma bit1_lt {n m : nat} (h : n < m) : bit1 n < bit1 m := succ_lt_succ (nat.add_lt_add h h) protected lemma bit0_lt_bit1 {n m : nat} (h : n ≀ m) : bit0 n < bit1 m := lt_succ_of_le (nat.add_le_add h h) protected lemma bit1_lt_bit0 : βˆ€ {n m : nat}, n < m β†’ bit1 n < bit0 m | n 0 h := absurd h (not_lt_zero _) | n (succ m) h := have n ≀ m, from le_of_lt_succ h, have succ (n + n) ≀ succ (m + m), from succ_le_succ (nat.add_le_add this this), have succ (n + n) ≀ succ m + m, {rw succ_add, assumption}, show succ (n + n) < succ (succ m + m), from lt_succ_of_le this protected lemma one_le_bit1 (n : β„•) : 1 ≀ bit1 n := show 1 ≀ succ (bit0 n), from succ_le_succ (zero_le (bit0 n)) protected lemma one_le_bit0 : βˆ€ (n : β„•), n β‰  0 β†’ 1 ≀ bit0 n | 0 h := absurd rfl h | (n+1) h := suffices 1 ≀ succ (succ (bit0 n)), from eq.symm (nat.bit0_succ_eq n) β–Έ this, succ_le_succ (zero_le (succ (bit0 n))) /- subtraction -/ @[simp] protected theorem sub_zero (n : β„•) : n - 0 = n := rfl theorem sub_succ (n m : β„•) : n - succ m = pred (n - m) := rfl theorem succ_sub_succ (n m : β„•) : succ n - succ m = n - m := succ_sub_succ_eq_sub n m protected theorem sub_self : βˆ€ (n : β„•), n - n = 0 | 0 := by rw nat.sub_zero | (succ n) := by rw [succ_sub_succ, sub_self n] /- TODO(Leo): remove the following ematch annotations as soon as we have arithmetic theory in the smt_stactic -/ @[ematch_lhs] protected theorem add_sub_add_right : βˆ€ (n k m : β„•), (n + k) - (m + k) = n - m | n 0 m := by rw [nat.add_zero, nat.add_zero] | n (succ k) m := by rw [add_succ, add_succ, succ_sub_succ, add_sub_add_right n k m] @[ematch_lhs] protected theorem add_sub_add_left (k n m : β„•) : (k + n) - (k + m) = n - m := by rw [nat.add_comm k n, nat.add_comm k m, nat.add_sub_add_right] @[ematch_lhs] protected theorem add_sub_cancel (n m : β„•) : n + m - m = n := suffices n + m - (0 + m) = n, from by rwa [nat.zero_add] at this, by rw [nat.add_sub_add_right, nat.sub_zero] @[ematch_lhs] protected theorem add_sub_cancel_left (n m : β„•) : n + m - n = m := show n + m - (n + 0) = m, from by rw [nat.add_sub_add_left, nat.sub_zero] protected theorem sub_sub : βˆ€ (n m k : β„•), n - m - k = n - (m + k) | n m 0 := by rw [nat.add_zero, nat.sub_zero] | n m (succ k) := by rw [add_succ, nat.sub_succ, nat.sub_succ, sub_sub n m k] theorem le_of_le_of_sub_le_sub_right {n m k : β„•} (hβ‚€ : k ≀ m) (h₁ : n - k ≀ m - k) : n ≀ m := begin revert k m, induction n with n ; intros k m hβ‚€ h₁, { apply zero_le }, { cases k with k, { apply h₁ }, cases m with m, { cases not_succ_le_zero _ hβ‚€ }, { simp [succ_sub_succ] at h₁, apply succ_le_succ, apply n_ih _ h₁, apply le_of_succ_le_succ hβ‚€ }, } end protected theorem sub_le_sub_right_iff (n m k : β„•) (h : k ≀ m) : n - k ≀ m - k ↔ n ≀ m := ⟨ le_of_le_of_sub_le_sub_right h , assume h, nat.sub_le_sub_right h k ⟩ theorem sub_self_add (n m : β„•) : n - (n + m) = 0 := show (n + 0) - (n + m) = 0, from by rw [nat.add_sub_add_left, nat.zero_sub] theorem add_le_to_le_sub (x : β„•) {y k : β„•} (h : k ≀ y) : x + k ≀ y ↔ x ≀ y - k := by rw [← nat.add_sub_cancel x k, nat.sub_le_sub_right_iff _ _ _ h, nat.add_sub_cancel] lemma sub_lt_of_pos_le (a b : β„•) (hβ‚€ : 0 < a) (h₁ : a ≀ b) : b - a < b := begin apply sub_lt _ hβ‚€, apply lt_of_lt_of_le hβ‚€ h₁ end theorem sub_one (n : β„•) : n - 1 = pred n := rfl theorem succ_sub_one (n : β„•) : succ n - 1 = n := rfl theorem succ_pred_eq_of_pos : βˆ€ {n : β„•}, 0 < n β†’ succ (pred n) = n | 0 h := absurd h (lt_irrefl 0) | (succ k) h := rfl theorem sub_eq_zero_of_le {n m : β„•} (h : n ≀ m) : n - m = 0 := exists.elim (nat.le.dest h) (assume k, assume hk : n + k = m, by rw [← hk, sub_self_add]) protected theorem le_of_sub_eq_zero : βˆ€{n m : β„•}, n - m = 0 β†’ n ≀ m | n 0 H := begin rw [nat.sub_zero] at H, simp [H] end | 0 (m+1) H := zero_le _ | (n+1) (m+1) H := nat.add_le_add_right (le_of_sub_eq_zero begin simp [nat.add_sub_add_right] at H, exact H end) _ protected theorem sub_eq_zero_iff_le {n m : β„•} : n - m = 0 ↔ n ≀ m := ⟨nat.le_of_sub_eq_zero, nat.sub_eq_zero_of_le⟩ theorem add_sub_of_le {n m : β„•} (h : n ≀ m) : n + (m - n) = m := exists.elim (nat.le.dest h) (assume k, assume hk : n + k = m, by rw [← hk, nat.add_sub_cancel_left]) protected theorem sub_add_cancel {n m : β„•} (h : m ≀ n) : n - m + m = n := by rw [nat.add_comm, add_sub_of_le h] protected theorem add_sub_assoc {m k : β„•} (h : k ≀ m) (n : β„•) : n + m - k = n + (m - k) := exists.elim (nat.le.dest h) (assume l, assume hl : k + l = m, by rw [← hl, nat.add_sub_cancel_left, nat.add_comm k, ← nat.add_assoc, nat.add_sub_cancel]) protected lemma sub_eq_iff_eq_add {a b c : β„•} (ab : b ≀ a) : a - b = c ↔ a = c + b := ⟨assume c_eq, begin rw [c_eq.symm, nat.sub_add_cancel ab] end, assume a_eq, begin rw [a_eq, nat.add_sub_cancel] end⟩ protected lemma lt_of_sub_eq_succ {m n l : β„•} (H : m - n = nat.succ l) : n < m := not_le.1 (assume (H' : n β‰₯ m), begin simp [nat.sub_eq_zero_of_le H'] at H, contradiction end) lemma zero_min (a : β„•) : min 0 a = 0 := min_eq_left (zero_le a) lemma min_zero (a : β„•) : min a 0 = 0 := min_eq_right (zero_le a) -- Distribute succ over min theorem min_succ_succ (x y : β„•) : min (succ x) (succ y) = succ (min x y) := have f : x ≀ y β†’ min (succ x) (succ y) = succ (min x y), from Ξ»p, calc min (succ x) (succ y) = succ x : if_pos (succ_le_succ p) ... = succ (min x y) : congr_arg succ (eq.symm (if_pos p)), have g : Β¬ (x ≀ y) β†’ min (succ x) (succ y) = succ (min x y), from Ξ»p, calc min (succ x) (succ y) = succ y : if_neg (Ξ»eq, p (pred_le_pred eq)) ... = succ (min x y) : congr_arg succ (eq.symm (if_neg p)), decidable.by_cases f g theorem sub_eq_sub_min (n m : β„•) : n - m = n - min n m := if h : n β‰₯ m then by rewrite [min_eq_right h] else by rewrite [sub_eq_zero_of_le (le_of_not_ge h), min_eq_left (le_of_not_ge h), nat.sub_self] @[simp] theorem sub_add_min_cancel (n m : β„•) : n - m + min n m = n := by rw [sub_eq_sub_min, nat.sub_add_cancel (min_le_left n m)] /- TODO(Leo): sub + inequalities -/ protected def strong_rec_on {p : nat β†’ Sort u} (n : nat) (h : βˆ€ n, (βˆ€ m, m < n β†’ p m) β†’ p n) : p n := suffices βˆ€ n m, m < n β†’ p m, from this (succ n) n (lt_succ_self _), begin intros n, induction n with n ih, {intros m h₁, exact absurd h₁ (not_lt_zero _)}, {intros m h₁, apply or.by_cases (decidable.lt_or_eq_of_le (le_of_lt_succ h₁)), {intros, apply ih, assumption}, {intros, subst m, apply h _ ih}} end protected lemma strong_induction_on {p : nat β†’ Prop} (n : nat) (h : βˆ€ n, (βˆ€ m, m < n β†’ p m) β†’ p n) : p n := nat.strong_rec_on n h protected lemma case_strong_induction_on {p : nat β†’ Prop} (a : nat) (hz : p 0) (hi : βˆ€ n, (βˆ€ m, m ≀ n β†’ p m) β†’ p (succ n)) : p a := nat.strong_induction_on a $ Ξ» n, match n with | 0 := Ξ» _, hz | (n+1) := Ξ» h₁, hi n (Ξ» m hβ‚‚, h₁ _ (lt_succ_of_le hβ‚‚)) end /- mod -/ lemma mod_def (x y : nat) : x % y = if 0 < y ∧ y ≀ x then (x - y) % y else x := by have h := mod_def_aux x y; rwa [dif_eq_if] at h @[simp] lemma mod_zero (a : nat) : a % 0 = a := begin rw mod_def, have h : Β¬ (0 < 0 ∧ 0 ≀ a), simp [lt_irrefl], simp [if_neg, h] end lemma mod_eq_of_lt {a b : nat} (h : a < b) : a % b = a := begin rw mod_def, have h' : Β¬(0 < b ∧ b ≀ a), simp [not_le_of_gt h], simp [if_neg, h'] end @[simp] lemma zero_mod (b : nat) : 0 % b = 0 := begin rw mod_def, have h : Β¬(0 < b ∧ b ≀ 0), {intro hn, cases hn with l r, exact absurd (lt_of_lt_of_le l r) (lt_irrefl 0)}, simp [if_neg, h] end lemma mod_eq_sub_mod {a b : nat} (h : b ≀ a) : a % b = (a - b) % b := or.elim (eq_zero_or_pos b) (Ξ»b0, by rw [b0, nat.sub_zero]) (Ξ»hβ‚‚, by rw [mod_def, if_pos (and.intro hβ‚‚ h)]) lemma mod_lt (x : nat) {y : nat} (h : 0 < y) : x % y < y := begin induction x using nat.case_strong_induction_on with x ih, { rw zero_mod, assumption }, { by_cases h₁ : succ x < y, { rwa [mod_eq_of_lt h₁] }, { have h₁ : succ x % y = (succ x - y) % y := mod_eq_sub_mod (not_lt.1 h₁), have : succ x - y ≀ x := le_of_lt_succ (sub_lt (succ_pos x) h), have hβ‚‚ : (succ x - y) % y < y := ih _ this, rwa [← h₁] at hβ‚‚ } } end @[simp] theorem mod_self (n : nat) : n % n = 0 := by rw [mod_eq_sub_mod (le_refl _), nat.sub_self, zero_mod] @[simp] lemma mod_one (n : β„•) : n % 1 = 0 := have n % 1 < 1, from (mod_lt n) (succ_pos 0), eq_zero_of_le_zero (le_of_lt_succ this) lemma mod_two_eq_zero_or_one (n : β„•) : n % 2 = 0 ∨ n % 2 = 1 := match n % 2, @nat.mod_lt n 2 dec_trivial with | 0, _ := or.inl rfl | 1, _ := or.inr rfl | k+2, h := absurd h dec_trivial end /- div & mod -/ lemma div_def (x y : nat) : x / y = if 0 < y ∧ y ≀ x then (x - y) / y + 1 else 0 := by have h := div_def_aux x y; rwa dif_eq_if at h lemma mod_add_div (m k : β„•) : m % k + k * (m / k) = m := begin apply nat.strong_induction_on m, clear m, intros m IH, cases decidable.em (0 < k ∧ k ≀ m) with h h', -- 0 < k ∧ k ≀ m { have h' : m - k < m, { apply nat.sub_lt _ h.left, apply lt_of_lt_of_le h.left h.right }, rw [div_def, mod_def, if_pos h, if_pos h], simp [nat.left_distrib, IH _ h', nat.add_comm, nat.add_left_comm], rw [nat.add_comm, ← nat.add_sub_assoc h.right, nat.mul_one, nat.add_sub_cancel_left] }, -- Β¬ (0 < k ∧ k ≀ m) { rw [div_def, mod_def, if_neg h', if_neg h', nat.mul_zero, nat.add_zero] }, end /- div -/ @[simp] protected lemma div_one (n : β„•) : n / 1 = n := have n % 1 + 1 * (n / 1) = n, from mod_add_div _ _, by { rwa [mod_one, nat.zero_add, nat.one_mul] at this } @[simp] protected lemma div_zero (n : β„•) : n / 0 = 0 := begin rw [div_def], simp [lt_irrefl] end @[simp] protected lemma zero_div (b : β„•) : 0 / b = 0 := eq.trans (div_def 0 b) $ if_neg (and.rec not_le_of_gt) protected lemma div_le_of_le_mul {m n : β„•} : βˆ€ {k}, m ≀ k * n β†’ m / k ≀ n | 0 h := by simp [nat.div_zero]; apply zero_le | (succ k) h := suffices succ k * (m / succ k) ≀ succ k * n, from nat.le_of_mul_le_mul_left this (zero_lt_succ _), calc succ k * (m / succ k) ≀ m % succ k + succ k * (m / succ k) : le_add_left _ _ ... = m : by rw mod_add_div ... ≀ succ k * n : h protected lemma div_le_self : βˆ€ (m n : β„•), m / n ≀ m | m 0 := by simp [nat.div_zero]; apply zero_le | m (succ n) := have m ≀ succ n * m, from calc m = 1 * m : by rw nat.one_mul ... ≀ succ n * m : mul_le_mul_right _ (succ_le_succ (zero_le _)), nat.div_le_of_le_mul this lemma div_eq_sub_div {a b : nat} (h₁ : 0 < b) (hβ‚‚ : b ≀ a) : a / b = (a - b) / b + 1 := begin rw [div_def a, if_pos], split ; assumption end lemma div_eq_of_lt {a b : β„•} (hβ‚€ : a < b) : a / b = 0 := begin rw [div_def a, if_neg], intro h₁, apply not_le_of_gt hβ‚€ h₁.right end -- this is a Galois connection -- f x ≀ y ↔ x ≀ g y -- with -- f x = x * k -- g y = y / k theorem le_div_iff_mul_le (x y : β„•) {k : β„•} (Hk : 0 < k) : x ≀ y / k ↔ x * k ≀ y := begin -- Hk is needed because, despite div being made total, y / 0 := 0 -- x * 0 ≀ y ↔ x ≀ y / 0 -- ↔ 0 ≀ y ↔ x ≀ 0 -- ↔ true ↔ x = 0 -- ↔ x = 0 revert x, apply nat.strong_induction_on y _, clear y, intros y IH x, cases lt_or_le y k with h h, -- base case: y < k { rw [div_eq_of_lt h], cases x with x, { simp [nat.zero_mul, zero_le] }, { simp [succ_mul, not_succ_le_zero, nat.add_comm], apply lt_of_lt_of_le h, apply le_add_right } }, -- step: k ≀ y { rw [div_eq_sub_div Hk h], cases x with x, { simp [nat.zero_mul, zero_le] }, { have Hlt : y - k < y, { apply sub_lt_of_pos_le ; assumption }, rw [ ← add_one , nat.add_le_add_iff_le_right , IH (y - k) Hlt x , add_one , succ_mul, add_le_to_le_sub _ h ] } } end theorem div_lt_iff_lt_mul (x y : β„•) {k : β„•} (Hk : 0 < k) : x / k < y ↔ x < y * k := begin simp [← not_le], apply not_iff_not_of_iff, apply le_div_iff_mul_le _ _ Hk end def iterate {Ξ± : Sort u} (op : Ξ± β†’ Ξ±) : β„• β†’ Ξ± β†’ Ξ± | 0 a := a | (succ k) a := iterate k (op a) notation f`^[`n`]` := iterate f n /- successor and predecessor -/ theorem add_one_ne_zero (n : β„•) : n + 1 β‰  0 := succ_ne_zero _ theorem eq_zero_or_eq_succ_pred (n : β„•) : n = 0 ∨ n = succ (pred n) := by cases n; simp theorem exists_eq_succ_of_ne_zero {n : β„•} (H : n β‰  0) : βˆƒk : β„•, n = succ k := ⟨_, (eq_zero_or_eq_succ_pred _).resolve_left H⟩ def discriminate {B : Sort u} {n : β„•} (H1: n = 0 β†’ B) (H2 : βˆ€m, n = succ m β†’ B) : B := by induction h : n; [exact H1 h, exact H2 _ h] theorem one_succ_zero : 1 = succ 0 := rfl def two_step_induction {P : β„• β†’ Sort u} (H1 : P 0) (H2 : P 1) (H3 : βˆ€ (n : β„•) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : Ξ  (a : β„•), P a | 0 := H1 | 1 := H2 | (succ (succ n)) := H3 _ (two_step_induction _) (two_step_induction _) def sub_induction {P : β„• β†’ β„• β†’ Sort u} (H1 : βˆ€m, P 0 m) (H2 : βˆ€n, P (succ n) 0) (H3 : βˆ€n m, P n m β†’ P (succ n) (succ m)) : Ξ  (n m : β„•), P n m | 0 m := H1 _ | (succ n) 0 := H2 _ | (succ n) (succ m) := H3 _ _ (sub_induction n m) /- addition -/ theorem succ_add_eq_succ_add (n m : β„•) : succ n + m = n + succ m := by simp [succ_add, add_succ] -- theorem one_add (n : β„•) : 1 + n = succ n := by simp [add_comm] protected theorem add_right_comm : βˆ€ (n m k : β„•), n + m + k = n + k + m := right_comm nat.add nat.add_comm nat.add_assoc theorem eq_zero_of_add_eq_zero {n m : β„•} (H : n + m = 0) : n = 0 ∧ m = 0 := ⟨nat.eq_zero_of_add_eq_zero_right H, nat.eq_zero_of_add_eq_zero_left H⟩ theorem eq_zero_of_mul_eq_zero : βˆ€ {n m : β„•}, n * m = 0 β†’ n = 0 ∨ m = 0 | 0 m := Ξ» h, or.inl rfl | (succ n) m := begin rw succ_mul, intro h, exact or.inr (eq_zero_of_add_eq_zero_left h) end /- properties of inequality -/ theorem le_succ_of_pred_le {n m : β„•} : pred n ≀ m β†’ n ≀ succ m := nat.cases_on n less_than_or_equal.step (Ξ» a, succ_le_succ) theorem le_lt_antisymm {n m : β„•} (h₁ : n ≀ m) (hβ‚‚ : m < n) : false := nat.lt_irrefl n (nat.lt_of_le_of_lt h₁ hβ‚‚) theorem lt_le_antisymm {n m : β„•} (h₁ : n < m) (hβ‚‚ : m ≀ n) : false := le_lt_antisymm hβ‚‚ h₁ protected theorem lt_asymm {n m : β„•} (h₁ : n < m) : Β¬ m < n := le_lt_antisymm (nat.le_of_lt h₁) protected def lt_ge_by_cases {a b : β„•} {C : Sort u} (h₁ : a < b β†’ C) (hβ‚‚ : b ≀ a β†’ C) : C := decidable.by_cases h₁ (Ξ» h, hβ‚‚ (or.elim (nat.lt_or_ge a b) (Ξ» a, absurd a h) (Ξ» a, a))) protected def lt_by_cases {a b : β„•} {C : Sort u} (h₁ : a < b β†’ C) (hβ‚‚ : a = b β†’ C) (h₃ : b < a β†’ C) : C := nat.lt_ge_by_cases h₁ (Ξ» h₁, nat.lt_ge_by_cases h₃ (Ξ» h, hβ‚‚ (nat.le_antisymm h h₁))) protected theorem lt_trichotomy (a b : β„•) : a < b ∨ a = b ∨ b < a := nat.lt_by_cases (Ξ» h, or.inl h) (Ξ» h, or.inr (or.inl h)) (Ξ» h, or.inr (or.inr h)) protected theorem eq_or_lt_of_not_lt {a b : β„•} (hnlt : Β¬ a < b) : a = b ∨ b < a := (nat.lt_trichotomy a b).resolve_left hnlt theorem lt_succ_of_lt {a b : nat} (h : a < b) : a < succ b := le_succ_of_le h lemma one_pos : 0 < 1 := nat.zero_lt_one /- subtraction -/ protected theorem sub_le_sub_left {n m : β„•} (k) (h : n ≀ m) : k - m ≀ k - n := by induction h; [refl, exact le_trans (pred_le _) h_ih] theorem succ_sub_sub_succ (n m k : β„•) : succ n - m - succ k = n - m - k := by rw [nat.sub_sub, nat.sub_sub, add_succ, succ_sub_succ] protected theorem sub.right_comm (m n k : β„•) : m - n - k = m - k - n := by rw [nat.sub_sub, nat.sub_sub, nat.add_comm] theorem mul_pred_left : βˆ€ (n m : β„•), pred n * m = n * m - m | 0 m := by simp [nat.zero_sub, pred_zero, nat.zero_mul] | (succ n) m := by rw [pred_succ, succ_mul, nat.add_sub_cancel] theorem mul_pred_right (n m : β„•) : n * pred m = n * m - n := by rw [nat.mul_comm, mul_pred_left, nat.mul_comm] protected theorem mul_sub_right_distrib : βˆ€ (n m k : β„•), (n - m) * k = n * k - m * k | n 0 k := by simp [nat.sub_zero, nat.zero_mul] | n (succ m) k := by rw [nat.sub_succ, mul_pred_left, mul_sub_right_distrib, succ_mul, nat.sub_sub] protected theorem mul_sub_left_distrib (n m k : β„•) : n * (m - k) = n * m - n * k := by rw [nat.mul_comm, nat.mul_sub_right_distrib, nat.mul_comm m n, nat.mul_comm n k] protected theorem mul_self_sub_mul_self_eq (a b : nat) : a * a - b * b = (a + b) * (a - b) := by rw [nat.mul_sub_left_distrib, nat.right_distrib, nat.right_distrib, nat.mul_comm b a, nat.add_comm (a*a) (a*b), nat.add_sub_add_left] theorem succ_mul_succ_eq (a b : nat) : succ a * succ b = a*b + a + b + 1 := begin rw [← add_one, ← add_one], simp [nat.right_distrib, nat.left_distrib, nat.add_left_comm, nat.mul_one, nat.one_mul, nat.add_assoc], end theorem succ_sub {m n : β„•} (h : n ≀ m) : succ m - n = succ (m - n) := exists.elim (nat.le.dest h) (assume k, assume hk : n + k = m, by rw [← hk, nat.add_sub_cancel_left, ← add_succ, nat.add_sub_cancel_left]) protected theorem sub_pos_of_lt {m n : β„•} (h : m < n) : 0 < n - m := have 0 + m < n - m + m, begin rw [nat.zero_add, nat.sub_add_cancel (le_of_lt h)], exact h end, nat.lt_of_add_lt_add_right this protected theorem sub_sub_self {n m : β„•} (h : m ≀ n) : n - (n - m) = m := (nat.sub_eq_iff_eq_add (nat.sub_le _ _)).2 (eq.symm (add_sub_of_le h)) protected theorem sub_add_comm {n m k : β„•} (h : k ≀ n) : n + m - k = n - k + m := (nat.sub_eq_iff_eq_add (nat.le_trans h (nat.le_add_right _ _))).2 (by rwa [nat.add_right_comm, nat.sub_add_cancel]) theorem sub_one_sub_lt {n i} (h : i < n) : n - 1 - i < n := begin rw nat.sub_sub, apply nat.sub_lt, apply lt_of_lt_of_le (nat.zero_lt_succ _) h, rw nat.add_comm, apply nat.zero_lt_succ end theorem pred_inj : βˆ€ {a b : nat}, 0 < a β†’ 0 < b β†’ nat.pred a = nat.pred b β†’ a = b | (succ a) (succ b) ha hb h := have a = b, from h, by rw this | (succ a) 0 ha hb h := absurd hb (lt_irrefl _) | 0 (succ b) ha hb h := absurd ha (lt_irrefl _) | 0 0 ha hb h := rfl /- find -/ section find parameter {p : β„• β†’ Prop} private def lbp (m n : β„•) : Prop := m = n + 1 ∧ βˆ€ k ≀ n, Β¬p k parameters [decidable_pred p] (H : βˆƒn, p n) private def wf_lbp : well_founded lbp := ⟨let ⟨n, pn⟩ := H in suffices βˆ€m k, n ≀ k + m β†’ acc lbp k, from Ξ»a, this _ _ (nat.le_add_left _ _), Ξ»m, nat.rec_on m (Ξ»k kn, ⟨_, Ξ»y r, match y, r with ._, ⟨rfl, a⟩ := absurd pn (a _ kn) end⟩) (Ξ»m IH k kn, ⟨_, Ξ»y r, match y, r with ._, ⟨rfl, a⟩ := IH _ (by rw nat.add_right_comm; exact kn) end⟩)⟩ protected def find_x : {n // p n ∧ βˆ€m < n, Β¬p m} := @well_founded.fix _ (Ξ»k, (βˆ€n < k, Β¬p n) β†’ {n // p n ∧ βˆ€m < n, Β¬p m}) lbp wf_lbp (Ξ»m IH al, if pm : p m then ⟨m, pm, al⟩ else have βˆ€ n ≀ m, Β¬p n, from Ξ»n h, or.elim (decidable.lt_or_eq_of_le h) (al n) (Ξ»e, by rw e; exact pm), IH _ ⟨rfl, this⟩ (Ξ»n h, this n $ nat.le_of_succ_le_succ h)) 0 (Ξ»n h, absurd h (nat.not_lt_zero _)) /-- If `p` is a (decidable) predicate and `hp : βˆƒ (n : β„•), p n` is a proof that there exists some natural number satisfying `p`, then `nat.find hp` is the smallest natural number satisfying `p`. Note that `nat.find` is protected, meaning that you can't just write `find`, even if the `nat` namespace is open. The API for `nat.find` is: * `nat.find_spec` is the proof that `nat.find hp` satisfies `p`. * `nat.find_min` is the proof that if `m < nat.find hp` then `m` does not satisfy `p`. * `nat.find_min'` is the proof that if `m` does satisfy `p` then nat.find hp ≀ m`. -/ protected def find : β„• := nat.find_x.1 protected theorem find_spec : p nat.find := nat.find_x.2.left protected theorem find_min : βˆ€ {m : β„•}, m < nat.find β†’ Β¬p m := nat.find_x.2.right protected theorem find_min' {m : β„•} (h : p m) : nat.find ≀ m := le_of_not_lt (Ξ» l, find_min l h) end find /- mod -/ theorem mod_le (x y : β„•) : x % y ≀ x := or.elim (lt_or_le x y) (Ξ»xlty, by rw mod_eq_of_lt xlty; refl) (Ξ»ylex, or.elim (eq_zero_or_pos y) (Ξ»y0, by rw [y0, mod_zero]; refl) (Ξ»ypos, le_trans (le_of_lt (mod_lt _ ypos)) ylex)) @[simp] theorem add_mod_right (x z : β„•) : (x + z) % z = x % z := by rw [mod_eq_sub_mod (nat.le_add_left _ _), nat.add_sub_cancel] @[simp] theorem add_mod_left (x z : β„•) : (x + z) % x = z % x := by rw [nat.add_comm, add_mod_right] @[simp] theorem add_mul_mod_self_left (x y z : β„•) : (x + y * z) % y = x % y := by {induction z with z ih, rw [nat.mul_zero, nat.add_zero], rw [mul_succ, ← nat.add_assoc, add_mod_right, ih]} @[simp] theorem add_mul_mod_self_right (x y z : β„•) : (x + y * z) % z = x % z := by rw [nat.mul_comm, add_mul_mod_self_left] @[simp] theorem mul_mod_right (m n : β„•) : (m * n) % m = 0 := by rw [← nat.zero_add (m*n), add_mul_mod_self_left, zero_mod] @[simp] theorem mul_mod_left (m n : β„•) : (m * n) % n = 0 := by rw [nat.mul_comm, mul_mod_right] theorem mul_mod_mul_left (z x y : β„•) : (z * x) % (z * y) = z * (x % y) := if y0 : y = 0 then by rw [y0, nat.mul_zero, mod_zero, mod_zero] else if z0 : z = 0 then by rw [z0, nat.zero_mul, nat.zero_mul, nat.zero_mul, mod_zero] else x.strong_induction_on $ Ξ»n IH, have y0 : y > 0, from nat.pos_of_ne_zero y0, have z0 : z > 0, from nat.pos_of_ne_zero z0, or.elim (le_or_lt y n) (Ξ»yn, by rw [ mod_eq_sub_mod yn, mod_eq_sub_mod (mul_le_mul_left z yn), ← nat.mul_sub_left_distrib]; exact IH _ (sub_lt (lt_of_lt_of_le y0 yn) y0)) (Ξ»yn, by rw [mod_eq_of_lt yn, mod_eq_of_lt (nat.mul_lt_mul_of_pos_left yn z0)]) theorem mul_mod_mul_right (z x y : β„•) : (x * z) % (y * z) = (x % y) * z := by rw [nat.mul_comm x z, nat.mul_comm y z, nat.mul_comm (x % y) z]; apply mul_mod_mul_left theorem cond_to_bool_mod_two (x : β„•) [d : decidable (x % 2 = 1)] : cond (@to_bool (x % 2 = 1) d) 1 0 = x % 2 := begin by_cases h : x % 2 = 1, { simp! [*] }, { cases mod_two_eq_zero_or_one x; simp! [*, nat.zero_ne_one] } end theorem sub_mul_mod (x k n : β„•) (h₁ : n*k ≀ x) : (x - n*k) % n = x % n := begin induction k with k, { rw [nat.mul_zero, nat.sub_zero] }, { have hβ‚‚ : n * k ≀ x, { rw [mul_succ] at h₁, apply nat.le_trans _ h₁, apply le_add_right _ n }, have hβ‚„ : x - n * k β‰₯ n, { apply @nat.le_of_add_le_add_right (n*k), rw [nat.sub_add_cancel hβ‚‚], simp [mul_succ, nat.add_comm] at h₁, simp [h₁] }, rw [mul_succ, ← nat.sub_sub, ← mod_eq_sub_mod hβ‚„, k_ih hβ‚‚] } end /- div -/ theorem sub_mul_div (x n p : β„•) (h₁ : n*p ≀ x) : (x - n*p) / n = x / n - p := begin cases eq_zero_or_pos n with hβ‚€ hβ‚€, { rw [hβ‚€, nat.div_zero, nat.div_zero, nat.zero_sub] }, { induction p with p, { rw [nat.mul_zero, nat.sub_zero, nat.sub_zero] }, { have hβ‚‚ : n*p ≀ x, { transitivity, { apply nat.mul_le_mul_left, apply le_succ }, { apply h₁ } }, have h₃ : x - n * p β‰₯ n, { apply nat.le_of_add_le_add_right, rw [nat.sub_add_cancel hβ‚‚, nat.add_comm], rw [mul_succ] at h₁, apply h₁ }, rw [sub_succ, ← p_ih hβ‚‚], rw [@div_eq_sub_div (x - n*p) _ hβ‚€ h₃], simp [add_one, pred_succ, mul_succ, nat.sub_sub] } } end theorem div_mul_le_self : βˆ€ (m n : β„•), m / n * n ≀ m | m 0 := by simp; apply zero_le | m (succ n) := (le_div_iff_mul_le _ _ (nat.succ_pos _)).1 (le_refl _) @[simp] theorem add_div_right (x : β„•) {z : β„•} (H : 0 < z) : (x + z) / z = succ (x / z) := by rw [div_eq_sub_div H (nat.le_add_left _ _), nat.add_sub_cancel] @[simp] theorem add_div_left (x : β„•) {z : β„•} (H : 0 < z) : (z + x) / z = succ (x / z) := by rw [nat.add_comm, add_div_right x H] @[simp] theorem mul_div_right (n : β„•) {m : β„•} (H : 0 < m) : m * n / m = n := by {induction n; simp [*, mul_succ, nat.mul_zero] } @[simp] theorem mul_div_left (m : β„•) {n : β„•} (H : 0 < n) : m * n / n = m := by rw [nat.mul_comm, mul_div_right _ H] protected theorem div_self {n : β„•} (H : 0 < n) : n / n = 1 := let t := add_div_right 0 H in by rwa [nat.zero_add, nat.zero_div] at t theorem add_mul_div_left (x z : β„•) {y : β„•} (H : 0 < y) : (x + y * z) / y = x / y + z := begin induction z with z ih, { rw [nat.mul_zero, nat.add_zero, nat.add_zero] }, { rw [mul_succ, ← nat.add_assoc, add_div_right _ H, ih] } end theorem add_mul_div_right (x y : β„•) {z : β„•} (H : 0 < z) : (x + y * z) / z = x / z + y := by rw [nat.mul_comm, add_mul_div_left _ _ H] protected theorem mul_div_cancel (m : β„•) {n : β„•} (H : 0 < n) : m * n / n = m := let t := add_mul_div_right 0 m H in by rwa [nat.zero_add, nat.zero_div, nat.zero_add] at t protected theorem mul_div_cancel_left (m : β„•) {n : β„•} (H : 0 < n) : n * m / n = m := by rw [nat.mul_comm, nat.mul_div_cancel _ H] protected theorem div_eq_of_eq_mul_left {m n k : β„•} (H1 : 0 < n) (H2 : m = k * n) : m / n = k := by rw [H2, nat.mul_div_cancel _ H1] protected theorem div_eq_of_eq_mul_right {m n k : β„•} (H1 : 0 < n) (H2 : m = n * k) : m / n = k := by rw [H2, nat.mul_div_cancel_left _ H1] protected theorem div_eq_of_lt_le {m n k : β„•} (lo : k * n ≀ m) (hi : m < succ k * n) : m / n = k := have npos : 0 < n, from (eq_zero_or_pos _).resolve_left $ Ξ» hn, by rw [hn, nat.mul_zero] at hi lo; exact absurd lo (not_le_of_gt hi), le_antisymm (le_of_lt_succ ((nat.div_lt_iff_lt_mul _ _ npos).2 hi)) ((nat.le_div_iff_mul_le _ _ npos).2 lo) theorem mul_sub_div (x n p : β„•) (h₁ : x < n*p) : (n * p - succ x) / n = p - succ (x / n) := begin have npos : 0 < n := (eq_zero_or_pos _).resolve_left (Ξ» n0, by rw [n0, nat.zero_mul] at h₁; exact not_lt_zero _ h₁), apply nat.div_eq_of_lt_le, { rw [nat.mul_sub_right_distrib, nat.mul_comm], apply nat.sub_le_sub_left, exact (div_lt_iff_lt_mul _ _ npos).1 (lt_succ_self _) }, { change succ (pred (n * p - x)) ≀ (succ (pred (p - x / n))) * n, rw [succ_pred_eq_of_pos (nat.sub_pos_of_lt h₁), succ_pred_eq_of_pos (nat.sub_pos_of_lt _)], { rw [nat.mul_sub_right_distrib, nat.mul_comm], apply nat.sub_le_sub_left, apply div_mul_le_self }, { apply (div_lt_iff_lt_mul _ _ npos).2, rwa nat.mul_comm } } end protected lemma mul_pos {a b : β„•} (ha : 0 < a) (hb : 0 < b) : 0 < a * b := have h : 0 * b < a * b, from nat.mul_lt_mul_of_pos_right ha hb, by rwa nat.zero_mul at h protected theorem div_div_eq_div_mul (m n k : β„•) : m / n / k = m / (n * k) := begin cases eq_zero_or_pos k with k0 kpos, {rw [k0, nat.mul_zero, nat.div_zero, nat.div_zero]}, cases eq_zero_or_pos n with n0 npos, {rw [n0, nat.zero_mul, nat.div_zero, nat.zero_div]}, apply le_antisymm, { apply (le_div_iff_mul_le _ _ (nat.mul_pos npos kpos)).2, rw [nat.mul_comm n k, ← nat.mul_assoc], apply (le_div_iff_mul_le _ _ npos).1, apply (le_div_iff_mul_le _ _ kpos).1, refl }, { apply (le_div_iff_mul_le _ _ kpos).2, apply (le_div_iff_mul_le _ _ npos).2, rw [nat.mul_assoc, nat.mul_comm n k], apply (le_div_iff_mul_le _ _ (nat.mul_pos kpos npos)).1, refl } end protected theorem mul_div_mul {m : β„•} (n k : β„•) (H : 0 < m) : m * n / (m * k) = n / k := by rw [← nat.div_div_eq_div_mul, nat.mul_div_cancel_left _ H] /- dvd -/ protected theorem dvd_mul_right (a b : β„•) : a ∣ a * b := ⟨b, rfl⟩ protected theorem dvd_trans {a b c : β„•} (h₁ : a ∣ b) (hβ‚‚ : b ∣ c) : a ∣ c := match h₁, hβ‚‚ with | ⟨d, (h₃ : b = a * d)⟩, ⟨e, (hβ‚„ : c = b * e)⟩ := ⟨d * e, show c = a * (d * e), by simp [h₃, hβ‚„, nat.mul_assoc]⟩ end protected theorem eq_zero_of_zero_dvd {a : β„•} (h : 0 ∣ a) : a = 0 := exists.elim h (assume c, assume H' : a = 0 * c, eq.trans H' (nat.zero_mul c)) protected theorem dvd_add {a b c : β„•} (h₁ : a ∣ b) (hβ‚‚ : a ∣ c) : a ∣ b + c := exists.elim h₁ (Ξ» d hd, exists.elim hβ‚‚ (Ξ» e he, ⟨d + e, by simp [nat.left_distrib, hd, he]⟩)) protected theorem dvd_add_iff_right {k m n : β„•} (h : k ∣ m) : k ∣ n ↔ k ∣ m + n := ⟨nat.dvd_add h, exists.elim h $ Ξ»d hd, match m, hd with | ._, rfl := Ξ»hβ‚‚, exists.elim hβ‚‚ $ Ξ»e he, ⟨e - d, by rw [nat.mul_sub_left_distrib, ← he, nat.add_sub_cancel_left]⟩ end⟩ protected theorem dvd_add_iff_left {k m n : β„•} (h : k ∣ n) : k ∣ m ↔ k ∣ m + n := by rw nat.add_comm; exact nat.dvd_add_iff_right h theorem dvd_sub {k m n : β„•} (H : n ≀ m) (h₁ : k ∣ m) (hβ‚‚ : k ∣ n) : k ∣ m - n := (nat.dvd_add_iff_left hβ‚‚).2 $ by rw nat.sub_add_cancel H; exact h₁ theorem dvd_mod_iff {k m n : β„•} (h : k ∣ n) : k ∣ m % n ↔ k ∣ m := let t := @nat.dvd_add_iff_left _ (m % n) _ (nat.dvd_trans h (nat.dvd_mul_right n (m / n))) in by rwa mod_add_div at t theorem le_of_dvd {m n : β„•} (h : 0 < n) : m ∣ n β†’ m ≀ n := λ⟨k, e⟩, by { revert h, rw e, refine k.cases_on _ _, exact Ξ»hn, absurd hn (lt_irrefl _), exact Ξ»k _, let t := mul_le_mul_left m (succ_pos k) in by rwa nat.mul_one at t } theorem dvd_antisymm : Ξ  {m n : β„•}, m ∣ n β†’ n ∣ m β†’ m = n | m 0 h₁ hβ‚‚ := nat.eq_zero_of_zero_dvd hβ‚‚ | 0 n h₁ hβ‚‚ := (nat.eq_zero_of_zero_dvd h₁).symm | (succ m) (succ n) h₁ hβ‚‚ := le_antisymm (le_of_dvd (succ_pos _) h₁) (le_of_dvd (succ_pos _) hβ‚‚) theorem pos_of_dvd_of_pos {m n : β„•} (H1 : m ∣ n) (H2 : 0 < n) : 0 < m := nat.pos_of_ne_zero $ Ξ»m0, by rw m0 at H1; rw nat.eq_zero_of_zero_dvd H1 at H2; exact lt_irrefl _ H2 theorem eq_one_of_dvd_one {n : β„•} (H : n ∣ 1) : n = 1 := le_antisymm (le_of_dvd dec_trivial H) (pos_of_dvd_of_pos H dec_trivial) theorem dvd_of_mod_eq_zero {m n : β„•} (H : n % m = 0) : m ∣ n := ⟨n / m, by { have t := (mod_add_div n m).symm, rwa [H, nat.zero_add] at t }⟩ theorem mod_eq_zero_of_dvd {m n : β„•} (H : m ∣ n) : n % m = 0 := exists.elim H (Ξ» z H1, by rw [H1, mul_mod_right]) theorem dvd_iff_mod_eq_zero (m n : β„•) : m ∣ n ↔ n % m = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ instance decidable_dvd : @decidable_rel β„• (∣) := Ξ»m n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem mul_div_cancel' {m n : β„•} (H : n ∣ m) : n * (m / n) = m := let t := mod_add_div m n in by rwa [mod_eq_zero_of_dvd H, nat.zero_add] at t protected theorem div_mul_cancel {m n : β„•} (H : n ∣ m) : m / n * n = m := by rw [nat.mul_comm, nat.mul_div_cancel' H] protected theorem mul_div_assoc (m : β„•) {n k : β„•} (H : k ∣ n) : m * n / k = m * (n / k) := or.elim (eq_zero_or_pos k) (Ξ»h, by rw [h, nat.div_zero, nat.div_zero, nat.mul_zero]) (Ξ»h, have m * n / k = m * (n / k * k) / k, by rw nat.div_mul_cancel H, by rw[this, ← nat.mul_assoc, nat.mul_div_cancel _ h]) theorem dvd_of_mul_dvd_mul_left {m n k : β„•} (kpos : 0 < k) (H : k * m ∣ k * n) : m ∣ n := exists.elim H (Ξ»l H1, by rw nat.mul_assoc at H1; exact ⟨_, eq_of_mul_eq_mul_left kpos H1⟩) theorem dvd_of_mul_dvd_mul_right {m n k : β„•} (kpos : 0 < k) (H : m * k ∣ n * k) : m ∣ n := by rw [nat.mul_comm m k, nat.mul_comm n k] at H; exact dvd_of_mul_dvd_mul_left kpos H /- --- -/ protected lemma mul_le_mul_of_nonneg_left {a b c : β„•} (h₁ : a ≀ b) : c * a ≀ c * b := begin by_cases hba : b ≀ a, { simp [le_antisymm hba h₁] }, by_cases hc0 : c ≀ 0, { simp [le_antisymm hc0 (zero_le c), nat.zero_mul] }, exact (le_not_le_of_lt (nat.mul_lt_mul_of_pos_left (lt_of_le_not_le h₁ hba) (lt_of_le_not_le (zero_le c) hc0))).left, end protected lemma mul_le_mul_of_nonneg_right {a b c : β„•} (h₁ : a ≀ b) : a * c ≀ b * c := begin by_cases hba : b ≀ a, { simp [le_antisymm hba h₁] }, by_cases hc0 : c ≀ 0, { simp [le_antisymm hc0 (zero_le c), nat.mul_zero] }, exact (le_not_le_of_lt (nat.mul_lt_mul_of_pos_right (lt_of_le_not_le h₁ hba) (lt_of_le_not_le (zero_le c) hc0))).left, end protected lemma mul_lt_mul {a b c d : β„•} (hac : a < c) (hbd : b ≀ d) (pos_b : 0 < b) : a * b < c * d := calc a * b < c * b : nat.mul_lt_mul_of_pos_right hac pos_b ... ≀ c * d : nat.mul_le_mul_of_nonneg_left hbd protected lemma mul_lt_mul' {a b c d : β„•} (h1 : a ≀ c) (h2 : b < d) (h3 : 0 < c) : a * b < c * d := calc a * b ≀ c * b : nat.mul_le_mul_of_nonneg_right h1 ... < c * d : nat.mul_lt_mul_of_pos_left h2 h3 -- TODO: there are four variations, depending on which variables we assume to be nonneg protected lemma mul_le_mul {a b c d : β„•} (hac : a ≀ c) (hbd : b ≀ d) : a * b ≀ c * d := calc a * b ≀ c * b : nat.mul_le_mul_of_nonneg_right hac ... ≀ c * d : nat.mul_le_mul_of_nonneg_left hbd lemma div_lt_self {n m : nat} : 0 < n β†’ 1 < m β†’ n / m < n := begin intros h₁ hβ‚‚, have m_pos : 0 < m, { apply lt_trans _ hβ‚‚, comp_val }, suffices : 1 * n < m * n, { rw [nat.one_mul, nat.mul_comm] at this, exact iff.mpr (nat.div_lt_iff_lt_mul n n m_pos) this }, exact nat.mul_lt_mul hβ‚‚ (le_refl _) h₁ end end nat
e94fdee4e06c61d43a55eb7e7278748d51694afc
4fa118f6209450d4e8d058790e2967337811b2b5
/src/Huber_ring/basic.lean
85c85cca16e483c918a3d6f75adc4b5c3a1c91f7
[ "Apache-2.0" ]
permissive
leanprover-community/lean-perfectoid-spaces
16ab697a220ed3669bf76311daa8c466382207f7
95a6520ce578b30a80b4c36e36ab2d559a842690
refs/heads/master
1,639,557,829,139
1,638,797,866,000
1,638,797,866,000
135,769,296
96
10
Apache-2.0
1,638,797,866,000
1,527,892,754,000
Lean
UTF-8
Lean
false
false
4,256
lean
import topology.algebra.ring import ring_theory.algebra_operations import group_theory.subgroup import power_bounded import for_mathlib.submodule import for_mathlib.nonarchimedean.adic_topology import for_mathlib.open_embeddings /-! # Huber rings Huber rings (called β€œf-adic rings” by Huber and [Wedhorn], but Scholze renamed them) play a crucial role in the theory of adic spaces, as one of the main ingredients in the definition of so-called β€œHuber pairs”. They are topological rings that satisfy a certain topological finiteness condition. ## Implementation details In the following definition we record the ideal J as data, whereas usually one takes its existence as a property. For practical purposes it is however easier to package it as data. In the definition of Huber ring (below it), we return to the existence statement so that `Huber_ring` is a property of a topological commutative ring not involving any data. -/ local attribute [instance, priority 0] classical.prop_decidable universes u v section open set topological_space /-- A β€œring of definition” of a topological ring A is an open subring Aβ‚€ that has a finitely generated ideal J such that the topology on Aβ‚€ is J-adic. See [Wedhorn, Def 6.1] -/ structure Huber_ring.ring_of_definition (Aβ‚€ : Type*) (A : Type*) [comm_ring Aβ‚€] [topological_space Aβ‚€] [topological_ring Aβ‚€] [comm_ring A] [topological_space A] [topological_ring A] extends algebra Aβ‚€ A := (emb : open_embedding to_fun) (J : ideal Aβ‚€) (fin : J.fg) (top : is_ideal_adic J) /-- A Huber ring is a topological ring A that contains an open subring Aβ‚€ such that the subspace topology on Aβ‚€ is I-adic, where I is a finitely generated ideal of Aβ‚€. The pair (Aβ‚€, I) is called a pair of definition (pod) and is not part of the data. (The name β€œHuber ring” was introduced by Scholze. Before that, they were called f-adic rings.) See [Wedhorn, Def 6.1] -/ class Huber_ring (A : Type u) extends comm_ring A, topological_space A, topological_ring A := (pod : βˆƒ (Aβ‚€ : Type u) [comm_ring Aβ‚€] [topological_space Aβ‚€] [topological_ring Aβ‚€], by exactI nonempty (Huber_ring.ring_of_definition Aβ‚€ A)) end namespace Huber_ring open topological_add_group variables {A : Type u} [Huber_ring A] /-- A Huber ring is nonarchimedean. -/ protected lemma nonarchimedean : nonarchimedean A := begin rcases Huber_ring.pod A with ⟨Aβ‚€, H₁, Hβ‚‚, H₃, Hβ‚„, emb, J, Hfin, Htop⟩, resetI, apply nonarchimedean_of_nonarchimedean_open_embedding (algebra_map A) emb, exact Htop.nonarchimedean end /-- The subset of power bounded elements of a Huber ring is a subring.-/ instance power_bounded_subring.is_subring : is_subring (power_bounded_subring A) := power_bounded_subring.is_subring Huber_ring.nonarchimedean /-- For every neighbourhood U of 0 ∈ A, there exists a pair of definition (Aβ‚€, J) such that J βŠ† U. -/ lemma exists_pod_subset (U : set A) (hU : U ∈ nhds (0:A)) : βˆƒ (Aβ‚€ : Type u) [comm_ring Aβ‚€] [topological_space Aβ‚€], by exactI βˆƒ [topological_ring Aβ‚€], by exactI βˆƒ (rod : ring_of_definition Aβ‚€ A), by letI := ring_of_definition.to_algebra rod; exact (algebra_map A : Aβ‚€ β†’ A) '' (rod.J) βŠ† U := begin -- We start by unpacking the fact that A is a Huber ring. unfreezeI, rcases β€ΉHuber_ring Aβ€Ί with ⟨_, _, _, ⟨Aβ‚€, _, _, _, ⟨⟨alg, emb, J, fin, top⟩⟩⟩⟩, resetI, rw is_ideal_adic_iff at top, cases top with H₁ Hβ‚‚, -- There exists an n such that J^n βŠ† U. Choose such an n. cases Hβ‚‚ (algebra_map A ⁻¹' U) _ with n hn, -- Now it is time to pack everything up again. refine ⟨Aβ‚€, β€Ή_β€Ί, β€Ή_β€Ί, β€Ή_β€Ί, ⟨⟨alg, emb, _, _, _⟩, _⟩⟩, { -- We have to use the ideal J^(n+1), because Aβ‚€ is not J^0-adic. exact J^(n+1) }, { exact submodule.fg_pow J fin _, }, { apply is_ideal_adic_pow top, apply nat.succ_pos }, { show algebra_map A '' ↑(J ^ (n + 1)) βŠ† U, rw set.image_subset_iff, exact set.subset.trans (ideal.pow_le_pow $ nat.le_succ n) hn }, { apply emb.continuous.tendsto, convert hU, haveI : is_ring_hom (algebra.to_fun A : Aβ‚€ β†’ A) := algebra.is_ring_hom, exact is_ring_hom.map_zero _ } end end Huber_ring
0aeef94ba1d65e97f98ec4e25e5ee89594fceab0
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/analysis/fourier.lean
d893585bf1908f0660dc04293d5d907a02373f04
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,216
lean
/- Copyright (c) 2021 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import measure_theory.function.continuous_map_dense import measure_theory.function.l2_space import measure_theory.measure.haar import analysis.complex.circle import topology.metric_space.emetric_paracompact import topology.continuous_function.stone_weierstrass /-! # Fourier analysis on the circle This file contains basic technical results for a development of Fourier series. ## Main definitions * `haar_circle`, Haar measure on the circle, normalized to have total measure `1` * instances `measure_space`, `probability_measure` for the circle with respect to this measure * for `n : β„€`, `fourier n` is the monomial `Ξ» z, z ^ n`, bundled as a continuous map from `circle` to `β„‚` * for `n : β„€` and `p : ℝβ‰₯0∞`, `fourier_Lp p n` is an abbreviation for the monomial `fourier n` considered as an element of the Lα΅–-space `Lp β„‚ p haar_circle`, via the embedding `continuous_map.to_Lp` ## Main statements The theorem `span_fourier_closure_eq_top` states that the span of the monomials `fourier n` is dense in `C(circle, β„‚)`, i.e. that its `submodule.topological_closure` is `⊀`. This follows from the Stone-Weierstrass theorem after checking that it is a subalgebra, closed under conjugation, and separates points. The theorem `span_fourier_Lp_closure_eq_top` states that for `1 ≀ p < ∞` the span of the monomials `fourier_Lp` is dense in `Lp β„‚ p haar_circle`, i.e. that its `submodule.topological_closure` is `⊀`. This follows from the previous theorem using general theory on approximation of Lα΅– functions by continuous functions. The theorem `orthonormal_fourier` states that the monomials `fourier_Lp 2 n` form an orthonormal set (in the LΒ² space of the circle). By definition, a Hilbert basis for an inner product space is an orthonormal set whose span is dense. Thus, the last two results together establish that the functions `fourier_Lp 2 n` form a Hilbert basis for LΒ². ## TODO Once mathlib has general theory showing that a Hilbert basis of an inner product space induces a unitary equivalence with LΒ², the results in this file will give Fourier series applications such as Parseval's formula. -/ noncomputable theory open_locale ennreal open topological_space continuous_map measure_theory measure_theory.measure algebra submodule set local attribute [instance] fact_one_le_two_ennreal /-! ### Choice of measure on the circle -/ section haar_circle /-! We make the circle into a measure space, using the Haar measure normalized to have total measure 1. -/ instance : measurable_space circle := borel circle instance : borel_space circle := ⟨rfl⟩ /-- Haar measure on the circle, normalized to have total measure 1. -/ def haar_circle : measure circle := haar_measure positive_compacts_univ instance : probability_measure haar_circle := ⟨haar_measure_self⟩ instance : measure_space circle := { volume := haar_circle, .. circle.measurable_space } end haar_circle /-! ### Monomials on the circle -/ section fourier /-- The family of monomials `Ξ» z, z ^ n`, parametrized by `n : β„€` and considered as bundled continuous maps from `circle` to `β„‚`. -/ @[simps] def fourier (n : β„€) : C(circle, β„‚) := { to_fun := Ξ» z, z ^ n, continuous_to_fun := continuous_subtype_coe.fpow n $ Ξ» z, or.inl (nonzero_of_mem_circle z) } @[simp] lemma fourier_zero {z : circle} : fourier 0 z = 1 := rfl @[simp] lemma fourier_neg {n : β„€} {z : circle} : fourier (-n) z = complex.conj (fourier n z) := by simp [← coe_inv_circle_eq_conj z] @[simp] lemma fourier_add {m n : β„€} {z : circle} : fourier (m + n) z = (fourier m z) * (fourier n z) := by simp [fpow_add (nonzero_of_mem_circle z)] /-- The subalgebra of `C(circle, β„‚)` generated by `z ^ n` for `n ∈ β„€`; equivalently, polynomials in `z` and `conj z`. -/ def fourier_subalgebra : subalgebra β„‚ C(circle, β„‚) := algebra.adjoin β„‚ (range fourier) /-- The subalgebra of `C(circle, β„‚)` generated by `z ^ n` for `n ∈ β„€` is in fact the linear span of these functions. -/ lemma fourier_subalgebra_coe : fourier_subalgebra.to_submodule = span β„‚ (range fourier) := begin apply adjoin_eq_span_of_subset, refine subset.trans _ submodule.subset_span, intros x hx, apply submonoid.closure_induction hx (Ξ» _, id) ⟨0, rfl⟩, rintros _ _ ⟨m, rfl⟩ ⟨n, rfl⟩, refine ⟨m + n, _⟩, ext1 z, exact fourier_add, end /-- The subalgebra of `C(circle, β„‚)` generated by `z ^ n` for `n ∈ β„€` separates points. -/ lemma fourier_subalgebra_separates_points : fourier_subalgebra.separates_points := begin intros x y hxy, refine ⟨_, ⟨fourier 1, _, rfl⟩, _⟩, { exact subset_adjoin ⟨1, rfl⟩ }, { simp [hxy] } end /-- The subalgebra of `C(circle, β„‚)` generated by `z ^ n` for `n ∈ β„€` is invariant under complex conjugation. -/ lemma fourier_subalgebra_conj_invariant : conj_invariant_subalgebra (fourier_subalgebra.restrict_scalars ℝ) := begin rintros _ ⟨f, hf, rfl⟩, change _ ∈ fourier_subalgebra, change _ ∈ fourier_subalgebra at hf, apply adjoin_induction hf, { rintros _ ⟨n, rfl⟩, suffices : fourier (-n) ∈ fourier_subalgebra, { convert this, ext1, simp }, exact subset_adjoin ⟨-n, rfl⟩ }, { intros c, exact fourier_subalgebra.algebra_map_mem (complex.conj c) }, { intros f g hf hg, convert fourier_subalgebra.add_mem hf hg, exact alg_hom.map_add _ f g, }, { intros f g hf hg, convert fourier_subalgebra.mul_mem hf hg, exact alg_hom.map_mul _ f g, } end /-- The subalgebra of `C(circle, β„‚)` generated by `z ^ n` for `n ∈ β„€` is dense. -/ lemma fourier_subalgebra_closure_eq_top : fourier_subalgebra.topological_closure = ⊀ := continuous_map.subalgebra_complex_topological_closure_eq_top_of_separates_points fourier_subalgebra fourier_subalgebra_separates_points fourier_subalgebra_conj_invariant /-- The linear span of the monomials `z ^ n` is dense in `C(circle, β„‚)`. -/ lemma span_fourier_closure_eq_top : (span β„‚ (range fourier)).topological_closure = ⊀ := begin rw ← fourier_subalgebra_coe, exact congr_arg subalgebra.to_submodule fourier_subalgebra_closure_eq_top, end /-- The family of monomials `Ξ» z, z ^ n`, parametrized by `n : β„€` and considered as elements of the `Lp` space of functions on `circle` taking values in `β„‚`. -/ abbreviation fourier_Lp (p : ℝβ‰₯0∞) [fact (1 ≀ p)] (n : β„€) : Lp β„‚ p haar_circle := to_Lp p haar_circle β„‚ (fourier n) /-- For each `1 ≀ p < ∞`, the linear span of the monomials `z ^ n` is dense in `Lp β„‚ p haar_circle`. -/ lemma span_fourier_Lp_closure_eq_top {p : ℝβ‰₯0∞} [fact (1 ≀ p)] (hp : p β‰  ∞) : (span β„‚ (range (fourier_Lp p))).topological_closure = ⊀ := begin convert (continuous_map.to_Lp_dense_range β„‚ hp haar_circle β„‚).topological_closure_map_submodule span_fourier_closure_eq_top, rw [map_span, range_comp], simp end /-- For `n β‰  0`, a rotation by `n⁻¹ * real.pi` negates the monomial `z ^ n`. -/ lemma fourier_add_half_inv_index {n : β„€} (hn : n β‰  0) (z : circle) : fourier n ((exp_map_circle (n⁻¹ * real.pi) * z)) = - fourier n z := begin have : ↑n * ((↑n)⁻¹ * ↑real.pi * complex.I) = ↑real.pi * complex.I, { have : (n:β„‚) β‰  0 := by exact_mod_cast hn, field_simp, ring }, simp [mul_fpow, ← complex.exp_int_mul, complex.exp_pi_mul_I, this] end /-- The monomials `z ^ n` are an orthonormal set with respect to Haar measure on the circle. -/ lemma orthonormal_fourier : orthonormal β„‚ (fourier_Lp 2) := begin rw orthonormal_iff_ite, intros i j, rw continuous_map.inner_to_Lp haar_circle (fourier i) (fourier j), split_ifs, { simp [h, probability_measure.measure_univ, ← fourier_neg, ← fourier_add, -fourier_to_fun] }, simp only [← fourier_add, ← fourier_neg, is_R_or_C.conj_to_complex], have hij : -i + j β‰  0, { rw add_comm, exact sub_ne_zero.mpr (ne.symm h) }, exact integral_zero_of_mul_left_eq_neg (is_mul_left_invariant_haar_measure _) (fourier_add_half_inv_index hij) end end fourier
312b2f12056fb5f42fb807604e177a3ae0de4872
33340b3a23ca62ef3c8a7f6a2d4e14c07c6d3354
/lia/atom.lean
92515de9e3da71ab3686c6b4063b76274c6fe8f6
[]
no_license
lclem/cooper
79554e72ced343c64fed24b2d892d24bf9447dfe
812afc6b158821f2e7dac9c91d3b6123c7a19faf
refs/heads/master
1,607,554,257,488
1,578,694,133,000
1,578,694,133,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,358
lean
import ..num ..dot_prod open list znum inductive atom : Type | le : znum β†’ list znum β†’ atom | dvd : znum β†’ znum β†’ list znum β†’ atom | ndvd : znum β†’ znum β†’ list znum β†’ atom open atom -- | (i ≀' ks) := sorry -- | (d ∣' i ks) := sorry -- | (d Β¬| i ks) := sorry notation c `≀'` ks := atom.le c ks notation d `|'` c := atom.dvd d c notation d `Β¬|'` c := atom.ndvd d c meta def coeffs_to_format : nat β†’ list znum β†’ format | n [] := "0" | n [k] := to_fmt k ++ "x" ++ to_fmt n | n (k1::k2::ks) := to_fmt k1 ++ "x" ++ to_fmt n ++ " + " ++ coeffs_to_format (n+1) (k2::ks) meta def atom.to_format : atom β†’ format | (atom.le i ks) := to_fmt i ++ " ≀ " ++ coeffs_to_format 0 ks | (atom.dvd d i ks) := to_fmt d ++ " | " ++ to_fmt i ++ " + " ++ coeffs_to_format 0 ks | (atom.ndvd d i ks) := "Β¬(" ++ atom.to_format (atom.dvd d i ks) ++ ")" meta instance atom.has_to_format : has_to_format atom := ⟨atom.to_format⟩ meta instance : has_to_tactic_format atom := has_to_format_to_has_to_tactic_format _ def atom.eval : list znum β†’ atom β†’ Prop | xs (le i ks) := i ≀ dot_prod ks xs | xs (dvd d i ks) := d ∣ (i + dot_prod ks xs) | xs (ndvd d i ks) := Β¬ (d ∣ i + dot_prod ks xs) instance atom.dec_eval {zs : list znum} {a : atom} : decidable (a.eval zs) := begin cases a; {simp [atom.eval], apply_instance} end open znum
e0917f9d0d5e13ab1cc6f21e78dd791f42f4656c
495c02489c2d6a1db94dfdba71dd800d3cc67df2
/group_theory/action.lean
8de9794e564dc4bc6c764bdbef451c4f43647ba5
[ "Apache-2.0" ]
permissive
leodemoura/leanproved
e0fcbe4f4d72bf0dad9a962ed111b5975cf90712
de56e0af159dd0c0421733289c76aa79c78a0191
refs/heads/master
1,606,822,676,898
1,435,711,541,000
1,435,711,541,000
36,675,856
0
0
null
1,433,178,724,000
1,433,178,724,000
null
UTF-8
Lean
false
false
14,130
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ import algebra.group data .hom .perm .finsubg namespace group open finset algebra function local attribute perm.f [coercion] section def variables {G S : Type} [ambientG : group G] [finS : fintype S] [deceqS : decidable_eq S] include ambientG finS deceqS definition orbit (hom : G β†’ perm S) (H : finset G) (a : S) : finset S := image (move_by a) (image hom H) variable [deceqG : decidable_eq G] include deceqG -- required by {x ∈ H |p x} filtering definition moverset (hom : G β†’ perm S) (H : finset G) (a b : S) : finset G := {f ∈ H | hom f a = b} definition stab (hom : G β†’ perm S) (H : finset G) (a : S) : finset G := {f ∈ H | hom f a = a} end def section orbit_stabilizer variables {G S : Type} variable [ambientG : group G] include ambientG variable [finS : fintype S] include finS variable [deceqS : decidable_eq S] include deceqS section variables {hom : G β†’ perm S} {H : finset G} {a : S} [Hom : is_hom_class hom] include Hom lemma exists_of_orbit {b : S} : b ∈ orbit hom H a β†’ βˆƒ h, h ∈ H ∧ hom h a = b := assume Pb, obtain p (Pp₁ : p ∈ image hom H) (Ppβ‚‚ : move_by a p = b), from exists_of_mem_image Pb, obtain h (Ph₁ : h ∈ H) (Phβ‚‚ : hom h = p), from exists_of_mem_image Pp₁, assert Phab : hom h a = b, from calc hom h a = p a : Phβ‚‚ ... = b : Ppβ‚‚, exists.intro h (and.intro Ph₁ Phab) lemma orbit_of_exists {b : S} : (βˆƒ h, h ∈ H ∧ hom h a = b) β†’ b ∈ orbit hom H a := assume Pex, obtain h PinH Phab, from Pex, mem_image_of_mem_of_eq (mem_image_of_mem hom PinH) Phab end variable [deceqG : decidable_eq G] include deceqG -- these are already specified by stab hom H a variables {hom : G β†’ perm S} {H : finset G} {a : S} variable [Hom : is_hom_class hom] include Hom lemma stab_lmul {f g : G} : g ∈ stab hom H a β†’ hom (f*g) a = hom f a := assume Pgstab, assert Pg : hom g a = a, from of_mem_filter Pgstab, calc hom (f*g) a = perm.f ((hom f) * (hom g)) a : is_hom hom ... = ((hom f) ∘ (hom g)) a : rfl ... = (hom f) a : Pg lemma stab_subset : stab hom H a βŠ† H := begin apply subset_of_forall, intro f Pfstab, apply mem_of_mem_filter Pfstab end lemma reverse_move {h g : G} : g ∈ moverset hom H a (hom h a) β†’ hom (h⁻¹*g) a = a := assume Pg, assert Pga : hom g a = hom h a, from of_mem_filter Pg, calc hom (h⁻¹*g) a = perm.f ((hom h⁻¹) * (hom g)) a : is_hom hom ... = ((hom h⁻¹) ∘ hom g) a : rfl ... = ((hom h⁻¹) ∘ hom h) a : {Pga} ... = perm.f ((hom h⁻¹) * hom h) a : rfl ... = perm.f ((hom h)⁻¹ * hom h) a : hom_map_inv hom h ... = (1 : perm S) a : mul.left_inv (hom h) ... = a : rfl lemma moverset_inj_on_orbit : set.inj_on (moverset hom H a) (ts (orbit hom H a)) := take b1 b2, assume Pb1, obtain h1 Ph1₁ Ph1β‚‚, from exists_of_orbit Pb1, assert Ph1b1 : h1 ∈ moverset hom H a b1, from mem_filter_of_mem Ph1₁ Ph1β‚‚, assume Psetb2 Pmeq, begin subst b1, rewrite Pmeq at Ph1b1, apply of_mem_filter Ph1b1 end variable [subgH : is_finsubg H] include subgH lemma subg_stab_of_move {h g : G} : h ∈ H β†’ g ∈ moverset hom H a (hom h a) β†’ h⁻¹*g ∈ stab hom H a := assume Ph Pg, assert Phinvg : h⁻¹*g ∈ H, from begin apply finsubg_mul_closed H, apply finsubg_has_inv H, assumption, apply mem_of_mem_filter Pg end, mem_filter_of_mem Phinvg (reverse_move Pg) lemma subg_stab_closed : finset_mul_closed_on (stab hom H a) := take f g, assume Pfstab, assert Pf : hom f a = a, from of_mem_filter Pfstab, assume Pgstab, assert Pfg : hom (f*g) a = a, from calc hom (f*g) a = (hom f) a : stab_lmul Pgstab ... = a : Pf, assert PfginH : (f*g) ∈ H, from finsubg_mul_closed H f g (mem_of_mem_filter Pfstab) (mem_of_mem_filter Pgstab), mem_filter_of_mem PfginH Pfg lemma subg_stab_has_one : 1 ∈ stab hom H a := assert P : hom 1 a = a, from calc hom 1 a = (1 : perm S) a : {hom_map_one hom} ... = a : rfl, assert PoneinH : 1 ∈ H, from finsubg_has_one H, mem_filter_of_mem PoneinH P lemma subg_stab_has_inv : finset_has_inv (stab hom H a) := take f, assume Pfstab, assert Pf : hom f a = a, from of_mem_filter Pfstab, assert Pfinv : hom f⁻¹ a = a, from calc hom f⁻¹ a = hom f⁻¹ ((hom f) a) : Pf ... = perm.f ((hom f⁻¹) * (hom f)) a : rfl ... = hom (f⁻¹ * f) a : is_hom hom ... = hom 1 a : mul.left_inv ... = (1 : perm S) a : hom_map_one hom, assert PfinvinH : f⁻¹ ∈ H, from finsubg_has_inv H f (mem_of_mem_filter Pfstab), mem_filter_of_mem PfinvinH Pfinv definition subg_stab_is_finsubg [instance] : is_finsubg (stab hom H a) := is_finsubg.mk subg_stab_has_one subg_stab_closed subg_stab_has_inv lemma subg_lcoset_eq_moverset {h : G} : h ∈ H β†’ fin_lcoset (stab hom H a) h = moverset hom H a (hom h a) := assume Ph, ext (take g, iff.intro (assume Pl, obtain f (Pf₁ : f ∈ stab hom H a) (Pfβ‚‚ : h*f = g), from exists_of_mem_image Pl, assert Pfstab : hom f a = a, from of_mem_filter Pf₁, assert PginH : g ∈ H, begin subst Pfβ‚‚, apply finsubg_mul_closed H, assumption, apply mem_of_mem_filter Pf₁ end, assert Pga : hom g a = hom h a, from calc hom g a = hom (h*f) a : by subst g ... = hom h a : stab_lmul Pf₁, mem_filter_of_mem PginH Pga) (assume Pr, begin rewrite [↑fin_lcoset, mem_image_iff], existsi h⁻¹*g, split, exact subg_stab_of_move Ph Pr, apply mul_inv_cancel_left end)) lemma subg_moverset_of_orbit_is_lcoset_of_stab (b : S) : b ∈ orbit hom H a β†’ βˆƒ h, h ∈ H ∧ fin_lcoset (stab hom H a) h = moverset hom H a b := assume Porb, obtain p (Pp₁ : p ∈ image hom H) (Ppβ‚‚ : move_by a p = b), from exists_of_mem_image Porb, obtain h (Ph₁ : h ∈ H) (Phβ‚‚ : hom h = p), from exists_of_mem_image Pp₁, assert Phab : hom h a = b, from by subst p; assumption, exists.intro h (and.intro Ph₁ (Phab β–Έ subg_lcoset_eq_moverset Ph₁)) lemma subg_lcoset_of_stab_is_moverset_of_orbit (h : G) : h ∈ H β†’ βˆƒ b, b ∈ orbit hom H a ∧ moverset hom H a b = fin_lcoset (stab hom H a) h := assume Ph, have Pha : (hom h a) ∈ orbit hom H a, by apply mem_image_of_mem; apply mem_image_of_mem; exact Ph, exists.intro (hom h a) (and.intro Pha (eq.symm (subg_lcoset_eq_moverset Ph))) lemma subg_moversets_of_orbit_eq_stab_lcosets : image (moverset hom H a) (orbit hom H a) = fin_lcosets (stab hom H a) H := ext (take s, iff.intro (assume Pl, obtain b Pb₁ Pbβ‚‚, from exists_of_mem_image Pl, obtain h Ph, from subg_moverset_of_orbit_is_lcoset_of_stab b Pb₁, begin rewrite [↑fin_lcosets, mem_image_eq], existsi h, subst Pbβ‚‚, assumption end) (assume Pr, obtain h Ph₁ Phβ‚‚, from exists_of_mem_image Pr, obtain b Pb, from @subg_lcoset_of_stab_is_moverset_of_orbit G S ambientG finS deceqS deceqG hom H a Hom subgH h Ph₁, begin rewrite [mem_image_eq], existsi b, subst Phβ‚‚, assumption end)) open nat theorem orbit_stabilizer_theorem : card H = card (orbit hom H a) * card (stab hom H a) := calc card H = card (fin_lcosets (stab hom H a) H) * card (stab hom H a) : lagrange_theorem stab_subset ... = card (image (moverset hom H a) (orbit hom H a)) * card (stab hom H a) : subg_moversets_of_orbit_eq_stab_lcosets ... = card (orbit hom H a) * card (stab hom H a) : card_image_eq_of_inj_on moverset_inj_on_orbit end orbit_stabilizer section orbit_partition variables {G S : Type} [ambientG : group G] [finS : fintype S] variables [deceqS : decidable_eq S] include ambientG finS deceqS variables {hom : G β†’ perm S} [Hom : is_hom_class hom] {H : finset G} [subgH : is_finsubg H] include Hom subgH lemma self_in_orbit {a : S} : a ∈ orbit hom H a := mem_image_of_mem_of_eq (mem_image_of_mem_of_eq (finsubg_has_one H) (hom_map_one hom)) rfl lemma orbit_is_partition : is_partition (orbit hom H) := take a b, propext (iff.intro (assume Painb, obtain h PhinH Phba, from exists_of_orbit Painb, assert Phinvab : perm.f (hom h)⁻¹ a = b, from calc perm.f (hom h)⁻¹ a = perm.f (hom h)⁻¹ ((hom h) b) : Phba ... = perm.f ((hom h)⁻¹ * (hom h)) b : rfl ... = perm.f (1 : perm S) b : mul.left_inv, ext take c, iff.intro (assume Pcina, obtain g PginH Pgac, from exists_of_orbit Pcina, orbit_of_exists (exists.intro (g*h) (and.intro (finsubg_mul_closed H _ _ PginH PhinH) (calc hom (g*h) b = perm.f ((hom g) * (hom h)) b : is_hom hom ... = ((hom g) ∘ (hom h)) b : rfl ... = (hom g) a : Phba ... = c : Pgac)))) (assume Pcinb, obtain g PginH Pgbc, from exists_of_orbit Pcinb, orbit_of_exists (exists.intro (g*h⁻¹) (and.intro (finsubg_mul_closed H _ _ PginH (finsubg_has_inv H _ PhinH)) (calc (hom (g * h⁻¹)) a = perm.f ((hom g) * (hom h⁻¹)) a : is_hom hom ... = perm.f ((hom g) * (hom h)⁻¹) a : hom_map_inv hom h ... = ((hom g) ∘ (hom h)⁻¹) a : rfl ... = (hom g) b : Phinvab ... = c : Pgbc))))) (assume Peq, Peq β–Έ self_in_orbit)) end orbit_partition section cayley variables {G : Type} variable [ambientG : group G] include ambientG variable [finG : fintype G] include finG definition action_by_lmul : G β†’ perm G := take g, perm.mk (lmul_by g) (lmul_inj g) variable [deceqG : decidable_eq G] include deceqG lemma action_by_lmul_hom : homomorphic (@action_by_lmul G _ _) := take g₁ (gβ‚‚ : G), eq.symm (calc action_by_lmul g₁ * action_by_lmul gβ‚‚ = perm.mk ((lmul_by g₁)∘(lmul_by gβ‚‚)) _ : rfl ... = perm.mk (lmul_by (g₁*gβ‚‚)) _ : by congruence; apply coset.lmul_compose) lemma action_by_lmul_inj : injective (@action_by_lmul G _ _) := take g₁ gβ‚‚, assume Peq, perm.no_confusion Peq (Ξ» Pfeq Pqeq, have Pappeq : g₁*1 = gβ‚‚*1, from congr_fun Pfeq _, calc g₁ = g₁ * 1 : mul_one ... = gβ‚‚ * 1 : Pappeq ... = gβ‚‚ : mul_one) definition action_by_lmul_is_iso [instance] : is_iso_class (@action_by_lmul G _ _) := is_iso_class.mk action_by_lmul_hom action_by_lmul_inj end cayley section perm_fin open fin nat eq.ops variable {n : nat} definition lift_perm (p : perm (fin n)) : perm (fin (succ n)) := perm.mk (lift_fun p) (lift_fun_of_inj (perm.inj p)) definition lower_perm (p : perm (fin (succ n))) (P : p maxi = maxi) : perm (fin n) := perm.mk (lower_inj p (perm.inj p) P) (take i j, begin rewrite [-eq_iff_veq, *lower_inj_apply, eq_iff_veq], apply injective_compose (perm.inj p) lift_succ_inj end) lemma lift_lower_eq : βˆ€ {p : perm (fin (succ n))} (P : p maxi = maxi), lift_perm (lower_perm p P) = p | (perm.mk pf Pinj) := assume Pmax, begin rewrite [↑lift_perm], congruence, apply funext, intro i, assert Pfmax : pf maxi = maxi, apply Pmax, assert Pd : decidable (i = maxi), exact _, cases Pd with Pe Pne, rewrite [Pe, Pfmax], apply lift_fun_max, rewrite [lift_fun_of_ne_max Pne, ↑lower_perm, ↑lift_succ], rewrite [-eq_iff_veq, -val_lift, lower_inj_apply, eq_iff_veq], congruence, rewrite [-eq_iff_veq] end lemma lift_perm_inj : injective (@lift_perm n) := take p1 p2, assume Peq, eq_of_feq (lift_fun_inj (feq_of_eq Peq)) lemma lift_perm_inj_on_univ : set.inj_on (@lift_perm n) (ts univ) := eq.symm to_set_univ β–Έ iff.elim_left set.injective_iff_inj_on_univ lift_perm_inj lemma lift_to_stab : image (@lift_perm n) univ = stab id univ maxi := ext (take (pp : perm (fin (succ n))), iff.intro (assume Pimg, obtain p P_ Pp, from exists_of_mem_image Pimg, assert Ppp : pp maxi = maxi, from calc pp maxi = lift_perm p maxi : {eq.symm Pp} ... = lift_fun p maxi : rfl ... = maxi : lift_fun_max, mem_filter_of_mem !mem_univ Ppp) (assume Pstab, assert Ppp : pp maxi = maxi, from of_mem_filter Pstab, mem_image_of_mem_of_eq !mem_univ (lift_lower_eq Ppp))) definition move_from_max_to (i : fin (succ n)) : perm (fin (succ n)) := perm.mk (madd (i - maxi)) madd_inj lemma orbit_max : orbit (@id (perm (fin (succ n)))) univ maxi = univ := ext (take i, iff.intro (assume P, !mem_univ) (assume P, begin apply mem_image_of_mem_of_eq, apply mem_image_of_mem_of_eq, apply mem_univ (move_from_max_to i), apply rfl, apply sub_add_cancel end)) lemma card_orbit_max : card (orbit (@id (perm (fin (succ n)))) univ maxi) = succ n := calc card (orbit (@id (perm (fin (succ n)))) univ maxi) = card univ : {orbit_max} ... = succ n : card_fin (succ n) open fintype lemma card_lift_to_stab : card (stab (@id (perm (fin (succ n)))) univ maxi) = card (perm (fin n)) := calc finset.card (stab (@id (perm (fin (succ n)))) univ maxi) = finset.card (image (@lift_perm n) univ) : {eq.symm lift_to_stab} ... = card univ : card_image_eq_of_inj_on lift_perm_inj_on_univ lemma card_perm_step : card (perm (fin (succ n))) = (succ n) * card (perm (fin n)) := calc card (perm (fin (succ n))) = card (orbit id univ maxi) * card (stab id univ maxi) : orbit_stabilizer_theorem ... = (succ n) * card (stab id univ maxi) : {card_orbit_max} ... = (succ n) * card (perm (fin n)) : {card_lift_to_stab} end perm_fin end group
22addf78214fe9b04089d3e056a43ba6efe5fccd
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/linear_algebra/dimension.lean
30f879d56a1d193e9f2276704a36f62cb4dd7d2c
[ "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
20,627
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Johannes HΓΆlzl, Sander Dahmen -/ import linear_algebra.basis import set_theory.cardinal_ordinal /-! # Dimension of modules and vector spaces ## Main definitions * The dimension of a vector space is defined as `vector_space.dim : cardinal`. ## Main statements * `mk_eq_mk_of_basis`: the dimension theorem, any two bases of the same vector space have the same cardinality. * `dim_quotient_add_dim`: if V₁ is a submodule of V, then dim (V/V₁) + dim V₁ = dim V. * `dim_range_add_dim_ker`: the rank-nullity theorem. ## Implementation notes Many theorems in this file are not universe-generic when they relate dimensions in different universes. They should be as general as they can be without inserting `lift`s. The types `V`, `V'`, ... all live in different universes, and `V₁`, `Vβ‚‚`, ... all live in the same universe. -/ noncomputable theory universes u v v' v'' u₁' w w' variables {K : Type u} {V V₁ Vβ‚‚ V₃ : Type v} {V' V'₁ : Type v'} {V'' : Type v''} variables {ΞΉ : Type w} {ΞΉ' : Type w'} {Ξ· : Type u₁'} {Ο† : Ξ· β†’ Type*} open_locale classical big_operators section vector_space variables [field K] [add_comm_group V] [vector_space K V] [add_comm_group V₁] [vector_space K V₁] include K open submodule function set variables (K V) /-- the dimension of a vector space, defined as a term of type `cardinal` -/ def vector_space.dim : cardinal := cardinal.min (nonempty_subtype.2 (@exists_is_basis K V _ _ _)) (Ξ» b, cardinal.mk b.1) variables {K V} open vector_space section theorem is_basis.le_span {v : ΞΉ β†’ V} {J : set V} (hv : is_basis K v) (hJ : span K J = ⊀) : cardinal.mk (range v) ≀ cardinal.mk J := begin cases le_or_lt cardinal.omega (cardinal.mk J) with oJ oJ, { have := cardinal.mk_range_eq_of_injective (linear_independent.injective hv.1), let S : J β†’ set ΞΉ := Ξ» j, ↑(is_basis.repr hv j).support, let S' : J β†’ set V := Ξ» j, v '' S j, have hs : range v βŠ† ⋃ j, S' j, { intros b hb, rcases mem_range.1 hb with ⟨i, hi⟩, have : span K J ≀ comap hv.repr (finsupp.supported K K (⋃ j, S j)) := span_le.2 (Ξ» j hj x hx, ⟨_, ⟨⟨j, hj⟩, rfl⟩, hx⟩), rw hJ at this, replace : hv.repr (v i) ∈ (finsupp.supported K K (⋃ j, S j)) := this trivial, rw [hv.repr_eq_single, finsupp.mem_supported, finsupp.support_single_ne_zero one_ne_zero] at this, { subst b, rcases mem_Union.1 (this (finset.mem_singleton_self _)) with ⟨j, hj⟩, exact mem_Union.2 ⟨j, (mem_image _ _ _).2 ⟨i, hj, rfl⟩⟩ }, { apply_instance } }, refine le_of_not_lt (Ξ» IJ, _), suffices : cardinal.mk (⋃ j, S' j) < cardinal.mk (range v), { exact not_le_of_lt this ⟨set.embedding_of_subset _ _ hs⟩ }, refine lt_of_le_of_lt (le_trans cardinal.mk_Union_le_sum_mk (cardinal.sum_le_sum _ (Ξ» _, cardinal.omega) _)) _, { exact Ξ» j, le_of_lt (cardinal.lt_omega_iff_finite.2 $ (finset.finite_to_set _).image _) }, { rwa [cardinal.sum_const, cardinal.mul_eq_max oJ (le_refl _), max_eq_left oJ] } }, { rcases exists_finite_card_le_of_finite_of_linear_independent_of_span (cardinal.lt_omega_iff_finite.1 oJ) hv.1.to_subtype_range _ with ⟨fI, hi⟩, { rwa [← cardinal.nat_cast_le, cardinal.finset_card, set.finite.coe_to_finset, cardinal.finset_card, set.finite.coe_to_finset] at hi, }, { rw hJ, apply set.subset_univ } }, end end /-- dimension theorem -/ theorem mk_eq_mk_of_basis {v : ΞΉ β†’ V} {v' : ΞΉ' β†’ V} (hv : is_basis K v) (hv' : is_basis K v') : cardinal.lift.{w w'} (cardinal.mk ΞΉ) = cardinal.lift.{w' w} (cardinal.mk ΞΉ') := begin rw ←cardinal.lift_inj.{(max w w') v}, rw [cardinal.lift_lift, cardinal.lift_lift], apply le_antisymm, { convert cardinal.lift_le.{v (max w w')}.2 (hv.le_span hv'.2), { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_injective hv.injective).symm, }, { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_injective hv'.injective).symm, }, }, { convert cardinal.lift_le.{v (max w w')}.2 (hv'.le_span hv.2), { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_injective hv'.injective).symm, }, { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_injective hv.injective).symm, }, } end theorem is_basis.mk_range_eq_dim {v : ΞΉ β†’ V} (h : is_basis K v) : cardinal.mk (range v) = dim K V := begin have := show βˆƒ v', dim K V = _, from cardinal.min_eq _ _, rcases this with ⟨v', e⟩, rw e, apply cardinal.lift_inj.1, rw cardinal.mk_range_eq_of_injective h.injective, convert @mk_eq_mk_of_basis _ _ _ _ _ _ _ _ _ h v'.property end theorem is_basis.mk_eq_dim {v : ΞΉ β†’ V} (h : is_basis K v) : cardinal.lift.{w v} (cardinal.mk ΞΉ) = cardinal.lift.{v w} (dim K V) := by rw [←h.mk_range_eq_dim, cardinal.mk_range_eq_of_injective h.injective] theorem {m} is_basis.mk_eq_dim' {v : ΞΉ β†’ V} (h : is_basis K v) : cardinal.lift.{w (max v m)} (cardinal.mk ΞΉ) = cardinal.lift.{v (max w m)} (dim K V) := by simpa using h.mk_eq_dim theorem is_basis.mk_eq_dim'' {b : set V} (hb : is_basis K (Ξ» x : b, (x : V))) : cardinal.mk b = dim K V := (dim K V).lift_id β–Έ hb.mk_eq_dim β–Έ (cardinal.mk b).lift_id.symm theorem dim_le {n : β„•} (H : βˆ€ s : finset V, linear_independent K (Ξ» i : (↑s : set V), (i : V)) β†’ s.card ≀ n) : dim K V ≀ n := let ⟨b, hb⟩ := exists_is_basis K V in hb.mk_eq_dim'' β–Έ cardinal.card_le_of (Ξ» s, @finset.card_map _ _ ⟨_, subtype.val_injective⟩ s β–Έ H _ (by { refine hb.1.mono (Ξ» y h, _), rw [finset.mem_coe, finset.mem_map] at h, rcases h with ⟨x, hx, rfl⟩, exact x.2 } )) variables [add_comm_group V'] [vector_space K V'] /-- Two linearly equivalent vector spaces have the same dimension. -/ theorem linear_equiv.dim_eq (f : V ≃ₗ[K] V₁) : dim K V = dim K V₁ := by letI := classical.dec_eq V; letI := classical.dec_eq V₁; exact let ⟨b, hb⟩ := exists_is_basis K V in cardinal.lift_inj.1 $ hb.mk_eq_dim.symm.trans (f.is_basis hb).mk_eq_dim @[simp] lemma dim_bot : dim K (βŠ₯ : submodule K V) = 0 := by letI := classical.dec_eq V; rw [← cardinal.lift_inj, ← (@is_basis_empty_bot pempty K V _ _ _ not_nonempty_pempty).mk_eq_dim, cardinal.mk_pempty] @[simp] lemma dim_top : dim K (⊀ : submodule K V) = dim K V := linear_equiv.dim_eq (linear_equiv.of_top _ rfl) lemma dim_of_field (K : Type*) [field K] : dim K K = 1 := by rw [←cardinal.lift_inj, ← (@is_basis_singleton_one punit K _ _).mk_eq_dim, cardinal.mk_punit] lemma dim_span {v : ΞΉ β†’ V} (hv : linear_independent K v) : dim K β†₯(span K (range v)) = cardinal.mk (range v) := by rw [←cardinal.lift_inj, ← (is_basis_span hv).mk_eq_dim, cardinal.mk_range_eq_of_injective (@linear_independent.injective ΞΉ K V v _ _ _ _ hv)] lemma dim_span_set {s : set V} (hs : linear_independent K (Ξ» x, x : s β†’ V)) : dim K β†₯(span K s) = cardinal.mk s := by { rw [← @set_of_mem_eq _ s, ← subtype.range_coe_subtype], exact dim_span hs } lemma {m} cardinal_lift_le_dim_of_linear_independent {ΞΉ : Type w} {v : ΞΉ β†’ V} (hv : linear_independent K v) : cardinal.lift.{w (max v m)} (cardinal.mk ΞΉ) ≀ cardinal.lift.{v (max w m)} (dim K V) := begin obtain ⟨ι', v', is⟩ := exists_sum_is_basis hv, rw [← cardinal.lift_umax, ← cardinal.lift_umax.{v}], simpa using le_trans (cardinal.lift_mk_le.{w _ (max v m)}.2 ⟨@function.embedding.inl ΞΉ ΞΉ'⟩) (le_of_eq $ is_basis.mk_eq_dim'.{_ _ _ (max w m)} is), end lemma cardinal_le_dim_of_linear_independent {ΞΉ : Type v} {v : ΞΉ β†’ V} (hv : linear_independent K v) : (cardinal.mk ΞΉ) ≀ (dim.{u v} K V) := by simpa using cardinal_lift_le_dim_of_linear_independent hv lemma cardinal_le_dim_of_linear_independent' {s : set V} (hs : linear_independent K (Ξ» x, x : s β†’ V)) : cardinal.mk s ≀ dim K V := begin -- extend s to a basis obtain ⟨b, ss, h⟩ := exists_subset_is_basis hs, rw [←h.mk_range_eq_dim, subtype.range_coe], apply cardinal.mk_le_of_injective (inclusion_injective ss), end lemma dim_span_le (s : set V) : dim K (span K s) ≀ cardinal.mk s := begin classical, rcases exists_linear_independent (linear_independent_empty K V) (set.empty_subset s) with ⟨b, hb, _, hsb, hlib⟩, have hsab : span K s = span K b, from span_eq_of_le _ hsb (span_le.2 (Ξ» x hx, subset_span (hb hx))), convert cardinal.mk_le_mk_of_subset hb, rw [hsab, dim_span_set hlib] end lemma dim_span_of_finset (s : finset V) : dim K (span K (↑s : set V)) < cardinal.omega := calc dim K (span K (↑s : set V)) ≀ cardinal.mk (↑s : set V) : dim_span_le ↑s ... = s.card : by rw ←cardinal.finset_card ... < cardinal.omega : cardinal.nat_lt_omega _ theorem dim_prod : dim K (V Γ— V₁) = dim K V + dim K V₁ := begin letI := classical.dec_eq V, letI := classical.dec_eq V₁, rcases exists_is_basis K V with ⟨b, hb⟩, rcases exists_is_basis K V₁ with ⟨c, hc⟩, rw [← cardinal.lift_inj, ← @is_basis.mk_eq_dim K (V Γ— V₁) _ _ _ _ _ (is_basis_inl_union_inr hb hc), cardinal.lift_add, cardinal.lift_mk, ← hb.mk_eq_dim, ← hc.mk_eq_dim, cardinal.lift_mk, cardinal.lift_mk, cardinal.add_def (ulift b) (ulift c)], exact cardinal.lift_inj.1 (cardinal.lift_mk_eq.2 ⟨equiv.ulift.trans (equiv.sum_congr (@equiv.ulift b) (@equiv.ulift c)).symm ⟩), end theorem dim_quotient_add_dim (p : submodule K V) : dim K p.quotient + dim K p = dim K V := by classical; exact let ⟨f⟩ := quotient_prod_linear_equiv p in dim_prod.symm.trans f.dim_eq theorem dim_quotient_le (p : submodule K V) : dim K p.quotient ≀ dim K V := by { rw ← dim_quotient_add_dim p, exact cardinal.le_add_right _ _ } /-- rank-nullity theorem -/ theorem dim_range_add_dim_ker (f : V β†’β‚—[K] V₁) : dim K f.range + dim K f.ker = dim K V := begin haveI := Ξ» (p : submodule K V), classical.dec_eq p.quotient, rw [← f.quot_ker_equiv_range.dim_eq, dim_quotient_add_dim] end lemma dim_range_le (f : V β†’β‚—[K] V₁) : dim K f.range ≀ dim K V := by rw ← dim_range_add_dim_ker f; exact le_add_right (le_refl _) lemma dim_map_le (f : V β†’β‚— V₁) (p : submodule K V) : dim K (p.map f) ≀ dim K p := begin have h := dim_range_le (f.comp (submodule.subtype p)), rwa [linear_map.range_comp, range_subtype] at h, end lemma dim_range_of_surjective (f : V β†’β‚—[K] V') (h : surjective f) : dim K f.range = dim K V' := begin refine linear_equiv.dim_eq (linear_equiv.of_bijective (submodule.subtype _) _ _), exact linear_map.ker_eq_bot.2 subtype.val_injective, rwa [range_subtype, linear_map.range_eq_top] end lemma dim_eq_of_surjective (f : V β†’β‚—[K] V₁) (h : surjective f) : dim K V = dim K V₁ + dim K f.ker := by rw [← dim_range_add_dim_ker f, ← dim_range_of_surjective f h] lemma dim_le_of_surjective (f : V β†’β‚—[K] V₁) (h : surjective f) : dim K V₁ ≀ dim K V := by rw [dim_eq_of_surjective f h]; refine le_add_right (le_refl _) lemma dim_eq_of_injective (f : V β†’β‚—[K] V₁) (h : injective f) : dim K V = dim K f.range := by rw [← dim_range_add_dim_ker f, linear_map.ker_eq_bot.2 h]; simp [dim_bot] lemma dim_submodule_le (s : submodule K V) : dim K s ≀ dim K V := by { rw ← dim_quotient_add_dim s, exact cardinal.le_add_left _ _ } lemma dim_le_of_injective (f : V β†’β‚—[K] V₁) (h : injective f) : dim K V ≀ dim K V₁ := by { rw [dim_eq_of_injective f h], exact dim_submodule_le _ } lemma dim_le_of_submodule (s t : submodule K V) (h : s ≀ t) : dim K s ≀ dim K t := dim_le_of_injective (of_le h) $ assume ⟨x, hx⟩ ⟨y, hy⟩ eq, subtype.eq $ show x = y, from subtype.ext_iff_val.1 eq lemma linear_independent_le_dim {v : ΞΉ β†’ V} (hv : linear_independent K v) : cardinal.lift.{w v} (cardinal.mk ΞΉ) ≀ cardinal.lift.{v w} (dim K V) := calc cardinal.lift.{w v} (cardinal.mk ΞΉ) = cardinal.lift.{v w} (cardinal.mk (set.range v)) : (cardinal.mk_range_eq_of_injective (linear_independent.injective hv)).symm ... = cardinal.lift.{v w} (dim K (submodule.span K (set.range v))) : by rw (dim_span hv).symm ... ≀ cardinal.lift.{v w} (dim K V) : cardinal.lift_le.2 (dim_submodule_le (submodule.span K _)) theorem {u₁} linear_independent_le_dim' {v : ΞΉ β†’ V} (hs : linear_independent K v) : ((cardinal.mk ΞΉ).lift : cardinal.{(max w v u₁)}) ≀ ((vector_space.dim K V).lift : cardinal.{(max v w u₁)}) := cardinal.mk_range_eq_lift hs.injective β–Έ dim_span hs β–Έ cardinal.lift_le.2 (dim_submodule_le _) section variables [add_comm_group Vβ‚‚] [vector_space K Vβ‚‚] variables [add_comm_group V₃] [vector_space K V₃] open linear_map /-- This is mostly an auxiliary lemma for `dim_sup_add_dim_inf_eq`. -/ lemma dim_add_dim_split (db : Vβ‚‚ β†’β‚—[K] V) (eb : V₃ β†’β‚—[K] V) (cd : V₁ β†’β‚—[K] Vβ‚‚) (ce : V₁ β†’β‚—[K] V₃) (hde : ⊀ ≀ db.range βŠ” eb.range) (hgd : ker cd = βŠ₯) (eq : db.comp cd = eb.comp ce) (eqβ‚‚ : βˆ€d e, db d = eb e β†’ (βˆƒc, cd c = d ∧ ce c = e)) : dim K V + dim K V₁ = dim K Vβ‚‚ + dim K V₃ := have hf : surjective (coprod db eb), begin refine (range_eq_top.1 $ top_unique $ _), rwa [← map_top, ← prod_top, map_coprod_prod] end, begin conv {to_rhs, rw [← dim_prod, dim_eq_of_surjective _ hf] }, congr' 1, apply linear_equiv.dim_eq, refine linear_equiv.of_bijective _ _ _, { refine cod_restrict _ (prod cd (- ce)) _, { assume c, simp only [add_eq_zero_iff_eq_neg, prod_apply, mem_ker, coprod_apply, neg_neg, map_neg, neg_apply], exact linear_map.ext_iff.1 eq c } }, { rw [ker_cod_restrict, ker_prod, hgd, bot_inf_eq] }, { rw [eq_top_iff, range_cod_restrict, ← map_le_iff_le_comap, map_top, range_subtype], rintros ⟨d, e⟩, have h := eqβ‚‚ d (-e), simp only [add_eq_zero_iff_eq_neg, prod_apply, mem_ker, mem_coe, prod.mk.inj_iff, coprod_apply, map_neg, neg_apply, linear_map.mem_range] at ⊒ h, assume hde, rcases h hde with ⟨c, h₁, hβ‚‚βŸ©, refine ⟨c, h₁, _⟩, rw [hβ‚‚, _root_.neg_neg] } end lemma dim_sup_add_dim_inf_eq (s t : submodule K V) : dim K (s βŠ” t : submodule K V) + dim K (s βŠ“ t : submodule K V) = dim K s + dim K t := dim_add_dim_split (of_le le_sup_left) (of_le le_sup_right) (of_le inf_le_left) (of_le inf_le_right) begin rw [← map_le_map_iff' (ker_subtype $ s βŠ” t), map_sup, map_top, ← linear_map.range_comp, ← linear_map.range_comp, subtype_comp_of_le, subtype_comp_of_le, range_subtype, range_subtype, range_subtype], exact le_refl _ end (ker_of_le _ _ _) begin ext ⟨x, hx⟩, refl end begin rintros ⟨b₁, hbβ‚βŸ© ⟨bβ‚‚, hbβ‚‚βŸ© eq, have : b₁ = bβ‚‚ := congr_arg subtype.val eq, subst this, exact ⟨⟨b₁, hb₁, hbβ‚‚βŸ©, rfl, rfl⟩ end lemma dim_add_le_dim_add_dim (s t : submodule K V) : dim K (s βŠ” t : submodule K V) ≀ dim K s + dim K t := by rw [← dim_sup_add_dim_inf_eq]; exact le_add_right (le_refl _) end section fintype variable [fintype Ξ·] variables [βˆ€i, add_comm_group (Ο† i)] [βˆ€i, vector_space K (Ο† i)] open linear_map lemma dim_pi : vector_space.dim K (Ξ i, Ο† i) = cardinal.sum (Ξ»i, vector_space.dim K (Ο† i)) := begin choose b hb using assume i, exists_is_basis K (Ο† i), have : is_basis K (Ξ» (ji : Ξ£ j, b j), std_basis K (Ξ» j, Ο† j) ji.fst ji.snd.val), by apply pi.is_basis_std_basis _ hb, rw [←cardinal.lift_inj, ← this.mk_eq_dim], simp [Ξ» i, (hb i).mk_range_eq_dim.symm, cardinal.sum_mk] end lemma dim_fun {V Ξ· : Type u} [fintype Ξ·] [add_comm_group V] [vector_space K V] : vector_space.dim K (Ξ· β†’ V) = fintype.card Ξ· * vector_space.dim K V := by rw [dim_pi, cardinal.sum_const, cardinal.fintype_card] lemma dim_fun_eq_lift_mul : vector_space.dim K (Ξ· β†’ V) = (fintype.card Ξ· : cardinal.{max u₁' v}) * cardinal.lift.{v u₁'} (vector_space.dim K V) := by rw [dim_pi, cardinal.sum_const_eq_lift_mul, cardinal.fintype_card, cardinal.lift_nat_cast] lemma dim_fun' : vector_space.dim K (Ξ· β†’ K) = fintype.card Ξ· := by rw [dim_fun_eq_lift_mul, dim_of_field K, cardinal.lift_one, mul_one, cardinal.nat_cast_inj] lemma dim_fin_fun (n : β„•) : dim K (fin n β†’ K) = n := by simp [dim_fun'] end fintype lemma exists_mem_ne_zero_of_ne_bot {s : submodule K V} (h : s β‰  βŠ₯) : βˆƒ b : V, b ∈ s ∧ b β‰  0 := begin classical, by_contradiction hex, have : βˆ€x∈s, (x:V) = 0, { simpa only [not_exists, not_and, not_not, ne.def] using hex }, exact (h $ bot_unique $ assume s hs, (submodule.mem_bot K).2 $ this s hs) end lemma exists_mem_ne_zero_of_dim_pos {s : submodule K V} (h : 0 < vector_space.dim K s) : βˆƒ b : V, b ∈ s ∧ b β‰  0 := exists_mem_ne_zero_of_ne_bot $ assume eq, by rw [eq, dim_bot] at h; exact lt_irrefl _ h lemma exists_is_basis_fintype (h : dim K V < cardinal.omega) : βˆƒ s : (set V), (is_basis K (subtype.val : s β†’ V)) ∧ nonempty (fintype s) := begin cases exists_is_basis K V with s hs, rw [←cardinal.lift_lt, ← is_basis.mk_eq_dim hs, cardinal.lift_lt, cardinal.lt_omega_iff_fintype] at h, exact ⟨s, hs, h⟩ end section rank /-- `rank f` is the rank of a `linear_map f`, defined as the dimension of `f.range`. -/ def rank (f : V β†’β‚—[K] V') : cardinal := dim K f.range lemma rank_le_domain (f : V β†’β‚—[K] V₁) : rank f ≀ dim K V := by rw [← dim_range_add_dim_ker f]; exact le_add_right (le_refl _) lemma rank_le_range (f : V β†’β‚—[K] V₁) : rank f ≀ dim K V₁ := dim_submodule_le _ lemma rank_add_le (f g : V β†’β‚—[K] V') : rank (f + g) ≀ rank f + rank g := calc rank (f + g) ≀ dim K (f.range βŠ” g.range : submodule K V') : begin refine dim_le_of_submodule _ _ _, exact (linear_map.range_le_iff_comap.2 $ eq_top_iff'.2 $ assume x, show f x + g x ∈ (f.range βŠ” g.range : submodule K V'), from mem_sup.2 ⟨_, mem_image_of_mem _ (mem_univ _), _, mem_image_of_mem _ (mem_univ _), rfl⟩) end ... ≀ rank f + rank g : dim_add_le_dim_add_dim _ _ @[simp] lemma rank_zero : rank (0 : V β†’β‚—[K] V') = 0 := by rw [rank, linear_map.range_zero, dim_bot] lemma rank_finset_sum_le {Ξ·} (s : finset Ξ·) (f : Ξ· β†’ V β†’β‚—[K] V') : rank (βˆ‘ d in s, f d) ≀ βˆ‘ d in s, rank (f d) := @finset.sum_hom_rel _ _ _ _ _ (Ξ»a b, rank a ≀ b) f (Ξ» d, rank (f d)) s (le_of_eq rank_zero) (Ξ» i g c h, le_trans (rank_add_le _ _) (add_le_add_left h _)) variables [add_comm_group V''] [vector_space K V''] lemma rank_comp_le1 (g : V β†’β‚—[K] V') (f : V' β†’β‚—[K] V'') : rank (f.comp g) ≀ rank f := begin refine dim_le_of_submodule _ _ _, rw [linear_map.range_comp], exact image_subset _ (subset_univ _) end variables [add_comm_group V'₁] [vector_space K V'₁] lemma rank_comp_le2 (g : V β†’β‚—[K] V') (f : V' β†’β‚— V'₁) : rank (f.comp g) ≀ rank g := by rw [rank, rank, linear_map.range_comp]; exact dim_map_le _ _ end rank lemma dim_zero_iff_forall_zero : vector_space.dim K V = 0 ↔ βˆ€ x : V, x = 0 := begin split, { intros h x, cases exists_is_basis K V with w hw, have card_mk_range := hw.mk_range_eq_dim, rw [h, cardinal.mk_emptyc_iff, subtype.range_coe] at card_mk_range, simpa [card_mk_range] using hw.mem_span x }, { intro h, have : (⊀ : submodule K V) = βŠ₯, { ext x, simp [h x] }, rw [←dim_top, this, dim_bot] } end lemma dim_pos_iff_exists_ne_zero : 0 < vector_space.dim K V ↔ βˆƒ x : V, x β‰  0 := begin rw ←not_iff_not, simpa using dim_zero_iff_forall_zero end lemma dim_pos_iff_nontrivial : 0 < vector_space.dim K V ↔ nontrivial V := begin rw dim_pos_iff_exists_ne_zero, split, { rintros ⟨x, h⟩, exact ⟨⟨x, 0, h⟩⟩ }, { introsI, exact exists_ne 0 } end lemma dim_pos [h : nontrivial V] : 0 < vector_space.dim K V := dim_pos_iff_nontrivial.2 h end vector_space section unconstrained_universes variables {E : Type v'} variables [field K] [add_comm_group V] [vector_space K V] [add_comm_group E] [vector_space K E] open vector_space /-- Version of linear_equiv.dim_eq without universe constraints. -/ theorem linear_equiv.dim_eq_lift (f : V ≃ₗ[K] E) : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K E) := begin cases exists_is_basis K V with b hb, rw [← cardinal.lift_inj.1 hb.mk_eq_dim, ← (f.is_basis hb).mk_eq_dim, cardinal.lift_mk], end end unconstrained_universes
4b3a70da88f03405cbddde5f39cca81b2355464b
bdb33f8b7ea65f7705fc342a178508e2722eb851
/data/multiset.lean
c359be70f9f2df78513fc07556700d15475e2ffc
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
95,900
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Multisets. -/ import logic.function data.list.basic data.list.perm data.list.sort order.boolean_algebra algebra.order_functions data.quot algebra.group_power algebra.ordered_group open list subtype nat lattice variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} local infix ` β€’ ` := add_monoid.smul instance list.perm.setoid (Ξ± : Type*) : setoid (list Ξ±) := setoid.mk perm ⟨perm.refl, @perm.symm _, @perm.trans _⟩ /-- `multiset Ξ±` is the quotient of `list Ξ±` by list permutation. The result is a type of finite sets with duplicates allowed. -/ def {u} multiset (Ξ± : Type u) : Type u := quotient (list.perm.setoid Ξ±) namespace multiset instance : has_coe (list Ξ±) (multiset Ξ±) := ⟨quot.mk _⟩ @[simp] theorem quot_mk_to_coe (l : list Ξ±) : @eq (multiset Ξ±) ⟦l⟧ l := rfl @[simp] theorem quot_mk_to_coe' (l : list Ξ±) : @eq (multiset Ξ±) (quot.mk (β‰ˆ) l) l := rfl @[simp] theorem quot_mk_to_coe'' (l : list Ξ±) : @eq (multiset Ξ±) (quot.mk setoid.r l) l := rfl @[simp] theorem coe_eq_coe (l₁ lβ‚‚ : list Ξ±) : (l₁ : multiset Ξ±) = lβ‚‚ ↔ l₁ ~ lβ‚‚ := quotient.eq instance has_decidable_eq [decidable_eq Ξ±] : decidable_eq (multiset Ξ±) | s₁ sβ‚‚ := quotient.rec_on_subsingletonβ‚‚ s₁ sβ‚‚ $ Ξ» l₁ lβ‚‚, decidable_of_iff' _ quotient.eq /- empty multiset -/ /-- `0 : multiset Ξ±` is the empty set -/ protected def zero : multiset Ξ± := @nil Ξ± instance : has_zero (multiset Ξ±) := ⟨multiset.zero⟩ instance : has_emptyc (multiset Ξ±) := ⟨0⟩ instance : inhabited (multiset Ξ±) := ⟨0⟩ @[simp] theorem coe_nil_eq_zero : (@nil Ξ± : multiset Ξ±) = 0 := rfl /- cons -/ /-- `cons a s` is the multiset which contains `s` plus one more instance of `a`. -/ def cons (a : Ξ±) (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (a :: l : multiset Ξ±)) (Ξ» l₁ lβ‚‚ p, quot.sound ((perm_cons a).2 p)) notation a :: b := cons a b instance : has_insert Ξ± (multiset Ξ±) := ⟨cons⟩ @[simp] theorem insert_eq_cons (a : Ξ±) (s : multiset Ξ±) : insert a s = a::s := rfl @[simp] theorem cons_coe (a : Ξ±) (l : list Ξ±) : (a::l : multiset Ξ±) = (a::l : list Ξ±) := rfl theorem singleton_coe (a : Ξ±) : (a::0 : multiset Ξ±) = ([a] : list Ξ±) := rfl @[simp] theorem cons_inj_left {a b : Ξ±} (s : multiset Ξ±) : a::s = b::s ↔ a = b := ⟨quot.induction_on s $ Ξ» l e, have [a] ++ l ~ [b] ++ l, from quotient.exact e, eq_singleton_of_perm $ (perm_app_right_iff _).1 this, congr_arg _⟩ @[simp] theorem cons_inj_right (a : Ξ±) {s t : multiset Ξ±} : a::s = a::t ↔ s = t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, by simp [perm_cons] @[recursor 5] protected theorem induction {p : multiset Ξ± β†’ Prop} (h₁ : p 0) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : multiset Ξ±}, p s β†’ p (a :: s)) (s) : p s := quot.induction_on s $ Ξ» l, by induction l with _ _ ih; [exact h₁, exact hβ‚‚ ih] @[elab_as_eliminator] protected theorem induction_on {p : multiset Ξ± β†’ Prop} (s : multiset Ξ±) (h₁ : p 0) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : multiset Ξ±}, p s β†’ p (a :: s)) : p s := multiset.induction h₁ hβ‚‚ s theorem cons_swap (a b : Ξ±) (s : multiset Ξ±) : a :: b :: s = b :: a :: s := quot.induction_on s $ Ξ» l, quotient.sound $ perm.swap _ _ _ section rec variables {C : multiset Ξ± β†’ Sort*} /-- Dependent recursor on multisets. TODO: should be @[recursor 6], but then the definition of `multiset.pi` failes with a stack overflow in `whnf`. -/ protected def rec (C_0 : C 0) (C_cons : Ξ a m, C m β†’ C (a::m)) (C_cons_heq : βˆ€a a' m b, C_cons a (a'::m) (C_cons a' m b) == C_cons a' (a::m) (C_cons a m b)) (m : multiset Ξ±) : C m := quotient.hrec_on m (@list.rec Ξ± (Ξ»l, C ⟦l⟧) C_0 (Ξ»a l b, C_cons a ⟦l⟧ b)) $ assume l l' h, list.rec_heq_of_perm h (assume a l l' b b' hl, have ⟦l⟧ = ⟦l'⟧, from quot.sound hl, by cc) (assume a a' l, C_cons_heq a a' ⟦l⟧) @[elab_as_eliminator] protected def rec_on (m : multiset Ξ±) (C_0 : C 0) (C_cons : Ξ a m, C m β†’ C (a::m)) (C_cons_heq : βˆ€a a' m b, C_cons a (a'::m) (C_cons a' m b) == C_cons a' (a::m) (C_cons a m b)) : C m := multiset.rec C_0 C_cons C_cons_heq m variables {C_0 : C 0} {C_cons : Ξ a m, C m β†’ C (a::m)} {C_cons_heq : βˆ€a a' m b, C_cons a (a'::m) (C_cons a' m b) == C_cons a' (a::m) (C_cons a m b)} @[simp] lemma rec_on_0 : @multiset.rec_on Ξ± C (0:multiset Ξ±) C_0 C_cons C_cons_heq = C_0 := rfl @[simp] lemma rec_on_cons (a : Ξ±) (m : multiset Ξ±) : (a :: m).rec_on C_0 C_cons C_cons_heq = C_cons a m (m.rec_on C_0 C_cons C_cons_heq) := quotient.induction_on m $ assume l, rfl end rec section mem /-- `a ∈ s` means that `a` has nonzero multiplicity in `s`. -/ def mem (a : Ξ±) (s : multiset Ξ±) : Prop := quot.lift_on s (Ξ» l, a ∈ l) (Ξ» l₁ lβ‚‚ (e : l₁ ~ lβ‚‚), propext $ mem_of_perm e) instance : has_mem Ξ± (multiset Ξ±) := ⟨mem⟩ @[simp] lemma mem_coe {a : Ξ±} {l : list Ξ±} : a ∈ (l : multiset Ξ±) ↔ a ∈ l := iff.rfl instance decidable_mem [decidable_eq Ξ±] (a : Ξ±) (s : multiset Ξ±) : decidable (a ∈ s) := quot.rec_on_subsingleton s $ list.decidable_mem a @[simp] theorem mem_cons {a b : Ξ±} {s : multiset Ξ±} : a ∈ b :: s ↔ a = b ∨ a ∈ s := quot.induction_on s $ Ξ» l, iff.rfl lemma mem_cons_of_mem {a b : Ξ±} {s : multiset Ξ±} (h : a ∈ s) : a ∈ b :: s := mem_cons.2 $ or.inr h @[simp] theorem mem_cons_self (a : Ξ±) (s : multiset Ξ±) : a ∈ a :: s := mem_cons.2 (or.inl rfl) theorem exists_cons_of_mem {s : multiset Ξ±} {a : Ξ±} : a ∈ s β†’ βˆƒ t, s = a :: t := quot.induction_on s $ Ξ» l (h : a ∈ l), let ⟨l₁, lβ‚‚, e⟩ := mem_split h in e.symm β–Έ ⟨(l₁++lβ‚‚ : list Ξ±), quot.sound perm_middle⟩ @[simp] theorem not_mem_zero (a : Ξ±) : a βˆ‰ (0 : multiset Ξ±) := id theorem eq_zero_of_forall_not_mem {s : multiset Ξ±} : (βˆ€x, x βˆ‰ s) β†’ s = 0 := quot.induction_on s $ Ξ» l H, by rw eq_nil_of_forall_not_mem H; refl theorem exists_mem_of_ne_zero {s : multiset Ξ±} : s β‰  0 β†’ βˆƒ a : Ξ±, a ∈ s := quot.induction_on s $ assume l hl, match l, hl with | [] := assume h, false.elim $ h rfl | (a :: l) := assume _, ⟨a, by simp⟩ end end mem /- subset -/ section subset /-- `s βŠ† t` is the lift of the list subset relation. It means that any element with nonzero multiplicity in `s` has nonzero multiplicity in `t`, but it does not imply that the multiplicity of `a` in `s` is less or equal than in `t`; see `s ≀ t` for this relation. -/ protected def subset (s t : multiset Ξ±) : Prop := βˆ€ ⦃a : α⦄, a ∈ s β†’ a ∈ t instance : has_subset (multiset Ξ±) := ⟨multiset.subset⟩ @[simp] theorem coe_subset {l₁ lβ‚‚ : list Ξ±} : (l₁ : multiset Ξ±) βŠ† lβ‚‚ ↔ l₁ βŠ† lβ‚‚ := iff.rfl @[simp] theorem subset.refl (s : multiset Ξ±) : s βŠ† s := Ξ» a h, h theorem subset.trans {s t u : multiset Ξ±} : s βŠ† t β†’ t βŠ† u β†’ s βŠ† u := Ξ» h₁ hβ‚‚ a m, hβ‚‚ (h₁ m) theorem subset_iff {s t : multiset Ξ±} : s βŠ† t ↔ (βˆ€β¦ƒx⦄, x ∈ s β†’ x ∈ t) := iff.rfl theorem mem_of_subset {s t : multiset Ξ±} {a : Ξ±} (h : s βŠ† t) : a ∈ s β†’ a ∈ t := @h _ @[simp] theorem zero_subset (s : multiset Ξ±) : 0 βŠ† s := Ξ» a, (not_mem_nil a).elim @[simp] theorem cons_subset {a : Ξ±} {s t : multiset Ξ±} : (a :: s) βŠ† t ↔ a ∈ t ∧ s βŠ† t := by simp [subset_iff, or_imp_distrib, forall_and_distrib] theorem eq_zero_of_subset_zero {s : multiset Ξ±} (h : s βŠ† 0) : s = 0 := eq_zero_of_forall_not_mem h theorem subset_zero {s : multiset Ξ±} : s βŠ† 0 ↔ s = 0 := ⟨eq_zero_of_subset_zero, Ξ» xeq, xeq.symm β–Έ subset.refl 0⟩ end subset /- multiset order -/ /-- `s ≀ t` means that `s` is a sublist of `t` (up to permutation). Equivalently, `s ≀ t` means that `count a s ≀ count a t` for all `a`. -/ protected def le (s t : multiset Ξ±) : Prop := quotient.lift_onβ‚‚ s t (<+~) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, propext (pβ‚‚.subperm_left.trans p₁.subperm_right) instance : partial_order (multiset Ξ±) := { le := multiset.le, le_refl := Ξ» s, quot.induction_on s $ Ξ» l, subperm.refl _, le_trans := Ξ» s t u, quotient.induction_on₃ s t u $ @subperm.trans _, le_antisymm := Ξ» s t, quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h₁ hβ‚‚, quot.sound (subperm.antisymm h₁ hβ‚‚) } theorem subset_of_le {s t : multiset Ξ±} : s ≀ t β†’ s βŠ† t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, subset_of_subperm theorem mem_of_le {s t : multiset Ξ±} {a : Ξ±} (h : s ≀ t) : a ∈ s β†’ a ∈ t := mem_of_subset (subset_of_le h) @[simp] theorem coe_le {l₁ lβ‚‚ : list Ξ±} : (l₁ : multiset Ξ±) ≀ lβ‚‚ ↔ l₁ <+~ lβ‚‚ := iff.rfl @[elab_as_eliminator] theorem le_induction_on {C : multiset Ξ± β†’ multiset Ξ± β†’ Prop} {s t : multiset Ξ±} (h : s ≀ t) (H : βˆ€ {l₁ lβ‚‚ : list Ξ±}, l₁ <+ lβ‚‚ β†’ C l₁ lβ‚‚) : C s t := quotient.induction_onβ‚‚ s t (Ξ» l₁ lβ‚‚ ⟨l, p, s⟩, (show ⟦l⟧ = ⟦lβ‚βŸ§, from quot.sound p) β–Έ H s) h theorem zero_le (s : multiset Ξ±) : 0 ≀ s := quot.induction_on s $ Ξ» l, subperm_of_sublist $ nil_sublist l theorem le_zero {s : multiset Ξ±} : s ≀ 0 ↔ s = 0 := ⟨λ h, le_antisymm h (zero_le _), le_of_eq⟩ theorem lt_cons_self (s : multiset Ξ±) (a : Ξ±) : s < a :: s := quot.induction_on s $ Ξ» l, suffices l <+~ a :: l ∧ (Β¬l ~ a :: l), by simpa [lt_iff_le_and_ne], ⟨subperm_of_sublist (sublist_cons _ _), Ξ» p, ne_of_lt (lt_succ_self (length l)) (perm_length p)⟩ theorem le_cons_self (s : multiset Ξ±) (a : Ξ±) : s ≀ a :: s := le_of_lt $ lt_cons_self _ _ theorem cons_le_cons_iff (a : Ξ±) {s t : multiset Ξ±} : a :: s ≀ a :: t ↔ s ≀ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, subperm_cons a theorem cons_le_cons (a : Ξ±) {s t : multiset Ξ±} : s ≀ t β†’ a :: s ≀ a :: t := (cons_le_cons_iff a).2 theorem le_cons_of_not_mem {a : Ξ±} {s t : multiset Ξ±} (m : a βˆ‰ s) : s ≀ a :: t ↔ s ≀ t := begin refine ⟨_, Ξ» h, le_trans h $ le_cons_self _ _⟩, suffices : βˆ€ {t'} (_ : s ≀ t') (_ : a ∈ t'), a :: s ≀ t', { exact Ξ» h, (cons_le_cons_iff a).1 (this h (mem_cons_self _ _)) }, introv h, revert m, refine le_induction_on h _, introv s m₁ mβ‚‚, rcases mem_split mβ‚‚ with ⟨r₁, rβ‚‚, rfl⟩, exact perm_middle.subperm_left.2 ((subperm_cons _).2 $ subperm_of_sublist $ (sublist_or_mem_of_sublist s).resolve_right m₁) end /- cardinality -/ /-- The cardinality of a multiset is the sum of the multiplicities of all its elements, or simply the length of the underlying list. -/ def card (s : multiset Ξ±) : β„• := quot.lift_on s length $ Ξ» l₁ lβ‚‚, perm_length @[simp] theorem coe_card (l : list Ξ±) : card (l : multiset Ξ±) = length l := rfl @[simp] theorem card_zero : @card Ξ± 0 = 0 := rfl @[simp] theorem card_cons (a : Ξ±) (s : multiset Ξ±) : card (a :: s) = card s + 1 := quot.induction_on s $ Ξ» l, rfl @[simp] theorem card_singleton (a : Ξ±) : card ({a} : multiset Ξ±) = 1 := calc card ({a} : multiset Ξ±) = card (0 : multiset Ξ±) + 1 : multiset.card_cons a 0 ... = 1 : by simp theorem card_le_of_le {s t : multiset Ξ±} (h : s ≀ t) : card s ≀ card t := le_induction_on h $ Ξ» l₁ lβ‚‚, length_le_of_sublist theorem eq_of_le_of_card_le {s t : multiset Ξ±} (h : s ≀ t) : card t ≀ card s β†’ s = t := le_induction_on h $ Ξ» l₁ lβ‚‚ s hβ‚‚, congr_arg coe $ eq_of_sublist_of_length_le s hβ‚‚ theorem card_lt_of_lt {s t : multiset Ξ±} (h : s < t) : card s < card t := lt_of_not_ge $ Ξ» hβ‚‚, ne_of_lt h $ eq_of_le_of_card_le (le_of_lt h) hβ‚‚ theorem lt_iff_cons_le {s t : multiset Ξ±} : s < t ↔ βˆƒ a, a :: s ≀ t := ⟨quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, subperm.exists_of_length_lt (le_of_lt h) (card_lt_of_lt h), Ξ» ⟨a, h⟩, lt_of_lt_of_le (lt_cons_self _ _) h⟩ @[simp] theorem card_eq_zero {s : multiset Ξ±} : card s = 0 ↔ s = 0 := ⟨λ h, (eq_of_le_of_card_le (zero_le _) (le_of_eq h)).symm, Ξ» e, by simp [e]⟩ theorem card_pos {s : multiset Ξ±} : 0 < card s ↔ s β‰  0 := pos_iff_ne_zero.trans $ not_congr card_eq_zero theorem card_pos_iff_exists_mem {s : multiset Ξ±} : 0 < card s ↔ βˆƒ a, a ∈ s := quot.induction_on s $ Ξ» l, length_pos_iff_exists_mem @[elab_as_eliminator] lemma strong_induction_on {p : multiset Ξ± β†’ Sort*} : βˆ€ (s : multiset Ξ±), (βˆ€ s, (βˆ€t < s, p t) β†’ p s) β†’ p s | s := Ξ» ih, ih s $ Ξ» t h, have card t < card s, from card_lt_of_lt h, strong_induction_on t ih using_well_founded {rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf card⟩]} @[elab_as_eliminator] lemma case_strong_induction_on {p : multiset Ξ± β†’ Prop} (s : multiset Ξ±) (hβ‚€ : p 0) (h₁ : βˆ€ a s, (βˆ€t ≀ s, p t) β†’ p (a :: s)) : p s := multiset.strong_induction_on s $ assume s, multiset.induction_on s (Ξ» _, hβ‚€) $ Ξ» a s _ ih, h₁ _ _ $ Ξ» t h, ih _ $ lt_of_le_of_lt h $ lt_cons_self _ _ /- singleton -/ @[simp] theorem mem_singleton {a b : Ξ±} : b ∈ a::0 ↔ b = a := by simp theorem mem_singleton_self (a : Ξ±) : a ∈ (a::0 : multiset Ξ±) := mem_cons_self _ _ theorem singleton_inj {a b : Ξ±} : a::0 = b::0 ↔ a = b := cons_inj_left _ @[simp] theorem singleton_ne_zero (a : Ξ±) : a::0 β‰  0 := ne_of_gt (lt_cons_self _ _) @[simp] theorem singleton_le {a : Ξ±} {s : multiset Ξ±} : a::0 ≀ s ↔ a ∈ s := ⟨λ h, mem_of_le h (mem_singleton_self _), Ξ» h, let ⟨t, e⟩ := exists_cons_of_mem h in e.symm β–Έ cons_le_cons _ (zero_le _)⟩ /- add -/ /-- The sum of two multisets is the lift of the list append operation. This adds the multiplicities of each element, i.e. `count a (s + t) = count a s + count a t`. -/ protected def add (s₁ sβ‚‚ : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s₁ sβ‚‚ (Ξ» l₁ lβ‚‚, ((l₁ ++ lβ‚‚ : list Ξ±) : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ perm_app p₁ pβ‚‚ instance : has_add (multiset Ξ±) := ⟨multiset.add⟩ @[simp] theorem coe_add (s t : list Ξ±) : (s + t : multiset Ξ±) = (s ++ t : list Ξ±) := rfl protected theorem add_comm (s t : multiset Ξ±) : s + t = t + s := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, quot.sound perm_app_comm protected theorem zero_add (s : multiset Ξ±) : 0 + s = s := quot.induction_on s $ Ξ» l, rfl theorem singleton_add (a : Ξ±) (s : multiset Ξ±) : ↑[a] + s = a::s := rfl protected theorem add_le_add_left (s) {t u : multiset Ξ±} : s + t ≀ s + u ↔ t ≀ u := quotient.induction_on₃ s t u $ Ξ» l₁ lβ‚‚ l₃, subperm_app_left _ protected theorem add_left_cancel (s) {t u : multiset Ξ±} (h : s + t = s + u) : t = u := le_antisymm ((multiset.add_le_add_left _).1 (le_of_eq h)) ((multiset.add_le_add_left _).1 (le_of_eq h.symm)) instance : ordered_cancel_comm_monoid (multiset Ξ±) := { zero := 0, add := (+), add_comm := multiset.add_comm, add_assoc := Ξ» s₁ sβ‚‚ s₃, quotient.induction_on₃ s₁ sβ‚‚ s₃ $ Ξ» l₁ lβ‚‚ l₃, congr_arg coe $ append_assoc l₁ lβ‚‚ l₃, zero_add := multiset.zero_add, add_zero := Ξ» s, by rw [multiset.add_comm, multiset.zero_add], add_left_cancel := multiset.add_left_cancel, add_right_cancel := Ξ» s₁ sβ‚‚ s₃ h, multiset.add_left_cancel sβ‚‚ $ by simpa [multiset.add_comm] using h, add_le_add_left := Ξ» s₁ sβ‚‚ h s₃, (multiset.add_le_add_left _).2 h, le_of_add_le_add_left := Ξ» s₁ sβ‚‚ s₃, (multiset.add_le_add_left _).1, ..@multiset.partial_order Ξ± } @[simp] theorem cons_add (a : Ξ±) (s t : multiset Ξ±) : a :: s + t = a :: (s + t) := by rw [← singleton_add, ← singleton_add, add_assoc] @[simp] theorem add_cons (a : Ξ±) (s t : multiset Ξ±) : s + a :: t = a :: (s + t) := by rw [add_comm, cons_add, add_comm] theorem le_add_right (s t : multiset Ξ±) : s ≀ s + t := by simpa using add_le_add_left (zero_le t) s theorem le_add_left (s t : multiset Ξ±) : s ≀ t + s := by simpa using add_le_add_right (zero_le t) s @[simp] theorem card_add (s t : multiset Ξ±) : card (s + t) = card s + card t := quotient.induction_onβ‚‚ s t length_append @[simp] theorem mem_add {a : Ξ±} {s t : multiset Ξ±} : a ∈ s + t ↔ a ∈ s ∨ a ∈ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, mem_append theorem le_iff_exists_add {s t : multiset Ξ±} : s ≀ t ↔ βˆƒ u, t = s + u := ⟨λ h, le_induction_on h $ Ξ» l₁ lβ‚‚ s, let ⟨l, p⟩ := exists_perm_append_of_sublist s in ⟨l, quot.sound p⟩, λ⟨u, e⟩, e.symm β–Έ le_add_right s u⟩ instance : canonically_ordered_monoid (multiset Ξ±) := { lt_of_add_lt_add_left := @lt_of_add_lt_add_left _ _, le_iff_exists_add := @le_iff_exists_add _, ..multiset.ordered_cancel_comm_monoid } /- repeat -/ /-- `repeat a n` is the multiset containing only `a` with multiplicity `n`. -/ def repeat (a : Ξ±) (n : β„•) : multiset Ξ± := repeat a n @[simp] lemma card_repeat : βˆ€ (a : Ξ±) n, card (repeat a n) = n := length_repeat theorem eq_of_mem_repeat {a b : Ξ±} {n} : b ∈ repeat a n β†’ b = a := eq_of_mem_repeat theorem eq_repeat' {a : Ξ±} {s : multiset Ξ±} : s = repeat a s.card ↔ βˆ€ b ∈ s, b = a := quot.induction_on s $ Ξ» l, iff.trans ⟨λ h, (perm_repeat.1 $ (quotient.exact h).symm).symm, congr_arg coe⟩ eq_repeat' theorem eq_repeat_of_mem {a : Ξ±} {s : multiset Ξ±} : (βˆ€ b ∈ s, b = a) β†’ s = repeat a s.card := eq_repeat'.2 theorem eq_repeat {a : Ξ±} {n} {s : multiset Ξ±} : s = repeat a n ↔ card s = n ∧ βˆ€ b ∈ s, b = a := ⟨λ h, h.symm β–Έ ⟨card_repeat _ _, Ξ» b, eq_of_mem_repeat⟩, Ξ» ⟨e, al⟩, e β–Έ eq_repeat_of_mem al⟩ theorem repeat_subset_singleton : βˆ€ (a : Ξ±) n, repeat a n βŠ† a::0 := repeat_subset_singleton theorem repeat_le_coe {a : Ξ±} {n} {l : list Ξ±} : repeat a n ≀ l ↔ list.repeat a n <+ l := ⟨λ ⟨l', p, s⟩, (perm_repeat.1 p.symm).symm β–Έ s, subperm_of_sublist⟩ /- range -/ /-- `range n` is the multiset lifted from the list `range n`, that is, the set `{0, 1, ..., n-1}`. -/ def range (n : β„•) : multiset β„• := range n @[simp] theorem range_zero (n : β„•) : range 0 = 0 := rfl @[simp] theorem range_succ (n : β„•) : range (succ n) = n :: range n := by rw [range, range_concat, ← coe_add, add_comm]; refl @[simp] theorem card_range (n : β„•) : card (range n) = n := length_range _ theorem range_subset {m n : β„•} : range m βŠ† range n ↔ m ≀ n := range_subset @[simp] theorem mem_range {m n : β„•} : m ∈ range n ↔ m < n := mem_range @[simp] theorem not_mem_range_self {n : β„•} : n βˆ‰ range n := not_mem_range_self /- erase -/ section erase variables [decidable_eq Ξ±] {s t : multiset Ξ±} {a b : Ξ±} /-- `erase s a` is the multiset that subtracts 1 from the multiplicity of `a`. -/ def erase (s : multiset Ξ±) (a : Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (l.erase a : multiset Ξ±)) (Ξ» l₁ lβ‚‚ p, quot.sound (erase_perm_erase a p)) @[simp] theorem coe_erase (l : list Ξ±) (a : Ξ±) : erase (l : multiset Ξ±) a = l.erase a := rfl @[simp] theorem erase_zero (a : Ξ±) : (0 : multiset Ξ±).erase a = 0 := rfl @[simp] theorem erase_cons_head (a : Ξ±) (s : multiset Ξ±) : (a :: s).erase a = s := quot.induction_on s $ Ξ» l, congr_arg coe $ erase_cons_head a l @[simp] theorem erase_cons_tail {a b : Ξ±} (s : multiset Ξ±) (h : b β‰  a) : (b::s).erase a = b :: s.erase a := quot.induction_on s $ Ξ» l, congr_arg coe $ erase_cons_tail l h @[simp] theorem erase_of_not_mem {a : Ξ±} {s : multiset Ξ±} : a βˆ‰ s β†’ s.erase a = s := quot.induction_on s $ Ξ» l h, congr_arg coe $ erase_of_not_mem h @[simp] theorem cons_erase {s : multiset Ξ±} {a : Ξ±} : a ∈ s β†’ a :: s.erase a = s := quot.induction_on s $ Ξ» l h, quot.sound (perm_erase h).symm theorem le_cons_erase (s : multiset Ξ±) (a : Ξ±) : s ≀ a :: s.erase a := if h : a ∈ s then le_of_eq (cons_erase h).symm else by rw erase_of_not_mem h; apply le_cons_self @[simp] theorem card_erase_of_mem {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ card (s.erase a) = pred (card s) := quot.induction_on s $ Ξ» l, length_erase_of_mem theorem erase_add_left_pos {a : Ξ±} {s : multiset Ξ±} (t) : a ∈ s β†’ (s + t).erase a = s.erase a + t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ erase_append_left lβ‚‚ h theorem erase_add_right_pos {a : Ξ±} (s) {t : multiset Ξ±} (h : a ∈ t) : (s + t).erase a = s + t.erase a := by rw [add_comm, erase_add_left_pos s h, add_comm] theorem erase_add_right_neg {a : Ξ±} {s : multiset Ξ±} (t) : a βˆ‰ s β†’ (s + t).erase a = s + t.erase a := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ erase_append_right lβ‚‚ h theorem erase_add_left_neg {a : Ξ±} (s) {t : multiset Ξ±} (h : a βˆ‰ t) : (s + t).erase a = s.erase a + t := by rw [add_comm, erase_add_right_neg s h, add_comm] theorem erase_le (a : Ξ±) (s : multiset Ξ±) : s.erase a ≀ s := quot.induction_on s $ Ξ» l, subperm_of_sublist (erase_sublist a l) @[simp] theorem erase_lt {a : Ξ±} {s : multiset Ξ±} : s.erase a < s ↔ a ∈ s := ⟨λ h, not_imp_comm.1 erase_of_not_mem (ne_of_lt h), Ξ» h, by simpa [h] using lt_cons_self (s.erase a) a⟩ theorem erase_subset (a : Ξ±) (s : multiset Ξ±) : s.erase a βŠ† s := subset_of_le (erase_le a s) theorem mem_erase_of_ne {a b : Ξ±} {s : multiset Ξ±} (ab : a β‰  b) : a ∈ s.erase b ↔ a ∈ s := quot.induction_on s $ Ξ» l, list.mem_erase_of_ne ab theorem mem_of_mem_erase {a b : Ξ±} {s : multiset Ξ±} : a ∈ s.erase b β†’ a ∈ s := mem_of_subset (erase_subset _ _) theorem erase_comm (s : multiset Ξ±) (a b : Ξ±) : (s.erase a).erase b = (s.erase b).erase a := quot.induction_on s $ Ξ» l, congr_arg coe $ l.erase_comm a b theorem erase_le_erase {s t : multiset Ξ±} (a : Ξ±) (h : s ≀ t) : s.erase a ≀ t.erase a := le_induction_on h $ Ξ» l₁ lβ‚‚ h, subperm_of_sublist (erase_sublist_erase _ h) theorem erase_le_iff_le_cons {s t : multiset Ξ±} {a : Ξ±} : s.erase a ≀ t ↔ s ≀ a :: t := ⟨λ h, le_trans (le_cons_erase _ _) (cons_le_cons _ h), Ξ» h, if m : a ∈ s then by rw ← cons_erase m at h; exact (cons_le_cons_iff _).1 h else le_trans (erase_le _ _) ((le_cons_of_not_mem m).1 h)⟩ end erase @[simp] theorem coe_reverse (l : list Ξ±) : (reverse l : multiset Ξ±) = l := quot.sound $ reverse_perm _ /- map -/ /-- `map f s` is the lift of the list `map` operation. The multiplicity of `b` in `map f s` is the number of `a ∈ s` (counting multiplicity) such that `f a = b`. -/ def map (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : multiset Ξ² := quot.lift_on s (Ξ» l : list Ξ±, (l.map f : multiset Ξ²)) (Ξ» l₁ lβ‚‚ p, quot.sound (perm_map f p)) @[simp] theorem coe_map (f : Ξ± β†’ Ξ²) (l : list Ξ±) : map f ↑l = l.map f := rfl @[simp] theorem map_zero (f : Ξ± β†’ Ξ²) : map f 0 = 0 := rfl @[simp] theorem map_cons (f : Ξ± β†’ Ξ²) (a s) : map f (a::s) = f a :: map f s := quot.induction_on s $ Ξ» l, rfl @[simp] lemma map_singleton (f : Ξ± β†’ Ξ²) (a : Ξ±) : ({a} : multiset Ξ±).map f = {f a} := rfl @[simp] theorem map_add (f : Ξ± β†’ Ξ²) (s t) : map f (s + t) = map f s + map f t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ map_append _ _ _ @[simp] theorem mem_map {f : Ξ± β†’ Ξ²} {b : Ξ²} {s : multiset Ξ±} : b ∈ map f s ↔ βˆƒ a, a ∈ s ∧ f a = b := quot.induction_on s $ Ξ» l, mem_map @[simp] theorem card_map (f : Ξ± β†’ Ξ²) (s) : card (map f s) = card s := quot.induction_on s $ Ξ» l, length_map _ _ theorem mem_map_of_mem (f : Ξ± β†’ Ξ²) {a : Ξ±} {s : multiset Ξ±} (h : a ∈ s) : f a ∈ map f s := mem_map.2 ⟨_, h, rfl⟩ @[simp] theorem mem_map_of_inj {f : Ξ± β†’ Ξ²} (H : function.injective f) {a : Ξ±} {s : multiset Ξ±} : f a ∈ map f s ↔ a ∈ s := quot.induction_on s $ Ξ» l, mem_map_of_inj H @[simp] theorem map_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : map g (map f s) = map (g ∘ f) s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_map _ _ _ @[simp] theorem map_id (s : multiset Ξ±) : map id s = s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_id _ @[simp] theorem map_const (s : multiset Ξ±) (b : Ξ²) : map (function.const Ξ± b) s = repeat b s.card := quot.induction_on s $ Ξ» l, congr_arg coe $ map_const _ _ @[congr] theorem map_congr {f g : Ξ± β†’ Ξ²} {s : multiset Ξ±} : (βˆ€ x ∈ s, f x = g x) β†’ map f s = map g s := quot.induction_on s $ Ξ» l H, congr_arg coe $ map_congr H lemma map_hcongr {Ξ²' : Type*} {m : multiset Ξ±} {f : Ξ± β†’ Ξ²} {f' : Ξ± β†’ Ξ²'} (h : Ξ² = Ξ²') (hf : βˆ€a∈m, f a == f' a) : map f m == map f' m := begin subst h, simp at hf, simp [map_congr hf] end theorem eq_of_mem_map_const {b₁ bβ‚‚ : Ξ²} {l : list Ξ±} (h : b₁ ∈ map (function.const Ξ± bβ‚‚) l) : b₁ = bβ‚‚ := eq_of_mem_repeat $ by rwa map_const at h @[simp] theorem map_le_map {f : Ξ± β†’ Ξ²} {s t : multiset Ξ±} (h : s ≀ t) : map f s ≀ map f t := le_induction_on h $ Ξ» l₁ lβ‚‚ h, subperm_of_sublist $ map_sublist_map f h @[simp] theorem map_subset_map {f : Ξ± β†’ Ξ²} {s t : multiset Ξ±} (H : s βŠ† t) : map f s βŠ† map f t := Ξ» b m, let ⟨a, h, e⟩ := mem_map.1 m in mem_map.2 ⟨a, H h, e⟩ /- fold -/ /-- `foldl f H b s` is the lift of the list operation `foldl f b l`, which folds `f` over the multiset. It is well defined when `f` is right-commutative, that is, `f (f b a₁) aβ‚‚ = f (f b aβ‚‚) a₁`. -/ def foldl (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (b : Ξ²) (s : multiset Ξ±) : Ξ² := quot.lift_on s (Ξ» l, foldl f b l) (Ξ» l₁ lβ‚‚ p, foldl_eq_of_perm H p b) @[simp] theorem foldl_zero (f : Ξ² β†’ Ξ± β†’ Ξ²) (H b) : foldl f H b 0 = b := rfl @[simp] theorem foldl_cons (f : Ξ² β†’ Ξ± β†’ Ξ²) (H b a s) : foldl f H b (a :: s) = foldl f H (f b a) s := quot.induction_on s $ Ξ» l, rfl @[simp] theorem foldl_add (f : Ξ² β†’ Ξ± β†’ Ξ²) (H b s t) : foldl f H b (s + t) = foldl f H (foldl f H b s) t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, foldl_append _ _ _ _ /-- `foldr f H b s` is the lift of the list operation `foldr f b l`, which folds `f` over the multiset. It is well defined when `f` is left-commutative, that is, `f a₁ (f aβ‚‚ b) = f aβ‚‚ (f a₁ b)`. -/ def foldr (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (s : multiset Ξ±) : Ξ² := quot.lift_on s (Ξ» l, foldr f b l) (Ξ» l₁ lβ‚‚ p, foldr_eq_of_perm H p b) @[simp] theorem foldr_zero (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b) : foldr f H b 0 = b := rfl @[simp] theorem foldr_cons (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b a s) : foldr f H b (a :: s) = f a (foldr f H b s) := quot.induction_on s $ Ξ» l, rfl @[simp] theorem foldr_add (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b s t) : foldr f H b (s + t) = foldr f H (foldr f H b t) s := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, foldr_append _ _ _ _ @[simp] theorem coe_foldr (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (l : list Ξ±) : foldr f H b l = l.foldr f b := rfl @[simp] theorem coe_foldl (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (b : Ξ²) (l : list Ξ±) : foldl f H b l = l.foldl f b := rfl theorem coe_foldr_swap (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (l : list Ξ±) : foldr f H b l = l.foldl (Ξ» x y, f y x) b := (congr_arg (foldr f H b) (coe_reverse l)).symm.trans $ foldr_reverse _ _ _ theorem foldr_swap (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (s : multiset Ξ±) : foldr f H b s = foldl (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) b s := quot.induction_on s $ Ξ» l, coe_foldr_swap _ _ _ _ theorem foldl_swap (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (b : Ξ²) (s : multiset Ξ±) : foldl f H b s = foldr (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) b s := (foldr_swap _ _ _ _).symm /-- Product of a multiset given a commutative monoid structure on `Ξ±`. `prod {a, b, c} = a * b * c` -/ def prod [comm_monoid Ξ±] : multiset Ξ± β†’ Ξ± := foldr (*) (Ξ» x y z, by simp [mul_left_comm]) 1 attribute [to_additive multiset.sum._proof_1] prod._proof_1 attribute [to_additive multiset.sum] prod @[to_additive multiset.sum_eq_foldr] theorem prod_eq_foldr [comm_monoid Ξ±] (s : multiset Ξ±) : prod s = foldr (*) (Ξ» x y z, by simp [mul_left_comm]) 1 s := rfl @[to_additive multiset.sum_eq_foldl] theorem prod_eq_foldl [comm_monoid Ξ±] (s : multiset Ξ±) : prod s = foldl (*) (Ξ» x y z, by simp [mul_right_comm]) 1 s := (foldr_swap _ _ _ _).trans (by simp [mul_comm]) @[simp, to_additive multiset.coe_sum] theorem coe_prod [comm_monoid Ξ±] (l : list Ξ±) : prod ↑l = l.prod := prod_eq_foldl _ @[simp, to_additive multiset.sum_zero] theorem prod_zero [comm_monoid Ξ±] : @prod Ξ± _ 0 = 1 := rfl @[simp, to_additive multiset.sum_cons] theorem prod_cons [comm_monoid Ξ±] (a : Ξ±) (s) : prod (a :: s) = a * prod s := foldr_cons _ _ _ _ _ @[simp, to_additive multiset.sum_add] theorem prod_add [comm_monoid Ξ±] (s t : multiset Ξ±) : prod (s + t) = prod s * prod t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, by simp @[simp] theorem prod_repeat [comm_monoid Ξ±] (a : Ξ±) (n : β„•) : prod (multiset.repeat a n) = a ^ n := by simp [repeat, list.prod_repeat] @[simp] theorem sum_repeat [add_comm_monoid Ξ±] : βˆ€ (a : Ξ±) (n : β„•), sum (multiset.repeat a n) = n β€’ a := @prod_repeat (multiplicative Ξ±) _ attribute [to_additive multiset.sum_repeat] prod_repeat @[simp, to_additive multiset.sum_map_add] lemma prod_map_mul [comm_monoid Ξ³] {m : multiset Ξ±} {f g : Ξ± β†’ Ξ³} : prod (m.map $ Ξ»a, f a * g a) = prod (m.map f) * prod (m.map g) := multiset.induction_on m (by simp) (assume a m ih, by simp [ih]; cc) lemma prod_map_prod_map [comm_monoid Ξ³] (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ Ξ³} : prod (m.map $ Ξ»a, prod $ n.map $ Ξ»b, f a b) = prod (n.map $ Ξ»b, prod $ m.map $ Ξ»a, f a b) := multiset.induction_on m (by simp) (assume a m ih, by simp [ih]) lemma sum_map_sum_map [add_comm_monoid Ξ³] : βˆ€ (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ Ξ³}, sum (m.map $ Ξ»a, sum $ n.map $ Ξ»b, f a b) = sum (n.map $ Ξ»b, sum $ m.map $ Ξ»a, f a b) := @prod_map_prod_map _ _ (multiplicative Ξ³) _ attribute [to_additive multiset.sum_map_sum_map] prod_map_prod_map /- join -/ /-- `join S`, where `S` is a multiset of multisets, is the lift of the list join operation, that is, the union of all the sets. join {{1, 2}, {1, 2}, {0, 1}} = {0, 1, 1, 1, 2, 2} -/ def join : multiset (multiset Ξ±) β†’ multiset Ξ± := sum theorem coe_join : βˆ€ L : list (list Ξ±), join (L.map (@coe _ (multiset Ξ±) _) : multiset (multiset Ξ±)) = L.join | [] := rfl | (l :: L) := congr_arg (Ξ» s : multiset Ξ±, ↑l + s) (coe_join L) @[simp] theorem join_zero : @join Ξ± 0 = 0 := rfl @[simp] theorem join_cons (s S) : @join Ξ± (s :: S) = s + join S := sum_cons _ _ @[simp] theorem join_add (S T) : @join Ξ± (S + T) = join S + join T := sum_add _ _ @[simp] theorem mem_join {a S} : a ∈ @join Ξ± S ↔ βˆƒ s ∈ S, a ∈ s := multiset.induction_on S (by simp) $ by simp [or_and_distrib_right, exists_or_distrib] {contextual := tt} @[simp] theorem card_join (S) : card (@join Ξ± S) = sum (map card S) := multiset.induction_on S (by simp) (by simp) /- bind -/ /-- `bind s f` is the monad bind operation, defined as `join (map f s)`. It is the union of `f a` as `a` ranges over `s`. -/ def bind (s : multiset Ξ±) (f : Ξ± β†’ multiset Ξ²) : multiset Ξ² := join (map f s) @[simp] theorem coe_bind (l : list Ξ±) (f : Ξ± β†’ list Ξ²) : @bind Ξ± Ξ² l (Ξ» a, f a) = l.bind f := by rw [list.bind, ← coe_join, list.map_map]; refl @[simp] theorem zero_bind (f : Ξ± β†’ multiset Ξ²) : bind 0 f = 0 := rfl @[simp] theorem cons_bind (a s) (f : Ξ± β†’ multiset Ξ²) : bind (a::s) f = f a + bind s f := by simp [bind] @[simp] theorem add_bind (s t) (f : Ξ± β†’ multiset Ξ²) : bind (s + t) f = bind s f + bind t f := by simp [bind] @[simp] theorem mem_bind {b s} {f : Ξ± β†’ multiset Ξ²} : b ∈ bind s f ↔ βˆƒ a ∈ s, b ∈ f a := by simp [bind]; simp [-exists_and_distrib_right, exists_and_distrib_right.symm]; rw exists_swap; simp [and_assoc] @[simp] theorem card_bind (s) (f : Ξ± β†’ multiset Ξ²) : card (bind s f) = sum (map (card ∘ f) s) := by simp [bind] lemma bind_congr {f g : Ξ± β†’ multiset Ξ²} {m : multiset Ξ±} : (βˆ€a∈m, f a = g a) β†’ bind m f = bind m g := by simp [bind] {contextual := tt} lemma bind_hcongr {Ξ²' : Type*} {m : multiset Ξ±} {f : Ξ± β†’ multiset Ξ²} {f' : Ξ± β†’ multiset Ξ²'} (h : Ξ² = Ξ²') (hf : βˆ€a∈m, f a == f' a) : bind m f == bind m f' := begin subst h, simp at hf, simp [bind_congr hf] end lemma map_bind [decidable_eq Ξ³] (m : multiset Ξ±) (n : Ξ± β†’ multiset Ξ²) (f : Ξ² β†’ Ξ³) : map f (bind m n) = bind m (Ξ»a, map f (n a)) := multiset.induction_on m (by simp) (by simp {contextual := tt}) @[simp, to_additive multiset.sum_bind] lemma prod_bind [comm_monoid Ξ²] (s : multiset Ξ±) (t : Ξ± β†’ multiset Ξ²) : prod (bind s t) = prod (s.map $ Ξ»a, prod (t a)) := multiset.induction_on s (by simp) (assume a s ih, by simp [ih, cons_bind]) /- product -/ /-- The multiplicity of `(a, b)` in `product s t` is the product of the multiplicity of `a` in `s` and `b` in `t`. -/ def product (s : multiset Ξ±) (t : multiset Ξ²) : multiset (Ξ± Γ— Ξ²) := s.bind $ Ξ» a, t.map $ prod.mk a @[simp] theorem coe_product (l₁ : list Ξ±) (lβ‚‚ : list Ξ²) : @product Ξ± Ξ² l₁ lβ‚‚ = l₁.product lβ‚‚ := by rw [product, list.product, ← coe_bind]; simp @[simp] theorem zero_product (t) : @product Ξ± Ξ² 0 t = 0 := rfl @[simp] theorem cons_product (a : Ξ±) (s : multiset Ξ±) (t : multiset Ξ²) : product (a :: s) t = map (prod.mk a) t + product s t := by simp [product] @[simp] theorem product_singleton (a : Ξ±) (b : Ξ²) : product (a::0) (b::0) = (a,b)::0 := rfl @[simp] theorem add_product (s t : multiset Ξ±) (u : multiset Ξ²) : product (s + t) u = product s u + product t u := by simp [product] @[simp] theorem product_add (s : multiset Ξ±) : βˆ€ t u : multiset Ξ², product s (t + u) = product s t + product s u := multiset.induction_on s (Ξ» t u, rfl) $ Ξ» a s IH t u, by rw [cons_product, IH]; simp @[simp] theorem mem_product {s t} : βˆ€ {p : Ξ± Γ— Ξ²}, p ∈ @product Ξ± Ξ² s t ↔ p.1 ∈ s ∧ p.2 ∈ t | (a, b) := by simp [product, and.left_comm] @[simp] theorem card_product (s : multiset Ξ±) (t : multiset Ξ²) : card (product s t) = card s * card t := by simp [product, repeat, (∘), mul_comm] /- sigma -/ section variable {Οƒ : Ξ± β†’ Type*} /-- `sigma s t` is the dependent version of `product`. It is the sum of `(a, b)` as `a` ranges over `s` and `b` ranges over `t a`. -/ protected def sigma (s : multiset Ξ±) (t : Ξ  a, multiset (Οƒ a)) : multiset (Ξ£ a, Οƒ a) := s.bind $ Ξ» a, (t a).map $ sigma.mk a @[simp] theorem coe_sigma (l₁ : list Ξ±) (lβ‚‚ : Ξ  a, list (Οƒ a)) : @multiset.sigma Ξ± Οƒ l₁ (Ξ» a, lβ‚‚ a) = l₁.sigma lβ‚‚ := by rw [multiset.sigma, list.sigma, ← coe_bind]; simp @[simp] theorem zero_sigma (t) : @multiset.sigma Ξ± Οƒ 0 t = 0 := rfl @[simp] theorem cons_sigma (a : Ξ±) (s : multiset Ξ±) (t : Ξ  a, multiset (Οƒ a)) : (a :: s).sigma t = map (sigma.mk a) (t a) + s.sigma t := by simp [multiset.sigma] @[simp] theorem sigma_singleton (a : Ξ±) (b : Ξ± β†’ Ξ²) : (a::0).sigma (Ξ» a, b a::0) = ⟨a, b a⟩::0 := rfl @[simp] theorem add_sigma (s t : multiset Ξ±) (u : Ξ  a, multiset (Οƒ a)) : (s + t).sigma u = s.sigma u + t.sigma u := by simp [multiset.sigma] @[simp] theorem sigma_add (s : multiset Ξ±) : βˆ€ t u : Ξ  a, multiset (Οƒ a), s.sigma (Ξ» a, t a + u a) = s.sigma t + s.sigma u := multiset.induction_on s (Ξ» t u, rfl) $ Ξ» a s IH t u, by rw [cons_sigma, IH]; simp @[simp] theorem mem_sigma {s t} : βˆ€ {p : Ξ£ a, Οƒ a}, p ∈ @multiset.sigma Ξ± Οƒ s t ↔ p.1 ∈ s ∧ p.2 ∈ t p.1 | ⟨a, b⟩ := by simp [multiset.sigma, and_assoc, and.left_comm] @[simp] theorem card_sigma (s : multiset Ξ±) (t : Ξ  a, multiset (Οƒ a)) : card (s.sigma t) = sum (map (Ξ» a, card (t a)) s) := by simp [multiset.sigma, (∘)] end /- map for partial functions -/ /-- Lift of the list `pmap` operation. Map a partial function `f` over a multiset `s` whose elements are all in the domain of `f`. -/ def pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (s : multiset Ξ±) : (βˆ€ a ∈ s, p a) β†’ multiset Ξ² := quot.rec_on s (Ξ» l H, ↑(pmap f l H)) $ Ξ» l₁ lβ‚‚ (pp : l₁ ~ lβ‚‚), funext $ Ξ» (Hβ‚‚ : βˆ€ a ∈ lβ‚‚, p a), have H₁ : βˆ€ a ∈ l₁, p a, from Ξ» a h, Hβ‚‚ a ((mem_of_perm pp).1 h), have βˆ€ {sβ‚‚ e H}, @eq.rec (multiset Ξ±) l₁ (Ξ» s, (βˆ€ a ∈ s, p a) β†’ multiset Ξ²) (Ξ» _, ↑(pmap f l₁ H₁)) sβ‚‚ e H = ↑(pmap f l₁ H₁), by intros sβ‚‚ e _; subst e, this.trans $ quot.sound $ perm_pmap f pp @[simp] theorem coe_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (l : list Ξ±) (H : βˆ€ a ∈ l, p a) : pmap f l H = l.pmap f H := rfl @[simp] lemma pmap_zero {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (h : βˆ€a∈(0:multiset Ξ±), p a) : pmap f 0 h = 0 := rfl @[simp] lemma pmap_cons {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (a : Ξ±) (m : multiset Ξ±) : βˆ€(h : βˆ€b∈a::m, p b), pmap f (a :: m) h = f a (h a (mem_cons_self a m)) :: pmap f m (Ξ»a ha, h a $ mem_cons_of_mem ha) := quotient.induction_on m $ assume l h, rfl /-- "Attach" a proof that `a ∈ s` to each element `a` in `s` to produce a multiset on `{x // x ∈ s}`. -/ def attach (s : multiset Ξ±) : multiset {x // x ∈ s} := pmap subtype.mk s (Ξ» a, id) @[simp] theorem coe_attach (l : list Ξ±) : @eq (multiset {x // x ∈ l}) (@attach Ξ± l) l.attach := rfl theorem pmap_eq_map (p : Ξ± β†’ Prop) (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : βˆ€ H, @pmap _ _ p (Ξ» a _, f a) s H = map f s := quot.induction_on s $ Ξ» l H, congr_arg coe $ pmap_eq_map p f l H theorem pmap_congr {p q : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {g : Ξ  a, q a β†’ Ξ²} (s : multiset Ξ±) {H₁ Hβ‚‚} (h : βˆ€ a h₁ hβ‚‚, f a h₁ = g a hβ‚‚) : pmap f s H₁ = pmap g s Hβ‚‚ := quot.induction_on s (Ξ» l H₁ Hβ‚‚, congr_arg coe $ pmap_congr l h) H₁ Hβ‚‚ theorem map_pmap {p : Ξ± β†’ Prop} (g : Ξ² β†’ Ξ³) (f : Ξ  a, p a β†’ Ξ²) (s) : βˆ€ H, map g (pmap f s H) = pmap (Ξ» a h, g (f a h)) s H := quot.induction_on s $ Ξ» l H, congr_arg coe $ map_pmap g f l H theorem pmap_eq_map_attach {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (s) : βˆ€ H, pmap f s H = s.attach.map (Ξ» x, f x.1 (H _ x.2)) := quot.induction_on s $ Ξ» l H, congr_arg coe $ pmap_eq_map_attach f l H theorem attach_map_val (s : multiset Ξ±) : s.attach.map subtype.val = s := quot.induction_on s $ Ξ» l, congr_arg coe $ attach_map_val l @[simp] theorem mem_attach (s : multiset Ξ±) : βˆ€ x, x ∈ s.attach := quot.induction_on s $ Ξ» l, mem_attach _ @[simp] theorem mem_pmap {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {s H b} : b ∈ pmap f s H ↔ βˆƒ a (h : a ∈ s), f a (H a h) = b := quot.induction_on s (Ξ» l H, mem_pmap) H @[simp] theorem card_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (s H) : card (pmap f s H) = card s := quot.induction_on s (Ξ» l H, length_pmap) H @[simp] theorem card_attach {m : multiset Ξ±} : card (attach m) = card m := card_pmap _ _ _ @[simp] lemma attach_zero : (0 : multiset Ξ±).attach = 0 := rfl lemma attach_cons (a : Ξ±) (m : multiset Ξ±) : (a :: m).attach = ⟨a, mem_cons_self a m⟩ :: (m.attach.map $ Ξ»p, ⟨p.1, mem_cons_of_mem p.2⟩) := quotient.induction_on m $ assume l, congr_arg coe $ congr_arg (list.cons _) $ by rw [list.map_pmap]; exact list.pmap_congr _ (assume a' h₁ hβ‚‚, subtype.eq rfl) section decidable_pi_multiset variables {m : multiset Ξ±} protected def decidable_forall_multiset {p : Ξ± β†’ Prop} [hp : βˆ€a, decidable (p a)] : decidable (βˆ€a∈m, p a) := quotient.rec_on_subsingleton m (Ξ»l, decidable_of_iff (βˆ€a∈l, p a) $ by simp) instance decidable_dforall_multiset {p : Ξ a∈m, Prop} [hp : βˆ€a (h : a ∈ m), decidable (p a h)] : decidable (βˆ€a (h : a ∈ m), p a h) := decidable_of_decidable_of_iff (@multiset.decidable_forall_multiset {a // a ∈ m} m.attach (Ξ»a, p a.1 a.2) _) (iff.intro (assume h a ha, h ⟨a, ha⟩ (mem_attach _ _)) (assume h ⟨a, ha⟩ _, h _ _)) /-- decidable equality for functions whose domain is bounded by multisets -/ instance decidable_eq_pi_multiset {Ξ² : Ξ± β†’ Type*} [h : βˆ€a, decidable_eq (Ξ² a)] : decidable_eq (Ξ a∈m, Ξ² a) := assume f g, decidable_of_iff (βˆ€a (h : a ∈ m), f a h = g a h) (by simp [function.funext_iff]) end decidable_pi_multiset /- subtraction -/ section variables [decidable_eq Ξ±] {s t u : multiset Ξ±} {a b : Ξ±} /-- `s - t` is the multiset such that `count a (s - t) = count a s - count a t` for all `a`. -/ protected def sub (s t : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s t (Ξ» l₁ lβ‚‚, (l₁.diff lβ‚‚ : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ perm_diff_right w₁ pβ‚‚ β–Έ perm_diff_left _ p₁ instance : has_sub (multiset Ξ±) := ⟨multiset.sub⟩ @[simp] theorem coe_sub (s t : list Ξ±) : (s - t : multiset Ξ±) = (s.diff t : list Ξ±) := rfl theorem sub_eq_fold_erase (s t : multiset Ξ±) : s - t = foldl erase erase_comm s t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, show ↑(l₁.diff lβ‚‚) = foldl erase erase_comm ↑l₁ ↑lβ‚‚, by rw diff_eq_foldl l₁ lβ‚‚; exact foldl_hom _ _ _ _ (Ξ» x y, rfl) _ @[simp] theorem sub_zero (s : multiset Ξ±) : s - 0 = s := quot.induction_on s $ Ξ» l, rfl @[simp] theorem sub_cons (a : Ξ±) (s t : multiset Ξ±) : s - a::t = s.erase a - t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ diff_cons _ _ _ theorem add_sub_of_le (h : s ≀ t) : s + (t - s) = t := begin revert t, refine multiset.induction_on s (by simp) (Ξ» a s IH t h, _), have := cons_erase (mem_of_le h (mem_cons_self _ _)), rw [cons_add, sub_cons, IH, this], exact (cons_le_cons_iff a).1 (this.symm β–Έ h) end theorem sub_add' : s - (t + u) = s - t - u := quotient.induction_on₃ s t u $ Ξ» l₁ lβ‚‚ l₃, congr_arg coe $ diff_append _ _ _ theorem sub_add_cancel (h : t ≀ s) : s - t + t = s := by rw [add_comm, add_sub_of_le h] theorem add_sub_cancel_left (s : multiset Ξ±) : βˆ€ t, s + t - s = t := multiset.induction_on s (by simp) (Ξ» a s IH t, by rw [cons_add, sub_cons, erase_cons_head, IH]) theorem add_sub_cancel (s t : multiset Ξ±) : s + t - t = s := by rw [add_comm, add_sub_cancel_left] theorem sub_le_sub_right (h : s ≀ t) (u) : s - u ≀ t - u := by revert s t h; exact multiset.induction_on u (by simp {contextual := tt}) (Ξ» a u IH s t h, by simp [IH, erase_le_erase a h]) theorem sub_le_sub_left (h : s ≀ t) : βˆ€ u, u - t ≀ u - s := le_induction_on h $ Ξ» l₁ lβ‚‚ h, begin induction h with l₁ lβ‚‚ a s IH l₁ lβ‚‚ a s IH; intro u, { refl }, { rw [← cons_coe, sub_cons], exact le_trans (sub_le_sub_right (erase_le _ _) _) (IH u) }, { rw [← cons_coe, sub_cons, ← cons_coe, sub_cons], exact IH _ } end theorem sub_le_iff_le_add : s - t ≀ u ↔ s ≀ u + t := by revert s; exact multiset.induction_on t (by simp) (Ξ» a t IH s, by simp [IH, erase_le_iff_le_cons]) theorem le_sub_add (s t : multiset Ξ±) : s ≀ s - t + t := sub_le_iff_le_add.1 (le_refl _) theorem sub_le_self (s t : multiset Ξ±) : s - t ≀ s := sub_le_iff_le_add.2 (le_add_right _ _) @[simp] theorem card_sub {s t : multiset Ξ±} (h : t ≀ s) : card (s - t) = card s - card t := (nat.sub_eq_of_eq_add $ by rw [add_comm, ← card_add, sub_add_cancel h]).symm /- union -/ /-- `s βˆͺ t` is the lattice join operation with respect to the multiset `≀`. The multiplicity of `a` in `s βˆͺ t` is the maximum of the multiplicities in `s` and `t`. -/ def union (s t : multiset Ξ±) : multiset Ξ± := s - t + t instance : has_union (multiset Ξ±) := ⟨union⟩ theorem union_def (s t : multiset Ξ±) : s βˆͺ t = s - t + t := rfl theorem le_union_left (s t : multiset Ξ±) : s ≀ s βˆͺ t := le_sub_add _ _ theorem le_union_right (s t : multiset Ξ±) : t ≀ s βˆͺ t := le_add_left _ _ theorem eq_union_left : t ≀ s β†’ s βˆͺ t = s := sub_add_cancel theorem union_le_union_right (h : s ≀ t) (u) : s βˆͺ u ≀ t βˆͺ u := add_le_add_right (sub_le_sub_right h _) u theorem union_le (h₁ : s ≀ u) (hβ‚‚ : t ≀ u) : s βˆͺ t ≀ u := by rw ← eq_union_left hβ‚‚; exact union_le_union_right h₁ t @[simp] theorem mem_union : a ∈ s βˆͺ t ↔ a ∈ s ∨ a ∈ t := ⟨λ h, (mem_add.1 h).imp_left (mem_of_le $ sub_le_self _ _), or.rec (mem_of_le $ le_union_left _ _) (mem_of_le $ le_union_right _ _)⟩ /- inter -/ /-- `s ∩ t` is the lattice meet operation with respect to the multiset `≀`. The multiplicity of `a` in `s ∩ t` is the minimum of the multiplicities in `s` and `t`. -/ def inter (s t : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s t (Ξ» l₁ lβ‚‚, (l₁.bag_inter lβ‚‚ : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ perm_bag_inter_right w₁ pβ‚‚ β–Έ perm_bag_inter_left _ p₁ instance : has_inter (multiset Ξ±) := ⟨inter⟩ @[simp] theorem inter_zero (s : multiset Ξ±) : s ∩ 0 = 0 := quot.induction_on s $ Ξ» l, congr_arg coe l.bag_inter_nil @[simp] theorem zero_inter (s : multiset Ξ±) : 0 ∩ s = 0 := quot.induction_on s $ Ξ» l, congr_arg coe l.nil_bag_inter @[simp] theorem cons_inter_of_pos {a} (s : multiset Ξ±) {t} : a ∈ t β†’ (a :: s) ∩ t = a :: s ∩ t.erase a := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ cons_bag_inter_of_pos _ h @[simp] theorem cons_inter_of_neg {a} (s : multiset Ξ±) {t} : a βˆ‰ t β†’ (a :: s) ∩ t = s ∩ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ cons_bag_inter_of_neg _ h theorem inter_le_left (s t : multiset Ξ±) : s ∩ t ≀ s := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, subperm_of_sublist $ bag_inter_sublist_left _ _ theorem inter_le_right (s : multiset Ξ±) : βˆ€ t, s ∩ t ≀ t := multiset.induction_on s (Ξ» t, (zero_inter t).symm β–Έ zero_le _) $ Ξ» a s IH t, if h : a ∈ t then by simpa [h] using cons_le_cons a (IH (t.erase a)) else by simp [h, IH] theorem le_inter (h₁ : s ≀ t) (hβ‚‚ : s ≀ u) : s ≀ t ∩ u := begin revert s u, refine multiset.induction_on t _ (Ξ» a t IH, _); intros, { simp [h₁] }, by_cases a ∈ u, { rw [cons_inter_of_pos _ h, ← erase_le_iff_le_cons], exact IH (erase_le_iff_le_cons.2 h₁) (erase_le_erase _ hβ‚‚) }, { rw cons_inter_of_neg _ h, exact IH ((le_cons_of_not_mem $ mt (mem_of_le hβ‚‚) h).1 h₁) hβ‚‚ } end @[simp] theorem mem_inter : a ∈ s ∩ t ↔ a ∈ s ∧ a ∈ t := ⟨λ h, ⟨mem_of_le (inter_le_left _ _) h, mem_of_le (inter_le_right _ _) h⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, by rw [← cons_erase h₁, cons_inter_of_pos _ hβ‚‚]; apply mem_cons_self⟩ instance : lattice (multiset Ξ±) := { sup := (βˆͺ), sup_le := @union_le _ _, le_sup_left := le_union_left, le_sup_right := le_union_right, inf := (∩), le_inf := @le_inter _ _, inf_le_left := inter_le_left, inf_le_right := inter_le_right, ..@multiset.partial_order Ξ± } @[simp] theorem sup_eq_union (s t : multiset Ξ±) : s βŠ” t = s βˆͺ t := rfl @[simp] theorem inf_eq_inter (s t : multiset Ξ±) : s βŠ“ t = s ∩ t := rfl @[simp] theorem le_inter_iff : s ≀ t ∩ u ↔ s ≀ t ∧ s ≀ u := le_inf_iff @[simp] theorem union_le_iff : s βˆͺ t ≀ u ↔ s ≀ u ∧ t ≀ u := sup_le_iff instance : semilattice_inf_bot (multiset Ξ±) := { bot := 0, bot_le := zero_le, ..multiset.lattice.lattice } theorem union_comm (s t : multiset Ξ±) : s βˆͺ t = t βˆͺ s := sup_comm theorem inter_comm (s t : multiset Ξ±) : s ∩ t = t ∩ s := inf_comm theorem eq_union_right (h : s ≀ t) : s βˆͺ t = t := by rw [union_comm, eq_union_left h] theorem union_le_union_left (h : s ≀ t) (u) : u βˆͺ s ≀ u βˆͺ t := sup_le_sup_left h _ theorem union_le_add (s t : multiset Ξ±) : s βˆͺ t ≀ s + t := union_le (le_add_right _ _) (le_add_left _ _) theorem union_add_distrib (s t u : multiset Ξ±) : (s βˆͺ t) + u = (s + u) βˆͺ (t + u) := by simpa [(βˆͺ), union, eq_comm] using show s + u - (t + u) = s - t, by rw [add_comm t, sub_add', add_sub_cancel] theorem add_union_distrib (s t u : multiset Ξ±) : s + (t βˆͺ u) = (s + t) βˆͺ (s + u) := by rw [add_comm, union_add_distrib, add_comm s, add_comm s] theorem cons_union_distrib (a : Ξ±) (s t : multiset Ξ±) : a :: (s βˆͺ t) = (a :: s) βˆͺ (a :: t) := by simpa using add_union_distrib (a::0) s t theorem inter_add_distrib (s t u : multiset Ξ±) : (s ∩ t) + u = (s + u) ∩ (t + u) := begin by_contra h, cases lt_iff_cons_le.1 (lt_of_le_of_ne (le_inter (add_le_add_right (inter_le_left s t) u) (add_le_add_right (inter_le_right s t) u)) h) with a hl, rw ← cons_add at hl, exact not_le_of_lt (lt_cons_self (s ∩ t) a) (le_inter (le_of_add_le_add_right (le_trans hl (inter_le_left _ _))) (le_of_add_le_add_right (le_trans hl (inter_le_right _ _)))) end theorem add_inter_distrib (s t u : multiset Ξ±) : s + (t ∩ u) = (s + t) ∩ (s + u) := by rw [add_comm, inter_add_distrib, add_comm s, add_comm s] theorem cons_inter_distrib (a : Ξ±) (s t : multiset Ξ±) : a :: (s ∩ t) = (a :: s) ∩ (a :: t) := by simp theorem union_add_inter (s t : multiset Ξ±) : s βˆͺ t + s ∩ t = s + t := begin apply le_antisymm, { rw union_add_distrib, refine union_le (add_le_add_left (inter_le_right _ _) _) _, rw add_comm, exact add_le_add_right (inter_le_left _ _) _ }, { rw [add_comm, add_inter_distrib], refine le_inter (add_le_add_right (le_union_right _ _) _) _, rw add_comm, exact add_le_add_right (le_union_left _ _) _ } end theorem sub_add_inter (s t : multiset Ξ±) : s - t + s ∩ t = s := begin rw [inter_comm], revert s, refine multiset.induction_on t (by simp) (Ξ» a t IH s, _), by_cases a ∈ s, { rw [cons_inter_of_pos _ h, sub_cons, add_cons, IH, cons_erase h] }, { rw [cons_inter_of_neg _ h, sub_cons, erase_of_not_mem h, IH] } end theorem sub_inter (s t : multiset Ξ±) : s - (s ∩ t) = s - t := add_right_cancel $ by rw [sub_add_inter s t, sub_add_cancel (inter_le_left _ _)] end /- filter -/ section variables {p : Ξ± β†’ Prop} [decidable_pred p] /-- `filter p s` returns the elements in `s` (with the same multiplicities) which satisfy `p`, and removes the rest. -/ def filter (p : Ξ± β†’ Prop) [h : decidable_pred p] (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (filter p l : multiset Ξ±)) (Ξ» l₁ lβ‚‚ h, quot.sound $ perm_filter p h) @[simp] theorem coe_filter (p : Ξ± β†’ Prop) [h : decidable_pred p] (l : list Ξ±) : filter p (↑l) = l.filter p := rfl @[simp] theorem filter_zero (p : Ξ± β†’ Prop) [h : decidable_pred p] : filter p 0 = 0 := rfl @[simp] theorem filter_cons_of_pos {a : Ξ±} (s) : p a β†’ filter p (a::s) = a :: filter p s := quot.induction_on s $ Ξ» l h, congr_arg coe $ filter_cons_of_pos l h @[simp] theorem filter_cons_of_neg {a : Ξ±} (s) : Β¬ p a β†’ filter p (a::s) = filter p s := quot.induction_on s $ Ξ» l h, @congr_arg _ _ _ _ coe $ filter_cons_of_neg l h @[simp] theorem filter_add (s t : multiset Ξ±) : filter p (s + t) = filter p s + filter p t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ filter_append _ _ @[simp] theorem filter_le (s : multiset Ξ±) : filter p s ≀ s := quot.induction_on s $ Ξ» l, subperm_of_sublist $ filter_sublist _ @[simp] theorem filter_subset (s : multiset Ξ±) : filter p s βŠ† s := subset_of_le $ filter_le _ @[simp] theorem mem_filter {a : Ξ±} {s} : a ∈ filter p s ↔ a ∈ s ∧ p a := quot.induction_on s $ Ξ» l, mem_filter theorem of_mem_filter {a : Ξ±} {s} (h : a ∈ filter p s) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : Ξ±} {s} (h : a ∈ filter p s) : a ∈ s := (mem_filter.1 h).1 theorem mem_filter_of_mem {a : Ξ±} {l} (m : a ∈ l) (h : p a) : a ∈ filter p l := mem_filter.2 ⟨m, h⟩ theorem filter_eq_self {s} : filter p s = s ↔ βˆ€ a ∈ s, p a := quot.induction_on s $ Ξ» l, iff.trans ⟨λ h, eq_of_sublist_of_length_eq (filter_sublist _) (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_self theorem filter_eq_nil {s} : filter p s = 0 ↔ βˆ€ a ∈ s, Β¬p a := quot.induction_on s $ Ξ» l, iff.trans ⟨λ h, eq_nil_of_length_eq_zero (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_nil theorem filter_le_filter {s t} (h : s ≀ t) : filter p s ≀ filter p t := le_induction_on h $ Ξ» l₁ lβ‚‚ h, subperm_of_sublist $ filter_sublist_filter h theorem le_filter {s t} : s ≀ filter p t ↔ s ≀ t ∧ βˆ€ a ∈ s, p a := ⟨λ h, ⟨le_trans h (filter_le _), Ξ» a m, of_mem_filter (mem_of_le h m)⟩, Ξ» ⟨h, al⟩, filter_eq_self.2 al β–Έ filter_le_filter h⟩ @[simp] theorem filter_sub [decidable_eq Ξ±] (s t : multiset Ξ±) : filter p (s - t) = filter p s - filter p t := begin revert s, refine multiset.induction_on t (by simp) (Ξ» a t IH s, _), rw [sub_cons, IH], by_cases p a, { rw [filter_cons_of_pos _ h, sub_cons], congr, by_cases m : a ∈ s, { rw [← cons_inj_right a, ← filter_cons_of_pos _ h, cons_erase (mem_filter_of_mem m h), cons_erase m] }, { rw [erase_of_not_mem m, erase_of_not_mem (mt mem_of_mem_filter m)] } }, { rw [filter_cons_of_neg _ h], by_cases m : a ∈ s, { rw [(by rw filter_cons_of_neg _ h : filter p (erase s a) = filter p (a :: erase s a)), cons_erase m] }, { rw [erase_of_not_mem m] } } end @[simp] theorem filter_union [decidable_eq Ξ±] (s t : multiset Ξ±) : filter p (s βˆͺ t) = filter p s βˆͺ filter p t := by simp [(βˆͺ), union] @[simp] theorem filter_inter [decidable_eq Ξ±] (s t : multiset Ξ±) : filter p (s ∩ t) = filter p s ∩ filter p t := le_antisymm (le_inter (filter_le_filter $ inter_le_left _ _) (filter_le_filter $ inter_le_right _ _)) $ le_filter.2 ⟨inf_le_inf (filter_le _) (filter_le _), Ξ» a h, of_mem_filter (mem_of_le (inter_le_left _ _) h)⟩ /- filter_map -/ /-- `filter_map f s` is a combination filter/map operation on `s`. The function `f : Ξ± β†’ option Ξ²` is applied to each element of `s`; if `f a` is `some b` then `b` is added to the result, otherwise `a` is removed from the resulting multiset. -/ def filter_map (f : Ξ± β†’ option Ξ²) (s : multiset Ξ±) : multiset Ξ² := quot.lift_on s (Ξ» l, (filter_map f l : multiset Ξ²)) (Ξ» l₁ lβ‚‚ h, quot.sound $perm_filter_map f h) @[simp] theorem coe_filter_map (f : Ξ± β†’ option Ξ²) (l : list Ξ±) : filter_map f l = l.filter_map f := rfl @[simp] theorem filter_map_zero (f : Ξ± β†’ option Ξ²) : filter_map f 0 = 0 := rfl @[simp] theorem filter_map_cons_none {f : Ξ± β†’ option Ξ²} (a : Ξ±) (s : multiset Ξ±) (h : f a = none) : filter_map f (a :: s) = filter_map f s := quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ filter_map_cons_none a l h @[simp] theorem filter_map_cons_some (f : Ξ± β†’ option Ξ²) (a : Ξ±) (s : multiset Ξ±) {b : Ξ²} (h : f a = some b) : filter_map f (a :: s) = b :: filter_map f s := quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ filter_map_cons_some f a l h theorem filter_map_eq_map (f : Ξ± β†’ Ξ²) : filter_map (some ∘ f) = map f := funext $ Ξ» s, quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_map f) l theorem filter_map_eq_filter (p : Ξ± β†’ Prop) [decidable_pred p] : filter_map (option.guard p) = filter p := funext $ Ξ» s, quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_filter p) l theorem filter_map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ option Ξ³) (s : multiset Ξ±) : filter_map g (filter_map f s) = filter_map (Ξ» x, (f x).bind g) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_filter_map f g l theorem map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ³) (s : multiset Ξ±) : map g (filter_map f s) = filter_map (Ξ» x, (f x).map g) s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_filter_map f g l theorem filter_map_map (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ option Ξ³) (s : multiset Ξ±) : filter_map g (map f s) = filter_map (g ∘ f) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_map f g l theorem filter_filter_map (f : Ξ± β†’ option Ξ²) (p : Ξ² β†’ Prop) [decidable_pred p] (s : multiset Ξ±) : filter p (filter_map f s) = filter_map (Ξ» x, (f x).filter p) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_filter_map f p l theorem filter_map_filter (p : Ξ± β†’ Prop) [decidable_pred p] (f : Ξ± β†’ option Ξ²) (s : multiset Ξ±) : filter_map f (filter p s) = filter_map (Ξ» x, if p x then f x else none) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_filter p f l @[simp] theorem filter_map_some (s : multiset Ξ±) : filter_map some s = s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_some l @[simp] theorem mem_filter_map (f : Ξ± β†’ option Ξ²) (s : multiset Ξ±) {b : Ξ²} : b ∈ filter_map f s ↔ βˆƒ a, a ∈ s ∧ f a = some b := quot.induction_on s $ Ξ» l, mem_filter_map f l theorem map_filter_map_of_inv (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ±) (H : βˆ€ x : Ξ±, (f x).map g = some x) (s : multiset Ξ±) : map g (filter_map f s) = s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_filter_map_of_inv f g H l theorem filter_map_le_filter_map (f : Ξ± β†’ option Ξ²) {s t : multiset Ξ±} (h : s ≀ t) : filter_map f s ≀ filter_map f t := le_induction_on h $ Ξ» l₁ lβ‚‚ h, subperm_of_sublist $ filter_map_sublist_filter_map _ h /- powerset -/ def powerset_aux (l : list Ξ±) : list (multiset Ξ±) := 0 :: sublists_aux l (Ξ» x y, x :: y) theorem powerset_aux_eq_map_coe {l : list Ξ±} : powerset_aux l = (sublists l).map coe := by simp [powerset_aux, sublists]; rw [← show @sublists_aux₁ Ξ± (multiset Ξ±) l (Ξ» x, [↑x]) = sublists_aux l (Ξ» x, list.cons ↑x), from sublists_aux₁_eq_sublists_aux _ _, sublists_aux_cons_eq_sublists_aux₁, ← bind_ret_eq_map, sublists_aux₁_bind]; refl def powerset_aux' (l : list Ξ±) : list (multiset Ξ±) := (sublists' l).map coe theorem powerset_aux_perm_powerset_aux' {l : list Ξ±} : powerset_aux l ~ powerset_aux' l := by rw powerset_aux_eq_map_coe; exact perm_map _ (sublists_perm_sublists' _) @[simp] theorem powerset_aux'_nil : powerset_aux' (@nil Ξ±) = [0] := rfl @[simp] theorem powerset_aux'_cons (a : Ξ±) (l : list Ξ±) : powerset_aux' (a::l) = powerset_aux' l ++ list.map (cons a) (powerset_aux' l) := by simp [powerset_aux']; refl theorem powerset_aux'_perm {l₁ lβ‚‚ : list Ξ±} (p : l₁ ~ lβ‚‚) : powerset_aux' l₁ ~ powerset_aux' lβ‚‚ := begin induction p with a l₁ lβ‚‚ p IH a b l l₁ lβ‚‚ l₃ p₁ pβ‚‚ IH₁ IHβ‚‚, {simp}, { simp, exact perm_app IH (perm_map _ IH) }, { simp, apply perm_app_right, rw [← append_assoc, ← append_assoc, (by funext s; simp [cons_swap] : cons b ∘ cons a = cons a ∘ cons b)], exact perm_app_left _ perm_app_comm }, { exact IH₁.trans IHβ‚‚ } end theorem powerset_aux_perm {l₁ lβ‚‚ : list Ξ±} (p : l₁ ~ lβ‚‚) : powerset_aux l₁ ~ powerset_aux lβ‚‚ := powerset_aux_perm_powerset_aux'.trans $ (powerset_aux'_perm p).trans powerset_aux_perm_powerset_aux'.symm def powerset (s : multiset Ξ±) : multiset (multiset Ξ±) := quot.lift_on s (Ξ» l, (powerset_aux l : multiset (multiset Ξ±))) (Ξ» l₁ lβ‚‚ h, quot.sound (powerset_aux_perm h)) theorem powerset_coe (l : list Ξ±) : @powerset Ξ± l = ((sublists l).map coe : list (multiset Ξ±)) := congr_arg coe powerset_aux_eq_map_coe @[simp] theorem powerset_coe' (l : list Ξ±) : @powerset Ξ± l = ((sublists' l).map coe : list (multiset Ξ±)) := quot.sound powerset_aux_perm_powerset_aux' @[simp] theorem mem_powerset {s t : multiset Ξ±} : s ∈ powerset t ↔ s ≀ t := quotient.induction_onβ‚‚ s t $ by simp [subperm, and.comm] theorem map_single_le_powerset (s : multiset Ξ±) : s.map (Ξ» a, a::0) ≀ powerset s := quotient.induction_on s $ Ξ» l, begin simp [powerset_coe], show l.map (coe ∘ list.ret) <+~ (sublists l).map coe, rw ← list.map_map, exact subperm_of_sublist (map_sublist_map _ (map_ret_sublist_sublists _)) end @[simp] theorem card_powerset (s : multiset Ξ±) : card (powerset s) = 2 ^ card s := quotient.induction_on s $ by simp /- countp -/ /-- `countp p s` counts the number of elements of `s` (with multiplicity) that satisfy `p`. -/ def countp (p : Ξ± β†’ Prop) [decidable_pred p] (s : multiset Ξ±) : β„• := quot.lift_on s (countp p) (Ξ» l₁ lβ‚‚, perm_countp p) @[simp] theorem coe_countp (l : list Ξ±) : countp p l = l.countp p := rfl @[simp] theorem countp_zero (p : Ξ± β†’ Prop) [decidable_pred p] : countp p 0 = 0 := rfl @[simp] theorem countp_cons_of_pos {a : Ξ±} (s) : p a β†’ countp p (a::s) = countp p s + 1 := quot.induction_on s countp_cons_of_pos @[simp] theorem countp_cons_of_neg {a : Ξ±} (s) : Β¬ p a β†’ countp p (a::s) = countp p s := quot.induction_on s countp_cons_of_neg theorem countp_eq_card_filter (s) : countp p s = card (filter p s) := quot.induction_on s $ Ξ» l, countp_eq_length_filter _ @[simp] theorem countp_add (s t) : countp p (s + t) = countp p s + countp p t := by simp [countp_eq_card_filter] theorem countp_pos {s} : 0 < countp p s ↔ βˆƒ a ∈ s, p a := by simp [countp_eq_card_filter, card_pos_iff_exists_mem] @[simp] theorem countp_sub [decidable_eq Ξ±] {s t : multiset Ξ±} (h : t ≀ s) : countp p (s - t) = countp p s - countp p t := by simp [countp_eq_card_filter, h, filter_le_filter] theorem countp_pos_of_mem {s a} (h : a ∈ s) (pa : p a) : 0 < countp p s := countp_pos.2 ⟨_, h, pa⟩ theorem countp_le_of_le {s t} (h : s ≀ t) : countp p s ≀ countp p t := by simpa [countp_eq_card_filter] using card_le_of_le (filter_le_filter h) end /- count -/ section variable [decidable_eq Ξ±] /-- `count a s` is the multiplicity of `a` in `s`. -/ def count (a : Ξ±) : multiset Ξ± β†’ β„• := countp (eq a) @[simp] theorem coe_count (a : Ξ±) (l : list Ξ±) : count a (↑l) = l.count a := coe_countp _ @[simp] theorem count_zero (a : Ξ±) : count a 0 = 0 := rfl @[simp] theorem count_cons_self (a : Ξ±) (s : multiset Ξ±) : count a (a::s) = succ (count a s) := countp_cons_of_pos _ rfl @[simp] theorem count_cons_of_ne {a b : Ξ±} (h : a β‰  b) (s : multiset Ξ±) : count a (b::s) = count a s := countp_cons_of_neg _ h theorem count_le_of_le (a : Ξ±) {s t} : s ≀ t β†’ count a s ≀ count a t := countp_le_of_le theorem count_le_count_cons (a b : Ξ±) (s : multiset Ξ±) : count a s ≀ count a (b :: s) := count_le_of_le _ (le_cons_self _ _) theorem count_singleton (a : Ξ±) : count a (a::0) = 1 := by simp @[simp] theorem count_add (a : Ξ±) : βˆ€ s t, count a (s + t) = count a s + count a t := countp_add @[simp] theorem count_smul (a : Ξ±) (n s) : count a (n β€’ s) = n * count a s := by induction n; simp [*, succ_smul', succ_mul] theorem count_pos {a : Ξ±} {s : multiset Ξ±} : 0 < count a s ↔ a ∈ s := by simp [count, countp_pos] @[simp] theorem count_eq_zero_of_not_mem {a : Ξ±} {l : list Ξ±} (h : a βˆ‰ l) : count a l = 0 := by_contradiction $ Ξ» h', h $ count_pos.1 (nat.pos_of_ne_zero h') theorem count_eq_zero {a : Ξ±} {s : multiset Ξ±} : count a s = 0 ↔ a βˆ‰ s := iff_not_comm.1 $ count_pos.symm.trans pos_iff_ne_zero @[simp] theorem count_repeat (a : Ξ±) (n : β„•) : count a (repeat a n) = n := by simp [repeat] @[simp] theorem count_erase_self (a : Ξ±) (s : multiset Ξ±) : count a (erase s a) = pred (count a s) := begin by_cases a ∈ s, { rw [(by rw cons_erase h : count a s = count a (a::erase s a)), count_cons_self]; refl }, { rw [erase_of_not_mem h, count_eq_zero.2 h]; refl } end @[simp] theorem count_erase_of_ne {a b : Ξ±} (ab : a β‰  b) (s : multiset Ξ±) : count a (erase s b) = count a s := begin by_cases b ∈ s, { rw [← count_cons_of_ne ab, cons_erase h] }, { rw [erase_of_not_mem h] } end @[simp] theorem count_sub (a : Ξ±) (s t : multiset Ξ±) : count a (s - t) = count a s - count a t := begin revert s, refine multiset.induction_on t (by simp) (Ξ» b t IH s, _), rw [sub_cons, IH], by_cases ab : a = b, { subst b, rw [count_erase_self, count_cons_self, sub_succ, pred_sub] }, { rw [count_erase_of_ne ab, count_cons_of_ne ab] } end @[simp] theorem count_union (a : Ξ±) (s t : multiset Ξ±) : count a (s βˆͺ t) = max (count a s) (count a t) := by simp [(βˆͺ), union, sub_add_eq_max, -add_comm] @[simp] theorem count_inter (a : Ξ±) (s t : multiset Ξ±) : count a (s ∩ t) = min (count a s) (count a t) := begin apply @nat.add_left_cancel (count a (s - t)), rw [← count_add, sub_add_inter, count_sub, sub_add_min], end lemma count_bind {m : multiset Ξ²} {f : Ξ² β†’ multiset Ξ±} {a : Ξ±} : count a (bind m f) = sum (m.map $ Ξ»b, count a $ f b) := multiset.induction_on m (by simp) (by simp) theorem le_count_iff_repeat_le {a : Ξ±} {s : multiset Ξ±} {n : β„•} : n ≀ count a s ↔ repeat a n ≀ s := quot.induction_on s $ Ξ» l, le_count_iff_repeat_sublist.trans repeat_le_coe.symm theorem ext {s t : multiset Ξ±} : s = t ↔ βˆ€ a, count a s = count a t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, quotient.eq.trans perm_iff_count lemma bind_bind [decidable_eq Ξ³] (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ multiset Ξ³} : (bind m $ Ξ»a, bind n $ Ξ»b, f a b) = (bind n $ Ξ»b, bind m $ Ξ»a, f a b) := by simp [multiset.ext, count_bind, multiset.sum_map_sum_map m n] theorem le_iff_count {s t : multiset Ξ±} : s ≀ t ↔ βˆ€ a, count a s ≀ count a t := ⟨λ h a, count_le_of_le a h, Ξ» al, by rw ← (ext.2 (Ξ» a, by simp [max_eq_right (al a)]) : s βˆͺ t = t); apply le_union_left⟩ instance : distrib_lattice (multiset Ξ±) := { le_sup_inf := Ξ» s t u, le_of_eq $ eq.symm $ ext.2 $ Ξ» a, by simp [max_min_distrib_left], ..multiset.lattice.lattice } instance : semilattice_sup_bot (multiset Ξ±) := { bot := 0, bot_le := zero_le, ..multiset.lattice.lattice } end /- disjoint -/ /-- `disjoint s t` means that `s` and `t` have no elements in common. -/ def disjoint (s t : multiset Ξ±) : Prop := βˆ€ ⦃a⦄, a ∈ s β†’ a ∈ t β†’ false @[simp] theorem coe_disjoint (l₁ lβ‚‚ : list Ξ±) : @disjoint Ξ± l₁ lβ‚‚ ↔ l₁.disjoint lβ‚‚ := iff.rfl theorem disjoint.symm {s t : multiset Ξ±} (d : disjoint s t) : disjoint t s | a iβ‚‚ i₁ := d i₁ iβ‚‚ @[simp] theorem disjoint_comm {s t : multiset Ξ±} : disjoint s t ↔ disjoint t s := ⟨disjoint.symm, disjoint.symm⟩ theorem disjoint_left {s t : multiset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ s β†’ a βˆ‰ t := iff.rfl theorem disjoint_right {s t : multiset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ t β†’ a βˆ‰ s := disjoint_comm theorem disjoint_iff_ne {s t : multiset Ξ±} : disjoint s t ↔ βˆ€ a ∈ s, βˆ€ b ∈ t, a β‰  b := by simp [disjoint_left, imp_not_comm] theorem disjoint_of_subset_left {s t u : multiset Ξ±} (h : s βŠ† u) (d : disjoint u t) : disjoint s t | x m₁ := d (h m₁) theorem disjoint_of_subset_right {s t u : multiset Ξ±} (h : t βŠ† u) (d : disjoint s u) : disjoint s t | x m m₁ := d m (h m₁) theorem disjoint_of_le_left {s t u : multiset Ξ±} (h : s ≀ u) : disjoint u t β†’ disjoint s t := disjoint_of_subset_left (subset_of_le h) theorem disjoint_of_le_right {s t u : multiset Ξ±} (h : t ≀ u) : disjoint s u β†’ disjoint s t := disjoint_of_subset_right (subset_of_le h) @[simp] theorem zero_disjoint (l : multiset Ξ±) : disjoint 0 l | a := (not_mem_nil a).elim @[simp] theorem singleton_disjoint {l : multiset Ξ±} {a : Ξ±} : disjoint (a::0) l ↔ a βˆ‰ l := by simp [disjoint]; refl @[simp] theorem disjoint_singleton {l : multiset Ξ±} {a : Ξ±} : disjoint l (a::0) ↔ a βˆ‰ l := by rw disjoint_comm; simp @[simp] theorem disjoint_add_left {s t u : multiset Ξ±} : disjoint (s + t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_add_right {s t u : multiset Ξ±} : disjoint s (t + u) ↔ disjoint s t ∧ disjoint s u := disjoint_comm.trans $ by simp [disjoint_append_left] @[simp] theorem disjoint_cons_left {a : Ξ±} {s t : multiset Ξ±} : disjoint (a::s) t ↔ a βˆ‰ t ∧ disjoint s t := (@disjoint_add_left _ (a::0) s t).trans $ by simp @[simp] theorem disjoint_cons_right {a : Ξ±} {s t : multiset Ξ±} : disjoint s (a::t) ↔ a βˆ‰ s ∧ disjoint s t := disjoint_comm.trans $ by simp [disjoint_cons_left] theorem inter_eq_zero_iff_disjoint [decidable_eq Ξ±] {s t : multiset Ξ±} : s ∩ t = 0 ↔ disjoint s t := by rw ← subset_zero; simp [subset_iff, disjoint] @[simp] theorem disjoint_union_left [decidable_eq Ξ±] {s t u : multiset Ξ±} : disjoint (s βˆͺ t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right [decidable_eq Ξ±] {s t u : multiset Ξ±} : disjoint s (t βˆͺ u) ↔ disjoint s t ∧ disjoint s u := by simp [disjoint, or_imp_distrib, forall_and_distrib] lemma disjoint_map_map {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ³} {s : multiset Ξ±} {t : multiset Ξ²} : disjoint (s.map f) (t.map g) ↔ (βˆ€a∈s, βˆ€b∈t, f a β‰  g b) := begin simp [disjoint], split, from assume h a ha b hb eq, h _ ha rfl _ hb eq.symm, from assume h c a ha eq₁ b hb eqβ‚‚, h _ ha _ hb (eqβ‚‚.symm β–Έ eq₁) end /-- `pairwise r m` states that there exists a list of the elements s.t. `r` holds pairwise on this list. -/ def pairwise (r : Ξ± β†’ Ξ± β†’ Prop) (m : multiset Ξ±) : Prop := βˆƒl:list Ξ±, m = l ∧ l.pairwise r lemma pairwise_coe_iff_pairwise {r : Ξ± β†’ Ξ± β†’ Prop} (hr : symmetric r) {l : list Ξ±} : multiset.pairwise r l ↔ l.pairwise r := iff.intro (assume ⟨l', eq, h⟩, (list.perm_pairwise hr (quotient.exact eq)).2 h) (assume h, ⟨l, rfl, h⟩) /- nodup -/ /-- `nodup s` means that `s` has no duplicates, i.e. the multiplicity of any element is at most 1. -/ def nodup (s : multiset Ξ±) : Prop := quot.lift_on s nodup (Ξ» s t p, propext $ perm_nodup p) @[simp] theorem coe_nodup {l : list Ξ±} : @nodup Ξ± l ↔ l.nodup := iff.rfl @[simp] theorem forall_mem_ne {a : Ξ±} {l : list Ξ±} : (βˆ€ (a' : Ξ±), a' ∈ l β†’ Β¬a = a') ↔ a βˆ‰ l := ⟨λ h m, h _ m rfl, Ξ» h a' m e, h (e.symm β–Έ m)⟩ @[simp] theorem nodup_zero : @nodup Ξ± 0 := pairwise.nil _ @[simp] theorem nodup_cons {a : Ξ±} {s : multiset Ξ±} : nodup (a::s) ↔ a βˆ‰ s ∧ nodup s := quot.induction_on s $ Ξ» l, nodup_cons theorem nodup_cons_of_nodup {a : Ξ±} {s : multiset Ξ±} (m : a βˆ‰ s) (n : nodup s) : nodup (a::s) := nodup_cons.2 ⟨m, n⟩ theorem nodup_singleton : βˆ€ a : Ξ±, nodup (a::0) := nodup_singleton theorem nodup_of_nodup_cons {a : Ξ±} {s : multiset Ξ±} (h : nodup (a::s)) : nodup s := (nodup_cons.1 h).2 theorem not_mem_of_nodup_cons {a : Ξ±} {s : multiset Ξ±} (h : nodup (a::s)) : a βˆ‰ s := (nodup_cons.1 h).1 theorem nodup_of_le {s t : multiset Ξ±} (h : s ≀ t) : nodup t β†’ nodup s := le_induction_on h $ Ξ» l₁ lβ‚‚, nodup_of_sublist theorem not_nodup_pair : βˆ€ a : Ξ±, Β¬ nodup (a::a::0) := not_nodup_pair theorem nodup_iff_le {s : multiset Ξ±} : nodup s ↔ βˆ€ a : Ξ±, Β¬ a::a::0 ≀ s := quot.induction_on s $ Ξ» l, nodup_iff_sublist.trans $ forall_congr $ Ξ» a, not_congr (@repeat_le_coe _ a 2 _).symm theorem nodup_iff_count_le_one [decidable_eq Ξ±] {s : multiset Ξ±} : nodup s ↔ βˆ€ a, count a s ≀ 1 := quot.induction_on s $ Ξ» l, nodup_iff_count_le_one @[simp] theorem count_eq_one_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : multiset Ξ±} (d : nodup s) (h : a ∈ s) : count a s = 1 := le_antisymm (nodup_iff_count_le_one.1 d a) (count_pos.2 h) lemma pairwise_of_nodup {r : Ξ± β†’ Ξ± β†’ Prop} {s : multiset Ξ±} : (βˆ€a∈s, βˆ€b∈s, a β‰  b β†’ r a b) β†’ nodup s β†’ pairwise r s := quotient.induction_on s $ assume l h hl, ⟨l, rfl, hl.imp_of_mem $ assume a b ha hb, h a ha b hb⟩ theorem nodup_add {s t : multiset Ξ±} : nodup (s + t) ↔ nodup s ∧ nodup t ∧ disjoint s t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, nodup_append theorem disjoint_of_nodup_add {s t : multiset Ξ±} (d : nodup (s + t)) : disjoint s t := (nodup_add.1 d).2.2 theorem nodup_add_of_nodup {s t : multiset Ξ±} (d₁ : nodup s) (dβ‚‚ : nodup t) : nodup (s + t) ↔ disjoint s t := by simp [nodup_add, d₁, dβ‚‚] theorem nodup_of_nodup_map (f : Ξ± β†’ Ξ²) {s : multiset Ξ±} : nodup (map f s) β†’ nodup s := quot.induction_on s $ Ξ» l, nodup_of_nodup_map f theorem nodup_map_on {f : Ξ± β†’ Ξ²} {s : multiset Ξ±} : (βˆ€x∈s, βˆ€y∈s, f x = f y β†’ x = y) β†’ nodup s β†’ nodup (map f s) := quot.induction_on s $ Ξ» l, nodup_map_on theorem nodup_map {f : Ξ± β†’ Ξ²} {s : multiset Ξ±} (hf : function.injective f) : nodup s β†’ nodup (map f s) := nodup_map_on (Ξ» x _ y _ h, hf h) theorem nodup_filter (p : Ξ± β†’ Prop) [decidable_pred p] {s} : nodup s β†’ nodup (filter p s) := quot.induction_on s $ Ξ» l, nodup_filter p @[simp] theorem nodup_attach {s : multiset Ξ±} : nodup (attach s) ↔ nodup s := quot.induction_on s $ Ξ» l, nodup_attach theorem nodup_pmap {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {s : multiset Ξ±} {H} (hf : βˆ€ a ha b hb, f a ha = f b hb β†’ a = b) : nodup s β†’ nodup (pmap f s H) := quot.induction_on s (Ξ» l H, nodup_pmap hf) H instance nodup_decidable [decidable_eq Ξ±] (s : multiset Ξ±) : decidable (nodup s) := quotient.rec_on_subsingleton s $ Ξ» l, l.nodup_decidable theorem nodup_erase_eq_filter [decidable_eq Ξ±] (a : Ξ±) {s} : nodup s β†’ s.erase a = filter (β‰  a) s := quot.induction_on s $ Ξ» l d, congr_arg coe $ nodup_erase_eq_filter a d theorem nodup_erase_of_nodup [decidable_eq Ξ±] (a : Ξ±) {l} : nodup l β†’ nodup (l.erase a) := nodup_of_le (erase_le _ _) theorem mem_erase_iff_of_nodup [decidable_eq Ξ±] {a b : Ξ±} {l} (d : nodup l) : a ∈ l.erase b ↔ a β‰  b ∧ a ∈ l := by rw nodup_erase_eq_filter b d; simp [and_comm] theorem mem_erase_of_nodup [decidable_eq Ξ±] {a : Ξ±} {l} (h : nodup l) : a βˆ‰ l.erase a := by rw mem_erase_iff_of_nodup h; simp theorem nodup_product {s : multiset Ξ±} {t : multiset Ξ²} : nodup s β†’ nodup t β†’ nodup (product s t) := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ d₁ dβ‚‚, by simp [nodup_product d₁ dβ‚‚] theorem nodup_sigma {Οƒ : Ξ± β†’ Type*} {s : multiset Ξ±} {t : Ξ  a, multiset (Οƒ a)} : nodup s β†’ (βˆ€ a, nodup (t a)) β†’ nodup (s.sigma t) := quot.induction_on s $ Ξ» l₁, let lβ‚‚ (a) : list (Οƒ a) := classical.some (quotient.exists_rep (t a)) in have t = Ξ» a, lβ‚‚ a, from eq.symm $ funext $ Ξ» a, classical.some_spec (quotient.exists_rep (t a)), by rw [this]; simpa using nodup_sigma theorem nodup_filter_map (f : Ξ± β†’ option Ξ²) {s : multiset Ξ±} (H : βˆ€ (a a' : Ξ±) (b : Ξ²), b ∈ f a β†’ b ∈ f a' β†’ a = a') : nodup s β†’ nodup (filter_map f s) := quot.induction_on s $ Ξ» l, nodup_filter_map H theorem nodup_range (n : β„•) : nodup (range n) := nodup_range _ theorem nodup_inter_left [decidable_eq Ξ±] {s : multiset Ξ±} (t) : nodup s β†’ nodup (s ∩ t) := nodup_of_le $ inter_le_left _ _ theorem nodup_inter_right [decidable_eq Ξ±] (s) {t : multiset Ξ±} : nodup t β†’ nodup (s ∩ t) := nodup_of_le $ inter_le_right _ _ @[simp] theorem nodup_union [decidable_eq Ξ±] {s t : multiset Ξ±} : nodup (s βˆͺ t) ↔ nodup s ∧ nodup t := ⟨λ h, ⟨nodup_of_le (le_union_left _ _) h, nodup_of_le (le_union_right _ _) h⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, nodup_iff_count_le_one.2 $ Ξ» a, by rw [count_union]; exact max_le (nodup_iff_count_le_one.1 h₁ a) (nodup_iff_count_le_one.1 hβ‚‚ a)⟩ @[simp] theorem nodup_powerset {s : multiset Ξ±} : nodup (powerset s) ↔ nodup s := ⟨λ h, nodup_of_nodup_map _ (nodup_of_le (map_single_le_powerset _) h), quotient.induction_on s $ Ξ» l h, by simp; refine list.nodup_map_on _ (nodup_sublists'.2 h); exact Ξ» x sx y sy e, (perm_ext_sublist_nodup h (mem_sublists'.1 sx) (mem_sublists'.1 sy)).1 (quotient.exact e)⟩ @[simp] lemma nodup_bind {s : multiset Ξ±} {t : Ξ± β†’ multiset Ξ²} : nodup (bind s t) ↔ ((βˆ€a∈s, nodup (t a)) ∧ (s.pairwise (Ξ»a b, disjoint (t a) (t b)))) := have h₁ : βˆ€a, βˆƒl:list Ξ², t a = l, from assume a, quot.induction_on (t a) $ assume l, ⟨l, rfl⟩, let ⟨t', h'⟩ := classical.axiom_of_choice h₁ in have t = Ξ»a, t' a, from funext h', have hd : symmetric (Ξ»a b, list.disjoint (t' a) (t' b)), from assume a b h, h.symm, quot.induction_on s $ by simp [this, list.nodup_bind, pairwise_coe_iff_pairwise hd] theorem nodup_ext {s t : multiset Ξ±} : nodup s β†’ nodup t β†’ (s = t ↔ βˆ€ a, a ∈ s ↔ a ∈ t) := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ d₁ dβ‚‚, quotient.eq.trans $ perm_ext d₁ dβ‚‚ theorem le_iff_subset {s t : multiset Ξ±} : nodup s β†’ (s ≀ t ↔ s βŠ† t) := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ d, ⟨subset_of_le, subperm_of_subset_nodup d⟩ theorem range_le {m n : β„•} : range m ≀ range n ↔ m ≀ n := (le_iff_subset (nodup_range _)).trans range_subset theorem mem_sub_of_nodup [decidable_eq Ξ±] {a : Ξ±} {s t : multiset Ξ±} (d : nodup s) : a ∈ s - t ↔ a ∈ s ∧ a βˆ‰ t := ⟨λ h, ⟨mem_of_le (sub_le_self _ _) h, Ξ» h', by refine count_eq_zero.1 _ h; rw [count_sub a s t, nat.sub_eq_zero_iff_le]; exact le_trans (nodup_iff_count_le_one.1 d _) (count_pos.2 h')⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, or.resolve_right (mem_add.1 $ mem_of_le (le_sub_add _ _) h₁) hβ‚‚βŸ© section variable [decidable_eq Ξ±] /- erase_dup -/ /-- `erase_dup s` removes duplicates from `s`, yielding a `nodup` multiset. -/ def erase_dup (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (l.erase_dup : multiset Ξ±)) (Ξ» s t p, quot.sound (perm_erase_dup_of_perm p)) @[simp] theorem coe_erase_dup (l : list Ξ±) : @erase_dup Ξ± _ l = l.erase_dup := rfl @[simp] theorem erase_dup_zero : @erase_dup Ξ± _ 0 = 0 := rfl @[simp] theorem mem_erase_dup {a : Ξ±} {s : multiset Ξ±} : a ∈ erase_dup s ↔ a ∈ s := quot.induction_on s $ Ξ» l, mem_erase_dup @[simp] theorem erase_dup_cons_of_mem {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ erase_dup (a::s) = erase_dup s := quot.induction_on s $ Ξ» l m, @congr_arg _ _ _ _ coe $ erase_dup_cons_of_mem m @[simp] theorem erase_dup_cons_of_not_mem {a : Ξ±} {s : multiset Ξ±} : a βˆ‰ s β†’ erase_dup (a::s) = a :: erase_dup s := quot.induction_on s $ Ξ» l m, congr_arg coe $ erase_dup_cons_of_not_mem m theorem erase_dup_le (s : multiset Ξ±) : erase_dup s ≀ s := quot.induction_on s $ Ξ» l, subperm_of_sublist $ erase_dup_sublist _ theorem erase_dup_subset (s : multiset Ξ±) : erase_dup s βŠ† s := subset_of_le $ erase_dup_le _ theorem subset_erase_dup (s : multiset Ξ±) : s βŠ† erase_dup s := Ξ» a, mem_erase_dup.2 @[simp] theorem erase_dup_subset' {s t : multiset Ξ±} : erase_dup s βŠ† t ↔ s βŠ† t := ⟨subset.trans (subset_erase_dup _), subset.trans (erase_dup_subset _)⟩ @[simp] theorem subset_erase_dup' {s t : multiset Ξ±} : s βŠ† erase_dup t ↔ s βŠ† t := ⟨λ h, subset.trans h (erase_dup_subset _), Ξ» h, subset.trans h (subset_erase_dup _)⟩ @[simp] theorem nodup_erase_dup (s : multiset Ξ±) : nodup (erase_dup s) := quot.induction_on s nodup_erase_dup theorem erase_dup_eq_self {s : multiset Ξ±} : erase_dup s = s ↔ nodup s := ⟨λ e, e β–Έ nodup_erase_dup s, quot.induction_on s $ Ξ» l h, congr_arg coe $ erase_dup_eq_self.2 h⟩ theorem le_erase_dup {s t : multiset Ξ±} : s ≀ erase_dup t ↔ s ≀ t ∧ nodup s := ⟨λ h, ⟨le_trans h (erase_dup_le _), nodup_of_le h (nodup_erase_dup _)⟩, Ξ» ⟨l, d⟩, (le_iff_subset d).2 $ subset.trans (subset_of_le l) (subset_erase_dup _)⟩ theorem erase_dup_ext {s t : multiset Ξ±} : erase_dup s = erase_dup t ↔ βˆ€ a, a ∈ s ↔ a ∈ t := by simp [nodup_ext] theorem erase_dup_map_erase_dup_eq [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : erase_dup (map f (erase_dup s)) = erase_dup (map f s) := by simp [erase_dup_ext] /- finset insert -/ /-- `ndinsert a s` is the lift of the list `insert` operation. This operation does not respect multiplicities, unlike `cons`, but it is suitable as an insert operation on `finset`. -/ def ndinsert (a : Ξ±) (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (l.insert a : multiset Ξ±)) (Ξ» s t p, quot.sound (perm_insert a p)) @[simp] theorem coe_ndinsert (a : Ξ±) (l : list Ξ±) : ndinsert a l = (insert a l : list Ξ±) := rfl @[simp] theorem ndinsert_zero (a : Ξ±) : ndinsert a 0 = a::0 := rfl @[simp] theorem ndinsert_of_mem {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ ndinsert a s = s := quot.induction_on s $ Ξ» l h, congr_arg coe $ insert_of_mem h @[simp] theorem ndinsert_of_not_mem {a : Ξ±} {s : multiset Ξ±} : a βˆ‰ s β†’ ndinsert a s = a :: s := quot.induction_on s $ Ξ» l h, congr_arg coe $ insert_of_not_mem h @[simp] theorem mem_ndinsert {a b : Ξ±} {s : multiset Ξ±} : a ∈ ndinsert b s ↔ a = b ∨ a ∈ s := quot.induction_on s $ Ξ» l, mem_insert_iff @[simp] theorem le_ndinsert_self (a : Ξ±) (s : multiset Ξ±) : s ≀ ndinsert a s := quot.induction_on s $ Ξ» l, subperm_of_sublist $ sublist_of_suffix $ suffix_insert _ _ @[simp] theorem mem_ndinsert_self (a : Ξ±) (s : multiset Ξ±) : a ∈ ndinsert a s := mem_ndinsert.2 (or.inl rfl) @[simp] theorem mem_ndinsert_of_mem {a b : Ξ±} {s : multiset Ξ±} (h : a ∈ s) : a ∈ ndinsert b s := mem_ndinsert.2 (or.inr h) @[simp] theorem length_ndinsert_of_mem {a : Ξ±} [decidable_eq Ξ±] {s : multiset Ξ±} (h : a ∈ s) : card (ndinsert a s) = card s := by simp [h] @[simp] theorem length_ndinsert_of_not_mem {a : Ξ±} [decidable_eq Ξ±] {s : multiset Ξ±} (h : a βˆ‰ s) : card (ndinsert a s) = card s + 1 := by simp [h] theorem erase_dup_cons {a : Ξ±} {s : multiset Ξ±} : erase_dup (a::s) = ndinsert a (erase_dup s) := by by_cases a ∈ s; simp [h] theorem nodup_ndinsert (a : Ξ±) {s : multiset Ξ±} : nodup s β†’ nodup (ndinsert a s) := quot.induction_on s $ Ξ» l, nodup_insert theorem ndinsert_le {a : Ξ±} {s t : multiset Ξ±} : ndinsert a s ≀ t ↔ s ≀ t ∧ a ∈ t := ⟨λ h, ⟨le_trans (le_ndinsert_self _ _) h, mem_of_le h (mem_ndinsert_self _ _)⟩, Ξ» ⟨l, m⟩, if h : a ∈ s then by simp [h, l] else by rw [ndinsert_of_not_mem h, ← cons_erase m, cons_le_cons_iff, ← le_cons_of_not_mem h, cons_erase m]; exact l⟩ lemma attach_ndinsert (a : Ξ±) (s : multiset Ξ±) : (s.ndinsert a).attach = ndinsert ⟨a, mem_ndinsert_self a s⟩ (s.attach.map $ Ξ»p, ⟨p.1, mem_ndinsert_of_mem p.2⟩) := have eq : βˆ€h : βˆ€(p : {x // x ∈ s}), p.1 ∈ s, (Ξ» (p : {x // x ∈ s}), ⟨p.val, h p⟩ : {x // x ∈ s} β†’ {x // x ∈ s}) = id, from assume h, funext $ assume p, subtype.eq rfl, have βˆ€t (eq : s.ndinsert a = t), t.attach = ndinsert ⟨a, eq β–Έ mem_ndinsert_self a s⟩ (s.attach.map $ Ξ»p, ⟨p.1, eq β–Έ mem_ndinsert_of_mem p.2⟩), begin intros t ht, by_cases a ∈ s, { rw [ndinsert_of_mem h] at ht, subst ht, rw [eq, map_id, ndinsert_of_mem (mem_attach _ _)] }, { rw [ndinsert_of_not_mem h] at ht, subst ht, simp [attach_cons, h] } end, this _ rfl @[simp] theorem disjoint_ndinsert_left {a : Ξ±} {s t : multiset Ξ±} : disjoint (ndinsert a s) t ↔ a βˆ‰ t ∧ disjoint s t := iff.trans (by simp [disjoint]) disjoint_cons_left @[simp] theorem disjoint_ndinsert_right {a : Ξ±} {s t : multiset Ξ±} : disjoint s (ndinsert a t) ↔ a βˆ‰ s ∧ disjoint s t := disjoint_comm.trans $ by simp /- finset union -/ /-- `ndunion s t` is the lift of the list `union` operation. This operation does not respect multiplicities, unlike `s βˆͺ t`, but it is suitable as a union operation on `finset`. (`s βˆͺ t` would also work as a union operation on finset, but this is more efficient.) -/ def ndunion (s t : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s t (Ξ» l₁ lβ‚‚, (l₁.union lβ‚‚ : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ perm_union p₁ pβ‚‚ @[simp] theorem coe_ndunion (l₁ lβ‚‚ : list Ξ±) : @ndunion Ξ± _ l₁ lβ‚‚ = (l₁ βˆͺ lβ‚‚ : list Ξ±) := rfl @[simp] theorem zero_ndunion (s : multiset Ξ±) : ndunion 0 s = s := quot.induction_on s $ Ξ» l, rfl @[simp] theorem cons_ndunion (s t : multiset Ξ±) (a : Ξ±) : ndunion (a :: s) t = ndinsert a (ndunion s t) := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, rfl @[simp] theorem mem_ndunion {s t : multiset Ξ±} {a : Ξ±} : a ∈ ndunion s t ↔ a ∈ s ∨ a ∈ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, list.mem_union theorem le_ndunion_right (s t : multiset Ξ±) : t ≀ ndunion s t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, subperm_of_sublist $ sublist_of_suffix $ suffix_union_right _ _ theorem ndunion_le_add (s t : multiset Ξ±) : ndunion s t ≀ s + t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, subperm_of_sublist $ union_sublist_append _ _ theorem ndunion_le {s t u : multiset Ξ±} : ndunion s t ≀ u ↔ s βŠ† u ∧ t ≀ u := multiset.induction_on s (by simp) (by simp [ndinsert_le, and_comm, and.left_comm] {contextual := tt}) theorem subset_ndunion_left (s t : multiset Ξ±) : s βŠ† ndunion s t := Ξ» a h, mem_ndunion.2 $ or.inl h theorem le_ndunion_left {s} (t : multiset Ξ±) (d : nodup s) : s ≀ ndunion s t := (le_iff_subset d).2 $ subset_ndunion_left _ _ theorem ndunion_le_union (s t : multiset Ξ±) : ndunion s t ≀ s βˆͺ t := ndunion_le.2 ⟨subset_of_le (le_union_left _ _), le_union_right _ _⟩ theorem nodup_ndunion (s : multiset Ξ±) {t : multiset Ξ±} : nodup t β†’ nodup (ndunion s t) := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, list.nodup_union _ @[simp] theorem ndunion_eq_union {s t : multiset Ξ±} (d : nodup s) : ndunion s t = s βˆͺ t := le_antisymm (ndunion_le_union _ _) $ union_le (le_ndunion_left _ d) (le_ndunion_right _ _) theorem erase_dup_add (s t : multiset Ξ±) : erase_dup (s + t) = ndunion s (erase_dup t) := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ erase_dup_append _ _ /- finset inter -/ /-- `ndinter s t` is the lift of the list `∩` operation. This operation does not respect multiplicities, unlike `s ∩ t`, but it is suitable as an intersection operation on `finset`. (`s ∩ t` would also work as a union operation on finset, but this is more efficient.) -/ def ndinter (s t : multiset Ξ±) : multiset Ξ± := filter (∈ t) s @[simp] theorem coe_ndinter (l₁ lβ‚‚ : list Ξ±) : @ndinter Ξ± _ l₁ lβ‚‚ = (l₁ ∩ lβ‚‚ : list Ξ±) := rfl @[simp] theorem zero_ndinter (s : multiset Ξ±) : ndinter 0 s = 0 := rfl @[simp] theorem cons_ndinter_of_mem {a : Ξ±} (s : multiset Ξ±) {t : multiset Ξ±} (h : a ∈ t) : ndinter (a::s) t = a :: (ndinter s t) := by simp [ndinter, h] @[simp] theorem ndinter_cons_of_not_mem {a : Ξ±} (s : multiset Ξ±) {t : multiset Ξ±} (h : a βˆ‰ t) : ndinter (a::s) t = ndinter s t := by simp [ndinter, h] @[simp] theorem mem_ndinter {s t : multiset Ξ±} {a : Ξ±} : a ∈ ndinter s t ↔ a ∈ s ∧ a ∈ t := mem_filter theorem nodup_ndinter {s : multiset Ξ±} (t : multiset Ξ±) : nodup s β†’ nodup (ndinter s t) := nodup_filter _ theorem le_ndinter {s t u : multiset Ξ±} : s ≀ ndinter t u ↔ s ≀ t ∧ s βŠ† u := by simp [ndinter, le_filter, subset_iff] theorem ndinter_le_left (s t : multiset Ξ±) : ndinter s t ≀ s := (le_ndinter.1 (le_refl _)).1 theorem ndinter_subset_right (s t : multiset Ξ±) : ndinter s t βŠ† t := (le_ndinter.1 (le_refl _)).2 theorem ndinter_le_right {s} (t : multiset Ξ±) (d : nodup s) : ndinter s t ≀ t := (le_iff_subset $ nodup_ndinter _ d).2 (ndinter_subset_right _ _) theorem inter_le_ndinter (s t : multiset Ξ±) : s ∩ t ≀ ndinter s t := le_ndinter.2 ⟨inter_le_left _ _, subset_of_le $ inter_le_right _ _⟩ @[simp] theorem ndinter_eq_inter {s t : multiset Ξ±} (d : nodup s) : ndinter s t = s ∩ t := le_antisymm (le_inter (ndinter_le_left _ _) (ndinter_le_right _ d)) (inter_le_ndinter _ _) theorem ndinter_eq_zero_iff_disjoint {s t : multiset Ξ±} : ndinter s t = 0 ↔ disjoint s t := by rw ← subset_zero; simp [subset_iff, disjoint] end /- fold -/ section fold variables (op : Ξ± β†’ Ξ± β†’ Ξ±) [hc : is_commutative Ξ± op] [ha : is_associative Ξ± op] local notation a * b := op a b include hc ha /-- `fold op b s` folds a commutative associative operation `op` over the multiset `s`. -/ def fold : Ξ± β†’ multiset Ξ± β†’ Ξ± := foldr op (left_comm _ hc.comm ha.assoc) theorem fold_eq_foldr (b : Ξ±) (s : multiset Ξ±) : fold op b s = foldr op (left_comm _ hc.comm ha.assoc) b s := rfl @[simp] theorem coe_fold_r (b : Ξ±) (l : list Ξ±) : fold op b l = l.foldr op b := rfl theorem coe_fold_l (b : Ξ±) (l : list Ξ±) : fold op b l = l.foldl op b := (coe_foldr_swap op _ b l).trans $ by simp [hc.comm] theorem fold_eq_foldl (b : Ξ±) (s : multiset Ξ±) : fold op b s = foldl op (right_comm _ hc.comm ha.assoc) b s := quot.induction_on s $ Ξ» l, coe_fold_l _ _ _ @[simp] theorem fold_zero (b : Ξ±) : (0 : multiset Ξ±).fold op b = b := rfl @[simp] theorem fold_cons_left : βˆ€ (b a : Ξ±) (s : multiset Ξ±), (a :: s).fold op b = a * s.fold op b := foldr_cons _ _ theorem fold_cons_right (b a : Ξ±) (s : multiset Ξ±) : (a :: s).fold op b = s.fold op b * a := by simp [hc.comm] theorem fold_cons'_right (b a : Ξ±) (s : multiset Ξ±) : (a :: s).fold op b = s.fold op (b * a) := by rw [fold_eq_foldl, foldl_cons, ← fold_eq_foldl] theorem fold_cons'_left (b a : Ξ±) (s : multiset Ξ±) : (a :: s).fold op b = s.fold op (a * b) := by rw [fold_cons'_right, hc.comm] theorem fold_add (b₁ bβ‚‚ : Ξ±) (s₁ sβ‚‚ : multiset Ξ±) : (s₁ + sβ‚‚).fold op (b₁ * bβ‚‚) = s₁.fold op b₁ * sβ‚‚.fold op bβ‚‚ := multiset.induction_on sβ‚‚ (by rw [add_zero, fold_zero, ← fold_cons'_right, ← fold_cons_right op]) (by simp {contextual := tt}; cc) theorem fold_singleton (b a : Ξ±) : (a::0 : multiset Ξ±).fold op b = a * b := by simp theorem fold_distrib {f g : Ξ² β†’ Ξ±} (u₁ uβ‚‚ : Ξ±) (s : multiset Ξ²) : (s.map (Ξ»x, f x * g x)).fold op (u₁ * uβ‚‚) = (s.map f).fold op u₁ * (s.map g).fold op uβ‚‚ := multiset.induction_on s (by simp) (by simp {contextual := tt}; cc) theorem fold_hom {op' : Ξ² β†’ Ξ² β†’ Ξ²} [is_commutative Ξ² op'] [is_associative Ξ² op'] {m : Ξ± β†’ Ξ²} (hm : βˆ€x y, m (op x y) = op' (m x) (m y)) (b : Ξ±) (s : multiset Ξ±) : (s.map m).fold op' (m b) = m (s.fold op b) := multiset.induction_on s (by simp) (by simp [hm] {contextual := tt}) theorem fold_union_inter [decidable_eq Ξ±] (s₁ sβ‚‚ : multiset Ξ±) (b₁ bβ‚‚ : Ξ±) : (s₁ βˆͺ sβ‚‚).fold op b₁ * (s₁ ∩ sβ‚‚).fold op bβ‚‚ = s₁.fold op b₁ * sβ‚‚.fold op bβ‚‚ := by rw [← fold_add op, union_add_inter, fold_add op] @[simp] theorem fold_erase_dup_idem [decidable_eq Ξ±] [hi : is_idempotent Ξ± op] (s : multiset Ξ±) (b : Ξ±) : (erase_dup s).fold op b = s.fold op b := multiset.induction_on s (by simp) $ Ξ» a s IH, begin by_cases a ∈ s; simp [IH, h], show fold op b s = op a (fold op b s), rw [← cons_erase h, fold_cons_left, ← ha.assoc, hi.idempotent], end end fold theorem le_smul_erase_dup [decidable_eq Ξ±] (s : multiset Ξ±) : βˆƒ n : β„•, s ≀ n β€’ erase_dup s := ⟨(s.map (Ξ» a, count a s)).fold max 0, le_iff_count.2 $ Ξ» a, begin rw count_smul, by_cases a ∈ s, { refine le_trans _ (mul_le_mul_left _ $ count_pos.2 $ mem_erase_dup.2 h), have : count a s ≀ fold max 0 (map (Ξ» a, count a s) (a :: erase s a)); [simp [le_max_left], simpa [cons_erase h]] }, { simp [count_eq_zero.2 h, nat.zero_le] } end⟩ section sort variables (r : Ξ± β†’ Ξ± β†’ Prop) [decidable_rel r] [tr : is_trans Ξ± r] [an : is_antisymm Ξ± r] [to : is_total Ξ± r] include tr an to /-- `sort s` constructs a sorted list from the multiset `s`. (Uses merge sort algorithm.) -/ def sort (s : multiset Ξ±) : list Ξ± := quot.lift_on s (merge_sort r) $ Ξ» a b h, eq_of_sorted_of_perm tr.trans an.antisymm ((perm_merge_sort _ _).trans $ h.trans (perm_merge_sort _ _).symm) (sorted_merge_sort r to.total tr.trans _) (sorted_merge_sort r to.total tr.trans _) @[simp] theorem coe_sort (l : list Ξ±) : sort r l = merge_sort r l := rfl @[simp] theorem sort_sorted (s : multiset Ξ±) : sorted r (sort r s) := quot.induction_on s $ Ξ» l, sorted_merge_sort r to.total tr.trans _ @[simp] theorem sort_eq (s : multiset Ξ±) : ↑(sort r s) = s := quot.induction_on s $ Ξ» l, quot.sound $ perm_merge_sort _ _ end sort section pi variables [decidable_eq Ξ±] {Ξ΄ : Ξ± β†’ Type*} [βˆ€a, decidable_eq (Ξ΄ a)] open function def pi.cons (m : multiset Ξ±) (a : Ξ±) (b : Ξ΄ a) (f : Ξ a∈m, Ξ΄ a) : Ξ a'∈a::m, Ξ΄ a' := Ξ»a' ha', if h : a' = a then eq.rec b h.symm else f a' $ (mem_cons.1 ha').resolve_left h def pi.empty (Ξ΄ : Ξ± β†’ Type*) : (Ξ a∈(0:multiset Ξ±), Ξ΄ a) . lemma pi.cons_same {m : multiset Ξ±} {a : Ξ±} {b : Ξ΄ a} {f : Ξ a∈m, Ξ΄ a} (h : a ∈ a :: m) : pi.cons m a b f a h = b := dif_pos rfl lemma pi.cons_ne {m : multiset Ξ±} {a a' : Ξ±} {b : Ξ΄ a} {f : Ξ a∈m, Ξ΄ a} (h' : a' ∈ a :: m) (h : a' β‰  a) : pi.cons m a b f a' h' = f a' ((mem_cons.1 h').resolve_left h) := dif_neg h lemma pi.cons_swap {a a' : Ξ±} {b : Ξ΄ a} {b' : Ξ΄ a'} {m : multiset Ξ±} {f : Ξ a∈m, Ξ΄ a} (h : a β‰  a') : pi.cons (a' :: m) a b (pi.cons m a' b' f) == pi.cons (a :: m) a' b' (pi.cons m a b f) := begin apply hfunext, { refl }, intros a'' _ h, subst h, apply hfunext, { rw [cons_swap] }, intros ha₁ haβ‚‚ h, by_cases h₁ : a'' = a; by_cases hβ‚‚ : a'' = a'; simp [*, pi.cons_same, pi.cons_ne] at *, { subst h₁, rw [pi.cons_same, pi.cons_same] }, { subst hβ‚‚, rw [pi.cons_same, pi.cons_same] } end /-- `pi m t` constructs the Cartesian product over `t` indexed by `m`. -/ def pi (m : multiset Ξ±) (t : Ξ a, multiset (Ξ΄ a)) : multiset (Ξ a∈m, Ξ΄ a) := m.rec_on { pi.empty Ξ΄ } (Ξ»a m (p : multiset (Ξ a∈m, Ξ΄ a)), (t a).bind $ Ξ»b, p.map $ pi.cons m a b) begin intros a a' m n, by_cases eq : a = a', { subst eq }, { simp [map_bind, bind_bind (t a') (t a)], apply bind_hcongr, { rw [cons_swap a a'] }, intros b hb, apply bind_hcongr, { rw [cons_swap a a'] }, intros b' hb', apply map_hcongr, { rw [cons_swap a a'] }, intros f hf, exact pi.cons_swap eq } end @[simp] lemma pi_zero (t : Ξ a, multiset (Ξ΄ a)) : pi 0 t = {pi.empty Ξ΄} := rfl @[simp] lemma pi_cons (m : multiset Ξ±) (t : Ξ a, multiset (Ξ΄ a)) (a : Ξ±) : pi (a :: m) t = ((t a).bind $ Ξ»b, (pi m t).map $ pi.cons m a b) := rec_on_cons a m lemma injective_pi_cons {a : Ξ±} {b : Ξ΄ a} {s : multiset Ξ±} (hs : a βˆ‰ s) : function.injective (pi.cons s a b) := assume f₁ fβ‚‚ eq, funext $ assume a', funext $ assume h', have ne : a β‰  a', from assume h, hs $ h.symm β–Έ h', have a' ∈ a :: s, from mem_cons_of_mem h', calc f₁ a' h' = pi.cons s a b f₁ a' this : by rw [pi.cons_ne this ne.symm] ... = pi.cons s a b fβ‚‚ a' this : by rw [eq] ... = fβ‚‚ a' h' : by rw [pi.cons_ne this ne.symm] lemma card_pi (m : multiset Ξ±) (t : Ξ a, multiset (Ξ΄ a)) : card (pi m t) = prod (m.map $ Ξ»a, card (t a)) := multiset.induction_on m (by simp) (by simp [mul_comm] {contextual := tt}) lemma nodup_pi {s : multiset Ξ±} {t : Ξ a, multiset (Ξ΄ a)} : nodup s β†’ (βˆ€a∈s, nodup (t a)) β†’ nodup (pi s t) := multiset.induction_on s (assume _ _ , nodup_singleton _) begin assume a s ih hs ht, have has : a βˆ‰ s, by simp at hs; exact hs.1, have hs : nodup s, by simp at hs; exact hs.2, simp, split, { assume b hb, from nodup_map (injective_pi_cons has) (ih hs $ assume a' h', ht a' $ mem_cons_of_mem h') }, { apply pairwise_of_nodup _ (ht a $ mem_cons_self _ _), from assume b₁ hb₁ bβ‚‚ hbβ‚‚ neb, disjoint_map_map.2 (assume f hf g hg eq, have pi.cons s a b₁ f a (mem_cons_self _ _) = pi.cons s a bβ‚‚ g a (mem_cons_self _ _), by rw [eq], neb $ show b₁ = bβ‚‚, by rwa [pi.cons_same, pi.cons_same] at this) } end lemma mem_pi (m : multiset Ξ±) (t : Ξ a, multiset (Ξ΄ a)) : βˆ€f:Ξ a∈m, Ξ΄ a, (f ∈ pi m t) ↔ (βˆ€a (h : a ∈ m), f a h ∈ t a) := multiset.induction_on m begin assume f, have : f = pi.empty Ξ΄, { funext a ha, exact ha.elim }, subst this, split, { intros _ a ha, exact ha.elim }, { intros, simp, exact mem_cons_self _ 0 } end begin assume a m ih f, simp [iff_def], split, { intros b hb f' hf' eq a' ha', subst eq, rw [ih] at hf', by_cases a' = a, { subst h, rw [pi.cons_same], exact hb }, { rw [pi.cons_ne _ h], exact hf' _ _ } }, { assume hf, refine ⟨f a (mem_cons_self a _), hf a (mem_cons_self a _), (Ξ»a ha, f a (mem_cons_of_mem ha)), _, _⟩, { rw [ih], assume a' h', exact hf _ _ }, { funext a' h', by_cases a' = a, { subst h, rw [pi.cons_same] }, { rw [pi.cons_ne _ h] } } } end end pi end multiset
b464fc28b39302706072e55d173f046b4c29fba3
92b50235facfbc08dfe7f334827d47281471333b
/library/classical.lean
fb497f98fe5f00877ff2baac4e739d6ed244ef51
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
248
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad The standard library together with the classical axioms. -/ import standard logic.axioms.classical
032de2710fc77d29fb279958861a67cfab021ecd
56e5b79a7ab4f2c52e6eb94f76d8100a25273cf3
/src/evaluation/greedy/baseline.lean
695aa717f4179d2837af334809a8adcd2b844424
[ "Apache-2.0" ]
permissive
DyeKuu/lean-tpe-public
3a9968f286ca182723ef7e7d97e155d8cb6b1e70
750ade767ab28037e80b7a80360d213a875038f8
refs/heads/master
1,682,842,633,115
1,621,330,793,000
1,621,330,793,000
368,475,816
0
0
Apache-2.0
1,621,330,745,000
1,621,330,744,000
null
UTF-8
Lean
false
false
807
lean
import evaluation_step import backends.greedy.baseline section main -- use this version for testing using tidy_greedy_proof_search meta def main : io unit := do { args ← io.cmdline_args, decls_file ← lift_option $ args.nth 0 | io.fail "must supply decls_file as first argument", dest ← lift_option $ args.nth 1 | io.fail "must supply dest as second argument", fuel ← string.to_nat <$> (lift_option $ args.nth 2) | io.fail "must supply fuel", tac_timeout ← string.to_nat <$> (lift_option $ args.nth 3) | io.fail "tac_timeout", global_timeout ← string.to_nat <$> (lift_option $ args.nth 4) | io.fail "global_timeout", evaluation_harness_from_decls_file (baseline.tidy_greedy_proof_search_core fuel) decls_file dest global_timeout (pure {tac_timeout := tac_timeout}) } end main
df27d621b73974760257c12e18c21b410337a1c4
2102833783742d9cd20d98b382ecb3af427d8071
/library/init/meta/expr.lean
1af8cb045844b36a276fa65bd1f6b3784d920f50
[ "Apache-2.0" ]
permissive
bryangingechen/lean
b63dd2b7d2ed46c13c6226148691b7d88282e9b2
ec76117504723667bdc0dda4b93aa1b31536147d
refs/heads/master
1,592,587,806,769
1,568,328,715,000
1,568,328,715,000
196,801,202
0
0
Apache-2.0
1,563,085,022,000
1,563,085,022,000
null
UTF-8
Lean
false
false
20,983
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.level init.category.monad init.meta.rb_map universes u v open native /-- Column and line position in a Lean source file. -/ structure pos := (line : nat) (column : nat) instance : decidable_eq pos | ⟨l₁, cβ‚βŸ© ⟨lβ‚‚, cβ‚‚βŸ© := if h₁ : l₁ = lβ‚‚ then if hβ‚‚ : c₁ = cβ‚‚ then is_true (eq.rec_on h₁ (eq.rec_on hβ‚‚ rfl)) else is_false (Ξ» contra, pos.no_confusion contra (Ξ» e₁ eβ‚‚, absurd eβ‚‚ hβ‚‚)) else is_false (Ξ» contra, pos.no_confusion contra (Ξ» e₁ eβ‚‚, absurd e₁ h₁)) meta instance : has_to_format pos := ⟨λ ⟨l, c⟩, "⟨" ++ l ++ ", " ++ c ++ "⟩"⟩ /-- Auxiliary annotation for binders (Lambda and Pi). This information is only used for elaboration. The difference between `{}` and `⦃⦄` is how implicit arguments are treated that are *not* followed by explicit arguments. `{}` arguments are applied eagerly, while `⦃⦄` arguments are left partially applied: ```lean def foo {x : β„•} : β„• := x def bar ⦃x : ℕ⦄ : β„• := x #check foo -- foo : β„• #check bar -- bar : Ξ  ⦃x : ℕ⦄, β„• ``` -/ inductive binder_info /- `(x : Ξ±)` -/ | default /- `{x : Ξ±}` -/ | implicit /- `⦃x:α⦄` -/ | strict_implicit /- `[x : Ξ±]`. Should be inferred with typeclass resolution. -/ | inst_implicit /- Auxiliary internal attribute used to mark local constants representing recursive functions in recursive equations and `match` statements. -/ | aux_decl instance : has_repr binder_info := ⟨λ bi, match bi with | binder_info.default := "default" | binder_info.implicit := "implicit" | binder_info.strict_implicit := "strict_implicit" | binder_info.inst_implicit := "inst_implicit" | binder_info.aux_decl := "aux_decl" end⟩ /-- Macros are basically "promises" to build an expr by some C++ code, you can't build them in Lean. You can unfold a macro and force it to evaluate. They are used for - `sorry`. - Term placeholders (`_`) in `pexpr`s. - Expression annotations. See `expr.is_annotation`. - Meta-recursive calls. Eg: ``` meta def Y : (Ξ± β†’ Ξ±) β†’ Ξ± | f := f (Y f) ``` The `Y` that appears in `f (Y f)` is a macro. - Builtin projections: ``` structure foo := (mynat : β„•) #print foo.mynat -- @[reducible] -- def foo.mynat : foo β†’ β„• := -- Ξ» (c : foo), [foo.mynat c] ``` The thing in square brackets is a macro. - Ephemeral structures inside certain specialised C++ implemented tactics. -/ meta constant macro_def : Type /-- An expression. eg ```(4+5)```. The `elab` flag is indicates whether the `expr` has been elaborated and doesn't contain any placeholder macros. For example the equality `x = x` is represented in `expr ff` as ``app (app (const `eq _) x) x`` while in `expr tt` it is represented as ``app (app (app (const `eq _) t) x) x`` (one more argument). The VM replaces instances of this datatype with the C++ implementation. -/ meta inductive expr (elaborated : bool := tt) /- A bound variable with a de-Bruijn index. -/ | var {} : nat β†’ expr /- A type universe: `Sort u` -/ | sort {} : level β†’ expr /- A global constant. These include definitions, constants and inductive type stuff present in the environment as well as hard-coded definitions. -/ | const {} : name β†’ list level β†’ expr /- [WARNING] Do not trust the types for `mvar` and `local_const`, they are sometimes dummy values. Use `tactic.infer_type` instead. -/ /- An `mvar` is a 'hole' yet to be filled in by the elaborator or tactic state. -/ | mvar (unique : name) (pretty : name) (type : expr) : expr /- A local constant. For example, if our tactic state was `h : P ⊒ Q`, `h` would be a local constant. -/ | local_const (unique : name) (pretty : name) (bi : binder_info) (type : expr) : expr /- Function application. -/ | app : expr β†’ expr β†’ expr /- Lambda abstraction. eg ```(Ξ» a : Ξ±, x)`` -/ | lam (var_name : name) (bi : binder_info) (var_type : expr) (body : expr) : expr /- Pi type constructor. eg ```(Ξ  a : Ξ±, x)`` and ```(Ξ± β†’ Ξ²)`` -/ | pi (var_name : name) (bi : binder_info) (var_type : expr) (body : expr) : expr /- An explicit let binding. -/ | elet (var_name : name) (type : expr) (assignment : expr) (body : expr) : expr /- A macro, see the docstring for `macro_def`. The list of expressions are local constants and metavariables that the macro depends on. -/ | macro : macro_def β†’ list expr β†’ expr variable {elab : bool} meta instance : inhabited expr := ⟨expr.sort level.zero⟩ /-- Get the name of the macro definition. -/ meta constant expr.macro_def_name (d : macro_def) : name meta def expr.mk_var (n : nat) : expr := expr.var n /-- Expressions can be annotated using an annotation macro during compilation. For example, a `have x:X, from p, q` expression will be compiled to `(Ξ» x:X,q)(p)`, but nested in an annotation macro with the name `"have"`. These annotations have no real semantic meaning, but are useful for helping Lean's pretty printer. -/ meta constant expr.is_annotation : expr elab β†’ option (name Γ— expr elab) /-- Remove all macro annotations from the given `expr`. -/ meta def expr.erase_annotations : expr elab β†’ expr elab | e := match e.is_annotation with | some (_, a) := expr.erase_annotations a | none := e end /-- Compares expressions, including binder names. -/ meta constant expr.has_decidable_eq : decidable_eq expr attribute [instance] expr.has_decidable_eq /-- Compares expressions while ignoring binder names. -/ meta constant expr.alpha_eqv : expr β†’ expr β†’ bool notation a ` =ₐ `:50 b:50 := expr.alpha_eqv a b = bool.tt protected meta constant expr.to_string : expr elab β†’ string meta instance : has_to_string (expr elab) := ⟨expr.to_string⟩ meta instance : has_to_format (expr elab) := ⟨λ e, e.to_string⟩ /-- Coercion for letting users write (f a) instead of (expr.app f a) -/ meta instance : has_coe_to_fun (expr elab) := { F := Ξ» e, expr elab β†’ expr elab, coe := Ξ» e, expr.app e } /-- Each expression created by Lean carries a hash. This is calculated upon creation of the expression. Two structurally equal expressions will have the same hash. -/ meta constant expr.hash : expr β†’ nat /-- Compares expressions, ignoring binder names, and sorting by hash. -/ meta constant expr.lt : expr β†’ expr β†’ bool /-- Compares expressions, ignoring binder names. -/ meta constant expr.lex_lt : expr β†’ expr β†’ bool /-- `expr.fold e a f`: Traverses each subexpression of `e`. The `nat` passed to the folder `f` is the binder depth. -/ meta constant expr.fold {Ξ± : Type} : expr β†’ Ξ± β†’ (expr β†’ nat β†’ Ξ± β†’ Ξ±) β†’ Ξ± /-- `expr.replace e f` Traverse over an expr `e` with a function `f` which can decide to replace subexpressions or not. For each subexpression `s` in the expression tree, `f s n` is called where `n` is how many binders are present above the given subexpression `s`. If `f s n` returns `none`, the children of `s` will be traversed. Otherwise if `some s'` is returned, `s'` will replace `s` and this subexpression will not be traversed further. -/ meta constant expr.replace : expr β†’ (expr β†’ nat β†’ option expr) β†’ expr /-- `abstract_local e n` replaces each instance of the local constant with unique (not pretty) name `n` in `e` with a de-Bruijn variable. -/ meta constant expr.abstract_local : expr β†’ name β†’ expr /-- Multi version of `abstract_local`. Note that the given expression will only be traversed once, so this is not the same as `list.foldl expr.abstract_local`.-/ meta constant expr.abstract_locals : expr β†’ list name β†’ expr /-- `abstract e x` Abstracts the expression `e` over the local constant `x`. -/ meta def expr.abstract : expr β†’ expr β†’ expr | e (expr.local_const n m bi t) := e.abstract_local n | e _ := e /-- Expressions depend on `level`s, and these may depend on universe parameters which have names. `instantiate_univ_params e [(n₁,l₁), ...]` will traverse `e` and replace any universe parameters with name `nα΅’` with the corresponding level `lα΅’`. -/ meta constant expr.instantiate_univ_params : expr β†’ list (name Γ— level) β†’ expr /-- `instantiate_var a b` takes the 0th de-Bruijn variable in `a` and replaces each occurrence with `b`. -/ meta constant expr.instantiate_var : expr β†’ expr β†’ expr /-- ``instantiate_vars `(#0 #1 #2) [x,y,z] = `(%%x %%y %%z)`` -/ meta constant expr.instantiate_vars : expr β†’ list expr β†’ expr /-- Perform beta-reduction if the left expression is a lambda. Ie: ``expr.subst | `(Ξ» x, %%Y) Z := Y[x/Z] | X Z := X``-/ protected meta constant expr.subst : expr elab β†’ expr elab β†’ expr elab /-- `has_var e` returns true iff e has free variables. -/ meta constant expr.has_var : expr β†’ bool /-- `has_var_idx e n` returns true iff `e` has a free variable with de-Bruijn index `n`. -/ meta constant expr.has_var_idx : expr β†’ nat β†’ bool /-- `has_local e` returns true if `e` contains a local constant. -/ meta constant expr.has_local : expr β†’ bool /-- `has_meta_var e` returns true iff `e` contains a metavariable. -/ meta constant expr.has_meta_var : expr β†’ bool /-- `lower_vars e s d` lowers the free variables >= s in `e` by `d`. Note that this can cause variable clashes. examples: - ``lower_vars `(#2 #1 #0) 1 1 = `(#1 #0 #0)`` - ``lower_vars `(Ξ» x, #2 #1 #0) 1 1 = `(Ξ» x, #1 #1 #0 )`` -/ meta constant expr.lower_vars : expr β†’ nat β†’ nat β†’ expr /-- Lifts free variables. `lift_vars e s d` will lift all free variables with index `β‰₯ s` in `e` by `d`. -/ meta constant expr.lift_vars : expr β†’ nat β†’ nat β†’ expr /-- Get the position of the given expression in the Lean source file, if anywhere. -/ protected meta constant expr.pos : expr elab β†’ option pos /-- `copy_pos_info src tgt` copies position information from `src` to `tgt`. -/ meta constant expr.copy_pos_info : expr β†’ expr β†’ expr /-- Returns `some n` when the given expression is a constant with the name `..._cnstr.n` ``` is_internal_cnstr : expr β†’ option unsigned |(const (mk_numeral n (mk_string "_cnstr" _)) _) := some n |_ := none ``` [NOTE] This is not used anywhere in core Lean. -/ meta constant expr.is_internal_cnstr : expr β†’ option unsigned /-- There is a macro called a "nat_value_macro" holding a natural number which are used during compilation. This function extracts that to a natural number. [NOTE] This is not used anywhere in Lean. -/ meta constant expr.get_nat_value : expr β†’ option nat /-- Get a list of all of the universe parameters that the given expression depends on. -/ meta constant expr.collect_univ_params : expr β†’ list name /-- `occurs e t` returns `tt` iff `e` occurs in `t` up to Ξ±-equivalence. Purely structural: no unification or definitional equality. -/ meta constant expr.occurs : expr β†’ expr β†’ bool /-- Returns true if any of the names in the given `name_set` are present in the given `expr`. -/ meta constant expr.has_local_in : expr β†’ name_set β†’ bool /-- (reflected a) is a special opaque container for a closed `expr` representing `a`. It can only be obtained via type class inference, which will use the representation of `a` in the calling context. Local constants in the representation are replaced by nested inference of `reflected` instances. The quotation expression `` `(a) `` (outside of patterns) is equivalent to `reflect a` and thus can be used as an explicit way of inferring an instance of `reflected a`. -/ @[class] meta def reflected {Ξ± : Sort u} : Ξ± β†’ Type := Ξ» _, expr @[inline] meta def reflected.to_expr {Ξ± : Sort u} {a : Ξ±} : reflected a β†’ expr := id @[inline] meta def reflected.subst {Ξ± : Sort v} {Ξ² : Ξ± β†’ Sort u} {f : Ξ  a : Ξ±, Ξ² a} {a : Ξ±} : reflected f β†’ reflected a β†’ reflected (f a) := Ξ» ef ea, match ef with | (expr.lam _ _ _ _) := expr.subst ef ea | _ := expr.app ef ea end attribute [irreducible] reflected reflected.subst reflected.to_expr @[instance] protected meta constant expr.reflect (e : expr elab) : reflected e @[instance] protected meta constant string.reflect (s : string) : reflected s @[inline] meta instance {Ξ± : Sort u} (a : Ξ±) : has_coe (reflected a) expr := ⟨reflected.to_expr⟩ protected meta def reflect {Ξ± : Sort u} (a : Ξ±) [h : reflected a] : reflected a := h meta instance {Ξ±} (a : Ξ±) : has_to_format (reflected a) := ⟨λ h, to_fmt h.to_expr⟩ namespace expr open decidable meta def expr.lt_prop (a b : expr) : Prop := expr.lt a b = tt meta instance : decidable_rel expr.lt_prop := Ξ» a b, bool.decidable_eq _ _ /-- Compares expressions, ignoring binder names, and sorting by hash. -/ meta instance : has_lt expr := ⟨ expr.lt_prop ⟩ meta def mk_true : expr := const `true [] meta def mk_false : expr := const `false [] /-- Returns the sorry macro with the given type. -/ meta constant mk_sorry (type : expr) : expr /-- Checks whether e is sorry, and returns its type. -/ meta constant is_sorry (e : expr) : option expr /-- Replace each instance of the local constant with name `n` by the expression `s` in `e`. -/ meta def instantiate_local (n : name) (s : expr) (e : expr) : expr := instantiate_var (abstract_local e n) s meta def instantiate_locals (s : list (name Γ— expr)) (e : expr) : expr := instantiate_vars (abstract_locals e (list.reverse (list.map prod.fst s))) (list.map prod.snd s) meta def is_var : expr β†’ bool | (var _) := tt | _ := ff meta def app_of_list : expr β†’ list expr β†’ expr | f [] := f | f (p::ps) := app_of_list (f p) ps meta def is_app : expr β†’ bool | (app f a) := tt | e := ff meta def app_fn : expr β†’ expr | (app f a) := f | a := a meta def app_arg : expr β†’ expr | (app f a) := a | a := a meta def get_app_fn : expr elab β†’ expr elab | (app f a) := get_app_fn f | a := a meta def get_app_num_args : expr β†’ nat | (app f a) := get_app_num_args f + 1 | e := 0 meta def get_app_args_aux : list expr β†’ expr β†’ list expr | r (app f a) := get_app_args_aux (a::r) f | r e := r meta def get_app_args : expr β†’ list expr := get_app_args_aux [] meta def mk_app : expr β†’ list expr β†’ expr | e [] := e | e (x::xs) := mk_app (e x) xs meta def mk_binding (ctor : name β†’ binder_info β†’ expr β†’ expr β†’ expr) (e : expr) : Ξ  (l : expr), expr | (local_const n pp_n bi ty) := ctor pp_n bi ty (e.abstract_local n) | _ := e /-- (bind_pi e l) abstracts and pi-binds the local `l` in `e` -/ meta def bind_pi := mk_binding pi /-- (bind_lambda e l) abstracts and lambda-binds the local `l` in `e` -/ meta def bind_lambda := mk_binding lam meta def ith_arg_aux : expr β†’ nat β†’ expr | (app f a) 0 := a | (app f a) (n+1) := ith_arg_aux f n | e _ := e meta def ith_arg (e : expr) (i : nat) : expr := ith_arg_aux e (get_app_num_args e - i - 1) meta def const_name : expr elab β†’ name | (const n ls) := n | e := name.anonymous meta def is_constant : expr elab β†’ bool | (const n ls) := tt | e := ff meta def is_local_constant : expr β†’ bool | (local_const n m bi t) := tt | e := ff meta def local_uniq_name : expr β†’ name | (local_const n m bi t) := n | e := name.anonymous meta def local_pp_name : expr elab β†’ name | (local_const x n bi t) := n | e := name.anonymous meta def local_type : expr elab β†’ expr elab | (local_const _ _ _ t) := t | e := e meta def is_aux_decl : expr β†’ bool | (local_const _ _ binder_info.aux_decl _) := tt | _ := ff meta def is_constant_of : expr elab β†’ name β†’ bool | (const n₁ ls) nβ‚‚ := n₁ = nβ‚‚ | e n := ff meta def is_app_of (e : expr) (n : name) : bool := is_constant_of (get_app_fn e) n /-- The same as `is_app_of` but must also have exactly `n` arguments. -/ meta def is_napp_of (e : expr) (c : name) (n : nat) : bool := is_app_of e c ∧ get_app_num_args e = n meta def is_false : expr β†’ bool | `(false) := tt | _ := ff meta def is_not : expr β†’ option expr | `(not %%a) := some a | `(%%a β†’ false) := some a | e := none meta def is_and : expr β†’ option (expr Γ— expr) | `(and %%Ξ± %%Ξ²) := some (Ξ±, Ξ²) | _ := none meta def is_or : expr β†’ option (expr Γ— expr) | `(or %%Ξ± %%Ξ²) := some (Ξ±, Ξ²) | _ := none meta def is_iff : expr β†’ option (expr Γ— expr) | `((%%a : Prop) ↔ %%b) := some (a, b) | _ := none meta def is_eq : expr β†’ option (expr Γ— expr) | `((%%a : %%_) = %%b) := some (a, b) | _ := none meta def is_ne : expr β†’ option (expr Γ— expr) | `((%%a : %%_) β‰  %%b) := some (a, b) | _ := none meta def is_bin_arith_app (e : expr) (op : name) : option (expr Γ— expr) := if is_napp_of e op 4 then some (app_arg (app_fn e), app_arg e) else none meta def is_lt (e : expr) : option (expr Γ— expr) := is_bin_arith_app e ``has_lt.lt meta def is_gt (e : expr) : option (expr Γ— expr) := is_bin_arith_app e ``gt meta def is_le (e : expr) : option (expr Γ— expr) := is_bin_arith_app e ``has_le.le meta def is_ge (e : expr) : option (expr Γ— expr) := is_bin_arith_app e ``ge meta def is_heq : expr β†’ option (expr Γ— expr Γ— expr Γ— expr) | `(@heq %%Ξ± %%a %%Ξ² %%b) := some (Ξ±, a, Ξ², b) | _ := none meta def is_lambda : expr β†’ bool | (lam _ _ _ _) := tt | e := ff meta def is_pi : expr β†’ bool | (pi _ _ _ _) := tt | e := ff meta def is_arrow : expr β†’ bool | (pi _ _ _ b) := bnot (has_var b) | e := ff meta def is_let : expr β†’ bool | (elet _ _ _ _) := tt | e := ff meta def binding_name : expr β†’ name | (pi n _ _ _) := n | (lam n _ _ _) := n | e := name.anonymous meta def binding_info : expr β†’ binder_info | (pi _ bi _ _) := bi | (lam _ bi _ _) := bi | e := binder_info.default meta def binding_domain : expr β†’ expr | (pi _ _ d _) := d | (lam _ _ d _) := d | e := e meta def binding_body : expr β†’ expr | (pi _ _ _ b) := b | (lam _ _ _ b) := b | e := e meta def is_macro : expr β†’ bool | (macro d a) := tt | e := ff meta def is_numeral : expr β†’ bool | `(@has_zero.zero %%Ξ± %%s) := tt | `(@has_one.one %%Ξ± %%s) := tt | `(@bit0 %%Ξ± %%s %%v) := is_numeral v | `(@bit1 %%Ξ± %%s₁ %%sβ‚‚ %%v) := is_numeral v | _ := ff meta def imp (a b : expr) : expr := pi `_ binder_info.default a b /-- `lambdas cs e` lambda binds `e` with each of the local constants in `cs`. -/ meta def lambdas : list expr β†’ expr β†’ expr | (local_const uniq pp info t :: es) f := lam pp info t (abstract_local (lambdas es f) uniq) | _ f := f /-- Same as `expr.lambdas` but with `pi`. -/ meta def pis : list expr β†’ expr β†’ expr | (local_const uniq pp info t :: es) f := pi pp info t (abstract_local (pis es f) uniq) | _ f := f meta def extract_opt_auto_param : expr β†’ expr | `(@opt_param %%t _) := extract_opt_auto_param t | `(@auto_param %%t _) := extract_opt_auto_param t | e := e open format private meta def p := Ξ» xs, paren (format.join (list.intersperse " " xs)) meta def to_raw_fmt : expr elab β†’ format | (var n) := p ["var", to_fmt n] | (sort l) := p ["sort", to_fmt l] | (const n ls) := p ["const", to_fmt n, to_fmt ls] | (mvar n m t) := p ["mvar", to_fmt n, to_fmt m, to_raw_fmt t] | (local_const n m bi t) := p ["local_const", to_fmt n, to_fmt m, to_raw_fmt t] | (app e f) := p ["app", to_raw_fmt e, to_raw_fmt f] | (lam n bi e t) := p ["lam", to_fmt n, repr bi, to_raw_fmt e, to_raw_fmt t] | (pi n bi e t) := p ["pi", to_fmt n, repr bi, to_raw_fmt e, to_raw_fmt t] | (elet n g e f) := p ["elet", to_fmt n, to_raw_fmt g, to_raw_fmt e, to_raw_fmt f] | (macro d args) := sbracket (format.join (list.intersperse " " ("macro" :: to_fmt (macro_def_name d) :: args.map to_raw_fmt))) /-- Fold an accumulator `a` over each subexpression in the expression `e`. The `nat` passed to `fn` is the number of binders above the subexpression. -/ meta def mfold {Ξ± : Type} {m : Type β†’ Type} [monad m] (e : expr) (a : Ξ±) (fn : expr β†’ nat β†’ Ξ± β†’ m Ξ±) : m Ξ± := fold e (return a) (Ξ» e n a, a >>= fn e n) end expr /-- An dictionary from `data` to expressions. -/ @[reducible] meta def expr_map (data : Type) := rb_map expr data namespace expr_map export native.rb_map (hiding mk) meta def mk (data : Type) : expr_map data := rb_map.mk expr data end expr_map meta def mk_expr_map {data : Type} : expr_map data := expr_map.mk data @[reducible] meta def expr_set := rb_set expr meta def mk_expr_set : expr_set := mk_rb_set
daa01db72778a61b956c93499558daff20c7c0c5
abd85493667895c57a7507870867b28124b3998f
/src/data/real/cardinality.lean
d80b7ff372766984f43058cbd89351c4a353c9fb
[ "Apache-2.0" ]
permissive
pechersky/mathlib
d56eef16bddb0bfc8bc552b05b7270aff5944393
f1df14c2214ee114c9738e733efd5de174deb95d
refs/heads/master
1,666,714,392,571
1,591,747,567,000
1,591,747,567,000
270,557,274
0
0
Apache-2.0
1,591,597,975,000
1,591,597,974,000
null
UTF-8
Lean
false
false
4,897
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn The cardinality of the reals. -/ import set_theory.ordinal import analysis.specific_limits import data.rat.denumerable open nat set noncomputable theory namespace cardinal variables {c : ℝ} {f g : β„• β†’ bool} {n : β„•} def cantor_function_aux (c : ℝ) (f : β„• β†’ bool) (n : β„•) : ℝ := cond (f n) (c ^ n) 0 @[simp] lemma cantor_function_aux_tt (h : f n = tt) : cantor_function_aux c f n = c ^ n := by simp [cantor_function_aux, h] @[simp] lemma cantor_function_aux_ff (h : f n = ff) : cantor_function_aux c f n = 0 := by simp [cantor_function_aux, h] lemma cantor_function_aux_nonneg (h : 0 ≀ c) : 0 ≀ cantor_function_aux c f n := by { cases h' : f n; simp [h'], apply pow_nonneg h } lemma cantor_function_aux_eq (h : f n = g n) : cantor_function_aux c f n = cantor_function_aux c g n := by simp [cantor_function_aux, h] lemma cantor_function_aux_succ (f : β„• β†’ bool) : (Ξ» n, cantor_function_aux c f (n + 1)) = Ξ» n, c * cantor_function_aux c (Ξ» n, f (n + 1)) n := by { ext n, cases h : f (n + 1); simp [h, pow_succ] } lemma summable_cantor_function (f : β„• β†’ bool) (h1 : 0 ≀ c) (h2 : c < 1) : summable (cantor_function_aux c f) := begin apply (summable_geometric_of_lt_1 h1 h2).summable_of_eq_zero_or_self, intro n, cases h : f n; simp [h] end def cantor_function (c : ℝ) (f : β„• β†’ bool) : ℝ := βˆ‘' n, cantor_function_aux c f n lemma cantor_function_le (h1 : 0 ≀ c) (h2 : c < 1) (h3 : βˆ€ n, f n β†’ g n) : cantor_function c f ≀ cantor_function c g := begin apply tsum_le_tsum _ (summable_cantor_function f h1 h2) (summable_cantor_function g h1 h2), intro n, cases h : f n, simp [h, cantor_function_aux_nonneg h1], replace h3 : g n = tt := h3 n h, simp [h, h3] end lemma cantor_function_succ (f : β„• β†’ bool) (h1 : 0 ≀ c) (h2 : c < 1) : cantor_function c f = cond (f 0) 1 0 + c * cantor_function c (Ξ» n, f (n+1)) := begin rw [cantor_function, tsum_eq_zero_add (summable_cantor_function f h1 h2)], rw [cantor_function_aux_succ, tsum_mul_left _ (summable_cantor_function _ h1 h2)], refl end lemma increasing_cantor_function (h1 : 0 < c) (h2 : c < 1 / 2) {n : β„•} {f g : β„• β†’ bool} (hn : βˆ€(k < n), f k = g k) (fn : f n = ff) (gn : g n = tt) : cantor_function c f < cantor_function c g := begin have h3 : c < 1, { apply lt_trans h2, norm_num }, induction n with n ih generalizing f g, { let f_max : β„• β†’ bool := Ξ» n, nat.rec ff (Ξ» _ _, tt) n, have hf_max : βˆ€n, f n β†’ f_max n, { intros n hn, cases n, rw [fn] at hn, contradiction, apply rfl }, let g_min : β„• β†’ bool := Ξ» n, nat.rec tt (Ξ» _ _, ff) n, have hg_min : βˆ€n, g_min n β†’ g n, { intros n hn, cases n, rw [gn], apply rfl, contradiction }, apply lt_of_le_of_lt (cantor_function_le (le_of_lt h1) h3 hf_max), apply lt_of_lt_of_le _ (cantor_function_le (le_of_lt h1) h3 hg_min), have : c / (1 - c) < 1, { rw [div_lt_one_iff_lt, lt_sub_iff_add_lt], { convert add_lt_add h2 h2, norm_num }, rwa sub_pos }, convert this, { rw [cantor_function_succ _ (le_of_lt h1) h3, div_eq_mul_inv, ←tsum_geometric_of_lt_1 (le_of_lt h1) h3], apply zero_add }, { apply tsum_eq_single 0, intros n hn, cases n, contradiction, refl, apply_instance }}, rw [cantor_function_succ f (le_of_lt h1) h3, cantor_function_succ g (le_of_lt h1) h3], rw [hn 0 $ zero_lt_succ n], apply add_lt_add_left, rw mul_lt_mul_left h1, exact ih (Ξ» k hk, hn _ $ succ_lt_succ hk) fn gn end lemma injective_cantor_function (h1 : 0 < c) (h2 : c < 1 / 2) : function.injective (cantor_function c) := begin intros f g hfg, classical, by_contra h, revert hfg, have : βˆƒn, f n β‰  g n, { rw [←not_forall], intro h', apply h, ext, apply h' }, let n := nat.find this, have hn : βˆ€ (k : β„•), k < n β†’ f k = g k, { intros k hk, apply of_not_not, exact nat.find_min this hk }, cases fn : f n, { apply ne_of_lt, refine increasing_cantor_function h1 h2 hn fn _, apply eq_tt_of_not_eq_ff, rw [←fn], apply ne.symm, exact nat.find_spec this }, { apply ne_of_gt, refine increasing_cantor_function h1 h2 (Ξ» k hk, (hn k hk).symm) _ fn, apply eq_ff_of_not_eq_tt, rw [←fn], apply ne.symm, exact nat.find_spec this } end lemma mk_real : mk ℝ = 2 ^ omega.{0} := begin apply le_antisymm, { dsimp [real], apply le_trans mk_quotient_le, apply le_trans (mk_subtype_le _), rw [←power_def, mk_nat, mk_rat, power_self_eq (le_refl _)] }, { convert mk_le_of_injective (injective_cantor_function _ _), rw [←power_def, mk_bool, mk_nat], exact 1 / 3, norm_num, norm_num } end lemma not_countable_real : Β¬ countable (set.univ : set ℝ) := by { rw [countable_iff, not_le, mk_univ, mk_real], apply cantor } end cardinal
1f0a174813b2c671a9a5b03f8ed4d3d75d3e21b0
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/data/list/intervals.lean
db7221f3d85382701b9e6f34d7684b052fc57955
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,951
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.list.range import data.list.bag_inter /-! # Intervals in β„• This file defines intervals of naturals. `list.Ico m n` is the list of integers greater than `m` and strictly less than `n`. ## TODO - Define `Ioo` and `Icc`, state basic lemmas about them. - Also do the versions for integers? - One could generalise even further, defining 'locally finite partial orders', for which `set.Ico a b` is `[finite]`, and 'locally finite total orders', for which there is a list model. - Once the above is done, get rid of `data.int.range` (and maybe `list.range'`?). -/ open nat namespace list /-- `Ico n m` is the list of natural numbers `n ≀ x < m`. (Ico stands for "interval, closed-open".) See also `data/set/intervals.lean` for `set.Ico`, modelling intervals in general preorders, and `multiset.Ico` and `finset.Ico` for `n ≀ x < m` as a multiset or as a finset. -/ def Ico (n m : β„•) : list β„• := range' n (m - n) namespace Ico theorem zero_bot (n : β„•) : Ico 0 n = range n := by rw [Ico, nat.sub_zero, range_eq_range'] @[simp] theorem length (n m : β„•) : length (Ico n m) = m - n := by dsimp [Ico]; simp only [length_range'] theorem pairwise_lt (n m : β„•) : pairwise (<) (Ico n m) := by dsimp [Ico]; simp only [pairwise_lt_range'] theorem nodup (n m : β„•) : nodup (Ico n m) := by dsimp [Ico]; simp only [nodup_range'] @[simp] theorem mem {n m l : β„•} : l ∈ Ico n m ↔ n ≀ l ∧ l < m := suffices n ≀ l ∧ l < n + (m - n) ↔ n ≀ l ∧ l < m, by simp [Ico, this], begin cases le_total n m with hnm hmn, { rw [nat.add_sub_of_le hnm] }, { rw [nat.sub_eq_zero_of_le hmn, add_zero], exact and_congr_right (assume hnl, iff.intro (assume hln, (not_le_of_gt hln hnl).elim) (assume hlm, lt_of_lt_of_le hlm hmn)) } end theorem eq_nil_of_le {n m : β„•} (h : m ≀ n) : Ico n m = [] := by simp [Ico, nat.sub_eq_zero_of_le h] theorem map_add (n m k : β„•) : (Ico n m).map ((+) k) = Ico (n + k) (m + k) := by rw [Ico, Ico, map_add_range', nat.add_sub_add_right, add_comm n k] theorem map_sub (n m k : β„•) (h₁ : k ≀ n) : (Ico n m).map (Ξ» x, x - k) = Ico (n - k) (m - k) := begin by_cases hβ‚‚ : n < m, { rw [Ico, Ico], rw nat.sub_sub_sub_cancel_right h₁, rw [map_sub_range' _ _ _ h₁] }, { simp at hβ‚‚, rw [eq_nil_of_le hβ‚‚], rw [eq_nil_of_le (nat.sub_le_sub_right hβ‚‚ _)], refl } end @[simp] theorem self_empty {n : β„•} : Ico n n = [] := eq_nil_of_le (le_refl n) @[simp] theorem eq_empty_iff {n m : β„•} : Ico n m = [] ↔ m ≀ n := iff.intro (assume h, nat.le_of_sub_eq_zero $ by rw [← length, h]; refl) eq_nil_of_le lemma append_consecutive {n m l : β„•} (hnm : n ≀ m) (hml : m ≀ l) : Ico n m ++ Ico m l = Ico n l := begin dunfold Ico, convert range'_append _ _ _, { exact (nat.add_sub_of_le hnm).symm }, { rwa [← nat.add_sub_assoc hnm, nat.sub_add_cancel] } end @[simp] lemma inter_consecutive (n m l : β„•) : Ico n m ∩ Ico m l = [] := begin apply eq_nil_iff_forall_not_mem.2, intro a, simp only [and_imp, not_and, not_lt, list.mem_inter, list.Ico.mem], intros h₁ hβ‚‚ h₃, exfalso, exact not_lt_of_ge h₃ hβ‚‚ end @[simp] lemma bag_inter_consecutive (n m l : β„•) : list.bag_inter (Ico n m) (Ico m l) = [] := (bag_inter_nil_iff_inter_nil _ _).2 (inter_consecutive n m l) @[simp] theorem succ_singleton {n : β„•} : Ico n (n+1) = [n] := by dsimp [Ico]; simp [nat.add_sub_cancel_left] theorem succ_top {n m : β„•} (h : n ≀ m) : Ico n (m + 1) = Ico n m ++ [m] := by rwa [← succ_singleton, append_consecutive]; exact nat.le_succ _ theorem eq_cons {n m : β„•} (h : n < m) : Ico n m = n :: Ico (n + 1) m := by rw [← append_consecutive (nat.le_succ n) h, succ_singleton]; refl @[simp] theorem pred_singleton {m : β„•} (h : 0 < m) : Ico (m - 1) m = [m - 1] := by dsimp [Ico]; rw nat.sub_sub_self h; simp theorem chain'_succ (n m : β„•) : chain' (Ξ»a b, b = succ a) (Ico n m) := begin by_cases n < m, { rw [eq_cons h], exact chain_succ_range' _ _ }, { rw [eq_nil_of_le (le_of_not_gt h)], trivial } end @[simp] theorem not_mem_top {n m : β„•} : m βˆ‰ Ico n m := by simp; intros; refl lemma filter_lt_of_top_le {n m l : β„•} (hml : m ≀ l) : (Ico n m).filter (Ξ» x, x < l) = Ico n m := filter_eq_self.2 $ assume k hk, lt_of_lt_of_le (mem.1 hk).2 hml lemma filter_lt_of_le_bot {n m l : β„•} (hln : l ≀ n) : (Ico n m).filter (Ξ» x, x < l) = [] := filter_eq_nil.2 $ assume k hk, not_lt_of_le $ le_trans hln $ (mem.1 hk).1 lemma filter_lt_of_ge {n m l : β„•} (hlm : l ≀ m) : (Ico n m).filter (Ξ» x, x < l) = Ico n l := begin cases le_total n l with hnl hln, { rw [← append_consecutive hnl hlm, filter_append, filter_lt_of_top_le (le_refl l), filter_lt_of_le_bot (le_refl l), append_nil] }, { rw [eq_nil_of_le hln, filter_lt_of_le_bot hln] } end @[simp] lemma filter_lt (n m l : β„•) : (Ico n m).filter (Ξ» x, x < l) = Ico n (min m l) := begin cases le_total m l with hml hlm, { rw [min_eq_left hml, filter_lt_of_top_le hml] }, { rw [min_eq_right hlm, filter_lt_of_ge hlm] } end lemma filter_le_of_le_bot {n m l : β„•} (hln : l ≀ n) : (Ico n m).filter (Ξ» x, l ≀ x) = Ico n m := filter_eq_self.2 $ assume k hk, le_trans hln (mem.1 hk).1 lemma filter_le_of_top_le {n m l : β„•} (hml : m ≀ l) : (Ico n m).filter (Ξ» x, l ≀ x) = [] := filter_eq_nil.2 $ assume k hk, not_le_of_gt (lt_of_lt_of_le (mem.1 hk).2 hml) lemma filter_le_of_le {n m l : β„•} (hnl : n ≀ l) : (Ico n m).filter (Ξ» x, l ≀ x) = Ico l m := begin cases le_total l m with hlm hml, { rw [← append_consecutive hnl hlm, filter_append, filter_le_of_top_le (le_refl l), filter_le_of_le_bot (le_refl l), nil_append] }, { rw [eq_nil_of_le hml, filter_le_of_top_le hml] } end @[simp] lemma filter_le (n m l : β„•) : (Ico n m).filter (Ξ» x, l ≀ x) = Ico (max n l) m := begin cases le_total n l with hnl hln, { rw [max_eq_right hnl, filter_le_of_le hnl] }, { rw [max_eq_left hln, filter_le_of_le_bot hln] } end lemma filter_lt_of_succ_bot {n m : β„•} (hnm : n < m) : (Ico n m).filter (Ξ» x, x < n + 1) = [n] := begin have r : min m (n + 1) = n + 1 := (@inf_eq_right _ _ m (n + 1)).mpr hnm, simp [filter_lt n m (n + 1), r], end @[simp] lemma filter_le_of_bot {n m : β„•} (hnm : n < m) : (Ico n m).filter (Ξ» x, x ≀ n) = [n] := begin rw ←filter_lt_of_succ_bot hnm, exact filter_congr (Ξ» _ _, lt_succ_iff.symm), end /-- For any natural numbers n, a, and b, one of the following holds: 1. n < a 2. n β‰₯ b 3. n ∈ Ico a b -/ lemma trichotomy (n a b : β„•) : n < a ∨ b ≀ n ∨ n ∈ Ico a b := begin by_cases h₁ : n < a, { left, exact h₁ }, { right, by_cases hβ‚‚ : n ∈ Ico a b, { right, exact hβ‚‚ }, { left, simp only [Ico.mem, not_and, not_lt] at *, exact hβ‚‚ h₁ }} end end Ico end list
fa853487a502e836426c8ca6639ce32a2420b41c
a5e2e6395319779f21675263bc0e486be5bc03bd
/bench/conv_eval.lean
3bb8209a9f61190e35777067325d1992d33ee615
[ "MIT" ]
permissive
AndrasKovacs/smalltt
10f0ec55fb3f487e9dd21a740fe1a899e0cdb790
c306f727ba3c92abe6ef75013da5a5dade6b15c8
refs/heads/master
1,689,497,785,394
1,680,893,106,000
1,680,893,106,000
112,098,494
483
26
MIT
1,640,720,815,000
1,511,713,805,000
Lean
UTF-8
Lean
false
false
4,249
lean
set_option maxHeartbeats 10000000000 set_option maxRecDepth 10000000000 universe u v def CBool := βˆ€ (B : Type), B β†’ B β†’ B def ctrue : CBool := Ξ» B t f => t def cand : CBool β†’ CBool β†’ CBool := Ξ» a b B t f => a B (b B t f) f def the (A : Type u) (x : A) := x def CEq {A : Type u}(x y : A) := βˆ€ (P : A β†’ Type v), P x β†’ P y def crefl {A}{x:A} : CEq x x := Ξ» P px => px def CNat := βˆ€ (N : Type 2), (N β†’ N) β†’ N β†’ N def add : CNat β†’ CNat β†’ CNat := Ξ» a b n s z => a n s (b n s z) def mul : CNat β†’ CNat β†’ CNat := Ξ» a b N s z => a N (b N s) z def suc : CNat β†’ CNat := Ξ» a N s z => s (a N s z) def n2 : CNat := Ξ» N s z => s (s z) def n3 : CNat := Ξ» N s z => s (s (s z)) def n4 : CNat := Ξ» N s z => s (s (s (s z))) def n5 : CNat := Ξ» N s z => s (s (s (s (s z)))) def n10 := mul n2 n5 def n10b := mul n5 n2 def n15 := add n10 n5 def n15b := add n10b n5 def n18 := add n15 n3 def n18b := add n15b n3 def n19 := add n15 n4 def n19b := add n15b n4 def n20 := mul n2 n10 def n20b := mul n2 n10b def n21 := suc n20 def n21b := suc n20b def n22 := suc n21 def n22b := suc n21b def n23 := suc n22 def n23b := suc n22b def n100 := mul n10 n10 def n100b := mul n10b n10b def n10k := mul n100 n100 def n10kb := mul n100b n100b def n100k := mul n10k n10 def n100kb := mul n10kb n10b def n1M := mul n10k n100 def n1Mb := mul n10kb n100b def n5M := mul n1M n5 def n5Mb := mul n1Mb n5 def n10M := mul n5M n2 def n10Mb := mul n5Mb n2 def Tree := βˆ€ (T : Type 1), (T β†’ T β†’ T) β†’ T β†’ T def leaf : Tree := Ξ» T n l => l def node (t1 t2 : Tree) : Tree := Ξ» T n l => n (t1 T n l) (t2 T n l) def fullTree (n : CNat) : Tree := n Tree (Ξ» t => node t t) leaf def fullTree2 (n : CNat) : Tree := n Tree (Ξ» t => node t (node t leaf)) leaf -- full tree with given trees at bottom level def fullTreeWithLeaf : Tree β†’ CNat β†’ Tree := Ξ» bottom n => n Tree (Ξ» t => node t t) bottom def forceTree : Tree β†’ Bool := Ξ» t => t CBool cand ctrue _ true false -------------------------------------------------------------------------------- def t15 := fullTree n15 def t15b := fullTree n15b def t18 := fullTree n18 def t18b := fullTree n18b def t19 := fullTree n19 def t19b := fullTree n19b def t20 := fullTree n20 def t20b := fullTree n20b def t21 := fullTree n21 def t21b := fullTree n21b def t22 := fullTree n22 def t22b := fullTree n22b def t23 := fullTree n23 def t23b := fullTree n23b -- Nat conversion -------------------------------------------------------------------------------- -- def convn1M : CEq n1M n1Mb := crefl -- def convn5M : CEq n5M n5Mb := crefl -- def convn10M : CEq n10M n10Mb := crefl -- Full tree conversion -------------------------------------------------------------------------------- -- def convt15 : CEq t15 t15b := crefl -- def convt18 : CEq t18 t18b := crefl -- def convt19 : CEq t19 t19b := crefl -- def convt20 : CEq t20 t20b := crefl -- def convt21 : CEq t21 t21b := crefl -- def convt22 : CEq t22 t22b := crefl -- def convt23 : CEq t23 t23b := crefl -- Full meta-containing tree conversion -------------------------------------------------------------------------------- -- def convmt15 := the (CEq t15b (fullTreeWithLeaf _ n15)) crefl -- def convmt18 := the (CEq t18b (fullTreeWithLeaf _ n18)) crefl -- def convmt19 := the (CEq t19b (fullTreeWithLeaf _ n19)) crefl -- def convmt20 := the (CEq t20b (fullTreeWithLeaf _ n20)) crefl -- def convmt21 := the (CEq t21b (fullTreeWithLeaf _ n21)) crefl -- def convmt22 := the (CEq t22b (fullTreeWithLeaf _ n22)) crefl -- def convmt23 := the (CEq t23b (fullTreeWithLeaf _ n23)) crefl -- Full tree forcing -------------------------------------------------------------------------------- -- #reduce forceTree t15 -- #reduce forceTree t18 -- #reduce forceTree t19 -- #reduce forceTree t20 -- #reduce forceTree t21 -- #reduce forceTree t22 -- #reduce forceTree t23 -- #reduce t15 -- #reduce t18 -- #reduce t19 -- #reduce t20 -- #reduce t21 -- #reduce t22 -- #reduce t23 -- #eval forceTree t15 -- #eval forceTree t18 -- #eval forceTree t19 -- #eval forceTree t20 -- #eval forceTree t21 -- #eval forceTree t22 -- #eval forceTree t23
9658bbe1636ac3b6f0ba9db5278bd2b782a1c972
367134ba5a65885e863bdc4507601606690974c1
/src/set_theory/game/domineering.lean
67ceaa818dc4e18514744114404ca4f042998e2b
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
7,815
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import set_theory.game.state /-! # Domineering as a combinatorial game. We define the game of Domineering, played on a chessboard of arbitrary shape (possibly even disconnected). Left moves by placing a domino vertically, while Right moves by placing a domino horizontally. This is only a fragment of a full development; in order to successfully analyse positions we would need some more theorems. Most importantly, we need a general statement that allows us to discard irrelevant moves. Specifically to domineering, we need the fact that disjoint parts of the chessboard give sums of games. -/ namespace pgame namespace domineering open function /-- The embedding `(x, y) ↦ (x, y+1)`. -/ def shift_up : β„€ Γ— β„€ β†ͺ β„€ Γ— β„€ := (embedding.refl β„€).prod_map ⟨λ n, n + 1, add_left_injective 1⟩ /-- The embedding `(x, y) ↦ (x+1, y)`. -/ def shift_right : β„€ Γ— β„€ β†ͺ β„€ Γ— β„€ := embedding.prod_map ⟨λ n, n + 1, add_left_injective 1⟩ (embedding.refl β„€) /-- A Domineering board is an arbitrary finite subset of `β„€ Γ— β„€`. -/ @[derive inhabited] def board := finset (β„€ Γ— β„€) local attribute [reducible] board /-- Left can play anywhere that a square and the square below it are open. -/ def left (b : board) : finset (β„€ Γ— β„€) := b ∩ b.map shift_up /-- Right can play anywhere that a square and the square to the left are open. -/ def right (b : board) : finset (β„€ Γ— β„€) := b ∩ b.map shift_right /-- After Left moves, two vertically adjacent squares are removed from the board. -/ def move_left (b : board) (m : β„€ Γ— β„€) : board := (b.erase m).erase (m.1, m.2 - 1) /-- After Left moves, two horizontally adjacent squares are removed from the board. -/ def move_right (b : board) (m : β„€ Γ— β„€) : board := (b.erase m).erase (m.1 - 1, m.2) lemma card_of_mem_left {b : board} {m : β„€ Γ— β„€} (h : m ∈ left b) : 2 ≀ finset.card b := begin dsimp [left] at h, have w₁ : m ∈ b, { rw finset.mem_inter at h, exact h.1 }, have wβ‚‚ : (m.1, m.2 - 1) ∈ b.erase m, { simp only [finset.mem_erase], fsplit, { exact Ξ» w, pred_ne_self m.2 (congr_arg prod.snd w) }, { rw finset.mem_inter at h, have hβ‚‚ := h.2, clear h, rw finset.mem_map at hβ‚‚, rcases hβ‚‚ with ⟨m', ⟨hβ‚‚, rfl⟩⟩, dsimp [shift_up], simpa, }, }, have i₁ := finset.card_erase_lt_of_mem w₁, have iβ‚‚ := nat.lt_of_le_of_lt (nat.zero_le _) (finset.card_erase_lt_of_mem wβ‚‚), exact nat.lt_of_le_of_lt iβ‚‚ i₁, end lemma card_of_mem_right {b : board} {m : β„€ Γ— β„€} (h : m ∈ right b) : 2 ≀ finset.card b := begin dsimp [right] at h, have w₁ : m ∈ b, { rw finset.mem_inter at h, exact h.1 }, have wβ‚‚ : (m.1 - 1, m.2) ∈ b.erase m, { simp only [finset.mem_erase], fsplit, { exact Ξ» w, pred_ne_self m.1 (congr_arg prod.fst w) }, { rw finset.mem_inter at h, have hβ‚‚ := h.2, clear h, rw finset.mem_map at hβ‚‚, rcases hβ‚‚ with ⟨m', ⟨hβ‚‚, rfl⟩⟩, dsimp [shift_right], simpa, }, }, have i₁ := finset.card_erase_lt_of_mem w₁, have iβ‚‚ := nat.lt_of_le_of_lt (nat.zero_le _) (finset.card_erase_lt_of_mem wβ‚‚), exact nat.lt_of_le_of_lt iβ‚‚ i₁, end lemma move_left_card {b : board} {m : β„€ Γ— β„€} (h : m ∈ left b) : finset.card (move_left b m) + 2 = finset.card b := begin dsimp [move_left], rw finset.card_erase_of_mem, { rw finset.card_erase_of_mem, { exact nat.sub_add_cancel (card_of_mem_left h), }, { exact finset.mem_of_mem_inter_left h, } }, { apply finset.mem_erase_of_ne_of_mem, { exact Ξ» w, pred_ne_self m.2 (congr_arg prod.snd w), }, { have t := finset.mem_of_mem_inter_right h, dsimp [shift_up] at t, simp only [finset.mem_map, prod.exists] at t, rcases t with ⟨x,y,w,h⟩, rw ←h, convert w, simp, } } end lemma move_right_card {b : board} {m : β„€ Γ— β„€} (h : m ∈ right b) : finset.card (move_right b m) + 2 = finset.card b := begin dsimp [move_right], rw finset.card_erase_of_mem, { rw finset.card_erase_of_mem, { exact nat.sub_add_cancel (card_of_mem_right h), }, { exact finset.mem_of_mem_inter_left h, } }, { apply finset.mem_erase_of_ne_of_mem, { exact Ξ» w, pred_ne_self m.1 (congr_arg prod.fst w), }, { have t := finset.mem_of_mem_inter_right h, dsimp [shift_right] at t, simp only [finset.mem_map, prod.exists] at t, rcases t with ⟨x,y,w,h⟩, rw ←h, convert w, simp, } } end lemma move_left_smaller {b : board} {m : β„€ Γ— β„€} (h : m ∈ left b) : finset.card (move_left b m) / 2 < finset.card b / 2 := by simp [←move_left_card h, lt_add_one] lemma move_right_smaller {b : board} {m : β„€ Γ— β„€} (h : m ∈ right b) : finset.card (move_right b m) / 2 < finset.card b / 2 := by simp [←move_right_card h, lt_add_one] /-- The instance describing allowed moves on a Domineering board. -/ instance state : state board := { turn_bound := Ξ» s, s.card / 2, L := Ξ» s, (left s).image (move_left s), R := Ξ» s, (right s).image (move_right s), left_bound := Ξ» s t m, begin simp only [finset.mem_image, prod.exists] at m, rcases m with ⟨_, _, ⟨h, rfl⟩⟩, exact move_left_smaller h end, right_bound := Ξ» s t m, begin simp only [finset.mem_image, prod.exists] at m, rcases m with ⟨_, _, ⟨h, rfl⟩⟩, exact move_right_smaller h end, } end domineering /-- Construct a pre-game from a Domineering board. -/ def domineering (b : domineering.board) : pgame := pgame.of b /-- All games of Domineering are short, because each move removes two squares. -/ instance short_domineering (b : domineering.board) : short (domineering b) := by { dsimp [domineering], apply_instance } /-- The Domineering board with two squares arranged vertically, in which Left has the only move. -/ def domineering.one := domineering ([(0,0), (0,1)].to_finset) /-- The `L` shaped Domineering board, in which Left is exactly half a move ahead. -/ def domineering.L := domineering ([(0,2), (0,1), (0,0), (1,0)].to_finset) instance short_one : short domineering.one := by { dsimp [domineering.one], apply_instance } instance short_L : short domineering.L := by { dsimp [domineering.L], apply_instance } -- The VM can play small games successfully: -- #eval to_bool (domineering.one β‰ˆ 1) -- #eval to_bool (domineering.L + domineering.L β‰ˆ 1) -- We can check that `decidable` instances reduce as expected, -- and so our implementation of domineering is computable. -- run_cmd tactic.whnf `(by apply_instance : decidable (domineering.one ≀ 1)) >>= tactic.trace -- dec_trivial can handle most of the dictionary of small games described in [conway2001] example : domineering.one β‰ˆ 1 := dec_trivial example : domineering.L + domineering.L β‰ˆ 1 := dec_trivial example : domineering.L β‰ˆ pgame.of_lists [0] [1] := dec_trivial example : (domineering ([(0,0), (0,1), (0,2), (0,3)].to_finset) β‰ˆ 2) := dec_trivial example : (domineering ([(0,0), (0,1), (1,0), (1,1)].to_finset) β‰ˆ pgame.of_lists [1] [-1]) := dec_trivial. -- The 3x3 grid is doable, but takes a minute... -- example : -- (domineering ([(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)].to_finset) β‰ˆ -- pgame.of_lists [1] [-1]) := dec_trivial -- The 5x5 grid is actually 0, but brute-forcing this is too challenging even for the VM. -- #eval to_bool (domineering ([ -- (0,0), (0,1), (0,2), (0,3), (0,4), -- (1,0), (1,1), (1,2), (1,3), (1,4), -- (2,0), (2,1), (2,2), (2,3), (2,4), -- (3,0), (3,1), (3,2), (3,3), (3,4), -- (4,0), (4,1), (4,2), (4,3), (4,4) -- ].to_finset) β‰ˆ 0) end pgame
0e38a1a88f38cad4e58f7aca4fdc8adba79b1440
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Elab/LetRec.lean
8367fa68a2626e2e95614b997c19f5a816fbfa8f
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
5,038
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.Elab.Attributes import Lean.Elab.Binders import Lean.Elab.DeclModifiers import Lean.Elab.SyntheticMVars import Lean.Elab.DeclarationRange namespace Lean.Elab.Term open Meta structure LetRecDeclView where ref : Syntax attrs : Array Attribute shortDeclName : Name declName : Name binderIds : Array Syntax type : Expr mvar : Expr -- auxiliary metavariable used to lift the 'let rec' valStx : Syntax structure LetRecView where decls : Array LetRecDeclView body : Syntax /- group ("let " >> nonReservedSymbol "rec ") >> sepBy1 (group (optional Β«attributesΒ» >> letDecl)) ", " >> "; " >> termParser -/ private def mkLetRecDeclView (letRec : Syntax) : TermElabM LetRecView := do let decls ← letRec[1][0].getSepArgs.mapM fun (attrDeclStx : Syntax) => do let docStr? ← expandOptDocComment? attrDeclStx[0] let attrOptStx := attrDeclStx[1] let attrs ← if attrOptStx.isNone then pure #[] else elabDeclAttrs attrOptStx[0] let decl := attrDeclStx[2][0] if decl.isOfKind `Lean.Parser.Term.letPatDecl then throwErrorAt decl "patterns are not allowed in 'let rec' expressions" else if decl.isOfKind `Lean.Parser.Term.letIdDecl || decl.isOfKind `Lean.Parser.Term.letEqnsDecl then let declId := decl[0] let shortDeclName := declId.getId let currDeclName? ← getDeclName? let declName := currDeclName?.getD Name.anonymous ++ shortDeclName checkNotAlreadyDeclared declName applyAttributesAt declName attrs AttributeApplicationTime.beforeElaboration addDocString' declName docStr? addAuxDeclarationRanges declName decl declId let binders := decl[1].getArgs let typeStx := expandOptType declId decl[2] let (type, binderIds) ← elabBindersEx binders fun xs => do let type ← elabType typeStx registerCustomErrorIfMVar type typeStx "failed to infer 'let rec' declaration type" let (binderIds, xs) := xs.unzip let type ← mkForallFVars xs type pure (type, binderIds) let mvar ← mkFreshExprMVar type MetavarKind.syntheticOpaque let valStx ← if decl.isOfKind `Lean.Parser.Term.letIdDecl then pure decl[4] else liftMacroM <| expandMatchAltsIntoMatch decl decl[3] pure { ref := declId, attrs, shortDeclName, declName, binderIds, type, mvar, valStx : LetRecDeclView } else throwUnsupportedSyntax return { decls, body := letRec[3] } private partial def withAuxLocalDecls {Ξ±} (views : Array LetRecDeclView) (k : Array Expr β†’ TermElabM Ξ±) : TermElabM Ξ± := let rec loop (i : Nat) (fvars : Array Expr) : TermElabM Ξ± := if h : i < views.size then let view := views.get ⟨i, h⟩ withAuxDecl view.shortDeclName view.type view.declName fun fvar => loop (i+1) (fvars.push fvar) else k fvars loop 0 #[] private def elabLetRecDeclValues (view : LetRecView) : TermElabM (Array Expr) := view.decls.mapM fun view => do forallBoundedTelescope view.type view.binderIds.size fun xs type => do -- Add new info nodes for new fvars. The server will detect all fvars of a binder by the binder's source location. for i in [0:view.binderIds.size] do addLocalVarInfo view.binderIds[i]! xs[i]! withDeclName view.declName do let value ← elabTermEnsuringType view.valStx type mkLambdaFVars xs value private def registerLetRecsToLift (views : Array LetRecDeclView) (fvars : Array Expr) (values : Array Expr) : TermElabM Unit := do let letRecsToLiftCurr := (← get).letRecsToLift for view in views do if letRecsToLiftCurr.any fun toLift => toLift.declName == view.declName then withRef view.ref do throwError "'{view.declName}' has already been declared" let lctx ← getLCtx let localInstances ← getLocalInstances let toLift := views.mapIdx fun i view => { ref := view.ref fvarId := fvars[i]!.fvarId! attrs := view.attrs shortDeclName := view.shortDeclName declName := view.declName lctx localInstances type := view.type val := values[i]! mvarId := view.mvar.mvarId! : LetRecToLift } modify fun s => { s with letRecsToLift := toLift.toList ++ s.letRecsToLift } @[builtinTermElab Β«letrecΒ»] def elabLetRec : TermElab := fun stx expectedType? => do let view ← mkLetRecDeclView stx withAuxLocalDecls view.decls fun fvars => do for decl in view.decls, fvar in fvars do addLocalVarInfo decl.ref fvar let values ← elabLetRecDeclValues view let body ← elabTermEnsuringType view.body expectedType? registerLetRecsToLift view.decls fvars values let mvars := view.decls.map (Β·.mvar) return mkAppN (← mkLambdaFVars fvars body) mvars end Lean.Elab.Term
fd9ae1ad099749ab20d9f5ce38e3865dc1427a41
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/real/golden_ratio.lean
ccc4f579041c5856f6c71a74f1416ba24d391498
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
4,291
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Alexey Soloyev, Junyan Xu -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.real.irrational import Mathlib.data.nat.fib import Mathlib.data.matrix.notation import Mathlib.tactic.ring_exp import Mathlib.algebra.linear_recurrence import Mathlib.PostPort universes u_1 namespace Mathlib /-! # The golden ratio and its conjugate This file defines the golden ratio `Ο† := (1 + √5)/2` and its conjugate `ψ := (1 - √5)/2`, which are the two real roots of `XΒ² - X - 1`. Along with various computational facts about them, we prove their irrationality, and we link them to the Fibonacci sequence by proving Binet's formula. -/ /-- The golden ratio `Ο† := (1 + √5)/2`. -/ def golden_ratio : ℝ := (1 + real.sqrt (bit1 (bit0 1))) / bit0 1 /-- The conjugate of the golden ratio `ψ := (1 - √5)/2`. -/ def golden_conj : ℝ := (1 - real.sqrt (bit1 (bit0 1))) / bit0 1 /-- The inverse of the golden ratio is the opposite of its conjugate. -/ theorem inv_gold : golden_ratio⁻¹ = -golden_conj := sorry /-- The opposite of the golden ratio is the inverse of its conjugate. -/ theorem inv_gold_conj : golden_conj⁻¹ = -golden_ratio := sorry @[simp] theorem gold_mul_gold_conj : golden_ratio * golden_conj = -1 := sorry @[simp] theorem gold_conj_mul_gold : golden_conj * golden_ratio = -1 := eq.mpr (id (Eq._oldrec (Eq.refl (golden_conj * golden_ratio = -1)) (mul_comm golden_conj golden_ratio))) gold_mul_gold_conj @[simp] theorem gold_add_gold_conj : golden_ratio + golden_conj = 1 := sorry theorem one_sub_gold_conj : 1 - golden_ratio = golden_conj := sorry theorem one_sub_gold : 1 - golden_conj = golden_ratio := sorry @[simp] theorem gold_sub_gold_conj : golden_ratio - golden_conj = real.sqrt (bit1 (bit0 1)) := sorry @[simp] theorem gold_sq : golden_ratio ^ bit0 1 = golden_ratio + 1 := sorry @[simp] theorem gold_conj_sq : golden_conj ^ bit0 1 = golden_conj + 1 := sorry theorem gold_pos : 0 < golden_ratio := sorry theorem gold_ne_zero : golden_ratio β‰  0 := ne_of_gt gold_pos theorem one_lt_gold : 1 < golden_ratio := sorry theorem gold_conj_neg : golden_conj < 0 := sorry theorem gold_conj_ne_zero : golden_conj β‰  0 := ne_of_lt gold_conj_neg theorem neg_one_lt_gold_conj : -1 < golden_conj := eq.mpr (id (Eq._oldrec (Eq.refl (-1 < golden_conj)) (propext neg_lt))) (eq.mpr (id (Eq._oldrec (Eq.refl (-golden_conj < 1)) (Eq.symm inv_gold))) (inv_lt_one one_lt_gold)) /-! ## Irrationality -/ /-- The golden ratio is irrational. -/ theorem gold_irrational : irrational golden_ratio := sorry /-- The conjugate of the golden ratio is irrational. -/ theorem gold_conj_irrational : irrational golden_conj := sorry /-! ## Links with Fibonacci sequence -/ /-- The recurrence relation satisfied by the Fibonacci sequence. -/ def fib_rec {Ξ± : Type u_1} [comm_semiring Ξ±] : linear_recurrence Ξ± := linear_recurrence.mk (bit0 1) (matrix.vec_cons 1 (matrix.vec_cons 1 matrix.vec_empty)) /-- The characteristic polynomial of `fib_rec` is `XΒ² - (X + 1)`. -/ theorem fib_rec_char_poly_eq {Ξ² : Type u_1} [comm_ring Ξ²] : linear_recurrence.char_poly fib_rec = polynomial.X ^ bit0 1 - (polynomial.X + 1) := sorry /-- As expected, the Fibonacci sequence is a solution of `fib_rec`. -/ theorem fib_is_sol_fib_rec {Ξ± : Type u_1} [comm_semiring Ξ±] : linear_recurrence.is_solution fib_rec fun (x : β„•) => ↑(nat.fib x) := sorry /-- The geometric sequence `Ξ» n, Ο†^n` is a solution of `fib_rec`. -/ theorem geom_gold_is_sol_fib_rec : linear_recurrence.is_solution fib_rec (pow golden_ratio) := sorry /-- The geometric sequence `Ξ» n, ψ^n` is a solution of `fib_rec`. -/ theorem geom_gold_conj_is_sol_fib_rec : linear_recurrence.is_solution fib_rec (pow golden_conj) := sorry /-- Binet's formula as a function equality. -/ theorem real.coe_fib_eq' : (fun (n : β„•) => ↑(nat.fib n)) = fun (n : β„•) => (golden_ratio ^ n - golden_conj ^ n) / real.sqrt (bit1 (bit0 1)) := sorry /-- Binet's formula as a dependent equality. -/ theorem real.coe_fib_eq (n : β„•) : ↑(nat.fib n) = (golden_ratio ^ n - golden_conj ^ n) / real.sqrt (bit1 (bit0 1)) := sorry
db9e01dfdb2ff2c6c1a493719e938b2bf0f36bd2
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/quot_auto.lean
1c8743d56ddc7c8bde29484cca8639db17c2d7ec
[]
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,663
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura Quotient types. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.sigma.basic import Mathlib.Lean3Lib.init.logic import Mathlib.Lean3Lib.init.propext import Mathlib.Lean3Lib.init.data.setoid universes u v u_a u_b u_c namespace Mathlib /- We import propext here, otherwise we would need a quot.lift for propositions. -/ -- iff can now be used to do substitutions in a calculation theorem iff_subst {a : Prop} {b : Prop} {p : Prop β†’ Prop} (h₁ : a ↔ b) (hβ‚‚ : p a) : p b := propext h₁ β–Έ hβ‚‚ namespace quot axiom sound {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {a : Ξ±} {b : Ξ±} : r a b β†’ Quot.mk r a = Quot.mk r b protected theorem lift_beta {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Sort v} (f : Ξ± β†’ Ξ²) (c : βˆ€ (a b : Ξ±), r a b β†’ f a = f b) (a : Ξ±) : Quot.lift f c (Quot.mk r a) = f a := rfl protected theorem ind_beta {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Prop} (p : βˆ€ (a : Ξ±), Ξ² (Quot.mk r a)) (a : Ξ±) : Quot.ind p (Quot.mk r a) = p a := rfl protected def lift_on {Ξ± : Sort u} {Ξ² : Sort v} {r : Ξ± β†’ Ξ± β†’ Prop} (q : Quot r) (f : Ξ± β†’ Ξ²) (c : βˆ€ (a b : Ξ±), r a b β†’ f a = f b) : Ξ² := Quot.lift f c q protected theorem induction_on {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Prop} (q : Quot r) (h : βˆ€ (a : Ξ±), Ξ² (Quot.mk r a)) : Ξ² q := Quot.ind h q theorem exists_rep {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} (q : Quot r) : βˆƒ (a : Ξ±), Quot.mk r a = q := quot.induction_on q fun (a : Ξ±) => Exists.intro a rfl protected def indep {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Sort v} (f : (a : Ξ±) β†’ Ξ² (Quot.mk r a)) (a : Ξ±) : psigma Ξ² := psigma.mk (Quot.mk r a) (f a) protected theorem indep_coherent {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Sort v} (f : (a : Ξ±) β†’ Ξ² (Quot.mk r a)) (h : βˆ€ (a b : Ξ±) (p : r a b), Eq._oldrec (f a) (sound p) = f b) (a : Ξ±) (b : Ξ±) : r a b β†’ quot.indep f a = quot.indep f b := fun (e : r a b) => psigma.eq (sound e) (h a b e) protected theorem lift_indep_pr1 {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Sort v} (f : (a : Ξ±) β†’ Ξ² (Quot.mk r a)) (h : βˆ€ (a b : Ξ±) (p : r a b), Eq._oldrec (f a) (sound p) = f b) (q : Quot r) : psigma.fst (Quot.lift (quot.indep f) (quot.indep_coherent f h) q) = q := Quot.ind (fun (a : Ξ±) => Eq.refl (psigma.fst (quot.indep f a))) q protected def rec {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Sort v} (f : (a : Ξ±) β†’ Ξ² (Quot.mk r a)) (h : βˆ€ (a b : Ξ±) (p : r a b), Eq._oldrec (f a) (sound p) = f b) (q : Quot r) : Ξ² q := eq.rec_on (quot.lift_indep_pr1 f h q) (psigma.snd (Quot.lift (quot.indep f) (quot.indep_coherent f h) q)) protected def rec_on {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Sort v} (q : Quot r) (f : (a : Ξ±) β†’ Ξ² (Quot.mk r a)) (h : βˆ€ (a b : Ξ±) (p : r a b), Eq._oldrec (f a) (sound p) = f b) : Ξ² q := quot.rec f h q protected def rec_on_subsingleton {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Sort v} [h : βˆ€ (a : Ξ±), subsingleton (Ξ² (Quot.mk r a))] (q : Quot r) (f : (a : Ξ±) β†’ Ξ² (Quot.mk r a)) : Ξ² q := quot.rec f sorry q protected def hrec_on {Ξ± : Sort u} {r : Ξ± β†’ Ξ± β†’ Prop} {Ξ² : Quot r β†’ Sort v} (q : Quot r) (f : (a : Ξ±) β†’ Ξ² (Quot.mk r a)) (c : βˆ€ (a b : Ξ±), r a b β†’ f a == f b) : Ξ² q := quot.rec_on q f sorry end quot def quotient {Ξ± : Sort u} (s : setoid Ξ±) := Quot setoid.r namespace quotient protected def mk {Ξ± : Sort u} [s : setoid Ξ±] (a : Ξ±) : quotient s := Quot.mk setoid.r a def sound {Ξ± : Sort u} [s : setoid Ξ±] {a : Ξ±} {b : Ξ±} : a β‰ˆ b β†’ quotient.mk a = quotient.mk b := quot.sound protected def lift {Ξ± : Sort u} {Ξ² : Sort v} [s : setoid Ξ±] (f : Ξ± β†’ Ξ²) : (βˆ€ (a b : Ξ±), a β‰ˆ b β†’ f a = f b) β†’ quotient s β†’ Ξ² := Quot.lift f protected theorem ind {Ξ± : Sort u} [s : setoid Ξ±] {Ξ² : quotient s β†’ Prop} : (βˆ€ (a : Ξ±), Ξ² (quotient.mk a)) β†’ βˆ€ (q : quotient s), Ξ² q := Quot.ind protected def lift_on {Ξ± : Sort u} {Ξ² : Sort v} [s : setoid Ξ±] (q : quotient s) (f : Ξ± β†’ Ξ²) (c : βˆ€ (a b : Ξ±), a β‰ˆ b β†’ f a = f b) : Ξ² := quot.lift_on q f c protected theorem induction_on {Ξ± : Sort u} [s : setoid Ξ±] {Ξ² : quotient s β†’ Prop} (q : quotient s) (h : βˆ€ (a : Ξ±), Ξ² (quotient.mk a)) : Ξ² q := quot.induction_on q h theorem exists_rep {Ξ± : Sort u} [s : setoid Ξ±] (q : quotient s) : βˆƒ (a : Ξ±), quotient.mk a = q := quot.exists_rep q protected def rec {Ξ± : Sort u} [s : setoid Ξ±] {Ξ² : quotient s β†’ Sort v} (f : (a : Ξ±) β†’ Ξ² (quotient.mk a)) (h : βˆ€ (a b : Ξ±) (p : a β‰ˆ b), Eq._oldrec (f a) (sound p) = f b) (q : quotient s) : Ξ² q := quot.rec f h q protected def rec_on {Ξ± : Sort u} [s : setoid Ξ±] {Ξ² : quotient s β†’ Sort v} (q : quotient s) (f : (a : Ξ±) β†’ Ξ² (quotient.mk a)) (h : βˆ€ (a b : Ξ±) (p : a β‰ˆ b), Eq._oldrec (f a) (sound p) = f b) : Ξ² q := quot.rec_on q f h protected def rec_on_subsingleton {Ξ± : Sort u} [s : setoid Ξ±] {Ξ² : quotient s β†’ Sort v} [h : βˆ€ (a : Ξ±), subsingleton (Ξ² (quotient.mk a))] (q : quotient s) (f : (a : Ξ±) β†’ Ξ² (quotient.mk a)) : Ξ² q := quot.rec_on_subsingleton q f protected def hrec_on {Ξ± : Sort u} [s : setoid Ξ±] {Ξ² : quotient s β†’ Sort v} (q : quotient s) (f : (a : Ξ±) β†’ Ξ² (quotient.mk a)) (c : βˆ€ (a b : Ξ±), a β‰ˆ b β†’ f a == f b) : Ξ² q := quot.hrec_on q f c protected def liftβ‚‚ {Ξ± : Sort u_a} {Ξ² : Sort u_b} {Ο† : Sort u_c} [s₁ : setoid Ξ±] [sβ‚‚ : setoid Ξ²] (f : Ξ± β†’ Ξ² β†’ Ο†) (c : βˆ€ (a₁ : Ξ±) (aβ‚‚ : Ξ²) (b₁ : Ξ±) (bβ‚‚ : Ξ²), a₁ β‰ˆ b₁ β†’ aβ‚‚ β‰ˆ bβ‚‚ β†’ f a₁ aβ‚‚ = f b₁ bβ‚‚) (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) : Ο† := quotient.lift (fun (a₁ : Ξ±) => quotient.lift (f a₁) sorry qβ‚‚) sorry q₁ protected def lift_onβ‚‚ {Ξ± : Sort u_a} {Ξ² : Sort u_b} {Ο† : Sort u_c} [s₁ : setoid Ξ±] [sβ‚‚ : setoid Ξ²] (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) (f : Ξ± β†’ Ξ² β†’ Ο†) (c : βˆ€ (a₁ : Ξ±) (aβ‚‚ : Ξ²) (b₁ : Ξ±) (bβ‚‚ : Ξ²), a₁ β‰ˆ b₁ β†’ aβ‚‚ β‰ˆ bβ‚‚ β†’ f a₁ aβ‚‚ = f b₁ bβ‚‚) : Ο† := quotient.liftβ‚‚ f c q₁ qβ‚‚ protected theorem indβ‚‚ {Ξ± : Sort u_a} {Ξ² : Sort u_b} [s₁ : setoid Ξ±] [sβ‚‚ : setoid Ξ²] {Ο† : quotient s₁ β†’ quotient sβ‚‚ β†’ Prop} (h : βˆ€ (a : Ξ±) (b : Ξ²), Ο† (quotient.mk a) (quotient.mk b)) (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) : Ο† q₁ qβ‚‚ := quotient.ind (fun (a₁ : Ξ±) => quotient.ind (fun (aβ‚‚ : Ξ²) => h a₁ aβ‚‚) qβ‚‚) q₁ protected theorem induction_onβ‚‚ {Ξ± : Sort u_a} {Ξ² : Sort u_b} [s₁ : setoid Ξ±] [sβ‚‚ : setoid Ξ²] {Ο† : quotient s₁ β†’ quotient sβ‚‚ β†’ Prop} (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) (h : βˆ€ (a : Ξ±) (b : Ξ²), Ο† (quotient.mk a) (quotient.mk b)) : Ο† q₁ qβ‚‚ := quotient.ind (fun (a₁ : Ξ±) => quotient.ind (fun (aβ‚‚ : Ξ²) => h a₁ aβ‚‚) qβ‚‚) q₁ protected theorem induction_on₃ {Ξ± : Sort u_a} {Ξ² : Sort u_b} {Ο† : Sort u_c} [s₁ : setoid Ξ±] [sβ‚‚ : setoid Ξ²] [s₃ : setoid Ο†] {Ξ΄ : quotient s₁ β†’ quotient sβ‚‚ β†’ quotient s₃ β†’ Prop} (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) (q₃ : quotient s₃) (h : βˆ€ (a : Ξ±) (b : Ξ²) (c : Ο†), Ξ΄ (quotient.mk a) (quotient.mk b) (quotient.mk c)) : Ξ΄ q₁ qβ‚‚ q₃ := quotient.ind (fun (a₁ : Ξ±) => quotient.ind (fun (aβ‚‚ : Ξ²) => quotient.ind (fun (a₃ : Ο†) => h a₁ aβ‚‚ a₃) q₃) qβ‚‚) q₁ theorem exact {Ξ± : Sort u} [s : setoid Ξ±] {a : Ξ±} {b : Ξ±} : quotient.mk a = quotient.mk b β†’ a β‰ˆ b := fun (h : quotient.mk a = quotient.mk b) => eq_imp_rel h protected def rec_on_subsingletonβ‚‚ {Ξ± : Sort u_a} {Ξ² : Sort u_b} [s₁ : setoid Ξ±] [sβ‚‚ : setoid Ξ²] {Ο† : quotient s₁ β†’ quotient sβ‚‚ β†’ Sort u_c} [h : βˆ€ (a : Ξ±) (b : Ξ²), subsingleton (Ο† (quotient.mk a) (quotient.mk b))] (q₁ : quotient s₁) (qβ‚‚ : quotient sβ‚‚) (f : (a : Ξ±) β†’ (b : Ξ²) β†’ Ο† (quotient.mk a) (quotient.mk b)) : Ο† q₁ qβ‚‚ := quotient.rec_on_subsingleton q₁ fun (a : Ξ±) => quotient.rec_on_subsingleton qβ‚‚ fun (b : Ξ²) => f a b end quotient inductive eqv_gen {Ξ± : Type u} (r : Ξ± β†’ Ξ± β†’ Prop) : Ξ± β†’ Ξ± β†’ Prop where | rel : βˆ€ (x y : Ξ±), r x y β†’ eqv_gen r x y | refl : βˆ€ (x : Ξ±), eqv_gen r x x | symm : βˆ€ (x y : Ξ±), eqv_gen r x y β†’ eqv_gen r y x | trans : βˆ€ (x y z : Ξ±), eqv_gen r x y β†’ eqv_gen r y z β†’ eqv_gen r x z theorem eqv_gen.is_equivalence {Ξ± : Type u} (r : Ξ± β†’ Ξ± β†’ Prop) : equivalence (eqv_gen r) := mk_equivalence (eqv_gen r) eqv_gen.refl eqv_gen.symm eqv_gen.trans def eqv_gen.setoid {Ξ± : Type u} (r : Ξ± β†’ Ξ± β†’ Prop) : setoid Ξ± := setoid.mk (eqv_gen r) (eqv_gen.is_equivalence r) theorem quot.exact {Ξ± : Type u} (r : Ξ± β†’ Ξ± β†’ Prop) {a : Ξ±} {b : Ξ±} (H : Quot.mk r a = Quot.mk r b) : eqv_gen r a b := quotient.exact (congr_arg (Quot.lift quotient.mk fun (x y : Ξ±) (h : r x y) => quot.sound (eqv_gen.rel x y h)) H) theorem quot.eqv_gen_sound {Ξ± : Type u} {r : Ξ± β†’ Ξ± β†’ Prop} {a : Ξ±} {b : Ξ±} (H : eqv_gen r a b) : Quot.mk r a = Quot.mk r b := sorry protected instance quotient.decidable_eq {Ξ± : Sort u} {s : setoid Ξ±} [d : (a b : Ξ±) β†’ Decidable (a β‰ˆ b)] : DecidableEq (quotient s) := fun (q₁ qβ‚‚ : quotient s) => quotient.rec_on_subsingletonβ‚‚ q₁ qβ‚‚ fun (a₁ aβ‚‚ : Ξ±) => sorry end Mathlib
cc3934aaaf05037736d04597478edf9323ddc68a
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/simp6.lean
ad1064a6907cadec35c3d9b6f661fc2f1cb0cc0e
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
709
lean
theorem ex1 (a : Ξ±) (b : List Ξ±) : (a::b = []) = False := by simp theorem ex2 (a : Ξ±) (b : List Ξ±) : (a::b = []) = False := by simp theorem ex3 (x : Nat) : (if Nat.succ x = 0 then 1 else 2) = 2 := by simp theorem ex4 (x : Nat) : (if 10 = 0 then 1 else 2) = 2 := by simp theorem ex5 : (10 = 20) = False := by simp #print ex5 theorem ex6 : (if "hello" = "world" then 1 else 2) = 2 := by simp #print ex6 theorem ex7 : (if "hello" = "world" then 1 else 2) = 2 := by simp { decide := false } simp theorem ex8 : (10 + 2000 = 20) = False := by simp def fact : Nat β†’ Nat | 0 => 1 | x+1 => (x+1) * fact x theorem ex9 : (if fact 100 > 10 then true else false) = true := by simp
3a96dcbfc17fde14b31412db223b68fbf6e7ea88
e2fc96178628c7451e998a0db2b73877d0648be5
/src/classes/context_sensitive/basics/toolbox.lean
6c185d39d5d80b32d755e2b64c3f1e554146f690
[ "BSD-2-Clause" ]
permissive
madvorak/grammars
cd324ae19b28f7b8be9c3ad010ef7bf0fabe5df2
1447343a45fcb7821070f1e20b57288d437323a6
refs/heads/main
1,692,383,644,884
1,692,032,429,000
1,692,032,429,000
453,948,141
7
0
null
null
null
null
UTF-8
Lean
false
false
1,188
lean
import classes.context_sensitive.basics.definition variables {T : Type} {g : CS_grammar T} /-- The relation `CS_derives` is reflexive. -/ lemma CS_deri_self {w : list (symbol T g.nt)} : CS_derives g w w := relation.refl_trans_gen.refl lemma CS_deri_of_tran {v w : list (symbol T g.nt)} : CS_transforms g v w β†’ CS_derives g v w := relation.refl_trans_gen.single /-- The relation `CS_derives` is transitive. -/ lemma CS_deri_of_deri_deri {u v w : list (symbol T g.nt)} (huv : CS_derives g u v) (hvw : CS_derives g v w) : CS_derives g u w := relation.refl_trans_gen.trans huv hvw lemma CS_deri_of_deri_tran {u v w : list (symbol T g.nt)} (huv : CS_derives g u v) (hvw : CS_transforms g v w) : CS_derives g u w := CS_deri_of_deri_deri huv (CS_deri_of_tran hvw) lemma CS_deri_of_tran_deri {u v w : list (symbol T g.nt)} (huv : CS_transforms g u v) (hvw : CS_derives g v w) : CS_derives g u w := CS_deri_of_deri_deri (CS_deri_of_tran huv) hvw lemma CS_tran_or_id_of_deri {u w : list (symbol T g.nt)} (ass : CS_derives g u w) : (u = w) ∨ (βˆƒ v : list (symbol T g.nt), (CS_transforms g u v) ∧ (CS_derives g v w)) := relation.refl_trans_gen.cases_head ass
b30008851f7a581f99f286030be1d566ca811c33
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/data/set/lattice.lean
9a1ebcf79feaf31e881402d6c845507712ff3085
[ "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
47,545
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, Johannes HΓΆlzl, Mario Carneiro -- QUESTION: can make the first argument in βˆ€ x ∈ a, ... implicit? -/ import order.complete_boolean_algebra import data.sigma.basic import order.galois_connection import order.directed open function tactic set auto universes u v w x y variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {ΞΉ : Sort x} {ΞΉ' : Sort y} namespace set instance lattice_set : complete_lattice (set Ξ±) := { Sup := Ξ»s, {a | βˆƒ t ∈ s, a ∈ t }, Inf := Ξ»s, {a | βˆ€ t ∈ s, a ∈ t }, le_Sup := assume s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := assume s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, le_Inf := assume s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := assume s t t_in a h, h _ t_in, .. set.boolean_algebra, .. (infer_instance : complete_lattice (Ξ± β†’ Prop)) } /-- Image is monotone. See `set.image_image` for the statement in terms of `βŠ†`. -/ lemma monotone_image {f : Ξ± β†’ Ξ²} : monotone (image f) := assume s t, assume h : s βŠ† t, image_subset _ h theorem monotone_inter [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} (hf : monotone f) (hg : monotone g) : monotone (Ξ»x, f x ∩ g x) := assume b₁ bβ‚‚ h, inter_subset_inter (hf h) (hg h) theorem monotone_union [preorder Ξ²] {f g : Ξ² β†’ set Ξ±} (hf : monotone f) (hg : monotone g) : monotone (Ξ»x, f x βˆͺ g x) := assume b₁ bβ‚‚ h, union_subset_union (hf h) (hg h) theorem monotone_set_of [preorder Ξ±] {p : Ξ± β†’ Ξ² β†’ Prop} (hp : βˆ€b, monotone (Ξ»a, p a b)) : monotone (Ξ»a, {b | p a b}) := assume a a' h b, hp b h section galois_connection variables {f : Ξ± β†’ Ξ²} protected lemma image_preimage : galois_connection (image f) (preimage f) := assume a b, image_subset_iff /-- `kern_image f s` is the set of `y` such that `f ⁻¹ y βŠ† s` -/ def kern_image (f : Ξ± β†’ Ξ²) (s : set Ξ±) : set Ξ² := {y | βˆ€ ⦃x⦄, f x = y β†’ x ∈ s} protected lemma preimage_kern_image : galois_connection (preimage f) (kern_image f) := assume a b, ⟨ assume h x hx y hy, have f y ∈ a, from hy.symm β–Έ hx, h this, assume h x (hx : f x ∈ a), h hx rfl⟩ end galois_connection /- union and intersection over a family of sets indexed by a type -/ /-- Indexed union of a family of sets -/ @[reducible] def Union (s : ΞΉ β†’ set Ξ²) : set Ξ² := supr s /-- Indexed intersection of a family of sets -/ @[reducible] def Inter (s : ΞΉ β†’ set Ξ²) : set Ξ² := infi s notation `⋃` binders `, ` r:(scoped f, Union f) := r notation `β‹‚` binders `, ` r:(scoped f, Inter f) := r @[simp] theorem mem_Union {x : Ξ²} {s : ΞΉ β†’ set Ξ²} : x ∈ Union s ↔ βˆƒ i, x ∈ s i := ⟨assume ⟨t, ⟨⟨a, (t_eq : s a = t)⟩, (h : x ∈ t)⟩⟩, ⟨a, t_eq.symm β–Έ h⟩, assume ⟨a, h⟩, ⟨s a, ⟨⟨a, rfl⟩, h⟩⟩⟩ /- alternative proof: dsimp [Union, supr, Sup]; simp -/ -- TODO: more rewrite rules wrt forall / existentials and logical connectives -- TODO: also eliminate βˆƒi, ... ∧ i = t ∧ ... theorem set_of_exists (p : ΞΉ β†’ Ξ² β†’ Prop) : {x | βˆƒ i, p i x} = ⋃ i, {x | p i x} := ext $ Ξ» i, mem_Union.symm @[simp] theorem mem_Inter {x : Ξ²} {s : ΞΉ β†’ set Ξ²} : x ∈ Inter s ↔ βˆ€ i, x ∈ s i := ⟨assume (h : βˆ€a ∈ {a : set Ξ² | βˆƒi, s i = a}, x ∈ a) a, h (s a) ⟨a, rfl⟩, assume h t ⟨a, (eq : s a = t)⟩, eq β–Έ h a⟩ theorem set_of_forall (p : ΞΉ β†’ Ξ² β†’ Prop) : {x | βˆ€ i, p i x} = β‹‚ i, {x | p i x} := ext $ Ξ» i, mem_Inter.symm theorem Union_subset {s : ΞΉ β†’ set Ξ²} {t : set Ξ²} (h : βˆ€ i, s i βŠ† t) : (⋃ i, s i) βŠ† t := -- TODO: should be simpler when sets' order is based on lattices @supr_le (set Ξ²) _ set.lattice_set _ _ h theorem Union_subset_iff {s : ΞΉ β†’ set Ξ²} {t : set Ξ²} : (⋃ i, s i) βŠ† t ↔ (βˆ€ i, s i βŠ† t) := ⟨assume h i, subset.trans (le_supr s _) h, Union_subset⟩ theorem mem_Inter_of_mem {x : Ξ²} {s : ΞΉ β†’ set Ξ²} : (βˆ€ i, x ∈ s i) β†’ (x ∈ β‹‚ i, s i) := mem_Inter.2 theorem subset_Inter {t : set Ξ²} {s : ΞΉ β†’ set Ξ²} (h : βˆ€ i, t βŠ† s i) : t βŠ† β‹‚ i, s i := -- TODO: should be simpler when sets' order is based on lattices @le_infi (set Ξ²) _ set.lattice_set _ _ h theorem subset_Union : βˆ€ (s : ΞΉ β†’ set Ξ²) (i : ΞΉ), s i βŠ† (⋃ i, s i) := le_supr -- This rather trivial consequence is convenient with `apply`, -- and has `i` explicit for this use case. theorem subset_subset_Union {A : set Ξ²} {s : ΞΉ β†’ set Ξ²} (i : ΞΉ) (h : A βŠ† s i) : A βŠ† ⋃ (i : ΞΉ), s i := subset.trans h (subset_Union s i) theorem Inter_subset : βˆ€ (s : ΞΉ β†’ set Ξ²) (i : ΞΉ), (β‹‚ i, s i) βŠ† s i := infi_le lemma Inter_subset_of_subset {s : ΞΉ β†’ set Ξ±} {t : set Ξ±} (i : ΞΉ) (h : s i βŠ† t) : (β‹‚ i, s i) βŠ† t := set.subset.trans (set.Inter_subset s i) h lemma Inter_subset_Inter {s t : ΞΉ β†’ set Ξ±} (h : βˆ€ i, s i βŠ† t i) : (β‹‚ i, s i) βŠ† (β‹‚ i, t i) := set.subset_Inter $ Ξ» i, set.Inter_subset_of_subset i (h i) lemma Inter_subset_Inter2 {s : ΞΉ β†’ set Ξ±} {t : ΞΉ' β†’ set Ξ±} (h : βˆ€ j, βˆƒ i, s i βŠ† t j) : (β‹‚ i, s i) βŠ† (β‹‚ j, t j) := set.subset_Inter $ Ξ» j, let ⟨i, hi⟩ := h j in Inter_subset_of_subset i hi lemma Inter_set_of (P : ΞΉ β†’ Ξ± β†’ Prop) : (β‹‚ i, {x : Ξ± | P i x }) = {x : Ξ± | βˆ€ i, P i x} := by { ext, simp } theorem Union_const [nonempty ΞΉ] (s : set Ξ²) : (⋃ i:ΞΉ, s) = s := ext $ by simp theorem Inter_const [nonempty ΞΉ] (s : set Ξ²) : (β‹‚ i:ΞΉ, s) = s := ext $ by simp @[simp] -- complete_boolean_algebra theorem compl_Union (s : ΞΉ β†’ set Ξ²) : (⋃ i, s i)ᢜ = (β‹‚ i, (s i)ᢜ) := ext (by simp) -- classical -- complete_boolean_algebra theorem compl_Inter (s : ΞΉ β†’ set Ξ²) : (β‹‚ i, s i)ᢜ = (⋃ i, (s i)ᢜ) := ext (Ξ» x, by simp [not_forall]) -- classical -- complete_boolean_algebra theorem Union_eq_comp_Inter_comp (s : ΞΉ β†’ set Ξ²) : (⋃ i, s i) = (β‹‚ i, (s i)ᢜ)ᢜ := by simp [compl_Inter, compl_compl] -- classical -- complete_boolean_algebra theorem Inter_eq_comp_Union_comp (s : ΞΉ β†’ set Ξ²) : (β‹‚ i, s i) = (⋃ i, (s i)ᢜ)ᢜ := by simp [compl_compl] theorem inter_Union (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s ∩ (⋃ i, t i) = ⋃ i, s ∩ t i := ext $ by simp theorem Union_inter (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : (⋃ i, t i) ∩ s = ⋃ i, t i ∩ s := ext $ by simp theorem Union_union_distrib (s : ΞΉ β†’ set Ξ²) (t : ΞΉ β†’ set Ξ²) : (⋃ i, s i βˆͺ t i) = (⋃ i, s i) βˆͺ (⋃ i, t i) := ext $ by simp [exists_or_distrib] theorem Inter_inter_distrib (s : ΞΉ β†’ set Ξ²) (t : ΞΉ β†’ set Ξ²) : (β‹‚ i, s i ∩ t i) = (β‹‚ i, s i) ∩ (β‹‚ i, t i) := ext $ by simp [forall_and_distrib] theorem union_Union [nonempty ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s βˆͺ (⋃ i, t i) = ⋃ i, s βˆͺ t i := by rw [Union_union_distrib, Union_const] theorem Union_union [nonempty ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : (⋃ i, t i) βˆͺ s = ⋃ i, t i βˆͺ s := by rw [Union_union_distrib, Union_const] theorem inter_Inter [nonempty ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s ∩ (β‹‚ i, t i) = β‹‚ i, s ∩ t i := by rw [Inter_inter_distrib, Inter_const] theorem Inter_inter [nonempty ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : (β‹‚ i, t i) ∩ s = β‹‚ i, t i ∩ s := by rw [Inter_inter_distrib, Inter_const] -- classical theorem union_Inter (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s βˆͺ (β‹‚ i, t i) = β‹‚ i, s βˆͺ t i := ext $ assume x, by simp [forall_or_distrib_left] theorem Union_diff (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : (⋃ i, t i) \ s = ⋃ i, t i \ s := Union_inter _ _ theorem diff_Union [nonempty ΞΉ] (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s \ (⋃ i, t i) = β‹‚ i, s \ t i := by rw [diff_eq, compl_Union, inter_Inter]; refl theorem diff_Inter (s : set Ξ²) (t : ΞΉ β†’ set Ξ²) : s \ (β‹‚ i, t i) = ⋃ i, s \ t i := by rw [diff_eq, compl_Inter, inter_Union]; refl lemma directed_on_Union {r} {ΞΉ : Sort v} {f : ΞΉ β†’ set Ξ±} (hd : directed (βŠ†) f) (h : βˆ€x, directed_on r (f x)) : directed_on r (⋃x, f x) := by simp only [directed_on, exists_prop, mem_Union, exists_imp_distrib]; exact assume a₁ b₁ fb₁ aβ‚‚ bβ‚‚ fbβ‚‚, let ⟨z, zb₁, zbβ‚‚βŸ© := hd b₁ bβ‚‚, ⟨x, xf, xa₁, xaβ‚‚βŸ© := h z a₁ (zb₁ fb₁) aβ‚‚ (zbβ‚‚ fbβ‚‚) in ⟨x, ⟨z, xf⟩, xa₁, xaβ‚‚βŸ© lemma Union_inter_subset {ΞΉ Ξ±} {s t : ΞΉ β†’ set Ξ±} : (⋃ i, s i ∩ t i) βŠ† (⋃ i, s i) ∩ (⋃ i, t i) := by { rintro x ⟨_, ⟨i, rfl⟩, ⟨xs, xt⟩⟩, exact ⟨⟨_, ⟨i, rfl⟩, xs⟩, ⟨_, ⟨i, rfl⟩, xt⟩⟩ } lemma Union_inter_of_monotone {ΞΉ Ξ±} [semilattice_sup ΞΉ] {s t : ΞΉ β†’ set Ξ±} (hs : monotone s) (ht : monotone t) : (⋃ i, s i ∩ t i) = (⋃ i, s i) ∩ (⋃ i, t i) := begin ext x, refine ⟨λ hx, Union_inter_subset hx, _⟩, rintro ⟨⟨_, ⟨i, rfl⟩, xs⟩, ⟨_, ⟨j, rfl⟩, xt⟩⟩, exact ⟨_, ⟨i βŠ” j, rfl⟩, ⟨hs le_sup_left xs, ht le_sup_right xt⟩⟩ end /-- An equality version of this lemma is `Union_Inter_of_monotone` in `data.set.finite`. -/ lemma Union_Inter_subset {ΞΉ ΞΉ' Ξ±} {s : ΞΉ β†’ ΞΉ' β†’ set Ξ±} : (⋃ j, β‹‚ i, s i j) βŠ† β‹‚ i, ⋃ j, s i j := by { rintro x ⟨_, ⟨i, rfl⟩, hx⟩ _ ⟨j, rfl⟩, exact ⟨_, ⟨i, rfl⟩, hx _ ⟨j, rfl⟩⟩ } /- bounded unions and intersections -/ theorem mem_bUnion_iff {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {y : Ξ²} : y ∈ (⋃ x ∈ s, t x) ↔ βˆƒ x ∈ s, y ∈ t x := by simp theorem mem_bInter_iff {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {y : Ξ²} : y ∈ (β‹‚ x ∈ s, t x) ↔ βˆ€ x ∈ s, y ∈ t x := by simp theorem mem_bUnion {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {x : Ξ±} {y : Ξ²} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := by simp; exact ⟨x, ⟨xs, ytx⟩⟩ theorem mem_bInter {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {y : Ξ²} (h : βˆ€ x ∈ s, y ∈ t x) : y ∈ β‹‚ x ∈ s, t x := by simp; assumption theorem bUnion_subset {s : set Ξ±} {t : set Ξ²} {u : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, u x βŠ† t) : (⋃ x ∈ s, u x) βŠ† t := show (⨆ x ∈ s, u x) ≀ t, -- TODO: should not be necessary when sets' order is based on lattices from supr_le $ assume x, supr_le (h x) theorem subset_bInter {s : set Ξ±} {t : set Ξ²} {u : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, t βŠ† u x) : t βŠ† (β‹‚ x ∈ s, u x) := subset_Inter $ assume x, subset_Inter $ h x theorem subset_bUnion_of_mem {s : set Ξ±} {u : Ξ± β†’ set Ξ²} {x : Ξ±} (xs : x ∈ s) : u x βŠ† (⋃ x ∈ s, u x) := show u x ≀ (⨆ x ∈ s, u x), from le_supr_of_le x $ le_supr _ xs theorem bInter_subset_of_mem {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {x : Ξ±} (xs : x ∈ s) : (β‹‚ x ∈ s, t x) βŠ† t x := show (β¨…x ∈ s, t x) ≀ t x, from infi_le_of_le x $ infi_le _ xs theorem bUnion_subset_bUnion_left {s s' : set Ξ±} {t : Ξ± β†’ set Ξ²} (h : s βŠ† s') : (⋃ x ∈ s, t x) βŠ† (⋃ x ∈ s', t x) := bUnion_subset (Ξ» x xs, subset_bUnion_of_mem (h xs)) theorem bInter_subset_bInter_left {s s' : set Ξ±} {t : Ξ± β†’ set Ξ²} (h : s' βŠ† s) : (β‹‚ x ∈ s, t x) βŠ† (β‹‚ x ∈ s', t x) := subset_bInter (Ξ» x xs, bInter_subset_of_mem (h xs)) theorem bUnion_subset_bUnion_right {s : set Ξ±} {t1 t2 : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, t1 x βŠ† t2 x) : (⋃ x ∈ s, t1 x) βŠ† (⋃ x ∈ s, t2 x) := bUnion_subset (Ξ» x xs, subset.trans (h x xs) (subset_bUnion_of_mem xs)) theorem bInter_subset_bInter_right {s : set Ξ±} {t1 t2 : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, t1 x βŠ† t2 x) : (β‹‚ x ∈ s, t1 x) βŠ† (β‹‚ x ∈ s, t2 x) := subset_bInter (Ξ» x xs, subset.trans (bInter_subset_of_mem xs) (h x xs)) theorem bUnion_subset_bUnion {Ξ³ : Type*} {s : set Ξ±} {t : Ξ± β†’ set Ξ²} {s' : set Ξ³} {t' : Ξ³ β†’ set Ξ²} (h : βˆ€ x ∈ s, βˆƒ y ∈ s', t x βŠ† t' y) : (⋃ x ∈ s, t x) βŠ† (⋃ y ∈ s', t' y) := begin intros x, simp only [mem_Union], rintros ⟨a, a_in, ha⟩, rcases h a a_in with ⟨c, c_in, hc⟩, exact ⟨c, c_in, hc ha⟩ end theorem bInter_mono' {s s' : set Ξ±} {t t' : Ξ± β†’ set Ξ²} (hs : s βŠ† s') (h : βˆ€ x ∈ s, t x βŠ† t' x) : (β‹‚ x ∈ s', t x) βŠ† (β‹‚ x ∈ s, t' x) := begin intros x x_in, simp only [mem_Inter] at *, exact Ξ» a a_in, h a a_in $ x_in _ (hs a_in) end theorem bInter_mono {s : set Ξ±} {t t' : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, t x βŠ† t' x) : (β‹‚ x ∈ s, t x) βŠ† (β‹‚ x ∈ s, t' x) := bInter_mono' (subset.refl s) h theorem bUnion_mono {s : set Ξ±} {t t' : Ξ± β†’ set Ξ²} (h : βˆ€ x ∈ s, t x βŠ† t' x) : (⋃ x ∈ s, t x) βŠ† (⋃ x ∈ s, t' x) := bUnion_subset_bUnion (Ξ» x x_in, ⟨x, x_in, h x x_in⟩) theorem bUnion_eq_Union (s : set Ξ±) (t : Ξ  x ∈ s, set Ξ²) : (⋃ x ∈ s, t x β€Ή_β€Ί) = (⋃ x : s, t x x.2) := supr_subtype' theorem bInter_eq_Inter (s : set Ξ±) (t : Ξ  x ∈ s, set Ξ²) : (β‹‚ x ∈ s, t x β€Ή_β€Ί) = (β‹‚ x : s, t x x.2) := infi_subtype' theorem bInter_empty (u : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ (βˆ… : set Ξ±), u x) = univ := show (β¨…x ∈ (βˆ… : set Ξ±), u x) = ⊀, -- simplifier should be able to rewrite x ∈ βˆ… to false. from infi_emptyset theorem bInter_univ (u : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ @univ Ξ±, u x) = β‹‚ x, u x := infi_univ -- TODO(Jeremy): here is an artifact of the the encoding of bounded intersection: -- without dsimp, the next theorem fails to type check, because there is a lambda -- in a type that needs to be contracted. Using simp [eq_of_mem_singleton xa] also works. @[simp] theorem bInter_singleton (a : Ξ±) (s : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ ({a} : set Ξ±), s x) = s a := show (β¨… x ∈ ({a} : set Ξ±), s x) = s a, by simp theorem bInter_union (s t : set Ξ±) (u : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ s βˆͺ t, u x) = (β‹‚ x ∈ s, u x) ∩ (β‹‚ x ∈ t, u x) := show (β¨… x ∈ s βˆͺ t, u x) = (β¨… x ∈ s, u x) βŠ“ (β¨… x ∈ t, u x), from infi_union -- TODO(Jeremy): simp [insert_eq, bInter_union] doesn't work @[simp] theorem bInter_insert (a : Ξ±) (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ insert a s, t x) = t a ∩ (β‹‚ x ∈ s, t x) := begin rw insert_eq, simp [bInter_union] end -- TODO(Jeremy): another example of where an annotation is needed theorem bInter_pair (a b : Ξ±) (s : Ξ± β†’ set Ξ²) : (β‹‚ x ∈ ({a, b} : set Ξ±), s x) = s a ∩ s b := by simp [inter_comm] theorem bUnion_empty (s : Ξ± β†’ set Ξ²) : (⋃ x ∈ (βˆ… : set Ξ±), s x) = βˆ… := supr_emptyset theorem bUnion_univ (s : Ξ± β†’ set Ξ²) : (⋃ x ∈ @univ Ξ±, s x) = ⋃ x, s x := supr_univ @[simp] theorem bUnion_singleton (a : Ξ±) (s : Ξ± β†’ set Ξ²) : (⋃ x ∈ ({a} : set Ξ±), s x) = s a := supr_singleton @[simp] theorem bUnion_of_singleton (s : set Ξ±) : (⋃ x ∈ s, {x}) = s := ext $ by simp theorem bUnion_union (s t : set Ξ±) (u : Ξ± β†’ set Ξ²) : (⋃ x ∈ s βˆͺ t, u x) = (⋃ x ∈ s, u x) βˆͺ (⋃ x ∈ t, u x) := supr_union -- TODO(Jeremy): once again, simp doesn't do it alone. @[simp] theorem bUnion_insert (a : Ξ±) (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : (⋃ x ∈ insert a s, t x) = t a βˆͺ (⋃ x ∈ s, t x) := begin rw [insert_eq], simp [bUnion_union] end theorem bUnion_pair (a b : Ξ±) (s : Ξ± β†’ set Ξ²) : (⋃ x ∈ ({a, b} : set Ξ±), s x) = s a βˆͺ s b := by simp [union_comm] @[simp] -- complete_boolean_algebra theorem compl_bUnion (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : (⋃ i ∈ s, t i)ᢜ = (β‹‚ i ∈ s, (t i)ᢜ) := ext (Ξ» x, by simp) -- classical -- complete_boolean_algebra theorem compl_bInter (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : (β‹‚ i ∈ s, t i)ᢜ = (⋃ i ∈ s, (t i)ᢜ) := ext (Ξ» x, by simp [not_forall]) theorem inter_bUnion (s : set Ξ±) (t : Ξ± β†’ set Ξ²) (u : set Ξ²) : u ∩ (⋃ i ∈ s, t i) = ⋃ i ∈ s, u ∩ t i := begin ext x, simp only [exists_prop, mem_Union, mem_inter_eq], exact ⟨λ ⟨hx, ⟨i, is, xi⟩⟩, ⟨i, is, hx, xi⟩, Ξ» ⟨i, is, hx, xi⟩, ⟨hx, ⟨i, is, xi⟩⟩⟩ end theorem bUnion_inter (s : set Ξ±) (t : Ξ± β†’ set Ξ²) (u : set Ξ²) : (⋃ i ∈ s, t i) ∩ u = (⋃ i ∈ s, t i ∩ u) := by simp [@inter_comm _ _ u, inter_bUnion] /-- Intersection of a set of sets. -/ @[reducible] def sInter (S : set (set Ξ±)) : set Ξ± := Inf S prefix `β‹‚β‚€`:110 := sInter theorem mem_sUnion_of_mem {x : Ξ±} {t : set Ξ±} {S : set (set Ξ±)} (hx : x ∈ t) (ht : t ∈ S) : x ∈ ⋃₀ S := ⟨t, ⟨ht, hx⟩⟩ theorem mem_sUnion {x : Ξ±} {S : set (set Ξ±)} : x ∈ ⋃₀ S ↔ βˆƒt ∈ S, x ∈ t := iff.rfl -- is this theorem really necessary? theorem not_mem_of_not_mem_sUnion {x : Ξ±} {t : set Ξ±} {S : set (set Ξ±)} (hx : x βˆ‰ ⋃₀ S) (ht : t ∈ S) : x βˆ‰ t := Ξ» h, hx ⟨t, ht, h⟩ @[simp] theorem mem_sInter {x : Ξ±} {S : set (set Ξ±)} : x ∈ β‹‚β‚€ S ↔ βˆ€ t ∈ S, x ∈ t := iff.rfl theorem sInter_subset_of_mem {S : set (set Ξ±)} {t : set Ξ±} (tS : t ∈ S) : β‹‚β‚€ S βŠ† t := Inf_le tS theorem subset_sUnion_of_mem {S : set (set Ξ±)} {t : set Ξ±} (tS : t ∈ S) : t βŠ† ⋃₀ S := le_Sup tS lemma subset_sUnion_of_subset {s : set Ξ±} (t : set (set Ξ±)) (u : set Ξ±) (h₁ : s βŠ† u) (hβ‚‚ : u ∈ t) : s βŠ† ⋃₀ t := subset.trans h₁ (subset_sUnion_of_mem hβ‚‚) theorem sUnion_subset {S : set (set Ξ±)} {t : set Ξ±} (h : βˆ€t' ∈ S, t' βŠ† t) : (⋃₀ S) βŠ† t := Sup_le h theorem sUnion_subset_iff {s : set (set Ξ±)} {t : set Ξ±} : ⋃₀ s βŠ† t ↔ βˆ€t' ∈ s, t' βŠ† t := ⟨assume h t' ht', subset.trans (subset_sUnion_of_mem ht') h, sUnion_subset⟩ theorem subset_sInter {S : set (set Ξ±)} {t : set Ξ±} (h : βˆ€t' ∈ S, t βŠ† t') : t βŠ† (β‹‚β‚€ S) := le_Inf h theorem sUnion_subset_sUnion {S T : set (set Ξ±)} (h : S βŠ† T) : ⋃₀ S βŠ† ⋃₀ T := sUnion_subset $ Ξ» s hs, subset_sUnion_of_mem (h hs) theorem sInter_subset_sInter {S T : set (set Ξ±)} (h : S βŠ† T) : β‹‚β‚€ T βŠ† β‹‚β‚€ S := subset_sInter $ Ξ» s hs, sInter_subset_of_mem (h hs) @[simp] theorem sUnion_empty : ⋃₀ βˆ… = (βˆ… : set Ξ±) := Sup_empty @[simp] theorem sInter_empty : β‹‚β‚€ βˆ… = (univ : set Ξ±) := Inf_empty @[simp] theorem sUnion_singleton (s : set Ξ±) : ⋃₀ {s} = s := Sup_singleton @[simp] theorem sInter_singleton (s : set Ξ±) : β‹‚β‚€ {s} = s := Inf_singleton theorem sUnion_union (S T : set (set Ξ±)) : ⋃₀ (S βˆͺ T) = ⋃₀ S βˆͺ ⋃₀ T := Sup_union theorem sInter_union (S T : set (set Ξ±)) : β‹‚β‚€ (S βˆͺ T) = β‹‚β‚€ S ∩ β‹‚β‚€ T := Inf_union theorem sInter_Union (s : ΞΉ β†’ set (set Ξ±)) : β‹‚β‚€ (⋃ i, s i) = β‹‚ i, β‹‚β‚€ s i := begin ext x, simp only [mem_Union, mem_Inter, mem_sInter, exists_imp_distrib], split ; tauto end @[simp] theorem sUnion_insert (s : set Ξ±) (T : set (set Ξ±)) : ⋃₀ (insert s T) = s βˆͺ ⋃₀ T := Sup_insert @[simp] theorem sInter_insert (s : set Ξ±) (T : set (set Ξ±)) : β‹‚β‚€ (insert s T) = s ∩ β‹‚β‚€ T := Inf_insert theorem sUnion_pair (s t : set Ξ±) : ⋃₀ {s, t} = s βˆͺ t := Sup_pair theorem sInter_pair (s t : set Ξ±) : β‹‚β‚€ {s, t} = s ∩ t := Inf_pair @[simp] theorem sUnion_image (f : Ξ± β†’ set Ξ²) (s : set Ξ±) : ⋃₀ (f '' s) = ⋃ x ∈ s, f x := Sup_image @[simp] theorem sInter_image (f : Ξ± β†’ set Ξ²) (s : set Ξ±) : β‹‚β‚€ (f '' s) = β‹‚ x ∈ s, f x := Inf_image @[simp] theorem sUnion_range (f : ΞΉ β†’ set Ξ²) : ⋃₀ (range f) = ⋃ x, f x := rfl @[simp] theorem sInter_range (f : ΞΉ β†’ set Ξ²) : β‹‚β‚€ (range f) = β‹‚ x, f x := rfl lemma sUnion_eq_univ_iff {c : set (set Ξ±)} : ⋃₀ c = @set.univ Ξ± ↔ βˆ€ a, βˆƒ b ∈ c, a ∈ b := ⟨λ H a, let ⟨b, hm, hb⟩ := mem_sUnion.1 $ by rw H; exact mem_univ a in ⟨b, hm, hb⟩, Ξ» H, set.univ_subset_iff.1 $ Ξ» x hx, let ⟨b, hm, hb⟩ := H x in set.mem_sUnion_of_mem hb hm⟩ theorem compl_sUnion (S : set (set Ξ±)) : (⋃₀ S)ᢜ = β‹‚β‚€ (compl '' S) := set.ext $ assume x, ⟨assume : Β¬ (βˆƒs∈S, x ∈ s), assume s h, match s, h with ._, ⟨t, hs, rfl⟩ := assume h, this ⟨t, hs, h⟩ end, assume : βˆ€s, s ∈ compl '' S β†’ x ∈ s, assume ⟨t, tS, xt⟩, this (compl t) (mem_image_of_mem _ tS) xt⟩ -- classical theorem sUnion_eq_compl_sInter_compl (S : set (set Ξ±)) : ⋃₀ S = (β‹‚β‚€ (compl '' S))ᢜ := by rw [←compl_compl (⋃₀ S), compl_sUnion] -- classical theorem compl_sInter (S : set (set Ξ±)) : (β‹‚β‚€ S)ᢜ = ⋃₀ (compl '' S) := by rw [sUnion_eq_compl_sInter_compl, compl_compl_image] -- classical theorem sInter_eq_comp_sUnion_compl (S : set (set Ξ±)) : β‹‚β‚€ S = (⋃₀ (compl '' S))ᢜ := by rw [←compl_compl (β‹‚β‚€ S), compl_sInter] theorem inter_empty_of_inter_sUnion_empty {s t : set Ξ±} {S : set (set Ξ±)} (hs : t ∈ S) (h : s ∩ ⋃₀ S = βˆ…) : s ∩ t = βˆ… := eq_empty_of_subset_empty $ by rw ← h; exact inter_subset_inter_right _ (subset_sUnion_of_mem hs) theorem range_sigma_eq_Union_range {Ξ³ : Ξ± β†’ Type*} (f : sigma Ξ³ β†’ Ξ²) : range f = ⋃ a, range (Ξ» b, f ⟨a, b⟩) := set.ext $ by simp theorem Union_eq_range_sigma (s : Ξ± β†’ set Ξ²) : (⋃ i, s i) = range (Ξ» a : Ξ£ i, s i, a.2) := by simp [set.ext_iff] theorem Union_image_preimage_sigma_mk_eq_self {ΞΉ : Type*} {Οƒ : ΞΉ β†’ Type*} (s : set (sigma Οƒ)) : (⋃ i, sigma.mk i '' (sigma.mk i ⁻¹' s)) = s := begin ext x, simp only [mem_Union, mem_image, mem_preimage], split, { rintros ⟨i, a, h, rfl⟩, exact h }, { intro h, cases x with i a, exact ⟨i, a, h, rfl⟩ } end lemma sUnion_mono {s t : set (set Ξ±)} (h : s βŠ† t) : (⋃₀ s) βŠ† (⋃₀ t) := sUnion_subset $ assume t' ht', subset_sUnion_of_mem $ h ht' lemma Union_subset_Union {s t : ΞΉ β†’ set Ξ±} (h : βˆ€i, s i βŠ† t i) : (⋃i, s i) βŠ† (⋃i, t i) := @supr_le_supr (set Ξ±) ΞΉ _ s t h lemma Union_subset_Union2 {ΞΉβ‚‚ : Sort*} {s : ΞΉ β†’ set Ξ±} {t : ΞΉβ‚‚ β†’ set Ξ±} (h : βˆ€i, βˆƒj, s i βŠ† t j) : (⋃i, s i) βŠ† (⋃i, t i) := @supr_le_supr2 (set Ξ±) ΞΉ ΞΉβ‚‚ _ s t h lemma Union_subset_Union_const {ΞΉβ‚‚ : Sort x} {s : set Ξ±} (h : ΞΉ β†’ ΞΉβ‚‚) : (⋃ i:ΞΉ, s) βŠ† (⋃ j:ΞΉβ‚‚, s) := @supr_le_supr_const (set Ξ±) ΞΉ ΞΉβ‚‚ _ s h @[simp] lemma Union_of_singleton (Ξ± : Type u) : (⋃(x : Ξ±), {x}) = @set.univ Ξ± := ext $ Ξ» x, ⟨λ h, ⟨⟩, Ξ» h, ⟨{x}, ⟨⟨x, rfl⟩, mem_singleton x⟩⟩⟩ theorem bUnion_subset_Union (s : set Ξ±) (t : Ξ± β†’ set Ξ²) : (⋃ x ∈ s, t x) βŠ† (⋃ x, t x) := Union_subset_Union $ Ξ» i, Union_subset $ Ξ» h, by refl lemma sUnion_eq_bUnion {s : set (set Ξ±)} : (⋃₀ s) = (⋃ (i : set Ξ±) (h : i ∈ s), i) := by rw [← sUnion_image, image_id'] lemma sInter_eq_bInter {s : set (set Ξ±)} : (β‹‚β‚€ s) = (β‹‚ (i : set Ξ±) (h : i ∈ s), i) := by rw [← sInter_image, image_id'] lemma sUnion_eq_Union {s : set (set Ξ±)} : (⋃₀ s) = (⋃ (i : s), i) := by simp only [←sUnion_range, subtype.range_coe] lemma sInter_eq_Inter {s : set (set Ξ±)} : (β‹‚β‚€ s) = (β‹‚ (i : s), i) := by simp only [←sInter_range, subtype.range_coe] lemma union_eq_Union {s₁ sβ‚‚ : set Ξ±} : s₁ βˆͺ sβ‚‚ = ⋃ b : bool, cond b s₁ sβ‚‚ := set.ext $ Ξ» x, by simp [bool.exists_bool, or_comm] lemma inter_eq_Inter {s₁ sβ‚‚ : set Ξ±} : s₁ ∩ sβ‚‚ = β‹‚ b : bool, cond b s₁ sβ‚‚ := set.ext $ Ξ» x, by simp [bool.forall_bool, and_comm] instance : complete_boolean_algebra (set Ξ±) := { compl := compl, sdiff := (\), infi_sup_le_sup_Inf := assume s t x, show x ∈ (β‹‚ b ∈ t, s βˆͺ b) β†’ x ∈ s βˆͺ (β‹‚β‚€ t), by simp; exact assume h, or.imp_right (assume hn : x βˆ‰ s, assume i hi, or.resolve_left (h i hi) hn) (classical.em $ x ∈ s), inf_Sup_le_supr_inf := assume s t x, show x ∈ s ∩ (⋃₀ t) β†’ x ∈ (⋃ b ∈ t, s ∩ b), by simp [-and_imp, and.left_comm], .. set.boolean_algebra, .. set.lattice_set } lemma sInter_union_sInter {S T : set (set Ξ±)} : (β‹‚β‚€S) βˆͺ (β‹‚β‚€T) = (β‹‚p ∈ S.prod T, (p : (set Ξ±) Γ— (set Ξ±)).1 βˆͺ p.2) := Inf_sup_Inf lemma sUnion_inter_sUnion {s t : set (set Ξ±)} : (⋃₀s) ∩ (⋃₀t) = (⋃p ∈ s.prod t, (p : (set Ξ±) Γ— (set Ξ± )).1 ∩ p.2) := Sup_inf_Sup /-- If `S` is a set of sets, and each `s ∈ S` can be represented as an intersection of sets `T s hs`, then `β‹‚β‚€ S` is the intersection of the union of all `T s hs`. -/ lemma sInter_bUnion {S : set (set Ξ±)} {T : Ξ  s ∈ S, set (set Ξ±)} (hT : βˆ€s∈S, s = β‹‚β‚€ T s β€Ήs ∈ Sβ€Ί) : β‹‚β‚€ (⋃s∈S, T s β€Ή_β€Ί) = β‹‚β‚€ S := begin ext, simp only [and_imp, exists_prop, set.mem_sInter, set.mem_Union, exists_imp_distrib], split, { assume H s sS, rw [hT s sS, mem_sInter], assume t tTs, exact H t s sS tTs }, { assume H t s sS tTs, suffices : s βŠ† t, exact this (H s sS), rw [hT s sS, sInter_eq_bInter], exact bInter_subset_of_mem tTs } end /-- If `S` is a set of sets, and each `s ∈ S` can be represented as an union of sets `T s hs`, then `⋃₀ S` is the union of the union of all `T s hs`. -/ lemma sUnion_bUnion {S : set (set Ξ±)} {T : Ξ  s ∈ S, set (set Ξ±)} (hT : βˆ€s∈S, s = ⋃₀ T s β€Ή_β€Ί) : ⋃₀ (⋃s∈S, T s β€Ή_β€Ί) = ⋃₀ S := begin ext, simp only [exists_prop, set.mem_Union, set.mem_set_of_eq], split, { rintros ⟨t, ⟨⟨s, ⟨sS, tTs⟩⟩, xt⟩⟩, refine ⟨s, ⟨sS, _⟩⟩, rw hT s sS, exact subset_sUnion_of_mem tTs xt }, { rintros ⟨s, ⟨sS, xs⟩⟩, rw hT s sS at xs, rcases mem_sUnion.1 xs with ⟨t, tTs, xt⟩, exact ⟨t, ⟨⟨s, ⟨sS, tTs⟩⟩, xt⟩⟩ } end lemma Union_range_eq_sUnion {Ξ± Ξ² : Type*} (C : set (set Ξ±)) {f : βˆ€(s : C), Ξ² β†’ s} (hf : βˆ€(s : C), surjective (f s)) : (⋃(y : Ξ²), range (Ξ»(s : C), (f s y).val)) = ⋃₀ C := begin ext x, split, { rintro ⟨s, ⟨y, rfl⟩, ⟨⟨s, hs⟩, rfl⟩⟩, refine ⟨_, hs, _⟩, exact (f ⟨s, hs⟩ y).2 }, { rintro ⟨s, hs, hx⟩, cases hf ⟨s, hs⟩ ⟨x, hx⟩ with y hy, refine ⟨_, ⟨y, rfl⟩, ⟨⟨s, hs⟩, _⟩⟩, exact congr_arg subtype.val hy } end lemma Union_range_eq_Union {ΞΉ Ξ± Ξ² : Type*} (C : ΞΉ β†’ set Ξ±) {f : βˆ€(x : ΞΉ), Ξ² β†’ C x} (hf : βˆ€(x : ΞΉ), surjective (f x)) : (⋃(y : Ξ²), range (Ξ»(x : ΞΉ), (f x y).val)) = ⋃x, C x := begin ext x, rw [mem_Union, mem_Union], split, { rintro ⟨y, ⟨i, rfl⟩⟩, exact ⟨i, (f i y).2⟩ }, { rintro ⟨i, hx⟩, cases hf i ⟨x, hx⟩ with y hy, refine ⟨y, ⟨i, congr_arg subtype.val hy⟩⟩ } end lemma union_distrib_Inter_right {ΞΉ : Type*} (s : ΞΉ β†’ set Ξ±) (t : set Ξ±) : (β‹‚ i, s i) βˆͺ t = (β‹‚ i, s i βˆͺ t) := begin ext x, rw [mem_union_eq, mem_Inter], split ; finish end lemma union_distrib_Inter_left {ΞΉ : Type*} (s : ΞΉ β†’ set Ξ±) (t : set Ξ±) : t βˆͺ (β‹‚ i, s i) = (β‹‚ i, t βˆͺ s i) := begin rw [union_comm, union_distrib_Inter_right], simp [union_comm] end section function /-! ### `maps_to` -/ lemma maps_to_sUnion {S : set (set Ξ±)} {t : set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ s ∈ S, maps_to f s t) : maps_to f (⋃₀ S) t := Ξ» x ⟨s, hs, hx⟩, H s hs hx lemma maps_to_Union {s : ΞΉ β†’ set Ξ±} {t : set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, maps_to f (s i) t) : maps_to f (⋃ i, s i) t := maps_to_sUnion $ forall_range_iff.2 H lemma maps_to_bUnion {p : ΞΉ β†’ Prop} {s : Ξ  (i : ΞΉ) (hi : p i), set Ξ±} {t : set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i hi, maps_to f (s i hi) t) : maps_to f (⋃ i hi, s i hi) t := maps_to_Union $ Ξ» i, maps_to_Union (H i) lemma maps_to_Union_Union {s : ΞΉ β†’ set Ξ±} {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, maps_to f (s i) (t i)) : maps_to f (⋃ i, s i) (⋃ i, t i) := maps_to_Union $ Ξ» i, (H i).mono (subset.refl _) (subset_Union t i) lemma maps_to_bUnion_bUnion {p : ΞΉ β†’ Prop} {s : Ξ  i (hi : p i), set Ξ±} {t : Ξ  i (hi : p i), set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i hi, maps_to f (s i hi) (t i hi)) : maps_to f (⋃ i hi, s i hi) (⋃ i hi, t i hi) := maps_to_Union_Union $ Ξ» i, maps_to_Union_Union (H i) lemma maps_to_sInter {s : set Ξ±} {T : set (set Ξ²)} {f : Ξ± β†’ Ξ²} (H : βˆ€ t ∈ T, maps_to f s t) : maps_to f s (β‹‚β‚€ T) := Ξ» x hx t ht, H t ht hx lemma maps_to_Inter {s : set Ξ±} {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, maps_to f s (t i)) : maps_to f s (β‹‚ i, t i) := Ξ» x hx, mem_Inter.2 $ Ξ» i, H i hx lemma maps_to_bInter {p : ΞΉ β†’ Prop} {s : set Ξ±} {t : Ξ  i (hi : p i), set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i hi, maps_to f s (t i hi)) : maps_to f s (β‹‚ i hi, t i hi) := maps_to_Inter $ Ξ» i, maps_to_Inter (H i) lemma maps_to_Inter_Inter {s : ΞΉ β†’ set Ξ±} {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, maps_to f (s i) (t i)) : maps_to f (β‹‚ i, s i) (β‹‚ i, t i) := maps_to_Inter $ Ξ» i, (H i).mono (Inter_subset s i) (subset.refl _) lemma maps_to_bInter_bInter {p : ΞΉ β†’ Prop} {s : Ξ  i (hi : p i), set Ξ±} {t : Ξ  i (hi : p i), set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i hi, maps_to f (s i hi) (t i hi)) : maps_to f (β‹‚ i hi, s i hi) (β‹‚ i hi, t i hi) := maps_to_Inter_Inter $ Ξ» i, maps_to_Inter_Inter (H i) lemma image_Inter_subset (s : ΞΉ β†’ set Ξ±) (f : Ξ± β†’ Ξ²) : f '' (β‹‚ i, s i) βŠ† β‹‚ i, f '' (s i) := (maps_to_Inter_Inter $ Ξ» i, maps_to_image f (s i)).image_subset lemma image_bInter_subset {p : ΞΉ β†’ Prop} (s : Ξ  i (hi : p i), set Ξ±) (f : Ξ± β†’ Ξ²) : f '' (β‹‚ i hi, s i hi) βŠ† β‹‚ i hi, f '' (s i hi) := (maps_to_bInter_bInter $ Ξ» i hi, maps_to_image f (s i hi)).image_subset lemma image_sInter_subset (S : set (set Ξ±)) (f : Ξ± β†’ Ξ²) : f '' (β‹‚β‚€ S) βŠ† β‹‚ s ∈ S, f '' s := by { rw sInter_eq_bInter, apply image_bInter_subset } /-! ### `inj_on` -/ lemma inj_on.image_Inter_eq [nonempty ΞΉ] {s : ΞΉ β†’ set Ξ±} {f : Ξ± β†’ Ξ²} (h : inj_on f (⋃ i, s i)) : f '' (β‹‚ i, s i) = β‹‚ i, f '' (s i) := begin inhabit ΞΉ, refine subset.antisymm (image_Inter_subset s f) (Ξ» y hy, _), simp only [mem_Inter, mem_image_iff_bex] at hy, choose x hx hy using hy, refine ⟨x (default ΞΉ), mem_Inter.2 $ Ξ» i, _, hy _⟩, suffices : x (default ΞΉ) = x i, { rw this, apply hx }, replace hx : βˆ€ i, x i ∈ ⋃ j, s j := Ξ» i, (subset_Union _ _) (hx i), apply h (hx _) (hx _), simp only [hy] end lemma inj_on.image_bInter_eq {p : ΞΉ β†’ Prop} {s : Ξ  i (hi : p i), set Ξ±} (hp : βˆƒ i, p i) {f : Ξ± β†’ Ξ²} (h : inj_on f (⋃ i hi, s i hi)) : f '' (β‹‚ i hi, s i hi) = β‹‚ i hi, f '' (s i hi) := begin simp only [Inter, infi_subtype'], haveI : nonempty {i // p i} := nonempty_subtype.2 hp, apply inj_on.image_Inter_eq, simpa only [Union, supr_subtype'] using h end lemma inj_on_Union_of_directed {s : ΞΉ β†’ set Ξ±} (hs : directed (βŠ†) s) {f : Ξ± β†’ Ξ²} (hf : βˆ€ i, inj_on f (s i)) : inj_on f (⋃ i, s i) := begin intros x hx y hy hxy, rcases mem_Union.1 hx with ⟨i, hx⟩, rcases mem_Union.1 hy with ⟨j, hy⟩, rcases hs i j with ⟨k, hi, hj⟩, exact hf k (hi hx) (hj hy) hxy end /-! ### `surj_on` -/ lemma surj_on_sUnion {s : set Ξ±} {T : set (set Ξ²)} {f : Ξ± β†’ Ξ²} (H : βˆ€ t ∈ T, surj_on f s t) : surj_on f s (⋃₀ T) := Ξ» x ⟨t, ht, hx⟩, H t ht hx lemma surj_on_Union {s : set Ξ±} {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, surj_on f s (t i)) : surj_on f s (⋃ i, t i) := surj_on_sUnion $ forall_range_iff.2 H lemma surj_on_Union_Union {s : ΞΉ β†’ set Ξ±} {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, surj_on f (s i) (t i)) : surj_on f (⋃ i, s i) (⋃ i, t i) := surj_on_Union $ Ξ» i, (H i).mono (subset_Union _ _) (subset.refl _) lemma surj_on_bUnion {p : ΞΉ β†’ Prop} {s : set Ξ±} {t : Ξ  i (hi : p i), set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i hi, surj_on f s (t i hi)) : surj_on f s (⋃ i hi, t i hi) := surj_on_Union $ Ξ» i, surj_on_Union (H i) lemma surj_on_bUnion_bUnion {p : ΞΉ β†’ Prop} {s : Ξ  i (hi : p i), set Ξ±} {t : Ξ  i (hi : p i), set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i hi, surj_on f (s i hi) (t i hi)) : surj_on f (⋃ i hi, s i hi) (⋃ i hi, t i hi) := surj_on_Union_Union $ Ξ» i, surj_on_Union_Union (H i) lemma surj_on_Inter [hi : nonempty ΞΉ] {s : ΞΉ β†’ set Ξ±} {t : set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, surj_on f (s i) t) (Hinj : inj_on f (⋃ i, s i)) : surj_on f (β‹‚ i, s i) t := begin intros y hy, rw [Hinj.image_Inter_eq, mem_Inter], exact Ξ» i, H i hy end lemma surj_on_Inter_Inter [hi : nonempty ΞΉ] {s : ΞΉ β†’ set Ξ±} {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, surj_on f (s i) (t i)) (Hinj : inj_on f (⋃ i, s i)) : surj_on f (β‹‚ i, s i) (β‹‚ i, t i) := surj_on_Inter (Ξ» i, (H i).mono (subset.refl _) (Inter_subset _ _)) Hinj /-! ### `bij_on` -/ lemma bij_on_Union {s : ΞΉ β†’ set Ξ±} {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, bij_on f (s i) (t i)) (Hinj : inj_on f (⋃ i, s i)) : bij_on f (⋃ i, s i) (⋃ i, t i) := ⟨maps_to_Union_Union $ Ξ» i, (H i).maps_to, Hinj, surj_on_Union_Union $ Ξ» i, (H i).surj_on⟩ lemma bij_on_Inter [hi :nonempty ΞΉ] {s : ΞΉ β†’ set Ξ±} {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, bij_on f (s i) (t i)) (Hinj : inj_on f (⋃ i, s i)) : bij_on f (β‹‚ i, s i) (β‹‚ i, t i) := ⟨maps_to_Inter_Inter $ Ξ» i, (H i).maps_to, hi.elim $ Ξ» i, (H i).inj_on.mono (Inter_subset _ _), surj_on_Inter_Inter (Ξ» i, (H i).surj_on) Hinj⟩ lemma bij_on_Union_of_directed {s : ΞΉ β†’ set Ξ±} (hs : directed (βŠ†) s) {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, bij_on f (s i) (t i)) : bij_on f (⋃ i, s i) (⋃ i, t i) := bij_on_Union H $ inj_on_Union_of_directed hs (Ξ» i, (H i).inj_on) lemma bij_on_Inter_of_directed [nonempty ΞΉ] {s : ΞΉ β†’ set Ξ±} (hs : directed (βŠ†) s) {t : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} (H : βˆ€ i, bij_on f (s i) (t i)) : bij_on f (β‹‚ i, s i) (β‹‚ i, t i) := bij_on_Inter H $ inj_on_Union_of_directed hs (Ξ» i, (H i).inj_on) end function section variables {p : Prop} {ΞΌ : p β†’ set Ξ±} @[simp] lemma Inter_pos (hp : p) : (β‹‚h:p, ΞΌ h) = ΞΌ hp := infi_pos hp @[simp] lemma Inter_neg (hp : Β¬ p) : (β‹‚h:p, ΞΌ h) = univ := infi_neg hp @[simp] lemma Union_pos (hp : p) : (⋃h:p, ΞΌ h) = ΞΌ hp := supr_pos hp @[simp] lemma Union_neg (hp : Β¬ p) : (⋃h:p, ΞΌ h) = βˆ… := supr_neg hp @[simp] lemma Union_empty {ΞΉ : Sort*} : (⋃i:ΞΉ, βˆ…:set Ξ±) = βˆ… := supr_bot @[simp] lemma Inter_univ {ΞΉ : Sort*} : (β‹‚i:ΞΉ, univ:set Ξ±) = univ := infi_top end section image lemma image_Union {f : Ξ± β†’ Ξ²} {s : ΞΉ β†’ set Ξ±} : f '' (⋃ i, s i) = (⋃i, f '' s i) := begin apply set.ext, intro x, simp [image, exists_and_distrib_right.symm, -exists_and_distrib_right], exact exists_swap end lemma univ_subtype {p : Ξ± β†’ Prop} : (univ : set (subtype p)) = (⋃x (h : p x), {⟨x, h⟩}) := set.ext $ assume ⟨x, h⟩, by simp [h] lemma range_eq_Union {ΞΉ} (f : ΞΉ β†’ Ξ±) : range f = (⋃i, {f i}) := set.ext $ assume a, by simp [@eq_comm Ξ± a] lemma image_eq_Union (f : Ξ± β†’ Ξ²) (s : set Ξ±) : f '' s = (⋃i∈s, {f i}) := set.ext $ assume b, by simp [@eq_comm Ξ² b] @[simp] lemma bUnion_range {f : ΞΉ β†’ Ξ±} {g : Ξ± β†’ set Ξ²} : (⋃x ∈ range f, g x) = (⋃y, g (f y)) := supr_range @[simp] lemma bInter_range {f : ΞΉ β†’ Ξ±} {g : Ξ± β†’ set Ξ²} : (β‹‚x ∈ range f, g x) = (β‹‚y, g (f y)) := infi_range variables {s : set Ξ³} {f : Ξ³ β†’ Ξ±} {g : Ξ± β†’ set Ξ²} @[simp] lemma bUnion_image : (⋃x∈ (f '' s), g x) = (⋃y ∈ s, g (f y)) := supr_image @[simp] lemma bInter_image : (β‹‚x∈ (f '' s), g x) = (β‹‚y ∈ s, g (f y)) := infi_image end image section image2 variables (f : Ξ± β†’ Ξ² β†’ Ξ³) {s : set Ξ±} {t : set Ξ²} lemma Union_image_left : (⋃ a ∈ s, f a '' t) = image2 f s t := by { ext y, split; simp only [mem_Union]; rintros ⟨a, ha, x, hx, ax⟩; exact ⟨a, x, ha, hx, ax⟩ } lemma Union_image_right : (⋃ b ∈ t, (Ξ» a, f a b) '' s) = image2 f s t := by { ext y, split; simp only [mem_Union]; rintros ⟨a, b, c, d, e⟩, exact ⟨c, a, d, b, e⟩, exact ⟨b, d, a, c, e⟩ } end image2 section preimage theorem monotone_preimage {f : Ξ± β†’ Ξ²} : monotone (preimage f) := assume a b h, preimage_mono h @[simp] theorem preimage_Union {ΞΉ : Sort w} {f : Ξ± β†’ Ξ²} {s : ΞΉ β†’ set Ξ²} : preimage f (⋃i, s i) = (⋃i, preimage f (s i)) := set.ext $ by simp [preimage] theorem preimage_bUnion {ΞΉ} {f : Ξ± β†’ Ξ²} {s : set ΞΉ} {t : ΞΉ β†’ set Ξ²} : f ⁻¹' (⋃i ∈ s, t i) = (⋃i ∈ s, f ⁻¹' (t i)) := by simp @[simp] theorem preimage_sUnion {f : Ξ± β†’ Ξ²} {s : set (set Ξ²)} : f ⁻¹' (⋃₀ s) = (⋃t ∈ s, f ⁻¹' t) := set.ext $ by simp [preimage] lemma preimage_Inter {ΞΉ : Sort*} {s : ΞΉ β†’ set Ξ²} {f : Ξ± β†’ Ξ²} : f ⁻¹' (β‹‚ i, s i) = (β‹‚ i, f ⁻¹' s i) := by ext; simp lemma preimage_bInter {s : Ξ³ β†’ set Ξ²} {t : set Ξ³} {f : Ξ± β†’ Ξ²} : f ⁻¹' (β‹‚ i∈t, s i) = (β‹‚ i∈t, f ⁻¹' s i) := by ext; simp @[simp] lemma bUnion_preimage_singleton (f : Ξ± β†’ Ξ²) (s : set Ξ²) : (⋃ y ∈ s, f ⁻¹' {y}) = f ⁻¹' s := by rw [← preimage_bUnion, bUnion_of_singleton] lemma bUnion_range_preimage_singleton (f : Ξ± β†’ Ξ²) : (⋃ y ∈ range f, f ⁻¹' {y}) = univ := by simp end preimage section prod theorem monotone_prod [preorder Ξ±] {f : Ξ± β†’ set Ξ²} {g : Ξ± β†’ set Ξ³} (hf : monotone f) (hg : monotone g) : monotone (Ξ»x, (f x).prod (g x)) := assume a b h, prod_mono (hf h) (hg h) alias monotone_prod ← monotone.set_prod lemma Union_prod_of_monotone [semilattice_sup Ξ±] {s : Ξ± β†’ set Ξ²} {t : Ξ± β†’ set Ξ³} (hs : monotone s) (ht : monotone t) : (⋃ x, (s x).prod (t x)) = (⋃ x, (s x)).prod (⋃ x, (t x)) := begin ext ⟨z, w⟩, simp only [mem_prod, mem_Union, exists_imp_distrib, and_imp, iff_def], split, { intros x hz hw, exact ⟨⟨x, hz⟩, ⟨x, hw⟩⟩ }, { intros x hz x' hw, exact ⟨x βŠ” x', hs le_sup_left hz, ht le_sup_right hw⟩ } end end prod section seq /-- Given a set `s` of functions `Ξ± β†’ Ξ²` and `t : set Ξ±`, `seq s t` is the union of `f '' t` over all `f ∈ s`. -/ def seq (s : set (Ξ± β†’ Ξ²)) (t : set Ξ±) : set Ξ² := {b | βˆƒf∈s, βˆƒa∈t, (f : Ξ± β†’ Ξ²) a = b} lemma seq_def {s : set (Ξ± β†’ Ξ²)} {t : set Ξ±} : seq s t = ⋃f∈s, f '' t := set.ext $ by simp [seq] @[simp] lemma mem_seq_iff {s : set (Ξ± β†’ Ξ²)} {t : set Ξ±} {b : Ξ²} : b ∈ seq s t ↔ βˆƒ (f ∈ s) (a ∈ t), (f : Ξ± β†’ Ξ²) a = b := iff.rfl lemma seq_subset {s : set (Ξ± β†’ Ξ²)} {t : set Ξ±} {u : set Ξ²} : seq s t βŠ† u ↔ (βˆ€f∈s, βˆ€a∈t, (f : Ξ± β†’ Ξ²) a ∈ u) := iff.intro (assume h f hf a ha, h ⟨f, hf, a, ha, rfl⟩) (assume h b ⟨f, hf, a, ha, eq⟩, eq β–Έ h f hf a ha) lemma seq_mono {sβ‚€ s₁ : set (Ξ± β†’ Ξ²)} {tβ‚€ t₁ : set Ξ±} (hs : sβ‚€ βŠ† s₁) (ht : tβ‚€ βŠ† t₁) : seq sβ‚€ tβ‚€ βŠ† seq s₁ t₁ := assume b ⟨f, hf, a, ha, eq⟩, ⟨f, hs hf, a, ht ha, eq⟩ lemma singleton_seq {f : Ξ± β†’ Ξ²} {t : set Ξ±} : set.seq {f} t = f '' t := set.ext $ by simp lemma seq_singleton {s : set (Ξ± β†’ Ξ²)} {a : Ξ±} : set.seq s {a} = (Ξ»f:Ξ±β†’Ξ², f a) '' s := set.ext $ by simp lemma seq_seq {s : set (Ξ² β†’ Ξ³)} {t : set (Ξ± β†’ Ξ²)} {u : set Ξ±} : seq s (seq t u) = seq (seq ((∘) '' s) t) u := begin refine set.ext (assume c, iff.intro _ _), { rintros ⟨f, hfs, b, ⟨g, hg, a, hau, rfl⟩, rfl⟩, exact ⟨f ∘ g, ⟨(∘) f, mem_image_of_mem _ hfs, g, hg, rfl⟩, a, hau, rfl⟩ }, { rintros ⟨fg, ⟨fc, ⟨f, hfs, rfl⟩, g, hgt, rfl⟩, a, ha, rfl⟩, exact ⟨f, hfs, g a, ⟨g, hgt, a, ha, rfl⟩, rfl⟩ } end lemma image_seq {f : Ξ² β†’ Ξ³} {s : set (Ξ± β†’ Ξ²)} {t : set Ξ±} : f '' seq s t = seq ((∘) f '' s) t := by rw [← singleton_seq, ← singleton_seq, seq_seq, image_singleton] lemma prod_eq_seq {s : set Ξ±} {t : set Ξ²} : s.prod t = (prod.mk '' s).seq t := begin ext ⟨a, b⟩, split, { rintros ⟨ha, hb⟩, exact ⟨prod.mk a, ⟨a, ha, rfl⟩, b, hb, rfl⟩ }, { rintros ⟨f, ⟨x, hx, rfl⟩, y, hy, eq⟩, rw ← eq, exact ⟨hx, hy⟩ } end lemma prod_image_seq_comm (s : set Ξ±) (t : set Ξ²) : (prod.mk '' s).seq t = seq ((Ξ»b a, (a, b)) '' t) s := by rw [← prod_eq_seq, ← image_swap_prod, prod_eq_seq, image_seq, ← image_comp, prod.swap] lemma image2_eq_seq (f : Ξ± β†’ Ξ² β†’ Ξ³) (s : set Ξ±) (t : set Ξ²) : image2 f s t = seq (f '' s) t := by { ext, simp } end seq instance : monad set := { pure := Ξ»(Ξ± : Type u) a, {a}, bind := Ξ»(Ξ± Ξ² : Type u) s f, ⋃i∈s, f i, seq := Ξ»(Ξ± Ξ² : Type u), set.seq, map := Ξ»(Ξ± Ξ² : Type u), set.image } section monad variables {Ξ±' Ξ²' : Type u} {s : set Ξ±'} {f : Ξ±' β†’ set Ξ²'} {g : set (Ξ±' β†’ Ξ²')} @[simp] lemma bind_def : s >>= f = ⋃i∈s, f i := rfl @[simp] lemma fmap_eq_image (f : Ξ±' β†’ Ξ²') : f <$> s = f '' s := rfl @[simp] lemma seq_eq_set_seq {Ξ± Ξ² : Type*} (s : set (Ξ± β†’ Ξ²)) (t : set Ξ±) : s <*> t = s.seq t := rfl @[simp] lemma pure_def (a : Ξ±) : (pure a : set Ξ±) = {a} := rfl end monad instance : is_lawful_monad set := { pure_bind := assume Ξ± Ξ² x f, by simp, bind_assoc := assume Ξ± Ξ² Ξ³ s f g, set.ext $ assume a, by simp [exists_and_distrib_right.symm, -exists_and_distrib_right, exists_and_distrib_left.symm, -exists_and_distrib_left, and_assoc]; exact exists_swap, id_map := assume Ξ±, id_map, bind_pure_comp_eq_map := assume Ξ± Ξ² f s, set.ext $ by simp [set.image, eq_comm], bind_map_eq_seq := assume Ξ± Ξ² s t, by simp [seq_def] } instance : is_comm_applicative (set : Type u β†’ Type u) := ⟨ assume Ξ± Ξ² s t, prod_image_seq_comm s t ⟩ section pi lemma pi_def {Ξ± : Type*} {Ο€ : Ξ± β†’ Type*} (i : set Ξ±) (s : Ξ a, set (Ο€ a)) : pi i s = (β‹‚ a∈i, ((Ξ»f:(Ξ a, Ο€ a), f a) ⁻¹' (s a))) := by ext; simp [pi] end pi end set /- disjoint sets -/ section disjoint variables {s t u : set Ξ±} namespace disjoint /-! We define some lemmas in the `disjoint` namespace to be able to use projection notation. -/ theorem union_left (hs : disjoint s u) (ht : disjoint t u) : disjoint (s βˆͺ t) u := hs.sup_left ht theorem union_right (ht : disjoint s t) (hu : disjoint s u) : disjoint s (t βˆͺ u) := ht.sup_right hu lemma preimage {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) {s t : set Ξ²} (h : disjoint s t) : disjoint (f ⁻¹' s) (f ⁻¹' t) := Ξ» x hx, h hx end disjoint namespace set protected theorem disjoint_iff : disjoint s t ↔ s ∩ t βŠ† βˆ… := iff.rfl theorem disjoint_iff_inter_eq_empty : disjoint s t ↔ s ∩ t = βˆ… := disjoint_iff lemma not_disjoint_iff : Β¬disjoint s t ↔ βˆƒx, x ∈ s ∧ x ∈ t := not_forall.trans $ exists_congr $ Ξ» x, not_not lemma disjoint_left : disjoint s t ↔ βˆ€ {a}, a ∈ s β†’ a βˆ‰ t := show (βˆ€ x, Β¬(x ∈ s ∩ t)) ↔ _, from ⟨λ h a, not_and.1 $ h a, Ξ» h a, not_and.2 $ h a⟩ theorem disjoint_right : disjoint s t ↔ βˆ€ {a}, a ∈ t β†’ a βˆ‰ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_of_subset_left (h : s βŠ† u) (d : disjoint u t) : disjoint s t := d.mono_left h theorem disjoint_of_subset_right (h : t βŠ† u) (d : disjoint s u) : disjoint s t := d.mono_right h theorem disjoint_of_subset {s t u v : set Ξ±} (h1 : s βŠ† u) (h2 : t βŠ† v) (d : disjoint u v) : disjoint s t := d.mono h1 h2 @[simp] theorem disjoint_union_left : disjoint (s βˆͺ t) u ↔ disjoint s u ∧ disjoint t u := disjoint_sup_left @[simp] theorem disjoint_union_right : disjoint s (t βˆͺ u) ↔ disjoint s t ∧ disjoint s u := disjoint_sup_right theorem disjoint_diff {a b : set Ξ±} : disjoint a (b \ a) := disjoint_iff.2 (inter_diff_self _ _) theorem disjoint_compl_left (s : set Ξ±) : disjoint sᢜ s := assume a ⟨h₁, hβ‚‚βŸ©, h₁ hβ‚‚ theorem disjoint_compl_right (s : set Ξ±) : disjoint s sᢜ := assume a ⟨h₁, hβ‚‚βŸ©, hβ‚‚ h₁ @[simp] lemma univ_disjoint {s : set Ξ±}: disjoint univ s ↔ s = βˆ… := by simp [set.disjoint_iff_inter_eq_empty] @[simp] lemma disjoint_univ {s : set Ξ±} : disjoint s univ ↔ s = βˆ… := by simp [set.disjoint_iff_inter_eq_empty] @[simp] theorem disjoint_singleton_left {a : Ξ±} {s : set Ξ±} : disjoint {a} s ↔ a βˆ‰ s := by simp [set.disjoint_iff, subset_def]; exact iff.rfl @[simp] theorem disjoint_singleton_right {a : Ξ±} {s : set Ξ±} : disjoint s {a} ↔ a βˆ‰ s := by rw [disjoint.comm]; exact disjoint_singleton_left theorem disjoint_image_image {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ±} {s : set Ξ²} {t : set Ξ³} (h : βˆ€b∈s, βˆ€c∈t, f b β‰  g c) : disjoint (f '' s) (g '' t) := by rintros a ⟨⟨b, hb, eq⟩, ⟨c, hc, rfl⟩⟩; exact h b hb c hc eq theorem pairwise_on_disjoint_fiber (f : Ξ± β†’ Ξ²) (s : set Ξ²) : pairwise_on s (disjoint on (Ξ» y, f ⁻¹' {y})) := Ξ» y₁ _ yβ‚‚ _ hy x ⟨hx₁, hxβ‚‚βŸ©, hy (eq.trans (eq.symm hx₁) hxβ‚‚) lemma preimage_eq_empty {f : Ξ± β†’ Ξ²} {s : set Ξ²} (h : disjoint s (range f)) : f ⁻¹' s = βˆ… := by simpa using h.preimage f lemma preimage_eq_empty_iff {f : Ξ± β†’ Ξ²} {s : set Ξ²} : disjoint s (range f) ↔ f ⁻¹' s = βˆ… := ⟨preimage_eq_empty, Ξ» h, by { simp [eq_empty_iff_forall_not_mem, set.disjoint_iff_inter_eq_empty] at h ⊒, finish }⟩ end set end disjoint namespace set /-- A collection of sets is `pairwise_disjoint`, if any two different sets in this collection are disjoint. -/ def pairwise_disjoint (s : set (set Ξ±)) : Prop := pairwise_on s disjoint lemma pairwise_disjoint.subset {s t : set (set Ξ±)} (h : s βŠ† t) (ht : pairwise_disjoint t) : pairwise_disjoint s := pairwise_on.mono h ht lemma pairwise_disjoint.range {s : set (set Ξ±)} (f : s β†’ set Ξ±) (hf : βˆ€(x : s), f x βŠ† x.1) (ht : pairwise_disjoint s) : pairwise_disjoint (range f) := begin rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ hxy, refine (ht _ x.2 _ y.2 _).mono (hf x) (hf y), intro h, apply hxy, apply congr_arg f, exact subtype.eq h end /- classical -/ lemma pairwise_disjoint.elim {s : set (set Ξ±)} (h : pairwise_disjoint s) {x y : set Ξ±} (hx : x ∈ s) (hy : y ∈ s) (z : Ξ±) (hzx : z ∈ x) (hzy : z ∈ y) : x = y := not_not.1 $ Ξ» h', h x hx y hy h' ⟨hzx, hzy⟩ end set namespace set variables (t : Ξ± β†’ set Ξ²) lemma subset_diff {s t u : set Ξ±} : s βŠ† t \ u ↔ s βŠ† t ∧ disjoint s u := ⟨λ h, ⟨λ x hxs, (h hxs).1, Ξ» x ⟨hxs, hxu⟩, (h hxs).2 hxu⟩, Ξ» ⟨h1, h2⟩ x hxs, ⟨h1 hxs, Ξ» hxu, h2 ⟨hxs, hxu⟩⟩⟩ /-- If `t` is an indexed family of sets, then there is a natural map from `Ξ£ i, t i` to `⋃ i, t i` sending `⟨i, x⟩` to `x`. -/ def sigma_to_Union (x : Ξ£i, t i) : (⋃i, t i) := ⟨x.2, mem_Union.2 ⟨x.1, x.2.2⟩⟩ lemma sigma_to_Union_surjective : surjective (sigma_to_Union t) | ⟨b, hb⟩ := have βˆƒa, b ∈ t a, by simpa using hb, let ⟨a, hb⟩ := this in ⟨⟨a, ⟨b, hb⟩⟩, rfl⟩ lemma sigma_to_Union_injective (h : βˆ€i j, i β‰  j β†’ disjoint (t i) (t j)) : injective (sigma_to_Union t) | ⟨a₁, ⟨b₁, hβ‚βŸ©βŸ© ⟨aβ‚‚, ⟨bβ‚‚, hβ‚‚βŸ©βŸ© eq := have b_eq : b₁ = bβ‚‚, from congr_arg subtype.val eq, have a_eq : a₁ = aβ‚‚, from classical.by_contradiction $ assume ne, have b₁ ∈ t a₁ ∩ t aβ‚‚, from ⟨h₁, b_eq.symm β–Έ hβ‚‚βŸ©, h _ _ ne this, sigma.eq a_eq $ subtype.eq $ by subst b_eq; subst a_eq lemma sigma_to_Union_bijective (h : βˆ€i j, i β‰  j β†’ disjoint (t i) (t j)) : bijective (sigma_to_Union t) := ⟨sigma_to_Union_injective t h, sigma_to_Union_surjective t⟩ /-- Equivalence between a disjoint union and a dependent sum. -/ noncomputable def Union_eq_sigma_of_disjoint {t : Ξ± β†’ set Ξ²} (h : βˆ€i j, i β‰  j β†’ disjoint (t i) (t j)) : (⋃i, t i) ≃ (Ξ£i, t i) := (equiv.of_bijective _ $ sigma_to_Union_bijective t h).symm /-- Equivalence between a disjoint bounded union and a dependent sum. -/ noncomputable def bUnion_eq_sigma_of_disjoint {s : set Ξ±} {t : Ξ± β†’ set Ξ²} (h : pairwise_on s (disjoint on t)) : (⋃i∈s, t i) ≃ (Ξ£i:s, t i.val) := equiv.trans (equiv.set_congr (bUnion_eq_Union _ _)) $ Union_eq_sigma_of_disjoint $ assume ⟨i, hi⟩ ⟨j, hj⟩ ne, h _ hi _ hj $ assume eq, ne $ subtype.eq eq end set
4392af94def7d61b8acea9ad5473a226a5fc9b88
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/trace.lean
265094f69eb158c567b79c90c37807ce31f4351d
[ "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
5,430
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import linear_algebra.bilinear_form import linear_algebra.char_poly.coeff import linear_algebra.trace import ring_theory.power_basis /-! # Trace for (finite) ring extensions. Suppose we have an `R`-algebra `S` with a finite basis. For each `s : S`, the trace of the linear map given by multiplying by `s` gives information about the roots of the minimal polynomial of `s` over `R`. ## Implementation notes Typically, the trace is defined specifically for finite field extensions. The definition is as general as possible and the assumption that we have fields or that the extension is finite is added to the lemmas as needed. We only define the trace for left multiplication (`algebra.left_mul_matrix`, i.e. `algebra.lmul_left`). For now, the definitions assume `S` is commutative, so the choice doesn't matter anyway. ## References * https://en.wikipedia.org/wiki/Field_trace -/ universes u v w variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] variables [algebra R S] [algebra R T] variables {K L : Type*} [field K] [field L] [algebra K L] variables {ΞΉ : Type w} [fintype ΞΉ] open finite_dimensional open linear_map open matrix open_locale big_operators open_locale matrix namespace algebra variables (b : basis ΞΉ R S) variables (R S) /-- The trace of an element `s` of an `R`-algebra is the trace of `(*) s`, as an `R`-linear map. -/ @[simps] noncomputable def trace : S β†’β‚—[R] R := (linear_map.trace R S).comp (lmul R S).to_linear_map variables {S} lemma trace_eq_zero_of_not_exists_basis (h : Β¬ βˆƒ (s : finset S), nonempty (basis s R S)) : trace R S = 0 := by { ext s, simp [linear_map.trace, h] } include b variables {R} -- Can't be a `simp` lemma because it depends on a choice of basis lemma trace_eq_matrix_trace [decidable_eq ΞΉ] (b : basis ΞΉ R S) (s : S) : trace R S s = matrix.trace _ R _ (algebra.left_mul_matrix b s) := by rw [trace_apply, linear_map.trace_eq_matrix_trace _ b, to_matrix_lmul_eq] /-- If `x` is in the base field `K`, then the trace is `[L : K] * x`. -/ lemma trace_algebra_map_of_basis (x : R) : trace R S (algebra_map R S x) = fintype.card ΞΉ β€’ x := begin haveI := classical.dec_eq ΞΉ, rw [trace_apply, linear_map.trace_eq_matrix_trace R b, trace_diag], convert finset.sum_const _, ext i, simp, end omit b /-- If `x` is in the base field `K`, then the trace is `[L : K] * x`. (If `L` is not finite-dimensional over `K`, then `trace` and `finrank` return `0`.) -/ @[simp] lemma trace_algebra_map (x : K) : trace K L (algebra_map K L x) = finrank K L β€’ x := begin by_cases H : βˆƒ (s : finset L), nonempty (basis s K L), { rw [trace_algebra_map_of_basis H.some_spec.some, finrank_eq_card_basis H.some_spec.some] }, { simp [trace_eq_zero_of_not_exists_basis K H, finrank_eq_zero_of_not_exists_basis_finset H] } end section trace_form variables (R S) /-- The `trace_form` maps `x y : S` to the trace of `x * y`. It is a symmetric bilinear form and is nondegenerate if the extension is separable. -/ noncomputable def trace_form : bilin_form R S := (linear_map.comprβ‚‚ (lmul R S).to_linear_map (trace R S)).to_bilin variables {S} -- This is a nicer lemma than the one produced by `@[simps] def trace_form`. @[simp] lemma trace_form_apply (x y : S) : trace_form R S x y = trace R S (x * y) := rfl lemma trace_form_is_sym : sym_bilin_form.is_sym (trace_form R S) := Ξ» x y, congr_arg (trace R S) (mul_comm _ _) lemma trace_form_to_matrix [decidable_eq ΞΉ] (i j) : bilin_form.to_matrix b (trace_form R S) i j = trace R S (b i * b j) := by rw [bilin_form.to_matrix_apply, trace_form_apply] lemma trace_form_to_matrix_power_basis (h : power_basis R S) : bilin_form.to_matrix h.basis (trace_form R S) = Ξ» i j, (trace R S (h.gen ^ (i + j : β„•))) := by { ext, rw [trace_form_to_matrix, pow_add, h.basis_eq_pow, h.basis_eq_pow] } end trace_form section eq_prod_roots open polynomial variables {F : Type*} [field F] variables [algebra K S] [algebra K F] lemma trace_gen_eq_sum_roots [nontrivial S] (pb : power_basis K S) (hf : (minpoly K pb.gen).splits (algebra_map K F)) : algebra_map K F (trace K S pb.gen) = ((minpoly K pb.gen).map (algebra_map K F)).roots.sum := begin have d_pos : 0 < pb.dim := power_basis.dim_pos pb, have d_pos' : 0 < (minpoly K pb.gen).nat_degree, { simpa }, haveI : nonempty (fin pb.dim) := ⟨⟨0, d_pos⟩⟩, -- Write the LHS as the `d-1`'th coefficient of `minpoly K pb.gen` rw [trace_eq_matrix_trace pb.basis, trace_eq_neg_char_poly_coeff, char_poly_left_mul_matrix, ring_hom.map_neg, ← pb.nat_degree_minpoly, fintype.card_fin, ← next_coeff_of_pos_nat_degree _ d_pos', ← next_coeff_map (algebra_map K F).injective], -- Rewrite `minpoly K pb.gen` as a product over the roots. conv_lhs { rw eq_prod_roots_of_splits hf }, rw [monic.next_coeff_mul, next_coeff_C_eq_zero, zero_add, monic.next_coeff_multiset_prod], -- And conclude both sides are the same. simp_rw [next_coeff_X_sub_C, multiset.sum_map_neg, neg_neg], -- Now we deal with the side conditions. { intros, apply monic_X_sub_C }, { convert monic_one, simp [(minpoly.monic pb.is_integral_gen).leading_coeff] }, { apply monic_multiset_prod_of_monic, intros, apply monic_X_sub_C }, end end eq_prod_roots end algebra
43a1fe93681c38d91e5a201bd13f2463d65732d5
57fdc8de88f5ea3bfde4325e6ecd13f93a274ab5
/data/int/basic.lean
edcb5aa36d9c62b6cc56f7ba7be40e698d4a25d4
[ "Apache-2.0" ]
permissive
louisanu/mathlib
11f56f2d40dc792bc05ee2f78ea37d73e98ecbfe
2bd5e2159d20a8f20d04fc4d382e65eea775ed39
refs/heads/master
1,617,706,993,439
1,523,163,654,000
1,523,163,654,000
124,519,997
0
0
Apache-2.0
1,520,588,283,000
1,520,588,283,000
null
UTF-8
Lean
false
false
42,188
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The integers, with addition, multiplication, and subtraction. -/ import data.nat.basic algebra.char_zero algebra.order_functions open nat namespace int instance : inhabited β„€ := ⟨0⟩ meta instance : has_to_format β„€ := ⟨λ z, int.rec_on z (Ξ» k, ↑k) (Ξ» k, "-("++↑k++"+1)")⟩ attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ @[simp] theorem coe_nat_mul_neg_succ (m n : β„•) : (m : β„€) * -[1+ n] = -(m * succ n) := rfl @[simp] theorem neg_succ_mul_coe_nat (m n : β„•) : -[1+ m] * n = -(succ m * n) := rfl @[simp] theorem neg_succ_mul_neg_succ (m n : β„•) : -[1+ m] * -[1+ n] = succ m * succ n := rfl theorem coe_nat_le {m n : β„•} : (↑m : β„€) ≀ ↑n ↔ m ≀ n := coe_nat_le_coe_nat_iff m n theorem coe_nat_lt {m n : β„•} : (↑m : β„€) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n theorem coe_nat_inj' {m n : β„•} : (↑m : β„€) = ↑n ↔ m = n := int.coe_nat_eq_coe_nat_iff m n @[simp] theorem coe_nat_pos {n : β„•} : (0 : β„€) < n ↔ 0 < n := by rw [← int.coe_nat_zero, coe_nat_lt] @[simp] theorem coe_nat_eq_zero {n : β„•} : (n : β„€) = 0 ↔ n = 0 := by rw [← int.coe_nat_zero, coe_nat_inj'] @[simp] theorem coe_nat_ne_zero {n : β„•} : (n : β„€) β‰  0 ↔ n β‰  0 := not_congr coe_nat_eq_zero /- succ and pred -/ /-- Immediate successor of an integer: `succ n = n + 1` -/ def succ (a : β„€) := a + 1 /-- Immediate predecessor of an integer: `pred n = n - 1` -/ def pred (a : β„€) := a - 1 theorem nat_succ_eq_int_succ (n : β„•) : (nat.succ n : β„€) = int.succ n := rfl theorem pred_succ (a : β„€) : pred (succ a) = a := add_sub_cancel _ _ theorem succ_pred (a : β„€) : succ (pred a) = a := sub_add_cancel _ _ theorem neg_succ (a : β„€) : -succ a = pred (-a) := neg_add _ _ theorem succ_neg_succ (a : β„€) : succ (-succ a) = -a := by rw [neg_succ, succ_pred] theorem neg_pred (a : β„€) : -pred a = succ (-a) := by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg] theorem pred_neg_pred (a : β„€) : pred (-pred a) = -a := by rw [neg_pred, pred_succ] theorem pred_nat_succ (n : β„•) : pred (nat.succ n) = n := pred_succ n theorem neg_nat_succ (n : β„•) : -(nat.succ n : β„€) = pred (-n) := neg_succ n theorem succ_neg_nat_succ (n : β„•) : succ (-nat.succ n) = -n := succ_neg_succ n theorem lt_succ_self (a : β„€) : a < succ a := lt_add_of_pos_right _ zero_lt_one theorem pred_self_lt (a : β„€) : pred a < a := sub_lt_self _ zero_lt_one theorem add_one_le_iff {a b : β„€} : a + 1 ≀ b ↔ a < b := iff.rfl theorem lt_add_one_iff {a b : β„€} : a < b + 1 ↔ a ≀ b := @add_le_add_iff_right _ _ a b 1 theorem sub_one_le_iff {a b : β„€} : a - 1 < b ↔ a ≀ b := sub_lt_iff_lt_add.trans lt_add_one_iff theorem le_sub_one_iff {a b : β„€} : a ≀ b - 1 ↔ a < b := le_sub_iff_add_le protected lemma induction_on {p : β„€ β†’ Prop} (i : β„€) (hz : p 0) (hp : βˆ€i, p i β†’ p (i + 1)) (hn : βˆ€i, p i β†’ p (i - 1)) : p i := begin induction i, { induction i, { exact hz }, { exact hp _ i_ih } }, { have : βˆ€n:β„•, p (- n), { intro n, induction n, { simp [hz] }, { have := hn _ n_ih, simpa } }, exact this (i + 1) } end /- / -/ theorem of_nat_div (m n : nat) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl theorem neg_succ_of_nat_div (m : nat) {b : β„€} (H : b > 0) : -[1+m] / b = -(m / b + 1) := match b, eq_succ_of_zero_lt H with ._, ⟨n, rfl⟩ := rfl end @[simp] protected theorem div_neg : βˆ€ (a b : β„€), a / -b = -(a / b) | (m : β„•) 0 := show of_nat (m / 0) = -(m / 0 : β„•), by rw nat.div_zero; refl | (m : β„•) (n+1:β„•) := rfl | 0 -[1+ n] := rfl | (m+1:β„•) -[1+ n] := (neg_neg _).symm | -[1+ m] 0 := rfl | -[1+ m] (n+1:β„•) := rfl | -[1+ m] -[1+ n] := rfl theorem div_of_neg_of_pos {a b : β„€} (Ha : a < 0) (Hb : b > 0) : a / b = -((-a - 1) / b + 1) := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := by change (- -[1+ m] : β„€) with (m+1 : β„€); rw add_sub_cancel; refl end protected theorem div_nonneg {a b : β„€} (Ha : a β‰₯ 0) (Hb : b β‰₯ 0) : a / b β‰₯ 0 := match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := coe_zero_le _ end protected theorem div_nonpos {a b : β„€} (Ha : a β‰₯ 0) (Hb : b ≀ 0) : a / b ≀ 0 := nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb) theorem div_neg' {a b : β„€} (Ha : a < 0) (Hb : b > 0) : a / b < 0 := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := neg_succ_lt_zero _ end @[simp] protected theorem zero_div : βˆ€ (b : β„€), 0 / b = 0 | 0 := rfl | (n+1:β„•) := rfl | -[1+ n] := rfl @[simp] protected theorem div_zero : βˆ€ (a : β„€), a / 0 = 0 | 0 := rfl | (n+1:β„•) := rfl | -[1+ n] := rfl @[simp] protected theorem div_one : βˆ€ (a : β„€), a / 1 = a | 0 := rfl | (n+1:β„•) := congr_arg of_nat (nat.div_one _) | -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _) theorem div_eq_zero_of_lt {a b : β„€} (H1 : 0 ≀ a) (H2 : a < b) : a / b = 0 := match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2 end theorem div_eq_zero_of_lt_abs {a b : β„€} (H1 : 0 ≀ a) (H2 : a < abs b) : a / b = 0 := match b, abs b, abs_eq_nat_abs b, H2 with | (n : β„•), ._, rfl, H2 := div_eq_zero_of_lt H1 H2 | -[1+ n], ._, rfl, H2 := neg_inj $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2 end protected theorem add_mul_div_right (a b : β„€) {c : β„€} (H : c β‰  0) : (a + b * c) / c = a / c + b := have βˆ€ {k n : β„•} {a : β„€}, (a + n * k.succ) / k.succ = a / k.succ + n, from Ξ» k n a, match a with | (m : β„•) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos | -[1+ m] := show ((n * k.succ:β„•) - m.succ : β„€) / k.succ = n - (m / k.succ + 1 : β„•), begin cases lt_or_ge m (n*k.succ) with h h, { rw [← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)], apply congr_arg of_nat, rw [mul_comm, nat.mul_sub_div], rwa mul_comm }, { change (↑(n * nat.succ k) - (m + 1) : β„€) / ↑(nat.succ k) = ↑n - ((m / nat.succ k : β„•) + 1), rw [← sub_sub, ← sub_sub, ← neg_sub (m:β„€), ← neg_sub _ (n:β„€), ← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h), ← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'], { apply congr_arg neg_succ_of_nat, rw [mul_comm, nat.sub_mul_div], rwa mul_comm } } end end, have βˆ€ {a b c : β„€}, c > 0 β†’ (a + b * c) / c = a / c + b, from Ξ» a b c H, match c, eq_succ_of_zero_lt H, b with | ._, ⟨k, rfl⟩, (n : β„•) := this | ._, ⟨k, rfl⟩, -[1+ n] := show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from eq_sub_of_add_eq $ by rw [← this, sub_add_cancel] end, match lt_trichotomy c 0 with | or.inl hlt := neg_inj $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg]; apply this (neg_pos_of_neg hlt) | or.inr (or.inl heq) := absurd heq H | or.inr (or.inr hgt) := this hgt end protected theorem add_mul_div_left (a : β„€) {b : β„€} (c : β„€) (H : b β‰  0) : (a + b * c) / b = a / b + c := by rw [mul_comm, int.add_mul_div_right _ _ H] @[simp] protected theorem mul_div_cancel (a : β„€) {b : β„€} (H : b β‰  0) : a * b / b = a := by have := int.add_mul_div_right 0 a H; rwa [zero_add, int.zero_div, zero_add] at this @[simp] protected theorem mul_div_cancel_left {a : β„€} (b : β„€) (H : a β‰  0) : a * b / a = b := by rw [mul_comm, int.mul_div_cancel _ H] @[simp] protected theorem div_self {a : β„€} (H : a β‰  0) : a / a = 1 := by have := int.mul_div_cancel 1 H; rwa one_mul at this /- mod -/ theorem of_nat_mod (m n : nat) : (m % n : β„€) = of_nat (m % n) := rfl @[simp] theorem coe_nat_mod (m n : β„•) : (↑(m % n) : β„€) = ↑m % ↑n := rfl theorem neg_succ_of_nat_mod (m : β„•) {b : β„€} (bpos : b > 0) : -[1+m] % b = b - 1 - m % b := by rw [sub_sub, add_comm]; exact match b, eq_succ_of_zero_lt bpos with ._, ⟨n, rfl⟩ := rfl end @[simp] theorem mod_neg : βˆ€ (a b : β„€), a % -b = a % b | (m : β„•) n := @congr_arg β„• β„€ _ _ (Ξ» i, ↑(m % i)) (nat_abs_neg _) | -[1+ m] n := @congr_arg β„• β„€ _ _ (Ξ» i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _) @[simp] theorem mod_abs (a b : β„€) : a % (abs b) = a % b := abs_by_cases (Ξ» i, a % i = a % b) rfl (mod_neg _ _) @[simp] theorem zero_mod (b : β„€) : 0 % b = 0 := congr_arg of_nat $ nat.zero_mod _ @[simp] theorem mod_zero : βˆ€ (a : β„€), a % 0 = a | (m : β„•) := congr_arg of_nat $ nat.mod_zero _ | -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _ @[simp] theorem mod_one : βˆ€ (a : β„€), a % 1 = 0 | (m : β„•) := congr_arg of_nat $ nat.mod_one _ | -[1+ m] := show (1 - (m % 1).succ : β„€) = 0, by rw nat.mod_one; refl theorem mod_eq_of_lt {a b : β„€} (H1 : 0 ≀ a) (H2 : a < b) : a % b = a := match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2) end theorem mod_nonneg : βˆ€ (a : β„€) {b : β„€}, b β‰  0 β†’ a % b β‰₯ 0 | (m : β„•) n H := coe_zero_le _ | -[1+ m] n H := sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H) theorem mod_lt_of_pos (a : β„€) {b : β„€} (H : b > 0) : a % b < b := match a, b, eq_succ_of_zero_lt H with | (m : β„•), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _)) | -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _) end theorem mod_lt (a : β„€) {b : β„€} (H : b β‰  0) : a % b < abs b := by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos_of_ne_zero H) theorem mod_add_div_aux (m n : β„•) : (n - (m % n + 1) - (n * (m / n) + n) : β„€) = -[1+ m] := begin rw [← sub_sub, neg_succ_of_nat_coe, sub_sub (n:β„€)], apply eq_neg_of_eq_neg, rw [neg_sub, sub_sub_self, add_right_comm], exact @congr_arg β„• β„€ _ _ (Ξ»i, (i + 1 : β„€)) (nat.mod_add_div _ _).symm end theorem mod_add_div : βˆ€ (a b : β„€), a % b + b * (a / b) = a | (m : β„•) 0 := congr_arg of_nat (nat.mod_add_div _ _) | (m : β„•) (n+1:β„•) := congr_arg of_nat (nat.mod_add_div _ _) | 0 -[1+ n] := rfl | (m+1:β„•) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : β„•) : β„€) = _, by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _) | -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl | -[1+ m] (n+1:β„•) := mod_add_div_aux m n.succ | -[1+ m] -[1+ n] := mod_add_div_aux m n.succ theorem mod_def (a b : β„€) : a % b = a - b * (a / b) := eq_sub_of_add_eq (mod_add_div _ _) @[simp] theorem add_mul_mod_self {a b c : β„€} : (a + b * c) % c = a % c := if cz : c = 0 then by rw [cz, mul_zero, add_zero] else by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz, mul_add, mul_comm, add_sub_add_right_eq_sub] @[simp] theorem add_mul_mod_self_left (a b c : β„€) : (a + b * c) % b = a % b := by rw [mul_comm, add_mul_mod_self] @[simp] theorem add_mod_self {a b : β„€} : (a + b) % b = a % b := by have := add_mul_mod_self_left a b 1; rwa mul_one at this @[simp] theorem add_mod_self_left {a b : β„€} : (a + b) % a = b % a := by rw [add_comm, add_mod_self] @[simp] theorem mod_add_mod (m n k : β„€) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : β„€) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] theorem add_mod_eq_add_mod_right {m n k : β„€} (i : β„€) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : β„€} (i : β„€) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] theorem mod_add_cancel_right {m n k : β„€} (i) : (m + i) % n = (k + i) % n ↔ m % n = k % n := ⟨λ H, by have := add_mod_eq_add_mod_right (-i) H; rwa [add_neg_cancel_right, add_neg_cancel_right] at this, add_mod_eq_add_mod_right _⟩ theorem mod_add_cancel_left {m n k i : β„€} : (i + m) % n = (i + k) % n ↔ m % n = k % n := by rw [add_comm, add_comm i, mod_add_cancel_right] theorem mod_sub_cancel_right {m n k : β„€} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n := mod_add_cancel_right _ theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : β„€} : m % n = k % n ↔ (m - k) % n = 0 := (mod_sub_cancel_right k).symm.trans $ by simp @[simp] theorem mul_mod_left (a b : β„€) : (a * b) % b = 0 := by rw [← zero_add (a * b), add_mul_mod_self, zero_mod] @[simp] theorem mul_mod_right (a b : β„€) : (a * b) % a = 0 := by rw [mul_comm, mul_mod_left] @[simp] theorem mod_self {a : β„€} : a % a = 0 := by have := mul_mod_left 1 a; rwa one_mul at this /- properties of / and % -/ @[simp] theorem mul_div_mul_of_pos {a : β„€} (b c : β„€) (H : a > 0) : a * b / (a * c) = b / c := suffices βˆ€ (m k : β„•) (b : β„€), (m.succ * b / (m.succ * k) : β„€) = b / k, from match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with | ._, ⟨m, rfl⟩, ._, ⟨k, or.inl rfl⟩ := this _ _ _ | ._, ⟨m, rfl⟩, ._, ⟨k, or.inr rfl⟩ := by rw [← neg_mul_eq_mul_neg, int.div_neg, int.div_neg]; apply congr_arg has_neg.neg; apply this end, Ξ» m k b, match b, k with | (n : β„•), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos) | -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero] | -[1+ n], k+1 := congr_arg neg_succ_of_nat $ show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin apply nat.div_eq_of_lt_le, { refine le_trans _ (nat.le_add_right _ _), rw [← nat.mul_div_mul _ _ m.succ_pos], apply nat.div_mul_le_self }, { change m.succ * n.succ ≀ _, rw [mul_left_comm], apply nat.mul_le_mul_left, apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1, apply nat.lt_succ_self } end end @[simp] theorem mul_div_mul_of_pos_left (a : β„€) {b : β„€} (c : β„€) (H : b > 0) : a * b / (c * b) = a / c := by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H] @[simp] theorem mul_mod_mul_of_pos {a : β„€} (b c : β„€) (H : a > 0) : a * b % (a * c) = a * (b % c) := by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc] theorem lt_div_add_one_mul_self (a : β„€) {b : β„€} (H : b > 0) : a < (a / b + 1) * b := by rw [add_mul, one_mul, mul_comm]; apply lt_add_of_sub_left_lt; rw [← mod_def]; apply mod_lt_of_pos _ H theorem abs_div_le_abs : βˆ€ (a b : β„€), abs (a / b) ≀ abs a := suffices βˆ€ (a : β„€) (n : β„•), abs (a / n) ≀ abs a, from Ξ» a b, match b, eq_coe_or_neg b with | ._, ⟨n, or.inl rfl⟩ := this _ _ | ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this end, Ξ» a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact coe_nat_le_coe_nat_of_le (match a, n with | (m : β„•), n := nat.div_le_self _ _ | -[1+ m], 0 := nat.zero_le _ | -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _) end) theorem div_le_self {a : β„€} (b : β„€) (Ha : a β‰₯ 0) : a / b ≀ a := by have := le_trans (le_abs_self _) (abs_div_le_abs a b); rwa [abs_of_nonneg Ha] at this theorem mul_div_cancel_of_mod_eq_zero {a b : β„€} (H : a % b = 0) : b * (a / b) = a := by have := mod_add_div a b; rwa [H, zero_add] at this theorem div_mul_cancel_of_mod_eq_zero {a b : β„€} (H : a % b = 0) : a / b * b = a := by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H] /- dvd -/ theorem coe_nat_dvd {m n : β„•} : (↑m : β„€) ∣ ↑n ↔ m ∣ n := ⟨λ ⟨a, ae⟩, m.eq_zero_or_pos.elim (Ξ»m0, by simp [m0] at ae; simp [ae, m0]) (Ξ»m0l, by { cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left β„€ _ m a (by simp [ae.symm]) (by simpa using m0l)) with k e, subst a, exact ⟨k, int.coe_nat_inj ae⟩ }), Ξ» ⟨k, e⟩, dvd.intro k $ by rw [e, int.coe_nat_mul]⟩ theorem dvd_antisymm {a b : β„€} (H1 : a β‰₯ 0) (H2 : b β‰₯ 0) : a ∣ b β†’ b ∣ a β†’ a = b := begin rw [← abs_of_nonneg H1, ← abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs], rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'], apply nat.dvd_antisymm end theorem dvd_of_mod_eq_zero {a b : β„€} (H : b % a = 0) : a ∣ b := ⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩ theorem mod_eq_zero_of_dvd : βˆ€ {a b : β„€}, a ∣ b β†’ b % a = 0 | a ._ ⟨c, rfl⟩ := mul_mod_right _ _ theorem dvd_iff_mod_eq_zero (a b : β„€) : a ∣ b ↔ b % a = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ theorem nat_abs_dvd {a b : β„€} : (a.nat_abs : β„€) ∣ b ↔ a ∣ b := (nat_abs_eq a).elim (Ξ» e, by rw ← e) (Ξ» e, by rw [← neg_dvd_iff_dvd, ← e]) theorem dvd_nat_abs {a b : β„€} : a ∣ b.nat_abs ↔ a ∣ b := (nat_abs_eq b).elim (Ξ» e, by rw ← e) (Ξ» e, by rw [← dvd_neg_iff_dvd, ← e]) instance decidable_dvd : @decidable_rel β„€ (∣) := assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem div_mul_cancel {a b : β„€} (H : b ∣ a) : a / b * b = a := div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H) protected theorem mul_div_cancel' {a b : β„€} (H : a ∣ b) : a * (b / a) = b := by rw [mul_comm, int.div_mul_cancel H] protected theorem mul_div_assoc (a : β„€) : βˆ€ {b c : β„€}, c ∣ b β†’ (a * b) / c = a * (b / c) | ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz] theorem div_dvd_div : βˆ€ {a b c : β„€} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a | a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az]; apply dvd_mul_right protected theorem eq_mul_of_div_eq_right {a b c : β„€} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, int.mul_div_cancel' H1] protected theorem div_eq_of_eq_mul_right {a b c : β„€} (H1 : b β‰  0) (H2 : a = b * c) : a / b = c := by rw [H2, int.mul_div_cancel_left _ H1] protected theorem div_eq_iff_eq_mul_right {a b c : β„€} (H : b β‰  0) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : β„€} (H : b β‰  0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : β„€} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2] protected theorem div_eq_of_eq_mul_left {a b c : β„€} (H1 : b β‰  0) (H2 : a = c * b) : a / b = c := int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2]) theorem neg_div_of_dvd : βˆ€ {a b : β„€} (H : b ∣ a), -a / b = -(a / b) | ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz] theorem div_sign : βˆ€ a b, a / sign b = a * sign b | a (n+1:β„•) := by unfold sign; simp | a 0 := by simp [sign] | a -[1+ n] := by simp [sign] @[simp] theorem sign_mul : βˆ€ a b, sign (a * b) = sign a * sign b | a 0 := by simp | 0 b := by simp | (m+1:β„•) (n+1:β„•) := rfl | (m+1:β„•) -[1+ n] := rfl | -[1+ m] (n+1:β„•) := rfl | -[1+ m] -[1+ n] := rfl protected theorem sign_eq_div_abs (a : β„€) : sign a = a / (abs a) := if az : a = 0 then by simp [az] else (int.div_eq_of_eq_mul_left (mt eq_zero_of_abs_eq_zero az) (sign_mul_abs _).symm).symm theorem mul_sign : βˆ€ (i : β„€), i * sign i = nat_abs i | (n+1:β„•) := mul_one _ | 0 := mul_zero _ | -[1+ n] := mul_neg_one _ theorem le_of_dvd {a b : β„€} (bpos : b > 0) (H : a ∣ b) : a ≀ b := match a, b, eq_succ_of_zero_lt bpos, H with | (m : β„•), ._, ⟨n, rfl⟩, H := coe_nat_le_coe_nat_of_le $ nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H | -[1+ m], ._, ⟨n, rfl⟩, _ := le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _) end theorem eq_one_of_dvd_one {a : β„€} (H : a β‰₯ 0) (H' : a ∣ 1) : a = 1 := match a, eq_coe_of_zero_le H, H' with | ._, ⟨n, rfl⟩, H' := congr_arg coe $ nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H' end theorem eq_one_of_mul_eq_one_right {a b : β„€} (H : a β‰₯ 0) (H' : a * b = 1) : a = 1 := eq_one_of_dvd_one H ⟨b, H'.symm⟩ theorem eq_one_of_mul_eq_one_left {a b : β„€} (H : b β‰₯ 0) (H' : a * b = 1) : b = 1 := eq_one_of_mul_eq_one_right H (by rw [mul_comm, H']) /- / and ordering -/ protected theorem div_mul_le (a : β„€) {b : β„€} (H : b β‰  0) : a / b * b ≀ a := le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H protected theorem div_le_of_le_mul {a b c : β„€} (H : c > 0) (H' : a ≀ b * c) : a / c ≀ b := le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H protected theorem mul_lt_of_lt_div {a b c : β„€} (H : c > 0) (H3 : a < b / c) : a * c < b := lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3) protected theorem mul_le_of_le_div {a b c : β„€} (H1 : c > 0) (H2 : a ≀ b / c) : a * c ≀ b := le_trans (mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1)) protected theorem le_div_of_mul_le {a b c : β„€} (H1 : c > 0) (H2 : a * c ≀ b) : a ≀ b / c := le_of_lt_add_one $ lt_of_mul_lt_mul_right (lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1) protected theorem le_div_iff_mul_le {a b c : β„€} (H : c > 0) : a ≀ b / c ↔ a * c ≀ b := ⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩ protected theorem div_le_div {a b c : β„€} (H : c > 0) (H' : a ≀ b) : a / c ≀ b / c := int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H') protected theorem div_lt_of_lt_mul {a b c : β„€} (H : c > 0) (H' : a < b * c) : a / c < b := lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H') protected theorem lt_mul_of_div_lt {a b c : β„€} (H1 : c > 0) (H2 : a / c < b) : a < b * c := lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2) protected theorem div_lt_iff_lt_mul {a b c : β„€} (H : c > 0) : a / c < b ↔ a < b * c := ⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩ protected theorem le_mul_of_div_le {a b c : β„€} (H1 : b β‰₯ 0) (H2 : b ∣ a) (H3 : a / b ≀ c) : a ≀ c * b := by rw [← int.div_mul_cancel H2]; exact mul_le_mul_of_nonneg_right H3 H1 protected theorem lt_div_of_mul_lt {a b c : β„€} (H1 : b β‰₯ 0) (H2 : b ∣ c) (H3 : a * b < c) : a < c / b := lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3) protected theorem lt_div_iff_mul_lt {a b : β„€} (c : β„€) (H : c > 0) (H' : c ∣ b) : a < b / c ↔ a * c < b := ⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩ theorem div_pos_of_pos_of_dvd {a b : β„€} (H1 : a > 0) (H2 : b β‰₯ 0) (H3 : b ∣ a) : a / b > 0 := int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul) theorem div_eq_div_of_mul_eq_mul {a b c d : β„€} (H1 : b ∣ a) (H2 : d ∣ c) (H3 : b β‰  0) (H4 : d β‰  0) (H5 : a * d = b * c) : a / b = c / d := int.div_eq_of_eq_mul_right H3 $ by rw [← int.mul_div_assoc _ H2]; exact (int.div_eq_of_eq_mul_left H4 H5.symm).symm theorem of_nat_add_neg_succ_of_nat_of_lt {m n : β„•} (h : m < n.succ) : of_nat m + -[1+n] = -[1+ n - m] := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = (n - m).succ, apply succ_sub, apply le_of_lt_succ h, simp [*, sub_nat_nat] end theorem of_nat_add_neg_succ_of_nat_of_ge {m n : β„•} (h : m β‰₯ n.succ) : of_nat m + -[1+n] = of_nat (m - n.succ) := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = 0, apply sub_eq_zero_of_le h, simp [*, sub_nat_nat] end @[simp] theorem neg_add_neg (m n : β„•) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl /- nat abs -/ attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one theorem nat_abs_add_le (a b : β„€) : nat_abs (a + b) ≀ nat_abs a + nat_abs b := begin have, { refine (Ξ» a b : β„•, sub_nat_nat_elim a b.succ (Ξ» m n i, n = b.succ β†’ nat_abs i ≀ (m + b).succ) _ _ rfl); intros i n e, { subst e, rw [add_comm _ i, add_assoc], exact nat.le_add_right i (b.succ + b).succ }, { apply succ_le_succ, rw [← succ_inj e, ← add_assoc, add_comm], apply nat.le_add_right } }, cases a; cases b with b b; simp [nat_abs, nat.succ_add]; try {refl}; [skip, rw add_comm a b]; apply this end theorem nat_abs_neg_of_nat (n : nat) : nat_abs (neg_of_nat n) = n := by cases n; refl theorem nat_abs_mul (a b : β„€) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) := by cases a; cases b; simp [(*), int.mul, nat_abs_neg_of_nat] theorem neg_succ_of_nat_eq' (m : β„•) : -[1+ m] = -m - 1 := by simp [neg_succ_of_nat_eq] /- to_nat -/ theorem to_nat_eq_max : βˆ€ (a : β„€), (to_nat a : β„€) = max a 0 | (n : β„•) := (max_eq_left (coe_zero_le n)).symm | -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm @[simp] theorem to_nat_of_nonneg {a : β„€} (h : 0 ≀ a) : (to_nat a : β„€) = a := by rw [to_nat_eq_max, max_eq_left h] @[simp] theorem to_nat_coe_nat (n : β„•) : to_nat ↑n = n := rfl theorem le_to_nat (a : β„€) : a ≀ to_nat a := by rw [to_nat_eq_max]; apply le_max_left @[simp] theorem to_nat_le (a : β„€) (n : β„•) : to_nat a ≀ n ↔ a ≀ n := by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff]; exact and_iff_left (coe_zero_le _) /- bitwise ops -/ @[simp] lemma bodd_zero : bodd 0 = ff := rfl @[simp] lemma bodd_one : bodd 1 = tt := rfl @[simp] lemma bodd_two : bodd 2 = ff := rfl @[simp] lemma bodd_sub_nat_nat (m n : β„•) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd := by apply sub_nat_nat_elim m n (Ξ» m n i, bodd i = bxor m.bodd n.bodd); intros i m; simp [bodd]; cases i.bodd; cases m.bodd; refl @[simp] lemma bodd_neg_of_nat (n : β„•) : bodd (neg_of_nat n) = n.bodd := by cases n; simp; refl @[simp] lemma bodd_neg (n : β„€) : bodd (-n) = bodd n := by cases n; unfold has_neg.neg; simp [int.coe_nat_eq, int.neg, bodd] @[simp] lemma bodd_add (m n : β„€) : bodd (m + n) = bxor (bodd m) (bodd n) := by cases m with m m; cases n with n n; unfold has_add.add; simp [int.add, bodd]; cases m.bodd; cases n.bodd; refl @[simp] lemma bodd_mul (m n : β„€) : bodd (m * n) = bodd m && bodd n := by cases m with m m; cases n with n n; unfold has_mul.mul; simp [int.mul, bodd]; cases m.bodd; cases n.bodd; refl theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | (n : β„•) := by rw [show (cond (bodd n) 1 0 : β„€) = (cond (bodd n) 1 0 : β„•), by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2 | -[1+ n] := begin refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2), dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul], { change -[1+ 2 * nat.div2 n] = _, rw zero_add }, { rw [zero_add, add_comm], refl } end theorem div2_val : βˆ€ n, div2 n = n / 2 | (n : β„•) := congr_arg of_nat n.div2_val | -[1+ n] := congr_arg neg_succ_of_nat n.div2_val lemma bit0_val (n : β„€) : bit0 n = 2 * n := (two_mul _).symm lemma bit1_val (n : β„€) : bit1 n = 2 * n + 1 := congr_arg (+(1:β„€)) (bit0_val _) lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val } lemma bit_decomp (n : β„€) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _ def {u} bit_cases_on {C : β„€ β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := by rw [← bit_decomp n]; apply h @[simp] lemma bit_zero : bit ff 0 = 0 := rfl @[simp] lemma bit_coe_nat (b) (n : β„•) : bit b n = nat.bit b n := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bit_neg_succ (b) (n : β„•) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bodd_bit (b n) : bodd (bit b n) = b := by rw bit_val; simp; cases b; cases bodd n; refl @[simp] lemma div2_bit (b n) : div2 (bit b n) = n := begin rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:β„€) = 0), zero_add], cases b, all_goals {exact dec_trivial} end @[simp] lemma test_bit_zero (b) : βˆ€ n, test_bit (bit b n) 0 = b | (n : β„•) := by rw [bit_coe_nat]; apply nat.test_bit_zero | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero]; clear test_bit_zero; cases b; refl @[simp] lemma test_bit_succ (m b) : βˆ€ n, test_bit (bit b n) (nat.succ m) = test_bit n m | (n : β„•) := by rw [bit_coe_nat]; apply nat.test_bit_succ | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ] private meta def bitwise_tac : tactic unit := `[ funext m, funext n, cases m with m m; cases n with n n; try {refl}, all_goals { apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat, try {dsimp [nat.land, nat.ldiff, nat.lor]}, try {rw [ show nat.bitwise (Ξ» a b, a && bnot b) n m = nat.bitwise (Ξ» a b, b && bnot a) m n, from congr_fun (congr_fun (@nat.bitwise_swap (Ξ» a b, b && bnot a) rfl) n) m]}, apply congr_arg (Ξ» f, nat.bitwise f m n), funext a, funext b, cases a; cases b; refl }, all_goals {unfold nat.land nat.ldiff nat.lor} ] theorem bitwise_or : bitwise bor = lor := by bitwise_tac theorem bitwise_and : bitwise band = land := by bitwise_tac theorem bitwise_diff : bitwise (Ξ» a b, a && bnot b) = ldiff := by bitwise_tac theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac @[simp] lemma bitwise_bit (f : bool β†’ bool β†’ bool) (a m b n) : bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) := begin cases m with m m; cases n with n n; repeat { rw [← int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ }; unfold bitwise nat_bitwise bnot; [ induction h : f ff ff, induction h : f ff tt, induction h : f tt ff, induction h : f tt tt ], all_goals { unfold cond, rw nat.bitwise_bit, repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } }, all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl } end @[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) := by rw [← bitwise_or, bitwise_bit] @[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) := by rw [← bitwise_and, bitwise_bit] @[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) := by rw [← bitwise_diff, bitwise_bit] @[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) := by rw [← bitwise_xor, bitwise_bit] @[simp] lemma lnot_bit (b) : βˆ€ n, lnot (bit b n) = bit (bnot b) (lnot n) | (n : β„•) := by simp [lnot] | -[1+ n] := by simp [lnot] @[simp] lemma test_bit_bitwise (f : bool β†’ bool β†’ bool) (m n k) : test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) := begin induction k with k IH generalizing m n; apply bit_cases_on m; intros a m'; apply bit_cases_on n; intros b n'; rw bitwise_bit, { simp [test_bit_zero] }, { simp [test_bit_succ, IH] } end @[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k := by rw [← bitwise_or, test_bit_bitwise] @[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k := by rw [← bitwise_and, test_bit_bitwise] @[simp] lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) := by rw [← bitwise_diff, test_bit_bitwise] @[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) := by rw [← bitwise_xor, test_bit_bitwise] @[simp] lemma test_bit_lnot : βˆ€ n k, test_bit (lnot n) k = bnot (test_bit n k) | (n : β„•) k := by simp [lnot, test_bit] | -[1+ n] k := by simp [lnot, test_bit] lemma shiftl_add : βˆ€ (m : β„€) (n : β„•) (k : β„€), shiftl m (n + k) = shiftl (shiftl m n) k | (m : β„•) n (k:β„•) := congr_arg of_nat (nat.shiftl_add _ _ _) | -[1+ m] n (k:β„•) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _) | (m : β„•) n -[1+k] := sub_nat_nat_elim n k.succ (Ξ» n k i, shiftl ↑m i = nat.shiftr (nat.shiftl m n) k) (Ξ» i n, congr_arg coe $ by rw [← nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (Ξ» i n, congr_arg coe $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, nat.sub_self]; refl) | -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ (Ξ» n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k]) (Ξ» i n, congr_arg neg_succ_of_nat $ by rw [← nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (Ξ» i n, congr_arg neg_succ_of_nat $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl'_sub, nat.sub_self]; refl) lemma shiftl_sub (m : β„€) (n : β„•) (k : β„€) : shiftl m (n - k) = shiftr (shiftl m n) k := shiftl_add _ _ _ @[simp] lemma shiftl_neg (m n : β„€) : shiftl m (-n) = shiftr m n := rfl @[simp] lemma shiftr_neg (m n : β„€) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg] @[simp] lemma shiftl_coe_nat (m n : β„•) : shiftl m n = nat.shiftl m n := rfl @[simp] lemma shiftr_coe_nat (m n : β„•) : shiftr m n = nat.shiftr m n := by cases n; refl @[simp] lemma shiftl_neg_succ (m n : β„•) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl @[simp] lemma shiftr_neg_succ (m n : β„•) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl lemma shiftr_add : βˆ€ (m : β„€) (n k : β„•), shiftr m (n + k) = shiftr (shiftr m n) k | (m : β„•) n k := by rw [shiftr_coe_nat, shiftr_coe_nat, ← int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add] | -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ, ← int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add] lemma shiftl_eq_mul_pow : βˆ€ (m : β„€) (n : β„•), shiftl m n = m * ↑(2 ^ n) | (m : β„•) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _) | -[1+ m] n := @congr_arg β„• β„€ _ _ (Ξ»i, -i) (nat.shiftl'_tt_eq_mul_pow _ _) lemma shiftr_eq_div_pow : βˆ€ (m : β„€) (n : β„•), shiftr m n = m / ↑(2 ^ n) | (m : β„•) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _) | -[1+ m] n := begin rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl, exact coe_nat_lt_coe_nat_of_lt (nat.pos_pow_of_pos _ dec_trivial) end lemma one_shiftl (n : β„•) : shiftl 1 n = (2 ^ n : β„•) := congr_arg coe (nat.one_shiftl _) @[simp] lemma zero_shiftl : βˆ€ n : β„€, shiftl 0 n = 0 | (n : β„•) := congr_arg coe (nat.zero_shiftl _) | -[1+ n] := congr_arg coe (nat.zero_shiftr _) @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _ /- Least upper bound property for integers -/ theorem exists_least_of_bdd {P : β„€ β†’ Prop} [HP : decidable_pred P] (Hbdd : βˆƒ b : β„€, βˆ€ z : β„€, P z β†’ b ≀ z) (Hinh : βˆƒ z : β„€, P z) : βˆƒ lb : β„€, P lb ∧ (βˆ€ z : β„€, P z β†’ lb ≀ z) := let ⟨b, Hb⟩ := Hbdd in have EX : βˆƒ n : β„•, P (b + n), from let ⟨elt, Helt⟩ := Hinh in match elt, le.dest (Hb _ Helt), Helt with | ._, ⟨n, rfl⟩, Hn := ⟨n, Hn⟩ end, ⟨b + (nat.find EX : β„€), nat.find_spec EX, Ξ» z h, match z, le.dest (Hb _ h), h with | ._, ⟨n, rfl⟩, h := add_le_add_left (int.coe_nat_le.2 $ nat.find_min' _ h) _ end⟩ theorem exists_greatest_of_bdd {P : β„€ β†’ Prop} [HP : decidable_pred P] (Hbdd : βˆƒ b : β„€, βˆ€ z : β„€, P z β†’ z ≀ b) (Hinh : βˆƒ z : β„€, P z) : βˆƒ ub : β„€, P ub ∧ (βˆ€ z : β„€, P z β†’ z ≀ ub) := have Hbdd' : βˆƒ (b : β„€), βˆ€ (z : β„€), P (-z) β†’ b ≀ z, from let ⟨b, Hb⟩ := Hbdd in ⟨-b, Ξ» z h, neg_le.1 (Hb _ h)⟩, have Hinh' : βˆƒ z : β„€, P (-z), from let ⟨elt, Helt⟩ := Hinh in ⟨-elt, by rw [neg_neg]; exact Helt⟩, let ⟨lb, Plb, al⟩ := exists_least_of_bdd Hbdd' Hinh' in ⟨-lb, Plb, Ξ» z h, le_neg.1 $ al _ $ by rwa neg_neg⟩ /- cast (injection into groups with one) -/ @[simp] theorem nat_cast_eq_coe_nat : βˆ€ n, @coe β„• β„€ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n = @coe β„• β„€ (@coe_to_lift _ _ (@coe_base _ _ int.has_coe)) n | 0 := rfl | (n+1) := congr_arg (+(1:β„€)) (nat_cast_eq_coe_nat n) section cast variables {Ξ± : Type*} section variables [has_zero Ξ±] [has_one Ξ±] [has_add Ξ±] [has_neg Ξ±] /-- Canonical homomorphism from the integers to any ring(-like) structure `Ξ±` -/ protected def cast : β„€ β†’ Ξ± | (n : β„•) := n | -[1+ n] := -(n+1) @[priority 0] instance cast_coe : has_coe β„€ Ξ± := ⟨int.cast⟩ @[simp] theorem cast_zero : ((0 : β„€) : Ξ±) = 0 := rfl @[simp] theorem cast_of_nat (n : β„•) : (of_nat n : Ξ±) = n := rfl @[simp] theorem cast_coe_nat (n : β„•) : ((n : β„€) : Ξ±) = n := rfl @[simp] theorem cast_coe_nat' (n : β„•) : (@coe β„• β„€ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n : Ξ±) = n := by simp @[simp] theorem cast_neg_succ_of_nat (n : β„•) : (-[1+ n] : Ξ±) = -(n + 1) := rfl end @[simp] theorem cast_one [add_monoid Ξ±] [has_one Ξ±] [has_neg Ξ±] : ((1 : β„€) : Ξ±) = 1 := nat.cast_one @[simp] theorem cast_sub_nat_nat [add_group Ξ±] [has_one Ξ±] (m n) : ((int.sub_nat_nat m n : β„€) : Ξ±) = m - n := begin unfold sub_nat_nat, cases e : n - m, { simp [sub_nat_nat, e, nat.le_of_sub_eq_zero e] }, { rw [sub_nat_nat, cast_neg_succ_of_nat, ← nat.cast_succ, ← e, nat.cast_sub $ _root_.le_of_lt $ nat.lt_of_sub_eq_succ e, neg_sub] }, end @[simp] theorem cast_neg_of_nat [add_group Ξ±] [has_one Ξ±] : βˆ€ n, ((neg_of_nat n : β„€) : Ξ±) = -n | 0 := neg_zero.symm | (n+1) := rfl @[simp] theorem cast_add [add_group Ξ±] [has_one Ξ±] : βˆ€ m n, ((m + n : β„€) : Ξ±) = m + n | (m : β„•) (n : β„•) := nat.cast_add _ _ | (m : β„•) -[1+ n] := cast_sub_nat_nat _ _ | -[1+ m] (n : β„•) := (cast_sub_nat_nat _ _).trans $ sub_eq_of_eq_add $ show (n:Ξ±) = -(m+1) + n + (m+1), by rw [add_assoc, ← cast_succ, ← nat.cast_add, add_comm, nat.cast_add, cast_succ, neg_add_cancel_left] | -[1+ m] -[1+ n] := show -((m + n + 1 + 1 : β„•) : Ξ±) = -(m + 1) + -(n + 1), by rw [← neg_add_rev, ← nat.cast_add_one, ← nat.cast_add_one, ← nat.cast_add]; apply congr_arg (Ξ» x:β„•, -(x:Ξ±)); simp @[simp] theorem cast_neg [add_group Ξ±] [has_one Ξ±] : βˆ€ n, ((-n : β„€) : Ξ±) = -n | (n : β„•) := cast_neg_of_nat _ | -[1+ n] := (neg_neg _).symm theorem cast_sub [add_group Ξ±] [has_one Ξ±] (m n) : ((m - n : β„€) : Ξ±) = m - n := by simp @[simp] theorem cast_eq_zero [add_group Ξ±] [has_one Ξ±] [char_zero Ξ±] {n : β„€} : (n : Ξ±) = 0 ↔ n = 0 := ⟨λ h, begin cases n, { exact congr_arg coe (nat.cast_eq_zero.1 h) }, { rw [cast_neg_succ_of_nat, neg_eq_zero, ← cast_succ, nat.cast_eq_zero] at h, contradiction } end, Ξ» h, by rw [h, cast_zero]⟩ @[simp] theorem cast_inj [add_group Ξ±] [has_one Ξ±] [char_zero Ξ±] {m n : β„€} : (m : Ξ±) = n ↔ m = n := by rw [← sub_eq_zero, ← cast_sub, cast_eq_zero, sub_eq_zero] theorem cast_injective [add_group Ξ±] [has_one Ξ±] [char_zero Ξ±] : function.injective (coe : β„€ β†’ Ξ±) | m n := cast_inj.1 @[simp] theorem cast_ne_zero [add_group Ξ±] [has_one Ξ±] [char_zero Ξ±] {n : β„€} : (n : Ξ±) β‰  0 ↔ n β‰  0 := not_congr cast_eq_zero @[simp] theorem cast_mul [ring Ξ±] : βˆ€ m n, ((m * n : β„€) : Ξ±) = m * n | (m : β„•) (n : β„•) := nat.cast_mul _ _ | (m : β„•) -[1+ n] := (cast_neg_of_nat _).trans $ show (-(m * (n + 1) : β„•) : Ξ±) = m * -(n + 1), by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_mul_neg] | -[1+ m] (n : β„•) := (cast_neg_of_nat _).trans $ show (-((m + 1) * n : β„•) : Ξ±) = -(m + 1) * n, by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_neg_mul] | -[1+ m] -[1+ n] := show (((m + 1) * (n + 1) : β„•) : Ξ±) = -(m + 1) * -(n + 1), by rw [nat.cast_mul, nat.cast_add_one, nat.cast_add_one, neg_mul_neg] theorem mul_cast_comm [ring Ξ±] (a : Ξ±) (n : β„€) : a * n = n * a := by cases n; simp [nat.mul_cast_comm, left_distrib, right_distrib, *] @[simp] theorem cast_bit0 [ring Ξ±] (n : β„€) : ((bit0 n : β„€) : Ξ±) = bit0 n := cast_add _ _ @[simp] theorem cast_bit1 [ring Ξ±] (n : β„€) : ((bit1 n : β„€) : Ξ±) = bit1 n := by rw [bit1, cast_add, cast_one, cast_bit0]; refl theorem cast_nonneg [linear_ordered_ring Ξ±] : βˆ€ {n : β„€}, (0 : Ξ±) ≀ n ↔ 0 ≀ n | (n : β„•) := by simp | -[1+ n] := by simpa [not_le_of_gt (neg_succ_lt_zero n)] using show -(n:Ξ±) < 1, from lt_of_le_of_lt (by simp) zero_lt_one @[simp] theorem cast_le [linear_ordered_ring Ξ±] {m n : β„€} : (m : Ξ±) ≀ n ↔ m ≀ n := by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg] @[simp] theorem cast_lt [linear_ordered_ring Ξ±] {m n : β„€} : (m : Ξ±) < n ↔ m < n := by simpa [-cast_le] using not_congr (@cast_le Ξ± _ n m) @[simp] theorem cast_nonpos [linear_ordered_ring Ξ±] {n : β„€} : (n : Ξ±) ≀ 0 ↔ n ≀ 0 := by rw [← cast_zero, cast_le] @[simp] theorem cast_pos [linear_ordered_ring Ξ±] {n : β„€} : (0 : Ξ±) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] @[simp] theorem cast_lt_zero [linear_ordered_ring Ξ±] {n : β„€} : (n : Ξ±) < 0 ↔ n < 0 := by rw [← cast_zero, cast_lt] theorem eq_cast [add_group Ξ±] [has_one Ξ±] (f : β„€ β†’ Ξ±) (H1 : f 1 = 1) (Hadd : βˆ€ x y, f (x + y) = f x + f y) (n : β„€) : f n = n := begin have H : βˆ€ (n : β„•), f n = n := nat.eq_cast' (Ξ» n, f n) H1 (Ξ» x y, Hadd x y), cases n, {apply H}, apply eq_neg_of_add_eq_zero, rw [← nat.cast_zero, ← H 0, int.coe_nat_zero, ← show -[1+ n] + (↑n + 1) = 0, from neg_add_self (↑n+1), Hadd, show f (n+1) = n+1, from H (n+1)] end @[simp] theorem cast_id (n : β„€) : ↑n = n := (eq_cast id rfl (Ξ» _ _, rfl) n).symm @[simp] theorem cast_min [decidable_linear_ordered_comm_ring Ξ±] {a b : β„€} : (↑(min a b) : Ξ±) = min a b := by by_cases a ≀ b; simp [h, min] @[simp] theorem cast_max [decidable_linear_ordered_comm_ring Ξ±] {a b : β„€} : (↑(max a b) : Ξ±) = max a b := by by_cases a ≀ b; simp [h, max] @[simp] theorem cast_abs [decidable_linear_ordered_comm_ring Ξ±] {q : β„€} : ((abs q : β„€) : Ξ±) = abs q := by simp [abs] end cast end int
6904551ab43ae4e53f1fb380c8a3a506c80fb42f
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/set.lean
dbbb98d920c81bf48118c884f9b0e66bb788c08c
[ "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
125
lean
import standard using bool definition set {{T : Type}} := T β†’ bool infix `∈` := Ξ»x A, A x = tt check 1 ∈ (Ξ» x, tt)
6e6b9ab2c4f719a573d37a252c847d32316e9bcd
367134ba5a65885e863bdc4507601606690974c1
/src/control/equiv_functor.lean
1d781d112f6a6f0d9fae2a80a46128b782203b3a
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
3,310
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Scott Morrison -/ import category_theory.category import data.equiv.functor /-! # Functions functorial with respect to equivalences An `equiv_functor` is a function from `Type β†’ Type` equipped with the additional data of coherently mapping equivalences to equivalences. In categorical language, it is an endofunctor of the "core" of the category `Type`. -/ universes uβ‚€ u₁ uβ‚‚ vβ‚€ v₁ vβ‚‚ open function /-- An `equiv_functor` is only functorial with respect to equivalences. To construct an `equiv_functor`, it suffices to supply just the function `f Ξ± β†’ f Ξ²` from an equivalence `Ξ± ≃ Ξ²`, and then prove the functor laws. It's then a consequence that this function is part of an equivalence, provided by `equiv_functor.map_equiv`. -/ class equiv_functor (f : Type uβ‚€ β†’ Type u₁) := (map : Ξ  {Ξ± Ξ²}, (Ξ± ≃ Ξ²) β†’ (f Ξ± β†’ f Ξ²)) (map_refl' : Ξ  Ξ±, map (equiv.refl Ξ±) = @id (f Ξ±) . obviously) (map_trans' : Ξ  {Ξ± Ξ² Ξ³} (k : Ξ± ≃ Ξ²) (h : Ξ² ≃ Ξ³), map (k.trans h) = (map h) ∘ (map k) . obviously) restate_axiom equiv_functor.map_refl' restate_axiom equiv_functor.map_trans' attribute [simp] equiv_functor.map_refl namespace equiv_functor section variables (f : Type uβ‚€ β†’ Type u₁) [equiv_functor f] {Ξ± Ξ² : Type uβ‚€} (e : Ξ± ≃ Ξ²) /-- An `equiv_functor` in fact takes every equiv to an equiv. -/ def map_equiv : f Ξ± ≃ f Ξ² := { to_fun := equiv_functor.map e, inv_fun := equiv_functor.map e.symm, left_inv := Ξ» x, by { convert (congr_fun (equiv_functor.map_trans e e.symm) x).symm, simp, }, right_inv := Ξ» y, by { convert (congr_fun (equiv_functor.map_trans e.symm e) y).symm, simp, }, } @[simp] lemma map_equiv_apply (x : f Ξ±) : map_equiv f e x = equiv_functor.map e x := rfl lemma map_equiv_symm_apply (y : f Ξ²) : (map_equiv f e).symm y = equiv_functor.map e.symm y := rfl @[simp] lemma map_equiv_refl (Ξ±) : map_equiv f (equiv.refl Ξ±) = equiv.refl (f Ξ±) := by simpa [equiv_functor.map_equiv] @[simp] lemma map_equiv_symm : (map_equiv f e).symm = map_equiv f e.symm := equiv.ext $ map_equiv_symm_apply f e /-- The composition of `map_equiv`s is carried over the `equiv_functor`. For plain `functor`s, this lemma is named `map_map` when applied or `map_comp_map` when not applied. -/ @[simp] lemma map_equiv_trans {Ξ³ : Type uβ‚€} (ab : Ξ± ≃ Ξ²) (bc : Ξ² ≃ Ξ³) : (map_equiv f ab).trans (map_equiv f bc) = map_equiv f (ab.trans bc) := equiv.ext $ Ξ» x, by simp [map_equiv, map_trans'] end @[priority 100] instance of_is_lawful_functor (f : Type uβ‚€ β†’ Type u₁) [functor f] [is_lawful_functor f] : equiv_functor f := { map := Ξ» Ξ± Ξ² e, functor.map e, map_refl' := Ξ» Ξ±, by { ext, apply is_lawful_functor.id_map, }, map_trans' := Ξ» Ξ± Ξ² Ξ³ k h, by { ext x, apply (is_lawful_functor.comp_map k h x), } } lemma map_equiv.injective (f : Type uβ‚€ β†’ Type u₁) [applicative f] [is_lawful_applicative f] {Ξ± Ξ² : Type uβ‚€} (h : βˆ€ Ξ³, function.injective (pure : Ξ³ β†’ f Ξ³)) : function.injective (@equiv_functor.map_equiv f _ Ξ± Ξ²) := Ξ» e₁ eβ‚‚ H, equiv.ext $ Ξ» x, h Ξ² (by simpa [equiv_functor.map] using equiv.congr_fun H (pure x)) end equiv_functor
6f778c37b7d7acc26c3623e95d6bdd5e5d483515
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/nat/totient.lean
faaf415fcbb7fef58114a701ce875b7270f02b03
[ "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
14,472
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 algebra.big_operators.basic import data.nat.prime import data.zmod.basic import ring_theory.multiplicity import data.nat.periodic import algebra.char_p.two import number_theory.divisors /-! # Euler's totient function This file defines [Euler's totient function](https://en.wikipedia.org/wiki/Euler's_totient_function) `nat.totient n` which counts the number of naturals less than `n` that are coprime with `n`. We prove the divisor sum formula, namely that `n` equals `Ο†` summed over the divisors of `n`. See `sum_totient`. We also prove two lemmas to help compute totients, namely `totient_mul` and `totient_prime_pow`. -/ open finset open_locale big_operators namespace nat /-- Euler's totient function. This counts the number of naturals strictly less than `n` which are coprime with `n`. -/ def totient (n : β„•) : β„• := ((range n).filter n.coprime).card localized "notation `Ο†` := nat.totient" in nat @[simp] theorem totient_zero : Ο† 0 = 0 := rfl @[simp] theorem totient_one : Ο† 1 = 1 := by simp [totient] lemma totient_eq_card_coprime (n : β„•) : Ο† n = ((range n).filter n.coprime).card := rfl lemma totient_le (n : β„•) : Ο† n ≀ n := ((range n).card_filter_le _).trans_eq (card_range n) lemma totient_lt (n : β„•) (hn : 1 < n) : Ο† n < n := (card_lt_card (filter_ssubset.2 ⟨0, by simp [hn.ne', pos_of_gt hn]⟩)).trans_eq (card_range n) lemma totient_pos : βˆ€ {n : β„•}, 0 < n β†’ 0 < Ο† n | 0 := dec_trivial | 1 := by simp [totient] | (n+2) := Ξ» h, card_pos.2 ⟨1, mem_filter.2 ⟨mem_range.2 dec_trivial, coprime_one_right _⟩⟩ lemma filter_coprime_Ico_eq_totient (a n : β„•) : ((Ico n (n+a)).filter (coprime a)).card = totient a := begin rw [totient, filter_Ico_card_eq_of_periodic, count_eq_card_filter_range], exact periodic_coprime a, end lemma Ico_filter_coprime_le {a : β„•} (k n : β„•) (a_pos : 0 < a) : ((Ico k (k + n)).filter (coprime a)).card ≀ totient a * (n / a + 1) := begin conv_lhs { rw ←nat.mod_add_div n a }, induction n / a with i ih, { rw ←filter_coprime_Ico_eq_totient a k, simp only [add_zero, mul_one, mul_zero, le_of_lt (mod_lt n a_pos)], mono, refine monotone_filter_left a.coprime _, simp only [finset.le_eq_subset], exact Ico_subset_Ico rfl.le (add_le_add_left (le_of_lt (mod_lt n a_pos)) k), }, simp only [mul_succ], simp_rw ←add_assoc at ih ⊒, calc (filter a.coprime (Ico k (k + n % a + a * i + a))).card = (filter a.coprime (Ico k (k + n % a + a * i) βˆͺ Ico (k + n % a + a * i) (k + n % a + a * i + a))).card : begin congr, rw Ico_union_Ico_eq_Ico, rw add_assoc, exact le_self_add, exact le_self_add, end ... ≀ (filter a.coprime (Ico k (k + n % a + a * i))).card + a.totient : begin rw [filter_union, ←filter_coprime_Ico_eq_totient a (k + n % a + a * i)], apply card_union_le, end ... ≀ a.totient * i + a.totient + a.totient : add_le_add_right ih (totient a), end open zmod /-- Note this takes an explicit `fintype ((zmod n)Λ£)` argument to avoid trouble with instance diamonds. -/ @[simp] lemma _root_.zmod.card_units_eq_totient (n : β„•) [h : fact (0 < n)] [fintype ((zmod n)Λ£)] : fintype.card ((zmod n)Λ£) = Ο† n := calc fintype.card ((zmod n)Λ£) = fintype.card {x : zmod n // x.val.coprime n} : fintype.card_congr zmod.units_equiv_coprime ... = Ο† n : begin unfreezingI { obtain ⟨m, rfl⟩ : βˆƒ m, n = m + 1 := exists_eq_succ_of_ne_zero h.out.ne' }, simp only [totient, finset.card_eq_sum_ones, fintype.card_subtype, finset.sum_filter, ← fin.sum_univ_eq_sum_range, @nat.coprime_comm (m + 1)], refl end lemma totient_even {n : β„•} (hn : 2 < n) : even n.totient := begin haveI : fact (1 < n) := ⟨one_lt_two.trans hn⟩, suffices : 2 = order_of (-1 : (zmod n)Λ£), { rw [← zmod.card_units_eq_totient, even_iff_two_dvd, this], exact order_of_dvd_card_univ }, rw [←order_of_units, units.coe_neg_one, order_of_neg_one, ring_char.eq (zmod n) n, if_neg hn.ne'], end lemma totient_mul {m n : β„•} (h : m.coprime n) : Ο† (m * n) = Ο† m * Ο† n := if hmn0 : m * n = 0 then by cases nat.mul_eq_zero.1 hmn0 with h h; simp only [totient_zero, mul_zero, zero_mul, h] else begin haveI : fact (0 < (m * n)) := ⟨nat.pos_of_ne_zero hmn0⟩, haveI : fact (0 < m) := ⟨nat.pos_of_ne_zero $ left_ne_zero_of_mul hmn0⟩, haveI : fact (0 < n) := ⟨nat.pos_of_ne_zero $ right_ne_zero_of_mul hmn0⟩, simp only [← zmod.card_units_eq_totient], rw [fintype.card_congr (units.map_equiv (zmod.chinese_remainder h).to_mul_equiv).to_equiv, fintype.card_congr (@mul_equiv.prod_units (zmod m) (zmod n) _ _).to_equiv, fintype.card_prod] end /-- For `d ∣ n`, the totient of `n/d` equals the number of values `k < n` such that `gcd n k = d` -/ lemma totient_div_of_dvd {n d : β„•} (hnd : d ∣ n) : Ο† (n/d) = (filter (Ξ» (k : β„•), n.gcd k = d) (range n)).card := begin rcases d.eq_zero_or_pos with rfl | hd0, { simp [eq_zero_of_zero_dvd hnd] }, rcases hnd with ⟨x, rfl⟩, rw nat.mul_div_cancel_left x hd0, apply finset.card_congr (Ξ» k _, d * k), { simp only [mem_filter, mem_range, and_imp, coprime], refine Ξ» a ha1 ha2, ⟨(mul_lt_mul_left hd0).2 ha1, _⟩, rw [gcd_mul_left, ha2, mul_one] }, { simp [hd0.ne'] }, { simp only [mem_filter, mem_range, exists_prop, and_imp], refine Ξ» b hb1 hb2, _, have : d ∣ b, { rw ←hb2, apply gcd_dvd_right }, rcases this with ⟨q, rfl⟩, refine ⟨q, ⟨⟨(mul_lt_mul_left hd0).1 hb1, _⟩, rfl⟩⟩, rwa [gcd_mul_left, mul_right_eq_self_iff hd0] at hb2 }, end lemma sum_totient (n : β„•) : n.divisors.sum Ο† = n := begin rcases n.eq_zero_or_pos with rfl | hn, { simp }, rw ←sum_div_divisors n Ο†, have : n = βˆ‘ (d : β„•) in n.divisors, (filter (Ξ» (k : β„•), n.gcd k = d) (range n)).card, { nth_rewrite_lhs 0 ←card_range n, refine card_eq_sum_card_fiberwise (Ξ» x hx, mem_divisors.2 ⟨_, hn.ne'⟩), apply gcd_dvd_left }, nth_rewrite_rhs 0 this, exact sum_congr rfl (Ξ» x hx, totient_div_of_dvd (dvd_of_mem_divisors hx)), end lemma sum_totient' (n : β„•) : βˆ‘ m in (range n.succ).filter (∣ n), Ο† m = n := begin convert sum_totient _ using 1, simp only [nat.divisors, sum_filter, range_eq_Ico], rw sum_eq_sum_Ico_succ_bot; simp end /-- When `p` is prime, then the totient of `p ^ (n + 1)` is `p ^ n * (p - 1)` -/ lemma totient_prime_pow_succ {p : β„•} (hp : p.prime) (n : β„•) : Ο† (p ^ (n + 1)) = p ^ n * (p - 1) := calc Ο† (p ^ (n + 1)) = ((range (p ^ (n + 1))).filter (coprime (p ^ (n + 1)))).card : totient_eq_card_coprime _ ... = (range (p ^ (n + 1)) \ ((range (p ^ n)).image (* p))).card : congr_arg card begin rw [sdiff_eq_filter], apply filter_congr, simp only [mem_range, mem_filter, coprime_pow_left_iff n.succ_pos, mem_image, not_exists, hp.coprime_iff_not_dvd], intros a ha, split, { rintros hap b _ rfl, exact hap (dvd_mul_left _ _) }, { rintros h ⟨b, rfl⟩, rw [pow_succ] at ha, exact h b (lt_of_mul_lt_mul_left ha (zero_le _)) (mul_comm _ _) } end ... = _ : have h1 : set.inj_on (* p) (range (p ^ n)), from Ξ» x _ y _, (nat.mul_left_inj hp.pos).1, have h2 : (range (p ^ n)).image (* p) βŠ† range (p ^ (n + 1)), from Ξ» a, begin simp only [mem_image, mem_range, exists_imp_distrib], rintros b h rfl, rw [pow_succ'], exact (mul_lt_mul_right hp.pos).2 h end, begin rw [card_sdiff h2, card_image_of_inj_on h1, card_range, card_range, ← one_mul (p ^ n), pow_succ, ← tsub_mul, one_mul, mul_comm] end /-- When `p` is prime, then the totient of `p ^ n` is `p ^ (n - 1) * (p - 1)` -/ lemma totient_prime_pow {p : β„•} (hp : p.prime) {n : β„•} (hn : 0 < n) : Ο† (p ^ n) = p ^ (n - 1) * (p - 1) := by rcases exists_eq_succ_of_ne_zero (pos_iff_ne_zero.1 hn) with ⟨m, rfl⟩; exact totient_prime_pow_succ hp _ lemma totient_prime {p : β„•} (hp : p.prime) : Ο† p = p - 1 := by rw [← pow_one p, totient_prime_pow hp]; simp lemma totient_mul_of_prime_of_dvd {p n : β„•} (hp : p.prime) (h : p ∣ n) : (p * n).totient = p * n.totient := begin by_cases hzero : n = 0, { simp [hzero] }, { have hfin := (multiplicity.finite_nat_iff.2 ⟨hp.ne_one, zero_lt_iff.2 hzero⟩), have h0 : 0 < (multiplicity p n).get hfin := multiplicity.pos_of_dvd hfin h, obtain ⟨m, hm, hndiv⟩ := multiplicity.exists_eq_pow_mul_and_not_dvd hfin, rw [hm, ← mul_assoc, ← pow_succ, nat.totient_mul (coprime_comm.mp (hp.coprime_pow_of_not_dvd hndiv)), nat.totient_mul (coprime_comm.mp (hp.coprime_pow_of_not_dvd hndiv)), ← mul_assoc], congr, rw [ ← succ_pred_eq_of_pos h0, totient_prime_pow_succ hp, totient_prime_pow_succ hp, succ_pred_eq_of_pos h0, ← mul_assoc p, ← pow_succ, ← succ_pred_eq_of_pos h0, nat.pred_succ] } end lemma totient_eq_iff_prime {p : β„•} (hp : 0 < p) : p.totient = p - 1 ↔ p.prime := begin refine ⟨λ h, _, totient_prime⟩, replace hp : 1 < p, { apply lt_of_le_of_ne, { rwa succ_le_iff }, { rintro rfl, rw [totient_one, tsub_self] at h, exact one_ne_zero h } }, rw [totient_eq_card_coprime, range_eq_Ico, ←Ico_insert_succ_left hp.le, finset.filter_insert, if_neg (not_coprime_of_dvd_of_dvd hp (dvd_refl p) (dvd_zero p)), ←nat.card_Ico 1 p] at h, refine p.prime_of_coprime hp (Ξ» n hn hnz, finset.filter_card_eq h n $ finset.mem_Ico.mpr ⟨_, hn⟩), rwa [succ_le_iff, pos_iff_ne_zero], end lemma card_units_zmod_lt_sub_one {p : β„•} (hp : 1 < p) [fintype ((zmod p)Λ£)] : fintype.card ((zmod p)Λ£) ≀ p - 1 := begin haveI : fact (0 < p) := ⟨zero_lt_one.trans hp⟩, rw zmod.card_units_eq_totient p, exact nat.le_pred_of_lt (nat.totient_lt p hp), end lemma prime_iff_card_units (p : β„•) [fintype ((zmod p)Λ£)] : p.prime ↔ fintype.card ((zmod p)Λ£) = p - 1 := begin by_cases hp : p = 0, { substI hp, simp only [zmod, not_prime_zero, false_iff, zero_tsub], -- the substI created an non-defeq but subsingleton instance diamond; resolve it suffices : fintype.card β„€Λ£ β‰  0, { convert this }, simp }, haveI : fact (0 < p) := ⟨nat.pos_of_ne_zero hp⟩, rw [zmod.card_units_eq_totient, nat.totient_eq_iff_prime (fact.out (0 < p))], end @[simp] lemma totient_two : Ο† 2 = 1 := (totient_prime prime_two).trans rfl lemma totient_eq_one_iff : βˆ€ {n : β„•}, n.totient = 1 ↔ n = 1 ∨ n = 2 | 0 := by simp | 1 := by simp | 2 := by simp | (n+3) := begin have : 3 ≀ n + 3 := le_add_self, simp only [succ_succ_ne_one, false_or], exact ⟨λ h, not_even_one.elim $ h β–Έ totient_even this, by rintro ⟨⟩⟩, end /-! ### Euler's product formula for the totient function We prove several different statements of this formula. -/ /-- Euler's product formula for the totient function. -/ theorem totient_eq_prod_factorization {n : β„•} (hn : n β‰  0) : Ο† n = n.factorization.prod (Ξ» p k, p ^ (k - 1) * (p - 1)) := begin rw multiplicative_factorization Ο† @totient_mul totient_one hn, apply finsupp.prod_congr (Ξ» p hp, _), have h := zero_lt_iff.mpr (finsupp.mem_support_iff.mp hp), rw [totient_prime_pow (prime_of_mem_factorization hp) h], end /-- Euler's product formula for the totient function. -/ theorem totient_mul_prod_factors (n : β„•) : Ο† n * ∏ p in n.factors.to_finset, p = n * ∏ p in n.factors.to_finset, (p - 1) := begin by_cases hn : n = 0, { simp [hn] }, rw totient_eq_prod_factorization hn, nth_rewrite 2 ←factorization_prod_pow_eq_self hn, simp only [←prod_factorization_eq_prod_factors, ←finsupp.prod_mul], refine finsupp.prod_congr (Ξ» p hp, _), rw [finsupp.mem_support_iff, ← zero_lt_iff] at hp, rw [mul_comm, ←mul_assoc, ←pow_succ, nat.sub_add_cancel hp], end /-- Euler's product formula for the totient function. -/ theorem totient_eq_div_factors_mul (n : β„•) : Ο† n = n / (∏ p in n.factors.to_finset, p) * (∏ p in n.factors.to_finset, (p - 1)) := begin rw [← mul_div_left n.totient, totient_mul_prod_factors, mul_comm, nat.mul_div_assoc _ (prod_prime_factors_dvd n), mul_comm], simpa [prod_factorization_eq_prod_factors] using prod_pos (Ξ» p, pos_of_mem_factorization), end /-- Euler's product formula for the totient function. -/ theorem totient_eq_mul_prod_factors (n : β„•) : (Ο† n : β„š) = n * ∏ p in n.factors.to_finset, (1 - p⁻¹) := begin by_cases hn : n = 0, { simp [hn] }, have hn' : (n : β„š) β‰  0, { simp [hn] }, have hpQ : ∏ p in n.factors.to_finset, (p : β„š) β‰  0, { rw [←cast_prod, cast_ne_zero, ←zero_lt_iff, ←prod_factorization_eq_prod_factors], exact prod_pos (Ξ» p hp, pos_of_mem_factorization hp) }, simp only [totient_eq_div_factors_mul n, prod_prime_factors_dvd n, cast_mul, cast_prod, cast_div_char_zero, mul_comm_div, mul_right_inj' hn', div_eq_iff hpQ, ←prod_mul_distrib], refine prod_congr rfl (Ξ» p hp, _), have hp := pos_of_mem_factors (list.mem_to_finset.mp hp), have hp' : (p : β„š) β‰  0 := cast_ne_zero.mpr hp.ne.symm, rw [sub_mul, one_mul, mul_comm, mul_inv_cancel hp', cast_pred hp], end lemma totient_gcd_mul_totient_mul (a b : β„•) : Ο† (a.gcd b) * Ο† (a * b) = Ο† a * Ο† b * (a.gcd b) := begin have shuffle : βˆ€ a1 a2 b1 b2 c1 c2 : β„•, b1 ∣ a1 β†’ b2 ∣ a2 β†’ (a1/b1 * c1) * (a2/b2 * c2) = (a1*a2)/(b1*b2) * (c1*c2), { intros a1 a2 b1 b2 c1 c2 h1 h2, calc (a1/b1 * c1) * (a2/b2 * c2) = ((a1/b1) * (a2/b2)) * (c1*c2) : by apply mul_mul_mul_comm ... = (a1*a2)/(b1*b2) * (c1*c2) : by { congr' 1, exact div_mul_div_comm h1 h2 } }, simp only [totient_eq_div_factors_mul], rw [shuffle, shuffle], rotate, repeat { apply prod_prime_factors_dvd }, { simp only [prod_factors_gcd_mul_prod_factors_mul], rw [eq_comm, mul_comm, ←mul_assoc, ←nat.mul_div_assoc], exact mul_dvd_mul (prod_prime_factors_dvd a) (prod_prime_factors_dvd b) } end lemma totient_super_multiplicative (a b : β„•) : Ο† a * Ο† b ≀ Ο† (a * b) := begin let d := a.gcd b, rcases (zero_le a).eq_or_lt with rfl | ha0, { simp }, have hd0 : 0 < d, from nat.gcd_pos_of_pos_left _ ha0, rw [←mul_le_mul_right hd0, ←totient_gcd_mul_totient_mul a b, mul_comm], apply mul_le_mul_left' (nat.totient_le d), end end nat
c161ee76659f7f6190e0af4708d6378d102c12b6
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/set/card.lean
a677fbf438e4e5e5e171fa211288843a6b5940aa
[ "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
5,883
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Cardinality of finite sets. -/ import .finite data.finset.card open nat classical namespace set variable {A : Type} noncomputable definition card (s : set A) := finset.card (set.to_finset s) theorem card_to_set (s : finset A) : card (finset.to_set s) = finset.card s := by rewrite [↑card, to_finset_to_set] theorem card_of_not_finite {s : set A} (nfins : Β¬ finite s) : card s = 0 := by rewrite [↑card, to_finset_of_not_finite nfins] theorem card_empty : card (βˆ… : set A) = 0 := by rewrite [-finset.to_set_empty, card_to_set] theorem card_insert_of_mem {a : A} {s : set A} (H : a ∈ s) : card (insert a s) = card s := if fins : finite s then (by rewrite [↑card, to_finset_insert, -mem_to_finset_eq at H, finset.card_insert_of_mem H]) else (have Β¬ finite (insert a s), from suppose _, absurd (!finite_of_finite_insert this) fins, by rewrite [card_of_not_finite fins, card_of_not_finite this]) theorem card_insert_of_not_mem {a : A} {s : set A} [finite s] (H : a βˆ‰ s) : card (insert a s) = card s + 1 := by rewrite [↑card, to_finset_insert, -mem_to_finset_eq at H, finset.card_insert_of_not_mem H] theorem card_insert_le (a : A) (s : set A) [finite s] : card (insert a s) ≀ card s + 1 := if H : a ∈ s then by rewrite [card_insert_of_mem H]; apply le_succ else by rewrite [card_insert_of_not_mem H] theorem card_singleton (a : A) : card '{a} = 1 := by rewrite [card_insert_of_not_mem !not_mem_empty, card_empty] /- Note: the induction tactic does not work well with the set induction principle with the extra predicate "finite". -/ theorem eq_empty_of_card_eq_zero {s : set A} [finite s] : card s = 0 β†’ s = βˆ… := induction_on_finite s (by intro H; exact rfl) (begin intro a s' fins' anins IH H, rewrite (card_insert_of_not_mem anins) at H, apply nat.no_confusion H end) theorem card_upto (n : β„•) : card {i | i < n} = n := by rewrite [↑card, to_finset_upto, finset.card_upto] theorem card_add_card (s₁ sβ‚‚ : set A) [finite s₁] [finite sβ‚‚] : card s₁ + card sβ‚‚ = card (s₁ βˆͺ sβ‚‚) + card (s₁ ∩ sβ‚‚) := begin rewrite [-to_set_to_finset s₁, -to_set_to_finset sβ‚‚], rewrite [-finset.to_set_union, -finset.to_set_inter, *card_to_set], apply finset.card_add_card end theorem card_union (s₁ sβ‚‚ : set A) [finite s₁] [finite sβ‚‚] : card (s₁ βˆͺ sβ‚‚) = card s₁ + card sβ‚‚ - card (s₁ ∩ sβ‚‚) := calc card (s₁ βˆͺ sβ‚‚) = card (s₁ βˆͺ sβ‚‚) + card (s₁ ∩ sβ‚‚) - card (s₁ ∩ sβ‚‚) : nat.add_sub_cancel ... = card s₁ + card sβ‚‚ - card (s₁ ∩ sβ‚‚) : card_add_card s₁ sβ‚‚ theorem card_union_of_disjoint {s₁ sβ‚‚ : set A} [finite s₁] [finite sβ‚‚] (H : s₁ ∩ sβ‚‚ = βˆ…) : card (s₁ βˆͺ sβ‚‚) = card s₁ + card sβ‚‚ := by rewrite [card_union, H, card_empty] theorem card_eq_card_add_card_diff {s₁ sβ‚‚ : set A} [finite s₁] [finite sβ‚‚] (H : s₁ βŠ† sβ‚‚) : card sβ‚‚ = card s₁ + card (sβ‚‚ \ s₁) := have H1 : s₁ ∩ (sβ‚‚ \ s₁) = βˆ…, from eq_empty_of_forall_not_mem (take x, assume H, (and.right (and.right H)) (and.left H)), have sβ‚‚ = s₁ βˆͺ (sβ‚‚ \ s₁), from eq.symm (union_diff_cancel H), calc card sβ‚‚ = card (s₁ βˆͺ (sβ‚‚ \ s₁)) : {this} ... = card s₁ + card (sβ‚‚ \ s₁) : card_union_of_disjoint H1 theorem card_le_card_of_subset {s₁ sβ‚‚ : set A} [finite s₁] [finite sβ‚‚] (H : s₁ βŠ† sβ‚‚) : card s₁ ≀ card sβ‚‚ := calc card sβ‚‚ = card s₁ + card (sβ‚‚ \ s₁) : card_eq_card_add_card_diff H ... β‰₯ card s₁ : le_add_right variable {B : Type} theorem card_image_eq_of_inj_on {f : A β†’ B} {s : set A} [finite s] (injfs : inj_on f s) : card (image f s) = card s := begin rewrite [↑card, to_finset_image]; apply finset.card_image_eq_of_inj_on, rewrite to_set_to_finset, apply injfs end theorem card_le_of_inj_on (a : set A) (b : set B) [finite b] (Pex : βˆƒ f : A β†’ B, inj_on f a ∧ (image f a βŠ† b)) : card a ≀ card b := by_cases (assume fina : finite a, obtain f H, from Pex, finset.card_le_of_inj_on (to_finset a) (to_finset b) (exists.intro f begin rewrite [finset.subset_eq_to_set_subset, finset.to_set_image, *to_set_to_finset], exact H end)) (assume nfina : Β¬ finite a, by rewrite [card_of_not_finite nfina]; exact !zero_le) theorem card_image_le (f : A β†’ B) (s : set A) [finite s] : card (image f s) ≀ card s := by rewrite [↑card, to_finset_image]; apply finset.card_image_le theorem inj_on_of_card_image_eq {f : A β†’ B} {s : set A} [finite s] (H : card (image f s) = card s) : inj_on f s := begin rewrite -(to_set_to_finset s), apply finset.inj_on_of_card_image_eq, rewrite [-to_finset_to_set (finset.image _ _), finset.to_set_image, to_set_to_finset], exact H end theorem card_pos_of_mem {a : A} {s : set A} [finite s] (H : a ∈ s) : card s > 0 := have (#finset a ∈ to_finset s), by rewrite [finset.mem_eq_mem_to_set, to_set_to_finset]; apply H, finset.card_pos_of_mem this theorem eq_of_card_eq_of_subset {s₁ sβ‚‚ : set A} [finite s₁] [finite sβ‚‚] (Hcard : card s₁ = card sβ‚‚) (Hsub : s₁ βŠ† sβ‚‚) : s₁ = sβ‚‚ := begin rewrite [-to_set_to_finset s₁, -to_set_to_finset sβ‚‚, -finset.eq_eq_to_set_eq], apply finset.eq_of_card_eq_of_subset Hcard, rewrite [to_finset_subset_to_finset_eq], exact Hsub end theorem exists_two_of_card_gt_one {s : set A} (H : 1 < card s) : βˆƒ a b, a ∈ s ∧ b ∈ s ∧ a β‰  b := have fins : finite s, from by_contradiction (assume nfins, by rewrite [card_of_not_finite nfins at H]; apply !not_succ_le_zero H), by rewrite [-to_set_to_finset s]; apply finset.exists_two_of_card_gt_one H end set
8bb3bcaba29ca9eb211ee74503c12daa8272a7cc
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/whnf.lean
6988f71aafb7020f319bf4ad54520ec2d1c0ce94
[ "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
140
lean
import data.nat open nat eval [whnf] (fun x, x + 1) 2 eval (fun x, x + 1) 2 variable a : nat eval [whnf] a + succ zero eval a + succ zero
e8d3a541cf9792e5cb0f35837c5c766edfce31f4
968e2f50b755d3048175f176376eff7139e9df70
/examples/prop_logic_theory/unnamed_710.lean
94cb79cd988106550910c928d766683ef9357462
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
283
lean
variables p q r : Prop -- BEGIN example : q β†’ (p β†’ q) := begin intro h₁, -- Assume `h₁ : q`. It suffices to prove `p β†’ q`. intro hβ‚‚, -- Assume `hβ‚‚ : p`. It suffices to prove `q`. show q, from h₁, -- We show `q` by reiteration on `h₁`. end -- END
505f3ebc92d11c5b1d5639fa3d2c20fcfa10759f
fe84e287c662151bb313504482b218a503b972f3
/src/data/birange.lean
ed134b63d7b3848d53bd7722c9b054337ef20ec7
[]
no_license
NeilStrickland/lean_lib
91e163f514b829c42fe75636407138b5c75cba83
6a9563de93748ace509d9db4302db6cd77d8f92c
refs/heads/master
1,653,408,198,261
1,652,996,419,000
1,652,996,419,000
181,006,067
4
1
null
null
null
null
UTF-8
Lean
false
false
4,305
lean
import data.fintype.basic import tactic.squeeze variable (n : β„•) def birange : Type := { u : β„• Γ— β„• // u.1 + u.2 = n } namespace birange variable {n} def fst (u : birange n) := u.val.fst def snd (u : birange n) := u.val.snd lemma rel (u : birange n) : u.fst + u.snd = n := u.property @[ext] lemma ext (uβ‚€ u₁ : birange n) : uβ‚€ = u₁ ↔ (uβ‚€.fst = u₁.fst ∧ uβ‚€.snd = u₁.snd) := begin rcases uβ‚€ with ⟨⟨iβ‚€,jβ‚€βŸ©,hβ‚€βŸ©, rcases u₁ with ⟨⟨i₁,jβ‚βŸ©,hβ‚βŸ©, dsimp[fst,snd] at *, rw[subtype.ext_iff_val,prod.mk.inj_iff] end lemma ext_left (uβ‚€ u₁ : birange n) : uβ‚€ = u₁ ↔ uβ‚€.fst = u₁.fst := ⟨λ e, by rw[e], Ξ» e, by {apply (ext uβ‚€ u₁).mpr,split,assumption, let e' := uβ‚€.rel.trans u₁.rel.symm, rw[e] at e',exact nat.add_left_cancel e', } ⟩ lemma ext_right (uβ‚€ u₁ : birange n) : uβ‚€ = u₁ ↔ uβ‚€.snd = u₁.snd := ⟨λ e, by rw[e], Ξ» e, by {apply (ext uβ‚€ u₁).mpr,split, let e' := uβ‚€.rel.trans u₁.rel.symm, rw[e] at e',exact nat.add_right_cancel e', assumption } ⟩ instance : decidable_eq (birange n) := Ξ» uβ‚€ u₁, by { apply_instance } def swap : birange n β†’ birange n := Ξ» ⟨⟨i,j⟩,e⟩ , ⟨⟨j,i⟩,(add_comm j i).trans e⟩ lemma swap_swap (u : birange n) : u.swap.swap = u := by { rcases u with ⟨⟨i,j⟩,h⟩, rw[ext], split; refl, } variable (n) def fin_equiv : (birange n) ≃ fin n.succ := { to_fun := Ξ» u, ⟨u.val.1,by {apply nat.lt_succ_of_le, have := nat.le_add_right u.val.1 u.val.2, rw[u.property] at this,exact this}⟩ , inv_fun := Ξ» i, ⟨prod.mk i.val (n - i.val),nat.add_sub_of_le (nat.le_of_lt_succ i.is_lt)⟩, left_inv := Ξ» ⟨⟨i,j⟩,h⟩, begin apply (ext_left _ _).mpr,refl end, right_inv := Ξ» ⟨i,i_is_lt⟩, by { apply fin.eq_of_veq, refl } } instance : fintype (birange n) := fintype.of_equiv (fin n.succ) (fin_equiv n).symm end birange variable (n) def trirange : Type := { u : β„• Γ— β„• Γ— β„• // u.1 + (u.2.1 + u.2.2) = n } namespace trirange variable {n} def fst (u : trirange n) := u.val.1 def snd (u : trirange n) := u.val.2.1 def thd (u : trirange n) := u.val.2.2 @[ext] lemma ext (uβ‚€ u₁ : trirange n) : uβ‚€ = u₁ ↔ (uβ‚€.fst = u₁.fst ∧ uβ‚€.snd = u₁.snd ∧ uβ‚€.thd = u₁.thd) := by {rcases uβ‚€ with ⟨⟨iβ‚€,jβ‚€,kβ‚€βŸ©,hβ‚€βŸ©, rcases u₁ with ⟨⟨i₁,j₁,kβ‚βŸ©,hβ‚βŸ©, dsimp[fst,snd,thd], rw[subtype.ext_iff_val,prod.mk.inj_iff,prod.mk.inj_iff], } instance : decidable_eq (trirange n) := Ξ» uβ‚€ u₁, by { apply_instance } variable (n) def sigma_equiv : (trirange n) ≃ Ξ£ (i : fin n.succ), birange (n - i) := { to_fun := Ξ» ⟨⟨i,j,k⟩,h⟩,begin simp only[] at h, have hi : i < n.succ := by { apply nat.lt_succ_of_le, have := nat.le_add_right i (j + k), rw[h] at this, exact this, }, have hjk : j + k = n - i := by {rw[← h,add_comm i,nat.add_sub_cancel],}, exact ⟨⟨i,hi⟩,⟨⟨j,k⟩,hjk⟩⟩ end, inv_fun := Ξ» ⟨⟨i,hi⟩,⟨⟨j,k⟩,hjk⟩⟩,begin change j + k = n - i at hjk, have h := calc i + (j + k) = i + (n - i) : by {rw[hjk]} ... = n : by rw[add_comm,nat.sub_add_cancel (nat.le_of_lt_succ hi)], exact ⟨⟨i,j,k⟩,h⟩ end, left_inv := Ξ» ⟨⟨i,j,k⟩,h⟩, by {apply subtype.eq,refl,}, right_inv := Ξ» ⟨⟨i,hi⟩,⟨⟨j,k⟩,hjk⟩⟩, by {refl,} } def sigma_equiv' : (trirange n) ≃ Ξ£ (k : fin n.succ), birange (n - k) := { to_fun := Ξ» ⟨⟨i,j,k⟩,h⟩,begin simp only[] at h, have hk : k < n.succ := by { apply nat.lt_succ_of_le, have := nat.le_add_left k (i + j), rw[add_assoc,h] at this, exact this, }, have hij : i + j = n - k := by {rw[← h,← add_assoc,nat.add_sub_cancel],}, exact ⟨⟨k,hk⟩,⟨⟨i,j⟩,hij⟩⟩ end, inv_fun := Ξ» ⟨⟨k,hk⟩,⟨⟨i,j⟩,hij⟩⟩,begin change i + j = n - k at hij, have h := calc i + (j + k) = (n - k) + k : by {rw[← add_assoc,hij]} ... = n : by rw[nat.sub_add_cancel (nat.le_of_lt_succ hk)], exact ⟨⟨i,j,k⟩,h⟩ end, left_inv := Ξ» ⟨⟨i,j,k⟩,h⟩, by {apply subtype.eq,refl,}, right_inv := Ξ» ⟨⟨i,hi⟩,⟨⟨j,k⟩,hjk⟩⟩, by {refl,} } instance : fintype (trirange n) := fintype.of_equiv _ (sigma_equiv n).symm end trirange
75a614b21ee18fa384ce3e6deadb422043ca6afb
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/algebra/ring/ulift.lean
33a6aeb77756a2fa3178d6c7cdf752f7de0770f8
[ "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
2,269
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.group.ulift import data.equiv.ring /-! # `ulift` instances for ring This file defines instances for ring, semiring and related structures on `ulift` types. (Recall `ulift Ξ±` is just a "copy" of a type `Ξ±` in a higher universe.) We also provide `ulift.ring_equiv : ulift R ≃+* R`. -/ universes u v variables {Ξ± : Type u} {x y : ulift.{v} Ξ±} namespace ulift instance mul_zero_class [mul_zero_class Ξ±] : mul_zero_class (ulift Ξ±) := by refine_struct { zero := (0 : ulift Ξ±), mul := (*), .. }; tactic.pi_instance_derive_field instance distrib [distrib Ξ±] : distrib (ulift Ξ±) := by refine_struct { add := (+), mul := (*), .. }; tactic.pi_instance_derive_field instance semiring [semiring Ξ±] : semiring (ulift Ξ±) := by refine_struct { zero := (0 : ulift Ξ±), one := 1, add := (+), mul := (*), nsmul := Ξ» n f, ⟨nsmul n f.down⟩, npow := Ξ» n f, ⟨npow n f.down⟩ }; tactic.pi_instance_derive_field /-- The ring equivalence between `ulift Ξ±` and `Ξ±`. -/ def ring_equiv [semiring Ξ±] : ulift Ξ± ≃+* Ξ± := { to_fun := ulift.down, inv_fun := ulift.up, map_mul' := Ξ» x y, rfl, map_add' := Ξ» x y, rfl, left_inv := by tidy, right_inv := by tidy, } instance comm_semiring [comm_semiring Ξ±] : comm_semiring (ulift Ξ±) := by refine_struct { zero := (0 : ulift Ξ±), one := 1, add := (+), mul := (*), nsmul := Ξ» n f, ⟨nsmul n f.down⟩, npow := Ξ» n f, ⟨npow n f.down⟩ }; tactic.pi_instance_derive_field instance ring [ring Ξ±] : ring (ulift Ξ±) := by refine_struct { zero := (0 : ulift Ξ±), one := 1, add := (+), mul := (*), sub := has_sub.sub, neg := has_neg.neg, nsmul := Ξ» n f, ⟨nsmul n f.down⟩, npow := Ξ» n f, ⟨npow n f.down⟩, gsmul := Ξ» n f, ⟨gsmul n f.down⟩ }; tactic.pi_instance_derive_field instance comm_ring [comm_ring Ξ±] : comm_ring (ulift Ξ±) := by refine_struct { zero := (0 : ulift Ξ±), one := 1, add := (+), mul := (*), sub := has_sub.sub, neg := has_neg.neg, nsmul := Ξ» n f, ⟨nsmul n f.down⟩, npow := Ξ» n f, ⟨npow n f.down⟩, gsmul := Ξ» n f, ⟨gsmul n f.down⟩ }; tactic.pi_instance_derive_field end ulift
136e7a9ef3c23c7f3b0d430b8268ea7a87e6ab23
b2fe74b11b57d362c13326bc5651244f111fa6f4
/src/algebra/ordered_group.lean
8b95c819ed436d185072dbbf3d7647b0035183e4
[ "Apache-2.0" ]
permissive
midfield/mathlib
c4db5fa898b5ac8f2f80ae0d00c95eb6f745f4c7
775edc615ecec631d65b6180dbcc7bc26c3abc26
refs/heads/master
1,675,330,551,921
1,608,304,514,000
1,608,304,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
34,134
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes HΓΆlzl -/ import algebra.ordered_monoid set_option old_structure_cmd true /-! # Ordered groups This file develops the basics of ordered groups. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. -/ universe u variable {Ξ± : Type u} /-- An ordered additive commutative group is an additive commutative group with a partial order in which addition is strictly monotone. -/ @[protect_proj, ancestor add_comm_group partial_order] class ordered_add_comm_group (Ξ± : Type u) extends add_comm_group Ξ±, partial_order Ξ± := (add_le_add_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c + a ≀ c + b) /-- An ordered commutative group is an commutative group with a partial order in which multiplication is strictly monotone. -/ @[protect_proj, ancestor comm_group partial_order] class ordered_comm_group (Ξ± : Type u) extends comm_group Ξ±, partial_order Ξ± := (mul_le_mul_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c * a ≀ c * b) attribute [to_additive] ordered_comm_group /--The units of an ordered commutative monoid form an ordered commutative group. -/ @[to_additive] instance units.ordered_comm_group [ordered_comm_monoid Ξ±] : ordered_comm_group (units Ξ±) := { mul_le_mul_left := Ξ» a b h c, mul_le_mul_left' h _, .. units.partial_order, .. (infer_instance : comm_group (units Ξ±)) } section ordered_comm_group variables [ordered_comm_group Ξ±] {a b c d : Ξ±} @[to_additive ordered_add_comm_group.add_lt_add_left] lemma ordered_comm_group.mul_lt_mul_left' (a b : Ξ±) (h : a < b) (c : Ξ±) : c * a < c * b := begin rw lt_iff_le_not_le at h ⊒, split, { apply ordered_comm_group.mul_le_mul_left _ _ h.1 }, { intro w, replace w : c⁻¹ * (c * b) ≀ c⁻¹ * (c * a) := ordered_comm_group.mul_le_mul_left _ _ w _, simp only [mul_one, mul_comm, mul_left_inv, mul_left_comm] at w, exact h.2 w }, end @[to_additive ordered_add_comm_group.le_of_add_le_add_left] lemma ordered_comm_group.le_of_mul_le_mul_left (h : a * b ≀ a * c) : b ≀ c := have a⁻¹ * (a * b) ≀ a⁻¹ * (a * c), from ordered_comm_group.mul_le_mul_left _ _ h _, begin simp [inv_mul_cancel_left] at this, assumption end @[to_additive] lemma ordered_comm_group.lt_of_mul_lt_mul_left (h : a * b < a * c) : b < c := have a⁻¹ * (a * b) < a⁻¹ * (a * c), from ordered_comm_group.mul_lt_mul_left' _ _ h _, begin simp [inv_mul_cancel_left] at this, assumption end @[priority 100, to_additive] -- see Note [lower instance priority] instance ordered_comm_group.to_ordered_cancel_comm_monoid (Ξ± : Type u) [s : ordered_comm_group Ξ±] : ordered_cancel_comm_monoid Ξ± := { mul_left_cancel := @mul_left_cancel Ξ± _, mul_right_cancel := @mul_right_cancel Ξ± _, le_of_mul_le_mul_left := @ordered_comm_group.le_of_mul_le_mul_left Ξ± _, ..s } @[to_additive neg_le_neg] lemma inv_le_inv' (h : a ≀ b) : b⁻¹ ≀ a⁻¹ := have 1 ≀ a⁻¹ * b, from mul_left_inv a β–Έ mul_le_mul_left' h _, have 1 * b⁻¹ ≀ a⁻¹ * b * b⁻¹, from mul_le_mul_right' this _, by rwa [mul_inv_cancel_right, one_mul] at this @[to_additive] lemma le_of_inv_le_inv (h : b⁻¹ ≀ a⁻¹) : a ≀ b := suffices (a⁻¹)⁻¹ ≀ (b⁻¹)⁻¹, from begin simp [inv_inv] at this, assumption end, inv_le_inv' h @[to_additive] lemma one_le_of_inv_le_one (h : a⁻¹ ≀ 1) : 1 ≀ a := have a⁻¹ ≀ 1⁻¹, by rwa one_inv, le_of_inv_le_inv this @[to_additive] lemma inv_le_one_of_one_le (h : 1 ≀ a) : a⁻¹ ≀ 1 := have a⁻¹ ≀ 1⁻¹, from inv_le_inv' h, by rwa one_inv at this @[to_additive nonpos_of_neg_nonneg] lemma le_one_of_one_le_inv (h : 1 ≀ a⁻¹) : a ≀ 1 := have 1⁻¹ ≀ a⁻¹, by rwa one_inv, le_of_inv_le_inv this @[to_additive neg_nonneg_of_nonpos] lemma one_le_inv_of_le_one (h : a ≀ 1) : 1 ≀ a⁻¹ := have 1⁻¹ ≀ a⁻¹, from inv_le_inv' h, by rwa one_inv at this @[to_additive neg_lt_neg] lemma inv_lt_inv' (h : a < b) : b⁻¹ < a⁻¹ := have 1 < a⁻¹ * b, from mul_left_inv a β–Έ mul_lt_mul_left' h (a⁻¹), have 1 * b⁻¹ < a⁻¹ * b * b⁻¹, from mul_lt_mul_right' this (b⁻¹), by rwa [mul_inv_cancel_right, one_mul] at this @[to_additive] lemma lt_of_inv_lt_inv (h : b⁻¹ < a⁻¹) : a < b := inv_inv a β–Έ inv_inv b β–Έ inv_lt_inv' h @[to_additive] lemma one_lt_of_inv_inv (h : a⁻¹ < 1) : 1 < a := have a⁻¹ < 1⁻¹, by rwa one_inv, lt_of_inv_lt_inv this @[to_additive] lemma inv_inv_of_one_lt (h : 1 < a) : a⁻¹ < 1 := have a⁻¹ < 1⁻¹, from inv_lt_inv' h, by rwa one_inv at this @[to_additive neg_of_neg_pos] lemma inv_of_one_lt_inv (h : 1 < a⁻¹) : a < 1 := have 1⁻¹ < a⁻¹, by rwa one_inv, lt_of_inv_lt_inv this @[to_additive neg_pos_of_neg] lemma one_lt_inv_of_inv (h : a < 1) : 1 < a⁻¹ := have 1⁻¹ < a⁻¹, from inv_lt_inv' h, by rwa one_inv at this @[to_additive] lemma le_inv_of_le_inv (h : a ≀ b⁻¹) : b ≀ a⁻¹ := begin have h := inv_le_inv' h, rwa inv_inv at h end @[to_additive] lemma inv_le_of_inv_le (h : a⁻¹ ≀ b) : b⁻¹ ≀ a := begin have h := inv_le_inv' h, rwa inv_inv at h end @[to_additive] lemma lt_inv_of_lt_inv (h : a < b⁻¹) : b < a⁻¹ := begin have h := inv_lt_inv' h, rwa inv_inv at h end @[to_additive] lemma inv_lt_of_inv_lt (h : a⁻¹ < b) : b⁻¹ < a := begin have h := inv_lt_inv' h, rwa inv_inv at h end @[to_additive] lemma mul_le_of_le_inv_mul (h : b ≀ a⁻¹ * c) : a * b ≀ c := begin have h := mul_le_mul_left' h a, rwa mul_inv_cancel_left at h end @[to_additive] lemma le_inv_mul_of_mul_le (h : a * b ≀ c) : b ≀ a⁻¹ * c := begin have h := mul_le_mul_left' h a⁻¹, rwa inv_mul_cancel_left at h end @[to_additive] lemma le_mul_of_inv_mul_le (h : b⁻¹ * a ≀ c) : a ≀ b * c := begin have h := mul_le_mul_left' h b, rwa mul_inv_cancel_left at h end @[to_additive] lemma inv_mul_le_of_le_mul (h : a ≀ b * c) : b⁻¹ * a ≀ c := begin have h := mul_le_mul_left' h b⁻¹, rwa inv_mul_cancel_left at h end @[to_additive] lemma le_mul_of_inv_mul_le_left (h : b⁻¹ * a ≀ c) : a ≀ b * c := le_mul_of_inv_mul_le h @[to_additive] lemma inv_mul_le_left_of_le_mul (h : a ≀ b * c) : b⁻¹ * a ≀ c := inv_mul_le_of_le_mul h @[to_additive] lemma le_mul_of_inv_mul_le_right (h : c⁻¹ * a ≀ b) : a ≀ b * c := by { rw mul_comm, exact le_mul_of_inv_mul_le h } @[to_additive] lemma inv_mul_le_right_of_le_mul (h : a ≀ b * c) : c⁻¹ * a ≀ b := by { rw mul_comm at h, apply inv_mul_le_left_of_le_mul h } @[to_additive] lemma mul_lt_of_lt_inv_mul (h : b < a⁻¹ * c) : a * b < c := begin have h := mul_lt_mul_left' h a, rwa mul_inv_cancel_left at h end @[to_additive] lemma lt_inv_mul_of_mul_lt (h : a * b < c) : b < a⁻¹ * c := begin have h := mul_lt_mul_left' h (a⁻¹), rwa inv_mul_cancel_left at h end @[to_additive] lemma lt_mul_of_inv_mul_lt (h : b⁻¹ * a < c) : a < b * c := begin have h := mul_lt_mul_left' h b, rwa mul_inv_cancel_left at h end @[to_additive] lemma inv_mul_lt_of_lt_mul (h : a < b * c) : b⁻¹ * a < c := begin have h := mul_lt_mul_left' h (b⁻¹), rwa inv_mul_cancel_left at h end @[to_additive] lemma lt_mul_of_inv_mul_lt_left (h : b⁻¹ * a < c) : a < b * c := lt_mul_of_inv_mul_lt h @[to_additive] lemma inv_mul_lt_left_of_lt_mul (h : a < b * c) : b⁻¹ * a < c := inv_mul_lt_of_lt_mul h @[to_additive] lemma lt_mul_of_inv_mul_lt_right (h : c⁻¹ * a < b) : a < b * c := by { rw mul_comm, exact lt_mul_of_inv_mul_lt h } @[to_additive] lemma inv_mul_lt_right_of_lt_mul (h : a < b * c) : c⁻¹ * a < b := by { rw mul_comm at h, exact inv_mul_lt_of_lt_mul h } @[simp, to_additive] lemma inv_lt_one_iff_one_lt : a⁻¹ < 1 ↔ 1 < a := ⟨ one_lt_of_inv_inv, inv_inv_of_one_lt ⟩ @[simp, to_additive] lemma inv_le_inv_iff : a⁻¹ ≀ b⁻¹ ↔ b ≀ a := have a * b * a⁻¹ ≀ a * b * b⁻¹ ↔ a⁻¹ ≀ b⁻¹, from mul_le_mul_iff_left _, by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] } @[to_additive neg_le] lemma inv_le' : a⁻¹ ≀ b ↔ b⁻¹ ≀ a := have a⁻¹ ≀ (b⁻¹)⁻¹ ↔ b⁻¹ ≀ a, from inv_le_inv_iff, by rwa inv_inv at this @[to_additive le_neg] lemma le_inv' : a ≀ b⁻¹ ↔ b ≀ a⁻¹ := have (a⁻¹)⁻¹ ≀ b⁻¹ ↔ b ≀ a⁻¹, from inv_le_inv_iff, by rwa inv_inv at this @[to_additive neg_le_iff_add_nonneg] lemma inv_le_iff_one_le_mul : a⁻¹ ≀ b ↔ 1 ≀ a * b := (mul_le_mul_iff_left a).symm.trans $ by rw mul_inv_self @[to_additive] lemma le_inv_iff_mul_le_one : a ≀ b⁻¹ ↔ a * b ≀ 1 := (mul_le_mul_iff_right b).symm.trans $ by rw inv_mul_self @[simp, to_additive neg_nonpos] lemma inv_le_one' : a⁻¹ ≀ 1 ↔ 1 ≀ a := have a⁻¹ ≀ 1⁻¹ ↔ 1 ≀ a, from inv_le_inv_iff, by rwa one_inv at this @[simp, to_additive neg_nonneg] lemma one_le_inv' : 1 ≀ a⁻¹ ↔ a ≀ 1 := have 1⁻¹ ≀ a⁻¹ ↔ a ≀ 1, from inv_le_inv_iff, by rwa one_inv at this @[to_additive] lemma inv_le_self (h : 1 ≀ a) : a⁻¹ ≀ a := le_trans (inv_le_one'.2 h) h @[to_additive] lemma self_le_inv (h : a ≀ 1) : a ≀ a⁻¹ := le_trans h (one_le_inv'.2 h) @[simp, to_additive] lemma inv_lt_inv_iff : a⁻¹ < b⁻¹ ↔ b < a := have a * b * a⁻¹ < a * b * b⁻¹ ↔ a⁻¹ < b⁻¹, from mul_lt_mul_iff_left _, by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] } @[to_additive neg_lt_zero] lemma inv_lt_one' : a⁻¹ < 1 ↔ 1 < a := have a⁻¹ < 1⁻¹ ↔ 1 < a, from inv_lt_inv_iff, by rwa one_inv at this @[to_additive neg_pos] lemma one_lt_inv' : 1 < a⁻¹ ↔ a < 1 := have 1⁻¹ < a⁻¹ ↔ a < 1, from inv_lt_inv_iff, by rwa one_inv at this @[to_additive neg_lt] lemma inv_lt' : a⁻¹ < b ↔ b⁻¹ < a := have a⁻¹ < (b⁻¹)⁻¹ ↔ b⁻¹ < a, from inv_lt_inv_iff, by rwa inv_inv at this @[to_additive lt_neg] lemma lt_inv' : a < b⁻¹ ↔ b < a⁻¹ := have (a⁻¹)⁻¹ < b⁻¹ ↔ b < a⁻¹, from inv_lt_inv_iff, by rwa inv_inv at this @[to_additive] lemma le_inv_mul_iff_mul_le : b ≀ a⁻¹ * c ↔ a * b ≀ c := have a⁻¹ * (a * b) ≀ a⁻¹ * c ↔ a * b ≀ c, from mul_le_mul_iff_left _, by rwa inv_mul_cancel_left at this @[simp, to_additive] lemma inv_mul_le_iff_le_mul : b⁻¹ * a ≀ c ↔ a ≀ b * c := have b⁻¹ * a ≀ b⁻¹ * (b * c) ↔ a ≀ b * c, from mul_le_mul_iff_left _, by rwa inv_mul_cancel_left at this @[to_additive] lemma mul_inv_le_iff_le_mul : a * c⁻¹ ≀ b ↔ a ≀ b * c := by rw [mul_comm a, mul_comm b, inv_mul_le_iff_le_mul] @[simp, to_additive] lemma mul_inv_le_iff_le_mul' : a * b⁻¹ ≀ c ↔ a ≀ b * c := by rw [← inv_mul_le_iff_le_mul, mul_comm] @[to_additive] lemma inv_mul_le_iff_le_mul' : c⁻¹ * a ≀ b ↔ a ≀ b * c := by rw [inv_mul_le_iff_le_mul, mul_comm] @[simp, to_additive] lemma lt_inv_mul_iff_mul_lt : b < a⁻¹ * c ↔ a * b < c := have a⁻¹ * (a * b) < a⁻¹ * c ↔ a * b < c, from mul_lt_mul_iff_left _, by rwa inv_mul_cancel_left at this @[simp, to_additive] lemma inv_mul_lt_iff_lt_mul : b⁻¹ * a < c ↔ a < b * c := have b⁻¹ * a < b⁻¹ * (b * c) ↔ a < b * c, from mul_lt_mul_iff_left _, by rwa inv_mul_cancel_left at this @[to_additive] lemma inv_mul_lt_iff_lt_mul_right : c⁻¹ * a < b ↔ a < b * c := by rw [inv_mul_lt_iff_lt_mul, mul_comm] @[to_additive add_neg_le_add_neg_iff] lemma div_le_div_iff' : a * b⁻¹ ≀ c * d⁻¹ ↔ a * d ≀ c * b := begin split ; intro h, have := mul_le_mul_right' (mul_le_mul_right' h b) d, rwa [inv_mul_cancel_right, mul_assoc _ _ b, mul_comm _ b, ← mul_assoc, inv_mul_cancel_right] at this, have := mul_le_mul_right' (mul_le_mul_right' h d⁻¹) b⁻¹, rwa [mul_inv_cancel_right, _root_.mul_assoc, _root_.mul_comm d⁻¹ b⁻¹, ← mul_assoc, mul_inv_cancel_right] at this, end end ordered_comm_group section ordered_add_comm_group variables [ordered_add_comm_group Ξ±] {a b c d : Ξ±} lemma sub_nonneg_of_le (h : b ≀ a) : 0 ≀ a - b := begin have h := add_le_add_right h (-b), rwa [add_right_neg, ← sub_eq_add_neg] at h end lemma le_of_sub_nonneg (h : 0 ≀ a - b) : b ≀ a := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_nonpos_of_le (h : a ≀ b) : a - b ≀ 0 := begin have h := add_le_add_right h (-b), rwa [add_right_neg, ← sub_eq_add_neg] at h end lemma le_of_sub_nonpos (h : a - b ≀ 0) : a ≀ b := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_pos_of_lt (h : b < a) : 0 < a - b := begin have h := add_lt_add_right h (-b), rwa [add_right_neg, ← sub_eq_add_neg] at h end lemma lt_of_sub_pos (h : 0 < a - b) : b < a := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_neg_of_lt (h : a < b) : a - b < 0 := begin have h := add_lt_add_right h (-b), rwa [add_right_neg, ← sub_eq_add_neg] at h end lemma lt_of_sub_neg (h : a - b < 0) : a < b := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma add_le_of_le_sub_left (h : b ≀ c - a) : a + b ≀ c := begin have h := add_le_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma le_sub_left_of_add_le (h : a + b ≀ c) : b ≀ c - a := begin have h := add_le_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right, ← sub_eq_add_neg] at h end lemma add_le_of_le_sub_right (h : a ≀ c - b) : a + b ≀ c := begin have h := add_le_add_right h b, rwa sub_add_cancel at h end lemma le_sub_right_of_add_le (h : a + b ≀ c) : a ≀ c - b := begin have h := add_le_add_right h (-b), rwa [add_neg_cancel_right, ← sub_eq_add_neg] at h end lemma le_add_of_sub_left_le (h : a - b ≀ c) : a ≀ b + c := begin have h := add_le_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_le_of_le_add (h : a ≀ b + c) : a - b ≀ c := begin have h := add_le_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right, ← sub_eq_add_neg] at h end lemma le_add_of_sub_right_le (h : a - c ≀ b) : a ≀ b + c := begin have h := add_le_add_right h c, rwa sub_add_cancel at h end lemma sub_right_le_of_le_add (h : a ≀ b + c) : a - c ≀ b := begin have h := add_le_add_right h (-c), rwa [add_neg_cancel_right, ← sub_eq_add_neg] at h end lemma le_add_of_neg_le_sub_left (h : -a ≀ b - c) : c ≀ a + b := le_add_of_neg_add_le_left (add_le_of_le_sub_right h) lemma neg_le_sub_left_of_le_add (h : c ≀ a + b) : -a ≀ b - c := begin rw [sub_eq_add_neg, add_comm], apply le_neg_add_of_add_le, have h := (sub_left_le_of_le_add h), rwa sub_eq_add_neg at h end lemma le_add_of_neg_le_sub_right (h : -b ≀ a - c) : c ≀ a + b := begin have h := add_le_of_le_sub_left h, rw ← sub_eq_add_neg at h, exact le_add_of_sub_right_le h end lemma neg_le_sub_right_of_le_add (h : c ≀ a + b) : -b ≀ a - c := begin have h := sub_right_le_of_le_add h, rw sub_eq_add_neg at h, exact le_sub_left_of_add_le h end lemma sub_le_of_sub_le (h : a - b ≀ c) : a - c ≀ b := sub_left_le_of_le_add (le_add_of_sub_right_le h) lemma sub_le_sub_left (h : a ≀ b) (c : Ξ±) : c - b ≀ c - a := by simpa only [sub_eq_add_neg] using add_le_add_left (neg_le_neg h) c lemma sub_le_sub_right (h : a ≀ b) (c : Ξ±) : a - c ≀ b - c := by simpa only [sub_eq_add_neg] using add_le_add_right h (-c) lemma sub_le_sub (hab : a ≀ b) (hcd : c ≀ d) : a - d ≀ b - c := by simpa only [sub_eq_add_neg] using add_le_add hab (neg_le_neg hcd) lemma add_lt_of_lt_sub_left (h : b < c - a) : a + b < c := begin have h := add_lt_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma lt_sub_left_of_add_lt (h : a + b < c) : b < c - a := begin have h := add_lt_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right, ← sub_eq_add_neg] at h end lemma add_lt_of_lt_sub_right (h : a < c - b) : a + b < c := begin have h := add_lt_add_right h b, rwa sub_add_cancel at h end lemma lt_sub_right_of_add_lt (h : a + b < c) : a < c - b := begin have h := add_lt_add_right h (-b), rwa [add_neg_cancel_right, ← sub_eq_add_neg] at h end lemma lt_add_of_sub_left_lt (h : a - b < c) : a < b + c := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_lt_of_lt_add (h : a < b + c) : a - b < c := begin have h := add_lt_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right, ← sub_eq_add_neg] at h end lemma lt_add_of_sub_right_lt (h : a - c < b) : a < b + c := begin have h := add_lt_add_right h c, rwa sub_add_cancel at h end lemma sub_right_lt_of_lt_add (h : a < b + c) : a - c < b := begin have h := add_lt_add_right h (-c), rwa [add_neg_cancel_right, ← sub_eq_add_neg] at h end lemma lt_add_of_neg_lt_sub_left (h : -a < b - c) : c < a + b := lt_add_of_neg_add_lt_left (add_lt_of_lt_sub_right h) lemma neg_lt_sub_left_of_lt_add (h : c < a + b) : -a < b - c := begin have h := sub_left_lt_of_lt_add h, rw sub_eq_add_neg at h, have h := lt_neg_add_of_add_lt h, rwa [add_comm, ← sub_eq_add_neg] at h end lemma lt_add_of_neg_lt_sub_right (h : -b < a - c) : c < a + b := begin have h := add_lt_of_lt_sub_left h, rw ← sub_eq_add_neg at h, exact lt_add_of_sub_right_lt h end lemma neg_lt_sub_right_of_lt_add (h : c < a + b) : -b < a - c := begin have h := sub_right_lt_of_lt_add h, rw sub_eq_add_neg at h, exact lt_sub_left_of_add_lt h end lemma sub_lt_of_sub_lt (h : a - b < c) : a - c < b := sub_left_lt_of_lt_add (lt_add_of_sub_right_lt h) lemma sub_lt_sub_left (h : a < b) (c : Ξ±) : c - b < c - a := by simpa only [sub_eq_add_neg] using add_lt_add_left (neg_lt_neg h) c lemma sub_lt_sub_right (h : a < b) (c : Ξ±) : a - c < b - c := by simpa only [sub_eq_add_neg] using add_lt_add_right h (-c) lemma sub_lt_sub (hab : a < b) (hcd : c < d) : a - d < b - c := by simpa only [sub_eq_add_neg] using add_lt_add hab (neg_lt_neg hcd) lemma sub_lt_sub_of_le_of_lt (hab : a ≀ b) (hcd : c < d) : a - d < b - c := by simpa only [sub_eq_add_neg] using add_lt_add_of_le_of_lt hab (neg_lt_neg hcd) lemma sub_lt_sub_of_lt_of_le (hab : a < b) (hcd : c ≀ d) : a - d < b - c := by simpa only [sub_eq_add_neg] using add_lt_add_of_lt_of_le hab (neg_le_neg hcd) lemma sub_le_self (a : Ξ±) {b : Ξ±} (h : 0 ≀ b) : a - b ≀ a := calc a - b = a + -b : sub_eq_add_neg _ _ ... ≀ a + 0 : add_le_add_left (neg_nonpos_of_nonneg h) _ ... = a : by rw add_zero lemma sub_lt_self (a : Ξ±) {b : Ξ±} (h : 0 < b) : a - b < a := calc a - b = a + -b : sub_eq_add_neg _ _ ... < a + 0 : add_lt_add_left (neg_neg_of_pos h) _ ... = a : by rw add_zero lemma sub_le_sub_iff : a - b ≀ c - d ↔ a + d ≀ c + b := by simpa only [sub_eq_add_neg] using add_neg_le_add_neg_iff @[simp] lemma sub_le_sub_iff_left (a : Ξ±) {b c : Ξ±} : a - b ≀ a - c ↔ c ≀ b := by rw [sub_eq_add_neg, sub_eq_add_neg, add_le_add_iff_left, neg_le_neg_iff] @[simp] lemma sub_le_sub_iff_right (c : Ξ±) : a - c ≀ b - c ↔ a ≀ b := by simpa only [sub_eq_add_neg] using add_le_add_iff_right _ @[simp] lemma sub_lt_sub_iff_left (a : Ξ±) {b c : Ξ±} : a - b < a - c ↔ c < b := by rw [sub_eq_add_neg, sub_eq_add_neg, add_lt_add_iff_left, neg_lt_neg_iff] @[simp] lemma sub_lt_sub_iff_right (c : Ξ±) : a - c < b - c ↔ a < b := by simpa only [sub_eq_add_neg] using add_lt_add_iff_right _ @[simp] lemma sub_nonneg : 0 ≀ a - b ↔ b ≀ a := have a - a ≀ a - b ↔ b ≀ a, from sub_le_sub_iff_left a, by rwa sub_self at this @[simp] lemma sub_nonpos : a - b ≀ 0 ↔ a ≀ b := have a - b ≀ b - b ↔ a ≀ b, from sub_le_sub_iff_right b, by rwa sub_self at this @[simp] lemma sub_pos : 0 < a - b ↔ b < a := have a - a < a - b ↔ b < a, from sub_lt_sub_iff_left a, by rwa sub_self at this @[simp] lemma sub_lt_zero : a - b < 0 ↔ a < b := have a - b < b - b ↔ a < b, from sub_lt_sub_iff_right b, by rwa sub_self at this lemma le_sub_iff_add_le' : b ≀ c - a ↔ a + b ≀ c := by rw [sub_eq_add_neg, add_comm, le_neg_add_iff_add_le] lemma le_sub_iff_add_le : a ≀ c - b ↔ a + b ≀ c := by rw [le_sub_iff_add_le', add_comm] lemma sub_le_iff_le_add' : a - b ≀ c ↔ a ≀ b + c := by rw [sub_eq_add_neg, add_comm, neg_add_le_iff_le_add] lemma sub_le_iff_le_add : a - c ≀ b ↔ a ≀ b + c := by rw [sub_le_iff_le_add', add_comm] @[simp] lemma neg_le_sub_iff_le_add : -b ≀ a - c ↔ c ≀ a + b := le_sub_iff_add_le.trans neg_add_le_iff_le_add' lemma neg_le_sub_iff_le_add' : -a ≀ b - c ↔ c ≀ a + b := by rw [neg_le_sub_iff_le_add, add_comm] lemma sub_le : a - b ≀ c ↔ a - c ≀ b := sub_le_iff_le_add'.trans sub_le_iff_le_add.symm theorem le_sub : a ≀ b - c ↔ c ≀ b - a := le_sub_iff_add_le'.trans le_sub_iff_add_le.symm lemma lt_sub_iff_add_lt' : b < c - a ↔ a + b < c := by rw [sub_eq_add_neg, add_comm, lt_neg_add_iff_add_lt] lemma lt_sub_iff_add_lt : a < c - b ↔ a + b < c := by rw [lt_sub_iff_add_lt', add_comm] lemma sub_lt_iff_lt_add' : a - b < c ↔ a < b + c := by rw [sub_eq_add_neg, add_comm, neg_add_lt_iff_lt_add] lemma sub_lt_iff_lt_add : a - c < b ↔ a < b + c := by rw [sub_lt_iff_lt_add', add_comm] @[simp] lemma neg_lt_sub_iff_lt_add : -b < a - c ↔ c < a + b := lt_sub_iff_add_lt.trans neg_add_lt_iff_lt_add_right lemma neg_lt_sub_iff_lt_add' : -a < b - c ↔ c < a + b := by rw [neg_lt_sub_iff_lt_add, add_comm] lemma sub_lt : a - b < c ↔ a - c < b := sub_lt_iff_lt_add'.trans sub_lt_iff_lt_add.symm theorem lt_sub : a < b - c ↔ c < b - a := lt_sub_iff_add_lt'.trans lt_sub_iff_add_lt.symm lemma sub_le_self_iff (a : Ξ±) {b : Ξ±} : a - b ≀ a ↔ 0 ≀ b := sub_le_iff_le_add'.trans (le_add_iff_nonneg_left _) lemma sub_lt_self_iff (a : Ξ±) {b : Ξ±} : a - b < a ↔ 0 < b := sub_lt_iff_lt_add'.trans (lt_add_iff_pos_left _) end ordered_add_comm_group /-- A decidable linearly ordered additive commutative group is an additive commutative group with a decidable linear order in which addition is strictly monotone. -/ @[protect_proj] class linear_ordered_add_comm_group (Ξ± : Type u) extends add_comm_group Ξ±, linear_order Ξ± := (add_le_add_left : βˆ€ a b : Ξ±, a ≀ b β†’ βˆ€ c : Ξ±, c + a ≀ c + b) @[priority 100] -- see Note [lower instance priority] instance linear_ordered_comm_group.to_ordered_add_comm_group (Ξ± : Type u) [s : linear_ordered_add_comm_group Ξ±] : ordered_add_comm_group Ξ± := { add := s.add, ..s } section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group Ξ±] {a b c : Ξ±} @[priority 100] -- see Note [lower instance priority] instance linear_ordered_add_comm_group.to_linear_ordered_cancel_add_comm_monoid : linear_ordered_cancel_add_comm_monoid Ξ± := { le_of_add_le_add_left := Ξ» x y z, le_of_add_le_add_left, add_left_cancel := Ξ» x y z, add_left_cancel, add_right_cancel := Ξ» x y z, add_right_cancel, ..β€Ήlinear_ordered_add_comm_group Ξ±β€Ί } lemma linear_ordered_add_comm_group.add_lt_add_left (a b : Ξ±) (h : a < b) (c : Ξ±) : c + a < c + b := ordered_add_comm_group.add_lt_add_left a b h c lemma min_neg_neg (a b : Ξ±) : min (-a) (-b) = -max a b := eq.symm $ @monotone.map_max Ξ± (order_dual Ξ±) _ _ has_neg.neg a b $ Ξ» a b, neg_le_neg lemma max_neg_neg (a b : Ξ±) : max (-a) (-b) = -min a b := eq.symm $ @monotone.map_min Ξ± (order_dual Ξ±) _ _ has_neg.neg a b $ Ξ» a b, neg_le_neg lemma min_sub_sub_right (a b c : Ξ±) : min (a - c) (b - c) = min a b - c := by simpa only [sub_eq_add_neg] using min_add_add_right a b (-c) lemma max_sub_sub_right (a b c : Ξ±) : max (a - c) (b - c) = max a b - c := by simpa only [sub_eq_add_neg] using max_add_add_right a b (-c) lemma min_sub_sub_left (a b c : Ξ±) : min (a - b) (a - c) = a - max b c := by simp only [sub_eq_add_neg, min_add_add_left, min_neg_neg] lemma max_sub_sub_left (a b c : Ξ±) : max (a - b) (a - c) = a - min b c := by simp only [sub_eq_add_neg, max_add_add_left, max_neg_neg] lemma max_zero_sub_eq_self (a : Ξ±) : max a 0 - max (-a) 0 = a := begin rcases le_total a 0, { rw [max_eq_right h, max_eq_left, zero_sub, neg_neg], { rwa [le_neg, neg_zero] } }, { rw [max_eq_left, max_eq_right, sub_zero], { rwa [neg_le, neg_zero] }, exact h } end /-- `abs a` is the absolute value of `a`. -/ def abs (a : Ξ±) : Ξ± := max a (-a) lemma abs_of_nonneg (h : 0 ≀ a) : abs a = a := max_eq_left $ (neg_nonpos.2 h).trans h lemma abs_of_pos (h : 0 < a) : abs a = a := abs_of_nonneg h.le lemma abs_of_nonpos (h : a ≀ 0) : abs a = -a := max_eq_right $ h.trans (neg_nonneg.2 h) lemma abs_of_neg (h : a < 0) : abs a = -a := abs_of_nonpos h.le @[simp] lemma abs_zero : abs 0 = (0:Ξ±) := abs_of_nonneg le_rfl @[simp] lemma abs_neg (a : Ξ±) : abs (-a) = abs a := begin unfold abs, rw [max_comm, neg_neg] end @[simp] lemma abs_pos : 0 < abs a ↔ a β‰  0 := begin rcases lt_trichotomy a 0 with (ha|rfl|ha), { simp [abs_of_neg ha, neg_pos, ha.ne, ha] }, { simp }, { simp [abs_of_pos ha, ha, ha.ne.symm] } end lemma abs_pos_of_pos (h : 0 < a) : 0 < abs a := abs_pos.2 h.ne.symm lemma abs_pos_of_neg (h : a < 0) : 0 < abs a := abs_pos.2 h.ne lemma abs_sub (a b : Ξ±) : abs (a - b) = abs (b - a) := by rw [← neg_sub, abs_neg] theorem abs_le' : abs a ≀ b ↔ a ≀ b ∧ -a ≀ b := max_le_iff theorem abs_le : abs a ≀ b ↔ - b ≀ a ∧ a ≀ b := by rw [abs_le', and.comm, neg_le] lemma le_abs_self (a : Ξ±) : a ≀ abs a := le_max_left _ _ lemma neg_le_abs_self (a : Ξ±) : -a ≀ abs a := le_max_right _ _ lemma abs_nonneg (a : Ξ±) : 0 ≀ abs a := (le_total 0 a).elim (Ξ» h, h.trans (le_abs_self a)) (Ξ» h, (neg_nonneg.2 h).trans $ neg_le_abs_self a) @[simp] lemma abs_abs (a : Ξ±) : abs (abs a) = abs a := abs_of_nonneg $ abs_nonneg a @[simp] lemma abs_eq_zero : abs a = 0 ↔ a = 0 := not_iff_not.1 $ ne_comm.trans $ (abs_nonneg a).lt_iff_ne.symm.trans abs_pos @[simp] lemma abs_nonpos_iff {a : Ξ±} : abs a ≀ 0 ↔ a = 0 := (abs_nonneg a).le_iff_eq.trans abs_eq_zero lemma abs_lt {a b : Ξ±} : abs a < b ↔ - b < a ∧ a < b := max_lt_iff.trans $ and.comm.trans $ by rw [neg_lt] lemma lt_abs {a b : Ξ±} : a < abs b ↔ a < b ∨ a < -b := lt_max_iff lemma max_sub_min_eq_abs' (a b : Ξ±) : max a b - min a b = abs (a - b) := begin cases le_total a b with ab ba, { rw [max_eq_right ab, min_eq_left ab, abs_of_nonpos, neg_sub], rwa sub_nonpos }, { rw [max_eq_left ba, min_eq_right ba, abs_of_nonneg], exact sub_nonneg_of_le ba } end lemma max_sub_min_eq_abs (a b : Ξ±) : max a b - min a b = abs (b - a) := by { rw [abs_sub], exact max_sub_min_eq_abs' _ _ } lemma abs_add (a b : Ξ±) : abs (a + b) ≀ abs a + abs b := abs_le.2 ⟨(neg_add (abs a) (abs b)).symm β–Έ add_le_add (neg_le.2 $ neg_le_abs_self _) (neg_le.2 $ neg_le_abs_self _), add_le_add (le_abs_self _) (le_abs_self _)⟩ lemma abs_sub_le_iff : abs (a - b) ≀ c ↔ a - b ≀ c ∧ b - a ≀ c := by rw [abs_le, neg_le_sub_iff_le_add, @sub_le_iff_le_add' _ _ b, and_comm] lemma abs_sub_lt_iff : abs (a - b) < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add, @sub_lt_iff_lt_add' _ _ b, and_comm] lemma abs_sub_abs_le_abs_sub (a b : Ξ±) : abs a - abs b ≀ abs (a - b) := sub_le_iff_le_add.2 $ calc abs a = abs (a - b + b) : by rw [sub_add_cancel] ... ≀ abs (a - b) + abs b : abs_add _ _ lemma abs_abs_sub_abs_le_abs_sub (a b : Ξ±) : abs (abs a - abs b) ≀ abs (a - b) := abs_sub_le_iff.2 ⟨abs_sub_abs_le_abs_sub _ _, by rw abs_sub; apply abs_sub_abs_le_abs_sub⟩ lemma abs_eq (hb : 0 ≀ b) : abs a = b ↔ a = b ∨ a = -b := iff.intro begin cases le_total a 0 with a_nonpos a_nonneg, { rw [abs_of_nonpos a_nonpos, neg_eq_iff_neg_eq, eq_comm], exact or.inr }, { rw [abs_of_nonneg a_nonneg, eq_comm], exact or.inl } end (by intro h; cases h; subst h; try { rw abs_neg }; exact abs_of_nonneg hb) lemma abs_le_max_abs_abs (hab : a ≀ b) (hbc : b ≀ c) : abs b ≀ max (abs a) (abs c) := abs_le'.2 ⟨by simp [hbc.trans (le_abs_self c)], by simp [(neg_le_neg hab).trans (neg_le_abs_self a)]⟩ theorem abs_le_abs (hβ‚€ : a ≀ b) (h₁ : -a ≀ b) : abs a ≀ abs b := (abs_le'.2 ⟨hβ‚€, hβ‚βŸ©).trans (le_abs_self b) lemma abs_max_sub_max_le_abs (a b c : Ξ±) : abs (max a c - max b c) ≀ abs (a - b) := begin simp_rw [abs_le, le_sub_iff_add_le, sub_le_iff_le_add, ← max_add_add_left], split; apply max_le_max; simp only [← le_sub_iff_add_le, ← sub_le_iff_le_add, sub_self, neg_le, neg_le_abs_self, neg_zero, abs_nonneg, le_abs_self] end lemma eq_zero_of_neg_eq {a : Ξ±} (h : -a = a) : a = 0 := match lt_trichotomy a 0 with | or.inl h₁ := have 0 < a, from h β–Έ neg_pos_of_neg h₁, absurd h₁ this.asymm | or.inr (or.inl h₁) := h₁ | or.inr (or.inr h₁) := have a < 0, from h β–Έ neg_neg_of_pos h₁, absurd h₁ this.asymm end lemma eq_of_abs_sub_eq_zero {a b : Ξ±} (h : abs (a - b) = 0) : a = b := sub_eq_zero.1 $ abs_eq_zero.1 h lemma abs_by_cases (P : Ξ± β†’ Prop) {a : Ξ±} (h1 : P a) (h2 : P (-a)) : P (abs a) := sup_ind _ _ h1 h2 lemma abs_sub_le (a b c : Ξ±) : abs (a - c) ≀ abs (a - b) + abs (b - c) := calc abs (a - c) = abs (a - b + (b - c)) : by rw [sub_add_sub_cancel] ... ≀ abs (a - b) + abs (b - c) : abs_add _ _ lemma abs_add_three (a b c : Ξ±) : abs (a + b + c) ≀ abs a + abs b + abs c := (abs_add _ _).trans (add_le_add_right (abs_add _ _) _) lemma dist_bdd_within_interval {a b lb ub : Ξ±} (hal : lb ≀ a) (hau : a ≀ ub) (hbl : lb ≀ b) (hbu : b ≀ ub) : abs (a - b) ≀ ub - lb := abs_sub_le_iff.2 ⟨sub_le_sub hau hbl, sub_le_sub hbu hal⟩ lemma eq_of_abs_sub_nonpos (h : abs (a - b) ≀ 0) : a = b := eq_of_abs_sub_eq_zero (le_antisymm h (abs_nonneg (a - b))) lemma exists_gt_zero [nontrivial Ξ±] : βˆƒ (a:Ξ±), 0 < a := begin obtain ⟨y, hy⟩ := exists_ne (0 : Ξ±), cases hy.lt_or_lt, { exact ⟨- y, neg_pos.mpr h⟩ }, { exact ⟨y, h⟩ } end @[priority 100] -- see Note [lower instance priority] instance linear_ordered_add_comm_group.to_no_top_order [nontrivial Ξ±] : no_top_order Ξ± := ⟨ begin obtain ⟨y, hy⟩ : βˆƒ (a:Ξ±), 0 < a := exists_gt_zero, exact Ξ» a, ⟨a + y, lt_add_of_pos_right a hy⟩ end ⟩ @[priority 100] -- see Note [lower instance priority] instance linear_ordered_add_comm_group.to_no_bot_order [nontrivial Ξ±] : no_bot_order Ξ± := ⟨ begin obtain ⟨y, hy⟩ : βˆƒ (a:Ξ±), 0 < a := exists_gt_zero, exact Ξ» a, ⟨a - y, sub_lt_self a hy⟩ end ⟩ end linear_ordered_add_comm_group /-- This is not so much a new structure as a construction mechanism for ordered groups, by specifying only the "positive cone" of the group. -/ class nonneg_add_comm_group (Ξ± : Type*) extends add_comm_group Ξ± := (nonneg : Ξ± β†’ Prop) (pos : Ξ± β†’ Prop := Ξ» a, nonneg a ∧ Β¬ nonneg (neg a)) (pos_iff : βˆ€ a, pos a ↔ nonneg a ∧ Β¬ nonneg (-a) . order_laws_tac) (zero_nonneg : nonneg 0) (add_nonneg : βˆ€ {a b}, nonneg a β†’ nonneg b β†’ nonneg (a + b)) (nonneg_antisymm : βˆ€ {a}, nonneg a β†’ nonneg (-a) β†’ a = 0) namespace nonneg_add_comm_group variable [s : nonneg_add_comm_group Ξ±] include s @[reducible, priority 100] -- see Note [lower instance priority] instance to_ordered_add_comm_group : ordered_add_comm_group Ξ± := { le := Ξ» a b, nonneg (b - a), lt := Ξ» a b, pos (b - a), lt_iff_le_not_le := Ξ» a b, by simp; rw [pos_iff]; simp, le_refl := Ξ» a, by simp [zero_nonneg], le_trans := Ξ» a b c nab nbc, by simp [-sub_eq_add_neg]; rw ← sub_add_sub_cancel; exact add_nonneg nbc nab, le_antisymm := Ξ» a b nab nba, eq_of_sub_eq_zero $ nonneg_antisymm nba (by rw neg_sub; exact nab), add_le_add_left := Ξ» a b nab c, by simpa [(≀), preorder.le] using nab, ..s } theorem nonneg_def {a : Ξ±} : nonneg a ↔ 0 ≀ a := show _ ↔ nonneg _, by simp theorem pos_def {a : Ξ±} : pos a ↔ 0 < a := show _ ↔ pos _, by simp theorem not_zero_pos : Β¬ pos (0 : Ξ±) := mt pos_def.1 (lt_irrefl _) theorem zero_lt_iff_nonneg_nonneg {a : Ξ±} : 0 < a ↔ nonneg a ∧ Β¬ nonneg (-a) := pos_def.symm.trans (pos_iff _) theorem nonneg_total_iff : (βˆ€ a : Ξ±, nonneg a ∨ nonneg (-a)) ↔ (βˆ€ a b : Ξ±, a ≀ b ∨ b ≀ a) := ⟨λ h a b, by have := h (b - a); rwa [neg_sub] at this, Ξ» h a, by rw [nonneg_def, nonneg_def, neg_nonneg]; apply h⟩ /-- A `nonneg_add_comm_group` is a `linear_ordered_add_comm_group` if `nonneg` is total and decidable. -/ def to_linear_ordered_add_comm_group [decidable_pred (@nonneg Ξ± _)] (nonneg_total : βˆ€ a : Ξ±, nonneg a ∨ nonneg (-a)) : linear_ordered_add_comm_group Ξ± := { le := (≀), lt := (<), le_total := nonneg_total_iff.1 nonneg_total, decidable_le := by apply_instance, decidable_lt := by apply_instance, ..@nonneg_add_comm_group.to_ordered_add_comm_group _ s } end nonneg_add_comm_group namespace order_dual instance [ordered_add_comm_group Ξ±] : ordered_add_comm_group (order_dual Ξ±) := { add_left_neg := Ξ» a : Ξ±, add_left_neg a, sub := Ξ» a b, (a - b : Ξ±), ..order_dual.ordered_add_comm_monoid, ..show add_comm_group Ξ±, by apply_instance } instance [linear_ordered_add_comm_group Ξ±] : linear_ordered_add_comm_group (order_dual Ξ±) := { add_le_add_left := Ξ» a b h c, @add_le_add_left Ξ± _ b a h _, ..order_dual.linear_order Ξ±, ..show add_comm_group Ξ±, by apply_instance } end order_dual namespace prod variables {G H : Type*} @[to_additive] instance [ordered_comm_group G] [ordered_comm_group H] : ordered_comm_group (G Γ— H) := { .. prod.comm_group, .. prod.partial_order G H, .. prod.ordered_cancel_comm_monoid } end prod section type_tags instance [ordered_add_comm_group Ξ±] : ordered_comm_group (multiplicative Ξ±) := { ..multiplicative.comm_group, ..multiplicative.ordered_comm_monoid } instance [ordered_comm_group Ξ±] : ordered_add_comm_group (additive Ξ±) := { ..additive.add_comm_group, ..additive.ordered_add_comm_monoid } end type_tags
76a38c02dad4b93480959a8f40b90f9a253f408f
f3bade00efffc20399e2ac0a6b2e7726e8f9920e
/examples/lean/group.lean
b113cec92d3c47002bf9ff2f553a6be7aed29ec5
[ "Apache-2.0" ]
permissive
leanparikshit/lean
466542d4e183361d28404b3a91412f2df2ead7e2
01eda46b69a87c8fe0eb764328eb1f526a587997
refs/heads/master
1,610,970,145,234
1,400,030,607,000
1,400,030,607,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,388
lean
import macros definition associative {A : (Type U)} (f : A β†’ A β†’ A) := βˆ€ x y z, f (f x y) z = f x (f y z) definition is_identity {A : (Type U)} (f : A β†’ A β†’ A) (id : A) := βˆ€ x, f x id = x definition inverse_ex {A : (Type U)} (f : A β†’ A β†’ A) (id : A) := βˆ€ x, βˆƒ y, f x y = id universe s β‰₯ 1 definition group := sig A : (Type s), sig mul : A β†’ A β†’ A, sig one : A, (associative mul) # (is_identity mul one) # (inverse_ex mul one) definition to_group (A : (Type s)) (mul : A β†’ A β†’ A) (one : A) (H1 : associative mul) (H2 : is_identity mul one) (H3 : inverse_ex mul one) : group := pair A (pair mul (pair one (pair H1 (pair H2 H3)))) -- The following definitions can be generated automatically. definition carrier (g : group) := proj1 g definition G_mul {g : group} : carrier g β†’ carrier g β†’ carrier g := proj1 (proj2 g) infixl 70 * : G_mul definition one {g : group} : carrier g := proj1 (proj2 (proj2 g)) theorem G_assoc {g : group} (x y z : carrier g) : (x * y) * z = x * (y * z) := (proj1 (proj2 (proj2 (proj2 g)))) x y z theorem G_id {g : group} (x : carrier g) : x * one = x := (proj1 (proj2 (proj2 (proj2 (proj2 g))))) x theorem G_inv {g : group} (x : carrier g) : βˆƒ y, x * y = one := (proj2 (proj2 (proj2 (proj2 (proj2 g))))) x -- First example: the pairwise product of two groups is a group definition product (g1 g2 : group) : group := let S := carrier g1 # carrier g2, -- It would be nice to be able to define local notation, and write _*_ instead of f f := Ξ» x y, pair (proj1 x * proj1 y) (proj2 x * proj2 y), o := pair one one -- this is actually (pair (@one g1) (@one g2)) in have assoc : associative f, -- The elaborator failed to infer the type of the pairs. -- I had to annotate the pairs with their types. from take x y z : S, -- We don't really need to provide S, but it will make the elaborator to work much harder -- since * is an overloaded operator, we also have * as notation for Nat::mul in the context. calc f (f x y) z = (pair ((proj1 x * proj1 y) * proj1 z) ((proj2 x * proj2 y) * proj2 z) : S) : refl (f (f x y) z) ... = (pair (proj1 x * (proj1 y * proj1 z)) ((proj2 x * proj2 y) * proj2 z) : S) : { G_assoc (proj1 x) (proj1 y) (proj1 z) } ... = (pair (proj1 x * (proj1 y * proj1 z)) (proj2 x * (proj2 y * proj2 z)) : S) : { G_assoc (proj2 x) (proj2 y) (proj2 z) } ... = f x (f y z) : refl (f x (f y z)), have id : is_identity f o, from take x : S, calc f x o = (pair (proj1 x * one) (proj2 x * one) : S) : refl (f x o) ... = (pair (proj1 x) (proj2 x * one) : S) : { G_id (proj1 x) } ... = (pair (proj1 x) (proj2 x) : S) : { G_id (proj2 x) } ... = x : pair_proj_eq x, have inv : inverse_ex f o, from take x : S, obtain (y1 : carrier g1) (Hy1 : proj1 x * y1 = one), from G_inv (proj1 x), obtain (y2 : carrier g2) (Hy2 : proj2 x * y2 = one), from G_inv (proj2 x), show βˆƒ y, f x y = o, from exists_intro (pair y1 y2 : S) (calc f x (pair y1 y2 : S) = (pair (proj1 x * y1) (proj2 x * y2) : S) : refl (f x (pair y1 y2 : S)) ... = (pair one (proj2 x * y2) : S) : { Hy1 } ... = (pair one one : S) : { Hy2 } ... = o : refl o), to_group S f o assoc id inv -- It would be nice to be able to write x.first and x.second instead of (proj1 x) and (proj2 x) -- Remark: * is overloaded since Lean loads Nat.lean by default. -- The type errors related to * are quite cryptic because of that -- Use 'star' for creating products infixr 50 ⋆ : product -- It would be nice to be able to write (p1 p2 : g1 ⋆ g2 ⋆ g3) check Ξ» (g1 g2 g3 : group) (p1 p2 : carrier (g1 ⋆ g2 ⋆ g3)), p1 * p2 = p2 * p1 theorem group_inhab (g : group) : inhabited (carrier g) := inhabited_intro (@one g) definition inv {g : group} (a : carrier g) : carrier g := Ξ΅ (group_inhab g) (Ξ» x : carrier g, a * x = one) theorem G_idl {g : group} (x : carrier g) : x * one = x := G_id x theorem G_invl {g : group} (x : carrier g) : x * inv x = one := obtain (y : carrier g) (Hy : x * y = one), from G_inv x, eps_ax (group_inhab g) y Hy theorem G_inv_aux {g : group} (x : carrier g) : inv x = (inv x * x) * inv x := symm (calc (inv x * x) * inv x = inv x * (x * inv x) : G_assoc (inv x) x (inv x) ... = inv x * one : { G_invl x } ... = inv x : G_idl (inv x)) theorem G_invr {g : group} (x : carrier g) : inv x * x = one := calc inv x * x = (inv x * x) * one : symm (G_idl (inv x * x)) ... = (inv x * x) * (inv x * inv (inv x)) : { symm (G_invl (inv x)) } ... = ((inv x * x) * inv x) * inv (inv x) : symm (G_assoc (inv x * x) (inv x) (inv (inv x))) ... = (inv x * (x * inv x)) * inv (inv x) : { G_assoc (inv x) x (inv x) } ... = (inv x * one) * inv (inv x) : { G_invl x } ... = (inv x) * inv (inv x) : { G_idl (inv x) } ... = one : G_invl (inv x) theorem G_idr {g : group} (x : carrier g) : one * x = x := calc one * x = (x * inv x) * x : { symm (G_invl x) } ... = x * (inv x * x) : G_assoc x (inv x) x ... = x * one : { G_invr x } ... = x : G_idl x theorem G_inv_inv {g : group} (x : carrier g) : inv (inv x) = x := calc inv (inv x) = inv (inv x) * one : symm (G_idl (inv (inv x))) ... = inv (inv x) * (inv x * x) : { symm (G_invr x) } ... = (inv (inv x) * inv x) * x : symm (G_assoc (inv (inv x)) (inv x) x) ... = one * x : { G_invr (inv x) } ... = x : G_idr x
c3899b0230963ae7177602eb2f51de6b4c6a595b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/filter/extr_auto.lean
98228f25dcfdc3d80f1c2f6023c8e4b55e6b0d51
[]
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
34,002
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.filter.basic import Mathlib.PostPort universes u v w x u_1 u_2 namespace Mathlib /-! # Minimum and maximum w.r.t. a filter and on a aet ## Main Definitions This file defines six predicates of the form `is_A_B`, where `A` is `min`, `max`, or `extr`, and `B` is `filter` or `on`. * `is_min_filter f l a` means that `f a ≀ f x` in some `l`-neighborhood of `a`; * `is_max_filter f l a` means that `f x ≀ f a` in some `l`-neighborhood of `a`; * `is_extr_filter f l a` means `is_min_filter f l a` or `is_max_filter f l a`. Similar predicates with `_on` suffix are particular cases for `l = π“Ÿ s`. ## Main statements ### Change of the filter (set) argument * `is_*_filter.filter_mono` : replace the filter with a smaller one; * `is_*_filter.filter_inf` : replace a filter `l` with `l βŠ“ l'`; * `is_*_on.on_subset` : restrict to a smaller set; * `is_*_on.inter` : replace a set `s` wtih `s ∩ t`. ### Composition * `is_*_*.comp_mono` : if `x` is an extremum for `f` and `g` is a monotone function, then `x` is an extremum for `g ∘ f`; * `is_*_*.comp_antimono` : similarly for the case of monotonically decreasing `g`; * `is_*_*.bicomp_mono` : if `x` is an extremum of the same type for `f` and `g` and a binary operation `op` is monotone in both arguments, then `x` is an extremum of the same type for `Ξ» x, op (f x) (g x)`. * `is_*_filter.comp_tendsto` : if `g x` is an extremum for `f` w.r.t. `l'` and `tendsto g l l'`, then `x` is an extremum for `f ∘ g` w.r.t. `l`. * `is_*_on.on_preimage` : if `g x` is an extremum for `f` on `s`, then `x` is an extremum for `f ∘ g` on `g ⁻¹' s`. ### Algebraic operations * `is_*_*.add` : if `x` is an extremum of the same type for two functions, then it is an extremum of the same type for their sum; * `is_*_*.neg` : if `x` is an extremum for `f`, then it is an extremum of the opposite type for `-f`; * `is_*_*.sub` : if `x` is an a minimum for `f` and a maximum for `g`, then it is a minimum for `f - g` and a maximum for `g - f`; * `is_*_*.max`, `is_*_*.min`, `is_*_*.sup`, `is_*_*.inf` : similarly for `is_*_*.add` for pointwise `max`, `min`, `sup`, `inf`, respectively. ### Miscellaneous definitions * `is_*_*_const` : any point is both a minimum and maximum for a constant function; * `is_min/max_*.is_ext` : any minimum/maximum point is an extremum; * `is_*_*.dual`, `is_*_*.undual`: conversion between codomains `Ξ±` and `dual Ξ±`; ## Missing features (TODO) * Multiplication and division; * `is_*_*.bicompl` : if `x` is a minimum for `f`, `y` is a minimum for `g`, and `op` is a monotone binary operation, then `(x, y)` is a minimum for `uncurry (bicompl op f g)`. From this point of view, `is_*_*.bicomp` is a composition * It would be nice to have a tactic that specializes `comp_(anti)mono` or `bicomp_mono` based on a proof of monotonicity of a given (binary) function. The tactic should maintain a `meta` list of known (anti)monotone (binary) functions with their names, as well as a list of special types of filters, and define the missing lemmas once one of these two lists grows. -/ /-! ### Definitions -/ /-- `is_min_filter f l a` means that `f a ≀ f x` in some `l`-neighborhood of `a` -/ def is_min_filter {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] (f : Ξ± β†’ Ξ²) (l : filter Ξ±) (a : Ξ±) := filter.eventually (fun (x : Ξ±) => f a ≀ f x) l /-- `is_max_filter f l a` means that `f x ≀ f a` in some `l`-neighborhood of `a` -/ def is_max_filter {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] (f : Ξ± β†’ Ξ²) (l : filter Ξ±) (a : Ξ±) := filter.eventually (fun (x : Ξ±) => f x ≀ f a) l /-- `is_extr_filter f l a` means `is_min_filter f l a` or `is_max_filter f l a` -/ def is_extr_filter {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] (f : Ξ± β†’ Ξ²) (l : filter Ξ±) (a : Ξ±) := is_min_filter f l a ∨ is_max_filter f l a /-- `is_min_on f s a` means that `f a ≀ f x` for all `x ∈ a`. Note that we do not assume `a ∈ s`. -/ def is_min_on {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (a : Ξ±) := is_min_filter f (filter.principal s) a /-- `is_max_on f s a` means that `f x ≀ f a` for all `x ∈ a`. Note that we do not assume `a ∈ s`. -/ def is_max_on {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (a : Ξ±) := is_max_filter f (filter.principal s) a /-- `is_extr_on f s a` means `is_min_on f s a` or `is_max_on f s a` -/ def is_extr_on {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] (f : Ξ± β†’ Ξ²) (s : set Ξ±) (a : Ξ±) := is_extr_filter f (filter.principal s) a theorem is_extr_on.elim {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} {p : Prop} : is_extr_on f s a β†’ (is_min_on f s a β†’ p) β†’ (is_max_on f s a β†’ p) β†’ p := or.elim theorem is_min_on_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} : is_min_on f s a ↔ βˆ€ (x : Ξ±), x ∈ s β†’ f a ≀ f x := iff.rfl theorem is_max_on_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} : is_max_on f s a ↔ βˆ€ (x : Ξ±), x ∈ s β†’ f x ≀ f a := iff.rfl theorem is_min_on_univ_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {a : Ξ±} : is_min_on f set.univ a ↔ βˆ€ (x : Ξ±), f a ≀ f x := iff.trans set.univ_subset_iff set.eq_univ_iff_forall theorem is_max_on_univ_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {a : Ξ±} : is_max_on f set.univ a ↔ βˆ€ (x : Ξ±), f x ≀ f a := iff.trans set.univ_subset_iff set.eq_univ_iff_forall /-! ### Conversion to `is_extr_*` -/ theorem is_min_filter.is_extr {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} : is_min_filter f l a β†’ is_extr_filter f l a := Or.inl theorem is_max_filter.is_extr {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} : is_max_filter f l a β†’ is_extr_filter f l a := Or.inr theorem is_min_on.is_extr {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h : is_min_on f s a) : is_extr_on f s a := is_min_filter.is_extr h theorem is_max_on.is_extr {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (h : is_max_on f s a) : is_extr_on f s a := is_max_filter.is_extr h /-! ### Constant function -/ theorem is_min_filter_const {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {l : filter Ξ±} {a : Ξ±} {b : Ξ²} : is_min_filter (fun (_x : Ξ±) => b) l a := filter.univ_mem_sets' fun (_x : Ξ±) => le_refl ((fun (_x : Ξ±) => b) a) theorem is_max_filter_const {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {l : filter Ξ±} {a : Ξ±} {b : Ξ²} : is_max_filter (fun (_x : Ξ±) => b) l a := filter.univ_mem_sets' fun (_x : Ξ±) => le_refl ((fun (_x : Ξ±) => b) _x) theorem is_extr_filter_const {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {l : filter Ξ±} {a : Ξ±} {b : Ξ²} : is_extr_filter (fun (_x : Ξ±) => b) l a := is_min_filter.is_extr is_min_filter_const theorem is_min_on_const {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {s : set Ξ±} {a : Ξ±} {b : Ξ²} : is_min_on (fun (_x : Ξ±) => b) s a := is_min_filter_const theorem is_max_on_const {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {s : set Ξ±} {a : Ξ±} {b : Ξ²} : is_max_on (fun (_x : Ξ±) => b) s a := is_max_filter_const theorem is_extr_on_const {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {s : set Ξ±} {a : Ξ±} {b : Ξ²} : is_extr_on (fun (_x : Ξ±) => b) s a := is_extr_filter_const /-! ### Order dual -/ theorem is_min_filter_dual_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} : is_min_filter f l a ↔ is_max_filter f l a := iff.rfl theorem is_max_filter_dual_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} : is_max_filter f l a ↔ is_min_filter f l a := iff.rfl theorem is_extr_filter_dual_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} : is_extr_filter f l a ↔ is_extr_filter f l a := or_comm (is_min_filter f l a) (is_max_filter f l a) theorem is_max_filter.dual {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} : is_max_filter f l a β†’ is_min_filter f l a := iff.mpr is_min_filter_dual_iff theorem is_max_filter.undual {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} : is_max_filter f l a β†’ is_min_filter f l a := iff.mp is_max_filter_dual_iff theorem is_extr_filter.dual {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} : is_extr_filter f l a β†’ is_extr_filter f l a := iff.mpr is_extr_filter_dual_iff theorem is_min_on_dual_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} : is_min_on f s a ↔ is_max_on f s a := iff.rfl theorem is_max_on_dual_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} : is_max_on f s a ↔ is_min_on f s a := iff.rfl theorem is_extr_on_dual_iff {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} : is_extr_on f s a ↔ is_extr_on f s a := or_comm (is_min_filter f (filter.principal s) a) (is_max_filter f (filter.principal s) a) theorem is_max_on.dual {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} : is_max_on f s a β†’ is_min_on f s a := iff.mpr is_min_on_dual_iff theorem is_min_on.dual {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} : is_min_on f s a β†’ is_max_on f s a := iff.mpr is_max_on_dual_iff theorem is_extr_on.dual {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} : is_extr_on f s a β†’ is_extr_on f s a := iff.mpr is_extr_on_dual_iff /-! ### Operations on the filter/set -/ theorem is_min_filter.filter_mono {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} {l' : filter Ξ±} (h : is_min_filter f l a) (hl : l' ≀ l) : is_min_filter f l' a := hl h theorem is_max_filter.filter_mono {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} {l' : filter Ξ±} (h : is_max_filter f l a) (hl : l' ≀ l) : is_max_filter f l' a := hl h theorem is_extr_filter.filter_mono {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} {l' : filter Ξ±} (h : is_extr_filter f l a) (hl : l' ≀ l) : is_extr_filter f l' a := or.elim h (fun (h : is_min_filter f l a) => is_min_filter.is_extr (is_min_filter.filter_mono h hl)) fun (h : is_max_filter f l a) => is_max_filter.is_extr (is_max_filter.filter_mono h hl) theorem is_min_filter.filter_inf {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} (h : is_min_filter f l a) (l' : filter Ξ±) : is_min_filter f (l βŠ“ l') a := is_min_filter.filter_mono h inf_le_left theorem is_max_filter.filter_inf {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} (h : is_max_filter f l a) (l' : filter Ξ±) : is_max_filter f (l βŠ“ l') a := is_max_filter.filter_mono h inf_le_left theorem is_extr_filter.filter_inf {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} (h : is_extr_filter f l a) (l' : filter Ξ±) : is_extr_filter f (l βŠ“ l') a := is_extr_filter.filter_mono h inf_le_left theorem is_min_on.on_subset {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} {t : set Ξ±} (hf : is_min_on f t a) (h : s βŠ† t) : is_min_on f s a := is_min_filter.filter_mono hf (iff.mpr filter.principal_mono h) theorem is_max_on.on_subset {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} {t : set Ξ±} (hf : is_max_on f t a) (h : s βŠ† t) : is_max_on f s a := is_max_filter.filter_mono hf (iff.mpr filter.principal_mono h) theorem is_extr_on.on_subset {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} {t : set Ξ±} (hf : is_extr_on f t a) (h : s βŠ† t) : is_extr_on f s a := is_extr_filter.filter_mono hf (iff.mpr filter.principal_mono h) theorem is_min_on.inter {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : is_min_on f s a) (t : set Ξ±) : is_min_on f (s ∩ t) a := is_min_on.on_subset hf (set.inter_subset_left s t) theorem is_max_on.inter {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : is_max_on f s a) (t : set Ξ±) : is_max_on f (s ∩ t) a := is_max_on.on_subset hf (set.inter_subset_left s t) theorem is_extr_on.inter {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : is_extr_on f s a) (t : set Ξ±) : is_extr_on f (s ∩ t) a := is_extr_on.on_subset hf (set.inter_subset_left s t) /-! ### Composition with (anti)monotone functions -/ theorem is_min_filter.comp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} (hf : is_min_filter f l a) {g : Ξ² β†’ Ξ³} (hg : monotone g) : is_min_filter (g ∘ f) l a := filter.mem_sets_of_superset hf fun (x : Ξ±) (hx : x ∈ set_of fun (x : Ξ±) => (fun (x : Ξ±) => f a ≀ f x) x) => hg hx theorem is_max_filter.comp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} (hf : is_max_filter f l a) {g : Ξ² β†’ Ξ³} (hg : monotone g) : is_max_filter (g ∘ f) l a := filter.mem_sets_of_superset hf fun (x : Ξ±) (hx : x ∈ set_of fun (x : Ξ±) => (fun (x : Ξ±) => f x ≀ f a) x) => hg hx theorem is_extr_filter.comp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} (hf : is_extr_filter f l a) {g : Ξ² β†’ Ξ³} (hg : monotone g) : is_extr_filter (g ∘ f) l a := or.elim hf (fun (hf : is_min_filter f l a) => is_min_filter.is_extr (is_min_filter.comp_mono hf hg)) fun (hf : is_max_filter f l a) => is_max_filter.is_extr (is_max_filter.comp_mono hf hg) theorem is_min_filter.comp_antimono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} (hf : is_min_filter f l a) {g : Ξ² β†’ Ξ³} (hg : βˆ€ {x y : Ξ²}, x ≀ y β†’ g y ≀ g x) : is_max_filter (g ∘ f) l a := is_max_filter.comp_mono (is_min_filter.dual hf) fun (x y : order_dual Ξ²) (h : x ≀ y) => hg h theorem is_max_filter.comp_antimono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} (hf : is_max_filter f l a) {g : Ξ² β†’ Ξ³} (hg : βˆ€ {x y : Ξ²}, x ≀ y β†’ g y ≀ g x) : is_min_filter (g ∘ f) l a := is_min_filter.comp_mono (is_max_filter.dual hf) fun (x y : order_dual Ξ²) (h : x ≀ y) => hg h theorem is_extr_filter.comp_antimono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} (hf : is_extr_filter f l a) {g : Ξ² β†’ Ξ³} (hg : βˆ€ {x y : Ξ²}, x ≀ y β†’ g y ≀ g x) : is_extr_filter (g ∘ f) l a := is_extr_filter.comp_mono (is_extr_filter.dual hf) fun (x y : order_dual Ξ²) (h : x ≀ y) => hg h theorem is_min_on.comp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : is_min_on f s a) {g : Ξ² β†’ Ξ³} (hg : monotone g) : is_min_on (g ∘ f) s a := is_min_filter.comp_mono hf hg theorem is_max_on.comp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : is_max_on f s a) {g : Ξ² β†’ Ξ³} (hg : monotone g) : is_max_on (g ∘ f) s a := is_max_filter.comp_mono hf hg theorem is_extr_on.comp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : is_extr_on f s a) {g : Ξ² β†’ Ξ³} (hg : monotone g) : is_extr_on (g ∘ f) s a := is_extr_filter.comp_mono hf hg theorem is_min_on.comp_antimono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : is_min_on f s a) {g : Ξ² β†’ Ξ³} (hg : βˆ€ {x y : Ξ²}, x ≀ y β†’ g y ≀ g x) : is_max_on (g ∘ f) s a := is_min_filter.comp_antimono hf hg theorem is_max_on.comp_antimono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : is_max_on f s a) {g : Ξ² β†’ Ξ³} (hg : βˆ€ {x y : Ξ²}, x ≀ y β†’ g y ≀ g x) : is_min_on (g ∘ f) s a := is_max_filter.comp_antimono hf hg theorem is_extr_on.comp_antimono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} (hf : is_extr_on f s a) {g : Ξ² β†’ Ξ³} (hg : βˆ€ {x y : Ξ²}, x ≀ y β†’ g y ≀ g x) : is_extr_on (g ∘ f) s a := is_extr_filter.comp_antimono hf hg theorem is_min_filter.bicomp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} [preorder Ξ΄] {op : Ξ² β†’ Ξ³ β†’ Ξ΄} (hop : relator.lift_fun LessEq (LessEq β‡’ LessEq) op op) (hf : is_min_filter f l a) {g : Ξ± β†’ Ξ³} (hg : is_min_filter g l a) : is_min_filter (fun (x : Ξ±) => op (f x) (g x)) l a := sorry theorem is_max_filter.bicomp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {a : Ξ±} [preorder Ξ΄] {op : Ξ² β†’ Ξ³ β†’ Ξ΄} (hop : relator.lift_fun LessEq (LessEq β‡’ LessEq) op op) (hf : is_max_filter f l a) {g : Ξ± β†’ Ξ³} (hg : is_max_filter g l a) : is_max_filter (fun (x : Ξ±) => op (f x) (g x)) l a := sorry -- No `extr` version because we need `hf` and `hg` to be of the same kind theorem is_min_on.bicomp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} [preorder Ξ΄] {op : Ξ² β†’ Ξ³ β†’ Ξ΄} (hop : relator.lift_fun LessEq (LessEq β‡’ LessEq) op op) (hf : is_min_on f s a) {g : Ξ± β†’ Ξ³} (hg : is_min_on g s a) : is_min_on (fun (x : Ξ±) => op (f x) (g x)) s a := is_min_filter.bicomp_mono hop hf hg theorem is_max_on.bicomp_mono {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} [preorder Ξ²] [preorder Ξ³] {f : Ξ± β†’ Ξ²} {s : set Ξ±} {a : Ξ±} [preorder Ξ΄] {op : Ξ² β†’ Ξ³ β†’ Ξ΄} (hop : relator.lift_fun LessEq (LessEq β‡’ LessEq) op op) (hf : is_max_on f s a) {g : Ξ± β†’ Ξ³} (hg : is_max_on g s a) : is_max_on (fun (x : Ξ±) => op (f x) (g x)) s a := is_max_filter.bicomp_mono hop hf hg /-! ### Composition with `tendsto` -/ theorem is_min_filter.comp_tendsto {Ξ± : Type u} {Ξ² : Type v} {Ξ΄ : Type x} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {g : Ξ΄ β†’ Ξ±} {l' : filter Ξ΄} {b : Ξ΄} (hf : is_min_filter f l (g b)) (hg : filter.tendsto g l' l) : is_min_filter (f ∘ g) l' b := hg hf theorem is_max_filter.comp_tendsto {Ξ± : Type u} {Ξ² : Type v} {Ξ΄ : Type x} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {g : Ξ΄ β†’ Ξ±} {l' : filter Ξ΄} {b : Ξ΄} (hf : is_max_filter f l (g b)) (hg : filter.tendsto g l' l) : is_max_filter (f ∘ g) l' b := hg hf theorem is_extr_filter.comp_tendsto {Ξ± : Type u} {Ξ² : Type v} {Ξ΄ : Type x} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {l : filter Ξ±} {g : Ξ΄ β†’ Ξ±} {l' : filter Ξ΄} {b : Ξ΄} (hf : is_extr_filter f l (g b)) (hg : filter.tendsto g l' l) : is_extr_filter (f ∘ g) l' b := or.elim hf (fun (hf : is_min_filter f l (g b)) => is_min_filter.is_extr (is_min_filter.comp_tendsto hf hg)) fun (hf : is_max_filter f l (g b)) => is_max_filter.is_extr (is_max_filter.comp_tendsto hf hg) theorem is_min_on.on_preimage {Ξ± : Type u} {Ξ² : Type v} {Ξ΄ : Type x} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} (g : Ξ΄ β†’ Ξ±) {b : Ξ΄} (hf : is_min_on f s (g b)) : is_min_on (f ∘ g) (g ⁻¹' s) b := is_min_filter.comp_tendsto hf (iff.mpr filter.tendsto_principal_principal (set.subset.refl (g ⁻¹' s))) theorem is_max_on.on_preimage {Ξ± : Type u} {Ξ² : Type v} {Ξ΄ : Type x} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} (g : Ξ΄ β†’ Ξ±) {b : Ξ΄} (hf : is_max_on f s (g b)) : is_max_on (f ∘ g) (g ⁻¹' s) b := is_max_filter.comp_tendsto hf (iff.mpr filter.tendsto_principal_principal (set.subset.refl (g ⁻¹' s))) theorem is_extr_on.on_preimage {Ξ± : Type u} {Ξ² : Type v} {Ξ΄ : Type x} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {s : set Ξ±} (g : Ξ΄ β†’ Ξ±) {b : Ξ΄} (hf : is_extr_on f s (g b)) : is_extr_on (f ∘ g) (g ⁻¹' s) b := is_extr_on.elim hf (fun (hf : is_min_on f s (g b)) => is_min_on.is_extr (is_min_on.on_preimage g hf)) fun (hf : is_max_on f s (g b)) => is_max_on.is_extr (is_max_on.on_preimage g hf) /-! ### Pointwise addition -/ theorem is_min_filter.add {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_monoid Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (fun (x : Ξ±) => f x + g x) l a := (fun (this : is_min_filter (fun (x : Ξ±) => f x + g x) l a) => this) (is_min_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => add_le_add hx hy) hf hg) theorem is_max_filter.add {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_monoid Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (fun (x : Ξ±) => f x + g x) l a := (fun (this : is_max_filter (fun (x : Ξ±) => f x + g x) l a) => this) (is_max_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => add_le_add hx hy) hf hg) theorem is_min_on.add {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_monoid Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (fun (x : Ξ±) => f x + g x) s a := is_min_filter.add hf hg theorem is_max_on.add {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_monoid Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (fun (x : Ξ±) => f x + g x) s a := is_max_filter.add hf hg /-! ### Pointwise negation and subtraction -/ theorem is_min_filter.neg {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_min_filter f l a) : is_max_filter (fun (x : Ξ±) => -f x) l a := is_min_filter.comp_antimono hf fun (x y : Ξ²) (hx : x ≀ y) => neg_le_neg hx theorem is_max_filter.neg {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_max_filter f l a) : is_min_filter (fun (x : Ξ±) => -f x) l a := is_max_filter.comp_antimono hf fun (x y : Ξ²) (hx : x ≀ y) => neg_le_neg hx theorem is_extr_filter.neg {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_extr_filter f l a) : is_extr_filter (fun (x : Ξ±) => -f x) l a := or.elim hf (fun (hf : is_min_filter f l a) => is_max_filter.is_extr (is_min_filter.neg hf)) fun (hf : is_max_filter f l a) => is_min_filter.is_extr (is_max_filter.neg hf) theorem is_min_on.neg {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_min_on f s a) : is_max_on (fun (x : Ξ±) => -f x) s a := is_min_on.comp_antimono hf fun (x y : Ξ²) (hx : x ≀ y) => neg_le_neg hx theorem is_max_on.neg {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_max_on f s a) : is_min_on (fun (x : Ξ±) => -f x) s a := is_max_on.comp_antimono hf fun (x y : Ξ²) (hx : x ≀ y) => neg_le_neg hx theorem is_extr_on.neg {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_extr_on f s a) : is_extr_on (fun (x : Ξ±) => -f x) s a := is_extr_on.elim hf (fun (hf : is_min_on f s a) => is_max_on.is_extr (is_min_on.neg hf)) fun (hf : is_max_on f s a) => is_min_on.is_extr (is_max_on.neg hf) theorem is_min_filter.sub {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_min_filter f l a) (hg : is_max_filter g l a) : is_min_filter (fun (x : Ξ±) => f x - g x) l a := sorry theorem is_max_filter.sub {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_max_filter f l a) (hg : is_min_filter g l a) : is_max_filter (fun (x : Ξ±) => f x - g x) l a := sorry theorem is_min_on.sub {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_min_on f s a) (hg : is_max_on g s a) : is_min_on (fun (x : Ξ±) => f x - g x) s a := sorry theorem is_max_on.sub {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_max_on f s a) (hg : is_min_on g s a) : is_max_on (fun (x : Ξ±) => f x - g x) s a := sorry /-! ### Pointwise `sup`/`inf` -/ theorem is_min_filter.sup {Ξ± : Type u} {Ξ² : Type v} [semilattice_sup Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (fun (x : Ξ±) => f x βŠ” g x) l a := (fun (this : is_min_filter (fun (x : Ξ±) => f x βŠ” g x) l a) => this) (is_min_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => sup_le_sup hx hy) hf hg) theorem is_max_filter.sup {Ξ± : Type u} {Ξ² : Type v} [semilattice_sup Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (fun (x : Ξ±) => f x βŠ” g x) l a := (fun (this : is_max_filter (fun (x : Ξ±) => f x βŠ” g x) l a) => this) (is_max_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => sup_le_sup hx hy) hf hg) theorem is_min_on.sup {Ξ± : Type u} {Ξ² : Type v} [semilattice_sup Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (fun (x : Ξ±) => f x βŠ” g x) s a := is_min_filter.sup hf hg theorem is_max_on.sup {Ξ± : Type u} {Ξ² : Type v} [semilattice_sup Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (fun (x : Ξ±) => f x βŠ” g x) s a := is_max_filter.sup hf hg theorem is_min_filter.inf {Ξ± : Type u} {Ξ² : Type v} [semilattice_inf Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (fun (x : Ξ±) => f x βŠ“ g x) l a := (fun (this : is_min_filter (fun (x : Ξ±) => f x βŠ“ g x) l a) => this) (is_min_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => inf_le_inf hx hy) hf hg) theorem is_max_filter.inf {Ξ± : Type u} {Ξ² : Type v} [semilattice_inf Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (fun (x : Ξ±) => f x βŠ“ g x) l a := (fun (this : is_max_filter (fun (x : Ξ±) => f x βŠ“ g x) l a) => this) (is_max_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => inf_le_inf hx hy) hf hg) theorem is_min_on.inf {Ξ± : Type u} {Ξ² : Type v} [semilattice_inf Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (fun (x : Ξ±) => f x βŠ“ g x) s a := is_min_filter.inf hf hg theorem is_max_on.inf {Ξ± : Type u} {Ξ² : Type v} [semilattice_inf Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (fun (x : Ξ±) => f x βŠ“ g x) s a := is_max_filter.inf hf hg /-! ### Pointwise `min`/`max` -/ theorem is_min_filter.min {Ξ± : Type u} {Ξ² : Type v} [linear_order Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (fun (x : Ξ±) => min (f x) (g x)) l a := (fun (this : is_min_filter (fun (x : Ξ±) => min (f x) (g x)) l a) => this) (is_min_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => min_le_min hx hy) hf hg) theorem is_max_filter.min {Ξ± : Type u} {Ξ² : Type v} [linear_order Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (fun (x : Ξ±) => min (f x) (g x)) l a := (fun (this : is_max_filter (fun (x : Ξ±) => min (f x) (g x)) l a) => this) (is_max_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => min_le_min hx hy) hf hg) theorem is_min_on.min {Ξ± : Type u} {Ξ² : Type v} [linear_order Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (fun (x : Ξ±) => min (f x) (g x)) s a := is_min_filter.min hf hg theorem is_max_on.min {Ξ± : Type u} {Ξ² : Type v} [linear_order Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (fun (x : Ξ±) => min (f x) (g x)) s a := is_max_filter.min hf hg theorem is_min_filter.max {Ξ± : Type u} {Ξ² : Type v} [linear_order Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (fun (x : Ξ±) => max (f x) (g x)) l a := (fun (this : is_min_filter (fun (x : Ξ±) => max (f x) (g x)) l a) => this) (is_min_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => max_le_max hx hy) hf hg) theorem is_max_filter.max {Ξ± : Type u} {Ξ² : Type v} [linear_order Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (fun (x : Ξ±) => max (f x) (g x)) l a := (fun (this : is_max_filter (fun (x : Ξ±) => max (f x) (g x)) l a) => this) (is_max_filter.bicomp_mono (fun (x x' : Ξ²) (hx : x ≀ x') (y y' : Ξ²) (hy : y ≀ y') => max_le_max hx hy) hf hg) theorem is_min_on.max {Ξ± : Type u} {Ξ² : Type v} [linear_order Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (fun (x : Ξ±) => max (f x) (g x)) s a := is_min_filter.max hf hg theorem is_max_on.max {Ξ± : Type u} {Ξ² : Type v} [linear_order Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (fun (x : Ξ±) => max (f x) (g x)) s a := is_max_filter.max hf hg /-! ### Relation with `eventually` comparisons of two functions -/ theorem filter.eventually_le.is_max_filter {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hle : filter.eventually_le l g f) (hfga : f a = g a) (h : is_max_filter f l a) : is_max_filter g l a := sorry theorem is_max_filter.congr {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (h : is_max_filter f l a) (heq : filter.eventually_eq l f g) (hfga : f a = g a) : is_max_filter g l a := filter.eventually_le.is_max_filter (filter.eventually_eq.le (filter.eventually_eq.symm heq)) hfga h theorem filter.eventually_eq.is_max_filter_iff {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (heq : filter.eventually_eq l f g) (hfga : f a = g a) : is_max_filter f l a ↔ is_max_filter g l a := { mp := fun (h : is_max_filter f l a) => is_max_filter.congr h heq hfga, mpr := fun (h : is_max_filter g l a) => is_max_filter.congr h (filter.eventually_eq.symm heq) (Eq.symm hfga) } theorem filter.eventually_le.is_min_filter {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (hle : filter.eventually_le l f g) (hfga : f a = g a) (h : is_min_filter f l a) : is_min_filter g l a := filter.eventually_le.is_max_filter hle hfga h theorem is_min_filter.congr {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (h : is_min_filter f l a) (heq : filter.eventually_eq l f g) (hfga : f a = g a) : is_min_filter g l a := filter.eventually_le.is_min_filter (filter.eventually_eq.le heq) hfga h theorem filter.eventually_eq.is_min_filter_iff {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (heq : filter.eventually_eq l f g) (hfga : f a = g a) : is_min_filter f l a ↔ is_min_filter g l a := { mp := fun (h : is_min_filter f l a) => is_min_filter.congr h heq hfga, mpr := fun (h : is_min_filter g l a) => is_min_filter.congr h (filter.eventually_eq.symm heq) (Eq.symm hfga) } theorem is_extr_filter.congr {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (h : is_extr_filter f l a) (heq : filter.eventually_eq l f g) (hfga : f a = g a) : is_extr_filter g l a := sorry theorem filter.eventually_eq.is_extr_filter_iff {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ²] {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {a : Ξ±} {l : filter Ξ±} (heq : filter.eventually_eq l f g) (hfga : f a = g a) : is_extr_filter f l a ↔ is_extr_filter g l a := { mp := fun (h : is_extr_filter f l a) => is_extr_filter.congr h heq hfga, mpr := fun (h : is_extr_filter g l a) => is_extr_filter.congr h (filter.eventually_eq.symm heq) (Eq.symm hfga) } end Mathlib
422652ac6d44621079b52e68cb3bfc23f658adc9
d0f9af2b0ace5ce352570d61b09019c8ef4a3b96
/hw6_quiz/propositional.lean
c326486e8011791129204ee8930a5d973a93fde3
[]
no_license
jngo13/Discrete-Mathematics
8671540ef2da7c75915d32332dd20c02f001474e
bf674a866e61f60e6e6d128df85fa73819091787
refs/heads/master
1,675,615,657,924
1,609,142,011,000
1,609,142,011,000
267,190,341
0
0
null
null
null
null
UTF-8
Lean
false
false
4,339
lean
/- Propositional logic. Propositions. P: Kevin Is at Home. Q: It's warm outside. If P and Q are propositions, then so are - not P - P and Q - P or Q - P implies Q - etc. -/ /- Syntax -- defines set of well formed expressions. We formalize syntax as an inductive data type. Expressions are then just values of this type. Here we formalize the syntax of propositional logic except for variable expressions. That will come next class. -/ -- 3/24/20 Lecture Notes /-inductive var : Type | P | Q | R open var -- End of lecture note edit-/ inductive var : Type | mk : β„• β†’ var def V_0 := var.mk 0 def V_1 := var.mk 1 def V_2 := var.mk 2 -- variables indexed by natural numbers inductive pExp : Type | pTrue : pExp | pFalse : pExp | pNot : pExp β†’ pExp | pAnd : pExp β†’ pExp β†’ pExp | pOr : pExp β†’ pExp β†’ pExp | pImp : pExp β†’ pExp β†’ pExp | pFol : pExp β†’ pExp β†’ pExp | pVar : var β†’ pExp | pIff : pExp β†’ pExp β†’ pExp open pExp /- Semantics -- what does each such expression mean? The semantics of propositional logic assigns a Boolean truth value to each well formed expression. Moreover, it does this in a "compositional" manner. What that means is that the meaning of a "larger" expression is defined by "composing"", using Boolean operators, the meaning(s) of the smaller expression(s) from which it is constructed. -/ /- Recall how we defined a few Boolean operators. -/ -- and def band' : bool β†’ bool β†’ bool | tt tt := tt | tt ff := ff | ff tt := ff | ff ff := ff -- or def bor' : bool β†’ bool β†’ bool | tt tt := tt | tt ff := tt | ff tt := tt | ff ff := ff -- implies def bimp : bool β†’ bool β†’ bool | tt tt := tt | tt ff := ff | ff tt := tt | ff ff := tt -- follows def bfol : bool β†’ bool β†’ bool | tt tt := tt | tt ff := tt | ff tt := ff | ff ff := ff -- if and only if (iff) def biff : bool β†’ bool β†’ bool | tt tt := tt | ff ff := tt | _ _ := ff /- An interpretation is a mapping i.e, a function, from variables to a boolean "meanings" (values) -/ def interp_all_false: var β†’ bool | _ := ff /- We formalize the "assignment" of a bool meaning (value) to each well formed expression as a *function* from expressions (values of type pExp) to bool. Here are the rules. -/ def pEval : pExp β†’ (var β†’ bool) β†’ bool | pTrue _ := tt | pFalse _ := ff /- | (pVar P) := tt | (pVar Q) := ff | (pVar R) := tt -/ |(pVar v) i := i v -- what variable v means comes from the interpretation | (pNot e) i := bnot (pEval e i) | (pAnd e1 e2) i := band (pEval e1 i) (pEval e2 i) | (pOr e1 e2) i := bor (pEval e1 i) (pEval e2 i) | (pImp e1 e2) i := bimp (pEval e1 i) (pEval e2 i) | (pFol e1 e2) i := bfol (pEval e1 i) (pEval e2 i) | (pIff e1 e2) i := biff (pEval e1 i) (pEval e2 i) -- P ↔ Q (if and only if \iff) -- tt tt tt -- ff ff tt -- _ _ ff /- Examples -/ #eval pEval pTrue #eval pEval pFalse #eval pEval (pNot pTrue) #eval pEval (pNot pFalse) def p1 := pTrue def p2 := pFalse def p25 := pNot p2 def p3 := pAnd pTrue pFalse def p4 := pOr p3 p2 #eval pEval p3 #eval pEval p4 #eval pEval (pImp p3 p4) -- Variable expressions def P := V_0 def Q := V_1 def R := V_2 def p5 := pVar P def p6 := pOr (pVar P) (pVar Q) def p7 := pAnd (pOr (pVar P) (pVar Q)) (pNot (pVar Q)) -- (P ∨ Q) ∧ (-Q) #eval pEval p7 interp_all_false -- needs interpretation as pEval p7 def interp_makes_p7_true : var β†’ bool | (var.mk 0) := tt -- basically variable P | (var.mk 1) := ff -- basically variable Q | (var.mk 2) := ff -- basically variable R | (var.mk _) := ff -- for everything else #eval pEval p7 interp_makes_p7_true -- Boolean satisfiability -- Satisfiable: has some interpretation that makes it true -- interp_makes_p7_true is satisfiable but not valid -- Unsatisfiable -- pFalse -- P ∧ Β¬ P -- Valid : under every interpretation it's true -- pTrue -- P ∨ Β¬ P -- Satisfiable but not valid (not every) -- P -- interpretation but not valid /-Problem of Boolean satisfiability: Given a proposition, P, in propositional logic, or a Boolean expression, *find* an interpretation under which that expression is true. -/ /- Example: (Q ∨ R) ∧ (Β¬ R ∧ Β¬ Q) -- Not satisfiable (P ∧ Q) β†’ (Q ∧ P) -- VALID IMPLICATION (no matter P and Q) Β¬(P ∧ Q) β†’ (Β¬ P) ∨ (Β¬ Q) -- DeMorgan Laws Β¬ (P ∨ Q) β†’ (Β¬ P) ∧ (Β¬ Q) -/
9e46019f022226d847369a8f1721a1fe394ce702
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/order_iso_nat_auto.lean
b8c3ac437f8e50ba64e3b143b400f48bedea92a7
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,134
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.basic import Mathlib.order.rel_iso import Mathlib.logic.function.iterate import Mathlib.PostPort universes u_1 namespace Mathlib namespace rel_embedding /-- If `f` is a strictly `r`-increasing sequence, then this returns `f` as an order embedding. -/ def nat_lt {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} [is_strict_order Ξ± r] (f : β„• β†’ Ξ±) (H : βˆ€ (n : β„•), r (f n) (f (n + 1))) : Less β†ͺr r := of_monotone f sorry /-- If `f` is a strictly `r`-decreasing sequence, then this returns `f` as an order embedding. -/ def nat_gt {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} [is_strict_order Ξ± r] (f : β„• β†’ Ξ±) (H : βˆ€ (n : β„•), r (f (n + 1)) (f n)) : gt β†ͺr r := rel_embedding.swap (nat_lt f H) theorem well_founded_iff_no_descending_seq {Ξ± : Type u_1} {r : Ξ± β†’ Ξ± β†’ Prop} [is_strict_order Ξ± r] : well_founded r ↔ Β¬Nonempty (gt β†ͺr r) := sorry end Mathlib
2f7eab9be02b231ea3e7235f2c254036e9fb2c06
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/convex/strict_convex_space.lean
293808bc66e2e7ca022d94358ff244be4e71d58a
[ "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
14,918
lean
/- Copyright (c) 2022 YaΓ«l Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: YaΓ«l Dillies, Yury Kudryashov -/ import analysis.convex.strict import analysis.convex.topology import analysis.normed_space.ordered import analysis.normed_space.pointwise /-! # Strictly convex spaces This file defines strictly convex spaces. A normed space is strictly convex if all closed balls are strictly convex. This does **not** mean that the norm is strictly convex (in fact, it never is). ## Main definitions `strict_convex_space`: a typeclass saying that a given normed space over a normed linear ordered field (e.g., `ℝ` or `β„š`) is strictly convex. The definition requires strict convexity of a closed ball of positive radius with center at the origin; strict convexity of any other closed ball follows from this assumption. ## Main results In a strictly convex space, we prove - `strict_convex_closed_ball`: a closed ball is strictly convex. - `combo_mem_ball_of_ne`, `open_segment_subset_ball_of_ne`, `norm_combo_lt_of_ne`: a nontrivial convex combination of two points in a closed ball belong to the corresponding open ball; - `norm_add_lt_of_not_same_ray`, `same_ray_iff_norm_add`, `dist_add_dist_eq_iff`: the triangle inequality `dist x y + dist y z ≀ dist x z` is a strict inequality unless `y` belongs to the segment `[x -[ℝ] z]`. - `isometry.affine_isometry_of_strict_convex_space`: an isometry of `normed_add_torsor`s for real normed spaces, strictly convex in the case of the codomain, is an affine isometry. We also provide several lemmas that can be used as alternative constructors for `strict_convex ℝ E`: - `strict_convex_space.of_strict_convex_closed_unit_ball`: if `closed_ball (0 : E) 1` is strictly convex, then `E` is a strictly convex space; - `strict_convex_space.of_norm_add`: if `βˆ₯x + yβˆ₯ = βˆ₯xβˆ₯ + βˆ₯yβˆ₯` implies `same_ray ℝ x y` for all `x y : E`, then `E` is a strictly convex space. ## Implementation notes While the definition is formulated for any normed linear ordered field, most of the lemmas are formulated only for the case `π•œ = ℝ`. ## Tags convex, strictly convex -/ open set metric open_locale convex pointwise /-- A *strictly convex space* is a normed space where the closed balls are strictly convex. We only require balls of positive radius with center at the origin to be strictly convex in the definition, then prove that any closed ball is strictly convex in `strict_convex_closed_ball` below. See also `strict_convex_space.of_strict_convex_closed_unit_ball`. -/ class strict_convex_space (π•œ E : Type*) [normed_linear_ordered_field π•œ] [normed_group E] [normed_space π•œ E] : Prop := (strict_convex_closed_ball : βˆ€ r : ℝ, 0 < r β†’ strict_convex π•œ (closed_ball (0 : E) r)) variables (π•œ : Type*) {E : Type*} [normed_linear_ordered_field π•œ] [normed_group E] [normed_space π•œ E] /-- A closed ball in a strictly convex space is strictly convex. -/ lemma strict_convex_closed_ball [strict_convex_space π•œ E] (x : E) (r : ℝ) : strict_convex π•œ (closed_ball x r) := begin cases le_or_lt r 0 with hr hr, { exact (subsingleton_closed_ball x hr).strict_convex }, rw ← vadd_closed_ball_zero, exact (strict_convex_space.strict_convex_closed_ball r hr).vadd _, end variables [normed_space ℝ E] /-- A real normed vector space is strictly convex provided that the unit ball is strictly convex. -/ lemma strict_convex_space.of_strict_convex_closed_unit_ball [linear_map.compatible_smul E E π•œ ℝ] (h : strict_convex π•œ (closed_ball (0 : E) 1)) : strict_convex_space π•œ E := ⟨λ r hr, by simpa only [smul_closed_unit_ball_of_nonneg hr.le] using h.smul r⟩ /-- If `βˆ₯x + yβˆ₯ = βˆ₯xβˆ₯ + βˆ₯yβˆ₯` implies that `x y : E` are in the same ray, then `E` is a strictly convex space. -/ lemma strict_convex_space.of_norm_add (h : βˆ€ x y : E, βˆ₯x + yβˆ₯ = βˆ₯xβˆ₯ + βˆ₯yβˆ₯ β†’ same_ray ℝ x y) : strict_convex_space ℝ E := begin refine strict_convex_space.of_strict_convex_closed_unit_ball ℝ (Ξ» x hx y hy hne a b ha hb hab, _), have hx' := hx, have hy' := hy, rw [← closure_closed_ball, closure_eq_interior_union_frontier, frontier_closed_ball (0 : E) one_ne_zero] at hx hy, cases hx, { exact (convex_closed_ball _ _).combo_interior_self_mem_interior hx hy' ha hb.le hab }, cases hy, { exact (convex_closed_ball _ _).combo_self_interior_mem_interior hx' hy ha.le hb hab }, rw [interior_closed_ball (0 : E) one_ne_zero, mem_ball_zero_iff], have hx₁ : βˆ₯xβˆ₯ = 1, from mem_sphere_zero_iff_norm.1 hx, have hy₁ : βˆ₯yβˆ₯ = 1, from mem_sphere_zero_iff_norm.1 hy, have ha' : βˆ₯aβˆ₯ = a, from real.norm_of_nonneg ha.le, have hb' : βˆ₯bβˆ₯ = b, from real.norm_of_nonneg hb.le, calc βˆ₯a β€’ x + b β€’ yβˆ₯ < βˆ₯a β€’ xβˆ₯ + βˆ₯b β€’ yβˆ₯ : (norm_add_le _ _).lt_of_ne (Ξ» H, hne _) ... = 1 : by simpa only [norm_smul, hx₁, hy₁, mul_one, ha', hb'], simpa only [norm_smul, hx₁, hy₁, ha', hb', mul_one, smul_comm a, smul_right_inj ha.ne', smul_right_inj hb.ne'] using (h _ _ H).norm_smul_eq.symm end lemma strict_convex_space.of_norm_add_lt_aux {a b c d : ℝ} (ha : 0 < a) (hab : a + b = 1) (hc : 0 < c) (hd : 0 < d) (hcd : c + d = 1) (hca : c ≀ a) {x y : E} (hy : βˆ₯yβˆ₯ ≀ 1) (hxy : βˆ₯a β€’ x + b β€’ yβˆ₯ < 1) : βˆ₯c β€’ x + d β€’ yβˆ₯ < 1 := begin have hbd : b ≀ d, { refine le_of_add_le_add_left (hab.trans_le _), rw ←hcd, exact add_le_add_right hca _ }, have h₁ : 0 < c / a := div_pos hc ha, have hβ‚‚ : 0 ≀ d - c / a * b, { rw [sub_nonneg, mul_comm_div, ←le_div_iff' hc], exact div_le_div hd.le hbd hc hca }, calc βˆ₯c β€’ x + d β€’ yβˆ₯ = βˆ₯(c / a) β€’ (a β€’ x + b β€’ y) + (d - c / a * b) β€’ yβˆ₯ : by rw [smul_add, ←mul_smul, ←mul_smul, div_mul_cancel _ ha.ne', sub_smul, add_add_sub_cancel] ... ≀ βˆ₯(c / a) β€’ (a β€’ x + b β€’ y)βˆ₯ + βˆ₯(d - c / a * b) β€’ yβˆ₯ : norm_add_le _ _ ... = c / a * βˆ₯a β€’ x + b β€’ yβˆ₯ + (d - c / a * b) * βˆ₯yβˆ₯ : by rw [norm_smul_of_nonneg h₁.le, norm_smul_of_nonneg hβ‚‚] ... < c / a * 1 + (d - c / a * b) * 1 : add_lt_add_of_lt_of_le (mul_lt_mul_of_pos_left hxy h₁) (mul_le_mul_of_nonneg_left hy hβ‚‚) ... = 1 : begin nth_rewrite 0 ←hab, rw [mul_add, div_mul_cancel _ ha.ne', mul_one, add_add_sub_cancel, hcd], end, end /-- Strict convexity is equivalent to `βˆ₯a β€’ x + b β€’ yβˆ₯ < 1` for all `x` and `y` of norm at most `1` and all strictly positive `a` and `b` such that `a + b = 1`. This shows that we only need to check it for fixed `a` and `b`. -/ lemma strict_convex_space.of_norm_add_lt {a b : ℝ} (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) (h : βˆ€ x y : E, βˆ₯xβˆ₯ ≀ 1 β†’ βˆ₯yβˆ₯ ≀ 1 β†’ x β‰  y β†’ βˆ₯a β€’ x + b β€’ yβˆ₯ < 1) : strict_convex_space ℝ E := begin refine strict_convex_space.of_strict_convex_closed_unit_ball _ (Ξ» x hx y hy hxy c d hc hd hcd, _), rw [interior_closed_ball (0 : E) one_ne_zero, mem_ball_zero_iff], rw mem_closed_ball_zero_iff at hx hy, obtain hca | hac := le_total c a, { exact strict_convex_space.of_norm_add_lt_aux ha hab hc hd hcd hca hy (h _ _ hx hy hxy) }, rw add_comm at ⊒ hab hcd, refine strict_convex_space.of_norm_add_lt_aux hb hab hd hc hcd _ hx _, { refine le_of_add_le_add_right (hcd.trans_le _), rw ←hab, exact add_le_add_left hac _ }, { rw add_comm, exact h _ _ hx hy hxy } end variables [strict_convex_space ℝ E] {x y z : E} {a b r : ℝ} /-- If `x β‰  y` belong to the same closed ball, then a convex combination of `x` and `y` with positive coefficients belongs to the corresponding open ball. -/ lemma combo_mem_ball_of_ne (hx : x ∈ closed_ball z r) (hy : y ∈ closed_ball z r) (hne : x β‰  y) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) : a β€’ x + b β€’ y ∈ ball z r := begin rcases eq_or_ne r 0 with rfl|hr, { rw [closed_ball_zero, mem_singleton_iff] at hx hy, exact (hne (hx.trans hy.symm)).elim }, { simp only [← interior_closed_ball _ hr] at hx hy ⊒, exact strict_convex_closed_ball ℝ z r hx hy hne ha hb hab } end /-- If `x β‰  y` belong to the same closed ball, then the open segment with endpoints `x` and `y` is included in the corresponding open ball. -/ lemma open_segment_subset_ball_of_ne (hx : x ∈ closed_ball z r) (hy : y ∈ closed_ball z r) (hne : x β‰  y) : open_segment ℝ x y βŠ† ball z r := (open_segment_subset_iff _).2 $ Ξ» a b, combo_mem_ball_of_ne hx hy hne /-- If `x` and `y` are two distinct vectors of norm at most `r`, then a convex combination of `x` and `y` with positive coefficients has norm strictly less than `r`. -/ lemma norm_combo_lt_of_ne (hx : βˆ₯xβˆ₯ ≀ r) (hy : βˆ₯yβˆ₯ ≀ r) (hne : x β‰  y) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1) : βˆ₯a β€’ x + b β€’ yβˆ₯ < r := begin simp only [← mem_ball_zero_iff, ← mem_closed_ball_zero_iff] at hx hy ⊒, exact combo_mem_ball_of_ne hx hy hne ha hb hab end /-- In a strictly convex space, if `x` and `y` are not in the same ray, then `βˆ₯x + yβˆ₯ < βˆ₯xβˆ₯ + βˆ₯yβˆ₯`. -/ lemma norm_add_lt_of_not_same_ray (h : Β¬same_ray ℝ x y) : βˆ₯x + yβˆ₯ < βˆ₯xβˆ₯ + βˆ₯yβˆ₯ := begin simp only [same_ray_iff_inv_norm_smul_eq, not_or_distrib, ← ne.def] at h, rcases h with ⟨hx, hy, hne⟩, rw ← norm_pos_iff at hx hy, have hxy : 0 < βˆ₯xβˆ₯ + βˆ₯yβˆ₯ := add_pos hx hy, have := combo_mem_ball_of_ne (inv_norm_smul_mem_closed_unit_ball x) (inv_norm_smul_mem_closed_unit_ball y) hne (div_pos hx hxy) (div_pos hy hxy) (by rw [← add_div, div_self hxy.ne']), rwa [mem_ball_zero_iff, div_eq_inv_mul, div_eq_inv_mul, mul_smul, mul_smul, smul_inv_smulβ‚€ hx.ne', smul_inv_smulβ‚€ hy.ne', ← smul_add, norm_smul, real.norm_of_nonneg (inv_pos.2 hxy).le, ← div_eq_inv_mul, div_lt_one hxy] at this end lemma lt_norm_sub_of_not_same_ray (h : Β¬same_ray ℝ x y) : βˆ₯xβˆ₯ - βˆ₯yβˆ₯ < βˆ₯x - yβˆ₯ := begin nth_rewrite 0 ←sub_add_cancel x y at ⊒ h, exact sub_lt_iff_lt_add.2 (norm_add_lt_of_not_same_ray $ Ξ» H', h $ H'.add_left same_ray.rfl), end lemma abs_lt_norm_sub_of_not_same_ray (h : Β¬same_ray ℝ x y) : |βˆ₯xβˆ₯ - βˆ₯yβˆ₯| < βˆ₯x - yβˆ₯ := begin refine abs_sub_lt_iff.2 ⟨lt_norm_sub_of_not_same_ray h, _⟩, rw norm_sub_rev, exact lt_norm_sub_of_not_same_ray (mt same_ray.symm h), end /-- In a strictly convex space, two vectors `x`, `y` are in the same ray if and only if the triangle inequality for `x` and `y` becomes an equality. -/ lemma same_ray_iff_norm_add : same_ray ℝ x y ↔ βˆ₯x + yβˆ₯ = βˆ₯xβˆ₯ + βˆ₯yβˆ₯ := ⟨same_ray.norm_add, Ξ» h, not_not.1 $ Ξ» h', (norm_add_lt_of_not_same_ray h').ne h⟩ /-- If `x` and `y` are two vectors in a strictly convex space have the same norm and the norm of their sum is equal to the sum of their norms, then they are equal. -/ lemma eq_of_norm_eq_of_norm_add_eq (h₁ : βˆ₯xβˆ₯ = βˆ₯yβˆ₯) (hβ‚‚ : βˆ₯x + yβˆ₯ = βˆ₯xβˆ₯ + βˆ₯yβˆ₯) : x = y := (same_ray_iff_norm_add.mpr hβ‚‚).eq_of_norm_eq h₁ /-- In a strictly convex space, two vectors `x`, `y` are not in the same ray if and only if the triangle inequality for `x` and `y` is strict. -/ lemma not_same_ray_iff_norm_add_lt : Β¬ same_ray ℝ x y ↔ βˆ₯x + yβˆ₯ < βˆ₯xβˆ₯ + βˆ₯yβˆ₯ := same_ray_iff_norm_add.not.trans (norm_add_le _ _).lt_iff_ne.symm lemma same_ray_iff_norm_sub : same_ray ℝ x y ↔ βˆ₯x - yβˆ₯ = |βˆ₯xβˆ₯ - βˆ₯yβˆ₯| := ⟨same_ray.norm_sub, Ξ» h, not_not.1 $ Ξ» h', (abs_lt_norm_sub_of_not_same_ray h').ne' h⟩ lemma not_same_ray_iff_abs_lt_norm_sub : Β¬ same_ray ℝ x y ↔ |βˆ₯xβˆ₯ - βˆ₯yβˆ₯| < βˆ₯x - yβˆ₯ := same_ray_iff_norm_sub.not.trans $ ne_comm.trans (abs_norm_sub_norm_le _ _).lt_iff_ne.symm /-- In a strictly convex space, the triangle inequality turns into an equality if and only if the middle point belongs to the segment joining two other points. -/ lemma dist_add_dist_eq_iff : dist x y + dist y z = dist x z ↔ y ∈ [x -[ℝ] z] := by simp only [mem_segment_iff_same_ray, same_ray_iff_norm_add, dist_eq_norm', sub_add_sub_cancel', eq_comm] lemma norm_midpoint_lt_iff (h : βˆ₯xβˆ₯ = βˆ₯yβˆ₯) : βˆ₯(1/2 : ℝ) β€’ (x + y)βˆ₯ < βˆ₯xβˆ₯ ↔ x β‰  y := by rw [norm_smul, real.norm_of_nonneg (one_div_nonneg.2 zero_le_two), ←inv_eq_one_div, ←div_eq_inv_mul, div_lt_iff (@zero_lt_two ℝ _ _), mul_two, ←not_same_ray_iff_of_norm_eq h, not_same_ray_iff_norm_add_lt, h] variables {F : Type*} [normed_group F] [normed_space ℝ F] variables {PF : Type*} {PE : Type*} [metric_space PF] [metric_space PE] variables [normed_add_torsor F PF] [normed_add_torsor E PE] include E lemma eq_line_map_of_dist_eq_mul_of_dist_eq_mul {x y z : PE} (hxy : dist x y = r * dist x z) (hyz : dist y z = (1 - r) * dist x z) : y = affine_map.line_map x z r := begin have : y -α΅₯ x ∈ [(0 : E) -[ℝ] z -α΅₯ x], { rw [← dist_add_dist_eq_iff, dist_zero_left, dist_vsub_cancel_right, ← dist_eq_norm_vsub', ← dist_eq_norm_vsub', hxy, hyz, ← add_mul, add_sub_cancel'_right, one_mul] }, rcases eq_or_ne x z with rfl|hne, { obtain rfl : y = x, by simpa, simp }, { rw [← dist_ne_zero] at hne, rcases this with ⟨a, b, ha, hb, hab, H⟩, rw [smul_zero, zero_add] at H, have H' := congr_arg norm H, rw [norm_smul, real.norm_of_nonneg hb, ← dist_eq_norm_vsub', ← dist_eq_norm_vsub', hxy, mul_left_inj' hne] at H', rw [affine_map.line_map_apply, ← H', H, vsub_vadd] }, end lemma eq_midpoint_of_dist_eq_half {x y z : PE} (hx : dist x y = dist x z / 2) (hy : dist y z = dist x z / 2) : y = midpoint ℝ x z := begin apply eq_line_map_of_dist_eq_mul_of_dist_eq_mul, { rwa [inv_of_eq_inv, ← div_eq_inv_mul] }, { rwa [inv_of_eq_inv, ← one_div, sub_half, one_div, ← div_eq_inv_mul] } end namespace isometry include F /-- An isometry of `normed_add_torsor`s for real normed spaces, strictly convex in the case of the codomain, is an affine isometry. Unlike Mazur-Ulam, this does not require the isometry to be surjective. -/ noncomputable def affine_isometry_of_strict_convex_space {f : PF β†’ PE} (hi : isometry f) : PF →ᡃⁱ[ℝ] PE := { norm_map := Ξ» x, by simp [affine_map.of_map_midpoint, ←dist_eq_norm_vsub E, hi.dist_eq], ..affine_map.of_map_midpoint f (Ξ» x y, begin apply eq_midpoint_of_dist_eq_half, { rw [hi.dist_eq, hi.dist_eq, dist_left_midpoint, real.norm_of_nonneg zero_le_two, div_eq_inv_mul] }, { rw [hi.dist_eq, hi.dist_eq, dist_midpoint_right, real.norm_of_nonneg zero_le_two, div_eq_inv_mul] }, end) hi.continuous } @[simp] lemma coe_affine_isometry_of_strict_convex_space {f : PF β†’ PE} (hi : isometry f) : ⇑(hi.affine_isometry_of_strict_convex_space) = f := rfl @[simp] lemma affine_isometry_of_strict_convex_space_apply {f : PF β†’ PE} (hi : isometry f) (p : PF) : hi.affine_isometry_of_strict_convex_space p = f p := rfl end isometry
4ddb2a99270afb637c9d687fda10375f89422510
bcdb337a071a6642a282053e6c285336b9716772
/Tensor/Has.lean
cc73c25849490c0ba91b8e7281155f8ddcdbf298
[]
no_license
kovach/lean4-sparse-matrix-test
87a8b3a0b7785976eccfaf69b0d289940549f446
be9f6029883c88bf1f7588a0e6d3cde033368809
refs/heads/master
1,678,513,001,774
1,614,027,522,000
1,614,027,522,000
341,329,194
1
0
null
null
null
null
UTF-8
Lean
false
false
280
lean
-- todo: should have used OfNat? class HasZero (Ξ± : Type _) where zero : Ξ± class HasOne (Ξ± : Type _) where one : Ξ± instance : HasZero Nat where zero := 0 instance : HasOne Nat where one := 1 instance : HasZero Float where zero := 0.0 instance : HasOne Float where one := 1.0
eaa9e2baa633226f652f61152c02804b5c7c4715
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/sets/order.lean
a1a05edf4a398e5d824c667d06a7f693c2c7f47d
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
2,362
lean
/- Copyright (c) 2022 YaΓ«l Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: YaΓ«l Dillies -/ import order.upper_lower import topology.sets.closeds /-! # Clopen upper sets In this file we define the type of clopen upper sets. -/ open set topological_space variables {Ξ± Ξ² : Type*} [topological_space Ξ±] [has_le Ξ±] [topological_space Ξ²] [has_le Ξ²] /-! ### Compact open sets -/ /-- The type of clopen upper sets of a topological space. -/ structure clopen_upper_set (Ξ± : Type*) [topological_space Ξ±] [has_le Ξ±] extends clopens Ξ± := (upper' : is_upper_set carrier) namespace clopen_upper_set instance : set_like (clopen_upper_set Ξ±) Ξ± := { coe := Ξ» s, s.carrier, coe_injective' := Ξ» s t h, by { obtain ⟨⟨_, _⟩, _⟩ := s, obtain ⟨⟨_, _⟩, _⟩ := t, congr' } } lemma upper (s : clopen_upper_set Ξ±) : is_upper_set (s : set Ξ±) := s.upper' lemma clopen (s : clopen_upper_set Ξ±) : is_clopen (s : set Ξ±) := s.clopen' /-- Reinterpret a upper clopen as an upper set. -/ @[simps] def to_upper_set (s : clopen_upper_set Ξ±) : upper_set Ξ± := ⟨s, s.upper⟩ @[ext] protected lemma ext {s t : clopen_upper_set Ξ±} (h : (s : set Ξ±) = t) : s = t := set_like.ext' h @[simp] lemma coe_mk (s : clopens Ξ±) (h) : (mk s h : set Ξ±) = s := rfl instance : has_sup (clopen_upper_set Ξ±) := ⟨λ s t, ⟨s.to_clopens βŠ” t.to_clopens, s.upper.union t.upper⟩⟩ instance : has_inf (clopen_upper_set Ξ±) := ⟨λ s t, ⟨s.to_clopens βŠ“ t.to_clopens, s.upper.inter t.upper⟩⟩ instance : has_top (clopen_upper_set Ξ±) := ⟨⟨⊀, is_upper_set_univ⟩⟩ instance : has_bot (clopen_upper_set Ξ±) := ⟨⟨βŠ₯, is_upper_set_empty⟩⟩ instance : lattice (clopen_upper_set Ξ±) := set_like.coe_injective.lattice _ (Ξ» _ _, rfl) (Ξ» _ _, rfl) instance : bounded_order (clopen_upper_set Ξ±) := bounded_order.lift (coe : _ β†’ set Ξ±) (Ξ» _ _, id) rfl rfl @[simp] lemma coe_sup (s t : clopen_upper_set Ξ±) : (↑(s βŠ” t) : set Ξ±) = s βˆͺ t := rfl @[simp] lemma coe_inf (s t : clopen_upper_set Ξ±) : (↑(s βŠ“ t) : set Ξ±) = s ∩ t := rfl @[simp] lemma coe_top : (↑(⊀ : clopen_upper_set Ξ±) : set Ξ±) = univ := rfl @[simp] lemma coe_bot : (↑(βŠ₯ : clopen_upper_set Ξ±) : set Ξ±) = βˆ… := rfl instance : inhabited (clopen_upper_set Ξ±) := ⟨βŠ₯⟩ end clopen_upper_set
c4d9c58e1672988ba4962c8a192849522e15ff4e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/dynamics/omega_limit.lean
1f1866f150ec76b1f2e2aebaf6e860b9b405faa2
[ "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,756
lean
/- Copyright (c) 2020 Jean Lo. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jean Lo -/ import dynamics.flow /-! # Ο‰-limits > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. For a function `Ο• : Ο„ β†’ Ξ± β†’ Ξ²` where `Ξ²` is a topological space, we define the Ο‰-limit under `Ο•` of a set `s` in `Ξ±` with respect to filter `f` on `Ο„`: an element `y : Ξ²` is in the Ο‰-limit of `s` if the forward images of `s` intersect arbitrarily small neighbourhoods of `y` frequently "in the direction of `f`". In practice `Ο•` is often a continuous monoid-act, but the definition requires only that `Ο•` has a coercion to the appropriate function type. In the case where `Ο„` is `β„•` or `ℝ` and `f` is `at_top`, we recover the usual definition of the Ο‰-limit set as the set of all `y` such that there exist sequences `(tβ‚™)`, `(xβ‚™)` such that `Ο• tβ‚™ xβ‚™ ⟢ y` as `n ⟢ ∞`. ## Notations The `omega_limit` locale provides the localised notation `Ο‰` for `omega_limit`, as well as `ω⁺` and `ω⁻` for `omega_limit at_top` and `omega_limit at_bot` respectively for when the acting monoid is endowed with an order. -/ open set function filter open_locale topology /-! ### Definition and notation -/ section omega_limit variables {Ο„ : Type*} {Ξ± : Type*} {Ξ² : Type*} {ΞΉ : Type*} /-- The Ο‰-limit of a set `s` under `Ο•` with respect to a filter `f` is β‹‚ u ∈ f, cl (Ο• u s). -/ def omega_limit [topological_space Ξ²] (f : filter Ο„) (Ο• : Ο„ β†’ Ξ± β†’ Ξ²) (s : set Ξ±) : set Ξ² := β‹‚ u ∈ f, closure (image2 Ο• u s) localized "notation (name := omega_limit) `Ο‰` := omega_limit" in omega_limit localized "notation (name := omega_limit.at_top) `ω⁺` := omega_limit filter.at_top" in omega_limit localized "notation (name := omega_limit.at_bot) `ω⁻` := omega_limit filter.at_bot" in omega_limit variables [topological_space Ξ²] variables (f : filter Ο„) (Ο• : Ο„ β†’ Ξ± β†’ Ξ²) (s s₁ sβ‚‚: set Ξ±) /-! ### Elementary properties -/ lemma omega_limit_def : Ο‰ f Ο• s = β‹‚ u ∈ f, closure (image2 Ο• u s) := rfl lemma omega_limit_subset_of_tendsto {m : Ο„ β†’ Ο„} {f₁ fβ‚‚ : filter Ο„} (hf : tendsto m f₁ fβ‚‚) : Ο‰ f₁ (Ξ» t x, Ο• (m t) x) s βŠ† Ο‰ fβ‚‚ Ο• s := begin refine Interβ‚‚_mono' (Ξ» u hu, ⟨m ⁻¹' u, tendsto_def.mp hf _ hu, _⟩), rw ←image2_image_left, exact closure_mono (image2_subset (image_preimage_subset _ _) subset.rfl), end lemma omega_limit_mono_left {f₁ fβ‚‚ : filter Ο„} (hf : f₁ ≀ fβ‚‚) : Ο‰ f₁ Ο• s βŠ† Ο‰ fβ‚‚ Ο• s := omega_limit_subset_of_tendsto Ο• s (tendsto_id'.2 hf) lemma omega_limit_mono_right {s₁ sβ‚‚ : set Ξ±} (hs : s₁ βŠ† sβ‚‚) : Ο‰ f Ο• s₁ βŠ† Ο‰ f Ο• sβ‚‚ := Interβ‚‚_mono $ Ξ» u hu, closure_mono (image2_subset subset.rfl hs) lemma is_closed_omega_limit : is_closed (Ο‰ f Ο• s) := is_closed_Inter $ Ξ» u, is_closed_Inter $ Ξ» hu, is_closed_closure lemma maps_to_omega_limit' {Ξ±' Ξ²' : Type*} [topological_space Ξ²'] {f : filter Ο„} {Ο• : Ο„ β†’ Ξ± β†’ Ξ²} {Ο•' : Ο„ β†’ Ξ±' β†’ Ξ²'} {ga : Ξ± β†’ Ξ±'} {s' : set Ξ±'} (hs : maps_to ga s s') {gb : Ξ² β†’ Ξ²'} (hg : βˆ€αΆ  t in f, eq_on (gb ∘ (Ο• t)) (Ο•' t ∘ ga) s) (hgc : continuous gb) : maps_to gb (Ο‰ f Ο• s) (Ο‰ f Ο•' s') := begin simp only [omega_limit_def, mem_Inter, maps_to], intros y hy u hu, refine map_mem_closure hgc (hy _ (inter_mem hu hg)) (forall_image2_iff.2 $ Ξ» t ht x hx, _), calc gb (Ο• t x) = Ο•' t (ga x) : ht.2 hx ... ∈ image2 Ο•' u s' : mem_image2_of_mem ht.1 (hs hx) end lemma maps_to_omega_limit {Ξ±' Ξ²' : Type*} [topological_space Ξ²'] {f : filter Ο„} {Ο• : Ο„ β†’ Ξ± β†’ Ξ²} {Ο•' : Ο„ β†’ Ξ±' β†’ Ξ²'} {ga : Ξ± β†’ Ξ±'} {s' : set Ξ±'} (hs : maps_to ga s s') {gb : Ξ² β†’ Ξ²'} (hg : βˆ€ t x, gb (Ο• t x) = Ο•' t (ga x)) (hgc : continuous gb) : maps_to gb (Ο‰ f Ο• s) (Ο‰ f Ο•' s') := maps_to_omega_limit' _ hs (eventually_of_forall $ Ξ» t x hx, hg t x) hgc lemma omega_limit_image_eq {Ξ±' : Type*} (Ο• : Ο„ β†’ Ξ±' β†’ Ξ²) (f : filter Ο„) (g : Ξ± β†’ Ξ±') : Ο‰ f Ο• (g '' s) = Ο‰ f (Ξ» t x, Ο• t (g x)) s := by simp only [omega_limit, image2_image_right] lemma omega_limit_preimage_subset {Ξ±' : Type*} (Ο• : Ο„ β†’ Ξ±' β†’ Ξ²) (s : set Ξ±') (f : filter Ο„) (g : Ξ± β†’ Ξ±') : Ο‰ f (Ξ» t x, Ο• t (g x)) (g ⁻¹' s) βŠ† Ο‰ f Ο• s := maps_to_omega_limit _ (maps_to_preimage _ _) (Ξ» t x, rfl) continuous_id /-! ### Equivalent definitions of the omega limit The next few lemmas are various versions of the property characterising Ο‰-limits: -/ /-- An element `y` is in the Ο‰-limit set of `s` w.r.t. `f` if the preimages of an arbitrary neighbourhood of `y` frequently (w.r.t. `f`) intersects of `s`. -/ lemma mem_omega_limit_iff_frequently (y : Ξ²) : y ∈ Ο‰ f Ο• s ↔ βˆ€ n ∈ 𝓝 y, βˆƒαΆ  t in f, (s ∩ Ο• t ⁻¹' n).nonempty := begin simp_rw [frequently_iff, omega_limit_def, mem_Inter, mem_closure_iff_nhds], split, { intros h _ hn _ hu, rcases h _ hu _ hn with ⟨_, _, _, _, ht, hx, hΟ•tx⟩, exact ⟨_, ht, _, hx, by rwa [mem_preimage, hΟ•tx]⟩, }, { intros h _ hu _ hn, rcases h _ hn hu with ⟨_, ht, _, hx, hΟ•tx⟩, exact ⟨_, hΟ•tx, _, _, ht, hx, rfl⟩ } end /-- An element `y` is in the Ο‰-limit set of `s` w.r.t. `f` if the forward images of `s` frequently (w.r.t. `f`) intersect arbitrary neighbourhoods of `y`. -/ lemma mem_omega_limit_iff_frequentlyβ‚‚ (y : Ξ²) : y ∈ Ο‰ f Ο• s ↔ βˆ€ n ∈ 𝓝 y, βˆƒαΆ  t in f, (Ο• t '' s ∩ n).nonempty := by simp_rw [mem_omega_limit_iff_frequently, image_inter_nonempty_iff] /-- An element `y` is in the Ο‰-limit of `x` w.r.t. `f` if the forward images of `x` frequently (w.r.t. `f`) falls within an arbitrary neighbourhood of `y`. -/ lemma mem_omega_limit_singleton_iff_map_cluster_point (x : Ξ±) (y : Ξ²) : y ∈ Ο‰ f Ο• {x} ↔ map_cluster_pt y f (Ξ» t, Ο• t x) := by simp_rw [mem_omega_limit_iff_frequently, map_cluster_pt_iff, singleton_inter_nonempty, mem_preimage] /-! ### Set operations and omega limits -/ lemma omega_limit_inter : Ο‰ f Ο• (s₁ ∩ sβ‚‚) βŠ† Ο‰ f Ο• s₁ ∩ Ο‰ f Ο• sβ‚‚ := subset_inter (omega_limit_mono_right _ _ (inter_subset_left _ _)) (omega_limit_mono_right _ _(inter_subset_right _ _)) lemma omega_limit_Inter (p : ΞΉ β†’ set Ξ±) : Ο‰ f Ο• (β‹‚ i, p i) βŠ† β‹‚ i, Ο‰ f Ο• (p i) := subset_Inter $ Ξ» i, omega_limit_mono_right _ _ (Inter_subset _ _) lemma omega_limit_union : Ο‰ f Ο• (s₁ βˆͺ sβ‚‚) = Ο‰ f Ο• s₁ βˆͺ Ο‰ f Ο• sβ‚‚ := begin ext y, split, { simp only [mem_union, mem_omega_limit_iff_frequently, union_inter_distrib_right, union_nonempty, frequently_or_distrib], contrapose!, simp only [not_frequently, not_nonempty_iff_eq_empty, ← subset_empty_iff], rintro ⟨⟨n₁, hn₁, hβ‚βŸ©, ⟨nβ‚‚, hnβ‚‚, hβ‚‚βŸ©βŸ©, refine ⟨n₁ ∩ nβ‚‚, inter_mem hn₁ hnβ‚‚, h₁.mono $ Ξ» t, _, hβ‚‚.mono $ Ξ» t, _⟩, exacts [subset.trans $ inter_subset_inter_right _ $ preimage_mono $ inter_subset_left _ _, subset.trans $ inter_subset_inter_right _ $ preimage_mono $ inter_subset_right _ _] }, { rintros (hy|hy), exacts [omega_limit_mono_right _ _ (subset_union_left _ _) hy, omega_limit_mono_right _ _ (subset_union_right _ _) hy] }, end lemma omega_limit_Union (p : ΞΉ β†’ set Ξ±) : (⋃ i, Ο‰ f Ο• (p i)) βŠ† Ο‰ f Ο• ⋃ i, p i := by { rw Union_subset_iff, exact Ξ» i, omega_limit_mono_right _ _ (subset_Union _ _)} /-! Different expressions for omega limits, useful for rewrites. In particular, one may restrict the intersection to sets in `f` which are subsets of some set `v` also in `f`. -/ lemma omega_limit_eq_Inter : Ο‰ f Ο• s = β‹‚ u : β†₯f.sets, closure (image2 Ο• u s) := bInter_eq_Inter _ _ lemma omega_limit_eq_bInter_inter {v : set Ο„} (hv : v ∈ f) : Ο‰ f Ο• s = β‹‚ u ∈ f, closure (image2 Ο• (u ∩ v) s) := subset.antisymm (Interβ‚‚_mono' $ Ξ» u hu, ⟨u ∩ v, inter_mem hu hv, subset.rfl⟩) (Interβ‚‚_mono $ Ξ» u hu, closure_mono $ image2_subset (inter_subset_left _ _) subset.rfl) lemma omega_limit_eq_Inter_inter {v : set Ο„} (hv : v ∈ f) : Ο‰ f Ο• s = β‹‚ (u : β†₯f.sets), closure (image2 Ο• (u ∩ v) s) := by { rw omega_limit_eq_bInter_inter _ _ _ hv, apply bInter_eq_Inter } lemma omega_limit_subset_closure_fw_image {u : set Ο„} (hu : u ∈ f) : Ο‰ f Ο• s βŠ† closure (image2 Ο• u s) := begin rw omega_limit_eq_Inter, intros _ hx, rw mem_Inter at hx, exact hx ⟨u, hu⟩, end /-! ### `Ο‰-limits and compactness -/ /-- A set is eventually carried into any open neighbourhood of its Ο‰-limit: if `c` is a compact set such that `closure {Ο• t x | t ∈ v, x ∈ s} βŠ† c` for some `v ∈ f` and `n` is an open neighbourhood of `Ο‰ f Ο• s`, then for some `u ∈ f` we have `closure {Ο• t x | t ∈ u, x ∈ s} βŠ† n`. -/ lemma eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset' {c : set Ξ²} (hc₁ : is_compact c) (hcβ‚‚ : βˆƒ v ∈ f, closure (image2 Ο• v s) βŠ† c) {n : set Ξ²} (hn₁ : is_open n) (hnβ‚‚ : Ο‰ f Ο• s βŠ† n) : βˆƒ u ∈ f, closure (image2 Ο• u s) βŠ† n := begin rcases hcβ‚‚ with ⟨v, hv₁, hvβ‚‚βŸ©, let k := closure (image2 Ο• v s), have hk : is_compact (k \ n) := is_compact.diff (is_compact_of_is_closed_subset hc₁ is_closed_closure hvβ‚‚) hn₁, let j := Ξ» u, (closure (image2 Ο• (u ∩ v) s))ᢜ, have hj₁ : βˆ€ u ∈ f, is_open (j u), from Ξ» _ _, (is_open_compl_iff.mpr is_closed_closure), have hjβ‚‚ : k \ n βŠ† ⋃ u ∈ f, j u, begin have : (⋃ u ∈ f, j u) = ⋃ (u : β†₯f.sets), j u, from bUnion_eq_Union _ _, rw [this, diff_subset_comm, diff_Union], rw omega_limit_eq_Inter_inter _ _ _ hv₁ at hnβ‚‚, simp_rw diff_compl, rw ←inter_Inter, exact subset.trans (inter_subset_right _ _) hnβ‚‚, end, rcases hk.elim_finite_subcover_image hj₁ hjβ‚‚ with ⟨g, hg₁ : βˆ€ u ∈ g, u ∈ f, hgβ‚‚, hgβ‚ƒβŸ©, let w := (β‹‚ u ∈ g, u) ∩ v, have hwβ‚‚ : w ∈ f, by simpa *, have hw₃ : k \ n βŠ† (closure (image2 Ο• w s))ᢜ, from calc k \ n βŠ† ⋃ u ∈ g, j u : hg₃ ... βŠ† (closure (image2 Ο• w s))ᢜ : begin simp only [Union_subset_iff, compl_subset_compl], intros u hu, mono* using [w], exact Inter_subset_of_subset u (Inter_subset_of_subset hu subset.rfl), end, have hwβ‚„ : kᢜ βŠ† (closure (image2 Ο• w s))ᢜ, begin rw compl_subset_compl, calc closure (image2 Ο• w s) βŠ† _ : closure_mono (image2_subset (inter_subset_right _ _) subset.rfl) end, have hnc : nᢜ βŠ† (k \ n) βˆͺ kᢜ, by rw [union_comm, ←inter_subset, diff_eq, inter_comm], have hw : closure (image2 Ο• w s) βŠ† n, from compl_subset_compl.mp (subset.trans hnc (union_subset hw₃ hwβ‚„)), exact ⟨_, hwβ‚‚, hw⟩ end /-- A set is eventually carried into any open neighbourhood of its Ο‰-limit: if `c` is a compact set such that `closure {Ο• t x | t ∈ v, x ∈ s} βŠ† c` for some `v ∈ f` and `n` is an open neighbourhood of `Ο‰ f Ο• s`, then for some `u ∈ f` we have `closure {Ο• t x | t ∈ u, x ∈ s} βŠ† n`. -/ lemma eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset [t2_space Ξ²] {c : set Ξ²} (hc₁ : is_compact c) (hcβ‚‚ : βˆ€αΆ  t in f, maps_to (Ο• t) s c) {n : set Ξ²} (hn₁ : is_open n) (hnβ‚‚ : Ο‰ f Ο• s βŠ† n) : βˆƒ u ∈ f, closure (image2 Ο• u s) βŠ† n := eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset' f Ο• _ hc₁ ⟨_, hcβ‚‚, closure_minimal (image2_subset_iff.2 (Ξ» t, id)) hc₁.is_closed⟩ hn₁ hnβ‚‚ lemma eventually_maps_to_of_is_compact_absorbing_of_is_open_of_omega_limit_subset [t2_space Ξ²] {c : set Ξ²} (hc₁ : is_compact c) (hcβ‚‚ : βˆ€αΆ  t in f, maps_to (Ο• t) s c) {n : set Ξ²} (hn₁ : is_open n) (hnβ‚‚ : Ο‰ f Ο• s βŠ† n) : βˆ€αΆ  t in f, maps_to (Ο• t) s n := begin rcases eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset f Ο• s hc₁ hcβ‚‚ hn₁ hnβ‚‚ with ⟨u, hu_mem, hu⟩, refine mem_of_superset hu_mem (Ξ» t ht x hx, _), exact hu (subset_closure $ mem_image2_of_mem ht hx) end lemma eventually_closure_subset_of_is_open_of_omega_limit_subset [compact_space Ξ²] {v : set Ξ²} (hv₁ : is_open v) (hvβ‚‚ : Ο‰ f Ο• s βŠ† v) : βˆƒ u ∈ f, closure (image2 Ο• u s) βŠ† v := eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset' _ _ _ is_compact_univ ⟨univ, univ_mem, subset_univ _⟩ hv₁ hvβ‚‚ lemma eventually_maps_to_of_is_open_of_omega_limit_subset [compact_space Ξ²] {v : set Ξ²} (hv₁ : is_open v) (hvβ‚‚ : Ο‰ f Ο• s βŠ† v) : βˆ€αΆ  t in f, maps_to (Ο• t) s v := begin rcases eventually_closure_subset_of_is_open_of_omega_limit_subset f Ο• s hv₁ hvβ‚‚ with ⟨u, hu_mem, hu⟩, refine mem_of_superset hu_mem (Ξ» t ht x hx, _), exact hu (subset_closure $ mem_image2_of_mem ht hx) end /-- The Ο‰-limit of a nonempty set w.r.t. a nontrivial filter is nonempty. -/ lemma nonempty_omega_limit_of_is_compact_absorbing [ne_bot f] {c : set Ξ²} (hc₁ : is_compact c) (hcβ‚‚ : βˆƒ v ∈ f, closure (image2 Ο• v s) βŠ† c) (hs : s.nonempty) : (Ο‰ f Ο• s).nonempty := begin rcases hcβ‚‚ with ⟨v, hv₁, hvβ‚‚βŸ©, rw omega_limit_eq_Inter_inter _ _ _ hv₁, apply is_compact.nonempty_Inter_of_directed_nonempty_compact_closed, { rintro ⟨u₁, huβ‚βŸ© ⟨uβ‚‚, huβ‚‚βŸ©, use ⟨u₁ ∩ uβ‚‚, inter_mem hu₁ huβ‚‚βŸ©, split, all_goals { exact closure_mono (image2_subset (inter_subset_inter_left _ (by simp)) subset.rfl) }}, { intro u, have hn : (image2 Ο• (u ∩ v) s).nonempty, from nonempty.image2 (nonempty_of_mem (inter_mem u.prop hv₁)) hs, exact hn.mono subset_closure }, { intro _, apply is_compact_of_is_closed_subset hc₁ is_closed_closure, calc _ βŠ† closure (image2 Ο• v s) : closure_mono (image2_subset (inter_subset_right _ _) subset.rfl) ... βŠ† c : hvβ‚‚ }, { exact Ξ» _, is_closed_closure }, end lemma nonempty_omega_limit [compact_space Ξ²] [ne_bot f] (hs : s.nonempty) : (Ο‰ f Ο• s).nonempty := nonempty_omega_limit_of_is_compact_absorbing _ _ _ is_compact_univ ⟨univ, univ_mem, subset_univ _⟩ hs end omega_limit /-! ### Ο‰-limits of Flows by a Monoid -/ namespace flow variables {Ο„ : Type*} [topological_space Ο„] [add_monoid Ο„] [has_continuous_add Ο„] {Ξ± : Type*} [topological_space Ξ±] (f : filter Ο„) (Ο• : flow Ο„ Ξ±) (s : set Ξ±) open_locale omega_limit lemma is_invariant_omega_limit (hf : βˆ€ t, tendsto ((+) t) f f) : is_invariant Ο• (Ο‰ f Ο• s) := begin refine Ξ» t, maps_to.mono_right _ (omega_limit_subset_of_tendsto Ο• s (hf t)), exact maps_to_omega_limit _ (maps_to_id _) (Ξ» t' x, (Ο•.map_add _ _ _).symm) (continuous_const.flow Ο• continuous_id) end lemma omega_limit_image_subset (t : Ο„) (ht : tendsto (+ t) f f) : Ο‰ f Ο• (Ο• t '' s) βŠ† Ο‰ f Ο• s := begin simp only [omega_limit_image_eq, ← map_add], exact omega_limit_subset_of_tendsto Ο• s ht end end flow /-! ### Ο‰-limits of Flows by a Group -/ namespace flow variables {Ο„ : Type*} [topological_space Ο„] [add_comm_group Ο„] [topological_add_group Ο„] {Ξ± : Type*} [topological_space Ξ±] (f : filter Ο„) (Ο• : flow Ο„ Ξ±) (s : set Ξ±) open_locale omega_limit /-- the Ο‰-limit of a forward image of `s` is the same as the Ο‰-limit of `s`. -/ @[simp] lemma omega_limit_image_eq (hf : βˆ€ t, tendsto (+ t) f f) (t : Ο„) : Ο‰ f Ο• (Ο• t '' s) = Ο‰ f Ο• s := subset.antisymm (omega_limit_image_subset _ _ _ _ (hf t)) $ calc Ο‰ f Ο• s = Ο‰ f Ο• (Ο• (-t) '' (Ο• t '' s)) : by simp [image_image, ← map_add] ... βŠ† Ο‰ f Ο• (Ο• t '' s) : omega_limit_image_subset _ _ _ _ (hf _) lemma omega_limit_omega_limit (hf : βˆ€ t, tendsto ((+) t) f f) : Ο‰ f Ο• (Ο‰ f Ο• s) βŠ† Ο‰ f Ο• s := begin simp only [subset_def, mem_omega_limit_iff_frequentlyβ‚‚, frequently_iff], intros _ h, rintro n hn u hu, rcases mem_nhds_iff.mp hn with ⟨o, ho₁, hoβ‚‚, hoβ‚ƒβŸ©, rcases h o (is_open.mem_nhds hoβ‚‚ ho₃) hu with ⟨t, ht₁, htβ‚‚βŸ©, have l₁ : (Ο‰ f Ο• s ∩ o).nonempty, from htβ‚‚.mono (inter_subset_inter_left _ ((is_invariant_iff_image _ _).mp (is_invariant_omega_limit _ _ _ hf) _)), have lβ‚‚ : ((closure (image2 Ο• u s)) ∩ o).nonempty := l₁.mono (Ξ» b hb, ⟨omega_limit_subset_closure_fw_image _ _ _ hu hb.1, hb.2⟩), have l₃ : (o ∩ image2 Ο• u s).nonempty, begin rcases lβ‚‚ with ⟨b, hb₁, hbβ‚‚βŸ©, exact mem_closure_iff_nhds.mp hb₁ o (is_open.mem_nhds hoβ‚‚ hbβ‚‚) end, rcases l₃ with βŸ¨Ο•ra, ho, ⟨_, _, hr, ha, hΟ•ra⟩⟩, exact ⟨_, hr, Ο•ra, ⟨_, ha, hΟ•ra⟩, ho₁ ho⟩, end end flow
69d85d3aefa783c9f4caaac70b53a9d05021dea8
85485c410781936e7dd6aba0ba77b8a82f76c1d6
/src/mutation.lean
92c318053436016c822ab3c95578e62245d632f0
[]
no_license
yuma-mizuno/lean-cluster-algebra
5b33657b3cf27c24cab292d4ac62d6e1e5e715b9
4a4fb128566305eda2b82aea068157cb43afdf18
refs/heads/master
1,689,542,026,900
1,630,963,668,000
1,630,964,456,000
392,828,775
0
0
null
null
null
null
UTF-8
Lean
false
false
20,995
lean
import linear_algebra.dual import algebra.monoid_algebra import ring_theory.localization import linear_algebra.bilinear_form import tactic.basic import localization /-! # Mutations This file defines mutations as isomorphisms between ambient fields. A seed mutation is a combinatorial operation on a β„€-module `N` given by a piecewise-linear transformation on `N`. A mutation is a field isomorphism associated with a seed mutation. It is an isomorphism on the `K` that is a field of fractions of the group algebra of the dual module of `N`. We define mutations by the following three steps. Step 1. We define a map `seed_mutation.monomial_to_away` that is a monoid hom from `module.dual β„€ N` to `S`, where `S` is a localization of the group algebra of `module.dual β„€ N` away from a non-zero element in the group algebra of `module.dual β„€ N`. Step 2. We show that the map defined in Step 1 induces a isomorphism on `S`. Step 3. We show that the map defined in Step 2 induces a isomorphism on `K` by using the following general fact on localizations: a composition of localizations are equivalent to a single localization when submonoids under these localizations satisfies appropriate inclusion conditions. ## Main definitions * `seed_mutation (s s' : multiset N)` is a structure expressing that `ΞΌ : seed_mutatoin s s'` is a seed mutation with a source seed `s` and a target seed `s'`. * `seed_mutation.field_equiv ΞΌ K` is a field isomorphism `K ≃+* K` associated with a seed mutation `ΞΌ`. ## Main statements * `mutation_field_equiv_map_monomial` gives a fomula for a mutation on the monomials. -/ open_locale classical namespace skew_sym_bilin_form section skew_sym_bilin_form open bilin_form variables {R : Type*} {M : Type*} [ring R] [add_comm_monoid M] [module R M] {B : bilin_form R M} def is_skew_sym (B : bilin_form R M) : Prop := βˆ€ (x y : M), B x y = - B y x variable (H : is_skew_sym B) include H lemma skew_sym (x y : M) : B x y = - B y x := H x y lemma is_refl : refl_bilin_form.is_refl B := Ξ» x y H1, (H y x).symm β–Έ neg_eq_zero.mpr H1 lemma ortho_sym {x y : M} : is_ortho B x y ↔ is_ortho B y x := refl_bilin_form.ortho_sym (is_refl H) lemma is_alt [no_zero_divisors R] [char_zero R] : alt_bilin_form.is_alt B := Ξ» n, add_self_eq_zero.mp (eq_neg_iff_add_eq_zero.mp (H n n)) @[simp] lemma self_eq_zero [no_zero_divisors R] [char_zero R] (x : M) : B x x = 0 := is_alt H x @[simp] lemma self_eq_zero_to_lin [no_zero_divisors R] [char_zero R] (x : M) : to_lin' B x x = 0 := self_eq_zero H x end skew_sym_bilin_form end skew_sym_bilin_form class inductive is_sign (Ξ΅ : β„€) : Prop | pos : Ξ΅ = 1 β†’ is_sign | neg : Ξ΅ = -1 β†’ is_sign instance one.is_sign : is_sign 1 := is_sign.pos rfl instance neg_one.is_sign : is_sign (-1) := is_sign.neg rfl instance neg.is_sign (Ξ΅ : β„€) [is_sign Ξ΅] : is_sign (-Ξ΅) := begin let h : is_sign Ξ΅ := by apply_instance, refine is_sign.rec_on h (Ξ» H, _) (Ξ» H, _), repeat {any_goals { rw H <|> apply_instance <|> rw neg_neg }}, end open skew_sym_bilin_form class skew_symmetric_form (N : Type*) [add_comm_group N] := (form : bilin_form β„€ N) (skew : is_skew_sym form) variables {N : Type*} [add_comm_group N] [skew_symmetric_form N] section seed_mutation variables (s s' : multiset N) (v : N) (Ξ΅ : β„€) open skew_symmetric_form abbreviation B := @bilin_form.to_lin β„€ N _ _ _ form def pl_mutation (v : N) (Ξ΅ : β„€) : N β†’ N := Ξ» n, n + (max 0 (Ξ΅ * (B n v))) β€’ v def pl_mutation.equiv : N ≃ N := { to_fun := pl_mutation v Ξ΅, inv_fun := pl_mutation (-v) (-Ξ΅), left_inv := Ξ» n, by unfold pl_mutation; by simp only [ neg_mul_eq_neg_mul_symm, algebra.id.smul_eq_mul, bilin_form.to_lin_apply, linear_map.smul_apply, bilin_form.neg_right, mul_neg_eq_neg_mul_symm, smul_neg, linear_map.map_smul, linear_map.add_apply, linear_map.map_add, self_eq_zero skew, add_zero, add_neg_cancel_right, neg_neg, mul_zero, neg_zero], right_inv := Ξ» n, by unfold pl_mutation; by simp only [ neg_mul_eq_neg_mul_symm, linear_map.map_neg, algebra.id.smul_eq_mul, bilin_form.to_lin_apply, linear_map.smul_apply, bilin_form.neg_right, mul_neg_eq_neg_mul_symm, linear_map.neg_apply, smul_neg, neg_neg, linear_map.map_smul, linear_map.add_apply, linear_map.map_add, self_eq_zero skew, add_zero, neg_add_cancel_right, eq_self_iff_true, mul_zero, neg_zero] } lemma pl_mutation.bijective : function.bijective (pl_mutation v Ξ΅) := (pl_mutation.equiv v Ξ΅).bijective @[simp] lemma pl_mutation_neg_left_id : pl_mutation (-v) (-Ξ΅) ∘ pl_mutation v Ξ΅ = id := by ext x; apply (pl_mutation.equiv v Ξ΅).left_inv x @[simp] lemma pl_mutation_neg_righ_id : pl_mutation v Ξ΅ ∘ pl_mutation (-v) (-Ξ΅) = id := by ext x; apply (pl_mutation.equiv v Ξ΅).right_inv x structure seed_mutation (s s' : multiset N) := (sign : β„€) (is_sign : is_sign sign) (src_vect : N) (tar_vect : N) (src_in : src_vect ∈ s) (tar_in : tar_vect ∈ s') (seed_tar_src : s'.erase tar_vect = multiset.map (pl_mutation src_vect sign) (s.erase src_vect)) (vect_tar_src : tar_vect = - src_vect) end seed_mutation section direction variables {s s' : multiset N} (ΞΌ : seed_mutation s s') (v : N) class is_mutation_direction : Prop := (is_direction : βˆƒ k : β„€, v = k β€’ ΞΌ.src_vect) lemma seed_mutation.is_direction [is_mutation_direction ΞΌ v] : βˆƒ k : β„€, v = k β€’ ΞΌ.src_vect := is_mutation_direction.is_direction instance src_vect_is_mutation_direction : is_mutation_direction ΞΌ ΞΌ.src_vect := by {use 1, simp} instance tar_vect_is_mutation_direction : is_mutation_direction ΞΌ ΞΌ.tar_vect := by {use -1, simp, exact ΞΌ.vect_tar_src} lemma seed_mutation.tar_vect_eq_neg_src_vect {s s' : multiset N} (ΞΌ : seed_mutation s s') : ΞΌ.tar_vect = - ΞΌ.src_vect := ΞΌ.vect_tar_src lemma seed_mutation.src_vect_eq_neg_tar_vect {s s' : multiset N} (ΞΌ : seed_mutation s s') : ΞΌ.src_vect = - ΞΌ.tar_vect := by calc ΞΌ.src_vect = - - ΞΌ.src_vect : by rw neg_neg ... = - ΞΌ.tar_vect : by rw ΞΌ.vect_tar_src instance sign_tar_vect_is_mutation_direction : is_mutation_direction ΞΌ (ΞΌ.sign β€’ ΞΌ.tar_vect) := begin cases ΞΌ.is_sign with h h, repeat { any_goals {rw h <|> simp <|> apply_instance <|> rw ΞΌ.tar_vect_eq_neg_src_vect }}, end instance sign_src_vect_is_mutation_direction : is_mutation_direction ΞΌ (ΞΌ.sign β€’ ΞΌ.src_vect) := begin cases ΞΌ.is_sign with h h, repeat { any_goals {rw h <|> simp <|> apply_instance <|> rw ΞΌ.src_vect_eq_neg_tar_vect }}, end end direction section seed_mutation open skew_symmetric_form namespace seed_mutation @[simp] lemma form_mutation_direction_eq_zero {s s' : multiset N} (ΞΌ : seed_mutation s s') (v w : N) [is_mutation_direction ΞΌ v] [is_mutation_direction ΞΌ w] : form v w = 0 := begin cases ΞΌ.is_direction v with k hk, cases ΞΌ.is_direction w with l hl, rw [hk, hl], simp only [bilin_form.smul_right, bilin_form.smul_left, mul_eq_zero, self_eq_zero skew, or_true, eq_self_iff_true], end @[simp] lemma form_mutation_direction_eq_zero' {s s' : multiset N} (ΞΌ : seed_mutation s s') (v w : N) [is_mutation_direction ΞΌ v] [is_mutation_direction ΞΌ w] : B v w = 0 := begin simp only [bilin_form.to_lin_apply], exact form_mutation_direction_eq_zero ΞΌ v w, end end seed_mutation lemma pl_mutation_eq (v : N) {w : N} (Ξ΅ : β„€) (c : β„€) (h : w = c β€’ v) : pl_mutation v Ξ΅ w = w := begin unfold pl_mutation, rw h, simp only [add_right_eq_self, algebra.id.smul_eq_mul, bilin_form.to_lin_apply, linear_map.smul_apply, linear_map.map_smul, self_eq_zero skew, max_eq_right, eq_self_iff_true, zero_smul, mul_zero], end @[simp] lemma pl_mutation_eq' (v : N) (Ξ΅ : β„€) : pl_mutation v Ξ΅ v = v := pl_mutation_eq v Ξ΅ 1 (one_gsmul _).symm def seed_mutation.symm {s s' : multiset N} (ΞΌ : seed_mutation s s') : seed_mutation s' s := { sign := - ΞΌ.sign, is_sign := @is_sign.rec_on _ (is_sign (- ΞΌ.sign)) ΞΌ.is_sign (Ξ» h, h.symm β–Έ neg_one.is_sign) (Ξ» h, h.symm β–Έ one.is_sign), src_vect := ΞΌ.tar_vect, tar_vect := ΞΌ.src_vect, src_in := ΞΌ.tar_in, tar_in := ΞΌ.src_in, seed_tar_src := begin let h := ΞΌ.seed_tar_src, rw [multiset.map_erase] at h, rw [h, multiset.map_erase], simp only [function.comp_app, multiset.map_congr, multiset.map_map], rw [pl_mutation_eq ΞΌ.src_vect ΞΌ.sign 1, pl_mutation_eq ΞΌ.tar_vect (-ΞΌ.sign) (-1), ΞΌ.tar_vect_eq_neg_src_vect], simp only [id.def, multiset.map_id', eq_self_iff_true, multiset.map_congr, pl_mutation_neg_left_id], congr, apply eq.symm, apply multiset.map_id', any_goals {simp only [one_gsmul, neg_smul]}, apply ΞΌ.src_vect_eq_neg_tar_vect, exact function.bijective.injective (pl_mutation.bijective ΞΌ.tar_vect (-ΞΌ.sign)), exact function.bijective.injective (pl_mutation.bijective ΞΌ.src_vect ΞΌ.sign), end, vect_tar_src := ΞΌ.src_vect_eq_neg_tar_vect } end seed_mutation section function_of_vector def ring_of_function (N : Type*) [add_comm_group N] := add_monoid_algebra β„€ (module.dual β„€ N) local attribute [reducible, inline] add_monoid_algebra ring_of_function noncomputable instance : comm_ring (ring_of_function N) := add_monoid_algebra.comm_ring noncomputable instance : comm_ring (module.dual β„€ N β†’β‚€ β„€) := add_monoid_algebra.comm_ring open skew_symmetric_form noncomputable theory def function_of_vector (v : N) : (ring_of_function N) := finsupp.single 0 1 + finsupp.single (B v) 1 lemma function_of_vector_ne_zero (v : N) : function_of_vector v β‰  0 := begin unfold function_of_vector, let H := eq_or_ne (B v) 0, rw [ne.def] at H, cases H, rw [H, finsupp.nonzero_iff_exists], use 0, simp, rw finsupp.nonzero_iff_exists, use 0, simp only [finsupp.single_eq_same, pi.add_apply, finsupp.coe_add], have : ( 0 : module.dual β„€ N) βˆ‰ (finsupp.single (B v) 1 : ring_of_function N).support, { rw [finsupp.support_single_ne_zero, finset.mem_singleton, <- ne.def, ne_comm], exact H, simp }, rw finsupp.not_mem_support_iff at this, rw this, simp, end end function_of_vector section mutation_away local attribute [class] is_integral_domain variables {s s' : multiset N} (ΞΌ : seed_mutation s s') (S : Type*) [integral_domain S] [algebra (ring_of_function N) S] [is_localization.away (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect)) S] instance algebra_away : algebra (module.dual β„€ N β†’β‚€ β„€) S := by assumption open skew_symmetric_form variables (Ξ΅ : β„€) [is_sign Ξ΅] namespace seed_mutation def away_unit : units S := { val := algebra_map (ring_of_function N) S (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect)), inv := is_localization.mk' S 1 ⟨function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect), submonoid.mem_powers _⟩, val_inv := by rw [is_localization.mul_mk'_eq_mk'_of_mul, mul_one, is_localization.mk'_self], inv_val := by rw [mul_comm, is_localization.mul_mk'_eq_mk'_of_mul, mul_one, is_localization.mk'_self] } def monomial_to_away : multiplicative (module.dual β„€ N) β†’* S := { to_fun := Ξ» m, is_localization.mk' S (finsupp.single (multiplicative.to_add m) 1) (1 : submonoid.powers (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect))) * ↑((ΞΌ.away_unit S)^(Ξ΅ β€’ (-(multiplicative.to_add m) ΞΌ.src_vect))), map_one' := begin simp only [mul_one, algebra.id.smul_eq_mul, gpow_zero, units.coe_one, to_add_one, mul_zero, linear_map.zero_apply, neg_zero], rw [<- add_monoid_algebra.one_def, is_localization.mk'_one, ring_hom.map_one], end, map_mul' := Ξ» x y, begin simp only [algebra.id.smul_eq_mul, gpow_neg, mul_neg_eq_neg_mul_symm, neg_add_rev, linear_map.add_apply, to_add_mul], rw [<- one_mul (1 : β„€), <- add_monoid_algebra.single_mul_single], rw [<- one_mul (1 : submonoid.powers (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect))), is_localization.mk'_mul, mul_add, gpow_add], simp only [mul_one, gpow_neg, mul_neg_eq_neg_mul_symm, units.coe_mul], ring_exp, end } def to_away : ring_of_function N β†’+* S := add_monoid_algebra.lift_nc_ring_hom (int.cast_ring_hom S) (ΞΌ.monomial_to_away S Ξ΅) (Ξ» _ _, (commute.all _ _)) end seed_mutation @[simp] lemma to_away_of_function_of_mutation_direction (v : N) [is_mutation_direction ΞΌ v] : (ΞΌ.to_away S Ξ΅) (function_of_vector v) = is_localization.mk' S (function_of_vector v) (1 : submonoid.powers (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect))) := begin unfold seed_mutation.to_away function_of_vector seed_mutation.monomial_to_away add_monoid_algebra.lift_nc_ring_hom, cases ΞΌ.is_direction v with k hk, simp only [mul_one, ring_hom.eq_int_cast, one_mul, bilin_form.to_lin_apply, ring_hom.coe_mk, ring_hom.map_add, to_add_of_add, int.cast_one, ring_hom.coe_add_monoid_hom, monoid_hom.coe_mk, add_monoid_algebra.lift_nc_single, of_add_zero, monoid_hom.map_one, ΞΌ.form_mutation_direction_eq_zero, algebra.id.smul_eq_mul, gpow_zero, mul_zero, neg_zero, units.coe_one, mul_one], rw [is_localization.mk'_one, is_localization.mk'_one, <- add_monoid_algebra.one_def], simp only [ring_hom.map_add, add_left_inj, ring_hom.map_one], refl, end lemma is_unit_to_away : is_unit (ΞΌ.to_away S Ξ΅ (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect))) := begin rw to_away_of_function_of_mutation_direction, rw is_localization.mk'_one, refine @is_localization.map_units (ring_of_function N) _ _ S _ _ _ ⟨function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect), submonoid.mem_powers _⟩, end def seed_mutation.ring_hom_away : S β†’+* S := is_localization.away.lift (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect)) (is_unit_to_away ΞΌ S Ξ΅) @[simp] lemma mutation_away_map_const' : ((ΞΌ.ring_hom_away S Ξ΅).comp (algebra_map (ring_of_function N) S)).comp add_monoid_algebra.single_zero_ring_hom = (algebra_map (ring_of_function N) S ).comp add_monoid_algebra.single_zero_ring_hom := dec_trivial @[simp] lemma mutation_away_map_const (b : β„€) : ΞΌ.ring_hom_away S Ξ΅ ((algebra_map (ring_of_function N) S) (finsupp.single 0 b)) = algebra_map (ring_of_function N) S (finsupp.single 0 b) := begin have h : finsupp.single (0 : module.dual β„€ N) b = add_monoid_algebra.single_zero_ring_hom b := by refl, rw h, repeat {rw <- ring_hom.comp_apply}, repeat {rw <- ring_hom.coe_comp}, rw mutation_away_map_const', end @[simp] lemma mutation_away_map_monomial (a : multiplicative(module.dual β„€ N)) : (ΞΌ.ring_hom_away S Ξ΅) ((algebra_map (ring_of_function N) S) (finsupp.single a 1)) = algebra_map (ring_of_function N) S (finsupp.single a 1) * ↑((ΞΌ.away_unit S) ^ (Ξ΅ β€’ (- a ΞΌ.src_vect))) := begin unfold seed_mutation.ring_hom_away is_localization.away.lift, rw is_localization.lift_eq, unfold seed_mutation.to_away add_monoid_algebra.lift_nc_ring_hom, dsimp, rw add_monoid_algebra.lift_nc_single, unfold seed_mutation.monomial_to_away, dsimp, rw [int.cast_one, one_mul], simp only [gpow_neg, units.ne_zero, or_false, mul_neg_eq_neg_mul_symm, mul_eq_mul_right_iff], rw is_localization.mk'_one, congr, end @[simp] lemma mutation_away_eq_self_of_gpow_of_unit (k : β„€) : ΞΌ.ring_hom_away S Ξ΅ ↑((ΞΌ.away_unit S) ^ k ) = ↑((ΞΌ.away_unit S) ^ k) := begin unfold seed_mutation.ring_hom_away is_localization.away.lift seed_mutation.away_unit, induction k, { dsimp, rw [gpow_coe_nat, units.coe_pow, ring_hom.map_pow], dsimp, rw [is_localization.lift_eq], apply congr_arg (Ξ» x : S, x ^ k), rw to_away_of_function_of_mutation_direction, rw is_localization.mk'_one }, { rw [gpow_neg_succ_of_nat, <- inv_pow,units.coe_pow], rw [ring_hom.map_pow], apply congr_arg (Ξ» x : S, x ^ k.succ), simp only [units.coe_mk, units.inv_mk], rw is_localization.lift_mk'_spec, simp only [set_like.coe_mk, to_away_of_function_of_mutation_direction, ring_hom.map_one], rw <- is_localization.mk'_mul, rw [one_mul, mul_one, is_localization.mk'_self] }, end def seed_mutation.ring_equiv_away : S ≃+* S := ring_equiv.of_hom_inv (ΞΌ.ring_hom_away S Ξ΅) (ΞΌ.ring_hom_away S (-Ξ΅)) begin ext x, have : ring_hom.id S = is_localization.lift (@is_localization.map_units _ _ (submonoid.powers (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect))) S _ _ _), { ext z, rw ring_hom.id_apply, rw is_localization.lift_id }, rw this, rw is_localization.lift_unique, rw <- function.funext_iff, rw [<- function.comp, <- ring_hom.coe_comp, function.funext_iff, <- @ring_hom.ext_iff (ring_of_function N) S], apply add_monoid_algebra.ring_hom_ext, { intros a, repeat {rw ring_hom.coe_comp, rw function.comp}, dsimp, rw [mutation_away_map_const, mutation_away_map_const] }, { intros a, repeat {rw ring_hom.coe_comp, rw function.comp}, dsimp, rw [mutation_away_map_monomial, ring_hom.map_mul, mutation_away_map_monomial, mul_assoc], simp only [gpow_neg], rw mutation_away_eq_self_of_gpow_of_unit, simp only [algebra.id.smul_eq_mul, gpow_neg, mul_neg_eq_neg_mul_symm], simp only [neg_mul_eq_neg_mul_symm, gpow_neg, inv_inv], erw units.val_inv, apply mul_one }, end begin ext x, have : ring_hom.id S = is_localization.lift (@is_localization.map_units _ _ (submonoid.powers (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect))) S _ _ _), { ext z, rw ring_hom.id_apply, rw is_localization.lift_id }, rw this, rw is_localization.lift_unique, rw <- function.funext_iff, rw [<- function.comp, <- ring_hom.coe_comp, function.funext_iff, <- @ring_hom.ext_iff (ring_of_function N) S], apply add_monoid_algebra.ring_hom_ext, { intros a, repeat {rw ring_hom.coe_comp, rw function.comp}, dsimp, rw [mutation_away_map_const, mutation_away_map_const] }, { intros a, repeat {rw ring_hom.coe_comp, rw function.comp}, dsimp, rw [mutation_away_map_monomial, ring_hom.map_mul, mutation_away_map_monomial, mul_assoc], simp only [gpow_neg], rw mutation_away_eq_self_of_gpow_of_unit, simp only [algebra.id.smul_eq_mul, gpow_neg, mul_neg_eq_neg_mul_symm], simp only [neg_mul_eq_neg_mul_symm, gpow_neg, inv_inv], erw units.val_inv, apply mul_one }, end end mutation_away section mutation_frac variables [is_integral_domain (ring_of_function N)] {s s' : multiset N} (ΞΌ : seed_mutation s s') (K : Type*) [field K] [algebra (ring_of_function N) K] [is_fraction_ring (ring_of_function N) K] (Ξ΅ : β„€) [is_sign Ξ΅] local attribute [class] is_integral_domain def ring_of_function.integral_domain : integral_domain (ring_of_function N) := is_integral_domain.to_integral_domain _ (by assumption) local attribute [instance] ring_of_function.integral_domain abbreviation seed_mutation.away := localization.away (function_of_vector (ΞΌ.sign β€’ ΞΌ.src_vect)) def away.integral_domain : integral_domain ΞΌ.away := is_localization.integral_domain_of_le_non_zero_divisors ΞΌ.away (powers_le_non_zero_divisors_of_no_zero_divisors (function_of_vector_ne_zero (ΞΌ.sign β€’ ΞΌ.src_vect))) local attribute [instance] away.integral_domain def seed_mutation.algebra_of_away_frac : algebra ΞΌ.away K := is_localization.algebra_of_away_frac (function_of_vector_ne_zero (ΞΌ.sign β€’ ΞΌ.src_vect)) ΞΌ.away K local attribute[instance] seed_mutation.algebra_of_away_frac def seed_mutation.is_fraction_of_algebra_of_away_frac : @is_fraction_ring ΞΌ.away _ K _ (ΞΌ.algebra_of_away_frac K) := is_localization.is_fraction_of_algebra_of_away_frac _ ΞΌ.away K local attribute[instance] seed_mutation.is_fraction_of_algebra_of_away_frac private def z {K : Type*} [field K] [algebra (ring_of_function N) K] [is_fraction_ring (ring_of_function N) K] (m : module.dual β„€ N) := algebra_map (ring_of_function N) K (finsupp.single m 1) def seed_mutation.field_equiv : K ≃+* K := is_fraction_ring.field_equiv_of_ring_equiv (ΞΌ.ring_equiv_away ΞΌ.away 1) lemma mutation_field_equiv_map_monomial (m : module.dual β„€ N) : ΞΌ.field_equiv K (z m) = z m * (1 + z (B (ΞΌ.sign β€’ ΞΌ.src_vect))) ^ - m ΞΌ.src_vect := begin unfold z seed_mutation.field_equiv is_fraction_ring.field_equiv_of_ring_equiv seed_mutation.ring_equiv_away, let h_ne := function_of_vector_ne_zero (ΞΌ.sign β€’ ΞΌ.src_vect), repeat {rw is_localization.eq_comp_map_of_lift_of_of_away_frac h_ne ΞΌ.away K}, simp only [fpow_neg, linear_map.map_smul, is_localization.ring_equiv_of_ring_equiv_eq, mutation_away_map_monomial, algebra.id.smul_eq_mul, one_mul, gpow_neg, mul_eq_mul_left_iff, inv_inj', mul_neg_eq_neg_mul_symm, ring_hom.map_units_inv, ring_equiv.of_hom_inv_apply, ring_hom.map_mul], apply or.inl, unfold seed_mutation.away_unit function_of_vector, induction m ΞΌ.src_vect; simp only [ring_hom.map_add, units.coe_mk, gpow_neg_succ_of_nat, inv_inj', ring_hom.map_pow, ring_hom.map_units_inv, linear_map.map_smul, units.coe_pow, int.of_nat_eq_coe, gpow_coe_nat]; rw <- add_monoid_algebra.one_def; simp only [ring_hom.map_one], end end mutation_frac
5937aa9261b770bf935ac0232a51d914d70de9c7
690889011852559ee5ac4dfea77092de8c832e7e
/src/data/zsqrtd/gaussian_int.lean
1d2fa04df40c3528ae5ab012b4fe52f5c2754ed9
[ "Apache-2.0" ]
permissive
williamdemeo/mathlib
f6df180148f8acc91de9ba5e558976ab40a872c7
1fa03c29f9f273203bbffb79d10d31f696b3d317
refs/heads/master
1,584,785,260,929
1,572,195,914,000
1,572,195,913,000
138,435,193
0
0
Apache-2.0
1,529,789,739,000
1,529,789,739,000
null
UTF-8
Lean
false
false
7,234
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Chris Hughes The gaussian integers β„€[i]. -/ import data.zsqrtd.basic data.complex.basic algebra.euclidean_domain open zsqrtd complex @[reducible] def gaussian_int : Type := zsqrtd (-1) local notation `β„€[i]` := gaussian_int namespace gaussian_int instance : has_repr β„€[i] := ⟨λ x, "⟨" ++ repr x.re ++ ", " ++ repr x.im ++ "⟩"⟩ instance : comm_ring β„€[i] := zsqrtd.comm_ring def to_complex (x : β„€[i]) : β„‚ := x.re + x.im * I instance : has_coe (β„€[i]) β„‚ := ⟨to_complex⟩ lemma to_complex_def (x : β„€[i]) : (x : β„‚) = x.re + x.im * I := rfl lemma to_complex_def' (x y : β„€) : ((⟨x, y⟩ : β„€[i]) : β„‚) = x + y * I := by simp [to_complex_def] lemma to_complex_defβ‚‚ (x : β„€[i]) : (x : β„‚) = ⟨x.re, x.im⟩ := by apply complex.ext; simp [to_complex_def] instance to_complex.is_ring_hom : is_ring_hom to_complex:= by refine_struct {..}; intros; apply complex.ext; simp [to_complex] instance : is_ring_hom (coe : β„€[i] β†’ β„‚) := to_complex.is_ring_hom @[simp] lemma to_real_re (x : β„€[i]) : ((x.re : β„€) : ℝ) = (x : β„‚).re := by simp [to_complex_def] @[simp] lemma to_real_im (x : β„€[i]) : ((x.im : β„€) : ℝ) = (x : β„‚).im := by simp [to_complex_def] @[simp] lemma to_complex_re (x y : β„€) : ((⟨x, y⟩ : β„€[i]) : β„‚).re = x := by simp [to_complex_def] @[simp] lemma to_complex_im (x y : β„€) : ((⟨x, y⟩ : β„€[i]) : β„‚).im = y := by simp [to_complex_def] @[simp] lemma to_complex_add (x y : β„€[i]) : ((x + y : β„€[i]) : β„‚) = x + y := is_ring_hom.map_add coe @[simp] lemma to_complex_mul (x y : β„€[i]) : ((x * y : β„€[i]) : β„‚) = x * y := is_ring_hom.map_mul coe @[simp] lemma to_complex_one : ((1 : β„€[i]) : β„‚) = 1 := is_ring_hom.map_one coe @[simp] lemma to_complex_zero : ((0 : β„€[i]) : β„‚) = 0 := is_ring_hom.map_zero coe @[simp] lemma to_complex_neg (x : β„€[i]) : ((-x : β„€[i]) : β„‚) = -x := is_ring_hom.map_neg coe @[simp] lemma to_complex_sub (x y : β„€[i]) : ((x - y : β„€[i]) : β„‚) = x - y := is_ring_hom.map_sub coe @[simp] lemma to_complex_inj {x y : β„€[i]} : (x : β„‚) = y ↔ x = y := by cases x; cases y; simp [to_complex_defβ‚‚] @[simp] lemma to_complex_eq_zero {x : β„€[i]} : (x : β„‚) = 0 ↔ x = 0 := by rw [← to_complex_zero, to_complex_inj] @[simp] lemma nat_cast_real_norm (x : β„€[i]) : (x.norm : ℝ) = (x : β„‚).norm_sq := by rw [norm, norm_sq]; simp @[simp] lemma nat_cast_complex_norm (x : β„€[i]) : (x.norm : β„‚) = (x : β„‚).norm_sq := by cases x; rw [norm, norm_sq]; simp lemma norm_nonneg (x : β„€[i]) : 0 ≀ norm x := norm_nonneg trivial _ @[simp] lemma norm_eq_zero {x : β„€[i]} : norm x = 0 ↔ x = 0 := by rw [← @int.cast_inj ℝ _ _ _]; simp lemma norm_pos {x : β„€[i]} : 0 < norm x ↔ x β‰  0 := by rw [lt_iff_le_and_ne, ne.def, eq_comm, norm_eq_zero]; simp [norm_nonneg] @[simp] lemma coe_nat_abs_norm (x : β„€[i]) : (x.norm.nat_abs : β„€) = x.norm := int.nat_abs_of_nonneg (norm_nonneg _) @[simp] lemma nat_cast_nat_abs_norm {Ξ± : Type*} [ring Ξ±] (x : β„€[i]) : (x.norm.nat_abs : Ξ±) = x.norm := by rw [← int.cast_coe_nat, coe_nat_abs_norm] lemma nat_abs_norm_eq (x : β„€[i]) : x.norm.nat_abs = x.re.nat_abs * x.re.nat_abs + x.im.nat_abs * x.im.nat_abs := int.coe_nat_inj $ begin simp, simp [norm] end protected def div (x y : β„€[i]) : β„€[i] := let n := (rat.of_int (norm y))⁻¹ in let c := y.conj in ⟨round (rat.of_int (x * c).re * n : β„š), round (rat.of_int (x * c).im * n : β„š)⟩ instance : has_div β„€[i] := ⟨gaussian_int.div⟩ lemma div_def (x y : β„€[i]) : x / y = ⟨round ((x * conj y).re / norm y : β„š), round ((x * conj y).im / norm y : β„š)⟩ := show zsqrtd.mk _ _ = _, by simp [rat.of_int_eq_mk, rat.mk_eq_div, div_eq_mul_inv] lemma to_complex_div_re (x y : β„€[i]) : ((x / y : β„€[i]) : β„‚).re = round ((x / y : β„‚).re) := by rw [div_def, ← @rat.cast_round ℝ _ _]; simp [-rat.cast_round, mul_assoc, div_eq_mul_inv, mul_add, add_mul] lemma to_complex_div_im (x y : β„€[i]) : ((x / y : β„€[i]) : β„‚).im = round ((x / y : β„‚).im) := by rw [div_def, ← @rat.cast_round ℝ _ _, ← @rat.cast_round ℝ _ _]; simp [-rat.cast_round, mul_assoc, div_eq_mul_inv, mul_add, add_mul] local notation `abs'` := _root_.abs lemma norm_sq_le_norm_sq_of_re_le_of_im_le {x y : β„‚} (hre : abs' x.re ≀ abs' y.re) (him : abs' x.im ≀ abs' y.im) : x.norm_sq ≀ y.norm_sq := by rw [norm_sq, norm_sq, ← _root_.abs_mul_self, _root_.abs_mul, ← _root_.abs_mul_self y.re, _root_.abs_mul y.re, ← _root_.abs_mul_self x.im, _root_.abs_mul x.im, ← _root_.abs_mul_self y.im, _root_.abs_mul y.im]; exact (add_le_add (mul_self_le_mul_self (abs_nonneg _) hre) (mul_self_le_mul_self (abs_nonneg _) him)) lemma norm_sq_div_sub_div_lt_one (x y : β„€[i]) : ((x / y : β„‚) - ((x / y : β„€[i]) : β„‚)).norm_sq < 1 := calc ((x / y : β„‚) - ((x / y : β„€[i]) : β„‚)).norm_sq = ((x / y : β„‚).re - ((x / y : β„€[i]) : β„‚).re + ((x / y : β„‚).im - ((x / y : β„€[i]) : β„‚).im) * I : β„‚).norm_sq : congr_arg _ $ by apply complex.ext; simp ... ≀ (1 / 2 + 1 / 2 * I).norm_sq : have abs' (2 / (2 * 2) : ℝ) = 1 / 2, by rw _root_.abs_of_nonneg; norm_num, norm_sq_le_norm_sq_of_re_le_of_im_le (by rw [to_complex_div_re]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : β„‚).re) (by rw [to_complex_div_im]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : β„‚).im) ... < 1 : by simp [norm_sq]; norm_num protected def mod (x y : β„€[i]) : β„€[i] := x - y * (x / y) instance : has_mod β„€[i] := ⟨gaussian_int.mod⟩ lemma mod_def (x y : β„€[i]) : x % y = x - y * (x / y) := rfl lemma norm_mod_lt (x : β„€[i]) {y : β„€[i]} (hy : y β‰  0) : (x % y).norm < y.norm := have (y : β„‚) β‰  0, by rwa [ne.def, ← to_complex_zero, to_complex_inj], (@int.cast_lt ℝ _ _ _).1 $ calc ↑(norm (x % y)) = (x - y * (x / y : β„€[i]) : β„‚).norm_sq : by simp [mod_def] ... = (y : β„‚).norm_sq * (((x / y) - (x / y : β„€[i])) : β„‚).norm_sq : by rw [← norm_sq_mul, mul_sub, mul_div_cancel' _ this] ... < (y : β„‚).norm_sq * 1 : mul_lt_mul_of_pos_left (norm_sq_div_sub_div_lt_one _ _) (norm_sq_pos.2 this) ... = norm y : by simp lemma nat_abs_norm_mod_lt (x : β„€[i]) {y : β„€[i]} (hy : y β‰  0) : (x % y).norm.nat_abs < y.norm.nat_abs := int.coe_nat_lt.1 (by simp [-int.coe_nat_lt, norm_mod_lt x hy]) lemma norm_le_norm_mul_left (x : β„€[i]) {y : β„€[i]} (hy : y β‰  0) : (norm x).nat_abs ≀ (norm (x * y)).nat_abs := by rw [norm_mul, int.nat_abs_mul]; exact le_mul_of_one_le_right' (nat.zero_le _) (int.coe_nat_le.1 (by rw [coe_nat_abs_norm]; exact norm_pos.2 hy)) instance : nonzero_comm_ring β„€[i] := { zero_ne_one := dec_trivial, ..gaussian_int.comm_ring } instance : euclidean_domain β„€[i] := { quotient := (/), remainder := (%), quotient_zero := Ξ» _, by simp [div_def]; refl, quotient_mul_add_remainder_eq := Ξ» _ _, by simp [mod_def], r := _, r_well_founded := measure_wf (int.nat_abs ∘ norm), remainder_lt := nat_abs_norm_mod_lt, mul_left_not_lt := Ξ» a b hb0, not_lt_of_ge $ norm_le_norm_mul_left a hb0 } end gaussian_int
9f25cb39c810af72b769c0d70719e863a3b47b20
c777c32c8e484e195053731103c5e52af26a25d1
/src/group_theory/exponent.lean
f77a742a263136b727a6fd4673c9b035dfbf2ebe
[ "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
12,676
lean
/- Copyright (c) 2021 Julian Kuelshammer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Kuelshammer -/ import data.zmod.quotient import group_theory.noncomm_pi_coprod import group_theory.order_of_element import algebra.gcd_monoid.finset import data.nat.factorization.basic import tactic.by_contra /-! # Exponent of a group This file defines the exponent of a group, or more generally a monoid. For a group `G` it is defined to be the minimal `nβ‰₯1` such that `g ^ n = 1` for all `g ∈ G`. For a finite group `G`, it is equal to the lowest common multiple of the order of all elements of the group `G`. ## Main definitions * `monoid.exponent_exists` is a predicate on a monoid `G` saying that there is some positive `n` such that `g ^ n = 1` for all `g ∈ G`. * `monoid.exponent` defines the exponent of a monoid `G` as the minimal positive `n` such that `g ^ n = 1` for all `g ∈ G`, by convention it is `0` if no such `n` exists. * `add_monoid.exponent_exists` the additive version of `monoid.exponent_exists`. * `add_monoid.exponent` the additive version of `monoid.exponent`. ## Main results * `monoid.lcm_order_eq_exponent`: For a finite left cancel monoid `G`, the exponent is equal to the `finset.lcm` of the order of its elements. * `monoid.exponent_eq_supr_order_of(')`: For a commutative cancel monoid, the exponent is equal to `⨆ g : G, order_of g` (or zero if it has any order-zero elements). ## TODO * Refactor the characteristic of a ring to be the exponent of its underlying additive group. -/ universe u variable {G : Type u} open_locale classical namespace monoid section monoid variables (G) [monoid G] /--A predicate on a monoid saying that there is a positive integer `n` such that `g ^ n = 1` for all `g`.-/ @[to_additive "A predicate on an additive monoid saying that there is a positive integer `n` such that `n β€’ g = 0` for all `g`."] def exponent_exists := βˆƒ n, 0 < n ∧ βˆ€ g : G, g ^ n = 1 /--The exponent of a group is the smallest positive integer `n` such that `g ^ n = 1` for all `g ∈ G` if it exists, otherwise it is zero by convention.-/ @[to_additive "The exponent of an additive group is the smallest positive integer `n` such that `n β€’ g = 0` for all `g ∈ G` if it exists, otherwise it is zero by convention."] noncomputable def exponent := if h : exponent_exists G then nat.find h else 0 variable {G} @[to_additive] lemma exponent_exists_iff_ne_zero : exponent_exists G ↔ exponent G β‰  0 := begin rw [exponent], split_ifs, { simp [h, @not_lt_zero' β„•] }, --if this isn't done this way, `to_additive` freaks { tauto }, end @[to_additive] lemma exponent_eq_zero_iff : exponent G = 0 ↔ Β¬ exponent_exists G := by simp only [exponent_exists_iff_ne_zero, not_not] @[to_additive] lemma exponent_eq_zero_of_order_zero {g : G} (hg : order_of g = 0) : exponent G = 0 := exponent_eq_zero_iff.mpr $ Ξ» ⟨n, hn, hgn⟩, order_of_eq_zero_iff'.mp hg n hn $ hgn g @[to_additive exponent_nsmul_eq_zero] lemma pow_exponent_eq_one (g : G) : g ^ exponent G = 1 := begin by_cases exponent_exists G, { simp_rw [exponent, dif_pos h], exact (nat.find_spec h).2 g }, { simp_rw [exponent, dif_neg h, pow_zero] } end @[to_additive] lemma pow_eq_mod_exponent {n : β„•} (g : G): g ^ n = g ^ (n % exponent G) := calc g ^ n = g ^ (n % exponent G + exponent G * (n / exponent G)) : by rw [nat.mod_add_div] ... = g ^ (n % exponent G) : by simp [pow_add, pow_mul, pow_exponent_eq_one] @[to_additive] lemma exponent_pos_of_exists (n : β„•) (hpos : 0 < n) (hG : βˆ€ g : G, g ^ n = 1) : 0 < exponent G := begin have h : βˆƒ n, 0 < n ∧ βˆ€ g : G, g ^ n = 1 := ⟨n, hpos, hG⟩, rw [exponent, dif_pos], exact (nat.find_spec h).1, end @[to_additive] lemma exponent_min' (n : β„•) (hpos : 0 < n) (hG : βˆ€ g : G, g ^ n = 1) : exponent G ≀ n := begin rw [exponent, dif_pos], { apply nat.find_min', exact ⟨hpos, hG⟩ }, { exact ⟨n, hpos, hG⟩ }, end @[to_additive] lemma exponent_min (m : β„•) (hpos : 0 < m) (hm : m < exponent G) : βˆƒ g : G, g ^ m β‰  1 := begin by_contra' h, have hcon : exponent G ≀ m := exponent_min' m hpos h, linarith, end @[simp, to_additive] lemma exp_eq_one_of_subsingleton [subsingleton G] : exponent G = 1 := begin apply le_antisymm, { apply exponent_min' _ nat.one_pos, simp }, { apply nat.succ_le_of_lt, apply exponent_pos_of_exists 1 (nat.one_pos), simp }, end @[to_additive add_order_dvd_exponent] lemma order_dvd_exponent (g : G) : (order_of g) ∣ exponent G := order_of_dvd_of_pow_eq_one $ pow_exponent_eq_one g variable (G) @[to_additive] lemma exponent_dvd_of_forall_pow_eq_one (G) [monoid G] (n : β„•) (hG : βˆ€ g : G, g ^ n = 1) : exponent G ∣ n := begin rcases n.eq_zero_or_pos with rfl | hpos, { exact dvd_zero _ }, apply nat.dvd_of_mod_eq_zero, by_contradiction h, have h₁ := nat.pos_of_ne_zero h, have hβ‚‚ : n % exponent G < exponent G := nat.mod_lt _ (exponent_pos_of_exists n hpos hG), have h₃ : exponent G ≀ n % exponent G, { apply exponent_min' _ h₁, simp_rw ←pow_eq_mod_exponent, exact hG }, linarith, end @[to_additive lcm_add_order_of_dvd_exponent] lemma lcm_order_of_dvd_exponent [fintype G] : (finset.univ : finset G).lcm order_of ∣ exponent G := begin apply finset.lcm_dvd, intros g hg, exact order_dvd_exponent g end @[to_additive exists_order_of_eq_pow_padic_val_nat_add_exponent] lemma _root_.nat.prime.exists_order_of_eq_pow_factorization_exponent {p : β„•} (hp : p.prime) : βˆƒ g : G, order_of g = p ^ (exponent G).factorization p := begin haveI := fact.mk hp, rcases eq_or_ne ((exponent G).factorization p) 0 with h | h, { refine ⟨1, by rw [h, pow_zero, order_of_one]⟩ }, have he : 0 < exponent G := ne.bot_lt (Ξ» ht, by {rw ht at h, apply h, rw [bot_eq_zero, nat.factorization_zero, finsupp.zero_apply] }), rw ← finsupp.mem_support_iff at h, obtain ⟨g, hg⟩ : βˆƒ (g : G), g ^ (exponent G / p) β‰  1, { suffices key : Β¬ exponent G ∣ exponent G / p, { simpa using mt (exponent_dvd_of_forall_pow_eq_one G (exponent G / p)) key }, exact Ξ» hd, hp.one_lt.not_le ((mul_le_iff_le_one_left he).mp $ nat.le_of_dvd he $ nat.mul_dvd_of_dvd_div (nat.dvd_of_mem_factorization h) hd) }, obtain ⟨k, hk : exponent G = p ^ _ * k⟩ := nat.ord_proj_dvd _ _, obtain ⟨t, ht⟩ := nat.exists_eq_succ_of_ne_zero (finsupp.mem_support_iff.mp h), refine ⟨g ^ k, _⟩, rw ht, apply order_of_eq_prime_pow, { rwa [hk, mul_comm, ht, pow_succ', ←mul_assoc, nat.mul_div_cancel _ hp.pos, pow_mul] at hg }, { rw [←nat.succ_eq_add_one, ←ht, ←pow_mul, mul_comm, ←hk], exact pow_exponent_eq_one g }, end variable {G} @[to_additive] lemma exponent_ne_zero_iff_range_order_of_finite (h : βˆ€ g : G, 0 < order_of g) : exponent G β‰  0 ↔ (set.range (order_of : G β†’ β„•)).finite := begin refine ⟨λ he, _, Ξ» he, _⟩, { by_contra h, obtain ⟨m, ⟨t, rfl⟩, het⟩ := set.infinite.exists_gt h (exponent G), exact pow_ne_one_of_lt_order_of' he het (pow_exponent_eq_one t) }, { lift (set.range order_of) to finset β„• using he with t ht, have htpos : 0 < t.prod id, { refine finset.prod_pos (Ξ» a ha, _), rw [←finset.mem_coe, ht] at ha, obtain ⟨k, rfl⟩ := ha, exact h k }, suffices : exponent G ∣ t.prod id, { intro h, rw [h, zero_dvd_iff] at this, exact htpos.ne' this }, refine exponent_dvd_of_forall_pow_eq_one _ _ (Ξ» g, _), rw [pow_eq_mod_order_of, nat.mod_eq_zero_of_dvd, pow_zero g], apply finset.dvd_prod_of_mem, rw [←finset.mem_coe, ht], exact set.mem_range_self g }, end @[to_additive] lemma exponent_eq_zero_iff_range_order_of_infinite (h : βˆ€ g : G, 0 < order_of g) : exponent G = 0 ↔ (set.range (order_of : G β†’ β„•)).infinite := have _ := exponent_ne_zero_iff_range_order_of_finite h, by rwa [ne.def, not_iff_comm, iff.comm] at this @[to_additive lcm_add_order_eq_exponent] lemma lcm_order_eq_exponent [fintype G] : (finset.univ : finset G).lcm order_of = exponent G := begin apply nat.dvd_antisymm (lcm_order_of_dvd_exponent G), refine exponent_dvd_of_forall_pow_eq_one G _ (Ξ» g, _), obtain ⟨m, hm⟩ : order_of g ∣ finset.univ.lcm order_of := finset.dvd_lcm (finset.mem_univ g), rw [hm, pow_mul, pow_order_of_eq_one, one_pow] end end monoid section left_cancel_monoid variable [left_cancel_monoid G] @[to_additive] lemma exponent_ne_zero_of_finite [finite G] : exponent G β‰  0 := by { casesI nonempty_fintype G, simpa [←lcm_order_eq_exponent, finset.lcm_eq_zero_iff] using Ξ» x, (order_of_pos x).ne' } end left_cancel_monoid section comm_monoid variable [comm_monoid G] @[to_additive] lemma exponent_eq_supr_order_of (h : βˆ€ g : G, 0 < order_of g) : exponent G = ⨆ g : G, order_of g := begin rw supr, rcases eq_or_ne (exponent G) 0 with he | he, { rw [he, set.infinite.nat.Sup_eq_zero $ (exponent_eq_zero_iff_range_order_of_infinite h).1 he] }, have hne : (set.range (order_of : G β†’ β„•)).nonempty := ⟨1, 1, order_of_one⟩, have hfin : (set.range (order_of : G β†’ β„•)).finite, { rwa [← exponent_ne_zero_iff_range_order_of_finite h] }, obtain ⟨t, ht⟩ := hne.cSup_mem hfin, apply nat.dvd_antisymm _, { rw ←ht, apply order_dvd_exponent }, refine nat.dvd_of_factors_subperm he _, rw list.subperm_ext_iff, by_contra' h, obtain ⟨p, hp, hpe⟩ := h, replace hp := nat.prime_of_mem_factors hp, simp only [nat.factors_count_eq] at hpe, set k := (order_of t).factorization p with hk, obtain ⟨g, hg⟩ := hp.exists_order_of_eq_pow_factorization_exponent G, suffices : order_of t < order_of (t ^ (p ^ k) * g), { rw ht at this, exact this.not_le (le_cSup hfin.bdd_above $ set.mem_range_self _) }, have hpk : p ^ k ∣ order_of t := nat.ord_proj_dvd _ _, have hpk' : order_of (t ^ p ^ k) = order_of t / p ^ k, { rw [order_of_pow' t (pow_ne_zero k hp.ne_zero), nat.gcd_eq_right hpk] }, obtain ⟨a, ha⟩ := nat.exists_eq_add_of_lt hpe, have hcoprime : (order_of (t ^ p ^ k)).coprime (order_of g), { rw [hg, nat.coprime_pow_right_iff (pos_of_gt hpe), nat.coprime_comm], apply or.resolve_right (nat.coprime_or_dvd_of_prime hp _), nth_rewrite 0 ←pow_one p, convert nat.pow_succ_factorization_not_dvd (h $ t ^ p ^ k).ne' hp, rw [hpk', nat.factorization_div hpk], simp [hp] }, rw [(commute.all _ g).order_of_mul_eq_mul_order_of_of_coprime hcoprime, hpk', hg, ha, ←ht, ←hk, pow_add, pow_add, pow_one, ←mul_assoc, ←mul_assoc, nat.div_mul_cancel, mul_assoc, lt_mul_iff_one_lt_right $ h t, ←pow_succ'], exact one_lt_pow hp.one_lt a.succ_ne_zero, exact hpk end @[to_additive] lemma exponent_eq_supr_order_of' : exponent G = if βˆƒ g : G, order_of g = 0 then 0 else ⨆ g : G, order_of g := begin split_ifs, { obtain ⟨g, hg⟩ := h, exact exponent_eq_zero_of_order_zero hg }, { have := not_exists.mp h, exact exponent_eq_supr_order_of (Ξ» g, ne.bot_lt $ this g) } end end comm_monoid section cancel_comm_monoid variables [cancel_comm_monoid G] @[to_additive] lemma exponent_eq_max'_order_of [fintype G] : exponent G = ((@finset.univ G _).image order_of).max' ⟨1, by simp⟩ := begin rw [←finset.nonempty.cSup_eq_max', finset.coe_image, finset.coe_univ, set.image_univ, ← supr], exact exponent_eq_supr_order_of order_of_pos end end cancel_comm_monoid end monoid section comm_group open subgroup open_locale big_operators variables (G) [comm_group G] [group.fg G] @[to_additive] lemma card_dvd_exponent_pow_rank : nat.card G ∣ monoid.exponent G ^ group.rank G := begin obtain ⟨S, hS1, hS2⟩ := group.rank_spec G, rw [←hS1, ←fintype.card_coe, ←finset.card_univ, ←finset.prod_const], let f : (Ξ  g : S, zpowers (g : G)) β†’* G := noncomm_pi_coprod (Ξ» s t h x y hx hy, mul_comm x y), have hf : function.surjective f, { rw [←monoid_hom.range_top_iff_surjective, eq_top_iff, ←hS2, closure_le], exact Ξ» g hg, ⟨pi.mul_single ⟨g, hg⟩ ⟨g, mem_zpowers g⟩, noncomm_pi_coprod_mul_single _ _⟩ }, replace hf := nat_card_dvd_of_surjective f hf, rw nat.card_pi at hf, refine hf.trans (finset.prod_dvd_prod_of_dvd _ _ (Ξ» g hg, _)), rw ← order_eq_card_zpowers', exact monoid.order_dvd_exponent (g : G), end @[to_additive] lemma card_dvd_exponent_pow_rank' {n : β„•} (hG : βˆ€ g : G, g ^ n = 1) : nat.card G ∣ n ^ group.rank G := (card_dvd_exponent_pow_rank G).trans (pow_dvd_pow_of_dvd (monoid.exponent_dvd_of_forall_pow_eq_one G n hG) (group.rank G)) end comm_group
a94ab794b678a2af177d725f175da0ec78204290
b9a81ebb9de684db509231c4469a7d2c88915808
/src/super/subsumption.lean
d035b72414850cb6f1b0b6ceed7e59d9442a6d69
[]
no_license
leanprover/super
3dd81ce8d9ac3cba20bce55e84833fadb2f5716e
47b107b4cec8f3b41d72daba9cbda2f9d54025de
refs/heads/master
1,678,482,996,979
1,676,526,367,000
1,676,526,367,000
92,215,900
12
6
null
1,513,327,539,000
1,495,570,640,000
Lean
UTF-8
Lean
false
false
3,432
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .clause .prover_state open tactic monad native namespace super private meta def try_subsume_core : list clause.literal β†’ list clause.literal β†’ tactic unit | [] _ := skip | small large := first $ do i ← small.zip_with_index, j ← large.zip_with_index, return $ do unify_lit i.1 j.1, try_subsume_core (small.remove_nth i.2) (large.remove_nth j.2) -- FIXME: this is incorrect if a quantifier is unused meta def try_subsume (small large : clause) : tactic unit := do small_open ← clause.open_metan small (clause.num_quants small), large_open ← clause.open_constn large (clause.num_quants large), guard $ small.num_lits ≀ large.num_lits, try_subsume_core small_open.1.get_lits large_open.1.get_lits meta def does_subsume (small large : clause) : tactic bool := (try_subsume small large >> return tt) <|> return ff meta def does_subsume_with_assertions (small large : derived_clause) : prover bool := do if small.assertions.subset_of large.assertions then do does_subsume small.c large.c else return ff meta def any_tt {m : Type β†’ Type} [monad m] (active : rb_map clause_id derived_clause) (pred : derived_clause β†’ m bool) : m bool := active.fold (return ff) $ Ξ»k a cont, do v ← pred a, if v then return tt else cont meta def any_tt_list {m : Type β†’ Type} [monad m] {A} (pred : A β†’ m bool) : list A β†’ m bool | [] := return ff | (x::xs) := do v ← pred x, if v then return tt else any_tt_list xs @[super.inf] meta def forward_subsumption : inf_decl := inf_decl.mk 20 $ assume given, do active ← get_active, sequence' $ do a ← active.values, guard $ a.id β‰  given.id, return $ do ss ← does_subsume a.c given.c, if ss then remove_redundant given.id [a] else return () meta def forward_subsumption_pre : prover unit := preprocessing_rule $ Ξ»new, do active ← get_active, filter (Ξ»n, do do ss ← any_tt active (Ξ»a, if a.assertions.subset_of n.assertions then do does_subsume a.c n.c else -- TODO: move to locked return ff), return (bnot ss)) new meta def subsumption_interreduction : list derived_clause β†’ prover (list derived_clause) | (c::cs) := do -- TODO: move to locked cs_that_subsume_c ← filter (Ξ»d, does_subsume_with_assertions d c) cs, if Β¬cs_that_subsume_c.empty then -- TODO: update score subsumption_interreduction cs else do cs_not_subsumed_by_c ← filter (Ξ»d, bnot <$> does_subsume_with_assertions c d) cs, cs' ← subsumption_interreduction cs_not_subsumed_by_c, return (c::cs') | [] := return [] meta def subsumption_interreduction_pre : prover unit := preprocessing_rule $ Ξ»new, let new' := list.sort_on (Ξ»c : derived_clause, c.c.num_lits) new in subsumption_interreduction new' meta def keys_where_tt {m} {K V : Type} [monad m] (active : rb_map K V) (pred : V β†’ m bool) : m (list K) := @rb_map.fold _ _ (m (list K)) active (return []) $ Ξ»k a cont, do v ← pred a, rest ← cont, return $ if v then k::rest else rest @[super.inf] meta def backward_subsumption : inf_decl := inf_decl.mk 20 $ Ξ»given, do active ← get_active, ss ← keys_where_tt active (Ξ»a, does_subsume given.c a.c), sequence' $ do id ← ss, guard (id β‰  given.id), [remove_redundant id [given]] end super
85297aaa96ae2cc2cdcccbb3584a1d3bd9e6e90c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/galois_connection_auto.lean
848a4d2fb6545409ff1f52ca1db5160da282cb15
[]
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
29,257
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes HΓΆlzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.complete_lattice import Mathlib.order.rel_iso import Mathlib.PostPort universes u v x w u_1 u_2 l namespace Mathlib /-! # Galois connections, insertions and coinsertions Galois connections are order theoretic adjoints, i.e. a pair of functions `u` and `l`, such that `βˆ€a b, l a ≀ b ↔ a ≀ u b`. ## Main definitions * `galois_connection`: A Galois connection is a pair of functions `l` and `u` satisfying `l a ≀ b ↔ a ≀ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. * `galois_insertion`: A Galois insertion is a Galois connection where `l ∘ u = id` * `galois_coinsertion`: A Galois coinsertion is a Galois connection where `u ∘ l = id` ## Implementation details Galois insertions can be used to lift order structures from one type to another. For example if `Ξ±` is a complete lattice, and `l : Ξ± β†’ Ξ²`, and `u : Ξ² β†’ Ξ±` form a Galois insertion, then `Ξ²` is also a complete lattice. `l` is the lower adjoint and `u` is the upper adjoint. An example of this is the Galois insertion is in group thery. If `G` is a topological space, then there is a Galois insertion between the set of subsets of `G`, `set G`, and the set of subgroups of `G`, `subgroup G`. The left adjoint is `subgroup.closure`, taking the `subgroup` generated by a `set`, The right adjoint is the coercion from `subgroup G` to `set G`, taking the underlying set of an subgroup. Naively lifting a lattice structure along this Galois insertion would mean that the definition of `inf` on subgroups would be `subgroup.closure (↑S ∩ ↑T)`. This is an undesirable definition because the intersection of subgroups is already a subgroup, so there is no need to take the closure. For this reason a `choice` function is added as a field to the `galois_insertion` structure. It has type `Ξ  S : set G, ↑(closure S) ≀ S β†’ subgroup G`. When `↑(closure S) ≀ S`, then `S` is already a subgroup, so this function can be defined using `subgroup.mk` and not `closure`. This means the infimum of subgroups will be defined to be the intersection of sets, paired with a proof that intersection of subgroups is a subgroup, rather than the closure of the intersection. -/ /-- A Galois connection is a pair of functions `l` and `u` satisfying `l a ≀ b ↔ a ≀ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. -/ def galois_connection {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] (l : Ξ± β†’ Ξ²) (u : Ξ² β†’ Ξ±) := βˆ€ (a : Ξ±) (b : Ξ²), l a ≀ b ↔ a ≀ u b /-- Makes a Galois connection from an order-preserving bijection. -/ theorem order_iso.to_galois_connection {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] (oi : Ξ± ≃o Ξ²) : galois_connection ⇑oi ⇑(order_iso.symm oi) := fun (b : Ξ±) (g : Ξ²) => iff.symm (rel_iso.rel_symm_apply oi) namespace galois_connection theorem monotone_intro {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (hu : monotone u) (hl : monotone l) (hul : βˆ€ (a : Ξ±), a ≀ u (l a)) (hlu : βˆ€ (a : Ξ²), l (u a) ≀ a) : galois_connection l u := fun (a : Ξ±) (b : Ξ²) => { mp := fun (h : l a ≀ b) => le_trans (hul a) (hu h), mpr := fun (h : a ≀ u b) => le_trans (hl h) (hlu b) } theorem l_le {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {a : Ξ±} {b : Ξ²} : a ≀ u b β†’ l a ≀ b := iff.mpr (gc a b) theorem le_u {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {a : Ξ±} {b : Ξ²} : l a ≀ b β†’ a ≀ u b := iff.mp (gc a b) theorem le_u_l {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) (a : Ξ±) : a ≀ u (l a) := le_u gc (le_refl (l a)) theorem l_u_le {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) (a : Ξ²) : l (u a) ≀ a := l_le gc (le_refl (u a)) theorem monotone_u {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : monotone u := fun (a b : Ξ²) (H : a ≀ b) => le_u gc (le_trans (l_u_le gc a) H) theorem monotone_l {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : monotone l := fun (a b : Ξ±) (H : a ≀ b) => l_le gc (le_trans H (le_u_l gc b)) theorem upper_bounds_l_image_subset {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {s : set Ξ±} : upper_bounds (l '' s) βŠ† u ⁻¹' upper_bounds s := fun (b : Ξ²) (hb : b ∈ upper_bounds (l '' s)) (c : Ξ±) (this : c ∈ s) => le_u gc (hb (set.mem_image_of_mem l this)) theorem lower_bounds_u_image_subset {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {s : set Ξ²} : lower_bounds (u '' s) βŠ† l ⁻¹' lower_bounds s := fun (a : Ξ±) (ha : a ∈ lower_bounds (u '' s)) (c : Ξ²) (this : c ∈ s) => l_le gc (ha (set.mem_image_of_mem u this)) theorem is_lub_l_image {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {s : set Ξ±} {a : Ξ±} (h : is_lub s a) : is_lub (l '' s) (l a) := sorry theorem is_glb_u_image {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {s : set Ξ²} {b : Ξ²} (h : is_glb s b) : is_glb (u '' s) (u b) := sorry theorem is_glb_l {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {a : Ξ±} : is_glb (set_of fun (b : Ξ²) => a ≀ u b) (l a) := { left := fun (b : Ξ²) => l_le gc, right := fun (b : Ξ²) (h : b ∈ lower_bounds (set_of fun (b : Ξ²) => a ≀ u b)) => h (le_u_l gc a) } theorem is_lub_u {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {b : Ξ²} : is_lub (set_of fun (a : Ξ±) => l a ≀ b) (u b) := { left := fun (b_1 : Ξ±) => le_u gc, right := fun (b_1 : Ξ±) (h : b_1 ∈ upper_bounds (set_of fun (a : Ξ±) => l a ≀ b)) => h (l_u_le gc b) } theorem u_l_u_eq_u {Ξ± : Type u} {Ξ² : Type v} [partial_order Ξ±] [partial_order Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : u ∘ l ∘ u = u := funext fun (x : Ξ²) => le_antisymm (monotone_u gc (l_u_le gc x)) (le_u_l gc (u x)) theorem l_u_l_eq_l {Ξ± : Type u} {Ξ² : Type v} [partial_order Ξ±] [partial_order Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : l ∘ u ∘ l = l := funext fun (x : Ξ±) => le_antisymm (l_u_le gc (l x)) (monotone_l gc (le_u_l gc x)) theorem l_unique {Ξ± : Type u} {Ξ² : Type v} [partial_order Ξ±] [partial_order Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {l' : Ξ± β†’ Ξ²} {u' : Ξ² β†’ Ξ±} (gc' : galois_connection l' u') (hu : βˆ€ (b : Ξ²), u b = u' b) {a : Ξ±} : l a = l' a := le_antisymm (l_le gc (Eq.symm (hu (l' a)) β–Έ le_u_l gc' a)) (l_le gc' (hu (l a) β–Έ le_u_l gc a)) theorem u_unique {Ξ± : Type u} {Ξ² : Type v} [partial_order Ξ±] [partial_order Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {l' : Ξ± β†’ Ξ²} {u' : Ξ² β†’ Ξ±} (gc' : galois_connection l' u') (hl : βˆ€ (a : Ξ±), l a = l' a) {b : Ξ²} : u b = u' b := le_antisymm (le_u gc' (hl (u b) β–Έ l_u_le gc b)) (le_u gc (Eq.symm (hl (u' b)) β–Έ l_u_le gc' b)) theorem u_top {Ξ± : Type u} {Ξ² : Type v} [order_top Ξ±] [order_top Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : u ⊀ = ⊀ := sorry theorem l_bot {Ξ± : Type u} {Ξ² : Type v} [order_bot Ξ±] [order_bot Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : l βŠ₯ = βŠ₯ := sorry theorem l_sup {Ξ± : Type u} {Ξ² : Type v} {a₁ : Ξ±} {aβ‚‚ : Ξ±} [semilattice_sup Ξ±] [semilattice_sup Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : l (a₁ βŠ” aβ‚‚) = l a₁ βŠ” l aβ‚‚ := sorry theorem u_inf {Ξ± : Type u} {Ξ² : Type v} {b₁ : Ξ²} {bβ‚‚ : Ξ²} [semilattice_inf Ξ±] [semilattice_inf Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : u (b₁ βŠ“ bβ‚‚) = u b₁ βŠ“ u bβ‚‚ := sorry theorem l_supr {Ξ± : Type u} {Ξ² : Type v} {ΞΉ : Sort x} [complete_lattice Ξ±] [complete_lattice Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {f : ΞΉ β†’ Ξ±} : l (supr f) = supr fun (i : ΞΉ) => l (f i) := sorry theorem u_infi {Ξ± : Type u} {Ξ² : Type v} {ΞΉ : Sort x} [complete_lattice Ξ±] [complete_lattice Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {f : ΞΉ β†’ Ξ²} : u (infi f) = infi fun (i : ΞΉ) => u (f i) := sorry theorem l_Sup {Ξ± : Type u} {Ξ² : Type v} [complete_lattice Ξ±] [complete_lattice Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {s : set Ξ±} : l (Sup s) = supr fun (a : Ξ±) => supr fun (H : a ∈ s) => l a := sorry theorem u_Inf {Ξ± : Type u} {Ξ² : Type v} [complete_lattice Ξ±] [complete_lattice Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) {s : set Ξ²} : u (Inf s) = infi fun (a : Ξ²) => infi fun (H : a ∈ s) => u a := sorry /- Constructing Galois connections -/ protected theorem id {Ξ± : Type u} [pΞ± : preorder Ξ±] : galois_connection id id := fun (a b : Ξ±) => { mp := fun (x : id a ≀ b) => x, mpr := fun (x : a ≀ id b) => x } protected theorem compose {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [preorder Ξ±] [preorder Ξ²] [preorder Ξ³] (l1 : Ξ± β†’ Ξ²) (u1 : Ξ² β†’ Ξ±) (l2 : Ξ² β†’ Ξ³) (u2 : Ξ³ β†’ Ξ²) (gc1 : galois_connection l1 u1) (gc2 : galois_connection l2 u2) : galois_connection (l2 ∘ l1) (u1 ∘ u2) := sorry protected theorem dual {Ξ± : Type u} {Ξ² : Type v} [pΞ± : preorder Ξ±] [pΞ² : preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : galois_connection u l := fun (a : order_dual Ξ²) (b : order_dual Ξ±) => iff.symm (gc b a) protected theorem dfun {ΞΉ : Type u} {Ξ± : ΞΉ β†’ Type v} {Ξ² : ΞΉ β†’ Type w} [(i : ΞΉ) β†’ preorder (Ξ± i)] [(i : ΞΉ) β†’ preorder (Ξ² i)] (l : (i : ΞΉ) β†’ Ξ± i β†’ Ξ² i) (u : (i : ΞΉ) β†’ Ξ² i β†’ Ξ± i) (gc : βˆ€ (i : ΞΉ), galois_connection (l i) (u i)) : galois_connection (fun (a : (i : ΞΉ) β†’ Ξ± i) (i : ΞΉ) => l i (a i)) fun (b : (i : ΞΉ) β†’ Ξ² i) (i : ΞΉ) => u i (b i) := fun (a : (i : ΞΉ) β†’ Ξ± i) (b : (i : ΞΉ) β†’ Ξ² i) => forall_congr fun (i : ΞΉ) => gc i (a i) (b i) end galois_connection namespace nat theorem galois_connection_mul_div {k : β„•} (h : 0 < k) : galois_connection (fun (n : β„•) => n * k) fun (n : β„•) => n / k := fun (x y : β„•) => iff.symm (le_div_iff_mul_le x y h) end nat /-- A Galois insertion is a Galois connection where `l ∘ u = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_coinsertion` -/ structure galois_insertion {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] (l : Ξ± β†’ Ξ²) (u : Ξ² β†’ Ξ±) where choice : (x : Ξ±) β†’ u (l x) ≀ x β†’ Ξ² gc : galois_connection l u le_l_u : βˆ€ (x : Ξ²), x ≀ l (u x) choice_eq : βˆ€ (a : Ξ±) (h : u (l a) ≀ a), choice a h = l a /-- A constructor for a Galois insertion with the trivial `choice` function. -/ def galois_insertion.monotone_intro {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (hu : monotone u) (hl : monotone l) (hul : βˆ€ (a : Ξ±), a ≀ u (l a)) (hlu : βˆ€ (b : Ξ²), l (u b) = b) : galois_insertion l u := galois_insertion.mk (fun (x : Ξ±) (_x : u (l x) ≀ x) => l x) sorry sorry sorry /-- Makes a Galois insertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_insertion {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] (oi : Ξ± ≃o Ξ²) : galois_insertion ⇑oi ⇑(order_iso.symm oi) := galois_insertion.mk (fun (b : Ξ±) (h : coe_fn (order_iso.symm oi) (coe_fn oi b) ≀ b) => coe_fn oi b) (order_iso.to_galois_connection oi) sorry sorry /-- Make a `galois_insertion l u` from a `galois_connection l u` such that `βˆ€ b, b ≀ l (u b)` -/ def galois_connection.to_galois_insertion {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) (h : βˆ€ (b : Ξ²), b ≀ l (u b)) : galois_insertion l u := galois_insertion.mk (fun (x : Ξ±) (_x : u (l x) ≀ x) => l x) gc h sorry /-- Lift the bottom along a Galois connection -/ def galois_connection.lift_order_bot {Ξ± : Type u_1} {Ξ² : Type u_2} [order_bot Ξ±] [partial_order Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : order_bot Ξ² := order_bot.mk (l βŠ₯) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry namespace galois_insertion theorem l_u_eq {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) (b : Ξ²) : l (u b) = b := le_antisymm (galois_connection.l_u_le (gc gi) b) (le_l_u gi b) theorem l_surjective {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) : function.surjective l := fun (b : Ξ²) => Exists.intro (u b) (l_u_eq gi b) theorem u_injective {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) : function.injective u := fun (a b : Ξ²) (h : u a = u b) => Eq.trans (Eq.trans (Eq.symm (l_u_eq gi a)) (congr_arg l h)) (l_u_eq gi b) theorem l_sup_u {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [semilattice_sup Ξ±] [semilattice_sup Ξ²] (gi : galois_insertion l u) (a : Ξ²) (b : Ξ²) : l (u a βŠ” u b) = a βŠ” b := sorry theorem l_supr_u {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ²) : l (supr fun (i : ΞΉ) => u (f i)) = supr fun (i : ΞΉ) => f i := Eq.trans (galois_connection.l_supr (gc gi)) (congr_arg supr (funext fun (i : ΞΉ) => l_u_eq gi (f i))) theorem l_supr_of_ul_eq_self {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ±) (hf : βˆ€ (i : ΞΉ), u (l (f i)) = f i) : l (supr fun (i : ΞΉ) => f i) = supr fun (i : ΞΉ) => l (f i) := sorry theorem l_inf_u {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [semilattice_inf Ξ±] [semilattice_inf Ξ²] (gi : galois_insertion l u) (a : Ξ²) (b : Ξ²) : l (u a βŠ“ u b) = a βŠ“ b := sorry theorem l_infi_u {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ²) : l (infi fun (i : ΞΉ) => u (f i)) = infi fun (i : ΞΉ) => f i := Eq.trans (congr_arg l (Eq.symm (galois_connection.u_infi (gc gi)))) (l_u_eq gi (infi fun (i : ΞΉ) => f i)) theorem l_infi_of_ul_eq_self {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ±) (hf : βˆ€ (i : ΞΉ), u (l (f i)) = f i) : l (infi fun (i : ΞΉ) => f i) = infi fun (i : ΞΉ) => l (f i) := sorry theorem u_le_u_iff {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [preorder Ξ±] [preorder Ξ²] (gi : galois_insertion l u) {a : Ξ²} {b : Ξ²} : u a ≀ u b ↔ a ≀ b := { mp := fun (h : u a ≀ u b) => le_trans (le_l_u gi a) (galois_connection.l_le (gc gi) h), mpr := fun (h : a ≀ b) => galois_connection.monotone_u (gc gi) h } theorem strict_mono_u {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) : strict_mono u := strict_mono_of_le_iff_le fun (_x _x_1 : Ξ²) => iff.symm (u_le_u_iff gi) /-- Lift the suprema along a Galois insertion -/ def lift_semilattice_sup {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ²] [semilattice_sup Ξ±] (gi : galois_insertion l u) : semilattice_sup Ξ² := semilattice_sup.mk (fun (a b : Ξ²) => l (u a βŠ” u b)) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry sorry sorry /-- Lift the infima along a Galois insertion -/ def lift_semilattice_inf {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ²] [semilattice_inf Ξ±] (gi : galois_insertion l u) : semilattice_inf Ξ² := semilattice_inf.mk (fun (a b : Ξ²) => choice gi (u a βŠ“ u b) sorry) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry sorry sorry /-- Lift the suprema and infima along a Galois insertion -/ def lift_lattice {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ²] [lattice Ξ±] (gi : galois_insertion l u) : lattice Ξ² := lattice.mk semilattice_sup.sup semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry /-- Lift the top along a Galois insertion -/ def lift_order_top {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ²] [order_top Ξ±] (gi : galois_insertion l u) : order_top Ξ² := order_top.mk (choice gi ⊀ sorry) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry /-- Lift the top, bottom, suprema, and infima along a Galois insertion -/ def lift_bounded_lattice {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ²] [bounded_lattice Ξ±] (gi : galois_insertion l u) : bounded_lattice Ξ² := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry order_top.top sorry order_bot.bot sorry /-- Lift all suprema and infima along a Galois insertion -/ def lift_complete_lattice {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ²] [complete_lattice Ξ±] (gi : galois_insertion l u) : complete_lattice Ξ² := complete_lattice.mk bounded_lattice.sup bounded_lattice.le bounded_lattice.lt sorry sorry sorry sorry sorry sorry bounded_lattice.inf sorry sorry sorry bounded_lattice.top sorry bounded_lattice.bot sorry (fun (s : set Ξ²) => l (supr fun (b : Ξ²) => supr fun (H : b ∈ s) => u b)) (fun (s : set Ξ²) => choice gi (infi fun (b : Ξ²) => infi fun (H : b ∈ s) => u b) sorry) sorry sorry sorry sorry end galois_insertion /-- A Galois coinsertion is a Galois connection where `u ∘ l = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_insertion` -/ structure galois_coinsertion {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] (l : Ξ± β†’ Ξ²) (u : Ξ² β†’ Ξ±) where choice : (x : Ξ²) β†’ x ≀ l (u x) β†’ Ξ± gc : galois_connection l u u_l_le : βˆ€ (x : Ξ±), u (l x) ≀ x choice_eq : βˆ€ (a : Ξ²) (h : a ≀ l (u a)), choice a h = u a /-- Make a `galois_insertion u l` in the `order_dual`, from a `galois_coinsertion l u` -/ def galois_coinsertion.dual {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} : galois_coinsertion l u β†’ galois_insertion u l := fun (x : galois_coinsertion l u) => galois_insertion.mk (galois_coinsertion.choice x) sorry (galois_coinsertion.u_l_le x) (galois_coinsertion.choice_eq x) /-- Make a `galois_coinsertion u l` in the `order_dual`, from a `galois_insertion l u` -/ def galois_insertion.dual {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} : galois_insertion l u β†’ galois_coinsertion u l := fun (x : galois_insertion l u) => galois_coinsertion.mk (galois_insertion.choice x) sorry (galois_insertion.le_l_u x) (galois_insertion.choice_eq x) /-- Make a `galois_coinsertion l u` from a `galois_insertion l u` in the `order_dual` -/ def galois_coinsertion.of_dual {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} : galois_insertion u l β†’ galois_coinsertion l u := fun (x : galois_insertion u l) => galois_coinsertion.mk (galois_insertion.choice x) sorry sorry sorry /-- Make a `galois_insertion l u` from a `galois_coinsertion l u` in the `order_dual` -/ def galois_insertion.of_dual {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} : galois_coinsertion u l β†’ galois_insertion l u := fun (x : galois_coinsertion u l) => galois_insertion.mk (galois_coinsertion.choice x) sorry sorry sorry /-- Makes a Galois coinsertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_coinsertion {Ξ± : Type u} {Ξ² : Type v} [preorder Ξ±] [preorder Ξ²] (oi : Ξ± ≃o Ξ²) : galois_coinsertion ⇑oi ⇑(order_iso.symm oi) := galois_coinsertion.mk (fun (b : Ξ²) (h : b ≀ coe_fn oi (coe_fn (order_iso.symm oi) b)) => coe_fn (order_iso.symm oi) b) (order_iso.to_galois_connection oi) sorry sorry /-- A constructor for a Galois coinsertion with the trivial `choice` function. -/ def galois_coinsertion.monotone_intro {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (hu : monotone u) (hl : monotone l) (hlu : βˆ€ (b : Ξ²), l (u b) ≀ b) (hul : βˆ€ (a : Ξ±), u (l a) = a) : galois_coinsertion l u := galois_coinsertion.of_dual (galois_insertion.monotone_intro (monotone.order_dual hl) (monotone.order_dual hu) hlu hul) /-- Make a `galois_coinsertion l u` from a `galois_connection l u` such that `βˆ€ b, b ≀ l (u b)` -/ def galois_connection.to_galois_coinsertion {Ξ± : Type u_1} {Ξ² : Type u_2} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) (h : βˆ€ (a : Ξ±), u (l a) ≀ a) : galois_coinsertion l u := galois_coinsertion.mk (fun (x : Ξ²) (_x : x ≀ l (u x)) => u x) gc h sorry /-- Lift the top along a Galois connection -/ def galois_connection.lift_order_top {Ξ± : Type u_1} {Ξ² : Type u_2} [partial_order Ξ±] [order_top Ξ²] {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} (gc : galois_connection l u) : order_top Ξ± := order_top.mk (u ⊀) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry namespace galois_coinsertion theorem u_l_eq {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) (a : Ξ±) : u (l a) = a := galois_insertion.l_u_eq (dual gi) a theorem u_surjective {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) : function.surjective u := galois_insertion.l_surjective (dual gi) theorem l_injective {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) : function.injective l := galois_insertion.u_injective (dual gi) theorem u_inf_l {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [semilattice_inf Ξ±] [semilattice_inf Ξ²] (gi : galois_coinsertion l u) (a : Ξ±) (b : Ξ±) : u (l a βŠ“ l b) = a βŠ“ b := galois_insertion.l_sup_u (dual gi) a b theorem u_infi_l {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ±) : u (infi fun (i : ΞΉ) => l (f i)) = infi fun (i : ΞΉ) => f i := galois_insertion.l_supr_u (dual gi) fun (i : ΞΉ) => f i theorem u_infi_of_lu_eq_self {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ²) (hf : βˆ€ (i : ΞΉ), l (u (f i)) = f i) : u (infi fun (i : ΞΉ) => f i) = infi fun (i : ΞΉ) => u (f i) := galois_insertion.l_supr_of_ul_eq_self (dual gi) (fun (i : ΞΉ) => f i) hf theorem u_sup_l {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [semilattice_sup Ξ±] [semilattice_sup Ξ²] (gi : galois_coinsertion l u) (a : Ξ±) (b : Ξ±) : u (l a βŠ” l b) = a βŠ” b := galois_insertion.l_inf_u (dual gi) a b theorem u_supr_l {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ±) : u (supr fun (i : ΞΉ) => l (f i)) = supr fun (i : ΞΉ) => f i := galois_insertion.l_infi_u (dual gi) fun (i : ΞΉ) => f i theorem u_supr_of_lu_eq_self {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u) {ΞΉ : Sort x} (f : ΞΉ β†’ Ξ²) (hf : βˆ€ (i : ΞΉ), l (u (f i)) = f i) : u (supr fun (i : ΞΉ) => f i) = supr fun (i : ΞΉ) => u (f i) := galois_insertion.l_infi_of_ul_eq_self (dual gi) (fun (i : ΞΉ) => f i) hf theorem l_le_l_iff {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [preorder Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) {a : Ξ±} {b : Ξ±} : l a ≀ l b ↔ a ≀ b := galois_insertion.u_le_u_iff (dual gi) theorem strict_mono_l {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) : strict_mono l := fun (a b : Ξ±) (h : a < b) => galois_insertion.strict_mono_u (dual gi) h /-- Lift the infima along a Galois coinsertion -/ def lift_semilattice_inf {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [semilattice_inf Ξ²] (gi : galois_coinsertion l u) : semilattice_inf Ξ± := semilattice_inf.mk (fun (a b : Ξ±) => u (l a βŠ“ l b)) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry sorry sorry /-- Lift the suprema along a Galois coinsertion -/ def lift_semilattice_sup {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [semilattice_sup Ξ²] (gi : galois_coinsertion l u) : semilattice_sup Ξ± := semilattice_sup.mk (fun (a b : Ξ±) => choice gi (l a βŠ” l b) sorry) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry sorry sorry /-- Lift the suprema and infima along a Galois coinsertion -/ def lift_lattice {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [lattice Ξ²] (gi : galois_coinsertion l u) : lattice Ξ± := lattice.mk semilattice_sup.sup semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry /-- Lift the bot along a Galois coinsertion -/ def lift_order_bot {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [order_bot Ξ²] (gi : galois_coinsertion l u) : order_bot Ξ± := order_bot.mk (choice gi βŠ₯ sorry) partial_order.le partial_order.lt partial_order.le_refl partial_order.le_trans partial_order.le_antisymm sorry /-- Lift the top, bottom, suprema, and infima along a Galois coinsertion -/ def lift_bounded_lattice {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [bounded_lattice Ξ²] (gi : galois_coinsertion l u) : bounded_lattice Ξ± := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry order_top.top sorry order_bot.bot sorry /-- Lift all suprema and infima along a Galois coinsertion -/ def lift_complete_lattice {Ξ± : Type u} {Ξ² : Type v} {l : Ξ± β†’ Ξ²} {u : Ξ² β†’ Ξ±} [partial_order Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u) : complete_lattice Ξ± := complete_lattice.mk bounded_lattice.sup bounded_lattice.le bounded_lattice.lt sorry sorry sorry sorry sorry sorry bounded_lattice.inf sorry sorry sorry bounded_lattice.top sorry bounded_lattice.bot sorry (fun (s : set Ξ±) => choice gi (supr fun (a : Ξ±) => supr fun (H : a ∈ s) => l a) sorry) (fun (s : set Ξ±) => u (infi fun (a : Ξ±) => infi fun (H : a ∈ s) => l a)) sorry sorry sorry sorry end galois_coinsertion /-- If `Ξ±` is a partial order with bottom element (e.g., `β„•`, `ℝβ‰₯0`), then `Ξ» o : with_bot Ξ±, o.get_or_else βŠ₯` and coercion form a Galois insertion. -/ def with_bot.gi_get_or_else_bot {Ξ± : Type u} [order_bot Ξ±] : galois_insertion (fun (o : with_bot Ξ±) => option.get_or_else o βŠ₯) coe := galois_insertion.mk (fun (o : with_bot Ξ±) (ho : ↑(option.get_or_else o βŠ₯) ≀ o) => option.get_or_else o βŠ₯) sorry sorry sorry end Mathlib
8b6e39d90f19e506d0b6cfe286e784a2f3dd2dc9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Util/Trace.lean
17530a4d92d86f3723180b3e5abe9f83e5fb9a67
[ "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
10,590
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich, Leonardo de Moura -/ import Lean.Exception /-! # Trace messages Trace messages explain to the user what problem Lean solved and what steps it took. Think of trace messages like a figure in a paper. They are shown in the editor as a collapsible tree, usually as `[class.name] message β–Έ`. Every trace node has a class name, a message, and an array of children. This module provides the API to produce trace messages, the key entry points are: - ``registerTraceMessage `class.name`` registers a trace class - ``withTraceNode `class.name (fun result => return msg) do body` produces a trace message containing the trace messages in `body` as children - `trace[class.name] msg` produces a trace message without children Users can enable trace options for a class using `set_option trace.class.name true`. This only enables trace messages for the `class.name` trace class as well as child classes that are explicitly marked as inherited (see `registerTraceClass`). Internally, trace messages are stored as `MessageData`: `.trace cls msg #[.trace .., .trace ..]` When writing trace messages, try to follow these guidelines: 1. **Expansion progressively increases detail.** Each level of expansion (of the trace node in the editor) should reveal more details. For example, the unexpanded node should show the top-level goal. Expanding it should show a list of steps. Expanding one of the steps then shows what happens during that step. 2. **One trace message per step.** The `[trace.class]` marker functions as a visual bullet point, making it easy to identify the different steps at a glance. 3. **Outcome first.** The top-level trace message should already show whether the action failed or succeeded, as opposed to a "success" trace message that comes pages later. 4. **Be concise.** Keep messages short. Avoid repetitive text. (This is also why the editor plugins abbreviate the common prefixes.) 5. **Emoji are concisest.** Several helper functions in this module help with a consistent emoji language. 6. **Good defaults.** Setting `set_option trace.Meta.synthInstance true` (etc.) should produce great trace messages out-of-the-box, without needing extra options to tweak it. -/ namespace Lean structure TraceElem where ref : Syntax msg : MessageData deriving Inhabited structure TraceState where traces : PersistentArray TraceElem := {} deriving Inhabited builtin_initialize inheritedTraceOptions : IO.Ref (HashSet Name) ← IO.mkRef βˆ… class MonadTrace (m : Type β†’ Type) where modifyTraceState : (TraceState β†’ TraceState) β†’ m Unit getTraceState : m TraceState export MonadTrace (getTraceState modifyTraceState) instance (m n) [MonadLift m n] [MonadTrace m] : MonadTrace n where modifyTraceState := fun f => liftM (modifyTraceState f : m _) getTraceState := liftM (getTraceState : m _) variable {Ξ± : Type} {m : Type β†’ Type} [Monad m] [MonadTrace m] [MonadOptions m] [MonadLiftT IO m] def printTraces : m Unit := do for {msg, ..} in (← getTraceState).traces do IO.println (← msg.format) def resetTraceState : m Unit := modifyTraceState (fun _ => {}) private def checkTraceOption (inherited : HashSet Name) (opts : Options) (cls : Name) : Bool := !opts.isEmpty && go (`trace ++ cls) where go (opt : Name) : Bool := if let some enabled := opts.get? opt then enabled else if let .str parent _ := opt then inherited.contains opt && go parent else false def isTracingEnabledFor (cls : Name) : m Bool := do let inherited ← (inheritedTraceOptions.get : IO _) pure (checkTraceOption inherited (← getOptions) cls) @[inline] def getTraces : m (PersistentArray TraceElem) := do let s ← getTraceState pure s.traces @[inline] def modifyTraces (f : PersistentArray TraceElem β†’ PersistentArray TraceElem) : m Unit := modifyTraceState fun s => { s with traces := f s.traces } @[inline] def setTraceState (s : TraceState) : m Unit := modifyTraceState fun _ => s private def getResetTraces : m (PersistentArray TraceElem) := do let oldTraces ← getTraces modifyTraces fun _ => {} pure oldTraces section variable [MonadRef m] [AddMessageContext m] [MonadOptions m] def addRawTrace (msg : MessageData) : m Unit := do let ref ← getRef let msg ← addMessageContext msg modifyTraces (Β·.push { ref, msg }) def addTrace (cls : Name) (msg : MessageData) : m Unit := do let ref ← getRef let msg ← addMessageContext msg modifyTraces (Β·.push { ref, msg := .trace cls msg #[] }) @[inline] def trace (cls : Name) (msg : Unit β†’ MessageData) : m Unit := do if (← isTracingEnabledFor cls) then addTrace cls (msg ()) @[inline] def traceM (cls : Name) (mkMsg : m MessageData) : m Unit := do if (← isTracingEnabledFor cls) then let msg ← mkMsg addTrace cls msg private def addTraceNodeCore (oldTraces : PersistentArray TraceElem) (cls : Name) (ref : Syntax) (msg : MessageData) (collapsed : Bool) : m Unit := modifyTraces fun newTraces => oldTraces.push { ref, msg := .trace cls msg (newTraces.toArray.map (Β·.msg)) collapsed } private def addTraceNode (oldTraces : PersistentArray TraceElem) (cls : Name) (ref : Syntax) (msg : MessageData) (collapsed : Bool) : m Unit := withRef ref do let msg ← addMessageContext msg addTraceNodeCore oldTraces cls ref msg collapsed def withSeconds [Monad m] [MonadLiftT BaseIO m] (act : m Ξ±) : m (Ξ± Γ— Float) := do let start ← IO.monoNanosNow let a ← act let stop ← IO.monoNanosNow return (a, (stop - start).toFloat / 1000000000) register_builtin_option trace.profiler : Bool := { defValue := false group := "profiler" descr := "activate nested traces with execution time over threshold" } register_builtin_option trace.profiler.threshold : Nat := { defValue := 10 group := "profiler" descr := "threshold in milliseconds, traces below threshold will not be activated" } def trace.profiler.threshold.getSecs (o : Options) : Float := (trace.profiler.threshold.get o).toFloat / 1000 @[inline] def shouldProfile : m Bool := do let opts ← getOptions return profiler.get opts || trace.profiler.get opts @[inline] def shouldEnableNestedTrace (cls : Name) (secs : Float) : m Bool := do return (← isTracingEnabledFor cls) || secs < trace.profiler.threshold.getSecs (← getOptions) def withTraceNode [MonadExcept Ξ΅ m] [MonadLiftT BaseIO m] (cls : Name) (msg : Except Ξ΅ Ξ± β†’ m MessageData) (k : m Ξ±) (collapsed := true) : m Ξ± := do let opts ← getOptions let clsEnabled ← isTracingEnabledFor cls unless clsEnabled || trace.profiler.get opts do return (← k) let oldTraces ← getResetTraces let (res, secs) ← withSeconds <| observing k let aboveThresh := trace.profiler.get opts && secs > trace.profiler.threshold.getSecs opts unless clsEnabled || aboveThresh do modifyTraces (oldTraces ++ Β·) return (← MonadExcept.ofExcept res) let ref ← getRef let mut m ← try msg res catch _ => pure m!"<exception thrown while producing trace node message>" if profiler.get opts || aboveThresh then m := m!"[{secs}s] {m}" addTraceNode oldTraces cls ref m collapsed MonadExcept.ofExcept res def withTraceNode' [MonadExcept Exception m] [MonadLiftT BaseIO m] (cls : Name) (k : m (Ξ± Γ— MessageData)) (collapsed := true) : m Ξ± := let msg := fun | .ok (_, msg) => return msg | .error err => return err.toMessageData Prod.fst <$> withTraceNode cls msg k collapsed end /-- Registers a trace class. By default, trace classes are not inherited; that is, `set_option trace.foo true` does not imply `set_option trace.foo.bar true`. Calling ``registerTraceClass `foo.bar (inherited := true)`` enables this inheritance on an opt-in basis. -/ def registerTraceClass (traceClassName : Name) (inherited := false) (ref : Name := by exact decl_name%) : IO Unit := do let optionName := `trace ++ traceClassName registerOption optionName { declName := ref group := "trace" defValue := false descr := "enable/disable tracing for the given module and submodules" } if inherited then inheritedTraceOptions.modify (Β·.insert optionName) macro "trace[" id:ident "]" s:(interpolatedStr(term) <|> term) : doElem => do let msg ← if s.raw.getKind == interpolatedStrKind then `(m! $(⟨s⟩)) else `(($(⟨s⟩) : MessageData)) `(doElem| do let cls := $(quote id.getId.eraseMacroScopes) if (← Lean.isTracingEnabledFor cls) then Lean.addTrace cls $msg) def bombEmoji := "πŸ’₯" def checkEmoji := "βœ…" def crossEmoji := "❌" def exceptBoolEmoji : Except Ξ΅ Bool β†’ String | .error _ => bombEmoji | .ok true => checkEmoji | .ok false => crossEmoji def exceptOptionEmoji : Except Ξ΅ (Option Ξ±) β†’ String | .error _ => bombEmoji | .ok (some _) => checkEmoji | .ok none => crossEmoji class ExceptToEmoji (Ξ΅ Ξ± : Type) where toEmoji : Except Ξ΅ Ξ± β†’ String instance : ExceptToEmoji Ξ΅ Bool where toEmoji := exceptBoolEmoji instance : ExceptToEmoji Ξ΅ (Option Ξ±) where toEmoji := exceptOptionEmoji /-- Similar to `withTraceNode`, but msg is constructed **before** executing `k`. This is important when debugging methods such as `isDefEq`, and we want to generate the message before `k` updates the metavariable assignment. The class `ExceptToEmoji` is used to convert the result produced by `k` into an emoji (e.g., `πŸ’₯`, `βœ…`, `❌`). TODO: find better name for this function. -/ def withTraceNodeBefore [MonadRef m] [AddMessageContext m] [MonadOptions m] [MonadExcept Ξ΅ m] [MonadLiftT BaseIO m] [ExceptToEmoji Ξ΅ Ξ±] (cls : Name) (msg : m MessageData) (k : m Ξ±) (collapsed := true) : m Ξ± := do let opts ← getOptions let clsEnabled ← isTracingEnabledFor cls unless clsEnabled || trace.profiler.get opts do return (← k) let oldTraces ← getResetTraces let ref ← getRef let msg ← withRef ref do addMessageContext (← msg) let (res, secs) ← withSeconds <| observing k let aboveThresh := trace.profiler.get opts && secs > trace.profiler.threshold.getSecs opts unless clsEnabled || aboveThresh do modifyTraces (oldTraces ++ Β·) return (← MonadExcept.ofExcept res) let mut msg := m!"{ExceptToEmoji.toEmoji res} {msg}" if profiler.get opts || aboveThresh then msg := m!"[{secs}s] {msg}" addTraceNodeCore oldTraces cls ref msg collapsed MonadExcept.ofExcept res end Lean
c789eb90250eef4b85da4f9cad71d189d2e2be09
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/src/power.lean
0e5996fc56d50ee2cd92db11dec054b81cfcbab6
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
36,494
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.limits.shapes import category_theory.limits.types import pullbacks import to_mathlib import sub import subobject_classifier import binary_products /-! # Power objects Define power objects -/ universes v u vβ‚‚ uβ‚‚ open category_theory category_theory.category category_theory.limits attribute [instance] has_pullbacks_of_has_finite_limits variables {C : Type u} [π’ž : category.{v} C] include π’ž lemma cone_is_pullback {X Y Z : C} (f : X ⟢ Z) (g : Y ⟢ Z) [has_limit (cospan f g)] : is_limit (pullback_cone.mk _ _ pullback.condition : pullback_cone f g) := begin apply is_limit.of_iso_limit, apply limit.is_limit, refine cones.ext _ _, refl, intro j, erw id_comp, cases j, refl, refl, rw limit.cone_Ο€, erw ← limit.w (cospan _ _) walking_cospan.hom.inl, refl end section faithful variables {D : Type uβ‚‚} [π’Ÿ : category.{vβ‚‚} D] include π’Ÿ instance right_op_faithful {F : Cα΅’α΅– β₯€ D} [faithful F] : faithful F.right_op := { injectivity' := begin dsimp, intros X Y f g h, have := has_hom.hom.op_inj ((faithful.injectivity F (has_hom.hom.op_inj h))), exact this end } end faithful def op_equiv (A : C) (B : Cα΅’α΅–): (opposite.op A ⟢ B) ≃ (B.unop ⟢ A) := { to_fun := Ξ» f, f.unop, inv_fun := Ξ» g, g.op, left_inv := Ξ» _, rfl, right_inv := Ξ» _, rfl } variables [has_finite_limits.{v} C] structure powerises {A PA niA B R : C} (memA : niA ⟢ PA β¨― A) (m : R ⟢ B β¨― A) (mhat : B ⟢ PA) := (top : R ⟢ niA) (commutes : top ≫ memA = m ≫ limits.prod.map mhat (πŸ™ A)) (forms_pullback' : is_limit (pullback_cone.mk _ _ commutes)) restate_axiom powerises.forms_pullback' class has_power_object (A : C) := (PA niA : C) (memA : niA ⟢ PA β¨― A) (mem_mono' : @mono _ π’ž _ _ memA) (hat : βˆ€ {B R} (m : R ⟢ B β¨― A) [hm : @mono _ π’ž _ _ m], B ⟢ PA) (powerises' : βˆ€ {B R} (m : R ⟢ B β¨― A) [hm : @mono _ π’ž _ _ m], powerises memA m (hat m)) (uniquely' : βˆ€ {B R} (m : R ⟢ B β¨― A) [hm : @mono _ π’ž _ _ m] (hat' : B ⟢ PA), powerises memA m hat' β†’ hat' = hat m) variable (C) class has_power_objects := (has_power_object : Ξ  (A : C), has_power_object.{v} A) variable {C} instance has_power_object_of_has_all [has_power_objects.{v} C] {A : C} : has_power_object.{v} A := has_power_objects.has_power_object A section convenience variables (A : C) [has_power_object.{v} A] def P : C := @has_power_object.PA _ π’ž _ A _ def ni : C := @has_power_object.niA _ π’ž _ A _ def mem : ni A ⟢ P A β¨― A := has_power_object.memA A instance mem_mono : mono (mem A) := has_power_object.mem_mono' A variables {A} {B R : C} (m : R ⟢ B β¨― A) [mono m] def hat : B ⟢ P A := has_power_object.hat m def hat_powerises : powerises (mem A) m (hat m) := has_power_object.powerises' m def square.top : R ⟢ ni A := (hat_powerises m).top def square.commutes : square.top m ≫ mem A = m ≫ limits.prod.map (hat m) (πŸ™ A) := (hat_powerises m).commutes def square.is_pullback : is_limit (pullback_cone.mk _ _ (square.commutes m)) := (hat_powerises m).forms_pullback lemma unique_hat (hat' : B ⟢ P A) (hp : powerises (mem A) m hat') : hat' = hat m := has_power_object.uniquely' m hat' hp end convenience section functor_setup variables {A B : C} (f : A ⟢ B) [has_power_object.{v} B] def E : C := pullback (mem B) (limits.prod.map (πŸ™ _) f) def Emap : E f ⟢ P B β¨― A := pullback.snd instance : mono (Emap f) := pullback.snd_of_mono lemma Esquare : (pullback.fst : E f ⟢ _) ≫ mem B = Emap f ≫ limits.prod.map (πŸ™ _) f := pullback.condition lemma Epb : is_limit (pullback_cone.mk _ _ (Esquare f)) := cone_is_pullback _ _ variable [has_power_object.{v} A] def P_map : P B ⟢ P A := hat (Emap f) lemma Psquare : square.top (Emap f) ≫ mem A = Emap f ≫ limits.prod.map (P_map f) (πŸ™ A) := square.commutes (Emap f) lemma Ppb : is_limit (pullback_cone.mk _ _ (Psquare f)) := square.is_pullback (Emap f) lemma easy_lemma {D R : C} (m : R ⟢ D β¨― B) [hm : mono m] : hat (pullback.snd : pullback m (limits.prod.map (πŸ™ D) f) ⟢ D β¨― A) = hat m ≫ P_map f := begin symmetry, apply unique_hat, set p : pullback m (limits.prod.map (πŸ™ D) f) ⟢ R := pullback.fst, set q : pullback m (limits.prod.map (πŸ™ D) f) ⟢ D β¨― A := pullback.snd, have := (pasting pullback.fst _ pullback.snd m _ (limits.prod.map (πŸ™ D) f) _ pullback.condition (square.commutes m) (square.is_pullback m)).inv (cone_is_pullback _ _), have comm'': limits.prod.map (πŸ™ D) f ≫ limits.prod.map (hat m) (πŸ™ B) = _ := prod_map_comm _ _, set f2 : pullback m (limits.prod.map (πŸ™ D) f) ⟢ P B β¨― A := q ≫ limits.prod.map (hat m) (πŸ™ A), set f1 : pullback m (limits.prod.map (πŸ™ D) f) ⟢ ni B := p ≫ square.top m, have comm: f1 ≫ mem B = f2 ≫ limits.prod.map (πŸ™ (P B)) f, slice_rhs 2 3 {rw comm''.symm}, slice_lhs 2 3 {rw square.commutes m}, slice_lhs 1 2 {rw pullback.condition}, rw ← assoc, have comm' : f1 ≫ mem B = pullback.snd ≫ limits.prod.map (hat m) (πŸ™ A) ≫ limits.prod.map (πŸ™ (P B)) f, rw comm, rw assoc, have newlim: is_limit (pullback_cone.mk f1 pullback.snd comm' : pullback_cone (mem B) (limits.prod.map (hat m) (πŸ™ A) ≫ limits.prod.map (πŸ™ (P B)) f)), convert this using 2, exact comm''.symm, exact comm''.symm, set r := pullback.lift f1 f2 comm, have comm''' : r ≫ Emap f = q ≫ limits.prod.map (hat m) (πŸ™ A), erw limit.lift_Ο€, refl, have := (pasting r pullback.fst q (Emap f) (mem B) (limits.prod.map (hat m) (πŸ™ A)) (limits.prod.map (πŸ™ (P B)) f) comm''' pullback.condition (Epb f)).hom _, swap, convert newlim using 2, erw limit.lift_Ο€, refl, have := (pasting r (square.top (Emap f)) q (Emap f) (mem A) (limits.prod.map (hat m) (πŸ™ A)) (limits.prod.map (P_map f) (πŸ™ A)) comm''' (Psquare f) (square.is_pullback _)).inv this, have comm4: limits.prod.map (hat m) (πŸ™ A) ≫ limits.prod.map (P_map f) (πŸ™ A) = limits.prod.map (hat m ≫ P_map f) (πŸ™ A), apply prod.hom_ext, simp, simp, erw comp_id, refine ⟨r ≫ square.top (Emap f), _, _⟩, slice_lhs 2 3 {rw square.commutes}, slice_lhs 1 2 {rw comm'''}, slice_lhs 2 3 {erw comm4}, convert this using 2, exact comm4.symm, exact comm4.symm end -- We need to assume g₁ = hom ≫ gβ‚‚. From here if we know that hom,inv cancel then we get gβ‚‚ = inv ≫ g₁. -- Instead we assume this and derive that hom,inv cancel lemma lifting {A B R₁ Rβ‚‚ : C} [has_power_object.{v} A] {g₁ : R₁ ⟢ B β¨― A} {gβ‚‚ : Rβ‚‚ ⟢ B β¨― A} [mono g₁] [mono gβ‚‚] (hom : R₁ ⟢ Rβ‚‚) (inv : Rβ‚‚ ⟢ R₁) : g₁ = hom ≫ gβ‚‚ β†’ gβ‚‚ = inv ≫ g₁ β†’ hat g₁ = hat gβ‚‚ := begin intros k l, have hi: hom ≫ inv = πŸ™ _, rw ← cancel_mono g₁, conv_rhs {rw [k, l]}, simp, have ih: inv ≫ hom = πŸ™ _, rw ← cancel_mono gβ‚‚, conv_rhs {rw [l, k]}, simp, apply unique_hat, refine ⟨inv ≫ square.top g₁, _, _⟩, slice_lhs 2 3 {rw square.commutes g₁}, slice_lhs 1 2 {rw ← l}, apply is_limit.of_iso_limit (square.is_pullback g₁), ext, swap, refine ⟨hom, inv, β€Ή_β€Ί, β€Ή_β€ΊβŸ©, cases j, simp, slice_rhs 1 2 {rw hi}, erw id_comp, simpa, simp, show _ ≫ _ = _ ≫ _ ≫ _, slice_rhs 1 2 {rw hi}, erw id_comp end def how_inj_is_hat {A B R₁ Rβ‚‚ : C} [has_power_object.{v} A] {f₁ : R₁ ⟢ B β¨― A} {fβ‚‚ : Rβ‚‚ ⟢ B β¨― A} [mono f₁] [mono fβ‚‚] (h : hat f₁ = hat fβ‚‚) : R₁ β‰… Rβ‚‚ := { hom := (square.is_pullback fβ‚‚).lift (pullback_cone.mk (square.top f₁) f₁ (h β–Έ square.commutes f₁)), inv := (square.is_pullback f₁).lift (pullback_cone.mk (square.top fβ‚‚) fβ‚‚ (h.symm β–Έ square.commutes fβ‚‚)), hom_inv_id' := begin erw [← cancel_mono f₁, assoc, (square.is_pullback f₁).fac _ walking_cospan.right, (square.is_pullback fβ‚‚).fac _ walking_cospan.right], simp end, inv_hom_id' := begin erw [← cancel_mono fβ‚‚, assoc, (square.is_pullback fβ‚‚).fac _ walking_cospan.right, (square.is_pullback f₁).fac _ walking_cospan.right], simp end } lemma very_inj {A B R₁ Rβ‚‚ : C} [has_power_object.{v} A] {f₁ : R₁ ⟢ B β¨― A} {fβ‚‚ : Rβ‚‚ ⟢ B β¨― A} [mono f₁] [mono fβ‚‚] (h : hat f₁ = hat fβ‚‚) : (how_inj_is_hat h).hom ≫ fβ‚‚ = f₁ := (square.is_pullback fβ‚‚).fac _ walking_cospan.right lemma liftable {A B : C} [has_power_object.{v} A] (a b : sub' (B β¨― A)) : (a β‰ˆ b) β†’ @hat _ _ _ _ _ _ _ a.1.hom a.2 = @hat _ _ _ _ _ _ _ b.1.hom b.2 := begin rintros ⟨⟨hom, k⟩, ⟨inv, l⟩⟩, exact @lifting _ _ _ _ _ _ _ _ _ _ a.2 b.2 _ _ k l, end def hat_sub {A B : C} [has_power_object.{v} A] : sub (B β¨― A) β†’ (B ⟢ P A) := quotient.lift (Ξ» (f : sub' (B β¨― A)), @hat _ _ _ _ _ _ _ f.1.hom f.2) liftable def hat_sub' {A B : C} [has_power_object.{v} A] (k : B ⟢ P A) : sub (B β¨― A) := quotient.mk ⟨over.mk (pullback.snd : pullback (mem A) (limits.prod.map k (πŸ™ _)) ⟢ B β¨― A), pullback.snd_of_mono⟩ def hat_natural_right {A A' B R : C} [has_power_object.{v} A] [has_power_object.{v} A'] (k : R ⟢ B β¨― A) [mono k] (g : A' ⟢ A) : hat k ≫ P_map g = hat (pullback.snd : pullback k (limits.prod.map (πŸ™ B) g) ⟢ B β¨― A') := begin rw easy_lemma end def hat_natural_left {A B B' R : C} [has_power_object.{v} A] (k : R ⟢ B β¨― A) [mono k] (g : B' ⟢ B) : g ≫ hat k = hat (pullback.snd : pullback k (limits.prod.map g (πŸ™ A)) ⟢ B' β¨― A) := -- hat_sub (sub_map (limits.prod.map g (πŸ™ A)) k) := begin apply unique_hat, refine ⟨pullback.fst ≫ square.top k, _, _⟩, slice_lhs 2 3 {rw square.commutes}, slice_lhs 1 2 {rw pullback.condition}, rw assoc, rw ← prod_functorial, have := (pasting pullback.fst _ pullback.snd k _ (limits.prod.map g (πŸ™ A)) _ _ _ (square.is_pullback k)).inv (cone_is_pullback _ _), convert this, rw prod_functorial, rw prod_functorial, end def hat_sub_natural_left (A B B' : C) [has_power_object.{v} A] (k : sub (B β¨― A)) (g : B' ⟢ B) : g ≫ hat_sub k = hat_sub (sub_map (limits.prod.map g (πŸ™ A)) k) := begin apply quotient.induction_on k, dsimp [hat_sub, sub_map], intro a, rw hat_natural_left end def hat_sub_natural_right {A A' B : C} [has_power_object.{v} A] [has_power_object.{v} A'] (k : sub (B β¨― A)) (g : A' ⟢ A) : hat_sub k ≫ P_map g = hat_sub (sub_map (limits.prod.map (πŸ™ B) g) k) := begin apply quotient.induction_on k, dsimp [hat_sub, sub_map], intro a, rw ← easy_lemma end def hat_sub'' {A B : C} [has_power_object.{v} A] : (B ⟢ P A) ≃ sub (B β¨― A) := { to_fun := hat_sub', inv_fun := hat_sub, left_inv := begin intro g, dsimp [hat_sub, hat_sub'], symmetry, apply unique_hat, exact ⟨_, pullback.condition, cone_is_pullback _ _⟩ end, right_inv := begin intro g, dsimp [hat_sub, hat_sub'], apply quotient.induction_on g, intro g', haveI := g'.2, apply quotient.sound, dsimp, split, refine ⟨_, _⟩, apply (square.is_pullback g'.1.hom).lift (pullback_cone.mk pullback.fst pullback.snd pullback.condition), dsimp, erw (square.is_pullback g'.1.hom).fac _ walking_cospan.right, refl, refine ⟨_, _⟩, apply pullback.lift (square.top g'.1.hom) g'.1.hom (square.commutes g'.1.hom), simp end } def hat_sub'_natural_right (A A' B : C) [has_power_object.{v} A] [has_power_object.{v} A'] (k : B ⟢ P A) (g : A' ⟢ A) : hat_sub' (k ≫ P_map g) = sub_map (limits.prod.map (πŸ™ B) g) (hat_sub' k) := begin erw ← hat_sub''.eq_symm_apply, dsimp [hat_sub''], rw ← hat_sub_natural_right, congr' 1, apply (hat_sub''.left_inv k).symm end def hat_sub'_natural_left {A B B' : C} [has_power_object.{v} A] (k : B ⟢ P A) (g : B' ⟢ B) : hat_sub' (g ≫ k) = sub_map (limits.prod.map g (πŸ™ A)) (hat_sub' k) := begin erw ← hat_sub''.eq_symm_apply, dsimp [hat_sub''], rw ← hat_sub_natural_left, congr' 1, apply (hat_sub''.left_inv k).symm end lemma P_map_id (X : C) [has_power_object.{v} X] : P_map (πŸ™ X) = πŸ™ (P X) := begin symmetry, apply unique_hat, refine ⟨pullback.fst, pullback.condition, cone_is_pullback _ _⟩ end lemma P_map_comp {X Y Z : C} [has_power_object.{v} X] [has_power_object.{v} Y] [has_power_object.{v} Z] (f : X ⟢ Y) (g : Y ⟢ Z) : P_map (f ≫ g) = P_map g ≫ P_map f := begin erw ← easy_lemma, rw P_map, refine lifting _ _ _ _, { refine pullback.lift (pullback.lift pullback.fst (pullback.snd ≫ limits.prod.map (πŸ™ _) f) _) pullback.snd _, { rw pullback.condition, rw assoc, congr' 1, apply prod.hom_ext; simp, erw comp_id, erw comp_id, erw comp_id }, { erw limit.lift_Ο€, refl } }, { refine pullback.lift _ _ _, apply pullback.fst ≫ pullback.fst, apply pullback.snd, slice_lhs 2 3 {rw pullback.condition}, slice_lhs 1 2 {erw pullback.condition}, rw assoc, apply prod.hom_ext; simp, erw comp_id }, { simp, refl }, { erw limit.lift_Ο€, refl } end def P_functor [has_power_objects.{v} C] : Cα΅’α΅– β₯€ C := { obj := Ξ» X, P X.unop, map := Ξ» X Y f, P_map f.unop, map_id' := Ξ» X, P_map_id _, map_comp' := Ξ» X Y Z f g, P_map_comp _ _ } end functor_setup def thing (X Y Z : C) (g : Y ⟢ Z) : is_limit (pullback_cone.mk (limits.prod.map g (πŸ™ X)) (prod.lift limits.prod.snd limits.prod.fst) (begin apply prod.hom_ext; simp end) : pullback_cone (prod.lift limits.prod.snd limits.prod.fst) (limits.prod.map (πŸ™ X) g)) := begin refine ⟨_, _, _⟩, intro c, apply pullback_cone.snd c ≫ (limits.prod.braiding _ _).hom, intro c, apply pi_app_left (pullback_cone.mk (limits.prod.map g (πŸ™ X)) (limits.prod.lift limits.prod.snd limits.prod.fst) _) c, change (pullback_cone.snd c ≫ (limits.prod.braiding _ _).hom) ≫ (limits.prod.map _ _) = pullback_cone.fst c, apply prod.hom_ext, have := pullback_cone.condition c =≫ limits.prod.snd, simp at this, simp, exact this.symm, simp, have := pullback_cone.condition c =≫ limits.prod.fst, simp at this, exact this.symm, change (pullback_cone.snd c ≫ (limits.prod.braiding _ _).hom) ≫ (limits.prod.lift limits.prod.snd limits.prod.fst) = pullback_cone.snd c, rw category.assoc, apply prod.hom_ext, simp, simp, intros c m J, rw ← cancel_mono (limits.prod.braiding X Y).inv, rw category.assoc, rw iso.hom_inv_id, rw comp_id, apply J walking_cospan.right, end def self_adj [has_power_objects.{v} C] : is_right_adjoint (@P_functor C π’ž _ _) := { left := P_functor.right_op, adj := adjunction.mk_of_hom_equiv { hom_equiv := begin intros A B, apply equiv.trans _ hat_sub''.symm, apply equiv.trans (op_equiv (P_functor.obj (opposite.op A)) B), apply equiv.trans hat_sub'', apply sub_iso_compose (limits.prod.braiding _ _), end, hom_equiv_naturality_left_symm' := begin intros X' X Y f g, dsimp [hat_sub''], simp, change (hat_sub ((sub_iso_compose (prod.braiding (opposite.unop Y) X')).inv_fun (hat_sub' (f ≫ g)))).op = (P_functor.map (has_hom.hom.op f)).op ≫ (hat_sub ((sub_iso_compose (prod.braiding (opposite.unop Y) X)).inv_fun (hat_sub' g))).op, rw ← op_comp, congr' 1, erw hat_sub_natural_right, congr' 1, rw has_hom.hom.unop_op, dsimp [sub_iso_compose], rw hat_sub'_natural_left, apply postcompose_sub_comm, swap, apply prod.hom_ext, simp, simp, apply thing end, hom_equiv_naturality_right' := begin intros X Y Y' f g, dsimp [hat_sub'', sub_iso_compose, op_equiv], erw hat_sub_natural_right, congr' 1, rw hat_sub'_natural_left, apply postcompose_sub_comm, swap, apply prod.hom_ext, simp, simp, apply thing end } } @[reducible] def diagonal (A : C) : A ⟢ A β¨― A := limits.prod.lift (πŸ™ A) (πŸ™ A) instance mono_prod_lift_of_left {X Y Z : C} (f : X ⟢ Y) (g : X ⟢ Z) [mono f] : mono (limits.prod.lift f g) := begin split, intros W h k l, have := l =≫ limits.prod.fst, simp at this, rwa cancel_mono at this, end instance mono_prod_lift_of_right {X Y Z : C} (f : X ⟢ Y) (g : X ⟢ Z) [mono g] : mono (limits.prod.lift f g) := begin split, intros W h k l, have := l =≫ limits.prod.snd, simp at this, rwa cancel_mono at this, end def singleton_arrow (A : C) [has_power_object.{v} A] : A ⟢ P A := hat (diagonal A) lemma seven_six_one {A B : C} [has_power_object.{v} B] (f : A ⟢ B) : hat (limits.prod.lift (πŸ™ A) f) = f ≫ singleton_arrow B := begin erw hat_natural_left, refine lifting (pullback.lift f (limits.prod.lift (πŸ™ A) f) _) (pullback.snd ≫ limits.prod.fst) _ _, apply prod.hom_ext, simp, erw id_comp, simp, erw comp_id, simp, apply prod.hom_ext, simp, slice_rhs 3 4 {rw limit.lift_Ο€}, have: (_ ≫ diagonal B) ≫ _ = (_ ≫ limits.prod.map f (πŸ™ B)) ≫ _ := pullback.condition =≫ limits.prod.fst, simp at this, erw ← this, have: (_ ≫ diagonal B) ≫ _ = (_ ≫ limits.prod.map f (πŸ™ B)) ≫ _ := pullback.condition =≫ limits.prod.snd, simp at this, rw this, dsimp, rw comp_id end lemma seven_six_two {A B : C} [has_power_object.{v} A] [has_power_object.{v} B] (f : A ⟢ B) : hat (limits.prod.lift f (πŸ™ A)) = singleton_arrow B ≫ P_map f := begin erw hat_natural_right, refine lifting (pullback.lift f (limits.prod.lift f (πŸ™ A)) _) (pullback.snd ≫ limits.prod.snd) _ _, apply prod.hom_ext, simp, erw comp_id, simp, erw id_comp, simp, apply prod.hom_ext, simp, have: (_ ≫ diagonal B) ≫ _ = (_ ≫ limits.prod.map (πŸ™ B) f) ≫ _ := pullback.condition =≫ limits.prod.snd, simp at this, erw ← this, have: (_ ≫ diagonal B) ≫ _ = (_ ≫ limits.prod.map (πŸ™ B) f) ≫ _ := pullback.condition =≫ limits.prod.fst, simp at this, rw this, dsimp, simp, simp end instance singleton_mono (A : C) [has_power_object.{v} A] : mono (singleton_arrow A) := begin split, intros, rw ← seven_six_one at w, rw ← seven_six_one at w, have q := very_inj w =≫ limits.prod.fst, simp at q, have r := very_inj w =≫ limits.prod.snd, simp [q] at r, rw r end instance pfaithful [has_power_objects.{v} C] : faithful (@P_functor _ π’ž _ _) := begin refine ⟨_⟩, dsimp, intros A B f g k, have w: hat (limits.prod.lift f.unop (πŸ™ B.unop)) = hat (limits.prod.lift g.unop (πŸ™ B.unop)), rw seven_six_two, rw seven_six_two, show _ ≫ P_functor.map f = _ ≫ P_map (has_hom.hom.unop g), rw k, refl, have q := very_inj w =≫ limits.prod.snd, simp at q, have r := very_inj w =≫ limits.prod.fst, simp [q] at r, apply has_hom.hom.unop_inj, symmetry, assumption end lemma p_faithful {A B : C} [has_power_object.{v} A] [has_power_object.{v} B] (f g : A ⟢ B) : P_map f = P_map g β†’ f = g := begin intro k, have w: hat (limits.prod.lift f (πŸ™ _)) = hat (limits.prod.lift g (πŸ™ _)), rw [seven_six_two, seven_six_two, k], have q := very_inj w =≫ limits.prod.snd, simp at q, have r := very_inj w =≫ limits.prod.fst, simp [q] at r, symmetry, assumption end instance mono_prod_map {X Y Z W : C} (f : X ⟢ Y) (g : W ⟢ Z) [mono f] [mono g] : mono (limits.prod.map f g) := begin split, intros A h k l, apply prod.hom_ext, rw ← cancel_mono f, rw assoc, rw assoc, have := l =≫ limits.prod.fst, simp at this, assumption, have := l =≫ limits.prod.snd, simp at this, rwa ← cancel_mono g, simpa end def internal_image {A B : C} [has_power_object.{v} A] [has_power_object.{v} B] (f : A ⟢ B) [mono f] : P A ⟢ P B := hat (mem A ≫ limits.prod.map (πŸ™ (P A)) f) -- TODO: this doesn't use pasting so it's super long. can we make it nicer by using pasting? lemma naturalish {A B : C} [has_power_object.{v} A] [has_power_object.{v} B] (f : A ⟢ B) [mono f] {R D : C} (m : R ⟢ D β¨― A) [mono m] : hat m ≫ internal_image f = hat (m ≫ limits.prod.map (πŸ™ D) f) := begin apply unique_hat, refine ⟨square.top m ≫ square.top (mem A ≫ limits.prod.map (πŸ™ (P A)) f), _, _⟩, slice_lhs 2 3 {rw square.commutes}, slice_lhs 1 2 {rw square.commutes}, repeat {rw assoc}, congr' 1, apply prod.hom_ext, simp, erw id_comp, erw id_comp, refl, simp, erw comp_id, erw id_comp, refine ⟨_, _, _⟩, { intro c, have qcomm: pullback_cone.fst c ≫ mem B = (pullback_cone.snd c ≫ limits.prod.map (hat m) (πŸ™ B)) ≫ limits.prod.map (internal_image f) (πŸ™ B), { rw pullback_cone.condition, rw assoc, congr' 1, apply prod_functorial }, set q : c.X ⟢ ni A := (square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).lift (pullback_cone.mk (pullback_cone.fst c) (pullback_cone.snd c ≫ limits.prod.map (hat m) (πŸ™ B)) qcomm), have: q ≫ mem A ≫ limits.prod.map (πŸ™ (P A)) f = c.Ο€.app walking_cospan.right ≫ (limits.prod.map (hat m) (πŸ™ B)), { erw (square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).fac _ walking_cospan.right, refl }, refine (square.is_pullback m).lift (pullback_cone.mk q _ _), { apply limits.prod.lift (pullback_cone.snd c ≫ limits.prod.fst) (q ≫ mem A ≫ limits.prod.snd) }, { apply prod.hom_ext, { simp, have := this =≫ limits.prod.fst, simp at this, rw ← this, erw comp_id }, { simp, erw comp_id } } }, { intros c, dsimp, refine pi_app_left (pullback_cone.mk (square.top m ≫ square.top (mem A ≫ limits.prod.map (πŸ™ (P A)) f)) (m ≫ limits.prod.map (πŸ™ D) f) _) c _ _ _, erw ← assoc, erw (square.is_pullback m).fac _ walking_cospan.left, dsimp, erw (square.is_pullback _).fac _ walking_cospan.left, refl, erw ← assoc, erw (square.is_pullback m).fac _ walking_cospan.right, dsimp, apply prod.hom_ext, simp, erw comp_id, simp, have qcomm: pullback_cone.fst c ≫ mem B = (pullback_cone.snd c ≫ limits.prod.map (hat m) (πŸ™ B)) ≫ limits.prod.map (internal_image f) (πŸ™ B), { rw pullback_cone.condition, rw assoc, congr' 1, apply prod_functorial }, set q : c.X ⟢ ni A := (square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).lift (pullback_cone.mk (pullback_cone.fst c) (pullback_cone.snd c ≫ limits.prod.map (hat m) (πŸ™ B)) qcomm), have: q ≫ mem A ≫ limits.prod.map (πŸ™ (P A)) f = c.Ο€.app walking_cospan.right ≫ (limits.prod.map (hat m) (πŸ™ B)), { erw (square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).fac _ walking_cospan.right, refl }, show q ≫ _ ≫ _ ≫ _ = _, have := this =≫ limits.prod.snd, simp at this, rw this, erw comp_id }, { dsimp, intros c k J, apply (square.is_pullback m).hom_ext, apply pullback_cone.equalizer_ext (pullback_cone.mk (square.top m) m _), { apply (square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).hom_ext, apply pullback_cone.equalizer_ext (pullback_cone.mk _ _ _), simp, exact J walking_cospan.left, change (k ≫ square.top m) ≫ (mem A ≫ limits.prod.map (πŸ™ (P A)) f) = ((square.is_pullback m).lift (pullback_cone.mk ((square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).lift (pullback_cone.mk (pullback_cone.fst c) (_ ≫ limits.prod.map (hat m) (πŸ™ B)) _)) (prod.lift (_ ≫ limits.prod.fst) ((square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).lift (pullback_cone.mk (pullback_cone.fst c) (_ ≫ limits.prod.map (hat m) (πŸ™ B)) _) ≫ mem A ≫ limits.prod.snd)) _) ≫ square.top m) ≫ (mem A ≫ limits.prod.map (πŸ™ (P A)) f), erw (square.is_pullback m).fac _ walking_cospan.left, dsimp, erw (square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).fac _ walking_cospan.right, dsimp, have: k ≫ (m ≫ limits.prod.map (πŸ™ D) f) = pullback_cone.snd c := J walking_cospan.right, erw ← this, conv_lhs { rw ← assoc, congr, rw assoc, congr, skip, rw square.commutes m }, apply prod.hom_ext; simp, congr' 3, dsimp, simp, dsimp, simp }, { apply prod.hom_ext, { erw (square.is_pullback m).fac _ walking_cospan.right, dsimp, simp, have: k ≫ (m ≫ limits.prod.map (πŸ™ D) f) = pullback_cone.snd c := J walking_cospan.right, erw ← this, simp, congr' 2, erw comp_id }, { rw ← cancel_mono f, simp, have qcomm: pullback_cone.fst c ≫ mem B = (pullback_cone.snd c ≫ limits.prod.map (hat m) (πŸ™ B)) ≫ limits.prod.map (internal_image f) (πŸ™ B), { rw pullback_cone.condition, rw assoc, congr' 1, apply prod_functorial }, set q : c.X ⟢ ni A := (square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).lift (pullback_cone.mk (pullback_cone.fst c) (pullback_cone.snd c ≫ limits.prod.map (hat m) (πŸ™ B)) qcomm), have: q ≫ mem A ≫ limits.prod.map (πŸ™ (P A)) f = c.Ο€.app walking_cospan.right ≫ (limits.prod.map (hat m) (πŸ™ B)), { erw (square.is_pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f)).fac _ walking_cospan.right, refl }, have := this =≫ limits.prod.snd, simp at this, rw this, erw ← J walking_cospan.right, simp, congr' 3, erw comp_id } } } end lemma internal_image_map_comp {X Y Z : C} [has_power_object.{v} X] [has_power_object.{v} Y] [has_power_object.{v} Z] (f : X ⟢ Y) (g : Y ⟢ Z) [mono f] [mono g] : internal_image (f ≫ g) = internal_image f ≫ internal_image g := begin erw naturalish, rw internal_image, congr' 1, rw prod_functorial', simp end def powerises_id (A : C) [has_power_object.{v} A] : powerises (mem A) (mem A) (πŸ™ (P A)) := { top := πŸ™ _, commutes := begin apply prod.hom_ext; simp, erw comp_id, erw comp_id end, forms_pullback' := begin convert pullback.with_id_l' (mem A), all_goals {apply prod.hom_ext; simp, erw comp_id, erw comp_id }, end } lemma internal_image_map_id {X : C} [has_power_object.{v} X] : internal_image (πŸ™ X) = πŸ™ (P X) := begin symmetry, apply unique_hat, convert powerises_id X, apply prod.hom_ext; simp, erw comp_id, erw comp_id end theorem beck_chevalley {A B C' D : C} [has_power_object.{v} A] [has_power_object.{v} B] [has_power_object.{v} C'] [has_power_object.{v} D] (h : D ⟢ A) (f : A ⟢ C') (k : D ⟢ B) (g : B ⟢ C') (comm : h ≫ f = k ≫ g) [mono f] [mono k] (t : is_limit (pullback_cone.mk h k comm)) : internal_image f ≫ P_map g = P_map h ≫ internal_image k := begin erw naturalish, erw hat_natural_right, set X := pullback (mem A ≫ limits.prod.map (πŸ™ (P A)) f) (limits.prod.map (πŸ™ (P A)) g), set π₁ : X ⟢ ni A := pullback.fst, set Ο€β‚‚ : X ⟢ P A β¨― B := pullback.snd, have comm2: (π₁ ≫ mem A ≫ limits.prod.snd) ≫ f = (Ο€β‚‚ ≫ limits.prod.snd) ≫ g, have: (π₁ ≫ _) ≫ _ = (_ ≫ _) ≫ _ := pullback.condition =≫ limits.prod.snd, simp at this, rwa [assoc, assoc, assoc], set l: X ⟢ D := t.lift (pullback_cone.mk (π₁ ≫ mem A ≫ limits.prod.snd) (Ο€β‚‚ ≫ limits.prod.snd) comm2), have lprop₁: l ≫ h = π₁ ≫ mem A ≫ limits.prod.snd, exact t.fac (pullback_cone.mk (π₁ ≫ mem A ≫ limits.prod.snd) (Ο€β‚‚ ≫ limits.prod.snd) comm2) walking_cospan.left, have lpropβ‚‚: l ≫ k = Ο€β‚‚ ≫ limits.prod.snd, exact t.fac (pullback_cone.mk (π₁ ≫ mem A ≫ limits.prod.snd) (Ο€β‚‚ ≫ limits.prod.snd) comm2) walking_cospan.right, have comm3: π₁ ≫ mem A ≫ limits.prod.fst = Ο€β‚‚ ≫ limits.prod.fst, have: (π₁ ≫ _) ≫ _ = (_ ≫ _) ≫ _ := pullback.condition =≫ limits.prod.fst, simp at this, erw [comp_id, comp_id] at this, assumption, refine lifting _ _ _ _, { apply pullback.lift π₁ (limits.prod.lift (Ο€β‚‚ ≫ limits.prod.fst) l) _, apply prod.hom_ext, rw [assoc, comm3], simp, erw comp_id, rw [assoc, ← lprop₁], simp }, { refine pullback.lift pullback.fst (pullback.snd ≫ limits.prod.map (πŸ™ _) k) _, slice_lhs 1 2 {rw pullback.condition}, rw [assoc, assoc, ← prod_functorial', comm, prod_functorial'] }, { rw ← assoc, erw limit.lift_Ο€, apply prod.hom_ext; simp, erw comp_id, exact lpropβ‚‚.symm }, { erw limit.lift_Ο€, refl } end def classifying_powers [has_power_object.{v} (⊀_ C)] {U X : C} (f : U ⟢ X) [mono f] : classifying (mem (⊀_ C) ≫ limits.prod.fst) f (hat (f ≫ prod.lift (πŸ™ X) (terminal.from X))) := { k := square.top (f ≫ prod.lift (πŸ™ X) (terminal.from X)), commutes := begin rw ← assoc, rw square.commutes (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X)), simp, erw id_comp, end, forms_pullback' := { lift := Ξ» s, begin apply (square.is_pullback (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X))).lift (pullback_cone.mk (pullback_cone.fst s) _ _), apply pullback_cone.snd s ≫ (prod.right_unitor _).inv, apply prod.hom_ext, simp, rw pullback_cone.condition s, erw id_comp, apply subsingleton.elim, end, fac' := Ξ» s, begin have comm: pullback_cone.fst s ≫ mem (⊀_ C) = (pullback_cone.snd s ≫ (prod.right_unitor X).inv) ≫ limits.prod.map (hat (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X))) (πŸ™ (⊀_ C)), apply prod.hom_ext, simp, rw pullback_cone.condition s, erw id_comp, apply subsingleton.elim, apply pi_app_left (pullback_cone.mk (square.top (f ≫ prod.lift (πŸ™ X) (terminal.from X))) f _) s, exact (square.is_pullback (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X))).fac (pullback_cone.mk (pullback_cone.fst s) _ comm) walking_cospan.left, have := (square.is_pullback (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X))).fac (pullback_cone.mk (pullback_cone.fst s) (pullback_cone.snd s ≫ (prod.right_unitor _).inv) comm) walking_cospan.right =≫ limits.prod.fst, dsimp at this, rw [assoc, assoc, assoc] at this, simp at this, exact this end, uniq' := Ξ» s m J, begin have comm: pullback_cone.fst s ≫ mem (⊀_ C) = (pullback_cone.snd s ≫ (prod.right_unitor X).inv) ≫ limits.prod.map (hat (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X))) (πŸ™ (⊀_ C)), apply prod.hom_ext, simp, rw pullback_cone.condition s, erw id_comp, apply subsingleton.elim, apply (square.is_pullback (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X))).uniq (pullback_cone.mk (pullback_cone.fst s) _ comm), apply pi_app_left (pullback_cone.mk (square.top (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X))) (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X)) _) (pullback_cone.mk (pullback_cone.fst s) (pullback_cone.snd s ≫ (prod.right_unitor X).inv) comm), dsimp, -- change m ≫ (square.top (f ≫ limits.prod.lift (πŸ™ X) (terminal.from X))) = (pullback_cone.fst s), exact J walking_cospan.left, change m ≫ (f ≫ prod.lift (πŸ™ X) (terminal.from X)) = pullback_cone.snd s ≫ (prod.right_unitor X).inv, apply prod.hom_ext, simp, exact J walking_cospan.right, apply subsingleton.elim end } } def classifying_powers' [has_power_object.{v} (⊀_ C)] {U X : C} (f : U ⟢ X) [mono f] (χ₁ : X ⟢ P (⊀_ C)) (k : classifying (mem (⊀_ C) ≫ (prod.right_unitor (P (⊀_ C))).hom) f χ₁) : powerises (mem (⊀_ C)) (f ≫ prod.lift (πŸ™ X) (terminal.from X)) χ₁ := begin set top := k.k, have comm: top ≫ _ = _ ≫ _ := k.commutes, have pb: is_limit (pullback_cone.mk _ _ comm) := k.forms_pullback', refine ⟨top, _, _⟩, { apply prod.hom_ext, { rw assoc, erw comm, simp, erw id_comp }, { apply subsingleton.elim } }, { refine ⟨_, _, _⟩, { intro s, apply pb.lift (pullback_cone.mk (pullback_cone.fst s) (pullback_cone.snd s ≫ limits.prod.fst) _), rw assoc, have := pullback_cone.condition s =≫ limits.prod.fst, simp at this, exact this }, { intro s, apply pi_app_left (pullback_cone.mk top (f ≫ prod.lift (πŸ™ X) (terminal.from X)) _) _, exact pb.fac (pullback_cone.mk (pullback_cone.fst s) (pullback_cone.snd s ≫ limits.prod.fst) _) walking_cospan.left, erw ← assoc, erw pb.fac (pullback_cone.mk (pullback_cone.fst s) (pullback_cone.snd s ≫ limits.prod.fst) _) walking_cospan.right, erw assoc, erw (prod.right_unitor X).hom_inv_id, erw comp_id }, { intros s m J, apply pb.uniq (pullback_cone.mk (pullback_cone.fst s) (pullback_cone.snd s ≫ limits.prod.fst) _), apply pi_app_left (pullback_cone.mk top f comm) (pullback_cone.mk (pullback_cone.fst s) (pullback_cone.snd s ≫ limits.prod.fst) _), exact J walking_cospan.left, dunfold pullback_cone.snd, dsimp, conv_rhs {rw [← J walking_cospan.right, assoc]}, dsimp, simp } } end instance weak_topos_has_subobj [has_power_object.{v} (⊀_ C)] : has_subobject_classifier.{v} C := { Ξ© := P (⊀_ C), Ξ©β‚€ := ni (⊀_ C), truth := mem (⊀_ C) ≫ (prod.right_unitor _).hom, truth_mono' := begin apply_instance end, classifier_of := Ξ» U X f hf, begin haveI := hf, apply hat (f ≫ limits.prod.lift (πŸ™ _) (terminal.from _)) end, classifies' := Ξ» U X f hf, @classifying_powers _ _ _ _ _ _ _ hf, uniquely' := Ξ» U X f hf χ₁ k, begin haveI := hf, apply unique_hat, apply classifying_powers' f, exact k end } instance p_conservative [has_power_objects.{v} C] {A B : C} (f : A ⟢ B) [is_iso (P_map f)] : is_iso f := begin apply @balanced _ π’ž _ _ _ _ _ _, { split, intros, apply p_faithful g h, rw [← cancel_mono (P_map f), ← P_map_comp, w, P_map_comp] }, { split, intros, apply p_faithful g h, rw [← cancel_epi (P_map f), ← P_map_comp, w, P_map_comp] } end namespace intersect variables (A : C) [has_power_object.{v} A] @[reducible] def π₁₃ : (P A β¨― P A) β¨― A ⟢ P A β¨― A := limits.prod.map limits.prod.fst (πŸ™ A) @[reducible] def π₂₃ : (P A β¨― P A) β¨― A ⟢ P A β¨― A := limits.prod.map limits.prod.snd (πŸ™ A) def L1 : C := pullback (mem A) (π₁₃ A) def R1 : C := pullback (mem A) (π₂₃ A) @[reducible] def left : L1 A ⟢ (P A β¨― P A) β¨― A := pullback.snd def right : R1 A ⟢ (P A β¨― P A) β¨― A := pullback.snd instance mono_right: mono (right A) := begin dunfold right, apply_instance end @[reducible] def both : pullback (left A) (right A) ⟢ (P A β¨― P A) β¨― A := pullback.fst ≫ left A def intersect : P A β¨― P A ⟢ P A := hat (both A) end intersect -- lemma intersect_prop (R₁ Rβ‚‚ : C) (f₁ : R₁ ⟢ B β¨― A) (fβ‚‚ : Rβ‚‚ ⟢ B β¨― A) [mono f₁] [mono fβ‚‚] : -- hat ((pullback.fst : pullback f₁ fβ‚‚ ⟢ R₁) ≫ f₁) = limits.prod.lift (hat f₁) (hat fβ‚‚) ≫ intersect.intersect A := -- begin -- symmetry, -- apply unique_hat, -- refine ⟨_ ≫ square.top (intersect.both A), _, _⟩, -- { apply pullback.lift _ _ _, -- { apply pullback.lift _ _ _, -- { apply pullback.fst ≫ square.top f₁ }, -- { apply pullback.fst ≫ f₁ ≫ limits.prod.map (limits.prod.lift (hat f₁) (hat fβ‚‚)) (πŸ™ A) }, -- { slice_lhs 2 3 {rw square.commutes f₁}, -- rw [assoc, assoc, ← prod_functorial, limit.lift_Ο€], refl } }, -- { apply pullback.lift _ _ _, -- { apply pullback.snd ≫ square.top fβ‚‚ }, -- { apply pullback.fst ≫ f₁ ≫ limits.prod.map (limits.prod.lift (hat f₁) (hat fβ‚‚)) (πŸ™ A) }, -- { slice_lhs 2 3 {rw square.commutes fβ‚‚}, -- slice_lhs 1 2 {rw ← pullback.condition}, -- rw [assoc, assoc, assoc, ← prod_functorial, limit.lift_Ο€], refl } }, -- { rw [limit.lift_Ο€], dsimp, erw [limit.lift_Ο€], dsimp, refl } }, -- { slice_lhs 2 3 {rw square.commutes (intersect.both A)}, -- dunfold intersect.both, -- slice_lhs 1 2 {rw limit.lift_Ο€}, -- dsimp, -- slice_lhs 1 2 {rw limit.lift_Ο€}, -- dsimp, -- rw [assoc, assoc, assoc], -- rw prod_functorial, -- refl }, -- { refine ⟨_, _, _⟩, -- { intro s, -- set toB' : s.X ⟢ pullback (intersect.left A) (intersect.right A) := (square.is_pullback (intersect.both A)).lift (pullback_cone.mk (pullback_cone.fst s) (pullback_cone.snd s ≫ limits.prod.map (limits.prod.lift (hat f₁) (hat fβ‚‚)) (πŸ™ A)) _), -- } -- } -- end
f4f964543a3e7a86e86683dea67ca41900aff38c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/complex/schwarz.lean
39c31bbd8930272288f7071647cf20d570fb7b7a
[ "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
10,042
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.complex.abs_max import analysis.complex.removable_singularity /-! # Schwarz lemma In this file we prove several versions of the Schwarz lemma. * `complex.norm_deriv_le_div_of_maps_to_ball`, `complex.abs_deriv_le_div_of_maps_to_ball`: if `f : β„‚ β†’ E` sends an open disk with center `c` and a positive radius `R₁` to an open ball with center `f c` and radius `Rβ‚‚`, then the absolute value of the derivative of `f` at `c` is at most the ratio `Rβ‚‚ / R₁`; * `complex.dist_le_div_mul_dist_of_maps_to_ball`: if `f : β„‚ β†’ E` sends an open disk with center `c` and radius `R₁` to an open disk with center `f c` and radius `Rβ‚‚`, then for any `z` in the former disk we have `dist (f z) (f c) ≀ (Rβ‚‚ / R₁) * dist z c`; * `complex.abs_deriv_le_one_of_maps_to_ball`: if `f : β„‚ β†’ β„‚` sends an open disk of positive radius to itself and the center of this disk to itself, then the absolute value of the derivative of `f` at the center of this disk is at most `1`; * `complex.dist_le_dist_of_maps_to_ball`: if `f : β„‚ β†’ β„‚` sends an open disk to itself and the center `c` of this disk to itself, then for any point `z` of this disk we have `dist (f z) c ≀ dist z c`; * `complex.abs_le_abs_of_maps_to_ball`: if `f : β„‚ β†’ β„‚` sends an open disk with center `0` to itself, then for any point `z` of this disk we have `abs (f z) ≀ abs z`. ## Implementation notes We prove some versions of the Schwarz lemma for a map `f : β„‚ β†’ E` taking values in any normed space over complex numbers. ## TODO * Prove that these inequalities are strict unless `f` is an affine map. * Prove that any diffeomorphism of the unit disk to itself is a MΓΆbius map. ## Tags Schwarz lemma -/ open metric set function filter topological_space open_locale topological_space namespace complex section space variables {E : Type*} [normed_add_comm_group E] [normed_space β„‚ E] {R R₁ Rβ‚‚ : ℝ} {f : β„‚ β†’ E} {c z zβ‚€ : β„‚} /-- An auxiliary lemma for `complex.norm_dslope_le_div_of_maps_to_ball`. -/ lemma schwarz_aux {f : β„‚ β†’ β„‚} (hd : differentiable_on β„‚ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) Rβ‚‚)) (hz : z ∈ ball c R₁) : β€–dslope f c zβ€– ≀ Rβ‚‚ / R₁ := begin have hR₁ : 0 < R₁, from nonempty_ball.1 ⟨z, hz⟩, suffices : βˆ€αΆ  r in 𝓝[<] R₁, β€–dslope f c zβ€– ≀ Rβ‚‚ / r, { refine ge_of_tendsto _ this, exact (tendsto_const_nhds.div tendsto_id hR₁.ne').mono_left nhds_within_le_nhds }, rw mem_ball at hz, filter_upwards [Ioo_mem_nhds_within_Iio ⟨hz, le_rfl⟩] with r hr, have hrβ‚€ : 0 < r, from dist_nonneg.trans_lt hr.1, replace hd : diff_cont_on_cl β„‚ (dslope f c) (ball c r), { refine differentiable_on.diff_cont_on_cl _, rw closure_ball c hrβ‚€.ne', exact ((differentiable_on_dslope $ ball_mem_nhds _ hR₁).mpr hd).mono (closed_ball_subset_ball hr.2) }, refine norm_le_of_forall_mem_frontier_norm_le bounded_ball hd _ _, { rw frontier_ball c hrβ‚€.ne', intros z hz, have hz' : z β‰  c, from ne_of_mem_sphere hz hrβ‚€.ne', rw [dslope_of_ne _ hz', slope_def_module, norm_smul, norm_inv, mem_sphere_iff_norm.1 hz, ← div_eq_inv_mul, div_le_div_right hrβ‚€, ← dist_eq_norm], exact le_of_lt (h_maps (mem_ball.2 (by { rw mem_sphere.1 hz, exact hr.2 }))) }, { rw [closure_ball c hrβ‚€.ne', mem_closed_ball], exact hr.1.le } end /-- Two cases of the **Schwarz Lemma** (derivative and distance), merged together. -/ lemma norm_dslope_le_div_of_maps_to_ball (hd : differentiable_on β„‚ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) Rβ‚‚)) (hz : z ∈ ball c R₁) : β€–dslope f c zβ€– ≀ Rβ‚‚ / R₁ := begin have hR₁ : 0 < R₁, from nonempty_ball.1 ⟨z, hz⟩, have hRβ‚‚ : 0 < Rβ‚‚, from nonempty_ball.1 ⟨f z, h_maps hz⟩, cases eq_or_ne (dslope f c z) 0 with hc hc, { rw [hc, norm_zero], exact div_nonneg hRβ‚‚.le hR₁.le }, rcases exists_dual_vector β„‚ _ hc with ⟨g, hg, hgf⟩, have hg' : β€–gβ€–β‚Š = 1, from nnreal.eq hg, have hgβ‚€ : β€–gβ€–β‚Š β‰  0, by simpa only [hg'] using one_ne_zero, calc β€–dslope f c zβ€– = β€–dslope (g ∘ f) c zβ€– : begin rw [g.dslope_comp, hgf, is_R_or_C.norm_of_real, norm_norm], exact Ξ» _, hd.differentiable_at (ball_mem_nhds _ hR₁) end ... ≀ Rβ‚‚ / R₁ : begin refine schwarz_aux (g.differentiable.comp_differentiable_on hd) (maps_to.comp _ h_maps) hz, simpa only [hg', nnreal.coe_one, one_mul] using g.lipschitz.maps_to_ball hgβ‚€ (f c) Rβ‚‚ end end /-- Equality case in the **Schwarz Lemma**: in the setup of `norm_dslope_le_div_of_maps_to_ball`, if `β€–dslope f c zβ‚€β€– = Rβ‚‚ / R₁` holds at a point in the ball then the map `f` is affine. -/ lemma affine_of_maps_to_ball_of_exists_norm_dslope_eq_div [complete_space E] [strict_convex_space ℝ E] (hd : differentiable_on β„‚ f (ball c R₁)) (h_maps : set.maps_to f (ball c R₁) (ball (f c) Rβ‚‚)) (h_zβ‚€ : zβ‚€ ∈ ball c R₁) (h_eq : β€–dslope f c zβ‚€β€– = Rβ‚‚ / R₁) : set.eq_on f (Ξ» z, f c + (z - c) β€’ dslope f c zβ‚€) (ball c R₁) := begin set g := dslope f c, rintro z hz, by_cases z = c, { simp [h] }, have h_R₁ : 0 < R₁ := nonempty_ball.mp ⟨_, h_zβ‚€βŸ©, have g_le_div : βˆ€ z ∈ ball c R₁, β€–g zβ€– ≀ Rβ‚‚ / R₁, from Ξ» z hz, norm_dslope_le_div_of_maps_to_ball hd h_maps hz, have g_max : is_max_on (norm ∘ g) (ball c R₁) zβ‚€, from is_max_on_iff.mpr (Ξ» z hz, by simpa [h_eq] using g_le_div z hz), have g_diff : differentiable_on β„‚ g (ball c R₁), from (differentiable_on_dslope (is_open_ball.mem_nhds (mem_ball_self h_R₁))).mpr hd, have : g z = g zβ‚€ := eq_on_of_is_preconnected_of_is_max_on_norm (convex_ball c R₁).is_preconnected is_open_ball g_diff h_zβ‚€ g_max hz, simp [← this] end lemma affine_of_maps_to_ball_of_exists_norm_dslope_eq_div' [complete_space E] [strict_convex_space ℝ E] (hd : differentiable_on β„‚ f (ball c R₁)) (h_maps : set.maps_to f (ball c R₁) (ball (f c) Rβ‚‚)) (h_zβ‚€ : βˆƒ zβ‚€ ∈ ball c R₁, β€–dslope f c zβ‚€β€– = Rβ‚‚ / R₁) : βˆƒ C : E, β€–Cβ€– = Rβ‚‚ / R₁ ∧ set.eq_on f (Ξ» z, f c + (z - c) β€’ C) (ball c R₁) := let ⟨zβ‚€, h_zβ‚€, h_eq⟩ := h_zβ‚€ in ⟨dslope f c zβ‚€, h_eq, affine_of_maps_to_ball_of_exists_norm_dslope_eq_div hd h_maps h_zβ‚€ h_eq⟩ /-- The **Schwarz Lemma**: if `f : β„‚ β†’ E` sends an open disk with center `c` and a positive radius `R₁` to an open ball with center `f c` and radius `Rβ‚‚`, then the absolute value of the derivative of `f` at `c` is at most the ratio `Rβ‚‚ / R₁`. -/ lemma norm_deriv_le_div_of_maps_to_ball (hd : differentiable_on β„‚ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) Rβ‚‚)) (hβ‚€ : 0 < R₁) : β€–deriv f cβ€– ≀ Rβ‚‚ / R₁ := by simpa only [dslope_same] using norm_dslope_le_div_of_maps_to_ball hd h_maps (mem_ball_self hβ‚€) /-- The **Schwarz Lemma**: if `f : β„‚ β†’ E` sends an open disk with center `c` and radius `R₁` to an open ball with center `f c` and radius `Rβ‚‚`, then for any `z` in the former disk we have `dist (f z) (f c) ≀ (Rβ‚‚ / R₁) * dist z c`. -/ lemma dist_le_div_mul_dist_of_maps_to_ball (hd : differentiable_on β„‚ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) Rβ‚‚)) (hz : z ∈ ball c R₁) : dist (f z) (f c) ≀ (Rβ‚‚ / R₁) * dist z c := begin rcases eq_or_ne z c with rfl|hne, { simp only [dist_self, mul_zero] }, simpa only [dslope_of_ne _ hne, slope_def_module, norm_smul, norm_inv, ← div_eq_inv_mul, ← dist_eq_norm, div_le_iff (dist_pos.2 hne)] using norm_dslope_le_div_of_maps_to_ball hd h_maps hz end end space variables {f : β„‚ β†’ β„‚} {c z : β„‚} {R R₁ Rβ‚‚ : ℝ} /-- The **Schwarz Lemma**: if `f : β„‚ β†’ β„‚` sends an open disk with center `c` and a positive radius `R₁` to an open disk with center `f c` and radius `Rβ‚‚`, then the absolute value of the derivative of `f` at `c` is at most the ratio `Rβ‚‚ / R₁`. -/ lemma abs_deriv_le_div_of_maps_to_ball (hd : differentiable_on β„‚ f (ball c R₁)) (h_maps : maps_to f (ball c R₁) (ball (f c) Rβ‚‚)) (hβ‚€ : 0 < R₁) : abs (deriv f c) ≀ Rβ‚‚ / R₁ := norm_deriv_le_div_of_maps_to_ball hd h_maps hβ‚€ /-- The **Schwarz Lemma**: if `f : β„‚ β†’ β„‚` sends an open disk of positive radius to itself and the center of this disk to itself, then the absolute value of the derivative of `f` at the center of this disk is at most `1`. -/ lemma abs_deriv_le_one_of_maps_to_ball (hd : differentiable_on β„‚ f (ball c R)) (h_maps : maps_to f (ball c R) (ball c R)) (hc : f c = c) (hβ‚€ : 0 < R) : abs (deriv f c) ≀ 1 := (norm_deriv_le_div_of_maps_to_ball hd (by rwa hc) hβ‚€).trans_eq (div_self hβ‚€.ne') /-- The **Schwarz Lemma**: if `f : β„‚ β†’ β„‚` sends an open disk to itself and the center `c` of this disk to itself, then for any point `z` of this disk we have `dist (f z) c ≀ dist z c`. -/ lemma dist_le_dist_of_maps_to_ball_self (hd : differentiable_on β„‚ f (ball c R)) (h_maps : maps_to f (ball c R) (ball c R)) (hc : f c = c) (hz : z ∈ ball c R) : dist (f z) c ≀ dist z c := have hR : 0 < R, from nonempty_ball.1 ⟨z, hz⟩, by simpa only [hc, div_self hR.ne', one_mul] using dist_le_div_mul_dist_of_maps_to_ball hd (by rwa hc) hz /-- The **Schwarz Lemma**: if `f : β„‚ β†’ β„‚` sends an open disk with center `0` to itself, the for any point `z` of this disk we have `abs (f z) ≀ abs z`. -/ lemma abs_le_abs_of_maps_to_ball_self (hd : differentiable_on β„‚ f (ball 0 R)) (h_maps : maps_to f (ball 0 R) (ball 0 R)) (hβ‚€ : f 0 = 0) (hz : abs z < R) : abs (f z) ≀ abs z := begin replace hz : z ∈ ball (0 : β„‚) R, from mem_ball_zero_iff.2 hz, simpa only [dist_zero_right] using dist_le_dist_of_maps_to_ball_self hd h_maps hβ‚€ hz end end complex
1b67e05c8bdde55e4eba78f6ba8d32506c5a0639
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/stage0/src/Lean/Elab/Inductive.lean
e35c6fc570ca4388c591ef5fc3871475a60c5743
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
24,701
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.ReplaceLevel import Lean.Util.ReplaceExpr import Lean.Util.CollectLevelParams import Lean.Util.Constructions import Lean.Meta.CollectFVars import Lean.Meta.SizeOf import Lean.Elab.Command import Lean.Elab.DefView import Lean.Elab.DeclUtil import Lean.Elab.Deriving.Basic namespace Lean.Elab.Command open Meta builtin_initialize registerTraceClass `Elab.inductive def checkValidInductiveModifier [Monad m] [MonadError m] (modifiers : Modifiers) : m Unit := do if modifiers.isNoncomputable then throwError "invalid use of 'noncomputable' in inductive declaration" if modifiers.isPartial then throwError "invalid use of 'partial' in inductive declaration" unless modifiers.attrs.size == 0 || (modifiers.attrs.size == 1 && modifiers.attrs[0].name == `class) do throwError "invalid use of attributes in inductive declaration" def checkValidCtorModifier [Monad m] [MonadError m] (modifiers : Modifiers) : m Unit := do if modifiers.isNoncomputable then throwError "invalid use of 'noncomputable' in constructor declaration" if modifiers.isPartial then throwError "invalid use of 'partial' in constructor declaration" if modifiers.isUnsafe then throwError "invalid use of 'unsafe' in constructor declaration" if modifiers.attrs.size != 0 then throwError "invalid use of attributes in constructor declaration" structure CtorView where ref : Syntax modifiers : Modifiers inferMod : Bool -- true if `{}` is used in the constructor declaration declName : Name binders : Syntax type? : Option Syntax deriving Inhabited structure InductiveView where ref : Syntax modifiers : Modifiers shortDeclName : Name declName : Name levelNames : List Name binders : Syntax type? : Option Syntax ctors : Array CtorView derivingClasses : Array DerivingClassView deriving Inhabited structure ElabHeaderResult where view : InductiveView lctx : LocalContext localInsts : LocalInstances params : Array Expr type : Expr deriving Inhabited private partial def elabHeaderAux (views : Array InductiveView) (i : Nat) (acc : Array ElabHeaderResult) : TermElabM (Array ElabHeaderResult) := do if h : i < views.size then let view := views.get ⟨i, h⟩ let acc ← Term.withAutoBoundImplicit <| Term.elabBinders view.binders.getArgs fun params => do match view.type? with | none => let u ← mkFreshLevelMVar let type := mkSort u Term.synthesizeSyntheticMVarsNoPostponing let params ← Term.addAutoBoundImplicits params pure <| acc.push { lctx := (← getLCtx), localInsts := (← getLocalInstances), params := params, type := type, view := view } | some typeStx => let type ← Term.elabType typeStx unless (← isTypeFormerType type) do throwErrorAt typeStx "invalid inductive type, resultant type is not a sort" Term.synthesizeSyntheticMVarsNoPostponing let params ← Term.addAutoBoundImplicits params pure <| acc.push { lctx := (← getLCtx), localInsts := (← getLocalInstances), params := params, type := type, view := view } elabHeaderAux views (i+1) acc else pure acc private def checkNumParams (rs : Array ElabHeaderResult) : TermElabM Nat := do let numParams := rs[0].params.size for r in rs do unless r.params.size == numParams do throwErrorAt r.view.ref "invalid inductive type, number of parameters mismatch in mutually inductive datatypes" pure numParams private def checkUnsafe (rs : Array ElabHeaderResult) : TermElabM Unit := do let isUnsafe := rs[0].view.modifiers.isUnsafe for r in rs do unless r.view.modifiers.isUnsafe == isUnsafe do throwErrorAt r.view.ref "invalid inductive type, cannot mix unsafe and safe declarations in a mutually inductive datatypes" private def checkLevelNames (views : Array InductiveView) : TermElabM Unit := do if views.size > 1 then let levelNames := views[0].levelNames for view in views do unless view.levelNames == levelNames do throwErrorAt view.ref "invalid inductive type, universe parameters mismatch in mutually inductive datatypes" private def mkTypeFor (r : ElabHeaderResult) : TermElabM Expr := do withLCtx r.lctx r.localInsts do mkForallFVars r.params r.type private def throwUnexpectedInductiveType {Ξ±} : TermElabM Ξ± := throwError "unexpected inductive resulting type" private def eqvFirstTypeResult (firstType type : Expr) : MetaM Bool := forallTelescopeReducing firstType fun _ firstTypeResult => isDefEq firstTypeResult type -- Auxiliary function for checking whether the types in mutually inductive declaration are compatible. private partial def checkParamsAndResultType (type firstType : Expr) (numParams : Nat) : TermElabM Unit := do try forallTelescopeCompatible type firstType numParams fun _ type firstType => forallTelescopeReducing type fun _ type => forallTelescopeReducing firstType fun _ firstType => do match type with | Expr.sort .. => unless (← isDefEq firstType type) do throwError "resulting universe mismatch, given{indentExpr type}\nexpected type{indentExpr firstType}" | _ => throwError "unexpected inductive resulting type" catch | Exception.error ref msg => throw (Exception.error ref m!"invalid mutually inductive types, {msg}") | ex => throw ex -- Auxiliary function for checking whether the types in mutually inductive declaration are compatible. private def checkHeader (r : ElabHeaderResult) (numParams : Nat) (firstType? : Option Expr) : TermElabM Expr := do let type ← mkTypeFor r match firstType? with | none => pure type | some firstType => withRef r.view.ref $ checkParamsAndResultType type firstType numParams pure firstType -- Auxiliary function for checking whether the types in mutually inductive declaration are compatible. private partial def checkHeaders (rs : Array ElabHeaderResult) (numParams : Nat) (i : Nat) (firstType? : Option Expr) : TermElabM Unit := do if i < rs.size then let type ← checkHeader rs[i] numParams firstType? checkHeaders rs numParams (i+1) type private def elabHeader (views : Array InductiveView) : TermElabM (Array ElabHeaderResult) := do let rs ← elabHeaderAux views 0 #[] if rs.size > 1 then checkUnsafe rs let numParams ← checkNumParams rs checkHeaders rs numParams 0 none return rs /- Create a local declaration for each inductive type in `rs`, and execute `x params indFVars`, where `params` are the inductive type parameters and `indFVars` are the new local declarations. We use the local context/instances and parameters of rs[0]. Note that this method is executed after we executed `checkHeaders` and established all parameters are compatible. -/ private partial def withInductiveLocalDecls {Ξ±} (rs : Array ElabHeaderResult) (x : Array Expr β†’ Array Expr β†’ TermElabM Ξ±) : TermElabM Ξ± := do let namesAndTypes ← rs.mapM fun r => do let type ← mkTypeFor r pure (r.view.shortDeclName, type) let r0 := rs[0] let params := r0.params withLCtx r0.lctx r0.localInsts $ withRef r0.view.ref do let rec loop (i : Nat) (indFVars : Array Expr) := do if h : i < namesAndTypes.size then let (id, type) := namesAndTypes.get ⟨i, h⟩ withLocalDeclD id type fun indFVar => loop (i+1) (indFVars.push indFVar) else x params indFVars loop 0 #[] private def isInductiveFamily (numParams : Nat) (indFVar : Expr) : TermElabM Bool := do let indFVarType ← inferType indFVar forallTelescopeReducing indFVarType fun xs _ => return xs.size > numParams /- Elaborate constructor types. Remark: we check whether the resulting type is correct, and the parameter occurrences are consistent, but we currently do not check for: - Positivity (it is a rare failure, and the kernel already checks for it). - Universe constraints (the kernel checks for it). -/ private def elabCtors (indFVars : Array Expr) (indFVar : Expr) (params : Array Expr) (r : ElabHeaderResult) : TermElabM (List Constructor) := withRef r.view.ref do let indFamily ← isInductiveFamily params.size indFVar r.view.ctors.toList.mapM fun ctorView => Term.withAutoBoundImplicit <| Term.elabBinders ctorView.binders.getArgs fun ctorParams => withRef ctorView.ref do let rec elabCtorType (k : Expr β†’ TermElabM Constructor) : TermElabM Constructor := do match ctorView.type? with | none => if indFamily then throwError "constructor resulting type must be specified in inductive family declaration" k <| mkAppN indFVar params | some ctorType => let type ← Term.elabType ctorType Term.synthesizeSyntheticMVars (mayPostpone := true) let type ← instantiateMVars type let type ← checkParamOccs type forallTelescopeReducing type fun _ resultingType => do unless resultingType.getAppFn == indFVar do throwError "unexpected constructor resulting type{indentExpr resultingType}" unless (← isType resultingType) do throwError "unexpected constructor resulting type, type expected{indentExpr resultingType}" k type elabCtorType fun type => do Term.synthesizeSyntheticMVarsNoPostponing let ctorParams ← Term.addAutoBoundImplicits ctorParams let type ← mkForallFVars ctorParams type let type ← mkForallFVars params type return { name := ctorView.declName, type := type } where checkParamOccs (ctorType : Expr) : MetaM Expr := let visit (e : Expr) : MetaM TransformStep := do let f := e.getAppFn if indFVars.contains f then let mut args := e.getAppArgs unless args.size β‰₯ params.size do throwError "unexpected inductive type occurrence{indentExpr e}" for i in [:params.size] do let param := params[i] let arg := args[i] unless (← isDefEq param arg) do throwError "inductive datatype parameter mismatch{indentExpr arg}\nexpected{indentExpr param}" args := args.set! i param return TransformStep.done (mkAppN f args) else return TransformStep.visit e transform ctorType (pre := visit) /- Convert universe metavariables occurring in the `indTypes` into new parameters. Remark: if the resulting inductive datatype has universe metavariables, we will fix it later using `inferResultingUniverse`. -/ private def levelMVarToParamAux (indTypes : List InductiveType) : StateRefT Nat TermElabM (List InductiveType) := indTypes.mapM fun indType => do let type ← Term.levelMVarToParam' indType.type let ctors ← indType.ctors.mapM fun ctor => do let ctorType ← Term.levelMVarToParam' ctor.type pure { ctor with type := ctorType } pure { indType with ctors := ctors, type := type } private def levelMVarToParam (indTypes : List InductiveType) : TermElabM (List InductiveType) := (levelMVarToParamAux indTypes).run' 1 private def getResultingUniverse : List InductiveType β†’ TermElabM Level | [] => throwError "unexpected empty inductive declaration" | indType :: _ => forallTelescopeReducing indType.type fun _ r => do match r with | Expr.sort u _ => pure u | _ => throwError "unexpected inductive type resulting type" def tmpIndParam := mkLevelParam `_tmp_ind_univ_param /-- Return true if `u` is of the form `?m + k`. Return false if `u` does not contain universe metavariables. Throw exception otherwise. -/ def shouldInferResultUniverse (u : Level) : TermElabM Bool := do let u ← instantiateLevelMVars u if u.hasMVar then match u.getLevelOffset with | Level.mvar mvarId _ => do Term.assignLevelMVar mvarId tmpIndParam pure true | _ => throwError "cannot infer resulting universe level of inductive datatype, given level contains metavariables {mkSort u}, provide universe explicitly" else pure false /- Auxiliary function for `updateResultingUniverse` `accLevelAtCtor u r rOffset us` add `u` components to `us` if they are not already there and it is different from the resulting universe level `r+rOffset`. If `u` is a `max`, then its components are recursively processed. If `u` is a `succ` and `rOffset > 0`, we process the `u`s child using `rOffset-1`. This method is used to infer the resulting universe level of an inductive datatype. -/ def accLevelAtCtor : Level β†’ Level β†’ Nat β†’ Array Level β†’ TermElabM (Array Level) | Level.max u v _, r, rOffset, us => do let us ← accLevelAtCtor u r rOffset us; accLevelAtCtor v r rOffset us | Level.imax u v _, r, rOffset, us => do let us ← accLevelAtCtor u r rOffset us; accLevelAtCtor v r rOffset us | Level.zero _, _, _, us => pure us | Level.succ u _, r, rOffset+1, us => accLevelAtCtor u r rOffset us | u, r, rOffset, us => if rOffset == 0 && u == r then pure us else if r.occurs u then throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly" else if rOffset > 0 then throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly" else if us.contains u then pure us else pure (us.push u) /- Auxiliary function for `updateResultingUniverse` -/ private partial def collectUniversesFromCtorTypeAux (r : Level) (rOffset : Nat) : Nat β†’ Expr β†’ Array Level β†’ TermElabM (Array Level) | 0, Expr.forallE n d b c, us => do let u ← getLevel d let u ← instantiateLevelMVars u let us ← accLevelAtCtor u r rOffset us withLocalDecl n c.binderInfo d fun x => let e := b.instantiate1 x collectUniversesFromCtorTypeAux r rOffset 0 e us | i+1, Expr.forallE n d b c, us => do withLocalDecl n c.binderInfo d fun x => let e := b.instantiate1 x collectUniversesFromCtorTypeAux r rOffset i e us | _, _, us => pure us /- Auxiliary function for `updateResultingUniverse` -/ private partial def collectUniversesFromCtorType (r : Level) (rOffset : Nat) (ctorType : Expr) (numParams : Nat) (us : Array Level) : TermElabM (Array Level) := collectUniversesFromCtorTypeAux r rOffset numParams ctorType us /- Auxiliary function for `updateResultingUniverse` -/ private partial def collectUniverses (r : Level) (rOffset : Nat) (numParams : Nat) (indTypes : List InductiveType) : TermElabM (Array Level) := do let mut us := #[] for indType in indTypes do for ctor in indType.ctors do us ← collectUniversesFromCtorType r rOffset ctor.type numParams us return us def mkResultUniverse (us : Array Level) (rOffset : Nat) : Level := if us.isEmpty && rOffset == 0 then levelOne else let r := Level.mkNaryMax us.toList if rOffset == 0 && !r.isZero && !r.isNeverZero then (mkLevelMax r levelOne).normalize else r.normalize private def updateResultingUniverse (numParams : Nat) (indTypes : List InductiveType) : TermElabM (List InductiveType) := do let r ← getResultingUniverse indTypes let rOffset : Nat := r.getOffset let r : Level := r.getLevelOffset unless r.isParam do throwError "failed to compute resulting universe level of inductive datatype, provide universe explicitly" let us ← collectUniverses r rOffset numParams indTypes trace[Elab.inductive] "updateResultingUniverse us: {us}, r: {r}, rOffset: {rOffset}" let rNew := mkResultUniverse us rOffset let updateLevel (e : Expr) : Expr := e.replaceLevel fun u => if u == tmpIndParam then some rNew else none return indTypes.map fun indType => let type := updateLevel indType.type; let ctors := indType.ctors.map fun ctor => { ctor with type := updateLevel ctor.type }; { indType with type := type, ctors := ctors } register_builtin_option bootstrap.inductiveCheckResultingUniverse : Bool := { defValue := true, group := "bootstrap", descr := "by default the `inductive/structure commands report an error if the resulting universe is not zero, but may be zero for some universe parameters. Reason: unless this type is a subsingleton, it is hardly what the user wants since it can only eliminate into `Prop`. In the `Init` package, we define subsingletons, and we use this option to disable the check. This option may be deleted in the future after we improve the validator" } def checkResultingUniverse (u : Level) : TermElabM Unit := do if bootstrap.inductiveCheckResultingUniverse.get (← getOptions) then let u ← instantiateLevelMVars u if !u.isZero && !u.isNeverZero then throwError "invalid universe polymorphic type, the resultant universe is not Prop (i.e., 0), but it may be Prop for some parameter values (solution: use 'u+1' or 'max 1 u'{indentD u}" private def checkResultingUniverses (indTypes : List InductiveType) : TermElabM Unit := do checkResultingUniverse (← getResultingUniverse indTypes) private def collectUsed (indTypes : List InductiveType) : StateRefT CollectFVars.State MetaM Unit := do indTypes.forM fun indType => do Term.collectUsedFVars indType.type indType.ctors.forM fun ctor => Term.collectUsedFVars ctor.type private def removeUnused (vars : Array Expr) (indTypes : List InductiveType) : TermElabM (LocalContext Γ— LocalInstances Γ— Array Expr) := do let (_, used) ← (collectUsed indTypes).run {} Term.removeUnused vars used private def withUsed {Ξ±} (vars : Array Expr) (indTypes : List InductiveType) (k : Array Expr β†’ TermElabM Ξ±) : TermElabM Ξ± := do let (lctx, localInsts, vars) ← removeUnused vars indTypes withLCtx lctx localInsts $ k vars private def updateParams (vars : Array Expr) (indTypes : List InductiveType) : TermElabM (List InductiveType) := indTypes.mapM fun indType => do let type ← mkForallFVars vars indType.type let ctors ← indType.ctors.mapM fun ctor => do let ctorType ← mkForallFVars vars ctor.type pure { ctor with type := ctorType } pure { indType with type := type, ctors := ctors } private def collectLevelParamsInInductive (indTypes : List InductiveType) : Array Name := do let mut usedParams : CollectLevelParams.State := {} for indType in indTypes do usedParams := collectLevelParams usedParams indType.type for ctor in indType.ctors do usedParams := collectLevelParams usedParams ctor.type return usedParams.params private def mkIndFVar2Const (views : Array InductiveView) (indFVars : Array Expr) (levelNames : List Name) : ExprMap Expr := do let levelParams := levelNames.map mkLevelParam; let mut m : ExprMap Expr := {} for i in [:views.size] do let view := views[i] let indFVar := indFVars[i] m := m.insert indFVar (mkConst view.declName levelParams) return m /- Remark: `numVars <= numParams`. `numVars` is the number of context `variables` used in the inductive declaration, and `numParams` is `numVars` + number of explicit parameters provided in the declaration. -/ private def replaceIndFVarsWithConsts (views : Array InductiveView) (indFVars : Array Expr) (levelNames : List Name) (numVars : Nat) (numParams : Nat) (indTypes : List InductiveType) : TermElabM (List InductiveType) := let indFVar2Const := mkIndFVar2Const views indFVars levelNames indTypes.mapM fun indType => do let ctors ← indType.ctors.mapM fun ctor => do let type ← forallBoundedTelescope ctor.type numParams fun params type => do let type := type.replace fun e => if !e.isFVar then none else match indFVar2Const.find? e with | none => none | some c => mkAppN c (params.extract 0 numVars) mkForallFVars params type pure { ctor with type := type } pure { indType with ctors := ctors } abbrev Ctor2InferMod := Std.HashMap Name Bool private def mkCtor2InferMod (views : Array InductiveView) : Ctor2InferMod := do let mut m := {} for view in views do for ctorView in view.ctors do m := m.insert ctorView.declName ctorView.inferMod return m private def applyInferMod (views : Array InductiveView) (numParams : Nat) (indTypes : List InductiveType) : List InductiveType := let ctor2InferMod := mkCtor2InferMod views indTypes.map fun indType => let ctors := indType.ctors.map fun ctor => let inferMod := ctor2InferMod.find! ctor.name -- true if `{}` was used let ctorType := ctor.type.inferImplicit numParams !inferMod { ctor with type := ctorType } { indType with ctors := ctors } private def mkAuxConstructions (views : Array InductiveView) : TermElabM Unit := do let env ← getEnv let hasEq := env.contains ``Eq let hasHEq := env.contains ``HEq let hasUnit := env.contains ``PUnit let hasProd := env.contains ``Prod for view in views do let n := view.declName mkRecOn n if hasUnit then mkCasesOn n if hasUnit && hasEq && hasHEq then mkNoConfusion n if hasUnit && hasProd then mkBelow n if hasUnit && hasProd then mkIBelow n for view in views do let n := view.declName; if hasUnit && hasProd then mkBRecOn n if hasUnit && hasProd then mkBInductionOn n private def mkInductiveDecl (vars : Array Expr) (views : Array InductiveView) : TermElabM Unit := do let view0 := views[0] let scopeLevelNames ← Term.getLevelNames checkLevelNames views let allUserLevelNames := view0.levelNames let isUnsafe := view0.modifiers.isUnsafe withRef view0.ref <| Term.withLevelNames allUserLevelNames do let rs ← elabHeader views withInductiveLocalDecls rs fun params indFVars => do let numExplicitParams := params.size let mut indTypes := #[] for i in [:views.size] do let indFVar := indFVars[i] let r := rs[i] let type ← mkForallFVars params r.type let ctors ← elabCtors indFVars indFVar params r indTypes := indTypes.push { name := r.view.declName, type := type, ctors := ctors : InductiveType } let indTypes := indTypes.toList Term.synthesizeSyntheticMVarsNoPostponing let u ← getResultingUniverse indTypes let inferLevel ← shouldInferResultUniverse u withUsed vars indTypes fun vars => do let numVars := vars.size let numParams := numVars + numExplicitParams let indTypes ← updateParams vars indTypes let indTypes ← levelMVarToParam indTypes let indTypes ← if inferLevel then updateResultingUniverse numParams indTypes else checkResultingUniverses indTypes; pure indTypes let usedLevelNames := collectLevelParamsInInductive indTypes match sortDeclLevelParams scopeLevelNames allUserLevelNames usedLevelNames with | Except.error msg => throwError msg | Except.ok levelParams => do let indTypes ← replaceIndFVarsWithConsts views indFVars levelParams numVars numParams indTypes let indTypes := applyInferMod views numParams indTypes let decl := Declaration.inductDecl levelParams numParams indTypes isUnsafe Term.ensureNoUnassignedMVars decl addDecl decl mkAuxConstructions views -- We need to invoke `applyAttributes` because `class` is implemented as an attribute. for view in views do Term.applyAttributesAt view.declName view.modifiers.attrs AttributeApplicationTime.afterTypeChecking private def applyDerivingHandlers (views : Array InductiveView) : CommandElabM Unit := do let mut processed : NameSet := {} for view in views do for classView in view.derivingClasses do let className := classView.className unless processed.contains className do processed := processed.insert className let mut declNames := #[] for view in views do if view.derivingClasses.any fun classView => classView.className == className then declNames := declNames.push view.declName classView.applyHandlers declNames def elabInductiveViews (views : Array InductiveView) : CommandElabM Unit := do let view0 := views[0] let ref := view0.ref runTermElabM view0.declName fun vars => withRef ref do mkInductiveDecl vars views mkSizeOfInstances view0.declName applyDerivingHandlers views end Lean.Elab.Command
f6e743c5446f24c8905ab545b95895c3ba5acb3c
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/ptrAddr.lean
0b963244799c16e9369c75c5f302b13837a33ed1
[ "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
465
lean
axiom TrustMe {p : Prop} : p def x := (1, 2) def y := x @[noinline] def mk (v : Nat) := (v, v+1) #eval withPtrAddr x (fun a => dbgTrace (">> " ++ toString a) $ fun _ => 0) TrustMe #eval withPtrEq x y (fun _ => dbgTrace (">> " ++ toString x ++ " == " ++ toString y) $ fun _ => x == y) TrustMe -- should not print message #eval withPtrEq x (mk 1) (fun _ => dbgTrace (">> " ++ toString x ++ " == " ++ toString y) $ fun _ => x == y) TrustMe -- should print message
0afad5c38f6a1c66030fbce2113f79bd266ec9ea
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/run/eq15.lean
10671d8b2ea3a09821457a200a6d28757dd50142
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
452
lean
import data.list open list set_option pp.implicit true definition append : Ξ  {A : Type}, list A β†’ list A β†’ list A, append nil l := l, append (h :: t) l := h :: (append t l) theorem append_nil {A : Type} (l : list A) : append nil l = l := rfl theorem append_cons {A : Type} (h : A) (t l : list A) : append (h :: t) l = h :: (append t l) := rfl example : append (1 :: 2 :: nil) (3 :: 4 :: 5 :: nil) = (1 :: 2 :: 3 :: 4 :: 5 :: nil) := rfl
8053ab9b61bb65d281c41a847c1a8cee06dea3e6
2a2864136cf8f2871e86f5fd14e624e3daa8fd24
/Monads/Applicative.lean
4dc476eaf48fb4178d8c29fa88310384315b1dae
[ "MIT" ]
permissive
hargoniX/lean-monads
d054ac71a351b7c86f318a477977cc166117b8ec
2e87ca7ddf394641ea1b16bcbd8c384026d68e2f
refs/heads/main
1,693,530,528,286
1,633,100,386,000
1,633,100,386,000
412,509,769
1
0
null
null
null
null
UTF-8
Lean
false
false
4,757
lean
import Monads.Functor namespace Monads /- Applicative functors allow us to apply functions that are already wrapped withing the functor to other values wrapped withing the functor. This allows chaining of methods like so: f <$> g <*> h <*> x. -/ class Applicative (f : Type u β†’ Type v) extends Functor f where /- Lift a normal value into an applicative context. -/ pure : Ξ± β†’ f Ξ± /- Apply a function inside an applicative to a value inside an applicative. -/ apply : βˆ€ {Ξ± Ξ² : Type u}, f (Ξ± β†’ Ξ²) β†’ f Ξ± β†’ f Ξ² /- Lift a funcion of arity 2 (2 parameters) to the applicative context. -/ liftA2 : (Ξ± β†’ Ξ² β†’ Ξ³) β†’ f Ξ± β†’ f Ξ² β†’ f Ξ³ := Ξ» g x y => apply (g <$> x) y /- Drop the action on the left and only return the right one. If left contains e.g. a sideffect it will still be executed. -/ seq_right : f Ξ± β†’ f Ξ² β†’ f Ξ² := Ξ» a1 a2 => apply (id <$ a1) a2 /- Drop the action on the right and only return the left one. If right contains e.g. a sideffect it will still be executed. -/ seq_left : f Ξ± β†’ f Ξ² β†’ f Ξ± := liftA2 (Function.const Ξ²) export Applicative (pure liftA2) infixl:60 " <*> " => Monads.Applicative.apply -- The arrow points at the value that is kept infixl:60 " *> " => Monads.Applicative.seq_right infixl:60 " <* " => Monads.Applicative.seq_left /- Lawful applicatives essentially capture the same concept of modifying only the contents and not the context they are captured in as a lawful regular functor. -/ class LawfulApplicative (f : Type u β†’ Type v) [app: Applicative f] extends LawfulFunctor f : Prop where /- If the function maps values to themselves the values in the applicative shall remain unchanged. -/ apply_id : βˆ€ x : f Ξ±, pure id <*> x = x /- Applying a pure function to a pure value with <*> shall be the same as first applying them outside of the applicative and wrapping the result into it. -/ apply_homomorphism: βˆ€ (x : Ξ±) (g : Ξ± β†’ Ξ²), pure g <*> app.pure x = pure (g x) /- Applying an effectful function to a pure value should yield the same result, regardless of order. -/ apply_interchange: βˆ€ {Ξ± Ξ² : Type u} (x : Ξ±) (g : f (Ξ± β†’ Ξ²)), g <*> pure x = pure (Β· $ x) <*> g /- A modified associativity property for <*>. -/ apply_comp : βˆ€ {Ξ± Ξ² Ξ³: Type u} (x : f (Ξ² β†’ Ξ³)) (y : f (Ξ± β†’ Ξ²)) (z : f Ξ±), pure (@Function.comp Ξ± Ξ² Ξ³) <*> x <*> y <*> z = x <*> (y <*> z) /- If there is a custom liftA2 definition it has to behave like the default one -/ lifta2_behaved : βˆ€ (g : Ξ± β†’ Ξ² β†’ Ξ³) (x : f Ξ±) (y : f Ξ²), liftA2 g x y = g <$> x <*> y /- If there is a custom *> definition it has to behave like the default one -/ seq_right_behaved : βˆ€ (a1 : f Ξ±) (a2 : f Ξ²), a1 *> a2 = (id <$ a1) <*> a2 /- If there is a custom <* definition it has to behave like the default one -/ seq_left_behaved : βˆ€ (a1 : f Ξ±) (a2 : f Ξ²), a1 <* a2 = liftA2 (Function.const Ξ²) a1 a2 /- This law is rather intuitive and provable with the free theorem in Haskell. However I'm unsure whether it applies in lean and no formalization exists, hence it will be an axiom. (Mathlib does this too). Furthermore it is shown below that the right hand side is a lawful functor. -/ fmap_eq_pure_apply : βˆ€ (g : Ξ± β†’ Ξ²) (x : f Ξ±), g <$> x = app.pure g <*> x namespace LawfulApplicative variable {f : Type u β†’ Type v} [Applicative f] [LawfulApplicative f] -- These 4 laws allow us to prove equality of any term involving only <*> or pure (if they are equal) example {Ξ± Ξ² : Type u} {g : f (Ξ± β†’ Ξ²)} {x : f Ξ±} : g <*> (pure id <*> x) = g <*> x := by rw [apply_id] example {Ξ± Ξ² : Type u} {g : f (Ξ± β†’ Ξ²)} {x : f Ξ±} : pure id <*> (g <*> x) = g <*> x := by rw [apply_id] example {Ξ± Ξ² Ξ³ Ξ΄ Ξ΅: Type u} {h : f (Ξ΄ β†’ Ξ΅)}{g : Ξ± β†’ Ξ² β†’ Ξ³ β†’ Ξ΄} {x : Ξ±} { y : Ξ²} { z : Ξ³} : h <*> (pure g <*> pure x <*> pure y <*> pure z) = h <*> pure (g x y z) := by simp only [apply_homomorphism] -- Constructing a lawful functor from a lawful applicative def apply_fmap : (Ξ± β†’ Ξ²) β†’ f Ξ± β†’ f Ξ² := Ξ» g a => pure g <*> a -- First law example : βˆ€ {Ξ± : Type u} (x : f Ξ±), apply_fmap id x = x := by intro Ξ± x simp only [apply_fmap] rw [apply_id] -- Second law example : βˆ€ {Ξ± Ξ² Ξ³ : Type u} (g : Ξ± β†’ Ξ²) (h : Ξ² β†’ Ξ³) (x : f Ξ±), apply_fmap (h ∘ g) x = apply_fmap h (apply_fmap g x) := by intro Ξ± Ξ² Ξ³ g h x simp only [apply_fmap] rw [←apply_comp] rw [apply_homomorphism] rw [apply_homomorphism] end LawfulApplicative end Monads
43d03a918cd36dfb66f98578d183b4b39393ec91
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/pp_struct.lean
14ea0caae3ade40a0e68c703a6b1e7abaee06fb6
[ "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,250
lean
structure foo := (x : nat) (y : nat) (z : bool) #check let s := {foo . x := let v1 := 10 + 10 + 20 + 30 + 40 + 10 + 20 + 30 + 40 + 50 + 10 + 10, v2 := 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 20, v3 := 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 20, v4 := 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 20 in v1 + v2 + v3 + v4, y := 20, z := tt} in s^.x + s^.y set_option pp.structure_instances_qualifier true #check let s := {foo . x := let v1 := 10 + 10 + 20 + 30 + 40 + 10 + 20 + 30 + 40 + 50 + 10 + 10, v2 := 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 20, v3 := 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 20, v4 := 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 20 in v1 + v2 + v3 + v4, y := 20, z := tt} in s^.x + s^.y set_option pp.structure_instances false #check {foo . x := 10, y := 20, z := ff} set_option pp.structure_instances true #check {foo . x := 10, y := 20, z := ff} set_option pp.structure_instances_qualifier false #check {foo . x := 10, y := 20, z := ff} #check {foo . x := 10, y := 20, z := ff}^.x #check (1, 2).1 constant boo : nat β†’ nat β†’ nat Γ— nat #check (boo 1 1)^.fst structure F := (fn : nat β†’ nat β†’ nat) (v : nat) constant h : nat β†’ F #check (h 0)^.fn 10 20
5d7483956fba331dc0ee8dc2c3fea0b8380ae58b
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/convex/cone.lean
795a445305b492184f7f039c88474dab267c9048
[ "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
25,236
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, FrΓ©dΓ©ric Dupuis -/ import analysis.convex.hull import analysis.inner_product_space.basic /-! # Convex cones In a `π•œ`-module `E`, we define a convex cone as a set `s` such that `a β€’ x + b β€’ y ∈ s` whenever `x, y ∈ s` and `a, b > 0`. We prove that convex cones form a `complete_lattice`, and define their images (`convex_cone.map`) and preimages (`convex_cone.comap`) under linear maps. We define pointed, blunt, flat and salient cones, and prove the correspondence between convex cones and ordered modules. We also define `convex.to_cone` to be the minimal cone that includes a given convex set. We define `set.inner_dual_cone` to be the cone consisting of all points `y` such that for all points `x` in a given set `0 ≀ βŸͺ x, y ⟫`. ## Main statements We prove two extension theorems: * `riesz_extension`: [M. Riesz extension theorem](https://en.wikipedia.org/wiki/M._Riesz_extension_theorem) says that if `s` is a convex cone in a real vector space `E`, `p` is a submodule of `E` such that `p + s = E`, and `f` is a linear function `p β†’ ℝ` which is nonnegative on `p ∩ s`, then there exists a globally defined linear function `g : E β†’ ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. * `exists_extension_of_le_sublinear`: Hahn-Banach theorem: if `N : E β†’ ℝ` is a sublinear map, `f` is a linear map defined on a subspace of `E`, and `f x ≀ N x` for all `x` in the domain of `f`, then `f` can be extended to the whole space to a linear map `g` such that `g x ≀ N x` for all `x` ## Implementation notes While `convex π•œ` is a predicate on sets, `convex_cone π•œ E` is a bundled convex cone. ## References * https://en.wikipedia.org/wiki/Convex_cone -/ open set linear_map open_locale classical pointwise variables {π•œ E F G : Type*} /-! ### Definition of `convex_cone` and basic properties -/ section definitions variables (π•œ E) [ordered_semiring π•œ] /-- A convex cone is a subset `s` of a `π•œ`-module such that `a β€’ x + b β€’ y ∈ s` whenever `a, b > 0` and `x, y ∈ s`. -/ structure convex_cone [add_comm_monoid E] [has_smul π•œ E] := (carrier : set E) (smul_mem' : βˆ€ ⦃c : π•œβ¦„, 0 < c β†’ βˆ€ ⦃x : E⦄, x ∈ carrier β†’ c β€’ x ∈ carrier) (add_mem' : βˆ€ ⦃x⦄ (hx : x ∈ carrier) ⦃y⦄ (hy : y ∈ carrier), x + y ∈ carrier) end definitions variables {π•œ E} namespace convex_cone section ordered_semiring variables [ordered_semiring π•œ] [add_comm_monoid E] section has_smul variables [has_smul π•œ E] (S T : convex_cone π•œ E) instance : has_coe (convex_cone π•œ E) (set E) := ⟨convex_cone.carrier⟩ instance : has_mem E (convex_cone π•œ E) := ⟨λ m S, m ∈ S.carrier⟩ instance : has_le (convex_cone π•œ E) := ⟨λ S T, S.carrier βŠ† T.carrier⟩ instance : has_lt (convex_cone π•œ E) := ⟨λ S T, S.carrier βŠ‚ T.carrier⟩ @[simp, norm_cast] lemma mem_coe {x : E} : x ∈ (S : set E) ↔ x ∈ S := iff.rfl @[simp] lemma mem_mk {s : set E} {h₁ hβ‚‚ x} : x ∈ @mk π•œ _ _ _ _ s h₁ hβ‚‚ ↔ x ∈ s := iff.rfl /-- Two `convex_cone`s are equal if the underlying sets are equal. -/ theorem ext' {S T : convex_cone π•œ E} (h : (S : set E) = T) : S = T := by cases S; cases T; congr' /-- Two `convex_cone`s are equal if and only if the underlying sets are equal. -/ protected theorem ext'_iff {S T : convex_cone π•œ E} : (S : set E) = T ↔ S = T := ⟨ext', Ξ» h, h β–Έ rfl⟩ /-- Two `convex_cone`s are equal if they have the same elements. -/ @[ext] theorem ext {S T : convex_cone π•œ E} (h : βˆ€ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h lemma smul_mem {c : π•œ} {x : E} (hc : 0 < c) (hx : x ∈ S) : c β€’ x ∈ S := S.smul_mem' hc hx lemma add_mem ⦃x⦄ (hx : x ∈ S) ⦃y⦄ (hy : y ∈ S) : x + y ∈ S := S.add_mem' hx hy instance : has_inf (convex_cone π•œ E) := ⟨λ S T, ⟨S ∩ T, Ξ» c hc x hx, ⟨S.smul_mem hc hx.1, T.smul_mem hc hx.2⟩, Ξ» x hx y hy, ⟨S.add_mem hx.1 hy.1, T.add_mem hx.2 hy.2⟩⟩⟩ lemma coe_inf : ((S βŠ“ T : convex_cone π•œ E) : set E) = ↑S ∩ ↑T := rfl lemma mem_inf {x} : x ∈ S βŠ“ T ↔ x ∈ S ∧ x ∈ T := iff.rfl instance : has_Inf (convex_cone π•œ E) := ⟨λ S, βŸ¨β‹‚ s ∈ S, ↑s, Ξ» c hc x hx, mem_bInter $ Ξ» s hs, s.smul_mem hc $ mem_Interβ‚‚.1 hx s hs, Ξ» x hx y hy, mem_bInter $ Ξ» s hs, s.add_mem (mem_Interβ‚‚.1 hx s hs) (mem_Interβ‚‚.1 hy s hs)⟩⟩ lemma mem_Inf {x : E} {S : set (convex_cone π•œ E)} : x ∈ Inf S ↔ βˆ€ s ∈ S, x ∈ s := mem_Interβ‚‚ variables (π•œ) instance : has_bot (convex_cone π•œ E) := βŸ¨βŸ¨βˆ…, Ξ» c hc x, false.elim, Ξ» x, false.elim⟩⟩ lemma mem_bot (x : E) : x ∈ (βŠ₯ : convex_cone π•œ E) = false := rfl instance : has_top (convex_cone π•œ E) := ⟨⟨univ, Ξ» c hc x hx, mem_univ _, Ξ» x hx y hy, mem_univ _⟩⟩ lemma mem_top (x : E) : x ∈ (⊀ : convex_cone π•œ E) := mem_univ x instance : complete_lattice (convex_cone π•œ E) := { le := (≀), lt := (<), bot := (βŠ₯), bot_le := Ξ» S x, false.elim, top := (⊀), le_top := Ξ» S x hx, mem_top π•œ x, inf := (βŠ“), Inf := has_Inf.Inf, sup := Ξ» a b, Inf {x | a ≀ x ∧ b ≀ x}, Sup := Ξ» s, Inf {T | βˆ€ S ∈ s, S ≀ T}, le_sup_left := Ξ» a b, Ξ» x hx, mem_Inf.2 $ Ξ» s hs, hs.1 hx, le_sup_right := Ξ» a b, Ξ» x hx, mem_Inf.2 $ Ξ» s hs, hs.2 hx, sup_le := Ξ» a b c ha hb x hx, mem_Inf.1 hx c ⟨ha, hb⟩, le_inf := Ξ» a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := Ξ» a b x, and.left, inf_le_right := Ξ» a b x, and.right, le_Sup := Ξ» s p hs x hx, mem_Inf.2 $ Ξ» t ht, ht p hs hx, Sup_le := Ξ» s p hs x hx, mem_Inf.1 hx p hs, le_Inf := Ξ» s a ha x hx, mem_Inf.2 $ Ξ» t ht, ha t ht hx, Inf_le := Ξ» s a ha x hx, mem_Inf.1 hx _ ha, .. partial_order.lift (coe : convex_cone π•œ E β†’ set E) (Ξ» a b, ext') } instance : inhabited (convex_cone π•œ E) := ⟨βŠ₯⟩ end has_smul section module variables [module π•œ E] (S : convex_cone π•œ E) protected lemma convex : convex π•œ (S : set E) := convex_iff_forall_pos.2 $ Ξ» x y hx hy a b ha hb hab, S.add_mem (S.smul_mem ha hx) (S.smul_mem hb hy) end module end ordered_semiring section linear_ordered_field variables [linear_ordered_field π•œ] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] [add_comm_monoid G] section mul_action variables [mul_action π•œ E] (S : convex_cone π•œ E) lemma smul_mem_iff {c : π•œ} (hc : 0 < c) {x : E} : c β€’ x ∈ S ↔ x ∈ S := ⟨λ h, inv_smul_smulβ‚€ hc.ne' x β–Έ S.smul_mem (inv_pos.2 hc) h, S.smul_mem hc⟩ end mul_action section module variables [module π•œ E] [module π•œ F] [module π•œ G] /-- The image of a convex cone under a `π•œ`-linear map is a convex cone. -/ def map (f : E β†’β‚—[π•œ] F) (S : convex_cone π•œ E) : convex_cone π•œ F := { carrier := f '' S, smul_mem' := Ξ» c hc y ⟨x, hx, hy⟩, hy β–Έ f.map_smul c x β–Έ mem_image_of_mem f (S.smul_mem hc hx), add_mem' := Ξ» y₁ ⟨x₁, hx₁, hyβ‚βŸ© yβ‚‚ ⟨xβ‚‚, hxβ‚‚, hyβ‚‚βŸ©, hy₁ β–Έ hyβ‚‚ β–Έ f.map_add x₁ xβ‚‚ β–Έ mem_image_of_mem f (S.add_mem hx₁ hxβ‚‚) } lemma map_map (g : F β†’β‚—[π•œ] G) (f : E β†’β‚—[π•œ] F) (S : convex_cone π•œ E) : (S.map f).map g = S.map (g.comp f) := ext' $ image_image g f S @[simp] lemma map_id (S : convex_cone π•œ E) : S.map linear_map.id = S := ext' $ image_id _ /-- The preimage of a convex cone under a `π•œ`-linear map is a convex cone. -/ def comap (f : E β†’β‚—[π•œ] F) (S : convex_cone π•œ F) : convex_cone π•œ E := { carrier := f ⁻¹' S, smul_mem' := Ξ» c hc x hx, by { rw [mem_preimage, f.map_smul c], exact S.smul_mem hc hx }, add_mem' := Ξ» x hx y hy, by { rw [mem_preimage, f.map_add], exact S.add_mem hx hy } } @[simp] lemma comap_id (S : convex_cone π•œ E) : S.comap linear_map.id = S := ext' preimage_id lemma comap_comap (g : F β†’β‚—[π•œ] G) (f : E β†’β‚—[π•œ] F) (S : convex_cone π•œ G) : (S.comap g).comap f = S.comap (g.comp f) := ext' $ preimage_comp.symm @[simp] lemma mem_comap {f : E β†’β‚—[π•œ] F} {S : convex_cone π•œ F} {x : E} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl end module end add_comm_monoid section ordered_add_comm_group variables [ordered_add_comm_group E] [module π•œ E] /-- Constructs an ordered module given an `ordered_add_comm_group`, a cone, and a proof that the order relation is the one defined by the cone. -/ lemma to_ordered_smul (S : convex_cone π•œ E) (h : βˆ€ x y : E, x ≀ y ↔ y - x ∈ S) : ordered_smul π•œ E := ordered_smul.mk' begin intros x y z xy hz, rw [h (z β€’ x) (z β€’ y), ←smul_sub z y x], exact smul_mem S hz ((h x y).mp xy.le), end end ordered_add_comm_group end linear_ordered_field /-! ### Convex cones with extra properties -/ section ordered_semiring variables [ordered_semiring π•œ] section add_comm_monoid variables [add_comm_monoid E] [has_smul π•œ E] (S : convex_cone π•œ E) /-- A convex cone is pointed if it includes `0`. -/ def pointed (S : convex_cone π•œ E) : Prop := (0 : E) ∈ S /-- A convex cone is blunt if it doesn't include `0`. -/ def blunt (S : convex_cone π•œ E) : Prop := (0 : E) βˆ‰ S lemma pointed_iff_not_blunt (S : convex_cone π•œ E) : S.pointed ↔ Β¬S.blunt := ⟨λ h₁ hβ‚‚, hβ‚‚ h₁, not_not.mp⟩ lemma blunt_iff_not_pointed (S : convex_cone π•œ E) : S.blunt ↔ Β¬S.pointed := by rw [pointed_iff_not_blunt, not_not] end add_comm_monoid section add_comm_group variables [add_comm_group E] [has_smul π•œ E] (S : convex_cone π•œ E) /-- A convex cone is flat if it contains some nonzero vector `x` and its opposite `-x`. -/ def flat : Prop := βˆƒ x ∈ S, x β‰  (0 : E) ∧ -x ∈ S /-- A convex cone is salient if it doesn't include `x` and `-x` for any nonzero `x`. -/ def salient : Prop := βˆ€ x ∈ S, x β‰  (0 : E) β†’ -x βˆ‰ S lemma salient_iff_not_flat (S : convex_cone π•œ E) : S.salient ↔ Β¬S.flat := begin split, { rintros h₁ ⟨x, xs, H₁, Hβ‚‚βŸ©, exact h₁ x xs H₁ Hβ‚‚ }, { intro h, unfold flat at h, push_neg at h, exact h } end /-- A flat cone is always pointed (contains `0`). -/ lemma flat.pointed {S : convex_cone π•œ E} (hS : S.flat) : S.pointed := begin obtain ⟨x, hx, _, hxneg⟩ := hS, rw [pointed, ←add_neg_self x], exact add_mem S hx hxneg, end /-- A blunt cone (one not containing `0`) is always salient. -/ lemma blunt.salient {S : convex_cone π•œ E} : S.blunt β†’ S.salient := begin rw [salient_iff_not_flat, blunt_iff_not_pointed], exact mt flat.pointed, end /-- A pointed convex cone defines a preorder. -/ def to_preorder (h₁ : S.pointed) : preorder E := { le := Ξ» x y, y - x ∈ S, le_refl := Ξ» x, by change x - x ∈ S; rw [sub_self x]; exact h₁, le_trans := Ξ» x y z xy zy, by simpa using add_mem S zy xy } /-- A pointed and salient cone defines a partial order. -/ def to_partial_order (h₁ : S.pointed) (hβ‚‚ : S.salient) : partial_order E := { le_antisymm := begin intros a b ab ba, by_contradiction h, have h' : b - a β‰  0 := Ξ» h'', h (eq_of_sub_eq_zero h'').symm, have H := hβ‚‚ (b-a) ab h', rw neg_sub b a at H, exact H ba, end, ..to_preorder S h₁ } /-- A pointed and salient cone defines an `ordered_add_comm_group`. -/ def to_ordered_add_comm_group (h₁ : S.pointed) (hβ‚‚ : S.salient) : ordered_add_comm_group E := { add_le_add_left := begin intros a b hab c, change c + b - (c + a) ∈ S, rw add_sub_add_left_eq_sub, exact hab, end, ..to_partial_order S h₁ hβ‚‚, ..show add_comm_group E, by apply_instance } end add_comm_group end ordered_semiring /-! ### Positive cone of an ordered module -/ section positive_cone variables (π•œ E) [ordered_semiring π•œ] [ordered_add_comm_group E] [module π•œ E] [ordered_smul π•œ E] /-- The positive cone is the convex cone formed by the set of nonnegative elements in an ordered module. -/ def positive_cone : convex_cone π•œ E := { carrier := {x | 0 ≀ x}, smul_mem' := begin rintro c hc x (hx : _ ≀ _), rw ←smul_zero c, exact smul_le_smul_of_nonneg hx hc.le, end, add_mem' := Ξ» x (hx : _ ≀ _) y (hy : _ ≀ _), add_nonneg hx hy } /-- The positive cone of an ordered module is always salient. -/ lemma salient_positive_cone : salient (positive_cone π•œ E) := Ξ» x xs hx hx', lt_irrefl (0 : E) (calc 0 < x : lt_of_le_of_ne xs hx.symm ... ≀ x + (-x) : le_add_of_nonneg_right hx' ... = 0 : add_neg_self x) /-- The positive cone of an ordered module is always pointed. -/ lemma pointed_positive_cone : pointed (positive_cone π•œ E) := le_refl 0 end positive_cone end convex_cone /-! ### Cone over a convex set -/ section cone_from_convex variables [linear_ordered_field π•œ] [ordered_add_comm_group E] [module π•œ E] namespace convex /-- The set of vectors proportional to those in a convex set forms a convex cone. -/ def to_cone (s : set E) (hs : convex π•œ s) : convex_cone π•œ E := begin apply convex_cone.mk (⋃ (c : π•œ) (H : 0 < c), c β€’ s); simp only [mem_Union, mem_smul_set], { rintros c c_pos _ ⟨c', c'_pos, x, hx, rfl⟩, exact ⟨c * c', mul_pos c_pos c'_pos, x, hx, (smul_smul _ _ _).symm⟩ }, { rintros _ ⟨cx, cx_pos, x, hx, rfl⟩ _ ⟨cy, cy_pos, y, hy, rfl⟩, have : 0 < cx + cy, from add_pos cx_pos cy_pos, refine ⟨_, this, _, convex_iff_div.1 hs hx hy cx_pos.le cy_pos.le this, _⟩, simp only [smul_add, smul_smul, mul_div_assoc', mul_div_cancel_left _ this.ne'] } end variables {s : set E} (hs : convex π•œ s) {x : E} lemma mem_to_cone : x ∈ hs.to_cone s ↔ βˆƒ (c : π•œ), 0 < c ∧ βˆƒ y ∈ s, c β€’ y = x := by simp only [to_cone, convex_cone.mem_mk, mem_Union, mem_smul_set, eq_comm, exists_prop] lemma mem_to_cone' : x ∈ hs.to_cone s ↔ βˆƒ (c : π•œ), 0 < c ∧ c β€’ x ∈ s := begin refine hs.mem_to_cone.trans ⟨_, _⟩, { rintros ⟨c, hc, y, hy, rfl⟩, exact ⟨c⁻¹, inv_pos.2 hc, by rwa [smul_smul, inv_mul_cancel hc.ne', one_smul]⟩ }, { rintros ⟨c, hc, hcx⟩, exact ⟨c⁻¹, inv_pos.2 hc, _, hcx, by rw [smul_smul, inv_mul_cancel hc.ne', one_smul]⟩ } end lemma subset_to_cone : s βŠ† hs.to_cone s := Ξ» x hx, hs.mem_to_cone'.2 ⟨1, zero_lt_one, by rwa one_smul⟩ /-- `hs.to_cone s` is the least cone that includes `s`. -/ lemma to_cone_is_least : is_least { t : convex_cone π•œ E | s βŠ† t } (hs.to_cone s) := begin refine ⟨hs.subset_to_cone, Ξ» t ht x hx, _⟩, rcases hs.mem_to_cone.1 hx with ⟨c, hc, y, hy, rfl⟩, exact t.smul_mem hc (ht hy) end lemma to_cone_eq_Inf : hs.to_cone s = Inf { t : convex_cone π•œ E | s βŠ† t } := hs.to_cone_is_least.is_glb.Inf_eq.symm end convex lemma convex_hull_to_cone_is_least (s : set E) : is_least {t : convex_cone π•œ E | s βŠ† t} ((convex_convex_hull π•œ s).to_cone _) := begin convert (convex_convex_hull π•œ s).to_cone_is_least, ext t, exact ⟨λ h, convex_hull_min h t.convex, (subset_convex_hull π•œ s).trans⟩, end lemma convex_hull_to_cone_eq_Inf (s : set E) : (convex_convex_hull π•œ s).to_cone _ = Inf {t : convex_cone π•œ E | s βŠ† t} := (convex_hull_to_cone_is_least s).is_glb.Inf_eq.symm end cone_from_convex /-! ### M. Riesz extension theorem Given a convex cone `s` in a vector space `E`, a submodule `p`, and a linear `f : p β†’ ℝ`, assume that `f` is nonnegative on `p ∩ s` and `p + s = E`. Then there exists a globally defined linear function `g : E β†’ ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. We prove this theorem using Zorn's lemma. `riesz_extension.step` is the main part of the proof. It says that if the domain `p` of `f` is not the whole space, then `f` can be extended to a larger subspace `p βŠ” span ℝ {y}` without breaking the non-negativity condition. In `riesz_extension.exists_top` we use Zorn's lemma to prove that we can extend `f` to a linear map `g` on `⊀ : submodule E`. Mathematically this is the same as a linear map on `E` but in Lean `⊀ : submodule E` is isomorphic but is not equal to `E`. In `riesz_extension` we use this isomorphism to prove the theorem. -/ variables [add_comm_group E] [module ℝ E] namespace riesz_extension open submodule variables (s : convex_cone ℝ E) (f : linear_pmap ℝ E ℝ) /-- Induction step in M. Riesz extension theorem. Given a convex cone `s` in a vector space `E`, a partially defined linear map `f : f.domain β†’ ℝ`, assume that `f` is nonnegative on `f.domain ∩ p` and `p + s = E`. If `f` is not defined on the whole `E`, then we can extend it to a larger submodule without breaking the non-negativity condition. -/ lemma step (nonneg : βˆ€ x : f.domain, (x : E) ∈ s β†’ 0 ≀ f x) (dense : βˆ€ y, βˆƒ x : f.domain, (x : E) + y ∈ s) (hdom : f.domain β‰  ⊀) : βˆƒ g, f < g ∧ βˆ€ x : g.domain, (x : E) ∈ s β†’ 0 ≀ g x := begin obtain ⟨y, -, hy⟩ : βˆƒ (y : E) (h : y ∈ ⊀), y βˆ‰ f.domain, { exact @set_like.exists_of_lt (submodule ℝ E) _ _ _ _ (lt_top_iff_ne_top.2 hdom) }, obtain ⟨c, le_c, c_le⟩ : βˆƒ c, (βˆ€ x : f.domain, -(x:E) - y ∈ s β†’ f x ≀ c) ∧ (βˆ€ x : f.domain, (x:E) + y ∈ s β†’ c ≀ f x), { set Sp := f '' {x : f.domain | (x:E) + y ∈ s}, set Sn := f '' {x : f.domain | -(x:E) - y ∈ s}, suffices : (upper_bounds Sn ∩ lower_bounds Sp).nonempty, by simpa only [set.nonempty, upper_bounds, lower_bounds, ball_image_iff] using this, refine exists_between_of_forall_le (nonempty.image f _) (nonempty.image f (dense y)) _, { rcases (dense (-y)) with ⟨x, hx⟩, rw [← neg_neg x, add_subgroup_class.coe_neg, ← sub_eq_add_neg] at hx, exact ⟨_, hx⟩ }, rintros a ⟨xn, hxn, rfl⟩ b ⟨xp, hxp, rfl⟩, have := s.add_mem hxp hxn, rw [add_assoc, add_sub_cancel'_right, ← sub_eq_add_neg, ← add_subgroup_class.coe_sub] at this, replace := nonneg _ this, rwa [f.map_sub, sub_nonneg] at this }, have hy' : y β‰  0, from Ξ» hyβ‚€, hy (hyβ‚€.symm β–Έ zero_mem _), refine ⟨f.sup_span_singleton y (-c) hy, _, _⟩, { refine lt_iff_le_not_le.2 ⟨f.left_le_sup _ _, Ξ» H, _⟩, replace H := linear_pmap.domain_mono.monotone H, rw [linear_pmap.domain_sup_span_singleton, sup_le_iff, span_le, singleton_subset_iff] at H, exact hy H.2 }, { rintros ⟨z, hz⟩ hzs, rcases mem_sup.1 hz with ⟨x, hx, y', hy', rfl⟩, rcases mem_span_singleton.1 hy' with ⟨r, rfl⟩, simp only [subtype.coe_mk] at hzs, erw [linear_pmap.sup_span_singleton_apply_mk _ _ _ _ _ hx, smul_neg, ← sub_eq_add_neg, sub_nonneg], rcases lt_trichotomy r 0 with hr|hr|hr, { have : -(r⁻¹ β€’ x) - y ∈ s, by rwa [← s.smul_mem_iff (neg_pos.2 hr), smul_sub, smul_neg, neg_smul, neg_neg, smul_smul, mul_inv_cancel hr.ne, one_smul, sub_eq_add_neg, neg_smul, neg_neg], replace := le_c (r⁻¹ β€’ ⟨x, hx⟩) this, rwa [← mul_le_mul_left (neg_pos.2 hr), neg_mul, neg_mul, neg_le_neg_iff, f.map_smul, smul_eq_mul, ← mul_assoc, mul_inv_cancel hr.ne, one_mul] at this }, { subst r, simp only [zero_smul, add_zero] at hzs ⊒, apply nonneg, exact hzs }, { have : r⁻¹ β€’ x + y ∈ s, by rwa [← s.smul_mem_iff hr, smul_add, smul_smul, mul_inv_cancel hr.ne', one_smul], replace := c_le (r⁻¹ β€’ ⟨x, hx⟩) this, rwa [← mul_le_mul_left hr, f.map_smul, smul_eq_mul, ← mul_assoc, mul_inv_cancel hr.ne', one_mul] at this } } end theorem exists_top (p : linear_pmap ℝ E ℝ) (hp_nonneg : βˆ€ x : p.domain, (x : E) ∈ s β†’ 0 ≀ p x) (hp_dense : βˆ€ y, βˆƒ x : p.domain, (x : E) + y ∈ s) : βˆƒ q β‰₯ p, q.domain = ⊀ ∧ βˆ€ x : q.domain, (x : E) ∈ s β†’ 0 ≀ q x := begin replace hp_nonneg : p ∈ { p | _ }, by { rw mem_set_of_eq, exact hp_nonneg }, obtain ⟨q, hqs, hpq, hq⟩ := zorn_nonempty_partial_orderβ‚€ _ _ _ hp_nonneg, { refine ⟨q, hpq, _, hqs⟩, contrapose! hq, rcases step s q hqs _ hq with ⟨r, hqr, hr⟩, { exact ⟨r, hr, hqr.le, hqr.ne'⟩ }, { exact Ξ» y, let ⟨x, hx⟩ := hp_dense y in ⟨of_le hpq.left x, hx⟩ } }, { intros c hcs c_chain y hy, clear hp_nonneg hp_dense p, have cne : c.nonempty := ⟨y, hy⟩, refine ⟨linear_pmap.Sup c c_chain.directed_on, _, Ξ» _, linear_pmap.le_Sup c_chain.directed_on⟩, rintros ⟨x, hx⟩ hxs, have hdir : directed_on (≀) (linear_pmap.domain '' c), from directed_on_image.2 (c_chain.directed_on.mono linear_pmap.domain_mono.monotone), rcases (mem_Sup_of_directed (cne.image _) hdir).1 hx with ⟨_, ⟨f, hfc, rfl⟩, hfx⟩, have : f ≀ linear_pmap.Sup c c_chain.directed_on, from linear_pmap.le_Sup _ hfc, convert ← hcs hfc ⟨x, hfx⟩ hxs, apply this.2, refl } end end riesz_extension /-- M. **Riesz extension theorem**: given a convex cone `s` in a vector space `E`, a submodule `p`, and a linear `f : p β†’ ℝ`, assume that `f` is nonnegative on `p ∩ s` and `p + s = E`. Then there exists a globally defined linear function `g : E β†’ ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. -/ theorem riesz_extension (s : convex_cone ℝ E) (f : linear_pmap ℝ E ℝ) (nonneg : βˆ€ x : f.domain, (x : E) ∈ s β†’ 0 ≀ f x) (dense : βˆ€ y, βˆƒ x : f.domain, (x : E) + y ∈ s) : βˆƒ g : E β†’β‚—[ℝ] ℝ, (βˆ€ x : f.domain, g x = f x) ∧ (βˆ€ x ∈ s, 0 ≀ g x) := begin rcases riesz_extension.exists_top s f nonneg dense with ⟨⟨g_dom, g⟩, ⟨hpg, hfg⟩, htop, hgs⟩, clear hpg, refine ⟨g βˆ˜β‚— ↑(linear_equiv.of_top _ htop).symm, _, _⟩; simp only [comp_apply, linear_equiv.coe_coe, linear_equiv.of_top_symm_apply], { exact Ξ» x, (hfg (submodule.coe_mk _ _).symm).symm }, { exact Ξ» x hx, hgs ⟨x, _⟩ hx } end /-- **Hahn-Banach theorem**: if `N : E β†’ ℝ` is a sublinear map, `f` is a linear map defined on a subspace of `E`, and `f x ≀ N x` for all `x` in the domain of `f`, then `f` can be extended to the whole space to a linear map `g` such that `g x ≀ N x` for all `x`. -/ theorem exists_extension_of_le_sublinear (f : linear_pmap ℝ E ℝ) (N : E β†’ ℝ) (N_hom : βˆ€ (c : ℝ), 0 < c β†’ βˆ€ x, N (c β€’ x) = c * N x) (N_add : βˆ€ x y, N (x + y) ≀ N x + N y) (hf : βˆ€ x : f.domain, f x ≀ N x) : βˆƒ g : E β†’β‚—[ℝ] ℝ, (βˆ€ x : f.domain, g x = f x) ∧ (βˆ€ x, g x ≀ N x) := begin let s : convex_cone ℝ (E Γ— ℝ) := { carrier := {p : E Γ— ℝ | N p.1 ≀ p.2 }, smul_mem' := Ξ» c hc p hp, calc N (c β€’ p.1) = c * N p.1 : N_hom c hc p.1 ... ≀ c * p.2 : mul_le_mul_of_nonneg_left hp hc.le, add_mem' := Ξ» x hx y hy, (N_add _ _).trans (add_le_add hx hy) }, obtain ⟨g, g_eq, g_nonneg⟩ := riesz_extension s ((-f).coprod (linear_map.id.to_pmap ⊀)) _ _; try { simp only [linear_pmap.coprod_apply, to_pmap_apply, id_apply, linear_pmap.neg_apply, ← sub_eq_neg_add, sub_nonneg, subtype.coe_mk] at * }, replace g_eq : βˆ€ (x : f.domain) (y : ℝ), g (x, y) = y - f x, { intros x y, simpa only [subtype.coe_mk, subtype.coe_eta] using g_eq ⟨(x, y), ⟨x.2, trivial⟩⟩ }, { refine ⟨-g.comp (inl ℝ E ℝ), _, _⟩; simp only [neg_apply, inl_apply, comp_apply], { intro x, simp [g_eq x 0] }, { intro x, have A : (x, N x) = (x, 0) + (0, N x), by simp, have B := g_nonneg ⟨x, N x⟩ (le_refl (N x)), rw [A, map_add, ← neg_le_iff_add_nonneg'] at B, have C := g_eq 0 (N x), simp only [submodule.coe_zero, f.map_zero, sub_zero] at C, rwa ← C } }, { exact Ξ» x hx, le_trans (hf _) hx }, { rintros ⟨x, y⟩, refine ⟨⟨(0, N x - y), ⟨f.domain.zero_mem, trivial⟩⟩, _⟩, simp only [convex_cone.mem_mk, mem_set_of_eq, subtype.coe_mk, prod.fst_add, prod.snd_add, zero_add, sub_add_cancel] } end /-! ### The dual cone -/ section dual variables {H : Type*} [inner_product_space ℝ H] (s t : set H) open_locale real_inner_product_space /-- The dual cone is the cone consisting of all points `y` such that for all points `x` in a given set `0 ≀ βŸͺ x, y ⟫`. -/ def set.inner_dual_cone (s : set H) : convex_cone ℝ H := { carrier := { y | βˆ€ x ∈ s, 0 ≀ βŸͺ x, y ⟫ }, smul_mem' := Ξ» c hc y hy x hx, begin rw real_inner_smul_right, exact mul_nonneg hc.le (hy x hx) end, add_mem' := Ξ» u hu v hv x hx, begin rw inner_add_right, exact add_nonneg (hu x hx) (hv x hx) end } lemma mem_inner_dual_cone (y : H) (s : set H) : y ∈ s.inner_dual_cone ↔ βˆ€ x ∈ s, 0 ≀ βŸͺ x, y ⟫ := by refl @[simp] lemma inner_dual_cone_empty : (βˆ… : set H).inner_dual_cone = ⊀ := convex_cone.ext' (eq_univ_of_forall (Ξ» x y hy, false.elim (set.not_mem_empty _ hy))) lemma inner_dual_cone_le_inner_dual_cone (h : t βŠ† s) : s.inner_dual_cone ≀ t.inner_dual_cone := Ξ» y hy x hx, hy x (h hx) lemma pointed_inner_dual_cone : s.inner_dual_cone.pointed := Ξ» x hx, by rw inner_zero_right end dual
8bde818b9c7d142c3f8432cac9f87b0b70ce07c7
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/linear_algebra/basic.lean
297b86398c6814f47cccde92640018204b3996ed
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
101,838
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 -/ import algebra.big_operators.pi import algebra.module.pi import algebra.module.prod import algebra.module.submodule import algebra.group.prod import data.finsupp.basic import algebra.pointwise /-! # Linear algebra This file defines the basics of linear algebra. It sets up the "categorical/lattice structure" of modules over a ring, submodules, and linear maps. If `p` and `q` are submodules of a module, `p ≀ q` means that `p βŠ† q`. Many of the relevant definitions, including `module`, `submodule`, and `linear_map`, are found in `src/algebra/module.lean`. ## Main definitions * Many constructors for linear maps, including `prod` and `coprod` * `submodule.span s` is defined to be the smallest submodule containing the set `s`. * If `p` is a submodule of `M`, `submodule.quotient p` is the quotient of `M` with respect to `p`: that is, elements of `M` are identified if their difference is in `p`. This is itself a module. * The kernel `ker` and range `range` of a linear map are submodules of the domain and codomain respectively. * `linear_equiv M Mβ‚‚`, the type of linear equivalences between `M` and `Mβ‚‚`, is a structure that extends `linear_map` and `equiv`. * The general linear group is defined to be the group of invertible linear maps from `M` to itself. ## Main statements * The first and second isomorphism laws for modules are proved as `quot_ker_equiv_range` and `quotient_inf_equiv_sup_quotient`. ## Notations * We continue to use the notation `M β†’β‚—[R] Mβ‚‚` for the type of linear maps from `M` to `Mβ‚‚` over the ring `R`. * We introduce the notations `M ≃ₗ Mβ‚‚` and `M ≃ₗ[R] Mβ‚‚` for `linear_equiv M Mβ‚‚`. In the first, the ring `R` is implicit. ## Implementation notes We note that, when constructing linear maps, it is convenient to use operations defined on bundled maps (`prod`, `coprod`, arithmetic operations like `+`) instead of defining a function and proving it is linear. ## Tags linear algebra, vector space, module -/ open function open_locale big_operators reserve infix ` ≃ₗ `:25 universes u v w x y z u' v' w' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {Mβ‚‚ : Type w} {Vβ‚‚ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {Mβ‚„ : Type z} {ΞΉ : Type x} namespace finsupp lemma smul_sum {Ξ± : Type u} {Ξ² : Type v} {R : Type w} {M : Type y} [has_zero Ξ²] [semiring R] [add_comm_monoid M] [semimodule R M] {v : Ξ± β†’β‚€ Ξ²} {c : R} {h : Ξ± β†’ Ξ² β†’ M} : c β€’ (v.sum h) = v.sum (Ξ»a b, c β€’ h a b) := finset.smul_sum end finsupp section open_locale classical /-- decomposing `x : ΞΉ β†’ R` as a sum along the canonical basis -/ lemma pi_eq_sum_univ {ΞΉ : Type u} [fintype ΞΉ] {R : Type v} [semiring R] (x : ΞΉ β†’ R) : x = βˆ‘ i, x i β€’ (Ξ»j, if i = j then 1 else 0) := by { ext, simp } end /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] [add_comm_monoid Mβ‚„] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] [semimodule R Mβ‚„] variables (f g : M β†’β‚—[R] Mβ‚‚) include R @[simp] theorem comp_id : f.comp id = f := linear_map.ext $ Ξ» x, rfl @[simp] theorem id_comp : id.comp f = f := linear_map.ext $ Ξ» x, rfl theorem comp_assoc (g : Mβ‚‚ β†’β‚—[R] M₃) (h : M₃ β†’β‚—[R] Mβ‚„) : (h.comp g).comp f = h.comp (g.comp f) := rfl /-- The restriction of a linear map `f : M β†’ Mβ‚‚` to a submodule `p βŠ† M` gives a linear map `p β†’ Mβ‚‚`. -/ def dom_restrict (f : M β†’β‚—[R] Mβ‚‚) (p : submodule R M) : p β†’β‚—[R] Mβ‚‚ := f.comp p.subtype @[simp] lemma dom_restrict_apply (f : M β†’β‚—[R] Mβ‚‚) (p : submodule R M) (x : p) : f.dom_restrict p x = f x := rfl /-- A linear map `f : Mβ‚‚ β†’ M` whose values lie in a submodule `p βŠ† M` can be restricted to a linear map Mβ‚‚ β†’ p. -/ def cod_restrict (p : submodule R M) (f : Mβ‚‚ β†’β‚—[R] M) (h : βˆ€c, f c ∈ p) : Mβ‚‚ β†’β‚—[R] p := by refine {to_fun := Ξ»c, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule R M) (f : Mβ‚‚ β†’β‚—[R] M) {h} (x : Mβ‚‚) : (cod_restrict p f h x : M) = f x := rfl @[simp] lemma comp_cod_restrict (p : submodule R Mβ‚‚) (h : βˆ€b, f b ∈ p) (g : M₃ β†’β‚—[R] M) : (cod_restrict p f h).comp g = cod_restrict p (f.comp g) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (p : submodule R Mβ‚‚) (h : βˆ€b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl /-- If a function `g` is a left and right inverse of a linear map `f`, then `g` is linear itself. -/ def inverse (g : Mβ‚‚ β†’ M) (h₁ : left_inverse g f) (hβ‚‚ : right_inverse g f) : Mβ‚‚ β†’β‚—[R] M := by dsimp [left_inverse, function.right_inverse] at h₁ hβ‚‚; exact ⟨g, Ξ» x y, by rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [hβ‚‚], Ξ» a b, by rw [← h₁ (g (a β€’ b)), ← h₁ (a β€’ g b)]; simp [hβ‚‚]⟩ /-- The constant 0 map is linear. -/ instance : has_zero (M β†’β‚—[R] Mβ‚‚) := ⟨⟨λ _, 0, by simp, by simp⟩⟩ instance : inhabited (M β†’β‚—[R] Mβ‚‚) := ⟨0⟩ @[simp] lemma zero_apply (x : M) : (0 : M β†’β‚—[R] Mβ‚‚) x = 0 := rfl /-- The sum of two linear maps is linear. -/ instance : has_add (M β†’β‚—[R] Mβ‚‚) := ⟨λ f g, ⟨λ b, f b + g b, by simp [add_comm, add_left_comm], by simp [smul_add]⟩⟩ @[simp] lemma add_apply (x : M) : (f + g) x = f x + g x := rfl /-- The type of linear maps is an additive monoid. -/ instance : add_comm_monoid (M β†’β‚—[R] Mβ‚‚) := by refine {zero := 0, add := (+), ..}; intros; ext; simp [add_comm, add_left_comm] instance linear_map_apply_is_add_monoid_hom (a : M) : is_add_monoid_hom (Ξ» f : M β†’β‚—[R] Mβ‚‚, f a) := { map_add := Ξ» f g, linear_map.add_apply f g a, map_zero := rfl } lemma sum_apply (t : finset ΞΉ) (f : ΞΉ β†’ M β†’β‚—[R] Mβ‚‚) (b : M) : (βˆ‘ d in t, f d) b = βˆ‘ d in t, f d b := (t.sum_hom (Ξ» g : M β†’β‚—[R] Mβ‚‚, g b)).symm /-- `Ξ»b, f b β€’ x` is a linear map. -/ def smul_right (f : Mβ‚‚ β†’β‚—[R] R) (x : M) : Mβ‚‚ β†’β‚—[R] M := ⟨λb, f b β€’ x, by simp [add_smul], by simp [smul_smul]⟩. @[simp] theorem smul_right_apply (f : Mβ‚‚ β†’β‚—[R] R) (x : M) (c : Mβ‚‚) : (smul_right f x : Mβ‚‚ β†’ M) c = f c β€’ x := rfl instance : has_one (M β†’β‚—[R] M) := ⟨linear_map.id⟩ instance : has_mul (M β†’β‚—[R] M) := ⟨linear_map.comp⟩ @[simp] lemma one_app (x : M) : (1 : M β†’β‚—[R] M) x = x := rfl @[simp] lemma mul_app (A B : M β†’β‚—[R] M) (x : M) : (A * B) x = A (B x) := rfl @[simp] theorem comp_zero : f.comp (0 : M₃ β†’β‚—[R] M) = 0 := ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, f.map_zero] @[simp] theorem zero_comp : (0 : Mβ‚‚ β†’β‚—[R] M₃).comp f = 0 := rfl @[norm_cast] lemma coe_fn_sum {ΞΉ : Type*} (t : finset ΞΉ) (f : ΞΉ β†’ M β†’β‚—[R] Mβ‚‚) : ⇑(βˆ‘ i in t, f i) = βˆ‘ i in t, (f i : M β†’ Mβ‚‚) := add_monoid_hom.map_sum ⟨@to_fun R M Mβ‚‚ _ _ _ _ _, rfl, Ξ» x y, rfl⟩ _ _ instance : monoid (M β†’β‚—[R] M) := by refine {mul := (*), one := 1, ..}; { intros, apply linear_map.ext, simp {proj := ff} } section open_locale classical /-- A linear map `f` applied to `x : ΞΉ β†’ R` can be computed using the image under `f` of elements of the canonical basis. -/ lemma pi_apply_eq_sum_univ [fintype ΞΉ] (f : (ΞΉ β†’ R) β†’β‚—[R] M) (x : ΞΉ β†’ R) : f x = βˆ‘ i, x i β€’ (f (Ξ»j, if i = j then 1 else 0)) := begin conv_lhs { rw [pi_eq_sum_univ x, f.map_sum] }, apply finset.sum_congr rfl (Ξ»l hl, _), rw f.map_smul end end section variables (R M Mβ‚‚) /-- The first projection of a product is a linear map. -/ def fst : M Γ— Mβ‚‚ β†’β‚—[R] M := ⟨prod.fst, Ξ» x y, rfl, Ξ» x y, rfl⟩ /-- The second projection of a product is a linear map. -/ def snd : M Γ— Mβ‚‚ β†’β‚—[R] Mβ‚‚ := ⟨prod.snd, Ξ» x y, rfl, Ξ» x y, rfl⟩ end @[simp] theorem fst_apply (x : M Γ— Mβ‚‚) : fst R M Mβ‚‚ x = x.1 := rfl @[simp] theorem snd_apply (x : M Γ— Mβ‚‚) : snd R M Mβ‚‚ x = x.2 := rfl /-- The prod of two linear maps is a linear map. -/ def prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : M β†’β‚—[R] Mβ‚‚ Γ— M₃ := { to_fun := Ξ» x, (f x, g x), map_add' := Ξ» x y, by simp only [prod.mk_add_mk, map_add], map_smul' := Ξ» c x, by simp only [prod.smul_mk, map_smul] } @[simp] theorem prod_apply (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) (x : M) : prod f g x = (f x, g x) := rfl @[simp] theorem fst_prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : (fst R Mβ‚‚ M₃).comp (prod f g) = f := by ext; refl @[simp] theorem snd_prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : (snd R Mβ‚‚ M₃).comp (prod f g) = g := by ext; refl @[simp] theorem pair_fst_snd : prod (fst R M Mβ‚‚) (snd R M Mβ‚‚) = linear_map.id := by ext; refl section variables (R M Mβ‚‚) /-- The left injection into a product is a linear map. -/ def inl : M β†’β‚—[R] M Γ— Mβ‚‚ := by refine ⟨add_monoid_hom.inl _ _, _, _⟩; intros; simp /-- The right injection into a product is a linear map. -/ def inr : Mβ‚‚ β†’β‚—[R] M Γ— Mβ‚‚ := by refine ⟨add_monoid_hom.inr _ _, _, _⟩; intros; simp end @[simp] theorem inl_apply (x : M) : inl R M Mβ‚‚ x = (x, 0) := rfl @[simp] theorem inr_apply (x : Mβ‚‚) : inr R M Mβ‚‚ x = (0, x) := rfl theorem inl_injective : function.injective (inl R M Mβ‚‚) := Ξ» _, by simp theorem inr_injective : function.injective (inr R M Mβ‚‚) := Ξ» _, by simp /-- The coprod function `Ξ» x : M Γ— Mβ‚‚, f x.1 + g x.2` is a linear map. -/ def coprod (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) : M Γ— Mβ‚‚ β†’β‚—[R] M₃ := { to_fun := Ξ» x, f x.1 + g x.2, map_add' := Ξ» x y, by simp only [map_add, prod.snd_add, prod.fst_add]; cc, map_smul' := Ξ» x y, by simp only [smul_add, prod.smul_snd, prod.smul_fst, map_smul] } @[simp] theorem coprod_apply (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) (x : M) (y : Mβ‚‚) : coprod f g (x, y) = f x + g y := rfl @[simp] theorem coprod_inl (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) : (coprod f g).comp (inl R M Mβ‚‚) = f := by ext; simp only [map_zero, add_zero, coprod_apply, inl_apply, comp_apply] @[simp] theorem coprod_inr (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) : (coprod f g).comp (inr R M Mβ‚‚) = g := by ext; simp only [map_zero, coprod_apply, inr_apply, zero_add, comp_apply] @[simp] theorem coprod_inl_inr : coprod (inl R M Mβ‚‚) (inr R M Mβ‚‚) = linear_map.id := by ext ⟨x, y⟩; simp only [prod.mk_add_mk, add_zero, id_apply, coprod_apply, inl_apply, inr_apply, zero_add] theorem fst_eq_coprod : fst R M Mβ‚‚ = coprod linear_map.id 0 := by ext ⟨x, y⟩; simp theorem snd_eq_coprod : snd R M Mβ‚‚ = coprod 0 linear_map.id := by ext ⟨x, y⟩; simp theorem inl_eq_prod : inl R M Mβ‚‚ = prod linear_map.id 0 := rfl theorem inr_eq_prod : inr R M Mβ‚‚ = prod 0 linear_map.id := rfl /-- `prod.map` of two linear maps. -/ def prod_map (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] Mβ‚„) : (M Γ— Mβ‚‚) β†’β‚—[R] (M₃ Γ— Mβ‚„) := (f.comp (fst R M Mβ‚‚)).prod (g.comp (snd R M Mβ‚‚)) @[simp] theorem prod_map_apply (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] Mβ‚„) (x) : f.prod_map g x = (f x.1, g x.2) := rfl end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] [add_comm_group Mβ‚„] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] [semimodule R Mβ‚„] variables (f g : M β†’β‚—[R] Mβ‚‚) include R /-- The negation of a linear map is linear. -/ instance : has_neg (M β†’β‚—[R] Mβ‚‚) := ⟨λ f, ⟨λ b, - f b, by simp [add_comm], by simp⟩⟩ @[simp] lemma neg_apply (x : M) : (- f) x = - f x := rfl /-- The type of linear maps is an additive group. -/ instance : add_comm_group (M β†’β‚—[R] Mβ‚‚) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp [add_comm, add_left_comm] instance linear_map_apply_is_add_group_hom (a : M) : is_add_group_hom (Ξ» f : M β†’β‚—[R] Mβ‚‚, f a) := { map_add := Ξ» f g, linear_map.add_apply f g a } @[simp] lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl end add_comm_group section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] variables (f g : M β†’β‚—[R] Mβ‚‚) include R instance : has_scalar R (M β†’β‚—[R] Mβ‚‚) := ⟨λ a f, ⟨λ b, a β€’ f b, by simp [smul_add], by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (a : R) (x : M) : (a β€’ f) x = a β€’ f x := rfl instance : semimodule R (M β†’β‚—[R] Mβ‚‚) := by refine { smul := (β€’), .. }; intros; ext; simp [smul_add, add_smul, smul_smul] /-- Composition by `f : Mβ‚‚ β†’ M₃` is a linear map from the space of linear maps `M β†’ Mβ‚‚` to the space of linear maps `Mβ‚‚ β†’ M₃`. -/ def comp_right (f : Mβ‚‚ β†’β‚—[R] M₃) : (M β†’β‚—[R] Mβ‚‚) β†’β‚—[R] (M β†’β‚—[R] M₃) := ⟨linear_map.comp f, Ξ» _ _, linear_map.ext $ Ξ» _, f.2 _ _, Ξ» _ _, linear_map.ext $ Ξ» _, f.3 _ _⟩ theorem smul_comp (g : Mβ‚‚ β†’β‚—[R] M₃) (a : R) : (a β€’ g).comp f = a β€’ (g.comp f) := rfl theorem comp_smul (g : Mβ‚‚ β†’β‚—[R] M₃) (a : R) : g.comp (a β€’ f) = a β€’ (g.comp f) := ext $ assume b, by rw [comp_apply, smul_apply, g.map_smul]; refl end comm_semiring section ring instance endomorphism_ring [ring R] [add_comm_group M] [semimodule R M] : ring (M β†’β‚—[R] M) := by refine {mul := (*), one := 1, ..linear_map.add_comm_group, ..}; { intros, apply linear_map.ext, simp {proj := ff} } end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] /-- The family of linear maps `Mβ‚‚ β†’ M` parameterised by `f ∈ Mβ‚‚ β†’ R`, `x ∈ M`, is linear in `f`, `x`. -/ def smul_rightβ‚— : (Mβ‚‚ β†’β‚—[R] R) β†’β‚—[R] M β†’β‚—[R] Mβ‚‚ β†’β‚—[R] M := { to_fun := Ξ» f, { to_fun := linear_map.smul_right f, map_add' := Ξ» m m', by { ext, apply smul_add, }, map_smul' := Ξ» c m, by { ext, apply smul_comm, } }, map_add' := Ξ» f f', by { ext, apply add_smul, }, map_smul' := Ξ» c f, by { ext, apply mul_smul, } } @[simp] lemma smul_rightβ‚—_apply (f : Mβ‚‚ β†’β‚—[R] R) (x : M) (c : Mβ‚‚) : (smul_rightβ‚— : (Mβ‚‚ β†’β‚— R) β†’β‚— M β†’β‚— Mβ‚‚ β†’β‚— M) f x c = (f c) β€’ x := rfl end comm_ring end linear_map /-! ### Properties of submodules -/ namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] variables (p p' : submodule R M) (q q' : submodule R Mβ‚‚) variables {r : R} {x y : M} open set instance : partial_order (submodule R M) := { le := Ξ» p p', βˆ€ ⦃x⦄, x ∈ p β†’ x ∈ p', ..partial_order.lift (coe : submodule R M β†’ set M) coe_injective } variables {p p'} lemma le_def : p ≀ p' ↔ (p : set M) βŠ† p' := iff.rfl lemma le_def' : p ≀ p' ↔ βˆ€ x ∈ p, x ∈ p' := iff.rfl lemma lt_def : p < p' ↔ (p : set M) βŠ‚ p' := iff.rfl lemma not_le_iff_exists : Β¬ (p ≀ p') ↔ βˆƒ x ∈ p, x βˆ‰ p' := not_subset lemma exists_of_lt {p p' : submodule R M} : p < p' β†’ βˆƒ x ∈ p', x βˆ‰ p := exists_of_ssubset lemma lt_iff_le_and_exists : p < p' ↔ p ≀ p' ∧ βˆƒ x ∈ p', x βˆ‰ p := by rw [lt_iff_le_not_le, not_le_iff_exists] /-- If two submodules `p` and `p'` satisfy `p βŠ† p'`, then `of_le p p'` is the linear map version of this inclusion. -/ def of_le (h : p ≀ p') : p β†’β‚—[R] p' := p.subtype.cod_restrict p' $ Ξ» ⟨x, hx⟩, h hx @[simp] theorem coe_of_le (h : p ≀ p') (x : p) : (of_le h x : M) = x := rfl theorem of_le_apply (h : p ≀ p') (x : p) : of_le h x = ⟨x, h x.2⟩ := rfl variables (p p') lemma subtype_comp_of_le (p q : submodule R M) (h : p ≀ q) : q.subtype.comp (of_le h) = p.subtype := by { ext ⟨b, hb⟩, refl } /-- The set `{0}` is the bottom element of the lattice of submodules. -/ instance : has_bot (submodule R M) := ⟨{ carrier := {0}, smul_mem' := by simp { contextual := tt }, .. (βŠ₯ : add_submonoid M)}⟩ instance inhabited' : inhabited (submodule R M) := ⟨βŠ₯⟩ @[simp] lemma bot_coe : ((βŠ₯ : submodule R M) : set M) = {0} := rfl section variables (R) @[simp] lemma mem_bot : x ∈ (βŠ₯ : submodule R M) ↔ x = 0 := mem_singleton_iff end lemma nonzero_mem_of_bot_lt {I : submodule R M} (bot_lt : βŠ₯ < I) : βˆƒ a : I, a β‰  0 := begin have h := (submodule.lt_iff_le_and_exists.1 bot_lt).2, tidy, end instance : order_bot (submodule R M) := { bot := βŠ₯, bot_le := Ξ» p x, by simp {contextual := tt}, ..submodule.partial_order } protected lemma eq_bot_iff (p : submodule R M) : p = βŠ₯ ↔ βˆ€ x ∈ p, x = (0 : M) := ⟨ Ξ» h, h.symm β–Έ Ξ» x hx, (mem_bot R).mp hx, Ξ» h, eq_bot_iff.mpr (Ξ» x hx, (mem_bot R).mpr (h x hx)) ⟩ protected lemma ne_bot_iff (p : submodule R M) : p β‰  βŠ₯ ↔ βˆƒ x ∈ p, x β‰  (0 : M) := by { haveI := classical.prop_decidable, simp_rw [ne.def, p.eq_bot_iff, not_forall] } /-- The universal set is the top element of the lattice of submodules. -/ instance : has_top (submodule R M) := ⟨{ carrier := univ, smul_mem' := Ξ» _ _ _, trivial, .. (⊀ : add_submonoid M)}⟩ @[simp] lemma top_coe : ((⊀ : submodule R M) : set M) = univ := rfl @[simp] lemma mem_top : x ∈ (⊀ : submodule R M) := trivial lemma eq_bot_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : p = βŠ₯ := by ext x; simp [semimodule.eq_zero_of_zero_eq_one x zero_eq_one] instance : order_top (submodule R M) := { top := ⊀, le_top := Ξ» p x _, trivial, ..submodule.partial_order } instance : has_Inf (submodule R M) := ⟨λ S, { carrier := β‹‚ s ∈ S, (s : set M), zero_mem' := by simp, add_mem' := by simp [add_mem] {contextual := tt}, smul_mem' := by simp [smul_mem] {contextual := tt} }⟩ private lemma Inf_le' {S : set (submodule R M)} {p} : p ∈ S β†’ Inf S ≀ p := bInter_subset_of_mem private lemma le_Inf' {S : set (submodule R M)} {p} : (βˆ€p' ∈ S, p ≀ p') β†’ p ≀ Inf S := subset_bInter instance : has_inf (submodule R M) := ⟨λ p p', { carrier := p ∩ p', zero_mem' := by simp, add_mem' := by simp [add_mem] {contextual := tt}, smul_mem' := by simp [smul_mem] {contextual := tt} }⟩ instance : complete_lattice (submodule R M) := { sup := Ξ» a b, Inf {x | a ≀ x ∧ b ≀ x}, le_sup_left := Ξ» a b, le_Inf' $ Ξ» x ⟨ha, hb⟩, ha, le_sup_right := Ξ» a b, le_Inf' $ Ξ» x ⟨ha, hb⟩, hb, sup_le := Ξ» a b c h₁ hβ‚‚, Inf_le' ⟨h₁, hβ‚‚βŸ©, inf := (βŠ“), le_inf := Ξ» a b c, subset_inter, inf_le_left := Ξ» a b, inter_subset_left _ _, inf_le_right := Ξ» a b, inter_subset_right _ _, Sup := Ξ»tt, Inf {t | βˆ€t'∈tt, t' ≀ t}, le_Sup := Ξ» s p hs, le_Inf' $ Ξ» p' hp', hp' _ hs, Sup_le := Ξ» s p hs, Inf_le' hs, Inf := Inf, le_Inf := Ξ» s a, le_Inf', Inf_le := Ξ» s a, Inf_le', ..submodule.order_top, ..submodule.order_bot } instance add_comm_monoid_submodule : add_comm_monoid (submodule R M) := { add := (βŠ”), add_assoc := Ξ» _ _ _, sup_assoc, zero := βŠ₯, zero_add := Ξ» _, bot_sup_eq, add_zero := Ξ» _, sup_bot_eq, add_comm := Ξ» _ _, sup_comm } @[simp] lemma add_eq_sup (p q : submodule R M) : p + q = p βŠ” q := rfl @[simp] lemma zero_eq_bot : (0 : submodule R M) = βŠ₯ := rfl lemma eq_top_iff' {p : submodule R M} : p = ⊀ ↔ βˆ€ x, x ∈ p := eq_top_iff.trans ⟨λ h x, @h x trivial, Ξ» h x _, h x⟩ lemma bot_ne_top [nontrivial M] : (βŠ₯ : submodule R M) β‰  ⊀ := Ξ» h, let ⟨a, ha⟩ := exists_ne (0 : M) in ha $ (mem_bot R).1 $ (eq_top_iff.1 h) trivial @[simp] theorem inf_coe : (p βŠ“ p' : set M) = p ∩ p' := rfl @[simp] theorem mem_inf {p p' : submodule R M} : x ∈ p βŠ“ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[simp] theorem Inf_coe (P : set (submodule R M)) : (↑(Inf P) : set M) = β‹‚ p ∈ P, ↑p := rfl @[simp] theorem infi_coe {ΞΉ} (p : ΞΉ β†’ submodule R M) : (↑⨅ i, p i : set M) = β‹‚ i, ↑(p i) := by rw [infi, Inf_coe]; ext a; simp; exact ⟨λ h i, h _ i rfl, Ξ» h i x e, e β–Έ h _⟩ @[simp] theorem mem_infi {ΞΉ} (p : ΞΉ β†’ submodule R M) : x ∈ (β¨… i, p i) ↔ βˆ€ i, x ∈ p i := by rw [← mem_coe, infi_coe, mem_Inter]; refl theorem disjoint_def {p p' : submodule R M} : disjoint p p' ↔ βˆ€ x ∈ p, x ∈ p' β†’ x = (0:M) := show (βˆ€ x, x ∈ p ∧ x ∈ p' β†’ x ∈ ({0} : set M)) ↔ _, by simp theorem mem_right_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p} : (x:M) ∈ p' ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x x.2 hx, Ξ» h, h.symm β–Έ p'.zero_mem⟩ theorem mem_left_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p'} : (x:M) ∈ p ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x hx x.2, Ξ» h, h.symm β–Έ p.zero_mem⟩ /-- The pushforward of a submodule `p βŠ† M` by `f : M β†’ Mβ‚‚` -/ def map (f : M β†’β‚—[R] Mβ‚‚) (p : submodule R M) : submodule R Mβ‚‚ := { carrier := f '' p, smul_mem' := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩, .. p.to_add_submonoid.map f.to_add_monoid_hom } @[simp] lemma map_coe (f : M β†’β‚—[R] Mβ‚‚) (p : submodule R M) : (map f p : set Mβ‚‚) = f '' p := rfl @[simp] lemma mem_map {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} {x : Mβ‚‚} : x ∈ map f p ↔ βˆƒ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h lemma map_id : map linear_map.id p = p := submodule.ext $ Ξ» a, by simp lemma map_comp (f : M β†’β‚—[R] Mβ‚‚) (g : Mβ‚‚ β†’β‚—[R] M₃) (p : submodule R M) : map (g.comp f) p = map g (map f p) := submodule.coe_injective $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : M β†’β‚—[R] Mβ‚‚} {p p' : submodule R M} : p ≀ p' β†’ map f p ≀ map f p' := image_subset _ @[simp] lemma map_zero : map (0 : M β†’β‚—[R] Mβ‚‚) p = βŠ₯ := have βˆƒ (x : M), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] /-- The pullback of a submodule `p βŠ† Mβ‚‚` along `f : M β†’ Mβ‚‚` -/ def comap (f : M β†’β‚—[R] Mβ‚‚) (p : submodule R Mβ‚‚) : submodule R M := { carrier := f ⁻¹' p, smul_mem' := Ξ» a x h, by simp [p.smul_mem _ h], .. p.to_add_submonoid.comap f.to_add_monoid_hom } @[simp] lemma comap_coe (f : M β†’β‚—[R] Mβ‚‚) (p : submodule R Mβ‚‚) : (comap f p : set M) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R Mβ‚‚} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := submodule.coe_injective rfl lemma comap_comp (f : M β†’β‚—[R] Mβ‚‚) (g : Mβ‚‚ β†’β‚—[R] M₃) (p : submodule R M₃) : comap (g.comp f) p = comap f (comap g p) := rfl lemma comap_mono {f : M β†’β‚—[R] Mβ‚‚} {q q' : submodule R Mβ‚‚} : q ≀ q' β†’ comap f q ≀ comap f q' := preimage_mono lemma map_le_iff_le_comap {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} {q : submodule R Mβ‚‚} : map f p ≀ q ↔ p ≀ comap f q := image_subset_iff lemma gc_map_comap (f : M β†’β‚—[R] Mβ‚‚) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : M β†’β‚—[R] Mβ‚‚) : map f βŠ₯ = βŠ₯ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : M β†’β‚—[R] Mβ‚‚) : map f (p βŠ” p') = map f p βŠ” map f p' := (gc_map_comap f).l_sup @[simp] lemma map_supr {ΞΉ : Sort*} (f : M β†’β‚—[R] Mβ‚‚) (p : ΞΉ β†’ submodule R M) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f).l_supr @[simp] lemma comap_top (f : M β†’β‚—[R] Mβ‚‚) : comap f ⊀ = ⊀ := rfl @[simp] lemma comap_inf (f : M β†’β‚—[R] Mβ‚‚) : comap f (q βŠ“ q') = comap f q βŠ“ comap f q' := rfl @[simp] lemma comap_infi {ΞΉ : Sort*} (f : M β†’β‚—[R] Mβ‚‚) (p : ΞΉ β†’ submodule R Mβ‚‚) : comap f (β¨…i, p i) = (β¨…i, comap f (p i)) := (gc_map_comap f).u_infi @[simp] lemma comap_zero : comap (0 : M β†’β‚—[R] Mβ‚‚) q = ⊀ := ext $ by simp lemma map_comap_le (f : M β†’β‚—[R] Mβ‚‚) (q : submodule R Mβ‚‚) : map f (comap f q) ≀ q := (gc_map_comap f).l_u_le _ lemma le_comap_map (f : M β†’β‚—[R] Mβ‚‚) (p : submodule R M) : p ≀ comap f (map f p) := (gc_map_comap f).le_u_l _ --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} {p' : submodule R Mβ‚‚} : map f p βŠ“ p' = map f (p βŠ“ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, hβ‚‚βŸ©; exact ⟨_, ⟨h₁, hβ‚‚βŸ©, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p βŠ“ p' := ext $ Ξ» x, ⟨by rintro ⟨⟨_, hβ‚βŸ©, hβ‚‚, rfl⟩; exact ⟨h₁, hβ‚‚βŸ©, Ξ» ⟨h₁, hβ‚‚βŸ©, ⟨⟨_, hβ‚βŸ©, hβ‚‚, rfl⟩⟩ lemma eq_zero_of_bot_submodule : βˆ€(b : (βŠ₯ : submodule R M)), b = 0 | ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot R).1 hb section variables (R) /-- The span of a set `s βŠ† M` is the smallest submodule of M that contains `s`. -/ def span (s : set M) : submodule R M := Inf {p | s βŠ† p} end variables {s t : set M} lemma mem_span : x ∈ span R s ↔ βˆ€ p : submodule R M, s βŠ† p β†’ x ∈ p := mem_bInter_iff lemma subset_span : s βŠ† span R s := Ξ» x h, mem_span.2 $ Ξ» p hp, hp h lemma span_le {p} : span R s ≀ p ↔ s βŠ† p := ⟨subset.trans subset_span, Ξ» ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s βŠ† t) : span R s ≀ span R t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s βŠ† p) (hβ‚‚ : p ≀ span R s) : span R s = p := le_antisymm (span_le.2 h₁) hβ‚‚ @[simp] lemma span_eq : span R (p : set M) = p := span_eq_of_le _ (subset.refl _) subset_span /-- An induction principle for span membership. If `p` holds for 0 and all elements of `s`, and is preserved under addition and scalar multiplication, then `p` holds for all elements of the span of `s`. -/ @[elab_as_eliminator] lemma span_induction {p : M β†’ Prop} (h : x ∈ span R s) (Hs : βˆ€ x ∈ s, p x) (H0 : p 0) (H1 : βˆ€ x y, p x β†’ p y β†’ p (x + y)) (H2 : βˆ€ (a:R) x, p x β†’ p (a β€’ x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h section variables (R M) /-- `span` forms a Galois insertion with the coercion from submodule to set. -/ protected def gi : galois_insertion (@span R M _ _ _) coe := { choice := Ξ» s _, span R s, gc := Ξ» s t, span_le, le_l_u := Ξ» s, subset_span, choice_eq := Ξ» s h, rfl } end @[simp] lemma span_empty : span R (βˆ… : set M) = βŠ₯ := (submodule.gi R M).gc.l_bot @[simp] lemma span_univ : span R (univ : set M) = ⊀ := eq_top_iff.2 $ le_def.2 $ subset_span lemma span_union (s t : set M) : span R (s βˆͺ t) = span R s βŠ” span R t := (submodule.gi R M).gc.l_sup lemma span_Union {ΞΉ} (s : ΞΉ β†’ set M) : span R (⋃ i, s i) = ⨆ i, span R (s i) := (submodule.gi R M).gc.l_supr @[simp] theorem coe_supr_of_directed {ΞΉ} [hΞΉ : nonempty ΞΉ] (S : ΞΉ β†’ submodule R M) (H : directed (≀) S) : ((supr S : submodule R M) : set M) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), suffices : (span R (⋃ i, (S i : set M)) : set M) βŠ† ⋃ (i : ΞΉ), ↑(S i), by simpa only [span_Union, span_eq] using this, refine (Ξ» x hx, span_induction hx (Ξ» _, id) _ _ _); simp only [mem_Union, exists_imp_distrib], { exact hΞΉ.elim (Ξ» i, ⟨i, (S i).zero_mem⟩) }, { intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { exact Ξ» a x i hi, ⟨i, smul_mem _ a hi⟩ }, end lemma mem_sup_left {S T : submodule R M} : βˆ€ {x : M}, x ∈ S β†’ x ∈ S βŠ” T := show S ≀ S βŠ” T, from le_sup_left lemma mem_sup_right {S T : submodule R M} : βˆ€ {x : M}, x ∈ T β†’ x ∈ S βŠ” T := show T ≀ S βŠ” T, from le_sup_right lemma mem_supr_of_mem {ΞΉ : Sort*} {b : M} {p : ΞΉ β†’ submodule R M} (i : ΞΉ) (h : b ∈ p i) : b ∈ (⨆i, p i) := have p i ≀ (⨆i, p i) := le_supr p i, @this b h lemma mem_Sup_of_mem {S : set (submodule R M)} {s : submodule R M} (hs : s ∈ S) : βˆ€ {x : M}, x ∈ s β†’ x ∈ Sup S := show s ≀ Sup S, from le_Sup hs @[simp] theorem mem_supr_of_directed {ΞΉ} [nonempty ΞΉ] (S : ΞΉ β†’ submodule R M) (H : directed (≀) S) {x} : x ∈ supr S ↔ βˆƒ i, x ∈ S i := by { rw [← mem_coe, coe_supr_of_directed S H, mem_Union], refl } theorem mem_Sup_of_directed {s : set (submodule R M)} {z} (hs : s.nonempty) (hdir : directed_on (≀) s) : z ∈ Sup s ↔ βˆƒ y ∈ s, z ∈ y := begin haveI : nonempty s := hs.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed _ hdir.directed_coe, set_coe.exists, subtype.coe_mk] end section variables {p p'} lemma mem_sup : x ∈ p βŠ” p' ↔ βˆƒ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨yβ‚‚, hyβ‚‚, zβ‚‚, hzβ‚‚, rfl⟩, exact ⟨_, add_mem _ hy₁ hyβ‚‚, _, add_mem _ hz₁ hzβ‚‚, by simp [add_assoc]; cc⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _ ((le_sup_left : p ≀ p βŠ” p') hy) ((le_sup_right : p' ≀ p βŠ” p') hz)⟩ lemma mem_sup' : x ∈ p βŠ” p' ↔ βˆƒ (y : p) (z : p'), (y:M) + z = x := mem_sup.trans $ by simp only [submodule.exists, coe_mk] end lemma mem_span_singleton_self (x : M) : x ∈ span R ({x} : set M) := subset_span rfl lemma mem_span_singleton {y : M} : x ∈ span R ({y} : set M) ↔ βˆƒ a:R, a β€’ y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma span_singleton_eq_range (y : M) : (span R ({y} : set M) : set M) = range ((β€’ y) : R β†’ M) := set.ext $ Ξ» x, mem_span_singleton lemma disjoint_span_singleton {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {s : submodule K E} {x : E} : disjoint s (span K {x}) ↔ (x ∈ s β†’ x = 0) := begin refine disjoint_def.trans ⟨λ H hx, H x hx $ subset_span $ mem_singleton x, _⟩, assume H y hy hyx, obtain ⟨c, hc⟩ := mem_span_singleton.1 hyx, subst y, classical, by_cases hc : c = 0, by simp only [hc, zero_smul], rw [s.smul_mem_iff hc] at hy, rw [H hy, smul_zero] end lemma mem_span_insert {y} : x ∈ span R (insert y s) ↔ βˆƒ (a:R) (z ∈ span R s), x = a β€’ y + z := begin simp only [← union_singleton, span_union, mem_sup, mem_span_singleton, exists_prop, exists_exists_eq_and], rw [exists_comm], simp only [eq_comm, add_comm, exists_and_distrib_left] end lemma span_insert_eq_span (h : x ∈ span R s) : span R (insert x s) = span R s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span R (span R s : set M) = span R s := span_eq _ lemma span_eq_bot : span R (s : set M) = βŠ₯ ↔ βˆ€ x ∈ s, (x:M) = 0 := eq_bot_iff.trans ⟨ Ξ» H x h, (mem_bot R).1 $ H $ subset_span h, Ξ» H, span_le.2 (Ξ» x h, (mem_bot R).2 $ H x h)⟩ @[simp] lemma span_singleton_eq_bot : span R ({x} : set M) = βŠ₯ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_zero : span R (0 : set M) = βŠ₯ := by rw [←singleton_zero, span_singleton_eq_bot] @[simp] lemma span_image (f : M β†’β‚—[R] Mβ‚‚) : span R (f '' s) = map f (span R s) := span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ image_subset_iff.1 subset_span lemma linear_eq_on (s : set M) {f g : M β†’β‚—[R] Mβ‚‚} (H : βˆ€x∈s, f x = g x) {x} (h : x ∈ span R s) : f x = g x := by apply span_induction h H; simp {contextual := tt} lemma supr_eq_span {ΞΉ : Sort w} (p : ΞΉ β†’ submodule R M) : (⨆ (i : ΞΉ), p i) = submodule.span R (⋃ (i : ΞΉ), ↑(p i)) := le_antisymm (supr_le $ assume i, subset.trans (assume m hm, set.mem_Union.mpr ⟨i, hm⟩) subset_span) (span_le.mpr $ Union_subset_iff.mpr $ assume i m hm, mem_supr_of_mem i hm) lemma span_singleton_le_iff_mem (m : M) (p : submodule R M) : span R {m} ≀ p ↔ m ∈ p := by rw [span_le, singleton_subset_iff, mem_coe] lemma lt_add_iff_not_mem {I : submodule R M} {a : M} : I < I + span R {a} ↔ a βˆ‰ I := begin split, { intro h, by_contra akey, have h1 : I + span R {a} ≀ I, { simp only [add_eq_sup, sup_le_iff], split, { exact le_refl I, }, { exact (span_singleton_le_iff_mem a I).mpr akey, } }, have h2 := gt_of_ge_of_gt h1 h, exact lt_irrefl I h2, }, { intro h, apply lt_iff_le_and_exists.mpr, split, simp only [add_eq_sup, le_sup_left], use a, split, swap, { assumption, }, { have : span R {a} ≀ I + span R{a} := le_sup_right, exact this (mem_span_singleton_self a), } }, end lemma mem_supr {ΞΉ : Sort w} (p : ΞΉ β†’ submodule R M) {m : M} : (m ∈ ⨆ i, p i) ↔ (βˆ€ N, (βˆ€ i, p i ≀ N) β†’ m ∈ N) := begin rw [← span_singleton_le_iff_mem, le_supr_iff], simp only [span_singleton_le_iff_mem], end /-- The product of two submodules is a submodule. -/ def prod : submodule R (M Γ— Mβ‚‚) := { carrier := set.prod p q, smul_mem' := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩, .. p.to_add_submonoid.prod q.to_add_submonoid } @[simp] lemma prod_coe : (prod p q : set (M Γ— Mβ‚‚)) = set.prod p q := rfl @[simp] lemma mem_prod {p : submodule R M} {q : submodule R Mβ‚‚} {x : M Γ— Mβ‚‚} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set M) (t : set Mβ‚‚) : span R (set.prod s t) ≀ prod (span R s) (span R t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊀ ⊀ : submodule R (M Γ— Mβ‚‚)) = ⊀ := by ext; simp @[simp] lemma prod_bot : (prod βŠ₯ βŠ₯ : submodule R (M Γ— Mβ‚‚)) = βŠ₯ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule R M} {q q' : submodule R Mβ‚‚} : p ≀ p' β†’ q ≀ q' β†’ prod p q ≀ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q βŠ“ prod p' q' = prod (p βŠ“ p') (q βŠ“ q') := coe_injective set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q βŠ” prod p' q' = prod (p βŠ” p') (q βŠ” q') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [le_def'], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end end add_comm_monoid variables [ring R] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] variables (p p' : submodule R M) (q q' : submodule R Mβ‚‚) variables {r : R} {x y : M} open set lemma mem_span_insert' {y} {s : set M} : x ∈ span R (insert y s) ↔ βˆƒ(a:R), x + a β€’ y ∈ span R s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz, add_assoc]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp [add_comm, add_left_comm]⟩ } end -- TODO(Mario): Factor through add_subgroup /-- The equivalence relation associated to a submodule `p`, defined by `x β‰ˆ y` iff `y - x ∈ p`. -/ def quotient_rel : setoid M := ⟨λ x y, x - y ∈ p, Ξ» x, by simp, Ξ» x y h, by simpa using neg_mem _ h, Ξ» x y z h₁ hβ‚‚, by simpa [sub_eq_add_neg, add_left_comm, add_assoc] using add_mem _ h₁ hβ‚‚βŸ© /-- The quotient of a module `M` by a submodule `p βŠ† M`. -/ def quotient : Type* := quotient (quotient_rel p) namespace quotient /-- Map associating to an element of `M` the corresponding element of `M/p`, when `p` is a submodule of `M`. -/ def mk {p : submodule R M} : M β†’ quotient p := quotient.mk' @[simp] theorem mk_eq_mk {p : submodule R M} (x : M) : (quotient.mk x : quotient p) = mk x := rfl @[simp] theorem mk'_eq_mk {p : submodule R M} (x : M) : (quotient.mk' x : quotient p) = mk x := rfl @[simp] theorem quot_mk_eq_mk {p : submodule R M} (x : M) : (quot.mk _ x : quotient p) = mk x := rfl protected theorem eq {x y : M} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq' instance : has_zero (quotient p) := ⟨mk 0⟩ instance : inhabited (quotient p) := ⟨0⟩ @[simp] theorem mk_zero : mk 0 = (0 : quotient p) := rfl @[simp] theorem mk_eq_zero : (mk x : quotient p) = 0 ↔ x ∈ p := by simpa using (quotient.eq p : mk x = 0 ↔ _) instance : has_add (quotient p) := ⟨λ a b, quotient.lift_onβ‚‚' a b (Ξ» a b, mk (a + b)) $ Ξ» a₁ aβ‚‚ b₁ bβ‚‚ h₁ hβ‚‚, (quotient.eq p).2 $ by simpa [sub_eq_add_neg, add_left_comm, add_comm] using add_mem p h₁ hβ‚‚βŸ© @[simp] theorem mk_add : (mk (x + y) : quotient p) = mk x + mk y := rfl instance : has_neg (quotient p) := ⟨λ a, quotient.lift_on' a (Ξ» a, mk (-a)) $ Ξ» a b h, (quotient.eq p).2 $ by simpa using neg_mem p h⟩ @[simp] theorem mk_neg : (mk (-x) : quotient p) = -mk x := rfl instance : add_comm_group (quotient p) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; repeat {rintro ⟨⟩}; simp [-mk_zero, (mk_zero p).symm, -mk_add, (mk_add p).symm, -mk_neg, (mk_neg p).symm]; cc instance : has_scalar R (quotient p) := ⟨λ a x, quotient.lift_on' x (Ξ» x, mk (a β€’ x)) $ Ξ» x y h, (quotient.eq p).2 $ by simpa [smul_sub] using smul_mem p a h⟩ @[simp] theorem mk_smul : (mk (r β€’ x) : quotient p) = r β€’ mk x := rfl instance : semimodule R (quotient p) := semimodule.of_core $ by refine {smul := (β€’), ..}; repeat {rintro ⟨⟩ <|> intro}; simp [smul_add, add_smul, smul_smul, -mk_add, (mk_add p).symm, -mk_smul, (mk_smul p).symm] lemma mk_surjective : function.surjective (@mk _ _ _ _ _ p) := by { rintros ⟨x⟩, exact ⟨x, rfl⟩ } lemma nontrivial_of_lt_top (h : p < ⊀) : nontrivial (p.quotient) := begin obtain ⟨x, _, not_mem_s⟩ := exists_of_lt h, refine ⟨⟨mk x, 0, _⟩⟩, simpa using not_mem_s end end quotient lemma quot_hom_ext ⦃f g : quotient p β†’β‚—[R] M₂⦄ (h : βˆ€ x, f (quotient.mk x) = g (quotient.mk x)) : f = g := linear_map.ext $ Ξ» x, quotient.induction_on' x h end submodule namespace submodule variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group Vβ‚‚] [vector_space K Vβ‚‚] lemma comap_smul (f : V β†’β‚—[K] Vβ‚‚) (p : submodule K Vβ‚‚) (a : K) (h : a β‰  0) : p.comap (a β€’ f) = p.comap f := by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply] lemma map_smul (f : V β†’β‚—[K] Vβ‚‚) (p : submodule K V) (a : K) (h : a β‰  0) : p.map (a β€’ f) = p.map f := le_antisymm begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end lemma comap_smul' (f : V β†’β‚—[K] Vβ‚‚) (p : submodule K Vβ‚‚) (a : K) : p.comap (a β€’ f) = (β¨… h : a β‰  0, p.comap f) := by classical; by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : V β†’β‚—[K] Vβ‚‚) (p : submodule K V) (a : K) : p.map (a β€’ f) = (⨆ h : a β‰  0, p.map f) := by classical; by_cases a = 0; simp [h, map_smul] end submodule /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] include R open submodule @[simp] lemma finsupp_sum {Ξ³} [has_zero Ξ³] (f : M β†’β‚—[R] Mβ‚‚) {t : ΞΉ β†’β‚€ Ξ³} {g : ΞΉ β†’ Ξ³ β†’ M} : f (t.sum g) = t.sum (Ξ»i d, f (g i d)) := f.map_sum theorem map_cod_restrict (p : submodule R M) (f : Mβ‚‚ β†’β‚—[R] M) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ Ξ» ⟨x, hx⟩, by simp [subtype.ext_iff_val] theorem comap_cod_restrict (p : submodule R M) (f : Mβ‚‚ β†’β‚—[R] M) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ Ξ» x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ /-- The range of a linear map `f : M β†’ Mβ‚‚` is a submodule of `Mβ‚‚`. -/ def range (f : M β†’β‚—[R] Mβ‚‚) : submodule R Mβ‚‚ := map f ⊀ theorem range_coe (f : M β†’β‚—[R] Mβ‚‚) : (range f : set Mβ‚‚) = set.range f := set.image_univ @[simp] theorem mem_range {f : M β†’β‚—[R] Mβ‚‚} : βˆ€ {x}, x ∈ range f ↔ βˆƒ y, f y = x := set.ext_iff.1 (range_coe f) theorem mem_range_self (f : M β†’β‚—[R] Mβ‚‚) (x : M) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ @[simp] theorem range_id : range (linear_map.id : M β†’β‚—[R] M) = ⊀ := map_id _ theorem range_comp (f : M β†’β‚—[R] Mβ‚‚) (g : Mβ‚‚ β†’β‚—[R] M₃) : range (g.comp f) = map g (range f) := map_comp _ _ _ theorem range_comp_le_range (f : M β†’β‚—[R] Mβ‚‚) (g : Mβ‚‚ β†’β‚—[R] M₃) : range (g.comp f) ≀ range g := by rw range_comp; exact map_mono le_top theorem range_eq_top {f : M β†’β‚—[R] Mβ‚‚} : range f = ⊀ ↔ surjective f := by rw [submodule.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R Mβ‚‚} : range f ≀ p ↔ comap f p = ⊀ := by rw [range, map_le_iff_le_comap, eq_top_iff] lemma map_le_range {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} : map f p ≀ range f := map_mono le_top lemma range_coprod (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) : (f.coprod g).range = f.range βŠ” g.range := submodule.ext $ Ξ» x, by simp [mem_sup] lemma sup_range_inl_inr : (inl R M Mβ‚‚).range βŠ” (inr R M Mβ‚‚).range = ⊀ := begin refine eq_top_iff'.2 (Ξ» x, mem_sup.2 _), rcases x with ⟨x₁, xβ‚‚βŸ© , have h₁ : prod.mk x₁ (0 : Mβ‚‚) ∈ (inl R M Mβ‚‚).range, by simp, have hβ‚‚ : prod.mk (0 : M) xβ‚‚ ∈ (inr R M Mβ‚‚).range, by simp, use [⟨x₁, 0⟩, h₁, ⟨0, xβ‚‚βŸ©, hβ‚‚], simp end /-- Restrict the codomain of a linear map `f` to `f.range`. -/ @[reducible] def range_restrict (f : M β†’β‚—[R] Mβ‚‚) : M β†’β‚—[R] f.range := f.cod_restrict f.range f.mem_range_self section variables (R) (M) /-- Given an element `x` of a module `M` over `R`, the natural map from `R` to scalar multiples of `x`.-/ def to_span_singleton (x : M) : R β†’β‚—[R] M := linear_map.id.smul_right x /-- The range of `to_span_singleton x` is the span of `x`.-/ lemma span_singleton_eq_range (x : M) : span R {x} = (to_span_singleton R M x).range := submodule.ext $ Ξ» y, by {refine iff.trans _ mem_range.symm, exact mem_span_singleton } lemma to_span_singleton_one (x : M) : to_span_singleton R M x 1 = x := one_smul _ _ end /-- The kernel of a linear map `f : M β†’ Mβ‚‚` is defined to be `comap f βŠ₯`. This is equivalent to the set of `x : M` such that `f x = 0`. The kernel is a submodule of `M`. -/ def ker (f : M β†’β‚—[R] Mβ‚‚) : submodule R M := comap f βŠ₯ @[simp] theorem mem_ker {f : M β†’β‚—[R] Mβ‚‚} {y} : y ∈ ker f ↔ f y = 0 := mem_bot R @[simp] theorem ker_id : ker (linear_map.id : M β†’β‚—[R] M) = βŠ₯ := rfl @[simp] theorem map_coe_ker (f : M β†’β‚—[R] Mβ‚‚) (x : ker f) : f x = 0 := mem_ker.1 x.2 lemma comp_ker_subtype (f : M β†’β‚—[R] Mβ‚‚) : f.comp f.ker.subtype = 0 := linear_map.ext $ Ξ» x, suffices f x = 0, by simp [this], mem_ker.1 x.2 theorem ker_comp (f : M β†’β‚—[R] Mβ‚‚) (g : Mβ‚‚ β†’β‚—[R] M₃) : ker (g.comp f) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : M β†’β‚—[R] Mβ‚‚) (g : Mβ‚‚ β†’β‚—[R] M₃) : ker f ≀ ker (g.comp f) := by rw ker_comp; exact comap_mono bot_le theorem disjoint_ker {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} : disjoint p (ker f) ↔ βˆ€ x ∈ p, f x = 0 β†’ x = 0 := by simp [disjoint_def] lemma disjoint_inl_inr : disjoint (inl R M Mβ‚‚).range (inr R M Mβ‚‚).range := by simp [disjoint_def, @eq_comm M 0, @eq_comm Mβ‚‚ 0] {contextual := tt}; intros; refl theorem ker_eq_bot' {f : M β†’β‚—[R] Mβ‚‚} : ker f = βŠ₯ ↔ (βˆ€ m, f m = 0 β†’ m = 0) := have h : (βˆ€ m ∈ (⊀ : submodule R M), f m = 0 β†’ m = 0) ↔ (βˆ€ m, f m = 0 β†’ m = 0), from ⟨λ h m, h m mem_top, Ξ» h m _, h m⟩, by simpa [h, disjoint] using @disjoint_ker _ _ _ _ _ _ _ _ f ⊀ lemma le_ker_iff_map {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} : p ≀ ker f ↔ map f p = βŠ₯ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict (p : submodule R M) (f : Mβ‚‚ β†’β‚—[R] M) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict (p : submodule R M) (f : Mβ‚‚ β†’β‚—[R] M) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := map_cod_restrict _ _ _ _ lemma map_comap_eq (f : M β†’β‚—[R] Mβ‚‚) (q : submodule R Mβ‚‚) : map f (comap f q) = range f βŠ“ q := le_antisymm (le_inf (map_mono le_top) (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma map_comap_eq_self {f : M β†’β‚—[R] Mβ‚‚} {q : submodule R Mβ‚‚} (h : q ≀ range f) : map f (comap f q) = q := by rwa [map_comap_eq, inf_eq_right] @[simp] theorem ker_zero : ker (0 : M β†’β‚—[R] Mβ‚‚) = ⊀ := eq_top_iff'.2 $ Ξ» x, by simp @[simp] theorem range_zero : range (0 : M β†’β‚—[R] Mβ‚‚) = βŠ₯ := submodule.map_zero _ theorem ker_eq_top {f : M β†’β‚—[R] Mβ‚‚} : ker f = ⊀ ↔ f = 0 := ⟨λ h, ext $ Ξ» x, mem_ker.1 $ h.symm β–Έ trivial, Ξ» h, h.symm β–Έ ker_zero⟩ lemma range_le_bot_iff (f : M β†’β‚—[R] Mβ‚‚) : range f ≀ βŠ₯ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top lemma range_le_ker_iff {f : M β†’β‚—[R] Mβ‚‚} {g : Mβ‚‚ β†’β‚—[R] M₃} : range f ≀ ker g ↔ g.comp f = 0 := ⟨λ h, ker_eq_top.1 $ eq_top_iff'.2 $ Ξ» x, h $ mem_map_of_mem trivial, Ξ» h x hx, mem_ker.2 $ exists.elim hx $ Ξ» y ⟨_, hy⟩, by rw [←hy, ←comp_apply, h, zero_apply]⟩ theorem comap_le_comap_iff {f : M β†’β‚—[R] Mβ‚‚} (hf : range f = ⊀) {p p'} : comap f p ≀ comap f p' ↔ p ≀ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : M β†’β‚—[R] Mβ‚‚} (hf : range f = ⊀) : injective (comap f) := Ξ» p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) theorem map_coprod_prod (f : M β†’β‚—[R] M₃) (g : Mβ‚‚ β†’β‚—[R] M₃) (p : submodule R M) (q : submodule R Mβ‚‚) : map (coprod f g) (p.prod q) = map f p βŠ” map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw le_def', rintro _ ⟨x, ⟨h₁, hβ‚‚βŸ©, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, hβ‚‚, rfl⟩, rfl⟩ }, { exact Ξ» x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact Ξ» x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_prod_prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) (p : submodule R Mβ‚‚) (q : submodule R M₃) : comap (prod f g) (p.prod q) = comap f p βŠ“ comap g q := submodule.ext $ Ξ» x, iff.rfl theorem prod_eq_inf_comap (p : submodule R M) (q : submodule R Mβ‚‚) : p.prod q = p.comap (linear_map.fst R M Mβ‚‚) βŠ“ q.comap (linear_map.snd R M Mβ‚‚) := submodule.ext $ Ξ» x, iff.rfl theorem prod_eq_sup_map (p : submodule R M) (q : submodule R Mβ‚‚) : p.prod q = p.map (linear_map.inl R M Mβ‚‚) βŠ” q.map (linear_map.inr R M Mβ‚‚) := by rw [← map_coprod_prod, coprod_inl_inr, map_id] lemma span_inl_union_inr {s : set M} {t : set Mβ‚‚} : span R (inl R M Mβ‚‚ '' s βˆͺ inr R M Mβ‚‚ '' t) = (span R s).prod (span R t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]; refl @[simp] lemma ker_prod (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : ker (prod f g) = ker f βŠ“ ker g := by rw [ker, ← prod_bot, comap_prod_prod]; refl lemma range_prod_le (f : M β†’β‚—[R] Mβ‚‚) (g : M β†’β‚—[R] M₃) : range (prod f g) ≀ (range f).prod (range g) := begin simp only [le_def', prod_apply, mem_range, mem_coe, mem_prod, exists_imp_distrib], rintro _ x rfl, exact ⟨⟨x, rfl⟩, ⟨x, rfl⟩⟩ end theorem ker_eq_bot_of_injective {f : M β†’β‚—[R] Mβ‚‚} (hf : injective f) : ker f = βŠ₯ := begin have : disjoint ⊀ f.ker, by { rw [disjoint_ker, ← map_zero f], exact Ξ» x hx H, hf H }, simpa [disjoint] end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] include R open submodule lemma comap_map_eq (f : M β†’β‚—[R] Mβ‚‚) (p : submodule R M) : comap f (map f p) = p βŠ” ker f := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} (h : ker f ≀ p) : comap f (map f p) = p := by rw [comap_map_eq, sup_of_le_left h] theorem map_le_map_iff (f : M β†’β‚—[R] Mβ‚‚) {p p'} : map f p ≀ map f p' ↔ p ≀ p' βŠ” ker f := by rw [map_le_iff_le_comap, comap_map_eq] theorem map_le_map_iff' {f : M β†’β‚—[R] Mβ‚‚} (hf : ker f = βŠ₯) {p p'} : map f p ≀ map f p' ↔ p ≀ p' := by rw [map_le_map_iff, hf, sup_bot_eq] theorem map_injective {f : M β†’β‚—[R] Mβ‚‚} (hf : ker f = βŠ₯) : injective (map f) := Ξ» p p' h, le_antisymm ((map_le_map_iff' hf).1 (le_of_eq h)) ((map_le_map_iff' hf).1 (ge_of_eq h)) theorem map_eq_top_iff {f : M β†’β‚—[R] Mβ‚‚} (hf : range f = ⊀) {p : submodule R M} : p.map f = ⊀ ↔ p βŠ” f.ker = ⊀ := by simp_rw [← top_le_iff, ← hf, range, map_le_map_iff] end add_comm_group section ring variables [ring R] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] include R open submodule theorem sub_mem_ker_iff {f : M β†’β‚—[R] Mβ‚‚} {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker' {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} : disjoint p (ker f) ↔ βˆ€ x y ∈ p, f x = f y β†’ x = y := disjoint_ker.trans ⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]), Ξ» H x h₁ hβ‚‚, H x 0 h₁ (zero_mem _) (by simpa using hβ‚‚)⟩ theorem inj_of_disjoint_ker {f : M β†’β‚—[R] Mβ‚‚} {p : submodule R M} {s : set M} (h : s βŠ† p) (hd : disjoint p (ker f)) : βˆ€ x y ∈ s, f x = f y β†’ x = y := Ξ» x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy) theorem ker_eq_bot {f : M β†’β‚—[R] Mβ‚‚} : ker f = βŠ₯ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊀ /-- If the union of the kernels `ker f` and `ker g` spans the domain, then the range of `prod f g` is equal to the product of `range f` and `range g`. -/ lemma range_prod_eq {f : M β†’β‚—[R] Mβ‚‚} {g : M β†’β‚—[R] M₃} (h : ker f βŠ” ker g = ⊀) : range (prod f g) = (range f).prod (range g) := begin refine le_antisymm (f.range_prod_le g) _, simp only [le_def', prod_apply, mem_range, mem_coe, mem_prod, exists_imp_distrib, and_imp, prod.forall], rintros _ _ x rfl y rfl, simp only [prod.mk.inj_iff, ← sub_mem_ker_iff], have : y - x ∈ ker f βŠ” ker g, { simp only [h, mem_top] }, rcases mem_sup.1 this with ⟨x', hx', y', hy', H⟩, refine ⟨x' + x, _, _⟩, { rwa add_sub_cancel }, { rwa [← eq_sub_iff_add_eq.1 H, add_sub_add_right_eq_sub, ← neg_mem_iff, neg_sub, add_sub_cancel'] } end end ring section field variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group Vβ‚‚] [vector_space K Vβ‚‚] lemma ker_smul (f : V β†’β‚—[K] Vβ‚‚) (a : K) (h : a β‰  0) : ker (a β€’ f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : V β†’β‚—[K] Vβ‚‚) (a : K) : ker (a β€’ f) = β¨…(h : a β‰  0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : V β†’β‚—[K] Vβ‚‚) (a : K) (h : a β‰  0) : range (a β€’ f) = range f := submodule.map_smul f _ a h lemma range_smul' (f : V β†’β‚—[K] Vβ‚‚) (a : K) : range (a β€’ f) = ⨆(h : a β‰  0), range f := submodule.map_smul' f _ a end field end linear_map lemma submodule.sup_eq_range [semiring R] [add_comm_monoid M] [semimodule R M] (p q : submodule R M) : p βŠ” q = (p.subtype.coprod q.subtype).range := submodule.ext $ Ξ» x, by simp [submodule.mem_sup, submodule.exists] namespace is_linear_map lemma is_linear_map_add [semiring R] [add_comm_monoid M] [semimodule R M] : is_linear_map R (Ξ» (x : M Γ— M), x.1 + x.2) := begin apply is_linear_map.mk, { intros x y, simp, cc }, { intros x y, simp [smul_add] } end lemma is_linear_map_sub {R M : Type*} [semiring R] [add_comm_group M] [semimodule R M]: is_linear_map R (Ξ» (x : M Γ— M), x.1 - x.2) := begin apply is_linear_map.mk, { intros x y, simp [add_comm, add_left_comm, sub_eq_add_neg] }, { intros x y, simp [smul_sub] } end end is_linear_map namespace submodule section add_comm_monoid variables {T : semiring R} [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [semimodule R M] [semimodule R Mβ‚‚] variables (p p' : submodule R M) (q : submodule R Mβ‚‚) include T open linear_map @[simp] theorem map_top (f : M β†’β‚—[R] Mβ‚‚) : map f ⊀ = range f := rfl @[simp] theorem comap_bot (f : M β†’β‚—[R] Mβ‚‚) : comap f βŠ₯ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = βŠ₯ := ker_eq_bot_of_injective $ Ξ» x y, subtype.ext_val @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊀ lemma map_subtype_le (p' : submodule R p) : map p.subtype p' ≀ p := by simpa using (map_mono le_top : map p.subtype p' ≀ p.subtype.range) /-- Under the canonical linear map from a submodule `p` to the ambient space `M`, the image of the maximal submodule of `p` is just `p `. -/ @[simp] lemma map_subtype_top : map p.subtype (⊀ : submodule R p) = p := by simp @[simp] lemma comap_subtype_eq_top {p p' : submodule R M} : comap p.subtype p' = ⊀ ↔ p ≀ p' := eq_top_iff.trans $ map_le_iff_le_comap.symm.trans $ by rw [map_subtype_top] @[simp] lemma comap_subtype_self : comap p.subtype p = ⊀ := comap_subtype_eq_top.2 (le_refl _) @[simp] theorem ker_of_le (p p' : submodule R M) (h : p ≀ p') : (of_le h).ker = βŠ₯ := by rw [of_le, ker_cod_restrict, ker_subtype] lemma range_of_le (p q : submodule R M) (h : p ≀ q) : (of_le h).range = comap q.subtype p := by rw [← map_top, of_le, linear_map.map_cod_restrict, map_top, range_subtype] @[simp] theorem map_inl : p.map (inl R M Mβ‚‚) = prod p βŠ₯ := by { ext ⟨x, y⟩, simp only [and.left_comm, eq_comm, mem_map, prod.mk.inj_iff, inl_apply, mem_bot, exists_eq_left', mem_prod] } @[simp] theorem map_inr : q.map (inr R M Mβ‚‚) = prod βŠ₯ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst R M Mβ‚‚) = prod p ⊀ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd R M Mβ‚‚) = prod ⊀ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl R M Mβ‚‚) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr R M Mβ‚‚) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst R M Mβ‚‚) = p := by ext x; simp [(⟨0, zero_mem _⟩ : βˆƒ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd R M Mβ‚‚) = q := by ext x; simp [(⟨0, zero_mem _⟩ : βˆƒ x, x ∈ p)] @[simp] theorem ker_inl : (inl R M Mβ‚‚).ker = βŠ₯ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr R M Mβ‚‚).ker = βŠ₯ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst R M Mβ‚‚).range = ⊀ := by rw [range, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd R M Mβ‚‚).range = ⊀ := by rw [range, ← prod_top, prod_map_snd] end add_comm_monoid section ring variables {T : ring R} [add_comm_group M] [add_comm_group Mβ‚‚] [semimodule R M] [semimodule R Mβ‚‚] variables (p p' : submodule R M) (q : submodule R Mβ‚‚) include T open linear_map lemma disjoint_iff_comap_eq_bot {p q : submodule R M} : disjoint p q ↔ comap p.subtype q = βŠ₯ := by rw [eq_bot_iff, ← map_le_map_iff' p.ker_subtype, map_bot, map_comap_subtype, disjoint] /-- If `N βŠ† M` then submodules of `N` are the same as submodules of `M` contained in `N` -/ def map_subtype.rel_iso : submodule R p ≃o {p' : submodule R M // p' ≀ p} := { to_fun := Ξ» p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := Ξ» q, comap p.subtype q, left_inv := Ξ» p', comap_map_eq_self $ by simp, right_inv := Ξ» ⟨q, hq⟩, subtype.ext_val $ by simp [map_comap_subtype p, inf_of_le_right hq], map_rel_iff' := Ξ» p₁ pβ‚‚, (map_le_map_iff' (ker_subtype p)).symm } /-- If `p βŠ† M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of `M`. -/ def map_subtype.order_embedding : submodule R p β†ͺo submodule R M := (rel_iso.to_rel_embedding $ map_subtype.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule R p) : map_subtype.order_embedding p p' = map p.subtype p' := rfl /-- The map from a module `M` to the quotient of `M` by a submodule `p` as a linear map. -/ def mkq : M β†’β‚—[R] p.quotient := ⟨quotient.mk, by simp, by simp⟩ @[simp] theorem mkq_apply (x : M) : p.mkq x = quotient.mk x := rfl /-- The map from the quotient of `M` by a submodule `p` to `Mβ‚‚` induced by a linear map `f : M β†’ Mβ‚‚` vanishing on `p`, as a linear map. -/ def liftq (f : M β†’β‚—[R] Mβ‚‚) (h : p ≀ f.ker) : p.quotient β†’β‚—[R] Mβ‚‚ := ⟨λ x, _root_.quotient.lift_on' x f $ Ξ» a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab, by rintro ⟨x⟩ ⟨y⟩; exact f.map_add x y, by rintro a ⟨x⟩; exact f.map_smul a x⟩ @[simp] theorem liftq_apply (f : M β†’β‚—[R] Mβ‚‚) {h} (x : M) : p.liftq f h (quotient.mk x) = f x := rfl @[simp] theorem liftq_mkq (f : M β†’β‚—[R] Mβ‚‚) (h) : (p.liftq f h).comp p.mkq = f := by ext; refl @[simp] theorem range_mkq : p.mkq.range = ⊀ := eq_top_iff'.2 $ by rintro ⟨x⟩; exact ⟨x, trivial, rfl⟩ @[simp] theorem ker_mkq : p.mkq.ker = p := by ext; simp lemma le_comap_mkq (p' : submodule R p.quotient) : p ≀ comap p.mkq p' := by simpa using (comap_mono bot_le : p.mkq.ker ≀ comap p.mkq p') @[simp] theorem mkq_map_self : map p.mkq p = βŠ₯ := by rw [eq_bot_iff, map_le_iff_le_comap, comap_bot, ker_mkq]; exact le_refl _ @[simp] theorem comap_map_mkq : comap p.mkq (map p.mkq p') = p βŠ” p' := by simp [comap_map_eq, sup_comm] @[simp] theorem map_mkq_eq_top : map p.mkq p' = ⊀ ↔ p βŠ” p' = ⊀ := by simp only [map_eq_top_iff p.range_mkq, sup_comm, ker_mkq] /-- The map from the quotient of `M` by submodule `p` to the quotient of `Mβ‚‚` by submodule `q` along `f : M β†’ Mβ‚‚` is linear. -/ def mapq (f : M β†’β‚—[R] Mβ‚‚) (h : p ≀ comap f q) : p.quotient β†’β‚—[R] q.quotient := p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h @[simp] theorem mapq_apply (f : M β†’β‚—[R] Mβ‚‚) {h} (x : M) : mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl theorem mapq_mkq (f : M β†’β‚—[R] Mβ‚‚) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f := by ext x; refl theorem comap_liftq (f : M β†’β‚—[R] Mβ‚‚) (h) : q.comap (p.liftq f h) = (q.comap f).map (mkq p) := le_antisymm (by rintro ⟨x⟩ hx; exact ⟨_, hx, rfl⟩) (by rw [map_le_iff_le_comap, ← comap_comp, liftq_mkq]; exact le_refl _) theorem map_liftq (f : M β†’β‚—[R] Mβ‚‚) (h) (q : submodule R (quotient p)) : q.map (p.liftq f h) = (q.comap p.mkq).map f := le_antisymm (by rintro _ ⟨⟨x⟩, hxq, rfl⟩; exact ⟨x, hxq, rfl⟩) (by rintro _ ⟨x, hxq, rfl⟩; exact ⟨quotient.mk x, hxq, rfl⟩) theorem ker_liftq (f : M β†’β‚—[R] Mβ‚‚) (h) : ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _ theorem range_liftq (f : M β†’β‚—[R] Mβ‚‚) (h) : range (p.liftq f h) = range f := map_liftq _ _ _ _ theorem ker_liftq_eq_bot (f : M β†’β‚—[R] Mβ‚‚) (h) (h' : ker f ≀ p) : ker (p.liftq f h) = βŠ₯ := by rw [ker_liftq, le_antisymm h h', mkq_map_self] /-- The correspondence theorem for modules: there is an order isomorphism between submodules of the quotient of `M` by `p`, and submodules of `M` larger than `p`. -/ def comap_mkq.rel_iso : submodule R p.quotient ≃o {p' : submodule R M // p ≀ p'} := { to_fun := Ξ» p', ⟨comap p.mkq p', le_comap_mkq p _⟩, inv_fun := Ξ» q, map p.mkq q, left_inv := Ξ» p', map_comap_eq_self $ by simp, right_inv := Ξ» ⟨q, hq⟩, subtype.ext_val $ by simpa [comap_map_mkq p], map_rel_iff' := Ξ» p₁ pβ‚‚, (comap_le_comap_iff $ range_mkq _).symm } /-- The ordering on submodules of the quotient of `M` by `p` embeds into the ordering on submodules of `M`. -/ def comap_mkq.order_embedding : submodule R p.quotient β†ͺo submodule R M := (rel_iso.to_rel_embedding $ comap_mkq.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule R p.quotient) : comap_mkq.order_embedding p p' = comap p.mkq p' := rfl end ring end submodule namespace linear_map variables [ring R] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [module R M] [module R Mβ‚‚] [module R M₃] lemma range_mkq_comp (f : M β†’β‚—[R] Mβ‚‚) : f.range.mkq.comp f = 0 := linear_map.ext $ Ξ» x, by { simp, use x } lemma ker_le_range_iff {f : M β†’β‚—[R] Mβ‚‚} {g : Mβ‚‚ β†’β‚—[R] M₃} : g.ker ≀ f.range ↔ f.range.mkq.comp g.ker.subtype = 0 := by rw [←range_le_ker_iff, submodule.ker_mkq, submodule.range_subtype] /-- A monomorphism is injective. -/ lemma ker_eq_bot_of_cancel {f : M β†’β‚—[R] Mβ‚‚} (h : βˆ€ (u v : f.ker β†’β‚—[R] M), f.comp u = f.comp v β†’ u = v) : f.ker = βŠ₯ := begin have h₁ : f.comp (0 : f.ker β†’β‚—[R] M) = 0 := comp_zero _, rw [←submodule.range_subtype f.ker, ←h 0 f.ker.subtype (eq.trans h₁ (comp_ker_subtype f).symm)], exact range_zero end /-- An epimorphism is surjective. -/ lemma range_eq_top_of_cancel {f : M β†’β‚—[R] Mβ‚‚} (h : βˆ€ (u v : Mβ‚‚ β†’β‚—[R] f.range.quotient), u.comp f = v.comp f β†’ u = v) : f.range = ⊀ := begin have h₁ : (0 : Mβ‚‚ β†’β‚—[R] f.range.quotient).comp f = 0 := zero_comp _, rw [←submodule.ker_mkq f.range, ←h 0 f.range.mkq (eq.trans h₁ (range_mkq_comp _).symm)], exact ker_zero end end linear_map @[simp] lemma linear_map.range_range_restrict [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [semimodule R M] [semimodule R Mβ‚‚] (f : M β†’β‚—[R] Mβ‚‚) : f.range_restrict.range = ⊀ := by simp [f.range_cod_restrict _] /-! ### Linear equivalences -/ section set_option old_structure_cmd true /-- A linear equivalence is an invertible linear map. -/ @[nolint has_inhabited_instance] structure linear_equiv (R : Type u) (M : Type v) (Mβ‚‚ : Type w) [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [semimodule R M] [semimodule R Mβ‚‚] extends M β†’β‚—[R] Mβ‚‚, M ≃ Mβ‚‚ end attribute [nolint doc_blame] linear_equiv.to_linear_map attribute [nolint doc_blame] linear_equiv.to_equiv infix ` ≃ₗ ` := linear_equiv _ notation M ` ≃ₗ[`:50 R `] ` Mβ‚‚ := linear_equiv R M Mβ‚‚ namespace linear_equiv section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_monoid M₃] [add_comm_monoid Mβ‚„] section variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] include R instance : has_coe (M ≃ₗ[R] Mβ‚‚) (M β†’β‚—[R] Mβ‚‚) := ⟨to_linear_map⟩ -- see Note [function coercion] instance : has_coe_to_fun (M ≃ₗ[R] Mβ‚‚) := ⟨_, Ξ» f, f.to_fun⟩ @[simp] lemma mk_apply {to_fun inv_fun map_add map_smul left_inv right_inv a} : (⟨to_fun, map_add, map_smul, inv_fun, left_inv, right_inv⟩ : M ≃ₗ[R] Mβ‚‚) a = to_fun a := rfl lemma to_equiv_injective : function.injective (to_equiv : (M ≃ₗ[R] Mβ‚‚) β†’ M ≃ Mβ‚‚) := Ξ» ⟨_, _, _, _, _, _⟩ ⟨_, _, _, _, _, _⟩ h, linear_equiv.mk.inj_eq.mpr (equiv.mk.inj h) end section variables {semimodule_M : semimodule R M} {semimodule_Mβ‚‚ : semimodule R Mβ‚‚} variables (e e' : M ≃ₗ[R] Mβ‚‚) @[simp, norm_cast] theorem coe_coe : ((e : M β†’β‚—[R] Mβ‚‚) : M β†’ Mβ‚‚) = (e : M β†’ Mβ‚‚) := rfl @[simp] lemma coe_to_equiv : ((e.to_equiv) : M β†’ Mβ‚‚) = (e : M β†’ Mβ‚‚) := rfl @[simp] lemma to_fun_apply {m : M} : e.to_fun m = e m := rfl section variables {e e'} @[ext] lemma ext (h : βˆ€ x, e x = e' x) : e = e' := to_equiv_injective (equiv.ext h) variables [semimodule R M] [semimodule R Mβ‚‚] lemma eq_of_linear_map_eq {f f' : M ≃ₗ[R] Mβ‚‚} (h : (f : M β†’β‚—[R] Mβ‚‚) = f') : f = f' := begin ext x, change (f : M β†’β‚—[R] Mβ‚‚) x = (f' : M β†’β‚—[R] Mβ‚‚) x, rw h end end section variables (M R) /-- The identity map is a linear equivalence. -/ @[refl] def refl [semimodule R M] : M ≃ₗ[R] M := { .. linear_map.id, .. equiv.refl M } end @[simp] lemma refl_apply [semimodule R M] (x : M) : refl R M x = x := rfl /-- Linear equivalences are symmetric. -/ @[symm] def symm : Mβ‚‚ ≃ₗ[R] M := { .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } @[simp] lemma inv_fun_apply {m : Mβ‚‚} : e.inv_fun m = e.symm m := rfl variables {semimodule_M₃ : semimodule R M₃} (e₁ : M ≃ₗ[R] Mβ‚‚) (eβ‚‚ : Mβ‚‚ ≃ₗ[R] M₃) /-- Linear equivalences are transitive. -/ @[trans] def trans : M ≃ₗ[R] M₃ := { .. eβ‚‚.to_linear_map.comp e₁.to_linear_map, .. e₁.to_equiv.trans eβ‚‚.to_equiv } /-- A linear equivalence is an additive equivalence. -/ def to_add_equiv : M ≃+ Mβ‚‚ := { .. e } @[simp] lemma coe_to_add_equiv : ⇑(e.to_add_equiv) = e := rfl @[simp] theorem trans_apply (c : M) : (e₁.trans eβ‚‚) c = eβ‚‚ (e₁ c) := rfl @[simp] theorem apply_symm_apply (c : Mβ‚‚) : e (e.symm c) = c := e.6 c @[simp] theorem symm_apply_apply (b : M) : e.symm (e b) = b := e.5 b @[simp] lemma symm_trans_apply (c : M₃) : (e₁.trans eβ‚‚).symm c = e₁.symm (eβ‚‚.symm c) := rfl @[simp] lemma trans_refl : e.trans (refl R Mβ‚‚) = e := to_equiv_injective e.to_equiv.trans_refl @[simp] lemma refl_trans : (refl R M).trans e = e := to_equiv_injective e.to_equiv.refl_trans lemma symm_apply_eq {x y} : e.symm x = y ↔ x = e y := e.to_equiv.symm_apply_eq lemma eq_symm_apply {x y} : y = e.symm x ↔ e y = x := e.to_equiv.eq_symm_apply @[simp] lemma trans_symm [semimodule R M] [semimodule R Mβ‚‚] (f : M ≃ₗ[R] Mβ‚‚) : f.trans f.symm = linear_equiv.refl R M := by { ext x, simp } @[simp] lemma symm_trans [semimodule R M] [semimodule R Mβ‚‚] (f : M ≃ₗ[R] Mβ‚‚) : f.symm.trans f = linear_equiv.refl R Mβ‚‚ := by { ext x, simp } @[simp, norm_cast] lemma refl_to_linear_map [semimodule R M] : (linear_equiv.refl R M : M β†’β‚—[R] M) = linear_map.id := rfl @[simp, norm_cast] lemma comp_coe [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] (f : M ≃ₗ[R] Mβ‚‚) (f' : Mβ‚‚ ≃ₗ[R] M₃) : (f' : Mβ‚‚ β†’β‚—[R] M₃).comp (f : M β†’β‚—[R] Mβ‚‚) = (f.trans f' : M β†’β‚—[R] M₃) := rfl @[simp] theorem map_add (a b : M) : e (a + b) = e a + e b := e.map_add' a b @[simp] theorem map_zero : e 0 = 0 := e.to_linear_map.map_zero @[simp] theorem map_smul (c : R) (x : M) : e (c β€’ x) = c β€’ e x := e.map_smul' c x @[simp] lemma map_sum {s : finset ΞΉ} (u : ΞΉ β†’ M) : e (βˆ‘ i in s, u i) = βˆ‘ i in s, e (u i) := e.to_linear_map.map_sum @[simp] theorem map_eq_zero_iff {x : M} : e x = 0 ↔ x = 0 := e.to_add_equiv.map_eq_zero_iff theorem map_ne_zero_iff {x : M} : e x β‰  0 ↔ x β‰  0 := e.to_add_equiv.map_ne_zero_iff @[simp] theorem symm_symm : e.symm.symm = e := by { cases e, refl } protected lemma bijective : function.bijective e := e.to_equiv.bijective protected lemma injective : function.injective e := e.to_equiv.injective protected lemma surjective : function.surjective e := e.to_equiv.surjective protected lemma image_eq_preimage (s : set M) : e '' s = e.symm ⁻¹' s := e.to_equiv.image_eq_preimage s lemma map_eq_comap {p : submodule R M} : (p.map e : submodule R Mβ‚‚) = p.comap e.symm := submodule.coe_injective $ by simp [e.image_eq_preimage] /-- A linear equivalence of two modules restricts to a linear equivalence from any submodule of the domain onto the image of the submodule. -/ def of_submodule (p : submodule R M) : p ≃ₗ[R] β†₯(p.map ↑e : submodule R Mβ‚‚) := { inv_fun := Ξ» y, ⟨e.symm y, by { rcases y with ⟨y', hy⟩, rw submodule.mem_map at hy, rcases hy with ⟨x, hx, hxy⟩, subst hxy, simp only [symm_apply_apply, submodule.coe_mk, coe_coe, hx], }⟩, left_inv := Ξ» x, by simp, right_inv := Ξ» y, by { apply set_coe.ext, simp, }, ..((e : M β†’β‚—[R] Mβ‚‚).dom_restrict p).cod_restrict (p.map ↑e) (Ξ» x, ⟨x, by simp⟩) } @[simp] lemma of_submodule_apply (p : submodule R M) (x : p) : ↑(e.of_submodule p x) = e x := rfl @[simp] lemma of_submodule_symm_apply (p : submodule R M) (x : (p.map ↑e : submodule R Mβ‚‚)) : ↑((e.of_submodule p).symm x) = e.symm x := rfl end section prod variables {semimodule_M : semimodule R M} {semimodule_Mβ‚‚ : semimodule R Mβ‚‚} variables {semimodule_M₃ : semimodule R M₃} {semimodule_Mβ‚„ : semimodule R Mβ‚„} variables (e₁ : M ≃ₗ[R] Mβ‚‚) (eβ‚‚ : M₃ ≃ₗ[R] Mβ‚„) /-- Product of linear equivalences; the maps come from `equiv.prod_congr`. -/ protected def prod : (M Γ— M₃) ≃ₗ[R] (Mβ‚‚ Γ— Mβ‚„) := { map_add' := Ξ» x y, prod.ext (e₁.map_add _ _) (eβ‚‚.map_add _ _), map_smul' := Ξ» c x, prod.ext (e₁.map_smul c _) (eβ‚‚.map_smul c _), .. equiv.prod_congr e₁.to_equiv eβ‚‚.to_equiv } lemma prod_symm : (e₁.prod eβ‚‚).symm = e₁.symm.prod eβ‚‚.symm := rfl @[simp] lemma prod_apply (p) : e₁.prod eβ‚‚ p = (e₁ p.1, eβ‚‚ p.2) := rfl @[simp, norm_cast] lemma coe_prod : (e₁.prod eβ‚‚ : (M Γ— M₃) β†’β‚—[R] (Mβ‚‚ Γ— Mβ‚„)) = (e₁ : M β†’β‚—[R] Mβ‚‚).prod_map (eβ‚‚ : M₃ β†’β‚—[R] Mβ‚„) := rfl end prod section uncurry variables (V Vβ‚‚ R) /-- Linear equivalence between a curried and uncurried function. Differs from `tensor_product.curry`. -/ protected def uncurry : (V β†’ Vβ‚‚ β†’ R) ≃ₗ[R] (V Γ— Vβ‚‚ β†’ R) := { map_add' := Ξ» _ _, by { ext ⟨⟩, refl }, map_smul' := Ξ» _ _, by { ext ⟨⟩, refl }, .. equiv.arrow_arrow_equiv_prod_arrow _ _ _} @[simp] lemma coe_uncurry : ⇑(linear_equiv.uncurry R V Vβ‚‚) = uncurry := rfl @[simp] lemma coe_uncurry_symm : ⇑(linear_equiv.uncurry R V Vβ‚‚).symm = curry := rfl end uncurry section variables {semimodule_M : semimodule R M} {semimodule_Mβ‚‚ : semimodule R Mβ‚‚} variables (f : M β†’β‚—[R] Mβ‚‚) (g : Mβ‚‚ β†’β‚—[R] M) (e : M ≃ₗ[R] Mβ‚‚) variables (p q : submodule R M) /-- Linear equivalence between two equal submodules. -/ def of_eq (h : p = q) : p ≃ₗ[R] q := { map_smul' := Ξ» _ _, rfl, map_add' := Ξ» _ _, rfl, .. equiv.set.of_eq (congr_arg _ h) } variables {p q} @[simp] lemma coe_of_eq_apply (h : p = q) (x : p) : (of_eq p q h x : M) = x := rfl @[simp] lemma of_eq_symm (h : p = q) : (of_eq p q h).symm = of_eq q p h.symm := rfl /-- A linear equivalence which maps a submodule of one module onto another, restricts to a linear equivalence of the two submodules. -/ def of_submodules (p : submodule R M) (q : submodule R Mβ‚‚) (h : p.map ↑e = q) : p ≃ₗ[R] q := (e.of_submodule p).trans (linear_equiv.of_eq _ _ h) @[simp] lemma of_submodules_apply {p : submodule R M} {q : submodule R Mβ‚‚} (h : p.map ↑e = q) (x : p) : ↑(e.of_submodules p q h x) = e x := rfl @[simp] lemma of_submodules_symm_apply {p : submodule R M} {q : submodule R Mβ‚‚} (h : p.map ↑e = q) (x : q) : ↑((e.of_submodules p q h).symm x) = e.symm x := rfl variable (p) /-- The top submodule of `M` is linearly equivalent to `M`. -/ def of_top (h : p = ⊀) : p ≃ₗ[R] M := { inv_fun := Ξ» x, ⟨x, h.symm β–Έ trivial⟩, left_inv := Ξ» ⟨x, h⟩, rfl, right_inv := Ξ» x, rfl, .. p.subtype } @[simp] theorem of_top_apply {h} (x : p) : of_top p h x = x := rfl @[simp] theorem coe_of_top_symm_apply {h} (x : M) : ((of_top p h).symm x : M) = x := rfl theorem of_top_symm_apply {h} (x : M) : (of_top p h).symm x = ⟨x, h.symm β–Έ trivial⟩ := rfl /-- If a linear map has an inverse, it is a linear equivalence. -/ def of_linear (h₁ : f.comp g = linear_map.id) (hβ‚‚ : g.comp f = linear_map.id) : M ≃ₗ[R] Mβ‚‚ := { inv_fun := g, left_inv := linear_map.ext_iff.1 hβ‚‚, right_inv := linear_map.ext_iff.1 h₁, ..f } @[simp] theorem of_linear_apply {h₁ hβ‚‚} (x : M) : of_linear f g h₁ hβ‚‚ x = f x := rfl @[simp] theorem of_linear_symm_apply {h₁ hβ‚‚} (x : Mβ‚‚) : (of_linear f g h₁ hβ‚‚).symm x = g x := rfl @[simp] protected theorem range : (e : M β†’β‚—[R] Mβ‚‚).range = ⊀ := linear_map.range_eq_top.2 e.to_equiv.surjective lemma eq_bot_of_equiv [semimodule R Mβ‚‚] (e : p ≃ₗ[R] (βŠ₯ : submodule R Mβ‚‚)) : p = βŠ₯ := begin refine bot_unique (submodule.le_def'.2 $ assume b hb, (submodule.mem_bot R).2 _), rw [← p.mk_eq_zero hb, ← e.map_eq_zero_iff], apply submodule.eq_zero_of_bot_submodule end @[simp] protected theorem ker : (e : M β†’β‚—[R] Mβ‚‚).ker = βŠ₯ := linear_map.ker_eq_bot_of_injective e.to_equiv.injective end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] [add_comm_group Mβ‚„] variables {semimodule_M : semimodule R M} {semimodule_Mβ‚‚ : semimodule R Mβ‚‚} variables {semimodule_M₃ : semimodule R M₃} {semimodule_Mβ‚„ : semimodule R Mβ‚„} variables (e e₁ : M ≃ₗ[R] Mβ‚‚) (eβ‚‚ : M₃ ≃ₗ[R] Mβ‚„) @[simp] theorem map_neg (a : M) : e (-a) = -e a := e.to_linear_map.map_neg a @[simp] theorem map_sub (a b : M) : e (a - b) = e a - e b := e.to_linear_map.map_sub a b /-- Equivalence given by a block lower diagonal matrix. `e₁` and `eβ‚‚` are diagonal square blocks, and `f` is a rectangular block below the diagonal. -/ protected def skew_prod (f : M β†’β‚—[R] Mβ‚„) : (M Γ— M₃) ≃ₗ[R] Mβ‚‚ Γ— Mβ‚„ := { inv_fun := Ξ» p : Mβ‚‚ Γ— Mβ‚„, (e₁.symm p.1, eβ‚‚.symm (p.2 - f (e₁.symm p.1))), left_inv := Ξ» p, by simp, right_inv := Ξ» p, by simp, .. ((e₁ : M β†’β‚—[R] Mβ‚‚).comp (linear_map.fst R M M₃)).prod ((eβ‚‚ : M₃ β†’β‚—[R] Mβ‚„).comp (linear_map.snd R M M₃) + f.comp (linear_map.fst R M M₃)) } @[simp] lemma skew_prod_apply (f : M β†’β‚—[R] Mβ‚„) (x) : e₁.skew_prod eβ‚‚ f x = (e₁ x.1, eβ‚‚ x.2 + f x.1) := rfl @[simp] lemma skew_prod_symm_apply (f : M β†’β‚—[R] Mβ‚„) (x) : (e₁.skew_prod eβ‚‚ f).symm x = (e₁.symm x.1, eβ‚‚.symm (x.2 - f (e₁.symm x.1))) := rfl end add_comm_group section ring variables [ring R] [add_comm_group M] [add_comm_group Mβ‚‚] variables {semimodule_M : semimodule R M} {semimodule_Mβ‚‚ : semimodule R Mβ‚‚} variables (f : M β†’β‚—[R] Mβ‚‚) (e : M ≃ₗ[R] Mβ‚‚) /-- An `injective` linear map `f : M β†’β‚—[R] Mβ‚‚` defines a linear equivalence between `M` and `f.range`. -/ noncomputable def of_injective (h : f.ker = βŠ₯) : M ≃ₗ[R] f.range := { .. (equiv.set.range f $ linear_map.ker_eq_bot.1 h).trans (equiv.set.of_eq f.range_coe.symm), .. f.cod_restrict f.range (Ξ» x, f.mem_range_self x) } @[simp] theorem of_injective_apply {h : f.ker = βŠ₯} (x : M) : ↑(of_injective f h x) = f x := rfl /-- A bijective linear map is a linear equivalence. Here, bijectivity is described by saying that the kernel of `f` is `{0}` and the range is the universal set. -/ noncomputable def of_bijective (hf₁ : f.ker = βŠ₯) (hfβ‚‚ : f.range = ⊀) : M ≃ₗ[R] Mβ‚‚ := (of_injective f hf₁).trans (of_top _ hfβ‚‚) @[simp] theorem of_bijective_apply {hf₁ hfβ‚‚} (x : M) : of_bijective f hf₁ hfβ‚‚ x = f x := rfl end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] open linear_map /-- Multiplying by a unit `a` of the ring `R` is a linear equivalence. -/ def smul_of_unit (a : units R) : M ≃ₗ[R] M := of_linear ((a:R) β€’ 1 : M β†’β‚— M) (((a⁻¹ : units R) : R) β€’ 1 : M β†’β‚— M) (by rw [smul_comp, comp_smul, smul_smul, units.mul_inv, one_smul]; refl) (by rw [smul_comp, comp_smul, smul_smul, units.inv_mul, one_smul]; refl) /-- A linear isomorphism between the domains and codomains of two spaces of linear maps gives a linear isomorphism between the two function spaces. -/ def arrow_congr {R M₁ Mβ‚‚ M₂₁ Mβ‚‚β‚‚ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group Mβ‚‚] [add_comm_group M₂₁] [add_comm_group Mβ‚‚β‚‚] [module R M₁] [module R Mβ‚‚] [module R M₂₁] [module R Mβ‚‚β‚‚] (e₁ : M₁ ≃ₗ[R] Mβ‚‚) (eβ‚‚ : M₂₁ ≃ₗ[R] Mβ‚‚β‚‚) : (M₁ β†’β‚—[R] M₂₁) ≃ₗ[R] (Mβ‚‚ β†’β‚—[R] Mβ‚‚β‚‚) := { to_fun := Ξ» f, (eβ‚‚ : M₂₁ β†’β‚—[R] Mβ‚‚β‚‚).comp $ f.comp e₁.symm, inv_fun := Ξ» f, (eβ‚‚.symm : Mβ‚‚β‚‚ β†’β‚—[R] M₂₁).comp $ f.comp e₁, left_inv := Ξ» f, by { ext x, simp }, right_inv := Ξ» f, by { ext x, simp }, map_add' := Ξ» f g, by { ext x, simp }, map_smul' := Ξ» c f, by { ext x, simp } } @[simp] lemma arrow_congr_apply {R M₁ Mβ‚‚ M₂₁ Mβ‚‚β‚‚ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group Mβ‚‚] [add_comm_group M₂₁] [add_comm_group Mβ‚‚β‚‚] [module R M₁] [module R Mβ‚‚] [module R M₂₁] [module R Mβ‚‚β‚‚] (e₁ : M₁ ≃ₗ[R] Mβ‚‚) (eβ‚‚ : M₂₁ ≃ₗ[R] Mβ‚‚β‚‚) (f : M₁ β†’β‚—[R] M₂₁) (x : Mβ‚‚) : arrow_congr e₁ eβ‚‚ f x = eβ‚‚ (f (e₁.symm x)) := rfl lemma arrow_congr_comp {N Nβ‚‚ N₃ : Sort*} [add_comm_group N] [add_comm_group Nβ‚‚] [add_comm_group N₃] [module R N] [module R Nβ‚‚] [module R N₃] (e₁ : M ≃ₗ[R] N) (eβ‚‚ : Mβ‚‚ ≃ₗ[R] Nβ‚‚) (e₃ : M₃ ≃ₗ[R] N₃) (f : M β†’β‚—[R] Mβ‚‚) (g : Mβ‚‚ β†’β‚—[R] M₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr eβ‚‚ e₃ g).comp (arrow_congr e₁ eβ‚‚ f) := by { ext, simp only [symm_apply_apply, arrow_congr_apply, linear_map.comp_apply], } lemma arrow_congr_trans {M₁ Mβ‚‚ M₃ N₁ Nβ‚‚ N₃ : Sort*} [add_comm_group M₁] [module R M₁] [add_comm_group Mβ‚‚] [module R Mβ‚‚] [add_comm_group M₃] [module R M₃] [add_comm_group N₁] [module R N₁] [add_comm_group Nβ‚‚] [module R Nβ‚‚] [add_comm_group N₃] [module R N₃] (e₁ : M₁ ≃ₗ[R] Mβ‚‚) (eβ‚‚ : N₁ ≃ₗ[R] Nβ‚‚) (e₃ : Mβ‚‚ ≃ₗ[R] M₃) (eβ‚„ : Nβ‚‚ ≃ₗ[R] N₃) : (arrow_congr e₁ eβ‚‚).trans (arrow_congr e₃ eβ‚„) = arrow_congr (e₁.trans e₃) (eβ‚‚.trans eβ‚„) := rfl /-- If `Mβ‚‚` and `M₃` are linearly isomorphic then the two spaces of linear maps from `M` into `Mβ‚‚` and `M` into `M₃` are linearly isomorphic. -/ def congr_right (f : Mβ‚‚ ≃ₗ[R] M₃) : (M β†’β‚—[R] Mβ‚‚) ≃ₗ (M β†’β‚— M₃) := arrow_congr (linear_equiv.refl R M) f /-- If `M` and `Mβ‚‚` are linearly isomorphic then the two spaces of linear maps from `M` and `Mβ‚‚` to themselves are linearly isomorphic. -/ def conj (e : M ≃ₗ[R] Mβ‚‚) : (module.End R M) ≃ₗ[R] (module.End R Mβ‚‚) := arrow_congr e e lemma conj_apply (e : M ≃ₗ[R] Mβ‚‚) (f : module.End R M) : e.conj f = ((↑e : M β†’β‚—[R] Mβ‚‚).comp f).comp e.symm := rfl lemma symm_conj_apply (e : M ≃ₗ[R] Mβ‚‚) (f : module.End R Mβ‚‚) : e.symm.conj f = ((↑e.symm : Mβ‚‚ β†’β‚—[R] M).comp f).comp e := rfl lemma conj_comp (e : M ≃ₗ[R] Mβ‚‚) (f g : module.End R M) : e.conj (g.comp f) = (e.conj g).comp (e.conj f) := arrow_congr_comp e e e f g lemma conj_trans (e₁ : M ≃ₗ[R] Mβ‚‚) (eβ‚‚ : Mβ‚‚ ≃ₗ[R] M₃) : e₁.conj.trans eβ‚‚.conj = (e₁.trans eβ‚‚).conj := by { ext f x, refl, } @[simp] lemma conj_id (e : M ≃ₗ[R] Mβ‚‚) : e.conj linear_map.id = linear_map.id := by { ext, simp [conj_apply], } end comm_ring section field variables [field K] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [module K M] [module K Mβ‚‚] [module K M₃] variables (K) (M) open linear_map /-- Multiplying by a nonzero element `a` of the field `K` is a linear equivalence. -/ def smul_of_ne_zero (a : K) (ha : a β‰  0) : M ≃ₗ[K] M := smul_of_unit $ units.mk0 a ha section noncomputable theory open_locale classical /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from `K` to the span of `x`, with invertibility check to consider it as an isomorphism.-/ def to_span_nonzero_singleton (x : M) (h : x β‰  0) : K ≃ₗ[K] (submodule.span K ({x} : set M)) := linear_equiv.trans ( linear_equiv.of_injective (to_span_singleton K M x) begin ext c, split, { intros hc, rw submodule.mem_bot, rw mem_ker at hc, by_contra hc', have : x = 0, calc x = c⁻¹ β€’ (c β€’ x) : by rw [← mul_smul, inv_mul_cancel hc', one_smul] ... = c⁻¹ β€’ ((to_span_singleton K M x) c) : rfl ... = 0 : by rw [hc, smul_zero], tauto }, { rw [mem_ker, submodule.mem_bot], intros h, rw h, simp } end ) (of_eq (to_span_singleton K M x).range (submodule.span K {x}) (span_singleton_eq_range K M x).symm) lemma to_span_nonzero_singleton_one (x : M) (h : x β‰  0) : to_span_nonzero_singleton K M x h 1 = (⟨x, submodule.mem_span_singleton_self x⟩ : submodule.span K ({x} : set M)) := begin apply submodule.coe_eq_coe.mp, have : ↑(to_span_nonzero_singleton K M x h 1) = to_span_singleton K M x 1 := rfl, rw [this, to_span_singleton_one, submodule.coe_mk], end /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from the span of `x` to `K`.-/ abbreviation coord (x : M) (h : x β‰  0) : (submodule.span K ({x} : set M)) ≃ₗ[K] K := (to_span_nonzero_singleton K M x h).symm lemma coord_self (x : M) (h : x β‰  0) : (coord K M x h) ( ⟨x, submodule.mem_span_singleton_self x⟩ : submodule.span K ({x} : set M)) = 1 := by rw [← to_span_nonzero_singleton_one K M x h, symm_apply_apply] end end field end linear_equiv namespace submodule section semimodule variables [semiring R] [add_comm_monoid M] [semimodule R M] /-- If `s ≀ t`, then we can view `s` as a submodule of `t` by taking the comap of `t.subtype`. -/ def comap_subtype_equiv_of_le {p q : submodule R M} (hpq : p ≀ q) : comap q.subtype p ≃ₗ[R] p := { to_fun := Ξ» x, ⟨x, x.2⟩, inv_fun := Ξ» x, ⟨⟨x, hpq x.2⟩, x.2⟩, left_inv := Ξ» x, by simp only [coe_mk, submodule.eta, coe_coe], right_inv := Ξ» x, by simp only [subtype.coe_mk, submodule.eta, coe_coe], map_add' := Ξ» x y, rfl, map_smul' := Ξ» c x, rfl } end semimodule variables [ring R] [add_comm_group M] [module R M] variables (p : submodule R M) open linear_map /-- If `p = βŠ₯`, then `M / p ≃ₗ[R] M`. -/ def quot_equiv_of_eq_bot (hp : p = βŠ₯) : p.quotient ≃ₗ[R] M := linear_equiv.of_linear (p.liftq id $ hp.symm β–Έ bot_le) p.mkq (liftq_mkq _ _ _) $ p.quot_hom_ext $ Ξ» x, rfl @[simp] lemma quot_equiv_of_eq_bot_apply_mk (hp : p = βŠ₯) (x : M) : p.quot_equiv_of_eq_bot hp (quotient.mk x) = x := rfl @[simp] lemma quot_equiv_of_eq_bot_symm_apply (hp : p = βŠ₯) (x : M) : (p.quot_equiv_of_eq_bot hp).symm x = quotient.mk x := rfl @[simp] lemma coe_quot_equiv_of_eq_bot_symm (hp : p = βŠ₯) : ((p.quot_equiv_of_eq_bot hp).symm : M β†’β‚—[R] p.quotient) = p.mkq := rfl variables (q : submodule R M) /-- Quotienting by equal submodules gives linearly equivalent quotients. -/ def quot_equiv_of_eq (h : p = q) : p.quotient ≃ₗ[R] q.quotient := { map_add' := by { rintros ⟨x⟩ ⟨y⟩, refl }, map_smul' := by { rintros x ⟨y⟩, refl }, ..@quotient.congr _ _ (quotient_rel p) (quotient_rel q) (equiv.refl _) $ Ξ» a b, by { subst h, refl } } end submodule namespace submodule variables [comm_ring R] [add_comm_group M] [add_comm_group Mβ‚‚] [module R M] [module R Mβ‚‚] variables (p : submodule R M) (q : submodule R Mβ‚‚) @[simp] lemma mem_map_equiv {e : M ≃ₗ[R] Mβ‚‚} {x : Mβ‚‚} : x ∈ p.map (e : M β†’β‚—[R] Mβ‚‚) ↔ e.symm x ∈ p := begin rw submodule.mem_map, split, { rintros ⟨y, hy, hx⟩, simp [←hx, hy], }, { intros hx, refine ⟨e.symm x, hx, by simp⟩, }, end lemma comap_le_comap_smul (f : M β†’β‚—[R] Mβ‚‚) (c : R) : comap f q ≀ comap (c β€’ f) q := begin rw le_def', intros m h, change c β€’ (f m) ∈ q, change f m ∈ q at h, apply q.smul_mem _ h, end lemma inf_comap_le_comap_add (f₁ fβ‚‚ : M β†’β‚—[R] Mβ‚‚) : comap f₁ q βŠ“ comap fβ‚‚ q ≀ comap (f₁ + fβ‚‚) q := begin rw le_def', intros m h, change f₁ m + fβ‚‚ m ∈ q, change f₁ m ∈ q ∧ fβ‚‚ m ∈ q at h, apply q.add_mem h.1 h.2, end /-- Given modules `M`, `Mβ‚‚` over a commutative ring, together with submodules `p βŠ† M`, `q βŠ† Mβ‚‚`, the set of maps $\\{f ∈ Hom(M, Mβ‚‚) | f(p) βŠ† q \\}$ is a submodule of `Hom(M, Mβ‚‚)`. -/ def compatible_maps : submodule R (M β†’β‚—[R] Mβ‚‚) := { carrier := {f | p ≀ comap f q}, zero_mem' := by { change p ≀ comap 0 q, rw comap_zero, refine le_top, }, add_mem' := Ξ» f₁ fβ‚‚ h₁ hβ‚‚, by { apply le_trans _ (inf_comap_le_comap_add q f₁ fβ‚‚), rw le_inf_iff, exact ⟨h₁, hβ‚‚βŸ©, }, smul_mem' := Ξ» c f h, le_trans h (comap_le_comap_smul q f c), } /-- Given modules `M`, `Mβ‚‚` over a commutative ring, together with submodules `p βŠ† M`, `q βŠ† Mβ‚‚`, the natural map $\\{f ∈ Hom(M, Mβ‚‚) | f(p) βŠ† q \\} \to Hom(M/p, Mβ‚‚/q)$ is linear. -/ def mapq_linear : compatible_maps p q β†’β‚—[R] p.quotient β†’β‚—[R] q.quotient := { to_fun := Ξ» f, mapq _ _ f.val f.property, map_add' := Ξ» x y, by { ext m', apply quotient.induction_on' m', intros m, refl, }, map_smul' := Ξ» c f, by { ext m', apply quotient.induction_on' m', intros m, refl, } } end submodule namespace equiv variables [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid Mβ‚‚] [semimodule R Mβ‚‚] /-- An equivalence whose underlying function is linear is a linear equivalence. -/ def to_linear_equiv (e : M ≃ Mβ‚‚) (h : is_linear_map R (e : M β†’ Mβ‚‚)) : M ≃ₗ[R] Mβ‚‚ := { .. e, .. h.mk' e} end equiv namespace linear_map open submodule section isomorphism_laws variables [ring R] [add_comm_group M] [add_comm_group Mβ‚‚] [add_comm_group M₃] variables [module R M] [module R Mβ‚‚] [module R M₃] variables (f : M β†’β‚—[R] Mβ‚‚) /-- The first isomorphism law for modules. The quotient of `M` by the kernel of `f` is linearly equivalent to the range of `f`. -/ noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[R] f.range := (linear_equiv.of_injective (f.ker.liftq f $ le_refl _) $ submodule.ker_liftq_eq_bot _ _ _ (le_refl f.ker)).trans (linear_equiv.of_eq _ _ $ submodule.range_liftq _ _ _) @[simp] lemma quot_ker_equiv_range_apply_mk (x : M) : (f.quot_ker_equiv_range (submodule.quotient.mk x) : Mβ‚‚) = f x := rfl @[simp] lemma quot_ker_equiv_range_symm_apply_image (x : M) (h : f x ∈ f.range) : f.quot_ker_equiv_range.symm ⟨f x, h⟩ = f.ker.mkq x := f.quot_ker_equiv_range.symm_apply_apply (f.ker.mkq x) /-- Canonical linear map from the quotient `p/(p ∩ p')` to `(p+p')/p'`, mapping `x + (p ∩ p')` to `x + p'`, where `p` and `p'` are submodules of an ambient module. -/ def quotient_inf_to_sup_quotient (p p' : submodule R M) : (comap p.subtype (p βŠ“ p')).quotient β†’β‚—[R] (comap (p βŠ” p').subtype p').quotient := (comap p.subtype (p βŠ“ p')).liftq ((comap (p βŠ” p').subtype p').mkq.comp (of_le le_sup_left)) begin rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype], exact comap_mono (inf_le_inf_right _ le_sup_left) end /-- Second Isomorphism Law : the canonical map from `p/(p ∩ p')` to `(p+p')/p'` as a linear isomorphism. -/ noncomputable def quotient_inf_equiv_sup_quotient (p p' : submodule R M) : (comap p.subtype (p βŠ“ p')).quotient ≃ₗ[R] (comap (p βŠ” p').subtype p').quotient := linear_equiv.of_bijective (quotient_inf_to_sup_quotient p p') begin rw [quotient_inf_to_sup_quotient, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], exact Ξ» ⟨x, hx1⟩ hx2, ⟨hx1, hx2⟩ end begin rw [quotient_inf_to_sup_quotient, range_liftq, eq_top_iff'], rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩, use [⟨y, hy⟩, trivial], apply (submodule.quotient.eq _).2, change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff] end @[simp] lemma coe_quotient_inf_to_sup_quotient (p p' : submodule R M) : ⇑(quotient_inf_to_sup_quotient p p') = quotient_inf_equiv_sup_quotient p p' := rfl @[simp] lemma quotient_inf_equiv_sup_quotient_apply_mk (p p' : submodule R M) (x : p) : quotient_inf_equiv_sup_quotient p p' (submodule.quotient.mk x) = submodule.quotient.mk (of_le (le_sup_left : p ≀ p βŠ” p') x) := rfl lemma quotient_inf_equiv_sup_quotient_symm_apply_left (p p' : submodule R M) (x : p βŠ” p') (hx : (x:M) ∈ p) : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = submodule.quotient.mk ⟨x, hx⟩ := (linear_equiv.symm_apply_eq _).2 $ by simp [of_le_apply] @[simp] lemma quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff {p p' : submodule R M} {x : p βŠ” p'} : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 ↔ (x:M) ∈ p' := (linear_equiv.symm_apply_eq _).trans $ by simp [of_le_apply] lemma quotient_inf_equiv_sup_quotient_symm_apply_right (p p' : submodule R M) {x : p βŠ” p'} (hx : (x:M) ∈ p') : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 := quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff.2 hx end isomorphism_laws section prod lemma is_linear_map_prod_iso {R M Mβ‚‚ M₃ : Type*} [comm_semiring R] [add_comm_monoid M] [add_comm_monoid Mβ‚‚] [add_comm_group M₃] [semimodule R M] [semimodule R Mβ‚‚] [semimodule R M₃] : is_linear_map R (Ξ»(p : (M β†’β‚—[R] Mβ‚‚) Γ— (M β†’β‚—[R] M₃)), (linear_map.prod p.1 p.2 : (M β†’β‚—[R] (Mβ‚‚ Γ— M₃)))) := ⟨λu v, rfl, Ξ»c u, rfl⟩ end prod section pi universe i variables [semiring R] [add_comm_monoid Mβ‚‚] [semimodule R Mβ‚‚] [add_comm_monoid M₃] [semimodule R M₃] {Ο† : ΞΉ β†’ Type i} [βˆ€i, add_comm_monoid (Ο† i)] [βˆ€i, semimodule R (Ο† i)] /-- `pi` construction for linear functions. From a family of linear functions it produces a linear function into a family of modules. -/ def pi (f : Ξ i, Mβ‚‚ β†’β‚—[R] Ο† i) : Mβ‚‚ β†’β‚—[R] (Ξ i, Ο† i) := ⟨λc i, f i c, Ξ» c d, funext $ Ξ» i, (f i).map_add _ _, Ξ» c d, funext $ Ξ» i, (f i).map_smul _ _⟩ @[simp] lemma pi_apply (f : Ξ i, Mβ‚‚ β†’β‚—[R] Ο† i) (c : Mβ‚‚) (i : ΞΉ) : pi f c i = f i c := rfl lemma ker_pi (f : Ξ i, Mβ‚‚ β†’β‚—[R] Ο† i) : ker (pi f) = (β¨…i:ΞΉ, ker (f i)) := by ext c; simp [funext_iff]; refl lemma pi_eq_zero (f : Ξ i, Mβ‚‚ β†’β‚—[R] Ο† i) : pi f = 0 ↔ (βˆ€i, f i = 0) := by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, Ξ»h a b, h b a⟩ lemma pi_zero : pi (Ξ»i, 0 : Ξ i, Mβ‚‚ β†’β‚—[R] Ο† i) = 0 := by ext; refl lemma pi_comp (f : Ξ i, Mβ‚‚ β†’β‚—[R] Ο† i) (g : M₃ β†’β‚—[R] Mβ‚‚) : (pi f).comp g = pi (Ξ»i, (f i).comp g) := rfl /-- The projections from a family of modules are linear maps. -/ def proj (i : ΞΉ) : (Ξ i, Ο† i) β†’β‚—[R] Ο† i := ⟨ Ξ»a, a i, assume f g, rfl, assume c f, rfl ⟩ @[simp] lemma 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 $ submodule.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 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 section variable [decidable_eq ΞΉ] variables (R Ο†) /-- The standard basis of the product of `Ο†`. -/ def std_basis (i : ΞΉ) : Ο† i β†’β‚—[R] (Ξ i, Ο† i) := pi (diag i) lemma std_basis_apply (i : ΞΉ) (b : Ο† i) : std_basis R Ο† i b = update 0 i b := by ext j; rw [std_basis, pi_apply, diag, update_apply]; refl @[simp] lemma std_basis_same (i : ΞΉ) (b : Ο† i) : std_basis R Ο† i b i = b := by rw [std_basis_apply, update_same] lemma std_basis_ne (i j : ΞΉ) (h : j β‰  i) (b : Ο† i) : std_basis R Ο† i b j = 0 := by rw [std_basis_apply, update_noteq h]; refl lemma ker_std_basis (i : ΞΉ) : ker (std_basis R Ο† i) = βŠ₯ := ker_eq_bot_of_injective $ assume f g hfg, have std_basis R Ο† i f i = std_basis R Ο† i g i := hfg β–Έ rfl, by simpa only [std_basis_same] lemma proj_comp_std_basis (i j : ΞΉ) : (proj i).comp (std_basis R Ο† j) = diag j i := by rw [std_basis, proj_pi] lemma proj_std_basis_same (i : ΞΉ) : (proj i).comp (std_basis R Ο† i) = id := by ext b; simp lemma proj_std_basis_ne (i j : ΞΉ) (h : i β‰  j) : (proj i).comp (std_basis R Ο† j) = 0 := by ext b; simp [std_basis_ne R Ο† _ _ h] lemma supr_range_std_basis_le_infi_ker_proj (I J : set ΞΉ) (h : disjoint I J) : (⨆i∈I, range (std_basis R Ο† i)) ≀ (β¨…i∈J, ker (proj i)) := begin refine (supr_le $ assume i, supr_le $ assume hi, range_le_iff_comap.2 _), simp only [(ker_comp _ _).symm, eq_top_iff, le_def', mem_ker, comap_infi, mem_infi], assume b hb j hj, have : i β‰  j := assume eq, h ⟨hi, eq.symm β–Έ hj⟩, rw [proj_std_basis_ne R Ο† j i this.symm, zero_apply] end lemma infi_ker_proj_le_supr_range_std_basis {I : finset ΞΉ} {J : set ΞΉ} (hu : set.univ βŠ† ↑I βˆͺ J) : (β¨… i∈J, ker (proj i)) ≀ (⨆i∈I, range (std_basis R Ο† i)) := submodule.le_def'.2 begin assume b hb, simp only [mem_infi, mem_ker, proj_apply] at hb, rw ← show βˆ‘ i in I, std_basis R Ο† i (b i) = b, { ext i, rw [finset.sum_apply, ← std_basis_same R Ο† i (b i)], refine finset.sum_eq_single i (assume j hjI ne, std_basis_ne _ _ _ _ ne.symm _) _, assume hiI, rw [std_basis_same], exact hb _ ((hu trivial).resolve_left hiI) }, exact sum_mem _ (assume i hiI, mem_supr_of_mem i $ mem_supr_of_mem hiI $ (std_basis R Ο† i).mem_range_self (b i)) end lemma supr_range_std_basis_eq_infi_ker_proj {I J : set ΞΉ} (hd : disjoint I J) (hu : set.univ βŠ† I βˆͺ J) (hI : set.finite I) : (⨆i∈I, range (std_basis R Ο† i)) = (β¨…i∈J, ker (proj i)) := begin refine le_antisymm (supr_range_std_basis_le_infi_ker_proj _ _ _ _ hd) _, have : set.univ βŠ† ↑hI.to_finset βˆͺ J, { rwa [hI.coe_to_finset] }, refine le_trans (infi_ker_proj_le_supr_range_std_basis R Ο† this) (supr_le_supr $ assume i, _), rw [set.finite.mem_to_finset], exact le_refl _ end lemma supr_range_std_basis [fintype ΞΉ] : (⨆i:ΞΉ, range (std_basis R Ο† i)) = ⊀ := have (set.univ : set ΞΉ) βŠ† ↑(finset.univ : finset ΞΉ) βˆͺ βˆ… := by rw [finset.coe_univ, set.union_empty], begin apply top_unique, convert (infi_ker_proj_le_supr_range_std_basis R Ο† this), exact infi_emptyset.symm, exact (funext $ Ξ»i, (@supr_pos _ _ _ (Ξ»h, range (std_basis R Ο† i)) $ finset.mem_univ i).symm) end lemma disjoint_std_basis_std_basis (I J : set ΞΉ) (h : disjoint I J) : disjoint (⨆i∈I, range (std_basis R Ο† i)) (⨆i∈J, range (std_basis R Ο† i)) := begin refine disjoint.mono (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl I) (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl J) _, simp only [disjoint, submodule.le_def', mem_infi, mem_inf, mem_ker, mem_bot, proj_apply, funext_iff], rintros b ⟨hI, hJ⟩ i, classical, by_cases hiI : i ∈ I, { by_cases hiJ : i ∈ J, { exact (h ⟨hiI, hiJ⟩).elim }, { exact hJ i hiJ } }, { exact hI i hiI } end lemma std_basis_eq_single {a : R} : (Ξ» (i : ΞΉ), (std_basis R (Ξ» _ : ΞΉ, R) i) a) = Ξ» (i : ΞΉ), (finsupp.single i a) := begin ext i j, rw [std_basis_apply, finsupp.single_apply], split_ifs, { rw [h, function.update_same] }, { rw [function.update_noteq (ne.symm h)], refl }, end end end pi section fun_left variables (R M) [semiring R] [add_comm_monoid M] [semimodule R M] variables {m n p : Type*} /-- Given an `R`-module `M` and a function `m β†’ n` between arbitrary types, construct a linear map `(n β†’ M) β†’β‚—[R] (m β†’ M)` -/ def fun_left (f : m β†’ n) : (n β†’ M) β†’β‚—[R] (m β†’ M) := mk (∘f) (Ξ» _ _, rfl) (Ξ» _ _, rfl) @[simp] theorem fun_left_apply (f : m β†’ n) (g : n β†’ M) (i : m) : fun_left R M f g i = g (f i) := rfl @[simp] theorem fun_left_id (g : n β†’ M) : fun_left R M _root_.id g = g := rfl theorem fun_left_comp (f₁ : n β†’ p) (fβ‚‚ : m β†’ n) : fun_left R M (f₁ ∘ fβ‚‚) = (fun_left R M fβ‚‚).comp (fun_left R M f₁) := rfl /-- Given an `R`-module `M` and an equivalence `m ≃ n` between arbitrary types, construct a linear equivalence `(n β†’ M) ≃ₗ[R] (m β†’ M)` -/ def fun_congr_left (e : m ≃ n) : (n β†’ M) ≃ₗ[R] (m β†’ M) := linear_equiv.of_linear (fun_left R M e) (fun_left R M e.symm) (ext $ Ξ» x, funext $ Ξ» i, by rw [id_apply, ← fun_left_comp, equiv.symm_comp_self, fun_left_id]) (ext $ Ξ» x, funext $ Ξ» i, by rw [id_apply, ← fun_left_comp, equiv.self_comp_symm, fun_left_id]) @[simp] theorem fun_congr_left_apply (e : m ≃ n) (x : n β†’ M) : fun_congr_left R M e x = fun_left R M e x := rfl @[simp] theorem fun_congr_left_id : fun_congr_left R M (equiv.refl n) = linear_equiv.refl R (n β†’ M) := rfl @[simp] theorem fun_congr_left_comp (e₁ : m ≃ n) (eβ‚‚ : n ≃ p) : fun_congr_left R M (equiv.trans e₁ eβ‚‚) = linear_equiv.trans (fun_congr_left R M eβ‚‚) (fun_congr_left R M e₁) := rfl @[simp] lemma fun_congr_left_symm (e : m ≃ n) : (fun_congr_left R M e).symm = fun_congr_left R M e.symm := rfl end fun_left universe i variables [semiring R] [add_comm_monoid M] [semimodule R M] variables (R M) instance automorphism_group : group (M ≃ₗ[R] M) := { mul := Ξ» f g, g.trans f, one := linear_equiv.refl R M, inv := Ξ» f, f.symm, mul_assoc := Ξ» f g h, by {ext, refl}, mul_one := Ξ» f, by {ext, refl}, one_mul := Ξ» f, by {ext, refl}, mul_left_inv := Ξ» f, by {ext, exact f.left_inv x} } instance automorphism_group.to_linear_map_is_monoid_hom : is_monoid_hom (linear_equiv.to_linear_map : (M ≃ₗ[R] M) β†’ (M β†’β‚—[R] M)) := { map_one := rfl, map_mul := Ξ» f g, rfl } /-- The group of invertible linear maps from `M` to itself -/ @[reducible] def general_linear_group := units (M β†’β‚—[R] M) namespace general_linear_group variables {R M} instance : has_coe_to_fun (general_linear_group R M) := by apply_instance /-- An invertible linear map `f` determines an equivalence from `M` to itself. -/ def to_linear_equiv (f : general_linear_group R M) : (M ≃ₗ[R] M) := { inv_fun := f.inv.to_fun, left_inv := Ξ» m, show (f.inv * f.val) m = m, by erw f.inv_val; simp, right_inv := Ξ» m, show (f.val * f.inv) m = m, by erw f.val_inv; simp, ..f.val } /-- An equivalence from `M` to itself determines an invertible linear map. -/ def of_linear_equiv (f : (M ≃ₗ[R] M)) : general_linear_group R M := { val := f, inv := f.symm, val_inv := linear_map.ext $ Ξ» _, f.apply_symm_apply _, inv_val := linear_map.ext $ Ξ» _, f.symm_apply_apply _ } variables (R M) /-- The general linear group on `R` and `M` is multiplicatively equivalent to the type of linear equivalences between `M` and itself. -/ def general_linear_equiv : general_linear_group R M ≃* (M ≃ₗ[R] M) := { to_fun := to_linear_equiv, inv_fun := of_linear_equiv, left_inv := Ξ» f, by { ext, refl }, right_inv := Ξ» f, by { ext, refl }, map_mul' := Ξ» x y, by {ext, refl} } @[simp] lemma general_linear_equiv_to_linear_map (f : general_linear_group R M) : (general_linear_equiv R M f : M β†’β‚—[R] M) = f := by {ext, refl} end general_linear_group end linear_map
de3e3d30e720732c086447b6de66dc0c63efd558
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/floatarray.lean
9a648a950a87b65b79962d4d3aa3dae624a80dbd
[ "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
272
lean
new_frontend def tst : IO Unit := do let bs := [(1 : Float), 2, 3].toFloatArray; IO.println bs; let bs := bs.push (4 : Float); let bs := bs.set! 1 (20 / 3); IO.println bs; let bs₁ := bs.set! 2 30; IO.println bs₁; IO.println bs; IO.println bs.size; pure () #eval tst
0bd7333428a95688435c819b129923169fa3bfe8
4d3f29a7b2eff44af8fd0d3176232e039acb9ee3
/Mathlib/Mathlib/Data/Int/Basic.lean
7893f8172909920f25306480a458d01cc1efa237
[]
no_license
marijnheule/lamr
5fc5d69d326ff92e321242cfd7f72e78d7f99d7e
28cc4114c7361059bb54f407fa312bf38b48728b
refs/heads/main
1,689,338,013,620
1,630,359,632,000
1,630,359,632,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,259
lean
/- Ported by Deniz Aydin from the lean3 prelude's data/int/basic.lean. Should be in a "prelude" https://github.com/leanprover-community/lean/blob/master/library/init/data/int/basic.lean Original file license: Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import Mathlib.Data.Nat.Basic import Mathlib.Tactic.Coe open Nat /- ## Additional notation -/ notation "β„€" => Int notation "-[1+ " n "]" => Int.negSucc n namespace Int lemma ofNat_zero : ofNat (0 : β„•) = (0 : β„€) := rfl lemma ofNat_one : ofNat (1 : β„•) = (1 : β„€) := rfl /- ## Definitions of basic functions -/ lemma subNatNat_of_sub_eq_zero {m n : β„•} (h : n - m = 0) : subNatNat m n = ofNat (m - n) := by show (match (n - m) with -- No `unfold` so I do this | 0 => ofNat (m-n) | succ k => -[1+ k] ) = ofNat (m - n) rw [h] rfl lemma subNatNat_of_sub_eq_succ {m n k : β„•} (h : n - m = succ k) : subNatNat m n = -[1+ k] := by show (match n - m with | 0 => ofNat (m - n) | succ k => -[1+ k]) = -[1+ k] rw [h] protected lemma neg_zero : -(0:β„€) = 0 := rfl lemma ofNat_add (n m : β„•) : ofNat (n + m) = ofNat n + ofNat m := rfl lemma ofNat_mul (n m : β„•) : ofNat (n * m) = ofNat n * ofNat m := rfl lemma ofNat_succ (n : β„•) : ofNat (succ n) = ofNat n + 1 := rfl lemma neg_ofNat_zero : -(ofNat 0) = 0 := rfl lemma neg_ofNat_of_succ (n : β„•) : -(ofNat (succ n)) = -[1+ n] := rfl lemma neg_neg_ofNat_succ (n : β„•) : -(-[1+ n]) = ofNat (succ n) := rfl lemma negSucc_ofNat_coe (n : β„•) : -[1+ n] = -↑(n + 1) := rfl /- ## These are only for internal use -/ lemma ofNat_add_ofNat (m n : β„•) : ofNat m + ofNat n = ofNat (m + n) := rfl lemma ofNat_add_negSucc_ofNat (m n : β„•) : ofNat m + -[1+ n] = subNatNat m (succ n) := rfl lemma negSucc_ofNat_add_ofNat (m n : β„•) : -[1+ m] + ofNat n = subNatNat n (succ m) := rfl lemma negSucc_ofNat_add_negSucc_ofNat (m n : β„•) : -[1+ m] + -[1+ n] = -[1+ succ (m + n)] := rfl lemma ofNat_mul_ofNat (m n : β„•) : ofNat m * ofNat n = ofNat (m * n) := rfl lemma ofNat_mul_negSucc_ofNat (m n : β„•) : ofNat m * -[1+ n] = negOfNat (m * succ n) := rfl lemma negSucc_ofNat_ofNat (m n : β„•) : -[1+ m] * ofNat n = negOfNat (succ m * n) := rfl lemma mul_negSucc_ofNat_negSucc_ofNat (m n : β„•) : -[1+ m] * -[1+ n] = ofNat (succ m * succ n) := rfl attribute [local simp] ofNat_add_ofNat ofNat_mul_ofNat neg_ofNat_zero neg_ofNat_of_succ neg_neg_ofNat_succ ofNat_add_negSucc_ofNat negSucc_ofNat_add_ofNat negSucc_ofNat_add_negSucc_ofNat ofNat_mul_negSucc_ofNat negSucc_ofNat_ofNat mul_negSucc_ofNat_negSucc_ofNat /- ## some basic functions and properties -/ protected lemma coe_nat_inj {m n : β„•} (h : (↑m : β„€) = ↑n) : m = n := Int.ofNat.inj h lemma ofNat_eq_ofNat_iff (m n : β„•) : ofNat m = ofNat n ↔ m = n := Iff.intro Int.ofNat.inj (congr_arg _) lemma negSucc_ofNat_inj_iff {m n : β„•} : negSucc m = negSucc n ↔ m = n := ⟨Int.negSucc.inj, Ξ» H => by simp [H]⟩ lemma negSucc_ofNat_eq (n : β„•) : -[1+ n] = -((↑n) + 1) := rfl /- ## neg -/ protected lemma neg_neg : βˆ€ a : β„€, -(-a) = a | ofNat 0 => rfl | ofNat (n+1) => rfl | -[1+ n] => rfl protected lemma neg_inj {a b : β„€} (h : -a = -b) : a = b := by rw [← Int.neg_neg a, ← Int.neg_neg b, h] protected lemma sub_eq_add_neg {a b : β„€} : a - b = a + -b := rfl /- ## basic properties of subNatNat -/ lemma subNatNat_elim (m n : β„•) (P : β„• β†’ β„• β†’ β„€ β†’ Prop) (hp : βˆ€i n, P (n + i) n (ofNat i)) (hn : βˆ€i m, P m (m + i + 1) (-[1+ i])) : P m n (subNatNat m n) := by have H : βˆ€ k , n - m = k β†’ P m n (match n - m with | 0 => ofNat (m - n) | succ k => -[1+ k] : β„€) := by intros k h cases k with | zero => have ⟨k, h⟩ := (Nat.le.dest (Nat.le_of_sub_eq_zero h)) rw [h.symm, Nat.add_sub_cancel_left, sub_self_add] apply hp | succ k => simp have hle : m ≀ n := Nat.le_of_lt (Nat.lt_of_sub_eq_succ h) rw [Nat.sub_eq_iff_eq_add hle] at h rw [h, Nat.add_comm, Nat.add_sub_cancel_left] apply hn exact H _ rfl lemma subNatNat_add_left {m n : β„•} : subNatNat (m + n) m = ofNat n := by change (match m - (m + n) with | 0 => ofNat (m + n - m) | succ k => -[1+ k]) = ofNat n rw [Nat.sub_eq_zero_of_le (Nat.le_add_right _ _)] rw [Nat.add_sub_cancel_left] rfl lemma subNatNat_add_right {m n : β„•} : subNatNat m (m + n + 1) = negSucc n := by change (match m + n + 1 - m with | 0 => ofNat (m - (m + n + 1)) | succ k => -[1+ k]) = negSucc n simp rw [Nat.add_assoc, Nat.add_one, Nat.add_sub_cancel_left] lemma subNatNat_add_add (m n k : β„•) : subNatNat (m + k) (n + k) = subNatNat m n := subNatNat_elim m n (Ξ»m n i => subNatNat (m + k) (n + k) = i) (by intros i j rw [Nat.add_assoc, Nat.add_comm i k, ← Nat.add_assoc] exact subNatNat_add_left ) (by intros i j rw [Nat.add_assoc j i 1, Nat.add_comm j (i+1), Nat.add_assoc, Nat.add_comm (i+1) (j+k)] exact subNatNat_add_right ) lemma subNatNat_of_le {m n : β„•} (h : n ≀ m) : subNatNat m n = ofNat (m - n) := subNatNat_of_sub_eq_zero (sub_eq_zero_of_le h) lemma subNatNat_of_lt {m n : β„•} (h : m < n) : subNatNat m n = -[1+ pred (n - m)] := by rw [subNatNat_of_sub_eq_succ] rw [Nat.succ_pred_eq_of_pos (Nat.sub_pos_of_lt h)] /- ## natAbs -/ lemma natAbs_ofNat (n : β„•) : natAbs ↑n = n := rfl lemma eq_zero_ofNatAbs_eq_zero : βˆ€ {a : β„€}, natAbs a = 0 β†’ a = 0 | (ofNat _), H => congr_arg ofNat H | -[1+ _], H => absurd H (succ_ne_zero _) lemma natAbs_pos_of_ne_zero {a : β„€} (h : a β‰  0) : 0 < natAbs a := (eq_zero_or_pos _).resolve_left $ mt eq_zero_ofNatAbs_eq_zero h lemma natAbs_zero : natAbs (0 : β„€) = (0 : β„•) := rfl lemma natAbs_one : natAbs (1 : β„€) = (1 : β„•) := rfl lemma natAbs_mul_self : βˆ€ {a : β„€}, ↑(natAbs a * natAbs a) = a * a | (ofNat _) => rfl | -[1+ _] => rfl @[simp] lemma natAbs_neg (a : β„€) : natAbs (-a) = natAbs a := match a with | (ofNat zero) => rfl | (ofNat (succ _)) => rfl | (negSucc _) => rfl lemma natAbs_eq : βˆ€ (a : β„€), a = natAbs a ∨ a = -↑(natAbs a) | (ofNat _) => Or.inl rfl | -[1+ _] => Or.inr rfl lemma eq_x_or_neg (a : β„€) : βˆƒn : β„•, a = n ∨ a = -↑n := ⟨_, natAbs_eq a⟩ /- ## sign -/ def sign : β„€ β†’ β„€ | (n+1:β„•) => 1 | 0 => 0 | -[1+ n] => -1 @[simp] theorem sign_zero : sign 0 = 0 := rfl @[simp] theorem sign_one : sign 1 = 1 := rfl @[simp] theorem sign_neg_one : sign (-1) = -1 := rfl /- ## Quotient and remainder There are three main conventions for integer division, referred here as the E, F, T rounding conventions. All three pairs satisfy the identity x % y + (x / y) * y = x unconditionally. -/ -- E-rounding (`div` and `mod` defined in core) satisfy 0 ≀ mod x y < natAbs y for y β‰  0 -- F-rounding: This pair satisfies fdiv x y = floor (x / y) def fdiv : β„€ β†’ β„€ β†’ β„€ | 0, _ => 0 | (m : β„•), (n : β„•) => ofNat (m / n) | (m+1:β„•), -[1+ n] => -[1+ m / succ n] | -[1+ m], 0 => 0 | -[1+ m], (n+1:β„•) => -[1+ m / succ n] | -[1+ m], -[1+ n] => ofNat (succ m / succ n) def fmod : β„€ β†’ β„€ β†’ β„€ | 0, _ => 0 | (m : β„•), (n : β„•) => ofNat (m % n) | (m+1:β„•), -[1+ n] => subNatNat (m % succ n) n | -[1+ m], (n : β„•) => subNatNat n (succ (m % n)) | -[1+ m], -[1+ n] => -ofNat (succ m % succ n) -- T-rounding: This pair satisfies quot x y = round_to_zero (x / y) def quot : β„€ β†’ β„€ β†’ β„€ | (m : β„•), (n : β„•) => ofNat (m / n) | (m : β„•), -[1+ n] => -ofNat (m / succ n) | -[1+ m], (n : β„•) => -ofNat (succ m / n) | -[1+ m], -[1+ n] => ofNat (succ m / succ n) def rem : β„€ β†’ β„€ β†’ β„€ | (m : β„•), (n : β„•) => ofNat (m % n) | (m : β„•), -[1+ n] => ofNat (m % succ n) | -[1+ m], (n : β„•) => -ofNat (succ m % n) | -[1+ m], -[1+ n] => -ofNat (succ m % succ n) /- ## gcd -/ def gcd (m n : β„€) : β„• := Nat.gcd (natAbs m) (natAbs n) /- # ring properties -/ /- addition -/ protected lemma add_comm : βˆ€ a b : β„€, a + b = b + a | (n : β„•), (m : β„•) => by simp [Nat.add_comm] | (_ : β„•), -[1+ _] => rfl | -[1+ _], (_ : β„•) => rfl | -[1+ _], -[1+ _] => by simp [Nat.add_comm] protected lemma add_zero : βˆ€ a : β„€, a + 0 = a | (ofNat n) => rfl | -[1+ n] => rfl protected lemma zero_add (a : β„€) : 0 + a = a := Int.add_comm a 0 β–Έ Int.add_zero a lemma subNatNat_sub {m n : β„•} (h : n ≀ m) (k : β„•) : subNatNat (m - n) k = subNatNat m (k + n) := by rwa [← subNatNat_add_add _ _ n, Nat.sub_add_cancel] lemma subNatNat_add (m n k : β„•) : subNatNat (m + n) k = ofNat m + subNatNat n k := by have h := Nat.lt_or_ge n k cases h with | inl h' => rw [subNatNat_of_lt h'] simp rw [succ_pred_eq_of_pos (Nat.sub_pos_of_lt h')] apply Eq.trans rw [← Nat.sub_add_cancel (Nat.le_of_lt h')] apply subNatNat_add_add | inr h' => rw [subNatNat_of_le (h')] have hβ‚‚ : k ≀ m + n := Nat.le_trans h' (le_add_left _ _) rw [subNatNat_of_le hβ‚‚] simp rw [Nat.add_sub_assoc h'] lemma subNatNat_add_negSucc_ofNat (m n k : β„•) : subNatNat m n + -[1+ k] = subNatNat m (n + succ k) := by have h := Nat.lt_or_ge m n cases h with | inr h' => rw [subNatNat_of_le h'] simp rw [subNatNat_sub h', Nat.add_comm] | inl h' => have hβ‚‚ : m < n + succ k := Nat.lt_of_lt_of_le h' (le_add_right _ _) have h₃ : m ≀ n + k := le_of_succ_le_succ hβ‚‚ rw [subNatNat_of_lt h', subNatNat_of_lt hβ‚‚] simp [Nat.add_comm] rw [← add_succ, succ_pred_eq_of_pos (Nat.sub_pos_of_lt h'), add_succ, succ_sub h₃, pred_succ] rw [Nat.add_comm n, Nat.add_sub_assoc (Nat.le_of_lt h')] lemma add_assoc_aux1 (m n : β„•) : βˆ€ c : β„€, ofNat m + ofNat n + c = ofNat m + (ofNat n + c) | (ofNat k) => by simp [Nat.add_assoc] | -[1+ k] => by simp [subNatNat_add] lemma add_assoc_aux2 (m n k : β„•) : -[1+ m] + -[1+ n] + ofNat k = -[1+ m] + (-[1+ n] + ofNat k) := by simp [add_succ] rw [Int.add_comm, subNatNat_add_negSucc_ofNat] simp [add_succ, succ_add, Nat.add_comm] protected lemma add_assoc : βˆ€ a b c : β„€, a + b + c = a + (b + c) | (ofNat m), (ofNat n), c => add_assoc_aux1 _ _ _ | (ofNat m), b, (ofNat k) => by rw [Int.add_comm, ← add_assoc_aux1, Int.add_comm k, add_assoc_aux1, Int.add_comm b] | a, (ofNat n), (ofNat k) => by rw [Int.add_comm, Int.add_comm a, ← add_assoc_aux1, Int.add_comm a, Int.add_comm (ofNat k)] | -[1+ m], -[1+ n], (ofNat k) => add_assoc_aux2 _ _ _ | -[1+ m], (ofNat n), -[1+ k] => by rw [Int.add_comm, ← add_assoc_aux2, Int.add_comm n, ← add_assoc_aux2, Int.add_comm -[1+ m]] | (ofNat m), -[1+ n], -[1+ k] => by rw [Int.add_comm, Int.add_comm (ofNat m), Int.add_comm m, ← add_assoc_aux2, Int.add_comm -[1+ k]] | -[1+ m], -[1+ n], -[1+ k] => by simp [add_succ, Nat.add_comm, Nat.add_left_comm, neg_ofNat_of_succ] /- ## negation -/ lemma sub_nat_self : βˆ€ n, subNatNat n n = 0 | 0 => rfl | (succ m) => by rw [subNatNat_of_sub_eq_zero, Nat.sub_self, ofNat_zero] rw [Nat.sub_self] attribute [local simp] sub_nat_self protected lemma add_left_neg : βˆ€ a : β„€, -a + a = 0 | (ofNat 0) => rfl | (ofNat (succ m)) => by simp | -[1+ m] => by simp protected lemma add_right_neg (a : β„€) : a + -a = 0 := by rw [Int.add_comm, Int.add_left_neg] /- ## multiplication -/ protected lemma mul_comm : βˆ€ a b : β„€, a * b = b * a | a, b => by cases a <;> cases b <;> simp [Nat.mul_comm] lemma ofNat_mul_negOfNat (m : β„•) : βˆ€ n, ofNat m * negOfNat n = negOfNat (m * n) | 0 => rfl | (succ n) => rfl lemma negOfNat_mul_ofNat (m n : β„•) : negOfNat m * ofNat n = negOfNat (m * n) := by rw [Int.mul_comm] simp [ofNat_mul_negOfNat, Nat.mul_comm] lemma negSucc_ofNat_mul_negOfNat (m : β„•) : βˆ€ n, -[1+ m] * negOfNat n = ofNat (succ m * n) | 0 => rfl | (succ n) => rfl lemma negOfNat_mul_negSucc_ofNat (m n : β„•) : negOfNat n * -[1+ m] = ofNat (n * succ m) := by rw [Int.mul_comm, negSucc_ofNat_mul_negOfNat, Nat.mul_comm] attribute [local simp] ofNat_mul_negOfNat negOfNat_mul_ofNat negSucc_ofNat_mul_negOfNat negOfNat_mul_negSucc_ofNat protected lemma mul_assoc : βˆ€ a b c : β„€, a * b * c = a * (b * c) | a, b, c => by cases a <;> cases b <;> cases c <;> simp [Nat.mul_assoc] protected lemma mul_zero : βˆ€ (a : β„€), a * 0 = 0 | (ofNat m) => rfl | -[1+ m] => rfl protected lemma zero_mul (a : β„€) : 0 * a = 0 := Int.mul_comm a 0 β–Έ Int.mul_zero a lemma negOfNat_eq_subNatNat_zero : βˆ€ n, negOfNat n = subNatNat 0 n | 0 => rfl | (succ n) => rfl lemma ofNat_mul_subNatNat (m n k : β„•) : ofNat m * subNatNat n k = subNatNat (m * n) (m * k) := by cases m with | zero => simp [ofNat_zero, Int.zero_mul, Nat.zero_mul] | succ m => have h := Nat.lt_or_ge n k cases h with | inl h => have h' : (succ m) * n < (succ m) * k := Nat.mul_lt_mul_of_pos_left h (Nat.succ_pos m) rw [subNatNat_of_lt h, subNatNat_of_lt h'] simp rw [succ_pred_eq_of_pos (Nat.sub_pos_of_lt h)] rw [← neg_ofNat_of_succ, Nat.mul_sub_left_distrib] rw [← succ_pred_eq_of_pos (Nat.sub_pos_of_lt h')] rfl | inr h => have h' : (succ m) * k ≀ (succ m) * n := Nat.mul_le_mul_left _ h rw [subNatNat_of_le h, subNatNat_of_le h'] simp rw [Nat.mul_sub_left_distrib] lemma negOfNat_add : βˆ€ m n : β„•, negOfNat m + negOfNat n = negOfNat (m + n) | zero, zero => by simp | zero, succ n => by simp [Nat.zero_add]; rfl | succ m, zero => by simp; rfl | succ m, succ n => by simp [Nat.succ_add]; rfl lemma negSucc_ofNat_mul_subNatNat (m n k : β„•) : -[1+ m] * subNatNat n k = subNatNat (succ m * k) (succ m * n) := by have h := Nat.lt_or_ge n k cases h with | inl h => have h' : succ m * n < succ m * k := Nat.mul_lt_mul_of_pos_left h (Nat.succ_pos m) rw [subNatNat_of_lt h, subNatNat_of_le (Nat.le_of_lt h')] simp [succ_pred_eq_of_pos (Nat.sub_pos_of_lt h), Nat.mul_sub_left_distrib] | inr h => cases Nat.lt_or_ge k n with | inl h' => have h₁ : succ m * n > succ m * k := Nat.mul_lt_mul_of_pos_left h' (Nat.succ_pos m) rw [subNatNat_of_le h, subNatNat_of_lt h₁] simp [Nat.mul_sub_left_distrib, Nat.mul_comm] rw [Nat.mul_comm k, Nat.mul_comm n, ← succ_pred_eq_of_pos (Nat.sub_pos_of_lt h₁), ← neg_ofNat_of_succ] rfl | inr h' => rw [Nat.le_antisymm h h'] simp rfl attribute [local simp] ofNat_mul_subNatNat negOfNat_add negSucc_ofNat_mul_subNatNat protected lemma distrib_left : βˆ€ a b c : β„€, a * (b + c) = a * b + a * c | (ofNat m), (ofNat n), (ofNat k) => by simp [Nat.left_distrib] | (ofNat m), (ofNat n), -[1+ k] => by simp [negOfNat_eq_subNatNat_zero] rw [← subNatNat_add] rfl | (ofNat m), -[1+ n], (ofNat k) => by simp [negOfNat_eq_subNatNat_zero] rw [Int.add_comm, ← subNatNat_add] rfl | (ofNat m), -[1+ n], -[1+ k] => by simp rw [← Nat.left_distrib, succ_add] rfl | -[1+ m], (ofNat n), (ofNat k) => by simp [Nat.mul_comm] rw [← Nat.right_distrib, Nat.mul_comm] | -[1+ m], (ofNat n), -[1+ k] => by simp [negOfNat_eq_subNatNat_zero] rw [Int.add_comm, ← subNatNat_add] rfl | -[1+ m], -[1+ n], (ofNat k) => by simp [negOfNat_eq_subNatNat_zero] rw [← subNatNat_add] rfl | -[1+ m], -[1+ n], -[1+ k] => by simp rw [← Nat.left_distrib, succ_add] rfl protected lemma distrib_right (a b c : β„€) : (a + b) * c = a * c + b * c := by simp [Int.mul_comm, Int.distrib_left] protected lemma zero_ne_one : (0 : β„€) β‰  1 := Ξ» h => Int.noConfusion h fun. lemma ofNat_sub {n m : β„•} (h : m ≀ n) : ofNat (n - m) = ofNat n - ofNat m := by show ofNat (n - m) = ofNat n + negOfNat m match m, h with | 0, h => rfl | succ m, h => show ofNat (n - succ m) = subNatNat n (succ m) simp [subNatNat, subNatNat] -- TODO: How to avoid having to simp through rename definitions to unfold them? rw [sub_eq_zero_of_le h] rfl protected lemma add_left_comm (a b c : β„€) : a + (b + c) = b + (a + c) := by rw [← Int.add_assoc, Int.add_comm a, Int.add_assoc] protected lemma add_left_cancel {a b c : β„€} (h : a + b = a + c) : b = c := by have h₁ : -a + (a + b) = -a + (a + c) := by rw [h] rwa [← Int.add_assoc, ← Int.add_assoc, Int.add_left_neg, Int.zero_add, Int.zero_add] at h₁ protected lemma neg_add {a b : β„€} : - (a + b) = -a + -b := by have h₁ : - (a + b) = -(a + b) + (a + b) + -a + -b := by rw [Int.add_assoc, Int.add_comm (-a), Int.add_assoc, Int.add_assoc, ← Int.add_assoc b] rw [Int.add_right_neg, Int.zero_add, Int.add_right_neg, Int.add_zero] rwa [Int.add_left_neg, Int.zero_add] at h₁ lemma negSucc_ofNat_coe' (n : β„•) : -[1+ n] = -↑n - 1 := by rw [Int.sub_eq_add_neg, ← Int.neg_add]; rfl protected lemma coe_nat_sub {n m : β„•} : n ≀ m β†’ (↑(m - n) : β„€) = ↑m - ↑n := ofNat_sub protected lemma subNatNat_eq_coe {m n : β„•} : subNatNat m n = ↑m - ↑n := by refine subNatNat_elim m n (fun m n i => i = ↑m - ↑n) ?p ?n case p => intros i n simp only [Int.ofNat_add, Int.add_left_comm, Int.add_assoc, Int.add_right_neg, Int.sub_eq_add_neg] rfl case n => intros i n simp only [negSucc_ofNat_coe, ofNat_add, Int.sub_eq_add_neg, Int.neg_add, ← Int.add_assoc] rw [← @Int.sub_eq_add_neg n, ← ofNat_sub, Nat.sub_self, ofNat_zero, Int.zero_add] apply Nat.le_refl theorem toNat_sub (m n : β„•) : toNat (m - n : β„•) = m - n := rfl protected lemma one_mul : βˆ€ (a : β„€), (1 : β„€) * a = a | (ofNat n) => show ofNat (1 * n) = ofNat n by rw [Nat.one_mul] | -[1+ n] => show -[1+ (1 * n)] = -[1+ n] by rw [Nat.one_mul] protected lemma mul_one (a : β„€) : a * 1 = a := by rw [Int.mul_comm, Int.one_mul] protected lemma neg_eq_neg_one_mul : βˆ€ a : β„€, -a = -1 * a | (ofNat 0) => rfl | (ofNat (n+1)) => show _ = -[1+ (1*n)+0] by rw [Nat.one_mul] rfl | -[1+ n] => show _ = ofNat _ by rw [Nat.one_mul] rfl theorem sign_mul_natAbs : βˆ€ (a : β„€), sign a * natAbs a = a | (n+1:β„•) => Int.one_mul _ | 0 => rfl | -[1+ n] => (Int.neg_eq_neg_one_mul _).symm end Int
2d07590216e887d1b8f0478a80e66d101988f6e8
a7602958ab456501ff85db8cf5553f7bcab201d7
/Notes/Logic_and_Proof/Chapter9/9.12.lean
cdc7ee16aaf1bfce212abd5a67d75106027c1811
[]
no_license
enlauren/math-logic
081e2e737c8afb28dbb337968df95ead47321ba0
086b6935543d1841f1db92d0e49add1124054c37
refs/heads/master
1,594,506,621,950
1,558,634,976,000
1,558,634,976,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
600
lean
-- 9.1 ...continued -- Actually, some of the terms and formulas from the last program are built-in -- Lean under the `init.data.nat` library(?). import init.data.nat open nat -- Now we have basic infix operations available to us. constant square: β„• -> β„• constant add: β„• -> β„• -> β„• constant even: β„• -> Prop constant odd: β„• -> Prop constant lt: β„• -> β„• -> Prop variables w x y z: β„• #check even(square y) /\ odd(square(z)) -- Note that parens are optional in these cases. #check even (add x y) \/ even (x * y) #check x < y /\ even x #check (lt x y) /\ even x -> odd(square(z) + y)
7c06c8448e72c9c17ecf2d619fb264d91608bd67
7cef822f3b952965621309e88eadf618da0c8ae9
/src/topology/sheaves/presheaf_of_functions.lean
68023f648e0b943de2e49d767381217cb4a6c2d8
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
3,671
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import topology.sheaves.presheaf import topology.category.TopCommRing import category_theory.yoneda import ring_theory.subring import topology.algebra.continuous_functions universes v u open category_theory open topological_space open opposite namespace Top variables (X : Top.{v}) /-- The presheaf of continuous functions on `X` with values in fixed target topological space `T`. -/ def presheaf_to_Top (T : Top.{v}) : X.presheaf (Type v) := (opens.to_Top X).op β‹™ (yoneda.obj T) /-- The (bundled) commutative ring of continuous functions from a topological space to a topological commutative ring, with pointwise multiplication. -/ -- TODO upgrade the result to TopCommRing? def continuous_functions (X : Top.{v}α΅’α΅–) (R : TopCommRing.{v}) : CommRing.{v} := { Ξ± := unop X ⟢ (forgetβ‚‚ TopCommRing Top).obj R, str := _root_.continuous_comm_ring } namespace continuous_functions @[simp] lemma one (X : Top.{v}α΅’α΅–) (R : TopCommRing.{v}) (x) : (monoid.one β†₯(continuous_functions X R)).val x = 1 := rfl @[simp] lemma zero (X : Top.{v}α΅’α΅–) (R : TopCommRing.{v}) (x) : (comm_ring.zero β†₯(continuous_functions X R)).val x = 0 := rfl @[simp] lemma add (X : Top.{v}α΅’α΅–) (R : TopCommRing.{v}) (f g : continuous_functions X R) (x) : (comm_ring.add f g).val x = f.1 x + g.1 x := rfl @[simp] lemma mul (X : Top.{v}α΅’α΅–) (R : TopCommRing.{v}) (f g : continuous_functions X R) (x) : (ring.mul f g).val x = f.1 x * g.1 x := rfl /-- Pulling back functions into a topological ring along a continuous map is a ring homomorphism. -/ def pullback {X Y : Topα΅’α΅–} (f : X ⟢ Y) (R : TopCommRing) : continuous_functions X R ⟢ continuous_functions Y R := { to_fun := Ξ» g, f.unop ≫ g, map_one' := rfl, map_zero' := rfl, map_add' := by tidy, map_mul' := by tidy } local attribute [ext] subtype.eq /-- A homomorphism of topological rings can be postcomposed with functions from a source space `X`; this is a ring homomorphism (with respect to the pointwise ring operations on functions). -/ def map (X : Topα΅’α΅–) {R S : TopCommRing} (Ο† : R ⟢ S) : continuous_functions X R ⟢ continuous_functions X S := { to_fun := Ξ» g, g ≫ ((forgetβ‚‚ TopCommRing Top).map Ο†), map_one' := by ext; exact Ο†.1.map_one, map_zero' := by ext; exact Ο†.1.map_zero, map_add' := by intros; ext; apply Ο†.1.map_add, map_mul' := by intros; ext; apply Ο†.1.map_mul } end continuous_functions /-- An upgraded version of the Yoneda embedding, observing that the continuous maps from `X : Top` to `R : TopCommRing` form a commutative ring, functorial in both `X` and `R`. -/ def CommRing_yoneda : TopCommRing.{u} β₯€ (Top.{u}α΅’α΅– β₯€ CommRing.{u}) := { obj := Ξ» R, { obj := Ξ» X, continuous_functions X R, map := Ξ» X Y f, continuous_functions.pullback f R }, map := Ξ» R S Ο†, { app := Ξ» X, continuous_functions.map X Ο† } } /-- The presheaf (of commutative rings), consisting of functions on an open set `U βŠ† X` with values in some topological commutative ring `T`. -/ def presheaf_to_TopCommRing (T : TopCommRing.{v}) : X.presheaf CommRing.{v} := (opens.to_Top X).op β‹™ (CommRing_yoneda.obj T) /-- The presheaf (of commutative rings) of real valued functions. -/ noncomputable def presheaf_ℝ (Y : Top) : Y.presheaf CommRing := presheaf_to_TopCommRing Y (TopCommRing.of ℝ) /-- The presheaf (of commutative rings) of complex valued functions. -/ noncomputable def presheaf_β„‚ (Y : Top) : Y.presheaf CommRing := presheaf_to_TopCommRing Y (TopCommRing.of β„‚) end Top
5f94f517c0baa9aab7b3a1114e3f328d227aaa8b
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/slim_check.lean
d714a1ca82b20ddc5b8e52be37cb8c2734cdbd05
[ "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
6,476
lean
import tactic.slim_check import .mk_slim_check_test example : true := begin have : βˆ€ i j : β„•, i < j β†’ j < i, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! i := 0 j := 1 guard: 0 < 1 (by construction) issue: 1 < 0 does not hold (0 shrinks) ------------------- ", admit, trivial end example : true := begin have : (βˆ€ x : β„•, 2 ∣ x β†’ x < 100), success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 104 issue: 104 < 100 does not hold (2 shrinks) ------------------- ", admit, trivial end example (xs : list β„•) (w : βˆƒ x ∈ xs, x < 3) : true := begin have : βˆ€ y ∈ xs, y < 5, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! xs := [5, 5, 0, 1] x := 0 y := 5 issue: 5 < 5 does not hold (5 shrinks) ------------------- ", admit, trivial end example (x : β„•) (h : 2 ∣ x) : true := begin have : x < 100, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 104 issue: 104 < 100 does not hold (2 shrinks) ------------------- ", admit, trivial end example (Ξ± : Type) (xs ys : list Ξ±) : true := begin have : xs ++ ys = ys ++ xs, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! Ξ± := β„€ xs := [0] ys := [1] issue: [0, 1] = [1, 0] does not hold (4 shrinks) ------------------- ", admit, trivial end example : true := begin have : βˆ€ x ∈ [1,2,3], x < 4, slim_check { random_seed := some 257, quiet := tt }, -- success trivial, end open function slim_check example (f : β„€ β†’ β„€) (h : injective f) : true := begin have : monotone (f ∘ small.mk), success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [2 ↦ 3, 3 ↦ 9, 4 ↦ 6, 5 ↦ 4, 6 ↦ 2, 8 ↦ 5, 9 ↦ 8, x ↦ x] x := 3 y := 4 guard: 3 ≀ 4 (by construction) issue: 9 ≀ 6 does not hold (5 shrinks) ------------------- ", admit, trivial, end example (f : β„€ β†’ β„€) (h : injective f) (g : β„€ β†’ β„€) (h : injective g) (i) : true := begin have : f i = g i, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [x ↦ x] g := [1 ↦ 2, 2 ↦ 1, x ↦ x] i := 1 issue: 1 = 2 does not hold (5 shrinks) ------------------- ", admit, trivial, end example (f : β„€ β†’ β„€) (h : injective f) : true := begin have : monotone f, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [2 ↦ 3, 3 ↦ 9, 4 ↦ 6, 5 ↦ 4, 6 ↦ 2, 8 ↦ 5, 9 ↦ 8, x ↦ x] x := 3 y := 4 guard: 3 ≀ 4 (by construction) issue: 9 ≀ 6 does not hold (5 shrinks) ------------------- ", admit, trivial, end example (f : β„€ β†’ β„€) : true := begin have : injective f, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [_ ↦ 0] x := 0 y := -1 guard: 0 = 0 issue: 0 = -1 does not hold (0 shrinks) ------------------- ", admit, trivial, end example (f : β„€ β†’ β„€) : true := begin have : monotone f, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! f := [-6 ↦ 97, 0 ↦ 0, _ ↦ 4] x := -6 y := -2 guard: -6 ≀ -2 (by construction) issue: 97 ≀ 4 does not hold (5 shrinks) ------------------- ", admit, trivial, end example (xs ys : list β„€) (h : xs ~ ys) : true := begin have : list.qsort (Ξ» x y, x β‰  y) xs = list.qsort (Ξ» x y, x β‰  y) ys, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! xs := [0, 1] ys := [1, 0] guard: [0, 1] ~ [1, 0] (by construction) issue: [0, 1] = [1, 0] does not hold (4 shrinks) ------------------- ", admit, trivial end example (x y : β„•) : true := begin have : y ≀ x β†’ x + y < 100, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 59 y := 41 guard: 41 ≀ 59 (by construction) issue: 100 < 100 does not hold (8 shrinks) ------------------- ", admit, trivial, end example (x : β„€) : true := begin have : x ≀ 3 β†’ 3 ≀ x, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 2 guard: 2 ≀ 3 (by construction) issue: 3 ≀ 2 does not hold (1 shrinks) ------------------- ", admit, trivial, end example (x y : β„€) : true := begin have : y ≀ x β†’ x + y < 100, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := 52 y := 52 guard: 52 ≀ 52 (by construction) issue: 104 < 100 does not hold (4 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin have : x ∨ y β†’ y ∧ x, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := tt y := ff guard: (true ∨ false) issue: false does not hold (0 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin have : (Β¬x ↔ y) β†’ y ∧ x, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := tt y := ff guard: (Β¬ true ↔ false) issue: false does not hold (0 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin -- deterministic have : (x ↔ y) β†’ y ∨ x, success_if_fail_with_msg { slim_check } " =================== Found problems! x := ff y := ff guard: (false ↔ false) issue: false does not hold issue: false does not hold (0 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin -- deterministic have : y ∨ x, success_if_fail_with_msg { slim_check } " =================== Found problems! x := ff y := ff issue: false does not hold issue: false does not hold (0 shrinks) ------------------- ", admit, trivial, end example (x y : Prop) : true := begin have : x ↔ y, success_if_fail_with_msg { slim_check { random_seed := some 257 } } " =================== Found problems! x := tt y := ff issue: false does not hold issue: Β¬ true does not hold (0 shrinks) ------------------- ", admit, trivial, end
06c813c1e16da1ccd78696a96e8893ae2867bff6
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/pnat/prime.lean
9cd88e8eb748d0c8361b4f971e9e3e510d4eba84
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
6,953
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Neil Strickland -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.prime import Mathlib.data.pnat.basic import Mathlib.PostPort namespace Mathlib /-! # Primality and GCD on pnat This file extends the theory of `β„•+` with `gcd`, `lcm` and `prime` functions, analogous to those on `nat`. -/ namespace nat.primes protected instance coe_pnat : has_coe primes β„•+ := has_coe.mk fun (p : primes) => { val := ↑p, property := sorry } theorem coe_pnat_nat (p : primes) : ↑↑p = ↑p := rfl theorem coe_pnat_inj (p : primes) (q : primes) : ↑p = ↑q β†’ p = q := sorry end nat.primes namespace pnat /-- The greatest common divisor (gcd) of two positive natural numbers, viewed as positive natural number. -/ def gcd (n : β„•+) (m : β„•+) : β„•+ := { val := nat.gcd ↑n ↑m, property := sorry } /-- The least common multiple (lcm) of two positive natural numbers, viewed as positive natural number. -/ def lcm (n : β„•+) (m : β„•+) : β„•+ := { val := nat.lcm ↑n ↑m, property := sorry } @[simp] theorem gcd_coe (n : β„•+) (m : β„•+) : ↑(gcd n m) = nat.gcd ↑n ↑m := rfl @[simp] theorem lcm_coe (n : β„•+) (m : β„•+) : ↑(lcm n m) = nat.lcm ↑n ↑m := rfl theorem gcd_dvd_left (n : β„•+) (m : β„•+) : gcd n m ∣ n := iff.mpr dvd_iff (nat.gcd_dvd_left ↑n ↑m) theorem gcd_dvd_right (n : β„•+) (m : β„•+) : gcd n m ∣ m := iff.mpr dvd_iff (nat.gcd_dvd_right ↑n ↑m) theorem dvd_gcd {m : β„•+} {n : β„•+} {k : β„•+} (hm : k ∣ m) (hn : k ∣ n) : k ∣ gcd m n := iff.mpr dvd_iff (nat.dvd_gcd (iff.mp dvd_iff hm) (iff.mp dvd_iff hn)) theorem dvd_lcm_left (n : β„•+) (m : β„•+) : n ∣ lcm n m := iff.mpr dvd_iff (nat.dvd_lcm_left ↑n ↑m) theorem dvd_lcm_right (n : β„•+) (m : β„•+) : m ∣ lcm n m := iff.mpr dvd_iff (nat.dvd_lcm_right ↑n ↑m) theorem lcm_dvd {m : β„•+} {n : β„•+} {k : β„•+} (hm : m ∣ k) (hn : n ∣ k) : lcm m n ∣ k := iff.mpr dvd_iff (nat.lcm_dvd (iff.mp dvd_iff hm) (iff.mp dvd_iff hn)) theorem gcd_mul_lcm (n : β„•+) (m : β„•+) : gcd n m * lcm n m = n * m := subtype.eq (nat.gcd_mul_lcm ↑n ↑m) theorem eq_one_of_lt_two {n : β„•+} : n < bit0 1 β†’ n = 1 := fun (h : n < bit0 1) => le_antisymm (id (fun (h : n < 1 + 1) => eq.mp (Eq._oldrec (Eq.refl (n < 1 + 1)) (propext lt_add_one_iff)) h) h) (one_le n) /-! ### Prime numbers -/ /-- Primality predicate for `β„•+`, defined in terms of `nat.prime`. -/ def prime (p : β„•+) := nat.prime ↑p theorem prime.one_lt {p : β„•+} : prime p β†’ 1 < p := nat.prime.one_lt theorem prime_two : prime (bit0 1) := nat.prime_two theorem dvd_prime {p : β„•+} {m : β„•+} (pp : prime p) : m ∣ p ↔ m = 1 ∨ m = p := sorry theorem prime.ne_one {p : β„•+} : prime p β†’ p β‰  1 := sorry @[simp] theorem not_prime_one : Β¬prime 1 := nat.not_prime_one theorem prime.not_dvd_one {p : β„•+} : prime p β†’ Β¬p ∣ 1 := fun (pp : prime p) => eq.mpr (id (Eq._oldrec (Eq.refl (Β¬p ∣ 1)) (propext dvd_iff))) (nat.prime.not_dvd_one pp) theorem exists_prime_and_dvd {n : β„•+} : bit0 1 ≀ n β†’ βˆƒ (p : β„•+), prime p ∧ p ∣ n := sorry /-! ### Coprime numbers and gcd -/ /-- Two pnats are coprime if their gcd is 1. -/ def coprime (m : β„•+) (n : β„•+) := gcd m n = 1 @[simp] theorem coprime_coe {m : β„•+} {n : β„•+} : nat.coprime ↑m ↑n ↔ coprime m n := sorry theorem coprime.mul {k : β„•+} {m : β„•+} {n : β„•+} : coprime m k β†’ coprime n k β†’ coprime (m * n) k := sorry theorem coprime.mul_right {k : β„•+} {m : β„•+} {n : β„•+} : coprime k m β†’ coprime k n β†’ coprime k (m * n) := sorry theorem gcd_comm {m : β„•+} {n : β„•+} : gcd m n = gcd n m := sorry theorem gcd_eq_left_iff_dvd {m : β„•+} {n : β„•+} : m ∣ n ↔ gcd m n = m := sorry theorem gcd_eq_right_iff_dvd {m : β„•+} {n : β„•+} : m ∣ n ↔ gcd n m = m := eq.mpr (id (Eq._oldrec (Eq.refl (m ∣ n ↔ gcd n m = m)) gcd_comm)) gcd_eq_left_iff_dvd theorem coprime.gcd_mul_left_cancel (m : β„•+) {n : β„•+} {k : β„•+} : coprime k n β†’ gcd (k * m) n = gcd m n := sorry theorem coprime.gcd_mul_right_cancel (m : β„•+) {n : β„•+} {k : β„•+} : coprime k n β†’ gcd (m * k) n = gcd m n := eq.mpr (id (Eq._oldrec (Eq.refl (coprime k n β†’ gcd (m * k) n = gcd m n)) (mul_comm m k))) (coprime.gcd_mul_left_cancel m) theorem coprime.gcd_mul_left_cancel_right (m : β„•+) {n : β„•+} {k : β„•+} : coprime k m β†’ gcd m (k * n) = gcd m n := sorry theorem coprime.gcd_mul_right_cancel_right (m : β„•+) {n : β„•+} {k : β„•+} : coprime k m β†’ gcd m (n * k) = gcd m n := eq.mpr (id (Eq._oldrec (Eq.refl (coprime k m β†’ gcd m (n * k) = gcd m n)) (mul_comm n k))) (coprime.gcd_mul_left_cancel_right m) @[simp] theorem one_gcd {n : β„•+} : gcd 1 n = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (gcd 1 n = 1)) (Eq.symm (propext gcd_eq_left_iff_dvd)))) (one_dvd n) @[simp] theorem gcd_one {n : β„•+} : gcd n 1 = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (gcd n 1 = 1)) gcd_comm)) one_gcd theorem coprime.symm {m : β„•+} {n : β„•+} : coprime m n β†’ coprime n m := eq.mpr (id (imp_congr_eq (coprime.equations._eqn_1 m n) (coprime.equations._eqn_1 n m))) (eq.mpr (id (Eq._oldrec (Eq.refl (gcd m n = 1 β†’ gcd n m = 1)) gcd_comm)) (eq.mpr (id (propext imp_self)) trivial)) @[simp] theorem one_coprime {n : β„•+} : coprime 1 n := one_gcd @[simp] theorem coprime_one {n : β„•+} : coprime n 1 := coprime.symm one_coprime theorem coprime.coprime_dvd_left {m : β„•+} {k : β„•+} {n : β„•+} : m ∣ k β†’ coprime k n β†’ coprime m n := sorry theorem coprime.factor_eq_gcd_left {a : β„•+} {b : β„•+} {m : β„•+} {n : β„•+} (cop : coprime m n) (am : a ∣ m) (bn : b ∣ n) : a = gcd (a * b) m := sorry theorem coprime.factor_eq_gcd_right {a : β„•+} {b : β„•+} {m : β„•+} {n : β„•+} (cop : coprime m n) (am : a ∣ m) (bn : b ∣ n) : a = gcd (b * a) m := eq.mpr (id (Eq._oldrec (Eq.refl (a = gcd (b * a) m)) (mul_comm b a))) (coprime.factor_eq_gcd_left cop am bn) theorem coprime.factor_eq_gcd_left_right {a : β„•+} {b : β„•+} {m : β„•+} {n : β„•+} (cop : coprime m n) (am : a ∣ m) (bn : b ∣ n) : a = gcd m (a * b) := eq.mpr (id (Eq._oldrec (Eq.refl (a = gcd m (a * b))) gcd_comm)) (coprime.factor_eq_gcd_left cop am bn) theorem coprime.factor_eq_gcd_right_right {a : β„•+} {b : β„•+} {m : β„•+} {n : β„•+} (cop : coprime m n) (am : a ∣ m) (bn : b ∣ n) : a = gcd m (b * a) := eq.mpr (id (Eq._oldrec (Eq.refl (a = gcd m (b * a))) gcd_comm)) (coprime.factor_eq_gcd_right cop am bn) theorem coprime.gcd_mul (k : β„•+) {m : β„•+} {n : β„•+} (h : coprime m n) : gcd k (m * n) = gcd k m * gcd k n := sorry theorem gcd_eq_left {m : β„•+} {n : β„•+} : m ∣ n β†’ gcd m n = m := sorry theorem coprime.pow {m : β„•+} {n : β„•+} (k : β„•) (l : β„•) (h : coprime m n) : coprime (m ^ k) (n ^ l) := sorry
5f9e8307ac1423c8f7bc6a0a49ba7a0fc851aa74
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/doc_string7b.lean
88ad5e324d976cc1067d092cf8ab28527ca9c335
[ "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
232
lean
import .doc_string7a open tactic #eval do ds ← olean_doc_strings, let ds := (do ⟨some fn, d⟩ ← ds | [], guard $ fn.backn 17 = "doc_string7a.lean", d), trace ds, guard $ ds = [(⟨1, 0⟩, "a"), (⟨2,0⟩, "b")]
282531805aa852665ae83aded5d3accbb75df878
93c4ed09750233a3b114256e099d6a03939794b8
/chapter2.lean
8d87389785c3ceb1cc8cf54244a1bed02d7d86b0
[]
no_license
OwenGraves/TaoAnalysis
1177c5772f3736b007f458b8817f6a25b13b5e77
695b149b5decdb6f8e46c883ea112f0a12082548
refs/heads/master
1,644,242,117,512
1,564,441,707,000
1,564,441,707,000
98,614,833
0
0
null
null
null
null
UTF-8
Lean
false
false
20,417
lean
-- Tao's Analysis I Chapter 2 Results -- The aim of this project is to adhere as strictly as reasonable -- to the textbook, sometimes even at the cost of clarity. -- However, as a result, this project comes close to serving -- as a fully formalized solutions manual for the textbook. namespace chapter2 open nat open classical -- Predefined Lean library theorems may appear in other places, -- but are mainly used only to prove the basic axioms. -- Since in Lean the natural numbers are defined inductively, -- the "Peano Axioms" will be results instead of "axioms". local postfix `++` := succ -- 0 is a natural number theorem ax2_1 : β„• := 0 -- If n is a natural number, then n++ is a natural number theorem ax2_2 (n : β„•) : β„• := n++ theorem p2_1_4 : β„• := 3 example : β„• := ax2_2 $ ax2_2 $ ax2_2 ax2_1 -- 0 is not the succesor of any natural number theorem ax2_3 : βˆ€ {n : β„•}, n++ β‰  0 := succ_ne_zero theorem p2_1_6 : 4 β‰  0 := ax2_3 theorem ax2_4 {n m : β„•} : n++ = m++ β†’ n = m := assume h : n++ = m++, nat.no_confusion h id theorem p2_1_8 : 6 β‰  2 := not.intro (assume h : 6 = 2, have hβ‚‚ : 5 = 1, from ax2_4 h, have h₃ : 4 = 0, from ax2_4 hβ‚‚, show false, from p2_1_6 h₃) -- Note that Lean has induction built into inductive datatypes -- The "inductive" tactic will be used most frequently theorem ax2_5 (P : β„• β†’ Prop) (hβ‚€ : P 0) (ih : βˆ€ n, P n β†’ P (n++)) : βˆ€ n, P n := assume n : β„•, @nat.rec_on P n hβ‚€ ih theorem p2_1_16 (fβ‚™ : β„• β†’ β„•) (c : β„•) : β„• β†’ β„• | 0 := c | (a++) := fβ‚™ a -- Once all relevant properties have been proved, -- Lean's natural numbers and "add" will be used instead def d2_2_1 : β„• β†’ β„• β†’ β„• | 0 m := m | (n++) m := (d2_2_1 n m)++ def tempadd := d2_2_1 local infix `+` := tempadd lemma l2_2_2 : βˆ€ {n : β„•}, n + 0 = n := begin intro n, apply nat.rec_on n, reflexivity, intro n, intro ih, exact calc n++ + 0 = (n+0)++ : rfl ... = n++ : congr_arg nat.succ ih end lemma l2_2_3 : βˆ€ {n m}, n + (m++) = (n + m)++ := begin intros n m, apply nat.rec_on n, show 0 + (m++) = (0 + m)++, from calc 0 + (m++) = m++ : rfl ... = (0 + m)++ : rfl, intro n, intro ih, show (n++) + (m++) = ((n++) + m)++, from calc (n++) + (m++) = (n + (m++))++ : rfl ... = ((n + m)++)++ : congr_arg nat.succ ih ... = ((n++) + m)++ : rfl end -- See nat.add_comm theorem p2_2_4 : βˆ€ {n m}, n + m = m + n := begin intros n m, apply nat.rec_on n, show 0 + m = m + 0, from calc 0 + m = m : rfl ... = m + 0 : eq.symm $ l2_2_2, intro n, intro ih, show (n++) + m = m + (n++), from calc (n++) + m = (n + m)++ : rfl ... = (m + n)++ : congr_arg _ ih ... = m + (n++) : eq.symm $ l2_2_3 end -- See nat.add_assoc theorem p2_2_5 : βˆ€ {a b c}, (a + b) + c = a + (b + c) := begin intros a b c, apply nat.rec_on c, show (a + b) + 0 = a + (b + 0), from calc (a + b) + 0 = a + b : l2_2_2 ... = a + (b + 0) : congr_arg _ (eq.symm l2_2_2), intro c, intro ih, show (a + b) + (c++) = a + (b + (c++)), from calc (a + b) + (c++) = ((a + b) + c)++ : l2_2_3 ... = (a + (b + c))++ : congr_arg _ ih ... = a + ((b + c)++) : eq.symm l2_2_3 ... = a + (b + (c++)) : congr_arg _ (eq.symm l2_2_3) end theorem p2_2_6 : βˆ€ {a b c}, a + b = a + c β†’ b = c := begin intros a b c, induction a with a ih, intro, exact calc b = 0 + b : rfl ... = 0 + c : by assumption ... = c : rfl, intro, have : (a + b)++ = (a + c)++, from calc (a + b)++ = (a++) + b : rfl ... = (a++) + c : by assumption ... = (a + c)++ : rfl, have : a + b = a + c, from ax2_4 this, show b = c, from ih this end def d2_2_7 (n : β„•) : Prop := n β‰  0 def pos := d2_2_7 -- extra lemma zero_neq_one : pos 1 := begin end theorem p2_2_8 : βˆ€ a b, pos a β†’ pos (a + b) := begin intros a b pa, induction b with b ih, have : a + 0 = a, from l2_2_2, exact eq.substr this pa, have h : a + (b++) = (a + b)++, from l2_2_3, have : pos ((a + b)++), from ax2_3, exact eq.substr h this end theorem c2_2_9 : βˆ€ {a b}, a + b = 0 β†’ a = 0 ∧ b = 0 := begin intros a b h, constructor, apply by_contradiction, intro na, have : pos a, from na, have : pos (a + b), from p2_2_8 a b this, exact this h, apply by_contradiction, intro nb, have : pos b, from nb, have : pos (b + a), from p2_2_8 b a this, have : pos (a + b), from eq.subst (@p2_2_4 b a) this, exact this h end lemma l2_2_10 : βˆ€ {a}, pos a β†’ βˆƒ! b, b++ = a := begin intros a pa, induction a with a ih, have : Β¬pos 0, from not.intro (assume h : 0 β‰  0, h (@rfl β„• 0)), exact false.elim (this pa), apply exists_unique.intro a, reflexivity, intro y, exact ax2_4 end section ordering_properties -- See ge (β‰₯) and less_than_or_equal -- Lean uses alternate definitions based on inductive types -- We make use of the predefined ordering typeclass for convenience def d2_2_11a (n m : β„•) : Prop := βˆƒ a : β„•, n = m + a def ge := d2_2_11a def le (n m : β„•) := ge m n instance : has_le β„• := ⟨le⟩ -- See gt (>) def d2_2_11b (n m : β„•) : Prop := (d2_2_11a n m) ∧ n β‰  m def gt := d2_2_11b def lt (n m : β„•) := gt m n instance : has_lt β„• := ⟨lt⟩ variables {a b c : β„•} theorem p2_2_12a : a β‰₯ a := begin apply exists.intro 0, exact eq.symm l2_2_2 end theorem p2_2_12b : a β‰₯ b ∧ b β‰₯ c β†’ a β‰₯ c := begin intro h, cases h with ageb bgec, have ab : βˆƒ m, a = b + m, from ageb, have bc : βˆƒ n, b = c + n, from bgec, clear ageb bgec, change (βˆƒ k, a = c + k), cases ab with m h₁, cases bc with n hβ‚‚, apply exists.intro, change a = c + (n + m), exact calc a = b + m : h₁ ... = c + n + m : congr_arg _ hβ‚‚ ... = c + (n + m) : p2_2_5 end theorem p2_2_12c : a β‰₯ b ∧ b β‰₯ a β†’ a = b := begin intro h, cases h with aa bb, have ab : βˆƒ m, a = b + m, from aa, have ba : βˆƒ n, b = a + n, from bb, clear aa bb, cases ab with m h₁, cases ba with n hβ‚‚, have : a = a + (n + m), from calc a = b + m : h₁ ... = a + n + m : congr_arg _ hβ‚‚ ... = a + (n + m) : p2_2_5, have : a + 0 = a + (n + m), rw l2_2_2, assumption, have : 0 = n + m, from p2_2_6 this, have : n = 0 ∧ m = 0, from c2_2_9 (eq.symm this), cases this with n0 m0, exact calc a = b + m : h₁ ... = b + 0 : by rw m0 ... = b : l2_2_2 end theorem p2_2_12d : a β‰₯ b ↔ a + c β‰₯ b + c := begin constructor, intro, have ab : βˆƒ m, a = b + m, assumption, cases ab with m h, apply exists.intro m, show a + c = b + c + m, from calc a + c = a + c : rfl ... = b + m + c : by rw h ... = b + (m + c) : by rw p2_2_5 ... = b + (c + m) : by rw (@p2_2_4 c m) ... = b + c + m : by rw ← p2_2_5, intro, have acbc : βˆƒ m, a + c = b + c + m, assumption, cases acbc with m h, constructor, change a = b + m, have : a + c = c + (b + m), rw [h, (@p2_2_4 b c), p2_2_5], have : c + a = c + (b + m), rw p2_2_4, assumption, show a = b + m, from p2_2_6 this end theorem p2_2_12e : a < b ↔ a++ ≀ b := begin constructor, intro, have ab : (βˆƒ m, b = a + m) ∧ b β‰  a, assumption, cases ab with em ne, cases em with m ba, have mn0 : m β‰  0, assume : m = 0, have : b = a, rw [ba, this, l2_2_2], from ne this, have : βˆƒ! d, d++ = m, from l2_2_10 mn0, cases this with d h₁, cases h₁ with hβ‚‚ h₃, constructor, show b = (a++) + d, from calc b = a + m : ba ... = a + (d++) : by rw hβ‚‚ ... = (a + d)++ : by rw l2_2_3 ... = (a++) + d : rfl, intro, have ab : βˆƒ m, b = (a++) + m, assumption, constructor, change βˆƒ d, b = a + d, cases ab with m ab, constructor, show b = a + (m++), from calc b = (a++) + m : ab ... = (a + m)++ : rfl ... = a + (m++) : by rw ← l2_2_3, cases ab with m ab, assume : b = a, have : b = a + (m++), from calc b = (a++) + m : ab ... = (a + m)++ : rfl ... = a + (m++) : by rw ← l2_2_3, have : a + (m++) = b, from eq.symm this, have : a + (m++) = a + 0, rw [this, β€Ήb = aβ€Ί, l2_2_2], have : (m++) = 0, from p2_2_6 this, exact ax2_3 this end theorem p2_2_12f : a < b ↔ βˆƒ d, b = a + d ∧ pos d := begin constructor, intro ab, cases ab with ab ne, have ab : βˆƒ d, b = a + d, assumption, cases ab with d ab, constructor, constructor, exact ab, assume : d = 0, have : b = a, rw [ab, this, l2_2_2], contradiction, intro h, cases h with d h, cases h with ba pd, constructor, constructor, exact ba, assume : b = a, have ba : b + 0 = a + d, rw [ba, l2_2_2], have ba : a + d = b + 0, from eq.symm ba, have : a + d = a + 0, rw [ba, this], have : d = 0, from p2_2_6 this, contradiction end end ordering_properties section order_trichotomy variables {a b : β„•} lemma p2_2_13_l1_l1 : b++ β‰  b := begin induction b with b ih, exact ax2_3, assume : (b++)++ = b++, have : b++ = b, exact ax2_4 this, contradiction end lemma p2_2_13_l1 : (a < b ∨ a = b ∨ a > b) := begin induction a with a ih, have : 0 ≀ b, constructor, change b = 0 + b, reflexivity, apply by_cases, assume (eq : b = 0), right, left, exact eq.symm, assume (ne : b β‰  0), have : 0 < b, constructor, assumption, assumption, left, assumption, cases ih with ablt ih, have : a++ ≀ b, from p2_2_12e.mp ablt, apply by_cases, assume (eq : b = a++), right, left, exact eq.symm, assume (ne : b β‰  a++), have : a++ < b, constructor, assumption, assumption, left, assumption, cases ih with abeq abgt, right, right, constructor, constructor, show a++ = b + (0++), rw [← abeq, l2_2_3, l2_2_2], rw abeq, exact p2_2_13_l1_l1, right, right, cases abgt with abge ne, cases abge with m ab, constructor, constructor, change a++ = b + (m++), rw [ab, ← l2_2_3], assume : a++ = b, rw [← @l2_2_2 b, ab, ← l2_2_3] at this, have : (m++) = 0, from p2_2_6 this, apply ax2_3, exact this end lemma p2_2_13_l2 : Β¬(a = b ∧ a > b) := begin apply not.intro, intro h, cases h with eq gt, cases gt with ge ne, contradiction end lemma p2_2_13_l3 : Β¬(a = b ∧ a < b) := begin apply not.intro, intro h, cases h with eq lt, cases lt with le ne, have : b = a, from eq.symm, contradiction end lemma p2_2_13_l4 : Β¬(a > b ∧ a < b) := begin apply not.intro, intro h, cases h with gt lt, cases gt with ge ne, cases lt with le ne, have : a = b, from p2_2_12c ⟨ge, le⟩, contradiction end theorem p2_2_13 : (a < b ∨ a = b ∨ a > b) ∧ Β¬(a = b ∧ a > b) ∧ Β¬(a = b ∧ a < b) ∧ Β¬(a > b ∧ a < b) := ⟨p2_2_13_l1, p2_2_13_l2, p2_2_13_l3, p2_2_13_l4⟩ end order_trichotomy section strong_induction lemma strong_ind_simp_l1 : βˆ€ {m n : β„•}, m < n++ β†’ m ≀ n := begin intros m n h, have : (m++) ≀ (n++), from p2_2_12e.mp h, cases this with k hβ‚‚, constructor, have : n++ = (m + k)++, assumption, show n = m + k, from ax2_4 this end lemma strong_ind_simp_l2 : βˆ€ {a b : β„•}, a ≀ b β†’ a < b ∨ a = b := begin intros a b ab, cases ab with k ab, apply by_cases, assume : k = 0, rw [β€Ήk = 0β€Ί, l2_2_2] at ab, right, exact ab.symm, assume : k β‰  0, left, apply p2_2_12f.mpr, constructor, exact ⟨ab, this⟩ end -- A simpler example that starts at 0 instead of mβ‚€ lemma strong_ind_simple (P : β„• β†’ Prop) (k : β„•) (hh : βˆ€ n, (βˆ€ m, m < n β†’ P m) β†’ P n) : P k := begin let Q : β„• β†’ Prop := Ξ» n, βˆ€ m, m < n β†’ P m, have hq : βˆ€ {n : β„•}, Q n β†’ P n, assumption, have qk : Q k, induction k with k qk, intros n h, cases h with k h, cases k with k hβ‚‚, have : 0 = n, rw ← (c2_2_9 hβ‚‚.symm).left, contradiction, intros m h, have : m < k ∨ m = k, from (strong_ind_simp_l2 (strong_ind_simp_l1 h)), cases this with ltk eqk, apply qk, assumption, subst m, apply hq, assumption, exact hq qk end theorem p2_2_14 (mβ‚€ : β„•) (P : β„• β†’ Prop) (k : β„•) (hh : βˆ€ m, (βˆ€ m', mβ‚€ ≀ m' ∧ m' < m β†’ P m') β†’ P m) : βˆ€ k, k β‰₯ mβ‚€ β†’ P k := begin intros k h, let Q : β„• β†’ Prop := Ξ» m, βˆ€ m', mβ‚€ ≀ m' ∧ m' < m β†’ P m', have hq : βˆ€ {n : β„•}, Q n β†’ P n, assumption, have qk : Q k, induction k with k qk, intros m hand, have : m < 0, from hand.right, cases this with hβ‚‚ ne, cases hβ‚‚ with c hβ‚‚, have : 0 = m, rw ← (c2_2_9 hβ‚‚.symm).left, contradiction, intros m hand, cases hand with hl hr, have : m < k ∨ m = k, from (strong_ind_simp_l2 (strong_ind_simp_l1 hr)), cases this with ltk eqk, apply qk, cases ltk with km ne, have km : k β‰₯ m, from km, exact p2_2_12b ⟨km, hl⟩, exact ⟨hl, ltk⟩, rw eqk, apply hq, apply qk, rw ← eqk, exact hl, exact hq qk end end strong_induction theorem ex2_2_6 (n m : β„•) (P : β„• β†’ Prop) (h : βˆ€ {m}, P (m++) β†’ P m) (hβ‚€ : P n) : m ≀ n β†’ P m := begin intro mn, induction n with n ih, cases mn with k mk, rw (c2_2_9 mk.symm).left, assumption, have : m < n++ ∨ m = n++, from strong_ind_simp_l2 mn, cases this with mlt meq, have : m++ ≀ n++, from p2_2_12e.mp mlt, rw [← (@l2_2_2 m), ← (@l2_2_2 n)] at this, rw [← l2_2_3, ← l2_2_3] at this, have : m ≀ n, from p2_2_12d.mpr this, exact ih (h hβ‚€) this, rw meq, exact hβ‚€ end def d2_3_1 : β„• β†’ β„• β†’ β„• | 0 m := 0 | (n++) m := (d2_3_1 n m) + m def tempmul := d2_3_1 local infix `*` := tempmul lemma l2_3_2_l1 {n : β„•} : n * 0 = 0 := begin induction n with n ih, reflexivity, change (n * 0) + 0 = 0, rw l2_2_2, assumption end lemma l2_3_2_l2_l1 {a b c : β„•} : b = c β†’ a + b = a + c := begin intro h, induction a with a ih, exact h, change (a + b)++ = (a + c)++, rw ih end lemma l2_3_2_l2 {n m : β„•} : n * (m++) = n * m + n := begin induction n with n ih, change 0 = 0 * m + 0, rw l2_2_2, reflexivity, change (n * (m++)) + (m++) = (n * m) + m + (n++), rw ih, rw p2_2_5, rw p2_2_5, rw l2_3_2_l2_l1, rw l2_2_3, rw l2_2_3, rw p2_2_4 end lemma l2_3_2 {n m : β„•} : n * m = m * n := begin induction n with n ih, rw l2_3_2_l1, reflexivity, change (n * m) + m = m * (n++), rw l2_3_2_l2, rw ih end lemma l2_3_3a_l1 {n m : β„•} : (n * m) + m = 0 β†’ m = 0 := begin intro h, induction m with m ih, reflexivity, rw l2_3_2_l2 at h, by_contradiction, have : (m++) = 0, from (c2_2_9 h).right, contradiction end lemma l2_3_3a {n m : β„•} : n * m = 0 ↔ n = 0 ∨ m = 0 := begin constructor, intro h, induction n with n ih, left, reflexivity, unfold tempmul at h, unfold d2_3_1 at h, right, exact l2_3_3a_l1 h, intro h, cases h with h h, rw h, reflexivity, rw h, rw l2_3_2_l1 end lemma l2_3_3b {n m : β„•} : pos n ∧ pos m ↔ pos (n * m) := begin constructor, intro h, cases h with pn pm, assume : n * m = 0, have : n = 0 ∨ m = 0, from l2_3_3a.mp this, cases this, contradiction, contradiction, intro h, constructor, assume : n = 0, rw this at h, change pos 0 at h, exact ne.irrefl h, assume : m = 0, rw this at h, rw l2_3_2_l1 at h, exact ne.irrefl h, end theorem p2_3_4a {a b c : β„•} : a * (b + c) = (a * b) + (a * c) := begin induction c with c ih, rw [l2_2_2, l2_3_2_l1, l2_2_2], rw [l2_2_3, l2_3_2_l2, ih, l2_3_2_l2, p2_2_5] end theorem p2_3_4b {a b c : β„•} : (b + c) * a = (b * a) + (c * a) := begin rw [l2_3_2, @l2_3_2 b a, @l2_3_2 c a], exact p2_3_4a end theorem p2_3_5 {a b c : β„•} : (a * b) * c = a * (b * c) := begin induction b with b ih, rw l2_3_2_l1, change 0 = a * 0, rw l2_3_2_l1, rw [l2_3_2_l2, p2_3_4b], change a * b * c + a * c = a * (b * c + c), rw p2_3_4a, rw ih.symm end theorem p2_3_6 {a b c : β„•} : a < b β†’ pos c β†’ a * c < b * c := begin intros h pc, have, from p2_2_12f.mp h, cases this with d h, cases h with h pd, have : b * c = (a + d) * c, from h β–Έ rfl, rw p2_3_4b at this, have pdc : pos (d * c), from l2_3_3b.mp ⟨pd, pc⟩, apply p2_2_12f.mpr, constructor, exact ⟨this, pdc⟩ end theorem c2_3_7 {a b c : β„•} : a * c = b * c β†’ pos c β†’ a = b := begin intros h pc, have tri : a < b ∨ a = b ∨ a > b, from p2_2_13_l1, cases tri with ablt tri, have : a * c < b * c, from p2_3_6 ablt pc, cases this, have h, from eq.symm h, contradiction, cases tri with abeq abgt, assumption, have : a * c > b * c, from p2_3_6 abgt pc, cases this, contradiction end theorem p2_3_9 {n q : β„•} (pq : pos q) : βˆƒ m r, 0 ≀ r ∧ r < q ∧ n = m * q + r := begin induction n with n ih, repeat {constructor}, contradiction, rw [l2_2_2, l2_2_2], change 0 = 0 * q, reflexivity, cases ih with m ih, cases ih with r ih, cases ih with rβ‚€ ih, cases ih with rq ih, have : r++ ≀ q, from p2_2_12e.mp rq, have : r++ < q ∨ r++ = q, from strong_ind_simp_l2 this, cases this with rlt req, existsi m, existsi (r++), constructor, constructor, reflexivity, constructor, exact rlt, rw [ih, l2_2_3], existsi (m++), existsi 0, constructor, constructor, reflexivity, constructor, constructor, constructor, reflexivity, exact pq, change n++ = m * q + q + 0, rw [ih, l2_2_2, ← l2_2_3, req] end def d2_3_11 : β„• β†’ β„• β†’ β„• | m 0 := 1 | m (n++) := (d2_3_11 m n) * m def tempexp := d2_3_11 local infix `^` := tempexp example {x : β„•} : x ^ 3 = x ^ 2 * x := rfl example {x : β„•} : x ^ 3 = x * x * x := rfl lemma ex2_3_4_l1 {n : β„•} : n + n = 2 * n := rfl theorem ex2_3_4 {a b : β„•} : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := calc (a + b) ^ 2 = (a + b) * (a + b) : rfl ... = (a + b) * a + (a + b) * b : p2_3_4a ... = a * a + b * a + (a + b) * b : by rw p2_3_4b ... = a * a + b * a + (a * b + b * b) : by rw p2_3_4b ... = a * a + b * a + a * b + b * b : by rw ← p2_2_5 ... = a ^ 2 + b * a + a * b + b ^ 2 : rfl ... = a ^ 2 + a * b + a * b + b ^ 2 : by rw l2_3_2 ... = a ^ 2 + (a * b + a * b) + b ^ 2 : by rw ← p2_2_5 ... = a ^ 2 + 2 * (a * b) + b ^ 2 : by rw ex2_3_4_l1 ... = a ^ 2 + 2 * a * b + b ^ 2 : by rw p2_3_5 end chapter2 -- We will now assume and employ theorems regarding -- the natural numbers from Lean's standard library since -- they are no longer the focus of any future chapters.
8db3aebd17a9b0d67562ca50a2da51edfdcb2dbb
7da5ceac20aaab989eeb795a4be9639982e7b35a
/src/category_theory/limits2.lean
1422e616602371960d906480cbf8abb8ff38d982
[ "MIT" ]
permissive
formalabstracts/formalabstracts
46c2f1b3a172e62ca6ffeb46fbbdf1705718af49
b0173da1af45421239d44492eeecd54bf65ee0f6
refs/heads/master
1,606,896,370,374
1,572,988,776,000
1,572,988,776,000
96,763,004
165
28
null
1,555,709,319,000
1,499,680,948,000
Lean
UTF-8
Lean
false
false
5,636
lean
import .basic category_theory.limits.limits universes u u₁ uβ‚‚ v v₁ vβ‚‚ namespace category_theory open function variables {C : Sort u₁} [π’ž : category.{v₁} C] include π’ž lemma injective_hom_op (X Y : C) : injective (@has_hom.hom.op _ _ X Y) := Ξ» f f' hf, by rw [←@has_hom.hom.unop_op _ _ _ _ f, ←@has_hom.hom.unop_op _ _ _ _ f', hf] lemma injective_hom_unop (X Y : Cα΅’α΅–) : injective (@has_hom.hom.unop _ _ X Y) := Ξ» f f' hf, by rw [←@has_hom.hom.op_unop _ _ _ _ f, ←@has_hom.hom.op_unop _ _ _ _ f', hf] end category_theory open category_theory namespace category_theory.nat_trans variables {C : Sort u₁} [π’ž : category.{v₁} C] {D : Sort uβ‚‚} [π’Ÿ : category.{vβ‚‚} D] include π’ž π’Ÿ protected def unop' (F F' : C β₯€ D) (Ξ· : F.op ⟢ F'.op) : F' ⟢ F := { app := Ξ» x, (Ξ·.app $ op x).unop, naturality' := by { intros, apply injective_hom_op, have := Ξ·.naturality f.op, simp at this, simp [this] } } protected def op' (F F' : Cα΅’α΅– β₯€ Dα΅’α΅–) (Ξ· : F.unop ⟢ F'.unop) : F' ⟢ F := { app := Ξ» x, (Ξ·.app $ unop x).op, naturality' := by { intros, apply injective_hom_unop, have := Ξ·.naturality f.unop, simp at this, simp [this] } } end category_theory.nat_trans open category_theory namespace category_theory.nat_iso variables {C : Sort u₁} [π’ž : category.{v₁} C] {D : Sort uβ‚‚} [π’Ÿ : category.{vβ‚‚} D] include π’ž π’Ÿ protected def op_unop (F : C β₯€ D) : F.op.unop β‰… F := by { cases F, refl } -- maybe not the best definition protected def unop_op (F : Cα΅’α΅– β₯€ Dα΅’α΅–) : F.unop.op β‰… F := by { cases F, refl } -- maybe not the best definition protected def op_functor_const (d : D) : ((category_theory.functor.const C).obj d).op β‰… (category_theory.functor.const Cα΅’α΅–).obj (op d) := by refl end category_theory.nat_iso open category_theory namespace category_theory.limits variables {J : Type v} [small_category J] variables {C : Sort u} [category.{v+1} C] -- protected def cocone.op {F : J β₯€ C} (s : cocone F) : cone F.op := -- ⟨op s.X, s.ΞΉ.op⟩ -- protected def cone.op {F : J β₯€ C} (s : cone F) : cocone F.op := -- ⟨op s.X, s.Ο€.op⟩ -- protected def cocone.unop {F : Jα΅’α΅– β₯€ Cα΅’α΅–} (s : cocone F) : cone F.unop := -- ⟨unop s.X, s.ΞΉ.unop⟩ -- protected def cone.unop {F : Jα΅’α΅– β₯€ Cα΅’α΅–} (s : cone F) : cocone F.unop := -- ⟨unop s.X, s.Ο€.unop⟩ -- protected def cocone.op' {F : Jα΅’α΅– β₯€ Cα΅’α΅–} (s : cocone F.unop) : cone F := -- ⟨op s.X, s.ΞΉ.op' F ((category_theory.functor.const Jα΅’α΅–).obj $ op s.X)⟩ -- protected def cone.op' {F : Jα΅’α΅– β₯€ Cα΅’α΅–} (s : cone F.unop) : cocone F := -- ⟨op s.X, s.Ο€.op' ((category_theory.functor.const Jα΅’α΅–).obj $ op s.X) F⟩ -- protected def cocone.unop' {F : J β₯€ C} (s : cocone F.op) : cone F := -- ⟨unop s.X, s.ΞΉ.unop' F ((category_theory.functor.const J).obj $ unop s.X)⟩ -- protected def cone.unop' {F : J β₯€ C} (s : cone F.op) : cocone F := -- ⟨unop s.X, s.Ο€.unop' ((category_theory.functor.const J).obj $ unop s.X) F⟩ -- def has_limit_op {F : J β₯€ C} (H : has_colimit F) : has_limit F.op := -- { cone := H.cocone.op, -- is_limit := -- { lift := Ξ» s, (H.is_colimit.desc s.unop').op, -- fac' := omitted, -- uniq' := omitted } } -- def has_colimit_op {F : J β₯€ C} (H : has_limit F) : has_colimit F.op := -- { cocone := H.cone.op, -- is_colimit := -- { desc := Ξ» s, (H.is_limit.lift s.unop').op, -- fac' := omitted, -- uniq' := omitted } } -- -- def has_limit_op {F : J β₯€ C} (H : has_colimit F) : has_limit F.op := -- -- { cone := H.cocone.op, -- -- is_limit := -- -- { lift := Ξ» s, begin unfreezeI, cases F, exact (H.is_colimit.desc s.unop).op end, -- -- fac' := omitted, -- -- uniq' := omitted } } -- -- def has_colimit_op {F : J β₯€ C} (H : has_limit F) : has_colimit F.op := -- -- { cocone := H.cone.op, -- -- is_colimit := -- -- { desc := Ξ» s, begin unfreezeI, cases F, exact (H.is_limit.lift s.unop).op end, -- -- fac' := omitted, -- -- uniq' := omitted } } -- def has_limit_unop {F : Jα΅’α΅– β₯€ Cα΅’α΅–} (H : has_colimit F) : has_limit F.unop := -- { cone := H.cocone.unop, -- is_limit := -- { lift := Ξ» s, (H.is_colimit.desc s.op').unop, -- fac' := omitted, -- uniq' := omitted } } -- def has_colimit_unop {F : Jα΅’α΅– β₯€ Cα΅’α΅–} (H : has_limit F) : has_colimit F.unop := -- { cocone := H.cone.unop, -- is_colimit := -- { desc := Ξ» s, (H.is_limit.lift s.op').unop, -- fac' := omitted, -- uniq' := omitted } } -- def has_limit_op' {F : Jα΅’α΅– β₯€ Cα΅’α΅–} (H : has_colimit F.unop) : has_limit F := -- { cone := H.cocone.op', -- is_limit := -- { lift := Ξ» s, (H.is_colimit.desc s.unop).op, -- fac' := omitted, -- uniq' := omitted } } -- def has_colimit_op' {F : Jα΅’α΅– β₯€ Cα΅’α΅–} (H : has_limit F.unop) : has_colimit F := -- { cocone := H.cone.op', -- is_colimit := -- { desc := Ξ» s, (H.is_limit.lift s.unop).op, -- fac' := omitted, -- uniq' := omitted } } -- def has_limit_unop' {F : J β₯€ C} (H : has_colimit F.op) : has_limit F := -- { cone := H.cocone.unop', -- is_limit := -- { lift := Ξ» s, (H.is_colimit.desc s.op).unop, -- fac' := omitted, -- uniq' := omitted } } -- def has_colimit_unop' {F : J β₯€ C} (H : has_limit F.op) : has_colimit F := -- { cocone := H.cone.unop', -- is_colimit := -- { desc := Ξ» s, (H.is_limit.lift s.op).unop, -- fac' := omitted, -- uniq' := omitted } } -- def has_limits_of_shape_op (H : has_colimits_of_shape J C) : has_limits_of_shape Jα΅’α΅– Cα΅’α΅– := -- Ξ» F, has_limit_op' (H _) end category_theory.limits
1c167340514a25f37eb12f43c7421ab45d5a195e
bb31430994044506fa42fd667e2d556327e18dfe
/src/data/int/order/basic.lean
94841ad2c129768e615e1c44824a8345dbb4ef84
[ "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
25,500
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import data.int.basic import data.int.cast.basic import algebra.ring.divisibility import algebra.order.group.abs import algebra.order.ring.char_zero import tactic.assert_exists /-! # Order instances on the integers > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains: * instances on `β„€`. The stronger one is `int.linear_ordered_comm_ring`. * basic lemmas about integers that involve order properties. ## Recursors * `int.rec`: Sign disjunction. Something is true/defined on `β„€` if it's true/defined for nonnegative and for negative values. (Defined in core Lean 3) * `int.induction_on`: Simple growing induction on positive numbers, plus simple decreasing induction on negative numbers. Note that this recursor is currently only `Prop`-valued. * `int.induction_on'`: Simple growing induction for numbers greater than `b`, plus simple decreasing induction on numbers less than `b`. -/ open nat namespace int instance : linear_ordered_comm_ring β„€ := { add_le_add_left := @int.add_le_add_left, mul_pos := @int.mul_pos, zero_le_one := le_of_lt int.zero_lt_one, .. int.comm_ring, .. int.linear_order, .. int.nontrivial } /-! ### Extra instances to short-circuit type class resolution -/ instance : ordered_comm_ring β„€ := strict_ordered_comm_ring.to_ordered_comm_ring' instance : ordered_ring β„€ := strict_ordered_ring.to_ordered_ring' instance : linear_ordered_add_comm_group β„€ := by apply_instance end int namespace int theorem abs_eq_nat_abs : βˆ€ a : β„€, |a| = nat_abs a | (n : β„•) := abs_of_nonneg $ coe_zero_le _ | -[1+ n] := abs_of_nonpos $ le_of_lt $ neg_succ_lt_zero _ @[simp, norm_cast] lemma coe_nat_abs (n : β„€) : (n.nat_abs : β„€) = |n| := n.abs_eq_nat_abs.symm lemma _root_.nat.cast_nat_abs {Ξ± : Type*} [add_group_with_one Ξ±] (n : β„€) : (n.nat_abs : Ξ±) = ↑|n| := by rw [←int.coe_nat_abs, int.cast_coe_nat] theorem nat_abs_abs (a : β„€) : nat_abs (|a|) = nat_abs a := by rw [abs_eq_nat_abs]; refl theorem sign_mul_abs (a : β„€) : sign a * |a| = a := by rw [abs_eq_nat_abs, sign_mul_nat_abs] theorem coe_nat_eq_zero {n : β„•} : (n : β„€) = 0 ↔ n = 0 := nat.cast_eq_zero theorem coe_nat_ne_zero {n : β„•} : (n : β„€) β‰  0 ↔ n β‰  0 := by simp lemma coe_nat_ne_zero_iff_pos {n : β„•} : (n : β„€) β‰  0 ↔ 0 < n := ⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h), Ξ» h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩ @[norm_cast] lemma abs_coe_nat (n : β„•) : |(n : β„€)| = n := abs_of_nonneg (coe_nat_nonneg n) /-! ### succ and pred -/ theorem lt_succ_self (a : β„€) : a < succ a := lt_add_of_pos_right _ zero_lt_one theorem pred_self_lt (a : β„€) : pred a < a := sub_lt_self _ zero_lt_one theorem lt_add_one_iff {a b : β„€} : a < b + 1 ↔ a ≀ b := add_le_add_iff_right _ lemma le_add_one {a b : β„€} (h : a ≀ b) : a ≀ b + 1 := le_of_lt (int.lt_add_one_iff.mpr h) theorem sub_one_lt_iff {a b : β„€} : a - 1 < b ↔ a ≀ b := sub_lt_iff_lt_add.trans lt_add_one_iff theorem le_sub_one_iff {a b : β„€} : a ≀ b - 1 ↔ a < b := le_sub_iff_add_le @[simp] lemma abs_lt_one_iff {a : β„€} : |a| < 1 ↔ a = 0 := ⟨λ a0, let ⟨hn, hp⟩ := abs_lt.mp a0 in (le_of_lt_add_one (by exact hp)).antisymm hn, Ξ» a0, (abs_eq_zero.mpr a0).le.trans_lt zero_lt_one⟩ lemma abs_le_one_iff {a : β„€} : |a| ≀ 1 ↔ a = 0 ∨ a = 1 ∨ a = -1 := by rw [le_iff_lt_or_eq, abs_lt_one_iff, abs_eq (zero_le_one' β„€)] lemma one_le_abs {z : β„€} (hβ‚€: z β‰  0) : 1 ≀ |z| := add_one_le_iff.mpr (abs_pos.mpr hβ‚€) /-- Inductively define a function on `β„€` by defining it at `b`, for the `succ` of a number greater than `b`, and the `pred` of a number less than `b`. -/ @[elab_as_eliminator] protected def induction_on' {C : β„€ β†’ Sort*} (z : β„€) (b : β„€) (H0 : C b) (Hs : βˆ€ k, b ≀ k β†’ C k β†’ C (k + 1)) (Hp : βˆ€ k ≀ b, C k β†’ C (k - 1)) : C z := begin -- Note that we use `convert` here where possible as we are constructing data, and this reduces -- the number of times `eq.mpr` appears in the term. rw ←sub_add_cancel z b, induction (z - b) with n n, { induction n with n ih, { convert H0 using 1, rw [of_nat_zero, zero_add] }, convert Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih using 1, rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc] }, { induction n with n ih, { convert Hp _ le_rfl H0 using 1, rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub] }, { convert Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) le_rfl)) ih using 1, rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub] } } end /-- See `int.induction_on'` for an induction in both directions. -/ protected lemma le_induction {P : β„€ β†’ Prop} {m : β„€} (h0 : P m) (h1 : βˆ€ (n : β„€), m ≀ n β†’ P n β†’ P (n + 1)) (n : β„€) : m ≀ n β†’ P n := begin apply int.induction_on' n m, { intro _, exact h0, }, { intros k hle hi _, exact h1 k hle (hi hle), }, { intros _ hle _ hle', exfalso, exact lt_irrefl k (le_sub_one_iff.mp (hle.trans hle')), }, end /-- See `int.induction_on'` for an induction in both directions. -/ protected lemma le_induction_down {P : β„€ β†’ Prop} {m : β„€} (h0 : P m) (h1 : βˆ€ (n : β„€), n ≀ m β†’ P n β†’ P (n - 1)) (n : β„€) : n ≀ m β†’ P n := begin apply int.induction_on' n m, { intro _, exact h0, }, { intros _ hle _ hle', exfalso, exact lt_irrefl k (add_one_le_iff.mp (hle'.trans hle)), }, { intros k hle hi _, exact h1 k hle (hi hle), }, end /-! ### nat abs -/ variables {a b : β„€} {n : β„•} attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one @[simp] lemma nat_abs_dvd_iff_dvd {a b : β„€} : a.nat_abs ∣ b.nat_abs ↔ a ∣ b := begin refine ⟨_, Ξ» ⟨k, hk⟩, ⟨k.nat_abs, hk.symm β–Έ nat_abs_mul a k⟩⟩, rintro ⟨k, hk⟩, rw [←nat_abs_of_nat k, ←nat_abs_mul, nat_abs_eq_nat_abs_iff, neg_mul_eq_mul_neg] at hk, cases hk; exact ⟨_, hk⟩ end /-! ### `/` -/ protected theorem div_nonpos {a b : β„€} (Ha : 0 ≀ a) (Hb : b ≀ 0) : a / b ≀ 0 := nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb) theorem div_eq_zero_of_lt_abs {a b : β„€} (H1 : 0 ≀ a) (H2 : a < |b|) : a / b = 0 := match b, |b|, abs_eq_nat_abs b, H2 with | (n : β„•), ._, rfl, H2 := div_eq_zero_of_lt H1 H2 | -[1+ n], ._, rfl, H2 := neg_injective $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2 end protected theorem add_mul_div_right (a b : β„€) {c : β„€} (H : c β‰  0) : (a + b * c) / c = a / c + b := have βˆ€ {k n : β„•} {a : β„€}, (a + n * k.succ) / k.succ = a / k.succ + n, from Ξ» k n a, match a with | (m : β„•) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos | -[1+ m] := show ((n * k.succ:β„•) - m.succ : β„€) / k.succ = n - (m / k.succ + 1 : β„•), begin cases lt_or_ge m (n*k.succ) with h h, { rw [← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.div_lt_iff_lt_mul k.succ_pos).2 h)], apply congr_arg of_nat, rw [mul_comm, nat.mul_sub_div], rwa mul_comm }, { change (↑(n * nat.succ k) - (m + 1) : β„€) / ↑(nat.succ k) = ↑n - ((m / nat.succ k : β„•) + 1), rw [← sub_sub, ← sub_sub, ← neg_sub (m:β„€), ← neg_sub _ (n:β„€), ← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.le_div_iff_mul_le k.succ_pos).2 h), ← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'], { apply congr_arg neg_succ_of_nat, rw [mul_comm, nat.sub_mul_div], rwa mul_comm } } end end, have βˆ€ {a b c : β„€}, 0 < c β†’ (a + b * c) / c = a / c + b, from Ξ» a b c H, match c, eq_succ_of_zero_lt H, b with | ._, ⟨k, rfl⟩, (n : β„•) := this | ._, ⟨k, rfl⟩, -[1+ n] := show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from eq_sub_of_add_eq $ by rw [← this, sub_add_cancel] end, match lt_trichotomy c 0 with | or.inl hlt := neg_inj.1 $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg]; apply this (neg_pos_of_neg hlt) | or.inr (or.inl heq) := absurd heq H | or.inr (or.inr hgt) := this hgt end protected theorem add_mul_div_left (a : β„€) {b : β„€} (c : β„€) (H : b β‰  0) : (a + b * c) / b = a / b + c := by rw [mul_comm, int.add_mul_div_right _ _ H] @[simp] protected theorem mul_div_cancel (a : β„€) {b : β„€} (H : b β‰  0) : a * b / b = a := by have := int.add_mul_div_right 0 a H; rwa [zero_add, int.zero_div, zero_add] at this @[simp] protected theorem mul_div_cancel_left {a : β„€} (b : β„€) (H : a β‰  0) : a * b / a = b := by rw [mul_comm, int.mul_div_cancel _ H] @[simp] protected theorem div_self {a : β„€} (H : a β‰  0) : a / a = 1 := by have := int.mul_div_cancel 1 H; rwa one_mul at this local attribute [simp] int.zero_div int.div_zero protected theorem add_div_of_dvd_right {a b c : β„€} (H : c ∣ b) : (a + b) / c = a / c + b / c := begin by_cases h1 : c = 0, { simp [h1] }, cases H with k hk, rw hk, change c β‰  0 at h1, rw [mul_comm c k, int.add_mul_div_right _ _ h1, ←zero_add (k * c), int.add_mul_div_right _ _ h1, int.zero_div, zero_add] end protected theorem add_div_of_dvd_left {a b c : β„€} (H : c ∣ a) : (a + b) / c = a / c + b / c := by rw [add_comm, int.add_div_of_dvd_right H, add_comm] /-! ### mod -/ @[simp] theorem mod_abs (a b : β„€) : a % (|b|) = a % b := abs_by_cases (Ξ» i, a % i = a % b) rfl (mod_neg _ _) theorem mod_nonneg : βˆ€ (a : β„€) {b : β„€}, b β‰  0 β†’ 0 ≀ a % b | (m : β„•) n H := coe_zero_le _ | -[1+ m] n H := sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H) theorem mod_lt_of_pos (a : β„€) {b : β„€} (H : 0 < b) : a % b < b := match a, b, eq_succ_of_zero_lt H with | (m : β„•), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _)) | -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _) end theorem mod_lt (a : β„€) {b : β„€} (H : b β‰  0) : a % b < |b| := by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos.2 H) @[simp] theorem add_mul_mod_self {a b c : β„€} : (a + b * c) % c = a % c := if cz : c = 0 then by rw [cz, mul_zero, add_zero] else by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz, mul_add, mul_comm, add_sub_add_right_eq_sub] @[simp] theorem add_mul_mod_self_left (a b c : β„€) : (a + b * c) % b = a % b := by rw [mul_comm, add_mul_mod_self] @[simp] theorem add_mod_self {a b : β„€} : (a + b) % b = a % b := by have := add_mul_mod_self_left a b 1; rwa mul_one at this @[simp] theorem add_mod_self_left {a b : β„€} : (a + b) % a = b % a := by rw [add_comm, add_mod_self] @[simp] theorem mod_add_mod (m n k : β„€) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : β„€) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] lemma add_mod (a b n : β„€) : (a + b) % n = ((a % n) + (b % n)) % n := by rw [add_mod_mod, mod_add_mod] theorem add_mod_eq_add_mod_right {m n k : β„€} (i : β„€) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : β„€} (i : β„€) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] theorem mod_add_cancel_right {m n k : β„€} (i) : (m + i) % n = (k + i) % n ↔ m % n = k % n := ⟨λ H, by have := add_mod_eq_add_mod_right (-i) H; rwa [add_neg_cancel_right, add_neg_cancel_right] at this, add_mod_eq_add_mod_right _⟩ theorem mod_add_cancel_left {m n k i : β„€} : (i + m) % n = (i + k) % n ↔ m % n = k % n := by rw [add_comm, add_comm i, mod_add_cancel_right] theorem mod_sub_cancel_right {m n k : β„€} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n := mod_add_cancel_right _ @[simp] theorem mul_mod_left (a b : β„€) : (a * b) % b = 0 := by rw [← zero_add (a * b), add_mul_mod_self, zero_mod] @[simp] theorem mul_mod_right (a b : β„€) : (a * b) % a = 0 := by rw [mul_comm, mul_mod_left] lemma mul_mod (a b n : β„€) : (a * b) % n = ((a % n) * (b % n)) % n := begin conv_lhs { rw [←mod_add_div a n, ←mod_add_div' b n, right_distrib, left_distrib, left_distrib, mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left, ← mul_assoc, add_mul_mod_self] } end local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_self {a : β„€} : a % a = 0 := by have := mul_mod_left 1 a; rwa one_mul at this @[simp] theorem mod_mod_of_dvd (n : β„€) {m k : β„€} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end @[simp] theorem mod_mod (a b : β„€) : a % b % b = a % b := by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]} lemma sub_mod (a b n : β„€) : (a - b) % n = ((a % n) - (b % n)) % n := begin apply (mod_add_cancel_right b).mp, rw [sub_add_cancel, ← add_mod_mod, sub_add_cancel, mod_mod] end protected theorem div_mod_unique {a b r q : β„€} (h : 0 < b) : a / b = q ∧ a % b = r ↔ r + b * q = a ∧ 0 ≀ r ∧ r < b := begin split, { rintro ⟨rfl, rfl⟩, exact ⟨mod_add_div a b, mod_nonneg _ h.ne.symm, mod_lt_of_pos _ h⟩, }, { rintro ⟨rfl, hz, hb⟩, split, { rw [int.add_mul_div_left r q (ne_of_gt h), div_eq_zero_of_lt hz hb], simp, }, { rw [add_mul_mod_self_left, mod_eq_of_lt hz hb] } }, end local attribute [simp] int.zero_mod theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : β„€} : m % n = k % n ↔ (m - k) % n = 0 := (mod_sub_cancel_right k).symm.trans $ by simp @[simp] lemma neg_mod_two (i : β„€) : (-i) % 2 = i % 2 := begin apply int.mod_eq_mod_iff_mod_sub_eq_zero.mpr, convert int.mul_mod_right 2 (-i), simp only [two_mul, sub_eq_add_neg] end /-! ### properties of `/` and `%` -/ theorem lt_div_add_one_mul_self (a : β„€) {b : β„€} (H : 0 < b) : a < (a / b + 1) * b := by { rw [add_mul, one_mul, mul_comm, ← sub_lt_iff_lt_add', ← mod_def], exact mod_lt_of_pos _ H } theorem abs_div_le_abs : βˆ€ (a b : β„€), |a / b| ≀ |a| := suffices βˆ€ (a : β„€) (n : β„•), |a / n| ≀ |a|, from Ξ» a b, match b, eq_coe_or_neg b with | ._, ⟨n, or.inl rfl⟩ := this _ _ | ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this end, Ξ» a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact coe_nat_le_coe_nat_of_le (match a, n with | (m : β„•), n := nat.div_le_self _ _ | -[1+ m], 0 := nat.zero_le _ | -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _) end) theorem div_le_self {a : β„€} (b : β„€) (Ha : 0 ≀ a) : a / b ≀ a := by have := le_trans (le_abs_self _) (abs_div_le_abs a b); rwa [abs_of_nonneg Ha] at this lemma mod_two_eq_zero_or_one (n : β„€) : n % 2 = 0 ∨ n % 2 = 1 := have h : n % 2 < 2 := abs_of_nonneg (show 0 ≀ (2 : β„€), from dec_trivial) β–Έ int.mod_lt _ dec_trivial, have h₁ : 0 ≀ n % 2 := int.mod_nonneg _ dec_trivial, match (n % 2), h, h₁ with | (0 : β„•) := Ξ» _ _, or.inl rfl | (1 : β„•) := Ξ» _ _, or.inr rfl | (k + 2 : β„•) := Ξ» h _, absurd h dec_trivial | -[1+ a] := Ξ» _ h₁, absurd h₁ dec_trivial end /-! ### dvd -/ theorem dvd_of_mod_eq_zero {a b : β„€} (H : b % a = 0) : a ∣ b := ⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩ theorem mod_eq_zero_of_dvd : βˆ€ {a b : β„€}, a ∣ b β†’ b % a = 0 | a ._ ⟨c, rfl⟩ := mul_mod_right _ _ theorem dvd_iff_mod_eq_zero (a b : β„€) : a ∣ b ↔ b % a = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ /-- If `a % b = c` then `b` divides `a - c`. -/ lemma dvd_sub_of_mod_eq {a b c : β„€} (h : a % b = c) : b ∣ a - c := begin have hx : a % b % b = c % b, { rw h }, rw [mod_mod, ←mod_sub_cancel_right c, sub_self, zero_mod] at hx, exact dvd_of_mod_eq_zero hx end theorem nat_abs_dvd {a b : β„€} : (a.nat_abs : β„€) ∣ b ↔ a ∣ b := (nat_abs_eq a).elim (Ξ» e, by rw ← e) (Ξ» e, by rw [← neg_dvd, ← e]) theorem dvd_nat_abs {a b : β„€} : a ∣ b.nat_abs ↔ a ∣ b := (nat_abs_eq b).elim (Ξ» e, by rw ← e) (Ξ» e, by rw [← dvd_neg, ← e]) instance decidable_dvd : @decidable_rel β„€ (∣) := assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem div_mul_cancel {a b : β„€} (H : b ∣ a) : a / b * b = a := div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H) protected theorem mul_div_cancel' {a b : β„€} (H : a ∣ b) : a * (b / a) = b := by rw [mul_comm, int.div_mul_cancel H] theorem div_dvd_div : βˆ€ {a b c : β„€} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a | a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az]; apply dvd_mul_right protected theorem eq_mul_of_div_eq_right {a b c : β„€} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, int.mul_div_cancel' H1] protected theorem div_eq_of_eq_mul_right {a b c : β„€} (H1 : b β‰  0) (H2 : a = b * c) : a / b = c := by rw [H2, int.mul_div_cancel_left _ H1] protected theorem eq_div_of_mul_eq_right {a b c : β„€} (H1 : a β‰  0) (H2 : a * b = c) : b = c / a := eq.symm $ int.div_eq_of_eq_mul_right H1 H2.symm protected theorem div_eq_iff_eq_mul_right {a b c : β„€} (H : b β‰  0) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : β„€} (H : b β‰  0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : β„€} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2] protected theorem div_eq_of_eq_mul_left {a b c : β„€} (H1 : b β‰  0) (H2 : a = c * b) : a / b = c := int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2]) protected lemma eq_zero_of_div_eq_zero {d n : β„€} (h : d ∣ n) (H : n / d = 0) : n = 0 := by rw [← int.mul_div_cancel' h, H, mul_zero] @[simp] protected lemma div_left_inj {a b d : β„€} (hda : d ∣ a) (hdb : d ∣ b) : a / d = b / d ↔ a = b := begin refine ⟨λ h, _, congr_arg _⟩, rw [←int.mul_div_cancel' hda, ←int.mul_div_cancel' hdb, h], end lemma abs_sign_of_nonzero {z : β„€} (hz : z β‰  0) : |z.sign| = 1 := by rw [abs_eq_nat_abs, nat_abs_sign_of_nonzero hz, int.coe_nat_one] /-- If `n > 0` then `m` is not divisible by `n` iff it is between `n * k` and `n * (k + 1)` for some `k`. -/ lemma exists_lt_and_lt_iff_not_dvd (m : β„€) {n : β„€} (hn : 0 < n) : (βˆƒ k, n * k < m ∧ m < n * (k + 1)) ↔ Β¬ n ∣ m := begin split, { rintro ⟨k, h1k, h2k⟩ ⟨l, rfl⟩, rw [mul_lt_mul_left hn] at h1k h2k, rw [lt_add_one_iff, ← not_lt] at h2k, exact h2k h1k }, { intro h, rw [dvd_iff_mod_eq_zero, ← ne.def] at h, have := (mod_nonneg m hn.ne.symm).lt_of_ne h.symm, simp only [← mod_add_div m n] {single_pass := tt}, refine ⟨m / n, lt_add_of_pos_left _ this, _⟩, rw [add_comm _ (1 : β„€), left_distrib, mul_one], exact add_lt_add_right (mod_lt_of_pos _ hn) _ } end local attribute [simp] int.div_zero protected theorem mul_div_assoc (a : β„€) : βˆ€ {b c : β„€}, c ∣ b β†’ (a * b) / c = a * (b / c) | ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz] protected theorem mul_div_assoc' (b : β„€) {a c : β„€} (h : c ∣ a) : a * b / c = a / c * b := by rw [mul_comm, int.mul_div_assoc _ h, mul_comm] theorem neg_div_of_dvd : βˆ€ {a b : β„€} (H : b ∣ a), -a / b = -(a / b) | ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz] lemma sub_div_of_dvd (a : β„€) {b c : β„€} (hcb : c ∣ b) : (a - b) / c = a / c - b / c := begin rw [sub_eq_add_neg, sub_eq_add_neg, int.add_div_of_dvd_right ((dvd_neg c b).mpr hcb)], congr, exact neg_div_of_dvd hcb, end lemma sub_div_of_dvd_sub {a b c : β„€} (hcab : c ∣ (a - b)) : (a - b) / c = a / c - b / c := by rw [eq_sub_iff_add_eq, ← int.add_div_of_dvd_left hcab, sub_add_cancel] protected theorem sign_eq_div_abs (a : β„€) : sign a = a / |a| := if az : a = 0 then by simp [az] else (int.div_eq_of_eq_mul_left (mt abs_eq_zero.1 az) (sign_mul_abs _).symm).symm /-! ### `/` and ordering -/ protected theorem div_mul_le (a : β„€) {b : β„€} (H : b β‰  0) : a / b * b ≀ a := le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H protected theorem div_le_of_le_mul {a b c : β„€} (H : 0 < c) (H' : a ≀ b * c) : a / c ≀ b := le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H protected theorem mul_lt_of_lt_div {a b c : β„€} (H : 0 < c) (H3 : a < b / c) : a * c < b := lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3) protected theorem mul_le_of_le_div {a b c : β„€} (H1 : 0 < c) (H2 : a ≀ b / c) : a * c ≀ b := le_trans (mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1)) protected theorem le_div_of_mul_le {a b c : β„€} (H1 : 0 < c) (H2 : a * c ≀ b) : a ≀ b / c := le_of_lt_add_one $ lt_of_mul_lt_mul_right (lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1) protected theorem le_div_iff_mul_le {a b c : β„€} (H : 0 < c) : a ≀ b / c ↔ a * c ≀ b := ⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩ protected theorem div_le_div {a b c : β„€} (H : 0 < c) (H' : a ≀ b) : a / c ≀ b / c := int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H') protected theorem div_lt_of_lt_mul {a b c : β„€} (H : 0 < c) (H' : a < b * c) : a / c < b := lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H') protected theorem lt_mul_of_div_lt {a b c : β„€} (H1 : 0 < c) (H2 : a / c < b) : a < b * c := lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2) protected theorem div_lt_iff_lt_mul {a b c : β„€} (H : 0 < c) : a / c < b ↔ a < b * c := ⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩ protected theorem le_mul_of_div_le {a b c : β„€} (H1 : 0 ≀ b) (H2 : b ∣ a) (H3 : a / b ≀ c) : a ≀ c * b := by rw [← int.div_mul_cancel H2]; exact mul_le_mul_of_nonneg_right H3 H1 protected theorem lt_div_of_mul_lt {a b c : β„€} (H1 : 0 ≀ b) (H2 : b ∣ c) (H3 : a * b < c) : a < c / b := lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3) protected theorem lt_div_iff_mul_lt {a b : β„€} (c : β„€) (H : 0 < c) (H' : c ∣ b) : a < b / c ↔ a * c < b := ⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩ theorem div_pos_of_pos_of_dvd {a b : β„€} (H1 : 0 < a) (H2 : 0 ≀ b) (H3 : b ∣ a) : 0 < a / b := int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul) lemma nat_abs_eq_of_dvd_dvd {s t : β„€} (hst : s ∣ t) (hts : t ∣ s) : nat_abs s = nat_abs t := nat.dvd_antisymm (nat_abs_dvd_iff_dvd.mpr hst) (nat_abs_dvd_iff_dvd.mpr hts) theorem div_eq_div_of_mul_eq_mul {a b c d : β„€} (H2 : d ∣ c) (H3 : b β‰  0) (H4 : d β‰  0) (H5 : a * d = b * c) : a / b = c / d := int.div_eq_of_eq_mul_right H3 $ by rw [← int.mul_div_assoc _ H2]; exact (int.div_eq_of_eq_mul_left H4 H5.symm).symm lemma div_dvd_of_dvd {s t : β„€} (hst : s ∣ t) : (t / s) ∣ t := begin rcases eq_or_ne s 0 with rfl | hs, { simpa using hst }, rcases hst with ⟨c, hc⟩, simp [hc, int.mul_div_cancel_left _ hs], end /-! ### to_nat -/ @[simp] theorem to_nat_le {a : β„€} {n : β„•} : to_nat a ≀ n ↔ a ≀ n := by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff]; exact and_iff_left (coe_zero_le _) @[simp] theorem lt_to_nat {n : β„•} {a : β„€} : n < to_nat a ↔ (n : β„€) < a := le_iff_le_iff_lt_iff_lt.1 to_nat_le @[simp] lemma coe_nat_nonpos_iff {n : β„•} : (n : β„€) ≀ 0 ↔ n = 0 := ⟨ Ξ» h, le_antisymm (int.coe_nat_le.mp (h.trans int.coe_nat_zero.le)) n.zero_le, Ξ» h, (coe_nat_eq_zero.mpr h).le⟩ theorem to_nat_le_to_nat {a b : β„€} (h : a ≀ b) : to_nat a ≀ to_nat b := by rw to_nat_le; exact le_trans h (le_to_nat b) theorem to_nat_lt_to_nat {a b : β„€} (hb : 0 < b) : to_nat a < to_nat b ↔ a < b := ⟨λ h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end, Ξ» h, begin rw lt_to_nat, cases a, exact h, exact hb end⟩ theorem lt_of_to_nat_lt {a b : β„€} (h : to_nat a < to_nat b) : a < b := (to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h @[simp] lemma to_nat_pred_coe_of_pos {i : β„€} (h : 0 < i) : ((i.to_nat - 1 : β„•) : β„€) = i - 1 := by simp [h, le_of_lt h] with push_cast @[simp] lemma to_nat_eq_zero : βˆ€ {n : β„€}, n.to_nat = 0 ↔ n ≀ 0 | (n : β„•) := calc _ ↔ (n = 0) : ⟨(to_nat_coe_nat n).symm.trans, (to_nat_coe_nat n).trans⟩ ... ↔ _ : coe_nat_nonpos_iff.symm | -[1+ n] := show ((-((n : β„€) + 1)).to_nat = 0) ↔ (-(n + 1) : β„€) ≀ 0, from calc _ ↔ true : ⟨λ _, trivial, Ξ» h, to_nat_neg_nat _⟩ ... ↔ _ : ⟨λ h, neg_nonpos_of_nonneg (coe_zero_le _), Ξ» _, trivial⟩ @[simp] lemma to_nat_sub_of_le {a b : β„€} (h : b ≀ a) : (to_nat (a - b) : β„€) = a - b := int.to_nat_of_nonneg (sub_nonneg_of_le h) end int -- We should need only a minimal development of sets in order to get here. assert_not_exists set.range
0c4ac0ba63d9dd23efd3cfcc3d7361b5545fae83
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/complex/module.lean
06be9e28e30ec56e5e86b496bec9e878a939348f
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
843
lean
/- Copyright (c) 2020 Alexander Bentkamp, SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, SΓ©bastien GouΓ«zel -/ import data.complex.basic import ring_theory.algebra /-! This file contains two instance, the fact the β„‚ is an ℝ algebra, and an instance to view any complex vector space as a real vector space -/ noncomputable theory namespace complex instance algebra_over_reals : algebra ℝ β„‚ := (complex.of_real).to_algebra end complex /- Register as an instance (with low priority) the fact that a complex vector space is also a real vector space. -/ instance module.complex_to_real (E : Type*) [add_comm_group E] [module β„‚ E] : module ℝ E := module.restrict_scalars' ℝ β„‚ E attribute [instance, priority 900] module.complex_to_real
451ae1abc6441018eade17337db96bc8370edde0
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/init/nat.lean
584d251ade4d6dc89d9b485da615e39a27582f9a
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,764
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import init.wf init.tactic init.num open eq.ops decidable or notation `β„•` := nat namespace nat /- basic definitions on natural numbers -/ inductive le (a : β„•) : β„• β†’ Prop := | refl : le a a | step : Ξ  {b}, le a b β†’ le a (succ b) infix ≀ := le attribute le.refl [refl] definition lt [reducible] (n m : β„•) := succ n ≀ m definition ge [reducible] (n m : β„•) := m ≀ n definition gt [reducible] (n m : β„•) := succ m ≀ n infix < := lt infix β‰₯ := ge infix > := gt definition pred [unfold 1] (a : nat) : nat := nat.cases_on a zero (Ξ» a₁, a₁) -- add is defined in init.num definition sub (a b : nat) : nat := nat.rec_on b a (Ξ» b₁, pred) definition mul (a b : nat) : nat := nat.rec_on b zero (Ξ» b₁ r, r + a) notation a - b := sub a b notation a * b := mul a b /- properties of β„• -/ protected definition is_inhabited [instance] : inhabited nat := inhabited.mk zero protected definition has_decidable_eq [instance] : βˆ€ x y : nat, decidable (x = y) | has_decidable_eq zero zero := inl rfl | has_decidable_eq (succ x) zero := inr (by contradiction) | has_decidable_eq zero (succ y) := inr (by contradiction) | has_decidable_eq (succ x) (succ y) := match has_decidable_eq x y with | inl xeqy := inl (by rewrite xeqy) | inr xney := inr (Ξ» h : succ x = succ y, by injection h with xeqy; exact absurd xeqy xney) end /- properties of inequality -/ theorem le_of_eq {n m : β„•} (p : n = m) : n ≀ m := p β–Έ !le.refl theorem le_succ (n : β„•) : n ≀ succ n := le.step !le.refl theorem pred_le (n : β„•) : pred n ≀ n := by cases n;repeat constructor theorem le_succ_iff_true [simp] (n : β„•) : n ≀ succ n ↔ true := iff_true_intro (le_succ n) theorem pred_le_iff_true [simp] (n : β„•) : pred n ≀ n ↔ true := iff_true_intro (pred_le n) theorem le.trans [trans] {n m k : β„•} (H1 : n ≀ m) : m ≀ k β†’ n ≀ k := le.rec H1 (Ξ»p H2, le.step) theorem le_succ_of_le {n m : β„•} (H : n ≀ m) : n ≀ succ m := le.trans H !le_succ theorem le_of_succ_le {n m : β„•} (H : succ n ≀ m) : n ≀ m := le.trans !le_succ H theorem le_of_lt {n m : β„•} (H : n < m) : n ≀ m := le_of_succ_le H theorem succ_le_succ {n m : β„•} : n ≀ m β†’ succ n ≀ succ m := le.rec !le.refl (Ξ»a b, le.step) theorem pred_le_pred {n m : β„•} : n ≀ m β†’ pred n ≀ pred m := le.rec !le.refl (nat.rec (Ξ»a b, b) (Ξ»a b c, le.step)) theorem le_of_succ_le_succ {n m : β„•} : succ n ≀ succ m β†’ n ≀ m := pred_le_pred theorem le_succ_of_pred_le {n m : β„•} : pred n ≀ m β†’ n ≀ succ m := nat.cases_on n le.step (Ξ»a, succ_le_succ) theorem not_succ_le_zero (n : β„•) : Β¬succ n ≀ zero := by intro H; cases H theorem succ_le_zero_iff_false (n : β„•) : succ n ≀ zero ↔ false := iff_false_intro !not_succ_le_zero theorem not_succ_le_self : Ξ  {n : β„•}, Β¬succ n ≀ n := nat.rec !not_succ_le_zero (Ξ»a b c, b (le_of_succ_le_succ c)) theorem succ_le_self_iff_false [simp] (n : β„•) : succ n ≀ n ↔ false := iff_false_intro not_succ_le_self theorem zero_le : βˆ€ (n : β„•), 0 ≀ n := nat.rec !le.refl (Ξ»a, le.step) theorem zero_le_iff_true [simp] (n : β„•) : 0 ≀ n ↔ true := iff_true_intro !zero_le theorem lt.step {n m : β„•} : n < m β†’ n < succ m := le.step theorem zero_lt_succ (n : β„•) : 0 < succ n := succ_le_succ !zero_le theorem zero_lt_succ_iff_true [simp] (n : β„•) : 0 < succ n ↔ true := iff_true_intro (zero_lt_succ n) theorem lt.trans [trans] {n m k : β„•} (H1 : n < m) : m < k β†’ n < k := le.trans (le.step H1) theorem lt_of_le_of_lt [trans] {n m k : β„•} (H1 : n ≀ m) : m < k β†’ n < k := le.trans (succ_le_succ H1) theorem lt_of_lt_of_le [trans] {n m k : β„•} : n < m β†’ m ≀ k β†’ n < k := le.trans theorem lt.irrefl (n : β„•) : Β¬n < n := not_succ_le_self theorem lt_self_iff_false [simp] (n : β„•) : n < n ↔ false := iff_false_intro (lt.irrefl n) theorem self_lt_succ (n : β„•) : n < succ n := !le.refl theorem self_lt_succ_iff_true [simp] (n : β„•) : n < succ n ↔ true := iff_true_intro (self_lt_succ n) theorem lt.base (n : β„•) : n < succ n := !le.refl theorem le_lt_antisymm {n m : β„•} (H1 : n ≀ m) (H2 : m < n) : false := !lt.irrefl (lt_of_le_of_lt H1 H2) theorem le.antisymm {n m : β„•} (H1 : n ≀ m) : m ≀ n β†’ n = m := le.cases_on H1 (Ξ»a, rfl) (Ξ»a b c, absurd (lt_of_le_of_lt b c) !lt.irrefl) theorem lt_le_antisymm {n m : β„•} (H1 : n < m) (H2 : m ≀ n) : false := le_lt_antisymm H2 H1 theorem lt.asymm {n m : β„•} (H1 : n < m) : Β¬ m < n := le_lt_antisymm (le_of_lt H1) theorem not_lt_zero (a : β„•) : Β¬ a < zero := !not_succ_le_zero theorem lt_zero_iff_false [simp] (a : β„•) : a < zero ↔ false := iff_false_intro (not_lt_zero a) theorem eq_or_lt_of_le {a b : β„•} (H : a ≀ b) : a = b ∨ a < b := le.cases_on H (inl rfl) (Ξ»n h, inr (succ_le_succ h)) theorem le_of_eq_or_lt {a b : β„•} (H : a = b ∨ a < b) : a ≀ b := or.elim H !le_of_eq !le_of_lt -- less-than is well-founded definition lt.wf [instance] : well_founded lt := well_founded.intro (nat.rec (!acc.intro (Ξ»n H, absurd H (not_lt_zero n))) (Ξ»n IH, !acc.intro (Ξ»m H, elim (eq_or_lt_of_le (le_of_succ_le_succ H)) (Ξ»e, eq.substr e IH) (acc.inv IH)))) definition measure {A : Type} : (A β†’ β„•) β†’ A β†’ A β†’ Prop := inv_image lt definition measure.wf {A : Type} (f : A β†’ β„•) : well_founded (measure f) := inv_image.wf f lt.wf theorem succ_lt_succ {a b : β„•} : a < b β†’ succ a < succ b := succ_le_succ theorem lt_of_succ_lt {a b : β„•} : succ a < b β†’ a < b := le_of_succ_le theorem lt_of_succ_lt_succ {a b : β„•} : succ a < succ b β†’ a < b := le_of_succ_le_succ definition decidable_le [instance] : decidable_rel le := nat.rec (Ξ»m, (decidable.inl !zero_le)) (Ξ»n IH m, !nat.cases_on (decidable.inr (not_succ_le_zero n)) (Ξ»m, decidable.rec (Ξ»H, inl (succ_le_succ H)) (Ξ»H, inr (Ξ»a, H (le_of_succ_le_succ a))) (IH m))) definition decidable_lt [instance] : decidable_rel lt := _ definition decidable_gt [instance] : decidable_rel gt := _ definition decidable_ge [instance] : decidable_rel ge := _ theorem lt_or_ge (a b : β„•) : a < b ∨ a β‰₯ b := nat.rec (inr !zero_le) (Ξ»n, or.rec (Ξ»h, inl (le_succ_of_le h)) (Ξ»h, elim (eq_or_lt_of_le h) (Ξ»e, inl (eq.subst e !le.refl)) inr)) b definition lt_ge_by_cases {a b : β„•} {P : Type} (H1 : a < b β†’ P) (H2 : a β‰₯ b β†’ P) : P := by_cases H1 (Ξ»h, H2 (elim !lt_or_ge (Ξ»a, absurd a h) (Ξ»a, a))) definition lt.by_cases {a b : β„•} {P : Type} (H1 : a < b β†’ P) (H2 : a = b β†’ P) (H3 : b < a β†’ P) : P := lt_ge_by_cases H1 (Ξ»h₁, lt_ge_by_cases H3 (Ξ»hβ‚‚, H2 (le.antisymm hβ‚‚ h₁))) theorem lt.trichotomy (a b : β„•) : a < b ∨ a = b ∨ b < a := lt.by_cases (Ξ»H, inl H) (Ξ»H, inr (inl H)) (Ξ»H, inr (inr H)) theorem eq_or_lt_of_not_lt {a b : β„•} (hnlt : Β¬ a < b) : a = b ∨ b < a := or.rec_on (lt.trichotomy a b) (Ξ» hlt, absurd hlt hnlt) (Ξ» h, h) theorem lt_succ_of_le {a b : β„•} : a ≀ b β†’ a < succ b := succ_le_succ theorem lt_of_succ_le {a b : β„•} (h : succ a ≀ b) : a < b := h theorem succ_le_of_lt {a b : β„•} (h : a < b) : succ a ≀ b := h theorem succ_sub_succ_eq_sub [simp] (a b : β„•) : succ a - succ b = a - b := nat.rec rfl (Ξ» b, congr_arg pred) b theorem sub_eq_succ_sub_succ (a b : β„•) : a - b = succ a - succ b := eq.symm !succ_sub_succ_eq_sub theorem zero_sub_eq_zero [simp] (a : β„•) : zero - a = zero := nat.rec rfl (Ξ» a, congr_arg pred) a theorem zero_eq_zero_sub (a : β„•) : zero = zero - a := eq.symm !zero_sub_eq_zero theorem sub_le (a b : β„•) : a - b ≀ a := nat.rec_on b !le.refl (Ξ» b₁, le.trans !pred_le) theorem sub_le_iff_true [simp] (a b : β„•) : a - b ≀ a ↔ true := iff_true_intro (sub_le a b) theorem sub_lt {a b : β„•} (H1 : zero < a) (H2 : zero < b) : a - b < a := !nat.cases_on (Ξ»h, absurd h !lt.irrefl) (Ξ»a h, succ_le_succ (!nat.cases_on (Ξ»h, absurd h !lt.irrefl) (Ξ»b c, eq.substr !succ_sub_succ_eq_sub !sub_le) H2)) H1 theorem sub_lt_succ (a b : β„•) : a - b < succ a := lt_succ_of_le !sub_le theorem sub_lt_succ_iff_true [simp] (a b : β„•) : a - b < succ a ↔ true := iff_true_intro !sub_lt_succ end nat namespace nat_esimp open nat attribute add mul sub [unfold 2] attribute of_num [unfold 1] end nat_esimp
77b18975a4220bb60558a458b49939da8abca4a8
5a5e1bb8063d7934afac91f30aa17c715821040b
/lean3SOS/src/sos.lean
47e6dddad8fbe684c7e8f3aef6316aede868bf6f
[]
no_license
ramonfmir/leanSOS
1883392d73710db5c6e291a2abd03a6c5b44a42b
14b50713dc887f6d408b7b2bce1f8af5bb619958
refs/heads/main
1,683,348,826,105
1,622,056,982,000
1,622,056,982,000
341,232,766
1
0
null
null
null
null
UTF-8
Lean
false
false
4,348
lean
/- SOS definition and the SOS tactic. -/ import system.io import tactic.ring import data.real.basic import data.mv_polynomial.basic import util.parser lib.poly float.basic open mv_polynomial poly open tactic open lean.parser interactive interactive.types (texpr) meta def execute (input : string) : tactic string := let path := "/Users/ramon/Documents/experiments/leanSOS/lean/scripts/client.py" in unsafe_run_io $ io.cmd { cmd := "python3.9", args := [path, input] } --set_option trace.eqn_compiler.elim_match true set_option eqn_compiler.max_steps 10000 meta def parse_num : option nat β†’ string | (some n) := to_string n | _ := "" meta def parse_sos : expr β†’ string | `(@has_le.le %%Ξ± %%inst %%e₁ %%eβ‚‚) := (parse_sos e₁) ++ "<=" ++ (parse_sos eβ‚‚) | `(%%e₁ + %%eβ‚‚) := "(" ++ (parse_sos e₁) ++ "+" ++ (parse_sos eβ‚‚) ++ ")" --| `(@has_sub.sub %%Ξ± %%inst %%e₁ %%eβ‚‚) := (parse_sos e₁) ++ "-" ++ (parse_sos eβ‚‚) --| `(- %%e) := "-" ++ (parse_sos e) | `(%%e₁ * %%eβ‚‚) := "(" ++ (parse_sos e₁) ++ "*" ++ (parse_sos eβ‚‚) ++ ")" --| `(@has_pow.pow _ _ %%P %%e₁ %%eβ‚‚) := (parse_sos e₁) ++ "^" ++ (parse_sos eβ‚‚) | `(mv_polynomial.C %%e) := (parse_num (expr.to_nat e)) | `(mv_polynomial.X %%e) := "x[" ++ (parse_num (expr.to_nat e)) ++ "]" | e := "" -- Quick and dirty tactic to prove that Q is symmetric. meta def prove_symmetric : tactic unit := do `(matrix.symmetric %%Q) ← target, [i, j] ← tactic.intro_lst [`i, `j], (_, _, _) ← simplify simp_lemmas.mk [] Q {fail_if_unchanged := ff}, `[fin_cases i; fin_cases j; simp [matrix.map, list_to_vector, list_to_monomials, list_to_matrix, list_to_monomial, fin.sum_univ_succ]; ring] -- Quick and dirty tactic to prove that p = xT * Q * x. meta def prove_poly_eq : tactic unit := focus1 $ do `(%%p = matrix.dot_product %%ms (matrix.mul_vec (matrix.to_poly %%Q) %%ms')) ← target, let l := [``matrix.dot_product, ``matrix.mul_vec, ``matrix.to_poly], lemmas ← l.mfoldl simp_lemmas.add_simp simp_lemmas.mk, (new_t, pr, _) ← target >>= simplify lemmas [``ms, ``Q, ``p], replace_target new_t pr, `[simp [matrix.map, list_to_vector, list_to_monomials, list_to_matrix, list_to_monomial, fin.sum_univ_succ]; ring] setup_tactic_parser -- Quick and dirty tactic to prove that Q = L^T * L meta def prove_cholesky (pL : parse texpr) : tactic unit := do `(cholesky_decomposition %%Q %%hQ) ← target, tactic.use [pL], let l := [``matrix.mul, ``matrix.dot_product], lemmas ← l.mfoldl simp_lemmas.add_simp simp_lemmas.mk, (new_t, pr, _) ← target >>= simplify lemmas [``Q], replace_target new_t pr, `[simp [matrix.map, list_to_vector, list_to_monomials, list_to_matrix, list_to_monomial, fin.sum_univ_succ]; ext i j; fin_cases i; fin_cases j; ring] meta def sos_aux (input : expr) : tactic unit := do `(%%q ≀ %%p) ← target, --m ← execute (parse_sos input), let f := "/Users/ramon/Documents/experiments/leanSOS/lean/scripts/temp.txt", buf ← unsafe_run_io (io.fs.read_file f), match (buf.to_string.split_on '\n') with | sQdim::sQ::sms::sLdim::sL::_ := do { -- Parse strings. let n : β„• := parse_dim sQdim, lms ← nat_list_of_lists_from_string sms, lQ ← rat_list_of_lists_from_string sQ, let m : β„• := parse_dim sLdim, lL ← rat_list_of_lists_from_string sL, -- Some defaults. Ξ³ ← to_expr ``(fin %%n), Ξ³i ← to_expr ``(fin.fintype %%n), ΞΌ ← to_expr ``(fin %%m), ΞΌi ← to_expr ``(fin.fintype %%m), R ← to_expr ``(float), Ri ← to_expr ``(float.linear_ordered_comm_ring), -- Monomials and main matrix. ms ← monomials_from_list n lms, QZZ ← matrix_from_list n n lQ, LZZ ← matrix_from_list m n lL, Q ← to_expr ``(matrix.map %%QZZ float.mk), L ← to_expr ``(matrix.map %%LZZ float.mk), -- Apply the main theorem. res ← mk_mapp ``nonneg_of_cholesky [Ξ³, Ξ³i, ΞΌ, ΞΌi, R, Ri, p, ms, Q], interactive.concat_tags $ tactic.apply res -- Prove the three subgoals. --prove_poly_eq, swap, --prove_symmetric, --prove_cholesky ``(%%L) } | _ := tactic.trace "Error" end meta def sos : tactic unit := do t ← target, sos_aux t
39746f5647b5d405f8e0b2e51f48fef920f7ea42
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/linear_algebra/tensor_product.lean
92a1200b937e248e0f14d5cdccb31c41ede75c22
[ "Apache-2.0" ]
permissive
sebzim4500/mathlib
e0b5a63b1655f910dee30badf09bd7e191d3cf30
6997cafbd3a7325af5cb318561768c316ceb7757
refs/heads/master
1,585,549,958,618
1,538,221,723,000
1,538,221,723,000
150,869,076
0
0
Apache-2.0
1,538,229,323,000
1,538,229,323,000
null
UTF-8
Lean
false
false
13,263
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Tensor product of modules over commutative rings. -/ import group_theory.free_abelian_group import linear_algebra.linear_map_module variables {R : Type*} [comm_ring R] variables (M : Type*) (N : Type*) (P : Type*) (Q : Type*) variables [module R M] [module R N] [module R P] [module R Q] include R structure is_bilinear_map {M N P} [module R M] [module R N] [module R P] (f : M β†’ N β†’ P) : Prop := (add_left : βˆ€ x y z, f (x + y) z = f x z + f y z) (add_right : βˆ€ x y z, f x (y + z) = f x y + f x z) (smul_left : βˆ€ r x y, f (r β€’ x) y = r β€’ f x y) (smul_right : βˆ€ r x y, f x (r β€’ y) = r β€’ f x y) namespace is_bilinear_map variables {M N P Q} variables {f : M β†’ N β†’ P} (hf : is_bilinear_map f) include hf theorem zero_left : βˆ€ y, f 0 y = 0 := Ξ» y, calc f 0 y = f (0 + 0) y - f 0 y : by rw [hf.add_left 0 0 y]; simp ... = 0 : by simp theorem zero_right : βˆ€ x, f x 0 = 0 := Ξ» x, calc f x 0 = f x (0 + 0) - f x 0 : by rw [hf.add_right x 0 0]; simp ... = 0 : by simp theorem neg_left : βˆ€ x y, f (-x) y = -f x y := Ξ» x y, by convert hf.smul_left (-1) x y; simp theorem neg_right : βˆ€ x y, f x (-y) = -f x y := Ξ» x y, by convert hf.smul_right (-1) x y; simp theorem linear_left (y : N) : is_linear_map (Ξ» x, f x y) := { add := Ξ» m n, hf.add_left m n y, smul := Ξ» r m, hf.smul_left r m y } theorem linear_right (x : M) : is_linear_map (Ξ» y, f x y) := { add := Ξ» m n, hf.add_right x m n, smul := Ξ» r m, hf.smul_right r x m } theorem comm : is_bilinear_map (Ξ» y x, f x y) := by cases hf; constructor; intros; solve_by_elim section comp variables {g : P β†’ Q} (hg : is_linear_map g) include hg theorem comp : is_bilinear_map (Ξ» x y, g (f x y)) := { add_left := Ξ» x y z, by rw [hf.add_left, hg.add], add_right := Ξ» x y z, by rw [hf.add_right, hg.add], smul_left := Ξ» r x y, by rw [hf.smul_left, hg.smul], smul_right := Ξ» r x y, by rw [hf.smul_right, hg.smul] } end comp end is_bilinear_map namespace tensor_product def relators : set (free_abelian_group (M Γ— N)) := add_group.closure { x : free_abelian_group (M Γ— N) | (βˆƒ (m₁ mβ‚‚ : M) (n : N), x = (m₁, n) + (mβ‚‚, n) - (m₁ + mβ‚‚, n)) ∨ (βˆƒ (m : M) (n₁ nβ‚‚ : N), x = (m, n₁) + (m, nβ‚‚) - (m, n₁ + nβ‚‚)) ∨ (βˆƒ (r : R) (m : M) (n : N), x = (r β€’ m, n) - (m, r β€’ n)) } namespace relators instance : normal_add_subgroup (relators M N) := { normal := Ξ» x hx g, by rwa [add_sub_cancel'], .. add_group.closure.is_add_subgroup _ } end relators end tensor_product def tensor_product : Type* := quotient_add_group.quotient (tensor_product.relators M N) local infix ` βŠ— `:100 := tensor_product namespace tensor_product section module local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup instance : add_comm_group (M βŠ— N) := quotient_add_group.add_comm_group _ instance quotient.mk.is_add_group_hom : @is_add_group_hom (free_abelian_group (M Γ— N)) (M βŠ— N) _ _ quotient.mk := quotient_add_group.is_add_group_hom _ section tmul variables {M N} def tmul (m : M) (n : N) : M βŠ— N := quotient_add_group.mk $ free_abelian_group.of (m, n) infix ` βŠ—β‚œ `:100 := tmul lemma tmul.add_left (m₁ mβ‚‚ : M) (n : N) : (m₁ + mβ‚‚) βŠ—β‚œ n = m₁ βŠ—β‚œ n + mβ‚‚ βŠ—β‚œ n := eq.symm $ sub_eq_zero.1 $ eq.symm $ quotient.sound $ group.in_closure.basic $ or.inl $ ⟨m₁, mβ‚‚, n, rfl⟩ lemma tmul.add_right (m : M) (n₁ nβ‚‚ : N) : m βŠ—β‚œ (n₁ + nβ‚‚) = m βŠ—β‚œ n₁ + m βŠ—β‚œ nβ‚‚ := eq.symm $ sub_eq_zero.1 $ eq.symm $ quotient.sound $ group.in_closure.basic $ or.inr $ or.inl $ ⟨m, n₁, nβ‚‚, rfl⟩ lemma tmul.smul (r : R) (m : M) (n : N) : (r β€’ m) βŠ—β‚œ n = m βŠ—β‚œ (r β€’ n) := sub_eq_zero.1 $ eq.symm $ quotient.sound $ group.in_closure.basic $ or.inr $ or.inr $ ⟨r, m, n, rfl⟩ end tmul local attribute [instance] free_abelian_group.to_add_comm_group.is_add_group_hom local attribute [instance] quotient_add_group.is_add_group_hom_quotient_lift @[reducible] def smul.aux (r : R) (x : free_abelian_group (M Γ— N)) : M βŠ— N := free_abelian_group.to_add_comm_group (Ξ» (y : M Γ— N), (r β€’ y.1) βŠ—β‚œ (y.2)) x @[reducible] def smul (r : R) : M βŠ— N β†’ M βŠ— N := quotient_add_group.lift _ (smul.aux M N r) begin assume x hx, induction hx with _ hx _ _ ih _ _ _ _ ih1 ih2, { rcases hx with ⟨m₁, mβ‚‚, n, rfl⟩ | ⟨m, n₁, nβ‚‚, rfl⟩ | ⟨q, m, n, rfl⟩; simp [smul.aux, -sub_eq_add_neg, sub_self, tmul.add_left, tmul.add_right, tmul.smul, smul_add, smul_smul, mul_comm] }, { refl }, { change smul.aux M N r (-_) = 0, rw [is_add_group_hom.neg (smul.aux M N r), ih, neg_zero] }, { change smul.aux M N r (_ + _) = 0, rw [is_add_group_hom.add (smul.aux M N r), ih1, ih2, zero_add] } end lemma smul.is_add_group_hom (r : R) : is_add_group_hom (smul M N r) := by apply_instance local attribute [instance] smul.is_add_group_hom protected theorem smul_add (r : R) (x y : M βŠ— N) : smul M N r (x + y) = smul M N r x + smul M N r y := is_add_group_hom.add _ _ _ instance : module R (M βŠ— N) := { smul := smul M N, smul_add := tensor_product.smul_add M N, add_smul := begin intros r s x, apply quotient_add_group.induction_on' x, intro z, symmetry, refine @free_abelian_group.to_add_comm_group.unique _ _ _ _ _ ⟨λ p q, _⟩ _ z, { simp [tensor_product.smul_add] }, rintro ⟨m, n⟩, show (r β€’ m) βŠ—β‚œ n + (s β€’ m) βŠ—β‚œ n = ((r + s) β€’ m) βŠ—β‚œ n, simp [add_smul, tmul.add_left] end, mul_smul := begin intros r s x, apply quotient_add_group.induction_on' x, intro z, symmetry, refine @free_abelian_group.to_add_comm_group.unique _ _ _ _ _ ⟨λ p q, _⟩ _ z, { simp [tensor_product.smul_add] }, rintro ⟨m, n⟩, simp [smul, smul.aux, mul_smul, tmul] end, one_smul := Ξ» x, quotient.induction_on x $ Ξ» _, eq.symm $ free_abelian_group.to_add_comm_group.unique _ _ $ Ξ» ⟨p, q⟩, by rw [one_smul]; refl, .. tensor_product.add_comm_group M N } theorem bilinear : is_bilinear_map (@tmul R _ M N _ _) := { add_left := tmul.add_left, add_right := tmul.add_right, smul_left := Ξ» r x y, rfl, smul_right := assume r m n, (tmul.smul r m n).symm } @[simp] lemma add_tmul (m₁ mβ‚‚ : M) (n : N) : (m₁ + mβ‚‚) βŠ—β‚œ n = m₁ βŠ—β‚œ n + mβ‚‚ βŠ—β‚œ n := (bilinear M N).add_left m₁ mβ‚‚ n @[simp] lemma tmul_add (m : M) (n₁ nβ‚‚ : N) : m βŠ—β‚œ (n₁ + nβ‚‚) = m βŠ—β‚œ n₁ + m βŠ—β‚œ nβ‚‚ := (bilinear M N).add_right m n₁ nβ‚‚ @[simp] lemma smul_tmul (r : R) (x : M) (y : N) : (r β€’ x) βŠ—β‚œ y = r β€’ (x βŠ—β‚œ y) := rfl @[simp] lemma tmul_smul (r : R) (x : M) (y : N) : x βŠ—β‚œ (r β€’ y) = r β€’ (x βŠ—β‚œ y) := (bilinear M N).smul_right r x y end module local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup variables {M N} @[elab_as_eliminator] protected theorem induction_on {C : M βŠ— N β†’ Prop} (z : M βŠ— N) (C0 : C 0) (C1 : βˆ€ x y, C $ x βŠ—β‚œ y) (Cp : βˆ€ x y, C x β†’ C y β†’ C (x + y)) : C z := quotient.induction_on z $ Ξ» x, free_abelian_group.induction_on x C0 (Ξ» ⟨p, q⟩, C1 p q) (Ξ» ⟨p, q⟩ _, show C (-(p βŠ—β‚œ q)), by rw ← (bilinear M N).neg_left; from C1 (-p) q) (Ξ» _ _, Cp _ _) section UMP variables {M N P Q} variables (f : M β†’ N β†’ P) (hf : is_bilinear_map f) include hf local attribute [instance] free_abelian_group.to_add_comm_group.is_add_group_hom def to_module : M βŠ— N β†’ P := quotient_add_group.lift _ (free_abelian_group.to_add_comm_group $ Ξ» z, f z.1 z.2) $ Ξ» x hx, begin induction hx with _ hx _ _ ih _ _ _ _ ih1 ih2, { rcases hx with ⟨m₁, mβ‚‚, n, rfl⟩ | ⟨m, n₁, nβ‚‚, rfl⟩ | ⟨r, m, n, rfl⟩; simp [-sub_eq_add_neg, hf.add_left, hf.add_right, hf.smul_left, hf.smul_right, sub_self] }, { refl }, { change free_abelian_group.to_add_comm_group _ (-_) = (0:P), simp [ih] }, { change free_abelian_group.to_add_comm_group _ (_ + _) = (0:P), simp [ih1, ih2] } end variable {f} local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup @[simp] lemma to_module.add (x y) : to_module f hf (x + y) = to_module f hf x + to_module f hf y := quotient.induction_onβ‚‚ x y $ Ξ» m n, free_abelian_group.to_add_comm_group.add _ _ _ @[simp] lemma to_module.smul (r x) : to_module f hf (r β€’ x) = r β€’ to_module f hf x := tensor_product.induction_on x smul_zero.symm (Ξ» p q, by rw [← (bilinear M N).smul_left]; simp [to_module, tmul, hf.smul_left]) (Ξ» p q ih1 ih2, by simp [@smul_add _ _ _ _ r p q, to_module.add, ih1, ih2, smul_add]) def to_module.linear : is_linear_map (to_module f hf) := { add := to_module.add hf, smul := to_module.smul hf } @[simp] lemma to_module.tmul (x y) : to_module f hf (x βŠ—β‚œ y) = f x y := by simp [to_module, tmul] theorem to_module.unique {g : M βŠ— N β†’ P} (hg : is_linear_map g) (H : βˆ€ x y, g (x βŠ—β‚œ y) = f x y) (z : M βŠ— N) : g z = to_module f hf z := begin apply quotient_add_group.induction_on' z, intro z, refine @free_abelian_group.to_add_comm_group.unique _ _ _ _ _ ⟨λ p q, _⟩ _ z, { simp [hg.add] }, exact Ξ» ⟨m, n⟩, H m n end omit hf theorem to_module.ext {g h : M βŠ— N β†’ P} (hg : is_linear_map g) (hh : is_linear_map h) (H : βˆ€ x y, g (x βŠ—β‚œ y) = h (x βŠ—β‚œ y)) (z : M βŠ— N) : g z = h z := begin apply quotient_add_group.induction_on' z, intro z, apply free_abelian_group.to_add_comm_group.ext (g ∘ _) _ _, { constructor, intros p q, simp [hg.add] }, { constructor, intros p q, simp [hh.add] }, rintro ⟨m, n⟩, exact H m n end def to_module.equiv : { f : M β†’ N β†’ P // is_bilinear_map f } ≃ linear_map (M βŠ— N) P := { to_fun := Ξ» f, ⟨to_module f.1 f.2, to_module.linear f.2⟩, inv_fun := Ξ» f, ⟨λ m n, f (m βŠ—β‚œ n), is_bilinear_map.comp (bilinear M N) f.2⟩, left_inv := Ξ» f, subtype.eq $ funext $ Ξ» x, funext $ Ξ» y, to_module.tmul f.2 _ _, right_inv := Ξ» f, subtype.eq $ eq.symm $ funext $ Ξ» z, to_module.unique _ f.2 (Ξ» x y, rfl) _ } end UMP protected def id : R βŠ— M ≃ₗ M := { to_fun := @to_module _ _ _ _ _ _ _ _ (Ξ» c x, c β€’ x) $ by refine {..}; intros; simp [smul_add, add_smul, smul_smul, mul_comm, mul_left_comm], inv_fun := Ξ» x, 1 βŠ—β‚œ x, left_inv := Ξ» z, by refine to_module.ext (((bilinear R M).linear_right 1).comp $ to_module.linear _) is_linear_map.id (Ξ» c x, _) z; simp; rw [← smul_tmul, smul_eq_mul, mul_one], right_inv := Ξ» z, by simp, linear_fun := to_module.linear _ } protected def comm : M βŠ— N ≃ₗ N βŠ— M := { to_fun := to_module _ (bilinear N M).comm, inv_fun := to_module _ (bilinear M N).comm, left_inv := Ξ» z, by refine to_module.ext ((to_module.linear _).comp (to_module.linear _)) is_linear_map.id (Ξ» x y, _) z; simp, right_inv := Ξ» z, by refine to_module.ext ((to_module.linear _).comp (to_module.linear _)) is_linear_map.id (Ξ» x y, _) z; simp, linear_fun := to_module.linear _ } protected def assoc : (M βŠ— N) βŠ— P ≃ₗ M βŠ— (N βŠ— P) := { to_fun := begin refine to_module (Ξ» mn p, to_module (Ξ» m n, m βŠ—β‚œ (n βŠ—β‚œ p)) _ mn) _; constructor; intros; simp, { symmetry, refine to_module.unique _ (is_linear_map.map_add (to_module.linear _) (to_module.linear _)) _ _, intros; simp }, { symmetry, refine to_module.unique _ (is_linear_map.map_smul_right (to_module.linear _)) _ _, intros; simp } end, inv_fun := begin refine to_module (Ξ» m, to_module (Ξ» n p, (m βŠ—β‚œ n) βŠ—β‚œ p) _) _; constructor; intros; simp, { symmetry, refine to_module.unique _ (is_linear_map.map_add (to_module.linear _) (to_module.linear _)) _ _, intros; simp }, { symmetry, refine to_module.unique _ (is_linear_map.map_smul_right (to_module.linear _)) _ _, intros; simp } end, left_inv := begin intro z, refine to_module.ext ((to_module.linear _).comp (to_module.linear _)) is_linear_map.id (Ξ» mn p, _) z, simp, refine to_module.ext ((to_module.linear _).comp (to_module.linear _)) ((bilinear _ _).linear_left p) (Ξ» m n, _) mn, simp end, right_inv := begin intro z, refine to_module.ext ((to_module.linear _).comp (to_module.linear _)) is_linear_map.id (Ξ» m np, _) z, simp, refine to_module.ext ((to_module.linear _).comp (to_module.linear _)) ((bilinear _ _).linear_right m) (Ξ» n p, _) np, simp end, linear_fun := to_module.linear _ } end tensor_product
0c9227c4718878ac65ac9845bd4b372133eb79c2
b147e1312077cdcfea8e6756207b3fa538982e12
/tactic/norm_num.lean
4badb342edfb3d7715d4ae85bbf997aa33ed9651
[ "Apache-2.0" ]
permissive
SzJS/mathlib
07836ee708ca27cd18347e1e11ce7dd5afb3e926
23a5591fca0d43ee5d49d89f6f0ee07a24a6ca29
refs/heads/master
1,584,980,332,064
1,532,063,841,000
1,532,063,841,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,216
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) | `(@has_pow.pow %%Ξ± _ %%m %%e₁ %%eβ‚‚) := match m with | `(nat.has_pow) := mk_app ``nat.pow [e₁, eβ‚‚] >>= eval_pow | `(@monoid.has_pow %%Ξ± %%m) := mk_app ``monoid.pow [e₁, eβ‚‚] >>= eval_pow | _ := failed end | `(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 (hs : parse simp_arg_list) (l : parse location) : tactic unit := let t := orelse' (norm_num1 l) $ simp_core {} (norm_num1 (loc.ns [none])) ff hs [] l 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