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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.