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
0ce89c2804ae3c765df5e7b69d070108146f3d1a
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Util.lean
efa27dd82672f6adc26c151025391e0b21b387e6
[ "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
702
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.CollectFVars import Lean.Util.CollectLevelParams import Lean.Util.CollectMVars import Lean.Util.FindMVar import Lean.Util.FindLevelMVar import Lean.Util.MonadCache import Lean.Util.PPExt import Lean.Util.Path import Lean.Util.Profile import Lean.Util.RecDepth import Lean.Util.Sorry import Lean.Util.Trace import Lean.Util.FindExpr import Lean.Util.ReplaceExpr import Lean.Util.ForEachExpr import Lean.Util.ReplaceLevel import Lean.Util.FoldConsts import Lean.Util.SCC import Lean.Util.OccursCheck import Lean.Util.Paths
bd93ef8faba6947e5db70cdbbed8346e9f3415af
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/stage0/src/Lean/Elab/BuiltinNotation.lean
dc86d257356ac8abc6fff0ded61462a5a8503f83
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,026
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Term import Lean.Elab.Quotation import Lean.Elab.SyntheticMVars namespace Lean namespace Elab namespace Term open Meta @[builtinMacro Lean.Parser.Term.dollar] def expandDollar : Macro := fun stx => match_syntax stx with | `($f $args* $ $a) => let args := args.push a; `($f $args*) | `($f $ $a) => `($f $a) | _ => Macro.throwUnsupported @[builtinMacro Lean.Parser.Term.dollarProj] def expandDollarProj : Macro := fun stx => match_syntax stx with | `($term $.$field) => `($(term).$field) | _ => Macro.throwUnsupported @[builtinMacro Lean.Parser.Term.if] def expandIf : Macro := fun stx => match_syntax stx with | `(if $h : $cond then $t else $e) => `(dite $cond (fun $h:ident => $t) (fun $h:ident => $e)) | `(if $cond then $t else $e) => `(ite $cond $t $e) | _ => Macro.throwUnsupported @[builtinMacro Lean.Parser.Term.subtype] def expandSubtype : Macro := fun stx => match_syntax stx with | `({ $x : $type // $p }) => `(Subtype (fun ($x:ident : $type) => $p)) | `({ $x // $p }) => `(Subtype (fun ($x:ident : _) => $p)) | _ => Macro.throwUnsupported @[builtinTermElab anonymousCtor] def elabAnonymousCtor : TermElab := fun stx expectedType? => match_syntax stx with | `(⟨$args*⟩) => do tryPostponeIfNoneOrMVar expectedType?; match expectedType? with | some expectedType => do expectedType ← instantiateMVars expectedType; let expectedType := expectedType.consumeMData; expectedType ← whnf expectedType; matchConstStruct expectedType.getAppFn (fun _ => throwError ("invalid constructor ⟨...⟩, expected type must be a structure " ++ indentExpr expectedType)) (fun val _ ctor => do newStx ← `($(mkCIdentFrom stx ctor.name) $(args.getSepElems)*); withMacroExpansion stx newStx $ elabTerm newStx expectedType?) | none => throwError "invalid constructor ⟨...⟩, expected type must be known" | _ => throwUnsupportedSyntax @[builtinMacro Lean.Parser.Term.show] def expandShow : Macro := fun stx => match_syntax stx with | `(show $type from $val) => let thisId := mkIdentFrom stx `this; `(let! $thisId : $type := $val; $thisId) | _ => Macro.throwUnsupported @[builtinMacro Lean.Parser.Term.have] def expandHave : Macro := fun stx => match_syntax stx with | `(have $type from $val; $body) => let thisId := mkIdentFrom stx `this; `(let! $thisId : $type := $val; $body) | `(have $type := $val; $body) => let thisId := mkIdentFrom stx `this; `(let! $thisId : $type := $val; $body) | `(have $x : $type from $val; $body) => `(let! $x:ident : $type := $val; $body) | `(have $x : $type := $val; $body) => `(let! $x:ident : $type := $val; $body) | _ => Macro.throwUnsupported @[builtinMacro Lean.Parser.Term.where] def expandWhere : Macro := fun stx => match_syntax stx with | `($body where $decls:letDecl*) => do let decls := decls.getEvenElems; decls.foldrM (fun decl body => `(let $decl:letDecl; $body)) body | _ => Macro.throwUnsupported private def elabParserMacroAux (prec : Syntax) (e : Syntax) : TermElabM Syntax := do some declName ← getDeclName? | throwError "invalid `parser!` macro, it must be used in definitions"; match extractMacroScopes declName with | { name := Name.str _ s _, scopes := scps, .. } => do let kind := quote declName; let s := quote s; p ← `(Lean.Parser.leadingNode $kind $prec $e); if scps == [] then -- TODO simplify the following quotation as soon as we have coercions `(HasOrelse.orelse (Lean.Parser.mkAntiquot $s (some $kind)) $p) else -- if the parser decl is hidden by hygiene, it doesn't make sense to provide an antiquotation kind `(HasOrelse.orelse (Lean.Parser.mkAntiquot $s none) $p) | _ => throwError "invalid `parser!` macro, unexpected declaration name" @[builtinTermElab «parser!»] def elabParserMacro : TermElab := adaptExpander $ fun stx => match_syntax stx with | `(parser! $e) => elabParserMacroAux (quote Parser.maxPrec) e | `(parser! : $prec $e) => elabParserMacroAux prec e | _ => throwUnsupportedSyntax private def elabTParserMacroAux (prec : Syntax) (e : Syntax) : TermElabM Syntax := do declName? ← getDeclName?; match declName? with | some declName => let kind := quote declName; `(Lean.Parser.trailingNode $kind $prec $e) | none => throwError "invalid `tparser!` macro, it must be used in definitions" @[builtinTermElab «tparser!»] def elabTParserMacro : TermElab := adaptExpander $ fun stx => match_syntax stx with | `(tparser! $e) => elabTParserMacroAux (quote Parser.maxPrec) e | `(tparser! : $prec $e) => elabTParserMacroAux prec e | _ => throwUnsupportedSyntax private def mkNativeReflAuxDecl (type val : Expr) : TermElabM Name := do auxName ← mkAuxName `_nativeRefl; let decl := Declaration.defnDecl { name := auxName, lparams := [], type := type, value := val, hints := ReducibilityHints.abbrev, isUnsafe := false }; addDecl decl; compileDecl decl; pure auxName private def elabClosedTerm (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do e ← elabTermAndSynthesize stx expectedType?; when e.hasMVar $ throwError ("invalid macro application, term contains metavariables" ++ indentExpr e); when e.hasFVar $ throwError ("invalid macro application, term contains free variables" ++ indentExpr e); pure e @[builtinTermElab «nativeRefl»] def elabNativeRefl : TermElab := fun stx _ => do let arg := stx.getArg 1; e ← elabClosedTerm arg none; type ← inferType e; type ← whnf type; unless (type.isConstOf `Bool || type.isConstOf `Nat) $ throwError ("invalid `nativeRefl!` macro application, term must have type `Nat` or `Bool`" ++ indentExpr type); auxDeclName ← mkNativeReflAuxDecl type e; let isBool := type.isConstOf `Bool; let reduceValFn := if isBool then `Lean.reduceBool else `Lean.reduceNat; let reduceThm := if isBool then `Lean.ofReduceBool else `Lean.ofReduceNat; let aux := Lean.mkConst auxDeclName; let reduceVal := mkApp (Lean.mkConst reduceValFn) aux; val? ← liftMetaM $ Meta.reduceNative? reduceVal; match val? with | none => throwError ("failed to reduce term at `nativeRefl!` macro application" ++ indentExpr e) | some val => do rflPrf ← mkEqRefl val; let r := mkApp3 (Lean.mkConst reduceThm) aux val rflPrf; eq ← mkEq e val; mkExpectedTypeHint r eq private def getPropToDecide (arg : Syntax) (expectedType? : Option Expr) : TermElabM Expr := if arg.isOfKind `Lean.Parser.Term.hole then do tryPostponeIfNoneOrMVar expectedType?; match expectedType? with | none => throwError "invalid macro, expected type is not available" | some expectedType => do expectedType ← instantiateMVars expectedType; when (expectedType.hasFVar || expectedType.hasMVar) $ throwError ("expected type must not contain free or meta variables" ++ indentExpr expectedType); pure expectedType else let prop := mkSort levelZero; elabClosedTerm arg prop @[builtinTermElab «nativeDecide»] def elabNativeDecide : TermElab := fun stx expectedType? => do let arg := stx.getArg 1; p ← getPropToDecide arg expectedType?; d ← mkAppM `Decidable.decide #[p]; auxDeclName ← mkNativeReflAuxDecl (Lean.mkConst `Bool) d; rflPrf ← mkEqRefl (toExpr true); let r := mkApp3 (Lean.mkConst `Lean.ofReduceBool) (Lean.mkConst auxDeclName) (toExpr true) rflPrf; mkExpectedTypeHint r p @[builtinTermElab Lean.Parser.Term.decide] def elabDecide : TermElab := fun stx expectedType? => do let arg := stx.getArg 1; p ← getPropToDecide arg expectedType?; d ← mkAppM `Decidable.decide #[p]; d ← instantiateMVars d; let s := d.appArg!; -- get instance from `d` rflPrf ← mkEqRefl (toExpr true); pure $ mkApp3 (Lean.mkConst `ofDecideEqTrue) p s rflPrf def expandInfix (f : Syntax) : Macro := fun stx => do -- term `op` term let a := stx.getArg 0; let b := stx.getArg 2; pure (mkAppStx f #[a, b]) def expandInfixOp (op : Name) : Macro := fun stx => expandInfix (mkCIdentFrom (stx.getArg 1) op) stx def expandPrefixOp (op : Name) : Macro := fun stx => do -- `op` term let a := stx.getArg 1; pure (mkAppStx (mkCIdentFrom (stx.getArg 0) op) #[a]) @[builtinMacro Lean.Parser.Term.prod] def expandProd : Macro := expandInfixOp `Prod @[builtinMacro Lean.Parser.Term.fcomp] def ExpandFComp : Macro := expandInfixOp `Function.comp @[builtinMacro Lean.Parser.Term.add] def expandAdd : Macro := expandInfixOp `HasAdd.add @[builtinMacro Lean.Parser.Term.sub] def expandSub : Macro := expandInfixOp `HasSub.sub @[builtinMacro Lean.Parser.Term.mul] def expandMul : Macro := expandInfixOp `HasMul.mul @[builtinMacro Lean.Parser.Term.div] def expandDiv : Macro := expandInfixOp `HasDiv.div @[builtinMacro Lean.Parser.Term.mod] def expandMod : Macro := expandInfixOp `HasMod.mod @[builtinMacro Lean.Parser.Term.modN] def expandModN : Macro := expandInfixOp `HasModN.modn @[builtinMacro Lean.Parser.Term.pow] def expandPow : Macro := expandInfixOp `HasPow.pow @[builtinMacro Lean.Parser.Term.le] def expandLE : Macro := expandInfixOp `HasLessEq.LessEq @[builtinMacro Lean.Parser.Term.ge] def expandGE : Macro := expandInfixOp `GreaterEq @[builtinMacro Lean.Parser.Term.lt] def expandLT : Macro := expandInfixOp `HasLess.Less @[builtinMacro Lean.Parser.Term.gt] def expandGT : Macro := expandInfixOp `Greater @[builtinMacro Lean.Parser.Term.eq] def expandEq : Macro := expandInfixOp `Eq @[builtinMacro Lean.Parser.Term.ne] def expandNe : Macro := expandInfixOp `Ne @[builtinMacro Lean.Parser.Term.beq] def expandBEq : Macro := expandInfixOp `HasBeq.beq @[builtinMacro Lean.Parser.Term.bne] def expandBNe : Macro := expandInfixOp `bne @[builtinMacro Lean.Parser.Term.heq] def expandHEq : Macro := expandInfixOp `HEq @[builtinMacro Lean.Parser.Term.equiv] def expandEquiv : Macro := expandInfixOp `HasEquiv.Equiv @[builtinMacro Lean.Parser.Term.and] def expandAnd : Macro := expandInfixOp `And @[builtinMacro Lean.Parser.Term.or] def expandOr : Macro := expandInfixOp `Or @[builtinMacro Lean.Parser.Term.iff] def expandIff : Macro := expandInfixOp `Iff @[builtinMacro Lean.Parser.Term.band] def expandBAnd : Macro := expandInfixOp `and @[builtinMacro Lean.Parser.Term.bor] def expandBOr : Macro := expandInfixOp `or @[builtinMacro Lean.Parser.Term.append] def expandAppend : Macro := expandInfixOp `HasAppend.append @[builtinMacro Lean.Parser.Term.cons] def expandCons : Macro := expandInfixOp `List.cons @[builtinMacro Lean.Parser.Term.andthen] def expandAndThen : Macro := expandInfixOp `HasAndthen.andthen @[builtinMacro Lean.Parser.Term.bindOp] def expandBind : Macro := expandInfixOp `HasBind.bind @[builtinMacro Lean.Parser.Term.seq] def expandseq : Macro := expandInfixOp `HasSeq.seq @[builtinMacro Lean.Parser.Term.seqLeft] def expandseqLeft : Macro := expandInfixOp `HasSeqLeft.seqLeft @[builtinMacro Lean.Parser.Term.seqRight] def expandseqRight : Macro := expandInfixOp `HasSeqRight.seqRight @[builtinMacro Lean.Parser.Term.map] def expandMap : Macro := expandInfixOp `Functor.map @[builtinMacro Lean.Parser.Term.mapRev] def expandMapRev : Macro := expandInfixOp `Functor.mapRev @[builtinMacro Lean.Parser.Term.orelse] def expandOrElse : Macro := expandInfixOp `HasOrelse.orelse @[builtinMacro Lean.Parser.Term.orM] def expandOrM : Macro := expandInfixOp `orM @[builtinMacro Lean.Parser.Term.andM] def expandAndM : Macro := expandInfixOp `andM @[builtinMacro Lean.Parser.Term.not] def expandNot : Macro := expandPrefixOp `Not @[builtinMacro Lean.Parser.Term.bnot] def expandBNot : Macro := expandPrefixOp `not /- TODO @[builtinTermElab] def elabsubst : TermElab := expandInfixOp infixR " ▸ " 75 -/ end Term end Elab end Lean
3296f8924d4901628ac7c925a7eb78ea552696f2
ef4d3feecef33d1c1b4bd3a023b85e6a58f9e708
/theorem-proving-in-lean/ch2/Exercises.lean
aa9e66198e396dcdaf9e93c850483a8c96ee723a
[]
no_license
MikeMKH/kata
1b7da1b8d2cc115c912f2b06b583a8e675a449e1
305b054a37517dbe4d09545d41f024937f536c20
refs/heads/master
1,585,594,368,426
1,542,835,119,000
1,542,835,119,000
16,891,298
0
0
null
1,542,835,120,000
1,392,575,890,000
Racket
UTF-8
Lean
false
false
1,742
lean
-- https://leanprover.github.io/theorem_proving_in_lean/dependent_type_theory.html -- 1 def double : ℕ → ℕ := λ x, x + x def square : ℕ → ℕ := λ x, x * x def do_twice : (ℕ → ℕ) → ℕ → ℕ := λ f x, f (f x) #eval do_twice double 2 def mul_by_8 : ℕ → ℕ := λ x, x * (do_twice double 2) #eval mul_by_8 2 def Do_Twice : ((ℕ → ℕ) → (ℕ → ℕ)) → (ℕ → ℕ) → (ℕ → ℕ) := λ g f, g f #check Do_Twice do_twice #reduce Do_Twice do_twice #eval Do_Twice do_twice double 2 -- 2 def add_tuple : (ℕ × ℕ) → ℕ := λ x, x.1 + x.2 #eval add_tuple (2, 3) def curry (α β γ : Type) (f : α × β → γ) : α → β → γ := λ x y, f (x, y) #check curry ℕ ℕ ℕ add_tuple #eval (curry ℕ ℕ ℕ add_tuple) 2 3 def uncurry (α β γ : Type) (f : α → β → γ) : α × β → γ := λ (x : α × β), f x.1 x.2 #check uncurry ℕ ℕ ℕ (curry ℕ ℕ ℕ add_tuple) #eval uncurry ℕ ℕ ℕ (curry ℕ ℕ ℕ add_tuple) (2, 3) -- 3 universe u constant vec : Type u → ℕ → Type u constant vec_add : Π (α : Type) (n : ℕ), vec α n → vec α n → vec α n constant vec_reserve : Π (α : Type) (n : ℕ), vec α n → vec α n variables v1 v2: vec ℕ 3 #check vec_add ℕ 3 v1 v2 #check vec_reserve ℕ 3 v1 -- 4 constant matrix : Type u → ℕ → ℕ → Type u constant matrix_add : Π (α : Type) (n : ℕ) (m : ℕ), matrix α n m → matrix α n m → matrix α n m constant matrix_mul : Π (α : Type) (n : ℕ) (m : ℕ) (p : ℕ), matrix α n m → matrix α m p → matrix α n p variables m1 m2 : matrix ℕ 2 3 variable m3 : matrix ℕ 3 4 #check matrix_add ℕ 2 3 m1 m2 #check matrix_mul ℕ 2 3 4 m1 m3
9b7c8b557fdfc6abcdc98134dd06c07ed2affe6b
4727251e0cd73359b15b664c3170e5d754078599
/src/data/matrix/notation.lean
d6d5bf6659a7def76bb3b9109d82671598ebc23e
[ "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
11,275
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 data.matrix.basic import data.fin.vec_notation import tactic.fin_cases import algebra.big_operators.fin /-! # Matrix and vector notation This file includes `simp` lemmas for applying operations in `data.matrix.basic` to values built out of the matrix notation `![a, b] = vec_cons a (vec_cons b vec_empty)` defined in `data.fin.vec_notation`. ## Implementation notes The `simp` lemmas require that one of the arguments is of the form `vec_cons _ _`. This ensures `simp` works with entries only when (some) entries are already given. In other words, this notation will only appear in the output of `simp` if it already appears in the input. ## Notations We reuse notation `![a, b]` for `vec_cons a (vec_cons b vec_empty)`. It is a localized notation in the `matrix` locale. ## Examples Examples of usage can be found in the `test/matrix.lean` file. -/ namespace matrix universe u variables {α : Type u} {o n m : ℕ} {m' n' o' : Type*} open_locale matrix /-- Use `![...]` notation for displaying a `fin`-indexed matrix, for example: ``` #eval ![![1, 2], ![3, 4]] + ![![3, 4], ![5, 6]] -- ![![4, 6], ![8, 10]] ``` -/ instance [has_repr α] : has_repr (matrix (fin m) (fin n) α) := pi_fin.has_repr @[simp] lemma cons_val' (v : n' → α) (B : matrix (fin m) n' α) (i j) : vec_cons v B i j = vec_cons (v j) (λ i, B i j) i := by { refine fin.cases _ _ i; simp } @[simp] lemma head_val' (B : matrix (fin m.succ) n' α) (j : n') : vec_head (λ i, B i j) = vec_head B j := rfl @[simp] lemma tail_val' (B : matrix (fin m.succ) n' α) (j : n') : vec_tail (λ i, B i j) = λ i, vec_tail B i j := by { ext, simp [vec_tail] } section dot_product variables [add_comm_monoid α] [has_mul α] @[simp] lemma dot_product_empty (v w : fin 0 → α) : dot_product v w = 0 := finset.sum_empty @[simp] lemma cons_dot_product (x : α) (v : fin n → α) (w : fin n.succ → α) : dot_product (vec_cons x v) w = x * vec_head w + dot_product v (vec_tail w) := by simp [dot_product, fin.sum_univ_succ, vec_head, vec_tail] @[simp] lemma dot_product_cons (v : fin n.succ → α) (x : α) (w : fin n → α) : dot_product v (vec_cons x w) = vec_head v * x + dot_product (vec_tail v) w := by simp [dot_product, fin.sum_univ_succ, vec_head, vec_tail] @[simp] lemma cons_dot_product_cons (x : α) (v : fin n → α) (y : α) (w : fin n → α) : dot_product (vec_cons x v) (vec_cons y w) = x * y + dot_product v w := by simp end dot_product section col_row @[simp] lemma col_empty (v : fin 0 → α) : col v = vec_empty := empty_eq _ @[simp] lemma col_cons (x : α) (u : fin m → α) : col (vec_cons x u) = vec_cons (λ _, x) (col u) := by { ext i j, refine fin.cases _ _ i; simp [vec_head, vec_tail] } @[simp] lemma row_empty : row (vec_empty : fin 0 → α) = λ _, vec_empty := by { ext, refl } @[simp] lemma row_cons (x : α) (u : fin m → α) : row (vec_cons x u) = λ _, vec_cons x u := by { ext, refl } end col_row section transpose @[simp] lemma transpose_empty_rows (A : matrix m' (fin 0) α) : Aᵀ = ![] := empty_eq _ @[simp] lemma transpose_empty_cols : (![] : matrix (fin 0) m' α)ᵀ = λ i, ![] := funext (λ i, empty_eq _) @[simp] lemma cons_transpose (v : n' → α) (A : matrix (fin m) n' α) : (vec_cons v A)ᵀ = λ i, vec_cons (v i) (Aᵀ i) := by { ext i j, refine fin.cases _ _ j; simp } @[simp] lemma head_transpose (A : matrix m' (fin n.succ) α) : vec_head (Aᵀ) = vec_head ∘ A := rfl @[simp] lemma tail_transpose (A : matrix m' (fin n.succ) α) : vec_tail (Aᵀ) = (vec_tail ∘ A)ᵀ := by { ext i j, refl } end transpose section mul variables [semiring α] @[simp] lemma empty_mul [fintype n'] (A : matrix (fin 0) n' α) (B : matrix n' o' α) : A ⬝ B = ![] := empty_eq _ @[simp] lemma empty_mul_empty (A : matrix m' (fin 0) α) (B : matrix (fin 0) o' α) : A ⬝ B = 0 := rfl @[simp] lemma mul_empty [fintype n'] (A : matrix m' n' α) (B : matrix n' (fin 0) α) : A ⬝ B = λ _, ![] := funext (λ _, empty_eq _) lemma mul_val_succ [fintype n'] (A : matrix (fin m.succ) n' α) (B : matrix n' o' α) (i : fin m) (j : o') : (A ⬝ B) i.succ j = (vec_tail A ⬝ B) i j := rfl @[simp] lemma cons_mul [fintype n'] (v : n' → α) (A : matrix (fin m) n' α) (B : matrix n' o' α) : vec_cons v A ⬝ B = vec_cons (vec_mul v B) (A ⬝ B) := by { ext i j, refine fin.cases _ _ i, { refl }, simp [mul_val_succ] } end mul section vec_mul variables [semiring α] @[simp] lemma empty_vec_mul (v : fin 0 → α) (B : matrix (fin 0) o' α) : vec_mul v B = 0 := rfl @[simp] lemma vec_mul_empty [fintype n'] (v : n' → α) (B : matrix n' (fin 0) α) : vec_mul v B = ![] := empty_eq _ @[simp] lemma cons_vec_mul (x : α) (v : fin n → α) (B : matrix (fin n.succ) o' α) : vec_mul (vec_cons x v) B = x • (vec_head B) + vec_mul v (vec_tail B) := by { ext i, simp [vec_mul] } @[simp] lemma vec_mul_cons (v : fin n.succ → α) (w : o' → α) (B : matrix (fin n) o' α) : vec_mul v (vec_cons w B) = vec_head v • w + vec_mul (vec_tail v) B := by { ext i, simp [vec_mul] } end vec_mul section mul_vec variables [semiring α] @[simp] lemma empty_mul_vec [fintype n'] (A : matrix (fin 0) n' α) (v : n' → α) : mul_vec A v = ![] := empty_eq _ @[simp] lemma mul_vec_empty (A : matrix m' (fin 0) α) (v : fin 0 → α) : mul_vec A v = 0 := rfl @[simp] lemma cons_mul_vec [fintype n'] (v : n' → α) (A : fin m → n' → α) (w : n' → α) : mul_vec (vec_cons v A) w = vec_cons (dot_product v w) (mul_vec A w) := by { ext i, refine fin.cases _ _ i; simp [mul_vec] } @[simp] lemma mul_vec_cons {α} [comm_semiring α] (A : m' → (fin n.succ) → α) (x : α) (v : fin n → α) : mul_vec A (vec_cons x v) = (x • vec_head ∘ A) + mul_vec (vec_tail ∘ A) v := by { ext i, simp [mul_vec, mul_comm] } end mul_vec section vec_mul_vec variables [semiring α] @[simp] lemma empty_vec_mul_vec (v : fin 0 → α) (w : n' → α) : vec_mul_vec v w = ![] := empty_eq _ @[simp] lemma vec_mul_vec_empty (v : m' → α) (w : fin 0 → α) : vec_mul_vec v w = λ _, ![] := funext (λ i, empty_eq _) @[simp] lemma cons_vec_mul_vec (x : α) (v : fin m → α) (w : n' → α) : vec_mul_vec (vec_cons x v) w = vec_cons (x • w) (vec_mul_vec v w) := by { ext i, refine fin.cases _ _ i; simp [vec_mul_vec] } @[simp] lemma vec_mul_vec_cons (v : m' → α) (x : α) (w : fin n → α) : vec_mul_vec v (vec_cons x w) = λ i, v i • vec_cons x w := by { ext i j, rw [vec_mul_vec, pi.smul_apply, smul_eq_mul] } end vec_mul_vec section smul variables [semiring α] @[simp] lemma smul_mat_empty {m' : Type*} (x : α) (A : fin 0 → m' → α) : x • A = ![] := empty_eq _ @[simp] lemma smul_mat_cons (x : α) (v : n' → α) (A : matrix (fin m) n' α) : x • vec_cons v A = vec_cons (x • v) (x • A) := by { ext i, refine fin.cases _ _ i; simp } end smul section minor @[simp] lemma minor_empty (A : matrix m' n' α) (row : fin 0 → m') (col : o' → n') : minor A row col = ![] := empty_eq _ @[simp] lemma minor_cons_row (A : matrix m' n' α) (i : m') (row : fin m → m') (col : o' → n') : minor A (vec_cons i row) col = vec_cons (λ j, A i (col j)) (minor A row col) := by { ext i j, refine fin.cases _ _ i; simp [minor] } end minor section vec2_and_vec3 section one variables [has_zero α] [has_one α] lemma one_fin_two : (1 : matrix (fin 2) (fin 2) α) = ![![1, 0], ![0, 1]] := by { ext i j, fin_cases i; fin_cases j; refl } lemma one_fin_three : (1 : matrix (fin 3) (fin 3) α) = ![![1, 0, 0], ![0, 1, 0], ![0, 0, 1]] := by { ext i j, fin_cases i; fin_cases j; refl } end one lemma mul_fin_two [add_comm_monoid α] [has_mul α] (a₁₁ a₁₂ a₂₁ a₂₂ b₁₁ b₁₂ b₂₁ b₂₂ : α) : ![![a₁₁, a₁₂], ![a₂₁, a₂₂]] ⬝ ![![b₁₁, b₁₂], ![b₂₁, b₂₂]] = ![![a₁₁ * b₁₁ + a₁₂ * b₂₁, a₁₁ * b₁₂ + a₁₂ * b₂₂], ![a₂₁ * b₁₁ + a₂₂ * b₂₁, a₂₁ * b₁₂ + a₂₂ * b₂₂]] := begin ext i j, fin_cases i; fin_cases j; simp [matrix.mul, dot_product, fin.sum_univ_succ] end lemma mul_fin_three [add_comm_monoid α] [has_mul α] (a₁₁ a₁₂ a₁₃ a₂₁ a₂₂ a₂₃ a₃₁ a₃₂ a₃₃ b₁₁ b₁₂ b₁₃ b₂₁ b₂₂ b₂₃ b₃₁ b₃₂ b₃₃ : α) : ![![a₁₁, a₁₂, a₁₃], ![a₂₁, a₂₂, a₂₃], ![a₃₁, a₃₂, a₃₃]] ⬝ ![![b₁₁, b₁₂, b₁₃], ![b₂₁, b₂₂, b₂₃], ![b₃₁, b₃₂, b₃₃]] = ![![a₁₁*b₁₁ + a₁₂*b₂₁ + a₁₃*b₃₁, a₁₁*b₁₂ + a₁₂*b₂₂ + a₁₃*b₃₂, a₁₁*b₁₃ + a₁₂*b₂₃ + a₁₃*b₃₃], ![a₂₁*b₁₁ + a₂₂*b₂₁ + a₂₃*b₃₁, a₂₁*b₁₂ + a₂₂*b₂₂ + a₂₃*b₃₂, a₂₁*b₁₃ + a₂₂*b₂₃ + a₂₃*b₃₃], ![a₃₁*b₁₁ + a₃₂*b₂₁ + a₃₃*b₃₁, a₃₁*b₁₂ + a₃₂*b₂₂ + a₃₃*b₃₂, a₃₁*b₁₃ + a₃₂*b₂₃ + a₃₃*b₃₃]] := begin ext i j, fin_cases i; fin_cases j; simp [matrix.mul, dot_product, fin.sum_univ_succ, ←add_assoc], end lemma vec2_eq {a₀ a₁ b₀ b₁ : α} (h₀ : a₀ = b₀) (h₁ : a₁ = b₁) : ![a₀, a₁] = ![b₀, b₁] := by subst_vars lemma vec3_eq {a₀ a₁ a₂ b₀ b₁ b₂ : α} (h₀ : a₀ = b₀) (h₁ : a₁ = b₁) (h₂ : a₂ = b₂) : ![a₀, a₁, a₂] = ![b₀, b₁, b₂] := by subst_vars lemma vec2_add [has_add α] (a₀ a₁ b₀ b₁ : α) : ![a₀, a₁] + ![b₀, b₁] = ![a₀ + b₀, a₁ + b₁] := by rw [cons_add_cons, cons_add_cons, empty_add_empty] lemma vec3_add [has_add α] (a₀ a₁ a₂ b₀ b₁ b₂ : α) : ![a₀, a₁, a₂] + ![b₀, b₁, b₂] = ![a₀ + b₀, a₁ + b₁, a₂ + b₂] := by rw [cons_add_cons, cons_add_cons, cons_add_cons, empty_add_empty] lemma smul_vec2 {R : Type*} [has_scalar R α] (x : R) (a₀ a₁ : α) : x • ![a₀, a₁] = ![x • a₀, x • a₁] := by rw [smul_cons, smul_cons, smul_empty] lemma smul_vec3 {R : Type*} [has_scalar R α] (x : R) (a₀ a₁ a₂ : α) : x • ![a₀, a₁, a₂] = ![x • a₀, x • a₁, x • a₂] := by rw [smul_cons, smul_cons, smul_cons, smul_empty] variables [add_comm_monoid α] [has_mul α] lemma vec2_dot_product' {a₀ a₁ b₀ b₁ : α} : ![a₀, a₁] ⬝ᵥ ![b₀, b₁] = a₀ * b₀ + a₁ * b₁ := by rw [cons_dot_product_cons, cons_dot_product_cons, dot_product_empty, add_zero] @[simp] lemma vec2_dot_product (v w : fin 2 → α) : v ⬝ᵥ w = v 0 * w 0 + v 1 * w 1 := vec2_dot_product' lemma vec3_dot_product' {a₀ a₁ a₂ b₀ b₁ b₂ : α} : ![a₀, a₁, a₂] ⬝ᵥ ![b₀, b₁, b₂] = a₀ * b₀ + a₁ * b₁ + a₂ * b₂ := by rw [cons_dot_product_cons, cons_dot_product_cons, cons_dot_product_cons, dot_product_empty, add_zero, add_assoc] @[simp] lemma vec3_dot_product (v w : fin 3 → α) : v ⬝ᵥ w = v 0 * w 0 + v 1 * w 1 + v 2 * w 2 := vec3_dot_product' end vec2_and_vec3 end matrix
4ccb9e1fae1d4523695090d4ed9967c6e2eef6c4
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/group_theory/submonoid/default.lean
d87fa65c1a5242084338eed86a1bbe5b35ea0101
[]
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
153
lean
import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.group_theory.submonoid.membership import Mathlib.PostPort namespace Mathlib
1ef38b8bb2b67daae54dd594265cd9cc250e9b6e
367134ba5a65885e863bdc4507601606690974c1
/src/topology/tactic.lean
c0d8fc7fc4a9bdc09544373ab4fc307653aec67c
[ "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
4,154
lean
/- Copyright (c) 2020 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton -/ import tactic.auto_cases import tactic.tidy import tactic.with_local_reducibility import tactic.show_term import topology.basic /-! # Tactics for topology Currently we have one domain-specific tactic for topology: `continuity`. -/ /-! ### `continuity` tactic Automatically solve goals of the form `continuous f`. Mark lemmas with `@[continuity]` to add them to the set of lemmas used by `continuity`. Note: `to_additive` doesn't know yet how to copy the attribute to the additive version. -/ /-- User attribute used to mark tactics used by `continuity`. -/ @[user_attribute] meta def continuity : user_attribute := { name := `continuity, descr := "lemmas usable to prove continuity" } -- Mark some continuity lemmas already defined in `topology.basic` attribute [continuity] continuous_id continuous_const -- As we will be using `apply_rules` with `md := semireducible`, -- we need another version of `continuous_id`. @[continuity] lemma continuous_id' {α : Type*} [topological_space α] : continuous (λ a : α, a) := continuous_id namespace tactic /-- Tactic to apply `continuous.comp` when appropriate. Applying `continuous.comp` is not always a good idea, so we have some extra logic here to try to avoid bad cases. * If the function we're trying to prove continuous is actually constant, and that constant is a function application `f z`, then continuous.comp would produce new goals `continuous f`, `continuous (λ _, z)`, which is silly. We avoid this by failing if we could apply continuous_const. * continuous.comp will always succeed on `continuous (λ x, f x)` and produce new goals `continuous (λ x, x)`, `continuous f`. We detect this by failing if a new goal can be closed by applying continuous_id. -/ meta def apply_continuous.comp : tactic unit := `[fail_if_success { exact continuous_const }; refine continuous.comp _ _; fail_if_success { exact continuous_id }] /-- List of tactics used by `continuity` internally. -/ meta def continuity_tactics (md : transparency := reducible) : list (tactic string) := [ intros1 >>= λ ns, pure ("intros " ++ (" ".intercalate (ns.map (λ e, e.to_string)))), apply_rules [``(continuity)] 50 { md := md } >> pure "apply_rules continuity", apply_continuous.comp >> pure "refine continuous.comp _ _" ] namespace interactive setup_tactic_parser /-- Solve goals of the form `continuous f`. `continuity?` reports back the proof term it found. -/ meta def continuity (bang : parse $ optional (tk "!")) (trace : parse $ optional (tk "?")) (cfg : tidy.cfg := {}) : tactic unit := let md := if bang.is_some then semireducible else reducible, continuity_core := tactic.tidy { tactics := continuity_tactics md, ..cfg }, trace_fn := if trace.is_some then show_term else id in trace_fn continuity_core /-- Version of `continuity` for use with auto_param. -/ meta def continuity' : tactic unit := continuity none none {} /-- `continuity` solves goals of the form `continuous f` by applying lemmas tagged with the `continuity` user attribute. ``` example {X Y : Type*} [topological_space X] [topological_space Y] (f₁ f₂ : X → Y) (hf₁ : continuous f₁) (hf₂ : continuous f₂) (g : Y → ℝ) (hg : continuous g) : continuous (λ x, (max (g (f₁ x)) (g (f₂ x))) + 1) := by continuity ``` will discharge the goal, generating a proof term like `((continuous.comp hg hf₁).max (continuous.comp hg hf₂)).add continuous_const` You can also use `continuity!`, which applies lemmas with `{ md := semireducible }`. The default behaviour is more conservative, and only unfolds `reducible` definitions when attempting to match lemmas with the goal. `continuity?` reports back the proof term it found. -/ add_tactic_doc { name := "continuity / continuity'", category := doc_category.tactic, decl_names := [`tactic.interactive.continuity, `tactic.interactive.continuity'], tags := ["lemma application"] } end interactive end tactic
c1a89d8cd3e3a1e603c3ca7f0f33b6216ff29daa
9dc8cecdf3c4634764a18254e94d43da07142918
/src/order/filter/archimedean.lean
11cb64425b1f3fc15e68db2061ce298828e91c6d
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
9,799
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import algebra.order.archimedean import order.filter.at_top_bot /-! # `at_top` filter and archimedean (semi)rings/fields In this file we prove that for a linear ordered archimedean semiring `R` and a function `f : α → ℕ`, the function `coe ∘ f : α → R` tends to `at_top` along a filter `l` if and only if so does `f`. We also prove that `coe : ℕ → R` tends to `at_top` along `at_top`, as well as version of these two results for `ℤ` (and a ring `R`) and `ℚ` (and a field `R`). -/ variables {α R : Type*} open filter set @[simp] lemma nat.comap_coe_at_top [ordered_semiring R] [nontrivial R] [archimedean R] : comap (coe : ℕ → R) at_top = at_top := comap_embedding_at_top (λ _ _, nat.cast_le) exists_nat_ge lemma tendsto_coe_nat_at_top_iff [ordered_semiring R] [nontrivial R] [archimedean R] {f : α → ℕ} {l : filter α} : tendsto (λ n, (f n : R)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, nat.cast_le) exists_nat_ge lemma tendsto_coe_nat_at_top_at_top [ordered_semiring R] [archimedean R] : tendsto (coe : ℕ → R) at_top at_top := nat.mono_cast.tendsto_at_top_at_top exists_nat_ge @[simp] lemma int.comap_coe_at_top [ordered_ring R] [nontrivial R] [archimedean R] : comap (coe : ℤ → R) at_top = at_top := comap_embedding_at_top (λ _ _, int.cast_le) $ λ r, let ⟨n, hn⟩ := exists_nat_ge r in ⟨n, by exact_mod_cast hn⟩ @[simp] lemma int.comap_coe_at_bot [ordered_ring R] [nontrivial R] [archimedean R] : comap (coe : ℤ → R) at_bot = at_bot := comap_embedding_at_bot (λ _ _, int.cast_le) $ λ r, let ⟨n, hn⟩ := exists_nat_ge (-r) in ⟨-n, by simpa [neg_le] using hn⟩ lemma tendsto_coe_int_at_top_iff [ordered_ring R] [nontrivial R] [archimedean R] {f : α → ℤ} {l : filter α} : tendsto (λ n, (f n : R)) l at_top ↔ tendsto f l at_top := by rw [← tendsto_comap_iff, int.comap_coe_at_top] lemma tendsto_coe_int_at_bot_iff [ordered_ring R] [nontrivial R] [archimedean R] {f : α → ℤ} {l : filter α} : tendsto (λ n, (f n : R)) l at_bot ↔ tendsto f l at_bot := by rw [← tendsto_comap_iff, int.comap_coe_at_bot] lemma tendsto_coe_int_at_top_at_top [ordered_ring R] [archimedean R] : tendsto (coe : ℤ → R) at_top at_top := int.cast_mono.tendsto_at_top_at_top $ λ b, let ⟨n, hn⟩ := exists_nat_ge b in ⟨n, by exact_mod_cast hn⟩ @[simp] lemma rat.comap_coe_at_top [linear_ordered_field R] [archimedean R] : comap (coe : ℚ → R) at_top = at_top := comap_embedding_at_top (λ _ _, rat.cast_le) $ λ r, let ⟨n, hn⟩ := exists_nat_ge r in ⟨n, by simpa⟩ @[simp] lemma rat.comap_coe_at_bot [linear_ordered_field R] [archimedean R] : comap (coe : ℚ → R) at_bot = at_bot := comap_embedding_at_bot (λ _ _, rat.cast_le) $ λ r, let ⟨n, hn⟩ := exists_nat_ge (-r) in ⟨-n, by simpa [neg_le]⟩ lemma tendsto_coe_rat_at_top_iff [linear_ordered_field R] [archimedean R] {f : α → ℚ} {l : filter α} : tendsto (λ n, (f n : R)) l at_top ↔ tendsto f l at_top := by rw [← tendsto_comap_iff, rat.comap_coe_at_top] lemma tendsto_coe_rat_at_bot_iff [linear_ordered_field R] [archimedean R] {f : α → ℚ} {l : filter α} : tendsto (λ n, (f n : R)) l at_bot ↔ tendsto f l at_bot := by rw [← tendsto_comap_iff, rat.comap_coe_at_bot] lemma at_top_countable_basis_of_archimedean [linear_ordered_semiring R] [archimedean R] : (at_top : filter R).has_countable_basis (λ n : ℕ, true) (λ n, Ici n) := { countable := to_countable _, to_has_basis := at_top_basis.to_has_basis (λ x hx, let ⟨n, hn⟩ := exists_nat_ge x in ⟨n, trivial, Ici_subset_Ici.2 hn⟩) (λ n hn, ⟨n, trivial, subset.rfl⟩) } lemma at_bot_countable_basis_of_archimedean [linear_ordered_ring R] [archimedean R] : (at_bot : filter R).has_countable_basis (λ m : ℤ, true) (λ m, Iic m) := { countable := to_countable _, to_has_basis := at_bot_basis.to_has_basis (λ x hx, let ⟨m, hm⟩ := exists_int_lt x in ⟨m, trivial, Iic_subset_Iic.2 hm.le⟩) (λ m hm, ⟨m, trivial, subset.rfl⟩) } @[priority 100] instance at_top_countably_generated_of_archimedean [linear_ordered_semiring R] [archimedean R] : (at_top : filter R).is_countably_generated := at_top_countable_basis_of_archimedean.is_countably_generated @[priority 100] instance at_bot_countably_generated_of_archimedean [linear_ordered_ring R] [archimedean R] : (at_bot : filter R).is_countably_generated := at_bot_countable_basis_of_archimedean.is_countably_generated namespace filter variables {l : filter α} {f : α → R} {r : R} section linear_ordered_semiring variables [linear_ordered_semiring R] [archimedean R] /-- If a function tends to infinity along a filter, then this function multiplied by a positive constant (on the left) also tends to infinity. The archimedean assumption is convenient to get a statement that works on `ℕ`, `ℤ` and `ℝ`, although not necessary (a version in ordered fields is given in `filter.tendsto.const_mul_at_top`). -/ lemma tendsto.const_mul_at_top' (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λx, r * f x) l at_top := begin apply tendsto_at_top.2 (λb, _), obtain ⟨n : ℕ, hn : 1 ≤ n • r⟩ := archimedean.arch 1 hr, rw nsmul_eq_mul' at hn, filter_upwards [tendsto_at_top.1 hf (n * max b 0)] with x hx, calc b ≤ 1 * max b 0 : by { rw [one_mul], exact le_max_left _ _ } ... ≤ (r * n) * max b 0 : mul_le_mul_of_nonneg_right hn (le_max_right _ _) ... = r * (n * max b 0) : by rw [mul_assoc] ... ≤ r * f x : mul_le_mul_of_nonneg_left hx (le_of_lt hr) end /-- If a function tends to infinity along a filter, then this function multiplied by a positive constant (on the right) also tends to infinity. The archimedean assumption is convenient to get a statement that works on `ℕ`, `ℤ` and `ℝ`, although not necessary (a version in ordered fields is given in `filter.tendsto.at_top_mul_const`). -/ lemma tendsto.at_top_mul_const' (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λx, f x * r) l at_top := begin apply tendsto_at_top.2 (λb, _), obtain ⟨n : ℕ, hn : 1 ≤ n • r⟩ := archimedean.arch 1 hr, have hn' : 1 ≤ (n : R) * r, by rwa nsmul_eq_mul at hn, filter_upwards [tendsto_at_top.1 hf (max b 0 * n)] with x hx, calc b ≤ max b 0 * 1 : by { rw [mul_one], exact le_max_left _ _ } ... ≤ max b 0 * (n * r) : mul_le_mul_of_nonneg_left hn' (le_max_right _ _) ... = (max b 0 * n) * r : by rw [mul_assoc] ... ≤ f x * r : mul_le_mul_of_nonneg_right hx (le_of_lt hr) end end linear_ordered_semiring section linear_ordered_ring variables [linear_ordered_ring R] [archimedean R] /-- See also `filter.tendsto.at_top_mul_neg_const` for a version of this lemma for `linear_ordered_field`s which does not require the `archimedean` assumption. -/ lemma tendsto.at_top_mul_neg_const' (hr : r < 0) (hf : tendsto f l at_top) : tendsto (λx, f x * r) l at_bot := by simpa only [tendsto_neg_at_top_iff, mul_neg] using hf.at_top_mul_const' (neg_pos.mpr hr) /-- See also `filter.tendsto.at_bot_mul_const` for a version of this lemma for `linear_ordered_field`s which does not require the `archimedean` assumption. -/ lemma tendsto.at_bot_mul_const' (hr : 0 < r) (hf : tendsto f l at_bot) : tendsto (λx, f x * r) l at_bot := begin simp only [← tendsto_neg_at_top_iff, ← neg_mul] at hf ⊢, exact hf.at_top_mul_const' hr end /-- See also `filter.tendsto.at_bot_mul_neg_const` for a version of this lemma for `linear_ordered_field`s which does not require the `archimedean` assumption. -/ lemma tendsto.at_bot_mul_neg_const' (hr : r < 0) (hf : tendsto f l at_bot) : tendsto (λx, f x * r) l at_top := by simpa only [mul_neg, tendsto_neg_at_bot_iff] using hf.at_bot_mul_const' (neg_pos.2 hr) end linear_ordered_ring section linear_ordered_cancel_add_comm_monoid variables [linear_ordered_cancel_add_comm_monoid R] [archimedean R] lemma tendsto.at_top_nsmul_const {f : α → ℕ} (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λ x, f x • r) l at_top := begin refine tendsto_at_top.mpr (λ s, _), obtain ⟨n : ℕ, hn : s ≤ n • r⟩ := archimedean.arch s hr, exact (tendsto_at_top.mp hf n).mono (λ a ha, hn.trans (nsmul_le_nsmul hr.le ha)), end end linear_ordered_cancel_add_comm_monoid section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group R] [archimedean R] lemma tendsto.at_top_nsmul_neg_const {f : α → ℕ} (hr : r < 0) (hf : tendsto f l at_top) : tendsto (λ x, f x • r) l at_bot := by simpa using hf.at_top_nsmul_const (neg_pos.2 hr) lemma tendsto.at_top_zsmul_const {f : α → ℤ} (hr : 0 < r) (hf : tendsto f l at_top) : tendsto (λ x, f x • r) l at_top := begin refine tendsto_at_top.mpr (λ s, _), obtain ⟨n : ℕ, hn : s ≤ n • r⟩ := archimedean.arch s hr, replace hn : s ≤ (n : ℤ) • r, { simpa, }, exact (tendsto_at_top.mp hf n).mono (λ a ha, hn.trans (zsmul_le_zsmul hr.le ha)), end lemma tendsto.at_top_zsmul_neg_const {f : α → ℤ} (hr : r < 0) (hf : tendsto f l at_top) : tendsto (λ x, f x • r) l at_bot := by simpa using hf.at_top_zsmul_const (neg_pos.2 hr) lemma tendsto.at_bot_zsmul_const {f : α → ℤ} (hr : 0 < r) (hf : tendsto f l at_bot) : tendsto (λ x, f x • r) l at_bot := begin simp only [← tendsto_neg_at_top_iff, ← neg_zsmul] at hf ⊢, exact hf.at_top_zsmul_const hr end lemma tendsto.at_bot_zsmul_neg_const {f : α → ℤ} (hr : r < 0) (hf : tendsto f l at_bot) : tendsto (λ x, f x • r) l at_top := by simpa using hf.at_bot_zsmul_const (neg_pos.2 hr) end linear_ordered_add_comm_group end filter
6a13edbbc812d2879c9ab12548637a3d7592e8fc
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/set/intervals/image_preimage.lean
093143dcb71fb89e51e6cadb4d0735b4ea754392
[ "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
11,710
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov, Patrick Massot -/ import data.set.intervals.basic import data.equiv.mul_add import algebra.pointwise /-! # (Pre)images of intervals In this file we prove a bunch of trivial lemmas like “if we add `a` to all points of `[b, c]`, then we get `[a + b, a + c]`”. For the functions `x ↦ x ± a`, `x ↦ a ± x`, and `x ↦ -x` we prove lemmas about preimages and images of all intervals. We also prove a few lemmas about images under `x ↦ a * x`, `x ↦ x * a` and `x ↦ x⁻¹`. -/ universe u namespace set section ordered_add_comm_group variables {G : Type u} [ordered_add_comm_group G] (a b c : G) /-! ### Preimages under `x ↦ a + x` -/ @[simp] lemma preimage_const_add_Ici : (λ x, a + x) ⁻¹' (Ici b) = Ici (b - a) := ext $ λ x, sub_le_iff_le_add'.symm @[simp] lemma preimage_const_add_Ioi : (λ x, a + x) ⁻¹' (Ioi b) = Ioi (b - a) := ext $ λ x, sub_lt_iff_lt_add'.symm @[simp] lemma preimage_const_add_Iic : (λ x, a + x) ⁻¹' (Iic b) = Iic (b - a) := ext $ λ x, le_sub_iff_add_le'.symm @[simp] lemma preimage_const_add_Iio : (λ x, a + x) ⁻¹' (Iio b) = Iio (b - a) := ext $ λ x, lt_sub_iff_add_lt'.symm @[simp] lemma preimage_const_add_Icc : (λ x, a + x) ⁻¹' (Icc b c) = Icc (b - a) (c - a) := by simp [← Ici_inter_Iic] @[simp] lemma preimage_const_add_Ico : (λ x, a + x) ⁻¹' (Ico b c) = Ico (b - a) (c - a) := by simp [← Ici_inter_Iio] @[simp] lemma preimage_const_add_Ioc : (λ x, a + x) ⁻¹' (Ioc b c) = Ioc (b - a) (c - a) := by simp [← Ioi_inter_Iic] @[simp] lemma preimage_const_add_Ioo : (λ x, a + x) ⁻¹' (Ioo b c) = Ioo (b - a) (c - a) := by simp [← Ioi_inter_Iio] /-! ### Preimages under `x ↦ x + a` -/ @[simp] lemma preimage_add_const_Ici : (λ x, x + a) ⁻¹' (Ici b) = Ici (b - a) := ext $ λ x, sub_le_iff_le_add.symm @[simp] lemma preimage_add_const_Ioi : (λ x, x + a) ⁻¹' (Ioi b) = Ioi (b - a) := ext $ λ x, sub_lt_iff_lt_add.symm @[simp] lemma preimage_add_const_Iic : (λ x, x + a) ⁻¹' (Iic b) = Iic (b - a) := ext $ λ x, le_sub_iff_add_le.symm @[simp] lemma preimage_add_const_Iio : (λ x, x + a) ⁻¹' (Iio b) = Iio (b - a) := ext $ λ x, lt_sub_iff_add_lt.symm @[simp] lemma preimage_add_const_Icc : (λ x, x + a) ⁻¹' (Icc b c) = Icc (b - a) (c - a) := by simp [← Ici_inter_Iic] @[simp] lemma preimage_add_const_Ico : (λ x, x + a) ⁻¹' (Ico b c) = Ico (b - a) (c - a) := by simp [← Ici_inter_Iio] @[simp] lemma preimage_add_const_Ioc : (λ x, x + a) ⁻¹' (Ioc b c) = Ioc (b - a) (c - a) := by simp [← Ioi_inter_Iic] @[simp] lemma preimage_add_const_Ioo : (λ x, x + a) ⁻¹' (Ioo b c) = Ioo (b - a) (c - a) := by simp [← Ioi_inter_Iio] /-! ### Preimages under `x ↦ -x` -/ @[simp] lemma preimage_neg_Ici : - Ici a = Iic (-a) := ext $ λ x, le_neg @[simp] lemma preimage_neg_Iic : - Iic a = Ici (-a) := ext $ λ x, neg_le @[simp] lemma preimage_neg_Ioi : - Ioi a = Iio (-a) := ext $ λ x, lt_neg @[simp] lemma preimage_neg_Iio : - Iio a = Ioi (-a) := ext $ λ x, neg_lt @[simp] lemma preimage_neg_Icc : - Icc a b = Icc (-b) (-a) := by simp [← Ici_inter_Iic, inter_comm] @[simp] lemma preimage_neg_Ico : - Ico a b = Ioc (-b) (-a) := by simp [← Ici_inter_Iio, ← Ioi_inter_Iic, inter_comm] @[simp] lemma preimage_neg_Ioc : - Ioc a b = Ico (-b) (-a) := by simp [← Ioi_inter_Iic, ← Ici_inter_Iio, inter_comm] @[simp] lemma preimage_neg_Ioo : - Ioo a b = Ioo (-b) (-a) := by simp [← Ioi_inter_Iio, inter_comm] /-! ### Preimages under `x ↦ x - a` -/ @[simp] lemma preimage_sub_const_Ici : (λ x, x - a) ⁻¹' (Ici b) = Ici (b + a) := by simp [sub_eq_add_neg] @[simp] lemma preimage_sub_const_Ioi : (λ x, x - a) ⁻¹' (Ioi b) = Ioi (b + a) := by simp [sub_eq_add_neg] @[simp] lemma preimage_sub_const_Iic : (λ x, x - a) ⁻¹' (Iic b) = Iic (b + a) := by simp [sub_eq_add_neg] @[simp] lemma preimage_sub_const_Iio : (λ x, x - a) ⁻¹' (Iio b) = Iio (b + a) := by simp [sub_eq_add_neg] @[simp] lemma preimage_sub_const_Icc : (λ x, x - a) ⁻¹' (Icc b c) = Icc (b + a) (c + a) := by simp [sub_eq_add_neg] @[simp] lemma preimage_sub_const_Ico : (λ x, x - a) ⁻¹' (Ico b c) = Ico (b + a) (c + a) := by simp [sub_eq_add_neg] @[simp] lemma preimage_sub_const_Ioc : (λ x, x - a) ⁻¹' (Ioc b c) = Ioc (b + a) (c + a) := by simp [sub_eq_add_neg] @[simp] lemma preimage_sub_const_Ioo : (λ x, x - a) ⁻¹' (Ioo b c) = Ioo (b + a) (c + a) := by simp [sub_eq_add_neg] /-! ### Preimages under `x ↦ a - x` -/ @[simp] lemma preimage_const_sub_Ici : (λ x, a - x) ⁻¹' (Ici b) = Iic (a - b) := ext $ λ x, le_sub @[simp] lemma preimage_const_sub_Iic : (λ x, a - x) ⁻¹' (Iic b) = Ici (a - b) := ext $ λ x, sub_le @[simp] lemma preimage_const_sub_Ioi : (λ x, a - x) ⁻¹' (Ioi b) = Iio (a - b) := ext $ λ x, lt_sub @[simp] lemma preimage_const_sub_Iio : (λ x, a - x) ⁻¹' (Iio b) = Ioi (a - b) := ext $ λ x, sub_lt @[simp] lemma preimage_const_sub_Icc : (λ x, a - x) ⁻¹' (Icc b c) = Icc (a - c) (a - b) := by simp [← Ici_inter_Iic, inter_comm] @[simp] lemma preimage_const_sub_Ico : (λ x, a - x) ⁻¹' (Ico b c) = Ioc (a - c) (a - b) := by simp [← Ioi_inter_Iic, ← Ici_inter_Iio, inter_comm] @[simp] lemma preimage_const_sub_Ioc : (λ x, a - x) ⁻¹' (Ioc b c) = Ico (a - c) (a - b) := by simp [← Ioi_inter_Iic, ← Ici_inter_Iio, inter_comm] @[simp] lemma preimage_const_sub_Ioo : (λ x, a - x) ⁻¹' (Ioo b c) = Ioo (a - c) (a - b) := by simp [← Ioi_inter_Iio, inter_comm] /-! ### Images under `x ↦ a + x` -/ @[simp] lemma image_const_add_Ici : (λ x, a + x) '' Ici b = Ici (a + b) := by simp [add_comm] @[simp] lemma image_const_add_Iic : (λ x, a + x) '' Iic b = Iic (a + b) := by simp [add_comm] @[simp] lemma image_const_add_Iio : (λ x, a + x) '' Iio b = Iio (a + b) := by simp [add_comm] @[simp] lemma image_const_add_Ioi : (λ x, a + x) '' Ioi b = Ioi (a + b) := by simp [add_comm] @[simp] lemma image_const_add_Icc : (λ x, a + x) '' Icc b c = Icc (a + b) (a + c) := by simp [add_comm] @[simp] lemma image_const_add_Ico : (λ x, a + x) '' Ico b c = Ico (a + b) (a + c) := by simp [add_comm] @[simp] lemma image_const_add_Ioc : (λ x, a + x) '' Ioc b c = Ioc (a + b) (a + c) := by simp [add_comm] @[simp] lemma image_const_add_Ioo : (λ x, a + x) '' Ioo b c = Ioo (a + b) (a + c) := by simp [add_comm] /-! ### Images under `x ↦ x + a` -/ @[simp] lemma image_add_const_Ici : (λ x, x + a) '' Ici b = Ici (a + b) := by simp [add_comm] @[simp] lemma image_add_const_Iic : (λ x, x + a) '' Iic b = Iic (a + b) := by simp [add_comm] @[simp] lemma image_add_const_Iio : (λ x, x + a) '' Iio b = Iio (a + b) := by simp [add_comm] @[simp] lemma image_add_const_Ioi : (λ x, x + a) '' Ioi b = Ioi (a + b) := by simp [add_comm] @[simp] lemma image_add_const_Icc : (λ x, x + a) '' Icc b c = Icc (a + b) (a + c) := by simp [add_comm] @[simp] lemma image_add_const_Ico : (λ x, x + a) '' Ico b c = Ico (a + b) (a + c) := by simp [add_comm] @[simp] lemma image_add_const_Ioc : (λ x, x + a) '' Ioc b c = Ioc (a + b) (a + c) := by simp [add_comm] @[simp] lemma image_add_const_Ioo : (λ x, x + a) '' Ioo b c = Ioo (a + b) (a + c) := by simp [add_comm] /-! ### Images under `x ↦ -x` -/ lemma image_neg_Ici : has_neg.neg '' (Ici a) = Iic (-a) := by simp lemma image_neg_Iic : has_neg.neg '' (Iic a) = Ici (-a) := by simp lemma image_neg_Ioi : has_neg.neg '' (Ioi a) = Iio (-a) := by simp lemma image_neg_Iio : has_neg.neg '' (Iio a) = Ioi (-a) := by simp lemma image_neg_Icc : has_neg.neg '' (Icc a b) = Icc (-b) (-a) := by simp lemma image_neg_Ico : has_neg.neg '' (Ico a b) = Ioc (-b) (-a) := by simp lemma image_neg_Ioc : has_neg.neg '' (Ioc a b) = Ico (-b) (-a) := by simp lemma image_neg_Ioo : has_neg.neg '' (Ioo a b) = Ioo (-b) (-a) := by simp /-! ### Images under `x ↦ a - x` -/ @[simp] lemma image_const_sub_Ici : (λ x, a - x) '' Ici b = Iic (a - b) := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] @[simp] lemma image_const_sub_Iic : (λ x, a - x) '' Iic b = Ici (a - b) := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] @[simp] lemma image_const_sub_Ioi : (λ x, a - x) '' Ioi b = Iio (a - b) := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] @[simp] lemma image_const_sub_Iio : (λ x, a - x) '' Iio b = Ioi (a - b) := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] @[simp] lemma image_const_sub_Icc : (λ x, a - x) '' Icc b c = Icc (a - c) (a - b) := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] @[simp] lemma image_const_sub_Ico : (λ x, a - x) '' Ico b c = Ioc (a - c) (a - b) := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] @[simp] lemma image_const_sub_Ioc : (λ x, a - x) '' Ioc b c = Ico (a - c) (a - b) := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] @[simp] lemma image_const_sub_Ioo : (λ x, a - x) '' Ioo b c = Ioo (a - c) (a - b) := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] /-! ### Images under `x ↦ x - a` -/ @[simp] lemma image_sub_const_Ici : (λ x, x - a) '' Ici b = Ici (b - a) := by simp [sub_eq_neg_add] @[simp] lemma image_sub_const_Iic : (λ x, x - a) '' Iic b = Iic (b - a) := by simp [sub_eq_neg_add] @[simp] lemma image_sub_const_Ioi : (λ x, x - a) '' Ioi b = Ioi (b - a) := by simp [sub_eq_neg_add] @[simp] lemma image_sub_const_Iio : (λ x, x - a) '' Iio b = Iio (b - a) := by simp [sub_eq_neg_add] @[simp] lemma image_sub_const_Icc : (λ x, x - a) '' Icc b c = Icc (b - a) (c - a) := by simp [sub_eq_neg_add] @[simp] lemma image_sub_const_Ico : (λ x, x - a) '' Ico b c = Ico (b - a) (c - a) := by simp [sub_eq_neg_add] @[simp] lemma image_sub_const_Ioc : (λ x, x - a) '' Ioc b c = Ioc (b - a) (c - a) := by simp [sub_eq_neg_add] @[simp] lemma image_sub_const_Ioo : (λ x, x - a) '' Ioo b c = Ioo (b - a) (c - a) := by simp [sub_eq_neg_add] end ordered_add_comm_group /-! ### Multiplication and inverse in a field -/ section linear_ordered_field variables {k : Type u} [linear_ordered_field k] lemma image_mul_right_Icc' (a b : k) {c : k} (h : 0 < c) : (λ x, x * c) '' Icc a b = Icc (a * c) (b * c) := begin ext x, split, { rintros ⟨x, hx, rfl⟩, exact ⟨mul_le_mul_of_nonneg_right hx.1 (le_of_lt h), mul_le_mul_of_nonneg_right hx.2 (le_of_lt h)⟩ }, { intro hx, refine ⟨x / c, _, div_mul_cancel x (ne_of_gt h)⟩, exact ⟨le_div_of_mul_le h hx.1, div_le_of_le_mul h (mul_comm b c ▸ hx.2)⟩ } end lemma image_mul_right_Icc {a b c : k} (hab : a ≤ b) (hc : 0 ≤ c) : (λ x, x * c) '' Icc a b = Icc (a * c) (b * c) := begin cases eq_or_lt_of_le hc, { subst c, simp [(nonempty_Icc.2 hab).image_const] }, exact image_mul_right_Icc' a b ‹0 < c› end lemma image_mul_left_Icc' {a : k} (h : 0 < a) (b c : k) : ((*) a) '' Icc b c = Icc (a * b) (a * c) := by { convert image_mul_right_Icc' b c h using 1; simp only [mul_comm _ a] } lemma image_mul_left_Icc {a b c : k} (ha : 0 ≤ a) (hbc : b ≤ c) : ((*) a) '' Icc b c = Icc (a * b) (a * c) := by { convert image_mul_right_Icc hbc ha using 1; simp only [mul_comm _ a] } /-- The image under `inv` of `Ioo 0 a` is `Ioi a⁻¹`. -/ lemma image_inv_Ioo_0_left {a : k} (ha : 0 < a) : has_inv.inv '' Ioo 0 a = Ioi a⁻¹ := begin ext x, split, { rintros ⟨y, ⟨hy0, hya⟩, hyx⟩, exact hyx ▸ (inv_lt_inv ha hy0).2 hya }, { exact λ h, ⟨x⁻¹, ⟨inv_pos.2 (lt_trans (inv_pos.2 ha) h), (inv_lt ha (lt_trans (inv_pos.2 ha) h)).1 h⟩, inv_inv' x⟩ } end end linear_ordered_field end set
7829f0b03f7a4644c915ba12137fbaa6ae871b51
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/mathport18.lean
23991118f8fef6264e217336640643f185631dff
[ "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
189
lean
def foo := @id def bar := @id theorem foo_eq {α} (x : α) : foo x = bar x := rfl example {p : Nat → Prop} {x} (h : x = bar 1) : p x := by simp [*, ← foo_eq] show p (foo 1) admit
a1fdc0578a014486b09947c9903c9a4a098c7844
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/data/hashmap/default.lean
868f9566162aba8f4771bad561d683ccaef1acdb
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
202
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.data.hashmap.basic
35cdc88ce97d9529ce343c19915d37eea59bcd06
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/hott/class_loop.hlean
e0a11ce250d5cc11d3d3a77fadc34500ab0396dd
[ "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
605
hlean
constant (A : Type₁) constant (hom : A → A → Type₁) constant (id' : Πa, hom a a) structure is_iso [class] {a b : A} (f : hom a b) := (inverse : hom b a) open is_iso set_option pp.metavar_args true set_option pp.purify_metavars false definition inverse_id [instance] {a : A} : is_iso (id' a) := is_iso.mk (id' a) (id' a) definition inverse_is_iso [instance] {a b : A} (f : hom a b) (H : is_iso f) : is_iso (@inverse a b f H) := is_iso.mk (inverse f) f constant a : A set_option trace.class_instances true definition foo := inverse (id' a) set_option pp.implicit true print definition foo
72f0378637c52e8cf4a0ffbb334c82542224f1de
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/playground/cmdparsertest1.lean
31e661630caf6a1d64a6b7ab2413f1a361180e60
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
1,197
lean
import Init.Lean.Parser.Command open Lean open Lean.Parser def testParser (input : String) : IO Unit := do env ← mkEmptyEnvironment; cmdPTables ← builtinCommandParsingTable.get; stx ← IO.ofExcept $ runParser env cmdPTables input "<input>" "command"; IO.println stx def test (is : List String) : IO Unit := is.mfor $ fun input => do IO.println input; testParser input def main (xs : List String) : IO Unit := do IO.println Command.declaration.info.firstTokens; test [ "@[inline] def x := 2", "protected def length.{u} {α : Type u} : List α → Nat | [] := 0 | (a::as) := 1 + length as", "/-- doc string test -/ private theorem bla (x : Nat) : x = x := Eq.refl x", "class Alternative (f : Type u → Type v) extends Applicative f : Type (max (u+1) v) := (failure : ∀ {α : Type u}, f α) (orelse : ∀ {α : Type u}, f α → f α → f α) ", "local attribute [instance] foo bla", "attribute [inline] test", "open Lean (hiding Name)", "reserve infixr ` ∨ `:30", "reserve prefix `¬`:40", "infixr ` ^ ` := HasPow.pow", "notation f ` $ `:1 a:0 := f a", "notation `Prop` := Sort 0", "notation `∅` := HasEmptyc.emptyc _", "notation `⟦`:max a `⟧`:0 := Quotient.mk a" ]
d624f4b3f1056727ab522062da30550f02bdd9df
ebf7140a9ea507409ff4c994124fa36e79b4ae35
/src/exercises_sources/thursday/category_theory/exercise1.lean
7fb46cc0f38f225e27c3f2dd362ad5cc5f57d953
[]
no_license
fundou/lftcm2020
3e88d58a92755ea5dd49f19c36239c35286ecf5e
99d11bf3bcd71ffeaef0250caa08ecc46e69b55b
refs/heads/master
1,685,610,799,304
1,624,070,416,000
1,624,070,416,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
718
lean
import category_theory.isomorphism import category_theory.yoneda open category_theory open opposite variables {C : Type*} [category C] /-! Hint 1: `yoneda` is set up so that `(yoneda.obj X).obj (op Y) = (Y ⟶ X)` (we need to write `op Y` to explicitly move `Y` to the opposite category). -/ /-! Hint 2: If you have a natural isomorphism `α : F ≅ G`, you can access * the forward natural transformation as `α.hom` * the backwards natural transformation as `α.inv` * the component at `X`, as an isomorphism `F.obj X ≅ G.obj X` as `α.app X`. -/ def iso_of_hom_iso (X Y : C) (h : yoneda.obj X ≅ yoneda.obj Y) : X ≅ Y := sorry /-! There are some further hints in `hints/category_theory/exercise1/` -/
4ed16c763823e3de30b696e7a61836cb87cae68d
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/linear_algebra/multilinear.lean
dc56bebe045f1b35dd3761acfd5e1b13c3115613
[ "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
34,208
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import linear_algebra.basic import tactic.omega import data.fintype.sort /-! # Multilinear maps We define multilinear maps as maps from `Π(i : ι), M₁ i` to `M₂` which are linear in each coordinate. Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type (although some statements will require it to be a fintype). This space, denoted by `multilinear_map R M₁ M₂`, inherits a module structure by pointwise addition and multiplication. ## Main definitions * `multilinear_map R M₁ M₂` is the space of multilinear maps from `Π(i : ι), M₁ i` to `M₂`. * `f.map_smul` is the multiplicativity of the multilinear map `f` along each coordinate. * `f.map_add` is the additivity of the multilinear map `f` along each coordinate. * `f.map_smul_univ` expresses the multiplicativity of `f` over all coordinates at the same time, writing `f (λi, c i • m i)` as `(∏ i, c i) • f m`. * `f.map_add_univ` expresses the additivity of `f` over all coordinates at the same time, writing `f (m + m')` as the sum over all subsets `s` of `ι` of `f (s.piecewise m m')`. * `f.map_sum` expresses `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` as the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all possible functions. We also register isomorphisms corresponding to currying or uncurrying variables, transforming a multilinear function `f` on `n+1` variables into a linear function taking values in multilinear functions in `n` variables, and into a multilinear function in `n` variables taking values in linear functions. These operations are called `f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and `f.uncurry_right`). These operations induce linear equivalences between spaces of multilinear functions in `n+1` variables and spaces of linear functions into multilinear functions in `n` variables (resp. multilinear functions in `n` variables taking values in linear functions), called respectively `multilinear_curry_left_equiv` and `multilinear_curry_right_equiv`. ## Implementation notes Expressing that a map is linear along the `i`-th coordinate when all other coordinates are fixed can be done in two (equivalent) different ways: * fixing a vector `m : Π(j : ι - i), M₁ j.val`, and then choosing separately the `i`-th coordinate * fixing a vector `m : Πj, M₁ j`, and then modifying its `i`-th coordinate The second way is more artificial as the value of `m` at `i` is not relevant, but it has the advantage of avoiding subtype inclusion issues. This is the definition we use, based on `function.update` that allows to change the value of `m` at `i`. -/ open function fin set open_locale big_operators universes u v v' v₁ v₂ v₃ w u' variables {R : Type u} {ι : Type u'} {n : ℕ} {M : fin n.succ → Type v} {M₁ : ι → Type v₁} {M₂ : Type v₂} {M₃ : Type v₃} {M' : Type v'} [decidable_eq ι] /-- Multilinear maps over the ring `R`, from `Πi, M₁ i` to `M₂` where `M₁ i` and `M₂` are modules over `R`. -/ structure multilinear_map (R : Type u) {ι : Type u'} (M₁ : ι → Type v) (M₂ : Type w) [decidable_eq ι] [semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [∀i, semimodule R (M₁ i)] [semimodule R M₂] := (to_fun : (Πi, M₁ i) → M₂) (map_add' : ∀(m : Πi, M₁ i) (i : ι) (x y : M₁ i), to_fun (update m i (x + y)) = to_fun (update m i x) + to_fun (update m i y)) (map_smul' : ∀(m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i), to_fun (update m i (c • x)) = c • to_fun (update m i x)) namespace multilinear_map section semiring variables [semiring R] [∀i, add_comm_monoid (M i)] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M'] [∀i, semimodule R (M i)] [∀i, semimodule R (M₁ i)] [semimodule R M₂] [semimodule R M₃] [semimodule R M'] (f f' : multilinear_map R M₁ M₂) instance : has_coe_to_fun (multilinear_map R M₁ M₂) := ⟨_, to_fun⟩ @[ext] theorem ext {f f' : multilinear_map R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' := by cases f; cases f'; congr'; exact funext H @[simp] lemma map_add (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x + y)) = f (update m i x) + f (update m i y) := f.map_add' m i x y @[simp] lemma map_smul (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) : f (update m i (c • x)) = c • f (update m i x) := f.map_smul' m i c x lemma map_coord_zero {m : Πi, M₁ i} (i : ι) (h : m i = 0) : f m = 0 := begin have : (0 : R) • (0 : M₁ i) = 0, by simp, rw [← update_eq_self i m, h, ← this, f.map_smul, zero_smul] end @[simp] lemma map_zero [nonempty ι] : f 0 = 0 := begin obtain ⟨i, _⟩ : ∃i:ι, i ∈ set.univ := set.exists_mem_of_nonempty ι, exact map_coord_zero f i rfl end instance : has_add (multilinear_map R M₁ M₂) := ⟨λf f', ⟨λx, f x + f' x, λm i x y, by simp [add_left_comm, add_assoc], λm i c x, by simp [smul_add]⟩⟩ @[simp] lemma add_apply (m : Πi, M₁ i) : (f + f') m = f m + f' m := rfl instance : has_zero (multilinear_map R M₁ M₂) := ⟨⟨λ _, 0, λm i x y, by simp, λm i c x, by simp⟩⟩ instance : inhabited (multilinear_map R M₁ M₂) := ⟨0⟩ @[simp] lemma zero_apply (m : Πi, M₁ i) : (0 : multilinear_map R M₁ M₂) m = 0 := rfl instance : add_comm_monoid (multilinear_map R M₁ M₂) := by refine {zero := 0, add := (+), ..}; intros; ext; simp [add_comm, add_left_comm] @[simp] lemma sum_apply {α : Type*} (f : α → multilinear_map R M₁ M₂) (m : Πi, M₁ i) : ∀ {s : finset α}, (∑ a in s, f a) m = ∑ a in s, f a m := begin classical, apply finset.induction, { rw finset.sum_empty, simp }, { assume a s has H, rw finset.sum_insert has, simp [H, has] } end /-- If `f` is a multilinear map, then `f.to_linear_map m i` is the linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/ def to_linear_map (m : Πi, M₁ i) (i : ι) : M₁ i →ₗ[R] M₂ := { to_fun := λx, f (update m i x), map_add' := λx y, by simp, map_smul' := λc x, by simp } /-- The cartesian product of two multilinear maps, as a multilinear map. -/ def prod (f : multilinear_map R M₁ M₂) (g : multilinear_map R M₁ M₃) : multilinear_map R M₁ (M₂ × M₃) := { to_fun := λ m, (f m, g m), map_add' := λ m i x y, by simp, map_smul' := λ m i c x, by simp } /-- Given a multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset `s` of `k` of these variables, one gets a new multilinear map on `fin k` by varying these variables, and fixing the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/ noncomputable def restr {k n : ℕ} (f : multilinear_map R (λ i : fin n, M') M₂) (s : finset (fin n)) (hk : s.card = k) (z : M') : multilinear_map R (λ i : fin k, M') M₂ := { to_fun := λ v, f (λ j, if h : j ∈ s then v ((s.mono_equiv_of_fin hk).symm ⟨j, h⟩) else z), map_add' := λ v i x y, by { erw [dite_comp_equiv_update, dite_comp_equiv_update, dite_comp_equiv_update], simp }, map_smul' := λ v i c x, by { erw [dite_comp_equiv_update, dite_comp_equiv_update], simp } } variable {R} /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the additivity of a multilinear map along the first variable. -/ lemma cons_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (x y : M 0) : f (cons (x+y) m) = f (cons x m) + f (cons y m) := by rw [← update_cons_zero x m (x+y), f.map_add, update_cons_zero, update_cons_zero] /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ lemma cons_smul (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (c : R) (x : M 0) : f (cons (c • x) m) = c • f (cons x m) := by rw [← update_cons_zero x m (c • x), f.map_smul, update_cons_zero] /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `snoc`, one can express directly the additivity of a multilinear map along the first variable. -/ lemma snoc_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x y : M (last n)) : f (snoc m (x+y)) = f (snoc m x) + f (snoc m y) := by rw [← update_snoc_last x m (x+y), f.map_add, update_snoc_last, update_snoc_last] /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ lemma snoc_smul (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (c : R) (x : M (last n)) : f (snoc m (c • x)) = c • f (snoc m x) := by rw [← update_snoc_last x m (c • x), f.map_smul, update_snoc_last] section variables {M₁' : ι → Type*} [Π i, add_comm_monoid (M₁' i)] [Π i, semimodule R (M₁' i)] /-- If `g` is a multilinear map and `f` is a collection of linear maps, then `g (f₁ m₁, ..., fₙ mₙ)` is again a multilinear map, that we call `g.comp_linear_map f`. -/ def comp_linear_map (g : multilinear_map R M₁' M₂) (f : Π i, M₁ i →ₗ[R] M₁' i) : multilinear_map R M₁ M₂ := { to_fun := λ m, g $ λ i, f i (m i), map_add' := λ m i x y, have ∀ j z, f j (update m i z j) = update (λ k, f k (m k)) i (f i z) j := λ j z, function.apply_update (λ k, f k) _ _ _ _, by simp [this], map_smul' := λ m i c x, have ∀ j z, f j (update m i z j) = update (λ k, f k (m k)) i (f i z) j := λ j z, function.apply_update (λ k, f k) _ _ _ _, by simp [this] } @[simp] lemma comp_linear_map_apply (g : multilinear_map R M₁' M₂) (f : Π i, M₁ i →ₗ[R] M₁' i) (m : Π i, M₁ i) : g.comp_linear_map f m = g (λ i, f i (m i)) := rfl end /-- If one adds to a vector `m'` another vector `m`, but only for coordinates in a finset `t`, then the image under a multilinear map `f` is the sum of `f (s.piecewise m m')` along all subsets `s` of `t`. This is mainly an auxiliary statement to prove the result when `t = univ`, given in `map_add_univ`, although it can be useful in its own right as it does not require the index set `ι` to be finite.-/ lemma map_piecewise_add (m m' : Πi, M₁ i) (t : finset ι) : f (t.piecewise (m + m') m') = ∑ s in t.powerset, f (s.piecewise m m') := begin revert m', refine finset.induction_on t (by simp) _, assume i t hit Hrec m', have A : (insert i t).piecewise (m + m') m' = update (t.piecewise (m + m') m') i (m i + m' i) := t.piecewise_insert _ _ _, have B : update (t.piecewise (m + m') m') i (m' i) = t.piecewise (m + m') m', { ext j, by_cases h : j = i, { rw h, simp [hit] }, { simp [h] } }, let m'' := update m' i (m i), have C : update (t.piecewise (m + m') m') i (m i) = t.piecewise (m + m'') m'', { ext j, by_cases h : j = i, { rw h, simp [m'', hit] }, { by_cases h' : j ∈ t; simp [h, hit, m'', h'] } }, rw [A, f.map_add, B, C, finset.sum_powerset_insert hit, Hrec, Hrec, add_comm], congr' 1, apply finset.sum_congr rfl (λs hs, _), have : (insert i s).piecewise m m' = s.piecewise m m'', { ext j, by_cases h : j = i, { rw h, simp [m'', finset.not_mem_of_mem_powerset_of_not_mem hs hit] }, { by_cases h' : j ∈ s; simp [h, m'', h'] } }, rw this end /-- Additivity of a multilinear map along all coordinates at the same time, writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/ lemma map_add_univ [fintype ι] (m m' : Πi, M₁ i) : f (m + m') = ∑ s : finset ι, f (s.piecewise m m') := by simpa using f.map_piecewise_add m m' finset.univ section apply_sum variables {α : ι → Type*} [fintype ι] (g : Π i, α i → M₁ i) (A : Π i, finset (α i)) open_locale classical open fintype finset /-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each coordinate. Here, we give an auxiliary statement tailored for an inductive proof. Use instead `map_sum_finset`. -/ lemma map_sum_finset_aux {n : ℕ} (h : ∑ i, (A i).card = n) : f (λ i, ∑ j in A i, g i j) = ∑ r in pi_finset A, f (λ i, g i (r i)) := begin induction n using nat.strong_induction_on with n IH generalizing A, -- If one of the sets is empty, then all the sums are zero by_cases Ai_empty : ∃ i, A i = ∅, { rcases Ai_empty with ⟨i, hi⟩, have : ∑ j in A i, g i j = 0, by convert sum_empty, rw f.map_coord_zero i this, have : pi_finset A = ∅, { apply finset.eq_empty_of_forall_not_mem (λ r hr, _), have : r i ∈ A i := mem_pi_finset.mp hr i, rwa hi at this }, convert sum_empty.symm }, push_neg at Ai_empty, -- Otherwise, if all sets are at most singletons, then they are exactly singletons and the result -- is again straightforward by_cases Ai_singleton : ∀ i, (A i).card ≤ 1, { have Ai_card : ∀ i, (A i).card = 1, { assume i, have : finset.card (A i) ≠ 0, by simp [finset.card_eq_zero, Ai_empty i], have : finset.card (A i) ≤ 1 := Ai_singleton i, omega }, have : ∀ (r : Π i, α i), r ∈ pi_finset A → f (λ i, g i (r i)) = f (λ i, ∑ j in A i, g i j), { assume r hr, unfold_coes, congr' with i, have : ∀ j ∈ A i, g i j = g i (r i), { assume j hj, congr, apply finset.card_le_one_iff.1 (Ai_singleton i) hj, exact mem_pi_finset.mp hr i }, simp only [finset.sum_congr rfl this, finset.mem_univ, finset.sum_const, Ai_card i, one_nsmul] }, simp only [sum_congr rfl this, Ai_card, card_pi_finset, prod_const_one, one_nsmul, sum_const] }, -- Remains the interesting case where one of the `A i`, say `A i₀`, has cardinality at least 2. -- We will split into two parts `B i₀` and `C i₀` of smaller cardinality, let `B i = C i = A i` -- for `i ≠ i₀`, apply the inductive assumption to `B` and `C`, and add up the corresponding -- parts to get the sum for `A`. push_neg at Ai_singleton, obtain ⟨i₀, hi₀⟩ : ∃ i, 1 < (A i).card := Ai_singleton, obtain ⟨j₁, j₂, hj₁, hj₂, j₁_ne_j₂⟩ : ∃ j₁ j₂, (j₁ ∈ A i₀) ∧ (j₂ ∈ A i₀) ∧ j₁ ≠ j₂ := finset.one_lt_card_iff.1 hi₀, let B := function.update A i₀ (A i₀ \ {j₂}), let C := function.update A i₀ {j₂}, have B_subset_A : ∀ i, B i ⊆ A i, { assume i, by_cases hi : i = i₀, { rw hi, simp only [B, sdiff_subset, update_same]}, { simp only [hi, B, update_noteq, ne.def, not_false_iff, finset.subset.refl] } }, have C_subset_A : ∀ i, C i ⊆ A i, { assume i, by_cases hi : i = i₀, { rw hi, simp only [C, hj₂, finset.singleton_subset_iff, update_same] }, { simp only [hi, C, update_noteq, ne.def, not_false_iff, finset.subset.refl] } }, -- split the sum at `i₀` as the sum over `B i₀` plus the sum over `C i₀`, to use additivity. have A_eq_BC : (λ i, ∑ j in A i, g i j) = function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in B i₀, g i₀ j + ∑ j in C i₀, g i₀ j), { ext i, by_cases hi : i = i₀, { rw [hi], simp only [function.update_same], have : A i₀ = B i₀ ∪ C i₀, { simp only [B, C, function.update_same, finset.sdiff_union_self_eq_union], symmetry, simp only [hj₂, finset.singleton_subset_iff, union_eq_left_iff_subset] }, rw this, apply finset.sum_union, apply finset.disjoint_right.2 (λ j hj, _), have : j = j₂, by { dsimp [C] at hj, simpa using hj }, rw this, dsimp [B], simp only [mem_sdiff, eq_self_iff_true, not_true, not_false_iff, finset.mem_singleton, update_same, and_false] }, { simp [hi] } }, have Beq : function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in B i₀, g i₀ j) = (λ i, ∑ j in B i, g i j), { ext i, by_cases hi : i = i₀, { rw hi, simp only [update_same] }, { simp only [hi, B, update_noteq, ne.def, not_false_iff] } }, have Ceq : function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in C i₀, g i₀ j) = (λ i, ∑ j in C i, g i j), { ext i, by_cases hi : i = i₀, { rw hi, simp only [update_same] }, { simp only [hi, C, update_noteq, ne.def, not_false_iff] } }, -- Express the inductive assumption for `B` have Brec : f (λ i, ∑ j in B i, g i j) = ∑ r in pi_finset B, f (λ i, g i (r i)), { have : ∑ i, finset.card (B i) < ∑ i, finset.card (A i), { refine finset.sum_lt_sum (λ i hi, finset.card_le_of_subset (B_subset_A i)) ⟨i₀, finset.mem_univ _, _⟩, have : {j₂} ⊆ A i₀, by simp [hj₂], simp only [B, finset.card_sdiff this, function.update_same, finset.card_singleton], exact nat.pred_lt (ne_of_gt (lt_trans nat.zero_lt_one hi₀)) }, rw h at this, exact IH _ this B rfl }, -- Express the inductive assumption for `C` have Crec : f (λ i, ∑ j in C i, g i j) = ∑ r in pi_finset C, f (λ i, g i (r i)), { have : ∑ i, finset.card (C i) < ∑ i, finset.card (A i) := finset.sum_lt_sum (λ i hi, finset.card_le_of_subset (C_subset_A i)) ⟨i₀, finset.mem_univ _, by simp [C, hi₀]⟩, rw h at this, exact IH _ this C rfl }, have D : disjoint (pi_finset B) (pi_finset C), { have : disjoint (B i₀) (C i₀), by simp [B, C], exact pi_finset_disjoint_of_disjoint B C this }, have pi_BC : pi_finset A = pi_finset B ∪ pi_finset C, { apply finset.subset.antisymm, { assume r hr, by_cases hri₀ : r i₀ = j₂, { apply finset.mem_union_right, apply mem_pi_finset.2 (λ i, _), by_cases hi : i = i₀, { have : r i₀ ∈ C i₀, by simp [C, hri₀], convert this }, { simp [C, hi, mem_pi_finset.1 hr i] } }, { apply finset.mem_union_left, apply mem_pi_finset.2 (λ i, _), by_cases hi : i = i₀, { have : r i₀ ∈ B i₀, by simp [B, hri₀, mem_pi_finset.1 hr i₀], convert this }, { simp [B, hi, mem_pi_finset.1 hr i] } } }, { exact finset.union_subset (pi_finset_subset _ _ (λ i, B_subset_A i)) (pi_finset_subset _ _ (λ i, C_subset_A i)) } }, rw A_eq_BC, simp only [multilinear_map.map_add, Beq, Ceq, Brec, Crec, pi_BC], rw ← finset.sum_union D, end /-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum_finset : f (λ i, ∑ j in A i, g i j) = ∑ r in pi_finset A, f (λ i, g i (r i)) := f.map_sum_finset_aux _ _ rfl /-- If `f` is multilinear, then `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions `r`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum [∀ i, fintype (α i)] : f (λ i, ∑ j, g i j) = ∑ r : Π i, α i, f (λ i, g i (r i)) := f.map_sum_finset g (λ i, finset.univ) end apply_sum end semiring section comm_semiring variables [comm_semiring R] [∀i, add_comm_monoid (M₁ i)] [∀i, add_comm_monoid (M i)] [add_comm_monoid M₂] [∀i, semimodule R (M i)] [∀i, semimodule R (M₁ i)] [semimodule R M₂] (f f' : multilinear_map R M₁ M₂) /-- If one multiplies by `c i` the coordinates in a finset `s`, then the image under a multilinear map is multiplied by `∏ i in s, c i`. This is mainly an auxiliary statement to prove the result when `s = univ`, given in `map_smul_univ`, although it can be useful in its own right as it does not require the index set `ι` to be finite. -/ lemma map_piecewise_smul (c : ι → R) (m : Πi, M₁ i) (s : finset ι) : f (s.piecewise (λi, c i • m i) m) = (∏ i in s, c i) • f m := begin refine s.induction_on (by simp) _, assume j s j_not_mem_s Hrec, have A : function.update (s.piecewise (λi, c i • m i) m) j (m j) = s.piecewise (λi, c i • m i) m, { ext i, by_cases h : i = j, { rw h, simp [j_not_mem_s] }, { simp [h] } }, rw [s.piecewise_insert, f.map_smul, A, Hrec], simp [j_not_mem_s, mul_smul] end /-- Multiplicativity of a multilinear map along all coordinates at the same time, writing `f (λi, c i • m i)` as `(∏ i, c i) • f m`. -/ lemma map_smul_univ [fintype ι] (c : ι → R) (m : Πi, M₁ i) : f (λi, c i • m i) = (∏ i, c i) • f m := by simpa using map_piecewise_smul f c m finset.univ instance : has_scalar R (multilinear_map R M₁ M₂) := ⟨λ c f, ⟨λ m, c • f m, λm i x y, by simp [smul_add], λl i x d, by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (c : R) (m : Πi, M₁ i) : (c • f) m = c • f m := rfl variables (R ι) /-- The canonical multilinear map on `R^ι` when `ι` is finite, associating to `m` the product of all the `m i` (multiplied by a fixed reference element `z` in the target module) -/ protected def mk_pi_ring [fintype ι] (z : M₂) : multilinear_map R (λ(i : ι), R) M₂ := { to_fun := λm, (∏ i, m i) • z, map_add' := λ m i x y, by simp [finset.prod_update_of_mem, add_mul, add_smul], map_smul' := λ m i c x, by { rw [smul_eq_mul], simp [finset.prod_update_of_mem, smul_smul, mul_assoc] } } variables {R ι} @[simp] lemma mk_pi_ring_apply [fintype ι] (z : M₂) (m : ι → R) : (multilinear_map.mk_pi_ring R ι z : (ι → R) → M₂) m = (∏ i, m i) • z := rfl lemma mk_pi_ring_apply_one_eq_self [fintype ι] (f : multilinear_map R (λ(i : ι), R) M₂) : multilinear_map.mk_pi_ring R ι (f (λi, 1)) = f := begin ext m, have : m = (λi, m i • 1), by { ext j, simp }, conv_rhs { rw [this, f.map_smul_univ] }, refl end end comm_semiring section ring variables [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, semimodule R (M₁ i)] [semimodule R M₂] (f : multilinear_map R M₁ M₂) @[simp] lemma map_sub (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x - y)) = f (update m i x) - f (update m i y) := by { simp only [map_add, add_left_inj, sub_eq_add_neg, (neg_one_smul R y).symm, map_smul], simp } instance : has_neg (multilinear_map R M₁ M₂) := ⟨λ f, ⟨λ m, - f m, λm i x y, by simp [add_comm], λm i c x, by simp⟩⟩ @[simp] lemma neg_apply (m : Πi, M₁ i) : (-f) m = - (f m) := rfl instance : add_comm_group (multilinear_map R M₁ M₂) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp [add_comm, add_left_comm] end ring section comm_ring variables [comm_ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, semimodule R (M₁ i)] [semimodule R M₂] variables (R ι M₁ M₂) /-- The space of multilinear maps is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance semimodule : semimodule R (multilinear_map R M₁ M₂) := semimodule.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] -- This instance should not be needed! instance semimodule_ring : semimodule R (multilinear_map R (λ (i : ι), R) M₂) := multilinear_map.semimodule _ _ (λ (i : ι), R) _ /-- When `ι` is finite, multilinear maps on `R^ι` with values in `M₂` are in bijection with `M₂`, as such a multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a linear equivalence in `multilinear_map.pi_ring_equiv`. -/ protected def pi_ring_equiv [fintype ι] : M₂ ≃ₗ[R] (multilinear_map R (λ(i : ι), R) M₂) := { to_fun := λ z, multilinear_map.mk_pi_ring R ι z, inv_fun := λ f, f (λi, 1), map_add' := λ z z', by { ext m, simp [smul_add] }, map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] }, left_inv := λ z, by simp, right_inv := λ f, f.mk_pi_ring_apply_one_eq_self } end comm_ring end multilinear_map namespace linear_map variables [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [add_comm_group M₃] [∀i, module R (M₁ i)] [module R M₂] [module R M₃] /-- Composing a multilinear map with a linear map gives again a multilinear map. -/ def comp_multilinear_map (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) : multilinear_map R M₁ M₃ := { to_fun := λ m, g (f m), map_add' := λ m i x y, by simp, map_smul' := λ m i c x, by simp } end linear_map section currying /-! ### Currying We associate to a multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two curried functions, named `f.curry_left` (which is a linear map on `E 0` taking values in multilinear maps in `n` variables) and `f.curry_right` (wich is a multilinear map in `n` variables taking values in linear maps on `E 0`). In both constructions, the variable that is singled out is `0`, to take advantage of the operations `cons` and `tail` on `fin n`. The inverse operations are called `uncurry_left` and `uncurry_right`. We also register linear equiv versions of these correspondences, in `multilinear_curry_left_equiv` and `multilinear_curry_right_equiv`. -/ open multilinear_map variables {R M M₂} [comm_ring R] [∀i, add_comm_group (M i)] [add_comm_group M'] [add_comm_group M₂] [∀i, module R (M i)] [module R M'] [module R M₂] /-! #### Left currying -/ /-- Given a linear map `f` from `M 0` to multilinear maps on `n` variables, construct the corresponding multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (m 0) (tail m)`-/ def linear_map.uncurry_left (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) : multilinear_map R M M₂ := { to_fun := λm, f (m 0) (tail m), map_add' := λm i x y, begin by_cases h : i = 0, { revert x y, rw h, assume x y, rw [update_same, update_same, update_same, f.map_add, add_apply, tail_update_zero, tail_update_zero, tail_update_zero] }, { rw [update_noteq (ne.symm h), update_noteq (ne.symm h), update_noteq (ne.symm h)], revert x y, rw ← succ_pred i h, assume x y, rw [tail_update_succ, map_add, tail_update_succ, tail_update_succ] } end, map_smul' := λm i c x, begin by_cases h : i = 0, { revert x, rw h, assume x, rw [update_same, update_same, tail_update_zero, tail_update_zero, ← smul_apply, f.map_smul] }, { rw [update_noteq (ne.symm h), update_noteq (ne.symm h)], revert x, rw ← succ_pred i h, assume x, rw [tail_update_succ, tail_update_succ, map_smul] } end } @[simp] lemma linear_map.uncurry_left_apply (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) (m : Πi, M i) : f.uncurry_left m = f (m 0) (tail m) := rfl /-- Given a multilinear map `f` in `n+1` variables, split the first variable to obtain a linear map into multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/ def multilinear_map.curry_left (f : multilinear_map R M M₂) : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂) := { to_fun := λx, { to_fun := λm, f (cons x m), map_add' := λm i y y', by simp, map_smul' := λm i y c, by simp }, map_add' := λx y, by { ext m, exact cons_add f m x y }, map_smul' := λc x, by { ext m, exact cons_smul f m c x } } @[simp] lemma multilinear_map.curry_left_apply (f : multilinear_map R M M₂) (x : M 0) (m : Π(i : fin n), M i.succ) : f.curry_left x m = f (cons x m) := rfl @[simp] lemma linear_map.curry_uncurry_left (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) : f.uncurry_left.curry_left = f := begin ext m x, simp only [tail_cons, linear_map.uncurry_left_apply, multilinear_map.curry_left_apply], rw cons_zero end @[simp] lemma multilinear_map.uncurry_curry_left (f : multilinear_map R M M₂) : f.curry_left.uncurry_left = f := by { ext m, simp } variables (R M M₂) /-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to the space of linear maps from `M 0` to the space of multilinear maps on `Π(i : fin n), M i.succ `, by separating the first variable. We register this isomorphism as a linear isomorphism in `multilinear_curry_left_equiv R M M₂`. The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these unless you need the full framework of linear equivs. -/ def multilinear_curry_left_equiv : (M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) ≃ₗ[R] (multilinear_map R M M₂) := { to_fun := linear_map.uncurry_left, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, refl }, inv_fun := multilinear_map.curry_left, left_inv := linear_map.curry_uncurry_left, right_inv := multilinear_map.uncurry_curry_left } variables {R M M₂} /-! #### Right currying -/ /-- Given a multilinear map `f` in `n` variables to the space of linear maps from `M (last n)` to `M₂`, construct the corresponding multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`-/ def multilinear_map.uncurry_right (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) (M (last n) →ₗ[R] M₂))) : multilinear_map R M M₂ := { to_fun := λm, f (init m) (m (last n)), map_add' := λm i x y, begin by_cases h : i.val < n, { have : last n ≠ i := ne.symm (ne_of_lt h), rw [update_noteq this, update_noteq this, update_noteq this], revert x y, rw [(cast_succ_cast_lt i h).symm], assume x y, rw [init_update_cast_succ, map_add, init_update_cast_succ, init_update_cast_succ, linear_map.add_apply] }, { revert x y, rw eq_last_of_not_lt h, assume x y, rw [init_update_last, init_update_last, init_update_last, update_same, update_same, update_same, linear_map.map_add] } end, map_smul' := λm i c x, begin by_cases h : i.val < n, { have : last n ≠ i := ne.symm (ne_of_lt h), rw [update_noteq this, update_noteq this], revert x, rw [(cast_succ_cast_lt i h).symm], assume x, rw [init_update_cast_succ, init_update_cast_succ, map_smul, linear_map.smul_apply] }, { revert x, rw eq_last_of_not_lt h, assume x, rw [update_same, update_same, init_update_last, init_update_last, linear_map.map_smul] } end } @[simp] lemma multilinear_map.uncurry_right_apply (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) (m : Πi, M i) : f.uncurry_right m = f (init m) (m (last n)) := rfl /-- Given a multilinear map `f` in `n+1` variables, split the last variable to obtain a multilinear map in `n` variables taking values in linear maps from `M (last n)` to `M₂`, given by `m ↦ (x ↦ f (snoc m x))`. -/ def multilinear_map.curry_right (f : multilinear_map R M M₂) : multilinear_map R (λ(i : fin n), M (fin.cast_succ i)) ((M (last n)) →ₗ[R] M₂) := { to_fun := λm, { to_fun := λx, f (snoc m x), map_add' := λx y, by rw f.snoc_add, map_smul' := λc x, by rw f.snoc_smul }, map_add' := λm i x y, begin ext z, change f (snoc (update m i (x + y)) z) = f (snoc (update m i x) z) + f (snoc (update m i y) z), rw [snoc_update, snoc_update, snoc_update, f.map_add] end, map_smul' := λm i c x, begin ext z, change f (snoc (update m i (c • x)) z) = c • f (snoc (update m i x) z), rw [snoc_update, snoc_update, f.map_smul] end } @[simp] lemma multilinear_map.curry_right_apply (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x : M (last n)) : f.curry_right m x = f (snoc m x) := rfl @[simp] lemma multilinear_map.curry_uncurry_right (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) : f.uncurry_right.curry_right = f := begin ext m x, simp only [snoc_last, multilinear_map.curry_right_apply, multilinear_map.uncurry_right_apply], rw init_snoc end @[simp] lemma multilinear_map.uncurry_curry_right (f : multilinear_map R M M₂) : f.curry_right.uncurry_right = f := by { ext m, simp } variables (R M M₂) /-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to the space of linear maps from the space of multilinear maps on `Π(i : fin n), M i.cast_succ` to the space of linear maps on `M (last n)`, by separating the last variable. We register this isomorphism as a linear isomorphism in `multilinear_curry_right_equiv R M M₂`. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear equivs. -/ def multilinear_curry_right_equiv : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂)) ≃ₗ[R] (multilinear_map R M M₂) := { to_fun := multilinear_map.uncurry_right, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, rw [smul_apply], refl }, inv_fun := multilinear_map.curry_right, left_inv := multilinear_map.curry_uncurry_right, right_inv := multilinear_map.uncurry_curry_right } end currying
c568298420c81fddde9809c4537918e88b91c82b
fffbc47930dc6615e66ece42324ce57a21d5b64b
/src/category_theory/instances/CommRing/basic.lean
1ff3904d92664a3d54b77441d2cb6b070352e689
[ "Apache-2.0" ]
permissive
skbaek/mathlib
3caae8ae413c66862293a95fd2fbada3647b1228
f25340175631cdc85ad768a262433f968d0d6450
refs/heads/master
1,588,130,123,636
1,558,287,609,000
1,558,287,609,000
160,935,713
0
0
Apache-2.0
1,544,271,146,000
1,544,271,146,000
null
UTF-8
Lean
false
false
3,376
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl Introduce CommRing -- the category of commutative rings. -/ import category_theory.instances.Mon.basic import category_theory.fully_faithful import algebra.ring import data.int.basic universes u v open category_theory namespace category_theory.instances /-- The category of rings. -/ @[reducible] def Ring : Type (u+1) := bundled ring instance (x : Ring) : ring x := x.str instance concrete_is_ring_hom : concrete_category @is_ring_hom := ⟨by introsI α ia; apply_instance, by introsI α β γ ia ib ic f g hf hg; apply_instance⟩ instance Ring.hom_is_ring_hom {R S : Ring} (f : R ⟶ S) : is_ring_hom (f : R → S) := f.2 /-- The category of commutative rings. -/ @[reducible] def CommRing : Type (u+1) := bundled comm_ring instance (x : CommRing) : comm_ring x := x.str -- Here we don't use the `concrete` machinery, -- because it would require introducing a useless synonym for `is_ring_hom`. instance : category CommRing := { hom := λ R S, { f : R → S // is_ring_hom f }, id := λ R, ⟨ id, by resetI; apply_instance ⟩, comp := λ R S T g h, ⟨ h.1 ∘ g.1, begin haveI := g.2, haveI := h.2, apply_instance end ⟩ } namespace CommRing variables {R S T : CommRing.{u}} def of (α : Type u) [comm_ring α] : CommRing := ⟨α, by apply_instance⟩ @[simp] lemma id_val : subtype.val (𝟙 R) = id := rfl @[simp] lemma comp_val (f : R ⟶ S) (g : S ⟶ T) : (f ≫ g).val = g.val ∘ f.val := rfl instance hom_coe : has_coe_to_fun (R ⟶ S) := { F := λ f, R → S, coe := λ f, f.1 } @[extensionality] lemma hom.ext {f g : R ⟶ S} : (∀ x : R, f x = g x) → f = g := λ w, subtype.ext.2 $ funext w @[simp] lemma hom_coe_app (val : R → S) (prop) (r : R) : (⟨val, prop⟩ : R ⟶ S) r = val r := rfl instance hom_is_ring_hom (f : R ⟶ S) : is_ring_hom (f : R → S) := f.2 def Int : CommRing := ⟨ℤ, infer_instance⟩ def Int.cast {R : CommRing} : Int ⟶ R := { val := int.cast, property := by apply_instance } def int.eq_cast' {R : Type u} [ring R] (f : int → R) [is_ring_hom f] : f = int.cast := funext $ int.eq_cast f (is_ring_hom.map_one f) (λ _ _, is_ring_hom.map_add f) def Int.hom_unique {R : CommRing} : unique (Int ⟶ R) := { default := Int.cast, uniq := λ f, subtype.ext.mpr $ funext $ int.eq_cast f f.2.map_one f.2.map_add } /-- The forgetful functor commutative rings to Type. -/ def forget : CommRing.{u} ⥤ Type u := { obj := λ R, R, map := λ _ _ f, f } instance forget.faithful : faithful (forget) := {} /-- The functor from commutative rings to rings. -/ def to_Ring : CommRing.{u} ⥤ Ring.{u} := { obj := λ X, { α := X.1, str := by apply_instance }, map := λ X Y f, ⟨ f, by apply_instance ⟩ } instance to_Ring.faithful : faithful (to_Ring) := {} /-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/ def forget_to_CommMon : CommRing.{u} ⥤ CommMon.{u} := { obj := λ X, { α := X.1, str := by apply_instance }, map := λ X Y f, ⟨ f, by apply_instance ⟩ } instance forget_to_CommMon.faithful : faithful (forget_to_CommMon) := {} example : faithful (forget_to_CommMon ⋙ CommMon.forget_to_Mon) := by apply_instance end CommRing end category_theory.instances
c47bb47e32ada968cae78c87dad49e1df9ac5e32
ba306bac106b8f27befb2a800f4357237f86c760
/lean/love06_monads_demo.lean
52a07e010c445b3bcc769727662d624b464ae6e1
[]
no_license
qyqnl/logical_verification_2020
406aa4cc47797501275ea07de5d6f59f01e977d0
17dff35b56336acb671ddfb36871f98475bc0b96
refs/heads/master
1,672,501,336,112
1,603,724,260,000
1,603,724,260,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,480
lean
import .lovelib /- # LoVe Demo 6: Monads We take a look at an important functional programming abstraction: monads. Monads generalize computation with side effects. Haskell has shown that monads can be used very successful to write imperative programs. For us, they are interesting in their own right and for two more reasons: * They provide a nice example of axiomatic reasoning. * They are useful for programming Lean itself (metaprogramming, lecture 7). -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /- ## Introductory Example Consider the following programming task: Implement a function `sum_2_5_7 ns` that sums up the second, fifth, and seventh items of a list `ns` of natural numbers. Use `option ℕ` for the result so that if the list has fewer than seven elements, you can return `option.none`. A straightforward solution follows: -/ def sum_2_5_7 (ns : list ℕ) : option ℕ := match list.nth ns 1 with | option.none := option.none | option.some n2 := match list.nth ns 4 with | option.none := option.none | option.some n5 := match list.nth ns 6 with | option.none := option.none | option.some n7 := option.some (n2 + n5 + n7) end end end /- The code is ugly, because of all the pattern matching on options. We can put all the ugliness in one function, which we call `connect`: -/ def connect {α : Type} {β : Type} : option α → (α → option β) → option β | option.none f := option.none | (option.some a) f := f a def sum_2_5_7₂ (ns : list ℕ) : option ℕ := connect (list.nth ns 1) (λn2, connect (list.nth ns 4) (λn5, connect (list.nth ns 6) (λn7, option.some (n2 + n5 + n7)))) /- Instead of defining `connect` ourselves, we can use Lean's predefined general `bind` operation. We can also use `pure` instead of `option.some`: -/ #check bind def sum_2_5_7₃ (ns : list ℕ) : option ℕ := bind (list.nth ns 1) (λn2, bind (list.nth ns 4) (λn5, bind (list.nth ns 6) (λn7, pure (n2 + n5 + n7)))) /- Syntactic sugar: `ma >>= f` := `bind ma f` -/ #check (>>=) def sum_2_5_7₄ (ns : list ℕ) : option ℕ := list.nth ns 1 >>= λn2, list.nth ns 4 >>= λn5, list.nth ns 6 >>= λn7, pure (n2 + n5 + n7) /- Syntactic sugar: `do a ← ma, t` := `ma >>= (λa, t)` `do ma, t` := `ma >>= (λ_, t)` -/ def sum_2_5_7₅ (ns : list ℕ) : option ℕ := do n2 ← list.nth ns 1, do n5 ← list.nth ns 4, do n7 ← list.nth ns 6, pure (n2 + n5 + n7) /- The `do`s can be combined: -/ def sum_2_5_7₆ (ns : list ℕ) : option ℕ := do n2 ← list.nth ns 1, n5 ← list.nth ns 4, n7 ← list.nth ns 6, pure (n2 + n5 + n7) /- Although the notation has an imperative flavor, the function is a pure functional program. ## Two Operations and Three Laws The `option` type constructor is an example of a monad. In general, a __monad__ is a type constructor `m` that depends on some type parameter `α` (i.e., `m α`) equipped with two distinguished operations: `pure {α : Type} : α → m α` `bind {α β : Type} : m α → (α → m β) → m β` For `option`: `pure` := `option.some` `bind` := `connect` Intuitively, we can think of a monad as a "box": * `pure` puts the data into the box. * `bind` allows us to access the data in the box and modify it (possibly even changing its type, since the result is an `m β` monad, not a `m α` monad). There is no general way to extract the data from the monad, i.e., to obtain an `α` from an `m α`. To summarize, `pure a` provides no side effect and simply provides a box containing the the value `a`, whereas `bind ma f` (also written `ma >>= f`) executes `ma`, then executes `f` with the boxed result `a` of `ma`. The option monad is only one instance among many. Type | Effect ------------ | -------------------------------------------------------------- `id α` | no effect `option α` | simple exceptions `σ → α × σ` | threading through a state of type `σ` `set α` | nondeterministic computation returning `α` values `t → α` | reading elements of type `t` (e.g., a configuration) `ℕ × α` | adjoining running time (e.g., to model algorithmic complexity) `string × α` | adjoining text output (e.g., for logging) `prob α` | probability (e.g., using random number generators) `io α` | interaction with the operating system `tactic α` | interaction with the proof assistant All of the above are type constructors `m` are parameterized by a type `α`. Some effects can be combined (e.g., `option (t → α)`). Some effects are not executable (e.g., `set α`, `prob α`). They are nonetheless useful for modeling programs abstractly in the logic. Specific monads may provide a way to extract the boxed value stored in the monad without `bind`'s requirement of putting it back in a monad. Monads have several benefits, including: * They provide the convenient and highly readable `do` notation. * They support generic operations, such as `mmap {α β : Type} : (α → m β) → list α → m (list β)`, which work uniformly across all monads. The `bind` and `pure` operations are normally required to obey three laws, called the monad laws. Pure data as the first program can be simplified away: do a' ← pure a, f a' = f a Pure data as the second program can be simplified away: do a ← x, pure a = x Nested programs `x`, `f`, `g` can be flattened using this associativity rule: do b ← do { a ← x, f a }, g b = do a ← x, b ← f a, g b ## A Type Class of Monads Monads are a mathematical structure, so we use class to add them as a type class (lecture 12). We can think of a type class as a structure that is parameterized by a type—or here, by a type constructor `m : Type → Type`. -/ @[class] structure lawful_monad (m : Type → Type) extends has_bind m, has_pure m := (pure_bind {α β : Type} (a : α) (f : α → m β) : (pure a >>= f) = f a) (bind_pure {α : Type} (ma : m α) : (ma >>= pure) = ma) (bind_assoc {α β γ : Type} (f : α → m β) (g : β → m γ) (ma : m α) : ((ma >>= f) >>= g) = (ma >>= (λa, f a >>= g))) #print monad #print is_lawful_monad /- Step by step: * We are creating a structure parameterized by a unary type constructor `m`. * The structure inherits the fields, and any syntactic sugar, from structures called `has_bind` and `has_pure`, which provide the `bind` and `pure` operations on `m` and some syntactic sugar. * The definition adds three fields to those already provided by `has_bind` and `has_pure`, to store the proofs of the monad laws. To instantiate this definition with a concrete monad, we must supply the type constructor `m` (e.g., `option`), `bind` and `pure` operators, and proofs of the monad laws. (Lean's actual definition of monads is more complicated.) ## Identity -/ def id.pure {α : Type} : α → id α := id def id.bind {α β : Type} : id α → (α → id β) → id β | a f := f a @[instance] def id.lawful_monad : lawful_monad (@id Type) := { pure := @id.pure, bind := @id.bind, pure_bind := begin intros α β a f, refl end, bind_pure := begin intros α m, refl end, bind_assoc := begin intros α β γ f g m, refl end } /- ## Exceptions -/ def option.pure {α : Type} : α → option α := option.some def option.bind {α β : Type} : option α → (α → option β) → option β | option.none f := option.none | (option.some a) f := f a @[instance] def option.lawful_monad : lawful_monad option := { pure := @option.pure, bind := @option.bind, pure_bind := begin intros α β a f, refl end, bind_pure := begin intros α m, cases' m, { refl }, { refl } end, bind_assoc := begin intros α β γ f g m, cases' m, { refl }, { refl } end } def option.throw {α : Type} : option α := option.none def option.catch {α : Type} : option α → option α → option α | option.none ma' := ma' | (option.some a) _ := option.some a @[instance] def option.has_orelse : has_orelse option := { orelse := @option.catch } /- ## Mutable State -/ def action (σ α : Type) := σ → α × σ def action.read {σ : Type} : action σ σ | s := (s, s) def action.write {σ : Type} (s : σ) : action σ unit | _ := ((), s) def action.pure {σ α : Type} (a : α) : action σ α | s := (a, s) def action.bind {σ : Type} {α β : Type} (ma : action σ α) (f : α → action σ β) : action σ β | s := match ma s with | (a, s') := f a s' end @[instance] def action.lawful_monad {σ : Type} : lawful_monad (action σ) := { pure := @action.pure σ, bind := @action.bind σ, pure_bind := begin intros α β a f, apply funext, intro s, refl end, bind_pure := begin intros α m, apply funext, intro s, simp [action.bind], cases' m s, refl end, bind_assoc := begin intros α β γ f g m, apply funext, intro s, simp [action.bind], cases' m s, refl end } def diff_list : list ℕ → action ℕ (list ℕ) | [] := pure [] | (n :: ns) := do prev ← action.read, if n < prev then diff_list ns else do action.write n, ns' ← diff_list ns, pure (n :: ns') #eval diff_list [1, 2, 3, 2] 0 #eval diff_list [1, 2, 3, 2, 4, 5, 2] 0 /- ## Nondeterminism -/ #check set def set.pure {α : Type} : α → set α | a := {a} def set.bind {α β : Type} : set α → (α → set β) → set β | A f := {b | ∃a, a ∈ A ∧ b ∈ f a} @[instance] def set.lawful_monad : lawful_monad set := { pure := @set.pure, bind := @set.bind, pure_bind := begin intros α β a f, simp [set.pure, set.bind] end, bind_pure := begin intros α m, simp [set.pure, set.bind] end, bind_assoc := begin intros α β γ f g m, simp [set.pure, set.bind], apply set.ext, simp, tautology end } /- `tautology` performs elimination of the logical symbols `∧`, `∨`, `↔`, and `∃` in hypotheses and introduction of `∧`, `↔`, and `∃` in the conclusion, until all the emerging subgoals can be trivially proved (e.g., by `refl`). ## A Generic Algorithm: Iteration over a List -/ def mmap {m : Type → Type} [lawful_monad m] {α β : Type} (f : α → m β) : list α → m (list β) | [] := pure [] | (a :: as) := do b ← f a, bs ← mmap as, pure (b :: bs) lemma mmap_append {m : Type → Type} [lawful_monad m] {α β : Type} (f : α → m β) : ∀as as' : list α, mmap f (as ++ as') = do bs ← mmap f as, bs' ← mmap f as', pure (bs ++ bs') | [] _ := by simp [mmap, lawful_monad.bind_pure, lawful_monad.pure_bind] | (a :: as) as' := by simp [mmap, mmap_append as as', lawful_monad.pure_bind, lawful_monad.bind_assoc] def nths {α : Type} (xss : list (list α)) (n : ℕ) : option (list α) := mmap (λxs, list.nth xs n) xss #eval nths [[11, 12, 13, 14], [21, 22, 23], [31, 32, 33]] 2 end LoVe
a452ee874c34d24a642d89c76262ed36860d51af
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/ring/defs.lean
08afb5f438d3d0842a0421115c1cba296b03245d
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
16,847
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Yury Kudryashov, Neil Strickland -/ import algebra.group.basic import algebra.group_with_zero.defs import data.int.cast.defs /-! # Semirings and rings > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines semirings, rings and domains. This is analogous to `algebra.group.defs` and `algebra.group.basic`, the difference being that the former is about `+` and `*` separately, while the present file is about their interaction. ## Main definitions * `distrib`: Typeclass for distributivity of multiplication over addition. * `has_distrib_neg`: Typeclass for commutativity of negation and multiplication. This is useful when dealing with multiplicative submonoids which are closed under negation without being closed under addition, for example `units`. * `(non_unital_)(non_assoc_)(semi)ring`: Typeclasses for possibly non-unital or non-associative rings and semirings. Some combinations are not defined yet because they haven't found use. ## Tags `semiring`, `comm_semiring`, `ring`, `comm_ring`, `domain`, `is_domain`, `nonzero`, `units` -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {R : Type x} set_option old_structure_cmd true open function /-! ### `distrib` class -/ /-- A typeclass stating that multiplication is left and right distributive over addition. -/ @[protect_proj, ancestor has_mul has_add] class distrib (R : Type*) extends has_mul R, has_add R := (left_distrib : ∀ a b c : R, a * (b + c) = a * b + a * c) (right_distrib : ∀ a b c : R, (a + b) * c = a * c + b * c) /-- A typeclass stating that multiplication is left distributive over addition. -/ @[protect_proj] class left_distrib_class (R : Type*) [has_mul R] [has_add R] := (left_distrib : ∀ a b c : R, a * (b + c) = a * b + a * c) /-- A typeclass stating that multiplication is right distributive over addition. -/ @[protect_proj] class right_distrib_class (R : Type*) [has_mul R] [has_add R] := (right_distrib : ∀ a b c : R, (a + b) * c = a * c + b * c) @[priority 100] -- see Note [lower instance priority] instance distrib.left_distrib_class (R : Type*) [distrib R] : left_distrib_class R := ⟨distrib.left_distrib⟩ @[priority 100] -- see Note [lower instance priority] instance distrib.right_distrib_class (R : Type*) [distrib R] : right_distrib_class R := ⟨distrib.right_distrib⟩ lemma left_distrib [has_mul R] [has_add R] [left_distrib_class R] (a b c : R) : a * (b + c) = a * b + a * c := left_distrib_class.left_distrib a b c alias left_distrib ← mul_add lemma right_distrib [has_mul R] [has_add R] [right_distrib_class R] (a b c : R) : (a + b) * c = a * c + b * c := right_distrib_class.right_distrib a b c alias right_distrib ← add_mul lemma distrib_three_right [has_mul R] [has_add R] [right_distrib_class R] (a b c d : R) : (a + b + c) * d = a * d + b * d + c * d := by simp [right_distrib] /-! ### Semirings -/ /-- A not-necessarily-unital, not-necessarily-associative semiring. -/ @[protect_proj, ancestor add_comm_monoid distrib mul_zero_class] class non_unital_non_assoc_semiring (α : Type u) extends add_comm_monoid α, distrib α, mul_zero_class α /-- An associative but not-necessarily unital semiring. -/ @[protect_proj, ancestor non_unital_non_assoc_semiring semigroup_with_zero] class non_unital_semiring (α : Type u) extends non_unital_non_assoc_semiring α, semigroup_with_zero α /-- A unital but not-necessarily-associative semiring. -/ @[protect_proj, ancestor non_unital_non_assoc_semiring mul_zero_one_class] class non_assoc_semiring (α : Type u) extends non_unital_non_assoc_semiring α, mul_zero_one_class α, add_comm_monoid_with_one α /-- A semiring is a type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative monoid (`monoid`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). The actual definition extends `monoid_with_zero` instead of `monoid` and `mul_zero_class`. -/ @[protect_proj, ancestor non_unital_semiring non_assoc_semiring monoid_with_zero] class semiring (α : Type u) extends non_unital_semiring α, non_assoc_semiring α, monoid_with_zero α section has_one_has_add variables [has_one α] [has_add α] lemma one_add_one_eq_two : 1 + 1 = (2 : α) := rfl end has_one_has_add section distrib_mul_one_class variables [has_add α] [mul_one_class α] lemma add_one_mul [right_distrib_class α] (a b : α) : (a + 1) * b = a * b + b := by rw [add_mul, one_mul] lemma mul_add_one [left_distrib_class α] (a b : α) : a * (b + 1) = a * b + a := by rw [mul_add, mul_one] lemma one_add_mul [right_distrib_class α] (a b : α) : (1 + a) * b = b + a * b := by rw [add_mul, one_mul] lemma mul_one_add [left_distrib_class α] (a b : α) : a * (1 + b) = a + a * b := by rw [mul_add, mul_one] theorem two_mul [right_distrib_class α] (n : α) : 2 * n = n + n := eq.trans (right_distrib 1 1 n) (by simp) theorem bit0_eq_two_mul [right_distrib_class α] (n : α) : bit0 n = 2 * n := (two_mul _).symm theorem mul_two [left_distrib_class α] (n : α) : n * 2 = n + n := (left_distrib n 1 1).trans (by simp) end distrib_mul_one_class section semiring variables [semiring α] @[to_additive] lemma mul_ite {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : a * (if P then b else c) = if P then a * b else a * c := by split_ifs; refl @[to_additive] lemma ite_mul {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : (if P then a else b) * c = if P then a * c else b * c := by split_ifs; refl -- We make `mul_ite` and `ite_mul` simp lemmas, -- but not `add_ite` or `ite_add`. -- The problem we're trying to avoid is dealing with -- summations of the form `∑ x in s, (f x + ite P 1 0)`, -- in which `add_ite` followed by `sum_ite` would needlessly slice up -- the `f x` terms according to whether `P` holds at `x`. -- There doesn't appear to be a corresponding difficulty so far with -- `mul_ite` and `ite_mul`. attribute [simp] mul_ite ite_mul @[simp] lemma mul_boole {α} [mul_zero_one_class α] (P : Prop) [decidable P] (a : α) : a * (if P then 1 else 0) = if P then a else 0 := by simp @[simp] lemma boole_mul {α} [mul_zero_one_class α] (P : Prop) [decidable P] (a : α) : (if P then 1 else 0) * a = if P then a else 0 := by simp lemma ite_mul_zero_left {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) : ite P (a * b) 0 = ite P a 0 * b := by { by_cases h : P; simp [h], } lemma ite_mul_zero_right {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) : ite P (a * b) 0 = a * ite P b 0 := by { by_cases h : P; simp [h], } lemma ite_and_mul_zero {α : Type*} [mul_zero_class α] (P Q : Prop) [decidable P] [decidable Q] (a b : α) : ite (P ∧ Q) (a * b) 0 = ite P a 0 * ite Q b 0 := by simp only [←ite_and, ite_mul, mul_ite, mul_zero, zero_mul, and_comm] end semiring /-- A non-unital commutative semiring is a `non_unital_semiring` with commutative multiplication. In other words, it is a type with the following structures: additive commutative monoid (`add_comm_monoid`), commutative semigroup (`comm_semigroup`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). -/ @[protect_proj, ancestor non_unital_semiring comm_semigroup] class non_unital_comm_semiring (α : Type u) extends non_unital_semiring α, comm_semigroup α /-- A commutative semiring is a `semiring` with commutative multiplication. In other words, it is a type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative commutative monoid (`comm_monoid`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). -/ @[protect_proj, ancestor semiring comm_monoid] class comm_semiring (α : Type u) extends semiring α, comm_monoid α @[priority 100] -- see Note [lower instance priority] instance comm_semiring.to_non_unital_comm_semiring [comm_semiring α] : non_unital_comm_semiring α := { .. comm_semiring.to_comm_monoid α, .. comm_semiring.to_semiring α } @[priority 100] -- see Note [lower instance priority] instance comm_semiring.to_comm_monoid_with_zero [comm_semiring α] : comm_monoid_with_zero α := { .. comm_semiring.to_comm_monoid α, .. comm_semiring.to_semiring α } section comm_semiring variables [comm_semiring α] {a b c : α} lemma add_mul_self_eq (a b : α) : (a + b) * (a + b) = a*a + 2*a*b + b*b := by simp only [two_mul, add_mul, mul_add, add_assoc, mul_comm b] end comm_semiring section has_distrib_neg /-- Typeclass for a negation operator that distributes across multiplication. This is useful for dealing with submonoids of a ring that contain `-1` without having to duplicate lemmas. -/ class has_distrib_neg (α : Type*) [has_mul α] extends has_involutive_neg α := (neg_mul : ∀ x y : α, -x * y = -(x * y)) (mul_neg : ∀ x y : α, x * -y = -(x * y)) section has_mul variables [has_mul α] [has_distrib_neg α] @[simp] lemma neg_mul (a b : α) : - a * b = - (a * b) := has_distrib_neg.neg_mul _ _ @[simp] lemma mul_neg (a b : α) : a * - b = - (a * b) := has_distrib_neg.mul_neg _ _ lemma neg_mul_neg (a b : α) : -a * -b = a * b := by simp lemma neg_mul_eq_neg_mul (a b : α) : -(a * b) = -a * b := (neg_mul _ _).symm lemma neg_mul_eq_mul_neg (a b : α) : -(a * b) = a * -b := (mul_neg _ _).symm lemma neg_mul_comm (a b : α) : -a * b = a * -b := by simp end has_mul section mul_one_class variables [mul_one_class α] [has_distrib_neg α] theorem neg_eq_neg_one_mul (a : α) : -a = -1 * a := by simp /-- An element of a ring multiplied by the additive inverse of one is the element's additive inverse. -/ lemma mul_neg_one (a : α) : a * -1 = -a := by simp /-- The additive inverse of one multiplied by an element of a ring is the element's additive inverse. -/ lemma neg_one_mul (a : α) : -1 * a = -a := by simp end mul_one_class section mul_zero_class variables [mul_zero_class α] [has_distrib_neg α] @[priority 100] instance mul_zero_class.neg_zero_class : neg_zero_class α := { neg_zero := by rw [←zero_mul (0 : α), ←neg_mul, mul_zero, mul_zero], ..mul_zero_class.to_has_zero α, ..has_distrib_neg.to_has_involutive_neg α } end mul_zero_class end has_distrib_neg /-! ### Rings -/ /-- A not-necessarily-unital, not-necessarily-associative ring. -/ @[protect_proj, ancestor add_comm_group non_unital_non_assoc_semiring] class non_unital_non_assoc_ring (α : Type u) extends add_comm_group α, non_unital_non_assoc_semiring α -- We defer the instance `non_unital_non_assoc_ring.to_has_distrib_neg` to `algebra.ring.basic` -- as it relies on the lemma `eq_neg_of_add_eq_zero_left`. /-- An associative but not-necessarily unital ring. -/ @[protect_proj, ancestor non_unital_non_assoc_ring non_unital_semiring] class non_unital_ring (α : Type*) extends non_unital_non_assoc_ring α, non_unital_semiring α /-- A unital but not-necessarily-associative ring. -/ @[protect_proj, ancestor non_unital_non_assoc_ring non_assoc_semiring] class non_assoc_ring (α : Type*) extends non_unital_non_assoc_ring α, non_assoc_semiring α, add_group_with_one α /-- A ring is a type with the following structures: additive commutative group (`add_comm_group`), multiplicative monoid (`monoid`), and distributive laws (`distrib`). Equivalently, a ring is a `semiring` with a negation operation making it an additive group. -/ @[protect_proj, ancestor add_comm_group monoid distrib] class ring (α : Type u) extends add_comm_group_with_one α, monoid α, distrib α section non_unital_non_assoc_ring variables [non_unital_non_assoc_ring α] @[priority 100] instance non_unital_non_assoc_ring.to_has_distrib_neg : has_distrib_neg α := { neg := has_neg.neg, neg_neg := neg_neg, neg_mul := λ a b, eq_neg_of_add_eq_zero_left $ by rw [←right_distrib, add_left_neg, zero_mul], mul_neg := λ a b, eq_neg_of_add_eq_zero_left $ by rw [←left_distrib, add_left_neg, mul_zero] } lemma mul_sub_left_distrib (a b c : α) : a * (b - c) = a * b - a * c := by simpa only [sub_eq_add_neg, neg_mul_eq_mul_neg] using mul_add a b (-c) alias mul_sub_left_distrib ← mul_sub lemma mul_sub_right_distrib (a b c : α) : (a - b) * c = a * c - b * c := by simpa only [sub_eq_add_neg, neg_mul_eq_neg_mul] using add_mul a (-b) c alias mul_sub_right_distrib ← sub_mul variables {a b c d e : α} /-- An iff statement following from right distributivity in rings and the definition of subtraction. -/ theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp [add_comm] ... ↔ a * e + c - b * e = d : iff.intro (λ h, begin rw h, simp end) (λ h, begin rw ← h, simp end) ... ↔ (a - b) * e + c = d : begin simp [sub_mul, sub_add_eq_add_sub] end /-- A simplification of one side of an equation exploiting right distributivity in rings and the definition of subtraction. -/ theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d := assume h, calc (a - b) * e + c = (a * e + c) - b * e : begin simp [sub_mul, sub_add_eq_add_sub] end ... = d : begin rw h, simp [@add_sub_cancel α] end end non_unital_non_assoc_ring section non_assoc_ring variables [non_assoc_ring α] lemma sub_one_mul (a b : α) : (a - 1) * b = a * b - b := by rw [sub_mul, one_mul] lemma mul_sub_one (a b : α) : a * (b - 1) = a * b - a := by rw [mul_sub, mul_one] lemma one_sub_mul (a b : α) : (1 - a) * b = b - a * b := by rw [sub_mul, one_mul] lemma mul_one_sub (a b : α) : a * (1 - b) = a - a * b := by rw [mul_sub, mul_one] end non_assoc_ring section ring variables [ring α] {a b c d e : α} /- A (unital, associative) ring is a not-necessarily-unital ring -/ @[priority 100] -- see Note [lower instance priority] instance ring.to_non_unital_ring : non_unital_ring α := { zero_mul := λ a, add_left_cancel $ show 0 * a + 0 * a = 0 * a + 0, by rw [← add_mul, zero_add, add_zero], mul_zero := λ a, add_left_cancel $ show a * 0 + a * 0 = a * 0 + 0, by rw [← mul_add, add_zero, add_zero], ..‹ring α› } /- A (unital, associative) ring is a not-necessarily-associative ring -/ @[priority 100] -- see Note [lower instance priority] instance ring.to_non_assoc_ring : non_assoc_ring α := { zero_mul := λ a, add_left_cancel $ show 0 * a + 0 * a = 0 * a + 0, by rw [← add_mul, zero_add, add_zero], mul_zero := λ a, add_left_cancel $ show a * 0 + a * 0 = a * 0 + 0, by rw [← mul_add, add_zero, add_zero], ..‹ring α› } /- The instance from `ring` to `semiring` happens often in linear algebra, for which all the basic definitions are given in terms of semirings, but many applications use rings or fields. We increase a little bit its priority above 100 to try it quickly, but remaining below the default 1000 so that more specific instances are tried first. -/ @[priority 200] instance ring.to_semiring : semiring α := { ..‹ring α›, .. ring.to_non_unital_ring } end ring /-- A non-unital commutative ring is a `non_unital_ring` with commutative multiplication. -/ @[protect_proj, ancestor non_unital_ring comm_semigroup] class non_unital_comm_ring (α : Type u) extends non_unital_ring α, comm_semigroup α @[priority 100] -- see Note [lower instance priority] instance non_unital_comm_ring.to_non_unital_comm_semiring [s : non_unital_comm_ring α] : non_unital_comm_semiring α := { ..s } /-- A commutative ring is a `ring` with commutative multiplication. -/ @[protect_proj, ancestor ring comm_semigroup] class comm_ring (α : Type u) extends ring α, comm_monoid α @[priority 100] -- see Note [lower instance priority] instance comm_ring.to_comm_semiring [s : comm_ring α] : comm_semiring α := { mul_zero := mul_zero, zero_mul := zero_mul, ..s } @[priority 100] -- see Note [lower instance priority] instance comm_ring.to_non_unital_comm_ring [s : comm_ring α] : non_unital_comm_ring α := { mul_zero := mul_zero, zero_mul := zero_mul, ..s } /-- A domain is a nontrivial semiring such multiplication by a non zero element is cancellative, on both sides. In other words, a nontrivial semiring `R` satisfying `∀ {a b c : R}, a ≠ 0 → a * b = a * c → b = c` and `∀ {a b c : R}, b ≠ 0 → a * b = c * b → a = c`. This is implemented as a mixin for `semiring α`. To obtain an integral domain use `[comm_ring α] [is_domain α]`. -/ @[protect_proj, ancestor is_cancel_mul_zero nontrivial] class is_domain (α : Type u) [semiring α] extends is_cancel_mul_zero α, nontrivial α : Prop
75da9ed70a6425feb27eb3c2957b0dd7454e79b7
26b8b0964ca8e1c2e203585ba5940f83fe05e48a
/src/tidy/force.lean
7d79533ca3102bc8d8094544ed98772b2f1692ca
[]
no_license
jcommelin/lean-tidy
ef3cd32a3804221d93f0dff9e180bb2c52f4b143
9cecf497e90db64b5ea140ad6ae1603976dcd402
refs/heads/master
1,585,129,919,276
1,533,512,680,000
1,533,512,680,000
143,677,361
0
0
null
1,616,803,481,000
1,533,530,576,000
Lean
UTF-8
Lean
false
false
407
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison open tactic meta def force { α : Type } (t : tactic α) : tactic α := do goals ← get_goals, result ← t, goals' ← get_goals, guard (goals ≠ goals') <|> fail "force tactic failed", return result run_cmd add_interactive [`force]
e0c496caedbebb7021f3b8bcaef434f0182594d7
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/hott/algebra/ring.hlean
ce8105cebe1587d0546146a80faf19f96a8b5df9
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
18,390
hlean
/- 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 Structures with multiplicative and additive components, including semirings, rings, and fields. The development is modeled after Isabelle's library. -/ import algebra.binary algebra.group open eq eq.ops algebra set_option class.force_new true variable {A : Type} namespace algebra /- auxiliary classes -/ structure distrib [class] (A : Type) extends has_mul A, has_add A := (left_distrib : Πa b c, mul a (add b c) = add (mul a b) (mul a c)) (right_distrib : Πa b c, mul (add a b) c = add (mul a c) (mul b c)) theorem left_distrib [s : distrib A] (a b c : A) : a * (b + c) = a * b + a * c := !distrib.left_distrib theorem right_distrib [s: distrib A] (a b c : A) : (a + b) * c = a * c + b * c := !distrib.right_distrib structure mul_zero_class [class] (A : Type) extends has_mul A, has_zero A := (zero_mul : Πa, mul zero a = zero) (mul_zero : Πa, mul a zero = zero) theorem zero_mul [s : mul_zero_class A] (a : A) : 0 * a = 0 := !mul_zero_class.zero_mul theorem mul_zero [s : mul_zero_class A] (a : A) : a * 0 = 0 := !mul_zero_class.mul_zero structure zero_ne_one_class [class] (A : Type) extends has_zero A, has_one A := (zero_ne_one : zero ≠ one) theorem zero_ne_one [s: zero_ne_one_class A] : 0 ≠ (1:A) := @zero_ne_one_class.zero_ne_one A s /- semiring -/ structure semiring [class] (A : Type) extends add_comm_monoid A, monoid A, distrib A, mul_zero_class A section semiring variables [s : semiring A] (a b c : A) include s theorem one_add_one_eq_two : 1 + 1 = (2:A) := by unfold bit0 theorem ne_zero_of_mul_ne_zero_right {a b : A} (H : a * b ≠ 0) : a ≠ 0 := suppose a = 0, have a * b = 0, from this⁻¹ ▸ zero_mul b, H this theorem ne_zero_of_mul_ne_zero_left {a b : A} (H : a * b ≠ 0) : b ≠ 0 := suppose b = 0, have a * b = 0, from this⁻¹ ▸ mul_zero a, H this theorem distrib_three_right (a b c d : A) : (a + b + c) * d = a * d + b * d + c * d := by rewrite *right_distrib end semiring /- comm semiring -/ structure comm_semiring [class] (A : Type) extends semiring A, comm_monoid A -- TODO: we could also define a cancelative comm_semiring, i.e. satisfying -- c ≠ 0 → c * a = c * b → a = b. section comm_semiring variables [s : comm_semiring A] (a b c : A) include s protected definition algebra.dvd (a b : A) : Type := Σc, b = a * c definition comm_semiring_has_dvd [instance] [priority algebra.prio] : has_dvd A := has_dvd.mk algebra.dvd theorem dvd.intro {a b c : A} (H : a * c = b) : a ∣ b := sigma.mk _ H⁻¹ theorem dvd_of_mul_right_eq {a b c : A} (H : a * c = b) : a ∣ b := dvd.intro H theorem dvd.intro_left {a b c : A} (H : c * a = b) : a ∣ b := dvd.intro (!mul.comm ▸ H) theorem dvd_of_mul_left_eq {a b c : A} (H : c * a = b) : a ∣ b := dvd.intro_left H theorem exists_eq_mul_right_of_dvd {a b : A} (H : a ∣ b) : Σc, b = a * c := H theorem dvd.elim {P : Type} {a b : A} (H₁ : a ∣ b) (H₂ : Πc, b = a * c → P) : P := sigma.rec_on H₁ H₂ theorem exists_eq_mul_left_of_dvd {a b : A} (H : a ∣ b) : Σc, b = c * a := dvd.elim H (take c, assume H1 : b = a * c, sigma.mk c (H1 ⬝ !mul.comm)) theorem dvd.elim_left {P : Type} {a b : A} (H₁ : a ∣ b) (H₂ : Πc, b = c * a → P) : P := sigma.rec_on (exists_eq_mul_left_of_dvd H₁) (take c, assume H₃ : b = c * a, H₂ c H₃) theorem dvd.refl : a ∣ a := dvd.intro !mul_one theorem dvd.trans {a b c : A} (H₁ : a ∣ b) (H₂ : b ∣ c) : a ∣ c := dvd.elim H₁ (take d, assume H₃ : b = a * d, dvd.elim H₂ (take e, assume H₄ : c = b * e, dvd.intro (show a * (d * e) = c, by rewrite [-mul.assoc, -H₃, H₄]))) theorem eq_zero_of_zero_dvd {a : A} (H : 0 ∣ a) : a = 0 := dvd.elim H (take c, assume H' : a = 0 * c, H' ⬝ !zero_mul) theorem dvd_zero : a ∣ 0 := dvd.intro !mul_zero theorem one_dvd : 1 ∣ a := dvd.intro !one_mul theorem dvd_mul_right : a ∣ a * b := dvd.intro rfl theorem dvd_mul_left : a ∣ b * a := mul.comm a b ▸ dvd_mul_right a b theorem dvd_mul_of_dvd_left {a b : A} (H : a ∣ b) (c : A) : a ∣ b * c := dvd.elim H (take d, suppose b = a * d, dvd.intro (show a * (d * c) = b * c, from by rewrite [-mul.assoc]; substvars)) theorem dvd_mul_of_dvd_right {a b : A} (H : a ∣ b) (c : A) : a ∣ c * b := !mul.comm ▸ (dvd_mul_of_dvd_left H _) theorem mul_dvd_mul {a b c d : A} (dvd_ab : a ∣ b) (dvd_cd : c ∣ d) : a * c ∣ b * d := dvd.elim dvd_ab (take e, suppose b = a * e, dvd.elim dvd_cd (take f, suppose d = c * f, dvd.intro (show a * c * (e * f) = b * d, by rewrite [mul.assoc, {c*_}mul.left_comm, -mul.assoc]; substvars))) theorem dvd_of_mul_right_dvd {a b c : A} (H : a * b ∣ c) : a ∣ c := dvd.elim H (take d, assume Habdc : c = a * b * d, dvd.intro (!mul.assoc⁻¹ ⬝ Habdc⁻¹)) theorem dvd_of_mul_left_dvd {a b c : A} (H : a * b ∣ c) : b ∣ c := dvd_of_mul_right_dvd (mul.comm a b ▸ H) theorem dvd_add {a b c : A} (Hab : a ∣ b) (Hac : a ∣ c) : a ∣ b + c := dvd.elim Hab (take d, suppose b = a * d, dvd.elim Hac (take e, suppose c = a * e, dvd.intro (show a * (d + e) = b + c, by rewrite [left_distrib]; substvars))) end comm_semiring /- ring -/ structure ring [class] (A : Type) extends add_comm_group A, monoid A, distrib A theorem ring.mul_zero [s : ring A] (a : A) : a * 0 = 0 := have a * 0 + 0 = a * 0 + a * 0, from calc a * 0 + 0 = a * 0 : by rewrite add_zero ... = a * (0 + 0) : by rewrite add_zero ... = a * 0 + a * 0 : by rewrite {a*_}ring.left_distrib, show a * 0 = 0, from (add.left_cancel this)⁻¹ theorem ring.zero_mul [s : ring A] (a : A) : 0 * a = 0 := have 0 * a + 0 = 0 * a + 0 * a, from calc 0 * a + 0 = 0 * a : by rewrite add_zero ... = (0 + 0) * a : by rewrite add_zero ... = 0 * a + 0 * a : by rewrite {_*a}ring.right_distrib, show 0 * a = 0, from (add.left_cancel this)⁻¹ definition ring.to_semiring [trans_instance] [s : ring A] : semiring A := ⦃ semiring, s, mul_zero := ring.mul_zero, zero_mul := ring.zero_mul ⦄ section variables [s : ring A] (a b c d e : A) include s theorem neg_mul_eq_neg_mul : -(a * b) = -a * b := neg_eq_of_add_eq_zero begin rewrite [-right_distrib, add.right_inv, zero_mul] end theorem neg_mul_eq_mul_neg : -(a * b) = a * -b := neg_eq_of_add_eq_zero begin rewrite [-left_distrib, add.right_inv, mul_zero] end theorem neg_mul_eq_neg_mul_symm : - a * b = - (a * b) := inverse !neg_mul_eq_neg_mul theorem mul_neg_eq_neg_mul_symm : a * - b = - (a * b) := inverse !neg_mul_eq_mul_neg theorem neg_mul_neg : -a * -b = a * b := calc -a * -b = -(a * -b) : by rewrite -neg_mul_eq_neg_mul ... = - -(a * b) : by rewrite -neg_mul_eq_mul_neg ... = a * b : by rewrite neg_neg theorem neg_mul_comm : -a * b = a * -b := !neg_mul_eq_neg_mul⁻¹ ⬝ !neg_mul_eq_mul_neg theorem neg_eq_neg_one_mul : -a = -1 * a := calc -a = -(1 * a) : by rewrite one_mul ... = -1 * a : by rewrite neg_mul_eq_neg_mul theorem mul_sub_left_distrib : a * (b - c) = a * b - a * c := calc a * (b - c) = a * b + a * -c : left_distrib ... = a * b + - (a * c) : by rewrite -neg_mul_eq_mul_neg ... = a * b - a * c : rfl theorem mul_sub_right_distrib : (a - b) * c = a * c - b * c := calc (a - b) * c = a * c + -b * c : right_distrib ... = a * c + - (b * c) : by rewrite neg_mul_eq_neg_mul ... = a * c - b * c : rfl -- TODO: can calc mode be improved to make this easier? -- TODO: there is also the other direction. It will be easier when we -- have the simplifier. theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by rewrite {b*e+_}add.comm ... ↔ a * e + c - b * e = d : iff.symm !sub_eq_iff_eq_add ... ↔ a * e - b * e + c = d : by rewrite sub_add_eq_add_sub ... ↔ (a - b) * e + c = d : by rewrite mul_sub_right_distrib theorem mul_add_eq_mul_add_of_sub_mul_add_eq : (a - b) * e + c = d → a * e + c = b * e + d := iff.mpr !mul_add_eq_mul_add_iff_sub_mul_add_eq theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d := iff.mp !mul_add_eq_mul_add_iff_sub_mul_add_eq theorem mul_neg_one_eq_neg : a * (-1) = -a := have a + a * -1 = 0, from calc a + a * -1 = a * 1 + a * -1 : mul_one ... = a * (1 + -1) : left_distrib ... = a * 0 : add.right_inv ... = 0 : mul_zero, symm (neg_eq_of_add_eq_zero this) theorem ne_zero_prod_ne_zero_of_mul_ne_zero {a b : A} (H : a * b ≠ 0) : a ≠ 0 × b ≠ 0 := have a ≠ 0, from (suppose a = 0, have a * b = 0, by rewrite [this, zero_mul], absurd this H), have b ≠ 0, from (suppose b = 0, have a * b = 0, by rewrite [this, mul_zero], absurd this H), prod.mk `a ≠ 0` `b ≠ 0` end structure comm_ring [class] (A : Type) extends ring A, comm_semigroup A definition comm_ring.to_comm_semiring [trans_instance] [s : comm_ring A] : comm_semiring A := ⦃ comm_semiring, s, mul_zero := mul_zero, zero_mul := zero_mul ⦄ section variables [s : comm_ring A] (a b c d e : A) include s theorem mul_self_sub_mul_self_eq : a * a - b * b = (a + b) * (a - b) := begin krewrite [left_distrib, *right_distrib, add.assoc], rewrite [-{b*a + _}add.assoc, -*neg_mul_eq_mul_neg, {a*b}mul.comm, add.right_inv, zero_add] end theorem mul_self_sub_one_eq : a * a - 1 = (a + 1) * (a - 1) := by rewrite [-mul_self_sub_mul_self_eq, mul_one] theorem dvd_neg_iff_dvd : (a ∣ -b) ↔ (a ∣ b) := iff.intro (suppose a ∣ -b, dvd.elim this (take c, suppose -b = a * c, dvd.intro (show a * -c = b, by rewrite [-neg_mul_eq_mul_neg, -this, neg_neg]))) (suppose a ∣ b, dvd.elim this (take c, suppose b = a * c, dvd.intro (show a * -c = -b, by rewrite [-neg_mul_eq_mul_neg, -this]))) theorem dvd_neg_of_dvd : (a ∣ b) → (a ∣ -b) := iff.mpr !dvd_neg_iff_dvd theorem dvd_of_dvd_neg : (a ∣ -b) → (a ∣ b) := iff.mp !dvd_neg_iff_dvd theorem neg_dvd_iff_dvd : (-a ∣ b) ↔ (a ∣ b) := iff.intro (suppose -a ∣ b, dvd.elim this (take c, suppose b = -a * c, dvd.intro (show a * -c = b, by rewrite [-neg_mul_comm, this]))) (suppose a ∣ b, dvd.elim this (take c, suppose b = a * c, dvd.intro (show -a * -c = b, by rewrite [neg_mul_neg, this]))) theorem neg_dvd_of_dvd : (a ∣ b) → (-a ∣ b) := iff.mpr !neg_dvd_iff_dvd theorem dvd_of_neg_dvd : (-a ∣ b) → (a ∣ b) := iff.mp !neg_dvd_iff_dvd theorem dvd_sub (H₁ : (a ∣ b)) (H₂ : (a ∣ c)) : (a ∣ b - c) := dvd_add H₁ (!dvd_neg_of_dvd H₂) end /- integral domains -/ structure no_zero_divisors [class] (A : Type) extends has_mul A, has_zero A := (eq_zero_sum_eq_zero_of_mul_eq_zero : Πa b, mul a b = zero → a = zero ⊎ b = zero) definition eq_zero_sum_eq_zero_of_mul_eq_zero {A : Type} [s : no_zero_divisors A] {a b : A} (H : a * b = 0) : a = 0 ⊎ b = 0 := !no_zero_divisors.eq_zero_sum_eq_zero_of_mul_eq_zero H structure integral_domain [class] (A : Type) extends comm_ring A, no_zero_divisors A, zero_ne_one_class A section variables [s : integral_domain A] (a b c d e : A) include s theorem mul_ne_zero {a b : A} (H1 : a ≠ 0) (H2 : b ≠ 0) : a * b ≠ 0 := suppose a * b = 0, sum.elim (eq_zero_sum_eq_zero_of_mul_eq_zero this) (assume H3, H1 H3) (assume H4, H2 H4) theorem eq_of_mul_eq_mul_right {a b c : A} (Ha : a ≠ 0) (H : b * a = c * a) : b = c := have b * a - c * a = 0, from iff.mp !eq_iff_sub_eq_zero H, have (b - c) * a = 0, by rewrite [mul_sub_right_distrib, this], have b - c = 0, from sum_resolve_left (eq_zero_sum_eq_zero_of_mul_eq_zero this) Ha, iff.elim_right !eq_iff_sub_eq_zero this theorem eq_of_mul_eq_mul_left {a b c : A} (Ha : a ≠ 0) (H : a * b = a * c) : b = c := have a * b - a * c = 0, from iff.mp !eq_iff_sub_eq_zero H, have a * (b - c) = 0, by rewrite [mul_sub_left_distrib, this], have b - c = 0, from sum_resolve_right (eq_zero_sum_eq_zero_of_mul_eq_zero this) Ha, iff.elim_right !eq_iff_sub_eq_zero this -- TODO: do we want the iff versions? theorem eq_zero_of_mul_eq_self_right {a b : A} (H₁ : b ≠ 1) (H₂ : a * b = a) : a = 0 := have b - 1 ≠ 0, from suppose b - 1 = 0, H₁ (!zero_add ▸ eq_add_of_sub_eq this), have a * b - a = 0, by rewrite H₂; apply sub_self, have a * (b - 1) = 0, by rewrite [mul_sub_left_distrib, mul_one]; apply this, show a = 0, from sum_resolve_left (eq_zero_sum_eq_zero_of_mul_eq_zero this) `b - 1 ≠ 0` theorem eq_zero_of_mul_eq_self_left {a b : A} (H₁ : b ≠ 1) (H₂ : b * a = a) : a = 0 := eq_zero_of_mul_eq_self_right H₁ (!mul.comm ▸ H₂) theorem mul_self_eq_mul_self_iff (a b : A) : a * a = b * b ↔ a = b ⊎ a = -b := iff.intro (suppose a * a = b * b, have (a - b) * (a + b) = 0, by rewrite [mul.comm, -mul_self_sub_mul_self_eq, this, sub_self], have a - b = 0 ⊎ a + b = 0, from !eq_zero_sum_eq_zero_of_mul_eq_zero this, sum.elim this (suppose a - b = 0, sum.inl (eq_of_sub_eq_zero this)) (suppose a + b = 0, sum.inr (eq_neg_of_add_eq_zero this))) (suppose a = b ⊎ a = -b, sum.elim this (suppose a = b, by rewrite this) (suppose a = -b, by rewrite [this, neg_mul_neg])) theorem mul_self_eq_one_iff (a : A) : a * a = 1 ↔ a = 1 ⊎ a = -1 := have a * a = 1 * 1 ↔ a = 1 ⊎ a = -1, from mul_self_eq_mul_self_iff a 1, by rewrite mul_one at this; exact this -- TODO: c - b * c → c = 0 ⊎ b = 1 and variants theorem dvd_of_mul_dvd_mul_left {a b c : A} (Ha : a ≠ 0) (Hdvd : (a * b ∣ a * c)) : (b ∣ c) := dvd.elim Hdvd (take d, suppose a * c = a * b * d, have b * d = c, from eq_of_mul_eq_mul_left Ha (mul.assoc a b d ▸ this⁻¹), dvd.intro this) theorem dvd_of_mul_dvd_mul_right {a b c : A} (Ha : a ≠ 0) (Hdvd : (b * a ∣ c * a)) : (b ∣ c) := dvd.elim Hdvd (take d, suppose c * a = b * a * d, have b * d * a = c * a, from by rewrite [mul.right_comm, -this], have b * d = c, from eq_of_mul_eq_mul_right Ha this, dvd.intro this) end namespace norm_num theorem mul_zero [s : mul_zero_class A] (a : A) : a * zero = zero := by rewrite [↑zero, mul_zero] theorem zero_mul [s : mul_zero_class A] (a : A) : zero * a = zero := by rewrite [↑zero, zero_mul] theorem mul_one [s : monoid A] (a : A) : a * one = a := by rewrite [↑one, mul_one] theorem mul_bit0 [s : distrib A] (a b : A) : a * (bit0 b) = bit0 (a * b) := by rewrite [↑bit0, left_distrib] theorem mul_bit0_helper [s : distrib A] (a b t : A) (H : a * b = t) : a * (bit0 b) = bit0 t := by rewrite -H; apply mul_bit0 theorem mul_bit1 [s : semiring A] (a b : A) : a * (bit1 b) = bit0 (a * b) + a := by rewrite [↑bit1, ↑bit0, +left_distrib, ↑one, mul_one] theorem mul_bit1_helper [s : semiring A] (a b s t : A) (Hs : a * b = s) (Ht : bit0 s + a = t) : a * (bit1 b) = t := begin rewrite [-Ht, -Hs, mul_bit1] end theorem subst_into_prod [s : has_mul A] (l r tl tr t : A) (prl : l = tl) (prr : r = tr) (prt : tl * tr = t) : l * r = t := by rewrite [prl, prr, prt] theorem mk_cong (op : A → A) (a b : A) (H : a = b) : op a = op b := by congruence; exact H theorem mk_eq (a : A) : a = a := rfl theorem neg_add_neg_eq_of_add_add_eq_zero [s : add_comm_group A] (a b c : A) (H : c + a + b = 0) : -a + -b = c := begin apply add_neg_eq_of_eq_add, apply neg_eq_of_add_eq_zero, rewrite [add.comm, add.assoc, add.comm b, -add.assoc, H] end theorem neg_add_neg_helper [s : add_comm_group A] (a b c : A) (H : a + b = c) : -a + -b = -c := begin apply iff.mp !neg_eq_neg_iff_eq, rewrite [neg_add, *neg_neg, H] end theorem neg_add_pos_eq_of_eq_add [s : add_comm_group A] (a b c : A) (H : b = c + a) : -a + b = c := begin apply neg_add_eq_of_eq_add, rewrite add.comm, exact H end theorem neg_add_pos_helper1 [s : add_comm_group A] (a b c : A) (H : b + c = a) : -a + b = -c := begin apply neg_add_eq_of_eq_add, apply eq_add_neg_of_add_eq H end theorem neg_add_pos_helper2 [s : add_comm_group A] (a b c : A) (H : a + c = b) : -a + b = c := begin apply neg_add_eq_of_eq_add, rewrite H end theorem pos_add_neg_helper [s : add_comm_group A] (a b c : A) (H : b + a = c) : a + b = c := by rewrite [add.comm, H] theorem sub_eq_add_neg_helper [s : add_comm_group A] (t₁ t₂ e w₁ w₂: A) (H₁ : t₁ = w₁) (H₂ : t₂ = w₂) (H : w₁ + -w₂ = e) : t₁ - t₂ = e := by rewrite [sub_eq_add_neg, H₁, H₂, H] theorem pos_add_pos_helper [s : add_comm_group A] (a b c h₁ h₂ : A) (H₁ : a = h₁) (H₂ : b = h₂) (H : h₁ + h₂ = c) : a + b = c := by rewrite [H₁, H₂, H] theorem subst_into_subtr [s : add_group A] (l r t : A) (prt : l + -r = t) : l - r = t := by rewrite [sub_eq_add_neg, prt] theorem neg_neg_helper [s : add_group A] (a b : A) (H : a = -b) : -a = b := by rewrite [H, neg_neg] theorem neg_mul_neg_helper [s : ring A] (a b c : A) (H : a * b = c) : (-a) * (-b) = c := begin rewrite [neg_mul_neg, H] end theorem neg_mul_pos_helper [s : ring A] (a b c : A) (H : a * b = c) : (-a) * b = -c := begin rewrite [-neg_mul_eq_neg_mul, H] end theorem pos_mul_neg_helper [s : ring A] (a b c : A) (H : a * b = c) : a * (-b) = -c := begin rewrite [-neg_mul_comm, -neg_mul_eq_neg_mul, H] end end norm_num end algebra open algebra attribute [simp] zero_mul mul_zero at simplifier.unit attribute [simp] neg_mul_eq_neg_mul_symm mul_neg_eq_neg_mul_symm at simplifier.neg attribute [simp] left_distrib right_distrib at simplifier.distrib
e8ee19df84b2d886a3c86fc645a0bf1dae83414a
4fa161becb8ce7378a709f5992a594764699e268
/src/algebra/module.lean
d7cbe86d151d65f1a843031c7eb2aa3e9ca464e1
[ "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
29,053
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro -/ import group_theory.group_action /-! # Modules over a ring In this file we define * `semimodule R M` : an additive commutative monoid `M` is a `semimodule` over a `semiring` `R` if for `r : R` and `x : M` their "scalar multiplication `r • x : M` is defined, and the operation `•` satisfies some natural associativity and distributivity axioms similar to those on a ring. * `module R M` : same as `semimodule R M` but assumes that `R` is a `ring` and `M` is an additive commutative group. * `vector_space k M` : same as `semimodule k M` and `module k M` but assumes that `k` is a `field` and `M` is an additive commutative group. * `linear_map R M M₂`, `M →ₗ[R] M₂` : a linear map between two R-`semimodule`s. * `is_linear_map R f` : predicate saying that `f : M → M₂` is a linear map. * `submodule R M` : a subset of `M` that contains zero and is closed with respect to addition and scalar multiplication. * `subspace k M` : an abbreviation for `submodule` assuming that `k` is a `field`. ## Implementation notes * `vector_space` and `module` are abbreviations for `semimodule R M`. ## TODO * `submodule R M` was written before bundled `submonoid`s, so it does not extend it. ## Tags semimodule, module, vector space, submodule, subspace, linear map -/ open function open_locale big_operators universes u u' v w x y z variables {R : Type u} {k : Type u'} {S : Type v} {M : Type w} {M₂ : Type x} {M₃ : Type y} {ι : Type z} section prio set_option default_priority 100 -- see Note [default priority] /-- A semimodule is a generalization of vector spaces to a scalar semiring. It consists of a scalar semiring `R` and an additive monoid of "vectors" `M`, connected by a "scalar multiplication" operation `r • x : M` (where `r : R` and `x : M`) with some natural associativity and distributivity axioms similar to those on a ring. -/ @[protect_proj] class semimodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] extends distrib_mul_action R M := (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (zero_smul : ∀x : M, (0 : R) • x = 0) end prio section add_comm_monoid variables [semiring R] [add_comm_monoid M] [semimodule R M] (r s : R) (x y : M) theorem add_smul : (r + s) • x = r • x + s • x := semimodule.add_smul r s x variables (R) @[simp] theorem zero_smul : (0 : R) • x = 0 := semimodule.zero_smul x theorem two_smul : (2 : R) • x = x + x := by rw [bit0, add_smul, one_smul] variable (M) /-- `(•)` as an `add_monoid_hom`. -/ def smul_add_hom : R →+ M →+ M := { to_fun := const_smul_hom M, map_zero' := add_monoid_hom.ext $ λ r, by simp, map_add' := λ x y, add_monoid_hom.ext $ λ r, by simp [add_smul] } variables {R M} @[simp] lemma smul_add_hom_apply (r : R) (x : M) : smul_add_hom R M r x = r • x := rfl lemma semimodule.eq_zero_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : x = 0 := by rw [←one_smul R x, ←zero_eq_one, zero_smul] lemma list.sum_smul {l : list R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_list_sum l lemma multiset.sum_smul {l : multiset R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_multiset_sum l lemma finset.sum_smul {f : ι → R} {s : finset ι} {x : M} : (∑ i in s, f i) • x = (∑ i in s, (f i) • x) := ((smul_add_hom R M).flip x).map_sum f s end add_comm_monoid section add_comm_group variables (R M) [semiring R] [add_comm_group M] /-- A structure containing most informations as in a semimodule, except the fields `zero_smul` and `smul_zero`. As these fields can be deduced from the other ones when `M` is an `add_comm_group`, this provides a way to construct a semimodule structure by checking less properties, in `semimodule.of_core`. -/ @[nolint has_inhabited_instance] structure semimodule.core extends has_scalar R M := (smul_add : ∀(r : R) (x y : M), r • (x + y) = r • x + r • y) (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (mul_smul : ∀(r s : R) (x : M), (r * s) • x = r • s • x) (one_smul : ∀x : M, (1 : R) • x = x) variables {R M} /-- Define `semimodule` without proving `zero_smul` and `smul_zero` by using an auxiliary structure `semimodule.core`, when the underlying space is an `add_comm_group`. -/ def semimodule.of_core (H : semimodule.core R M) : semimodule R M := by letI := H.to_has_scalar; exact { zero_smul := λ x, (add_monoid_hom.mk' (λ r : R, r • x) (λ r s, H.add_smul r s x)).map_zero, smul_zero := λ r, (add_monoid_hom.mk' ((•) r) (H.smul_add r)).map_zero, ..H } variable [semimodule R M] @[simp] theorem smul_neg (r : R) (x : M) : r • (-x) = -(r • x) := eq_neg_of_add_eq_zero (by simp [← smul_add]) theorem smul_sub (r : R) (x y : M) : r • (x - y) = r • x - r • y := by simp [smul_add, sub_eq_add_neg]; rw smul_neg end add_comm_group /-- Modules are defined as an `abbreviation` for semimodules, if the base semiring is a ring. (A previous definition made `module` a structure defined to be `semimodule`.) This has as advantage that modules are completely transparent for type class inference, which means that all instances for semimodules are immediately picked up for modules as well. A cosmetic disadvantage is that one can not extend modules as such, in definitions such as `normed_space`. The solution is to extend `semimodule` instead. -/ library_note "module definition" /-- A module is the same as a semimodule, except the scalar semiring is actually a ring. This is the traditional generalization of spaces like `ℤ^n`, which have a natural addition operation and a way to multiply them by elements of a ring, but no multiplication operation between vectors. -/ abbreviation module (R : Type u) (M : Type v) [ring R] [add_comm_group M] := semimodule R M /-- To prove two module structures on a fixed `add_comm_group` agree, it suffices to check the scalar multiplications agree. -/ -- We'll later use this to show `module ℤ M` is a subsingleton. @[ext] lemma module_ext {R : Type*} [ring R] {M : Type*} [add_comm_group M] (P Q : module R M) (w : ∀ (r : R) (m : M), by { haveI := P, exact r • m } = by { haveI := Q, exact r • m }) : P = Q := begin resetI, rcases P with ⟨⟨⟨⟨P⟩⟩⟩⟩, rcases Q with ⟨⟨⟨⟨Q⟩⟩⟩⟩, congr, funext r m, exact w r m, all_goals { apply proof_irrel_heq }, end section module variables [ring R] [add_comm_group M] [module R M] (r s : R) (x y : M) @[simp] theorem neg_smul : -r • x = - (r • x) := eq_neg_of_add_eq_zero (by rw [← add_smul, add_left_neg, zero_smul]) variables (R) theorem neg_one_smul (x : M) : (-1 : R) • x = -x := by simp variables {R} theorem sub_smul (r s : R) (y : M) : (r - s) • y = r • y - s • y := by simp [add_smul, sub_eq_add_neg] theorem smul_eq_zero {R E : Type*} [division_ring R] [add_comm_group E] [module R E] {c : R} {x : E} : c • x = 0 ↔ c = 0 ∨ x = 0 := ⟨λ h, classical.or_iff_not_imp_left.2 $ λ hc, (units.mk0 c hc).smul_eq_zero.1 h, λ h, h.elim (λ hc, hc.symm ▸ zero_smul R x) (λ hx, hx.symm ▸ smul_zero c)⟩ end module section set_option default_priority 910 instance semiring.to_semimodule [semiring R] : semimodule R R := { smul := (*), smul_add := mul_add, add_smul := add_mul, mul_smul := mul_assoc, one_smul := one_mul, zero_smul := zero_mul, smul_zero := mul_zero } end @[simp] lemma smul_eq_mul [semiring R] {a a' : R} : a • a' = a * a' := rfl /-- A ring homomorphism `f : R →+* M` defines a module structure by `r • x = f r * x`. -/ def ring_hom.to_semimodule [semiring R] [semiring S] (f : R →+* S) : semimodule R S := { smul := λ r x, f r * x, smul_add := λ r x y, by unfold has_scalar.smul; rw [mul_add], add_smul := λ r s x, by unfold has_scalar.smul; rw [f.map_add, add_mul], mul_smul := λ r s x, by unfold has_scalar.smul; rw [f.map_mul, mul_assoc], one_smul := λ x, show f 1 * x = _, by rw [f.map_one, one_mul], zero_smul := λ x, show f 0 * x = 0, by rw [f.map_zero, zero_mul], smul_zero := λ r, mul_zero (f r) } /-- A map `f` between semimodules over a semiring is linear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = c • f x`. The predicate `is_linear_map R f` asserts this property. A bundled version is available with `linear_map`, and should be favored over `is_linear_map` most of the time. -/ structure is_linear_map (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₂] (f : M → M₂) : Prop := (map_add : ∀ x y, f (x + y) = f x + f y) (map_smul : ∀ (c : R) x, f (c • x) = c • f x) /-- A map `f` between semimodules over a semiring is linear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = c • f x`. Elements of `linear_map R M M₂` (available under the notation `M →ₗ[R] M₂`) are bundled versions of such maps. An unbundled version is available with the predicate `is_linear_map`, but it should be avoided most of the time. -/ structure linear_map (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₂] := (to_fun : M → M₂) (map_add' : ∀x y, to_fun (x + y) = to_fun x + to_fun y) (map_smul' : ∀(c : R) x, to_fun (c • x) = c • to_fun x) infixr ` →ₗ `:25 := linear_map _ notation M ` →ₗ[`:25 R:25 `] `:0 M₂:0 := linear_map R M M₂ namespace linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] section variables [semimodule R M] [semimodule R M₂] instance : has_coe_to_fun (M →ₗ[R] M₂) := ⟨_, to_fun⟩ @[simp] lemma coe_mk (f : M → M₂) (h₁ h₂) : ((linear_map.mk f h₁ h₂ : M →ₗ[R] M₂) : M → M₂) = f := rfl /-- Identity map as a `linear_map` -/ def id : M →ₗ[R] M := ⟨id, λ _ _, rfl, λ _ _, rfl⟩ @[simp] lemma id_apply (x : M) : @id R M _ _ _ x = x := rfl end section -- We can infer the module structure implicitly from the linear maps, -- rather than via typeclass resolution. variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f g : M →ₗ[R] M₂) @[simp] lemma to_fun_eq_coe : f.to_fun = ⇑f := rfl theorem is_linear : is_linear_map R f := ⟨f.2, f.3⟩ variables {f g} @[ext] theorem ext (H : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H lemma coe_fn_congr : Π {x x' : M}, x = x' → f x = f x' | _ _ rfl := rfl theorem ext_iff : f = g ↔ ∀ x, f x = g x := ⟨by { rintro rfl x, refl } , ext⟩ variables (f g) @[simp] lemma map_add (x y : M) : f (x + y) = f x + f y := f.map_add' x y @[simp] lemma map_smul (c : R) (x : M) : f (c • x) = c • f x := f.map_smul' c x @[simp] lemma map_zero : f 0 = 0 := by rw [← zero_smul R, map_smul f 0 0, zero_smul] instance : is_add_monoid_hom f := { map_add := map_add f, map_zero := map_zero f } /-- convert a linear map to an additive map -/ def to_add_monoid_hom : M →+ M₂ := { to_fun := f, map_zero' := f.map_zero, map_add' := f.map_add } @[simp] lemma to_add_monoid_hom_coe : ((f.to_add_monoid_hom) : M → M₂) = f := rfl @[simp] lemma map_sum {ι} {t : finset ι} {g : ι → M} : f (∑ i in t, g i) = (∑ i in t, f (g i)) := f.to_add_monoid_hom.map_sum _ _ end section variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} {semimodule_M₃ : semimodule R M₃} variables (f : M₂ →ₗ[R] M₃) (g : M →ₗ[R] M₂) /-- Composition of two linear maps is a linear map -/ def comp : M →ₗ[R] M₃ := ⟨f ∘ g, by simp, by simp⟩ @[simp] lemma comp_apply (x : M) : f.comp g x = f (g x) := rfl end end add_comm_monoid section add_comm_group variables [semiring 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₂) @[simp] lemma map_neg (x : M) : f (- x) = - f x := f.to_add_monoid_hom.map_neg x @[simp] lemma map_sub (x y : M) : f (x - y) = f x - f y := f.to_add_monoid_hom.map_sub x y instance : is_add_group_hom f := { map_add := map_add f} end add_comm_group end linear_map namespace is_linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] variables [semimodule R M] [semimodule R M₂] include R /-- Convert an `is_linear_map` predicate to a `linear_map` -/ def mk' (f : M → M₂) (H : is_linear_map R f) : M →ₗ M₂ := ⟨f, H.1, H.2⟩ @[simp] theorem mk'_apply {f : M → M₂} (H : is_linear_map R f) (x : M) : mk' f H x = f x := rfl lemma is_linear_map_smul {R M : Type*} [comm_semiring R] [add_comm_monoid M] [semimodule R M] (c : R) : is_linear_map R (λ (z : M), c • z) := begin refine is_linear_map.mk (smul_add c) _, intros _ _, simp only [smul_smul, mul_comm] end --TODO: move lemma is_linear_map_smul' {R M : Type*} [semiring R] [add_comm_monoid M] [semimodule R M] (a : M) : is_linear_map R (λ (c : R), c • a) := is_linear_map.mk (λ x y, add_smul x y a) (λ x y, mul_smul x y a) variables {f : M → M₂} (lin : is_linear_map R f) include M M₂ lin lemma map_zero : f (0 : M) = (0 : M₂) := (lin.mk' f).map_zero end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] variables [semimodule R M] [semimodule R M₂] include R lemma is_linear_map_neg : is_linear_map R (λ (z : M), -z) := is_linear_map.mk neg_add (λ x y, (smul_neg x y).symm) variables {f : M → M₂} (lin : is_linear_map R f) include M M₂ lin lemma map_neg (x : M) : f (- x) = - f x := (lin.mk' f).map_neg x lemma map_sub (x y) : f (x - y) = f x - f y := (lin.mk' f).map_sub x y end add_comm_group end is_linear_map /-- Ring of linear endomorphismsms of a module. -/ abbreviation module.End (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [semimodule R M] := M →ₗ[R] M set_option old_structure_cmd true /-- A submodule of a module is one which is closed under vector operations. This is a sufficient condition for the subset of vectors in the submodule to themselves form a module. -/ structure submodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [semimodule R M] extends add_submonoid M : Type v := (smul_mem' : ∀ (c:R) {x}, x ∈ carrier → c • x ∈ carrier) /-- Reinterpret a `submodule` as an `add_submonoid`. -/ add_decl_doc submodule.to_add_submonoid namespace submodule variables [semiring R] [add_comm_monoid M] [semimodule R M] instance : has_coe_t (submodule R M) (set M) := ⟨λ s, s.carrier⟩ instance : has_mem M (submodule R M) := ⟨λ x p, x ∈ (p : set M)⟩ instance : has_coe_to_sort (submodule R M) := ⟨_, λ p, {x : M // x ∈ p}⟩ variables (p q : submodule R M) @[simp, norm_cast] theorem coe_sort_coe : ↥(p : set M) = p := rfl variables {p q} protected theorem «exists» {q : p → Prop} : (∃ x, q x) ↔ (∃ x ∈ p, q ⟨x, ‹_›⟩) := set_coe.exists protected theorem «forall» {q : p → Prop} : (∀ x, q x) ↔ (∀ x ∈ p, q ⟨x, ‹_›⟩) := set_coe.forall theorem coe_injective : injective (coe : submodule R M → set M) := λ p q h, by cases p; cases q; congr' @[simp, norm_cast] theorem coe_set_eq : (p : set M) = q ↔ p = q := coe_injective.eq_iff theorem ext'_iff : p = q ↔ (p : set M) = q := coe_set_eq.symm @[ext] theorem ext (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := coe_injective $ set.ext h theorem to_add_submonoid_injective : injective (to_add_submonoid : submodule R M → add_submonoid M) := λ p q h, ext'_iff.2 $ add_submonoid.ext'_iff.1 h @[simp] theorem to_add_submonoid_eq : p.to_add_submonoid = q.to_add_submonoid ↔ p = q := to_add_submonoid_injective.eq_iff end submodule namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] -- We can infer the module structure implicitly from the bundled submodule, -- rather than via typeclass resolution. variables {semimodule_M : semimodule R M} variables {p q : submodule R M} variables {r : R} {x y : M} variables (p) @[simp] theorem mem_coe : x ∈ (p : set M) ↔ x ∈ p := iff.rfl @[simp] lemma zero_mem : (0 : M) ∈ p := p.zero_mem' lemma add_mem (h₁ : x ∈ p) (h₂ : y ∈ p) : x + y ∈ p := p.add_mem' h₁ h₂ lemma smul_mem (r : R) (h : x ∈ p) : r • x ∈ p := p.smul_mem' r h lemma sum_mem {t : finset ι} {f : ι → M} : (∀c∈t, f c ∈ p) → (∑ i in t, f i) ∈ p := p.to_add_submonoid.sum_mem @[simp] lemma smul_mem_iff' (u : units R) : (u:R) • x ∈ p ↔ x ∈ p := ⟨λ h, by simpa only [smul_smul, u.inv_mul, one_smul] using p.smul_mem ↑u⁻¹ h, p.smul_mem u⟩ instance : has_add p := ⟨λx y, ⟨x.1 + y.1, add_mem _ x.2 y.2⟩⟩ instance : has_zero p := ⟨⟨0, zero_mem _⟩⟩ instance : inhabited p := ⟨0⟩ instance : has_scalar R p := ⟨λ c x, ⟨c • x.1, smul_mem _ c x.2⟩⟩ @[simp] lemma mk_eq_zero {x} (h : x ∈ p) : (⟨x, h⟩ : p) = 0 ↔ x = 0 := subtype.ext variables {p} @[simp, norm_cast] lemma coe_eq_coe {x y : p} : (x : M) = y ↔ x = y := subtype.ext.symm @[simp, norm_cast] lemma coe_eq_zero {x : p} : (x : M) = 0 ↔ x = 0 := @coe_eq_coe _ _ _ _ _ _ x 0 @[simp, norm_cast] lemma coe_add (x y : p) : (↑(x + y) : M) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : p) : M) = 0 := rfl @[simp, norm_cast] lemma coe_smul (r : R) (x : p) : ((r • x : p) : M) = r • ↑x := rfl @[simp, norm_cast] lemma coe_mk (x : M) (hx : x ∈ p) : ((⟨x, hx⟩ : p) : M) = x := rfl @[simp] lemma coe_mem (x : p) : (x : M) ∈ p := x.2 @[simp] protected lemma eta (x : p) (hx : (x : M) ∈ p) : (⟨x, hx⟩ : p) = x := subtype.eta x hx variables (p) instance : add_comm_monoid p := { add := (+), zero := 0, .. p.to_add_submonoid.to_add_comm_monoid } instance : semimodule R p := by refine {smul := (•), ..}; { intros, apply set_coe.ext, simp [smul_add, add_smul, mul_smul] } /-- Embedding of a submodule `p` to the ambient space `M`. -/ protected def subtype : p →ₗ[R] M := by refine {to_fun := coe, ..}; simp [coe_smul] @[simp] theorem subtype_apply (x : p) : p.subtype x = x := rfl lemma subtype_eq_val : ((submodule.subtype p) : p → M) = subtype.val := rfl end add_comm_monoid section add_comm_group variables [ring R] [add_comm_group M] variables {semimodule_M : semimodule R M} variables (p p' : submodule R M) variables {r : R} {x y : M} lemma neg_mem (hx : x ∈ p) : -x ∈ p := by rw ← neg_one_smul R; exact p.smul_mem _ hx /-- Reinterpret a submodule as an additive subgroup. -/ def to_add_subgroup : add_subgroup M := { neg_mem' := λ _, p.neg_mem , .. p.to_add_submonoid } @[simp] lemma coe_to_add_subgroup : (p.to_add_subgroup : set M) = p := rfl lemma sub_mem (hx : x ∈ p) (hy : y ∈ p) : x - y ∈ p := p.add_mem hx (p.neg_mem hy) @[simp] lemma neg_mem_iff : -x ∈ p ↔ x ∈ p := p.to_add_subgroup.neg_mem_iff lemma add_mem_iff_left (h₁ : y ∈ p) : x + y ∈ p ↔ x ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, λ h₂, add_mem _ h₂ h₁⟩ lemma add_mem_iff_right (h₁ : x ∈ p) : x + y ∈ p ↔ y ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, add_mem _ h₁⟩ instance : has_neg p := ⟨λx, ⟨-x.1, neg_mem _ x.2⟩⟩ @[simp, norm_cast] lemma coe_neg (x : p) : ((-x : p) : M) = -x := rfl instance : add_comm_group p := { add := (+), zero := 0, neg := has_neg.neg, ..p.to_add_subgroup.to_add_comm_group } @[simp, norm_cast] lemma coe_sub (x y : p) : (↑(x - y) : M) = ↑x - ↑y := rfl end add_comm_group end submodule -- TODO: Do we want one-sided ideals? /-- Ideal in a commutative ring is an additive subgroup `s` such that `a * b ∈ s` whenever `b ∈ s`. We define `ideal R` as `submodule R R`. -/ @[reducible] def ideal (R : Type u) [comm_ring R] := submodule R R namespace ideal variables [comm_ring R] (I : ideal R) {a b : R} protected lemma zero_mem : (0 : R) ∈ I := I.zero_mem protected lemma add_mem : a ∈ I → b ∈ I → a + b ∈ I := I.add_mem lemma neg_mem_iff : -a ∈ I ↔ a ∈ I := I.neg_mem_iff lemma add_mem_iff_left : b ∈ I → (a + b ∈ I ↔ a ∈ I) := I.add_mem_iff_left lemma add_mem_iff_right : a ∈ I → (a + b ∈ I ↔ b ∈ I) := I.add_mem_iff_right protected lemma sub_mem : a ∈ I → b ∈ I → a - b ∈ I := I.sub_mem lemma mul_mem_left : b ∈ I → a * b ∈ I := I.smul_mem _ lemma mul_mem_right (h : a ∈ I) : a * b ∈ I := mul_comm b a ▸ I.mul_mem_left h end ideal /-- Vector spaces are defined as an `abbreviation` for semimodules, if the base ring is a field. (A previous definition made `vector_space` a structure defined to be `module`.) This has as advantage that vector spaces are completely transparent for type class inference, which means that all instances for semimodules are immediately picked up for vector spaces as well. A cosmetic disadvantage is that one can not extend vector spaces as such, in definitions such as `normed_space`. The solution is to extend `semimodule` instead. -/ library_note "vector space definition" /-- A vector space is the same as a module, except the scalar ring is actually a field. (This adds commutativity of the multiplication and existence of inverses.) This is the traditional generalization of spaces like `ℝ^n`, which have a natural addition operation and a way to multiply them by real numbers, but no multiplication operation between vectors. -/ abbreviation vector_space (R : Type u) (M : Type v) [field R] [add_comm_group M] := semimodule R M /-- Subspace of a vector space. Defined to equal `submodule`. -/ abbreviation subspace (R : Type u) (M : Type v) [field R] [add_comm_group M] [vector_space R M] := submodule R M namespace submodule variables [division_ring R] [add_comm_group M] [module R M] variables (p : submodule R M) {r : R} {x y : M} theorem smul_mem_iff (r0 : r ≠ 0) : r • x ∈ p ↔ x ∈ p := p.smul_mem_iff' (units.mk0 r r0) end submodule namespace add_comm_monoid open add_monoid variables [add_comm_monoid M] /-- The natural ℕ-semimodule structure on any `add_comm_monoid`. -/ -- We don't make this a global instance, as it results in too many instances, -- and confusing ambiguity in the notation `n • x` when `n : ℕ`. def nat_semimodule : semimodule ℕ M := { smul := nsmul, smul_add := λ _ _ _, nsmul_add _ _ _, add_smul := λ _ _ _, add_nsmul _ _ _, mul_smul := λ _ _ _, mul_nsmul _ _ _, one_smul := one_nsmul, zero_smul := zero_nsmul, smul_zero := nsmul_zero } end add_comm_monoid namespace add_comm_group variables [add_comm_group M] /-- The natural ℤ-module structure on any `add_comm_group`. -/ -- We don't immediately make this a global instance, as it results in too many instances, -- and confusing ambiguity in the notation `n • x` when `n : ℤ`. -- We do turn it into a global instance, but only at the end of this file, -- and I remain dubious whether this is a good idea. def int_module : module ℤ M := { smul := gsmul, smul_add := λ _ _ _, gsmul_add _ _ _, add_smul := λ _ _ _, add_gsmul _ _ _, mul_smul := λ _ _ _, gsmul_mul _ _ _, one_smul := one_gsmul, zero_smul := zero_gsmul, smul_zero := gsmul_zero } instance : subsingleton (module ℤ M) := begin split, intros P Q, ext, -- isn't that lovely: `r • m = r • m` have one_smul : by { haveI := P, exact (1 : ℤ) • m } = by { haveI := Q, exact (1 : ℤ) • m }, begin rw [@one_smul ℤ _ _ (by { haveI := P, apply_instance, }) m], rw [@one_smul ℤ _ _ (by { haveI := Q, apply_instance, }) m], end, have nat_smul : ∀ n : ℕ, by { haveI := P, exact (n : ℤ) • m } = by { haveI := Q, exact (n : ℤ) • m }, begin intro n, induction n with n ih, { erw [zero_smul, zero_smul], }, { rw [int.coe_nat_succ, add_smul, add_smul], erw ih, rw [one_smul], } end, cases r, { rw [int.of_nat_eq_coe, nat_smul], }, { rw [int.neg_succ_of_nat_coe, neg_smul, neg_smul, nat_smul], } end end add_comm_group section local attribute [instance] add_comm_monoid.nat_semimodule lemma semimodule.smul_eq_smul (R : Type*) [semiring R] {M : Type*} [add_comm_monoid M] [semimodule R M] (n : ℕ) (b : M) : n • b = (n : R) • b := begin induction n with n ih, { rw [nat.cast_zero, zero_smul, zero_smul] }, { change (n + 1) • b = (n + 1 : R) • b, rw [add_smul, add_smul, one_smul, ih, one_smul] } end lemma semimodule.nsmul_eq_smul (R : Type*) [semiring R] {M : Type*} [add_comm_monoid M] [semimodule R M] (n : ℕ) (b : M) : n •ℕ b = (n : R) • b := semimodule.smul_eq_smul R n b lemma nat.smul_def {M : Type*} [add_comm_monoid M] (n : ℕ) (x : M) : n • x = n •ℕ x := rfl end section local attribute [instance] add_comm_group.int_module lemma gsmul_eq_smul {M : Type*} [add_comm_group M] (n : ℤ) (x : M) : gsmul n x = n • x := rfl lemma module.gsmul_eq_smul_cast (R : Type*) [ring R] {M : Type*} [add_comm_group M] [module R M] (n : ℤ) (b : M) : gsmul n b = (n : R) • b := begin cases n, { apply semimodule.nsmul_eq_smul, }, { dsimp, rw semimodule.nsmul_eq_smul R, push_cast, rw neg_smul, } end lemma module.gsmul_eq_smul {M : Type*} [add_comm_group M] [module ℤ M] (n : ℤ) (b : M) : gsmul n b = n • b := by rw [module.gsmul_eq_smul_cast ℤ, int.cast_id] end -- We prove this without using the `add_comm_group.int_module` instance, so the `•`s here -- come from whatever the local `module ℤ` structure actually is. lemma add_monoid_hom.map_int_module_smul [add_comm_group M] [add_comm_group M₂] [module ℤ M] [module ℤ M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f (x • a) = x • f a := by simp only [← module.gsmul_eq_smul, f.map_gsmul] lemma add_monoid_hom.map_int_cast_smul [ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [← module.gsmul_eq_smul_cast, f.map_gsmul] lemma add_monoid_hom.map_nat_cast_smul [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] (f : M →+ M₂) (x : ℕ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [← semimodule.nsmul_eq_smul, f.map_nsmul] lemma add_monoid_hom.map_rat_cast_smul {R : Type*} [division_ring R] [char_zero R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] (f : E →+ F) (c : ℚ) (x : E) : f ((c : R) • x) = (c : R) • f x := begin have : ∀ (x : E) (n : ℕ), 0 < n → f (((n⁻¹ : ℚ) : R) • x) = ((n⁻¹ : ℚ) : R) • f x, { intros x n hn, replace hn : (n : R) ≠ 0 := nat.cast_ne_zero.2 (ne_of_gt hn), conv_rhs { congr, skip, rw [← one_smul R x, ← mul_inv_cancel hn, mul_smul] }, rw [f.map_nat_cast_smul, smul_smul, rat.cast_inv, rat.cast_coe_nat, inv_mul_cancel hn, one_smul] }, refine c.num_denom_cases_on (λ m n hn hmn, _), rw [rat.mk_eq_div, div_eq_mul_inv, rat.cast_mul, int.cast_coe_nat, mul_smul, mul_smul, rat.cast_coe_int, f.map_int_cast_smul, this _ n hn] end lemma add_monoid_hom.map_rat_module_smul {E : Type*} [add_comm_group E] [vector_space ℚ E] {F : Type*} [add_comm_group F] [module ℚ F] (f : E →+ F) (c : ℚ) (x : E) : f (c • x) = c • f x := rat.cast_id c ▸ f.map_rat_cast_smul c x -- We finally turn on these instances globally: attribute [instance] add_comm_monoid.nat_semimodule add_comm_group.int_module /-- Reinterpret an additive homomorphism as a `ℤ`-linear map. -/ def add_monoid_hom.to_int_linear_map [add_comm_group M] [add_comm_group M₂] (f : M →+ M₂) : M →ₗ[ℤ] M₂ := ⟨f, f.map_add, f.map_int_module_smul⟩ /-- Reinterpret an additive homomorphism as a `ℚ`-linear map. -/ def add_monoid_hom.to_rat_linear_map [add_comm_group M] [vector_space ℚ M] [add_comm_group M₂] [vector_space ℚ M₂] (f : M →+ M₂) : M →ₗ[ℚ] M₂ := ⟨f, f.map_add, f.map_rat_module_smul⟩ namespace finset variable (R) lemma sum_const' [semiring R] [add_comm_monoid M] [semimodule R M] {s : finset ι} (b : M) : (∑ i in s, b) = (finset.card s : R) • b := by rw [finset.sum_const, ← semimodule.smul_eq_smul]; refl variables {R} [decidable_linear_ordered_cancel_add_comm_monoid M] {s : finset ι} (f : ι → M) theorem exists_card_smul_le_sum (hs : s.nonempty) : ∃ i ∈ s, s.card • f i ≤ (∑ i in s, f i) := exists_le_of_sum_le hs $ by rw [sum_const, ← nat.smul_def, smul_sum] theorem exists_card_smul_ge_sum (hs : s.nonempty) : ∃ i ∈ s, (∑ i in s, f i) ≤ s.card • f i := exists_le_of_sum_le hs $ by rw [sum_const, ← nat.smul_def, smul_sum] end finset
8ccc1542264e6e28cb701084a76bc1f0b5a74c11
fe84e287c662151bb313504482b218a503b972f3
/src/homotopy/itloc.lean
2d357f228bc870db1aa52120a536a79046641fef
[]
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
71,258
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland This file formalises part of the paper "Iterated chromatic localization" by Nicola Bellumat and Neil Strickland. We have formalised all of the combinatorial content (including the results in the combinatorial homotopy theory of finite posets). We have not formalised any of the theory of derivators. -/ import data.list.basic import data.fin.basic import data.fintype.basic import order.lattice import algebra.big_operators import data.fin_extra import poset.basic poset.upper import tactic.squeeze open poset namespace itloc lemma two_pos' : 2 > 0 := dec_trivial /-- `𝕀 n` is the set {0,1,...,n-1} -/ variable (n : ℕ) def 𝕀 := fin n namespace 𝕀 /-- The type `𝕀 n` is finite, with decidable equality, and there is an obvious way of printing a string representation of any element. The lines below use the `apply_instance` tactic to deal with these things. -/ instance : fintype (𝕀 n) := by { dsimp [𝕀], apply_instance } instance : decidable_eq (𝕀 n) := by { dsimp [𝕀], apply_instance } instance : has_repr (𝕀 n) := by { dsimp [𝕀], apply_instance } instance : linear_order (𝕀 n) := (@fin.linear_order n). end 𝕀 /-- `ℙ n` is the poset of subsets of `𝕀 n`, ordered by inclusion LaTeX: defn-P -/ def ℙ := finset (𝕀 n) namespace ℙ /-- The type `ℙ n` is finite, with decidable equality, and an obvious string representation. Additionally, there is a membership relation between `𝕀 n` and `ℙ n`, for which we have a `has_mem` instance. -/ instance : fintype (ℙ n) := by { dsimp [ ℙ] , apply_instance } instance : decidable_eq (ℙ n) := by { dsimp [ ℙ] , apply_instance } instance : has_mem (𝕀 n) (ℙ n) := by { dsimp [𝕀,ℙ], apply_instance } instance : has_singleton (𝕀 n) (ℙ n) := by { dsimp [𝕀,ℙ], apply_instance } instance : has_repr (ℙ n) := by { dsimp [ ℙ] , apply_instance } /-- The `apply_instance` tactic also knows enough to give `ℙ n` a structure as a distributive lattice. However, the library does not contain any general rule showing that `ℙ n` has a top and bottom element, so we prove that explicitly. -/ instance dl : distrib_lattice (ℙ n) := by { dsimp [ℙ], apply_instance } instance bo : bounded_order (ℙ n) := { bot := finset.empty, top := finset.univ, le_top := λ (A : finset (𝕀 n)), begin change A ⊆ finset.univ,intros i _,exact finset.mem_univ i end, bot_le := λ (A : finset (𝕀 n)), begin change finset.empty ⊆ A,intros i h, exact (finset.not_mem_empty i h).elim end, .. (ℙ.dl n) } /-- From the lattice structure we can obtain a partial order. It is not normally necessary to mention that fact explicitly, but we do so here to avoid some subtle technical issues later. -/ instance po : partial_order (ℙ n) := by apply_instance /-- The only element of `ℙ 0` is the empty set. -/ lemma mem_zero (A : ℙ 0) : A = ⊥ := by {ext a, exact fin.elim0 a} /-- The order relation on `ℙ n` is decidable -/ instance decidable_le : decidable_rel (λ (A B : ℙ n), A ≤ B) := λ (A B : finset (𝕀 n)), by { change decidable (A ⊆ B), apply_instance, } variable {n} /-- The bottom element is the empty set, so nothing is a member. -/ lemma not_mem_bot (i : 𝕀 n) : ¬ (i ∈ (⊥ : ℙ n)) := finset.not_mem_empty i /-- The top element is the full set `ℙ n`, so everything is a member. -/ lemma mem_top (i : 𝕀 n) : (i ∈ (⊤ : ℙ n)) := finset.mem_univ i /-- The membership rule for a union (= sup in the lattice) -/ lemma mem_sup {A B : ℙ n} {i : 𝕀 n} : i ∈ A ⊔ B ↔ (i ∈ A) ∨ (i ∈ B) := finset.mem_union /-- The membership rule for an intersection (= inf in the lattice) -/ lemma mem_inf {A B : ℙ n} {i : 𝕀 n} : i ∈ A ⊓ B ↔ (i ∈ A) ∧ (i ∈ B) := finset.mem_inter /-- `A.filter_lt i` is Lean notation for `{a ∈ A : a < i}` -/ def filter_lt (i : ℕ) (A : ℙ n) := A.filter (λ a, a.val < i) /-- `A.filter_ge i` is Lean notation for `{a ∈ A : a ≥ i}` -/ def filter_ge (i : ℕ) (A : ℙ n) := A.filter (λ a, a.val ≥ i) /-- Membership rule for these sets. -/ lemma mem_filter_lt {i : ℕ} {A : ℙ n} {a : 𝕀 n} : a ∈ (A.filter_lt i) ↔ a ∈ A ∧ a.val < i := finset.mem_filter lemma mem_filter_ge {i : ℕ} {A : ℙ n} {a : 𝕀 n} : a ∈ (A.filter_ge i) ↔ a ∈ A ∧ a.val ≥ i := finset.mem_filter lemma filter_lt_is_le (i : ℕ) (A : ℙ n) : A.filter_lt i ≤ A := λ a ha, (mem_filter_lt.mp ha).left lemma filter_ge_is_le (i : ℕ) (A : ℙ n) : A.filter_ge i ≤ A := λ a ha, (mem_filter_ge.mp ha).left lemma filter_sup {i j : ℕ} (h : i ≥ j) (A : ℙ n) : A.filter_lt i ⊔ A.filter_ge j = A := begin ext a, rw [mem_sup, mem_filter_lt, mem_filter_ge, ← and_or_distrib_left], have : a.val < i ∨ a.val ≥ j := begin rcases lt_or_ge a.val i with h_lt | h_ge, exact or.inl h_lt, exact or.inr (le_trans h h_ge) end, rw [eq_true_intro this, and_true], end lemma filter_lt_zero (A : ℙ n) : A.filter_lt 0 = ⊥ := by { ext a, rw[ℙ.mem_filter_lt], have : ¬ (a ∈ ⊥) := finset.not_mem_empty a, simp only [nat.not_lt_zero,this,iff_self,and_false]} lemma filter_ge_zero (A : ℙ n) : A.filter_ge 0 = A := by { ext a, rw[ℙ.mem_filter_ge], have : a.val ≥ 0 := nat.zero_le a.val, simp only [this,iff_self,and_true]} lemma filter_lt_last {i : ℕ} (hi : i ≥ n) (A : ℙ n) : A.filter_lt i = A := begin ext a, rw[ℙ.mem_filter_lt], simp only [lt_of_lt_of_le a.is_lt hi, and_true, fin.val_eq_coe] end lemma filter_ge_last {i : ℕ} (hi : i ≥ n) (A : ℙ n) : A.filter_ge i = ⊥ := begin ext a, rw[ℙ.mem_filter_ge], have h₀ : ¬ (a ∈ ⊥) := finset.not_mem_empty a, have h₁ : ¬ (a.val ≥ i) := not_le_of_gt (lt_of_lt_of_le a.is_lt hi), simp only [h₀, h₁, iff_self, and_false] end /- For subsets `A,B ⊆ 𝕀 n`, the notation `A ∟ B` means that every element of `A` is less than or equal to every element of `B` LaTeX: defn-P -/ def angle : (ℙ n) → (ℙ n) → Prop := λ A B, ∀ {{i j : 𝕀 n}}, i ∈ A → j ∈ B → i ≤ j reserve infix ` ∟ `:50 notation A ∟ B := angle A B /-- The angle relation is decidable -/ instance : ∀ (A B : ℙ n), decidable (angle A B) := by { dsimp [angle], apply_instance } lemma bot_angle (B : ℙ n) : ⊥ ∟ B := λ i j i_in_A j_in_B, (finset.not_mem_empty i i_in_A).elim lemma angle_bot (A : ℙ n) : A ∟ ⊥ := λ i j i_in_A j_in_B, (finset.not_mem_empty j j_in_B).elim lemma sup_angle (A B C : ℙ n) : A ⊔ B ∟ C ↔ (A ∟ C) ∧ (B ∟ C) := begin split, { rintro hAB, split, exact λ i k i_in_A k_in_C, hAB (finset.subset_union_left A B i_in_A) k_in_C, exact λ j k j_in_B k_in_C, hAB (finset.subset_union_right A B j_in_B) k_in_C }, { rintro ⟨hA,hB⟩ i k i_in_AB k_in_C, rcases finset.mem_union.mp i_in_AB with i_in_A | i_in_B, exact hA i_in_A k_in_C, exact hB i_in_B k_in_C } end lemma angle_sup (A B C : ℙ n) : A ∟ B ⊔ C ↔ (A ∟ B) ∧ (A ∟ C) := begin split, { rintro hBC, split, exact λ i j i_in_A j_in_B, hBC i_in_A (finset.subset_union_left B C j_in_B), exact λ i k i_in_A k_in_C, hBC i_in_A (finset.subset_union_right B C k_in_C) }, { rintro ⟨hB,hC⟩ i j i_in_A j_in_BC, rcases finset.mem_union.mp j_in_BC with j_in_B | j_in_C, exact hB i_in_A j_in_B, exact hC i_in_A j_in_C } end lemma filter_angle {i j : ℕ} (h : i ≤ j + 1) (A : ℙ n) : (A.filter_lt i) ∟ (A.filter_ge j) := begin intros a b ha hb, replace ha : a.val + 1 ≤ i := (mem_filter_lt.mp ha).right, replace hb := nat.succ_le_succ (mem_filter_ge.mp hb).right, exact nat.le_of_succ_le_succ (le_trans (le_trans ha h) hb) end lemma angle_mono {A₀ A₁ B₀ B₁ : ℙ n} (hA : A₀ ≤ A₁) (hB : B₀ ≤ B₁) (h : A₁ ∟ B₁) : A₀ ∟ B₀ := λ x y hx hy, h (hA hx) (hB hy) lemma split_angle {A B : ℙ n} (k : 𝕀 n) (hA : ∀ i, i ∈ A → i ≤ k) (hB : ∀ j, j ∈ B → k ≤ j) : A ∟ B := λ i j i_in_A j_in_B, le_trans (hA i i_in_A) (hB j j_in_B) lemma angle_iff {A B : ℙ n} : A ∟ B ↔ n = 0 ∨ ∃ (k : 𝕀 n), (∀ {i}, (i ∈ A) → i ≤ k) ∧ (∀ {i}, i ∈ B → k ≤ i) := begin cases n with n, { rw[A.mem_zero, B.mem_zero], simp only [bot_angle], simp }, split, { intro h_angle, right, let z : 𝕀 n.succ := ⟨0,nat.zero_lt_succ n⟩, let A0 : finset (𝕀 n.succ) := insert z A, rcases fin.finset_largest_element A0 ⟨_,finset.mem_insert_self z A⟩ with ⟨k, k_in_A0, k_largest⟩, use k, split, { intros a a_in_A, exact k_largest a (finset.mem_insert_of_mem a_in_A) }, { intros b b_in_B, rcases (finset.mem_insert.mp k_in_A0) with k_eq_z | k_in_A, { rw [k_eq_z], exact nat.zero_le b.val }, { exact h_angle k_in_A b_in_B } } }, { rintro (⟨⟨⟩⟩ | ⟨k,hA,hB⟩) a b a_in_A b_in_B, exact le_trans (hA a_in_A) (hB b_in_B) } end lemma not_angle_iff {A B : ℙ n} : ¬ (A ∟ B) ↔ (∃ (a ∈ A) (b ∈ B), a > b) := begin dsimp[angle], rw [not_forall], apply exists_congr, intro a, rw [not_forall], split; intro h, { rcases h with ⟨b,hb⟩, rw[not_imp, not_imp, ← lt_iff_not_ge] at hb, exact ⟨hb.1,b,hb.2.1,hb.2.2⟩ }, { rcases h with ⟨ha,b,hb,h_lt⟩, change b < a at h_lt, use b, simp [ha, hb, h_lt] } end lemma angle_iff' {A B : ℙ n} : A ∟ B ↔ (A = ⊥ ∧ B = ⊥) ∨ ∃ (k : 𝕀 n), (∀ i, (i ∈ A) → i ≤ k) ∧ (∀ i, i ∈ B → k ≤ i) := begin split, {intro h, by_cases hA : A.nonempty, { rcases (fin.finset_largest_element A hA) with ⟨k,⟨k_in_A,k_largest⟩⟩, right,use k,split, {exact k_largest}, {intros j j_in_B,exact h k_in_A j_in_B,} },{ by_cases hB : B.nonempty, {rcases (fin.finset_least_element B hB) with ⟨k,⟨k_in_B,k_least⟩⟩, right,use k,split, {intros i i_in_A,exact h i_in_A k_in_B,}, {exact k_least,} }, {left, split, exact finset.not_nonempty_iff_eq_empty.mp hA, exact finset.not_nonempty_iff_eq_empty.mp hB} }, }, {rintro (⟨A_empty,B_empty⟩ | ⟨k,⟨hA,hB⟩⟩), {rw[A_empty],exact bot_angle B}, {exact split_angle k hA hB,} } end end ℙ /- We define `𝕂 n` to be the poset of upwards-closed subsets of `ℙ n`, ordered by reverse inclusion. LaTeX: defn-Q -/ variable (n) def 𝕂 := poset.upper (ℙ n) namespace 𝕂 instance : distrib_lattice (𝕂 n) := @upper.dl (ℙ n) _ _ _ _ instance : bounded_order (𝕂 n) := @upper.bo (ℙ n) _ _ _ _ instance : partial_order (𝕂 n) := by apply_instance instance : fintype (𝕂 n) := upper.fintype (ℙ n) instance : has_repr (𝕂 n) := upper.has_repr (ℙ n) instance ℙ_mem_𝕂 : has_mem (ℙ n) (𝕂 n) := by { unfold 𝕂, apply_instance } instance decidable_mem (A : ℙ n) (U : 𝕂 n) : decidable (A ∈ U) := by { change decidable (A ∈ U.val), apply_instance } variable {n} /-- LaTeX: defn-Q -/ def u : poset.hom (ℙ n) (𝕂 n) := upper.u lemma mem_u {T : ℙ n} {A : ℙ n} : A ∈ @u n T ↔ T ≤ A := begin change A ∈ finset.filter _ _ ↔ T ≤ A, simp [finset.mem_filter, finset.mem_univ] end /-- LaTeX: defn-Q -/ def v (T : ℙ n) : (𝕂 n) := ⟨ finset.univ.filter (λ A, ∃ i, i ∈ A ∧ i ∈ T), begin rintro A B A_le_B h, rw [finset.mem_filter] at *, rcases h with ⟨A_in_univ,i,i_in_A,i_in_T⟩, exact ⟨finset.mem_univ B,⟨i,⟨A_le_B i_in_A,i_in_T⟩⟩⟩ end ⟩ lemma mem_v {T : ℙ n} {A : ℙ n} : A ∈ v T ↔ ∃ i, i ∈ A ∧ i ∈ T := begin change (A ∈ finset.filter _ _) ↔ _, rw [finset.mem_filter], simp [finset.mem_univ A] end lemma v_mono {T₀ T₁ : ℙ n} (h : T₀ ≤ T₁) : v T₀ ≥ v T₁ := begin intros A h₀, rcases finset.mem_filter.mp h₀ with ⟨A_in_univ,⟨i,i_in_A,i_in_T₀⟩⟩, exact finset.mem_filter.mpr ⟨A_in_univ,⟨i,i_in_A,h i_in_T₀⟩⟩ end /- We make `𝕂 n` into a monoid as follows: `U * V` is the set of all sets of the form `A ∪ B`, where `A ∈ U` and `B ∈ V` and `A ∟ B`. The monoid structure is compatible with the partial order, and this allows us to regard `𝕂 n` as a monoidal category (in which all hom sets have size at most one). LaTeX: lem-mu -/ def mul0 (U V : finset (ℙ n)) : finset (ℙ n) := U.bUnion (λ A, (V.filter (λ B,A ∟ B)).image (λ B, A ⊔ B)) lemma mem_mul0 (U V : finset (ℙ n)) (C : ℙ n) : (C ∈ (mul0 U V)) ↔ ∃ A B, A ∈ U ∧ B ∈ V ∧ (A ∟ B) ∧ (A ⊔ B = C) := begin split, {intro hC, rcases finset.mem_bUnion.mp hC with ⟨A,⟨A_in_U,C_in_image⟩⟩, rcases finset.mem_image.mp C_in_image with ⟨B,⟨B_in_filter,e⟩⟩, rcases finset.mem_filter.mp B_in_filter with ⟨B_in_V,A_angle_B⟩, use A, use B, exact ⟨A_in_U,B_in_V,A_angle_B,e⟩, },{ rintro ⟨A,B,A_in_U,B_in_V,A_angle_B,e⟩, apply finset.mem_bUnion.mpr,use A,use A_in_U, apply finset.mem_image.mpr,use B, have B_in_filter : B ∈ V.val.filter _ := finset.mem_filter.mpr ⟨B_in_V,A_angle_B⟩, use B_in_filter, exact e, } end lemma bot_mul0 (V : finset (ℙ n)) (hV : is_upper V) : mul0 finset.univ V = V := begin ext C, rw[mem_mul0 finset.univ V C], split, {rintro ⟨A,B,hA,hB,hAB,hC⟩, have B_le_C : B ≤ C := hC ▸ (@lattice.le_sup_right _ _ A B), exact hV B C B_le_C hB, },{ intro C_in_V, have A_in_U : (⊥ : ℙ n) ∈ finset.univ := @finset.mem_univ (ℙ n) _ ⊥, use ⊥,use C, exact ⟨A_in_U,C_in_V,C.bot_angle,bot_sup_eq⟩, } end lemma mul0_bot (U : finset (ℙ n)) (hU : is_upper U) : mul0 U finset.univ = U := begin ext C, rw[mem_mul0 U finset.univ C], split, {rintro ⟨A,B,hA,hB,hAB,hC⟩, have A_le_C : A ≤ C := hC ▸ (@lattice.le_sup_left _ _ A B), exact hU A C A_le_C hA, },{ intro C_in_U, have B_in_V : (⊥ : ℙ n) ∈ (⊥ : 𝕂 n) := @finset.mem_univ (ℙ n) _ ⊥, use C,use ⊥, exact ⟨C_in_U,B_in_V,C.angle_bot,sup_bot_eq⟩, } end lemma is_upper_mul0 (U V : finset (ℙ n)) (hU : is_upper U) (hV : is_upper V) : is_upper (mul0 U V) := begin intros C C' C_le_C' C_in_mul, rcases (mem_mul0 U V C).mp C_in_mul with ⟨A,B,A_in_U,B_in_V,A_angle_B,e⟩, apply (mem_mul0 U V C').mpr, rcases (ℙ.angle_iff.mp A_angle_B) with ⟨⟨⟩⟩ | ⟨k,⟨hA,hB⟩⟩, { rw[C.mem_zero] at *, rw[C'.mem_zero] at *, use A, use B, exact ⟨A_in_U, B_in_V, A_angle_B, e⟩ }, { let A' := C'.filter (λ i, i ≤ k), let B' := C'.filter (λ j, k ≤ j), have A'_angle_B' : A' ∟ B' := λ i j i_in_A' j_in_B', le_trans (finset.mem_filter.mp i_in_A').right (finset.mem_filter.mp j_in_B').right, have A_le_C : A ≤ C := e ▸ (@lattice.le_sup_left _ _ A B), have B_le_C : B ≤ C := e ▸ (@lattice.le_sup_right _ _ A B), have A_le_A' : A ≤ A' := λ i i_in_A, finset.mem_filter.mpr ⟨(le_trans A_le_C C_le_C') i_in_A,hA i_in_A⟩, have B_le_B' : B ≤ B' := λ j j_in_B, finset.mem_filter.mpr ⟨(le_trans B_le_C C_le_C') j_in_B,hB j_in_B⟩, have A'_in_U : A' ∈ U.val := hU A A' A_le_A' A_in_U, have B'_in_V : B' ∈ V.val := hV B B' B_le_B' B_in_V, have eC' : C' = A' ⊔ B' := begin ext i,split, { intro i_in_C', by_cases h : i ≤ k, { exact finset.mem_union_left B' (finset.mem_filter.mpr ⟨i_in_C',h⟩)}, { replace h := le_of_lt (lt_of_not_ge h), exact finset.mem_union_right A' (finset.mem_filter.mpr ⟨i_in_C',h⟩) } }, { intro i_in_union, rcases finset.mem_union.mp i_in_union with i_in_A' | i_in_B', { exact (finset.mem_filter.mp i_in_A').left }, { exact (finset.mem_filter.mp i_in_B').left } } end, use A', use B', exact ⟨A'_in_U,B'_in_V,A'_angle_B',eC'.symm⟩, } end variable (n) instance : monoid (𝕂 n) := { one := ⊥, mul := λ U V, ⟨mul0 U.val V.val,is_upper_mul0 U.val V.val U.property V.property⟩, one_mul := λ V, subtype.eq (bot_mul0 V.val V.property), mul_one := λ U, subtype.eq (mul0_bot U.val U.property), mul_assoc := λ ⟨U,hU⟩ ⟨V,hV⟩ ⟨W,hW⟩, begin -- Proof of associativity apply subtype.eq, change mul0 (mul0 U V) W = mul0 U (mul0 V W), ext E,split, {intro h, rcases (mem_mul0 _ W E).mp h with ⟨AB,C,⟨hAB,hC,AB_angle_C,e_AB_C⟩⟩, rcases (mem_mul0 U V AB).mp hAB with ⟨A,B,hA,hB,A_angle_B,e_A_B⟩, have A_le_AB : A ≤ AB := e_A_B ▸ (finset.subset_union_left A B), have B_le_AB : B ≤ AB := e_A_B ▸ (finset.subset_union_right A B), have A_angle_C : A ∟ C := λ i k i_in_A k_in_C, AB_angle_C (A_le_AB i_in_A) k_in_C, have B_angle_C : B ∟ C := λ j k j_in_B k_in_C, AB_angle_C (B_le_AB j_in_B) k_in_C, let BC := B ⊔ C, have A_angle_BC : A ∟ BC := begin rintros i j i_in_A j_in_BC, rcases (finset.mem_union.mp j_in_BC) with j_in_B | j_in_C, {exact A_angle_B i_in_A j_in_B,}, {exact A_angle_C i_in_A j_in_C,} end, have hBC : BC ∈ mul0 V W := begin apply (mem_mul0 V W BC).mpr,use B, use C, exact ⟨hB,hC,B_angle_C,rfl⟩ end, have e_A_BC := calc A ⊔ BC = A ⊔ (B ⊔ C) : rfl ... = (A ⊔ B) ⊔ C : by rw[sup_assoc] ... = E : by rw[e_A_B,e_AB_C], apply (mem_mul0 U (mul0 V W) E).mpr, use A,use BC, exact ⟨hA,hBC,A_angle_BC,e_A_BC⟩, }, {intro h, rcases (mem_mul0 U _ E).mp h with ⟨A,BC,⟨hA,hBC,A_angle_BC,e_A_BC⟩⟩, rcases (mem_mul0 V W BC).mp hBC with ⟨B,C,hB,hC,B_angle_C,e_B_C⟩, have B_le_BC : B ≤ BC := e_B_C ▸ (finset.subset_union_left B C), have C_le_BC : C ≤ BC := e_B_C ▸ (finset.subset_union_right B C), have A_angle_B : A ∟ B := λ i j i_in_A j_in_B, A_angle_BC i_in_A (B_le_BC j_in_B), have A_angle_C : A ∟ C := λ i k i_in_A k_in_C, A_angle_BC i_in_A (C_le_BC k_in_C), let AB := A ⊔ B, have AB_angle_C : AB ∟ C := begin rintros i k i_in_AB k_in_C, rcases (finset.mem_union.mp i_in_AB) with i_in_A | i_in_B, {exact A_angle_C i_in_A k_in_C,}, {exact B_angle_C i_in_B k_in_C,} end, have hAB : AB ∈ mul0 U V := begin apply (mem_mul0 U V AB).mpr,use A, use B, exact ⟨hA,hB,A_angle_B,rfl⟩ end, have e_AB_C := calc AB ⊔ C = (A ⊔ B) ⊔ C : rfl ... = A ⊔ (B ⊔ C) : by rw[← sup_assoc] ... = E : by rw[e_B_C,e_A_BC], apply (mem_mul0 (mul0 U V) W E).mpr, use AB,use C, exact ⟨hAB,hC,AB_angle_C,e_AB_C⟩, } end } variable {n} /-- Membership rule for `U * V` -/ lemma mem_mul (U V : 𝕂 n) (C : ℙ n) : C ∈ U * V ↔ ∃ A B, A ∈ U ∧ B ∈ V ∧ (A ∟ B) ∧ (A ⊔ B = C) := mem_mul0 U.val V.val C /-- Multiplication is monotone in both variables. -/ lemma mul_le_mul (U₀ V₀ U₁ V₁ : 𝕂 n) (hU : U₀ ≤ U₁ ) (hV : V₀ ≤ V₁) : U₀ * V₀ ≤ U₁ * V₁ := begin change (mul0 U₁.val V₁.val) ⊆ (mul0 U₀.val V₀.val), intros C C_in_W₁, rcases (mem_mul0 _ _ C).mp C_in_W₁ with ⟨A,B,hA,hB,A_angle_B,e_A_B⟩, exact (mem_mul0 U₀.val V₀.val C).mpr ⟨A,B,hU hA,hV hB,A_angle_B,e_A_B⟩, end /-- Multiplication distributes over union (on both sides). Recall that we order 𝕂 n by reverse inclusion, so the union is the lattice inf operation, and is written as U ⊓ V. LaTeX: lem-mu -/ lemma mul_inf (U V W : 𝕂 n) : U * (V ⊓ W) = (U * V) ⊓ (U * W) := begin ext C, rw [upper.mem_inf, mem_mul U (V ⊓ W)], split, { rintro ⟨A,B,hAU,hBVW,h_angle,h_ABC⟩, rw [upper.mem_inf] at hBVW, rcases hBVW with hBV | hBW, { left , exact (mem_mul U V C).mpr ⟨A,B,hAU,hBV,h_angle,h_ABC⟩ }, { right, exact (mem_mul U W C).mpr ⟨A,B,hAU,hBW,h_angle,h_ABC⟩ } }, { rintro (hCUV | hCUW), { rcases (mem_mul U V C).mp hCUV with ⟨A,B,hAU,hBV,h_angle,h_ABC⟩, have hBVW : B ∈ V ⊓ W := (upper.mem_inf B).mpr (or.inl hBV), exact ⟨A,B,hAU,hBVW,h_angle,h_ABC⟩ }, { rcases (mem_mul U W C).mp hCUW with ⟨A,B,hAU,hBW,h_angle,h_ABC⟩, have hBVW : B ∈ V ⊓ W := (upper.mem_inf B).mpr (or.inr hBW), exact ⟨A,B,hAU,hBVW,h_angle,h_ABC⟩ } } end lemma inf_mul (U V W : 𝕂 n) : (U ⊓ V) * W = (U * W) ⊓ (V * W) := begin ext C, rw [upper.mem_inf, mem_mul (U ⊓ V) W], split, { rintro ⟨A,B,hAUV,hBW,h_angle,h_ABC⟩, rw [upper.mem_inf] at hAUV, rcases hAUV with hAU | hAV, { left , exact (mem_mul U W C).mpr ⟨A,B,hAU,hBW,h_angle,h_ABC⟩ }, { right, exact (mem_mul V W C).mpr ⟨A,B,hAV,hBW,h_angle,h_ABC⟩ } }, { rintro (hCUW | hCVW), { rcases (mem_mul U W C).mp hCUW with ⟨A,B,hAU,hBW,h_angle,h_ABC⟩, have hAUV : A ∈ U ⊓ V := (upper.mem_inf A).mpr (or.inl hAU), exact ⟨A,B,hAUV,hBW,h_angle,h_ABC⟩ }, { rcases (mem_mul V W C).mp hCVW with ⟨A,B,hAV,hBW,h_angle,h_ABC⟩, have hAUV : A ∈ U ⊓ V := (upper.mem_inf A).mpr (or.inr hAV), exact ⟨A,B,hAUV,hBW,h_angle,h_ABC⟩ } } end /-- LaTeX: rem-kp -/ def κ (U : 𝕂 n) : ℙ n := finset.univ.filter (λ i, {i} ∈ U) lemma κ_mul (U V : 𝕂 n) : κ (U * V) = (κ U) ⊓ (κ V) := begin ext i, let ii : (ℙ n) := {i}, have ii_angle : ii ∟ ii := λ j k hj hk, by { rw[finset.mem_singleton.mp hj, finset.mem_singleton.mp hk] }, have ii_union : ii ⊔ ii = ii := sup_idem, have : ii ∈ U * V ↔ ii ∈ U ∧ ii ∈ V := begin rw[𝕂.mem_mul], split, { rintro ⟨A,B,A_in_U,B_in_V,h_angle,h_union⟩, have hA : A ≤ ii := by { rw[← h_union], exact le_sup_left }, have hB : B ≤ ii := by { rw[← h_union], exact le_sup_right }, have hU : ii ∈ U := U.property A ii hA A_in_U, have hV : ii ∈ V := V.property B ii hB B_in_V, exact ⟨hU,hV⟩ }, { rintro ⟨hU,hV⟩, use ii, use ii, exact ⟨hU,hV,ii_angle,ii_union⟩ } end, rw [ℙ.mem_inf], repeat {rw [κ, finset.mem_filter]}, simp [this] end /-- LaTeX: defn-thread -/ def threads : list (ℙ n) → finset (list (𝕀 n)) | list.nil := {list.nil} | (list.cons A AA) := A.bUnion (λ a, ((threads AA).filter (λ B, ∀ b ∈ B, a ≤ b)).image (list.cons a)) def thread_sets (AA : list (ℙ n)) : 𝕂 n := ⟨ finset.univ.filter (λ T, ∃ u ∈ threads AA, (∀ a ∈ u, a ∈ T)), begin intros T₀ T₁ h_le h_mem, rcases finset.mem_filter.mp h_mem with ⟨_,⟨u,h_thread,u_in_T₀⟩⟩, apply finset.mem_filter.mpr, exact ⟨finset.mem_univ T₁,u, h_thread,λ a ha, h_le (u_in_T₀ a ha)⟩ end ⟩ lemma mem_thread_sets {AA : list (ℙ n)} {T : ℙ n} : T ∈ thread_sets AA ↔ ∃ u ∈ threads AA, (∀ a ∈ u, a ∈ T) := begin change T ∈ (finset.univ.filter _) ↔ _, rw [finset.mem_filter], simp [finset.mem_univ T] end /-- LaTeX: prop-thread -/ lemma v_mul (AA : list (ℙ n)) : (AA.map v).prod = thread_sets AA := begin induction AA with A AA ih, { change ⊥ = _, ext A, have : A ∈ (⊥ : 𝕂 n) := upper.mem_bot A, simp[this], apply finset.mem_filter.mpr ⟨finset.mem_univ A,_⟩, use list.nil, use finset.mem_singleton_self list.nil, intros a a_in_nil, exact false.elim (list.not_mem_nil a a_in_nil) }, { rw [list.map_cons, list.prod_cons], ext T, rw [ih, 𝕂.mem_mul (v A) _], split; intro h, { rcases h with ⟨R,S,hR,hS,h_angle,h_union⟩, rcases mem_thread_sets.mp hS with ⟨u,u_in_threads,u_in_S⟩, rcases mem_v.mp hR with ⟨i,i_in_R,i_in_A⟩, apply (@mem_thread_sets n (A :: AA) T).mpr, use (i :: u), have : (list.cons i u) ∈ threads (A :: AA) := begin rw [threads, finset.mem_bUnion], use i, use i_in_A, rw [finset.mem_image], use u, have : u ∈ finset.filter (λ (v : list (𝕀 n)), ∀ (b : 𝕀 n), b ∈ v → i ≤ b) (threads AA) := begin rw [finset.mem_filter], split, {exact u_in_threads}, intros a a_in_u, exact h_angle i_in_R (u_in_S a a_in_u), end, use this, end, use this, rintro a (a_eq_i | a_in_u); rw [← h_union], { rw[a_eq_i], exact finset.mem_union_left S i_in_R }, { exact finset.mem_union_right R (u_in_S a a_in_u), } }, { rcases (@mem_thread_sets n (A :: AA) T).mp h with ⟨w,w_in_threads,w_in_T⟩, rw [threads, finset.mem_bUnion] at w_in_threads, rcases w_in_threads with ⟨a,a_in_A,w_in_image⟩, rcases finset.mem_image.mp w_in_image with ⟨u,⟨u_in_filter,au_eq_w⟩⟩, rcases finset.mem_filter.mp u_in_filter with ⟨u_in_threads, u_ge_a⟩, use T.filter_lt a.val.succ, use T.filter_ge a.val, have a_in_w : a ∈ w := by { rw[← au_eq_w], exact list.mem_cons_self a u }, have a_in_T : a ∈ T := w_in_T a a_in_w, have u_in_T1 : ∀ (i : 𝕀 n) (i_in_u : i ∈ u), i ∈ (T.filter_ge a.val) := λ i i_in_u, begin apply ℙ.mem_filter_ge.mpr, have : i ∈ (list.cons a u) := list.mem_cons_of_mem a i_in_u, rw [au_eq_w] at this, exact ⟨w_in_T i this, u_ge_a i i_in_u⟩, end, split, { exact mem_v.mpr ⟨a,⟨ℙ.mem_filter_lt.mpr ⟨a_in_T,a.val.lt_succ_self⟩,a_in_A⟩⟩ }, split, { rw [mem_thread_sets], use u, use u_in_threads, exact u_in_T1 }, split, { exact ℙ.filter_angle (le_refl _) T }, { exact ℙ.filter_sup (le_of_lt a.val.lt_succ_self) T } } } end end 𝕂 def is_universal {α : Type*} [fintype α] [decidable_eq α] (l : list α) := l.nodup ∧ l.to_finset = finset.univ instance {α : Type*} [fintype α] [decidable_eq α] (l : list α) : decidable (is_universal l) := by { dsimp[is_universal], apply_instance } /- LaTeX: eg-fracture-obj -/ namespace example_two def i₀ : 𝕀 2 := ⟨0,dec_trivial⟩ def i₁ : 𝕀 2 := ⟨1,dec_trivial⟩ lemma 𝕀_univ : is_universal [i₀, i₁] := dec_trivial def p : ℙ 2 := ⊥ def p₀ : ℙ 2 := [i₀].to_finset def p₁ : ℙ 2 := [i₁].to_finset def p₀₁ : ℙ 2 := ⊤ lemma ℙ_univ : is_universal [p, p₀, p₁, p₀₁] := dec_trivial def u := @𝕂.u 2 def L : list (𝕂 2) := [u p, u p₀, u p₁, u p₀₁, u p₀ ⊓ u p₁, ⊤] #eval (L.nodup : bool) #eval (is_universal L : bool) lemma 𝕂_univ : is_universal L := dec_trivial end example_two /- LaTeX: eg-fracture-obj -/ namespace example_three def i₀ : 𝕀 3 := ⟨0,dec_trivial⟩ def i₁ : 𝕀 3 := ⟨1,dec_trivial⟩ def i₂ : 𝕀 3 := ⟨2,dec_trivial⟩ lemma 𝕀_univ : is_universal [i₀, i₁, i₂] := dec_trivial def p : ℙ 3 := ⊥ def p₀ : ℙ 3 := [i₀].to_finset def p₁ : ℙ 3 := [i₁].to_finset def p₂ : ℙ 3 := [i₂].to_finset def p₀₁ : ℙ 3 := [i₀,i₁].to_finset def p₀₂ : ℙ 3 := [i₀,i₂].to_finset def p₁₂ : ℙ 3 := [i₁,i₂].to_finset def p₀₁₂ : ℙ 3 := ⊤ lemma ℙ_univ : is_universal [p, p₀, p₁, p₂, p₀₁, p₀₂, p₁₂, p₀₁₂] := dec_trivial def u := @𝕂.u 3 def v := @𝕂.v 3 def u₀ : 𝕂 3 := u p₀ def u₁ : 𝕂 3 := u p₁ def u₂ : 𝕂 3 := u p₂ def u₀₁ : 𝕂 3 := u p₀₁ def u₀₂ : 𝕂 3 := u p₀₂ def u₁₂ : 𝕂 3 := u p₁₂ def u₀₁₂ : 𝕂 3 := u p₀₁₂ def v₀₁ : 𝕂 3 := u p₀ ⊓ u p₁ def v₀₂ : 𝕂 3 := u p₀ ⊓ u p₂ def v₁₂ : 𝕂 3 := u p₁ ⊓ u p₂ def x₀ : 𝕂 3 := u p₀ ⊓ u p₁₂ def x₁ : 𝕂 3 := u p₁ ⊓ u p₀₂ def x₂ : 𝕂 3 := u p₂ ⊓ u p₀₁ def w₀ : 𝕂 3 := u₀₁ ⊓ u₀₂ def w₁ : 𝕂 3 := u₀₁ ⊓ u₁₂ def w₂ : 𝕂 3 := u₀₂ ⊓ u₁₂ def y := u₀₁ ⊓ u₀₂ ⊓ u₁₂ def L : list (𝕂 3) := [ ⊥, v p₀₁₂, v₀₁, v₀₂, v₁₂, x₀, x₁, x₂, u₀, u₁, u₂, w₀, w₁, w₂, u₀₁, u₀₂, u₁₂, u₀₁₂, y, ⊤ ] #eval (L.nodup : bool) #eval (is_universal L : bool) -- lemma 𝕂_univ : is_universal L := dec_trivial lemma eqs : x₀ = v₀₁ * v₀₂ ∧ x₁ = v₀₁ * v₁₂ ∧ x₂ = v₀₂ * v₁₂ ∧ w₀ = u₀ * v₁₂ ∧ w₂ = v₀₁ * u₂ ∧ y = v₀₁ * v₀₂ * v₁₂ := begin repeat { split }; ext A; revert A; exact dec_trivial, end end example_three variable (n) /-- LaTeX : defn-doubly-localising-alt -/ def 𝕄 := { AB : (ℙ n) × (ℙ n) // AB.1 ∟ AB.2 } namespace 𝕄 instance : partial_order (𝕄 n) := { le := λ ⟨⟨A,B⟩,hAB⟩ ⟨⟨C,D⟩,hCD⟩, (A ≤ C) ∧ (B ≤ D), le_refl := λ ⟨⟨A,B⟩,hAB⟩, by { split; apply @le_refl (ℙ n) _, }, le_antisymm := λ ⟨⟨A,B⟩,hAB⟩ ⟨⟨C,D⟩,hCD⟩ ⟨hAC,hBD⟩ ⟨hCA,hDB⟩, begin congr,exact le_antisymm hAC hCA,exact le_antisymm hBD hDB end, le_trans := λ ⟨⟨A,B⟩,hAB⟩ ⟨⟨C,D⟩,hCD⟩ ⟨⟨E,F⟩,hEF⟩ ⟨hAC,hBD⟩ ⟨hCE,hDF⟩, ⟨le_trans hAC hCE,le_trans hBD hDF⟩, } instance : has_bot (𝕄 n) := ⟨⟨⟨⊥,⊥⟩,ℙ.bot_angle ⊥⟩⟩ /-- The only element of `𝕄 0` is `⊥`. -/ lemma mem_zero (AB : 𝕄 0) : AB = ⊥ := begin apply subtype.eq, apply prod.ext, { rw [ℙ.mem_zero AB.val.1], refl }, { rw [ℙ.mem_zero AB.val.2], refl } end instance : fintype (𝕄 n) := by { unfold 𝕄, apply_instance } end 𝕄 instance 𝕄.decidable_rel : decidable_rel (λ (AB CD : 𝕄 n), AB ≤ CD) := λ ⟨⟨A,B⟩,hAB⟩ ⟨⟨C,D⟩,hCD⟩, by { change decidable ((A ≤ C) ∧ (B ≤ D)), apply_instance, } variable {n} /-- LaTeX : defn-doubly-localising-alt -/ def σ : poset.hom (𝕄 n) (ℙ n) := ⟨λ ⟨⟨A,B⟩,hAB⟩, A ⊔ B, λ ⟨⟨A,B⟩,hAB⟩ ⟨⟨C,D⟩,hCD⟩ ⟨hAC,hBD⟩, sup_le_sup hAC hBD⟩ def ζ : poset.hom (ℙ n) (𝕄 n) := ⟨λ B, ⟨⟨⊥,B⟩,B.bot_angle⟩, λ B D hBD, ⟨le_refl ⊥,hBD⟩⟩ def ξ : poset.hom (ℙ n) (𝕄 n) := ⟨λ A, ⟨⟨A,⊥⟩,A.angle_bot⟩, λ A C hAC, ⟨hAC,le_refl ⊥⟩⟩ lemma half_step (i : ℕ) : (i / 2) ≤ ((i + 1) / 2) ∧ ((i + 1) / 2) ≤ (i / 2) + 1 := begin split, exact nat.div_le_div_right (le_of_lt i.lt_succ_self), exact calc (i + 1) / 2 ≤ (i + 2) / 2 : nat.div_le_div_right (le_of_lt i.succ.lt_succ_self) ... = (i / 2) + 1 : nat.add_div_right i two_pos' end lemma half_misc (i : ℕ) : ((2 * i) / 2) = i ∧ ((2 * i + 1) / 2) = i ∧ ((2 * i + 2) / 2) = i + 1 ∧ ((2 * i + 3) / 2) = i + 1 := begin have h₁ : 1 / 2 = 0 := rfl, have h₂ : 2 / 2 = 1 := rfl, have h₃ : 3 / 2 = 1 := rfl, rw [mul_comm, add_comm _ 1, add_comm _ 2, add_comm _ 3, nat.mul_div_cancel i two_pos', nat.add_mul_div_right 1 i two_pos', nat.add_mul_div_right 2 i two_pos', nat.add_mul_div_right 3 i two_pos', h₁, h₂, h₃, zero_add, add_comm 1], repeat {split}; refl end /-- LaTeX: defn-al-bt -/ def α (i : ℕ) : hom (𝕄 n) (𝕄 n) := ⟨λ ABh, ⟨⟨ABh.val.1.filter_lt ((i + 1)/2), ABh.val.1.filter_ge (i / 2) ⊔ ABh.val.2⟩, begin -- Proof that we have an element of 𝕄 n rcases ABh with ⟨⟨A,B⟩,h_angle⟩, let u := (i + 1) / 2, let v := i / 2, rcases half_step i with ⟨hvu,huv⟩, change (A.filter_lt u) ∟ ((A.filter_ge v) ⊔ B), rw[ℙ.angle_sup], split, { exact ℙ.filter_angle huv A }, { exact ℙ.angle_mono (A.filter_lt_is_le u) (le_refl _) h_angle } end⟩, begin -- Proof of monotonicity rintro ⟨⟨A₀,B₀⟩,h_angle₀⟩ ⟨⟨A₁,B₁⟩,h_angle₁⟩ ⟨hA,hB⟩, let u := (i + 1) / 2, let v := i / 2, change (A₀.filter_lt u ≤ A₁.filter_lt u) ∧ (A₀.filter_ge v ⊔ B₀) ≤ (A₁.filter_ge v ⊔ B₁), split; intros x hx, { rw[ℙ.mem_filter_lt] at hx ⊢, exact ⟨hA hx.1,hx.2⟩ }, { rw [ℙ.mem_sup, ℙ.mem_filter_ge] at hx ⊢, rcases hx with hxA | hxB, { exact or.inl ⟨hA hxA.1,hxA.2⟩ }, { exact or.inr (hB hxB) } } end⟩ def β (i : ℕ) : hom (𝕄 n) (𝕄 n) := ⟨λ ABh, ⟨⟨ABh.val.1 ⊔ ABh.val.2.filter_lt ((i + 1)/2), ABh.val.2.filter_ge (i / 2)⟩, begin -- Proof that we have an element of 𝕄 n rcases ABh with ⟨⟨A,B⟩,h_angle⟩, let u := (i + 1) / 2, let v := i / 2, rcases half_step i with ⟨hvu,huv⟩, change (A ⊔ B.filter_lt u) ∟ (B.filter_ge v), rw[ℙ.sup_angle], split, { exact ℙ.angle_mono (le_refl A) (B.filter_ge_is_le v) h_angle }, { exact ℙ.filter_angle huv B } end⟩, begin -- Proof of monotonicity rintro ⟨⟨A₀,B₀⟩,h_angle₀⟩ ⟨⟨A₁,B₁⟩,h_angle₁⟩ ⟨hA,hB⟩, let u := (i + 1) / 2, let v := i / 2, change (A₀ ⊔ B₀.filter_lt u ≤ A₁ ⊔ B₁.filter_lt u) ∧ (B₀.filter_ge v) ≤ (B₁.filter_ge v), split; intros x hx, { rw [ℙ.mem_sup, ℙ.mem_filter_lt] at hx ⊢, rcases hx with hxA | hxB, { exact or.inl (hA hxA) }, { exact or.inr ⟨hB hxB.1,hxB.2⟩ } }, { rw[ℙ.mem_filter_ge] at hx ⊢, exact ⟨hB hx.1,hx.2⟩ } end⟩ /-- The relation `σ αᵢ = σ` -/ lemma σα (i : ℕ) : poset.comp (@σ n) (@α n i) = @σ n := begin apply poset.hom_ext, rintro ⟨⟨A,B⟩,h_angle⟩, let u := (i + 1) / 2, let v := i / 2, rcases half_step i with ⟨hvu,huv⟩, rw[poset.comp], change (A.filter_lt u) ⊔ ((A.filter_ge v) ⊔ B) = A ⊔ B, rw [← sup_assoc, ℙ.filter_sup hvu A] end /-- The relation `σ βᵢ = σ` -/ lemma σβ (i : ℕ) : poset.comp (@σ n) (@β n i) = @σ n := begin apply poset.hom_ext, rintro ⟨⟨A,B⟩,h_angle⟩, let u := (i + 1) / 2, let v := i / 2, rcases half_step i with ⟨hvu,huv⟩, rw[poset.comp], change (A ⊔ B.filter_lt u) ⊔ (B.filter_ge v) = A ⊔ B, rw [sup_assoc, ℙ.filter_sup hvu B] end /-- The relation `α₀ ⟨A,B⟩ = ⟨⊥, A⊔ B⟩` -/ lemma α_zero : (@α n 0) = poset.comp (@ζ n) (@σ n) := begin apply poset.hom_ext, rintro ⟨⟨A,B⟩,h_angle⟩, rw[poset.comp], apply subtype.eq, change prod.mk (A.filter_lt 0) ((A.filter_ge 0) ⊔ B) = prod.mk ⊥ (A ⊔ B), rw[ℙ.filter_lt_zero, ℙ.filter_ge_zero] end /-- The relation `αᵢ = 1` for `i ≥ 2n` -/ lemma α_last {i : ℕ} (hi : i ≥ 2 * n) : (@α n i) = poset.id _ := begin apply poset.hom_ext, rintro ⟨⟨A,B⟩,h_angle⟩, rw[poset.id], apply subtype.eq, let u := (i + 1) / 2, let v := i / 2, have hv : v ≥ n := calc n = (2 * n) / 2 : by rw [mul_comm, nat.mul_div_cancel n two_pos'] ... ≤ v : nat.div_le_div_right hi, have hu : u ≥ n := le_trans hv (nat.div_le_div_right (le_of_lt i.lt_succ_self)), change prod.mk (A.filter_lt u) ((A.filter_ge v) ⊔ B) = prod.mk A B, rw [ℙ.filter_lt_last hu, ℙ.filter_ge_last hv], congr, exact bot_sup_eq end /-- The relation `β₀ = 1` -/ lemma β_zero : (@β n 0) = poset.id _ := begin apply poset.hom_ext, rintro ⟨⟨A,B⟩,h_angle⟩, rw[poset.id], apply subtype.eq, change prod.mk (A ⊔ B.filter_lt 0) (B.filter_ge 0) = prod.mk A B, rw[ℙ.filter_lt_zero, ℙ.filter_ge_zero], congr, exact sup_bot_eq end /-- The relation `βᵢ ⟨A,B⟩ = ⟨A⊔ B, ⊥⟩` for `i ≥ 2n` -/ lemma β_last {i : ℕ} (hi : i ≥ 2 * n) : (@β n i) = poset.comp (@ξ n) (@σ n) := begin apply poset.hom_ext, rintro ⟨⟨A,B⟩,h_angle⟩, rw [poset.comp], apply subtype.eq, let u := (i + 1) / 2, let v := i / 2, have hv : v ≥ n := calc n = (2 * n) / 2 : by rw [mul_comm, nat.mul_div_cancel n two_pos'] ... ≤ v : nat.div_le_div_right hi, have hu : u ≥ n := le_trans hv (nat.div_le_div_right (le_of_lt i.lt_succ_self)), change prod.mk (A ⊔ B.filter_lt u) (B.filter_ge v) = prod.mk (A ⊔ B) ⊥, rw [ℙ.filter_lt_last hu, ℙ.filter_ge_last hv] end /-- The inequality `α₂ᵢ ≤ α₂ᵢ₊₁` -/ lemma α_even_step (i : ℕ) : (@α n (2 * i)) ≤ (@α n (2 * i + 1)) := begin rcases half_misc i with ⟨h₀,h₁,h₂,h₃⟩, rintro ⟨⟨A,B⟩,h_angle⟩, change ((A.filter_lt ((2*i+1)/2) ≤ (A.filter_lt ((2*i+2)/2))) ∧ (A.filter_ge ((2*i)/2) ⊔ B ≤ (A.filter_ge ((2*i+1)/2)) ⊔ B)), rw [h₀, h₁, h₂], split, { intros x hx, rw[ℙ.mem_filter_lt] at hx ⊢, exact ⟨hx.1, lt_trans hx.2 i.lt_succ_self⟩ }, { exact le_refl _ } end /-- The inequality `α₂ᵢ₊₂ ≤ α₂ᵢ₊₁` -/ lemma α_odd_step (i : ℕ) : (@α n (2 * i + 2)) ≤ (@α n (2 * i + 1)) := begin rcases half_misc i with ⟨h₀,h₁,h₂,h₃⟩, rintro ⟨⟨A,B⟩,h_angle⟩, change ((A.filter_lt ((2*i+3)/2) ≤ (A.filter_lt ((2*i+2)/2))) ∧ (A.filter_ge ((2*i+2)/2) ⊔ B ≤ (A.filter_ge ((2*i+1)/2)) ⊔ B)), rw [h₁, h₂, h₃], split, { exact le_refl _ }, { apply sup_le_sup _ (le_refl _), intros x hx, rw[ℙ.mem_filter_ge] at hx ⊢, exact ⟨hx.1,le_trans (le_of_lt i.lt_succ_self) hx.2⟩ } end /-- The inequality `β₂ᵢ ≤ β₂ᵢ₊₁` -/ lemma β_even_step (i : ℕ) : (@β n (2 * i)) ≤ (@β n (2 * i + 1)) := begin rcases half_misc i with ⟨h₀,h₁,h₂,h₃⟩, rintro ⟨⟨A,B⟩,h_angle⟩, change ((A ⊔ B.filter_lt ((2*i+1)/2) ≤ (A ⊔ B.filter_lt ((2*i+2)/2))) ∧ (B.filter_ge ((2*i)/2) ≤ (B.filter_ge ((2*i+1)/2)))), rw [h₀, h₁, h₂], split, { apply sup_le_sup (le_refl _) _, intros x hx, rw[ℙ.mem_filter_lt] at hx ⊢, exact ⟨hx.1, lt_trans hx.2 i.lt_succ_self⟩ }, { exact le_refl _ } end /-- The inequality `β₂ᵢ₊₁ ≤ β₂ᵢ₊₁` -/ lemma β_odd_step (i : ℕ) : (@β n (2 * i + 2)) ≤ (@β n (2 * i + 1)) := begin rcases half_misc i with ⟨h₀,h₁,h₂,h₃⟩, rintro ⟨⟨A,B⟩,h_angle⟩, change ((A ⊔ B.filter_lt ((2*i+3)/2) ≤ (A ⊔ B.filter_lt ((2*i+2)/2))) ∧ (B.filter_ge ((2*i+2)/2) ≤ (B.filter_ge ((2*i+1)/2)))), rw [h₁, h₂, h₃], split, { exact le_refl _ }, { intros x hx, rw[ℙ.mem_filter_ge] at hx ⊢, exact ⟨hx.1, le_trans (le_of_lt i.lt_succ_self) hx.2⟩ } end /-- All `αᵢ` are in the identity component. -/ lemma α_component : ∀ i, poset.component (@α n i) = poset.idₕ _ := begin let c := λ i, poset.component (@α n i), change ∀ i, c i = poset.idₕ _, have h_all : ∀ i, c i = c 0 := poset.zigzag α α_even_step α_odd_step, have : c (2 * n) = poset.idₕ _ := congr_arg component (α_last (le_refl _)), intro i, exact ((h_all i).trans (h_all (2 * n)).symm).trans this end /-- All `βᵢ` are in the identity component. -/ lemma β_component : ∀ i, poset.component (@β n i) = poset.idₕ _ := begin let c := λ i, poset.component (@β n i), change ∀ i, c i = poset.idₕ _, have h_all : ∀ i, c i = c 0 := poset.zigzag β β_even_step β_odd_step, intro i, have : c 0 = poset.idₕ _ := congr_arg component β_zero, rw [← this], exact h_all i end /-- `ζσ = 1` up to strong homotopy -/ lemma ζσ_component : poset.component (poset.comp (@ζ n) (@σ n)) = poset.idₕ _ := (congr_arg poset.component (@α_zero n)).symm.trans (@α_component n 0) /-- `𝕃 n` is the poset of upper sets in `𝕄 n`. This is not mentioned explicitly in the LaTeX document, but is there in the background. -/ def 𝕃 (n : ℕ) := poset.upper (𝕄 n) namespace 𝕃 instance : distrib_lattice (𝕃 n) := @upper.dl (𝕄 n) _ _ _ _ instance : bounded_order (𝕃 n) := @upper.bo (𝕄 n) _ _ _ _ instance : partial_order (𝕄 n) := by apply_instance instance 𝕄_mem_𝕃 : has_mem (𝕄 n) (𝕃 n) := by { unfold 𝕃, apply_instance } end 𝕃 /-- LaTeX : defn-ostar -/ def omul0 (U V : finset (ℙ n)) : finset (𝕄 n) := (U.bUnion (λ A, V.image (λ B, prod.mk A B))).subtype (λ AB, AB.1 ∟ AB.2) lemma mem_omul0 (U V : finset (ℙ n)) (AB : 𝕄 n) : AB ∈ omul0 U V ↔ AB.val.1 ∈ U ∧ AB.val.2 ∈ V := begin rw[omul0,finset.mem_subtype], split, {rcases AB with ⟨⟨A,B⟩,h⟩, change A ∟ B at h, intro h0, rcases (finset.mem_bUnion.mp h0) with ⟨C,⟨hCU,h1⟩⟩, rcases (finset.mem_image.mp h1) with ⟨D,⟨hDV,h2⟩⟩, cases h2, exact ⟨hCU,hDV⟩, }, {rcases AB with ⟨⟨A,B⟩,hAB⟩, change A ∟ B at hAB, rintros ⟨hAU,hBV⟩, change A ∈ U at hAU, change B ∈ V at hBV, apply finset.mem_bUnion.mpr,use A,use hAU, apply finset.mem_image.mpr,use B,use hBV, simp, } end lemma is_upper_omul0 {U V : finset (ℙ n)} (hU : is_upper U) (hV : is_upper V) : is_upper (omul0 U V) := begin rintros ⟨⟨A₀,B₀⟩,hAB₀⟩ ⟨⟨A₁,B₁⟩,hAB₁⟩ ⟨h_le_A,h_le_B⟩ AB₀_in_omul, rcases (mem_omul0 U V _).mp AB₀_in_omul with ⟨A₀_in_U,B₀_in_V⟩, apply (mem_omul0 U V _).mpr, simp only [], exact ⟨hU A₀ A₁ h_le_A A₀_in_U,hV B₀ B₁ h_le_B B₀_in_V⟩, end def omul : (𝕂 n) → (𝕂 n) → (𝕃 n) := λ U V, ⟨omul0 U.val V.val, is_upper_omul0 U.property V.property⟩ lemma mem_omul (U V : (𝕂 n)) (AB : 𝕄 n) : AB ∈ omul U V ↔ AB.val.1 ∈ U ∧ AB.val.2 ∈ V := mem_omul0 U.val V.val AB lemma omul_mono₂ : ∀ {U₀ U₁ V₀ V₁ : 𝕂 n} (hU : U₀ ≤ U₁) (hV : V₀ ≤ V₁), omul U₀ V₀ ≤ omul U₁ V₁ | ⟨U₀,hU₀⟩ ⟨U₁,hU₁⟩ ⟨V₀,hV₀⟩ ⟨V₁,hV₁⟩ hU hV ⟨A,B⟩ h := begin rcases (mem_omul0 U₁ V₁ ⟨A,B⟩).mp h with ⟨hAU,hBV⟩, apply (mem_omul0 U₀ V₀ ⟨A,B⟩).mpr, exact ⟨hU hAU,hV hBV⟩, end def σ0 (W : 𝕃 n) : 𝕂 n := ⟨W.val.image (@σ n), begin intros C C' h_le C_in_sg_W, rcases finset.mem_image.mp C_in_sg_W with ⟨⟨⟨A,B⟩,h_angle⟩,h_mem,h_eq⟩, let AB : 𝕄 n := ⟨⟨A,B⟩,h_angle⟩, rcases ℙ.angle_iff.mp h_angle with ⟨⟨⟨⟩⟩ | h⟩, { rw[C.mem_zero] at *, rw[C'.mem_zero], exact C_in_sg_W }, { rcases h with ⟨k,hA,hB⟩, change (A ∪ B : finset _) = C at h_eq, rw [← h_eq] at h_le, let A' := C'.filter_lt k.val.succ, let B' := C'.filter_ge k.val, have h_angle' : A' ∟ B' := ℙ.filter_angle k.val.lt_succ_self C', let AB' : 𝕄 n := ⟨⟨A',B'⟩,h_angle'⟩, have h_le' : AB ≤ AB' := begin split, { intros a a_in_A, exact finset.mem_filter.mpr ⟨h_le (finset.mem_union_left B a_in_A), lt_of_le_of_lt (hA a_in_A) k.val.lt_succ_self⟩ }, { intros b b_in_B, exact finset.mem_filter.mpr ⟨h_le (finset.mem_union_right A b_in_B),hB b_in_B⟩ } end, have h_mem' : AB' ∈ W.val := W.property AB AB' h_le' h_mem, have h_eq' : (@σ n) AB' = C' := begin change A' ∪ B' = C', ext c, rw [finset.mem_union, ℙ.mem_filter_lt, ℙ.mem_filter_ge, ← and_or_distrib_left, nat.lt_succ_iff], simp [le_total] end, exact finset.mem_image.mpr ⟨AB',h_mem',h_eq'⟩ } end⟩ def σ' : poset.hom (𝕃 n) (𝕂 n) := ⟨@σ0 n, begin rintro W₀ W₁ h_le C C_in_sg_W, rcases finset.mem_image.mp C_in_sg_W with ⟨AB,h_mem,h_eq⟩, exact finset.mem_image.mpr ⟨AB,h_le h_mem,h_eq⟩ end⟩ lemma mem_σ' (W : 𝕃 n) (C : ℙ n) : C ∈ @σ' n W ↔ ∃ AB, AB ∈ W ∧ @σ n AB = C := begin change (C ∈ W.val.image (@σ n) ↔ _), rw[finset.mem_image], apply exists_congr, intro AB, have : AB ∈ W.val ↔ AB ∈ W := by { refl }, rw[this], simp, end lemma factor_σ (U V : 𝕂 n) : U * V = @σ' n (omul U V) := begin ext C, rw [𝕂.mem_mul U V C, mem_σ' (omul U V)], split; intro h, { rcases h with ⟨A,B,A_in_U,B_in_V,h_angle,h_eq⟩, use ⟨⟨A,B⟩,h_angle⟩, exact ⟨(mem_omul U V ⟨⟨A,B⟩,h_angle⟩).mpr ⟨A_in_U,B_in_V⟩,h_eq⟩ }, { rcases h with ⟨⟨⟨A,B⟩,h_angle⟩,h_mem,h_eq⟩, use A, use B, replace h_mem := (mem_omul U V ⟨⟨A,B⟩,h_angle⟩).mp h_mem, change A ∈ U ∧ B ∈ V at h_mem, exact ⟨h_mem.left,h_mem.right,h_angle,h_eq⟩ } end /-- LaTeX: lem-mu-u -/ lemma mul_u (A B : ℙ n) : (@𝕂.u n A) * (@𝕂.u n B) = ite (A ∟ B) (@𝕂.u n (A ⊔ B)) ⊤ := begin ext C, rw [𝕂.mem_mul (@𝕂.u n A) (@𝕂.u n B) C], split; intro h, { rcases h with ⟨A',B',hA,hB,h_angle,h_union⟩, rw [𝕂.mem_u] at hA hB, have : A ∟ B := λ a b ha hb, h_angle (hA ha) (hB hb), rw [if_pos this, @𝕂.mem_u n (A ⊔ B) C, ← h_union], exact sup_le_sup hA hB }, { by_cases h_angle : A ∟ B, { rw [if_pos h_angle, @𝕂.mem_u n] at h, rcases (ℙ.angle_iff.mp h_angle) with ⟨⟨⟩⟩ | ⟨k,⟨hA,hB⟩⟩, { use ⊥, use ⊥, rw[ℙ.mem_zero A, ℙ.mem_zero B, ℙ.mem_zero C, 𝕂.mem_u], simp only [ℙ.bot_angle, le_refl, sup_bot_eq, true_and] }, { use C.filter_lt k.val.succ, use C.filter_ge k.val, rw [𝕂.mem_u, 𝕂.mem_u], have hA' : A ≤ C.filter_lt k.val.succ := λ a ha, ℙ.mem_filter_lt.mpr ⟨h (finset.mem_union_left B ha),nat.lt_succ_iff.mpr (hA ha)⟩, have hB' : B ≤ C.filter_ge k.val := λ b hb, ℙ.mem_filter_ge.mpr ⟨h (finset.mem_union_right A hb),hB hb⟩, exact ⟨hA', hB', ℙ.filter_angle k.val.lt_succ_self C, ℙ.filter_sup (le_of_lt k.val.lt_succ_self) C⟩ } }, { rw [if_neg h_angle] at h, exact (upper.not_mem_top C h).elim } } end /-- `σ_slice U V` is the map from `U # V` to `U * V` whose (co)finality is proved in prop-sg-final and prop-sg-cofinal. -/ def σ_slice (U V : 𝕂 n) : hom (omul U V).els (U * V).els := ⟨ λ ABh, ⟨@σ n ABh.val, begin rw [factor_σ U V], exact finset.mem_image_of_mem _ ABh.property, end ⟩, λ ABh₀ ABh₁ h, σ.property h ⟩ namespace σ_slice variables (U V : 𝕂 n) (C : (U * V).els) /-- This is the map sending `A` to `C_{≤ max A}`, which is used (but not named) in prop-sg-cofinal. -/ def η : hom (ℙ n) (ℙ n) := ⟨ λ A, C.val.filter (λ c, ∃ a, a ∈ A ∧ c ≤ a), λ A₀ A₁ h c hc, begin rw [finset.mem_filter] at *, rcases hc with ⟨hcC,⟨a,⟨haA,hca⟩⟩⟩, exact ⟨hcC,⟨a,⟨h haA,hca⟩⟩⟩ end⟩ /-- This is the map sending `A` to `C_{≥ min A}`, which is used (but not named) in prop-sg-cofinal. -/ def θ : hom (ℙ n) (ℙ n) := ⟨ λ B, C.val.filter (λ c, ∃ b, b ∈ B ∧ b ≤ c), λ B₀ B₁ h c hc, begin rw [finset.mem_filter] at *, rcases hc with ⟨hcC,⟨b,⟨hbB,hbc⟩⟩⟩, exact ⟨hcC,⟨b,⟨h hbB,hbc⟩⟩⟩ end⟩ lemma η_angle_θ (A B : ℙ n) : A ∟ B → (η U V C A) ∟ (θ U V C B) := λ h_angle x y hx hy, begin rcases (finset.mem_filter.mp hx).right with ⟨a,⟨haA,hxa⟩⟩, rcases (finset.mem_filter.mp hy).right with ⟨b,⟨hbB,hby⟩⟩, exact le_trans (le_trans hxa (h_angle haA hbB)) hby, end def φ₀ : hom (𝕄 n) (𝕄 n) := ⟨λ AB, ⟨⟨η U V C AB.val.1,θ U V C AB.val.2⟩, η_angle_θ U V C AB.val.1 AB.val.2 AB.property⟩, λ ⟨⟨A₀,B₀⟩,h_angle₀⟩ ⟨⟨A₁,B₁⟩,h_angle₁⟩ h, begin change A₀ ≤ A₁ ∧ B₀ ≤ B₁ at h, change ((η U V C A₀) ≤ (η U V C A₁)) ∧ ((θ U V C B₀) ≤ (θ U V C B₁)), exact ⟨(η U V C).property h.left,(θ U V C).property h.right⟩ end⟩ /-- This is the main map used in prop-sg-cofinal -/ def φ : hom (comma (σ_slice U V) C) (comma (σ_slice U V) C) := ⟨ λ X, ⟨⟨φ₀ U V C X.val.val, begin rcases X with ⟨⟨⟨⟨A,B⟩,h_angle⟩,h_omul⟩,hABC⟩, rcases (mem_omul U V _).mp h_omul with ⟨hAU,hBV⟩, have hABC' : A ⊔ B ≤ C.val := hABC, rcases sup_le_iff.mp hABC' with ⟨hAC,hBC⟩, apply (mem_omul U V _).mpr, let A₁ := η U V C A, let B₁ := θ U V C B, change A₁ ∈ U ∧ B₁ ∈ V, split, { have : A ≤ A₁ := λ a ha, finset.mem_filter.mpr ⟨hAC ha,⟨a,⟨ha,le_refl a⟩⟩⟩, exact U.property A A₁ this hAU }, { have : B ≤ B₁ := λ b hb, finset.mem_filter.mpr ⟨hBC hb,⟨b,⟨hb,le_refl b⟩⟩⟩, exact V.property B B₁ this hBV } end⟩, begin rcases X with ⟨⟨⟨⟨A,B⟩,h_angle⟩,h_omul⟩,hABC⟩, change (η U V C A) ⊔ (θ U V C B) ≤ C.val, intros x hx, rcases finset.mem_union.mp hx with hx' | hx'; exact (finset.mem_filter.mp hx').left, end⟩, begin rintro ⟨⟨AB₀,h_omul₀⟩,hABC₀⟩ ⟨⟨AB₁,h_omul₁⟩,hABC₁⟩ h, exact (φ₀ U V C).property (h : AB₀ ≤ AB₁), end⟩ lemma id_le_φ : (id (comma (σ_slice U V) C)) ≤ (φ U V C) := begin intro X, rcases X with ⟨⟨⟨⟨A,B⟩,h_angle⟩,h_omul⟩,hABC⟩, change A ∟ B at h_angle, rcases (mem_omul U V _).mp h_omul with ⟨hAU,hBV⟩, change A ∈ U at hAU, change B ∈ V at hBV, change A ⊔ B ≤ C.val at hABC, rcases sup_le_iff.mp hABC with ⟨hAC,hBC⟩, change (A ≤ η U V C A) ∧ (B ≤ θ U V C B), split, { intros a ha, exact finset.mem_filter.mpr ⟨hAC ha, ⟨a,⟨ha,le_refl a⟩⟩⟩, }, { intros b hb, exact finset.mem_filter.mpr ⟨hBC hb, ⟨b,⟨hb,le_refl b⟩⟩⟩, }, end /-- The proof of prop-sg-cofinal uses a pair of natural numbers `i` and `j` with certain properties. The definition below encapsulates these properties. -/ def ij_spec (i j : ℕ) := i ≤ n ∧ j ≤ n ∧ C.val.filter_lt i ∈ U ∧ C.val.filter_ge j ∈ V ∧ (∀ k, k ≤ n → C.val.filter_lt k ∈ U → i ≤ k) ∧ (∀ k, k ≤ n → C.val.filter_ge k ∈ V → k ≤ j) ∧ i ≤ j + 1 /-- We now prove that a pair of numbers with the required properties exists. Note that this is formulated as a bare existence statement, from which we cannot extract a witness. A constructive version would be possible but would require a little reorganisation. -/ lemma ij_exists : ∃ i j : ℕ, ij_spec U V C i j := begin rcases C with ⟨C,hC⟩, rcases (𝕂.mem_mul U V C).mp hC with ⟨A₀,B₀,hA₀,hB₀,h_angle₀,h_eq₀⟩, have hAC₀ : A₀ ≤ C := by { rw[← h_eq₀], exact le_sup_left }, have hBC₀ : B₀ ≤ C := by { rw[← h_eq₀], exact le_sup_right }, let i_prop : fin n.succ → Prop := λ i, C.filter_lt i.val ∈ U, let j_prop : fin n.succ → Prop := λ j, C.filter_ge j ∈ V, let k_zero : fin n.succ := ⟨0, n.zero_lt_succ⟩, let k_last : fin n.succ := ⟨n, n.lt_succ_self⟩, have i_prop_last : i_prop k_last := begin have : A₀ ≤ C.filter_lt n := λ a ha, ℙ.mem_filter_lt.mpr ⟨hAC₀ ha, a.is_lt⟩, exact U.property A₀ _ this hA₀, end, have j_prop_zero : j_prop k_zero := begin have : B₀ ≤ C.filter_ge 0 := λ b hb, ℙ.mem_filter_ge.mpr ⟨hBC₀ hb, nat.zero_le _⟩, exact V.property B₀ _ this hB₀, end, have i_prop_last' : k_last ∈ finset.univ.filter i_prop := finset.mem_filter.mpr ⟨finset.mem_univ k_last, i_prop_last⟩, have j_prop_zero' : k_zero ∈ finset.univ.filter j_prop := finset.mem_filter.mpr ⟨finset.mem_univ k_zero, j_prop_zero⟩, rcases fin.finset_least_element (finset.univ.filter i_prop) ⟨_,i_prop_last'⟩ with ⟨i,⟨i_prop_i',i_least'⟩⟩, rcases fin.finset_largest_element (finset.univ.filter j_prop) ⟨_,j_prop_zero'⟩ with ⟨j,⟨j_prop_j',j_largest'⟩⟩, let i_prop_i := (finset.mem_filter.mp i_prop_i').right, let j_prop_j := (finset.mem_filter.mp j_prop_j').right, have i_least : ∀ (k : fin n.succ) (hk : i_prop k), i ≤ k := λ k hk, i_least' k (finset.mem_filter.mpr ⟨finset.mem_univ _,hk⟩), have j_largest : ∀ (k : fin n.succ) (hk : j_prop k), k ≤ j := λ k hk, j_largest' k (finset.mem_filter.mpr ⟨finset.mem_univ _,hk⟩), use i.val, use j.val, split, exact nat.le_of_lt_succ i.is_lt, split, exact nat.le_of_lt_succ j.is_lt, split, exact i_prop_i, split, exact j_prop_j, split, { intros k hkn hk, exact i_least ⟨k, nat.lt_succ_of_le hkn⟩ hk }, split, { intros k hkn hk, exact j_largest ⟨k, nat.lt_succ_of_le hkn⟩ hk }, rcases ℙ.angle_iff.mp h_angle₀ with ⟨⟨⟩⟩ | ⟨k,hkA₀,hkB₀⟩, { apply le_add_left, exact le_of_lt i.is_lt }, { let k₀ : fin n.succ := ⟨k.val, lt_trans k.is_lt n.lt_succ_self⟩, let k₁ : fin n.succ := ⟨k.val.succ, nat.succ_lt_succ k.is_lt⟩, have i_prop_k : i_prop k₁ := begin have : A₀ ≤ C.filter_lt k₁ := λ i hi, ℙ.mem_filter_lt.mpr ⟨by { rw[← h_eq₀], exact finset.mem_union_left B₀ hi}, nat.lt_succ_of_le (hkA₀ hi) ⟩, exact U.property A₀ (C.filter_lt k₁) this hA₀, end, have j_prop_k : j_prop k₀ := begin have : B₀ ≤ C.filter_ge k₀ := λ i hi, ℙ.mem_filter_ge.mpr ⟨by { rw[← h_eq₀], exact finset.mem_union_right A₀ hi}, hkB₀ hi ⟩, exact V.property B₀ (C.filter_ge k.val) this hB₀, end, have hik : i.val ≤ k.val + 1 := i_least k₁ i_prop_k, have hkj : k.val ≤ j.val := j_largest k₀ j_prop_k, exact le_trans hik (nat.succ_le_succ hkj) }, end section with_ij /-- In this section, we assume that we are given `i` and `j`, together with a proof of the required properties. Only in the final part of the proof do we invoke `ij_exists`. -/ variables (i j : ℕ) (h : ij_spec U V C i j) include i j h /-- This is the basepoint to which we will contract the comma poset. -/ def base : comma (σ_slice U V) C := begin rcases h with ⟨hi_is_le,hj_is_le,hiU,hjV,hi,hj,hij⟩, let A₁ := C.val.filter_lt i, let B₁ := C.val.filter_ge j, have hA₁ : A₁ ∈ U := hiU, have hB₁ : B₁ ∈ V := hjV, have h_angle₁ : A₁ ∟ B₁ := λ a b ha hb, nat.le_of_lt_succ $ calc a.val < i : (finset.mem_filter.mp ha).right ... ≤ j + 1 : hij ... ≤ b.val + 1 : nat.succ_le_succ (finset.mem_filter.mp hb).right, let AB₁ : 𝕄 n := ⟨⟨A₁,B₁⟩,h_angle₁⟩, let AB₂ : (omul U V).els := ⟨AB₁,(mem_omul U V AB₁).mpr ⟨hA₁,hB₁⟩⟩, have hABC : σ_slice U V AB₂ ≤ C := begin let hAC : A₁ ≤ C.val := ℙ.filter_lt_is_le i C.val, let hBC : B₁ ≤ C.val := ℙ.filter_ge_is_le j C.val, exact sup_le hAC hBC, end, exact ⟨AB₂, hABC⟩ end /-- The values of the map `φ` lie above the basepoint. -/ lemma base_le_φ (X : (comma (σ_slice U V) C)) : base U V C i j h ≤ (φ U V C).val X := begin rcases h with ⟨hi_is_le,hj_is_le,hiU,hjV,hi,hj,hij⟩, rcases X with ⟨⟨⟨⟨A,B⟩,h_angle⟩,h_omul⟩,hABC⟩, change A ∟ B at h_angle, rcases (mem_omul U V _).mp h_omul with ⟨hAU,hBV⟩, change A ∈ U at hAU, change B ∈ V at hBV, change A ⊔ B ≤ C.val at hABC, rcases sup_le_iff.mp hABC with ⟨hAC,hBC⟩, have hi' : ((C.val.filter_lt i) ≤ η U V C A) := begin intros c hc, rw [ℙ.mem_filter_lt] at hc, apply finset.mem_filter.mpr, split, {exact hc.left}, by_contradiction h, rw [not_exists] at h, have : A ≤ C.val.filter_lt c.val := λ a ha, begin rw [ℙ.mem_filter_lt], exact ⟨hAC ha, lt_of_not_ge (λ h₀, h a ⟨ha,h₀⟩)⟩, end, have : C.val.filter_lt c.val ∈ U := U.property A (C.val.filter_lt c.val) this hAU, have : i ≤ c.val := hi c.val (le_of_lt c.is_lt) this, exact not_lt_of_ge this hc.right, end, have hj' : ((C.val.filter_ge j) ≤ θ U V C B) := begin intros c hc, rw [ℙ.mem_filter_ge] at hc, apply finset.mem_filter.mpr, split, {exact hc.left}, by_contradiction h, rw [not_exists] at h, have : B ≤ C.val.filter_ge c.val.succ := λ b hb, begin rw [ℙ.mem_filter_ge], exact ⟨hBC hb, le_of_not_gt (λ h₀, h b ⟨hb, nat.le_of_lt_succ h₀⟩)⟩, end, have : C.val.filter_ge c.val.succ ∈ V := V.property B (C.val.filter_ge c.val.succ) this hBV, have : c.val < j := hj c.val.succ c.is_lt this, exact not_le_of_gt this hc.right, end, exact ⟨hi',hj'⟩ end end with_ij /-- LaTeX: prop-sg-cofinal -/ theorem cofinal : cofinalₕ (σ_slice U V) := begin intro C, rcases ij_exists U V C with ⟨i,j,hij⟩, let M := (comma (σ_slice U V) C), change nonempty (equivₕ M unit), let m : M := base U V C i j hij, let f : hom M unit := const M unit.star, let g : hom unit M := const unit m, have hfg : comp f g = id unit := by { ext p }, have gf_le_φ : comp g f ≤ φ U V C := λ X, base_le_φ U V C i j hij X, have hgf : compₕ (component g) (component f) = idₕ M := (π₀.sound gf_le_φ).trans (π₀.sound (id_le_φ U V C)).symm, let e : equivₕ M unit := { to_fun := component f, inv_fun := component g, left_inv := hgf, right_inv := congr_arg component hfg }, exact ⟨e⟩ end /-- The proof of prop-sg-final uses a number `k` with certain properties. We handle this in the same way as the pair `⟨i,j⟩` in the previous proof. -/ def k_spec (k : ℕ) : Prop := k ≤ n ∧ C.val.filter_lt k.succ ∈ U ∧ C.val.filter_ge k ∈ V lemma k_exists : ∃ (k : ℕ), k_spec U V C k := begin rcases C with ⟨C,hC⟩, rcases (𝕂.mem_mul U V C).mp hC with ⟨A,B,hAU,hBV,h_angle,hABC⟩, rcases ℙ.angle_iff.mp h_angle with ⟨⟨⟩⟩ | ⟨k,hkA,hkB⟩, { use 0, dsimp [k_spec], rw [ℙ.mem_zero A] at hAU, rw [ℙ.mem_zero B] at hBV, rw [ℙ.mem_zero (C.filter_lt 1), ℙ.mem_zero (C.filter_ge 0)], exact ⟨le_refl 0, hAU, hBV⟩ }, { use k.val, dsimp [k_spec], split, { exact le_of_lt k.is_lt }, have hAC : A ≤ C := by { rw[← hABC], exact le_sup_left }, have hBC : B ≤ C := by { rw[← hABC], exact le_sup_right }, let A' := C.filter_lt k.val.succ, let B' := C.filter_ge k.val, have hABC' : A' ⊔ B' = C := ℙ.filter_sup (le_of_lt k.val.lt_succ_self) C, have h_angle' : A' ∟ B' := ℙ.filter_angle (le_refl _) C, have hAA' : A ≤ A' := λ a ha, ℙ.mem_filter_lt.mpr ⟨hAC ha,nat.lt_succ_iff.mpr (hkA ha)⟩, have hBB' : B ≤ B' := λ b hb, ℙ.mem_filter_ge.mpr ⟨hBC hb,hkB hb⟩, have hAU' : A' ∈ U := U.property A A' hAA' hAU, have hBV' : B' ∈ V := V.property B B' hBB' hBV, exact ⟨hAU',hBV'⟩ } end section with_k variables (k : ℕ) (hk : k_spec U V C k) include k hk /-- The restrictions of `αᵢ` and `βᵢ` to the comma poset are denoted by `α'` and `β'`. -/ def α' (i : ℕ) (hi : i > 2 * k) : hom (cocomma (σ_slice U V) C) (cocomma (σ_slice U V) C) := let u := (i + 1)/2 in let v := i / 2 in ⟨ λ x, ⟨⟨(α i).val x.val.val, begin have hu : u > k := begin have : 2 * k + 2 ≤ i + 1 := nat.succ_le_succ hi, have : (2 * k + 2) / 2 ≤ u := nat.div_le_div_right this, exact calc k + 1 = ((k + 1) * 2) / 2 : (nat.mul_div_cancel (k + 1) two_pos').symm ... = (2 * k + 2) / 2 : by { rw [add_mul, one_mul, mul_comm] } ... ≤ u : this, end, rcases half_step i with ⟨hvu,huv⟩, rcases x with ⟨⟨⟨⟨A,B⟩,h_angle⟩,h₀⟩,h₁⟩, rcases (mem_omul U V _).mp h₀ with ⟨hAU,hBV⟩, rcases hk with ⟨hkn,hkU,hkV⟩, change A ∟ B at h_angle, change A ∈ U at hAU, change B ∈ V at hBV, change C.val ≤ A ⊔ B at h₁, apply (mem_omul U V _).mpr, change A.filter_lt u ∈ U ∧ (A.filter_ge v ⊔ B) ∈ V, split, { by_cases h : ∃ (a : 𝕀 n), a ∈ A ∧ a.val ≥ u, { rcases h with ⟨a,ha⟩, have : C.val.filter_lt k.succ ≤ A.filter_lt u := λ c hc, begin rcases ℙ.mem_filter_lt.mp hc with ⟨hcC,hck⟩, replace hck := nat.lt_succ_iff.mp hck, have hcu : c.val < u := lt_of_le_of_lt hck hu, rcases finset.mem_union.mp (h₁ hcC) with hcA | hcB, { rw [ℙ.mem_filter_lt], exact ⟨hcA,hcu⟩ }, { exfalso, have hca : c.val < a.val := lt_of_lt_of_le hcu ha.right, exact not_le_of_gt hca (h_angle ha.left hcB) } end, exact U.property (C.val.filter_lt k.succ) (A.filter_lt u) this hkU }, { have : A.filter_lt u = A := begin rw [not_exists] at h, ext a, rw [ℙ.mem_filter_lt], split, { exact λ h, h.left }, { exact λ ha, ⟨ha, lt_of_not_ge (λ h₀, h a ⟨ha,h₀⟩)⟩ } end, rw [this], exact hAU } }, { exact V.property B (A.filter_ge v ⊔ B) le_sup_right hBV } end⟩, begin rcases half_step i with ⟨hvu,huv⟩, rcases x with ⟨⟨⟨⟨A,B⟩,h_angle⟩,h₀⟩,h₁⟩, change C.val ≤ ((A.filter_lt u) ⊔ (A.filter_ge v ⊔ B)), rw [← sup_assoc, ℙ.filter_sup hvu A], exact h₁, end⟩, begin rintro ⟨⟨X₀,_⟩,_⟩ ⟨⟨X₁,_⟩,_⟩ h, exact (α i).property h end⟩ def β' (i : ℕ) (hi : i ≤ 2 * k + 1) : hom (cocomma (σ_slice U V) C) (cocomma (σ_slice U V) C) := let u := (i + 1)/2 in let v := i / 2 in ⟨ λ x, ⟨⟨(β i).val x.val.val, begin have hv : i/2 ≤ k := calc i / 2 ≤ (2 * k + 1) / 2 : nat.div_le_div_right hi ... = k : (half_misc k).2.1, rcases half_step i with ⟨hvu,huv⟩, rcases x with ⟨⟨⟨⟨A,B⟩,h_angle⟩,h₀⟩,h₁⟩, rcases (mem_omul U V _).mp h₀ with ⟨hAU,hBV⟩, rcases hk with ⟨hkn,hkU,hkV⟩, change A ∟ B at h_angle, change A ∈ U at hAU, change B ∈ V at hBV, change C.val ≤ A ⊔ B at h₁, apply (mem_omul U V _).mpr, change A ⊔ B.filter_lt u ∈ U ∧ B.filter_ge v ∈ V, split, { exact U.property A (A ⊔ B.filter_lt u) le_sup_left hAU }, { by_cases h : ∃ (b : 𝕀 n), b ∈ B ∧ b.val < v, { rcases h with ⟨b,hb⟩, have : C.val.filter_ge k ≤ B.filter_ge v := λ c hc, begin rcases ℙ.mem_filter_ge.mp hc with ⟨hcC,hck⟩, have hcu : c.val ≥ v := le_trans hv hck, rcases finset.mem_union.mp (h₁ hcC) with hcA | hcB, { exfalso, have hbc : b.val < c.val := lt_of_lt_of_le hb.right hcu, exact not_le_of_gt hbc (h_angle hcA hb.left) }, { rw [ℙ.mem_filter_ge], exact ⟨hcB,hcu⟩ } end, exact V.property (C.val.filter_ge k) (B.filter_ge v) this hkV }, { have : B.filter_ge v = B := begin rw [not_exists] at h, ext a, rw [ℙ.mem_filter_ge], split, { exact λ h, h.left }, { exact λ ha, ⟨ha, le_of_not_gt (λ h₀, h a ⟨ha,h₀⟩)⟩ } end, rw [this], exact hBV } }, end⟩, begin rcases half_step i with ⟨hvu,huv⟩, rcases x with ⟨⟨⟨⟨A,B⟩,h_angle⟩,h₀⟩,h₁⟩, change C.val ≤ A ⊔ (B.filter_lt u) ⊔ (B.filter_ge v), rw [sup_assoc, ℙ.filter_sup hvu B], exact h₁, end⟩, begin rintro ⟨⟨X₀,_⟩,_⟩ ⟨⟨X₁,_⟩,_⟩ h, exact (β i).property h end⟩ lemma α'_last : α' U V C k hk (2 * n + 1) (lt_of_le_of_lt (nat.mul_le_mul_left 2 hk.1) (2 * n).lt_succ_self) = poset.id _ := begin ext1 X, rcases X with ⟨⟨Y,_⟩,_⟩, apply subtype.eq, apply subtype.eq, change (α (2 * n + 1)).val Y = Y, rw [α_last (le_of_lt (2 * n).lt_succ_self)], refl, end lemma β'_zero : β' U V C k hk 0 (nat.zero_le _) = poset.id _ := begin ext1 X, rcases X with ⟨⟨Y,_⟩,_⟩, apply subtype.eq, apply subtype.eq, change (β 0).val Y = Y, rw [β_zero], refl, end /-- All the maps `α'` lie in the identity component. -/ lemma α'_component : ∀ (i : ℕ) (hi : i > 2 * k), component (α' U V C k hk i hi) = poset.idₕ _ := begin let c := λ (i : ℕ) (hi : i > 2 * k), component (α' U V C k hk i hi), change ∀ (i : ℕ) (hi : i > 2 * k), c i hi = poset.idₕ _, let u := λ (i : ℕ), ∀ (hi : i > 2 * k), c i hi = c (2 * k + 1) (2 * k).lt_succ_self, have u_zero : u 0 := λ hi, (nat.not_lt_zero _ hi).elim, have u_even : ∀ i, u (2 * i) → u (2 * i + 1) := begin intros i hu hm, let hi : k ≤ i := (mul_le_mul_left two_pos').mp (nat.le_of_succ_le_succ hm), by_cases h : k = i, { cases h, refl }, replace hi := lt_of_le_of_ne hi h, have hm' : 2 * i > 2 * k := (mul_lt_mul_left two_pos').mpr hi, rw [← hu hm'], symmetry, apply π₀.sound, rintro ⟨⟨X,_⟩,_⟩, exact α_even_step i X end, have u_odd : ∀ i, u (2 * i + 1) → u (2 * i + 2) := begin intros i hu hm, let hi := @nat.div_le_div_right _ _ (nat.le_of_succ_le_succ hm) 2, rw [(half_misc k).1, (half_misc i).2.1] at hi, have hm' : 2 * i + 1 > 2 * k := lt_of_le_of_lt ((mul_le_mul_left two_pos').mpr hi) (2 * i).lt_succ_self, rw [← hu hm'], apply π₀.sound, rintro ⟨⟨X,_⟩,_⟩, exact α_odd_step i X end, have u_all := parity_induction u u_zero u_even u_odd, have h : (2 * n + 1) > 2 * k := lt_of_le_of_lt ((mul_le_mul_left two_pos').mpr hk.1) (2 * n).lt_succ_self, have : c (2 * n + 1) h = poset.idₕ _ := congr_arg component (α'_last U V C k hk), intros i hi, rw [← this, u_all i hi, u_all (2 * n + 1) h] end /-- All the maps `β'` lie in the identity component. -/ lemma β'_component : ∀ (i : ℕ) (hi : i ≤ 2 * k + 1), component (β' U V C k hk i hi) = poset.idₕ _ := begin let c := λ (i : ℕ) (hi : i ≤ 2 * k + 1), component (β' U V C k hk i hi), let u := λ (i : ℕ), ∀ (hi : i ≤ 2 * k + 1), c i hi = idₕ _, change ∀ (i : ℕ), u i, have u_zero : u 0 := λ _, congr_arg component (β'_zero U V C k hk), have u_even : ∀ i, u (2 * i) → u (2 * i + 1) := begin intros i hu hm, have hm' := le_trans (le_of_lt (2 * i).lt_succ_self) hm, rw [← hu hm'], symmetry, apply π₀.sound, rintro ⟨⟨X,_⟩,_⟩, exact β_even_step i X end, have u_odd : ∀ i, u (2 * i + 1) → u (2 * i + 2) := begin intros i hu hm, have hm' := le_trans (le_of_lt (2 * i + 1).lt_succ_self) hm, rw [← hu hm'], apply π₀.sound, rintro ⟨⟨X,_⟩,_⟩, exact β_odd_step i X end, exact parity_induction u u_zero u_even u_odd end def α_middle := α' U V C k hk (2 * k + 1) (2 * k).lt_succ_self def β_middle := β' U V C k hk (2 * k + 1) (le_refl _) def α_middle' : (ℙ n) × (ℙ n) → (ℙ n) × (ℙ n) := λ AB, ⟨AB.1.filter_lt k.succ,AB.1.filter_ge k ⊔ AB.2⟩ def β_middle' : (ℙ n) × (ℙ n) → (ℙ n) × (ℙ n) := λ AB, ⟨AB.1 ⊔ AB.2.filter_lt k.succ,AB.2.filter_ge k⟩ lemma α_middle_val (X : cocomma (σ_slice U V) C) : ((α_middle U V C k hk).val X).val.val.val = α_middle' U V C k hk X.val.val.val := begin rcases X with ⟨⟨⟨⟨A,B⟩,_⟩,_⟩,_⟩, change (prod.mk _ _) = (prod.mk _ _), rcases half_misc k with ⟨h₀,h₁,h₂,h₃⟩, rw [h₁, h₂] end lemma β_middle_val (X : cocomma (σ_slice U V) C) : ((β_middle U V C k hk).val X).val.val.val = β_middle' U V C k hk X.val.val.val := begin rcases X with ⟨⟨⟨⟨A,B⟩,_⟩,_⟩,_⟩, change (prod.mk _ _) = (prod.mk _ _), rcases half_misc k with ⟨h₀,h₁,h₂,h₃⟩, rw [h₁, h₂] end def αβ_middle := comp (α_middle U V C k hk) (β_middle U V C k hk) def αβ_middle' : (ℙ n) × (ℙ n) → (ℙ n) × (ℙ n) := λ AB, ⟨(AB.1 ⊔ AB.2).filter_lt k.succ, (AB.1 ⊔ AB.2).filter_ge k⟩ lemma αβ_middle_val (X : cocomma (σ_slice U V) C) : ((αβ_middle U V C k hk).val X).val.val.val = αβ_middle' U V C k hk X.val.val.val := begin dsimp [αβ_middle, comp], change ((α_middle U V C k hk).val _).val.val.val = _, rw[α_middle_val,β_middle_val,α_middle',β_middle'], change (prod.mk _ _) = (prod.mk _ _), apply prod.ext ; simp only [], { ext x, simp [ℙ.mem_filter_lt, ℙ.mem_filter_ge, ℙ.mem_sup], tauto }, { ext x, simp [ℙ.mem_filter_lt, ℙ.mem_filter_ge, ℙ.mem_sup], tauto }, end /-- This defines the basepoint to which we will contract -/ def cobase : cocomma (σ_slice U V) C := ⟨⟨⟨⟨C.val.filter_lt k.succ,C.val.filter_ge k⟩, ℙ.filter_angle (le_refl k.succ) C.val⟩, (mem_omul U V _).mpr hk.2⟩, begin change _ ≤ _ ⊔ _, rw [ℙ.filter_sup (le_of_lt k.lt_succ_self) C.val], exact le_refl C.val end⟩ lemma cocomma_order (X Y : cocomma (σ_slice U V) C) : X ≤ Y ↔ X.val.val.val ≤ Y.val.val.val := begin rcases X with ⟨⟨⟨⟨A,B⟩,u₀⟩,u₁⟩,u₂⟩, rcases Y with ⟨⟨⟨⟨C,D⟩,v₀⟩,v₁⟩,v₂⟩, let X₀ : (ℙ n) × (ℙ n) := ⟨A,B⟩, let Y₀ : (ℙ n) × (ℙ n) := ⟨C,D⟩, change (X₀ ≤ Y₀) ↔ (A ≤ C ∧ B ≤ D), dsimp [X₀, Y₀], refl end lemma αβ_middle_ge (X : cocomma (σ_slice U V) C) : (cobase U V C k hk) ≤ ((αβ_middle U V C k hk).val X) := begin let h := cocomma_order U V C k hk, let hh := h (cobase U V C k hk) ((αβ_middle U V C k hk).val X), rw [hh, αβ_middle_val, αβ_middle', cobase], rcases X with ⟨⟨⟨⟨A,B⟩,h₀⟩,h₁⟩,h₂⟩, let D := A ⊔ B, change C.val ≤ D at h₂, change (C.val.filter_lt k.succ) ≤ (D.filter_lt k.succ) ∧ (C.val.filter_ge k) ≤ (D.filter_ge k), split; intros x hx, { rw[ℙ.mem_filter_lt] at hx ⊢, exact ⟨h₂ hx.1, hx.2⟩ }, { rw[ℙ.mem_filter_ge] at hx ⊢, exact ⟨h₂ hx.1, hx.2⟩ } end end with_k theorem final : finalₕ (σ_slice U V) := begin intro C, let M := (cocomma (σ_slice U V) C), change nonempty (equivₕ M unit), let f : hom M unit := const M unit.star, rcases k_exists U V C with ⟨k, hk⟩, let m := cobase U V C k hk, let g : hom unit M := const unit m, have hfg : comp f g = id unit := by { ext t }, have hgf : component (comp g f) = idₕ _ := begin let gf₀ : hom M M := comp g f, let gf₁ : hom M M := αβ_middle U V C k hk, let gf₂ : hom M M := poset.id _, have : gf₀ ≤ gf₁ := λ X, αβ_middle_ge U V C k hk X, rw [π₀.sound this], let u := component (α_middle U V C k hk), let v := component (β_middle U V C k hk), have huv : component gf₁ = compₕ u v := rfl, have : u = idₕ _ := α'_component U V C k hk (2 * k + 1) _, rw [this] at huv, have : v = idₕ _ := β'_component U V C k hk (2 * k + 1) _, rw [this, comp_idₕ] at huv, exact huv, end, let e : equivₕ M unit := { to_fun := component f, inv_fun := component g, left_inv := hgf, right_inv := congr_arg component hfg }, exact ⟨e⟩ end end σ_slice end itloc
280fc2626a128311524989cb60e78a911db26ae7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/prod/lex.lean
a0bfe98ca03ffd3464ada98ee38a418bc52057e0
[ "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
6,263
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Minchao Wu -/ import order.bounded_order /-! # Lexicographic order > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/783 > Any changes to this file require a corresponding PR to mathlib4. This file defines the lexicographic relation for pairs of orders, partial orders and linear orders. ## Main declarations * `prod.lex.<pre/partial_/linear_>order`: Instances lifting the orders on `α` and `β` to `α ×ₗ β`. ## Notation * `α ×ₗ β`: `α × β` equipped with the lexicographic order ## See also Related files are: * `data.finset.colex`: Colexicographic order on finite sets. * `data.list.lex`: Lexicographic order on lists. * `data.pi.lex`: Lexicographic order on `Πₗ i, α i`. * `data.psigma.order`: Lexicographic order on `Σ' i, α i`. * `data.sigma.order`: Lexicographic order on `Σ i, α i`. -/ variables {α β γ : Type*} namespace prod.lex notation α ` ×ₗ `:35 β:34 := lex (prod α β) meta instance [has_to_format α] [has_to_format β] : has_to_format (α ×ₗ β) := prod.has_to_format instance decidable_eq (α β : Type*) [decidable_eq α] [decidable_eq β] : decidable_eq (α ×ₗ β) := prod.decidable_eq instance inhabited (α β : Type*) [inhabited α] [inhabited β] : inhabited (α ×ₗ β) := prod.inhabited /-- Dictionary / lexicographic ordering on pairs. -/ instance has_le (α β : Type*) [has_lt α] [has_le β] : has_le (α ×ₗ β) := { le := prod.lex (<) (≤) } instance has_lt (α β : Type*) [has_lt α] [has_lt β] : has_lt (α ×ₗ β) := { lt := prod.lex (<) (<) } lemma le_iff [has_lt α] [has_le β] (a b : α × β) : to_lex a ≤ to_lex b ↔ a.1 < b.1 ∨ a.1 = b.1 ∧ a.2 ≤ b.2 := prod.lex_def (<) (≤) lemma lt_iff [has_lt α] [has_lt β] (a b : α × β) : to_lex a < to_lex b ↔ a.1 < b.1 ∨ a.1 = b.1 ∧ a.2 < b.2 := prod.lex_def (<) (<) /-- Dictionary / lexicographic preorder for pairs. -/ instance preorder (α β : Type*) [preorder α] [preorder β] : preorder (α ×ₗ β) := { le_refl := refl_of $ prod.lex _ _, le_trans := λ _ _ _, trans_of $ prod.lex _ _, lt_iff_le_not_le := λ x₁ x₂, match x₁, x₂ with | to_lex (a₁, b₁), to_lex (a₂, b₂) := begin split, { rintro (⟨_, _, hlt⟩ | ⟨_, hlt⟩), { split, { left, assumption }, { rintro ⟨⟩, { apply lt_asymm hlt, assumption }, { apply lt_irrefl _ hlt } } }, { split, { right, rw lt_iff_le_not_le at hlt, exact hlt.1 }, { rintro ⟨⟩, { apply lt_irrefl a₁, assumption }, { rw lt_iff_le_not_le at hlt, apply hlt.2, assumption } } } }, { rintros ⟨⟨⟩, h₂r⟩, { left, assumption }, { right, rw lt_iff_le_not_le, split, { assumption }, { intro h, apply h₂r, right, exact h } } } end end, .. prod.lex.has_le α β, .. prod.lex.has_lt α β } section preorder variables [partial_order α] [preorder β] lemma to_lex_mono : monotone (to_lex : α × β → α ×ₗ β) := begin rintro ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ ⟨ha, hb⟩, obtain rfl | ha : a₁ = a₂ ∨ _ := ha.eq_or_lt, { exact right _ hb }, { exact left _ _ ha } end lemma to_lex_strict_mono : strict_mono (to_lex : α × β → α ×ₗ β) := begin rintro ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, obtain rfl | ha : a₁ = a₂ ∨ _ := h.le.1.eq_or_lt, { exact right _ (prod.mk_lt_mk_iff_right.1 h) }, { exact left _ _ ha } end end preorder /-- Dictionary / lexicographic partial_order for pairs. -/ instance partial_order (α β : Type*) [partial_order α] [partial_order β] : partial_order (α ×ₗ β) := { le_antisymm := by { haveI : is_strict_order α (<) := { irrefl := lt_irrefl, trans := λ _ _ _, lt_trans }, haveI : is_antisymm β (≤) := ⟨λ _ _, le_antisymm⟩, exact @antisymm _ (prod.lex _ _) _, }, .. prod.lex.preorder α β } /-- Dictionary / lexicographic linear_order for pairs. -/ instance linear_order (α β : Type*) [linear_order α] [linear_order β] : linear_order (α ×ₗ β) := { le_total := total_of (prod.lex _ _), decidable_le := prod.lex.decidable _ _, decidable_lt := prod.lex.decidable _ _, decidable_eq := lex.decidable_eq _ _, .. prod.lex.partial_order α β } instance order_bot [partial_order α] [preorder β] [order_bot α] [order_bot β] : order_bot (α ×ₗ β) := { bot := to_lex ⊥, bot_le := λ a, to_lex_mono bot_le } instance order_top [partial_order α] [preorder β] [order_top α] [order_top β] : order_top (α ×ₗ β) := { top := to_lex ⊤, le_top := λ a, to_lex_mono le_top } instance bounded_order [partial_order α] [preorder β] [bounded_order α] [bounded_order β] : bounded_order (α ×ₗ β) := { ..lex.order_bot, ..lex.order_top } instance [preorder α] [preorder β] [densely_ordered α] [densely_ordered β] : densely_ordered (α ×ₗ β) := ⟨begin rintro _ _ (@⟨a₁, b₁, a₂, b₂, h⟩ | @⟨a, b₁, b₂, h⟩), { obtain ⟨c, h₁, h₂⟩ := exists_between h, exact ⟨(c, b₁), left _ _ h₁, left _ _ h₂⟩ }, { obtain ⟨c, h₁, h₂⟩ := exists_between h, exact ⟨(a, c), right _ h₁, right _ h₂⟩ } end⟩ instance no_max_order_of_left [preorder α] [preorder β] [no_max_order α] : no_max_order (α ×ₗ β) := ⟨by { rintro ⟨a, b⟩, obtain ⟨c, h⟩ := exists_gt a, exact ⟨⟨c, b⟩, left _ _ h⟩ }⟩ instance no_min_order_of_left [preorder α] [preorder β] [no_min_order α] : no_min_order (α ×ₗ β) := ⟨by { rintro ⟨a, b⟩, obtain ⟨c, h⟩ := exists_lt a, exact ⟨⟨c, b⟩, left _ _ h⟩ }⟩ instance no_max_order_of_right [preorder α] [preorder β] [no_max_order β] : no_max_order (α ×ₗ β) := ⟨by { rintro ⟨a, b⟩, obtain ⟨c, h⟩ := exists_gt b, exact ⟨⟨a, c⟩, right _ h⟩ }⟩ instance no_min_order_of_right [preorder α] [preorder β] [no_min_order β] : no_min_order (α ×ₗ β) := ⟨by { rintro ⟨a, b⟩, obtain ⟨c, h⟩ := exists_lt b, exact ⟨⟨a, c⟩, right _ h⟩ }⟩ end prod.lex
fe9007ee35915a7f7b9812191ad19cbbe37de267
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/euclidean_domain.lean
f0c264783931ac77a3be9204296f62daa2bcbfe6
[ "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
18,232
lean
/- Copyright (c) 2018 Louis Carlin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Louis Carlin, Mario Carneiro -/ import data.int.basic import algebra.field.basic /-! # Euclidean domains This file introduces Euclidean domains and provides the extended Euclidean algorithm. To be precise, a slightly more general version is provided which is sometimes called a transfinite Euclidean domain and differs in the fact that the degree function need not take values in `ℕ` but can take values in any well-ordered set. Transfinite Euclidean domains were introduced by Motzkin and examples which don't satisfy the classical notion were provided independently by Hiblot and Nagata. ## Main definitions * `euclidean_domain`: Defines Euclidean domain with functions `quotient` and `remainder`. Instances of `has_div` and `has_mod` are provided, so that one can write `a = b * (a / b) + a % b`. * `gcd`: defines the greatest common divisors of two elements of a Euclidean domain. * `xgcd`: given two elements `a b : R`, `xgcd a b` defines the pair `(x, y)` such that `x * a + y * b = gcd a b`. * `lcm`: defines the lowest common multiple of two elements `a` and `b` of a Euclidean domain as `a * b / (gcd a b)` ## Main statements * `gcd_eq_gcd_ab`: states Bézout's lemma for Euclidean domains. * `int.euclidean_domain`: shows that `ℤ` is a Euclidean domain. * `field.to_euclidean_domain`: shows that any field is a Euclidean domain. ## Notation `≺` denotes the well founded relation on the Euclidean domain, e.g. in the example of the polynomial ring over a field, `p ≺ q` for polynomials `p` and `q` if and only if the degree of `p` is less than the degree of `q`. ## Implementation details Instead of working with a valuation, `euclidean_domain` is implemented with the existence of a well founded relation `r` on the integral domain `R`, which in the example of `ℤ` would correspond to setting `i ≺ j` for integers `i` and `j` if the absolute value of `i` is smaller than the absolute value of `j`. ## References * [Th. Motzkin, *The Euclidean algorithm*][MR32592] * [J.-J. Hiblot, *Des anneaux euclidiens dont le plus petit algorithme n'est pas à valeurs finies*] [MR399081] * [M. Nagata, *On Euclid algorithm*][MR541021] ## Tags Euclidean domain, transfinite Euclidean domain, Bézout's lemma -/ universe u /-- A `euclidean_domain` is an non-trivial commutative ring with a division and a remainder, satisfying `b * (a / b) + a % b = a`. The definition of a euclidean domain usually includes a valuation function `R → ℕ`. This definition is slightly generalised to include a well founded relation `r` with the property that `r (a % b) b`, instead of a valuation. -/ @[protect_proj without mul_left_not_lt r_well_founded] class euclidean_domain (R : Type u) extends comm_ring R, nontrivial R := (quotient : R → R → R) (quotient_zero : ∀ a, quotient a 0 = 0) (remainder : R → R → R) (quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a) (r : R → R → Prop) (r_well_founded : well_founded r) (remainder_lt : ∀ a {b}, b ≠ 0 → r (remainder a b) b) (mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a) namespace euclidean_domain variable {R : Type u} variables [euclidean_domain R] local infix ` ≺ `:50 := euclidean_domain.r @[priority 70] -- see Note [lower instance priority] instance : has_div R := ⟨euclidean_domain.quotient⟩ @[priority 70] -- see Note [lower instance priority] instance : has_mod R := ⟨euclidean_domain.remainder⟩ theorem div_add_mod (a b : R) : b * (a / b) + a % b = a := euclidean_domain.quotient_mul_add_remainder_eq _ _ lemma mod_add_div (a b : R) : a % b + b * (a / b) = a := (add_comm _ _).trans (div_add_mod _ _) lemma mod_add_div' (m k : R) : m % k + (m / k) * k = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : R) : (m / k) * k + m % k = m := by { rw mul_comm, exact div_add_mod _ _ } lemma mod_eq_sub_mul_div {R : Type*} [euclidean_domain R] (a b : R) : a % b = a - b * (a / b) := calc a % b = b * (a / b) + a % b - b * (a / b) : (add_sub_cancel' _ _).symm ... = a - b * (a / b) : by rw div_add_mod theorem mod_lt : ∀ a {b : R}, b ≠ 0 → (a % b) ≺ b := euclidean_domain.remainder_lt theorem mul_right_not_lt {a : R} (b) (h : a ≠ 0) : ¬(a * b) ≺ b := by { rw mul_comm, exact mul_left_not_lt b h } lemma mul_div_cancel_left {a : R} (b) (a0 : a ≠ 0) : a * b / a = b := eq.symm $ eq_of_sub_eq_zero $ classical.by_contradiction $ λ h, begin have := mul_left_not_lt a h, rw [mul_sub, sub_eq_iff_eq_add'.2 (div_add_mod (a*b) a).symm] at this, exact this (mod_lt _ a0) end lemma mul_div_cancel (a) {b : R} (b0 : b ≠ 0) : a * b / b = a := by { rw mul_comm, exact mul_div_cancel_left a b0 } @[simp] lemma mod_zero (a : R) : a % 0 = a := by simpa only [zero_mul, zero_add] using div_add_mod a 0 @[simp] lemma mod_eq_zero {a b : R} : a % b = 0 ↔ b ∣ a := ⟨λ h, by { rw [← div_add_mod a b, h, add_zero], exact dvd_mul_right _ _ }, λ ⟨c, e⟩, begin rw [e, ← add_left_cancel_iff, div_add_mod, add_zero], haveI := classical.dec, by_cases b0 : b = 0, { simp only [b0, zero_mul] }, { rw [mul_div_cancel_left _ b0] } end⟩ @[simp] lemma mod_self (a : R) : a % a = 0 := mod_eq_zero.2 dvd_rfl lemma dvd_mod_iff {a b c : R} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a := by rw [dvd_add_iff_right (h.mul_right _), div_add_mod] lemma lt_one (a : R) : a ≺ (1:R) → a = 0 := by { haveI := classical.dec, exact not_imp_not.1 (λ h, by simpa only [one_mul] using mul_left_not_lt 1 h) } lemma val_dvd_le : ∀ a b : R, b ∣ a → a ≠ 0 → ¬a ≺ b | _ b ⟨d, rfl⟩ ha := mul_left_not_lt b (mt (by { rintro rfl, exact mul_zero _ }) ha) @[simp] lemma mod_one (a : R) : a % 1 = 0 := mod_eq_zero.2 (one_dvd _) @[simp] lemma zero_mod (b : R) : 0 % b = 0 := mod_eq_zero.2 (dvd_zero _) @[simp, priority 900] lemma div_zero (a : R) : a / 0 = 0 := euclidean_domain.quotient_zero a @[simp, priority 900] lemma zero_div {a : R} : 0 / a = 0 := classical.by_cases (λ a0 : a = 0, a0.symm ▸ div_zero 0) (λ a0, by simpa only [zero_mul] using mul_div_cancel 0 a0) @[simp, priority 900] lemma div_self {a : R} (a0 : a ≠ 0) : a / a = 1 := by simpa only [one_mul] using mul_div_cancel 1 a0 lemma eq_div_of_mul_eq_left {a b c : R} (hb : b ≠ 0) (h : a * b = c) : a = c / b := by rw [← h, mul_div_cancel _ hb] lemma eq_div_of_mul_eq_right {a b c : R} (ha : a ≠ 0) (h : a * b = c) : b = c / a := by rw [← h, mul_div_cancel_left _ ha] theorem mul_div_assoc (x : R) {y z : R} (h : z ∣ y) : x * y / z = x * (y / z) := begin classical, by_cases hz : z = 0, { subst hz, rw [div_zero, div_zero, mul_zero] }, rcases h with ⟨p, rfl⟩, rw [mul_div_cancel_left _ hz, mul_left_comm, mul_div_cancel_left _ hz] end @[simp, priority 900] -- This generalizes `int.div_one`, see note [simp-normal form] lemma div_one (p : R) : p / 1 = p := (euclidean_domain.eq_div_of_mul_eq_left (@one_ne_zero R _ _) (mul_one p)).symm lemma div_dvd_of_dvd {p q : R} (hpq : q ∣ p) : p / q ∣ p := begin by_cases hq : q = 0, { rw [hq, zero_dvd_iff] at hpq, rw hpq, exact dvd_zero _ }, use q, rw [mul_comm, ← euclidean_domain.mul_div_assoc _ hpq, mul_comm, euclidean_domain.mul_div_cancel _ hq] end lemma dvd_div_of_mul_dvd {a b c : R} (h : a * b ∣ c) : b ∣ c / a := begin rcases eq_or_ne a 0 with rfl | ha, { simp only [div_zero, dvd_zero] }, rcases h with ⟨d, rfl⟩, refine ⟨d, _⟩, rw [mul_assoc, mul_div_cancel_left _ ha] end section open_locale classical @[elab_as_eliminator] theorem gcd.induction {P : R → R → Prop} : ∀ a b : R, (∀ x, P 0 x) → (∀ a b, a ≠ 0 → P (b % a) a → P a b) → P a b | a := λ b H0 H1, if a0 : a = 0 then a0.symm ▸ H0 _ else have h:_ := mod_lt b a0, H1 _ _ a0 (gcd.induction (b%a) a H0 H1) using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} end section gcd variable [decidable_eq R] /-- `gcd a b` is a (non-unique) element such that `gcd a b ∣ a` `gcd a b ∣ b`, and for any element `c` such that `c ∣ a` and `c ∣ b`, then `c ∣ gcd a b` -/ def gcd : R → R → R | a := λ b, if a0 : a = 0 then b else have h:_ := mod_lt b a0, gcd (b%a) a using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} @[simp] theorem gcd_zero_left (a : R) : gcd 0 a = a := by { rw gcd, exact if_pos rfl } @[simp] theorem gcd_zero_right (a : R) : gcd a 0 = a := by { rw gcd, split_ifs; simp only [h, zero_mod, gcd_zero_left] } theorem gcd_val (a b : R) : gcd a b = gcd (b % a) a := by { rw gcd, split_ifs; [simp only [h, mod_zero, gcd_zero_right], refl]} theorem gcd_dvd (a b : R) : gcd a b ∣ a ∧ gcd a b ∣ b := gcd.induction a b (λ b, by { rw gcd_zero_left, exact ⟨dvd_zero _, dvd_rfl⟩ }) (λ a b aneq ⟨IH₁, IH₂⟩, by { rw gcd_val, exact ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩ }) theorem gcd_dvd_left (a b : R) : gcd a b ∣ a := (gcd_dvd a b).left theorem gcd_dvd_right (a b : R) : gcd a b ∣ b := (gcd_dvd a b).right protected theorem gcd_eq_zero_iff {a b : R} : gcd a b = 0 ↔ a = 0 ∧ b = 0 := ⟨λ h, by simpa [h] using gcd_dvd a b, by { rintro ⟨rfl, rfl⟩, exact gcd_zero_right _ }⟩ theorem dvd_gcd {a b c : R} : c ∣ a → c ∣ b → c ∣ gcd a b := gcd.induction a b (λ _ _ H, by simpa only [gcd_zero_left] using H) (λ a b a0 IH ca cb, by { rw gcd_val, exact IH ((dvd_mod_iff ca).2 cb) ca }) theorem gcd_eq_left {a b : R} : gcd a b = a ↔ a ∣ b := ⟨λ h, by {rw ← h, apply gcd_dvd_right }, λ h, by rw [gcd_val, mod_eq_zero.2 h, gcd_zero_left]⟩ @[simp] theorem gcd_one_left (a : R) : gcd 1 a = 1 := gcd_eq_left.2 (one_dvd _) @[simp] theorem gcd_self (a : R) : gcd a a = a := gcd_eq_left.2 dvd_rfl /-- An implementation of the extended GCD algorithm. At each step we are computing a triple `(r, s, t)`, where `r` is the next value of the GCD algorithm, to compute the greatest common divisor of the input (say `x` and `y`), and `s` and `t` are the coefficients in front of `x` and `y` to obtain `r` (i.e. `r = s * x + t * y`). The function `xgcd_aux` takes in two triples, and from these recursively computes the next triple: ``` xgcd_aux (r, s, t) (r', s', t') = xgcd_aux (r' % r, s' - (r' / r) * s, t' - (r' / r) * t) (r, s, t) ``` -/ def xgcd_aux : R → R → R → R → R → R → R × R × R | r := λ s t r' s' t', if hr : r = 0 then (r', s', t') else have r' % r ≺ r, from mod_lt _ hr, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} @[simp] theorem xgcd_zero_left {s t r' s' t' : R} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by { unfold xgcd_aux, exact if_pos rfl } theorem xgcd_aux_rec {r s t r' s' t' : R} (h : r ≠ 0) : xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t := by { conv {to_lhs, rw [xgcd_aux]}, exact if_neg h} /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : R) : R × R := (xgcd_aux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a (x y : R) : R := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : R) : R := (xgcd x y).2 @[simp] theorem gcd_a_zero_left {s : R} : gcd_a 0 s = 0 := by { unfold gcd_a, rw [xgcd, xgcd_zero_left] } @[simp] theorem gcd_b_zero_left {s : R} : gcd_b 0 s = 1 := by { unfold gcd_b, rw [xgcd, xgcd_zero_left] } @[simp] theorem xgcd_aux_fst (x y : R) : ∀ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by { intros, rw [xgcd_zero_left, gcd_zero_left] }) (λ x y h IH s t s' t', by { simp only [xgcd_aux_rec h, if_neg h, IH], rw ← gcd_val }) theorem xgcd_aux_val (x y : R) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1, prod.mk.eta] theorem xgcd_val (x y : R) : xgcd x y = (gcd_a x y, gcd_b x y) := prod.mk.eta.symm private def P (a b : R) : R × R × R → Prop | (r, s, t) := (r : R) = a * s + b * t theorem xgcd_aux_P (a b : R) {r r' : R} : ∀ {s t s' t'}, P a b (r, s, t) → P a b (r', s', t') → P a b (xgcd_aux r s t r' s' t') := gcd.induction r r' (by { intros, simpa only [xgcd_zero_left] }) $ λ x y h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, unfold P at p p' ⊢, rw [mul_sub, mul_sub, add_sub, sub_add_eq_add_sub, ← p', sub_sub, mul_comm _ s, ← mul_assoc, mul_comm _ t, ← mul_assoc, ← add_mul, ← p, mod_eq_sub_mul_div] end /-- An explicit version of **Bézout's lemma** for Euclidean domains. -/ theorem gcd_eq_gcd_ab (a b : R) : (gcd a b : R) = a * gcd_a a b + b * gcd_b a b := by { have := @xgcd_aux_P _ _ _ a b a b 1 0 0 1 (by rw [P, mul_one, mul_zero, add_zero]) (by rw [P, mul_one, mul_zero, zero_add]), rwa [xgcd_aux_val, xgcd_val] at this } @[priority 70] -- see Note [lower instance priority] instance (R : Type*) [e : euclidean_domain R] : is_domain R := by { haveI := classical.dec_eq R, exact { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, (or_iff_not_and_not.2 $ λ h0, h0.1 $ by rw [← mul_div_cancel a h0.2, h, zero_div]), ..e }} end gcd section lcm variables [decidable_eq R] /-- `lcm a b` is a (non-unique) element such that `a ∣ lcm a b` `b ∣ lcm a b`, and for any element `c` such that `a ∣ c` and `b ∣ c`, then `lcm a b ∣ c` -/ def lcm (x y : R) : R := x * y / gcd x y theorem dvd_lcm_left (x y : R) : x ∣ lcm x y := classical.by_cases (assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ }) (λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).2 in ⟨z, eq.symm $ eq_div_of_mul_eq_left hxy $ by rw [mul_right_comm, mul_assoc, ← hz]⟩) theorem dvd_lcm_right (x y : R) : y ∣ lcm x y := classical.by_cases (assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ }) (λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).1 in ⟨z, eq.symm $ eq_div_of_mul_eq_right hxy $ by rw [← mul_assoc, mul_right_comm, ← hz]⟩) theorem lcm_dvd {x y z : R} (hxz : x ∣ z) (hyz : y ∣ z) : lcm x y ∣ z := begin rw lcm, by_cases hxy : gcd x y = 0, { rw [hxy, div_zero], rw euclidean_domain.gcd_eq_zero_iff at hxy, rwa hxy.1 at hxz }, rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, suffices : x * y ∣ z * gcd x y, { cases this with p hp, use p, generalize_hyp : gcd x y = g at hxy hs hp ⊢, subst hs, rw [mul_left_comm, mul_div_cancel_left _ hxy, ← mul_left_inj' hxy, hp], rw [← mul_assoc], simp only [mul_right_comm] }, rw [gcd_eq_gcd_ab, mul_add], apply dvd_add, { rw mul_left_comm, exact mul_dvd_mul_left _ (hyz.mul_right _) }, { rw [mul_left_comm, mul_comm], exact mul_dvd_mul_left _ (hxz.mul_right _) } end @[simp] lemma lcm_dvd_iff {x y z : R} : lcm x y ∣ z ↔ x ∣ z ∧ y ∣ z := ⟨λ hz, ⟨(dvd_lcm_left _ _).trans hz, (dvd_lcm_right _ _).trans hz⟩, λ ⟨hxz, hyz⟩, lcm_dvd hxz hyz⟩ @[simp] lemma lcm_zero_left (x : R) : lcm 0 x = 0 := by rw [lcm, zero_mul, zero_div] @[simp] lemma lcm_zero_right (x : R) : lcm x 0 = 0 := by rw [lcm, mul_zero, zero_div] @[simp] lemma lcm_eq_zero_iff {x y : R} : lcm x y = 0 ↔ x = 0 ∨ y = 0 := begin split, { intro hxy, rw [lcm, mul_div_assoc _ (gcd_dvd_right _ _), mul_eq_zero] at hxy, apply or_of_or_of_imp_right hxy, intro hy, by_cases hgxy : gcd x y = 0, { rw euclidean_domain.gcd_eq_zero_iff at hgxy, exact hgxy.2 }, { rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, generalize_hyp : gcd x y = g at hr hs hy hgxy ⊢, subst hs, rw [mul_div_cancel_left _ hgxy] at hy, rw [hy, mul_zero] } }, rintro (hx | hy), { rw [hx, lcm_zero_left] }, { rw [hy, lcm_zero_right] } end @[simp] lemma gcd_mul_lcm (x y : R) : gcd x y * lcm x y = x * y := begin rw lcm, by_cases h : gcd x y = 0, { rw [h, zero_mul], rw euclidean_domain.gcd_eq_zero_iff at h, rw [h.1, zero_mul] }, rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, generalize_hyp : gcd x y = g at h hr ⊢, subst hr, rw [mul_assoc, mul_div_cancel_left _ h] end end lcm section div lemma mul_div_mul_cancel {a b c : R} (ha : a ≠ 0) (hcb : c ∣ b) : a * b / (a * c) = b / c := begin by_cases hc : c = 0, { simp [hc] }, refine eq_div_of_mul_eq_right hc (mul_left_cancel₀ ha _), rw [← mul_assoc, ← mul_div_assoc _ (mul_dvd_mul_left a hcb), mul_div_cancel_left _ (mul_ne_zero ha hc)] end end div end euclidean_domain instance int.euclidean_domain : euclidean_domain ℤ := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), quotient_zero := int.div_zero, remainder := (%), quotient_mul_add_remainder_eq := λ a b, int.div_add_mod _ _, r := λ a b, a.nat_abs < b.nat_abs, r_well_founded := measure_wf (λ a, int.nat_abs a), remainder_lt := λ a b b0, int.coe_nat_lt.1 $ by { rw [int.nat_abs_of_nonneg (int.mod_nonneg _ b0), ← int.abs_eq_nat_abs], exact int.mod_lt _ b0 }, mul_left_not_lt := λ a b b0, not_lt_of_ge $ by {rw [← mul_one a.nat_abs, int.nat_abs_mul], exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) }, .. int.comm_ring, .. int.nontrivial } @[priority 100] -- see Note [lower instance priority] instance field.to_euclidean_domain {K : Type u} [field K] : euclidean_domain K := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), remainder := λ a b, a - a * b / b, quotient_zero := div_zero, quotient_mul_add_remainder_eq := λ a b, by { classical, by_cases b = 0; simp [h, mul_div_cancel'] }, r := λ a b, a = 0 ∧ b ≠ 0, r_well_founded := well_founded.intro $ λ a, acc.intro _ $ λ b ⟨hb, hna⟩, acc.intro _ $ λ c ⟨hc, hnb⟩, false.elim $ hnb hb, remainder_lt := λ a b hnb, by simp [hnb], mul_left_not_lt := λ a b hnb ⟨hab, hna⟩, or.cases_on (mul_eq_zero.1 hab) hna hnb, .. ‹field K› }
168b082fe0eae81c111dc9d412fb472afb623080
d1bbf1801b3dcb214451d48214589f511061da63
/src/analysis/asymptotics.lean
11ab57b5eaf1cd73b675b8b41fe3ad1f44d2e81c
[ "Apache-2.0" ]
permissive
cheraghchi/mathlib
5c366f8c4f8e66973b60c37881889da8390cab86
f29d1c3038422168fbbdb2526abf7c0ff13e86db
refs/heads/master
1,676,577,831,283
1,610,894,638,000
1,610,894,638,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
53,792
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Yury Kudryashov -/ import analysis.normed_space.basic import topology.local_homeomorph /-! # Asymptotics We introduce these relations: * `is_O_with c f g l` : "f is big O of g along l with constant c"; * `is_O f g l` : "f is big O of g along l"; * `is_o f g l` : "f is little o of g along l". Here `l` is any filter on the domain of `f` and `g`, which are assumed to be the same. The codomains of `f` and `g` do not need to be the same; all that is needed that there is a norm associated with these types, and it is the norm that is compared asymptotically. The relation `is_O_with c` is introduced to factor out common algebraic arguments in the proofs of similar properties of `is_O` and `is_o`. Usually proofs outside of this file should use `is_O` instead. Often the ranges of `f` and `g` will be the real numbers, in which case the norm is the absolute value. In general, we have `is_O f g l ↔ is_O (λ x, ∥f x∥) (λ x, ∥g x∥) l`, and similarly for `is_o`. But our setup allows us to use the notions e.g. with functions to the integers, rationals, complex numbers, or any normed vector space without mentioning the norm explicitly. If `f` and `g` are functions to a normed field like the reals or complex numbers and `g` is always nonzero, we have `is_o f g l ↔ tendsto (λ x, f x / (g x)) l (𝓝 0)`. In fact, the right-to-left direction holds without the hypothesis on `g`, and in the other direction it suffices to assume that `f` is zero wherever `g` is. (This generalization is useful in defining the Fréchet derivative.) -/ open filter set open_locale topological_space big_operators classical namespace asymptotics variables {α : Type*} {β : Type*} {E : Type*} {F : Type*} {G : Type*} {E' : Type*} {F' : Type*} {G' : Type*} {R : Type*} {R' : Type*} {𝕜 : Type*} {𝕜' : Type*} variables [has_norm E] [has_norm F] [has_norm G] [normed_group E'] [normed_group F'] [normed_group G'] [normed_ring R] [normed_ring R'] [normed_field 𝕜] [normed_field 𝕜'] {c c' : ℝ} {f : α → E} {g : α → F} {k : α → G} {f' : α → E'} {g' : α → F'} {k' : α → G'} {l l' : filter α} section defs /-! ### Definitions -/ /-- This version of the Landau notation `is_O_with C f g l` where `f` and `g` are two functions on a type `α` and `l` is a filter on `α`, means that eventually for `l`, `∥f∥` is bounded by `C * ∥g∥`. In other words, `∥f∥ / ∥g∥` is eventually bounded by `C`, modulo division by zero issues that are avoided by this definition. Probably you want to use `is_O` instead of this relation. -/ def is_O_with (c : ℝ) (f : α → E) (g : α → F) (l : filter α) : Prop := ∀ᶠ x in l, ∥ f x ∥ ≤ c * ∥ g x ∥ /-- Definition of `is_O_with`. We record it in a lemma as we will set `is_O_with` to be irreducible at the end of this file. -/ lemma is_O_with_iff {c : ℝ} {f : α → E} {g : α → F} {l : filter α} : is_O_with c f g l ↔ ∀ᶠ x in l, ∥ f x ∥ ≤ c * ∥ g x ∥ := iff.rfl lemma is_O_with.of_bound {c : ℝ} {f : α → E} {g : α → F} {l : filter α} (h : ∀ᶠ x in l, ∥ f x ∥ ≤ c * ∥ g x ∥) : is_O_with c f g l := h /-- The Landau notation `is_O f g l` where `f` and `g` are two functions on a type `α` and `l` is a filter on `α`, means that eventually for `l`, `∥f∥` is bounded by a constant multiple of `∥g∥`. In other words, `∥f∥ / ∥g∥` is eventually bounded, modulo division by zero issues that are avoided by this definition. -/ def is_O (f : α → E) (g : α → F) (l : filter α) : Prop := ∃ c : ℝ, is_O_with c f g l /-- Definition of `is_O` in terms of `is_O_with`. We record it in a lemma as we will set `is_O` to be irreducible at the end of this file. -/ lemma is_O_iff_is_O_with {f : α → E} {g : α → F} {l : filter α} : is_O f g l ↔ ∃ c : ℝ, is_O_with c f g l := iff.rfl /-- Definition of `is_O` in terms of filters. We record it in a lemma as we will set `is_O` to be irreducible at the end of this file. -/ lemma is_O_iff {f : α → E} {g : α → F} {l : filter α} : is_O f g l ↔ ∃ c : ℝ, ∀ᶠ x in l, ∥ f x ∥ ≤ c * ∥ g x ∥ := iff.rfl lemma is_O.of_bound (c : ℝ) {f : α → E} {g : α → F} {l : filter α} (h : ∀ᶠ x in l, ∥ f x ∥ ≤ c * ∥ g x ∥) : is_O f g l := ⟨c, h⟩ /-- The Landau notation `is_o f g l` where `f` and `g` are two functions on a type `α` and `l` is a filter on `α`, means that eventually for `l`, `∥f∥` is bounded by an arbitrarily small constant multiple of `∥g∥`. In other words, `∥f∥ / ∥g∥` tends to `0` along `l`, modulo division by zero issues that are avoided by this definition. -/ def is_o (f : α → E) (g : α → F) (l : filter α) : Prop := ∀ ⦃c : ℝ⦄, 0 < c → is_O_with c f g l /-- Definition of `is_o` in terms of `is_O_with`. We record it in a lemma as we will set `is_o` to be irreducible at the end of this file. -/ lemma is_o_iff_forall_is_O_with {f : α → E} {g : α → F} {l : filter α} : is_o f g l ↔ ∀ ⦃c : ℝ⦄, 0 < c → is_O_with c f g l := iff.rfl /-- Definition of `is_o` in terms of filters. We record it in a lemma as we will set `is_o` to be irreducible at the end of this file. -/ lemma is_o_iff {f : α → E} {g : α → F} {l : filter α} : is_o f g l ↔ ∀ ⦃c : ℝ⦄, 0 < c → ∀ᶠ x in l, ∥ f x ∥ ≤ c * ∥ g x ∥ := iff.rfl lemma is_o.def {f : α → E} {g : α → F} {l : filter α} (h : is_o f g l) {c : ℝ} (hc : 0 < c) : ∀ᶠ x in l, ∥ f x ∥ ≤ c * ∥ g x ∥ := h hc lemma is_o.def' {f : α → E} {g : α → F} {l : filter α} (h : is_o f g l) {c : ℝ} (hc : 0 < c) : is_O_with c f g l := h hc end defs /-! ### Conversions -/ theorem is_O_with.is_O (h : is_O_with c f g l) : is_O f g l := ⟨c, h⟩ theorem is_o.is_O_with (hgf : is_o f g l) : is_O_with 1 f g l := hgf zero_lt_one theorem is_o.is_O (hgf : is_o f g l) : is_O f g l := hgf.is_O_with.is_O theorem is_O_with.weaken (h : is_O_with c f g' l) (hc : c ≤ c') : is_O_with c' f g' l := mem_sets_of_superset h $ λ x hx, calc ∥f x∥ ≤ c * ∥g' x∥ : hx ... ≤ _ : mul_le_mul_of_nonneg_right hc (norm_nonneg _) theorem is_O_with.exists_pos (h : is_O_with c f g' l) : ∃ c' (H : 0 < c'), is_O_with c' f g' l := ⟨max c 1, lt_of_lt_of_le zero_lt_one (le_max_right c 1), h.weaken $ le_max_left c 1⟩ theorem is_O.exists_pos (h : is_O f g' l) : ∃ c (H : 0 < c), is_O_with c f g' l := let ⟨c, hc⟩ := h in hc.exists_pos theorem is_O_with.exists_nonneg (h : is_O_with c f g' l) : ∃ c' (H : 0 ≤ c'), is_O_with c' f g' l := let ⟨c, cpos, hc⟩ := h.exists_pos in ⟨c, le_of_lt cpos, hc⟩ theorem is_O.exists_nonneg (h : is_O f g' l) : ∃ c (H : 0 ≤ c), is_O_with c f g' l := let ⟨c, hc⟩ := h in hc.exists_nonneg /-! ### Subsingleton -/ @[nontriviality] lemma is_o_of_subsingleton [subsingleton E'] : is_o f' g' l := λ c hc, is_O_with.of_bound $ by simp [subsingleton.elim (f' _) 0, mul_nonneg hc.le] @[nontriviality] lemma is_O_of_subsingleton [subsingleton E'] : is_O f' g' l := is_o_of_subsingleton.is_O /-! ### Congruence -/ theorem is_O_with_congr {c₁ c₂} {f₁ f₂ : α → E} {g₁ g₂ : α → F} {l : filter α} (hc : c₁ = c₂) (hf : f₁ =ᶠ[l] f₂) (hg : g₁ =ᶠ[l] g₂) : is_O_with c₁ f₁ g₁ l ↔ is_O_with c₂ f₂ g₂ l := begin subst c₂, apply filter.congr_sets, filter_upwards [hf, hg], assume x e₁ e₂, rw [e₁, e₂] end theorem is_O_with.congr' {c₁ c₂} {f₁ f₂ : α → E} {g₁ g₂ : α → F} {l : filter α} (hc : c₁ = c₂) (hf : f₁ =ᶠ[l] f₂) (hg : g₁ =ᶠ[l] g₂) : is_O_with c₁ f₁ g₁ l → is_O_with c₂ f₂ g₂ l := (is_O_with_congr hc hf hg).mp theorem is_O_with.congr {c₁ c₂} {f₁ f₂ : α → E} {g₁ g₂ : α → F} {l : filter α} (hc : c₁ = c₂) (hf : ∀ x, f₁ x = f₂ x) (hg : ∀ x, g₁ x = g₂ x) : is_O_with c₁ f₁ g₁ l → is_O_with c₂ f₂ g₂ l := λ h, h.congr' hc (univ_mem_sets' hf) (univ_mem_sets' hg) theorem is_O_with.congr_left {f₁ f₂ : α → E} {l : filter α} (hf : ∀ x, f₁ x = f₂ x) : is_O_with c f₁ g l → is_O_with c f₂ g l := is_O_with.congr rfl hf (λ _, rfl) theorem is_O_with.congr_right {g₁ g₂ : α → F} {l : filter α} (hg : ∀ x, g₁ x = g₂ x) : is_O_with c f g₁ l → is_O_with c f g₂ l := is_O_with.congr rfl (λ _, rfl) hg theorem is_O_with.congr_const {c₁ c₂} {l : filter α} (hc : c₁ = c₂) : is_O_with c₁ f g l → is_O_with c₂ f g l := is_O_with.congr hc (λ _, rfl) (λ _, rfl) theorem is_O_congr {f₁ f₂ : α → E} {g₁ g₂ : α → F} {l : filter α} (hf : f₁ =ᶠ[l] f₂) (hg : g₁ =ᶠ[l] g₂) : is_O f₁ g₁ l ↔ is_O f₂ g₂ l := exists_congr $ λ c, is_O_with_congr rfl hf hg theorem is_O.congr' {f₁ f₂ : α → E} {g₁ g₂ : α → F} {l : filter α} (hf : f₁ =ᶠ[l] f₂) (hg : g₁ =ᶠ[l] g₂) : is_O f₁ g₁ l → is_O f₂ g₂ l := (is_O_congr hf hg).mp theorem is_O.congr {f₁ f₂ : α → E} {g₁ g₂ : α → F} {l : filter α} (hf : ∀ x, f₁ x = f₂ x) (hg : ∀ x, g₁ x = g₂ x) : is_O f₁ g₁ l → is_O f₂ g₂ l := λ h, h.congr' (univ_mem_sets' hf) (univ_mem_sets' hg) theorem is_O.congr_left {f₁ f₂ : α → E} {l : filter α} (hf : ∀ x, f₁ x = f₂ x) : is_O f₁ g l → is_O f₂ g l := is_O.congr hf (λ _, rfl) theorem is_O.congr_right {g₁ g₂ : α → E} {l : filter α} (hg : ∀ x, g₁ x = g₂ x) : is_O f g₁ l → is_O f g₂ l := is_O.congr (λ _, rfl) hg theorem is_o_congr {f₁ f₂ : α → E} {g₁ g₂ : α → F} {l : filter α} (hf : f₁ =ᶠ[l] f₂) (hg : g₁ =ᶠ[l] g₂) : is_o f₁ g₁ l ↔ is_o f₂ g₂ l := ball_congr (λ c hc, is_O_with_congr (eq.refl c) hf hg) theorem is_o.congr' {f₁ f₂ : α → E} {g₁ g₂ : α → F} {l : filter α} (hf : f₁ =ᶠ[l] f₂) (hg : g₁ =ᶠ[l] g₂) : is_o f₁ g₁ l → is_o f₂ g₂ l := (is_o_congr hf hg).mp theorem is_o.congr {f₁ f₂ : α → E} {g₁ g₂ : α → F} {l : filter α} (hf : ∀ x, f₁ x = f₂ x) (hg : ∀ x, g₁ x = g₂ x) : is_o f₁ g₁ l → is_o f₂ g₂ l := λ h, h.congr' (univ_mem_sets' hf) (univ_mem_sets' hg) theorem is_o.congr_left {f₁ f₂ : α → E} {l : filter α} (hf : ∀ x, f₁ x = f₂ x) : is_o f₁ g l → is_o f₂ g l := is_o.congr hf (λ _, rfl) theorem is_o.congr_right {g₁ g₂ : α → E} {l : filter α} (hg : ∀ x, g₁ x = g₂ x) : is_o f g₁ l → is_o f g₂ l := is_o.congr (λ _, rfl) hg /-! ### Filter operations and transitivity -/ theorem is_O_with.comp_tendsto (hcfg : is_O_with c f g l) {k : β → α} {l' : filter β} (hk : tendsto k l' l): is_O_with c (f ∘ k) (g ∘ k) l' := hk hcfg theorem is_O.comp_tendsto (hfg : is_O f g l) {k : β → α} {l' : filter β} (hk : tendsto k l' l) : is_O (f ∘ k) (g ∘ k) l' := hfg.imp (λ c h, h.comp_tendsto hk) theorem is_o.comp_tendsto (hfg : is_o f g l) {k : β → α} {l' : filter β} (hk : tendsto k l' l) : is_o (f ∘ k) (g ∘ k) l' := λ c cpos, (hfg cpos).comp_tendsto hk @[simp] theorem is_O_with_map {k : β → α} {l : filter β} : is_O_with c f g (map k l) ↔ is_O_with c (f ∘ k) (g ∘ k) l := mem_map @[simp] theorem is_O_map {k : β → α} {l : filter β} : is_O f g (map k l) ↔ is_O (f ∘ k) (g ∘ k) l := by simp only [is_O, is_O_with_map] @[simp] theorem is_o_map {k : β → α} {l : filter β} : is_o f g (map k l) ↔ is_o (f ∘ k) (g ∘ k) l := by simp only [is_o, is_O_with_map] theorem is_O_with.mono (h : is_O_with c f g l') (hl : l ≤ l') : is_O_with c f g l := hl h theorem is_O.mono (h : is_O f g l') (hl : l ≤ l') : is_O f g l := h.imp (λ c h, h.mono hl) theorem is_o.mono (h : is_o f g l') (hl : l ≤ l') : is_o f g l := λ c cpos, (h cpos).mono hl theorem is_O_with.trans (hfg : is_O_with c f g l) (hgk : is_O_with c' g k l) (hc : 0 ≤ c) : is_O_with (c * c') f k l := begin filter_upwards [hfg, hgk], assume x hx hx', calc ∥f x∥ ≤ c * ∥g x∥ : hx ... ≤ c * (c' * ∥k x∥) : mul_le_mul_of_nonneg_left hx' hc ... = c * c' * ∥k x∥ : (mul_assoc _ _ _).symm end theorem is_O.trans (hfg : is_O f g' l) (hgk : is_O g' k l) : is_O f k l := let ⟨c, cnonneg, hc⟩ := hfg.exists_nonneg, ⟨c', hc'⟩ := hgk in (hc.trans hc' cnonneg).is_O theorem is_o.trans_is_O_with (hfg : is_o f g l) (hgk : is_O_with c g k l) (hc : 0 < c) : is_o f k l := begin intros c' c'pos, have : 0 < c' / c, from div_pos c'pos hc, exact ((hfg this).trans hgk (le_of_lt this)).congr_const (div_mul_cancel _ (ne_of_gt hc)) end theorem is_o.trans_is_O (hfg : is_o f g l) (hgk : is_O g k' l) : is_o f k' l := let ⟨c, cpos, hc⟩ := hgk.exists_pos in hfg.trans_is_O_with hc cpos theorem is_O_with.trans_is_o (hfg : is_O_with c f g l) (hgk : is_o g k l) (hc : 0 < c) : is_o f k l := begin intros c' c'pos, have : 0 < c' / c, from div_pos c'pos hc, exact (hfg.trans (hgk this) (le_of_lt hc)).congr_const (mul_div_cancel' _ (ne_of_gt hc)) end theorem is_O.trans_is_o (hfg : is_O f g' l) (hgk : is_o g' k l) : is_o f k l := let ⟨c, cpos, hc⟩ := hfg.exists_pos in hc.trans_is_o hgk cpos theorem is_o.trans (hfg : is_o f g l) (hgk : is_o g k' l) : is_o f k' l := hfg.trans_is_O hgk.is_O theorem is_o.trans' (hfg : is_o f g' l) (hgk : is_o g' k l) : is_o f k l := hfg.is_O.trans_is_o hgk section variable (l) theorem is_O_with_of_le' (hfg : ∀ x, ∥f x∥ ≤ c * ∥g x∥) : is_O_with c f g l := univ_mem_sets' hfg theorem is_O_with_of_le (hfg : ∀ x, ∥f x∥ ≤ ∥g x∥) : is_O_with 1 f g l := is_O_with_of_le' l $ λ x, by { rw one_mul, exact hfg x } theorem is_O_of_le' (hfg : ∀ x, ∥f x∥ ≤ c * ∥g x∥) : is_O f g l := (is_O_with_of_le' l hfg).is_O theorem is_O_of_le (hfg : ∀ x, ∥f x∥ ≤ ∥g x∥) : is_O f g l := (is_O_with_of_le l hfg).is_O end theorem is_O_with_refl (f : α → E) (l : filter α) : is_O_with 1 f f l := is_O_with_of_le l $ λ _, le_refl _ theorem is_O_refl (f : α → E) (l : filter α) : is_O f f l := (is_O_with_refl f l).is_O theorem is_O_with.trans_le (hfg : is_O_with c f g l) (hgk : ∀ x, ∥g x∥ ≤ ∥k x∥) (hc : 0 ≤ c) : is_O_with c f k l := (hfg.trans (is_O_with_of_le l hgk) hc).congr_const $ mul_one c theorem is_O.trans_le (hfg : is_O f g' l) (hgk : ∀ x, ∥g' x∥ ≤ ∥k x∥) : is_O f k l := hfg.trans (is_O_of_le l hgk) theorem is_o.trans_le (hfg : is_o f g l) (hgk : ∀ x, ∥g x∥ ≤ ∥k x∥) : is_o f k l := hfg.trans_is_O_with (is_O_with_of_le _ hgk) zero_lt_one section bot variables (c f g) theorem is_O_with_bot : is_O_with c f g ⊥ := trivial theorem is_O_bot : is_O f g ⊥ := (is_O_with_bot c f g).is_O theorem is_o_bot : is_o f g ⊥ := λ c _, is_O_with_bot c f g end bot theorem is_O_with.join (h : is_O_with c f g l) (h' : is_O_with c f g l') : is_O_with c f g (l ⊔ l') := mem_sup_sets.2 ⟨h, h'⟩ theorem is_O_with.join' (h : is_O_with c f g' l) (h' : is_O_with c' f g' l') : is_O_with (max c c') f g' (l ⊔ l') := mem_sup_sets.2 ⟨(h.weaken $ le_max_left c c'), (h'.weaken $ le_max_right c c')⟩ theorem is_O.join (h : is_O f g' l) (h' : is_O f g' l') : is_O f g' (l ⊔ l') := let ⟨c, hc⟩ := h, ⟨c', hc'⟩ := h' in (hc.join' hc').is_O theorem is_o.join (h : is_o f g l) (h' : is_o f g l') : is_o f g (l ⊔ l') := λ c cpos, (h cpos).join (h' cpos) /-! ### Simplification : norm -/ @[simp] theorem is_O_with_norm_right : is_O_with c f (λ x, ∥g' x∥) l ↔ is_O_with c f g' l := by simp only [is_O_with, norm_norm] alias is_O_with_norm_right ↔ asymptotics.is_O_with.of_norm_right asymptotics.is_O_with.norm_right @[simp] theorem is_O_norm_right : is_O f (λ x, ∥g' x∥) l ↔ is_O f g' l := exists_congr $ λ _, is_O_with_norm_right alias is_O_norm_right ↔ asymptotics.is_O.of_norm_right asymptotics.is_O.norm_right @[simp] theorem is_o_norm_right : is_o f (λ x, ∥g' x∥) l ↔ is_o f g' l := forall_congr $ λ _, forall_congr $ λ _, is_O_with_norm_right alias is_o_norm_right ↔ asymptotics.is_o.of_norm_right asymptotics.is_o.norm_right @[simp] theorem is_O_with_norm_left : is_O_with c (λ x, ∥f' x∥) g l ↔ is_O_with c f' g l := by simp only [is_O_with, norm_norm] alias is_O_with_norm_left ↔ asymptotics.is_O_with.of_norm_left asymptotics.is_O_with.norm_left @[simp] theorem is_O_norm_left : is_O (λ x, ∥f' x∥) g l ↔ is_O f' g l := exists_congr $ λ _, is_O_with_norm_left alias is_O_norm_left ↔ asymptotics.is_O.of_norm_left asymptotics.is_O.norm_left @[simp] theorem is_o_norm_left : is_o (λ x, ∥f' x∥) g l ↔ is_o f' g l := forall_congr $ λ _, forall_congr $ λ _, is_O_with_norm_left alias is_o_norm_left ↔ asymptotics.is_o.of_norm_left asymptotics.is_o.norm_left theorem is_O_with_norm_norm : is_O_with c (λ x, ∥f' x∥) (λ x, ∥g' x∥) l ↔ is_O_with c f' g' l := is_O_with_norm_left.trans is_O_with_norm_right alias is_O_with_norm_norm ↔ asymptotics.is_O_with.of_norm_norm asymptotics.is_O_with.norm_norm theorem is_O_norm_norm : is_O (λ x, ∥f' x∥) (λ x, ∥g' x∥) l ↔ is_O f' g' l := is_O_norm_left.trans is_O_norm_right alias is_O_norm_norm ↔ asymptotics.is_O.of_norm_norm asymptotics.is_O.norm_norm theorem is_o_norm_norm : is_o (λ x, ∥f' x∥) (λ x, ∥g' x∥) l ↔ is_o f' g' l := is_o_norm_left.trans is_o_norm_right alias is_o_norm_norm ↔ asymptotics.is_o.of_norm_norm asymptotics.is_o.norm_norm /-! ### Simplification: negate -/ @[simp] theorem is_O_with_neg_right : is_O_with c f (λ x, -(g' x)) l ↔ is_O_with c f g' l := by simp only [is_O_with, norm_neg] alias is_O_with_neg_right ↔ asymptotics.is_O_with.of_neg_right asymptotics.is_O_with.neg_right @[simp] theorem is_O_neg_right : is_O f (λ x, -(g' x)) l ↔ is_O f g' l := exists_congr $ λ _, is_O_with_neg_right alias is_O_neg_right ↔ asymptotics.is_O.of_neg_right asymptotics.is_O.neg_right @[simp] theorem is_o_neg_right : is_o f (λ x, -(g' x)) l ↔ is_o f g' l := forall_congr $ λ _, forall_congr $ λ _, is_O_with_neg_right alias is_o_neg_right ↔ asymptotics.is_o.of_neg_right asymptotics.is_o.neg_right @[simp] theorem is_O_with_neg_left : is_O_with c (λ x, -(f' x)) g l ↔ is_O_with c f' g l := by simp only [is_O_with, norm_neg] alias is_O_with_neg_left ↔ asymptotics.is_O_with.of_neg_left asymptotics.is_O_with.neg_left @[simp] theorem is_O_neg_left : is_O (λ x, -(f' x)) g l ↔ is_O f' g l := exists_congr $ λ _, is_O_with_neg_left alias is_O_neg_left ↔ asymptotics.is_O.of_neg_left asymptotics.is_O.neg_left @[simp] theorem is_o_neg_left : is_o (λ x, -(f' x)) g l ↔ is_o f' g l := forall_congr $ λ _, forall_congr $ λ _, is_O_with_neg_left alias is_o_neg_left ↔ asymptotics.is_o.of_neg_right asymptotics.is_o.neg_left /-! ### Product of functions (right) -/ lemma is_O_with_fst_prod : is_O_with 1 f' (λ x, (f' x, g' x)) l := is_O_with_of_le l $ λ x, le_max_left _ _ lemma is_O_with_snd_prod : is_O_with 1 g' (λ x, (f' x, g' x)) l := is_O_with_of_le l $ λ x, le_max_right _ _ lemma is_O_fst_prod : is_O f' (λ x, (f' x, g' x)) l := is_O_with_fst_prod.is_O lemma is_O_snd_prod : is_O g' (λ x, (f' x, g' x)) l := is_O_with_snd_prod.is_O lemma is_O_fst_prod' {f' : α → E' × F'} : is_O (λ x, (f' x).1) f' l := is_O_fst_prod lemma is_O_snd_prod' {f' : α → E' × F'} : is_O (λ x, (f' x).2) f' l := is_O_snd_prod section variables (f' k') lemma is_O_with.prod_rightl (h : is_O_with c f g' l) (hc : 0 ≤ c) : is_O_with c f (λ x, (g' x, k' x)) l := (h.trans is_O_with_fst_prod hc).congr_const (mul_one c) lemma is_O.prod_rightl (h : is_O f g' l) : is_O f (λx, (g' x, k' x)) l := let ⟨c, cnonneg, hc⟩ := h.exists_nonneg in (hc.prod_rightl k' cnonneg).is_O lemma is_o.prod_rightl (h : is_o f g' l) : is_o f (λ x, (g' x, k' x)) l := λ c cpos, (h cpos).prod_rightl k' (le_of_lt cpos) lemma is_O_with.prod_rightr (h : is_O_with c f g' l) (hc : 0 ≤ c) : is_O_with c f (λ x, (f' x, g' x)) l := (h.trans is_O_with_snd_prod hc).congr_const (mul_one c) lemma is_O.prod_rightr (h : is_O f g' l) : is_O f (λx, (f' x, g' x)) l := let ⟨c, cnonneg, hc⟩ := h.exists_nonneg in (hc.prod_rightr f' cnonneg).is_O lemma is_o.prod_rightr (h : is_o f g' l) : is_o f (λx, (f' x, g' x)) l := λ c cpos, (h cpos).prod_rightr f' (le_of_lt cpos) end lemma is_O_with.prod_left_same (hf : is_O_with c f' k' l) (hg : is_O_with c g' k' l) : is_O_with c (λ x, (f' x, g' x)) k' l := by filter_upwards [hf, hg] λ x, max_le lemma is_O_with.prod_left (hf : is_O_with c f' k' l) (hg : is_O_with c' g' k' l) : is_O_with (max c c') (λ x, (f' x, g' x)) k' l := (hf.weaken $ le_max_left c c').prod_left_same (hg.weaken $ le_max_right c c') lemma is_O_with.prod_left_fst (h : is_O_with c (λ x, (f' x, g' x)) k' l) : is_O_with c f' k' l := (is_O_with_fst_prod.trans h zero_le_one).congr_const $ one_mul c lemma is_O_with.prod_left_snd (h : is_O_with c (λ x, (f' x, g' x)) k' l) : is_O_with c g' k' l := (is_O_with_snd_prod.trans h zero_le_one).congr_const $ one_mul c lemma is_O_with_prod_left : is_O_with c (λ x, (f' x, g' x)) k' l ↔ is_O_with c f' k' l ∧ is_O_with c g' k' l := ⟨λ h, ⟨h.prod_left_fst, h.prod_left_snd⟩, λ h, h.1.prod_left_same h.2⟩ lemma is_O.prod_left (hf : is_O f' k' l) (hg : is_O g' k' l) : is_O (λ x, (f' x, g' x)) k' l := let ⟨c, hf⟩ := hf, ⟨c', hg⟩ := hg in (hf.prod_left hg).is_O lemma is_O.prod_left_fst (h : is_O (λ x, (f' x, g' x)) k' l) : is_O f' k' l := is_O_fst_prod.trans h lemma is_O.prod_left_snd (h : is_O (λ x, (f' x, g' x)) k' l) : is_O g' k' l := is_O_snd_prod.trans h @[simp] lemma is_O_prod_left : is_O (λ x, (f' x, g' x)) k' l ↔ is_O f' k' l ∧ is_O g' k' l := ⟨λ h, ⟨h.prod_left_fst, h.prod_left_snd⟩, λ h, h.1.prod_left h.2⟩ lemma is_o.prod_left (hf : is_o f' k' l) (hg : is_o g' k' l) : is_o (λ x, (f' x, g' x)) k' l := λ c hc, (hf hc).prod_left_same (hg hc) lemma is_o.prod_left_fst (h : is_o (λ x, (f' x, g' x)) k' l) : is_o f' k' l := is_O_fst_prod.trans_is_o h lemma is_o.prod_left_snd (h : is_o (λ x, (f' x, g' x)) k' l) : is_o g' k' l := is_O_snd_prod.trans_is_o h @[simp] lemma is_o_prod_left : is_o (λ x, (f' x, g' x)) k' l ↔ is_o f' k' l ∧ is_o g' k' l := ⟨λ h, ⟨h.prod_left_fst, h.prod_left_snd⟩, λ h, h.1.prod_left h.2⟩ lemma is_O_with.eq_zero_imp (h : is_O_with c f' g' l) : ∀ᶠ x in l, g' x = 0 → f' x = 0 := eventually.mono h $ λ x hx hg, norm_le_zero_iff.1 $ by simpa [hg] using hx lemma is_O.eq_zero_imp (h : is_O f' g' l) : ∀ᶠ x in l, g' x = 0 → f' x = 0 := let ⟨C, hC⟩ := h in hC.eq_zero_imp /-! ### Addition and subtraction -/ section add_sub variables {c₁ c₂ : ℝ} {f₁ f₂ : α → E'} theorem is_O_with.add (h₁ : is_O_with c₁ f₁ g l) (h₂ : is_O_with c₂ f₂ g l) : is_O_with (c₁ + c₂) (λ x, f₁ x + f₂ x) g l := by filter_upwards [h₁, h₂] λ x hx₁ hx₂, calc ∥f₁ x + f₂ x∥ ≤ c₁ * ∥g x∥ + c₂ * ∥g x∥ : norm_add_le_of_le hx₁ hx₂ ... = (c₁ + c₂) * ∥g x∥ : (add_mul _ _ _).symm theorem is_O.add : is_O f₁ g l → is_O f₂ g l → is_O (λ x, f₁ x + f₂ x) g l | ⟨c₁, hc₁⟩ ⟨c₂, hc₂⟩ := (hc₁.add hc₂).is_O theorem is_o.add (h₁ : is_o f₁ g l) (h₂ : is_o f₂ g l) : is_o (λ x, f₁ x + f₂ x) g l := λ c cpos, ((h₁ $ half_pos cpos).add (h₂ $ half_pos cpos)).congr_const (add_halves c) theorem is_o.add_add {g₁ g₂ : α → F'} (h₁ : is_o f₁ g₁ l) (h₂ : is_o f₂ g₂ l) : is_o (λ x, f₁ x + f₂ x) (λ x, ∥g₁ x∥ + ∥g₂ x∥) l := by refine (h₁.trans_le $ λ x, _).add (h₂.trans_le _); simp [real.norm_eq_abs, abs_of_nonneg, add_nonneg] theorem is_O.add_is_o (h₁ : is_O f₁ g l) (h₂ : is_o f₂ g l) : is_O (λ x, f₁ x + f₂ x) g l := h₁.add h₂.is_O theorem is_o.add_is_O (h₁ : is_o f₁ g l) (h₂ : is_O f₂ g l) : is_O (λ x, f₁ x + f₂ x) g l := h₁.is_O.add h₂ theorem is_O_with.add_is_o (h₁ : is_O_with c₁ f₁ g l) (h₂ : is_o f₂ g l) (hc : c₁ < c₂) : is_O_with c₂ (λx, f₁ x + f₂ x) g l := (h₁.add (h₂ (sub_pos.2 hc))).congr_const (add_sub_cancel'_right _ _) theorem is_o.add_is_O_with (h₁ : is_o f₁ g l) (h₂ : is_O_with c₁ f₂ g l) (hc : c₁ < c₂) : is_O_with c₂ (λx, f₁ x + f₂ x) g l := (h₂.add_is_o h₁ hc).congr_left $ λ _, add_comm _ _ theorem is_O_with.sub (h₁ : is_O_with c₁ f₁ g l) (h₂ : is_O_with c₂ f₂ g l) : is_O_with (c₁ + c₂) (λ x, f₁ x - f₂ x) g l := by simpa only [sub_eq_add_neg] using h₁.add h₂.neg_left theorem is_O_with.sub_is_o (h₁ : is_O_with c₁ f₁ g l) (h₂ : is_o f₂ g l) (hc : c₁ < c₂) : is_O_with c₂ (λ x, f₁ x - f₂ x) g l := by simpa only [sub_eq_add_neg] using h₁.add_is_o h₂.neg_left hc theorem is_O.sub (h₁ : is_O f₁ g l) (h₂ : is_O f₂ g l) : is_O (λ x, f₁ x - f₂ x) g l := by simpa only [sub_eq_add_neg] using h₁.add h₂.neg_left theorem is_o.sub (h₁ : is_o f₁ g l) (h₂ : is_o f₂ g l) : is_o (λ x, f₁ x - f₂ x) g l := by simpa only [sub_eq_add_neg] using h₁.add h₂.neg_left end add_sub /-! ### Lemmas about `is_O (f₁ - f₂) g l` / `is_o (f₁ - f₂) g l` treated as a binary relation -/ section is_oO_as_rel variables {f₁ f₂ f₃ : α → E'} theorem is_O_with.symm (h : is_O_with c (λ x, f₁ x - f₂ x) g l) : is_O_with c (λ x, f₂ x - f₁ x) g l := h.neg_left.congr_left $ λ x, neg_sub _ _ theorem is_O_with_comm : is_O_with c (λ x, f₁ x - f₂ x) g l ↔ is_O_with c (λ x, f₂ x - f₁ x) g l := ⟨is_O_with.symm, is_O_with.symm⟩ theorem is_O.symm (h : is_O (λ x, f₁ x - f₂ x) g l) : is_O (λ x, f₂ x - f₁ x) g l := h.neg_left.congr_left $ λ x, neg_sub _ _ theorem is_O_comm : is_O (λ x, f₁ x - f₂ x) g l ↔ is_O (λ x, f₂ x - f₁ x) g l := ⟨is_O.symm, is_O.symm⟩ theorem is_o.symm (h : is_o (λ x, f₁ x - f₂ x) g l) : is_o (λ x, f₂ x - f₁ x) g l := by simpa only [neg_sub] using h.neg_left theorem is_o_comm : is_o (λ x, f₁ x - f₂ x) g l ↔ is_o (λ x, f₂ x - f₁ x) g l := ⟨is_o.symm, is_o.symm⟩ theorem is_O_with.triangle (h₁ : is_O_with c (λ x, f₁ x - f₂ x) g l) (h₂ : is_O_with c' (λ x, f₂ x - f₃ x) g l) : is_O_with (c + c') (λ x, f₁ x - f₃ x) g l := (h₁.add h₂).congr_left $ λ x, sub_add_sub_cancel _ _ _ theorem is_O.triangle (h₁ : is_O (λ x, f₁ x - f₂ x) g l) (h₂ : is_O (λ x, f₂ x - f₃ x) g l) : is_O (λ x, f₁ x - f₃ x) g l := (h₁.add h₂).congr_left $ λ x, sub_add_sub_cancel _ _ _ theorem is_o.triangle (h₁ : is_o (λ x, f₁ x - f₂ x) g l) (h₂ : is_o (λ x, f₂ x - f₃ x) g l) : is_o (λ x, f₁ x - f₃ x) g l := (h₁.add h₂).congr_left $ λ x, sub_add_sub_cancel _ _ _ theorem is_O.congr_of_sub (h : is_O (λ x, f₁ x - f₂ x) g l) : is_O f₁ g l ↔ is_O f₂ g l := ⟨λ h', (h'.sub h).congr_left (λ x, sub_sub_cancel _ _), λ h', (h.add h').congr_left (λ x, sub_add_cancel _ _)⟩ theorem is_o.congr_of_sub (h : is_o (λ x, f₁ x - f₂ x) g l) : is_o f₁ g l ↔ is_o f₂ g l := ⟨λ h', (h'.sub h).congr_left (λ x, sub_sub_cancel _ _), λ h', (h.add h').congr_left (λ x, sub_add_cancel _ _)⟩ end is_oO_as_rel /-! ### Zero, one, and other constants -/ section zero_const variables (g g' l) theorem is_o_zero : is_o (λ x, (0 : E')) g' l := λ c hc, univ_mem_sets' $ λ x, by simpa using mul_nonneg (le_of_lt hc) (norm_nonneg $ g' x) theorem is_O_with_zero (hc : 0 ≤ c) : is_O_with c (λ x, (0 : E')) g' l := univ_mem_sets' $ λ x, by simpa using mul_nonneg hc (norm_nonneg $ g' x) theorem is_O_with_zero' : is_O_with 0 (λ x, (0 : E')) g l := univ_mem_sets' $ λ x, by simp theorem is_O_zero : is_O (λ x, (0 : E')) g l := ⟨0, is_O_with_zero' _ _⟩ theorem is_O_refl_left : is_O (λ x, f' x - f' x) g' l := (is_O_zero g' l).congr_left $ λ x, (sub_self _).symm theorem is_o_refl_left : is_o (λ x, f' x - f' x) g' l := (is_o_zero g' l).congr_left $ λ x, (sub_self _).symm variables {g g' l} @[simp] theorem is_O_with_zero_right_iff : is_O_with c f' (λ x, (0 : F')) l ↔ ∀ᶠ x in l, f' x = 0 := by simp only [is_O_with, exists_prop, true_and, norm_zero, mul_zero, norm_le_zero_iff] @[simp] theorem is_O_zero_right_iff : is_O f' (λ x, (0 : F')) l ↔ ∀ᶠ x in l, f' x = 0 := ⟨λ h, let ⟨c, hc⟩ := h in (is_O_with_zero_right_iff).1 hc, λ h, (is_O_with_zero_right_iff.2 h : is_O_with 1 _ _ _).is_O⟩ @[simp] theorem is_o_zero_right_iff : is_o f' (λ x, (0 : F')) l ↔ ∀ᶠ x in l, f' x = 0 := ⟨λ h, is_O_zero_right_iff.1 h.is_O, λ h c hc, is_O_with_zero_right_iff.2 h⟩ theorem is_O_with_const_const (c : E) {c' : F'} (hc' : c' ≠ 0) (l : filter α) : is_O_with (∥c∥ / ∥c'∥) (λ x : α, c) (λ x, c') l := begin apply univ_mem_sets', intro x, rw [mem_set_of_eq, div_mul_cancel], rwa [ne.def, norm_eq_zero] end theorem is_O_const_const (c : E) {c' : F'} (hc' : c' ≠ 0) (l : filter α) : is_O (λ x : α, c) (λ x, c') l := (is_O_with_const_const c hc' l).is_O end zero_const @[simp] lemma is_O_with_top : is_O_with c f g ⊤ ↔ ∀ x, ∥f x∥ ≤ c * ∥g x∥ := iff.rfl @[simp] lemma is_O_top : is_O f g ⊤ ↔ ∃ C, ∀ x, ∥f x∥ ≤ C * ∥g x∥ := iff.rfl @[simp] lemma is_o_top : is_o f' g' ⊤ ↔ ∀ x, f' x = 0 := begin refine ⟨_, λ h, (is_o_zero g' ⊤).congr (λ x, (h x).symm) (λ x, rfl)⟩, simp only [is_o_iff, eventually_top], refine λ h x, norm_le_zero_iff.1 _, have : tendsto (λ c : ℝ, c * ∥g' x∥) (𝓝[Ioi 0] 0) (𝓝 0) := ((continuous_id.mul continuous_const).tendsto' _ _ (zero_mul _)).mono_left inf_le_left, exact le_of_tendsto_of_tendsto tendsto_const_nhds this (eventually_nhds_within_iff.2 $ eventually_of_forall $ λ c hc, h hc x) end theorem is_O_with_const_one (c : E) (l : filter α) : is_O_with ∥c∥ (λ x : α, c) (λ x, (1 : 𝕜)) l := begin refine (is_O_with_const_const c _ l).congr_const _, { rw [norm_one, div_one] }, { exact one_ne_zero } end theorem is_O_const_one (c : E) (l : filter α) : is_O (λ x : α, c) (λ x, (1 : 𝕜)) l := (is_O_with_const_one c l).is_O section variable (𝕜) theorem is_o_const_iff_is_o_one {c : F'} (hc : c ≠ 0) : is_o f (λ x, c) l ↔ is_o f (λ x, (1:𝕜)) l := ⟨λ h, h.trans_is_O $ is_O_const_one c l, λ h, h.trans_is_O $ is_O_const_const _ hc _⟩ end theorem is_o_const_iff {c : F'} (hc : c ≠ 0) : is_o f' (λ x, c) l ↔ tendsto f' l (𝓝 0) := (is_o_const_iff_is_o_one ℝ hc).trans begin clear hc c, simp only [is_o, is_O_with, norm_one, mul_one, metric.nhds_basis_closed_ball.tendsto_right_iff, metric.mem_closed_ball, dist_zero_right] end lemma is_o_id_const {c : F'} (hc : c ≠ 0) : is_o (λ (x : E'), x) (λ x, c) (𝓝 0) := (is_o_const_iff hc).mpr (continuous_id.tendsto 0) theorem is_O_const_of_tendsto {y : E'} (h : tendsto f' l (𝓝 y)) {c : F'} (hc : c ≠ 0) : is_O f' (λ x, c) l := begin refine is_O.trans _ (is_O_const_const (∥y∥ + 1) hc l), use 1, simp only [is_O_with, one_mul], have : tendsto (λx, ∥f' x∥) l (𝓝 ∥y∥), from (continuous_norm.tendsto _).comp h, have Iy : ∥y∥ < ∥∥y∥ + 1∥, from lt_of_lt_of_le (lt_add_one _) (le_abs_self _), exact this (ge_mem_nhds Iy) end section variable (𝕜) theorem is_o_one_iff : is_o f' (λ x, (1 : 𝕜)) l ↔ tendsto f' l (𝓝 0) := is_o_const_iff one_ne_zero theorem is_O_one_of_tendsto {y : E'} (h : tendsto f' l (𝓝 y)) : is_O f' (λ x, (1:𝕜)) l := is_O_const_of_tendsto h one_ne_zero theorem is_O.trans_tendsto_nhds (hfg : is_O f g' l) {y : F'} (hg : tendsto g' l (𝓝 y)) : is_O f (λ x, (1:𝕜)) l := hfg.trans $ is_O_one_of_tendsto 𝕜 hg end theorem is_O.trans_tendsto (hfg : is_O f' g' l) (hg : tendsto g' l (𝓝 0)) : tendsto f' l (𝓝 0) := (is_o_one_iff ℝ).1 $ hfg.trans_is_o $ (is_o_one_iff ℝ).2 hg theorem is_o.trans_tendsto (hfg : is_o f' g' l) (hg : tendsto g' l (𝓝 0)) : tendsto f' l (𝓝 0) := hfg.is_O.trans_tendsto hg /-! ### Multiplication by a constant -/ theorem is_O_with_const_mul_self (c : R) (f : α → R) (l : filter α) : is_O_with ∥c∥ (λ x, c * f x) f l := is_O_with_of_le' _ $ λ x, norm_mul_le _ _ theorem is_O_const_mul_self (c : R) (f : α → R) (l : filter α) : is_O (λ x, c * f x) f l := (is_O_with_const_mul_self c f l).is_O theorem is_O_with.const_mul_left {f : α → R} (h : is_O_with c f g l) (c' : R) : is_O_with (∥c'∥ * c) (λ x, c' * f x) g l := (is_O_with_const_mul_self c' f l).trans h (norm_nonneg c') theorem is_O.const_mul_left {f : α → R} (h : is_O f g l) (c' : R) : is_O (λ x, c' * f x) g l := let ⟨c, hc⟩ := h in (hc.const_mul_left c').is_O theorem is_O_with_self_const_mul' (u : units R) (f : α → R) (l : filter α) : is_O_with ∥(↑u⁻¹:R)∥ f (λ x, ↑u * f x) l := (is_O_with_const_mul_self ↑u⁻¹ _ l).congr_left $ λ x, u.inv_mul_cancel_left (f x) theorem is_O_with_self_const_mul (c : 𝕜) (hc : c ≠ 0) (f : α → 𝕜) (l : filter α) : is_O_with ∥c∥⁻¹ f (λ x, c * f x) l := (is_O_with_self_const_mul' (units.mk0 c hc) f l).congr_const $ normed_field.norm_inv c theorem is_O_self_const_mul' {c : R} (hc : is_unit c) (f : α → R) (l : filter α) : is_O f (λ x, c * f x) l := let ⟨u, hu⟩ := hc in hu ▸ (is_O_with_self_const_mul' u f l).is_O theorem is_O_self_const_mul (c : 𝕜) (hc : c ≠ 0) (f : α → 𝕜) (l : filter α) : is_O f (λ x, c * f x) l := is_O_self_const_mul' (is_unit.mk0 c hc) f l theorem is_O_const_mul_left_iff' {f : α → R} {c : R} (hc : is_unit c) : is_O (λ x, c * f x) g l ↔ is_O f g l := ⟨(is_O_self_const_mul' hc f l).trans, λ h, h.const_mul_left c⟩ theorem is_O_const_mul_left_iff {f : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) : is_O (λ x, c * f x) g l ↔ is_O f g l := is_O_const_mul_left_iff' $ is_unit.mk0 c hc theorem is_o.const_mul_left {f : α → R} (h : is_o f g l) (c : R) : is_o (λ x, c * f x) g l := (is_O_const_mul_self c f l).trans_is_o h theorem is_o_const_mul_left_iff' {f : α → R} {c : R} (hc : is_unit c) : is_o (λ x, c * f x) g l ↔ is_o f g l := ⟨(is_O_self_const_mul' hc f l).trans_is_o, λ h, h.const_mul_left c⟩ theorem is_o_const_mul_left_iff {f : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) : is_o (λ x, c * f x) g l ↔ is_o f g l := is_o_const_mul_left_iff' $ is_unit.mk0 c hc theorem is_O_with.of_const_mul_right {g : α → R} {c : R} (hc' : 0 ≤ c') (h : is_O_with c' f (λ x, c * g x) l) : is_O_with (c' * ∥c∥) f g l := h.trans (is_O_with_const_mul_self c g l) hc' theorem is_O.of_const_mul_right {g : α → R} {c : R} (h : is_O f (λ x, c * g x) l) : is_O f g l := let ⟨c, cnonneg, hc⟩ := h.exists_nonneg in (hc.of_const_mul_right cnonneg).is_O theorem is_O_with.const_mul_right' {g : α → R} {u : units R} {c' : ℝ} (hc' : 0 ≤ c') (h : is_O_with c' f g l) : is_O_with (c' * ∥(↑u⁻¹:R)∥) f (λ x, ↑u * g x) l := h.trans (is_O_with_self_const_mul' _ _ _) hc' theorem is_O_with.const_mul_right {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) {c' : ℝ} (hc' : 0 ≤ c') (h : is_O_with c' f g l) : is_O_with (c' * ∥c∥⁻¹) f (λ x, c * g x) l := h.trans (is_O_with_self_const_mul c hc g l) hc' theorem is_O.const_mul_right' {g : α → R} {c : R} (hc : is_unit c) (h : is_O f g l) : is_O f (λ x, c * g x) l := h.trans (is_O_self_const_mul' hc g l) theorem is_O.const_mul_right {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) (h : is_O f g l) : is_O f (λ x, c * g x) l := h.const_mul_right' $ is_unit.mk0 c hc theorem is_O_const_mul_right_iff' {g : α → R} {c : R} (hc : is_unit c) : is_O f (λ x, c * g x) l ↔ is_O f g l := ⟨λ h, h.of_const_mul_right, λ h, h.const_mul_right' hc⟩ theorem is_O_const_mul_right_iff {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) : is_O f (λ x, c * g x) l ↔ is_O f g l := is_O_const_mul_right_iff' $ is_unit.mk0 c hc theorem is_o.of_const_mul_right {g : α → R} {c : R} (h : is_o f (λ x, c * g x) l) : is_o f g l := h.trans_is_O (is_O_const_mul_self c g l) theorem is_o.const_mul_right' {g : α → R} {c : R} (hc : is_unit c) (h : is_o f g l) : is_o f (λ x, c * g x) l := h.trans_is_O (is_O_self_const_mul' hc g l) theorem is_o.const_mul_right {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) (h : is_o f g l) : is_o f (λ x, c * g x) l := h.const_mul_right' $ is_unit.mk0 c hc theorem is_o_const_mul_right_iff' {g : α → R} {c : R} (hc : is_unit c) : is_o f (λ x, c * g x) l ↔ is_o f g l := ⟨λ h, h.of_const_mul_right, λ h, h.const_mul_right' hc⟩ theorem is_o_const_mul_right_iff {g : α → 𝕜} {c : 𝕜} (hc : c ≠ 0) : is_o f (λ x, c * g x) l ↔ is_o f g l := is_o_const_mul_right_iff' $ is_unit.mk0 c hc /-! ### Multiplication -/ theorem is_O_with.mul {f₁ f₂ : α → R} {g₁ g₂ : α → 𝕜} {c₁ c₂ : ℝ} (h₁ : is_O_with c₁ f₁ g₁ l) (h₂ : is_O_with c₂ f₂ g₂ l) : is_O_with (c₁ * c₂) (λ x, f₁ x * f₂ x) (λ x, g₁ x * g₂ x) l := begin filter_upwards [h₁, h₂], intros x hx₁ hx₂, apply le_trans (norm_mul_le _ _), convert mul_le_mul hx₁ hx₂ (norm_nonneg _) (le_trans (norm_nonneg _) hx₁) using 1, rw normed_field.norm_mul, ac_refl end theorem is_O.mul {f₁ f₂ : α → R} {g₁ g₂ : α → 𝕜} (h₁ : is_O f₁ g₁ l) (h₂ : is_O f₂ g₂ l) : is_O (λ x, f₁ x * f₂ x) (λ x, g₁ x * g₂ x) l := let ⟨c, hc⟩ := h₁, ⟨c', hc'⟩ := h₂ in (hc.mul hc').is_O theorem is_O.mul_is_o {f₁ f₂ : α → R} {g₁ g₂ : α → 𝕜} (h₁ : is_O f₁ g₁ l) (h₂ : is_o f₂ g₂ l) : is_o (λ x, f₁ x * f₂ x) (λ x, g₁ x * g₂ x) l := begin intros c cpos, rcases h₁.exists_pos with ⟨c', c'pos, hc'⟩, exact (hc'.mul (h₂ (div_pos cpos c'pos))).congr_const (mul_div_cancel' _ (ne_of_gt c'pos)) end theorem is_o.mul_is_O {f₁ f₂ : α → R} {g₁ g₂ : α → 𝕜} (h₁ : is_o f₁ g₁ l) (h₂ : is_O f₂ g₂ l) : is_o (λ x, f₁ x * f₂ x) (λ x, g₁ x * g₂ x) l := begin intros c cpos, rcases h₂.exists_pos with ⟨c', c'pos, hc'⟩, exact ((h₁ (div_pos cpos c'pos)).mul hc').congr_const (div_mul_cancel _ (ne_of_gt c'pos)) end theorem is_o.mul {f₁ f₂ : α → R} {g₁ g₂ : α → 𝕜} (h₁ : is_o f₁ g₁ l) (h₂ : is_o f₂ g₂ l) : is_o (λ x, f₁ x * f₂ x) (λ x, g₁ x * g₂ x) l := h₁.mul_is_O h₂.is_O /-! ### Scalar multiplication -/ section smul_const variables [normed_space 𝕜 E'] theorem is_O_with.const_smul_left (h : is_O_with c f' g l) (c' : 𝕜) : is_O_with (∥c'∥ * c) (λ x, c' • f' x) g l := by refine ((h.norm_left.const_mul_left (∥c'∥)).congr _ _ (λ _, rfl)).of_norm_left; intros; simp only [norm_norm, norm_smul] theorem is_O_const_smul_left_iff {c : 𝕜} (hc : c ≠ 0) : is_O (λ x, c • f' x) g l ↔ is_O f' g l := begin have cne0 : ∥c∥ ≠ 0, from mt norm_eq_zero.mp hc, rw [←is_O_norm_left], simp only [norm_smul], rw [is_O_const_mul_left_iff cne0, is_O_norm_left], end theorem is_o_const_smul_left (h : is_o f' g l) (c : 𝕜) : is_o (λ x, c • f' x) g l := begin refine ((h.norm_left.const_mul_left (∥c∥)).congr_left _).of_norm_left, exact λ x, (norm_smul _ _).symm end theorem is_o_const_smul_left_iff {c : 𝕜} (hc : c ≠ 0) : is_o (λ x, c • f' x) g l ↔ is_o f' g l := begin have cne0 : ∥c∥ ≠ 0, from mt norm_eq_zero.mp hc, rw [←is_o_norm_left], simp only [norm_smul], rw [is_o_const_mul_left_iff cne0, is_o_norm_left] end theorem is_O_const_smul_right {c : 𝕜} (hc : c ≠ 0) : is_O f (λ x, c • f' x) l ↔ is_O f f' l := begin have cne0 : ∥c∥ ≠ 0, from mt norm_eq_zero.mp hc, rw [←is_O_norm_right], simp only [norm_smul], rw [is_O_const_mul_right_iff cne0, is_O_norm_right] end theorem is_o_const_smul_right {c : 𝕜} (hc : c ≠ 0) : is_o f (λ x, c • f' x) l ↔ is_o f f' l := begin have cne0 : ∥c∥ ≠ 0, from mt norm_eq_zero.mp hc, rw [←is_o_norm_right], simp only [norm_smul], rw [is_o_const_mul_right_iff cne0, is_o_norm_right] end end smul_const section smul variables [normed_space 𝕜 E'] [normed_space 𝕜 F'] theorem is_O_with.smul {k₁ k₂ : α → 𝕜} (h₁ : is_O_with c k₁ k₂ l) (h₂ : is_O_with c' f' g' l) : is_O_with (c * c') (λ x, k₁ x • f' x) (λ x, k₂ x • g' x) l := by refine ((h₁.norm_norm.mul h₂.norm_norm).congr rfl _ _).of_norm_norm; by intros; simp only [norm_smul] theorem is_O.smul {k₁ k₂ : α → 𝕜} (h₁ : is_O k₁ k₂ l) (h₂ : is_O f' g' l) : is_O (λ x, k₁ x • f' x) (λ x, k₂ x • g' x) l := by refine ((h₁.norm_norm.mul h₂.norm_norm).congr _ _).of_norm_norm; by intros; simp only [norm_smul] theorem is_O.smul_is_o {k₁ k₂ : α → 𝕜} (h₁ : is_O k₁ k₂ l) (h₂ : is_o f' g' l) : is_o (λ x, k₁ x • f' x) (λ x, k₂ x • g' x) l := by refine ((h₁.norm_norm.mul_is_o h₂.norm_norm).congr _ _).of_norm_norm; by intros; simp only [norm_smul] theorem is_o.smul_is_O {k₁ k₂ : α → 𝕜} (h₁ : is_o k₁ k₂ l) (h₂ : is_O f' g' l) : is_o (λ x, k₁ x • f' x) (λ x, k₂ x • g' x) l := by refine ((h₁.norm_norm.mul_is_O h₂.norm_norm).congr _ _).of_norm_norm; by intros; simp only [norm_smul] theorem is_o.smul {k₁ k₂ : α → 𝕜} (h₁ : is_o k₁ k₂ l) (h₂ : is_o f' g' l) : is_o (λ x, k₁ x • f' x) (λ x, k₂ x • g' x) l := by refine ((h₁.norm_norm.mul h₂.norm_norm).congr _ _).of_norm_norm; by intros; simp only [norm_smul] end smul /-! ### Sum -/ section sum variables {ι : Type*} {A : ι → α → E'} {C : ι → ℝ} {s : finset ι} theorem is_O_with.sum (h : ∀ i ∈ s, is_O_with (C i) (A i) g l) : is_O_with (∑ i in s, C i) (λ x, ∑ i in s, A i x) g l := begin induction s using finset.induction_on with i s is IH, { simp only [is_O_with_zero', finset.sum_empty, forall_true_iff] }, { simp only [is, finset.sum_insert, not_false_iff], exact (h _ (finset.mem_insert_self i s)).add (IH (λ j hj, h _ (finset.mem_insert_of_mem hj))) } end theorem is_O.sum (h : ∀ i ∈ s, is_O (A i) g l) : is_O (λ x, ∑ i in s, A i x) g l := begin induction s using finset.induction_on with i s is IH, { simp only [is_O_zero, finset.sum_empty, forall_true_iff] }, { simp only [is, finset.sum_insert, not_false_iff], exact (h _ (finset.mem_insert_self i s)).add (IH (λ j hj, h _ (finset.mem_insert_of_mem hj))) } end theorem is_o.sum (h : ∀ i ∈ s, is_o (A i) g' l) : is_o (λ x, ∑ i in s, A i x) g' l := begin induction s using finset.induction_on with i s is IH, { simp only [is_o_zero, finset.sum_empty, forall_true_iff] }, { simp only [is, finset.sum_insert, not_false_iff], exact (h _ (finset.mem_insert_self i s)).add (IH (λ j hj, h _ (finset.mem_insert_of_mem hj))) } end end sum /-! ### Relation between `f = o(g)` and `f / g → 0` -/ theorem is_o.tendsto_0 {f g : α → 𝕜} {l : filter α} (h : is_o f g l) : tendsto (λ x, f x / (g x)) l (𝓝 0) := have eq₁ : is_o (λ x, f x / g x) (λ x, g x / g x) l, from h.mul_is_O (is_O_refl _ _), have eq₂ : is_O (λ x, g x / g x) (λ x, (1 : 𝕜)) l, from is_O_of_le _ (λ x, by by_cases h : ∥g x∥ = 0; simp [h, zero_le_one]), (is_o_one_iff 𝕜).mp (eq₁.trans_is_O eq₂) theorem is_o_iff_tendsto' {f g : α → 𝕜} {l : filter α} (hgf : ∀ᶠ x in l, g x = 0 → f x = 0) : is_o f g l ↔ tendsto (λ x, f x / (g x)) l (𝓝 0) := iff.intro is_o.tendsto_0 $ λ h, (((is_o_one_iff _).mpr h).mul_is_O (is_O_refl g l)).congr' (hgf.mono $ λ x, div_mul_cancel_of_imp) (eventually_of_forall $ λ x, one_mul _) theorem is_o_iff_tendsto {f g : α → 𝕜} {l : filter α} (hgf : ∀ x, g x = 0 → f x = 0) : is_o f g l ↔ tendsto (λ x, f x / (g x)) l (𝓝 0) := ⟨λ h, h.tendsto_0, (is_o_iff_tendsto' (eventually_of_forall hgf)).2⟩ alias is_o_iff_tendsto' ↔ _ asymptotics.is_o_of_tendsto' alias is_o_iff_tendsto ↔ _ asymptotics.is_o_of_tendsto /-! ### Eventually (u / v) * v = u If `u` and `v` are linked by an `is_O_with` relation, then we eventually have `(u / v) * v = u`, even if `v` vanishes. -/ section eventually_mul_div_cancel variables {u v : α → 𝕜} lemma is_O_with.eventually_mul_div_cancel (h : is_O_with c u v l) : (u / v) * v =ᶠ[l] u := begin refine eventually.mono h (λ y hy, div_mul_cancel_of_imp $ λ hv, _), rw hv at *, simpa using hy end /-- If `u = O(v)` along `l`, then `(u / v) * v = u` eventually at `l`. -/ lemma is_O.eventually_mul_div_cancel (h : is_O u v l) : (u / v) * v =ᶠ[l] u := let ⟨c, hc⟩ := h in hc.eventually_mul_div_cancel /-- If `u = o(v)` along `l`, then `(u / v) * v = u` eventually at `l`. -/ lemma is_o.eventually_mul_div_cancel (h : is_o u v l) : (u / v) * v =ᶠ[l] u := (h zero_lt_one).eventually_mul_div_cancel end eventually_mul_div_cancel /-! ### Equivalent definitions of the form `∃ φ, u =ᶠ[l] φ * v` in a `normed_field`. -/ section exists_mul_eq variables {u v : α → 𝕜} /-- If `∥φ∥` is eventually bounded by `c`, and `u =ᶠ[l] φ * v`, then we have `is_O_with c u v l`. This does not require any assumptions on `c`, which is why we keep this version along with `is_O_with_iff_exists_eq_mul`. -/ lemma is_O_with_of_eq_mul (φ : α → 𝕜) (hφ : ∀ᶠ x in l, ∥φ x∥ ≤ c) (h : u =ᶠ[l] φ * v) : is_O_with c u v l := begin refine h.symm.rw (λ x a, ∥a∥ ≤ c * ∥v x∥) (hφ.mono $ λ x hx, _), simp only [normed_field.norm_mul, pi.mul_apply], exact mul_le_mul_of_nonneg_right hx (norm_nonneg _) end lemma is_O_with_iff_exists_eq_mul (hc : 0 ≤ c) : is_O_with c u v l ↔ ∃ (φ : α → 𝕜) (hφ : ∀ᶠ x in l, ∥φ x∥ ≤ c), u =ᶠ[l] φ * v := begin split, { intro h, use (λ x, u x / v x), refine ⟨eventually.mono h (λ y hy, _), h.eventually_mul_div_cancel.symm⟩, simpa using div_le_of_nonneg_of_le_mul (norm_nonneg _) hc hy }, { rintros ⟨φ, hφ, h⟩, exact is_O_with_of_eq_mul φ hφ h } end lemma is_O_with.exists_eq_mul (h : is_O_with c u v l) (hc : 0 ≤ c) : ∃ (φ : α → 𝕜) (hφ : ∀ᶠ x in l, ∥φ x∥ ≤ c), u =ᶠ[l] φ * v := (is_O_with_iff_exists_eq_mul hc).mp h lemma is_O_iff_exists_eq_mul : is_O u v l ↔ ∃ (φ : α → 𝕜) (hφ : l.is_bounded_under (≤) (norm ∘ φ)), u =ᶠ[l] φ * v := begin split, { rintros h, rcases h.exists_nonneg with ⟨c, hnnc, hc⟩, rcases hc.exists_eq_mul hnnc with ⟨φ, hφ, huvφ⟩, exact ⟨φ, ⟨c, hφ⟩, huvφ⟩ }, { rintros ⟨φ, ⟨c, hφ⟩, huvφ⟩, exact ⟨c, is_O_with_of_eq_mul φ hφ huvφ⟩ } end alias is_O_iff_exists_eq_mul ↔ asymptotics.is_O.exists_eq_mul _ lemma is_o_iff_exists_eq_mul : is_o u v l ↔ ∃ (φ : α → 𝕜) (hφ : tendsto φ l (𝓝 0)), u =ᶠ[l] φ * v := begin split, { exact λ h, ⟨λ x, u x / v x, h.tendsto_0, h.eventually_mul_div_cancel.symm⟩ }, { rintros ⟨φ, hφ, huvφ⟩ c hpos, rw normed_group.tendsto_nhds_zero at hφ, exact is_O_with_of_eq_mul _ ((hφ c hpos).mono $ λ x, le_of_lt) huvφ } end alias is_o_iff_exists_eq_mul ↔ asymptotics.is_o.exists_eq_mul _ end exists_mul_eq /-! ### Miscellanous lemmas -/ theorem is_o_pow_pow {m n : ℕ} (h : m < n) : is_o (λ(x : 𝕜), x^n) (λx, x^m) (𝓝 0) := begin let p := n - m, have nmp : n = m + p := (nat.add_sub_cancel' (le_of_lt h)).symm, have : (λ(x : 𝕜), x^m) = (λx, x^m * 1), by simp only [mul_one], simp only [this, pow_add, nmp], refine is_O.mul_is_o (is_O_refl _ _) ((is_o_one_iff _).2 _), convert (continuous_pow p).tendsto (0 : 𝕜), exact (zero_pow (nat.sub_pos_of_lt h)).symm end theorem is_o_norm_pow_norm_pow {m n : ℕ} (h : m < n) : is_o (λ(x : E'), ∥x∥^n) (λx, ∥x∥^m) (𝓝 (0 : E')) := (is_o_pow_pow h).comp_tendsto tendsto_norm_zero theorem is_o_pow_id {n : ℕ} (h : 1 < n) : is_o (λ(x : 𝕜), x^n) (λx, x) (𝓝 0) := by { convert is_o_pow_pow h, simp only [pow_one] } theorem is_o_norm_pow_id {n : ℕ} (h : 1 < n) : is_o (λ(x : E'), ∥x∥^n) (λx, x) (𝓝 0) := by simpa only [pow_one, is_o_norm_right] using is_o_norm_pow_norm_pow h theorem is_O_with.right_le_sub_of_lt_1 {f₁ f₂ : α → E'} (h : is_O_with c f₁ f₂ l) (hc : c < 1) : is_O_with (1 / (1 - c)) f₂ (λx, f₂ x - f₁ x) l := mem_sets_of_superset h $ λ x hx, begin simp only [mem_set_of_eq] at hx ⊢, rw [mul_comm, one_div, ← div_eq_mul_inv, le_div_iff, mul_sub, mul_one, mul_comm], { exact le_trans (sub_le_sub_left hx _) (norm_sub_norm_le _ _) }, { exact sub_pos.2 hc } end theorem is_O_with.right_le_add_of_lt_1 {f₁ f₂ : α → E'} (h : is_O_with c f₁ f₂ l) (hc : c < 1) : is_O_with (1 / (1 - c)) f₂ (λx, f₁ x + f₂ x) l := (h.neg_right.right_le_sub_of_lt_1 hc).neg_right.of_neg_left.congr rfl (λ x, rfl) (λ x, by rw [neg_sub, sub_neg_eq_add]) theorem is_o.right_is_O_sub {f₁ f₂ : α → E'} (h : is_o f₁ f₂ l) : is_O f₂ (λx, f₂ x - f₁ x) l := ((h.def' one_half_pos).right_le_sub_of_lt_1 one_half_lt_one).is_O theorem is_o.right_is_O_add {f₁ f₂ : α → E'} (h : is_o f₁ f₂ l) : is_O f₂ (λx, f₁ x + f₂ x) l := ((h.def' one_half_pos).right_le_add_of_lt_1 one_half_lt_one).is_O /-- If `f x = O(g x)` along `cofinite`, then there exists a positive constant `C` such that `∥f x∥ ≤ C * ∥g x∥` whenever `g x ≠ 0`. -/ theorem bound_of_is_O_cofinite (h : is_O f g' cofinite) : ∃ C > 0, ∀ ⦃x⦄, g' x ≠ 0 → ∥f x∥ ≤ C * ∥g' x∥ := begin rcases h.exists_pos with ⟨C, C₀, hC⟩, rw [is_O_with, eventually_cofinite] at hC, rcases (hC.to_finset.image (λ x, ∥f x∥ / ∥g' x∥)).exists_le with ⟨C', hC'⟩, have : ∀ x, C * ∥g' x∥ < ∥f x∥ → ∥f x∥ / ∥g' x∥ ≤ C', by simpa using hC', refine ⟨max C C', lt_max_iff.2 (or.inl C₀), λ x h₀, _⟩, rw [max_mul_of_nonneg _ _ (norm_nonneg _), le_max_iff, or_iff_not_imp_left, not_le], exact λ hx, (div_le_iff (norm_pos_iff.2 h₀)).1 (this _ hx) end theorem is_O_cofinite_iff (h : ∀ x, g' x = 0 → f' x = 0) : is_O f' g' cofinite ↔ ∃ C, ∀ x, ∥f' x∥ ≤ C * ∥g' x∥ := ⟨λ h', let ⟨C, C₀, hC⟩ := bound_of_is_O_cofinite h' in ⟨C, λ x, if hx : g' x = 0 then by simp [h _ hx, hx] else hC hx⟩, λ h, (is_O_top.2 h).mono le_top⟩ theorem bound_of_is_O_nat_at_top {f : ℕ → E} {g' : ℕ → E'} (h : is_O f g' at_top) : ∃ C > 0, ∀ ⦃x⦄, g' x ≠ 0 → ∥f x∥ ≤ C * ∥g' x∥ := bound_of_is_O_cofinite $ by rwa nat.cofinite_eq_at_top theorem is_O_nat_at_top_iff {f : ℕ → E'} {g : ℕ → F'} (h : ∀ x, g x = 0 → f x = 0) : is_O f g at_top ↔ ∃ C, ∀ x, ∥f x∥ ≤ C * ∥g x∥ := by rw [← nat.cofinite_eq_at_top, is_O_cofinite_iff h] theorem is_O_one_nat_at_top_iff {f : ℕ → E'} : is_O f (λ n, 1 : ℕ → ℝ) at_top ↔ ∃ C, ∀ n, ∥f n∥ ≤ C := iff.trans (is_O_nat_at_top_iff (λ n h, (one_ne_zero h).elim)) $ by simp only [norm_one, mul_one] end asymptotics namespace local_homeomorph variables {α : Type*} {β : Type*} [topological_space α] [topological_space β] variables {E : Type*} [has_norm E] {F : Type*} [has_norm F] open asymptotics /-- Transfer `is_O_with` over a `local_homeomorph`. -/ lemma is_O_with_congr (e : local_homeomorph α β) {b : β} (hb : b ∈ e.target) {f : β → E} {g : β → F} {C : ℝ} : is_O_with C f g (𝓝 b) ↔ is_O_with C (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := ⟨λ h, h.comp_tendsto $ by { convert e.continuous_at (e.map_target hb), exact (e.right_inv hb).symm }, λ h, (h.comp_tendsto (e.continuous_at_symm hb)).congr' rfl ((e.eventually_right_inverse hb).mono $ λ x hx, congr_arg f hx) ((e.eventually_right_inverse hb).mono $ λ x hx, congr_arg g hx)⟩ /-- Transfer `is_O` over a `local_homeomorph`. -/ lemma is_O_congr (e : local_homeomorph α β) {b : β} (hb : b ∈ e.target) {f : β → E} {g : β → F} : is_O f g (𝓝 b) ↔ is_O (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := exists_congr $ λ C, e.is_O_with_congr hb /-- Transfer `is_o` over a `local_homeomorph`. -/ lemma is_o_congr (e : local_homeomorph α β) {b : β} (hb : b ∈ e.target) {f : β → E} {g : β → F} : is_o f g (𝓝 b) ↔ is_o (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := forall_congr $ λ c, forall_congr $ λ hc, e.is_O_with_congr hb end local_homeomorph namespace homeomorph variables {α : Type*} {β : Type*} [topological_space α] [topological_space β] variables {E : Type*} [has_norm E] {F : Type*} [has_norm F] open asymptotics /-- Transfer `is_O_with` over a `homeomorph`. -/ lemma is_O_with_congr (e : α ≃ₜ β) {b : β} {f : β → E} {g : β → F} {C : ℝ} : is_O_with C f g (𝓝 b) ↔ is_O_with C (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := e.to_local_homeomorph.is_O_with_congr trivial /-- Transfer `is_O` over a `homeomorph`. -/ lemma is_O_congr (e : α ≃ₜ β) {b : β} {f : β → E} {g : β → F} : is_O f g (𝓝 b) ↔ is_O (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := exists_congr $ λ C, e.is_O_with_congr /-- Transfer `is_o` over a `homeomorph`. -/ lemma is_o_congr (e : α ≃ₜ β) {b : β} {f : β → E} {g : β → F} : is_o f g (𝓝 b) ↔ is_o (f ∘ e) (g ∘ e) (𝓝 (e.symm b)) := forall_congr $ λ c, forall_congr $ λ hc, e.is_O_with_congr end homeomorph attribute [irreducible] asymptotics.is_o asymptotics.is_O asymptotics.is_O_with
b7eff06c6f138fd975c78ac38200b8ac1b16ab14
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/representation_theory/character.lean
07ddd2fbf264d627759935446d38e10ad31b1574
[ "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
4,441
lean
/- Copyright (c) 2022 Antoine Labelle. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Antoine Labelle -/ import representation_theory.fdRep import linear_algebra.trace import representation_theory.invariants /-! # Characters of representations > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file introduces characters of representation and proves basic lemmas about how characters behave under various operations on representations. # TODO * Once we have the monoidal closed structure on `fdRep k G` and a better API for the rigid structure, `char_dual` and `char_lin_hom` should probably be stated in terms of `Vᘁ` and `ihom V W`. -/ noncomputable theory universes u open category_theory linear_map category_theory.monoidal_category representation finite_dimensional open_locale big_operators variables {k : Type u} [field k] namespace fdRep section monoid variables {G : Type u} [monoid G] /-- The character of a representation `V : fdRep k G` is the function associating to `g : G` the trace of the linear map `V.ρ g`.-/ def character (V : fdRep k G) (g : G) := linear_map.trace k V (V.ρ g) lemma char_mul_comm (V : fdRep k G) (g : G) (h : G) : V.character (h * g) = V.character (g * h) := by simp only [trace_mul_comm, character, map_mul] @[simp] lemma char_one (V : fdRep k G) : V.character 1 = finite_dimensional.finrank k V := by simp only [character, map_one, trace_one] /-- The character is multiplicative under the tensor product. -/ @[simp] lemma char_tensor (V W : fdRep k G) : (V ⊗ W).character = V.character * W.character := by { ext g, convert trace_tensor_product' (V.ρ g) (W.ρ g) } /-- The character of isomorphic representations is the same. -/ lemma char_iso {V W : fdRep k G} (i : V ≅ W) : V.character = W.character := by { ext g, simp only [character, fdRep.iso.conj_ρ i], exact (trace_conj' (V.ρ g) _).symm } end monoid section group variables {G : Type u} [group G] /-- The character of a representation is constant on conjugacy classes. -/ @[simp] lemma char_conj (V : fdRep k G) (g : G) (h : G) : V.character (h * g * h⁻¹) = V.character g := by rw [char_mul_comm, inv_mul_cancel_left] @[simp] lemma char_dual (V : fdRep k G) (g : G) : (of (dual V.ρ)).character g = V.character g⁻¹ := trace_transpose' (V.ρ g⁻¹) @[simp] lemma char_lin_hom (V W : fdRep k G) (g : G) : (of (lin_hom V.ρ W.ρ)).character g = (V.character g⁻¹) * (W.character g) := by rw [←char_iso (dual_tensor_iso_lin_hom _ _), char_tensor, pi.mul_apply, char_dual] variables [fintype G] [invertible (fintype.card G : k)] theorem average_char_eq_finrank_invariants (V : fdRep k G) : ⅟(fintype.card G : k) • ∑ g : G, V.character g = finrank k (invariants V.ρ) := by { rw ←(is_proj_average_map V.ρ).trace, simp [character, group_algebra.average, _root_.map_sum], } end group section orthogonality variables {G : Group.{u}} [is_alg_closed k] open_locale classical variables [fintype G] [invertible (fintype.card G : k)] /-- Orthogonality of characters for irreducible representations of finite group over an algebraically closed field whose characteristic doesn't divide the order of the group. -/ lemma char_orthonormal (V W : fdRep k G) [simple V] [simple W] : ⅟(fintype.card G : k) • ∑ g : G, V.character g * W.character g⁻¹ = if nonempty (V ≅ W) then ↑1 else ↑0 := begin -- First, we can rewrite the summand `V.character g * W.character g⁻¹` as the character -- of the representation `V ⊗ W* ≅ Hom(W, V)` applied to `g`. conv in (V.character _ * W.character _) { rw [mul_comm, ←char_dual, ←pi.mul_apply, ←char_tensor], rw [char_iso (fdRep.dual_tensor_iso_lin_hom W.ρ V)], } , -- The average over the group of the character of a representation equals the dimension of the -- space of invariants. rw average_char_eq_finrank_invariants, rw [show (of (lin_hom W.ρ V.ρ)).ρ = lin_hom W.ρ V.ρ, from fdRep.of_ρ (lin_hom W.ρ V.ρ)], -- The space of invariants of `Hom(W, V)` is the subspace of `G`-equivariant linear maps, -- `Hom_G(W, V)`. rw (lin_hom.invariants_equiv_fdRep_hom W V).finrank_eq, -- By Schur's Lemma, the dimension of `Hom_G(W, V)` is `1` is `V ≅ W` and `0` otherwise. rw_mod_cast [finrank_hom_simple_simple W V, iso.nonempty_iso_symm], end end orthogonality end fdRep
0bf548c9048ec2b3c4e3b1c7105d4cd6c524cd7c
9dc8cecdf3c4634764a18254e94d43da07142918
/src/order/bounds.lean
b1a2e3681b28ffa9d83dd2d599ba6db7ce0d610e
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
52,804
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, Yury Kudryashov -/ import data.set.intervals.basic /-! # Upper / lower bounds In this file we define: * `upper_bounds`, `lower_bounds` : the set of upper bounds (resp., lower bounds) of a set; * `bdd_above s`, `bdd_below s` : the set `s` is bounded above (resp., below), i.e., the set of upper (resp., lower) bounds of `s` is nonempty; * `is_least s a`, `is_greatest s a` : `a` is a least (resp., greatest) element of `s`; for a partial order, it is unique if exists; * `is_lub s a`, `is_glb s a` : `a` is a least upper bound (resp., a greatest lower bound) of `s`; for a partial order, it is unique if exists. We also prove various lemmas about monotonicity, behaviour under `∪`, `∩`, `insert`, and provide formulas for `∅`, `univ`, and intervals. -/ open function set order_dual (to_dual of_dual) universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section variables [preorder α] [preorder β] {s t : set α} {a b : α} /-! ### Definitions -/ /-- The set of upper bounds of a set. -/ def upper_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → a ≤ x } /-- The set of lower bounds of a set. -/ def lower_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → x ≤ a } /-- A set is bounded above if there exists an upper bound. -/ def bdd_above (s : set α) := (upper_bounds s).nonempty /-- A set is bounded below if there exists a lower bound. -/ def bdd_below (s : set α) := (lower_bounds s).nonempty /-- `a` is a least element of a set `s`; for a partial order, it is unique if exists. -/ def is_least (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ lower_bounds s /-- `a` is a greatest element of a set `s`; for a partial order, it is unique if exists -/ def is_greatest (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ upper_bounds s /-- `a` is a least upper bound of a set `s`; for a partial order, it is unique if exists. -/ def is_lub (s : set α) : α → Prop := is_least (upper_bounds s) /-- `a` is a greatest lower bound of a set `s`; for a partial order, it is unique if exists. -/ def is_glb (s : set α) : α → Prop := is_greatest (lower_bounds s) lemma mem_upper_bounds : a ∈ upper_bounds s ↔ ∀ x ∈ s, x ≤ a := iff.rfl lemma mem_lower_bounds : a ∈ lower_bounds s ↔ ∀ x ∈ s, a ≤ x := iff.rfl lemma bdd_above_def : bdd_above s ↔ ∃ x, ∀ y ∈ s, y ≤ x := iff.rfl lemma bdd_below_def : bdd_below s ↔ ∃ x, ∀ y ∈ s, x ≤ y := iff.rfl lemma bot_mem_lower_bounds [order_bot α] (s : set α) : ⊥ ∈ lower_bounds s := λ _ _, bot_le lemma top_mem_upper_bounds [order_top α] (s : set α) : ⊤ ∈ upper_bounds s := λ _ _, le_top @[simp] lemma is_least_bot_iff [order_bot α] : is_least s ⊥ ↔ ⊥ ∈ s := and_iff_left $ bot_mem_lower_bounds _ @[simp] lemma is_greatest_top_iff [order_top α] : is_greatest s ⊤ ↔ ⊤ ∈ s := and_iff_left $ top_mem_upper_bounds _ /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` such that `x` is not greater than or equal to `y`. This version only assumes `preorder` structure and uses `¬(y ≤ x)`. A version for linear orders is called `not_bdd_above_iff`. -/ lemma not_bdd_above_iff' : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, ¬(y ≤ x) := by simp [bdd_above, upper_bounds, set.nonempty] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` such that `x` is not less than or equal to `y`. This version only assumes `preorder` structure and uses `¬(x ≤ y)`. A version for linear orders is called `not_bdd_below_iff`. -/ lemma not_bdd_below_iff' : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, ¬ x ≤ y := @not_bdd_above_iff' αᵒᵈ _ _ /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` that is greater than `x`. A version for preorders is called `not_bdd_above_iff'`. -/ lemma not_bdd_above_iff {α : Type*} [linear_order α] {s : set α} : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, x < y := by simp only [not_bdd_above_iff', not_le] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` that is less than `x`. A version for preorders is called `not_bdd_below_iff'`. -/ lemma not_bdd_below_iff {α : Type*} [linear_order α] {s : set α} : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, y < x := @not_bdd_above_iff αᵒᵈ _ _ lemma bdd_above.dual (h : bdd_above s) : bdd_below (of_dual ⁻¹' s) := h lemma bdd_below.dual (h : bdd_below s) : bdd_above (of_dual ⁻¹' s) := h lemma is_least.dual (h : is_least s a) : is_greatest (of_dual ⁻¹' s) (to_dual a) := h lemma is_greatest.dual (h : is_greatest s a) : is_least (of_dual ⁻¹' s) (to_dual a) := h lemma is_lub.dual (h : is_lub s a) : is_glb (of_dual ⁻¹' s) (to_dual a) := h lemma is_glb.dual (h : is_glb s a) : is_lub (of_dual ⁻¹' s) (to_dual a) := h /-- If `a` is the least element of a set `s`, then subtype `s` is an order with bottom element. -/ @[reducible] def is_least.order_bot (h : is_least s a) : order_bot s := { bot := ⟨a, h.1⟩, bot_le := subtype.forall.2 h.2 } /-- If `a` is the greatest element of a set `s`, then subtype `s` is an order with top element. -/ @[reducible] def is_greatest.order_top (h : is_greatest s a) : order_top s := { top := ⟨a, h.1⟩, le_top := subtype.forall.2 h.2 } /-! ### Monotonicity -/ lemma upper_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) : upper_bounds t ⊆ upper_bounds s := λ b hb x h, hb $ hst h lemma lower_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) : lower_bounds t ⊆ lower_bounds s := λ b hb x h, hb $ hst h lemma upper_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds s → b ∈ upper_bounds s := λ ha x h, le_trans (ha h) hab lemma lower_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds s → a ∈ lower_bounds s := λ hb x h, le_trans hab (hb h) lemma upper_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds t → b ∈ upper_bounds s := λ ha, upper_bounds_mono_set hst $ upper_bounds_mono_mem hab ha lemma lower_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds t → a ∈ lower_bounds s := λ hb, lower_bounds_mono_set hst $ lower_bounds_mono_mem hab hb /-- If `s ⊆ t` and `t` is bounded above, then so is `s`. -/ lemma bdd_above.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_above t → bdd_above s := nonempty.mono $ upper_bounds_mono_set h /-- If `s ⊆ t` and `t` is bounded below, then so is `s`. -/ lemma bdd_below.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_below t → bdd_below s := nonempty.mono $ lower_bounds_mono_set h /-- If `a` is a least upper bound for sets `s` and `p`, then it is a least upper bound for any set `t`, `s ⊆ t ⊆ p`. -/ lemma is_lub.of_subset_of_superset {s t p : set α} (hs : is_lub s a) (hp : is_lub p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_lub t a := ⟨upper_bounds_mono_set htp hp.1, lower_bounds_mono_set (upper_bounds_mono_set hst) hs.2⟩ /-- If `a` is a greatest lower bound for sets `s` and `p`, then it is a greater lower bound for any set `t`, `s ⊆ t ⊆ p`. -/ lemma is_glb.of_subset_of_superset {s t p : set α} (hs : is_glb s a) (hp : is_glb p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_glb t a := hs.dual.of_subset_of_superset hp hst htp lemma is_least.mono (ha : is_least s a) (hb : is_least t b) (hst : s ⊆ t) : b ≤ a := hb.2 (hst ha.1) lemma is_greatest.mono (ha : is_greatest s a) (hb : is_greatest t b) (hst : s ⊆ t) : a ≤ b := hb.2 (hst ha.1) lemma is_lub.mono (ha : is_lub s a) (hb : is_lub t b) (hst : s ⊆ t) : a ≤ b := hb.mono ha $ upper_bounds_mono_set hst lemma is_glb.mono (ha : is_glb s a) (hb : is_glb t b) (hst : s ⊆ t) : b ≤ a := hb.mono ha $ lower_bounds_mono_set hst lemma subset_lower_bounds_upper_bounds (s : set α) : s ⊆ lower_bounds (upper_bounds s) := λ x hx y hy, hy hx lemma subset_upper_bounds_lower_bounds (s : set α) : s ⊆ upper_bounds (lower_bounds s) := λ x hx y hy, hy hx lemma set.nonempty.bdd_above_lower_bounds (hs : s.nonempty) : bdd_above (lower_bounds s) := hs.mono (subset_upper_bounds_lower_bounds s) lemma set.nonempty.bdd_below_upper_bounds (hs : s.nonempty) : bdd_below (upper_bounds s) := hs.mono (subset_lower_bounds_upper_bounds s) /-! ### Conversions -/ lemma is_least.is_glb (h : is_least s a) : is_glb s a := ⟨h.2, λ b hb, hb h.1⟩ lemma is_greatest.is_lub (h : is_greatest s a) : is_lub s a := ⟨h.2, λ b hb, hb h.1⟩ lemma is_lub.upper_bounds_eq (h : is_lub s a) : upper_bounds s = Ici a := set.ext $ λ b, ⟨λ hb, h.2 hb, λ hb, upper_bounds_mono_mem hb h.1⟩ lemma is_glb.lower_bounds_eq (h : is_glb s a) : lower_bounds s = Iic a := h.dual.upper_bounds_eq lemma is_least.lower_bounds_eq (h : is_least s a) : lower_bounds s = Iic a := h.is_glb.lower_bounds_eq lemma is_greatest.upper_bounds_eq (h : is_greatest s a) : upper_bounds s = Ici a := h.is_lub.upper_bounds_eq lemma is_lub_le_iff (h : is_lub s a) : a ≤ b ↔ b ∈ upper_bounds s := by { rw h.upper_bounds_eq, refl } lemma le_is_glb_iff (h : is_glb s a) : b ≤ a ↔ b ∈ lower_bounds s := by { rw h.lower_bounds_eq, refl } lemma is_lub_iff_le_iff : is_lub s a ↔ ∀ b, a ≤ b ↔ b ∈ upper_bounds s := ⟨λ h b, is_lub_le_iff h, λ H, ⟨(H _).1 le_rfl, λ b hb, (H b).2 hb⟩⟩ lemma is_glb_iff_le_iff : is_glb s a ↔ ∀ b, b ≤ a ↔ b ∈ lower_bounds s := @is_lub_iff_le_iff αᵒᵈ _ _ _ /-- If `s` has a least upper bound, then it is bounded above. -/ lemma is_lub.bdd_above (h : is_lub s a) : bdd_above s := ⟨a, h.1⟩ /-- If `s` has a greatest lower bound, then it is bounded below. -/ lemma is_glb.bdd_below (h : is_glb s a) : bdd_below s := ⟨a, h.1⟩ /-- If `s` has a greatest element, then it is bounded above. -/ lemma is_greatest.bdd_above (h : is_greatest s a) : bdd_above s := ⟨a, h.2⟩ /-- If `s` has a least element, then it is bounded below. -/ lemma is_least.bdd_below (h : is_least s a) : bdd_below s := ⟨a, h.2⟩ lemma is_least.nonempty (h : is_least s a) : s.nonempty := ⟨a, h.1⟩ lemma is_greatest.nonempty (h : is_greatest s a) : s.nonempty := ⟨a, h.1⟩ /-! ### Union and intersection -/ @[simp] lemma upper_bounds_union : upper_bounds (s ∪ t) = upper_bounds s ∩ upper_bounds t := subset.antisymm (λ b hb, ⟨λ x hx, hb (or.inl hx), λ x hx, hb (or.inr hx)⟩) (λ b hb x hx, hx.elim (λ hs, hb.1 hs) (λ ht, hb.2 ht)) @[simp] lemma lower_bounds_union : lower_bounds (s ∪ t) = lower_bounds s ∩ lower_bounds t := @upper_bounds_union αᵒᵈ _ s t lemma union_upper_bounds_subset_upper_bounds_inter : upper_bounds s ∪ upper_bounds t ⊆ upper_bounds (s ∩ t) := union_subset (upper_bounds_mono_set $ inter_subset_left _ _) (upper_bounds_mono_set $ inter_subset_right _ _) lemma union_lower_bounds_subset_lower_bounds_inter : lower_bounds s ∪ lower_bounds t ⊆ lower_bounds (s ∩ t) := @union_upper_bounds_subset_upper_bounds_inter αᵒᵈ _ s t lemma is_least_union_iff {a : α} {s t : set α} : is_least (s ∪ t) a ↔ (is_least s a ∧ a ∈ lower_bounds t ∨ a ∈ lower_bounds s ∧ is_least t a) := by simp [is_least, lower_bounds_union, or_and_distrib_right, and_comm (a ∈ t), and_assoc] lemma is_greatest_union_iff : is_greatest (s ∪ t) a ↔ (is_greatest s a ∧ a ∈ upper_bounds t ∨ a ∈ upper_bounds s ∧ is_greatest t a) := @is_least_union_iff αᵒᵈ _ a s t /-- If `s` is bounded, then so is `s ∩ t` -/ lemma bdd_above.inter_of_left (h : bdd_above s) : bdd_above (s ∩ t) := h.mono $ inter_subset_left s t /-- If `t` is bounded, then so is `s ∩ t` -/ lemma bdd_above.inter_of_right (h : bdd_above t) : bdd_above (s ∩ t) := h.mono $ inter_subset_right s t /-- If `s` is bounded, then so is `s ∩ t` -/ lemma bdd_below.inter_of_left (h : bdd_below s) : bdd_below (s ∩ t) := h.mono $ inter_subset_left s t /-- If `t` is bounded, then so is `s ∩ t` -/ lemma bdd_below.inter_of_right (h : bdd_below t) : bdd_below (s ∩ t) := h.mono $ inter_subset_right s t /-- If `s` and `t` are bounded above sets in a `semilattice_sup`, then so is `s ∪ t`. -/ lemma bdd_above.union [semilattice_sup γ] {s t : set γ} : bdd_above s → bdd_above t → bdd_above (s ∪ t) := begin rintros ⟨bs, hs⟩ ⟨bt, ht⟩, use bs ⊔ bt, rw upper_bounds_union, exact ⟨upper_bounds_mono_mem le_sup_left hs, upper_bounds_mono_mem le_sup_right ht⟩ end /-- The union of two sets is bounded above if and only if each of the sets is. -/ lemma bdd_above_union [semilattice_sup γ] {s t : set γ} : bdd_above (s ∪ t) ↔ bdd_above s ∧ bdd_above t := ⟨λ h, ⟨h.mono $ subset_union_left s t, h.mono $ subset_union_right s t⟩, λ h, h.1.union h.2⟩ lemma bdd_below.union [semilattice_inf γ] {s t : set γ} : bdd_below s → bdd_below t → bdd_below (s ∪ t) := @bdd_above.union γᵒᵈ _ s t /--The union of two sets is bounded above if and only if each of the sets is.-/ lemma bdd_below_union [semilattice_inf γ] {s t : set γ} : bdd_below (s ∪ t) ↔ bdd_below s ∧ bdd_below t := @bdd_above_union γᵒᵈ _ s t /-- If `a` is the least upper bound of `s` and `b` is the least upper bound of `t`, then `a ⊔ b` is the least upper bound of `s ∪ t`. -/ lemma is_lub.union [semilattice_sup γ] {a b : γ} {s t : set γ} (hs : is_lub s a) (ht : is_lub t b) : is_lub (s ∪ t) (a ⊔ b) := ⟨λ c h, h.cases_on (λ h, le_sup_of_le_left $ hs.left h) (λ h, le_sup_of_le_right $ ht.left h), λ c hc, sup_le (hs.right $ λ d hd, hc $ or.inl hd) (ht.right $ λ d hd, hc $ or.inr hd)⟩ /-- If `a` is the greatest lower bound of `s` and `b` is the greatest lower bound of `t`, then `a ⊓ b` is the greatest lower bound of `s ∪ t`. -/ lemma is_glb.union [semilattice_inf γ] {a₁ a₂ : γ} {s t : set γ} (hs : is_glb s a₁) (ht : is_glb t a₂) : is_glb (s ∪ t) (a₁ ⊓ a₂) := hs.dual.union ht /-- If `a` is the least element of `s` and `b` is the least element of `t`, then `min a b` is the least element of `s ∪ t`. -/ lemma is_least.union [linear_order γ] {a b : γ} {s t : set γ} (ha : is_least s a) (hb : is_least t b) : is_least (s ∪ t) (min a b) := ⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1], (ha.is_glb.union hb.is_glb).1⟩ /-- If `a` is the greatest element of `s` and `b` is the greatest element of `t`, then `max a b` is the greatest element of `s ∪ t`. -/ lemma is_greatest.union [linear_order γ] {a b : γ} {s t : set γ} (ha : is_greatest s a) (hb : is_greatest t b) : is_greatest (s ∪ t) (max a b) := ⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1], (ha.is_lub.union hb.is_lub).1⟩ lemma is_lub.inter_Ici_of_mem [linear_order γ] {s : set γ} {a b : γ} (ha : is_lub s a) (hb : b ∈ s) : is_lub (s ∩ Ici b) a := ⟨λ x hx, ha.1 hx.1, λ c hc, have hbc : b ≤ c, from hc ⟨hb, le_rfl⟩, ha.2 $ λ x hx, (le_total x b).elim (λ hxb, hxb.trans hbc) $ λ hbx, hc ⟨hx, hbx⟩⟩ lemma is_glb.inter_Iic_of_mem [linear_order γ] {s : set γ} {a b : γ} (ha : is_glb s a) (hb : b ∈ s) : is_glb (s ∩ Iic b) a := ha.dual.inter_Ici_of_mem hb lemma bdd_above_iff_exists_ge [semilattice_sup γ] {s : set γ} (x₀ : γ) : bdd_above s ↔ ∃ x, x₀ ≤ x ∧ ∀ y ∈ s, y ≤ x := by { rw [bdd_above_def, exists_ge_and_iff_exists], exact monotone.ball (λ x hx, monotone_le) } lemma bdd_below_iff_exists_le [semilattice_inf γ] {s : set γ} (x₀ : γ) : bdd_below s ↔ ∃ x, x ≤ x₀ ∧ ∀ y ∈ s, x ≤ y := bdd_above_iff_exists_ge (to_dual x₀) lemma bdd_above.exists_ge [semilattice_sup γ] {s : set γ} (hs : bdd_above s) (x₀ : γ) : ∃ x, x₀ ≤ x ∧ ∀ y ∈ s, y ≤ x := (bdd_above_iff_exists_ge x₀).mp hs lemma bdd_below.exists_le [semilattice_inf γ] {s : set γ} (hs : bdd_below s) (x₀ : γ) : ∃ x, x ≤ x₀ ∧ ∀ y ∈ s, x ≤ y := (bdd_below_iff_exists_le x₀).mp hs /-! ### Specific sets #### Unbounded intervals -/ lemma is_least_Ici : is_least (Ici a) a := ⟨left_mem_Ici, λ x, id⟩ lemma is_greatest_Iic : is_greatest (Iic a) a := ⟨right_mem_Iic, λ x, id⟩ lemma is_lub_Iic : is_lub (Iic a) a := is_greatest_Iic.is_lub lemma is_glb_Ici : is_glb (Ici a) a := is_least_Ici.is_glb lemma upper_bounds_Iic : upper_bounds (Iic a) = Ici a := is_lub_Iic.upper_bounds_eq lemma lower_bounds_Ici : lower_bounds (Ici a) = Iic a := is_glb_Ici.lower_bounds_eq lemma bdd_above_Iic : bdd_above (Iic a) := is_lub_Iic.bdd_above lemma bdd_below_Ici : bdd_below (Ici a) := is_glb_Ici.bdd_below lemma bdd_above_Iio : bdd_above (Iio a) := ⟨a, λ x hx, le_of_lt hx⟩ lemma bdd_below_Ioi : bdd_below (Ioi a) := ⟨a, λ x hx, le_of_lt hx⟩ lemma lub_Iio_le (a : α) (hb : is_lub (set.Iio a) b) : b ≤ a := (is_lub_le_iff hb).mpr $ λ k hk, le_of_lt hk lemma le_glb_Ioi (a : α) (hb : is_glb (set.Ioi a) b) : a ≤ b := @lub_Iio_le αᵒᵈ _ _ a hb lemma lub_Iio_eq_self_or_Iio_eq_Iic [partial_order γ] {j : γ} (i : γ) (hj : is_lub (set.Iio i) j) : j = i ∨ set.Iio i = set.Iic j := begin cases eq_or_lt_of_le (lub_Iio_le i hj) with hj_eq_i hj_lt_i, { exact or.inl hj_eq_i, }, { right, exact set.ext (λ k, ⟨λ hk_lt, hj.1 hk_lt, λ hk_le_j, lt_of_le_of_lt hk_le_j hj_lt_i⟩), }, end lemma glb_Ioi_eq_self_or_Ioi_eq_Ici [partial_order γ] {j : γ} (i : γ) (hj : is_glb (set.Ioi i) j) : j = i ∨ set.Ioi i = set.Ici j := @lub_Iio_eq_self_or_Iio_eq_Iic γᵒᵈ _ j i hj section variables [linear_order γ] lemma exists_lub_Iio (i : γ) : ∃ j, is_lub (set.Iio i) j := begin by_cases h_exists_lt : ∃ j, j ∈ upper_bounds (set.Iio i) ∧ j < i, { obtain ⟨j, hj_ub, hj_lt_i⟩ := h_exists_lt, exact ⟨j, hj_ub, λ k hk_ub, hk_ub hj_lt_i⟩, }, { refine ⟨i, λ j hj, le_of_lt hj, _⟩, rw mem_lower_bounds, by_contra, refine h_exists_lt _, push_neg at h, exact h, }, end lemma exists_glb_Ioi (i : γ) : ∃ j, is_glb (set.Ioi i) j := @exists_lub_Iio γᵒᵈ _ i variables [densely_ordered γ] lemma is_lub_Iio {a : γ} : is_lub (Iio a) a := ⟨λ x hx, le_of_lt hx, λ y hy, le_of_forall_ge_of_dense hy⟩ lemma is_glb_Ioi {a : γ} : is_glb (Ioi a) a := @is_lub_Iio γᵒᵈ _ _ a lemma upper_bounds_Iio {a : γ} : upper_bounds (Iio a) = Ici a := is_lub_Iio.upper_bounds_eq lemma lower_bounds_Ioi {a : γ} : lower_bounds (Ioi a) = Iic a := is_glb_Ioi.lower_bounds_eq end /-! #### Singleton -/ lemma is_greatest_singleton : is_greatest {a} a := ⟨mem_singleton a, λ x hx, le_of_eq $ eq_of_mem_singleton hx⟩ lemma is_least_singleton : is_least {a} a := @is_greatest_singleton αᵒᵈ _ a lemma is_lub_singleton : is_lub {a} a := is_greatest_singleton.is_lub lemma is_glb_singleton : is_glb {a} a := is_least_singleton.is_glb lemma bdd_above_singleton : bdd_above ({a} : set α) := is_lub_singleton.bdd_above lemma bdd_below_singleton : bdd_below ({a} : set α) := is_glb_singleton.bdd_below @[simp] lemma upper_bounds_singleton : upper_bounds {a} = Ici a := is_lub_singleton.upper_bounds_eq @[simp] lemma lower_bounds_singleton : lower_bounds {a} = Iic a := is_glb_singleton.lower_bounds_eq /-! #### Bounded intervals -/ lemma bdd_above_Icc : bdd_above (Icc a b) := ⟨b, λ _, and.right⟩ lemma bdd_below_Icc : bdd_below (Icc a b) := ⟨a, λ _, and.left⟩ lemma bdd_above_Ico : bdd_above (Ico a b) := bdd_above_Icc.mono Ico_subset_Icc_self lemma bdd_below_Ico : bdd_below (Ico a b) := bdd_below_Icc.mono Ico_subset_Icc_self lemma bdd_above_Ioc : bdd_above (Ioc a b) := bdd_above_Icc.mono Ioc_subset_Icc_self lemma bdd_below_Ioc : bdd_below (Ioc a b) := bdd_below_Icc.mono Ioc_subset_Icc_self lemma bdd_above_Ioo : bdd_above (Ioo a b) := bdd_above_Icc.mono Ioo_subset_Icc_self lemma bdd_below_Ioo : bdd_below (Ioo a b) := bdd_below_Icc.mono Ioo_subset_Icc_self lemma is_greatest_Icc (h : a ≤ b) : is_greatest (Icc a b) b := ⟨right_mem_Icc.2 h, λ x, and.right⟩ lemma is_lub_Icc (h : a ≤ b) : is_lub (Icc a b) b := (is_greatest_Icc h).is_lub lemma upper_bounds_Icc (h : a ≤ b) : upper_bounds (Icc a b) = Ici b := (is_lub_Icc h).upper_bounds_eq lemma is_least_Icc (h : a ≤ b) : is_least (Icc a b) a := ⟨left_mem_Icc.2 h, λ x, and.left⟩ lemma is_glb_Icc (h : a ≤ b) : is_glb (Icc a b) a := (is_least_Icc h).is_glb lemma lower_bounds_Icc (h : a ≤ b) : lower_bounds (Icc a b) = Iic a := (is_glb_Icc h).lower_bounds_eq lemma is_greatest_Ioc (h : a < b) : is_greatest (Ioc a b) b := ⟨right_mem_Ioc.2 h, λ x, and.right⟩ lemma is_lub_Ioc (h : a < b) : is_lub (Ioc a b) b := (is_greatest_Ioc h).is_lub lemma upper_bounds_Ioc (h : a < b) : upper_bounds (Ioc a b) = Ici b := (is_lub_Ioc h).upper_bounds_eq lemma is_least_Ico (h : a < b) : is_least (Ico a b) a := ⟨left_mem_Ico.2 h, λ x, and.left⟩ lemma is_glb_Ico (h : a < b) : is_glb (Ico a b) a := (is_least_Ico h).is_glb lemma lower_bounds_Ico (h : a < b) : lower_bounds (Ico a b) = Iic a := (is_glb_Ico h).lower_bounds_eq section variables [semilattice_sup γ] [densely_ordered γ] lemma is_glb_Ioo {a b : γ} (h : a < b) : is_glb (Ioo a b) a := ⟨λ x hx, hx.1.le, λ x hx, begin cases eq_or_lt_of_le (le_sup_right : a ≤ x ⊔ a) with h₁ h₂, { exact h₁.symm ▸ le_sup_left }, obtain ⟨y, lty, ylt⟩ := exists_between h₂, apply (not_lt_of_le (sup_le (hx ⟨lty, ylt.trans_le (sup_le _ h.le)⟩) lty.le) ylt).elim, obtain ⟨u, au, ub⟩ := exists_between h, apply (hx ⟨au, ub⟩).trans ub.le, end⟩ lemma lower_bounds_Ioo {a b : γ} (hab : a < b) : lower_bounds (Ioo a b) = Iic a := (is_glb_Ioo hab).lower_bounds_eq lemma is_glb_Ioc {a b : γ} (hab : a < b) : is_glb (Ioc a b) a := (is_glb_Ioo hab).of_subset_of_superset (is_glb_Icc hab.le) Ioo_subset_Ioc_self Ioc_subset_Icc_self lemma lower_bound_Ioc {a b : γ} (hab : a < b) : lower_bounds (Ioc a b) = Iic a := (is_glb_Ioc hab).lower_bounds_eq end section variables [semilattice_inf γ] [densely_ordered γ] lemma is_lub_Ioo {a b : γ} (hab : a < b) : is_lub (Ioo a b) b := by simpa only [dual_Ioo] using is_glb_Ioo hab.dual lemma upper_bounds_Ioo {a b : γ} (hab : a < b) : upper_bounds (Ioo a b) = Ici b := (is_lub_Ioo hab).upper_bounds_eq lemma is_lub_Ico {a b : γ} (hab : a < b) : is_lub (Ico a b) b := by simpa only [dual_Ioc] using is_glb_Ioc hab.dual lemma upper_bounds_Ico {a b : γ} (hab : a < b) : upper_bounds (Ico a b) = Ici b := (is_lub_Ico hab).upper_bounds_eq end lemma bdd_below_iff_subset_Ici : bdd_below s ↔ ∃ a, s ⊆ Ici a := iff.rfl lemma bdd_above_iff_subset_Iic : bdd_above s ↔ ∃ a, s ⊆ Iic a := iff.rfl lemma bdd_below_bdd_above_iff_subset_Icc : bdd_below s ∧ bdd_above s ↔ ∃ a b, s ⊆ Icc a b := by simp only [Ici_inter_Iic.symm, subset_inter_iff, bdd_below_iff_subset_Ici, bdd_above_iff_subset_Iic, exists_and_distrib_left, exists_and_distrib_right] /-! #### Univ -/ lemma is_greatest_univ [preorder γ] [order_top γ] : is_greatest (univ : set γ) ⊤ := ⟨mem_univ _, λ x hx, le_top⟩ @[simp] lemma order_top.upper_bounds_univ [partial_order γ] [order_top γ] : upper_bounds (univ : set γ) = {⊤} := by rw [is_greatest_univ.upper_bounds_eq, Ici_top] lemma is_lub_univ [preorder γ] [order_top γ] : is_lub (univ : set γ) ⊤ := is_greatest_univ.is_lub @[simp] lemma order_bot.lower_bounds_univ [partial_order γ] [order_bot γ] : lower_bounds (univ : set γ) = {⊥} := @order_top.upper_bounds_univ γᵒᵈ _ _ lemma is_least_univ [preorder γ] [order_bot γ] : is_least (univ : set γ) ⊥ := @is_greatest_univ γᵒᵈ _ _ lemma is_glb_univ [preorder γ] [order_bot γ] : is_glb (univ : set γ) ⊥ := is_least_univ.is_glb @[simp] lemma no_max_order.upper_bounds_univ [no_max_order α] : upper_bounds (univ : set α) = ∅ := eq_empty_of_subset_empty $ λ b hb, let ⟨x, hx⟩ := exists_gt b in not_le_of_lt hx (hb trivial) @[simp] lemma no_min_order.lower_bounds_univ [no_min_order α] : lower_bounds (univ : set α) = ∅ := @no_max_order.upper_bounds_univ αᵒᵈ _ _ @[simp] lemma not_bdd_above_univ [no_max_order α] : ¬bdd_above (univ : set α) := by simp [bdd_above] @[simp] lemma not_bdd_below_univ [no_min_order α] : ¬bdd_below (univ : set α) := @not_bdd_above_univ αᵒᵈ _ _ /-! #### Empty set -/ @[simp] lemma upper_bounds_empty : upper_bounds (∅ : set α) = univ := by simp only [upper_bounds, eq_univ_iff_forall, mem_set_of_eq, ball_empty_iff, forall_true_iff] @[simp] lemma lower_bounds_empty : lower_bounds (∅ : set α) = univ := @upper_bounds_empty αᵒᵈ _ @[simp] lemma bdd_above_empty [nonempty α] : bdd_above (∅ : set α) := by simp only [bdd_above, upper_bounds_empty, univ_nonempty] @[simp] lemma bdd_below_empty [nonempty α] : bdd_below (∅ : set α) := by simp only [bdd_below, lower_bounds_empty, univ_nonempty] lemma is_glb_empty [preorder γ] [order_top γ] : is_glb ∅ (⊤:γ) := by simp only [is_glb, lower_bounds_empty, is_greatest_univ] lemma is_lub_empty [preorder γ] [order_bot γ] : is_lub ∅ (⊥:γ) := @is_glb_empty γᵒᵈ _ _ lemma is_lub.nonempty [no_min_order α] (hs : is_lub s a) : s.nonempty := let ⟨a', ha'⟩ := exists_lt a in ne_empty_iff_nonempty.1 $ λ h, not_le_of_lt ha' $ hs.right $ by simp only [h, upper_bounds_empty] lemma is_glb.nonempty [no_max_order α] (hs : is_glb s a) : s.nonempty := hs.dual.nonempty lemma nonempty_of_not_bdd_above [ha : nonempty α] (h : ¬bdd_above s) : s.nonempty := nonempty.elim ha $ λ x, (not_bdd_above_iff'.1 h x).imp $ λ a ha, ha.fst lemma nonempty_of_not_bdd_below [ha : nonempty α] (h : ¬bdd_below s) : s.nonempty := @nonempty_of_not_bdd_above αᵒᵈ _ _ _ h /-! #### insert -/ /-- Adding a point to a set preserves its boundedness above. -/ @[simp] lemma bdd_above_insert [semilattice_sup γ] (a : γ) {s : set γ} : bdd_above (insert a s) ↔ bdd_above s := by simp only [insert_eq, bdd_above_union, bdd_above_singleton, true_and] lemma bdd_above.insert [semilattice_sup γ] (a : γ) {s : set γ} (hs : bdd_above s) : bdd_above (insert a s) := (bdd_above_insert a).2 hs /--Adding a point to a set preserves its boundedness below.-/ @[simp] lemma bdd_below_insert [semilattice_inf γ] (a : γ) {s : set γ} : bdd_below (insert a s) ↔ bdd_below s := by simp only [insert_eq, bdd_below_union, bdd_below_singleton, true_and] lemma bdd_below.insert [semilattice_inf γ] (a : γ) {s : set γ} (hs : bdd_below s) : bdd_below (insert a s) := (bdd_below_insert a).2 hs lemma is_lub.insert [semilattice_sup γ] (a) {b} {s : set γ} (hs : is_lub s b) : is_lub (insert a s) (a ⊔ b) := by { rw insert_eq, exact is_lub_singleton.union hs } lemma is_glb.insert [semilattice_inf γ] (a) {b} {s : set γ} (hs : is_glb s b) : is_glb (insert a s) (a ⊓ b) := by { rw insert_eq, exact is_glb_singleton.union hs } lemma is_greatest.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_greatest s b) : is_greatest (insert a s) (max a b) := by { rw insert_eq, exact is_greatest_singleton.union hs } lemma is_least.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_least s b) : is_least (insert a s) (min a b) := by { rw insert_eq, exact is_least_singleton.union hs } @[simp] lemma upper_bounds_insert (a : α) (s : set α) : upper_bounds (insert a s) = Ici a ∩ upper_bounds s := by rw [insert_eq, upper_bounds_union, upper_bounds_singleton] @[simp] lemma lower_bounds_insert (a : α) (s : set α) : lower_bounds (insert a s) = Iic a ∩ lower_bounds s := by rw [insert_eq, lower_bounds_union, lower_bounds_singleton] /-- When there is a global maximum, every set is bounded above. -/ @[simp] protected lemma order_top.bdd_above [preorder γ] [order_top γ] (s : set γ) : bdd_above s := ⟨⊤, λ a ha, order_top.le_top a⟩ /-- When there is a global minimum, every set is bounded below. -/ @[simp] protected lemma order_bot.bdd_below [preorder γ] [order_bot γ] (s : set γ) : bdd_below s := ⟨⊥, λ a ha, order_bot.bot_le a⟩ /-! #### Pair -/ lemma is_lub_pair [semilattice_sup γ] {a b : γ} : is_lub {a, b} (a ⊔ b) := is_lub_singleton.insert _ lemma is_glb_pair [semilattice_inf γ] {a b : γ} : is_glb {a, b} (a ⊓ b) := is_glb_singleton.insert _ lemma is_least_pair [linear_order γ] {a b : γ} : is_least {a, b} (min a b) := is_least_singleton.insert _ lemma is_greatest_pair [linear_order γ] {a b : γ} : is_greatest {a, b} (max a b) := is_greatest_singleton.insert _ /-! #### Lower/upper bounds -/ @[simp] lemma is_lub_lower_bounds : is_lub (lower_bounds s) a ↔ is_glb s a := ⟨λ H, ⟨λ x hx, H.2 $ subset_upper_bounds_lower_bounds s hx, H.1⟩, is_greatest.is_lub⟩ @[simp] lemma is_glb_upper_bounds : is_glb (upper_bounds s) a ↔ is_lub s a := @is_lub_lower_bounds αᵒᵈ _ _ _ end /-! ### (In)equalities with the least upper bound and the greatest lower bound -/ section preorder variables [preorder α] {s : set α} {a b : α} lemma lower_bounds_le_upper_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) : s.nonempty → a ≤ b | ⟨c, hc⟩ := le_trans (ha hc) (hb hc) lemma is_glb_le_is_lub (ha : is_glb s a) (hb : is_lub s b) (hs : s.nonempty) : a ≤ b := lower_bounds_le_upper_bounds ha.1 hb.1 hs lemma is_lub_lt_iff (ha : is_lub s a) : a < b ↔ ∃ c ∈ upper_bounds s, c < b := ⟨λ hb, ⟨a, ha.1, hb⟩, λ ⟨c, hcs, hcb⟩, lt_of_le_of_lt (ha.2 hcs) hcb⟩ lemma lt_is_glb_iff (ha : is_glb s a) : b < a ↔ ∃ c ∈ lower_bounds s, b < c := is_lub_lt_iff ha.dual lemma le_of_is_lub_le_is_glb {x y} (ha : is_glb s a) (hb : is_lub s b) (hab : b ≤ a) (hx : x ∈ s) (hy : y ∈ s) : x ≤ y := calc x ≤ b : hb.1 hx ... ≤ a : hab ... ≤ y : ha.1 hy end preorder section partial_order variables [partial_order α] {s : set α} {a b : α} lemma is_least.unique (Ha : is_least s a) (Hb : is_least s b) : a = b := le_antisymm (Ha.right Hb.left) (Hb.right Ha.left) lemma is_least.is_least_iff_eq (Ha : is_least s a) : is_least s b ↔ a = b := iff.intro Ha.unique (λ h, h ▸ Ha) lemma is_greatest.unique (Ha : is_greatest s a) (Hb : is_greatest s b) : a = b := le_antisymm (Hb.right Ha.left) (Ha.right Hb.left) lemma is_greatest.is_greatest_iff_eq (Ha : is_greatest s a) : is_greatest s b ↔ a = b := iff.intro Ha.unique (λ h, h ▸ Ha) lemma is_lub.unique (Ha : is_lub s a) (Hb : is_lub s b) : a = b := Ha.unique Hb lemma is_glb.unique (Ha : is_glb s a) (Hb : is_glb s b) : a = b := Ha.unique Hb lemma set.subsingleton_of_is_lub_le_is_glb (Ha : is_glb s a) (Hb : is_lub s b) (hab : b ≤ a) : s.subsingleton := λ x hx y hy, le_antisymm (le_of_is_lub_le_is_glb Ha Hb hab hx hy) (le_of_is_lub_le_is_glb Ha Hb hab hy hx) lemma is_glb_lt_is_lub_of_ne (Ha : is_glb s a) (Hb : is_lub s b) {x y} (Hx : x ∈ s) (Hy : y ∈ s) (Hxy : x ≠ y) : a < b := lt_iff_le_not_le.2 ⟨lower_bounds_le_upper_bounds Ha.1 Hb.1 ⟨x, Hx⟩, λ hab, Hxy $ set.subsingleton_of_is_lub_le_is_glb Ha Hb hab Hx Hy⟩ end partial_order section linear_order variables [linear_order α] {s : set α} {a b : α} lemma lt_is_lub_iff (h : is_lub s a) : b < a ↔ ∃ c ∈ s, b < c := by simp only [← not_le, is_lub_le_iff h, mem_upper_bounds, not_forall] lemma is_glb_lt_iff (h : is_glb s a) : a < b ↔ ∃ c ∈ s, c < b := lt_is_lub_iff h.dual lemma is_lub.exists_between (h : is_lub s a) (hb : b < a) : ∃ c ∈ s, b < c ∧ c ≤ a := let ⟨c, hcs, hbc⟩ := (lt_is_lub_iff h).1 hb in ⟨c, hcs, hbc, h.1 hcs⟩ lemma is_lub.exists_between' (h : is_lub s a) (h' : a ∉ s) (hb : b < a) : ∃ c ∈ s, b < c ∧ c < a := let ⟨c, hcs, hbc, hca⟩ := h.exists_between hb in ⟨c, hcs, hbc, hca.lt_of_ne $ λ hac, h' $ hac ▸ hcs⟩ lemma is_glb.exists_between (h : is_glb s a) (hb : a < b) : ∃ c ∈ s, a ≤ c ∧ c < b := let ⟨c, hcs, hbc⟩ := (is_glb_lt_iff h).1 hb in ⟨c, hcs, h.1 hcs, hbc⟩ lemma is_glb.exists_between' (h : is_glb s a) (h' : a ∉ s) (hb : a < b) : ∃ c ∈ s, a < c ∧ c < b := let ⟨c, hcs, hac, hcb⟩ := h.exists_between hb in ⟨c, hcs, hac.lt_of_ne $ λ hac, h' $ hac.symm ▸ hcs, hcb⟩ end linear_order /-! ### Least upper bound and the greatest lower bound in linear ordered additive commutative groups -/ section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] {s : set α} {a ε : α} lemma is_glb.exists_between_self_add (h : is_glb s a) (hε : 0 < ε) : ∃ b ∈ s, a ≤ b ∧ b < a + ε := h.exists_between $ lt_add_of_pos_right _ hε lemma is_glb.exists_between_self_add' (h : is_glb s a) (h₂ : a ∉ s) (hε : 0 < ε) : ∃ b ∈ s, a < b ∧ b < a + ε := h.exists_between' h₂ $ lt_add_of_pos_right _ hε lemma is_lub.exists_between_sub_self (h : is_lub s a) (hε : 0 < ε) : ∃ b ∈ s, a - ε < b ∧ b ≤ a := h.exists_between $ sub_lt_self _ hε lemma is_lub.exists_between_sub_self' (h : is_lub s a) (h₂ : a ∉ s) (hε : 0 < ε) : ∃ b ∈ s, a - ε < b ∧ b < a := h.exists_between' h₂ $ sub_lt_self _ hε end linear_ordered_add_comm_group /-! ### Images of upper/lower bounds under monotone functions -/ namespace monotone_on variables [preorder α] [preorder β] {f : α → β} {s t : set α} (Hf : monotone_on f t) {a : α} (Hst : s ⊆ t) include Hf lemma mem_upper_bounds_image (Has : a ∈ upper_bounds s) (Hat : a ∈ t) : f a ∈ upper_bounds (f '' s) := ball_image_of_ball (λ x H, Hf (Hst H) Hat (Has H)) lemma mem_upper_bounds_image_self : a ∈ upper_bounds t → a ∈ t → f a ∈ upper_bounds (f '' t) := Hf.mem_upper_bounds_image subset_rfl lemma mem_lower_bounds_image (Has : a ∈ lower_bounds s) (Hat : a ∈ t) : f a ∈ lower_bounds (f '' s) := ball_image_of_ball (λ x H, Hf Hat (Hst H) (Has H)) lemma mem_lower_bounds_image_self : a ∈ lower_bounds t → a ∈ t → f a ∈ lower_bounds (f '' t) := Hf.mem_lower_bounds_image subset_rfl lemma image_upper_bounds_subset_upper_bounds_image (Hst : s ⊆ t) : f '' (upper_bounds s ∩ t) ⊆ upper_bounds (f '' s) := by { rintro _ ⟨a, ha, rfl⟩, exact Hf.mem_upper_bounds_image Hst ha.1 ha.2 } lemma image_lower_bounds_subset_lower_bounds_image : f '' (lower_bounds s ∩ t) ⊆ lower_bounds (f '' s) := Hf.dual.image_upper_bounds_subset_upper_bounds_image Hst /-- The image under a monotone function on a set `t` of a subset which has an upper bound in `t` is bounded above. -/ lemma map_bdd_above : (upper_bounds s ∩ t).nonempty → bdd_above (f '' s) := λ ⟨C, hs, ht⟩, ⟨f C, Hf.mem_upper_bounds_image Hst hs ht⟩ /-- The image under a monotone function on a set `t` of a subset which has a lower bound in `t` is bounded below. -/ lemma map_bdd_below : (lower_bounds s ∩ t).nonempty → bdd_below (f '' s) := λ ⟨C, hs, ht⟩, ⟨f C, Hf.mem_lower_bounds_image Hst hs ht⟩ /-- A monotone map sends a least element of a set to a least element of its image. -/ lemma map_is_least (Ha : is_least t a) : is_least (f '' t) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_lower_bounds_image_self Ha.2 Ha.1⟩ /-- A monotone map sends a greatest element of a set to a greatest element of its image. -/ lemma map_is_greatest (Ha : is_greatest t a) : is_greatest (f '' t) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_upper_bounds_image_self Ha.2 Ha.1⟩ end monotone_on namespace antitone_on variables [preorder α] [preorder β] {f : α → β} {s t : set α} (Hf : antitone_on f t) {a : α} (Hst : s ⊆ t) include Hf lemma mem_upper_bounds_image (Has : a ∈ lower_bounds s) : a ∈ t → f a ∈ upper_bounds (f '' s) := Hf.dual_right.mem_lower_bounds_image Hst Has lemma mem_upper_bounds_image_self : a ∈ lower_bounds t → a ∈ t → f a ∈ upper_bounds (f '' t) := Hf.dual_right.mem_lower_bounds_image_self lemma mem_lower_bounds_image : a ∈ upper_bounds s → a ∈ t → f a ∈ lower_bounds (f '' s) := Hf.dual_right.mem_upper_bounds_image Hst lemma mem_lower_bounds_image_self : a ∈ upper_bounds t → a ∈ t → f a ∈ lower_bounds (f '' t) := Hf.dual_right.mem_upper_bounds_image_self lemma image_lower_bounds_subset_upper_bounds_image : f '' (lower_bounds s ∩ t) ⊆ upper_bounds (f '' s) := Hf.dual_right.image_lower_bounds_subset_lower_bounds_image Hst lemma image_upper_bounds_subset_lower_bounds_image : f '' (upper_bounds s ∩ t) ⊆ lower_bounds (f '' s) := Hf.dual_right.image_upper_bounds_subset_upper_bounds_image Hst /-- The image under an antitone function of a set which is bounded above is bounded below. -/ lemma map_bdd_above : (upper_bounds s ∩ t).nonempty → bdd_below (f '' s) := Hf.dual_right.map_bdd_above Hst /-- The image under an antitone function of a set which is bounded below is bounded above. -/ lemma map_bdd_below : (lower_bounds s ∩ t).nonempty → bdd_above (f '' s) := Hf.dual_right.map_bdd_below Hst /-- An antitone map sends a greatest element of a set to a least element of its image. -/ lemma map_is_greatest : is_greatest t a → is_least (f '' t) (f a) := Hf.dual_right.map_is_greatest /-- An antitone map sends a least element of a set to a greatest element of its image. -/ lemma map_is_least : is_least t a → is_greatest (f '' t) (f a) := Hf.dual_right.map_is_least end antitone_on namespace monotone variables [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} include Hf lemma mem_upper_bounds_image (Ha : a ∈ upper_bounds s) : f a ∈ upper_bounds (f '' s) := ball_image_of_ball (λ x H, Hf (Ha H)) lemma mem_lower_bounds_image (Ha : a ∈ lower_bounds s) : f a ∈ lower_bounds (f '' s) := ball_image_of_ball (λ x H, Hf (Ha H)) lemma image_upper_bounds_subset_upper_bounds_image : f '' upper_bounds s ⊆ upper_bounds (f '' s) := by { rintro _ ⟨a, ha, rfl⟩, exact Hf.mem_upper_bounds_image ha } lemma image_lower_bounds_subset_lower_bounds_image : f '' lower_bounds s ⊆ lower_bounds (f '' s) := Hf.dual.image_upper_bounds_subset_upper_bounds_image /-- The image under a monotone function of a set which is bounded above is bounded above. See also `bdd_above.image2`. -/ lemma map_bdd_above : bdd_above s → bdd_above (f '' s) | ⟨C, hC⟩ := ⟨f C, Hf.mem_upper_bounds_image hC⟩ /-- The image under a monotone function of a set which is bounded below is bounded below. See also `bdd_below.image2`. -/ lemma map_bdd_below : bdd_below s → bdd_below (f '' s) | ⟨C, hC⟩ := ⟨f C, Hf.mem_lower_bounds_image hC⟩ /-- A monotone map sends a least element of a set to a least element of its image. -/ lemma map_is_least (Ha : is_least s a) : is_least (f '' s) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_lower_bounds_image Ha.2⟩ /-- A monotone map sends a greatest element of a set to a greatest element of its image. -/ lemma map_is_greatest (Ha : is_greatest s a) : is_greatest (f '' s) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_upper_bounds_image Ha.2⟩ end monotone namespace antitone variables [preorder α] [preorder β] {f : α → β} (hf : antitone f) {a : α} {s : set α} lemma mem_upper_bounds_image : a ∈ lower_bounds s → f a ∈ upper_bounds (f '' s) := hf.dual_right.mem_lower_bounds_image lemma mem_lower_bounds_image : a ∈ upper_bounds s → f a ∈ lower_bounds (f '' s) := hf.dual_right.mem_upper_bounds_image lemma image_lower_bounds_subset_upper_bounds_image : f '' lower_bounds s ⊆ upper_bounds (f '' s) := hf.dual_right.image_lower_bounds_subset_lower_bounds_image lemma image_upper_bounds_subset_lower_bounds_image : f '' upper_bounds s ⊆ lower_bounds (f '' s) := hf.dual_right.image_upper_bounds_subset_upper_bounds_image /-- The image under an antitone function of a set which is bounded above is bounded below. -/ lemma map_bdd_above : bdd_above s → bdd_below (f '' s) := hf.dual_right.map_bdd_above /-- The image under an antitone function of a set which is bounded below is bounded above. -/ lemma map_bdd_below : bdd_below s → bdd_above (f '' s) := hf.dual_right.map_bdd_below /-- An antitone map sends a greatest element of a set to a least element of its image. -/ lemma map_is_greatest : is_greatest s a → is_least (f '' s) (f a) := hf.dual_right.map_is_greatest /-- An antitone map sends a least element of a set to a greatest element of its image. -/ lemma map_is_least : is_least s a → is_greatest (f '' s) (f a) := hf.dual_right.map_is_least end antitone section image2 variables [preorder α] [preorder β] [preorder γ] {f : α → β → γ} {s : set α} {t : set β} {a : α} {b : β} section monotone_monotone variables (h₀ : ∀ b, monotone (swap f b)) (h₁ : ∀ a, monotone (f a)) include h₀ h₁ lemma mem_upper_bounds_image2 (ha : a ∈ upper_bounds s) (hb : b ∈ upper_bounds t) : f a b ∈ upper_bounds (image2 f s t) := forall_image2_iff.2 $ λ x hx y hy, (h₀ _ $ ha hx).trans $ h₁ _ $ hb hy lemma mem_lower_bounds_image2 (ha : a ∈ lower_bounds s) (hb : b ∈ lower_bounds t) : f a b ∈ lower_bounds (image2 f s t) := forall_image2_iff.2 $ λ x hx y hy, (h₀ _ $ ha hx).trans $ h₁ _ $ hb hy lemma image2_upper_bounds_upper_bounds_subset : image2 f (upper_bounds s) (upper_bounds t) ⊆ upper_bounds (image2 f s t) := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_upper_bounds_image2 h₀ h₁ ha hb } lemma image2_lower_bounds_lower_bounds_subset : image2 f (lower_bounds s) (lower_bounds t) ⊆ lower_bounds (image2 f s t) := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_lower_bounds_image2 h₀ h₁ ha hb } /-- See also `monotone.map_bdd_above`. -/ lemma bdd_above.image2 : bdd_above s → bdd_above t → bdd_above (image2 f s t) := by { rintro ⟨a, ha⟩ ⟨b, hb⟩, exact ⟨f a b, mem_upper_bounds_image2 h₀ h₁ ha hb⟩ } /-- See also `monotone.map_bdd_below`. -/ lemma bdd_below.image2 : bdd_below s → bdd_below t → bdd_below (image2 f s t) := by { rintro ⟨a, ha⟩ ⟨b, hb⟩, exact ⟨f a b, mem_lower_bounds_image2 h₀ h₁ ha hb⟩ } lemma is_greatest.image2 (ha : is_greatest s a) (hb : is_greatest t b) : is_greatest (image2 f s t) (f a b) := ⟨mem_image2_of_mem ha.1 hb.1, mem_upper_bounds_image2 h₀ h₁ ha.2 hb.2⟩ lemma is_least.image2 (ha : is_least s a) (hb : is_least t b) : is_least (image2 f s t) (f a b) := ⟨mem_image2_of_mem ha.1 hb.1, mem_lower_bounds_image2 h₀ h₁ ha.2 hb.2⟩ end monotone_monotone section monotone_antitone variables (h₀ : ∀ b, monotone (swap f b)) (h₁ : ∀ a, antitone (f a)) include h₀ h₁ lemma mem_upper_bounds_image2_of_mem_upper_bounds_of_mem_lower_bounds (ha : a ∈ upper_bounds s) (hb : b ∈ lower_bounds t) : f a b ∈ upper_bounds (image2 f s t) := forall_image2_iff.2 $ λ x hx y hy, (h₀ _ $ ha hx).trans $ h₁ _ $ hb hy lemma mem_lower_bounds_image2_of_mem_lower_bounds_of_mem_upper_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds t) : f a b ∈ lower_bounds (image2 f s t) := forall_image2_iff.2 $ λ x hx y hy, (h₀ _ $ ha hx).trans $ h₁ _ $ hb hy lemma image2_upper_bounds_lower_bounds_subset_upper_bounds_image2 : image2 f (upper_bounds s) (lower_bounds t) ⊆ upper_bounds (image2 f s t) := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_upper_bounds_image2_of_mem_upper_bounds_of_mem_lower_bounds h₀ h₁ ha hb } lemma image2_lower_bounds_upper_bounds_subset_lower_bounds_image2 : image2 f (lower_bounds s) (upper_bounds t) ⊆ lower_bounds (image2 f s t) := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_lower_bounds_image2_of_mem_lower_bounds_of_mem_upper_bounds h₀ h₁ ha hb } lemma bdd_above.bdd_above_image2_of_bdd_below : bdd_above s → bdd_below t → bdd_above (image2 f s t) := by { rintro ⟨a, ha⟩ ⟨b, hb⟩, exact ⟨f a b, mem_upper_bounds_image2_of_mem_upper_bounds_of_mem_lower_bounds h₀ h₁ ha hb⟩ } lemma bdd_below.bdd_below_image2_of_bdd_above : bdd_below s → bdd_above t → bdd_below (image2 f s t) := by { rintro ⟨a, ha⟩ ⟨b, hb⟩, exact ⟨f a b, mem_lower_bounds_image2_of_mem_lower_bounds_of_mem_upper_bounds h₀ h₁ ha hb⟩ } lemma is_greatest.is_greatest_image2_of_is_least (ha : is_greatest s a) (hb : is_least t b) : is_greatest (image2 f s t) (f a b) := ⟨mem_image2_of_mem ha.1 hb.1, mem_upper_bounds_image2_of_mem_upper_bounds_of_mem_lower_bounds h₀ h₁ ha.2 hb.2⟩ lemma is_least.is_least_image2_of_is_greatest (ha : is_least s a) (hb : is_greatest t b) : is_least (image2 f s t) (f a b) := ⟨mem_image2_of_mem ha.1 hb.1, mem_lower_bounds_image2_of_mem_lower_bounds_of_mem_upper_bounds h₀ h₁ ha.2 hb.2⟩ end monotone_antitone section antitone_antitone variables (h₀ : ∀ b, antitone (swap f b)) (h₁ : ∀ a, antitone (f a)) include h₀ h₁ lemma mem_upper_bounds_image2_of_mem_lower_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ lower_bounds t) : f a b ∈ upper_bounds (image2 f s t) := forall_image2_iff.2 $ λ x hx y hy, (h₀ _ $ ha hx).trans $ h₁ _ $ hb hy lemma mem_lower_bounds_image2_of_mem_upper_bounds (ha : a ∈ upper_bounds s) (hb : b ∈ upper_bounds t) : f a b ∈ lower_bounds (image2 f s t) := forall_image2_iff.2 $ λ x hx y hy, (h₀ _ $ ha hx).trans $ h₁ _ $ hb hy lemma image2_upper_bounds_upper_bounds_subset_upper_bounds_image2 : image2 f (lower_bounds s) (lower_bounds t) ⊆ upper_bounds (image2 f s t) := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_upper_bounds_image2_of_mem_lower_bounds h₀ h₁ ha hb } lemma image2_lower_bounds_lower_bounds_subset_lower_bounds_image2 : image2 f (upper_bounds s) (upper_bounds t) ⊆ lower_bounds (image2 f s t) := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_lower_bounds_image2_of_mem_upper_bounds h₀ h₁ ha hb } lemma bdd_below.image2_bdd_above : bdd_below s → bdd_below t → bdd_above (image2 f s t) := by { rintro ⟨a, ha⟩ ⟨b, hb⟩, exact ⟨f a b, mem_upper_bounds_image2_of_mem_lower_bounds h₀ h₁ ha hb⟩ } lemma bdd_above.image2_bdd_below : bdd_above s → bdd_above t → bdd_below (image2 f s t) := by { rintro ⟨a, ha⟩ ⟨b, hb⟩, exact ⟨f a b, mem_lower_bounds_image2_of_mem_upper_bounds h₀ h₁ ha hb⟩ } lemma is_least.is_greatest_image2 (ha : is_least s a) (hb : is_least t b) : is_greatest (image2 f s t) (f a b) := ⟨mem_image2_of_mem ha.1 hb.1, mem_upper_bounds_image2_of_mem_lower_bounds h₀ h₁ ha.2 hb.2⟩ lemma is_greatest.is_least_image2 (ha : is_greatest s a) (hb : is_greatest t b) : is_least (image2 f s t) (f a b) := ⟨mem_image2_of_mem ha.1 hb.1, mem_lower_bounds_image2_of_mem_upper_bounds h₀ h₁ ha.2 hb.2⟩ end antitone_antitone section antitone_monotone variables (h₀ : ∀ b, antitone (swap f b)) (h₁ : ∀ a, monotone (f a)) include h₀ h₁ lemma mem_upper_bounds_image2_of_mem_upper_bounds_of_mem_upper_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds t) : f a b ∈ upper_bounds (image2 f s t) := forall_image2_iff.2 $ λ x hx y hy, (h₀ _ $ ha hx).trans $ h₁ _ $ hb hy lemma mem_lower_bounds_image2_of_mem_lower_bounds_of_mem_lower_bounds (ha : a ∈ upper_bounds s) (hb : b ∈ lower_bounds t) : f a b ∈ lower_bounds (image2 f s t) := forall_image2_iff.2 $ λ x hx y hy, (h₀ _ $ ha hx).trans $ h₁ _ $ hb hy lemma image2_lower_bounds_upper_bounds_subset_upper_bounds_image2 : image2 f (lower_bounds s) (upper_bounds t) ⊆ upper_bounds (image2 f s t) := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_upper_bounds_image2_of_mem_upper_bounds_of_mem_upper_bounds h₀ h₁ ha hb } lemma image2_upper_bounds_lower_bounds_subset_lower_bounds_image2 : image2 f (upper_bounds s) (lower_bounds t) ⊆ lower_bounds (image2 f s t) := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_lower_bounds_image2_of_mem_lower_bounds_of_mem_lower_bounds h₀ h₁ ha hb } lemma bdd_below.bdd_above_image2_of_bdd_above : bdd_below s → bdd_above t → bdd_above (image2 f s t) := by { rintro ⟨a, ha⟩ ⟨b, hb⟩, exact ⟨f a b, mem_upper_bounds_image2_of_mem_upper_bounds_of_mem_upper_bounds h₀ h₁ ha hb⟩ } lemma bdd_above.bdd_below_image2_of_bdd_above : bdd_above s → bdd_below t → bdd_below (image2 f s t) := by { rintro ⟨a, ha⟩ ⟨b, hb⟩, exact ⟨f a b, mem_lower_bounds_image2_of_mem_lower_bounds_of_mem_lower_bounds h₀ h₁ ha hb⟩ } lemma is_least.is_greatest_image2_of_is_greatest (ha : is_least s a) (hb : is_greatest t b) : is_greatest (image2 f s t) (f a b) := ⟨mem_image2_of_mem ha.1 hb.1, mem_upper_bounds_image2_of_mem_upper_bounds_of_mem_upper_bounds h₀ h₁ ha.2 hb.2⟩ lemma is_greatest.is_least_image2_of_is_least (ha : is_greatest s a) (hb : is_least t b) : is_least (image2 f s t) (f a b) := ⟨mem_image2_of_mem ha.1 hb.1, mem_lower_bounds_image2_of_mem_lower_bounds_of_mem_lower_bounds h₀ h₁ ha.2 hb.2⟩ end antitone_monotone end image2 lemma is_glb.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_glb (f '' s) (f x)) : is_glb s x := ⟨λ y hy, hf.1 $ hx.1 $ mem_image_of_mem _ hy, λ y hy, hf.1 $ hx.2 $ monotone.mem_lower_bounds_image (λ x y, hf.2) hy⟩ lemma is_lub.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_lub (f '' s) (f x)) : is_lub s x := @is_glb.of_image αᵒᵈ βᵒᵈ _ _ f (λ x y, hf) _ _ hx lemma is_lub_pi {π : α → Type*} [Π a, preorder (π a)] {s : set (Π a, π a)} {f : Π a, π a} : is_lub s f ↔ ∀ a, is_lub (function.eval a '' s) (f a) := begin classical, refine ⟨λ H a, ⟨(function.monotone_eval a).mem_upper_bounds_image H.1, λ b hb, _⟩, λ H, ⟨_, _⟩⟩, { suffices : function.update f a b ∈ upper_bounds s, from function.update_same a b f ▸ H.2 this a, refine λ g hg, le_update_iff.2 ⟨hb $ mem_image_of_mem _ hg, λ i hi, H.1 hg i⟩ }, { exact λ g hg a, (H a).1 (mem_image_of_mem _ hg) }, { exact λ g hg a, (H a).2 ((function.monotone_eval a).mem_upper_bounds_image hg) } end lemma is_glb_pi {π : α → Type*} [Π a, preorder (π a)] {s : set (Π a, π a)} {f : Π a, π a} : is_glb s f ↔ ∀ a, is_glb (function.eval a '' s) (f a) := @is_lub_pi α (λ a, (π a)ᵒᵈ) _ s f lemma is_lub_prod [preorder α] [preorder β] {s : set (α × β)} (p : α × β) : is_lub s p ↔ is_lub (prod.fst '' s) p.1 ∧ is_lub (prod.snd '' s) p.2 := begin refine ⟨λ H, ⟨⟨monotone_fst.mem_upper_bounds_image H.1, λ a ha, _⟩, ⟨monotone_snd.mem_upper_bounds_image H.1, λ a ha, _⟩⟩, λ H, ⟨_, _⟩⟩, { suffices : (a, p.2) ∈ upper_bounds s, from (H.2 this).1, exact λ q hq, ⟨ha $ mem_image_of_mem _ hq, (H.1 hq).2⟩ }, { suffices : (p.1, a) ∈ upper_bounds s, from (H.2 this).2, exact λ q hq, ⟨(H.1 hq).1, ha $ mem_image_of_mem _ hq⟩ }, { exact λ q hq, ⟨H.1.1 $ mem_image_of_mem _ hq, H.2.1 $ mem_image_of_mem _ hq⟩ }, { exact λ q hq, ⟨H.1.2 $ monotone_fst.mem_upper_bounds_image hq, H.2.2 $ monotone_snd.mem_upper_bounds_image hq⟩ } end lemma is_glb_prod [preorder α] [preorder β] {s : set (α × β)} (p : α × β) : is_glb s p ↔ is_glb (prod.fst '' s) p.1 ∧ is_glb (prod.snd '' s) p.2 := @is_lub_prod αᵒᵈ βᵒᵈ _ _ _ _ namespace order_iso variables [preorder α] [preorder β] (f : α ≃o β) lemma upper_bounds_image {s : set α} : upper_bounds (f '' s) = f '' upper_bounds s := subset.antisymm (λ x hx, ⟨f.symm x, λ y hy, f.le_symm_apply.2 (hx $ mem_image_of_mem _ hy), f.apply_symm_apply x⟩) f.monotone.image_upper_bounds_subset_upper_bounds_image lemma lower_bounds_image {s : set α} : lower_bounds (f '' s) = f '' lower_bounds s := @upper_bounds_image αᵒᵈ βᵒᵈ _ _ f.dual _ @[simp] lemma is_lub_image {s : set α} {x : β} : is_lub (f '' s) x ↔ is_lub s (f.symm x) := ⟨λ h, is_lub.of_image (λ _ _, f.le_iff_le) ((f.apply_symm_apply x).symm ▸ h), λ h, is_lub.of_image (λ _ _, f.symm.le_iff_le) $ (f.symm_image_image s).symm ▸ h⟩ lemma is_lub_image' {s : set α} {x : α} : is_lub (f '' s) (f x) ↔ is_lub s x := by rw [is_lub_image, f.symm_apply_apply] @[simp] lemma is_glb_image {s : set α} {x : β} : is_glb (f '' s) x ↔ is_glb s (f.symm x) := f.dual.is_lub_image lemma is_glb_image' {s : set α} {x : α} : is_glb (f '' s) (f x) ↔ is_glb s x := f.dual.is_lub_image' @[simp] lemma is_lub_preimage {s : set β} {x : α} : is_lub (f ⁻¹' s) x ↔ is_lub s (f x) := by rw [← f.symm_symm, ← image_eq_preimage, is_lub_image] lemma is_lub_preimage' {s : set β} {x : β} : is_lub (f ⁻¹' s) (f.symm x) ↔ is_lub s x := by rw [is_lub_preimage, f.apply_symm_apply] @[simp] lemma is_glb_preimage {s : set β} {x : α} : is_glb (f ⁻¹' s) x ↔ is_glb s (f x) := f.dual.is_lub_preimage lemma is_glb_preimage' {s : set β} {x : β} : is_glb (f ⁻¹' s) (f.symm x) ↔ is_glb s x := f.dual.is_lub_preimage' end order_iso
0de9104342f9cd69716c0fc50b41726276a40b9c
1fbca480c1574e809ae95a3eda58188ff42a5e41
/test/tactic/basic.lean
1ef5e6be137f9916cad59f33b8606d099bbb7c6a
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
1,006
lean
import util.meta.tactic open tactic section variables x y z : ℕ include x y z example : ℕ := begin clear_except x, (do xs ← tactic.local_context, x ← get_local `x, assert (xs = [x]), return ()), exact x end end example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := begin apply_assumption, apply_assumption, end example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := by solve_by_elim example {α : Type} {p : α → Prop} (h₀ : ∀ x, p x) (y : α) : p y := begin apply_assumption, end example : (∃ x : ℕ, x = 7) := begin one_point, refl, end example (p : Prop) : (∃ x : ℕ, x = 7) ∨ p := begin one_point, left, refl, end example (p q : ℕ → Prop) : (∃ x : ℕ, (x = 7 ∧ q x) ∧ p x) ∨ p 1 := begin one_point, guard_target (7 = 7 ∧ q 7) ∧ p 7 ∨ p 1, admit end example (p q : ℕ → Prop) : (∃ x : ℕ, (p x ∧ x = 7 ∧ q x) ∧ p x) ∨ p 1 := begin one_point, guard_target (p 7 ∧ 7 = 7 ∧ q 7) ∧ p 7 ∨ p 1, admit end
029e48adf12115a7477a999e1909534896e0d80f
59a4b050600ed7b3d5826a8478db0a9bdc190252
/src/category_theory/universal/types.lean
642fa1b8454358222c1c07f33b96897b5c89af89
[]
no_license
rwbarton/lean-category-theory
f720268d800b62a25d69842ca7b5d27822f00652
00df814d463406b7a13a56f5dcda67758ba1b419
refs/heads/master
1,585,366,296,767
1,536,151,349,000
1,536,151,349,000
147,652,096
0
0
null
1,536,226,960,000
1,536,226,960,000
null
UTF-8
Lean
false
false
4,270
lean
import category_theory.limits universe u open category_theory open category_theory.limits namespace category_theory.universal.types local attribute [forward] fork.w square.w instance : has_terminal_object.{u+1 u} (Type u) := { terminal := punit } instance : has_binary_products.{u+1 u} (Type u) := { prod := λ Y Z, { X := Y × Z, π₁ := prod.fst, π₂ := prod.snd } } @[simp] lemma types_prod (Y Z : Type u) : limits.prod Y Z = (Y × Z) := rfl instance : has_products.{u+1 u} (Type u) := { prod := λ β f, { X := Π b, f b, π := λ b x, x b } }. @[simp] lemma types_pi {β : Type u} (f : β → Type u) : pi f = Π b, f b := rfl @[simp] lemma types_pi_π {β : Type u} (f : β → Type u) (b : β) : pi.π f b = λ (g : Π b, f b), g b := rfl. @[simp] lemma types_pi_pre {β α : Type u} (f : α → Type u) (g : β → Type u) (h : β → α) : pi.pre f h = λ (d : Π a, f a), λ b, d (h b) := rfl @[simp] lemma types_pi_map {β : Type u} (f : β → Type u) (g : β → Type u) (k : Π b, f b ⟶ g b) : pi.map k = λ (d : Π a, f a), (λ (b : β), k b (d b)) := rfl @[simp] lemma types_pi_lift {β : Type u} (f : β → Type u) {P : Type u} (p : Π b, P ⟶ f b) : pi.lift p = λ q b, p b q := rfl instance : has_equalizers.{u+1 u} (Type u) := { equalizer := λ Y Z f g, { X := { y : Y // f y = g y }, ι := subtype.val } } instance : has_pullbacks.{u+1 u} (Type u) := { pullback := λ Y₁ Y₂ Z r₁ r₂, { X := { z : Y₁ × Y₂ // r₁ z.1 = r₂ z.2 }, π₁ := λ z, z.val.1, π₂ := λ z, z.val.2 } } local attribute [elab_with_expected_type] quot.lift instance : has_initial_object.{u+1 u} (Type u) := { initial := pempty } instance : has_binary_coproducts.{u+1 u} (Type u) := { coprod := λ Y Z, { X := Y ⊕ Z, ι₁ := sum.inl, ι₂ := sum.inr } } instance : has_coproducts.{u+1 u} (Type u) := { coprod := λ β f, { X := Σ b, f b, ι := λ b x, ⟨b, x⟩ } } def pushout {Y₁ Y₂ Z : Type u} (r₁ : Z ⟶ Y₁) (r₂ : Z ⟶ Y₂) : cosquare r₁ r₂ := { X := @quot (Y₁ ⊕ Y₂) (λ p p', ∃ z : Z, p = sum.inl (r₁ z) ∧ p' = sum.inr (r₂ z) ), ι₁ := λ o, quot.mk _ (sum.inl o), ι₂ := λ o, quot.mk _ (sum.inr o), w := funext $ λ z, quot.sound ⟨ z, by tidy ⟩, }. def pushout_is_pushout {Y₁ Y₂ Z : Type u} (r₁ : Z ⟶ Y₁) (r₂ : Z ⟶ Y₂) : is_pushout (pushout r₁ r₂) := { desc := λ s, quot.lift (λ o, sum.cases_on o s.ι₁ s.ι₂) (assume o o' ⟨z, hz⟩, begin rw hz.left, rw hz.right, dsimp, exact congr_fun s.w z end) } instance : has_pushouts.{u+1 u} (Type u) := { pushout := @pushout, is_pushout := @pushout_is_pushout } def coequalizer {Y Z : Type u} (f g : Y ⟶ Z) : cofork f g := { X := @quot Z (λ z z', ∃ y : Y, z = f y ∧ z' = g y), π := λ z, quot.mk _ z, w := funext $ λ x, quot.sound ⟨ x, by tidy ⟩ }. def coequalizer_is_coequalizer {Y Z : Type u} (f g : Y ⟶ Z) : is_coequalizer (coequalizer f g) := { desc := λ s, quot.lift (λ (z : Z), s.π z) (assume z z' ⟨y, hy⟩, begin rw hy.left, rw hy.right, exact congr_fun s.w y, end) } instance : has_coequalizers.{u+1 u} (Type u) := { coequalizer := @coequalizer, is_coequalizer := @coequalizer_is_coequalizer } variables {J : Type u} [𝒥 : small_category J] include 𝒥 def limit (F : J ⥤ Type u) : cone F := { X := {u : Π j, F j // ∀ (j j' : J) (f : j ⟶ j'), F.map f (u j) = u j'}, π := λ j u, u.val j } def limit_is_limit (F : J ⥤ Type u) : is_limit (limit F) := { lift := λ s v, ⟨λ j, s.π j v, λ j j' f, congr_fun (s.w f) _⟩ } instance : has_limits.{u+1 u} (Type u) := { limit := @limit, is_limit := @limit_is_limit } def colimit (F : J ⥤ Type u) : cocone F := { X := @quot (Σ j, F j) (λ p p', ∃ f : p.1 ⟶ p'.1, p'.2 = F.map f p.2), ι := λ j x, quot.mk _ ⟨j, x⟩, w := λ j j' f, funext $ λ x, eq.symm (quot.sound ⟨f, rfl⟩) } def colimit_is_colimit (F : J ⥤ Type u) : is_colimit (colimit F) := { desc := λ s, quot.lift (λ (p : Σ j, F j), s.ι p.1 p.2) (assume ⟨j, x⟩ ⟨j', x'⟩ ⟨f, hf⟩, by rw hf; exact (congr_fun (s.w f) x).symm) } instance : has_colimits.{u+1 u} (Type u) := { colimit := @colimit, is_colimit := @colimit_is_colimit } end category_theory.universal.types
de13e667d5a89b26cdf0e3dca11b20190c502feb
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/field_theory/polynomial_galois_group.lean
cb29cbc08bc77b5dd067d12071a5431d292f9e0b
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
21,720
lean
/- Copyright (c) 2020 Thomas Browning, Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning, Patrick Lutz -/ import analysis.complex.polynomial import field_theory.galois import group_theory.perm.cycle.type /-! # Galois Groups of Polynomials In this file, we introduce the Galois group of a polynomial `p` over a field `F`, defined as the automorphism group of its splitting field. We also provide some results about some extension `E` above `p.splitting_field`, and some specific results about the Galois groups of ℚ-polynomials with specific numbers of non-real roots. ## Main definitions - `polynomial.gal p`: the Galois group of a polynomial p. - `polynomial.gal.restrict p E`: the restriction homomorphism `(E ≃ₐ[F] E) → gal p`. - `polynomial.gal.gal_action p E`: the action of `gal p` on the roots of `p` in `E`. ## Main results - `polynomial.gal.restrict_smul`: `restrict p E` is compatible with `gal_action p E`. - `polynomial.gal.gal_action_hom_injective`: `gal p` acting on the roots of `p` in `E` is faithful. - `polynomial.gal.restrict_prod_injective`: `gal (p * q)` embeds as a subgroup of `gal p × gal q`. - `polynomial.gal.card_of_separable`: For a separable polynomial, its Galois group has cardinality equal to the dimension of its splitting field over `F`. - `polynomial.gal.gal_action_hom_bijective_of_prime_degree`: An irreducible polynomial of prime degree with two non-real roots has full Galois group. ## Other results - `polynomial.gal.card_complex_roots_eq_card_real_add_card_not_gal_inv`: The number of complex roots equals the number of real roots plus the number of roots not fixed by complex conjugation (i.e. with some imaginary component). -/ noncomputable theory open_locale classical polynomial open finite_dimensional namespace polynomial variables {F : Type*} [field F] (p q : F[X]) (E : Type*) [field E] [algebra F E] /-- The Galois group of a polynomial. -/ @[derive [group, fintype]] def gal := p.splitting_field ≃ₐ[F] p.splitting_field namespace gal instance : has_coe_to_fun p.gal (λ _, p.splitting_field → p.splitting_field) := alg_equiv.has_coe_to_fun instance apply_mul_semiring_action : mul_semiring_action p.gal p.splitting_field := alg_equiv.apply_mul_semiring_action @[ext] lemma ext {σ τ : p.gal} (h : ∀ x ∈ p.root_set p.splitting_field, σ x = τ x) : σ = τ := begin refine alg_equiv.ext (λ x, (alg_hom.mem_equalizer σ.to_alg_hom τ.to_alg_hom x).mp ((set_like.ext_iff.mp _ x).mpr algebra.mem_top)), rwa [eq_top_iff, ←splitting_field.adjoin_roots, algebra.adjoin_le_iff], end /-- If `p` splits in `F` then the `p.gal` is trivial. -/ def unique_gal_of_splits (h : p.splits (ring_hom.id F)) : unique p.gal := { default := 1, uniq := λ f, alg_equiv.ext (λ x, by { obtain ⟨y, rfl⟩ := algebra.mem_bot.mp ((set_like.ext_iff.mp ((is_splitting_field.splits_iff _ p).mp h) x).mp algebra.mem_top), rw [alg_equiv.commutes, alg_equiv.commutes] }) } instance [h : fact (p.splits (ring_hom.id F))] : unique p.gal := unique_gal_of_splits _ (h.1) instance unique_gal_zero : unique (0 : F[X]).gal := unique_gal_of_splits _ (splits_zero _) instance unique_gal_one : unique (1 : F[X]).gal := unique_gal_of_splits _ (splits_one _) instance unique_gal_C (x : F) : unique (C x).gal := unique_gal_of_splits _ (splits_C _ _) instance unique_gal_X : unique (X : F[X]).gal := unique_gal_of_splits _ (splits_X _) instance unique_gal_X_sub_C (x : F) : unique (X - C x).gal := unique_gal_of_splits _ (splits_X_sub_C _) instance unique_gal_X_pow (n : ℕ) : unique (X ^ n : F[X]).gal := unique_gal_of_splits _ (splits_X_pow _ _) instance [h : fact (p.splits (algebra_map F E))] : algebra p.splitting_field E := (is_splitting_field.lift p.splitting_field p h.1).to_ring_hom.to_algebra instance [h : fact (p.splits (algebra_map F E))] : is_scalar_tower F p.splitting_field E := is_scalar_tower.of_algebra_map_eq (λ x, ((is_splitting_field.lift p.splitting_field p h.1).commutes x).symm) -- The `algebra p.splitting_field E` instance above behaves badly when -- `E := p.splitting_field`, since it may result in a unification problem -- `is_splitting_field.lift.to_ring_hom.to_algebra =?= algebra.id`, -- which takes an extremely long time to resolve, causing timeouts. -- Since we don't really care about this definition, marking it as irreducible -- causes that unification to error out early. attribute [irreducible] gal.algebra /-- Restrict from a superfield automorphism into a member of `gal p`. -/ def restrict [fact (p.splits (algebra_map F E))] : (E ≃ₐ[F] E) →* p.gal := alg_equiv.restrict_normal_hom p.splitting_field lemma restrict_surjective [fact (p.splits (algebra_map F E))] [normal F E] : function.surjective (restrict p E) := alg_equiv.restrict_normal_hom_surjective E section roots_action /-- The function taking `roots p p.splitting_field` to `roots p E`. This is actually a bijection, see `polynomial.gal.map_roots_bijective`. -/ def map_roots [fact (p.splits (algebra_map F E))] : root_set p p.splitting_field → root_set p E := λ x, ⟨is_scalar_tower.to_alg_hom F p.splitting_field E x, begin have key := subtype.mem x, by_cases p = 0, { simp only [h, root_set_zero] at key, exact false.rec _ key }, { rw [mem_root_set h, aeval_alg_hom_apply, (mem_root_set h).mp key, alg_hom.map_zero] } end⟩ lemma map_roots_bijective [h : fact (p.splits (algebra_map F E))] : function.bijective (map_roots p E) := begin split, { exact λ _ _ h, subtype.ext (ring_hom.injective _ (subtype.ext_iff.mp h)) }, { intro y, -- this is just an equality of two different ways to write the roots of `p` as an `E`-polynomial have key := roots_map (is_scalar_tower.to_alg_hom F p.splitting_field E : p.splitting_field →+* E) ((splits_id_iff_splits _).mpr (is_splitting_field.splits p.splitting_field p)), rw [map_map, alg_hom.comp_algebra_map] at key, have hy := subtype.mem y, simp only [root_set, finset.mem_coe, multiset.mem_to_finset, key, multiset.mem_map] at hy, rcases hy with ⟨x, hx1, hx2⟩, exact ⟨⟨x, multiset.mem_to_finset.mpr hx1⟩, subtype.ext hx2⟩ } end /-- The bijection between `root_set p p.splitting_field` and `root_set p E`. -/ def roots_equiv_roots [fact (p.splits (algebra_map F E))] : (root_set p p.splitting_field) ≃ (root_set p E) := equiv.of_bijective (map_roots p E) (map_roots_bijective p E) instance gal_action_aux : mul_action p.gal (root_set p p.splitting_field) := { smul := λ ϕ x, ⟨ϕ x, begin have key := subtype.mem x, --simp only [root_set, finset.mem_coe, multiset.mem_to_finset] at *, by_cases p = 0, { simp only [h, root_set_zero] at key, exact false.rec _ key }, { rw mem_root_set h, change aeval (ϕ.to_alg_hom x) p = 0, rw [aeval_alg_hom_apply, (mem_root_set h).mp key, alg_hom.map_zero] } end⟩, one_smul := λ _, by { ext, refl }, mul_smul := λ _ _ _, by { ext, refl } } /-- The action of `gal p` on the roots of `p` in `E`. -/ instance gal_action [fact (p.splits (algebra_map F E))] : mul_action p.gal (root_set p E) := { smul := λ ϕ x, roots_equiv_roots p E (ϕ • ((roots_equiv_roots p E).symm x)), one_smul := λ _, by simp only [equiv.apply_symm_apply, one_smul], mul_smul := λ _ _ _, by simp only [equiv.apply_symm_apply, equiv.symm_apply_apply, mul_smul] } variables {p E} /-- `polynomial.gal.restrict p E` is compatible with `polynomial.gal.gal_action p E`. -/ @[simp] lemma restrict_smul [fact (p.splits (algebra_map F E))] (ϕ : E ≃ₐ[F] E) (x : root_set p E) : ↑((restrict p E ϕ) • x) = ϕ x := begin let ψ := alg_equiv.of_injective_field (is_scalar_tower.to_alg_hom F p.splitting_field E), change ↑(ψ (ψ.symm _)) = ϕ x, rw alg_equiv.apply_symm_apply ψ, change ϕ (roots_equiv_roots p E ((roots_equiv_roots p E).symm x)) = ϕ x, rw equiv.apply_symm_apply (roots_equiv_roots p E), end variables (p E) /-- `polynomial.gal.gal_action` as a permutation representation -/ def gal_action_hom [fact (p.splits (algebra_map F E))] : p.gal →* equiv.perm (root_set p E) := mul_action.to_perm_hom _ _ lemma gal_action_hom_restrict [fact (p.splits (algebra_map F E))] (ϕ : E ≃ₐ[F] E) (x : root_set p E) : ↑(gal_action_hom p E (restrict p E ϕ) x) = ϕ x := restrict_smul ϕ x /-- `gal p` embeds as a subgroup of permutations of the roots of `p` in `E`. -/ lemma gal_action_hom_injective [fact (p.splits (algebra_map F E))] : function.injective (gal_action_hom p E) := begin rw injective_iff_map_eq_one, intros ϕ hϕ, ext x hx, have key := equiv.perm.ext_iff.mp hϕ (roots_equiv_roots p E ⟨x, hx⟩), change roots_equiv_roots p E (ϕ • (roots_equiv_roots p E).symm (roots_equiv_roots p E ⟨x, hx⟩)) = roots_equiv_roots p E ⟨x, hx⟩ at key, rw equiv.symm_apply_apply at key, exact subtype.ext_iff.mp (equiv.injective (roots_equiv_roots p E) key), end end roots_action variables {p q} /-- `polynomial.gal.restrict`, when both fields are splitting fields of polynomials. -/ def restrict_dvd (hpq : p ∣ q) : q.gal →* p.gal := if hq : q = 0 then 1 else @restrict F _ p _ _ _ ⟨splits_of_splits_of_dvd (algebra_map F q.splitting_field) hq (splitting_field.splits q) hpq⟩ lemma restrict_dvd_surjective (hpq : p ∣ q) (hq : q ≠ 0) : function.surjective (restrict_dvd hpq) := by simp only [restrict_dvd, dif_neg hq, restrict_surjective] variables (p q) /-- The Galois group of a product maps into the product of the Galois groups. -/ def restrict_prod : (p * q).gal →* p.gal × q.gal := monoid_hom.prod (restrict_dvd (dvd_mul_right p q)) (restrict_dvd (dvd_mul_left q p)) /-- `polynomial.gal.restrict_prod` is actually a subgroup embedding. -/ lemma restrict_prod_injective : function.injective (restrict_prod p q) := begin by_cases hpq : (p * q) = 0, { haveI : unique (p * q).gal, { rw hpq, apply_instance }, exact λ f g h, eq.trans (unique.eq_default f) (unique.eq_default g).symm }, intros f g hfg, dsimp only [restrict_prod, restrict_dvd] at hfg, simp only [dif_neg hpq, monoid_hom.prod_apply, prod.mk.inj_iff] at hfg, ext x hx, rw [root_set, polynomial.map_mul, polynomial.roots_mul] at hx, cases multiset.mem_add.mp (multiset.mem_to_finset.mp hx) with h h, { haveI : fact (p.splits (algebra_map F (p * q).splitting_field)) := ⟨splits_of_splits_of_dvd _ hpq (splitting_field.splits (p * q)) (dvd_mul_right p q)⟩, have key : x = algebra_map (p.splitting_field) (p * q).splitting_field ((roots_equiv_roots p _).inv_fun ⟨x, multiset.mem_to_finset.mpr h⟩) := subtype.ext_iff.mp (equiv.apply_symm_apply (roots_equiv_roots p _) ⟨x, _⟩).symm, rw [key, ←alg_equiv.restrict_normal_commutes, ←alg_equiv.restrict_normal_commutes], exact congr_arg _ (alg_equiv.ext_iff.mp hfg.1 _) }, { haveI : fact (q.splits (algebra_map F (p * q).splitting_field)) := ⟨splits_of_splits_of_dvd _ hpq (splitting_field.splits (p * q)) (dvd_mul_left q p)⟩, have key : x = algebra_map (q.splitting_field) (p * q).splitting_field ((roots_equiv_roots q _).inv_fun ⟨x, multiset.mem_to_finset.mpr h⟩) := subtype.ext_iff.mp (equiv.apply_symm_apply (roots_equiv_roots q _) ⟨x, _⟩).symm, rw [key, ←alg_equiv.restrict_normal_commutes, ←alg_equiv.restrict_normal_commutes], exact congr_arg _ (alg_equiv.ext_iff.mp hfg.2 _) }, { rwa [ne.def, mul_eq_zero, map_eq_zero, map_eq_zero, ←mul_eq_zero] } end lemma mul_splits_in_splitting_field_of_mul {p₁ q₁ p₂ q₂ : F[X]} (hq₁ : q₁ ≠ 0) (hq₂ : q₂ ≠ 0) (h₁ : p₁.splits (algebra_map F q₁.splitting_field)) (h₂ : p₂.splits (algebra_map F q₂.splitting_field)) : (p₁ * p₂).splits (algebra_map F (q₁ * q₂).splitting_field) := begin apply splits_mul, { rw ← (splitting_field.lift q₁ (splits_of_splits_of_dvd _ (mul_ne_zero hq₁ hq₂) (splitting_field.splits _) (dvd_mul_right q₁ q₂))).comp_algebra_map, exact splits_comp_of_splits _ _ h₁, }, { rw ← (splitting_field.lift q₂ (splits_of_splits_of_dvd _ (mul_ne_zero hq₁ hq₂) (splitting_field.splits _) (dvd_mul_left q₂ q₁))).comp_algebra_map, exact splits_comp_of_splits _ _ h₂, }, end /-- `p` splits in the splitting field of `p ∘ q`, for `q` non-constant. -/ lemma splits_in_splitting_field_of_comp (hq : q.nat_degree ≠ 0) : p.splits (algebra_map F (p.comp q).splitting_field) := begin let P : F[X] → Prop := λ r, r.splits (algebra_map F (r.comp q).splitting_field), have key1 : ∀ {r : F[X]}, irreducible r → P r, { intros r hr, by_cases hr' : nat_degree r = 0, { exact splits_of_nat_degree_le_one _ (le_trans (le_of_eq hr') zero_le_one) }, obtain ⟨x, hx⟩ := exists_root_of_splits _ (splitting_field.splits (r.comp q)) (λ h, hr' ((mul_eq_zero.mp (nat_degree_comp.symm.trans (nat_degree_eq_of_degree_eq_some h))).resolve_right hq)), rw [←aeval_def, aeval_comp] at hx, have h_normal : normal F (r.comp q).splitting_field := splitting_field.normal (r.comp q), have qx_int := normal.is_integral h_normal (aeval x q), exact splits_of_splits_of_dvd _ (minpoly.ne_zero qx_int) (normal.splits h_normal _) ((minpoly.irreducible qx_int).dvd_symm hr (minpoly.dvd F _ hx)) }, have key2 : ∀ {p₁ p₂ : F[X]}, P p₁ → P p₂ → P (p₁ * p₂), { intros p₁ p₂ hp₁ hp₂, by_cases h₁ : p₁.comp q = 0, { cases comp_eq_zero_iff.mp h₁ with h h, { rw [h, zero_mul], exact splits_zero _ }, { exact false.rec _ (hq (by rw [h.2, nat_degree_C])) } }, by_cases h₂ : p₂.comp q = 0, { cases comp_eq_zero_iff.mp h₂ with h h, { rw [h, mul_zero], exact splits_zero _ }, { exact false.rec _ (hq (by rw [h.2, nat_degree_C])) } }, have key := mul_splits_in_splitting_field_of_mul h₁ h₂ hp₁ hp₂, rwa ← mul_comp at key }, exact wf_dvd_monoid.induction_on_irreducible p (splits_zero _) (λ _, splits_of_is_unit _) (λ _ _ _ h, key2 (key1 h)), end /-- `polynomial.gal.restrict` for the composition of polynomials. -/ def restrict_comp (hq : q.nat_degree ≠ 0) : (p.comp q).gal →* p.gal := @restrict F _ p _ _ _ ⟨splits_in_splitting_field_of_comp p q hq⟩ lemma restrict_comp_surjective (hq : q.nat_degree ≠ 0) : function.surjective (restrict_comp p q hq) := by simp only [restrict_comp, restrict_surjective] variables {p q} /-- For a separable polynomial, its Galois group has cardinality equal to the dimension of its splitting field over `F`. -/ lemma card_of_separable (hp : p.separable) : fintype.card p.gal = finrank F p.splitting_field := begin haveI : is_galois F p.splitting_field := is_galois.of_separable_splitting_field hp, exact is_galois.card_aut_eq_finrank F p.splitting_field, end lemma prime_degree_dvd_card [char_zero F] (p_irr : irreducible p) (p_deg : p.nat_degree.prime) : p.nat_degree ∣ fintype.card p.gal := begin rw gal.card_of_separable p_irr.separable, have hp : p.degree ≠ 0 := λ h, nat.prime.ne_zero p_deg (nat_degree_eq_zero_iff_degree_le_zero.mpr (le_of_eq h)), let α : p.splitting_field := root_of_splits (algebra_map F p.splitting_field) (splitting_field.splits p) hp, have hα : is_integral F α := algebra.is_integral_of_finite _ _ α, use finite_dimensional.finrank F⟮α⟯ p.splitting_field, suffices : (minpoly F α).nat_degree = p.nat_degree, { rw [←finite_dimensional.finrank_mul_finrank F F⟮α⟯ p.splitting_field, intermediate_field.adjoin.finrank hα, this] }, suffices : minpoly F α ∣ p, { have key := (minpoly.irreducible hα).dvd_symm p_irr this, apply le_antisymm, { exact nat_degree_le_of_dvd this p_irr.ne_zero }, { exact nat_degree_le_of_dvd key (minpoly.ne_zero hα) } }, apply minpoly.dvd F α, rw [aeval_def, map_root_of_splits _ (splitting_field.splits p) hp], end section rationals lemma splits_ℚ_ℂ {p : ℚ[X]} : fact (p.splits (algebra_map ℚ ℂ)) := ⟨is_alg_closed.splits_codomain p⟩ local attribute [instance] splits_ℚ_ℂ /-- The number of complex roots equals the number of real roots plus the number of roots not fixed by complex conjugation (i.e. with some imaginary component). -/ lemma card_complex_roots_eq_card_real_add_card_not_gal_inv (p : ℚ[X]) : (p.root_set ℂ).to_finset.card = (p.root_set ℝ).to_finset.card + (gal_action_hom p ℂ (restrict p ℂ (complex.conj_ae.restrict_scalars ℚ))).support.card := begin by_cases hp : p = 0, { simp_rw [hp, root_set_zero, set.to_finset_eq_empty_iff.mpr rfl, finset.card_empty, zero_add], refine eq.symm (nat.le_zero_iff.mp ((finset.card_le_univ _).trans (le_of_eq _))), simp_rw [hp, root_set_zero, fintype.card_eq_zero_iff], apply_instance }, have inj : function.injective (is_scalar_tower.to_alg_hom ℚ ℝ ℂ) := (algebra_map ℝ ℂ).injective, rw [←finset.card_image_of_injective _ subtype.coe_injective, ←finset.card_image_of_injective _ inj], let a : finset ℂ := _, let b : finset ℂ := _, let c : finset ℂ := _, change a.card = b.card + c.card, have ha : ∀ z : ℂ, z ∈ a ↔ aeval z p = 0 := λ z, by rw [set.mem_to_finset, mem_root_set hp], have hb : ∀ z : ℂ, z ∈ b ↔ aeval z p = 0 ∧ z.im = 0, { intro z, simp_rw [finset.mem_image, exists_prop, set.mem_to_finset, mem_root_set hp], split, { rintros ⟨w, hw, rfl⟩, exact ⟨by rw [aeval_alg_hom_apply, hw, alg_hom.map_zero], rfl⟩ }, { rintros ⟨hz1, hz2⟩, have key : is_scalar_tower.to_alg_hom ℚ ℝ ℂ z.re = z := by { ext, refl, rw hz2, refl }, exact ⟨z.re, inj (by rwa [←aeval_alg_hom_apply, key, alg_hom.map_zero]), key⟩ } }, have hc0 : ∀ w : p.root_set ℂ, gal_action_hom p ℂ (restrict p ℂ (complex.conj_ae.restrict_scalars ℚ)) w = w ↔ w.val.im = 0, { intro w, rw [subtype.ext_iff, gal_action_hom_restrict], exact complex.eq_conj_iff_im }, have hc : ∀ z : ℂ, z ∈ c ↔ aeval z p = 0 ∧ z.im ≠ 0, { intro z, simp_rw [finset.mem_image, exists_prop], split, { rintros ⟨w, hw, rfl⟩, exact ⟨(mem_root_set hp).mp w.2, mt (hc0 w).mpr (equiv.perm.mem_support.mp hw)⟩ }, { rintros ⟨hz1, hz2⟩, exact ⟨⟨z, (mem_root_set hp).mpr hz1⟩, equiv.perm.mem_support.mpr (mt (hc0 _).mp hz2), rfl⟩ } }, rw ← finset.card_disjoint_union, { apply congr_arg finset.card, simp_rw [finset.ext_iff, finset.mem_union, ha, hb, hc], tauto }, { intro z, rw [finset.inf_eq_inter, finset.mem_inter, hb, hc], tauto }, { apply_instance }, end /-- An irreducible polynomial of prime degree with two non-real roots has full Galois group. -/ lemma gal_action_hom_bijective_of_prime_degree {p : ℚ[X]} (p_irr : irreducible p) (p_deg : p.nat_degree.prime) (p_roots : fintype.card (p.root_set ℂ) = fintype.card (p.root_set ℝ) + 2) : function.bijective (gal_action_hom p ℂ) := begin have h1 : fintype.card (p.root_set ℂ) = p.nat_degree, { simp_rw [root_set_def, finset.coe_sort_coe, fintype.card_coe], rw [multiset.to_finset_card_of_nodup, ←nat_degree_eq_card_roots], { exact is_alg_closed.splits_codomain p }, { exact nodup_roots ((separable_map (algebra_map ℚ ℂ)).mpr p_irr.separable) } }, have h2 : fintype.card p.gal = fintype.card (gal_action_hom p ℂ).range := fintype.card_congr (monoid_hom.of_injective (gal_action_hom_injective p ℂ)).to_equiv, let conj := restrict p ℂ (complex.conj_ae.restrict_scalars ℚ), refine ⟨gal_action_hom_injective p ℂ, λ x, (congr_arg (has_mem.mem x) (show (gal_action_hom p ℂ).range = ⊤, from _)).mpr (subgroup.mem_top x)⟩, apply equiv.perm.subgroup_eq_top_of_swap_mem, { rwa h1 }, { rw h1, convert prime_degree_dvd_card p_irr p_deg using 1, convert h2.symm }, { exact ⟨conj, rfl⟩ }, { rw ← equiv.perm.card_support_eq_two, apply nat.add_left_cancel, rw [←p_roots, ←set.to_finset_card (root_set p ℝ), ←set.to_finset_card (root_set p ℂ)], exact (card_complex_roots_eq_card_real_add_card_not_gal_inv p).symm }, end /-- An irreducible polynomial of prime degree with 1-3 non-real roots has full Galois group. -/ lemma gal_action_hom_bijective_of_prime_degree' {p : ℚ[X]} (p_irr : irreducible p) (p_deg : p.nat_degree.prime) (p_roots1 : fintype.card (p.root_set ℝ) + 1 ≤ fintype.card (p.root_set ℂ)) (p_roots2 : fintype.card (p.root_set ℂ) ≤ fintype.card (p.root_set ℝ) + 3) : function.bijective (gal_action_hom p ℂ) := begin apply gal_action_hom_bijective_of_prime_degree p_irr p_deg, let n := (gal_action_hom p ℂ (restrict p ℂ (complex.conj_ae.restrict_scalars ℚ))).support.card, have hn : 2 ∣ n := equiv.perm.two_dvd_card_support (by rw [←monoid_hom.map_pow, ←monoid_hom.map_pow, show alg_equiv.restrict_scalars ℚ complex.conj_ae ^ 2 = 1, from alg_equiv.ext complex.conj_conj, monoid_hom.map_one, monoid_hom.map_one]), have key := card_complex_roots_eq_card_real_add_card_not_gal_inv p, simp_rw [set.to_finset_card] at key, rw [key, add_le_add_iff_left] at p_roots1 p_roots2, rw [key, add_right_inj], suffices : ∀ m : ℕ, 2 ∣ m → 1 ≤ m → m ≤ 3 → m = 2, { exact this n hn p_roots1 p_roots2 }, rintros m ⟨k, rfl⟩ h2 h3, exact le_antisymm (nat.lt_succ_iff.mp (lt_of_le_of_ne h3 (show 2 * k ≠ 2 * 1 + 1, from nat.two_mul_ne_two_mul_add_one))) (nat.succ_le_iff.mpr (lt_of_le_of_ne h2 (show 2 * 0 + 1 ≠ 2 * k, from nat.two_mul_ne_two_mul_add_one.symm))), end end rationals end gal end polynomial
9adff4cf81adccf2685c2ff4f2101ce8e48040bf
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/mv_polynomial/basic.lean
aa5dd0f7aae240c3a21525840397d75bcbf4ee58
[ "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
36,207
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, Johan Commelin, Mario Carneiro -/ import data.polynomial.eval /-! # Multivariate polynomials This file defines polynomial rings over a base ring (or even semiring), with variables from a general type `σ` (which could be infinite). ## Important definitions Let `R` be a commutative ring (or a semiring) and let `σ` be an arbitrary type. This file creates the type `mv_polynomial σ R`, which mathematicians might denote $R[X_i : i \in σ]$. It is the type of multivariate (a.k.a. multivariable) polynomials, with variables corresponding to the terms in `σ`, and coefficients in `R`. ### Notation In the definitions below, we use the following notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[comm_semiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` ### Definitions * `mv_polynomial σ R` : the type of polynomials with variables of type `σ` and coefficients in the commutative semiring `R` * `monomial s a` : the monomial which mathematically would be denoted `a * X^s` * `C a` : the constant polynomial with value `a` * `X i` : the degree one monomial corresponding to i; mathematically this might be denoted `Xᵢ`. * `coeff s p` : the coefficient of `s` in `p`. * `eval₂ (f : R → S₁) (g : σ → S₁) p` : given a semiring homomorphism from `R` to another semiring `S₁`, and a map `σ → S₁`, evaluates `p` at this valuation, returning a term of type `S₁`. Note that `eval₂` can be made using `eval` and `map` (see below), and it has been suggested that sticking to `eval` and `map` might make the code less brittle. * `eval (g : σ → R) p` : given a map `σ → R`, evaluates `p` at this valuation, returning a term of type `R` * `map (f : R → S₁) p` : returns the multivariate polynomial obtained from `p` by the change of coefficient semiring corresponding to `f` ## Implementation notes Recall that if `Y` has a zero, then `X →₀ Y` is the type of functions from `X` to `Y` with finite support, i.e. such that only finitely many elements of `X` get sent to non-zero terms in `Y`. The definition of `mv_polynomial σ R` is `(σ →₀ ℕ) →₀ R` ; here `σ →₀ ℕ` denotes the space of all monomials in the variables, and the function to `R` sends a monomial to its coefficient in the polynomial being represented. ## Tags polynomial, multivariate polynomial, multivariable polynomial S₁ S₂ S₃ -/ noncomputable theory open_locale classical big_operators open set function finsupp add_monoid_algebra open_locale big_operators universes u v w x variables {R : Type u} {S₁ : Type v} {S₂ : Type w} {S₃ : Type x} /-- Multivariate polynomial, where `σ` is the index set of the variables and `R` is the coefficient ring -/ def mv_polynomial (σ : Type*) (R : Type*) [comm_semiring R] := add_monoid_algebra R (σ →₀ ℕ) namespace mv_polynomial variables {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section comm_semiring variables [comm_semiring R] {p q : mv_polynomial σ R} instance decidable_eq_mv_polynomial [decidable_eq σ] [decidable_eq R] : decidable_eq (mv_polynomial σ R) := finsupp.decidable_eq instance : comm_semiring (mv_polynomial σ R) := add_monoid_algebra.comm_semiring instance : inhabited (mv_polynomial σ R) := ⟨0⟩ instance : has_scalar R (mv_polynomial σ R) := add_monoid_algebra.has_scalar instance : semimodule R (mv_polynomial σ R) := add_monoid_algebra.semimodule instance : algebra R (mv_polynomial σ R) := add_monoid_algebra.algebra /-- the coercion turning an `mv_polynomial` into the function which reports the coefficient of a given monomial -/ def coeff_coe_to_fun : has_coe_to_fun (mv_polynomial σ R) := finsupp.has_coe_to_fun local attribute [instance] coeff_coe_to_fun /-- `monomial s a` is the monomial `a * X^s` -/ def monomial (s : σ →₀ ℕ) (a : R) : mv_polynomial σ R := single s a /-- `C a` is the constant polynomial with value `a` -/ def C : R →+* mv_polynomial σ R := { to_fun := monomial 0, map_zero' := by simp [monomial], map_one' := rfl, map_add' := λ a a', single_add, map_mul' := λ a a', by simp [monomial, single_mul_single] } variables (R σ) theorem algebra_map_eq : algebra_map R (mv_polynomial σ R) = C := rfl variables {R σ} /-- `X n` is the degree `1` monomial $X_n$. -/ def X (n : σ) : mv_polynomial σ R := monomial (single n 1) 1 @[simp] lemma C_0 : C 0 = (0 : mv_polynomial σ R) := by simp [C, monomial]; refl @[simp] lemma C_1 : C 1 = (1 : mv_polynomial σ R) := rfl lemma C_mul_monomial : C a * monomial s a' = monomial s (a * a') := by simp [C, monomial, single_mul_single] @[simp] lemma C_add : (C (a + a') : mv_polynomial σ R) = C a + C a' := single_add @[simp] lemma C_mul : (C (a * a') : mv_polynomial σ R) = C a * C a' := C_mul_monomial.symm @[simp] lemma C_pow (a : R) (n : ℕ) : (C (a^n) : mv_polynomial σ R) = (C a)^n := by induction n; simp [pow_succ, *] lemma C_injective (σ : Type*) (R : Type*) [comm_semiring R] : function.injective (C : R → mv_polynomial σ R) := finsupp.single_injective _ @[simp] lemma C_inj {σ : Type*} (R : Type*) [comm_semiring R] (r s : R) : (C r : mv_polynomial σ R) = C s ↔ r = s := (C_injective σ R).eq_iff instance infinite_of_infinite (σ : Type*) (R : Type*) [comm_semiring R] [infinite R] : infinite (mv_polynomial σ R) := infinite.of_injective C (C_injective _ _) instance infinite_of_nonempty (σ : Type*) (R : Type*) [nonempty σ] [comm_semiring R] [nontrivial R] : infinite (mv_polynomial σ R) := infinite.of_injective (λ i : ℕ, monomial (single (classical.arbitrary σ) i) 1) begin intros m n h, have := (single_eq_single_iff _ _ _ _).mp h, simp only [and_true, eq_self_iff_true, or_false, one_ne_zero, and_self, single_eq_single_iff, eq_self_iff_true, true_and] at this, rcases this with (rfl|⟨rfl, rfl⟩); refl end lemma C_eq_coe_nat (n : ℕ) : (C ↑n : mv_polynomial σ R) = n := by induction n; simp [nat.succ_eq_add_one, *] theorem C_mul' : mv_polynomial.C a * p = a • p := begin apply finsupp.induction p, { exact (mul_zero $ mv_polynomial.C a).trans (@smul_zero R (mv_polynomial σ R) _ _ _ a).symm }, intros p b f haf hb0 ih, rw [mul_add, ih, @smul_add R (mv_polynomial σ R) _ _ _ a], congr' 1, rw [add_monoid_algebra.mul_def, finsupp.smul_single], simp only [mv_polynomial.C], dsimp [mv_polynomial.monomial], rw [finsupp.sum_single_index, finsupp.sum_single_index, zero_add], { rw [mul_zero, finsupp.single_zero] }, { rw finsupp.sum_single_index, all_goals { rw [zero_mul, finsupp.single_zero] }, } end lemma smul_eq_C_mul (p : mv_polynomial σ R) (a : R) : a • p = C a * p := C_mul'.symm lemma X_pow_eq_single : X n ^ e = monomial (single n e) (1 : R) := begin induction e, { simp [X], refl }, { simp [pow_succ, e_ih], simp [X, monomial, single_mul_single, nat.succ_eq_add_one, add_comm] } end lemma monomial_add_single : monomial (s + single n e) a = (monomial s a * X n ^ e) := by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma monomial_single_add : monomial (single n e + s) a = (X n ^ e * monomial s a) := by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma single_eq_C_mul_X {s : σ} {a : R} {n : ℕ} : monomial (single s n) a = C a * (X s)^n := by { rw [← zero_add (single s n), monomial_add_single, C], refl } @[simp] lemma monomial_add {s : σ →₀ ℕ} {a b : R} : monomial s a + monomial s b = monomial s (a + b) := by simp [monomial] @[simp] lemma monomial_mul {s s' : σ →₀ ℕ} {a b : R} : monomial s a * monomial s' b = monomial (s + s') (a * b) := by rw [monomial, monomial, monomial, add_monoid_algebra.single_mul_single] @[simp] lemma monomial_zero {s : σ →₀ ℕ}: monomial s (0 : R) = 0 := by rw [monomial, single_zero]; refl @[simp] lemma sum_monomial {A : Type*} [add_comm_monoid A] {u : σ →₀ ℕ} {r : R} {b : (σ →₀ ℕ) → R → A} (w : b u 0 = 0) : sum (monomial u r) b = b u r := sum_single_index w lemma monomial_eq : monomial s a = C a * (s.prod $ λn e, X n ^ e : mv_polynomial σ R) := begin apply @finsupp.induction σ ℕ _ _ s, { simp only [C, prod_zero_index]; exact (mul_one _).symm }, { assume n e s hns he ih, rw [monomial_single_add, ih, prod_add_index, prod_single_index, mul_left_comm], { simp only [pow_zero], }, { intro a, simp only [pow_zero], }, { intros, rw pow_add, }, } end @[recursor 5] lemma induction_on {M : mv_polynomial σ R → Prop} (p : mv_polynomial σ R) (h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_X : ∀p n, M p → M (p * X n)) : M p := have ∀s a, M (monomial s a), begin assume s a, apply @finsupp.induction σ ℕ _ _ s, { show M (monomial 0 a), from h_C a, }, { assume n e p hpn he ih, have : ∀e:ℕ, M (monomial p a * X n ^ e), { intro e, induction e, { simp [ih] }, { simp [ih, pow_succ', (mul_assoc _ _ _).symm, h_X, e_ih] } }, simp [add_comm, monomial_add_single, this] } end, finsupp.induction p (by have : M (C 0) := h_C 0; rwa [C_0] at this) (assume s a p hsp ha hp, h_add _ _ (this s a) hp) theorem induction_on' {P : mv_polynomial σ R → Prop} (p : mv_polynomial σ R) (h1 : ∀ (u : σ →₀ ℕ) (a : R), P (monomial u a)) (h2 : ∀ (p q : mv_polynomial σ R), P p → P q → P (p + q)) : P p := finsupp.induction p (suffices P (monomial 0 0), by rwa monomial_zero at this, show P (monomial 0 0), from h1 0 0) (λ a b f ha hb hPf, h2 _ _ (h1 _ _) hPf) lemma hom_eq_hom [semiring S₂] (f g : mv_polynomial σ R →+* S₂) (hC : ∀a:R, f (C a) = g (C a)) (hX : ∀n:σ, f (X n) = g (X n)) (p : mv_polynomial σ R) : f p = g p := mv_polynomial.induction_on p hC begin assume p q hp hq, rw [is_semiring_hom.map_add f, is_semiring_hom.map_add g, hp, hq] end begin assume p n hp, rw [is_semiring_hom.map_mul f, is_semiring_hom.map_mul g, hp, hX] end lemma is_id (f : mv_polynomial σ R →+* mv_polynomial σ R) (hC : ∀a:R, f (C a) = (C a)) (hX : ∀n:σ, f (X n) = (X n)) (p : mv_polynomial σ R) : f p = p := hom_eq_hom f (ring_hom.id _) hC hX p lemma ring_hom_ext {A : Type*} [comm_semiring A] (f g : mv_polynomial σ R →+* A) (hC : ∀ r, f (C r) = g (C r)) (hX : ∀ i, f (X i) = g (X i)) : f = g := begin ext p : 1, apply mv_polynomial.induction_on' p, { intros m r, rw [monomial_eq, finsupp.prod], simp only [monomial_eq, ring_hom.map_mul, ring_hom.map_prod, ring_hom.map_pow, hC, hX], }, { intros p q hp hq, simp only [ring_hom.map_add, hp, hq] } end lemma alg_hom_ext {A : Type*} [comm_semiring A] [algebra R A] (f g : mv_polynomial σ R →ₐ[R] A) (hf : ∀ i : σ, f (X i) = g (X i)) : f = g := begin apply alg_hom.coe_ring_hom_injective, apply ring_hom_ext, { intro r, calc f (C r) = algebra_map R A r : f.commutes r ... = g (C r) : (g.commutes r).symm }, { simpa only [hf] }, end @[simp] lemma alg_hom_C (f : mv_polynomial σ R →ₐ[R] mv_polynomial σ R) (r : R) : f (C r) = C r := f.commutes r section coeff section -- While setting up `coeff`, we make `mv_polynomial` reducible so we can treat it as a function. local attribute [reducible] mv_polynomial /-- The coefficient of the monomial `m` in the multi-variable polynomial `p`. -/ def coeff (m : σ →₀ ℕ) (p : mv_polynomial σ R) : R := p m end lemma ext (p q : mv_polynomial σ R) : (∀ m, coeff m p = coeff m q) → p = q := ext lemma ext_iff (p q : mv_polynomial σ R) : p = q ↔ (∀ m, coeff m p = coeff m q) := ⟨ λ h m, by rw h, ext p q⟩ @[simp] lemma coeff_add (m : σ →₀ ℕ) (p q : mv_polynomial σ R) : coeff m (p + q) = coeff m p + coeff m q := add_apply @[simp] lemma coeff_zero (m : σ →₀ ℕ) : coeff m (0 : mv_polynomial σ R) = 0 := rfl @[simp] lemma coeff_zero_X (i : σ) : coeff 0 (X i : mv_polynomial σ R) = 0 := single_eq_of_ne (λ h, by cases single_eq_zero.1 h) instance coeff.is_add_monoid_hom (m : σ →₀ ℕ) : is_add_monoid_hom (coeff m : mv_polynomial σ R → R) := { map_add := coeff_add m, map_zero := coeff_zero m } lemma coeff_sum {X : Type*} (s : finset X) (f : X → mv_polynomial σ R) (m : σ →₀ ℕ) : coeff m (∑ x in s, f x) = ∑ x in s, coeff m (f x) := (s.sum_hom _).symm lemma monic_monomial_eq (m) : monomial m (1:R) = (m.prod $ λn e, X n ^ e : mv_polynomial σ R) := by simp [monomial_eq] @[simp] lemma coeff_monomial (m n) (a) : coeff m (monomial n a : mv_polynomial σ R) = if n = m then a else 0 := by convert single_apply @[simp] lemma coeff_C (m) (a) : coeff m (C a : mv_polynomial σ R) = if 0 = m then a else 0 := by convert single_apply lemma coeff_X_pow (i : σ) (m) (k : ℕ) : coeff m (X i ^ k : mv_polynomial σ R) = if single i k = m then 1 else 0 := begin have := coeff_monomial m (finsupp.single i k) (1:R), rwa [@monomial_eq _ _ (1:R) (finsupp.single i k) _, C_1, one_mul, finsupp.prod_single_index] at this, exact pow_zero _ end lemma coeff_X' (i : σ) (m) : coeff m (X i : mv_polynomial σ R) = if single i 1 = m then 1 else 0 := by rw [← coeff_X_pow, pow_one] @[simp] lemma coeff_X (i : σ) : coeff (single i 1) (X i : mv_polynomial σ R) = 1 := by rw [coeff_X', if_pos rfl] @[simp] lemma coeff_C_mul (m) (a : R) (p : mv_polynomial σ R) : coeff m (C a * p) = a * coeff m p := begin rw [mul_def], simp only [C, monomial], dsimp, rw [monomial], rw sum_single_index, { simp only [zero_add], convert sum_apply, simp only [single_apply, finsupp.sum], rw finset.sum_eq_single m, { rw if_pos rfl, refl }, { intros m' hm' H, apply if_neg, exact H }, { intros hm, rw if_pos rfl, rw not_mem_support_iff at hm, simp [hm] } }, simp only [zero_mul, single_zero, zero_add, sum_zero], end lemma coeff_mul (p q : mv_polynomial σ R) (n : σ →₀ ℕ) : coeff n (p * q) = ∑ x in (antidiagonal n).support, coeff x.1 p * coeff x.2 q := begin rw mul_def, have := @finset.sum_sigma (σ →₀ ℕ) R _ _ p.support (λ _, q.support) (λ x, if (x.1 + x.2 = n) then coeff x.1 p * coeff x.2 q else 0), convert this.symm using 1; clear this, { rw [coeff], repeat {rw sum_apply, apply finset.sum_congr rfl, intros, dsimp only}, convert single_apply }, { have : (antidiagonal n).support.filter (λ x, x.1 ∈ p.support ∧ x.2 ∈ q.support) ⊆ (antidiagonal n).support := finset.filter_subset _, rw [← finset.sum_sdiff this, finset.sum_eq_zero, zero_add], swap, { intros x hx, rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and, not_and, not_mem_support_iff] at hx, by_cases H : x.1 ∈ p.support, { rw [coeff, coeff, hx.2 hx.1 H, mul_zero] }, { rw not_mem_support_iff at H, rw [coeff, H, zero_mul] } }, symmetry, rw [← finset.sum_sdiff (finset.filter_subset _), finset.sum_eq_zero, zero_add], swap, { intros x hx, rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and] at hx, simp only [if_neg (hx.2 hx.1)] }, { apply finset.sum_bij, swap 5, { intros x hx, exact (x.1, x.2) }, { intros x hx, rw [finset.mem_filter, finset.mem_sigma] at hx, simpa [finset.mem_filter, mem_antidiagonal_support] using hx.symm }, { intros x hx, rw finset.mem_filter at hx, simp only [if_pos hx.2], }, { rintros ⟨i,j⟩ ⟨k,l⟩ hij hkl, simpa using and.intro }, { rintros ⟨i,j⟩ hij, refine ⟨⟨i,j⟩, _, _⟩, { apply_instance }, { rw [finset.mem_filter, mem_antidiagonal_support] at hij, simpa [finset.mem_filter, finset.mem_sigma] using hij.symm }, { refl } } }, all_goals { apply_instance } } end @[simp] lemma coeff_mul_X (m) (s : σ) (p : mv_polynomial σ R) : coeff (m + single s 1) (p * X s) = coeff m p := begin have : (m, single s 1) ∈ (m + single s 1).antidiagonal.support := mem_antidiagonal_support.2 rfl, rw [coeff_mul, ← finset.insert_erase this, finset.sum_insert (finset.not_mem_erase _ _), finset.sum_eq_zero, add_zero, coeff_X, mul_one], rintros ⟨i,j⟩ hij, rw [finset.mem_erase, mem_antidiagonal_support] at hij, by_cases H : single s 1 = j, { subst j, simpa using hij }, { rw [coeff_X', if_neg H, mul_zero] }, end lemma coeff_mul_X' (m) (s : σ) (p : mv_polynomial σ R) : coeff m (p * X s) = if s ∈ m.support then coeff (m - single s 1) p else 0 := begin split_ifs with h h, { conv_rhs {rw ← coeff_mul_X _ s}, congr' with t, by_cases hj : s = t, { subst t, simp only [nat_sub_apply, add_apply, single_eq_same], refine (nat.sub_add_cancel $ nat.pos_of_ne_zero _).symm, rwa mem_support_iff at h }, { simp [single_eq_of_ne hj] } }, { delta coeff, rw ← not_mem_support_iff, intro hm, apply h, have H := support_mul _ _ hm, simp only [finset.mem_bind] at H, rcases H with ⟨j, hj, i', hi', H⟩, delta X monomial at hi', rw mem_support_single at hi', cases hi', subst i', erw finset.mem_singleton at H, subst m, rw [mem_support_iff, add_apply, single_apply, if_pos rfl], intro H, rw [_root_.add_eq_zero_iff] at H, exact one_ne_zero H.2 } end lemma eq_zero_iff {p : mv_polynomial σ R} : p = 0 ↔ ∀ d, coeff d p = 0 := by { rw ext_iff, simp only [coeff_zero], } lemma ne_zero_iff {p : mv_polynomial σ R} : p ≠ 0 ↔ ∃ d, coeff d p ≠ 0 := by { rw [ne.def, eq_zero_iff], push_neg, } lemma exists_coeff_ne_zero {p : mv_polynomial σ R} (h : p ≠ 0) : ∃ d, coeff d p ≠ 0 := ne_zero_iff.mp h lemma C_dvd_iff_dvd_coeff (r : R) (φ : mv_polynomial σ R) : C r ∣ φ ↔ ∀ i, r ∣ φ.coeff i := begin split, { rintros ⟨φ, rfl⟩ c, rw coeff_C_mul, apply dvd_mul_right }, { intro h, choose c hc using h, classical, let c' : (σ →₀ ℕ) → R := λ i, if i ∈ φ.support then c i else 0, let ψ : mv_polynomial σ R := ∑ i in φ.support, monomial i (c' i), use ψ, apply mv_polynomial.ext, intro i, simp only [coeff_C_mul, coeff_sum, coeff_monomial, finset.sum_ite_eq', c'], split_ifs with hi hi, { rw hc }, { rw finsupp.not_mem_support_iff at hi, rwa mul_zero } }, end end coeff section constant_coeff /-- `constant_coeff p` returns the constant term of the polynomial `p`, defined as `coeff 0 p`. This is a ring homomorphism. -/ def constant_coeff : mv_polynomial σ R →+* R := { to_fun := coeff 0, map_one' := by simp [coeff, add_monoid_algebra.one_def], map_mul' := by simp [coeff_mul, finsupp.support_single_ne_zero], map_zero' := coeff_zero _, map_add' := coeff_add _ } lemma constant_coeff_eq : (constant_coeff : mv_polynomial σ R → R) = coeff 0 := rfl @[simp] lemma constant_coeff_C (r : R) : constant_coeff (C r : mv_polynomial σ R) = r := by simp [constant_coeff_eq] @[simp] lemma constant_coeff_X (i : σ) : constant_coeff (X i : mv_polynomial σ R) = 0 := by simp [constant_coeff_eq] lemma constant_coeff_monomial (d : σ →₀ ℕ) (r : R) : constant_coeff (monomial d r) = if d = 0 then r else 0 := by rw [constant_coeff_eq, coeff_monomial] end constant_coeff section as_sum @[simp] lemma support_sum_monomial_coeff (p : mv_polynomial σ R) : ∑ v in p.support, monomial v (coeff v p) = p := finsupp.sum_single p lemma as_sum (p : mv_polynomial σ R) : p = ∑ v in p.support, monomial v (coeff v p) := (support_sum_monomial_coeff p).symm end as_sum section eval₂ variables [comm_semiring S₁] variables (f : R →+* S₁) (g : σ → S₁) /-- Evaluate a polynomial `p` given a valuation `g` of all the variables and a ring hom `f` from the scalar ring to the target -/ def eval₂ (p : mv_polynomial σ R) : S₁ := p.sum (λs a, f a * s.prod (λn e, g n ^ e)) lemma eval₂_eq (g : R →+* S₁) (x : σ → S₁) (f : mv_polynomial σ R) : f.eval₂ g x = ∑ d in f.support, g (f.coeff d) * ∏ i in d.support, x i ^ d i := rfl lemma eval₂_eq' [fintype σ] (g : R →+* S₁) (x : σ → S₁) (f : mv_polynomial σ R) : f.eval₂ g x = ∑ d in f.support, g (f.coeff d) * ∏ i, x i ^ d i := by { simp only [eval₂_eq, ← finsupp.prod_pow], refl } @[simp] lemma eval₂_zero : (0 : mv_polynomial σ R).eval₂ f g = 0 := finsupp.sum_zero_index section @[simp] lemma eval₂_add : (p + q).eval₂ f g = p.eval₂ f g + q.eval₂ f g := finsupp.sum_add_index (by simp [is_semiring_hom.map_zero f]) (by simp [add_mul, is_semiring_hom.map_add f]) @[simp] lemma eval₂_monomial : (monomial s a).eval₂ f g = f a * s.prod (λn e, g n ^ e) := finsupp.sum_single_index (by simp [is_semiring_hom.map_zero f]) @[simp] lemma eval₂_C (a) : (C a).eval₂ f g = f a := by simp [eval₂_monomial, C, prod_zero_index] @[simp] lemma eval₂_one : (1 : mv_polynomial σ R).eval₂ f g = 1 := (eval₂_C _ _ _).trans (is_semiring_hom.map_one f) @[simp] lemma eval₂_X (n) : (X n).eval₂ f g = g n := by simp [eval₂_monomial, is_semiring_hom.map_one f, X, prod_single_index, pow_one] lemma eval₂_mul_monomial : ∀{s a}, (p * monomial s a).eval₂ f g = p.eval₂ f g * f a * s.prod (λn e, g n ^ e) := begin apply mv_polynomial.induction_on p, { assume a' s a, simp [C_mul_monomial, eval₂_monomial, is_semiring_hom.map_mul f] }, { assume p q ih_p ih_q, simp [add_mul, eval₂_add, ih_p, ih_q] }, { assume p n ih s a, from calc (p * X n * monomial s a).eval₂ f g = (p * monomial (single n 1 + s) a).eval₂ f g : by simp [monomial_single_add, -add_comm, pow_one, mul_assoc] ... = (p * monomial (single n 1) 1).eval₂ f g * f a * s.prod (λn e, g n ^ e) : by simp [ih, prod_single_index, prod_add_index, pow_one, pow_add, mul_assoc, mul_left_comm, is_semiring_hom.map_one f, -add_comm] } end @[simp] lemma eval₂_mul : ∀{p}, (p * q).eval₂ f g = p.eval₂ f g * q.eval₂ f g := begin apply mv_polynomial.induction_on q, { simp [C, eval₂_monomial, eval₂_mul_monomial, prod_zero_index] }, { simp [mul_add, eval₂_add] {contextual := tt} }, { simp [X, eval₂_monomial, eval₂_mul_monomial, (mul_assoc _ _ _).symm] { contextual := tt} } end @[simp] lemma eval₂_pow {p:mv_polynomial σ R} : ∀{n:ℕ}, (p ^ n).eval₂ f g = (p.eval₂ f g)^n | 0 := eval₂_one _ _ | (n + 1) := by rw [pow_add, pow_one, pow_add, pow_one, eval₂_mul, eval₂_pow] instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f g) := { map_zero := eval₂_zero _ _, map_one := eval₂_one _ _, map_add := λ p q, eval₂_add _ _, map_mul := λ p q, eval₂_mul _ _ } /-- `mv_polynomial.eval₂` as a `ring_hom`. -/ def eval₂_hom (f : R →+* S₁) (g : σ → S₁) : mv_polynomial σ R →+* S₁ := ring_hom.of (eval₂ f g) @[simp] lemma coe_eval₂_hom (f : R →+* S₁) (g : σ → S₁) : ⇑(eval₂_hom f g) = eval₂ f g := rfl lemma eval₂_hom_congr {f₁ f₂ : R →+* S₁} {g₁ g₂ : σ → S₁} {p₁ p₂ : mv_polynomial σ R} : f₁ = f₂ → g₁ = g₂ → p₁ = p₂ → eval₂_hom f₁ g₁ p₁ = eval₂_hom f₂ g₂ p₂ := by rintros rfl rfl rfl; refl end @[simp] lemma eval₂_hom_C (f : R →+* S₁) (g : σ → S₁) (r : R) : eval₂_hom f g (C r) = f r := eval₂_C f g r @[simp] lemma eval₂_hom_X' (f : R →+* S₁) (g : σ → S₁) (i : σ) : eval₂_hom f g (X i) = g i := eval₂_X f g i @[simp] lemma comp_eval₂_hom [comm_semiring S₂] (f : R →+* S₁) (g : σ → S₁) (φ : S₁ →+* S₂) : φ.comp (eval₂_hom f g) = (eval₂_hom (φ.comp f) (λ i, φ (g i))) := begin apply mv_polynomial.ring_hom_ext, { intro r, rw [ring_hom.comp_apply, eval₂_hom_C, eval₂_hom_C, ring_hom.comp_apply] }, { intro i, rw [ring_hom.comp_apply, eval₂_hom_X', eval₂_hom_X'] } end lemma map_eval₂_hom [comm_semiring S₂] (f : R →+* S₁) (g : σ → S₁) (φ : S₁ →+* S₂) (p : mv_polynomial σ R) : φ (eval₂_hom f g p) = (eval₂_hom (φ.comp f) (λ i, φ (g i)) p) := by { rw ← comp_eval₂_hom, refl } lemma eval₂_hom_monomial (f : R →+* S₁) (g : σ → S₁) (d : σ →₀ ℕ) (r : R) : eval₂_hom f g (monomial d r) = f r * d.prod (λ i k, g i ^ k) := by simp only [monomial_eq, ring_hom.map_mul, eval₂_hom_C, finsupp.prod, ring_hom.map_prod, ring_hom.map_pow, eval₂_hom_X'] section local attribute [instance, priority 10] is_semiring_hom.comp lemma eval₂_comp_left {S₂} [comm_semiring S₂] (k : S₁ →+* S₂) (f : R →+* S₁) (g : σ → S₁) (p) : k (eval₂ f g p) = eval₂ (k.comp f) (k ∘ g) p := by apply mv_polynomial.induction_on p; simp [ eval₂_add, k.map_add, eval₂_mul, k.map_mul] {contextual := tt} end @[simp] lemma eval₂_eta (p : mv_polynomial σ R) : eval₂ C X p = p := by apply mv_polynomial.induction_on p; simp [eval₂_add, eval₂_mul] {contextual := tt} lemma eval₂_congr (g₁ g₂ : σ → S₁) (h : ∀ {i : σ} {c : σ →₀ ℕ}, i ∈ c.support → coeff c p ≠ 0 → g₁ i = g₂ i) : p.eval₂ f g₁ = p.eval₂ f g₂ := begin apply finset.sum_congr rfl, intros c hc, dsimp, congr' 1, apply finset.prod_congr rfl, intros i hi, dsimp, congr' 1, apply h hi, rwa finsupp.mem_support_iff at hc end @[simp] lemma eval₂_prod (s : finset S₂) (p : S₂ → mv_polynomial σ R) : eval₂ f g (∏ x in s, p x) = ∏ x in s, eval₂ f g (p x) := (s.prod_hom _).symm @[simp] lemma eval₂_sum (s : finset S₂) (p : S₂ → mv_polynomial σ R) : eval₂ f g (∑ x in s, p x) = ∑ x in s, eval₂ f g (p x) := (s.sum_hom _).symm attribute [to_additive] eval₂_prod lemma eval₂_assoc (q : S₂ → mv_polynomial σ R) (p : mv_polynomial S₂ R) : eval₂ f (λ t, eval₂ f g (q t)) p = eval₂ f g (eval₂ C q p) := begin show _ = eval₂_hom f g (eval₂ C q p), rw eval₂_comp_left (eval₂_hom f g), congr' with a, simp, end end eval₂ section eval variables {f : σ → R} /-- Evaluate a polynomial `p` given a valuation `f` of all the variables -/ def eval (f : σ → R) : mv_polynomial σ R →+* R := eval₂_hom (ring_hom.id _) f lemma eval_eq (x : σ → R) (f : mv_polynomial σ R) : eval x f = ∑ d in f.support, f.coeff d * ∏ i in d.support, x i ^ d i := rfl lemma eval_eq' [fintype σ] (x : σ → R) (f : mv_polynomial σ R) : eval x f = ∑ d in f.support, f.coeff d * ∏ i, x i ^ d i := eval₂_eq' (ring_hom.id R) x f lemma eval_monomial : eval f (monomial s a) = a * s.prod (λn e, f n ^ e) := eval₂_monomial _ _ @[simp] lemma eval_C : ∀ a, eval f (C a) = a := eval₂_C _ _ @[simp] lemma eval_X : ∀ n, eval f (X n) = f n := eval₂_X _ _ @[simp] lemma smul_eval (x) (p : mv_polynomial σ R) (s) : eval x (s • p) = s * eval x p := by rw [smul_eq_C_mul, (eval x).map_mul, eval_C] lemma eval_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ R) (g : σ → R) : eval g (∑ i in s, f i) = ∑ i in s, eval g (f i) := (eval g).map_sum _ _ @[to_additive] lemma eval_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ R) (g : σ → R) : eval g (∏ i in s, f i) = ∏ i in s, eval g (f i) := (eval g).map_prod _ _ theorem eval_assoc {τ} (f : σ → mv_polynomial τ R) (g : τ → R) (p : mv_polynomial σ R) : eval (eval g ∘ f) p = eval g (eval₂ C f p) := begin rw eval₂_comp_left (eval g), unfold eval, simp only [coe_eval₂_hom], congr' with a, simp end end eval section map variables [comm_semiring S₁] variables (f : R →+* S₁) /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : mv_polynomial σ R →+* mv_polynomial σ S₁ := eval₂_hom (C.comp f) X @[simp] theorem map_monomial (s : σ →₀ ℕ) (a : R) : map f (monomial s a) = monomial s (f a) := (eval₂_monomial _ _).trans monomial_eq.symm @[simp] theorem map_C : ∀ (a : R), map f (C a : mv_polynomial σ R) = C (f a) := map_monomial _ _ @[simp] theorem map_X : ∀ (n : σ), map f (X n : mv_polynomial σ R) = X n := eval₂_X _ _ theorem map_id : ∀ (p : mv_polynomial σ R), map (ring_hom.id R) p = p := eval₂_eta theorem map_map [comm_semiring S₂] (g : S₁ →+* S₂) (p : mv_polynomial σ R) : map g (map f p) = map (g.comp f) p := (eval₂_comp_left (map g) (C.comp f) X p).trans $ begin congr, { ext1 a, simp only [map_C, comp_app, ring_hom.coe_comp], }, { ext1 n, simp only [map_X, comp_app], } end theorem eval₂_eq_eval_map (g : σ → S₁) (p : mv_polynomial σ R) : p.eval₂ f g = eval g (map f p) := begin unfold map eval, simp only [coe_eval₂_hom], have h := eval₂_comp_left (eval₂_hom _ g), dsimp at h, rw h, congr, { ext1 a, simp only [coe_eval₂_hom, ring_hom.id_apply, comp_app, eval₂_C, ring_hom.coe_comp], }, { ext1 n, simp only [comp_app, eval₂_X], }, end lemma eval₂_comp_right {S₂} [comm_semiring S₂] (k : S₁ →+* S₂) (f : R →+* S₁) (g : σ → S₁) (p) : k (eval₂ f g p) = eval₂ k (k ∘ g) (map f p) := begin apply mv_polynomial.induction_on p, { intro r, rw [eval₂_C, map_C, eval₂_C] }, { intros p q hp hq, rw [eval₂_add, k.map_add, (map f).map_add, eval₂_add, hp, hq] }, { intros p s hp, rw [eval₂_mul, k.map_mul, (map f).map_mul, eval₂_mul, map_X, hp, eval₂_X, eval₂_X] } end lemma map_eval₂ (f : R →+* S₁) (g : S₂ → mv_polynomial S₃ R) (p : mv_polynomial S₂ R) : map f (eval₂ C g p) = eval₂ C (map f ∘ g) (map f p) := begin apply mv_polynomial.induction_on p, { intro r, rw [eval₂_C, map_C, map_C, eval₂_C] }, { intros p q hp hq, rw [eval₂_add, (map f).map_add, hp, hq, (map f).map_add, eval₂_add] }, { intros p s hp, rw [eval₂_mul, (map f).map_mul, hp, (map f).map_mul, map_X, eval₂_mul, eval₂_X, eval₂_X] } end lemma coeff_map (p : mv_polynomial σ R) : ∀ (m : σ →₀ ℕ), coeff m (map f p) = f (coeff m p) := begin apply mv_polynomial.induction_on p; clear p, { intros r m, rw [map_C], simp only [coeff_C], split_ifs, {refl}, rw f.map_zero }, { intros p q hp hq m, simp only [hp, hq, (map f).map_add, coeff_add], rw f.map_add }, { intros p i hp m, simp only [hp, (map f).map_mul, map_X], simp only [hp, mem_support_iff, coeff_mul_X'], split_ifs, {refl}, rw is_semiring_hom.map_zero f } end lemma map_injective (hf : function.injective f) : function.injective (map f : mv_polynomial σ R → mv_polynomial σ S₁) := begin intros p q h, simp only [ext_iff, coeff_map] at h ⊢, intro m, exact hf (h m), end @[simp] lemma eval_map (f : R →+* S₁) (g : σ → S₁) (p : mv_polynomial σ R) : eval g (map f p) = eval₂ f g p := by { apply mv_polynomial.induction_on p; { simp { contextual := tt } } } @[simp] lemma eval₂_map [comm_semiring S₂] (f : R →+* S₁) (g : σ → S₂) (φ : S₁ →+* S₂) (p : mv_polynomial σ R) : eval₂ φ g (map f p) = eval₂ (φ.comp f) g p := by { rw [← eval_map, ← eval_map, map_map], } @[simp] lemma eval₂_hom_map_hom [comm_semiring S₂] (f : R →+* S₁) (g : σ → S₂) (φ : S₁ →+* S₂) (p : mv_polynomial σ R) : eval₂_hom φ g (map f p) = eval₂_hom (φ.comp f) g p := eval₂_map f g φ p @[simp] lemma constant_coeff_map (f : R →+* S₁) (φ : mv_polynomial σ R) : constant_coeff (mv_polynomial.map f φ) = f (constant_coeff φ) := coeff_map f φ 0 lemma constant_coeff_comp_map (f : R →+* S₁) : (constant_coeff : mv_polynomial σ S₁ →+* S₁).comp (mv_polynomial.map f) = f.comp (constant_coeff) := by { ext, apply constant_coeff_map } lemma support_map_subset (p : mv_polynomial σ R) : (map f p).support ⊆ p.support := begin intro x, simp only [finsupp.mem_support_iff], contrapose!, change p.coeff x = 0 → (map f p).coeff x = 0, rw coeff_map, intro hx, rw hx, exact ring_hom.map_zero f end lemma support_map_of_injective (p : mv_polynomial σ R) {f : R →+* S₁} (hf : injective f) : (map f p).support = p.support := begin apply finset.subset.antisymm, { exact mv_polynomial.support_map_subset _ _ }, intros x hx, rw finsupp.mem_support_iff, contrapose! hx, simp only [not_not, finsupp.mem_support_iff], change (map f p).coeff x = 0 at hx, rw [coeff_map, ← f.map_zero] at hx, exact hf hx end lemma C_dvd_iff_map_hom_eq_zero (q : R →+* S₁) (r : R) (hr : ∀ r' : R, q r' = 0 ↔ r ∣ r') (φ : mv_polynomial σ R) : C r ∣ φ ↔ map q φ = 0 := begin rw [C_dvd_iff_dvd_coeff, mv_polynomial.ext_iff], simp only [coeff_map, ring_hom.coe_of, coeff_zero, hr], end end map section aeval /-! ### The algebra of multivariate polynomials -/ variables (f : σ → S₁) variables [comm_semiring S₁] [algebra R S₁] [comm_semiring S₂] /-- A map `σ → S₁` where `S₁` is an algebra over `R` generates an `R`-algebra homomorphism from multivariate polynomials over `σ` to `S₁`. -/ def aeval : mv_polynomial σ R →ₐ[R] S₁ := { commutes' := λ r, eval₂_C _ _ _ .. eval₂_hom (algebra_map R S₁) f } theorem aeval_def (p : mv_polynomial σ R) : aeval f p = eval₂ (algebra_map R S₁) f p := rfl lemma aeval_eq_eval₂_hom (p : mv_polynomial σ R) : aeval f p = eval₂_hom (algebra_map R S₁) f p := rfl @[simp] lemma aeval_X (s : σ) : aeval f (X s : mv_polynomial _ R) = f s := eval₂_X _ _ _ @[simp] lemma aeval_C (r : R) : aeval f (C r) = algebra_map R S₁ r := eval₂_C _ _ _ theorem eval_unique (φ : mv_polynomial σ R →ₐ[R] S₁) : φ = aeval (φ ∘ X) := begin ext p, apply mv_polynomial.induction_on p, { intro r, rw aeval_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [φ.map_add, ih1, ih2, alg_hom.map_add] }, { intros p j ih, rw [φ.map_mul, alg_hom.map_mul, aeval_X, ih] } end lemma comp_aeval {B : Type*} [comm_semiring B] [algebra R B] (φ : S₁ →ₐ[R] B) : φ.comp (aeval f) = (aeval (λ i, φ (f i))) := begin apply mv_polynomial.alg_hom_ext, intros i, rw [alg_hom.comp_apply, aeval_X, aeval_X], end @[simp] lemma map_aeval {B : Type*} [comm_semiring B] (g : σ → S₁) (φ : S₁ →+* B) (p : mv_polynomial σ R) : φ (aeval g p) = (eval₂_hom (φ.comp (algebra_map R S₁)) (λ i, φ (g i)) p) := by { rw ← comp_eval₂_hom, refl } @[simp] lemma aeval_zero (p : mv_polynomial σ R) : aeval (0 : σ → S₁) p = algebra_map _ _ (constant_coeff p) := begin apply mv_polynomial.induction_on p, { simp only [aeval_C, forall_const, if_true, constant_coeff_C, eq_self_iff_true] }, { intros, simp only [*, alg_hom.map_add, ring_hom.map_add, coeff_add] }, { intros, simp only [ring_hom.map_mul, constant_coeff_X, pi.zero_apply, ring_hom.map_zero, eq_self_iff_true, mem_support_iff, not_true, aeval_X, if_false, ne.def, mul_zero, alg_hom.map_mul, zero_apply] } end @[simp] lemma aeval_zero' (p : mv_polynomial σ R) : aeval (λ _, 0 : σ → S₁) p = algebra_map _ _ (constant_coeff p) := aeval_zero p lemma aeval_monomial (g : σ → S₁) (d : σ →₀ ℕ) (r : R) : aeval g (monomial d r) = algebra_map _ _ r * d.prod (λ i k, g i ^ k) := eval₂_hom_monomial _ _ _ _ lemma eval₂_hom_eq_zero (f : R →+* S₂) (g : σ → S₂) (φ : mv_polynomial σ R) (h : ∀ d, φ.coeff d ≠ 0 → ∃ i ∈ d.support, g i = 0) : eval₂_hom f g φ = 0 := begin rw [φ.as_sum, ring_hom.map_sum, finset.sum_eq_zero], intros d hd, obtain ⟨i, hi, hgi⟩ : ∃ i ∈ d.support, g i = 0 := h d (finsupp.mem_support_iff.mp hd), rw [eval₂_hom_monomial, finsupp.prod, finset.prod_eq_zero hi, mul_zero], rw [hgi, zero_pow], rwa [nat.pos_iff_ne_zero, ← finsupp.mem_support_iff] end lemma aeval_eq_zero [algebra R S₂] (f : σ → S₂) (φ : mv_polynomial σ R) (h : ∀ d, φ.coeff d ≠ 0 → ∃ i ∈ d.support, f i = 0) : aeval f φ = 0 := eval₂_hom_eq_zero _ _ _ h end aeval end comm_semiring end mv_polynomial
2d7a978074efd5509165f473096e4d384dc8ea2e
69bc7d0780be17e452d542a93f9599488f1c0c8e
/11-05-2019.lean
3f42510b6591d7b0e11b874a2a4fbb9d887e83df
[]
no_license
joek13/cs2102-notes
b7352285b1d1184fae25594f89f5926d74e6d7b4
25bb18788641b20af9cf3c429afe1da9b2f5eafb
refs/heads/master
1,673,461,162,867
1,575,561,090,000
1,575,561,090,000
207,573,549
0
0
null
null
null
null
UTF-8
Lean
false
false
1,501
lean
-- propositions -- proofs -- predicates -- sets -- relations -- equality -- connectives -- not -- and -- or -- proposition inductive nifty_was_a_cat : Prop -- proof | there_are_pictures_of_nifty | we_remember_nifty_fondly inductive pet : Type | nifty | tom | cheese | kevin open pet inductive was_a_cat : pet → Prop | nifty_proof : was_a_cat nifty | tom_proof : was_a_cat tom open was_a_cat theorem nifty_cat : was_a_cat nifty := nifty_proof -- Predicates with single arguments, in essence, define a *set* -- The elements of this set all satisfy the predicate -- (elements that do not satisfy the predicate are not members) -- A kind of "membership test" for being in a set inductive and'' (α β : Prop) : Prop | intro : α → β → and'' def nwac_and_cwac : Prop := and'' (was_a_cat nifty) (was_a_cat cheese) theorem pf : (and'' (was_a_cat nifty) (was_a_cat tom)) := and''.intro (nifty_proof) (tom_proof) def and''_intro (α β : Prop) : α → β → (and'' α β) := and''.intro def and''_elim_left {α β : Prop} : (and'' α β) → α | (and''.intro a b) := a def and''_elim_right {α β : Prop} : (and'' α β) → β | (and''.intro a b) := b theorem twac : (was_a_cat tom) := (and''_elim_right pf) -- if you have a data type of one constructor, you can use the "structure" keyword to declare it inductive and''' (α β : Prop) : Prop | intro (left : α) (right : β) : and''' structure and' (a b : Prop) : Prop := intro :: (left : a) (right : b)
7816e302143cc6b458891cafa185974d708c9bfb
41ebf3cb010344adfa84907b3304db00e02db0a6
/uexp/src/uexp/rules/transitiveInferenceAggregate.lean
85ef59a4fded47f040da7c356b296ae18861f6e5
[ "BSD-2-Clause" ]
permissive
ReinierKoops/Cosette
e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb
eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29
refs/heads/master
1,686,483,953,198
1,624,293,498,000
1,624,293,498,000
378,997,885
0
0
BSD-2-Clause
1,624,293,485,000
1,624,293,484,000
null
UTF-8
Lean
false
false
2,896
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..meta.cosette_tactics open Expr open Proj open Pred open SQL open tree notation `int` := datatypes.int constant integer_1 : const int constant integer_7 : const int lemma rule: forall ( Γ scm_t scm_account scm_bonus scm_dept scm_emp: Schema) (rel_t: relation scm_t) (rel_account: relation scm_account) (rel_bonus: relation scm_bonus) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (t_k0 : Column int scm_t) (t_c1 : Column int scm_t) (t_f1_a0 : Column int scm_t) (t_f2_a0 : Column int scm_t) (t_f0_c0 : Column int scm_t) (t_f1_c0 : Column int scm_t) (t_f0_c1 : Column int scm_t) (t_f1_c2 : Column int scm_t) (t_f2_c3 : Column int scm_t) (account_acctno : Column int scm_account) (account_type : Column int scm_account) (account_balance : Column int scm_account) (bonus_ename : Column int scm_bonus) (bonus_job : Column int scm_bonus) (bonus_sal : Column int scm_bonus) (bonus_comm : Column int scm_bonus) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp), denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product ((SELECT (combineGroupByProj PLAIN(uvariable (right⋅emp_deptno)) (COUNT(uvariable (right⋅emp_slacker)))) FROM1 ((table rel_emp) WHERE (castPred (combine (right⋅emp_deptno) (e2p (constantExpr integer_7)) ) predicates.gt)) GROUP BY (right⋅emp_deptno))) (table rel_emp)) WHERE (equal (uvariable (right⋅left⋅left)) (uvariable (right⋅right⋅emp_deptno)))) : SQL Γ _ ) = denoteSQL ((SELECT1 (e2p (constantExpr integer_1)) FROM1 (product ((SELECT (combineGroupByProj PLAIN(uvariable (right⋅emp_deptno)) (COUNT(uvariable (right⋅emp_slacker)))) FROM1 (table rel_emp) WHERE (castPred (combine (right⋅emp_deptno) (e2p (constantExpr integer_7)) ) predicates.gt) GROUP BY (right⋅emp_deptno))) ((SELECT * FROM1 (table rel_emp) WHERE (castPred (combine (right⋅emp_deptno) (e2p (constantExpr integer_7)) ) predicates.gt)))) WHERE (equal (uvariable (right⋅left⋅left)) (uvariable (right⋅right⋅emp_deptno)))) : SQL Γ _) := begin intros, unfold_all_denotations, funext, dsimp, print_size, simp, print_size, congr, funext, apply congr_arg _, funext, apply congr_arg _, funext, congr, sorry -- TODO: here push predicate into squashed expressions and SDP is required end
8ca2caddc5701d7104326f548f54ca3e14b5cf30
80746c6dba6a866de5431094bf9f8f841b043d77
/src/category_theory/functor_category.lean
4233f1f745f7416cd08b36c8c59a28fb43b2c619
[ "Apache-2.0" ]
permissive
leanprover-fork/mathlib-backup
8b5c95c535b148fca858f7e8db75a76252e32987
0eb9db6a1a8a605f0cf9e33873d0450f9f0ae9b0
refs/heads/master
1,585,156,056,139
1,548,864,430,000
1,548,864,438,000
143,964,213
0
0
Apache-2.0
1,550,795,966,000
1,533,705,322,000
Lean
UTF-8
Lean
false
false
2,719
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Tim Baumann, Stephen Morgan, Scott Morrison import category_theory.natural_transformation namespace category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation open nat_trans variables (C : Type u₁) [𝒞 : category.{v₁} C] (D : Type u₂) [𝒟 : category.{v₂} D] include 𝒞 𝒟 /-- `functor.category C D` gives the category structure on functors and natural transformations between categories `C` and `D`. Notice that if `C` and `D` are both small categories at the same universe level, this is another small category at that level. However if `C` and `D` are both large categories at the same universe level, this is a small category at the next higher level. -/ instance functor.category : category.{(max u₁ v₂)} (C ⥤ D) := { hom := λ F G, F ⟹ G, id := λ F, nat_trans.id F, comp := λ _ _ _ α β, α ⊟ β } variables {C D} {E : Type u₃} [ℰ : category.{v₃} E] namespace functor.category section @[simp] lemma id_app (F : C ⥤ D) (X : C) : (𝟙 F : F ⟹ F).app X = 𝟙 (F.obj X) := rfl @[simp] lemma comp_app {F G H : C ⥤ D} (α : F ⟶ G) (β : G ⟶ H) (X : C) : ((α ≫ β) : F ⟹ H).app X = (α : F ⟹ G).app X ≫ (β : G ⟹ H).app X := rfl end namespace nat_trans -- This section gives two lemmas about natural transformations -- between functors into functor categories, -- spelling them out in components. include ℰ lemma app_naturality {F G : C ⥤ (D ⥤ E)} (T : F ⟹ G) (X : C) {Y Z : D} (f : Y ⟶ Z) : ((F.obj X).map f) ≫ ((T.app X).app Z) = ((T.app X).app Y) ≫ ((G.obj X).map f) := (T.app X).naturality f lemma naturality_app {F G : C ⥤ (D ⥤ E)} (T : F ⟹ G) (Z : D) {X Y : C} (f : X ⟶ Y) : ((F.map f).app Z) ≫ ((T.app Y).app Z) = ((T.app X).app Z) ≫ ((G.map f).app Z) := congr_fun (congr_arg app (T.naturality f)) Z end nat_trans end functor.category namespace functor include ℰ protected def flip (F : C ⥤ (D ⥤ E)) : D ⥤ (C ⥤ E) := { obj := λ k, { obj := λ j, (F.obj j).obj k, map := λ j j' f, (F.map f).app k, map_id' := λ X, begin rw category_theory.functor.map_id, refl end, map_comp' := λ X Y Z f g, by rw [functor.map_comp, ←functor.category.comp_app] }, map := λ c c' f, { app := λ j, (F.obj j).map f, naturality' := λ X Y g, by dsimp; rw ←nat_trans.naturality } }. @[simp] lemma flip_obj_map (F : C ⥤ (D ⥤ E)) {c c' : C} (f : c ⟶ c') (d : D) : ((F.flip).obj d).map f = (F.map f).app d := rfl end functor end category_theory
1a36bb339f68c36dcf31f80bd6a8524ff062e474
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/analysis/inner_product_space/lax_milgram.lean
85a09de31cf8508c5dca61aea87c50c8b84c4c1d
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
4,484
lean
/- Copyright (c) 2022 Daniel Roca González. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Roca González -/ import analysis.inner_product_space.projection import analysis.inner_product_space.dual import analysis.normed_space.banach import analysis.normed_space.operator_norm import topology.metric_space.antilipschitz /-! # The Lax-Milgram Theorem We consider an Hilbert space `V` over `ℝ` equipped with a bounded bilinear form `B : V →L[ℝ] V →L[ℝ] ℝ`. Recall that a bilinear form `B : V →L[ℝ] V →L[ℝ] ℝ` is *coercive* iff `∃ C, (0 < C) ∧ ∀ u, C * ∥u∥ * ∥u∥ ≤ B u u`. Under the hypothesis that `B` is coercive we prove the Lax-Milgram theorem: that is, the map `inner_product_space.continuous_linear_map_of_bilin` from `analysis.inner_product_space.dual` can be upgraded to a continuous equivalence `is_coercive.continuous_linear_equiv_of_bilin : V ≃L[ℝ] V`. ## References * We follow the notes of Peter Howard's Spring 2020 *M612: Partial Differential Equations* lecture, see[howard] ## Tags dual, Lax-Milgram -/ noncomputable theory open is_R_or_C linear_map continuous_linear_map inner_product_space open_locale real_inner_product_space nnreal universe u namespace is_coercive variables {V : Type u} [inner_product_space ℝ V] [complete_space V] variables {B : V →L[ℝ] V →L[ℝ] ℝ} local postfix `♯`:1025 := @continuous_linear_map_of_bilin ℝ V _ _ _ lemma bounded_below (coercive : is_coercive B) : ∃ C, 0 < C ∧ ∀ v, C * ∥v∥ ≤ ∥B♯ v∥ := begin rcases coercive with ⟨C, C_ge_0, coercivity⟩, refine ⟨C, C_ge_0, _⟩, intro v, by_cases h : 0 < ∥v∥, { refine (mul_le_mul_right h).mp _, exact calc C * ∥v∥ * ∥v∥ ≤ B v v : coercivity v ... = ⟪B♯ v, v⟫_ℝ : by simp ... ≤ ∥B♯ v∥ * ∥v∥ : real_inner_le_norm (B♯ v) v, }, { have : v = 0 := by simpa using h, simp [this], } end lemma antilipschitz (coercive : is_coercive B) : ∃ C : ℝ≥0, 0 < C ∧ antilipschitz_with C B♯ := begin rcases coercive.bounded_below with ⟨C, C_pos, below_bound⟩, refine ⟨(C⁻¹).to_nnreal, real.to_nnreal_pos.mpr (inv_pos.mpr C_pos), _⟩, refine linear_map.antilipschitz_of_bound B♯ _, simp_rw [real.coe_to_nnreal', max_eq_left_of_lt (inv_pos.mpr C_pos), ←inv_mul_le_iff (inv_pos.mpr C_pos)], simpa using below_bound, end lemma ker_eq_bot (coercive : is_coercive B) : B♯.ker = ⊥ := begin rw [←ker_coe, linear_map.ker_eq_bot], rcases coercive.antilipschitz with ⟨_, _, antilipschitz⟩, exact antilipschitz.injective, end lemma closed_range (coercive : is_coercive B) : is_closed (B♯.range : set V) := begin rcases coercive.antilipschitz with ⟨_, _, antilipschitz⟩, exact antilipschitz.is_closed_range B♯.uniform_continuous, end lemma range_eq_top (coercive : is_coercive B) : B♯.range = ⊤ := begin haveI := coercive.closed_range.complete_space_coe, rw ← B♯.range.orthogonal_orthogonal, rw submodule.eq_top_iff', intros v w mem_w_orthogonal, rcases coercive with ⟨C, C_ge_0, coercivity⟩, have : C * ∥w∥ * ∥w∥ ≤ 0 := calc C * ∥w∥ * ∥w∥ ≤ B w w : coercivity w ... = ⟪B♯ w, w⟫_ℝ : by simp ... = 0 : mem_w_orthogonal _ ⟨w, rfl⟩, have : ∥w∥ * ∥w∥ ≤ 0 := by nlinarith, have h : ∥w∥ = 0 := by nlinarith [norm_nonneg w], have w_eq_zero : w = 0 := by simpa using h, simp [w_eq_zero], end /-- The Lax-Milgram equivalence of a coercive bounded bilinear operator: for all `v : V`, `continuous_linear_equiv_of_bilin B v` is the unique element `V` such that `⟪continuous_linear_equiv_of_bilin B v, w⟫ = B v w`. The Lax-Milgram theorem states that this is a continuous equivalence. -/ def continuous_linear_equiv_of_bilin (coercive : is_coercive B) : V ≃L[ℝ] V := continuous_linear_equiv.of_bijective B♯ coercive.ker_eq_bot coercive.range_eq_top @[simp] lemma continuous_linear_equiv_of_bilin_apply (coercive : is_coercive B) (v w : V) : ⟪coercive.continuous_linear_equiv_of_bilin v, w⟫_ℝ = B v w := continuous_linear_map_of_bilin_apply ℝ B v w lemma unique_continuous_linear_equiv_of_bilin (coercive : is_coercive B) {v f : V} (is_lax_milgram : (∀ w, ⟪f, w⟫_ℝ = B v w)) : f = coercive.continuous_linear_equiv_of_bilin v := unique_continuous_linear_map_of_bilin ℝ B is_lax_milgram end is_coercive
2f1ebbcbbb793a6edc7aff42839858c056b06647
94e33a31faa76775069b071adea97e86e218a8ee
/src/number_theory/sum_four_squares.lean
c9f5476093a622a813a78edd78b37015ea520011
[ "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
11,862
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.group_power.identities import data.zmod.basic import field_theory.finite.basic import data.int.parity import data.fintype.card /-! # Lagrange's four square theorem The main result in this file is `sum_four_squares`, a proof that every natural number is the sum of four square numbers. ## Implementation Notes The proof used is close to Lagrange's original proof. -/ open finset polynomial finite_field equiv open_locale big_operators namespace int lemma sq_add_sq_of_two_mul_sq_add_sq {m x y : ℤ} (h : 2 * m = x^2 + y^2) : m = ((x - y) / 2) ^ 2 + ((x + y) / 2) ^ 2 := have even (x^2 + y^2), by simp [h.symm, even_mul], have hxaddy : even (x + y), by simpa [sq] with parity_simps, have hxsuby : even (x - y), by simpa [sq] with parity_simps, (mul_right_inj' (show (2*2 : ℤ) ≠ 0, from dec_trivial)).1 $ calc 2 * 2 * m = (x - y)^2 + (x + y)^2 : by rw [mul_assoc, h]; ring ... = (2 * ((x - y) / 2))^2 + (2 * ((x + y) / 2))^2 : by { rw even_iff_two_dvd at hxsuby hxaddy, rw [int.mul_div_cancel' hxsuby, int.mul_div_cancel' hxaddy] } ... = 2 * 2 * (((x - y) / 2) ^ 2 + ((x + y) / 2) ^ 2) : by simp [mul_add, pow_succ, mul_comm, mul_assoc, mul_left_comm] lemma exists_sq_add_sq_add_one_eq_k (p : ℕ) [hp : fact p.prime] : ∃ (a b : ℤ) (k : ℕ), a^2 + b^2 + 1 = k * p ∧ k < p := hp.1.eq_two_or_odd.elim (λ hp2, hp2.symm ▸ ⟨1, 0, 1, rfl, dec_trivial⟩) $ λ hp1, let ⟨a, b, hab⟩ := zmod.sq_add_sq p (-1) in have hab' : (p : ℤ) ∣ a.val_min_abs ^ 2 + b.val_min_abs ^ 2 + 1, from (char_p.int_cast_eq_zero_iff (zmod p) p _).1 $ by simpa [eq_neg_iff_add_eq_zero] using hab, let ⟨k, hk⟩ := hab' in have hk0 : 0 ≤ k, from nonneg_of_mul_nonneg_right (by rw ← hk; exact (add_nonneg (add_nonneg (sq_nonneg _) (sq_nonneg _)) zero_le_one)) (int.coe_nat_pos.2 hp.1.pos), ⟨a.val_min_abs, b.val_min_abs, k.nat_abs, by rw [hk, int.nat_abs_of_nonneg hk0, mul_comm], lt_of_mul_lt_mul_left (calc p * k.nat_abs = a.val_min_abs.nat_abs ^ 2 + b.val_min_abs.nat_abs ^ 2 + 1 : by rw [← int.coe_nat_inj', int.coe_nat_add, int.coe_nat_add, int.coe_nat_pow, int.coe_nat_pow, int.nat_abs_sq, int.nat_abs_sq, int.coe_nat_one, hk, int.coe_nat_mul, int.nat_abs_of_nonneg hk0] ... ≤ (p / 2) ^ 2 + (p / 2)^2 + 1 : add_le_add (add_le_add (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) le_rfl ... < (p / 2) ^ 2 + (p / 2)^ 2 + (p % 2)^2 + ((2 * (p / 2)^2 + (4 * (p / 2) * (p % 2)))) : by rw [hp1, one_pow, mul_one]; exact (lt_add_iff_pos_right _).2 (add_pos_of_nonneg_of_pos (nat.zero_le _) (mul_pos dec_trivial (nat.div_pos hp.1.two_le dec_trivial))) ... = p * p : by { conv_rhs { rw [← nat.mod_add_div p 2] }, ring }) (show 0 ≤ p, from nat.zero_le _)⟩ end int namespace nat open int open_locale classical private lemma sum_four_squares_of_two_mul_sum_four_squares {m a b c d : ℤ} (h : a^2 + b^2 + c^2 + d^2 = 2 * m) : ∃ w x y z : ℤ, w^2 + x^2 + y^2 + z^2 = m := have ∀ f : fin 4 → zmod 2, (f 0)^2 + (f 1)^2 + (f 2)^2 + (f 3)^2 = 0 → ∃ i : (fin 4), (f i)^2 + f (swap i 0 1)^2 = 0 ∧ f (swap i 0 2)^2 + f (swap i 0 3)^2 = 0, from dec_trivial, let f : fin 4 → ℤ := vector.nth (a ::ᵥ b ::ᵥ c ::ᵥ d ::ᵥ vector.nil) in let ⟨i, hσ⟩ := this (coe ∘ f) (by rw [← @zero_mul (zmod 2) _ m, ← show ((2 : ℤ) : zmod 2) = 0, from rfl, ← int.cast_mul, ← h]; simp only [int.cast_add, int.cast_pow]; refl) in let σ := swap i 0 in have h01 : 2 ∣ f (σ 0) ^ 2 + f (σ 1) ^ 2, from (char_p.int_cast_eq_zero_iff (zmod 2) 2 _).1 $ by simpa [σ] using hσ.1, have h23 : 2 ∣ f (σ 2) ^ 2 + f (σ 3) ^ 2, from (char_p.int_cast_eq_zero_iff (zmod 2) 2 _).1 $ by simpa using hσ.2, let ⟨x, hx⟩ := h01 in let ⟨y, hy⟩ := h23 in ⟨(f (σ 0) - f (σ 1)) / 2, (f (σ 0) + f (σ 1)) / 2, (f (σ 2) - f (σ 3)) / 2, (f (σ 2) + f (σ 3)) / 2, begin rw [← int.sq_add_sq_of_two_mul_sq_add_sq hx.symm, add_assoc, ← int.sq_add_sq_of_two_mul_sq_add_sq hy.symm, ← mul_right_inj' (show (2 : ℤ) ≠ 0, from dec_trivial), ← h, mul_add, ← hx, ← hy], have : ∑ x, f (σ x)^2 = ∑ x, f x^2, { conv_rhs { rw ←equiv.sum_comp σ } }, have fin4univ : (univ : finset (fin 4)).1 = 0 ::ₘ 1 ::ₘ 2 ::ₘ 3 ::ₘ 0, from dec_trivial, simpa [finset.sum_eq_multiset_sum, fin4univ, multiset.sum_cons, f, add_assoc] end⟩ private lemma prime_sum_four_squares (p : ℕ) [hp : _root_.fact p.prime] : ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = p := have hm : ∃ m < p, 0 < m ∧ ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = m * p, from let ⟨a, b, k, hk⟩ := exists_sq_add_sq_add_one_eq_k p in ⟨k, hk.2, nat.pos_of_ne_zero $ (λ hk0, by { rw [hk0, int.coe_nat_zero, zero_mul] at hk, exact ne_of_gt (show a^2 + b^2 + 1 > 0, from add_pos_of_nonneg_of_pos (add_nonneg (sq_nonneg _) (sq_nonneg _)) zero_lt_one) hk.1 }), a, b, 1, 0, by simpa [sq] using hk.1⟩, let m := nat.find hm in let ⟨a, b, c, d, (habcd : a^2 + b^2 + c^2 + d^2 = m * p)⟩ := (nat.find_spec hm).snd.2 in by haveI hm0 : _root_.fact (0 < m) := ⟨(nat.find_spec hm).snd.1⟩; exact have hmp : m < p, from (nat.find_spec hm).fst, m.mod_two_eq_zero_or_one.elim (λ hm2 : m % 2 = 0, let ⟨k, hk⟩ := nat.dvd_iff_mod_eq_zero.2 hm2 in have hk0 : 0 < k, from nat.pos_of_ne_zero $ λ _, by { simp [*, lt_irrefl] at * }, have hkm : k < m, { rw [hk, two_mul], exact (lt_add_iff_pos_left _).2 hk0 }, false.elim $ nat.find_min hm hkm ⟨lt_trans hkm hmp, hk0, sum_four_squares_of_two_mul_sum_four_squares (show a^2 + b^2 + c^2 + d^2 = 2 * (k * p), by { rw [habcd, hk, int.coe_nat_mul, mul_assoc], norm_num })⟩) (λ hm2 : m % 2 = 1, if hm1 : m = 1 then ⟨a, b, c, d, by simp only [hm1, habcd, int.coe_nat_one, one_mul]⟩ else let w := (a : zmod m).val_min_abs, x := (b : zmod m).val_min_abs, y := (c : zmod m).val_min_abs, z := (d : zmod m).val_min_abs in have hnat_abs : w^2 + x^2 + y^2 + z^2 = (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs ^2 + z.nat_abs ^ 2 : ℕ), by simp [sq], have hwxyzlt : w^2 + x^2 + y^2 + z^2 < m^2, from calc w^2 + x^2 + y^2 + z^2 = (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs ^2 + z.nat_abs ^ 2 : ℕ) : hnat_abs ... ≤ ((m / 2) ^ 2 + (m / 2) ^ 2 + (m / 2) ^ 2 + (m / 2) ^ 2 : ℕ) : int.coe_nat_le.2 $ add_le_add (add_le_add (add_le_add (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) ... = 4 * (m / 2 : ℕ) ^ 2 : by simp [sq, bit0, bit1, mul_add, add_mul, add_assoc] ... < 4 * (m / 2 : ℕ) ^ 2 + ((4 * (m / 2) : ℕ) * (m % 2 : ℕ) + (m % 2 : ℕ)^2) : (lt_add_iff_pos_right _).2 (by { rw [hm2, int.coe_nat_one, one_pow, mul_one], exact add_pos_of_nonneg_of_pos (int.coe_nat_nonneg _) zero_lt_one }) ... = m ^ 2 : by { conv_rhs {rw [← nat.mod_add_div m 2]}, simp [-nat.mod_add_div, mul_add, add_mul, bit0, bit1, mul_comm, mul_assoc, mul_left_comm, pow_add, add_comm, add_left_comm] }, have hwxyzabcd : ((w^2 + x^2 + y^2 + z^2 : ℤ) : zmod m) = ((a^2 + b^2 + c^2 + d^2 : ℤ) : zmod m), by simp [w, x, y, z, sq], have hwxyz0 : ((w^2 + x^2 + y^2 + z^2 : ℤ) : zmod m) = 0, by rw [hwxyzabcd, habcd, int.cast_mul, cast_coe_nat, zmod.nat_cast_self, zero_mul], let ⟨n, hn⟩ := ((char_p.int_cast_eq_zero_iff _ m _).1 hwxyz0) in have hn0 : 0 < n.nat_abs, from int.nat_abs_pos_of_ne_zero (λ hn0, have hwxyz0 : (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs^2 + z.nat_abs^2 : ℕ) = 0, by { rw [← int.coe_nat_eq_zero, ← hnat_abs], rwa [hn0, mul_zero] at hn }, have habcd0 : (m : ℤ) ∣ a ∧ (m : ℤ) ∣ b ∧ (m : ℤ) ∣ c ∧ (m : ℤ) ∣ d, by simpa [add_eq_zero_iff' (sq_nonneg (_ : ℤ)) (sq_nonneg _), pow_two, w, x, y, z, (char_p.int_cast_eq_zero_iff _ m _), and.assoc] using hwxyz0, let ⟨ma, hma⟩ := habcd0.1, ⟨mb, hmb⟩ := habcd0.2.1, ⟨mc, hmc⟩ := habcd0.2.2.1, ⟨md, hmd⟩ := habcd0.2.2.2 in have hmdvdp : m ∣ p, from int.coe_nat_dvd.1 ⟨ma^2 + mb^2 + mc^2 + md^2, (mul_right_inj' (show (m : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 hm0.1)).1 $ by { rw [← habcd, hma, hmb, hmc, hmd], ring }⟩, (hp.1.eq_one_or_self_of_dvd _ hmdvdp).elim hm1 (λ hmeqp, by simpa [lt_irrefl, hmeqp] using hmp)), have hawbxcydz : ((m : ℕ) : ℤ) ∣ a * w + b * x + c * y + d * z, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { rw [← hwxyz0], simp, ring }, have haxbwczdy : ((m : ℕ) : ℤ) ∣ a * x - b * w - c * z + d * y, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { simp [sub_eq_add_neg], ring }, have haybzcwdx : ((m : ℕ) : ℤ) ∣ a * y + b * z - c * w - d * x, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { simp [sub_eq_add_neg], ring }, have hazbycxdw : ((m : ℕ) : ℤ) ∣ a * z - b * y + c * x - d * w, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { simp [sub_eq_add_neg], ring }, let ⟨s, hs⟩ := hawbxcydz, ⟨t, ht⟩ := haxbwczdy, ⟨u, hu⟩ := haybzcwdx, ⟨v, hv⟩ := hazbycxdw in have hn_nonneg : 0 ≤ n, from nonneg_of_mul_nonneg_right (by { erw [← hn], repeat {try {refine add_nonneg _ _}, try {exact sq_nonneg _}} }) (int.coe_nat_pos.2 hm0.1), have hnm : n.nat_abs < m, from int.coe_nat_lt.1 (lt_of_mul_lt_mul_left (by { rw [int.nat_abs_of_nonneg hn_nonneg, ← hn, ← sq], exact hwxyzlt }) (int.coe_nat_nonneg m)), have hstuv : s^2 + t^2 + u^2 + v^2 = n.nat_abs * p, from (mul_right_inj' (show (m^2 : ℤ) ≠ 0, from pow_ne_zero 2 (int.coe_nat_ne_zero_iff_pos.2 hm0.1))).1 $ calc (m : ℤ)^2 * (s^2 + t^2 + u^2 + v^2) = ((m : ℕ) * s)^2 + ((m : ℕ) * t)^2 + ((m : ℕ) * u)^2 + ((m : ℕ) * v)^2 : by { simp [mul_pow], ring } ... = (w^2 + x^2 + y^2 + z^2) * (a^2 + b^2 + c^2 + d^2) : by { simp only [hs.symm, ht.symm, hu.symm, hv.symm], ring } ... = _ : by { rw [hn, habcd, int.nat_abs_of_nonneg hn_nonneg], dsimp [m], ring }, false.elim $ nat.find_min hm hnm ⟨lt_trans hnm hmp, hn0, s, t, u, v, hstuv⟩) /-- **Four squares theorem** -/ lemma sum_four_squares : ∀ n : ℕ, ∃ a b c d : ℕ, a^2 + b^2 + c^2 + d^2 = n | 0 := ⟨0, 0, 0, 0, rfl⟩ | 1 := ⟨1, 0, 0, 0, rfl⟩ | n@(k+2) := have hm : _root_.fact (min_fac (k+2)).prime := ⟨min_fac_prime dec_trivial⟩, have n / min_fac n < n := factors_lemma, let ⟨a, b, c, d, h₁⟩ := show ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = min_fac n, by exactI prime_sum_four_squares (min_fac (k+2)) in let ⟨w, x, y, z, h₂⟩ := sum_four_squares (n / min_fac n) in ⟨(a * w - b * x - c * y - d * z).nat_abs, (a * x + b * w + c * z - d * y).nat_abs, (a * y - b * z + c * w + d * x).nat_abs, (a * z + b * y - c * x + d * w).nat_abs, begin rw [← int.coe_nat_inj', ← nat.mul_div_cancel' (min_fac_dvd (k+2)), int.coe_nat_mul, ← h₁, ← h₂], simp [sum_four_sq_mul_sum_four_sq], end⟩ end nat
79ab165364080270eda480d468466933dd74921e
94e33a31faa76775069b071adea97e86e218a8ee
/src/topology/instances/matrix.lean
2ecca9e4fa0664e7cca561e777bc9757f250a6ab
[ "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
18,456
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash, Eric Wieser -/ import linear_algebra.determinant import topology.algebra.infinite_sum import topology.algebra.ring import topology.algebra.star /-! # Topological properties of matrices This file is a place to collect topological results about matrices. ## Main definitions: * `matrix.topological_ring`: square matrices form a topological ring ## Main results * Continuity: * `continuous.matrix_det`: the determinant is continuous over a topological ring. * `continuous.matrix_adjugate`: the adjugate is continuous over a topological ring. * Infinite sums * `matrix.transpose_tsum`: transpose commutes with infinite sums * `matrix.diagonal_tsum`: diagonal commutes with infinite sums * `matrix.block_diagonal_tsum`: block diagonal commutes with infinite sums * `matrix.block_diagonal'_tsum`: non-uniform block diagonal commutes with infinite sums -/ open matrix open_locale matrix variables {X α l m n p S R : Type*} {m' n' : l → Type*} instance [topological_space R] : topological_space (matrix m n R) := Pi.topological_space instance [topological_space R] [t2_space R] : t2_space (matrix m n R) := Pi.t2_space /-! ### Lemmas about continuity of operations -/ section continuity variables [topological_space X] [topological_space R] instance [has_smul α R] [has_continuous_const_smul α R] : has_continuous_const_smul α (matrix m n R) := pi.has_continuous_const_smul instance [topological_space α] [has_smul α R] [has_continuous_smul α R] : has_continuous_smul α (matrix m n R) := pi.has_continuous_smul instance [has_add R] [has_continuous_add R] : has_continuous_add (matrix m n R) := pi.has_continuous_add instance [has_neg R] [has_continuous_neg R] : has_continuous_neg (matrix m n R) := pi.has_continuous_neg instance [add_group R] [topological_add_group R] : topological_add_group (matrix m n R) := pi.topological_add_group /-- To show a function into matrices is continuous it suffices to show the coefficients of the resulting matrix are continuous -/ @[continuity] lemma continuous_matrix [topological_space α] {f : α → matrix m n R} (h : ∀ i j, continuous (λ a, f a i j)) : continuous f := continuous_pi $ λ _, continuous_pi $ λ j, h _ _ lemma continuous.matrix_elem {A : X → matrix m n R} (hA : continuous A) (i : m) (j : n) : continuous (λ x, A x i j) := (continuous_apply_apply i j).comp hA @[continuity] lemma continuous.matrix_map [topological_space S] {A : X → matrix m n S} {f : S → R} (hA : continuous A) (hf : continuous f) : continuous (λ x, (A x).map f) := continuous_matrix $ λ i j, hf.comp $ hA.matrix_elem _ _ @[continuity] lemma continuous.matrix_transpose {A : X → matrix m n R} (hA : continuous A) : continuous (λ x, (A x)ᵀ) := continuous_matrix $ λ i j, hA.matrix_elem j i lemma continuous.matrix_conj_transpose [has_star R] [has_continuous_star R] {A : X → matrix m n R} (hA : continuous A) : continuous (λ x, (A x)ᴴ) := hA.matrix_transpose.matrix_map continuous_star instance [has_star R] [has_continuous_star R] : has_continuous_star (matrix m m R) := ⟨continuous_id.matrix_conj_transpose⟩ @[continuity] lemma continuous.matrix_col {A : X → n → R} (hA : continuous A) : continuous (λ x, col (A x)) := continuous_matrix $ λ i j, (continuous_apply _).comp hA @[continuity] lemma continuous.matrix_row {A : X → n → R} (hA : continuous A) : continuous (λ x, row (A x)) := continuous_matrix $ λ i j, (continuous_apply _).comp hA @[continuity] lemma continuous.matrix_diagonal [has_zero R] [decidable_eq n] {A : X → n → R} (hA : continuous A) : continuous (λ x, diagonal (A x)) := continuous_matrix $ λ i j, ((continuous_apply i).comp hA).if_const _ continuous_zero @[continuity] lemma continuous.matrix_dot_product [fintype n] [has_mul R] [add_comm_monoid R] [has_continuous_add R] [has_continuous_mul R] {A : X → n → R} {B : X → n → R} (hA : continuous A) (hB : continuous B) : continuous (λ x, dot_product (A x) (B x)) := continuous_finset_sum _ $ λ i _, ((continuous_apply i).comp hA).mul ((continuous_apply i).comp hB) /-- For square matrices the usual `continuous_mul` can be used. -/ @[continuity] lemma continuous.matrix_mul [fintype n] [has_mul R] [add_comm_monoid R] [has_continuous_add R] [has_continuous_mul R] {A : X → matrix m n R} {B : X → matrix n p R} (hA : continuous A) (hB : continuous B) : continuous (λ x, (A x).mul (B x)) := continuous_matrix $ λ i j, continuous_finset_sum _ $ λ k _, (hA.matrix_elem _ _).mul (hB.matrix_elem _ _) instance [fintype n] [has_mul R] [add_comm_monoid R] [has_continuous_add R] [has_continuous_mul R] : has_continuous_mul (matrix n n R) := ⟨continuous_fst.matrix_mul continuous_snd⟩ instance [fintype n] [non_unital_non_assoc_semiring R] [topological_semiring R] : topological_semiring (matrix n n R) := {} instance [fintype n] [non_unital_non_assoc_ring R] [topological_ring R] : topological_ring (matrix n n R) := {} @[continuity] lemma continuous.matrix_vec_mul_vec [has_mul R] [has_continuous_mul R] {A : X → m → R} {B : X → n → R} (hA : continuous A) (hB : continuous B) : continuous (λ x, vec_mul_vec (A x) (B x)) := continuous_matrix $ λ i j, ((continuous_apply _).comp hA).mul ((continuous_apply _).comp hB) @[continuity] lemma continuous.matrix_mul_vec [non_unital_non_assoc_semiring R] [has_continuous_add R] [has_continuous_mul R] [fintype n] {A : X → matrix m n R} {B : X → n → R} (hA : continuous A) (hB : continuous B) : continuous (λ x, (A x).mul_vec (B x)) := continuous_pi $ λ i, ((continuous_apply i).comp hA).matrix_dot_product hB @[continuity] lemma continuous.matrix_vec_mul [non_unital_non_assoc_semiring R] [has_continuous_add R] [has_continuous_mul R] [fintype m] {A : X → m → R} {B : X → matrix m n R} (hA : continuous A) (hB : continuous B) : continuous (λ x, vec_mul (A x) (B x)) := continuous_pi $ λ i, hA.matrix_dot_product $ continuous_pi $ λ j, hB.matrix_elem _ _ @[continuity] lemma continuous.matrix_minor {A : X → matrix l n R} (hA : continuous A) (e₁ : m → l) (e₂ : p → n) : continuous (λ x, (A x).minor e₁ e₂) := continuous_matrix $ λ i j, hA.matrix_elem _ _ @[continuity] lemma continuous.matrix_reindex {A : X → matrix l n R} (hA : continuous A) (e₁ : l ≃ m) (e₂ : n ≃ p) : continuous (λ x, reindex e₁ e₂ (A x)) := hA.matrix_minor _ _ @[continuity] lemma continuous.matrix_diag {A : X → matrix n n R} (hA : continuous A) : continuous (λ x, matrix.diag (A x)) := continuous_pi $ λ _, hA.matrix_elem _ _ -- note this doesn't elaborate well from the above lemma continuous_matrix_diag : continuous (matrix.diag : matrix n n R → n → R) := show continuous (λ x : matrix n n R, matrix.diag x), from continuous_id.matrix_diag @[continuity] lemma continuous.matrix_trace [fintype n] [add_comm_monoid R] [has_continuous_add R] {A : X → matrix n n R} (hA : continuous A) : continuous (λ x, trace (A x)) := continuous_finset_sum _ $ λ i hi, hA.matrix_elem _ _ @[continuity] lemma continuous.matrix_det [fintype n] [decidable_eq n] [comm_ring R] [topological_ring R] {A : X → matrix n n R} (hA : continuous A) : continuous (λ x, (A x).det) := begin simp_rw matrix.det_apply, refine continuous_finset_sum _ (λ l _, continuous.const_smul _ _), refine continuous_finset_prod _ (λ l _, hA.matrix_elem _ _), end @[continuity] lemma continuous.matrix_update_column [decidable_eq n] (i : n) {A : X → matrix m n R} {B : X → m → R} (hA : continuous A) (hB : continuous B) : continuous (λ x, (A x).update_column i (B x)) := continuous_matrix $ λ j k, (continuous_apply k).comp $ ((continuous_apply _).comp hA).update i ((continuous_apply _).comp hB) @[continuity] lemma continuous.matrix_update_row [decidable_eq m] (i : m) {A : X → matrix m n R} {B : X → n → R} (hA : continuous A) (hB : continuous B) : continuous (λ x, (A x).update_row i (B x)) := hA.update i hB @[continuity] lemma continuous.matrix_cramer [fintype n] [decidable_eq n] [comm_ring R] [topological_ring R] {A : X → matrix n n R} {B : X → n → R} (hA : continuous A) (hB : continuous B) : continuous (λ x, (A x).cramer (B x)) := continuous_pi $ λ i, (hA.matrix_update_column _ hB).matrix_det @[continuity] lemma continuous.matrix_adjugate [fintype n] [decidable_eq n] [comm_ring R] [topological_ring R] {A : X → matrix n n R} (hA : continuous A) : continuous (λ x, (A x).adjugate) := continuous_matrix $ λ j k, (hA.matrix_transpose.matrix_update_column k continuous_const).matrix_det /-- When `ring.inverse` is continuous at the determinant (such as in a `normed_ring`, or a `topological_field`), so is `matrix.has_inv`. -/ lemma continuous_at_matrix_inv [fintype n] [decidable_eq n] [comm_ring R] [topological_ring R] (A : matrix n n R) (h : continuous_at ring.inverse A.det) : continuous_at has_inv.inv A := (h.comp continuous_id.matrix_det.continuous_at).smul continuous_id.matrix_adjugate.continuous_at -- lemmas about functions in `data/matrix/block.lean` section block_matrices @[continuity] lemma continuous.matrix_from_blocks {A : X → matrix n l R} {B : X → matrix n m R} {C : X → matrix p l R} {D : X → matrix p m R} (hA : continuous A) (hB : continuous B) (hC : continuous C) (hD : continuous D) : continuous (λ x, matrix.from_blocks (A x) (B x) (C x) (D x)) := continuous_matrix $ λ i j, by cases i; cases j; refine continuous.matrix_elem _ i j; assumption @[continuity] lemma continuous.matrix_block_diagonal [has_zero R] [decidable_eq p] {A : X → p → matrix m n R} (hA : continuous A) : continuous (λ x, block_diagonal (A x)) := continuous_matrix $ λ ⟨i₁, i₂⟩ ⟨j₁, j₂⟩, (((continuous_apply i₂).comp hA).matrix_elem i₁ j₁).if_const _ continuous_zero @[continuity] lemma continuous.matrix_block_diag {A : X → matrix (m × p) (n × p) R} (hA : continuous A) : continuous (λ x, block_diag (A x)) := continuous_pi $ λ i, continuous_matrix $ λ j k, hA.matrix_elem _ _ @[continuity] lemma continuous.matrix_block_diagonal' [has_zero R] [decidable_eq l] {A : X → Π i, matrix (m' i) (n' i) R} (hA : continuous A) : continuous (λ x, block_diagonal' (A x)) := continuous_matrix $ λ ⟨i₁, i₂⟩ ⟨j₁, j₂⟩, begin dsimp only [block_diagonal'], split_ifs, { subst h, exact ((continuous_apply i₁).comp hA).matrix_elem i₂ j₂ }, { exact continuous_const }, end @[continuity] lemma continuous.matrix_block_diag' {A : X → matrix (Σ i, m' i) (Σ i, n' i) R} (hA : continuous A) : continuous (λ x, block_diag' (A x)) := continuous_pi $ λ i, continuous_matrix $ λ j k, hA.matrix_elem _ _ end block_matrices end continuity /-! ### Lemmas about infinite sums -/ section tsum variables [semiring α] [add_comm_monoid R] [topological_space R] [module α R] lemma has_sum.matrix_transpose {f : X → matrix m n R} {a : matrix m n R} (hf : has_sum f a) : has_sum (λ x, (f x)ᵀ) aᵀ := (hf.map (@matrix.transpose_add_equiv m n R _) continuous_id.matrix_transpose : _) lemma summable.matrix_transpose {f : X → matrix m n R} (hf : summable f) : summable (λ x, (f x)ᵀ) := hf.has_sum.matrix_transpose.summable @[simp] lemma summable_matrix_transpose {f : X → matrix m n R} : summable (λ x, (f x)ᵀ) ↔ summable f := (summable.map_iff_of_equiv (@matrix.transpose_add_equiv m n R _) (@continuous_id (matrix m n R) _).matrix_transpose (continuous_id.matrix_transpose) : _) lemma matrix.transpose_tsum [t2_space R] {f : X → matrix m n R} : (∑' x, f x)ᵀ = ∑' x, (f x)ᵀ := begin by_cases hf : summable f, { exact hf.has_sum.matrix_transpose.tsum_eq.symm }, { have hft := summable_matrix_transpose.not.mpr hf, rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft, transpose_zero] }, end lemma has_sum.matrix_conj_transpose [star_add_monoid R] [has_continuous_star R] {f : X → matrix m n R} {a : matrix m n R} (hf : has_sum f a) : has_sum (λ x, (f x)ᴴ) aᴴ := (hf.map (@matrix.conj_transpose_add_equiv m n R _ _) continuous_id.matrix_conj_transpose : _) lemma summable.matrix_conj_transpose [star_add_monoid R] [has_continuous_star R] {f : X → matrix m n R} (hf : summable f) : summable (λ x, (f x)ᴴ) := hf.has_sum.matrix_conj_transpose.summable @[simp] lemma summable_matrix_conj_transpose [star_add_monoid R] [has_continuous_star R] {f : X → matrix m n R} : summable (λ x, (f x)ᴴ) ↔ summable f := (summable.map_iff_of_equiv (@matrix.conj_transpose_add_equiv m n R _ _) (@continuous_id (matrix m n R) _).matrix_conj_transpose (continuous_id.matrix_conj_transpose) : _) lemma matrix.conj_transpose_tsum [star_add_monoid R] [has_continuous_star R] [t2_space R] {f : X → matrix m n R} : (∑' x, f x)ᴴ = ∑' x, (f x)ᴴ := begin by_cases hf : summable f, { exact hf.has_sum.matrix_conj_transpose.tsum_eq.symm }, { have hft := summable_matrix_conj_transpose.not.mpr hf, rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft, conj_transpose_zero] }, end lemma has_sum.matrix_diagonal [decidable_eq n] {f : X → n → R} {a : n → R} (hf : has_sum f a) : has_sum (λ x, diagonal (f x)) (diagonal a) := (hf.map (diagonal_add_monoid_hom n R) $ continuous.matrix_diagonal $ by exact continuous_id : _) lemma summable.matrix_diagonal [decidable_eq n] {f : X → n → R} (hf : summable f) : summable (λ x, diagonal (f x)) := hf.has_sum.matrix_diagonal.summable @[simp] lemma summable_matrix_diagonal [decidable_eq n] {f : X → n → R} : summable (λ x, diagonal (f x)) ↔ summable f := (summable.map_iff_of_left_inverse (@matrix.diagonal_add_monoid_hom n R _ _) (matrix.diag_add_monoid_hom n R) (by exact continuous.matrix_diagonal continuous_id) continuous_matrix_diag (λ A, diag_diagonal A) : _) lemma matrix.diagonal_tsum [decidable_eq n] [t2_space R] {f : X → n → R} : diagonal (∑' x, f x) = ∑' x, diagonal (f x) := begin by_cases hf : summable f, { exact hf.has_sum.matrix_diagonal.tsum_eq.symm }, { have hft := summable_matrix_diagonal.not.mpr hf, rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft], exact diagonal_zero }, end lemma has_sum.matrix_diag {f : X → matrix n n R} {a : matrix n n R} (hf : has_sum f a) : has_sum (λ x, diag (f x)) (diag a) := (hf.map (diag_add_monoid_hom n R) continuous_matrix_diag : _) lemma summable.matrix_diag {f : X → matrix n n R} (hf : summable f) : summable (λ x, diag (f x)) := hf.has_sum.matrix_diag.summable section block_matrices lemma has_sum.matrix_block_diagonal [decidable_eq p] {f : X → p → matrix m n R} {a : p → matrix m n R} (hf : has_sum f a) : has_sum (λ x, block_diagonal (f x)) (block_diagonal a) := (hf.map (block_diagonal_add_monoid_hom m n p R) $ continuous.matrix_block_diagonal $ by exact continuous_id : _) lemma summable.matrix_block_diagonal [decidable_eq p] {f : X → p → matrix m n R} (hf : summable f) : summable (λ x, block_diagonal (f x)) := hf.has_sum.matrix_block_diagonal.summable lemma summable_matrix_block_diagonal [decidable_eq p] {f : X → p → matrix m n R} : summable (λ x, block_diagonal (f x)) ↔ summable f := (summable.map_iff_of_left_inverse (matrix.block_diagonal_add_monoid_hom m n p R) (matrix.block_diag_add_monoid_hom m n p R) (by exact continuous.matrix_block_diagonal continuous_id) (by exact continuous.matrix_block_diag continuous_id) (λ A, block_diag_block_diagonal A) : _) lemma matrix.block_diagonal_tsum [decidable_eq p] [t2_space R] {f : X → p → matrix m n R} : block_diagonal (∑' x, f x) = ∑' x, block_diagonal (f x) := begin by_cases hf : summable f, { exact hf.has_sum.matrix_block_diagonal.tsum_eq.symm }, { have hft := summable_matrix_block_diagonal.not.mpr hf, rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft], exact block_diagonal_zero }, end lemma has_sum.matrix_block_diag {f : X → matrix (m × p) (n × p) R} {a : matrix (m × p) (n × p) R} (hf : has_sum f a) : has_sum (λ x, block_diag (f x)) (block_diag a) := (hf.map (block_diag_add_monoid_hom m n p R) $ continuous.matrix_block_diag continuous_id : _) lemma summable.matrix_block_diag {f : X → matrix (m × p) (n × p) R} (hf : summable f) : summable (λ x, block_diag (f x)) := hf.has_sum.matrix_block_diag.summable lemma has_sum.matrix_block_diagonal' [decidable_eq l] {f : X → Π i, matrix (m' i) (n' i) R} {a : Π i, matrix (m' i) (n' i) R} (hf : has_sum f a) : has_sum (λ x, block_diagonal' (f x)) (block_diagonal' a) := (hf.map (block_diagonal'_add_monoid_hom m' n' R) $ continuous.matrix_block_diagonal' $ by exact continuous_id : _) lemma summable.matrix_block_diagonal' [decidable_eq l] {f : X → Π i, matrix (m' i) (n' i) R} (hf : summable f) : summable (λ x, block_diagonal' (f x)) := hf.has_sum.matrix_block_diagonal'.summable lemma summable_matrix_block_diagonal' [decidable_eq l] {f : X → Π i, matrix (m' i) (n' i) R} : summable (λ x, block_diagonal' (f x)) ↔ summable f := (summable.map_iff_of_left_inverse (matrix.block_diagonal'_add_monoid_hom m' n' R) (matrix.block_diag'_add_monoid_hom m' n' R) (by exact continuous.matrix_block_diagonal' continuous_id) (by exact continuous.matrix_block_diag' continuous_id) (λ A, block_diag'_block_diagonal' A) : _) lemma matrix.block_diagonal'_tsum [decidable_eq l] [t2_space R] {f : X → Π i, matrix (m' i) (n' i) R} : block_diagonal' (∑' x, f x) = ∑' x, block_diagonal' (f x) := begin by_cases hf : summable f, { exact hf.has_sum.matrix_block_diagonal'.tsum_eq.symm }, { have hft := summable_matrix_block_diagonal'.not.mpr hf, rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hft], exact block_diagonal'_zero }, end lemma has_sum.matrix_block_diag' {f : X → matrix (Σ i, m' i) (Σ i, n' i) R} {a : matrix (Σ i, m' i) (Σ i, n' i) R} (hf : has_sum f a) : has_sum (λ x, block_diag' (f x)) (block_diag' a) := (hf.map (block_diag'_add_monoid_hom m' n' R) $ continuous.matrix_block_diag' continuous_id : _) lemma summable.matrix_block_diag' {f : X → matrix (Σ i, m' i) (Σ i, n' i) R} (hf : summable f) : summable (λ x, block_diag' (f x)) := hf.has_sum.matrix_block_diag'.summable end block_matrices end tsum
83af2e3baa3abc3e390d6a09396384249ba87117
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/polynomial/integral_normalization.lean
5199de1781956d85be0853b667daa23d026a0d7d
[ "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
5,305
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.algebra_map import data.polynomial.monic /-! # Theory of monic polynomials We define `integral_normalization`, which relate arbitrary polynomials to monic ones. -/ noncomputable theory open finsupp namespace polynomial universes u v y variables {R : Type u} {S : Type v} {a b : R} {m n : ℕ} {ι : Type y} section integral_normalization section semiring variables [semiring R] /-- If `f : polynomial R` is a nonzero polynomial with root `z`, `integral_normalization f` is a monic polynomial with root `leading_coeff f * z`. Moreover, `integral_normalization 0 = 0`. -/ noncomputable def integral_normalization (f : polynomial R) : polynomial R := on_finset f.support (λ i, if f.degree = i then 1 else coeff f i * f.leading_coeff ^ (f.nat_degree - 1 - i)) begin intros i h, apply mem_support_iff.mpr, split_ifs at h with hi, { exact coeff_ne_zero_of_eq_degree hi }, { exact left_ne_zero_of_mul h }, end lemma integral_normalization_coeff_degree {f : polynomial R} {i : ℕ} (hi : f.degree = i) : (integral_normalization f).coeff i = 1 := if_pos hi lemma integral_normalization_coeff_nat_degree {f : polynomial R} (hf : f ≠ 0) : (integral_normalization f).coeff (nat_degree f) = 1 := integral_normalization_coeff_degree (degree_eq_nat_degree hf) lemma integral_normalization_coeff_ne_degree {f : polynomial R} {i : ℕ} (hi : f.degree ≠ i) : coeff (integral_normalization f) i = coeff f i * f.leading_coeff ^ (f.nat_degree - 1 - i) := if_neg hi lemma integral_normalization_coeff_ne_nat_degree {f : polynomial R} {i : ℕ} (hi : i ≠ nat_degree f) : coeff (integral_normalization f) i = coeff f i * f.leading_coeff ^ (f.nat_degree - 1 - i) := integral_normalization_coeff_ne_degree (degree_ne_of_nat_degree_ne hi.symm) lemma monic_integral_normalization {f : polynomial R} (hf : f ≠ 0) : monic (integral_normalization f) := begin apply monic_of_degree_le f.nat_degree, { refine finset.sup_le (λ i h, _), rw [integral_normalization, mem_support_iff, on_finset_apply] at h, split_ifs at h with hi, { exact le_trans (le_of_eq hi.symm) degree_le_nat_degree }, { erw [with_bot.some_le_some], apply le_nat_degree_of_ne_zero, exact left_ne_zero_of_mul h } }, { exact integral_normalization_coeff_nat_degree hf } end end semiring section domain variables [integral_domain R] @[simp] lemma support_integral_normalization {f : polynomial R} (hf : f ≠ 0) : (integral_normalization f).support = f.support := begin ext i, simp only [integral_normalization, on_finset_apply, mem_support_iff], split_ifs with hi, { simp only [ne.def, not_false_iff, true_iff, one_ne_zero, hi], exact coeff_ne_zero_of_eq_degree hi }, split, { intro h, exact left_ne_zero_of_mul h }, { intro h, refine mul_ne_zero h (pow_ne_zero _ _), exact λ h, hf (leading_coeff_eq_zero.mp h) } end variables [comm_ring S] lemma integral_normalization_eval₂_eq_zero {p : polynomial R} (hp : p ≠ 0) (f : R →+* S) {z : S} (hz : eval₂ f z p = 0) (inj : ∀ (x : R), f x = 0 → x = 0) : eval₂ f (z * f p.leading_coeff) (integral_normalization p) = 0 := calc eval₂ f (z * f p.leading_coeff) (integral_normalization p) = p.support.attach.sum (λ i, f (coeff (integral_normalization p) i.1 * p.leading_coeff ^ i.1) * z ^ i.1) : by { rw [eval₂, finsupp.sum, support_integral_normalization hp], simp only [mul_comm z, mul_pow, mul_assoc, ring_hom.map_pow, ring_hom.map_mul], exact finset.sum_attach.symm } ... = p.support.attach.sum (λ i, f (coeff p i.1 * p.leading_coeff ^ (nat_degree p - 1)) * z ^ i.1) : begin have one_le_deg : 1 ≤ nat_degree p := nat.succ_le_of_lt (nat_degree_pos_of_eval₂_root hp f hz inj), congr' with i, congr' 2, by_cases hi : i.1 = nat_degree p, { rw [hi, integral_normalization_coeff_degree, one_mul, leading_coeff, ←pow_succ, nat.sub_add_cancel one_le_deg], exact degree_eq_nat_degree hp }, { have : i.1 ≤ p.nat_degree - 1 := nat.le_pred_of_lt (lt_of_le_of_ne (le_nat_degree_of_ne_zero (finsupp.mem_support_iff.mp i.2)) hi), rw [integral_normalization_coeff_ne_nat_degree hi, mul_assoc, ←pow_add, nat.sub_add_cancel this] } end ... = f p.leading_coeff ^ (nat_degree p - 1) * eval₂ f z p : by { simp_rw [eval₂, finsupp.sum, λ i, mul_comm (coeff p i), ring_hom.map_mul, ring_hom.map_pow, mul_assoc, ←finset.mul_sum], congr' 1, exact @finset.sum_attach _ _ p.support _ (λ i, f (p.coeff i) * z ^ i) } ... = 0 : by rw [hz, _root_.mul_zero] lemma integral_normalization_aeval_eq_zero [algebra R S] {f : polynomial R} (hf : f ≠ 0) {z : S} (hz : aeval z f = 0) (inj : ∀ (x : R), algebra_map R S x = 0 → x = 0) : aeval (z * algebra_map R S f.leading_coeff) (integral_normalization f) = 0 := integral_normalization_eval₂_eq_zero hf (algebra_map R S) hz inj end domain end integral_normalization end polynomial
4be4ab71272ebcd8cc081a604f328b85b093c9e2
cc5bb0a18c45fa529fc8bf0365b1fbf48464c5a8
/library/init/data/nat/lemmas.lean
5d66a047928359a04883c7a3be72198b8c3610c0
[ "Apache-2.0" ]
permissive
mk12/lean
6bb58b9f3cfe312236b41779478d345399f00918
092fc89333160661c4c5a6295f3054cafff4341e
refs/heads/master
1,609,531,450,083
1,501,275,267,000
1,501,278,025,000
98,760,144
0
0
null
1,501,364,485,000
1,501,364,485,000
null
UTF-8
Lean
false
false
50,768
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.data.nat.pow init.meta init.algebra.functions universes u namespace nat attribute [pre_smt] nat_zero_eq_zero protected lemma zero_add : ∀ n : ℕ, 0 + n = n | 0 := rfl | (n+1) := congr_arg succ (zero_add n) lemma succ_add : ∀ n m : ℕ, (succ n) + m = succ (n + m) | n 0 := rfl | n (m+1) := congr_arg succ (succ_add n m) lemma add_succ (n m : ℕ) : n + succ m = succ (n + m) := rfl protected lemma add_zero (n : ℕ) : n + 0 = n := rfl lemma add_one (n : ℕ) : n + 1 = succ n := rfl lemma succ_eq_add_one (n : ℕ) : succ n = n + 1 := rfl 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; injection h, 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 instance : zero_ne_one_class ℕ := { zero := 0, one := 1, zero_ne_one := nat.zero_ne_one } 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] instance : comm_semiring nat := {add := nat.add, add_assoc := nat.add_assoc, zero := nat.zero, zero_add := nat.zero_add, add_zero := nat.add_zero, add_comm := nat.add_comm, mul := nat.mul, mul_assoc := nat.mul_assoc, one := nat.succ nat.zero, one_mul := nat.one_mul, mul_one := nat.mul_one, left_distrib := nat.left_distrib, right_distrib := nat.right_distrib, zero_mul := nat.zero_mul, mul_zero := nat.mul_zero, mul_comm := nat.mul_comm} /- properties of inequality -/ protected lemma le_of_eq {n m : ℕ} (p : n = m) : n ≤ m := p ▸ less_than_or_equal.refl n 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 def lt.step {n m : ℕ} : n < m → n < succ m := less_than_or_equal.step lemma eq_zero_or_pos (n : ℕ) : n = 0 ∨ n > 0 := by {cases n, exact or.inl rfl, exact or.inr (succ_pos _)} protected lemma pos_of_ne_zero {n : nat} : n ≠ 0 → n > 0 := 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₁) def 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)) instance : weak_order ℕ := ⟨@nat.less_than_or_equal, @nat.le_refl, @nat.le_trans, @nat.le_antisymm⟩ lemma eq_zero_of_le_zero {n : nat} (h : n ≤ 0) : n = 0 := le_antisymm h (zero_le _) protected lemma le_of_eq_or_lt {a b : ℕ} (h : a = b ∨ a < b) : a ≤ b := or.elim h nat.le_of_eq nat.le_of_lt 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 → m ≠ 0 → n < m → pred n < pred m | 0 _ h₁ h₂ h := absurd rfl h₁ | _ 0 h₁ h₂ h := absurd rfl h₂ | (succ n) (succ m) _ _ h := lt_of_succ_lt_succ h protected lemma lt_or_ge : ∀ (a b : ℕ), a < b ∨ a ≥ b | 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 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 ._ m 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 dsimp at hw, 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 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 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 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 : k > 0) : n < n + k := nat.add_lt_add_left h n protected lemma lt_add_of_pos_left {n k : ℕ} (h : k > 0) : n < k + n := by rw add_comm; exact nat.lt_add_of_pos_right h protected lemma zero_lt_one : 0 < (1:nat) := zero_lt_succ 0 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 le_of_lt_or_eq {m n : ℕ} (h : m < n ∨ m = n) : m ≤ n := nat.le_of_eq_or_lt (or.swap h) protected lemma lt_or_eq_of_le {m n : ℕ} (h : m ≤ n) : m < n ∨ m = n := or.swap (nat.eq_or_lt_of_le h) protected lemma le_iff_lt_or_eq (m n : ℕ) : m ≤ n ↔ m < n ∨ m = n := iff.intro nat.lt_or_eq_of_le nat.le_of_lt_or_eq 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 [← left_distrib, hl], le.intro this end lemma mul_le_mul_right {n m : ℕ} (k : ℕ) (h : n ≤ m) : n * k ≤ m * k := mul_comm k m ▸ 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 : k > 0) : 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 : k > 0) : n * k < m * k := mul_comm k m ▸ mul_comm k n ▸ nat.mul_lt_mul_of_pos_left h hk instance : decidable_linear_ordered_semiring nat := { nat.comm_semiring with add_left_cancel := @nat.add_left_cancel, add_right_cancel := @nat.add_right_cancel, lt := nat.lt, le := nat.le, le_refl := nat.le_refl, le_trans := @nat.le_trans, le_antisymm := @nat.le_antisymm, le_total := @nat.le_total, le_iff_lt_or_eq := @nat.le_iff_lt_or_eq, le_of_lt := @nat.le_of_lt, lt_irrefl := @nat.lt_irrefl, lt_of_lt_of_le := @nat.lt_of_lt_of_le, lt_of_le_of_lt := @nat.lt_of_le_of_lt, lt_of_add_lt_add_left := @nat.lt_of_add_lt_add_left, add_lt_add_left := @nat.add_lt_add_left, add_le_add_left := @nat.add_le_add_left, le_of_add_le_add_left := @nat.le_of_add_le_add_left, zero_lt_one := zero_lt_succ 0, mul_le_mul_of_nonneg_left := assume a b c h₁ h₂, nat.mul_le_mul_left c h₁, mul_le_mul_of_nonneg_right := assume a b c h₁ h₂, nat.mul_le_mul_right c h₁, mul_lt_mul_of_pos_left := @nat.mul_lt_mul_of_pos_left, mul_lt_mul_of_pos_right := @nat.mul_lt_mul_of_pos_right, decidable_lt := nat.decidable_lt, decidable_le := nat.decidable_le, decidable_eq := nat.decidable_eq } -- all the fields are already included in the decidable_linear_ordered_semiring instance instance : decidable_linear_ordered_cancel_comm_monoid ℕ := { nat.decidable_linear_ordered_semiring with add_left_cancel := @nat.add_left_cancel } 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 : n > 0) (H : n * m = n * k) : m = k := le_antisymm (le_of_mul_le_mul_left (le_of_eq H) Hn) (le_of_mul_le_mul_left (le_of_eq H.symm) Hn) theorem eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : m > 0) (H : n * m = k * m) : n = k := by rw [mul_comm n m, mul_comm k m] at H; exact eq_of_mul_eq_mul_left Hm H /- 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 bit0_succ_eq (n : ℕ) : bit0 (succ n) = succ (succ (bit0 n)) := show succ (succ n + n) = succ (succ (n + n)), from congr_arg succ (succ_add n n) 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 bit0_ne_zero : ∀ {n : ℕ}, n ≠ 0 → bit0 n ≠ 0 | 0 h := absurd rfl h | (n+1) h := succ_ne_zero _ protected lemma bit1_ne_zero (n : ℕ) : bit1 n ≠ 0 := show succ (n + n) ≠ 0, from succ_ne_zero (n + 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)), begin unfold bit0 at h, simp [add_one, add_succ, succ_add] at h, exact h end, have n + n = m + m, by repeat {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, assumption 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 zero_lt_bit1 (n : nat) : 0 < bit1 n := zero_lt_succ _ protected lemma zero_lt_bit0 : ∀ {n : nat}, n ≠ 0 → 0 < bit0 n | 0 h := by contradiction | (succ n) h := begin rw nat.bit0_succ_eq, apply zero_lt_succ end 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 := add_lt_add h h protected lemma bit1_lt {n m : nat} (h : n < m) : bit1 n < bit1 m := succ_lt_succ (add_lt_add h h) protected lemma bit0_lt_bit1 {n m : nat} (h : n ≤ m) : bit0 n < bit1 m := lt_succ_of_le (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 (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))) /- Extra instances to short-circuit type class resolution -/ instance : add_comm_monoid nat := by apply_instance instance : add_monoid nat := by apply_instance instance : monoid nat := by apply_instance instance : comm_monoid nat := by apply_instance instance : comm_semigroup nat := by apply_instance instance : semigroup nat := by apply_instance instance : add_comm_semigroup nat := by apply_instance instance : add_semigroup nat := by apply_instance instance : distrib nat := by apply_instance instance : semiring nat := by apply_instance instance : ordered_semiring nat := by apply_instance /- 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 [add_zero, 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 [add_comm k n, 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 [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 [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 ih_1 _ 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 : ℕ}, n > 0 → 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 := 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 : n ≥ m) : n - m + m = n := by rw [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, add_comm k, ← 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 := lt_of_not_ge (assume (H' : n ≥ m), begin simp [nat.sub_eq_zero_of_le H'] at H, contradiction end) @[simp] lemma zero_min (a : ℕ) : min 0 a = 0 := min_eq_left (zero_le a) @[simp] 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 (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 : a ≥ b) : 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 : y > 0) : x % y < y := begin induction x using nat.case_strong_induction_on with x ih, {rw zero_mod, assumption}, {apply or.elim (decidable.em (succ x < y)), {intro h₁, rwa [mod_eq_of_lt h₁]}, {intro h₁, have h₁ : succ x % y = (succ x - y) % y, {exact mod_eq_sub_mod (le_of_not_gt h₁)}, have this : succ x - y ≤ x, {exact le_of_lt_succ (sub_lt (succ_pos x) h)}, have h₂ : (succ x - y) % y < y, {exact 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 [left_distrib, IH _ h'], rw [← nat.add_sub_assoc h.right, nat.add_sub_cancel_left] }, -- ¬ (0 < k ∧ k ≤ m) { rw [div_def, mod_def, if_neg h', if_neg h'], simp }, end /- div -/ @[simp] protected lemma div_one (n : ℕ) : n / 1 = n := have n % 1 + 1 * (n / 1) = n, from mod_add_div _ _, by simp [mod_one] at this; assumption @[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 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 simp ... ≤ 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₁ : b > 0) (h₂ : a ≥ b) : 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 : k > 0) : 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_ge y k with h h, -- base case: y < k { rw [div_eq_of_lt h], cases x with x, { simp [zero_mul, zero_le] }, { simp [succ_mul, not_succ_le_zero], apply not_le_of_gt, 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 [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 : k > 0) : x / k < y ↔ x < y * k := begin simp [lt_iff_not_ge], apply not_iff_not_of_iff, apply le_div_iff_mul_le _ _ Hk end def iterate {A : Type} (op : A → A) : ℕ → A → A | 0 := λ a, a | (succ k) := λ a, op (iterate k 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⟩ theorem succ_inj {n m : ℕ} (H : succ n = succ m) : n = m := nat.succ.inj_arrow H id theorem discriminate {B : Sort u} {n : ℕ} (H1: n = 0 → B) (H2 : ∀m, n = succ m → B) : B := by ginduction n with h; [exact H1 h, exact H2 _ h] theorem one_succ_zero : 1 = succ 0 := rfl theorem 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 _) theorem 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 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 nat.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₂ : a ≥ b → 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 def one_pos := nat.zero_lt_one theorem mul_self_le_mul_self {n m : ℕ} (h : n ≤ m) : n * n ≤ m * m := mul_le_mul h h (zero_le _) (zero_le _) theorem mul_self_lt_mul_self : Π {n m : ℕ}, n < m → n * n < m * m | 0 m h := mul_pos h h | (succ n) m h := mul_lt_mul h (le_of_lt h) (succ_pos _) (zero_le _) theorem mul_self_le_mul_self_iff {n m : ℕ} : n ≤ m ↔ n * n ≤ m * m := ⟨mul_self_le_mul_self, λh, decidable.by_contradiction $ λhn, not_lt_of_ge h $ mul_self_lt_mul_self $ lt_of_not_ge hn⟩ theorem mul_self_lt_mul_self_iff {n m : ℕ} : n < m ↔ n * n < m * m := iff.trans (lt_iff_not_ge _ _) $ iff.trans (not_iff_not_of_iff mul_self_le_mul_self_iff) $ iff.symm (lt_iff_not_ge _ _) theorem le_mul_self : Π (n : ℕ), n ≤ n * n | 0 := le_refl _ | (n+1) := let t := mul_le_mul_left (n+1) (succ_pos n) in by simp at t; exact t /- 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 _) ih_1] 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, add_comm] theorem mul_pred_left : ∀ (n m : ℕ), pred n * m = n * m - m | 0 m := by simp [nat.zero_sub, pred_zero, 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 [mul_comm, mul_pred_left, 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] | 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 [mul_comm, nat.mul_sub_right_distrib, mul_comm m n, 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, right_distrib, right_distrib, mul_comm b a, 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 [right_distrib, left_distrib] end theorem succ_sub {m n : ℕ} (h : m ≥ n) : 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) : n - m > 0 := have 0 + m < n - m + m, begin rw [zero_add, nat.sub_add_cancel (le_of_lt h)], exact h end, 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 add_comm, apply nat.zero_lt_succ end theorem pred_inj : ∀ {a b : nat}, a > 0 → b > 0 → 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 (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 _)) 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_gt (λ l, find_min l h) end find /- mod -/ theorem mod_le (x y : ℕ) : x % y ≤ x := or.elim (lt_or_ge 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 [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, simp, rw[mul_succ, ← add_assoc, add_mod_right, ih]} @[simp] theorem add_mul_mod_self_right (x y z : ℕ) : (x + y * z) % z = x % z := by rw [mul_comm, add_mul_mod_self_left] @[simp] theorem mul_mod_right (m n : ℕ) : (m * n) % m = 0 := by rw [← zero_add (m*n), add_mul_mod_self_left, zero_mod] @[simp] theorem mul_mod_left (m n : ℕ) : (m * n) % n = 0 := by rw [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, mul_zero, mod_zero, mod_zero] else if z0 : z = 0 then by rw [z0, zero_mul, zero_mul, 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_gt 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 (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 [mul_comm x z, mul_comm y z, 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 cases d with h h ; unfold decidable.to_bool cond, { cases mod_two_eq_zero_or_one x with h' h', rw h', cases h h' }, { rw h }, end theorem sub_mul_mod (x k n : ℕ) (h₁ : n*k ≤ x) : (x - n*k) % n = x % n := begin induction k with k, { simp }, { 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] at h₁, simp [h₁] }, rw [mul_succ, ← nat.sub_sub, ← mod_eq_sub_mod h₄, ih_1 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, { simp }, { have h₂ : n*p ≤ x, { transitivity, { apply nat.mul_le_mul_left, apply le_succ }, { apply h₁ } }, have h₃ : x - n * p ≥ n, { apply le_of_add_le_add_right, rw [nat.sub_add_cancel h₂, add_comm], rw [mul_succ] at h₁, apply h₁ }, rw [sub_succ, ← ih_1 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 : z > 0) : (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 : z > 0) : (z + x) / z = succ (x / z) := by rw [add_comm, add_div_right x H] @[simp] theorem mul_div_right (n : ℕ) {m : ℕ} (H : m > 0) : m * n / m = n := by {induction n; simp [*, mul_succ, -mul_comm]} @[simp] theorem mul_div_left (m : ℕ) {n : ℕ} (H : n > 0) : m * n / n = m := by rw [mul_comm, mul_div_right _ H] protected theorem div_self {n : ℕ} (H : n > 0) : n / n = 1 := let t := add_div_right 0 H in by rwa [zero_add, nat.zero_div] at t theorem add_mul_div_left (x z : ℕ) {y : ℕ} (H : y > 0) : (x + y * z) / y = x / y + z := by {induction z with z ih, simp, rw [mul_succ, ← add_assoc, add_div_right _ H, ih]} theorem add_mul_div_right (x y : ℕ) {z : ℕ} (H : z > 0) : (x + y * z) / z = x / z + y := by rw [mul_comm, add_mul_div_left _ _ H] protected theorem mul_div_cancel (m : ℕ) {n : ℕ} (H : n > 0) : m * n / n = m := let t := add_mul_div_right 0 m H in by rwa [zero_add, nat.zero_div, zero_add] at t protected theorem mul_div_cancel_left (m : ℕ) {n : ℕ} (H : n > 0) : n * m / n = m := by rw [mul_comm, nat.mul_div_cancel _ H] protected theorem div_eq_of_eq_mul_left {m n k : ℕ} (H1 : n > 0) (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 : n > 0) (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 : n > 0, from (eq_zero_or_pos _).resolve_left $ λ hn, by rw [hn, 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 : n > 0 := (eq_zero_or_pos _).resolve_left (λ n0, by rw [n0, zero_mul] at h₁; exact not_lt_zero _ h₁), apply nat.div_eq_of_lt_le, { rw [nat.mul_sub_right_distrib, 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, mul_comm], apply nat.sub_le_sub_left, apply div_mul_le_self }, { apply (div_lt_iff_lt_mul _ _ npos).2, rwa mul_comm } } end 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, mul_zero, nat.div_zero, nat.div_zero]}, cases eq_zero_or_pos n with n0 npos, {rw [n0, zero_mul, nat.div_zero, nat.zero_div]}, apply le_antisymm, { apply (le_div_iff_mul_le _ _ (mul_pos npos kpos)).2, rw [mul_comm n k, ← 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 [mul_assoc, mul_comm n k], apply (le_div_iff_mul_le _ _ (mul_pos kpos npos)).1, refl } end protected theorem mul_div_mul {m : ℕ} (n k : ℕ) (H : m > 0) : m * n / (m * k) = n / k := by rw [← nat.div_div_eq_div_mul, nat.mul_div_cancel_left _ H] /- dvd -/ protected theorem dvd_add_iff_right {k m n : ℕ} (h : k ∣ m) : k ∣ n ↔ k ∣ m + n := ⟨dvd_add h, dvd.elim h $ λd hd, match m, hd with | ._, rfl := λh₂, dvd.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 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) _ (dvd_trans h (dvd_mul_right n (m / n))) in by rwa mod_add_div at t theorem le_of_dvd {m n : ℕ} (h : n > 0) : 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 mul_one at t } theorem dvd_antisymm : Π {m n : ℕ}, m ∣ n → n ∣ m → m = n | m 0 h₁ h₂ := eq_zero_of_zero_dvd h₂ | 0 n h₁ h₂ := (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 : n > 0) : m > 0 := nat.pos_of_ne_zero $ λm0, by rw m0 at H1; rw 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 := dvd.intro (n / m) $ let t := mod_add_div n m in by simp [H] at t; exact t theorem mod_eq_zero_of_dvd {m n : ℕ} (H : m ∣ n) : n % m = 0 := dvd.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, zero_add] at t protected theorem div_mul_cancel {m n : ℕ} (H : n ∣ m) : m / n * n = m := by rw [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, mul_zero]) (λh, have m * n / k = m * (n / k * k) / k, by rw nat.div_mul_cancel H, by rw[this, ← mul_assoc, nat.mul_div_cancel _ h]) theorem dvd_of_mul_dvd_mul_left {m n k : ℕ} (kpos : k > 0) (H : k * m ∣ k * n) : m ∣ n := dvd.elim H (λl H1, by rw mul_assoc at H1; exact ⟨_, eq_of_mul_eq_mul_left kpos H1⟩) theorem dvd_of_mul_dvd_mul_right {m n k : ℕ} (kpos : k > 0) (H : m * k ∣ n * k) : m ∣ n := by rw [mul_comm m k, mul_comm n k] at H; exact dvd_of_mul_dvd_mul_left kpos H /- pow -/ @[simp] theorem pow_one (b : ℕ) : b^1 = b := by simp [pow_succ] theorem pow_le_pow_of_le_left {x y : ℕ} (H : x ≤ y) : ∀ i, x^i ≤ y^i | 0 := le_refl _ | (succ i) := mul_le_mul (pow_le_pow_of_le_left i) H (zero_le _) (zero_le _) theorem pow_le_pow_of_le_right {x : ℕ} (H : x > 0) {i} : ∀ {j}, i ≤ j → x^i ≤ x^j | 0 h := by rw eq_zero_of_le_zero h; apply le_refl | (succ j) h := (lt_or_eq_of_le h).elim (λhl, by rw [pow_succ, ← mul_one (x^i)]; exact mul_le_mul (pow_le_pow_of_le_right $ le_of_lt_succ hl) H (zero_le _) (zero_le _)) (λe, by rw e; refl) theorem pos_pow_of_pos {b : ℕ} (n : ℕ) (h : 0 < b) : 0 < b^n := pow_le_pow_of_le_right h (zero_le _) theorem zero_pow {n : ℕ} (h : 0 < n) : 0^n = 0 := by rw [← succ_pred_eq_of_pos h, pow_succ, mul_zero] theorem pow_lt_pow_of_lt_left {x y : ℕ} (H : x < y) {i} (h : i > 0) : x^i < y^i := begin cases i with i, { exact absurd h (not_lt_zero _) }, rw [pow_succ, pow_succ], exact mul_lt_mul' (pow_le_pow_of_le_left (le_of_lt H) _) H (zero_le _) (pos_pow_of_pos _ $ lt_of_le_of_lt (zero_le _) H) end theorem pow_lt_pow_of_lt_right {x : ℕ} (H : x > 1) {i j} (h : i < j) : x^i < x^j := begin have xpos := lt_of_succ_lt H, refine lt_of_lt_of_le _ (pow_le_pow_of_le_right xpos h), rw [← mul_one (x^i), pow_succ], exact nat.mul_lt_mul_of_pos_left H (pos_pow_of_pos _ xpos) end /- mod / div / pow -/ theorem mod_pow_succ {b : ℕ} (b_pos : b > 0) (w m : ℕ) : m % (b^succ w) = b * (m/b % b^w) + m % b := begin apply nat.strong_induction_on m, clear m, intros p IH, cases lt_or_ge p (b^succ w) with h₁ h₁, -- base case: p < b^succ w { have h₂ : p / b < b^w, { rw [div_lt_iff_lt_mul p _ b_pos], simp [pow_succ] at h₁, simp [h₁] }, rw [mod_eq_of_lt h₁, mod_eq_of_lt h₂], simp [mod_add_div] }, -- step: p ≥ b^succ w { -- Generate condiition for induction principal have h₂ : p - b^succ w < p, { apply sub_lt_of_pos_le _ _ (pos_pow_of_pos _ b_pos) h₁ }, -- Apply induction rw [mod_eq_sub_mod h₁, IH _ h₂], -- Normalize goal and h1 simp [pow_succ], simp [ge, pow_succ] at h₁, -- Pull subtraction outside mod and div rw [sub_mul_mod _ _ _ h₁, sub_mul_div _ _ _ h₁], -- Cancel subtraction inside mod b^w have p_b_ge : b^w ≤ p / b, { rw [le_div_iff_mul_le _ _ b_pos], simp [h₁] }, rw [eq.symm (mod_eq_sub_mod p_b_ge)] } end end nat
6d50415e4e17a868e675356491e7ecf22c588868
737dc4b96c97368cb66b925eeea3ab633ec3d702
/stage0/src/Lean/Parser/Syntax.lean
1d8750f9e29aa7627bb6d89bfa3823dedd77cf46
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,035
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Parser.Command import Lean.Parser.Tactic namespace Lean namespace Parser builtin_initialize registerBuiltinParserAttribute `builtinSyntaxParser `stx LeadingIdentBehavior.both registerBuiltinDynamicParserAttribute `stxParser `stx builtin_initialize registerBuiltinParserAttribute `builtinPrecParser `prec LeadingIdentBehavior.both registerBuiltinDynamicParserAttribute `precParser `prec @[inline] def precedenceParser (rbp : Nat := 0) : Parser := categoryParser `prec rbp @[inline] def syntaxParser (rbp : Nat := 0) : Parser := categoryParser `stx rbp def «precedence» := leading_parser ":" >> precedenceParser maxPrec def optPrecedence := optional (atomic «precedence») namespace Syntax @[builtinPrecParser] def numPrec := checkPrec maxPrec >> numLit @[builtinSyntaxParser] def paren := leading_parser "(" >> many1 syntaxParser >> ")" @[builtinSyntaxParser] def cat := leading_parser ident >> optPrecedence @[builtinSyntaxParser] def unary := leading_parser ident >> checkNoWsBefore >> "(" >> many1 syntaxParser >> ")" @[builtinSyntaxParser] def binary := leading_parser ident >> checkNoWsBefore >> "(" >> many1 syntaxParser >> ", " >> many1 syntaxParser >> ")" @[builtinSyntaxParser] def sepBy := leading_parser "sepBy(" >> many1 syntaxParser >> ", " >> strLit >> optional (", " >> many1 syntaxParser) >> optional (", " >> nonReservedSymbol "allowTrailingSep") >> ")" @[builtinSyntaxParser] def sepBy1 := leading_parser "sepBy1(" >> many1 syntaxParser >> ", " >> strLit >> optional (", " >> many1 syntaxParser) >> optional (", " >> nonReservedSymbol "allowTrailingSep") >> ")" @[builtinSyntaxParser] def atom := leading_parser strLit @[builtinSyntaxParser] def nonReserved := leading_parser "&" >> strLit end Syntax namespace Term @[builtinTermParser] def stx.quot : Parser := leading_parser "`(stx|" >> incQuotDepth syntaxParser >> ")" @[builtinTermParser] def prec.quot : Parser := leading_parser "`(prec|" >> incQuotDepth precedenceParser >> ")" @[builtinTermParser] def prio.quot : Parser := leading_parser "`(prio|" >> incQuotDepth priorityParser >> ")" end Term namespace Command def namedName := leading_parser (atomic ("(" >> nonReservedSymbol "name") >> " := " >> ident >> ")") def optNamedName := optional namedName def «prefix» := leading_parser "prefix" def «infix» := leading_parser "infix" def «infixl» := leading_parser "infixl" def «infixr» := leading_parser "infixr" def «postfix» := leading_parser "postfix" def mixfixKind := «prefix» <|> «infix» <|> «infixl» <|> «infixr» <|> «postfix» @[builtinCommandParser] def «mixfix» := leading_parser Term.attrKind >> mixfixKind >> precedence >> optNamedName >> optNamedPrio >> ppSpace >> strLit >> darrow >> termParser -- NOTE: We use `suppressInsideQuot` in the following parsers because quotations inside them are evaluated in the same stage and -- thus should be ignored when we use `checkInsideQuot` to prepare the next stage for a builtin syntax change def identPrec := leading_parser ident >> optPrecedence def optKind : Parser := optional ("(" >> nonReservedSymbol "kind" >> ":=" >> ident >> ")") def notationItem := ppSpace >> withAntiquot (mkAntiquot "notationItem" `Lean.Parser.Command.notationItem) (strLit <|> identPrec) @[builtinCommandParser] def «notation» := leading_parser Term.attrKind >> "notation" >> optPrecedence >> optNamedName >> optNamedPrio >> many notationItem >> darrow >> termParser @[builtinCommandParser] def «macro_rules» := suppressInsideQuot (leading_parser optional docComment >> Term.attrKind >> "macro_rules" >> optKind >> Term.matchAlts) @[builtinCommandParser] def «syntax» := leading_parser optional docComment >> Term.attrKind >> "syntax " >> optPrecedence >> optNamedName >> optNamedPrio >> many1 (syntaxParser argPrec) >> " : " >> ident @[builtinCommandParser] def syntaxAbbrev := leading_parser "syntax " >> ident >> " := " >> many1 syntaxParser def catBehaviorBoth := leading_parser nonReservedSymbol "both" def catBehaviorSymbol := leading_parser nonReservedSymbol "symbol" def catBehavior := optional ("(" >> nonReservedSymbol "behavior" >> " := " >> (catBehaviorBoth <|> catBehaviorSymbol) >> ")") @[builtinCommandParser] def syntaxCat := leading_parser "declare_syntax_cat " >> ident >> catBehavior def macroArg := leading_parser optional (atomic (ident >> checkNoWsBefore "no space before ':'" >> ":")) >> syntaxParser argPrec def macroRhs (quotP : Parser) : Parser := leading_parser "`(" >> incQuotDepth quotP >> ")" <|> termParser def macroTailTactic : Parser := atomic (" : " >> identEq "tactic") >> darrow >> macroRhs Tactic.seq1 def macroTailCommand : Parser := atomic (" : " >> identEq "command") >> darrow >> macroRhs (many1Unbox commandParser) def macroTailDefault : Parser := atomic (" : " >> ident) >> darrow >> macroRhs (categoryParserOfStack 2) def macroTail := leading_parser macroTailTactic <|> macroTailCommand <|> macroTailDefault @[builtinCommandParser] def «macro» := leading_parser suppressInsideQuot (optional docComment >> Term.attrKind >> "macro " >> optPrecedence >> optNamedName >> optNamedPrio >> many1 macroArg >> macroTail) @[builtinCommandParser] def «elab_rules» := leading_parser suppressInsideQuot (optional docComment >> Term.attrKind >> "elab_rules" >> optKind >> optional (" : " >> ident) >> optional (" <= " >> ident) >> Term.matchAlts) def elabArg := macroArg def elabTail := leading_parser atomic (" : " >> ident >> optional (" <= " >> ident)) >> darrow >> termParser @[builtinCommandParser] def «elab» := leading_parser suppressInsideQuot (optional docComment >> Term.attrKind >> "elab " >> optPrecedence >> optNamedName >> optNamedPrio >> many1 elabArg >> elabTail) end Command end Parser end Lean
4ed1c0f1d045a0c8c80782578fd363aaa1f063ce
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/stage0/src/Init/Meta.lean
88290d500b606369b8a82cb6965c618a12b3aa49
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,467
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura and Sebastian Ullrich Additional goodies for writing macros -/ prelude import Init.Data.Array.Basic namespace Lean @[extern c inline "lean_box(LEAN_VERSION_MAJOR)"] private constant version.getMajor (u : Unit) : Nat def version.major : Nat := version.getMajor () @[extern c inline "lean_box(LEAN_VERSION_MINOR)"] private constant version.getMinor (u : Unit) : Nat def version.minor : Nat := version.getMinor () @[extern c inline "lean_box(LEAN_VERSION_PATCH)"] private constant version.getPatch (u : Unit) : Nat def version.patch : Nat := version.getPatch () -- @[extern c inline "lean_mk_string(LEAN_GITHASH)"] -- constant getGithash (u : Unit) : String -- def githash : String := getGithash () @[extern c inline "LEAN_VERSION_IS_RELEASE"] constant version.getIsRelease (u : Unit) : Bool def version.isRelease : Bool := version.getIsRelease () /-- Additional version description like "nightly-2018-03-11" -/ @[extern c inline "lean_mk_string(LEAN_SPECIAL_VERSION_DESC)"] constant version.getSpecialDesc (u : Unit) : String def version.specialDesc : String := version.getSpecialDesc () /- Valid identifier names -/ def isGreek (c : Char) : Bool := 0x391 ≤ c.val && c.val ≤ 0x3dd def isLetterLike (c : Char) : Bool := (0x3b1 ≤ c.val && c.val ≤ 0x3c9 && c.val ≠ 0x3bb) || -- Lower greek, but lambda (0x391 ≤ c.val && c.val ≤ 0x3A9 && c.val ≠ 0x3A0 && c.val ≠ 0x3A3) || -- Upper greek, but Pi and Sigma (0x3ca ≤ c.val && c.val ≤ 0x3fb) || -- Coptic letters (0x1f00 ≤ c.val && c.val ≤ 0x1ffe) || -- Polytonic Greek Extended Character Set (0x2100 ≤ c.val && c.val ≤ 0x214f) || -- Letter like block (0x1d49c ≤ c.val && c.val ≤ 0x1d59f) -- Latin letters, Script, Double-struck, Fractur def isNumericSubscript (c : Char) : Bool := 0x2080 ≤ c.val && c.val ≤ 0x2089 def isSubScriptAlnum (c : Char) : Bool := isNumericSubscript c || (0x2090 ≤ c.val && c.val ≤ 0x209c) || (0x1d62 ≤ c.val && c.val ≤ 0x1d6a) def isIdFirst (c : Char) : Bool := c.isAlpha || c = '_' || isLetterLike c def isIdRest (c : Char) : Bool := c.isAlphanum || c = '_' || c = '\'' || c == '!' || c == '?' || isLetterLike c || isSubScriptAlnum c def idBeginEscape := '«' def idEndEscape := '»' def isIdBeginEscape (c : Char) : Bool := c = idBeginEscape def isIdEndEscape (c : Char) : Bool := c = idEndEscape namespace Name def toStringWithSep (sep : String) : Name → String | anonymous => "[anonymous]" | str anonymous s _ => s | num anonymous v _ => toString v | str n s _ => toStringWithSep sep n ++ sep ++ s | num n v _ => toStringWithSep sep n ++ sep ++ Nat.repr v protected def toString : Name → String := toStringWithSep "." instance : ToString Name where toString n := n.toString instance : Repr Name where reprPrec n _ := Std.Format.text "`" ++ n.toString def capitalize : Name → Name | Name.str p s _ => Name.mkStr p s.capitalize | n => n def appendAfter : Name → String → Name | str p s _, suffix => Name.mkStr p (s ++ suffix) | n, suffix => Name.mkStr n suffix def appendIndexAfter : Name → Nat → Name | str p s _, idx => Name.mkStr p (s ++ "_" ++ toString idx) | n, idx => Name.mkStr n ("_" ++ toString idx) def appendBefore : Name → String → Name | anonymous, pre => Name.mkStr anonymous pre | str p s _, pre => Name.mkStr p (pre ++ s) | num p n _, pre => Name.mkNum (Name.mkStr p pre) n end Name structure NameGenerator where namePrefix : Name := `_uniq idx : Nat := 1 deriving Inhabited namespace NameGenerator @[inline] def curr (g : NameGenerator) : Name := Name.mkNum g.namePrefix g.idx @[inline] def next (g : NameGenerator) : NameGenerator := { g with idx := g.idx + 1 } @[inline] def mkChild (g : NameGenerator) : NameGenerator × NameGenerator := ({ namePrefix := Name.mkNum g.namePrefix g.idx, idx := 1 }, { g with idx := g.idx + 1 }) end NameGenerator class MonadNameGenerator (m : Type → Type) where getNGen : m NameGenerator setNGen : NameGenerator → m Unit export MonadNameGenerator (getNGen setNGen) def mkFreshId {m : Type → Type} [Monad m] [MonadNameGenerator m] : m Name := do let ngen ← getNGen let r := ngen.curr setNGen ngen.next pure r instance monadNameGeneratorLift (m n : Type → Type) [MonadLift m n] [MonadNameGenerator m] : MonadNameGenerator n := { getNGen := liftM (getNGen : m _), setNGen := fun ngen => liftM (setNGen ngen : m _) } namespace Syntax partial def getTailInfo : Syntax → Option SourceInfo | atom info _ => info | ident info .. => info | node _ args => args.findSomeRev? getTailInfo | _ => none partial def getTailPos : Syntax → Option String.Pos | atom { pos := some pos, .. } val => some (pos + val.bsize) | ident { pos := some pos, .. } val .. => some (pos + val.toString.bsize) | node _ args => args.findSomeRev? getTailPos | _ => none @[specialize] private partial def updateLast {α} [Inhabited α] (a : Array α) (f : α → Option α) (i : Nat) : Option (Array α) := if i == 0 then none else let i := i - 1 let v := a[i] match f v with | some v => some <| a.set! i v | none => updateLast a f i partial def setTailInfoAux (info : SourceInfo) : Syntax → Option Syntax | atom _ val => some <| atom info val | ident _ rawVal val pre => some <| ident info rawVal val pre | node k args => match updateLast args (setTailInfoAux info) args.size with | some args => some <| node k args | none => none | stx => none def setTailInfo (stx : Syntax) (info : SourceInfo) : Syntax := match setTailInfoAux info stx with | some stx => stx | none => stx def unsetTrailing (stx : Syntax) : Syntax := match stx.getTailInfo with | none => stx | some info => stx.setTailInfo { info with trailing := none } @[specialize] private partial def updateFirst {α} [Inhabited α] (a : Array α) (f : α → Option α) (i : Nat) : Option (Array α) := if h : i < a.size then let v := a.get ⟨i, h⟩; match f v with | some v => some <| a.set ⟨i, h⟩ v | none => updateFirst a f (i+1) else none partial def setHeadInfoAux (info : SourceInfo) : Syntax → Option Syntax | atom _ val => some <| atom info val | ident _ rawVal val pre => some <| ident info rawVal val pre | node k args => match updateFirst args (setHeadInfoAux info) 0 with | some args => some <| node k args | noxne => none | stx => none def setHeadInfo (stx : Syntax) (info : SourceInfo) : Syntax := match setHeadInfoAux info stx with | some stx => stx | none => stx def setInfo (info : SourceInfo) : Syntax → Syntax | atom _ val => atom info val | ident _ rawVal val pre => ident info rawVal val pre | stx => stx partial def replaceInfo (info : SourceInfo) : Syntax → Syntax | node k args => node k <| args.map (replaceInfo info) | stx => setInfo info stx def copyHeadInfo (s : Syntax) (source : Syntax) : Syntax := match source.getHeadInfo with | none => s | some info => s.setHeadInfo info def copyTailInfo (s : Syntax) (source : Syntax) : Syntax := match source.getTailInfo with | none => s | some info => s.setTailInfo info def copyInfo (s : Syntax) (source : Syntax) : Syntax := let s := s.copyHeadInfo source s.copyTailInfo source end Syntax def mkAtom (val : String) : Syntax := Syntax.atom {} val @[inline] def mkNode (k : SyntaxNodeKind) (args : Array Syntax) : Syntax := Syntax.node k args /- Syntax objects for a Lean module. -/ structure Module where header : Syntax commands : Array Syntax /-- Expand all macros in the given syntax -/ partial def expandMacros : Syntax → MacroM Syntax | stx@(Syntax.node k args) => do match (← expandMacro? stx) with | some stxNew => expandMacros stxNew | none => do let args ← Macro.withIncRecDepth stx <| args.mapM expandMacros pure <| Syntax.node k args | stx => pure stx /- Helper functions for processing Syntax programmatically -/ /-- Create an identifier using `SourceInfo` from `src`. To refer to a specific constant, use `mkCIdentFrom` instead. -/ def mkIdentFrom (src : Syntax) (val : Name) : Syntax := let info := src.getHeadInfo.getD {} Syntax.ident info (toString val).toSubstring val [] /-- Create an identifier referring to a constant `c` using `SourceInfo` from `src`. This variant of `mkIdentFrom` makes sure that the identifier cannot accidentally be captured. -/ def mkCIdentFrom (src : Syntax) (c : Name) : Syntax := let info := src.getHeadInfo.getD {} -- Remark: We use the reserved macro scope to make sure there are no accidental collision with our frontend let id := addMacroScope `_internal c reservedMacroScope Syntax.ident info (toString id).toSubstring id [(c, [])] def mkCIdent (c : Name) : Syntax := mkCIdentFrom Syntax.missing c def Syntax.identToAtom (stx : Syntax) : Syntax := match stx with | Syntax.ident info _ val _ => Syntax.atom info (toString val.eraseMacroScopes) | _ => stx @[export lean_mk_syntax_ident] def mkIdent (val : Name) : Syntax := Syntax.ident {} (toString val).toSubstring val [] @[inline] def mkNullNode (args : Array Syntax := #[]) : Syntax := Syntax.node nullKind args def mkSepArray (as : Array Syntax) (sep : Syntax) : Array Syntax := do let mut i := 0 let mut r := #[] for a in as do if i > 0 then r := r.push sep |>.push a else r := r.push a i := i + 1 return r def mkOptionalNode (arg : Option Syntax) : Syntax := match arg with | some arg => Syntax.node nullKind #[arg] | none => Syntax.node nullKind #[] def mkHole (ref : Syntax) : Syntax := Syntax.node `Lean.Parser.Term.hole #[mkAtomFrom ref "_"] namespace Syntax def mkSep (a : Array Syntax) (sep : Syntax) : Syntax := mkNullNode <| mkSepArray a sep def SepArray.ofElems {sep} (elems : Array Syntax) : SepArray sep := ⟨mkSepArray elems (mkAtom sep)⟩ def SepArray.ofElemsUsingRef [Monad m] [MonadRef m] {sep} (elems : Array Syntax) : m (SepArray sep) := do let ref ← getRef; return ⟨mkSepArray elems (mkAtomFrom ref sep)⟩ instance (sep) : Coe (Array Syntax) (SepArray sep) where coe := SepArray.ofElems /-- Create syntax representing a Lean term application, but avoid degenerate empty applications. -/ def mkApp (fn : Syntax) : (args : Array Syntax) → Syntax | #[] => fn | args => Syntax.node `Lean.Parser.Term.app #[fn, mkNullNode args] def mkCApp (fn : Name) (args : Array Syntax) : Syntax := mkApp (mkCIdent fn) args def mkLit (kind : SyntaxNodeKind) (val : String) (info : SourceInfo := {}) : Syntax := let atom : Syntax := Syntax.atom info val Syntax.node kind #[atom] def mkStrLit (val : String) (info : SourceInfo := {}) : Syntax := mkLit strLitKind (String.quote val) info def mkNumLit (val : String) (info : SourceInfo := {}) : Syntax := mkLit numLitKind val info def mkScientificLit (val : String) (info : SourceInfo := {}) : Syntax := mkLit scientificLitKind val info /- Recall that we don't have special Syntax constructors for storing numeric and string atoms. The idea is to have an extensible approach where embedded DSLs may have new kind of atoms and/or different ways of representing them. So, our atoms contain just the parsed string. The main Lean parser uses the kind `numLitKind` for storing natural numbers that can be encoded in binary, octal, decimal and hexadecimal format. `isNatLit` implements a "decoder" for Syntax objects representing these numerals. -/ private partial def decodeBinLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else let c := s.get i if c == '0' then decodeBinLitAux s (s.next i) (2*val) else if c == '1' then decodeBinLitAux s (s.next i) (2*val + 1) else none private partial def decodeOctalLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else let c := s.get i if '0' ≤ c && c ≤ '7' then decodeOctalLitAux s (s.next i) (8*val + c.toNat - '0'.toNat) else none private def decodeHexDigit (s : String) (i : String.Pos) : Option (Nat × String.Pos) := let c := s.get i let i := s.next i if '0' ≤ c && c ≤ '9' then some (c.toNat - '0'.toNat, i) else if 'a' ≤ c && c ≤ 'f' then some (10 + c.toNat - 'a'.toNat, i) else if 'A' ≤ c && c ≤ 'F' then some (10 + c.toNat - 'A'.toNat, i) else none private partial def decodeHexLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else match decodeHexDigit s i with | some (d, i) => decodeHexLitAux s i (16*val + d) | none => none private partial def decodeDecimalLitAux (s : String) (i : String.Pos) (val : Nat) : Option Nat := if s.atEnd i then some val else let c := s.get i if '0' ≤ c && c ≤ '9' then decodeDecimalLitAux s (s.next i) (10*val + c.toNat - '0'.toNat) else none def decodeNatLitVal? (s : String) : Option Nat := let len := s.length if len == 0 then none else let c := s.get 0 if c == '0' then if len == 1 then some 0 else let c := s.get 1 if c == 'x' || c == 'X' then decodeHexLitAux s 2 0 else if c == 'b' || c == 'B' then decodeBinLitAux s 2 0 else if c == 'o' || c == 'O' then decodeOctalLitAux s 2 0 else if c.isDigit then decodeDecimalLitAux s 0 0 else none else if c.isDigit then decodeDecimalLitAux s 0 0 else none def isLit? (litKind : SyntaxNodeKind) (stx : Syntax) : Option String := match stx with | Syntax.node k args => if k == litKind && args.size == 1 then match args.get! 0 with | (Syntax.atom _ val) => some val | _ => none else none | _ => none private def isNatLitAux (litKind : SyntaxNodeKind) (stx : Syntax) : Option Nat := match isLit? litKind stx with | some val => decodeNatLitVal? val | _ => none def isNatLit? (s : Syntax) : Option Nat := isNatLitAux numLitKind s def isFieldIdx? (s : Syntax) : Option Nat := isNatLitAux fieldIdxKind s partial def decodeScientificLitVal? (s : String) : Option (Nat × Bool × Nat) := let len := s.length if len == 0 then none else let c := s.get 0 if c.isDigit then decode 0 0 else none where decodeAfterExp (i : String.Pos) (val : Nat) (e : Nat) (sign : Bool) (exp : Nat) : Option (Nat × Bool × Nat) := if s.atEnd i then if sign then some (val, sign, exp + e) else if exp >= e then some (val, sign, exp - e) else some (val, true, e - exp) else let c := s.get i if '0' ≤ c && c ≤ '9' then decodeAfterExp (s.next i) val e sign (10*exp + c.toNat - '0'.toNat) else none decodeExp (i : String.Pos) (val : Nat) (e : Nat) : Option (Nat × Bool × Nat) := let c := s.get i if c == '-' then decodeAfterExp (s.next i) val e true 0 else decodeAfterExp i val e false 0 decodeAfterDot (i : String.Pos) (val : Nat) (e : Nat) : Option (Nat × Bool × Nat) := if s.atEnd i then some (val, true, e) else let c := s.get i if '0' ≤ c && c ≤ '9' then decodeAfterDot (s.next i) (10*val + c.toNat - '0'.toNat) (e+1) else if c == 'e' || c == 'E' then decodeExp (s.next i) val e else none decode (i : String.Pos) (val : Nat) : Option (Nat × Bool × Nat) := if s.atEnd i then none else let c := s.get i if '0' ≤ c && c ≤ '9' then decode (s.next i) (10*val + c.toNat - '0'.toNat) else if c == '.' then decodeAfterDot (s.next i) val 0 else if c == 'e' || c == 'E' then decodeExp (s.next i) val 0 else none def isScientificLit? (stx : Syntax) : Option (Nat × Bool × Nat) := match isLit? scientificLitKind stx with | some val => decodeScientificLitVal? val | _ => none def isIdOrAtom? : Syntax → Option String | Syntax.atom _ val => some val | Syntax.ident _ rawVal _ _ => some rawVal.toString | _ => none def toNat (stx : Syntax) : Nat := match stx.isNatLit? with | some val => val | none => 0 def decodeQuotedChar (s : String) (i : String.Pos) : Option (Char × String.Pos) := do let c := s.get i let i := s.next i if c == '\\' then pure ('\\', i) else if c = '\"' then pure ('\"', i) else if c = '\'' then pure ('\'', i) else if c = 'r' then pure ('\r', i) else if c = 'n' then pure ('\n', i) else if c = 't' then pure ('\t', i) else if c = 'x' then let (d₁, i) ← decodeHexDigit s i let (d₂, i) ← decodeHexDigit s i pure (Char.ofNat (16*d₁ + d₂), i) else if c = 'u' then do let (d₁, i) ← decodeHexDigit s i let (d₂, i) ← decodeHexDigit s i let (d₃, i) ← decodeHexDigit s i let (d₄, i) ← decodeHexDigit s i pure (Char.ofNat (16*(16*(16*d₁ + d₂) + d₃) + d₄), i) else none partial def decodeStrLitAux (s : String) (i : String.Pos) (acc : String) : Option String := do let c := s.get i let i := s.next i if c == '\"' then pure acc else if s.atEnd i then none else if c == '\\' then do let (c, i) ← decodeQuotedChar s i decodeStrLitAux s i (acc.push c) else decodeStrLitAux s i (acc.push c) def decodeStrLit (s : String) : Option String := decodeStrLitAux s 1 "" def isStrLit? (stx : Syntax) : Option String := match isLit? strLitKind stx with | some val => decodeStrLit val | _ => none def decodeCharLit (s : String) : Option Char := let c := s.get 1 if c == '\\' then do let (c, _) ← decodeQuotedChar s 2 pure c else pure c def isCharLit? (stx : Syntax) : Option Char := match isLit? charLitKind stx with | some val => decodeCharLit val | _ => none private partial def decodeNameLitAux (s : String) (i : Nat) (r : Name) : Option Name := do let continue? (i : Nat) (r : Name) : Option Name := if s.get i == '.' then decodeNameLitAux s (s.next i) r else if s.atEnd i then pure r else none let curr := s.get i if isIdBeginEscape curr then let startPart := s.next i let stopPart := s.nextUntil isIdEndEscape startPart if !isIdEndEscape (s.get stopPart) then none else continue? (s.next stopPart) (Name.mkStr r (s.extract startPart stopPart)) else if isIdFirst curr then let startPart := i let stopPart := s.nextWhile isIdRest startPart continue? stopPart (Name.mkStr r (s.extract startPart stopPart)) else none def decodeNameLit (s : String) : Option Name := if s.get 0 == '`' then decodeNameLitAux s 1 Name.anonymous else none def isNameLit? (stx : Syntax) : Option Name := match isLit? nameLitKind stx with | some val => decodeNameLit val | _ => none def hasArgs : Syntax → Bool | Syntax.node _ args => args.size > 0 | _ => false def identToStrLit (stx : Syntax) : Syntax := match stx with | Syntax.ident info _ val _ => mkStrLit (toString val) info | _ => stx def strLitToAtom (stx : Syntax) : Syntax := match stx.isStrLit? with | none => stx | some val => match stx.getHeadInfo with | some info => Syntax.atom info val | none => unreachable! def isAtom : Syntax → Bool | atom _ _ => true | _ => false def isToken (token : String) : Syntax → Bool | atom _ val => val.trim == token.trim | _ => false def isIdent : Syntax → Bool | ident _ _ _ _ => true | _ => false def getId : Syntax → Name | ident _ _ val _ => val | _ => Name.anonymous def isNone (stx : Syntax) : Bool := match stx with | Syntax.node k args => k == nullKind && args.size == 0 -- when elaborating partial syntax trees, it's reasonable to interpret missing parts as `none` | Syntax.missing => true | _ => false def getOptional? (stx : Syntax) : Option Syntax := match stx with | Syntax.node k args => if k == nullKind && args.size == 1 then some (args.get! 0) else none | _ => none def getOptionalIdent? (stx : Syntax) : Option Name := match stx.getOptional? with | some stx => some stx.getId | none => none partial def findAux (p : Syntax → Bool) : Syntax → Option Syntax | stx@(Syntax.node _ args) => if p stx then some stx else args.findSome? (findAux p) | stx => if p stx then some stx else none def find? (stx : Syntax) (p : Syntax → Bool) : Option Syntax := findAux p stx end Syntax /-- Reflect a runtime datum back to surface syntax (best-effort). -/ class Quote (α : Type) where quote : α → Syntax export Quote (quote) instance : Quote Syntax := ⟨id⟩ instance : Quote Bool := ⟨fun | true => mkCIdent `Bool.true | false => mkCIdent `Bool.false⟩ instance : Quote String := ⟨Syntax.mkStrLit⟩ instance : Quote Nat := ⟨fun n => Syntax.mkNumLit <| toString n⟩ instance : Quote Substring := ⟨fun s => Syntax.mkCApp `String.toSubstring #[quote s.toString]⟩ private def quoteName : Name → Syntax | Name.anonymous => mkCIdent ``Name.anonymous | Name.str n s _ => Syntax.mkCApp ``Name.mkStr #[quoteName n, quote s] | Name.num n i _ => Syntax.mkCApp ``Name.mkNum #[quoteName n, quote i] instance : Quote Name := ⟨quoteName⟩ instance {α β : Type} [Quote α] [Quote β] : Quote (α × β) where quote | ⟨a, b⟩ => Syntax.mkCApp ``Prod.mk #[quote a, quote b] private def quoteList {α : Type} [Quote α] : List α → Syntax | [] => mkCIdent ``List.nil | (x::xs) => Syntax.mkCApp ``List.cons #[quote x, quoteList xs] instance {α : Type} [Quote α] : Quote (List α) where quote := quoteList instance {α : Type} [Quote α] : Quote (Array α) where quote xs := Syntax.mkCApp ``List.toArray #[quote xs.toList] private def quoteOption {α : Type} [Quote α] : Option α → Syntax | none => mkIdent ``none | (some x) => Syntax.mkCApp ``some #[quote x] instance Option.hasQuote {α : Type} [Quote α] : Quote (Option α) where quote := quoteOption /- Evaluator for `prec` DSL -/ def evalPrec (stx : Syntax) : MacroM Nat := Macro.withIncRecDepth stx do let stx ← expandMacros stx match stx with | `(prec| $num:numLit) => return num.isNatLit?.getD 0 | _ => Macro.throwErrorAt stx "unexpected precedence" macro_rules | `(prec| $a + $b) => do `(prec| $(quote <| (← evalPrec a) + (← evalPrec b)):numLit) macro_rules | `(prec| $a - $b) => do `(prec| $(quote <| (← evalPrec a) - (← evalPrec b)):numLit) macro "evalPrec! " p:prec:max : term => return quote (← evalPrec p) def evalOptPrec : Option Syntax → MacroM Nat | some prec => evalPrec prec | none => return 0 /- Evaluator for `prio` DSL -/ def evalPrio (stx : Syntax) : MacroM Nat := Macro.withIncRecDepth stx do let stx ← expandMacros stx match stx with | `(prio| $num:numLit) => return num.isNatLit?.getD 0 | _ => Macro.throwErrorAt stx "unexpected priority" macro_rules | `(prio| $a + $b) => do `(prio| $(quote <| (← evalPrio a) + (← evalPrio b)):numLit) macro_rules | `(prio| $a - $b) => do `(prio| $(quote <| (← evalPrio a) - (← evalPrio b)):numLit) macro "evalPrio! " p:prio:max : term => return quote (← evalPrio p) def evalOptPrio : Option Syntax → MacroM Nat | some prio => evalPrio prio | none => return evalPrio! default end Lean namespace Array abbrev getSepElems := @getEvenElems open Lean private partial def filterSepElemsMAux {m : Type → Type} [Monad m] (a : Array Syntax) (p : Syntax → m Bool) (i : Nat) (acc : Array Syntax) : m (Array Syntax) := do if h : i < a.size then let stx := a.get ⟨i, h⟩ if (← p stx) then if acc.isEmpty then filterSepElemsMAux a p (i+2) (acc.push stx) else if hz : i ≠ 0 then have i.pred < i from Nat.predLt hz let sepStx := a.get ⟨i.pred, Nat.ltTrans this h⟩ filterSepElemsMAux a p (i+2) ((acc.push sepStx).push stx) else filterSepElemsMAux a p (i+2) (acc.push stx) else filterSepElemsMAux a p (i+2) acc else pure acc def filterSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (p : Syntax → m Bool) : m (Array Syntax) := filterSepElemsMAux a p 0 #[] def filterSepElems (a : Array Syntax) (p : Syntax → Bool) : Array Syntax := Id.run <| a.filterSepElemsM p private partial def mapSepElemsMAux {m : Type → Type} [Monad m] (a : Array Syntax) (f : Syntax → m Syntax) (i : Nat) (acc : Array Syntax) : m (Array Syntax) := do if h : i < a.size then let stx := a.get ⟨i, h⟩ if i % 2 == 0 then do let stx ← f stx mapSepElemsMAux a f (i+1) (acc.push stx) else mapSepElemsMAux a f (i+1) (acc.push stx) else pure acc def mapSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (f : Syntax → m Syntax) : m (Array Syntax) := mapSepElemsMAux a f 0 #[] def mapSepElems (a : Array Syntax) (f : Syntax → Syntax) : Array Syntax := Id.run <| a.mapSepElemsM f end Array namespace Lean.Syntax.SepArray def getElems {sep} (sa : SepArray sep) : Array Syntax := sa.elemsAndSeps.getSepElems instance (sep) : Coe (SepArray sep) (Array Syntax) where coe := getElems end Lean.Syntax.SepArray /-- Gadget for automatic parameter support. This is similar to the `optParam` gadget, but it uses the given tactic. Like `optParam`, this gadget only affects elaboration. For example, the tactic will *not* be invoked during type class resolution. -/ abbrev autoParam.{u} (α : Sort u) (tactic : Lean.Syntax) : Sort u := α /- Helper functions for manipulating interpolated strings -/ namespace Lean.Syntax private def decodeInterpStrQuotedChar (s : String) (i : String.Pos) : Option (Char × String.Pos) := match decodeQuotedChar s i with | some r => some r | none => let c := s.get i let i := s.next i if c == '{' then pure ('{', i) else none private partial def decodeInterpStrLit (s : String) : Option String := let rec loop (i : String.Pos) (acc : String) := let c := s.get i let i := s.next i if c == '\"' || c == '{' then pure acc else if s.atEnd i then none else if c == '\\' then do let (c, i) ← decodeInterpStrQuotedChar s i loop i (acc.push c) else loop i (acc.push c) loop 1 "" partial def isInterpolatedStrLit? (stx : Syntax) : Option String := match isLit? interpolatedStrLitKind stx with | none => none | some val => decodeInterpStrLit val def expandInterpolatedStrChunks (chunks : Array Syntax) (mkAppend : Syntax → Syntax → MacroM Syntax) (mkElem : Syntax → MacroM Syntax) : MacroM Syntax := do let mut i := 0 let mut result := Syntax.missing for elem in chunks do let elem ← match elem.isInterpolatedStrLit? with | none => mkElem elem | some str => mkElem (Syntax.mkStrLit str) if i == 0 then result := elem else result ← mkAppend result elem i := i+1 return result def expandInterpolatedStr (interpStr : Syntax) (type : Syntax) (toTypeFn : Syntax) : MacroM Syntax := do let ref := interpStr let r ← expandInterpolatedStrChunks interpStr.getArgs (fun a b => `($a ++ $b)) (fun a => `($toTypeFn $a)) `(($r : $type)) def getSepArgs (stx : Syntax) : Array Syntax := stx.getArgs.getSepElems end Syntax namespace Meta.Simp def defaultMaxSteps := 100000 structure Config where maxSteps : Nat := defaultMaxSteps contextual : Bool := false memoize : Bool := true singlePass : Bool := false zeta : Bool := true beta : Bool := true eta : Bool := true iota : Bool := true proj : Bool := true ctorEq : Bool := true deriving Inhabited, BEq, Repr end Meta.Simp end Lean
4c5fa0087198aa69417715c840a4ce2185fc8560
82e44445c70db0f03e30d7be725775f122d72f3e
/src/category_theory/category/Kleisli.lean
204325a09cff360a3757b2e192d4c8d78b3457ae
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
1,659
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon The Kleisli construction on the Type category TODO: generalise this to work with category_theory.monad -/ import category_theory.category /-! # The Kleisli construction on the Type category Define the Kleisli category for (control) monads. `category_theory/monad/kleisli` defines the general version for a monad on `C`, and demonstrates the equivalence between the two. -/ universes u v namespace category_theory /-- The Kleisli category on the (type-)monad `m`. Note that the monad is not assumed to be lawful yet. -/ @[nolint unused_arguments] def Kleisli (m : Type u → Type v) := Type u /-- Construct an object of the Kleisli category from a type. -/ def Kleisli.mk (m) (α : Type u) : Kleisli m := α instance Kleisli.category_struct {m} [monad.{u v} m] : category_struct (Kleisli m) := { hom := λ α β, α → m β, id := λ α x, pure x, comp := λ X Y Z f g, f >=> g } instance Kleisli.category {m} [monad.{u v} m] [is_lawful_monad m] : category (Kleisli m) := by refine { id_comp' := _, comp_id' := _, assoc' := _ }; intros; ext; unfold_projs; simp only [(>=>)] with functor_norm @[simp] lemma Kleisli.id_def {m} [monad m] (α : Kleisli m) : 𝟙 α = @pure m _ α := rfl lemma Kleisli.comp_def {m} [monad m] (α β γ : Kleisli m) (xs : α ⟶ β) (ys : β ⟶ γ) (a : α) : (xs ≫ ys) a = xs a >>= ys := rfl instance : inhabited (Kleisli id) := ⟨punit⟩ instance {α : Type u} [inhabited α] : inhabited (Kleisli.mk id α) := ⟨(default α : _)⟩ end category_theory
ca4a96ac21e111690e50c2a8af45321725e6fe4e
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch2/ex0201.lean
6778f1dda98fb80f104d89f755977b40253c0365
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
221
lean
#check nat #check bool #check nat → bool #check nat × bool #check nat → nat #check nat × nat → nat #check nat → nat → nat #check nat → (nat → nat) #check nat → nat → bool #check (nat → nat) → nat
5fb81a864f328ed53d814ce7772e87ba886d3884
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/field_theory/polynomial_galois_group.lean
26955e7676afbabd6021f5911d416d378f730e2b
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,625
lean
/- Copyright (c) 2020 Thomas Browning and Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning and Patrick Lutz -/ import field_theory.galois /-! # Galois Groups of Polynomials In this file we introduce the Galois group of a polynomial, defined as the automorphism group of the splitting field. ## Main definitions - `gal p`: the Galois group of a polynomial p. - `restrict p E`: the restriction homomorphism `(E ≃ₐ[F] E) → gal p`. - `gal_action p E`: the action of `gal p` on the roots of `p` in `E`. ## Main results - `restrict_smul`: `restrict p E` is compatible with `gal_action p E`. - `gal_action_hom_injective`: the action of `gal p` on the roots of `p` in `E` is faithful. - `restrict_prod_inj`: `gal (p * q)` embeds as a subgroup of `gal p × gal q`. -/ noncomputable theory open_locale classical open finite_dimensional namespace polynomial variables {F : Type*} [field F] (p q : polynomial F) (E : Type*) [field E] [algebra F E] /-- The Galois group of a polynomial -/ @[derive [has_coe_to_fun, group, fintype]] def gal := p.splitting_field ≃ₐ[F] p.splitting_field namespace gal @[ext] lemma ext {σ τ : p.gal} (h : ∀ x ∈ p.root_set p.splitting_field, σ x = τ x) : σ = τ := begin refine alg_equiv.ext (λ x, (alg_hom.mem_equalizer σ.to_alg_hom τ.to_alg_hom x).mp ((subalgebra.ext_iff.mp _ x).mpr algebra.mem_top)), rwa [eq_top_iff, ←splitting_field.adjoin_roots, algebra.adjoin_le_iff], end instance [h : fact (p.splits (ring_hom.id F))] : unique p.gal := { default := 1, uniq := λ f, alg_equiv.ext (λ x, by { obtain ⟨y, rfl⟩ := algebra.mem_bot.mp ((subalgebra.ext_iff.mp ((is_splitting_field.splits_iff _ p).mp h) x).mp algebra.mem_top), rw [alg_equiv.commutes, alg_equiv.commutes] }) } /-- If `p` splits in `F` then the `p.gal` is trivial. -/ def unique_gal_of_splits (h : p.splits (ring_hom.id F)) : unique p.gal := { default := 1, uniq := λ f, alg_equiv.ext (λ x, by { obtain ⟨y, rfl⟩ := algebra.mem_bot.mp ((subalgebra.ext_iff.mp ((is_splitting_field.splits_iff _ p).mp h) x).mp algebra.mem_top), rw [alg_equiv.commutes, alg_equiv.commutes] }) } instance unique_gal_zero : unique (0 : polynomial F).gal := unique_gal_of_splits _ (splits_zero _) instance unique_gal_one : unique (1 : polynomial F).gal := unique_gal_of_splits _ (splits_one _) instance unique_gal_C (x : F) : unique (C x).gal := unique_gal_of_splits _ (splits_C _ _) instance unique_gal_X : unique (X : polynomial F).gal := unique_gal_of_splits _ (splits_X _) instance unique_gal_X_sub_C (x : F) : unique (X - C x).gal := unique_gal_of_splits _ (splits_X_sub_C _) instance unique_gal_X_pow (n : ℕ) : unique (X ^ n : polynomial F).gal := unique_gal_of_splits _ (splits_X_pow _ _) instance [h : fact (p.splits (algebra_map F E))] : algebra p.splitting_field E := (is_splitting_field.lift p.splitting_field p h).to_ring_hom.to_algebra instance [h : fact (p.splits (algebra_map F E))] : is_scalar_tower F p.splitting_field E := is_scalar_tower.of_algebra_map_eq (λ x, ((is_splitting_field.lift p.splitting_field p h).commutes x).symm) /-- The restriction homomorphism -/ def restrict [h : fact (p.splits (algebra_map F E))] : (E ≃ₐ[F] E) →* p.gal := alg_equiv.restrict_normal_hom p.splitting_field lemma restrict_surjective [h : fact (p.splits (algebra_map F E))] [normal F E] : function.surjective (restrict p E) := alg_equiv.restrict_normal_hom_surjective E section roots_action /-- The function from `roots p p.splitting_field` to `roots p E` -/ def map_roots [h : fact (p.splits (algebra_map F E))] : root_set p p.splitting_field → root_set p E := λ x, ⟨is_scalar_tower.to_alg_hom F p.splitting_field E x, begin have key := subtype.mem x, by_cases p = 0, { simp only [h, root_set_zero] at key, exact false.rec _ key }, { rw [mem_root_set h, aeval_alg_hom_apply, (mem_root_set h).mp key, alg_hom.map_zero] } end⟩ lemma map_roots_bijective [h : fact (p.splits (algebra_map F E))] : function.bijective (map_roots p E) := begin split, { exact λ _ _ h, subtype.ext (ring_hom.injective _ (subtype.ext_iff.mp h)) }, { intro y, have key := roots_map (is_scalar_tower.to_alg_hom F p.splitting_field E : p.splitting_field →+* E) ((splits_id_iff_splits _).mpr (is_splitting_field.splits p.splitting_field p)), rw [map_map, alg_hom.comp_algebra_map] at key, have hy := subtype.mem y, simp only [root_set, finset.mem_coe, multiset.mem_to_finset, key, multiset.mem_map] at hy, rcases hy with ⟨x, hx1, hx2⟩, exact ⟨⟨x, multiset.mem_to_finset.mpr hx1⟩, subtype.ext hx2⟩ } end /-- The bijection between `root_set p p.splitting_field` and `root_set p E` -/ def roots_equiv_roots [h : fact (p.splits (algebra_map F E))] : (root_set p p.splitting_field) ≃ (root_set p E) := equiv.of_bijective (map_roots p E) (map_roots_bijective p E) instance gal_action_aux : mul_action p.gal (root_set p p.splitting_field) := { smul := λ ϕ x, ⟨ϕ x, begin have key := subtype.mem x, --simp only [root_set, finset.mem_coe, multiset.mem_to_finset] at *, by_cases p = 0, { simp only [h, root_set_zero] at key, exact false.rec _ key }, { rw mem_root_set h, change aeval (ϕ.to_alg_hom x) p = 0, rw [aeval_alg_hom_apply, (mem_root_set h).mp key, alg_hom.map_zero] } end⟩, one_smul := λ _, by { ext, refl }, mul_smul := λ _ _ _, by { ext, refl } } instance gal_action [h : fact (p.splits (algebra_map F E))] : mul_action p.gal (root_set p E) := { smul := λ ϕ x, roots_equiv_roots p E (ϕ • ((roots_equiv_roots p E).symm x)), one_smul := λ _, by simp only [equiv.apply_symm_apply, one_smul], mul_smul := λ _ _ _, by simp only [equiv.apply_symm_apply, equiv.symm_apply_apply, mul_smul] } variables {p E} @[simp] lemma restrict_smul [h : fact (p.splits (algebra_map F E))] (ϕ : E ≃ₐ[F] E) (x : root_set p E) : ↑((restrict p E ϕ) • x) = ϕ x := begin let ψ := alg_equiv.of_injective_field (is_scalar_tower.to_alg_hom F p.splitting_field E), change ↑(ψ (ψ.symm _)) = ϕ x, rw alg_equiv.apply_symm_apply ψ, change ϕ (roots_equiv_roots p E ((roots_equiv_roots p E).symm x)) = ϕ x, rw equiv.apply_symm_apply (roots_equiv_roots p E), end variables (p E) /-- `gal_action` as a permutation representation -/ def gal_action_hom [h : fact (p.splits (algebra_map F E))] : p.gal →* equiv.perm (root_set p E) := { to_fun := λ ϕ, equiv.mk (λ x, ϕ • x) (λ x, ϕ⁻¹ • x) (λ x, inv_smul_smul ϕ x) (λ x, smul_inv_smul ϕ x), map_one' := by { ext1 x, exact mul_action.one_smul x }, map_mul' := λ x y, by { ext1 z, exact mul_action.mul_smul x y z } } lemma gal_action_hom_injective [h : fact (p.splits (algebra_map F E))] : function.injective (gal_action_hom p E) := begin rw monoid_hom.injective_iff, intros ϕ hϕ, let equalizer := alg_hom.equalizer ϕ.to_alg_hom (alg_hom.id F p.splitting_field), suffices : equalizer = ⊤, { exact alg_equiv.ext (λ x, (subalgebra.ext_iff.mp this x).mpr algebra.mem_top) }, rw [eq_top_iff, ←splitting_field.adjoin_roots, algebra.adjoin_le_iff], intros x hx, have key := equiv.perm.ext_iff.mp hϕ (roots_equiv_roots p E ⟨x, hx⟩), change roots_equiv_roots p E (ϕ • (roots_equiv_roots p E).symm (roots_equiv_roots p E ⟨x, hx⟩)) = roots_equiv_roots p E ⟨x, hx⟩ at key, rw equiv.symm_apply_apply at key, exact subtype.ext_iff.mp (equiv.injective (roots_equiv_roots p E) key), end end roots_action variables {p q} /-- The restriction homomorphism between Galois groups -/ def restrict_dvd (hpq : p ∣ q) : q.gal →* p.gal := if hq : q = 0 then 1 else @restrict F _ p _ _ _ (splits_of_splits_of_dvd (algebra_map F q.splitting_field) hq (splitting_field.splits q) hpq) lemma restrict_dvd_surjective (hpq : p ∣ q) (hq : q ≠ 0) : function.surjective (restrict_dvd hpq) := by simp only [restrict_dvd, dif_neg hq, restrict_surjective] variables (p q) /-- The Galois group of a product embeds into the product of the Galois groups -/ def restrict_prod : (p * q).gal →* p.gal × q.gal := monoid_hom.prod (restrict_dvd (dvd_mul_right p q)) (restrict_dvd (dvd_mul_left q p)) lemma restrict_prod_injective : function.injective (restrict_prod p q) := begin by_cases hpq : (p * q) = 0, { haveI : unique (p * q).gal := by { rw hpq, apply_instance }, exact λ f g h, eq.trans (unique.eq_default f) (unique.eq_default g).symm }, intros f g hfg, dsimp only [restrict_prod, restrict_dvd] at hfg, simp only [dif_neg hpq, monoid_hom.prod_apply, prod.mk.inj_iff] at hfg, suffices : alg_hom.equalizer f.to_alg_hom g.to_alg_hom = ⊤, { exact alg_equiv.ext (λ x, (subalgebra.ext_iff.mp this x).mpr algebra.mem_top) }, rw [eq_top_iff, ←splitting_field.adjoin_roots, algebra.adjoin_le_iff], intros x hx, rw [map_mul, polynomial.roots_mul] at hx, cases multiset.mem_add.mp (multiset.mem_to_finset.mp hx) with h h, { change f x = g x, haveI : fact (p.splits (algebra_map F (p * q).splitting_field)) := splits_of_splits_of_dvd _ hpq (splitting_field.splits (p * q)) (dvd_mul_right p q), have key : x = algebra_map (p.splitting_field) (p * q).splitting_field ((roots_equiv_roots p _).inv_fun ⟨x, multiset.mem_to_finset.mpr h⟩) := subtype.ext_iff.mp (equiv.apply_symm_apply (roots_equiv_roots p _) ⟨x, _⟩).symm, rw [key, ←alg_equiv.restrict_normal_commutes, ←alg_equiv.restrict_normal_commutes], exact congr_arg _ (alg_equiv.ext_iff.mp hfg.1 _) }, { change f x = g x, haveI : fact (q.splits (algebra_map F (p * q).splitting_field)) := splits_of_splits_of_dvd _ hpq (splitting_field.splits (p * q)) (dvd_mul_left q p), have key : x = algebra_map (q.splitting_field) (p * q).splitting_field ((roots_equiv_roots q _).inv_fun ⟨x, multiset.mem_to_finset.mpr h⟩) := subtype.ext_iff.mp (equiv.apply_symm_apply (roots_equiv_roots q _) ⟨x, _⟩).symm, rw [key, ←alg_equiv.restrict_normal_commutes, ←alg_equiv.restrict_normal_commutes], exact congr_arg _ (alg_equiv.ext_iff.mp hfg.2 _) }, { rwa [ne.def, mul_eq_zero, map_eq_zero, map_eq_zero, ←mul_eq_zero] } end lemma mul_splits_in_splitting_field_of_mul {p₁ q₁ p₂ q₂ : polynomial F} (hq₁ : q₁ ≠ 0) (hq₂ : q₂ ≠ 0) (h₁ : p₁.splits (algebra_map F q₁.splitting_field)) (h₂ : p₂.splits (algebra_map F q₂.splitting_field)) : (p₁ * p₂).splits (algebra_map F (q₁ * q₂).splitting_field) := begin apply splits_mul, { rw ← (splitting_field.lift q₁ (splits_of_splits_of_dvd _ (mul_ne_zero hq₁ hq₂) (splitting_field.splits _) (dvd_mul_right q₁ q₂))).comp_algebra_map, exact splits_comp_of_splits _ _ h₁, }, { rw ← (splitting_field.lift q₂ (splits_of_splits_of_dvd _ (mul_ne_zero hq₁ hq₂) (splitting_field.splits _) (dvd_mul_left q₂ q₁))).comp_algebra_map, exact splits_comp_of_splits _ _ h₂, }, end lemma splits_in_splitting_field_of_comp (hq : q.nat_degree ≠ 0) : p.splits (algebra_map F (p.comp q).splitting_field) := begin let P : polynomial F → Prop := λ r, r.splits (algebra_map F (r.comp q).splitting_field), have key1 : ∀ {r : polynomial F}, irreducible r → P r, { intros r hr, by_cases hr' : nat_degree r = 0, { exact splits_of_nat_degree_le_one _ (le_trans (le_of_eq hr') zero_le_one) }, obtain ⟨x, hx⟩ := exists_root_of_splits _ (splitting_field.splits (r.comp q)) (λ h, hr' ((mul_eq_zero.mp (nat_degree_comp.symm.trans (nat_degree_eq_of_degree_eq_some h))).resolve_right hq)), rw [←aeval_def, aeval_comp] at hx, have h_normal : normal F (r.comp q).splitting_field := splitting_field.normal (r.comp q), have qx_int := normal.is_integral h_normal (aeval x q), exact splits_of_splits_of_dvd _ (minpoly.ne_zero qx_int) (normal.splits h_normal _) (dvd_symm_of_irreducible (minpoly.irreducible qx_int) hr (minpoly.dvd F _ hx)) }, have key2 : ∀ {p₁ p₂ : polynomial F}, P p₁ → P p₂ → P (p₁ * p₂), { intros p₁ p₂ hp₁ hp₂, by_cases h₁ : p₁.comp q = 0, { cases comp_eq_zero_iff.mp h₁ with h h, { rw [h, zero_mul], exact splits_zero _ }, { exact false.rec _ (hq (by rw [h.2, nat_degree_C])) } }, by_cases h₂ : p₂.comp q = 0, { cases comp_eq_zero_iff.mp h₂ with h h, { rw [h, mul_zero], exact splits_zero _ }, { exact false.rec _ (hq (by rw [h.2, nat_degree_C])) } }, have key := mul_splits_in_splitting_field_of_mul h₁ h₂ hp₁ hp₂, rwa ← mul_comp at key }, exact wf_dvd_monoid.induction_on_irreducible p (splits_zero _) (λ _, splits_of_is_unit _) (λ _ _ _ h, key2 (key1 h)), end /-- The restriction homomorphism from the Galois group of a homomorphism -/ def restrict_comp (hq : q.nat_degree ≠ 0) : (p.comp q).gal →* p.gal := @restrict F _ p _ _ _ (splits_in_splitting_field_of_comp p q hq) lemma restrict_comp_surjective (hq : q.nat_degree ≠ 0) : function.surjective (restrict_comp p q hq) := by simp only [restrict_comp, restrict_surjective] variables {p q} lemma card_of_separable (hp : p.separable) : fintype.card p.gal = findim F p.splitting_field := begin haveI : is_galois F p.splitting_field := is_galois.of_separable_splitting_field hp, exact is_galois.card_aut_eq_findim F p.splitting_field, end lemma prime_degree_dvd_card [char_zero F] (p_irr : irreducible p) (p_deg : p.nat_degree.prime) : p.nat_degree ∣ fintype.card p.gal := begin rw gal.card_of_separable p_irr.separable, have hp : p.degree ≠ 0 := λ h, nat.prime.ne_zero p_deg (nat_degree_eq_zero_iff_degree_le_zero.mpr (le_of_eq h)), let α : p.splitting_field := root_of_splits (algebra_map F p.splitting_field) (splitting_field.splits p) hp, have hα : is_integral F α := (is_algebraic_iff_is_integral F).mp (algebra.is_algebraic_of_finite α), use finite_dimensional.findim F⟮α⟯ p.splitting_field, suffices : (minpoly F α).nat_degree = p.nat_degree, { rw [←finite_dimensional.findim_mul_findim F F⟮α⟯ p.splitting_field, intermediate_field.adjoin.findim hα, this] }, suffices : minpoly F α ∣ p, { have key := dvd_symm_of_irreducible (minpoly.irreducible hα) p_irr this, apply le_antisymm, { exact nat_degree_le_of_dvd this p_irr.ne_zero }, { exact nat_degree_le_of_dvd key (minpoly.ne_zero hα) } }, apply minpoly.dvd F α, rw [aeval_def, map_root_of_splits _ (splitting_field.splits p) hp], end end gal end polynomial
0af6fb346846738bebba3d691489708b9fc26729
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebraic_geometry/Gamma_Spec_adjunction.lean
ebf8d621b59e427c01f6b1e340af7d9d33cf65fe
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
16,141
lean
/- Copyright (c) 2021 Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Junyan Xu -/ import algebraic_geometry.Scheme import category_theory.adjunction.limits import category_theory.adjunction.reflective /-! # Adjunction between `Γ` and `Spec` We define the adjunction `Γ_Spec.adjunction : Γ ⊣ Spec` by defining the unit (`to_Γ_Spec`, in multiple steps in this file) and counit (done in Spec.lean) and checking that they satisfy the left and right triangle identities. The constructions and proofs make use of maps and lemmas defined and proved in structure_sheaf.lean extensively. Notice that since the adjunction is between contravariant functors, you get to choose one of the two categories to have arrows reversed, and it is equally valid to present the adjunction as `Spec ⊣ Γ` (`Spec.to_LocallyRingedSpace.right_op ⊣ Γ`), in which case the unit and the counit would switch to each other. ## Main definition * `algebraic_geometry.identity_to_Γ_Spec` : The natural transformation `𝟭 _ ⟶ Γ ⋙ Spec`. * `algebraic_geometry.Γ_Spec.LocallyRingedSpace_adjunction` : The adjunction `Γ ⊣ Spec` from `CommRingᵒᵖ` to `LocallyRingedSpace`. * `algebraic_geometry.Γ_Spec.adjunction` : The adjunction `Γ ⊣ Spec` from `CommRingᵒᵖ` to `Scheme`. -/ noncomputable theory universes u open prime_spectrum namespace algebraic_geometry open opposite open category_theory open structure_sheaf Spec (structure_sheaf) open topological_space open algebraic_geometry.LocallyRingedSpace open Top.presheaf open Top.presheaf.sheaf_condition namespace LocallyRingedSpace variable (X : LocallyRingedSpace.{u}) /-- The map from the global sections to a stalk. -/ def Γ_to_stalk (x : X) : Γ.obj (op X) ⟶ X.presheaf.stalk x := X.presheaf.germ (⟨x,trivial⟩ : (⊤ : opens X)) /-- The canonical map from the underlying set to the prime spectrum of `Γ(X)`. -/ def to_Γ_Spec_fun : X → prime_spectrum (Γ.obj (op X)) := λ x, comap (X.Γ_to_stalk x) (local_ring.closed_point (X.presheaf.stalk x)) lemma not_mem_prime_iff_unit_in_stalk (r : Γ.obj (op X)) (x : X) : r ∉ (X.to_Γ_Spec_fun x).as_ideal ↔ is_unit (X.Γ_to_stalk x r) := by erw [local_ring.mem_maximal_ideal, not_not] /-- The preimage of a basic open in `Spec Γ(X)` under the unit is the basic open in `X` defined by the same element (they are equal as sets). -/ lemma to_Γ_Spec_preim_basic_open_eq (r : Γ.obj (op X)) : X.to_Γ_Spec_fun⁻¹' (basic_open r).1 = (X.to_RingedSpace.basic_open r).1 := by { ext, erw X.to_RingedSpace.mem_top_basic_open, apply not_mem_prime_iff_unit_in_stalk } /-- `to_Γ_Spec_fun` is continuous. -/ lemma to_Γ_Spec_continuous : continuous X.to_Γ_Spec_fun := begin apply is_topological_basis_basic_opens.continuous, rintro _ ⟨r, rfl⟩, erw X.to_Γ_Spec_preim_basic_open_eq r, exact (X.to_RingedSpace.basic_open r).2, end /-- The canonical (bundled) continuous map from the underlying topological space of `X` to the prime spectrum of its global sections. -/ @[simps] def to_Γ_Spec_base : X.to_Top ⟶ Spec.Top_obj (Γ.obj (op X)) := { to_fun := X.to_Γ_Spec_fun, continuous_to_fun := X.to_Γ_Spec_continuous } variable (r : Γ.obj (op X)) /-- The preimage in `X` of a basic open in `Spec Γ(X)` (as an open set). -/ abbreviation to_Γ_Spec_map_basic_open : opens X := (opens.map X.to_Γ_Spec_base).obj (basic_open r) /-- The preimage is the basic open in `X` defined by the same element `r`. -/ lemma to_Γ_Spec_map_basic_open_eq : X.to_Γ_Spec_map_basic_open r = X.to_RingedSpace.basic_open r := subtype.eq (X.to_Γ_Spec_preim_basic_open_eq r) /-- The map from the global sections `Γ(X)` to the sections on the (preimage of) a basic open. -/ abbreviation to_to_Γ_Spec_map_basic_open : X.presheaf.obj (op ⊤) ⟶ X.presheaf.obj (op $ X.to_Γ_Spec_map_basic_open r) := X.presheaf.map (X.to_Γ_Spec_map_basic_open r).le_top.op /-- `r` is a unit as a section on the basic open defined by `r`. -/ lemma is_unit_res_to_Γ_Spec_map_basic_open : is_unit (X.to_to_Γ_Spec_map_basic_open r r) := begin convert (X.presheaf.map $ (eq_to_hom $ X.to_Γ_Spec_map_basic_open_eq r).op) .is_unit_map (X.to_RingedSpace.is_unit_res_basic_open r), rw ← comp_apply, erw ← functor.map_comp, congr end /-- Define the sheaf hom on individual basic opens for the unit. -/ def to_Γ_Spec_c_app : (structure_sheaf $ Γ.obj $ op X).val.obj (op $ basic_open r) ⟶ X.presheaf.obj (op $ X.to_Γ_Spec_map_basic_open r) := is_localization.away.lift r (is_unit_res_to_Γ_Spec_map_basic_open _ r) /-- Characterization of the sheaf hom on basic opens, direction ← (next lemma) is used at various places, but → is not used in this file. -/ lemma to_Γ_Spec_c_app_iff (f : (structure_sheaf $ Γ.obj $ op X).val.obj (op $ basic_open r) ⟶ X.presheaf.obj (op $ X.to_Γ_Spec_map_basic_open r)) : to_open _ (basic_open r) ≫ f = X.to_to_Γ_Spec_map_basic_open r ↔ f = X.to_Γ_Spec_c_app r := begin rw ← (is_localization.away.away_map.lift_comp r (X.is_unit_res_to_Γ_Spec_map_basic_open r)), swap 5, exact is_localization.to_basic_open _ r, split, { intro h, refine is_localization.ring_hom_ext _ _, swap 5, exact is_localization.to_basic_open _ r, exact h }, apply congr_arg, end lemma to_Γ_Spec_c_app_spec : to_open _ (basic_open r) ≫ X.to_Γ_Spec_c_app r = X.to_to_Γ_Spec_map_basic_open r := (X.to_Γ_Spec_c_app_iff r _).2 rfl /-- The sheaf hom on all basic opens, commuting with restrictions. -/ def to_Γ_Spec_c_basic_opens : (induced_functor basic_open).op ⋙ (structure_sheaf (Γ.obj (op X))).1 ⟶ (induced_functor basic_open).op ⋙ ((Top.sheaf.pushforward X.to_Γ_Spec_base).obj X.𝒪).1 := { app := λ r, X.to_Γ_Spec_c_app r.unop, naturality' := λ r s f, begin apply (structure_sheaf.to_basic_open_epi (Γ.obj (op X)) r.unop).1, simp only [← category.assoc], erw X.to_Γ_Spec_c_app_spec r.unop, convert X.to_Γ_Spec_c_app_spec s.unop, symmetry, apply X.presheaf.map_comp end } /-- The canonical morphism of sheafed spaces from `X` to the spectrum of its global sections. -/ @[simps] def to_Γ_Spec_SheafedSpace : X.to_SheafedSpace ⟶ Spec.to_SheafedSpace.obj (op (Γ.obj (op X))) := { base := X.to_Γ_Spec_base, c := Top.sheaf.restrict_hom_equiv_hom (structure_sheaf (Γ.obj (op X))).1 _ is_basis_basic_opens X.to_Γ_Spec_c_basic_opens } lemma to_Γ_Spec_SheafedSpace_app_eq : X.to_Γ_Spec_SheafedSpace.c.app (op (basic_open r)) = X.to_Γ_Spec_c_app r := Top.sheaf.extend_hom_app _ _ _ _ _ lemma to_Γ_Spec_SheafedSpace_app_spec (r : Γ.obj (op X)) : to_open _ (basic_open r) ≫ X.to_Γ_Spec_SheafedSpace.c.app (op (basic_open r)) = X.to_to_Γ_Spec_map_basic_open r := (X.to_Γ_Spec_SheafedSpace_app_eq r).symm ▸ X.to_Γ_Spec_c_app_spec r /-- The map on stalks induced by the unit commutes with maps from `Γ(X)` to stalks (in `Spec Γ(X)` and in `X`). -/ lemma to_stalk_stalk_map_to_Γ_Spec (x : X) : to_stalk _ _ ≫ PresheafedSpace.stalk_map X.to_Γ_Spec_SheafedSpace x = X.Γ_to_stalk x := begin rw PresheafedSpace.stalk_map, erw ← to_open_germ _ (basic_open (1 : Γ.obj (op X))) ⟨X.to_Γ_Spec_fun x, by rw basic_open_one; trivial⟩, rw [← category.assoc, category.assoc (to_open _ _)], erw stalk_functor_map_germ, rw [← category.assoc (to_open _ _), X.to_Γ_Spec_SheafedSpace_app_spec 1], unfold Γ_to_stalk, rw ← stalk_pushforward_germ _ X.to_Γ_Spec_base X.presheaf ⊤, congr' 1, change (X.to_Γ_Spec_base _* X.presheaf).map le_top.hom.op ≫ _ = _, apply germ_res, end /-- The canonical morphism from `X` to the spectrum of its global sections. -/ @[simps val_base] def to_Γ_Spec : X ⟶ Spec.LocallyRingedSpace_obj (Γ.obj (op X)) := { val := X.to_Γ_Spec_SheafedSpace, prop := begin intro x, let p : prime_spectrum (Γ.obj (op X)) := X.to_Γ_Spec_fun x, constructor, /- show stalk map is local hom ↓ -/ let S := (structure_sheaf _).presheaf.stalk p, rintros (t : S) ht, obtain ⟨⟨r, s⟩, he⟩ := is_localization.surj p.as_ideal.prime_compl t, dsimp at he, apply is_unit_of_mul_is_unit_left, rw he, refine is_localization.map_units S (⟨r, _⟩ : p.as_ideal.prime_compl), apply (not_mem_prime_iff_unit_in_stalk _ _ _).mpr, rw [← to_stalk_stalk_map_to_Γ_Spec, comp_apply], erw ← he, rw ring_hom.map_mul, exact ht.mul ((is_localization.map_units S s : _).map (PresheafedSpace.stalk_map X.to_Γ_Spec_SheafedSpace x)) end } lemma comp_ring_hom_ext {X : LocallyRingedSpace} {R : CommRing} {f : R ⟶ Γ.obj (op X)} {β : X ⟶ Spec.LocallyRingedSpace_obj R} (w : X.to_Γ_Spec.1.base ≫ (Spec.LocallyRingedSpace_map f).1.base = β.1.base) (h : ∀ r : R, f ≫ X.presheaf.map (hom_of_le le_top : (opens.map β.1.base).obj (basic_open r) ⟶ _).op = to_open R (basic_open r) ≫ β.1.c.app (op (basic_open r))) : X.to_Γ_Spec ≫ Spec.LocallyRingedSpace_map f = β := begin ext1, apply Spec.basic_open_hom_ext, { intros r _, rw LocallyRingedSpace.comp_val_c_app, erw to_open_comp_comap_assoc, rw category.assoc, erw [to_Γ_Spec_SheafedSpace_app_spec, ← X.presheaf.map_comp], convert h r }, exact w, end /-- `to_Spec_Γ _` is an isomorphism so these are mutually two-sided inverses. -/ lemma Γ_Spec_left_triangle : to_Spec_Γ (Γ.obj (op X)) ≫ X.to_Γ_Spec.1.c.app (op ⊤) = 𝟙 _ := begin unfold to_Spec_Γ, rw ← to_open_res _ (basic_open (1 : Γ.obj (op X))) ⊤ (eq_to_hom basic_open_one.symm), erw category.assoc, rw [nat_trans.naturality, ← category.assoc], erw [X.to_Γ_Spec_SheafedSpace_app_spec 1, ← functor.map_comp], convert eq_to_hom_map X.presheaf _, refl, end end LocallyRingedSpace /-- The unit as a natural transformation. -/ def identity_to_Γ_Spec : 𝟭 LocallyRingedSpace.{u} ⟶ Γ.right_op ⋙ Spec.to_LocallyRingedSpace := { app := LocallyRingedSpace.to_Γ_Spec, naturality' := λ X Y f, begin symmetry, apply LocallyRingedSpace.comp_ring_hom_ext, { ext1 x, dsimp [Spec.Top_map, LocallyRingedSpace.to_Γ_Spec_fun], rw [← local_ring.comap_closed_point (PresheafedSpace.stalk_map _ x), ← prime_spectrum.comap_comp_apply, ← prime_spectrum.comap_comp_apply], congr' 2, exact (PresheafedSpace.stalk_map_germ f.1 ⊤ ⟨x,trivial⟩).symm, apply_instance }, { intro r, rw [LocallyRingedSpace.comp_val_c_app, ← category.assoc], erw [Y.to_Γ_Spec_SheafedSpace_app_spec, f.1.c.naturality], refl }, end } namespace Γ_Spec lemma left_triangle (X : LocallyRingedSpace) : Spec_Γ_identity.inv.app (Γ.obj (op X)) ≫ (identity_to_Γ_Spec.app X).val.c.app (op ⊤) = 𝟙 _ := X.Γ_Spec_left_triangle /-- `Spec_Γ_identity` is iso so these are mutually two-sided inverses. -/ lemma right_triangle (R : CommRing) : identity_to_Γ_Spec.app (Spec.to_LocallyRingedSpace.obj $ op R) ≫ Spec.to_LocallyRingedSpace.map (Spec_Γ_identity.inv.app R).op = 𝟙 _ := begin apply LocallyRingedSpace.comp_ring_hom_ext, { ext (p : prime_spectrum R) x, erw ← is_localization.at_prime.to_map_mem_maximal_iff ((structure_sheaf R).presheaf.stalk p) p.as_ideal x, refl }, { intro r, apply to_open_res }, end -- Removing this makes the following definition time out. local attribute [irreducible] Spec_Γ_identity identity_to_Γ_Spec Spec.to_LocallyRingedSpace /-- The adjunction `Γ ⊣ Spec` from `CommRingᵒᵖ` to `LocallyRingedSpace`. -/ @[simps unit counit] def LocallyRingedSpace_adjunction : Γ.right_op ⊣ Spec.to_LocallyRingedSpace := adjunction.mk_of_unit_counit { unit := identity_to_Γ_Spec, counit := (nat_iso.op Spec_Γ_identity).inv, left_triangle' := by { ext X, erw category.id_comp, exact congr_arg quiver.hom.op (left_triangle X) }, right_triangle' := by { ext1, ext1 R, erw category.id_comp, exact right_triangle R.unop } } local attribute [semireducible] Spec.to_LocallyRingedSpace /-- The adjunction `Γ ⊣ Spec` from `CommRingᵒᵖ` to `Scheme`. -/ def adjunction : Scheme.Γ.right_op ⊣ Scheme.Spec := LocallyRingedSpace_adjunction.restrict_fully_faithful Scheme.forget_to_LocallyRingedSpace (𝟭 _) (nat_iso.of_components (λ X, iso.refl _) (λ _ _ f, by simpa)) (nat_iso.of_components (λ X, iso.refl _) (λ _ _ f, by simpa)) lemma adjunction_hom_equiv_apply {X : Scheme} {R : CommRingᵒᵖ} (f : (op $ Scheme.Γ.obj $ op X) ⟶ R) : Γ_Spec.adjunction.hom_equiv X R f = LocallyRingedSpace_adjunction.hom_equiv X.1 R f := by { dsimp [adjunction, adjunction.restrict_fully_faithful], simp } local attribute [irreducible] LocallyRingedSpace_adjunction Γ_Spec.adjunction lemma adjunction_hom_equiv (X : Scheme) (R : CommRingᵒᵖ) : Γ_Spec.adjunction.hom_equiv X R = LocallyRingedSpace_adjunction.hom_equiv X.1 R := equiv.ext $ λ f, adjunction_hom_equiv_apply f lemma adjunction_hom_equiv_symm_apply {X : Scheme} {R : CommRingᵒᵖ} (f : X ⟶ Scheme.Spec.obj R) : (Γ_Spec.adjunction.hom_equiv X R).symm f = (LocallyRingedSpace_adjunction.hom_equiv X.1 R).symm f := by { congr' 2, exact adjunction_hom_equiv _ _ } @[simp] lemma adjunction_counit_app {R : CommRingᵒᵖ} : Γ_Spec.adjunction.counit.app R = LocallyRingedSpace_adjunction.counit.app R := by { rw [← adjunction.hom_equiv_symm_id, ← adjunction.hom_equiv_symm_id, adjunction_hom_equiv_symm_apply], refl } @[simp] lemma adjunction_unit_app {X : Scheme} : Γ_Spec.adjunction.unit.app X = LocallyRingedSpace_adjunction.unit.app X.1 := by { rw [← adjunction.hom_equiv_id, ← adjunction.hom_equiv_id, adjunction_hom_equiv_apply], refl } local attribute [semireducible] LocallyRingedSpace_adjunction Γ_Spec.adjunction instance is_iso_LocallyRingedSpace_adjunction_counit : is_iso LocallyRingedSpace_adjunction.counit := is_iso.of_iso_inv _ instance is_iso_adjunction_counit : is_iso Γ_Spec.adjunction.counit := begin apply_with nat_iso.is_iso_of_is_iso_app { instances := ff }, intro R, rw adjunction_counit_app, apply_instance, end -- This is just -- `(Γ_Spec.adjunction.unit.app X).1.c.app (op ⊤) = Spec_Γ_identity.hom.app (X.presheaf.obj (op ⊤))` -- But lean times out when trying to unify the types of the two sides. lemma adjunction_unit_app_app_top (X : Scheme) : @eq ((Scheme.Spec.obj (op $ X.presheaf.obj (op ⊤))).presheaf.obj (op ⊤) ⟶ ((Γ_Spec.adjunction.unit.app X).1.base _* X.presheaf).obj (op ⊤)) ((Γ_Spec.adjunction.unit.app X).val.c.app (op ⊤)) (Spec_Γ_identity.hom.app (X.presheaf.obj (op ⊤))) := begin have := congr_app Γ_Spec.adjunction.left_triangle X, dsimp at this, rw ← is_iso.eq_comp_inv at this, simp only [Γ_Spec.LocallyRingedSpace_adjunction_counit, nat_trans.op_app, category.id_comp, Γ_Spec.adjunction_counit_app] at this, rw [← op_inv, nat_iso.inv_inv_app, quiver.hom.op_inj.eq_iff] at this, exact this end end Γ_Spec /-! Immediate consequences of the adjunction. -/ /-- Spec preserves limits. -/ instance : limits.preserves_limits Spec.to_LocallyRingedSpace := Γ_Spec.LocallyRingedSpace_adjunction.right_adjoint_preserves_limits instance Spec.preserves_limits : limits.preserves_limits Scheme.Spec := Γ_Spec.adjunction.right_adjoint_preserves_limits /-- Spec is a full functor. -/ instance : full Spec.to_LocallyRingedSpace := R_full_of_counit_is_iso Γ_Spec.LocallyRingedSpace_adjunction instance Spec.full : full Scheme.Spec := R_full_of_counit_is_iso Γ_Spec.adjunction /-- Spec is a faithful functor. -/ instance : faithful Spec.to_LocallyRingedSpace := R_faithful_of_counit_is_iso Γ_Spec.LocallyRingedSpace_adjunction instance Spec.faithful : faithful Scheme.Spec := R_faithful_of_counit_is_iso Γ_Spec.adjunction instance : is_right_adjoint Spec.to_LocallyRingedSpace := ⟨_, Γ_Spec.LocallyRingedSpace_adjunction⟩ instance : is_right_adjoint Scheme.Spec := ⟨_, Γ_Spec.adjunction⟩ instance : reflective Spec.to_LocallyRingedSpace := ⟨⟩ instance Spec.reflective : reflective Scheme.Spec := ⟨⟩ end algebraic_geometry
32f1cddedb9a0b383fdc7b00f08ec4181a2c6f9f
ebb7367fa8ab324601b5abf705720fd4cc0e8598
/homotopy/spherical_fibrations.hlean
a53c6ea4ab80b586862c62fe6afb61b53d871ccf
[ "Apache-2.0" ]
permissive
radams78/Spectral
3e34916d9bbd0939ee6a629e36744827ff27bfc2
c8145341046cfa2b4960ef3cc5a1117d12c43f63
refs/heads/master
1,610,421,583,830
1,481,232,014,000
1,481,232,014,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,795
hlean
import homotopy.join homotopy.smash open eq equiv trunc function bool join sphere sphere_index sphere.ops prod open pointed sigma smash is_trunc namespace spherical_fibrations /- classifying type of spherical fibrations -/ definition BG (n : ℕ) : Type₁ := Σ(X : Type₀), ∥ X ≃ S n..-1 ∥ definition pointed_BG [instance] [constructor] (n : ℕ) : pointed (BG n) := pointed.mk ⟨ S n..-1 , tr erfl ⟩ definition pBG [constructor] (n : ℕ) : Type* := pointed.mk' (BG n) definition G (n : ℕ) : Type₁ := pt = pt :> BG n definition G_char (n : ℕ) : G n ≃ (S n..-1 ≃ S n..-1) := calc G n ≃ Σ(p : S n..-1 = S n..-1), _ : sigma_eq_equiv ... ≃ (S n..-1 = S n..-1) : sigma_equiv_of_is_contr_right ... ≃ (S n..-1 ≃ S n..-1) : eq_equiv_equiv definition mirror (n : ℕ) : S n..-1 → G n := begin intro v, apply to_inv (G_char n), exact sorry end /- Can we give a fibration P : S n → Type, P base = F n = Ω(BF n) = (S. n ≃* S. n) and total space sigma P ≃ G (n+1) = Ω(BG (n+1)) = (S n.+1 ≃ S .n+1) Yes, let eval : BG (n+1) → S n be the evaluation map -/ definition S_of_BG (n : ℕ) : Ω(pBG (n+1)) → S n := λ f, f..1 ▸ base definition BG_succ (n : ℕ) : BG n → BG (n+1) := begin intro X, cases X with X p, apply sigma.mk (susp X), induction p with f, apply tr, apply susp.equiv f end /- classifying type of pointed spherical fibrations -/ definition BF (n : ℕ) : Type₁ := Σ(X : Type*), ∥ X ≃* S* n ∥ definition pointed_BF [instance] [constructor] (n : ℕ) : pointed (BF n) := pointed.mk ⟨ S* n , tr pequiv.rfl ⟩ definition pBF [constructor] (n : ℕ) : Type* := pointed.mk' (BF n) definition BF_succ (n : ℕ) : BF n → BF (n+1) := begin intro X, cases X with X p, apply sigma.mk (psusp X), induction p with f, apply tr, apply susp.psusp_equiv f end definition BF_of_BG {n : ℕ} : BG n → BF n := begin intro X, cases X with X p, apply sigma.mk (pointed.MK (susp X) susp.north), induction p with f, apply tr, apply pequiv_of_equiv (susp.equiv f), reflexivity end definition BG_of_BF {n : ℕ} : BF n → BG (n + 1) := begin intro X, cases X with X hX, apply sigma.mk (carrier X), induction hX with fX, apply tr, exact fX end definition BG_mul {n m : ℕ} (X : BG n) (Y : BG m) : BG (n + m) := begin cases X with X pX, cases Y with Y pY, apply sigma.mk (join X Y), induction pX with fX, induction pY with fY, apply tr, rewrite add_sub_one, exact (join.equiv_closed fX fY) ⬝e (join.spheres n..-1 m..-1) end definition BF_mul {n m : ℕ} (X : BF n) (Y : BF m) : BF (n + m) := begin cases X with X hX, cases Y with Y hY, apply sigma.mk (smash X Y), induction hX with fX, induction hY with fY, apply tr, exact sorry -- needs smash.spheres : psmash (S. n) (S. m) ≃ S. (n + m) end definition BF_of_BG_mul (n m : ℕ) (X : BG n) (Y : BG m) : BF_of_BG (BG_mul X Y) = BF_mul (BF_of_BG X) (BF_of_BG Y) := sorry -- Thom spaces namespace thom variables {X : Type} {n : ℕ} (α : X → BF n) -- the canonical section of an F-object protected definition sec (x : X) : carrier (sigma.pr1 (α x)) := Point _ open pushout sigma definition thom_space : Type := pushout (λx : X, ⟨x , thom.sec α x⟩) (const X unit.star) end thom /- Things to do: - Orientability and orientations * Thom class u ∈ ~Hⁿ(Tξ) * eventually prove Thom-Isomorphism (Rudyak IV.5.7) - define BG∞ and BF∞ as colimits of BG n and BF n - Ω(BF n) = ΩⁿSⁿ₁ + ΩⁿSⁿ₋₁ (self-maps of degree ±1) - succ_BF n is (n - 2) connected (from Freudenthal) - pfiber (BG_of_BF n) ≃* S. n - π₁(BF n)=π₁(BG n)=ℤ/2ℤ - double covers BSG and BSF - O : BF n → BG 1 = Σ(A : Type), ∥ A = bool ∥ - BSG n = sigma O - π₁(BSG n)=π₁(BSF n)=O - BSO(n), - find BF' n : Type₀ with BF' n ≃ BF n etc. - canonical bundle γₙ : ℝP(n) → ℝP∞=BO(1) → Type₀ prove T(γₙ) = ℝP(n+1) - BG∞ = BF∞ (in fact = BGL₁(S), the group of units of the sphere spectrum) - clutching construction: any f : S n → SG(n) gives S n.+1 → BSG(n) (mut.mut. for O(n),SO(n),etc.) - all bundles on S 3 are trivial, incl. tangent bundle - Adams' result on vector fields on spheres: there are maximally ρ(n)-1 indep.sections of the tangent bundle of S (n-1) where ρ(n) is the n'th Radon-Hurwitz number.→ -/ -- tangent bundle on S 2: namespace two_sphere definition tau : S 2 → BG 2 := begin intro v, induction v with x, do 2 exact pt, exact sorry end end two_sphere end spherical_fibrations
b3c883eed080b4cfb4ee08596ccc7e14707129ef
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/polynomial/monic.lean
ef120b0243466dc13f8f4e7d19609fa3f8b799c0
[ "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
17,098
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.reverse import algebra.regular.smul /-! # Theory of monic polynomials We give several tools for proving that polynomials are monic, e.g. `monic.mul`, `monic.map`, `monic.pow`. -/ noncomputable theory open finset open_locale big_operators classical polynomial namespace polynomial universes u v y variables {R : Type u} {S : Type v} {a b : R} {m n : ℕ} {ι : Type y} section semiring variables [semiring R] {p q r : R[X]} lemma monic_zero_iff_subsingleton : monic (0 : R[X]) ↔ subsingleton R := subsingleton_iff_zero_eq_one lemma not_monic_zero_iff : ¬ monic (0 : R[X]) ↔ (0 : R) ≠ 1 := (monic_zero_iff_subsingleton.trans subsingleton_iff_zero_eq_one.symm).not lemma monic_zero_iff_subsingleton' : monic (0 : R[X]) ↔ (∀ f g : R[X], f = g) ∧ (∀ a b : R, a = b) := polynomial.monic_zero_iff_subsingleton.trans ⟨by { introI, simp }, λ h, subsingleton_iff.mpr h.2⟩ lemma monic.as_sum (hp : p.monic) : p = X^(p.nat_degree) + (∑ i in range p.nat_degree, C (p.coeff i) * X^i) := begin conv_lhs { rw [p.as_sum_range_C_mul_X_pow, sum_range_succ_comm] }, suffices : C (p.coeff p.nat_degree) = 1, { rw [this, one_mul] }, exact congr_arg C hp end lemma ne_zero_of_ne_zero_of_monic (hp : p ≠ 0) (hq : monic q) : q ≠ 0 := begin rintro rfl, rw [monic.def, leading_coeff_zero] at hq, rw [← mul_one p, ← C_1, ← hq, C_0, mul_zero] at hp, exact hp rfl end lemma monic.map [semiring S] (f : R →+* S) (hp : monic p) : monic (p.map f) := begin nontriviality, have : f (leading_coeff p) ≠ 0, { rw [show _ = _, from hp, f.map_one], exact one_ne_zero, }, rw [monic, leading_coeff, coeff_map], suffices : p.coeff (map f p).nat_degree = 1, { simp [this], }, rwa nat_degree_eq_of_degree_eq (degree_map_eq_of_leading_coeff_ne_zero f this), end lemma monic_C_mul_of_mul_leading_coeff_eq_one {b : R} (hp : b * p.leading_coeff = 1) : monic (C b * p) := by { nontriviality, rw [monic, leading_coeff_mul' _]; simp [leading_coeff_C b, hp] } lemma monic_mul_C_of_leading_coeff_mul_eq_one {b : R} (hp : p.leading_coeff * b = 1) : monic (p * C b) := by { nontriviality, rw [monic, leading_coeff_mul' _]; simp [leading_coeff_C b, hp] } theorem monic_of_degree_le (n : ℕ) (H1 : degree p ≤ n) (H2 : coeff p n = 1) : monic p := decidable.by_cases (assume H : degree p < n, eq_of_zero_eq_one (H2 ▸ (coeff_eq_zero_of_degree_lt H).symm) _ _) (assume H : ¬degree p < n, by rwa [monic, leading_coeff, nat_degree, (lt_or_eq_of_le H1).resolve_left H]) theorem monic_X_pow_add {n : ℕ} (H : degree p ≤ n) : monic (X ^ (n+1) + p) := have H1 : degree p < n+1, from lt_of_le_of_lt H (with_bot.coe_lt_coe.2 (nat.lt_succ_self n)), monic_of_degree_le (n+1) (le_trans (degree_add_le _ _) (max_le (degree_X_pow_le _) (le_of_lt H1))) (by rw [coeff_add, coeff_X_pow, if_pos rfl, coeff_eq_zero_of_degree_lt H1, add_zero]) theorem monic_X_add_C (x : R) : monic (X + C x) := pow_one (X : R[X]) ▸ monic_X_pow_add degree_C_le lemma monic.mul (hp : monic p) (hq : monic q) : monic (p * q) := if h0 : (0 : R) = 1 then by haveI := subsingleton_of_zero_eq_one h0; exact subsingleton.elim _ _ else have leading_coeff p * leading_coeff q ≠ 0, by simp [monic.def.1 hp, monic.def.1 hq, ne.symm h0], by rw [monic.def, leading_coeff_mul' this, monic.def.1 hp, monic.def.1 hq, one_mul] lemma monic.pow (hp : monic p) : ∀ (n : ℕ), monic (p ^ n) | 0 := monic_one | (n+1) := by { rw pow_succ, exact hp.mul (monic.pow n) } lemma monic.add_of_left (hp : monic p) (hpq : degree q < degree p) : monic (p + q) := by rwa [monic, add_comm, leading_coeff_add_of_degree_lt hpq] lemma monic.add_of_right (hq : monic q) (hpq : degree p < degree q) : monic (p + q) := by rwa [monic, leading_coeff_add_of_degree_lt hpq] lemma monic.of_mul_monic_left (hp : p.monic) (hpq : (p * q).monic) : q.monic := begin contrapose! hpq, rw monic.def at hpq ⊢, rwa leading_coeff_monic_mul hp, end lemma monic.of_mul_monic_right (hq : q.monic) (hpq : (p * q).monic) : p.monic := begin contrapose! hpq, rw monic.def at hpq ⊢, rwa leading_coeff_mul_monic hq, end namespace monic @[simp] lemma nat_degree_eq_zero_iff_eq_one {p : R[X]} (hp : p.monic) : p.nat_degree = 0 ↔ p = 1 := begin split; intro h, swap, { rw h, exact nat_degree_one }, have : p = C (p.coeff 0), { rw ← polynomial.degree_le_zero_iff, rwa polynomial.nat_degree_eq_zero_iff_degree_le_zero at h }, rw this, convert C_1, rw ← h, apply hp, end @[simp] lemma degree_le_zero_iff_eq_one {p : R[X]} (hp : p.monic) : p.degree ≤ 0 ↔ p = 1 := by rw [←hp.nat_degree_eq_zero_iff_eq_one, nat_degree_eq_zero_iff_degree_le_zero] lemma nat_degree_mul {p q : R[X]} (hp : p.monic) (hq : q.monic) : (p * q).nat_degree = p.nat_degree + q.nat_degree := begin nontriviality R, apply nat_degree_mul', simp [hp.leading_coeff, hq.leading_coeff] end lemma degree_mul_comm {p : R[X]} (hp : p.monic) (q : R[X]) : (p * q).degree = (q * p).degree := begin by_cases h : q = 0, { simp [h] }, rw [degree_mul', hp.degree_mul], { exact add_comm _ _ }, { rwa [hp.leading_coeff, one_mul, leading_coeff_ne_zero] } end lemma nat_degree_mul' {p q : R[X]} (hp : p.monic) (hq : q ≠ 0) : (p * q).nat_degree = p.nat_degree + q.nat_degree := begin rw [nat_degree_mul', add_comm], simpa [hp.leading_coeff, leading_coeff_ne_zero] end lemma nat_degree_mul_comm {p : R[X]} (hp : p.monic) (q : R[X]) : (p * q).nat_degree = (q * p).nat_degree := begin by_cases h : q = 0, { simp [h] }, rw [hp.nat_degree_mul' h, polynomial.nat_degree_mul', add_comm], simpa [hp.leading_coeff, leading_coeff_ne_zero] end lemma next_coeff_mul {p q : R[X]} (hp : monic p) (hq : monic q) : next_coeff (p * q) = next_coeff p + next_coeff q := begin nontriviality, simp only [← coeff_one_reverse], rw reverse_mul; simp [coeff_mul, nat.antidiagonal, hp.leading_coeff, hq.leading_coeff, add_comm] end lemma eq_one_of_map_eq_one {S : Type*} [semiring S] [nontrivial S] (f : R →+* S) (hp : p.monic) (map_eq : p.map f = 1) : p = 1 := begin nontriviality R, have hdeg : p.degree = 0, { rw [← degree_map_eq_of_leading_coeff_ne_zero f _, map_eq, degree_one], { rw [hp.leading_coeff, f.map_one], exact one_ne_zero } }, have hndeg : p.nat_degree = 0 := with_bot.coe_eq_coe.mp ((degree_eq_nat_degree hp.ne_zero).symm.trans hdeg), convert eq_C_of_degree_eq_zero hdeg, rw [← hndeg, ← polynomial.leading_coeff, hp.leading_coeff, C.map_one] end lemma nat_degree_pow (hp : p.monic) (n : ℕ) : (p ^ n).nat_degree = n * p.nat_degree := begin induction n with n hn, { simp }, { rw [pow_succ, hp.nat_degree_mul (hp.pow n), hn], ring } end end monic @[simp] lemma nat_degree_pow_X_add_C [nontrivial R] (n : ℕ) (r : R) : ((X + C r) ^ n).nat_degree = n := by rw [(monic_X_add_C r).nat_degree_pow, nat_degree_X_add_C, mul_one] lemma monic.eq_one_of_is_unit (hm : monic p) (hpu : is_unit p) : p = 1 := begin nontriviality R, obtain ⟨q, h⟩ := hpu.exists_right_inv, have := hm.nat_degree_mul' (right_ne_zero_of_mul_eq_one h), rw [h, nat_degree_one, eq_comm, add_eq_zero_iff] at this, exact hm.nat_degree_eq_zero_iff_eq_one.mp this.1, end end semiring section comm_semiring variables [comm_semiring R] {p : R[X]} lemma monic_multiset_prod_of_monic (t : multiset ι) (f : ι → R[X]) (ht : ∀ i ∈ t, monic (f i)) : monic (t.map f).prod := begin revert ht, refine t.induction_on _ _, { simp }, intros a t ih ht, rw [multiset.map_cons, multiset.prod_cons], exact (ht _ (multiset.mem_cons_self _ _)).mul (ih (λ _ hi, ht _ (multiset.mem_cons_of_mem hi))) end lemma monic_prod_of_monic (s : finset ι) (f : ι → R[X]) (hs : ∀ i ∈ s, monic (f i)) : monic (∏ i in s, f i) := monic_multiset_prod_of_monic s.1 f hs lemma monic.next_coeff_multiset_prod (t : multiset ι) (f : ι → R[X]) (h : ∀ i ∈ t, monic (f i)) : next_coeff (t.map f).prod = (t.map (λ i, next_coeff (f i))).sum := begin revert h, refine multiset.induction_on t _ (λ a t ih ht, _), { simp only [multiset.not_mem_zero, forall_prop_of_true, forall_prop_of_false, multiset.map_zero, multiset.prod_zero, multiset.sum_zero, not_false_iff, forall_true_iff], rw ← C_1, rw next_coeff_C_eq_zero }, { rw [multiset.map_cons, multiset.prod_cons, multiset.map_cons, multiset.sum_cons, monic.next_coeff_mul, ih], exacts [λ i hi, ht i (multiset.mem_cons_of_mem hi), ht a (multiset.mem_cons_self _ _), monic_multiset_prod_of_monic _ _ (λ b bs, ht _ (multiset.mem_cons_of_mem bs))] } end lemma monic.next_coeff_prod (s : finset ι) (f : ι → R[X]) (h : ∀ i ∈ s, monic (f i)) : next_coeff (∏ i in s, f i) = ∑ i in s, next_coeff (f i) := monic.next_coeff_multiset_prod s.1 f h end comm_semiring section semiring variables [semiring R] @[simp] lemma monic.nat_degree_map [semiring S] [nontrivial S] {P : R[X]} (hmo : P.monic) (f : R →+* S) : (P.map f).nat_degree = P.nat_degree := begin refine le_antisymm (nat_degree_map_le _ _) (le_nat_degree_of_ne_zero _), rw [coeff_map, monic.coeff_nat_degree hmo, ring_hom.map_one], exact one_ne_zero end @[simp] lemma monic.degree_map [semiring S] [nontrivial S] {P : R[X]} (hmo : P.monic) (f : R →+* S) : (P.map f).degree = P.degree := begin by_cases hP : P = 0, { simp [hP] }, { refine le_antisymm (degree_map_le _ _) _, rw [degree_eq_nat_degree hP], refine le_degree_of_ne_zero _, rw [coeff_map, monic.coeff_nat_degree hmo, ring_hom.map_one], exact one_ne_zero } end section injective open function variables [semiring S] {f : R →+* S} (hf : injective f) include hf lemma degree_map_eq_of_injective (p : R[X]) : degree (p.map f) = degree p := if h : p = 0 then by simp [h] else degree_map_eq_of_leading_coeff_ne_zero _ (by rw [← f.map_zero]; exact mt hf.eq_iff.1 (mt leading_coeff_eq_zero.1 h)) lemma nat_degree_map_eq_of_injective (p : R[X]) : nat_degree (p.map f) = nat_degree p := nat_degree_eq_of_degree_eq (degree_map_eq_of_injective hf p) lemma leading_coeff_map' (p : R[X]) : leading_coeff (p.map f) = f (leading_coeff p) := begin unfold leading_coeff, rw [coeff_map, nat_degree_map_eq_of_injective hf p], end lemma next_coeff_map (p : R[X]) : (p.map f).next_coeff = f p.next_coeff := begin unfold next_coeff, rw nat_degree_map_eq_of_injective hf, split_ifs; simp end lemma leading_coeff_of_injective (p : R[X]) : leading_coeff (p.map f) = f (leading_coeff p) := begin delta leading_coeff, rw [coeff_map f, nat_degree_map_eq_of_injective hf p] end lemma monic_of_injective {p : R[X]} (hp : (p.map f).monic) : p.monic := begin apply hf, rw [← leading_coeff_of_injective hf, hp.leading_coeff, f.map_one] end end injective end semiring section ring variables [ring R] {p : R[X]} theorem monic_X_sub_C (x : R) : monic (X - C x) := by simpa only [sub_eq_add_neg, C_neg] using monic_X_add_C (-x) theorem monic_X_pow_sub {n : ℕ} (H : degree p ≤ n) : monic (X ^ (n+1) - p) := by simpa [sub_eq_add_neg] using monic_X_pow_add (show degree (-p) ≤ n, by rwa ←degree_neg p at H) /-- `X ^ n - a` is monic. -/ lemma monic_X_pow_sub_C {R : Type u} [ring R] (a : R) {n : ℕ} (h : n ≠ 0) : (X ^ n - C a).monic := begin obtain ⟨k, hk⟩ := nat.exists_eq_succ_of_ne_zero h, convert monic_X_pow_sub _, exact le_trans degree_C_le nat.with_bot.coe_nonneg, end lemma not_is_unit_X_pow_sub_one (R : Type*) [comm_ring R] [nontrivial R] (n : ℕ) : ¬ is_unit (X ^ n - 1 : R[X]) := begin intro h, rcases eq_or_ne n 0 with rfl | hn, { simpa using h }, apply hn, rw [←@nat_degree_one R, ←(monic_X_pow_sub_C _ hn).eq_one_of_is_unit h, nat_degree_X_pow_sub_C], end lemma monic.sub_of_left {p q : R[X]} (hp : monic p) (hpq : degree q < degree p) : monic (p - q) := by { rw sub_eq_add_neg, apply hp.add_of_left, rwa degree_neg } lemma monic.sub_of_right {p q : R[X]} (hq : q.leading_coeff = -1) (hpq : degree p < degree q) : monic (p - q) := have (-q).coeff (-q).nat_degree = 1 := by rw [nat_degree_neg, coeff_neg, show q.coeff q.nat_degree = -1, from hq, neg_neg], by { rw sub_eq_add_neg, apply monic.add_of_right this, rwa degree_neg } end ring section nonzero_semiring variables [semiring R] [nontrivial R] {p q : R[X]} @[simp] lemma not_monic_zero : ¬monic (0 : R[X]) := not_monic_zero_iff.mp zero_ne_one end nonzero_semiring section not_zero_divisor -- TODO: using gh-8537, rephrase lemmas that involve commutation around `*` using the op-ring variables [semiring R] {p : R[X]} lemma monic.mul_left_ne_zero (hp : monic p) {q : R[X]} (hq : q ≠ 0) : q * p ≠ 0 := begin by_cases h : p = 1, { simpa [h] }, rw [ne.def, ←degree_eq_bot, hp.degree_mul, with_bot.add_eq_bot, not_or_distrib, degree_eq_bot], refine ⟨hq, _⟩, rw [←hp.degree_le_zero_iff_eq_one, not_le] at h, refine (lt_trans _ h).ne', simp end lemma monic.mul_right_ne_zero (hp : monic p) {q : R[X]} (hq : q ≠ 0) : p * q ≠ 0 := begin by_cases h : p = 1, { simpa [h] }, rw [ne.def, ←degree_eq_bot, hp.degree_mul_comm, hp.degree_mul, with_bot.add_eq_bot, not_or_distrib, degree_eq_bot], refine ⟨hq, _⟩, rw [←hp.degree_le_zero_iff_eq_one, not_le] at h, refine (lt_trans _ h).ne', simp end lemma monic.mul_nat_degree_lt_iff (h : monic p) {q : R[X]} : (p * q).nat_degree < p.nat_degree ↔ p ≠ 1 ∧ q = 0 := begin by_cases hq : q = 0, { suffices : 0 < p.nat_degree ↔ p.nat_degree ≠ 0, { simpa [hq, ←h.nat_degree_eq_zero_iff_eq_one] }, exact ⟨λ h, h.ne', λ h, lt_of_le_of_ne (nat.zero_le _) h.symm ⟩ }, { simp [h.nat_degree_mul', hq] } end lemma monic.mul_right_eq_zero_iff (h : monic p) {q : R[X]} : p * q = 0 ↔ q = 0 := begin by_cases hq : q = 0; simp [h.mul_right_ne_zero, hq] end lemma monic.mul_left_eq_zero_iff (h : monic p) {q : R[X]} : q * p = 0 ↔ q = 0 := begin by_cases hq : q = 0; simp [h.mul_left_ne_zero, hq] end lemma monic.is_regular {R : Type*} [ring R] {p : R[X]} (hp : monic p) : is_regular p := begin split, { intros q r h, rw [←sub_eq_zero, ←hp.mul_right_eq_zero_iff, mul_sub, h, sub_self] }, { intros q r h, simp only at h, rw [←sub_eq_zero, ←hp.mul_left_eq_zero_iff, sub_mul, h, sub_self] } end lemma degree_smul_of_smul_regular {S : Type*} [monoid S] [distrib_mul_action S R] {k : S} (p : R[X]) (h : is_smul_regular R k) : (k • p).degree = p.degree := begin refine le_antisymm _ _, { rw degree_le_iff_coeff_zero, intros m hm, rw degree_lt_iff_coeff_zero at hm, simp [hm m le_rfl] }, { rw degree_le_iff_coeff_zero, intros m hm, rw degree_lt_iff_coeff_zero at hm, refine h _, simpa using hm m le_rfl }, end lemma nat_degree_smul_of_smul_regular {S : Type*} [monoid S] [distrib_mul_action S R] {k : S} (p : R[X]) (h : is_smul_regular R k) : (k • p).nat_degree = p.nat_degree := begin by_cases hp : p = 0, { simp [hp] }, rw [←with_bot.coe_eq_coe, ←degree_eq_nat_degree hp, ←degree_eq_nat_degree, degree_smul_of_smul_regular p h], contrapose! hp, rw ←smul_zero k at hp, exact h.polynomial hp end lemma leading_coeff_smul_of_smul_regular {S : Type*} [monoid S] [distrib_mul_action S R] {k : S} (p : R[X]) (h : is_smul_regular R k) : (k • p).leading_coeff = k • p.leading_coeff := by rw [leading_coeff, leading_coeff, coeff_smul, nat_degree_smul_of_smul_regular p h] lemma monic_of_is_unit_leading_coeff_inv_smul (h : is_unit p.leading_coeff) : monic (h.unit⁻¹ • p) := begin rw [monic.def, leading_coeff_smul_of_smul_regular _ (is_smul_regular_of_group _), units.smul_def], obtain ⟨k, hk⟩ := h, simp only [←hk, smul_eq_mul, ←units.coe_mul, units.coe_eq_one, inv_mul_eq_iff_eq_mul], simp [units.ext_iff, is_unit.unit_spec] end lemma is_unit_leading_coeff_mul_right_eq_zero_iff (h : is_unit p.leading_coeff) {q : R[X]} : p * q = 0 ↔ q = 0 := begin split, { intro hp, rw ←smul_eq_zero_iff_eq (h.unit)⁻¹ at hp, have : (h.unit)⁻¹ • (p * q) = ((h.unit)⁻¹ • p) * q, { ext, simp only [units.smul_def, coeff_smul, coeff_mul, smul_eq_mul, mul_sum], refine sum_congr rfl (λ x hx, _), rw ←mul_assoc }, rwa [this, monic.mul_right_eq_zero_iff] at hp, exact monic_of_is_unit_leading_coeff_inv_smul _ }, { rintro rfl, simp } end lemma is_unit_leading_coeff_mul_left_eq_zero_iff (h : is_unit p.leading_coeff) {q : R[X]} : q * p = 0 ↔ q = 0 := begin split, { intro hp, replace hp := congr_arg (* C ↑(h.unit)⁻¹) hp, simp only [zero_mul] at hp, rwa [mul_assoc, monic.mul_left_eq_zero_iff] at hp, refine monic_mul_C_of_leading_coeff_mul_eq_one _, simp [units.mul_inv_eq_iff_eq_mul, is_unit.unit_spec] }, { rintro rfl, rw zero_mul } end end not_zero_divisor end polynomial
167c8c31cd3f6bf3554193815a52f9912111b362
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/group_theory/free_abelian_group.lean
dab1946d3180e42e483f4444d64960c22c47adfc
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
8,414
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Free abelian groups as abelianization of free groups. -/ import algebra.pi_instances import group_theory.free_group import group_theory.abelianization universes u v variables (α : Type u) def free_abelian_group : Type u := additive $ abelianization $ free_group α instance : add_comm_group (free_abelian_group α) := @additive.add_comm_group _ $ abelianization.comm_group _ variable {α} namespace free_abelian_group def of (x : α) : free_abelian_group α := abelianization.of $ free_group.of x def lift {β : Type v} [add_comm_group β] (f : α → β) (x : free_abelian_group α) : β := @abelianization.lift _ _ (multiplicative β) _ (@free_group.to_group _ (multiplicative β) _ f) _ x namespace lift variables {β : Type v} [add_comm_group β] (f : α → β) open free_abelian_group instance is_add_group_hom : is_add_group_hom (lift f) := ⟨λ x y, @is_group_hom.map_mul _ (multiplicative β) _ _ _ (abelianization.lift.is_group_hom _) x y⟩ @[simp] protected lemma add (x y : free_abelian_group α) : lift f (x + y) = lift f x + lift f y := is_add_group_hom.map_add _ _ _ @[simp] protected lemma neg (x : free_abelian_group α) : lift f (-x) = -lift f x := is_add_group_hom.map_neg _ _ @[simp] protected lemma sub (x y : free_abelian_group α) : lift f (x - y) = lift f x - lift f y := by simp @[simp] protected lemma zero : lift f 0 = 0 := is_add_group_hom.map_zero _ @[simp] protected lemma of (x : α) : lift f (of x) = f x := by unfold of; unfold lift; simp protected theorem unique (g : free_abelian_group α → β) [is_add_group_hom g] (hg : ∀ x, g (of x) = f x) {x} : g x = lift f x := @abelianization.lift.unique (free_group α) _ (multiplicative β) _ _ _ g ⟨λ x y, @is_add_group_hom.map_add (additive $ abelianization (free_group α)) _ _ _ _ _ x y⟩ (λ x, @free_group.to_group.unique α (multiplicative β) _ _ (g ∘ abelianization.of) ⟨λ m n, is_add_group_hom.map_add g (abelianization.of m) (abelianization.of n)⟩ hg _) _ protected theorem ext (g h : free_abelian_group α → β) [is_add_group_hom g] [is_add_group_hom h] (H : ∀ x, g (of x) = h (of x)) {x} : g x = h x := (lift.unique (g ∘ of) g (λ _, rfl)).trans $ eq.symm $ lift.unique _ _ $ λ x, eq.symm $ H x lemma map_hom {α β γ} [add_comm_group β] [add_comm_group γ] (a : free_abelian_group α) (f : α → β) (g : β → γ) [is_add_group_hom g] : g (a.lift f) = a.lift (g ∘ f) := show (g ∘ lift f) a = a.lift (g ∘ f), begin apply @lift.unique, assume a, simp only [(∘), lift.of] end def universal : (α → β) ≃ { f : free_abelian_group α → β // is_add_group_hom f } := { to_fun := λ f, ⟨_, lift.is_add_group_hom f⟩, inv_fun := λ f, f.1 ∘ of, left_inv := λ f, funext $ λ x, lift.of f x, right_inv := λ f, subtype.eq $ funext $ λ x, eq.symm $ by letI := f.2; from lift.unique _ _ (λ _, rfl) } end lift local attribute [instance] quotient_group.left_rel normal_subgroup.to_is_subgroup @[elab_as_eliminator] protected theorem induction_on {C : free_abelian_group α → Prop} (z : free_abelian_group α) (C0 : C 0) (C1 : ∀ x, C $ of x) (Cn : ∀ x, C (of x) → C (-of x)) (Cp : ∀ x y, C x → C y → C (x + y)) : C z := quotient.induction_on z $ λ x, quot.induction_on x $ λ L, list.rec_on L C0 $ λ ⟨x, b⟩ tl ih, bool.rec_on b (Cp _ _ (Cn _ (C1 x)) ih) (Cp _ _ (C1 x) ih) instance is_add_group_hom_lift' {α} (β) [add_comm_group β] (a : free_abelian_group α) : is_add_group_hom (λf, (a.lift f : β)) := begin refine ⟨assume f g, free_abelian_group.induction_on a _ _ _ _⟩, { simp [is_add_group_hom.map_zero (free_abelian_group.lift f)] }, { simp [lift.of], assume x, refl }, { simp [is_add_group_hom.map_neg (free_abelian_group.lift f)], assume x h, show - (f x + g x) = -f x + - g x, exact neg_add _ _ }, { simp [is_add_group_hom.map_add (free_abelian_group.lift f)], assume x y hx hy, rw [hx, hy], ac_refl } end variables {β : Type u} instance : monad free_abelian_group.{u} := { pure := λ α, of, bind := λ α β x f, lift f x } @[elab_as_eliminator] protected theorem induction_on' {C : free_abelian_group α → Prop} (z : free_abelian_group α) (C0 : C 0) (C1 : ∀ x, C $ pure x) (Cn : ∀ x, C (pure x) → C (-pure x)) (Cp : ∀ x y, C x → C y → C (x + y)) : C z := free_abelian_group.induction_on z C0 C1 Cn Cp @[simp] lemma map_pure (f : α → β) (x : α) : f <$> (pure x : free_abelian_group α) = pure (f x) := lift.of _ _ @[simp] lemma map_zero (f : α → β) : f <$> (0 : free_abelian_group α) = 0 := lift.zero (of ∘ f) @[simp] lemma map_add (f : α → β) (x y : free_abelian_group α) : f <$> (x + y) = f <$> x + f <$> y := lift.add _ _ _ @[simp] lemma map_neg (f : α → β) (x : free_abelian_group α) : f <$> (-x) = -(f <$> x) := lift.neg _ _ @[simp] lemma map_sub (f : α → β) (x y : free_abelian_group α) : f <$> (x - y) = f <$> x - f <$> y := lift.sub _ _ _ @[simp] lemma pure_bind (f : α → free_abelian_group β) (x) : pure x >>= f = f x := lift.of _ _ @[simp] lemma zero_bind (f : α → free_abelian_group β) : 0 >>= f = 0 := lift.zero f @[simp] lemma add_bind (f : α → free_abelian_group β) (x y : free_abelian_group α) : x + y >>= f = (x >>= f) + (y >>= f) := lift.add _ _ _ @[simp] lemma neg_bind (f : α → free_abelian_group β) (x : free_abelian_group α) : -x >>= f = -(x >>= f) := lift.neg _ _ @[simp] lemma sub_bind (f : α → free_abelian_group β) (x y : free_abelian_group α) : x - y >>= f = (x >>= f) - (y >>= f) := lift.sub _ _ _ @[simp] lemma pure_seq (f : α → β) (x : free_abelian_group α) : pure f <*> x = f <$> x := pure_bind _ _ @[simp] lemma zero_seq (x : free_abelian_group α) : (0 : free_abelian_group (α → β)) <*> x = 0 := zero_bind _ @[simp] lemma add_seq (f g : free_abelian_group (α → β)) (x : free_abelian_group α) : f + g <*> x = (f <*> x) + (g <*> x) := add_bind _ _ _ @[simp] lemma neg_seq (f : free_abelian_group (α → β)) (x : free_abelian_group α) : -f <*> x = -(f <*> x) := neg_bind _ _ @[simp] lemma sub_seq (f g : free_abelian_group (α → β)) (x : free_abelian_group α) : f - g <*> x = (f <*> x) - (g <*> x) := sub_bind _ _ _ instance is_add_group_hom_seq (f : free_abelian_group (α → β)) : is_add_group_hom ((<*>) f) := ⟨λ x y, show lift (<$> (x+y)) _ = _, by simp only [map_add]; exact @@is_add_group_hom.map_add _ _ _ (@@free_abelian_group.is_add_group_hom_lift' (free_abelian_group β) _ _) _ _⟩ @[simp] lemma seq_zero (f : free_abelian_group (α → β)) : f <*> 0 = 0 := is_add_group_hom.map_zero _ @[simp] lemma seq_add (f : free_abelian_group (α → β)) (x y : free_abelian_group α) : f <*> (x + y) = (f <*> x) + (f <*> y) := is_add_group_hom.map_add _ _ _ @[simp] lemma seq_neg (f : free_abelian_group (α → β)) (x : free_abelian_group α) : f <*> (-x) = -(f <*> x) := is_add_group_hom.map_neg _ _ @[simp] lemma seq_sub (f : free_abelian_group (α → β)) (x y : free_abelian_group α) : f <*> (x - y) = (f <*> x) - (f <*> y) := is_add_group_hom.map_sub _ _ _ instance : is_lawful_monad free_abelian_group.{u} := { id_map := λ α x, free_abelian_group.induction_on' x (map_zero id) (λ x, map_pure id x) (λ x ih, by rw [map_neg, ih]) (λ x y ihx ihy, by rw [map_add, ihx, ihy]), pure_bind := λ α β x f, pure_bind f x, bind_assoc := λ α β γ x f g, free_abelian_group.induction_on' x (by iterate 3 { rw zero_bind }) (λ x, by iterate 2 { rw pure_bind }) (λ x ih, by iterate 3 { rw neg_bind }; rw ih) (λ x y ihx ihy, by iterate 3 { rw add_bind }; rw [ihx, ihy]) } instance : is_comm_applicative free_abelian_group.{u} := { commutative_prod := λ α β x y, free_abelian_group.induction_on' x (by rw [map_zero, zero_seq, seq_zero]) (λ p, by rw [map_pure, pure_seq]; exact free_abelian_group.induction_on' y (by rw [map_zero, map_zero, zero_seq]) (λ q, by rw [map_pure, map_pure, pure_seq, map_pure]) (λ q ih, by rw [map_neg, map_neg, neg_seq, ih]) (λ y₁ y₂ ih1 ih2, by rw [map_add, map_add, add_seq, ih1, ih2])) (λ p ih, by rw [map_neg, neg_seq, seq_neg, ih]) (λ x₁ x₂ ih1 ih2, by rw [map_add, add_seq, seq_add, ih1, ih2]) } end free_abelian_group
3b91d339f5e158d530bc277182a9f6e87320941d
cc060cf567f81c404a13ee79bf21f2e720fa6db0
/lean/20170621-finite.lean
fc60436afe3c55a18ac496a9d4e3aa4ab16a7f5b
[ "Apache-2.0" ]
permissive
semorrison/proof
cf0a8c6957153bdb206fd5d5a762a75958a82bca
5ee398aa239a379a431190edbb6022b1a0aa2c70
refs/heads/master
1,610,414,502,842
1,518,696,851,000
1,518,696,851,000
78,375,937
2
1
null
null
null
null
UTF-8
Lean
false
false
731
lean
structure {u v} Bijection ( P : Type u ) ( Q : Type v ) := ( function : P → Q ) ( inverse : Q → P ) ( witness_1 : function ∘ inverse = id ) ( witness_2 : inverse ∘ function = id ) class Finite ( α : Type ) := ( n : nat ) ( bijection : Bijection α (fin n) ) inductive Two : Type | _0 : Two | _1 : Two instance Two_is_Finite : Finite Two := sorry instance sum_is_Finite ( P : Type ) [ Finite P ] ( Q : Type ) [ Finite Q ] : Finite ( sum P Q ) := sorry instance prod_is_Finite ( P : Type ) [ Finite P ] ( Q : Type ) [ Finite Q ] : Finite ( prod P Q ) := sorry instance fun_is_Finite ( P : Type ) [ Finite P ] ( Q : Type ) [ Finite Q ] : Finite ( P → Q ) := sorry -- finiteness for Pi and Sigma types?
2af1d702dbfd574abeca5facc2976ebd9f6d8124
f7315930643edc12e76c229a742d5446dad77097
/hott/init/reserved_notation.hlean
2207e12f8e53b701fc08efee853265b69a3ec67a
[ "Apache-2.0" ]
permissive
bmalehorn/lean
8f77b762a76c59afff7b7403f9eb5fc2c3ce70c1
53653c352643751c4b62ff63ec5e555f11dae8eb
refs/heads/master
1,610,945,684,489
1,429,681,220,000
1,429,681,449,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,414
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: init.reserved_notation Authors: Leonardo de Moura -/ prelude import init.datatypes notation `assume` binders `,` r:(scoped f, f) := r notation `take` binders `,` r:(scoped f, f) := r /- Global declarations of right binding strength If a module reassigns these, it will be incompatible with other modules that adhere to these conventions. When hovering over a symbol, use "C-u C-x =" to see how to input it. -/ /- Logical operations and relations -/ definition std.prec.max : num := 1024 -- the strength of application, identifiers, (, [, etc. definition std.prec.arrow : num := 25 /- The next definition is "max + 10". It can be used e.g. for postfix operations that should be stronger than application. -/ definition std.prec.max_plus := num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ std.prec.max))))))))) /- Logical operations and relations -/ reserve prefix `¬`:40 reserve prefix `~`:40 reserve infixr `∧`:35 reserve infixr `/\`:35 reserve infixr `\/`:30 reserve infixr `∨`:30 reserve infix `<->`:25 reserve infix `↔`:25 reserve infix `=`:50 reserve infix `≠`:50 reserve infix `≈`:50 reserve infix `∼`:50 reserve infixr `∘`:60 -- input with \comp reserve postfix `⁻¹`:std.prec.max_plus -- input with \sy or \-1 or \inv reserve infixl `⬝`:75 reserve infixr `▸`:75 /- types and type constructors -/ reserve infixr `⊎`:25 reserve infixr `×`:30 /- arithmetic operations -/ reserve infixl `+`:65 reserve infixl `-`:65 reserve infixl `*`:70 reserve infixl `div`:70 reserve infixl `mod`:70 reserve infixl `/`:70 reserve prefix `-`:100 reserve infix `<=`:50 reserve infix `≤`:50 reserve infix `<`:50 reserve infix `>=`:50 reserve infix `≥`:50 reserve infix `>`:50 /- boolean operations -/ reserve infixl `&&`:70 reserve infixl `||`:65 /- set operations -/ reserve infix `∈`:50 reserve infix `∉`:50 reserve infixl `∩`:70 reserve infixl `∪`:65 /- other symbols -/ reserve infix `∣`:50 reserve infixl `++`:65 reserve infixr `::`:65 -- Yet another trick to anotate an expression with a type abbreviation is_typeof (A : Type) (a : A) : A := a notation `typeof` t `:` T := is_typeof T t notation `(` t `:` T `)` := is_typeof T t
c79cc301a43f933f2ef1ab311789687431d71017
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/data/set/function.lean
3751674f66ac4e08b6fb156692c999f9ae13c0f3
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
49,070
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Andrew Zipperer, Haitao Zhang, Minchao Wu, Yury Kudryashov -/ import data.set.prod import logic.function.conjugate /-! # Functions over sets ## Main definitions ### Predicate * `set.eq_on f₁ f₂ s` : functions `f₁` and `f₂` are equal at every point of `s`; * `set.maps_to f s t` : `f` sends every point of `s` to a point of `t`; * `set.inj_on f s` : restriction of `f` to `s` is injective; * `set.surj_on f s t` : every point in `s` has a preimage in `s`; * `set.bij_on f s t` : `f` is a bijection between `s` and `t`; * `set.left_inv_on f' f s` : for every `x ∈ s` we have `f' (f x) = x`; * `set.right_inv_on f' f t` : for every `y ∈ t` we have `f (f' y) = y`; * `set.inv_on f' f s t` : `f'` is a two-side inverse of `f` on `s` and `t`, i.e. we have `set.left_inv_on f' f s` and `set.right_inv_on f' f t`. ### Functions * `set.restrict f s` : restrict the domain of `f` to the set `s`; * `set.cod_restrict f s h` : given `h : ∀ x, f x ∈ s`, restrict the codomain of `f` to the set `s`; * `set.maps_to.restrict f s t h`: given `h : maps_to f s t`, restrict the domain of `f` to `s` and the codomain to `t`. -/ universes u v w x y variables {α : Type u} {β : Type v} {π : α → Type v} {γ : Type w} {ι : Sort x} open function namespace set /-! ### Restrict -/ /-- Restrict domain of a function `f` to a set `s`. Same as `subtype.restrict` but this version takes an argument `↥s` instead of `subtype s`. -/ def restrict (s : set α) (f : Π a : α, π a) : Π a : s, π a := λ x, f x lemma restrict_eq (f : α → β) (s : set α) : s.restrict f = f ∘ coe := rfl @[simp] lemma restrict_apply (f : α → β) (s : set α) (x : s) : s.restrict f x = f x := rfl lemma restrict_eq_iff {f : Π a, π a} {s : set α} {g : Π a : s, π a} : restrict s f = g ↔ ∀ a (ha : a ∈ s), f a = g ⟨a, ha⟩ := funext_iff.trans subtype.forall lemma eq_restrict_iff {s : set α} {f : Π a : s, π a} {g : Π a, π a} : f = restrict s g ↔ ∀ a (ha : a ∈ s), f ⟨a, ha⟩ = g a := funext_iff.trans subtype.forall @[simp] lemma range_restrict (f : α → β) (s : set α) : set.range (s.restrict f) = f '' s := (range_comp _ _).trans $ congr_arg (('') f) subtype.range_coe lemma image_restrict (f : α → β) (s t : set α) : s.restrict f '' (coe ⁻¹' t) = f '' (t ∩ s) := by rw [restrict, image_comp, image_preimage_eq_inter_range, subtype.range_coe] @[simp] lemma restrict_dite {s : set α} [∀ x, decidable (x ∈ s)] (f : Π a ∈ s, β) (g : Π a ∉ s, β) : s.restrict (λ a, if h : a ∈ s then f a h else g a h) = λ a, f a a.2 := funext $ λ a, dif_pos a.2 @[simp] lemma restrict_dite_compl {s : set α} [∀ x, decidable (x ∈ s)] (f : Π a ∈ s, β) (g : Π a ∉ s, β) : sᶜ.restrict (λ a, if h : a ∈ s then f a h else g a h) = λ a, g a a.2 := funext $ λ a, dif_neg a.2 @[simp] lemma restrict_ite (f g : α → β) (s : set α) [∀ x, decidable (x ∈ s)] : s.restrict (λ a, if a ∈ s then f a else g a) = s.restrict f := restrict_dite _ _ @[simp] lemma restrict_ite_compl (f g : α → β) (s : set α) [∀ x, decidable (x ∈ s)] : sᶜ.restrict (λ a, if a ∈ s then f a else g a) = sᶜ.restrict g := restrict_dite_compl _ _ @[simp] lemma restrict_piecewise (f g : α → β) (s : set α) [∀ x, decidable (x ∈ s)] : s.restrict (piecewise s f g) = s.restrict f := restrict_ite _ _ _ @[simp] lemma restrict_piecewise_compl (f g : α → β) (s : set α) [∀ x, decidable (x ∈ s)] : sᶜ.restrict (piecewise s f g) = sᶜ.restrict g := restrict_ite_compl _ _ _ lemma restrict_extend_range (f : α → β) (g : α → γ) (g' : β → γ) : (range f).restrict (extend f g g') = λ x, g x.coe_prop.some := by convert restrict_dite _ _ @[simp] lemma restrict_extend_compl_range (f : α → β) (g : α → γ) (g' : β → γ) : (range f)ᶜ.restrict (extend f g g') = g' ∘ coe := by convert restrict_dite_compl _ _ lemma range_extend_subset (f : α → β) (g : α → γ) (g' : β → γ) : range (extend f g g') ⊆ range g ∪ g' '' (range f)ᶜ := begin classical, rintro _ ⟨y, rfl⟩, rw extend_def, split_ifs, exacts [or.inl (mem_range_self _), or.inr (mem_image_of_mem _ h)] end lemma range_extend {f : α → β} (hf : injective f) (g : α → γ) (g' : β → γ) : range (extend f g g') = range g ∪ g' '' (range f)ᶜ := begin refine (range_extend_subset _ _ _).antisymm _, rintro z (⟨x, rfl⟩|⟨y, hy, rfl⟩), exacts [⟨f x, extend_apply hf _ _ _⟩, ⟨y, extend_apply' _ _ _ hy⟩] end /-- Restrict codomain of a function `f` to a set `s`. Same as `subtype.coind` but this version has codomain `↥s` instead of `subtype s`. -/ def cod_restrict (f : ι → α) (s : set α) (h : ∀ x, f x ∈ s) : ι → s := λ x, ⟨f x, h x⟩ @[simp] lemma coe_cod_restrict_apply (f : ι → α) (s : set α) (h : ∀ x, f x ∈ s) (x : ι) : (cod_restrict f s h x : α) = f x := rfl @[simp] lemma restrict_comp_cod_restrict {f : ι → α} {g : α → β} {b : set α} (h : ∀ x, f x ∈ b) : (b.restrict g) ∘ (b.cod_restrict f h) = g ∘ f := rfl @[simp] lemma injective_cod_restrict {f : ι → α} {s : set α} (h : ∀ x, f x ∈ s) : injective (cod_restrict f s h) ↔ injective f := by simp only [injective, subtype.ext_iff, coe_cod_restrict_apply] alias injective_cod_restrict ↔ _ _root_.function.injective.cod_restrict variables {s s₁ s₂ : set α} {t t₁ t₂ : set β} {p : set γ} {f f₁ f₂ f₃ : α → β} {g g₁ g₂ : β → γ} {f' f₁' f₂' : β → α} {g' : γ → β} /-! ### Equality on a set -/ /-- Two functions `f₁ f₂ : α → β` are equal on `s` if `f₁ x = f₂ x` for all `x ∈ a`. -/ def eq_on (f₁ f₂ : α → β) (s : set α) : Prop := ∀ ⦃x⦄, x ∈ s → f₁ x = f₂ x @[simp] lemma eq_on_empty (f₁ f₂ : α → β) : eq_on f₁ f₂ ∅ := λ x, false.elim @[simp] lemma restrict_eq_restrict_iff : restrict s f₁ = restrict s f₂ ↔ eq_on f₁ f₂ s := restrict_eq_iff @[symm] lemma eq_on.symm (h : eq_on f₁ f₂ s) : eq_on f₂ f₁ s := λ x hx, (h hx).symm lemma eq_on_comm : eq_on f₁ f₂ s ↔ eq_on f₂ f₁ s := ⟨eq_on.symm, eq_on.symm⟩ @[refl] lemma eq_on_refl (f : α → β) (s : set α) : eq_on f f s := λ _ _, rfl @[trans] lemma eq_on.trans (h₁ : eq_on f₁ f₂ s) (h₂ : eq_on f₂ f₃ s) : eq_on f₁ f₃ s := λ x hx, (h₁ hx).trans (h₂ hx) theorem eq_on.image_eq (heq : eq_on f₁ f₂ s) : f₁ '' s = f₂ '' s := image_congr heq theorem eq_on.inter_preimage_eq (heq : eq_on f₁ f₂ s) (t : set β) : s ∩ f₁ ⁻¹' t = s ∩ f₂ ⁻¹' t := ext $ λ x, and.congr_right_iff.2 $ λ hx, by rw [mem_preimage, mem_preimage, heq hx] lemma eq_on.mono (hs : s₁ ⊆ s₂) (hf : eq_on f₁ f₂ s₂) : eq_on f₁ f₂ s₁ := λ x hx, hf (hs hx) lemma eq_on.comp_left (h : s.eq_on f₁ f₂) : s.eq_on (g ∘ f₁) (g ∘ f₂) := λ a ha, congr_arg _ $ h ha lemma comp_eq_of_eq_on_range {ι : Sort*} {f : ι → α} {g₁ g₂ : α → β} (h : eq_on g₁ g₂ (range f)) : g₁ ∘ f = g₂ ∘ f := funext $ λ x, h $ mem_range_self _ /-! ### Congruence lemmas -/ section order variables [preorder α] [preorder β] lemma _root_.monotone_on.congr (h₁ : monotone_on f₁ s) (h : s.eq_on f₁ f₂) : monotone_on f₂ s := begin intros a ha b hb hab, rw [←h ha, ←h hb], exact h₁ ha hb hab, end lemma _root_.antitone_on.congr (h₁ : antitone_on f₁ s) (h : s.eq_on f₁ f₂) : antitone_on f₂ s := h₁.dual_right.congr h lemma _root_.strict_mono_on.congr (h₁ : strict_mono_on f₁ s) (h : s.eq_on f₁ f₂) : strict_mono_on f₂ s := begin intros a ha b hb hab, rw [←h ha, ←h hb], exact h₁ ha hb hab, end lemma _root_.strict_anti_on.congr (h₁ : strict_anti_on f₁ s) (h : s.eq_on f₁ f₂) : strict_anti_on f₂ s := h₁.dual_right.congr h lemma eq_on.congr_monotone_on (h : s.eq_on f₁ f₂) : monotone_on f₁ s ↔ monotone_on f₂ s := ⟨λ h₁, h₁.congr h, λ h₂, h₂.congr h.symm⟩ lemma eq_on.congr_antitone_on (h : s.eq_on f₁ f₂) : antitone_on f₁ s ↔ antitone_on f₂ s := ⟨λ h₁, h₁.congr h, λ h₂, h₂.congr h.symm⟩ lemma eq_on.congr_strict_mono_on (h : s.eq_on f₁ f₂) : strict_mono_on f₁ s ↔ strict_mono_on f₂ s := ⟨λ h₁, h₁.congr h, λ h₂, h₂.congr h.symm⟩ lemma eq_on.congr_strict_anti_on (h : s.eq_on f₁ f₂) : strict_anti_on f₁ s ↔ strict_anti_on f₂ s := ⟨λ h₁, h₁.congr h, λ h₂, h₂.congr h.symm⟩ end order /-! ### Mono lemmas-/ section mono variables [preorder α] [preorder β] lemma _root_.monotone_on.mono (h : monotone_on f s) (h' : s₂ ⊆ s) : monotone_on f s₂ := λ x hx y hy, h (h' hx) (h' hy) lemma _root_.antitone_on.mono (h : antitone_on f s) (h' : s₂ ⊆ s) : antitone_on f s₂ := λ x hx y hy, h (h' hx) (h' hy) lemma _root_.strict_mono_on.mono (h : strict_mono_on f s) (h' : s₂ ⊆ s) : strict_mono_on f s₂ := λ x hx y hy, h (h' hx) (h' hy) lemma _root_.strict_anti_on.mono (h : strict_anti_on f s) (h' : s₂ ⊆ s) : strict_anti_on f s₂ := λ x hx y hy, h (h' hx) (h' hy) protected lemma _root_.monotone_on.monotone (h : monotone_on f s) : monotone (f ∘ coe : s → β) := λ x y hle, h x.coe_prop y.coe_prop hle protected lemma _root_.antitone_on.monotone (h : antitone_on f s) : antitone (f ∘ coe : s → β) := λ x y hle, h x.coe_prop y.coe_prop hle protected lemma _root_.strict_mono_on.strict_mono (h : strict_mono_on f s) : strict_mono (f ∘ coe : s → β) := λ x y hlt, h x.coe_prop y.coe_prop hlt protected lemma _root_.strict_anti_on.strict_anti (h : strict_anti_on f s) : strict_anti (f ∘ coe : s → β) := λ x y hlt, h x.coe_prop y.coe_prop hlt end mono /-! ### maps to -/ /-- `maps_to f a b` means that the image of `a` is contained in `b`. -/ def maps_to (f : α → β) (s : set α) (t : set β) : Prop := ∀ ⦃x⦄, x ∈ s → f x ∈ t /-- Given a map `f` sending `s : set α` into `t : set β`, restrict domain of `f` to `s` and the codomain to `t`. Same as `subtype.map`. -/ def maps_to.restrict (f : α → β) (s : set α) (t : set β) (h : maps_to f s t) : s → t := subtype.map f h @[simp] lemma maps_to.coe_restrict_apply (h : maps_to f s t) (x : s) : (h.restrict f s t x : β) = f x := rfl lemma maps_to.coe_restrict (h : set.maps_to f s t) : coe ∘ h.restrict f s t = s.restrict f := rfl lemma maps_to.range_restrict (f : α → β) (s : set α) (t : set β) (h : maps_to f s t) : range (h.restrict f s t) = coe ⁻¹' (f '' s) := set.range_subtype_map f h lemma maps_to_iff_exists_map_subtype : maps_to f s t ↔ ∃ g : s → t, ∀ x : s, f x = g x := ⟨λ h, ⟨h.restrict f s t, λ _, rfl⟩, λ ⟨g, hg⟩ x hx, by { erw [hg ⟨x, hx⟩], apply subtype.coe_prop }⟩ theorem maps_to' : maps_to f s t ↔ f '' s ⊆ t := image_subset_iff.symm @[simp] theorem maps_to_singleton {x : α} : maps_to f {x} t ↔ f x ∈ t := singleton_subset_iff theorem maps_to_empty (f : α → β) (t : set β) : maps_to f ∅ t := empty_subset _ theorem maps_to.image_subset (h : maps_to f s t) : f '' s ⊆ t := maps_to'.1 h theorem maps_to.congr (h₁ : maps_to f₁ s t) (h : eq_on f₁ f₂ s) : maps_to f₂ s t := λ x hx, h hx ▸ h₁ hx lemma eq_on.comp_right (hg : t.eq_on g₁ g₂) (hf : s.maps_to f t) : s.eq_on (g₁ ∘ f) (g₂ ∘ f) := λ a ha, hg $ hf ha theorem eq_on.maps_to_iff (H : eq_on f₁ f₂ s) : maps_to f₁ s t ↔ maps_to f₂ s t := ⟨λ h, h.congr H, λ h, h.congr H.symm⟩ theorem maps_to.comp (h₁ : maps_to g t p) (h₂ : maps_to f s t) : maps_to (g ∘ f) s p := λ x h, h₁ (h₂ h) theorem maps_to_id (s : set α) : maps_to id s s := λ x, id theorem maps_to.iterate {f : α → α} {s : set α} (h : maps_to f s s) : ∀ n, maps_to (f^[n]) s s | 0 := λ _, id | (n+1) := (maps_to.iterate n).comp h theorem maps_to.iterate_restrict {f : α → α} {s : set α} (h : maps_to f s s) (n : ℕ) : (h.restrict f s s^[n]) = (h.iterate n).restrict _ _ _ := begin funext x, rw [subtype.ext_iff, maps_to.coe_restrict_apply], induction n with n ihn generalizing x, { refl }, { simp [nat.iterate, ihn] } end theorem maps_to.mono (hf : maps_to f s₁ t₁) (hs : s₂ ⊆ s₁) (ht : t₁ ⊆ t₂) : maps_to f s₂ t₂ := λ x hx, ht (hf $ hs hx) theorem maps_to.mono_left (hf : maps_to f s₁ t) (hs : s₂ ⊆ s₁) : maps_to f s₂ t := λ x hx, hf (hs hx) theorem maps_to.mono_right (hf : maps_to f s t₁) (ht : t₁ ⊆ t₂) : maps_to f s t₂ := λ x hx, ht (hf hx) theorem maps_to.union_union (h₁ : maps_to f s₁ t₁) (h₂ : maps_to f s₂ t₂) : maps_to f (s₁ ∪ s₂) (t₁ ∪ t₂) := λ x hx, hx.elim (λ hx, or.inl $ h₁ hx) (λ hx, or.inr $ h₂ hx) theorem maps_to.union (h₁ : maps_to f s₁ t) (h₂ : maps_to f s₂ t) : maps_to f (s₁ ∪ s₂) t := union_self t ▸ h₁.union_union h₂ @[simp] theorem maps_to_union : maps_to f (s₁ ∪ s₂) t ↔ maps_to f s₁ t ∧ maps_to f s₂ t := ⟨λ h, ⟨h.mono (subset_union_left s₁ s₂) (subset.refl t), h.mono (subset_union_right s₁ s₂) (subset.refl t)⟩, λ h, h.1.union h.2⟩ theorem maps_to.inter (h₁ : maps_to f s t₁) (h₂ : maps_to f s t₂) : maps_to f s (t₁ ∩ t₂) := λ x hx, ⟨h₁ hx, h₂ hx⟩ theorem maps_to.inter_inter (h₁ : maps_to f s₁ t₁) (h₂ : maps_to f s₂ t₂) : maps_to f (s₁ ∩ s₂) (t₁ ∩ t₂) := λ x hx, ⟨h₁ hx.1, h₂ hx.2⟩ @[simp] theorem maps_to_inter : maps_to f s (t₁ ∩ t₂) ↔ maps_to f s t₁ ∧ maps_to f s t₂ := ⟨λ h, ⟨h.mono (subset.refl s) (inter_subset_left t₁ t₂), h.mono (subset.refl s) (inter_subset_right t₁ t₂)⟩, λ h, h.1.inter h.2⟩ theorem maps_to_univ (f : α → β) (s : set α) : maps_to f s univ := λ x h, trivial theorem maps_to_image (f : α → β) (s : set α) : maps_to f s (f '' s) := by rw maps_to' theorem maps_to_preimage (f : α → β) (t : set β) : maps_to f (f ⁻¹' t) t := subset.refl _ theorem maps_to_range (f : α → β) (s : set α) : maps_to f s (range f) := (maps_to_image f s).mono (subset.refl s) (image_subset_range _ _) @[simp] lemma maps_image_to (f : α → β) (g : γ → α) (s : set γ) (t : set β) : maps_to f (g '' s) t ↔ maps_to (f ∘ g) s t := ⟨λ h c hc, h ⟨c, hc, rfl⟩, λ h d ⟨c, hc⟩, hc.2 ▸ h hc.1⟩ @[simp] lemma maps_univ_to (f : α → β) (s : set β) : maps_to f univ s ↔ ∀ a, f a ∈ s := ⟨λ h a, h (mem_univ _), λ h x _, h x⟩ @[simp] lemma maps_range_to (f : α → β) (g : γ → α) (s : set β) : maps_to f (range g) s ↔ maps_to (f ∘ g) univ s := by rw [←image_univ, maps_image_to] theorem surjective_maps_to_image_restrict (f : α → β) (s : set α) : surjective ((maps_to_image f s).restrict f s (f '' s)) := λ ⟨y, x, hs, hxy⟩, ⟨⟨x, hs⟩, subtype.ext hxy⟩ theorem maps_to.mem_iff (h : maps_to f s t) (hc : maps_to f sᶜ tᶜ) {x} : f x ∈ t ↔ x ∈ s := ⟨λ ht, by_contra $ λ hs, hc hs ht, λ hx, h hx⟩ /-! ### Restriction onto preimage -/ section variables (t f) /-- The restriction of a function onto the preimage of a set. -/ @[simps] def restrict_preimage : f ⁻¹' t → t := (set.maps_to_preimage f t).restrict _ _ _ lemma range_restrict_preimage : range (t.restrict_preimage f) = coe ⁻¹' (range f) := begin delta set.restrict_preimage, rw [maps_to.range_restrict, set.image_preimage_eq_inter_range, set.preimage_inter, subtype.coe_preimage_self, set.univ_inter], end end /-! ### Injectivity on a set -/ /-- `f` is injective on `a` if the restriction of `f` to `a` is injective. -/ def inj_on (f : α → β) (s : set α) : Prop := ∀ ⦃x₁ : α⦄, x₁ ∈ s → ∀ ⦃x₂ : α⦄, x₂ ∈ s → f x₁ = f x₂ → x₁ = x₂ theorem subsingleton.inj_on (hs : s.subsingleton) (f : α → β) : inj_on f s := λ x hx y hy h, hs hx hy @[simp] theorem inj_on_empty (f : α → β) : inj_on f ∅ := subsingleton_empty.inj_on f @[simp] theorem inj_on_singleton (f : α → β) (a : α) : inj_on f {a} := subsingleton_singleton.inj_on f theorem inj_on.eq_iff {x y} (h : inj_on f s) (hx : x ∈ s) (hy : y ∈ s) : f x = f y ↔ x = y := ⟨h hx hy, λ h, h ▸ rfl⟩ theorem inj_on.congr (h₁ : inj_on f₁ s) (h : eq_on f₁ f₂ s) : inj_on f₂ s := λ x hx y hy, h hx ▸ h hy ▸ h₁ hx hy theorem eq_on.inj_on_iff (H : eq_on f₁ f₂ s) : inj_on f₁ s ↔ inj_on f₂ s := ⟨λ h, h.congr H, λ h, h.congr H.symm⟩ theorem inj_on.mono (h : s₁ ⊆ s₂) (ht : inj_on f s₂) : inj_on f s₁ := λ x hx y hy H, ht (h hx) (h hy) H theorem inj_on_union (h : disjoint s₁ s₂) : inj_on f (s₁ ∪ s₂) ↔ inj_on f s₁ ∧ inj_on f s₂ ∧ ∀ (x ∈ s₁) (y ∈ s₂), f x ≠ f y := begin refine ⟨λ H, ⟨H.mono $ subset_union_left _ _, H.mono $ subset_union_right _ _, _⟩, _⟩, { intros x hx y hy hxy, obtain rfl : x = y, from H (or.inl hx) (or.inr hy) hxy, exact h ⟨hx, hy⟩ }, { rintro ⟨h₁, h₂, h₁₂⟩, rintro x (hx|hx) y (hy|hy) hxy, exacts [h₁ hx hy hxy, (h₁₂ _ hx _ hy hxy).elim, (h₁₂ _ hy _ hx hxy.symm).elim, h₂ hx hy hxy] } end theorem inj_on_insert {f : α → β} {s : set α} {a : α} (has : a ∉ s) : set.inj_on f (insert a s) ↔ set.inj_on f s ∧ f a ∉ f '' s := have disjoint s {a}, from λ x ⟨hxs, (hxa : x = a)⟩, has (hxa ▸ hxs), by { rw [← union_singleton, inj_on_union this], simp } lemma injective_iff_inj_on_univ : injective f ↔ inj_on f univ := ⟨λ h x hx y hy hxy, h hxy, λ h _ _ heq, h trivial trivial heq⟩ lemma inj_on_of_injective (h : injective f) (s : set α) : inj_on f s := λ x hx y hy hxy, h hxy alias inj_on_of_injective ← _root_.function.injective.inj_on theorem inj_on.comp (hg : inj_on g t) (hf: inj_on f s) (h : maps_to f s t) : inj_on (g ∘ f) s := λ x hx y hy heq, hf hx hy $ hg (h hx) (h hy) heq lemma inj_on_iff_injective : inj_on f s ↔ injective (s.restrict f) := ⟨λ H a b h, subtype.eq $ H a.2 b.2 h, λ H a as b bs h, congr_arg subtype.val $ @H ⟨a, as⟩ ⟨b, bs⟩ h⟩ alias inj_on_iff_injective ↔ inj_on.injective _ lemma exists_inj_on_iff_injective [nonempty β] : (∃ f : α → β, inj_on f s) ↔ ∃ f : s → β, injective f := ⟨λ ⟨f, hf⟩, ⟨_, hf.injective⟩, λ ⟨f, hf⟩, by { lift f to α → β using trivial, exact ⟨f, inj_on_iff_injective.2 hf⟩ }⟩ lemma inj_on_preimage {B : set (set β)} (hB : B ⊆ 𝒫 (range f)) : inj_on (preimage f) B := λ s hs t ht hst, (preimage_eq_preimage' (hB hs) (hB ht)).1 hst lemma inj_on.mem_of_mem_image {x} (hf : inj_on f s) (hs : s₁ ⊆ s) (h : x ∈ s) (h₁ : f x ∈ f '' s₁) : x ∈ s₁ := let ⟨x', h', eq⟩ := h₁ in hf (hs h') h eq ▸ h' lemma inj_on.mem_image_iff {x} (hf : inj_on f s) (hs : s₁ ⊆ s) (hx : x ∈ s) : f x ∈ f '' s₁ ↔ x ∈ s₁ := ⟨hf.mem_of_mem_image hs hx, mem_image_of_mem f⟩ lemma inj_on.preimage_image_inter (hf : inj_on f s) (hs : s₁ ⊆ s) : f ⁻¹' (f '' s₁) ∩ s = s₁ := ext $ λ x, ⟨λ ⟨h₁, h₂⟩, hf.mem_of_mem_image hs h₂ h₁, λ h, ⟨mem_image_of_mem _ h, hs h⟩⟩ lemma eq_on.cancel_left (h : s.eq_on (g ∘ f₁) (g ∘ f₂)) (hg : t.inj_on g) (hf₁ : s.maps_to f₁ t) (hf₂ : s.maps_to f₂ t) : s.eq_on f₁ f₂ := λ a ha, hg (hf₁ ha) (hf₂ ha) (h ha) lemma inj_on.cancel_left (hg : t.inj_on g) (hf₁ : s.maps_to f₁ t) (hf₂ : s.maps_to f₂ t) : s.eq_on (g ∘ f₁) (g ∘ f₂) ↔ s.eq_on f₁ f₂ := ⟨λ h, h.cancel_left hg hf₁ hf₂, eq_on.comp_left⟩ /-! ### Surjectivity on a set -/ /-- `f` is surjective from `a` to `b` if `b` is contained in the image of `a`. -/ def surj_on (f : α → β) (s : set α) (t : set β) : Prop := t ⊆ f '' s theorem surj_on.subset_range (h : surj_on f s t) : t ⊆ range f := subset.trans h $ image_subset_range f s lemma surj_on_iff_exists_map_subtype : surj_on f s t ↔ ∃ (t' : set β) (g : s → t'), t ⊆ t' ∧ surjective g ∧ ∀ x : s, f x = g x := ⟨λ h, ⟨_, (maps_to_image f s).restrict f s _, h, surjective_maps_to_image_restrict _ _, λ _, rfl⟩, λ ⟨t', g, htt', hg, hfg⟩ y hy, let ⟨x, hx⟩ := hg ⟨y, htt' hy⟩ in ⟨x, x.2, by rw [hfg, hx, subtype.coe_mk]⟩⟩ theorem surj_on_empty (f : α → β) (s : set α) : surj_on f s ∅ := empty_subset _ theorem surj_on_image (f : α → β) (s : set α) : surj_on f s (f '' s) := subset.rfl theorem surj_on.comap_nonempty (h : surj_on f s t) (ht : t.nonempty) : s.nonempty := (ht.mono h).of_image theorem surj_on.congr (h : surj_on f₁ s t) (H : eq_on f₁ f₂ s) : surj_on f₂ s t := by rwa [surj_on, ← H.image_eq] theorem eq_on.surj_on_iff (h : eq_on f₁ f₂ s) : surj_on f₁ s t ↔ surj_on f₂ s t := ⟨λ H, H.congr h, λ H, H.congr h.symm⟩ theorem surj_on.mono (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) (hf : surj_on f s₁ t₂) : surj_on f s₂ t₁ := subset.trans ht $ subset.trans hf $ image_subset _ hs theorem surj_on.union (h₁ : surj_on f s t₁) (h₂ : surj_on f s t₂) : surj_on f s (t₁ ∪ t₂) := λ x hx, hx.elim (λ hx, h₁ hx) (λ hx, h₂ hx) theorem surj_on.union_union (h₁ : surj_on f s₁ t₁) (h₂ : surj_on f s₂ t₂) : surj_on f (s₁ ∪ s₂) (t₁ ∪ t₂) := (h₁.mono (subset_union_left _ _) (subset.refl _)).union (h₂.mono (subset_union_right _ _) (subset.refl _)) theorem surj_on.inter_inter (h₁ : surj_on f s₁ t₁) (h₂ : surj_on f s₂ t₂) (h : inj_on f (s₁ ∪ s₂)) : surj_on f (s₁ ∩ s₂) (t₁ ∩ t₂) := begin intros y hy, rcases h₁ hy.1 with ⟨x₁, hx₁, rfl⟩, rcases h₂ hy.2 with ⟨x₂, hx₂, heq⟩, obtain rfl : x₁ = x₂ := h (or.inl hx₁) (or.inr hx₂) heq.symm, exact mem_image_of_mem f ⟨hx₁, hx₂⟩ end theorem surj_on.inter (h₁ : surj_on f s₁ t) (h₂ : surj_on f s₂ t) (h : inj_on f (s₁ ∪ s₂)) : surj_on f (s₁ ∩ s₂) t := inter_self t ▸ h₁.inter_inter h₂ h theorem surj_on.comp (hg : surj_on g t p) (hf : surj_on f s t) : surj_on (g ∘ f) s p := subset.trans hg $ subset.trans (image_subset g hf) $ (image_comp g f s) ▸ subset.refl _ lemma surjective_iff_surj_on_univ : surjective f ↔ surj_on f univ univ := by simp [surjective, surj_on, subset_def] lemma surj_on_iff_surjective : surj_on f s univ ↔ surjective (s.restrict f) := ⟨λ H b, let ⟨a, as, e⟩ := @H b trivial in ⟨⟨a, as⟩, e⟩, λ H b _, let ⟨⟨a, as⟩, e⟩ := H b in ⟨a, as, e⟩⟩ lemma surj_on.image_eq_of_maps_to (h₁ : surj_on f s t) (h₂ : maps_to f s t) : f '' s = t := eq_of_subset_of_subset h₂.image_subset h₁ lemma image_eq_iff_surj_on_maps_to : f '' s = t ↔ s.surj_on f t ∧ s.maps_to f t := begin refine ⟨_, λ h, h.1.image_eq_of_maps_to h.2⟩, rintro rfl, exact ⟨s.surj_on_image f, s.maps_to_image f⟩, end lemma surj_on.maps_to_compl (h : surj_on f s t) (h' : injective f) : maps_to f sᶜ tᶜ := λ x hs ht, let ⟨x', hx', heq⟩ := h ht in hs $ h' heq ▸ hx' lemma maps_to.surj_on_compl (h : maps_to f s t) (h' : surjective f) : surj_on f sᶜ tᶜ := h'.forall.2 $ λ x ht, mem_image_of_mem _ $ λ hs, ht (h hs) lemma eq_on.cancel_right (hf : s.eq_on (g₁ ∘ f) (g₂ ∘ f)) (hf' : s.surj_on f t) : t.eq_on g₁ g₂ := begin intros b hb, obtain ⟨a, ha, rfl⟩ := hf' hb, exact hf ha, end lemma surj_on.cancel_right (hf : s.surj_on f t) (hf' : s.maps_to f t) : s.eq_on (g₁ ∘ f) (g₂ ∘ f) ↔ t.eq_on g₁ g₂ := ⟨λ h, h.cancel_right hf, λ h, h.comp_right hf'⟩ lemma eq_on_comp_right_iff : s.eq_on (g₁ ∘ f) (g₂ ∘ f) ↔ (f '' s).eq_on g₁ g₂ := (s.surj_on_image f).cancel_right $ s.maps_to_image f /-! ### Bijectivity -/ /-- `f` is bijective from `s` to `t` if `f` is injective on `s` and `f '' s = t`. -/ def bij_on (f : α → β) (s : set α) (t : set β) : Prop := maps_to f s t ∧ inj_on f s ∧ surj_on f s t lemma bij_on.maps_to (h : bij_on f s t) : maps_to f s t := h.left lemma bij_on.inj_on (h : bij_on f s t) : inj_on f s := h.right.left lemma bij_on.surj_on (h : bij_on f s t) : surj_on f s t := h.right.right lemma bij_on.mk (h₁ : maps_to f s t) (h₂ : inj_on f s) (h₃ : surj_on f s t) : bij_on f s t := ⟨h₁, h₂, h₃⟩ lemma bij_on_empty (f : α → β) : bij_on f ∅ ∅ := ⟨maps_to_empty f ∅, inj_on_empty f, surj_on_empty f ∅⟩ lemma bij_on.inter (h₁ : bij_on f s₁ t₁) (h₂ : bij_on f s₂ t₂) (h : inj_on f (s₁ ∪ s₂)) : bij_on f (s₁ ∩ s₂) (t₁ ∩ t₂) := ⟨h₁.maps_to.inter_inter h₂.maps_to, h₁.inj_on.mono $ inter_subset_left _ _, h₁.surj_on.inter_inter h₂.surj_on h⟩ lemma bij_on.union (h₁ : bij_on f s₁ t₁) (h₂ : bij_on f s₂ t₂) (h : inj_on f (s₁ ∪ s₂)) : bij_on f (s₁ ∪ s₂) (t₁ ∪ t₂) := ⟨h₁.maps_to.union_union h₂.maps_to, h, h₁.surj_on.union_union h₂.surj_on⟩ theorem bij_on.subset_range (h : bij_on f s t) : t ⊆ range f := h.surj_on.subset_range lemma inj_on.bij_on_image (h : inj_on f s) : bij_on f s (f '' s) := bij_on.mk (maps_to_image f s) h (subset.refl _) theorem bij_on.congr (h₁ : bij_on f₁ s t) (h : eq_on f₁ f₂ s) : bij_on f₂ s t := bij_on.mk (h₁.maps_to.congr h) (h₁.inj_on.congr h) (h₁.surj_on.congr h) theorem eq_on.bij_on_iff (H : eq_on f₁ f₂ s) : bij_on f₁ s t ↔ bij_on f₂ s t := ⟨λ h, h.congr H, λ h, h.congr H.symm⟩ lemma bij_on.image_eq (h : bij_on f s t) : f '' s = t := h.surj_on.image_eq_of_maps_to h.maps_to theorem bij_on.comp (hg : bij_on g t p) (hf : bij_on f s t) : bij_on (g ∘ f) s p := bij_on.mk (hg.maps_to.comp hf.maps_to) (hg.inj_on.comp hf.inj_on hf.maps_to) (hg.surj_on.comp hf.surj_on) theorem bij_on.bijective (h : bij_on f s t) : bijective (t.cod_restrict (s.restrict f) $ λ x, h.maps_to x.val_prop) := ⟨λ x y h', subtype.ext $ h.inj_on x.2 y.2 $ subtype.ext_iff.1 h', λ ⟨y, hy⟩, let ⟨x, hx, hxy⟩ := h.surj_on hy in ⟨⟨x, hx⟩, subtype.eq hxy⟩⟩ lemma bijective_iff_bij_on_univ : bijective f ↔ bij_on f univ univ := iff.intro (λ h, let ⟨inj, surj⟩ := h in ⟨maps_to_univ f _, inj.inj_on _, iff.mp surjective_iff_surj_on_univ surj⟩) (λ h, let ⟨map, inj, surj⟩ := h in ⟨iff.mpr injective_iff_inj_on_univ inj, iff.mpr surjective_iff_surj_on_univ surj⟩) lemma bij_on.compl (hst : bij_on f s t) (hf : bijective f) : bij_on f sᶜ tᶜ := ⟨hst.surj_on.maps_to_compl hf.1, hf.1.inj_on _, hst.maps_to.surj_on_compl hf.2⟩ /-! ### left inverse -/ /-- `g` is a left inverse to `f` on `a` means that `g (f x) = x` for all `x ∈ a`. -/ def left_inv_on (f' : β → α) (f : α → β) (s : set α) : Prop := ∀ ⦃x⦄, x ∈ s → f' (f x) = x lemma left_inv_on.eq_on (h : left_inv_on f' f s) : eq_on (f' ∘ f) id s := h lemma left_inv_on.eq (h : left_inv_on f' f s) {x} (hx : x ∈ s) : f' (f x) = x := h hx lemma left_inv_on.congr_left (h₁ : left_inv_on f₁' f s) {t : set β} (h₁' : maps_to f s t) (heq : eq_on f₁' f₂' t) : left_inv_on f₂' f s := λ x hx, heq (h₁' hx) ▸ h₁ hx theorem left_inv_on.congr_right (h₁ : left_inv_on f₁' f₁ s) (heq : eq_on f₁ f₂ s) : left_inv_on f₁' f₂ s := λ x hx, heq hx ▸ h₁ hx theorem left_inv_on.inj_on (h : left_inv_on f₁' f s) : inj_on f s := λ x₁ h₁ x₂ h₂ heq, calc x₁ = f₁' (f x₁) : eq.symm $ h h₁ ... = f₁' (f x₂) : congr_arg f₁' heq ... = x₂ : h h₂ theorem left_inv_on.surj_on (h : left_inv_on f' f s) (hf : maps_to f s t) : surj_on f' t s := λ x hx, ⟨f x, hf hx, h hx⟩ theorem left_inv_on.maps_to (h : left_inv_on f' f s) (hf : surj_on f s t) : maps_to f' t s := λ y hy, let ⟨x, hs, hx⟩ := hf hy in by rwa [← hx, h hs] theorem left_inv_on.comp (hf' : left_inv_on f' f s) (hg' : left_inv_on g' g t) (hf : maps_to f s t) : left_inv_on (f' ∘ g') (g ∘ f) s := λ x h, calc (f' ∘ g') ((g ∘ f) x) = f' (f x) : congr_arg f' (hg' (hf h)) ... = x : hf' h theorem left_inv_on.mono (hf : left_inv_on f' f s) (ht : s₁ ⊆ s) : left_inv_on f' f s₁ := λ x hx, hf (ht hx) theorem left_inv_on.image_inter' (hf : left_inv_on f' f s) : f '' (s₁ ∩ s) = f' ⁻¹' s₁ ∩ f '' s := begin apply subset.antisymm, { rintro _ ⟨x, ⟨h₁, h⟩, rfl⟩, exact ⟨by rwa [mem_preimage, hf h], mem_image_of_mem _ h⟩ }, { rintro _ ⟨h₁, ⟨x, h, rfl⟩⟩, exact mem_image_of_mem _ ⟨by rwa ← hf h, h⟩ } end theorem left_inv_on.image_inter (hf : left_inv_on f' f s) : f '' (s₁ ∩ s) = f' ⁻¹' (s₁ ∩ s) ∩ f '' s := begin rw hf.image_inter', refine subset.antisymm _ (inter_subset_inter_left _ (preimage_mono $ inter_subset_left _ _)), rintro _ ⟨h₁, x, hx, rfl⟩, exact ⟨⟨h₁, by rwa hf hx⟩, mem_image_of_mem _ hx⟩ end theorem left_inv_on.image_image (hf : left_inv_on f' f s) : f' '' (f '' s) = s := by rw [image_image, image_congr hf, image_id'] theorem left_inv_on.image_image' (hf : left_inv_on f' f s) (hs : s₁ ⊆ s) : f' '' (f '' s₁) = s₁ := (hf.mono hs).image_image /-! ### Right inverse -/ /-- `g` is a right inverse to `f` on `b` if `f (g x) = x` for all `x ∈ b`. -/ @[reducible] def right_inv_on (f' : β → α) (f : α → β) (t : set β) : Prop := left_inv_on f f' t lemma right_inv_on.eq_on (h : right_inv_on f' f t) : eq_on (f ∘ f') id t := h lemma right_inv_on.eq (h : right_inv_on f' f t) {y} (hy : y ∈ t) : f (f' y) = y := h hy lemma left_inv_on.right_inv_on_image (h : left_inv_on f' f s) : right_inv_on f' f (f '' s) := λ y ⟨x, hx, eq⟩, eq ▸ congr_arg f $ h.eq hx theorem right_inv_on.congr_left (h₁ : right_inv_on f₁' f t) (heq : eq_on f₁' f₂' t) : right_inv_on f₂' f t := h₁.congr_right heq theorem right_inv_on.congr_right (h₁ : right_inv_on f' f₁ t) (hg : maps_to f' t s) (heq : eq_on f₁ f₂ s) : right_inv_on f' f₂ t := left_inv_on.congr_left h₁ hg heq theorem right_inv_on.surj_on (hf : right_inv_on f' f t) (hf' : maps_to f' t s) : surj_on f s t := hf.surj_on hf' theorem right_inv_on.maps_to (h : right_inv_on f' f t) (hf : surj_on f' t s) : maps_to f s t := h.maps_to hf theorem right_inv_on.comp (hf : right_inv_on f' f t) (hg : right_inv_on g' g p) (g'pt : maps_to g' p t) : right_inv_on (f' ∘ g') (g ∘ f) p := hg.comp hf g'pt theorem right_inv_on.mono (hf : right_inv_on f' f t) (ht : t₁ ⊆ t) : right_inv_on f' f t₁ := hf.mono ht theorem inj_on.right_inv_on_of_left_inv_on (hf : inj_on f s) (hf' : left_inv_on f f' t) (h₁ : maps_to f s t) (h₂ : maps_to f' t s) : right_inv_on f f' s := λ x h, hf (h₂ $ h₁ h) h (hf' (h₁ h)) theorem eq_on_of_left_inv_on_of_right_inv_on (h₁ : left_inv_on f₁' f s) (h₂ : right_inv_on f₂' f t) (h : maps_to f₂' t s) : eq_on f₁' f₂' t := λ y hy, calc f₁' y = (f₁' ∘ f ∘ f₂') y : congr_arg f₁' (h₂ hy).symm ... = f₂' y : h₁ (h hy) theorem surj_on.left_inv_on_of_right_inv_on (hf : surj_on f s t) (hf' : right_inv_on f f' s) : left_inv_on f f' t := λ y hy, let ⟨x, hx, heq⟩ := hf hy in by rw [← heq, hf' hx] /-! ### Two-side inverses -/ /-- `g` is an inverse to `f` viewed as a map from `a` to `b` -/ def inv_on (g : β → α) (f : α → β) (s : set α) (t : set β) : Prop := left_inv_on g f s ∧ right_inv_on g f t lemma inv_on.symm (h : inv_on f' f s t) : inv_on f f' t s := ⟨h.right, h.left⟩ lemma inv_on.mono (h : inv_on f' f s t) (hs : s₁ ⊆ s) (ht : t₁ ⊆ t) : inv_on f' f s₁ t₁ := ⟨h.1.mono hs, h.2.mono ht⟩ /-- If functions `f'` and `f` are inverse on `s` and `t`, `f` maps `s` into `t`, and `f'` maps `t` into `s`, then `f` is a bijection between `s` and `t`. The `maps_to` arguments can be deduced from `surj_on` statements using `left_inv_on.maps_to` and `right_inv_on.maps_to`. -/ theorem inv_on.bij_on (h : inv_on f' f s t) (hf : maps_to f s t) (hf' : maps_to f' t s) : bij_on f s t := ⟨hf, h.left.inj_on, h.right.surj_on hf'⟩ end set /-! ### `inv_fun_on` is a left/right inverse -/ namespace function variables [nonempty α] {s : set α} {f : α → β} {a : α} {b : β} local attribute [instance, priority 10] classical.prop_decidable /-- Construct the inverse for a function `f` on domain `s`. This function is a right inverse of `f` on `f '' s`. For a computable version, see `function.injective.inv_of_mem_range`. -/ noncomputable def inv_fun_on (f : α → β) (s : set α) (b : β) : α := if h : ∃a, a ∈ s ∧ f a = b then classical.some h else classical.choice ‹nonempty α› theorem inv_fun_on_pos (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s ∧ f (inv_fun_on f s b) = b := by rw [bex_def] at h; rw [inv_fun_on, dif_pos h]; exact classical.some_spec h theorem inv_fun_on_mem (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s := (inv_fun_on_pos h).left theorem inv_fun_on_eq (h : ∃a∈s, f a = b) : f (inv_fun_on f s b) = b := (inv_fun_on_pos h).right theorem inv_fun_on_neg (h : ¬ ∃a∈s, f a = b) : inv_fun_on f s b = classical.choice ‹nonempty α› := by rw [bex_def] at h; rw [inv_fun_on, dif_neg h] end function namespace set open function variables {s s₁ s₂ : set α} {t : set β} {f : α → β} theorem inj_on.left_inv_on_inv_fun_on [nonempty α] (h : inj_on f s) : left_inv_on (inv_fun_on f s) f s := λ a ha, have ∃a'∈s, f a' = f a, from ⟨a, ha, rfl⟩, h (inv_fun_on_mem this) ha (inv_fun_on_eq this) lemma inj_on.inv_fun_on_image [nonempty α] (h : inj_on f s₂) (ht : s₁ ⊆ s₂) : (inv_fun_on f s₂) '' (f '' s₁) = s₁ := h.left_inv_on_inv_fun_on.image_image' ht theorem surj_on.right_inv_on_inv_fun_on [nonempty α] (h : surj_on f s t) : right_inv_on (inv_fun_on f s) f t := λ y hy, inv_fun_on_eq $ mem_image_iff_bex.1 $ h hy theorem bij_on.inv_on_inv_fun_on [nonempty α] (h : bij_on f s t) : inv_on (inv_fun_on f s) f s t := ⟨h.inj_on.left_inv_on_inv_fun_on, h.surj_on.right_inv_on_inv_fun_on⟩ theorem surj_on.inv_on_inv_fun_on [nonempty α] (h : surj_on f s t) : inv_on (inv_fun_on f s) f (inv_fun_on f s '' t) t := begin refine ⟨_, h.right_inv_on_inv_fun_on⟩, rintros _ ⟨y, hy, rfl⟩, rw [h.right_inv_on_inv_fun_on hy] end theorem surj_on.maps_to_inv_fun_on [nonempty α] (h : surj_on f s t) : maps_to (inv_fun_on f s) t s := λ y hy, mem_preimage.2 $ inv_fun_on_mem $ mem_image_iff_bex.1 $ h hy theorem surj_on.bij_on_subset [nonempty α] (h : surj_on f s t) : bij_on f (inv_fun_on f s '' t) t := begin refine h.inv_on_inv_fun_on.bij_on _ (maps_to_image _ _), rintros _ ⟨y, hy, rfl⟩, rwa [h.right_inv_on_inv_fun_on hy] end theorem surj_on_iff_exists_bij_on_subset : surj_on f s t ↔ ∃ s' ⊆ s, bij_on f s' t := begin split, { rcases eq_empty_or_nonempty t with rfl|ht, { exact λ _, ⟨∅, empty_subset _, bij_on_empty f⟩ }, { assume h, haveI : nonempty α := ⟨classical.some (h.comap_nonempty ht)⟩, exact ⟨_, h.maps_to_inv_fun_on.image_subset, h.bij_on_subset⟩ }}, { rintros ⟨s', hs', hfs'⟩, exact hfs'.surj_on.mono hs' (subset.refl _) } end lemma preimage_inv_fun_of_mem [n : nonempty α] {f : α → β} (hf : injective f) {s : set α} (h : classical.choice n ∈ s) : inv_fun f ⁻¹' s = f '' s ∪ (range f)ᶜ := begin ext x, rcases em (x ∈ range f) with ⟨a, rfl⟩|hx, { simp [left_inverse_inv_fun hf _, hf.mem_set_image] }, { simp [mem_preimage, inv_fun_neg hx, h, hx] } end lemma preimage_inv_fun_of_not_mem [n : nonempty α] {f : α → β} (hf : injective f) {s : set α} (h : classical.choice n ∉ s) : inv_fun f ⁻¹' s = f '' s := begin ext x, rcases em (x ∈ range f) with ⟨a, rfl⟩|hx, { rw [mem_preimage, left_inverse_inv_fun hf, hf.mem_set_image] }, { have : x ∉ f '' s, from λ h', hx (image_subset_range _ _ h'), simp only [mem_preimage, inv_fun_neg hx, h, this] }, end end set /-! ### Monotone -/ namespace monotone variables [preorder α] [preorder β] {f : α → β} protected lemma restrict (h : monotone f) (s : set α) : monotone (s.restrict f) := λ x y hxy, h hxy protected lemma cod_restrict (h : monotone f) {s : set β} (hs : ∀ x, f x ∈ s) : monotone (s.cod_restrict f hs) := h protected lemma range_factorization (h : monotone f) : monotone (set.range_factorization f) := h end monotone /-! ### Piecewise defined function -/ namespace set variables {δ : α → Sort y} (s : set α) (f g : Πi, δ i) @[simp] lemma piecewise_empty [∀i : α, decidable (i ∈ (∅ : set α))] : piecewise ∅ f g = g := by { ext i, simp [piecewise] } @[simp] lemma piecewise_univ [∀i : α, decidable (i ∈ (set.univ : set α))] : piecewise set.univ f g = f := by { ext i, simp [piecewise] } @[simp] lemma piecewise_insert_self {j : α} [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] variable [∀j, decidable (j ∈ s)] instance compl.decidable_mem (j : α) : decidable (j ∈ sᶜ) := not.decidable lemma piecewise_insert [decidable_eq α] (j : α) [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = function.update (s.piecewise f g) j (f j) := begin simp [piecewise], ext i, by_cases h : i = j, { rw h, simp }, { by_cases h' : i ∈ s; simp [h, h'] } end @[simp, priority 990] lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i := if_pos hi @[simp, priority 990] lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i := if_neg hi lemma piecewise_singleton (x : α) [Π y, decidable (y ∈ ({x} : set α))] [decidable_eq α] (f g : α → β) : piecewise {x} f g = function.update g x (f x) := by { ext y, by_cases hy : y = x, { subst y, simp }, { simp [hy] } } lemma piecewise_eq_on (f g : α → β) : eq_on (s.piecewise f g) f s := λ _, piecewise_eq_of_mem _ _ _ lemma piecewise_eq_on_compl (f g : α → β) : eq_on (s.piecewise f g) g sᶜ := λ _, piecewise_eq_of_not_mem _ _ _ lemma piecewise_le {δ : α → Type*} [Π i, preorder (δ i)] {s : set α} [Π j, decidable (j ∈ s)] {f₁ f₂ g : Π i, δ i} (h₁ : ∀ i ∈ s, f₁ i ≤ g i) (h₂ : ∀ i ∉ s, f₂ i ≤ g i) : s.piecewise f₁ f₂ ≤ g := λ i, if h : i ∈ s then by simp * else by simp * lemma le_piecewise {δ : α → Type*} [Π i, preorder (δ i)] {s : set α} [Π j, decidable (j ∈ s)] {f₁ f₂ g : Π i, δ i} (h₁ : ∀ i ∈ s, g i ≤ f₁ i) (h₂ : ∀ i ∉ s, g i ≤ f₂ i) : g ≤ s.piecewise f₁ f₂ := @piecewise_le α (λ i, (δ i)ᵒᵈ) _ s _ _ _ _ h₁ h₂ lemma piecewise_le_piecewise {δ : α → Type*} [Π i, preorder (δ i)] {s : set α} [Π j, decidable (j ∈ s)] {f₁ f₂ g₁ g₂ : Π i, δ i} (h₁ : ∀ i ∈ s, f₁ i ≤ g₁ i) (h₂ : ∀ i ∉ s, f₂ i ≤ g₂ i) : s.piecewise f₁ f₂ ≤ s.piecewise g₁ g₂ := by apply piecewise_le; intros; simp * @[simp, priority 990] lemma piecewise_insert_of_ne {i j : α} (h : i ≠ j) [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] @[simp] lemma piecewise_compl [∀ i, decidable (i ∈ sᶜ)] : sᶜ.piecewise f g = s.piecewise g f := funext $ λ x, if hx : x ∈ s then by simp [hx] else by simp [hx] @[simp] lemma piecewise_range_comp {ι : Sort*} (f : ι → α) [Π j, decidable (j ∈ range f)] (g₁ g₂ : α → β) : (range f).piecewise g₁ g₂ ∘ f = g₁ ∘ f := comp_eq_of_eq_on_range $ piecewise_eq_on _ _ _ theorem maps_to.piecewise_ite {s s₁ s₂ : set α} {t t₁ t₂ : set β} {f₁ f₂ : α → β} [∀ i, decidable (i ∈ s)] (h₁ : maps_to f₁ (s₁ ∩ s) (t₁ ∩ t)) (h₂ : maps_to f₂ (s₂ ∩ sᶜ) (t₂ ∩ tᶜ)) : maps_to (s.piecewise f₁ f₂) (s.ite s₁ s₂) (t.ite t₁ t₂) := begin refine (h₁.congr _).union_union (h₂.congr _), exacts [(piecewise_eq_on s f₁ f₂).symm.mono (inter_subset_right _ _), (piecewise_eq_on_compl s f₁ f₂).symm.mono (inter_subset_right _ _)] end theorem eq_on_piecewise {f f' g : α → β} {t} : eq_on (s.piecewise f f') g t ↔ eq_on f g (t ∩ s) ∧ eq_on f' g (t ∩ sᶜ) := begin simp only [eq_on, ← forall_and_distrib], refine forall_congr (λ a, _), by_cases a ∈ s; simp * end theorem eq_on.piecewise_ite' {f f' g : α → β} {t t'} (h : eq_on f g (t ∩ s)) (h' : eq_on f' g (t' ∩ sᶜ)) : eq_on (s.piecewise f f') g (s.ite t t') := by simp [eq_on_piecewise, *] theorem eq_on.piecewise_ite {f f' g : α → β} {t t'} (h : eq_on f g t) (h' : eq_on f' g t') : eq_on (s.piecewise f f') g (s.ite t t') := (h.mono (inter_subset_left _ _)).piecewise_ite' s (h'.mono (inter_subset_left _ _)) lemma piecewise_preimage (f g : α → β) (t) : s.piecewise f g ⁻¹' t = s.ite (f ⁻¹' t) (g ⁻¹' t) := ext $ λ x, by by_cases x ∈ s; simp [*, set.ite] lemma apply_piecewise {δ' : α → Sort*} (h : Π i, δ i → δ' i) {x : α} : h x (s.piecewise f g x) = s.piecewise (λ x, h x (f x)) (λ x, h x (g x)) x := by by_cases hx : x ∈ s; simp [hx] lemma apply_piecewise₂ {δ' δ'' : α → Sort*} (f' g' : Π i, δ' i) (h : Π i, δ i → δ' i → δ'' i) {x : α} : h x (s.piecewise f g x) (s.piecewise f' g' x) = s.piecewise (λ x, h x (f x) (f' x)) (λ x, h x (g x) (g' x)) x := by by_cases hx : x ∈ s; simp [hx] lemma piecewise_op {δ' : α → Sort*} (h : Π i, δ i → δ' i) : s.piecewise (λ x, h x (f x)) (λ x, h x (g x)) = λ x, h x (s.piecewise f g x) := funext $ λ x, (apply_piecewise _ _ _ _).symm lemma piecewise_op₂ {δ' δ'' : α → Sort*} (f' g' : Π i, δ' i) (h : Π i, δ i → δ' i → δ'' i) : s.piecewise (λ x, h x (f x) (f' x)) (λ x, h x (g x) (g' x)) = λ x, h x (s.piecewise f g x) (s.piecewise f' g' x) := funext $ λ x, (apply_piecewise₂ _ _ _ _ _ _).symm @[simp] lemma piecewise_same : s.piecewise f f = f := by { ext x, by_cases hx : x ∈ s; simp [hx] } lemma range_piecewise (f g : α → β) : range (s.piecewise f g) = f '' s ∪ g '' sᶜ := begin ext y, split, { rintro ⟨x, rfl⟩, by_cases h : x ∈ s;[left, right]; use x; simp [h] }, { rintro (⟨x, hx, rfl⟩|⟨x, hx, rfl⟩); use x; simp * at * } end lemma injective_piecewise_iff {f g : α → β} : injective (s.piecewise f g) ↔ inj_on f s ∧ inj_on g sᶜ ∧ (∀ (x ∈ s) (y ∉ s), f x ≠ g y) := begin rw [injective_iff_inj_on_univ, ← union_compl_self s, inj_on_union (@disjoint_compl_right _ s _), (piecewise_eq_on s f g).inj_on_iff, (piecewise_eq_on_compl s f g).inj_on_iff], refine and_congr iff.rfl (and_congr iff.rfl $ forall₄_congr $ λ x hx y hy, _), rw [piecewise_eq_of_mem s f g hx, piecewise_eq_of_not_mem s f g hy] end lemma piecewise_mem_pi {δ : α → Type*} {t : set α} {t' : Π i, set (δ i)} {f g} (hf : f ∈ pi t t') (hg : g ∈ pi t t') : s.piecewise f g ∈ pi t t' := by { intros i ht, by_cases hs : i ∈ s; simp [hf i ht, hg i ht, hs] } @[simp] lemma pi_piecewise {ι : Type*} {α : ι → Type*} (s s' : set ι) (t t' : Π i, set (α i)) [Π x, decidable (x ∈ s')] : pi s (s'.piecewise t t') = pi (s ∩ s') t ∩ pi (s \ s') t' := begin ext x, simp only [mem_pi, mem_inter_eq, ← forall_and_distrib], refine forall_congr (λ i, _), by_cases hi : i ∈ s'; simp * end lemma univ_pi_piecewise {ι : Type*} {α : ι → Type*} (s : set ι) (t : Π i, set (α i)) [Π x, decidable (x ∈ s)] : pi univ (s.piecewise t (λ _, univ)) = pi s t := by simp end set lemma strict_mono_on.inj_on [linear_order α] [preorder β] {f : α → β} {s : set α} (H : strict_mono_on f s) : s.inj_on f := λ x hx y hy hxy, show ordering.eq.compares x y, from (H.compares hx hy).1 hxy lemma strict_anti_on.inj_on [linear_order α] [preorder β] {f : α → β} {s : set α} (H : strict_anti_on f s) : s.inj_on f := @strict_mono_on.inj_on α βᵒᵈ _ _ f s H lemma strict_mono_on.comp [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : strict_mono_on g t) (hf : strict_mono_on f s) (hs : set.maps_to f s t) : strict_mono_on (g ∘ f) s := λ x hx y hy hxy, hg (hs hx) (hs hy) $ hf hx hy hxy lemma strict_mono_on.comp_strict_anti_on [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : strict_mono_on g t) (hf : strict_anti_on f s) (hs : set.maps_to f s t) : strict_anti_on (g ∘ f) s := λ x hx y hy hxy, hg (hs hy) (hs hx) $ hf hx hy hxy lemma strict_anti_on.comp [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : strict_anti_on g t) (hf : strict_anti_on f s) (hs : set.maps_to f s t) : strict_mono_on (g ∘ f) s := λ x hx y hy hxy, hg (hs hy) (hs hx) $ hf hx hy hxy lemma strict_anti_on.comp_strict_mono_on [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : strict_anti_on g t) (hf : strict_mono_on f s) (hs : set.maps_to f s t) : strict_anti_on (g ∘ f) s := λ x hx y hy hxy, hg (hs hx) (hs hy) $ hf hx hy hxy lemma strict_mono.cod_restrict [preorder α] [preorder β] {f : α → β} (hf : strict_mono f) {s : set β} (hs : ∀ x, f x ∈ s) : strict_mono (set.cod_restrict f s hs) := hf namespace function open set variables {fa : α → α} {fb : β → β} {f : α → β} {g : β → γ} {s t : set α} lemma injective.comp_inj_on (hg : injective g) (hf : s.inj_on f) : s.inj_on (g ∘ f) := (hg.inj_on univ).comp hf (maps_to_univ _ _) lemma surjective.surj_on (hf : surjective f) (s : set β) : surj_on f univ s := (surjective_iff_surj_on_univ.1 hf).mono (subset.refl _) (subset_univ _) lemma left_inverse.left_inv_on {g : β → α} (h : left_inverse f g) (s : set β) : left_inv_on f g s := λ x hx, h x lemma right_inverse.right_inv_on {g : β → α} (h : right_inverse f g) (s : set α) : right_inv_on f g s := λ x hx, h x lemma left_inverse.right_inv_on_range {g : β → α} (h : left_inverse f g) : right_inv_on f g (range g) := forall_range_iff.2 $ λ i, congr_arg g (h i) namespace semiconj lemma maps_to_image (h : semiconj f fa fb) (ha : maps_to fa s t) : maps_to fb (f '' s) (f '' t) := λ y ⟨x, hx, hy⟩, hy ▸ ⟨fa x, ha hx, h x⟩ lemma maps_to_range (h : semiconj f fa fb) : maps_to fb (range f) (range f) := λ y ⟨x, hy⟩, hy ▸ ⟨fa x, h x⟩ lemma surj_on_image (h : semiconj f fa fb) (ha : surj_on fa s t) : surj_on fb (f '' s) (f '' t) := begin rintros y ⟨x, hxt, rfl⟩, rcases ha hxt with ⟨x, hxs, rfl⟩, rw [h x], exact mem_image_of_mem _ (mem_image_of_mem _ hxs) end lemma surj_on_range (h : semiconj f fa fb) (ha : surjective fa) : surj_on fb (range f) (range f) := by { rw ← image_univ, exact h.surj_on_image (ha.surj_on univ) } lemma inj_on_image (h : semiconj f fa fb) (ha : inj_on fa s) (hf : inj_on f (fa '' s)) : inj_on fb (f '' s) := begin rintros _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩ H, simp only [← h.eq] at H, exact congr_arg f (ha hx hy $ hf (mem_image_of_mem fa hx) (mem_image_of_mem fa hy) H) end lemma inj_on_range (h : semiconj f fa fb) (ha : injective fa) (hf : inj_on f (range fa)) : inj_on fb (range f) := by { rw ← image_univ at *, exact h.inj_on_image (ha.inj_on univ) hf } lemma bij_on_image (h : semiconj f fa fb) (ha : bij_on fa s t) (hf : inj_on f t) : bij_on fb (f '' s) (f '' t) := ⟨h.maps_to_image ha.maps_to, h.inj_on_image ha.inj_on (ha.image_eq.symm ▸ hf), h.surj_on_image ha.surj_on⟩ lemma bij_on_range (h : semiconj f fa fb) (ha : bijective fa) (hf : injective f) : bij_on fb (range f) (range f) := begin rw [← image_univ], exact h.bij_on_image (bijective_iff_bij_on_univ.1 ha) (hf.inj_on univ) end lemma maps_to_preimage (h : semiconj f fa fb) {s t : set β} (hb : maps_to fb s t) : maps_to fa (f ⁻¹' s) (f ⁻¹' t) := λ x hx, by simp only [mem_preimage, h x, hb hx] lemma inj_on_preimage (h : semiconj f fa fb) {s : set β} (hb : inj_on fb s) (hf : inj_on f (f ⁻¹' s)) : inj_on fa (f ⁻¹' s) := begin intros x hx y hy H, have := congr_arg f H, rw [h.eq, h.eq] at this, exact hf hx hy (hb hx hy this) end end semiconj lemma update_comp_eq_of_not_mem_range' {α β : Sort*} {γ : β → Sort*} [decidable_eq β] (g : Π b, γ b) {f : α → β} {i : β} (a : γ i) (h : i ∉ set.range f) : (λ j, (function.update g i a) (f j)) = (λ j, g (f j)) := update_comp_eq_of_forall_ne' _ _ $ λ x hx, h ⟨x, hx⟩ /-- Non-dependent version of `function.update_comp_eq_of_not_mem_range'` -/ lemma update_comp_eq_of_not_mem_range {α β γ : Sort*} [decidable_eq β] (g : β → γ) {f : α → β} {i : β} (a : γ) (h : i ∉ set.range f) : (function.update g i a) ∘ f = g ∘ f := update_comp_eq_of_not_mem_range' g a h lemma insert_inj_on (s : set α) : sᶜ.inj_on (λ a, insert a s) := λ a ha b _, (insert_inj ha).1 end function
b95454f5c1d17c0d964178b37c19aa06add241b4
3dd1b66af77106badae6edb1c4dea91a146ead30
/library/standard/pair.lean
186f6636ec1d44043365d797124782991e034da4
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
1,377
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 import logic namespace pair inductive pair (A : Type) (B : Type) : Type := | mk_pair : A → B → pair A B section parameter {A : Type} parameter {B : Type} definition fst [inline] (p : pair A B) := pair_rec (λ x y, x) p definition snd [inline] (p : pair A B) := pair_rec (λ x y, y) p theorem pair_inhabited (H1 : inhabited A) (H2 : inhabited B) : inhabited (pair A B) := inhabited_elim H1 (λ a, inhabited_elim H2 (λ b, inhabited_intro (mk_pair a b))) theorem fst_mk_pair (a : A) (b : B) : fst (mk_pair a b) = a := refl a theorem snd_mk_pair (a : A) (b : B) : snd (mk_pair a b) = b := refl b theorem pair_ext (p : pair A B) : mk_pair (fst p) (snd p) = p := pair_rec (λ x y, refl (mk_pair x y)) p theorem pair_ext_eq {p1 p2 : pair A B} (H1 : fst p1 = fst p2) (H2 : snd p1 = snd p2) : p1 = p2 := calc p1 = mk_pair (fst p1) (snd p1) : symm (pair_ext p1) ... = mk_pair (fst p2) (snd p1) : {H1} ... = mk_pair (fst p2) (snd p2) : {H2} ... = p2 : pair_ext p2 end instance pair_inhabited precedence `×`:30 infixr × := pair -- notation for n-ary tuples notation `(` h `,` t:(foldl `,` (e r, mk_pair r e) h) `)` := t end
ede7eed4f684ff488355a435b80baa39d84f0294
c055f4b7c29cf1aac2223bd8c1ac8d181a7c6447
/src/categories/types/default.lean
11d9ca9c3bfa1dfc778009a2c7a86a793368e8bf
[ "Apache-2.0" ]
permissive
rwbarton/lean-category-theory-pr
77207b6674eeec1e258ec85dea58f3bff8d27065
591847d70c6a11c4d5561cd0eaf69b1fe85a70ab
refs/heads/master
1,584,595,111,303
1,528,029,041,000
1,528,029,041,000
135,919,126
0
0
null
1,528,041,805,000
1,528,041,805,000
null
UTF-8
Lean
false
false
3,040
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import ..category import ..isomorphism import ..functor import ..natural_transformation namespace categories.types open categories open categories.isomorphism open categories.functor universes u v w instance CategoryOfTypes : large_category (Type u) := { Hom := λ a b, (a → b), identity := λ a, id, compose := λ _ _ _ f g, g ∘ f, left_identity := begin -- `obviously'` says: intros, refl end, right_identity := begin -- `obviously'` says: intros, refl end, associativity := begin -- `obviously'` says: intros, refl end } @[simp] lemma Types.Hom {α β : Type u} : (α ⟶ β) = (α → β) := by refl @[simp] lemma Types.identity {α : Type u} (a : α) : (𝟙 α : α → α) a = a := by refl @[simp] lemma Types.compose {α β γ : Type u} (f : α → β) (g : β → γ) (a : α) : (((f : α ⟶ β) ≫ (g : β ⟶ γ)) : α ⟶ γ) a = g (f a) := by refl variables {C : Type (v+1)} [large_category C] (F G H : C ↝ (Type u)) {X Y Z : C} variables (σ : F ⟹ G) (τ : G ⟹ H) @[simp,ematch] lemma Functor_to_Types.functoriality (f : X ⟶ Y) (g : Y ⟶ Z) (a : F +> X) : (F &> (f ≫ g)) a = (F &> g) ((F &> f) a) := begin -- `obviously'` says: simp, end @[simp,ematch] lemma Functor_to_Types.identities (a : F +> X) : (F &> (𝟙 X)) a = a := begin -- `obviously'` says: simp, end @[ematch] lemma Functor_to_Types.naturality (f : X ⟶ Y) (x : F +> X) : σ.components Y ((F &> f) x) = (G &> f) (σ.components X x) := begin have p := σ.naturality_lemma f, exact congr_fun p x, end. @[simp] lemma Functor_to_Types.vertical_composition (x : F +> X) : (σ ⊟ τ).components X x = τ.components X (σ.components X x) := begin -- `obviously'` says: refl end variables {D : Type (w+1)} [large_category D] (I J : D ↝ C) (ρ : I ⟹ J) {W : D} @[simp] lemma Functor_to_Types.horizontal_composition (x : (I ⋙ F) +> W) : (ρ ◫ σ).components W x = (G &> ρ.components W) (σ.components (I +> W) x) := begin -- `obviously'` says: refl end definition UniverseLift : (Type u) ↝ (Type (max u v)) := { onObjects := λ X, ulift.{v} X, onMorphisms := λ X Y f, λ x : ulift.{v} X, ulift.up (f x.down), identities := begin -- `obviously'` says: intros, apply funext, intros, apply ulifts_equal, refl end, functoriality := begin -- `obviously'` says: intros, refl end } end categories.types
d21723fef5eaa137c6baa52fad76b42f2ce7a7f0
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/concept.lean
feea7695528d41c7f806a5db21c889ec975dc9bb
[ "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
11,012
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 data.set.lattice /-! # Formal concept analysis > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines concept lattices. A concept of a relation `r : α → β → Prop` is a pair of sets `s : set α` and `t : set β` such that `s` is the set of all `a : α` that are related to all elements of `t`, and `t` is the set of all `b : β` that are related to all elements of `s`. Ordering the concepts of a relation `r` by inclusion on the first component gives rise to a *concept lattice*. Every concept lattice is complete and in fact every complete lattice arises as the concept lattice of its `≤`. ## Implementation notes Concept lattices are usually defined from a *context*, that is the triple `(α, β, r)`, but the type of `r` determines `α` and `β` already, so we do not define contexts as a separate object. ## TODO Prove the fundamental theorem of concept lattices. ## References * [Davey, Priestley *Introduction to Lattices and Order*][davey_priestley] ## Tags concept, formal concept analysis, intent, extend, attribute -/ open function order_dual set variables {ι : Sort*} {α β γ : Type*} {κ : ι → Sort*} (r : α → β → Prop) {s s₁ s₂ : set α} {t t₁ t₂ : set β} /-! ### Intent and extent -/ /-- The intent closure of `s : set α` along a relation `r : α → β → Prop` is the set of all elements which `r` relates to all elements of `s`. -/ def intent_closure (s : set α) : set β := {b | ∀ ⦃a⦄, a ∈ s → r a b} /-- The extent closure of `t : set β` along a relation `r : α → β → Prop` is the set of all elements which `r` relates to all elements of `t`. -/ def extent_closure (t : set β) : set α := {a | ∀ ⦃b⦄, b ∈ t → r a b} variables {r} lemma subset_intent_closure_iff_subset_extent_closure : t ⊆ intent_closure r s ↔ s ⊆ extent_closure r t := ⟨λ h a ha b hb, h hb ha, λ h b hb a ha, h ha hb⟩ variables (r) lemma gc_intent_closure_extent_closure : galois_connection (to_dual ∘ intent_closure r) (extent_closure r ∘ of_dual) := λ s t, subset_intent_closure_iff_subset_extent_closure lemma intent_closure_swap (t : set β) : intent_closure (swap r) t = extent_closure r t := rfl lemma extent_closure_swap (s : set α) : extent_closure (swap r) s = intent_closure r s := rfl @[simp] lemma intent_closure_empty : intent_closure r ∅ = univ := eq_univ_of_forall $ λ _ _, false.elim @[simp] lemma extent_closure_empty : extent_closure r ∅ = univ := intent_closure_empty _ @[simp] lemma intent_closure_union (s₁ s₂ : set α) : intent_closure r (s₁ ∪ s₂) = intent_closure r s₁ ∩ intent_closure r s₂ := set.ext $ λ _, ball_or_left_distrib @[simp] lemma extent_closure_union (t₁ t₂ : set β) : extent_closure r (t₁ ∪ t₂) = extent_closure r t₁ ∩ extent_closure r t₂ := intent_closure_union _ _ _ @[simp] lemma intent_closure_Union (f : ι → set α) : intent_closure r (⋃ i, f i) = ⋂ i, intent_closure r (f i) := (gc_intent_closure_extent_closure r).l_supr @[simp] lemma extent_closure_Union (f : ι → set β) : extent_closure r (⋃ i, f i) = ⋂ i, extent_closure r (f i) := intent_closure_Union _ _ @[simp] lemma intent_closure_Union₂ (f : Π i, κ i → set α) : intent_closure r (⋃ i j, f i j) = ⋂ i j, intent_closure r (f i j) := (gc_intent_closure_extent_closure r).l_supr₂ @[simp] lemma extent_closure_Union₂ (f : Π i, κ i → set β) : extent_closure r (⋃ i j, f i j) = ⋂ i j, extent_closure r (f i j) := intent_closure_Union₂ _ _ lemma subset_extent_closure_intent_closure (s : set α) : s ⊆ extent_closure r (intent_closure r s) := (gc_intent_closure_extent_closure r).le_u_l _ lemma subset_intent_closure_extent_closure (t : set β) : t ⊆ intent_closure r (extent_closure r t) := subset_extent_closure_intent_closure _ t @[simp] lemma intent_closure_extent_closure_intent_closure (s : set α) : intent_closure r (extent_closure r $ intent_closure r s) = intent_closure r s := (gc_intent_closure_extent_closure r).l_u_l_eq_l _ @[simp] lemma extent_closure_intent_closure_extent_closure (t : set β) : extent_closure r (intent_closure r $ extent_closure r t) = extent_closure r t := intent_closure_extent_closure_intent_closure _ t lemma intent_closure_anti : antitone (intent_closure r) := (gc_intent_closure_extent_closure r).monotone_l lemma extent_closure_anti : antitone (extent_closure r) := intent_closure_anti _ /-! ### Concepts -/ variables (α β) /-- The formal concepts of a relation. A concept of `r : α → β → Prop` is a pair of sets `s`, `t` such that `s` is the set of all elements that are `r`-related to all of `t` and `t` is the set of all elements that are `r`-related to all of `s`. -/ structure concept extends set α × set β := (closure_fst : intent_closure r fst = snd) (closure_snd : extent_closure r snd = fst) namespace concept variables {r α β} {c d : concept α β r} attribute [simp] closure_fst closure_snd @[ext] lemma ext (h : c.fst = d.fst) : c = d := begin obtain ⟨⟨s₁, t₁⟩, h₁, _⟩ := c, obtain ⟨⟨s₂, t₂⟩, h₂, _⟩ := d, dsimp at h₁ h₂ h, subst h, subst h₁, subst h₂, end lemma ext' (h : c.snd = d.snd) : c = d := begin obtain ⟨⟨s₁, t₁⟩, _, h₁⟩ := c, obtain ⟨⟨s₂, t₂⟩, _, h₂⟩ := d, dsimp at h₁ h₂ h, subst h, subst h₁, subst h₂, end lemma fst_injective : injective (λ c : concept α β r, c.fst) := λ c d, ext lemma snd_injective : injective (λ c : concept α β r, c.snd) := λ c d, ext' instance : has_sup (concept α β r) := ⟨λ c d, { fst := extent_closure r (c.snd ∩ d.snd), snd := c.snd ∩ d.snd, closure_fst := by rw [←c.closure_fst, ←d.closure_fst, ←intent_closure_union, intent_closure_extent_closure_intent_closure], closure_snd := rfl }⟩ instance : has_inf (concept α β r) := ⟨λ c d, { fst := c.fst ∩ d.fst, snd := intent_closure r (c.fst ∩ d.fst), closure_fst := rfl, closure_snd := by rw [←c.closure_snd, ←d.closure_snd, ←extent_closure_union, extent_closure_intent_closure_extent_closure] }⟩ instance : semilattice_inf (concept α β r) := fst_injective.semilattice_inf _ $ λ _ _, rfl @[simp] lemma fst_subset_fst_iff : c.fst ⊆ d.fst ↔ c ≤ d := iff.rfl @[simp] lemma fst_ssubset_fst_iff : c.fst ⊂ d.fst ↔ c < d := iff.rfl @[simp] lemma snd_subset_snd_iff : c.snd ⊆ d.snd ↔ d ≤ c := begin refine ⟨λ h, _, λ h, _⟩, { rw [←fst_subset_fst_iff, ←c.closure_snd, ←d.closure_snd], exact extent_closure_anti _ h }, { rw [←c.closure_fst, ←d.closure_fst], exact intent_closure_anti _ h } end @[simp] lemma snd_ssubset_snd_iff : c.snd ⊂ d.snd ↔ d < c := by rw [ssubset_iff_subset_not_subset, lt_iff_le_not_le, snd_subset_snd_iff, snd_subset_snd_iff] lemma strict_mono_fst : strict_mono (prod.fst ∘ to_prod : concept α β r → set α) := λ c d, fst_ssubset_fst_iff.2 lemma strict_anti_snd : strict_anti (prod.snd ∘ to_prod : concept α β r → set β) := λ c d, snd_ssubset_snd_iff.2 instance : lattice (concept α β r) := { sup := (⊔), le_sup_left := λ c d, snd_subset_snd_iff.1 $ inter_subset_left _ _, le_sup_right := λ c d, snd_subset_snd_iff.1 $ inter_subset_right _ _, sup_le := λ c d e, by { simp_rw ←snd_subset_snd_iff, exact subset_inter }, ..concept.semilattice_inf } instance : bounded_order (concept α β r) := { top := ⟨⟨univ, intent_closure r univ⟩, rfl, eq_univ_of_forall $ λ a b hb, hb trivial⟩, le_top := λ _, subset_univ _, bot := ⟨⟨extent_closure r univ, univ⟩, eq_univ_of_forall $ λ b a ha, ha trivial, rfl⟩, bot_le := λ _, snd_subset_snd_iff.1 $ subset_univ _ } instance : has_Sup (concept α β r) := ⟨λ S, { fst := extent_closure r (⋂ c ∈ S, (c : concept _ _ _).snd), snd := ⋂ c ∈ S, (c : concept _ _ _).snd, closure_fst := by simp_rw [←closure_fst, ←intent_closure_Union₂, intent_closure_extent_closure_intent_closure], closure_snd := rfl }⟩ instance : has_Inf (concept α β r) := ⟨λ S, { fst := ⋂ c ∈ S, (c : concept _ _ _).fst, snd := intent_closure r (⋂ c ∈ S, (c : concept _ _ _).fst), closure_fst := rfl, closure_snd := by simp_rw [←closure_snd, ←extent_closure_Union₂, extent_closure_intent_closure_extent_closure] }⟩ instance : complete_lattice (concept α β r) := { Sup := Sup, le_Sup := λ S c hc, snd_subset_snd_iff.1 $ bInter_subset_of_mem hc, Sup_le := λ S c hc, snd_subset_snd_iff.1 $ subset_Inter₂ $ λ d hd, snd_subset_snd_iff.2 $ hc d hd, Inf := Inf, Inf_le := λ S c, bInter_subset_of_mem, le_Inf := λ S c, subset_Inter₂, ..concept.lattice, ..concept.bounded_order } @[simp] lemma top_fst : (⊤ : concept α β r).fst = univ := rfl @[simp] lemma top_snd : (⊤ : concept α β r).snd = intent_closure r univ := rfl @[simp] lemma bot_fst : (⊥ : concept α β r).fst = extent_closure r univ := rfl @[simp] lemma bot_snd : (⊥ : concept α β r).snd = univ := rfl @[simp] lemma sup_fst (c d : concept α β r) : (c ⊔ d).fst = extent_closure r (c.snd ∩ d.snd) := rfl @[simp] lemma sup_snd (c d : concept α β r) : (c ⊔ d).snd = c.snd ∩ d.snd := rfl @[simp] lemma inf_fst (c d : concept α β r) : (c ⊓ d).fst = c.fst ∩ d.fst := rfl @[simp] lemma inf_snd (c d : concept α β r) : (c ⊓ d).snd = intent_closure r (c.fst ∩ d.fst) := rfl @[simp] lemma Sup_fst (S : set (concept α β r)) : (Sup S).fst = extent_closure r ⋂ c ∈ S, (c : concept _ _ _).snd := rfl @[simp] lemma Sup_snd (S : set (concept α β r)) : (Sup S).snd = ⋂ c ∈ S, (c : concept _ _ _).snd := rfl @[simp] lemma Inf_fst (S : set (concept α β r)) : (Inf S).fst = ⋂ c ∈ S, (c : concept _ _ _).fst := rfl @[simp] lemma Inf_snd (S : set (concept α β r)) : (Inf S).snd = intent_closure r ⋂ c ∈ S, (c : concept _ _ _).fst := rfl instance : inhabited (concept α β r) := ⟨⊥⟩ /-- Swap the sets of a concept to make it a concept of the dual context. -/ @[simps] def swap (c : concept α β r) : concept β α (swap r) := ⟨c.to_prod.swap, c.closure_snd, c.closure_fst⟩ @[simp] lemma swap_swap (c : concept α β r) : c.swap.swap = c := ext rfl @[simp] lemma swap_le_swap_iff : c.swap ≤ d.swap ↔ d ≤ c := snd_subset_snd_iff @[simp] lemma swap_lt_swap_iff : c.swap < d.swap ↔ d < c := snd_ssubset_snd_iff /-- The dual of a concept lattice is isomorphic to the concept lattice of the dual context. -/ @[simps] def swap_equiv : (concept α β r)ᵒᵈ ≃o concept β α (function.swap r) := { to_fun := swap ∘ of_dual, inv_fun := to_dual ∘ swap, left_inv := swap_swap, right_inv := swap_swap, map_rel_iff' := λ c d, swap_le_swap_iff } end concept
274712bae5175e8202a9bd1473b30517edcac71d
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/tactic/derive_inhabited.lean
2f407a5976a1b03d6fe22413f392034e3c40257e
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,376
lean
/- Copyright (c) 2020 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import logic.basic import data.rbmap.basic /-! # Derive handler for `inhabited` instances This file introduces a derive handler to automatically generate `inhabited` instances for structures and inductives. We also add various `inhabited` instances for types in the core library. -/ namespace tactic /-- Tries to derive an `inhabited` instance for inductives and structures. For example: ``` @[derive inhabited] structure foo := (a : ℕ := 42) (b : list ℕ) ``` Here, `@[derive inhabited]` adds the instance `foo.inhabited`, which is defined as `⟨⟨42, default (list ℕ)⟩⟩`. For inductives, the default value is the first constructor. If the structure/inductive has a type parameter `α`, then the generated instance will have an argument `inhabited α`, even if it is not used. (This is due to the implementation using `instance_derive_handler`.) -/ @[derive_handler] meta def inhabited_instance : derive_handler := instance_derive_handler ``inhabited $ do applyc ``inhabited.mk, `[refine {..}] <|> (constructor >> skip), all_goals' $ do applyc ``default <|> (do s ← read, fail $ to_fmt "could not find inhabited instance for:\n" ++ to_fmt s) end tactic attribute [derive inhabited] vm_decl_kind vm_obj_kind tactic.new_goals tactic.transparency tactic.apply_cfg smt_pre_config ematch_config cc_config smt_config rsimp.config tactic.dunfold_config tactic.dsimp_config tactic.unfold_proj_config tactic.simp_intros_config tactic.delta_config tactic.simp_config tactic.rewrite_cfg interactive.loc tactic.unfold_config param_info subsingleton_info fun_info format.color pos environment.projection_info reducibility_hints congr_arg_kind ulift plift string_imp string.iterator_imp rbnode.color ordering unification_constraint pprod unification_hint doc_category tactic_doc_entry instance {α} : inhabited (bin_tree α) := ⟨bin_tree.empty⟩ instance : inhabited unsigned := ⟨0⟩ instance : inhabited string.iterator := string.iterator_imp.inhabited instance {α} : inhabited (rbnode α) := ⟨rbnode.leaf⟩ instance {α lt} : inhabited (rbtree α lt) := ⟨mk_rbtree _ _⟩ instance {α β lt} : inhabited (rbmap α β lt) := ⟨mk_rbmap _ _ _⟩
59ccddadec764b81d93b9aeac5f804f5d1f370ea
4fa161becb8ce7378a709f5992a594764699e268
/src/analysis/calculus/times_cont_diff.lean
2e49117e88695fb10176ef8a07a2cbc431486ffe
[ "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
83,337
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.mean_value /-! # Higher differentiability A function is `C^1` on a domain if it is differentiable there, and its derivative is continuous. By induction, it is `C^n` if it is `C^{n-1}` and its (n-1)-th derivative is `C^1` there or, equivalently, if it is `C^1` and its derivative is `C^{n-1}`. Finally, it is `C^∞` if it is `C^n` for all n. We formalize these notions by defining iteratively the `n+1`-th derivative of a function as the derivative of the `n`-th derivative. It is called `iterated_fderiv 𝕜 n f x` where `𝕜` is the field, `n` is the number of iterations, `f` is the function and `x` is the point, and it is given as an `n`-multilinear map. We also define a version `iterated_fderiv_within` relative to a domain, as well as predicates `times_cont_diff 𝕜 n f` and `times_cont_diff_on 𝕜 n f s` saying that the function is `C^n`, respectively in the whole space or on the set `s`. To avoid the issue of choice when choosing a derivative in sets where the derivative is not necessarily unique, `times_cont_diff_on` is not defined directly in terms of the regularity of the specific choice `iterated_fderiv_within 𝕜 n f s` inside `s`, but in terms of the existence of a nice sequence of derivatives, expressed with a predicate `has_ftaylor_series_up_to_on`. We prove basic properties of these notions. ## Main definitions and results Let `f : E → F` be a map between normed vector spaces over a nondiscrete normed field `𝕜`. * `formal_multilinear_series 𝕜 E F`: a family of `n`-multilinear maps for all `n`, designed to model the sequence of derivatives of a function. * `has_ftaylor_series_up_to n f p`: expresses that the formal multilinear series `p` is a sequence of iterated derivatives of `f`, up to the `n`-th term (where `n` is a natural number or `∞`). * `has_ftaylor_series_up_to_on n f p s`: same thing, but inside a set `s`. The notion of derivative is now taken inside `s`. In particular, derivatives don't have to be unique. * `times_cont_diff 𝕜 n f`: expresses that `f` is `C^n`, i.e., it admits a Taylor series up to rank `n`. * `times_cont_diff_on 𝕜 n f s`: expresses that `f` is `C^n` in `s`. * `iterated_fderiv_within 𝕜 n f s x` is an `n`-th derivative of `f` over the field `𝕜` on the set `s` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a derivative within `s` of `iterated_fderiv_within 𝕜 (n-1) f s` if one exists, and `0` otherwise. * `iterated_fderiv 𝕜 n f x` is the `n`-th derivative of `f` over the field `𝕜` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a derivative of `iterated_fderiv 𝕜 (n-1) f` if one exists, and `0` otherwise. In sets of unique differentiability, `times_cont_diff_on 𝕜 n f s` can be expressed in terms of the properties of `iterated_fderiv_within 𝕜 m f s` for `m ≤ n`. In the whole space, `times_cont_diff 𝕜 n f` can be expressed in terms of the properties of `iterated_fderiv 𝕜 m f` for `m ≤ n`. We also prove that the usual operations (addition, multiplication, difference, composition, and so on) preserve `C^n` functions. ## Implementation notes ### Definition of `C^n` functions in domains One could define `C^n` functions in a domain `s` by fixing an arbitrary choice of derivatives (this is what we do with `iterated_fderiv_within`) and requiring that all these derivatives up to `n` are continuous. If the derivative is not unique, this could lead to strange behavior like two `C^n` functions `f` and `g` on `s` whose sum is not `C^n`. A better definition is thus to say that a function is `C^n` inside `s` if it admits a sequence of derivatives up to `n` inside `s`. This definition still has the problem that a function which is locally `C^n` would not need to be `C^n`, as different choices of sequences of derivatives around different points might possibly not be glued together to give a globally defined sequence of derivatives. Also, there are locality problems in time: one could image a function which, for each `n`, has a nice sequence of derivatives up to order `n`, but they do not coincide for varying `n` and can therefore not be glued to give rise to an infinite sequence of derivatives. This would give a function which is `C^n` for all `n`, but not `C^∞`. We solve this issue by putting locality conditions in space and time in our definition of `times_cont_diff_on`. The resulting definition is slightly more complicated to work with (in fact not so much), but it gives rise to completely satisfactory theorems. ### Side of the composition, and universe issues With a naïve direct definition, the `n`-th derivative of a function belongs to the space `E →L[𝕜] (E →L[𝕜] (E ... F)...)))` where there are n iterations of `E →L[𝕜]`. This space may also be seen as the space of continuous multilinear functions on `n` copies of `E` with values in `F`, by uncurrying. This is the point of view that is usually adopted in textbooks, and that we also use. This means that the definition and the first proofs are slightly involved, as one has to keep track of the uncurrying operation. The uncurrying can be done from the left or from the right, amounting to defining the `n+1`-th derivative either as the derivative of the `n`-th derivative, or as the `n`-th derivative of the derivative. For proofs, it would be more convenient to use the latter approach (from the right), as it means to prove things at the `n+1`-th step we only need to understand well enough the derivative in `E →L[𝕜] F` (contrary to the approach from the left, where one would need to know enough on the `n`-th derivative to deduce things on the `n+1`-th derivative). However, the definition from the right leads to a universe polymorphism problem: if we define `iterated_fderiv 𝕜 (n + 1) f x = iterated_fderiv 𝕜 n (fderiv 𝕜 f) x` by induction, we need to generalize over all spaces (as `f` and `fderiv 𝕜 f` don't take values in the same space). It is only possible to generalize over all spaces in some fixed universe in an inductive definition. For `f : E → F`, then `fderiv 𝕜 f` is a map `E → (E →L[𝕜] F)`. Therefore, the definition will only work if `F` and `E →L[𝕜] F` are in the same universe. This issue does not appear with the definition from the left, where one does not need to generalize over all spaces. Therefore, we use the definition from the left. This means some proofs later on become a little bit more complicated: to prove that a function is `C^n`, the most efficient approach is to exhibit a formula for its `n`-th derivative and prove it is continuous (contrary to the inductive approach where one would prove smoothness statements without giving a formula for the derivative). In the end, this approach is still satisfactory as it is good to have formulas for the iterated derivatives in various constructions. One point where we depart from this explicit approach is in the proof of smoothness of a composition: there is a formula for the `n`-th derivative of a composition (Faà di Bruno's formula), but it is very complicated and barely usable, while the inductive proof is very simple. Thus, we give the inductive proof. As explained above, it works by generalizing over the target space, hence it only works well if all spaces belong to the same universe. To get the general version, we lift things to a common universe using a trick. ### Variables management The textbook definitions and proofs use various identifications and abuse of notations, for instance when saying that the natural space in which the derivative lives, i.e., `E →L[𝕜] (E →L[𝕜] ( ... →L[𝕜] F))`, is the same as a space of multilinear maps. When doing things formally, we need to provide explicit maps for these identifications, and chase some diagrams to see everything is compatible with the identifications. In particular, one needs to check that taking the derivative and then doing the identification, or first doing the identification and then taking the derivative, gives the same result. The key point for this is that taking the derivative commutes with continuous linear equivalences. Therefore, we need to implement all our identifications with continuous linear equivs. ## Notations We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives. ## Tags derivative, differentiability, higher derivative, `C^n`, multilinear, Taylor series, formal series -/ noncomputable theory open_locale classical universes u v w local attribute [instance, priority 1001] normed_group.to_add_comm_group normed_space.to_semimodule add_comm_group.to_add_comm_monoid open set fin open_locale topological_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [normed_group G] [normed_space 𝕜 G] {s s₁ t u : set E} {f f₁ : E → F} {g : F → G} {x : E} {c : F} {b : E × F → G} /-- A formal multilinear series over a field `𝕜`, from `E` to `F`, is given by a family of multilinear maps from `E^n` to `F` for all `n`. -/ @[derive add_comm_group] def formal_multilinear_series (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] (F : Type*) [normed_group F] [normed_space 𝕜 F] := Π (n : ℕ), (E [×n]→L[𝕜] F) instance : inhabited (formal_multilinear_series 𝕜 E F) := ⟨0⟩ section module /- `derive` is not able to find the module structure, probably because Lean is confused by the dependent types. We register it explicitly. -/ local attribute [reducible] formal_multilinear_series instance : module 𝕜 (formal_multilinear_series 𝕜 E F) := begin letI : ∀ n, module 𝕜 (continuous_multilinear_map 𝕜 (λ (i : fin n), E) F) := λ n, by apply_instance, apply_instance end end module namespace formal_multilinear_series variables (p : formal_multilinear_series 𝕜 E F) /-- Forgetting the zeroth term in a formal multilinear series, and interpreting the following terms as multilinear maps into `E →L[𝕜] F`. If `p` corresponds to the Taylor series of a function, then `p.shift` is the Taylor series of the derivative of the function. -/ def shift : formal_multilinear_series 𝕜 E (E →L[𝕜] F) := λn, (p n.succ).curry_right /-- Adding a zeroth term to a formal multilinear series taking values in `E →L[𝕜] F`. This corresponds to starting from a Taylor series for the derivative of a function, and building a Taylor series for the function itself. -/ def unshift (q : formal_multilinear_series 𝕜 E (E →L[𝕜] F)) (z : F) : formal_multilinear_series 𝕜 E F | 0 := (continuous_multilinear_curry_fin0 𝕜 E F).symm z | (n + 1) := (continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin (n + 1)), E) F) (q n) /-- Convenience congruence lemma stating in a dependent setting that, if the arguments to a formal multilinear series are equal, then the values are also equal. -/ lemma congr (p : formal_multilinear_series 𝕜 E F) {m n : ℕ} {v : fin m → E} {w : fin n → E} (h1 : m = n) (h2 : ∀ (i : ℕ) (him : i < m) (hin : i < n), v ⟨i, him⟩ = w ⟨i, hin⟩) : p m v = p n w := by { cases h1, congr, ext ⟨i, hi⟩, exact h2 i hi hi } end formal_multilinear_series variable {p : E → formal_multilinear_series 𝕜 E F} /-- `has_ftaylor_series_up_to_on n f p s` registers the fact that `p 0 = f` and `p (m+1)` is a derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to `has_fderiv_within_at` but for higher order derivatives. -/ structure has_ftaylor_series_up_to_on (n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F) (s : set E) : Prop := (zero_eq : ∀ x ∈ s, (p x 0).uncurry0 = f x) (fderiv_within : ∀ (m : ℕ) (hm : (m : with_top ℕ) < n), ∀ x ∈ s, has_fderiv_within_at (λ y, p y m) (p x m.succ).curry_left s x) (cont : ∀ (m : ℕ) (hm : (m : with_top ℕ) ≤ n), continuous_on (λ x, p x m) s) lemma has_ftaylor_series_up_to_on.zero_eq' {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {x : E} (hx : x ∈ s) : p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x) := by { rw ← h.zero_eq x hx, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ } /-- If two functions coincide on a set `s`, then a Taylor series for the first one is as well a Taylor series for the second one. -/ lemma has_ftaylor_series_up_to_on.congr {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (h₁ : ∀ x ∈ s, f₁ x = f x) : has_ftaylor_series_up_to_on n f₁ p s := begin refine ⟨λ x hx, _, h.fderiv_within, h.cont⟩, rw h₁ x hx, exact h.zero_eq x hx end lemma has_ftaylor_series_up_to_on.mono {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {t : set E} (hst : t ⊆ s) : has_ftaylor_series_up_to_on n f p t := ⟨λ x hx, h.zero_eq x (hst hx), λ m hm x hx, (h.fderiv_within m hm x (hst hx)).mono hst, λ m hm, (h.cont m hm).mono hst⟩ lemma has_ftaylor_series_up_to_on.of_le {m n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hmn : m ≤ n) : has_ftaylor_series_up_to_on m f p s := ⟨h.zero_eq, λ k hk x hx, h.fderiv_within k (lt_of_lt_of_le hk hmn) x hx, λ k hk, h.cont k (le_trans hk hmn)⟩ lemma has_ftaylor_series_up_to_on.continuous_on {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) : continuous_on f s := begin have := (h.cont 0 bot_le).congr (λ x hx, (h.zero_eq' hx).symm), rwa continuous_linear_equiv.comp_continuous_on_iff at this end lemma has_ftaylor_series_up_to_on_zero_iff : has_ftaylor_series_up_to_on 0 f p s ↔ continuous_on f s ∧ (∀ x ∈ s, (p x 0).uncurry0 = f x) := begin refine ⟨λ H, ⟨H.continuous_on, H.zero_eq⟩, λ H, ⟨H.2, λ m hm, false.elim (not_le.2 hm bot_le), _⟩⟩, assume m hm, have : (m : with_top ℕ) = ((0 : ℕ) : with_bot ℕ) := le_antisymm hm bot_le, rw with_top.coe_eq_coe at this, rw this, have : ∀ x ∈ s, p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x), by { assume x hx, rw ← H.2 x hx, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ }, rw [continuous_on_congr this, continuous_linear_equiv.comp_continuous_on_iff], exact H.1 end lemma has_ftaylor_series_up_to_on_top_iff : (has_ftaylor_series_up_to_on ⊤ f p s) ↔ (∀ (n : ℕ), has_ftaylor_series_up_to_on n f p s) := begin split, { assume H n, exact H.of_le le_top }, { assume H, split, { exact (H 0).zero_eq }, { assume m hm, apply (H m.succ).fderiv_within m (with_top.coe_lt_coe.2 (lt_add_one m)) }, { assume m hm, apply (H m).cont m (le_refl _) } } end /-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this series is a derivative of `f`. -/ lemma has_ftaylor_series_up_to_on.has_fderiv_within_at {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : x ∈ s) : has_fderiv_within_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) s x := begin have A : ∀ y ∈ s, f y = (continuous_multilinear_curry_fin0 𝕜 E F) (p y 0), { assume y hy, rw ← h.zero_eq y hy, refl }, suffices H : has_fderiv_within_at (λ y, continuous_multilinear_curry_fin0 𝕜 E F (p y 0)) (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) s x, by exact H.congr A (A x hx), rw continuous_linear_equiv.comp_has_fderiv_within_at_iff', have : ((0 : ℕ) : with_top ℕ) < n := lt_of_lt_of_le (with_top.coe_lt_coe.2 zero_lt_one) hn, convert h.fderiv_within _ this x hx, ext y v, change (p x 1) (snoc 0 y) = (p x 1) (cons y v), unfold_coes, congr, ext i, have : i = 0 := subsingleton.elim i 0, rw this, refl end lemma has_ftaylor_series_up_to_on.differentiable_on {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s := λ x hx, (h.has_fderiv_within_at hn hx).differentiable_within_at /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p` is a Taylor series up to `n`, and `p (n + 1)` is a derivative of `p n`. -/ theorem has_ftaylor_series_up_to_on_succ_iff_left {n : ℕ} : has_ftaylor_series_up_to_on (n + 1) f p s ↔ has_ftaylor_series_up_to_on n f p s ∧ (∀ x ∈ s, has_fderiv_within_at (λ y, p y n) (p x n.succ).curry_left s x) ∧ continuous_on (λ x, p x (n + 1)) s := begin split, { assume h, exact ⟨h.of_le (with_top.coe_le_coe.2 (nat.le_succ n)), h.fderiv_within _ (with_top.coe_lt_coe.2 (lt_add_one n)), h.cont (n + 1) (le_refl _)⟩ }, { assume h, split, { exact h.1.zero_eq }, { assume m hm, by_cases h' : m < n, { exact h.1.fderiv_within m (with_top.coe_lt_coe.2 h') }, { have : m = n := nat.eq_of_lt_succ_of_not_lt (with_top.coe_lt_coe.1 hm) h', rw this, exact h.2.1 } }, { assume m hm, by_cases h' : m ≤ n, { apply h.1.cont m (with_top.coe_le_coe.2 h') }, { have : m = (n + 1) := le_antisymm (with_top.coe_le_coe.1 hm) (not_le.1 h'), rw this, exact h.2.2 } } } end /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n` for `p 1`, which is a derivative of `f`. -/ theorem has_ftaylor_series_up_to_on_succ_iff_right {n : ℕ} : has_ftaylor_series_up_to_on ((n + 1) : ℕ) f p s ↔ (∀ x ∈ s, (p x 0).uncurry0 = f x) ∧ (∀ x ∈ s, has_fderiv_within_at (λ y, p y 0) (p x 1).curry_left s x) ∧ has_ftaylor_series_up_to_on n (λ x, continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) (λ x, (p x).shift) s := begin split, { assume H, refine ⟨H.zero_eq, H.fderiv_within 0 (with_top.coe_lt_coe.2 (nat.succ_pos n)), _⟩, split, { assume x hx, refl }, { assume m (hm : (m : with_top ℕ) < n) x (hx : x ∈ s), have A : (m.succ : with_top ℕ) < n.succ, by { rw with_top.coe_lt_coe at ⊢ hm, exact nat.lt_succ_iff.mpr hm }, change has_fderiv_within_at ((continuous_multilinear_curry_right_equiv 𝕜 (λ i : fin m.succ, E) F).symm ∘ (λ (y : E), p y m.succ)) (p x m.succ.succ).curry_right.curry_left s x, rw continuous_linear_equiv.comp_has_fderiv_within_at_iff', convert H.fderiv_within _ A x hx, ext y v, change (p x m.succ.succ) (snoc (cons y (init v)) (v (last _))) = (p x (nat.succ (nat.succ m))) (cons y v), rw [← cons_snoc_eq_snoc_cons, snoc_init_self] }, { assume m (hm : (m : with_top ℕ) ≤ n), have A : (m.succ : with_top ℕ) ≤ n.succ, by { rw with_top.coe_le_coe at ⊢ hm, exact nat.pred_le_iff.mp hm }, change continuous_on ((continuous_multilinear_curry_right_equiv 𝕜 (λ i : fin m.succ, E) F).symm ∘ (λ (y : E), p y m.succ)) s, rw continuous_linear_equiv.comp_continuous_on_iff, exact H.cont _ A } }, { rintros ⟨Hzero_eq, Hfderiv_zero, Htaylor⟩, split, { exact Hzero_eq }, { assume m (hm : (m : with_top ℕ) < n.succ) x (hx : x ∈ s), cases m, { exact Hfderiv_zero x hx }, { have A : (m : with_top ℕ) < n, by { rw with_top.coe_lt_coe at hm ⊢, exact nat.lt_of_succ_lt_succ hm }, have : has_fderiv_within_at ((continuous_multilinear_curry_right_equiv 𝕜 (λ i : fin m.succ, E) F).symm ∘ (λ (y : E), p y m.succ)) ((p x).shift m.succ).curry_left s x := Htaylor.fderiv_within _ A x hx, rw continuous_linear_equiv.comp_has_fderiv_within_at_iff' at this, convert this, ext y v, change (p x (nat.succ (nat.succ m))) (cons y v) = (p x m.succ.succ) (snoc (cons y (init v)) (v (last _))), rw [← cons_snoc_eq_snoc_cons, snoc_init_self] } }, { assume m (hm : (m : with_top ℕ) ≤ n.succ), cases m, { have : differentiable_on 𝕜 (λ x, p x 0) s := λ x hx, (Hfderiv_zero x hx).differentiable_within_at, exact this.continuous_on }, { have A : (m : with_top ℕ) ≤ n, by { rw with_top.coe_le_coe at hm ⊢, exact nat.lt_succ_iff.mp hm }, have : continuous_on ((continuous_multilinear_curry_right_equiv 𝕜 (λ i : fin m.succ, E) F).symm ∘ (λ (y : E), p y m.succ)) s := Htaylor.cont _ A, rwa continuous_linear_equiv.comp_continuous_on_iff at this } } } end variable (𝕜) /-- A function is continuously differentiable up to `n` if it admits derivatives within `s` up to order `n`, which are continuous. There is a subtlety on sets where derivatives are not unique, that choices of derivatives around different points might not match. To ensure that being `C^n` is a local property, we therefore require it locally around each point. There is another subtlety that one might be able to find nice derivatives up to `n` for any finite `n`, but that they don't match so that one can not find them up to infinity. To get a good notion for `n = ∞`, we only require that for any finite `n` we may find such matching derivatives. -/ definition times_cont_diff_on (n : with_top ℕ) (f : E → F) (s : set E) := ∀ (m : ℕ), (m : with_top ℕ) ≤ n → ∀ x ∈ s, ∃ u ∈ nhds_within x s, ∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to_on m f p u variable {𝕜} lemma times_cont_diff_on_nat {n : ℕ} : times_cont_diff_on 𝕜 n f s ↔ ∀ x ∈ s, ∃ u ∈ nhds_within x s, ∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to_on n f p u := begin refine ⟨λ H, H n (le_refl _), λ H m hm x hx, _⟩, rcases H x hx with ⟨u, hu, p, hp⟩, exact ⟨u, hu, p, hp.of_le hm⟩ end lemma times_cont_diff_on_top : times_cont_diff_on 𝕜 ⊤ f s ↔ ∀ (n : ℕ), times_cont_diff_on 𝕜 n f s := begin split, { assume H n m hm x hx, rcases H m le_top x hx with ⟨u, hu, p, hp⟩, exact ⟨u, hu, p, hp⟩ }, { assume H m hm x hx, rcases H m m (le_refl _) x hx with ⟨u, hu, p, hp⟩, exact ⟨u, hu, p, hp⟩ } end lemma times_cont_diff_on.continuous_on {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) : continuous_on f s := begin apply continuous_on_of_locally_continuous_on (λ x hx, _), rcases h 0 bot_le x hx with ⟨u, hu, p, H⟩, rcases mem_nhds_within.1 hu with ⟨t, t_open, xt, tu⟩, refine ⟨t, t_open, xt, _⟩, rw inter_comm at tu, exact (H.mono tu).continuous_on end lemma times_cont_diff_on.congr {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (h₁ : ∀ x ∈ s, f₁ x = f x) : times_cont_diff_on 𝕜 n f₁ s := begin assume m hm x hx, rcases h m hm x hx with ⟨u, hu, p, H⟩, refine ⟨u ∩ s, filter.inter_mem_sets hu self_mem_nhds_within, p, _⟩, exact (H.mono (inter_subset_left u s)).congr (λ x hx, h₁ x hx.2) end lemma times_cont_diff_on_congr {n : with_top ℕ} (h₁ : ∀ x ∈ s, f₁ x = f x) : times_cont_diff_on 𝕜 n f₁ s ↔ times_cont_diff_on 𝕜 n f s := ⟨λ H, H.congr (λ x hx, (h₁ x hx).symm), λ H, H.congr h₁⟩ lemma times_cont_diff_on.mono {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) {t : set E} (hst : t ⊆ s) : times_cont_diff_on 𝕜 n f t := begin assume m hm x hx, rcases h m hm x (hst hx) with ⟨u, hu, p, H⟩, exact ⟨u, nhds_within_mono x hst hu, p, H⟩ end lemma times_cont_diff_on.congr_mono {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (h₁ : ∀ x ∈ s₁, f₁ x = f x) (hs : s₁ ⊆ s) : times_cont_diff_on 𝕜 n f₁ s₁ := (hf.mono hs).congr h₁ lemma times_cont_diff_on.of_le {m n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : m ≤ n) : times_cont_diff_on 𝕜 m f s := begin assume k hk x hx, rcases h k (le_trans hk hmn) x hx with ⟨u, hu, p, H⟩, exact ⟨u, hu, p, H⟩ end /-- If a function is `C^n` on a set with `n ≥ 1`, then it is differentiable there. -/ lemma times_cont_diff_on.differentiable_on {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s := begin apply differentiable_on_of_locally_differentiable_on (λ x hx, _), rcases h 1 hn x hx with ⟨u, hu, p, H⟩, rcases mem_nhds_within.1 hu with ⟨t, t_open, xt, tu⟩, rw inter_comm at tu, exact ⟨t, t_open, xt, (H.mono tu).differentiable_on (le_refl _)⟩ end /-- If a function is `C^n` around each point in a set, then it is `C^n` on the set. -/ lemma times_cont_diff_on_of_locally_times_cont_diff_on {n : with_top ℕ} (h : ∀ x ∈ s, ∃u, is_open u ∧ x ∈ u ∧ times_cont_diff_on 𝕜 n f (s ∩ u)) : times_cont_diff_on 𝕜 n f s := begin assume m hm x hx, rcases h x hx with ⟨u, u_open, xu, Hu⟩, rcases Hu m hm x ⟨hx, xu⟩ with ⟨v, hv, p, H⟩, rw ← nhds_within_restrict s xu u_open at hv, exact ⟨v, hv, p, H⟩, end /-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/ theorem times_cont_diff_on_succ_iff_has_fderiv_within_at {n : ℕ} : times_cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔ ∀ x ∈ s, ∃ u ∈ nhds_within x s, ∃ f' : E → (E →L[𝕜] F), (∀ x ∈ u, has_fderiv_within_at f (f' x) u x) ∧ (times_cont_diff_on 𝕜 n f' u) := begin split, { assume h x hx, rcases h n.succ (le_refl _) x hx with ⟨u, hu, p, Hp⟩, refine ⟨u, hu, λ y, (continuous_multilinear_curry_fin1 𝕜 E F) (p y 1), λ y hy, Hp.has_fderiv_within_at (with_top.coe_le_coe.2 (nat.le_add_left 1 n)) hy, _⟩, rw has_ftaylor_series_up_to_on_succ_iff_right at Hp, assume m hm z hz, exact ⟨u, self_mem_nhds_within, λ (x : E), (p x).shift, Hp.2.2.of_le hm⟩ }, { assume h, rw times_cont_diff_on_nat, assume x hx, rcases h x hx with ⟨u, hu, f', f'_eq_deriv, Hf'⟩, have xu : x ∈ u := mem_of_mem_nhds_within hx hu, rcases Hf' n (le_refl _) x xu with ⟨v, hv, p', Hp'⟩, refine ⟨v ∩ u, filter.inter_mem_sets (nhds_within_le_of_mem hu hv) hu, λ x, (p' x).unshift (f x), _⟩, rw has_ftaylor_series_up_to_on_succ_iff_right, refine ⟨λ y hy, rfl, λ y hy, _, _⟩, { change has_fderiv_within_at (λ (z : E), (continuous_multilinear_curry_fin0 𝕜 E F).symm (f z)) ((formal_multilinear_series.unshift (p' y) (f y) 1).curry_left) (v ∩ u) y, rw continuous_linear_equiv.comp_has_fderiv_within_at_iff', convert (f'_eq_deriv y hy.2).mono (inter_subset_right v u), rw ← Hp'.zero_eq y hy.1, ext z, change ((p' y 0) (init (@cons 0 (λ i, E) z 0))) (@cons 0 (λ i, E) z 0 (last 0)) = ((p' y 0) 0) z, unfold_coes, congr }, { convert (Hp'.mono (inter_subset_left v u)).congr (λ x hx, Hp'.zero_eq x hx.1), { ext x y, change p' x 0 (init (@snoc 0 (λ i : fin 1, E) 0 y)) y = p' x 0 0 y, rw init_snoc }, { ext x k v y, change p' x k (init (@snoc k (λ i : fin k.succ, E) v y)) (@snoc k (λ i : fin k.succ, E) v y (last k)) = p' x k v y, rw [snoc_last, init_snoc] } } } end /-! ### Iterated derivative within a set -/ variable (𝕜) /-- The `n`-th derivative of a function along a set, defined inductively by saying that the `n+1`-th derivative of `f` is the derivative of the `n`-th derivative of `f` along this set, together with an uncurrying step to see it as a multilinear map in `n+1` variables.. -/ noncomputable def iterated_fderiv_within (n : ℕ) (f : E → F) (s : set E) : E → (E [×n]→L[𝕜] F) := nat.rec_on n (λ x, continuous_multilinear_map.curry0 𝕜 E (f x)) (λ n rec x, continuous_linear_map.uncurry_left (fderiv_within 𝕜 rec s x)) /-- Formal Taylor series associated to a function within a set. -/ def ftaylor_series_within (f : E → F) (s : set E) (x : E) : formal_multilinear_series 𝕜 E F := λ n, iterated_fderiv_within 𝕜 n f s x variable {𝕜} @[simp] lemma iterated_fderiv_within_zero_apply (m : (fin 0) → E) : (iterated_fderiv_within 𝕜 0 f s x : ((fin 0) → E) → F) m = f x := rfl lemma iterated_fderiv_within_zero_eq_comp : iterated_fderiv_within 𝕜 0 f s = (continuous_multilinear_curry_fin0 𝕜 E F).symm ∘ f := rfl lemma iterated_fderiv_within_succ_apply_left {n : ℕ} (m : fin (n + 1) → E): (iterated_fderiv_within 𝕜 (n + 1) f s x : (fin (n + 1) → E) → F) m = (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) := rfl /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the derivative of the `n`-th derivative. -/ lemma iterated_fderiv_within_succ_eq_comp_left {n : ℕ} : iterated_fderiv_within 𝕜 (n + 1) f s = (continuous_multilinear_curry_left_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s) := rfl theorem iterated_fderiv_within_succ_apply_right {n : ℕ} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : fin (n + 1) → E) : (iterated_fderiv_within 𝕜 (n + 1) f s x : (fin (n + 1) → E) → F) m = iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s x (init m) (m (last n)) := begin induction n with n IH generalizing x, { rw [iterated_fderiv_within_succ_eq_comp_left, iterated_fderiv_within_zero_eq_comp, iterated_fderiv_within_zero_apply, function.comp_apply, continuous_linear_equiv.comp_fderiv_within _ (hs x hx)], refl }, { let I := (continuous_multilinear_curry_right_equiv 𝕜 (λ (i : fin (n + 1)), E) F), have A : ∀ y ∈ s, iterated_fderiv_within 𝕜 n.succ f s y = (I ∘ (iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s)) y, by { assume y hy, ext m, rw @IH m y hy, refl }, calc (iterated_fderiv_within 𝕜 (n+2) f s x : (fin (n+2) → E) → F) m = (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n.succ f s) s x : E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : rfl ... = (fderiv_within 𝕜 (I ∘ (iterated_fderiv_within 𝕜 n (fderiv_within 𝕜 f s) s)) s x : E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : by rw fderiv_within_congr (hs x hx) A (A x hx) ... = (I ∘ fderiv_within 𝕜 ((iterated_fderiv_within 𝕜 n (fderiv_within 𝕜 f s) s)) s x : E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : by { rw continuous_linear_equiv.comp_fderiv_within _ (hs x hx), refl } ... = (fderiv_within 𝕜 ((iterated_fderiv_within 𝕜 n (λ y, fderiv_within 𝕜 f s y) s)) s x : E → (E [×n]→L[𝕜] (E →L[𝕜] F))) (m 0) (init (tail m)) ((tail m) (last n)) : rfl ... = iterated_fderiv_within 𝕜 (nat.succ n) (λ y, fderiv_within 𝕜 f s y) s x (init m) (m (last (n + 1))) : by { rw [iterated_fderiv_within_succ_apply_left, tail_init_eq_init_tail], refl } } end /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the `n`-th derivative of the derivative. -/ lemma iterated_fderiv_within_succ_eq_comp_right {n : ℕ} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) : iterated_fderiv_within 𝕜 (n + 1) f s x = ((continuous_multilinear_curry_right_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s)) x := by { ext m, rw iterated_fderiv_within_succ_apply_right hs hx, refl } @[simp] lemma iterated_fderiv_within_one_apply (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : (fin 1) → E) : (iterated_fderiv_within 𝕜 1 f s x : ((fin 1) → E) → F) m = (fderiv_within 𝕜 f s x : E → F) (m 0) := by { rw [iterated_fderiv_within_succ_apply_right hs hx, iterated_fderiv_within_zero_apply], refl } /-- If two functions coincide on a set `s` of unique differentiability, then their iterated differentials within this set coincide. -/ lemma iterated_fderiv_within_congr {n : ℕ} (hs : unique_diff_on 𝕜 s) (hL : ∀y∈s, f₁ y = f y) (hx : x ∈ s) : iterated_fderiv_within 𝕜 n f₁ s x = iterated_fderiv_within 𝕜 n f s x := begin induction n with n IH generalizing x, { ext m, simp [hL x hx] }, { have : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f₁ s y) s x = fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) s x := fderiv_within_congr (hs x hx) (λ y hy, IH hy) (IH hx), ext m, rw [iterated_fderiv_within_succ_apply_left, iterated_fderiv_within_succ_apply_left, this] } end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with an open set containing `x`. -/ lemma iterated_fderiv_within_inter_open {n : ℕ} (hu : is_open u) (hs : unique_diff_on 𝕜 (s ∩ u)) (hx : x ∈ s ∩ u) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := begin induction n with n IH generalizing x, { ext m, simp }, { have A : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f (s ∩ u) y) (s ∩ u) x = fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) (s ∩ u) x := fderiv_within_congr (hs x hx) (λ y hy, IH hy) (IH hx), have B : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) (s ∩ u) x = fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) s x := fderiv_within_inter (mem_nhds_sets hu hx.2) ((unique_diff_within_at_inter (mem_nhds_sets hu hx.2)).1 (hs x hx)), ext m, rw [iterated_fderiv_within_succ_apply_left, iterated_fderiv_within_succ_apply_left, A, B] } end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with a neighborhood of `x` within `s`. -/ lemma iterated_fderiv_within_inter' {n : ℕ} (hu : u ∈ nhds_within x s) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := begin obtain ⟨v, v_open, xv, vu⟩ : ∃ v, is_open v ∧ x ∈ v ∧ v ∩ s ⊆ u := mem_nhds_within.1 hu, have A : (s ∩ u) ∩ v = s ∩ v, { apply subset.antisymm (inter_subset_inter (inter_subset_left _ _) (subset.refl _)), exact λ y ⟨ys, yv⟩, ⟨⟨ys, vu ⟨yv, ys⟩⟩, yv⟩ }, have : iterated_fderiv_within 𝕜 n f (s ∩ v) x = iterated_fderiv_within 𝕜 n f s x := iterated_fderiv_within_inter_open v_open (hs.inter v_open) ⟨xs, xv⟩, rw ← this, have : iterated_fderiv_within 𝕜 n f ((s ∩ u) ∩ v) x = iterated_fderiv_within 𝕜 n f (s ∩ u) x, { refine iterated_fderiv_within_inter_open v_open _ ⟨⟨xs, vu ⟨xv, xs⟩⟩, xv⟩, rw A, exact hs.inter v_open }, rw A at this, rw ← this end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with a neighborhood of `x`. -/ lemma iterated_fderiv_within_inter {n : ℕ} (hu : u ∈ nhds x) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) : iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x := iterated_fderiv_within_inter' (mem_nhds_within_of_mem_nhds hu) hs xs @[simp] lemma times_cont_diff_on_zero : times_cont_diff_on 𝕜 0 f s ↔ continuous_on f s := begin refine ⟨λ H, H.continuous_on, λ H, _⟩, assume m hm x hx, have : (m : with_top ℕ) = 0 := le_antisymm hm bot_le, rw this, refine ⟨s, self_mem_nhds_within, ftaylor_series_within 𝕜 f s, _⟩, rw has_ftaylor_series_up_to_on_zero_iff, exact ⟨H, λ x hx, by simp [ftaylor_series_within]⟩ end /-- On a set with unique differentiability, any choice of iterated differential has to coincide with the one we have chosen in `iterated_fderiv_within 𝕜 m f s`. -/ theorem has_ftaylor_series_up_to_on.eq_ftaylor_series_of_unique_diff_on {n : with_top ℕ} (h : has_ftaylor_series_up_to_on n f p s) {m : ℕ} (hmn : (m : with_top ℕ) ≤ n) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) : p x m = iterated_fderiv_within 𝕜 m f s x := begin induction m with m IH generalizing x, { rw [h.zero_eq' hx, iterated_fderiv_within_zero_eq_comp] }, { have A : (m : with_top ℕ) < n := lt_of_lt_of_le (with_top.coe_lt_coe.2 (lt_add_one m)) hmn, have : has_fderiv_within_at (λ (y : E), iterated_fderiv_within 𝕜 m f s y) (continuous_multilinear_map.curry_left (p x (nat.succ m))) s x := (h.fderiv_within m A x hx).congr (λ y hy, (IH (le_of_lt A) hy).symm) (IH (le_of_lt A) hx).symm, rw [iterated_fderiv_within_succ_eq_comp_left, function.comp_apply, this.fderiv_within (hs x hx)], exact (continuous_multilinear_map.uncurry_curry_left _).symm } end /-- When a function is `C^n` in a set `s` of unique differentiability, it admits `ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/ theorem times_cont_diff_on.ftaylor_series_within {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) : has_ftaylor_series_up_to_on n f (ftaylor_series_within 𝕜 f s) s := begin split, { assume x hx, simp only [ftaylor_series_within, continuous_multilinear_map.uncurry0_apply, iterated_fderiv_within_zero_apply] }, { assume m hm x hx, rcases h m.succ (with_top.add_one_le_of_lt hm) x hx with ⟨u, hu, p, Hp⟩, rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩, rw inter_comm at ho, have : p x m.succ = ftaylor_series_within 𝕜 f s x m.succ, { change p x m.succ = iterated_fderiv_within 𝕜 m.succ f s x, rw ← iterated_fderiv_within_inter (mem_nhds_sets o_open xo) hs hx, exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (le_refl _) (hs.inter o_open) ⟨hx, xo⟩ }, rw [← this, ← has_fderiv_within_at_inter (mem_nhds_sets o_open xo)], have A : ∀ y ∈ s ∩ o, p y m = ftaylor_series_within 𝕜 f s y m, { rintros y ⟨hy, yo⟩, change p y m = iterated_fderiv_within 𝕜 m f s y, rw ← iterated_fderiv_within_inter (mem_nhds_sets o_open yo) hs hy, exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (with_top.coe_le_coe.2 (nat.le_succ m)) (hs.inter o_open) ⟨hy, yo⟩ }, exact ((Hp.mono ho).fderiv_within m (with_top.coe_lt_coe.2 (lt_add_one m)) x ⟨hx, xo⟩).congr (λ y hy, (A y hy).symm) (A x ⟨hx, xo⟩).symm }, { assume m hm, apply continuous_on_of_locally_continuous_on, assume x hx, rcases h m hm x hx with ⟨u, hu, p, Hp⟩, rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩, rw inter_comm at ho, refine ⟨o, o_open, xo, _⟩, have A : ∀ y ∈ s ∩ o, p y m = ftaylor_series_within 𝕜 f s y m, { rintros y ⟨hy, yo⟩, change p y m = iterated_fderiv_within 𝕜 m f s y, rw ← iterated_fderiv_within_inter (mem_nhds_sets o_open yo) hs hy, exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (le_refl _) (hs.inter o_open) ⟨hy, yo⟩ }, exact ((Hp.mono ho).cont m (le_refl _)).congr (λ y hy, (A y hy).symm) } end lemma times_cont_diff_on_of_continuous_on_differentiable_on {n : with_top ℕ} (Hcont : ∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous_on (λ x, iterated_fderiv_within 𝕜 m f s x) s) (Hdiff : ∀ (m : ℕ), (m : with_top ℕ) < n → differentiable_on 𝕜 (λ x, iterated_fderiv_within 𝕜 m f s x) s) : times_cont_diff_on 𝕜 n f s := begin assume m hm x hx, refine ⟨s, self_mem_nhds_within, ftaylor_series_within 𝕜 f s, _⟩, split, { assume x hx, simp only [ftaylor_series_within, continuous_multilinear_map.uncurry0_apply, iterated_fderiv_within_zero_apply] }, { assume k hk x hx, convert (Hdiff k (lt_of_lt_of_le hk hm) x hx).has_fderiv_within_at, simp only [ftaylor_series_within, iterated_fderiv_within_succ_eq_comp_left, continuous_linear_equiv.coe_apply, function.comp_app, coe_fn_coe_base], exact continuous_linear_map.curry_uncurry_left _ }, { assume k hk, exact Hcont k (le_trans hk hm) } end lemma times_cont_diff_on_of_differentiable_on {n : with_top ℕ} (h : ∀(m : ℕ), (m : with_top ℕ) ≤ n → differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s) : times_cont_diff_on 𝕜 n f s := times_cont_diff_on_of_continuous_on_differentiable_on (λ m hm, (h m hm).continuous_on) (λ m hm, (h m (le_of_lt hm))) lemma times_cont_diff_on.continuous_on_iterated_fderiv_within {n : with_top ℕ} {m : ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : (m : with_top ℕ) ≤ n) (hs : unique_diff_on 𝕜 s) : continuous_on (iterated_fderiv_within 𝕜 m f s) s := (h.ftaylor_series_within hs).cont m hmn lemma times_cont_diff_on.differentiable_on_iterated_fderiv_within {n : with_top ℕ} {m : ℕ} (h : times_cont_diff_on 𝕜 n f s) (hmn : (m : with_top ℕ) < n) (hs : unique_diff_on 𝕜 s) : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s := λ x hx, ((h.ftaylor_series_within hs).fderiv_within m hmn x hx).differentiable_within_at lemma times_cont_diff_on_iff_continuous_on_differentiable_on {n : with_top ℕ} (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 n f s ↔ (∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous_on (λ x, iterated_fderiv_within 𝕜 m f s x) s) ∧ (∀ (m : ℕ), (m : with_top ℕ) < n → differentiable_on 𝕜 (λ x, iterated_fderiv_within 𝕜 m f s x) s) := begin split, { assume h, split, { assume m hm, exact h.continuous_on_iterated_fderiv_within hm hs }, { assume m hm, exact h.differentiable_on_iterated_fderiv_within hm hs } }, { assume h, exact times_cont_diff_on_of_continuous_on_differentiable_on h.1 h.2 } end /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^n`. -/ theorem times_cont_diff_on_succ_iff_fderiv_within {n : ℕ} (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 n (λ y, fderiv_within 𝕜 f s y) s := begin split, { assume H, refine ⟨H.differentiable_on (with_top.coe_le_coe.2 (nat.le_add_left 1 n)), _⟩, apply times_cont_diff_on_of_locally_times_cont_diff_on, assume x hx, rcases times_cont_diff_on_succ_iff_has_fderiv_within_at.1 H x hx with ⟨u, hu, f', hff', hf'⟩, rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩, rw inter_comm at ho, refine ⟨o, o_open, xo, _⟩, apply (hf'.mono ho).congr (λ y hy, _), have A : fderiv_within 𝕜 f (s ∩ o) y = f' y := ((hff' y (ho hy)).mono ho).fderiv_within (hs.inter o_open y hy), rwa fderiv_within_inter (mem_nhds_sets o_open hy.2) (hs y hy.1) at A }, { rw times_cont_diff_on_succ_iff_has_fderiv_within_at, rintros ⟨hdiff, h⟩ x hx, exact ⟨s, self_mem_nhds_within, fderiv_within 𝕜 f s, λ x hx, (hdiff x hx).has_fderiv_within_at, h⟩ } end /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^∞`. -/ theorem times_cont_diff_on_top_iff_fderiv_within (hs : unique_diff_on 𝕜 s) : times_cont_diff_on 𝕜 ⊤ f s ↔ differentiable_on 𝕜 f s ∧ times_cont_diff_on 𝕜 ⊤ (λ y, fderiv_within 𝕜 f s y) s := begin split, { assume h, refine ⟨h.differentiable_on le_top, _⟩, apply times_cont_diff_on_top.2 (λ n, ((times_cont_diff_on_succ_iff_fderiv_within hs).1 _).2), exact h.of_le le_top }, { assume h, refine times_cont_diff_on_top.2 (λ n, _), have A : (n : with_top ℕ) ≤ ⊤ := le_top, apply ((times_cont_diff_on_succ_iff_fderiv_within hs).2 ⟨h.1, h.2.of_le A⟩).of_le, exact with_top.coe_le_coe.2 (nat.le_succ n) } end lemma times_cont_diff_on.fderiv_within {m n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (λ y, fderiv_within 𝕜 f s y) s := begin cases m, { change ⊤ + 1 ≤ n at hmn, have : n = ⊤, by simpa using hmn, rw this at hf, exact ((times_cont_diff_on_top_iff_fderiv_within hs).1 hf).2 }, { change (m.succ : with_top ℕ) ≤ n at hmn, exact ((times_cont_diff_on_succ_iff_fderiv_within hs).1 (hf.of_le hmn)).2 } end lemma times_cont_diff_on.continuous_on_fderiv_within {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) : continuous_on (λ x, fderiv_within 𝕜 f s x) s := ((times_cont_diff_on_succ_iff_fderiv_within hs).1 (h.of_le hn)).2.continuous_on /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ lemma times_cont_diff_on.continuous_on_fderiv_within_apply {n : with_top ℕ} (h : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) : continuous_on (λp : E × E, (fderiv_within 𝕜 f s p.1 : E → F) p.2) (set.prod s univ) := begin have A : continuous (λq : (E →L[𝕜] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous_on (λp : E × E, (fderiv_within 𝕜 f s p.1, p.2)) (set.prod s univ), { apply continuous_on.prod _ continuous_snd.continuous_on, exact continuous_on.comp (h.continuous_on_fderiv_within hs hn) continuous_fst.continuous_on (prod_subset_preimage_fst _ _) }, exact A.comp_continuous_on B end /-- `has_ftaylor_series_up_to n f p` registers the fact that `p 0 = f` and `p (m+1)` is a derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to `has_fderiv_at` but for higher order derivatives. -/ structure has_ftaylor_series_up_to (n : with_top ℕ) (f : E → F) (p : E → formal_multilinear_series 𝕜 E F) : Prop := (zero_eq : ∀ x, (p x 0).uncurry0 = f x) (fderiv : ∀ (m : ℕ) (hm : (m : with_top ℕ) < n), ∀ x, has_fderiv_at (λ y, p y m) (p x m.succ).curry_left x) (cont : ∀ (m : ℕ) (hm : (m : with_top ℕ) ≤ n), continuous (λ x, p x m)) lemma has_ftaylor_series_up_to.zero_eq' {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (x : E) : p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x) := by { rw ← h.zero_eq x, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ } lemma has_ftaylor_series_up_to_on_univ_iff {n : with_top ℕ} : has_ftaylor_series_up_to_on n f p univ ↔ has_ftaylor_series_up_to n f p := begin split, { assume H, split, { exact λ x, H.zero_eq x (mem_univ x) }, { assume m hm x, rw ← has_fderiv_within_at_univ, exact H.fderiv_within m hm x (mem_univ x) }, { assume m hm, rw continuous_iff_continuous_on_univ, exact H.cont m hm } }, { assume H, split, { exact λ x hx, H.zero_eq x }, { assume m hm x hx, rw has_fderiv_within_at_univ, exact H.fderiv m hm x }, { assume m hm, rw ← continuous_iff_continuous_on_univ, exact H.cont m hm } } end lemma has_ftaylor_series_up_to.has_ftaylor_series_up_to_on {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (s : set E) : has_ftaylor_series_up_to_on n f p s := (has_ftaylor_series_up_to_on_univ_iff.2 h).mono (subset_univ _) lemma has_ftaylor_series_up_to.of_le {m n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hmn : m ≤ n) : has_ftaylor_series_up_to m f p := by { rw ← has_ftaylor_series_up_to_on_univ_iff at h ⊢, exact h.of_le hmn } lemma has_ftaylor_series_up_to.continuous {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) : continuous f := begin rw ← has_ftaylor_series_up_to_on_univ_iff at h, rw continuous_iff_continuous_on_univ, exact h.continuous_on end lemma has_ftaylor_series_up_to_zero_iff : has_ftaylor_series_up_to 0 f p ↔ continuous f ∧ (∀ x, (p x 0).uncurry0 = f x) := by simp [has_ftaylor_series_up_to_on_univ_iff.symm, continuous_iff_continuous_on_univ, has_ftaylor_series_up_to_on_zero_iff] /-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this series is a derivative of `f`. -/ lemma has_ftaylor_series_up_to.has_fderiv_at {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) (x : E) : has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) x := begin rw [← has_fderiv_within_at_univ], exact (has_ftaylor_series_up_to_on_univ_iff.2 h).has_fderiv_within_at hn (mem_univ _) end lemma has_ftaylor_series_up_to.differentiable {n : with_top ℕ} (h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) : differentiable 𝕜 f := λ x, (h.has_fderiv_at hn x).differentiable_at /-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n` for `p 1`, which is a derivative of `f`. -/ theorem has_ftaylor_series_up_to_succ_iff_right {n : ℕ} : has_ftaylor_series_up_to ((n + 1) : ℕ) f p ↔ (∀ x, (p x 0).uncurry0 = f x) ∧ (∀ x, has_fderiv_at (λ y, p y 0) (p x 1).curry_left x) ∧ has_ftaylor_series_up_to n (λ x, continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) (λ x, (p x).shift) := by simp [has_ftaylor_series_up_to_on_succ_iff_right, has_ftaylor_series_up_to_on_univ_iff.symm, -add_comm, -with_bot.coe_add] variable (𝕜) /-- A function is continuously differentiable up to `n` if it admits derivatives up to order `n`, which are continuous. Contrary to the case of definitions in domains (where derivatives might not be unique) we do not need to localize the definition in space or time. -/ definition times_cont_diff (n : with_top ℕ) (f : E → F) := ∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to n f p variable {𝕜} theorem times_cont_diff_on_univ {n : with_top ℕ} : times_cont_diff_on 𝕜 n f univ ↔ times_cont_diff 𝕜 n f := begin split, { assume H, use ftaylor_series_within 𝕜 f univ, rw ← has_ftaylor_series_up_to_on_univ_iff, exact H.ftaylor_series_within unique_diff_on_univ }, { rintros ⟨p, hp⟩ m hm x hx, exact ⟨univ, self_mem_nhds_within, p, (hp.has_ftaylor_series_up_to_on univ).of_le hm⟩ } end lemma times_cont_diff_top : times_cont_diff 𝕜 ⊤ f ↔ ∀ (n : ℕ), times_cont_diff 𝕜 n f := by simp [times_cont_diff_on_univ.symm, times_cont_diff_on_top] lemma times_cont_diff.times_cont_diff_on {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : times_cont_diff_on 𝕜 n f s := (times_cont_diff_on_univ.2 h).mono (subset_univ _) @[simp] lemma times_cont_diff_zero : times_cont_diff 𝕜 0 f ↔ continuous f := begin rw [← times_cont_diff_on_univ, continuous_iff_continuous_on_univ], exact times_cont_diff_on_zero end lemma times_cont_diff.of_le {m n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hmn : m ≤ n) : times_cont_diff 𝕜 m f := times_cont_diff_on_univ.1 $ (times_cont_diff_on_univ.2 h).of_le hmn lemma times_cont_diff.continuous {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) : continuous f := times_cont_diff_zero.1 (h.of_le bot_le) /-- If a function is `C^n` with `n ≥ 1`, then it is differentiable. -/ lemma times_cont_diff.differentiable {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : differentiable 𝕜 f := differentiable_on_univ.1 $ (times_cont_diff_on_univ.2 h).differentiable_on hn variable (𝕜) /-! ### Iterated derivative -/ /-- The `n`-th derivative of a function, as a multilinear map, defined inductively. -/ noncomputable def iterated_fderiv (n : ℕ) (f : E → F) : E → (E [×n]→L[𝕜] F) := nat.rec_on n (λ x, continuous_multilinear_map.curry0 𝕜 E (f x)) (λ n rec x, continuous_linear_map.uncurry_left (fderiv 𝕜 rec x)) /-- Formal Taylor series associated to a function within a set. -/ def ftaylor_series (f : E → F) (x : E) : formal_multilinear_series 𝕜 E F := λ n, iterated_fderiv 𝕜 n f x variable {𝕜} @[simp] lemma iterated_fderiv_zero_apply (m : (fin 0) → E) : (iterated_fderiv 𝕜 0 f x : ((fin 0) → E) → F) m = f x := rfl lemma iterated_fderiv_zero_eq_comp : iterated_fderiv 𝕜 0 f = (continuous_multilinear_curry_fin0 𝕜 E F).symm ∘ f := rfl lemma iterated_fderiv_succ_apply_left {n : ℕ} (m : fin (n + 1) → E): (iterated_fderiv 𝕜 (n + 1) f x : (fin (n + 1) → E) → F) m = (fderiv 𝕜 (iterated_fderiv 𝕜 n f) x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) := rfl /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the derivative of the `n`-th derivative. -/ lemma iterated_fderiv_succ_eq_comp_left {n : ℕ} : iterated_fderiv 𝕜 (n + 1) f = (continuous_multilinear_curry_left_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (fderiv 𝕜 (iterated_fderiv 𝕜 n f)) := rfl lemma iterated_fderiv_within_univ {n : ℕ} : iterated_fderiv_within 𝕜 n f univ = iterated_fderiv 𝕜 n f := begin induction n with n IH, { ext x, simp }, { ext x m, rw [iterated_fderiv_succ_apply_left, iterated_fderiv_within_succ_apply_left, IH, fderiv_within_univ] } end lemma ftaylor_series_within_univ : ftaylor_series_within 𝕜 f univ = ftaylor_series 𝕜 f := begin ext1 x, ext1 n, change iterated_fderiv_within 𝕜 n f univ x = iterated_fderiv 𝕜 n f x, rw iterated_fderiv_within_univ end theorem iterated_fderiv_succ_apply_right {n : ℕ} (m : fin (n + 1) → E) : (iterated_fderiv 𝕜 (n + 1) f x : (fin (n + 1) → E) → F) m = iterated_fderiv 𝕜 n (λy, fderiv 𝕜 f y) x (init m) (m (last n)) := begin rw [← iterated_fderiv_within_univ, ← iterated_fderiv_within_univ, ← fderiv_within_univ], exact iterated_fderiv_within_succ_apply_right unique_diff_on_univ (mem_univ _) _ end /-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv, and the `n`-th derivative of the derivative. -/ lemma iterated_fderiv_succ_eq_comp_right {n : ℕ} : iterated_fderiv 𝕜 (n + 1) f x = ((continuous_multilinear_curry_right_equiv 𝕜 (λ(i : fin (n + 1)), E) F) ∘ (iterated_fderiv 𝕜 n (λy, fderiv 𝕜 f y))) x := by { ext m, rw iterated_fderiv_succ_apply_right, refl } @[simp] lemma iterated_fderiv_one_apply (m : (fin 1) → E) : (iterated_fderiv 𝕜 1 f x : ((fin 1) → E) → F) m = (fderiv 𝕜 f x : E → F) (m 0) := by { rw [iterated_fderiv_succ_apply_right, iterated_fderiv_zero_apply], refl } /-- When a function is `C^n` in a set `s` of unique differentiability, it admits `ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/ theorem times_cont_diff_on_iff_ftaylor_series {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ has_ftaylor_series_up_to n f (ftaylor_series 𝕜 f) := begin split, { rw [← times_cont_diff_on_univ, ← has_ftaylor_series_up_to_on_univ_iff, ← ftaylor_series_within_univ], exact λ h, times_cont_diff_on.ftaylor_series_within h unique_diff_on_univ }, { assume h, exact ⟨ftaylor_series 𝕜 f, h⟩ } end lemma times_cont_diff_iff_continuous_differentiable {n : with_top ℕ} : times_cont_diff 𝕜 n f ↔ (∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous (λ x, iterated_fderiv 𝕜 m f x)) ∧ (∀ (m : ℕ), (m : with_top ℕ) < n → differentiable 𝕜 (λ x, iterated_fderiv 𝕜 m f x)) := by simp [times_cont_diff_on_univ.symm, continuous_iff_continuous_on_univ, differentiable_on_univ.symm, iterated_fderiv_within_univ, times_cont_diff_on_iff_continuous_on_differentiable_on unique_diff_on_univ] lemma times_cont_diff_of_differentiable_iterated_fderiv {n : with_top ℕ} (h : ∀(m : ℕ), (m : with_top ℕ) ≤ n → differentiable 𝕜 (iterated_fderiv 𝕜 m f)) : times_cont_diff 𝕜 n f := times_cont_diff_iff_continuous_differentiable.2 ⟨λ m hm, (h m hm).continuous, λ m hm, (h m (le_of_lt hm))⟩ /-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^n`. -/ theorem times_cont_diff_succ_iff_fderiv {n : ℕ} : times_cont_diff 𝕜 ((n + 1) : ℕ) f ↔ differentiable 𝕜 f ∧ times_cont_diff 𝕜 n (λ y, fderiv 𝕜 f y) := by simp [times_cont_diff_on_univ.symm, differentiable_on_univ.symm, fderiv_within_univ.symm, - fderiv_within_univ, times_cont_diff_on_succ_iff_fderiv_within unique_diff_on_univ, -with_bot.coe_add, -add_comm] /-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable there, and its derivative is `C^∞`. -/ theorem times_cont_diff_top_iff_fderiv : times_cont_diff 𝕜 ⊤ f ↔ differentiable 𝕜 f ∧ times_cont_diff 𝕜 ⊤ (λ y, fderiv 𝕜 f y) := begin simp [times_cont_diff_on_univ.symm, differentiable_on_univ.symm, fderiv_within_univ.symm, - fderiv_within_univ], rw times_cont_diff_on_top_iff_fderiv_within unique_diff_on_univ, end lemma times_cont_diff.continuous_fderiv {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : continuous (λ x, fderiv 𝕜 f x) := ((times_cont_diff_succ_iff_fderiv).1 (h.of_le hn)).2.continuous /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ lemma times_cont_diff.continuous_fderiv_apply {n : with_top ℕ} (h : times_cont_diff 𝕜 n f) (hn : 1 ≤ n) : continuous (λp : E × E, (fderiv 𝕜 f p.1 : E → F) p.2) := begin have A : continuous (λq : (E →L[𝕜] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous (λp : E × E, (fderiv 𝕜 f p.1, p.2)), { apply continuous.prod_mk _ continuous_snd, exact continuous.comp (h.continuous_fderiv hn) continuous_fst }, exact A.comp B end /-! ### Constants -/ lemma iterated_fderiv_within_zero_fun {n : ℕ} : iterated_fderiv 𝕜 n (λ x : E, (0 : F)) = 0 := begin induction n with n IH, { ext m, simp }, { ext x m, rw [iterated_fderiv_succ_apply_left, IH], change (fderiv 𝕜 (λ (x : E), (0 : (E [×n]→L[𝕜] F))) x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) = _, rw fderiv_const, refl } end lemma times_cont_diff_zero_fun {n : with_top ℕ} : times_cont_diff 𝕜 n (λ x : E, (0 : F)) := begin apply times_cont_diff_of_differentiable_iterated_fderiv (λm hm, _), rw iterated_fderiv_within_zero_fun, apply differentiable_const (0 : (E [×m]→L[𝕜] F)) end /-- Constants are `C^∞`. -/ lemma times_cont_diff_const {n : with_top ℕ} {c : F} : times_cont_diff 𝕜 n (λx : E, c) := begin suffices h : times_cont_diff 𝕜 ⊤ (λx : E, c), by exact h.of_le le_top, rw times_cont_diff_top_iff_fderiv, refine ⟨differentiable_const c, _⟩, rw fderiv_const, exact times_cont_diff_zero_fun end lemma times_cont_diff_on_const {n : with_top ℕ} {c : F} {s : set E} : times_cont_diff_on 𝕜 n (λx : E, c) s := times_cont_diff_const.times_cont_diff_on /-! ### Linear functions -/ /-- Unbundled bounded linear functions are `C^∞`. -/ lemma is_bounded_linear_map.times_cont_diff {n : with_top ℕ} (hf : is_bounded_linear_map 𝕜 f) : times_cont_diff 𝕜 n f := begin suffices h : times_cont_diff 𝕜 ⊤ f, by exact h.of_le le_top, rw times_cont_diff_top_iff_fderiv, refine ⟨hf.differentiable, _⟩, simp [hf.fderiv], exact times_cont_diff_const end lemma continuous_linear_map.times_cont_diff {n : with_top ℕ} (f : E →L[𝕜] F) : times_cont_diff 𝕜 n f := f.is_bounded_linear_map.times_cont_diff /-- The first projection in a product is `C^∞`. -/ lemma times_cont_diff_fst {n : with_top ℕ} : times_cont_diff 𝕜 n (prod.fst : E × F → E) := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.fst /-- The second projection in a product is `C^∞`. -/ lemma times_cont_diff_snd {n : with_top ℕ} : times_cont_diff 𝕜 n (prod.snd : E × F → F) := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.snd /-- The identity is `C^∞`. -/ lemma times_cont_diff_id {n : with_top ℕ} : times_cont_diff 𝕜 n (id : E → E) := is_bounded_linear_map.id.times_cont_diff /-- Bilinear functions are `C^∞`. -/ lemma is_bounded_bilinear_map.times_cont_diff {n : with_top ℕ} (hb : is_bounded_bilinear_map 𝕜 b) : times_cont_diff 𝕜 n b := begin suffices h : times_cont_diff 𝕜 ⊤ b, by exact h.of_le le_top, rw times_cont_diff_top_iff_fderiv, refine ⟨hb.differentiable, _⟩, simp [hb.fderiv], exact hb.is_bounded_linear_map_deriv.times_cont_diff end /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `g ∘ f` admits a Taylor series whose `k`-th term is given by `g ∘ (p k)`. -/ lemma has_ftaylor_series_up_to_on.continuous_linear_map_comp {n : with_top ℕ} (g : F →L[𝕜] G) (hf : has_ftaylor_series_up_to_on n f p s) : has_ftaylor_series_up_to_on n (g ∘ f) (λ x k, g.comp_continuous_multilinear_map (p x k)) s := begin split, { assume x hx, simp [(hf.zero_eq x hx).symm] }, { assume m hm x hx, let A : (E [×m]→L[𝕜] F) → (E [×m]→L[𝕜] G) := λ f, g.comp_continuous_multilinear_map f, have hA : is_bounded_linear_map 𝕜 A := is_bounded_bilinear_map_comp_multilinear.is_bounded_linear_map_right _, have := hf.fderiv_within m hm x hx, convert has_fderiv_at.comp_has_fderiv_within_at x (hA.has_fderiv_at) this }, { assume m hm, let A : (E [×m]→L[𝕜] F) → (E [×m]→L[𝕜] G) := λ f, g.comp_continuous_multilinear_map f, have hA : is_bounded_linear_map 𝕜 A := is_bounded_bilinear_map_comp_multilinear.is_bounded_linear_map_right _, exact hA.continuous.comp_continuous_on (hf.cont m hm) } end /-- Composition by continuous linear maps on the left preserves `C^n` functions on domains. -/ lemma times_cont_diff_on.continuous_linear_map_comp {n : with_top ℕ} (g : F →L[𝕜] G) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) s := begin assume m hm x hx, rcases hf m hm x hx with ⟨u, hu, p, hp⟩, exact ⟨u, hu, _, hp.continuous_linear_map_comp g⟩, end /-- Composition by continuous linear maps on the left preserves `C^n` functions. -/ lemma times_cont_diff.continuous_linear_map_comp {n : with_top ℕ} {f : E → F} (g : F →L[𝕜] G) (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (λx, g (f x)) := times_cont_diff_on_univ.1 $ times_cont_diff_on.continuous_linear_map_comp _ (times_cont_diff_on_univ.2 hf) /-- Composition by continuous linear equivs on the left respects higher differentiability on domains. -/ lemma continuous_linear_equiv.comp_times_cont_diff_on_iff {n : with_top ℕ} (e : F ≃L[𝕜] G) : times_cont_diff_on 𝕜 n (e ∘ f) s ↔ times_cont_diff_on 𝕜 n f s := begin split, { assume H, have : f = e.symm ∘ (e ∘ f), by { ext y, simp only [function.comp_app], rw e.symm_apply_apply (f y) }, rw this, exact H.continuous_linear_map_comp _ }, { assume H, exact H.continuous_linear_map_comp _ } end /-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `f ∘ g` admits a Taylor series in `g ⁻¹' s`, whose `k`-th term is given by `p k (g v₁, ..., g vₖ)` . -/ lemma has_ftaylor_series_up_to_on.comp_continuous_linear_map {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) (g : G →L[𝕜] E) : has_ftaylor_series_up_to_on n (f ∘ g) (λ x k, (p (g x) k).comp_continuous_linear_map 𝕜 E g) (g ⁻¹' s) := begin split, { assume x hx, simp only [(hf.zero_eq (g x) hx).symm, function.comp_app], change p (g x) 0 (λ (i : fin 0), g 0) = p (g x) 0 0, rw continuous_linear_map.map_zero, refl }, { assume m hm x hx, let A : (E [×m]→L[𝕜] F) → (G [×m]→L[𝕜] F) := λ h, h.comp_continuous_linear_map 𝕜 E g, have hA : is_bounded_linear_map 𝕜 A := is_bounded_linear_map_continuous_multilinear_map_comp_linear g, convert (hA.has_fderiv_at).comp_has_fderiv_within_at x ((hf.fderiv_within m hm (g x) hx).comp x (g.has_fderiv_within_at) (subset.refl _)), ext y v, change p (g x) (nat.succ m) (g ∘ (cons y v)) = p (g x) m.succ (cons (g y) (g ∘ v)), rw comp_cons }, { assume m hm, let A : (E [×m]→L[𝕜] F) → (G [×m]→L[𝕜] F) := λ h, h.comp_continuous_linear_map 𝕜 E g, have hA : is_bounded_linear_map 𝕜 A := is_bounded_linear_map_continuous_multilinear_map_comp_linear g, exact hA.continuous.comp_continuous_on ((hf.cont m hm).comp g.continuous.continuous_on (subset.refl _)) } end /-- Composition by continuous linear maps on the right preserves `C^n` functions on domains. -/ lemma times_cont_diff_on.comp_continuous_linear_map {n : with_top ℕ} (hf : times_cont_diff_on 𝕜 n f s) (g : G →L[𝕜] E) : times_cont_diff_on 𝕜 n (f ∘ g) (g ⁻¹' s) := begin assume m hm x hx, rcases hf m hm (g x) hx with ⟨u, hu, p, hp⟩, refine ⟨g ⁻¹' u, _, _, hp.comp_continuous_linear_map g⟩, apply continuous_within_at.preimage_mem_nhds_within', { exact g.continuous.continuous_within_at }, { exact nhds_within_mono (g x) (image_preimage_subset g s) hu } end /-- Composition by continuous linear maps on the right preserves `C^n` functions. -/ lemma times_cont_diff.comp_continuous_linear_map {n : with_top ℕ} {f : E → F} {g : G →L[𝕜] E} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (f ∘ g) := times_cont_diff_on_univ.1 $ times_cont_diff_on.comp_continuous_linear_map (times_cont_diff_on_univ.2 hf) _ /-- Composition by continuous linear equivs on the right respects higher differentiability on domains. -/ lemma continuous_linear_equiv.times_cont_diff_on_comp_iff {n : with_top ℕ} (e : G ≃L[𝕜] E) : times_cont_diff_on 𝕜 n (f ∘ e) (e ⁻¹' s) ↔ times_cont_diff_on 𝕜 n f s := begin refine ⟨λ H, _, λ H, H.comp_continuous_linear_map _⟩, have A : f = (f ∘ e) ∘ e.symm, by { ext y, simp only [function.comp_app], rw e.apply_symm_apply y }, have B : e.symm ⁻¹' (e ⁻¹' s) = s, by { rw [← preimage_comp, e.self_comp_symm], refl }, rw [A, ← B], exact H.comp_continuous_linear_map _ end /-- If two functions `f` and `g` admit Taylor series `p` and `q` in a set `s`, then the cartesian product of `f` and `g` admits the cartesian product of `p` and `q` as a Taylor series. -/ lemma has_ftaylor_series_up_to_on.prod {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) {g : E → G} {q : E → formal_multilinear_series 𝕜 E G} (hg : has_ftaylor_series_up_to_on n g q s) : has_ftaylor_series_up_to_on n (λ y, (f y, g y)) (λ y k, (p y k).prod (q y k)) s := begin split, { assume x hx, rw [← hf.zero_eq x hx, ← hg.zero_eq x hx], refl }, { assume m hm x hx, let A : (E [×m]→L[𝕜] F) × (E [×m]→L[𝕜] G) → (E [×m]→L[𝕜] (F × G)) := λ p, p.1.prod p.2, have hA : is_bounded_linear_map 𝕜 A := is_bounded_linear_map_prod_multilinear, convert hA.has_fderiv_at.comp_has_fderiv_within_at x ((hf.fderiv_within m hm x hx).prod (hg.fderiv_within m hm x hx)) }, { assume m hm, let A : (E [×m]→L[𝕜] F) × (E [×m]→L[𝕜] G) → (E [×m]→L[𝕜] (F × G)) := λ p, p.1.prod p.2, have hA : is_bounded_linear_map 𝕜 A := is_bounded_linear_map_prod_multilinear, exact hA.continuous.comp_continuous_on ((hf.cont m hm).prod (hg.cont m hm)) } end /-- The cartesian product of `C^n` functions on domains is `C^n`. -/ lemma times_cont_diff_on.prod {n : with_top ℕ} {s : set E} {f : E → F} {g : E → G} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λx:E, (f x, g x)) s := begin assume m hm x hx, rcases hf m hm x hx with ⟨u, hu, p, hp⟩, rcases hg m hm x hx with ⟨v, hv, q, hq⟩, exact ⟨u ∩ v, filter.inter_mem_sets hu hv, _, (hp.mono (inter_subset_left u v)).prod (hq.mono (inter_subset_right u v))⟩ end /-- The cartesian product of `C^n` functions is `C^n`. -/ lemma times_cont_diff.prod {n : with_top ℕ} {f : E → F} {g : E → G} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λx:E, (f x, g x)) := times_cont_diff_on_univ.1 $ times_cont_diff_on.prod (times_cont_diff_on_univ.2 hf) (times_cont_diff_on_univ.2 hg) /-! ### Composition of `C^n` functions We show that the composition of `C^n` functions is `C^n`. One way to prove it would be to write the `n`-th derivative of the composition (this is Faà di Bruno's formula) and check its continuity, but this is very painful. Instead, we go for a simple inductive proof. Assume it is done for `n`. Then, to check it for `n+1`, one needs to check that the derivative of `g ∘ f` is `C^n`, i.e., that `Dg(f x) ⬝ Df(x)` is `C^n`. The term `Dg (f x)` is the composition of two `C^n` functions, so it is `C^n` by the inductive assumption. The term `Df(x)` is also `C^n`. Then, the matrix multiplication is the application of a bilinear map (which is `C^∞`, and therefore `C^n`) to `x ↦ (Dg(f x), Df x)`. As the composition of two `C^n` maps, it is again `C^n`, and we are done. There is a subtlety in this argument: we apply the inductive assumption to functions on other Banach spaces. In maths, one would say: prove by induction over `n` that, for all `C^n` maps between all pairs of Banach spaces, their composition is `C^n`. In Lean, this is fine as long as the spaces stay in the same universe. This is not the case in the above argument: if `E` lives in universe `u` and `F` lives in universe `v`, then linear maps from `E` to `F` (to which the derivative of `f` belongs) is in universe `max u v`. If one could quantify over finitely many universes, the above proof would work fine, but this is not the case. One could still write the proof considering spaces in any universe in `u, v, w, max u v, max v w, max u v w`, but it would be extremely tedious and lead to a lot of duplication. Instead, we formulate the above proof when all spaces live in the same universe (where everything is fine), and then we deduce the general result by lifting all our spaces to a common universe. We use the trick that any space `H` is isomorphic through a continuous linear equiv to `continuous_multilinear_map (λ (i : fin 0), E × F × G) H` to change the universe level, and then argue that composing with such a linear equiv does not change the fact of being `C^n`, which we have already proved previously. -/ /-- Auxiliary lemma proving that the composition of `C^n` functions on domains is `C^n` when all spaces live in the same universe. Use instead `times_cont_diff_on.comp` which removes the universe assumption (but is deduced from this one). -/ private lemma times_cont_diff_on.comp_same_univ {Eu : Type u} [normed_group Eu] [normed_space 𝕜 Eu] {Fu : Type u} [normed_group Fu] [normed_space 𝕜 Fu] {Gu : Type u} [normed_group Gu] [normed_space 𝕜 Gu] {n : with_top ℕ} {s : set Eu} {t : set Fu} {g : Fu → Gu} {f : Eu → Fu} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) : times_cont_diff_on 𝕜 n (g ∘ f) s := begin unfreezeI, induction n using with_top.nat_induction with n IH Itop generalizing Eu Fu Gu, { rw times_cont_diff_on_zero at hf hg ⊢, exact continuous_on.comp hg hf st }, { rw times_cont_diff_on_succ_iff_has_fderiv_within_at at hg ⊢, assume x hx, rcases (times_cont_diff_on_succ_iff_has_fderiv_within_at.1 hf) x hx with ⟨u, hu, f', hf', f'_diff⟩, rcases hg (f x) (st hx) with ⟨v, hv, g', hg', g'_diff⟩, have xu : x ∈ u := mem_of_mem_nhds_within hx hu, let w := s ∩ (u ∩ f⁻¹' v), have wv : w ⊆ f ⁻¹' v := λ y hy, hy.2.2, have wu : w ⊆ u := λ y hy, hy.2.1, have ws : w ⊆ s := λ y hy, hy.1, refine ⟨w, _, λ y, (g' (f y)).comp (f' y), _, _⟩, show w ∈ nhds_within x s, { apply filter.inter_mem_sets self_mem_nhds_within, apply filter.inter_mem_sets hu, apply continuous_within_at.preimage_mem_nhds_within', { rw ← continuous_within_at_inter' hu, exact (hf' x xu).differentiable_within_at.continuous_within_at.mono (inter_subset_right _ _) }, { exact nhds_within_mono _ (image_subset_iff.2 st) hv } }, show ∀ y ∈ w, has_fderiv_within_at (g ∘ f) ((g' (f y)).comp (f' y)) w y, { rintros y ⟨ys, yu, yv⟩, exact (hg' (f y) yv).comp y ((hf' y yu).mono wu) wv }, show times_cont_diff_on 𝕜 n (λ y, (g' (f y)).comp (f' y)) w, { have A : times_cont_diff_on 𝕜 n (λ y, g' (f y)) w := IH g'_diff ((hf.of_le (with_top.coe_le_coe.2 (nat.le_succ n))).mono ws) wv, have B : times_cont_diff_on 𝕜 n f' w := f'_diff.mono wu, have C : times_cont_diff_on 𝕜 n (λ y, (f' y, g' (f y))) w := times_cont_diff_on.prod B A, have D : times_cont_diff_on 𝕜 n (λ(p : (Eu →L[𝕜] Fu) × (Fu →L[𝕜] Gu)), p.2.comp p.1) univ := is_bounded_bilinear_map_comp.times_cont_diff.times_cont_diff_on, exact IH D C (subset_univ _) } }, { rw times_cont_diff_on_top at hf hg ⊢, assume n, apply Itop n (hg n) (hf n) st } end /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma times_cont_diff_on.comp {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (hg : times_cont_diff_on 𝕜 n g t) (hf : times_cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) : times_cont_diff_on 𝕜 n (g ∘ f) s := begin /- we lift all the spaces to a common universe, as we have already proved the result in this situation. For the lift, we use the trick that `H` is isomorphic through a continuous linear equiv to `continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) H`, and continuous linear equivs respect smoothness classes. The instances are not found automatically by Lean, so we declare them by hand. TODO: fix. -/ let Eu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) E, letI : normed_group Eu := @continuous_multilinear_map.to_normed_group 𝕜 (fin 0) (λ (i : fin 0), E × F × G) E _ _ _ _ _ _ _, letI : normed_space 𝕜 Eu := @continuous_multilinear_map.to_normed_space 𝕜 (fin 0) (λ (i : fin 0), E × F × G) E _ _ _ _ _ _ _, let Fu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) F, letI : normed_group Fu := @continuous_multilinear_map.to_normed_group 𝕜 (fin 0) (λ (i : fin 0), E × F × G) F _ _ _ _ _ _ _, letI : normed_space 𝕜 Fu := @continuous_multilinear_map.to_normed_space 𝕜 (fin 0) (λ (i : fin 0), E × F × G) F _ _ _ _ _ _ _, let Gu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) G, letI : normed_group Gu := @continuous_multilinear_map.to_normed_group 𝕜 (fin 0) (λ (i : fin 0), E × F × G) G _ _ _ _ _ _ _, letI : normed_space 𝕜 Gu := @continuous_multilinear_map.to_normed_space 𝕜 (fin 0) (λ (i : fin 0), E × F × G) G _ _ _ _ _ _ _, -- declare the isomorphisms let isoE : Eu ≃L[𝕜] E := continuous_multilinear_curry_fin0 𝕜 (E × F × G) E, let isoF : Fu ≃L[𝕜] F := continuous_multilinear_curry_fin0 𝕜 (E × F × G) F, let isoG : Gu ≃L[𝕜] G := continuous_multilinear_curry_fin0 𝕜 (E × F × G) G, -- lift the functions to the new spaces, check smoothness there, and then go back. let fu : Eu → Fu := (isoF.symm ∘ f) ∘ isoE, have fu_diff : times_cont_diff_on 𝕜 n fu (isoE ⁻¹' s) := by rwa [isoE.times_cont_diff_on_comp_iff, isoF.symm.comp_times_cont_diff_on_iff], let gu : Fu → Gu := (isoG.symm ∘ g) ∘ isoF, have gu_diff : times_cont_diff_on 𝕜 n gu (isoF ⁻¹' t) := by rwa [isoF.times_cont_diff_on_comp_iff, isoG.symm.comp_times_cont_diff_on_iff], have main : times_cont_diff_on 𝕜 n (gu ∘ fu) (isoE ⁻¹' s), { apply times_cont_diff_on.comp_same_univ gu_diff fu_diff, assume y hy, simp only [fu, continuous_linear_equiv.coe_apply, function.comp_app, mem_preimage], rw isoF.apply_symm_apply (f (isoE y)), exact st hy }, have : gu ∘ fu = (isoG.symm ∘ (g ∘ f)) ∘ isoE, { ext y, simp only [function.comp_apply, gu, fu], rw isoF.apply_symm_apply (f (isoE y)) }, rwa [this, isoE.times_cont_diff_on_comp_iff, isoG.symm.comp_times_cont_diff_on_iff] at main end /-- The composition of a `C^n` function on a domain with a `C^n` function is `C^n`. -/ lemma times_cont_diff.comp_times_cont_diff_on {n : with_top ℕ} {s : set E} {g : F → G} {f : E → F} (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (g ∘ f) s := (times_cont_diff_on_univ.2 hg).comp hf subset_preimage_univ /-- The composition of `C^n` functions is `C^n`. -/ lemma times_cont_diff.comp {n : with_top ℕ} {g : F → G} {f : E → F} (hg : times_cont_diff 𝕜 n g) (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (g ∘ f) := times_cont_diff_on_univ.1 $ times_cont_diff_on.comp (times_cont_diff_on_univ.2 hg) (times_cont_diff_on_univ.2 hf) (subset_univ _) /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma times_cont_diff_on_fderiv_within_apply {m n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) : times_cont_diff_on 𝕜 m (λp : E × E, (fderiv_within 𝕜 f s p.1 : E →L[𝕜] F) p.2) (set.prod s (univ : set E)) := begin have A : times_cont_diff 𝕜 m (λp : (E →L[𝕜] F) × E, p.1 p.2), { apply is_bounded_bilinear_map.times_cont_diff, exact is_bounded_bilinear_map_apply }, have B : times_cont_diff_on 𝕜 m (λ (p : E × E), ((fderiv_within 𝕜 f s p.fst), p.snd)) (set.prod s univ), { apply times_cont_diff_on.prod _ _, { have I : times_cont_diff_on 𝕜 m (λ (x : E), fderiv_within 𝕜 f s x) s := hf.fderiv_within hs hmn, have J : times_cont_diff_on 𝕜 m (λ (x : E × E), x.1) (set.prod s univ) := times_cont_diff_fst.times_cont_diff_on, exact times_cont_diff_on.comp I J (prod_subset_preimage_fst _ _) }, { apply times_cont_diff.times_cont_diff_on _ , apply is_bounded_linear_map.snd.times_cont_diff } }, exact A.comp_times_cont_diff_on B end /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma times_cont_diff.times_cont_diff_fderiv_apply {n m : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f) (hmn : m + 1 ≤ n) : times_cont_diff 𝕜 m (λp : E × E, (fderiv 𝕜 f p.1 : E →L[𝕜] F) p.2) := begin rw ← times_cont_diff_on_univ at ⊢ hf, rw [← fderiv_within_univ, ← univ_prod_univ], exact times_cont_diff_on_fderiv_within_apply hf unique_diff_on_univ hmn end /-- The sum of two `C^n`functions on a domain is `C^n`. -/ lemma times_cont_diff_on.add {n : with_top ℕ} {s : set E} {f g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λx, f x + g x) s := begin have : times_cont_diff 𝕜 n (λp : F × F, p.1 + p.2), { apply is_bounded_linear_map.times_cont_diff, exact is_bounded_linear_map.add is_bounded_linear_map.fst is_bounded_linear_map.snd }, exact this.comp_times_cont_diff_on (hf.prod hg) end /-- The sum of two `C^n`functions is `C^n`. -/ lemma times_cont_diff.add {n : with_top ℕ} {f g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λx, f x + g x) := begin have : times_cont_diff 𝕜 n (λp : F × F, p.1 + p.2), { apply is_bounded_linear_map.times_cont_diff, exact is_bounded_linear_map.add is_bounded_linear_map.fst is_bounded_linear_map.snd }, exact this.comp (hf.prod hg) end /-- The negative of a `C^n`function on a domain is `C^n`. -/ lemma times_cont_diff_on.neg {n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on 𝕜 n f s) : times_cont_diff_on 𝕜 n (λx, -f x) s := begin have : times_cont_diff 𝕜 n (λp : F, -p), { apply is_bounded_linear_map.times_cont_diff, exact is_bounded_linear_map.neg is_bounded_linear_map.id }, exact this.comp_times_cont_diff_on hf end /-- The negative of a `C^n`function is `C^n`. -/ lemma times_cont_diff.neg {n : with_top ℕ} {f : E → F} (hf : times_cont_diff 𝕜 n f) : times_cont_diff 𝕜 n (λx, -f x) := begin have : times_cont_diff 𝕜 n (λp : F, -p), { apply is_bounded_linear_map.times_cont_diff, exact is_bounded_linear_map.neg is_bounded_linear_map.id }, exact this.comp hf end /-- The difference of two `C^n`functions on a domain is `C^n`. -/ lemma times_cont_diff_on.sub {n : with_top ℕ} {s : set E} {f g : E → F} (hf : times_cont_diff_on 𝕜 n f s) (hg : times_cont_diff_on 𝕜 n g s) : times_cont_diff_on 𝕜 n (λx, f x - g x) s := hf.add (hg.neg) /-- The difference of two `C^n`functions is `C^n`. -/ lemma times_cont_diff.sub {n : with_top ℕ} {f g : E → F} (hf : times_cont_diff 𝕜 n f) (hg : times_cont_diff 𝕜 n g) : times_cont_diff 𝕜 n (λx, f x - g x) := hf.add hg.neg section reals /-! ### Results over `ℝ` The results in this section rely on the Mean Value Theorem, and therefore hold only over `ℝ` (and its extension fields such as `ℂ`). -/ variables {E' : Type*} [normed_group E'] [normed_space ℝ E'] {F' : Type*} [normed_group F'] [normed_space ℝ F'] /-- If a function has a Taylor series at order at least 1, then at points in the interior of the domain of definition, the term of order 1 of this series is a strict derivative of `f`. -/ lemma has_ftaylor_series_up_to_on.has_strict_fderiv_at {s : set E'} {f : E' → F'} {x : E'} {p : E' → formal_multilinear_series ℝ E' F'} {n : with_top ℕ} (hf : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hs : s ∈ 𝓝 x) : has_strict_fderiv_at f ((continuous_multilinear_curry_fin1 ℝ E' F') (p x 1)) x := begin let f' := λ x, (continuous_multilinear_curry_fin1 ℝ E' F') (p x 1), have hf' : ∀ x, x ∈ s → has_fderiv_within_at f (f' x) s x := λ x, has_ftaylor_series_up_to_on.has_fderiv_within_at hf hn, have hcont : continuous_on f' s := (continuous_multilinear_curry_fin1 ℝ E' F').continuous.comp_continuous_on (hf.cont 1 hn), exact strict_fderiv_of_cont_diff hf' hcont hs, end /-- If a function is `C^n` with `1 ≤ n` on a domain, then at points in the interior of the domain of definition, the derivative of `f` is also a strict derivative. -/ lemma times_cont_diff_on.has_strict_fderiv_at {s : set E'} {f : E' → F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff_on ℝ n f s) (hn : 1 ≤ n) (hs : s ∈ 𝓝 x) : has_strict_fderiv_at f (fderiv ℝ f x) x := begin rcases (hf 1 hn x (mem_of_nhds hs)) with ⟨u, H, p, hp⟩, have := hp.has_strict_fderiv_at (by norm_num) (nhds_of_nhds_within_of_nhds hs H), convert this, exact this.has_fderiv_at.fderiv end /-- If a function is `C^n` with `1 ≤ n`, then the derivative of `f` is also a strict derivative. -/ lemma times_cont_diff.has_strict_fderiv_at {f : E' → F'} {x : E'} {n : with_top ℕ} (hf : times_cont_diff ℝ n f) (hn : 1 ≤ n) : has_strict_fderiv_at f (fderiv ℝ f x) x := times_cont_diff_on.has_strict_fderiv_at (times_cont_diff_on_univ.mpr hf) hn (𝓝 x).univ_sets end reals
b6f8d0637b1e5a157ffc20424b33cd64880cedf2
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/data/set/intervals/ord_connected.lean
8e80999326b93e0231568b4711f5afa01bd16f92
[ "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,737
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import data.set.intervals.unordered_interval import data.set.lattice /-! # Order-connected sets We say that a set `s : set α` is `ord_connected` if for all `x y ∈ s` it includes the interval `[x, y]`. If `α` is a `densely_ordered` `conditionally_complete_linear_order` with the `order_topology`, then this condition is equivalent to `is_preconnected s`. If `α` is a `linear_ordered_field`, then this condition is also equivalent to `convex α s`. In this file we prove that intersection of a family of `ord_connected` sets is `ord_connected` and that all standard intervals are `ord_connected`. -/ namespace set variables {α : Type*} [preorder α] {s t : set α} /-- We say that a set `s : set α` is `ord_connected` if for all `x y ∈ s` it includes the interval `[x, y]`. If `α` is a `densely_ordered` `conditionally_complete_linear_order` with the `order_topology`, then this condition is equivalent to `is_preconnected s`. If `α` is a `linear_ordered_field`, then this condition is also equivalent to `convex α s`. -/ class ord_connected (s : set α) : Prop := (out' ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s) : Icc x y ⊆ s) lemma ord_connected.out (h : ord_connected s) : ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), Icc x y ⊆ s := h.1 lemma ord_connected_def : ord_connected s ↔ ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), Icc x y ⊆ s := ⟨λ h, h.1, λ h, ⟨h⟩⟩ /-- It suffices to prove `[x, y] ⊆ s` for `x y ∈ s`, `x ≤ y`. -/ lemma ord_connected_iff : ord_connected s ↔ ∀ (x ∈ s) (y ∈ s), x ≤ y → Icc x y ⊆ s := ord_connected_def.trans ⟨λ hs x hx y hy hxy, hs hx hy, λ H x hx y hy z hz, H x hx y hy (le_trans hz.1 hz.2) hz⟩ lemma ord_connected_of_Ioo {α : Type*} [partial_order α] {s : set α} (hs : ∀ (x ∈ s) (y ∈ s), x < y → Ioo x y ⊆ s) : ord_connected s := begin rw ord_connected_iff, intros x hx y hy hxy, rcases eq_or_lt_of_le hxy with rfl|hxy', { simpa }, have := hs x hx y hy hxy', rw [← union_diff_cancel Ioo_subset_Icc_self], simp [*, insert_subset] end protected lemma Icc_subset (s : set α) [hs : ord_connected s] {x y} (hx : x ∈ s) (hy : y ∈ s) : Icc x y ⊆ s := hs.out hx hy lemma ord_connected.inter {s t : set α} (hs : ord_connected s) (ht : ord_connected t) : ord_connected (s ∩ t) := ⟨λ x hx y hy, subset_inter (hs.out hx.1 hy.1) (ht.out hx.2 hy.2)⟩ instance ord_connected.inter' {s t : set α} [ord_connected s] [ord_connected t] : ord_connected (s ∩ t) := ord_connected.inter ‹_› ‹_› lemma ord_connected.dual {s : set α} (hs : ord_connected s) : @ord_connected (order_dual α) _ s := ⟨λ x hx y hy z hz, hs.out hy hx ⟨hz.2, hz.1⟩⟩ lemma ord_connected_dual {s : set α} : @ord_connected (order_dual α) _ s ↔ ord_connected s := ⟨λ h, by simpa only [ord_connected_def] using h.dual, λ h, h.dual⟩ lemma ord_connected_sInter {S : set (set α)} (hS : ∀ s ∈ S, ord_connected s) : ord_connected (⋂₀ S) := ⟨λ x hx y hy, subset_sInter $ λ s hs, (hS s hs).out (hx s hs) (hy s hs)⟩ lemma ord_connected_Inter {ι : Sort*} {s : ι → set α} (hs : ∀ i, ord_connected (s i)) : ord_connected (⋂ i, s i) := ord_connected_sInter $ forall_range_iff.2 hs instance ord_connected_Inter' {ι : Sort*} {s : ι → set α} [∀ i, ord_connected (s i)] : ord_connected (⋂ i, s i) := ord_connected_Inter ‹_› lemma ord_connected_bInter {ι : Sort*} {p : ι → Prop} {s : Π (i : ι) (hi : p i), set α} (hs : ∀ i hi, ord_connected (s i hi)) : ord_connected (⋂ i hi, s i hi) := ord_connected_Inter $ λ i, ord_connected_Inter $ hs i lemma ord_connected_pi {ι : Type*} {α : ι → Type*} [Π i, preorder (α i)] {s : set ι} {t : Π i, set (α i)} (h : ∀ i ∈ s, ord_connected (t i)) : ord_connected (s.pi t) := ⟨λ x hx y hy z hz i hi, (h i hi).out (hx i hi) (hy i hi) ⟨hz.1 i, hz.2 i⟩⟩ instance ord_connected_pi' {ι : Type*} {α : ι → Type*} [Π i, preorder (α i)] {s : set ι} {t : Π i, set (α i)} [h : ∀ i, ord_connected (t i)] : ord_connected (s.pi t) := ord_connected_pi $ λ i hi, h i @[instance] lemma ord_connected_Ici {a : α} : ord_connected (Ici a) := ⟨λ x hx y hy z hz, le_trans hx hz.1⟩ @[instance] lemma ord_connected_Iic {a : α} : ord_connected (Iic a) := ⟨λ x hx y hy z hz, le_trans hz.2 hy⟩ @[instance] lemma ord_connected_Ioi {a : α} : ord_connected (Ioi a) := ⟨λ x hx y hy z hz, lt_of_lt_of_le hx hz.1⟩ @[instance] lemma ord_connected_Iio {a : α} : ord_connected (Iio a) := ⟨λ x hx y hy z hz, lt_of_le_of_lt hz.2 hy⟩ @[instance] lemma ord_connected_Icc {a b : α} : ord_connected (Icc a b) := ord_connected_Ici.inter ord_connected_Iic @[instance] lemma ord_connected_Ico {a b : α} : ord_connected (Ico a b) := ord_connected_Ici.inter ord_connected_Iio @[instance] lemma ord_connected_Ioc {a b : α} : ord_connected (Ioc a b) := ord_connected_Ioi.inter ord_connected_Iic @[instance] lemma ord_connected_Ioo {a b : α} : ord_connected (Ioo a b) := ord_connected_Ioi.inter ord_connected_Iio @[instance] lemma ord_connected_singleton {α : Type*} [partial_order α] {a : α} : ord_connected ({a} : set α) := by { rw ← Icc_self, exact ord_connected_Icc } @[instance] lemma ord_connected_empty : ord_connected (∅ : set α) := ⟨λ x, false.elim⟩ @[instance] lemma ord_connected_univ : ord_connected (univ : set α) := ⟨λ _ _ _ _, subset_univ _⟩ /-- In a dense order `α`, the subtype from an `ord_connected` set is also densely ordered. -/ instance [densely_ordered α] {s : set α} [hs : ord_connected s] : densely_ordered s := ⟨ begin intros a₁ a₂ ha, have ha' : ↑a₁ < ↑a₂ := ha, obtain ⟨x, ha₁x, hxa₂⟩ := exists_between ha', refine ⟨⟨x, _⟩, ⟨ha₁x, hxa₂⟩⟩, exact (hs.out a₁.2 a₂.2) (Ioo_subset_Icc_self ⟨ha₁x, hxa₂⟩), end ⟩ variables {β : Type*} [linear_order β] @[instance] lemma ord_connected_interval {a b : β} : ord_connected (interval a b) := ord_connected_Icc lemma ord_connected.interval_subset {s : set β} (hs : ord_connected s) ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s) : interval x y ⊆ s := by cases le_total x y; simp only [interval_of_le, interval_of_ge, *]; apply hs.out; assumption lemma ord_connected_iff_interval_subset {s : set β} : ord_connected s ↔ ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), interval x y ⊆ s := ⟨λ h, h.interval_subset, λ h, ord_connected_iff.2 $ λ x hx y hy hxy, by simpa only [interval_of_le hxy] using h hx hy⟩ end set
c6d811f3595340d16364a24186586ac236115087
4727251e0cd73359b15b664c3170e5d754078599
/src/tactic/field_simp.lean
7255410dc0798184eb0b5da8895f54bb53ae44fd
[ "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,194
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import tactic.interactive import tactic.norm_num /-! # `field_simp` tactic Tactic to clear denominators in algebraic expressions, based on `simp` with a specific simpset. -/ namespace tactic /-- Try to prove a goal of the form `x ≠ 0` by calling `assumption`, or `norm_num1` if `x` is a numeral. -/ meta def field_simp.ne_zero : tactic unit := do goal ← tactic.target, match goal with | `(%%e ≠ 0) := assumption <|> do n ← e.to_rat, `[norm_num1] | _ := tactic.fail "goal should be of the form `x ≠ 0`" end namespace interactive setup_tactic_parser /-- The goal of `field_simp` is to reduce an expression in a field to an expression of the form `n / d` where neither `n` nor `d` contains any division symbol, just using the simplifier (with a carefully crafted simpset named `field_simps`) to reduce the number of division symbols whenever possible by iterating the following steps: - write an inverse as a division - in any product, move the division to the right - if there are several divisions in a product, group them together at the end and write them as a single division - reduce a sum to a common denominator If the goal is an equality, this simpset will also clear the denominators, so that the proof can normally be concluded by an application of `ring` or `ring_exp`. `field_simp [hx, hy]` is a short form for `simp [-one_div, -mul_eq_zero, hx, hy] with field_simps {discharger := tactic.field_simp.ne_zero}` Note that this naive algorithm will not try to detect common factors in denominators to reduce the complexity of the resulting expression. Instead, it relies on the ability of `ring` to handle complicated expressions in the next step. As always with the simplifier, reduction steps will only be applied if the preconditions of the lemmas can be checked. This means that proofs that denominators are nonzero should be included. The fact that a product is nonzero when all factors are, and that a power of a nonzero number is nonzero, are included in the simpset, but more complicated assertions (especially dealing with sums) should be given explicitly. If your expression is not completely reduced by the simplifier invocation, check the denominators of the resulting expression and provide proofs that they are nonzero to enable further progress. To check that denominators are nonzero, `field_simp` will look for facts in the context, and will try to apply `norm_num` to close numerical goals. The invocation of `field_simp` removes the lemma `one_div` from the simpset, as this lemma works against the algorithm explained above. It also removes `mul_eq_zero : x * y = 0 ↔ x = 0 ∨ y = 0`, as `norm_num` can not work on disjunctions to close goals of the form `24 ≠ 0`, and replaces it with `mul_ne_zero : x ≠ 0 → y ≠ 0 → x * y ≠ 0` creating two goals instead of a disjunction. For example, ```lean example (a b c d x y : ℂ) (hx : x ≠ 0) (hy : y ≠ 0) : a + b / x + c / x^2 + d / x^3 = a + x⁻¹ * (y * b / y + (d / x + c) / x) := begin field_simp, ring end ``` See also the `cancel_denoms` tactic, which tries to do a similar simplification for expressions that have numerals in denominators. The tactics are not related: `cancel_denoms` will only handle numeric denominators, and will try to entirely remove (numeric) division from the expression by multiplying by a factor. -/ meta def field_simp (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (locat : parse location) (cfg : simp_config_ext := {discharger := field_simp.ne_zero}) : tactic unit := let attr_names := `field_simps :: attr_names, hs := simp_arg_type.except `one_div :: simp_arg_type.except `mul_eq_zero :: hs in propagate_tags (simp_core cfg.to_simp_config cfg.discharger no_dflt hs attr_names locat >> skip) add_tactic_doc { name := "field_simp", category := doc_category.tactic, decl_names := [`tactic.interactive.field_simp], tags := ["simplification", "arithmetic"] } end interactive end tactic
383b5ab10bbd7a31c3285d604cb3239141bd8ff9
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/eassumption.lean
eb53c180a3bc9423a77ae7c613eb543ddd66616b
[ "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
148
lean
variable p : nat → Prop variable q : nat → Prop variables a b c : nat example : p c → p b → q b → p a → ∃ x, p x ∧ q x := by blast
e7a9562fcc43295e6e68f07884d2d879d6ad4c83
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/measure/doubling.lean
1ca5de2930d7d1f5a000203f2a0a6f4ab597c13f
[ "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
7,164
lean
/- Copyright (c) 2022 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import analysis.special_functions.log.base import measure_theory.measure.measure_space_def /-! # Uniformly locally doubling measures A uniformly locally doubling measure `μ` on a metric space is a measure for which there exists a constant `C` such that for all sufficiently small radii `ε`, and for any centre, the measure of a ball of radius `2 * ε` is bounded by `C` times the measure of the concentric ball of radius `ε`. This file records basic facts about uniformly locally doubling measures. ## Main definitions * `is_unif_loc_doubling_measure`: the definition of a uniformly locally doubling measure (as a typeclass). * `is_unif_loc_doubling_measure.doubling_constant`: a function yielding the doubling constant `C` appearing in the definition of a uniformly locally doubling measure. -/ noncomputable theory open set filter metric measure_theory topological_space open_locale ennreal nnreal topology /-- A measure `μ` is said to be a uniformly locally doubling measure if there exists a constant `C` such that for all sufficiently small radii `ε`, and for any centre, the measure of a ball of radius `2 * ε` is bounded by `C` times the measure of the concentric ball of radius `ε`. Note: it is important that this definition makes a demand only for sufficiently small `ε`. For example we want hyperbolic space to carry the instance `is_unif_loc_doubling_measure volume` but volumes grow exponentially in hyperbolic space. To be really explicit, consider the hyperbolic plane of curvature -1, the area of a disc of radius `ε` is `A(ε) = 2π(cosh(ε) - 1)` so `A(2ε)/A(ε) ~ exp(ε)`. -/ class is_unif_loc_doubling_measure {α : Type*} [metric_space α] [measurable_space α] (μ : measure α) := (exists_measure_closed_ball_le_mul [] : ∃ (C : ℝ≥0), ∀ᶠ ε in 𝓝[>] 0, ∀ x, μ (closed_ball x (2 * ε)) ≤ C * μ (closed_ball x ε)) namespace is_unif_loc_doubling_measure variables {α : Type*} [metric_space α] [measurable_space α] (μ : measure α) [is_unif_loc_doubling_measure μ] /-- A doubling constant for a uniformly locally doubling measure. See also `is_unif_loc_doubling_measure.scaling_constant_of`. -/ def doubling_constant : ℝ≥0 := classical.some $ exists_measure_closed_ball_le_mul μ lemma exists_measure_closed_ball_le_mul' : ∀ᶠ ε in 𝓝[>] 0, ∀ x, μ (closed_ball x (2 * ε)) ≤ doubling_constant μ * μ (closed_ball x ε) := classical.some_spec $ exists_measure_closed_ball_le_mul μ lemma exists_eventually_forall_measure_closed_ball_le_mul (K : ℝ) : ∃ (C : ℝ≥0), ∀ᶠ ε in 𝓝[>] 0, ∀ x t (ht : t ≤ K), μ (closed_ball x (t * ε)) ≤ C * μ (closed_ball x ε) := begin let C := doubling_constant μ, have hμ : ∀ (n : ℕ), ∀ᶠ ε in 𝓝[>] 0, ∀ x, μ (closed_ball x (2^n * ε)) ≤ ↑(C^n) * μ (closed_ball x ε), { intros n, induction n with n ih, { simp, }, replace ih := eventually_nhds_within_pos_mul_left (two_pos : 0 < (2 : ℝ)) ih, refine (ih.and (exists_measure_closed_ball_le_mul' μ)).mono (λ ε hε x, _), calc μ (closed_ball x (2^(n + 1) * ε)) = μ (closed_ball x (2^n * (2 * ε))) : by rw [pow_succ', mul_assoc] ... ≤ ↑(C^n) * μ (closed_ball x (2 * ε)) : hε.1 x ... ≤ ↑(C^n) * (C * μ (closed_ball x ε)) : ennreal.mul_left_mono (hε.2 x) ... = ↑(C^(n + 1)) * μ (closed_ball x ε) : by rw [← mul_assoc, pow_succ', ennreal.coe_mul], }, rcases lt_or_le K 1 with hK | hK, { refine ⟨1, _⟩, simp only [ennreal.coe_one, one_mul], exact eventually_mem_nhds_within.mono (λ ε hε x t ht, measure_mono $ closed_ball_subset_closed_ball (by nlinarith [mem_Ioi.mp hε])), }, { refine ⟨C^⌈real.logb 2 K⌉₊, ((hμ ⌈real.logb 2 K⌉₊).and eventually_mem_nhds_within).mono (λ ε hε x t ht, le_trans (measure_mono $ closed_ball_subset_closed_ball _) (hε.1 x))⟩, refine mul_le_mul_of_nonneg_right (ht.trans _) (mem_Ioi.mp hε.2).le, conv_lhs { rw ← real.rpow_logb two_pos (by norm_num) (by linarith : 0 < K), }, rw ← real.rpow_nat_cast, exact real.rpow_le_rpow_of_exponent_le one_le_two (nat.le_ceil (real.logb 2 K)), }, end /-- A variant of `is_unif_loc_doubling_measure.doubling_constant` which allows for scaling the radius by values other than `2`. -/ def scaling_constant_of (K : ℝ) : ℝ≥0 := max (classical.some $ exists_eventually_forall_measure_closed_ball_le_mul μ K) 1 @[simp] lemma one_le_scaling_constant_of (K : ℝ) : 1 ≤ scaling_constant_of μ K := le_max_of_le_right $ le_refl 1 lemma eventually_measure_mul_le_scaling_constant_of_mul (K : ℝ) : ∃ (R : ℝ), 0 < R ∧ ∀ x t r (ht : t ∈ Ioc 0 K) (hr : r ≤ R), μ (closed_ball x (t * r)) ≤ scaling_constant_of μ K * μ (closed_ball x r) := begin have h := classical.some_spec (exists_eventually_forall_measure_closed_ball_le_mul μ K), rcases mem_nhds_within_Ioi_iff_exists_Ioc_subset.1 h with ⟨R, Rpos, hR⟩, refine ⟨R, Rpos, λ x t r ht hr, _⟩, rcases lt_trichotomy r 0 with rneg|rfl|rpos, { have : t * r < 0, from mul_neg_of_pos_of_neg ht.1 rneg, simp only [closed_ball_eq_empty.2 this, measure_empty, zero_le'] }, { simp only [mul_zero, closed_ball_zero], refine le_mul_of_one_le_of_le _ le_rfl, apply ennreal.one_le_coe_iff.2 (le_max_right _ _) }, { apply (hR ⟨rpos, hr⟩ x t ht.2).trans _, exact mul_le_mul_right' (ennreal.coe_le_coe.2 (le_max_left _ _)) _ } end lemma eventually_measure_le_scaling_constant_mul (K : ℝ) : ∀ᶠ r in 𝓝[>] 0, ∀ x, μ (closed_ball x (K * r)) ≤ scaling_constant_of μ K * μ (closed_ball x r) := begin filter_upwards [classical.some_spec (exists_eventually_forall_measure_closed_ball_le_mul μ K)] with r hr x, exact (hr x K le_rfl).trans (mul_le_mul_right' (ennreal.coe_le_coe.2 (le_max_left _ _)) _) end lemma eventually_measure_le_scaling_constant_mul' (K : ℝ) (hK : 0 < K) : ∀ᶠ r in 𝓝[>] 0, ∀ x, μ (closed_ball x r) ≤ scaling_constant_of μ K⁻¹ * μ (closed_ball x (K * r)) := begin convert eventually_nhds_within_pos_mul_left hK (eventually_measure_le_scaling_constant_mul μ K⁻¹), ext, simp [inv_mul_cancel_left₀ hK.ne'], end /-- A scale below which the doubling measure `μ` satisfies good rescaling properties when one multiplies the radius of balls by at most `K`, as stated in `measure_mul_le_scaling_constant_of_mul`. -/ def scaling_scale_of (K : ℝ) : ℝ := (eventually_measure_mul_le_scaling_constant_of_mul μ K).some lemma scaling_scale_of_pos (K : ℝ) : 0 < scaling_scale_of μ K := (eventually_measure_mul_le_scaling_constant_of_mul μ K).some_spec.1 lemma measure_mul_le_scaling_constant_of_mul {K : ℝ} {x : α} {t r : ℝ} (ht : t ∈ Ioc 0 K) (hr : r ≤ scaling_scale_of μ K) : μ (closed_ball x (t * r)) ≤ scaling_constant_of μ K * μ (closed_ball x r) := (eventually_measure_mul_le_scaling_constant_of_mul μ K).some_spec.2 x t r ht hr end is_unif_loc_doubling_measure
1276588def39626b4200d62b6d2371b5f2dbff87
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/category_theory/limits/shapes/equalizers.lean
c0a43eab5099d95d17331063f7011d9d033397bc
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
23,989
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import data.fintype import category_theory.epi_mono import category_theory.limits.limits import category_theory.limits.shapes.finite_limits /-! # Equalizers and coequalizers This file defines (co)equalizers as special cases of (co)limits. An equalizer is the categorical generalization of the subobject {a ∈ A | f(a) = g(a)} known from abelian groups or modules. It is a limit cone over the diagram formed by `f` and `g`. A coequalizer is the dual concept. ## Main definitions * `walking_parallel_pair` is the indexing category used for (co)equalizer_diagrams * `parallel_pair` is a functor from `walking_parallel_pair` to our category `C`. * a `fork` is a cone over a parallel pair. * there is really only one interesting morphism in a fork: the arrow from the vertex of the fork to the domain of f and g. It is called `fork.ι`. * an `equalizer` is now just a `limit (parallel_pair f g)` Each of these has a dual. ## Main statements * `equalizer.ι_mono` states that every equalizer map is a monomorphism * `is_limit_cone_parallel_pair_self` states that the identity on the domain of `f` is an equalizer of `f` and `f`. ## Implementation notes As with the other special shapes in the limits library, all the definitions here are given as `abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about general limits can be used. ## References * [F. Borceux, *Handbook of Categorical Algebra 1*][borceux-vol1] -/ open category_theory namespace category_theory.limits local attribute [tidy] tactic.case_bash universes v u /-- The type of objects for the diagram indexing a (co)equalizer. -/ @[derive decidable_eq] inductive walking_parallel_pair : Type v | zero | one instance fintype_walking_parallel_pair : fintype walking_parallel_pair := { elems := [walking_parallel_pair.zero, walking_parallel_pair.one].to_finset, complete := λ x, by { cases x; simp } } open walking_parallel_pair /-- The type family of morphisms for the diagram indexing a (co)equalizer. -/ @[derive _root_.decidable_eq] inductive walking_parallel_pair_hom : walking_parallel_pair → walking_parallel_pair → Type v | left : walking_parallel_pair_hom zero one | right : walking_parallel_pair_hom zero one | id : Π X : walking_parallel_pair.{v}, walking_parallel_pair_hom X X open walking_parallel_pair_hom instance (j j' : walking_parallel_pair) : fintype (walking_parallel_pair_hom j j') := { elems := walking_parallel_pair.rec_on j (walking_parallel_pair.rec_on j' [walking_parallel_pair_hom.id zero].to_finset [left, right].to_finset) (walking_parallel_pair.rec_on j' ∅ [walking_parallel_pair_hom.id one].to_finset), complete := by tidy } def walking_parallel_pair_hom.comp : Π (X Y Z : walking_parallel_pair) (f : walking_parallel_pair_hom X Y) (g : walking_parallel_pair_hom Y Z), walking_parallel_pair_hom X Z | _ _ _ (id _) h := h | _ _ _ left (id one) := left | _ _ _ right (id one) := right . instance walking_parallel_pair_hom_category : small_category.{v} walking_parallel_pair := { hom := walking_parallel_pair_hom, id := walking_parallel_pair_hom.id, comp := walking_parallel_pair_hom.comp } instance : fin_category.{v} walking_parallel_pair.{v} := { } @[simp] lemma walking_parallel_pair_hom_id (X : walking_parallel_pair.{v}) : walking_parallel_pair_hom.id X = 𝟙 X := rfl variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 variables {X Y : C} def parallel_pair (f g : X ⟶ Y) : walking_parallel_pair.{v} ⥤ C := { obj := λ x, match x with | zero := X | one := Y end, map := λ x y h, match x, y, h with | _, _, (id _) := 𝟙 _ | _, _, left := f | _, _, right := g end, -- `tidy` can cope with this, but it's too slow: map_comp' := begin rintros (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) (⟨⟩|⟨⟩) ⟨⟩⟨⟩; { unfold_aux, simp; refl }, end, }. @[simp] lemma parallel_pair_obj_zero (f g : X ⟶ Y) : (parallel_pair f g).obj zero = X := rfl @[simp] lemma parallel_pair_obj_one (f g : X ⟶ Y) : (parallel_pair f g).obj one = Y := rfl @[simp] lemma parallel_pair_map_left (f g : X ⟶ Y) : (parallel_pair f g).map left = f := rfl @[simp] lemma parallel_pair_map_right (f g : X ⟶ Y) : (parallel_pair f g).map right = g := rfl @[simp] lemma parallel_pair_functor_obj {F : walking_parallel_pair.{v} ⥤ C} (j : walking_parallel_pair.{v}) : (parallel_pair (F.map left) (F.map right)).obj j = F.obj j := begin cases j; refl end /-- Every functor indexing a (co)equalizer is naturally isomorphic (actually, equal) to a `parallel_pair` -/ def diagram_iso_parallel_pair (F : walking_parallel_pair.{v} ⥤ C) : F ≅ parallel_pair (F.map left) (F.map right) := nat_iso.of_components (λ j, eq_to_iso $ by cases j; tidy) $ by tidy abbreviation fork (f g : X ⟶ Y) := cone (parallel_pair f g) abbreviation cofork (f g : X ⟶ Y) := cocone (parallel_pair f g) variables {f g : X ⟶ Y} @[simp] lemma cone_parallel_pair_left (s : cone (parallel_pair f g)) : (s.π).app zero ≫ f = (s.π).app one := by { conv_lhs { congr, skip, rw ←parallel_pair_map_left f g }, rw s.w } @[simp] lemma cone_parallel_pair_right (s : cone (parallel_pair f g)) : (s.π).app zero ≫ g = (s.π).app one := by { conv_lhs { congr, skip, rw ←parallel_pair_map_right f g }, rw s.w } @[simp] lemma cocone_parallel_pair_left (s : cocone (parallel_pair f g)) : f ≫ (s.ι).app one = (s.ι).app zero := by { conv_lhs { congr, rw ←parallel_pair_map_left f g }, rw s.w } @[simp] lemma cocone_parallel_pair_right (s : cocone (parallel_pair f g)) : g ≫ (s.ι).app one = (s.ι).app zero := by { conv_lhs { congr, rw ←parallel_pair_map_right f g }, rw s.w } /-- To check whether two maps are equalized by both maps of a fork, it suffices to check it for the first map -/ lemma cone_parallel_pair_ext (s : cone (parallel_pair f g)) {W : C} {k l : W ⟶ s.X} (h : k ≫ s.π.app zero = l ≫ s.π.app zero) : ∀ (j : walking_parallel_pair), k ≫ s.π.app j = l ≫ s.π.app j | zero := h | one := by { rw [←cone_parallel_pair_left, ←category.assoc, ←category.assoc], congr, exact h } /-- To check whether two maps are coequalized by both maps of a cofork, it suffices to check it for the second map -/ lemma cocone_parallel_pair_ext (s : cocone (parallel_pair f g)) {W : C} {k l : s.X ⟶ W} (h : s.ι.app one ≫ k = s.ι.app one ≫ l) : ∀ (j : walking_parallel_pair), s.ι.app j ≫ k = s.ι.app j ≫ l | zero := by { rw [←cocone_parallel_pair_right, category.assoc, category.assoc], congr, exact h } | one := h def fork.of_ι {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : fork f g := { X := P, π := { app := λ X, begin cases X, exact ι, exact ι ≫ f, end, naturality' := λ X Y f, begin cases X; cases Y; cases f; dsimp; simp, { dsimp, simp, }, -- TODO If someone could decipher why these aren't done on the previous line, that would be great { exact w }, { dsimp, simp, }, -- TODO idem end } } def cofork.of_π {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : cofork f g := { X := P, ι := { app := λ X, begin cases X, exact f ≫ π, exact π, end, naturality' := λ X Y f, begin cases X; cases Y; cases f; dsimp; simp, { dsimp, simp, }, -- TODO idem { exact w.symm }, { dsimp, simp, }, -- TODO idem end } } @[simp] lemma fork.of_ι_app_zero {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : (fork.of_ι ι w).π.app zero = ι := rfl @[simp] lemma fork.of_ι_app_one {P : C} (ι : P ⟶ X) (w : ι ≫ f = ι ≫ g) : (fork.of_ι ι w).π.app one = ι ≫ f := rfl @[simp] lemma cofork.of_π_app_zero {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : (cofork.of_π π w).ι.app zero = f ≫ π := rfl @[simp] lemma cofork.of_π_app_one {P : C} (π : Y ⟶ P) (w : f ≫ π = g ≫ π) : (cofork.of_π π w).ι.app one = π := rfl def fork.ι (t : fork f g) := t.π.app zero def cofork.π (t : cofork f g) := t.ι.app one lemma fork.condition (t : fork f g) : (fork.ι t) ≫ f = (fork.ι t) ≫ g := begin erw [t.w left, ← t.w right], refl end lemma cofork.condition (t : cofork f g) : f ≫ (cofork.π t) = g ≫ (cofork.π t) := begin erw [t.w left, ← t.w right], refl end /-- This is a slightly more convenient method to verify that a fork is a limit cone. It only asks for a proof of facts that carry any mathematical content -/ def fork.is_limit.mk (t : fork f g) (lift : Π (s : fork f g), s.X ⟶ t.X) (fac : ∀ (s : fork f g), lift s ≫ fork.ι t = fork.ι s) (uniq : ∀ (s : fork f g) (m : s.X ⟶ t.X) (w : ∀ j : walking_parallel_pair, m ≫ t.π.app j = s.π.app j), m = lift s) : is_limit t := { lift := lift, fac' := λ s j, walking_parallel_pair.cases_on j (fac s) $ by erw [←s.w left, ←t.w left, ←category.assoc, fac]; refl, uniq' := uniq } /-- This is a slightly more convenient method to verify that a cofork is a colimit cocone. It only asks for a proof of facts that carry any mathematical content -/ def cofork.is_colimit.mk (t : cofork f g) (desc : Π (s : cofork f g), t.X ⟶ s.X) (fac : ∀ (s : cofork f g), cofork.π t ≫ desc s = cofork.π s) (uniq : ∀ (s : cofork f g) (m : t.X ⟶ s.X) (w : ∀ j : walking_parallel_pair, t.ι.app j ≫ m = s.ι.app j), m = desc s) : is_colimit t := { desc := desc, fac' := λ s j, walking_parallel_pair.cases_on j (by erw [←s.w left, ←t.w left, category.assoc, fac]; refl) (fac s), uniq' := uniq } section local attribute [ext] cone /-- The fork induced by the ι map of some fork `t` is the same as `t` -/ lemma fork.eq_of_ι_ι (t : fork f g) : t = fork.of_ι (fork.ι t) (fork.condition t) := begin have h : t.π = (fork.of_ι (fork.ι t) (fork.condition t)).π, { ext j, cases j, { refl }, { rw ←cone_parallel_pair_left, refl } }, tidy end end def cone.of_fork {F : walking_parallel_pair.{v} ⥤ C} (t : fork (F.map left) (F.map right)) : cone F := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy), naturality' := λ j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } section local attribute [ext] cocone /-- The cofork induced by the π map of some fork `t` is the same as `t` -/ lemma cofork.eq_of_π_π (t : cofork f g) : t = cofork.of_π (cofork.π t) (cofork.condition t) := begin have h : t.ι = (cofork.of_π (cofork.π t) (cofork.condition t)).ι, { ext j, cases j, { rw ←cocone_parallel_pair_left, refl }, { refl } }, tidy end end def cocone.of_cofork {F : walking_parallel_pair.{v} ⥤ C} (t : cofork (F.map left) (F.map right)) : cocone F := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X, naturality' := λ j j' g, by { cases j; cases j'; cases g; dsimp; simp } } } @[simp] lemma cone.of_fork_π {F : walking_parallel_pair.{v} ⥤ C} (t : fork (F.map left) (F.map right)) (j) : (cone.of_fork t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cocone.of_cofork_ι {F : walking_parallel_pair.{v} ⥤ C} (t : cofork (F.map left) (F.map right)) (j) : (cocone.of_cofork t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl def fork.of_cone {F : walking_parallel_pair.{v} ⥤ C} (t : cone F) : fork (F.map left) (F.map right) := { X := t.X, π := { app := λ X, t.π.app X ≫ eq_to_hom (by tidy) } } def cofork.of_cocone {F : walking_parallel_pair.{v} ⥤ C} (t : cocone F) : cofork (F.map left) (F.map right) := { X := t.X, ι := { app := λ X, eq_to_hom (by tidy) ≫ t.ι.app X } } @[simp] lemma fork.of_cone_π {F : walking_parallel_pair.{v} ⥤ C} (t : cone F) (j) : (fork.of_cone t).π.app j = t.π.app j ≫ eq_to_hom (by tidy) := rfl @[simp] lemma cofork.of_cocone_ι {F : walking_parallel_pair.{v} ⥤ C} (t : cocone F) (j) : (cofork.of_cocone t).ι.app j = eq_to_hom (by tidy) ≫ t.ι.app j := rfl variables (f g) section variables [has_limit (parallel_pair f g)] abbreviation equalizer := limit (parallel_pair f g) abbreviation equalizer.ι : equalizer f g ⟶ X := limit.π (parallel_pair f g) zero @[simp] lemma equalizer.ι.fork : fork.ι (limit.cone (parallel_pair f g)) = equalizer.ι f g := rfl @[simp] lemma equalizer.ι.eq_app_zero : (limit.cone (parallel_pair f g)).π.app zero = equalizer.ι f g := rfl @[reassoc] lemma equalizer.condition : equalizer.ι f g ≫ f = equalizer.ι f g ≫ g := fork.condition $ limit.cone $ parallel_pair f g variables {f g} abbreviation equalizer.lift {W : C} (k : W ⟶ X) (h : k ≫ f = k ≫ g) : W ⟶ equalizer f g := limit.lift (parallel_pair f g) (fork.of_ι k h) /-- Two maps into an equalizer are equal if they are are equal when composed with the equalizer map. -/ @[ext] lemma equalizer.hom_ext {W : C} {k l : W ⟶ equalizer f g} (h : k ≫ equalizer.ι f g = l ≫ equalizer.ι f g) : k = l := limit.hom_ext $ cone_parallel_pair_ext _ h /-- An equalizer morphism is a monomorphism -/ instance equalizer.ι_mono : mono (equalizer.ι f g) := { right_cancellation := λ Z h k w, equalizer.hom_ext w } end section variables {f g} /-- The equalizer morphism in any limit cone is a monomorphism. -/ lemma mono_of_is_limit_parallel_pair {c : cone (parallel_pair f g)} (i : is_limit c) : mono (c.π.app zero) := { right_cancellation := λ Z h k w, i.hom_ext $ cone_parallel_pair_ext _ w } end section /-- The identity determines a cone on the equalizer diagram of f and f -/ def cone_parallel_pair_self : cone (parallel_pair f f) := { X := X, π := { app := λ j, match j with | zero := 𝟙 X | one := f end } } @[simp] lemma cone_parallel_pair_self_π_app_zero : (cone_parallel_pair_self f).π.app zero = 𝟙 X := rfl @[simp] lemma cone_parallel_pair_self_X : (cone_parallel_pair_self f).X = X := rfl /-- The identity on X is an equalizer of (f, f) -/ def is_limit_cone_parallel_pair_self : is_limit (cone_parallel_pair_self f) := { lift := λ s, s.π.app zero, fac' := λ s j, match j with | zero := by erw category.comp_id | one := by erw cone_parallel_pair_left end, uniq' := λ s m w, by { convert w zero, erw category.comp_id } } /-- Every equalizer of (f, f) is an isomorphism -/ def limit_cone_parallel_pair_self_is_iso (c : cone (parallel_pair f f)) (h : is_limit c) : is_iso (c.π.app zero) := let c' := cone_parallel_pair_self f, z : c ≅ c' := is_limit.unique_up_to_iso h (is_limit_cone_parallel_pair_self f) in is_iso.of_iso (functor.map_iso (cones.forget _) z) /-- The equalizer of (f, f) is an isomorphism -/ def equalizer.ι_of_self [has_limit (parallel_pair f f)] : is_iso (equalizer.ι f f) := limit_cone_parallel_pair_self_is_iso _ _ $ limit.is_limit _ /-- Every equalizer of (f, g), where f = g, is an isomorphism -/ def limit_cone_parallel_pair_self_is_iso' (c : cone (parallel_pair f g)) (h₀ : is_limit c) (h₁ : f = g) : is_iso (c.π.app zero) := begin rw fork.eq_of_ι_ι c at *, have h₂ : is_limit (fork.of_ι (c.π.app zero) rfl : fork f f), by convert h₀, exact limit_cone_parallel_pair_self_is_iso f (fork.of_ι (c.π.app zero) rfl) h₂ end /-- The equalizer of (f, g), where f = g, is an isomorphism -/ def equalizer.ι_of_self' [has_limit (parallel_pair f g)] (h : f = g) : is_iso (equalizer.ι f g) := limit_cone_parallel_pair_self_is_iso' _ _ _ (limit.is_limit _) h /-- An equalizer that is an epimorphism is an isomorphism -/ def epi_limit_cone_parallel_pair_is_iso (c : cone (parallel_pair f g)) (h : is_limit c) [epi (c.π.app zero)] : is_iso (c.π.app zero) := limit_cone_parallel_pair_self_is_iso' f g c h $ (cancel_epi (c.π.app zero)).1 (fork.condition c) end section variables [has_colimit (parallel_pair f g)] abbreviation coequalizer := colimit (parallel_pair f g) abbreviation coequalizer.π : Y ⟶ coequalizer f g := colimit.ι (parallel_pair f g) one @[simp] lemma coequalizer.π.cofork : cofork.π (colimit.cocone (parallel_pair f g)) = coequalizer.π f g := rfl @[simp] lemma coequalizer.π.eq_app_one : (colimit.cocone (parallel_pair f g)).ι.app one = coequalizer.π f g := rfl @[reassoc] lemma coequalizer.condition : f ≫ coequalizer.π f g = g ≫ coequalizer.π f g := cofork.condition $ colimit.cocone $ parallel_pair f g variables {f g} abbreviation coequalizer.desc {W : C} (k : Y ⟶ W) (h : f ≫ k = g ≫ k) : coequalizer f g ⟶ W := colimit.desc (parallel_pair f g) (cofork.of_π k h) /-- Two maps from a coequalizer are equal if they are equal when composed with the coequalizer map -/ @[ext] lemma coequalizer.hom_ext {W : C} {k l : coequalizer f g ⟶ W} (h : coequalizer.π f g ≫ k = coequalizer.π f g ≫ l) : k = l := colimit.hom_ext $ cocone_parallel_pair_ext _ h /-- A coequalizer morphism is an epimorphism -/ instance coequalizer.π_epi : epi (coequalizer.π f g) := { left_cancellation := λ Z h k w, coequalizer.hom_ext w } end section variables {f g} /-- The coequalizer morphism in any colimit cocone is an epimorphism. -/ lemma epi_of_is_colimit_parallel_pair {c : cocone (parallel_pair f g)} (i : is_colimit c) : epi (c.ι.app one) := { left_cancellation := λ Z h k w, i.hom_ext $ cocone_parallel_pair_ext _ w } end section /-- The identity determines a cocone on the coequalizer diagram of f and f -/ def cocone_parallel_pair_self : cocone (parallel_pair f f) := { X := Y, ι := { app := λ j, match j with | zero := f | one := 𝟙 Y end, naturality' := λ j j' g, begin cases g, { refl }, { erw category.comp_id _ f }, { dsimp, simp } end } } @[simp] lemma cocone_parallel_pair_self_ι_app_one : (cocone_parallel_pair_self f).ι.app one = 𝟙 Y := rfl @[simp] lemma cocone_parallel_pair_self_X : (cocone_parallel_pair_self f).X = Y := rfl /-- The identity on Y is a colimit of (f, f) -/ def is_colimit_cocone_parallel_pair_self : is_colimit (cocone_parallel_pair_self f) := { desc := λ s, s.ι.app one, fac' := λ s j, match j with | zero := by erw cocone_parallel_pair_right | one := by erw category.id_comp end, uniq' := λ s m w, by { convert w one, erw category.id_comp } } /-- Every coequalizer of (f, f) is an isomorphism -/ def colimit_cocone_parallel_pair_self_is_iso (c : cocone (parallel_pair f f)) (h : is_colimit c) : is_iso (c.ι.app one) := let c' := cocone_parallel_pair_self f, z : c' ≅ c := is_colimit.unique_up_to_iso (is_colimit_cocone_parallel_pair_self f) h in is_iso.of_iso $ functor.map_iso (cocones.forget _) z /-- The coequalizer of (f, f) is an isomorphism -/ def coequalizer.π_of_self [has_colimit (parallel_pair f f)] : is_iso (coequalizer.π f f) := colimit_cocone_parallel_pair_self_is_iso _ _ $ colimit.is_colimit _ /-- Every coequalizer of (f, g), where f = g, is an isomorphism -/ def colimit_cocone_parallel_pair_self_is_iso' (c : cocone (parallel_pair f g)) (h₀ : is_colimit c) (h₁ : f = g) : is_iso (c.ι.app one) := begin rw cofork.eq_of_π_π c at *, have h₂ : is_colimit (cofork.of_π (c.ι.app one) rfl : cofork f f), by convert h₀, exact colimit_cocone_parallel_pair_self_is_iso f (cofork.of_π (c.ι.app one) rfl) h₂ end /-- The coequalizer of (f, g), where f = g, is an isomorphism -/ def coequalizer.π_of_self' [has_colimit (parallel_pair f g)] (h : f = g) : is_iso (coequalizer.π f g) := colimit_cocone_parallel_pair_self_is_iso' _ _ _ (colimit.is_colimit _) h /-- A coequalizer that is a monomorphism is an isomorphism -/ def mono_limit_cocone_parallel_pair_is_iso (c : cocone (parallel_pair f g)) (h : is_colimit c) [mono (c.ι.app one)] : is_iso (c.ι.app one) := colimit_cocone_parallel_pair_self_is_iso' f g c h $ (cancel_mono (c.ι.app one)).1 (cofork.condition c) end variables (C) /-- `has_equalizers` represents a choice of equalizer for every pair of morphisms -/ class has_equalizers := (has_limits_of_shape : has_limits_of_shape.{v} walking_parallel_pair C) /-- `has_coequalizers` represents a choice of coequalizer for every pair of morphisms -/ class has_coequalizers := (has_colimits_of_shape : has_colimits_of_shape.{v} walking_parallel_pair C) attribute [instance] has_equalizers.has_limits_of_shape has_coequalizers.has_colimits_of_shape /-- Equalizers are finite limits, so if `C` has all finite limits, it also has all equalizers -/ def has_equalizers_of_has_finite_limits [has_finite_limits.{v} C] : has_equalizers.{v} C := { has_limits_of_shape := infer_instance } /-- Coequalizers are finite colimits, of if `C` has all finite colimits, it also has all coequalizers -/ def has_coequalizers_of_has_finite_colimits [has_finite_colimits.{v} C] : has_coequalizers.{v} C := { has_colimits_of_shape := infer_instance } /-- If `C` has all limits of diagrams `parallel_pair f g`, then it has all equalizers -/ def has_equalizers_of_has_limit_parallel_pair [Π {X Y : C} {f g : X ⟶ Y}, has_limit (parallel_pair f g)] : has_equalizers.{v} C := { has_limits_of_shape := { has_limit := λ F, has_limit_of_iso (diagram_iso_parallel_pair F).symm } } /-- If `C` has all colimits of diagrams `parallel_pair f g`, then it has all coequalizers -/ def has_coequalizers_of_has_colimit_parallel_pair [Π {X Y : C} {f g : X ⟶ Y}, has_colimit (parallel_pair f g)] : has_coequalizers.{v} C := { has_colimits_of_shape := { has_colimit := λ F, has_colimit_of_iso (diagram_iso_parallel_pair F) } } section -- In this section we show that a split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. variables {C} [split_mono f] /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. Here we build the cone, and show in `split_mono_equalizes` that it is a limit cone. -/ def cone_of_split_mono : cone (parallel_pair (𝟙 Y) (retraction f ≫ f)) := fork.of_ι f (by tidy) @[simp] lemma cone_of_split_mono_π_app_zero : (cone_of_split_mono f).π.app zero = f := rfl @[simp] lemma cone_of_split_mono_π_app_one : (cone_of_split_mono f).π.app one = f ≫ 𝟙 Y := rfl /-- A split mono `f` equalizes `(retraction f ≫ f)` and `(𝟙 Y)`. -/ def split_mono_equalizes {X Y : C} (f : X ⟶ Y) [split_mono f] : is_limit (cone_of_split_mono f) := { lift := λ s, s.π.app zero ≫ retraction f, fac' := λ s, begin rintros (⟨⟩|⟨⟩), { rw [cone_of_split_mono_π_app_zero], erw [category.assoc, ← s.π.naturality right, s.π.naturality left, category.comp_id], }, { erw [cone_of_split_mono_π_app_one, category.comp_id, category.assoc, ← s.π.naturality right, category.id_comp], } end, uniq' := λ s m w, begin rw ←(w zero), simp, end, } end section -- In this section we show that a split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. variables {C} [split_epi f] /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. Here we build the cocone, and show in `split_epi_coequalizes` that it is a colimit cocone. -/ def cocone_of_split_epi : cocone (parallel_pair (𝟙 X) (f ≫ section_ f)) := cofork.of_π f (by tidy) @[simp] lemma cocone_of_split_epi_ι_app_one : (cocone_of_split_epi f).ι.app one = f := rfl @[simp] lemma cocone_of_split_epi_ι_app_zero : (cocone_of_split_epi f).ι.app zero = 𝟙 X ≫ f := rfl /-- A split epi `f` coequalizes `(f ≫ section_ f)` and `(𝟙 X)`. -/ def split_epi_coequalizes {X Y : C} (f : X ⟶ Y) [split_epi f] : is_colimit (cocone_of_split_epi f) := { desc := λ s, section_ f ≫ s.ι.app one, fac' := λ s, begin rintros (⟨⟩|⟨⟩), { erw [cocone_of_split_epi_ι_app_zero, category.assoc, category.id_comp, ←category.assoc, s.ι.naturality right, functor.const.obj_map, category.comp_id], }, { erw [cocone_of_split_epi_ι_app_one, ←category.assoc, s.ι.naturality right, ←s.ι.naturality left, category.id_comp] } end, uniq' := λ s m w, begin rw ←(w one), simp, end, } end end category_theory.limits
94a1da3eed022de5f36e46b44af1dd77be1ec8d8
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/category_theory/limits/shapes/regular_mono.lean
dad824a283b53f2df1eb946bcb0f225f9c664d8f
[ "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
3,182
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 category_theory.epi_mono import category_theory.limits.shapes.kernels /-! # Definitions and basic properties of regular and normal monomorphisms and epimorphisms. A regular monomorphism is a morphism that is the equalizer of some parallel pair. A normal monomorphism is a morphism that is the kernel of some other morphism. We give the constructions * `split_mono → regular_mono` * `normal_mono → regular_mono`, and * `regular_mono → mono`. -/ namespace category_theory open category_theory.limits universes v₁ u₁ variables {C : Type u₁} [𝒞 : category.{v₁} C] include 𝒞 variables {X Y : C} /-- A regular monomorphism is a morphism which is the equalizer of some parallel pair. -/ class regular_mono (f : X ⟶ Y) := (Z : C) (left right : Y ⟶ Z) (w : f ≫ left = f ≫ right) (is_limit : is_limit (fork.of_ι f w)) /-- Every regular monomorphism is a monomorphism. -/ @[priority 100] instance regular_mono.mono (f : X ⟶ Y) [regular_mono f] : mono f := mono_of_is_limit_parallel_pair regular_mono.is_limit /-- Every split monomorphism is a regular monomorphism. -/ @[priority 100] instance regular_mono.of_split_mono (f : X ⟶ Y) [split_mono f] : regular_mono f := { Z := Y, left := 𝟙 Y, right := retraction f ≫ f, w := by tidy, is_limit := split_mono_equalizes f } section variables [has_zero_morphisms.{v₁} C] /-- A normal monomorphism is a morphism which is the kernel of some morphism. -/ class normal_mono (f : X ⟶ Y) := (Z : C) (g : Y ⟶ Z) (w : f ≫ g = 0) (is_limit : is_limit (kernel_fork.of_ι f w)) /-- Every normal monomorphism is a regular monomorphism. -/ @[priority 100] instance normal_mono.regular_mono (f : X ⟶ Y) [I : normal_mono f] : regular_mono f := { left := I.g, right := 0, w := (by simpa using I.w), ..I } end /-- A regular epimorphism is a morphism which is the coequalizer of some parallel pair. -/ class regular_epi (f : X ⟶ Y) := (W : C) (left right : W ⟶ X) (w : left ≫ f = right ≫ f) (is_colimit : is_colimit (cofork.of_π f w)) /-- Every regular epimorphism is an epimorphism. -/ @[priority 100] instance regular_epi.epi (f : X ⟶ Y) [regular_epi f] : epi f := epi_of_is_colimit_parallel_pair regular_epi.is_colimit /-- Every split epimorphism is a regular epimorphism. -/ @[priority 100] instance regular_epi.of_split_epi (f : X ⟶ Y) [split_epi f] : regular_epi f := { W := X, left := 𝟙 X, right := f ≫ section_ f, w := by tidy, is_colimit := split_epi_coequalizes f } section variables [has_zero_morphisms.{v₁} C] /-- A normal epimorphism is a morphism which is the cokernel of some morphism. -/ class normal_epi (f : X ⟶ Y) := (W : C) (g : W ⟶ X) (w : g ≫ f = 0) (is_colimit : is_colimit (cokernel_cofork.of_π f w)) /-- Every normal epimorphism is a regular epimorphism. -/ @[priority 100] instance normal_epi.regular_epi (f : X ⟶ Y) [I : normal_epi f] : regular_epi f := { left := I.g, right := 0, w := (by simpa using I.w), ..I } end end category_theory
b94f44a388e82aeed1f398764828cf544da15fa4
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/int/bitwise.lean
73fb86f91588fd78cb26a671f87a79542f515f0c
[ "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,059
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.nat.pow import data.nat.size /-! # Bitwise operations on integers ## Recursors * `int.bit_cases_on`: Parity disjunction. Something is true/defined on `ℤ` if it's true/defined for even and for odd values. -/ namespace int /-! ### bitwise ops -/ @[simp] lemma bodd_zero : bodd 0 = ff := rfl @[simp] lemma bodd_one : bodd 1 = tt := rfl lemma bodd_two : bodd 2 = ff := rfl @[simp, norm_cast] lemma bodd_coe (n : ℕ) : int.bodd n = nat.bodd n := 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; simp; cases i.bodd; simp @[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; simp [has_neg.neg, int.coe_nat_eq, int.neg, bodd, -of_nat_eq_coe] @[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, -of_nat_eq_coe, bool.bxor_comm] @[simp] lemma bodd_mul (m n : ℤ) : bodd (m * n) = bodd m && bodd n := by cases m with m m; cases n with n n; simp [← int.mul_def, int.mul, -of_nat_eq_coe, bool.bxor_comm] 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 _ /-- Defines a function from `ℤ` conditionally, if it is defined for odd and even integers separately using `bit`. -/ 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 bodd_bit0 (n : ℤ) : bodd (bit0 n) = ff := bodd_bit ff n @[simp] lemma bodd_bit1 (n : ℤ) : bodd (bit1 n) = tt := bodd_bit tt n lemma bit0_ne_bit1 (m n : ℤ) : bit0 m ≠ bit1 n := mt (congr_arg bodd) $ by simp lemma bit1_ne_bit0 (m n : ℤ) : bit1 m ≠ bit0 n := (bit0_ne_bit1 _ _).symm lemma bit1_ne_zero (m : ℤ) : bit1 m ≠ 0 := by simpa only [bit0_zero] using bit1_ne_bit0 m 0 @[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] @[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] /-! ### bitwise ops -/ local attribute [simp] int.zero_div 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, add_tsub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg coe $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, tsub_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, add_tsub_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, tsub_self]; refl) lemma shiftl_sub (m : ℤ) (n : ℕ) (k : ℤ) : shiftl m (n - k) = shiftr (shiftl m n) k := shiftl_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 (pow_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 _ end int
65cc514acc582f0472f09932e98426a6ba0da090
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/noncomputable_lemma.lean
2ffcb72ed6c6913b5e2ebfc8984992be8bf660a8
[ "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
187
lean
noncomputable lemma a : ℕ := 37 lemma b : ℕ := 37 -- do not report missing noncomputable: lemma sorry_type : sorry := sorry -- do not report missing noncomputable: lemma incomplete :
ab167c3cc06413a11ee9f17385f453383cd4c672
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world6/level4.lean
73e6aeda5998228dee2d72a379a794cba5612441
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
1,280
lean
/- # Proposition world. ## Level 4: `apply`. Let's do the same level again: ![diagram](https://wwwf.imperial.ac.uk/~buzzard/xena/natural_number_game_images/implies_diag.jpg) We are given a proof $p$ of $P$ and our goal is to find a proof of $U$, or in other words to find a path through the maze that links $P$ to $U$. In level 3 we solved this by using `have`s to move forward, from $P$ to $Q$ to $T$ to $U$. Using the `apply` tactic we can instead construct the path backwards, moving from $U$ to $T$ to $Q$ to $P$. Our goal is to prove $U$. But $l:T\implies U$ is an implication which we are assuming, so it would suffice to prove $T$. Tell Lean this by starting the proof below with `apply l,` and notice that our assumptions don't change but *the goal changes* from `⊢ U` to `⊢ T`. Keep `apply`ing implications until your goal is `P`, and try not to get lost! Now solve this goal with `exact p`. Note: you will need to learn the difference between `exact p` (which works) and `exact P` (which doesn't, because $P$ is not a proof of $P$). -/ /- Lemma : no-side-bar We can solve a maze. -/ lemma maze (P Q R S T U: Prop) (p : P) (h : P → Q) (i : Q → R) (j : Q → T) (k : S → T) (l : T → U) : U := begin apply l, apply j, apply h, exact p, end
63cf1fdf12e50fa1c6343a0cf271a725961c4c35
c8af905dcd8475f414868d303b2eb0e9d3eb32f9
/src/data/cpi/semantics/space.lean
9b915ac2b11700ae4617a13252f5c03d728a6187
[ "BSD-3-Clause" ]
permissive
continuouspi/lean-cpi
81480a13842d67ff5f3698643210d8ed5dd08de4
443bf2cb236feadc45a01387099c236ab2b78237
refs/heads/master
1,650,307,316,582
1,587,033,364,000
1,587,033,364,000
207,499,661
1
0
null
null
null
null
UTF-8
Lean
false
false
5,333
lean
import data.cpi.semantics.relation import data.fin_fn namespace cpi /-- Determine if two concretions are equal. Effectively a decision procedure for structural congruence. -/ instance concretion_wrap.decidable_eq {ℍ ω Γ} [cpi_equiv ℍ ω] : decidable_eq ( prime_species' ℍ ω Γ × (Σ (b y : ℕ), concretion' ℍ ω Γ b y) × name Γ) | a b := by apply_instance /-- A vector-space representation of processes, mapping prime species into their concentrations. -/ @[nolint has_inhabited_instance] def process_space (ℂ ℍ : Type) (ω Γ : context) [add_monoid ℂ] [cpi_equiv ℍ ω] := fin_fn (prime_species' ℍ ω Γ) ℂ variables {ℂ ℍ : Type} {ω : context} [half_ring ℂ] [cpi_equiv ℍ ω] [decidable_eq ℂ] instance process_space.add_comm_monoid {Γ} : add_comm_group (process_space ℂ ℍ ω Γ) := fin_fn.add_comm_group _ ℂ instance process_space.semimodule {Γ} : semimodule ℂ (process_space ℂ ℍ ω Γ) := fin_fn.semimodule _ ℂ instance process_space.has_repr {ℂ} [add_monoid ℂ] [has_repr ℂ] {Γ} [has_repr (species' ℍ ω Γ)] : has_repr (process_space ℂ ℍ ω Γ) := ⟨ fin_fn.to_string "\n" ⟩ /-- Convert a species into a process space with a unit vector for each element of the prime decomposition. This is defined as ⟨A⟩ within the paper. -/ def to_process_space {Γ} (A : species' ℍ ω Γ) : process_space ℂ ℍ ω Γ := (cpi_equiv.prime_decompose' A).sum' (λ A, fin_fn.single A 1) @[simp] lemma to_process_space.nil {Γ} : to_process_space ⟦nil⟧ = (0 : process_space ℂ ℍ ω Γ) := by simp only [to_process_space, cpi_equiv.prime_decompose_nil', multiset.sum'_zero] @[simp] lemma to_process_space.prime {Γ} (A : prime_species' ℍ ω Γ) : (to_process_space (prime_species.unwrap A) : process_space ℂ ℍ ω Γ) = fin_fn.single A 1 := by simp only [to_process_space, cpi_equiv.prime_decompose_prime', multiset.sum'_singleton] @[simp] lemma to_process_space.parallel {Γ} (A B : species ℍ ω Γ) : (to_process_space ⟦ A |ₛ B ⟧ : process_space ℂ ℍ ω Γ) = to_process_space ⟦ A ⟧ + to_process_space ⟦ B ⟧ := by simp only [to_process_space, cpi_equiv.prime_decompose_parallel', multiset.sum'_add] /-- The vector space (A, E, a)→ℍ relating transitions from A to E with label #a. -/ @[nolint has_inhabited_instance] def interaction_space (ℂ ℍ : Type) (ω Γ : context) [add_monoid ℂ] [cpi_equiv ℍ ω] : Type := fin_fn ( prime_species' ℍ ω Γ × (Σ (b y), concretion' ℍ ω Γ b y) × name Γ) ℂ instance interaction_space.add_comm_monoid {Γ} : add_comm_group (interaction_space ℂ ℍ ω Γ) := fin_fn.add_comm_group _ ℂ instance interaction_space.semimodule {Γ} : semimodule ℂ (interaction_space ℂ ℍ ω Γ) := fin_fn.semimodule _ ℂ instance interaction_space.has_repr {ℂ} [add_monoid ℂ] [has_repr ℂ] {Γ} [has_repr (species' ℍ ω Γ)] [∀ b y, has_repr (concretion' ℍ ω Γ b y)] : has_repr (interaction_space ℂ ℍ ω Γ) := ⟨ @fin_fn.to_string ( prime_species' ℍ ω Γ × (Σ (b y), concretion' ℍ ω Γ b y) × name Γ) ℂ _ ⟨ λ ⟨ A, ⟨ _, _, F ⟩, a ⟩, "[" ++ repr A ++ "], [" ++ repr F ++ "], " ++ repr a ⟩ _ "\n" ⟩ /-- Convert a process into a process space. -/ def process.to_space {Γ} : process ℂ ℍ ω Γ → process_space ℂ ℍ ω Γ | (c ◯ A) := c • to_process_space ⟦ A ⟧ | (P |ₚ Q) := process.to_space P + process.to_space Q /-- Convert a process space into a process. -/ def process.from_space {ℂ} [add_comm_monoid ℂ] {Γ} : process_space ℂ ℍ ω Γ → process' ℂ ℍ ω Γ | Ps := process.from_prime_multiset Ps.space Ps.support.val /-- Convert a class of equivalent processes into a process space. -/ def process.to_space' {Γ} : process' ℂ ℍ ω Γ → process_space ℂ ℍ ω Γ | P := quot.lift_on P process.to_space (λ P Q eq, begin induction eq, case process.equiv.refl { from rfl }, case process.equiv.trans : P Q R _ _ pq qr { from trans pq qr }, case process.equiv.symm : P Q _ ih { from symm ih }, case process.equiv.ξ_species : c A A' eq { simp only [process.to_space, quotient.sound eq], }, case process.equiv.ξ_parallel₁ : P P' Q _ ih { simp only [process.to_space, ih] }, case process.equiv.ξ_parallel₂ : P Q Q' _ ih { simp only [process.to_space, ih] }, case process.equiv.parallel_nil : P c { simp only [process.to_space, cpi.to_process_space.nil, smul_zero, add_zero], }, case process.equiv.parallel_symm { simp only [process.to_space, add_comm] }, case process.equiv.parallel_assoc { simp only [process.to_space, add_assoc] }, case cpi.process.equiv.join : A c d { simp only [process.to_space, add_smul] }, case cpi.process.equiv.split : A B c { simp only [process.to_space, to_process_space.parallel, smul_add], } end) axiom process.from_inverse {Γ} : function.left_inverse process.to_space' (@process.from_space ℍ ω _ ℂ _ Γ) /-- Show that process spaces can be embeeded into equivalence classes of processes. -/ def process.space_embed {Γ} : process_space ℂ ℍ ω Γ ↪ process' ℂ ℍ ω Γ := { to_fun := process.from_space, inj := function.injective_of_left_inverse process.from_inverse } end cpi #lint-
90ca990aaf6acdaa835bbc8f65f442ebb6c56738
54ce0561cebde424526f41d45f490ed56be2bd0c
/src/game/ch3_Set_Theory/1_Fundamentals.lean
ae5a1415d60c55a4b8a75701b8c7d5011b4d0272
[]
no_license
melembroucarlitos/Tao_Analysis-LEAN
cf7b3298d317891a09e4bf21cfe7c7ffcb57b9a9
3f4fc7e090d96b6cef64896492fba4bef124794b
refs/heads/master
1,692,952,385,694
1,636,287,522,000
1,636,287,522,000
400,630,166
3
0
null
1,635,910,807,000
1,630,096,823,000
Lean
UTF-8
Lean
false
false
163
lean
-- Level name : Fundamentals /- # Hey yall ## This is just to a placeholder to make sure all is working these are some words and these are some other words -/
5b75bafa4d2fce91bd50571ab921c871ba2d9424
d642a6b1261b2cbe691e53561ac777b924751b63
/test/tactics.lean
d86d9deecb565c3a07de5ccc21733b9b255583fb
[ "Apache-2.0" ]
permissive
cipher1024/mathlib
fee56b9954e969721715e45fea8bcb95f9dc03fe
d077887141000fefa5a264e30fa57520e9f03522
refs/heads/master
1,651,806,490,504
1,573,508,694,000
1,573,508,694,000
107,216,176
0
0
Apache-2.0
1,647,363,136,000
1,508,213,014,000
Lean
UTF-8
Lean
false
false
11,976
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Scott Morrison -/ import tactic.interactive tactic.finish tactic.ext tactic.lift tactic.apply tactic.reassoc_axiom tactic.tfae tactic.elide example (m n p q : nat) (h : m + n = p) : true := begin have : m + n = q, { generalize_hyp h' : m + n = x at h, guard_hyp h' := m + n = x, guard_hyp h := x = p, guard_target m + n = q, admit }, have : m + n = q, { generalize_hyp h' : m + n = x at h ⊢, guard_hyp h' := m + n = x, guard_hyp h := x = p, guard_target x = q, admit }, trivial end example (α : Sort*) (L₁ L₂ L₃ : list α) (H : L₁ ++ L₂ = L₃) : true := begin have : L₁ ++ L₂ = L₂, { generalize_hyp h : L₁ ++ L₂ = L at H, induction L with hd tl ih, case list.nil { tactic.cleanup, change list.nil = L₃ at H, admit }, case list.cons { change list.cons hd tl = L₃ at H, admit } }, trivial end example (x y : ℕ) (p q : Prop) (h : x = y) (h' : p ↔ q) : true := begin symmetry' at h, guard_hyp' h := y = x, guard_hyp' h' := p ↔ q, symmetry' at *, guard_hyp' h := x = y, guard_hyp' h' := q ↔ p, trivial end section apply_rules example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := add_le_add (add_le_add (add_le_add (add_le_add h1 (mul_le_mul_of_nonneg_right h2 h3)) h1 ) h2) h3 example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules [add_le_add, mul_le_mul_of_nonneg_right] @[user_attribute] meta def mono_rules : user_attribute := { name := `mono_rules, descr := "lemmas usable to prove monotonicity" } attribute [mono_rules] add_le_add mul_le_mul_of_nonneg_right example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules [mono_rules] example {a b c d e : nat} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := by apply_rules mono_rules end apply_rules section h_generalize variables {α β γ φ ψ : Type} (f : α → α → α → φ → γ) (x y : α) (a b : β) (z : φ) (h₀ : β = α) (h₁ : β = α) (h₂ : φ = β) (hx : x == a) (hy : y == b) (hz : z == a) include f x y z a b hx hy hz example : f x y x z = f (eq.rec_on h₀ a) (cast h₀ b) (eq.mpr h₁.symm a) (eq.mpr h₂ a) := begin guard_hyp_nums 16, h_generalize hp : a == p with hh, guard_hyp_nums 19, guard_hyp' hh := β = α, guard_target f x y x z = f p (cast h₀ b) p (eq.mpr h₂ a), h_generalize hq : _ == q, guard_hyp_nums 21, guard_target f x y x z = f p q p (eq.mpr h₂ a), h_generalize _ : _ == r, guard_hyp_nums 23, guard_target f x y x z = f p q p r, casesm* [_ == _, _ = _], refl end end h_generalize section h_generalize variables {α β γ φ ψ : Type} (f : list α → list α → γ) (x : list α) (a : list β) (z : φ) (h₀ : β = α) (h₁ : list β = list α) (hx : x == a) include f x z a hx h₀ h₁ example : true := begin have : f x x = f (eq.rec_on h₀ a) (cast h₁ a), { guard_hyp_nums 11, h_generalize : a == p with _, guard_hyp_nums 13, guard_hyp' h := β = α, guard_target f x x = f p (cast h₁ a), h_generalize! : a == q , guard_hyp_nums 13, guard_target ∀ q, f x x = f p q, casesm* [_ == _, _ = _], success_if_fail { refl }, admit }, trivial end end h_generalize section tfae example (p q r s : Prop) (h₀ : p ↔ q) (h₁ : q ↔ r) (h₂ : r ↔ s) : p ↔ s := begin scc, end example (p' p q r r' s s' : Prop) (h₀ : p' → p) (h₀ : p → q) (h₁ : q → r) (h₁ : r' → r) (h₂ : r ↔ s) (h₂ : s → p) (h₂ : s → s') : p ↔ s := begin scc, end example (p' p q r r' s s' : Prop) (h₀ : p' → p) (h₀ : p → q) (h₁ : q → r) (h₁ : r' → r) (h₂ : r ↔ s) (h₂ : s → p) (h₂ : s → s') : p ↔ s := begin scc', assumption end example : tfae [true, ∀ n : ℕ, 0 ≤ n * n, true, true] := begin tfae_have : 3 → 1, { intro h, constructor }, tfae_have : 2 → 3, { intro h, constructor }, tfae_have : 2 ← 1, { intros h n, apply nat.zero_le }, tfae_have : 4 ↔ 2, { tauto }, tfae_finish, end example : tfae [] := begin tfae_finish, end variables P Q R : Prop example : tfae [P, Q, R] := begin have : P → Q := sorry, have : Q → R := sorry, have : R → P := sorry, --have : R → Q := sorry, -- uncommenting this makes the proof fail tfae_finish end example : tfae [P, Q, R] := begin have : P → Q := sorry, have : Q → R := sorry, have : R → P := sorry, have : R → Q := sorry, -- uncommenting this makes the proof fail tfae_finish end example : tfae [P, Q, R] := begin have : P ↔ Q := sorry, have : Q ↔ R := sorry, tfae_finish -- the success or failure of this tactic is nondeterministic! end end tfae section clear_aux_decl example (n m : ℕ) (h₁ : n = m) (h₂ : ∃ a : ℕ, a = n ∧ a = m) : 2 * m = 2 * n := let ⟨a, ha⟩ := h₂ in begin clear_aux_decl, -- subst will fail without this line subst h₁ end example (x y : ℕ) (h₁ : ∃ n : ℕ, n * 1 = 2) (h₂ : 1 + 1 = 2 → x * 1 = y) : x = y := let ⟨n, hn⟩ := h₁ in begin clear_aux_decl, -- finish produces an error without this line finish end end clear_aux_decl section congr example (c : Prop → Prop → Prop → Prop) (x x' y z z' : Prop) (h₀ : x ↔ x') (h₁ : z ↔ z') : c x y z ↔ c x' y z' := begin congr', { guard_target x = x', ext, assumption }, { guard_target z = z', ext, assumption }, end end congr section convert_to example {a b c d : ℕ} (H : a = c) (H' : b = d) : a + b = d + c := by {convert_to c + d = _ using 2, from H, from H', rw[add_comm]} example {a b c d : ℕ} (H : a = c) (H' : b = d) : a + b = d + c := by {convert_to c + d = _ using 0, congr' 2, from H, from H', rw[add_comm]} example (a b c d e f g N : ℕ) : (a + b) + (c + d) + (e + f) + g ≤ a + d + e + f + c + g + b := by {ac_change a + d + e + f + c + g + b ≤ _, refl} end convert_to section swap example {α₁ α₂ α₃ : Type} : true := by {have : α₁, have : α₂, have : α₃, swap, swap, rotate, rotate, rotate, rotate 2, rotate 2, triv, recover} end swap section lift example (n m k x z u : ℤ) (hn : 0 < n) (hk : 0 ≤ k + n) (hu : 0 ≤ u) (h : k + n = 2 + x) : k + n = m + x := begin lift n to ℕ using le_of_lt hn, guard_target (k + ↑n = m + x), guard_hyp hn := (0 : ℤ) < ↑n, lift m to ℕ, guard_target (k + ↑n = ↑m + x), tactic.swap, guard_target (0 ≤ m), tactic.swap, tactic.num_goals >>= λ n, guard (n = 2), lift (k + n) to ℕ using hk with l hl, guard_hyp l := ℕ, guard_hyp hl := ↑l = k + ↑n, guard_target (↑l = ↑m + x), tactic.success_if_fail (tactic.get_local `hk), lift x to ℕ with y hy, guard_hyp y := ℕ, guard_hyp hy := ↑y = x, guard_target (↑l = ↑m + x), lift z to ℕ with w, guard_hyp w := ℕ, tactic.success_if_fail (tactic.get_local `z), lift u to ℕ using hu with u rfl hu, guard_hyp hu := (0 : ℤ) ≤ ↑u, all_goals { admit } end instance can_lift_unit : can_lift unit unit := ⟨id, λ x, true, λ x _, ⟨x, rfl⟩⟩ /- test whether new instances of `can_lift` are added as simp lemmas -/ run_cmd do l ← can_lift_attr.get_cache, guard (`can_lift_unit ∈ l) /- test error messages -/ example (n : ℤ) (hn : 0 < n) : true := begin success_if_fail_with_msg {lift n to ℕ using hn} "lift tactic failed. The type of\n hn\nis 0 < n\nbut it is expected to be\n 0 ≤ n", success_if_fail_with_msg {lift (n : option ℤ) to ℕ} "Failed to find a lift from option ℤ to ℕ. Provide an instance of\n can_lift (option ℤ) ℕ", trivial end end lift private meta def get_exception_message (t : lean.parser unit) : lean.parser string | s := match t s with | result.success a s' := result.success "No exception" s | result.exception none pos s' := result.success "Exception no msg" s | result.exception (some msg) pos s' := result.success (msg ()).to_string s end @[user_command] meta def test_parser1_fail_cmd (_ : interactive.parse (lean.parser.tk "test_parser1")) : lean.parser unit := do let msg := "oh, no!", let t : lean.parser unit := tactic.fail msg, s ← get_exception_message t, if s = msg then tactic.skip else interaction_monad.fail "Message was corrupted while being passed through `lean.parser.of_tactic`" . -- Due to `lean.parser.of_tactic'` priority, the following *should not* fail with -- a VM check error, and instead catch the error gracefully and just -- run and succeed silently. test_parser1 section category_theory open category_theory variables {C : Type} [category.{1} C] example (X Y Z W : C) (x : X ⟶ Y) (y : Y ⟶ Z) (z z' : Z ⟶ W) (w : X ⟶ Z) (h : x ≫ y = w) (h' : y ≫ z = y ≫ z') : x ≫ y ≫ z = w ≫ z' := begin rw [h',reassoc_of h], end end category_theory section is_eta_expansion /- test the is_eta_expansion tactic -/ open function tactic structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix ` ≃ `:25 := equiv protected def my_rfl {α} : α ≃ α := ⟨id, λ x, x, λ x, rfl, λ x, rfl⟩ def eta_expansion_test : ℕ × ℕ := ((1,0).1,(1,0).2) run_cmd do e ← get_env, x ← e.get `eta_expansion_test, let v := (x.value.get_app_args).drop 2, let nms := [`prod.fst, `prod.snd], guard $ expr.is_eta_expansion_test (nms.zip v) = some `((1, 0)) def eta_expansion_test2 : ℕ ≃ ℕ := ⟨my_rfl.to_fun, my_rfl.inv_fun, λ x, rfl, λ x, rfl⟩ run_cmd do e ← get_env, x ← e.get `eta_expansion_test2, let v := (x.value.get_app_args).drop 2, projs ← e.get_projections `equiv, b ← expr.is_eta_expansion_aux x.value (projs.zip v), guard $ b = some `(@my_rfl ℕ) run_cmd do e ← get_env, x1 ← e.get `eta_expansion_test, x2 ← e.get `eta_expansion_test2, b1 ← expr.is_eta_expansion x1.value, b2 ← expr.is_eta_expansion x2.value, guard $ b1 = some `((1, 0)) ∧ b2 = some `(@my_rfl ℕ) structure my_str (n : ℕ) := (x y : ℕ) def dummy : my_str 3 := ⟨3, 1, 1⟩ def wrong_param : my_str 2 := ⟨2, dummy.1, dummy.2⟩ def right_param : my_str 3 := ⟨3, dummy.1, dummy.2⟩ run_cmd do e ← get_env, x ← e.get `wrong_param, o ← x.value.is_eta_expansion, guard o.is_none, x ← e.get `right_param, o ← x.value.is_eta_expansion, guard $ o = some `(dummy) end is_eta_expansion section elide variables {x y z w : ℕ} variables (h : x + y + z ≤ w) (h' : x ≤ y + z + w) include h h' example : x + y + z ≤ w := begin elide 0 at h, elide 2 at h', guard_hyp h := @hidden _ (x + y + z ≤ w), guard_hyp h' := x ≤ @has_add.add (@hidden Type nat) (@hidden (has_add nat) nat.has_add) (@hidden ℕ (y + z)) (@hidden ℕ w), unelide at h, unelide at h', guard_hyp h' := x ≤ y + z + w, exact h, -- there was a universe problem in `elide`. `exact h` lets the kernel check -- the consistency of the universes end end elide section struct_eq @[ext] structure foo (α : Type*) := (x y : ℕ) (z : {z // z < x}) (k : α) (h : x < y) example {α : Type*} : Π (x y : foo α), x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y := foo.ext example {α} (x y : foo α) (h : x = y) : y = x := begin ext, { guard_target' y.x = x.x, rw h }, { guard_target' y.y = x.y, rw h }, { guard_target' y.z == x.z, rw h }, { guard_target' y.k = x.k, rw h }, end end struct_eq
c22f80fed77662a6488a8cf0cf2fbcb0f6a41279
f3849be5d845a1cb97680f0bbbe03b85518312f0
/old_library/system/IO.lean
27d33c3c7dc59ff5a6dcaf2ab3d866510e988798
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,026
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Nelson, Jared Roesch and Leonardo de Moura -/ constant {u} IO : Type u → Type u constant functorIO : functor IO constant monadIO : monad IO attribute [instance] functorIO monadIO constant put_str : string → IO unit constant put_nat : nat → IO unit constant get_line : IO string constant forever : IO unit -> IO unit definition put_str_ln (x : string) : IO unit := put_str ('\n' :: x) definition print_str {A : Type} [str : has_to_string A] (a : A) : IO unit := put_str_ln (to_string a) meta_constant format.print_using : format → options → IO unit meta_definition format.print (fmt : format) : IO unit := format.print_using fmt options.mk meta_definition pp_using {A : Type} [has_to_format A] (a : A) (o : options) : IO unit := format.print_using (to_fmt a) o meta_definition pp {A : Type} [has_to_format A] (a : A) : IO unit := format.print (to_fmt a)
33810d609cf8648fdf3a9f65f1601e263e350707
6214e13b31733dc9aeb4833db6a6466005763162
/src/etc.lean
5c9e2b507bb36ed4449b487bccd11d751cb31d93
[]
no_license
joshua0pang/esverify-theory
272a250445f3aeea49a7e72d1ab58c2da6618bbe
8565b123c87b0113f83553d7732cd6696c9b5807
refs/heads/master
1,585,873,849,081
1,527,304,393,000
1,527,304,393,000
154,901,199
1
0
null
1,540,593,067,000
1,540,593,067,000
null
UTF-8
Lean
false
false
10,844
lean
-- auxiliary lemmas unrelated to verifier import data.set -- auxiliary lemmas for nat lemma nonneg_of_nat {n: ℕ}: 0 ≤ n := nat.rec_on n (show 0 ≤ 0, by refl) (λn zero_lt_n, show 0 ≤ n + 1, from le_add_of_le_of_nonneg zero_lt_n zero_le_one) lemma lt_of_add_one {n: ℕ}: n < n + 1 := have n ≤ n, by refl, show n < n + 1, from lt_add_of_le_of_pos this zero_lt_one lemma lt_of_add {n m: ℕ}: n < n + m + 1 ∧ m < n + m + 1 := have n_nonneg: 0 ≤ n, from nonneg_of_nat, have m_nonneg: 0 ≤ m, from nonneg_of_nat, have n ≤ n, by refl, have n ≤ n + m, from le_add_of_le_of_nonneg this m_nonneg, have h₁: n < n + m + 1, from lt_add_of_le_of_pos this zero_lt_one, have m ≤ m, by refl, have m ≤ m + n, from le_add_of_le_of_nonneg this n_nonneg, have m ≤ n + m, by { rw[add_comm], assumption }, have h₂: m < n + m + 1, from lt_add_of_le_of_pos this zero_lt_one, ⟨h₁, h₂⟩ lemma lt_of_x_s_y {x s y: ℕ}: s < x + (s + (y + 1)) := begin rw[←add_assoc], rw[←add_comm s x], rw[add_assoc], apply lt_add_of_pos_right s, change 0 < x + (y + 1), rw[←add_assoc], apply lt_add_of_le_of_pos nonneg_of_nat, from zero_lt_one end lemma nat.succ.inj.inv {n m: ℕ}: n = m → (nat.succ n = nat.succ m) := assume : n = m, by simp [this] lemma eq_add_right_of_eq {n m k: ℕ}: n = m → n + k = m + k := begin assume h, induction k, simp, from h, change (n + (a + 1) = m + (a + 1)), rw[←add_assoc n a 1], rw[←add_assoc m a 1], change (nat.succ (n + a) = nat.succ (m + a)), apply nat.succ.inj.inv, from ih_1 end lemma eq_add_left_of_eq {n m k: ℕ}: n = m → k + n = k + m := begin assume h, rw[←add_comm n k], rw[←add_comm m k], from eq_add_right_of_eq h end -- auxiliary lemmas for option lemma some.inj.inv {α: Type} {a b: α}: a = b → (some a = some b) := assume : a = b, by simp [this] lemma option.some_iff_not_none {α: Type} {a: option α}: option.is_some a ↔ ¬ option.is_none a := begin cases a, split, intro h, contradiction, intro h2, unfold option.is_none at h2, have h3: ↑tt = false, from eq_false_intro h2, have h4: ↑tt = true, by simp, have h5: false = true, from eq.trans h3.symm h4, have h6: true, from trivial, have h7: false, from h5.symm ▸ h6, contradiction, split, intro h8, intro h9, contradiction, intro h10, unfold option.is_some, change tt = tt, refl end lemma option.none_iff_not_some {α: Type} {a: option α}: option.is_none a ↔ ¬ option.is_some a := begin cases a, split, intro h, contradiction, intro h2, unfold option.is_none, change tt = tt, refl, split, intro h3, intro h4, unfold option.is_none at h3, change ff = tt at h3, contradiction, intro h5, unfold option.is_some at h5, have h6: ↑tt = false, from eq_false_intro h5, have h7: ↑tt = true, by simp, have h8: false = true, from eq.trans h6.symm h7, have h9: true, from trivial, have : false, from h8.symm ▸ h9, contradiction end lemma option.is_none.inv {α: Type} {a: option α}: (a = none) ↔ option.is_none a := begin cases a, case option.some v { split, assume is_none_some, contradiction, assume is_none_some, contradiction, }, case option.none { split, assume is_none_none, exact rfl, assume is_none_none, exact rfl } end lemma option.is_none.ninv {α: Type} {a: option α}: (a ≠ none) ↔ ¬ option.is_none a := begin split, intro h, cases a, contradiction, unfold option.is_none, intro h2, change ff = tt at h2, contradiction, intro h3, intro h4, rw[h4] at h3, unfold option.is_none at h3, have h5: ↑tt = false, from eq_false_intro h3, have h6: ↑tt = true, by simp, have h7: false = true, from eq.trans h5.symm h6, have h8: true, from trivial, have r9: false, from h7.symm ▸ h8, contradiction end lemma option.is_some_iff_exists {α: Type} {a: option α}: option.is_some a ↔ (∃b, a = some b) := begin split, cases a, assume c, cases c, intro h, existsi a, refl, intro h2, cases h2, rw[a_2], unfold option.is_some, change tt = tt, refl end def option.is_none_prop {α: Type} (a: option α): Prop := option.is_none a instance {α: Type} {a: option α} : decidable (option.is_none_prop a) := let r := a in have h: r = a, from rfl, @option.rec_on α (λv, (r = v) → decidable (option.is_none_prop a)) r ( assume : r = none, have a = none, from eq.trans h this, have option.is_none a, from option.is_none.inv.mp this, have option.is_none_prop a, from this, is_true this ) ( assume v: α, assume : r = some v, have a = some v, from eq.trans h this, have ∃v, a = some v, from exists.intro v this, have option.is_some a, from option.is_some_iff_exists.mpr this, have ¬ option.is_none a, from option.some_iff_not_none.mp this, is_false this ) rfl lemma eq_from_map_result_some {α: Type} {a: option α} {b: α} {f: α → α}: f <$> a = some b → ∃c: α, a=some c ∧ b=f c := begin assume h1, unfold has_map.map at h1, cases a with c, unfold option.map at h1, unfold option.bind at h1, contradiction, unfold option.map at h1, unfold option.bind at h1, unfold function.comp at h1, have h2, from option.some.inj h1, existsi c, split, from rfl, from h2.symm end -- auxiliary lemmas for sets lemma set.two_elems_mem {α: Type} {a b c: α}: a ∈ ({b, c}: set α) → (a = b) ∨ (a = c) := assume a_in_bc: a ∈ {b, c}, have a_in_bc: a ∈ insert b (insert c (∅: set α)), by { simp at a_in_bc, simp[a_in_bc] }, have a = b ∨ a ∈ insert c ∅, from set.eq_or_mem_of_mem_insert a_in_bc, or.elim this ( assume : a = b, show (a = b) ∨ (a = c), from or.inl this ) ( assume : a ∈ insert c ∅, have a = c ∨ a ∈ ∅, from set.eq_or_mem_of_mem_insert this, or.elim this ( assume : a = c, show (a = b) ∨ (a = c), from or.inr this ) ( assume : a ∈ ∅, show (a = b) ∨ (a = c), from absurd this (set.not_mem_empty a) ) ) lemma set.two_elems_mem.inv {α: Type} {a b c: α}: (a = b) ∨ (a = c) → a ∈ ({b, c}: set α) := assume : (a = b) ∨ (a = c), or.elim this ( assume : a = b, show a ∈ ({b, c}: set α), { simp, left, from this } ) ( assume : a = c, show a ∈ ({b, c}: set α), { simp, right, from this } ) lemma set.three_elems_mem {α: Type} {a b c d: α}: a ∈ ({b, c, d}: set α) → (a = b) ∨ (a = c) ∨ (a = d) := assume a_in_bcd: a ∈ {b, c, d}, have a_in_bcd: a ∈ insert b (insert c (insert d (∅: set α))), by { simp at a_in_bcd, simp[a_in_bcd] }, have a = b ∨ a ∈ insert c (insert d (∅: set α)), from set.eq_or_mem_of_mem_insert a_in_bcd, or.elim this ( assume : a = b, show (a = b) ∨ (a = c) ∨ (a = d), from or.inl this ) ( assume : a ∈ insert c (insert d (∅: set α)), have a = c ∨ a ∈ insert d (∅: set α), from set.eq_or_mem_of_mem_insert this, or.elim this ( assume : a = c, show (a = b) ∨ (a = c) ∨ (a = d), from or.inr (or.inl this) ) ( assume : a ∈ insert d (∅: set α), have a = d ∨ a ∈ ∅, from set.eq_or_mem_of_mem_insert this, or.elim this ( assume : a = d, show (a = b) ∨ (a = c) ∨ (a = d), from or.inr (or.inr this) ) ( assume : a ∈ ∅, show (a = b) ∨ (a = c) ∨ (a = d), from absurd this (set.not_mem_empty a) ) ) ) lemma set.three_elems_mem₁ {α: Type} {a b c d: α}: (a = b) → a ∈ ({b, c, d}: set α) := by { intro ab, rw[ab], simp } lemma set.three_elems_mem₂ {α: Type} {a b c d: α}: (a = c) → a ∈ ({b, c, d}: set α) := by { intro ac, rw[ac], simp } lemma set.three_elems_mem₃ {α: Type} {a b c d: α}: (a = d) → a ∈ ({b, c, d}: set α) := by { intro ad, rw[ad], simp } lemma set.forall_not_mem_of_eq_empty {α: Type} {s: set α}: s = ∅ → ∀ x, x ∉ s := by simp[set.set_eq_def] lemma set.two_elems_of_insert {α: Type} {a b: α}: set.insert a ∅ ∪ set.insert b ∅ = {a, b} := set.eq_of_subset_of_subset ( assume x: α, assume : x ∈ set.insert a ∅ ∪ set.insert b ∅, or.elim (set.mem_or_mem_of_mem_union this) ( assume : x ∈ set.insert a ∅, have x ∈ {a}, from @eq.subst (set α) (λc, x ∈ c) (set.insert a ∅) {a} (set.singleton_def a).symm this, show x ∈ {a, b}, by { simp, left, simp at this, from this } ) ( assume : x ∈ set.insert b ∅, have x ∈ {b}, from @eq.subst (set α) (λc, x ∈ c) (set.insert b ∅) {b} (set.singleton_def b).symm this, show x ∈ {a, b}, by { simp, right, simp at this, from this } ) ) ( assume x: α, assume : x ∈ {a, b}, or.elim (set.two_elems_mem this) ( assume : x = a, have x ∈ set.insert a ∅, from (set.mem_singleton_iff x a).mpr this, show x ∈ set.insert a ∅ ∪ set.insert b ∅, from set.mem_union_left (set.insert b ∅) this ) ( assume : x = b, have x ∈ set.insert b ∅, from (set.mem_singleton_iff x b).mpr this, show x ∈ set.insert a ∅ ∪ set.insert b ∅, from set.mem_union_right (set.insert a ∅) this ) ) lemma set.subset_of_eq {α: Type} {a b: set α}: (a = b) → (a ⊆ b) := assume a_eq_b: a = b, assume x: α, assume : x ∈ a, show x ∈ b, from a_eq_b ▸ this lemma set.not_mem_or_mem_of_not_mem_diff {α: Type} {a: α} {b c: set α}: a ∉ b \ c → (a ∉ b ∨ a ∈ c) := begin assume h1, rw[set.diff_eq] at h1, rw[←set.compl_compl b] at h1, rw[←set.compl_union] at h1, rw[←set.mem_compl_eq] at h1, rw[set.compl_compl] at h1, have h2, from set.mem_or_mem_of_mem_union h1, rw[set.mem_compl_eq] at h2, from h2 end lemma set.diff_self {α: Type} {a: set α}: a - a = ∅ := begin apply set.eq_of_subset_of_subset, assume x: α, assume h1: x ∈ a - a, have h2, from set.mem_of_mem_diff h1, have h3, from set.not_mem_of_mem_diff h1, contradiction, assume x: α, assume h1: x ∈ ∅, have h2: x ∉ ∅, from set.forall_not_mem_of_eq_empty rfl x, contradiction end -- lemmas about if then else lemma ite.if_true {a: Prop} {b: Type} {c d: b} [decidable a]: a → (ite a c d = c) := begin assume ha, simp[ha] end lemma ite.if_false {a: Prop} {b: Type} {c d: b} [decidable a]: ¬a → (ite a c d = d) := begin assume ha, simp[ha] end
2fba624187e4ea62ea260d5b6d980b72f0a0c893
c777c32c8e484e195053731103c5e52af26a25d1
/src/geometry/manifold/cont_mdiff_mfderiv.lean
b002d3ee823d9a96a3b2ce2eb0bc6fc3d94aa93a
[ "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
24,787
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Floris van Doorn -/ import geometry.manifold.mfderiv /-! ### Interactions between differentiability, smoothness and manifold derivatives We give the relation between `mdifferentiable`, `cont_mdiff`, `mfderiv`, `tangent_map` and related notions. ## Main statements * `cont_mdiff_on.cont_mdiff_on_tangent_map_within` states that the bundled derivative of a `Cⁿ` function in a domain is `Cᵐ` when `m + 1 ≤ n`. * `cont_mdiff.cont_mdiff_tangent_map` states that the bundled derivative of a `Cⁿ` function is `Cᵐ` when `m + 1 ≤ n`. -/ open set function filter charted_space smooth_manifold_with_corners bundle open_locale topology manifold bundle /-! ### Definition of smooth functions between manifolds -/ variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] -- declare a smooth manifold `M` over the pair `(E, H)`. {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {M : Type*} [topological_space M] [charted_space H M] [Is : smooth_manifold_with_corners I M] -- declare a smooth manifold `M'` over the pair `(E', H')`. {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} {M' : Type*} [topological_space M'] [charted_space H' M'] [I's : smooth_manifold_with_corners I' M'] -- declare a smooth manifold `N` over the pair `(F, G)`. {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] {G : Type*} [topological_space G] {J : model_with_corners 𝕜 F G} {N : Type*} [topological_space N] [charted_space G N] [Js : smooth_manifold_with_corners J N] -- declare a smooth manifold `N'` over the pair `(F', G')`. {F' : Type*} [normed_add_comm_group F'] [normed_space 𝕜 F'] {G' : Type*} [topological_space G'] {J' : model_with_corners 𝕜 F' G'} {N' : Type*} [topological_space N'] [charted_space G' N'] [J's : smooth_manifold_with_corners J' N'] -- declare some additional normed spaces, used for fibers of vector bundles {F₁ : Type*} [normed_add_comm_group F₁] [normed_space 𝕜 F₁] {F₂ : Type*} [normed_add_comm_group F₂] [normed_space 𝕜 F₂] -- declare functions, sets, points and smoothness indices {f f₁ : M → M'} {s s₁ t : set M} {x : M} {m n : ℕ∞} /-! ### Deducing differentiability from smoothness -/ lemma cont_mdiff_within_at.mdifferentiable_within_at (hf : cont_mdiff_within_at I I' n f s x) (hn : 1 ≤ n) : mdifferentiable_within_at I I' f s x := begin suffices h : mdifferentiable_within_at I I' f (s ∩ (f ⁻¹' (ext_chart_at I' (f x)).source)) x, { rwa mdifferentiable_within_at_inter' at h, apply (hf.1).preimage_mem_nhds_within, exact ext_chart_at_source_mem_nhds I' (f x) }, rw mdifferentiable_within_at_iff, exact ⟨hf.1.mono (inter_subset_left _ _), (hf.2.differentiable_within_at hn).mono (by mfld_set_tac)⟩, end lemma cont_mdiff_at.mdifferentiable_at (hf : cont_mdiff_at I I' n f x) (hn : 1 ≤ n) : mdifferentiable_at I I' f x := mdifferentiable_within_at_univ.1 $ cont_mdiff_within_at.mdifferentiable_within_at hf hn lemma cont_mdiff_on.mdifferentiable_on (hf : cont_mdiff_on I I' n f s) (hn : 1 ≤ n) : mdifferentiable_on I I' f s := λ x hx, (hf x hx).mdifferentiable_within_at hn lemma cont_mdiff.mdifferentiable (hf : cont_mdiff I I' n f) (hn : 1 ≤ n) : mdifferentiable I I' f := λ x, (hf x).mdifferentiable_at hn lemma smooth_within_at.mdifferentiable_within_at (hf : smooth_within_at I I' f s x) : mdifferentiable_within_at I I' f s x := hf.mdifferentiable_within_at le_top lemma smooth_at.mdifferentiable_at (hf : smooth_at I I' f x) : mdifferentiable_at I I' f x := hf.mdifferentiable_at le_top lemma smooth_on.mdifferentiable_on (hf : smooth_on I I' f s) : mdifferentiable_on I I' f s := hf.mdifferentiable_on le_top lemma smooth.mdifferentiable (hf : smooth I I' f) : mdifferentiable I I' f := cont_mdiff.mdifferentiable hf le_top lemma smooth.mdifferentiable_at (hf : smooth I I' f) : mdifferentiable_at I I' f x := hf.mdifferentiable x lemma smooth.mdifferentiable_within_at (hf : smooth I I' f) : mdifferentiable_within_at I I' f s x := hf.mdifferentiable_at.mdifferentiable_within_at /-! ### The tangent map of a smooth function is smooth -/ section tangent_map /-- If a function is `C^n` with `1 ≤ n` on a domain with unique derivatives, then its bundled derivative is continuous. In this auxiliary lemma, we prove this fact when the source and target space are model spaces in models with corners. The general fact is proved in `cont_mdiff_on.continuous_on_tangent_map_within`-/ lemma cont_mdiff_on.continuous_on_tangent_map_within_aux {f : H → H'} {s : set H} (hf : cont_mdiff_on I I' n f s) (hn : 1 ≤ n) (hs : unique_mdiff_on I s) : continuous_on (tangent_map_within I I' f s) (π (tangent_space I) ⁻¹' s) := begin suffices h : continuous_on (λ (p : H × E), (f p.fst, (fderiv_within 𝕜 (written_in_ext_chart_at I I' p.fst f) (I.symm ⁻¹' s ∩ range I) ((ext_chart_at I p.fst) p.fst) : E →L[𝕜] E') p.snd)) (prod.fst ⁻¹' s), { have A := (tangent_bundle_model_space_homeomorph H I).continuous, rw continuous_iff_continuous_on_univ at A, have B := ((tangent_bundle_model_space_homeomorph H' I').symm.continuous.comp_continuous_on h) .comp' A, have : (univ ∩ ⇑(tangent_bundle_model_space_homeomorph H I) ⁻¹' (prod.fst ⁻¹' s)) = π (tangent_space I) ⁻¹' s, by { ext ⟨x, v⟩, simp only with mfld_simps }, rw this at B, apply B.congr, rintros ⟨x, v⟩ hx, dsimp [tangent_map_within], ext, { refl }, simp only with mfld_simps, apply congr_fun, apply congr_arg, rw mdifferentiable_within_at.mfderiv_within (hf.mdifferentiable_on hn x hx), refl }, suffices h : continuous_on (λ (p : H × E), (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I p.fst) : E →L[𝕜] E') p.snd) (prod.fst ⁻¹' s), { dsimp [written_in_ext_chart_at, ext_chart_at], apply continuous_on.prod (continuous_on.comp hf.continuous_on continuous_fst.continuous_on (subset.refl _)), apply h.congr, assume p hp, refl }, suffices h : continuous_on (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I)) (I '' s), { have C := continuous_on.comp h I.continuous_to_fun.continuous_on (subset.refl _), have A : continuous (λq : (E →L[𝕜] E') × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous_on (λp : H × E, (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I p.1), p.2)) (prod.fst ⁻¹' s), { apply continuous_on.prod _ continuous_snd.continuous_on, refine (continuous_on.comp C continuous_fst.continuous_on _ : _), exact preimage_mono (subset_preimage_image _ _) }, exact A.comp_continuous_on B }, rw cont_mdiff_on_iff at hf, let x : H := I.symm (0 : E), let y : H' := I'.symm (0 : E'), have A := hf.2 x y, simp only [I.image_eq, inter_comm] with mfld_simps at A ⊢, apply A.continuous_on_fderiv_within _ hn, convert hs.unique_diff_on_target_inter x using 1, simp only [inter_comm] with mfld_simps end /-- If a function is `C^n` on a domain with unique derivatives, then its bundled derivative is `C^m` when `m+1 ≤ n`. In this auxiliary lemma, we prove this fact when the source and target space are model spaces in models with corners. The general fact is proved in `cont_mdiff_on.cont_mdiff_on_tangent_map_within` -/ lemma cont_mdiff_on.cont_mdiff_on_tangent_map_within_aux {f : H → H'} {s : set H} (hf : cont_mdiff_on I I' n f s) (hmn : m + 1 ≤ n) (hs : unique_mdiff_on I s) : cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' f s) (π (tangent_space I) ⁻¹' s) := begin have m_le_n : m ≤ n, { apply le_trans _ hmn, have : m + 0 ≤ m + 1 := add_le_add_left (zero_le _) _, simpa only [add_zero] using this }, have one_le_n : 1 ≤ n, { apply le_trans _ hmn, change 0 + 1 ≤ m + 1, exact add_le_add_right (zero_le _) _ }, have U': unique_diff_on 𝕜 (range I ∩ I.symm ⁻¹' s), { assume y hy, simpa only [unique_mdiff_on, unique_mdiff_within_at, hy.1, inter_comm] with mfld_simps using hs (I.symm y) hy.2 }, rw cont_mdiff_on_iff, refine ⟨hf.continuous_on_tangent_map_within_aux one_le_n hs, λp q, _⟩, have A : range I ×ˢ univ ∩ ((equiv.sigma_equiv_prod H E).symm ∘ λ (p : E × E), ((I.symm) p.fst, p.snd)) ⁻¹' (π (tangent_space I) ⁻¹' s) = (range I ∩ I.symm ⁻¹' s) ×ˢ univ, by { ext ⟨x, v⟩, simp only with mfld_simps }, suffices h : cont_diff_on 𝕜 m (((λ (p : H' × E'), (I' p.fst, p.snd)) ∘ (equiv.sigma_equiv_prod H' E')) ∘ tangent_map_within I I' f s ∘ ((equiv.sigma_equiv_prod H E).symm) ∘ λ (p : E × E), (I.symm p.fst, p.snd)) ((range ⇑I ∩ ⇑(I.symm) ⁻¹' s) ×ˢ univ), by simpa [A] using h, change cont_diff_on 𝕜 m (λ (p : E × E), ((I' (f (I.symm p.fst)), ((mfderiv_within I I' f s (I.symm p.fst)) : E → E') p.snd) : E' × E')) ((range I ∩ I.symm ⁻¹' s) ×ˢ univ), -- check that all bits in this formula are `C^n` have hf' := cont_mdiff_on_iff.1 hf, have A : cont_diff_on 𝕜 m (I' ∘ f ∘ I.symm) (range I ∩ I.symm ⁻¹' s) := by simpa only with mfld_simps using (hf'.2 (I.symm 0) (I'.symm 0)).of_le m_le_n, have B : cont_diff_on 𝕜 m ((I' ∘ f ∘ I.symm) ∘ prod.fst) ((range I ∩ I.symm ⁻¹' s) ×ˢ univ) := A.comp (cont_diff_fst.cont_diff_on) (prod_subset_preimage_fst _ _), suffices C : cont_diff_on 𝕜 m (λ (p : E × E), ((fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) p.1 : _) p.2)) ((range I ∩ I.symm ⁻¹' s) ×ˢ univ), { apply cont_diff_on.prod B _, apply C.congr (λp hp, _), simp only with mfld_simps at hp, simp only [mfderiv_within, hf.mdifferentiable_on one_le_n _ hp.2, hp.1, if_pos] with mfld_simps }, have D : cont_diff_on 𝕜 m (λ x, (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) x)) (range I ∩ I.symm ⁻¹' s), { have : cont_diff_on 𝕜 n (I' ∘ f ∘ I.symm) (range I ∩ I.symm ⁻¹' s) := by simpa only with mfld_simps using (hf'.2 (I.symm 0) (I'.symm 0)), simpa only [inter_comm] using this.fderiv_within U' hmn }, have := D.comp (cont_diff_fst.cont_diff_on) (prod_subset_preimage_fst _ _), have := cont_diff_on.prod this (cont_diff_snd.cont_diff_on), exact is_bounded_bilinear_map_apply.cont_diff.comp_cont_diff_on this, end include Is I's /-- If a function is `C^n` on a domain with unique derivatives, then its bundled derivative is `C^m` when `m+1 ≤ n`. -/ theorem cont_mdiff_on.cont_mdiff_on_tangent_map_within (hf : cont_mdiff_on I I' n f s) (hmn : m + 1 ≤ n) (hs : unique_mdiff_on I s) : cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' f s) (π (tangent_space I) ⁻¹' s) := begin /- The strategy of the proof is to avoid unfolding the definitions, and reduce by functoriality to the case of functions on the model spaces, where we have already proved the result. Let `l` and `r` be the charts to the left and to the right, so that we have ``` l^{-1} f r H --------> M ---> M' ---> H' ``` Then the tangent map `T(r ∘ f ∘ l)` is smooth by a previous result. Consider the composition ``` Tl T(r ∘ f ∘ l^{-1}) Tr^{-1} TM -----> TH -------------------> TH' ---------> TM' ``` where `Tr^{-1}` and `Tl` are the tangent maps of `r^{-1}` and `l`. Writing `Tl` and `Tr^{-1}` as composition of charts (called `Dl` and `il` for `l` and `Dr` and `ir` in the proof below), it follows that they are smooth. The composition of all these maps is `Tf`, and is therefore smooth as a composition of smooth maps. -/ have m_le_n : m ≤ n, { apply le_trans _ hmn, have : m + 0 ≤ m + 1 := add_le_add_left (zero_le _) _, simpa only [add_zero] }, have one_le_n : 1 ≤ n, { apply le_trans _ hmn, change 0 + 1 ≤ m + 1, exact add_le_add_right (zero_le _) _ }, /- First step: local reduction on the space, to a set `s'` which is contained in chart domains. -/ refine cont_mdiff_on_of_locally_cont_mdiff_on (λp hp, _), have hf' := cont_mdiff_on_iff.1 hf, simp only with mfld_simps at hp, let l := chart_at H p.proj, set Dl := chart_at (model_prod H E) p with hDl, let r := chart_at H' (f p.proj), let Dr := chart_at (model_prod H' E') (tangent_map_within I I' f s p), let il := chart_at (model_prod H E) (tangent_map I I l p), let ir := chart_at (model_prod H' E') (tangent_map I I' (r ∘ f) p), let s' := f ⁻¹' r.source ∩ s ∩ l.source, let s'_lift := π (tangent_space I) ⁻¹' s', let s'l := l.target ∩ l.symm ⁻¹' s', let s'l_lift := π (tangent_space I) ⁻¹' s'l, rcases continuous_on_iff'.1 hf'.1 r.source r.open_source with ⟨o, o_open, ho⟩, suffices h : cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' f s) s'_lift, { refine ⟨π (tangent_space I) ⁻¹' (o ∩ l.source), _, _, _⟩, show is_open (π (tangent_space I) ⁻¹' (o ∩ l.source)), from (is_open.inter o_open l.open_source).preimage (continuous_proj E _) , show p ∈ π (tangent_space I) ⁻¹' (o ∩ l.source), { simp, have : p.proj ∈ f ⁻¹' r.source ∩ s, by simp [hp], rw ho at this, exact this.1 }, { have : π (tangent_space I) ⁻¹' s ∩ π (tangent_space I) ⁻¹' (o ∩ l.source) = s'_lift, { dsimp only [s'_lift, s'], rw [ho], mfld_set_tac }, rw this, exact h } }, /- Second step: check that all functions are smooth, and use the chain rule to write the bundled derivative as a composition of a function between model spaces and of charts. Convention: statements about the differentiability of `a ∘ b ∘ c` are named `diff_abc`. Statements about differentiability in the bundle have a `_lift` suffix. -/ have U' : unique_mdiff_on I s', { apply unique_mdiff_on.inter _ l.open_source, rw [ho, inter_comm], exact hs.inter o_open }, have U'l : unique_mdiff_on I s'l := U'.unique_mdiff_on_preimage (mdifferentiable_chart _ _), have diff_f : cont_mdiff_on I I' n f s' := hf.mono (by mfld_set_tac), have diff_r : cont_mdiff_on I' I' n r r.source := cont_mdiff_on_chart, have diff_rf : cont_mdiff_on I I' n (r ∘ f) s', { apply cont_mdiff_on.comp diff_r diff_f (λx hx, _), simp only [s'] with mfld_simps at hx, simp only [hx] with mfld_simps }, have diff_l : cont_mdiff_on I I n l.symm s'l, { have A : cont_mdiff_on I I n l.symm l.target := cont_mdiff_on_chart_symm, exact A.mono (by mfld_set_tac) }, have diff_rfl : cont_mdiff_on I I' n (r ∘ f ∘ l.symm) s'l, { apply cont_mdiff_on.comp diff_rf diff_l, mfld_set_tac }, have diff_rfl_lift : cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l) s'l_lift := diff_rfl.cont_mdiff_on_tangent_map_within_aux hmn U'l, have diff_irrfl_lift : cont_mdiff_on I.tangent I'.tangent m (ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l)) s'l_lift, { have A : cont_mdiff_on I'.tangent I'.tangent m ir ir.source := cont_mdiff_on_chart, exact cont_mdiff_on.comp A diff_rfl_lift (λp hp, by simp only [ir] with mfld_simps) }, have diff_Drirrfl_lift : cont_mdiff_on I.tangent I'.tangent m (Dr.symm ∘ (ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l))) s'l_lift, { have A : cont_mdiff_on I'.tangent I'.tangent m Dr.symm Dr.target := cont_mdiff_on_chart_symm, apply cont_mdiff_on.comp A diff_irrfl_lift (λp hp, _), simp only [s'l_lift] with mfld_simps at hp, simp only [ir, hp] with mfld_simps }, -- conclusion of this step: the composition of all the maps above is smooth have diff_DrirrflilDl : cont_mdiff_on I.tangent I'.tangent m (Dr.symm ∘ (ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l)) ∘ (il.symm ∘ Dl)) s'_lift, { have A : cont_mdiff_on I.tangent I.tangent m Dl Dl.source := cont_mdiff_on_chart, have A' : cont_mdiff_on I.tangent I.tangent m Dl s'_lift, { apply A.mono (λp hp, _), simp only [s'_lift] with mfld_simps at hp, simp only [Dl, hp] with mfld_simps }, have B : cont_mdiff_on I.tangent I.tangent m il.symm il.target := cont_mdiff_on_chart_symm, have C : cont_mdiff_on I.tangent I.tangent m (il.symm ∘ Dl) s'_lift := cont_mdiff_on.comp B A' (λp hp, by simp only [il] with mfld_simps), apply cont_mdiff_on.comp diff_Drirrfl_lift C (λp hp, _), simp only [s'_lift] with mfld_simps at hp, simp only [il, s'l_lift, hp, total_space.proj] with mfld_simps }, /- Third step: check that the composition of all the maps indeed coincides with the derivative we are looking for -/ have eq_comp : ∀q ∈ s'_lift, tangent_map_within I I' f s q = (Dr.symm ∘ ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l) ∘ (il.symm ∘ Dl)) q, { assume q hq, simp only [s'_lift] with mfld_simps at hq, have U'q : unique_mdiff_within_at I s' q.1, by { apply U', simp only [hq, s'] with mfld_simps }, have U'lq : unique_mdiff_within_at I s'l (Dl q).1, by { apply U'l, simp only [hq, s'l] with mfld_simps }, have A : tangent_map_within I I' ((r ∘ f) ∘ l.symm) s'l (il.symm (Dl q)) = tangent_map_within I I' (r ∘ f) s' (tangent_map_within I I l.symm s'l (il.symm (Dl q))), { refine tangent_map_within_comp_at (il.symm (Dl q)) _ _ (λp hp, _) U'lq, { apply diff_rf.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, { apply diff_l.mdifferentiable_on one_le_n, simp only [s'l, hq] with mfld_simps }, { simp only with mfld_simps at hp, simp only [hp] with mfld_simps } }, have B : tangent_map_within I I l.symm s'l (il.symm (Dl q)) = q, { have : tangent_map_within I I l.symm s'l (il.symm (Dl q)) = tangent_map I I l.symm (il.symm (Dl q)), { refine tangent_map_within_eq_tangent_map U'lq _, refine mdifferentiable_at_atlas_symm _ (chart_mem_atlas _ _) _, simp only [hq] with mfld_simps }, rw [this, tangent_map_chart_symm, hDl], { simp only [hq] with mfld_simps, have : q ∈ (chart_at (model_prod H E) p).source, { simp only [hq] with mfld_simps }, exact (chart_at (model_prod H E) p).left_inv this }, { simp only [hq] with mfld_simps } }, have C : tangent_map_within I I' (r ∘ f) s' q = tangent_map_within I' I' r r.source (tangent_map_within I I' f s' q), { refine tangent_map_within_comp_at q _ _ (λr hr, _) U'q, { apply diff_r.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, { apply diff_f.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, { simp only [s'] with mfld_simps at hr, simp only [hr] with mfld_simps } }, have D : Dr.symm (ir (tangent_map_within I' I' r r.source (tangent_map_within I I' f s' q))) = tangent_map_within I I' f s' q, { have A : tangent_map_within I' I' r r.source (tangent_map_within I I' f s' q) = tangent_map I' I' r (tangent_map_within I I' f s' q), { apply tangent_map_within_eq_tangent_map, { apply is_open.unique_mdiff_within_at _ r.open_source, simp [hq] }, { refine mdifferentiable_at_atlas _ (chart_mem_atlas _ _) _, simp only [hq] with mfld_simps } }, have : f p.proj = (tangent_map_within I I' f s p).1 := rfl, rw [A], dsimp [r, Dr], rw [this, tangent_map_chart], { simp only [hq] with mfld_simps, have : tangent_map_within I I' f s' q ∈ (chart_at (model_prod H' E') (tangent_map_within I I' f s p)).source, by { simp only [hq] with mfld_simps }, exact (chart_at (model_prod H' E') (tangent_map_within I I' f s p)).left_inv this }, { simp only [hq] with mfld_simps } }, have E : tangent_map_within I I' f s' q = tangent_map_within I I' f s q, { refine tangent_map_within_subset (by mfld_set_tac) U'q _, apply hf.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, simp only [(∘), A, B, C, D, E.symm] }, exact diff_DrirrflilDl.congr eq_comp, end /-- If a function is `C^n` on a domain with unique derivatives, with `1 ≤ n`, then its bundled derivative is continuous there. -/ theorem cont_mdiff_on.continuous_on_tangent_map_within (hf : cont_mdiff_on I I' n f s) (hmn : 1 ≤ n) (hs : unique_mdiff_on I s) : continuous_on (tangent_map_within I I' f s) (π (tangent_space I) ⁻¹' s) := begin have : cont_mdiff_on I.tangent I'.tangent 0 (tangent_map_within I I' f s) (π (tangent_space I) ⁻¹' s) := hf.cont_mdiff_on_tangent_map_within hmn hs, exact this.continuous_on end /-- If a function is `C^n`, then its bundled derivative is `C^m` when `m+1 ≤ n`. -/ theorem cont_mdiff.cont_mdiff_tangent_map (hf : cont_mdiff I I' n f) (hmn : m + 1 ≤ n) : cont_mdiff I.tangent I'.tangent m (tangent_map I I' f) := begin rw ← cont_mdiff_on_univ at hf ⊢, convert hf.cont_mdiff_on_tangent_map_within hmn unique_mdiff_on_univ, rw tangent_map_within_univ end /-- If a function is `C^n`, with `1 ≤ n`, then its bundled derivative is continuous. -/ theorem cont_mdiff.continuous_tangent_map (hf : cont_mdiff I I' n f) (hmn : 1 ≤ n) : continuous (tangent_map I I' f) := begin rw ← cont_mdiff_on_univ at hf, rw continuous_iff_continuous_on_univ, convert hf.continuous_on_tangent_map_within hmn unique_mdiff_on_univ, rw tangent_map_within_univ end end tangent_map namespace tangent_bundle include Is variables (I M) open bundle /-- The derivative of the zero section of the tangent bundle maps `⟨x, v⟩` to `⟨⟨x, 0⟩, ⟨v, 0⟩⟩`. Note that, as currently framed, this is a statement in coordinates, thus reliant on the choice of the coordinate system we use on the tangent bundle. However, the result itself is coordinate-dependent only to the extent that the coordinates determine a splitting of the tangent bundle. Moreover, there is a canonical splitting at each point of the zero section (since there is a canonical horizontal space there, the tangent space to the zero section, in addition to the canonical vertical space which is the kernel of the derivative of the projection), and this canonical splitting is also the one that comes from the coordinates on the tangent bundle in our definitions. So this statement is not as crazy as it may seem. TODO define splittings of vector bundles; state this result invariantly. -/ lemma tangent_map_tangent_bundle_pure (p : tangent_bundle I M) : tangent_map I I.tangent (zero_section (tangent_space I)) p = ⟨⟨p.proj, 0⟩, ⟨p.2, 0⟩⟩ := begin rcases p with ⟨x, v⟩, have N : I.symm ⁻¹' (chart_at H x).target ∈ 𝓝 (I ((chart_at H x) x)), { apply is_open.mem_nhds, apply (local_homeomorph.open_target _).preimage I.continuous_inv_fun, simp only with mfld_simps }, have A : mdifferentiable_at I I.tangent (λ x, @total_space_mk M (tangent_space I) x 0) x, { have : smooth I (I.prod 𝓘(𝕜, E)) (zero_section (tangent_space I : M → Type*)) := bundle.smooth_zero_section 𝕜 (tangent_space I : M → Type*), exact this.mdifferentiable_at }, have B : fderiv_within 𝕜 (λ (x' : E), (x', (0 : E))) (set.range ⇑I) (I ((chart_at H x) x)) v = (v, 0), { rw [fderiv_within_eq_fderiv, differentiable_at.fderiv_prod], { simp }, { exact differentiable_at_id' }, { exact differentiable_at_const _ }, { exact model_with_corners.unique_diff_at_image I }, { exact differentiable_at_id'.prod (differentiable_at_const _) } }, simp only [bundle.zero_section, tangent_map, mfderiv, total_space.proj_mk, A, if_pos, chart_at, fiber_bundle.charted_space_chart_at, tangent_bundle.trivialization_at_apply, tangent_bundle_core, function.comp, continuous_linear_map.map_zero] with mfld_simps, rw ← fderiv_within_inter N (I.unique_diff (I ((chart_at H x) x)) (set.mem_range_self _)) at B, rw [← fderiv_within_inter N (I.unique_diff (I ((chart_at H x) x)) (set.mem_range_self _)), ← B], congr' 2, apply fderiv_within_congr _ (λ y hy, _), { simp only [prod.mk.inj_iff] with mfld_simps }, { apply unique_diff_within_at.inter (I.unique_diff _ _) N, simp only with mfld_simps }, { simp only with mfld_simps at hy, simp only [hy, prod.mk.inj_iff] with mfld_simps }, end end tangent_bundle
050e934547b729c047b3be0f295c811c8a22138a
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/subterminal.lean
cdc36f10017316182849248bfe8dd74e55ffa193
[ "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,032
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.terminal import category_theory.limits.shapes.binary_products import category_theory.subobject.basic /-! # Subterminal objects Subterminal objects are the objects which can be thought of as subobjects of the terminal object. In fact, the definition can be constructed to not require a terminal object, by defining `A` to be subterminal iff for any `Z`, there is at most one morphism `Z ⟶ A`. An alternate definition is that the diagonal morphism `A ⟶ A ⨯ A` is an isomorphism. In this file we define subterminal objects and show the equivalence of these three definitions. We also construct the subcategory of subterminal objects. ## TODO * Define exponential ideals, and show this subcategory is an exponential ideal. * Use the above to show that in a locally cartesian closed category, every subobject lattice is cartesian closed (equivalently, a Heyting algebra). -/ universes v₁ v₂ u₁ u₂ noncomputable theory namespace category_theory open limits category variables {C : Type u₁} [category.{v₁} C] {A : C} /-- An object `A` is subterminal iff for any `Z`, there is at most one morphism `Z ⟶ A`. -/ def is_subterminal (A : C) : Prop := ∀ ⦃Z : C⦄ (f g : Z ⟶ A), f = g lemma is_subterminal.def : is_subterminal A ↔ ∀ ⦃Z : C⦄ (f g : Z ⟶ A), f = g := iff.rfl /-- If `A` is subterminal, the unique morphism from it to a terminal object is a monomorphism. The converse of `is_subterminal_of_mono_is_terminal_from`. -/ lemma is_subterminal.mono_is_terminal_from (hA : is_subterminal A) {T : C} (hT : is_terminal T) : mono (hT.from A) := { right_cancellation := λ Z g h _, hA _ _ } /-- If `A` is subterminal, the unique morphism from it to the terminal object is a monomorphism. The converse of `is_subterminal_of_mono_terminal_from`. -/ lemma is_subterminal.mono_terminal_from [has_terminal C] (hA : is_subterminal A) : mono (terminal.from A) := hA.mono_is_terminal_from terminal_is_terminal /-- If the unique morphism from `A` to a terminal object is a monomorphism, `A` is subterminal. The converse of `is_subterminal.mono_is_terminal_from`. -/ lemma is_subterminal_of_mono_is_terminal_from {T : C} (hT : is_terminal T) [mono (hT.from A)] : is_subterminal A := λ Z f g, by { rw ← cancel_mono (hT.from A), apply hT.hom_ext } /-- If the unique morphism from `A` to the terminal object is a monomorphism, `A` is subterminal. The converse of `is_subterminal.mono_terminal_from`. -/ lemma is_subterminal_of_mono_terminal_from [has_terminal C] [mono (terminal.from A)] : is_subterminal A := λ Z f g, by { rw ← cancel_mono (terminal.from A), apply subsingleton.elim } lemma is_subterminal_of_is_terminal {T : C} (hT : is_terminal T) : is_subterminal T := λ Z f g, hT.hom_ext _ _ lemma is_subterminal_of_terminal [has_terminal C] : is_subterminal (⊤_ C) := λ Z f g, subsingleton.elim _ _ /-- If `A` is subterminal, its diagonal morphism is an isomorphism. The converse of `is_subterminal_of_is_iso_diag`. -/ lemma is_subterminal.is_iso_diag (hA : is_subterminal A) [has_binary_product A A] : is_iso (diag A) := ⟨⟨limits.prod.fst, ⟨by simp, by { rw is_subterminal.def at hA, tidy }⟩⟩⟩ /-- If the diagonal morphism of `A` is an isomorphism, then it is subterminal. The converse of `is_subterminal.is_iso_diag`. -/ lemma is_subterminal_of_is_iso_diag [has_binary_product A A] [is_iso (diag A)] : is_subterminal A := λ Z f g, begin have : (limits.prod.fst : A ⨯ A ⟶ _) = limits.prod.snd, { simp [←cancel_epi (diag A)] }, rw [←prod.lift_fst f g, this, prod.lift_snd], end /-- If `A` is subterminal, it is isomorphic to `A ⨯ A`. -/ @[simps] def is_subterminal.iso_diag (hA : is_subterminal A) [has_binary_product A A] : A ⨯ A ≅ A := begin letI := is_subterminal.is_iso_diag hA, apply (as_iso (diag A)).symm, end variables (C) /-- The (full sub)category of subterminal objects. TODO: If `C` is the category of sheaves on a topological space `X`, this category is equivalent to the lattice of open subsets of `X`. More generally, if `C` is a topos, this is the lattice of "external truth values". -/ @[derive category] def subterminals (C : Type u₁) [category.{v₁} C] := {A : C // is_subterminal A} instance [has_terminal C] : inhabited (subterminals C) := ⟨⟨⊤_ C, is_subterminal_of_terminal⟩⟩ /-- The inclusion of the subterminal objects into the original category. -/ @[derive [full, faithful], simps] def subterminal_inclusion : subterminals C ⥤ C := full_subcategory_inclusion _ instance subterminals_thin (X Y : subterminals C) : subsingleton (X ⟶ Y) := ⟨λ f g, Y.2 f g⟩ /-- The category of subterminal objects is equivalent to the category of monomorphisms to the terminal object (which is in turn equivalent to the subobjects of the terminal object). -/ @[simps] def subterminals_equiv_mono_over_terminal [has_terminal C] : subterminals C ≌ mono_over (⊤_ C) := { functor := { obj := λ X, ⟨over.mk (terminal.from X.1), X.2.mono_terminal_from⟩, map := λ X Y f, mono_over.hom_mk f (by ext1 ⟨⟩) }, inverse := { obj := λ X, ⟨X.val.left, λ Z f g, by { rw ← cancel_mono X.arrow, apply subsingleton.elim }⟩, map := λ X Y f, f.1 }, unit_iso := { hom := { app := λ X, 𝟙 _ }, inv := { app := λ X, 𝟙 _ } }, counit_iso := { hom := { app := λ X, over.hom_mk (𝟙 _) }, inv := { app := λ X, over.hom_mk (𝟙 _) } } } @[simp] lemma subterminals_to_mono_over_terminal_comp_forget [has_terminal C] : (subterminals_equiv_mono_over_terminal C).functor ⋙ mono_over.forget _ ⋙ over.forget _ = subterminal_inclusion C := rfl @[simp] lemma mono_over_terminal_to_subterminals_comp [has_terminal C] : (subterminals_equiv_mono_over_terminal C).inverse ⋙ subterminal_inclusion C = mono_over.forget _ ⋙ over.forget _ := rfl end category_theory
7c68b90e416ae733edbb11ad888833c44b103450
e16d4df4c2baa34ab203178cea2c27905a3b63d3
/src/utils.lean
8db03bc8ac2749c037d2d19dbb06ec734a8a0c50
[]
no_license
jembishop/advent-of-code-2020
ea29eecb7f1d676dc1fd34b1a66efdbd23248aec
32fd5bc28e7c178277e85dc034b55fce6dd4afce
refs/heads/main
1,675,354,147,711
1,608,705,139,000
1,608,705,139,000
317,705,877
0
0
null
null
null
null
UTF-8
Lean
false
false
901
lean
import data.vector import data.list -- can't prove decidablity to use normal filter def filter {α : Type } (p : α → bool) : list α → list α | [] := [] | (a::l) := if p a then a :: filter l else filter l def from_list {α} {n : ℕ} : list α → option (vector α n) | l := if h: l.length = n then some ⟨l, h⟩ else none def join_str : list string → string | [] := "" | (x::xs) := x ++ " " ++ (join_str xs) def split_l {α: Type} [decidable_eq α]: α → list α → list (list α ) | delim [] := [] | delim (x::xs) := let rest := split_l delim xs in if x=delim then []::rest else match rest with | [] := [[x]] | (y::ys) := (x::y)::ys end def split_lines : string → list string | s := s.split (='\n') def split_para : string → list string | s := join_str <$> (split_l "" (split_lines s)) def suml : list ℕ → ℕ := list.foldr (+) 0
49d2344deb60e040cb9a7dfbe1857e2828d411de
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/deprecated/submonoid.lean
e35af6947926828790ec5e2f2045232f7961543e
[ "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
20,290
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard -/ import group_theory.submonoid.basic import algebra.big_operators.basic /-! # Submonoids This file defines unbundled multiplicative and additive submonoids (deprecated). For bundled form see `group_theory/submonoid`. We some results about images and preimages of submonoids under monoid homomorphisms. These theorems use unbundled monoid homomorphisms (also deprecated). There are also theorems about the submonoids generated by an element or a subset of a monoid, defined inductively. ## Implementation notes Unbundled submonoids will slowly be removed from mathlib. ## Tags submonoid, submonoids, is_submonoid -/ open_locale big_operators variables {M : Type*} [monoid M] {s : set M} variables {A : Type*} [add_monoid A] {t : set A} /-- `s` is an additive submonoid: a set containing 0 and closed under addition. -/ class is_add_submonoid (s : set A) : Prop := (zero_mem : (0:A) ∈ s) (add_mem {a b} : a ∈ s → b ∈ s → a + b ∈ s) /-- `s` is a submonoid: a set containing 1 and closed under multiplication. -/ @[to_additive] class is_submonoid (s : set M) : Prop := (one_mem : (1:M) ∈ s) (mul_mem {a b} : a ∈ s → b ∈ s → a * b ∈ s) lemma additive.is_add_submonoid (s : set M) : ∀ [is_submonoid s], @is_add_submonoid (additive M) _ s | ⟨h₁, h₂⟩ := ⟨h₁, @h₂⟩ theorem additive.is_add_submonoid_iff {s : set M} : @is_add_submonoid (additive M) _ s ↔ is_submonoid s := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, @h₂⟩, λ h, by exactI additive.is_add_submonoid _⟩ lemma multiplicative.is_submonoid (s : set A) : ∀ [is_add_submonoid s], @is_submonoid (multiplicative A) _ s | ⟨h₁, h₂⟩ := ⟨h₁, @h₂⟩ theorem multiplicative.is_submonoid_iff {s : set A} : @is_submonoid (multiplicative A) _ s ↔ is_add_submonoid s := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, @h₂⟩, λ h, by exactI multiplicative.is_submonoid _⟩ /-- The intersection of two submonoids of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The intersection of two `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of M."] instance is_submonoid.inter (s₁ s₂ : set M) [is_submonoid s₁] [is_submonoid s₂] : is_submonoid (s₁ ∩ s₂) := { one_mem := ⟨is_submonoid.one_mem, is_submonoid.one_mem⟩, mul_mem := λ x y hx hy, ⟨is_submonoid.mul_mem hx.1 hy.1, is_submonoid.mul_mem hx.2 hy.2⟩ } /-- The intersection of an indexed set of submonoids of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The intersection of an indexed set of `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of `M`."] instance is_submonoid.Inter {ι : Sort*} (s : ι → set M) [h : ∀ y : ι, is_submonoid (s y)] : is_submonoid (set.Inter s) := { one_mem := set.mem_Inter.2 $ λ y, is_submonoid.one_mem, mul_mem := λ x₁ x₂ h₁ h₂, set.mem_Inter.2 $ λ y, is_submonoid.mul_mem (set.mem_Inter.1 h₁ y) (set.mem_Inter.1 h₂ y) } /-- The union of an indexed, directed, nonempty set of submonoids of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The union of an indexed, directed, nonempty set of `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of `M`. "] lemma is_submonoid_Union_of_directed {ι : Type*} [hι : nonempty ι] (s : ι → set M) [∀ i, is_submonoid (s i)] (directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) : is_submonoid (⋃i, s i) := { one_mem := let ⟨i⟩ := hι in set.mem_Union.2 ⟨i, is_submonoid.one_mem⟩, mul_mem := λ a b ha hb, let ⟨i, hi⟩ := set.mem_Union.1 ha in let ⟨j, hj⟩ := set.mem_Union.1 hb in let ⟨k, hk⟩ := directed i j in set.mem_Union.2 ⟨k, is_submonoid.mul_mem (hk.1 hi) (hk.2 hj)⟩ } section powers /-- The set of natural number powers `1, x, x², ...` of an element `x` of a monoid. -/ def powers (x : M) : set M := {y | ∃ n:ℕ, x^n = y} /-- The set of natural number multiples `0, x, 2x, ...` of an element `x` of an `add_monoid`. -/ def multiples (x : A) : set A := {y | ∃ n:ℕ, n • x = y} attribute [to_additive multiples] powers /-- 1 is in the set of natural number powers of an element of a monoid. -/ lemma powers.one_mem {x : M} : (1 : M) ∈ powers x := ⟨0, pow_zero _⟩ /-- 0 is in the set of natural number multiples of an element of an `add_monoid`. -/ lemma multiples.zero_mem {x : A} : (0 : A) ∈ multiples x := ⟨0, zero_nsmul _⟩ attribute [to_additive] powers.one_mem /-- An element of a monoid is in the set of that element's natural number powers. -/ lemma powers.self_mem {x : M} : x ∈ powers x := ⟨1, pow_one _⟩ /-- An element of an `add_monoid` is in the set of that element's natural number multiples. -/ lemma multiples.self_mem {x : A} : x ∈ multiples x := ⟨1, one_nsmul _⟩ attribute [to_additive] powers.self_mem /-- The set of natural number powers of an element of a monoid is closed under multiplication. -/ lemma powers.mul_mem {x y z : M} : (y ∈ powers x) → (z ∈ powers x) → (y * z ∈ powers x) := λ ⟨n₁, h₁⟩ ⟨n₂, h₂⟩, ⟨n₁ + n₂, by simp only [pow_add, *]⟩ /-- The set of natural number multiples of an element of an `add_monoid` is closed under addition. -/ lemma multiples.add_mem {x y z : A} : (y ∈ multiples x) → (z ∈ multiples x) → (y + z ∈ multiples x) := @powers.mul_mem (multiplicative A) _ _ _ _ attribute [to_additive] powers.mul_mem /-- The set of natural number powers of an element of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The set of natural number multiples of an element of an `add_monoid` `M` is an `add_submonoid` of `M`."] instance powers.is_submonoid (x : M) : is_submonoid (powers x) := { one_mem := powers.one_mem, mul_mem := λ y z, powers.mul_mem } /-- A monoid is a submonoid of itself. -/ @[to_additive "An `add_monoid` is an `add_submonoid` of itself."] instance univ.is_submonoid : is_submonoid (@set.univ M) := by split; simp /-- The preimage of a submonoid under a monoid hom is a submonoid of the domain. -/ @[to_additive "The preimage of an `add_submonoid` under an `add_monoid` hom is an `add_submonoid` of the domain."] instance preimage.is_submonoid {N : Type*} [monoid N] (f : M → N) [is_monoid_hom f] (s : set N) [is_submonoid s] : is_submonoid (f ⁻¹' s) := { one_mem := show f 1 ∈ s, by rw is_monoid_hom.map_one f; exact is_submonoid.one_mem, mul_mem := λ a b (ha : f a ∈ s) (hb : f b ∈ s), show f (a * b) ∈ s, by rw is_monoid_hom.map_mul f; exact is_submonoid.mul_mem ha hb } /-- The image of a submonoid under a monoid hom is a submonoid of the codomain. -/ @[instance, to_additive "The image of an `add_submonoid` under an `add_monoid` hom is an `add_submonoid` of the codomain."] lemma image.is_submonoid {γ : Type*} [monoid γ] (f : M → γ) [is_monoid_hom f] (s : set M) [is_submonoid s] : is_submonoid (f '' s) := { one_mem := ⟨1, is_submonoid.one_mem, is_monoid_hom.map_one f⟩, mul_mem := λ a b ⟨x, hx⟩ ⟨y, hy⟩, ⟨x * y, is_submonoid.mul_mem hx.1 hy.1, by rw [is_monoid_hom.map_mul f, hx.2, hy.2]⟩ } /-- The image of a monoid hom is a submonoid of the codomain. -/ @[to_additive "The image of an `add_monoid` hom is an `add_submonoid` of the codomain."] instance range.is_submonoid {γ : Type*} [monoid γ] (f : M → γ) [is_monoid_hom f] : is_submonoid (set.range f) := by rw ← set.image_univ; apply_instance /-- Submonoids are closed under natural powers. -/ lemma is_submonoid.pow_mem {a : M} [is_submonoid s] (h : a ∈ s) : ∀ {n : ℕ}, a ^ n ∈ s | 0 := by { rw pow_zero, exact is_submonoid.one_mem } | (n + 1) := by { rw pow_succ, exact is_submonoid.mul_mem h is_submonoid.pow_mem } /-- An `add_submonoid` is closed under multiplication by naturals. -/ lemma is_add_submonoid.smul_mem {a : A} [is_add_submonoid t] : ∀ (h : a ∈ t) {n : ℕ}, n • a ∈ t := @is_submonoid.pow_mem (multiplicative A) _ _ _ (multiplicative.is_submonoid _) attribute [to_additive smul_mem] is_submonoid.pow_mem /-- The set of natural number powers of an element of a submonoid is a subset of the submonoid. -/ lemma is_submonoid.power_subset {a : M} [is_submonoid s] (h : a ∈ s) : powers a ⊆ s := assume x ⟨n, hx⟩, hx ▸ is_submonoid.pow_mem h /-- The set of natural number multiples of an element of an `add_submonoid` is a subset of the `add_submonoid`. -/ lemma is_add_submonoid.multiple_subset {a : A} [is_add_submonoid t] : a ∈ t → multiples a ⊆ t := @is_submonoid.power_subset (multiplicative A) _ _ _ (multiplicative.is_submonoid _) attribute [to_additive multiple_subset] is_submonoid.power_subset end powers namespace is_submonoid /-- The product of a list of elements of a submonoid is an element of the submonoid. -/ @[to_additive "The sum of a list of elements of an `add_submonoid` is an element of the `add_submonoid`."] lemma list_prod_mem [is_submonoid s] : ∀{l : list M}, (∀x∈l, x ∈ s) → l.prod ∈ s | [] h := one_mem | (a::l) h := suffices a * l.prod ∈ s, by simpa, have a ∈ s ∧ (∀x∈l, x ∈ s), by simpa using h, is_submonoid.mul_mem this.1 (list_prod_mem this.2) /-- The product of a multiset of elements of a submonoid of a `comm_monoid` is an element of the submonoid. -/ @[to_additive "The sum of a multiset of elements of an `add_submonoid` of an `add_comm_monoid` is an element of the `add_submonoid`. "] lemma multiset_prod_mem {M} [comm_monoid M] (s : set M) [is_submonoid s] (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 list_prod_mem hl end /-- The product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is an element of the submonoid. -/ @[to_additive "The sum of elements of an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is an element of the `add_submonoid`."] lemma finset_prod_mem {M A} [comm_monoid M] (s : set M) [is_submonoid s] (f : A → M) : ∀(t : finset A), (∀b∈t, f b ∈ s) → ∏ b in t, f b ∈ s | ⟨m, hm⟩ hs := multiset_prod_mem s _ (by simpa) end is_submonoid -- TODO: modify `subtype_instance` to produce this definition, then use it here -- and for `subtype.group` /-- Submonoids are themselves monoids. -/ @[to_additive "An `add_submonoid` is itself an `add_monoid`."] def subtype.monoid {s : set M} [is_submonoid s] : monoid s := { one := ⟨1, is_submonoid.one_mem⟩, mul := λ x y, ⟨x * y, is_submonoid.mul_mem x.2 y.2⟩, mul_one := λ x, subtype.eq $ mul_one x.1, one_mul := λ x, subtype.eq $ one_mul x.1, mul_assoc := λ x y z, subtype.eq $ mul_assoc x.1 y.1 z.1 } /-- Submonoids of commutative monoids are themselves commutative monoids. -/ @[to_additive "An `add_submonoid` of a commutative `add_monoid` is itself a commutative `add_monoid`. "] def subtype.comm_monoid {M} [comm_monoid M] {s : set M} [is_submonoid s] : comm_monoid s := { mul_comm := λ x y, subtype.eq $ mul_comm x.1 y.1, .. subtype.monoid } section local attribute [instance] subtype.monoid subtype.add_monoid /-- Submonoids inherit the 1 of the monoid. -/ @[simp, norm_cast, to_additive "An `add_submonoid` inherits the 0 of the `add_monoid`. "] lemma is_submonoid.coe_one [is_submonoid s] : ((1 : s) : M) = 1 := rfl attribute [norm_cast] is_add_submonoid.coe_zero /-- Submonoids inherit the multiplication of the monoid. -/ @[simp, norm_cast, to_additive "An `add_submonoid` inherits the addition of the `add_monoid`. "] lemma is_submonoid.coe_mul [is_submonoid s] (a b : s) : ((a * b : s) : M) = a * b := rfl attribute [norm_cast] is_add_submonoid.coe_add /-- Submonoids inherit the exponentiation by naturals of the monoid. -/ @[simp, norm_cast] lemma is_submonoid.coe_pow [is_submonoid s] (a : s) (n : ℕ) : ((a ^ n : s) : M) = a ^ n := by induction n; simp [*, pow_succ] /-- An `add_submonoid` inherits the multiplication by naturals of the `add_monoid`. -/ @[simp, norm_cast] lemma is_add_submonoid.smul_coe {A : Type*} [add_monoid A] {s : set A} [is_add_submonoid s] (a : s) (n : ℕ) : ((n • a : s) : A) = n • a := by induction n; simp [*, succ_nsmul, zero_nsmul] attribute [to_additive smul_coe] is_submonoid.coe_pow /-- The natural injection from a submonoid into the monoid is a monoid hom. -/ @[to_additive "The natural injection from an `add_submonoid` into the `add_monoid` is an `add_monoid` hom. "] instance subtype_val.is_monoid_hom [is_submonoid s] : is_monoid_hom (subtype.val : s → M) := { map_one := rfl, map_mul := λ _ _, rfl } /-- The natural injection from a submonoid into the monoid is a monoid hom. -/ @[to_additive "The natural injection from an `add_submonoid` into the `add_monoid` is an `add_monoid` hom. "] instance coe.is_monoid_hom [is_submonoid s] : is_monoid_hom (coe : s → M) := subtype_val.is_monoid_hom /-- Given a monoid hom `f : γ → M` whose image is contained in a submonoid `s`, the induced map from `γ` to `s` is a monoid hom. -/ @[to_additive "Given an `add_monoid` hom `f : γ → M` whose image is contained in an `add_submonoid` s, the induced map from `γ` to `s` is an `add_monoid` hom."] instance subtype_mk.is_monoid_hom {γ : Type*} [monoid γ] [is_submonoid s] (f : γ → M) [is_monoid_hom f] (h : ∀ x, f x ∈ s) : is_monoid_hom (λ x, (⟨f x, h x⟩ : s)) := { map_one := subtype.eq (is_monoid_hom.map_one f), map_mul := λ x y, subtype.eq (is_monoid_hom.map_mul f x y) } /-- Given two submonoids `s` and `t` such that `s ⊆ t`, the natural injection from `s` into `t` is a monoid hom. -/ @[to_additive "Given two `add_submonoid`s `s` and `t` such that `s ⊆ t`, the natural injection from `s` into `t` is an `add_monoid` hom."] instance set_inclusion.is_monoid_hom (t : set M) [is_submonoid s] [is_submonoid t] (h : s ⊆ t) : is_monoid_hom (set.inclusion h) := subtype_mk.is_monoid_hom _ _ end namespace add_monoid /-- The inductively defined membership predicate for the submonoid generated by a subset of a monoid. -/ inductive in_closure (s : set A) : A → Prop | basic {a : A} : a ∈ s → in_closure a | zero : in_closure 0 | add {a b : A} : in_closure a → in_closure b → in_closure (a + b) end add_monoid namespace monoid /-- The inductively defined membership predicate for the `add_submonoid` generated by a subset of an add_monoid. -/ inductive in_closure (s : set M) : M → Prop | basic {a : M} : a ∈ s → in_closure a | one : in_closure 1 | mul {a b : M} : in_closure a → in_closure b → in_closure (a * b) attribute [to_additive] monoid.in_closure attribute [to_additive] monoid.in_closure.one attribute [to_additive] monoid.in_closure.mul /-- The inductively defined submonoid generated by a subset of a monoid. -/ @[to_additive "The inductively defined `add_submonoid` genrated by a subset of an `add_monoid`."] def closure (s : set M) : set M := {a | in_closure s a } @[to_additive] instance closure.is_submonoid (s : set M) : is_submonoid (closure s) := { one_mem := in_closure.one, mul_mem := assume a b, in_closure.mul } /-- A subset of a monoid is contained in the submonoid it generates. -/ @[to_additive "A subset of an `add_monoid` is contained in the `add_submonoid` it generates."] theorem subset_closure {s : set M} : s ⊆ closure s := assume a, in_closure.basic /-- The submonoid generated by a set is contained in any submonoid that contains the set. -/ @[to_additive "The `add_submonoid` generated by a set is contained in any `add_submonoid` that contains the set."] theorem closure_subset {s t : set M} [is_submonoid t] (h : s ⊆ t) : closure s ⊆ t := assume a ha, by induction ha; simp [h _, *, is_submonoid.one_mem, is_submonoid.mul_mem] /-- Given subsets `t` and `s` of a monoid `M`, if `s ⊆ t`, the submonoid of `M` generated by `s` is contained in the submonoid generated by `t`. -/ @[to_additive "Given subsets `t` and `s` of an `add_monoid M`, if `s ⊆ t`, the `add_submonoid` of `M` generated by `s` is contained in the `add_submonoid` generated by `t`."] theorem closure_mono {s t : set M} (h : s ⊆ t) : closure s ⊆ closure t := closure_subset $ set.subset.trans h subset_closure /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ @[to_additive "The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number multiples of the element."] theorem closure_singleton {x : M} : closure ({x} : set M) = powers x := set.eq_of_subset_of_subset (closure_subset $ set.singleton_subset_iff.2 $ powers.self_mem) $ is_submonoid.power_subset $ set.singleton_subset_iff.1 $ subset_closure /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set under the monoid hom. -/ @[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 under the `add_monoid` hom."] lemma image_closure {A : Type*} [monoid A] (f : M → A) [is_monoid_hom f] (s : set M) : f '' closure s = closure (f '' s) := le_antisymm begin rintros _ ⟨x, hx, rfl⟩, apply in_closure.rec_on hx; intros, { solve_by_elim [subset_closure, set.mem_image_of_mem] }, { rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem }, { rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] } end (closure_subset $ set.image_subset _ subset_closure) /-- Given an element `a` of the submonoid of a monoid `M` generated by a set `s`, there exists a list of elements of `s` whose product is `a`. -/ @[to_additive "Given an element `a` of the `add_submonoid` of an `add_monoid M` generated by a set `s`, there exists a list of elements of `s` whose sum is `a`."] theorem exists_list_of_mem_closure {s : set M} {a : M} (h : a ∈ closure s) : (∃l:list M, (∀x∈l, x ∈ s) ∧ l.prod = a) := begin induction h, case in_closure.basic : a ha { existsi ([a]), simp [ha] }, case in_closure.one { existsi ([]), simp }, case in_closure.mul : a b _ _ ha hb { rcases ha with ⟨la, ha, eqa⟩, rcases hb with ⟨lb, hb, eqb⟩, existsi (la ++ lb), simp [eqa.symm, eqb.symm, or_imp_distrib], exact assume a, ⟨ha a, hb a⟩ } end /-- Given sets `s, t` of a commutative monoid `M`, `x ∈ M` is in the submonoid of `M` generated by `s ∪ t` iff there exists an element of the submonoid generated by `s` and an element of the submonoid generated by `t` whose product is `x`. -/ @[to_additive "Given sets `s, t` of a commutative `add_monoid M`, `x ∈ M` is in the `add_submonoid` of `M` generated by `s ∪ t` iff there exists an element of the `add_submonoid` generated by `s` and an element of the `add_submonoid` generated by `t` whose sum is `x`."] theorem mem_closure_union_iff {M : Type*} [comm_monoid M] {s t : set M} {x : M} : x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x := ⟨λ hx, let ⟨L, HL1, HL2⟩ := exists_list_of_mem_closure hx in HL2 ▸ list.rec_on L (λ _, ⟨1, is_submonoid.one_mem, 1, is_submonoid.one_mem, mul_one _⟩) (λ hd tl ih HL1, let ⟨y, hy, z, hz, hyzx⟩ := ih (list.forall_mem_of_forall_mem_cons HL1) in or.cases_on (HL1 hd $ list.mem_cons_self _ _) (λ hs, ⟨hd * y, is_submonoid.mul_mem (subset_closure hs) hy, z, hz, by rw [mul_assoc, list.prod_cons, ← hyzx]; refl⟩) (λ ht, ⟨y, hy, z * hd, is_submonoid.mul_mem hz (subset_closure ht), by rw [← mul_assoc, list.prod_cons, ← hyzx, mul_comm hd]; refl⟩)) HL1, λ ⟨y, hy, z, hz, hyzx⟩, hyzx ▸ is_submonoid.mul_mem (closure_mono (set.subset_union_left _ _) hy) (closure_mono (set.subset_union_right _ _) hz)⟩ end monoid /-- Create a bundled submonoid from a set `s` and `[is_submonoid s]`. -/ @[to_additive "Create a bundled additive submonoid from a set `s` and `[is_add_submonoid s]`."] def submonoid.of (s : set M) [h : is_submonoid s] : submonoid M := ⟨s, h.1, h.2⟩ @[to_additive] instance submonoid.is_submonoid (S : submonoid M) : is_submonoid (S : set M) := ⟨S.2, S.3⟩
dca3acbfef5a93f658b4557d3753caab5b785ed8
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/function/simple_func_dense.lean
b9d7038805441e8e7ca38b589bfe7661a705a6ab
[ "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
7,367
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov, Heather Macbeth -/ import measure_theory.function.simple_func /-! # Density of simple functions Show that each Borel measurable function can be approximated pointwise by a sequence of simple functions. ## Main definitions * `measure_theory.simple_func.nearest_pt (e : ℕ → α) (N : ℕ) : α →ₛ ℕ`: the `simple_func` sending each `x : α` to the point `e k` which is the nearest to `x` among `e 0`, ..., `e N`. * `measure_theory.simple_func.approx_on (f : β → α) (hf : measurable f) (s : set α) (y₀ : α) (h₀ : y₀ ∈ s) [separable_space s] (n : ℕ) : β →ₛ α` : a simple function that takes values in `s` and approximates `f`. ## Main results * `tendsto_approx_on` (pointwise convergence): If `f x ∈ s`, then the sequence of simple approximations `measure_theory.simple_func.approx_on f hf s y₀ h₀ n`, evaluated at `x`, tends to `f x` as `n` tends to `∞`. ## Notations * `α →ₛ β` (local notation): the type of simple functions `α → β`. -/ open set function filter topological_space ennreal emetric finset open_locale classical topology ennreal measure_theory big_operators variables {α β ι E F 𝕜 : Type*} noncomputable theory namespace measure_theory local infixr ` →ₛ `:25 := simple_func namespace simple_func /-! ### Pointwise approximation by simple functions -/ variables [measurable_space α] [pseudo_emetric_space α] [opens_measurable_space α] /-- `nearest_pt_ind e N x` is the index `k` such that `e k` is the nearest point to `x` among the points `e 0`, ..., `e N`. If more than one point are at the same distance from `x`, then `nearest_pt_ind e N x` returns the least of their indexes. -/ noncomputable def nearest_pt_ind (e : ℕ → α) : ℕ → α →ₛ ℕ | 0 := const α 0 | (N + 1) := piecewise (⋂ k ≤ N, {x | edist (e (N + 1)) x < edist (e k) x}) (measurable_set.Inter $ λ k, measurable_set.Inter $ λ hk, measurable_set_lt measurable_edist_right measurable_edist_right) (const α $ N + 1) (nearest_pt_ind N) /-- `nearest_pt e N x` is the nearest point to `x` among the points `e 0`, ..., `e N`. If more than one point are at the same distance from `x`, then `nearest_pt e N x` returns the point with the least possible index. -/ noncomputable def nearest_pt (e : ℕ → α) (N : ℕ) : α →ₛ α := (nearest_pt_ind e N).map e @[simp] lemma nearest_pt_ind_zero (e : ℕ → α) : nearest_pt_ind e 0 = const α 0 := rfl @[simp] lemma nearest_pt_zero (e : ℕ → α) : nearest_pt e 0 = const α (e 0) := rfl lemma nearest_pt_ind_succ (e : ℕ → α) (N : ℕ) (x : α) : nearest_pt_ind e (N + 1) x = if ∀ k ≤ N, edist (e (N + 1)) x < edist (e k) x then N + 1 else nearest_pt_ind e N x := by { simp only [nearest_pt_ind, coe_piecewise, set.piecewise], congr, simp } lemma nearest_pt_ind_le (e : ℕ → α) (N : ℕ) (x : α) : nearest_pt_ind e N x ≤ N := begin induction N with N ihN, { simp }, simp only [nearest_pt_ind_succ], split_ifs, exacts [le_rfl, ihN.trans N.le_succ] end lemma edist_nearest_pt_le (e : ℕ → α) (x : α) {k N : ℕ} (hk : k ≤ N) : edist (nearest_pt e N x) x ≤ edist (e k) x := begin induction N with N ihN generalizing k, { simp [nonpos_iff_eq_zero.1 hk, le_refl] }, { simp only [nearest_pt, nearest_pt_ind_succ, map_apply], split_ifs, { rcases hk.eq_or_lt with rfl|hk, exacts [le_rfl, (h k (nat.lt_succ_iff.1 hk)).le] }, { push_neg at h, rcases h with ⟨l, hlN, hxl⟩, rcases hk.eq_or_lt with rfl|hk, exacts [(ihN hlN).trans hxl, ihN (nat.lt_succ_iff.1 hk)] } } end lemma tendsto_nearest_pt {e : ℕ → α} {x : α} (hx : x ∈ closure (range e)) : tendsto (λ N, nearest_pt e N x) at_top (𝓝 x) := begin refine (at_top_basis.tendsto_iff nhds_basis_eball).2 (λ ε hε, _), rcases emetric.mem_closure_iff.1 hx ε hε with ⟨_, ⟨N, rfl⟩, hN⟩, rw [edist_comm] at hN, exact ⟨N, trivial, λ n hn, (edist_nearest_pt_le e x hn).trans_lt hN⟩ end variables [measurable_space β] {f : β → α} /-- Approximate a measurable function by a sequence of simple functions `F n` such that `F n x ∈ s`. -/ noncomputable def approx_on (f : β → α) (hf : measurable f) (s : set α) (y₀ : α) (h₀ : y₀ ∈ s) [separable_space s] (n : ℕ) : β →ₛ α := by haveI : nonempty s := ⟨⟨y₀, h₀⟩⟩; exact comp (nearest_pt (λ k, nat.cases_on k y₀ (coe ∘ dense_seq s) : ℕ → α) n) f hf @[simp] lemma approx_on_zero {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s) [separable_space s] (x : β) : approx_on f hf s y₀ h₀ 0 x = y₀ := rfl lemma approx_on_mem {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s) [separable_space s] (n : ℕ) (x : β) : approx_on f hf s y₀ h₀ n x ∈ s := begin haveI : nonempty s := ⟨⟨y₀, h₀⟩⟩, suffices : ∀ n, (nat.cases_on n y₀ (coe ∘ dense_seq s) : α) ∈ s, { apply this }, rintro (_|n), exacts [h₀, subtype.mem _] end @[simp] lemma approx_on_comp {γ : Type*} [measurable_space γ] {f : β → α} (hf : measurable f) {g : γ → β} (hg : measurable g) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s) [separable_space s] (n : ℕ) : approx_on (f ∘ g) (hf.comp hg) s y₀ h₀ n = (approx_on f hf s y₀ h₀ n).comp g hg := rfl lemma tendsto_approx_on {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s) [separable_space s] {x : β} (hx : f x ∈ closure s) : tendsto (λ n, approx_on f hf s y₀ h₀ n x) at_top (𝓝 $ f x) := begin haveI : nonempty s := ⟨⟨y₀, h₀⟩⟩, rw [← @subtype.range_coe _ s, ← image_univ, ← (dense_range_dense_seq s).closure_eq] at hx, simp only [approx_on, coe_comp], refine tendsto_nearest_pt (closure_minimal _ is_closed_closure hx), simp only [nat.range_cases_on, closure_union, range_comp coe], exact subset.trans (image_closure_subset_closure_image continuous_subtype_coe) (subset_union_right _ _) end lemma edist_approx_on_mono {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s) [separable_space s] (x : β) {m n : ℕ} (h : m ≤ n) : edist (approx_on f hf s y₀ h₀ n x) (f x) ≤ edist (approx_on f hf s y₀ h₀ m x) (f x) := begin dsimp only [approx_on, coe_comp, (∘)], exact edist_nearest_pt_le _ _ ((nearest_pt_ind_le _ _ _).trans h) end lemma edist_approx_on_le {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s) [separable_space s] (x : β) (n : ℕ) : edist (approx_on f hf s y₀ h₀ n x) (f x) ≤ edist y₀ (f x) := edist_approx_on_mono hf h₀ x (zero_le n) lemma edist_approx_on_y0_le {f : β → α} (hf : measurable f) {s : set α} {y₀ : α} (h₀ : y₀ ∈ s) [separable_space s] (x : β) (n : ℕ) : edist y₀ (approx_on f hf s y₀ h₀ n x) ≤ edist y₀ (f x) + edist y₀ (f x) := calc edist y₀ (approx_on f hf s y₀ h₀ n x) ≤ edist y₀ (f x) + edist (approx_on f hf s y₀ h₀ n x) (f x) : edist_triangle_right _ _ _ ... ≤ edist y₀ (f x) + edist y₀ (f x) : add_le_add_left (edist_approx_on_le hf h₀ x n) _ end simple_func end measure_theory
71be800423e42f71ee7502051efb688b2b4619b1
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/qed_perf_bug.lean
e9c51e71ad306b0bc176fee119721f08044d157f
[ "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
514
lean
definition f (n : nat) : nat := if n = 100000 then 1 else 0 example (n : nat) : f 100000 = (if (100000 : nat) = 100000 then 1 else 0) := rfl example (n : nat) : f 100000 = (if (100000 : nat) = 100000 then 1 else 0) := by unfold f example (n : nat) : f 100000 = (if (100000 : nat) = 100000 then 1 else 0) := by simp [f] example (n : nat) : f 100000 = (if (100000 : nat) = 100000 then 1 else 0) := by dsimp [f]; refl example (n : nat) : f 100000 = (if (100000 : nat) = 100000 then 1 else 0) := by delta f; refl
aff9d32d45cc2124e6552dd4b332f2819d22ab71
7b02c598aa57070b4cf4fbfe2416d0479220187f
/homotopy/cofiber_sequence.hlean
9dfe53dbd21bf9d4b8d24ce0865692a826a66392
[ "Apache-2.0" ]
permissive
jdchristensen/Spectral
50d4f0ddaea1484d215ef74be951da6549de221d
6ded2b94d7ae07c4098d96a68f80a9cd3d433eb8
refs/heads/master
1,611,555,010,649
1,496,724,191,000
1,496,724,191,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,828
hlean
/- Copyright (c) 2017 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Cofiber sequence of a pointed map -/ import .cohomology .pushout open pointed eq cohomology sigma sigma.ops fiber cofiber chain_complex nat succ_str algebra prod group pushout int namespace cohomology definition pred_fun {A : ℕ → Type*} (f : Πn, A n →* A (n+1)) (n : ℕ) : A (pred n) →* A n := begin cases n with n, exact pconst (A 0) (A 0), exact f n end definition type_chain_complex_snat' [constructor] (A : ℕ → Type*) (f : Πn, A n →* A (n+1)) (p : Πn (x : A n), f (n+1) (f n x) = pt) : type_chain_complex -ℕ := type_chain_complex.mk A (pred_fun f) begin intro n, cases n with n, intro x, reflexivity, cases n with n, intro x, exact respect_pt (f 0), exact p n end definition chain_complex_snat' [constructor] (A : ℕ → Set*) (f : Πn, A n →* A (n+1)) (p : Πn (x : A n), f (n+1) (f n x) = pt) : chain_complex -ℕ := chain_complex.mk A (pred_fun f) begin intro n, cases n with n, intro x, reflexivity, cases n with n, intro x, exact respect_pt (f 0), exact p n end definition is_exact_at_t_snat' [constructor] {A : ℕ → Type*} (f : Πn, A n →* A (n+1)) (p : Πn (x : A n), f (n+1) (f n x) = pt) (q : Πn x, f (n+1) x = pt → fiber (f n) x) (n : ℕ) : is_exact_at_t (type_chain_complex_snat' A f p) (n+2) := q n definition cofiber_sequence_helper [constructor] (v : Σ(X Y : Type*), X →* Y) : Σ(Y Z : Type*), Y →* Z := ⟨v.2.1, pcofiber v.2.2, pcod v.2.2⟩ definition cofiber_sequence_helpern (v : Σ(X Y : Type*), X →* Y) (n : ℕ) : Σ(Z X : Type*), Z →* X := iterate cofiber_sequence_helper n v section universe variable u parameters {X Y : pType.{u}} (f : X →* Y) include f definition cofiber_sequence_carrier (n : ℕ) : Type* := (cofiber_sequence_helpern ⟨X, Y, f⟩ n).1 definition cofiber_sequence_fun (n : ℕ) : cofiber_sequence_carrier n →* cofiber_sequence_carrier (n+1) := (cofiber_sequence_helpern ⟨X, Y, f⟩ n).2.2 definition cofiber_sequence : type_chain_complex.{0 u} -ℕ := begin fapply type_chain_complex_snat', { exact cofiber_sequence_carrier }, { exact cofiber_sequence_fun }, { intro n x, exact pcod_pcompose (cofiber_sequence_fun n) x } end end section universe variable u parameters {X Y : pType.{u}} (f : X →* Y) (H : cohomology_theory.{u}) include f definition cohomology_groups [reducible] : -3ℤ → AbGroup | (n, fin.mk 0 p) := H n X | (n, fin.mk 1 p) := H n Y | (n, fin.mk k p) := H n (pcofiber f) -- definition cohomology_groups_pequiv_loop_spaces2 [reducible] -- : Π(n : -3ℤ), ptrunc 0 (loop_spaces2 n) ≃* cohomology_groups n -- | (n, fin.mk 0 p) := by reflexivity -- | (n, fin.mk 1 p) := by reflexivity -- | (n, fin.mk 2 p) := by reflexivity -- | (n, fin.mk (k+3) p) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition coboundary (n : ℤ) : H (pred n) X →g H n (pcofiber f) := H ^→ n (pcofiber_pcod f ∘* pcod (pcod f)) ∘g (Hsusp_neg H n X)⁻¹ᵍ definition cohomology_groups_fun : Π(n : -3ℤ), cohomology_groups (S n) →g cohomology_groups n | (n, fin.mk 0 p) := proof H ^→ n f qed | (n, fin.mk 1 p) := proof H ^→ n (pcod f) qed | (n, fin.mk 2 p) := proof coboundary n qed | (n, fin.mk (k+3) p) := begin exfalso, apply lt_le_antisymm p, apply le_add_left end -- definition cohomology_groups_fun_pcohomology_loop_spaces_fun2 [reducible] -- : Π(n : -3ℤ), cohomology_groups_pequiv_loop_spaces2 n ∘* ptrunc_functor 0 (loop_spaces_fun2 n) ~* -- cohomology_groups_fun n ∘* cohomology_groups_pequiv_loop_spaces2 (S n) -- | (n, fin.mk 0 p) := by reflexivity -- | (n, fin.mk 1 p) := by reflexivity -- | (n, fin.mk 2 p) := -- begin -- refine !pid_pcompose ⬝* _ ⬝* !pcompose_pid⁻¹*, -- refine !ptrunc_functor_pcompose -- end -- | (n, fin.mk (k+3) p) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end open cohomology_theory definition cohomology_groups_chain_0 (n : ℤ) (x : H n (pcofiber f)) : H ^→ n f (H ^→ n (pcod f) x) = 1 := begin refine (Hcompose H n (pcod f) f x)⁻¹ ⬝ _, refine Hhomotopy H n (pcod_pcompose f) x ⬝ _, exact Hconst H n x end definition cohomology_groups_chain_1 (n : ℤ) (x : H (pred n) X) : H ^→ n (pcod f) (coboundary n x) = 1 := begin refine (Hcompose H n (pcofiber_pcod f ∘* pcod (pcod f)) (pcod f) ((Hsusp_neg H n X)⁻¹ᵍ x))⁻¹ ⬝ _, refine Hhomotopy H n (!passoc ⬝* pwhisker_left _ !pcod_pcompose ⬝* !pcompose_pconst) _ ⬝ _, exact Hconst H n _ end definition cohomology_groups_chain_2 (n : ℤ) (x : H (pred n) Y) : coboundary n (H ^→ (pred n) f x) = 1 := begin exact sorry -- refine ap (H ^→ n (pcofiber_pcod f ∘* pcod (pcod f))) _ ⬝ _, --Hsusp_neg_inv_natural H n (pcofiber_pcod f ∘* pcod (pcod f)) _ end definition cohomology_groups_chain : Π(n : -3ℤ) (x : cohomology_groups (S (S n))), cohomology_groups_fun n (cohomology_groups_fun (S n) x) = 1 | (n, fin.mk 0 p) := cohomology_groups_chain_0 n | (n, fin.mk 1 p) := cohomology_groups_chain_1 n | (n, fin.mk 2 p) := cohomology_groups_chain_2 n | (n, fin.mk (k+3) p) := begin exfalso, apply lt_le_antisymm p, apply le_add_left end definition LES_of_cohomology_groups [constructor] : chain_complex -3ℤ := chain_complex.mk (λn, cohomology_groups n) (λn, pmap_of_homomorphism (cohomology_groups_fun n)) cohomology_groups_chain definition is_exact_LES_of_cohomology_groups : is_exact LES_of_cohomology_groups := begin intro n, exact sorry end end end cohomology
32a8d114e557c274bb1d15a86ed3e06d45530448
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/logic/hydra.lean
97260914248ebb1ace10f8e7dddd0018b78c1ce2
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,721
lean
/- Copyright (c) 2022 Junyan Xu. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Junyan Xu -/ import data.finsupp.lex import data.finsupp.multiset import order.game_add /-! # Termination of a hydra game > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file deals with the following version of the hydra game: each head of the hydra is labelled by an element in a type `α`, and when you cut off one head with label `a`, it grows back an arbitrary but finite number of heads, all labelled by elements smaller than `a` with respect to a well-founded relation `r` on `α`. We show that no matter how (in what order) you choose cut off the heads, the game always terminates, i.e. all heads will eventually be cut off (but of course it can last arbitrarily long, i.e. takes an arbitrary finite number of steps). This result is stated as the well-foundedness of the `cut_expand` relation defined in this file: we model the heads of the hydra as a multiset of elements of `α`, and the valid "moves" of the game are modelled by the relation `cut_expand r` on `multiset α`: `cut_expand r s' s` is true iff `s'` is obtained by removing one head `a ∈ s` and adding back an arbitrary multiset `t` of heads such that all `a' ∈ t` satisfy `r a' a`. We follow the proof by Peter LeFanu Lumsdaine at https://mathoverflow.net/a/229084/3332. TODO: formalize the relations corresponding to more powerful (e.g. Kirby–Paris and Buchholz) hydras, and prove their well-foundedness. -/ namespace relation open multiset prod variables {α : Type*} /-- The relation that specifies valid moves in our hydra game. `cut_expand r s' s` means that `s'` is obtained by removing one head `a ∈ s` and adding back an arbitrary multiset `t` of heads such that all `a' ∈ t` satisfy `r a' a`. This is most directly translated into `s' = s.erase a + t`, but `multiset.erase` requires `decidable_eq α`, so we use the equivalent condition `s' + {a} = s + t` instead, which is also easier to verify for explicit multisets `s'`, `s` and `t`. We also don't include the condition `a ∈ s` because `s' + {a} = s + t` already guarantees `a ∈ s + t`, and if `r` is irreflexive then `a ∉ t`, which is the case when `r` is well-founded, the case we are primarily interested in. The lemma `relation.cut_expand_iff` below converts between this convenient definition and the direct translation when `r` is irreflexive. -/ def cut_expand (r : α → α → Prop) (s' s : multiset α) : Prop := ∃ (t : multiset α) (a : α), (∀ a' ∈ t, r a' a) ∧ s' + {a} = s + t variable {r : α → α → Prop} lemma cut_expand_le_inv_image_lex [hi : is_irrefl α r] : cut_expand r ≤ inv_image (finsupp.lex (rᶜ ⊓ (≠)) (<)) to_finsupp := λ s t ⟨u, a, hr, he⟩, begin classical, refine ⟨a, λ b h, _, _⟩; simp_rw to_finsupp_apply, { apply_fun count b at he, simp_rw count_add at he, convert he; convert (add_zero _).symm; rw count_eq_zero; intro hb, exacts [h.2 (mem_singleton.1 hb), h.1 (hr b hb)] }, { apply_fun count a at he, simp_rw [count_add, count_singleton_self] at he, apply nat.lt_of_succ_le, convert he.le, convert (add_zero _).symm, exact count_eq_zero.2 (λ ha, hi.irrefl a $ hr a ha) }, end theorem cut_expand_singleton {s x} (h : ∀ x' ∈ s, r x' x) : cut_expand r s {x} := ⟨s, x, h, add_comm s _⟩ theorem cut_expand_singleton_singleton {x' x} (h : r x' x) : cut_expand r {x'} {x} := cut_expand_singleton (λ a h, by rwa mem_singleton.1 h) theorem cut_expand_add_left {t u} (s) : cut_expand r (s + t) (s + u) ↔ cut_expand r t u := exists₂_congr $ λ _ _, and_congr iff.rfl $ by rw [add_assoc, add_assoc, add_left_cancel_iff] lemma cut_expand_iff [decidable_eq α] [is_irrefl α r] {s' s : multiset α} : cut_expand r s' s ↔ ∃ (t : multiset α) a, (∀ a' ∈ t, r a' a) ∧ a ∈ s ∧ s' = s.erase a + t := begin simp_rw [cut_expand, add_singleton_eq_iff], refine exists₂_congr (λ t a, ⟨_, _⟩), { rintro ⟨ht, ha, rfl⟩, obtain h|h := mem_add.1 ha, exacts [⟨ht, h, t.erase_add_left_pos h⟩, (@irrefl α r _ a (ht a h)).elim] }, { rintro ⟨ht, h, rfl⟩, exact ⟨ht, mem_add.2 (or.inl h), (t.erase_add_left_pos h).symm⟩ }, end theorem not_cut_expand_zero [is_irrefl α r] (s) : ¬ cut_expand r s 0 := by { classical, rw cut_expand_iff, rintro ⟨_, _, _, ⟨⟩, _⟩ } /-- For any relation `r` on `α`, multiset addition `multiset α × multiset α → multiset α` is a fibration between the game sum of `cut_expand r` with itself and `cut_expand r` itself. -/ lemma cut_expand_fibration (r : α → α → Prop) : fibration (game_add (cut_expand r) (cut_expand r)) (cut_expand r) (λ s, s.1 + s.2) := begin rintro ⟨s₁, s₂⟩ s ⟨t, a, hr, he⟩, dsimp at he ⊢, classical, obtain ⟨ha, rfl⟩ := add_singleton_eq_iff.1 he, rw [add_assoc, mem_add] at ha, obtain (h|h) := ha, { refine ⟨(s₁.erase a + t, s₂), game_add.fst ⟨t, a, hr, _⟩, _⟩, { rw [add_comm, ← add_assoc, singleton_add, cons_erase h] }, { rw [add_assoc s₁, erase_add_left_pos _ h, add_right_comm, add_assoc] } }, { refine ⟨(s₁, (s₂ + t).erase a), game_add.snd ⟨t, a, hr, _⟩, _⟩, { rw [add_comm, singleton_add, cons_erase h] }, { rw [add_assoc, erase_add_right_pos _ h] } }, end /-- A multiset is accessible under `cut_expand` if all its singleton subsets are, assuming `r` is irreflexive. -/ lemma acc_of_singleton [is_irrefl α r] {s : multiset α} : (∀ a ∈ s, acc (cut_expand r) {a}) → acc (cut_expand r) s := begin refine multiset.induction _ _ s, { exact λ _, acc.intro 0 $ λ s h, (not_cut_expand_zero s h).elim }, { intros a s ih hacc, rw ← s.singleton_add a, exact ((hacc a $ s.mem_cons_self a).prod_game_add $ ih $ λ a ha, hacc a $ mem_cons_of_mem ha).of_fibration _ (cut_expand_fibration r) }, end /-- A singleton `{a}` is accessible under `cut_expand r` if `a` is accessible under `r`, assuming `r` is irreflexive. -/ lemma _root_.acc.cut_expand [is_irrefl α r] {a : α} (hacc : acc r a) : acc (cut_expand r) {a} := begin induction hacc with a h ih, refine acc.intro _ (λ s, _), classical, rw cut_expand_iff, rintro ⟨t, a, hr, rfl|⟨⟨⟩⟩, rfl⟩, refine acc_of_singleton (λ a', _), rw [erase_singleton, zero_add], exact ih a' ∘ hr a', end /-- `cut_expand r` is well-founded when `r` is. -/ theorem _root_.well_founded.cut_expand (hr : well_founded r) : well_founded (cut_expand r) := ⟨by { letI h := hr.is_irrefl, exact λ s, acc_of_singleton $ λ a _, (hr.apply a).cut_expand }⟩ end relation
823b7084318c5ef853fd623c5a7112c32c90ac42
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/analysis/specific_limits.lean
7f87c02cf5c027c22916fec60997aad7ed87dc48
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
6,174
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 A collection of specific limit computations. -/ import analysis.normed_space.basic import topology.instances.ennreal noncomputable theory local attribute [instance] classical.prop_decidable open classical function lattice filter finset metric variables {α : Type*} {β : Type*} {ι : Type*} lemma has_sum_of_absolute_convergence_real {f : ℕ → ℝ} : (∃r, tendsto (λn, (range n).sum (λi, abs (f i))) at_top (nhds r)) → has_sum f | ⟨r, hr⟩ := begin refine has_sum_of_has_sum_norm ⟨r, (is_sum_iff_tendsto_nat_of_nonneg _ _).2 _⟩, exact assume i, norm_nonneg _, simpa only using hr end lemma tendsto_pow_at_top_at_top_of_gt_1 {r : ℝ} (h : r > 1) : tendsto (λn:ℕ, r ^ n) at_top at_top := tendsto_infi.2 $ assume p, tendsto_principal.2 $ let ⟨n, hn⟩ := exists_nat_gt (p / (r - 1)) in have hn_nn : (0:ℝ) ≤ n, from nat.cast_nonneg n, have r - 1 > 0, from sub_lt_iff_lt_add.mp $ by simp; assumption, have p ≤ r ^ n, from calc p = (p / (r - 1)) * (r - 1) : (div_mul_cancel _ $ ne_of_gt this).symm ... ≤ n * (r - 1) : mul_le_mul (le_of_lt hn) (le_refl _) (le_of_lt this) hn_nn ... ≤ 1 + n * (r - 1) : le_add_of_nonneg_of_le zero_le_one (le_refl _) ... = 1 + add_monoid.smul n (r - 1) : by rw [add_monoid.smul_eq_mul] ... ≤ (1 + (r - 1)) ^ n : pow_ge_one_add_mul (le_of_lt this) _ ... ≤ r ^ n : by simp; exact le_refl _, show {n | p ≤ r ^ n} ∈ at_top.sets, from mem_at_top_sets.mpr ⟨n, assume m hnm, le_trans this (pow_le_pow (le_of_lt h) hnm)⟩ lemma tendsto_inverse_at_top_nhds_0 : tendsto (λr:ℝ, r⁻¹) at_top (nhds 0) := tendsto_orderable_unbounded (no_top 0) (no_bot 0) $ assume l u hl hu, mem_at_top_sets.mpr ⟨u⁻¹ + 1, assume b hb, have u⁻¹ < b, from lt_of_lt_of_le (lt_add_of_pos_right _ zero_lt_one) hb, ⟨lt_trans hl $ inv_pos $ lt_trans (inv_pos hu) this, lt_of_one_div_lt_one_div hu $ begin rw [inv_eq_one_div], simp [-one_div_eq_inv, div_div_eq_mul_div, div_one], simp [this] end⟩⟩ lemma tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (nhds 0) := by_cases (assume : r = 0, (tendsto_add_at_top_iff_nat 1).mp $ by simp [pow_succ, this, tendsto_const_nhds]) (assume : r ≠ 0, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (nhds 0), from (tendsto_pow_at_top_at_top_of_gt_1 $ one_lt_inv (lt_of_le_of_ne h₁ this.symm) h₂).comp tendsto_inverse_at_top_nhds_0, tendsto_cong this $ univ_mem_sets' $ by simp *) lemma tendsto_pow_at_top_at_top_of_gt_1_nat {k : ℕ} (h : 1 < k) : tendsto (λn:ℕ, k ^ n) at_top at_top := tendsto_coe_nat_real_at_top_iff.1 $ have hr : 1 < (k : ℝ), by rw [← nat.cast_one, nat.cast_lt]; exact h, by simpa using tendsto_pow_at_top_at_top_of_gt_1 hr lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (nhds 0) := tendsto.comp (tendsto_coe_nat_real_at_top_iff.2 tendsto_id) tendsto_inverse_at_top_nhds_0 lemma tendsto_one_div_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1/(n : ℝ)) at_top (nhds 0) := by simpa only [inv_eq_one_div] using tendsto_inverse_at_top_nhds_0_nat lemma tendsto_one_div_add_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1 / ((n : ℝ) + 1)) at_top (nhds 0) := suffices tendsto (λ n : ℕ, 1 / (↑(n + 1) : ℝ)) at_top (nhds 0), by simpa, (tendsto_add_at_top_iff_nat 1).2 tendsto_one_div_at_top_nhds_0_nat lemma is_sum_geometric {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : is_sum (λn:ℕ, r ^ n) (1 / (1 - r)) := have r ≠ 1, from ne_of_lt h₂, have r + -1 ≠ 0, by rw [←sub_eq_add_neg, ne, sub_eq_iff_eq_add]; simp; assumption, have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (nhds ((0 - 1) * (r - 1)⁻¹)), from tendsto_mul (tendsto_sub (tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂) tendsto_const_nhds) tendsto_const_nhds, (is_sum_iff_tendsto_nat_of_nonneg (pow_nonneg h₁) _).mpr $ by simp [neg_inv, geom_sum, div_eq_mul_inv, *] at * lemma is_sum_geometric_two (a : ℝ) : is_sum (λn:ℕ, (a / 2) / 2 ^ n) a := begin convert is_sum_mul_left (a / 2) (is_sum_geometric (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, rw ← pow_inv; [refl, exact two_ne_zero] }, { norm_num, rw div_mul_cancel _ two_ne_zero } end def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε) (ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, is_sum ε' c ∧ c ≤ ε} := begin let f := λ n, (ε / 2) / 2 ^ n, have hf : is_sum f ε := is_sum_geometric_two _, have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos two_pos _), refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩, rcases has_sum_comp_of_has_sum_of_injective f (has_sum_spec hf) (@encodable.encode_injective ι _) with ⟨c, hg⟩, refine ⟨c, hg, is_sum_le_inj _ (@encodable.encode_injective ι _) _ _ hg hf⟩, { assume i _, exact le_of_lt (f0 _) }, { assume n, exact le_refl _ } end namespace nnreal theorem exists_pos_sum_of_encodable {ε : nnreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ ∃c, is_sum ε' c ∧ c < ε := let ⟨a, a0, aε⟩ := dense hε in let ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι in ⟨ λi, ⟨ε' i, le_of_lt $ hε' i⟩, assume i, nnreal.coe_lt.2 $ hε' i, ⟨c, is_sum_le (assume i, le_of_lt $ hε' i) is_sum_zero hc ⟩, nnreal.is_sum_coe.1 hc, lt_of_le_of_lt (nnreal.coe_le.1 hcε) aε ⟩ end nnreal namespace ennreal theorem exists_pos_sum_of_encodable {ε : ennreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ (∑ i, (ε' i : ennreal)) < ε := begin rcases dense hε with ⟨r, h0r, hrε⟩, rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩, rcases nnreal.exists_pos_sum_of_encodable (coe_lt_coe.1 h0r) ι with ⟨ε', hp, c, hc, hcr⟩, exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩ end end ennreal
d8537dbc40aad256310ab618ddd29831bcccf00a
a44280b79dc85615010e3fbda46abf82c6730fa3
/tests/playground/parser/parser.lean
0ffc373b6042ab2cb7ab582e7454f336fe057998
[ "Apache-2.0" ]
permissive
kodyvajjha/lean4
8e1c613248b531d47367ca6e8d97ee1046645aa1
c8a045d69fac152fd5e3a577f718615cecb9c53d
refs/heads/master
1,589,684,450,102
1,555,200,447,000
1,556,139,945,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
32,700
lean
import init.lean.name init.lean.parser.trie init.lean.parser.identifier import syntax filemap open Lean export Lean.Parser (Trie) -- namespace Lean namespace Parser /-- A multimap indexed by tokens. Used for indexing parsers by their leading token. -/ def TokenMap (α : Type) := RBMap Name (List α) Name.quickLt namespace TokenMap def insert {α : Type} (map : TokenMap α) (k : Name) (v : α) : TokenMap α := match map.find k with | none := map.insert k [v] | some vs := map.insert k (v::vs) def ofListAux {α : Type} : List (Name × α) → TokenMap α → TokenMap α | [] m := m | (⟨k,v⟩::xs) m := ofListAux xs (m.insert k v) def ofList {α : Type} (es : List (Name × α)) : TokenMap α := ofListAux es RBMap.empty end TokenMap structure FrontendConfig := (filename : String) (input : String) (fileMap : FileMap) structure TokenConfig := (val : String) (lbp : Nat := 0) namespace TokenConfig def beq : TokenConfig → TokenConfig → Bool | ⟨val₁, lbp₁⟩ ⟨val₂, lbp₂⟩ := val₁ == val₂ && lbp₁ == lbp₂ instance : HasBeq TokenConfig := ⟨beq⟩ end TokenConfig structure TokenCacheEntry := (startPos stopPos : String.Pos) (token : Syntax) structure ParserCache := (tokenCache : Option TokenCacheEntry := none) structure ParserConfig extends FrontendConfig := (tokens : Trie TokenConfig) structure ParserData := (stxStack : Array Syntax) (pos : String.Pos) (cache : ParserCache) (errorMsg : Option String) @[inline] def ParserData.hasError (d : ParserData) : Bool := d.errorMsg != none @[inline] def ParserData.stackSize (d : ParserData) : Nat := d.stxStack.size def ParserData.restore (d : ParserData) (iniStackSz : Nat) (iniPos : Nat) : ParserData := match d with | ⟨stack, _, cache, _⟩ := ⟨stack.shrink iniStackSz, iniPos, cache, none⟩ def ParserData.setPos (d : ParserData) (pos : Nat) : ParserData := match d with | ⟨stack, _, cache, msg⟩ := ⟨stack, pos, cache, msg⟩ def ParserData.setCache (d : ParserData) (cache : ParserCache) : ParserData := match d with | ⟨stack, pos, _, msg⟩ := ⟨stack, pos, cache, msg⟩ def ParserData.pushSyntax (d : ParserData) (n : Syntax) : ParserData := match d with | ⟨stack, pos, cache, msg⟩ := ⟨stack.push n, pos, cache, msg⟩ def ParserData.shrinkStack (d : ParserData) (iniStackSz : Nat) : ParserData := match d with | ⟨stack, pos, cache, msg⟩ := ⟨stack.shrink iniStackSz, pos, cache, msg⟩ def ParserData.next (d : ParserData) (s : String) (pos : Nat) : ParserData := d.setPos (s.next pos) def ParserData.toErrorMsg (d : ParserData) (cfg : ParserConfig) : String := match d.errorMsg with | none := "" | some msg := let pos := cfg.fileMap.toPosition d.pos in cfg.filename ++ ":" ++ toString pos.line ++ ":" ++ toString pos.column ++ " " ++ msg def ParserFn := String → ParserData → ParserData instance : Inhabited ParserFn := ⟨λ s, id⟩ structure ParserInfo := (updateTokens : Trie TokenConfig → Trie TokenConfig := λ tks, tks) (firstTokens : List TokenConfig := []) @[inline] def andthenFn (p q : ParserFn) : ParserFn | s d := let d := p s d in if d.hasError then d else q s d @[noinline] def andthenInfo (p q : ParserInfo) : ParserInfo := { updateTokens := q.updateTokens ∘ p.updateTokens, firstTokens := p.firstTokens } def ParserData.mkNode (d : ParserData) (k : SyntaxNodeKind) (iniStackSz : Nat) : ParserData := match d with | ⟨stack, pos, cache, err⟩ := if err != none && stack.size == iniStackSz then -- If there is an error but there are no new nodes on the stack, we just return `d` d else let newNode := Syntax.node k (stack.extract iniStackSz stack.size) [] in let stack := stack.shrink iniStackSz in let stack := stack.push newNode in ⟨stack, pos, cache, err⟩ @[inline] def nodeFn (k : SyntaxNodeKind) (p : ParserFn) : ParserFn | s d := let iniSz := d.stackSize in let d := p s d in d.mkNode k iniSz @[noinline] def nodeInfo (p : ParserInfo) : ParserInfo := { updateTokens := p.updateTokens, firstTokens := p.firstTokens } @[inline] def orelseFn (p q : ParserFn) : ParserFn | s d := let iniSz := d.stackSize in let iniPos := d.pos in let d := p s d in if d.hasError && d.pos == iniPos then q s (d.restore iniSz iniPos) else d @[noinline] def orelseInfo (p q : ParserInfo) : ParserInfo := { updateTokens := q.updateTokens ∘ p.updateTokens, firstTokens := p.firstTokens ++ q.firstTokens } @[inline] def tryFn (p : ParserFn) : ParserFn | s d := let iniSz := d.stackSize in let iniPos := d.pos in match p s d with | ⟨stack, _, cache, some msg⟩ := ⟨stack.shrink iniSz, iniPos, cache, some msg⟩ | other := other @[noinline] def noFirstTokenInfo (info : ParserInfo) : ParserInfo := { updateTokens := info.updateTokens, firstTokens := [] } @[inline] def optionalFn (p : ParserFn) : ParserFn := λ s d, let iniSz := d.stackSize in let iniPos := d.pos in let d := p s d in let d := if d.hasError then d.restore iniSz iniPos else d in d.mkNode nullKind iniSz def ParserData.mkError (d : ParserData) (msg : String) : ParserData := match d with | ⟨stack, pos, cache, _⟩ := ⟨stack, pos, cache, some msg⟩ def ParserData.mkEOIError (d : ParserData) : ParserData := d.mkError "end of input" def ParserData.mkErrorAt (d : ParserData) (msg : String) (pos : String.Pos) : ParserData := match d with | ⟨stack, _, cache, _⟩ := ⟨stack, pos, cache, some msg⟩ @[specialize] partial def manyAux (p : ParserFn) : String → ParserData → ParserData | s d := let iniSz := d.stackSize in let iniPos := d.pos in let d := p s d in if d.hasError then d.restore iniSz iniPos else if iniPos == d.pos then d.mkError "invalid 'many' parser combinator application, parser did not consume anything" else manyAux s d @[inline] def manyFn (p : ParserFn) : ParserFn | s d := let iniSz := d.stackSize in let d := manyAux p s d in d.mkNode nullKind iniSz @[specialize] private partial def sepByFnAux (p : ParserFn) (sep : ParserFn) (allowTrailingSep : Bool) (iniSz : Nat) : Bool → ParserFn | pOpt s d := let sz := d.stackSize in let pos := d.pos in let d := p s d in if d.hasError then let d := d.restore sz pos in if pOpt then d.mkNode nullKind iniSz else -- append `Syntax.missing` to make clear that List is incomplete let d := d.pushSyntax Syntax.missing in d.mkNode nullKind iniSz else let sz := d.stackSize in let pos := d.pos in let d := sep s d in if d.hasError then let d := d.restore sz pos in d.mkNode nullKind iniSz else sepByFnAux allowTrailingSep s d @[specialize] def sepByFn (allowTrailingSep : Bool) (p : ParserFn) (sep : ParserFn) : ParserFn | s d := let iniSz := d.stackSize in sepByFnAux p sep allowTrailingSep iniSz true s d @[specialize] def sepBy1Fn (allowTrailingSep : Bool) (p : ParserFn) (sep : ParserFn) : ParserFn | s d := let iniSz := d.stackSize in sepByFnAux p sep allowTrailingSep iniSz false s d @[noinline] def sepByInfo (p sep : ParserInfo) : ParserInfo := { updateTokens := sep.updateTokens ∘ p.updateTokens, firstTokens := [] } @[noinline] def sepBy1Info (p sep : ParserInfo) : ParserInfo := { updateTokens := sep.updateTokens ∘ p.updateTokens, firstTokens := p.firstTokens } @[specialize] partial def satisfyFn (p : Char → Bool) (errorMsg : String := "unexpected character") : ParserFn | s d := let i := d.pos in if s.atEnd i then d.mkEOIError else let c := s.get i in if p c then d.next s i else d.mkError errorMsg @[specialize] partial def takeUntilFn (p : Char → Bool) : ParserFn | s d := let i := d.pos in if s.atEnd i then d else let c := s.get i in if p c then d else takeUntilFn s (d.next s i) @[specialize] def takeWhileFn (p : Char → Bool) : ParserFn := takeUntilFn (λ c, !p c) @[inline] def takeWhile1Fn (p : Char → Bool) (errorMsg : String) : ParserFn := andthenFn (satisfyFn p errorMsg) (takeWhileFn p) partial def finishCommentBlock : Nat → ParserFn | nesting s d := let i := d.pos in if s.atEnd i then d.mkEOIError else let c := s.get i in let i := s.next i in if c == '-' then if s.atEnd i then d.mkEOIError else let c := s.get i in if c == '/' then -- "-/" end of comment if nesting == 1 then d.next s i else finishCommentBlock (nesting-1) s (d.next s i) else finishCommentBlock nesting s (d.next s i) else if c == '/' then if s.atEnd i then d.mkEOIError else let c := s.get i in if c == '-' then finishCommentBlock (nesting+1) s (d.next s i) else finishCommentBlock nesting s (d.setPos i) else finishCommentBlock nesting s (d.setPos i) /- Consume whitespace and comments -/ partial def whitespace : ParserFn | s d := let i := d.pos in if s.atEnd i then d else let c := s.get i in if c.isWhitespace then whitespace s (d.next s i) else if c == '-' then let i := s.next i in let c := s.get i in if c == '-' then andthenFn (takeUntilFn (= '\n')) whitespace s (d.next s i) else d else if c == '/' then let i := s.next i in let c := s.get i in if c == '-' then let i := s.next i in let c := s.get i in if c == '-' then d -- "/--" doc comment is an actual token else andthenFn (finishCommentBlock 1) whitespace s (d.next s i) else d else d def mkEmptySubstringAt (s : String) (p : Nat) : Substring := {str := s, startPos := p, stopPos := p } private def rawAux (startPos : Nat) (trailingWs : Bool) : ParserFn | s d := let stopPos := d.pos in let leading := mkEmptySubstringAt s startPos in let val := s.extract startPos stopPos in if trailingWs then let d := whitespace s d in let stopPos' := d.pos in let trailing : Substring := { str := s, startPos := stopPos, stopPos := stopPos' } in let atom := Syntax.atom (some { leading := leading, pos := startPos, trailing := trailing }) val in d.pushSyntax atom else let trailing := mkEmptySubstringAt s stopPos in let atom := Syntax.atom (some { leading := leading, pos := startPos, trailing := trailing }) val in d.pushSyntax atom /-- Match an arbitrary Parser and return the consumed String in a `Syntax.atom`. -/ @[inline] def rawFn (p : ParserFn) (trailingWs := false) : ParserFn | s d := let startPos := d.pos in let d := p s d in if d.hasError then d else rawAux startPos trailingWs s d def hexDigitFn : ParserFn | s d := let i := d.pos in if s.atEnd i then d.mkEOIError else let c := s.get i in let i := s.next i in if c.isDigit || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') then d.setPos i else d.mkError "invalid hexadecimal numeral, hexadecimal digit expected" def quotedCharFn : ParserFn | s d := let i := d.pos in if s.atEnd i then d.mkEOIError else let c := s.get i in if c == '\\' || c == '\"' || c == '\'' || c == '\n' || c == '\t' then d.next s i else if c == 'x' then andthenFn hexDigitFn hexDigitFn s (d.next s i) else if c == 'u' then andthenFn hexDigitFn (andthenFn hexDigitFn (andthenFn hexDigitFn hexDigitFn)) s (d.next s i) else d.mkError "invalid escape sequence" def mkStrLitKind : IO SyntaxNodeKind := nextKind `strLit @[init mkStrLitKind] constant strLitKind : SyntaxNodeKind := default _ /-- Push `(Syntax.node tk <new-atom>)` into syntax stack -/ def mkNodeToken (k : SyntaxNodeKind) (startPos : Nat) (s : String) (d : ParserData) : ParserData := let stopPos := d.pos in let leading := mkEmptySubstringAt s startPos in let val := s.extract startPos stopPos in let d := whitespace s d in let wsStopPos := d.pos in let trailing := { Substring . str := s, startPos := stopPos, stopPos := wsStopPos } in let atom := Syntax.atom (some { leading := leading, pos := startPos, trailing := trailing }) val in let tk := Syntax.node k (Array.singleton atom) [] in d.pushSyntax tk partial def strLitFnAux (startPos : Nat) : ParserFn | s d := let i := d.pos in if s.atEnd i then d.mkEOIError else let c := s.get i in let d := d.setPos (s.next i) in if c == '\"' then mkNodeToken strLitKind startPos s d else if c == '\\' then andthenFn quotedCharFn strLitFnAux s d else strLitFnAux s d def mkNumberKind : IO SyntaxNodeKind := nextKind `number @[init mkNumberKind] constant numberKind : SyntaxNodeKind := default _ def decimalNumberFn (startPos : Nat) : ParserFn | s d := let d := takeWhileFn (λ c, c.isDigit) s d in let i := d.pos in let c := s.get i in let d := if c == '.' then let i := s.next i in let c := s.get i in if c.isDigit then takeWhileFn (λ c, c.isDigit) s (d.setPos i) else d else d in mkNodeToken numberKind startPos s d def binNumberFn (startPos : Nat) : ParserFn | s d := let d := takeWhile1Fn (λ c, c == '0' || c == '1') "expected binary number" s d in mkNodeToken numberKind startPos s d def octalNumberFn (startPos : Nat) : ParserFn | s d := let d := takeWhile1Fn (λ c, '0' ≤ c && c ≤ '7') "expected octal number" s d in mkNodeToken numberKind startPos s d def hexNumberFn (startPos : Nat) : ParserFn | s d := let d := takeWhile1Fn (λ c, ('0' ≤ c && c ≤ '9') || ('a' ≤ c && c ≤ 'f') || ('A' ≤ c && c ≤ 'F')) "expected hexadecimal number" s d in mkNodeToken numberKind startPos s d def numberFnAux : ParserFn | s d := let startPos := d.pos in if s.atEnd startPos then d.mkEOIError else let c := s.get startPos in if c == '0' then let i := s.next startPos in let c := s.get i in if c == 'b' || c == 'B' then binNumberFn startPos s (d.next s i) else if c == 'o' || c == 'O' then octalNumberFn startPos s (d.next s i) else if c == 'x' || c == 'X' then hexNumberFn startPos s (d.next s i) else decimalNumberFn startPos s (d.setPos i) else if c.isDigit then decimalNumberFn startPos s (d.next s startPos) else d.mkError "expected numeral" def isIdCont : String → ParserData → Bool | s d := let i := d.pos in let c := s.get i in if c == '.' then let i := s.next i in if s.atEnd i then false else let c := s.get i in isIdFirst c || isIdBeginEscape c else false private def isToken (idStartPos idStopPos : Nat) (tk : Option TokenConfig) : Bool := match tk with | none := false | some tk := -- if a token is both a symbol and a valid identifier (i.e. a keyword), -- we want it to be recognized as a symbol tk.val.bsize ≥ idStopPos - idStopPos def mkTokenAndFixPos (startPos : Nat) (tk : Option TokenConfig) (s : String) (d : ParserData) : ParserData := match tk with | none := d.mkErrorAt "token expected" startPos | some tk := let leading := mkEmptySubstringAt s startPos in let val := tk.val in let stopPos := startPos + val.bsize in let d := d.setPos stopPos in let d := whitespace s d in let wsStopPos := d.pos in let trailing := { Substring . str := s, startPos := stopPos, stopPos := wsStopPos } in let atom := Syntax.atom (some { leading := leading, pos := startPos, trailing := trailing }) val in d.pushSyntax atom def mkIdResult (startPos : Nat) (tk : Option TokenConfig) (val : Name) (s : String) (d : ParserData) : ParserData := let stopPos := d.pos in if isToken startPos stopPos tk then mkTokenAndFixPos startPos tk s d else let rawVal : Substring := { str := s, startPos := startPos, stopPos := stopPos } in let d := whitespace s d in let trailingStopPos := d.pos in let leading := mkEmptySubstringAt s startPos in let trailing : Substring := { str := s, startPos := stopPos, stopPos := trailingStopPos } in let info : SourceInfo := {leading := leading, trailing := trailing, pos := startPos} in let atom := Syntax.ident (some info) rawVal val [] [] in d.pushSyntax atom partial def identFnAux (startPos : Nat) (tk : Option TokenConfig) : Name → ParserFn | r s d := let i := d.pos in if s.atEnd i then d.mkEOIError else let c := s.get i in if isIdBeginEscape c then let startPart := s.next i in let d := takeUntilFn isIdEndEscape s (d.setPos startPart) in let stopPart := d.pos in let d := satisfyFn isIdEndEscape "end of escaped identifier expected" s d in if d.hasError then d else let r := Name.mkString r (s.extract startPart stopPart) in if isIdCont s d then identFnAux r s d else mkIdResult startPos tk r s d else if isIdFirst c then let startPart := i in let d := takeWhileFn isIdRest s (d.next s i) in let stopPart := d.pos in let r := Name.mkString r (s.extract startPart stopPart) in if isIdCont s d then identFnAux r s d else mkIdResult startPart tk r s d else mkTokenAndFixPos startPos tk s d def ParserData.keepNewError (d : ParserData) (oldStackSize : Nat) : ParserData := match d with | ⟨stack, pos, cache, err⟩ := ⟨stack.shrink oldStackSize, pos, cache, err⟩ def ParserData.keepPrevError (d : ParserData) (oldStackSize : Nat) (oldStopPos : String.Pos) (oldError : Option String) : ParserData := match d with | ⟨stack, _, cache, _⟩ := ⟨stack.shrink oldStackSize, oldStopPos, cache, oldError⟩ def ParserData.mergeErrors (d : ParserData) (oldStackSize : Nat) (oldError : String) : ParserData := match d with | ⟨stack, pos, cache, some err⟩ := ⟨stack.shrink oldStackSize, pos, cache, some (err ++ "; " ++ oldError)⟩ | other := other def ParserData.mkLongestNodeAlt (d : ParserData) (startSize : Nat) : ParserData := match d with | ⟨stack, pos, cache, _⟩ := if stack.size == startSize then ⟨stack.push Syntax.missing, pos, cache, none⟩ -- parser did not create any node, then we just add `Syntax.missing` else if stack.size == startSize + 1 then d else -- parser created more than one node, combine them into a single node let node := Syntax.node nullKind (stack.extract startSize stack.size) [] in let stack := stack.shrink startSize in ⟨stack.push node, pos, cache, none⟩ def ParserData.keepLatest (d : ParserData) (startStackSize : Nat) : ParserData := match d with | ⟨stack, pos, cache, _⟩ := let node := stack.back in let stack := stack.shrink startStackSize in let stack := stack.push node in ⟨stack, pos, cache, none⟩ def ParserData.replaceLongest (d : ParserData) (startStackSize : Nat) (prevStackSize : Nat) : ParserData := let d := d.mkLongestNodeAlt prevStackSize in d.keepLatest startStackSize def longestMatchStep (startSize : Nat) (startPos : String.Pos) (p : ParserFn) : ParserFn := λ s d, let prevErrorMsg := d.errorMsg in let prevStopPos := d.pos in let prevSize := d.stackSize in let d := d.restore prevSize startPos in let d := p s d in match prevErrorMsg, d.errorMsg with | none, none := -- both succeeded if d.pos > prevStopPos then d.replaceLongest startSize prevSize -- replace else if d.pos < prevStopPos then d.restore prevSize prevStopPos -- keep prev else d.mkLongestNodeAlt prevSize -- keep both | none, some _ := -- prev succeeded, current failed d.restore prevSize prevStopPos | some oldError, some _ := -- both failed if d.pos > prevStopPos then d.keepNewError prevSize else if d.pos < prevStopPos then d.keepPrevError prevSize prevStopPos prevErrorMsg else d.mergeErrors prevSize oldError | some _, none := -- prev failed, current succeeded d.mkLongestNodeAlt startSize def longestMatchMkResult (startSize : Nat) (d : ParserData) : ParserData := if !d.hasError && d.stackSize > startSize + 1 then d.mkNode choiceKind startSize else d def longestMatchFnAux (startSize : Nat) (startPos : String.Pos) : List ParserFn → ParserFn | [] := λ _ d, longestMatchMkResult startSize d | (p::ps) := λ s d, let d := longestMatchStep startSize startPos p s d in longestMatchFnAux ps s d def longestMatchFn₁ (p : ParserFn) : ParserFn := λ s d, let startSize := d.stackSize in let d := p s d in if d.hasError then d else d.mkLongestNodeAlt startSize def longestMatchFn₂ (p q : ParserFn) : ParserFn := λ s d, let startSize := d.stackSize in let startPos := d.pos in let d := p s d in let d := if d.hasError then d.shrinkStack startSize else d.mkLongestNodeAlt startSize in let d := longestMatchStep startSize startPos q s d in longestMatchMkResult startSize d def longestMatchFn : List ParserFn → ParserFn | [] := λ _ d, d.mkError "longest match: empty list" | [p] := longestMatchFn₁ p | (p::ps) := λ s d, let startSize := d.stackSize in let startPos := d.pos in let d := p s d in if d.hasError then let d := d.shrinkStack startSize in longestMatchFnAux startSize startPos ps s d else let d := d.mkLongestNodeAlt startSize in longestMatchFnAux startSize startPos ps s d structure AbsParser (ρ : Type) := (info : ParserInfo := {}) (fn : ρ) abbrev Parser := AbsParser ParserFn class ParserFnLift (ρ : Type) := (lift {} : ParserFn → ρ) (map : (ParserFn → ParserFn) → ρ → ρ) (map₂ : (ParserFn → ParserFn → ParserFn) → ρ → ρ → ρ) (mapList : (List ParserFn → ParserFn) → List ρ → ρ) instance parserLiftInhabited {ρ : Type} [ParserFnLift ρ] : Inhabited ρ := ⟨ParserFnLift.lift (default _)⟩ instance idParserLift : ParserFnLift ParserFn := { lift := λ p, p, map := λ m p, m p, map₂ := λ m p1 p2, m p1 p2, mapList := λ m ps, m ps } @[inline] def liftParser {ρ : Type} [ParserFnLift ρ] (info : ParserInfo) (fn : ParserFn) : AbsParser ρ := { info := info, fn := ParserFnLift.lift fn } @[inline] def mapParser {ρ : Type} [ParserFnLift ρ] (infoFn : ParserInfo → ParserInfo) (pFn : ParserFn → ParserFn) : AbsParser ρ → AbsParser ρ := λ p, { info := infoFn p.info, fn := ParserFnLift.map pFn p.fn } @[inline] def mapParser₂ {ρ : Type} [ParserFnLift ρ] (infoFn : ParserInfo → ParserInfo → ParserInfo) (pFn : ParserFn → ParserFn → ParserFn) : AbsParser ρ → AbsParser ρ → AbsParser ρ := λ p q, { info := infoFn p.info q.info, fn := ParserFnLift.map₂ pFn p.fn q.fn } def EnvParserFn (α : Type) (ρ : Type) := α → ρ def RecParserFn (α ρ : Type) := EnvParserFn (α → ρ) ρ instance envParserLift (α ρ : Type) [ParserFnLift ρ] : ParserFnLift (EnvParserFn α ρ) := { lift := λ p a, ParserFnLift.lift p, map := λ m p a, ParserFnLift.map m (p a), map₂ := λ m p1 p2 a, ParserFnLift.map₂ m (p1 a) (p2 a), mapList := λ m ps a, ParserFnLift.mapList m (ps.map (λ p, p a)) } instance recParserLift (α ρ : Type) [ParserFnLift ρ] : ParserFnLift (RecParserFn α ρ) := inferInstanceAs (ParserFnLift (EnvParserFn (α → ρ) ρ)) namespace RecParserFn variables {α ρ : Type} @[inline] def recurse (a : α) : RecParserFn α ρ := λ p, p a @[inline] def run [ParserFnLift ρ] (x : RecParserFn α ρ) (rec : α → RecParserFn α ρ) : ρ := x (fix (λ f a, rec a f)) end RecParserFn @[inline] def andthen {ρ : Type} [ParserFnLift ρ] : AbsParser ρ → AbsParser ρ → AbsParser ρ := mapParser₂ andthenInfo andthenFn instance absParserAndthen {ρ : Type} [ParserFnLift ρ] : HasAndthen (AbsParser ρ) := ⟨andthen⟩ @[inline] def node {ρ : Type} [ParserFnLift ρ] (k : SyntaxNodeKind) : AbsParser ρ → AbsParser ρ := mapParser nodeInfo (nodeFn k) @[inline] def orelse {ρ : Type} [ParserFnLift ρ] : AbsParser ρ → AbsParser ρ → AbsParser ρ := mapParser₂ orelseInfo orelseFn instance absParserHasOrelse {ρ : Type} [ParserFnLift ρ] : HasOrelse (AbsParser ρ) := ⟨orelse⟩ @[inline] def try {ρ : Type} [ParserFnLift ρ] : AbsParser ρ → AbsParser ρ := mapParser noFirstTokenInfo tryFn @[inline] def many {ρ : Type} [ParserFnLift ρ] : AbsParser ρ → AbsParser ρ := mapParser noFirstTokenInfo manyFn @[inline] def optional {ρ : Type} [ParserFnLift ρ] : AbsParser ρ → AbsParser ρ := mapParser noFirstTokenInfo optionalFn @[inline] def many1 {ρ : Type} [ParserFnLift ρ] (p : AbsParser ρ) : AbsParser ρ := andthen p (many p) @[inline] def sepBy {ρ : Type} [ParserFnLift ρ] (p sep : AbsParser ρ) (allowTrailingSep : Bool := false) : AbsParser ρ := mapParser₂ sepByInfo (sepByFn allowTrailingSep) p sep @[inline] def sepBy1 {ρ : Type} [ParserFnLift ρ] (p sep : AbsParser ρ) (allowTrailingSep : Bool := false) : AbsParser ρ := mapParser₂ sepBy1Info (sepBy1Fn allowTrailingSep) p sep def longestMatchInfo {ρ : Type} (ps : List (AbsParser ρ)) : ParserInfo := { updateTokens := λ trie, ps.foldl (λ trie p, p.info.updateTokens trie) trie, firstTokens := ps.foldl (λ tks p, p.info.firstTokens ++ tks) [] } def liftLongestMatchFn {ρ : Type} [ParserFnLift ρ] : List (AbsParser ρ) → ρ | [] := ParserFnLift.lift (longestMatchFn []) | [p] := ParserFnLift.map longestMatchFn₁ p.fn | [p, q] := ParserFnLift.map₂ longestMatchFn₂ p.fn q.fn | ps := ParserFnLift.mapList longestMatchFn (ps.map (λ p, p.fn)) @[inline] def longestMatch {ρ : Type} [ParserFnLift ρ] (ps : List (AbsParser ρ)) : AbsParser ρ := { info := longestMatchInfo ps, fn := liftLongestMatchFn ps } abbrev BasicParserFn : Type := EnvParserFn ParserConfig ParserFn abbrev BasicParser : Type := AbsParser BasicParserFn abbrev CmdParserFn (ρ : Type) : Type := EnvParserFn ρ (RecParserFn Unit ParserFn) abbrev TermParserFn : Type := RecParserFn Nat (CmdParserFn ParserConfig) abbrev TermParser : Type := AbsParser TermParserFn abbrev TrailingTermParserFn : Type := EnvParserFn Syntax TermParserFn abbrev TrailingTermParser : Type := AbsParser TrailingTermParserFn structure TermParsingTables := (leadingTermParsers : TokenMap TermParserFn) (trailingTermParsers : TokenMap TrailingTermParserFn) -- local Term parsers (such as from `local notation`) hide previous parsers instead of overloading them (localLeadingTermParsers : TokenMap TermParserFn := RBMap.empty) (localTrailingTermParsers : TokenMap TrailingTermParserFn := RBMap.empty) structure CommandParserConfig extends ParserConfig := (pTables : TermParsingTables) abbrev CommandParserFn : Type := CmdParserFn CommandParserConfig abbrev CommandParser : Type := AbsParser CommandParserFn @[inline] def Term.parser (rbp : Nat := 0) : TermParser := { fn := RecParserFn.recurse rbp } @[inline] def Command.parser : CommandParser := { fn := λ _, RecParserFn.recurse () } @[inline] def basicParser2TermParser (p : BasicParser) : TermParser := { info := p.info, fn := λ _ cfg _, p.fn cfg } instance basic2term : HasCoe BasicParser TermParser := ⟨basicParser2TermParser⟩ @[inline] def basicParser2CmdParser (p : BasicParser) : CommandParser := { info := p.info, fn := λ cfg _, p.fn cfg.toParserConfig } instance basicmd : HasCoe BasicParser CommandParser := ⟨basicParser2CmdParser⟩ private def tokenFnAux : BasicParserFn | cfg s d := let i := d.pos in let c := s.get i in if c == '\"' then strLitFnAux i s (d.next s i) else if c.isDigit then numberFnAux s d else let (_, tk) := cfg.tokens.matchPrefix s i in identFnAux i tk Name.anonymous s d private def updateCache (startPos : Nat) (d : ParserData) : ParserData := match d with | ⟨stack, pos, cache, none⟩ := if stack.size == 0 then d else let tk := stack.back in ⟨stack, pos, { tokenCache := some { startPos := startPos, stopPos := pos, token := tk } }, none⟩ | other := other def tokenFn : BasicParserFn | cfg s d := let i := d.pos in if s.atEnd i then d.mkEOIError else match d.cache with | { tokenCache := some tkc } := if tkc.startPos == i then let d := d.pushSyntax tkc.token in d.setPos tkc.stopPos else let d := tokenFnAux cfg s d in updateCache i d | _ := let d := tokenFnAux cfg s d in updateCache i d @[inline] def satisfySymbolFn (p : String → Bool) (errorMsg : String) : BasicParserFn | cfg s d := let startPos := d.pos in let d := tokenFn cfg s d in if d.hasError then d.mkErrorAt errorMsg startPos else match d.stxStack.back with | Syntax.atom _ sym := if p sym then d else d.mkErrorAt errorMsg startPos | _ := d.mkErrorAt errorMsg startPos def symbolFnAux (sym : String) (errorMsg : String) : BasicParserFn := satisfySymbolFn (== sym) errorMsg @[inline] def symbolFn (sym : String) : BasicParserFn := symbolFnAux sym ("expected '" ++ sym ++ "'") def symbolInfo (sym : String) (lbp : Nat) : ParserInfo := { updateTokens := λ trie, trie.insert sym { val := sym, lbp := lbp }, firstTokens := [ { val := sym, lbp := lbp } ] } @[inline] def symbol (sym : String) (lbp : Nat := 0) : BasicParser := { info := symbolInfo sym lbp, fn := symbolFn sym } def unicodeSymbolFnAux (sym asciiSym : String) (errorMsg : String) : BasicParserFn := satisfySymbolFn (λ s, s == sym || s == asciiSym) errorMsg @[inline] def unicodeSymbolFn (sym asciiSym : String) : BasicParserFn := unicodeSymbolFnAux sym asciiSym ("expected '" ++ sym ++ "' or '" ++ asciiSym ++ "'") def unicodeSymbolInfo (sym asciiSym : String) (lbp : Nat) : ParserInfo := { updateTokens := λ trie, let trie := trie.insert sym { val := sym, lbp := lbp } in trie.insert sym { val := asciiSym, lbp := lbp }, firstTokens := [ { val := sym, lbp := lbp }, { val := asciiSym, lbp := lbp } ] } @[inline] def unicodeSymbol (sym asciiSym : String) (lbp : Nat := 0) : BasicParser := { info := unicodeSymbolInfo sym asciiSym lbp, fn := unicodeSymbolFn sym asciiSym } def numberFn : BasicParserFn | cfg s d := let d := tokenFn cfg s d in if d.hasError || !(d.stxStack.back.isOfKind numberKind) then d.mkError "expected numeral" else d @[inline] def number : BasicParser := { fn := numberFn } def strLitFn : BasicParserFn | cfg s d := let d := tokenFn cfg s d in if d.hasError || !(d.stxStack.back.isOfKind strLitKind) then d.mkError "expected string literal" else d @[inline] def strLit : BasicParser := { fn := numberFn } def identFn : BasicParserFn | cfg s d := let d := tokenFn cfg s d in if d.hasError || !(d.stxStack.back.isIdent) then d.mkError "expected identifier" else d @[inline] def ident : BasicParser := { fn := identFn } instance string2basic : HasCoe String BasicParser := ⟨symbol⟩ def mkFrontendConfig (filename input : String) : FrontendConfig := { filename := filename, input := input, fileMap := input.toFileMap } def BasicParser.run (p : BasicParser) (input : String) (filename : String := "<input>") : Except String Syntax := let frontendCfg := mkFrontendConfig filename input in let tokens := p.info.updateTokens {} in let cfg : ParserConfig := { tokens := tokens, .. frontendCfg } in let d : ParserData := { stxStack := Array.empty, pos := 0, cache := {}, errorMsg := none } in let d := p.fn cfg input d in if d.hasError then Except.error (d.toErrorMsg cfg) else Except.ok d.stxStack.back -- Helper function for testing (non-recursive) term parsers def TermParser.test (p : TermParser) (input : String) (filename : String := "<input>") : Except String Syntax := let frontendCfg := mkFrontendConfig filename input in let tokens := p.info.updateTokens {} in let cfg : ParserConfig := { tokens := tokens, .. frontendCfg } in let d : ParserData := { stxStack := Array.empty, pos := 0, cache := {}, errorMsg := none } in let dummyCmdParser : Unit → ParserFn := λ _ _ d, d.mkError "no command parser" in let dummyTermParser : ℕ → CmdParserFn ParserConfig := λ _ _ _ _ d, d.mkError "no term parser" in let d := p.fn dummyTermParser cfg dummyCmdParser input d in if d.hasError then Except.error (d.toErrorMsg cfg) else Except.ok d.stxStack.back -- Stopped here @[noinline] def termPrattParser (tbl : TermParsingTables) (rbp : Nat) : TermParserFn := λ g, g 0 -- TODO(Leo) @[specialize] def TermParserFn.run (p : TermParserFn) : CommandParserFn | cfg r s d := let p := RecParserFn.run p (termPrattParser cfg.pTables) in p cfg.toParserConfig r s d def parseExpr (rbp : Nat) : CommandParserFn := TermParserFn.run (RecParserFn.recurse rbp) end Parser -- end Lean
f6dee165177ee0045a24137e6a16c23a6caf016f
bb31430994044506fa42fd667e2d556327e18dfe
/src/data/nat/modeq.lean
e3340f3d69167814c7ad2352946e08a242be8c13
[ "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
18,379
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.int.gcd import tactic.abel /-! # Congruences modulo a natural number > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the equivalence relation `a ≡ b [MOD n]` on the natural numbers, and proves basic properties about it such as the Chinese Remainder Theorem `modeq_and_modeq_iff_modeq_mul`. ## Notations `a ≡ b [MOD n]` is notation for `nat.modeq n a b`, which is defined to mean `a % n = b % n`. ## Tags modeq, congruence, mod, MOD, modulo -/ namespace nat /-- Modular equality. `n.modeq a b`, or `a ≡ b [MOD n]`, means that `a - b` is a multiple of `n`. -/ @[derive decidable] def modeq (n a b : ℕ) := a % n = b % n notation a ` ≡ `:50 b ` [MOD `:50 n `]`:0 := modeq n a b variables {m n a b c d : ℕ} namespace modeq @[refl] protected theorem refl (a : ℕ) : a ≡ a [MOD n] := @rfl _ _ protected theorem rfl : a ≡ a [MOD n] := modeq.refl _ instance : is_refl _ (modeq n) := ⟨modeq.refl⟩ @[symm] protected theorem symm : a ≡ b [MOD n] → b ≡ a [MOD n] := eq.symm @[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := eq.trans protected theorem comm : a ≡ b [MOD n] ↔ b ≡ a [MOD n] := ⟨modeq.symm, modeq.symm⟩ end modeq theorem modeq_zero_iff_dvd : a ≡ 0 [MOD n] ↔ n ∣ a := by rw [modeq, zero_mod, dvd_iff_mod_eq_zero] lemma _root_.has_dvd.dvd.modeq_zero_nat (h : n ∣ a) : a ≡ 0 [MOD n] := modeq_zero_iff_dvd.2 h lemma _root_.has_dvd.dvd.zero_modeq_nat (h : n ∣ a) : 0 ≡ a [MOD n] := h.modeq_zero_nat.symm theorem modeq_iff_dvd : a ≡ b [MOD n] ↔ (n:ℤ) ∣ b - a := by rw [modeq, eq_comm, ← int.coe_nat_inj', int.coe_nat_mod, int.coe_nat_mod, int.mod_eq_mod_iff_mod_sub_eq_zero, int.dvd_iff_mod_eq_zero] alias modeq_iff_dvd ↔ modeq.dvd modeq_of_dvd /-- A variant of `modeq_iff_dvd` with `nat` divisibility -/ theorem modeq_iff_dvd' (h : a ≤ b) : a ≡ b [MOD n] ↔ n ∣ b - a := by rw [modeq_iff_dvd, ←int.coe_nat_dvd, int.coe_nat_sub h] theorem mod_modeq (a n) : a % n ≡ a [MOD n] := mod_mod _ _ namespace modeq protected theorem of_dvd (d : m ∣ n) (h : a ≡ b [MOD n]) : a ≡ b [MOD m] := modeq_of_dvd ((int.coe_nat_dvd.2 d).trans h.dvd) protected theorem mul_left' (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD (c * n)] := by unfold modeq at *; rw [mul_mod_mul_left, mul_mod_mul_left, h] protected theorem mul_left (c : ℕ) (h : a ≡ b [MOD n]) : c * a ≡ c * b [MOD n] := (h.mul_left' _ ).of_dvd (dvd_mul_left _ _) protected theorem mul_right' (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD (n * c)] := by rw [mul_comm a, mul_comm b, mul_comm n]; exact h.mul_left' c protected theorem mul_right (c : ℕ) (h : a ≡ b [MOD n]) : a * c ≡ b * c [MOD n] := by rw [mul_comm a, mul_comm b]; exact h.mul_left c protected theorem mul (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a * c ≡ b * d [MOD n] := (h₂.mul_left _ ).trans (h₁.mul_right _) protected theorem pow (m : ℕ) (h : a ≡ b [MOD n]) : a ^ m ≡ b ^ m [MOD n] := begin induction m with d hd, {refl}, rw [pow_succ, pow_succ], exact h.mul hd, end protected theorem add (h₁ : a ≡ b [MOD n]) (h₂ : c ≡ d [MOD n]) : a + c ≡ b + d [MOD n] := begin rw [modeq_iff_dvd, int.coe_nat_add, int.coe_nat_add, add_sub_add_comm], exact dvd_add h₁.dvd h₂.dvd, end protected theorem add_left (c : ℕ) (h : a ≡ b [MOD n]) : c + a ≡ c + b [MOD n] := modeq.rfl.add h protected theorem add_right (c : ℕ) (h : a ≡ b [MOD n]) : a + c ≡ b + c [MOD n] := h.add modeq.rfl protected theorem add_left_cancel (h₁ : a ≡ b [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : c ≡ d [MOD n] := begin simp only [modeq_iff_dvd, int.coe_nat_add] at *, rw add_sub_add_comm at h₂, convert _root_.dvd_sub h₂ h₁ using 1, rw add_sub_cancel', end protected theorem add_left_cancel' (c : ℕ) (h : c + a ≡ c + b [MOD n]) : a ≡ b [MOD n] := modeq.rfl.add_left_cancel h protected theorem add_right_cancel (h₁ : c ≡ d [MOD n]) (h₂ : a + c ≡ b + d [MOD n]) : a ≡ b [MOD n] := by { rw [add_comm a, add_comm b] at h₂, exact h₁.add_left_cancel h₂ } protected theorem add_right_cancel' (c : ℕ) (h : a + c ≡ b + c [MOD n]) : a ≡ b [MOD n] := modeq.rfl.add_right_cancel h /-- Cancel left multiplication on both sides of the `≡` and in the modulus. For cancelling left multiplication in the modulus, see `nat.modeq.of_mul_left`. -/ protected theorem mul_left_cancel' {a b c m : ℕ} (hc : c ≠ 0) : c * a ≡ c * b [MOD c * m] → a ≡ b [MOD m] := by simp [modeq_iff_dvd, ←mul_sub, mul_dvd_mul_iff_left (by simp [hc] : (c : ℤ) ≠ 0)] protected theorem mul_left_cancel_iff' {a b c m : ℕ} (hc : c ≠ 0) : c * a ≡ c * b [MOD c * m] ↔ a ≡ b [MOD m] := ⟨modeq.mul_left_cancel' hc, modeq.mul_left' _⟩ /-- Cancel right multiplication on both sides of the `≡` and in the modulus. For cancelling right multiplication in the modulus, see `nat.modeq.of_mul_right`. -/ protected theorem mul_right_cancel' {a b c m : ℕ} (hc : c ≠ 0) : a * c ≡ b * c [MOD m * c] → a ≡ b [MOD m] := by simp [modeq_iff_dvd, ←sub_mul, mul_dvd_mul_iff_right (by simp [hc] : (c : ℤ) ≠ 0)] protected theorem mul_right_cancel_iff' {a b c m : ℕ} (hc : c ≠ 0) : a * c ≡ b * c [MOD m * c] ↔ a ≡ b [MOD m] := ⟨modeq.mul_right_cancel' hc, modeq.mul_right' _⟩ /-- Cancel left multiplication in the modulus. For cancelling left multiplication on both sides of the `≡`, see `nat.modeq.mul_left_cancel'`. -/ theorem of_mul_left (m : ℕ) (h : a ≡ b [MOD m * n]) : a ≡ b [MOD n] := by { rw [modeq_iff_dvd] at *, exact (dvd_mul_left (n : ℤ) (m : ℤ)).trans h } /-- Cancel right multiplication in the modulus. For cancelling right multiplication on both sides of the `≡`, see `nat.modeq.mul_right_cancel'`. -/ theorem of_mul_right (m : ℕ) : a ≡ b [MOD n * m] → a ≡ b [MOD n] := mul_comm m n ▸ of_mul_left _ end modeq lemma modeq_sub (h : b ≤ a) : a ≡ b [MOD a - b] := (modeq_of_dvd $ by rw [int.coe_nat_sub h]).symm lemma modeq_one : a ≡ b [MOD 1] := modeq_of_dvd $ one_dvd _ @[simp] lemma modeq_zero_iff : a ≡ b [MOD 0] ↔ a = b := by rw [modeq, mod_zero, mod_zero] @[simp] lemma add_modeq_left : n + a ≡ a [MOD n] := by rw [modeq, add_mod_left] @[simp] lemma add_modeq_right : a + n ≡ a [MOD n] := by rw [modeq, add_mod_right] namespace modeq lemma le_of_lt_add (h1 : a ≡ b [MOD m]) (h2 : a < b + m) : a ≤ b := (le_total a b).elim id (λ h3, nat.le_of_sub_eq_zero (eq_zero_of_dvd_of_lt ((modeq_iff_dvd' h3).mp h1.symm) ((tsub_lt_iff_left h3).mpr h2))) lemma add_le_of_lt (h1 : a ≡ b [MOD m]) (h2 : a < b) : a + m ≤ b := le_of_lt_add (add_modeq_right.trans h1) (add_lt_add_right h2 m) lemma dvd_iff (h : a ≡ b [MOD m]) (hdm : d ∣ m) : d ∣ a ↔ d ∣ b := begin simp only [←modeq_zero_iff_dvd], replace h := h.of_dvd hdm, exact ⟨h.symm.trans, h.trans⟩, end lemma gcd_eq (h : a ≡ b [MOD m]) : gcd a m = gcd b m := begin have h1 := gcd_dvd_right a m, have h2 := gcd_dvd_right b m, exact dvd_antisymm (dvd_gcd ((h.dvd_iff h1).mp (gcd_dvd_left a m)) h1) (dvd_gcd ((h.dvd_iff h2).mpr (gcd_dvd_left b m)) h2), end lemma eq_of_abs_lt (h : a ≡ b [MOD m]) (h2 : |(b - a : ℤ)| < m) : a = b := begin apply int.coe_nat_inj, rw [eq_comm, ←sub_eq_zero], exact int.eq_zero_of_abs_lt_dvd (modeq_iff_dvd.mp h) h2, end lemma eq_of_lt_of_lt (h : a ≡ b [MOD m]) (ha : a < m) (hb : b < m) : a = b := h.eq_of_abs_lt $ abs_sub_lt_iff.2 ⟨(sub_le_self _ $ int.coe_nat_nonneg _).trans_lt $ cast_lt.2 hb, (sub_le_self _ $ int.coe_nat_nonneg _).trans_lt $ cast_lt.2 ha⟩ /-- To cancel a common factor `c` from a `modeq` we must divide the modulus `m` by `gcd m c` -/ lemma cancel_left_div_gcd (hm : 0 < m) (h : c * a ≡ c * b [MOD m]) : a ≡ b [MOD m / gcd m c] := begin let d := gcd m c, have hmd := gcd_dvd_left m c, have hcd := gcd_dvd_right m c, rw modeq_iff_dvd, refine int.dvd_of_dvd_mul_right_of_gcd_one _ _, show (m/d : ℤ) ∣ (c/d) * (b - a), { rw [mul_comm, ←int.mul_div_assoc (b - a) (int.coe_nat_dvd.mpr hcd), mul_comm], apply int.div_dvd_div (int.coe_nat_dvd.mpr hmd), rw mul_sub, exact modeq_iff_dvd.mp h }, show int.gcd (m/d) (c/d) = 1, { simp only [←int.coe_nat_div, int.coe_nat_gcd (m / d) (c / d), gcd_div hmd hcd, nat.div_self (gcd_pos_of_pos_left c hm)] }, end lemma cancel_right_div_gcd (hm : 0 < m) (h : a * c ≡ b * c [MOD m]) : a ≡ b [MOD m / gcd m c] := by { apply cancel_left_div_gcd hm, simpa [mul_comm] using h } lemma cancel_left_div_gcd' (hm : 0 < m) (hcd : c ≡ d [MOD m]) (h : c * a ≡ d * b [MOD m]) : a ≡ b [MOD m / gcd m c] := (h.trans (modeq.mul_right b hcd).symm).cancel_left_div_gcd hm lemma cancel_right_div_gcd' (hm : 0 < m) (hcd : c ≡ d [MOD m]) (h : a * c ≡ b * d [MOD m]) : a ≡ b [MOD m / gcd m c] := hcd.cancel_left_div_gcd' hm $ by simpa [mul_comm] using h /-- A common factor that's coprime with the modulus can be cancelled from a `modeq` -/ lemma cancel_left_of_coprime (hmc : gcd m c = 1) (h : c * a ≡ c * b [MOD m]) : a ≡ b [MOD m] := begin rcases m.eq_zero_or_pos with rfl | hm, { simp only [gcd_zero_left] at hmc, simp only [gcd_zero_left, hmc, one_mul, modeq_zero_iff] at h, subst h }, simpa [hmc] using h.cancel_left_div_gcd hm end /-- A common factor that's coprime with the modulus can be cancelled from a `modeq` -/ lemma cancel_right_of_coprime (hmc : gcd m c = 1) (h : a * c ≡ b * c [MOD m]) : a ≡ b [MOD m] := cancel_left_of_coprime hmc $ by simpa [mul_comm] using h end modeq /-- The natural number less than `lcm n m` congruent to `a` mod `n` and `b` mod `m` -/ def chinese_remainder' (h : a ≡ b [MOD gcd n m]) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} := if hn : n = 0 then ⟨a, begin rw [hn, gcd_zero_left] at h, split, refl, exact h end⟩ else if hm : m = 0 then ⟨b, begin rw [hm, gcd_zero_right] at h, split, exact h.symm, refl end⟩ else ⟨let (c, d) := xgcd n m in int.to_nat (((n * c * b + m * d * a) / gcd n m) % lcm n m), begin rw xgcd_val, dsimp [chinese_remainder'._match_1], rw [modeq_iff_dvd, modeq_iff_dvd, int.to_nat_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero.2 (lcm_ne_zero hn hm)))], have hnonzero : (gcd n m : ℤ) ≠ 0 := begin norm_cast, rw [nat.gcd_eq_zero_iff, not_and], exact λ _, hm, end, have hcoedvd : ∀ t, (gcd n m : ℤ) ∣ t * (b - a) := λ t, h.dvd.mul_left _, have := gcd_eq_gcd_ab n m, split; rw [int.mod_def, ← sub_add]; refine dvd_add _ (dvd_mul_of_dvd_left _ _); try {norm_cast}, { rw ← sub_eq_iff_eq_add' at this, rw [← this, sub_mul, ← add_sub_assoc, add_comm, add_sub_assoc, ← mul_sub, int.add_div_of_dvd_left, int.mul_div_cancel_left _ hnonzero, int.mul_div_assoc _ h.dvd, ← sub_sub, sub_self, zero_sub, dvd_neg, mul_assoc], exact dvd_mul_right _ _, norm_cast, exact dvd_mul_right _ _, }, { exact dvd_lcm_left n m, }, { rw ← sub_eq_iff_eq_add at this, rw [← this, sub_mul, sub_add, ← mul_sub, int.sub_div_of_dvd, int.mul_div_cancel_left _ hnonzero, int.mul_div_assoc _ h.dvd, ← sub_add, sub_self, zero_add, mul_assoc], exact dvd_mul_right _ _, exact hcoedvd _ }, { exact dvd_lcm_right n m, }, end⟩ /-- The natural number less than `n*m` congruent to `a` mod `n` and `b` mod `m` -/ def chinese_remainder (co : coprime n m) (a b : ℕ) : {k // k ≡ a [MOD n] ∧ k ≡ b [MOD m]} := chinese_remainder' (by convert modeq_one) lemma chinese_remainder'_lt_lcm (h : a ≡ b [MOD gcd n m]) (hn : n ≠ 0) (hm : m ≠ 0) : ↑(chinese_remainder' h) < lcm n m := begin dsimp only [chinese_remainder'], rw [dif_neg hn, dif_neg hm, subtype.coe_mk, xgcd_val, ←int.to_nat_coe_nat (lcm n m)], have lcm_pos := int.coe_nat_pos.mpr (nat.pos_of_ne_zero (lcm_ne_zero hn hm)), exact (int.to_nat_lt_to_nat lcm_pos).mpr (int.mod_lt_of_pos _ lcm_pos), end lemma chinese_remainder_lt_mul (co : coprime n m) (a b : ℕ) (hn : n ≠ 0) (hm : m ≠ 0) : ↑(chinese_remainder co a b) < n * m := lt_of_lt_of_le (chinese_remainder'_lt_lcm _ hn hm) (le_of_eq co.lcm_eq_mul) lemma modeq_and_modeq_iff_modeq_mul {a b m n : ℕ} (hmn : coprime m n) : a ≡ b [MOD m] ∧ a ≡ b [MOD n] ↔ (a ≡ b [MOD m * n]) := ⟨λ h, begin rw [nat.modeq_iff_dvd, nat.modeq_iff_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd] at h, rw [nat.modeq_iff_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd], exact hmn.mul_dvd_of_dvd_of_dvd h.1 h.2 end, λ h, ⟨h.of_mul_right _, h.of_mul_left _⟩⟩ lemma coprime_of_mul_modeq_one (b : ℕ) {a n : ℕ} (h : a * b ≡ 1 [MOD n]) : coprime a n := begin obtain ⟨g, hh⟩ := nat.gcd_dvd_right a n, rw [nat.coprime_iff_gcd_eq_one, ← nat.dvd_one, ← nat.modeq_zero_iff_dvd], calc 1 ≡ a * b [MOD a.gcd n] : nat.modeq.of_mul_right g (hh.subst h).symm ... ≡ 0 * b [MOD a.gcd n] : (nat.modeq_zero_iff_dvd.mpr (nat.gcd_dvd_left _ _)).mul_right b ... = 0 : by rw zero_mul, end @[simp] lemma mod_mul_right_mod (a b c : ℕ) : a % (b * c) % b = a % b := (mod_modeq _ _).of_mul_right _ @[simp] lemma mod_mul_left_mod (a b c : ℕ) : a % (b * c) % c = a % c := (mod_modeq _ _).of_mul_left _ lemma div_mod_eq_mod_mul_div (a b c : ℕ) : a / b % c = a % (b * c) / b := if hb0 : b = 0 then by simp [hb0] else by rw [← @add_right_cancel_iff _ _ _ (c * (a / b / c)), mod_add_div, nat.div_div_eq_div_mul, ← mul_right_inj' hb0, ← @add_left_cancel_iff _ _ _ (a % b), mod_add_div, mul_add, ← @add_left_cancel_iff _ _ _ (a % (b * c) % b), add_left_comm, ← add_assoc (a % (b * c) % b), mod_add_div, ← mul_assoc, mod_add_div, mod_mul_right_mod] lemma add_mod_add_ite (a b c : ℕ) : (a + b) % c + (if c ≤ a % c + b % c then c else 0) = a % c + b % c := have (a + b) % c = (a % c + b % c) % c, from ((mod_modeq _ _).add $ mod_modeq _ _).symm, if hc0 : c = 0 then by simp [hc0] else begin rw this, split_ifs, { have h2 : (a % c + b % c) / c < 2, from nat.div_lt_of_lt_mul (by rw mul_two; exact add_lt_add (nat.mod_lt _ (nat.pos_of_ne_zero hc0)) (nat.mod_lt _ (nat.pos_of_ne_zero hc0))), have h0 : 0 < (a % c + b % c) / c, from nat.div_pos h (nat.pos_of_ne_zero hc0), rw [← @add_right_cancel_iff _ _ _ (c * ((a % c + b % c) / c)), add_comm _ c, add_assoc, mod_add_div, le_antisymm (le_of_lt_succ h2) h0, mul_one, add_comm] }, { rw [nat.mod_eq_of_lt (lt_of_not_ge h), add_zero] } end lemma add_mod_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) : (a + b) % c = a % c + b % c := by rw [← add_mod_add_ite, if_neg (not_le_of_lt hc), add_zero] lemma add_mod_add_of_le_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) : (a + b) % c + c = a % c + b % c := by rw [← add_mod_add_ite, if_pos hc] lemma add_div {a b c : ℕ} (hc0 : 0 < c) : (a + b) / c = a / c + b / c + if c ≤ a % c + b % c then 1 else 0 := begin rw [← mul_right_inj' hc0.ne', ← @add_left_cancel_iff _ _ _ ((a + b) % c + a % c + b % c)], suffices : (a + b) % c + c * ((a + b) / c) + a % c + b % c = a % c + c * (a / c) + (b % c + c * (b / c)) + c * (if c ≤ a % c + b % c then 1 else 0) + (a + b) % c, { simpa only [mul_add, add_comm, add_left_comm, add_assoc] }, rw [mod_add_div, mod_add_div, mod_add_div, mul_ite, add_assoc, add_assoc], conv_lhs { rw ← add_mod_add_ite }, simp, ac_refl end lemma add_div_eq_of_add_mod_lt {a b c : ℕ} (hc : a % c + b % c < c) : (a + b) / c = a / c + b / c := if hc0 : c = 0 then by simp [hc0] else by rw [add_div (nat.pos_of_ne_zero hc0), if_neg (not_le_of_lt hc), add_zero] protected lemma add_div_of_dvd_right {a b c : ℕ} (hca : c ∣ a) : (a + b) / c = a / c + b / c := if h : c = 0 then by simp [h] else add_div_eq_of_add_mod_lt begin rw [nat.mod_eq_zero_of_dvd hca, zero_add], exact nat.mod_lt _ (pos_iff_ne_zero.mpr h), end protected lemma add_div_of_dvd_left {a b c : ℕ} (hca : c ∣ b) : (a + b) / c = a / c + b / c := by rwa [add_comm, nat.add_div_of_dvd_right, add_comm] lemma add_div_eq_of_le_mod_add_mod {a b c : ℕ} (hc : c ≤ a % c + b % c) (hc0 : 0 < c) : (a + b) / c = a / c + b / c + 1 := by rw [add_div hc0, if_pos hc] lemma add_div_le_add_div (a b c : ℕ) : a / c + b / c ≤ (a + b) / c := if hc0 : c = 0 then by simp [hc0] else by rw [nat.add_div (nat.pos_of_ne_zero hc0)]; exact nat.le_add_right _ _ lemma le_mod_add_mod_of_dvd_add_of_not_dvd {a b c : ℕ} (h : c ∣ a + b) (ha : ¬ c ∣ a) : c ≤ a % c + b % c := by_contradiction $ λ hc, have (a + b) % c = a % c + b % c, from add_mod_of_add_mod_lt (lt_of_not_ge hc), by simp [dvd_iff_mod_eq_zero, *] at * lemma odd_mul_odd {n m : ℕ} : n % 2 = 1 → m % 2 = 1 → (n * m) % 2 = 1 := by simpa [nat.modeq] using @modeq.mul 2 n 1 m 1 lemma odd_mul_odd_div_two {m n : ℕ} (hm1 : m % 2 = 1) (hn1 : n % 2 = 1) : (m * n) / 2 = m * (n / 2) + m / 2 := have hm0 : 0 < m := nat.pos_of_ne_zero (λ h, by simp * at *), have hn0 : 0 < n := nat.pos_of_ne_zero (λ h, by simp * at *), mul_right_injective₀ two_ne_zero $ by rw [mul_add, two_mul_odd_div_two hm1, mul_left_comm, two_mul_odd_div_two hn1, two_mul_odd_div_two (nat.odd_mul_odd hm1 hn1), mul_tsub, mul_one, ← add_tsub_assoc_of_le (succ_le_of_lt hm0), tsub_add_cancel_of_le (le_mul_of_one_le_right (nat.zero_le _) hn0)] lemma odd_of_mod_four_eq_one {n : ℕ} : n % 4 = 1 → n % 2 = 1 := by simpa [modeq, show 2 * 2 = 4, by norm_num] using @modeq.of_mul_left 2 n 1 2 lemma odd_of_mod_four_eq_three {n : ℕ} : n % 4 = 3 → n % 2 = 1 := by simpa [modeq, show 2 * 2 = 4, by norm_num, show 3 % 4 = 3, by norm_num] using @modeq.of_mul_left 2 n 3 2 /-- A natural number is odd iff it has residue `1` or `3` mod `4`-/ lemma odd_mod_four_iff {n : ℕ} : n % 2 = 1 ↔ n % 4 = 1 ∨ n % 4 = 3 := have help : ∀ (m : ℕ), m < 4 → m % 2 = 1 → m = 1 ∨ m = 3 := dec_trivial, ⟨λ hn, help (n % 4) (mod_lt n (by norm_num)) $ (mod_mod_of_dvd n (by norm_num : 2 ∣ 4)).trans hn, λ h, or.dcases_on h odd_of_mod_four_eq_one odd_of_mod_four_eq_three⟩ end nat
c728ec994d91d255c59c9bbb86e0de396e7f11b5
d751a70f46ed26dc0111a87f5bbe83e5c6648904
/Code/src/inst/network/prec.lean
3e5bb32592adf8866b2949e5a0b225ed6fc160e1
[]
no_license
marcusrossel/bachelors-thesis
92cb12ae8436c10fbfab9bfe4929a0081e615b37
d1ec2c2b5c3c6700a506f2e3cc93f1160e44b422
refs/heads/main
1,682,873,547,703
1,619,795,735,000
1,619,795,735,000
306,041,494
2
0
null
null
null
null
UTF-8
Lean
false
false
9,997
lean
import lgraph import inst.network.graph open reactor.ports namespace inst namespace network -- An edge in a precedence graph connects reactions. structure prec.graph.edge := (src : reaction.id) (dst : reaction.id) -- Precedence graph edges are directed. instance prec.graph.lgraph_edge : lgraph.edge prec.graph.edge reaction.id := { src := (λ e, e.src), dst := (λ e, e.dst) } -- Cf. inst/primitives.lean variables (υ : Type*) [decidable_eq υ] -- A precedence graph is an L-graph of reactions, identified by reaction-IDs -- and connected by the edges define above. def prec.graph : Type* := lgraph reaction.id (reaction υ) prec.graph.edge variable {υ} namespace prec namespace graph -- A reaction is a member of a precedence graph if the graph contains an ID that maps to it. instance rcn_mem : has_mem (reaction υ) (prec.graph υ) := {mem := λ rcn ρ, ∃ i, ρ.data i = rcn} -- The reaction in a precedence graph, that is associated with a given reaction ID. noncomputable def rcn (ρ : prec.graph υ) (i : reaction.id) : reaction υ := ρ.data i -- A reaction `i` is internally dependent on `i'`, if they live in the same reactor and `i`'s -- priority is above `i'`'s. -- Note, this implies that reactions can have multiple internal dependencies, and hence a -- well-formed precedence graph will have an edge for each of these. This doesn't matter -- though, because the topological order is not affected by this. def internally_dependent (i i' : reaction.id) : Prop := (i.rtr = i'.rtr) ∧ (i.rcn > i'.rcn) -- A reaction `i` is externally dependent on `i'`, if there exists a connection from an -- output-dependency of `i` to an input-dependency of `i'` in the network graph. def externally_dependent (i i' : reaction.id) (η : inst.network.graph υ) : Prop := ∃ e : inst.network.graph.edge, (e ∈ η.edges) ∧ (e.src ∈ η.deps i role.output) ∧ (e.dst ∈ η.deps i' role.input) -- A well-formed precedence graph should contain edges between exactly those reactions that -- have a direct dependency in the corresponding network graph. def edges_are_well_formed_over (ρ : prec.graph υ) (η : inst.network.graph υ) : Prop := ∀ e : edge, e ∈ ρ.edges ↔ (internally_dependent e.src e.dst ∨ externally_dependent e.src e.dst η) -- A well-formed precedence graph should contain an ID (and by extension a member) iff -- the ID can be used to identify a reaction in the corresponding network graph. def ids_are_well_formed_over (ρ : prec.graph υ) (η : inst.network.graph υ) : Prop := ∀ i : reaction.id, i ∈ ρ.ids ↔ (i.rtr ∈ η.ids ∧ i.rcn ∈ (η.rtr i.rtr).priorities) -- A well-formed precedence graph's data map should return exactly those reactions that -- correspond to the given ID in the network graph. -- Originally this was formalized as: `∀ i, i ∈ ρ.ids → ρ.rcn i = η.rcn i` -- A consequence of stating it this way, is that precedence graphs may differ in the data that -- they associated with irrelevant (`∉ ρ.ids`) reaction-IDs. This then makes it impossible to -- prove `wf_prec_graphs_are_eq`. -- It would be possible to adjust the theorem to state that all well-formed precedence graphs -- are equal, excluding their `data` maps on irrelevant IDs - but that would be more work than -- it's worth. def data_is_well_formed_over (ρ : prec.graph υ) (η : inst.network.graph υ) : Prop := ∀ i, ρ.rcn i = η.rcn i -- A precedence graph is well formed over a given network graph, if its ids, data and edges -- fulfill the properties above. def is_well_formed_over (ρ : prec.graph υ) (η : inst.network.graph υ) : Prop := ρ.ids_are_well_formed_over η ∧ ρ.data_is_well_formed_over η ∧ ρ.edges_are_well_formed_over η notation ρ `⋈` η := (ρ.is_well_formed_over η) -- Two reactions are independent in a precedence graph if there are no paths between them. def indep (ρ : prec.graph υ) (i i' : reaction.id) : Prop := ¬(i~ρ~>i') ∧ ¬(i'~ρ~>i) -- Independent reactions can not live in the same reactor. lemma indep_rcns_neq_rtrs {η : inst.network.graph υ} {ρ : prec.graph υ} (hw : ρ ⋈ η) {i i' : reaction.id} (hₙ : i ≠ i') (hᵢ : ρ.indep i i') : i.rtr ≠ i'.rtr := begin by_contradiction hc, simp at hc, by_cases hg : i.rcn > i'.rcn, { let e := {edge . src := i, dst := i'}, have hd : internally_dependent i i', from ⟨hc, hg⟩, have hₑ, from (hw.right.right e).mpr (or.inl hd), have h_c', from lgraph.has_path_from_to.direct ⟨e, hₑ, refl _⟩, have hᵢ', from hᵢ.left, contradiction }, { by_cases hg' : i.rcn = i'.rcn, { have hₑ : i = i', { ext, exact hc, exact hg' }, contradiction }, { simp at hg hg', let e := {edge . src := i', dst := i}, have h_d : internally_dependent i' i, from ⟨symm hc, nat.lt_of_le_and_ne hg hg'⟩, have hₑ, from (hw.right.right e).mpr (or.inl h_d), have h_c', from lgraph.has_path_from_to.direct ⟨e, hₑ, refl _⟩, have hᵢ', from hᵢ.right, contradiction } } end -- If a reaction `i'` does not depend on another reaction `i`, then it is neither internally nor externally dependent on `i`. lemma no_path_no_dep {η : inst.network.graph υ} {ρ : prec.graph υ} (h : ρ ⋈ η) {i i' : reaction.id} (hᵢ : ¬(i~ρ~>i')) : ¬(internally_dependent i i') ∧ ¬(externally_dependent i i' η) := begin let e := {edge . src := i, dst := i'}, unfold is_well_formed_over edges_are_well_formed_over at h, replace h := h.right.right e, have hₑ' : e ∉ ρ.edges, { by_contradiction, have, from lgraph.has_path_from_to.direct ⟨e, h, refl _⟩, contradiction }, replace h := (not_iff_not_of_iff h).mp hₑ', exact not_or_distrib.mp h end -- If there is no path from a reaction `i` to a reaction `i'`, then none of the edges coming out of `i`'s output dependencies have -- a dependency relationship with `i'`. -- This lemma is somewhat specific, as it is required for `inst.network.graph.run_reaction_comm`. lemma no_path_no_eₒ_dep {η : inst.network.graph υ} {ρ : prec.graph υ} (hw : ρ ⋈ η) {i i' : reaction.id} (hᵢ : ¬(i~ρ~>i')) (hₙ : i.rtr ≠ i'.rtr) {ps : finset port.id} (hₛ : ps ⊆ η.deps i role.output) : ∀ (p ∈ ps) (e : inst.network.graph.edge), (e ∈ η.eₒ p) → e.dst ∉ (η.deps i' role.input) ∧ (e.src ∉ η.deps i' role.output) := begin intros p hₚ e hₑ, rw graph.mem_eₒ at hₑ, simp only [(⊆)] at hₛ, split, { have hd, from (no_path_no_dep hw hᵢ).right, rw [externally_dependent, not_exists] at hd, replace hd := hd e, repeat { rw not_and_distrib at hd }, replace hd := hd.resolve_left (not_not_intro hₑ.left), rw ←hₑ.right at hₚ, exact hd.resolve_left (not_not_intro (hₛ hₚ)) }, { simp only [graph.deps, finset.mem_image, not_exists] at hₛ ⊢, obtain ⟨x, hₓ, hₚ⟩ := hₛ hₚ, intros y hy, rw [hₑ.right, ←hₚ], by_contradiction, injection h, replace h := eq.symm h_1, contradiction } end end graph end prec -- The proposition, that every well-formed precedence graph over a network is acyclic. def graph.is_prec_acyclic (η : inst.network.graph υ) : Prop := ∀ ρ : prec.graph υ, (ρ ⋈ η) → ρ.is_acyclic -- All precedence graphs that are wellformed over a fixed network graph are equal. theorem prec.wf_prec_graphs_are_eq {η : inst.network.graph υ} {ρ ρ' : prec.graph υ} (hw : ρ ⋈ η) (hw' : ρ' ⋈ η) : ρ = ρ' := begin ext x, exact iff.trans (hw.left x) (iff.symm (hw'.left x)), { funext, exact eq.trans (hw.right.left x) (eq.symm (hw'.right.left x)) }, { rw finset.ext_iff, intro x, exact iff.trans (hw.right.right x) (iff.symm (hw'.right.right x)) } end namespace graph open prec.graph -- Equivalent network graphs have the same well-formed precedence graphs. theorem equiv_wf_prec_inv {η η' : graph υ} {ρ : prec.graph υ} (h : η' ≈ η) (hw : ρ ⋈ η) : ρ ⋈ η' := begin unfold is_well_formed_over at hw ⊢, simp only [(≈)] at h, repeat { split }, { unfold ids_are_well_formed_over at hw ⊢, intro x, rw ←h.right.left at hw, simp only [symm (h.right.right x.rtr).left, hw.left x] }, { unfold data_is_well_formed_over rcn at hw ⊢, intro x, simp only [(h.right.right x.rtr).right, hw.right.left x] }, { unfold edges_are_well_formed_over externally_dependent at hw ⊢, intro x, simp only [h.left, deps, rcn, (h.right.right x.src.rtr).right, (h.right.right x.dst.rtr).right, hw.right.right x] } end -- Equivalent network graphs have the same precedence-acyclicity. theorem equiv_prec_acyc_inv {η η' : graph υ} (hq : η ≈ η') (hₚ : η.is_prec_acyclic) : η'.is_prec_acyclic := begin unfold is_prec_acyclic at hₚ ⊢, intros ρ hw, exact hₚ ρ (equiv_wf_prec_inv hq hw) end end graph end network end inst
6fd535efa47b8d9240d384e8b20dbdca6cd9fc4f
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/simple.lean
a332ce138e03fa78d371e8caf0d703a30c0992d6
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,321
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel, Scott Morrison -/ import category_theory.limits.shapes.zero import category_theory.limits.shapes.kernels import category_theory.abelian.basic /-! # Simple objects We define simple objects in any category with zero morphisms. A simple object is an object `Y` such that any monomorphism `f : X ⟶ Y` is either an isomorphism or zero (but not both). This is formalized as a `Prop` valued typeclass `simple X`. If a morphism `f` out of a simple object is nonzero and has a kernel, then that kernel is zero. (We state this as `kernel.ι f = 0`, but should add `kernel f ≅ 0`.) When the category is abelian, being simple is the same as being cosimple (although we do not state a separate typeclass for this). As a consequence, any nonzero epimorphism out of a simple object is an isomorphism, and any nonzero morphism into a simple object has trivial cokernel. -/ noncomputable theory open category_theory.limits namespace category_theory universes v u variables {C : Type u} [category.{v} C] section variables [has_zero_morphisms C] /-- An object is simple if monomorphisms into it are (exclusively) either isomorphisms or zero. -/ class simple (X : C) : Prop := (mono_is_iso_iff_nonzero : ∀ {Y : C} (f : Y ⟶ X) [mono f], is_iso f ↔ (f ≠ 0)) /-- A nonzero monomorphism to a simple object is an isomorphism. -/ lemma is_iso_of_mono_of_nonzero {X Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : f ≠ 0) : is_iso f := (simple.mono_is_iso_iff_nonzero f).mpr w lemma kernel_zero_of_nonzero_from_simple {X Y : C} [simple X] {f : X ⟶ Y} [has_kernel f] (w : f ≠ 0) : kernel.ι f = 0 := begin classical, by_contradiction h, haveI := is_iso_of_mono_of_nonzero h, exact w (eq_zero_of_epi_kernel f), end lemma mono_to_simple_zero_of_not_iso {X Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : is_iso f → false) : f = 0 := begin classical, by_contradiction h, apply w, exact is_iso_of_mono_of_nonzero h, end lemma id_nonzero (X : C) [simple.{v} X] : 𝟙 X ≠ 0 := (simple.mono_is_iso_iff_nonzero (𝟙 X)).mp (by apply_instance) instance (X : C) [simple.{v} X] : nontrivial (End X) := nontrivial_of_ne 1 0 (id_nonzero X) section variable [has_zero_object C] open_locale zero_object /-- We don't want the definition of 'simple' to include the zero object, so we check that here. -/ lemma zero_not_simple [simple (0 : C)] : false := (simple.mono_is_iso_iff_nonzero (0 : (0 : C) ⟶ (0 : C))).mp ⟨⟨0, by tidy⟩⟩ rfl end end -- We next make the dual arguments, but for this we must be in an abelian category. section abelian variables [abelian C] /-- In an abelian category, an object satisfying the dual of the definition of a simple object is simple. -/ lemma simple_of_cosimple (X : C) (h : ∀ {Z : C} (f : X ⟶ Z) [epi f], is_iso f ↔ (f ≠ 0)) : simple X := ⟨λ Y f I, begin classical, fsplit, { introsI, have hx := cokernel.π_of_epi f, by_contradiction h, substI h, exact (h _).mp (cokernel.π_of_zero _ _) hx }, { intro hf, suffices : epi f, { resetI, apply abelian.is_iso_of_mono_of_epi }, apply preadditive.epi_of_cokernel_zero, by_contradiction h', exact cokernel_not_iso_of_nonzero hf ((h _).mpr h') } end⟩ /-- A nonzero epimorphism from a simple object is an isomorphism. -/ lemma is_iso_of_epi_of_nonzero {X Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : f ≠ 0) : is_iso f := begin -- `f ≠ 0` means that `kernel.ι f` is not an iso, and hence zero, and hence `f` is a mono. haveI : mono f := preadditive.mono_of_kernel_zero (mono_to_simple_zero_of_not_iso (kernel_not_iso_of_nonzero w)), exact abelian.is_iso_of_mono_of_epi f, end lemma cokernel_zero_of_nonzero_to_simple {X Y : C} [simple Y] {f : X ⟶ Y} [has_cokernel f] (w : f ≠ 0) : cokernel.π f = 0 := begin classical, by_contradiction h, haveI := is_iso_of_epi_of_nonzero h, exact w (eq_zero_of_mono_cokernel f), end lemma epi_from_simple_zero_of_not_iso {X Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : is_iso f → false) : f = 0 := begin classical, by_contradiction h, apply w, exact is_iso_of_epi_of_nonzero h, end end abelian end category_theory
4fb326ab8ca935b2d2133245d31d50d6caeb7b93
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/forBodyResultTypeIssue.lean
f04dfdabdae1635b8d6b490fe79c2488722880c1
[ "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
746
lean
abbrev M := ExceptT String <| StateT Nat Id def f (xs : List Nat) : M Unit := do for x in xs do if x == 0 then throw "contains zero" #eval f [1, 2, 3] |>.run' 0 #eval f [1, 0, 3] |>.run' 0 theorem ex1 : (f [1, 2, 3] |>.run' 0) = Except.ok () := rfl theorem ex2 : (f [1, 0, 3] |>.run' 0) = Except.error "contains zero" := rfl universes u abbrev N := ExceptT (ULift.{u} String) Id def idM {α : Type u} (a : α) : N α := pure a def checkEq {α : Type u} [BEq α] [ToString α] (a b : α) : N PUnit := do unless a == b do throw (ULift.up s!"{a} is not equal to {b}") def g {α : Type u} [BEq α] [ToString α] (xs : List α) (a : α) : N PUnit := do for x in xs do let a ← idM a checkEq x a #eval g [1, (2:Nat), 3] 1 |>.run
6b539d695c66895e6b2a43936ca9d51c39361941
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Util/Trace.lean
96e97f9d624ed4511729f15e3a723c92d5d47556
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
5,478
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.Message import Lean.MonadEnv universe u namespace Lean open Std (PersistentArray) structure TraceElem where ref : Syntax msg : MessageData deriving Inhabited structure TraceState where enabled : Bool := true traces : PersistentArray TraceElem := {} deriving Inhabited namespace TraceState private def toFormat (traces : PersistentArray TraceElem) (sep : Format) : IO Format := traces.size.foldM (fun i r => do let curr ← (traces.get! i).msg.format pure $ if i > 0 then r ++ sep ++ curr else r ++ curr) Format.nil end TraceState 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] def printTraces {m} [Monad m] [MonadTrace m] [MonadLiftT IO m] : m Unit := do let traceState ← getTraceState traceState.traces.forM fun m => do let d ← m.msg.format IO.println d def resetTraceState {m} [MonadTrace m] : m Unit := modifyTraceState (fun _ => {}) private def checkTraceOptionAux (opts : Options) : Name → Bool | n@(Name.str p _ _) => opts.getBool n || (!opts.contains n && checkTraceOptionAux opts p) | _ => false def checkTraceOption (opts : Options) (cls : Name) : Bool := if opts.isEmpty then false else checkTraceOptionAux opts (`trace ++ cls) private def checkTraceOptionM [MonadOptions m] (cls : Name) : m Bool := do let opts ← getOptions pure $ checkTraceOption opts cls @[inline] def isTracingEnabledFor [MonadOptions m] (cls : Name) : m Bool := do let s ← getTraceState if !s.enabled then pure false else checkTraceOptionM cls @[inline] def enableTracing (b : Bool) : m Bool := do let s ← getTraceState let oldEnabled := s.enabled modifyTraceState fun s => { s with enabled := b } pure oldEnabled @[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 addNode (oldTraces : PersistentArray TraceElem) (cls : Name) (ref : Syntax) : m Unit := modifyTraces fun traces => if traces.isEmpty then oldTraces else let d := MessageData.tagged cls m!"[{cls}] {MessageData.node (traces.toArray.map fun elem => elem.msg)}" oldTraces.push { ref := ref, msg := d } private def getResetTraces : m (PersistentArray TraceElem) := do let oldTraces ← getTraces modifyTraces fun _ => {} pure oldTraces section variable [MonadRef m] [AddMessageContext m] [MonadOptions m] def addTrace (cls : Name) (msg : MessageData) : m Unit := do let ref ← getRef let msg ← addMessageContext msg modifyTraces fun traces => traces.push { ref := ref, msg := MessageData.tagged cls m!"[{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 @[inline] def traceCtx [MonadFinally m] (cls : Name) (ctx : m α) : m α := do let b ← isTracingEnabledFor cls if !b then let old ← enableTracing false try ctx finally enableTracing old else let ref ← getRef let oldCurrTraces ← getResetTraces try ctx finally addNode oldCurrTraces cls ref -- TODO: delete after fix old frontend def MonadTracer.trace (cls : Name) (msg : Unit → MessageData) : m Unit := Lean.trace cls msg end def registerTraceClass (traceClassName : Name) : IO Unit := registerOption (`trace ++ traceClassName) { group := "trace", defValue := false, descr := "enable/disable tracing for the given module and submodules" } macro "trace[" id:ident "]" s:(interpolatedStr(term) <|> term) : doElem => do let msg ← if s.getKind == interpolatedStrKind then `(m! $s) else `(($s : MessageData)) `(doElem| do let cls := $(quote id.getId) if (← Lean.isTracingEnabledFor cls) then Lean.addTrace cls $msg) private def withNestedTracesFinalizer [Monad m] [MonadTrace m] (ref : Syntax) (currTraces : PersistentArray TraceElem) : m Unit := do modifyTraces fun traces => if traces.size == 0 then currTraces else if traces.size == 1 && traces[0].msg.isNest then currTraces ++ traces -- No nest of nest else let d := traces.foldl (init := MessageData.nil) fun d elem => if d.isNil then elem.msg else m!"{d}\n{elem.msg}" currTraces.push { ref := ref, msg := MessageData.nestD d } @[inline] def withNestedTraces [Monad m] [MonadFinally m] [MonadTrace m] [MonadRef m] (x : m α) : m α := do let currTraces ← getTraces modifyTraces fun _ => {} let ref ← getRef try x finally withNestedTracesFinalizer ref currTraces end Lean
6e243d1504b7e721265aac7b9206fb8c07a51166
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/assert_exists.lean
3d120fe89243aad352bd9609b54334632cb522cd
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
5,438
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Scott Morrison -/ import tactic.core import tactic.lint.basic /-! # User commands for assert the (non-)existence of declaration or instances. These commands are used to enforce the independence of different parts of mathlib. ## Implementation notes This file provides two linters that verify that things we assert do not _yet_ exist do _eventually_ exist. This works by creating declarations of the form: * ``assert_not_exists._checked.<uniq> : name := `foo`` for `assert_not_exists foo` * `assert_no_instance._checked.<uniq> := t` for `assert_instance t` These declarations are then picked up by the linter and analyzed accordingly. The `_` in the `_checked` prefix should hide them from doc-gen. -/ section setup_tactic_parser open tactic /-- `assert_exists n` is a user command that asserts that a declaration named `n` exists in the current import scope. Be careful to use names (e.g. `rat`) rather than notations (e.g. `ℚ`). -/ @[user_command] meta def assert_exists (_ : parse $ tk "assert_exists") : lean.parser unit := do decl ← ident, d ← get_decl decl, return () /-- `assert_not_exists n` is a user command that asserts that a declaration named `n` *does not exist* in the current import scope. Be careful to use names (e.g. `rat`) rather than notations (e.g. `ℚ`). It may be used (sparingly!) in mathlib to enforce plans that certain files are independent of each other. If you encounter an error on an `assert_not_exists` command while developing mathlib, it is probably because you have introduced new import dependencies to a file. In this case, you should refactor your work (for example by creating new files rather than adding imports to existing files). You should *not* delete the `assert_not_exists` statement without careful discussion ahead of time. -/ @[user_command] meta def assert_not_exists (_ : parse $ tk "assert_not_exists") : lean.parser unit := do decl ← ident, ff ← succeeds (get_decl decl) | fail format!"Declaration {decl} is not allowed to exist in this file.", n ← tactic.mk_fresh_name, let marker := (`assert_not_exists._checked).append (decl.append n), add_decl (declaration.defn marker [] `(name) `(decl) default tt), pure () /-- A linter for checking that the declarations marked `assert_not_exists` eventually exist. -/ meta def assert_not_exists.linter : linter := { test := λ d, (do let n := d.to_name, tt ← pure ((`assert_not_exists._checked).is_prefix_of n) | pure none, declaration.defn _ _ `(name) val _ _ ← pure d, n ← tactic.eval_expr name val, tt ← succeeds (get_decl n) | pure (some (format!"`{n}` does not ever exist").to_string), pure none), auto_decls := tt, no_errors_found := "All `assert_not_exists` declarations eventually exist.", errors_found := "The following declarations used in `assert_not_exists` never exist; perhaps there is a typo.", is_fast := tt } /-- `assert_instance e` is a user command that asserts that an instance `e` is available in the current import scope. Example usage: ``` assert_instance semiring ℕ ``` -/ @[user_command] meta def assert_instance (_ : parse $ tk "assert_instance") : lean.parser unit := do q ← texpr, e ← i_to_expr q, mk_instance e, return () /-- `assert_no_instance e` is a user command that asserts that an instance `e` *is not available* in the current import scope. It may be used (sparingly!) in mathlib to enforce plans that certain files are independent of each other. If you encounter an error on an `assert_no_instance` command while developing mathlib, it is probably because you have introduced new import dependencies to a file. In this case, you should refactor your work (for example by creating new files rather than adding imports to existing files). You should *not* delete the `assert_no_instance` statement without careful discussion ahead of time. Example usage: ``` assert_no_instance linear_ordered_field ℚ ``` -/ @[user_command] meta def assert_no_instance (_ : parse $ tk "assert_no_instance") : lean.parser unit := do q ← texpr, e ← i_to_expr q, i ← try_core (mk_instance e), match i with | none := do n ← tactic.mk_fresh_name, e_str ← to_string <$> pp e, let marker := ((`assert_no_instance._checked).mk_string e_str).append n, et ← infer_type e, tt ← succeeds (get_decl marker) | add_decl (declaration.defn marker [] et e default tt), pure () | some i := (fail!"Instance `{i} : {e}` is not allowed to be found in this file." : tactic unit) end /-- A linter for checking that the declarations marked `assert_no_instance` eventually exist. -/ meta def assert_no_instance.linter : linter := { test := λ d, (do let n := d.to_name, tt ← pure ((`assert_no_instance._checked).is_prefix_of n) | pure none, declaration.defn _ _ _ val _ _ ← pure d, tt ← succeeds (tactic.mk_instance val) | (some ∘ format.to_string) <$> pformat!"No instance of `{val}`", pure none), auto_decls := tt, no_errors_found := "All `assert_no_instance` instances eventually exist.", errors_found := "The following typeclass instances used in `assert_no_instance` never exist; perhaps they " ++ "are missing?", is_fast := ff } end
919dceaaf6fa7a60e0a417b77be92b6d394b7298
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/number_theory/liouville/liouville_constant.lean
f641942477311bbcc71af443a4bb040527f204e4
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
8,813
lean
/- Copyright (c) 2020 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Jujian Zhang -/ import number_theory.liouville.basic /-! # Liouville constants This file contains a construction of a family of Liouville numbers, indexed by a natural number $m$. The most important property is that they are examples of transcendental real numbers. This fact is recorded in `liouville.is_transcendental`. More precisely, for a real number $m$, Liouville's constant is $$ \sum_{i=0}^\infty\frac{1}{m^{i!}}. $$ The series converges only for $1 < m$. However, there is no restriction on $m$, since, if the series does not converge, then the sum of the series is defined to be zero. We prove that, for $m \in \mathbb{N}$ satisfying $2 \le m$, Liouville's constant associated to $m$ is a transcendental number. Classically, the Liouville number for $m = 2$ is the one called ``Liouville's constant''. ## Implementation notes The indexing $m$ is eventually a natural number satisfying $2 ≤ m$. However, we prove the first few lemmas for $m \in \mathbb{R}$. -/ noncomputable theory open_locale nat big_operators open real finset namespace liouville /-- For a real number `m`, Liouville's constant is $$ \sum_{i=0}^\infty\frac{1}{m^{i!}}. $$ The series converges only for `1 < m`. However, there is no restriction on `m`, since, if the series does not converge, then the sum of the series is defined to be zero. -/ def liouville_number (m : ℝ) : ℝ := ∑' (i : ℕ), 1 / m ^ i! /-- `liouville_number_initial_terms` is the sum of the first `k + 1` terms of Liouville's constant, i.e. $$ \sum_{i=0}^k\frac{1}{m^{i!}}. $$ -/ def liouville_number_initial_terms (m : ℝ) (k : ℕ) : ℝ := ∑ i in range (k+1), 1 / m ^ i! /-- `liouville_number_tail` is the sum of the series of the terms in `liouville_number m` starting from `k+1`, i.e $$ \sum_{i=k+1}^\infty\frac{1}{m^{i!}}. $$ -/ def liouville_number_tail (m : ℝ) (k : ℕ) : ℝ := ∑' i, 1 / m ^ (i + (k+1))! lemma liouville_number_tail_pos {m : ℝ} (hm : 1 < m) (k : ℕ) : 0 < liouville_number_tail m k := -- replace `0` with the constantly zero series `∑ i : ℕ, 0` calc (0 : ℝ) = ∑' i : ℕ, 0 : tsum_zero.symm ... < liouville_number_tail m k : -- to show that a series with non-negative terms has strictly positive sum it suffices -- to prove that tsum_lt_tsum_of_nonneg -- 1. the terms of the zero series are indeed non-negative (λ _, rfl.le) -- 2. the terms of our series are non-negative (λ i, one_div_nonneg.mpr (pow_nonneg (zero_le_one.trans hm.le) _)) -- 3. one term of our series is strictly positive -- they all are, we use the first term (one_div_pos.mpr (pow_pos (zero_lt_one.trans hm) (0 + (k + 1))!)) $ -- 4. our series converges -- it does since it is the tail of a converging series, though -- this is not the argument here. summable_one_div_pow_of_le hm (λ i, trans le_self_add (nat.self_le_factorial _)) /-- Split the sum definining a Liouville number into the first `k` term and the rest. -/ lemma liouville_number_eq_initial_terms_add_tail {m : ℝ} (hm : 1 < m) (k : ℕ) : liouville_number m = liouville_number_initial_terms m k + liouville_number_tail m k := (sum_add_tsum_nat_add _ (summable_one_div_pow_of_le hm (λ i, i.self_le_factorial))).symm /-! We now prove two useful inequalities, before collecting everything together. -/ /-- Partial inequality, works with `m ∈ ℝ` satisfying `1 < m`. -/ lemma tsum_one_div_pow_factorial_lt (n : ℕ) {m : ℝ} (m1 : 1 < m) : ∑' (i : ℕ), 1 / m ^ (i + (n + 1))! < (1 - 1 / m)⁻¹ * (1 / m ^ (n + 1)!) := -- two useful inequalities have m0 : 0 < m := (zero_lt_one.trans m1), have mi : |1 / m| < 1 := (le_of_eq (abs_of_pos (one_div_pos.mpr m0))).trans_lt ((div_lt_one m0).mpr m1), calc (∑' i, 1 / m ^ (i + (n + 1))!) < ∑' i, 1 / m ^ (i + (n + 1)!) : -- to show the strict inequality between these series, we prove that: tsum_lt_tsum_of_nonneg -- 1. the first series has non-negative terms (λ b, one_div_nonneg.mpr (pow_nonneg m0.le _)) -- 2. the second series dominates the first (λ b, one_div_pow_le_one_div_pow_of_le m1.le (b.add_factorial_succ_le_factorial_add_succ n)) -- 3. the term with index `i = 2` of the first series is strictly smaller than -- the corresponding term of the second series (one_div_pow_strict_mono m1 (n.add_factorial_succ_lt_factorial_add_succ rfl.le)) -- 4. the second series is summable, since its terms grow quickly (summable_one_div_pow_of_le m1 (λ j, nat.le.intro rfl)) ... = ∑' i, (1 / m) ^ i * (1 / m ^ (n + 1)!) : -- split the sum in the exponent and massage by { congr, ext i, rw [pow_add, ← div_div_eq_div_mul, div_eq_mul_one_div, ← one_div_pow i] } -- factor the constant `(1 / m ^ (n + 1)!)` out of the series ... = (∑' i, (1 / m) ^ i) * (1 / m ^ (n + 1)!) : tsum_mul_right ... = (1 - 1 / m)⁻¹ * (1 / m ^ (n + 1)!) : -- the series if the geometric series mul_eq_mul_right_iff.mpr (or.inl (tsum_geometric_of_abs_lt_1 mi)) lemma aux_calc (n : ℕ) {m : ℝ} (hm : 2 ≤ m) : (1 - 1 / m)⁻¹ * (1 / m ^ (n + 1)!) ≤ 1 / (m ^ n!) ^ n := calc (1 - 1 / m)⁻¹ * (1 / m ^ (n + 1)!) ≤ 2 * (1 / m ^ (n + 1)!) : -- the second factors coincide (and are non-negative), -- the first factors, satisfy the inequality `sub_one_div_inv_le_two` mul_mono_nonneg (one_div_nonneg.mpr (pow_nonneg (zero_le_two.trans hm) _)) (sub_one_div_inv_le_two hm) ... = 2 / m ^ (n + 1)! : mul_one_div 2 _ ... = 2 / m ^ (n! * (n + 1)) : congr_arg ((/) 2) (congr_arg (pow m) (mul_comm _ _)) ... ≤ 1 / m ^ (n! * n) : begin -- [ NB: in this block, I do not follow the brace convention for subgoals -- I wait until -- I solve all extraneous goals at once with `exact pow_pos (zero_lt_two.trans_le hm) _`. ] -- Clear denominators and massage* apply (div_le_div_iff _ _).mpr, conv_rhs { rw [one_mul, mul_add, pow_add, mul_one, pow_mul, mul_comm, ← pow_mul] }, -- the second factors coincide, so we prove the inequality of the first factors* apply (mul_le_mul_right _).mpr, -- solve all the inequalities `0 < m ^ ??` any_goals { exact pow_pos (zero_lt_two.trans_le hm) _ }, -- `2 ≤ m ^ n!` is a consequence of monotonicity of exponentiation at `2 ≤ m`. exact trans (trans hm (pow_one _).symm.le) (pow_mono (one_le_two.trans hm) n.factorial_pos) end ... = 1 / (m ^ n!) ^ n : congr_arg ((/) 1) (pow_mul m n! n) /-! Starting from here, we specialize to the case in which `m` is a natural number. -/ /-- The sum of the `k` initial terms of the Liouville number to base `m` is a ratio of natural numbers where the denominator is `m ^ k!`. -/ lemma liouville_number_rat_initial_terms {m : ℕ} (hm : 0 < m) (k : ℕ) : ∃ p : ℕ, liouville_number_initial_terms m k = p / m ^ k! := begin induction k with k h, { exact ⟨1, by rw [liouville_number_initial_terms, range_one, sum_singleton, nat.cast_one]⟩ }, { rcases h with ⟨p_k, h_k⟩, use p_k * (m ^ ((k + 1)! - k!)) + 1, unfold liouville_number_initial_terms at h_k ⊢, rw [sum_range_succ, h_k, div_add_div, div_eq_div_iff, add_mul], { norm_cast, rw [add_mul, one_mul, nat.factorial_succ, show k.succ * k! - k! = (k.succ - 1) * k!, by rw [tsub_mul, one_mul], nat.succ_sub_one, add_mul, one_mul, pow_add], simp [mul_assoc] }, refine mul_ne_zero_iff.mpr ⟨_, _⟩, all_goals { exact pow_ne_zero _ (nat.cast_ne_zero.mpr hm.ne.symm) } } end theorem is_liouville {m : ℕ} (hm : 2 ≤ m) : liouville (liouville_number m) := begin -- two useful inequalities have mZ1 : 1 < (m : ℤ), { norm_cast, exact one_lt_two.trans_le hm }, have m1 : 1 < (m : ℝ), { norm_cast, exact one_lt_two.trans_le hm }, intro n, -- the first `n` terms sum to `p / m ^ k!` rcases liouville_number_rat_initial_terms (zero_lt_two.trans_le hm) n with ⟨p, hp⟩, refine ⟨p, m ^ n!, one_lt_pow mZ1 n.factorial_ne_zero, _⟩, push_cast, -- separate out the sum of the first `n` terms and the rest rw [liouville_number_eq_initial_terms_add_tail m1 n, ← hp, add_sub_cancel', abs_of_nonneg (liouville_number_tail_pos m1 _).le], exact ⟨((lt_add_iff_pos_right _).mpr (liouville_number_tail_pos m1 n)).ne.symm, (tsum_one_div_pow_factorial_lt n m1).trans_le (aux_calc _ (nat.cast_two.symm.le.trans (nat.cast_le.mpr hm)))⟩ end /- Placing this lemma outside of the `open/closed liouville`-namespace would allow to remove `_root_.`, at the cost of some other small weirdness. -/ lemma is_transcendental {m : ℕ} (hm : 2 ≤ m) : _root_.transcendental ℤ (liouville_number m) := transcendental (is_liouville hm) end liouville
76418a3a14724a10429010512d674a1da21e87ee
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/dep_parents.lean
185edbaef361356a36209afe8332bc6abc646ebd
[ "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
59
lean
class A := mk class B [A] := mk class C extends A, B := mk
d4deca59c5c8cf6c3c454a398453f97c234b4638
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/free_algebra.lean
ece73a7b421ac7baa37459b754460dd97076f84a
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
17,219
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Adam Topaz -/ import algebra.algebra.subalgebra import algebra.monoid_algebra.basic /-! # Free Algebras Given a commutative semiring `R`, and a type `X`, we construct the free unital, associative `R`-algebra on `X`. ## Notation 1. `free_algebra R X` is the free algebra itself. It is endowed with an `R`-algebra structure. 2. `free_algebra.ι R` is the function `X → free_algebra R X`. 3. Given a function `f : X → A` to an R-algebra `A`, `lift R f` is the lift of `f` to an `R`-algebra morphism `free_algebra R X → A`. ## Theorems 1. `ι_comp_lift` states that the composition `(lift R f) ∘ (ι R)` is identical to `f`. 2. `lift_unique` states that whenever an R-algebra morphism `g : free_algebra R X → A` is given whose composition with `ι R` is `f`, then one has `g = lift R f`. 3. `hom_ext` is a variant of `lift_unique` in the form of an extensionality theorem. 4. `lift_comp_ι` is a combination of `ι_comp_lift` and `lift_unique`. It states that the lift of the composition of an algebra morphism with `ι` is the algebra morphism itself. 5. `equiv_monoid_algebra_free_monoid : free_algebra R X ≃ₐ[R] monoid_algebra R (free_monoid X)` 6. An inductive principle `induction`. ## Implementation details We construct the free algebra on `X` as a quotient of an inductive type `free_algebra.pre` by an inductively defined relation `free_algebra.rel`. Explicitly, the construction involves three steps: 1. We construct an inductive type `free_algebra.pre R X`, the terms of which should be thought of as representatives for the elements of `free_algebra R X`. It is the free type with maps from `R` and `X`, and with two binary operations `add` and `mul`. 2. We construct an inductive relation `free_algebra.rel R X` on `free_algebra.pre R X`. This is the smallest relation for which the quotient is an `R`-algebra where addition resp. multiplication are induced by `add` resp. `mul` from 1., and for which the map from `R` is the structure map for the algebra. 3. The free algebra `free_algebra R X` is the quotient of `free_algebra.pre R X` by the relation `free_algebra.rel R X`. -/ variables (R : Type*) [comm_semiring R] variables (X : Type*) namespace free_algebra /-- This inductive type is used to express representatives of the free algebra. -/ inductive pre | of : X → pre | of_scalar : R → pre | add : pre → pre → pre | mul : pre → pre → pre namespace pre instance : inhabited (pre R X) := ⟨of_scalar 0⟩ -- Note: These instances are only used to simplify the notation. /-- Coercion from `X` to `pre R X`. Note: Used for notation only. -/ def has_coe_generator : has_coe X (pre R X) := ⟨of⟩ /-- Coercion from `R` to `pre R X`. Note: Used for notation only. -/ def has_coe_semiring : has_coe R (pre R X) := ⟨of_scalar⟩ /-- Multiplication in `pre R X` defined as `pre.mul`. Note: Used for notation only. -/ def has_mul : has_mul (pre R X) := ⟨mul⟩ /-- Addition in `pre R X` defined as `pre.add`. Note: Used for notation only. -/ def has_add : has_add (pre R X) := ⟨add⟩ /-- Zero in `pre R X` defined as the image of `0` from `R`. Note: Used for notation only. -/ def has_zero : has_zero (pre R X) := ⟨of_scalar 0⟩ /-- One in `pre R X` defined as the image of `1` from `R`. Note: Used for notation only. -/ def has_one : has_one (pre R X) := ⟨of_scalar 1⟩ /-- Scalar multiplication defined as multiplication by the image of elements from `R`. Note: Used for notation only. -/ def has_scalar : has_scalar R (pre R X) := ⟨λ r m, mul (of_scalar r) m⟩ end pre local attribute [instance] pre.has_coe_generator pre.has_coe_semiring pre.has_mul pre.has_add pre.has_zero pre.has_one pre.has_scalar /-- Given a function from `X` to an `R`-algebra `A`, `lift_fun` provides a lift of `f` to a function from `pre R X` to `A`. This is mainly used in the construction of `free_algebra.lift`. -/ def lift_fun {A : Type*} [semiring A] [algebra R A] (f : X → A) : pre R X → A := λ t, pre.rec_on t f (algebra_map _ _) (λ _ _, (+)) (λ _ _, (*)) /-- An inductively defined relation on `pre R X` used to force the initial algebra structure on the associated quotient. -/ inductive rel : (pre R X) → (pre R X) → Prop -- force `of_scalar` to be a central semiring morphism | add_scalar {r s : R} : rel ↑(r + s) (↑r + ↑s) | mul_scalar {r s : R} : rel ↑(r * s) (↑r * ↑s) | central_scalar {r : R} {a : pre R X} : rel (r * a) (a * r) -- commutative additive semigroup | add_assoc {a b c : pre R X} : rel (a + b + c) (a + (b + c)) | add_comm {a b : pre R X} : rel (a + b) (b + a) | zero_add {a : pre R X} : rel (0 + a) a -- multiplicative monoid | mul_assoc {a b c : pre R X} : rel (a * b * c) (a * (b * c)) | one_mul {a : pre R X} : rel (1 * a) a | mul_one {a : pre R X} : rel (a * 1) a -- distributivity | left_distrib {a b c : pre R X} : rel (a * (b + c)) (a * b + a * c) | right_distrib {a b c : pre R X} : rel ((a + b) * c) (a * c + b * c) -- other relations needed for semiring | zero_mul {a : pre R X} : rel (0 * a) 0 | mul_zero {a : pre R X} : rel (a * 0) 0 -- compatibility | add_compat_left {a b c : pre R X} : rel a b → rel (a + c) (b + c) | add_compat_right {a b c : pre R X} : rel a b → rel (c + a) (c + b) | mul_compat_left {a b c : pre R X} : rel a b → rel (a * c) (b * c) | mul_compat_right {a b c : pre R X} : rel a b → rel (c * a) (c * b) end free_algebra /-- The free algebra for the type `X` over the commutative semiring `R`. -/ def free_algebra := quot (free_algebra.rel R X) namespace free_algebra local attribute [instance] pre.has_coe_generator pre.has_coe_semiring pre.has_mul pre.has_add pre.has_zero pre.has_one pre.has_scalar instance : semiring (free_algebra R X) := { add := quot.map₂ (+) (λ _ _ _, rel.add_compat_right) (λ _ _ _, rel.add_compat_left), add_assoc := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact quot.sound rel.add_assoc }, zero := quot.mk _ 0, zero_add := by { rintro ⟨⟩, exact quot.sound rel.zero_add }, add_zero := begin rintros ⟨⟩, change quot.mk _ _ = _, rw [quot.sound rel.add_comm, quot.sound rel.zero_add], end, add_comm := by { rintros ⟨⟩ ⟨⟩, exact quot.sound rel.add_comm }, mul := quot.map₂ (*) (λ _ _ _, rel.mul_compat_right) (λ _ _ _, rel.mul_compat_left), mul_assoc := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact quot.sound rel.mul_assoc }, one := quot.mk _ 1, one_mul := by { rintros ⟨⟩, exact quot.sound rel.one_mul }, mul_one := by { rintros ⟨⟩, exact quot.sound rel.mul_one }, left_distrib := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact quot.sound rel.left_distrib }, right_distrib := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact quot.sound rel.right_distrib }, zero_mul := by { rintros ⟨⟩, exact quot.sound rel.zero_mul }, mul_zero := by { rintros ⟨⟩, exact quot.sound rel.mul_zero } } instance : inhabited (free_algebra R X) := ⟨0⟩ instance : has_scalar R (free_algebra R X) := { smul := λ r, quot.map ((*) ↑r) (λ a b, rel.mul_compat_right) } instance : algebra R (free_algebra R X) := { to_fun := λ r, quot.mk _ r, map_one' := rfl, map_mul' := λ _ _, quot.sound rel.mul_scalar, map_zero' := rfl, map_add' := λ _ _, quot.sound rel.add_scalar, commutes' := λ _, by { rintros ⟨⟩, exact quot.sound rel.central_scalar }, smul_def' := λ _ _, rfl } instance {S : Type*} [comm_ring S] : ring (free_algebra S X) := algebra.semiring_to_ring S variables {X} /-- The canonical function `X → free_algebra R X`. -/ def ι : X → free_algebra R X := λ m, quot.mk _ m @[simp] lemma quot_mk_eq_ι (m : X) : quot.mk (free_algebra.rel R X) m = ι R m := rfl variables {A : Type*} [semiring A] [algebra R A] /-- Internal definition used to define `lift` -/ private def lift_aux (f : X → A) : (free_algebra R X →ₐ[R] A) := { to_fun := λ a, quot.lift_on a (lift_fun _ _ f) $ λ a b h, begin induction h, { exact (algebra_map R A).map_add h_r h_s, }, { exact (algebra_map R A).map_mul h_r h_s }, { apply algebra.commutes }, { change _ + _ + _ = _ + (_ + _), rw add_assoc }, { change _ + _ = _ + _, rw add_comm, }, { change (algebra_map _ _ _) + lift_fun R X f _ = lift_fun R X f _, simp, }, { change _ * _ * _ = _ * (_ * _), rw mul_assoc }, { change (algebra_map _ _ _) * lift_fun R X f _ = lift_fun R X f _, simp, }, { change lift_fun R X f _ * (algebra_map _ _ _) = lift_fun R X f _, simp, }, { change _ * (_ + _) = _ * _ + _ * _, rw left_distrib, }, { change (_ + _) * _ = _ * _ + _ * _, rw right_distrib, }, { change (algebra_map _ _ _) * _ = algebra_map _ _ _, simp }, { change _ * (algebra_map _ _ _) = algebra_map _ _ _, simp }, repeat { change lift_fun R X f _ + lift_fun R X f _ = _, rw h_ih, refl, }, repeat { change lift_fun R X f _ * lift_fun R X f _ = _, rw h_ih, refl, }, end, map_one' := by { change algebra_map _ _ _ = _, simp }, map_mul' := by { rintros ⟨⟩ ⟨⟩, refl }, map_zero' := by { change algebra_map _ _ _ = _, simp }, map_add' := by { rintros ⟨⟩ ⟨⟩, refl }, commutes' := by tauto } /-- Given a function `f : X → A` where `A` is an `R`-algebra, `lift R f` is the unique lift of `f` to a morphism of `R`-algebras `free_algebra R X → A`. -/ def lift : (X → A) ≃ (free_algebra R X →ₐ[R] A) := { to_fun := lift_aux R, inv_fun := λ F, F ∘ (ι R), left_inv := λ f, by {ext, refl}, right_inv := λ F, by { ext x, rcases x, induction x, case pre.of : { change ((F : free_algebra R X → A) ∘ (ι R)) _ = _, refl }, case pre.of_scalar : { change algebra_map _ _ x = F (algebra_map _ _ x), rw alg_hom.commutes F x, }, case pre.add : a b ha hb { change lift_aux R (F ∘ ι R) (quot.mk _ _ + quot.mk _ _) = F (quot.mk _ _ + quot.mk _ _), rw [alg_hom.map_add, alg_hom.map_add, ha, hb], }, case pre.mul : a b ha hb { change lift_aux R (F ∘ ι R) (quot.mk _ _ * quot.mk _ _) = F (quot.mk _ _ * quot.mk _ _), rw [alg_hom.map_mul, alg_hom.map_mul, ha, hb], }, }, } @[simp] lemma lift_aux_eq (f : X → A) : lift_aux R f = lift R f := rfl @[simp] lemma lift_symm_apply (F : free_algebra R X →ₐ[R] A) : (lift R).symm F = F ∘ (ι R) := rfl variables {R X} @[simp] theorem ι_comp_lift (f : X → A) : (lift R f : free_algebra R X → A) ∘ (ι R) = f := by {ext, refl} @[simp] theorem lift_ι_apply (f : X → A) (x) : lift R f (ι R x) = f x := rfl @[simp] theorem lift_unique (f : X → A) (g : free_algebra R X →ₐ[R] A) : (g : free_algebra R X → A) ∘ (ι R) = f ↔ g = lift R f := (lift R).symm_apply_eq /-! At this stage we set the basic definitions as `@[irreducible]`, so from this point onwards one should only use the universal properties of the free algebra, and consider the actual implementation as a quotient of an inductive type as completely hidden. Of course, one still has the option to locally make these definitions `semireducible` if so desired, and Lean is still willing in some circumstances to do unification based on the underlying definition. -/ attribute [irreducible] ι lift -- Marking `free_algebra` irreducible makes `ring` instances inaccessible on quotients. -- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/algebra.2Esemiring_to_ring.20breaks.20semimodule.20typeclass.20lookup/near/212580241 -- For now, we avoid this by not marking it irreducible. @[simp] theorem lift_comp_ι (g : free_algebra R X →ₐ[R] A) : lift R ((g : free_algebra R X → A) ∘ (ι R)) = g := by { rw ←lift_symm_apply, exact (lift R).apply_symm_apply g } /-- See note [partially-applied ext lemmas]. -/ @[ext] theorem hom_ext {f g : free_algebra R X →ₐ[R] A} (w : ((f : free_algebra R X → A) ∘ (ι R)) = ((g : free_algebra R X → A) ∘ (ι R))) : f = g := begin rw [←lift_symm_apply, ←lift_symm_apply] at w, exact (lift R).symm.injective w, end /-- The free algebra on `X` is "just" the monoid algebra on the free monoid on `X`. This would be useful when constructing linear maps out of a free algebra, for example. -/ noncomputable def equiv_monoid_algebra_free_monoid : free_algebra R X ≃ₐ[R] monoid_algebra R (free_monoid X) := alg_equiv.of_alg_hom (lift R (λ x, (monoid_algebra.of R (free_monoid X)) (free_monoid.of x))) ((monoid_algebra.lift R (free_monoid X) (free_algebra R X)) (free_monoid.lift (ι R))) begin apply monoid_algebra.alg_hom_ext, intro x, apply free_monoid.rec_on x, { simp, refl, }, { intros x y ih, simp at ih, simp [ih], } end (by { ext, simp, }) instance [nontrivial R] : nontrivial (free_algebra R X) := equiv_monoid_algebra_free_monoid.surjective.nontrivial section open_locale classical /-- The left-inverse of `algebra_map`. -/ def algebra_map_inv : free_algebra R X →ₐ[R] R := lift R (0 : X → R) lemma algebra_map_left_inverse : function.left_inverse algebra_map_inv (algebra_map R $ free_algebra R X) := λ x, by simp [algebra_map_inv] @[simp] lemma algebra_map_inj (x y : R) : algebra_map R (free_algebra R X) x = algebra_map R (free_algebra R X) y ↔ x = y := algebra_map_left_inverse.injective.eq_iff @[simp] lemma algebra_map_eq_zero_iff (x : R) : algebra_map R (free_algebra R X) x = 0 ↔ x = 0 := map_eq_zero_iff (algebra_map _ _) algebra_map_left_inverse.injective @[simp] lemma algebra_map_eq_one_iff (x : R) : algebra_map R (free_algebra R X) x = 1 ↔ x = 1 := map_eq_one_iff (algebra_map _ _) algebra_map_left_inverse.injective -- this proof is copied from the approach in `free_abelian_group.of_injective` lemma ι_injective [nontrivial R] : function.injective (ι R : X → free_algebra R X) := λ x y hoxy, classical.by_contradiction $ assume hxy : x ≠ y, let f : free_algebra R X →ₐ[R] R := lift R (λ z, if x = z then (1 : R) else 0) in have hfx1 : f (ι R x) = 1, from (lift_ι_apply _ _).trans $ if_pos rfl, have hfy1 : f (ι R y) = 1, from hoxy ▸ hfx1, have hfy0 : f (ι R y) = 0, from (lift_ι_apply _ _).trans $ if_neg hxy, one_ne_zero $ hfy1.symm.trans hfy0 @[simp] lemma ι_inj [nontrivial R] (x y : X) : ι R x = ι R y ↔ x = y := ι_injective.eq_iff @[simp] lemma ι_ne_algebra_map [nontrivial R] (x : X) (r : R) : ι R x ≠ algebra_map R _ r := λ h, let f0 : free_algebra R X →ₐ[R] R := lift R 0 in let f1 : free_algebra R X →ₐ[R] R := lift R 1 in have hf0 : f0 (ι R x) = 0, from lift_ι_apply _ _, have hf1 : f1 (ι R x) = 1, from lift_ι_apply _ _, begin rw [h, f0.commutes, algebra.id.map_eq_self] at hf0, rw [h, f1.commutes, algebra.id.map_eq_self] at hf1, exact zero_ne_one (hf0.symm.trans hf1), end @[simp] lemma ι_ne_zero [nontrivial R] (x : X) : ι R x ≠ 0 := ι_ne_algebra_map x 0 @[simp] lemma ι_ne_one [nontrivial R] (x : X) : ι R x ≠ 1 := ι_ne_algebra_map x 1 end end free_algebra /- There is something weird in the above namespace that breaks the typeclass resolution of `has_coe_to_sort` below. Closing it and reopening it fixes it... -/ namespace free_algebra /-- An induction principle for the free algebra. If `C` holds for the `algebra_map` of `r : R` into `free_algebra R X`, the `ι` of `x : X`, and is preserved under addition and muliplication, then it holds for all of `free_algebra R X`. -/ @[elab_as_eliminator] lemma induction {C : free_algebra R X → Prop} (h_grade0 : ∀ r, C (algebra_map R (free_algebra R X) r)) (h_grade1 : ∀ x, C (ι R x)) (h_mul : ∀ a b, C a → C b → C (a * b)) (h_add : ∀ a b, C a → C b → C (a + b)) (a : free_algebra R X) : C a := begin -- the arguments are enough to construct a subalgebra, and a mapping into it from X let s : subalgebra R (free_algebra R X) := { carrier := C, mul_mem' := h_mul, add_mem' := h_add, algebra_map_mem' := h_grade0, }, let of : X → s := subtype.coind (ι R) h_grade1, -- the mapping through the subalgebra is the identity have of_id : alg_hom.id R (free_algebra R X) = s.val.comp (lift R of), { ext, simp [of, subtype.coind], }, -- finding a proof is finding an element of the subalgebra convert subtype.prop (lift R of a), simp [alg_hom.ext_iff] at of_id, exact of_id a, end /-- The star ring formed by reversing the elements of products -/ instance : star_ring (free_algebra R X) := { star := mul_opposite.unop ∘ lift R (mul_opposite.op ∘ ι R), star_involutive := λ x, by { unfold has_star.star, simp only [function.comp_apply], refine free_algebra.induction R X _ _ _ _ x; intros; simp [*] }, star_mul := λ a b, by simp, star_add := λ a b, by simp } @[simp] lemma star_ι (x : X) : star (ι R x) = (ι R x) := by simp [star, has_star.star] @[simp] lemma star_algebra_map (r : R) : star (algebra_map R (free_algebra R X) r) = (algebra_map R _ r) := by simp [star, has_star.star] /-- `star` as an `alg_equiv` -/ def star_hom : free_algebra R X ≃ₐ[R] (free_algebra R X)ᵐᵒᵖ := { commutes' := λ r, by simp [star_algebra_map], ..star_ring_equiv } end free_algebra
eda78dbf56b621fa68ef6820dfcb45ad8b40af1b
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Meta/Tactic/Simp/SimpAll.lean
cb33b50673dfa100a9574c9248a7606a40fd885b
[ "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
3,268
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.Util import Lean.Meta.Tactic.Simp.Main namespace Lean.Meta namespace SimpAll structure Entry where fvarId : FVarId -- original fvarId userName : Name id : Name -- id of the lemma at `SimpLemmas` type : Expr proof : Expr deriving Inhabited structure State where modified : Bool := false mvarId : MVarId entries : Array Entry := #[] ctx : Simp.Context abbrev M := StateRefT State MetaM private def initEntries : M Unit := do let hs ← getNondepPropHyps (← get).mvarId let erased := (← get).ctx.simpLemmas.erased for h in hs do let localDecl ← getLocalDecl h unless erased.contains localDecl.userName do let fvarId := localDecl.fvarId let proof := localDecl.toExpr let id ← mkFreshUserName `h let simpLemmas ← (← get).ctx.simpLemmas.add #[] proof (name? := id) let entry : Entry := { fvarId := fvarId, userName := localDecl.userName, id := id, type := (← instantiateMVars localDecl.type), proof := proof } modify fun s => { s with entries := s.entries.push entry, ctx.simpLemmas := simpLemmas } private abbrev getSimpLemmas : M SimpLemmas := return (← get).ctx.simpLemmas private partial def loop : M Bool := do modify fun s => { s with modified := false } -- simplify entries for i in [:(← get).entries.size] do let entry := (← get).entries[i] let ctx := (← get).ctx -- We disable the current entry to prevent it to be simplified to `True` let simpLemmasWithoutEntry ← (← getSimpLemmas).eraseCore entry.id let ctx := { ctx with simpLemmas := simpLemmasWithoutEntry } match (← simpStep (← get).mvarId entry.proof entry.type ctx) with | none => return true -- closed the goal | some (proofNew, typeNew) => unless typeNew == entry.type do let id ← mkFreshUserName `h let simpLemmasNew ← (← getSimpLemmas).add #[] proofNew (name? := id) modify fun s => { s with modified := true ctx.simpLemmas := simpLemmasNew entries[i] := { entry with type := typeNew, proof := proofNew, id := id } } -- simplify target let mvarId := (← get).mvarId match (← simpTarget mvarId (← get).ctx) with | none => return true | some mvarIdNew => unless mvarId == mvarIdNew do modify fun s => { s with modified := true mvarId := mvarIdNew } if (← get).modified then loop else return false def main : M (Option MVarId) := do initEntries if (← loop) then return none -- close the goal else let mvarId := (← get).mvarId let entries := (← get).entries let (_, mvarId) ← assertHypotheses mvarId (entries.map fun e => { userName := e.userName, type := e.type, value := e.proof }) tryClearMany mvarId (entries.map fun e => e.fvarId) end SimpAll def simpAll (mvarId : MVarId) (ctx : Simp.Context) : MetaM (Option MVarId) := do withMVarContext mvarId do SimpAll.main.run' { mvarId := mvarId, ctx := ctx } end Lean.Meta
dbdc2737bb94dae5eed2949638ae4d1de8f16deb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/pell.lean
a9f498d0bd0fd0d564aa1202ecef058ac3c15d7e
[ "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
29,481
lean
/- Copyright (c) 2023 Michael Stoll. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Geißer, Michael Stoll -/ import tactic.qify import data.zmod.basic import number_theory.diophantine_approximation import number_theory.zsqrtd.basic /-! # Pell's Equation > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. *Pell's Equation* is the equation $x^2 - d y^2 = 1$, where $d$ is a positive integer that is not a square, and one is interested in solutions in integers $x$ and $y$. In this file, we aim at providing all of the essential theory of Pell's Equation for general $d$ (as opposed to the contents of `number_theory.pell_matiyasevic`, which is specific to the case $d = a^2 - 1$ for some $a > 1$). We begin by defining a type `pell.solution₁ d` for solutions of the equation, show that it has a natural structure as an abelian group, and prove some basic properties. We then prove the following **Theorem.** Let $d$ be a positive integer that is not a square. Then the equation $x^2 - d y^2 = 1$ has a nontrivial (i.e., with $y \ne 0$) solution in integers. See `pell.exists_of_not_is_square` and `pell.solution₁.exists_nontrivial_of_not_is_square`. We then define the *fundamental solution* to be the solution with smallest $x$ among all solutions satisfying $x > 1$ and $y > 0$. We show that every solution is a power (in the sense of the group structure mentioned above) of the fundamental solution up to a (common) sign, see `pell.is_fundamental.eq_zpow_or_neg_zpow`, and that a (positive) solution has this property if and only if it is fundamental, see `pell.pos_generator_iff_fundamental`. ## References * [K. Ireland, M. Rosen, *A classical introduction to modern number theory* (Section 17.5)][IrelandRosen1990] ## Tags Pell's equation ## TODO * Extend to `x ^ 2 - d * y ^ 2 = -1` and further generalizations. * Connect solutions to the continued fraction expansion of `√d`. -/ namespace pell /-! ### Group structure of the solution set We define a structure of a commutative multiplicative group with distributive negation on the set of all solutions to the Pell equation `x^2 - d*y^2 = 1`. The type of such solutions is `pell.solution₁ d`. It corresponds to a pair of integers `x` and `y` and a proof that `(x, y)` is indeed a solution. The multiplication is given by `(x, y) * (x', y') = (x*y' + d*y*y', x*y' + y*x')`. This is obtained by mapping `(x, y)` to `x + y*√d` and multiplying the results. In fact, we define `pell.solution₁ d` to be `↥(unitary (ℤ√d))` and transport the "commutative group with distributive negation" structure from `↥(unitary (ℤ√d))`. We then set up an API for `pell.solution₁ d`. -/ open zsqrtd /-- An element of `ℤ√d` has norm one (i.e., `a.re^2 - d*a.im^2 = 1`) if and only if it is contained in the submonoid of unitary elements. TODO: merge this result with `pell.is_pell_iff_mem_unitary`. -/ lemma is_pell_solution_iff_mem_unitary {d : ℤ} {a : ℤ√d} : a.re ^ 2 - d * a.im ^ 2 = 1 ↔ a ∈ unitary ℤ√d := by rw [← norm_eq_one_iff_mem_unitary, norm_def, sq, sq, ← mul_assoc] -- We use `solution₁ d` to allow for a more general structure `solution d m` that -- encodes solutions to `x^2 - d*y^2 = m` to be added later. /-- `pell.solution₁ d` is the type of solutions to the Pell equation `x^2 - d*y^2 = 1`. We define this in terms of elements of `ℤ√d` of norm one. -/ @[derive [comm_group, has_distrib_neg, inhabited]] def solution₁ (d : ℤ) : Type := ↥(unitary ℤ√d) namespace solution₁ variables {d : ℤ} instance : has_coe (solution₁ d) ℤ√d := { coe := subtype.val } /-- The `x` component of a solution to the Pell equation `x^2 - d*y^2 = 1` -/ protected def x (a : solution₁ d) : ℤ := (a : ℤ√d).re /-- The `y` component of a solution to the Pell equation `x^2 - d*y^2 = 1` -/ protected def y (a : solution₁ d) : ℤ := (a : ℤ√d).im /-- The proof that `a` is a solution to the Pell equation `x^2 - d*y^2 = 1` -/ lemma prop (a : solution₁ d) : a.x ^ 2 - d * a.y ^ 2 = 1 := is_pell_solution_iff_mem_unitary.mpr a.property /-- An alternative form of the equation, suitable for rewriting `x^2`. -/ lemma prop_x (a : solution₁ d) : a.x ^ 2 = 1 + d * a.y ^ 2 := by {rw ← a.prop, ring} /-- An alternative form of the equation, suitable for rewriting `d * y^2`. -/ lemma prop_y (a : solution₁ d) : d * a.y ^ 2 = a.x ^ 2 - 1 := by {rw ← a.prop, ring} /-- Two solutions are equal if their `x` and `y` components are equal. -/ @[ext] lemma ext {a b : solution₁ d} (hx : a.x = b.x) (hy : a.y = b.y) : a = b := subtype.ext $ ext.mpr ⟨hx, hy⟩ /-- Construct a solution from `x`, `y` and a proof that the equation is satisfied. -/ def mk (x y : ℤ) (prop : x ^ 2 - d * y ^ 2 = 1) : solution₁ d := { val := ⟨x, y⟩, property := is_pell_solution_iff_mem_unitary.mp prop } @[simp] lemma x_mk (x y : ℤ) (prop : x ^ 2 - d * y ^ 2 = 1) : (mk x y prop).x = x := rfl @[simp] lemma y_mk (x y : ℤ) (prop : x ^ 2 - d * y ^ 2 = 1) : (mk x y prop).y = y := rfl @[simp] lemma coe_mk (x y : ℤ) (prop : x ^ 2 - d * y ^ 2 = 1) : (↑(mk x y prop) : ℤ√d) = ⟨x,y⟩ := zsqrtd.ext.mpr ⟨x_mk x y prop, y_mk x y prop⟩ @[simp] lemma x_one : (1 : solution₁ d).x = 1 := rfl @[simp] lemma y_one : (1 : solution₁ d).y = 0 := rfl @[simp] lemma x_mul (a b : solution₁ d) : (a * b).x = a.x * b.x + d * (a.y * b.y) := by {rw ← mul_assoc, refl} @[simp] lemma y_mul (a b : solution₁ d) : (a * b).y = a.x * b.y + a.y * b.x := rfl @[simp] lemma x_inv (a : solution₁ d) : a⁻¹.x = a.x := rfl @[simp] lemma y_inv (a : solution₁ d) : a⁻¹.y = -a.y := rfl @[simp] lemma x_neg (a : solution₁ d) : (-a).x = -a.x := rfl @[simp] lemma y_neg (a : solution₁ d) : (-a).y = -a.y := rfl /-- When `d` is negative, then `x` or `y` must be zero in a solution. -/ lemma eq_zero_of_d_neg (h₀ : d < 0) (a : solution₁ d) : a.x = 0 ∨ a.y = 0 := begin have h := a.prop, contrapose! h, have h1 := sq_pos_of_ne_zero a.x h.1, have h2 := sq_pos_of_ne_zero a.y h.2, nlinarith, end /-- A solution has `x ≠ 0`. -/ lemma x_ne_zero (h₀ : 0 ≤ d) (a : solution₁ d) : a.x ≠ 0 := begin intro hx, have h : 0 ≤ d * a.y ^ 2 := mul_nonneg h₀ (sq_nonneg _), rw [a.prop_y, hx, sq, zero_mul, zero_sub] at h, exact not_le.mpr (neg_one_lt_zero : (-1 : ℤ) < 0) h, end /-- A solution with `x > 1` must have `y ≠ 0`. -/ lemma y_ne_zero_of_one_lt_x {a : solution₁ d} (ha : 1 < a.x) : a.y ≠ 0 := begin intro hy, have prop := a.prop, rw [hy, sq (0 : ℤ), zero_mul, mul_zero, sub_zero] at prop, exact lt_irrefl _ (((one_lt_sq_iff $ zero_le_one.trans ha.le).mpr ha).trans_eq prop), end /-- If a solution has `x > 1`, then `d` is positive. -/ lemma d_pos_of_one_lt_x {a : solution₁ d} (ha : 1 < a.x) : 0 < d := begin refine pos_of_mul_pos_left _ (sq_nonneg a.y), rw [a.prop_y, sub_pos], exact one_lt_pow ha two_ne_zero, end /-- If a solution has `x > 1`, then `d` is not a square. -/ lemma d_nonsquare_of_one_lt_x {a : solution₁ d} (ha : 1 < a.x) : ¬ is_square d := begin have hp := a.prop, rintros ⟨b, rfl⟩, simp_rw [← sq, ← mul_pow, sq_sub_sq, int.mul_eq_one_iff_eq_one_or_neg_one] at hp, rcases hp with ⟨hp₁, hp₂⟩ | ⟨hp₁, hp₂⟩; linarith [ha, hp₁, hp₂], end /-- A solution with `x = 1` is trivial. -/ lemma eq_one_of_x_eq_one (h₀ : d ≠ 0) {a : solution₁ d} (ha : a.x = 1) : a = 1 := begin have prop := a.prop_y, rw [ha, one_pow, sub_self, mul_eq_zero, or_iff_right h₀, sq_eq_zero_iff] at prop, exact ext ha prop, end /-- A solution is `1` or `-1` if and only if `y = 0`. -/ lemma eq_one_or_neg_one_iff_y_eq_zero {a : solution₁ d} : a = 1 ∨ a = -1 ↔ a.y = 0 := begin refine ⟨λ H, H.elim (λ h, by simp [h]) (λ h, by simp [h]), λ H, _⟩, have prop := a.prop, rw [H, sq (0 : ℤ), mul_zero, mul_zero, sub_zero, sq_eq_one_iff] at prop, exact prop.imp (λ h, ext h H) (λ h, ext h H), end /-- The set of solutions with `x > 0` is closed under multiplication. -/ lemma x_mul_pos {a b : solution₁ d} (ha : 0 < a.x) (hb : 0 < b.x) : 0 < (a * b).x := begin simp only [x_mul], refine neg_lt_iff_pos_add'.mp (abs_lt.mp _).1, rw [← abs_of_pos ha, ← abs_of_pos hb, ← abs_mul, ← sq_lt_sq, mul_pow a.x, a.prop_x, b.prop_x, ← sub_pos], ring_nf, cases le_or_lt 0 d with h h, { positivity, }, { rw [(eq_zero_of_d_neg h a).resolve_left ha.ne', (eq_zero_of_d_neg h b).resolve_left hb.ne', zero_pow two_pos, zero_add, zero_mul, zero_add], exact one_pos, }, end /-- The set of solutions with `x` and `y` positive is closed under multiplication. -/ lemma y_mul_pos {a b : solution₁ d} (hax : 0 < a.x) (hay : 0 < a.y) (hbx : 0 < b.x) (hby : 0 < b.y) : 0 < (a * b).y := begin simp only [y_mul], positivity, end /-- If `(x, y)` is a solution with `x` positive, then all its powers with natural exponents have positive `x`. -/ lemma x_pow_pos {a : solution₁ d} (hax : 0 < a.x) (n : ℕ) : 0 < (a ^ n).x := begin induction n with n ih, { simp only [pow_zero, x_one, zero_lt_one], }, { rw [pow_succ], exact x_mul_pos hax ih, } end /-- If `(x, y)` is a solution with `x` and `y` positive, then all its powers with positive natural exponents have positive `y`. -/ lemma y_pow_succ_pos {a : solution₁ d} (hax : 0 < a.x) (hay : 0 < a.y) (n : ℕ) : 0 < (a ^ n.succ).y := begin induction n with n ih, { simp only [hay, pow_one], }, { rw [pow_succ], exact y_mul_pos hax hay (x_pow_pos hax _) ih, } end /-- If `(x, y)` is a solution with `x` and `y` positive, then all its powers with positive exponents have positive `y`. -/ lemma y_zpow_pos {a : solution₁ d} (hax : 0 < a.x) (hay : 0 < a.y) {n : ℤ} (hn : 0 < n) : 0 < (a ^ n).y := begin lift n to ℕ using hn.le, norm_cast at hn ⊢, rw ← nat.succ_pred_eq_of_pos hn, exact y_pow_succ_pos hax hay _, end /-- If `(x, y)` is a solution with `x` positive, then all its powers have positive `x`. -/ lemma x_zpow_pos {a : solution₁ d} (hax : 0 < a.x) (n : ℤ) : 0 < (a ^ n).x := begin cases n, { rw zpow_of_nat, exact x_pow_pos hax n }, { rw zpow_neg_succ_of_nat, exact x_pow_pos hax (n + 1) }, end /-- If `(x, y)` is a solution with `x` and `y` positive, then the `y` component of any power has the same sign as the exponent. -/ lemma sign_y_zpow_eq_sign_of_x_pos_of_y_pos {a : solution₁ d} (hax : 0 < a.x) (hay : 0 < a.y) (n : ℤ) : (a ^ n).y.sign = n.sign := begin rcases n with (_ | _) | _, { refl }, { rw zpow_of_nat, exact int.sign_eq_one_of_pos (y_pow_succ_pos hax hay n) }, { rw zpow_neg_succ_of_nat, exact int.sign_eq_neg_one_of_neg (neg_neg_of_pos (y_pow_succ_pos hax hay n)) }, end /-- If `a` is any solution, then one of `a`, `a⁻¹`, `-a`, `-a⁻¹` has positive `x` and nonnegative `y`. -/ lemma exists_pos_variant (h₀ : 0 < d) (a : solution₁ d) : ∃ b : solution₁ d, 0 < b.x ∧ 0 ≤ b.y ∧ a ∈ ({b, b⁻¹, -b, -b⁻¹} : set (solution₁ d)) := begin refine (lt_or_gt_of_ne (a.x_ne_zero h₀.le)).elim ((le_total 0 a.y).elim (λ hy hx, ⟨-a⁻¹, _, _, _⟩) (λ hy hx, ⟨-a, _, _, _⟩)) ((le_total 0 a.y).elim (λ hy hx, ⟨a, hx, hy, _⟩) (λ hy hx, ⟨a⁻¹, hx, _, _⟩)); simp only [neg_neg, inv_inv, neg_inv, set.mem_insert_iff, set.mem_singleton_iff, true_or, eq_self_iff_true, x_neg, x_inv, y_neg, y_inv, neg_pos, neg_nonneg, or_true]; assumption, end end solution₁ section existence /-! ### Existence of nontrivial solutions -/ variables {d : ℤ} open set real /-- If `d` is a positive integer that is not a square, then there is a nontrivial solution to the Pell equation `x^2 - d*y^2 = 1`. -/ theorem exists_of_not_is_square (h₀ : 0 < d) (hd : ¬ is_square d) : ∃ x y : ℤ, x ^ 2 - d * y ^ 2 = 1 ∧ y ≠ 0 := begin let ξ : ℝ := sqrt d, have hξ : irrational ξ, { refine irrational_nrt_of_notint_nrt 2 d (sq_sqrt $ int.cast_nonneg.mpr h₀.le) _ two_pos, rintro ⟨x, hx⟩, refine hd ⟨x, @int.cast_injective ℝ _ _ d (x * x) _⟩, rw [← sq_sqrt $ int.cast_nonneg.mpr h₀.le, int.cast_mul, ← hx, sq], }, obtain ⟨M, hM₁⟩ := exists_int_gt (2 * |ξ| + 1), have hM : {q : ℚ | |q.1 ^ 2 - d * q.2 ^ 2| < M}.infinite, { refine infinite.mono (λ q h, _) (infinite_rat_abs_sub_lt_one_div_denom_sq_of_irrational hξ), have h0 : 0 < (q.2 : ℝ) ^ 2 := pow_pos (nat.cast_pos.mpr q.pos) 2, have h1 : (q.num : ℝ) / (q.denom : ℝ) = q := by exact_mod_cast q.num_div_denom, rw [mem_set_of, abs_sub_comm, ← @int.cast_lt ℝ, ← div_lt_div_right (abs_pos_of_pos h0)], push_cast, rw [← abs_div, abs_sq, sub_div, mul_div_cancel _ h0.ne', ← div_pow, h1, ← sq_sqrt (int.cast_pos.mpr h₀).le, sq_sub_sq, abs_mul, ← mul_one_div], refine mul_lt_mul'' (((abs_add ξ q).trans _).trans_lt hM₁) h (abs_nonneg _) (abs_nonneg _), rw [two_mul, add_assoc, add_le_add_iff_left, ← sub_le_iff_le_add'], rw [mem_set_of, abs_sub_comm] at h, refine (abs_sub_abs_le_abs_sub (q : ℝ) ξ).trans (h.le.trans _), rw [div_le_one h0, one_le_sq_iff_one_le_abs, nat.abs_cast, nat.one_le_cast], exact q.pos, }, obtain ⟨m, hm⟩ : ∃ m : ℤ, {q : ℚ | q.1 ^ 2 - d * q.2 ^ 2 = m}.infinite, { contrapose! hM, simp only [not_infinite] at hM ⊢, refine (congr_arg _ (ext (λ x, _))).mp (finite.bUnion (finite_Ioo (-M) M) (λ m _, hM m)), simp only [abs_lt, mem_set_of_eq, mem_Ioo, mem_Union, exists_prop, exists_eq_right'], }, have hm₀ : m ≠ 0, { rintro rfl, obtain ⟨q, hq⟩ := hm.nonempty, rw [mem_set_of, sub_eq_zero, mul_comm] at hq, obtain ⟨a, ha⟩ := (int.pow_dvd_pow_iff two_pos).mp ⟨d, hq⟩, rw [ha, mul_pow, mul_right_inj' (pow_pos (int.coe_nat_pos.mpr q.pos) 2).ne'] at hq, exact hd ⟨a, sq a ▸ hq.symm⟩, }, haveI := ne_zero_iff.mpr (int.nat_abs_ne_zero.mpr hm₀), let f : ℚ → (zmod m.nat_abs) × (zmod m.nat_abs) := λ q, (q.1, q.2), obtain ⟨q₁, h₁ : q₁.1 ^ 2 - d * q₁.2 ^ 2 = m, q₂, h₂ : q₂.1 ^ 2 - d * q₂.2 ^ 2 = m, hne, hqf⟩ := hm.exists_ne_map_eq_of_maps_to (maps_to_univ f _) finite_univ, obtain ⟨hq1 : (q₁.1 : zmod m.nat_abs) = q₂.1, hq2 : (q₁.2 : zmod m.nat_abs) = q₂.2⟩ := prod.ext_iff.mp hqf, have hd₁ : m ∣ q₁.1 * q₂.1 - d * (q₁.2 * q₂.2), { rw [← int.nat_abs_dvd, ← zmod.int_coe_zmod_eq_zero_iff_dvd], push_cast, rw [hq1, hq2, ← sq, ← sq], norm_cast, rw [zmod.int_coe_zmod_eq_zero_iff_dvd, int.nat_abs_dvd, nat.cast_pow, ← h₂], }, have hd₂ : m ∣ q₁.1 * q₂.2 - q₂.1 * q₁.2, { rw [← int.nat_abs_dvd, ← zmod.int_coe_eq_int_coe_iff_dvd_sub], push_cast, rw [hq1, hq2], }, replace hm₀ : (m : ℚ) ≠ 0 := int.cast_ne_zero.mpr hm₀, refine ⟨(q₁.1 * q₂.1 - d * (q₁.2 * q₂.2)) / m, (q₁.1 * q₂.2 - q₂.1 * q₁.2) / m, _, _⟩, { qify [hd₁, hd₂], field_simp [hm₀], norm_cast, conv_rhs {congr, rw sq, congr, rw ← h₁, skip, rw ← h₂}, push_cast, ring, }, { qify [hd₂], refine div_ne_zero_iff.mpr ⟨_, hm₀⟩, exact_mod_cast mt sub_eq_zero.mp (mt rat.eq_iff_mul_eq_mul.mpr hne), }, end /-- If `d` is a positive integer, then there is a nontrivial solution to the Pell equation `x^2 - d*y^2 = 1` if and only if `d` is not a square. -/ theorem exists_iff_not_is_square (h₀ : 0 < d) : (∃ x y : ℤ, x ^ 2 - d * y ^ 2 = 1 ∧ y ≠ 0) ↔ ¬ is_square d := begin refine ⟨_, exists_of_not_is_square h₀⟩, rintros ⟨x, y, hxy, hy⟩ ⟨a, rfl⟩, rw [← sq, ← mul_pow, sq_sub_sq] at hxy, simpa [mul_self_pos.mp h₀, sub_eq_add_neg, eq_neg_self_iff] using int.eq_of_mul_eq_one hxy, end namespace solution₁ /-- If `d` is a positive integer that is not a square, then there exists a nontrivial solution to the Pell equation `x^2 - d*y^2 = 1`. -/ theorem exists_nontrivial_of_not_is_square (h₀ : 0 < d) (hd : ¬ is_square d) : ∃ a : solution₁ d, a ≠ 1 ∧ a ≠ -1 := begin obtain ⟨x, y, prop, hy⟩ := exists_of_not_is_square h₀ hd, refine ⟨mk x y prop, λ H, _, λ H, _⟩; apply_fun solution₁.y at H; simpa only [hy] using H, end /-- If `d` is a positive integer that is not a square, then there exists a solution to the Pell equation `x^2 - d*y^2 = 1` with `x > 1` and `y > 0`. -/ lemma exists_pos_of_not_is_square (h₀ : 0 < d) (hd : ¬ is_square d) : ∃ a : solution₁ d, 1 < a.x ∧ 0 < a.y := begin obtain ⟨x, y, h, hy⟩ := exists_of_not_is_square h₀ hd, refine ⟨mk (|x|) (|y|) (by rwa [sq_abs, sq_abs]), _, abs_pos.mpr hy⟩, rw [x_mk, ← one_lt_sq_iff_one_lt_abs, eq_add_of_sub_eq h, lt_add_iff_pos_right], exact mul_pos h₀ (sq_pos_of_ne_zero y hy), end end solution₁ end existence /-! ### Fundamental solutions We define the notion of a *fundamental solution* of Pell's equation and show that it exists and is unique (when `d` is positive and non-square) and generates the group of solutions up to sign. -/ variables {d : ℤ} /-- We define a solution to be *fundamental* if it has `x > 1` and `y > 0` and its `x` is the smallest possible among solutions with `x > 1`. -/ def is_fundamental (a : solution₁ d) : Prop := 1 < a.x ∧ 0 < a.y ∧ ∀ {b : solution₁ d}, 1 < b.x → a.x ≤ b.x namespace is_fundamental open solution₁ /-- A fundamental solution has positive `x`. -/ lemma x_pos {a : solution₁ d} (h : is_fundamental a) : 0 < a.x := zero_lt_one.trans h.1 /-- If a fundamental solution exists, then `d` must be positive. -/ lemma d_pos {a : solution₁ d} (h : is_fundamental a) : 0 < d := d_pos_of_one_lt_x h.1 /-- If a fundamental solution exists, then `d` must be a non-square. -/ lemma d_nonsquare {a : solution₁ d} (h : is_fundamental a) : ¬ is_square d := d_nonsquare_of_one_lt_x h.1 /-- If there is a fundamental solution, it is unique. -/ lemma subsingleton {a b : solution₁ d} (ha : is_fundamental a) (hb : is_fundamental b) : a = b := begin have hx := le_antisymm (ha.2.2 hb.1) (hb.2.2 ha.1), refine solution₁.ext hx _, have : d * a.y ^ 2 = d * b.y ^ 2 := by rw [a.prop_y, b.prop_y, hx], exact (sq_eq_sq ha.2.1.le hb.2.1.le).mp (int.eq_of_mul_eq_mul_left ha.d_pos.ne' this), end /-- If `d` is positive and not a square, then a fundamental solution exists. -/ lemma exists_of_not_is_square (h₀ : 0 < d) (hd : ¬ is_square d) : ∃ a : solution₁ d, is_fundamental a := begin obtain ⟨a, ha₁, ha₂⟩ := exists_pos_of_not_is_square h₀ hd, -- convert to `x : ℕ` to be able to use `nat.find` have P : ∃ x' : ℕ, 1 < x' ∧ ∃ y' : ℤ, 0 < y' ∧ (x' : ℤ) ^ 2 - d * y' ^ 2 = 1, { have hax := a.prop, lift a.x to ℕ using (by positivity) with ax, norm_cast at ha₁, exact ⟨ax, ha₁, a.y, ha₂, hax⟩, }, classical, -- to avoid having to show that the predicate is decidable let x₁ := nat.find P, obtain ⟨hx, y₁, hy₀, hy₁⟩ := nat.find_spec P, refine ⟨mk x₁ y₁ hy₁, (by {rw x_mk, exact_mod_cast hx}), hy₀, λ b hb, _⟩, rw x_mk, have hb' := (int.to_nat_of_nonneg $ zero_le_one.trans hb.le).symm, have hb'' := hb, rw hb' at hb ⊢, norm_cast at hb ⊢, refine nat.find_min' P ⟨hb, |b.y|, abs_pos.mpr $ y_ne_zero_of_one_lt_x hb'', _⟩, rw [← hb', sq_abs], exact b.prop, end /-- The map sending an integer `n` to the `y`-coordinate of `a^n` for a fundamental solution `a` is stritcly increasing. -/ lemma y_strict_mono {a : solution₁ d} (h : is_fundamental a) : strict_mono (λ (n : ℤ), (a ^ n).y) := begin have H : ∀ (n : ℤ), 0 ≤ n → (a ^ n).y < (a ^ (n + 1)).y, { intros n hn, rw [← sub_pos, zpow_add, zpow_one, y_mul, add_sub_assoc], rw show (a ^ n).y * a.x - (a ^ n).y = (a ^ n).y * (a.x - 1), from by ring, refine add_pos_of_pos_of_nonneg (mul_pos (x_zpow_pos h.x_pos _) h.2.1) (mul_nonneg _ (by {rw sub_nonneg, exact h.1.le})), rcases hn.eq_or_lt with rfl | hn, { simp only [zpow_zero, y_one], }, { exact (y_zpow_pos h.x_pos h.2.1 hn).le, } }, refine strict_mono_int_of_lt_succ (λ n, _), cases le_or_lt 0 n with hn hn, { exact H n hn, }, { let m : ℤ := -n - 1, have hm : n = -m - 1 := by simp only [neg_sub, sub_neg_eq_add, add_tsub_cancel_left], rw [hm, sub_add_cancel, ← neg_add', zpow_neg, zpow_neg, y_inv, y_inv, neg_lt_neg_iff], exact H _ (by linarith [hn]), } end /-- If `a` is a fundamental solution, then `(a^m).y < (a^n).y` if and only if `m < n`. -/ lemma zpow_y_lt_iff_lt {a : solution₁ d} (h : is_fundamental a) (m n : ℤ) : (a ^ m).y < (a ^ n).y ↔ m < n := begin refine ⟨λ H, _, λ H, h.y_strict_mono H⟩, contrapose! H, exact h.y_strict_mono.monotone H, end /-- The `n`th power of a fundamental solution is trivial if and only if `n = 0`. -/ lemma zpow_eq_one_iff {a : solution₁ d} (h : is_fundamental a) (n : ℤ) : a ^ n = 1 ↔ n = 0 := begin rw ← zpow_zero a, exact ⟨λ H, h.y_strict_mono.injective (congr_arg solution₁.y H), λ H, H ▸ rfl⟩, end /-- A power of a fundamental solution is never equal to the negative of a power of this fundamental solution. -/ lemma zpow_ne_neg_zpow {a : solution₁ d} (h : is_fundamental a) {n n' : ℤ} : a ^ n ≠ -a ^ n' := begin intro hf, apply_fun solution₁.x at hf, have H := x_zpow_pos h.x_pos n, rw [hf, x_neg, lt_neg, neg_zero] at H, exact lt_irrefl _ ((x_zpow_pos h.x_pos n').trans H), end /-- The `x`-coordinate of a fundamental solution is a lower bound for the `x`-coordinate of any positive solution. -/ lemma x_le_x {a₁ : solution₁ d} (h : is_fundamental a₁) {a : solution₁ d} (hax : 1 < a.x) : a₁.x ≤ a.x := h.2.2 hax /-- The `y`-coordinate of a fundamental solution is a lower bound for the `y`-coordinate of any positive solution. -/ lemma y_le_y {a₁ : solution₁ d} (h : is_fundamental a₁) {a : solution₁ d} (hax : 1 < a.x) (hay : 0 < a.y) : a₁.y ≤ a.y := begin have H : d * (a₁.y ^ 2 - a.y ^ 2) = a₁.x ^ 2 - a.x ^ 2 := by { rw [a.prop_x, a₁.prop_x], ring }, rw [← abs_of_pos hay, ← abs_of_pos h.2.1, ← sq_le_sq, ← mul_le_mul_left h.d_pos, ← sub_nonpos, ← mul_sub, H, sub_nonpos, sq_le_sq, abs_of_pos (zero_lt_one.trans h.1), abs_of_pos (zero_lt_one.trans hax)], exact h.x_le_x hax, end -- helper lemma for the next three results lemma x_mul_y_le_y_mul_x {a₁ : solution₁ d} (h : is_fundamental a₁) {a : solution₁ d} (hax : 1 < a.x) (hay : 0 < a.y) : a.x * a₁.y ≤ a.y * a₁.x := begin rw [← abs_of_pos $ zero_lt_one.trans hax, ← abs_of_pos hay, ← abs_of_pos h.x_pos, ← abs_of_pos h.2.1, ← abs_mul, ← abs_mul, ← sq_le_sq, mul_pow, mul_pow, a.prop_x, a₁.prop_x, ← sub_nonneg], ring_nf, rw [sub_nonneg, sq_le_sq, abs_of_pos hay, abs_of_pos h.2.1], exact h.y_le_y hax hay, end /-- If we multiply a positive solution with the inverse of a fundamental solution, the `y`-coordinate remains nonnegative. -/ lemma mul_inv_y_nonneg {a₁ : solution₁ d} (h : is_fundamental a₁) {a : solution₁ d} (hax : 1 < a.x) (hay : 0 < a.y) : 0 ≤ (a * a₁⁻¹).y := by simpa only [y_inv, mul_neg, y_mul, le_neg_add_iff_add_le, add_zero] using h.x_mul_y_le_y_mul_x hax hay /-- If we multiply a positive solution with the inverse of a fundamental solution, the `x`-coordinate stays positive. -/ lemma mul_inv_x_pos {a₁ : solution₁ d} (h : is_fundamental a₁) {a : solution₁ d} (hax : 1 < a.x) (hay : 0 < a.y) : 0 < (a * a₁⁻¹).x := begin simp only [x_mul, x_inv, y_inv, mul_neg, lt_add_neg_iff_add_lt, zero_add], refine (mul_lt_mul_left $ zero_lt_one.trans hax).mp _, rw [(by ring : a.x * (d * (a.y * a₁.y)) = (d * a.y) * (a.x * a₁.y))], refine ((mul_le_mul_left $ mul_pos h.d_pos hay).mpr $ x_mul_y_le_y_mul_x h hax hay).trans_lt _, rw [← mul_assoc, mul_assoc d, ← sq, a.prop_y, ← sub_pos], ring_nf, exact zero_lt_one.trans h.1, end /-- If we multiply a positive solution with the inverse of a fundamental solution, the `x`-coordinate decreases. -/ lemma mul_inv_x_lt_x {a₁ : solution₁ d} (h : is_fundamental a₁) {a : solution₁ d} (hax : 1 < a.x) (hay : 0 < a.y) : (a * a₁⁻¹).x < a.x := begin simp only [x_mul, x_inv, y_inv, mul_neg, add_neg_lt_iff_le_add'], refine (mul_lt_mul_left h.2.1).mp _, rw [(by ring : a₁.y * (a.x * a₁.x) = a.x * a₁.y * a₁.x)], refine ((mul_le_mul_right $ zero_lt_one.trans h.1).mpr $ x_mul_y_le_y_mul_x h hax hay).trans_lt _, rw [mul_assoc, ← sq, a₁.prop_x, ← sub_neg], ring_nf, rw [sub_neg, ← abs_of_pos hay, ← abs_of_pos h.2.1, ← abs_of_pos $ zero_lt_one.trans hax, ← abs_mul, ← sq_lt_sq, mul_pow, a.prop_x], calc a.y ^ 2 = 1 * a.y ^ 2 : (one_mul _).symm ... ≤ d * a.y ^ 2 : (mul_le_mul_right $ sq_pos_of_pos hay).mpr h.d_pos ... < d * a.y ^ 2 + 1 : lt_add_one _ ... = (1 + d * a.y ^ 2) * 1 : by rw [add_comm, mul_one] ... ≤ (1 + d * a.y ^ 2) * a₁.y ^ 2 : (mul_le_mul_left (by {have := h.d_pos, positivity})).mpr (sq_pos_of_pos h.2.1), end /-- Any nonnegative solution is a power with nonnegative exponent of a fundamental solution. -/ lemma eq_pow_of_nonneg {a₁ : solution₁ d} (h : is_fundamental a₁) {a : solution₁ d} (hax : 0 < a.x) (hay : 0 ≤ a.y) : ∃ n : ℕ, a = a₁ ^ n := begin lift a.x to ℕ using hax.le with ax hax', induction ax using nat.strong_induction_on with x ih generalizing a, cases hay.eq_or_lt with hy hy, { -- case 1: `a = 1` refine ⟨0, _⟩, simp only [pow_zero], ext; simp only [x_one, y_one], { have prop := a.prop, rw [← hy, sq (0 : ℤ), zero_mul, mul_zero, sub_zero, sq_eq_one_iff] at prop, refine prop.resolve_right (λ hf, _), have := (hax.trans_eq hax').le.trans_eq hf, norm_num at this, }, { exact hy.symm, } }, { -- case 2: `a ≥ a₁` have hx₁ : 1 < a.x := by nlinarith [a.prop, h.d_pos], have hxx₁ := h.mul_inv_x_pos hx₁ hy, have hxx₂ := h.mul_inv_x_lt_x hx₁ hy, have hyy := h.mul_inv_y_nonneg hx₁ hy, lift (a * a₁⁻¹).x to ℕ using hxx₁.le with x' hx', obtain ⟨n, hn⟩ := ih x' (by exact_mod_cast hxx₂.trans_eq hax'.symm) hxx₁ hyy hx', exact ⟨n + 1, by rw [pow_succ, ← hn, mul_comm a, ← mul_assoc, mul_inv_self, one_mul]⟩, }, end /-- Every solution is, up to a sign, a power of a given fundamental solution. -/ lemma eq_zpow_or_neg_zpow {a₁ : solution₁ d} (h : is_fundamental a₁) (a : solution₁ d) : ∃ n : ℤ, a = a₁ ^ n ∨ a = -a₁ ^ n := begin obtain ⟨b, hbx, hby, hb⟩ := exists_pos_variant h.d_pos a, obtain ⟨n, hn⟩ := h.eq_pow_of_nonneg hbx hby, rcases hb with rfl | rfl | rfl | hb, { exact ⟨n, or.inl (by exact_mod_cast hn)⟩, }, { exact ⟨-n, or.inl (by simp [hn])⟩, }, { exact ⟨n, or.inr (by simp [hn])⟩, }, { rw set.mem_singleton_iff at hb, rw hb, exact ⟨-n, or.inr (by simp [hn])⟩, } end end is_fundamental open solution₁ is_fundamental /-- When `d` is positive and not a square, then the group of solutions to the Pell equation `x^2 - d*y^2 = 1` has a unique positive generator (up to sign). -/ theorem exists_unique_pos_generator (h₀ : 0 < d) (hd : ¬ is_square d) : ∃! a₁ : solution₁ d, 1 < a₁.x ∧ 0 < a₁.y ∧ ∀ a : solution₁ d, ∃ n : ℤ, a = a₁ ^ n ∨ a = -a₁ ^ n := begin obtain ⟨a₁, ha₁⟩ := is_fundamental.exists_of_not_is_square h₀ hd, refine ⟨a₁, ⟨ha₁.1, ha₁.2.1, ha₁.eq_zpow_or_neg_zpow⟩, λ a (H : 1 < _ ∧ _), _⟩, obtain ⟨Hx, Hy, H⟩ := H, obtain ⟨n₁, hn₁⟩ := H a₁, obtain ⟨n₂, hn₂⟩ := ha₁.eq_zpow_or_neg_zpow a, rcases hn₂ with rfl | rfl, { rw [← zpow_mul, eq_comm, @eq_comm _ a₁, ← mul_inv_eq_one, ← @mul_inv_eq_one _ _ _ a₁, ← zpow_neg_one, neg_mul, ← zpow_add, ← sub_eq_add_neg] at hn₁, cases hn₁, { rcases int.is_unit_iff.mp (is_unit_of_mul_eq_one _ _ $ sub_eq_zero.mp $ (ha₁.zpow_eq_one_iff (n₂ * n₁ - 1)).mp hn₁) with rfl | rfl, { rw zpow_one, }, { rw [zpow_neg_one, y_inv, lt_neg, neg_zero] at Hy, exact false.elim (lt_irrefl _ $ ha₁.2.1.trans Hy), } }, { rw [← zpow_zero a₁, eq_comm] at hn₁, exact false.elim (ha₁.zpow_ne_neg_zpow hn₁), } }, { rw [x_neg, lt_neg] at Hx, have := (x_zpow_pos (zero_lt_one.trans ha₁.1) n₂).trans Hx, norm_num at this, } end /-- A positive solution is a generator (up to sign) of the group of all solutions to the Pell equation `x^2 - d*y^2 = 1` if and only if it is a fundamental solution. -/ theorem pos_generator_iff_fundamental (a : solution₁ d) : (1 < a.x ∧ 0 < a.y ∧ ∀ b : solution₁ d, ∃ n : ℤ, b = a ^ n ∨ b = -a ^ n) ↔ is_fundamental a := begin refine ⟨λ h, _, λ H, ⟨H.1, H.2.1, H.eq_zpow_or_neg_zpow⟩⟩, have h₀ := d_pos_of_one_lt_x h.1, have hd := d_nonsquare_of_one_lt_x h.1, obtain ⟨a₁, ha₁⟩ := is_fundamental.exists_of_not_is_square h₀ hd, obtain ⟨b, hb₁, hb₂⟩ := exists_unique_pos_generator h₀ hd, rwa [hb₂ a h, ← hb₂ a₁ ⟨ha₁.1, ha₁.2.1, ha₁.eq_zpow_or_neg_zpow⟩], end end pell
bcaaa2d17f1f911ba25be2c712c8b090d7cfae12
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/matchDiscrType.lean
2a8accadaa303b4f8652c40ba173dd63fa888d36
[ "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
258
lean
def g (x : Nat) : List (Nat × List Nat) := [(x, [x, x]), (x, [])] def h (x : Nat) : List Nat := let xs := g x |>.filter (fun ⟨_, xs⟩ => xs.isEmpty) xs.map (·.1) theorem ex1 : g 10 = [(10, [10, 10]), (10, [])] := rfl theorem ex2 : h 10 = [10] := rfl
b8b2d0591045dac276657e1c95470101bad300c0
618003631150032a5676f229d13a079ac875ff77
/src/topology/basic.lean
ef1a031c1e44b46d251a4b85fce84fabaf96194c
[ "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
36,340
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Jeremy Avigad -/ import order.filter import order.filter.bases /-! # Basic theory of topological spaces. The main definition is the type class `topological space α` which endows a type `α` with a topology. Then `set α` gets predicates `is_open`, `is_closed` and functions `interior`, `closure` and `frontier`. Each point `x` of `α` gets a neighborhood filter `𝓝 x`. This file also defines locally finite families of subsets of `α`. For topological spaces `α` and `β`, a function `f : α → β` and a point `a : α`, `continuous_at f a` means `f` is continuous at `a`, and global continuity is `continuous f`. There is also a version of continuity `pcontinuous` for partially defined functions. ## Implementation notes Topology in mathlib heavily uses filters (even more than in Bourbaki). See explanations in <https://leanprover-community.github.io/theories/topology.html>. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] ## Tags topological space, interior, closure, frontier, neighborhood, continuity, continuous function -/ open set filter classical open_locale classical universes u v w /-- A topology on `α`. -/ @[protect_proj] structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space /-- A constructor for topologies by specifying the closed sets, and showing that they satisfy the appropriate conditions. -/ def topological_space.of_closed {α : Type u} (T : set (set α)) (empty_mem : ∅ ∈ T) (sInter_mem : ∀ A ⊆ T, ⋂₀ A ∈ T) (union_mem : ∀ A B ∈ T, A ∪ B ∈ T) : topological_space α := { is_open := λ X, -X ∈ T, is_open_univ := by simp [empty_mem], is_open_inter := λ s t hs ht, by simpa [set.compl_inter] using union_mem (-s) (-t) hs ht, is_open_sUnion := λ s hs, by rw set.compl_sUnion; exact sInter_mem (set.compl '' s) (λ z ⟨y, hy, hz⟩, by simpa [hz.symm] using hs y hy) } section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop} @[ext] lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g | ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl section variables [t : topological_space α] include t /-- `is_open s` means that `s` is open in the ambient topological space on `α` -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s := rfl variables [topological_space α] lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) : is_open (⋃i∈s, f i) := is_open_Union $ assume i, is_open_Union $ assume hi, h i hi lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp] lemma is_open_empty : is_open (∅ : set α) := by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim) lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) := finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $ λ a s has hs ih h, by rw sInter_insert; exact is_open_inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _) lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) := finite.induction_on hs (λ _, by rw bInter_empty; exact is_open_univ) (λ a s has hs ih h, by rw bInter_insert; exact is_open_inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_open_Inter [fintype β] {s : β → set α} (h : ∀ i, is_open (s i)) : is_open (⋂ i, s i) := suffices is_open (⋂ (i : β) (hi : i ∈ @univ β), s i), by simpa, is_open_bInter finite_univ (λ i _, h i) lemma is_open_Inter_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_open (s h)) : is_open (Inter s) := by by_cases p; simp * lemma is_open_const {p : Prop} : is_open {a : α | p} := by_cases (assume : p, begin simp only [this]; exact is_open_univ end) (assume : ¬ p, begin simp only [this]; exact is_open_empty end) lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} := is_open_inter /-- A set is closed if its complement is open -/ def is_closed (s : set α) : Prop := is_open (-s) @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by unfold is_closed; rw compl_empty; exact is_open_univ @[simp] lemma is_closed_univ : is_closed (univ : set α) := by unfold is_closed; rw compl_univ; exact is_open_empty lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := λ h₁ h₂, by unfold is_closed; rw compl_union; exact is_open_inter h₁ h₂ lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simp only [is_closed, compl_sInter, sUnion_image]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i @[simp] lemma is_open_compl_iff {s : set α} : is_open (-s) ↔ is_closed s := iff.rfl @[simp] lemma is_closed_compl_iff {s : set α} : is_closed (-s) ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) := is_open_inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂ lemma is_closed_bUnion {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := finite.induction_on hs (λ _, by rw bUnion_empty; exact is_closed_empty) (λ a s has hs ih h, by rw bUnion_insert; exact is_closed_union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_closed_Union [fintype β] {s : β → set α} (h : ∀ i, is_closed (s i)) : is_closed (Union s) := suffices is_closed (⋃ (i : β) (hi : i ∈ @univ β), s i), by convert this; simp [set.ext_iff], is_closed_bUnion finite_univ (λ i _, h i) lemma is_closed_Union_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_closed (s h)) : is_closed (Union s) := by by_cases p; simp * lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} := have {x | p x → q x} = (- {x | p x}) ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or, by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} := is_open_compl_iff.mpr /-- The interior of a set `s` is the largest open subset of `s`. -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} lemma mem_interior {s : set α} {x : α} : x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t := by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm] @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma interior_eq_of_open {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, interior_eq_of_open⟩ lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s := by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and] lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := interior_eq_of_open is_open_empty @[simp] lemma interior_univ : interior (univ : set α) = univ := interior_eq_of_open is_open_univ @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := interior_eq_of_open is_open_interior @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open_inter is_open_interior is_open_interior) lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u \ s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u \ s ⊆ interior t, by rwa subset_interior_iff_subset_of_open (is_open_diff hu₁ h₁), have u \ s ⊆ ∅, by rwa h₂ at this, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t := by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior] /-- The closure of `s` is the smallest closed set containing `s`. -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma closure_eq_of_is_closed {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, closure_eq_of_is_closed⟩ lemma closure_subset_iff_subset_of_is_closed {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure lemma monotone_closure (α : Type*) [topological_space α] : monotone (@closure α _) := λ _ _, closure_mono lemma closure_inter_subset_inter_closure (s t : set α) : closure (s ∩ t) ⊆ closure s ∩ closure t := (monotone_closure α).map_inf_le s t lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s := by rw subset.antisymm subset_closure h; exact is_closed_closure @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := closure_eq_of_is_closed is_closed_empty lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ := ⟨subset_eq_empty subset_closure, λ h, h.symm ▸ closure_empty⟩ lemma set.nonempty.closure {s : set α} (h : s.nonempty) : set.nonempty (closure s) := let ⟨x, hx⟩ := h in ⟨x, subset_closure hx⟩ @[simp] lemma closure_univ : closure (univ : set α) = univ := closure_eq_of_is_closed is_closed_univ @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := closure_eq_of_is_closed is_closed_closure @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed_union is_closed_closure is_closed_closure) ((monotone_closure α).le_map_sup s t) lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = - interior (- s) := begin unfold interior closure is_closed, rw [compl_sUnion, compl_image_set_of], simp only [compl_subset_compl] end @[simp] lemma interior_compl {s : set α} : interior (- s) = - closure s := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl {s : set α} : closure (- s) = - interior s := by simp [closure_eq_compl_interior_compl] theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → (o ∩ s).nonempty := ⟨λ h o oo ao, classical.by_contradiction $ λ os, have s ⊆ -o, from λ x xs xo, os ⟨x, xo, xs⟩, closure_minimal this (is_closed_compl_iff.2 oo) h ao, λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc, let ⟨x, hc, hs⟩ := (H _ h₁ nc) in hc (h₂ hs)⟩ lemma dense_iff_inter_open {s : set α} : closure s = univ ↔ ∀ U, is_open U → U.nonempty → (U ∩ s).nonempty := begin split ; intro h, { rintros U U_op ⟨x, x_in⟩, exact mem_closure_iff.1 (by simp only [h]) U U_op x_in }, { apply eq_univ_of_forall, intro x, rw mem_closure_iff, intros U U_op x_in, exact h U U_op ⟨_, x_in⟩ }, end lemma dense_of_subset_dense {s₁ s₂ : set α} (h : s₁ ⊆ s₂) (hd : closure s₁ = univ) : closure s₂ = univ := by { rw [← univ_subset_iff, ← hd], exact closure_mono h } /-- The frontier of a set is the set of points between the closure and interior. -/ def frontier (s : set α) : set α := closure s \ interior s lemma frontier_eq_closure_inter_closure {s : set α} : frontier s = closure s ∩ closure (- s) := by rw [closure_compl, frontier, diff_eq] /-- The complement of a set has the same frontier as the original set. -/ @[simp] lemma frontier_compl (s : set α) : frontier (-s) = frontier s := by simp only [frontier_eq_closure_inter_closure, compl_compl, inter_comm] lemma frontier_inter_subset (s t : set α) : frontier (s ∩ t) ⊆ (frontier s ∩ closure t) ∪ (closure s ∩ frontier t) := begin simp only [frontier_eq_closure_inter_closure, compl_inter, closure_union], convert inter_subset_inter_left _ (closure_inter_subset_inter_closure s t), simp only [inter_distrib_left, inter_distrib_right, inter_assoc], congr' 2, apply inter_comm end lemma frontier_union_subset (s t : set α) : frontier (s ∪ t) ⊆ (frontier s ∩ closure (-t)) ∪ (closure (-s) ∩ frontier t) := by simpa only [frontier_compl, (compl_union _ _).symm] using frontier_inter_subset (-s) (-t) lemma is_closed.frontier_eq {s : set α} (hs : is_closed s) : frontier s = s \ interior s := by rw [frontier, closure_eq_of_is_closed hs] lemma is_open.frontier_eq {s : set α} (hs : is_open s) : frontier s = closure s \ s := by rw [frontier, interior_eq_of_open hs] /-- The frontier of a set is closed. -/ lemma is_closed_frontier {s : set α} : is_closed (frontier s) := by rw frontier_eq_closure_inter_closure; exact is_closed_inter is_closed_closure is_closed_closure /-- The frontier of a set has no interior point. -/ lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ := begin have A : frontier s = s \ interior s, from h.frontier_eq, have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _), have C : interior (frontier s) ⊆ frontier s := interior_subset, have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) := subset_inter B (by simpa [A] using C), rwa [inter_diff_self, subset_empty_iff] at this, end /-- neighbourhood filter -/ def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) localized "notation `𝓝` := nhds" in topological_space lemma nhds_def (a : α) : 𝓝 a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) := rfl lemma nhds_basis_opens (a : α) : (𝓝 a).has_basis (λ s : set α, a ∈ s ∧ is_open s) (λ x, x) := has_basis_binfi_principal (λ s ⟨has, hs⟩ t ⟨hat, ht⟩, ⟨s ∩ t, ⟨⟨has, hat⟩, is_open_inter hs ht⟩, ⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩) ⟨univ, ⟨mem_univ a, is_open_univ⟩⟩ lemma le_nhds_iff {f a} : f ≤ 𝓝 a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f := by simp [nhds_def] lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : principal s ≤ f) : 𝓝 a ≤ f := by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf) lemma mem_nhds_sets_iff {a : α} {s : set α} : s ∈ 𝓝 a ↔ ∃t⊆s, is_open t ∧ a ∈ t := (nhds_basis_opens a).mem_iff.trans ⟨λ ⟨t, ⟨hat, ht⟩, hts⟩, ⟨t, hts, ht, hat⟩, λ ⟨t, hts, ht, hat⟩, ⟨t, ⟨hat, ht⟩, hts⟩⟩ lemma map_nhds {a : α} {f : α → β} : map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal (image f s)) := ((nhds_basis_opens a).map f).eq_binfi attribute [irreducible] nhds lemma mem_of_nhds {a : α} {s : set α} : s ∈ 𝓝 a → a ∈ s := λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs lemma filter.eventually.self_of_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) : p a := mem_of_nhds h lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ 𝓝 a := mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩ theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) : (∀ s ∈ 𝓝 x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) := iff.intro (λ h s os xs, h s (mem_nhds_sets os xs)) (λ h t, begin change t ∈ 𝓝 x → P t, rw mem_nhds_sets_iff, rintros ⟨s, hs, opens, xs⟩, exact hP _ _ hs (h s opens xs), end) theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t) (l : filter β) : (∀ s ∈ 𝓝 x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) := all_mem_nhds _ _ (λ s t ssubt h, mem_sets_of_superset h (hf s t ssubt)) theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) := all_mem_nhds_filter _ _ (λ s t, id) _ theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto' r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) := by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono } theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) := rtendsto_nhds theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto' f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) := rtendsto'_nhds theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} : tendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) := all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _ lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (𝓝 a) := tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha lemma pure_le_nhds : pure ≤ (𝓝 : α → filter α) := assume a s hs, mem_pure_sets.2 $ mem_of_nhds hs lemma tendsto_pure_nhds {α : Type*} [topological_space β] (f : α → β) (a : α) : tendsto f (pure a) (𝓝 (f a)) := begin rw [tendsto, filter.map_pure], exact pure_le_nhds (f a) end @[simp] lemma nhds_ne_bot {a : α} : 𝓝 a ≠ ⊥ := ne_bot_of_le_ne_bot pure_ne_bot (pure_le_nhds a) lemma interior_eq_nhds {s : set α} : interior s = {a | 𝓝 a ≤ principal s} := set.ext $ λ x, by simp only [mem_interior, le_principal_iff, mem_nhds_sets_iff]; refl lemma mem_interior_iff_mem_nhds {s : set α} {a : α} : a ∈ interior s ↔ s ∈ 𝓝 a := by simp only [interior_eq_nhds, le_principal_iff]; refl lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, 𝓝 a ≤ principal s := calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm ... ↔ (∀a∈s, 𝓝 a ≤ principal s) : by rw [interior_eq_nhds]; refl lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ 𝓝 a := is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff lemma closure_eq_nhds {s : set α} : closure s = {a | 𝓝 a ⊓ principal s ≠ ⊥} := calc closure s = - interior (- s) : closure_eq_compl_interior_compl ... = {a | ¬ 𝓝 a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl ... = {a | 𝓝 a ⊓ principal s ≠ ⊥} : set.ext $ assume a, not_congr (inf_eq_bot_iff_le_compl (show principal s ⊔ principal (-s) = ⊤, by simp only [sup_principal, union_compl_self, principal_univ]) (by simp only [inf_principal, inter_compl_self, principal_empty])).symm theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, (t ∩ s).nonempty := mem_closure_iff.trans ⟨λ H t ht, nonempty.mono (inter_subset_inter_left _ interior_subset) (H _ is_open_interior (mem_interior_iff_mem_nhds.2 ht)), λ H o oo ao, H _ (mem_nhds_sets oo ao)⟩ theorem mem_closure_iff_nhds_basis {a : α} {p : β → Prop} {s : β → set α} (h : (𝓝 a).has_basis p s) {t : set α} : a ∈ closure t ↔ ∀ i, p i → ∃ y ∈ t, y ∈ s i := mem_closure_iff_nhds.trans ⟨λ H i hi, let ⟨x, hx⟩ := (H _ $ h.mem_of_mem hi) in ⟨x, hx.2, hx.1⟩, λ H t' ht', let ⟨i, hi, hit⟩ := h.mem_iff.1 ht', ⟨x, xt, hx⟩ := H i hi in ⟨x, hit hx, xt⟩⟩ /-- `x` belongs to the closure of `s` if and only if some ultrafilter supported on `s` converges to `x`. -/ lemma mem_closure_iff_ultrafilter {s : set α} {x : α} : x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u.val ∧ u.val ≤ 𝓝 x := begin rw closure_eq_nhds, change 𝓝 x ⊓ principal s ≠ ⊥ ↔ _, symmetry, convert exists_ultrafilter_iff _, ext u, rw [←le_principal_iff, inf_comm, le_inf_iff] end lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀a, 𝓝 a ⊓ principal s ≠ ⊥ → a ∈ s := calc is_closed s ↔ closure s = s : by rw [closure_eq_iff_is_closed] ... ↔ closure s ⊆ s : ⟨assume h, by rw h, assume h, subset.antisymm h subset_closure⟩ ... ↔ (∀a, 𝓝 a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := assume a ⟨hs, ht⟩, have s ∈ 𝓝 a, from mem_nhds_sets h hs, have 𝓝 a ⊓ principal s = 𝓝 a, by rwa [inf_eq_left, le_principal_iff], have 𝓝 a ⊓ principal (s ∩ t) ≠ ⊥, from calc 𝓝 a ⊓ principal (s ∩ t) = 𝓝 a ⊓ (principal s ⊓ principal t) : by rw inf_principal ... = 𝓝 a ⊓ principal t : by rw [←inf_assoc, this] ... ≠ ⊥ : by rw [closure_eq_nhds] at ht; assumption, by rwa [closure_eq_nhds] lemma closure_diff {s t : set α} : closure s - closure t ⊆ closure (s - t) := calc closure s \ closure t = (- closure t) ∩ closure s : by simp only [diff_eq, inter_comm] ... ⊆ closure (- closure t ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp only [diff_eq, inter_comm] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure lemma mem_of_closed_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (𝓝 a)) (hs : is_closed s) (h : f ⁻¹' s ∈ b) : a ∈ s := have b.map f ≤ 𝓝 a ⊓ principal s, from le_trans (le_inf (le_refl _) (le_principal_iff.mpr h)) (inf_le_inf_right _ hf), is_closed_iff_nhds.mp hs a $ ne_bot_of_le_ne_bot (map_ne_bot hb) this lemma mem_of_closed_of_tendsto' {f : β → α} {x : filter β} {a : α} {s : set α} (hf : tendsto f x (𝓝 a)) (hs : is_closed s) (h : x ⊓ principal (f ⁻¹' s) ≠ ⊥) : a ∈ s := is_closed_iff_nhds.mp hs _ $ ne_bot_of_le_ne_bot (@map_ne_bot _ _ _ f h) $ le_inf (le_trans (map_mono $ inf_le_left) hf) $ le_trans (map_mono $ inf_le_right_of_le $ by simp only [comap_principal, le_principal_iff]; exact subset.refl _) (@map_comap_le _ _ _ f) lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ closure s := mem_of_closed_of_tendsto hb hf (is_closed_closure) $ filter.mem_sets_of_superset h (preimage_mono subset_closure) /-- Suppose that `f` sends the complement to `s` to a single point `a`, and `l` is some filter. Then `f` tends to `a` along `l` restricted to `s` if and only it tends to `a` along `l`. -/ lemma tendsto_inf_principal_nhds_iff_of_forall_eq {f : β → α} {l : filter β} {s : set β} {a : α} (h : ∀ x ∉ s, f x = a) : tendsto f (l ⊓ principal s) (𝓝 a) ↔ tendsto f l (𝓝 a) := begin rw [tendsto_iff_comap, tendsto_iff_comap], replace h : principal (-s) ≤ comap f (𝓝 a), { rintros U ⟨t, ht, htU⟩ x hx, have : f x ∈ t, from (h x hx).symm ▸ mem_of_nhds ht, exact htU this }, refine ⟨λ h', _, le_trans inf_le_left⟩, have := sup_le h' h, rw [sup_inf_right, sup_principal, union_compl_self, principal_univ, inf_top_eq, sup_le_iff] at this, exact this.1 end section lim variables [nonempty α] /-- If `f` is a filter, then `lim f` is a limit of the filter, if it exists. -/ noncomputable def lim (f : filter α) : α := epsilon $ λa, f ≤ 𝓝 a lemma lim_spec {f : filter α} (h : ∃a, f ≤ 𝓝 a) : f ≤ 𝓝 (lim f) := epsilon_spec h end lim /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite /-- A family of sets in `set α` is locally finite if at every point `x:α`, there is a neighborhood of `x` which meets only finitely many sets in the family -/ def locally_finite (f : β → set α) := ∀x:α, ∃t ∈ 𝓝 x, finite {i | (f i ∩ t).nonempty } lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f := assume x, ⟨univ, univ_mem_sets, finite_subset h $ subset_univ _⟩ lemma locally_finite_subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, finite_subset ht₂ $ assume i hi, hi.mono $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma is_closed_Union_of_locally_finite {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i), have ∀i, a ∈ -f i, from assume i hi, h $ mem_Union.2 ⟨i, hi⟩, have ∀i, - f i ∈ (𝓝 a), by simp only [mem_nhds_sets_iff]; exact assume i, ⟨- f i, subset.refl _, h₂ i, this i⟩, let ⟨t, h_sets, (h_fin : finite {i | (f i ∩ t).nonempty })⟩ := h₁ a in calc 𝓝 a ≤ principal (t ∩ (⋂ i∈{i | (f i ∩ t).nonempty }, - f i)) : begin rw [le_principal_iff], apply @filter.inter_mem_sets _ (𝓝 a) _ _ h_sets, apply @filter.Inter_mem_sets _ (𝓝 a) _ _ _ h_fin, exact assume i h, this i end ... ≤ principal (- ⋃i, f i) : begin simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq, mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists, exists_imp_distrib, ne_empty_iff_nonempty, set.nonempty], exact assume x xt ht i xfi, ht i x xfi xt xfi end end locally_finite end topological_space section continuous variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] open_locale topological_space /-- A function between topological spaces is continuous if the preimage of every open set is open. -/ def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s) /-- A function between topological spaces is continuous at a point `x₀` if `f x` tends to `f x₀` when `x` tends to `x₀`. -/ def continuous_at (f : α → β) (x : α) := tendsto f (𝓝 x) (𝓝 (f x)) lemma continuous_at.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝 x := h ht lemma preimage_interior_subset_interior_preimage {f : α → β} {s : set β} (hf : continuous f) : f⁻¹' (interior s) ⊆ interior (f⁻¹' s) := interior_maximal (preimage_mono interior_subset) (hf _ is_open_interior) lemma continuous_id : continuous (id : α → α) := assume s h, h lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) : continuous (g ∘ f) := assume s h, hf _ (hg s h) lemma continuous.iterate {f : α → α} (h : continuous f) (n : ℕ) : continuous (f^[n]) := nat.rec_on n continuous_id (λ n ihn, ihn.comp h) lemma continuous_at.comp {g : β → γ} {f : α → β} {x : α} (hg : continuous_at g (f x)) (hf : continuous_at f x) : continuous_at (g ∘ f) x := hg.comp hf lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (𝓝 x) (𝓝 (f x)) := ((nhds_basis_opens x).tendsto_iff $ nhds_basis_opens $ f x).2 $ λ t ⟨hxt, ht⟩, ⟨f ⁻¹' t, ⟨hxt, hf _ ht⟩, subset.refl _⟩ lemma continuous.continuous_at {f : α → β} {x : α} (h : continuous f) : continuous_at f x := h.tendsto x lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (𝓝 x) (𝓝 (f x)), assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ 𝓝 (f a), from λ a ha, mem_nhds_sets hs ha, show is_open (f ⁻¹' s), from is_open_iff_nhds.2 $ λ a ha, le_principal_iff.2 $ hf _ (this a ha)⟩ lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_continuous_at.mpr $ assume a, tendsto_const_nhds lemma continuous_at_const {x : α} {b : β} : continuous_at (λ a:α, b) x := continuous_const.continuous_at lemma continuous_at_id {x : α} : continuous_at id x := continuous_id.continuous_at lemma continuous_at.iterate {f : α → α} {x : α} (hf : continuous_at f x) (hx : f x = x) (n : ℕ) : continuous_at (f^[n]) x := nat.rec_on n continuous_at_id $ λ n ihn, show continuous_at (f^[n] ∘ f) x, from continuous_at.comp (hx.symm ▸ ihn) hf lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, hf (-s) hs, assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma continuous_at_iff_ultrafilter {f : α → β} (x) : continuous_at f x ↔ ∀ g, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) := tendsto_iff_ultrafilter f (𝓝 x) (𝓝 (f x)) lemma continuous_iff_ultrafilter {f : α → β} : continuous f ↔ ∀ x g, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) := by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter] /-- A piecewise defined function `if p then f else g` is continuous, if both `f` and `g` are continuous, and they coincide on the frontier (boundary) of the set `{a | p a}`. -/ lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (λa, @ite (p a) (h a) β (f a) (g a)) := continuous_iff_is_closed.mpr $ assume s hs, have (λa, ite (p a) (f a) (g a)) ⁻¹' s = (closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s), from set.ext $ assume a, classical.by_cases (assume : a ∈ frontier {a | p a}, have hac : a ∈ closure {a | p a}, from this.left, have hai : a ∈ closure {a | ¬ p a}, from have a ∈ - interior {a | p a}, from this.right, by rwa [←closure_compl] at this, by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt}) (assume hf : a ∈ - frontier {a | p a}, classical.by_cases (assume : p a, have hc : a ∈ closure {a | p a}, from subset_closure this, have hnc : a ∉ closure {a | ¬ p a}, by show a ∉ closure (- {a | p a}); rw [closure_compl]; simpa [frontier, hc] using hf, by simp [this, hc, hnc]) (assume : ¬ p a, have hc : a ∈ closure {a | ¬ p a}, from subset_closure this, have hnc : a ∉ closure {a | p a}, begin have hc : a ∈ closure (- {a | p a}), from hc, simp [closure_compl] at hc, simpa [frontier, hc] using hf end, by simp [this, hc, hnc])), by rw [this]; exact is_closed_union (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs) (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs) /- Continuity and partial functions -/ /-- Continuity of a partial function -/ def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s) lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom := by rw [←pfun.preimage_univ]; exact h _ is_open_univ lemma pcontinuous_iff' {f : α →. β} : pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (𝓝 x) (𝓝 y) := begin split, { intros h x y h', simp only [ptendsto'_def, mem_nhds_sets_iff], rintros s ⟨t, tsubs, opent, yt⟩, exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩ }, intros hf s os, rw is_open_iff_nhds, rintros x ⟨y, ys, fxy⟩ t, rw [mem_principal_sets], assume h : f.preimage s ⊆ t, change t ∈ 𝓝 x, apply mem_sets_of_superset _ h, have h' : ∀ s ∈ 𝓝 y, f.preimage s ∈ 𝓝 x, { intros s hs, have : ptendsto' f (𝓝 x) (𝓝 y) := hf fxy, rw ptendsto'_def at this, exact this s hs }, show f.preimage s ∈ 𝓝 x, apply h', rw mem_nhds_sets_iff, exact ⟨s, set.subset.refl _, os, ys⟩ end lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := have ∀ (a : α), 𝓝 a ⊓ principal s ≠ ⊥ → 𝓝 (f a) ⊓ principal (f '' s) ≠ ⊥, from assume a ha, have h₁ : ¬ map f (𝓝 a ⊓ principal s) = ⊥, by rwa[map_eq_bot_iff], have h₂ : map f (𝓝 a ⊓ principal s) ≤ 𝓝 (f a) ⊓ principal (f '' s), from le_inf (le_trans (map_mono inf_le_left) $ by rw [continuous_iff_continuous_at] at h; exact h a) (le_trans (map_mono inf_le_right) $ by simp; exact subset.refl _), ne_bot_of_le_ne_bot h₁ h₂, by simp [image_subset_iff, closure_eq_nhds]; assumption lemma mem_closure {s : set α} {t : set β} {f : α → β} {a : α} (hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t := subset.trans (image_closure_subset_closure_image hf) (closure_mono $ image_subset_iff.2 ht) $ (mem_image_of_mem f ha) end continuous
1a9bc549b948ac3d71f187f5c838a558a18f2302
97c8e5d8aca4afeebb5b335f26a492c53680efc8
/ground_zero/cubical/square.lean
a452e545feec22523aff0caae46c41ac7c17ec23
[]
no_license
jfrancese/lean
cf32f0d8d5520b6f0e9d3987deb95841c553c53c
06e7efaecce4093d97fb5ecc75479df2ef1dbbdb
refs/heads/master
1,587,915,151,351
1,551,012,140,000
1,551,012,140,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,202
lean
import ground_zero.cubical.path open ground_zero.HITs (I) ground_zero.cubical ground_zero.types /- * Constant square. * Connections (∧, ∨) in squares. -/ namespace ground_zero.cubical.Square universe u def lam {α : Sort u} (f : I → I → α) : Square (f 0) (f 1) (<i> f i 0) (<i> f i 1) := Cube.lambda 1 (product.elim f) def const {α : Sort u} (a : α) : Square (λ _, a) (λ _, a) (<i> a) (<i> a) := lam (λ i j, a) /- p a -----------------> b ^ ^ | | | | <j> a | and p | p | | | | | | a -----------------> a <i> a -/ def and {α : Sort u} {a b : α} (p : a ⇝ b) : Square (λ _, a) (λ i, p # i) (<i> a) p := lam (λ i j, p # i ∧ j) def or {α : Sort u} {a b : α} (p : a ⇝ b) : Square (λ i, p # i) (λ _, b) p (<i> b) := lam (λ i j, p # i ∨ j) /- def Square.compute {α : Sort u} {m n : I → α} {o : m 0 ⇝ n 0} {p : m 1 ⇝ n 1} (s : Square m n o p) : Π i, m i ⇝ n i -/ end ground_zero.cubical.Square
0140ee59fdf48bcc616e44ae34cc79112a9ec136
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/interactive/hover.lean
ddca593abebac1c29c70695d39f5b1555dbb952b
[ "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,637
lean
import Lean example : True := by apply True.intro --^ textDocument/hover example : True := by simp [True.intro] --^ textDocument/hover example (n : Nat) : True := by match n with | Nat.zero => _ --^ textDocument/hover | n + 1 => _ /-- My tactic -/ macro "mytac" o:"only"? e:term : tactic => `(exact $e) example : True := by mytac only True.intro --^ textDocument/hover --^ textDocument/hover --^ textDocument/hover /-- My way better tactic -/ macro_rules | `(tactic| mytac $[only]? $e) => `(apply $e) example : True := by mytac only True.intro --^ textDocument/hover /-- My ultimate tactic -/ elab_rules : tactic | `(tactic| mytac $[only]? $e) => `(tactic| refine $e) >>= Lean.Elab.Tactic.evalTactic example : True := by mytac only True.intro --^ textDocument/hover /-- My notation -/ macro "mynota" e:term : term => e #check mynota 1 --^ textDocument/hover /-- My way better notation -/ macro_rules | `(mynota $e) => `(2 * $e) #check mynota 1 --^ textDocument/hover -- macro_rules take precedence over elab_rules for term/command, so use new syntax syntax "mynota'" term : term /-- My ultimate notation -/ elab_rules : term | `(mynota' $e) => `($e * $e) >>= (Lean.Elab.Term.elabTerm · none) #check mynota' 1 --^ textDocument/hover /-- My command -/ macro "mycmd" e:term : command => `(def hi := $e) mycmd 1 --^ textDocument/hover /-- My way better command -/ macro_rules | `(mycmd $e) => `(@[inline] def hi := $e) mycmd 1 --^ textDocument/hover syntax "mycmd'" term : command /-- My ultimate command -/ elab_rules : command | `(mycmd' $e) => `(/-- hi -/ @[inline] def hi := $e) >>= Lean.Elab.Command.elabCommand mycmd' 1 --^ textDocument/hover #check ({ a := }) -- should not show `sorry` --^ textDocument/hover example : True := by simp [id True.intro] --^ textDocument/hover --^ textDocument/hover example : Id Nat := do let mut n := 1 n := 2 --^ textDocument/hover n constant foo : Nat #check _root_.foo --^ textDocument/hover namespace Bar constant foo : Nat --^ textDocument/hover #check _root_.foo --^ textDocument/hover def bar := 1 --^ textDocument/hover structure Foo := mk :: --^ textDocument/hover --^ textDocument/hover hi : Nat --^ textDocument/hover inductive Bar --^ textDocument/hover | mk : Bar --^ textDocument/hover instance : ToString Nat := ⟨toString⟩ --^ textDocument/hover instance f : ToString Nat := ⟨toString⟩ --^ textDocument/hover example : Type 0 := Nat --^ textDocument/hover
28c13571ec333580df047bc7f5b53652b638fce0
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/scope_bug1.lean
c99245fc96e8a305c7e20e84c5ca7ced0d87d088
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
430
lean
variables a b c d : Nat axiom H : a + (b + c) = a + (b + d) set_option pp::implicit true using Nat check add_succr a scope theorem mul_zerol2 (a : Nat) : 0 * a = 0 := induction_on a (show 0 * 0 = 0, from mul_zeror 0) (λ (n : Nat) (iH : 0 * n = 0), calc 0 * (n + 1) = (0 * n) + 0 : mul_succr 0 n ... = 0 + 0 : { iH } ... = 0 : add_zeror 0) end
340b6377e42d8f75218de58167eb8fb835a0962b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/quadratic_form/complex.lean
9a42c8445a840b065bb0d422c32d0e9a0515808c
[ "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
4,291
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Kexing Ying, Eric Wieser -/ import linear_algebra.quadratic_form.isometry import analysis.special_functions.pow.complex /-! # Quadratic forms over the complex numbers > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. `equivalent_sum_squares`: A nondegenerate quadratic form over the complex numbers is equivalent to a sum of squares. -/ namespace quadratic_form open_locale big_operators open finset variables {ι : Type*} [fintype ι] /-- The isometry between a weighted sum of squares on the complex numbers and the sum of squares, i.e. `weighted_sum_squares` with weights 1 or 0. -/ noncomputable def isometry_sum_squares [decidable_eq ι] (w' : ι → ℂ) : isometry (weighted_sum_squares ℂ w') (weighted_sum_squares ℂ (λ i, if w' i = 0 then 0 else 1 : ι → ℂ)) := begin let w := λ i, if h : w' i = 0 then (1 : units ℂ) else units.mk0 (w' i) h, have hw' : ∀ i : ι, (w i : ℂ) ^ - (1 / 2 : ℂ) ≠ 0, { intros i hi, exact (w i).ne_zero ((complex.cpow_eq_zero_iff _ _).1 hi).1 }, convert (weighted_sum_squares ℂ w').isometry_basis_repr ((pi.basis_fun ℂ ι).units_smul (λ i, (is_unit_iff_ne_zero.2 $ hw' i).unit)), ext1 v, erw [basis_repr_apply, weighted_sum_squares_apply, weighted_sum_squares_apply], refine sum_congr rfl (λ j hj, _), have hsum : (∑ (i : ι), v i • ((is_unit_iff_ne_zero.2 $ hw' i).unit : ℂ) • (pi.basis_fun ℂ ι) i) j = v j • w j ^ - (1 / 2 : ℂ), { rw [finset.sum_apply, sum_eq_single j, pi.basis_fun_apply, is_unit.unit_spec, linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_same, smul_eq_mul, smul_eq_mul, smul_eq_mul, mul_one], intros i _ hij, rw [pi.basis_fun_apply, linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_noteq hij.symm, pi.zero_apply, smul_eq_mul, smul_eq_mul, mul_zero, mul_zero], intro hj', exact false.elim (hj' hj) }, simp_rw basis.units_smul_apply, erw [hsum, smul_eq_mul], split_ifs, { simp only [h, zero_smul, zero_mul]}, have hww' : w' j = w j, { simp only [w, dif_neg h, units.coe_mk0] }, simp only [hww', one_mul], change v j * v j = ↑(w j) * ((v j * ↑(w j) ^ -(1 / 2 : ℂ)) * (v j * ↑(w j) ^ -(1 / 2 : ℂ))), suffices : v j * v j = w j ^ - (1 / 2 : ℂ) * w j ^ - (1 / 2 : ℂ) * w j * v j * v j, { rw [this], ring }, rw [← complex.cpow_add _ _ (w j).ne_zero, show -(1 / 2 : ℂ) + -(1 / 2) = -1, by simp [← two_mul], complex.cpow_neg_one, inv_mul_cancel (w j).ne_zero, one_mul], end /-- The isometry between a weighted sum of squares on the complex numbers and the sum of squares, i.e. `weighted_sum_squares` with weight `λ i : ι, 1`. -/ noncomputable def isometry_sum_squares_units [decidable_eq ι] (w : ι → units ℂ) : isometry (weighted_sum_squares ℂ w) (weighted_sum_squares ℂ (1 : ι → ℂ)) := begin have hw1 : (λ i, if (w i : ℂ) = 0 then 0 else 1 : ι → ℂ) = 1, { ext i : 1, exact dif_neg (w i).ne_zero }, have := isometry_sum_squares (coe ∘ w), rw hw1 at this, exact this, end /-- A nondegenerate quadratic form on the complex numbers is equivalent to the sum of squares, i.e. `weighted_sum_squares` with weight `λ i : ι, 1`. -/ theorem equivalent_sum_squares {M : Type*} [add_comm_group M] [module ℂ M] [finite_dimensional ℂ M] (Q : quadratic_form ℂ M) (hQ : (associated Q).nondegenerate) : equivalent Q (weighted_sum_squares ℂ (1 : fin (finite_dimensional.finrank ℂ M) → ℂ)) := let ⟨w, ⟨hw₁⟩⟩ := Q.equivalent_weighted_sum_squares_units_of_nondegenerate' hQ in ⟨hw₁.trans (isometry_sum_squares_units w)⟩ /-- All nondegenerate quadratic forms on the complex numbers are equivalent. -/ theorem complex_equivalent {M : Type*} [add_comm_group M] [module ℂ M] [finite_dimensional ℂ M] (Q₁ Q₂ : quadratic_form ℂ M) (hQ₁ : (associated Q₁).nondegenerate) (hQ₂ : (associated Q₂).nondegenerate) : equivalent Q₁ Q₂ := (Q₁.equivalent_sum_squares hQ₁).trans (Q₂.equivalent_sum_squares hQ₂).symm end quadratic_form
36edb9375a8bc0cc1915455ceca4195fb17ab425
4ec0e92c725fad3fd2871a0ab050a7da1c719444
/src/mywork/practice_1.lean
465a429cf2a61bd6dca8c8f70a72f5d3c0622619
[]
no_license
mitchelltaylor06/cs2120f21
cc2c2b61a7e45c07faa849fcb8a66eb948753a25
efb71a748d7c76e24834d03b8f01c3ae084c1756
refs/heads/main
1,693,841,444,092
1,633,713,850,000
1,633,713,850,000
399,946,415
0
0
null
null
null
null
UTF-8
Lean
false
false
6,049
lean
/- EQUALITY -/ /- #1 Suppose that x, y, z, and w are arbitrary objects of some type, T; and suppose further that we know (have proofs of the facts) that x = y, y = z, and w = z. Give a very, very short English proof of the conjecture that z = w. You can use not only the axioms of equality, but either of the theorems about properties of equality that we have proven. Hint: There's something about this question that makes it much easier to answer than it might at first appear. -/ /- By the symmetric theorem of equality, if w = z then z = w -/ /- #2 Give a formal statement of the conjecture (proposition) from #1 by filling in the "hole" in the following definition. The def is a keyword. The name you're binding to your proposition is prop_1. The type of the value is Prop (which is the type of all propositions in Lean). -/ def prop_1 : Prop := ∀ (T : Type) (w z : T), w = z → z = w /- #3 (extra credit) Give a formal proof of the proposition from #2 by filling in the hole in this next definition. Hint: Use Lean's versions of the axioms and basic theorems concerning equality. They are, again, called eq.refl, eq.subst, eq.symm, eq.trans. -/ theorem prop_1_proof : prop_1 := begin unfold prop_1, assume T w z e, rw e, end /- FOR ALL: ∀. -/ /- #4 Give a very brief explanation in English of the introduction rule for ∀. For example, suppose you need to prove (∀ x, P x); what do you do? (I'm being a little informal in leaving out the type of X.) -/ /- Assume you’re given an arbitrary but specific object if you can prove it true then it is true for all -/ /- #5 Suppose you have a proof, let's call it pf, of the proposition, (∀ x, P x), and you need a proof of P t, for some particular t. Write an expression then uses the elimination rule for ∀ to get such a proof. Complete the answer by replacing the underscores in the following expression: ( x → t ). -/ /- -/ /- IMPLIES: → In the "code" that follows, we define two predicates, each taking one natural number as an argument. We call them ev and odd. When applied to any value, n, ev yields the proposition that n is even (n % 2 = 0), while odd yields the proposition that n is odd (n % 2 = 1). -/ def ev (n : ℕ) := n % 2 = 0 def odd (n : ℕ) := n % 2 = 1 /- #6 Write a formal version of the proposition that, for *any* natural number n, *if* n is even, *then* n + 1 is odd. Give your answer by filling the hole in the following definition. Hint: put parenthesis around "n + 1" in your answer. -/ def successor_of_even_is_odd : Prop := ∀ (n : ℕ), ev n → odd (n+1) /- #7 Suppose that "its_raining" and "the_streets_are_wet" are propositions. (We formalize these assumptions as axioms in what follows. Then give a formal definition of the (larger) proposition, "if it's raining out then the streets are wet") by filling in the hole -/ axioms (raining streets_wet : Prop) axiom if_raining_then_streets_wet : raining → streets_wet /- #9 Now suppose that in addition, its_raining is true, and we have a proof of it, pf_its_raining. Again, we again give you this assumption formally as an axiom below. Finish the formal proof that the streets must be wet. Hint: here you are asked to use the elimination rule for →. -/ axiom pf_raining : raining example : streets_wet := if_raining_then_streets_wet pf_raining /- AND: ∧ -/ /- #10 In our last class, we proved that "∧ is *commutative*." That is, for any given *propositions*, P and Q, (P ∧ Q) → (Q ∧ P). The way we proved it was to *assume* that we're given such a P, Q, and proof, pq, of (P ∧ Q) -- applying the introduction rules for ∀ and →). In this context, we *use* the proof, pq, to derive separate proofs, let's call them p, a proof of P, and q, a proof of Q. With these in hand, we then apply the introduction rule for ∧ to put them back together into a proof of (Q ∧ P). We give you a formal version of this proof as a reminder, next. -/ theorem and_commutative : ∀ (P Q : Prop), P ∧ Q → Q ∧ P := begin assume P Q pq, apply and.intro _ _, exact (and.elim_right pq), exact (and.elim_left pq), end /- Your task now is to prove the theorem, "∧ is *associative*." What this means is that for arbitrary propositions, P, Q, and R, if (P ∧ (Q ∧ R)) is true, then ((P ∧ Q) ∧ R) is true, *and vice versa*. You just need to prove it in the first direction. Hint, if you have a proof, p_qr, of (P ∧ (Q ∧ R)), then the application of and.elim_left will give you a proof of P, and and.elim_right will give you a proof of (Q ∧ R). To help you along, we give you the first part of the proof, including an example of a new Lean tactic called have, which allows you to give a name to a new value in the middle of a proof script. -/ theorem and_associative : ∀ (P Q R : Prop), (P ∧ (Q ∧ R)) → ((P ∧ Q) ∧ R) := begin intros P Q R h, have p : P := and.elim_left h, have qr : Q ∧ R := and.elim_right h, have q : Q := and.elim_left qr, have r : R := and.elim_right qr, exact and.intro (and.intro p q) r, end /- #11 Give an English language proof of the preceding theorem. Do it by finishing off the following partial "proof explanation." Proof. We assume that P, Q, and R are arbitrary but specific propositions, and that we have a proof, let's call it p_qr, of (P ∧ (Q ∧ R)) [by application of ∧ and → introduction.] What now remains to be proved is ((P ∧ Q) ∧ R). We can construct a proof of this proposition by applying the elimination rule of and to a proof of (P ∧ Q) and a proof of R. What remains, then, is to obtain these proofs. But this is easily done by the application of the and introduction rules to p q and r. QED. -/ /- Note that Lean includes versions of these theorems (and many, many, many others) in its extensive library of formalized maths, as the following check commands reveal. Note the difference in naming relative to the definitions we give in this file. -/ #check @and.comm #check @and.assoc
2b3ceef78c689ff9c73f33d6c62553861e64c4f6
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/439.lean
dc17bd06879d8f94edecd1477dd34fcfa90bdbae
[ "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
807
lean
set_option pp.analyze.trustCoe true universe u structure Fn (E I : Sort u) := (exp : E) (imp : I) instance (E I : Sort u) : CoeFun (Fn E I) (fun _ => I) := {coe := fun K => K.imp} class Bar.{w} (P : Sort u) := fn : P -> Sort w variable {P : Sort u} (B : Bar P) variable (fn : Fn ((p : P) -> B.fn p) ({p : P} -> B.fn p)) #check (@fn : {p : P} → Bar.fn p) -- Result is as expected (implicit) /- fn.imp : {p : P} → Bar.fn p -/ variable (p : P) variable (Bp : Bar.fn p) #check fn Bp /- application type mismatch Fn.imp fn Bp argument Bp has type Bar.fn p : Sort ?u.635 but is expected to have type P : Sort u -/ #check fn p #check fn (p := p) variable (fn' : Fn ((p : P) -> B.fn p -> B.fn p) ({p : P} -> B.fn p -> B.fn p)) -- Expect no error #check fn' Bp -- Expect error #check fn' p
65a5399376961e241dec9abaef5b827fd171f87c
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/measure_theory/prod.lean
99a6211cce9d52de802ea99047af98ae7408ced2
[ "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
46,972
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import measure_theory.giry_monad import measure_theory.set_integral /-! # The product measure In this file we define and prove properties about the binary product measure. If `α` and `β` have σ-finite measures `μ` resp. `ν` then `α × β` can be equipped with a σ-finite measure `μ.prod ν` that satisfies `(μ.prod ν) s = ∫⁻ x, ν {y | (x, y) ∈ s} ∂μ`. We also have `(μ.prod ν) (s.prod t) = μ s * ν t`, i.e. the measure of a rectangle is the product of the measures of the sides. We also prove Tonelli's theorem and Fubini's theorem. ## Main definition * `measure_theory.measure.prod`: The product of two measures. ## Main results * `measure_theory.measure.prod_apply` states `μ.prod ν s = ∫⁻ x, ν {y | (x, y) ∈ s} ∂μ` for measurable `s`. `measure_theory.measure.prod_apply_symm` is the reversed version. * `measure_theory.measure.prod_prod` states `μ.prod ν (s.prod t) = μ s * ν t` for measurable sets `s` and `t`. * `measure_theory.lintegral_prod`: Tonelli's theorem. It states that for a measurable function `α × β → ℝ≥0∞` we have `∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ x, ∫⁻ y, f (x, y) ∂ν ∂μ`. The version for functions `α → β → ℝ≥0∞` is reversed, and called `lintegral_lintegral`. Both versions have a variant with `_symm` appended, where the order of integration is reversed. The lemma `measurable.lintegral_prod_right'` states that the inner integral of the right-hand side is measurable. * `measure_theory.integrable_prod_iff` states that a binary function is integrable iff both * `y ↦ f (x, y)` is integrable for almost every `x`, and * the function `x ↦ ∫ ∥f (x, y)∥ dy` is integrable. * `measure_theory.integral_prod`: Fubini's theorem. It states that for a integrable function `α × β → E` (where `E` is a second countable Banach space) we have `∫ z, f z ∂(μ.prod ν) = ∫ x, ∫ y, f (x, y) ∂ν ∂μ`. This theorem has the same variants as Tonelli's theorem. The lemma `measure_theory.integrable.integral_prod_right` states that the inner integral of the right-hand side is integrable. ## Implementation Notes Many results are proven twice, once for functions in curried form (`α → β → γ`) and one for functions in uncurried form (`α × β → γ`). The former often has an assumption `measurable (uncurry f)`, which could be inconvenient to discharge, but for the latter it is more common that the function has to be given explicitly, since Lean cannot synthesize the function by itself. We name the lemmas about the uncurried form with a prime. Tonelli's theorem and Fubini's theorem have a different naming scheme, since the version for the uncurried version is reversed. ## Tags product measure, Fubini's theorem, Tonelli's theorem, Fubini-Tonelli theorem -/ noncomputable theory open_locale classical topological_space ennreal measure_theory open set function real ennreal open measure_theory measurable_space measure_theory.measure open topological_space (hiding generate_from) open filter (hiding prod_eq map) variables {α α' β β' γ E : Type*} /-- Rectangles formed by π-systems form a π-system. -/ lemma is_pi_system.prod {C : set (set α)} {D : set (set β)} (hC : is_pi_system C) (hD : is_pi_system D) : is_pi_system (image2 set.prod C D) := begin rintro _ _ ⟨s₁, t₁, hs₁, ht₁, rfl⟩ ⟨s₂, t₂, hs₂, ht₂, rfl⟩ hst, rw [prod_inter_prod] at hst ⊢, rw [prod_nonempty_iff] at hst, exact mem_image2_of_mem (hC _ _ hs₁ hs₂ hst.1) (hD _ _ ht₁ ht₂ hst.2) end /-- Rectangles of countably spanning sets are countably spanning. -/ lemma is_countably_spanning.prod {C : set (set α)} {D : set (set β)} (hC : is_countably_spanning C) (hD : is_countably_spanning D) : is_countably_spanning (image2 set.prod C D) := begin rcases ⟨hC, hD⟩ with ⟨⟨s, h1s, h2s⟩, t, h1t, h2t⟩, refine ⟨λ n, (s n.unpair.1).prod (t n.unpair.2), λ n, mem_image2_of_mem (h1s _) (h1t _), _⟩, rw [Union_unpair_prod, h2s, h2t, univ_prod_univ] end variables [measurable_space α] [measurable_space α'] [measurable_space β] [measurable_space β'] variables [measurable_space γ] variables {μ : measure α} {ν : measure β} {τ : measure γ} variables [normed_group E] [measurable_space E] /-! ### Measurability Before we define the product measure, we can talk about the measurability of operations on binary functions. We show that if `f` is a binary measurable function, then the function that integrates along one of the variables (using either the Lebesgue or Bochner integral) is measurable. -/ /-- The product of generated σ-algebras is the one generated by rectangles, if both generating sets are countably spanning. -/ lemma generate_from_prod_eq {α β} {C : set (set α)} {D : set (set β)} (hC : is_countably_spanning C) (hD : is_countably_spanning D) : @prod.measurable_space _ _ (generate_from C) (generate_from D) = generate_from (image2 set.prod C D) := begin apply le_antisymm, { refine sup_le _ _; rw [comap_generate_from]; apply generate_from_le; rintro _ ⟨s, hs, rfl⟩, { rcases hD with ⟨t, h1t, h2t⟩, rw [← prod_univ, ← h2t, prod_Union], apply measurable_set.Union, intro n, apply measurable_set_generate_from, exact ⟨s, t n, hs, h1t n, rfl⟩ }, { rcases hC with ⟨t, h1t, h2t⟩, rw [← univ_prod, ← h2t, Union_prod_const], apply measurable_set.Union, rintro n, apply measurable_set_generate_from, exact mem_image2_of_mem (h1t n) hs } }, { apply generate_from_le, rintro _ ⟨s, t, hs, ht, rfl⟩, rw [prod_eq], apply (measurable_fst _).inter (measurable_snd _), { exact measurable_set_generate_from hs }, { exact measurable_set_generate_from ht } } end /-- If `C` and `D` generate the σ-algebras on `α` resp. `β`, then rectangles formed by `C` and `D` generate the σ-algebra on `α × β`. -/ lemma generate_from_eq_prod {C : set (set α)} {D : set (set β)} (hC : generate_from C = ‹_›) (hD : generate_from D = ‹_›) (h2C : is_countably_spanning C) (h2D : is_countably_spanning D) : generate_from (image2 set.prod C D) = prod.measurable_space := by rw [← hC, ← hD, generate_from_prod_eq h2C h2D] /-- The product σ-algebra is generated from boxes, i.e. `s.prod t` for sets `s : set α` and `t : set β`. -/ lemma generate_from_prod : generate_from (image2 set.prod {s : set α | measurable_set s} {t : set β | measurable_set t}) = prod.measurable_space := generate_from_eq_prod generate_from_measurable_set generate_from_measurable_set is_countably_spanning_measurable_set is_countably_spanning_measurable_set /-- Rectangles form a π-system. -/ lemma is_pi_system_prod : is_pi_system (image2 set.prod {s : set α | measurable_set s} {t : set β | measurable_set t}) := is_pi_system_measurable_set.prod is_pi_system_measurable_set /-- If `ν` is a finite measure, and `s ⊆ α × β` is measurable, then `x ↦ ν { y | (x, y) ∈ s }` is a measurable function. `measurable_measure_prod_mk_left` is strictly more general. -/ lemma measurable_measure_prod_mk_left_finite [finite_measure ν] {s : set (α × β)} (hs : measurable_set s) : measurable (λ x, ν (prod.mk x ⁻¹' s)) := begin refine induction_on_inter generate_from_prod.symm is_pi_system_prod _ _ _ _ hs, { simp [measurable_zero, const_def] }, { rintro _ ⟨s, t, hs, ht, rfl⟩, simp only [mk_preimage_prod_right_eq_if, measure_if], exact measurable_const.indicator hs }, { intros t ht h2t, simp_rw [preimage_compl, measure_compl (measurable_prod_mk_left ht) (measure_lt_top ν _)], exact h2t.const_sub _ }, { intros f h1f h2f h3f, simp_rw [preimage_Union], have : ∀ b, ν (⋃ i, prod.mk b ⁻¹' f i) = ∑' i, ν (prod.mk b ⁻¹' f i) := λ b, measure_Union (λ i j hij, disjoint.preimage _ (h1f i j hij)) (λ i, measurable_prod_mk_left (h2f i)), simp_rw [this], apply measurable.ennreal_tsum h3f }, end /-- If `ν` is a σ-finite measure, and `s ⊆ α × β` is measurable, then `x ↦ ν { y | (x, y) ∈ s }` is a measurable function. -/ lemma measurable_measure_prod_mk_left [sigma_finite ν] {s : set (α × β)} (hs : measurable_set s) : measurable (λ x, ν (prod.mk x ⁻¹' s)) := begin have : ∀ x, measurable_set (prod.mk x ⁻¹' s) := λ x, measurable_prod_mk_left hs, simp only [← @supr_restrict_spanning_sets _ _ ν, this], apply measurable_supr, intro i, haveI := fact.mk (measure_spanning_sets_lt_top ν i), exact measurable_measure_prod_mk_left_finite hs end /-- If `μ` is a σ-finite measure, and `s ⊆ α × β` is measurable, then `y ↦ μ { x | (x, y) ∈ s }` is a measurable function. -/ lemma measurable_measure_prod_mk_right {μ : measure α} [sigma_finite μ] {s : set (α × β)} (hs : measurable_set s) : measurable (λ y, μ ((λ x, (x, y)) ⁻¹' s)) := measurable_measure_prod_mk_left (measurable_set_swap_iff.mpr hs) lemma measurable.map_prod_mk_left [sigma_finite ν] : measurable (λ x : α, map (prod.mk x) ν) := begin apply measurable_of_measurable_coe, intros s hs, simp_rw [map_apply measurable_prod_mk_left hs], exact measurable_measure_prod_mk_left hs end lemma measurable.map_prod_mk_right {μ : measure α} [sigma_finite μ] : measurable (λ y : β, map (λ x : α, (x, y)) μ) := begin apply measurable_of_measurable_coe, intros s hs, simp_rw [map_apply measurable_prod_mk_right hs], exact measurable_measure_prod_mk_right hs end /-- The Lebesgue integral is measurable. This shows that the integrand of (the right-hand-side of) Tonelli's theorem is measurable. -/ lemma measurable.lintegral_prod_right' [sigma_finite ν] : ∀ {f : α × β → ℝ≥0∞} (hf : measurable f), measurable (λ x, ∫⁻ y, f (x, y) ∂ν) := begin have m := @measurable_prod_mk_left, refine measurable.ennreal_induction _ _ _, { intros c s hs, simp only [← indicator_comp_right], suffices : measurable (λ x, c * ν (prod.mk x ⁻¹' s)), { simpa [lintegral_indicator _ (m hs)] }, exact (measurable_measure_prod_mk_left hs).const_mul _ }, { rintro f g - hf hg h2f h2g, simp_rw [pi.add_apply, lintegral_add (hf.comp m) (hg.comp m)], exact h2f.add h2g }, { intros f hf h2f h3f, have := measurable_supr h3f, have : ∀ x, monotone (λ n y, f n (x, y)) := λ x i j hij y, h2f hij (x, y), simpa [lintegral_supr (λ n, (hf n).comp m), this] } end /-- The Lebesgue integral is measurable. This shows that the integrand of (the right-hand-side of) Tonelli's theorem is measurable. This version has the argument `f` in curried form. -/ lemma measurable.lintegral_prod_right [sigma_finite ν] {f : α → β → ℝ≥0∞} (hf : measurable (uncurry f)) : measurable (λ x, ∫⁻ y, f x y ∂ν) := hf.lintegral_prod_right' /-- The Lebesgue integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Tonelli's theorem is measurable. -/ lemma measurable.lintegral_prod_left' [sigma_finite μ] {f : α × β → ℝ≥0∞} (hf : measurable f) : measurable (λ y, ∫⁻ x, f (x, y) ∂μ) := (measurable_swap_iff.mpr hf).lintegral_prod_right' /-- The Lebesgue integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Tonelli's theorem is measurable. This version has the argument `f` in curried form. -/ lemma measurable.lintegral_prod_left [sigma_finite μ] {f : α → β → ℝ≥0∞} (hf : measurable (uncurry f)) : measurable (λ y, ∫⁻ x, f x y ∂μ) := hf.lintegral_prod_left' lemma measurable_set_integrable [sigma_finite ν] [opens_measurable_space E] ⦃f : α → β → E⦄ (hf : measurable (uncurry f)) : measurable_set { x | integrable (f x) ν } := begin simp_rw [integrable, hf.of_uncurry_left.ae_measurable, true_and], exact measurable_set_lt (measurable.lintegral_prod_right hf.ennnorm) measurable_const end section variables [second_countable_topology E] [normed_space ℝ E] [complete_space E] [borel_space E] /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) Fubini's theorem is measurable. This version has `f` in curried form. -/ lemma measurable.integral_prod_right [sigma_finite ν] ⦃f : α → β → E⦄ (hf : measurable (uncurry f)) : measurable (λ x, ∫ y, f x y ∂ν) := begin let s : ℕ → simple_func (α × β) E := simple_func.approx_on _ hf univ _ (mem_univ 0), let s' : ℕ → α → simple_func β E := λ n x, (s n).comp (prod.mk x) measurable_prod_mk_left, let f' : ℕ → α → E := λ n, {x | integrable (f x) ν}.indicator (λ x, (s' n x).integral ν), have hf' : ∀ n, measurable (f' n), { intro n, refine measurable.indicator _ (measurable_set_integrable hf), have : ∀ x, (s' n x).range.filter (λ x, x ≠ 0) ⊆ (s n).range, { intros x, refine finset.subset.trans (finset.filter_subset _ _) _, intro y, simp_rw [simple_func.mem_range], rintro ⟨z, rfl⟩, exact ⟨(x, z), rfl⟩ }, simp only [simple_func.integral_eq_sum_of_subset (this _)], refine finset.measurable_sum _ (λ x _, _), refine (measurable.to_real _).smul_const _, simp only [simple_func.coe_comp, preimage_comp] {single_pass := tt}, apply measurable_measure_prod_mk_left, exact (s n).measurable_set_fiber x }, have h2f' : tendsto f' at_top (𝓝 (λ (x : α), ∫ (y : β), f x y ∂ν)), { rw [tendsto_pi], intro x, by_cases hfx : integrable (f x) ν, { have : ∀ n, integrable (s' n x) ν, { intro n, apply (hfx.norm.add hfx.norm).mono' (s' n x).measurable.ae_measurable, apply eventually_of_forall, intro y, simp_rw [s', simple_func.coe_comp], exact simple_func.norm_approx_on_zero_le _ _ (x, y) n }, simp only [f', hfx, simple_func.integral_eq_integral _ (this _), indicator_of_mem, mem_set_of_eq], refine tendsto_integral_of_dominated_convergence (λ y, ∥f x y∥ + ∥f x y∥) (λ n, (s' n x).ae_measurable) hf.of_uncurry_left.ae_measurable (hfx.norm.add hfx.norm) _ _, { exact λ n, eventually_of_forall (λ y, simple_func.norm_approx_on_zero_le _ _ (x, y) n) }, { exact eventually_of_forall (λ y, simple_func.tendsto_approx_on _ _ (by simp)) } }, { simpa [f', hfx, integral_undef] using @tendsto_const_nhds _ _ _ (0 : E) _, } }, exact measurable_of_tendsto_metric hf' h2f' end /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) Fubini's theorem is measurable. -/ lemma measurable.integral_prod_right' [sigma_finite ν] ⦃f : α × β → E⦄ (hf : measurable f) : measurable (λ x, ∫ y, f (x, y) ∂ν) := by { rw [← uncurry_curry f] at hf, exact hf.integral_prod_right } /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Fubini's theorem is measurable. This version has `f` in curried form. -/ lemma measurable.integral_prod_left [sigma_finite μ] ⦃f : α → β → E⦄ (hf : measurable (uncurry f)) : measurable (λ y, ∫ x, f x y ∂μ) := (hf.comp measurable_swap).integral_prod_right' /-- The Bochner integral is measurable. This shows that the integrand of (the right-hand-side of) the symmetric version of Fubini's theorem is measurable. -/ lemma measurable.integral_prod_left' [sigma_finite μ] ⦃f : α × β → E⦄ (hf : measurable f) : measurable (λ y, ∫ x, f (x, y) ∂μ) := (hf.comp measurable_swap).integral_prod_right' end /-! ### The product measure -/ namespace measure_theory namespace measure /-- The binary product of measures. They are defined for arbitrary measures, but we basically prove all properties under the assumption that at least one of them is σ-finite. -/ @[irreducible] protected def prod (μ : measure α) (ν : measure β) : measure (α × β) := bind μ $ λ x : α, map (prod.mk x) ν instance prod.measure_space {α β} [measure_space α] [measure_space β] : measure_space (α × β) := { volume := volume.prod volume } variables {μ ν} [sigma_finite ν] lemma prod_apply {s : set (α × β)} (hs : measurable_set s) : μ.prod ν s = ∫⁻ x, ν (prod.mk x ⁻¹' s) ∂μ := by simp_rw [measure.prod, bind_apply hs measurable.map_prod_mk_left, map_apply measurable_prod_mk_left hs] @[simp] lemma prod_prod {s : set α} {t : set β} (hs : measurable_set s) (ht : measurable_set t) : μ.prod ν (s.prod t) = μ s * ν t := by simp_rw [prod_apply (hs.prod ht), mk_preimage_prod_right_eq_if, measure_if, lintegral_indicator _ hs, lintegral_const, restrict_apply measurable_set.univ, univ_inter, mul_comm] local attribute [instance] nonempty_measurable_superset /-- If we don't assume measurability of `s` and `t`, we can bound the measure of their product. -/ lemma prod_prod_le (s : set α) (t : set β) : μ.prod ν (s.prod t) ≤ μ s * ν t := begin by_cases hs0 : μ s = 0, { rcases (exists_measurable_superset_of_null hs0) with ⟨s', hs', h2s', h3s'⟩, convert measure_mono (prod_mono hs' (subset_univ _)), simp_rw [hs0, prod_prod h2s' measurable_set.univ, h3s', zero_mul] }, by_cases hti : ν t = ∞, { convert le_top, simp_rw [hti, ennreal.mul_top, hs0, if_false] }, rw [measure_eq_infi' μ], simp_rw [ennreal.infi_mul hti], refine le_infi _, rintro ⟨s', h1s', h2s'⟩, rw [subtype.coe_mk], by_cases ht0 : ν t = 0, { rcases (exists_measurable_superset_of_null ht0) with ⟨t', ht', h2t', h3t'⟩, convert measure_mono (prod_mono (subset_univ _) ht'), simp_rw [ht0, prod_prod measurable_set.univ h2t', h3t', mul_zero] }, by_cases hsi : μ s' = ∞, { convert le_top, simp_rw [hsi, ennreal.top_mul, ht0, if_false] }, rw [measure_eq_infi' ν], simp_rw [ennreal.mul_infi hsi], refine le_infi _, rintro ⟨t', h1t', h2t'⟩, convert measure_mono (prod_mono h1s' h1t'), simp [prod_prod h2s' h2t'], end lemma ae_measure_lt_top {s : set (α × β)} (hs : measurable_set s) (h2s : (μ.prod ν) s < ∞) : ∀ᵐ x ∂μ, ν (prod.mk x ⁻¹' s) < ∞ := by { simp_rw [prod_apply hs] at h2s, refine ae_lt_top (measurable_measure_prod_mk_left hs) h2s } lemma integrable_measure_prod_mk_left {s : set (α × β)} (hs : measurable_set s) (h2s : (μ.prod ν) s < ∞) : integrable (λ x, (ν (prod.mk x ⁻¹' s)).to_real) μ := begin refine ⟨(measurable_measure_prod_mk_left hs).to_real.ae_measurable, _⟩, simp_rw [has_finite_integral, ennnorm_eq_of_real to_real_nonneg], convert h2s using 1, simp_rw [prod_apply hs], apply lintegral_congr_ae, refine (ae_measure_lt_top hs h2s).mp _, apply eventually_of_forall, intros x hx, rw [lt_top_iff_ne_top] at hx, simp [of_real_to_real, hx], end /-- Note: the assumption `hs` cannot be dropped. For a counterexample, see Walter Rudin *Real and Complex Analysis*, example (c) in section 8.9. -/ lemma measure_prod_null {s : set (α × β)} (hs : measurable_set s) : μ.prod ν s = 0 ↔ (λ x, ν (prod.mk x ⁻¹' s)) =ᵐ[μ] 0 := by simp_rw [prod_apply hs, lintegral_eq_zero_iff (measurable_measure_prod_mk_left hs)] /-- Note: the converse is not true without assuming that `s` is measurable. For a counterexample, see Walter Rudin *Real and Complex Analysis*, example (c) in section 8.9. -/ lemma measure_ae_null_of_prod_null {s : set (α × β)} (h : μ.prod ν s = 0) : (λ x, ν (prod.mk x ⁻¹' s)) =ᵐ[μ] 0 := begin obtain ⟨t, hst, mt, ht⟩ := exists_measurable_superset_of_null h, simp_rw [measure_prod_null mt] at ht, rw [eventually_le_antisymm_iff], exact ⟨eventually_le.trans_eq (eventually_of_forall $ λ x, (measure_mono (preimage_mono hst) : _)) ht, eventually_of_forall $ λ x, zero_le _⟩ end /-- Note: the converse is not true. For a counterexample, see Walter Rudin *Real and Complex Analysis*, example (c) in section 8.9. -/ lemma ae_ae_of_ae_prod {p : α × β → Prop} (h : ∀ᵐ z ∂μ.prod ν, p z) : ∀ᵐ x ∂ μ, ∀ᵐ y ∂ ν, p (x, y) := measure_ae_null_of_prod_null h /-- `μ.prod ν` has finite spanning sets in rectangles of finite spanning sets. -/ def finite_spanning_sets_in.prod {ν : measure β} {C : set (set α)} {D : set (set β)} (hμ : μ.finite_spanning_sets_in C) (hν : ν.finite_spanning_sets_in D) (hC : ∀ s ∈ C, measurable_set s) (hD : ∀ t ∈ D, measurable_set t) : (μ.prod ν).finite_spanning_sets_in (image2 set.prod C D) := begin haveI := hν.sigma_finite hD, refine ⟨λ n, (hμ.set n.unpair.1).prod (hν.set n.unpair.2), λ n, mem_image2_of_mem (hμ.set_mem _) (hν.set_mem _), λ n, _, _⟩, { simp_rw [prod_prod (hC _ (hμ.set_mem _)) (hD _ (hν.set_mem _))], exact mul_lt_top (hμ.finite _) (hν.finite _) }, { simp_rw [Union_unpair_prod, hμ.spanning, hν.spanning, univ_prod_univ] } end lemma prod_fst_absolutely_continuous : map prod.fst (μ.prod ν) ≪ μ := begin refine absolutely_continuous.mk (λ s hs h2s, _), simp_rw [map_apply measurable_fst hs, ← prod_univ, prod_prod hs measurable_set.univ], rw [h2s, zero_mul] -- for some reason `simp_rw [h2s]` doesn't work end lemma prod_snd_absolutely_continuous : map prod.snd (μ.prod ν) ≪ ν := begin refine absolutely_continuous.mk (λ s hs h2s, _), simp_rw [map_apply measurable_snd hs, ← univ_prod, prod_prod measurable_set.univ hs], rw [h2s, mul_zero] -- for some reason `simp_rw [h2s]` doesn't work end variables [sigma_finite μ] instance prod.sigma_finite : sigma_finite (μ.prod ν) := ⟨⟨(μ.to_finite_spanning_sets_in.prod ν.to_finite_spanning_sets_in (λ _, id) (λ _, id)).mono $ by { rintro _ ⟨s, t, hs, ht, rfl⟩, exact hs.prod ht }⟩⟩ /-- A measure on a product space equals the product measure if they are equal on rectangles with as sides sets that generate the corresponding σ-algebras. -/ lemma prod_eq_generate_from {μ : measure α} {ν : measure β} {C : set (set α)} {D : set (set β)} (hC : generate_from C = ‹_›) (hD : generate_from D = ‹_›) (h2C : is_pi_system C) (h2D : is_pi_system D) (h3C : μ.finite_spanning_sets_in C) (h3D : ν.finite_spanning_sets_in D) {μν : measure (α × β)} (h₁ : ∀ (s ∈ C) (t ∈ D), μν (set.prod s t) = μ s * ν t) : μ.prod ν = μν := begin have h4C : ∀ (s : set α), s ∈ C → measurable_set s, { intros s hs, rw [← hC], exact measurable_set_generate_from hs }, have h4D : ∀ (t : set β), t ∈ D → measurable_set t, { intros t ht, rw [← hD], exact measurable_set_generate_from ht }, refine (h3C.prod h3D h4C h4D).ext (generate_from_eq_prod hC hD h3C.is_countably_spanning h3D.is_countably_spanning).symm (h2C.prod h2D) _, { rintro _ ⟨s, t, hs, ht, rfl⟩, haveI := h3D.sigma_finite h4D, simp_rw [h₁ s hs t ht, prod_prod (h4C s hs) (h4D t ht)] } end /-- A measure on a product space equals the product measure if they are equal on rectangles. -/ lemma prod_eq {μν : measure (α × β)} (h : ∀ s t, measurable_set s → measurable_set t → μν (s.prod t) = μ s * ν t) : μ.prod ν = μν := prod_eq_generate_from generate_from_measurable_set generate_from_measurable_set is_pi_system_measurable_set is_pi_system_measurable_set μ.to_finite_spanning_sets_in ν.to_finite_spanning_sets_in (λ s hs t ht, h s t hs ht) lemma prod_swap : map prod.swap (μ.prod ν) = ν.prod μ := begin refine (prod_eq _).symm, intros s t hs ht, simp_rw [map_apply measurable_swap (hs.prod ht), preimage_swap_prod, prod_prod ht hs, mul_comm] end lemma prod_apply_symm {s : set (α × β)} (hs : measurable_set s) : μ.prod ν s = ∫⁻ y, μ ((λ x, (x, y)) ⁻¹' s) ∂ν := by { rw [← prod_swap, map_apply measurable_swap hs], simp only [prod_apply (measurable_swap hs)], refl } lemma prod_assoc_prod [sigma_finite τ] : map measurable_equiv.prod_assoc ((μ.prod ν).prod τ) = μ.prod (ν.prod τ) := begin refine (prod_eq_generate_from generate_from_measurable_set generate_from_prod is_pi_system_measurable_set is_pi_system_prod μ.to_finite_spanning_sets_in (ν.to_finite_spanning_sets_in.prod τ.to_finite_spanning_sets_in (λ _, id) (λ _, id)) _).symm, rintro s hs _ ⟨t, u, ht, hu, rfl⟩, rw [mem_set_of_eq] at hs ht hu, simp_rw [map_apply (measurable_equiv.measurable _) (hs.prod (ht.prod hu)), prod_prod ht hu, measurable_equiv.prod_assoc, measurable_equiv.coe_eq, equiv.prod_assoc_preimage, prod_prod (hs.prod ht) hu, prod_prod hs ht, mul_assoc] end /-! ### The product of specific measures -/ lemma prod_restrict {s : set α} {t : set β} (hs : measurable_set s) (ht : measurable_set t) : (μ.restrict s).prod (ν.restrict t) = (μ.prod ν).restrict (s.prod t) := begin refine prod_eq (λ s' t' hs' ht', _), simp_rw [restrict_apply (hs'.prod ht'), prod_inter_prod, prod_prod (hs'.inter hs) (ht'.inter ht), restrict_apply hs', restrict_apply ht'] end lemma restrict_prod_eq_prod_univ {s : set α} (hs : measurable_set s) : (μ.restrict s).prod ν = (μ.prod ν).restrict (s.prod univ) := begin have : ν = ν.restrict set.univ := measure.restrict_univ.symm, rwa [this, measure.prod_restrict, ← this], exact measurable_set.univ, end lemma prod_dirac (y : β) : μ.prod (dirac y) = map (λ x, (x, y)) μ := begin refine prod_eq (λ s t hs ht, _), simp_rw [map_apply measurable_prod_mk_right (hs.prod ht), mk_preimage_prod_left_eq_if, measure_if, dirac_apply' _ ht, ← indicator_mul_right _ (λ x, μ s), pi.one_apply, mul_one] end lemma dirac_prod (x : α) : (dirac x).prod ν = map (prod.mk x) ν := begin refine prod_eq (λ s t hs ht, _), simp_rw [map_apply measurable_prod_mk_left (hs.prod ht), mk_preimage_prod_right_eq_if, measure_if, dirac_apply' _ hs, ← indicator_mul_left _ _ (λ x, ν t), pi.one_apply, one_mul] end lemma dirac_prod_dirac {x : α} {y : β} : (dirac x).prod (dirac y) = dirac (x, y) := by rw [prod_dirac, map_dirac measurable_prod_mk_right] lemma prod_sum {ι : Type*} [fintype ι] (ν : ι → measure β) [∀ i, sigma_finite (ν i)] : μ.prod (sum ν) = sum (λ i, μ.prod (ν i)) := begin refine prod_eq (λ s t hs ht, _), simp_rw [sum_apply _ (hs.prod ht), sum_apply _ ht, prod_prod hs ht, tsum_fintype, finset.mul_sum] end lemma sum_prod {ι : Type*} [fintype ι] (μ : ι → measure α) [∀ i, sigma_finite (μ i)] : (sum μ).prod ν = sum (λ i, (μ i).prod ν) := begin refine prod_eq (λ s t hs ht, _), simp_rw [sum_apply _ (hs.prod ht), sum_apply _ hs, prod_prod hs ht, tsum_fintype, finset.sum_mul] end lemma prod_add (ν' : measure β) [sigma_finite ν'] : μ.prod (ν + ν') = μ.prod ν + μ.prod ν' := by { refine prod_eq (λ s t hs ht, _), simp_rw [add_apply, prod_prod hs ht, left_distrib] } lemma add_prod (μ' : measure α) [sigma_finite μ'] : (μ + μ').prod ν = μ.prod ν + μ'.prod ν := by { refine prod_eq (λ s t hs ht, _), simp_rw [add_apply, prod_prod hs ht, right_distrib] } @[simp] lemma zero_prod (ν : measure β) : (0 : measure α).prod ν = 0 := by { rw measure.prod, exact bind_zero_left _ } @[simp] lemma prod_zero (μ : measure α) : μ.prod (0 : measure β) = 0 := by simp [measure.prod] lemma map_prod_map {δ} [measurable_space δ] {f : α → β} {g : γ → δ} {μa : measure α} {μc : measure γ} (hfa : sigma_finite (map f μa)) (hgc : sigma_finite (map g μc)) (hf : measurable f) (hg : measurable g) : (map f μa).prod (map g μc) = map (prod.map f g) (μa.prod μc) := begin haveI := hgc.of_map μc hg, refine prod_eq (λ s t hs ht, _), rw [map_apply (hf.prod_map hg) (hs.prod ht), map_apply hf hs, map_apply hg ht], exact prod_prod (hf hs) (hg ht) end end measure end measure_theory open measure_theory.measure section lemma ae_measurable.prod_swap [sigma_finite μ] [sigma_finite ν] {f : β × α → γ} (hf : ae_measurable f (ν.prod μ)) : ae_measurable (λ (z : α × β), f z.swap) (μ.prod ν) := by { rw ← prod_swap at hf, exact hf.comp_measurable measurable_swap } lemma ae_measurable.fst [sigma_finite ν] {f : α → γ} (hf : ae_measurable f μ) : ae_measurable (λ (z : α × β), f z.1) (μ.prod ν) := hf.comp_measurable' measurable_fst prod_fst_absolutely_continuous lemma ae_measurable.snd [sigma_finite ν] {f : β → γ} (hf : ae_measurable f ν) : ae_measurable (λ (z : α × β), f z.2) (μ.prod ν) := hf.comp_measurable' measurable_snd prod_snd_absolutely_continuous /-- The Bochner integral is a.e.-measurable. This shows that the integrand of (the right-hand-side of) Fubini's theorem is a.e.-measurable. -/ lemma ae_measurable.integral_prod_right' [sigma_finite ν] [second_countable_topology E] [normed_space ℝ E] [borel_space E] [complete_space E] ⦃f : α × β → E⦄ (hf : ae_measurable f (μ.prod ν)) : ae_measurable (λ x, ∫ y, f (x, y) ∂ν) μ := ⟨λ x, ∫ y, hf.mk f (x, y) ∂ν, hf.measurable_mk.integral_prod_right', begin filter_upwards [ae_ae_of_ae_prod hf.ae_eq_mk], assume x hx, exact integral_congr_ae hx end⟩ lemma ae_measurable.prod_mk_left [sigma_finite ν] {f : α × β → γ} (hf : ae_measurable f (μ.prod ν)) : ∀ᵐ x ∂μ, ae_measurable (λ y, f (x, y)) ν := begin filter_upwards [ae_ae_of_ae_prod hf.ae_eq_mk], intros x hx, exact ⟨λ y, hf.mk f (x, y), hf.measurable_mk.comp measurable_prod_mk_left, hx⟩ end end namespace measure_theory /-! ### The Lebesgue integral on a product -/ variables [sigma_finite ν] lemma lintegral_prod_swap [sigma_finite μ] (f : α × β → ℝ≥0∞) (hf : ae_measurable f (μ.prod ν)) : ∫⁻ z, f z.swap ∂(ν.prod μ) = ∫⁻ z, f z ∂(μ.prod ν) := by { rw ← prod_swap at hf, rw [← lintegral_map' hf measurable_swap, prod_swap] } /-- Tonelli's Theorem: For `ℝ≥0∞`-valued measurable functions on `α × β`, the integral of `f` is equal to the iterated integral. -/ lemma lintegral_prod_of_measurable : ∀ (f : α × β → ℝ≥0∞) (hf : measurable f), ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ x, ∫⁻ y, f (x, y) ∂ν ∂μ := begin have m := @measurable_prod_mk_left, refine measurable.ennreal_induction _ _ _, { intros c s hs, simp only [← indicator_comp_right], simp [lintegral_indicator, m hs, hs, lintegral_const_mul, measurable_measure_prod_mk_left hs, prod_apply] }, { rintro f g - hf hg h2f h2g, simp [lintegral_add, measurable.lintegral_prod_right', hf.comp m, hg.comp m, hf, hg, h2f, h2g] }, { intros f hf h2f h3f, have kf : ∀ x n, measurable (λ y, f n (x, y)) := λ x n, (hf n).comp m, have k2f : ∀ x, monotone (λ n y, f n (x, y)) := λ x i j hij y, h2f hij (x, y), have lf : ∀ n, measurable (λ x, ∫⁻ y, f n (x, y) ∂ν) := λ n, (hf n).lintegral_prod_right', have l2f : monotone (λ n x, ∫⁻ y, f n (x, y) ∂ν) := λ i j hij x, lintegral_mono (k2f x hij), simp only [lintegral_supr hf h2f, lintegral_supr (kf _), k2f, lintegral_supr lf l2f, h3f] }, end /-- Tonelli's Theorem: For `ℝ≥0∞`-valued almost everywhere measurable functions on `α × β`, the integral of `f` is equal to the iterated integral. -/ lemma lintegral_prod (f : α × β → ℝ≥0∞) (hf : ae_measurable f (μ.prod ν)) : ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ x, ∫⁻ y, f (x, y) ∂ν ∂μ := begin have A : ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ z, hf.mk f z ∂(μ.prod ν) := lintegral_congr_ae hf.ae_eq_mk, have B : ∫⁻ x, ∫⁻ y, f (x, y) ∂ν ∂μ = ∫⁻ x, ∫⁻ y, hf.mk f (x, y) ∂ν ∂μ, { apply lintegral_congr_ae, filter_upwards [ae_ae_of_ae_prod hf.ae_eq_mk], assume a ha, exact lintegral_congr_ae ha }, rw [A, B, lintegral_prod_of_measurable _ hf.measurable_mk], apply_instance end /-- The symmetric verion of Tonelli's Theorem: For `ℝ≥0∞`-valued almost everywhere measurable functions on `α × β`, the integral of `f` is equal to the iterated integral, in reverse order. -/ lemma lintegral_prod_symm' [sigma_finite μ] (f : α × β → ℝ≥0∞) (hf : ae_measurable f (μ.prod ν)) : ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ y, ∫⁻ x, f (x, y) ∂μ ∂ν := by { simp_rw [← lintegral_prod_swap f hf], exact lintegral_prod _ hf.prod_swap } /-- The symmetric verion of Tonelli's Theorem: For `ℝ≥0∞`-valued measurable functions on `α × β`, the integral of `f` is equal to the iterated integral, in reverse order. -/ lemma lintegral_prod_symm [sigma_finite μ] (f : α × β → ℝ≥0∞) (hf : ae_measurable f (μ.prod ν)) : ∫⁻ z, f z ∂(μ.prod ν) = ∫⁻ y, ∫⁻ x, f (x, y) ∂μ ∂ν := lintegral_prod_symm' f hf /-- The reversed version of Tonelli's Theorem. In this version `f` is in curried form, which makes it easier for the elaborator to figure out `f` automatically. -/ lemma lintegral_lintegral ⦃f : α → β → ℝ≥0∞⦄ (hf : ae_measurable (uncurry f) (μ.prod ν)) : ∫⁻ x, ∫⁻ y, f x y ∂ν ∂μ = ∫⁻ z, f z.1 z.2 ∂(μ.prod ν) := (lintegral_prod _ hf).symm /-- The reversed version of Tonelli's Theorem (symmetric version). In this version `f` is in curried form, which makes it easier for the elaborator to figure out `f` automatically. -/ lemma lintegral_lintegral_symm [sigma_finite μ] ⦃f : α → β → ℝ≥0∞⦄ (hf : ae_measurable (uncurry f) (μ.prod ν)) : ∫⁻ x, ∫⁻ y, f x y ∂ν ∂μ = ∫⁻ z, f z.2 z.1 ∂(ν.prod μ) := (lintegral_prod_symm _ hf.prod_swap).symm /-- Change the order of Lebesgue integration. -/ lemma lintegral_lintegral_swap [sigma_finite μ] ⦃f : α → β → ℝ≥0∞⦄ (hf : ae_measurable (uncurry f) (μ.prod ν)) : ∫⁻ x, ∫⁻ y, f x y ∂ν ∂μ = ∫⁻ y, ∫⁻ x, f x y ∂μ ∂ν := (lintegral_lintegral hf).trans (lintegral_prod_symm _ hf) lemma lintegral_prod_mul {f : α → ℝ≥0∞} {g : β → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g ν) : ∫⁻ z, f z.1 * g z.2 ∂(μ.prod ν) = ∫⁻ x, f x ∂μ * ∫⁻ y, g y ∂ν := by simp [lintegral_prod _ (hf.fst.mul hg.snd), lintegral_lintegral_mul hf hg] /-! ### Integrability on a product -/ section variables [opens_measurable_space E] lemma integrable.swap [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (f ∘ prod.swap) (ν.prod μ) := ⟨hf.ae_measurable.prod_swap, (lintegral_prod_swap _ hf.ae_measurable.ennnorm : _).le.trans_lt hf.has_finite_integral⟩ lemma integrable_swap_iff [sigma_finite μ] ⦃f : α × β → E⦄ : integrable (f ∘ prod.swap) (ν.prod μ) ↔ integrable f (μ.prod ν) := ⟨λ hf, by { convert hf.swap, ext ⟨x, y⟩, refl }, λ hf, hf.swap⟩ lemma has_finite_integral_prod_iff ⦃f : α × β → E⦄ (h1f : measurable f) : has_finite_integral f (μ.prod ν) ↔ (∀ᵐ x ∂ μ, has_finite_integral (λ y, f (x, y)) ν) ∧ has_finite_integral (λ x, ∫ y, ∥f (x, y)∥ ∂ν) μ := begin simp only [has_finite_integral, lintegral_prod_of_measurable _ h1f.ennnorm], have : ∀ x, ∀ᵐ y ∂ν, 0 ≤ ∥f (x, y)∥ := λ x, eventually_of_forall (λ y, norm_nonneg _), simp_rw [integral_eq_lintegral_of_nonneg_ae (this _) (h1f.norm.comp measurable_prod_mk_left).ae_measurable, ennnorm_eq_of_real to_real_nonneg, of_real_norm_eq_coe_nnnorm], -- this fact is probably too specialized to be its own lemma have : ∀ {p q r : Prop} (h1 : r → p), (r ↔ p ∧ q) ↔ (p → (r ↔ q)) := λ p q r h1, by rw [← and.congr_right_iff, and_iff_right_of_imp h1], rw [this], { intro h2f, rw lintegral_congr_ae, refine h2f.mp _, apply eventually_of_forall, intros x hx, dsimp only, rw [of_real_to_real], rw [← lt_top_iff_ne_top], exact hx }, { intro h2f, refine ae_lt_top _ h2f, exact h1f.ennnorm.lintegral_prod_right' }, end lemma has_finite_integral_prod_iff' ⦃f : α × β → E⦄ (h1f : ae_measurable f (μ.prod ν)) : has_finite_integral f (μ.prod ν) ↔ (∀ᵐ x ∂ μ, has_finite_integral (λ y, f (x, y)) ν) ∧ has_finite_integral (λ x, ∫ y, ∥f (x, y)∥ ∂ν) μ := begin rw [has_finite_integral_congr h1f.ae_eq_mk, has_finite_integral_prod_iff h1f.measurable_mk], apply and_congr, { apply eventually_congr, filter_upwards [ae_ae_of_ae_prod h1f.ae_eq_mk.symm], assume x hx, exact has_finite_integral_congr hx }, { apply has_finite_integral_congr, filter_upwards [ae_ae_of_ae_prod h1f.ae_eq_mk.symm], assume x hx, exact integral_congr_ae (eventually_eq.fun_comp hx _) }, { apply_instance } end /-- A binary function is integrable if the function `y ↦ f (x, y)` is integrable for almost every `x` and the function `x ↦ ∫ ∥f (x, y)∥ dy` is integrable. -/ lemma integrable_prod_iff ⦃f : α × β → E⦄ (h1f : ae_measurable f (μ.prod ν)) : integrable f (μ.prod ν) ↔ (∀ᵐ x ∂ μ, integrable (λ y, f (x, y)) ν) ∧ integrable (λ x, ∫ y, ∥f (x, y)∥ ∂ν) μ := by simp [integrable, h1f, has_finite_integral_prod_iff', h1f.norm.integral_prod_right', h1f.prod_mk_left] /-- A binary function is integrable if the function `x ↦ f (x, y)` is integrable for almost every `y` and the function `y ↦ ∫ ∥f (x, y)∥ dx` is integrable. -/ lemma integrable_prod_iff' [sigma_finite μ] ⦃f : α × β → E⦄ (h1f : ae_measurable f (μ.prod ν)) : integrable f (μ.prod ν) ↔ (∀ᵐ y ∂ ν, integrable (λ x, f (x, y)) μ) ∧ integrable (λ y, ∫ x, ∥f (x, y)∥ ∂μ) ν := by { convert integrable_prod_iff (h1f.prod_swap) using 1, rw [integrable_swap_iff] } lemma integrable.prod_left_ae [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : ∀ᵐ y ∂ ν, integrable (λ x, f (x, y)) μ := ((integrable_prod_iff' hf.ae_measurable).mp hf).1 lemma integrable.prod_right_ae [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : ∀ᵐ x ∂ μ, integrable (λ y, f (x, y)) ν := hf.swap.prod_left_ae lemma integrable.integral_norm_prod_left ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ x, ∫ y, ∥f (x, y)∥ ∂ν) μ := ((integrable_prod_iff hf.ae_measurable).mp hf).2 lemma integrable.integral_norm_prod_right [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ y, ∫ x, ∥f (x, y)∥ ∂μ) ν := hf.swap.integral_norm_prod_left end variables [second_countable_topology E] [normed_space ℝ E] [complete_space E] [borel_space E] lemma integrable.integral_prod_left ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ x, ∫ y, f (x, y) ∂ν) μ := integrable.mono hf.integral_norm_prod_left hf.ae_measurable.integral_prod_right' $ eventually_of_forall $ λ x, (norm_integral_le_integral_norm _).trans_eq $ (norm_of_nonneg $ integral_nonneg_of_ae $ eventually_of_forall $ λ y, (norm_nonneg (f (x, y)) : _)).symm lemma integrable.integral_prod_right [sigma_finite μ] ⦃f : α × β → E⦄ (hf : integrable f (μ.prod ν)) : integrable (λ y, ∫ x, f (x, y) ∂μ) ν := hf.swap.integral_prod_left /-! ### The Bochner integral on a product -/ variables [sigma_finite μ] lemma integral_prod_swap (f : α × β → E) (hf : ae_measurable f (μ.prod ν)) : ∫ z, f z.swap ∂(ν.prod μ) = ∫ z, f z ∂(μ.prod ν) := begin rw ← prod_swap at hf, rw [← integral_map measurable_swap hf, prod_swap] end variables {E' : Type*} [measurable_space E'] [normed_group E'] [borel_space E'] [complete_space E'] [normed_space ℝ E'] [second_countable_topology E'] /-! Some rules about the sum/difference of double integrals. They follow from `integral_add`, but we separate them out as separate lemmas, because they involve quite some steps. -/ /-- Integrals commute with addition inside another integral. `F` can be any function. -/ lemma integral_fn_integral_add ⦃f g : α × β → E⦄ (F : E → E') (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, F (∫ y, f (x, y) + g (x, y) ∂ν) ∂μ = ∫ x, F (∫ y, f (x, y) ∂ν + ∫ y, g (x, y) ∂ν) ∂μ := begin refine integral_congr_ae _, filter_upwards [hf.prod_right_ae, hg.prod_right_ae], intros x h2f h2g, simp [integral_add h2f h2g], end /-- Integrals commute with subtraction inside another integral. `F` can be any measurable function. -/ lemma integral_fn_integral_sub ⦃f g : α × β → E⦄ (F : E → E') (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, F (∫ y, f (x, y) - g (x, y) ∂ν) ∂μ = ∫ x, F (∫ y, f (x, y) ∂ν - ∫ y, g (x, y) ∂ν) ∂μ := begin refine integral_congr_ae _, filter_upwards [hf.prod_right_ae, hg.prod_right_ae], intros x h2f h2g, simp [integral_sub h2f h2g] end /-- Integrals commute with subtraction inside a lower Lebesgue integral. `F` can be any function. -/ lemma lintegral_fn_integral_sub ⦃f g : α × β → E⦄ (F : E → ℝ≥0∞) (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫⁻ x, F (∫ y, f (x, y) - g (x, y) ∂ν) ∂μ = ∫⁻ x, F (∫ y, f (x, y) ∂ν - ∫ y, g (x, y) ∂ν) ∂μ := begin refine lintegral_congr_ae _, filter_upwards [hf.prod_right_ae, hg.prod_right_ae], intros x h2f h2g, simp [integral_sub h2f h2g] end /-- Double integrals commute with addition. -/ lemma integral_integral_add ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, f (x, y) + g (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ + ∫ x, ∫ y, g (x, y) ∂ν ∂μ := (integral_fn_integral_add id hf hg).trans $ integral_add hf.integral_prod_left hg.integral_prod_left /-- Double integrals commute with addition. This is the version with `(f + g) (x, y)` (instead of `f (x, y) + g (x, y)`) in the LHS. -/ lemma integral_integral_add' ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, (f + g) (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ + ∫ x, ∫ y, g (x, y) ∂ν ∂μ := integral_integral_add hf hg /-- Double integrals commute with subtraction. -/ lemma integral_integral_sub ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, f (x, y) - g (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ - ∫ x, ∫ y, g (x, y) ∂ν ∂μ := (integral_fn_integral_sub id hf hg).trans $ integral_sub hf.integral_prod_left hg.integral_prod_left /-- Double integrals commute with subtraction. This is the version with `(f - g) (x, y)` (instead of `f (x, y) - g (x, y)`) in the LHS. -/ lemma integral_integral_sub' ⦃f g : α × β → E⦄ (hf : integrable f (μ.prod ν)) (hg : integrable g (μ.prod ν)) : ∫ x, ∫ y, (f - g) (x, y) ∂ν ∂μ = ∫ x, ∫ y, f (x, y) ∂ν ∂μ - ∫ x, ∫ y, g (x, y) ∂ν ∂μ := integral_integral_sub hf hg /-- The map that sends an L¹-function `f : α × β → E` to `∫∫f` is continuous. -/ lemma continuous_integral_integral : continuous (λ (f : α × β →₁[μ.prod ν] E), ∫ x, ∫ y, f (x, y) ∂ν ∂μ) := begin rw [continuous_iff_continuous_at], intro g, refine tendsto_integral_of_L1 _ (L1.integrable_coe_fn g).integral_prod_left (eventually_of_forall $ λ h, (L1.integrable_coe_fn h).integral_prod_left) _, simp_rw [edist_eq_coe_nnnorm_sub, ← lintegral_fn_integral_sub (λ x, (nnnorm x : ℝ≥0∞)) (L1.integrable_coe_fn _) (L1.integrable_coe_fn g)], refine tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds _ (λ i, zero_le _) _, { exact λ i, ∫⁻ x, ∫⁻ y, nnnorm (i (x, y) - g (x, y)) ∂ν ∂μ }, swap, { exact λ i, lintegral_mono (λ x, ennnorm_integral_le_lintegral_ennnorm _) }, show tendsto (λ (i : α × β →₁[μ.prod ν] E), ∫⁻ x, ∫⁻ (y : β), nnnorm (i (x, y) - g (x, y)) ∂ν ∂μ) (𝓝 g) (𝓝 0), have : ∀ (i : α × β →₁[μ.prod ν] E), measurable (λ z, (nnnorm (i z - g z) : ℝ≥0∞)) := λ i, ((Lp.measurable i).sub (Lp.measurable g)).ennnorm, simp_rw [← lintegral_prod_of_measurable _ (this _), ← L1.of_real_norm_sub_eq_lintegral, ← of_real_zero], refine (continuous_of_real.tendsto 0).comp _, rw [← tendsto_iff_norm_tendsto_zero], exact tendsto_id end /-- Fubini's Theorem: For integrable functions on `α × β`, the Bochner integral of `f` is equal to the iterated Bochner integral. `integrable_prod_iff` can be useful to show that the function in question in integrable. `measure_theory.integrable.integral_prod_right` is useful to show that the inner integral of the right-hand side is integrable. -/ lemma integral_prod : ∀ (f : α × β → E) (hf : integrable f (μ.prod ν)), ∫ z, f z ∂(μ.prod ν) = ∫ x, ∫ y, f (x, y) ∂ν ∂μ := begin apply integrable.induction, { intros c s hs h2s, simp_rw [integral_indicator hs, ← indicator_comp_right, function.comp, integral_indicator (measurable_prod_mk_left hs), set_integral_const, integral_smul_const, integral_to_real (measurable_measure_prod_mk_left hs).ae_measurable (ae_measure_lt_top hs h2s), prod_apply hs] }, { intros f g hfg i_f i_g hf hg, simp_rw [integral_add' i_f i_g, integral_integral_add' i_f i_g, hf, hg] }, { exact is_closed_eq continuous_integral continuous_integral_integral }, { intros f g hfg i_f hf, convert hf using 1, { exact integral_congr_ae hfg.symm }, { refine integral_congr_ae _, refine (ae_ae_of_ae_prod hfg).mp _, apply eventually_of_forall, intros x hfgx, exact integral_congr_ae (ae_eq_symm hfgx) } } end /-- Symmetric version of Fubini's Theorem: For integrable functions on `α × β`, the Bochner integral of `f` is equal to the iterated Bochner integral. This version has the integrals on the right-hand side in the other order. -/ lemma integral_prod_symm (f : α × β → E) (hf : integrable f (μ.prod ν)) : ∫ z, f z ∂(μ.prod ν) = ∫ y, ∫ x, f (x, y) ∂μ ∂ν := by { simp_rw [← integral_prod_swap f hf.ae_measurable], exact integral_prod _ hf.swap } /-- Reversed version of Fubini's Theorem. -/ lemma integral_integral {f : α → β → E} (hf : integrable (uncurry f) (μ.prod ν)) : ∫ x, ∫ y, f x y ∂ν ∂μ = ∫ z, f z.1 z.2 ∂(μ.prod ν) := (integral_prod _ hf).symm /-- Reversed version of Fubini's Theorem (symmetric version). -/ lemma integral_integral_symm {f : α → β → E} (hf : integrable (uncurry f) (μ.prod ν)) : ∫ x, ∫ y, f x y ∂ν ∂μ = ∫ z, f z.2 z.1 ∂(ν.prod μ) := (integral_prod_symm _ hf.swap).symm /-- Change the order of Bochner integration. -/ lemma integral_integral_swap ⦃f : α → β → E⦄ (hf : integrable (uncurry f) (μ.prod ν)) : ∫ x, ∫ y, f x y ∂ν ∂μ = ∫ y, ∫ x, f x y ∂μ ∂ν := (integral_integral hf).trans (integral_prod_symm _ hf) end measure_theory
8e8095227da3482868a729b1eec3645fac778f06
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/metric_space/antilipschitz.lean
3713842069136538ceba1d2ea76ac764a29760f6
[ "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
9,253
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.metric_space.lipschitz import topology.uniform_space.complete_separated /-! # Antilipschitz functions We say that a map `f : α → β` between two (extended) metric spaces is `antilipschitz_with K`, `K ≥ 0`, if for all `x, y` we have `edist x y ≤ K * edist (f x) (f y)`. For a metric space, the latter inequality is equivalent to `dist x y ≤ K * dist (f x) (f y)`. ## Implementation notes The parameter `K` has type `ℝ≥0`. This way we avoid conjuction in the definition and have coercions both to `ℝ` and `ℝ≥0∞`. We do not require `0 < K` in the definition, mostly because we do not have a `posreal` type. -/ variables {α : Type*} {β : Type*} {γ : Type*} open_locale nnreal ennreal uniformity open set /-- We say that `f : α → β` is `antilipschitz_with K` if for any two points `x`, `y` we have `K * edist x y ≤ edist (f x) (f y)`. -/ def antilipschitz_with [pseudo_emetric_space α] [pseudo_emetric_space β] (K : ℝ≥0) (f : α → β) := ∀ x y, edist x y ≤ K * edist (f x) (f y) lemma antilipschitz_with.edist_lt_top [pseudo_emetric_space α] [pseudo_metric_space β] {K : ℝ≥0} {f : α → β} (h : antilipschitz_with K f) (x y : α) : edist x y < ⊤ := (h x y).trans_lt $ ennreal.mul_lt_top ennreal.coe_ne_top (edist_ne_top _ _) lemma antilipschitz_with.edist_ne_top [pseudo_emetric_space α] [pseudo_metric_space β] {K : ℝ≥0} {f : α → β} (h : antilipschitz_with K f) (x y : α) : edist x y ≠ ⊤ := (h.edist_lt_top x y).ne section metric variables [pseudo_metric_space α] [pseudo_metric_space β] {K : ℝ≥0} {f : α → β} lemma antilipschitz_with_iff_le_mul_nndist : antilipschitz_with K f ↔ ∀ x y, nndist x y ≤ K * nndist (f x) (f y) := by { simp only [antilipschitz_with, edist_nndist], norm_cast } alias antilipschitz_with_iff_le_mul_nndist ↔ antilipschitz_with.le_mul_nndist antilipschitz_with.of_le_mul_nndist lemma antilipschitz_with_iff_le_mul_dist : antilipschitz_with K f ↔ ∀ x y, dist x y ≤ K * dist (f x) (f y) := by { simp only [antilipschitz_with_iff_le_mul_nndist, dist_nndist], norm_cast } alias antilipschitz_with_iff_le_mul_dist ↔ antilipschitz_with.le_mul_dist antilipschitz_with.of_le_mul_dist namespace antilipschitz_with lemma mul_le_nndist (hf : antilipschitz_with K f) (x y : α) : K⁻¹ * nndist x y ≤ nndist (f x) (f y) := by simpa only [div_eq_inv_mul] using nnreal.div_le_of_le_mul' (hf.le_mul_nndist x y) lemma mul_le_dist (hf : antilipschitz_with K f) (x y : α) : (K⁻¹ * dist x y : ℝ) ≤ dist (f x) (f y) := by exact_mod_cast hf.mul_le_nndist x y end antilipschitz_with end metric namespace antilipschitz_with variables [pseudo_emetric_space α] [pseudo_emetric_space β] [pseudo_emetric_space γ] variables {K : ℝ≥0} {f : α → β} open emetric /-- Extract the constant from `hf : antilipschitz_with K f`. This is useful, e.g., if `K` is given by a long formula, and we want to reuse this value. -/ @[nolint unused_arguments] -- uses neither `f` nor `hf` protected def K (hf : antilipschitz_with K f) : ℝ≥0 := K protected lemma injective {α : Type*} {β : Type*} [emetric_space α] [pseudo_emetric_space β] {K : ℝ≥0} {f : α → β} (hf : antilipschitz_with K f) : function.injective f := λ x y h, by simpa only [h, edist_self, mul_zero, edist_le_zero] using hf x y lemma mul_le_edist (hf : antilipschitz_with K f) (x y : α) : (K⁻¹ * edist x y : ℝ≥0∞) ≤ edist (f x) (f y) := begin rw [mul_comm, ← div_eq_mul_inv], exact ennreal.div_le_of_le_mul' (hf x y) end lemma ediam_preimage_le (hf : antilipschitz_with K f) (s : set β) : diam (f ⁻¹' s) ≤ K * diam s := diam_le $ λ x hx y hy, (hf x y).trans $ mul_le_mul_left' (edist_le_diam_of_mem hx hy) K lemma le_mul_ediam_image (hf : antilipschitz_with K f) (s : set α) : diam s ≤ K * diam (f '' s) := (diam_mono (subset_preimage_image _ _)).trans (hf.ediam_preimage_le (f '' s)) protected lemma id : antilipschitz_with 1 (id : α → α) := λ x y, by simp only [ennreal.coe_one, one_mul, id, le_refl] lemma comp {Kg : ℝ≥0} {g : β → γ} (hg : antilipschitz_with Kg g) {Kf : ℝ≥0} {f : α → β} (hf : antilipschitz_with Kf f) : antilipschitz_with (Kf * Kg) (g ∘ f) := λ x y, calc edist x y ≤ Kf * edist (f x) (f y) : hf x y ... ≤ Kf * (Kg * edist (g (f x)) (g (f y))) : ennreal.mul_left_mono (hg _ _) ... = _ : by rw [ennreal.coe_mul, mul_assoc] lemma restrict (hf : antilipschitz_with K f) (s : set α) : antilipschitz_with K (s.restrict f) := λ x y, hf x y lemma cod_restrict (hf : antilipschitz_with K f) {s : set β} (hs : ∀ x, f x ∈ s) : antilipschitz_with K (s.cod_restrict f hs) := λ x y, hf x y lemma to_right_inv_on' {s : set α} (hf : antilipschitz_with K (s.restrict f)) {g : β → α} {t : set β} (g_maps : maps_to g t s) (g_inv : right_inv_on g f t) : lipschitz_with K (t.restrict g) := λ x y, by simpa only [restrict_apply, g_inv x.mem, g_inv y.mem, subtype.edist_eq, subtype.coe_mk] using hf ⟨g x, g_maps x.mem⟩ ⟨g y, g_maps y.mem⟩ lemma to_right_inv_on (hf : antilipschitz_with K f) {g : β → α} {t : set β} (h : right_inv_on g f t) : lipschitz_with K (t.restrict g) := (hf.restrict univ).to_right_inv_on' (maps_to_univ g t) h lemma to_right_inverse (hf : antilipschitz_with K f) {g : β → α} (hg : function.right_inverse g f) : lipschitz_with K g := begin intros x y, have := hf (g x) (g y), rwa [hg x, hg y] at this end lemma comap_uniformity_le (hf : antilipschitz_with K f) : (𝓤 β).comap (prod.map f f) ≤ 𝓤 α := begin refine ((uniformity_basis_edist.comap _).le_basis_iff uniformity_basis_edist).2 (λ ε h₀, _), refine ⟨K⁻¹ * ε, ennreal.mul_pos (ennreal.inv_ne_zero.2 ennreal.coe_ne_top) h₀.ne', _⟩, refine λ x hx, (hf x.1 x.2).trans_lt _, rw [mul_comm, ← div_eq_mul_inv] at hx, rw mul_comm, exact ennreal.mul_lt_of_lt_div hx end protected lemma uniform_inducing (hf : antilipschitz_with K f) (hfc : uniform_continuous f) : uniform_inducing f := ⟨le_antisymm hf.comap_uniformity_le hfc.le_comap⟩ protected lemma uniform_embedding {α : Type*} {β : Type*} [emetric_space α] [pseudo_emetric_space β] {K : ℝ≥0} {f : α → β} (hf : antilipschitz_with K f) (hfc : uniform_continuous f) : uniform_embedding f := ⟨hf.uniform_inducing hfc, hf.injective⟩ lemma is_complete_range [complete_space α] (hf : antilipschitz_with K f) (hfc : uniform_continuous f) : is_complete (range f) := (hf.uniform_inducing hfc).is_complete_range lemma is_closed_range {α β : Type*} [pseudo_emetric_space α] [emetric_space β] [complete_space α] {f : α → β} {K : ℝ≥0} (hf : antilipschitz_with K f) (hfc : uniform_continuous f) : is_closed (range f) := (hf.is_complete_range hfc).is_closed lemma closed_embedding {α : Type*} {β : Type*} [emetric_space α] [emetric_space β] {K : ℝ≥0} {f : α → β} [complete_space α] (hf : antilipschitz_with K f) (hfc : uniform_continuous f) : closed_embedding f := { closed_range := hf.is_closed_range hfc, .. (hf.uniform_embedding hfc).embedding } lemma subtype_coe (s : set α) : antilipschitz_with 1 (coe : s → α) := antilipschitz_with.id.restrict s lemma of_subsingleton [subsingleton α] {K : ℝ≥0} : antilipschitz_with K f := λ x y, by simp only [subsingleton.elim x y, edist_self, zero_le] /-- If `f : α → β` is `0`-antilipschitz, then `α` is a `subsingleton`. -/ protected lemma subsingleton {α β} [emetric_space α] [pseudo_emetric_space β] {f : α → β} (h : antilipschitz_with 0 f) : subsingleton α := ⟨λ x y, edist_le_zero.1 $ (h x y).trans_eq $ zero_mul _⟩ end antilipschitz_with namespace antilipschitz_with open metric variables [pseudo_metric_space α] [pseudo_metric_space β] {K : ℝ≥0} {f : α → β} lemma bounded_preimage (hf : antilipschitz_with K f) {s : set β} (hs : bounded s) : bounded (f ⁻¹' s) := exists.intro (K * diam s) $ λ x hx y hy, calc dist x y ≤ K * dist (f x) (f y) : hf.le_mul_dist x y ... ≤ K * diam s : mul_le_mul_of_nonneg_left (dist_le_diam_of_mem hs hx hy) K.2 /-- The image of a proper space under an expanding onto map is proper. -/ protected lemma proper_space {α : Type*} [metric_space α] {K : ℝ≥0} {f : α → β} [proper_space α] (hK : antilipschitz_with K f) (f_cont : continuous f) (hf : function.surjective f) : proper_space β := begin apply proper_space_of_compact_closed_ball_of_le 0 (λx₀ r hr, _), let K := f ⁻¹' (closed_ball x₀ r), have A : is_closed K := is_closed_ball.preimage f_cont, have B : bounded K := hK.bounded_preimage bounded_closed_ball, have : is_compact K := compact_iff_closed_bounded.2 ⟨A, B⟩, convert this.image f_cont, exact (hf.image_preimage _).symm end end antilipschitz_with lemma lipschitz_with.to_right_inverse [pseudo_emetric_space α] [pseudo_emetric_space β] {K : ℝ≥0} {f : α → β} (hf : lipschitz_with K f) {g : β → α} (hg : function.right_inverse g f) : antilipschitz_with K g := λ x y, by simpa only [hg _] using hf (g x) (g y)