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
f680ee3e287e8a9c71a41db1184c424f180ec4bb
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/sleep.lean
e52744e9cd1cb70063b79c9a512cee6be732f8a7
[ "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
66
lean
import system.io run_cmd (tactic.unsafe_run_io $ io.proc.sleep 2)
6ede9bd5e3bdf7a0581e645a4cf1098034d264c7
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/src/Lean/Meta/Tactic/Simp/SimpLemmas.lean
3baf56b8ce5b1c06ccbb204e142686c8f34f6436
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
12,419
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.ScopedEnvExtension import Lean.Util.Recognizers import Lean.Meta.LevelDefEq import Lean.Meta.DiscrTree import Lean.Meta.AppBuilder import Lean.Meta.Tactic.AuxLemma namespace Lean.Meta /-- The fields `levelParams` and `proof` are used to encode the proof of the simp lemma. If the `proof` is a global declaration `c`, we store `Expr.const c []` at `proof` without the universe levels, and `levelParams` is set to `#[]` When using the lemma, we create fresh universe metavariables. Motivation: most simp lemmas are global declarations, and this approach is faster and saves memory. The field `levelParams` is not empty only when we elaborate an expression provided by the user, and it contains universe metavariables. Then, we use `abstractMVars` to abstract the universe metavariables and create new fresh universe parameters that are stored at the field `levelParams`. -/ structure SimpLemma where keys : Array DiscrTree.Key := #[] levelParams : Array Name := #[] -- non empty for local universe polymorhic proofs. proof : Expr priority : Nat := eval_prio default post : Bool := true perm : Bool := false -- true is lhs and rhs are identical modulo permutation of variables name? : Option Name := none -- for debugging and tracing purposes deriving Inhabited def SimpLemma.getName (s : SimpLemma) : Name := match s.name? with | some n => n | none => "<unknown>" instance : ToFormat SimpLemma where format s := let perm := if s.perm then ":perm" else "" let name := format s.getName let prio := f!":{s.priority}" name ++ prio ++ perm instance : ToMessageData SimpLemma where toMessageData s := format s instance : BEq SimpLemma where beq e₁ eβ‚‚ := e₁.proof == eβ‚‚.proof structure SimpLemmas where pre : DiscrTree SimpLemma := DiscrTree.empty post : DiscrTree SimpLemma := DiscrTree.empty lemmaNames : Std.PHashSet Name := {} toUnfold : Std.PHashSet Name := {} erased : Std.PHashSet Name := {} deriving Inhabited def addSimpLemmaEntry (d : SimpLemmas) (e : SimpLemma) : SimpLemmas := if e.post then { d with post := d.post.insertCore e.keys e, lemmaNames := updateLemmaNames d.lemmaNames } else { d with pre := d.pre.insertCore e.keys e, lemmaNames := updateLemmaNames d.lemmaNames } where updateLemmaNames (s : Std.PHashSet Name) : Std.PHashSet Name := match e.name? with | none => s | some name => s.insert name def SimpLemmas.addDeclToUnfold (d : SimpLemmas) (declName : Name) : SimpLemmas := { d with toUnfold := d.toUnfold.insert declName } def SimpLemmas.isDeclToUnfold (d : SimpLemmas) (declName : Name) : Bool := d.toUnfold.contains declName def SimpLemmas.isLemma (d : SimpLemmas) (declName : Name) : Bool := d.lemmaNames.contains declName def SimpLemmas.eraseCore [Monad m] [MonadError m] (d : SimpLemmas) (declName : Name) : m SimpLemmas := do return { d with erased := d.erased.insert declName, lemmaNames := d.lemmaNames.erase declName, toUnfold := d.toUnfold.erase declName } def SimpLemmas.erase [Monad m] [MonadError m] (d : SimpLemmas) (declName : Name) : m SimpLemmas := do unless d.isLemma declName || d.isDeclToUnfold declName do throwError "'{declName}' does not have [simp] attribute" d.eraseCore declName private partial def isPerm : Expr β†’ Expr β†’ MetaM Bool | Expr.app f₁ a₁ _, Expr.app fβ‚‚ aβ‚‚ _ => isPerm f₁ fβ‚‚ <&&> isPerm a₁ aβ‚‚ | Expr.mdata _ s _, t => isPerm s t | s, Expr.mdata _ t _ => isPerm s t | s@(Expr.mvar ..), t@(Expr.mvar ..) => isDefEq s t | Expr.forallE n₁ d₁ b₁ _, Expr.forallE nβ‚‚ dβ‚‚ bβ‚‚ _ => isPerm d₁ dβ‚‚ <&&> withLocalDeclD n₁ d₁ fun x => isPerm (b₁.instantiate1 x) (bβ‚‚.instantiate1 x) | Expr.lam n₁ d₁ b₁ _, Expr.lam nβ‚‚ dβ‚‚ bβ‚‚ _ => isPerm d₁ dβ‚‚ <&&> withLocalDeclD n₁ d₁ fun x => isPerm (b₁.instantiate1 x) (bβ‚‚.instantiate1 x) | Expr.letE n₁ t₁ v₁ b₁ _, Expr.letE nβ‚‚ tβ‚‚ vβ‚‚ bβ‚‚ _ => isPerm t₁ tβ‚‚ <&&> isPerm v₁ vβ‚‚ <&&> withLetDecl n₁ t₁ v₁ fun x => isPerm (b₁.instantiate1 x) (bβ‚‚.instantiate1 x) | Expr.proj _ i₁ b₁ _, Expr.proj _ iβ‚‚ bβ‚‚ _ => i₁ == iβ‚‚ <&&> isPerm b₁ bβ‚‚ | s, t => s == t private partial def shouldPreprocess (type : Expr) : MetaM Bool := forallTelescopeReducing type fun xs result => return !result.isEq private partial def preprocess (e type : Expr) (inv : Bool) : MetaM (List (Expr Γ— Expr)) := do let type ← whnf type if type.isForall then forallTelescopeReducing type fun xs type => do let e := mkAppN e xs let ps ← preprocess e type inv ps.mapM fun (e, type) => return (← mkLambdaFVars xs e, ← mkForallFVars xs type) else if let some (_, lhs, rhs) := type.eq? then if inv then let type ← mkEq rhs lhs let e ← mkEqSymm e return [(e, type)] else return [(e, type)] else if let some (lhs, rhs) := type.iff? then if inv then let type ← mkEq rhs lhs let e ← mkEqSymm (← mkPropExt e) return [(e, type)] else let type ← mkEq lhs rhs let e ← mkPropExt e return [(e, type)] else if let some (_, lhs, rhs) := type.ne? then if inv then throwError "invalid '←' modifier in rewrite rule to 'False'" let type ← mkEq (← mkEq lhs rhs) (mkConst ``False) let e ← mkEqFalse e return [(e, type)] else if let some p := type.not? then if inv then throwError "invalid '←' modifier in rewrite rule to 'False'" let type ← mkEq p (mkConst ``False) let e ← mkEqFalse e return [(e, type)] else if let some (type₁, typeβ‚‚) := type.and? then let e₁ := mkProj ``And 0 e let eβ‚‚ := mkProj ``And 1 e return (← preprocess e₁ type₁ inv) ++ (← preprocess eβ‚‚ typeβ‚‚ inv) else if inv then throwError "invalid '←' modifier in rewrite rule to 'True'" let type ← mkEq type (mkConst ``True) let e ← mkEqTrue e return [(e, type)] private def checkTypeIsProp (type : Expr) : MetaM Unit := unless (← isProp type) do throwError "invalid 'simp', proposition expected{indentExpr type}" private def mkSimpLemmaCore (e : Expr) (levelParams : Array Name) (proof : Expr) (post : Bool) (prio : Nat) (name? : Option Name) : MetaM SimpLemma := do let type ← instantiateMVars (← inferType e) withNewMCtxDepth do let (xs, _, type) ← withReducible <| forallMetaTelescopeReducing type let type ← whnfR type let (keys, perm) ← match type.eq? with | some (_, lhs, rhs) => pure (← DiscrTree.mkPath lhs, ← isPerm lhs rhs) | none => throwError "unexpected kind of 'simp' theorem{indentExpr type}" return { keys := keys, perm := perm, post := post, levelParams := levelParams, proof := proof, name? := name?, priority := prio } private def mkSimpLemmasFromConst (declName : Name) (post : Bool) (inv : Bool) (prio : Nat) : MetaM (Array SimpLemma) := do let cinfo ← getConstInfo declName let val := mkConst declName (cinfo.levelParams.map mkLevelParam) withReducible do let type ← inferType val checkTypeIsProp type if inv || (← shouldPreprocess type) then let mut r := #[] for (val, type) in (← preprocess val type inv) do let auxName ← mkAuxLemma cinfo.levelParams type val r := r.push <| (← mkSimpLemmaCore (mkConst auxName (cinfo.levelParams.map mkLevelParam)) #[] (mkConst auxName) post prio declName) return r else #[← mkSimpLemmaCore (mkConst declName (cinfo.levelParams.map mkLevelParam)) #[] (mkConst declName) post prio declName] inductive SimpEntry where | lemma : SimpLemma β†’ SimpEntry | toUnfold : Name β†’ SimpEntry deriving Inhabited abbrev SimpExtension := SimpleScopedEnvExtension SimpEntry SimpLemmas def SimpExtension.getLemmas (ext : SimpExtension) : CoreM SimpLemmas := return ext.getState (← getEnv) def addSimpLemma (ext : SimpExtension) (declName : Name) (post : Bool) (inv : Bool) (attrKind : AttributeKind) (prio : Nat) : MetaM Unit := do let simpLemmas ← mkSimpLemmasFromConst declName post inv prio for simpLemma in simpLemmas do ext.add (SimpEntry.lemma simpLemma) attrKind def mkSimpAttr (attrName : Name) (attrDescr : String) (ext : SimpExtension) : IO Unit := registerBuiltinAttribute { name := attrName descr := attrDescr add := fun declName stx attrKind => let go : MetaM Unit := do let info ← getConstInfo declName if (← isProp info.type) then let post := if stx[1].isNone then true else stx[1][0].getKind == ``Lean.Parser.Tactic.simpPost let prio ← getAttrParamOptPrio stx[2] addSimpLemma ext declName post (inv := false) attrKind prio else if info.hasValue then ext.add (SimpEntry.toUnfold declName) attrKind else throwError "invalid 'simp', it is not a proposition nor a definition (to unfold)" discard <| go.run {} {} erase := fun declName => do let s ← ext.getState (← getEnv) let s ← s.erase declName modifyEnv fun env => ext.modifyState env fun _ => s } def mkSimpExt (extName : Name) : IO SimpExtension := registerSimpleScopedEnvExtension { name := extName initial := {} addEntry := fun d e => match e with | SimpEntry.lemma e => addSimpLemmaEntry d e | SimpEntry.toUnfold n => d.addDeclToUnfold n } def registerSimpAttr (attrName : Name) (attrDescr : String) (extName : Name := attrName.appendAfter "Ext") : IO SimpExtension := do let ext ← mkSimpExt extName mkSimpAttr attrName attrDescr ext return ext builtin_initialize simpExtension : SimpExtension ← registerSimpAttr `simp "simplification theorem" def getSimpLemmas : CoreM SimpLemmas := simpExtension.getLemmas /- Auxiliary method for adding a global declaration to a `SimpLemmas` datastructure. -/ def SimpLemmas.addConst (s : SimpLemmas) (declName : Name) (post : Bool := true) (inv : Bool := false) (prio : Nat := eval_prio default) : MetaM SimpLemmas := do let simpLemmas ← mkSimpLemmasFromConst declName post inv prio return simpLemmas.foldl addSimpLemmaEntry s def SimpLemma.getValue (simpLemma : SimpLemma) : MetaM Expr := do if simpLemma.proof.isConst && simpLemma.levelParams.isEmpty then let info ← getConstInfo simpLemma.proof.constName! if info.levelParams.isEmpty then return simpLemma.proof else return simpLemma.proof.updateConst! (← info.levelParams.mapM (fun _ => mkFreshLevelMVar)) else let us ← simpLemma.levelParams.mapM fun _ => mkFreshLevelMVar simpLemma.proof.instantiateLevelParamsArray simpLemma.levelParams us private def preprocessProof (val : Expr) (inv : Bool) : MetaM (Array Expr) := do let type ← inferType val checkTypeIsProp type let ps ← preprocess val type inv return ps.toArray.map fun (val, _) => val /- Auxiliary method for creating simp lemmas from a proof term `val`. -/ def mkSimpLemmas (levelParams : Array Name) (proof : Expr) (post : Bool := true) (inv : Bool := false) (prio : Nat := eval_prio default) (name? : Option Name := none): MetaM (Array SimpLemma) := withReducible do (← preprocessProof proof inv).mapM fun val => mkSimpLemmaCore val levelParams val post prio name? /- Auxiliary method for adding a local simp lemma to a `SimpLemmas` datastructure. -/ def SimpLemmas.add (s : SimpLemmas) (levelParams : Array Name) (proof : Expr) (inv : Bool := false) (post : Bool := true) (prio : Nat := eval_prio default) (name? : Option Name := none): MetaM SimpLemmas := do if proof.isConst then s.addConst proof.constName! post inv prio else let simpLemmas ← mkSimpLemmas levelParams proof post inv prio (← getName? proof) return simpLemmas.foldl addSimpLemmaEntry s where getName? (e : Expr) : MetaM (Option Name) := do match name? with | some _ => return name? | none => let f := e.getAppFn if f.isConst then return f.constName! else if f.isFVar then let localDecl ← getFVarLocalDecl f return localDecl.userName else return none end Lean.Meta
0132fc4b0cf52dbf1db310d7ee06907002f207d3
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/apply_rules.lean
529d3dd7889aaaf1298ad3e9a7509dcc3eae8986
[ "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
1,126
lean
import data.nat.basic open nat 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 -- test that metavariables created for implicit arguments don't get stuck example (P : β„• β†’ Type) (f : Ξ  {n : β„•}, P n β†’ P (n + 1)) (g : P 0) : P 2 := begin apply_rules [f, g], end
89e6453eaee92e0ddfbe601ceec6753233abe146
680b0d1592ce164979dab866b232f6fa743f2cc8
/library/algebra/module.lean
edb5eedf88b77516d5ba6d9d36bf6f01e26842e7
[ "Apache-2.0" ]
permissive
syohex/lean
657428ab520f8277fc18cf04bea2ad200dbae782
081ad1212b686780f3ff8a6d0e5f8a1d29a7d8bc
refs/heads/master
1,611,274,838,635
1,452,668,188,000
1,452,668,188,000
49,562,028
0
0
null
1,452,675,604,000
1,452,675,602,000
null
UTF-8
Lean
false
false
4,455
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 Modules and vector spaces over a ring. -/ import algebra.field structure has_scalar [class] (F V : Type) := (smul : F β†’ V β†’ V) infixl ` β€’ `:73 := has_scalar.smul /- modules over a ring -/ structure left_module [class] (R M : Type) [ringR : ring R] extends has_scalar R M, add_comm_group M := (smul_left_distrib : βˆ€ (r : R) (x y : M), smul r (add x y) = (add (smul r x) (smul r y))) (smul_right_distrib : βˆ€ (r s : R) (x : M), smul (ring.add r s) x = (add (smul r x) (smul s x))) (smul_mul : βˆ€ r s x, smul (mul r s) x = smul r (smul s x)) (one_smul : βˆ€ x, smul one x = x) section left_module variables {R M : Type} variable [ringR : ring R] variable [moduleRM : left_module R M] include ringR moduleRM -- Note: the anonymous include does not work in the propositions below. proposition smul_left_distrib (a : R) (u v : M) : a β€’ (u + v) = a β€’ u + a β€’ v := !left_module.smul_left_distrib proposition smul_right_distrib (a b : R) (u : M) : (a + b)β€’u = aβ€’u + bβ€’u := !left_module.smul_right_distrib proposition smul_mul (a : R) (b : R) (u : M) : (a * b) β€’ u = a β€’ (b β€’ u) := !left_module.smul_mul proposition one_smul (u : M) : (1 : R) β€’ u = u := !left_module.one_smul proposition zero_smul (u : M) : (0 : R) β€’ u = 0 := have (0 : R) β€’ u + 0 β€’ u = 0 β€’ u + 0, by rewrite [-smul_right_distrib, *add_zero], !add.left_cancel this proposition smul_zero (a : R) : a β€’ (0 : M) = 0 := have a β€’ 0 + a β€’ 0 = a β€’ 0 + 0, by rewrite [-smul_left_distrib, *add_zero], !add.left_cancel this proposition neg_smul (a : R) (u : M) : (-a) β€’ u = - (a β€’ u) := eq_neg_of_add_eq_zero (by rewrite [-smul_right_distrib, add.left_inv, zero_smul]) proposition neg_one_smul (u : M) : -(1 : R) β€’ u = -u := by rewrite [neg_smul, one_smul] end left_module /- linear maps -/ structure is_linear_map [class] (R : Type) {M₁ Mβ‚‚ : Type} [smul₁ : has_scalar R M₁] [smulβ‚‚ : has_scalar R Mβ‚‚] [add₁ : has_add M₁] [addβ‚‚ : has_add Mβ‚‚] (T : M₁ β†’ Mβ‚‚) := (additive : βˆ€ u v : M₁, T (u + v) = T u + T v) (homogeneous : βˆ€ a : R, βˆ€ u : M₁, T (a β€’ u) = a β€’ T u) proposition linear_map_additive (R : Type) {M₁ Mβ‚‚ : Type} [smul₁ : has_scalar R M₁] [smulβ‚‚ : has_scalar R Mβ‚‚] [add₁ : has_add M₁] [addβ‚‚ : has_add Mβ‚‚] (T : M₁ β†’ Mβ‚‚) [linT : is_linear_map R T] (u v : M₁) : T (u + v) = T u + T v := is_linear_map.additive smul₁ smulβ‚‚ _ _ T u v proposition linear_map_homogeneous {R M₁ Mβ‚‚ : Type} [smul₁ : has_scalar R M₁] [smulβ‚‚ : has_scalar R Mβ‚‚] [add₁ : has_add M₁] [addβ‚‚ : has_add Mβ‚‚] (T : M₁ β†’ Mβ‚‚) [linT : is_linear_map R T] (a : R) (u : M₁) : T (a β€’ u) = a β€’ T u := is_linear_map.homogeneous smul₁ smulβ‚‚ _ _ T a u proposition is_linear_map_id [instance] (R : Type) {M : Type} [smulRM : has_scalar R M] [has_addM : has_add M] : is_linear_map R (id : M β†’ M) := is_linear_map.mk (take u v, rfl) (take a u, rfl) section is_linear_map variables {R M₁ Mβ‚‚ : Type} variable [ringR : ring R] variable [moduleRM₁ : left_module R M₁] variable [moduleRMβ‚‚ : left_module R Mβ‚‚] include ringR moduleRM₁ moduleRMβ‚‚ variable T : M₁ β†’ Mβ‚‚ variable [is_linear_mapT : is_linear_map R T] include is_linear_mapT proposition linear_map_zero : T 0 = 0 := calc T 0 = T ((0 : R) β€’ 0) : zero_smul ... = (0 : R) β€’ T 0 : linear_map_homogeneous T ... = 0 : zero_smul proposition linear_map_neg (u : M₁) : T (-u) = -(T u) := by rewrite [-neg_one_smul, linear_map_homogeneous T, neg_one_smul] proposition linear_map_smul_add_smul (a b : R) (u v : M₁) : T (a β€’ u + b β€’ v) = a β€’ T u + b β€’ T v := by rewrite [linear_map_additive R T, *linear_map_homogeneous T] end is_linear_map /- vector spaces -/ structure vector_space [class] (F V : Type) [fieldF : field F] extends left_module F V /- an example -/ section variables (F V : Type) variables [field F] [vector_spaceFV : vector_space F V] variable T : V β†’ V variable [is_linear_map F T] include vector_spaceFV example (a b : F) (u v : V) : T (a β€’ u + b β€’ v) = a β€’ T u + b β€’ T v := !linear_map_smul_add_smul end
6eaa5fe65e4f7f93b2671e0511d0cd75376d6ba6
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/bad_quoted_symbol.lean
c510326432418ebb82b8fe675b37b8651463ad43
[ "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
121
lean
notation a ` \/ ` b := a ∨ b notation a `1\/` b := a ∨ b notation a ` \ / ` b := a ∨ b notation a ` ` b := a ∨ b
3bb9654c602ccaca994683d90fff0525145d5787
63abd62053d479eae5abf4951554e1064a4c45b4
/src/tactic/suggest.lean
962fd1d7eac98ffec305671c7296dbe125ac5dce
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
20,829
lean
/- Copyright (c) 2019 Lucas Allen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Lucas Allen and Scott Morrison -/ import data.mllist import tactic.solve_by_elim /-! # `suggest` and `library_search` `suggest` and `library_search` are a pair of tactics for applying lemmas from the library to the current goal. * `suggest` prints a list of `exact ...` or `refine ...` statements, which may produce new goals * `library_search` prints a single `exact ...` which closes the goal, or fails -/ namespace tactic open native namespace suggest open solve_by_elim /-- Map a name (typically a head symbol) to a "canonical" definitional synonym. Given a name `n`, we want a name `n'` such that a sufficiently applied expression with head symbol `n` is always definitionally equal to an expression with head symbol `n'`. Thus, we can search through all lemmas with a result type of `n'` to solve a goal with head symbol `n`. For example, `>` is mapped to `<` because `a > b` is definitionally equal to `b < a`, and `not` is mapped to `false` because `Β¬ a` is definitionally equal to `p β†’ false` The default is that the original argument is returned, so `<` is just mapped to `<`. `normalize_synonym` is called for every lemma in the library, so it needs to be fast. -/ -- TODO this is a hack; if you suspect more cases here would help, please report them meta def normalize_synonym : name β†’ name | `gt := `has_lt.lt | `ge := `has_le.le | `monotone := `has_le.le | `not := `false | n := n /-- Compute the head symbol of an expression, then normalise synonyms. This is only used when analysing the goal, so it is okay to do more expensive analysis here. -/ -- We may want to tweak this further? meta def allowed_head_symbols : expr β†’ list name -- We first have a various "customisations": -- Because in `β„•` `a.succ ≀ b` is definitionally `a < b`, -- we add some special cases to allow looking for `<` lemmas even when the goal has a `≀`. -- Note we only do this in the `β„•` case, for performance. | `(@has_le.le β„• _ (nat.succ _) _) := [`has_le.le, `has_lt.lt] | `(@ge β„• _ _ (nat.succ _)) := [`has_le.le, `has_lt.lt] | `(@has_le.le β„• _ 1 _) := [`has_le.le, `has_lt.lt] | `(@ge β„• _ _ 1) := [`has_le.le, `has_lt.lt] -- And then the generic cases: | (expr.pi _ _ _ t) := allowed_head_symbols t | (expr.app f _) := allowed_head_symbols f | (expr.const n _) := [normalize_synonym n] | _ := [`_] . /-- A declaration can match the head symbol of the current goal in four possible ways: * `ex` : an exact match * `mp` : the declaration returns an `iff`, and the right hand side matches the goal * `mpr` : the declaration returns an `iff`, and the left hand side matches the goal * `both`: the declaration returns an `iff`, and the both sides match the goal -/ @[derive decidable_eq, derive inhabited] inductive head_symbol_match | ex | mp | mpr | both open head_symbol_match /-- a textual representation of a `head_symbol_match`, for trace debugging. -/ def head_symbol_match.to_string : head_symbol_match β†’ string | ex := "exact" | mp := "iff.mp" | mpr := "iff.mpr" | both := "iff.mp and iff.mpr" /-- Determine if, and in which way, a given expression matches the specified head symbol. -/ meta def match_head_symbol (hs : name_set) : expr β†’ option head_symbol_match | (expr.pi _ _ _ t) := match_head_symbol t | `(%%a ↔ %%b) := if hs.contains `iff then some ex else match (match_head_symbol a, match_head_symbol b) with | (some ex, some ex) := some both | (some ex, _) := some mpr | (_, some ex) := some mp | _ := none end | (expr.app f _) := match_head_symbol f | (expr.const n _) := if hs.contains (normalize_synonym n) then some ex else none | _ := if hs.contains `_ then some ex else none /-- A package of `declaration` metadata, including the way in which its type matches the head symbol which we are searching for. -/ meta structure decl_data := (d : declaration) (n : name) (m : head_symbol_match) (l : β„•) -- cached length of name /-- Generate a `decl_data` from the given declaration if it matches the head symbol `hs` for the current goal. -/ -- We used to check here for private declarations, or declarations with certain suffixes. -- It turns out `apply` is so fast, it's better to just try them all. meta def process_declaration (hs : name_set) (d : declaration) : option decl_data := let n := d.to_name in if !d.is_trusted || n.is_internal then none else (Ξ» m, ⟨d, n, m, n.length⟩) <$> match_head_symbol hs d.type /-- Retrieve all library definitions with a given head symbol. -/ meta def library_defs (hs : name_set) : tactic (list decl_data) := do trace_if_enabled `suggest format!"Looking for lemmas with head symbols {hs}.", env ← get_env, let defs := env.decl_filter_map (process_declaration hs), -- Sort by length; people like short proofs let defs := defs.qsort(Ξ» d₁ dβ‚‚, d₁.l ≀ dβ‚‚.l), trace_if_enabled `suggest format!"Found {defs.length} relevant lemmas:", trace_if_enabled `suggest $ defs.map (Ξ» ⟨d, n, m, l⟩, (n, m.to_string)), return defs /-- We unpack any element of a list of `decl_data` corresponding to an `↔` statement that could apply in both directions into two separate elements. This ensures that both directions can be independently returned by `suggest`, and avoids a problem where the application of one direction prevents the application of the other direction. (See `exp_le_exp` in the tests.) -/ meta def unpack_iff_both : list decl_data β†’ list decl_data | [] := [] | (⟨d, n, both, l⟩ :: L) := ⟨d, n, mp, l⟩ :: ⟨d, n, mpr, l⟩ :: unpack_iff_both L | (⟨d, n, m, l⟩ :: L) := ⟨d, n, m, l⟩ :: unpack_iff_both L /-- Apply the lemma `e`, then attempt to close all goals using `solve_by_elim opt`, failing if `close_goals = tt` and there are any goals remaining. Returns the number of subgoals which were closed using `solve_by_elim`. -/ -- Implementation note: as this is used by both `library_search` and `suggest`, -- we first run `solve_by_elim` separately on the independent goals, -- whether or not `close_goals` is set, -- and then run `solve_by_elim { all_goals := tt }`, -- requiring that it succeeds if `close_goals = tt`. meta def apply_and_solve (close_goals : bool) (opt : opt := { }) (e : expr) : tactic β„• := do trace_if_enabled `suggest format!"Trying to apply lemma: {e}", apply e opt.to_apply_cfg, trace_if_enabled `suggest format!"Applied lemma: {e}", ng ← num_goals, -- Phase 1 -- Run `solve_by_elim` on each "safe" goal separately, not worrying about failures. -- (We only attempt the "safe" goals in this way in Phase 1. In Phase 2 we will do -- backtracking search across all goals, allowing us to guess solutions that involve data, or -- unify metavariables, but only as long as we can finish all goals.) try (any_goals (independent_goal >> solve_by_elim opt)), -- Phase 2 (done >> return ng) <|> (do -- If there were any goals that we did not attempt solving in the first phase -- (because they weren't propositional, or contained a metavariable) -- as a second phase we attempt to solve all remaining goals at once -- (with backtracking across goals). (any_goals (success_if_fail independent_goal) >> solve_by_elim { backtrack_all_goals := tt, ..opt }) <|> -- and fail unless `close_goals = ff` guard Β¬ close_goals, ng' ← num_goals, return (ng - ng')) /-- Apply the declaration `d` (or the forward and backward implications separately, if it is an `iff`), and then attempt to solve the subgoal using `apply_and_solve`. Returns the number of subgoals successfully closed. -/ meta def apply_declaration (close_goals : bool) (opt : opt := { }) (d : decl_data) : tactic β„• := let tac := apply_and_solve close_goals opt in do (e, t) ← decl_mk_const d.d, match d.m with | ex := tac e | mp := do l ← iff_mp_core e t, tac l | mpr := do l ← iff_mpr_core e t, tac l | both := undefined -- we use `unpack_iff_both` to ensure this isn't reachable end /-- An `application` records the result of a successful application of a library lemma. -/ meta structure application := (state : tactic_state) (script : string) (decl : option declaration) (num_goals : β„•) (hyps_used : β„•) end suggest open solve_by_elim open suggest declare_trace suggest -- Trace a list of all relevant lemmas -- Call `apply_declaration`, then prepare the tactic script and -- count the number of local hypotheses used. private meta def apply_declaration_script (g : expr) (hyps : list expr) (opt : opt := { }) (d : decl_data) : tactic application := -- (This tactic block is only executed when we evaluate the mllist, -- so we need to do the `focus1` here.) retrieve $ focus1 $ do apply_declaration ff opt d, ng ← num_goals, -- This `instantiate_mvars` is necessary so that we count used hypotheses correctly. g ← instantiate_mvars g, s ← read, m ← tactic_statement g, return { application . state := s, decl := d.d, script := m, num_goals := ng, hyps_used := hyps.countp (Ξ» h, h.occurs g) } -- implementation note: we produce a `tactic (mllist tactic application)` first, -- because it's easier to work in the tactic monad, but in a moment we squash this -- down to an `mllist tactic application`. private meta def suggest_core' (opt : opt := { }) : tactic (mllist tactic application) := do g :: _ ← get_goals, hyps ← local_context, -- Make sure that `solve_by_elim` doesn't just solve the goal immediately: (retrieve (do focus1 $ solve_by_elim opt, s ← read, m ← tactic_statement g, -- This `instantiate_mvars` is necessary so that we count used hypotheses correctly. g ← instantiate_mvars g, return $ mllist.of_list [⟨s, m, none, 0, hyps.countp (Ξ» h, h.occurs g)⟩])) <|> -- Otherwise, let's actually try applying library lemmas. (do -- Collect all definitions with the correct head symbol t ← infer_type g, defs ← unpack_iff_both <$> library_defs (name_set.of_list $ allowed_head_symbols t), let defs : mllist tactic _ := mllist.of_list defs, -- Try applying each lemma against the goal, -- recording the tactic script as a string, -- the number of remaining goals, -- and number of local hypotheses used. let results := defs.mfilter_map (apply_declaration_script g hyps opt), -- Now call `symmetry` and try again. -- (Because we are using `mllist`, this is essentially free if we've already found a lemma.) symm_state ← retrieve $ try_core $ symmetry >> read, let results_symm := match symm_state with | (some s) := defs.mfilter_map (Ξ» d, retrieve $ set_state s >> apply_declaration_script g hyps opt d) | none := mllist.nil end, return (results.append results_symm)) /-- The core `suggest` tactic. It attempts to apply a declaration from the library, then solve new goals using `solve_by_elim`. It returns a list of `application`s consisting of fields: * `state`, a tactic state resulting from the successful application of a declaration from the library, * `script`, a string of the form `Try this: refine ...` or `Try this: exact ...` which will reproduce that tactic state, * `decl`, an `option declaration` indicating the declaration that was applied (or none, if `solve_by_elim` succeeded), * `num_goals`, the number of remaining goals, and * `hyps_used`, the number of local hypotheses used in the solution. -/ meta def suggest_core (opt : opt := { }) : mllist tactic application := (mllist.monad_lift (suggest_core' opt)).join /-- See `suggest_core`. Returns a list of at most `limit` `application`s, sorted by number of goals, and then (reverse) number of hypotheses used. -/ meta def suggest (limit : option β„• := none) (opt : opt := { }) : tactic (list application) := do let results := suggest_core opt, -- Get the first n elements of the successful lemmas L ← if h : limit.is_some then results.take (option.get h) else results.force, -- Sort by number of remaining goals, then by number of hypotheses used. return $ L.qsort (Ξ» d₁ dβ‚‚, d₁.num_goals < dβ‚‚.num_goals ∨ (d₁.num_goals = dβ‚‚.num_goals ∧ d₁.hyps_used β‰₯ dβ‚‚.hyps_used)) /-- Returns a list of at most `limit` strings, of the form `Try this: exact ...` or `Try this: refine ...`, which make progress on the current goal using a declaration from the library. -/ meta def suggest_scripts (limit : option β„• := none) (opt : opt := { }) : tactic (list string) := do L ← suggest limit opt, return $ L.map application.script /-- Returns a string of the form `Try this: exact ...`, which closes the current goal. -/ meta def library_search (opt : opt := { }) : tactic string := (suggest_core opt).mfirst (Ξ» a, do guard (a.num_goals = 0), write a.state, return a.script) namespace interactive open tactic open interactive open lean.parser open interactive.types open solve_by_elim local postfix `?`:9001 := optional declare_trace silence_suggest -- Turn off `Try this: exact/refine ...` trace messages for `suggest` /-- `suggest` tries to apply suitable theorems/defs from the library, and generates a list of `exact ...` or `refine ...` scripts that could be used at this step. It leaves the tactic state unchanged. It is intended as a complement of the search function in your editor, the `#find` tactic, and `library_search`. `suggest` takes an optional natural number `num` as input and returns the first `num` (or less, if all possibilities are exhausted) possibilities ordered by length of lemma names. The default for `num` is `50`. For performance reasons `suggest` uses monadic lazy lists (`mllist`). This means that `suggest` might miss some results if `num` is not large enough. However, because `suggest` uses monadic lazy lists, smaller values of `num` run faster than larger values. You can add additional lemmas to be used along with local hypotheses after the application of a library lemma, using the same syntax as for `solve_by_elim`, e.g. ``` example {a b c d: nat} (h₁ : a < c) (hβ‚‚ : b < d) : max (c + d) (a + b) = (c + d) := begin suggest [add_lt_add], -- Says: `Try this: exact max_eq_left_of_lt (add_lt_add h₁ hβ‚‚)` end ``` You can also use `suggest with attr` to include all lemmas with the attribute `attr`. -/ meta def suggest (n : parse (with_desc "n" small_nat)?) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (opt : opt := { }) : tactic unit := do (lemma_thunks, ctx_thunk) ← mk_assumption_set ff hs attr_names, L ← tactic.suggest_scripts (n.get_or_else 50) { lemma_thunks := some lemma_thunks, ctx_thunk := ctx_thunk, ..opt }, if is_trace_enabled_for `silence_suggest then skip else if L.length = 0 then fail "There are no applicable declarations" else L.mmap trace >> skip /-- `suggest` lists possible usages of the `refine` tactic and leaves the tactic state unchanged. It is intended as a complement of the search function in your editor, the `#find` tactic, and `library_search`. `suggest` takes an optional natural number `num` as input and returns the first `num` (or less, if all possibilities are exhausted) possibilities ordered by length of lemma names. The default for `num` is `50`. For performance reasons `suggest` uses monadic lazy lists (`mllist`). This means that `suggest` might miss some results if `num` is not large enough. However, because `suggest` uses monadic lazy lists, smaller values of `num` run faster than larger values. An example of `suggest` in action, ```lean example (n : nat) : n < n + 1 := begin suggest, sorry end ``` prints the list, ```lean Try this: exact nat.lt.base n Try this: exact nat.lt_succ_self n Try this: refine not_le.mp _ Try this: refine gt_iff_lt.mp _ Try this: refine nat.lt.step _ Try this: refine lt_of_not_ge _ ... ``` -/ add_tactic_doc { name := "suggest", category := doc_category.tactic, decl_names := [`tactic.interactive.suggest], tags := ["search", "Try this"] } -- Turn off `Try this: exact ...` trace message for `library_search` declare_trace silence_library_search /-- `library_search` attempts to apply every definition in the library whose head symbol matches the goal, and then discharge any new goals using `solve_by_elim`. If it succeeds, it prints a trace message `exact ...` which can replace the invocation of `library_search`. By default `library_search` only unfolds `reducible` definitions when attempting to match lemmas against the goal. Previously, it would unfold most definitions, sometimes giving surprising answers, or slow answers. The old behaviour is still available via `library_search!`. You can add additional lemmas to be used along with local hypotheses after the application of a library lemma, using the same syntax as for `solve_by_elim`, e.g. ``` example {a b c d: nat} (h₁ : a < c) (hβ‚‚ : b < d) : max (c + d) (a + b) = (c + d) := begin library_search [add_lt_add], -- Says: `Try this: exact max_eq_left_of_lt (add_lt_add h₁ hβ‚‚)` end ``` You can also use `library_search with attr` to include all lemmas with the attribute `attr`. -/ meta def library_search (semireducible : parse $ optional (tk "!")) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (opt : opt := { }) : tactic unit := do (lemma_thunks, ctx_thunk) ← mk_assumption_set ff hs attr_names, (tactic.library_search { backtrack_all_goals := tt, lemma_thunks := some lemma_thunks, ctx_thunk := ctx_thunk, md := if semireducible.is_some then tactic.transparency.semireducible else tactic.transparency.reducible, ..opt } >>= if is_trace_enabled_for `silence_library_search then (Ξ» _, skip) else trace) <|> fail "`library_search` failed. If you aren't sure what to do next, you can also try `library_search!`, `suggest`, or `hint`. Possible reasons why `library_search` failed: * `library_search` will only apply a single lemma from the library, and then try to fill in its hypotheses from local hypotheses. * If you haven't already, try stating the theorem you want in its own lemma. * Sometimes the library has one version of a lemma but not a very similar version obtained by permuting arguments. Try replacing `a + b` with `b + a`, or `a - b < c` with `a < b + c`, to see if maybe the lemma exists but isn't stated quite the way you would like. * Make sure that you have all the side conditions for your theorem to be true. For example you won't find `a - b + b = a` for natural numbers in the library because it's false! Search for `b ≀ a β†’ a - b + b = a` instead. * If a definition you made is in the goal, you won't find any theorems about it in the library. Try unfolding the definition using `unfold my_definition`. * If all else fails, ask on https://leanprover.zulipchat.com/, and maybe we can improve the library and/or `library_search` for next time." /-- `library_search` is a tactic to identify existing lemmas in the library. It tries to close the current goal by applying a lemma from the library, then discharging any new goals using `solve_by_elim`. Typical usage is: ```lean example (n m k : β„•) : n * (m - k) = n * m - n * k := by library_search -- Try this: exact nat.mul_sub_left_distrib n m k ``` `library_search` prints a trace message showing the proof it found, shown above as a comment. Typically you will then copy and paste this proof, replacing the call to `library_search`. -/ add_tactic_doc { name := "library_search", category := doc_category.tactic, decl_names := [`tactic.interactive.library_search], tags := ["search", "Try this"] } end interactive /-- Invoking the hole command `library_search` ("Use `library_search` to complete the goal") calls the tactic `library_search` to produce a proof term with the type of the hole. Running it on ```lean example : 0 < 1 := {!!} ``` produces ```lean example : 0 < 1 := nat.one_pos ``` -/ @[hole_command] meta def library_search_hole_cmd : hole_command := { name := "library_search", descr := "Use `library_search` to complete the goal.", action := Ξ» _, do script ← library_search, -- Is there a better API for dropping the 'Try this: exact ' prefix on this string? return [((script.get_rest "Try this: exact ").get_or_else script, "by library_search")] } add_tactic_doc { name := "library_search", category := doc_category.hole_cmd, decl_names := [`tactic.library_search_hole_cmd], tags := ["search", "Try this"] } end tactic
5f5e50a3061c3b081ecdc55af6488ac47e0f4f2e
9028d228ac200bbefe3a711342514dd4e4458bff
/src/data/num/basic.lean
566b2603f74c838f5761cf504e109920efbbd594
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,605
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ /-! # Binary representation of integers using inductive types Note: Unlike in Coq, where this representation is preferred because of the reliance on kernel reduction, in Lean this representation is discouraged in favor of the "Peano" natural numbers `nat`, and the purpose of this collection of theorems is to show the equivalence of the different approaches. -/ /-- The type of positive binary numbers. 13 = 1101(base 2) = bit1 (bit0 (bit1 one)) -/ @[derive has_reflect, derive decidable_eq] inductive pos_num : Type | one : pos_num | bit1 : pos_num β†’ pos_num | bit0 : pos_num β†’ pos_num instance : has_one pos_num := ⟨pos_num.one⟩ instance : inhabited pos_num := ⟨1⟩ /-- The type of nonnegative binary numbers, using `pos_num`. 13 = 1101(base 2) = pos (bit1 (bit0 (bit1 one))) -/ @[derive has_reflect, derive decidable_eq] inductive num : Type | zero : num | pos : pos_num β†’ num instance : has_zero num := ⟨num.zero⟩ instance : has_one num := ⟨num.pos 1⟩ instance : inhabited num := ⟨0⟩ /-- Representation of integers using trichotomy around zero. 13 = 1101(base 2) = pos (bit1 (bit0 (bit1 one))) -13 = -1101(base 2) = neg (bit1 (bit0 (bit1 one))) -/ @[derive has_reflect, derive decidable_eq] inductive znum : Type | zero : znum | pos : pos_num β†’ znum | neg : pos_num β†’ znum instance : has_zero znum := ⟨znum.zero⟩ instance : has_one znum := ⟨znum.pos 1⟩ instance : inhabited znum := ⟨0⟩ namespace pos_num /-- `bit b n` appends the bit `b` to the end of `n`, where `bit tt x = x1` and `bit ff x = x0`. -/ def bit (b : bool) : pos_num β†’ pos_num := cond b bit1 bit0 /-- The successor of a `pos_num`. -/ def succ : pos_num β†’ pos_num | 1 := bit0 one | (bit1 n) := bit0 (succ n) | (bit0 n) := bit1 n /-- Returns a boolean for whether the `pos_num` is `one`. -/ def is_one : pos_num β†’ bool | 1 := tt | _ := ff /-- Addition of two `pos_num`s. -/ protected def add : pos_num β†’ pos_num β†’ pos_num | 1 b := succ b | a 1 := succ a | (bit0 a) (bit0 b) := bit0 (add a b) | (bit1 a) (bit1 b) := bit0 (succ (add a b)) | (bit0 a) (bit1 b) := bit1 (add a b) | (bit1 a) (bit0 b) := bit1 (add a b) instance : has_add pos_num := ⟨pos_num.add⟩ /-- The predecessor of a `pos_num` as a `num`. -/ def pred' : pos_num β†’ num | 1 := 0 | (bit0 n) := num.pos (num.cases_on (pred' n) 1 bit1) | (bit1 n) := num.pos (bit0 n) /-- The predecessor of a `pos_num` as a `pos_num`. This means that `pred 1 = 1`. -/ def pred (a : pos_num) : pos_num := num.cases_on (pred' a) 1 id /-- The number of bits of a `pos_num`, as a `pos_num`. -/ def size : pos_num β†’ pos_num | 1 := 1 | (bit0 n) := succ (size n) | (bit1 n) := succ (size n) /-- The number of bits of a `pos_num`, as a `nat`. -/ def nat_size : pos_num β†’ nat | 1 := 1 | (bit0 n) := nat.succ (nat_size n) | (bit1 n) := nat.succ (nat_size n) /-- Multiplication of two `pos_num`s. -/ protected def mul (a : pos_num) : pos_num β†’ pos_num | 1 := a | (bit0 b) := bit0 (mul b) | (bit1 b) := bit0 (mul b) + a instance : has_mul pos_num := ⟨pos_num.mul⟩ /-- `of_nat_succ n` is the `pos_num` corresponding to `n + 1`. -/ def of_nat_succ : β„• β†’ pos_num | 0 := 1 | (nat.succ n) := succ (of_nat_succ n) /-- `of_nat n` is the `pos_num` corresponding to `n`, except for `of_nat 0 = 1`. -/ def of_nat (n : β„•) : pos_num := of_nat_succ (nat.pred n) open ordering /-- Ordering of `pos_num`s. -/ def cmp : pos_num β†’ pos_num β†’ ordering | 1 1 := eq | _ 1 := gt | 1 _ := lt | (bit0 a) (bit0 b) := cmp a b | (bit0 a) (bit1 b) := ordering.cases_on (cmp a b) lt lt gt | (bit1 a) (bit0 b) := ordering.cases_on (cmp a b) lt gt gt | (bit1 a) (bit1 b) := cmp a b instance : has_lt pos_num := ⟨λa b, cmp a b = ordering.lt⟩ instance : has_le pos_num := ⟨λa b, Β¬ b < a⟩ instance decidable_lt : @decidable_rel pos_num (<) | a b := by dsimp [(<)]; apply_instance instance decidable_le : @decidable_rel pos_num (≀) | a b := by dsimp [(≀)]; apply_instance end pos_num section variables {Ξ± : Type*} [has_zero Ξ±] [has_one Ξ±] [has_add Ξ±] /-- `cast_pos_num` casts a `pos_num` into any type which has `0`, `1` and `+`. -/ def cast_pos_num : pos_num β†’ Ξ± | 1 := 1 | (pos_num.bit0 a) := bit0 (cast_pos_num a) | (pos_num.bit1 a) := bit1 (cast_pos_num a) /-- `cast_num` casts a `num` into any type which has `0`, `1` and `+`. -/ def cast_num : num β†’ Ξ± | 0 := 0 | (num.pos p) := cast_pos_num p -- see Note [coercion into rings] @[priority 900] instance pos_num_coe : has_coe_t pos_num Ξ± := ⟨cast_pos_num⟩ -- see Note [coercion into rings] @[priority 900] instance num_nat_coe : has_coe_t num Ξ± := ⟨cast_num⟩ instance : has_repr pos_num := ⟨λ n, repr (n : β„•)⟩ instance : has_repr num := ⟨λ n, repr (n : β„•)⟩ end namespace num open pos_num /-- The successor of a `num` as a `pos_num`. -/ def succ' : num β†’ pos_num | 0 := 1 | (pos p) := succ p /-- The successor of a `num` as a `num`. -/ def succ (n : num) : num := pos (succ' n) /-- Addition of two `num`s. -/ protected def add : num β†’ num β†’ num | 0 a := a | b 0 := b | (pos a) (pos b) := pos (a + b) instance : has_add num := ⟨num.add⟩ /-- `bit0 n` appends a `0` to the end of `n`, where `bit0 n = n0`. -/ protected def bit0 : num β†’ num | 0 := 0 | (pos n) := pos (pos_num.bit0 n) /-- `bit1 n` appends a `1` to the end of `n`, where `bit1 n = n1`. -/ protected def bit1 : num β†’ num | 0 := 1 | (pos n) := pos (pos_num.bit1 n) /-- `bit b n` appends the bit `b` to the end of `n`, where `bit tt x = x1` and `bit ff x = x0`. -/ def bit (b : bool) : num β†’ num := cond b num.bit1 num.bit0 /-- The number of bits required to represent a `num`, as a `num`. `size 0` is defined to be `0`. -/ def size : num β†’ num | 0 := 0 | (pos n) := pos (pos_num.size n) /-- The number of bits required to represent a `num`, as a `nat`. `size 0` is defined to be `0`. -/ def nat_size : num β†’ nat | 0 := 0 | (pos n) := pos_num.nat_size n /-- Multiplication of two `num`s. -/ protected def mul : num β†’ num β†’ num | 0 _ := 0 | _ 0 := 0 | (pos a) (pos b) := pos (a * b) instance : has_mul num := ⟨num.mul⟩ open ordering /-- Ordering of `num`s. -/ def cmp : num β†’ num β†’ ordering | 0 0 := eq | _ 0 := gt | 0 _ := lt | (pos a) (pos b) := pos_num.cmp a b instance : has_lt num := ⟨λa b, cmp a b = ordering.lt⟩ instance : has_le num := ⟨λa b, Β¬ b < a⟩ instance decidable_lt : @decidable_rel num (<) | a b := by dsimp [(<)]; apply_instance instance decidable_le : @decidable_rel num (≀) | a b := by dsimp [(≀)]; apply_instance /-- Converts a `num` to a `znum`. -/ def to_znum : num β†’ znum | 0 := 0 | (pos a) := znum.pos a /-- Converts `x : num` to `-x : znum`. -/ def to_znum_neg : num β†’ znum | 0 := 0 | (pos a) := znum.neg a /-- Converts a `nat` to a `num`. -/ def of_nat' : β„• β†’ num := nat.binary_rec 0 (Ξ» b n, cond b num.bit1 num.bit0) end num namespace znum open pos_num /-- The negation of a `znum`. -/ def zneg : znum β†’ znum | 0 := 0 | (pos a) := neg a | (neg a) := pos a instance : has_neg znum := ⟨zneg⟩ /-- The absolute value of a `znum` as a `num`. -/ def abs : znum β†’ num | 0 := 0 | (pos a) := num.pos a | (neg a) := num.pos a /-- The successor of a `znum`. -/ def succ : znum β†’ znum | 0 := 1 | (pos a) := pos (pos_num.succ a) | (neg a) := (pos_num.pred' a).to_znum_neg /-- The predecessor of a `znum`. -/ def pred : znum β†’ znum | 0 := neg 1 | (pos a) := (pos_num.pred' a).to_znum | (neg a) := neg (pos_num.succ a) /-- `bit0 n` appends a `0` to the end of `n`, where `bit0 n = n0`. -/ protected def bit0 : znum β†’ znum | 0 := 0 | (pos n) := pos (pos_num.bit0 n) | (neg n) := neg (pos_num.bit0 n) /-- `bit1 x` appends a `1` to the end of `x`, mapping `x` to `2 * x + 1`. -/ protected def bit1 : znum β†’ znum | 0 := 1 | (pos n) := pos (pos_num.bit1 n) | (neg n) := neg (num.cases_on (pred' n) 1 pos_num.bit1) /-- `bitm1 x` appends a `1` to the end of `x`, mapping `x` to `2 * x - 1`. -/ protected def bitm1 : znum β†’ znum | 0 := neg 1 | (pos n) := pos (num.cases_on (pred' n) 1 pos_num.bit1) | (neg n) := neg (pos_num.bit1 n) /-- Converts an `int` to a `znum`. -/ def of_int' : β„€ β†’ znum | (n : β„•) := num.to_znum (num.of_nat' n) | -[1+ n] := num.to_znum_neg (num.of_nat' (n+1)) end znum namespace pos_num open znum /-- Subtraction of two `pos_num`s, producing a `znum`. -/ def sub' : pos_num β†’ pos_num β†’ znum | a 1 := (pred' a).to_znum | 1 b := (pred' b).to_znum_neg | (bit0 a) (bit0 b) := (sub' a b).bit0 | (bit0 a) (bit1 b) := (sub' a b).bitm1 | (bit1 a) (bit0 b) := (sub' a b).bit1 | (bit1 a) (bit1 b) := (sub' a b).bit0 /-- Converts a `znum` to `option pos_num`, where it is `some` if the `znum` was positive and `none` otherwise. -/ def of_znum' : znum β†’ option pos_num | (znum.pos p) := some p | _ := none /-- Converts a `znum` to a `pos_num`, mapping all out of range values to `1`. -/ def of_znum : znum β†’ pos_num | (znum.pos p) := p | _ := 1 /-- Subtraction of `pos_num`s, where if `a < b`, then `a - b = 1`. -/ protected def sub (a b : pos_num) : pos_num := match sub' a b with | (znum.pos p) := p | _ := 1 end instance : has_sub pos_num := ⟨pos_num.sub⟩ end pos_num namespace num /-- The predecessor of a `num` as an `option num`, where `ppred 0 = none` -/ def ppred : num β†’ option num | 0 := none | (pos p) := some p.pred' /-- The predecessor of a `num` as a `num`, where `pred 0 = 0`. -/ def pred : num β†’ num | 0 := 0 | (pos p) := p.pred' /-- Divides a `num` by `2` -/ def div2 : num β†’ num | 0 := 0 | 1 := 0 | (pos (pos_num.bit0 p)) := pos p | (pos (pos_num.bit1 p)) := pos p /-- Converts a `znum` to an `option num`, where `of_znum' p = none` if `p < 0`. -/ def of_znum' : znum β†’ option num | 0 := some 0 | (znum.pos p) := some (pos p) | (znum.neg p) := none /-- Converts a `znum` to an `option num`, where `of_znum p = 0` if `p < 0`. -/ def of_znum : znum β†’ num | (znum.pos p) := pos p | _ := 0 /-- Subtraction of two `num`s, producing a `znum`. -/ def sub' : num β†’ num β†’ znum | 0 0 := 0 | (pos a) 0 := znum.pos a | 0 (pos b) := znum.neg b | (pos a) (pos b) := a.sub' b /-- Subtraction of two `num`s, producing an `option num`. -/ def psub (a b : num) : option num := of_znum' (sub' a b) /-- Subtraction of two `num`s, where if `a < b`, `a - b = 0`. -/ protected def sub (a b : num) : num := of_znum (sub' a b) instance : has_sub num := ⟨num.sub⟩ end num namespace znum open pos_num /-- Addition of `znum`s. -/ protected def add : znum β†’ znum β†’ znum | 0 a := a | b 0 := b | (pos a) (pos b) := pos (a + b) | (pos a) (neg b) := sub' a b | (neg a) (pos b) := sub' b a | (neg a) (neg b) := neg (a + b) instance : has_add znum := ⟨znum.add⟩ /-- Multiplication of `znum`s. -/ protected def mul : znum β†’ znum β†’ znum | 0 a := 0 | b 0 := 0 | (pos a) (pos b) := pos (a * b) | (pos a) (neg b) := neg (a * b) | (neg a) (pos b) := neg (a * b) | (neg a) (neg b) := pos (a * b) instance : has_mul znum := ⟨znum.mul⟩ open ordering /-- Ordering on `znum`s. -/ def cmp : znum β†’ znum β†’ ordering | 0 0 := eq | (pos a) (pos b) := pos_num.cmp a b | (neg a) (neg b) := pos_num.cmp b a | (pos _) _ := gt | (neg _) _ := lt | _ (pos _) := lt | _ (neg _) := gt instance : has_lt znum := ⟨λa b, cmp a b = ordering.lt⟩ instance : has_le znum := ⟨λa b, Β¬ b < a⟩ instance decidable_lt : @decidable_rel znum (<) | a b := by dsimp [(<)]; apply_instance instance decidable_le : @decidable_rel znum (≀) | a b := by dsimp [(≀)]; apply_instance end znum namespace pos_num def divmod_aux (d : pos_num) (q r : num) : num Γ— num := match num.of_znum' (num.sub' r (num.pos d)) with | some r' := (num.bit1 q, r') | none := (num.bit0 q, r) end /-- `divmod x y = (y / x, y % x)`. -/ def divmod (d : pos_num) : pos_num β†’ num Γ— num | (bit0 n) := let (q, r₁) := divmod n in divmod_aux d q (num.bit0 r₁) | (bit1 n) := let (q, r₁) := divmod n in divmod_aux d q (num.bit1 r₁) | 1 := divmod_aux d 0 1 /-- Division of `pos_num`, -/ def div' (n d : pos_num) : num := (divmod d n).1 /-- Modulus of `pos_num`s. -/ def mod' (n d : pos_num) : num := (divmod d n).2 def sqrt_aux1 (b : pos_num) (r n : num) : num Γ— num := match num.of_znum' (n.sub' (r + num.pos b)) with | some n' := (r.div2 + num.pos b, n') | none := (r.div2, n) end def sqrt_aux : pos_num β†’ num β†’ num β†’ num | b@(bit0 b') r n := let (r', n') := sqrt_aux1 b r n in sqrt_aux b' r' n' | b@(bit1 b') r n := let (r', n') := sqrt_aux1 b r n in sqrt_aux b' r' n' | 1 r n := (sqrt_aux1 1 r n).1 /- def sqrt_aux : β„• β†’ β„• β†’ β„• β†’ β„• | b r n := if b0 : b = 0 then r else let b' := shiftr b 2 in have b' < b, from sqrt_aux_dec b0, match (n - (r + b : β„•) : β„€) with | (n' : β„•) := sqrt_aux b' (div2 r + b) n' | _ := sqrt_aux b' (div2 r) n end /-- `sqrt n` is the square root of a natural number `n`. If `n` is not a perfect square, it returns the largest `k:β„•` such that `k*k ≀ n`. -/ def sqrt (n : β„•) : β„• := match size n with | 0 := 0 | succ s := sqrt_aux (shiftl 1 (bit0 (div2 s))) 0 n end -/ end pos_num namespace num /-- Division of `num`s, where `x / 0 = 0`. -/ def div : num β†’ num β†’ num | 0 _ := 0 | _ 0 := 0 | (pos n) (pos d) := pos_num.div' n d /-- Modulus of `num`s. -/ def mod : num β†’ num β†’ num | 0 _ := 0 | n 0 := n | (pos n) (pos d) := pos_num.mod' n d instance : has_div num := ⟨num.div⟩ instance : has_mod num := ⟨num.mod⟩ def gcd_aux : nat β†’ num β†’ num β†’ num | 0 a b := b | (nat.succ n) 0 b := b | (nat.succ n) a b := gcd_aux n (b % a) a /-- Greatest Common Divisor (GCD) of two `num`s. -/ def gcd (a b : num) : num := if a ≀ b then gcd_aux (a.nat_size + b.nat_size) a b else gcd_aux (b.nat_size + a.nat_size) b a end num namespace znum /-- Division of `znum`, where `x / 0 = 0`. -/ def div : znum β†’ znum β†’ znum | 0 _ := 0 | _ 0 := 0 | (pos n) (pos d) := num.to_znum (pos_num.div' n d) | (pos n) (neg d) := num.to_znum_neg (pos_num.div' n d) | (neg n) (pos d) := neg (pos_num.pred' n / num.pos d).succ' | (neg n) (neg d) := pos (pos_num.pred' n / num.pos d).succ' /-- Modulus of `znum`s. -/ def mod : znum β†’ znum β†’ znum | 0 d := 0 | (pos n) d := num.to_znum (num.pos n % d.abs) | (neg n) d := d.abs.sub' (pos_num.pred' n % d.abs).succ instance : has_div znum := ⟨znum.div⟩ instance : has_mod znum := ⟨znum.mod⟩ /-- Greatest Common Divisor (GCD) of two `znum`s. -/ def gcd (a b : znum) : num := a.abs.gcd b.abs end znum section variables {Ξ± : Type*} [has_zero Ξ±] [has_one Ξ±] [has_add Ξ±] [has_neg Ξ±] /-- `cast_znum` casts a `znum` into any type which has `0`, `1`, `+` and `neg` -/ def cast_znum : znum β†’ Ξ± | 0 := 0 | (znum.pos p) := p | (znum.neg p) := -p -- see Note [coercion into rings] @[priority 900] instance znum_coe : has_coe_t znum Ξ± := ⟨cast_znum⟩ instance : has_repr znum := ⟨λ n, repr (n : β„€)⟩ end
d32f22165412fccce458bb320858a038fc0c2300
57fdc8de88f5ea3bfde4325e6ecd13f93a274ab5
/linear_algebra/basic.lean
2d814940f3271c8b5fa296bcd66c62355adb4ae0
[ "Apache-2.0" ]
permissive
louisanu/mathlib
11f56f2d40dc792bc05ee2f78ea37d73e98ecbfe
2bd5e2159d20a8f20d04fc4d382e65eea775ed39
refs/heads/master
1,617,706,993,439
1,523,163,654,000
1,523,163,654,000
124,519,997
0
0
Apache-2.0
1,520,588,283,000
1,520,588,283,000
null
UTF-8
Lean
false
false
37,233
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes HΓΆlzl Linear algebra -- classical This file is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light. We define the following concepts: * `lc Ξ± Ξ²`: linear combinations over `Ξ²` (`Ξ±` is the scalar ring) * `span s`: the submodule generated by `s` * `linear_independent s`: states that `s` are linear independent * `linear_independent.repr s b`: choose the linear combination representing `b` on the linear independent vectors `s`. `b` should be in `span b` (uses classical choice) * `is_basis s`: if `s` is a basis, i.e. linear independent and spans the entire space * `is_basis.repr s b`: like `linear_independent.repr` but as a `linear_map` * `is_basis.constr s g`: constructs a `linear_map` by extending `g` from the basis `s` -/ import algebra algebra.big_operators order.zorn data.finset data.finsupp noncomputable theory open classical set function lattice local attribute [instance] prop_decidable reserve infix `≃ₗ` : 50 universes u v w x y variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type y} {ΞΉ : Type x} @[simp] lemma set.diff_self {s : set Ξ±} : s \ s = βˆ… := set.ext $ by simp lemma zero_ne_one_or_forall_eq_0 (Ξ± : Type u) [ring Ξ±] : (0 : Ξ±) β‰  1 ∨ (βˆ€a:Ξ±, a = 0) := not_or_of_imp $ Ξ» h a, by simpa using congr_arg ((*) a) h.symm namespace finset lemma smul_sum [ring Ξ³] [module Ξ³ Ξ²] {s : finset Ξ±} {a : Ξ³} {f : Ξ± β†’ Ξ²} : a β€’ (s.sum f) = s.sum (Ξ»c, a β€’ f c) := (finset.sum_hom ((β€’) a) (@smul_zero Ξ³ Ξ² _ _ a) (assume _ _, smul_add)).symm end finset namespace finsupp lemma smul_sum [has_zero Ξ²] [ring Ξ³] [module Ξ³ Ξ΄] {v : Ξ± β†’β‚€ Ξ²} {c : Ξ³} {h : Ξ± β†’ Ξ² β†’ Ξ΄} : c β€’ (v.sum h) = v.sum (Ξ»a b, c β€’ h a b) := finset.smul_sum end finsupp /-- The type of linear coefficients, which are simply the finitely supported functions from the module `Ξ²` to the scalar ring `Ξ±`. -/ @[reducible] def lc (Ξ± : Type u) (Ξ² : Type v) [ring Ξ±] [module Ξ± Ξ²] : Type (max u v) := Ξ² β†’β‚€ Ξ± namespace lc variables [ring Ξ±] [module Ξ± Ξ²] instance : has_scalar Ξ± (lc Ξ± Ξ²) := finsupp.to_has_scalar instance : module Ξ± (lc Ξ± Ξ²) := finsupp.to_module lemma is_linear_map_sum [module Ξ± Ξ³] [module Ξ± Ξ΄] {f : Ξ² β†’ Ξ± β†’ Ξ³} {g : Ξ΄ β†’ lc Ξ± Ξ²} (hf : βˆ€b, is_linear_map (f b)) (hg : is_linear_map g) : is_linear_map (Ξ»d, (g d).sum f) := ⟨assume d₁ dβ‚‚, by simp [hg.add, finsupp.sum_add_index, (hf _).zero, (hf _).add], assume a d, by simp [hg.smul, finsupp.sum_smul_index, (hf _).zero, finsupp.smul_sum, ((hf _).smul _ _).symm]⟩ end lc namespace is_linear_map @[simp] lemma finsupp_sum [ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³] [has_zero Ξ΄] {f : Ξ² β†’ Ξ³} {t : ΞΉ β†’β‚€ Ξ΄} {g : ΞΉ β†’ Ξ΄ β†’ Ξ²} (hf : is_linear_map f) : f (t.sum g) = t.sum (Ξ»i d, f (g i d)) := hf.sum end is_linear_map structure linear_equiv {Ξ± : Type u} [ring Ξ±] (Ξ² : Type v) (Ξ³ : Type w) [module Ξ± Ξ²] [module Ξ± Ξ³] extends equiv Ξ² Ξ³ := (linear_fun : is_linear_map to_fun) infix ` ≃ₗ ` := linear_equiv namespace linear_equiv variables [ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³] [module Ξ± Ξ΄] include Ξ± lemma linear_inv (e : Ξ² ≃ₗ Ξ³) : is_linear_map e.inv_fun := e.linear_fun.inverse e.left_inv e.right_inv section variable (Ξ²) def refl : Ξ² ≃ₗ Ξ² := { linear_fun := is_linear_map.id, .. equiv.refl Ξ² } end def symm (e : Ξ² ≃ₗ Ξ³) : Ξ³ ≃ₗ Ξ² := { linear_fun := e.linear_inv, .. e.to_equiv.symm } def trans (e₁ : Ξ² ≃ₗ Ξ³) (eβ‚‚ : Ξ³ ≃ₗ Ξ΄) : Ξ² ≃ₗ Ξ΄ := { linear_fun := is_linear_map.comp eβ‚‚.linear_fun e₁.linear_fun, .. e₁.to_equiv.trans eβ‚‚.to_equiv } end linear_equiv section module variables [ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³] [module Ξ± Ξ΄] variables {a a' : Ξ±} {s t : set Ξ²} {b b' b₁ bβ‚‚ : Ξ²} include Ξ± /-- Linear span of a set of vectors -/ def span (s : set Ξ²) : set Ξ² := { x | βˆƒ(v : lc Ξ± Ξ²), (βˆ€xβˆ‰s, v x = 0) ∧ x = v.sum (Ξ»b a, a β€’ b) } instance is_submodule_span : is_submodule (span s) := { zero_ := ⟨0, by simp [finsupp.sum_zero_index]⟩, add_ := assume x y ⟨vx, hx, eqx⟩ ⟨vy, hy, eqy⟩, ⟨vx + vy, by simp [hx, hy, eqx, eqy, finsupp.sum_add_index, add_smul] {contextual := tt}⟩, smul := assume a b ⟨v, hv, veq⟩, ⟨a β€’ v, by simp [hv, veq, finsupp.sum_smul_index, finsupp.smul_sum, smul_smul] {contextual := tt}⟩ } lemma subset_span : s βŠ† span s := assume b (hb : b ∈ s), have βˆ€b'βˆ‰s, b β‰  b', by intros b' hb' ne; cc, ⟨finsupp.single b 1, by simp [finsupp.sum_single_index, this] {contextual := tt}⟩ lemma span_eq_of_is_submodule (hs : is_submodule s) : span s = s := have span s βŠ† s, from assume b ⟨v, hv, eq⟩, have βˆ€c, v c β€’ c ∈ s, from assume c, is_submodule.smul_ne_0 $ not_imp_comm.mp $ hv c, eq.symm β–Έ is_submodule.sum (by simp [this] {contextual := tt}), subset.antisymm this subset_span lemma span_mono (h : t βŠ† s) : span t βŠ† span s := assume b ⟨v, hv, eq⟩, ⟨v, assume b, hv b ∘ mt (@h b), eq⟩ lemma span_minimal (hs : is_submodule s) (h : t βŠ† s) : span t βŠ† s := calc span t βŠ† span s : span_mono h ... = s : span_eq_of_is_submodule hs lemma span_eq (hs : is_submodule s) (hts : t βŠ† s) (hst : s βŠ† span t) : span t = s := subset.antisymm (span_minimal hs hts) hst @[simp] lemma span_empty : span (βˆ… : set Ξ²) = {0} := span_eq is_submodule.single_zero (empty_subset _) (by simp [subset_def, is_submodule.zero]) lemma is_submodule_range_smul : is_submodule $ range (Ξ»a, a β€’ b) := is_submodule.range $ is_linear_map.map_smul_left is_linear_map.id lemma span_singleton : span {b} = range (Ξ»a, a β€’ b) := span_eq is_submodule_range_smul (assume b' hb', ⟨1, by simp * at *⟩) (assume b' ⟨a, eq⟩, eq β–Έ is_submodule.smul _ $ subset_span $ mem_singleton _) lemma span_union : span (s βˆͺ t) = {z | βˆƒx∈span s, βˆƒy∈span t, z = x + y } := span_eq is_submodule.add_submodule (union_subset (assume x hx, ⟨x, subset_span hx, 0, is_submodule.zero, by simp⟩) (assume y hy, ⟨0, is_submodule.zero, y, subset_span hy, by simp⟩)) (assume b ⟨x, hx, y, hy, eq⟩, eq.symm β–Έ is_submodule.add (span_mono (subset_union_left _ _) hx) (span_mono (subset_union_right _ _) hy)) lemma span_insert_eq_span (h : b ∈ span s) : span (insert b s) = span s := span_eq is_submodule_span (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_insert : span (insert b s) = {z | βˆƒa, βˆƒx∈span s, z = a β€’ b + x } := set.ext $ assume b', begin apply iff.intro; simp [insert_eq, span_union, span_singleton, set.set_eq_def, range, -add_comm], exact (assume y a eq_y x hx eq, ⟨a, x, hx, by simp [eq_y, eq]⟩), exact (assume a bβ‚‚ hbβ‚‚ eq, ⟨a β€’ b, ⟨a, rfl⟩, bβ‚‚, hbβ‚‚, eq⟩) end lemma mem_span_insert : b₁ ∈ span (insert b s) ↔ βˆƒa, b₁ + a β€’ b ∈ span s := begin simp [span_insert], constructor, exact assume ⟨a, b, hb, eq⟩, ⟨-a, by simp [eq, hb]⟩, exact assume ⟨a, hb⟩, ⟨-a, _, hb, by simp⟩ end @[simp] lemma span_span : span (span s) = span s := span_eq_of_is_submodule is_submodule_span @[simp] lemma span_image_of_linear_map {f : Ξ² β†’ Ξ³} (hf : is_linear_map f) : span (f '' s) = f '' span s := subset.antisymm (span_minimal (is_submodule.image hf) (image_subset _ subset_span)) (image_subset_iff.mpr $ span_minimal (is_submodule.preimage hf) (image_subset_iff.mp subset_span)) lemma linear_eq_on {f g : Ξ² β†’ Ξ³} (hf : is_linear_map f) (hg : is_linear_map g) (h : βˆ€x∈s, f x = g x) : βˆ€{x}, x ∈ span s β†’ f x = g x | _ ⟨l, hl, rfl⟩ := begin simp [hf.finsupp_sum, hg.finsupp_sum], apply finset.sum_congr rfl, assume b hb, have : b ∈ s, { by_contradiction, simp * at * }, simp [this, h, hf.smul, hg.smul] end /-- Linearly independent set of vectors -/ def linear_independent (s : set Ξ²) : Prop := βˆ€l : lc Ξ± Ξ², (βˆ€xβˆ‰s, l x = 0) β†’ l.sum (Ξ»v c, c β€’ v) = 0 β†’ l = 0 lemma linear_independent_empty : linear_independent (βˆ… : set Ξ²) := assume l hl eq, finsupp.ext $ by simp * at * lemma linear_independent.mono (hs : linear_independent s) (h : t βŠ† s) : linear_independent t := assume l hl eq, hs l (assume b, hl b ∘ mt (@h b)) eq lemma zero_not_mem_of_linear_independent (ne : 0 β‰  (1:Ξ±)) (hs : linear_independent s) : (0:Ξ²) βˆ‰ s := assume (h : 0 ∈ s), let l : lc Ξ± Ξ² := finsupp.single 0 1 in have l = 0, from hs l (by intro x; by_cases 0 = x; simp [l, finsupp.single_apply, *] at *) (by simp [finsupp.sum_single_index]), have l 0 = 1, from finsupp.single_eq_same, by rw [β€Ήl = 0β€Ί] at this; simp * at * lemma linear_independent_union {s t : set Ξ²} (hs : linear_independent s) (ht : linear_independent t) (hst : span s ∩ span t = {0}) : linear_independent (s βˆͺ t) := (zero_ne_one_or_forall_eq_0 Ξ±).elim (assume ne l hl eq0, let ls := l.filter $ Ξ»b, b ∈ s, lt := l.filter $ Ξ»b, b ∈ t in have hls : ↑ls.support βŠ† s, by simp [ls, subset_def], have hlt : ↑lt.support βŠ† t, by simp [ls, subset_def], have lt.sum (Ξ»b a, a β€’ b) ∈ span t, from is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ hlt hb, have l = ls + lt, from have βˆ€b, b ∈ s β†’ b βˆ‰ t, from assume b hbs hbt, have b ∈ span s ∩ span t, from ⟨subset_span hbs, subset_span hbt⟩, have b = 0, by rw [hst] at this; simp * at *, zero_not_mem_of_linear_independent ne hs $ this β–Έ hbs, have lt = l.filter (Ξ»b, b βˆ‰ s), from finsupp.ext $ assume b, by by_cases b ∈ t; by_cases b ∈ s; simp * at *, by rw [this]; exact finsupp.filter_pos_add_filter_neg.symm, have ls.sum (Ξ»b a, a β€’ b) + lt.sum (Ξ»b a, a β€’ b) = l.sum (Ξ»b a, a β€’ b), by rw [this, finsupp.sum_add_index]; simp [add_smul], have ls_eq_neg_lt : ls.sum (Ξ»b a, a β€’ b) = - lt.sum (Ξ»b a, a β€’ b), from eq_of_sub_eq_zero $ by simp [this, eq0], have ls_sum_eq : ls.sum (Ξ»b a, a β€’ b) = 0, from have - lt.sum (Ξ»b a, a β€’ b) ∈ span t, from is_submodule.neg $ is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ hlt hb, have ls.sum (Ξ»b a, a β€’ b) ∈ span s ∩ span t, from ⟨is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ hls hb, ls_eq_neg_lt.symm β–Έ this⟩, by rw [hst] at this; simp * at *, have ls = 0, from hs _ (finsupp.support_subset_iff.mp hls) ls_sum_eq, have lt_sum_eq : lt.sum (Ξ»b a, a β€’ b) = 0, from eq_of_neg_eq_neg $ by rw [←ls_eq_neg_lt, ls_sum_eq]; simp, have lt = 0, from ht _ (finsupp.support_subset_iff.mp hlt) lt_sum_eq, by simp [β€Ήl = ls + ltβ€Ί, β€Ήls = 0β€Ί, β€Ήlt = 0β€Ί]) (assume eq_0 l _ _, finsupp.ext $ assume b, eq_0 _) lemma linear_independent_Union_of_directed {s : set (set Ξ²)} (hs : βˆ€a∈s, βˆ€b∈s, βˆƒc∈s, a βˆͺ b βŠ† c) (h : βˆ€a∈s, linear_independent a) : linear_independent (⋃₀s) := assume l hl eq, have βˆ€f:finset Ξ², {x | x ∈ f} βŠ† ⋃₀ s β†’ f = βˆ… ∨ (βˆƒt∈s, {x | x ∈ f} βŠ† t), from assume f, finset.induction_on f (by simp) $ assume a f haf ih haf_s, let ⟨t, ht, hat⟩ := haf_s $ finset.mem_insert_self _ _ in have f = βˆ… ∨ βˆƒ (t : set Ξ²) (H : t ∈ s), {x : Ξ² | x ∈ f} βŠ† t, from ih $ assume x hx, haf_s $ finset.mem_insert_of_mem hx, or.inr $ this.elim (assume : f = βˆ…, ⟨t, ht, by simp [this, hat, subset_def]⟩) (assume ⟨t', ht', hft⟩, let ⟨t'', ht''s, ht''⟩ := hs t ht t' ht' in have a ∈ t'', from ht'' $ or.inl hat, have βˆ€x, x ∈ f β†’ x ∈ t'', from subset.trans (subset.trans hft $ subset_union_right _ _) ht'', ⟨t'', ht''s, by simp [subset_def, or_imp_distrib, *] {contextual := tt}⟩), have l.support = βˆ… ∨ (βˆƒt∈s, {x | x ∈ l.support} βŠ† t), from this _ $ by intros x hx; by_contradiction; simp * at *, this.elim (assume : l.support = βˆ…, by simp [finset.ext] at this; exact finsupp.ext this) (assume ⟨t, ht, hts⟩, have βˆ€x, l x β‰  0 β†’ x ∈ t, by simpa using hts, h t ht l (assume x, not_imp_comm.mp $ this x) eq) lemma linear_independent_bUnion_of_directed {ΞΉ : Type w} {i : set ΞΉ} {s : ΞΉ β†’ set Ξ²} (hs : βˆ€a∈i, βˆ€b∈i, βˆƒc∈i, s a βˆͺ s b βŠ† s c) (h : βˆ€a∈i, linear_independent (s a)) : linear_independent (⋃a∈i, s a) := have linear_independent (⋃₀ (s '' i)), from linear_independent_Union_of_directed (assume a ⟨j, hj, a_eq⟩ b ⟨l, hl, b_eq⟩, let ⟨k, hk, h⟩ := hs j hj l hl in ⟨s k, mem_image_of_mem _ hk, a_eq β–Έ b_eq β–Έ h⟩) (assume a ⟨j, hj, a_eq⟩, a_eq β–Έ h j hj), by rwa [sUnion_image] at this lemma linear_independent.unique (hs : linear_independent s) {l₁ lβ‚‚ : lc Ξ± Ξ²} (h₁ : βˆ€xβˆ‰s, l₁ x = 0) (hβ‚‚ : βˆ€xβˆ‰s, lβ‚‚ x = 0) (eq : l₁.sum (Ξ»v c, c β€’ v) = lβ‚‚.sum (Ξ»v c, c β€’ v)) : l₁ = lβ‚‚ := eq_of_sub_eq_zero $ show l₁ - lβ‚‚ = 0, from hs (l₁ - lβ‚‚) (by simp [h₁, hβ‚‚] {contextual:=tt}) (by simp [finsupp.sum_sub_index, eq, sub_smul, -sub_eq_add_neg, sub_self]) section repr variables (hs : linear_independent s) def linear_independent.repr (hs : linear_independent s) (b : Ξ²) : lc Ξ± Ξ² := if h : b ∈ span s then classical.some h else 0 lemma repr_not_span (h : b βˆ‰ span s) : hs.repr b = 0 := dif_neg h lemma repr_spec (h : b ∈ span s) : (βˆ€b'βˆ‰s, hs.repr b b' = 0) ∧ b = (hs.repr b).sum (Ξ»b a, a β€’ b) := have hs.repr b = classical.some h, from dif_pos h, by rw [this]; exact classical.some_spec h lemma repr_eq_zero (hb' : b' βˆ‰ s) : hs.repr b b' = 0 := by_cases (assume : b ∈ span s, (repr_spec hs this).left _ hb') (assume : b βˆ‰ span s, by rw [repr_not_span hs this]; refl) lemma repr_sum_eq (hb : b ∈ span s) : (hs.repr b).sum (Ξ»b a, a β€’ b) = b := (repr_spec hs hb).right.symm lemma repr_eq {l : lc Ξ± Ξ²} (hb : b ∈ span s) (h : βˆ€xβˆ‰s, l x = 0) (eq : l.sum (Ξ»v c, c β€’ v) = b) : hs.repr b = l := hs.unique (assume b, repr_eq_zero hs) h (by rw [repr_sum_eq hs hb, eq]) lemma repr_eq_single (hb : b ∈ s) : hs.repr b = finsupp.single b 1 := repr_eq hs (subset_span hb) (assume b' hb', finsupp.single_eq_of_ne $ show b β‰  b', from assume eq, by simp * at *) (by simp [finsupp.sum_single_index, add_smul]) @[simp] lemma repr_zero : hs.repr 0 = 0 := repr_eq hs is_submodule.zero (by simp) (by simp [finsupp.sum_zero_index]) lemma repr_support : ↑(hs.repr b).support βŠ† s := assume x hx, classical.by_contradiction $ assume hxs, by simp at hx; exact hx (repr_eq_zero hs hxs) @[simp] lemma repr_add (hb : b ∈ span s) (hb' : b' ∈ span s) : hs.repr (b + b') = hs.repr b + hs.repr b' := repr_eq hs (is_submodule.add hb hb') (by simp [repr_eq_zero] {contextual := tt}) (by simp [finsupp.sum_add_index, add_smul, repr_sum_eq hs, hb, hb']) @[simp] lemma repr_smul (hb : b ∈ span s) : hs.repr (a β€’ b) = a β€’ hs.repr b := repr_eq hs (is_submodule.smul _ hb) (by simp [repr_eq_zero] {contextual := tt}) (calc (a β€’ hs.repr b).sum (Ξ»b a, a β€’ b) = (hs.repr b).sum (Ξ»b a', a β€’ (a' β€’ b)) : by simp [finsupp.sum_smul_index, add_smul, smul_smul] ... = a β€’ (hs.repr b).sum (Ξ»b a', a' β€’ b) : finsupp.smul_sum.symm ... = a β€’ b : by rw [repr_sum_eq hs hb]) @[simp] lemma repr_neg : hs.repr (- b) = - hs.repr b := by_cases (assume hb : b ∈ span s, have hs.repr ((-1) β€’ b) = (-1) β€’ hs.repr b, from repr_smul hs hb, by simpa) (assume hb : b βˆ‰ span s, have -b βˆ‰ span s, from assume hb, have - - b ∈ span s, from is_submodule.neg hb, by simpa, by simp [repr_not_span, this, hb]) @[simp] lemma repr_sub (hb : b ∈ span s) (hb' : b' ∈ span s) : hs.repr (b - b') = hs.repr b - hs.repr b' := by simp [repr_add hs hb, repr_neg hs, is_submodule.neg hb'] @[simp] lemma repr_sum {ΞΉ : Type w} {f : finset ΞΉ} {b : ΞΉ β†’ Ξ²} : (βˆ€i∈f, b i ∈ span s) β†’ hs.repr (f.sum b) = f.sum (Ξ»i, hs.repr (b i)) := by apply f.induction_on; simp [or_imp_distrib, forall_and_distrib, repr_add hs, is_submodule.sum] {contextual := tt} @[simp] lemma repr_finsupp_sum {ΞΉ : Type w} {Ξ΄ : Type x} [has_zero Ξ΄] {f : ΞΉ β†’β‚€ Ξ΄} {b : ΞΉ β†’ Ξ΄ β†’ Ξ²} : (βˆ€i∈f.support, b i (f i) ∈ span s) β†’ hs.repr (f.sum b) = f.sum (Ξ»i d, hs.repr (b i d)) := repr_sum hs lemma repr_eq_repr_of_subset {ht : linear_independent t} (h : t βŠ† s) (hb : b ∈ span t) : ht.repr b = hs.repr b := eq.symm $ repr_eq hs (span_mono h hb) (assume x hx, repr_eq_zero _ $ assume hxt, hx $ h hxt) (repr_sum_eq ht hb) end repr section variables {f : Ξ² β†’ Ξ³} {l : lc Ξ± Ξ²} (hs : linear_independent (f '' s)) (hf : is_linear_map f) (hf_inj : βˆ€a∈s, βˆ€b∈s, f a = f b β†’ a = b) (hl : βˆ€xβˆ‰s, l x = 0) include hs hf hf_inj private lemma l_eq_0 (h : f (l.sum (Ξ»b a, a β€’ b)) = 0) : l = 0 := have l_imp_s : βˆ€{x}, l x β‰  0 β†’ x ∈ s, from assume x hx, classical.by_contradiction $ assume hnx, hx $ hl _ $ hnx, have βˆ€c, c βˆ‰ f '' s β†’ c βˆ‰ (l.map_domain f).support, from assume c, mt $ assume hb, have c ∈ l.support.image f, from finsupp.map_domain_support hb, have βˆƒb, l b β‰  0 ∧ f b = c, by simpa, let ⟨b, hb, c_eq⟩ := this in ⟨b, l_imp_s hb, c_eq⟩, have l.map_domain f = 0, from hs _ (by simpa) $ calc (l.map_domain f).sum (Ξ»b a, a β€’ b) = f (l.sum (Ξ»b a, a β€’ b)): by simp [finsupp.sum_map_domain_index, add_smul, hf.finsupp_sum, hf.smul] ... = 0 : h, calc l = l.map_domain id : by rw [finsupp.map_domain_id] ... = l.map_domain (@inv_fun_on _ ⟨0⟩ _ f s ∘ f) : finsupp.map_domain_congr $ assume b hb, (@inv_fun_on_eq' _ ⟨0⟩ _ _ _ _ hf_inj $ l_imp_s $ by simpa using hb).symm ... = 0 : by rw [finsupp.map_domain_comp, this, finsupp.map_domain_zero] lemma linear_independent.of_image : linear_independent s := assume l hl eq, l_eq_0 hs hf hf_inj hl $ by simp [eq, hf.zero] lemma linear_independent.eq_0_of_span : βˆ€a∈span s, f a = 0 β†’ a = 0 | _ ⟨l, hl, rfl⟩ eq_0 := by simp [l_eq_0 hs hf hf_inj hl eq_0, finsupp.sum_zero_index] end /-- A set of vectors is a basis if it is linearly independent and all vectors are in the span -/ def is_basis (s : set Ξ²) := linear_independent s ∧ (βˆ€x, x ∈ span s) section is_basis lemma is_basis.map_repr (hs : is_basis s) : is_linear_map hs.1.repr := ⟨assume b₁ bβ‚‚, repr_add hs.1 (hs.2 _) (hs.2 _), assume a b, repr_smul hs.1 (hs.2 _)⟩ def is_basis.constr (hs : is_basis s) (f : Ξ² β†’ Ξ³) (b : Ξ²) : Ξ³ := (hs.1.repr b).sum (Ξ»b a, a β€’ f b) lemma is_basis.map_constr (hs : is_basis s) {f : Ξ² β†’ Ξ³} : is_linear_map (hs.constr f) := lc.is_linear_map_sum (assume b, is_linear_map.map_smul_left is_linear_map.id) hs.map_repr lemma is_basis.eq_linear_map {f g : Ξ² β†’ Ξ³} (hf : is_linear_map f) (hg : is_linear_map g) (hs : is_basis s) (h : βˆ€b∈s, f b = g b) : f = g := funext $ assume b, linear_eq_on hf hg h (hs.2 b) lemma constr_congr {f g : Ξ² β†’ Ξ³} {b : Ξ²} (hs : is_basis s) (h : βˆ€b∈s, f b = g b) : hs.constr f = hs.constr g := funext $ assume b', finset.sum_congr rfl $ assume b hb, have b ∈ s, from repr_support hs.1 hb, by simp [h b this] lemma constr_basis {f : Ξ² β†’ Ξ³} {b : Ξ²} (hs : is_basis s) (hb : b ∈ s) : (hs.constr f : Ξ² β†’ Ξ³) b = f b := show (hs.1.repr b).sum (Ξ»b a, a β€’ f b) = f b, by simp [hs.1, hs.2, hb, repr_eq_single, finsupp.sum_single_index] lemma constr_eq {g : Ξ² β†’ Ξ³} {f : Ξ² β†’ Ξ³} (hs : is_basis s) (hf : is_linear_map f) (h : βˆ€x∈s, g x = f x) : hs.constr g = f := hs.eq_linear_map hs.map_constr hf $ assume b hb, h b hb β–Έ constr_basis hs hb lemma constr_zero (hs : is_basis s) : hs.constr (Ξ»b, (0 : Ξ³)) = (Ξ»b, 0) := constr_eq hs is_linear_map.map_zero $ by simp lemma constr_add {g f : Ξ² β†’ Ξ³} (hs : is_basis s) : hs.constr (Ξ»b, f b + g b) = (Ξ»b, hs.constr f b + hs.constr g b) := constr_eq hs (is_linear_map.map_add hs.map_constr hs.map_constr) $ by simp [constr_basis hs] {contextual := tt} lemma constr_sub {g f : Ξ² β†’ Ξ³} (hs : is_basis s) : hs.constr (Ξ»b, f b - g b) = (Ξ»b, hs.constr f b - hs.constr g b) := constr_eq hs (is_linear_map.map_sub hs.map_constr hs.map_constr) $ by simp [constr_basis hs] {contextual := tt} lemma constr_neg {f : Ξ² β†’ Ξ³} (hs : is_basis s) : hs.constr (Ξ»b, - f b) = (Ξ»b, - hs.constr f b) := constr_eq hs hs.map_constr.map_neg $ by simp [constr_basis hs] {contextual := tt} -- this only works on functions if `Ξ±` is a commutative ring lemma constr_smul {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [comm_ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³] {f : Ξ² β†’ Ξ³} {a : Ξ±} {s : set Ξ²} (hs : is_basis s) {b : Ξ²} : hs.constr (Ξ»b, a β€’ f b) = (Ξ»b, a β€’ (hs.constr f) b) := constr_eq hs hs.map_constr.map_smul_right $ by simp [constr_basis hs] {contextual := tt} lemma constr_mem_span (hs : is_basis s) {f : Ξ² β†’ Ξ³} : (hs.constr f : Ξ² β†’ Ξ³) b ∈ span (f '' s) := is_submodule.sum $ assume b' hb', have b' ∈ s, from repr_support hs.1 hb', is_submodule.smul _ $ subset_span $ mem_image_of_mem _ this lemma constr_im_eq_span (hs : is_basis s) {f : Ξ² β†’ Ξ³} : range (hs.constr f) = span (f '' s) := eq.symm $ span_eq (is_submodule.range hs.map_constr) (assume b' ⟨b, hb, eq⟩, ⟨b, eq β–Έ constr_basis hs hb⟩) (assume b' ⟨b, hb⟩, hb β–Έ constr_mem_span hs) def module_equiv_lc (hs : is_basis s) : Ξ² ≃ (s β†’β‚€ Ξ±) := { to_fun := assume b, (hs.1.repr b).subtype_domain _, inv_fun := assume v, v.sum $ Ξ»b a, a β€’ b.1, left_inv := assume b, calc ((hs.1.repr b).subtype_domain s).sum (Ξ»b a, a β€’ b.1) = (hs.1.repr b).sum (Ξ»b a, a β€’ b) : @finsupp.sum_subtype_domain_index Ξ² _ _ _ _ (Ξ»x, x ∈ s) _ _ _ _ (Ξ»b a, a β€’ b) (repr_support hs.1) ... = _ : repr_sum_eq _ $ hs.2 _, right_inv := assume v, finsupp.ext $ assume ⟨b, hb⟩, have v.sum (Ξ»b' a, hs.1.repr (a β€’ b'.val) b) = v ⟨b, hb⟩, from calc v.sum (Ξ»b' a, hs.1.repr (a β€’ b'.val) b) = v.sum (Ξ»b' a, a * (finsupp.single b'.val 1 : lc Ξ± Ξ²) b) : finset.sum_congr rfl $ assume ⟨b', hb'⟩ h', by dsimp; rw [repr_smul hs.1 (hs.2 _), repr_eq_single _ hb']; refl ... = ({⟨b, hb⟩} : finset s).sum (Ξ»b', v b' * (finsupp.single b'.val 1 : lc Ξ± Ξ²) b) : finset.sum_bij_ne_zero (Ξ»x hx x0, x) (assume ⟨x, hx⟩, by by_cases x = b; simp [*]) (by simp) (by simp) (assume ⟨x, hx⟩, by simp; intro e; subst x; exact assume h, ⟨b, hb, assume h', by simp * at *, h, rfl⟩) (by simp) ... = v ⟨b, hb⟩ : by simp, begin dsimp, rw [repr_finsupp_sum, finsupp.sum_apply], { exact this }, { simp [hs.2] } end } def equiv_of_is_basis {s : set Ξ²} {t : set Ξ³} {f : Ξ² β†’ Ξ³} {g : Ξ³ β†’ Ξ²} (hs : is_basis s) (ht : is_basis t) (hf : βˆ€b∈s, f b ∈ t) (hg : βˆ€c∈t, g c ∈ s) (hgf : βˆ€b∈s, g (f b) = b) (hfg : βˆ€c∈t, f (g c) = c) : Ξ² ≃ₗ Ξ³ := { to_fun := hs.constr f, inv_fun := ht.constr g, left_inv := assume b, congr_fun (hs.eq_linear_map (ht.map_constr.comp hs.map_constr) is_linear_map.id $ by simp [constr_basis, hs, ht, hf, hgf, (∘)] {contextual := tt}) b, right_inv := assume c, congr_fun (ht.eq_linear_map (hs.map_constr.comp ht.map_constr) is_linear_map.id $ by simp [constr_basis, hs, ht, hg, hfg, (∘)] {contextual := tt}) c, linear_fun := hs.map_constr } end is_basis lemma linear_independent.inj_span_iff_inj {s : set Ξ²} {f : Ξ² β†’ Ξ³} (hf : is_linear_map f) (hfs : linear_independent (f '' s)) : (βˆ€a∈span s, βˆ€b∈span s, f a = f b β†’ a = b) ↔ (βˆ€a∈s, βˆ€b∈s, f a = f b β†’ a = b) := iff.intro (assume h a ha b hb eq, h a (subset_span ha) b (subset_span hb) eq) (assume h a ha b hb eq, eq_of_sub_eq_zero $ hfs.eq_0_of_span hf h _ (is_submodule.sub ha hb) (by simp [eq, hf.add, hf.neg])) -- TODO: clean up proof / alternative proof lemma linear_independent.image {s : set Ξ²} {f : Ξ² β†’ Ξ³} (hf : is_linear_map f) (hs : linear_independent s) (hf_inj : βˆ€a∈span s, βˆ€b∈span s, f a = f b β†’ a = b) : linear_independent (f '' s) := let g := @inv_fun_on _ ⟨0⟩ _ f (span s) in have hg : βˆ€x∈span s, g (f x) = x, from assume x, @inv_fun_on_eq' _ ⟨0⟩ _ _ _ _ hf_inj, assume l hl eq, have l_g : βˆ€b∈(l.map_domain g).support, b ∈ s, from assume b hb, have b ∈ l.support.image g, from finsupp.map_domain_support hb, have βˆƒc, l c β‰  0 ∧ g c = b, by simpa, let ⟨c, hc, b_eq⟩ := this in have c ∈ f '' s, by by_contradiction h; simp * at *, let ⟨b', hb', c_eq⟩ := this in have b' = b, from b_eq β–Έ c_eq β–Έ (hg _ $ subset_span hb').symm, this β–Έ hb', have l_f_g : l.map_domain (f ∘ g) = l.map_domain id, from finsupp.map_domain_congr $ assume c hc, have c ∈ f '' s, by by_contradiction h; simp * at *, let ⟨b, hb, c_eq⟩ := this in by simp [c_eq.symm, (∘), hg, subset_span hb], have l.map_domain g = 0, from have l_g_s : (l.map_domain g).sum (Ξ»b a, a β€’ b) ∈ span s, from is_submodule.sum $ assume b hb, is_submodule.smul _ $ subset_span $ l_g b hb, have f_sum : f ((l.map_domain g).sum (Ξ»b a, a β€’ b)) = 0, from calc f ((l.map_domain g).sum (Ξ»b a, a β€’ b)) = ((l.map_domain g).map_domain f).sum (Ξ»b a, a β€’ b) : by simp [finsupp.sum_map_domain_index, add_smul, hf.finsupp_sum, hf.smul] ... = 0 : by rw [←finsupp.map_domain_comp, l_f_g, finsupp.map_domain_id, eq], have βˆ€bβˆ‰s, (l.map_domain g) b = 0, from assume b hb, classical.by_contradiction $ assume hnb, hb $ l_g b $ by simp *, hs _ this $ hf_inj _ l_g_s _ is_submodule.zero (by simpa [hf.zero] using f_sum), calc l = (l.map_domain g).map_domain f : by rw [←finsupp.map_domain_comp, l_f_g, finsupp.map_domain_id] ... = 0 : by rw [this, finsupp.map_domain_zero] lemma linear_map.linear_independent_image_iff {s : set Ξ²} {f : Ξ² β†’ Ξ³} (hf : is_linear_map f) (hf_inj : βˆ€a∈span s, βˆ€b∈span s, f a = f b β†’ a = b) : linear_independent (f '' s) ↔ linear_independent s := iff.intro (assume h, h.of_image hf $ assume x hx y hy, hf_inj x (subset_span hx) y (subset_span hy)) (assume h, h.image hf hf_inj) end module section vector_space variables [field Ξ±] [vector_space Ξ± Ξ²] [vector_space Ξ± Ξ³] {s t : set Ξ²} {b b₁ bβ‚‚ : Ξ²} include Ξ± local attribute [instance] is_submodule_span /- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class (instead of a data containing type classs) -/ lemma mem_span_insert_exchange : b₁ ∈ span (insert bβ‚‚ s) β†’ b₁ βˆ‰ span s β†’ bβ‚‚ ∈ span (insert b₁ s) := begin simp [span_insert], exact assume a b₃ hb₃ b₁_eq hb₁, have a β‰  0, from assume a0, by simp * at *, ⟨1/a, (- 1/a) β€’ b₃, is_submodule.smul _ hb₃, by simp [b₁_eq, smul_add, smul_smul, mul_inv_cancel, this, neg_div]⟩ end lemma linear_independent_iff_not_mem_span : linear_independent s ↔ (βˆ€b∈s, b βˆ‰ span (s \ {b})) := iff.intro (assume (hs : linear_independent s) b hb ⟨l, hl, b_eq⟩, let l' := l - finsupp.single b 1 in have βˆ€b', b' βˆ‰ s β†’ l' b' = 0, from assume b' hb', have ne: b β‰  b', from assume h, hb' $ h β–Έ hb, have b' βˆ‰ s \ {b}, from assume ⟨h₁, hβ‚‚βŸ©, hb' h₁, by simp [ne, hl b' this], have l' = 0, from hs l' this $ by simp [l', finsupp.sum_add_index, finsupp.sum_neg_index, add_smul, b_eq.symm, finsupp.sum_single_index], have - l' b = 1, from have b βˆ‰ s \ {b}, by simp, by simp [hl _ this], by rw [β€Ήl' = 0β€Ί] at this; simp at this; assumption) (assume hs l hl eq, finsupp.ext $ assume b, classical.by_contradiction $ assume h : l b β‰  0, let a := -1 / l b in hs b (show b ∈ s, from classical.by_contradiction $ assume hnb, h $ hl b hnb) ⟨a β€’ l + finsupp.single b 1, assume b', by_cases (assume : b' = b, by simp [this, h, neg_div, a]) (assume : b' β‰  b, by simp [finsupp.sub_apply, hl, this, this.symm] {contextual:=tt}), have l.sum (Ξ»b a', (a * a') β€’ b) = a β€’ l.sum (Ξ»b a, a β€’ b), by simp [finsupp.smul_sum, smul_smul], by simp [-sub_eq_add_neg, add_smul, finsupp.sum_add_index, finsupp.sum_single_index, finsupp.sum_smul_index, this, eq]⟩) lemma linear_independent_singleton {b : Ξ²} (hb : b β‰  0) : linear_independent ({b} : set Ξ²) := linear_independent_iff_not_mem_span.mpr $ by simp [hb] {contextual := tt} lemma linear_independent.insert (hs : linear_independent s) (hb : b βˆ‰ span s) : linear_independent (insert b s) := assume l hl eq, by_cases (assume : l b = 0, hs l (assume x hx, by_cases (assume h : x = b, h.symm β–Έ this) (assume h', hl _ $ by simp [not_or_distrib, hx, h'])) eq) (assume lb_ne_zero : l b β‰  0, have (1 / l b) β€’ (- (- l b β€’ b)) ∈ span s, from is_submodule.smul _ $ is_submodule.neg ⟨l - finsupp.single b (l b), assume x hx, by_cases (assume : b = x, by simp [this.symm]) (assume ne : b β‰  x, have x βˆ‰ insert b s, by simp [not_or_distrib, hx, ne.symm], by simp [hl x this, ne] {contextual := tt}), by simp [finsupp.sum_sub_index, finsupp.sum_single_index, -sub_eq_add_neg, sub_smul, eq]; simp⟩, have (1 / l b) β€’ (- (- l b β€’ b)) = b, by simp [smul_smul, mul_comm, mul_inv_cancel lb_ne_zero], by simp * at *) lemma exists_linear_independent (hs : linear_independent s) (hst : s βŠ† t) : βˆƒbβŠ†t, s βŠ† b ∧ t βŠ† span b ∧ linear_independent b := let C := { b : set Ξ² // s βŠ† b ∧ b βŠ† t ∧ linear_independent b }, s' : C := ⟨s, le_refl s, hst, hs⟩ in have βˆ€c, zorn.chain (Ξ»a b:C, a.val βŠ† b.val) c β†’ c β‰  βˆ… β†’ βˆƒ(m : C), βˆ€a:C, a ∈ c β†’ a.val βŠ† m.val, from assume c hc ne, let ⟨a, ha⟩ := exists_mem_of_ne_empty ne in ⟨⟨(⋃a ∈ c, (a : C).val), subset.trans a.property.1 $ subset_bUnion_of_mem ha, bUnion_subset $ assume c hc, c.property.right.left, linear_independent_bUnion_of_directed (assume a ha b hb, by_cases (assume h : a = b, ⟨a, ha, h β–Έ le_of_eq (@sup_idem (set _) _ a.val)⟩) (assume h : a β‰  b, (hc a ha b hb h).elim (assume h, ⟨b, hb, union_subset h (subset.refl _)⟩) (assume h, ⟨a, ha, union_subset (subset.refl _) h⟩))) (assume a ha, a.property.2.2)⟩, assume a ha, subset_bUnion_of_mem ha⟩, have βˆƒm:C, βˆ€a:C, m.val βŠ† a.val β†’ a.val βŠ† m.val, from zorn.zorn (assume c hc, by_cases (assume : c = βˆ…, ⟨s', assume a, this.symm β–Έ false.elim⟩) (this c hc)) (assume a b c, subset.trans), let ⟨⟨m, hsm, hmt, hml⟩, hm⟩ := this in have t βŠ† span m, from classical.by_contradiction $ assume : Β¬ t βŠ† span m, let ⟨b, hb⟩ := classical.not_forall.mp this, ⟨hbt, hbm⟩ := not_imp.mp hb in have insert b m βŠ† m, from hm ⟨_, subset.trans hsm $ subset_insert _ _, by simp [set.insert_subset, hmt, hbt], hml.insert hbm⟩ (subset_insert _ _), have b ∈ span m, from subset_span $ this $ mem_insert _ _, hbm this, ⟨m, hmt, hsm, this, hml⟩ lemma exists_subset_is_basis (hs : linear_independent s) : βˆƒb, s βŠ† b ∧ is_basis b := let ⟨b, hbβ‚€, hb₁, hbβ‚‚, hbβ‚ƒβŸ© := exists_linear_independent hs (@subset_univ _ _) in ⟨b, hb₁, hb₃, assume x, hbβ‚‚ trivial⟩ variable (Ξ²) lemma exists_is_basis : βˆƒb : set Ξ², is_basis b := let ⟨b, _, hb⟩ := exists_subset_is_basis linear_independent_empty in ⟨b, hb⟩ variable {Ξ²} lemma eq_of_linear_independent_of_span (hs : linear_independent s) (h : t βŠ† s) (hst : s βŠ† span t) : s = t := suffices s βŠ† t, from subset.antisymm this h, assume b hb, have (hs.mono h).repr b = finsupp.single b 1, from calc (hs.mono h).repr b = hs.repr b : repr_eq_repr_of_subset hs h $ hst hb ... = finsupp.single b 1 : repr_eq_single hs hb, have b ∈ (↑((hs.mono h).repr b).support : set Ξ²), by simp [this], repr_support _ this lemma exists_of_linear_independent_of_finite_span {t : finset Ξ²} (hs : linear_independent s) (hst : s βŠ† span ↑t) : βˆƒt':finset Ξ², ↑t' βŠ† s βˆͺ ↑t ∧ s βŠ† ↑t' ∧ t'.card = t.card := have βˆ€t, βˆ€(s' : finset Ξ²), ↑s' βŠ† s β†’ s ∩ ↑t = βˆ… β†’ s βŠ† span ↑(s' βˆͺ t) β†’ βˆƒt':finset Ξ², ↑t' βŠ† s βˆͺ ↑t ∧ s βŠ† ↑t' ∧ t'.card = (s' βˆͺ t).card := assume t, finset.induction_on t (assume s' hs' _ hss', have s = ↑s', from eq_of_linear_independent_of_span hs hs' $ by simpa using hss', ⟨s', by simp [this]⟩) (assume b₁ t hb₁t ih s' hs' hst hss', have hb₁s : b₁ βˆ‰ s, from assume h, have b₁ ∈ s ∩ ↑(insert b₁ t), from ⟨h, finset.mem_insert_self _ _⟩, by rwa [hst] at this, have hb₁s' : b₁ βˆ‰ s', from assume h, hb₁s $ hs' h, have hst : s ∩ ↑t = βˆ…, from eq_empty_of_subset_empty $ subset.trans (by simp [inter_subset_inter, subset.refl]) (le_of_eq hst), by_cases (assume : s βŠ† span ↑(s' βˆͺ t), let ⟨u, hust, hsu, eq⟩ := ih _ hs' hst this in have hb₁u : b₁ βˆ‰ u, from assume h, (hust h).elim hb₁s hb₁t, ⟨insert b₁ u, by simp [set.insert_subset_insert hust], subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩) (assume : Β¬ s βŠ† span ↑(s' βˆͺ t), let ⟨bβ‚‚, hbβ‚‚s, hbβ‚‚t⟩ := set.not_subset.mp this in have hbβ‚‚t' : bβ‚‚ βˆ‰ s' βˆͺ t, from assume h, hbβ‚‚t $ subset_span h, have s βŠ† span ↑(insert bβ‚‚ s' βˆͺ t), from assume b₃ hb₃, have ↑(s' βˆͺ insert b₁ t) βŠ† insert b₁ (insert bβ‚‚ ↑(s' βˆͺ t) : set Ξ²), by simp [insert_eq, union_subset_union, subset.refl, subset_union_right], have hb₃ : b₃ ∈ span (insert b₁ (insert bβ‚‚ ↑(s' βˆͺ t) : set Ξ²)), from span_mono this (hss' hb₃), have s βŠ† span (insert b₁ ↑(s' βˆͺ t)), by simpa [insert_eq] using hss', have hb₁ : b₁ ∈ span (insert bβ‚‚ ↑(s' βˆͺ t)), from mem_span_insert_exchange (this hbβ‚‚s) hbβ‚‚t, by rw [span_insert_eq_span hb₁] at hb₃; simpa using hb₃, let ⟨u, hust, hsu, eq⟩ := ih _ (by simp [set.insert_subset, hbβ‚‚s, hs']) hst this in ⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]), hsu, by rw [finset.union_comm] at hbβ‚‚t'; simp [eq, hbβ‚‚t', hb₁t, hb₁s']⟩)), have eq : t.filter (Ξ»x, x ∈ s) βˆͺ t.filter (Ξ»x, x βˆ‰ s) = t, from finset.ext.mpr $ assume x, by by_cases x ∈ s; simp *, let ⟨u, h₁, hβ‚‚, h⟩ := this (t.filter (Ξ»x, x βˆ‰ s)) (t.filter (Ξ»x, x ∈ s)) (by simp [set.subset_def]) (by simp [set.set_eq_def] {contextual := tt}) (by rwa [eq]) in ⟨u, subset.trans h₁ (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}), hβ‚‚, by rwa [eq] at h⟩ lemma exists_finite_card_le_of_finite_of_linear_independent_of_span (ht : finite t) (hs : linear_independent s) (hst : s βŠ† span t) : βˆƒh : finite s, h.to_finset.card ≀ ht.to_finset.card := have s βŠ† span ↑(ht.to_finset), by simp; assumption, let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in have finite s, from finite_subset u.finite_to_set hsu, ⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subseteq_coe.mp $ by simp [hsu])⟩ lemma exists_left_inverse_linear_map_of_injective {f : Ξ² β†’ Ξ³} (hf : is_linear_map f) (hf_inj : injective f) : βˆƒg:Ξ³ β†’ Ξ², is_linear_map g ∧ g ∘ f = id := let ⟨bΞ², hbβ⟩ := exists_is_basis Ξ² in have linear_independent (f '' bΞ²), from hbΞ².1.image hf $ assume b₁ _ bβ‚‚ _ eq, hf_inj eq, let ⟨bΞ³, hbγ₁, hbΞ³β‚‚βŸ© := exists_subset_is_basis this in have βˆ€b∈bΞ², (hbΞ³β‚‚.constr (@inv_fun _ ⟨0⟩ _ f) : Ξ³ β†’ Ξ²) (f b) = b, begin assume b hb, rw [constr_basis], { exact @inv_fun_on_eq' Ξ² ⟨0⟩ Ξ³ f univ b (assume b₁ _ bβ‚‚ _ eq, hf_inj eq) trivial }, { exact hbγ₁ (mem_image_of_mem _ hb) } end, ⟨hbΞ³β‚‚.constr $ @inv_fun _ ⟨0⟩ _ f, hbΞ³β‚‚.map_constr, hbΞ².eq_linear_map (hbΞ³β‚‚.map_constr.comp hf) is_linear_map.id this⟩ lemma exists_right_inverse_linear_map_of_surjective {f : Ξ² β†’ Ξ³} (hf : is_linear_map f) (hf_surj : surjective f) : βˆƒg:Ξ³ β†’ Ξ², is_linear_map g ∧ f ∘ g = id := let g := @inv_fun _ ⟨0⟩ _ f in have ri_gf : right_inverse g f, from @right_inverse_inv_fun _ ⟨0⟩ _ _ hf_surj, have injective g, from injective_of_left_inverse ri_gf, let ⟨bΞ³, hbγ⟩ := exists_is_basis Ξ³ in have βˆ€c∈bΞ³, f ((hbΞ³.constr g : Ξ³ β†’ Ξ²) c) = c, from assume c hc, by rw [constr_basis hbΞ³ hc, ri_gf], ⟨hbΞ³.constr g, hbΞ³.map_constr, hbΞ³.eq_linear_map (hf.comp hbΞ³.map_constr) is_linear_map.id this⟩ end vector_space
6309cfcb09830cedd3130dd68be0e36f1bd339aa
63abd62053d479eae5abf4951554e1064a4c45b4
/src/order/order_dual.lean
66b43e9a72b55637571f5db0866f87eceb6155a4
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
2,286
lean
/- Copyright (c) 2020 Johan Commelin, Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Damiano Testa -/ import order.basic import data.equiv.basic /-! # Initial lemmas to work with the `order_dual` ## Definitions `to_dual` and `of_dual` the order reversing identity maps, bundled as equivalences. ## Basic Lemmas to convert between an order and its dual This file is similar to algebra/group/type_tags.lean -/ open function universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {r : Ξ± β†’ Ξ± β†’ Prop} namespace order_dual /-- `to_dual` is the identity function to the `order_dual` of a linear order. -/ def to_dual : Ξ± ≃ order_dual Ξ± := ⟨id, id, Ξ» h, rfl, Ξ» h, rfl⟩ /-- `of_dual` is the identity function from the `order_dual` of a linear order. -/ def of_dual : order_dual Ξ± ≃ Ξ± := to_dual.symm @[simp] lemma to_dual_symm_eq : (@to_dual Ξ±).symm = of_dual := rfl @[simp] lemma of_dual_symm_eq : (@of_dual Ξ±).symm = to_dual := rfl @[simp] lemma to_dual_of_dual (a : order_dual Ξ±) : to_dual (of_dual a) = a := rfl @[simp] lemma of_dual_to_dual (a : Ξ±) : of_dual (to_dual a) = a := rfl @[simp] lemma to_dual_inj {a b : Ξ±} : to_dual a = to_dual b ↔ a = b := iff.rfl @[simp] lemma to_dual_le_to_dual [has_le Ξ±] {a b : Ξ±} : to_dual a ≀ to_dual b ↔ b ≀ a := iff.rfl @[simp] lemma to_dual_lt_to_dual [has_lt Ξ±] {a b : Ξ±} : to_dual a < to_dual b ↔ b < a := iff.rfl @[simp] lemma of_dual_inj {a b : order_dual Ξ±} : of_dual a = of_dual b ↔ a = b := iff.rfl @[simp] lemma of_dual_le_of_dual [has_le Ξ±] {a b : order_dual Ξ±} : of_dual a ≀ of_dual b ↔ b ≀ a := iff.rfl @[simp] lemma of_dual_lt_of_dual [has_lt Ξ±] {a b : order_dual Ξ±} : of_dual a < of_dual b ↔ b < a := iff.rfl lemma le_to_dual [has_le Ξ±] {a : order_dual Ξ±} {b : Ξ±} : a ≀ to_dual b ↔ b ≀ of_dual a := iff.rfl lemma lt_to_dual [has_lt Ξ±] {a : order_dual Ξ±} {b : Ξ±} : a < to_dual b ↔ b < of_dual a := iff.rfl lemma to_dual_le [has_le Ξ±] {a : Ξ±} {b : order_dual Ξ±} : to_dual a ≀ b ↔ of_dual b ≀ a := iff.rfl lemma to_dual_lt [has_lt Ξ±] {a : Ξ±} {b : order_dual Ξ±} : to_dual a < b ↔ of_dual b < a := iff.rfl end order_dual
0d59e401053641dcb6be61b56c06711bf6cbce4c
1963cb8789bf8cf695607cc2d4106d7fdd8fb9f2
/homework3/solutions3.lean
d6bcf877f0cbe8f2c8c6e02a148dc14e1ecbd9ff
[]
no_license
mtegene/homework-mtegene-master
ed43dd0b8cbcea936b6eec82c614842eca02a406
5f9b3623a46d2d3b98c54d8cbc82962d3fef49f5
refs/heads/master
1,610,605,612,607
1,488,481,866,000
1,488,481,866,000
81,875,687
0
0
null
null
null
null
UTF-8
Lean
false
false
10,119
lean
namespace nat /- Part 1. Summation. -/ -- define summation up to (but not including) n def sum_upto (n : β„•) (f : β„• β†’ β„•) : β„• := nat.rec_on n 0 (Ξ» n recval, recval + f n) -- the computation rules theorem sum_upto_zero (f : β„• β†’ β„•) : sum_upto 0 f = 0 := rfl theorem sum_upto_succ (f : β„• β†’ β„•) (n : β„•) : sum_upto (succ n) f = sum_upto n f + f n := rfl -- fill in the next two proofs theorem sum_upto_mul (n m : β„•) (f : β„• β†’ β„•) : sum_upto n (Ξ» x, m * f x) = m * (sum_upto n f) := nat.induction_on n rfl (take n, assume ih: sum_upto n (Ξ» x, m * f x) = m * (sum_upto n f), show sum_upto (succ n) (Ξ» x, m * f x) = m * (sum_upto (succ n) f), from calc sum_upto (succ n) (Ξ» x, m * f x) = sum_upto n (Ξ» x, m * f x) + (Ξ» x, m * f x) n : by rw sum_upto_succ ... = m * (sum_upto n f) + (Ξ» x, m * f x) n : by rw ih ... = m * (sum_upto n f) + m * ((Ξ» x, f x ) n) : rfl ... = m * (sum_upto n f + (Ξ» x, f x) n) : by rw mul_add ... = m * (sum_upto n f + f n) : rfl ... = m * (sum_upto (succ n) f) : by rw sum_upto_succ) theorem sum_upto_id (n : β„•) : 2 * sum_upto (succ n) id = n * (n + 1) := nat.induction_on n rfl (take n, assume ih: 2 * sum_upto (succ n) id = n * (n + 1), show 2 * sum_upto (succ (succ n)) id = (succ n) * ((succ n) + 1), from calc 2 * sum_upto (succ (succ n)) id = 2 * (sum_upto (succ n) id + id (succ n)) : by rw sum_upto_succ ... = 2 * (sum_upto (succ n) id) + 2 * id(succ n) : by rw mul_add ... = n * (n + 1) + 2 * id (succ n) : by rw ih ... = n * (n + 1) + 2 * (succ n) : rfl ... = n * (succ n) + 2 * (succ n) : rfl ... = (succ n) * n + 2 * (succ n) : by rw mul_comm ... = (succ n) * n + (succ n) * 2 : by simp --by rw mul_comm ... = (succ n) * (n + 2) : by rw mul_add ... = (succ n) * (n + 1 + 1) : rfl ... = (succ n) * ((succ n) + 1) : rfl) /- Part 2. Exponentiation on nat. -/ -- the definition def pow : β„• β†’ β„• β†’ β„• | m 0 := 1 | m (succ n) := m * pow m n -- declare the notation infix ^ := pow -- the computation rules theorem pow_zero (m : β„•) : m ^ 0 = 1 := rfl theorem pow_succ (m n : β„•) : m ^ (succ n) = m * m ^ n := rfl -- fill in the next four proofs theorem zero_pow_succ (n : β„•) : 0^(succ n) = 0 := nat.induction_on n rfl ( take n, assume ih, show 0^(succ (succ n)) = 0, from calc 0^(succ (succ n)) = 0 * 0^(succ n) : by rw pow_succ ... = 0 * 0 : by rw ih ... = 0 : by rw mul_zero) theorem pow_add (m n k : β„•) : m ^ (n + k) = m ^ n * m ^ k := nat.induction_on n (calc m ^ (0 + k) = m ^ k : by rw zero_add ... = 1 * m ^ k : by rw one_mul ... = m ^ 0 * m ^ k : rfl) (take n, assume ih, show m ^ ((succ n) + k) = m ^ (succ n) * m ^ k, from calc m ^ ((succ n) + k) = m ^ ((n + 1) + k) : rfl ... = m ^ (k + (n + 1)) : by rw add_comm ... = m ^ ((k + n) + 1) : by rw add_assoc ... = m ^ ((n + k) + 1) : by simp ... = m ^ (succ (n + k)) : rfl ... = m * m ^ (n + k) : by rw pow_succ ... = m * (m ^ n * m ^ k) : by rw ih ... = (m * m ^ n) * m ^ k : by rw mul_assoc ... = m ^ (succ n) * m ^ k : by rw pow_succ) theorem pow_mul (m n k : β„•) : m ^ (n * k) = (m ^ n) ^ k := nat.induction_on k (calc m ^ (n * 0) = m ^ (0) : by rw mul_zero ... = 1 : by rw pow_zero ... = (m ^ n) ^ 0 : by rw pow_zero) (take k, assume ih, show m ^ (n * (succ k)) = (m ^ n) ^ (succ k), from calc m ^ (n * (succ k)) = m ^ (n * (k + 1)) : rfl ... = m ^ (n * k + n * 1) : by rw mul_add ... = m ^ (n * k) * m ^ (n * 1) : sorry ... = (m ^ n) ^ k * m ^ (n * 1) : by rw ih ... = (m ^ n) ^ k * (m ^ n) : by rw mul_one ... = (m ^ n) * (m ^ n) ^ k : by rw mul_comm ... = (m ^ n) ^ (succ k) : by rw pow_succ) check zero_lt_one check @mul_pos theorem pow_pos {m : β„•} (h : m > 0) {n : β„•} : m^n > 0 := nat.induction_on n ( have H1: m^0 = 1, from rfl, show m^0 > 0, from zero_lt_one) ( take n, assume ih, have H1: m^ (succ n) = m * m^n, from rfl, show m^(succ n) > 0, from mul_pos h ih) /- The last one is pow_le, below. It is not easy, so just give it your best shot. The next few examples might be helpful. -/ -- By the way, this is a useful trick. I will explain it in class. example : 3 < 7 := dec_trivial check @mul_le_mul check @mul_le_mul_left check @nat.zero_le example (m : β„•) (h : m > 0) : m β‰₯ 1 := succ_le_of_lt h -- actually, on β„•, x < y is defined to mean succ x < y example (m : β„•) (h : m > 0) : m β‰₯ 1 := h example (m n : β„•) (h : m ≀ n) : βˆƒ k, m + k = n := le.dest h lemma le_mul_self (m n : β„•) (h : m = 0 ∨ n > 0) : m ≀ m * n := or.elim h (suppose m = 0, begin simp [this] end) (suppose n > 0, have m * 1 ≀ m * n, from mul_le_mul_left _ this, begin rw mul_one at this, exact this end) check @mul_le_mul_left -- Be careful! The following theorem is false without the hypotheses m > 0 (why?) theorem pow_le (m : β„•) {n k : β„•} (h : n ≀ k) (mpos : m > 0) : m^n ≀ m^k := have H1: m*n ≀ m*k, from (mul_le_mul_left m h), sorry --Are you supposed to use induction? Because I couldn't figure out what I --was supposed to induct on. --Without induction, I do not know how to expand m ^ n since ^ is defined for --m ^ succ n check le_of_eq /- Part 3. Division on the Natural Numbers. These have nothing to do with inductive types (except for the fact that the existential quantifier is really an inductive type). -/ protected def dvd (m n : β„•) : Prop := βˆƒ k, n = m * k instance : has_dvd nat := ⟨nat.dvd⟩ -- type "dvd" with \| theorem dvd_rfl (m : β„•) : m ∣ m := exists.intro 1 (by rw [(mul_one m)]) theorem dvd_trans : βˆ€ {m n k : β„•}, m ∣ n β†’ n ∣ k β†’ m ∣ k := take m n k, assume h1 h2, have H1: βˆƒ a, n = m * a, from h1, have H2: βˆƒ b, k = n * b, from h2, exists.elim H1 (take a, assume H3: n = m * a, exists.elim H2 (take b, assume H4: k = n * b, show βˆƒ c, k = m * c, from (have H5: k = m * a * b, from (by rw [-H3, H4]), have H6: k = m * (a * b), from (by rw [H5, mul_assoc]), exists.intro (a * b) H6))) theorem dvd_mul_left (m n : β„•) : m ∣ m * n := have H1: m ∣ m, from (dvd_rfl m), have H2: βˆƒ a, m = m * a, from H1, exists.elim H2 (take a, assume H3: m = m * a, have H4: m * n = m * a * n, from (by rw [-H3]), have H5: m * n = m * (a * n), from (by rw [H4, mul_assoc]), show m ∣ m * n , from exists.intro (a * n) H5) theorem dvd_mul_right (m n : β„•) : m ∣ n * m := have H1: m ∣ m * n, from (dvd_mul_left m n), have H2: βˆƒ a, m * n = m * a, from H1, exists.elim H2 (take a, assume H3: m * n = m * a, have H4: n * m = m * a, from (by rw [-H3, mul_comm]), show m ∣ n * m, from exists.intro a H4) theorem dvd_add : βˆ€ {m n k : β„•}, m ∣ n β†’ m ∣ k β†’ m ∣ (n + k) := take m n k, take h₁ hβ‚‚, have H1: βˆƒ a, n = m * a, from h₁, have H2: βˆƒ b, k = m * b, from hβ‚‚, exists.elim H1 (take a, assume H3: n = m * a, exists.elim H2 (take b, assume H4: k = m * b, have H5: n + k = m * (a + b), from (by rw [H3, H4, mul_add]), show m ∣ (n + k), from exists.intro (a + b) H5)) end nat /- Part 4. The size of a binary tree. This one is really optional, for those of you who said you want something more challenging. -/ open nat inductive btree : Type | leaf : btree | node : btree β†’ btree β†’ btree namespace btree def size : btree β†’ β„• | leaf := 1 | (node b₁ bβ‚‚) := size b₁ + size bβ‚‚ + 1 theorem size_pos (b : btree) : size b > 0 := btree.induction_on b dec_trivial (take b₁ bβ‚‚, assume ih₁ ihβ‚‚, succ_pos _) def depth : btree β†’ β„• | leaf := 1 | (node b₁ bβ‚‚) := max (depth b₁) (depth bβ‚‚) + 1 -- This is a scandal. I promise, eventually the simplifier will do this. lemma add_self_eq_two_mul (m : β„•) : m + m = 2 * m := calc m + m = (1 + 1) * m : by simp [add_mul, one_mul] ... = 2 * m : rfl -- these might be useful. check @nat.sub_add_cancel check @nat.add_sub_assoc -- prove this by induction on binary trees theorem size_le' : βˆ€ b : btree, size b + 1 ≀ 2 ^ depth b := take b, btree.induction_on b dec_trivial (take b₁ bβ‚‚, assume ih₁ ihβ‚‚, have H1: (size b₁ + 1) + (size bβ‚‚ + 1) ≀ (2 ^ depth b₁) + (2 ^ depth bβ‚‚), from (add_le_add ih₁ ihβ‚‚), have H2: 2 ^ (max (depth b₁) (depth bβ‚‚) + 1) = 2 ^ depth (node b₁ bβ‚‚), from rfl, have H3: 2 ^ (max (depth b₁) (depth bβ‚‚) + 1) ≀ 2 ^ depth (node b₁ bβ‚‚), from (le_of_eq H2), calc size (node b₁ bβ‚‚) + 1 = (size b₁ + size bβ‚‚ + 1) + 1 : rfl ... = (size b₁ + 1) + (size bβ‚‚ + 1) : by simp ... ≀ (2 ^ depth b₁) + (2 ^ depth bβ‚‚) : H1 ... ≀ 2 ^ (max (depth b₁) (depth bβ‚‚) + 1) : sorry ... = 2 ^ depth (node b₁ bβ‚‚) : rfl ... ≀ 2 ^ depth (node b₁ bβ‚‚) : H3) check @add_le_add theorem size_le : βˆ€ b : btree, size b ≀ 2 ^ depth b - 1:= take b, have size b = size b + 1 - 1, from rfl, calc size b = size b + 1 - 1 : this ... ≀ 2 ^ depth b - 1 : nat.sub_le_sub_right (size_le' b) _ end btree
8af170213916e699968ebee5910db549c3db5d8b
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/tests/lean/hott/670.hlean
39d0769109784360b0bf6608f6be56590559b06b
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
487
hlean
open equiv constants (A B : Typeβ‚€) (f : A β†’ B) (g : B β†’ A) (p : Ξ b, f (g b) = b) (q : Ξ a, g (f a) = a) definition e [constructor] : A ≃ B := equiv.MK f g p q example (b : B) : g (f (g b)) = g b := by rewrite [to_right_inv e b] example (b : B) : g (f (g b)) = g b := by xrewrite [to_right_inv e b] example (b : B) : g (f (g b)) = g b := by krewrite [to_right_inv e b] example (b : B) : g (f (g b)) = g b := begin let H := to_right_inv e b, esimp at H, rewrite H end
33ed1cbb6a39fc2b93751e639a382e29421ef357
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/category_theory/natural_transformation.lean
c8bd4c7dd357955b92f71f3cf254161e702abf7e
[ "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
4,235
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 Defines natural transformations between functors. Introduces notations `F ⟹ G` for the type of natural transformations between functors `F` and `G`, `Ο„.app X` for the components of natural transformations, `Οƒ ⊟ Ο„` for vertical compositions, and `Οƒ β—« Ο„` for horizontal compositions. -/ import category_theory.functor namespace category_theory universes v₁ vβ‚‚ v₃ vβ‚„ u₁ uβ‚‚ u₃ uβ‚„ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {C : Type u₁} [π’ž : category.{v₁} C] {D : Type uβ‚‚} [π’Ÿ : category.{vβ‚‚} D] include π’ž π’Ÿ /-- `nat_trans F G` represents a natural transformation between functors `F` and `G`. The field `app` provides the components of the natural transformation. Naturality is expressed by `Ξ±.naturality_lemma`. -/ structure nat_trans (F G : C β₯€ D) : Type (max u₁ vβ‚‚) := (app : Ξ  X : C, (F.obj X) ⟢ (G.obj X)) (naturality' : βˆ€ {X Y : C} (f : X ⟢ Y), (F.map f) ≫ (app Y) = (app X) ≫ (G.map f) . obviously) infixr ` ⟹ `:50 := nat_trans -- type as \==> or ⟹ restate_axiom nat_trans.naturality' namespace nat_trans /-- `nat_trans.id F` is the identity natural transformation on a functor `F`. -/ protected def id (F : C β₯€ D) : F ⟹ F := { app := Ξ» X, πŸ™ (F.obj X) } @[simp] lemma id_app (F : C β₯€ D) (X : C) : (nat_trans.id F).app X = πŸ™ (F.obj X) := rfl open category open category_theory.functor section variables {F G H I : C β₯€ D} -- We'll want to be able to prove that two natural transformations are equal if they are componentwise equal. @[extensionality] lemma ext (Ξ± Ξ² : F ⟹ G) (w : βˆ€ X : C, Ξ±.app X = Ξ².app X) : Ξ± = Ξ² := begin induction Ξ± with Ξ±_components Ξ±_naturality, induction Ξ² with Ξ²_components Ξ²_naturality, have hc : Ξ±_components = Ξ²_components := funext w, subst hc end lemma congr_app {Ξ± Ξ² : F ⟹ G} (h : Ξ± = Ξ²) (X : C) : Ξ±.app X = Ξ².app X := by rw h /-- `vcomp Ξ± Ξ²` is the vertical compositions of natural transformations. -/ def vcomp (Ξ± : F ⟹ G) (Ξ² : G ⟹ H) : F ⟹ H := { app := Ξ» X, (Ξ±.app X) ≫ (Ξ².app X), naturality' := begin /- `obviously'` says: -/ intros, simp, rw [←assoc, naturality, assoc, ←naturality], end } infixr ` ⊟ `:80 := vcomp @[simp] lemma vcomp_app (Ξ± : F ⟹ G) (Ξ² : G ⟹ H) (X : C) : (Ξ± ⊟ Ξ²).app X = (Ξ±.app X) ≫ (Ξ².app X) := rfl @[simp] lemma vcomp_assoc (Ξ± : F ⟹ G) (Ξ² : G ⟹ H) (Ξ³ : H ⟹ I) : (Ξ± ⊟ Ξ²) ⊟ Ξ³ = Ξ± ⊟ (Ξ² ⊟ Ξ³) := by tidy end variables {E : Type u₃} [β„° : category.{v₃} E] include β„° /-- `hcomp Ξ± Ξ²` is the horizontal composition of natural transformations. -/ def hcomp {F G : C β₯€ D} {H I : D β₯€ E} (Ξ± : F ⟹ G) (Ξ² : H ⟹ I) : (F β‹™ H) ⟹ (G β‹™ I) := { app := Ξ» X : C, (Ξ².app (F.obj X)) ≫ (I.map (Ξ±.app X)), naturality' := begin /- `obviously'` says: -/ intros, dsimp, simp, -- Actually, obviously doesn't use exactly this sequence of rewrites, but achieves the same result rw [← assoc, naturality, assoc], conv { to_rhs, rw [← map_comp, ← Ξ±.naturality, map_comp] } end } infix ` β—« `:80 := hcomp @[simp] lemma hcomp_app {F G : C β₯€ D} {H I : D β₯€ E} (Ξ± : F ⟹ G) (Ξ² : H ⟹ I) (X : C) : (Ξ± β—« Ξ²).app X = (Ξ².app (F.obj X)) ≫ (I.map (Ξ±.app X)) := rfl -- Note that we don't yet prove a `hcomp_assoc` lemma here: even stating it is painful, because we need to use associativity of functor composition lemma exchange {F G H : C β₯€ D} {I J K : D β₯€ E} (Ξ± : F ⟹ G) (Ξ² : G ⟹ H) (Ξ³ : I ⟹ J) (Ξ΄ : J ⟹ K) : ((Ξ± ⊟ Ξ²) β—« (Ξ³ ⊟ Ξ΄)) = ((Ξ± β—« Ξ³) ⊟ (Ξ² β—« Ξ΄)) := begin -- `obviously'` says: ext, intros, dsimp, simp, -- again, this isn't actually what obviously says, but it achieves the same effect. conv { to_lhs, congr, skip, rw [←assoc, ←naturality, assoc] } end end nat_trans end category_theory
5748a86305a0560c108dc1d7d6dc4ea7eb908e4c
de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41
/expressions/scalar_expr.lean
3a349d6744b973fff86d3597889bd7bde7ffa2ab
[]
no_license
kevinsullivan/lang
d3e526ba363dc1ddf5ff1c2f36607d7f891806a7
e9d869bff94fb13ad9262222a6f3c4aafba82d5e
refs/heads/master
1,687,840,064,795
1,628,047,969,000
1,628,047,969,000
282,210,749
0
1
null
1,608,153,830,000
1,595,592,637,000
Lean
UTF-8
Lean
false
false
3,169
lean
import .expr_base import ...phys.scalar structure scalar_var extends var inductive scalar_expr : Type | lit (s : scalar) : scalar_expr | var (v : scalar_var) : scalar_expr | add_scalar_scalar (s1 : scalar_expr) (s2 : scalar_expr) : scalar_expr | mul_scalar_scalar (s1 : scalar_expr) (s2 : scalar_expr) : scalar_expr | inv_scalar (s1 : scalar_expr) : scalar_expr abbreviation scalar_env := scalar_var β†’ scalar abbreviation scalar_eval := scalar_env β†’ scalar_expr β†’ scalar @[simp] def default_scalar_env : scalar_env := Ξ»v, 1 @[simp] def default_scalar_eval : scalar_eval := Ξ»env_, Ξ»expr_, 1 @[simp] noncomputable def static_scalar_eval : scalar_eval | env_ (scalar_expr.lit s) := s | env_ (scalar_expr.var v) := (env_ v) | env_ (scalar_expr.add_scalar_scalar s1 s2) := (static_scalar_eval env_ s1) + (static_scalar_eval env_ s2) | env_ (scalar_expr.mul_scalar_scalar s1 s2) := (static_scalar_eval env_ s1) * (static_scalar_eval env_ s2) | env_ (scalar_expr.inv_scalar s1) := 1/(static_scalar_eval env_ s1) @[simp] noncomputable def scalar_expr.value (expr_ : scalar_expr) : scalar := static_scalar_eval default_scalar_env expr_ notation `|`slit`|` := scalar_expr.lit slit notation s1`+`s2 := scalar_expr.add_scalar_scalar s1 s2 notation s1`*`s2 := scalar_expr.mul_scalar_scalar s1 s2 notation s1`/`s2 := scalar_expr.mul_scalar_scalar s1 (scalar_expr.inv_scalar s2) notation s1⁻¹ := scalar_expr.inv_scalar s1 structure real_scalar_var extends var inductive real_scalar_expr : Type | lit (s : real_scalar) : real_scalar_expr | var (v : real_scalar_var) : real_scalar_expr | add_real_scalar_real_scalar (s1 : real_scalar_expr) (s2 : real_scalar_expr) : real_scalar_expr | mul_real_scalar_real_scalar (s1 : real_scalar_expr) (s2 : real_scalar_expr) : real_scalar_expr | inv_real_scalar (s1 : real_scalar_expr) : real_scalar_expr abbreviation real_scalar_env := real_scalar_var β†’ real_scalar abbreviation real_scalar_eval := real_scalar_env β†’ real_scalar_expr β†’ real_scalar def default_real_scalar_env : real_scalar_env := Ξ»v, 1 def default_real_scalar_eval : real_scalar_eval := Ξ»env_, Ξ»expr_, 1 noncomputable def static_real_scalar_eval : real_scalar_eval | env_ (real_scalar_expr.lit s) := s | env_ (real_scalar_expr.var v) := (env_ v) | env_ (real_scalar_expr.add_real_scalar_real_scalar s1 s2) := (static_real_scalar_eval env_ s1) + (static_real_scalar_eval env_ s2) | env_ (real_scalar_expr.mul_real_scalar_real_scalar s1 s2) := (static_real_scalar_eval env_ s1) * (static_real_scalar_eval env_ s2) | env_ (real_scalar_expr.inv_real_scalar s1) := 1/(static_real_scalar_eval env_ s1) noncomputable def real_scalar_expr.value (expr_ : real_scalar_expr) : real_scalar := static_real_scalar_eval default_real_scalar_env expr_ notation `|`slit`|` := real_scalar_expr.lit slit notation s1`+`s2 := real_scalar_expr.add_real_scalar_real_scalar s1 s2 notation s1`*`s2 := real_scalar_expr.mul_real_scalar_real_scalar s1 s2 notation s1`/`s2 := real_scalar_expr.mul_real_scalar_real_scalar s1 (real_scalar_expr.inv_real_scalar s2) notation s1⁻¹ := real_scalar_expr.inv_real_scalar s1
7658307083d8b6d5ffa29f999720372fbc5d127d
b29f946a2f0afd23ef86b9219116968babbb9f4f
/src/lean_format_tests/patrick.lean
9eb9e2973875115e9ec60c0e241ff6783e5ec331
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/M1P1-lean
58be7394fded719d95e45e6b10e1ecf2ed3c7c4c
3723468cc50f8bebd00a9811caf25224a578de17
refs/heads/master
1,587,063,867,779
1,572,727,164,000
1,572,727,164,000
165,845,802
14
4
Apache-2.0
1,549,730,698,000
1,547,554,675,000
Lean
UTF-8
Lean
false
false
3,827
lean
-- begin header import tactic.linarith import data.real.basic import algebra.pi_instances import data.set.function noncomputable theory local attribute [instance, priority 0] classical.prop_decidable -- We introduce the usual mathematical notation for absolute value local notation `|` x `|` := abs x -- end header /- Section Limits of sequences -/ /- Sub-section Basic definitions -/ /- In this file, we introduce limits of sequences of real numbers. We model a sequence $aβ‚€, a₁, aβ‚‚, \dots$ of real numbers as a function from $β„• := \{0,1,2,\dots\}$ to $ℝ$, sending $n$ to $a_n$. So in the below definition of the limit of a sequence, $a : β„• β†’ ℝ$ is the sequence. -/ /- Definition A sequence $a$ converges to a real number $l$ if, for all positive $Ξ΅$, there is some $N$ such that, for every $n β‰₯ N$, $|a_n - l| < Ξ΅$. -/ definition is_limit (a : β„• β†’ ℝ) (l : ℝ) : Prop := βˆ€ Ξ΅ > 0, βˆƒ N, βˆ€ n β‰₯ N, | a n - l | < Ξ΅ /- Definition A sequence converges if and only if it has a limit. -/ definition has_limit (a : β„• β†’ ℝ) : Prop := βˆƒ l : ℝ, is_limit a l /- The difference between the above definition and the preceding one is that we don't specify the limit, we just claim that it exists. -/ /- Sub-section Basic lemmas -/ /- Lemma The constant sequence with value $a$ converges to $a$. -/ lemma tendsto_const (a : ℝ) : is_limit (Ξ» n, a) a := begin -- Let $Ξ΅$ be any positive real number. intros Ξ΅ Ξ΅pos, -- We choose $N = 0$ in the definition of a limit, use 0, -- and observe that, for every $n β‰₯ N$, |a - a| < Ξ΅ intros n _, simpa [sub_self] using Ξ΅pos end /- We will need an easy reformulation of the limit definition -/ /- Lemma A sequence $a_n$ converges to a number $l$ if and only if the sequence $a_n - l$ converges to zero. -/ lemma tendsto_iff_sub_tendsto_zero {a : β„• β†’ ℝ} {l : ℝ} : is_limit (Ξ» n, a n - l) 0 ↔ is_limit a l := begin -- We need to prove both implications, but both proofs are the same. split ; { -- We assume the premise, and consider any positive $Ξ΅$. intros h Ξ΅ Ξ΅pos, -- By the premise specialized to our $Ξ΅$, we get some $N$, rcases h Ξ΅ Ξ΅pos with ⟨N, H⟩, -- and use that $N$ to prove the other condition use N, -- which is immediate. intros n hn, simpa using H n hn } end /- In the definition of a limit, the final Ξ΅ can be replaced by a constant multiple of Ξ΅. We could assume this constant is positive but we don't want to deal with this when applying the lemma. -/ /- Lemma Let $a$ be a sequence. In order to prove that $a$ converges to some limit $l$, it is sufficient to find some number $K$ such that, for all $Ξ΅ > 0$, there is some $N$ such that, for all $n β‰₯ N$, $|a_n - l| < KΞ΅$. -/ lemma tendsto_of_mul_eps (a : β„• β†’ ℝ) (l : ℝ) (K : ℝ) (h : βˆ€ Ξ΅ > 0, βˆƒ N, βˆ€ n β‰₯ N, | a n - l | < K*Ξ΅) : is_limit a l := begin -- Let $Ξ΅$ be any positive number. intros Ξ΅ Ξ΅pos, -- $K$ is either non positive or positive cases le_or_gt K 0 with Knonpos Kpos, { -- If $K$ is non positive then our assumed bound quickly -- gives a contradiction. exfalso, -- Indeed we can apply our assumption to $Ξ΅ = 1$ to get $N$ such that -- for all $n β‰₯ N$, $|a n - l| < K * 1$ rcases h 1 (by linarith) with ⟨N, H⟩, -- in particular this holds when $n = N$ specialize H N (by linarith), -- but $|a N - l| β‰₯ 0$ so we get a contradiction. have : |a N - l| β‰₯ 0, from abs_nonneg _, linarith }, { -- Now assume $K$ is positive. Our assumption gives $N$ such that, -- for all $n β‰₯ N$, $|a n - l| < K * (Ξ΅ / K)$ rcases h (Ξ΅/K) (div_pos Ξ΅pos Kpos) with ⟨N, H⟩, -- we can simplify that $K (Ξ΅ / K)$ and we are done. rw mul_div_cancel' _ (ne_of_gt Kpos) at H, tauto } end
8d96bcdfeb2a313b66a8a825c8a8dea0d764d6a9
c777c32c8e484e195053731103c5e52af26a25d1
/src/probability/strong_law.lean
e09fa4393ca78d48814facb7b7a784ec0bee6a08
[ "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
36,978
lean
/- Copyright (c) 2022 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 probability.ident_distrib import measure_theory.integral.interval_integral import analysis.specific_limits.floor_pow import analysis.p_series import analysis.asymptotics.specific_asymptotics /-! # The strong law of large numbers We prove the strong law of large numbers, in `probability_theory.strong_law_ae`: If `X n` is a sequence of independent identically distributed integrable real-valued random variables, then `βˆ‘ i in range n, X i / n` converges almost surely to `𝔼[X 0]`. We give here the strong version, due to Etemadi, that only requires pairwise independence. This file also contains the Lα΅– version of the strong law of large numbers provided by `probability_theory.strong_law_Lp` which shows `βˆ‘ i in range n, X i / n` converges in Lα΅– to `𝔼[X 0]` provided `X n` is independent identically distributed and is Lα΅–. ## Implementation We follow the proof by Etemadi [Etemadi, *An elementary proof of the strong law of large numbers*][etemadi_strong_law], which goes as follows. It suffices to prove the result for nonnegative `X`, as one can prove the general result by splitting a general `X` into its positive part and negative part. Consider `Xβ‚™` a sequence of nonnegative integrable identically distributed pairwise independent random variables. Let `Yβ‚™` be the truncation of `Xβ‚™` up to `n`. We claim that * Almost surely, `Xβ‚™ = Yβ‚™` for all but finitely many indices. Indeed, `βˆ‘ β„™ (Xβ‚™ β‰  Yβ‚™)` is bounded by `1 + 𝔼[X]` (see `sum_prob_mem_Ioc_le` and `tsum_prob_mem_Ioi_lt_top`). * Let `c > 1`. Along the sequence `n = c ^ k`, then `(βˆ‘_{i=0}^{n-1} Yα΅’ - 𝔼[Yα΅’])/n` converges almost surely to `0`. This follows from a variance control, as ``` βˆ‘_k β„™ (|βˆ‘_{i=0}^{c^k - 1} Yα΅’ - 𝔼[Yα΅’]| > c^k Ξ΅) ≀ βˆ‘_k (c^k Ξ΅)^{-2} βˆ‘_{i=0}^{c^k - 1} Var[Yα΅’] (by Markov inequality) ≀ βˆ‘_i (C/i^2) Var[Yα΅’] (as βˆ‘_{c^k > i} 1/(c^k)^2 ≀ C/i^2) ≀ βˆ‘_i (C/i^2) 𝔼[Yα΅’^2] ≀ 2C 𝔼[X^2] (see `sum_variance_truncation_le`) ``` * As `𝔼[Yα΅’]` converges to `𝔼[X]`, it follows from the two previous items and Cesaro that, along the sequence `n = c^k`, one has `(βˆ‘_{i=0}^{n-1} Xα΅’) / n β†’ 𝔼[X]` almost surely. * To generalize it to all indices, we use the fact that `βˆ‘_{i=0}^{n-1} Xα΅’` is nondecreasing and that, if `c` is close enough to `1`, the gap between `c^k` and `c^(k+1)` is small. -/ noncomputable theory open measure_theory filter finset asymptotics open set (indicator) open_locale topology big_operators measure_theory probability_theory ennreal nnreal namespace probability_theory /-! ### Prerequisites on truncations -/ section truncation variables {Ξ± : Type*} /-- Truncating a real-valued function to the interval `(-A, A]`. -/ def truncation (f : Ξ± β†’ ℝ) (A : ℝ) := (indicator (set.Ioc (-A) A) id) ∘ f variables {m : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f : Ξ± β†’ ℝ} lemma _root_.measure_theory.ae_strongly_measurable.truncation (hf : ae_strongly_measurable f ΞΌ) {A : ℝ} : ae_strongly_measurable (truncation f A) ΞΌ := begin apply ae_strongly_measurable.comp_ae_measurable _ hf.ae_measurable, exact (strongly_measurable_id.indicator measurable_set_Ioc).ae_strongly_measurable, end lemma abs_truncation_le_bound (f : Ξ± β†’ ℝ) (A : ℝ) (x : Ξ±) : |truncation f A x| ≀ |A| := begin simp only [truncation, set.indicator, set.mem_Icc, id.def, function.comp_app], split_ifs, { exact abs_le_abs h.2 (neg_le.2 h.1.le) }, { simp [abs_nonneg] } end @[simp] lemma truncation_zero (f : Ξ± β†’ ℝ) : truncation f 0 = 0 := by simp [truncation] lemma abs_truncation_le_abs_self (f : Ξ± β†’ ℝ) (A : ℝ) (x : Ξ±) : |truncation f A x| ≀ |f x| := begin simp only [truncation, indicator, set.mem_Icc, id.def, function.comp_app], split_ifs, { exact le_rfl }, { simp [abs_nonneg] }, end lemma truncation_eq_self {f : Ξ± β†’ ℝ} {A : ℝ} {x : Ξ±} (h : |f x| < A) : truncation f A x = f x := begin simp only [truncation, indicator, set.mem_Icc, id.def, function.comp_app, ite_eq_left_iff], assume H, apply H.elim, simp [(abs_lt.1 h).1, (abs_lt.1 h).2.le], end lemma truncation_eq_of_nonneg {f : Ξ± β†’ ℝ} {A : ℝ} (h : βˆ€ x, 0 ≀ f x) : truncation f A = (indicator (set.Ioc 0 A) id) ∘ f := begin ext x, rcases (h x).lt_or_eq with hx|hx, { simp only [truncation, indicator, hx, set.mem_Ioc, id.def, function.comp_app, true_and], by_cases h'x : f x ≀ A, { have : - A < f x, by linarith [h x], simp only [this, true_and] }, { simp only [h'x, and_false] } }, { simp only [truncation, indicator, hx, id.def, function.comp_app, if_t_t]}, end lemma truncation_nonneg {f : Ξ± β†’ ℝ} (A : ℝ) {x : Ξ±} (h : 0 ≀ f x) : 0 ≀ truncation f A x := set.indicator_apply_nonneg $ Ξ» _, h lemma _root_.measure_theory.ae_strongly_measurable.mem_β„’p_truncation [is_finite_measure ΞΌ] (hf : ae_strongly_measurable f ΞΌ) {A : ℝ} {p : ℝβ‰₯0∞} : mem_β„’p (truncation f A) p ΞΌ := mem_β„’p.of_bound hf.truncation (|A|) (eventually_of_forall (Ξ» x, abs_truncation_le_bound _ _ _)) lemma _root_.measure_theory.ae_strongly_measurable.integrable_truncation [is_finite_measure ΞΌ] (hf : ae_strongly_measurable f ΞΌ) {A : ℝ} : integrable (truncation f A) ΞΌ := by { rw ← mem_β„’p_one_iff_integrable, exact hf.mem_β„’p_truncation } lemma moment_truncation_eq_interval_integral (hf : ae_strongly_measurable f ΞΌ) {A : ℝ} (hA : 0 ≀ A) {n : β„•} (hn : n β‰  0) : ∫ x, (truncation f A x) ^ n βˆ‚ΞΌ = ∫ y in (-A)..A, y ^ n βˆ‚(measure.map f ΞΌ) := begin have M : measurable_set (set.Ioc (-A) A) := measurable_set_Ioc, change ∫ x, (Ξ» z, (indicator (set.Ioc (-A) A) id z) ^ n) (f x) βˆ‚ΞΌ = _, rw [← integral_map hf.ae_measurable, interval_integral.integral_of_le, ← integral_indicator M], { simp only [indicator, zero_pow' _ hn, id.def, ite_pow] }, { linarith }, { exact ((measurable_id.indicator M).pow_const n).ae_strongly_measurable } end lemma moment_truncation_eq_interval_integral_of_nonneg (hf : ae_strongly_measurable f ΞΌ) {A : ℝ} {n : β„•} (hn : n β‰  0) (h'f : 0 ≀ f) : ∫ x, (truncation f A x) ^ n βˆ‚ΞΌ = ∫ y in 0..A, y ^ n βˆ‚(measure.map f ΞΌ) := begin have M : measurable_set (set.Ioc 0 A) := measurable_set_Ioc, have M' : measurable_set (set.Ioc A 0) := measurable_set_Ioc, rw truncation_eq_of_nonneg h'f, change ∫ x, (Ξ» z, (indicator (set.Ioc 0 A) id z) ^ n) (f x) βˆ‚ΞΌ = _, rcases le_or_lt 0 A with hA | hA, { rw [← integral_map hf.ae_measurable, interval_integral.integral_of_le hA, ← integral_indicator M], { simp only [indicator, zero_pow' _ hn, id.def, ite_pow] }, { exact ((measurable_id.indicator M).pow_const n).ae_strongly_measurable } }, { rw [← integral_map hf.ae_measurable, interval_integral.integral_of_ge hA.le, ← integral_indicator M'], { simp only [set.Ioc_eq_empty_of_le hA.le, zero_pow' _ hn, set.indicator_empty, integral_zero, zero_eq_neg], apply integral_eq_zero_of_ae, have : βˆ€α΅ x βˆ‚(measure.map f ΞΌ), (0 : ℝ) ≀ x := (ae_map_iff hf.ae_measurable measurable_set_Ici).2 (eventually_of_forall h'f), filter_upwards [this] with x hx, simp only [indicator, set.mem_Ioc, pi.zero_apply, ite_eq_right_iff, and_imp], assume h'x h''x, have : x = 0, by linarith, simp [this, zero_pow' _ hn] }, { exact ((measurable_id.indicator M).pow_const n).ae_strongly_measurable } } end lemma integral_truncation_eq_interval_integral (hf : ae_strongly_measurable f ΞΌ) {A : ℝ} (hA : 0 ≀ A) : ∫ x, truncation f A x βˆ‚ΞΌ = ∫ y in (-A)..A, y βˆ‚(measure.map f ΞΌ) := by simpa using moment_truncation_eq_interval_integral hf hA one_ne_zero lemma integral_truncation_eq_interval_integral_of_nonneg (hf : ae_strongly_measurable f ΞΌ) {A : ℝ} (h'f : 0 ≀ f) : ∫ x, truncation f A x βˆ‚ΞΌ = ∫ y in 0..A, y βˆ‚(measure.map f ΞΌ) := by simpa using moment_truncation_eq_interval_integral_of_nonneg hf one_ne_zero h'f lemma integral_truncation_le_integral_of_nonneg (hf : integrable f ΞΌ) (h'f : 0 ≀ f) {A : ℝ} : ∫ x, truncation f A x βˆ‚ΞΌ ≀ ∫ x, f x βˆ‚ΞΌ := begin apply integral_mono_of_nonneg (eventually_of_forall (Ξ» x, _)) hf (eventually_of_forall (Ξ» x, _)), { exact truncation_nonneg _ (h'f x) }, { calc truncation f A x ≀ |truncation f A x| : le_abs_self _ ... ≀ |f x| : abs_truncation_le_abs_self _ _ _ ... = f x : abs_of_nonneg (h'f x) } end /-- If a function is integrable, then the integral of its truncated versions converges to the integral of the whole function. -/ lemma tendsto_integral_truncation {f : Ξ± β†’ ℝ} (hf : integrable f ΞΌ) : tendsto (Ξ» A, ∫ x, truncation f A x βˆ‚ΞΌ) at_top (𝓝 (∫ x, f x βˆ‚ΞΌ)) := begin refine tendsto_integral_filter_of_dominated_convergence (Ξ» x, abs (f x)) _ _ _ _, { exact eventually_of_forall (Ξ» A, hf.ae_strongly_measurable.truncation) }, { apply eventually_of_forall (Ξ» A, _), apply eventually_of_forall (Ξ» x, _), rw real.norm_eq_abs, exact abs_truncation_le_abs_self _ _ _ }, { apply hf.abs }, { apply eventually_of_forall (Ξ» x, _), apply tendsto_const_nhds.congr' _, filter_upwards [Ioi_mem_at_top (abs (f x))] with A hA, exact (truncation_eq_self hA).symm }, end lemma ident_distrib.truncation {Ξ² : Type*} [measurable_space Ξ²] {Ξ½ : measure Ξ²} {f : Ξ± β†’ ℝ} {g : Ξ² β†’ ℝ} (h : ident_distrib f g ΞΌ Ξ½) {A : ℝ} : ident_distrib (truncation f A) (truncation g A) ΞΌ Ξ½ := h.comp (measurable_id.indicator measurable_set_Ioc) end truncation section strong_law_ae variables {Ξ© : Type*} [measure_space Ξ©] [is_probability_measure (β„™ : measure Ξ©)] section moment_estimates lemma sum_prob_mem_Ioc_le {X : Ξ© β†’ ℝ} (hint : integrable X) (hnonneg : 0 ≀ X) {K : β„•} {N : β„•} (hKN : K ≀ N) : βˆ‘ j in range K, β„™ {Ο‰ | X Ο‰ ∈ set.Ioc (j : ℝ) N} ≀ ennreal.of_real (𝔼[X] + 1) := begin let ρ : measure ℝ := measure.map X β„™, haveI : is_probability_measure ρ := is_probability_measure_map hint.ae_measurable, have A : βˆ‘ j in range K, ∫ x in j..N, (1 : ℝ) βˆ‚Ο ≀ 𝔼[X] + 1, from calc βˆ‘ j in range K, ∫ x in j..N, (1 : ℝ) βˆ‚Ο = βˆ‘ j in range K, βˆ‘ i in Ico j N, ∫ x in i..(i+1 : β„•), (1 : ℝ) βˆ‚Ο : begin apply sum_congr rfl (Ξ» j hj, _), rw interval_integral.sum_integral_adjacent_intervals_Ico ((mem_range.1 hj).le.trans hKN), assume k hk, exact continuous_const.interval_integrable _ _, end ... = βˆ‘ i in range N, βˆ‘ j in range (min (i+1) K), ∫ x in i..(i+1 : β„•), (1 : ℝ) βˆ‚Ο : begin simp_rw [sum_sigma'], refine sum_bij' (Ξ» (p : (Ξ£ (i : β„•), β„•)) hp, (⟨p.2, p.1⟩ : (Ξ£ (i : β„•), β„•))) _ (Ξ» a ha, rfl) (Ξ» (p : (Ξ£ (i : β„•), β„•)) hp, (⟨p.2, p.1⟩ : (Ξ£ (i : β„•), β„•))) _ _ _, { rintros ⟨i, j⟩ hij, simp only [mem_sigma, mem_range, mem_Ico] at hij, simp only [hij, nat.lt_succ_iff.2 hij.2.1, mem_sigma, mem_range, lt_min_iff, and_self] }, { rintros ⟨i, j⟩ hij, simp only [mem_sigma, mem_range, lt_min_iff] at hij, simp only [hij, nat.lt_succ_iff.1 hij.2.1, mem_sigma, mem_range, mem_Ico, and_self] }, { rintros ⟨i, j⟩ hij, refl }, { rintros ⟨i, j⟩ hij, refl }, end ... ≀ βˆ‘ i in range N, (i + 1) * ∫ x in i..(i+1 : β„•), (1 : ℝ) βˆ‚Ο : begin apply sum_le_sum (Ξ» i hi, _), simp only [nat.cast_add, nat.cast_one, sum_const, card_range, nsmul_eq_mul, nat.cast_min], refine mul_le_mul_of_nonneg_right (min_le_left _ _) _, apply interval_integral.integral_nonneg, { simp only [le_add_iff_nonneg_right, zero_le_one] }, { simp only [zero_le_one, implies_true_iff], } end ... ≀ βˆ‘ i in range N, ∫ x in i..(i+1 : β„•), (x + 1) βˆ‚Ο : begin apply sum_le_sum (Ξ» i hi, _), have I : (i : ℝ) ≀ (i + 1 : β„•), by simp only [nat.cast_add, nat.cast_one, le_add_iff_nonneg_right, zero_le_one], simp_rw [interval_integral.integral_of_le I, ← integral_mul_left], apply set_integral_mono_on, { exact continuous_const.integrable_on_Ioc }, { exact (continuous_id.add continuous_const).integrable_on_Ioc }, { exact measurable_set_Ioc }, { assume x hx, simp only [nat.cast_add, nat.cast_one, set.mem_Ioc] at hx, simp [hx.1.le] } end ... = ∫ x in 0..N, x + 1 βˆ‚Ο : begin rw interval_integral.sum_integral_adjacent_intervals (Ξ» k hk, _), { norm_cast }, { exact (continuous_id.add continuous_const).interval_integrable _ _ } end ... = ∫ x in 0..N, x βˆ‚Ο + ∫ x in 0..N, 1 βˆ‚Ο : begin rw interval_integral.integral_add, { exact continuous_id.interval_integrable _ _ }, { exact continuous_const.interval_integrable _ _ }, end ... = 𝔼[truncation X N] + ∫ x in 0..N, 1 βˆ‚Ο : by rw integral_truncation_eq_interval_integral_of_nonneg hint.1 hnonneg ... ≀ 𝔼[X] + ∫ x in 0..N, 1 βˆ‚Ο : add_le_add_right (integral_truncation_le_integral_of_nonneg hint hnonneg) _ ... ≀ 𝔼[X] + 1 : begin refine add_le_add le_rfl _, rw interval_integral.integral_of_le (nat.cast_nonneg _), simp only [integral_const, measure.restrict_apply', measurable_set_Ioc, set.univ_inter, algebra.id.smul_eq_mul, mul_one], rw ← ennreal.one_to_real, exact ennreal.to_real_mono ennreal.one_ne_top prob_le_one, end, have B : βˆ€ a b, β„™ {Ο‰ | X Ο‰ ∈ set.Ioc a b} = ennreal.of_real (∫ x in set.Ioc a b, (1 : ℝ) βˆ‚Ο), { assume a b, rw [of_real_set_integral_one ρ _, measure.map_apply_of_ae_measurable hint.ae_measurable measurable_set_Ioc], refl }, calc βˆ‘ j in range K, β„™ {Ο‰ | X Ο‰ ∈ set.Ioc (j : ℝ) N} = βˆ‘ j in range K, ennreal.of_real (∫ x in set.Ioc (j : ℝ) N, (1 : ℝ) βˆ‚Ο) : by simp_rw B ... = ennreal.of_real (βˆ‘ j in range K, ∫ x in set.Ioc (j : ℝ) N, (1 : ℝ) βˆ‚Ο) : begin rw ennreal.of_real_sum_of_nonneg, simp only [integral_const, algebra.id.smul_eq_mul, mul_one, ennreal.to_real_nonneg, implies_true_iff], end ... = ennreal.of_real (βˆ‘ j in range K, ∫ x in (j : ℝ)..N, (1 : ℝ) βˆ‚Ο) : begin congr' 1, refine sum_congr rfl (Ξ» j hj, _), rw interval_integral.integral_of_le (nat.cast_le.2 ((mem_range.1 hj).le.trans hKN)), end ... ≀ ennreal.of_real (𝔼[X] + 1) : ennreal.of_real_le_of_real A end lemma tsum_prob_mem_Ioi_lt_top {X : Ξ© β†’ ℝ} (hint : integrable X) (hnonneg : 0 ≀ X) : βˆ‘' (j : β„•), β„™ {Ο‰ | X Ο‰ ∈ set.Ioi (j : ℝ)} < ∞ := begin suffices : βˆ€ (K : β„•), βˆ‘ j in range K, β„™ {Ο‰ | X Ο‰ ∈ set.Ioi (j : ℝ)} ≀ ennreal.of_real (𝔼[X] + 1), from (le_of_tendsto_of_tendsto (ennreal.tendsto_nat_tsum _) tendsto_const_nhds (eventually_of_forall this)).trans_lt ennreal.of_real_lt_top, assume K, have A : tendsto (Ξ» (N : β„•), βˆ‘ j in range K, β„™ {Ο‰ | X Ο‰ ∈ set.Ioc (j : ℝ) N}) at_top (𝓝 (βˆ‘ j in range K, β„™ {Ο‰ | X Ο‰ ∈ set.Ioi (j : ℝ)})), { refine tendsto_finset_sum _ (Ξ» i hi, _), have : {Ο‰ | X Ο‰ ∈ set.Ioi (i : ℝ)} = ⋃ (N : β„•), {Ο‰ | X Ο‰ ∈ set.Ioc (i : ℝ) N}, { apply set.subset.antisymm _ _, { assume Ο‰ hΟ‰, obtain ⟨N, hN⟩ : βˆƒ (N : β„•), X Ο‰ ≀ N := exists_nat_ge (X Ο‰), exact set.mem_Union.2 ⟨N, hΟ‰, hN⟩ }, { simp only [set.mem_Ioc, set.mem_Ioi, set.Union_subset_iff, set.set_of_subset_set_of, implies_true_iff] {contextual := tt} } }, rw this, apply tendsto_measure_Union, assume m n hmn x hx, exact ⟨hx.1, hx.2.trans (nat.cast_le.2 hmn)⟩ }, apply le_of_tendsto_of_tendsto A tendsto_const_nhds, filter_upwards [Ici_mem_at_top K] with N hN, exact sum_prob_mem_Ioc_le hint hnonneg hN end lemma sum_variance_truncation_le {X : Ξ© β†’ ℝ} (hint : integrable X) (hnonneg : 0 ≀ X) (K : β„•) : βˆ‘ j in range K, ((j : ℝ) ^ 2) ⁻¹ * 𝔼[(truncation X j) ^ 2] ≀ 2 * 𝔼[X] := begin set Y := Ξ» (n : β„•), truncation X n, let ρ : measure ℝ := measure.map X β„™, have Y2 : βˆ€ n, 𝔼[Y n ^ 2] = ∫ x in 0..n, x ^ 2 βˆ‚Ο, { assume n, change 𝔼[Ξ» x, (Y n x)^2] = _, rw [moment_truncation_eq_interval_integral_of_nonneg hint.1 two_ne_zero hnonneg] }, calc βˆ‘ j in range K, ((j : ℝ) ^ 2) ⁻¹ * 𝔼[Y j ^ 2] = βˆ‘ j in range K, ((j : ℝ) ^ 2) ⁻¹ * ∫ x in 0..j, x ^ 2 βˆ‚Ο : by simp_rw [Y2] ... = βˆ‘ j in range K, ((j : ℝ) ^ 2) ⁻¹ * βˆ‘ k in range j, ∫ x in k..(k+1 : β„•), x ^ 2 βˆ‚Ο : begin congr' 1 with j, congr' 1, rw interval_integral.sum_integral_adjacent_intervals, { norm_cast }, assume k hk, exact (continuous_id.pow _).interval_integrable _ _, end ... = βˆ‘ k in range K, (βˆ‘ j in Ioo k K, ((j : ℝ) ^ 2) ⁻¹) * ∫ x in k..(k+1 : β„•), x ^ 2 βˆ‚Ο : begin simp_rw [mul_sum, sum_mul, sum_sigma'], refine sum_bij' (Ξ» (p : (Ξ£ (i : β„•), β„•)) hp, (⟨p.2, p.1⟩ : (Ξ£ (i : β„•), β„•))) _ (Ξ» a ha, rfl) (Ξ» (p : (Ξ£ (i : β„•), β„•)) hp, (⟨p.2, p.1⟩ : (Ξ£ (i : β„•), β„•))) _ _ _, { rintros ⟨i, j⟩ hij, simp only [mem_sigma, mem_range, mem_filter] at hij, simp [hij, mem_sigma, mem_range, and_self, hij.2.trans hij.1], }, { rintros ⟨i, j⟩ hij, simp only [mem_sigma, mem_range, mem_Ioo] at hij, simp only [hij, mem_sigma, mem_range, and_self], }, { rintros ⟨i, j⟩ hij, refl }, { rintros ⟨i, j⟩ hij, refl }, end ... ≀ βˆ‘ k in range K, (2/ (k+1)) * ∫ x in k..(k+1 : β„•), x ^ 2 βˆ‚Ο : begin apply sum_le_sum (Ξ» k hk, _), refine mul_le_mul_of_nonneg_right (sum_Ioo_inv_sq_le _ _) _, refine interval_integral.integral_nonneg_of_forall _ (Ξ» u, sq_nonneg _), simp only [nat.cast_add, nat.cast_one, le_add_iff_nonneg_right, zero_le_one], end ... ≀ βˆ‘ k in range K, ∫ x in k..(k+1 : β„•), 2 * x βˆ‚Ο : begin apply sum_le_sum (Ξ» k hk, _), have Ik : (k : ℝ) ≀ (k + 1 : β„•), by simp, rw [← interval_integral.integral_const_mul, interval_integral.integral_of_le Ik, interval_integral.integral_of_le Ik], refine set_integral_mono_on _ _ measurable_set_Ioc (Ξ» x hx, _), { apply continuous.integrable_on_Ioc, exact continuous_const.mul (continuous_pow 2) }, { apply continuous.integrable_on_Ioc, exact continuous_const.mul continuous_id' }, { calc 2 / (↑k + 1) * x ^ 2 = (x / (k+1)) * (2 * x) : by ring_exp ... ≀ 1 * (2 * x) : mul_le_mul_of_nonneg_right begin apply_mod_cast (div_le_one _).2 hx.2, simp only [nat.cast_add, nat.cast_one], linarith only [show (0 : ℝ) ≀ k, from nat.cast_nonneg k], end (mul_nonneg zero_le_two ((nat.cast_nonneg k).trans hx.1.le)) ... = 2 * x : by rw one_mul } end ... = 2 * ∫ x in (0 : ℝ)..K, x βˆ‚Ο : begin rw interval_integral.sum_integral_adjacent_intervals (Ξ» k hk, _), swap, { exact (continuous_const.mul continuous_id').interval_integrable _ _ }, rw interval_integral.integral_const_mul, norm_cast end ... ≀ 2 * 𝔼[X] : mul_le_mul_of_nonneg_left begin rw ← integral_truncation_eq_interval_integral_of_nonneg hint.1 hnonneg, exact integral_truncation_le_integral_of_nonneg hint hnonneg, end zero_le_two end end moment_estimates section strong_law_nonneg /- This paragraph proves the strong law of large numbers (almost sure version, assuming only pairwise independence) for nonnegative random variables, following Etemadi's proof. -/ variables (X : β„• β†’ Ξ© β†’ ℝ) (hint : integrable (X 0)) (hindep : pairwise (Ξ» i j, indep_fun (X i) (X j))) (hident : βˆ€ i, ident_distrib (X i) (X 0)) (hnonneg : βˆ€ i Ο‰, 0 ≀ X i Ο‰) include X hint hindep hident hnonneg /- The truncation of `Xα΅’` up to `i` satisfies the strong law of large numbers (with respect to the truncated expectation) along the sequence `c^n`, for any `c > 1`, up to a given `Ξ΅ > 0`. This follows from a variance control. -/ lemma strong_law_aux1 {c : ℝ} (c_one : 1 < c) {Ξ΅ : ℝ} (Ξ΅pos : 0 < Ξ΅) : βˆ€α΅ Ο‰, βˆ€αΆ  (n : β„•) in at_top, |βˆ‘ i in range ⌊c^nβŒ‹β‚Š, truncation (X i) i Ο‰ - 𝔼[βˆ‘ i in range ⌊c^nβŒ‹β‚Š, truncation (X i) i]| < Ξ΅ * ⌊c^nβŒ‹β‚Š := begin /- Let `S n = βˆ‘ i in range n, Y i` where `Y i = truncation (X i) i`. We should show that `|S k - 𝔼[S k]| / k ≀ Ξ΅` along the sequence of powers of `c`. For this, we apply Borel-Cantelli: it suffices to show that the converse probabilites are summable. From Chebyshev inequality, this will follow from a variance control `βˆ‘' Var[S (c^i)] / (c^i)^2 < ∞`. This is checked in `I2` using pairwise independence to expand the variance of the sum as the sum of the variances, and then a straightforward but tedious computation (essentially boiling down to the fact that the sum of `1/(c ^ i)^2` beyong a threshold `j` is comparable to `1/j^2`). Note that we have written `c^i` in the above proof sketch, but rigorously one should put integer parts everywhere, making things more painful. We write `u i = ⌊c^iβŒ‹β‚Š` for brevity. -/ have c_pos : 0 < c := zero_lt_one.trans c_one, let ρ : measure ℝ := measure.map (X 0) β„™, have hX : βˆ€ i, ae_strongly_measurable (X i) β„™ := Ξ» i, (hident i).symm.ae_strongly_measurable_snd hint.1, have A : βˆ€ i, strongly_measurable (indicator (set.Ioc (-i : ℝ) i) id) := Ξ» i, strongly_measurable_id.indicator measurable_set_Ioc, set Y := Ξ» (n : β„•), truncation (X n) n with hY, set S := Ξ» n, βˆ‘ i in range n, Y i with hS, let u : β„• β†’ β„• := Ξ» n, ⌊c ^ nβŒ‹β‚Š, have u_mono : monotone u := Ξ» i j hij, nat.floor_mono (pow_le_pow c_one.le hij), have I1 : βˆ€ K, βˆ‘ j in range K, ((j : ℝ) ^ 2) ⁻¹ * Var[Y j] ≀ 2 * 𝔼[X 0], { assume K, calc βˆ‘ j in range K, ((j : ℝ) ^ 2) ⁻¹ * Var[Y j] ≀ βˆ‘ j in range K, ((j : ℝ) ^ 2) ⁻¹ * 𝔼[(truncation (X 0) j)^2] : begin apply sum_le_sum (Ξ» j hj, _), refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 (sq_nonneg _)), rw (hident j).truncation.variance_eq, exact variance_le_expectation_sq (hX 0).truncation, end ... ≀ 2 * 𝔼[X 0] : sum_variance_truncation_le hint (hnonneg 0) K }, let C := (c ^ 5 * (c - 1) ⁻¹ ^ 3) * (2 * 𝔼[X 0]), have I2 : βˆ€ N, βˆ‘ i in range N, ((u i : ℝ) ^ 2) ⁻¹ * Var[S (u i)] ≀ C, { assume N, calc βˆ‘ i in range N, ((u i : ℝ) ^ 2) ⁻¹ * Var[S (u i)] = βˆ‘ i in range N, ((u i : ℝ) ^ 2) ⁻¹ * (βˆ‘ j in range (u i), Var[Y j]) : begin congr' 1 with i, congr' 1, rw [hS, indep_fun.variance_sum], { assume j hj, exact (hident j).ae_strongly_measurable_fst.mem_β„’p_truncation }, { assume k hk l hl hkl, exact (hindep hkl).comp (A k).measurable (A l).measurable } end ... = βˆ‘ j in range (u (N - 1)), (βˆ‘ i in (range N).filter (Ξ» i, j < u i), ((u i : ℝ) ^ 2) ⁻¹) * Var[Y j] : begin simp_rw [mul_sum, sum_mul, sum_sigma'], refine sum_bij' (Ξ» (p : (Ξ£ (i : β„•), β„•)) hp, (⟨p.2, p.1⟩ : (Ξ£ (i : β„•), β„•))) _ (Ξ» a ha, rfl) (Ξ» (p : (Ξ£ (i : β„•), β„•)) hp, (⟨p.2, p.1⟩ : (Ξ£ (i : β„•), β„•))) _ _ _, { rintros ⟨i, j⟩ hij, simp only [mem_sigma, mem_range] at hij, simp only [hij.1, hij.2, mem_sigma, mem_range, mem_filter, and_true], exact hij.2.trans_le (u_mono (nat.le_pred_of_lt hij.1)) }, { rintros ⟨i, j⟩ hij, simp only [mem_sigma, mem_range, mem_filter] at hij, simp only [hij.2.1, hij.2.2, mem_sigma, mem_range, and_self] }, { rintros ⟨i, j⟩ hij, refl }, { rintros ⟨i, j⟩ hij, refl }, end ... ≀ βˆ‘ j in range (u (N - 1)), (c ^ 5 * (c - 1) ⁻¹ ^ 3 / j ^ 2) * Var[Y j] : begin apply sum_le_sum (Ξ» j hj, _), rcases @eq_zero_or_pos _ _ j with rfl|hj, { simp only [Y, nat.cast_zero, zero_pow', ne.def, bit0_eq_zero, nat.one_ne_zero, not_false_iff, div_zero, zero_mul], simp only [nat.cast_zero, truncation_zero, variance_zero, mul_zero] }, apply mul_le_mul_of_nonneg_right _ (variance_nonneg _ _), convert sum_div_nat_floor_pow_sq_le_div_sq N (nat.cast_pos.2 hj) c_one, { simp only [nat.cast_lt] }, { simp only [one_div] } end ... = (c ^ 5 * (c - 1) ⁻¹ ^ 3) * βˆ‘ j in range (u (N - 1)), ((j : ℝ) ^ 2) ⁻¹ * Var[Y j] : by { simp_rw [mul_sum, div_eq_mul_inv], ring_nf } ... ≀ (c ^ 5 * (c - 1) ⁻¹ ^ 3) * (2 * 𝔼[X 0]) : begin apply mul_le_mul_of_nonneg_left (I1 _), apply mul_nonneg (pow_nonneg c_pos.le _), exact pow_nonneg (inv_nonneg.2 (sub_nonneg.2 c_one.le)) _ end }, have I3 : βˆ€ N, βˆ‘ i in range N, β„™ {Ο‰ | (u i * Ξ΅ : ℝ) ≀ |S (u i) Ο‰ - 𝔼[S (u i)]|} ≀ ennreal.of_real (Ξ΅ ⁻¹ ^ 2 * C), { assume N, calc βˆ‘ i in range N, β„™ {Ο‰ | (u i * Ξ΅ : ℝ) ≀ |S (u i) Ο‰ - 𝔼[S (u i)]|} ≀ βˆ‘ i in range N, ennreal.of_real (Var[S (u i)] / (u i * Ξ΅) ^ 2) : begin refine sum_le_sum (Ξ» i hi, _), apply meas_ge_le_variance_div_sq, { exact mem_β„’p_finset_sum' _ (Ξ» j hj, (hident j).ae_strongly_measurable_fst.mem_β„’p_truncation) }, { apply mul_pos (nat.cast_pos.2 _) Ξ΅pos, refine zero_lt_one.trans_le _, apply nat.le_floor, rw nat.cast_one, apply one_le_pow_of_one_le c_one.le } end ... = ennreal.of_real (βˆ‘ i in range N, Var[S (u i)] / (u i * Ξ΅) ^ 2) : begin rw ennreal.of_real_sum_of_nonneg (Ξ» i hi, _), exact div_nonneg (variance_nonneg _ _) (sq_nonneg _), end ... ≀ ennreal.of_real (Ξ΅ ⁻¹ ^ 2 * C) : begin apply ennreal.of_real_le_of_real, simp_rw [div_eq_inv_mul, ← inv_pow, mul_inv, mul_comm _ (Ρ⁻¹), mul_pow, mul_assoc, ← mul_sum], refine mul_le_mul_of_nonneg_left _ (sq_nonneg _), simp_rw [inv_pow], exact I2 N end }, have I4 : βˆ‘' i, β„™ {Ο‰ | (u i * Ξ΅ : ℝ) ≀ |S (u i) Ο‰ - 𝔼[S (u i)]|} < ∞ := (le_of_tendsto_of_tendsto' (ennreal.tendsto_nat_tsum _) tendsto_const_nhds I3).trans_lt ennreal.of_real_lt_top, filter_upwards [ae_eventually_not_mem I4.ne] with Ο‰ hΟ‰, simp_rw [not_le, mul_comm, S, sum_apply] at hΟ‰, exact hΟ‰, end /- The truncation of `Xα΅’` up to `i` satisfies the strong law of large numbers (with respect to the truncated expectation) along the sequence `c^n`, for any `c > 1`. This follows from `strong_law_aux1` by varying `Ξ΅`. -/ lemma strong_law_aux2 {c : ℝ} (c_one : 1 < c) : βˆ€α΅ Ο‰, (Ξ» (n : β„•), βˆ‘ i in range ⌊c^nβŒ‹β‚Š, truncation (X i) i Ο‰ - 𝔼[βˆ‘ i in range ⌊c^nβŒ‹β‚Š, truncation (X i) i]) =o[at_top] (Ξ» (n : β„•), (⌊c^nβŒ‹β‚Š : ℝ)) := begin obtain ⟨v, -, v_pos, v_lim⟩ : βˆƒ (v : β„• β†’ ℝ), strict_anti v ∧ (βˆ€ (n : β„•), 0 < v n) ∧ tendsto v at_top (𝓝 0) := exists_seq_strict_anti_tendsto (0 : ℝ), have := Ξ» i, strong_law_aux1 X hint hindep hident hnonneg c_one (v_pos i), filter_upwards [ae_all_iff.2 this] with Ο‰ hΟ‰, apply asymptotics.is_o_iff.2 (Ξ» Ξ΅ Ξ΅pos, _), obtain ⟨i, hi⟩ : βˆƒ i, v i < Ξ΅ := ((tendsto_order.1 v_lim).2 Ξ΅ Ξ΅pos).exists, filter_upwards [hΟ‰ i] with n hn, simp only [real.norm_eq_abs, lattice_ordered_comm_group.abs_abs, nat.abs_cast], exact hn.le.trans (mul_le_mul_of_nonneg_right hi.le (nat.cast_nonneg _)), end omit hindep hnonneg /-- The expectation of the truncated version of `Xα΅’` behaves asymptotically like the whole expectation. This follows from convergence and Cesaro averaging. -/ lemma strong_law_aux3 : (Ξ» n, 𝔼[βˆ‘ i in range n, truncation (X i) i] - n * 𝔼[X 0]) =o[at_top] (coe : β„• β†’ ℝ) := begin have A : tendsto (Ξ» i, 𝔼[truncation (X i) i]) at_top (𝓝 (𝔼[X 0])), { convert (tendsto_integral_truncation hint).comp tendsto_coe_nat_at_top_at_top, ext i, exact (hident i).truncation.integral_eq }, convert asymptotics.is_o_sum_range_of_tendsto_zero (tendsto_sub_nhds_zero_iff.2 A), ext1 n, simp only [sum_sub_distrib, sum_const, card_range, nsmul_eq_mul, sum_apply, sub_left_inj], rw integral_finset_sum _ (Ξ» i hi, _), exact ((hident i).symm.integrable_snd hint).1.integrable_truncation, end include hindep hnonneg /- The truncation of `Xα΅’` up to `i` satisfies the strong law of large numbers (with respect to the original expectation) along the sequence `c^n`, for any `c > 1`. This follows from the version from the truncated expectation, and the fact that the truncated and the original expectations have the same asymptotic behavior. -/ lemma strong_law_aux4 {c : ℝ} (c_one : 1 < c) : βˆ€α΅ Ο‰, (Ξ» (n : β„•), βˆ‘ i in range ⌊c^nβŒ‹β‚Š, truncation (X i) i Ο‰ - ⌊c^nβŒ‹β‚Š * 𝔼[X 0]) =o[at_top] (Ξ» (n : β„•), (⌊c^nβŒ‹β‚Š : ℝ)) := begin filter_upwards [strong_law_aux2 X hint hindep hident hnonneg c_one] with Ο‰ hΟ‰, have A : tendsto (Ξ» (n : β„•), ⌊c ^ nβŒ‹β‚Š) at_top at_top := tendsto_nat_floor_at_top.comp (tendsto_pow_at_top_at_top_of_one_lt c_one), convert hΟ‰.add ((strong_law_aux3 X hint hident).comp_tendsto A), ext1 n, simp, end omit hindep /-- The truncated and non-truncated versions of `Xα΅’` have the same asymptotic behavior, as they almost surely coincide at all but finitely many steps. This follows from a probability computation and Borel-Cantelli. -/ lemma strong_law_aux5 : βˆ€α΅ Ο‰, (Ξ» (n : β„•), βˆ‘ i in range n, truncation (X i) i Ο‰ - βˆ‘ i in range n, X i Ο‰) =o[at_top] (Ξ» (n : β„•), (n : ℝ)) := begin have A : βˆ‘' (j : β„•), β„™ {Ο‰ | X j Ο‰ ∈ set.Ioi (j : ℝ)} < ∞, { convert tsum_prob_mem_Ioi_lt_top hint (hnonneg 0), ext1 j, exact (hident j).measure_mem_eq measurable_set_Ioi }, have B : βˆ€α΅ Ο‰, tendsto (Ξ» (n : β„•), truncation (X n) n Ο‰ - X n Ο‰) at_top (𝓝 0), { filter_upwards [ae_eventually_not_mem A.ne] with Ο‰ hΟ‰, apply tendsto_const_nhds.congr' _, filter_upwards [hΟ‰, Ioi_mem_at_top 0] with n hn npos, simp only [truncation, indicator, set.mem_Ioc, id.def, function.comp_app], split_ifs, { exact (sub_self _).symm }, { have : - (n : ℝ) < X n Ο‰, { apply lt_of_lt_of_le _ (hnonneg n Ο‰), simpa only [right.neg_neg_iff, nat.cast_pos] using npos }, simp only [this, true_and, not_le] at h, exact (hn h).elim } }, filter_upwards [B] with Ο‰ hΟ‰, convert is_o_sum_range_of_tendsto_zero hΟ‰, ext n, rw sum_sub_distrib, end include hindep /- `Xα΅’` satisfies the strong law of large numbers along the sequence `c^n`, for any `c > 1`. This follows from the version for the truncated `Xα΅’`, and the fact that `Xα΅’` and its truncated version have the same asymptotic behavior. -/ lemma strong_law_aux6 {c : ℝ} (c_one : 1 < c) : βˆ€α΅ Ο‰, tendsto (Ξ» (n : β„•), (βˆ‘ i in range ⌊c^nβŒ‹β‚Š, X i Ο‰) / ⌊c^nβŒ‹β‚Š) at_top (𝓝 (𝔼[X 0])) := begin have H : βˆ€ (n : β„•), (0 : ℝ) < ⌊c ^ nβŒ‹β‚Š, { assume n, refine zero_lt_one.trans_le _, simp only [nat.one_le_cast, nat.one_le_floor_iff, one_le_pow_of_one_le c_one.le n] }, filter_upwards [strong_law_aux4 X hint hindep hident hnonneg c_one, strong_law_aux5 X hint hident hnonneg] with Ο‰ hΟ‰ h'Ο‰, rw [← tendsto_sub_nhds_zero_iff, ← asymptotics.is_o_one_iff ℝ], have L : (Ξ» n : β„•, βˆ‘ i in range ⌊c^nβŒ‹β‚Š, X i Ο‰ - ⌊c^nβŒ‹β‚Š * 𝔼[X 0]) =o[at_top] (Ξ» n, (⌊c^nβŒ‹β‚Š : ℝ)), { have A : tendsto (Ξ» (n : β„•), ⌊c ^ nβŒ‹β‚Š) at_top at_top := tendsto_nat_floor_at_top.comp (tendsto_pow_at_top_at_top_of_one_lt c_one), convert hΟ‰.sub (h'Ο‰.comp_tendsto A), ext1 n, simp only [sub_sub_sub_cancel_left] }, convert L.mul_is_O (is_O_refl (Ξ» (n : β„•), (⌊c ^ nβŒ‹β‚Š : ℝ) ⁻¹) at_top); { ext1 n, field_simp [(H n).ne'] }, end /-- `Xα΅’` satisfies the strong law of large numbers along all integers. This follows from the corresponding fact along the sequences `c^n`, and the fact that any integer can be sandwiched between `c^n` and `c^(n+1)` with comparably small error if `c` is close enough to `1` (which is formalized in `tendsto_div_of_monotone_of_tendsto_div_floor_pow`). -/ lemma strong_law_aux7 : βˆ€α΅ Ο‰, tendsto (Ξ» (n : β„•), (βˆ‘ i in range n, X i Ο‰) / n) at_top (𝓝 (𝔼[X 0])) := begin obtain ⟨c, -, cone, clim⟩ : βˆƒ (c : β„• β†’ ℝ), strict_anti c ∧ (βˆ€ (n : β„•), 1 < c n) ∧ tendsto c at_top (𝓝 1) := exists_seq_strict_anti_tendsto (1 : ℝ), have : βˆ€ k, βˆ€α΅ Ο‰, tendsto (Ξ» (n : β„•), (βˆ‘ i in range ⌊c k ^ nβŒ‹β‚Š, X i Ο‰) / ⌊c k ^ nβŒ‹β‚Š) at_top (𝓝 (𝔼[X 0])) := Ξ» k, strong_law_aux6 X hint hindep hident hnonneg (cone k), filter_upwards [ae_all_iff.2 this] with Ο‰ hΟ‰, apply tendsto_div_of_monotone_of_tendsto_div_floor_pow _ _ _ c cone clim _, { assume m n hmn, exact sum_le_sum_of_subset_of_nonneg (range_mono hmn) (Ξ» i hi h'i, hnonneg i Ο‰) }, { exact hΟ‰ } end end strong_law_nonneg /-- *Strong law of large numbers*, almost sure version: if `X n` is a sequence of independent identically distributed integrable real-valued random variables, then `βˆ‘ i in range n, X i / n` converges almost surely to `𝔼[X 0]`. We give here the strong version, due to Etemadi, that only requires pairwise independence. -/ theorem strong_law_ae (X : β„• β†’ Ξ© β†’ ℝ) (hint : integrable (X 0)) (hindep : pairwise (Ξ» i j, indep_fun (X i) (X j))) (hident : βˆ€ i, ident_distrib (X i) (X 0)) : βˆ€α΅ Ο‰, tendsto (Ξ» (n : β„•), (βˆ‘ i in range n, X i Ο‰) / n) at_top (𝓝 (𝔼[X 0])) := begin let pos : ℝ β†’ ℝ := (Ξ» x, max x 0), let neg : ℝ β†’ ℝ := (Ξ» x, max (-x) 0), have posm : measurable pos := measurable_id'.max measurable_const, have negm : measurable neg := measurable_id'.neg.max measurable_const, have A : βˆ€α΅ Ο‰, tendsto (Ξ» (n : β„•), (βˆ‘ i in range n, (pos ∘ (X i)) Ο‰) / n) at_top (𝓝 (𝔼[pos ∘ (X 0)])) := strong_law_aux7 _ hint.pos_part (Ξ» i j hij, (hindep hij).comp posm posm) (Ξ» i, (hident i).comp posm) (Ξ» i Ο‰, le_max_right _ _), have B : βˆ€α΅ Ο‰, tendsto (Ξ» (n : β„•), (βˆ‘ i in range n, (neg ∘ (X i)) Ο‰) / n) at_top (𝓝 (𝔼[neg ∘ (X 0)])) := strong_law_aux7 _ hint.neg_part (Ξ» i j hij, (hindep hij).comp negm negm) (Ξ» i, (hident i).comp negm) (Ξ» i Ο‰, le_max_right _ _), filter_upwards [A, B] with Ο‰ hΟ‰pos hΟ‰neg, convert hΟ‰pos.sub hΟ‰neg, { simp only [← sub_div, ← sum_sub_distrib, max_zero_sub_max_neg_zero_eq_self] }, { simp only [←integral_sub hint.pos_part hint.neg_part, max_zero_sub_max_neg_zero_eq_self] } end end strong_law_ae section strong_law_Lp variables {Ξ© : Type*} [measure_space Ξ©] [is_probability_measure (β„™ : measure Ξ©)] /-- *Strong law of large numbers*, Lα΅– version: if `X n` is a sequence of independent identically distributed real-valued random variables in Lα΅–, then `βˆ‘ i in range n, X i / n` converges in Lα΅– to `𝔼[X 0]`. -/ theorem strong_law_Lp {p : ℝβ‰₯0∞} (hp : 1 ≀ p) (hp' : p β‰  ∞) (X : β„• β†’ Ξ© β†’ ℝ) (hβ„’p : mem_β„’p (X 0) p) (hindep : pairwise (Ξ» i j, indep_fun (X i) (X j))) (hident : βˆ€ i, ident_distrib (X i) (X 0)) : tendsto (Ξ» n, snorm (Ξ» Ο‰, (βˆ‘ i in range n, X i Ο‰) / n - 𝔼[X 0]) p β„™) at_top (𝓝 0) := begin have hmeas : βˆ€ i, ae_strongly_measurable (X i) β„™ := Ξ» i, (hident i).ae_strongly_measurable_iff.2 hβ„’p.1, have hint : integrable (X 0) β„™ := hβ„’p.integrable hp, have havg : βˆ€ n, ae_strongly_measurable (Ξ» Ο‰, (βˆ‘ i in range n, X i Ο‰) / n) β„™, { intro n, simp_rw div_eq_mul_inv, exact ae_strongly_measurable.mul_const (ae_strongly_measurable_sum _ (Ξ» i _, hmeas i)) _ }, refine tendsto_Lp_of_tendsto_in_measure _ hp hp' havg (mem_β„’p_const _) _ (tendsto_in_measure_of_tendsto_ae havg (strong_law_ae _ hint hindep hident)), rw (_ : (Ξ» n Ο‰, (βˆ‘ i in range n, X i Ο‰) / ↑n) = Ξ» n, (βˆ‘ i in range n, X i) / ↑n), { exact (uniform_integrable_average hp $ mem_β„’p.uniform_integrable_of_ident_distrib hp hp' hβ„’p hident).2.1 }, { ext n Ο‰, simp only [pi.coe_nat, pi.div_apply, sum_apply] } end end strong_law_Lp end probability_theory
e11ef43f1f5d4dffc2e083124965a549f430631f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/concrete_category/unbundled_hom_auto.lean
f1fb60f30b3f09d0a023927411b0d200cdb15aa3
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,565
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.concrete_category.bundled_hom import Mathlib.PostPort universes u l namespace Mathlib /-! # Category instances for structures that use unbundled homs This file provides basic infrastructure to define concrete categories using unbundled homs (see `class unbundled_hom`), and define forgetful functors between them (see `unbundled_hom.mk_has_forgetβ‚‚`). -/ namespace category_theory /-- A class for unbundled homs used to define a category. `hom` must take two types `Ξ±`, `Ξ²` and instances of the corresponding structures, and return a predicate on `Ξ± β†’ Ξ²`. -/ class unbundled_hom {c : Type u β†’ Type u} (hom : {Ξ± Ξ² : Type u} β†’ c Ξ± β†’ c Ξ² β†’ (Ξ± β†’ Ξ²) β†’ Prop) where hom_id : βˆ€ {Ξ± : Type u} (ia : c Ξ±), hom ia ia id hom_comp : βˆ€ {Ξ± Ξ² Ξ³ : Type u} {IΞ± : c Ξ±} {IΞ² : c Ξ²} {IΞ³ : c Ξ³} {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²}, hom IΞ² IΞ³ g β†’ hom IΞ± IΞ² f β†’ hom IΞ± IΞ³ (g ∘ f) namespace unbundled_hom protected instance bundled_hom (c : Type u β†’ Type u) (hom : {Ξ± Ξ² : Type u} β†’ c Ξ± β†’ c Ξ² β†’ (Ξ± β†’ Ξ²) β†’ Prop) [π’ž : unbundled_hom hom] : bundled_hom fun (Ξ± Ξ² : Type u) (IΞ± : c Ξ±) (IΞ² : c Ξ²) => Subtype (hom IΞ± IΞ²) := bundled_hom.mk (fun (_x _x_1 : Type u) (_x_2 : c _x) (_x_3 : c _x_1) => subtype.val) (fun (Ξ± : Type u) (IΞ± : c Ξ±) => { val := id, property := sorry }) fun (_x _x_1 _x_2 : Type u) (_x_3 : c _x) (_x_4 : c _x_1) (_x_5 : c _x_2) (g : Subtype (hom _x_4 _x_5)) (f : Subtype (hom _x_3 _x_4)) => { val := subtype.val g ∘ subtype.val f, property := sorry } /-- A custom constructor for forgetful functor between concrete categories defined using `unbundled_hom`. -/ def mk_has_forgetβ‚‚ {c : Type u β†’ Type u} {hom : {Ξ± Ξ² : Type u} β†’ c Ξ± β†’ c Ξ² β†’ (Ξ± β†’ Ξ²) β†’ Prop} [π’ž : unbundled_hom hom] {c' : Type u β†’ Type u} {hom' : {Ξ± Ξ² : Type u} β†’ c' Ξ± β†’ c' Ξ² β†’ (Ξ± β†’ Ξ²) β†’ Prop} [π’ž' : unbundled_hom hom'] (obj : {Ξ± : Type u} β†’ c Ξ± β†’ c' Ξ±) (map : βˆ€ {Ξ± Ξ² : Type u} {IΞ± : c Ξ±} {IΞ² : c Ξ²} {f : Ξ± β†’ Ξ²}, hom IΞ± IΞ² f β†’ hom' (obj IΞ±) (obj IΞ²) f) : has_forgetβ‚‚ (bundled c) (bundled c') := bundled_hom.mk_has_forgetβ‚‚ obj (fun (X Y : bundled c) (f : X ⟢ Y) => { val := subtype.val f, property := sorry }) sorry end Mathlib
5ff8f57208a638203b4d9944976e117f144a2ca8
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/theories/analysis/bounded_linear_operator.lean
e24e7debf5830e4c65de59b24b55678e18636d4a
[ "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
23,267
lean
/- Copyright (c) 2016 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis Bounded linear operators -/ import .normed_space .real_limit algebra.module algebra.homomorphism open real nat classical topology set --open normed_vector_space (this confuses lots of stuff??) noncomputable theory namespace analysis -- define bounded linear operators and basic instances section bdd_lin_op structure is_bdd_linear_map [class] {V W : Type} [normed_vector_space V] [normed_vector_space W] (f : V β†’ W) extends is_module_hom ℝ f := (op_norm : ℝ) (op_norm_pos : op_norm > 0) (op_norm_bound : βˆ€ v : V, βˆ₯f vβˆ₯ ≀ op_norm * βˆ₯vβˆ₯) theorem is_bdd_linear_map_id [instance] (V : Type) [normed_vector_space V] : is_bdd_linear_map (Ξ» x : V, x) := begin fapply is_bdd_linear_map.mk, repeat (intros; reflexivity), exact 1, exact zero_lt_one, intro, rewrite one_mul, apply le.refl end theorem is_bdd_linear_map_zero [instance] (V W : Type) [normed_vector_space V] [normed_vector_space W] : is_bdd_linear_map (Ξ» x : V, (0 : W)) := begin fapply is_bdd_linear_map.mk, all_goals intros, rewrite zero_add, rewrite smul_zero, exact 1, exact zero_lt_one, rewrite [norm_zero, one_mul], apply norm_nonneg end theorem is_bdd_linear_map_add [instance] {V W : Type} [normed_vector_space V] [normed_vector_space W] (f g : V β†’ W) [Hbf : is_bdd_linear_map f] [Hbg : is_bdd_linear_map g] : is_bdd_linear_map (Ξ» x, f x + g x) := begin fapply is_bdd_linear_map.mk, all_goals intros, {rewrite [hom_add f, hom_add g],-- (this takes 4 seconds: rewrite [2 hom_add]) simp}, {rewrite [hom_smul f, hom_smul g, smul_left_distrib]}, {exact is_bdd_linear_map.op_norm _ _ f + is_bdd_linear_map.op_norm _ _ g}, {apply add_pos, repeat apply is_bdd_linear_map.op_norm_pos}, {apply le.trans, apply norm_triangle, rewrite right_distrib, apply add_le_add, repeat apply is_bdd_linear_map.op_norm_bound} end theorem is_bdd_linear_map_smul [instance] {V W : Type} [normed_vector_space V] [normed_vector_space W] (f : V β†’ W) (c : ℝ) [Hbf : is_bdd_linear_map f] : is_bdd_linear_map (Ξ» x, c β€’ f x) := begin apply @decidable.cases_on (c = 0), exact _, {intro Hcz, rewrite Hcz, have Hfe : (Ξ» x : V, (0 : ℝ) β€’ f x) = (Ξ» x : V, 0), from funext (Ξ» x, !zero_smul), rewrite Hfe, apply is_bdd_linear_map_zero}, intro Hcnz, fapply is_bdd_linear_map.mk, all_goals intros, {rewrite [hom_add f, smul_left_distrib]}, {rewrite [hom_smul f, -*mul_smul, {c*r}mul.comm]}, {exact (abs c) * is_bdd_linear_map.op_norm _ _ f}, {have Hpos : abs c > 0, from abs_pos_of_ne_zero Hcnz, apply mul_pos, assumption, apply is_bdd_linear_map.op_norm_pos}, {rewrite [norm_smul, mul.assoc], apply mul_le_mul_of_nonneg_left, apply is_bdd_linear_map.op_norm_bound, apply abs_nonneg} end theorem is_bdd_linear_map_neg [instance] {V W : Type} [normed_vector_space V] [normed_vector_space W] (f : V β†’ W) [Hbf : is_bdd_linear_map f] : is_bdd_linear_map (Ξ» x, -f x) := begin have H : (Ξ» x : V, -f x) = (Ξ» x : V, (-1 : ℝ) β€’ f x), from funext (Ξ» x, eq.symm !neg_one_smul), rewrite H, apply is_bdd_linear_map_smul end -- this can't be an instance because things loop theorem is_bdd_linear_map_comp {U V W : Type} [normed_vector_space U] [normed_vector_space V] [normed_vector_space W] (f : V β†’ W) (g : U β†’ V) [is_bdd_linear_map f] [is_bdd_linear_map g] : is_bdd_linear_map (Ξ» u, f (g u)) := begin fapply is_bdd_linear_map.mk, all_goals intros, {rewrite [hom_add g, hom_add f]}, {rewrite [hom_smul g, hom_smul f]}, {exact is_bdd_linear_map.op_norm _ _ f * is_bdd_linear_map.op_norm _ _ g}, {apply mul_pos, repeat apply is_bdd_linear_map.op_norm_pos}, {apply le.trans, apply is_bdd_linear_map.op_norm_bound _ _ f, apply le.trans, apply mul_le_mul_of_nonneg_left, apply is_bdd_linear_map.op_norm_bound _ _ g, apply le_of_lt !is_bdd_linear_map.op_norm_pos, rewrite *mul.assoc, apply le.refl} end variables {V W : Type} variables [HV : normed_vector_space V] [HW : normed_vector_space W] include HV HW variable f : V β†’ W variable [Hf : is_bdd_linear_map f] include Hf definition op_norm := is_bdd_linear_map.op_norm _ _ f theorem op_norm_pos : op_norm f > 0 := is_bdd_linear_map.op_norm_pos _ _ f theorem op_norm_bound (v : V) : βˆ₯f vβˆ₯ ≀ (op_norm f) * βˆ₯vβˆ₯ := is_bdd_linear_map.op_norm_bound _ _ f v theorem bdd_linear_map_continuous : continuous f := begin apply continuous_of_forall_continuous_at, intro x, apply normed_vector_space.continuous_at_intro, intro Ξ΅ HΞ΅, existsi Ξ΅ / (op_norm f), split, apply div_pos_of_pos_of_pos HΞ΅ !op_norm_pos, intro x' Hx', rewrite [-hom_sub f], apply lt_of_le_of_lt, apply op_norm_bound f, rewrite [-@mul_div_cancel' _ _ Ξ΅ (op_norm f) (ne_of_gt !op_norm_pos)], apply mul_lt_mul_of_pos_left, exact Hx', apply op_norm_pos end end bdd_lin_op -- define Frechet derivative and basic properties section frechet_deriv variables {V W : Type} variables [HV : normed_vector_space V] [HW : normed_vector_space W] include HV HW definition has_frechet_deriv_at (f A : V β†’ W) [is_bdd_linear_map A] (x : V) := (Ξ» h : V, βˆ₯f (x + h) - f x - A h βˆ₯ / βˆ₯ h βˆ₯) ⟢ 0 [at 0] lemma diff_quot_cts {f A : V β†’ W} [HA : is_bdd_linear_map A] {y : V} (Hf : has_frechet_deriv_at f A y) : continuous_at (Ξ» h, βˆ₯f (y + h) - f y - A hβˆ₯ / βˆ₯hβˆ₯) 0 := begin apply normed_vector_space.continuous_at_intro, intro Ξ΅ HΞ΅, cases normed_vector_space.approaches_at_dest Hf HΞ΅ with Ξ΄ HΞ΄, existsi Ξ΄, split, exact and.left HΞ΄, intro x' Hx', cases em (x' = 0) with Heq Hneq, {rewrite [Heq, norm_zero, div_zero, sub_zero, norm_zero], apply HΞ΅}, {rewrite [norm_zero, div_zero], apply and.right HΞ΄, repeat assumption} end theorem is_bdd_linear_map_of_eq {A B : V β†’ W} [HA : is_bdd_linear_map A] (HAB : A = B) : is_bdd_linear_map B := begin fapply is_bdd_linear_map.mk, all_goals try rewrite -HAB, {apply hom_add}, {apply hom_smul}, {exact op_norm A}, {exact op_norm_pos A}, {rewrite -HAB, apply op_norm_bound} end definition is_frechet_deriv_at_of_eq {f A : V β†’ W} [is_bdd_linear_map A] {x : V} (Hfd : has_frechet_deriv_at f A x) {B : V β†’ W} (HAB : A = B) : @has_frechet_deriv_at _ _ _ _ f B (is_bdd_linear_map_of_eq HAB) x := begin unfold has_frechet_deriv_at, rewrite -HAB, apply Hfd end theorem has_frechet_deriv_at_intro {f A : V β†’ W} [is_bdd_linear_map A] {x : V} (H : βˆ€ ⦃Ρ : ℝ⦄, Ξ΅ > 0 β†’ (βˆƒ Ξ΄ : ℝ, Ξ΄ > 0 ∧ βˆ€ ⦃x' : V⦄, x' β‰  0 ∧ βˆ₯x'βˆ₯ < Ξ΄ β†’ βˆ₯f (x + x') - f x - A x'βˆ₯ / βˆ₯x'βˆ₯ < Ξ΅)) : has_frechet_deriv_at f A x := begin apply normed_vector_space.approaches_at_intro, intros Ξ΅ HΞ΅, cases H HΞ΅ with Ξ΄ HΞ΄, cases HΞ΄ with HΞ΄ HΞ΄', existsi Ξ΄, split, assumption, intros x' Hx'1 Hx'2, show abs (βˆ₯f (x + x') - f x - A x'βˆ₯ / βˆ₯x'βˆ₯ - 0) < Ξ΅, begin have H : βˆ₯f (x + x') - f x - A x'βˆ₯ / βˆ₯x'βˆ₯ β‰₯ 0, from div_nonneg_of_nonneg_of_nonneg !norm_nonneg !norm_nonneg, rewrite [sub_zero, abs_of_nonneg H], apply HΞ΄', split, assumption, rewrite [-sub_zero x'], apply Hx'1 end end theorem has_frechet_deriv_at_elim {f A : V β†’ W} [is_bdd_linear_map A] {x : V} (H : has_frechet_deriv_at f A x) : βˆ€ ⦃Ρ : ℝ⦄, Ξ΅ > 0 β†’ (βˆƒ Ξ΄ : ℝ, Ξ΄ > 0 ∧ βˆ€ ⦃x' : V⦄, x' β‰  0 ∧ βˆ₯x'βˆ₯ < Ξ΄ β†’ βˆ₯f (x + x') - f x - A x'βˆ₯ / βˆ₯x'βˆ₯ < Ξ΅) := begin intros Ξ΅ HΞ΅, cases normed_vector_space.approaches_at_dest H HΞ΅ with Ξ΄ HΞ΄, cases HΞ΄ with HΞ΄ HΞ΄', existsi Ξ΄, split, assumption, intros x' Hx', rewrite [-sub_zero x' at Hx' {2}], have HΞ΄'' : abs (βˆ₯f (x + x') - f x - A x'βˆ₯ / βˆ₯x'βˆ₯ - 0) < Ξ΅, from HΞ΄' (and.right Hx') (and.left Hx'), have Hpos : βˆ₯f (x + x') - f x - A x'βˆ₯ / βˆ₯x'βˆ₯ β‰₯ 0, from div_nonneg_of_nonneg_of_nonneg !norm_nonneg !norm_nonneg, rewrite [sub_zero at HΞ΄'', abs_of_nonneg Hpos at HΞ΄''], assumption end structure frechet_diffable_at [class] (f : V β†’ W) (x : V) := (A : V β†’ W) [HA : is_bdd_linear_map A] (is_fr_der : has_frechet_deriv_at f A x) variables f g : V β†’ W variable x : V definition frechet_deriv_at [Hf : frechet_diffable_at f x] : V β†’ W := frechet_diffable_at.A _ _ f x definition frechet_deriv_at_is_bdd_linear_map [instance] (f : V β†’ W) (x : V) [Hf : frechet_diffable_at f x] : is_bdd_linear_map (frechet_deriv_at f x) := frechet_diffable_at.HA _ _ f x theorem frechet_deriv_spec [Hf : frechet_diffable_at f x] : (Ξ» h : V, βˆ₯f (x + h) - f x - (frechet_deriv_at f x h) βˆ₯ / βˆ₯ h βˆ₯) ⟢ 0 [at 0] := frechet_diffable_at.is_fr_der _ _ f x theorem has_frechet_deriv_at_const (w : W) : has_frechet_deriv_at (Ξ» v : V, w) (Ξ» v : V, 0) x := begin apply normed_vector_space.approaches_at_intro, intros Ξ΅ HΞ΅, existsi 1, split, exact zero_lt_one, intros x' Hx' _, rewrite [2 sub_self, norm_zero], krewrite [zero_div, sub_zero, norm_zero], assumption end theorem has_frechet_deriv_at_id : has_frechet_deriv_at (Ξ» v : V, v) (Ξ» v : V, v) x := begin apply normed_vector_space.approaches_at_intro, intros Ξ΅ HΞ΅, existsi 1, split, exact zero_lt_one, intros x' Hx' _, have x + x' - x - x' = 0, by simp, rewrite [this, norm_zero, zero_div, sub_self, norm_zero], exact HΞ΅ end theorem has_frechet_deriv_at_smul (c : ℝ) {A : V β†’ W} [is_bdd_linear_map A] (Hf : has_frechet_deriv_at f A x) : has_frechet_deriv_at (Ξ» y, c β€’ f y) (Ξ» y, c β€’ A y) x := begin eapply @decidable.cases_on (abs c = 0), exact _, {intro Hc, have Hz : c = 0, from eq_zero_of_abs_eq_zero Hc, have Hfz : (Ξ» y : V, (0 : ℝ) β€’ f y) = (Ξ» y : V, 0), from funext (Ξ» y, !zero_smul), --have Hfz' : (Ξ» x : V, (0 : ℝ) β€’ A x) = (Ξ» x : V, 0), from funext (Ξ» y, !zero_smul), -- rewriting Hfz' produces type-incorrect term rewrite [Hz, Hfz], apply metric_space.approaches_at_intro, intro Ξ΅ HΞ΅, existsi 1, split, exact zero_lt_one, intro x' Hx' _, rewrite [zero_smul, *sub_zero, norm_zero], krewrite [zero_div, dist_self], exact HΞ΅}, intro Hcnz, apply normed_vector_space.approaches_at_intro, intros Ξ΅ HΞ΅, have HΞ΅c : Ξ΅ / abs c > 0, from div_pos_of_pos_of_pos HΞ΅ (lt_of_le_of_ne !abs_nonneg (ne.symm Hcnz)), cases normed_vector_space.approaches_at_dest Hf HΞ΅c with Ξ΄ HΞ΄, cases HΞ΄ with HΞ΄p HΞ΄, existsi Ξ΄, split, assumption, intro x' Hx' _, show abs ((βˆ₯c β€’ f (x + x') - c β€’ f x - c β€’ A x'βˆ₯ / βˆ₯x'βˆ₯ - 0)) < Ξ΅, begin rewrite [sub_zero, -2 smul_sub_left_distrib, norm_smul], krewrite mul_div_assoc, rewrite [abs_mul, abs_abs, -{Ξ΅}mul_div_cancel' Hcnz], apply mul_lt_mul_of_pos_left, have HΞ΄' : abs (βˆ₯f (x + x') - f x - A x'βˆ₯ / βˆ₯x'βˆ₯ - 0) < Ξ΅ / abs c, from HΞ΄ Hx' a, rewrite sub_zero at HΞ΄', apply HΞ΄', apply lt_of_le_of_ne, apply abs_nonneg, apply ne.symm, apply Hcnz end end theorem has_frechet_deriv_at_neg {A : V β†’ W} [is_bdd_linear_map A] (Hf : has_frechet_deriv_at f A x) : has_frechet_deriv_at (Ξ» y, - f y) (Ξ» y, - A y) x := begin apply has_frechet_deriv_at_intro, intros Ξ΅ HΞ΅, cases has_frechet_deriv_at_elim Hf HΞ΅ with Ξ΄ HΞ΄, existsi Ξ΄, split, exact and.left HΞ΄, intro x' Hx', rewrite [-norm_neg, neg_sub, sub_neg_eq_add, sub_add_eq_sub_sub, sub_neg_eq_add, add_sub_assoc, add.comm, -sub_eq_add_neg], apply and.right HΞ΄, assumption end theorem has_frechet_deriv_at_add (A B : V β†’ W) [is_bdd_linear_map A] [is_bdd_linear_map B] (Hf : has_frechet_deriv_at f A x) (Hg : has_frechet_deriv_at g B x) : has_frechet_deriv_at (Ξ» y, f y + g y) (Ξ» y, A y + B y) x := begin have Hle : βˆ€ h, βˆ₯f (x + h) + g (x + h) - (f x + g x) - (A h + B h)βˆ₯ / βˆ₯hβˆ₯ ≀ βˆ₯f (x + h) - f x - A hβˆ₯ / βˆ₯hβˆ₯ + βˆ₯g (x + h) - g x - B hβˆ₯ / βˆ₯hβˆ₯, begin intro h, cases em (βˆ₯hβˆ₯ > 0) with Hh Hh, krewrite div_add_div_same, apply div_le_div_of_le_of_pos, have Hfeq : f (x + h) + g (x + h) - (f x + g x) - (A h + B h) = (f (x + h) - f x - A h) + (g (x + h) - g x - B h), by simp, rewrite Hfeq, apply norm_triangle, exact Hh, have Hhe : βˆ₯hβˆ₯ = 0, from eq_of_le_of_ge (le_of_not_gt Hh) !norm_nonneg, krewrite [Hhe, *div_zero, zero_add], eapply le.refl end, have Hlimge : (Ξ» h, βˆ₯f (x + h) - f x - A hβˆ₯ / βˆ₯hβˆ₯ + βˆ₯g (x + h) - g x - B hβˆ₯ / βˆ₯hβˆ₯) ⟢ 0 [at 0], begin rewrite [-zero_add 0], apply add_approaches, apply Hf, apply Hg end, have Hlimle : (Ξ» (h : V), (0 : ℝ)) ⟢ 0 [at 0], by apply approaches_constant, apply approaches_squeeze Hlimle Hlimge, apply filter.eventually_of_forall, intro y, apply div_nonneg_of_nonneg_of_nonneg, repeat apply norm_nonneg, apply filter.eventually_of_forall, apply Hle end open topology theorem continuous_at_of_diffable_at [Hf : frechet_diffable_at f x] : continuous_at f x := begin apply normed_vector_space.continuous_at_intro, intros Ξ΅ HΞ΅, note Hfds := normed_vector_space.approaches_at_dest (frechet_deriv_spec f x) HΞ΅, cases Hfds with Ξ΄ HΞ΄, cases HΞ΄ with HΞ΄ HΞ΄', existsi min Ξ΄ ((Ξ΅ / 2) / (Ξ΅ + op_norm (frechet_deriv_at f x))), split, {apply lt_min, exact HΞ΄, repeat apply div_pos_of_pos_of_pos, exact HΞ΅, apply two_pos, apply add_pos HΞ΅ !op_norm_pos}, {intro x' Hx', cases em (x' - x = 0) with Heq Hneq, rewrite [eq_of_sub_eq_zero Heq, sub_self, norm_zero], assumption, have Hx'x : x' - x β‰  0 ∧ βˆ₯(x' - x) - 0βˆ₯ < Ξ΄, from and.intro Hneq begin rewrite sub_zero, apply lt_of_lt_of_le, apply Hx', apply min_le_left end, have Hx'xp : βˆ₯x' - xβˆ₯ > 0, from norm_pos_of_ne_zero Hneq, have HΞ΄'' : abs (βˆ₯f (x + (x' - x)) - f x - frechet_deriv_at f x (x' - x)βˆ₯ / βˆ₯x' - xβˆ₯ - 0) < Ξ΅, from HΞ΄' (and.right Hx'x) (and.left Hx'x), have Hnn : βˆ₯f (x + (x' - x)) - f x - frechet_deriv_at f x (x' - x)βˆ₯ / βˆ₯x' - xβˆ₯ β‰₯ 0, from div_nonneg_of_nonneg_of_nonneg !norm_nonneg !norm_nonneg, rewrite [sub_zero at HΞ΄'', abs_of_nonneg Hnn at HΞ΄'', add.comm at HΞ΄'', sub_add_cancel at HΞ΄''], note H1 := lt_mul_of_div_lt_of_pos Hx'xp HΞ΄'', have H2 : f x' - f x = f x' - f x - frechet_deriv_at f x (x' - x) + frechet_deriv_at f x (x' - x), by rewrite sub_add_cancel, --by simp, (simp takes .5 seconds to do this!) rewrite H2, apply lt_of_le_of_lt, apply norm_triangle, apply lt.trans, apply add_lt_add_of_lt_of_le, apply H1, apply op_norm_bound (!frechet_deriv_at), rewrite [-add_halves Ξ΅ at {2}], apply add_lt_add, let on := op_norm (frechet_deriv_at f x), exact calc Ξ΅ * βˆ₯x' - xβˆ₯ < Ξ΅ * min Ξ΄ ((Ξ΅ / 2) / (Ξ΅ + on)) : mul_lt_mul_of_pos_left Hx' HΞ΅ ... ≀ Ξ΅ * ((Ξ΅ / 2) / (Ξ΅ + on)) : mul_le_mul_of_nonneg_left !min_le_right (le_of_lt HΞ΅) ... < Ξ΅ / 2 : mul_div_self_add_lt (div_pos_of_pos_of_pos HΞ΅ two_pos) HΞ΅ !op_norm_pos, exact calc on * βˆ₯x' - xβˆ₯ < on * min Ξ΄ ((Ξ΅ / 2) / (Ξ΅ + on)) : mul_lt_mul_of_pos_left Hx' !op_norm_pos ... ≀ on * ((Ξ΅ / 2) / (Ξ΅ + on)) : mul_le_mul_of_nonneg_left !min_le_right (le_of_lt !op_norm_pos) ... < Ξ΅ / 2 : mul_div_add_self_lt (div_pos_of_pos_of_pos HΞ΅ two_pos) HΞ΅ !op_norm_pos} end theorem continuous_at_of_has_frechet_deriv_at {A : V β†’ W} [is_bdd_linear_map A] (H : has_frechet_deriv_at f A x) : continuous_at f x := begin apply @continuous_at_of_diffable_at, existsi A, exact H end end frechet_deriv section comp lemma div_mul_div_cancel {A : Type} [field A] (a b : A) {c : A} (Hc : c β‰  0) : (a / c) * (c / b) = a / b := by rewrite [-mul_div_assoc, div_mul_cancel _ Hc] lemma div_add_eq_add_mul_div {A : Type} [field A] (a b c : A) (Hb : b β‰  0) : (a / b) + c = (a + c * b) / b := by rewrite [-div_add_div_same, mul_div_cancel _ Hb] -- I'm not sure why smul_approaches doesn't unify where I use this? private lemma real_limit_helper {U : Type} [normed_vector_space U] {f : U β†’ ℝ} {a : ℝ} {x : U} (Hf : f ⟢ a [at x]) (c : ℝ) : (Ξ» y, c * f y) ⟢ c * a [at x] := begin apply smul_approaches, exact Hf end variables {U V W : Type} variables [HU : normed_vector_space U] [HV : normed_vector_space V] [HW : normed_vector_space W] variables {f : V β†’ W} {g : U β†’ V} variables {A : V β†’ W} {B : U β†’ V} variables [HA : is_bdd_linear_map A] [HB : is_bdd_linear_map B] variable {x : U} include HU HV HW HA HB -- this takes 2 seconds without clearing the contexts before simp theorem has_frechet_deriv_at_comp (Hg : has_frechet_deriv_at g B x) (Hf : has_frechet_deriv_at f A (g x)) : @has_frechet_deriv_at _ _ _ _ (Ξ» y, f (g y)) (Ξ» y, A (B y)) !is_bdd_linear_map_comp x := begin unfold has_frechet_deriv_at, note Hf' := has_frechet_deriv_at_elim Hf, note Hg' := has_frechet_deriv_at_elim Hg, have H : βˆ€ h, f (g (x + h)) - f (g x) - A (B h) = (A (g (x + h) - g x - B h)) + (-f (g x) + f (g (x + h)) + A (g x - g (x + h))), begin intro; rewrite [3 hom_sub A], clear [Hf, Hg, Hf', Hg'], simp end, -- .5 seconds for simp have H' : (Ξ» h, βˆ₯f (g (x + h)) - f (g x) - A (B h)βˆ₯ / βˆ₯hβˆ₯) = (Ξ» h, βˆ₯(A (g (x + h) - g x - B h)) + (-f (g x) + f (g (x + h)) + A (g x - g (x + h)))βˆ₯ / βˆ₯hβˆ₯), from funext (Ξ» h, by rewrite H), rewrite H', clear [H, H'], apply approaches_squeeze, -- show the limit holds by bounding it by something that vanishes {apply approaches_constant}, rotate 1, {apply filter.eventually_of_forall, intro, apply div_nonneg_of_nonneg_of_nonneg, repeat apply norm_nonneg}, {apply filter.eventually_of_forall, intro, apply div_le_div_of_le_of_nonneg, apply norm_triangle, apply norm_nonneg}, have H : (Ξ» (y : U), (βˆ₯A (g (x + y) - g x - B y)βˆ₯ + βˆ₯-f (g x) + f (g (x + y)) + A (g x - g (x + y))βˆ₯) / βˆ₯yβˆ₯) = (Ξ» (y : U), (βˆ₯A (g (x + y) - g x - B y)βˆ₯ / βˆ₯yβˆ₯ + βˆ₯-f (g x) + f (g (x + y)) + A (g x - g (x + y))βˆ₯ / βˆ₯yβˆ₯)), from funext (Ξ» y, by rewrite [div_add_div_same]), rewrite [H, -zero_add 0], -- the function is a sum of two things that both vanish clear H, apply add_approaches, {apply approaches_squeeze, -- show the lhs vanishes by squeezing it again {apply approaches_constant}, rotate 1, {apply filter.eventually_of_forall, intro, apply div_nonneg_of_nonneg_of_nonneg, repeat apply norm_nonneg}, {apply filter.eventually_of_forall, intro y, show βˆ₯A (g (x + y) - g x - B y)βˆ₯ / βˆ₯yβˆ₯ ≀ op_norm A * (βˆ₯(g (x + y) - g x - B y)βˆ₯ / βˆ₯yβˆ₯), begin rewrite -mul_div_assoc, apply div_le_div_of_le_of_nonneg, apply op_norm_bound A, apply norm_nonneg end}, {rewrite [-mul_zero (op_norm A)], apply real_limit_helper, apply Hg}}, -- we have shown the lhs vanishes. now the rhs {have H : βˆ€ y, (βˆ₯-f (g x) + f (g (x + y)) + A (g x - g (x + y))βˆ₯ / βˆ₯yβˆ₯) = ((βˆ₯(f (g (x + y)) - f (g x)) - A (g (x + y) - g x) βˆ₯ / βˆ₯g (x + y) - g xβˆ₯) * (βˆ₯g (x + y) - g xβˆ₯ / βˆ₯yβˆ₯)), begin intro, cases em (g (x + y) - g x = 0) with Heq Hneq, {note Heq' := eq_of_sub_eq_zero Heq, rewrite [Heq', neg_add_eq_sub, *sub_self, hom_zero A, add_zero, *norm_zero, div_zero, zero_div]}, {rewrite [div_mul_div_cancel _ _ (norm_ne_zero_of_ne_zero Hneq), *sub_eq_add_neg, -hom_neg A], clear [Hf, Hg, Hf', Hg', Hneq], simp} --(.5 seconds) end, apply approaches_squeeze, -- again, by squeezing {apply approaches_constant}, rotate 1, {apply filter.eventually_of_forall, intro, apply div_nonneg_of_nonneg_of_nonneg, repeat apply norm_nonneg}, {apply filter.eventually_of_forall, intro y, rewrite H, apply mul_le_mul_of_nonneg_left, {show βˆ₯g (x + y) - g xβˆ₯ / βˆ₯yβˆ₯ ≀ βˆ₯g (x + y) - g x - B yβˆ₯ / βˆ₯yβˆ₯ + op_norm B, begin cases em (y = 0) with Heq Hneq, {rewrite [Heq, norm_zero, *div_zero, zero_add], apply le_of_lt, apply op_norm_pos}, rewrite [div_add_eq_add_mul_div _ _ _ (norm_ne_zero_of_ne_zero Hneq)], apply div_le_div_of_le_of_nonneg, apply le.trans, rotate 1, apply add_le_add_left, apply op_norm_bound, apply norm_nonneg, rewrite [-neg_add_cancel_right (g (x + y) - g x) (B y) at {1}, -sub_eq_add_neg], apply norm_triangle end}, {apply div_nonneg_of_nonneg_of_nonneg, repeat apply norm_nonneg}}, -- now to show the bounding function vanishes. it is a product of a vanishing function and a convergent one apply mul_approaches_zero_of_approaches_zero_of_approaches, {have H' : (Ξ» (y : U), βˆ₯f (g (x + y)) - f (g x) - A (g (x + y) - g x)βˆ₯ / βˆ₯g (x + y) - g xβˆ₯) = (Ξ» (y : U), βˆ₯f (g x + (g (x + y) - g x)) - f (g x) - A (g (x + y) - g x)βˆ₯ / βˆ₯g (x + y) - g xβˆ₯), from funext (Ξ» y, by rewrite [add.comm (g x), sub_add_cancel]), -- first, show lhs vanishes rewrite H', have Hgcts : continuous_at (Ξ» y, g (x + y) - g x) 0, begin apply normed_vector_space.continuous_at_intro, intro Ξ΅ HΞ΅, cases normed_vector_space.continuous_at_dest (continuous_at_of_has_frechet_deriv_at g x Hg) _ HΞ΅ with Ξ΄ HΞ΄, existsi Ξ΄, split, exact and.left HΞ΄, intro x' Hx', rewrite [add_zero, sub_self], rewrite sub_zero, apply and.right HΞ΄, have (x + x') - x = x' - 0, begin clear [Hg, Hf, Hf', Hg', H, H', HΞ΄, Hx'], simp end, -- (.6 seconds w/o clear, .1 with) rewrite this, apply Hx' end, have Hfcts : continuous_at (Ξ» (x' : V), βˆ₯f (g x + x') - f (g x) - A x'βˆ₯ / βˆ₯x'βˆ₯) (g (x + 0) - g x), begin rewrite [add_zero, sub_self], apply diff_quot_cts, exact Hf end, have Heqz : βˆ₯f (g x + (g (x + 0) - g x)) - f (g x) - A (g (x + 0) - g x)βˆ₯ / βˆ₯g (x + 0) - g xβˆ₯ = 0, by rewrite [*add_zero, sub_self, norm_zero, div_zero], apply @tendsto_comp _ _ _ (Ξ» y, g (x + y) - g x), apply tendsto_inf_left, apply tendsto_at_of_continuous_at Hgcts, note Hfcts' := tendsto_at_of_continuous_at Hfcts, rewrite Heqz at Hfcts', exact Hfcts'}, -- finally, show rhs converges to op_norm B {apply add_approaches, apply Hg, apply approaches_constant}} end end comp end analysis
7b43a1c27b0592289c555b6e2b25ca4a7a0c4e87
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Data/Json/Basic.lean
7bae4d83ec2915abb1f9de2f66183b69fbcc5b23
[ "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
6,482
lean
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Marc Huisinga -/ import Std.Data.RBTree namespace Lean -- mantissa * 10^-exponent structure JsonNumber where mantissa : Int exponent : Nat deriving DecidableEq namespace JsonNumber protected def fromNat (n : Nat) : JsonNumber := ⟨n, 0⟩ protected def fromInt (n : Int) : JsonNumber := ⟨n, 0⟩ instance : Coe Nat JsonNumber := ⟨JsonNumber.fromNat⟩ instance : Coe Int JsonNumber := ⟨JsonNumber.fromInt⟩ private partial def countDigits (n : Nat) : Nat := let rec loop (n digits : Nat) : Nat := if n ≀ 9 then digits else loop (n/10) (digits+1) loop n 1 -- convert mantissa * 10^-exponent to 0.mantissa * 10^exponent protected def normalize : JsonNumber β†’ Int Γ— Nat Γ— Int | ⟨m, e⟩ => do if m = 0 then (0, 0, 0) else let sign : Int := if m > 0 then 1 else -1 let mut mAbs := m.natAbs let nDigits := countDigits mAbs -- eliminate trailing zeros for _ in [0:nDigits] do if mAbs % 10 = 0 then mAbs := mAbs / 10 else break (sign, mAbs, -(e : Int) + nDigits) -- todo (Dany): We should have an Ordering version of this. def lt (a b : JsonNumber) : Bool := let (as, am, ae) := a.normalize let (bs, bm, be) := b.normalize match (as, bs) with | (-1, 1) => true | (1, -1) => false | _ => let ((am, ae), (bm, be)) := if as = -1 && bs = -1 then ((bm, be), (am, ae)) else ((am, ae), (bm, be)) let amDigits := countDigits am let bmDigits := countDigits bm -- align the mantissas let (am, bm) := if amDigits < bmDigits then (am * 10^(bmDigits - amDigits), bm) else (am, bm * 10^(amDigits - bmDigits)) if ae < be then true else if ae > be then false else am < bm def ltProp : LT JsonNumber := ⟨fun a b => lt a b = true⟩ instance : LT JsonNumber := ltProp instance (a b : JsonNumber) : Decidable (a < b) := inferInstanceAs (Decidable (lt a b = true)) instance : Ord JsonNumber where compare x y := if x < y then Ordering.lt else if x > y then Ordering.gt else Ordering.eq protected def toString : JsonNumber β†’ String | ⟨m, 0⟩ => m.repr | ⟨m, e⟩ => let sign := if m β‰₯ 0 then "" else "-" let m := m.natAbs -- if there are too many zeroes after the decimal, we -- use exponents to compress the representation. -- this is mostly done for memory usage reasons: -- the size of the representation would otherwise -- grow exponentially in the value of exponent. let exp : Int := 9 + countDigits m - (e : Int) let exp := if exp < 0 then exp else 0 let e' := (10 : Int) ^ (e - exp.natAbs) let left := (m / e').repr let right := e' + m % e' |>.repr.toSubstring.drop 1 |>.dropRightWhile (fun c => c = '0') |>.toString let exp := if exp = 0 then "" else "e" ++ exp.repr s!"{sign}{left}.{right}{exp}" -- shift a JsonNumber by a specified amount of places to the left protected def shiftl : JsonNumber β†’ Nat β†’ JsonNumber -- if s ≀ e, then 10 ^ (s - e) = 1, and hence the mantissa remains unchanged. -- otherwise, the expression pads the mantissa with zeroes -- to accomodate for the remaining places to shift. | ⟨m, e⟩, s => ⟨m * (10 ^ (s - e) : Nat), e - s⟩ -- shift a JsonNumber by a specified amount of places to the right protected def shiftr : JsonNumber β†’ Nat β†’ JsonNumber | ⟨m, e⟩, s => ⟨m, e + s⟩ instance : ToString JsonNumber := ⟨JsonNumber.toString⟩ instance : Repr JsonNumber where reprPrec | ⟨m, e⟩, _ => Std.Format.bracket "⟨" (repr m ++ "," ++ repr e) "⟩" end JsonNumber def strLt (a b : String) := Decidable.decide (a < b) open Std (RBNode RBNode.leaf) inductive Json where | null | bool (b : Bool) | num (n : JsonNumber) | str (s : String) | arr (elems : Array Json) -- uses RBNode instead of RBMap because RBMap is a def -- and thus currently cannot be used to define a type that -- is recursive in one of its parameters | obj (kvPairs : RBNode String (fun _ => Json)) deriving Inhabited namespace Json -- HACK(Marc): temporary ugliness until we can use RBMap for JSON objects def mkObj (o : List (String Γ— Json)) : Json := obj $ do let mut kvPairs := RBNode.leaf for ⟨k, v⟩ in o do kvPairs := kvPairs.insert compare k v kvPairs instance : Coe Nat Json := ⟨fun n => Json.num n⟩ instance : Coe Int Json := ⟨fun n => Json.num n⟩ instance : Coe String Json := ⟨Json.str⟩ instance : Coe Bool Json := ⟨Json.bool⟩ def isNull : Json -> Bool | null => true | _ => false def getObj? : Json β†’ Except String (RBNode String (fun _ => Json)) | obj kvs => kvs | _ => throw "object expected" def getArr? : Json β†’ Except String (Array Json) | arr a => a | _ => throw "array expected" def getStr? : Json β†’ Except String String | str s => s | _ => throw "String expected" def getNat? : Json β†’ Except String Nat | (n : Nat) => n | _ => throw "Natural number expected" def getInt? : Json β†’ Except String Int | (i : Int) => i | _ => throw "Integer expected" def getBool? : Json β†’ Except String Bool | (b : Bool) => b | _ => throw "Bool expected" def getNum? : Json β†’ Except String JsonNumber | num n => n | _ => throw "number expected" def getObjVal? : Json β†’ String β†’ Except String Json | obj kvs, k => match kvs.find compare k with | some v => v | none => throw s!"property not found: {k}" | _ , _ => throw "object expected" def getArrVal? : Json β†’ Nat β†’ Except String Json | arr a, i => match a.get? i with | some v => v | none => throw s!"index out of bounds: {i}" | _ , _ => throw "array expected" def getObjValD (j : Json) (k : String) : Json := (j.getObjVal? k).toOption.getD null def setObjVal! : Json β†’ String β†’ Json β†’ Json | obj kvs, k, v => obj <| kvs.insert compare k v | j , _, _ => panic! "Json.setObjVal!: not an object: {j}" inductive Structured where | arr (elems : Array Json) | obj (kvPairs : RBNode String (fun _ => Json)) instance : Coe (Array Json) Structured := ⟨Structured.arr⟩ instance : Coe (RBNode String (fun _ => Json)) Structured := ⟨Structured.obj⟩ end Json end Lean
3cbe8b5f8f6cdbe57fd7e3bfcbece4fb96273ba2
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/number_theory/dioph.lean
fe4a89d6d0409fe38ade10f42aa15a42c5080f01
[ "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
28,071
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.fin.fin2 import data.pfun import data.vector3 import number_theory.pell /-! # Diophantine functions and Matiyasevic's theorem Hilbert's tenth problem asked whether there exists an algorithm which for a given integer polynomial determines whether this polynomial has integer solutions. It was answered in the negative in 1970, the final step being completed by Matiyasevic who showed that the power function is Diophantine. Here a function is called Diophantine if its graph is Diophantine as a set. A subset `S βŠ† β„• ^ Ξ±` in turn is called Diophantine if there exists an integer polynomial on `Ξ± βŠ• Ξ²` such that `v ∈ S` iff there exists `t : β„•^Ξ²` with `p (v, t) = 0`. ## Main definitions * `is_poly`: a predicate stating that a function is a multivariate integer polynomial. * `poly`: the type of multivariate integer polynomial functions. * `dioph`: a predicate stating that a set `S βŠ† β„•^Ξ±` is Diophantine, i.e. that * `dioph_fn`: a predicate on a function stating that it is Diophantine in the sense that its graph is Diophantine as a set. ## Main statements * `pell_dioph` states that solutions to Pell's equation form a Diophantine set. * `pow_dioph` states that the power function is Diophantine, a version of Matiyasevic's theorem. ## References * [M. Carneiro, _A Lean formalization of Matiyasevic's theorem_][carneiro2018matiyasevic] * [M. Davis, _Hilbert's tenth problem is unsolvable_][MR317916] ## Tags Matiyasevic's theorem, Hilbert's tenth problem ## TODO * Finish the solution of Hilbert's tenth problem. * Connect `poly` to `mv_polynomial` -/ universe u open nat function namespace int lemma eq_nat_abs_iff_mul (x n) : nat_abs x = n ↔ (x - n) * (x + n) = 0 := begin refine iff.trans _ mul_eq_zero.symm, refine iff.trans _ (or_congr sub_eq_zero add_eq_zero_iff_eq_neg).symm, exact ⟨λe, by rw ← e; apply nat_abs_eq, Ξ»o, by cases o; subst x; simp [nat_abs_of_nat]⟩ end end int open fin2 /-- `list_all p l` is equivalent to `βˆ€ a ∈ l, p a`, but unfolds directly to a conjunction, i.e. `list_all p [0, 1, 2] = p 0 ∧ p 1 ∧ p 2`. -/ @[simp] def list_all {Ξ±} (p : Ξ± β†’ Prop) : list Ξ± β†’ Prop | [] := true | (x :: []) := p x | (x :: l) := p x ∧ list_all l @[simp] theorem list_all_cons {Ξ±} (p : Ξ± β†’ Prop) (x : Ξ±) : βˆ€ (l : list Ξ±), list_all p (x :: l) ↔ p x ∧ list_all p l | [] := (and_true _).symm | (x :: l) := iff.rfl theorem list_all_iff_forall {Ξ±} (p : Ξ± β†’ Prop) : βˆ€ (l : list Ξ±), list_all p l ↔ βˆ€ x ∈ l, p x | [] := (iff_true_intro $ list.ball_nil _).symm | (x :: l) := by rw [list.ball_cons, ← list_all_iff_forall l]; simp theorem list_all.imp {Ξ±} {p q : Ξ± β†’ Prop} (h : βˆ€ x, p x β†’ q x) : βˆ€ {l : list Ξ±}, list_all p l β†’ list_all q l | [] := id | (x :: l) := by simpa using and.imp (h x) list_all.imp @[simp] theorem list_all_map {Ξ± Ξ²} {p : Ξ² β†’ Prop} (f : Ξ± β†’ Ξ²) {l : list Ξ±} : list_all p (l.map f) ↔ list_all (p ∘ f) l := by induction l; simp * theorem list_all_congr {Ξ±} {p q : Ξ± β†’ Prop} (h : βˆ€ x, p x ↔ q x) {l : list Ξ±} : list_all p l ↔ list_all q l := ⟨list_all.imp (Ξ»x, (h x).1), list_all.imp (Ξ»x, (h x).2)⟩ instance decidable_list_all {Ξ±} (p : Ξ± β†’ Prop) [decidable_pred p] (l : list Ξ±) : decidable (list_all p l) := decidable_of_decidable_of_iff (by apply_instance) (list_all_iff_forall _ _).symm /- poly -/ /-- A predicate asserting that a function is a multivariate integer polynomial. (We are being a bit lazy here by allowing many representations for multiplication, rather than only allowing monomials and addition, but the definition is equivalent and this is easier to use.) -/ inductive is_poly {Ξ±} : ((Ξ± β†’ β„•) β†’ β„€) β†’ Prop | proj : βˆ€ i, is_poly (Ξ»x : Ξ± β†’ β„•, x i) | const : Ξ  (n : β„€), is_poly (Ξ»x : Ξ± β†’ β„•, n) | sub : Ξ  {f g : (Ξ± β†’ β„•) β†’ β„€}, is_poly f β†’ is_poly g β†’ is_poly (Ξ»x, f x - g x) | mul : Ξ  {f g : (Ξ± β†’ β„•) β†’ β„€}, is_poly f β†’ is_poly g β†’ is_poly (Ξ»x, f x * g x) /-- The type of multivariate integer polynomials -/ def poly (Ξ± : Type u) := {f : (Ξ± β†’ β„•) β†’ β„€ // is_poly f} namespace poly section parameter {Ξ± : Type u} instance : has_coe_to_fun (poly Ξ±) (Ξ» _, (Ξ± β†’ β„•) β†’ β„€) := ⟨λ f, f.1⟩ /-- The underlying function of a `poly` is a polynomial -/ lemma isp (f : poly Ξ±) : is_poly f := f.2 /-- Extensionality for `poly Ξ±` -/ lemma ext {f g : poly Ξ±} (e : βˆ€x, f x = g x) : f = g := subtype.eq (funext e) /-- Construct a `poly` given an extensionally equivalent `poly`. -/ def subst (f : poly Ξ±) (g : (Ξ± β†’ β„•) β†’ β„€) (e : βˆ€x, f x = g x) : poly Ξ± := ⟨g, by rw ← (funext e : coe_fn f = g); exact f.isp⟩ @[simp] theorem subst_eval (f g e x) : subst f g e x = g x := rfl /-- The `i`th projection function, `x_i`. -/ def proj (i) : poly Ξ± := ⟨_, is_poly.proj i⟩ @[simp] theorem proj_eval (i x) : proj i x = x i := rfl /-- The constant function with value `n : β„€`. -/ def const (n) : poly Ξ± := ⟨_, is_poly.const n⟩ @[simp] theorem const_eval (n x) : const n x = n := rfl /-- The zero polynomial -/ def zero : poly Ξ± := const 0 instance : has_zero (poly Ξ±) := ⟨poly.zero⟩ @[simp] theorem zero_eval (x) : (0 : poly Ξ±) x = 0 := rfl /-- The zero polynomial -/ def one : poly Ξ± := const 1 instance : has_one (poly Ξ±) := ⟨poly.one⟩ @[simp] theorem one_eval (x) : (1 : poly Ξ±) x = 1 := rfl /-- Subtraction of polynomials -/ def sub : poly Ξ± β†’ poly Ξ± β†’ poly Ξ± | ⟨f, pf⟩ ⟨g, pg⟩ := ⟨_, is_poly.sub pf pg⟩ instance : has_sub (poly Ξ±) := ⟨poly.sub⟩ @[simp] theorem sub_eval : Ξ  (f g x), (f - g : poly Ξ±) x = f x - g x | ⟨f, pf⟩ ⟨g, pg⟩ x := rfl /-- Negation of a polynomial -/ def neg (f : poly Ξ±) : poly Ξ± := 0 - f instance : has_neg (poly Ξ±) := ⟨poly.neg⟩ @[simp] theorem neg_eval (f x) : (-f : poly Ξ±) x = -f x := show (0-f) x = _, by simp /-- Addition of polynomials -/ def add : poly Ξ± β†’ poly Ξ± β†’ poly Ξ± | ⟨f, pf⟩ ⟨g, pg⟩ := subst (⟨f, pf⟩ - -⟨g, pg⟩) _ (Ξ»x, show f x - (0 - g x) = f x + g x, by simp) instance : has_add (poly Ξ±) := ⟨poly.add⟩ @[simp] theorem add_eval : Ξ  (f g x), (f + g : poly Ξ±) x = f x + g x | ⟨f, pf⟩ ⟨g, pg⟩ x := rfl /-- Multiplication of polynomials -/ def mul : poly Ξ± β†’ poly Ξ± β†’ poly Ξ± | ⟨f, pf⟩ ⟨g, pg⟩ := ⟨_, is_poly.mul pf pg⟩ instance : has_mul (poly Ξ±) := ⟨poly.mul⟩ @[simp] theorem mul_eval : Ξ  (f g x), (f * g : poly Ξ±) x = f x * g x | ⟨f, pf⟩ ⟨g, pg⟩ x := rfl instance : comm_ring (poly Ξ±) := by refine_struct { add := ((+) : poly Ξ± β†’ poly Ξ± β†’ poly Ξ±), zero := 0, neg := (has_neg.neg : poly Ξ± β†’ poly Ξ±), mul := ((*)), one := 1, sub := (has_sub.sub), npow := @npow_rec _ ⟨(1 : poly Ξ±)⟩ ⟨(*)⟩, nsmul := @nsmul_rec _ ⟨(0 : poly Ξ±)⟩ ⟨(+)⟩, zsmul := @zsmul_rec _ ⟨(0 : poly Ξ±)⟩ ⟨(+)⟩ ⟨neg⟩ }; intros; try { refl }; refine ext (Ξ» _, _); simp [sub_eq_add_neg, mul_add, mul_left_comm, mul_comm, add_comm, add_assoc] lemma induction {C : poly Ξ± β†’ Prop} (H1 : βˆ€i, C (proj i)) (H2 : βˆ€n, C (const n)) (H3 : βˆ€f g, C f β†’ C g β†’ C (f - g)) (H4 : βˆ€f g, C f β†’ C g β†’ C (f * g)) (f : poly Ξ±) : C f := begin cases f with f pf, induction pf with i n f g pf pg ihf ihg f g pf pg ihf ihg, apply H1, apply H2, apply H3 _ _ ihf ihg, apply H4 _ _ ihf ihg end /-- The sum of squares of a list of polynomials. This is relevant for Diophantine equations, because it means that a list of equations can be encoded as a single equation: `x = 0 ∧ y = 0 ∧ z = 0` is equivalent to `x^2 + y^2 + z^2 = 0`. -/ def sumsq : list (poly Ξ±) β†’ poly Ξ± | [] := 0 | (p::ps) := p*p + sumsq ps theorem sumsq_nonneg (x) : βˆ€ l, 0 ≀ sumsq l x | [] := le_refl 0 | (p::ps) := by rw sumsq; simp [-add_comm]; exact add_nonneg (mul_self_nonneg _) (sumsq_nonneg ps) theorem sumsq_eq_zero (x) : βˆ€ l, sumsq l x = 0 ↔ list_all (Ξ»a : poly Ξ±, a x = 0) l | [] := eq_self_iff_true _ | (p::ps) := by rw [list_all_cons, ← sumsq_eq_zero ps]; rw sumsq; simp [-add_comm]; exact ⟨λ(h : p x * p x + sumsq ps x = 0), have p x = 0, from eq_zero_of_mul_self_eq_zero $ le_antisymm (by rw ← h; have t := add_le_add_left (sumsq_nonneg x ps) (p x * p x); rwa [add_zero] at t) (mul_self_nonneg _), ⟨this, by simp [this] at h; exact h⟩, λ⟨h1, h2⟩, by rw [h1, h2]; refl⟩ end /-- Map the index set of variables, replacing `x_i` with `x_(f i)`. -/ def remap {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (g : poly Ξ±) : poly Ξ² := ⟨λv, g $ v ∘ f, g.induction (Ξ»i, by simp; apply is_poly.proj) (Ξ»n, by simp; apply is_poly.const) (Ξ»f g pf pg, by simp; apply is_poly.sub pf pg) (Ξ»f g pf pg, by simp; apply is_poly.mul pf pg)⟩ @[simp] theorem remap_eval {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (g : poly Ξ±) (v) : remap f g v = g (v ∘ f) := rfl end poly namespace sum /-- combine two functions into a function on the disjoint union -/ def join {Ξ± Ξ² Ξ³} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ³) : Ξ± βŠ• Ξ² β†’ Ξ³ := by {refine sum.rec _ _, exacts [f, g]} end sum local infixr ` βŠ— `:65 := sum.join open sum namespace option /-- Functions from `option` can be combined similarly to `vector.cons` -/ def cons {Ξ± Ξ²} (a : Ξ²) (v : Ξ± β†’ Ξ²) : option Ξ± β†’ Ξ² := by {refine option.rec _ _, exacts [a, v]} notation a :: b := cons a b @[simp] theorem cons_head_tail {Ξ± Ξ²} (v : option Ξ± β†’ Ξ²) : v none :: v ∘ some = v := funext $ Ξ»o, by cases o; refl end option /- dioph -/ /-- A set `S βŠ† β„•^Ξ±` is Diophantine if there exists a polynomial on `Ξ± βŠ• Ξ²` such that `v ∈ S` iff there exists `t : β„•^Ξ²` with `p (v, t) = 0`. -/ def dioph {Ξ± : Type u} (S : set (Ξ± β†’ β„•)) : Prop := βˆƒ {Ξ² : Type u} (p : poly (Ξ± βŠ• Ξ²)), βˆ€ (v : Ξ± β†’ β„•), S v ↔ βˆƒt, p (v βŠ— t) = 0 namespace dioph section variables {Ξ± Ξ² Ξ³ : Type u} theorem ext {S S' : set (Ξ± β†’ β„•)} (d : dioph S) (H : βˆ€v, S v ↔ S' v) : dioph S' := eq.rec d $ show S = S', from set.ext H theorem of_no_dummies (S : set (Ξ± β†’ β„•)) (p : poly Ξ±) (h : βˆ€ (v : Ξ± β†’ β„•), S v ↔ p v = 0) : dioph S := ⟨ulift empty, p.remap inl, Ξ»v, (h v).trans ⟨λh, ⟨λt, empty.rec _ t.down, by simp; rw [ show (v βŠ— Ξ»t:ulift empty, empty.rec _ t.down) ∘ inl = v, from rfl, h]⟩, λ⟨t, ht⟩, by simp at ht; rwa [show (v βŠ— t) ∘ inl = v, from rfl] at ht⟩⟩ lemma inject_dummies_lem (f : Ξ² β†’ Ξ³) (g : Ξ³ β†’ option Ξ²) (inv : βˆ€ x, g (f x) = some x) (p : poly (Ξ± βŠ• Ξ²)) (v : Ξ± β†’ β„•) : (βˆƒt, p (v βŠ— t) = 0) ↔ (βˆƒt, p.remap (inl βŠ— (inr ∘ f)) (v βŠ— t) = 0) := begin simp, refine ⟨λt, _, Ξ»t, _⟩; cases t with t ht, { have : (v βŠ— (0 :: t) ∘ g) ∘ (inl βŠ— inr ∘ f) = v βŠ— t := funext (Ξ»s, by cases s with a b; dsimp [join, (∘)]; try {rw inv}; refl), exact ⟨(0 :: t) ∘ g, by rwa this⟩ }, { have : v βŠ— t ∘ f = (v βŠ— t) ∘ (inl βŠ— inr ∘ f) := funext (Ξ»s, by cases s with a b; refl), exact ⟨t ∘ f, by rwa this⟩ } end theorem inject_dummies {S : set (Ξ± β†’ β„•)} (f : Ξ² β†’ Ξ³) (g : Ξ³ β†’ option Ξ²) (inv : βˆ€ x, g (f x) = some x) (p : poly (Ξ± βŠ• Ξ²)) (h : βˆ€ (v : Ξ± β†’ β„•), S v ↔ βˆƒt, p (v βŠ— t) = 0) : βˆƒ q : poly (Ξ± βŠ• Ξ³), βˆ€ (v : Ξ± β†’ β„•), S v ↔ βˆƒt, q (v βŠ— t) = 0 := ⟨p.remap (inl βŠ— (inr ∘ f)), Ξ»v, (h v).trans $ inject_dummies_lem f g inv _ _⟩ theorem reindex_dioph {S : set (Ξ± β†’ β„•)} : Ξ  (d : dioph S) (f : Ξ± β†’ Ξ²), dioph (Ξ»v, S (v ∘ f)) | ⟨γ, p, pe⟩ f := ⟨γ, p.remap ((inl ∘ f) βŠ— inr), Ξ»v, (pe _).trans $ exists_congr $ Ξ»t, suffices v ∘ f βŠ— t = (v βŠ— t) ∘ (inl ∘ f βŠ— inr), by simp [this], funext $ Ξ»s, by cases s with a b; refl⟩ theorem dioph_list_all (l) (d : list_all dioph l) : dioph (Ξ»v, list_all (Ξ»S : set (Ξ± β†’ β„•), S v) l) := suffices βˆƒ Ξ² (pl : list (poly (Ξ± βŠ• Ξ²))), βˆ€ v, list_all (Ξ»S : set _, S v) l ↔ βˆƒt, list_all (Ξ»p : poly (Ξ± βŠ• Ξ²), p (v βŠ— t) = 0) pl, from let ⟨β, pl, h⟩ := this in ⟨β, poly.sumsq pl, Ξ»v, (h v).trans $ exists_congr $ Ξ»t, (poly.sumsq_eq_zero _ _).symm⟩, begin induction l with S l IH, exact ⟨ulift empty, [], Ξ»v, by simp; exact ⟨λ⟨t⟩, empty.rec _ t, trivial⟩⟩, simp at d, exact let ⟨⟨β, p, pe⟩, dl⟩ := d, ⟨γ, pl, ple⟩ := IH dl in ⟨β βŠ• Ξ³, p.remap (inl βŠ— inr ∘ inl) :: pl.map (Ξ»q, q.remap (inl βŠ— (inr ∘ inr))), Ξ»v, by simp; exact iff.trans (and_congr (pe v) (ple v)) ⟨λ⟨⟨m, hm⟩, ⟨n, hn⟩⟩, ⟨m βŠ— n, by rw [ show (v βŠ— m βŠ— n) ∘ (inl βŠ— inr ∘ inl) = v βŠ— m, from funext $ Ξ»s, by cases s with a b; refl]; exact hm, by { refine list_all.imp (Ξ»q hq, _) hn, dsimp [(∘)], rw [show (Ξ» (x : Ξ± βŠ• Ξ³), (v βŠ— m βŠ— n) ((inl βŠ— Ξ» (x : Ξ³), inr (inr x)) x)) = v βŠ— n, from funext $ Ξ»s, by cases s with a b; refl]; exact hq }⟩, λ⟨t, hl, hr⟩, ⟨⟨t ∘ inl, by rwa [ show (v βŠ— t) ∘ (inl βŠ— inr ∘ inl) = v βŠ— t ∘ inl, from funext $ Ξ»s, by cases s with a b; refl] at hl⟩, ⟨t ∘ inr, by { refine list_all.imp (Ξ»q hq, _) hr, dsimp [(∘)] at hq, rwa [show (Ξ» (x : Ξ± βŠ• Ξ³), (v βŠ— t) ((inl βŠ— Ξ» (x : Ξ³), inr (inr x)) x)) = v βŠ— t ∘ inr, from funext $ Ξ»s, by cases s with a b; refl] at hq }⟩⟩⟩⟩ end theorem and_dioph {S S' : set (Ξ± β†’ β„•)} (d : dioph S) (d' : dioph S') : dioph (Ξ»v, S v ∧ S' v) := dioph_list_all [S, S'] ⟨d, d'⟩ theorem or_dioph {S S' : set (Ξ± β†’ β„•)} : βˆ€ (d : dioph S) (d' : dioph S'), dioph (Ξ»v, S v ∨ S' v) | ⟨β, p, pe⟩ ⟨γ, q, qe⟩ := ⟨β βŠ• Ξ³, p.remap (inl βŠ— inr ∘ inl) * q.remap (inl βŠ— inr ∘ inr), Ξ»v, begin refine iff.trans (or_congr ((pe v).trans _) ((qe v).trans _)) (exists_or_distrib.symm.trans (exists_congr $ Ξ»t, (@mul_eq_zero _ _ _ (p ((v βŠ— t) ∘ (inl βŠ— inr ∘ inl))) (q ((v βŠ— t) ∘ (inl βŠ— inr ∘ inr)))).symm)), exact inject_dummies_lem _ (some βŠ— (Ξ»_, none)) (Ξ»x, rfl) _ _, exact inject_dummies_lem _ ((Ξ»_, none) βŠ— some) (Ξ»x, rfl) _ _, end⟩ /-- A partial function is Diophantine if its graph is Diophantine. -/ def dioph_pfun (f : (Ξ± β†’ β„•) β†’. β„•) := dioph (Ξ»v : option Ξ± β†’ β„•, f.graph (v ∘ some, v none)) /-- A function is Diophantine if its graph is Diophantine. -/ def dioph_fn (f : (Ξ± β†’ β„•) β†’ β„•) := dioph (Ξ»v : option Ξ± β†’ β„•, f (v ∘ some) = v none) theorem reindex_dioph_fn {f : (Ξ± β†’ β„•) β†’ β„•} (d : dioph_fn f) (g : Ξ± β†’ Ξ²) : dioph_fn (Ξ»v, f (v ∘ g)) := reindex_dioph d (functor.map g) theorem ex_dioph {S : set (Ξ± βŠ• Ξ² β†’ β„•)} : dioph S β†’ dioph (Ξ»v, βˆƒx, S (v βŠ— x)) | ⟨γ, p, pe⟩ := ⟨β βŠ• Ξ³, p.remap ((inl βŠ— inr ∘ inl) βŠ— inr ∘ inr), Ξ»v, ⟨λ⟨x, hx⟩, let ⟨t, ht⟩ := (pe _).1 hx in ⟨x βŠ— t, by simp; rw [ show (v βŠ— x βŠ— t) ∘ ((inl βŠ— inr ∘ inl) βŠ— inr ∘ inr) = (v βŠ— x) βŠ— t, from funext $ Ξ»s, by cases s with a b; try {cases a}; refl]; exact ht⟩, λ⟨t, ht⟩, ⟨t ∘ inl, (pe _).2 ⟨t ∘ inr, by simp at ht; rwa [ show (v βŠ— t) ∘ ((inl βŠ— inr ∘ inl) βŠ— inr ∘ inr) = (v βŠ— t ∘ inl) βŠ— t ∘ inr, from funext $ Ξ»s, by cases s with a b; try {cases a}; refl] at ht⟩⟩⟩⟩ theorem ex1_dioph {S : set (option Ξ± β†’ β„•)} : dioph S β†’ dioph (Ξ»v, βˆƒx, S (x :: v)) | ⟨β, p, pe⟩ := ⟨option Ξ², p.remap (inr none :: inl βŠ— inr ∘ some), Ξ»v, ⟨λ⟨x, hx⟩, let ⟨t, ht⟩ := (pe _).1 hx in ⟨x :: t, by simp; rw [ show (v βŠ— x :: t) ∘ (inr none :: inl βŠ— inr ∘ some) = x :: v βŠ— t, from funext $ Ξ»s, by cases s with a b; try {cases a}; refl]; exact ht⟩, λ⟨t, ht⟩, ⟨t none, (pe _).2 ⟨t ∘ some, by simp at ht; rwa [ show (v βŠ— t) ∘ (inr none :: inl βŠ— inr ∘ some) = t none :: v βŠ— t ∘ some, from funext $ Ξ»s, by cases s with a b; try {cases a}; refl] at ht⟩⟩⟩⟩ theorem dom_dioph {f : (Ξ± β†’ β„•) β†’. β„•} (d : dioph_pfun f) : dioph f.dom := cast (congr_arg dioph $ set.ext $ Ξ»v, (pfun.dom_iff_graph _ _).symm) (ex1_dioph d) theorem dioph_fn_iff_pfun (f : (Ξ± β†’ β„•) β†’ β„•) : dioph_fn f = @dioph_pfun Ξ± f := by refine congr_arg dioph (set.ext $ Ξ»v, _); exact pfun.lift_graph.symm theorem abs_poly_dioph (p : poly Ξ±) : dioph_fn (Ξ»v, (p v).nat_abs) := by refine of_no_dummies _ ((p.remap some - poly.proj none) * (p.remap some + poly.proj none)) (Ξ»v, _); apply int.eq_nat_abs_iff_mul theorem proj_dioph (i : Ξ±) : dioph_fn (Ξ»v, v i) := abs_poly_dioph (poly.proj i) theorem dioph_pfun_comp1 {S : set (option Ξ± β†’ β„•)} (d : dioph S) {f} (df : dioph_pfun f) : dioph (Ξ»v : Ξ± β†’ β„•, βˆƒ h : f.dom v, S (f.fn v h :: v)) := ext (ex1_dioph (and_dioph d df)) $ Ξ»v, ⟨λ⟨x, hS, (h: Exists _)⟩, by rw [show (x :: v) ∘ some = v, from funext $ Ξ»s, rfl] at h; cases h with hf h; refine ⟨hf, _⟩; rw [pfun.fn, h]; exact hS, λ⟨x, hS⟩, ⟨f.fn v x, hS, show Exists _, by rw [show (f.fn v x :: v) ∘ some = v, from funext $ Ξ»s, rfl]; exact ⟨x, rfl⟩⟩⟩ theorem dioph_fn_comp1 {S : set (option Ξ± β†’ β„•)} (d : dioph S) {f : (Ξ± β†’ β„•) β†’ β„•} (df : dioph_fn f) : dioph (Ξ»v : Ξ± β†’ β„•, S (f v :: v)) := ext (dioph_pfun_comp1 d (cast (dioph_fn_iff_pfun f) df)) $ Ξ»v, ⟨λ⟨_, h⟩, h, Ξ»h, ⟨trivial, h⟩⟩ end section variables {Ξ± Ξ² Ξ³ : Type} open vector3 open_locale vector3 theorem dioph_fn_vec_comp1 {n} {S : set (vector3 β„• (succ n))} (d : dioph S) {f : (vector3 β„• n) β†’ β„•} (df : dioph_fn f) : dioph (Ξ»v : vector3 β„• n, S (cons (f v) v)) := ext (dioph_fn_comp1 (reindex_dioph d (none :: some)) df) $ Ξ»v, by rw [ show option.cons (f v) v ∘ (cons none some) = f v :: v, from funext $ Ξ»s, by cases s with a b; refl] theorem vec_ex1_dioph (n) {S : set (vector3 β„• (succ n))} (d : dioph S) : dioph (Ξ»v : vector3 β„• n, βˆƒx, S (x :: v)) := ext (ex1_dioph $ reindex_dioph d (none :: some)) $ Ξ»v, exists_congr $ Ξ»x, by rw [ show (option.cons x v) ∘ (cons none some) = x :: v, from funext $ Ξ»s, by cases s with a b; refl] theorem dioph_fn_vec {n} (f : vector3 β„• n β†’ β„•) : dioph_fn f ↔ dioph (Ξ»v : vector3 β„• (succ n), f (v ∘ fs) = v fz) := ⟨λh, reindex_dioph h (fz :: fs), Ξ»h, reindex_dioph h (none :: some)⟩ theorem dioph_pfun_vec {n} (f : vector3 β„• n β†’. β„•) : dioph_pfun f ↔ dioph (Ξ»v : vector3 β„• (succ n), f.graph (v ∘ fs, v fz)) := ⟨λh, reindex_dioph h (fz :: fs), Ξ»h, reindex_dioph h (none :: some)⟩ theorem dioph_fn_compn {Ξ± : Type} : βˆ€ {n} {S : set (Ξ± βŠ• fin2 n β†’ β„•)} (d : dioph S) {f : vector3 ((Ξ± β†’ β„•) β†’ β„•) n} (df : vector_allp dioph_fn f), dioph (Ξ»v : Ξ± β†’ β„•, S (v βŠ— Ξ»i, f i v)) | 0 S d f := Ξ»df, ext (reindex_dioph d (id βŠ— fin2.elim0)) $ Ξ»v, by refine eq.to_iff (congr_arg S $ funext $ Ξ»s, _); {cases s with a b, refl, cases b} | (succ n) S d f := f.cons_elim $ Ξ»f fl, by simp; exact Ξ» df dfl, have dioph (Ξ»v, S (v ∘ inl βŠ— f (v ∘ inl) :: v ∘ inr)), from ext (dioph_fn_comp1 (reindex_dioph d (some ∘ inl βŠ— none :: some ∘ inr)) (reindex_dioph_fn df inl)) $ Ξ»v, by {refine eq.to_iff (congr_arg S $ funext $ Ξ»s, _); cases s with a b, refl, cases b; refl}, have dioph (Ξ»v, S (v βŠ— f v :: Ξ» (i : fin2 n), fl i v)), from @dioph_fn_compn n (Ξ»v, S (v ∘ inl βŠ— f (v ∘ inl) :: v ∘ inr)) this _ dfl, ext this $ Ξ»v, by rw [ show cons (f v) (Ξ» (i : fin2 n), fl i v) = Ξ» (i : fin2 (succ n)), (f :: fl) i v, from funext $ Ξ»s, by cases s with a b; refl] theorem dioph_comp {n} {S : set (vector3 β„• n)} (d : dioph S) (f : vector3 ((Ξ± β†’ β„•) β†’ β„•) n) (df : vector_allp dioph_fn f) : dioph (Ξ»v, S (Ξ»i, f i v)) := dioph_fn_compn (reindex_dioph d inr) df theorem dioph_fn_comp {n} {f : vector3 β„• n β†’ β„•} (df : dioph_fn f) (g : vector3 ((Ξ± β†’ β„•) β†’ β„•) n) (dg : vector_allp dioph_fn g) : dioph_fn (Ξ»v, f (Ξ»i, g i v)) := dioph_comp ((dioph_fn_vec _).1 df) ((Ξ»v, v none) :: Ξ»i v, g i (v ∘ some)) $ by simp; exact ⟨proj_dioph none, (vector_allp_iff_forall _ _).2 $ Ξ»i, reindex_dioph_fn ((vector_allp_iff_forall _ _).1 dg _) _⟩ localized "notation x ` D∧ `:35 y := dioph.and_dioph x y" in dioph localized "notation x ` D∨ `:35 y := dioph.or_dioph x y" in dioph localized "notation `Dβˆƒ`:30 := dioph.vec_ex1_dioph" in dioph localized "prefix `&`:max := of_nat'" in dioph theorem proj_dioph_of_nat {n : β„•} (m : β„•) [is_lt m n] : dioph_fn (Ξ»v : vector3 β„• n, v &m) := proj_dioph &m localized "prefix `D&`:100 := dioph.proj_dioph_of_nat" in dioph theorem const_dioph (n : β„•) : dioph_fn (const (Ξ± β†’ β„•) n) := abs_poly_dioph (poly.const n) localized "prefix `D.`:100 := dioph.const_dioph" in dioph variables {f g : (Ξ± β†’ β„•) β†’ β„•} (df : dioph_fn f) (dg : dioph_fn g) include df dg theorem dioph_comp2 {S : β„• β†’ β„• β†’ Prop} (d : dioph (Ξ»v:vector3 β„• 2, S (v &0) (v &1))) : dioph (Ξ»v, S (f v) (g v)) := dioph_comp d [f, g] (by exact ⟨df, dg⟩) theorem dioph_fn_comp2 {h : β„• β†’ β„• β†’ β„•} (d : dioph_fn (Ξ»v:vector3 β„• 2, h (v &0) (v &1))) : dioph_fn (Ξ»v, h (f v) (g v)) := dioph_fn_comp d [f, g] (by exact ⟨df, dg⟩) theorem eq_dioph : dioph (Ξ»v, f v = g v) := dioph_comp2 df dg $ of_no_dummies _ (poly.proj &0 - poly.proj &1) (Ξ»v, (int.coe_nat_eq_coe_nat_iff _ _).symm.trans ⟨@sub_eq_zero_of_eq β„€ _ (v &0) (v &1), eq_of_sub_eq_zero⟩) localized "infix ` D= `:50 := dioph.eq_dioph" in dioph theorem add_dioph : dioph_fn (Ξ»v, f v + g v) := dioph_fn_comp2 df dg $ abs_poly_dioph (poly.proj &0 + poly.proj &1) localized "infix ` D+ `:80 := dioph.add_dioph" in dioph theorem mul_dioph : dioph_fn (Ξ»v, f v * g v) := dioph_fn_comp2 df dg $ abs_poly_dioph (poly.proj &0 * poly.proj &1) localized "infix ` D* `:90 := dioph.mul_dioph" in dioph theorem le_dioph : dioph (Ξ»v, f v ≀ g v) := dioph_comp2 df dg $ ext (Dβˆƒ2 $ D&1 D+ D&0 D= D&2) (Ξ»v, ⟨λ⟨x, hx⟩, le.intro hx, le.dest⟩) localized "infix ` D≀ `:50 := dioph.le_dioph" in dioph theorem lt_dioph : dioph (Ξ»v, f v < g v) := df D+ (D. 1) D≀ dg localized "infix ` D< `:50 := dioph.lt_dioph" in dioph theorem ne_dioph : dioph (Ξ»v, f v β‰  g v) := ext (df D< dg D∨ dg D< df) $ Ξ»v, ne_iff_lt_or_gt.symm localized "infix ` Dβ‰  `:50 := dioph.ne_dioph" in dioph theorem sub_dioph : dioph_fn (Ξ»v, f v - g v) := dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext (D&1 D= D&0 D+ D&2 D∨ D&1 D≀ D&2 D∧ D&0 D= D.0) $ (vector_all_iff_forall _).1 $ Ξ»x y z, show (y = x + z ∨ y ≀ z ∧ x = 0) ↔ y - z = x, from ⟨λo, begin rcases o with ae | ⟨yz, x0⟩, { rw [ae, add_tsub_cancel_right] }, { rw [x0, tsub_eq_zero_iff_le.mpr yz] } end, Ξ»h, begin subst x, cases le_total y z with yz zy, { exact or.inr ⟨yz, tsub_eq_zero_iff_le.mpr yz⟩ }, { exact or.inl (tsub_add_cancel_of_le zy).symm }, end⟩ localized "infix ` D- `:80 := dioph.sub_dioph" in dioph theorem dvd_dioph : dioph (Ξ»v, f v ∣ g v) := dioph_comp (Dβˆƒ2 $ D&2 D= D&1 D* D&0) [f, g] (by exact ⟨df, dg⟩) localized "infix ` D∣ `:50 := dioph.dvd_dioph" in dioph theorem mod_dioph : dioph_fn (Ξ»v, f v % g v) := have dioph (Ξ»v : vector3 β„• 3, (v &2 = 0 ∨ v &0 < v &2) ∧ βˆƒ (x : β„•), v &0 + v &2 * x = v &1), from (D&2 D= D.0 D∨ D&0 D< D&2) D∧ (Dβˆƒ3 $ D&1 D+ D&3 D* D&0 D= D&2), dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext this $ (vector_all_iff_forall _).1 $ Ξ»z x y, show ((y = 0 ∨ z < y) ∧ βˆƒ c, z + y * c = x) ↔ x % y = z, from ⟨λ⟨h, c, hc⟩, begin rw ← hc; simp; cases h with x0 hl, rw [x0, mod_zero], exact mod_eq_of_lt hl end, Ξ»e, by rw ← e; exact ⟨or_iff_not_imp_left.2 $ Ξ»h, mod_lt _ (nat.pos_of_ne_zero h), x / y, mod_add_div _ _⟩⟩ localized "infix ` D% `:80 := dioph.mod_dioph" in dioph theorem modeq_dioph {h : (Ξ± β†’ β„•) β†’ β„•} (dh : dioph_fn h) : dioph (Ξ»v, f v ≑ g v [MOD h v]) := df D% dh D= dg D% dh localized "notation `D≑` := dioph.modeq_dioph" in dioph theorem div_dioph : dioph_fn (Ξ»v, f v / g v) := have dioph (Ξ»v : vector3 β„• 3, v &2 = 0 ∧ v &0 = 0 ∨ v &0 * v &2 ≀ v &1 ∧ v &1 < (v &0 + 1) * v &2), from (D&2 D= D.0 D∧ D&0 D= D.0) D∨ D&0 D* D&2 D≀ D&1 D∧ D&1 D< (D&0 D+ D.1) D* D&2, dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ ext this $ (vector_all_iff_forall _).1 $ Ξ»z x y, show y = 0 ∧ z = 0 ∨ z * y ≀ x ∧ x < (z + 1) * y ↔ x / y = z, by refine iff.trans _ eq_comm; exact y.eq_zero_or_pos.elim (Ξ»y0, by rw [y0, nat.div_zero]; exact ⟨λo, (o.resolve_right $ λ⟨_, h2⟩, nat.not_lt_zero _ h2).right, Ξ»z0, or.inl ⟨rfl, z0⟩⟩) (Ξ»ypos, iff.trans ⟨λo, o.resolve_left $ λ⟨h1, _⟩, ne_of_gt ypos h1, or.inr⟩ (le_antisymm_iff.trans $ and_congr (nat.le_div_iff_mul_le _ _ ypos) $ iff.trans ⟨lt_succ_of_le, le_of_lt_succ⟩ (div_lt_iff_lt_mul _ _ ypos)).symm) localized "infix ` D/ `:80 := dioph.div_dioph" in dioph omit df dg open pell theorem pell_dioph : dioph (Ξ»v:vector3 β„• 4, βˆƒ h : 1 < v &0, xn h (v &1) = v &2 ∧ yn h (v &1) = v &3) := have dioph {v : vector3 β„• 4 | 1 < v &0 ∧ v &1 ≀ v &3 ∧ (v &2 = 1 ∧ v &3 = 0 ∨ βˆƒ (u w s t b : β„•), v &2 * v &2 - (v &0 * v &0 - 1) * v &3 * v &3 = 1 ∧ u * u - (v &0 * v &0 - 1) * w * w = 1 ∧ s * s - (b * b - 1) * t * t = 1 ∧ 1 < b ∧ (b ≑ 1 [MOD 4 * v &3]) ∧ (b ≑ v &0 [MOD u]) ∧ 0 < w ∧ v &3 * v &3 ∣ w ∧ (s ≑ v &2 [MOD u]) ∧ (t ≑ v &1 [MOD 4 * v &3]))}, from D.1 D< D&0 D∧ D&1 D≀ D&3 D∧ ((D&2 D= D.1 D∧ D&3 D= D.0) D∨ (Dβˆƒ4 $ Dβˆƒ5 $ Dβˆƒ6 $ Dβˆƒ7 $ Dβˆƒ8 $ D&7 D* D&7 D- (D&5 D* D&5 D- D.1) D* D&8 D* D&8 D= D.1 D∧ D&4 D* D&4 D- (D&5 D* D&5 D- D.1) D* D&3 D* D&3 D= D.1 D∧ D&2 D* D&2 D- (D&0 D* D&0 D- D.1) D* D&1 D* D&1 D= D.1 D∧ D.1 D< D&0 D∧ (D≑ (D&0) (D.1) (D.4 D* D&8)) D∧ (D≑ (D&0) (D&5) D&4) D∧ D.0 D< D&3 D∧ D&8 D* D&8 D∣ D&3 D∧ (D≑ (D&2) (D&7) D&4) D∧ (D≑ (D&1) (D&6) (D.4 D* D&8)))), dioph.ext this $ Ξ»v, matiyasevic.symm theorem xn_dioph : dioph_pfun (Ξ»v:vector3 β„• 2, ⟨1 < v &0, Ξ»h, xn h (v &1)⟩) := have dioph (Ξ»v:vector3 β„• 3, βˆƒ y, βˆƒ h : 1 < v &1, xn h (v &2) = v &0 ∧ yn h (v &2) = y), from let D_pell := @reindex_dioph _ (fin2 4) _ pell_dioph [&2, &3, &1, &0] in Dβˆƒ3 D_pell, (dioph_pfun_vec _).2 $ dioph.ext this $ Ξ»v, ⟨λ⟨y, h, xe, ye⟩, ⟨h, xe⟩, λ⟨h, xe⟩, ⟨_, h, xe, rfl⟩⟩ include df dg /-- A version of **Matiyasevic's theorem** -/ theorem pow_dioph : dioph_fn (Ξ»v, f v ^ g v) := have dioph {v : vector3 β„• 3 | v &2 = 0 ∧ v &0 = 1 ∨ 0 < v &2 ∧ (v &1 = 0 ∧ v &0 = 0 ∨ 0 < v &1 ∧ βˆƒ (w a t z x y : β„•), (βˆƒ (a1 : 1 < a), xn a1 (v &2) = x ∧ yn a1 (v &2) = y) ∧ (x ≑ y * (a - v &1) + v &0 [MOD t]) ∧ 2 * a * v &1 = t + (v &1 * v &1 + 1) ∧ v &0 < t ∧ v &1 ≀ w ∧ v &2 ≀ w ∧ a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)}, from let D_pell := @reindex_dioph _ (fin2 9) _ pell_dioph [&4, &8, &1, &0] in (D&2 D= D.0 D∧ D&0 D= D.1) D∨ (D.0 D< D&2 D∧ ((D&1 D= D.0 D∧ D&0 D= D.0) D∨ (D.0 D< D&1 D∧ (Dβˆƒ3 $ Dβˆƒ4 $ Dβˆƒ5 $ Dβˆƒ6 $ Dβˆƒ7 $ Dβˆƒ8 $ D_pell D∧ (D≑ (D&1) (D&0 D* (D&4 D- D&7) D+ D&6) (D&3)) D∧ D.2 D* D&4 D* D&7 D= D&3 D+ (D&7 D* D&7 D+ D.1) D∧ D&6 D< D&3 D∧ D&7 D≀ D&5 D∧ D&8 D≀ D&5 D∧ D&4 D* D&4 D- ((D&5 D+ D.1) D* (D&5 D+ D.1) D- D.1) D* (D&5 D* D&2) D* (D&5 D* D&2) D= D.1)))), dioph_fn_comp2 df dg $ (dioph_fn_vec _).2 $ dioph.ext this $ Ξ»v, iff.symm $ eq_pow_of_pell.trans $ or_congr iff.rfl $ and_congr iff.rfl $ or_congr iff.rfl $ and_congr iff.rfl $ ⟨λ⟨w, a, t, z, a1, h⟩, ⟨w, a, t, z, _, _, ⟨a1, rfl, rfl⟩, h⟩, λ⟨w, a, t, z, ._, ._, ⟨a1, rfl, rfl⟩, h⟩, ⟨w, a, t, z, a1, h⟩⟩ end end dioph
9412fee2934a2a3538ada65226455ba492473d79
99b5e6372af1f404777312358869f95be7de84a3
/src/hott/hit/pushout.lean
48b7aaf5212c1e058a1a312d6889cdf737b52016
[ "Apache-2.0" ]
permissive
forked-from-1kasper/hott3
8fa064ab5e8c9d6752a783d74ab226ddc5b5232a
2db24de7a361a7793b0eae4ca5c3fd4d4a0fc691
refs/heads/master
1,584,867,131,028
1,530,766,841,000
1,530,766,841,000
139,797,034
0
0
Apache-2.0
1,530,766,961,000
1,530,766,961,000
null
UTF-8
Lean
false
false
12,015
lean
/- Copyright (c) 2015-16 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Ulrik Buchholtz, Jakob von Raumer Declaration and properties of the pushout -/ import .quotient ..types.arrow_2 universes u v w hott_theory namespace hott open hott.quotient hott.sum sum hott.equiv is_trunc pointed hott.sigma namespace pushout section parameters {TL : Type u} {BL : Type v} {TR : Type w} (f : TL β†’ BL) (g : TL β†’ TR) private abbreviation A := BL ⊎ TR inductive pushout_rel : A β†’ A β†’ Type (max u v w) | Rmk : Ξ (x : TL), pushout_rel (inl (f x)) (inr (g x)) open pushout_rel private abbreviation R := pushout_rel @[hott] def pushout : Type _ := quotient R parameters {f g} @[hott] def inl (x : BL) : pushout := class_of R (inl x) @[hott] def inr (x : TR) : pushout := class_of R (inr x) @[hott] def glue (x : TL) : inl (f x) = inr (g x) := eq_of_rel pushout_rel (Rmk x) @[hott, elab_as_eliminator, induction] protected def rec {P : pushout β†’ Type _} (Pinl : Ξ (x : BL), P (inl x)) (Pinr : Ξ (x : TR), P (inr x)) (Pglue : Ξ (x : TL), Pinl (f x) =[glue x] Pinr (g x)) (y : pushout) : P y := begin hinduction y, { cases a, apply Pinl, apply Pinr}, { cases H, apply Pglue} end @[hott, reducible] protected def rec_on {P : pushout β†’ Type _} (y : pushout) (Pinl : Ξ (x : BL), P (inl x)) (Pinr : Ξ (x : TR), P (inr x)) (Pglue : Ξ (x : TL), Pinl (f x) =[glue x] Pinr (g x)) : P y := rec Pinl Pinr Pglue y @[hott] theorem rec_glue {P : pushout β†’ Type _} (Pinl : Ξ (x : BL), P (inl x)) (Pinr : Ξ (x : TR), P (inr x)) (Pglue : Ξ (x : TL), Pinl (f x) =[glue x] Pinr (g x)) (x : TL) : apd (rec Pinl Pinr Pglue) (glue x) = Pglue x := rec_eq_of_rel _ _ _ @[hott, induction, priority 1100] protected def elim {P : Type _} (Pinl : BL β†’ P) (Pinr : TR β†’ P) (Pglue : Ξ (x : TL), Pinl (f x) = Pinr (g x)) (y : pushout) : P := begin hinduction y, exact Pinl x, exact Pinr x, exact pathover_of_eq _ (Pglue x) end @[hott] theorem elim_glue {P : Type _} (Pinl : BL β†’ P) (Pinr : TR β†’ P) (Pglue : Ξ (x : TL), Pinl (f x) = Pinr (g x)) (x : TL) : ap (elim Pinl Pinr Pglue) (glue x) = Pglue x := begin apply eq_of_fn_eq_fn_inv ((pathover_constant (glue x)) _ _), refine (apd_eq_pathover_of_eq_ap _ _)⁻¹ ⬝ rec_eq_of_rel _ _ _ end @[hott] protected def elim_type (Pinl : BL β†’ Type _) (Pinr : TR β†’ Type _) (Pglue : Ξ (x : TL), Pinl (f x) ≃ Pinr (g x)) : pushout β†’ Type _ := quotient.elim_type (Ξ»z, sum.rec Pinl Pinr z) begin intros v v' r, induction r, apply Pglue end @[hott] theorem elim_type_glue (Pinl : BL β†’ Type _) (Pinr : TR β†’ Type _) (Pglue : Ξ (x : TL), Pinl (f x) ≃ Pinr (g x)) (x : TL) : transport (elim_type Pinl Pinr Pglue) (glue x) = Pglue x := elim_type_eq_of_rel_fn _ _ _ @[hott] theorem elim_type_glue_inv (Pinl : BL β†’ Type _) (Pinr : TR β†’ Type _) (Pglue : Ξ (x : TL), Pinl (f x) ≃ Pinr (g x)) (x : TL) : transport (elim_type Pinl Pinr Pglue) (glue x)⁻¹ = to_inv (Pglue x) := elim_type_eq_of_rel_inv _ _ _ @[hott] protected def rec_prop {P : pushout β†’ Type _} [H : Ξ x, is_prop (P x)] (Pinl : Ξ (x : BL), P (inl x)) (Pinr : Ξ (x : TR), P (inr x)) (y : pushout) := rec Pinl Pinr (Ξ»x, is_prop.elimo _ _ _) y @[hott] protected def elim_prop {P : Type _} [H : is_prop P] (Pinl : BL β†’ P) (Pinr : TR β†’ P) (y : pushout) : P := elim Pinl Pinr (Ξ»a, is_prop.elim _ _) y end variables {TL : Type u} {BL : Type v} {TR : Type w} (f : TL β†’ BL) (g : TL β†’ TR) @[hott] protected theorem elim_inl {P : Type _} (Pinl : BL β†’ P) (Pinr : TR β†’ P) (Pglue : Ξ (x : TL), Pinl (f x) = Pinr (g x)) {b b' : BL} (p : b = b') : ap (pushout.elim Pinl Pinr Pglue) (ap inl p) = ap Pinl p := (ap_compose _ _ _)⁻¹ @[hott] protected theorem elim_inr {P : Type _} (Pinl : BL β†’ P) (Pinr : TR β†’ P) (Pglue : Ξ (x : TL), Pinl (f x) = Pinr (g x)) {b b' : TR} (p : b = b') : ap (pushout.elim Pinl Pinr Pglue) (ap inr p) = ap Pinr p := (ap_compose _ _ _)⁻¹ /- The non-dependent universal property -/ @[hott] def pushout_arrow_equiv (C : Type _) : (pushout f g β†’ C) ≃ (Ξ£(i : BL β†’ C) (j : TR β†’ C), Ξ c, i (f c) = j (g c)) := begin fapply equiv.MK, { intro f, exact ⟨λx, f (inl x), Ξ»x, f (inr x), Ξ»x, ap f (glue x)⟩}, { intros v x, induction v with i w, induction w with j p, hinduction x, exact (i a), exact (j a), exact (p x)}, { intro v, induction v with i w, induction w with j p, dsimp, apply ap, apply ap, apply eq_of_homotopy, intro x, apply elim_glue}, { intro f, apply eq_of_homotopy, intro x, hinduction x, refl, refl, dsimp, apply eq_pathover, apply hdeg_square, apply elim_glue}, end /- glue squares -/ @[hott] protected def glue_square {x x' : TL} (p : x = x') : square (glue x) (glue x') (ap inl (ap f p)) (ap inr (ap g p)) := by cases p; apply vrefl end pushout --open function -- namespace pushout /- The flattening lemma -/ section parameters {TL : Type _} {BL : Type _} {TR : Type _} (f : TL β†’ BL) (g : TL β†’ TR) (Pinl : BL β†’ Type u) (Pinr : TR β†’ Type u) (Pglue : Ξ (x : TL), Pinl (f x) ≃ Pinr (g x)) include Pglue private abbreviation A := BL ⊎ TR private abbreviation R : A β†’ A β†’ Type _ := pushout_rel f g private abbreviation P := pushout.elim_type Pinl Pinr Pglue private abbreviation F : sigma (Pinl ∘ f) β†’ sigma Pinl := Ξ»z, ⟨ f z.1 , z.2 ⟩ private abbreviation G : sigma (Pinl ∘ f) β†’ sigma Pinr := Ξ»z, ⟨ g z.1 , Pglue z.1 z.2 ⟩ @[hott] protected def flattening : sigma P ≃ pushout F G := begin refine quotient.flattening.flattening_lemma _ _ _ ⬝e _, fapply equiv.MK, { intro q, hinduction q with z z z' fr, { induction z with a p, induction a with x x, { exact inl ⟨x, p⟩ }, { exact inr ⟨x, p⟩ } }, { induction fr with a a' r p, induction r with x, exact glue (⟨x, p⟩ : sigma _) } }, { intro q, hinduction q with xp xp xp, { exact class_of _ ⟨sum.inl xp.1, xp.2⟩ }, { exact class_of _ ⟨sum.inr xp.1, xp.2⟩ }, { apply eq_of_rel, dsimp, exact flattening.flattening_rel.mk _ (pushout_rel.Rmk _ _ _) _ } }, { intro q, hinduction q with xp xp xp; induction xp with x p, { apply ap inl, reflexivity }, { apply ap inr, reflexivity }, { apply eq_pathover, dsimp, rwr [ap_id, ←ap_compose' (quotient.elim _ _)], rwr elim_glue, rwr elim_eq_of_rel, dsimp, apply hrefl } }, { intro q, hinduction q with z z z' fr, { induction z with a p, induction a with x x, { reflexivity }, { reflexivity } }, { induction fr with a a' r p, induction r with x, apply eq_pathover, rwr [ap_id, ←ap_compose' (pushout.elim _ _ _)], rwr elim_eq_of_rel, rwr elim_glue, apply hrefl } } end end -- Commutativity of pushouts section variables {TL : Type u} {BL : Type v} {TR : Type w} (f : TL β†’ BL) (g : TL β†’ TR) @[hott] protected def transpose : pushout f g β†’ pushout g f := begin intro x, hinduction x, apply inr a, apply inl a, exact (glue _)⁻¹ end --TODO prove without krwr? @[hott] protected def transpose_involutive (x : pushout f g) : pushout.transpose g f (pushout.transpose f g x) = x := begin hinduction x, apply idp, apply idp, apply eq_pathover, refine _ ⬝hp (ap_id _)⁻¹ᡖ, refine ap_compose (pushout.transpose _ _) _ _ ⬝ph _, apply hdeg_square, dsimp [pushout.transpose], rwr [elim_glue, ap_inv, elim_glue, hott.eq.inv_inv], end @[hott] protected def symm : pushout f g ≃ pushout g f := begin fapply equiv.MK, apply pushout.transpose, apply pushout.transpose, intro x; apply pushout.transpose_involutive, intro x; apply pushout.transpose_involutive end end -- Functoriality of pushouts section section lemmas variables {X : Type _} {xβ‚€ x₁ xβ‚‚ x₃ : X} (p : xβ‚€ = x₁) (q : x₁ = xβ‚‚) (r : xβ‚‚ = x₃) @[hott] private def is_equiv_functor_lemma₁ : (r ⬝ ((p ⬝ q ⬝ r)⁻¹ ⬝ p)) = q⁻¹ := by cases p; cases r; cases q; reflexivity @[hott] private def is_equiv_functor_lemmaβ‚‚ : (p ⬝ q ⬝ r)⁻¹ ⬝ (p ⬝ q) = r⁻¹ := by cases p; cases r; cases q; reflexivity end lemmas variables {TL : Type _} {BL : Type _} {TR : Type _} {f : TL β†’ BL} {g : TL β†’ TR} {TL' : Type _} {BL' : Type _} {TR' : Type _} {f' : TL' β†’ BL'} {g' : TL' β†’ TR'} (tl : TL β†’ TL') (bl : BL β†’ BL') (tr : TR β†’ TR') (fh : bl ∘ f ~ f' ∘ tl) (gh : tr ∘ g ~ g' ∘ tl) include fh gh def pushout_rel_functor {{x x' : BL ⊎ TR}} (r : pushout_rel f g x x') : pushout_rel f' g' (x.functor bl tr) (x'.functor bl tr) := begin induction r, apply transport11 (pushout_rel f' g') (ap sum.inl (fh r)⁻¹ᡖ) (ap sum.inr (gh r)⁻¹ᡖ), constructor end @[hott] protected def functor : pushout f g β†’ pushout f' g' := begin intro x, hinduction x with a b z, { exact inl (bl a) }, { exact inr (tr b) }, { exact (ap inl (fh z)) ⬝ glue (tl z) ⬝ (ap inr (gh z)⁻¹) } end @[hott] protected def pushout_functor_homotopy_quotient_functor : pushout.functor tl bl tr fh gh ~ quotient.functor (sum.functor bl tr) (pushout_rel_functor tl bl tr fh gh) := begin intro x, hinduction x with a b z, { refl }, { refl }, { dsimp, apply eq_pathover, apply hdeg_square, refine elim_glue _ _ _ _ ⬝ _ ⬝ (functor_eq_of_rel _ _ _)⁻¹ᡖ, dsimp [glue, pushout_rel_functor], rwr [ap_inv], symmetry, apply eq_top_of_square, refine ap_compose (class_of _) _ _ ⬝ph _ ⬝hp (ap_compose (class_of _) _ _)⁻¹ᡖ, apply natural_square2, rwr [←transport11_con, ap_inv, con.left_inv, ap_inv, con.left_inv] } end @[hott] protected def ap_functor_inl {x x' : BL} (p : x = x') : ap (pushout.functor tl bl tr fh gh) (ap inl p) = ap inl (ap bl p) := by cases p; reflexivity @[hott] protected def ap_functor_inr {x x' : TR} (p : x = x') : ap (pushout.functor tl bl tr fh gh) (ap inr p) = ap inr (ap tr p) := by cases p; reflexivity variables [ietl : is_equiv tl] [iebl : is_equiv bl] [ietr : is_equiv tr] include ietl iebl ietr open equiv is_equiv arrow @[hott, instance] protected def is_equiv_functor : is_equiv (pushout.functor tl bl tr fh gh) := sorry end /- version giving the equivalence -/ section variables {TL : Type _} {BL : Type _} {TR : Type _} {f : TL β†’ BL} {g : TL β†’ TR} {TL' : Type _} {BL' : Type _} {TR' : Type _} {f' : TL' β†’ BL'} {g' : TL' β†’ TR'} (tl : TL ≃ TL') (bl : BL ≃ BL') (tr : TR ≃ TR') (fh : bl ∘ f ~ f' ∘ tl) (gh : tr ∘ g ~ g' ∘ tl) include fh gh @[hott] protected def equiv : pushout f g ≃ pushout f' g' := equiv.mk (pushout.functor tl bl tr fh gh) (by apply_instance) end @[hott, instance] def pointed_pushout {TL BL TR : Type _} [HTL : pointed TL] [HBL : pointed BL] [HTR : pointed TR] (f : TL β†’ BL) (g : TL β†’ TR) : pointed (pushout f g) := pointed.mk (inl (point _)) end pushout open pushout @[hott] def ppushout {TL BL TR : Type*} (f : TL β†’* BL) (g : TL β†’* TR) : Type* := pointed.mk' (pushout f g) namespace pushout section parameters {TL : Type*} {BL : Type*} {TR : Type*} (f : TL β†’* BL) (g : TL β†’* TR) parameters {f g} @[hott] def pinl : BL β†’* ppushout f g := pmap.mk inl idp @[hott] def pinr : TR β†’* ppushout f g := pmap.mk inr ((ap inr (respect_pt g))⁻¹ ⬝ (glue _)⁻¹ ⬝ (ap inl (respect_pt f))) @[hott] def pglue (x : TL) : pinl (f x) = pinr (g x) := -- TODO do we need this? glue _ end section variables {TL : Type*} {BL : Type*} {TR : Type*} (f : TL β†’* BL) (g : TL β†’* TR) @[hott] protected def psymm : ppushout f g ≃* ppushout g f := begin fapply pequiv_of_equiv, { apply pushout.symm }, { exact ap inr (respect_pt f)⁻¹ ⬝ (glue _)⁻¹ ⬝ ap inl (respect_pt g) } end end end pushout end hott
90c2b994e6d0aa9f061e0f08128cb43204448c77
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/measure_theory/giry_monad.lean
5bc5511f75bebee81632cda381cbd71a26cd3a8f
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
6,940
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 Giry monad: `measure` is a monad in the category of `measurable_space` and `measurable` functions. -/ import measure_theory.integration noncomputable theory local attribute [instance, priority 0] classical.prop_decidable open classical set lattice filter variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} {Ξ΅ : Type*} namespace measure_theory namespace measure variables [measurable_space Ξ±] [measurable_space Ξ²] /-- Measurability structure on `measure`: Measures are measurable w.r.t. all projections -/ instance : measurable_space (measure Ξ±) := ⨆ (s : set Ξ±) (hs : is_measurable s), (borel ennreal).comap (λμ, ΞΌ s) lemma measurable_coe {s : set Ξ±} (hs : is_measurable s) : measurable (λμ : measure Ξ±, ΞΌ s) := measurable_space.comap_le_iff_le_map.1 $ le_supr_of_le s $ le_supr_of_le hs $ le_refl _ lemma measurable_of_measurable_coe (f : Ξ² β†’ measure Ξ±) (h : βˆ€(s : set Ξ±) (hs : is_measurable s), measurable (Ξ»b, f b s)) : measurable f := supr_le $ assume s, supr_le $ assume hs, measurable_space.comap_le_iff_le_map.2 $ by rw [measurable_space.map_comp]; exact h s hs lemma measurable_map (f : Ξ± β†’ Ξ²) (hf : measurable f) : measurable (λμ : measure Ξ±, ΞΌ.map f) := measurable_of_measurable_coe _ $ assume s hs, suffices measurable (Ξ» (ΞΌ : measure Ξ±), ΞΌ (f ⁻¹' s)), by simpa [map_apply, hs, hf], measurable_coe (hf.preimage hs) lemma measurable_dirac : measurable (measure.dirac : Ξ± β†’ measure Ξ±) := measurable_of_measurable_coe _ $ assume s hs, begin simp [hs, lattice.supr_eq_if], exact measurable_const.if hs measurable_const end lemma measurable_integral (f : Ξ± β†’ ennreal) (hf : measurable f) : measurable (λμ : measure Ξ±, ΞΌ.integral f) := suffices measurable (λμ : measure Ξ±, (⨆n:β„•, @simple_func.integral Ξ± { ΞΌ := ΞΌ } (simple_func.eapprox f n)) : _ β†’ ennreal), begin convert this, funext ΞΌ, exact @lintegral_eq_supr_eapprox_integral Ξ± {ΞΌ := ΞΌ} f hf end, measurable.supr $ assume n, begin dunfold simple_func.integral, refine measurable_finset_sum (simple_func.eapprox f n).range _, assume i, refine ennreal.measurable_mul measurable_const _, exact measurable_coe ((simple_func.eapprox f n).preimage_measurable _) end /-- Monadic join on `measure` in the category of measurable spaces and measurable functions. -/ def join (m : measure (measure Ξ±)) : measure Ξ± := measure.of_measurable (Ξ»s hs, m.integral (λμ, ΞΌ s)) (by simp [integral]) begin assume f hf h, simp [measure_Union h hf], apply lintegral_tsum, assume i, exact measurable_coe (hf i) end @[simp] lemma join_apply {m : measure (measure Ξ±)} : βˆ€{s : set Ξ±}, is_measurable s β†’ join m s = m.integral (λμ, ΞΌ s) := measure.of_measurable_apply lemma measurable_join : measurable (join : measure (measure Ξ±) β†’ measure Ξ±) := measurable_of_measurable_coe _ $ assume s hs, by simp [hs]; exact measurable_integral _ (measurable_coe hs) lemma integral_join {m : measure (measure Ξ±)} {f : Ξ± β†’ ennreal} (hf : measurable f) : integral (join m) f = integral m (λμ, integral ΞΌ f) := begin transitivity, apply lintegral_eq_supr_eapprox_integral, { exact hf }, have : βˆ€n x, @volume Ξ± { ΞΌ := join m} (⇑(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) ⁻¹' {x}) = m.integral (λμ, @volume Ξ± { ΞΌ := ΞΌ } ((⇑(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) ⁻¹' {x}))) := assume n x, join_apply (simple_func.measurable_sn _ _), conv { to_lhs, congr, funext, rw [simple_func.integral] }, simp [this], transitivity, have : βˆ€(s : β„• β†’ finset ennreal) (f : β„• β†’ ennreal β†’ measure Ξ± β†’ ennreal) (hf : βˆ€n r, measurable (f n r)) (hm : monotone (Ξ»n ΞΌ, (s n).sum (Ξ» r, r * f n r ΞΌ))), (⨆n:β„•, (s n).sum (Ξ»r, r * integral m (f n r))) = integral m (λμ, ⨆n:β„•, (s n).sum (Ξ»r, r * f n r ΞΌ)), { assume s f hf hm, symmetry, transitivity, apply lintegral_supr, { exact assume n, measurable_finset_sum _ (assume r, ennreal.measurable_mul measurable_const (hf _ _)) }, { exact hm }, congr, funext n, transitivity, apply lintegral_finset_sum, { exact assume r, ennreal.measurable_mul measurable_const (hf _ _) }, congr, funext r, apply lintegral_const_mul, exact hf _ _ }, specialize this (Ξ»n, simple_func.range (simple_func.eapprox f n)), specialize this (Ξ»n r ΞΌ, @volume Ξ± { ΞΌ := ΞΌ } (⇑(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) ⁻¹' {r})), refine this _ _; clear this, { assume n r, apply measurable_coe, exact simple_func.measurable_sn _ _ }, { change monotone (Ξ»n ΞΌ, @simple_func.integral Ξ± {ΞΌ := ΞΌ} (simple_func.eapprox f n)), assume n m h ΞΌ, apply simple_func.integral_le_integral, apply simple_func.monotone_eapprox, assumption }, congr, funext ΞΌ, symmetry, apply lintegral_eq_supr_eapprox_integral, exact hf end /-- Monadic bind on `measure`, only works in the category of measurable spaces and measurable functions. When the function `f` is not measurable the result is not well defined. -/ def bind (m : measure Ξ±) (f : Ξ± β†’ measure Ξ²) : measure Ξ² := join (map f m) @[simp] lemma bind_apply {m : measure Ξ±} {f : Ξ± β†’ measure Ξ²} {s : set Ξ²} (hs : is_measurable s) (hf : measurable f) : bind m f s = m.integral (Ξ»a, f a s) := by rw [bind, join_apply hs, integral_map (measurable_coe hs) hf] lemma measurable_bind' {g : Ξ± β†’ measure Ξ²} (hg : measurable g) : measurable (Ξ»m, bind m g) := measurable_join.comp (measurable_map _ hg) lemma integral_bind {m : measure Ξ±} {g : Ξ± β†’ measure Ξ²} {f : Ξ² β†’ ennreal} (hg : measurable g) (hf : measurable f) : integral (bind m g) f = integral m (Ξ»a, integral (g a) f) := begin transitivity, exact integral_join hf, exact integral_map (measurable_integral _ hf) hg end lemma bind_bind {Ξ³} [measurable_space Ξ³] {m : measure Ξ±} {f : Ξ± β†’ measure Ξ²} {g : Ξ² β†’ measure Ξ³} (hf : measurable f) (hg : measurable g) : bind (bind m f) g = bind m (Ξ»a, bind (f a) g) := measure.ext $ assume s hs, begin rw [bind_apply hs hg, bind_apply hs ((measurable_bind' hg).comp hf), integral_bind hf], { congr, funext a, exact (bind_apply hs hg).symm }, exact (measurable_coe hs).comp hg end lemma bind_dirac {f : Ξ± β†’ measure Ξ²} (hf : measurable f) (a : Ξ±) : bind (dirac a) f = f a := measure.ext $ assume s hs, by rw [bind_apply hs hf, integral_dirac a ((measurable_coe hs).comp hf)] lemma dirac_bind {m : measure Ξ±} : bind m dirac = m := measure.ext $ assume s hs, begin rw [bind_apply hs measurable_dirac], simp [dirac_apply _ hs], transitivity, apply lintegral_supr_const, assumption, exact one_mul _ end end measure end measure_theory
4e4e654c64a303896d4650dddc7bb18022061578
2fbe653e4bc441efde5e5d250566e65538709888
/src/tactic/finish.lean
ed777ac421c64151fef1ee08d2333c44f6fb29ea
[ "Apache-2.0" ]
permissive
aceg00/mathlib
5e15e79a8af87ff7eb8c17e2629c442ef24e746b
8786ea6d6d46d6969ac9a869eb818bf100802882
refs/heads/master
1,649,202,698,930
1,580,924,783,000
1,580,924,783,000
149,197,272
0
0
Apache-2.0
1,537,224,208,000
1,537,224,207,000
null
UTF-8
Lean
false
false
22,207
lean
/- Copyright (c) 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Jesse Michael Han -/ import logic.basic tactic.core /-! # The `finish` family of tactics These tactics do straightforward things: they call the simplifier, split conjunctive assumptions, eliminate existential quantifiers on the left, and look for contradictions. They rely on ematching and congruence closure to try to finish off a goal at the end. The procedures *do* split on disjunctions and recreate the smt state for each terminal call, so they are only meant to be used on small, straightforward problems. ## Main definitions We provide the following tactics: `finish` -- solves the goal or fails `clarify` -- makes as much progress as possible while not leaving more than one goal `safe` -- splits freely, finishes off whatever subgoals it can, and leaves the rest All accept an optional list of simplifier rules, typically definitions that should be expanded. (The equations and identities should not refer to the local context.) ## Implementation notes The variants `ifinish`, `iclarify`, and `isafe` try to restrict to intuitionistic logic. But the `done` tactic leaks classical logic: ``` example {P : Prop} : ¬¬P β†’ P := by using_smt (do smt_tactic.intros, smt_tactic.close) ``` They also do not work well with the current heuristic instantiation method used by `ematch`. So they are left here mainly for reference. -/ declare_trace auto.done declare_trace auto.finish namespace tactic /- call (assert n t) with a fresh name n. -/ meta def assert_fresh (t : expr) : tactic expr := do n ← get_unused_name `h none, assert n t /- call (assertv n t v) with a fresh name n. -/ meta def assertv_fresh (t : expr) (v : expr) : tactic expr := do h ← get_unused_name `h none, assertv h t v namespace interactive meta def revert_all := tactic.revert_all end interactive end tactic open tactic expr namespace auto /- Utilities -/ meta def whnf_reducible (e : expr) : tactic expr := whnf e reducible -- stolen from interactive.lean meta def add_simps : simp_lemmas β†’ list name β†’ tactic simp_lemmas | s [] := return s | s (n::ns) := do s' ← s.add_simp n, add_simps s' ns /- Configuration information for the auto tactics. -/ @[derive decidable_eq, derive inhabited] structure auto_config : Type := (use_simp := tt) -- call the simplifier (classical := tt) -- use classical logic (max_ematch_rounds := 20) -- for the "done" tactic /- Preprocess goal. We want to move everything to the left of the sequent arrow. For intuitionistic logic, we replace the goal p with βˆ€ f, (p β†’ f) β†’ f and introduce. -/ theorem by_contradiction_trick (p : Prop) (h : βˆ€ f : Prop, (p β†’ f) β†’ f) : p := h p id meta def preprocess_goal (cfg : auto_config) : tactic unit := do repeat (intro1 >> skip), tgt ← target >>= whnf_reducible, if (Β¬ (is_false tgt)) then if cfg.classical then (mk_mapp ``classical.by_contradiction [some tgt]) >>= apply >> intro1 >> skip else (mk_mapp ``decidable.by_contradiction [some tgt, none] >>= apply >> intro1 >> skip) <|> applyc ``by_contradiction_trick >> intro1 >> intro1 >> skip else skip /- Normalize hypotheses. Bring conjunctions to the outside (for splitting), bring universal quantifiers to the outside (for ematching). The classical normalizer eliminates a β†’ b in favor of Β¬ a ∨ b. For efficiency, we push negations inwards from the top down. (For example, consider simplifying Β¬ Β¬ (p ∨ q).) -/ section universe u variable {Ξ± : Type u} variables (p q : Prop) variable (s : Ξ± β†’ Prop) local attribute [instance, priority 10] classical.prop_decidable theorem not_not_eq : (Β¬ Β¬ p) = p := propext not_not theorem not_and_eq : (Β¬ (p ∧ q)) = (Β¬ p ∨ Β¬ q) := propext not_and_distrib theorem not_or_eq : (Β¬ (p ∨ q)) = (Β¬ p ∧ Β¬ q) := propext not_or_distrib theorem not_forall_eq : (Β¬ βˆ€ x, s x) = (βˆƒ x, Β¬ s x) := propext not_forall theorem not_exists_eq : (Β¬ βˆƒ x, s x) = (βˆ€ x, Β¬ s x) := propext not_exists theorem not_implies_eq : (Β¬ (p β†’ q)) = (p ∧ Β¬ q) := propext not_imp theorem classical.implies_iff_not_or : (p β†’ q) ↔ (Β¬ p ∨ q) := imp_iff_not_or end def common_normalize_lemma_names : list name := [``bex_def, ``forall_and_distrib, ``exists_imp_distrib, ``or.assoc, ``or.comm, ``or.left_comm, ``and.assoc, ``and.comm, ``and.left_comm] def classical_normalize_lemma_names : list name := common_normalize_lemma_names ++ [``classical.implies_iff_not_or] -- optionally returns an equivalent expression and proof of equivalence private meta def transform_negation_step (cfg : auto_config) (e : expr) : tactic (option (expr Γ— expr)) := do e ← whnf_reducible e, match e with | `(Β¬ %%ne) := (do ne ← whnf_reducible ne, match ne with | `(Β¬ %%a) := if Β¬ cfg.classical then return none else do pr ← mk_app ``not_not_eq [a], return (some (a, pr)) | `(%%a ∧ %%b) := do pr ← mk_app ``not_and_eq [a, b], return (some (`(Β¬ %%a ∨ Β¬ %%b), pr)) | `(%%a ∨ %%b) := do pr ← mk_app ``not_or_eq [a, b], return (some (`(Β¬ %%a ∧ Β¬ %%b), pr)) | `(Exists %%p) := do pr ← mk_app ``not_exists_eq [p], `(%%_ = %%e') ← infer_type pr, return (some (e', pr)) | (pi n bi d p) := if Β¬ cfg.classical then return none else if p.has_var then do pr ← mk_app ``not_forall_eq [lam n bi d (expr.abstract_local p n)], `(%%_ = %%e') ← infer_type pr, return (some (e', pr)) else do pr ← mk_app ``not_implies_eq [d, p], `(%%_ = %%e') ← infer_type pr, return (some (e', pr)) | _ := return none end) | _ := return none end -- given an expr 'e', returns a new expression and a proof of equality private meta def transform_negation (cfg : auto_config) : expr β†’ tactic (option (expr Γ— expr)) := Ξ» e, do opr ← transform_negation_step cfg e, match opr with | (some (e', pr)) := do opr' ← transform_negation e', match opr' with | none := return (some (e', pr)) | (some (e'', pr')) := do pr'' ← mk_eq_trans pr pr', return (some (e'', pr'')) end | none := return none end meta def normalize_negations (cfg : auto_config) (h : expr) : tactic unit := do t ← infer_type h, (_, e, pr) ← simplify_top_down () (Ξ» _, Ξ» e, do oepr ← transform_negation cfg e, match oepr with | (some (e', pr)) := return ((), e', pr) | none := do pr ← mk_eq_refl e, return ((), e, pr) end) t, replace_hyp h e pr, skip meta def normalize_hyp (cfg : auto_config) (simps : simp_lemmas) (h : expr) : tactic unit := (do h ← simp_hyp simps [] h, try (normalize_negations cfg h)) <|> try (normalize_negations cfg h) meta def normalize_hyps (cfg : auto_config) : tactic unit := do simps ← if cfg.classical then add_simps simp_lemmas.mk classical_normalize_lemma_names else add_simps simp_lemmas.mk common_normalize_lemma_names, local_context >>= monad.mapm' (normalize_hyp cfg simps) /- Eliminate existential quantifiers. -/ -- eliminate an existential quantifier if there is one meta def eelim : tactic unit := do ctx ← local_context, first $ ctx.map $ Ξ» h, do t ← infer_type h >>= whnf_reducible, guard (is_app_of t ``Exists), tgt ← target, to_expr ``(@exists.elim _ _ %%tgt %%h) >>= apply, intros, clear h -- eliminate all existential quantifiers, fails if there aren't any meta def eelims : tactic unit := eelim >> repeat eelim /- Substitute if there is a hypothesis x = t or t = x. -/ -- carries out a subst if there is one, fails otherwise meta def do_subst : tactic unit := do ctx ← local_context, first $ ctx.map $ Ξ» h, do t ← infer_type h >>= whnf_reducible, match t with | `(%%a = %%b) := subst h | _ := failed end meta def do_substs : tactic unit := do_subst >> repeat do_subst /- Split all conjunctions. -/ -- Assumes pr is a proof of t. Adds the consequences of t to the context -- and returns tt if anything nontrivial has been added. meta def add_conjuncts : expr β†’ expr β†’ tactic bool := Ξ» pr t, let assert_consequences := Ξ» e t, mcond (add_conjuncts e t) skip (assertv_fresh t e >> skip) in do t' ← whnf_reducible t, match t' with | `(%%a ∧ %%b) := do e₁ ← mk_app ``and.left [pr], assert_consequences e₁ a, eβ‚‚ ← mk_app ``and.right [pr], assert_consequences eβ‚‚ b, return tt | `(true) := do return tt | _ := return ff end -- return tt if any progress is made meta def split_hyp (h : expr) : tactic bool := do t ← infer_type h, mcond (add_conjuncts h t) (clear h >> return tt) (return ff) -- return tt if any progress is made meta def split_hyps_aux : list expr β†’ tactic bool | [] := return ff | (h :: hs) := do b₁ ← split_hyp h, bβ‚‚ ← split_hyps_aux hs, return (b₁ || bβ‚‚) -- fail if no progress is made meta def split_hyps : tactic unit := local_context >>= split_hyps_aux >>= guardb /- Eagerly apply all the preprocessing rules. -/ meta def preprocess_hyps (cfg : auto_config) : tactic unit := do repeat (intro1 >> skip), preprocess_goal cfg, normalize_hyps cfg, repeat (do_substs <|> split_hyps <|> eelim /-<|> self_simplify_hyps-/) /- The terminal tactic, used to try to finish off goals: - Call the contradiction tactic. - Open an SMT state, and use ematching and congruence closure, with all the universal statements in the context. TODO(Jeremy): allow users to specify attribute for ematching lemmas? -/ meta def mk_hinst_lemmas : list expr β†’ smt_tactic hinst_lemmas | [] := -- return hinst_lemmas.mk do get_hinst_lemmas_for_attr `ematch | (h :: hs) := do his ← mk_hinst_lemmas hs, t ← infer_type h, match t with | (pi _ _ _ _) := do t' ← infer_type t, if t' = `(Prop) then (do new_lemma ← hinst_lemma.mk h, return (hinst_lemmas.add his new_lemma)) <|> return his else return his | _ := return his end private meta def report_invalid_em_lemma {Ξ± : Type} (n : name) : smt_tactic Ξ± := fail format!"invalid ematch lemma '{n}'" private meta def add_hinst_lemma_from_name (md : transparency) (lhs_lemma : bool) (n : name) (hs : hinst_lemmas) (ref : pexpr) : smt_tactic hinst_lemmas := do p ← resolve_name n, match p with | expr.const n _ := (do h ← hinst_lemma.mk_from_decl_core md n lhs_lemma, tactic.save_const_type_info n ref, return $ hs.add h) <|> (do hs₁ ← smt_tactic.mk_ematch_eqn_lemmas_for_core md n, tactic.save_const_type_info n ref, return $ hs.merge hs₁) <|> report_invalid_em_lemma n | _ := (do e ← to_expr p, h ← hinst_lemma.mk_core md e lhs_lemma, try (tactic.save_type_info e ref), return $ hs.add h) <|> report_invalid_em_lemma n end private meta def add_hinst_lemma_from_pexpr (md : transparency) (lhs_lemma : bool) (hs : hinst_lemmas) : pexpr β†’ smt_tactic hinst_lemmas | p@(expr.const c []) := add_hinst_lemma_from_name md lhs_lemma c hs p | p@(expr.local_const c _ _ _) := add_hinst_lemma_from_name md lhs_lemma c hs p | p := do new_e ← to_expr p, h ← hinst_lemma.mk_core md new_e lhs_lemma, return $ hs.add h private meta def add_hinst_lemmas_from_pexprs (md : transparency) (lhs_lemma : bool) (ps : list pexpr) (hs : hinst_lemmas) : smt_tactic hinst_lemmas := list.mfoldl (add_hinst_lemma_from_pexpr md lhs_lemma) hs ps /-- `done` first attempts to close the goal using `contradiction`. If this fails, it creates an SMT state and will repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `ps`) and congruence closure. -/ meta def done (ps : list pexpr) (cfg : auto_config := {}) : tactic unit := do when_tracing `auto.done (trace "entering done" >> trace_state), contradiction <|> (solve1 $ (do revert_all, using_smt (do smt_tactic.intros, ctx ← local_context, hs ← mk_hinst_lemmas ctx, hs' ← add_hinst_lemmas_from_pexprs reducible ff ps hs, smt_tactic.iterate_at_most cfg.max_ematch_rounds (smt_tactic.ematch_using hs' >> smt_tactic.try smt_tactic.close)))) /- Tactics that perform case splits. -/ @[derive decidable_eq, derive inhabited] inductive case_option | force -- fail unless all goals are solved | at_most_one -- leave at most one goal | accept -- leave as many goals as necessary private meta def case_cont (s : case_option) (cont : case_option β†’ tactic unit) : tactic unit := do match s with | case_option.force := cont case_option.force >> cont case_option.force | case_option.at_most_one := -- if the first one succeeds, commit to it, and try the second (mcond (cont case_option.force >> return tt) (cont case_option.at_most_one) skip) <|> -- otherwise, try the second (swap >> cont case_option.force >> cont case_option.at_most_one) | case_option.accept := focus [cont case_option.accept, cont case_option.accept] end -- three possible outcomes: -- finds something to case, the continuations succeed ==> returns tt -- finds something to case, the continutations fail ==> fails -- doesn't find anything to case ==> returns ff meta def case_hyp (h : expr) (s : case_option) (cont : case_option β†’ tactic unit) : tactic bool := do t ← infer_type h, match t with | `(%%a ∨ %%b) := cases h >> case_cont s cont >> return tt | _ := return ff end meta def case_some_hyp_aux (s : case_option) (cont : case_option β†’ tactic unit) : list expr β†’ tactic bool | [] := return ff | (h::hs) := mcond (case_hyp h s cont) (return tt) (case_some_hyp_aux hs) meta def case_some_hyp (s : case_option) (cont : case_option β†’ tactic unit) : tactic bool := local_context >>= case_some_hyp_aux s cont /- The main tactics. -/ /-- `safe_core s ps cfg opt` negates the goal, normalizes hypotheses (by splitting conjunctions, eliminating existentials, pushing negations inwards, and calling `simp` with the supplied lemmas `s`), and then tries `contradiction`. If this fails, it will create an SMT state and repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `ps`) and congruence closure. `safe_core` is complete for propositional logic. Depending on the form of `opt` it will: - (if `opt` is `case_option.force`) fail if it does not close the goal, - (if `opt` is `case_option.at_most_one`) fail if it produces more than one goal, and - (if `opt` is `case_option.accept`) ignore the number of goals it produces. -/ meta def safe_core (s : simp_lemmas Γ— list name) (ps : list pexpr) (cfg : auto_config) : case_option β†’ tactic unit := Ξ» co, focus1 $ do when_tracing `auto.finish (trace "entering safe_core" >> trace_state), if cfg.use_simp then do when_tracing `auto.finish (trace "simplifying hypotheses"), simp_all s.1 s.2 { fail_if_unchanged := ff }, when_tracing `auto.finish (trace "result:" >> trace_state) else skip, tactic.done <|> do when_tracing `auto.finish (trace "preprocessing hypotheses"), preprocess_hyps cfg, when_tracing `auto.finish (trace "result:" >> trace_state), done ps cfg <|> (mcond (case_some_hyp co safe_core) skip (match co with | case_option.force := done ps cfg | case_option.at_most_one := try (done ps cfg) | case_option.accept := try (done ps cfg) end)) /-- `clarify` is `safe_core`, but with the `(opt : case_option)` parameter fixed at `case_option.at_most_one`. -/ meta def clarify (s : simp_lemmas Γ— list name) (ps : list pexpr) (cfg : auto_config := {}) : tactic unit := safe_core s ps cfg case_option.at_most_one /-- `safe` is `safe_core`, but with the `(opt : case_option)` parameter fixed at `case_option.accept`. -/ meta def safe (s : simp_lemmas Γ— list name) (ps : list pexpr) (cfg : auto_config := {}) : tactic unit := safe_core s ps cfg case_option.accept /-- `finish` is `safe_core`, but with the `(opt : case_option)` parameter fixed at `case_option.force`. -/ meta def finish (s : simp_lemmas Γ— list name) (ps : list pexpr) (cfg : auto_config := {}) : tactic unit := safe_core s ps cfg case_option.force /-- `iclarify` is like `clarify`, but in some places restricts to intuitionistic logic. Classical logic still leaks, so this tactic is deprecated. -/ meta def iclarify (s : simp_lemmas Γ— list name) (ps : list pexpr) (cfg : auto_config := {}) : tactic unit := clarify s ps {classical := ff, ..cfg} /-- `isafe` is like `safe`, but in some places restricts to intuitionistic logic. Classical logic still leaks, so this tactic is deprecated. -/ meta def isafe (s : simp_lemmas Γ— list name) (ps : list pexpr) (cfg : auto_config := {}) : tactic unit := safe s ps {classical := ff, ..cfg} /-- `ifinish` is like `finish`, but in some places restricts to intuitionistic logic. Classical logic still leaks, so this tactic is deprecated. -/ meta def ifinish (s : simp_lemmas Γ— list name) (ps : list pexpr) (cfg : auto_config := {}) : tactic unit := finish s ps {classical := ff, ..cfg} end auto /- interactive versions -/ open auto namespace tactic namespace interactive open lean lean.parser interactive interactive.types local postfix `?`:9001 := optional local postfix *:9001 := many /-- `clarify [h1,...,hn] using [e1,...,en]` negates the goal, normalizes hypotheses (by splitting conjunctions, eliminating existentials, pushing negations inwards, and calling `simp` with the supplied lemmas `h1,...,hn`), and then tries `contradiction`. If this fails, it will create an SMT state and repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `e1,...,en`) and congruence closure. `clarify` is complete for propositional logic. Either of the supplied simp lemmas or the supplied ematch lemmas are optional. `clarify` will fail if it produces more than one goal. -/ meta def clarify (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?) (cfg : auto_config := {}) : tactic unit := do s ← mk_simp_set ff [] hs, auto.clarify s (ps.get_or_else []) cfg /-- `safe [h1,...,hn] using [e1,...,en]` negates the goal, normalizes hypotheses (by splitting conjunctions, eliminating existentials, pushing negations inwards, and calling `simp` with the supplied lemmas `h1,...,hn`), and then tries `contradiction`. If this fails, it will create an SMT state and repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `e1,...,en`) and congruence closure. `safe` is complete for propositional logic. Either of the supplied simp lemmas or the supplied ematch lemmas are optional. `safe` ignores the number of goals it produces, and should never fail. -/ meta def safe (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?) (cfg : auto_config := {}) : tactic unit := do s ← mk_simp_set ff [] hs, auto.safe s (ps.get_or_else []) cfg /-- `finish [h1,...,hn] using [e1,...,en]` negates the goal, normalizes hypotheses (by splitting conjunctions, eliminating existentials, pushing negations inwards, and calling `simp` with the supplied lemmas `h1,...,hn`), and then tries `contradiction`. If this fails, it will create an SMT state and repeatedly use `ematch` (using `ematch` lemmas in the environment, universally quantified assumptions, and the supplied lemmas `e1,...,en`) and congruence closure. `finish` is complete for propositional logic. Either of the supplied simp lemmas or the supplied ematch lemmas are optional. `finish` will fail if it does not close the goal. -/ meta def finish (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?) (cfg : auto_config := {}) : tactic unit := do s ← mk_simp_set ff [] hs, auto.finish s (ps.get_or_else []) cfg /-- `iclarify` is like `clarify`, but only uses intuitionistic logic. -/ meta def iclarify (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?) (cfg : auto_config := {}) : tactic unit := do s ← mk_simp_set ff [] hs, auto.iclarify s (ps.get_or_else []) cfg /-- `isafe` is like `safe`, but only uses intuitionistic logic. -/ meta def isafe (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?) (cfg : auto_config := {}) : tactic unit := do s ← mk_simp_set ff [] hs, auto.isafe s (ps.get_or_else []) cfg /-- `ifinish` is like `finish`, but only uses intuitionistic logic. -/ meta def ifinish (hs : parse simp_arg_list) (ps : parse (tk "using" *> pexpr_list_or_texpr)?) (cfg : auto_config := {}) : tactic unit := do s ← mk_simp_set ff [] hs, auto.ifinish s (ps.get_or_else []) cfg end interactive end tactic
ecf315bd7ad3b9d32b5c3aa5d7fd25e170648890
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/closure1.lean
87dcba6f7d8ecb5fb481589a8d0927cb6fccc59f
[ "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
1,468
lean
import Lean open Lean open Lean.Meta universes u inductive Vec (Ξ± : Type u) : Nat β†’ Type u | nil : Vec Ξ± 0 | cons {n} : Ξ± β†’ Vec Ξ± n β†’ Vec Ξ± (n+1) set_option trace.Meta.debug true def printDef (declName : Name) : MetaM Unit := do let cinfo ← getConstInfo declName; trace[Meta.debug] cinfo.value! def tst1 : MetaM Unit := do let u := mkLevelParam `u let v := mkLevelMVar `v let m1 ← mkFreshExprMVar (mkSort levelOne) withLocalDeclD `Ξ± (mkSort u) $ fun Ξ± => do withLocalDeclD `Ξ² (mkSort v) $ fun Ξ² => do let m2 ← mkFreshExprMVar (← mkArrow Ξ± m1) withLocalDeclD `a Ξ± $ fun a => do withLocalDeclD `f (← mkArrow Ξ± Ξ±) $ fun f => do withLetDecl `b Ξ± (mkApp f a) $ fun b => do let t := mkApp m2 (mkApp f b) let e ← mkAuxDefinitionFor `foo1 t trace[Meta.debug] e printDef `foo1 #eval tst1 def tst2 : MetaM Unit := do let u := mkLevelParam `u withLocalDeclD `Ξ± (mkSort (mkLevelSucc u)) $ fun Ξ± => do withLocalDeclD `v1 (mkApp2 (mkConst `Vec [u]) Ξ± (mkNatLit 10)) $ fun v1 => withLetDecl `n (mkConst `Nat) (mkNatLit 10) $ fun n => withLocalDeclD `v2 (mkApp2 (mkConst `Vec [u]) Ξ± n) $ fun v2 => do let m ← mkFreshExprMVar (← mkArrow (mkApp2 (mkConst `Vec [u]) Ξ± (mkNatLit 10)) (mkSort levelZero)) withLocalDeclD `p (mkSort levelZero) $ fun p => do let t ← mkEq v1 v2 let t := mkApp2 (mkConst `And) t (mkApp2 (mkConst `Or) (mkApp m v2) p) let e ← mkAuxDefinitionFor `foo2 t trace[Meta.debug] e printDef `foo2 #eval tst2
189688c7ab8a08e558a646c5881bfbae0165af62
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1274.lean
f80dd81b1f07253690807cdfe619877a9f5ad8bd
[ "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
110
lean
macro "πŸ‘‰" t:(ppSpace ident) : term => `($t) macro "πŸ‘ˆ" t:(lookahead(term) ident ppSpace) : term => `($t)
d64e02ce9d3949c4344b8b40d6fbdc9454a48149
649957717d58c43b5d8d200da34bf374293fe739
/src/category_theory/pempty.lean
78a0e525f57ea0b54bef0e5c0c5b39f2acc03022
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
587
lean
-- Copyright (c) 2018 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import category_theory.functor universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory instance pempty_category : small_category pempty := { hom := Ξ» X Y, pempty, id := by obviously, comp := by obviously } namespace functor variables (C : Type u) [π’ž : category.{v} C] include π’ž def empty : pempty β₯€ C := by tidy end functor end category_theory
afba6996bb5908bbde3d034f01533505712d5b43
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/normed_space/hahn_banach/separation.lean
42850fe41480399a7235ff422dddf3f65a3e28b8
[ "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
10,712
lean
/- Copyright (c) 2022 Bhavik Mehta All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, YaΓ«l Dillies -/ import analysis.convex.cone.basic import analysis.convex.gauge import topology.algebra.module.finite_dimension import topology.algebra.module.locally_convex /-! # Separation Hahn-Banach theorem > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove the geometric Hahn-Banach theorem. For any two disjoint convex sets, there exists a continuous linear functional separating them, geometrically meaning that we can intercalate a plane between them. We provide many variations to stricten the result under more assumptions on the convex sets: * `geometric_hahn_banach_open`: One set is open. Weak separation. * `geometric_hahn_banach_open_point`, `geometric_hahn_banach_point_open`: One set is open, the other is a singleton. Weak separation. * `geometric_hahn_banach_open_open`: Both sets are open. Semistrict separation. * `geometric_hahn_banach_compact_closed`, `geometric_hahn_banach_closed_compact`: One set is closed, the other one is compact. Strict separation. * `geometric_hahn_banach_point_closed`, `geometric_hahn_banach_closed_point`: One set is closed, the other one is a singleton. Strict separation. * `geometric_hahn_banach_point_point`: Both sets are singletons. Strict separation. ## TODO * Eidelheit's theorem * `convex ℝ s β†’ interior (closure s) βŠ† s` -/ open set open_locale pointwise variables {π•œ E : Type*} /-- Given a set `s` which is a convex neighbourhood of `0` and a point `xβ‚€` outside of it, there is a continuous linear functional `f` separating `xβ‚€` and `s`, in the sense that it sends `xβ‚€` to 1 and all of `s` to values strictly below `1`. -/ lemma separate_convex_open_set [topological_space E] [add_comm_group E] [topological_add_group E] [module ℝ E] [has_continuous_smul ℝ E] {s : set E} (hsβ‚€ : (0 : E) ∈ s) (hs₁ : convex ℝ s) (hsβ‚‚ : is_open s) {xβ‚€ : E} (hxβ‚€ : xβ‚€ βˆ‰ s) : βˆƒ f : E β†’L[ℝ] ℝ, f xβ‚€ = 1 ∧ βˆ€ x ∈ s, f x < 1 := begin let f : E β†’β‚—.[ℝ] ℝ := linear_pmap.mk_span_singleton xβ‚€ 1 (ne_of_mem_of_not_mem hsβ‚€ hxβ‚€).symm, obtain βŸ¨Ο†, hφ₁, hΟ†β‚‚βŸ© := exists_extension_of_le_sublinear f (gauge s) (Ξ» c hc, gauge_smul_of_nonneg hc.le) (gauge_add_le hs₁ $ absorbent_nhds_zero $ hsβ‚‚.mem_nhds hsβ‚€) _, have hφ₃ : Ο† xβ‚€ = 1, { rw [←submodule.coe_mk xβ‚€ (submodule.mem_span_singleton_self _), hφ₁, linear_pmap.mk_span_singleton'_apply_self] }, have hΟ†β‚„ : βˆ€ x ∈ s, Ο† x < 1, { exact Ξ» x hx, (hΟ†β‚‚ x).trans_lt (gauge_lt_one_of_mem_of_open hs₁ hsβ‚€ hsβ‚‚ hx) }, { refine βŸ¨βŸ¨Ο†, _⟩, hφ₃, hΟ†β‚„βŸ©, refine Ο†.continuous_of_nonzero_on_open _ (hsβ‚‚.vadd (-xβ‚€)) (nonempty.vadd_set ⟨0, hsβ‚€βŸ©) (vadd_set_subset_iff.mpr $ Ξ» x hx, _), change Ο† (-xβ‚€ + x) β‰  0, rw [map_add, map_neg], specialize hΟ†β‚„ x hx, linarith }, rintro ⟨x, hx⟩, obtain ⟨y, rfl⟩ := submodule.mem_span_singleton.1 hx, rw linear_pmap.mk_span_singleton'_apply, simp only [mul_one, algebra.id.smul_eq_mul, submodule.coe_mk], obtain h | h := le_or_lt y 0, { exact h.trans (gauge_nonneg _) }, { rw [gauge_smul_of_nonneg h.le, smul_eq_mul, le_mul_iff_one_le_right h], exact one_le_gauge_of_not_mem (hs₁.star_convex hsβ‚€) (absorbent_nhds_zero $ hsβ‚‚.mem_nhds hsβ‚€).absorbs hxβ‚€, apply_instance } end variables [topological_space E] [add_comm_group E] [topological_add_group E] [module ℝ E] [has_continuous_smul ℝ E] {s t : set E} {x y : E} /-- A version of the **Hahn-Banach theorem**: given disjoint convex sets `s`, `t` where `s` is open, there is a continuous linear functional which separates them. -/ theorem geometric_hahn_banach_open (hs₁ : convex ℝ s) (hsβ‚‚ : is_open s) (ht : convex ℝ t) (disj : disjoint s t) : βˆƒ (f : E β†’L[ℝ] ℝ) (u : ℝ), (βˆ€ a ∈ s, f a < u) ∧ βˆ€ b ∈ t, u ≀ f b := begin obtain rfl | ⟨aβ‚€, haβ‚€βŸ© := s.eq_empty_or_nonempty, { exact ⟨0, 0, by simp, Ξ» b hb, le_rfl⟩ }, obtain rfl | ⟨bβ‚€, hbβ‚€βŸ© := t.eq_empty_or_nonempty, { exact ⟨0, 1, Ξ» a ha, zero_lt_one, by simp⟩ }, let xβ‚€ := bβ‚€ - aβ‚€, let C := xβ‚€ +α΅₯ (s - t), have : (0:E) ∈ C := ⟨aβ‚€ - bβ‚€, sub_mem_sub haβ‚€ hbβ‚€, by rw [vadd_eq_add, sub_add_sub_cancel', sub_self]⟩, have : convex ℝ C := (hs₁.sub ht).vadd _, have : xβ‚€ βˆ‰ C, { intro hxβ‚€, rw ←add_zero xβ‚€ at hxβ‚€, exact disj.zero_not_mem_sub_set (vadd_mem_vadd_set_iff.1 hxβ‚€) }, obtain ⟨f, hf₁, hfβ‚‚βŸ© := separate_convex_open_set β€Ή0 ∈ Cβ€Ί β€Ή_β€Ί (hsβ‚‚.sub_right.vadd _) β€Ήxβ‚€ βˆ‰ Cβ€Ί, have : f bβ‚€ = f aβ‚€ + 1 := by simp [←hf₁], have forall_le : βˆ€ (a ∈ s) (b ∈ t), f a ≀ f b, { intros a ha b hb, have := hfβ‚‚ (xβ‚€ + (a - b)) (vadd_mem_vadd_set $ sub_mem_sub ha hb), simp only [f.map_add, f.map_sub, hf₁] at this, linarith }, refine ⟨f, Inf (f '' t), image_subset_iff.1 (_ : f '' s βŠ† Iio (Inf (f '' t))), Ξ» b hb, _⟩, { rw ←interior_Iic, refine interior_maximal (image_subset_iff.2 $ Ξ» a ha, _) (f.is_open_map_of_ne_zero _ _ hsβ‚‚), { exact le_cInf (nonempty.image _ ⟨_, hbβ‚€βŸ©) (ball_image_of_ball $ forall_le _ ha) }, { rintro rfl, simpa using hf₁ } }, { exact cInf_le ⟨f aβ‚€, ball_image_of_ball $ forall_le _ haβ‚€βŸ© (mem_image_of_mem _ hb) } end theorem geometric_hahn_banach_open_point (hs₁ : convex ℝ s) (hsβ‚‚ : is_open s) (disj : x βˆ‰ s) : βˆƒ f : E β†’L[ℝ] ℝ, βˆ€ a ∈ s, f a < f x := let ⟨f, s, hs, hx⟩ := geometric_hahn_banach_open hs₁ hsβ‚‚ (convex_singleton x) (disjoint_singleton_right.2 disj) in ⟨f, Ξ» a ha, lt_of_lt_of_le (hs a ha) (hx x (mem_singleton _))⟩ theorem geometric_hahn_banach_point_open (ht₁ : convex ℝ t) (htβ‚‚ : is_open t) (disj : x βˆ‰ t) : βˆƒ f : E β†’L[ℝ] ℝ, βˆ€ b ∈ t, f x < f b := let ⟨f, hf⟩ := geometric_hahn_banach_open_point ht₁ htβ‚‚ disj in ⟨-f, by simpa⟩ theorem geometric_hahn_banach_open_open (hs₁ : convex ℝ s) (hsβ‚‚ : is_open s) (ht₁ : convex ℝ t) (ht₃ : is_open t) (disj : disjoint s t) : βˆƒ (f : E β†’L[ℝ] ℝ) (u : ℝ), (βˆ€ a ∈ s, f a < u) ∧ βˆ€ b ∈ t, u < f b := begin obtain (rfl | ⟨aβ‚€, haβ‚€βŸ©) := s.eq_empty_or_nonempty, { exact ⟨0, -1, by simp, Ξ» b hb, by norm_num⟩ }, obtain (rfl | ⟨bβ‚€, hbβ‚€βŸ©) := t.eq_empty_or_nonempty, { exact ⟨0, 1, Ξ» a ha, by norm_num, by simp⟩ }, obtain ⟨f, s, hf₁, hfβ‚‚βŸ© := geometric_hahn_banach_open hs₁ hsβ‚‚ ht₁ disj, have hf : is_open_map f, { refine f.is_open_map_of_ne_zero _, rintro rfl, exact (hf₁ _ haβ‚€).not_le (hfβ‚‚ _ hbβ‚€) }, refine ⟨f, s, hf₁, image_subset_iff.1 (_ : f '' t βŠ† Ioi s)⟩, rw ←interior_Ici, refine interior_maximal (image_subset_iff.2 hfβ‚‚) (f.is_open_map_of_ne_zero _ _ ht₃), rintro rfl, exact (hf₁ _ haβ‚€).not_le (hfβ‚‚ _ hbβ‚€), end variables [locally_convex_space ℝ E] /-- A version of the **Hahn-Banach theorem**: given disjoint convex sets `s`, `t` where `s` is compact and `t` is closed, there is a continuous linear functional which strongly separates them. -/ theorem geometric_hahn_banach_compact_closed (hs₁ : convex ℝ s) (hsβ‚‚ : is_compact s) (ht₁ : convex ℝ t) (htβ‚‚ : is_closed t) (disj : disjoint s t) : βˆƒ (f : E β†’L[ℝ] ℝ) (u v : ℝ), (βˆ€ a ∈ s, f a < u) ∧ u < v ∧ βˆ€ b ∈ t, v < f b := begin obtain rfl | hs := s.eq_empty_or_nonempty, { exact ⟨0, -2, -1, by simp, by norm_num, Ξ» b hb, by norm_num⟩ }, unfreezingI { obtain rfl | ht := t.eq_empty_or_nonempty }, { exact ⟨0, 1, 2, Ξ» a ha, by norm_num, by norm_num, by simp⟩ }, obtain ⟨U, V, hU, hV, hU₁, hV₁, sU, tV, disj'⟩ := disj.exists_open_convexes hs₁ hsβ‚‚ ht₁ htβ‚‚, obtain ⟨f, u, hf₁, hfβ‚‚βŸ© := geometric_hahn_banach_open_open hU₁ hU hV₁ hV disj', obtain ⟨x, hx₁, hxβ‚‚βŸ© := hsβ‚‚.exists_forall_ge hs f.continuous.continuous_on, have : f x < u := hf₁ x (sU hx₁), exact ⟨f, (f x + u)/2, u, Ξ» a ha, by linarith [hxβ‚‚ a ha], by linarith, Ξ» b hb, hfβ‚‚ b (tV hb)⟩, end /-- A version of the **Hahn-Banach theorem**: given disjoint convex sets `s`, `t` where `s` is closed, and `t` is compact, there is a continuous linear functional which strongly separates them. -/ theorem geometric_hahn_banach_closed_compact (hs₁ : convex ℝ s) (hsβ‚‚ : is_closed s) (ht₁ : convex ℝ t) (htβ‚‚ : is_compact t) (disj : disjoint s t) : βˆƒ (f : E β†’L[ℝ] ℝ) (u v : ℝ), (βˆ€ a ∈ s, f a < u) ∧ u < v ∧ βˆ€ b ∈ t, v < f b := let ⟨f, s, t, hs, st, ht⟩ := geometric_hahn_banach_compact_closed ht₁ htβ‚‚ hs₁ hsβ‚‚ disj.symm in ⟨-f, -t, -s, by simpa using ht, by simpa using st, by simpa using hs⟩ theorem geometric_hahn_banach_point_closed (ht₁ : convex ℝ t) (htβ‚‚ : is_closed t) (disj : x βˆ‰ t) : βˆƒ (f : E β†’L[ℝ] ℝ) (u : ℝ), f x < u ∧ βˆ€ b ∈ t, u < f b := let ⟨f, u, v, ha, hst, hb⟩ := geometric_hahn_banach_compact_closed (convex_singleton x) is_compact_singleton ht₁ htβ‚‚ (disjoint_singleton_left.2 disj) in ⟨f, v, hst.trans' $ ha x $ mem_singleton _, hb⟩ theorem geometric_hahn_banach_closed_point (hs₁ : convex ℝ s) (hsβ‚‚ : is_closed s) (disj : x βˆ‰ s) : βˆƒ (f : E β†’L[ℝ] ℝ) (u : ℝ), (βˆ€ a ∈ s, f a < u) ∧ u < f x := let ⟨f, s, t, ha, hst, hb⟩ := geometric_hahn_banach_closed_compact hs₁ hsβ‚‚ (convex_singleton x) is_compact_singleton (disjoint_singleton_right.2 disj) in ⟨f, s, ha, hst.trans $ hb x $ mem_singleton _⟩ /-- See also `normed_space.eq_iff_forall_dual_eq`. -/ theorem geometric_hahn_banach_point_point [t1_space E] (hxy : x β‰  y) : βˆƒ (f : E β†’L[ℝ] ℝ), f x < f y := begin obtain ⟨f, s, t, hs, st, ht⟩ := geometric_hahn_banach_compact_closed (convex_singleton x) is_compact_singleton (convex_singleton y) is_closed_singleton (disjoint_singleton.2 hxy), exact ⟨f, by linarith [hs x rfl, ht y rfl]⟩, end /-- A closed convex set is the intersection of the halfspaces containing it. -/ lemma Inter_halfspaces_eq (hs₁ : convex ℝ s) (hsβ‚‚ : is_closed s) : (β‹‚ (l : E β†’L[ℝ] ℝ), {x | βˆƒ y ∈ s, l x ≀ l y}) = s := begin rw set.Inter_set_of, refine set.subset.antisymm (Ξ» x hx, _) (Ξ» x hx l, ⟨x, hx, le_rfl⟩), by_contra, obtain ⟨l, s, hlA, hl⟩ := geometric_hahn_banach_closed_point hs₁ hsβ‚‚ h, obtain ⟨y, hy, hxy⟩ := hx l, exact ((hxy.trans_lt (hlA y hy)).trans hl).not_le le_rfl, end
a8ce9fab8f20ff6e5865b3d881bc9933416b5ef2
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/tests/compiler/termparsertest1.lean
80ee6241a7561441ac607288925efb0a57126681
[ "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
2,587
lean
import Lean open Lean open Lean.Parser def testParser (input : String) : IO Unit := do env ← mkEmptyEnvironment; stx ← IO.ofExcept $ runParserCategory env `term input "<input>"; IO.println stx def test (is : List String) : IO Unit := is.forM $ fun input => do IO.println input; testParser input def testParserFailure (input : String) : IO Unit := do env ← mkEmptyEnvironment; match runParserCategory env `term input "<input>" with | Except.ok stx => throw (IO.userError ("unexpected success\n" ++ toString stx)) | Except.error msg => IO.println ("failed as expected, error: " ++ msg) def testFailures (is : List String) : IO Unit := is.forM $ fun input => do IO.println input; testParserFailure input def main (xs : List String) : IO Unit := do test [ "Prod.mk", "x.{u, v+1}", "x.{u}", "x", "x.{max u v}", "x.{max u v, 0}", "f 0 1", "f.{u+1} \"foo\" x", "(f x, 0, 1)", "()", "(f x)", "(f x : Type)", "h (f x) (g y)", "if x then f x else g x", "if h : x then f x h else g x h", "have p x y from f x; g this", "suffices h : p x y from f x; g this", "show p x y from f x", "fun x y => f y x", "fun (x y : Nat) => f y x", "fun (x, y) => f y x", "fun z (x, y) => f y x", "fun ⟨x, y⟩ ⟨z, w⟩ => f y x w z", "fun (Prod.mk x y) => f y x", "{ x := 10, y := 20 }", "{ x := 10, y := 20, }", "{ x // p x 10 }", "{ x : Nat // p x 10 }", "{ .. }", "{ fst := 10, .. : Nat Γ— Nat }", "a[i]", "f [10, 20]", "g a[x+2]", "g f.a.1.2.bla x.1.a", "x+y*z < 10/3", "id (Ξ± := Nat) 10", "(x : a)", "a -> b", "{x : a} -> b", "{a : Type} -> [HasToString a] -> (x : a) -> b", "f ({x : a} -> b)", "f (x : a) -> b", "f ((x : a) -> b)", "(f : (n : Nat) β†’ Vector Nat n) -> Nat", "βˆ€ x y (z : Nat), x > y -> x > y - z", " match x with | some x => true | none => false", " match x with | some y => match y with | some (a, b) => a + b | none => 1 | none => 0 ", "Type u", "Sort v", "Type 1", "f Type 1", "let x := 0; x + 1", "let x : Nat := 0; x + 1", "let f (x : Nat) := x + 1; f 0", "let f {Ξ± : Type} (a : Ξ±) : Ξ± := a; f 10", "let f (x) := x + 1; f 10 + f 20", "let (x, y) := f 10; x + y", "let { fst := x, .. } := f 10; x + x", "let x.y := f 10; x", "let x.1 := f 10; x", "let x[i].y := f 10; x", "let x[i] := f 20; x", "-x + y", "!x", "Β¬ a ∧ b", " do x ← f a; x : Nat ← f a; g x; let y := g x; (a, b) <- h x y; let (a, b) := (b, a); pure (a + b)", "do { x ← f a; pure $ a + a }", "let f : Nat β†’ Nat β†’ Nat | 0, a => a + 10 | n+1, b => n * b; f 20", "max a b" ]; testFailures [ "f {x : a} -> b", "(x := 20)", "let x 10; x", "let x := y" ]
0b2d52989751b3881fc4b90404c5e629f70f1211
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/algebra/order_functions.lean
49b2a8150ff74b6a9451dbefb08a0a00d96888d7
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
11,271
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.ordered_group order.lattice open lattice universes u v variables {Ξ± : Type u} {Ξ² : Type v} attribute [simp] max_eq_left max_eq_right min_eq_left min_eq_right /-- A function `f` is strictly monotone if `a < b` implies `f a < f b`. -/ def strict_mono [has_lt Ξ±] [has_lt Ξ²] (f : Ξ± β†’ Ξ²) : Prop := βˆ€ a b, a < b β†’ f a < f b namespace strict_mono open ordering function section variables [linear_order Ξ±] [preorder Ξ²] {f : Ξ± β†’ Ξ²} lemma lt_iff_lt (H : strict_mono f) {a b} : f a < f b ↔ a < b := ⟨λ h, ((lt_trichotomy b a) .resolve_left $ Ξ» h', lt_asymm h $ H _ _ h') .resolve_left $ Ξ» e, ne_of_gt h $ congr_arg _ e, H _ _⟩ lemma injective (H : strict_mono f) : injective f | a b e := ((lt_trichotomy a b) .resolve_left $ Ξ» h, ne_of_lt (H _ _ h) e) .resolve_right $ Ξ» h, ne_of_gt (H _ _ h) e theorem compares (H : strict_mono f) {a b} : βˆ€ {o}, compares o (f a) (f b) ↔ compares o a b | lt := H.lt_iff_lt | eq := ⟨λ h, H.injective h, congr_arg _⟩ | gt := H.lt_iff_lt lemma le_iff_le (H : strict_mono f) {a b} : f a ≀ f b ↔ a ≀ b := ⟨λ h, le_of_not_gt $ Ξ» h', not_le_of_lt (H b a h') h, Ξ» h, (lt_or_eq_of_le h).elim (Ξ» h', le_of_lt (H _ _ h')) (Ξ» h', h' β–Έ le_refl _)⟩ end protected lemma nat {Ξ²} [preorder Ξ²] {f : β„• β†’ Ξ²} (h : βˆ€n, f n < f (n+1)) : strict_mono f := by { intros n m hnm, induction hnm with m' hnm' ih, apply h, exact lt.trans ih (h _) } -- `preorder Ξ±` isn't strong enough: if the preorder on Ξ± is an equivalence relation, -- then `strict_mono f` is vacuously true. lemma monotone [partial_order Ξ±] [preorder Ξ²] {f : Ξ± β†’ Ξ²} (H : strict_mono f) : monotone f := Ξ» a b h, (lt_or_eq_of_le h).rec (le_of_lt ∘ (H _ _)) (by rintro rfl; refl) end strict_mono section open function variables [partial_order Ξ±] [partial_order Ξ²] {f : Ξ± β†’ Ξ²} lemma strict_mono_of_monotone_of_injective (h₁ : monotone f) (hβ‚‚ : injective f) : strict_mono f := Ξ» a b h, begin rw lt_iff_le_and_ne at ⊒ h, exact ⟨h₁ h.1, Ξ» e, h.2 (hβ‚‚ e)⟩ end end section variables [decidable_linear_order Ξ±] [decidable_linear_order Ξ²] {f : Ξ± β†’ Ξ²} {a b c d : Ξ±} -- translate from lattices to linear orders (sup β†’ max, inf β†’ min) @[simp] lemma le_min_iff : c ≀ min a b ↔ c ≀ a ∧ c ≀ b := le_inf_iff @[simp] lemma max_le_iff : max a b ≀ c ↔ a ≀ c ∧ b ≀ c := sup_le_iff lemma max_le_max : a ≀ c β†’ b ≀ d β†’ max a b ≀ max c d := sup_le_sup lemma min_le_min : a ≀ c β†’ b ≀ d β†’ min a b ≀ min c d := inf_le_inf lemma le_max_left_of_le : a ≀ b β†’ a ≀ max b c := le_sup_left_of_le lemma le_max_right_of_le : a ≀ c β†’ a ≀ max b c := le_sup_right_of_le lemma min_le_left_of_le : a ≀ c β†’ min a b ≀ c := inf_le_left_of_le lemma min_le_right_of_le : b ≀ c β†’ min a b ≀ c := inf_le_right_of_le lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) := sup_inf_left lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) := sup_inf_right lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) := inf_sup_left lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) := inf_sup_right instance max_idem : is_idempotent Ξ± max := by apply_instance instance min_idem : is_idempotent Ξ± min := by apply_instance @[simp] lemma min_le_iff : min a b ≀ c ↔ a ≀ c ∨ b ≀ c := have a ≀ b β†’ (a ≀ c ∨ b ≀ c ↔ a ≀ c), from assume h, or_iff_left_of_imp $ le_trans h, have b ≀ a β†’ (a ≀ c ∨ b ≀ c ↔ b ≀ c), from assume h, or_iff_right_of_imp $ le_trans h, by cases le_total a b; simp * @[simp] lemma le_max_iff : a ≀ max b c ↔ a ≀ b ∨ a ≀ c := have b ≀ c β†’ (a ≀ b ∨ a ≀ c ↔ a ≀ c), from assume h, or_iff_right_of_imp $ assume h', le_trans h' h, have c ≀ b β†’ (a ≀ b ∨ a ≀ c ↔ a ≀ b), from assume h, or_iff_left_of_imp $ assume h', le_trans h' h, by cases le_total b c; simp * @[simp] lemma max_lt_iff : max a b < c ↔ (a < c ∧ b < c) := by rw [lt_iff_not_ge]; simp [(β‰₯), le_max_iff, not_or_distrib] @[simp] lemma lt_min_iff : a < min b c ↔ (a < b ∧ a < c) := by rw [lt_iff_not_ge]; simp [(β‰₯), min_le_iff, not_or_distrib] @[simp] lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c := by rw [lt_iff_not_ge]; simp [(β‰₯), max_le_iff, not_and_distrib] @[simp] lemma min_lt_iff : min a b < c ↔ a < c ∨ b < c := by rw [lt_iff_not_ge]; simp [(β‰₯), le_min_iff, not_and_distrib] lemma max_lt_max (h₁ : a < c) (hβ‚‚ : b < d) : max a b < max c d := by apply max_lt; simp [lt_max_iff, h₁, hβ‚‚] lemma min_lt_min (h₁ : a < c) (hβ‚‚ : b < d) : min a b < min c d := by apply lt_min; simp [min_lt_iff, h₁, hβ‚‚] theorem min_right_comm (a b c : Ξ±) : min (min a b) c = min (min a c) b := right_comm min min_comm min_assoc a b c theorem max.left_comm (a b c : Ξ±) : max a (max b c) = max b (max a c) := left_comm max max_comm max_assoc a b c theorem max.right_comm (a b c : Ξ±) : max (max a b) c = max (max a c) b := right_comm max max_comm max_assoc a b c lemma max_distrib_of_monotone (hf : monotone f) : f (max a b) = max (f a) (f b) := by cases le_total a b; simp [h, hf h] lemma min_distrib_of_monotone (hf : monotone f) : f (min a b) = min (f a) (f b) := by cases le_total a b; simp [h, hf h] theorem min_choice (a b : Ξ±) : min a b = a ∨ min a b = b := by by_cases h : a ≀ b; simp [min, h] theorem max_choice (a b : Ξ±) : max a b = a ∨ max a b = b := by by_cases h : a ≀ b; simp [max, h] lemma le_of_max_le_left {a b c : Ξ±} (h : max a b ≀ c) : a ≀ c := le_trans (le_max_left _ _) h lemma le_of_max_le_right {a b c : Ξ±} (h : max a b ≀ c) : b ≀ c := le_trans (le_max_right _ _) h end lemma min_add {Ξ± : Type u} [decidable_linear_ordered_comm_group Ξ±] (a b c : Ξ±) : min a b + c = min (a + c) (b + c) := if hle : a ≀ b then have a - c ≀ b - c, from sub_le_sub hle (le_refl _), by simp * at * else have b - c ≀ a - c, from sub_le_sub (le_of_lt (lt_of_not_ge hle)) (le_refl _), by simp * at * lemma min_sub {Ξ± : Type u} [decidable_linear_ordered_comm_group Ξ±] (a b c : Ξ±) : min a b - c = min (a - c) (b - c) := by simp [min_add, sub_eq_add_neg] /- Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ lemma fn_min_add_fn_max [decidable_linear_order Ξ±] [add_comm_semigroup Ξ²] (f : Ξ± β†’ Ξ²) (n m : Ξ±) : f (min n m) + f (max n m) = f n + f m := by { cases le_total n m with h h; simp [h] } lemma min_add_max [decidable_linear_order Ξ±] [add_comm_semigroup Ξ±] (n m : Ξ±) : min n m + max n m = n + m := fn_min_add_fn_max id n m lemma fn_min_mul_fn_max [decidable_linear_order Ξ±] [comm_semigroup Ξ²] (f : Ξ± β†’ Ξ²) (n m : Ξ±) : f (min n m) * f (max n m) = f n * f m := by { cases le_total n m with h h; simp [h, mul_comm] } lemma min_mul_max [decidable_linear_order Ξ±] [comm_semigroup Ξ±] (n m : Ξ±) : min n m * max n m = n * m := fn_min_mul_fn_max id n m section decidable_linear_ordered_comm_group variables [decidable_linear_ordered_comm_group Ξ±] {a b c : Ξ±} attribute [simp] abs_zero abs_neg def abs_add := @abs_add_le_abs_add_abs theorem abs_le : abs a ≀ b ↔ - b ≀ a ∧ a ≀ b := ⟨assume h, ⟨neg_le_of_neg_le $ le_trans (neg_le_abs_self _) h, le_trans (le_abs_self _) h⟩, assume ⟨h₁, hβ‚‚βŸ©, abs_le_of_le_of_neg_le hβ‚‚ $ neg_le_of_neg_le hβ‚βŸ© lemma abs_lt : abs a < b ↔ - b < a ∧ a < b := ⟨assume h, ⟨neg_lt_of_neg_lt $ lt_of_le_of_lt (neg_le_abs_self _) h, lt_of_le_of_lt (le_abs_self _) h⟩, assume ⟨h₁, hβ‚‚βŸ©, abs_lt_of_lt_of_neg_lt hβ‚‚ $ neg_lt_of_neg_lt hβ‚βŸ© lemma abs_sub_le_iff : abs (a - b) ≀ c ↔ a - b ≀ c ∧ b - a ≀ c := by rw [abs_le, neg_le_sub_iff_le_add, @sub_le_iff_le_add' _ _ b, and_comm] lemma abs_sub_lt_iff : abs (a - b) < c ↔ a - b < c ∧ b - a < c := by rw [abs_lt, neg_lt_sub_iff_lt_add, @sub_lt_iff_lt_add' _ _ b, and_comm] def sub_abs_le_abs_sub := @abs_sub_abs_le_abs_sub lemma abs_abs_sub_le_abs_sub (a b : Ξ±) : abs (abs a - abs b) ≀ abs (a - b) := abs_sub_le_iff.2 ⟨sub_abs_le_abs_sub _ _, by rw abs_sub; apply sub_abs_le_abs_sub⟩ lemma abs_eq (hb : b β‰₯ 0) : abs a = b ↔ a = b ∨ a = -b := iff.intro begin cases le_total a 0 with a_nonpos a_nonneg, { rw [abs_of_nonpos a_nonpos, neg_eq_iff_neg_eq, eq_comm], exact or.inr }, { rw [abs_of_nonneg a_nonneg, eq_comm], exact or.inl } end (by intro h; cases h; subst h; try { rw abs_neg }; exact abs_of_nonneg hb) @[simp] lemma abs_eq_zero : abs a = 0 ↔ a = 0 := ⟨eq_zero_of_abs_eq_zero, Ξ» e, e.symm β–Έ abs_zero⟩ lemma abs_pos_iff {a : Ξ±} : 0 < abs a ↔ a β‰  0 := ⟨λ h, mt abs_eq_zero.2 (ne_of_gt h), abs_pos_of_ne_zero⟩ @[simp] lemma abs_nonpos_iff {a : Ξ±} : abs a ≀ 0 ↔ a = 0 := by rw [← not_lt, abs_pos_iff, not_not] lemma abs_le_max_abs_abs (hab : a ≀ b) (hbc : b ≀ c) : abs b ≀ max (abs a) (abs c) := abs_le_of_le_of_neg_le (by simp [le_max_iff, le_trans hbc (le_abs_self c)]) (by simp [le_max_iff, le_trans (neg_le_neg hab) (neg_le_abs_self a)]) theorem abs_le_abs {Ξ± : Type*} [decidable_linear_ordered_comm_group Ξ±] {a b : Ξ±} (hβ‚€ : a ≀ b) (h₁ : -a ≀ b) : abs a ≀ abs b := calc abs a ≀ b : by { apply abs_le_of_le_of_neg_le; assumption } ... ≀ abs b : le_abs_self _ lemma min_le_add_of_nonneg_right {a b : Ξ±} (hb : b β‰₯ 0) : min a b ≀ a + b := calc min a b ≀ a : by apply min_le_left ... ≀ a + b : le_add_of_nonneg_right hb lemma min_le_add_of_nonneg_left {a b : Ξ±} (ha : a β‰₯ 0) : min a b ≀ a + b := calc min a b ≀ b : by apply min_le_right ... ≀ a + b : le_add_of_nonneg_left ha lemma max_le_add_of_nonneg {a b : Ξ±} (ha : a β‰₯ 0) (hb : b β‰₯ 0) : max a b ≀ a + b := max_le_iff.2 (by split; simpa) end decidable_linear_ordered_comm_group section decidable_linear_ordered_semiring variables [decidable_linear_ordered_semiring Ξ±] {a b c d : Ξ±} lemma monotone_mul_of_nonneg (ha : 0 ≀ a) : monotone (Ξ» x, a*x) := assume b c b_le_c, mul_le_mul_of_nonneg_left b_le_c ha lemma mul_max_of_nonneg (b c : Ξ±) (ha : 0 ≀ a) : a * max b c = max (a * b) (a * c) := max_distrib_of_monotone (monotone_mul_of_nonneg ha) lemma mul_min_of_nonneg (b c : Ξ±) (ha : 0 ≀ a) : a * min b c = min (a * b) (a * c) := min_distrib_of_monotone (monotone_mul_of_nonneg ha) end decidable_linear_ordered_semiring section decidable_linear_ordered_comm_ring variables [decidable_linear_ordered_comm_ring Ξ±] {a b c d : Ξ±} @[simp] lemma abs_one : abs (1 : Ξ±) = 1 := abs_of_pos zero_lt_one lemma max_mul_mul_le_max_mul_max (b c : Ξ±) (ha : 0 ≀ a) (hd: 0 ≀ d) : max (a * b) (d * c) ≀ max a c * max d b := have ba : b * a ≀ max d b * max c a, from mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)), have cd : c * d ≀ max a c * max b d, from mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)), max_le (by simpa [mul_comm, max_comm] using ba) (by simpa [mul_comm, max_comm] using cd) end decidable_linear_ordered_comm_ring
5724224b3fabec6eedcba38e14069faf1430ee04
9c2e8d73b5c5932ceb1333265f17febc6a2f0a39
/src/S4/data.lean
0fe0da27c42eceae951c9640fef5484e3a791bb3
[ "MIT" ]
permissive
minchaowu/ModalTab
2150392108dfdcaffc620ff280a8b55fe13c187f
9bb0bf17faf0554d907ef7bdd639648742889178
refs/heads/master
1,626,266,863,244
1,592,056,874,000
1,592,056,874,000
153,314,364
12
1
null
null
null
null
UTF-8
Lean
false
false
6,586
lean
import defs data.list.perm open nnf namespace list universes u v w variables {Ξ± : Type u} theorem length_sub_lt_of_nodup_subperm [decidable_eq Ξ±] {l₁ lβ‚‚ : list Ξ±} {a : Ξ±} (h₁ : l₁ <+~ lβ‚‚) (hβ‚‚ : a ∈ lβ‚‚) (h₃ : a βˆ‰ l₁) (hβ‚„ : nodup l₁): length lβ‚‚ - length (a :: l₁) < length lβ‚‚ - length l₁ := begin rw nat.sub_lt_sub_left_iff, { simp [zero_lt_one] }, { apply subperm.length_le, apply cons_subperm_of_mem; assumption } end end list structure psig : Type := (d : nnf) (b : list nnf) instance : decidable_eq psig := by tactic.mk_dec_eq_instance def sig : Type := option psig instance : decidable_eq sig := by tactic.mk_dec_eq_instance def dsig : Ξ  (s : sig) (h : s β‰  none), nnf | none h := by contradiction | (some ⟨d, b⟩) h := d def bsig : Ξ  (s : sig) (h : s β‰  none), list nnf | none h := by contradiction | (some ⟨d, b⟩) h := b instance (Ξ“) : decidable_eq (no_literals Ξ“) := by tactic.mk_dec_eq_instance instance (Ξ“) : decidable_eq (saturated Ξ“) := by tactic.mk_dec_eq_instance instance (Ξ“) : decidable_eq (box_only Ξ“) := by tactic.mk_dec_eq_instance structure sseqt : Type := (goal : list nnf) (s : sig) -- sig := option psig (a : list psig) -- psig is the signature, which is of the form (d, b) (h b m: list nnf) (ndh : list.nodup h) -- nodup says there is no duplicate elements (ndb : list.nodup b) (sph : h <+~ closure goal) -- <+~ denotes sublist permutation (spb : b <+~ closure goal) (sbm : m βŠ† closure goal) (ha : βˆ€ Ο† ∈ h, (βŸ¨Ο†, b⟩ : psig) ∈ a) (hb : box_only b) -- dsig takes a signature (d, b) and a proof h, and returns d (ps₁ : Ξ  (h : s β‰  none), dsig s h ∈ m) -- bsig takes a signature (d, b) and a proof h, and returns b (psβ‚‚ : Ξ  (h : s β‰  none), bsig s h βŠ† m) instance : decidable_eq sseqt := by tactic.mk_dec_eq_instance class val_constructible (Ξ“ : sseqt) := (satu : saturated Ξ“.m) (no_box_main : βˆ€ {Ο†}, box Ο† βˆ‰ Ξ“.m) (no_contra_main : βˆ€ {n}, var n ∈ Ξ“.m β†’ neg n βˆ‰ Ξ“.m) class modal_applicable (Ξ“ : sseqt) extends val_constructible Ξ“ := (Ο† : nnf) (ex : dia Ο† ∈ Ξ“.m) class model_constructible (Ξ“ : sseqt) extends val_constructible Ξ“ := (no_dia : βˆ€ {Ο†}, nnf.dia Ο† βˆ‰ Ξ“.m) def and_child {Ο† ψ} (Ξ“ : sseqt) (h : nnf.and Ο† ψ ∈ Ξ“.m) : sseqt := { goal := Ξ“.goal, s := none, m := Ο† :: ψ :: Ξ“.m.erase (and Ο† ψ), sbm := begin intros x hx, cases hx, {rw hx, apply (mem_closure_and _ (Ξ“.sbm h)).1}, {cases hx, {rw hx, apply (mem_closure_and _ (Ξ“.sbm h)).2}, {apply Ξ“.sbm, apply list.erase_subset, exact hx}} end, ps₁ := by intro; contradiction, psβ‚‚ := by intro; contradiction, .. Ξ“} inductive and_instance_seqt (Ξ“ : sseqt) : sseqt β†’ Type | cons : Ξ  {Ο† ψ} (h : nnf.and Ο† ψ ∈ Ξ“.m), and_instance_seqt $ and_child Ξ“ h def or_child_left {Ο† ψ} (Ξ“ : sseqt) (h : nnf.or Ο† ψ ∈ Ξ“.m) : sseqt := { goal := Ξ“.goal, s := none, m := Ο† :: Ξ“.m.erase (or Ο† ψ), sbm := begin intros x hx, cases hx, {rw hx, apply (mem_closure_or _ (Ξ“.sbm h)).1}, {apply Ξ“.sbm, apply list.erase_subset, exact hx} end, ps₁ := by intro; contradiction, psβ‚‚ := by intro; contradiction, .. Ξ“} def or_child_right {Ο† ψ} (Ξ“ : sseqt) (h : nnf.or Ο† ψ ∈ Ξ“.m) : sseqt := { goal := Ξ“.goal, s := none, m := ψ :: Ξ“.m.erase (or Ο† ψ), sbm := begin intros x hx, cases hx, {rw hx, apply (mem_closure_or _ (Ξ“.sbm h)).2}, {apply Ξ“.sbm, apply list.erase_subset, exact hx} end, ps₁ := by intro; contradiction, psβ‚‚ := by intro; contradiction, .. Ξ“} inductive or_instance_seqt (Ξ“ : sseqt) : sseqt β†’ sseqt β†’ Type | cons : Ξ  {Ο† ψ} (h : nnf.or Ο† ψ ∈ Ξ“.m), or_instance_seqt (or_child_left Ξ“ h) (or_child_right Ξ“ h) def box_child_new {Ο†} (Ξ“ : sseqt) (h₁ : nnf.box Ο† ∈ Ξ“.m) (hβ‚‚ : nnf.box Ο† βˆ‰ Ξ“.b) : sseqt := { goal := Ξ“.goal, s := none, h := [], b := box Ο† :: Ξ“.b, m := Ο† :: Ξ“.m.erase (box Ο†), ndh := by simp, ndb := begin rw list.nodup_cons, split, exact hβ‚‚, exact Ξ“.ndb end, sph := begin apply list.nil_subperm end, spb := begin apply list.cons_subperm_of_mem Ξ“.ndb hβ‚‚, apply Ξ“.sbm h₁, apply Ξ“.spb end, sbm := begin intros x hx, cases hx, {rw hx, apply mem_closure_box _ (Ξ“.sbm h₁)}, {apply Ξ“.sbm, apply list.erase_subset, exact hx} end, ha := Ξ» Ο† h, absurd h $ list.not_mem_nil _, hb := cons_box_only Ξ“.hb, ps₁ := by intro; contradiction, psβ‚‚ := by intro; contradiction, .. Ξ“} inductive box_new_instance_seqt (Ξ“ : sseqt) : sseqt β†’ Type | cons : Ξ  {Ο†} (h₁ : nnf.box Ο† ∈ Ξ“.m) (hβ‚‚ : nnf.box Ο† βˆ‰ Ξ“.b), box_new_instance_seqt $ box_child_new Ξ“ h₁ hβ‚‚ def box_child {Ο†} (Ξ“ : sseqt) (h₁ : nnf.box Ο† ∈ Ξ“.m) : sseqt := { goal := Ξ“.goal, s := none, a := Ξ“.a, h := Ξ“.h, b := Ξ“.b, m := Ο† :: Ξ“.m.erase (box Ο†), ndh := Ξ“.ndh, ndb := Ξ“.ndb, sph := Ξ“.sph, spb := Ξ“.spb, sbm := begin intros x hx, cases hx, {rw hx, apply mem_closure_box _ (Ξ“.sbm h₁)}, {apply Ξ“.sbm, apply list.erase_subset, exact hx} end, ha := Ξ“.ha, hb := Ξ“.hb, ps₁ := by intro; contradiction, psβ‚‚ := by intro; contradiction} inductive box_dup_instance_seqt (Ξ“ : sseqt) : sseqt β†’ Type | cons : Ξ  {Ο†} (h₁ : nnf.box Ο† ∈ Ξ“.m) (hβ‚‚ : nnf.box Ο† ∈ Ξ“.b), box_dup_instance_seqt $ box_child Ξ“ h₁ theorem hintikka_vc {Ξ“} (h : val_constructible Ξ“) : hintikka Ξ“.m := {hno_contra := h.no_contra_main, hand_left := begin intros Ο† ψ h₁, exfalso, apply h.satu.no_and, exact h₁ end, hand_right := begin intros Ο† ψ h₁, exfalso, apply h.satu.no_and, exact h₁ end, hor := begin intros Ο† ψ h₁, exfalso, apply h.satu.no_or, exact h₁ end, hbox := begin intros Ο† h₁, exfalso, apply h.no_box_main, exact h₁ end} theorem hintikka_ma {Ξ“} (h : modal_applicable Ξ“) : hintikka Ξ“.m := hintikka_vc h.to_val_constructible theorem hintikka_mc {Ξ“} (h : model_constructible Ξ“) : hintikka Ξ“.m := hintikka_vc h.to_val_constructible structure info : Type := (id : sseqt) (htk : list nnf) (hhtk : hintikka htk) (mhtk : id.m βŠ† htk) instance : decidable_eq info := by tactic.mk_dec_eq_instance inductive tmodel | cons : info β†’ list tmodel β†’ list psig β†’ tmodel instance : decidable_eq tmodel := by tactic.mk_dec_eq_instance
8dbc36c627e2ef24c8a1e5c276c3a72bc3e46aae
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/nat/sqrt.lean
f8e3210cb501b7ad0753517c02ac4444ea10476d
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,788
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.int.basic import Mathlib.PostPort namespace Mathlib /-! # Square root of natural numbers An efficient binary implementation of a (`sqrt n`) function that returns `s` such that ``` s*s ≀ n ≀ s*s + s + s ``` -/ namespace nat theorem sqrt_aux_dec {b : β„•} (h : b β‰  0) : shiftr b (bit0 1) < b := sorry /-- Auxiliary function for `nat.sqrt`. See e.g. <https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2)> -/ def sqrt_aux : β„• β†’ β„• β†’ β„• β†’ β„• := sorry /-- `sqrt n` is the square root of a natural number `n`. If `n` is not a perfect square, it returns the largest `k:β„•` such that `k*k ≀ n`. -/ def sqrt (n : β„•) : β„• := sorry theorem sqrt_aux_0 (r : β„•) (n : β„•) : sqrt_aux 0 r n = r := sorry theorem sqrt_aux_1 {r : β„•} {n : β„•} {b : β„•} (h : b β‰  0) {n' : β„•} (hβ‚‚ : r + b + n' = n) : sqrt_aux b r n = sqrt_aux (shiftr b (bit0 1)) (div2 r + b) n' := sorry theorem sqrt_aux_2 {r : β„•} {n : β„•} {b : β„•} (h : b β‰  0) (hβ‚‚ : n < r + b) : sqrt_aux b r n = sqrt_aux (shiftr b (bit0 1)) (div2 r) n := sorry theorem sqrt_le (n : β„•) : sqrt n * sqrt n ≀ n := and.left (sqrt_is_sqrt n) theorem lt_succ_sqrt (n : β„•) : n < Nat.succ (sqrt n) * Nat.succ (sqrt n) := and.right (sqrt_is_sqrt n) theorem sqrt_le_add (n : β„•) : n ≀ sqrt n * sqrt n + sqrt n + sqrt n := eq.mpr (id (Eq._oldrec (Eq.refl (n ≀ sqrt n * sqrt n + sqrt n + sqrt n)) (Eq.symm (succ_mul (sqrt n) (sqrt n))))) (le_of_lt_succ (lt_succ_sqrt n)) theorem le_sqrt {m : β„•} {n : β„•} : m ≀ sqrt n ↔ m * m ≀ n := { mp := fun (h : m ≀ sqrt n) => le_trans (mul_self_le_mul_self h) (sqrt_le n), mpr := fun (h : m * m ≀ n) => le_of_lt_succ (iff.mpr mul_self_lt_mul_self_iff (lt_of_le_of_lt h (lt_succ_sqrt n))) } theorem sqrt_lt {m : β„•} {n : β„•} : sqrt m < n ↔ m < n * n := lt_iff_lt_of_le_iff_le le_sqrt theorem sqrt_le_self (n : β„•) : sqrt n ≀ n := le_trans (le_mul_self (sqrt n)) (sqrt_le n) theorem sqrt_le_sqrt {m : β„•} {n : β„•} (h : m ≀ n) : sqrt m ≀ sqrt n := iff.mpr le_sqrt (le_trans (sqrt_le m) h) theorem sqrt_eq_zero {n : β„•} : sqrt n = 0 ↔ n = 0 := sorry theorem eq_sqrt {n : β„•} {q : β„•} : q = sqrt n ↔ q * q ≀ n ∧ n < (q + 1) * (q + 1) := sorry theorem le_three_of_sqrt_eq_one {n : β„•} (h : sqrt n = 1) : n ≀ bit1 1 := le_of_lt_succ (iff.mp sqrt_lt (eq.mpr (id (Eq._oldrec (Eq.refl (sqrt n < bit0 1)) h)) (of_as_true trivial))) theorem sqrt_lt_self {n : β„•} (h : 1 < n) : sqrt n < n := iff.mpr sqrt_lt (eq.mp (Eq._oldrec (Eq.refl (n * 1 < n * n)) (mul_one n)) (nat.mul_lt_mul_of_pos_left h (lt_of_succ_lt h))) theorem sqrt_pos {n : β„•} : 0 < sqrt n ↔ 0 < n := le_sqrt theorem sqrt_add_eq (n : β„•) {a : β„•} (h : a ≀ n + n) : sqrt (n * n + a) = n := sorry theorem sqrt_eq (n : β„•) : sqrt (n * n) = n := sqrt_add_eq n (zero_le (n + n)) theorem sqrt_succ_le_succ_sqrt (n : β„•) : sqrt (Nat.succ n) ≀ Nat.succ (sqrt n) := sorry theorem exists_mul_self (x : β„•) : (βˆƒ (n : β„•), n * n = x) ↔ sqrt x * sqrt x = x := sorry theorem sqrt_mul_sqrt_lt_succ (n : β„•) : sqrt n * sqrt n < n + 1 := iff.mpr lt_succ_iff (sqrt_le n) theorem succ_le_succ_sqrt (n : β„•) : n + 1 ≀ (sqrt n + 1) * (sqrt n + 1) := le_of_pred_lt (lt_succ_sqrt (Nat.pred (n + 1))) /-- There are no perfect squares strictly between mΒ² and (m+1)Β² -/ theorem not_exists_sq {n : β„•} {m : β„•} (hl : m * m < n) (hr : n < (m + 1) * (m + 1)) : Β¬βˆƒ (t : β„•), t * t = n := sorry
1f23a6c78f056c288cfd694472670fe5f729d795
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/nat/mul_ind.lean
bd9b629db6a39afe949fd64b49737943ac15cfb5
[ "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,359
lean
/- Copyright (c) 2021 Eric Rodriguez. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Rodriguez -/ import number_theory.padics.padic_norm /-! # Multiplicative induction principles for β„• This file provides three (closely linked) induction principles for β„•, commonly used for proofs about multiplicative functions, such as the totient function and the MΓΆbius function. ## Main definitions * `nat.rec_on_prime_pow`: Given `P 0, P 1` and a way to extend `P a` to `P (p ^ k * a)`, you can define `P` for all natural numbers. * `nat.rec_on_prime_coprime`: Given `P 0`, `P (p ^ k)` for all `p` prime, and a way to extend `P a` and `P b` to `P (a * b)` when `a, b` are coprime, you can define `P` for all natural numbers. * `nat.rec_on_pos_prime_coprime`: Given `P 0`, `P 1`, and `P (p ^ k)` for positive prime powers, and a way to extend `P a` and `P b` to `P (a * b)` when `a, b` are coprime, you can define `P` for all natural numbers. * `nat.rec_on_mul`: Given `P 0`, `P 1`, `P p` for all primes, and a proof that you can extend `P a` and `P b` to `P (a * b)`, you can define `P` for all natural numbers. ## Implementation notes The proofs use `padic_val_nat`; however, we have `padic_val_nat p = nat.log p $ nat.gcd k (p ^ k)` for any `p ∣ k`, which requires far less imports - the API isn't there though; however, this is why it's in `data` even though we import `number_theory`; it's not a particularly deep theorem. ## TODO: Extend these results to any `normalization_monoid` with unique factorization. -/ namespace nat /-- Given `P 0, P 1` and a way to extend `P a` to `P (p ^ k * a)`, you can define `P` for all natural numbers. -/ @[elab_as_eliminator] def rec_on_prime_pow {P : β„• β†’ Sort*} (h0 : P 0) (h1 : P 1) (h : βˆ€ a p n : β„•, p.prime β†’ Β¬ p ∣ a β†’ P a β†’ P (p ^ n * a)) : βˆ€ (a : β„•), P a := Ξ» a, nat.strong_rec_on a $ Ξ» n, match n with | 0 := Ξ» _, h0 | 1 := Ξ» _, h1 | (k+2) := Ξ» hk, begin let p := (k + 2).min_fac, haveI : fact (prime p) := ⟨min_fac_prime (succ_succ_ne_one k)⟩, let t := padic_val_nat p (k+2), have hpt : p ^ t ∣ k + 2 := pow_padic_val_nat_dvd, have ht : 0 < t := one_le_padic_val_nat_of_dvd (nat.succ_ne_zero (k + 1)) (min_fac_dvd _), convert h ((k + 2) / p ^ t) p t (fact.out _) _ _, { rw nat.mul_div_cancel' hpt }, { rw [nat.dvd_div_iff hpt, ←pow_succ'], exact pow_succ_padic_val_nat_not_dvd nat.succ_pos' }, apply hk _ (nat.div_lt_of_lt_mul _), rw [lt_mul_iff_one_lt_left nat.succ_pos', one_lt_pow_iff ht.ne], exact (prime.one_lt' p).out end end /-- Given `P 0`, `P 1`, and `P (p ^ k)` for positive prime powers, and a way to extend `P a` and `P b` to `P (a * b)` when `a, b` are coprime, you can define `P` for all natural numbers. -/ @[elab_as_eliminator] def rec_on_pos_prime_coprime {P : β„• β†’ Sort*} (hp : βˆ€ p n : β„•, prime p β†’ 0 < n β†’ P (p ^ n)) (h0 : P 0) (h1 : P 1) (h : βˆ€ a b, 0 < a β†’ 0 < b β†’ coprime a b β†’ P a β†’ P b β†’ P (a * b)) : βˆ€ a, P a := rec_on_prime_pow h0 h1 $ Ξ» a p n hp' hpa ha, (h (p ^ n) a (pow_pos hp'.pos _) (nat.pos_of_ne_zero (Ξ» t, by simpa [t] using hpa)) (prime.coprime_pow_of_not_dvd hp' hpa).symm (if h : n = 0 then eq.rec h1 h.symm else hp p n hp' $ nat.pos_of_ne_zero h) ha) /-- Given `P 0`, `P (p ^ k)` for all prime powers, and a way to extend `P a` and `P b` to `P (a * b)` when `a, b` are coprime, you can define `P` for all natural numbers. -/ @[elab_as_eliminator] def rec_on_prime_coprime {P : β„• β†’ Sort*} (h0 : P 0) (hp : βˆ€ p n : β„•, prime p β†’ P (p ^ n)) (h : βˆ€ a b, 0 < a β†’ 0 < b β†’ coprime a b β†’ P a β†’ P b β†’ P (a * b)) : βˆ€ a, P a := rec_on_pos_prime_coprime (Ξ» p n h _, hp p n h) h0 (hp 2 0 prime_two) h /-- Given `P 0`, `P 1`, `P p` for all primes, and a proof that you can extend `P a` and `P b` to `P (a * b)`, you can define `P` for all natural numbers. -/ @[elab_as_eliminator] def rec_on_mul {P : β„• β†’ Sort*} (h0 : P 0) (h1 : P 1) (hp : βˆ€ p, prime p β†’ P p) (h : βˆ€ a b, P a β†’ P b β†’ P (a * b)) : βˆ€ a, P a := let hp : βˆ€ p n : β„•, prime p β†’ P (p ^ n) := Ξ» p n hp', match n with | 0 := h1 | (n+1) := by exact h _ _ (hp p hp') (_match _) end in rec_on_prime_coprime h0 hp $ Ξ» a b _ _ _, h a b end nat
805dd414cf52f6a37e470914357308c4aee9911e
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/ring_theory/multiplicity.lean
726f85cd2129e0643f64446527e7a1409982194e
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,086
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Chris Hughes -/ import algebra.associated import algebra.big_operators.basic import ring_theory.valuation.basic /-! # Multiplicity of a divisor For a commutative monoid, this file introduces the notion of multiplicity of a divisor and proves several basic results on it. ## Main definitions * `multiplicity a b`: for two elements `a` and `b` of a commutative monoid returns the largest number `n` such that `a ^ n ∣ b` or infinity, written `⊀`, if `a ^ n ∣ b` for all natural numbers `n`. * `multiplicity.finite a b`: a predicate denoting that the multiplicity of `a` in `b` is finite. -/ variables {Ξ± : Type*} open nat roption open_locale big_operators /-- `multiplicity a b` returns the largest natural number `n` such that `a ^ n ∣ b`, as an `enat` or natural with infinity. If `βˆ€ n, a ^ n ∣ b`, then it returns `⊀`-/ def multiplicity [comm_monoid Ξ±] [decidable_rel ((∣) : Ξ± β†’ Ξ± β†’ Prop)] (a b : Ξ±) : enat := enat.find $ Ξ» n, Β¬a ^ (n + 1) ∣ b namespace multiplicity section comm_monoid variables [comm_monoid Ξ±] /-- `multiplicity.finite a b` indicates that the multiplicity of `a` in `b` is finite. -/ @[reducible] def finite (a b : Ξ±) : Prop := βˆƒ n : β„•, Β¬a ^ (n + 1) ∣ b lemma finite_iff_dom [decidable_rel ((∣) : Ξ± β†’ Ξ± β†’ Prop)] {a b : Ξ±} : finite a b ↔ (multiplicity a b).dom := iff.rfl lemma finite_def {a b : Ξ±} : finite a b ↔ βˆƒ n : β„•, Β¬a ^ (n + 1) ∣ b := iff.rfl @[norm_cast] theorem int.coe_nat_multiplicity (a b : β„•) : multiplicity (a : β„€) (b : β„€) = multiplicity a b := begin apply roption.ext', { repeat {rw [← finite_iff_dom, finite_def]}, norm_cast }, { intros h1 h2, apply _root_.le_antisymm; { apply nat.find_le, norm_cast, simp }} end lemma not_finite_iff_forall {a b : Ξ±} : (Β¬ finite a b) ↔ βˆ€ n : β„•, a ^ n ∣ b := ⟨λ h n, nat.cases_on n (one_dvd _) (by simpa [finite, not_not] using h), by simp [finite, multiplicity, not_not]; tauto⟩ lemma not_unit_of_finite {a b : Ξ±} (h : finite a b) : Β¬is_unit a := let ⟨n, hn⟩ := h in mt (is_unit_iff_forall_dvd.1 ∘ is_unit.pow (n + 1)) $ Ξ» h, hn (h b) lemma finite_of_finite_mul_left {a b c : Ξ±} : finite a (b * c) β†’ finite a c := Ξ» ⟨n, hn⟩, ⟨n, Ξ» h, hn (dvd.trans h (by simp [mul_pow]))⟩ lemma finite_of_finite_mul_right {a b c : Ξ±} : finite a (b * c) β†’ finite a b := by rw mul_comm; exact finite_of_finite_mul_left variable [decidable_rel ((∣) : Ξ± β†’ Ξ± β†’ Prop)] lemma pow_dvd_of_le_multiplicity {a b : Ξ±} {k : β„•} : (k : enat) ≀ multiplicity a b β†’ a ^ k ∣ b := nat.cases_on k (Ξ» _, one_dvd _) (Ξ» k ⟨h₁, hβ‚‚βŸ©, by_contradiction (Ξ» hk, (nat.find_min _ (lt_of_succ_le (hβ‚‚ ⟨k, hk⟩)) hk))) lemma pow_multiplicity_dvd {a b : Ξ±} (h : finite a b) : a ^ get (multiplicity a b) h ∣ b := pow_dvd_of_le_multiplicity (by rw enat.coe_get) lemma is_greatest {a b : Ξ±} {m : β„•} (hm : multiplicity a b < m) : Β¬a ^ m ∣ b := Ξ» h, by rw [enat.lt_coe_iff] at hm; exact nat.find_spec hm.fst (dvd.trans (pow_dvd_pow _ hm.snd) h) lemma is_greatest' {a b : Ξ±} {m : β„•} (h : finite a b) (hm : get (multiplicity a b) h < m) : Β¬a ^ m ∣ b := is_greatest (by rwa [← enat.coe_lt_coe, enat.coe_get] at hm) lemma unique {a b : Ξ±} {k : β„•} (hk : a ^ k ∣ b) (hsucc : Β¬a ^ (k + 1) ∣ b) : (k : enat) = multiplicity a b := le_antisymm (le_of_not_gt (Ξ» hk', is_greatest hk' hk)) $ have finite a b, from ⟨k, hsucc⟩, by { rw [enat.le_coe_iff], exact ⟨this, nat.find_min' _ hsucc⟩ } lemma unique' {a b : Ξ±} {k : β„•} (hk : a ^ k ∣ b) (hsucc : Β¬ a ^ (k + 1) ∣ b) : k = get (multiplicity a b) ⟨k, hsucc⟩ := by rw [← enat.coe_inj, enat.coe_get, unique hk hsucc] lemma le_multiplicity_of_pow_dvd {a b : Ξ±} {k : β„•} (hk : a ^ k ∣ b) : (k : enat) ≀ multiplicity a b := le_of_not_gt $ Ξ» hk', is_greatest hk' hk lemma pow_dvd_iff_le_multiplicity {a b : Ξ±} {k : β„•} : a ^ k ∣ b ↔ (k : enat) ≀ multiplicity a b := ⟨le_multiplicity_of_pow_dvd, pow_dvd_of_le_multiplicity⟩ lemma multiplicity_lt_iff_neg_dvd {a b : Ξ±} {k : β„•} : multiplicity a b < (k : enat) ↔ Β¬ a ^ k ∣ b := by { rw [pow_dvd_iff_le_multiplicity, not_le] } lemma eq_some_iff {a b : Ξ±} {n : β„•} : multiplicity a b = (n : enat) ↔ a ^ n ∣ b ∧ Β¬a ^ (n + 1) ∣ b := ⟨λ h, let ⟨h₁, hβ‚‚βŸ© := eq_some_iff.1 h in hβ‚‚ β–Έ ⟨pow_multiplicity_dvd _, is_greatest (by { rw [enat.lt_coe_iff], exact ⟨h₁, lt_succ_self _⟩ })⟩, Ξ» h, eq_some_iff.2 ⟨⟨n, h.2⟩, eq.symm $ unique' h.1 h.2⟩⟩ lemma eq_top_iff {a b : Ξ±} : multiplicity a b = ⊀ ↔ βˆ€ n : β„•, a ^ n ∣ b := (enat.find_eq_top_iff _).trans $ by { simp only [not_not], exact ⟨λ h n, nat.cases_on n (one_dvd _) (Ξ» n, h _), Ξ» h n, h _⟩ } @[simp] lemma is_unit_left {a : Ξ±} (b : Ξ±) (ha : is_unit a) : multiplicity a b = ⊀ := eq_top_iff.2 (Ξ» _, is_unit_iff_forall_dvd.1 (ha.pow _) _) lemma is_unit_right {a b : Ξ±} (ha : Β¬is_unit a) (hb : is_unit b) : multiplicity a b = 0 := eq_some_iff.2 ⟨by simp, by { rw pow_one, exact Ξ» h, mt (is_unit_of_dvd_unit h) ha hb, }⟩ @[simp] lemma one_left (b : Ξ±) : multiplicity 1 b = ⊀ := is_unit_left b is_unit_one lemma one_right {a : Ξ±} (ha : Β¬is_unit a) : multiplicity a 1 = 0 := is_unit_right ha is_unit_one @[simp] lemma get_one_right {a : Ξ±} (ha : finite a 1) : get (multiplicity a 1) ha = 0 := get_eq_iff_eq_some.2 (eq_some_iff.2 ⟨dvd_refl _, by simpa [is_unit_iff_dvd_one.symm] using not_unit_of_finite ha⟩) @[simp] lemma unit_left (a : Ξ±) (u : units Ξ±) : multiplicity (u : Ξ±) a = ⊀ := is_unit_left a u.is_unit lemma unit_right {a : Ξ±} (ha : Β¬is_unit a) (u : units Ξ±) : multiplicity a u = 0 := is_unit_right ha u.is_unit lemma multiplicity_eq_zero_of_not_dvd {a b : Ξ±} (ha : Β¬a ∣ b) : multiplicity a b = 0 := eq_some_iff.2 (by simpa) lemma eq_top_iff_not_finite {a b : Ξ±} : multiplicity a b = ⊀ ↔ Β¬ finite a b := roption.eq_none_iff' open_locale classical lemma multiplicity_le_multiplicity_iff {a b c d : Ξ±} : multiplicity a b ≀ multiplicity c d ↔ (βˆ€ n : β„•, a ^ n ∣ b β†’ c ^ n ∣ d) := ⟨λ h n hab, (pow_dvd_of_le_multiplicity (le_trans (le_multiplicity_of_pow_dvd hab) h)), Ξ» h, if hab : finite a b then by rw [← enat.coe_get (finite_iff_dom.1 hab)]; exact le_multiplicity_of_pow_dvd (h _ (pow_multiplicity_dvd _)) else have βˆ€ n : β„•, c ^ n ∣ d, from Ξ» n, h n (not_finite_iff_forall.1 hab _), by rw [eq_top_iff_not_finite.2 hab, eq_top_iff_not_finite.2 (not_finite_iff_forall.2 this)]⟩ lemma multiplicity_le_multiplicity_of_dvd_left {a b c : Ξ±} (hdvd : a ∣ b) : multiplicity b c ≀ multiplicity a c := multiplicity_le_multiplicity_iff.2 $ Ξ» n h, dvd_trans (pow_dvd_pow_of_dvd hdvd n) h lemma eq_of_associated_left {a b c : Ξ±} (h : associated a b) : multiplicity b c = multiplicity a c := le_antisymm (multiplicity_le_multiplicity_of_dvd_left (dvd_of_associated h)) (multiplicity_le_multiplicity_of_dvd_left (dvd_of_associated h.symm)) lemma multiplicity_le_multiplicity_of_dvd_right {a b c : Ξ±} (h : b ∣ c) : multiplicity a b ≀ multiplicity a c := multiplicity_le_multiplicity_iff.2 $ Ξ» n hb, dvd.trans hb h lemma eq_of_associated_right {a b c : Ξ±} (h : associated b c) : multiplicity a b = multiplicity a c := le_antisymm (multiplicity_le_multiplicity_of_dvd_right (dvd_of_associated h)) (multiplicity_le_multiplicity_of_dvd_right (dvd_of_associated h.symm)) lemma dvd_of_multiplicity_pos {a b : Ξ±} (h : (0 : enat) < multiplicity a b) : a ∣ b := by rw [← pow_one a]; exact pow_dvd_of_le_multiplicity (enat.pos_iff_one_le.1 h) lemma dvd_iff_multiplicity_pos {a b : Ξ±} : (0 : enat) < multiplicity a b ↔ a ∣ b := ⟨dvd_of_multiplicity_pos, Ξ» hdvd, lt_of_le_of_ne (zero_le _) (Ξ» heq, is_greatest (show multiplicity a b < 1, from heq β–Έ enat.coe_lt_coe.mpr zero_lt_one) (by rwa pow_one a))⟩ lemma finite_nat_iff {a b : β„•} : finite a b ↔ (a β‰  1 ∧ 0 < b) := begin rw [← not_iff_not, not_finite_iff_forall, not_and_distrib, ne.def, not_not, not_lt, nat.le_zero_iff], exact ⟨λ h, or_iff_not_imp_right.2 (Ξ» hb, have ha : a β‰  0, from Ξ» ha, by simpa [ha] using h 1, by_contradiction (Ξ» ha1 : a β‰  1, have ha_gt_one : 1 < a, from lt_of_not_ge (Ξ» ha', by { clear h, revert ha ha1, dec_trivial! }), not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero hb) (h b)) (lt_pow_self ha_gt_one b))), Ξ» h, by cases h; simp *⟩ end end comm_monoid section comm_monoid_with_zero variable [comm_monoid_with_zero Ξ±] lemma ne_zero_of_finite {a b : Ξ±} (h : finite a b) : b β‰  0 := let ⟨n, hn⟩ := h in Ξ» hb, by simpa [hb] using hn variable [decidable_rel ((∣) : Ξ± β†’ Ξ± β†’ Prop)] @[simp] protected lemma zero (a : Ξ±) : multiplicity a 0 = ⊀ := roption.eq_none_iff.2 (Ξ» n ⟨⟨k, hk⟩, _⟩, hk (dvd_zero _)) @[simp] lemma multiplicity_zero_eq_zero_of_ne_zero (a : Ξ±) (ha : a β‰  0) : multiplicity 0 a = 0 := begin apply multiplicity.multiplicity_eq_zero_of_not_dvd, rwa zero_dvd_iff, end end comm_monoid_with_zero section comm_semiring variables [comm_semiring Ξ±] [decidable_rel ((∣) : Ξ± β†’ Ξ± β†’ Prop)] lemma min_le_multiplicity_add {p a b : Ξ±} : min (multiplicity p a) (multiplicity p b) ≀ multiplicity p (a + b) := (le_total (multiplicity p a) (multiplicity p b)).elim (Ξ» h, by rw [min_eq_left h, multiplicity_le_multiplicity_iff]; exact Ξ» n hn, dvd_add hn (multiplicity_le_multiplicity_iff.1 h n hn)) (Ξ» h, by rw [min_eq_right h, multiplicity_le_multiplicity_iff]; exact Ξ» n hn, dvd_add (multiplicity_le_multiplicity_iff.1 h n hn) hn) end comm_semiring section comm_ring variables [comm_ring Ξ±] [decidable_rel ((∣) : Ξ± β†’ Ξ± β†’ Prop)] open_locale classical @[simp] protected lemma neg (a b : Ξ±) : multiplicity a (-b) = multiplicity a b := roption.ext' (by simp only [multiplicity, enat.find, dvd_neg]) (Ξ» h₁ hβ‚‚, enat.coe_inj.1 (by rw [enat.coe_get]; exact eq.symm (unique ((dvd_neg _ _).2 (pow_multiplicity_dvd _)) (mt (dvd_neg _ _).1 (is_greatest' _ (lt_succ_self _)))))) lemma multiplicity_add_of_gt {p a b : Ξ±} (h : multiplicity p b < multiplicity p a) : multiplicity p (a + b) = multiplicity p b := begin apply le_antisymm, { apply enat.le_of_lt_add_one, cases enat.ne_top_iff.mp (enat.ne_top_of_lt h) with k hk, rw [hk], rw_mod_cast [multiplicity_lt_iff_neg_dvd], intro h_dvd, rw [← dvd_add_iff_right] at h_dvd, apply multiplicity.is_greatest _ h_dvd, rw [hk], apply_mod_cast nat.lt_succ_self, rw [pow_dvd_iff_le_multiplicity, enat.coe_add, ← hk], exact enat.add_one_le_of_lt h }, { convert min_le_multiplicity_add, rw [min_eq_right (le_of_lt h)] } end lemma multiplicity_sub_of_gt {p a b : Ξ±} (h : multiplicity p b < multiplicity p a) : multiplicity p (a - b) = multiplicity p b := by { rw [sub_eq_add_neg, multiplicity_add_of_gt]; rwa [multiplicity.neg] } lemma multiplicity_add_eq_min {p a b : Ξ±} (h : multiplicity p a β‰  multiplicity p b) : multiplicity p (a + b) = min (multiplicity p a) (multiplicity p b) := begin rcases lt_trichotomy (multiplicity p a) (multiplicity p b) with hab|hab|hab, { rw [add_comm, multiplicity_add_of_gt hab, min_eq_left], exact le_of_lt hab }, { contradiction }, { rw [multiplicity_add_of_gt hab, min_eq_right], exact le_of_lt hab}, end end comm_ring section comm_cancel_monoid_with_zero variables [comm_cancel_monoid_with_zero Ξ±] lemma finite_mul_aux {p : Ξ±} (hp : prime p) : βˆ€ {n m : β„•} {a b : Ξ±}, Β¬p ^ (n + 1) ∣ a β†’ Β¬p ^ (m + 1) ∣ b β†’ Β¬p ^ (n + m + 1) ∣ a * b | n m := Ξ» a b ha hb ⟨s, hs⟩, have p ∣ a * b, from ⟨p ^ (n + m) * s, by simp [hs, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩, (hp.2.2 a b this).elim (Ξ» ⟨x, hx⟩, have hn0 : 0 < n, from nat.pos_of_ne_zero (Ξ» hn0, by clear _fun_match _fun_match; simpa [hx, hn0] using ha), have wf : (n - 1) < n, from nat.sub_lt_self hn0 dec_trivial, have hpx : Β¬ p ^ (n - 1 + 1) ∣ x, from Ξ» ⟨y, hy⟩, ha (hx.symm β–Έ ⟨y, mul_right_cancel' hp.1 $ by rw [nat.sub_add_cancel hn0] at hy; simp [hy, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩), have 1 ≀ n + m, from le_trans hn0 (le_add_right n m), finite_mul_aux hpx hb ⟨s, mul_right_cancel' hp.1 begin rw [← nat.sub_add_comm hn0, nat.sub_add_cancel this], clear _fun_match _fun_match finite_mul_aux, simp [*, mul_comm, mul_assoc, mul_left_comm, pow_add] at * end⟩) (Ξ» ⟨x, hx⟩, have hm0 : 0 < m, from nat.pos_of_ne_zero (Ξ» hm0, by clear _fun_match _fun_match; simpa [hx, hm0] using hb), have wf : (m - 1) < m, from nat.sub_lt_self hm0 dec_trivial, have hpx : Β¬ p ^ (m - 1 + 1) ∣ x, from Ξ» ⟨y, hy⟩, hb (hx.symm β–Έ ⟨y, mul_right_cancel' hp.1 $ by rw [nat.sub_add_cancel hm0] at hy; simp [hy, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩), finite_mul_aux ha hpx ⟨s, mul_right_cancel' hp.1 begin rw [add_assoc, nat.sub_add_cancel hm0], clear _fun_match _fun_match finite_mul_aux, simp [*, mul_comm, mul_assoc, mul_left_comm, pow_add] at * end⟩) lemma finite_mul {p a b : Ξ±} (hp : prime p) : finite p a β†’ finite p b β†’ finite p (a * b) := Ξ» ⟨n, hn⟩ ⟨m, hm⟩, ⟨n + m, finite_mul_aux hp hn hm⟩ lemma finite_mul_iff {p a b : Ξ±} (hp : prime p) : finite p (a * b) ↔ finite p a ∧ finite p b := ⟨λ h, ⟨finite_of_finite_mul_right h, finite_of_finite_mul_left h⟩, Ξ» h, finite_mul hp h.1 h.2⟩ lemma finite_pow {p a : Ξ±} (hp : prime p) : Ξ  {k : β„•} (ha : finite p a), finite p (a ^ k) | 0 ha := ⟨0, by simp [mt is_unit_iff_dvd_one.2 hp.2.1]⟩ | (k+1) ha := by rw [pow_succ]; exact finite_mul hp ha (finite_pow ha) variable [decidable_rel ((∣) : Ξ± β†’ Ξ± β†’ Prop)] @[simp] lemma multiplicity_self {a : Ξ±} (ha : Β¬is_unit a) (ha0 : a β‰  0) : multiplicity a a = 1 := eq_some_iff.2 ⟨by simp, Ξ» ⟨b, hb⟩, ha (is_unit_iff_dvd_one.2 ⟨b, mul_left_cancel' ha0 $ by clear _fun_match; simpa [pow_succ, mul_assoc] using hb⟩)⟩ @[simp] lemma get_multiplicity_self {a : Ξ±} (ha : finite a a) : get (multiplicity a a) ha = 1 := roption.get_eq_iff_eq_some.2 (eq_some_iff.2 ⟨by simp, Ξ» ⟨b, hb⟩, by rw [← mul_one a, pow_add, pow_one, mul_assoc, mul_assoc, mul_right_inj' (ne_zero_of_finite ha)] at hb; exact mt is_unit_iff_dvd_one.2 (not_unit_of_finite ha) ⟨b, by clear _fun_match; simp * at *⟩⟩) protected lemma mul' {p a b : Ξ±} (hp : prime p) (h : (multiplicity p (a * b)).dom) : get (multiplicity p (a * b)) h = get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2 := have hdiva : p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 ∣ a, from pow_multiplicity_dvd _, have hdivb : p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2 ∣ b, from pow_multiplicity_dvd _, have hpoweq : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2) = p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 * p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2, by simp [pow_add], have hdiv : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2) ∣ a * b, by rw [hpoweq]; apply mul_dvd_mul; assumption, have hsucc : Β¬p ^ ((get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2) + 1) ∣ a * b, from Ξ» h, not_or (is_greatest' _ (lt_succ_self _)) (is_greatest' _ (lt_succ_self _)) (by exact succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul hp hdiva hdivb h), by rw [← enat.coe_inj, enat.coe_get, eq_some_iff]; exact ⟨hdiv, hsucc⟩ open_locale classical protected lemma mul {p a b : Ξ±} (hp : prime p) : multiplicity p (a * b) = multiplicity p a + multiplicity p b := if h : finite p a ∧ finite p b then by rw [← enat.coe_get (finite_iff_dom.1 h.1), ← enat.coe_get (finite_iff_dom.1 h.2), ← enat.coe_get (finite_iff_dom.1 (finite_mul hp h.1 h.2)), ← enat.coe_add, enat.coe_inj, multiplicity.mul' hp]; refl else begin rw [eq_top_iff_not_finite.2 (mt (finite_mul_iff hp).1 h)], cases not_and_distrib.1 h with h h; simp [eq_top_iff_not_finite.2 h] end lemma finset.prod {Ξ² : Type*} {p : Ξ±} (hp : prime p) (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : multiplicity p (∏ x in s, f x) = βˆ‘ x in s, multiplicity p (f x) := begin classical, induction s using finset.induction with a s has ih h, { simp only [finset.sum_empty, finset.prod_empty], convert one_right hp.not_unit }, { simp [has, ← ih], convert multiplicity.mul hp } end protected lemma pow' {p a : Ξ±} (hp : prime p) (ha : finite p a) : βˆ€ {k : β„•}, get (multiplicity p (a ^ k)) (finite_pow hp ha) = k * get (multiplicity p a) ha | 0 := by dsimp [pow_zero]; simp [one_right hp.not_unit]; refl | (k+1) := by dsimp only [pow_succ]; erw [multiplicity.mul' hp, pow', add_mul, one_mul, add_comm] lemma pow {p a : Ξ±} (hp : prime p) : βˆ€ {k : β„•}, multiplicity p (a ^ k) = k β€’β„• (multiplicity p a) | 0 := by simp [one_right hp.not_unit] | (succ k) := by simp [pow_succ, succ_nsmul, pow, multiplicity.mul hp] lemma multiplicity_pow_self {p : Ξ±} (h0 : p β‰  0) (hu : Β¬ is_unit p) (n : β„•) : multiplicity p (p ^ n) = n := by { rw [eq_some_iff], use dvd_refl _, rw [pow_dvd_pow_iff h0 hu], apply nat.not_succ_le_self } lemma multiplicity_pow_self_of_prime {p : Ξ±} (hp : prime p) (n : β„•) : multiplicity p (p ^ n) = n := multiplicity_pow_self hp.ne_zero hp.not_unit n end comm_cancel_monoid_with_zero section valuation variables {R : Type*} [integral_domain R] {p : R} [decidable_rel (has_dvd.dvd : R β†’ R β†’ Prop)] /-- `multiplicity` of a prime inan integral domain as an additive valuation to `enat`. -/ noncomputable def add_valuation (hp : prime p) : add_valuation R enat := add_valuation.of (multiplicity p) (multiplicity.zero _) (one_right hp.not_unit) (Ξ» _ _, min_le_multiplicity_add) (Ξ» a b, multiplicity.mul hp) @[simp] lemma add_valuation_apply {hp : prime p} {r : R} : add_valuation hp r = multiplicity p r := rfl end valuation end multiplicity section nat open multiplicity lemma multiplicity_eq_zero_of_coprime {p a b : β„•} (hp : p β‰  1) (hle : multiplicity p a ≀ multiplicity p b) (hab : nat.coprime a b) : multiplicity p a = 0 := begin rw [multiplicity_le_multiplicity_iff] at hle, rw [← nonpos_iff_eq_zero, ← not_lt, enat.pos_iff_one_le, ← enat.coe_one, ← pow_dvd_iff_le_multiplicity], assume h, have := nat.dvd_gcd h (hle _ h), rw [coprime.gcd_eq_one hab, nat.dvd_one, pow_one] at this, exact hp this end end nat
c1c8ba2641f77228ae89651078924aa52c3bf72f
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/algebra/category/Module/basic.lean
7d06f4ac61a6eba3f217de87ce0d9183527b4750
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,789
lean
/- Copyright (c) 2019 Robert A. Spencer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert A. Spencer, Markus Himmel -/ import algebra.category.Group.basic import category_theory.concrete_category import category_theory.limits.shapes.kernels import category_theory.linear import linear_algebra.basic /-! # The category of `R`-modules `Module.{v} R` is the category of bundled `R`-modules with carrier in the universe `v`. We show that it is preadditive and show that being an isomorphism, monomorphism and epimorphism is equivalent to being a linear equivalence, an injective linear map and a surjective linear map, respectively. ## Implementation details To construct an object in the category of `R`-modules from a type `M` with an instance of the `module` typeclass, write `of R M`. There is a coercion in the other direction. Similarly, there is a coercion from morphisms in `Module R` to linear maps. Unfortunately, Lean is not smart enough to see that, given an object `M : Module R`, the expression `of R M`, where we coerce `M` to the carrier type, is definitionally equal to `M` itself. This means that to go the other direction, i.e., from linear maps/equivalences to (iso)morphisms in the category of `R`-modules, we have to take care not to inadvertently end up with an `of R M` where `M` is already an object. Hence, given `f : M β†’β‚—[R] N`, * if `M N : Module R`, simply use `f`; * if `M : Module R` and `N` is an unbundled `R`-module, use `β†Ώf` or `as_hom_left f`; * if `M` is an unbundled `R`-module and `N : Module R`, use `β†Ύf` or `as_hom_right f`; * if `M` and `N` are unbundled `R`-modules, use `β†Ÿf` or `as_hom f`. Similarly, given `f : M ≃ₗ[R] N`, use `to_Module_iso`, `to_Module_iso'_left`, `to_Module_iso'_right` or `to_Module_iso'`, respectively. The arrow notations are localized, so you may have to `open_locale Module` to use them. Note that the notation for `as_hom_left` clashes with the notation used to promote functions between types to morphisms in the category `Type`, so to avoid confusion, it is probably a good idea to avoid having the locales `Module` and `category_theory.Type` open at the same time. If you get an error when trying to apply a theorem and the `convert` tactic produces goals of the form `M = of R M`, then you probably used an incorrect variant of `as_hom` or `to_Module_iso`. -/ open category_theory open category_theory.limits open category_theory.limits.walking_parallel_pair universes v u variables (R : Type u) [ring R] /-- The category of R-modules and their morphisms. Note that in the case of `R = β„€`, we can not impose here that the `β„€`-multiplication field from the module structure is defeq to the one coming from the `is_add_comm_group` structure (contrary to what we do for all module structures in mathlib), which creates some difficulties down the road. -/ structure Module := (carrier : Type v) [is_add_comm_group : add_comm_group carrier] [is_module : module R carrier] attribute [instance] Module.is_add_comm_group Module.is_module namespace Module instance : has_coe_to_sort (Module.{v} R) := { S := Type v, coe := Module.carrier } instance Module_category : category (Module.{v} R) := { hom := Ξ» M N, M β†’β‚—[R] N, id := Ξ» M, 1, comp := Ξ» A B C f g, g.comp f, id_comp' := Ξ» X Y f, linear_map.id_comp _, comp_id' := Ξ» X Y f, linear_map.comp_id _, assoc' := Ξ» W X Y Z f g h, linear_map.comp_assoc _ _ _ } instance Module_concrete_category : concrete_category.{v} (Module.{v} R) := { forget := { obj := Ξ» R, R, map := Ξ» R S f, (f : R β†’ S) }, forget_faithful := { } } instance has_forget_to_AddCommGroup : has_forgetβ‚‚ (Module R) AddCommGroup := { forgetβ‚‚ := { obj := Ξ» M, AddCommGroup.of M, map := Ξ» M₁ Mβ‚‚ f, linear_map.to_add_monoid_hom f } } /-- The object in the category of R-modules associated to an R-module -/ def of (X : Type v) [add_comm_group X] [module R X] : Module R := ⟨X⟩ /-- Typecheck a `linear_map` as a morphism in `Module R`. -/ def of_hom {R : Type u} [ring R] {X Y : Type u} [add_comm_group X] [module R X] [add_comm_group Y] [module R Y] (f : X β†’β‚—[R] Y) : of R X ⟢ of R Y := f instance : has_zero (Module R) := ⟨of R punit⟩ instance : inhabited (Module R) := ⟨0⟩ @[simp] lemma coe_of (X : Type u) [add_comm_group X] [module R X] : (of R X : Type u) = X := rfl variables {R} /-- Forgetting to the underlying type and then building the bundled object returns the original module. -/ @[simps] def of_self_iso (M : Module R) : Module.of R M β‰… M := { hom := πŸ™ M, inv := πŸ™ M } instance : subsingleton (of R punit) := by { rw coe_of R punit, apply_instance } instance : has_zero_object (Module.{v} R) := { zero := 0, unique_to := Ξ» X, { default := (0 : punit β†’β‚—[R] X), uniq := Ξ» _, linear_map.ext $ Ξ» x, have h : x = 0, from dec_trivial, by simp only [h, linear_map.map_zero]}, unique_from := Ξ» X, { default := (0 : X β†’β‚—[R] punit), uniq := Ξ» _, linear_map.ext $ Ξ» x, dec_trivial } } variables {R} {M N U : Module.{v} R} @[simp] lemma id_apply (m : M) : (πŸ™ M : M β†’ M) m = m := rfl @[simp] lemma coe_comp (f : M ⟢ N) (g : N ⟢ U) : ((f ≫ g) : M β†’ U) = g ∘ f := rfl lemma comp_def (f : M ⟢ N) (g : N ⟢ U) : f ≫ g = g.comp f := rfl end Module variables {R} variables {X₁ Xβ‚‚ : Type v} /-- Reinterpreting a linear map in the category of `R`-modules. -/ def Module.as_hom [add_comm_group X₁] [module R X₁] [add_comm_group Xβ‚‚] [module R Xβ‚‚] : (X₁ β†’β‚—[R] Xβ‚‚) β†’ (Module.of R X₁ ⟢ Module.of R Xβ‚‚) := id localized "notation `β†Ÿ` f : 1024 := Module.as_hom f" in Module /-- Reinterpreting a linear map in the category of `R`-modules. -/ def Module.as_hom_right [add_comm_group X₁] [module R X₁] {Xβ‚‚ : Module.{v} R} : (X₁ β†’β‚—[R] Xβ‚‚) β†’ (Module.of R X₁ ⟢ Xβ‚‚) := id localized "notation `β†Ύ` f : 1024 := Module.as_hom_right f" in Module /-- Reinterpreting a linear map in the category of `R`-modules. -/ def Module.as_hom_left {X₁ : Module.{v} R} [add_comm_group Xβ‚‚] [module R Xβ‚‚] : (X₁ β†’β‚—[R] Xβ‚‚) β†’ (X₁ ⟢ Module.of R Xβ‚‚) := id localized "notation `β†Ώ` f : 1024 := Module.as_hom_left f" in Module /-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. -/ @[simps] def linear_equiv.to_Module_iso {g₁ : add_comm_group X₁} {gβ‚‚ : add_comm_group Xβ‚‚} {m₁ : module R X₁} {mβ‚‚ : module R Xβ‚‚} (e : X₁ ≃ₗ[R] Xβ‚‚) : Module.of R X₁ β‰… Module.of R Xβ‚‚ := { hom := (e : X₁ β†’β‚—[R] Xβ‚‚), inv := (e.symm : Xβ‚‚ β†’β‚—[R] X₁), hom_inv_id' := begin ext, exact e.left_inv x, end, inv_hom_id' := begin ext, exact e.right_inv x, end, } /-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see `Module.of R M` is defeq to `M` when `M : Module R`. -/ @[simps] def linear_equiv.to_Module_iso' {M N : Module.{v} R} (i : M ≃ₗ[R] N) : M β‰… N := { hom := i, inv := i.symm, hom_inv_id' := linear_map.ext $ Ξ» x, by simp, inv_hom_id' := linear_map.ext $ Ξ» x, by simp } /-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see `Module.of R M` is defeq to `M` when `M : Module R`. -/ @[simps] def linear_equiv.to_Module_iso'_left {X₁ : Module.{v} R} {gβ‚‚ : add_comm_group Xβ‚‚} {mβ‚‚ : module R Xβ‚‚} (e : X₁ ≃ₗ[R] Xβ‚‚) : X₁ β‰… Module.of R Xβ‚‚ := { hom := (e : X₁ β†’β‚—[R] Xβ‚‚), inv := (e.symm : Xβ‚‚ β†’β‚—[R] X₁), hom_inv_id' := linear_map.ext $ Ξ» x, by simp, inv_hom_id' := linear_map.ext $ Ξ» x, by simp } /-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see `Module.of R M` is defeq to `M` when `M : Module R`. -/ @[simps] def linear_equiv.to_Module_iso'_right {g₁ : add_comm_group X₁} {m₁ : module R X₁} {Xβ‚‚ : Module.{v} R} (e : X₁ ≃ₗ[R] Xβ‚‚) : Module.of R X₁ β‰… Xβ‚‚ := { hom := (e : X₁ β†’β‚—[R] Xβ‚‚), inv := (e.symm : Xβ‚‚ β†’β‚—[R] X₁), hom_inv_id' := linear_map.ext $ Ξ» x, by simp, inv_hom_id' := linear_map.ext $ Ξ» x, by simp } namespace category_theory.iso /-- Build a `linear_equiv` from an isomorphism in the category `Module R`. -/ @[simps] def to_linear_equiv {X Y : Module R} (i : X β‰… Y) : X ≃ₗ[R] Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := by tidy, right_inv := by tidy, map_add' := by tidy, map_smul' := by tidy, }. end category_theory.iso /-- linear equivalences between `module`s are the same as (isomorphic to) isomorphisms in `Module` -/ @[simps] def linear_equiv_iso_Module_iso {X Y : Type u} [add_comm_group X] [add_comm_group Y] [module R X] [module R Y] : (X ≃ₗ[R] Y) β‰… (Module.of R X β‰… Module.of R Y) := { hom := Ξ» e, e.to_Module_iso, inv := Ξ» i, i.to_linear_equiv, } namespace Module instance : preadditive (Module.{v} R) := { add_comp' := Ξ» P Q R f f' g, show (f + f') ≫ g = f ≫ g + f' ≫ g, by { ext, simp }, comp_add' := Ξ» P Q R f g g', show f ≫ (g + g') = f ≫ g + f ≫ g', by { ext, simp } } section variables {S : Type u} [comm_ring S] instance : linear S (Module.{v} S) := { hom_module := Ξ» X Y, linear_map.module, smul_comp' := by { intros, ext, simp }, comp_smul' := by { intros, ext, simp }, } end end Module instance (M : Type u) [add_comm_group M] [module R M] : has_coe (submodule R M) (Module R) := ⟨ Ξ» N, Module.of R N ⟩
50d2d66f3aef49edc8ac60f3eb970a08f7e7b170
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/algebra/group_power.lean
bafe98b2a2250204e19b03afcba6806bb17cd63f
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
25,653
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis The power operation on monoids and groups. We separate this from group, because it depends on nat, which in turn depends on other parts of algebra. We have "pow a n" for natural number powers, and "gpow a i" for integer powers. The notation a^n is used for the first, but users can locally redefine it to gpow when needed. Note: power adopts the convention that 0^0=1. -/ import algebra.group import data.int.basic data.list.basic universes u v variable {Ξ± : Type u} @[simp] theorem inv_one [division_ring Ξ±] : (1⁻¹ : Ξ±) = 1 := by rw [inv_eq_one_div, one_div_one] @[simp] theorem inv_inv' [discrete_field Ξ±] {a:Ξ±} : a⁻¹⁻¹ = a := by rw [inv_eq_one_div, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_one] /-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/ def monoid.pow [monoid Ξ±] (a : Ξ±) : β„• β†’ Ξ± | 0 := 1 | (n+1) := a * monoid.pow n def add_monoid.smul [add_monoid Ξ±] (n : β„•) (a : Ξ±) : Ξ± := @monoid.pow (multiplicative Ξ±) _ a n precedence `β€’`:70 local infix ` β€’ ` := add_monoid.smul @[priority 5] instance monoid.has_pow [monoid Ξ±] : has_pow Ξ± β„• := ⟨monoid.pow⟩ /- monoid -/ section monoid variables [monoid Ξ±] {Ξ² : Type u} [add_monoid Ξ²] @[simp] theorem pow_zero (a : Ξ±) : a^0 = 1 := rfl @[simp] theorem add_monoid.zero_smul (a : Ξ²) : 0 β€’ a = 0 := rfl theorem pow_succ (a : Ξ±) (n : β„•) : a^(n+1) = a * a^n := rfl theorem succ_smul (a : Ξ²) (n : β„•) : (n+1)β€’a = a + nβ€’a := rfl @[simp] theorem pow_one (a : Ξ±) : a^1 = a := mul_one _ @[simp] theorem add_monoid.one_smul (a : Ξ²) : 1β€’a = a := add_zero _ theorem pow_mul_comm' (a : Ξ±) (n : β„•) : a^n * a = a * a^n := by induction n with n ih; [rw [pow_zero, one_mul, mul_one], rw [pow_succ, mul_assoc, ih]] theorem smul_add_comm' : βˆ€ (a : Ξ²) (n : β„•), nβ€’a + a = a + nβ€’a := @pow_mul_comm' (multiplicative Ξ²) _ theorem pow_succ' (a : Ξ±) (n : β„•) : a^(n+1) = a^n * a := by rw [pow_succ, pow_mul_comm'] theorem succ_smul' (a : Ξ²) (n : β„•) : (n+1)β€’a = nβ€’a + a := by rw [succ_smul, smul_add_comm'] theorem pow_two (a : Ξ±) : a^2 = a * a := show a*(a*1)=a*a, by rw mul_one theorem two_smul (a : Ξ²) : 2β€’a = a + a := show a+(a+0)=a+a, by rw add_zero theorem pow_add (a : Ξ±) (m n : β„•) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [add_zero, pow_zero, mul_one], rw [pow_succ, ← pow_mul_comm', ← mul_assoc, ← ih, ← pow_succ']]; refl theorem add_monoid.add_smul : βˆ€ (a : Ξ²) (m n : β„•), (m + n)β€’a = mβ€’a + nβ€’a := @pow_add (multiplicative Ξ²) _ @[simp] theorem one_pow (n : β„•) : (1 : Ξ±)^n = (1:Ξ±) := by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]] @[simp] theorem add_monoid.smul_zero (n : β„•) : nβ€’(0 : Ξ²) = (0:Ξ²) := by induction n with n ih; [refl, rw [succ_smul, ih, zero_add]] theorem pow_mul (a : Ξ±) (m n : β„•) : a^(m * n) = (a^m)^n := by induction n with n ih; [rw mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl theorem add_monoid.mul_smul' : βˆ€ (a : Ξ²) (m n : β„•), m * n β€’ a = nβ€’(mβ€’a) := @pow_mul (multiplicative Ξ²) _ theorem pow_mul' (a : Ξ±) (m n : β„•) : a^(m * n) = (a^n)^m := by rw [mul_comm, pow_mul] theorem add_monoid.mul_smul (a : Ξ²) (m n : β„•) : m * n β€’ a = mβ€’(nβ€’a) := by rw [mul_comm, add_monoid.mul_smul'] @[simp] theorem add_monoid.smul_one [has_one Ξ²] : βˆ€ n : β„•, n β€’ (1 : Ξ²) = n := nat.eq_cast _ (add_monoid.zero_smul _) (add_monoid.one_smul _) (add_monoid.add_smul _) theorem pow_bit0 (a : Ξ±) (n : β„•) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ theorem bit0_smul (a : Ξ²) (n : β„•) : bit0 n β€’ a = nβ€’a + nβ€’a := add_monoid.add_smul _ _ _ theorem pow_bit1 (a : Ξ±) (n : β„•) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] theorem bit1_smul : βˆ€ (a : Ξ²) (n : β„•), bit1 n β€’ a = nβ€’a + nβ€’a + a := @pow_bit1 (multiplicative Ξ²) _ theorem pow_mul_comm (a : Ξ±) (m n : β„•) : a^m * a^n = a^n * a^m := by rw [←pow_add, ←pow_add, add_comm] theorem smul_add_comm : βˆ€ (a : Ξ²) (m n : β„•), mβ€’a + nβ€’a = nβ€’a + mβ€’a := @pow_mul_comm (multiplicative Ξ²) _ @[simp] theorem list.prod_repeat (a : Ξ±) (n : β„•) : (list.repeat a n).prod = a ^ n := by induction n with n ih; [refl, rw [list.repeat_succ, list.prod_cons, ih]]; refl @[simp] theorem list.sum_repeat : βˆ€ (a : Ξ²) (n : β„•), (list.repeat a n).sum = n β€’ a := @list.prod_repeat (multiplicative Ξ²) _ @[simp] lemma units.coe_pow (u : units Ξ±) (n : β„•) : ((u ^ n : units Ξ±) : Ξ±) = u ^ n := by induction n; simp [*, pow_succ] end monoid namespace is_monoid_hom variables {Ξ² : Type v} [monoid Ξ±] [monoid Ξ²] (f : Ξ± β†’ Ξ²) [is_monoid_hom f] theorem map_pow (a : Ξ±) : βˆ€(n : β„•), f (a ^ n) = (f a) ^ n | 0 := is_monoid_hom.map_one f | (nat.succ n) := by rw [pow_succ, is_monoid_hom.map_mul f, map_pow n]; refl end is_monoid_hom namespace is_add_monoid_hom variables {Ξ² : Type*} [add_monoid Ξ±] [add_monoid Ξ²] (f : Ξ± β†’ Ξ²) [is_add_monoid_hom f] theorem map_smul (a : Ξ±) : βˆ€(n : β„•), f (n β€’ a) = n β€’ (f a) | 0 := is_add_monoid_hom.map_zero f | (nat.succ n) := by rw [succ_smul, is_add_monoid_hom.map_add f, map_smul n]; refl end is_add_monoid_hom @[simp] theorem nat.pow_eq_pow (p q : β„•) : @has_pow.pow _ _ monoid.has_pow p q = p ^ q := by induction q with q ih; [refl, rw [nat.pow_succ, pow_succ, mul_comm, ih]] @[simp] theorem nat.smul_eq_mul (m n : β„•) : m β€’ n = m * n := by induction m with m ih; [rw [add_monoid.zero_smul, zero_mul], rw [succ_smul', ih, nat.succ_mul]] /- commutative monoid -/ section comm_monoid variables [comm_monoid Ξ±] {Ξ² : Type*} [add_comm_monoid Ξ²] theorem mul_pow (a b : Ξ±) (n : β„•) : (a * b)^n = a^n * b^n := by induction n with n ih; [exact (mul_one _).symm, simp only [pow_succ, ih, mul_assoc, mul_left_comm]] theorem add_monoid.smul_add : βˆ€ (a b : Ξ²) (n : β„•), nβ€’(a + b) = nβ€’a + nβ€’b := @mul_pow (multiplicative Ξ²) _ instance pow.is_monoid_hom (n : β„•) : is_monoid_hom ((^ n) : Ξ± β†’ Ξ±) := { map_mul := Ξ» _ _, mul_pow _ _ _, map_one := one_pow _ } instance add_monoid.smul.is_add_monoid_hom (n : β„•) : is_add_monoid_hom (add_monoid.smul n : Ξ² β†’ Ξ²) := { map_add := Ξ» _ _, add_monoid.smul_add _ _ _, map_zero := add_monoid.smul_zero _ } end comm_monoid section group variables [group Ξ±] {Ξ² : Type*} [add_group Ξ²] section nat @[simp] theorem inv_pow (a : Ξ±) (n : β„•) : (a⁻¹)^n = (a^n)⁻¹ := by induction n with n ih; [exact one_inv.symm, rw [pow_succ', pow_succ, ih, mul_inv_rev]] @[simp] theorem add_monoid.neg_smul : βˆ€ (a : Ξ²) (n : β„•), nβ€’(-a) = -(nβ€’a) := @inv_pow (multiplicative Ξ²) _ theorem pow_sub (a : Ξ±) {m n : β„•} (h : m β‰₯ n) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 theorem add_monoid.smul_sub : βˆ€ (a : Ξ²) {m n : β„•}, m β‰₯ n β†’ (m - n)β€’a = mβ€’a - nβ€’a := @pow_sub (multiplicative Ξ²) _ theorem pow_inv_comm (a : Ξ±) (m n : β„•) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := by rw inv_pow; exact inv_comm_of_comm (pow_mul_comm _ _ _) theorem add_monoid.smul_neg_comm : βˆ€ (a : Ξ²) (m n : β„•), mβ€’(-a) + nβ€’a = nβ€’a + mβ€’(-a) := @pow_inv_comm (multiplicative Ξ²) _ end nat open int /-- The power operation in a group. This extends `monoid.pow` to negative integers with the definition `a^(-n) = (a^n)⁻¹`. -/ def gpow (a : Ξ±) : β„€ β†’ Ξ± | (of_nat n) := a^n | -[1+n] := (a^(nat.succ n))⁻¹ def gsmul (n : β„€) (a : Ξ²) : Ξ² := @gpow (multiplicative Ξ²) _ a n @[priority 10] instance group.has_pow : has_pow Ξ± β„€ := ⟨gpow⟩ local infix ` β€’ `:70 := gsmul local infix ` β€’β„• `:70 := add_monoid.smul @[simp] theorem gpow_coe_nat (a : Ξ±) (n : β„•) : a ^ (n:β„€) = a ^ n := rfl @[simp] theorem gsmul_coe_nat (a : Ξ²) (n : β„•) : (n:β„€) β€’ a = n β€’β„• a := rfl @[simp] theorem gpow_of_nat (a : Ξ±) (n : β„•) : a ^ of_nat n = a ^ n := rfl @[simp] theorem gsmul_of_nat (a : Ξ²) (n : β„•) : of_nat n β€’ a = n β€’β„• a := rfl @[simp] theorem gpow_neg_succ (a : Ξ±) (n : β„•) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl @[simp] theorem gsmul_neg_succ (a : Ξ²) (n : β„•) : -[1+n] β€’ a = - (n.succ β€’β„• a) := rfl local attribute [ematch] le_of_lt open nat @[simp] theorem gpow_zero (a : Ξ±) : a ^ (0:β„€) = 1 := rfl @[simp] theorem zero_gsmul (a : Ξ²) : (0:β„€) β€’ a = 0 := rfl @[simp] theorem gpow_one (a : Ξ±) : a ^ (1:β„€) = a := mul_one _ @[simp] theorem one_gsmul (a : Ξ²) : (1:β„€) β€’ a = a := add_zero _ @[simp] theorem one_gpow : βˆ€ (n : β„€), (1 : Ξ±) ^ n = 1 | (n : β„•) := one_pow _ | -[1+ n] := show _⁻¹=(1:Ξ±), by rw [_root_.one_pow, one_inv] @[simp] theorem gsmul_zero : βˆ€ (n : β„€), n β€’ (0 : Ξ²) = 0 := @one_gpow (multiplicative Ξ²) _ @[simp] theorem gpow_neg (a : Ξ±) : βˆ€ (n : β„€), a ^ -n = (a ^ n)⁻¹ | (n+1:β„•) := rfl | 0 := one_inv.symm | -[1+ n] := (inv_inv _).symm @[simp] theorem neg_gsmul : βˆ€ (a : Ξ²) (n : β„€), -n β€’ a = -(n β€’ a) := @gpow_neg (multiplicative Ξ²) _ theorem gpow_neg_one (x : Ξ±) : x ^ (-1:β„€) = x⁻¹ := congr_arg has_inv.inv $ pow_one x theorem neg_one_gsmul (x : Ξ²) : (-1:β„€) β€’ x = -x := congr_arg has_neg.neg $ add_monoid.one_smul x theorem inv_gpow (a : Ξ±) : βˆ€n:β„€, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : β„•) := inv_pow a n | -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1) private lemma gpow_add_aux (a : Ξ±) (m n : nat) : a ^ ((of_nat m) + -[1+n]) = a ^ of_nat m * a ^ -[1+n] := or.elim (nat.lt_or_ge m (nat.succ n)) (assume h1 : m < succ n, have h2 : m ≀ n, from le_of_lt_succ h1, suffices a ^ -[1+ n-m] = a ^ of_nat m * a ^ -[1+n], by rwa [of_nat_add_neg_succ_of_nat_of_lt h1], show (a ^ nat.succ (n - m))⁻¹ = a ^ of_nat m * a ^ -[1+n], by rw [← succ_sub h2, pow_sub _ (le_of_lt h1), mul_inv_rev, inv_inv]; refl) (assume : m β‰₯ succ n, suffices a ^ (of_nat (m - succ n)) = (a ^ (of_nat m)) * (a ^ -[1+ n]), by rw [of_nat_add_neg_succ_of_nat_of_ge]; assumption, suffices a ^ (m - succ n) = a ^ m * (a ^ n.succ)⁻¹, from this, by rw pow_sub; assumption) theorem gpow_add (a : Ξ±) : βˆ€ (i j : β„€), a ^ (i + j) = a ^ i * a ^ j | (of_nat m) (of_nat n) := pow_add _ _ _ | (of_nat m) -[1+n] := gpow_add_aux _ _ _ | -[1+m] (of_nat n) := by rw [add_comm, gpow_add_aux, gpow_neg_succ, gpow_of_nat, ← inv_pow, ← pow_inv_comm] | -[1+m] -[1+n] := suffices (a ^ (m + succ (succ n)))⁻¹ = (a ^ m.succ)⁻¹ * (a ^ n.succ)⁻¹, from this, by rw [← succ_add_eq_succ_add, add_comm, _root_.pow_add, mul_inv_rev] theorem add_gsmul : βˆ€ (a : Ξ²) (i j : β„€), (i + j) β€’ a = i β€’ a + j β€’ a := @gpow_add (multiplicative Ξ²) _ theorem gpow_add_one (a : Ξ±) (i : β„€) : a ^ (i + 1) = a ^ i * a := by rw [gpow_add, gpow_one] theorem add_one_gsmul : βˆ€ (a : Ξ²) (i : β„€), (i + 1) β€’ a = i β€’ a + a := @gpow_add_one (multiplicative Ξ²) _ theorem gpow_one_add (a : Ξ±) (i : β„€) : a ^ (1 + i) = a * a ^ i := by rw [gpow_add, gpow_one] theorem one_add_gsmul : βˆ€ (a : Ξ²) (i : β„€), (1 + i) β€’ a = a + i β€’ a := @gpow_one_add (multiplicative Ξ²) _ theorem gpow_mul_comm (a : Ξ±) (i j : β„€) : a ^ i * a ^ j = a ^ j * a ^ i := by rw [← gpow_add, ← gpow_add, add_comm] theorem gsmul_add_comm : βˆ€ (a : Ξ²) (i j), i β€’ a + j β€’ a = j β€’ a + i β€’ a := @gpow_mul_comm (multiplicative Ξ²) _ theorem gpow_mul (a : Ξ±) : βˆ€ m n : β„€, a ^ (m * n) = (a ^ m) ^ n | (m : β„•) (n : β„•) := pow_mul _ _ _ | (m : β„•) -[1+ n] := (gpow_neg _ (m * succ n)).trans $ show (a ^ (m * succ n))⁻¹ = _, by rw pow_mul; refl | -[1+ m] (n : β„•) := (gpow_neg _ (succ m * n)).trans $ show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow]; refl | -[1+ m] -[1+ n] := (pow_mul a (succ m) (succ n)).trans $ show _ = (_⁻¹^_)⁻¹, by rw [inv_pow, inv_inv] theorem gsmul_mul' : βˆ€ (a : Ξ²) (m n : β„€), m * n β€’ a = n β€’ (m β€’ a) := @gpow_mul (multiplicative Ξ²) _ theorem gpow_mul' (a : Ξ±) (m n : β„€) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, gpow_mul] theorem gsmul_mul (a : Ξ²) (m n : β„€) : m * n β€’ a = m β€’ (n β€’ a) := by rw [mul_comm, gsmul_mul'] theorem gpow_bit0 (a : Ξ±) (n : β„€) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _ theorem bit0_gsmul (a : Ξ²) (n : β„€) : bit0 n β€’ a = n β€’ a + n β€’ a := gpow_add _ _ _ theorem gpow_bit1 (a : Ξ±) (n : β„€) : a ^ bit1 n = a ^ n * a ^ n * a := by rw [bit1, gpow_add]; simp [gpow_bit0] theorem bit1_gsmul : βˆ€ (a : Ξ²) (n : β„€), bit1 n β€’ a = n β€’ a + n β€’ a + a := @gpow_bit1 (multiplicative Ξ²) _ theorem gsmul_neg (a : Ξ²) (n : β„€) : gsmul n (- a) = - gsmul n a := begin induction n using int.induction_on with z ih z ih, { simp }, { rw [add_comm] {occs := occurrences.pos [1]}, simp [add_gsmul, ih, -add_comm] }, { rw [sub_eq_add_neg, add_comm] {occs := occurrences.pos [1]}, simp [ih, add_gsmul, neg_gsmul, -add_comm] } end end group namespace is_group_hom variables {Ξ² : Type v} [group Ξ±] [group Ξ²] (f : Ξ± β†’ Ξ²) [is_group_hom f] theorem map_pow (a : Ξ±) (n : β„•) : f (a ^ n) = f a ^ n := is_monoid_hom.map_pow f a n theorem map_gpow (a : Ξ±) (n : β„€) : f (a ^ n) = f a ^ n := by cases n; [exact is_group_hom.map_pow f _ _, exact (is_group_hom.map_inv f _).trans (congr_arg _ $ is_group_hom.map_pow f _ _)] end is_group_hom namespace is_add_group_hom variables {Ξ² : Type v} [add_group Ξ±] [add_group Ξ²] (f : Ξ± β†’ Ξ²) [is_add_group_hom f] theorem map_smul (a : Ξ±) (n : β„•) : f (n β€’ a) = n β€’ f a := is_add_monoid_hom.map_smul f a n theorem map_gsmul (a : Ξ±) (n : β„€) : f (gsmul n a) = gsmul n (f a) := @is_group_hom.map_gpow (multiplicative Ξ±) (multiplicative Ξ²) _ _ f _ a n end is_add_group_hom local infix ` β€’β„€ `:70 := gsmul section comm_monoid variables [comm_group Ξ±] {Ξ² : Type*} [add_comm_group Ξ²] theorem mul_gpow (a b : Ξ±) : βˆ€ n:β„€, (a * b)^n = a^n * b^n | (n : β„•) := mul_pow a b n | -[1+ n] := show _⁻¹=_⁻¹*_⁻¹, by rw [mul_pow, mul_inv_rev, mul_comm] theorem gsmul_add : βˆ€ (a b : Ξ²) (n : β„€), n β€’β„€ (a + b) = n β€’β„€ a + n β€’β„€ b := @mul_gpow (multiplicative Ξ²) _ theorem gsmul_sub : βˆ€ (a b : Ξ²) (n : β„€), gsmul n (a - b) = gsmul n a - gsmul n b := by simp [gsmul_add, gsmul_neg] instance gpow.is_group_hom (n : β„€) : is_group_hom ((^ n) : Ξ± β†’ Ξ±) := { map_mul := Ξ» _ _, mul_gpow _ _ n } instance gsmul.is_add_group_hom (n : β„€) : is_add_group_hom (gsmul n : Ξ² β†’ Ξ²) := { map_add := Ξ» _ _, gsmul_add _ _ n } end comm_monoid section group @[instance] theorem is_add_group_hom.gsmul {Ξ± Ξ²} [add_group Ξ±] [add_comm_group Ξ²] (f : Ξ± β†’ Ξ²) [is_add_group_hom f] (z : β„€) : is_add_group_hom (Ξ»a, gsmul z (f a)) := { map_add := assume a b, by rw [is_add_hom.map_add f, gsmul_add] } end group @[simp] lemma with_bot.coe_smul [add_monoid Ξ±] (a : Ξ±) (n : β„•) : ((add_monoid.smul n a : Ξ±) : with_bot Ξ±) = add_monoid.smul n a := by induction n; simp [*, succ_smul]; refl theorem add_monoid.smul_eq_mul' [semiring Ξ±] (a : Ξ±) (n : β„•) : n β€’ a = a * n := by induction n with n ih; [rw [add_monoid.zero_smul, nat.cast_zero, mul_zero], rw [succ_smul', ih, nat.cast_succ, mul_add, mul_one]] theorem add_monoid.smul_eq_mul [semiring Ξ±] (n : β„•) (a : Ξ±) : n β€’ a = n * a := by rw [add_monoid.smul_eq_mul', nat.mul_cast_comm] theorem add_monoid.mul_smul_left [semiring Ξ±] (a b : Ξ±) (n : β„•) : n β€’ (a * b) = a * (n β€’ b) := by rw [add_monoid.smul_eq_mul', add_monoid.smul_eq_mul', mul_assoc] theorem add_monoid.mul_smul_assoc [semiring Ξ±] (a b : Ξ±) (n : β„•) : n β€’ (a * b) = n β€’ a * b := by rw [add_monoid.smul_eq_mul, add_monoid.smul_eq_mul, mul_assoc] lemma zero_pow [semiring Ξ±] : βˆ€ {n : β„•}, 0 < n β†’ (0 : Ξ±) ^ n = 0 | (n+1) _ := zero_mul _ @[simp, move_cast] theorem nat.cast_pow [semiring Ξ±] (n m : β„•) : (↑(n ^ m) : Ξ±) = ↑n ^ m := by induction m with m ih; [exact nat.cast_one, rw [nat.pow_succ, pow_succ', nat.cast_mul, ih]] @[simp, move_cast] theorem int.coe_nat_pow (n m : β„•) : ((n ^ m : β„•) : β„€) = n ^ m := by induction m with m ih; [exact int.coe_nat_one, rw [nat.pow_succ, pow_succ', int.coe_nat_mul, ih]] theorem int.nat_abs_pow (n : β„€) (k : β„•) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k := by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, nat.pow_succ, ih]] theorem is_semiring_hom.map_pow {Ξ²} [semiring Ξ±] [semiring Ξ²] (f : Ξ± β†’ Ξ²) [is_semiring_hom f] (x : Ξ±) (n : β„•) : f (x ^ n) = f x ^ n := by induction n with n ih; [exact is_semiring_hom.map_one f, rw [pow_succ, pow_succ, is_semiring_hom.map_mul f, ih]] theorem neg_one_pow_eq_or {R} [ring R] : βˆ€ n : β„•, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl rfl | (n+1) := (neg_one_pow_eq_or n).swap.imp (Ξ» h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (Ξ» h, by rw [pow_succ, h, mul_one]) lemma pow_dvd_pow [comm_semiring Ξ±] (a : Ξ±) {m n : β„•} (h : m ≀ n) : a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_sub_cancel' h]⟩ theorem gsmul_eq_mul [ring Ξ±] (a : Ξ±) : βˆ€ n, n β€’β„€ a = n * a | (n : β„•) := add_monoid.smul_eq_mul _ _ | -[1+ n] := show -(_β€’_)=-_*_, by rw [neg_mul_eq_neg_mul_symm, add_monoid.smul_eq_mul, nat.cast_succ] theorem gsmul_eq_mul' [ring Ξ±] (a : Ξ±) (n : β„€) : n β€’β„€ a = a * n := by rw [gsmul_eq_mul, int.mul_cast_comm] theorem mul_gsmul_left [ring Ξ±] (a b : Ξ±) (n : β„€) : n β€’β„€ (a * b) = a * (n β€’β„€ b) := by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc] theorem mul_gsmul_assoc [ring Ξ±] (a b : Ξ±) (n : β„€) : n β€’β„€ (a * b) = n β€’β„€ a * b := by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc] @[simp, move_cast] theorem int.cast_pow [ring Ξ±] (n : β„€) (m : β„•) : (↑(n ^ m) : Ξ±) = ↑n ^ m := by induction m with m ih; [exact int.cast_one, rw [pow_succ, pow_succ, int.cast_mul, ih]] lemma neg_one_pow_eq_pow_mod_two [ring Ξ±] {n : β„•} : (-1 : Ξ±) ^ n = -1 ^ (n % 2) := by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two] theorem sq_sub_sq [comm_ring Ξ±] (a b : Ξ±) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by rw [pow_two, pow_two, mul_self_sub_mul_self] theorem pow_eq_zero [domain Ξ±] {x : Ξ±} {n : β„•} (H : x^n = 0) : x = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one x, H, mul_zero] }, exact or.cases_on (mul_eq_zero.1 H) id ih end theorem pow_ne_zero [domain Ξ±] {a : Ξ±} (n : β„•) (h : a β‰  0) : a ^ n β‰  0 := mt pow_eq_zero h @[simp] theorem one_div_pow [division_ring Ξ±] {a : Ξ±} (ha : a β‰  0) (n : β„•) : (1 / a) ^ n = 1 / a ^ n := by induction n with n ih; [exact (div_one _).symm, rw [pow_succ', ih, division_ring.one_div_mul_one_div (pow_ne_zero _ ha) ha]]; refl @[simp] theorem division_ring.inv_pow [division_ring Ξ±] {a : Ξ±} (ha : a β‰  0) (n : β„•) : a⁻¹ ^ n = (a ^ n)⁻¹ := by simpa only [inv_eq_one_div] using one_div_pow ha n @[simp] theorem div_pow [field Ξ±] (a : Ξ±) {b : Ξ±} (hb : b β‰  0) (n : β„•) : (a / b) ^ n = a ^ n / b ^ n := by rw [div_eq_mul_one_div, mul_pow, one_div_pow hb, ← div_eq_mul_one_div] theorem add_monoid.smul_nonneg [ordered_comm_monoid Ξ±] {a : Ξ±} (H : 0 ≀ a) : βˆ€ n : β„•, 0 ≀ n β€’ a | 0 := le_refl _ | (n+1) := add_nonneg' H (add_monoid.smul_nonneg n) lemma pow_abs [decidable_linear_ordered_comm_ring Ξ±] (a : Ξ±) (n : β„•) : (abs a)^n = abs (a^n) := by induction n with n ih; [exact (abs_one).symm, rw [pow_succ, pow_succ, ih, abs_mul]] lemma abs_neg_one_pow [decidable_linear_ordered_comm_ring Ξ±] (n : β„•) : abs ((-1 : Ξ±)^n) = 1 := by rw [←pow_abs, abs_neg, abs_one, one_pow] lemma inv_pow' [discrete_field Ξ±] (a : Ξ±) (n : β„•) : (a ^ n)⁻¹ = a⁻¹ ^ n := by induction n; simp [*, pow_succ, mul_inv', mul_comm] lemma pow_inv [division_ring Ξ±] (a : Ξ±) : βˆ€ n : β„•, a β‰  0 β†’ (a^n)⁻¹ = (a⁻¹)^n | 0 ha := inv_one | (n+1) ha := by rw [pow_succ, pow_succ', mul_inv_eq (pow_ne_zero _ ha) ha, pow_inv _ ha] namespace add_monoid variable [ordered_comm_monoid Ξ±] theorem smul_le_smul {a : Ξ±} {n m : β„•} (ha : 0 ≀ a) (h : n ≀ m) : n β€’ a ≀ m β€’ a := let ⟨k, hk⟩ := nat.le.dest h in calc n β€’ a = n β€’ a + 0 : (add_zero _).symm ... ≀ n β€’ a + k β€’ a : add_le_add_left' (smul_nonneg ha _) ... = m β€’ a : by rw [← hk, add_smul] lemma smul_le_smul_of_le_right {a b : Ξ±} (hab : a ≀ b) : βˆ€ i : β„•, i β€’ a ≀ i β€’ b | 0 := by simp | (k+1) := add_le_add' hab (smul_le_smul_of_le_right _) end add_monoid section linear_ordered_semiring variable [linear_ordered_semiring Ξ±] theorem pow_pos {a : Ξ±} (H : 0 < a) : βˆ€ (n : β„•), 0 < a ^ n | 0 := zero_lt_one | (n+1) := mul_pos H (pow_pos _) theorem pow_nonneg {a : Ξ±} (H : 0 ≀ a) : βˆ€ (n : β„•), 0 ≀ a ^ n | 0 := zero_le_one | (n+1) := mul_nonneg H (pow_nonneg _) theorem pow_lt_pow_of_lt_left {x y : Ξ±} {n : β„•} (Hxy : x < y) (Hxpos : 0 ≀ x) (Hnpos : 0 < n) : x ^ n < y ^ n := begin cases lt_or_eq_of_le Hxpos, { rw ←nat.sub_add_cancel Hnpos, induction (n - 1), { simpa only [pow_one] }, rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one], apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) }, { rw [←h, zero_pow Hnpos], apply pow_pos (by rwa ←h at Hxy : 0 < y),} end theorem pow_right_inj {x y : Ξ±} {n : β„•} (Hxpos : 0 ≀ x) (Hypos : 0 ≀ y) (Hnpos : 0 < n) (Hxyn : x ^ n = y ^ n) : x = y := begin rcases lt_trichotomy x y with hxy | rfl | hyx, { exact absurd Hxyn (ne_of_lt (pow_lt_pow_of_lt_left hxy Hxpos Hnpos)) }, { refl }, { exact absurd Hxyn (ne_of_gt (pow_lt_pow_of_lt_left hyx Hypos Hnpos)) }, end theorem one_le_pow_of_one_le {a : Ξ±} (H : 1 ≀ a) : βˆ€ (n : β„•), 1 ≀ a ^ n | 0 := le_refl _ | (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n) zero_le_one (le_trans zero_le_one H) theorem pow_ge_one_add_mul {a : Ξ±} (H : a β‰₯ 0) : βˆ€ (n : β„•), 1 + n β€’ a ≀ (1 + a) ^ n | 0 := le_of_eq $ add_zero _ | (n+1) := begin rw [pow_succ', succ_smul'], refine le_trans _ (mul_le_mul_of_nonneg_right (pow_ge_one_add_mul n) (add_nonneg zero_le_one H)), rw [mul_add, mul_one, ← add_assoc, add_le_add_iff_left], simpa only [one_mul] using mul_le_mul_of_nonneg_right ((le_add_iff_nonneg_right 1).2 (add_monoid.smul_nonneg H n)) H end theorem pow_le_pow {a : Ξ±} {n m : β„•} (ha : 1 ≀ a) (h : n ≀ m) : a ^ n ≀ a ^ m := let ⟨k, hk⟩ := nat.le.dest h in calc a ^ n = a ^ n * 1 : (mul_one _).symm ... ≀ a ^ n * a ^ k : mul_le_mul_of_nonneg_left (one_le_pow_of_one_le ha _) (pow_nonneg (le_trans zero_le_one ha) _) ... = a ^ m : by rw [←hk, pow_add] lemma pow_lt_pow {a : Ξ±} {n m : β„•} (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m := begin have h' : 1 ≀ a := le_of_lt h, have h'' : 0 < a := lt_trans zero_lt_one h, cases m, cases h2, rw [pow_succ, ←one_mul (a ^ n)], exact mul_lt_mul h (pow_le_pow h' (nat.le_of_lt_succ h2)) (pow_pos h'' _) (le_of_lt h'') end lemma pow_le_pow_of_le_left {a b : Ξ±} (ha : 0 ≀ a) (hab : a ≀ b) : βˆ€ i : β„•, a^i ≀ b^i | 0 := by simp | (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab) lemma lt_of_pow_lt_pow {a b : Ξ±} (n : β„•) (hb : 0 ≀ b) (h : a ^ n < b ^ n) : a < b := lt_of_not_ge $ Ξ» hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h private lemma pow_lt_pow_of_lt_one_aux {a : Ξ±} (h : 0 < a) (ha : a < 1) (i : β„•) : βˆ€ k : β„•, a ^ (i + k + 1) < a ^ i | 0 := begin simp only [add_zero], rw ←one_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one end | (k+1) := begin rw ←one_mul (a^i), apply mul_lt_mul ha _ _ zero_le_one, { apply le_of_lt, apply pow_lt_pow_of_lt_one_aux }, { show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h } end private lemma pow_le_pow_of_le_one_aux {a : Ξ±} (h : 0 ≀ a) (ha : a ≀ 1) (i : β„•) : βˆ€ k : β„•, a ^ (i + k) ≀ a ^ i | 0 := by simp | (k+1) := by rw [←add_assoc, ←one_mul (a^i)]; exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one lemma pow_lt_pow_of_lt_one {a : Ξ±} (h : 0 < a) (ha : a < 1) {i j : β„•} (hij : i < j) : a ^ j < a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _ lemma pow_le_pow_of_le_one {a : Ξ±} (h : 0 ≀ a) (ha : a ≀ 1) {i j : β„•} (hij : i ≀ j) : a ^ j ≀ a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _ lemma pow_le_one {x : Ξ±} : βˆ€ (n : β„•) (h0 : 0 ≀ x) (h1 : x ≀ 1), x ^ n ≀ 1 | 0 h0 h1 := le_refl (1 : Ξ±) | (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1) end linear_ordered_semiring theorem pow_two_nonneg [linear_ordered_ring Ξ±] (a : Ξ±) : 0 ≀ a ^ 2 := by rw pow_two; exact mul_self_nonneg _ theorem pow_ge_one_add_sub_mul [linear_ordered_ring Ξ±] {a : Ξ±} (H : a β‰₯ 1) (n : β„•) : 1 + n β€’ (a - 1) ≀ a ^ n := by simpa only [add_sub_cancel'_right] using pow_ge_one_add_mul (sub_nonneg.2 H) n namespace int lemma units_pow_two (u : units β„€) : u ^ 2 = 1 := (units_eq_one_or u).elim (Ξ» h, h.symm β–Έ rfl) (Ξ» h, h.symm β–Έ rfl) lemma units_pow_eq_pow_mod_two (u : units β„€) (n : β„•) : u ^ n = u ^ (n % 2) := by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one] end int @[simp] lemma neg_square {Ξ±} [ring Ξ±] (z : Ξ±) : (-z)^2 = z^2 := by simp [pow, monoid.pow] lemma div_sq_cancel {Ξ±} [field Ξ±] {a : Ξ±} (ha : a β‰  0) (b : Ξ±) : a^2 * b / a = a * b := by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
cf81fe3583474932e90f399f9320b675af0db765
df561f413cfe0a88b1056655515399c546ff32a5
/8-inequality-world/l8.lean
188749dd92e2ba11e32922ac35919a5c1e20a0bb
[]
no_license
nicholaspun/natural-number-game-solutions
31d5158415c6f582694680044c5c6469032c2a06
1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0
refs/heads/main
1,675,123,625,012
1,607,633,548,000
1,607,633,548,000
318,933,860
3
1
null
null
null
null
UTF-8
Lean
false
false
131
lean
lemma succ_le_succ (a b : mynat) (h : a ≀ b) : succ a ≀ succ b := begin cases h with c hc, rw hc, use c, rw succ_add, refl, end
3f3e3bf9e97627756c284d3fbb85f8228ed349dd
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/set/basic.lean
f1d50731cf156db2d30f8e98ef502da523dfed8f
[ "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
108,081
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 -/ import logic.unique import order.boolean_algebra /-! # Basic properties of sets Sets in Lean are homogeneous; all their elements have the same type. Sets whose elements have type `X` are thus defined as `set X := X β†’ Prop`. Note that this function need not be decidable. The definition is in the core library. This file provides some basic definitions related to sets and functions not present in the core library, as well as extra lemmas for functions in the core library (empty set, univ, union, intersection, insert, singleton, set-theoretic difference, complement, and powerset). Note that a set is a term, not a type. There is a coercion from `set Ξ±` to `Type*` sending `s` to the corresponding subtype `β†₯s`. See also the file `set_theory/zfc.lean`, which contains an encoding of ZFC set theory in Lean. ## Main definitions Notation used here: - `f : Ξ± β†’ Ξ²` is a function, - `s : set Ξ±` and `s₁ sβ‚‚ : set Ξ±` are subsets of `Ξ±` - `t : set Ξ²` is a subset of `Ξ²`. Definitions in the file: * `strict_subset s₁ sβ‚‚ : Prop` : the predicate `s₁ βŠ† sβ‚‚` but `s₁ β‰  sβ‚‚`. * `nonempty s : Prop` : the predicate `s β‰  βˆ…`. Note that this is the preferred way to express the fact that `s` has an element (see the Implementation Notes). * `preimage f t : set Ξ±` : the preimage f⁻¹(t) (written `f ⁻¹' t` in Lean) of a subset of Ξ². * `subsingleton s : Prop` : the predicate saying that `s` has at most one element. * `range f : set Ξ²` : the image of `univ` under `f`. Also works for `{p : Prop} (f : p β†’ Ξ±)` (unlike `image`) * `s.prod t : set (Ξ± Γ— Ξ²)` : the subset `s Γ— t`. * `inclusion s₁ sβ‚‚ : β†₯s₁ β†’ β†₯sβ‚‚` : the map `β†₯s₁ β†’ β†₯sβ‚‚` induced by an inclusion `s₁ βŠ† sβ‚‚`. ## Notation * `f ⁻¹' t` for `preimage f t` * `f '' s` for `image f s` * `sᢜ` for the complement of `s` ## Implementation notes * `s.nonempty` is to be preferred to `s β‰  βˆ…` or `βˆƒ x, x ∈ s`. It has the advantage that the `s.nonempty` dot notation can be used. * For `s : set Ξ±`, do not use `subtype s`. Instead use `β†₯s` or `(s : Type*)` or `s`. ## Tags set, sets, subset, subsets, image, preimage, pre-image, range, union, intersection, insert, singleton, complement, powerset -/ /-! ### Set coercion to a type -/ open function universe variables u v w x run_cmd do e ← tactic.get_env, tactic.set_env $ e.mk_protected `set.compl lemma has_subset.subset.trans {Ξ± : Type*} [has_subset Ξ±] [is_trans Ξ± (βŠ†)] {a b c : Ξ±} (h : a βŠ† b) (h' : b βŠ† c) : a βŠ† c := trans h h' lemma has_subset.subset.antisymm {Ξ± : Type*} [has_subset Ξ±] [is_antisymm Ξ± (βŠ†)] {a b : Ξ±} (h : a βŠ† b) (h' : b βŠ† a) : a = b := antisymm h h' lemma has_ssubset.ssubset.trans {Ξ± : Type*} [has_ssubset Ξ±] [is_trans Ξ± (βŠ‚)] {a b c : Ξ±} (h : a βŠ‚ b) (h' : b βŠ‚ c) : a βŠ‚ c := trans h h' lemma has_ssubset.ssubset.asymm {Ξ± : Type*} [has_ssubset Ξ±] [is_asymm Ξ± (βŠ‚)] {a b : Ξ±} (h : a βŠ‚ b) : Β¬(b βŠ‚ a) := asymm h namespace set variable {Ξ± : Type*} instance : has_le (set Ξ±) := ⟨(βŠ†)⟩ instance : has_lt (set Ξ±) := ⟨λ s t, s ≀ t ∧ Β¬t ≀ s⟩ -- `βŠ‚` is not defined until further down instance {Ξ± : Type*} : boolean_algebra (set Ξ±) := { sup := (βˆͺ), le := (≀), lt := (<), inf := (∩), bot := βˆ…, compl := set.compl, top := univ, sdiff := (\), .. (infer_instance : boolean_algebra (Ξ± β†’ Prop)) } @[simp] lemma top_eq_univ : (⊀ : set Ξ±) = univ := rfl @[simp] lemma bot_eq_empty : (βŠ₯ : set Ξ±) = βˆ… := rfl @[simp] lemma sup_eq_union : ((βŠ”) : set Ξ± β†’ set Ξ± β†’ set Ξ±) = (βˆͺ) := rfl @[simp] lemma inf_eq_inter : ((βŠ“) : set Ξ± β†’ set Ξ± β†’ set Ξ±) = (∩) := rfl @[simp] lemma le_eq_subset : ((≀) : set Ξ± β†’ set Ξ± β†’ Prop) = (βŠ†) := rfl /-! `set.lt_eq_ssubset` is defined further down -/ @[simp] lemma compl_eq_compl : set.compl = (has_compl.compl : set Ξ± β†’ set Ξ±) := rfl /-- Coercion from a set to the corresponding subtype. -/ instance {Ξ± : Type*} : has_coe_to_sort (set Ξ±) := ⟨_, Ξ» s, {x // x ∈ s}⟩ instance pi_set_coe.can_lift (ΞΉ : Type u) (Ξ± : Ξ  i : ΞΉ, Type v) [ne : Ξ  i, nonempty (Ξ± i)] (s : set ΞΉ) : can_lift (Ξ  i : s, Ξ± i) (Ξ  i, Ξ± i) := { coe := Ξ» f i, f i, .. pi_subtype.can_lift ΞΉ Ξ± s } instance pi_set_coe.can_lift' (ΞΉ : Type u) (Ξ± : Type v) [ne : nonempty Ξ±] (s : set ΞΉ) : can_lift (s β†’ Ξ±) (ΞΉ β†’ Ξ±) := pi_set_coe.can_lift ΞΉ (Ξ» _, Ξ±) s instance set_coe.can_lift (s : set Ξ±) : can_lift Ξ± s := { coe := coe, cond := Ξ» a, a ∈ s, prf := Ξ» a ha, ⟨⟨a, ha⟩, rfl⟩ } end set section set_coe variables {Ξ± : Type u} theorem set.set_coe_eq_subtype (s : set Ξ±) : coe_sort.{(u+1) (u+2)} s = {x // x ∈ s} := rfl @[simp] theorem set_coe.forall {s : set Ξ±} {p : s β†’ Prop} : (βˆ€ x : s, p x) ↔ (βˆ€ x (h : x ∈ s), p ⟨x, h⟩) := subtype.forall @[simp] theorem set_coe.exists {s : set Ξ±} {p : s β†’ Prop} : (βˆƒ x : s, p x) ↔ (βˆƒ x (h : x ∈ s), p ⟨x, h⟩) := subtype.exists theorem set_coe.exists' {s : set Ξ±} {p : Ξ  x, x ∈ s β†’ Prop} : (βˆƒ x (h : x ∈ s), p x h) ↔ (βˆƒ x : s, p x x.2) := (@set_coe.exists _ _ $ Ξ» x, p x.1 x.2).symm theorem set_coe.forall' {s : set Ξ±} {p : Ξ  x, x ∈ s β†’ Prop} : (βˆ€ x (h : x ∈ s), p x h) ↔ (βˆ€ x : s, p x x.2) := (@set_coe.forall _ _ $ Ξ» x, p x.1 x.2).symm @[simp] theorem set_coe_cast : βˆ€ {s t : set Ξ±} (H' : s = t) (H : @eq (Type u) s t) (x : s), cast H x = ⟨x.1, H' β–Έ x.2⟩ | s _ rfl _ ⟨x, h⟩ := rfl theorem set_coe.ext {s : set Ξ±} {a b : s} : (↑a : Ξ±) = ↑b β†’ a = b := subtype.eq theorem set_coe.ext_iff {s : set Ξ±} {a b : s} : (↑a : Ξ±) = ↑b ↔ a = b := iff.intro set_coe.ext (assume h, h β–Έ rfl) end set_coe /-- See also `subtype.prop` -/ lemma subtype.mem {Ξ± : Type*} {s : set Ξ±} (p : s) : (p : Ξ±) ∈ s := p.prop lemma eq.subset {Ξ±} {s t : set Ξ±} : s = t β†’ s βŠ† t := by { rintro rfl x hx, exact hx } namespace set variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {ΞΉ : Sort x} {a : Ξ±} {s t : set Ξ±} instance : inhabited (set Ξ±) := βŸ¨βˆ…βŸ© @[ext] theorem ext {a b : set Ξ±} (h : βˆ€ x, x ∈ a ↔ x ∈ b) : a = b := funext (assume x, propext (h x)) theorem ext_iff {s t : set Ξ±} : s = t ↔ βˆ€ x, x ∈ s ↔ x ∈ t := ⟨λ h x, by rw h, ext⟩ @[trans] theorem mem_of_mem_of_subset {x : Ξ±} {s t : set Ξ±} (hx : x ∈ s) (h : s βŠ† t) : x ∈ t := h hx /-! ### Lemmas about `mem` and `set_of` -/ @[simp] theorem mem_set_of_eq {a : Ξ±} {p : Ξ± β†’ Prop} : a ∈ {a | p a} = p a := rfl theorem nmem_set_of_eq {a : Ξ±} {P : Ξ± β†’ Prop} : a βˆ‰ {a : Ξ± | P a} = Β¬ P a := rfl @[simp] theorem set_of_mem_eq {s : set Ξ±} : {x | x ∈ s} = s := rfl theorem set_of_set {s : set Ξ±} : set_of s = s := rfl lemma set_of_app_iff {p : Ξ± β†’ Prop} {x : Ξ±} : { x | p x } x ↔ p x := iff.rfl theorem mem_def {a : Ξ±} {s : set Ξ±} : a ∈ s ↔ s a := iff.rfl instance decidable_mem (s : set Ξ±) [H : decidable_pred s] : βˆ€ a, decidable (a ∈ s) := H instance decidable_set_of (p : Ξ± β†’ Prop) [H : decidable_pred p] : decidable_pred {a | p a} := H @[simp] theorem set_of_subset_set_of {p q : Ξ± β†’ Prop} : {a | p a} βŠ† {a | q a} ↔ (βˆ€a, p a β†’ q a) := iff.rfl @[simp] lemma sep_set_of {p q : Ξ± β†’ Prop} : {a ∈ {a | p a } | q a} = {a | p a ∧ q a} := rfl lemma set_of_and {p q : Ξ± β†’ Prop} : {a | p a ∧ q a} = {a | p a} ∩ {a | q a} := rfl lemma set_of_or {p q : Ξ± β†’ Prop} : {a | p a ∨ q a} = {a | p a} βˆͺ {a | q a} := rfl /-! ### Lemmas about subsets -/ -- TODO(Jeremy): write a tactic to unfold specific instances of generic notation? theorem subset_def {s t : set Ξ±} : (s βŠ† t) = βˆ€ x, x ∈ s β†’ x ∈ t := rfl @[refl] theorem subset.refl (a : set Ξ±) : a βŠ† a := assume x, id theorem subset.rfl {s : set Ξ±} : s βŠ† s := subset.refl s @[trans] theorem subset.trans {a b c : set Ξ±} (ab : a βŠ† b) (bc : b βŠ† c) : a βŠ† c := assume x h, bc (ab h) @[trans] theorem mem_of_eq_of_mem {x y : Ξ±} {s : set Ξ±} (hx : x = y) (h : y ∈ s) : x ∈ s := hx.symm β–Έ h theorem subset.antisymm {a b : set Ξ±} (h₁ : a βŠ† b) (hβ‚‚ : b βŠ† a) : a = b := set.ext $ Ξ» x, ⟨@h₁ _, @hβ‚‚ _⟩ theorem subset.antisymm_iff {a b : set Ξ±} : a = b ↔ a βŠ† b ∧ b βŠ† a := ⟨λ e, ⟨e.subset, e.symm.subset⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, subset.antisymm h₁ hβ‚‚βŸ© -- an alternative name theorem eq_of_subset_of_subset {a b : set Ξ±} : a βŠ† b β†’ b βŠ† a β†’ a = b := subset.antisymm theorem mem_of_subset_of_mem {s₁ sβ‚‚ : set Ξ±} {a : Ξ±} (h : s₁ βŠ† sβ‚‚) : a ∈ s₁ β†’ a ∈ sβ‚‚ := @h _ theorem not_mem_subset (h : s βŠ† t) : a βˆ‰ t β†’ a βˆ‰ s := mt $ mem_of_subset_of_mem h theorem not_subset : (Β¬ s βŠ† t) ↔ βˆƒa ∈ s, a βˆ‰ t := by simp only [subset_def, not_forall] /-! ### Definition of strict subsets `s βŠ‚ t` and basic properties. -/ instance : has_ssubset (set Ξ±) := ⟨(<)⟩ @[simp] lemma lt_eq_ssubset : ((<) : set Ξ± β†’ set Ξ± β†’ Prop) = (βŠ‚) := rfl theorem ssubset_def : (s βŠ‚ t) = (s βŠ† t ∧ Β¬ (t βŠ† s)) := rfl theorem eq_or_ssubset_of_subset (h : s βŠ† t) : s = t ∨ s βŠ‚ t := eq_or_lt_of_le h lemma exists_of_ssubset {s t : set Ξ±} (h : s βŠ‚ t) : (βˆƒx∈t, x βˆ‰ s) := not_subset.1 h.2 lemma ssubset_iff_subset_ne {s t : set Ξ±} : s βŠ‚ t ↔ s βŠ† t ∧ s β‰  t := @lt_iff_le_and_ne (set Ξ±) _ s t lemma ssubset_iff_of_subset {s t : set Ξ±} (h : s βŠ† t) : s βŠ‚ t ↔ βˆƒ x ∈ t, x βˆ‰ s := ⟨exists_of_ssubset, Ξ» ⟨x, hxt, hxs⟩, ⟨h, Ξ» h, hxs $ h hxt⟩⟩ lemma ssubset_of_ssubset_of_subset {s₁ sβ‚‚ s₃ : set Ξ±} (hs₁sβ‚‚ : s₁ βŠ‚ sβ‚‚) (hsβ‚‚s₃ : sβ‚‚ βŠ† s₃) : s₁ βŠ‚ s₃ := ⟨subset.trans hs₁sβ‚‚.1 hsβ‚‚s₃, Ξ» hs₃s₁, hs₁sβ‚‚.2 (subset.trans hsβ‚‚s₃ hs₃s₁)⟩ lemma ssubset_of_subset_of_ssubset {s₁ sβ‚‚ s₃ : set Ξ±} (hs₁sβ‚‚ : s₁ βŠ† sβ‚‚) (hsβ‚‚s₃ : sβ‚‚ βŠ‚ s₃) : s₁ βŠ‚ s₃ := ⟨subset.trans hs₁sβ‚‚ hsβ‚‚s₃.1, Ξ» hs₃s₁, hsβ‚‚s₃.2 (subset.trans hs₃s₁ hs₁sβ‚‚)⟩ theorem not_mem_empty (x : Ξ±) : Β¬ (x ∈ (βˆ… : set Ξ±)) := id @[simp] theorem not_not_mem : Β¬ (a βˆ‰ s) ↔ a ∈ s := not_not /-! ### Non-empty sets -/ /-- The property `s.nonempty` expresses the fact that the set `s` is not empty. It should be used in theorem assumptions instead of `βˆƒ x, x ∈ s` or `s β‰  βˆ…` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : set Ξ±) : Prop := βˆƒ x, x ∈ s lemma nonempty_def : s.nonempty ↔ βˆƒ x, x ∈ s := iff.rfl lemma nonempty_of_mem {x} (h : x ∈ s) : s.nonempty := ⟨x, h⟩ theorem nonempty.not_subset_empty : s.nonempty β†’ Β¬(s βŠ† βˆ…) | ⟨x, hx⟩ hs := hs hx theorem nonempty.ne_empty : βˆ€ {s : set Ξ±}, s.nonempty β†’ s β‰  βˆ… | _ ⟨x, hx⟩ rfl := hx @[simp] theorem not_nonempty_empty : Β¬(βˆ… : set Ξ±).nonempty := Ξ» h, h.ne_empty rfl /-- Extract a witness from `s.nonempty`. This function might be used instead of case analysis on the argument. Note that it makes a proof depend on the `classical.choice` axiom. -/ protected noncomputable def nonempty.some (h : s.nonempty) : Ξ± := classical.some h protected lemma nonempty.some_mem (h : s.nonempty) : h.some ∈ s := classical.some_spec h lemma nonempty.mono (ht : s βŠ† t) (hs : s.nonempty) : t.nonempty := hs.imp ht lemma nonempty_of_not_subset (h : Β¬s βŠ† t) : (s \ t).nonempty := let ⟨x, xs, xt⟩ := not_subset.1 h in ⟨x, xs, xt⟩ lemma nonempty_of_ssubset (ht : s βŠ‚ t) : (t \ s).nonempty := nonempty_of_not_subset ht.2 lemma nonempty.of_diff (h : (s \ t).nonempty) : s.nonempty := h.imp $ Ξ» _, and.left lemma nonempty_of_ssubset' (ht : s βŠ‚ t) : t.nonempty := (nonempty_of_ssubset ht).of_diff lemma nonempty.inl (hs : s.nonempty) : (s βˆͺ t).nonempty := hs.imp $ Ξ» _, or.inl lemma nonempty.inr (ht : t.nonempty) : (s βˆͺ t).nonempty := ht.imp $ Ξ» _, or.inr @[simp] lemma union_nonempty : (s βˆͺ t).nonempty ↔ s.nonempty ∨ t.nonempty := exists_or_distrib lemma nonempty.left (h : (s ∩ t).nonempty) : s.nonempty := h.imp $ Ξ» _, and.left lemma nonempty.right (h : (s ∩ t).nonempty) : t.nonempty := h.imp $ Ξ» _, and.right lemma nonempty_inter_iff_exists_right : (s ∩ t).nonempty ↔ βˆƒ x : t, ↑x ∈ s := ⟨λ ⟨x, xs, xt⟩, ⟨⟨x, xt⟩, xs⟩, Ξ» ⟨⟨x, xt⟩, xs⟩, ⟨x, xs, xt⟩⟩ lemma nonempty_inter_iff_exists_left : (s ∩ t).nonempty ↔ βˆƒ x : s, ↑x ∈ t := ⟨λ ⟨x, xs, xt⟩, ⟨⟨x, xs⟩, xt⟩, Ξ» ⟨⟨x, xt⟩, xs⟩, ⟨x, xt, xs⟩⟩ lemma nonempty_iff_univ_nonempty : nonempty Ξ± ↔ (univ : set Ξ±).nonempty := ⟨λ ⟨x⟩, ⟨x, trivial⟩, Ξ» ⟨x, _⟩, ⟨x⟩⟩ @[simp] lemma univ_nonempty : βˆ€ [h : nonempty Ξ±], (univ : set Ξ±).nonempty | ⟨x⟩ := ⟨x, trivial⟩ lemma nonempty.to_subtype (h : s.nonempty) : nonempty s := nonempty_subtype.2 h instance [nonempty Ξ±] : nonempty (set.univ : set Ξ±) := set.univ_nonempty.to_subtype @[simp] lemma nonempty_insert (a : Ξ±) (s : set Ξ±) : (insert a s).nonempty := ⟨a, or.inl rfl⟩ lemma nonempty_of_nonempty_subtype [nonempty s] : s.nonempty := nonempty_subtype.mp β€Ή_β€Ί /-! ### Lemmas about the empty set -/ theorem empty_def : (βˆ… : set Ξ±) = {x | false} := rfl @[simp] theorem mem_empty_eq (x : Ξ±) : x ∈ (βˆ… : set Ξ±) = false := rfl @[simp] theorem set_of_false : {a : Ξ± | false} = βˆ… := rfl @[simp] theorem empty_subset (s : set Ξ±) : βˆ… βŠ† s. theorem subset_empty_iff {s : set Ξ±} : s βŠ† βˆ… ↔ s = βˆ… := (subset.antisymm_iff.trans $ and_iff_left (empty_subset _)).symm theorem eq_empty_iff_forall_not_mem {s : set Ξ±} : s = βˆ… ↔ βˆ€ x, x βˆ‰ s := subset_empty_iff.symm theorem eq_empty_of_subset_empty {s : set Ξ±} : s βŠ† βˆ… β†’ s = βˆ… := subset_empty_iff.1 theorem eq_empty_of_not_nonempty (h : Β¬nonempty Ξ±) (s : set Ξ±) : s = βˆ… := eq_empty_of_subset_empty $ Ξ» x hx, h ⟨x⟩ lemma not_nonempty_iff_eq_empty {s : set Ξ±} : Β¬s.nonempty ↔ s = βˆ… := by simp only [set.nonempty, eq_empty_iff_forall_not_mem, not_exists] lemma empty_not_nonempty : Β¬(βˆ… : set Ξ±).nonempty := Ξ» h, h.ne_empty rfl theorem ne_empty_iff_nonempty : s β‰  βˆ… ↔ s.nonempty := not_iff_comm.1 not_nonempty_iff_eq_empty lemma eq_empty_or_nonempty (s : set Ξ±) : s = βˆ… ∨ s.nonempty := or_iff_not_imp_left.2 ne_empty_iff_nonempty.1 theorem subset_eq_empty {s t : set Ξ±} (h : t βŠ† s) (e : s = βˆ…) : t = βˆ… := subset_empty_iff.1 $ e β–Έ h theorem ball_empty_iff {p : Ξ± β†’ Prop} : (βˆ€ x ∈ (βˆ… : set Ξ±), p x) ↔ true := iff_true_intro $ Ξ» x, false.elim instance (Ξ± : Type u) : is_empty.{u+1} (βˆ… : set Ξ±) := ⟨λ x, x.2⟩ /-! ### Universal set. In Lean `@univ Ξ±` (or `univ : set Ξ±`) is the set that contains all elements of type `Ξ±`. Mathematically it is the same as `Ξ±` but it has a different type. -/ @[simp] theorem set_of_true : {x : Ξ± | true} = univ := rfl @[simp] theorem mem_univ (x : Ξ±) : x ∈ @univ Ξ± := trivial @[simp] lemma univ_eq_empty_iff : (univ : set Ξ±) = βˆ… ↔ Β¬ nonempty Ξ± := eq_empty_iff_forall_not_mem.trans ⟨λ H ⟨x⟩, H x trivial, Ξ» H x _, H ⟨x⟩⟩ theorem empty_ne_univ [h : nonempty Ξ±] : (βˆ… : set Ξ±) β‰  univ := Ξ» e, univ_eq_empty_iff.1 e.symm h @[simp] theorem subset_univ (s : set Ξ±) : s βŠ† univ := Ξ» x H, trivial theorem univ_subset_iff {s : set Ξ±} : univ βŠ† s ↔ s = univ := (subset.antisymm_iff.trans $ and_iff_right (subset_univ _)).symm theorem eq_univ_of_univ_subset {s : set Ξ±} : univ βŠ† s β†’ s = univ := univ_subset_iff.1 theorem eq_univ_iff_forall {s : set Ξ±} : s = univ ↔ βˆ€ x, x ∈ s := univ_subset_iff.symm.trans $ forall_congr $ Ξ» x, imp_iff_right ⟨⟩ theorem eq_univ_of_forall {s : set Ξ±} : (βˆ€ x, x ∈ s) β†’ s = univ := eq_univ_iff_forall.2 lemma eq_univ_of_subset {s t : set Ξ±} (h : s βŠ† t) (hs : s = univ) : t = univ := eq_univ_of_univ_subset $ hs β–Έ h lemma exists_mem_of_nonempty (Ξ±) : βˆ€ [nonempty Ξ±], βˆƒx:Ξ±, x ∈ (univ : set Ξ±) | ⟨x⟩ := ⟨x, trivial⟩ instance univ_decidable : decidable_pred (@set.univ Ξ±) := Ξ» x, is_true trivial /-- `diagonal Ξ±` is the subset of `Ξ± Γ— Ξ±` consisting of all pairs of the form `(a, a)`. -/ def diagonal (Ξ± : Type*) : set (Ξ± Γ— Ξ±) := {p | p.1 = p.2} @[simp] lemma mem_diagonal {Ξ± : Type*} (x : Ξ±) : (x, x) ∈ diagonal Ξ± := by simp [diagonal] /-! ### Lemmas about union -/ theorem union_def {s₁ sβ‚‚ : set Ξ±} : s₁ βˆͺ sβ‚‚ = {a | a ∈ s₁ ∨ a ∈ sβ‚‚} := rfl theorem mem_union_left {x : Ξ±} {a : set Ξ±} (b : set Ξ±) : x ∈ a β†’ x ∈ a βˆͺ b := or.inl theorem mem_union_right {x : Ξ±} {b : set Ξ±} (a : set Ξ±) : x ∈ b β†’ x ∈ a βˆͺ b := or.inr theorem mem_or_mem_of_mem_union {x : Ξ±} {a b : set Ξ±} (H : x ∈ a βˆͺ b) : x ∈ a ∨ x ∈ b := H theorem mem_union.elim {x : Ξ±} {a b : set Ξ±} {P : Prop} (H₁ : x ∈ a βˆͺ b) (Hβ‚‚ : x ∈ a β†’ P) (H₃ : x ∈ b β†’ P) : P := or.elim H₁ Hβ‚‚ H₃ theorem mem_union (x : Ξ±) (a b : set Ξ±) : x ∈ a βˆͺ b ↔ x ∈ a ∨ x ∈ b := iff.rfl @[simp] theorem mem_union_eq (x : Ξ±) (a b : set Ξ±) : x ∈ a βˆͺ b = (x ∈ a ∨ x ∈ b) := rfl @[simp] theorem union_self (a : set Ξ±) : a βˆͺ a = a := ext $ Ξ» x, or_self _ @[simp] theorem union_empty (a : set Ξ±) : a βˆͺ βˆ… = a := ext $ Ξ» x, or_false _ @[simp] theorem empty_union (a : set Ξ±) : βˆ… βˆͺ a = a := ext $ Ξ» x, false_or _ theorem union_comm (a b : set Ξ±) : a βˆͺ b = b βˆͺ a := ext $ Ξ» x, or.comm theorem union_assoc (a b c : set Ξ±) : (a βˆͺ b) βˆͺ c = a βˆͺ (b βˆͺ c) := ext $ Ξ» x, or.assoc instance union_is_assoc : is_associative (set Ξ±) (βˆͺ) := ⟨union_assoc⟩ instance union_is_comm : is_commutative (set Ξ±) (βˆͺ) := ⟨union_comm⟩ theorem union_left_comm (s₁ sβ‚‚ s₃ : set Ξ±) : s₁ βˆͺ (sβ‚‚ βˆͺ s₃) = sβ‚‚ βˆͺ (s₁ βˆͺ s₃) := ext $ Ξ» x, or.left_comm theorem union_right_comm (s₁ sβ‚‚ s₃ : set Ξ±) : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = (s₁ βˆͺ s₃) βˆͺ sβ‚‚ := ext $ Ξ» x, or.right_comm theorem union_eq_self_of_subset_left {s t : set Ξ±} (h : s βŠ† t) : s βˆͺ t = t := ext $ Ξ» x, or_iff_right_of_imp $ @h _ theorem union_eq_self_of_subset_right {s t : set Ξ±} (h : t βŠ† s) : s βˆͺ t = s := ext $ Ξ» x, or_iff_left_of_imp $ @h _ @[simp] theorem subset_union_left (s t : set Ξ±) : s βŠ† s βˆͺ t := Ξ» x, or.inl @[simp] theorem subset_union_right (s t : set Ξ±) : t βŠ† s βˆͺ t := Ξ» x, or.inr theorem union_subset {s t r : set Ξ±} (sr : s βŠ† r) (tr : t βŠ† r) : s βˆͺ t βŠ† r := Ξ» x, or.rec (@sr _) (@tr _) @[simp] theorem union_subset_iff {s t u : set Ξ±} : s βˆͺ t βŠ† u ↔ s βŠ† u ∧ t βŠ† u := (forall_congr (by exact Ξ» x, or_imp_distrib)).trans forall_and_distrib theorem union_subset_union {s₁ sβ‚‚ t₁ tβ‚‚ : set Ξ±} (h₁ : s₁ βŠ† sβ‚‚) (hβ‚‚ : t₁ βŠ† tβ‚‚) : s₁ βˆͺ t₁ βŠ† sβ‚‚ βˆͺ tβ‚‚ := Ξ» x, or.imp (@h₁ _) (@hβ‚‚ _) theorem union_subset_union_left {s₁ sβ‚‚ : set Ξ±} (t) (h : s₁ βŠ† sβ‚‚) : s₁ βˆͺ t βŠ† sβ‚‚ βˆͺ t := union_subset_union h subset.rfl theorem union_subset_union_right (s) {t₁ tβ‚‚ : set Ξ±} (h : t₁ βŠ† tβ‚‚) : s βˆͺ t₁ βŠ† s βˆͺ tβ‚‚ := union_subset_union subset.rfl h lemma subset_union_of_subset_left {s t : set Ξ±} (h : s βŠ† t) (u : set Ξ±) : s βŠ† t βˆͺ u := subset.trans h (subset_union_left t u) lemma subset_union_of_subset_right {s u : set Ξ±} (h : s βŠ† u) (t : set Ξ±) : s βŠ† t βˆͺ u := subset.trans h (subset_union_right t u) @[simp] theorem union_empty_iff {s t : set Ξ±} : s βˆͺ t = βˆ… ↔ s = βˆ… ∧ t = βˆ… := by simp only [← subset_empty_iff]; exact union_subset_iff @[simp] lemma union_univ {s : set Ξ±} : s βˆͺ univ = univ := sup_top_eq @[simp] lemma univ_union {s : set Ξ±} : univ βˆͺ s = univ := top_sup_eq /-! ### Lemmas about intersection -/ theorem inter_def {s₁ sβ‚‚ : set Ξ±} : s₁ ∩ sβ‚‚ = {a | a ∈ s₁ ∧ a ∈ sβ‚‚} := rfl theorem mem_inter_iff (x : Ξ±) (a b : set Ξ±) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl @[simp] theorem mem_inter_eq (x : Ξ±) (a b : set Ξ±) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl theorem mem_inter {x : Ξ±} {a b : set Ξ±} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b := ⟨ha, hb⟩ theorem mem_of_mem_inter_left {x : Ξ±} {a b : set Ξ±} (h : x ∈ a ∩ b) : x ∈ a := h.left theorem mem_of_mem_inter_right {x : Ξ±} {a b : set Ξ±} (h : x ∈ a ∩ b) : x ∈ b := h.right @[simp] theorem inter_self (a : set Ξ±) : a ∩ a = a := ext $ Ξ» x, and_self _ @[simp] theorem inter_empty (a : set Ξ±) : a ∩ βˆ… = βˆ… := ext $ Ξ» x, and_false _ @[simp] theorem empty_inter (a : set Ξ±) : βˆ… ∩ a = βˆ… := ext $ Ξ» x, false_and _ theorem inter_comm (a b : set Ξ±) : a ∩ b = b ∩ a := ext $ Ξ» x, and.comm theorem inter_assoc (a b c : set Ξ±) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext $ Ξ» x, and.assoc instance inter_is_assoc : is_associative (set Ξ±) (∩) := ⟨inter_assoc⟩ instance inter_is_comm : is_commutative (set Ξ±) (∩) := ⟨inter_comm⟩ theorem inter_left_comm (s₁ sβ‚‚ s₃ : set Ξ±) : s₁ ∩ (sβ‚‚ ∩ s₃) = sβ‚‚ ∩ (s₁ ∩ s₃) := ext $ Ξ» x, and.left_comm theorem inter_right_comm (s₁ sβ‚‚ s₃ : set Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = (s₁ ∩ s₃) ∩ sβ‚‚ := ext $ Ξ» x, and.right_comm @[simp] theorem inter_subset_left (s t : set Ξ±) : s ∩ t βŠ† s := Ξ» x, and.left @[simp] theorem inter_subset_right (s t : set Ξ±) : s ∩ t βŠ† t := Ξ» x, and.right theorem subset_inter {s t r : set Ξ±} (rs : r βŠ† s) (rt : r βŠ† t) : r βŠ† s ∩ t := Ξ» x h, ⟨rs h, rt h⟩ @[simp] theorem subset_inter_iff {s t r : set Ξ±} : r βŠ† s ∩ t ↔ r βŠ† s ∧ r βŠ† t := (forall_congr (by exact Ξ» x, imp_and_distrib)).trans forall_and_distrib theorem inter_eq_left_iff_subset {s t : set Ξ±} : s ∩ t = s ↔ s βŠ† t := ext_iff.trans $ forall_congr $ Ξ» x, and_iff_left_iff_imp theorem inter_eq_right_iff_subset {s t : set Ξ±} : s ∩ t = t ↔ t βŠ† s := ext_iff.trans $ forall_congr $ Ξ» x, and_iff_right_iff_imp theorem inter_eq_self_of_subset_left {s t : set Ξ±} : s βŠ† t β†’ s ∩ t = s := inter_eq_left_iff_subset.mpr theorem inter_eq_self_of_subset_right {s t : set Ξ±} : t βŠ† s β†’ s ∩ t = t := inter_eq_right_iff_subset.mpr @[simp] theorem inter_univ (a : set Ξ±) : a ∩ univ = a := inter_eq_self_of_subset_left $ subset_univ _ @[simp] theorem univ_inter (a : set Ξ±) : univ ∩ a = a := inter_eq_self_of_subset_right $ subset_univ _ theorem inter_subset_inter {s₁ sβ‚‚ t₁ tβ‚‚ : set Ξ±} (h₁ : s₁ βŠ† t₁) (hβ‚‚ : sβ‚‚ βŠ† tβ‚‚) : s₁ ∩ sβ‚‚ βŠ† t₁ ∩ tβ‚‚ := Ξ» x, and.imp (@h₁ _) (@hβ‚‚ _) theorem inter_subset_inter_left {s t : set Ξ±} (u : set Ξ±) (H : s βŠ† t) : s ∩ u βŠ† t ∩ u := inter_subset_inter H subset.rfl theorem inter_subset_inter_right {s t : set Ξ±} (u : set Ξ±) (H : s βŠ† t) : u ∩ s βŠ† u ∩ t := inter_subset_inter subset.rfl H theorem union_inter_cancel_left {s t : set Ξ±} : (s βˆͺ t) ∩ s = s := inter_eq_self_of_subset_right $ subset_union_left _ _ theorem union_inter_cancel_right {s t : set Ξ±} : (s βˆͺ t) ∩ t = t := inter_eq_self_of_subset_right $ subset_union_right _ _ /-! ### Distributivity laws -/ theorem inter_distrib_left (s t u : set Ξ±) : s ∩ (t βˆͺ u) = (s ∩ t) βˆͺ (s ∩ u) := inf_sup_left theorem inter_union_distrib_left {s t u : set Ξ±} : s ∩ (t βˆͺ u) = (s ∩ t) βˆͺ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : set Ξ±) : (s βˆͺ t) ∩ u = (s ∩ u) βˆͺ (t ∩ u) := inf_sup_right theorem union_inter_distrib_right {s t u : set Ξ±} : (s βˆͺ t) ∩ u = (s ∩ u) βˆͺ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : set Ξ±) : s βˆͺ (t ∩ u) = (s βˆͺ t) ∩ (s βˆͺ u) := sup_inf_left theorem union_inter_distrib_left {s t u : set Ξ±} : s βˆͺ (t ∩ u) = (s βˆͺ t) ∩ (s βˆͺ u) := sup_inf_left theorem union_distrib_right (s t u : set Ξ±) : (s ∩ t) βˆͺ u = (s βˆͺ u) ∩ (t βˆͺ u) := sup_inf_right theorem inter_union_distrib_right {s t u : set Ξ±} : (s ∩ t) βˆͺ u = (s βˆͺ u) ∩ (t βˆͺ u) := sup_inf_right /-! ### Lemmas about `insert` `insert Ξ± s` is the set `{Ξ±} βˆͺ s`. -/ theorem insert_def (x : Ξ±) (s : set Ξ±) : insert x s = { y | y = x ∨ y ∈ s } := rfl @[simp] theorem subset_insert (x : Ξ±) (s : set Ξ±) : s βŠ† insert x s := Ξ» y, or.inr theorem mem_insert (x : Ξ±) (s : set Ξ±) : x ∈ insert x s := or.inl rfl theorem mem_insert_of_mem {x : Ξ±} {s : set Ξ±} (y : Ξ±) : x ∈ s β†’ x ∈ insert y s := or.inr theorem eq_or_mem_of_mem_insert {x a : Ξ±} {s : set Ξ±} : x ∈ insert a s β†’ x = a ∨ x ∈ s := id theorem mem_of_mem_insert_of_ne {x a : Ξ±} {s : set Ξ±} : x ∈ insert a s β†’ x β‰  a β†’ x ∈ s := or.resolve_left @[simp] theorem mem_insert_iff {x a : Ξ±} {s : set Ξ±} : x ∈ insert a s ↔ x = a ∨ x ∈ s := iff.rfl @[simp] theorem insert_eq_of_mem {a : Ξ±} {s : set Ξ±} (h : a ∈ s) : insert a s = s := ext $ Ξ» x, or_iff_right_of_imp $ Ξ» e, e.symm β–Έ h lemma ne_insert_of_not_mem {s : set Ξ±} (t : set Ξ±) {a : Ξ±} : a βˆ‰ s β†’ s β‰  insert a t := mt $ Ξ» e, e.symm β–Έ mem_insert _ _ theorem insert_subset : insert a s βŠ† t ↔ (a ∈ t ∧ s βŠ† t) := by simp only [subset_def, or_imp_distrib, forall_and_distrib, forall_eq, mem_insert_iff] theorem insert_subset_insert (h : s βŠ† t) : insert a s βŠ† insert a t := Ξ» x, or.imp_right (@h _) theorem ssubset_iff_insert {s t : set Ξ±} : s βŠ‚ t ↔ βˆƒ a βˆ‰ s, insert a s βŠ† t := begin simp only [insert_subset, exists_and_distrib_right, ssubset_def, not_subset], simp only [exists_prop, and_comm] end theorem ssubset_insert {s : set Ξ±} {a : Ξ±} (h : a βˆ‰ s) : s βŠ‚ insert a s := ssubset_iff_insert.2 ⟨a, h, subset.refl _⟩ theorem insert_comm (a b : Ξ±) (s : set Ξ±) : insert a (insert b s) = insert b (insert a s) := ext $ Ξ» x, or.left_comm theorem insert_union : insert a s βˆͺ t = insert a (s βˆͺ t) := ext $ Ξ» x, or.assoc @[simp] theorem union_insert : s βˆͺ insert a t = insert a (s βˆͺ t) := ext $ Ξ» x, or.left_comm theorem insert_nonempty (a : Ξ±) (s : set Ξ±) : (insert a s).nonempty := ⟨a, mem_insert a s⟩ instance (a : Ξ±) (s : set Ξ±) : nonempty (insert a s : set Ξ±) := (insert_nonempty a s).to_subtype lemma insert_inter (x : Ξ±) (s t : set Ξ±) : insert x (s ∩ t) = insert x s ∩ insert x t := ext $ Ξ» y, or_and_distrib_left -- useful in proofs by induction theorem forall_of_forall_insert {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} (H : βˆ€ x, x ∈ insert a s β†’ P x) (x) (h : x ∈ s) : P x := H _ (or.inr h) theorem forall_insert_of_forall {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} (H : βˆ€ x, x ∈ s β†’ P x) (ha : P a) (x) (h : x ∈ insert a s) : P x := h.elim (Ξ» e, e.symm β–Έ ha) (H _) theorem bex_insert_iff {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} : (βˆƒ x ∈ insert a s, P x) ↔ P a ∨ (βˆƒ x ∈ s, P x) := bex_or_left_distrib.trans $ or_congr_left bex_eq_left theorem ball_insert_iff {P : Ξ± β†’ Prop} {a : Ξ±} {s : set Ξ±} : (βˆ€ x ∈ insert a s, P x) ↔ P a ∧ (βˆ€x ∈ s, P x) := ball_or_left_distrib.trans $ and_congr_left' forall_eq /-! ### Lemmas about singletons -/ theorem singleton_def (a : Ξ±) : ({a} : set Ξ±) = insert a βˆ… := (insert_emptyc_eq _).symm @[simp] theorem mem_singleton_iff {a b : Ξ±} : a ∈ ({b} : set Ξ±) ↔ a = b := iff.rfl @[simp] lemma set_of_eq_eq_singleton {a : Ξ±} : {n | n = a} = {a} := ext $ Ξ» n, (set.mem_singleton_iff).symm -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : Ξ±) : a ∈ ({a} : set Ξ±) := @rfl _ _ theorem eq_of_mem_singleton {x y : Ξ±} (h : x ∈ ({y} : set Ξ±)) : x = y := h @[simp] theorem singleton_eq_singleton_iff {x y : Ξ±} : {x} = ({y} : set Ξ±) ↔ x = y := ext_iff.trans eq_iff_eq_cancel_left theorem mem_singleton_of_eq {x y : Ξ±} (H : x = y) : x ∈ ({y} : set Ξ±) := H theorem insert_eq (x : Ξ±) (s : set Ξ±) : insert x s = ({x} : set Ξ±) βˆͺ s := rfl @[simp] theorem pair_eq_singleton (a : Ξ±) : ({a, a} : set Ξ±) = {a} := union_self _ theorem pair_comm (a b : Ξ±) : ({a, b} : set Ξ±) = {b, a} := union_comm _ _ @[simp] theorem singleton_nonempty (a : Ξ±) : ({a} : set Ξ±).nonempty := ⟨a, rfl⟩ @[simp] theorem singleton_subset_iff {a : Ξ±} {s : set Ξ±} : {a} βŠ† s ↔ a ∈ s := forall_eq theorem set_compr_eq_eq_singleton {a : Ξ±} : {b | b = a} = {a} := rfl @[simp] theorem singleton_union : {a} βˆͺ s = insert a s := rfl @[simp] theorem union_singleton : s βˆͺ {a} = insert a s := union_comm _ _ @[simp] theorem singleton_inter_nonempty : ({a} ∩ s).nonempty ↔ a ∈ s := by simp only [set.nonempty, mem_inter_eq, mem_singleton_iff, exists_eq_left] @[simp] theorem inter_singleton_nonempty : (s ∩ {a}).nonempty ↔ a ∈ s := by rw [inter_comm, singleton_inter_nonempty] @[simp] theorem singleton_inter_eq_empty : {a} ∩ s = βˆ… ↔ a βˆ‰ s := not_nonempty_iff_eq_empty.symm.trans $ not_congr singleton_inter_nonempty @[simp] theorem inter_singleton_eq_empty : s ∩ {a} = βˆ… ↔ a βˆ‰ s := by rw [inter_comm, singleton_inter_eq_empty] lemma nmem_singleton_empty {s : set Ξ±} : s βˆ‰ ({βˆ…} : set (set Ξ±)) ↔ s.nonempty := ne_empty_iff_nonempty instance unique_singleton (a : Ξ±) : unique β†₯({a} : set Ξ±) := ⟨⟨⟨a, mem_singleton a⟩⟩, Ξ» ⟨x, h⟩, subtype.eq h⟩ lemma eq_singleton_iff_unique_mem {s : set Ξ±} {a : Ξ±} : s = {a} ↔ a ∈ s ∧ βˆ€ x ∈ s, x = a := subset.antisymm_iff.trans $ and.comm.trans $ and_congr_left' singleton_subset_iff lemma eq_singleton_iff_nonempty_unique_mem {s : set Ξ±} {a : Ξ±} : s = {a} ↔ s.nonempty ∧ βˆ€ x ∈ s, x = a := eq_singleton_iff_unique_mem.trans $ and_congr_left $ Ξ» H, ⟨λ h', ⟨_, h'⟩, Ξ» ⟨x, h⟩, H x h β–Έ h⟩ -- while `simp` is capable of proving this, it is not capable of turning the LHS into the RHS. @[simp] lemma default_coe_singleton (x : Ξ±) : default ({x} : set Ξ±) = ⟨x, rfl⟩ := rfl /-! ### Lemmas about sets defined as `{x ∈ s | p x}`. -/ theorem mem_sep {s : set Ξ±} {p : Ξ± β†’ Prop} {x : Ξ±} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ @[simp] theorem sep_mem_eq {s t : set Ξ±} : {x ∈ s | x ∈ t} = s ∩ t := rfl @[simp] theorem mem_sep_eq {s : set Ξ±} {p : Ξ± β†’ Prop} {x : Ξ±} : x ∈ {x ∈ s | p x} = (x ∈ s ∧ p x) := rfl theorem mem_sep_iff {s : set Ξ±} {p : Ξ± β†’ Prop} {x : Ξ±} : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x := iff.rfl theorem eq_sep_of_subset {s t : set Ξ±} (h : s βŠ† t) : s = {x ∈ t | x ∈ s} := (inter_eq_self_of_subset_right h).symm theorem sep_subset (s : set Ξ±) (p : Ξ± β†’ Prop) : {x ∈ s | p x} βŠ† s := Ξ» x, and.left theorem forall_not_of_sep_empty {s : set Ξ±} {p : Ξ± β†’ Prop} (H : {x ∈ s | p x} = βˆ…) (x) : x ∈ s β†’ Β¬ p x := not_and.1 (eq_empty_iff_forall_not_mem.1 H x : _) @[simp] lemma sep_univ {Ξ±} {p : Ξ± β†’ Prop} : {a ∈ (univ : set Ξ±) | p a} = {a | p a} := univ_inter _ @[simp] lemma sep_true : {a ∈ s | true} = s := by { ext, simp } @[simp] lemma sep_false : {a ∈ s | false} = βˆ… := by { ext, simp } @[simp] lemma subset_singleton_iff {Ξ± : Type*} {s : set Ξ±} {x : Ξ±} : s βŠ† {x} ↔ βˆ€ y ∈ s, y = x := iff.rfl lemma subset_singleton_iff_eq {s : set Ξ±} {x : Ξ±} : s βŠ† {x} ↔ s = βˆ… ∨ s = {x} := begin obtain (rfl | hs) := s.eq_empty_or_nonempty, use ⟨λ _, or.inl rfl, Ξ» _, empty_subset _⟩, simp [eq_singleton_iff_nonempty_unique_mem, hs, ne_empty_iff_nonempty.2 hs], end lemma ssubset_singleton_iff {s : set Ξ±} {x : Ξ±} : s βŠ‚ {x} ↔ s = βˆ… := begin rw [ssubset_iff_subset_ne, subset_singleton_iff_eq, or_and_distrib_right, and_not_self, or_false, and_iff_left_iff_imp], rintro rfl, refine ne_comm.1 (ne_empty_iff_nonempty.2 (singleton_nonempty _)), end lemma eq_empty_of_ssubset_singleton {s : set Ξ±} {x : Ξ±} (hs : s βŠ‚ {x}) : s = βˆ… := ssubset_singleton_iff.1 hs /-! ### Lemmas about complement -/ theorem mem_compl {s : set Ξ±} {x : Ξ±} (h : x βˆ‰ s) : x ∈ sᢜ := h lemma compl_set_of {Ξ±} (p : Ξ± β†’ Prop) : {a | p a}ᢜ = { a | Β¬ p a } := rfl theorem not_mem_of_mem_compl {s : set Ξ±} {x : Ξ±} (h : x ∈ sᢜ) : x βˆ‰ s := h @[simp] theorem mem_compl_eq (s : set Ξ±) (x : Ξ±) : x ∈ sᢜ = (x βˆ‰ s) := rfl theorem mem_compl_iff (s : set Ξ±) (x : Ξ±) : x ∈ sᢜ ↔ x βˆ‰ s := iff.rfl @[simp] theorem inter_compl_self (s : set Ξ±) : s ∩ sᢜ = βˆ… := inf_compl_eq_bot @[simp] theorem compl_inter_self (s : set Ξ±) : sᢜ ∩ s = βˆ… := compl_inf_eq_bot @[simp] theorem compl_empty : (βˆ… : set Ξ±)ᢜ = univ := compl_bot @[simp] theorem compl_union (s t : set Ξ±) : (s βˆͺ t)ᢜ = sᢜ ∩ tᢜ := compl_sup theorem compl_inter (s t : set Ξ±) : (s ∩ t)ᢜ = sᢜ βˆͺ tᢜ := compl_inf @[simp] theorem compl_univ : (univ : set Ξ±)ᢜ = βˆ… := compl_top @[simp] lemma compl_empty_iff {s : set Ξ±} : sᢜ = βˆ… ↔ s = univ := compl_eq_bot @[simp] lemma compl_univ_iff {s : set Ξ±} : sᢜ = univ ↔ s = βˆ… := compl_eq_top lemma nonempty_compl {s : set Ξ±} : sᢜ.nonempty ↔ s β‰  univ := ne_empty_iff_nonempty.symm.trans $ not_congr $ compl_empty_iff lemma mem_compl_singleton_iff {a x : Ξ±} : x ∈ ({a} : set Ξ±)ᢜ ↔ x β‰  a := not_congr mem_singleton_iff lemma compl_singleton_eq (a : Ξ±) : ({a} : set Ξ±)ᢜ = {x | x β‰  a} := ext $ Ξ» x, mem_compl_singleton_iff @[simp] lemma compl_ne_eq_singleton (a : Ξ±) : ({x | x β‰  a} : set Ξ±)ᢜ = {a} := by { ext, simp, } theorem union_eq_compl_compl_inter_compl (s t : set Ξ±) : s βˆͺ t = (sᢜ ∩ tᢜ)ᢜ := ext $ Ξ» x, or_iff_not_and_not theorem inter_eq_compl_compl_union_compl (s t : set Ξ±) : s ∩ t = (sᢜ βˆͺ tᢜ)ᢜ := ext $ Ξ» x, and_iff_not_or_not @[simp] theorem union_compl_self (s : set Ξ±) : s βˆͺ sᢜ = univ := eq_univ_iff_forall.2 $ Ξ» x, em _ @[simp] theorem compl_union_self (s : set Ξ±) : sᢜ βˆͺ s = univ := by rw [union_comm, union_compl_self] theorem compl_comp_compl : compl ∘ compl = @id (set Ξ±) := funext compl_compl theorem compl_subset_comm {s t : set Ξ±} : sᢜ βŠ† t ↔ tᢜ βŠ† s := @compl_le_iff_compl_le _ s t _ @[simp] lemma compl_subset_compl {s t : set Ξ±} : sᢜ βŠ† tᢜ ↔ t βŠ† s := @compl_le_compl_iff_le _ t s _ theorem compl_subset_iff_union {s t : set Ξ±} : sᢜ βŠ† t ↔ s βˆͺ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ Ξ» a, or_iff_not_imp_left theorem subset_compl_comm {s t : set Ξ±} : s βŠ† tᢜ ↔ t βŠ† sᢜ := forall_congr $ Ξ» a, imp_not_comm theorem subset_compl_iff_disjoint {s t : set Ξ±} : s βŠ† tᢜ ↔ s ∩ t = βˆ… := iff.trans (forall_congr $ Ξ» a, and_imp.symm) subset_empty_iff lemma subset_compl_singleton_iff {a : Ξ±} {s : set Ξ±} : s βŠ† {a}ᢜ ↔ a βˆ‰ s := subset_compl_comm.trans singleton_subset_iff theorem inter_subset (a b c : set Ξ±) : a ∩ b βŠ† c ↔ a βŠ† bᢜ βˆͺ c := forall_congr $ Ξ» x, and_imp.trans $ imp_congr_right $ Ξ» _, imp_iff_not_or lemma inter_compl_nonempty_iff {s t : set Ξ±} : (s ∩ tᢜ).nonempty ↔ Β¬ s βŠ† t := (not_subset.trans $ exists_congr $ by exact Ξ» x, by simp [mem_compl]).symm /-! ### Lemmas about set difference -/ theorem diff_eq (s t : set Ξ±) : s \ t = s ∩ tᢜ := rfl @[simp] theorem mem_diff {s t : set Ξ±} (x : Ξ±) : x ∈ s \ t ↔ x ∈ s ∧ x βˆ‰ t := iff.rfl theorem mem_diff_of_mem {s t : set Ξ±} {x : Ξ±} (h1 : x ∈ s) (h2 : x βˆ‰ t) : x ∈ s \ t := ⟨h1, h2⟩ theorem mem_of_mem_diff {s t : set Ξ±} {x : Ξ±} (h : x ∈ s \ t) : x ∈ s := h.left theorem not_mem_of_mem_diff {s t : set Ξ±} {x : Ξ±} (h : x ∈ s \ t) : x βˆ‰ t := h.right theorem diff_eq_compl_inter {s t : set Ξ±} : s \ t = tᢜ ∩ s := by rw [diff_eq, inter_comm] theorem nonempty_diff {s t : set Ξ±} : (s \ t).nonempty ↔ Β¬ (s βŠ† t) := inter_compl_nonempty_iff theorem diff_subset (s t : set Ξ±) : s \ t βŠ† s := show s \ t ≀ s, from sdiff_le theorem union_diff_cancel' {s t u : set Ξ±} (h₁ : s βŠ† t) (hβ‚‚ : t βŠ† u) : t βˆͺ (u \ s) = u := sup_sdiff_cancel' h₁ hβ‚‚ theorem union_diff_cancel {s t : set Ξ±} (h : s βŠ† t) : s βˆͺ (t \ s) = t := sup_sdiff_of_le h theorem union_diff_cancel_left {s t : set Ξ±} (h : s ∩ t βŠ† βˆ…) : (s βˆͺ t) \ s = t := disjoint.sup_sdiff_cancel_left h theorem union_diff_cancel_right {s t : set Ξ±} (h : s ∩ t βŠ† βˆ…) : (s βˆͺ t) \ t = s := disjoint.sup_sdiff_cancel_right h @[simp] theorem union_diff_left {s t : set Ξ±} : (s βˆͺ t) \ s = t \ s := sup_sdiff_left_self @[simp] theorem union_diff_right {s t : set Ξ±} : (s βˆͺ t) \ t = s \ t := sup_sdiff_right_self theorem union_diff_distrib {s t u : set Ξ±} : (s βˆͺ t) \ u = s \ u βˆͺ t \ u := sup_sdiff theorem inter_diff_assoc (a b c : set Ξ±) : (a ∩ b) \ c = a ∩ (b \ c) := inf_sdiff_assoc @[simp] theorem inter_diff_self (a b : set Ξ±) : a ∩ (b \ a) = βˆ… := inf_sdiff_self_right @[simp] theorem inter_union_diff (s t : set Ξ±) : (s ∩ t) βˆͺ (s \ t) = s := sup_inf_sdiff s t @[simp] lemma diff_union_inter (s t : set Ξ±) : (s \ t) βˆͺ (s ∩ t) = s := by { rw union_comm, exact sup_inf_sdiff _ _ } @[simp] theorem inter_union_compl (s t : set Ξ±) : (s ∩ t) βˆͺ (s ∩ tᢜ) = s := inter_union_diff _ _ theorem diff_subset_diff {s₁ sβ‚‚ t₁ tβ‚‚ : set Ξ±} : s₁ βŠ† sβ‚‚ β†’ tβ‚‚ βŠ† t₁ β†’ s₁ \ t₁ βŠ† sβ‚‚ \ tβ‚‚ := show s₁ ≀ sβ‚‚ β†’ tβ‚‚ ≀ t₁ β†’ s₁ \ t₁ ≀ sβ‚‚ \ tβ‚‚, from sdiff_le_sdiff theorem diff_subset_diff_left {s₁ sβ‚‚ t : set Ξ±} (h : s₁ βŠ† sβ‚‚) : s₁ \ t βŠ† sβ‚‚ \ t := sdiff_le_self_sdiff β€Ήs₁ ≀ sβ‚‚β€Ί theorem diff_subset_diff_right {s t u : set Ξ±} (h : t βŠ† u) : s \ u βŠ† s \ t := sdiff_le_sdiff_self β€Ήt ≀ uβ€Ί theorem compl_eq_univ_diff (s : set Ξ±) : sᢜ = univ \ s := top_sdiff.symm @[simp] lemma empty_diff (s : set Ξ±) : (βˆ… \ s : set Ξ±) = βˆ… := bot_sdiff theorem diff_eq_empty {s t : set Ξ±} : s \ t = βˆ… ↔ s βŠ† t := sdiff_eq_bot_iff @[simp] theorem diff_empty {s : set Ξ±} : s \ βˆ… = s := sdiff_bot @[simp] lemma diff_univ (s : set Ξ±) : s \ univ = βˆ… := diff_eq_empty.2 (subset_univ s) theorem diff_diff {u : set Ξ±} : s \ t \ u = s \ (t βˆͺ u) := sdiff_sdiff_left -- the following statement contains parentheses to help the reader lemma diff_diff_comm {s t u : set Ξ±} : (s \ t) \ u = (s \ u) \ t := sdiff_sdiff_comm lemma diff_subset_iff {s t u : set Ξ±} : s \ t βŠ† u ↔ s βŠ† t βˆͺ u := show s \ t ≀ u ↔ s ≀ t βˆͺ u, from sdiff_le_iff lemma subset_diff_union (s t : set Ξ±) : s βŠ† (s \ t) βˆͺ t := show s ≀ (s \ t) βˆͺ t, from le_sdiff_sup lemma diff_union_of_subset {s t : set Ξ±} (h : t βŠ† s) : (s \ t) βˆͺ t = s := subset.antisymm (union_subset (diff_subset _ _) h) (subset_diff_union _ _) @[simp] lemma diff_singleton_subset_iff {x : Ξ±} {s t : set Ξ±} : s \ {x} βŠ† t ↔ s βŠ† insert x t := by { rw [←union_singleton, union_comm], apply diff_subset_iff } lemma subset_diff_singleton {x : Ξ±} {s t : set Ξ±} (h : s βŠ† t) (hx : x βˆ‰ s) : s βŠ† t \ {x} := subset_inter h $ subset_compl_comm.1 $ singleton_subset_iff.2 hx lemma subset_insert_diff_singleton (x : Ξ±) (s : set Ξ±) : s βŠ† insert x (s \ {x}) := by rw [←diff_singleton_subset_iff] lemma diff_subset_comm {s t u : set Ξ±} : s \ t βŠ† u ↔ s \ u βŠ† t := show s \ t ≀ u ↔ s \ u ≀ t, from sdiff_le_comm lemma diff_inter {s t u : set Ξ±} : s \ (t ∩ u) = (s \ t) βˆͺ (s \ u) := sdiff_inf lemma diff_inter_diff {s t u : set Ξ±} : s \ t ∩ (s \ u) = s \ (t βˆͺ u) := sdiff_sup.symm lemma diff_compl : s \ tᢜ = s ∩ t := sdiff_compl lemma diff_diff_right {s t u : set Ξ±} : s \ (t \ u) = (s \ t) βˆͺ (s ∩ u) := sdiff_sdiff_right' @[simp] theorem insert_diff_of_mem (s) (h : a ∈ t) : insert a s \ t = s \ t := by { ext, split; simp [or_imp_distrib, h] {contextual := tt} } theorem insert_diff_of_not_mem (s) (h : a βˆ‰ t) : insert a s \ t = insert a (s \ t) := begin classical, ext x, by_cases h' : x ∈ t, { have : x β‰  a, { assume H, rw H at h', exact h h' }, simp [h, h', this] }, { simp [h, h'] } end lemma insert_diff_self_of_not_mem {a : Ξ±} {s : set Ξ±} (h : a βˆ‰ s) : insert a s \ {a} = s := by { ext, simp [and_iff_left_of_imp (Ξ» hx : x ∈ s, show x β‰  a, from Ξ» hxa, h $ hxa β–Έ hx)] } lemma insert_inter_of_mem {s₁ sβ‚‚ : set Ξ±} {a : Ξ±} (h : a ∈ sβ‚‚) : insert a s₁ ∩ sβ‚‚ = insert a (s₁ ∩ sβ‚‚) := by simp [set.insert_inter, h] lemma insert_inter_of_not_mem {s₁ sβ‚‚ : set Ξ±} {a : Ξ±} (h : a βˆ‰ sβ‚‚) : insert a s₁ ∩ sβ‚‚ = s₁ ∩ sβ‚‚ := begin ext x, simp only [mem_inter_iff, mem_insert_iff, mem_inter_eq, and.congr_left_iff, or_iff_right_iff_imp], cc, end @[simp] theorem union_diff_self {s t : set Ξ±} : s βˆͺ (t \ s) = s βˆͺ t := sup_sdiff_self_right @[simp] theorem diff_union_self {s t : set Ξ±} : (s \ t) βˆͺ t = s βˆͺ t := sup_sdiff_self_left theorem diff_inter_self {a b : set Ξ±} : (b \ a) ∩ a = βˆ… := inf_sdiff_self_left theorem diff_inter_self_eq_diff {s t : set Ξ±} : s \ (t ∩ s) = s \ t := sdiff_inf_self_right theorem diff_self_inter {s t : set Ξ±} : s \ (s ∩ t) = s \ t := sdiff_inf_self_left theorem diff_eq_self {s t : set Ξ±} : s \ t = s ↔ t ∩ s βŠ† βˆ… := show s \ t = s ↔ t βŠ“ s ≀ βŠ₯, from sdiff_eq_self_iff_disjoint @[simp] theorem diff_singleton_eq_self {a : Ξ±} {s : set Ξ±} (h : a βˆ‰ s) : s \ {a} = s := diff_eq_self.2 $ by simp [singleton_inter_eq_empty.2 h] @[simp] theorem insert_diff_singleton {a : Ξ±} {s : set Ξ±} : insert a (s \ {a}) = insert a s := by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union] @[simp] lemma diff_self {s : set Ξ±} : s \ s = βˆ… := sdiff_self lemma diff_diff_cancel_left {s t : set Ξ±} (h : s βŠ† t) : t \ (t \ s) = s := sdiff_sdiff_eq_self h lemma mem_diff_singleton {x y : Ξ±} {s : set Ξ±} : x ∈ s \ {y} ↔ (x ∈ s ∧ x β‰  y) := iff.rfl lemma mem_diff_singleton_empty {s : set Ξ±} {t : set (set Ξ±)} : s ∈ t \ {βˆ…} ↔ (s ∈ t ∧ s.nonempty) := mem_diff_singleton.trans $ and_congr iff.rfl ne_empty_iff_nonempty lemma union_eq_diff_union_diff_union_inter (s t : set Ξ±) : s βˆͺ t = (s \ t) βˆͺ (t \ s) βˆͺ (s ∩ t) := sup_eq_sdiff_sup_sdiff_sup_inf /-! ### Powerset -/ theorem mem_powerset {x s : set Ξ±} (h : x βŠ† s) : x ∈ powerset s := h theorem subset_of_mem_powerset {x s : set Ξ±} (h : x ∈ powerset s) : x βŠ† s := h @[simp] theorem mem_powerset_iff (x s : set Ξ±) : x ∈ powerset s ↔ x βŠ† s := iff.rfl theorem powerset_inter (s t : set Ξ±) : 𝒫 (s ∩ t) = 𝒫 s ∩ 𝒫 t := ext $ Ξ» u, subset_inter_iff @[simp] theorem powerset_mono : 𝒫 s βŠ† 𝒫 t ↔ s βŠ† t := ⟨λ h, h (subset.refl s), Ξ» h u hu, subset.trans hu h⟩ theorem monotone_powerset : monotone (powerset : set Ξ± β†’ set (set Ξ±)) := Ξ» s t, powerset_mono.2 @[simp] theorem powerset_nonempty : (𝒫 s).nonempty := βŸ¨βˆ…, empty_subset s⟩ @[simp] theorem powerset_empty : 𝒫 (βˆ… : set Ξ±) = {βˆ…} := ext $ Ξ» s, subset_empty_iff @[simp] theorem powerset_univ : 𝒫 (univ : set Ξ±) = univ := eq_univ_of_forall subset_univ /-! ### If-then-else for sets -/ /-- `ite` for sets: `set.ite t s s' ∩ t = s ∩ t`, `set.ite t s s' ∩ tᢜ = s' ∩ tᢜ`. Defined as `s ∩ t βˆͺ s' \ t`. -/ protected def ite (t s s' : set Ξ±) : set Ξ± := s ∩ t βˆͺ s' \ t @[simp] lemma ite_inter_self (t s s' : set Ξ±) : t.ite s s' ∩ t = s ∩ t := by rw [set.ite, union_inter_distrib_right, diff_inter_self, inter_assoc, inter_self, union_empty] @[simp] lemma ite_compl (t s s' : set Ξ±) : tᢜ.ite s s' = t.ite s' s := by rw [set.ite, set.ite, diff_compl, union_comm, diff_eq] @[simp] lemma ite_inter_compl_self (t s s' : set Ξ±) : t.ite s s' ∩ tᢜ = s' ∩ tᢜ := by rw [← ite_compl, ite_inter_self] @[simp] lemma ite_diff_self (t s s' : set Ξ±) : t.ite s s' \ t = s' \ t := ite_inter_compl_self t s s' @[simp] lemma ite_same (t s : set Ξ±) : t.ite s s = s := inter_union_diff _ _ @[simp] lemma ite_left (s t : set Ξ±) : s.ite s t = s βˆͺ t := by simp [set.ite] @[simp] lemma ite_right (s t : set Ξ±) : s.ite t s = t ∩ s := by simp [set.ite] @[simp] lemma ite_empty (s s' : set Ξ±) : set.ite βˆ… s s' = s' := by simp [set.ite] @[simp] lemma ite_univ (s s' : set Ξ±) : set.ite univ s s' = s := by simp [set.ite] @[simp] lemma ite_empty_left (t s : set Ξ±) : t.ite βˆ… s = s \ t := by simp [set.ite] @[simp] lemma ite_empty_right (t s : set Ξ±) : t.ite s βˆ… = s ∩ t := by simp [set.ite] lemma ite_mono (t : set Ξ±) {s₁ s₁' sβ‚‚ sβ‚‚' : set Ξ±} (h : s₁ βŠ† sβ‚‚) (h' : s₁' βŠ† sβ‚‚') : t.ite s₁ s₁' βŠ† t.ite sβ‚‚ sβ‚‚' := union_subset_union (inter_subset_inter_left _ h) (inter_subset_inter_left _ h') lemma ite_subset_union (t s s' : set Ξ±) : t.ite s s' βŠ† s βˆͺ s' := union_subset_union (inter_subset_left _ _) (diff_subset _ _) lemma inter_subset_ite (t s s' : set Ξ±) : s ∩ s' βŠ† t.ite s s' := ite_same t (s ∩ s') β–Έ ite_mono _ (inter_subset_left _ _) (inter_subset_right _ _) lemma ite_inter_inter (t s₁ sβ‚‚ s₁' sβ‚‚' : set Ξ±) : t.ite (s₁ ∩ sβ‚‚) (s₁' ∩ sβ‚‚') = t.ite s₁ s₁' ∩ t.ite sβ‚‚ sβ‚‚' := by { ext x, finish [set.ite, iff_def] } lemma ite_inter (t s₁ sβ‚‚ s : set Ξ±) : t.ite (s₁ ∩ s) (sβ‚‚ ∩ s) = t.ite s₁ sβ‚‚ ∩ s := by rw [ite_inter_inter, ite_same] lemma ite_inter_of_inter_eq (t : set Ξ±) {s₁ sβ‚‚ s : set Ξ±} (h : s₁ ∩ s = sβ‚‚ ∩ s) : t.ite s₁ sβ‚‚ ∩ s = s₁ ∩ s := by rw [← ite_inter, ← h, ite_same] lemma subset_ite {t s s' u : set Ξ±} : u βŠ† t.ite s s' ↔ u ∩ t βŠ† s ∧ u \ t βŠ† s' := begin simp only [subset_def, ← forall_and_distrib], refine forall_congr (Ξ» x, _), by_cases hx : x ∈ t; simp [*, set.ite] end /-! ### Inverse image -/ /-- The preimage of `s : set Ξ²` by `f : Ξ± β†’ Ξ²`, written `f ⁻¹' s`, is the set of `x : Ξ±` such that `f x ∈ s`. -/ def preimage {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β†’ Ξ²) (s : set Ξ²) : set Ξ± := {x | f x ∈ s} infix ` ⁻¹' `:80 := preimage section preimage variables {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} @[simp] theorem preimage_empty : f ⁻¹' βˆ… = βˆ… := rfl @[simp] theorem mem_preimage {s : set Ξ²} {a : Ξ±} : (a ∈ f ⁻¹' s) ↔ (f a ∈ s) := iff.rfl lemma preimage_congr {f g : Ξ± β†’ Ξ²} {s : set Ξ²} (h : βˆ€ (x : Ξ±), f x = g x) : f ⁻¹' s = g ⁻¹' s := by { congr' with x, apply_assumption } theorem preimage_mono {s t : set Ξ²} (h : s βŠ† t) : f ⁻¹' s βŠ† f ⁻¹' t := assume x hx, h hx @[simp] theorem preimage_univ : f ⁻¹' univ = univ := rfl theorem subset_preimage_univ {s : set Ξ±} : s βŠ† f ⁻¹' univ := subset_univ _ @[simp] theorem preimage_inter {s t : set Ξ²} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl @[simp] theorem preimage_union {s t : set Ξ²} : f ⁻¹' (s βˆͺ t) = f ⁻¹' s βˆͺ f ⁻¹' t := rfl @[simp] theorem preimage_compl {s : set Ξ²} : f ⁻¹' sᢜ = (f ⁻¹' s)ᢜ := rfl @[simp] theorem preimage_diff (f : Ξ± β†’ Ξ²) (s t : set Ξ²) : f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl @[simp] theorem preimage_ite (f : Ξ± β†’ Ξ²) (s t₁ tβ‚‚ : set Ξ²) : f ⁻¹' (s.ite t₁ tβ‚‚) = (f ⁻¹' s).ite (f ⁻¹' t₁) (f ⁻¹' tβ‚‚) := rfl @[simp] theorem preimage_set_of_eq {p : Ξ± β†’ Prop} {f : Ξ² β†’ Ξ±} : f ⁻¹' {a | p a} = {a | p (f a)} := rfl @[simp] theorem preimage_id {s : set Ξ±} : id ⁻¹' s = s := rfl @[simp] theorem preimage_id' {s : set Ξ±} : (Ξ» x, x) ⁻¹' s = s := rfl theorem preimage_const_of_mem {b : Ξ²} {s : set Ξ²} (h : b ∈ s) : (Ξ» (x : Ξ±), b) ⁻¹' s = univ := eq_univ_of_forall $ Ξ» x, h theorem preimage_const_of_not_mem {b : Ξ²} {s : set Ξ²} (h : b βˆ‰ s) : (Ξ» (x : Ξ±), b) ⁻¹' s = βˆ… := eq_empty_of_subset_empty $ Ξ» x hx, h hx theorem preimage_const (b : Ξ²) (s : set Ξ²) [decidable (b ∈ s)] : (Ξ» (x : Ξ±), b) ⁻¹' s = if b ∈ s then univ else βˆ… := by { split_ifs with hb hb, exacts [preimage_const_of_mem hb, preimage_const_of_not_mem hb] } theorem preimage_comp {s : set Ξ³} : (g ∘ f) ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl lemma preimage_preimage {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ³} : f ⁻¹' (g ⁻¹' s) = (Ξ» x, g (f x)) ⁻¹' s := preimage_comp.symm theorem eq_preimage_subtype_val_iff {p : Ξ± β†’ Prop} {s : set (subtype p)} {t : set Ξ±} : s = subtype.val ⁻¹' t ↔ (βˆ€x (h : p x), (⟨x, h⟩ : subtype p) ∈ s ↔ x ∈ t) := ⟨assume s_eq x h, by { rw [s_eq], simp }, assume h, ext $ Ξ» ⟨x, hx⟩, by simp [h]⟩ lemma preimage_coe_coe_diagonal {Ξ± : Type*} (s : set Ξ±) : (prod.map coe coe) ⁻¹' (diagonal Ξ±) = diagonal s := begin ext ⟨⟨x, x_in⟩, ⟨y, y_in⟩⟩, simp [set.diagonal], end end preimage /-! ### Image of a set under a function -/ section image infix ` '' `:80 := image theorem mem_image_iff_bex {f : Ξ± β†’ Ξ²} {s : set Ξ±} {y : Ξ²} : y ∈ f '' s ↔ βˆƒ x (_ : x ∈ s), f x = y := bex_def.symm theorem mem_image_eq (f : Ξ± β†’ Ξ²) (s : set Ξ±) (y: Ξ²) : y ∈ f '' s = βˆƒ x, x ∈ s ∧ f x = y := rfl @[simp] theorem mem_image (f : Ξ± β†’ Ξ²) (s : set Ξ±) (y : Ξ²) : y ∈ f '' s ↔ βˆƒ x, x ∈ s ∧ f x = y := iff.rfl lemma image_eta (f : Ξ± β†’ Ξ²) : f '' s = (Ξ» x, f x) '' s := rfl theorem mem_image_of_mem (f : Ξ± β†’ Ξ²) {x : Ξ±} {a : set Ξ±} (h : x ∈ a) : f x ∈ f '' a := ⟨_, h, rfl⟩ theorem mem_image_of_injective {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} (hf : injective f) : f a ∈ f '' s ↔ a ∈ s := iff.intro (assume ⟨b, hb, eq⟩, (hf eq) β–Έ hb) (assume h, mem_image_of_mem _ h) theorem ball_image_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} {p : Ξ² β†’ Prop} : (βˆ€ y ∈ f '' s, p y) ↔ (βˆ€ x ∈ s, p (f x)) := by simp theorem ball_image_of_ball {f : Ξ± β†’ Ξ²} {s : set Ξ±} {p : Ξ² β†’ Prop} (h : βˆ€ x ∈ s, p (f x)) : βˆ€ y ∈ f '' s, p y := ball_image_iff.2 h theorem bex_image_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} {p : Ξ² β†’ Prop} : (βˆƒ y ∈ f '' s, p y) ↔ (βˆƒ x ∈ s, p (f x)) := by simp theorem mem_image_elim {f : Ξ± β†’ Ξ²} {s : set Ξ±} {C : Ξ² β†’ Prop} (h : βˆ€ (x : Ξ±), x ∈ s β†’ C (f x)) : βˆ€{y : Ξ²}, y ∈ f '' s β†’ C y | ._ ⟨a, a_in, rfl⟩ := h a a_in theorem mem_image_elim_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} {C : Ξ² β†’ Prop} {y : Ξ²} (h_y : y ∈ f '' s) (h : βˆ€ (x : Ξ±), x ∈ s β†’ C (f x)) : C y := mem_image_elim h h_y @[congr] lemma image_congr {f g : Ξ± β†’ Ξ²} {s : set Ξ±} (h : βˆ€a∈s, f a = g a) : f '' s = g '' s := by safe [ext_iff, iff_def] /-- A common special case of `image_congr` -/ lemma image_congr' {f g : Ξ± β†’ Ξ²} {s : set Ξ±} (h : βˆ€ (x : Ξ±), f x = g x) : f '' s = g '' s := image_congr (Ξ»x _, h x) theorem image_comp (f : Ξ² β†’ Ξ³) (g : Ξ± β†’ Ξ²) (a : set Ξ±) : (f ∘ g) '' a = f '' (g '' a) := subset.antisymm (ball_image_of_ball $ assume a ha, mem_image_of_mem _ $ mem_image_of_mem _ ha) (ball_image_of_ball $ ball_image_of_ball $ assume a ha, mem_image_of_mem _ ha) /-- A variant of `image_comp`, useful for rewriting -/ lemma image_image (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ²) (s : set Ξ±) : g '' (f '' s) = (Ξ» x, g (f x)) '' s := (image_comp g f s).symm /-- Image is monotone with respect to `βŠ†`. See `set.monotone_image` for the statement in terms of `≀`. -/ theorem image_subset {a b : set Ξ±} (f : Ξ± β†’ Ξ²) (h : a βŠ† b) : f '' a βŠ† f '' b := by finish [subset_def, mem_image_eq] theorem image_union (f : Ξ± β†’ Ξ²) (s t : set Ξ±) : f '' (s βˆͺ t) = f '' s βˆͺ f '' t := ext $ Ξ» x, ⟨by rintro ⟨a, h|h, rfl⟩; [left, right]; exact ⟨_, h, rfl⟩, by rintro (⟨a, h, rfl⟩ | ⟨a, h, rfl⟩); refine ⟨_, _, rfl⟩; [left, right]; exact h⟩ @[simp] theorem image_empty (f : Ξ± β†’ Ξ²) : f '' βˆ… = βˆ… := by { ext, simp } lemma image_inter_subset (f : Ξ± β†’ Ξ²) (s t : set Ξ±) : f '' (s ∩ t) βŠ† f '' s ∩ f '' t := subset_inter (image_subset _ $ inter_subset_left _ _) (image_subset _ $ inter_subset_right _ _) theorem image_inter_on {f : Ξ± β†’ Ξ²} {s t : set Ξ±} (h : βˆ€x∈t, βˆ€y∈s, f x = f y β†’ x = y) : f '' s ∩ f '' t = f '' (s ∩ t) := subset.antisymm (assume b ⟨⟨a₁, ha₁, hβ‚βŸ©, ⟨aβ‚‚, haβ‚‚, hβ‚‚βŸ©βŸ©, have aβ‚‚ = a₁, from h _ haβ‚‚ _ ha₁ (by simp *), ⟨a₁, ⟨ha₁, this β–Έ haβ‚‚βŸ©, hβ‚βŸ©) (image_inter_subset _ _ _) theorem image_inter {f : Ξ± β†’ Ξ²} {s t : set Ξ±} (H : injective f) : f '' s ∩ f '' t = f '' (s ∩ t) := image_inter_on (assume x _ y _ h, H h) theorem image_univ_of_surjective {ΞΉ : Type*} {f : ΞΉ β†’ Ξ²} (H : surjective f) : f '' univ = univ := eq_univ_of_forall $ by { simpa [image] } @[simp] theorem image_singleton {f : Ξ± β†’ Ξ²} {a : Ξ±} : f '' {a} = {f a} := by { ext, simp [image, eq_comm] } @[simp] theorem nonempty.image_const {s : set Ξ±} (hs : s.nonempty) (a : Ξ²) : (Ξ» _, a) '' s = {a} := ext $ Ξ» x, ⟨λ ⟨y, _, h⟩, h β–Έ mem_singleton _, Ξ» h, (eq_of_mem_singleton h).symm β–Έ hs.imp (Ξ» y hy, ⟨hy, rfl⟩)⟩ @[simp] lemma image_eq_empty {Ξ± Ξ²} {f : Ξ± β†’ Ξ²} {s : set Ξ±} : f '' s = βˆ… ↔ s = βˆ… := by { simp only [eq_empty_iff_forall_not_mem], exact ⟨λ H a ha, H _ ⟨_, ha, rfl⟩, Ξ» H b ⟨_, ha, _⟩, H _ ha⟩ } -- TODO(Jeremy): there is an issue with - t unfolding to compl t theorem mem_compl_image (t : set Ξ±) (S : set (set Ξ±)) : t ∈ compl '' S ↔ tᢜ ∈ S := begin suffices : βˆ€ x, xᢜ = t ↔ tᢜ = x, { simp [this] }, intro x, split; { intro e, subst e, simp } end /-- A variant of `image_id` -/ @[simp] lemma image_id' (s : set Ξ±) : (Ξ»x, x) '' s = s := by { ext, simp } theorem image_id (s : set Ξ±) : id '' s = s := by simp theorem compl_compl_image (S : set (set Ξ±)) : compl '' (compl '' S) = S := by rw [← image_comp, compl_comp_compl, image_id] theorem image_insert_eq {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} : f '' (insert a s) = insert (f a) (f '' s) := by { ext, simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm] } theorem image_pair (f : Ξ± β†’ Ξ²) (a b : Ξ±) : f '' {a, b} = {f a, f b} := by simp only [image_insert_eq, image_singleton] theorem image_subset_preimage_of_inverse {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} (I : left_inverse g f) (s : set Ξ±) : f '' s βŠ† g ⁻¹' s := Ξ» b ⟨a, h, e⟩, e β–Έ ((I a).symm β–Έ h : g (f a) ∈ s) theorem preimage_subset_image_of_inverse {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} (I : left_inverse g f) (s : set Ξ²) : f ⁻¹' s βŠ† g '' s := Ξ» b h, ⟨f b, h, I b⟩ theorem image_eq_preimage_of_inverse {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} (h₁ : left_inverse g f) (hβ‚‚ : right_inverse g f) : image f = preimage g := funext $ Ξ» s, subset.antisymm (image_subset_preimage_of_inverse h₁ s) (preimage_subset_image_of_inverse hβ‚‚ s) theorem mem_image_iff_of_inverse {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} {b : Ξ²} {s : set Ξ±} (h₁ : left_inverse g f) (hβ‚‚ : right_inverse g f) : b ∈ f '' s ↔ g b ∈ s := by rw image_eq_preimage_of_inverse h₁ hβ‚‚; refl theorem image_compl_subset {f : Ξ± β†’ Ξ²} {s : set Ξ±} (H : injective f) : f '' sᢜ βŠ† (f '' s)ᢜ := subset_compl_iff_disjoint.2 $ by simp [image_inter H] theorem subset_image_compl {f : Ξ± β†’ Ξ²} {s : set Ξ±} (H : surjective f) : (f '' s)ᢜ βŠ† f '' sᢜ := compl_subset_iff_union.2 $ by { rw ← image_union, simp [image_univ_of_surjective H] } theorem image_compl_eq {f : Ξ± β†’ Ξ²} {s : set Ξ±} (H : bijective f) : f '' sᢜ = (f '' s)ᢜ := subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2) theorem subset_image_diff (f : Ξ± β†’ Ξ²) (s t : set Ξ±) : f '' s \ f '' t βŠ† f '' (s \ t) := begin rw [diff_subset_iff, ← image_union, union_diff_self], exact image_subset f (subset_union_right t s) end theorem image_diff {f : Ξ± β†’ Ξ²} (hf : injective f) (s t : set Ξ±) : f '' (s \ t) = f '' s \ f '' t := subset.antisymm (subset.trans (image_inter_subset _ _ _) $ inter_subset_inter_right _ $ image_compl_subset hf) (subset_image_diff f s t) lemma nonempty.image (f : Ξ± β†’ Ξ²) {s : set Ξ±} : s.nonempty β†’ (f '' s).nonempty | ⟨x, hx⟩ := ⟨f x, mem_image_of_mem f hx⟩ lemma nonempty.of_image {f : Ξ± β†’ Ξ²} {s : set Ξ±} : (f '' s).nonempty β†’ s.nonempty | ⟨y, x, hx, _⟩ := ⟨x, hx⟩ @[simp] lemma nonempty_image_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} : (f '' s).nonempty ↔ s.nonempty := ⟨nonempty.of_image, Ξ» h, h.image f⟩ lemma nonempty.preimage {s : set Ξ²} (hs : s.nonempty) {f : Ξ± β†’ Ξ²} (hf : surjective f) : (f ⁻¹' s).nonempty := let ⟨y, hy⟩ := hs, ⟨x, hx⟩ := hf y in ⟨x, mem_preimage.2 $ hx.symm β–Έ hy⟩ instance (f : Ξ± β†’ Ξ²) (s : set Ξ±) [nonempty s] : nonempty (f '' s) := (set.nonempty.image f nonempty_of_nonempty_subtype).to_subtype /-- image and preimage are a Galois connection -/ @[simp] theorem image_subset_iff {s : set Ξ±} {t : set Ξ²} {f : Ξ± β†’ Ξ²} : f '' s βŠ† t ↔ s βŠ† f ⁻¹' t := ball_image_iff theorem image_preimage_subset (f : Ξ± β†’ Ξ²) (s : set Ξ²) : f '' (f ⁻¹' s) βŠ† s := image_subset_iff.2 (subset.refl _) theorem subset_preimage_image (f : Ξ± β†’ Ξ²) (s : set Ξ±) : s βŠ† f ⁻¹' (f '' s) := Ξ» x, mem_image_of_mem f theorem preimage_image_eq {f : Ξ± β†’ Ξ²} (s : set Ξ±) (h : injective f) : f ⁻¹' (f '' s) = s := subset.antisymm (Ξ» x ⟨y, hy, e⟩, h e β–Έ hy) (subset_preimage_image f s) theorem image_preimage_eq {f : Ξ± β†’ Ξ²} (s : set Ξ²) (h : surjective f) : f '' (f ⁻¹' s) = s := subset.antisymm (image_preimage_subset f s) (Ξ» x hx, let ⟨y, e⟩ := h x in ⟨y, (e.symm β–Έ hx : f y ∈ s), e⟩) lemma preimage_eq_preimage {f : Ξ² β†’ Ξ±} (hf : surjective f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := iff.intro (assume eq, by rw [← image_preimage_eq s hf, ← image_preimage_eq t hf, eq]) (assume eq, eq β–Έ rfl) lemma image_inter_preimage (f : Ξ± β†’ Ξ²) (s : set Ξ±) (t : set Ξ²) : f '' (s ∩ f ⁻¹' t) = f '' s ∩ t := begin apply subset.antisymm, { calc f '' (s ∩ f ⁻¹' t) βŠ† f '' s ∩ (f '' (f⁻¹' t)) : image_inter_subset _ _ _ ... βŠ† f '' s ∩ t : inter_subset_inter_right _ (image_preimage_subset f t) }, { rintros _ ⟨⟨x, h', rfl⟩, h⟩, exact ⟨x, ⟨h', h⟩, rfl⟩ } end lemma image_preimage_inter (f : Ξ± β†’ Ξ²) (s : set Ξ±) (t : set Ξ²) : f '' (f ⁻¹' t ∩ s) = t ∩ f '' s := by simp only [inter_comm, image_inter_preimage] @[simp] lemma image_inter_nonempty_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} : (f '' s ∩ t).nonempty ↔ (s ∩ f ⁻¹' t).nonempty := by rw [←image_inter_preimage, nonempty_image_iff] lemma image_diff_preimage {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} : f '' (s \ f ⁻¹' t) = f '' s \ t := by simp_rw [diff_eq, ← preimage_compl, image_inter_preimage] theorem compl_image : image (compl : set Ξ± β†’ set Ξ±) = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {p : set Ξ± β†’ Prop} : compl '' {s | p s} = {s | p sᢜ} := congr_fun compl_image p theorem inter_preimage_subset (s : set Ξ±) (t : set Ξ²) (f : Ξ± β†’ Ξ²) : s ∩ f ⁻¹' t βŠ† f ⁻¹' (f '' s ∩ t) := Ξ» x h, ⟨mem_image_of_mem _ h.left, h.right⟩ theorem union_preimage_subset (s : set Ξ±) (t : set Ξ²) (f : Ξ± β†’ Ξ²) : s βˆͺ f ⁻¹' t βŠ† f ⁻¹' (f '' s βˆͺ t) := Ξ» x h, or.elim h (Ξ» l, or.inl $ mem_image_of_mem _ l) (Ξ» r, or.inr r) theorem subset_image_union (f : Ξ± β†’ Ξ²) (s : set Ξ±) (t : set Ξ²) : f '' (s βˆͺ f ⁻¹' t) βŠ† f '' s βˆͺ t := image_subset_iff.2 (union_preimage_subset _ _ _) lemma preimage_subset_iff {A : set Ξ±} {B : set Ξ²} {f : Ξ± β†’ Ξ²} : f⁻¹' B βŠ† A ↔ (βˆ€ a : Ξ±, f a ∈ B β†’ a ∈ A) := iff.rfl lemma image_eq_image {f : Ξ± β†’ Ξ²} (hf : injective f) : f '' s = f '' t ↔ s = t := iff.symm $ iff.intro (assume eq, eq β–Έ rfl) $ assume eq, by rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, eq] lemma image_subset_image_iff {f : Ξ± β†’ Ξ²} (hf : injective f) : f '' s βŠ† f '' t ↔ s βŠ† t := begin refine (iff.symm $ iff.intro (image_subset f) $ assume h, _), rw [← preimage_image_eq s hf, ← preimage_image_eq t hf], exact preimage_mono h end lemma prod_quotient_preimage_eq_image [s : setoid Ξ±] (g : quotient s β†’ Ξ²) {h : Ξ± β†’ Ξ²} (Hh : h = g ∘ quotient.mk) (r : set (Ξ² Γ— Ξ²)) : {x : quotient s Γ— quotient s | (g x.1, g x.2) ∈ r} = (Ξ» a : Ξ± Γ— Ξ±, (⟦a.1⟧, ⟦a.2⟧)) '' ((Ξ» a : Ξ± Γ— Ξ±, (h a.1, h a.2)) ⁻¹' r) := Hh.symm β–Έ set.ext (Ξ» ⟨a₁, aβ‚‚βŸ©, ⟨quotient.induction_onβ‚‚ a₁ aβ‚‚ (Ξ» a₁ aβ‚‚ h, ⟨(a₁, aβ‚‚), h, rfl⟩), Ξ» ⟨⟨b₁, bβ‚‚βŸ©, h₁, hβ‚‚βŸ©, show (g a₁, g aβ‚‚) ∈ r, from have h₃ : ⟦bβ‚βŸ§ = a₁ ∧ ⟦bβ‚‚βŸ§ = aβ‚‚ := prod.ext_iff.1 hβ‚‚, h₃.1 β–Έ h₃.2 β–Έ hβ‚βŸ©) /-- Restriction of `f` to `s` factors through `s.image_factorization f : s β†’ f '' s`. -/ def image_factorization (f : Ξ± β†’ Ξ²) (s : set Ξ±) : s β†’ f '' s := Ξ» p, ⟨f p.1, mem_image_of_mem f p.2⟩ lemma image_factorization_eq {f : Ξ± β†’ Ξ²} {s : set Ξ±} : subtype.val ∘ image_factorization f s = f ∘ subtype.val := funext $ Ξ» p, rfl lemma surjective_onto_image {f : Ξ± β†’ Ξ²} {s : set Ξ±} : surjective (image_factorization f s) := Ξ» ⟨_, ⟨a, ha, rfl⟩⟩, ⟨⟨a, ha⟩, rfl⟩ end image /-! ### Subsingleton -/ /-- A set `s` is a `subsingleton`, if it has at most one element. -/ protected def subsingleton (s : set Ξ±) : Prop := βˆ€ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), x = y lemma subsingleton.mono (ht : t.subsingleton) (hst : s βŠ† t) : s.subsingleton := Ξ» x hx y hy, ht (hst hx) (hst hy) lemma subsingleton.image (hs : s.subsingleton) (f : Ξ± β†’ Ξ²) : (f '' s).subsingleton := Ξ» _ ⟨x, hx, Hx⟩ _ ⟨y, hy, Hy⟩, Hx β–Έ Hy β–Έ congr_arg f (hs hx hy) lemma subsingleton.eq_singleton_of_mem (hs : s.subsingleton) {x:Ξ±} (hx : x ∈ s) : s = {x} := ext $ Ξ» y, ⟨λ hy, (hs hx hy) β–Έ mem_singleton _, Ξ» hy, (eq_of_mem_singleton hy).symm β–Έ hx⟩ @[simp] lemma subsingleton_empty : (βˆ… : set Ξ±).subsingleton := Ξ» x, false.elim @[simp] lemma subsingleton_singleton {a} : ({a} : set Ξ±).subsingleton := Ξ» x hx y hy, (eq_of_mem_singleton hx).symm β–Έ (eq_of_mem_singleton hy).symm β–Έ rfl lemma subsingleton_iff_singleton {x} (hx : x ∈ s) : s.subsingleton ↔ s = {x} := ⟨λ h, h.eq_singleton_of_mem hx, Ξ» h,h.symm β–Έ subsingleton_singleton⟩ lemma subsingleton.eq_empty_or_singleton (hs : s.subsingleton) : s = βˆ… ∨ βˆƒ x, s = {x} := s.eq_empty_or_nonempty.elim or.inl (Ξ» ⟨x, hx⟩, or.inr ⟨x, hs.eq_singleton_of_mem hx⟩) lemma subsingleton.induction_on {p : set Ξ± β†’ Prop} (hs : s.subsingleton) (he : p βˆ…) (h₁ : βˆ€ x, p {x}) : p s := by { rcases hs.eq_empty_or_singleton with rfl|⟨x, rfl⟩, exacts [he, h₁ _] } lemma subsingleton_univ [subsingleton Ξ±] : (univ : set Ξ±).subsingleton := Ξ» x hx y hy, subsingleton.elim x y /-- `s`, coerced to a type, is a subsingleton type if and only if `s` is a subsingleton set. -/ @[simp, norm_cast] lemma subsingleton_coe (s : set Ξ±) : subsingleton s ↔ s.subsingleton := begin split, { refine Ξ» h, (Ξ» a ha b hb, _), exact set_coe.ext_iff.2 (@subsingleton.elim s h ⟨a, ha⟩ ⟨b, hb⟩) }, { exact Ξ» h, subsingleton.intro (Ξ» a b, set_coe.ext (h a.property b.property)) } end /-- `s` is a subsingleton, if its image of an injective function is. -/ theorem subsingleton_of_image {Ξ± Ξ² : Type*} {f : Ξ± β†’ Ξ²} (hf : function.injective f) (s : set Ξ±) (hs : (f '' s).subsingleton) : s.subsingleton := Ξ» a ha b hb, hf $ hs (mem_image_of_mem _ ha) (mem_image_of_mem _ hb) theorem univ_eq_true_false : univ = ({true, false} : set Prop) := eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp) /-! ### Lemmas about range of a function. -/ section range variables {f : ΞΉ β†’ Ξ±} open function /-- Range of a function. This function is more flexible than `f '' univ`, as the image requires that the domain is in Type and not an arbitrary Sort. -/ def range (f : ΞΉ β†’ Ξ±) : set Ξ± := {x | βˆƒy, f y = x} @[simp] theorem mem_range {x : Ξ±} : x ∈ range f ↔ βˆƒ y, f y = x := iff.rfl @[simp] theorem mem_range_self (i : ΞΉ) : f i ∈ range f := ⟨i, rfl⟩ theorem forall_range_iff {p : Ξ± β†’ Prop} : (βˆ€ a ∈ range f, p a) ↔ (βˆ€ i, p (f i)) := by simp theorem exists_range_iff {p : Ξ± β†’ Prop} : (βˆƒ a ∈ range f, p a) ↔ (βˆƒ i, p (f i)) := by simp lemma exists_range_iff' {p : Ξ± β†’ Prop} : (βˆƒ a, a ∈ range f ∧ p a) ↔ βˆƒ i, p (f i) := by simpa only [exists_prop] using exists_range_iff theorem range_iff_surjective : range f = univ ↔ surjective f := eq_univ_iff_forall alias range_iff_surjective ↔ _ function.surjective.range_eq @[simp] theorem range_id : range (@id Ξ±) = univ := range_iff_surjective.2 surjective_id theorem is_compl_range_inl_range_inr : is_compl (range $ @sum.inl Ξ± Ξ²) (range sum.inr) := ⟨by { rintro y ⟨⟨x₁, rfl⟩, ⟨xβ‚‚, _⟩⟩, cc }, by { rintro (x|y) -; [left, right]; exact mem_range_self _ }⟩ @[simp] theorem range_inl_union_range_inr : range (sum.inl : Ξ± β†’ Ξ± βŠ• Ξ²) βˆͺ range sum.inr = univ := is_compl_range_inl_range_inr.sup_eq_top @[simp] theorem range_inl_inter_range_inr : range (sum.inl : Ξ± β†’ Ξ± βŠ• Ξ²) ∩ range sum.inr = βˆ… := is_compl_range_inl_range_inr.inf_eq_bot @[simp] theorem range_inr_union_range_inl : range (sum.inr : Ξ² β†’ Ξ± βŠ• Ξ²) βˆͺ range sum.inl = univ := is_compl_range_inl_range_inr.symm.sup_eq_top @[simp] theorem range_inr_inter_range_inl : range (sum.inr : Ξ² β†’ Ξ± βŠ• Ξ²) ∩ range sum.inl = βˆ… := is_compl_range_inl_range_inr.symm.inf_eq_bot @[simp] theorem preimage_inl_range_inr : sum.inl ⁻¹' range (sum.inr : Ξ² β†’ Ξ± βŠ• Ξ²) = βˆ… := by { ext, simp } @[simp] theorem preimage_inr_range_inl : sum.inr ⁻¹' range (sum.inl : Ξ± β†’ Ξ± βŠ• Ξ²) = βˆ… := by { ext, simp } @[simp] theorem range_quot_mk (r : Ξ± β†’ Ξ± β†’ Prop) : range (quot.mk r) = univ := (surjective_quot_mk r).range_eq @[simp] theorem image_univ {ΞΉ : Type*} {f : ΞΉ β†’ Ξ²} : f '' univ = range f := by { ext, simp [image, range] } theorem image_subset_range {ΞΉ : Type*} (f : ΞΉ β†’ Ξ²) (s : set ΞΉ) : f '' s βŠ† range f := by rw ← image_univ; exact image_subset _ (subset_univ _) theorem range_comp (g : Ξ± β†’ Ξ²) (f : ΞΉ β†’ Ξ±) : range (g ∘ f) = g '' range f := subset.antisymm (forall_range_iff.mpr $ assume i, mem_image_of_mem g (mem_range_self _)) (ball_image_iff.mpr $ forall_range_iff.mpr mem_range_self) theorem range_subset_iff {s : set Ξ±} : range f βŠ† s ↔ βˆ€ y, f y ∈ s := forall_range_iff lemma range_comp_subset_range (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ Ξ³) : range (g ∘ f) βŠ† range g := by rw range_comp; apply image_subset_range lemma range_nonempty_iff_nonempty : (range f).nonempty ↔ nonempty ΞΉ := ⟨λ ⟨y, x, hxy⟩, ⟨x⟩, Ξ» ⟨x⟩, ⟨f x, mem_range_self x⟩⟩ lemma range_nonempty [h : nonempty ΞΉ] (f : ΞΉ β†’ Ξ±) : (range f).nonempty := range_nonempty_iff_nonempty.2 h @[simp] lemma range_eq_empty {f : ΞΉ β†’ Ξ±} : range f = βˆ… ↔ Β¬ nonempty ΞΉ := not_nonempty_iff_eq_empty.symm.trans $ not_congr range_nonempty_iff_nonempty instance [nonempty ΞΉ] (f : ΞΉ β†’ Ξ±) : nonempty (range f) := (range_nonempty f).to_subtype @[simp] lemma image_union_image_compl_eq_range (f : Ξ± β†’ Ξ²) : (f '' s) βˆͺ (f '' sᢜ) = range f := by rw [← image_union, ← image_univ, ← union_compl_self] theorem image_preimage_eq_inter_range {f : Ξ± β†’ Ξ²} {t : set Ξ²} : f '' (f ⁻¹' t) = t ∩ range f := ext $ assume x, ⟨assume ⟨x, hx, heq⟩, heq β–Έ ⟨hx, mem_range_self _⟩, assume ⟨hx, ⟨y, h_eq⟩⟩, h_eq β–Έ mem_image_of_mem f $ show y ∈ f ⁻¹' t, by simp [preimage, h_eq, hx]⟩ lemma image_preimage_eq_of_subset {f : Ξ± β†’ Ξ²} {s : set Ξ²} (hs : s βŠ† range f) : f '' (f ⁻¹' s) = s := by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs] lemma image_preimage_eq_iff {f : Ξ± β†’ Ξ²} {s : set Ξ²} : f '' (f ⁻¹' s) = s ↔ s βŠ† range f := ⟨by { intro h, rw [← h], apply image_subset_range }, image_preimage_eq_of_subset⟩ lemma preimage_subset_preimage_iff {s t : set Ξ±} {f : Ξ² β†’ Ξ±} (hs : s βŠ† range f) : f ⁻¹' s βŠ† f ⁻¹' t ↔ s βŠ† t := begin split, { intros h x hx, rcases hs hx with ⟨y, rfl⟩, exact h hx }, intros h x, apply h end lemma preimage_eq_preimage' {s t : set Ξ±} {f : Ξ² β†’ Ξ±} (hs : s βŠ† range f) (ht : t βŠ† range f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := begin split, { intro h, apply subset.antisymm, rw [←preimage_subset_preimage_iff hs, h], rw [←preimage_subset_preimage_iff ht, h] }, rintro rfl, refl end @[simp] theorem preimage_inter_range {f : Ξ± β†’ Ξ²} {s : set Ξ²} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := set.ext $ Ξ» x, and_iff_left ⟨x, rfl⟩ @[simp] theorem preimage_range_inter {f : Ξ± β†’ Ξ²} {s : set Ξ²} : f ⁻¹' (range f ∩ s) = f ⁻¹' s := by rw [inter_comm, preimage_inter_range] theorem preimage_image_preimage {f : Ξ± β†’ Ξ²} {s : set Ξ²} : f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s := by rw [image_preimage_eq_inter_range, preimage_inter_range] @[simp] theorem quot_mk_range_eq [setoid Ξ±] : range (Ξ»x : Ξ±, ⟦x⟧) = univ := range_iff_surjective.2 quot.exists_rep lemma range_const_subset {c : Ξ±} : range (Ξ»x:ΞΉ, c) βŠ† {c} := range_subset_iff.2 $ Ξ» x, rfl @[simp] lemma range_const : βˆ€ [nonempty ΞΉ] {c : Ξ±}, range (Ξ»x:ΞΉ, c) = {c} | ⟨x⟩ c := subset.antisymm range_const_subset $ assume y hy, (mem_singleton_iff.1 hy).symm β–Έ mem_range_self x lemma diagonal_eq_range {Ξ± : Type*} : diagonal Ξ± = range (Ξ» x, (x, x)) := by { ext ⟨x, y⟩, simp [diagonal, eq_comm] } theorem preimage_singleton_nonempty {f : Ξ± β†’ Ξ²} {y : Ξ²} : (f ⁻¹' {y}).nonempty ↔ y ∈ range f := iff.rfl theorem preimage_singleton_eq_empty {f : Ξ± β†’ Ξ²} {y : Ξ²} : f ⁻¹' {y} = βˆ… ↔ y βˆ‰ range f := not_nonempty_iff_eq_empty.symm.trans $ not_congr preimage_singleton_nonempty lemma range_subset_singleton {f : ΞΉ β†’ Ξ±} {x : Ξ±} : range f βŠ† {x} ↔ f = const ΞΉ x := by simp [range_subset_iff, funext_iff, mem_singleton] lemma image_compl_preimage {f : Ξ± β†’ Ξ²} {s : set Ξ²} : f '' ((f ⁻¹' s)ᢜ) = range f \ s := by rw [compl_eq_univ_diff, image_diff_preimage, image_univ] @[simp] theorem range_sigma_mk {Ξ² : Ξ± β†’ Type*} (a : Ξ±) : range (sigma.mk a : Ξ² a β†’ Ξ£ a, Ξ² a) = sigma.fst ⁻¹' {a} := begin apply subset.antisymm, { rintros _ ⟨b, rfl⟩, simp }, { rintros ⟨x, y⟩ (rfl|_), exact mem_range_self y } end /-- Any map `f : ΞΉ β†’ Ξ²` factors through a map `range_factorization f : ΞΉ β†’ range f`. -/ def range_factorization (f : ΞΉ β†’ Ξ²) : ΞΉ β†’ range f := Ξ» i, ⟨f i, mem_range_self i⟩ lemma range_factorization_eq {f : ΞΉ β†’ Ξ²} : subtype.val ∘ range_factorization f = f := funext $ Ξ» i, rfl lemma surjective_onto_range : surjective (range_factorization f) := Ξ» ⟨_, ⟨i, rfl⟩⟩, ⟨i, rfl⟩ lemma image_eq_range (f : Ξ± β†’ Ξ²) (s : set Ξ±) : f '' s = range (Ξ»(x : s), f x) := by { ext, split, rintro ⟨x, h1, h2⟩, exact ⟨⟨x, h1⟩, h2⟩, rintro ⟨⟨x, h1⟩, h2⟩, exact ⟨x, h1, h2⟩ } @[simp] lemma sum.elim_range {Ξ± Ξ² Ξ³ : Type*} (f : Ξ± β†’ Ξ³) (g : Ξ² β†’ Ξ³) : range (sum.elim f g) = range f βˆͺ range g := by simp [set.ext_iff, mem_range] lemma range_ite_subset' {p : Prop} [decidable p] {f g : Ξ± β†’ Ξ²} : range (if p then f else g) βŠ† range f βˆͺ range g := begin by_cases h : p, {rw if_pos h, exact subset_union_left _ _}, {rw if_neg h, exact subset_union_right _ _} end lemma range_ite_subset {p : Ξ± β†’ Prop} [decidable_pred p] {f g : Ξ± β†’ Ξ²} : range (Ξ» x, if p x then f x else g x) βŠ† range f βˆͺ range g := begin rw range_subset_iff, intro x, by_cases h : p x, simp [if_pos h, mem_union, mem_range_self], simp [if_neg h, mem_union, mem_range_self] end @[simp] lemma preimage_range (f : Ξ± β†’ Ξ²) : f ⁻¹' (range f) = univ := eq_univ_of_forall mem_range_self /-- The range of a function from a `unique` type contains just the function applied to its single value. -/ lemma range_unique [h : unique ΞΉ] : range f = {f $ default ΞΉ} := begin ext x, rw mem_range, split, { rintros ⟨i, hi⟩, rw h.uniq i at hi, exact hi β–Έ mem_singleton _ }, { exact Ξ» h, ⟨default ΞΉ, h.symm⟩ } end lemma range_diff_image_subset (f : Ξ± β†’ Ξ²) (s : set Ξ±) : range f \ f '' s βŠ† f '' sᢜ := Ξ» y ⟨⟨x, hβ‚βŸ©, hβ‚‚βŸ©, ⟨x, Ξ» h, hβ‚‚ ⟨x, h, hβ‚βŸ©, hβ‚βŸ© lemma range_diff_image {f : Ξ± β†’ Ξ²} (H : injective f) (s : set Ξ±) : range f \ f '' s = f '' sᢜ := subset.antisymm (range_diff_image_subset f s) $ Ξ» y ⟨x, hx, hy⟩, hy β–Έ ⟨mem_range_self _, Ξ» ⟨x', hx', eq⟩, hx $ H eq β–Έ hx'⟩ end range /-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/ def pairwise_on (s : set Ξ±) (r : Ξ± β†’ Ξ± β†’ Prop) := βˆ€ x ∈ s, βˆ€ y ∈ s, x β‰  y β†’ r x y lemma pairwise_on_of_forall (s : set Ξ±) (p : Ξ± β†’ Ξ± β†’ Prop) (h : βˆ€ (a b : Ξ±), p a b) : pairwise_on s p := Ξ» a _ b _ _, h a b lemma pairwise_on.imp_on {s : set Ξ±} {p q : Ξ± β†’ Ξ± β†’ Prop} (h : pairwise_on s p) (hpq : pairwise_on s (Ξ» ⦃a b : α⦄, p a b β†’ q a b)) : pairwise_on s q := Ξ» a ha b hb hab, hpq a ha b hb hab (h a ha b hb hab) lemma pairwise_on.imp {s : set Ξ±} {p q : Ξ± β†’ Ξ± β†’ Prop} (h : pairwise_on s p) (hpq : βˆ€ ⦃a b : α⦄, p a b β†’ q a b) : pairwise_on s q := h.imp_on (pairwise_on_of_forall s _ hpq) theorem pairwise_on.mono {s t : set Ξ±} {r} (h : t βŠ† s) (hp : pairwise_on s r) : pairwise_on t r := Ξ» x xt y yt, hp x (h xt) y (h yt) theorem pairwise_on.mono' {s : set Ξ±} {r r' : Ξ± β†’ Ξ± β†’ Prop} (H : r ≀ r') (hp : pairwise_on s r) : pairwise_on s r' := hp.imp H theorem pairwise_on_top (s : set Ξ±) : pairwise_on s ⊀ := pairwise_on_of_forall s _ (Ξ» a b, trivial) /-- If and only if `f` takes pairwise equal values on `s`, there is some value it takes everywhere on `s`. -/ lemma pairwise_on_eq_iff_exists_eq [nonempty Ξ²] (s : set Ξ±) (f : Ξ± β†’ Ξ²) : (pairwise_on s (Ξ» x y, f x = f y)) ↔ βˆƒ z, βˆ€ x ∈ s, f x = z := begin split, { intro h, rcases eq_empty_or_nonempty s with rfl | ⟨x, hx⟩, { exact ⟨classical.arbitrary Ξ², Ξ» x hx, false.elim hx⟩ }, { use f x, intros y hy, by_cases hyx : y = x, { rw hyx }, { exact h y hy x hx hyx } } }, { rintros ⟨z, hz⟩ x hx y hy hne, rw [hz x hx, hz y hy] } end protected lemma subsingleton.pairwise_on (h : s.subsingleton) (r : Ξ± β†’ Ξ± β†’ Prop) : pairwise_on s r := Ξ» x hx y hy hne, (hne (h hx hy)).elim @[simp] lemma pairwise_on_empty (r : Ξ± β†’ Ξ± β†’ Prop) : (βˆ… : set Ξ±).pairwise_on r := subsingleton_empty.pairwise_on r @[simp] lemma pairwise_on_singleton (a : Ξ±) (r : Ξ± β†’ Ξ± β†’ Prop) : pairwise_on {a} r := subsingleton_singleton.pairwise_on r lemma pairwise_on_insert_of_symmetric {Ξ±} {s : set Ξ±} {a : Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (hr : symmetric r) : (insert a s).pairwise_on r ↔ s.pairwise_on r ∧ βˆ€ b ∈ s, a β‰  b β†’ r a b := begin refine ⟨λ h, ⟨_, _⟩, Ξ» h, _⟩, { exact h.mono (s.subset_insert a) }, { intros b hb hn, exact h a (s.mem_insert _) b (set.mem_insert_of_mem _ hb) hn }, { intros b hb c hc hn, rw [mem_insert_iff] at hb hc, rcases hb with (rfl | hb); rcases hc with (rfl | hc), { exact absurd rfl hn }, { exact h.right _ hc hn }, { exact hr (h.right _ hb hn.symm) }, { exact h.left _ hb _ hc hn } } end end set open set namespace function variables {ΞΉ : Sort*} {Ξ± : Type*} {Ξ² : Type*} {f : Ξ± β†’ Ξ²} lemma surjective.preimage_injective (hf : surjective f) : injective (preimage f) := assume s t, (preimage_eq_preimage hf).1 lemma injective.preimage_image (hf : injective f) (s : set Ξ±) : f ⁻¹' (f '' s) = s := preimage_image_eq s hf lemma injective.preimage_surjective (hf : injective f) : surjective (preimage f) := by { intro s, use f '' s, rw hf.preimage_image } lemma injective.subsingleton_image_iff (hf : injective f) {s : set Ξ±} : (f '' s).subsingleton ↔ s.subsingleton := ⟨subsingleton_of_image hf s, Ξ» h, h.image f⟩ lemma surjective.image_preimage (hf : surjective f) (s : set Ξ²) : f '' (f ⁻¹' s) = s := image_preimage_eq s hf lemma surjective.image_surjective (hf : surjective f) : surjective (image f) := by { intro s, use f ⁻¹' s, rw hf.image_preimage } lemma injective.image_injective (hf : injective f) : injective (image f) := by { intros s t h, rw [←preimage_image_eq s hf, ←preimage_image_eq t hf, h] } lemma surjective.preimage_subset_preimage_iff {s t : set Ξ²} (hf : surjective f) : f ⁻¹' s βŠ† f ⁻¹' t ↔ s βŠ† t := by { apply preimage_subset_preimage_iff, rw [hf.range_eq], apply subset_univ } lemma surjective.range_comp {ΞΉ' : Sort*} {f : ΞΉ β†’ ΞΉ'} (hf : surjective f) (g : ΞΉ' β†’ Ξ±) : range (g ∘ f) = range g := ext $ Ξ» y, (@surjective.exists _ _ _ hf (Ξ» x, g x = y)).symm lemma injective.nonempty_apply_iff {f : set Ξ± β†’ set Ξ²} (hf : injective f) (h2 : f βˆ… = βˆ…) {s : set Ξ±} : (f s).nonempty ↔ s.nonempty := by rw [← ne_empty_iff_nonempty, ← h2, ← ne_empty_iff_nonempty, hf.ne_iff] lemma injective.mem_range_iff_exists_unique (hf : injective f) {b : Ξ²} : b ∈ range f ↔ βˆƒ! a, f a = b := ⟨λ ⟨a, h⟩, ⟨a, h, Ξ» a' ha, hf (ha.trans h.symm)⟩, exists_unique.exists⟩ lemma injective.exists_unique_of_mem_range (hf : injective f) {b : Ξ²} (hb : b ∈ range f) : βˆƒ! a, f a = b := hf.mem_range_iff_exists_unique.mp hb end function open function /-! ### Image and preimage on subtypes -/ namespace subtype variable {Ξ± : Type*} lemma coe_image {p : Ξ± β†’ Prop} {s : set (subtype p)} : coe '' s = {x | βˆƒh : p x, (⟨x, h⟩ : subtype p) ∈ s} := set.ext $ assume a, ⟨assume ⟨⟨a', ha'⟩, in_s, h_eq⟩, h_eq β–Έ ⟨ha', in_s⟩, assume ⟨ha, in_s⟩, ⟨⟨a, ha⟩, in_s, rfl⟩⟩ lemma range_coe {s : set Ξ±} : range (coe : s β†’ Ξ±) = s := by { rw ← set.image_univ, simp [-set.image_univ, coe_image] } /-- A variant of `range_coe`. Try to use `range_coe` if possible. This version is useful when defining a new type that is defined as the subtype of something. In that case, the coercion doesn't fire anymore. -/ lemma range_val {s : set Ξ±} : range (subtype.val : s β†’ Ξ±) = s := range_coe /-- We make this the simp lemma instead of `range_coe`. The reason is that if we write for `s : set Ξ±` the function `coe : s β†’ Ξ±`, then the inferred implicit arguments of `coe` are `coe Ξ± (Ξ» x, x ∈ s)`. -/ @[simp] lemma range_coe_subtype {p : Ξ± β†’ Prop} : range (coe : subtype p β†’ Ξ±) = {x | p x} := range_coe @[simp] lemma coe_preimage_self (s : set Ξ±) : (coe : s β†’ Ξ±) ⁻¹' s = univ := by rw [← preimage_range (coe : s β†’ Ξ±), range_coe] lemma range_val_subtype {p : Ξ± β†’ Prop} : range (subtype.val : subtype p β†’ Ξ±) = {x | p x} := range_coe theorem coe_image_subset (s : set Ξ±) (t : set s) : coe '' t βŠ† s := Ξ» x ⟨y, yt, yvaleq⟩, by rw ←yvaleq; exact y.property theorem coe_image_univ (s : set Ξ±) : (coe : s β†’ Ξ±) '' set.univ = s := image_univ.trans range_coe @[simp] theorem image_preimage_coe (s t : set Ξ±) : (coe : s β†’ Ξ±) '' (coe ⁻¹' t) = t ∩ s := image_preimage_eq_inter_range.trans $ congr_arg _ range_coe theorem image_preimage_val (s t : set Ξ±) : (subtype.val : s β†’ Ξ±) '' (subtype.val ⁻¹' t) = t ∩ s := image_preimage_coe s t theorem preimage_coe_eq_preimage_coe_iff {s t u : set Ξ±} : ((coe : s β†’ Ξ±) ⁻¹' t = coe ⁻¹' u) ↔ t ∩ s = u ∩ s := begin rw [←image_preimage_coe, ←image_preimage_coe], split, { intro h, rw h }, intro h, exact coe_injective.image_injective h end theorem preimage_val_eq_preimage_val_iff (s t u : set Ξ±) : ((subtype.val : s β†’ Ξ±) ⁻¹' t = subtype.val ⁻¹' u) ↔ (t ∩ s = u ∩ s) := preimage_coe_eq_preimage_coe_iff lemma exists_set_subtype {t : set Ξ±} (p : set Ξ± β†’ Prop) : (βˆƒ(s : set t), p (coe '' s)) ↔ βˆƒ(s : set Ξ±), s βŠ† t ∧ p s := begin split, { rintro ⟨s, hs⟩, refine ⟨coe '' s, _, hs⟩, convert image_subset_range _ _, rw [range_coe] }, rintro ⟨s, hs₁, hsβ‚‚βŸ©, refine ⟨coe ⁻¹' s, _⟩, rw [image_preimage_eq_of_subset], exact hsβ‚‚, rw [range_coe], exact hs₁ end lemma preimage_coe_nonempty {s t : set Ξ±} : ((coe : s β†’ Ξ±) ⁻¹' t).nonempty ↔ (s ∩ t).nonempty := by rw [inter_comm, ← image_preimage_coe, nonempty_image_iff] lemma preimage_coe_eq_empty {s t : set Ξ±} : (coe : s β†’ Ξ±) ⁻¹' t = βˆ… ↔ s ∩ t = βˆ… := by simp only [← not_nonempty_iff_eq_empty, preimage_coe_nonempty] @[simp] lemma preimage_coe_compl (s : set Ξ±) : (coe : s β†’ Ξ±) ⁻¹' sᢜ = βˆ… := preimage_coe_eq_empty.2 (inter_compl_self s) @[simp] lemma preimage_coe_compl' (s : set Ξ±) : (coe : sᢜ β†’ Ξ±) ⁻¹' s = βˆ… := preimage_coe_eq_empty.2 (compl_inter_self s) end subtype namespace set /-! ### Lemmas about cartesian product of sets -/ section prod variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} variables {s s₁ sβ‚‚ : set Ξ±} {t t₁ tβ‚‚ : set Ξ²} /-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def prod (s : set Ξ±) (t : set Ξ²) : set (Ξ± Γ— Ξ²) := {p | p.1 ∈ s ∧ p.2 ∈ t} lemma prod_eq (s : set Ξ±) (t : set Ξ²) : s.prod t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl theorem mem_prod_eq {p : Ξ± Γ— Ξ²} : p ∈ s.prod t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl @[simp] theorem mem_prod {p : Ξ± Γ— Ξ²} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[simp] theorem prod_mk_mem_set_prod_eq {a : Ξ±} {b : Ξ²} : (a, b) ∈ s.prod t = (a ∈ s ∧ b ∈ t) := rfl lemma mk_mem_prod {a : Ξ±} {b : Ξ²} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ s.prod t := ⟨a_in, b_in⟩ theorem prod_mono {s₁ sβ‚‚ : set Ξ±} {t₁ tβ‚‚ : set Ξ²} (hs : s₁ βŠ† sβ‚‚) (ht : t₁ βŠ† tβ‚‚) : s₁.prod t₁ βŠ† sβ‚‚.prod tβ‚‚ := assume x ⟨h₁, hβ‚‚βŸ©, ⟨hs h₁, ht hβ‚‚βŸ© lemma prod_subset_iff {P : set (Ξ± Γ— Ξ²)} : (s.prod t βŠ† P) ↔ βˆ€ (x ∈ s) (y ∈ t), (x, y) ∈ P := ⟨λ h _ xin _ yin, h (mk_mem_prod xin yin), Ξ» h ⟨_, _⟩ pin, h _ pin.1 _ pin.2⟩ lemma forall_prod_set {p : Ξ± Γ— Ξ² β†’ Prop} : (βˆ€ x ∈ s.prod t, p x) ↔ βˆ€ (x ∈ s) (y ∈ t), p (x, y) := prod_subset_iff lemma exists_prod_set {p : Ξ± Γ— Ξ² β†’ Prop} : (βˆƒ x ∈ s.prod t, p x) ↔ βˆƒ (x ∈ s) (y ∈ t), p (x, y) := by simp [and_assoc] @[simp] theorem prod_empty : s.prod βˆ… = (βˆ… : set (Ξ± Γ— Ξ²)) := by { ext, simp } @[simp] theorem empty_prod : set.prod βˆ… t = (βˆ… : set (Ξ± Γ— Ξ²)) := by { ext, simp } @[simp] theorem univ_prod_univ : (@univ Ξ±).prod (@univ Ξ²) = univ := by { ext ⟨x, y⟩, simp } lemma univ_prod {t : set Ξ²} : set.prod (univ : set Ξ±) t = prod.snd ⁻¹' t := by simp [prod_eq] lemma prod_univ {s : set Ξ±} : set.prod s (univ : set Ξ²) = prod.fst ⁻¹' s := by simp [prod_eq] @[simp] theorem singleton_prod {a : Ξ±} : set.prod {a} t = prod.mk a '' t := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } @[simp] theorem prod_singleton {b : Ξ²} : s.prod {b} = (Ξ» a, (a, b)) '' s := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } theorem singleton_prod_singleton {a : Ξ±} {b : Ξ²} : set.prod {a} {b} = ({(a, b)} : set (Ξ± Γ— Ξ²)) := by simp @[simp] theorem union_prod : (s₁ βˆͺ sβ‚‚).prod t = s₁.prod t βˆͺ sβ‚‚.prod t := by { ext ⟨x, y⟩, simp [or_and_distrib_right] } @[simp] theorem prod_union : s.prod (t₁ βˆͺ tβ‚‚) = s.prod t₁ βˆͺ s.prod tβ‚‚ := by { ext ⟨x, y⟩, simp [and_or_distrib_left] } theorem prod_inter_prod : s₁.prod t₁ ∩ sβ‚‚.prod tβ‚‚ = (s₁ ∩ sβ‚‚).prod (t₁ ∩ tβ‚‚) := by { ext ⟨x, y⟩, simp [and_assoc, and.left_comm] } theorem insert_prod {a : Ξ±} : (insert a s).prod t = (prod.mk a '' t) βˆͺ s.prod t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } theorem prod_insert {b : Ξ²} : s.prod (insert b t) = ((Ξ»a, (a, b)) '' s) βˆͺ s.prod t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } theorem prod_preimage_eq {f : Ξ³ β†’ Ξ±} {g : Ξ΄ β†’ Ξ²} : (f ⁻¹' s).prod (g ⁻¹' t) = (Ξ» p, (f p.1, g p.2)) ⁻¹' s.prod t := rfl lemma prod_preimage_left {f : Ξ³ β†’ Ξ±} : (f ⁻¹' s).prod t = (Ξ»p, (f p.1, p.2)) ⁻¹' (s.prod t) := rfl lemma prod_preimage_right {g : Ξ΄ β†’ Ξ²} : s.prod (g ⁻¹' t) = (Ξ»p, (p.1, g p.2)) ⁻¹' (s.prod t) := rfl lemma preimage_prod_map_prod (f : Ξ± β†’ Ξ²) (g : Ξ³ β†’ Ξ΄) (s : set Ξ²) (t : set Ξ΄) : prod.map f g ⁻¹' (s.prod t) = (f ⁻¹' s).prod (g ⁻¹' t) := rfl lemma mk_preimage_prod (f : Ξ³ β†’ Ξ±) (g : Ξ³ β†’ Ξ²) : (Ξ» x, (f x, g x)) ⁻¹' s.prod t = f ⁻¹' s ∩ g ⁻¹' t := rfl @[simp] lemma mk_preimage_prod_left {y : Ξ²} (h : y ∈ t) : (Ξ» x, (x, y)) ⁻¹' s.prod t = s := by { ext x, simp [h] } @[simp] lemma mk_preimage_prod_right {x : Ξ±} (h : x ∈ s) : prod.mk x ⁻¹' s.prod t = t := by { ext y, simp [h] } @[simp] lemma mk_preimage_prod_left_eq_empty {y : Ξ²} (hy : y βˆ‰ t) : (Ξ» x, (x, y)) ⁻¹' s.prod t = βˆ… := by { ext z, simp [hy] } @[simp] lemma mk_preimage_prod_right_eq_empty {x : Ξ±} (hx : x βˆ‰ s) : prod.mk x ⁻¹' s.prod t = βˆ… := by { ext z, simp [hx] } lemma mk_preimage_prod_left_eq_if {y : Ξ²} [decidable_pred (∈ t)] : (Ξ» x, (x, y)) ⁻¹' s.prod t = if y ∈ t then s else βˆ… := by { split_ifs; simp [h] } lemma mk_preimage_prod_right_eq_if {x : Ξ±} [decidable_pred (∈ s)] : prod.mk x ⁻¹' s.prod t = if x ∈ s then t else βˆ… := by { split_ifs; simp [h] } lemma mk_preimage_prod_left_fn_eq_if {y : Ξ²} [decidable_pred (∈ t)] (f : Ξ³ β†’ Ξ±) : (Ξ» x, (f x, y)) ⁻¹' s.prod t = if y ∈ t then f ⁻¹' s else βˆ… := by rw [← mk_preimage_prod_left_eq_if, prod_preimage_left, preimage_preimage] lemma mk_preimage_prod_right_fn_eq_if {x : Ξ±} [decidable_pred (∈ s)] (g : Ξ΄ β†’ Ξ²) : (Ξ» y, (x, g y)) ⁻¹' s.prod t = if x ∈ s then g ⁻¹' t else βˆ… := by rw [← mk_preimage_prod_right_eq_if, prod_preimage_right, preimage_preimage] theorem image_swap_eq_preimage_swap : image (@prod.swap Ξ± Ξ²) = preimage prod.swap := image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse theorem preimage_swap_prod {s : set Ξ±} {t : set Ξ²} : prod.swap ⁻¹' t.prod s = s.prod t := by { ext ⟨x, y⟩, simp [and_comm] } theorem image_swap_prod : prod.swap '' t.prod s = s.prod t := by rw [image_swap_eq_preimage_swap, preimage_swap_prod] theorem prod_image_image_eq {m₁ : Ξ± β†’ Ξ³} {mβ‚‚ : Ξ² β†’ Ξ΄} : (m₁ '' s).prod (mβ‚‚ '' t) = image (Ξ»p:Ξ±Γ—Ξ², (m₁ p.1, mβ‚‚ p.2)) (s.prod t) := ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm, and.assoc, and.comm] theorem prod_range_range_eq {Ξ± Ξ² Ξ³ Ξ΄} {m₁ : Ξ± β†’ Ξ³} {mβ‚‚ : Ξ² β†’ Ξ΄} : (range m₁).prod (range mβ‚‚) = range (Ξ»p:Ξ±Γ—Ξ², (m₁ p.1, mβ‚‚ p.2)) := ext $ by simp [range] @[simp] theorem range_prod_map {Ξ± Ξ² Ξ³ Ξ΄} {m₁ : Ξ± β†’ Ξ³} {mβ‚‚ : Ξ² β†’ Ξ΄} : range (prod.map m₁ mβ‚‚) = (range m₁).prod (range mβ‚‚) := prod_range_range_eq.symm theorem prod_range_univ_eq {Ξ± Ξ² Ξ³} {m₁ : Ξ± β†’ Ξ³} : (range m₁).prod (univ : set Ξ²) = range (Ξ»p:Ξ±Γ—Ξ², (m₁ p.1, p.2)) := ext $ by simp [range] theorem prod_univ_range_eq {Ξ± Ξ² Ξ΄} {mβ‚‚ : Ξ² β†’ Ξ΄} : (univ : set Ξ±).prod (range mβ‚‚) = range (Ξ»p:Ξ±Γ—Ξ², (p.1, mβ‚‚ p.2)) := ext $ by simp [range] theorem nonempty.prod : s.nonempty β†’ t.nonempty β†’ (s.prod t).nonempty | ⟨x, hx⟩ ⟨y, hy⟩ := ⟨(x, y), ⟨hx, hy⟩⟩ theorem nonempty.fst : (s.prod t).nonempty β†’ s.nonempty | ⟨p, hp⟩ := ⟨p.1, hp.1⟩ theorem nonempty.snd : (s.prod t).nonempty β†’ t.nonempty | ⟨p, hp⟩ := ⟨p.2, hp.2⟩ theorem prod_nonempty_iff : (s.prod t).nonempty ↔ s.nonempty ∧ t.nonempty := ⟨λ h, ⟨h.fst, h.snd⟩, Ξ» h, nonempty.prod h.1 h.2⟩ theorem prod_eq_empty_iff : s.prod t = βˆ… ↔ (s = βˆ… ∨ t = βˆ…) := by simp only [not_nonempty_iff_eq_empty.symm, prod_nonempty_iff, not_and_distrib] lemma prod_sub_preimage_iff {W : set Ξ³} {f : Ξ± Γ— Ξ² β†’ Ξ³} : s.prod t βŠ† f ⁻¹' W ↔ βˆ€ a b, a ∈ s β†’ b ∈ t β†’ f (a, b) ∈ W := by simp [subset_def] lemma fst_image_prod_subset (s : set Ξ±) (t : set Ξ²) : prod.fst '' (s.prod t) βŠ† s := Ξ» _ h, let ⟨_, ⟨hβ‚‚, _⟩, hβ‚βŸ© := (set.mem_image _ _ _).1 h in h₁ β–Έ hβ‚‚ lemma prod_subset_preimage_fst (s : set Ξ±) (t : set Ξ²) : s.prod t βŠ† prod.fst ⁻¹' s := image_subset_iff.1 (fst_image_prod_subset s t) lemma fst_image_prod (s : set Ξ²) {t : set Ξ±} (ht : t.nonempty) : prod.fst '' (s.prod t) = s := set.subset.antisymm (fst_image_prod_subset _ _) $ Ξ» y y_in, let ⟨x, x_in⟩ := ht in ⟨(y, x), ⟨y_in, x_in⟩, rfl⟩ lemma snd_image_prod_subset (s : set Ξ±) (t : set Ξ²) : prod.snd '' (s.prod t) βŠ† t := Ξ» _ h, let ⟨_, ⟨_, hβ‚‚βŸ©, hβ‚βŸ© := (set.mem_image _ _ _).1 h in h₁ β–Έ hβ‚‚ lemma prod_subset_preimage_snd (s : set Ξ±) (t : set Ξ²) : s.prod t βŠ† prod.snd ⁻¹' t := image_subset_iff.1 (snd_image_prod_subset s t) lemma snd_image_prod {s : set Ξ±} (hs : s.nonempty) (t : set Ξ²) : prod.snd '' (s.prod t) = t := set.subset.antisymm (snd_image_prod_subset _ _) $ Ξ» y y_in, let ⟨x, x_in⟩ := hs in ⟨(x, y), ⟨x_in, y_in⟩, rfl⟩ lemma prod_diff_prod : s.prod t \ s₁.prod t₁ = s.prod (t \ t₁) βˆͺ (s \ s₁).prod t := by { ext x, by_cases h₁ : x.1 ∈ s₁; by_cases hβ‚‚ : x.2 ∈ t₁; simp * } /-- A product set is included in a product set if and only factors are included, or a factor of the first set is empty. -/ lemma prod_subset_prod_iff : (s.prod t βŠ† s₁.prod t₁) ↔ (s βŠ† s₁ ∧ t βŠ† t₁) ∨ (s = βˆ…) ∨ (t = βˆ…) := begin classical, cases (s.prod t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.1 h] }, { have st : s.nonempty ∧ t.nonempty, by rwa [prod_nonempty_iff] at h, split, { assume H : s.prod t βŠ† s₁.prod t₁, have h' : s₁.nonempty ∧ t₁.nonempty := prod_nonempty_iff.1 (h.mono H), refine or.inl ⟨_, _⟩, show s βŠ† s₁, { have := image_subset (prod.fst : Ξ± Γ— Ξ² β†’ Ξ±) H, rwa [fst_image_prod _ st.2, fst_image_prod _ h'.2] at this }, show t βŠ† t₁, { have := image_subset (prod.snd : Ξ± Γ— Ξ² β†’ Ξ²) H, rwa [snd_image_prod st.1, snd_image_prod h'.1] at this } }, { assume H, simp only [st.1.ne_empty, st.2.ne_empty, or_false] at H, exact prod_mono H.1 H.2 } } end end prod /-! ### Lemmas about set-indexed products of sets -/ section pi variables {ΞΉ : Type*} {Ξ± : ΞΉ β†’ Type*} {s s₁ : set ΞΉ} {t t₁ tβ‚‚ : Ξ  i, set (Ξ± i)} /-- Given an index set `ΞΉ` and a family of sets `t : Ξ  i, set (Ξ± i)`, `pi s t` is the set of dependent functions `f : Ξ a, Ο€ a` such that `f a` belongs to `t a` whenever `a ∈ s`. -/ def pi (s : set ΞΉ) (t : Ξ  i, set (Ξ± i)) : set (Ξ  i, Ξ± i) := { f | βˆ€i ∈ s, f i ∈ t i } @[simp] lemma mem_pi {f : Ξ  i, Ξ± i} : f ∈ s.pi t ↔ βˆ€ i ∈ s, f i ∈ t i := by refl @[simp] lemma mem_univ_pi {f : Ξ  i, Ξ± i} : f ∈ pi univ t ↔ βˆ€ i, f i ∈ t i := by simp @[simp] lemma empty_pi (s : Ξ  i, set (Ξ± i)) : pi βˆ… s = univ := by { ext, simp [pi] } @[simp] lemma pi_univ (s : set ΞΉ) : pi s (Ξ» i, (univ : set (Ξ± i))) = univ := eq_univ_of_forall $ Ξ» f i hi, mem_univ _ lemma pi_mono (h : βˆ€ i ∈ s, t₁ i βŠ† tβ‚‚ i) : pi s t₁ βŠ† pi s tβ‚‚ := Ξ» x hx i hi, (h i hi $ hx i hi) lemma pi_inter_distrib : s.pi (Ξ» i, t i ∩ t₁ i) = s.pi t ∩ s.pi t₁ := ext $ Ξ» x, by simp only [forall_and_distrib, mem_pi, mem_inter_eq] lemma pi_congr (h : s = s₁) (h' : βˆ€ i ∈ s, t i = t₁ i) : pi s t = pi s₁ t₁ := h β–Έ (ext $ Ξ» x, forall_congr $ Ξ» i, forall_congr $ Ξ» hi, h' i hi β–Έ iff.rfl) lemma pi_eq_empty {i : ΞΉ} (hs : i ∈ s) (ht : t i = βˆ…) : s.pi t = βˆ… := by { ext f, simp only [mem_empty_eq, not_forall, iff_false, mem_pi, not_imp], exact ⟨i, hs, by simp [ht]⟩ } lemma univ_pi_eq_empty {i : ΞΉ} (ht : t i = βˆ…) : pi univ t = βˆ… := pi_eq_empty (mem_univ i) ht lemma pi_nonempty_iff : (s.pi t).nonempty ↔ βˆ€ i, βˆƒ x, i ∈ s β†’ x ∈ t i := by simp [classical.skolem, set.nonempty] lemma univ_pi_nonempty_iff : (pi univ t).nonempty ↔ βˆ€ i, (t i).nonempty := by simp [classical.skolem, set.nonempty] lemma pi_eq_empty_iff : s.pi t = βˆ… ↔ βˆƒ i, (Ξ± i β†’ false) ∨ (i ∈ s ∧ t i = βˆ…) := begin rw [← not_nonempty_iff_eq_empty, pi_nonempty_iff], push_neg, apply exists_congr, intro i, split, { intro h, by_cases hΞ± : nonempty (Ξ± i), { cases hΞ± with x, refine or.inr ⟨(h x).1, by simp [eq_empty_iff_forall_not_mem, h]⟩ }, { exact or.inl (Ξ» x, hΞ± ⟨x⟩) }}, { rintro (h|h) x, exfalso, exact h x, simp [h] } end lemma univ_pi_eq_empty_iff : pi univ t = βˆ… ↔ βˆƒ i, t i = βˆ… := by simp [← not_nonempty_iff_eq_empty, univ_pi_nonempty_iff] @[simp] lemma range_dcomp {Ξ² : ΞΉ β†’ Type*} (f : Ξ  i, Ξ± i β†’ Ξ² i) : range (Ξ» (g : Ξ  i, Ξ± i), (Ξ» i, f i (g i))) = pi univ (Ξ» i, range (f i)) := begin apply subset.antisymm, { rintro _ ⟨x, rfl⟩ i -, exact ⟨x i, rfl⟩ }, { intros x hx, choose y hy using hx, exact ⟨λ i, y i trivial, funext $ Ξ» i, hy i trivial⟩ } end @[simp] lemma insert_pi (i : ΞΉ) (s : set ΞΉ) (t : Ξ  i, set (Ξ± i)) : pi (insert i s) t = (eval i ⁻¹' t i) ∩ pi s t := by { ext, simp [pi, or_imp_distrib, forall_and_distrib] } @[simp] lemma singleton_pi (i : ΞΉ) (t : Ξ  i, set (Ξ± i)) : pi {i} t = (eval i ⁻¹' t i) := by { ext, simp [pi] } lemma singleton_pi' (i : ΞΉ) (t : Ξ  i, set (Ξ± i)) : pi {i} t = {x | x i ∈ t i} := singleton_pi i t lemma pi_if {p : ΞΉ β†’ Prop} [h : decidable_pred p] (s : set ΞΉ) (t₁ tβ‚‚ : Ξ  i, set (Ξ± i)) : pi s (Ξ» i, if p i then t₁ i else tβ‚‚ i) = pi {i ∈ s | p i} t₁ ∩ pi {i ∈ s | Β¬ p i} tβ‚‚ := begin ext f, split, { assume h, split; { rintros i ⟨his, hpi⟩, simpa [*] using h i } }, { rintros ⟨ht₁, htβ‚‚βŸ© i his, by_cases p i; simp * at * } end lemma union_pi : (s βˆͺ s₁).pi t = s.pi t ∩ s₁.pi t := by simp [pi, or_imp_distrib, forall_and_distrib, set_of_and] @[simp] lemma pi_inter_compl (s : set ΞΉ) : pi s t ∩ pi sᢜ t = pi univ t := by rw [← union_pi, union_compl_self] lemma pi_update_of_not_mem [decidable_eq ΞΉ] {Ξ² : Ξ  i, Type*} {i : ΞΉ} (hi : i βˆ‰ s) (f : Ξ  j, Ξ± j) (a : Ξ± i) (t : Ξ  j, Ξ± j β†’ set (Ξ² j)) : s.pi (Ξ» j, t j (update f i a j)) = s.pi (Ξ» j, t j (f j)) := pi_congr rfl $ Ξ» j hj, by { rw update_noteq, exact Ξ» h, hi (h β–Έ hj) } lemma pi_update_of_mem [decidable_eq ΞΉ] {Ξ² : Ξ  i, Type*} {i : ΞΉ} (hi : i ∈ s) (f : Ξ  j, Ξ± j) (a : Ξ± i) (t : Ξ  j, Ξ± j β†’ set (Ξ² j)) : s.pi (Ξ» j, t j (update f i a j)) = {x | x i ∈ t i a} ∩ (s \ {i}).pi (Ξ» j, t j (f j)) := calc s.pi (Ξ» j, t j (update f i a j)) = ({i} βˆͺ s \ {i}).pi (Ξ» j, t j (update f i a j)) : by rw [union_diff_self, union_eq_self_of_subset_left (singleton_subset_iff.2 hi)] ... = {x | x i ∈ t i a} ∩ (s \ {i}).pi (Ξ» j, t j (f j)) : by { rw [union_pi, singleton_pi', update_same, pi_update_of_not_mem], simp } lemma univ_pi_update [decidable_eq ΞΉ] {Ξ² : Ξ  i, Type*} (i : ΞΉ) (f : Ξ  j, Ξ± j) (a : Ξ± i) (t : Ξ  j, Ξ± j β†’ set (Ξ² j)) : pi univ (Ξ» j, t j (update f i a j)) = {x | x i ∈ t i a} ∩ pi {i}ᢜ (Ξ» j, t j (f j)) := by rw [compl_eq_univ_diff, ← pi_update_of_mem (mem_univ _)] lemma univ_pi_update_univ [decidable_eq ΞΉ] (i : ΞΉ) (s : set (Ξ± i)) : pi univ (update (Ξ» j : ΞΉ, (univ : set (Ξ± j))) i s) = eval i ⁻¹' s := by rw [univ_pi_update i (Ξ» j, (univ : set (Ξ± j))) s (Ξ» j t, t), pi_univ, inter_univ, preimage] open_locale classical lemma eval_image_pi {i : ΞΉ} (hs : i ∈ s) (ht : (s.pi t).nonempty) : eval i '' s.pi t = t i := begin ext x, rcases ht with ⟨f, hf⟩, split, { rintro ⟨g, hg, rfl⟩, exact hg i hs }, { intro hg, refine ⟨update f i x, _, by simp⟩, intros j hj, by_cases hji : j = i, { subst hji, simp [hg] }, { rw [mem_pi] at hf, simp [hji, hf, hj] }}, end @[simp] lemma eval_image_univ_pi {i : ΞΉ} (ht : (pi univ t).nonempty) : (Ξ» f : Ξ  i, Ξ± i, f i) '' pi univ t = t i := eval_image_pi (mem_univ i) ht lemma eval_preimage {ΞΉ} {Ξ± : ΞΉ β†’ Type*} {i : ΞΉ} {s : set (Ξ± i)} : eval i ⁻¹' s = pi univ (update (Ξ» i, univ) i s) := by { ext x, simp [@forall_update_iff _ (Ξ» i, set (Ξ± i)) _ _ _ _ (Ξ» i' y, x i' ∈ y)] } lemma eval_preimage' {ΞΉ} {Ξ± : ΞΉ β†’ Type*} {i : ΞΉ} {s : set (Ξ± i)} : eval i ⁻¹' s = pi {i} (update (Ξ» i, univ) i s) := by { ext, simp } lemma update_preimage_pi {i : ΞΉ} {f : Ξ  i, Ξ± i} (hi : i ∈ s) (hf : βˆ€ j ∈ s, j β‰  i β†’ f j ∈ t j) : (update f i) ⁻¹' s.pi t = t i := begin ext x, split, { intro h, convert h i hi, simp }, { intros hx j hj, by_cases h : j = i, { cases h, simpa }, { rw [update_noteq h], exact hf j hj h }} end lemma update_preimage_univ_pi {i : ΞΉ} {f : Ξ  i, Ξ± i} (hf : βˆ€ j β‰  i, f j ∈ t j) : (update f i) ⁻¹' pi univ t = t i := update_preimage_pi (mem_univ i) (Ξ» j _, hf j) lemma subset_pi_eval_image (s : set ΞΉ) (u : set (Ξ  i, Ξ± i)) : u βŠ† pi s (Ξ» i, eval i '' u) := Ξ» f hf i hi, ⟨f, hf, rfl⟩ lemma univ_pi_ite (s : set ΞΉ) (t : Ξ  i, set (Ξ± i)) : pi univ (Ξ» i, if i ∈ s then t i else univ) = s.pi t := by { ext, simp_rw [mem_univ_pi], apply forall_congr, intro i, split_ifs; simp [h] } end pi /-! ### Lemmas about `inclusion`, the injection of subtypes induced by `βŠ†` -/ section inclusion variable {Ξ± : Type*} /-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s βŠ† t` -/ def inclusion {s t : set Ξ±} (h : s βŠ† t) : s β†’ t := Ξ» x : s, (⟨x, h x.2⟩ : t) @[simp] lemma inclusion_self {s : set Ξ±} (x : s) : inclusion (set.subset.refl _) x = x := by { cases x, refl } @[simp] lemma inclusion_right {s t : set Ξ±} (h : s βŠ† t) (x : t) (m : (x : Ξ±) ∈ s) : inclusion h ⟨x, m⟩ = x := by { cases x, refl } @[simp] lemma inclusion_inclusion {s t u : set Ξ±} (hst : s βŠ† t) (htu : t βŠ† u) (x : s) : inclusion htu (inclusion hst x) = inclusion (set.subset.trans hst htu) x := by { cases x, refl } @[simp] lemma coe_inclusion {s t : set Ξ±} (h : s βŠ† t) (x : s) : (inclusion h x : Ξ±) = (x : Ξ±) := rfl lemma inclusion_injective {s t : set Ξ±} (h : s βŠ† t) : function.injective (inclusion h) | ⟨_, _⟩ ⟨_, _⟩ := subtype.ext_iff_val.2 ∘ subtype.ext_iff_val.1 @[simp] lemma range_inclusion {s t : set Ξ±} (h : s βŠ† t) : range (inclusion h) = {x : t | (x:Ξ±) ∈ s} := by { ext ⟨x, hx⟩, simp [inclusion] } lemma eq_of_inclusion_surjective {s t : set Ξ±} {h : s βŠ† t} (h_surj : function.surjective (inclusion h)) : s = t := begin rw [← range_iff_surjective, range_inclusion, eq_univ_iff_forall] at h_surj, exact set.subset.antisymm h (Ξ» x hx, h_surj ⟨x, hx⟩) end end inclusion /-! ### Injectivity and surjectivity lemmas for image and preimage -/ section image_preimage variables {Ξ± : Type u} {Ξ² : Type v} {f : Ξ± β†’ Ξ²} @[simp] lemma preimage_injective : injective (preimage f) ↔ surjective f := begin refine ⟨λ h y, _, surjective.preimage_injective⟩, obtain ⟨x, hx⟩ : (f ⁻¹' {y}).nonempty, { rw [h.nonempty_apply_iff preimage_empty], apply singleton_nonempty }, exact ⟨x, hx⟩ end @[simp] lemma preimage_surjective : surjective (preimage f) ↔ injective f := begin refine ⟨λ h x x' hx, _, injective.preimage_surjective⟩, cases h {x} with s hs, have := mem_singleton x, rwa [← hs, mem_preimage, hx, ← mem_preimage, hs, mem_singleton_iff, eq_comm] at this end @[simp] lemma image_surjective : surjective (image f) ↔ surjective f := begin refine ⟨λ h y, _, surjective.image_surjective⟩, cases h {y} with s hs, have := mem_singleton y, rw [← hs] at this, rcases this with ⟨x, h1x, h2x⟩, exact ⟨x, h2x⟩ end @[simp] lemma image_injective : injective (image f) ↔ injective f := begin refine ⟨λ h x x' hx, _, injective.image_injective⟩, rw [← singleton_eq_singleton_iff], apply h, rw [image_singleton, image_singleton, hx] end lemma preimage_eq_iff_eq_image {f : Ξ± β†’ Ξ²} (hf : bijective f) {s t} : f ⁻¹' s = t ↔ s = f '' t := by rw [← image_eq_image hf.1, hf.2.image_preimage] lemma eq_preimage_iff_image_eq {f : Ξ± β†’ Ξ²} (hf : bijective f) {s t} : s = f ⁻¹' t ↔ f '' s = t := by rw [← image_eq_image hf.1, hf.2.image_preimage] end image_preimage /-! ### Lemmas about images of binary and ternary functions -/ section n_ary_image variables {Ξ± Ξ² Ξ³ Ξ΄ Ξ΅ : Type*} {f f' : Ξ± β†’ Ξ² β†’ Ξ³} {g g' : Ξ± β†’ Ξ² β†’ Ξ³ β†’ Ξ΄} variables {s s' : set Ξ±} {t t' : set Ξ²} {u u' : set Ξ³} {a a' : Ξ±} {b b' : Ξ²} {c c' : Ξ³} {d d' : Ξ΄} /-- The image of a binary function `f : Ξ± β†’ Ξ² β†’ Ξ³` as a function `set Ξ± β†’ set Ξ² β†’ set Ξ³`. Mathematically this should be thought of as the image of the corresponding function `Ξ± Γ— Ξ² β†’ Ξ³`. -/ def image2 (f : Ξ± β†’ Ξ² β†’ Ξ³) (s : set Ξ±) (t : set Ξ²) : set Ξ³ := {c | βˆƒ a b, a ∈ s ∧ b ∈ t ∧ f a b = c } lemma mem_image2_eq : c ∈ image2 f s t = βˆƒ a b, a ∈ s ∧ b ∈ t ∧ f a b = c := rfl @[simp] lemma mem_image2 : c ∈ image2 f s t ↔ βˆƒ a b, a ∈ s ∧ b ∈ t ∧ f a b = c := iff.rfl lemma mem_image2_of_mem (h1 : a ∈ s) (h2 : b ∈ t) : f a b ∈ image2 f s t := ⟨a, b, h1, h2, rfl⟩ lemma mem_image2_iff (hf : injective2 f) : f a b ∈ image2 f s t ↔ a ∈ s ∧ b ∈ t := ⟨ by { rintro ⟨a', b', ha', hb', h⟩, rcases hf h with ⟨rfl, rfl⟩, exact ⟨ha', hb'⟩ }, Ξ» ⟨ha, hb⟩, mem_image2_of_mem ha hb⟩ /-- image2 is monotone with respect to `βŠ†`. -/ lemma image2_subset (hs : s βŠ† s') (ht : t βŠ† t') : image2 f s t βŠ† image2 f s' t' := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_image2_of_mem (hs ha) (ht hb) } lemma forall_image2_iff {p : Ξ³ β†’ Prop} : (βˆ€ z ∈ image2 f s t, p z) ↔ βˆ€ (x ∈ s) (y ∈ t), p (f x y) := ⟨λ h x hx y hy, h _ ⟨x, y, hx, hy, rfl⟩, Ξ» h z ⟨x, y, hx, hy, hz⟩, hz β–Έ h x hx y hy⟩ @[simp] lemma image2_subset_iff {u : set Ξ³} : image2 f s t βŠ† u ↔ βˆ€ (x ∈ s) (y ∈ t), f x y ∈ u := forall_image2_iff lemma image2_union_left : image2 f (s βˆͺ s') t = image2 f s t βˆͺ image2 f s' t := begin ext c, split, { rintros ⟨a, b, h1a|h2a, hb, rfl⟩;[left, right]; exact ⟨_, _, β€Ή_β€Ί, β€Ή_β€Ί, rfl⟩ }, { rintro (⟨_, _, _, _, rfl⟩|⟨_, _, _, _, rfl⟩); refine ⟨_, _, _, β€Ή_β€Ί, rfl⟩; simp [mem_union, *] } end lemma image2_union_right : image2 f s (t βˆͺ t') = image2 f s t βˆͺ image2 f s t' := begin ext c, split, { rintros ⟨a, b, ha, h1b|h2b, rfl⟩;[left, right]; exact ⟨_, _, β€Ή_β€Ί, β€Ή_β€Ί, rfl⟩ }, { rintro (⟨_, _, _, _, rfl⟩|⟨_, _, _, _, rfl⟩); refine ⟨_, _, β€Ή_β€Ί, _, rfl⟩; simp [mem_union, *] } end @[simp] lemma image2_empty_left : image2 f βˆ… t = βˆ… := ext $ by simp @[simp] lemma image2_empty_right : image2 f s βˆ… = βˆ… := ext $ by simp lemma image2_inter_subset_left : image2 f (s ∩ s') t βŠ† image2 f s t ∩ image2 f s' t := by { rintro _ ⟨a, b, ⟨h1a, h2a⟩, hb, rfl⟩, split; exact ⟨_, _, β€Ή_β€Ί, β€Ή_β€Ί, rfl⟩ } lemma image2_inter_subset_right : image2 f s (t ∩ t') βŠ† image2 f s t ∩ image2 f s t' := by { rintro _ ⟨a, b, ha, ⟨h1b, h2b⟩, rfl⟩, split; exact ⟨_, _, β€Ή_β€Ί, β€Ή_β€Ί, rfl⟩ } @[simp] lemma image2_singleton_left : image2 f {a} t = f a '' t := ext $ Ξ» x, by simp @[simp] lemma image2_singleton_right : image2 f s {b} = (Ξ» a, f a b) '' s := ext $ Ξ» x, by simp lemma image2_singleton : image2 f {a} {b} = {f a b} := by simp @[congr] lemma image2_congr (h : βˆ€ (a ∈ s) (b ∈ t), f a b = f' a b) : image2 f s t = image2 f' s t := by { ext, split; rintro ⟨a, b, ha, hb, rfl⟩; refine ⟨a, b, ha, hb, by rw h a ha b hb⟩ } /-- A common special case of `image2_congr` -/ lemma image2_congr' (h : βˆ€ a b, f a b = f' a b) : image2 f s t = image2 f' s t := image2_congr (Ξ» a _ b _, h a b) /-- The image of a ternary function `f : Ξ± β†’ Ξ² β†’ Ξ³ β†’ Ξ΄` as a function `set Ξ± β†’ set Ξ² β†’ set Ξ³ β†’ set Ξ΄`. Mathematically this should be thought of as the image of the corresponding function `Ξ± Γ— Ξ² Γ— Ξ³ β†’ Ξ΄`. -/ def image3 (g : Ξ± β†’ Ξ² β†’ Ξ³ β†’ Ξ΄) (s : set Ξ±) (t : set Ξ²) (u : set Ξ³) : set Ξ΄ := {d | βˆƒ a b c, a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d } @[simp] lemma mem_image3 : d ∈ image3 g s t u ↔ βˆƒ a b c, a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d := iff.rfl @[congr] lemma image3_congr (h : βˆ€ (a ∈ s) (b ∈ t) (c ∈ u), g a b c = g' a b c) : image3 g s t u = image3 g' s t u := by { ext x, split; rintro ⟨a, b, c, ha, hb, hc, rfl⟩; exact ⟨a, b, c, ha, hb, hc, by rw h a ha b hb c hc⟩ } /-- A common special case of `image3_congr` -/ lemma image3_congr' (h : βˆ€ a b c, g a b c = g' a b c) : image3 g s t u = image3 g' s t u := image3_congr (Ξ» a _ b _ c _, h a b c) lemma image2_image2_left (f : Ξ΄ β†’ Ξ³ β†’ Ξ΅) (g : Ξ± β†’ Ξ² β†’ Ξ΄) : image2 f (image2 g s t) u = image3 (Ξ» a b c, f (g a b) c) s t u := begin ext, split, { rintro ⟨_, c, ⟨a, b, ha, hb, rfl⟩, hc, rfl⟩, refine ⟨a, b, c, ha, hb, hc, rfl⟩ }, { rintro ⟨a, b, c, ha, hb, hc, rfl⟩, refine ⟨_, c, ⟨a, b, ha, hb, rfl⟩, hc, rfl⟩ } end lemma image2_image2_right (f : Ξ± β†’ Ξ΄ β†’ Ξ΅) (g : Ξ² β†’ Ξ³ β†’ Ξ΄) : image2 f s (image2 g t u) = image3 (Ξ» a b c, f a (g b c)) s t u := begin ext, split, { rintro ⟨a, _, ha, ⟨b, c, hb, hc, rfl⟩, rfl⟩, refine ⟨a, b, c, ha, hb, hc, rfl⟩ }, { rintro ⟨a, b, c, ha, hb, hc, rfl⟩, refine ⟨a, _, ha, ⟨b, c, hb, hc, rfl⟩, rfl⟩ } end lemma image2_assoc {Ξ΅'} {f : Ξ΄ β†’ Ξ³ β†’ Ξ΅} {g : Ξ± β†’ Ξ² β†’ Ξ΄} {f' : Ξ± β†’ Ξ΅' β†’ Ξ΅} {g' : Ξ² β†’ Ξ³ β†’ Ξ΅'} (h_assoc : βˆ€ a b c, f (g a b) c = f' a (g' b c)) : image2 f (image2 g s t) u = image2 f' s (image2 g' t u) := by simp only [image2_image2_left, image2_image2_right, h_assoc] lemma image_image2 (f : Ξ± β†’ Ξ² β†’ Ξ³) (g : Ξ³ β†’ Ξ΄) : g '' image2 f s t = image2 (Ξ» a b, g (f a b)) s t := begin ext, split, { rintro ⟨_, ⟨a, b, ha, hb, rfl⟩, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨_, ⟨a, b, ha, hb, rfl⟩, rfl⟩ } end lemma image2_image_left (f : Ξ³ β†’ Ξ² β†’ Ξ΄) (g : Ξ± β†’ Ξ³) : image2 f (g '' s) t = image2 (Ξ» a b, f (g a) b) s t := begin ext, split, { rintro ⟨_, b, ⟨a, ha, rfl⟩, hb, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨_, b, ⟨a, ha, rfl⟩, hb, rfl⟩ } end lemma image2_image_right (f : Ξ± β†’ Ξ³ β†’ Ξ΄) (g : Ξ² β†’ Ξ³) : image2 f s (g '' t) = image2 (Ξ» a b, f a (g b)) s t := begin ext, split, { rintro ⟨a, _, ha, ⟨b, hb, rfl⟩, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨a, _, ha, ⟨b, hb, rfl⟩, rfl⟩ } end lemma image2_swap (f : Ξ± β†’ Ξ² β†’ Ξ³) (s : set Ξ±) (t : set Ξ²) : image2 f s t = image2 (Ξ» a b, f b a) t s := by { ext, split; rintro ⟨a, b, ha, hb, rfl⟩; refine ⟨b, a, hb, ha, rfl⟩ } @[simp] lemma image2_left (h : t.nonempty) : image2 (Ξ» x y, x) s t = s := by simp [nonempty_def.mp h, ext_iff] @[simp] lemma image2_right (h : s.nonempty) : image2 (Ξ» x y, y) s t = t := by simp [nonempty_def.mp h, ext_iff] @[simp] lemma image_prod (f : Ξ± β†’ Ξ² β†’ Ξ³) : (Ξ» x : Ξ± Γ— Ξ², f x.1 x.2) '' s.prod t = image2 f s t := set.ext $ Ξ» a, ⟨ by { rintros ⟨_, _, rfl⟩, exact ⟨_, _, (mem_prod.mp β€Ή_β€Ί).1, (mem_prod.mp β€Ή_β€Ί).2, rfl⟩ }, by { rintros ⟨_, _, _, _, rfl⟩, exact ⟨(_, _), mem_prod.mpr βŸ¨β€Ή_β€Ί, β€Ή_β€ΊβŸ©, rfl⟩ }⟩ lemma nonempty.image2 (hs : s.nonempty) (ht : t.nonempty) : (image2 f s t).nonempty := by { cases hs with a ha, cases ht with b hb, exact ⟨f a b, ⟨a, b, ha, hb, rfl⟩⟩ } end n_ary_image end set namespace subsingleton variables {Ξ± : Type*} [subsingleton Ξ±] lemma eq_univ_of_nonempty {s : set Ξ±} : s.nonempty β†’ s = univ := Ξ» ⟨x, hx⟩, eq_univ_of_forall $ Ξ» y, subsingleton.elim x y β–Έ hx @[elab_as_eliminator] lemma set_cases {p : set Ξ± β†’ Prop} (h0 : p βˆ…) (h1 : p univ) (s) : p s := s.eq_empty_or_nonempty.elim (Ξ» h, h.symm β–Έ h0) $ Ξ» h, (eq_univ_of_nonempty h).symm β–Έ h1 end subsingleton
02cc8c20b0f2df5fcdf9e27a5434be32dac7434c
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/topology/metric_space/closeds.lean
873c9b828995d2bff4f6be64f97ac0d60ff6c685
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,321
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 topology.metric_space.hausdorff_distance import topology.compacts import analysis.specific_limits /-! # Closed subsets This file defines the metric and emetric space structure on the types of closed subsets and nonempty compact subsets of a metric or emetric space. The Hausdorff distance induces an emetric space structure on the type of closed subsets of an emetric space, called `closeds`. Its completeness, resp. compactness, resp. second-countability, follow from the corresponding properties of the original space. In a metric space, the type of nonempty compact subsets (called `nonempty_compacts`) also inherits a metric space structure from the Hausdorff distance, as the Hausdorff edistance is always finite in this context. -/ noncomputable theory open_locale classical topological_space ennreal universe u open classical set function topological_space filter namespace emetric section variables {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±} /-- In emetric spaces, the Hausdorff edistance defines an emetric space structure on the type of closed subsets -/ instance closeds.emetric_space : emetric_space (closeds Ξ±) := { edist := Ξ»s t, Hausdorff_edist s.val t.val, edist_self := Ξ»s, Hausdorff_edist_self, edist_comm := Ξ»s t, Hausdorff_edist_comm, edist_triangle := Ξ»s t u, Hausdorff_edist_triangle, eq_of_edist_eq_zero := Ξ»s t h, subtype.eq ((Hausdorff_edist_zero_iff_eq_of_closed s.property t.property).1 h) } /-- The edistance to a closed set depends continuously on the point and the set -/ lemma continuous_inf_edist_Hausdorff_edist : continuous (Ξ»p : Ξ± Γ— (closeds Ξ±), inf_edist p.1 (p.2).val) := begin refine continuous_of_le_add_edist 2 (by simp) _, rintros ⟨x, s⟩ ⟨y, t⟩, calc inf_edist x (s.val) ≀ inf_edist x (t.val) + Hausdorff_edist (t.val) (s.val) : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≀ (inf_edist y (t.val) + edist x y) + Hausdorff_edist (t.val) (s.val) : add_le_add_right inf_edist_le_inf_edist_add_edist _ ... = inf_edist y (t.val) + (edist x y + Hausdorff_edist (s.val) (t.val)) : by simp [add_comm, add_left_comm, Hausdorff_edist_comm, -subtype.val_eq_coe] ... ≀ inf_edist y (t.val) + (edist (x, s) (y, t) + edist (x, s) (y, t)) : add_le_add_left (add_le_add (le_max_left _ _) (le_max_right _ _)) _ ... = inf_edist y (t.val) + 2 * edist (x, s) (y, t) : by rw [← mul_two, mul_comm] end /-- Subsets of a given closed subset form a closed set -/ lemma is_closed_subsets_of_is_closed (hs : is_closed s) : is_closed {t : closeds Ξ± | t.val βŠ† s} := begin refine is_closed_of_closure_subset (Ξ»t ht x hx, _), -- t : closeds Ξ±, ht : t ∈ closure {t : closeds Ξ± | t.val βŠ† s}, -- x : Ξ±, hx : x ∈ t.val -- goal : x ∈ s have : x ∈ closure s, { refine mem_closure_iff.2 (λΡ Ξ΅pos, _), rcases mem_closure_iff.1 ht Ξ΅ Ξ΅pos with ⟨u, hu, Dtu⟩, -- u : closeds Ξ±, hu : u ∈ {t : closeds Ξ± | t.val βŠ† s}, hu' : edist t u < Ξ΅ rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dtu with ⟨y, hy, Dxy⟩, -- y : Ξ±, hy : y ∈ u.val, Dxy : edist x y < Ξ΅ exact ⟨y, hu hy, Dxy⟩ }, rwa hs.closure_eq at this, end /-- By definition, the edistance on `closeds Ξ±` is given by the Hausdorff edistance -/ lemma closeds.edist_eq {s t : closeds Ξ±} : edist s t = Hausdorff_edist s.val t.val := rfl /-- In a complete space, the type of closed subsets is complete for the Hausdorff edistance. -/ instance closeds.complete_space [complete_space Ξ±] : complete_space (closeds Ξ±) := begin /- We will show that, if a sequence of sets `s n` satisfies `edist (s n) (s (n+1)) < 2^{-n}`, then it converges. This is enough to guarantee completeness, by a standard completeness criterion. We use the shorthand `B n = 2^{-n}` in ennreal. -/ let B : β„• β†’ ℝβ‰₯0∞ := Ξ» n, (2⁻¹)^n, have B_pos : βˆ€ n, (0:ℝβ‰₯0∞) < B n, by simp [B, ennreal.pow_pos], have B_ne_top : βˆ€ n, B n β‰  ⊀, by simp [B, ennreal.pow_ne_top], /- Consider a sequence of closed sets `s n` with `edist (s n) (s (n+1)) < B n`. We will show that it converges. The limit set is t0 = β‹‚n, closure (⋃mβ‰₯n, s m). We will have to show that a point in `s n` is close to a point in `t0`, and a point in `t0` is close to a point in `s n`. The completeness then follows from a standard criterion. -/ refine complete_of_convergent_controlled_sequences B B_pos (Ξ»s hs, _), let t0 := β‹‚n, closure (⋃mβ‰₯n, (s m).val), let t : closeds Ξ± := ⟨t0, is_closed_Inter (Ξ»_, is_closed_closure)⟩, use t, -- The inequality is written this way to agree with `edist_le_of_edist_le_geometric_of_tendstoβ‚€` have I1 : βˆ€n:β„•, βˆ€x ∈ (s n).val, βˆƒy ∈ t0, edist x y ≀ 2 * B n, { /- This is the main difficulty of the proof. Starting from `x ∈ s n`, we want to find a point in `t0` which is close to `x`. Define inductively a sequence of points `z m` with `z n = x` and `z m ∈ s m` and `edist (z m) (z (m+1)) ≀ B m`. This is possible since the Hausdorff distance between `s m` and `s (m+1)` is at most `B m`. This sequence is a Cauchy sequence, therefore converging as the space is complete, to a limit which satisfies the required properties. -/ assume n x hx, obtain ⟨z, hzβ‚€, hz⟩ : βˆƒ z : Ξ  l, (s (n+l)).val, (z 0:Ξ±) = x ∧ βˆ€ k, edist (z k:Ξ±) (z (k+1):Ξ±) ≀ B n / 2^k, { -- We prove existence of the sequence by induction. have : βˆ€ (l : β„•) (z : (s (n+l)).val), βˆƒ z' : (s (n+l+1)).val, edist (z:Ξ±) z' ≀ B n / 2^l, { assume l z, obtain ⟨z', z'_mem, hz'⟩ : βˆƒ z' ∈ (s (n+l+1)).val, edist (z:Ξ±) z' < B n / 2^l, { apply exists_edist_lt_of_Hausdorff_edist_lt z.2, simp only [B, ennreal.inv_pow, div_eq_mul_inv], rw [← pow_add], apply hs; simp }, exact ⟨⟨z', z'_mem⟩, le_of_lt hz'⟩ }, use [Ξ» k, nat.rec_on k ⟨x, hx⟩ (Ξ»l z, some (this l z)), rfl], exact Ξ» k, some_spec (this k _) }, -- it follows from the previous bound that `z` is a Cauchy sequence have : cauchy_seq (Ξ» k, ((z k):Ξ±)), from cauchy_seq_of_edist_le_geometric_two (B n) (B_ne_top n) hz, -- therefore, it converges rcases cauchy_seq_tendsto_of_complete this with ⟨y, y_lim⟩, use y, -- the limit point `y` will be the desired point, in `t0` and close to our initial point `x`. -- First, we check it belongs to `t0`. have : y ∈ t0 := mem_Inter.2 (Ξ»k, mem_closure_of_tendsto y_lim begin simp only [exists_prop, set.mem_Union, filter.eventually_at_top, set.mem_preimage, set.preimage_Union], exact ⟨k, Ξ» m hm, ⟨n+m, zero_add k β–Έ add_le_add (zero_le n) hm, (z m).2⟩⟩ end), use this, -- Then, we check that `y` is close to `x = z n`. This follows from the fact that `y` -- is the limit of `z k`, and the distance between `z n` and `z k` has already been estimated. rw [← hzβ‚€], exact edist_le_of_edist_le_geometric_two_of_tendstoβ‚€ (B n) hz y_lim }, have I2 : βˆ€n:β„•, βˆ€x ∈ t0, βˆƒy ∈ (s n).val, edist x y ≀ 2 * B n, { /- For the (much easier) reverse inequality, we start from a point `x ∈ t0` and we want to find a point `y ∈ s n` which is close to `x`. `x` belongs to `t0`, the intersection of the closures. In particular, it is well approximated by a point `z` in `⋃mβ‰₯n, s m`, say in `s m`. Since `s m` and `s n` are close, this point is itself well approximated by a point `y` in `s n`, as required. -/ assume n x xt0, have : x ∈ closure (⋃mβ‰₯n, (s m).val), by apply mem_Inter.1 xt0 n, rcases mem_closure_iff.1 this (B n) (B_pos n) with ⟨z, hz, Dxz⟩, -- z : Ξ±, Dxz : edist x z < B n, simp only [exists_prop, set.mem_Union] at hz, rcases hz with ⟨m, ⟨m_ge_n, hm⟩⟩, -- m : β„•, m_ge_n : m β‰₯ n, hm : z ∈ (s m).val have : Hausdorff_edist (s m).val (s n).val < B n := hs n m n m_ge_n (le_refl n), rcases exists_edist_lt_of_Hausdorff_edist_lt hm this with ⟨y, hy, Dzy⟩, -- y : Ξ±, hy : y ∈ (s n).val, Dzy : edist z y < B n exact ⟨y, hy, calc edist x y ≀ edist x z + edist z y : edist_triangle _ _ _ ... ≀ B n + B n : add_le_add (le_of_lt Dxz) (le_of_lt Dzy) ... = 2 * B n : (two_mul _).symm ⟩ }, -- Deduce from the above inequalities that the distance between `s n` and `t0` is at most `2 B n`. have main : βˆ€n:β„•, edist (s n) t ≀ 2 * B n := Ξ»n, Hausdorff_edist_le_of_mem_edist (I1 n) (I2 n), -- from this, the convergence of `s n` to `t0` follows. refine tendsto_at_top.2 (λΡ Ξ΅pos, _), have : tendsto (Ξ»n, 2 * B n) at_top (𝓝 (2 * 0)), from ennreal.tendsto.const_mul (ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 $ by simp [ennreal.one_lt_two]) (or.inr $ by simp), rw mul_zero at this, obtain ⟨N, hN⟩ : βˆƒ N, βˆ€ b β‰₯ N, Ξ΅ > 2 * B b, from ((tendsto_order.1 this).2 Ξ΅ Ξ΅pos).exists_forall_of_at_top, exact ⟨N, Ξ»n hn, lt_of_le_of_lt (main n) (hN n hn)⟩ end /-- In a compact space, the type of closed subsets is compact. -/ instance closeds.compact_space [compact_space Ξ±] : compact_space (closeds Ξ±) := ⟨begin /- by completeness, it suffices to show that it is totally bounded, i.e., for all Ξ΅>0, there is a finite set which is Ξ΅-dense. start from a set `s` which is Ξ΅-dense in Ξ±. Then the subsets of `s` are finitely many, and Ξ΅-dense for the Hausdorff distance. -/ refine compact_of_totally_bounded_is_closed (emetric.totally_bounded_iff.2 (λΡ Ξ΅pos, _)) is_closed_univ, rcases exists_between Ξ΅pos with ⟨δ, Ξ΄pos, Ξ΄lt⟩, rcases emetric.totally_bounded_iff.1 (compact_iff_totally_bounded_complete.1 (@compact_univ Ξ± _ _)).1 Ξ΄ Ξ΄pos with ⟨s, fs, hs⟩, -- s : set Ξ±, fs : finite s, hs : univ βŠ† ⋃ (y : Ξ±) (H : y ∈ s), eball y Ξ΄ -- we first show that any set is well approximated by a subset of `s`. have main : βˆ€ u : set Ξ±, βˆƒv βŠ† s, Hausdorff_edist u v ≀ Ξ΄, { assume u, let v := {x : Ξ± | x ∈ s ∧ βˆƒy∈u, edist x y < Ξ΄}, existsi [v, ((Ξ»x hx, hx.1) : v βŠ† s)], refine Hausdorff_edist_le_of_mem_edist _ _, { assume x hx, have : x ∈ ⋃y ∈ s, ball y Ξ΄ := hs (by simp), rcases mem_bUnion_iff.1 this with ⟨y, ys, dy⟩, have : edist y x < Ξ΄ := by simp at dy; rwa [edist_comm] at dy, exact ⟨y, ⟨ys, ⟨x, hx, this⟩⟩, le_of_lt dy⟩ }, { rintros x ⟨hx1, ⟨y, yu, hy⟩⟩, exact ⟨y, yu, le_of_lt hy⟩ }}, -- introduce the set F of all subsets of `s` (seen as members of `closeds Ξ±`). let F := {f : closeds Ξ± | f.val βŠ† s}, use F, split, -- `F` is finite { apply @finite_of_finite_image _ _ F (Ξ»f, f.val), { exact subtype.val_injective.inj_on F }, { refine fs.finite_subsets.subset (Ξ»b, _), simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib], assume x hx hx', rwa hx' at hx }}, -- `F` is Ξ΅-dense { assume u _, rcases main u.val with ⟨t0, t0s, Dut0⟩, have : is_closed t0 := (fs.subset t0s).is_compact.is_closed, let t : closeds Ξ± := ⟨t0, this⟩, have : t ∈ F := t0s, have : edist u t < Ξ΅ := lt_of_le_of_lt Dut0 Ξ΄lt, apply mem_bUnion_iff.2, exact ⟨t, β€Ήt ∈ Fβ€Ί, this⟩ } end⟩ /-- In an emetric space, the type of non-empty compact subsets is an emetric space, where the edistance is the Hausdorff edistance -/ instance nonempty_compacts.emetric_space : emetric_space (nonempty_compacts Ξ±) := { edist := Ξ»s t, Hausdorff_edist s.val t.val, edist_self := Ξ»s, Hausdorff_edist_self, edist_comm := Ξ»s t, Hausdorff_edist_comm, edist_triangle := Ξ»s t u, Hausdorff_edist_triangle, eq_of_edist_eq_zero := Ξ»s t h, subtype.eq $ begin have : closure (s.val) = closure (t.val) := Hausdorff_edist_zero_iff_closure_eq_closure.1 h, rwa [s.property.2.is_closed.closure_eq, t.property.2.is_closed.closure_eq] at this, end } /-- `nonempty_compacts.to_closeds` is a uniform embedding (as it is an isometry) -/ lemma nonempty_compacts.to_closeds.uniform_embedding : uniform_embedding (@nonempty_compacts.to_closeds Ξ± _ _) := isometry.uniform_embedding $ Ξ»x y, rfl /-- The range of `nonempty_compacts.to_closeds` is closed in a complete space -/ lemma nonempty_compacts.is_closed_in_closeds [complete_space Ξ±] : is_closed (range $ @nonempty_compacts.to_closeds Ξ± _ _) := begin have : range nonempty_compacts.to_closeds = {s : closeds Ξ± | s.val.nonempty ∧ is_compact s.val}, from range_inclusion _, rw this, refine is_closed_of_closure_subset (Ξ»s hs, ⟨_, _⟩), { -- take a set set t which is nonempty and at a finite distance of s rcases mem_closure_iff.1 hs ⊀ ennreal.coe_lt_top with ⟨t, ht, Dst⟩, rw edist_comm at Dst, -- since `t` is nonempty, so is `s` exact nonempty_of_Hausdorff_edist_ne_top ht.1 (ne_of_lt Dst) }, { refine compact_iff_totally_bounded_complete.2 ⟨_, s.property.is_complete⟩, refine totally_bounded_iff.2 (λΡ (Ξ΅pos : 0 < Ξ΅), _), -- we have to show that s is covered by finitely many eballs of radius Ξ΅ -- pick a nonempty compact set t at distance at most Ξ΅/2 of s rcases mem_closure_iff.1 hs (Ξ΅/2) (ennreal.half_pos Ξ΅pos.ne') with ⟨t, ht, Dst⟩, -- cover this space with finitely many balls of radius Ξ΅/2 rcases totally_bounded_iff.1 (compact_iff_totally_bounded_complete.1 ht.2).1 (Ξ΅/2) (ennreal.half_pos Ξ΅pos.ne') with ⟨u, fu, ut⟩, refine ⟨u, ⟨fu, Ξ»x hx, _⟩⟩, -- u : set Ξ±, fu : finite u, ut : t.val βŠ† ⋃ (y : Ξ±) (H : y ∈ u), eball y (Ξ΅ / 2) -- then s is covered by the union of the balls centered at u of radius Ξ΅ rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dst with ⟨z, hz, Dxz⟩, rcases mem_bUnion_iff.1 (ut hz) with ⟨y, hy, Dzy⟩, have : edist x y < Ξ΅ := calc edist x y ≀ edist x z + edist z y : edist_triangle _ _ _ ... < Ξ΅/2 + Ξ΅/2 : ennreal.add_lt_add Dxz Dzy ... = Ξ΅ : ennreal.add_halves _, exact mem_bUnion hy this }, end /-- In a complete space, the type of nonempty compact subsets is complete. This follows from the same statement for closed subsets -/ instance nonempty_compacts.complete_space [complete_space Ξ±] : complete_space (nonempty_compacts Ξ±) := (complete_space_iff_is_complete_range nonempty_compacts.to_closeds.uniform_embedding.to_uniform_inducing).2 $ nonempty_compacts.is_closed_in_closeds.is_complete /-- In a compact space, the type of nonempty compact subsets is compact. This follows from the same statement for closed subsets -/ instance nonempty_compacts.compact_space [compact_space Ξ±] : compact_space (nonempty_compacts Ξ±) := ⟨begin rw nonempty_compacts.to_closeds.uniform_embedding.embedding.is_compact_iff_is_compact_image, rw [image_univ], exact nonempty_compacts.is_closed_in_closeds.is_compact end⟩ /-- In a second countable space, the type of nonempty compact subsets is second countable -/ instance nonempty_compacts.second_countable_topology [second_countable_topology Ξ±] : second_countable_topology (nonempty_compacts Ξ±) := begin haveI : separable_space (nonempty_compacts Ξ±) := begin /- To obtain a countable dense subset of `nonempty_compacts Ξ±`, start from a countable dense subset `s` of Ξ±, and then consider all its finite nonempty subsets. This set is countable and made of nonempty compact sets. It turns out to be dense: by total boundedness, any compact set `t` can be covered by finitely many small balls, and approximations in `s` of the centers of these balls give the required finite approximation of `t`. -/ rcases exists_countable_dense Ξ± with ⟨s, cs, s_dense⟩, let v0 := {t : set Ξ± | finite t ∧ t βŠ† s}, let v : set (nonempty_compacts Ξ±) := {t : nonempty_compacts Ξ± | t.val ∈ v0}, refine ⟨⟨v, ⟨_, _⟩⟩⟩, { have : countable (subtype.val '' v), { refine (countable_set_of_finite_subset cs).mono (Ξ»x hx, _), rcases (mem_image _ _ _).1 hx with ⟨y, ⟨hy, yx⟩⟩, rw ← yx, exact hy }, apply countable_of_injective_of_countable_image _ this, apply subtype.val_injective.inj_on }, { refine Ξ»t, mem_closure_iff.2 (λΡ Ξ΅pos, _), -- t is a compact nonempty set, that we have to approximate uniformly by a a set in `v`. rcases exists_between Ξ΅pos with ⟨δ, Ξ΄pos, Ξ΄lt⟩, have Ξ΄pos' : 0 < Ξ΄ / 2, from ennreal.half_pos Ξ΄pos.ne', -- construct a map F associating to a point in Ξ± an approximating point in s, up to Ξ΄/2. have Exy : βˆ€x, βˆƒy, y ∈ s ∧ edist x y < Ξ΄/2, { assume x, rcases mem_closure_iff.1 (s_dense x) (Ξ΄/2) Ξ΄pos' with ⟨y, ys, hy⟩, exact ⟨y, ⟨ys, hy⟩⟩ }, let F := Ξ»x, some (Exy x), have Fspec : βˆ€x, F x ∈ s ∧ edist x (F x) < Ξ΄/2 := Ξ»x, some_spec (Exy x), -- cover `t` with finitely many balls. Their centers form a set `a` have : totally_bounded t.val := (compact_iff_totally_bounded_complete.1 t.property.2).1, rcases totally_bounded_iff.1 this (Ξ΄/2) Ξ΄pos' with ⟨a, af, ta⟩, -- a : set Ξ±, af : finite a, ta : t.val βŠ† ⋃ (y : Ξ±) (H : y ∈ a), eball y (Ξ΄ / 2) -- replace each center by a nearby approximation in `s`, giving a new set `b` let b := F '' a, have : finite b := af.image _, have tb : βˆ€x ∈ t.val, βˆƒy ∈ b, edist x y < Ξ΄, { assume x hx, rcases mem_bUnion_iff.1 (ta hx) with ⟨z, za, Dxz⟩, existsi [F z, mem_image_of_mem _ za], calc edist x (F z) ≀ edist x z + edist z (F z) : edist_triangle _ _ _ ... < Ξ΄/2 + Ξ΄/2 : ennreal.add_lt_add Dxz (Fspec z).2 ... = Ξ΄ : ennreal.add_halves _ }, -- keep only the points in `b` that are close to point in `t`, yielding a new set `c` let c := {y ∈ b | βˆƒx∈t.val, edist x y < Ξ΄}, have : finite c := β€Ήfinite bβ€Ί.subset (Ξ»x hx, hx.1), -- points in `t` are well approximated by points in `c` have tc : βˆ€x ∈ t.val, βˆƒy ∈ c, edist x y ≀ Ξ΄, { assume x hx, rcases tb x hx with ⟨y, yv, Dxy⟩, have : y ∈ c := by simp [c, -mem_image]; exact ⟨yv, ⟨x, hx, Dxy⟩⟩, exact ⟨y, this, le_of_lt Dxy⟩ }, -- points in `c` are well approximated by points in `t` have ct : βˆ€y ∈ c, βˆƒx ∈ t.val, edist y x ≀ Ξ΄, { rintros y ⟨hy1, ⟨x, xt, Dyx⟩⟩, have : edist y x ≀ Ξ΄ := calc edist y x = edist x y : edist_comm _ _ ... ≀ Ξ΄ : le_of_lt Dyx, exact ⟨x, xt, this⟩ }, -- it follows that their Hausdorff distance is small have : Hausdorff_edist t.val c ≀ Ξ΄ := Hausdorff_edist_le_of_mem_edist tc ct, have Dtc : Hausdorff_edist t.val c < Ξ΅ := lt_of_le_of_lt this Ξ΄lt, -- the set `c` is not empty, as it is well approximated by a nonempty set have hc : c.nonempty, from nonempty_of_Hausdorff_edist_ne_top t.property.1 (ne_top_of_lt Dtc), -- let `d` be the version of `c` in the type `nonempty_compacts Ξ±` let d : nonempty_compacts Ξ± := ⟨c, ⟨hc, β€Ήfinite cβ€Ί.is_compact⟩⟩, have : c βŠ† s, { assume x hx, rcases (mem_image _ _ _).1 hx.1 with ⟨y, ⟨ya, yx⟩⟩, rw ← yx, exact (Fspec y).1 }, have : d ∈ v := βŸ¨β€Ήfinite cβ€Ί, this⟩, -- we have proved that `d` is a good approximation of `t` as requested exact ⟨d, β€Ήd ∈ vβ€Ί, Dtc⟩ }, end, apply second_countable_of_separable, end end --section end emetric --namespace namespace metric section variables {Ξ± : Type u} [metric_space Ξ±] /-- `nonempty_compacts Ξ±` inherits a metric space structure, as the Hausdorff edistance between two such sets is finite. -/ instance nonempty_compacts.metric_space : metric_space (nonempty_compacts Ξ±) := emetric_space.to_metric_space $ Ξ»x y, Hausdorff_edist_ne_top_of_nonempty_of_bounded x.2.1 y.2.1 x.2.2.bounded y.2.2.bounded /-- The distance on `nonempty_compacts Ξ±` is the Hausdorff distance, by construction -/ lemma nonempty_compacts.dist_eq {x y : nonempty_compacts Ξ±} : dist x y = Hausdorff_dist x.val y.val := rfl lemma lipschitz_inf_dist_set (x : Ξ±) : lipschitz_with 1 (Ξ» s : nonempty_compacts Ξ±, inf_dist x s.val) := lipschitz_with.of_le_add $ assume s t, by { rw dist_comm, exact inf_dist_le_inf_dist_add_Hausdorff_dist (edist_ne_top t s) } lemma lipschitz_inf_dist : lipschitz_with 2 (Ξ» p : Ξ± Γ— (nonempty_compacts Ξ±), inf_dist p.1 p.2.val) := @lipschitz_with.uncurry _ _ _ _ _ _ (Ξ» (x : Ξ±) (s : nonempty_compacts Ξ±), inf_dist x s.val) 1 1 (Ξ» s, lipschitz_inf_dist_pt s.val) lipschitz_inf_dist_set lemma uniform_continuous_inf_dist_Hausdorff_dist : uniform_continuous (Ξ»p : Ξ± Γ— (nonempty_compacts Ξ±), inf_dist p.1 (p.2).val) := lipschitz_inf_dist.uniform_continuous end --section end metric --namespace
9d3d71286c8c8f4f94cb0bcb0330d969a61081ac
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/tactic/norm_num.lean
29ca1f35a1488a019f384f2d8205a6d8f8498db2
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
62,145
lean
/- Copyright (c) 2017 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Mario Carneiro -/ import data.rat.cast import data.rat.meta_defs import tactic.doc_commands /-! # `norm_num` Evaluating arithmetic expressions including *, +, -, ^, ≀ -/ universes u v w namespace tactic /-- Reflexivity conversion: given `e` returns `(e, ⊒ e = e)` -/ meta def refl_conv (e : expr) : tactic (expr Γ— expr) := do p ← mk_eq_refl e, return (e, p) /-- Transitivity conversion: given two conversions (which take an expression `e` and returns `(e', ⊒ e = e')`), produces another conversion that combines them with transitivity, treating failures as reflexivity conversions. -/ meta def trans_conv (t₁ tβ‚‚ : expr β†’ tactic (expr Γ— expr)) (e : expr) : tactic (expr Γ— expr) := (do (e₁, p₁) ← t₁ e, (do (eβ‚‚, pβ‚‚) ← tβ‚‚ e₁, p ← mk_eq_trans p₁ pβ‚‚, return (eβ‚‚, p)) <|> return (e₁, p₁)) <|> tβ‚‚ e namespace instance_cache /-- Faster version of `mk_app ``bit0 [e]`. -/ meta def mk_bit0 (c : instance_cache) (e : expr) : tactic (instance_cache Γ— expr) := do (c, ai) ← c.get ``has_add, return (c, (expr.const ``bit0 [c.univ]).mk_app [c.Ξ±, ai, e]) /-- Faster version of `mk_app ``bit1 [e]`. -/ meta def mk_bit1 (c : instance_cache) (e : expr) : tactic (instance_cache Γ— expr) := do (c, ai) ← c.get ``has_add, (c, oi) ← c.get ``has_one, return (c, (expr.const ``bit1 [c.univ]).mk_app [c.Ξ±, oi, ai, e]) end instance_cache end tactic open tactic namespace norm_num variable {Ξ± : Type u} lemma subst_into_add {Ξ±} [has_add Ξ±] (l r tl tr t) (prl : (l : Ξ±) = tl) (prr : r = tr) (prt : tl + tr = t) : l + r = t := by rw [prl, prr, prt] lemma subst_into_mul {Ξ±} [has_mul Ξ±] (l r tl tr t) (prl : (l : Ξ±) = tl) (prr : r = tr) (prt : tl * tr = t) : l * r = t := by rw [prl, prr, prt] lemma subst_into_neg {Ξ±} [has_neg Ξ±] (a ta t : Ξ±) (pra : a = ta) (prt : -ta = t) : -a = t := by simp [pra, prt] /-- The result type of `match_numeral`, either `0`, `1`, or a top level decomposition of `bit0 e` or `bit1 e`. The `other` case means it is not a numeral. -/ meta inductive match_numeral_result | zero | one | bit0 (e : expr) | bit1 (e : expr) | other /-- Unfold the top level constructor of the numeral expression. -/ meta def match_numeral : expr β†’ match_numeral_result | `(bit0 %%e) := match_numeral_result.bit0 e | `(bit1 %%e) := match_numeral_result.bit1 e | `(@has_zero.zero _ _) := match_numeral_result.zero | `(@has_one.one _ _) := match_numeral_result.one | _ := match_numeral_result.other theorem zero_succ {Ξ±} [semiring Ξ±] : (0 + 1 : Ξ±) = 1 := zero_add _ theorem one_succ {Ξ±} [semiring Ξ±] : (1 + 1 : Ξ±) = 2 := rfl theorem bit0_succ {Ξ±} [semiring Ξ±] (a : Ξ±) : bit0 a + 1 = bit1 a := rfl theorem bit1_succ {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : bit1 a + 1 = bit0 b := h β–Έ by simp [bit1, bit0, add_left_comm, add_assoc] section open match_numeral_result /-- Given `a`, `b` natural numerals, proves `⊒ a + 1 = b`, assuming that this is provable. (It may prove garbage instead of failing if `a + 1 = b` is false.) -/ meta def prove_succ : instance_cache β†’ expr β†’ expr β†’ tactic (instance_cache Γ— expr) | c e r := match match_numeral e with | zero := c.mk_app ``zero_succ [] | one := c.mk_app ``one_succ [] | bit0 e := c.mk_app ``bit0_succ [e] | bit1 e := do let r := r.app_arg, (c, p) ← prove_succ c e r, c.mk_app ``bit1_succ [e, r, p] | _ := failed end end theorem zero_adc {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : 0 + a + 1 = b := by rwa zero_add theorem adc_zero {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : a + 0 + 1 = b := by rwa add_zero theorem one_add {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : 1 + a = b := by rwa add_comm theorem add_bit0_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b = c) : bit0 a + bit0 b = bit0 c := h β–Έ by simp [bit0, add_left_comm, add_assoc] theorem add_bit0_bit1 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b = c) : bit0 a + bit1 b = bit1 c := h β–Έ by simp [bit0, bit1, add_left_comm, add_assoc] theorem add_bit1_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b = c) : bit1 a + bit0 b = bit1 c := h β–Έ by simp [bit0, bit1, add_left_comm, add_comm] theorem add_bit1_bit1 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b + 1 = c) : bit1 a + bit1 b = bit0 c := h β–Έ by simp [bit0, bit1, add_left_comm, add_comm] theorem adc_one_one {Ξ±} [semiring Ξ±] : (1 + 1 + 1 : Ξ±) = 3 := rfl theorem adc_bit0_one {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : bit0 a + 1 + 1 = bit0 b := h β–Έ by simp [bit0, add_left_comm, add_assoc] theorem adc_one_bit0 {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : 1 + bit0 a + 1 = bit0 b := h β–Έ by simp [bit0, add_left_comm, add_assoc] theorem adc_bit1_one {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : bit1 a + 1 + 1 = bit1 b := h β–Έ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_one_bit1 {Ξ±} [semiring Ξ±] (a b : Ξ±) (h : a + 1 = b) : 1 + bit1 a + 1 = bit1 b := h β–Έ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_bit0_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b = c) : bit0 a + bit0 b + 1 = bit1 c := h β–Έ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_bit1_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b + 1 = c) : bit1 a + bit0 b + 1 = bit0 c := h β–Έ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_bit0_bit1 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b + 1 = c) : bit0 a + bit1 b + 1 = bit0 c := h β–Έ by simp [bit1, bit0, add_left_comm, add_assoc] theorem adc_bit1_bit1 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a + b + 1 = c) : bit1 a + bit1 b + 1 = bit1 c := h β–Έ by simp [bit1, bit0, add_left_comm, add_assoc] section open match_numeral_result meta mutual def prove_add_nat, prove_adc_nat with prove_add_nat : instance_cache β†’ expr β†’ expr β†’ expr β†’ tactic (instance_cache Γ— expr) | c a b r := do match match_numeral a, match_numeral b with | zero, _ := c.mk_app ``zero_add [b] | _, zero := c.mk_app ``add_zero [a] | _, one := prove_succ c a r | one, _ := do (c, p) ← prove_succ c b r, c.mk_app ``one_add [b, r, p] | bit0 a, bit0 b := do let r := r.app_arg, (c, p) ← prove_add_nat c a b r, c.mk_app ``add_bit0_bit0 [a, b, r, p] | bit0 a, bit1 b := do let r := r.app_arg, (c, p) ← prove_add_nat c a b r, c.mk_app ``add_bit0_bit1 [a, b, r, p] | bit1 a, bit0 b := do let r := r.app_arg, (c, p) ← prove_add_nat c a b r, c.mk_app ``add_bit1_bit0 [a, b, r, p] | bit1 a, bit1 b := do let r := r.app_arg, (c, p) ← prove_adc_nat c a b r, c.mk_app ``add_bit1_bit1 [a, b, r, p] | _, _ := failed end with prove_adc_nat : instance_cache β†’ expr β†’ expr β†’ expr β†’ tactic (instance_cache Γ— expr) | c a b r := do match match_numeral a, match_numeral b with | zero, _ := do (c, p) ← prove_succ c b r, c.mk_app ``zero_adc [b, r, p] | _, zero := do (c, p) ← prove_succ c b r, c.mk_app ``adc_zero [b, r, p] | one, one := c.mk_app ``adc_one_one [] | bit0 a, one := do let r := r.app_arg, (c, p) ← prove_succ c a r, c.mk_app ``adc_bit0_one [a, r, p] | one, bit0 b := do let r := r.app_arg, (c, p) ← prove_succ c b r, c.mk_app ``adc_one_bit0 [b, r, p] | bit1 a, one := do let r := r.app_arg, (c, p) ← prove_succ c a r, c.mk_app ``adc_bit1_one [a, r, p] | one, bit1 b := do let r := r.app_arg, (c, p) ← prove_succ c b r, c.mk_app ``adc_one_bit1 [b, r, p] | bit0 a, bit0 b := do let r := r.app_arg, (c, p) ← prove_add_nat c a b r, c.mk_app ``adc_bit0_bit0 [a, b, r, p] | bit0 a, bit1 b := do let r := r.app_arg, (c, p) ← prove_adc_nat c a b r, c.mk_app ``adc_bit0_bit1 [a, b, r, p] | bit1 a, bit0 b := do let r := r.app_arg, (c, p) ← prove_adc_nat c a b r, c.mk_app ``adc_bit1_bit0 [a, b, r, p] | bit1 a, bit1 b := do let r := r.app_arg, (c, p) ← prove_adc_nat c a b r, c.mk_app ``adc_bit1_bit1 [a, b, r, p] | _, _ := failed end /-- Given `a`,`b`,`r` natural numerals, proves `⊒ a + b = r`. -/ add_decl_doc prove_add_nat /-- Given `a`,`b`,`r` natural numerals, proves `⊒ a + b + 1 = r`. -/ add_decl_doc prove_adc_nat /-- Given `a`,`b` natural numerals, returns `(r, ⊒ a + b = r)`. -/ meta def prove_add_nat' (c : instance_cache) (a b : expr) : tactic (instance_cache Γ— expr Γ— expr) := do na ← a.to_nat, nb ← b.to_nat, (c, r) ← c.of_nat (na + nb), (c, p) ← prove_add_nat c a b r, return (c, r, p) end theorem bit0_mul {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a * b = c) : bit0 a * b = bit0 c := h β–Έ by simp [bit0, add_mul] theorem mul_bit0' {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a * b = c) : a * bit0 b = bit0 c := h β–Έ by simp [bit0, mul_add] theorem mul_bit0_bit0 {Ξ±} [semiring Ξ±] (a b c : Ξ±) (h : a * b = c) : bit0 a * bit0 b = bit0 (bit0 c) := bit0_mul _ _ _ (mul_bit0' _ _ _ h) theorem mul_bit1_bit1 {Ξ±} [semiring Ξ±] (a b c d e : Ξ±) (hc : a * b = c) (hd : a + b = d) (he : bit0 c + d = e) : bit1 a * bit1 b = bit1 e := by rw [← he, ← hd, ← hc]; simp [bit1, bit0, mul_add, add_mul, add_left_comm, add_assoc] section open match_numeral_result /-- Given `a`,`b` natural numerals, returns `(r, ⊒ a * b = r)`. -/ meta def prove_mul_nat : instance_cache β†’ expr β†’ expr β†’ tactic (instance_cache Γ— expr Γ— expr) | ic a b := match match_numeral a, match_numeral b with | zero, _ := do (ic, z) ← ic.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``zero_mul [b], return (ic, z, p) | _, zero := do (ic, z) ← ic.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``mul_zero [a], return (ic, z, p) | one, _ := do (ic, p) ← ic.mk_app ``one_mul [b], return (ic, b, p) | _, one := do (ic, p) ← ic.mk_app ``mul_one [a], return (ic, a, p) | bit0 a, bit0 b := do (ic, c, p) ← prove_mul_nat ic a b, (ic, p) ← ic.mk_app ``mul_bit0_bit0 [a, b, c, p], (ic, c') ← ic.mk_bit0 c, (ic, c') ← ic.mk_bit0 c', return (ic, c', p) | bit0 a, _ := do (ic, c, p) ← prove_mul_nat ic a b, (ic, p) ← ic.mk_app ``bit0_mul [a, b, c, p], (ic, c') ← ic.mk_bit0 c, return (ic, c', p) | _, bit0 b := do (ic, c, p) ← prove_mul_nat ic a b, (ic, p) ← ic.mk_app ``mul_bit0' [a, b, c, p], (ic, c') ← ic.mk_bit0 c, return (ic, c', p) | bit1 a, bit1 b := do (ic, c, pc) ← prove_mul_nat ic a b, (ic, d, pd) ← prove_add_nat' ic a b, (ic, c') ← ic.mk_bit0 c, (ic, e, pe) ← prove_add_nat' ic c' d, (ic, p) ← ic.mk_app ``mul_bit1_bit1 [a, b, c, d, e, pc, pd, pe], (ic, e') ← ic.mk_bit1 e, return (ic, e', p) | _, _ := failed end end section open match_numeral_result /-- Given `a` a positive natural numeral, returns `⊒ 0 < a`. -/ meta def prove_pos_nat (c : instance_cache) : expr β†’ tactic (instance_cache Γ— expr) | e := match match_numeral e with | one := c.mk_app ``zero_lt_one [] | bit0 e := do (c, p) ← prove_pos_nat e, c.mk_app ``bit0_pos [e, p] | bit1 e := do (c, p) ← prove_pos_nat e, c.mk_app ``bit1_pos' [e, p] | _ := failed end end /-- Given `a` a rational numeral, returns `⊒ 0 < a`. -/ meta def prove_pos (c : instance_cache) : expr β†’ tactic (instance_cache Γ— expr) | `(%%e₁ / %%eβ‚‚) := do (c, p₁) ← prove_pos_nat c e₁, (c, pβ‚‚) ← prove_pos_nat c eβ‚‚, c.mk_app ``div_pos_of_pos_of_pos [e₁, eβ‚‚, p₁, pβ‚‚] | e := prove_pos_nat c e /-- `match_neg (- e) = some e`, otherwise `none` -/ meta def match_neg : expr β†’ option expr | `(- %%e) := some e | _ := none /-- `match_sign (- e) = inl e`, `match_sign 0 = inr ff`, otherwise `inr tt` -/ meta def match_sign : expr β†’ expr βŠ• bool | `(- %%e) := sum.inl e | `(has_zero.zero) := sum.inr ff | _ := sum.inr tt theorem ne_zero_of_pos {Ξ±} [ordered_add_comm_group Ξ±] (a : Ξ±) : 0 < a β†’ a β‰  0 := ne_of_gt theorem ne_zero_neg {Ξ±} [add_group Ξ±] (a : Ξ±) : a β‰  0 β†’ -a β‰  0 := mt neg_eq_zero.1 /-- Given `a` a rational numeral, returns `⊒ a β‰  0`. -/ meta def prove_ne_zero (c : instance_cache) : expr β†’ tactic (instance_cache Γ— expr) | a := match match_neg a with | some a := do (c, p) ← prove_ne_zero a, c.mk_app ``ne_zero_neg [a, p] | none := do (c, p) ← prove_pos c a, c.mk_app ``ne_zero_of_pos [a, p] end theorem clear_denom_div {Ξ±} [division_ring Ξ±] (a b b' c d : Ξ±) (hβ‚€ : b β‰  0) (h₁ : b * b' = d) (hβ‚‚ : a * b' = c) : (a / b) * d = c := by rwa [← h₁, ← mul_assoc, div_mul_cancel _ hβ‚€] /-- Given `a` nonnegative rational and `d` a natural number, returns `(b, ⊒ a * d = b)`. (`d` should be a multiple of the denominator of `a`, so that `b` is a natural number.) -/ meta def prove_clear_denom (c : instance_cache) (a d : expr) (na : β„š) (nd : β„•) : tactic (instance_cache Γ— expr Γ— expr) := if na.denom = 1 then prove_mul_nat c a d else do [_, _, a, b] ← return a.get_app_args, (c, b') ← c.of_nat (nd / na.denom), (c, pβ‚€) ← prove_ne_zero c b, (c, _, p₁) ← prove_mul_nat c b b', (c, r, pβ‚‚) ← prove_mul_nat c a b', (c, p) ← c.mk_app ``clear_denom_div [a, b, b', r, d, pβ‚€, p₁, pβ‚‚], return (c, r, p) theorem clear_denom_add {Ξ±} [division_ring Ξ±] (a a' b b' c c' d : Ξ±) (hβ‚€ : d β‰  0) (ha : a * d = a') (hb : b * d = b') (hc : c * d = c') (h : a' + b' = c') : a + b = c := mul_right_cancel' hβ‚€ $ by rwa [add_mul, ha, hb, hc] /-- Given `a`,`b`,`c` nonnegative rational numerals, returns `⊒ a + b = c`. -/ meta def prove_add_nonneg_rat (ic : instance_cache) (a b c : expr) (na nb nc : β„š) : tactic (instance_cache Γ— expr) := if na.denom = 1 ∧ nb.denom = 1 then prove_add_nat ic a b c else do let nd := na.denom.lcm nb.denom, (ic, d) ← ic.of_nat nd, (ic, pβ‚€) ← prove_ne_zero ic d, (ic, a', pa) ← prove_clear_denom ic a d na nd, (ic, b', pb) ← prove_clear_denom ic b d nb nd, (ic, c', pc) ← prove_clear_denom ic c d nc nd, (ic, p) ← prove_add_nat ic a' b' c', ic.mk_app ``clear_denom_add [a, a', b, b', c, c', d, pβ‚€, pa, pb, pc, p] theorem add_pos_neg_pos {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : c + b = a) : a + -b = c := h β–Έ by simp theorem add_pos_neg_neg {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : c + a = b) : a + -b = -c := h β–Έ by simp theorem add_neg_pos_pos {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : a + c = b) : -a + b = c := h β–Έ by simp theorem add_neg_pos_neg {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : b + c = a) : -a + b = -c := h β–Έ by simp theorem add_neg_neg {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : b + a = c) : -a + -b = -c := h β–Έ by simp /-- Given `a`,`b`,`c` rational numerals, returns `⊒ a + b = c`. -/ meta def prove_add_rat (ic : instance_cache) (ea eb ec : expr) (a b c : β„š) : tactic (instance_cache Γ— expr) := match match_neg ea, match_neg eb, match_neg ec with | some ea, some eb, some ec := do (ic, p) ← prove_add_nonneg_rat ic eb ea ec (-b) (-a) (-c), ic.mk_app ``add_neg_neg [ea, eb, ec, p] | some ea, none, some ec := do (ic, p) ← prove_add_nonneg_rat ic eb ec ea b (-c) (-a), ic.mk_app ``add_neg_pos_neg [ea, eb, ec, p] | some ea, none, none := do (ic, p) ← prove_add_nonneg_rat ic ea ec eb (-a) c b, ic.mk_app ``add_neg_pos_pos [ea, eb, ec, p] | none, some eb, some ec := do (ic, p) ← prove_add_nonneg_rat ic ec ea eb (-c) a (-b), ic.mk_app ``add_pos_neg_neg [ea, eb, ec, p] | none, some eb, none := do (ic, p) ← prove_add_nonneg_rat ic ec eb ea c (-b) a, ic.mk_app ``add_pos_neg_pos [ea, eb, ec, p] | _, _, _ := prove_add_nonneg_rat ic ea eb ec a b c end /-- Given `a`,`b` rational numerals, returns `(c, ⊒ a + b = c)`. -/ meta def prove_add_rat' (ic : instance_cache) (a b : expr) : tactic (instance_cache Γ— expr Γ— expr) := do na ← a.to_rat, nb ← b.to_rat, let nc := na + nb, (ic, c) ← ic.of_rat nc, (ic, p) ← prove_add_rat ic a b c na nb nc, return (ic, c, p) theorem clear_denom_simple_nat {Ξ±} [division_ring Ξ±] (a : Ξ±) : (1:Ξ±) β‰  0 ∧ a * 1 = a := ⟨one_ne_zero, mul_one _⟩ theorem clear_denom_simple_div {Ξ±} [division_ring Ξ±] (a b : Ξ±) (h : b β‰  0) : b β‰  0 ∧ a / b * b = a := ⟨h, div_mul_cancel _ h⟩ /-- Given `a` a nonnegative rational numeral, returns `(b, c, ⊒ a * b = c)` where `b` and `c` are natural numerals. (`b` will be the denominator of `a`.) -/ meta def prove_clear_denom_simple (c : instance_cache) (a : expr) (na : β„š) : tactic (instance_cache Γ— expr Γ— expr Γ— expr) := if na.denom = 1 then do (c, d) ← c.mk_app ``has_one.one [], (c, p) ← c.mk_app ``clear_denom_simple_nat [a], return (c, d, a, p) else do [Ξ±, _, a, b] ← return a.get_app_args, (c, pβ‚€) ← prove_ne_zero c b, (c, p) ← c.mk_app ``clear_denom_simple_div [a, b, pβ‚€], return (c, b, a, p) theorem clear_denom_mul {Ξ±} [field Ξ±] (a a' b b' c c' d₁ dβ‚‚ d : Ξ±) (ha : d₁ β‰  0 ∧ a * d₁ = a') (hb : dβ‚‚ β‰  0 ∧ b * dβ‚‚ = b') (hc : c * d = c') (hd : d₁ * dβ‚‚ = d) (h : a' * b' = c') : a * b = c := mul_right_cancel' ha.1 $ mul_right_cancel' hb.1 $ by rw [mul_assoc c, hd, hc, ← h, ← ha.2, ← hb.2, ← mul_assoc, mul_right_comm a] /-- Given `a`,`b` nonnegative rational numerals, returns `(c, ⊒ a * b = c)`. -/ meta def prove_mul_nonneg_rat (ic : instance_cache) (a b : expr) (na nb : β„š) : tactic (instance_cache Γ— expr Γ— expr) := if na.denom = 1 ∧ nb.denom = 1 then prove_mul_nat ic a b else do let nc := na * nb, (ic, c) ← ic.of_rat nc, (ic, d₁, a', pa) ← prove_clear_denom_simple ic a na, (ic, dβ‚‚, b', pb) ← prove_clear_denom_simple ic b nb, (ic, d, pd) ← prove_mul_nat ic d₁ dβ‚‚, nd ← d.to_nat, (ic, c', pc) ← prove_clear_denom ic c d nc nd, (ic, _, p) ← prove_mul_nat ic a' b', (ic, p) ← ic.mk_app ``clear_denom_mul [a, a', b, b', c, c', d₁, dβ‚‚, d, pa, pb, pc, pd, p], return (ic, c, p) theorem mul_neg_pos {Ξ±} [ring Ξ±] (a b c : Ξ±) (h : a * b = c) : -a * b = -c := h β–Έ by simp theorem mul_pos_neg {Ξ±} [ring Ξ±] (a b c : Ξ±) (h : a * b = c) : a * -b = -c := h β–Έ by simp theorem mul_neg_neg {Ξ±} [ring Ξ±] (a b c : Ξ±) (h : a * b = c) : -a * -b = c := h β–Έ by simp /-- Given `a`,`b` rational numerals, returns `(c, ⊒ a * b = c)`. -/ meta def prove_mul_rat (ic : instance_cache) (a b : expr) (na nb : β„š) : tactic (instance_cache Γ— expr Γ— expr) := match match_sign a, match_sign b with | sum.inl a, sum.inl b := do (ic, c, p) ← prove_mul_nonneg_rat ic a b (-na) (-nb), (ic, p) ← ic.mk_app ``mul_neg_neg [a, b, c, p], return (ic, c, p) | sum.inr ff, _ := do (ic, z) ← ic.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``zero_mul [b], return (ic, z, p) | _, sum.inr ff := do (ic, z) ← ic.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``mul_zero [a], return (ic, z, p) | sum.inl a, sum.inr tt := do (ic, c, p) ← prove_mul_nonneg_rat ic a b (-na) nb, (ic, p) ← ic.mk_app ``mul_neg_pos [a, b, c, p], (ic, c') ← ic.mk_app ``has_neg.neg [c], return (ic, c', p) | sum.inr tt, sum.inl b := do (ic, c, p) ← prove_mul_nonneg_rat ic a b na (-nb), (ic, p) ← ic.mk_app ``mul_pos_neg [a, b, c, p], (ic, c') ← ic.mk_app ``has_neg.neg [c], return (ic, c', p) | sum.inr tt, sum.inr tt := prove_mul_nonneg_rat ic a b na nb end theorem inv_neg {Ξ±} [division_ring Ξ±] (a b : Ξ±) (h : a⁻¹ = b) : (-a)⁻¹ = -b := h β–Έ by simp only [inv_eq_one_div, one_div_neg_eq_neg_one_div] theorem inv_one {Ξ±} [division_ring Ξ±] : (1 : Ξ±)⁻¹ = 1 := inv_one theorem inv_one_div {Ξ±} [division_ring Ξ±] (a : Ξ±) : (1 / a)⁻¹ = a := by rw [one_div_eq_inv, inv_inv'] theorem inv_div_one {Ξ±} [division_ring Ξ±] (a : Ξ±) : a⁻¹ = 1 / a := inv_eq_one_div _ theorem inv_div {Ξ±} [division_ring Ξ±] (a b : Ξ±) : (a / b)⁻¹ = b / a := by simp only [inv_eq_one_div, one_div_div] /-- Given `a` a rational numeral, returns `(b, ⊒ a⁻¹ = b)`. -/ meta def prove_inv : instance_cache β†’ expr β†’ β„š β†’ tactic (instance_cache Γ— expr Γ— expr) | ic e n := match match_sign e with | sum.inl e := do (ic, e', p) ← prove_inv ic e (-n), (ic, r) ← ic.mk_app ``has_neg.neg [e'], (ic, p) ← ic.mk_app ``inv_neg [e, e', p], return (ic, r, p) | sum.inr ff := do (ic, p) ← ic.mk_app ``inv_zero [], return (ic, e, p) | sum.inr tt := if n.num = 1 then if n.denom = 1 then do (ic, p) ← ic.mk_app ``inv_one [], return (ic, e, p) else do let e := e.app_arg, (ic, p) ← ic.mk_app ``inv_one_div [e], return (ic, e, p) else if n.denom = 1 then do (ic, p) ← ic.mk_app ``inv_div_one [e], e ← infer_type p, return (ic, e.app_arg, p) else do [_, _, a, b] ← return e.get_app_args, (ic, e') ← ic.mk_app ``has_div.div [b, a], (ic, p) ← ic.mk_app ``inv_div [a, b], return (ic, e', p) end theorem div_eq {Ξ±} [division_ring Ξ±] (a b b' c : Ξ±) (hb : b⁻¹ = b') (h : a * b' = c) : a / b = c := by rwa ← hb at h /-- Given `a`,`b` rational numerals, returns `(c, ⊒ a / b = c)`. -/ meta def prove_div (ic : instance_cache) (a b : expr) (na nb : β„š) : tactic (instance_cache Γ— expr Γ— expr) := do (ic, b', pb) ← prove_inv ic b nb, (ic, c, p) ← prove_mul_rat ic a b' na nb⁻¹, (ic, p) ← ic.mk_app ``div_eq [a, b, b', c, pb, p], return (ic, c, p) /-- Given `a` a rational numeral, returns `(b, ⊒ -a = b)`. -/ meta def prove_neg (ic : instance_cache) (a : expr) : tactic (instance_cache Γ— expr Γ— expr) := match match_sign a with | sum.inl a := do (ic, p) ← ic.mk_app ``neg_neg [a], return (ic, a, p) | sum.inr ff := do (ic, p) ← ic.mk_app ``neg_zero [], return (ic, a, p) | sum.inr tt := do (ic, a') ← ic.mk_app ``has_neg.neg [a], p ← mk_eq_refl a', return (ic, a', p) end theorem sub_pos {Ξ±} [add_group Ξ±] (a b b' c : Ξ±) (hb : -b = b') (h : a + b' = c) : a - b = c := by rwa ← hb at h theorem sub_neg {Ξ±} [add_group Ξ±] (a b c : Ξ±) (h : a + b = c) : a - -b = c := by rwa sub_neg_eq_add /-- Given `a`,`b` rational numerals, returns `(c, ⊒ a - b = c)`. -/ meta def prove_sub (ic : instance_cache) (a b : expr) : tactic (instance_cache Γ— expr Γ— expr) := match match_sign b with | sum.inl b := do (ic, c, p) ← prove_add_rat' ic a b, (ic, p) ← ic.mk_app ``sub_neg [a, b, c, p], return (ic, c, p) | sum.inr ff := do (ic, p) ← ic.mk_app ``sub_zero [a], return (ic, a, p) | sum.inr tt := do (ic, b', pb) ← prove_neg ic b, (ic, c, p) ← prove_add_rat' ic a b', (ic, p) ← ic.mk_app ``sub_pos [a, b, b', c, pb, p], return (ic, c, p) end theorem sub_nat_pos (a b c : β„•) (h : b + c = a) : a - b = c := h β–Έ nat.add_sub_cancel_left _ _ theorem sub_nat_neg (a b c : β„•) (h : a + c = b) : a - b = 0 := nat.sub_eq_zero_of_le $ h β–Έ nat.le_add_right _ _ /-- Given `a : nat`,`b : nat` natural numerals, returns `(c, ⊒ a - b = c)`. -/ meta def prove_sub_nat (ic : instance_cache) (a b : expr) : tactic (expr Γ— expr) := do na ← a.to_nat, nb ← b.to_nat, if nb ≀ na then do (ic, c) ← ic.of_nat (na - nb), (ic, p) ← prove_add_nat ic b c a, return (c, `(sub_nat_pos).mk_app [a, b, c, p]) else do (ic, c) ← ic.of_nat (nb - na), (ic, p) ← prove_add_nat ic a c b, return (`(0 : β„•), `(sub_nat_neg).mk_app [a, b, c, p]) /-- This is needed because when `a` and `b` are numerals lean is more likely to unfold them than unfold the instances in order to prove that `add_group_has_sub = int.has_sub`. -/ theorem int_sub_hack (a b c : β„€) (h : @has_sub.sub β„€ add_group_has_sub a b = c) : a - b = c := h /-- Given `a : β„€`, `b : β„€` integral numerals, returns `(c, ⊒ a - b = c)`. -/ meta def prove_sub_int (ic : instance_cache) (a b : expr) : tactic (expr Γ— expr) := do (_, c, p) ← prove_sub ic a b, return (c, `(int_sub_hack).mk_app [a, b, c, p]) /-- Evaluates the basic field operations `+`,`neg`,`-`,`*`,`inv`,`/` on numerals. Also handles nat subtraction. Does not do recursive simplification; that is, `1 + 1 + 1` will not simplify but `2 + 1` will. This is handled by the top level `simp` call in `norm_num.derive`. -/ meta def eval_field : expr β†’ tactic (expr Γ— expr) | `(%%e₁ + %%eβ‚‚) := do n₁ ← e₁.to_rat, nβ‚‚ ← eβ‚‚.to_rat, c ← infer_type e₁ >>= mk_instance_cache, let n₃ := n₁ + nβ‚‚, (c, e₃) ← c.of_rat n₃, (_, p) ← prove_add_rat c e₁ eβ‚‚ e₃ n₁ nβ‚‚ n₃, return (e₃, p) | `(%%e₁ * %%eβ‚‚) := do n₁ ← e₁.to_rat, nβ‚‚ ← eβ‚‚.to_rat, c ← infer_type e₁ >>= mk_instance_cache, prod.snd <$> prove_mul_rat c e₁ eβ‚‚ n₁ nβ‚‚ | `(- %%e) := do c ← infer_type e >>= mk_instance_cache, prod.snd <$> prove_neg c e | `(@has_sub.sub %%Ξ± %%inst %%a %%b) := do c ← mk_instance_cache Ξ±, if Ξ± = `(nat) then prove_sub_nat c a b else if inst = `(int.has_sub) then prove_sub_int c a b else prod.snd <$> prove_sub c a b | `(has_inv.inv %%e) := do n ← e.to_rat, c ← infer_type e >>= mk_instance_cache, prod.snd <$> prove_inv c e n | `(%%e₁ / %%eβ‚‚) := do n₁ ← e₁.to_rat, nβ‚‚ ← eβ‚‚.to_rat, c ← infer_type e₁ >>= mk_instance_cache, prod.snd <$> prove_div c e₁ eβ‚‚ n₁ nβ‚‚ | _ := failed lemma pow_bit0 [monoid Ξ±] (a c' c : Ξ±) (b : β„•) (h : a ^ b = c') (hβ‚‚ : c' * c' = c) : a ^ bit0 b = c := hβ‚‚ β–Έ by simp [pow_bit0, h] lemma pow_bit1 [monoid Ξ±] (a c₁ cβ‚‚ c : Ξ±) (b : β„•) (h : a ^ b = c₁) (hβ‚‚ : c₁ * c₁ = cβ‚‚) (h₃ : cβ‚‚ * a = c) : a ^ bit1 b = c := by rw [← h₃, ← hβ‚‚]; simp [pow_bit1, h] section open match_numeral_result /-- Given `a` a rational numeral and `b : nat`, returns `(c, ⊒ a ^ b = c)`. -/ meta def prove_pow (a : expr) (na : β„š) : instance_cache β†’ expr β†’ tactic (instance_cache Γ— expr Γ— expr) | ic b := match match_numeral b with | zero := do (ic, p) ← ic.mk_app ``pow_zero [a], (ic, o) ← ic.mk_app ``has_one.one [], return (ic, o, p) | one := do (ic, p) ← ic.mk_app ``pow_one [a], return (ic, a, p) | bit0 b := do (ic, c', p) ← prove_pow ic b, nc' ← expr.to_rat c', (ic, c, pβ‚‚) ← prove_mul_rat ic c' c' nc' nc', (ic, p) ← ic.mk_app ``pow_bit0 [a, c', c, b, p, pβ‚‚], return (ic, c, p) | bit1 b := do (ic, c₁, p) ← prove_pow ic b, nc₁ ← expr.to_rat c₁, (ic, cβ‚‚, pβ‚‚) ← prove_mul_rat ic c₁ c₁ nc₁ nc₁, (ic, c, p₃) ← prove_mul_rat ic cβ‚‚ a (nc₁ * nc₁) na, (ic, p) ← ic.mk_app ``pow_bit1 [a, c₁, cβ‚‚, c, b, p, pβ‚‚, p₃], return (ic, c, p) | _ := failed end end lemma from_nat_pow (a b c : β„•) (h : @has_pow.pow _ _ monoid.has_pow a b = c) : a ^ b = c := (nat.pow_eq_pow _ _).symm.trans h /-- Evaluates expressions of the form `a ^ b`, `monoid.pow a b` or `nat.pow a b`. -/ meta def eval_pow : expr β†’ tactic (expr Γ— expr) | `(@has_pow.pow %%Ξ± _ %%m %%e₁ %%eβ‚‚) := do n₁ ← e₁.to_rat, c ← infer_type e₁ >>= mk_instance_cache, match m with | `(@monoid.has_pow %%_ %%_) := prod.snd <$> prove_pow e₁ n₁ c eβ‚‚ | `(nat.has_pow) := do (_, c, p) ← prove_pow e₁ n₁ c eβ‚‚, return (c, `(from_nat_pow).mk_app [e₁, eβ‚‚, c, p]) | _ := failed end | `(monoid.pow %%e₁ %%eβ‚‚) := do n₁ ← e₁.to_rat, c ← infer_type e₁ >>= mk_instance_cache, prod.snd <$> prove_pow e₁ n₁ c eβ‚‚ | `(nat.pow %%e₁ %%eβ‚‚) := do n₁ ← e₁.to_rat, c ← mk_instance_cache `(β„•), (_, c, p) ← prove_pow e₁ n₁ c eβ‚‚, return (c, `(from_nat_pow).mk_app [e₁, eβ‚‚, c, p]) | _ := failed theorem nonneg_pos {Ξ±} [ordered_cancel_add_comm_monoid Ξ±] (a : Ξ±) : 0 < a β†’ 0 ≀ a := le_of_lt theorem lt_one_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) (h : 1 ≀ a) : 1 < bit0 a := lt_of_lt_of_le one_lt_two (bit0_le_bit0.2 h) theorem lt_one_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) (h : 0 < a) : 1 < bit1 a := one_lt_bit1.2 h theorem lt_bit0_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a < b β†’ bit0 a < bit0 b := bit0_lt_bit0.2 theorem lt_bit0_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a ≀ b) : bit0 a < bit1 b := lt_of_le_of_lt (bit0_le_bit0.2 h) (lt_add_one _) theorem lt_bit1_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a + 1 ≀ b) : bit1 a < bit0 b := lt_of_lt_of_le (by simp [bit0, bit1, zero_lt_one, add_assoc]) (bit0_le_bit0.2 h) theorem lt_bit1_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a < b β†’ bit1 a < bit1 b := bit1_lt_bit1.2 theorem le_one_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) (h : 1 ≀ a) : 1 ≀ bit0 a := le_of_lt (lt_one_bit0 _ h) -- deliberately strong hypothesis because bit1 0 is not a numeral theorem le_one_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) (h : 0 < a) : 1 ≀ bit1 a := le_of_lt (lt_one_bit1 _ h) theorem le_bit0_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a ≀ b β†’ bit0 a ≀ bit0 b := bit0_le_bit0.2 theorem le_bit0_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a ≀ b) : bit0 a ≀ bit1 b := le_of_lt (lt_bit0_bit1 _ _ h) theorem le_bit1_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a + 1 ≀ b) : bit1 a ≀ bit0 b := le_of_lt (lt_bit1_bit0 _ _ h) theorem le_bit1_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a ≀ b β†’ bit1 a ≀ bit1 b := bit1_le_bit1.2 theorem sle_one_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) : 1 ≀ a β†’ 1 + 1 ≀ bit0 a := bit0_le_bit0.2 theorem sle_one_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a : Ξ±) : 1 ≀ a β†’ 1 + 1 ≀ bit1 a := le_bit0_bit1 _ _ theorem sle_bit0_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) : a + 1 ≀ b β†’ bit0 a + 1 ≀ bit0 b := le_bit1_bit0 _ _ theorem sle_bit0_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a ≀ b) : bit0 a + 1 ≀ bit1 b := bit1_le_bit1.2 h theorem sle_bit1_bit0 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a + 1 ≀ b) : bit1 a + 1 ≀ bit0 b := (bit1_succ a _ rfl).symm β–Έ bit0_le_bit0.2 h theorem sle_bit1_bit1 {Ξ±} [linear_ordered_semiring Ξ±] (a b : Ξ±) (h : a + 1 ≀ b) : bit1 a + 1 ≀ bit1 b := (bit1_succ a _ rfl).symm β–Έ le_bit0_bit1 _ _ h /-- Given `a` a rational numeral, returns `⊒ 0 ≀ a`. -/ meta def prove_nonneg (ic : instance_cache) : expr β†’ tactic (instance_cache Γ— expr) | e@`(has_zero.zero) := ic.mk_app ``le_refl [e] | e := if ic.Ξ± = `(β„•) then return (ic, `(nat.zero_le).mk_app [e]) else do (ic, p) ← prove_pos ic e, ic.mk_app ``nonneg_pos [e, p] section open match_numeral_result /-- Given `a` a rational numeral, returns `⊒ 1 ≀ a`. -/ meta def prove_one_le_nat (ic : instance_cache) : expr β†’ tactic (instance_cache Γ— expr) | a := match match_numeral a with | one := ic.mk_app ``le_refl [a] | bit0 a := do (ic, p) ← prove_one_le_nat a, ic.mk_app ``le_one_bit0 [a, p] | bit1 a := do (ic, p) ← prove_pos_nat ic a, ic.mk_app ``le_one_bit1 [a, p] | _ := failed end meta mutual def prove_le_nat, prove_sle_nat (ic : instance_cache) with prove_le_nat : expr β†’ expr β†’ tactic (instance_cache Γ— expr) | a b := if a = b then ic.mk_app ``le_refl [a] else match match_numeral a, match_numeral b with | zero, _ := prove_nonneg ic b | one, bit0 b := do (ic, p) ← prove_one_le_nat ic b, ic.mk_app ``le_one_bit0 [b, p] | one, bit1 b := do (ic, p) ← prove_pos_nat ic b, ic.mk_app ``le_one_bit1 [b, p] | bit0 a, bit0 b := do (ic, p) ← prove_le_nat a b, ic.mk_app ``le_bit0_bit0 [a, b, p] | bit0 a, bit1 b := do (ic, p) ← prove_le_nat a b, ic.mk_app ``le_bit0_bit1 [a, b, p] | bit1 a, bit0 b := do (ic, p) ← prove_sle_nat a b, ic.mk_app ``le_bit1_bit0 [a, b, p] | bit1 a, bit1 b := do (ic, p) ← prove_le_nat a b, ic.mk_app ``le_bit1_bit1 [a, b, p] | _, _ := failed end with prove_sle_nat : expr β†’ expr β†’ tactic (instance_cache Γ— expr) | a b := match match_numeral a, match_numeral b with | zero, _ := prove_nonneg ic b | one, bit0 b := do (ic, p) ← prove_one_le_nat ic b, ic.mk_app ``sle_one_bit0 [b, p] | one, bit1 b := do (ic, p) ← prove_one_le_nat ic b, ic.mk_app ``sle_one_bit1 [b, p] | bit0 a, bit0 b := do (ic, p) ← prove_sle_nat a b, ic.mk_app ``sle_bit0_bit0 [a, b, p] | bit0 a, bit1 b := do (ic, p) ← prove_le_nat a b, ic.mk_app ``sle_bit0_bit1 [a, b, p] | bit1 a, bit0 b := do (ic, p) ← prove_sle_nat a b, ic.mk_app ``sle_bit1_bit0 [a, b, p] | bit1 a, bit1 b := do (ic, p) ← prove_sle_nat a b, ic.mk_app ``sle_bit1_bit1 [a, b, p] | _, _ := failed end /-- Given `a`,`b` natural numerals, proves `⊒ a ≀ b`. -/ add_decl_doc prove_le_nat /-- Given `a`,`b` natural numerals, proves `⊒ a + 1 ≀ b`. -/ add_decl_doc prove_sle_nat /-- Given `a`,`b` natural numerals, proves `⊒ a < b`. -/ meta def prove_lt_nat (ic : instance_cache) : expr β†’ expr β†’ tactic (instance_cache Γ— expr) | a b := match match_numeral a, match_numeral b with | zero, _ := prove_pos ic b | one, bit0 b := do (ic, p) ← prove_one_le_nat ic b, ic.mk_app ``lt_one_bit0 [b, p] | one, bit1 b := do (ic, p) ← prove_pos_nat ic b, ic.mk_app ``lt_one_bit1 [b, p] | bit0 a, bit0 b := do (ic, p) ← prove_lt_nat a b, ic.mk_app ``lt_bit0_bit0 [a, b, p] | bit0 a, bit1 b := do (ic, p) ← prove_le_nat ic a b, ic.mk_app ``lt_bit0_bit1 [a, b, p] | bit1 a, bit0 b := do (ic, p) ← prove_sle_nat ic a b, ic.mk_app ``lt_bit1_bit0 [a, b, p] | bit1 a, bit1 b := do (ic, p) ← prove_lt_nat a b, ic.mk_app ``lt_bit1_bit1 [a, b, p] | _, _ := failed end end theorem clear_denom_lt {Ξ±} [linear_ordered_semiring Ξ±] (a a' b b' d : Ξ±) (hβ‚€ : 0 < d) (ha : a * d = a') (hb : b * d = b') (h : a' < b') : a < b := lt_of_mul_lt_mul_right (by rwa [ha, hb]) (le_of_lt hβ‚€) /-- Given `a`,`b` nonnegative rational numerals, proves `⊒ a < b`. -/ meta def prove_lt_nonneg_rat (ic : instance_cache) (a b : expr) (na nb : β„š) : tactic (instance_cache Γ— expr) := if na.denom = 1 ∧ nb.denom = 1 then prove_lt_nat ic a b else do let nd := na.denom.lcm nb.denom, (ic, d) ← ic.of_nat nd, (ic, pβ‚€) ← prove_pos ic d, (ic, a', pa) ← prove_clear_denom ic a d na nd, (ic, b', pb) ← prove_clear_denom ic b d nb nd, (ic, p) ← prove_lt_nat ic a' b', ic.mk_app ``clear_denom_lt [a, a', b, b', d, pβ‚€, pa, pb, p] lemma lt_neg_pos {Ξ±} [ordered_add_comm_group Ξ±] (a b : Ξ±) (ha : 0 < a) (hb : 0 < b) : -a < b := lt_trans (neg_neg_of_pos ha) hb /-- Given `a`,`b` rational numerals, proves `⊒ a < b`. -/ meta def prove_lt_rat (ic : instance_cache) (a b : expr) (na nb : β„š) : tactic (instance_cache Γ— expr) := match match_sign a, match_sign b with | sum.inl a, sum.inl b := do (ic, p) ← prove_lt_nonneg_rat ic a b (-na) (-nb), ic.mk_app ``neg_lt_neg [a, b, p] | sum.inl a, sum.inr ff := do (ic, p) ← prove_pos ic a, ic.mk_app ``neg_neg_of_pos [a, p] | sum.inl a, sum.inr tt := do (ic, pa) ← prove_pos ic a, (ic, pb) ← prove_pos ic b, ic.mk_app ``lt_neg_pos [a, b, pa, pb] | sum.inr ff, _ := prove_pos ic b | sum.inr tt, _ := prove_lt_nonneg_rat ic a b na nb end theorem clear_denom_le {Ξ±} [linear_ordered_semiring Ξ±] (a a' b b' d : Ξ±) (hβ‚€ : 0 < d) (ha : a * d = a') (hb : b * d = b') (h : a' ≀ b') : a ≀ b := le_of_mul_le_mul_right (by rwa [ha, hb]) hβ‚€ /-- Given `a`,`b` nonnegative rational numerals, proves `⊒ a ≀ b`. -/ meta def prove_le_nonneg_rat (ic : instance_cache) (a b : expr) (na nb : β„š) : tactic (instance_cache Γ— expr) := if na.denom = 1 ∧ nb.denom = 1 then prove_le_nat ic a b else do let nd := na.denom.lcm nb.denom, (ic, d) ← ic.of_nat nd, (ic, pβ‚€) ← prove_pos ic d, (ic, a', pa) ← prove_clear_denom ic a d na nd, (ic, b', pb) ← prove_clear_denom ic b d nb nd, (ic, p) ← prove_le_nat ic a' b', ic.mk_app ``clear_denom_le [a, a', b, b', d, pβ‚€, pa, pb, p] lemma le_neg_pos {Ξ±} [ordered_add_comm_group Ξ±] (a b : Ξ±) (ha : 0 ≀ a) (hb : 0 ≀ b) : -a ≀ b := le_trans (neg_nonpos_of_nonneg ha) hb /-- Given `a`,`b` rational numerals, proves `⊒ a ≀ b`. -/ meta def prove_le_rat (ic : instance_cache) (a b : expr) (na nb : β„š) : tactic (instance_cache Γ— expr) := match match_sign a, match_sign b with | sum.inl a, sum.inl b := do (ic, p) ← prove_le_nonneg_rat ic a b (-na) (-nb), ic.mk_app ``neg_le_neg [a, b, p] | sum.inl a, sum.inr ff := do (ic, p) ← prove_nonneg ic a, ic.mk_app ``neg_nonpos_of_nonneg [a, p] | sum.inl a, sum.inr tt := do (ic, pa) ← prove_nonneg ic a, (ic, pb) ← prove_nonneg ic b, ic.mk_app ``le_neg_pos [a, b, pa, pb] | sum.inr ff, _ := prove_nonneg ic b | sum.inr tt, _ := prove_le_nonneg_rat ic a b na nb end /-- Given `a`,`b` rational numerals, proves `⊒ a β‰  b`. This version tries to prove `⊒ a < b` or `⊒ b < a`, and so is not appropriate for types without an order relation. -/ meta def prove_ne_rat (ic : instance_cache) (a b : expr) (na nb : β„š) : tactic (instance_cache Γ— expr) := if na < nb then do (ic, p) ← prove_lt_rat ic a b na nb, ic.mk_app ``ne_of_lt [a, b, p] else do (ic, p) ← prove_lt_rat ic b a nb na, ic.mk_app ``ne_of_gt [a, b, p] theorem nat_cast_zero {Ξ±} [semiring Ξ±] : ↑(0 : β„•) = (0 : Ξ±) := nat.cast_zero theorem nat_cast_one {Ξ±} [semiring Ξ±] : ↑(1 : β„•) = (1 : Ξ±) := nat.cast_one theorem nat_cast_bit0 {Ξ±} [semiring Ξ±] (a : β„•) (a' : Ξ±) (h : ↑a = a') : ↑(bit0 a) = bit0 a' := h β–Έ nat.cast_bit0 _ theorem nat_cast_bit1 {Ξ±} [semiring Ξ±] (a : β„•) (a' : Ξ±) (h : ↑a = a') : ↑(bit1 a) = bit1 a' := h β–Έ nat.cast_bit1 _ theorem int_cast_zero {Ξ±} [ring Ξ±] : ↑(0 : β„€) = (0 : Ξ±) := int.cast_zero theorem int_cast_one {Ξ±} [ring Ξ±] : ↑(1 : β„€) = (1 : Ξ±) := int.cast_one theorem int_cast_bit0 {Ξ±} [ring Ξ±] (a : β„€) (a' : Ξ±) (h : ↑a = a') : ↑(bit0 a) = bit0 a' := h β–Έ int.cast_bit0 _ theorem int_cast_bit1 {Ξ±} [ring Ξ±] (a : β„€) (a' : Ξ±) (h : ↑a = a') : ↑(bit1 a) = bit1 a' := h β–Έ int.cast_bit1 _ theorem rat_cast_bit0 {Ξ±} [division_ring Ξ±] [char_zero Ξ±] (a : β„š) (a' : Ξ±) (h : ↑a = a') : ↑(bit0 a) = bit0 a' := h β–Έ rat.cast_bit0 _ theorem rat_cast_bit1 {Ξ±} [division_ring Ξ±] [char_zero Ξ±] (a : β„š) (a' : Ξ±) (h : ↑a = a') : ↑(bit1 a) = bit1 a' := h β–Έ rat.cast_bit1 _ /-- Given `a' : Ξ±` a natural numeral, returns `(a : β„•, ⊒ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_nat_uncast (ic nc : instance_cache) : βˆ€ (a' : expr), tactic (instance_cache Γ— instance_cache Γ— expr Γ— expr) | a' := match match_numeral a' with | match_numeral_result.zero := do (nc, e) ← nc.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``nat_cast_zero [], return (ic, nc, e, p) | match_numeral_result.one := do (nc, e) ← nc.mk_app ``has_one.one [], (ic, p) ← ic.mk_app ``nat_cast_one [], return (ic, nc, e, p) | match_numeral_result.bit0 a' := do (ic, nc, a, p) ← prove_nat_uncast a', (nc, a0) ← nc.mk_bit0 a, (ic, p) ← ic.mk_app ``nat_cast_bit0 [a, a', p], return (ic, nc, a0, p) | match_numeral_result.bit1 a' := do (ic, nc, a, p) ← prove_nat_uncast a', (nc, a1) ← nc.mk_bit1 a, (ic, p) ← ic.mk_app ``nat_cast_bit1 [a, a', p], return (ic, nc, a1, p) | _ := failed end /-- Given `a' : Ξ±` a natural numeral, returns `(a : β„€, ⊒ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_int_uncast_nat (ic zc : instance_cache) : βˆ€ (a' : expr), tactic (instance_cache Γ— instance_cache Γ— expr Γ— expr) | a' := match match_numeral a' with | match_numeral_result.zero := do (zc, e) ← zc.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``int_cast_zero [], return (ic, zc, e, p) | match_numeral_result.one := do (zc, e) ← zc.mk_app ``has_one.one [], (ic, p) ← ic.mk_app ``int_cast_one [], return (ic, zc, e, p) | match_numeral_result.bit0 a' := do (ic, zc, a, p) ← prove_int_uncast_nat a', (zc, a0) ← zc.mk_bit0 a, (ic, p) ← ic.mk_app ``int_cast_bit0 [a, a', p], return (ic, zc, a0, p) | match_numeral_result.bit1 a' := do (ic, zc, a, p) ← prove_int_uncast_nat a', (zc, a1) ← zc.mk_bit1 a, (ic, p) ← ic.mk_app ``int_cast_bit1 [a, a', p], return (ic, zc, a1, p) | _ := failed end /-- Given `a' : Ξ±` a natural numeral, returns `(a : β„š, ⊒ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_rat_uncast_nat (ic qc : instance_cache) (cz_inst : expr) : βˆ€ (a' : expr), tactic (instance_cache Γ— instance_cache Γ— expr Γ— expr) | a' := match match_numeral a' with | match_numeral_result.zero := do (qc, e) ← qc.mk_app ``has_zero.zero [], (ic, p) ← ic.mk_app ``rat.cast_zero [cz_inst], return (ic, qc, e, p) | match_numeral_result.one := do (qc, e) ← qc.mk_app ``has_one.one [], (ic, p) ← ic.mk_app ``rat.cast_one [], return (ic, qc, e, p) | match_numeral_result.bit0 a' := do (ic, qc, a, p) ← prove_rat_uncast_nat a', (qc, a0) ← qc.mk_bit0 a, (ic, p) ← ic.mk_app ``rat_cast_bit0 [cz_inst, a, a', p], return (ic, qc, a0, p) | match_numeral_result.bit1 a' := do (ic, qc, a, p) ← prove_rat_uncast_nat a', (qc, a1) ← qc.mk_bit1 a, (ic, p) ← ic.mk_app ``rat_cast_bit1 [cz_inst, a, a', p], return (ic, qc, a1, p) | _ := failed end theorem rat_cast_div {Ξ±} [division_ring Ξ±] [char_zero Ξ±] (a b : β„š) (a' b' : Ξ±) (ha : ↑a = a') (hb : ↑b = b') : ↑(a / b) = a' / b' := ha β–Έ hb β–Έ rat.cast_div _ _ /-- Given `a' : Ξ±` a nonnegative rational numeral, returns `(a : β„š, ⊒ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_rat_uncast_nonneg (ic qc : instance_cache) (cz_inst a' : expr) (na' : β„š) : tactic (instance_cache Γ— instance_cache Γ— expr Γ— expr) := if na'.denom = 1 then prove_rat_uncast_nat ic qc cz_inst a' else do [_, _, a', b'] ← return a'.get_app_args, (ic, qc, a, pa) ← prove_rat_uncast_nat ic qc cz_inst a', (ic, qc, b, pb) ← prove_rat_uncast_nat ic qc cz_inst b', (qc, e) ← qc.mk_app ``has_div.div [a, b], (ic, p) ← ic.mk_app ``rat_cast_div [cz_inst, a, b, a', b', pa, pb], return (ic, qc, e, p) theorem int_cast_neg {Ξ±} [ring Ξ±] (a : β„€) (a' : Ξ±) (h : ↑a = a') : ↑-a = -a' := h β–Έ int.cast_neg _ theorem rat_cast_neg {Ξ±} [division_ring Ξ±] (a : β„š) (a' : Ξ±) (h : ↑a = a') : ↑-a = -a' := h β–Έ rat.cast_neg _ /-- Given `a' : Ξ±` an integer numeral, returns `(a : β„€, ⊒ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_int_uncast (ic zc : instance_cache) (a' : expr) : tactic (instance_cache Γ— instance_cache Γ— expr Γ— expr) := match match_neg a' with | some a' := do (ic, zc, a, p) ← prove_int_uncast_nat ic zc a', (zc, e) ← zc.mk_app ``has_neg.neg [a], (ic, p) ← ic.mk_app ``int_cast_neg [a, a', p], return (ic, zc, e, p) | none := prove_int_uncast_nat ic zc a' end /-- Given `a' : Ξ±` a rational numeral, returns `(a : β„š, ⊒ ↑a = a')`. (Note that the returned value is on the left of the equality.) -/ meta def prove_rat_uncast (ic qc : instance_cache) (cz_inst a' : expr) (na' : β„š) : tactic (instance_cache Γ— instance_cache Γ— expr Γ— expr) := match match_neg a' with | some a' := do (ic, qc, a, p) ← prove_rat_uncast_nonneg ic qc cz_inst a' (-na'), (qc, e) ← qc.mk_app ``has_neg.neg [a], (ic, p) ← ic.mk_app ``rat_cast_neg [a, a', p], return (ic, qc, e, p) | none := prove_rat_uncast_nonneg ic qc cz_inst a' na' end theorem nat_cast_ne {Ξ±} [semiring Ξ±] [char_zero Ξ±] (a b : β„•) (a' b' : Ξ±) (ha : ↑a = a') (hb : ↑b = b') (h : a β‰  b) : a' β‰  b' := ha β–Έ hb β–Έ mt nat.cast_inj.1 h theorem int_cast_ne {Ξ±} [ring Ξ±] [char_zero Ξ±] (a b : β„€) (a' b' : Ξ±) (ha : ↑a = a') (hb : ↑b = b') (h : a β‰  b) : a' β‰  b' := ha β–Έ hb β–Έ mt int.cast_inj.1 h theorem rat_cast_ne {Ξ±} [division_ring Ξ±] [char_zero Ξ±] (a b : β„š) (a' b' : Ξ±) (ha : ↑a = a') (hb : ↑b = b') (h : a β‰  b) : a' β‰  b' := ha β–Έ hb β–Έ mt rat.cast_inj.1 h /-- Given `a`,`b` rational numerals, proves `⊒ a β‰  b`. Currently it tries two methods: * Prove `⊒ a < b` or `⊒ b < a`, if the base type has an order * Embed `↑(a':β„š) = a` and `↑(b':β„š) = b`, and then prove `a' β‰  b'`. This requires that the base type be `char_zero`, and also that it be a `division_ring` so that the coercion from `β„š` is well defined. We may also add coercions to `β„€` and `β„•` as well in order to support `char_zero` rings and semirings. -/ meta def prove_ne : instance_cache β†’ expr β†’ expr β†’ β„š β†’ β„š β†’ tactic (instance_cache Γ— expr) | ic a b na nb := prove_ne_rat ic a b na nb <|> do cz_inst ← mk_mapp ``char_zero [ic.Ξ±, none, none] >>= mk_instance, if na.denom = 1 ∧ nb.denom = 1 then if na β‰₯ 0 ∧ nb β‰₯ 0 then do guard (ic.Ξ± β‰  `(β„•)), nc ← mk_instance_cache `(β„•), (ic, nc, a', pa) ← prove_nat_uncast ic nc a, (ic, nc, b', pb) ← prove_nat_uncast ic nc b, (nc, p) ← prove_ne_rat nc a' b' na nb, ic.mk_app ``nat_cast_ne [cz_inst, a', b', a, b, pa, pb, p] else do guard (ic.Ξ± β‰  `(β„€)), zc ← mk_instance_cache `(β„€), (ic, zc, a', pa) ← prove_int_uncast ic zc a, (ic, zc, b', pb) ← prove_int_uncast ic zc b, (zc, p) ← prove_ne_rat zc a' b' na nb, ic.mk_app ``int_cast_ne [cz_inst, a', b', a, b, pa, pb, p] else do guard (ic.Ξ± β‰  `(β„š)), qc ← mk_instance_cache `(β„š), (ic, qc, a', pa) ← prove_rat_uncast ic qc cz_inst a na, (ic, qc, b', pb) ← prove_rat_uncast ic qc cz_inst b nb, (qc, p) ← prove_ne_rat qc a' b' na nb, ic.mk_app ``rat_cast_ne [cz_inst, a', b', a, b, pa, pb, p] /-- Given `∣- p`, returns `(true, ⊒ p = true)`. -/ meta def true_intro (p : expr) : tactic (expr Γ— expr) := prod.mk `(true) <$> mk_app ``eq_true_intro [p] /-- Given `∣- Β¬ p`, returns `(false, ⊒ p = false)`. -/ meta def false_intro (p : expr) : tactic (expr Γ— expr) := prod.mk `(false) <$> mk_app ``eq_false_intro [p] theorem not_refl_false_intro {Ξ±} (a : Ξ±) : (a β‰  a) = false := eq_false_intro $ not_not_intro rfl /-- Evaluates the inequality operations `=`,`<`,`>`,`≀`,`β‰₯`,`β‰ ` on numerals. -/ meta def eval_ineq : expr β†’ tactic (expr Γ— expr) | `(%%e₁ < %%eβ‚‚) := do n₁ ← e₁.to_rat, nβ‚‚ ← eβ‚‚.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ < nβ‚‚ then do (_, p) ← prove_lt_rat c e₁ eβ‚‚ n₁ nβ‚‚, true_intro p else if n₁ = nβ‚‚ then do (_, p) ← c.mk_app ``lt_irrefl [e₁], false_intro p else do (c, p') ← prove_lt_rat c eβ‚‚ e₁ nβ‚‚ n₁, (_, p) ← c.mk_app ``not_lt_of_gt [e₁, eβ‚‚, p'], false_intro p | `(%%e₁ ≀ %%eβ‚‚) := do n₁ ← e₁.to_rat, nβ‚‚ ← eβ‚‚.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ ≀ nβ‚‚ then do (_, p) ← if n₁ = nβ‚‚ then c.mk_app ``le_refl [e₁] else prove_le_rat c e₁ eβ‚‚ n₁ nβ‚‚, true_intro p else do (c, p) ← prove_lt_rat c eβ‚‚ e₁ nβ‚‚ n₁, (_, p) ← c.mk_app ``not_le_of_gt [e₁, eβ‚‚, p], false_intro p | `(%%e₁ = %%eβ‚‚) := do n₁ ← e₁.to_rat, nβ‚‚ ← eβ‚‚.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ = nβ‚‚ then mk_eq_refl e₁ >>= true_intro else do (_, p) ← prove_ne c e₁ eβ‚‚ n₁ nβ‚‚, false_intro p | `(%%e₁ > %%eβ‚‚) := mk_app ``has_lt.lt [eβ‚‚, e₁] >>= eval_ineq | `(%%e₁ β‰₯ %%eβ‚‚) := mk_app ``has_le.le [eβ‚‚, e₁] >>= eval_ineq | `(%%e₁ β‰  %%eβ‚‚) := do n₁ ← e₁.to_rat, nβ‚‚ ← eβ‚‚.to_rat, c ← infer_type e₁ >>= mk_instance_cache, if n₁ = nβ‚‚ then prod.mk `(false) <$> mk_app ``not_refl_false_intro [e₁] else do (_, p) ← prove_ne c e₁ eβ‚‚ n₁ nβ‚‚, true_intro p | _ := failed theorem nat_succ_eq (a b c : β„•) (h₁ : a = b) (hβ‚‚ : b + 1 = c) : nat.succ a = c := by rwa h₁ /-- Evaluates the expression `nat.succ ... (nat.succ n)` where `n` is a natural numeral. (We could also just handle `nat.succ n` here and rely on `simp` to work bottom up, but we figure that towers of successors coming from e.g. `induction` are a common case.) -/ meta def prove_nat_succ (ic : instance_cache) : expr β†’ tactic (instance_cache Γ— β„• Γ— expr Γ— expr) | `(nat.succ %%a) := do (ic, n, b, p₁) ← prove_nat_succ a, let n' := n + 1, (ic, c) ← ic.of_nat n', (ic, pβ‚‚) ← prove_add_nat ic b `(1) c, return (ic, n', c, `(nat_succ_eq).mk_app [a, b, c, p₁, pβ‚‚]) | e := do n ← e.to_nat, p ← mk_eq_refl e, return (ic, n, e, p) lemma nat_div (a b q r m : β„•) (hm : q * b = m) (h : r + m = a) (hβ‚‚ : r < b) : a / b = q := by rw [← h, ← hm, nat.add_mul_div_right _ _ (lt_of_le_of_lt (nat.zero_le _) hβ‚‚), nat.div_eq_of_lt hβ‚‚, zero_add] lemma int_div (a b q r m : β„€) (hm : q * b = m) (h : r + m = a) (h₁ : 0 ≀ r) (hβ‚‚ : r < b) : a / b = q := by rw [← h, ← hm, int.add_mul_div_right _ _ (ne_of_gt (lt_of_le_of_lt h₁ hβ‚‚)), int.div_eq_zero_of_lt h₁ hβ‚‚, zero_add] lemma nat_mod (a b q r m : β„•) (hm : q * b = m) (h : r + m = a) (hβ‚‚ : r < b) : a % b = r := by rw [← h, ← hm, nat.add_mul_mod_self_right, nat.mod_eq_of_lt hβ‚‚] lemma int_mod (a b q r m : β„€) (hm : q * b = m) (h : r + m = a) (h₁ : 0 ≀ r) (hβ‚‚ : r < b) : a % b = r := by rw [← h, ← hm, int.add_mul_mod_self, int.mod_eq_of_lt h₁ hβ‚‚] lemma int_div_neg (a b c' c : β„€) (h : a / b = c') (hβ‚‚ : -c' = c) : a / -b = c := hβ‚‚ β–Έ h β–Έ int.div_neg _ _ lemma int_mod_neg (a b c : β„€) (h : a % b = c) : a % -b = c := (int.mod_neg _ _).trans h /-- Given `a`,`b` numerals in `nat` or `int`, * `prove_div_mod ic a b ff` returns `(c, ⊒ a / b = c)` * `prove_div_mod ic a b tt` returns `(c, ⊒ a % b = c)` -/ meta def prove_div_mod (ic : instance_cache) : expr β†’ expr β†’ bool β†’ tactic (instance_cache Γ— expr Γ— expr) | a b mod := match match_neg b with | some b := do (ic, c', p) ← prove_div_mod a b mod, if mod then return (ic, c', `(int_mod_neg).mk_app [a, b, c', p]) else do (ic, c, pβ‚‚) ← prove_neg ic c', return (ic, c, `(int_div_neg).mk_app [a, b, c', c, p, pβ‚‚]) | none := do nb ← b.to_nat, na ← a.to_int, let nq := na / nb, let nr := na % nb, let nm := nq * nr, (ic, q) ← ic.of_int nq, (ic, r) ← ic.of_int nr, (ic, m, pm) ← prove_mul_rat ic q b (rat.of_int nq) (rat.of_int nb), (ic, p) ← prove_add_rat ic r m a (rat.of_int nr) (rat.of_int nm) (rat.of_int na), (ic, p') ← prove_lt_nat ic r b, if ic.Ξ± = `(nat) then if mod then return (ic, r, `(nat_mod).mk_app [a, b, q, r, m, pm, p, p']) else return (ic, q, `(nat_div).mk_app [a, b, q, r, m, pm, p, p']) else if ic.Ξ± = `(int) then do (ic, pβ‚€) ← prove_nonneg ic r, if mod then return (ic, r, `(int_mod).mk_app [a, b, q, r, m, pm, p, pβ‚€, p']) else return (ic, q, `(int_div).mk_app [a, b, q, r, m, pm, p, pβ‚€, p']) else failed end theorem dvd_eq_nat (a b c : β„•) (p) (h₁ : b % a = c) (hβ‚‚ : (c = 0) = p) : (a ∣ b) = p := (propext $ by rw [← h₁, nat.dvd_iff_mod_eq_zero]).trans hβ‚‚ theorem dvd_eq_int (a b c : β„€) (p) (h₁ : b % a = c) (hβ‚‚ : (c = 0) = p) : (a ∣ b) = p := (propext $ by rw [← h₁, int.dvd_iff_mod_eq_zero]).trans hβ‚‚ /-- Evaluates some extra numeric operations on `nat` and `int`, specifically `nat.succ`, `/` and `%`, and `∣` (divisibility). -/ meta def eval_nat_int_ext : expr β†’ tactic (expr Γ— expr) | e@`(nat.succ _) := do ic ← mk_instance_cache `(β„•), (_, _, ep) ← prove_nat_succ ic e, return ep | `(%%a / %%b) := do c ← infer_type a >>= mk_instance_cache, prod.snd <$> prove_div_mod c a b ff | `(%%a % %%b) := do c ← infer_type a >>= mk_instance_cache, prod.snd <$> prove_div_mod c a b tt | `(%%a ∣ %%b) := do Ξ± ← infer_type a, ic ← mk_instance_cache Ξ±, th ← if Ξ± = `(nat) then return (`(dvd_eq_nat):expr) else if Ξ± = `(int) then return `(dvd_eq_int) else failed, (ic, c, p₁) ← prove_div_mod ic b a tt, (ic, z) ← ic.mk_app ``has_zero.zero [], (e', pβ‚‚) ← mk_app ``eq [c, z] >>= eval_ineq, return (e', th.mk_app [a, b, c, e', p₁, pβ‚‚]) | _ := failed lemma not_prime_helper (a b n : β„•) (h : a * b = n) (h₁ : 1 < a) (hβ‚‚ : 1 < b) : Β¬ nat.prime n := by rw ← h; exact nat.not_prime_mul h₁ hβ‚‚ lemma is_prime_helper (n : β„•) (h₁ : 1 < n) (hβ‚‚ : nat.min_fac n = n) : nat.prime n := nat.prime_def_min_fac.2 ⟨h₁, hβ‚‚βŸ© lemma min_fac_bit0 (n : β„•) : nat.min_fac (bit0 n) = 2 := by simp [nat.min_fac_eq, show 2 ∣ bit0 n, by simp [bit0_eq_two_mul n]] /-- A predicate representing partial progress in a proof of `min_fac`. -/ def min_fac_helper (n k : β„•) : Prop := 0 < k ∧ bit1 k ≀ nat.min_fac (bit1 n) theorem min_fac_helper.n_pos {n k : β„•} (h : min_fac_helper n k) : 0 < n := nat.pos_iff_ne_zero.2 $ Ξ» e, by rw e at h; exact not_le_of_lt (nat.bit1_lt h.1) h.2 lemma min_fac_ne_bit0 {n k : β„•} : nat.min_fac (bit1 n) β‰  bit0 k := by rw bit0_eq_two_mul; exact Ξ» e, absurd ((nat.dvd_add_iff_right (by simp [bit0_eq_two_mul n])).2 (dvd_trans ⟨_, e⟩ (nat.min_fac_dvd _))) dec_trivial lemma min_fac_helper_0 (n : β„•) (h : 0 < n) : min_fac_helper n 1 := begin refine ⟨zero_lt_one, lt_of_le_of_ne _ min_fac_ne_bit0.symm⟩, refine @lt_of_le_of_ne β„• _ _ _ (nat.min_fac_pos _) _, intro e, have := nat.min_fac_prime _, { rw ← e at this, exact nat.not_prime_one this }, { exact ne_of_gt (nat.bit1_lt h) } end lemma min_fac_helper_1 {n k k' : β„•} (e : k + 1 = k') (np : nat.min_fac (bit1 n) β‰  bit1 k) (h : min_fac_helper n k) : min_fac_helper n k' := begin rw ← e, refine ⟨nat.succ_pos _, (lt_of_le_of_ne (lt_of_le_of_ne _ _ : k+1+k < _) min_fac_ne_bit0.symm : bit0 (k+1) < _)⟩, { rw add_right_comm, exact h.2 }, { rw add_right_comm, exact np.symm } end lemma min_fac_helper_2 (n k k' : β„•) (e : k + 1 = k') (np : Β¬ nat.prime (bit1 k)) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, intro e₁, rw ← e₁ at np, exact np (nat.min_fac_prime $ ne_of_gt $ nat.bit1_lt h.n_pos) end lemma min_fac_helper_3 (n k k' c : β„•) (e : k + 1 = k') (nc : bit1 n % bit1 k = c) (c0 : 0 < c) (h : min_fac_helper n k) : min_fac_helper n k' := begin refine min_fac_helper_1 e _ h, refine mt _ (ne_of_gt c0), intro e₁, rw [← nc, ← nat.dvd_iff_mod_eq_zero, ← e₁], apply nat.min_fac_dvd end lemma min_fac_helper_4 (n k : β„•) (hd : bit1 n % bit1 k = 0) (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 k := by rw ← nat.dvd_iff_mod_eq_zero at hd; exact le_antisymm (nat.min_fac_le_of_dvd (nat.bit1_lt h.1) hd) h.2 lemma min_fac_helper_5 (n k k' : β„•) (e : bit1 k * bit1 k = k') (hd : bit1 n < k') (h : min_fac_helper n k) : nat.min_fac (bit1 n) = bit1 n := begin refine (nat.prime_def_min_fac.1 (nat.prime_def_le_sqrt.2 ⟨nat.bit1_lt h.n_pos, _⟩)).2, rw ← e at hd, intros m m2 hm md, have := le_trans h.2 (le_trans (nat.min_fac_le_of_dvd m2 md) hm), rw nat.le_sqrt at this, exact not_le_of_lt hd this end /-- Given `e` a natural numeral and `d : nat` a factor of it, return `⊒ Β¬ prime e`. -/ meta def prove_non_prime (e : expr) (n d₁ : β„•) : tactic expr := do let e₁ := reflect d₁, c ← mk_instance_cache `(nat), (c, p₁) ← prove_lt_nat c `(1) e₁, let dβ‚‚ := n / d₁, let eβ‚‚ := reflect dβ‚‚, (c, e', p) ← prove_mul_nat c e₁ eβ‚‚, guard (e' =ₐ e), (c, pβ‚‚) ← prove_lt_nat c `(1) eβ‚‚, return $ `(not_prime_helper).mk_app [e₁, eβ‚‚, e, p, p₁, pβ‚‚] /-- Given `a`,`a1 := bit1 a`, `n1` the value of `a1`, `b` and `p : min_fac_helper a b`, returns `(c, ⊒ min_fac a1 = c)`. -/ meta def prove_min_fac_aux (a a1 : expr) (n1 : β„•) : instance_cache β†’ expr β†’ expr β†’ tactic (instance_cache Γ— expr Γ— expr) | ic b p := do k ← b.to_nat, let k1 := bit1 k, let b1 := `(bit1:β„•β†’β„•).mk_app [b], if n1 < k1*k1 then do (ic, e', p₁) ← prove_mul_nat ic b1 b1, (ic, pβ‚‚) ← prove_lt_nat ic a1 e', return (ic, a1, `(min_fac_helper_5).mk_app [a, b, e', p₁, pβ‚‚, p]) else let d := k1.min_fac in if to_bool (d < k1) then do let k' := k+1, let e' := reflect k', (ic, p₁) ← prove_succ ic b e', pβ‚‚ ← prove_non_prime b1 k1 d, prove_min_fac_aux ic e' $ `(min_fac_helper_2).mk_app [a, b, e', p₁, pβ‚‚, p] else do let nc := n1 % k1, (ic, c, pc) ← prove_div_mod ic a1 b1 tt, if nc = 0 then return (ic, b1, `(min_fac_helper_4).mk_app [a, b, pc, p]) else do (ic, pβ‚€) ← prove_pos ic c, let k' := k+1, let e' := reflect k', (ic, p₁) ← prove_succ ic b e', prove_min_fac_aux ic e' $ `(min_fac_helper_3).mk_app [a, b, e', c, p₁, pc, pβ‚€, p] /-- Given `a` a natural numeral, returns `(b, ⊒ min_fac a = b)`. -/ meta def prove_min_fac (ic : instance_cache) (e : expr) : tactic (instance_cache Γ— expr Γ— expr) := match match_numeral e with | match_numeral_result.zero := return (ic, `(2:β„•), `(nat.min_fac_zero)) | match_numeral_result.one := return (ic, `(1:β„•), `(nat.min_fac_one)) | match_numeral_result.bit0 e := return (ic, `(2), `(min_fac_bit0).mk_app [e]) | match_numeral_result.bit1 e := do n ← e.to_nat, c ← mk_instance_cache `(nat), (c, p) ← prove_pos c e, let a1 := `(bit1:β„•β†’β„•).mk_app [e], prove_min_fac_aux e a1 (bit1 n) c `(1) (`(min_fac_helper_0).mk_app [e, p]) | _ := failed end /-- Evaluates the `prime` and `min_fac` functions. -/ meta def eval_prime : expr β†’ tactic (expr Γ— expr) | `(nat.prime %%e) := do n ← e.to_nat, match n with | 0 := false_intro `(nat.not_prime_zero) | 1 := false_intro `(nat.not_prime_one) | _ := let d₁ := n.min_fac in if d₁ < n then prove_non_prime e n d₁ >>= false_intro else do let e₁ := reflect d₁, c ← mk_instance_cache `(nat), (c, p₁) ← prove_lt_nat c `(1) e₁, (c, e₁, p) ← prove_min_fac c e, true_intro $ `(is_prime_helper).mk_app [e, p₁, p] end | `(nat.min_fac %%e) := do ic ← mk_instance_cache `(β„•), prod.snd <$> prove_min_fac ic e | _ := failed /-- This version of `derive` does not fail when the input is already a numeral -/ meta def derive' (e : expr) : tactic (expr Γ— expr) := eval_field e <|> eval_nat_int_ext e <|> eval_pow e <|> eval_ineq e <|> eval_prime e meta def derive : expr β†’ tactic (expr Γ— expr) | e := do e ← instantiate_mvars e, (_, e', pr) ← ext_simplify_core () {} simp_lemmas.mk (Ξ» _, failed) (Ξ» _ _ _ _ _, failed) (Ξ» _ _ _ _ e, do (new_e, pr) ← derive' e, guard (Β¬ new_e =ₐ e), return ((), new_e, some pr, tt)) `eq e, return (e', pr) end norm_num namespace tactic.interactive open norm_num interactive interactive.types /-- Basic version of `norm_num` that does not call `simp`. -/ meta def norm_num1 (loc : parse location) : tactic unit := do ns ← loc.get_locals, tt ← tactic.replace_at derive ns loc.include_goal | fail "norm_num failed to simplify", when loc.include_goal $ try tactic.triv, when (Β¬ ns.empty) $ try tactic.contradiction /-- Normalize numerical expressions. Supports the operations `+` `-` `*` `/` `^` and `%` over numerical types such as `β„•`, `β„€`, `β„š`, `ℝ`, `β„‚` and some general algebraic types, and can prove goals of the form `A = B`, `A β‰  B`, `A < B` and `A ≀ B`, where `A` and `B` are numerical expressions. It also has a relatively simple primality prover. -/ meta def norm_num (hs : parse simp_arg_list) (l : parse location) : tactic unit := repeat1 $ orelse' (norm_num1 l) $ simp_core {} (norm_num1 (loc.ns [none])) ff hs [] l add_hint_tactic "norm_num" /-- Normalizes a numerical expression and tries to close the goal with the result. -/ meta def apply_normed (x : parse texpr) : tactic unit := do x₁ ← to_expr x, (xβ‚‚,_) ← derive x₁, tactic.exact xβ‚‚ /-- Normalises numerical expressions. It supports the operations `+` `-` `*` `/` `^` and `%` over numerical types such as `β„•`, `β„€`, `β„š`, `ℝ`, `β„‚`, and can prove goals of the form `A = B`, `A β‰  B`, `A < B` and `A ≀ B`, where `A` and `B` are numerical expressions. It also has a relatively simple primality prover. ```lean import data.real.basic example : (2 : ℝ) + 2 = 4 := by norm_num example : (12345.2 : ℝ) β‰  12345.3 := by norm_num example : (73 : ℝ) < 789/2 := by norm_num example : 123456789 + 987654321 = 1111111110 := by norm_num example (R : Type*) [ring R] : (2 : R) + 2 = 4 := by norm_num example (F : Type*) [linear_ordered_field F] : (2 : F) + 2 < 5 := by norm_num example : nat.prime (2^13 - 1) := by norm_num example : Β¬ nat.prime (2^11 - 1) := by norm_num example (x : ℝ) (h : x = 123 + 456) : x = 579 := by norm_num at h; assumption ``` The variant `norm_num1` does not call `simp`. Both `norm_num` and `norm_num1` can be called inside the `conv` tactic. The tactic `apply_normed` normalises a numerical expression and tries to close the goal with the result. Compare: ```lean def a : β„• := 2^100 #print a -- 2 ^ 100 def normed_a : β„• := by apply_normed 2^100 #print normed_a -- 1267650600228229401496703205376 ``` -/ add_tactic_doc { name := "norm_num", category := doc_category.tactic, decl_names := [`tactic.interactive.norm_num1, `tactic.interactive.norm_num, `tactic.interactive.apply_normed], tags := ["arithmetic", "decision procedure"] } end tactic.interactive namespace conv.interactive open conv interactive tactic.interactive open norm_num (derive) /-- Basic version of `norm_num` that does not call `simp`. -/ meta def norm_num1 : conv unit := replace_lhs derive /-- Normalize numerical expressions. Supports the operations `+` `-` `*` `/` `^` and `%` over numerical types such as `β„•`, `β„€`, `β„š`, `ℝ`, `β„‚` and some general algebraic types, and can prove goals of the form `A = B`, `A β‰  B`, `A < B` and `A ≀ B`, where `A` and `B` are numerical expressions. It also has a relatively simple primality prover. -/ meta def norm_num (hs : parse simp_arg_list) : conv unit := repeat1 $ orelse' norm_num1 $ conv.interactive.simp ff hs [] { discharger := tactic.interactive.norm_num1 (loc.ns [none]) } end conv.interactive
350de6425e0b6f88249969fea819b333d32b9aa9
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/number_theory/primes_congruent_one_auto.lean
e0439a56fccc58b9dafe7c8237d11c030e9c5c4f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,170
lean
/- Copyright (c) 2020 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Riccardo Brasca -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.polynomial.cyclotomic import Mathlib.topology.algebra.polynomial import Mathlib.field_theory.finite.basic import Mathlib.PostPort namespace Mathlib /-! # Primes congruent to one We prove that, for any positive `k : β„•`, there are infinitely many primes `p` such that `p ≑ 1 [MOD k]`. -/ namespace nat /-- For any positive `k : β„•` there are infinitely many primes `p` such that `p ≑ 1 [MOD k]`. -/ theorem exists_prime_ge_modeq_one (k : β„•) (n : β„•) (hpos : 0 < k) : βˆƒ (p : β„•), prime p ∧ n ≀ p ∧ modeq k p 1 := sorry theorem frequently_at_top_modeq_one (k : β„•) (hpos : 0 < k) : filter.frequently (fun (p : β„•) => prime p ∧ modeq k p 1) filter.at_top := sorry theorem infinite_set_of_prime_modeq_one (k : β„•) (hpos : 0 < k) : set.infinite (set_of fun (p : β„•) => prime p ∧ modeq k p 1) := iff.mp frequently_at_top_iff_infinite (frequently_at_top_modeq_one k hpos) end Mathlib
9c48bb0db9c556a82e01a7e1177381675516f98d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/measure/regular.lean
3b2ffe59991688650d1d8977bfd1c5775f44a742
[ "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
33,788
lean
/- Copyright (c) 2021 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris Van Doorn, Yury Kudryashov -/ import measure_theory.constructions.borel_space /-! # Regular measures A measure is `outer_regular` if the measure of any measurable set `A` is the infimum of `ΞΌ U` over all open sets `U` containing `A`. A measure is `regular` if it satisfies the following properties: * it is finite on compact sets; * it is outer regular; * it is inner regular for open sets with respect to compacts sets: the measure of any open set `U` is the supremum of `ΞΌ K` over all compact sets `K` contained in `U`. A measure is `weakly_regular` if it satisfies the following properties: * it is outer regular; * it is inner regular for open sets with respect to closed sets: the measure of any open set `U` is the supremum of `ΞΌ F` over all closed sets `F` contained in `U`. In a Hausdorff topological space, regularity implies weak regularity. These three conditions are registered as typeclasses for a measure `ΞΌ`, and this implication is recorded as an instance. In order to avoid code duplication, we also define a measure `ΞΌ` to be `inner_regular` for sets satisfying a predicate `q` with respect to sets satisfying a predicate `p` if for any set `U ∈ {U | q U}` and a number `r < ΞΌ U` there exists `F βŠ† U` such that `p F` and `r < ΞΌ F`. We prove that inner regularity for open sets with respect to compact sets or closed sets implies inner regularity for all measurable sets of finite measure (with respect to compact sets or closed sets respectively), and register some corollaries for (weakly) regular measures. Note that a similar statement for measurable sets of infinite mass can fail. For a counterexample, consider the group `ℝ Γ— ℝ` where the first factor has the discrete topology and the second one the usual topology. It is a locally compact Hausdorff topological group, with Haar measure equal to Lebesgue measure on each vertical fiber. The set `ℝ Γ— {0}` has infinite measure (by outer regularity), but any compact set it contains has zero measure (as it is finite). Several authors require as a definition of regularity that all measurable sets are inner regular. We have opted for the slightly weaker definition above as it holds for all Haar measures, it is enough for essentially all applications, and it is equivalent to the other definition when the measure is finite. The interest of the notion of weak regularity is that it is enough for many applications, and it is automatically satisfied by any finite measure on a metric space. ## Main definitions * `measure_theory.measure.outer_regular ΞΌ`: a typeclass registering that a measure `ΞΌ` on a topological space is outer regular. * `measure_theory.measure.regular ΞΌ`: a typeclass registering that a measure `ΞΌ` on a topological space is regular. * `measure_theory.measure.weakly_regular ΞΌ`: a typeclass registering that a measure `ΞΌ` on a topological space is weakly regular. * `measure_theory.measure.inner_regular ΞΌ p q`: a non-typeclass predicate saying that a measure `ΞΌ` is inner regular for sets satisfying `q` with respect to sets satisfying `p`. ## Main results ### Outer regular measures * `set.measure_eq_infi_is_open` asserts that, when `ΞΌ` is outer regular, the measure of a set is the infimum of the measure of open sets containing it. * `set.exists_is_open_lt_of_lt'` asserts that, when `ΞΌ` is outer regular, for every set `s` and `r > ΞΌ s` there exists an open superset `U βŠ‡ s` of measure less than `r`. * push forward of an outer regular measure is outer regular, and scalar multiplication of a regular measure by a finite number is outer regular. * `measure_theory.measure.outer_regular.of_sigma_compact_space_of_is_locally_finite_measure`: a locally finite measure on a `Οƒ`-compact metric (or even pseudo emetric) space is outer regular. ### Weakly regular measures * `is_open.measure_eq_supr_is_closed` asserts that the measure of an open set is the supremum of the measure of closed sets it contains. * `is_open.exists_lt_is_closed`: for an open set `U` and `r < ΞΌ U`, there exists a closed `F βŠ† U` of measure greater than `r`; * `measurable_set.measure_eq_supr_is_closed_of_ne_top` asserts that the measure of a measurable set of finite measure is the supremum of the measure of closed sets it contains. * `measurable_set.exists_lt_is_closed_of_ne_top` and `measurable_set.exists_is_closed_lt_add`: a measurable set of finite measure can be approximated by a closed subset (stated as `r < ΞΌ F` and `ΞΌ s < ΞΌ F + Ξ΅`, respectively). * `measure_theory.measure.weakly_regular.of_pseudo_emetric_space_of_is_finite_measure` is an instance registering that a finite measure on a metric space is weakly regular (in fact, a pseudo emetric space is enough); * `measure_theory.measure.weakly_regular.of_pseudo_emetric_sigma_compact_space_of_locally_finite` is an instance registering that a locally finite measure on a `Οƒ`-compact metric space (or even a pseudo emetric space) is weakly regular. ### Regular measures * `is_open.measure_eq_supr_is_compact` asserts that the measure of an open set is the supremum of the measure of compact sets it contains. * `is_open.exists_lt_is_compact`: for an open set `U` and `r < ΞΌ U`, there exists a compact `K βŠ† U` of measure greater than `r`; * `measurable_set.measure_eq_supr_is_compact_of_ne_top` asserts that the measure of a measurable set of finite measure is the supremum of the measure of compact sets it contains. * `measurable_set.exists_lt_is_compact_of_ne_top` and `measurable_set.exists_is_compact_lt_add`: a measurable set of finite measure can be approximated by a compact subset (stated as `r < ΞΌ K` and `ΞΌ s < ΞΌ K + Ξ΅`, respectively). * `measure_theory.measure.regular.of_sigma_compact_space_of_is_locally_finite_measure` is an instance registering that a locally finite measure on a `Οƒ`-compact metric space is regular (in fact, an emetric space is enough). ## Implementation notes The main nontrivial statement is `measure_theory.measure.inner_regular.weakly_regular_of_finite`, expressing that in a finite measure space, if every open set can be approximated from inside by closed sets, then the measure is in fact weakly regular. To prove that we show that any measurable set can be approximated from inside by closed sets and from outside by open sets. This statement is proved by measurable induction, starting from open sets and checking that it is stable by taking complements (this is the point of this condition, being symmetrical between inside and outside) and countable disjoint unions. Once this statement is proved, one deduces results for `Οƒ`-finite measures from this statement, by restricting them to finite measure sets (and proving that this restriction is weakly regular, using again the same statement). ## References [Halmos, Measure Theory, Β§52][halmos1950measure]. Note that Halmos uses an unusual definition of Borel sets (for him, they are elements of the `Οƒ`-algebra generated by compact sets!), so his proofs or statements do not apply directly. [Billingsley, Convergence of Probability Measures][billingsley1999] -/ open set filter open_locale ennreal topological_space nnreal big_operators namespace measure_theory namespace measure /-- We say that a measure `ΞΌ` is *inner regular* with respect to predicates `p q : set Ξ± β†’ Prop`, if for every `U` such that `q U` and `r < ΞΌ U`, there exists a subset `K βŠ† U` satisfying `p K` of measure greater than `r`. This definition is used to prove some facts about regular and weakly regular measures without repeating the proofs. -/ def inner_regular {Ξ±} {m : measurable_space Ξ±} (ΞΌ : measure Ξ±) (p q : set Ξ± β†’ Prop) := βˆ€ ⦃U⦄, q U β†’ βˆ€ r < ΞΌ U, βˆƒ K βŠ† U, p K ∧ r < ΞΌ K namespace inner_regular variables {Ξ± : Type*} {m : measurable_space Ξ±} {ΞΌ : measure Ξ±} {p q : set Ξ± β†’ Prop} {U : set Ξ±} {Ξ΅ : ℝβ‰₯0∞} lemma measure_eq_supr (H : inner_regular ΞΌ p q) (hU : q U) : ΞΌ U = ⨆ (K βŠ† U) (hK : p K), ΞΌ K := begin refine le_antisymm (le_of_forall_lt $ Ξ» r hr, _) (suprβ‚‚_le $ Ξ» K hK, supr_le $ Ξ» _, ΞΌ.mono hK), simpa only [lt_supr_iff, exists_prop] using H hU r hr end lemma exists_subset_lt_add (H : inner_regular ΞΌ p q) (h0 : p βˆ…) (hU : q U) (hΞΌU : ΞΌ U β‰  ∞) (hΞ΅ : Ξ΅ β‰  0) : βˆƒ K βŠ† U, p K ∧ ΞΌ U < ΞΌ K + Ξ΅ := begin cases eq_or_ne (ΞΌ U) 0 with hβ‚€ hβ‚€, { refine βŸ¨βˆ…, empty_subset _, h0, _⟩, rwa [measure_empty, hβ‚€, zero_add, pos_iff_ne_zero] }, { rcases H hU _ (ennreal.sub_lt_self hΞΌU hβ‚€ hΞ΅) with ⟨K, hKU, hKc, hrK⟩, exact ⟨K, hKU, hKc, ennreal.lt_add_of_sub_lt_right (or.inl hΞΌU) hrK⟩ } end lemma map {Ξ± Ξ²} [measurable_space Ξ±] [measurable_space Ξ²] {ΞΌ : measure Ξ±} {pa qa : set Ξ± β†’ Prop} (H : inner_regular ΞΌ pa qa) (f : Ξ± ≃ Ξ²) (hf : ae_measurable f ΞΌ) {pb qb : set Ξ² β†’ Prop} (hAB : βˆ€ U, qb U β†’ qa (f ⁻¹' U)) (hAB' : βˆ€ K, pa K β†’ pb (f '' K)) (hB₁ : βˆ€ K, pb K β†’ measurable_set K) (hBβ‚‚ : βˆ€ U, qb U β†’ measurable_set U) : inner_regular (map f ΞΌ) pb qb := begin intros U hU r hr, rw [map_apply_of_ae_measurable hf (hBβ‚‚ _ hU)] at hr, rcases H (hAB U hU) r hr with ⟨K, hKU, hKc, hK⟩, refine ⟨f '' K, image_subset_iff.2 hKU, hAB' _ hKc, _⟩, rwa [map_apply_of_ae_measurable hf (hB₁ _ $ hAB' _ hKc), f.preimage_image] end lemma smul (H : inner_regular ΞΌ p q) (c : ℝβ‰₯0∞) : inner_regular (c β€’ ΞΌ) p q := begin intros U hU r hr, rw [smul_apply, H.measure_eq_supr hU, smul_eq_mul] at hr, simpa only [ennreal.mul_supr, lt_supr_iff, exists_prop] using hr end lemma trans {q' : set Ξ± β†’ Prop} (H : inner_regular ΞΌ p q) (H' : inner_regular ΞΌ q q') : inner_regular ΞΌ p q' := begin intros U hU r hr, rcases H' hU r hr with ⟨F, hFU, hqF, hF⟩, rcases H hqF _ hF with ⟨K, hKF, hpK, hrK⟩, exact ⟨K, hKF.trans hFU, hpK, hrK⟩ end end inner_regular variables {Ξ± Ξ² : Type*} [measurable_space Ξ±] [topological_space Ξ±] {ΞΌ : measure Ξ±} /-- A measure `ΞΌ` is outer regular if `ΞΌ(A) = inf {ΞΌ(U) | A βŠ† U open}` for a measurable set `A`. This definition implies the same equality for any (not necessarily measurable) set, see `set.measure_eq_infi_is_open`. -/ @[protect_proj] class outer_regular (ΞΌ : measure Ξ±) : Prop := (outer_regular : βˆ€ ⦃A : set α⦄, measurable_set A β†’ βˆ€ r > ΞΌ A, βˆƒ U βŠ‡ A, is_open U ∧ ΞΌ U < r) /-- A measure `ΞΌ` is regular if - it is finite on all compact sets; - it is outer regular: `ΞΌ(A) = inf {ΞΌ(U) | A βŠ† U open}` for `A` measurable; - it is inner regular for open sets, using compact sets: `ΞΌ(U) = sup {ΞΌ(K) | K βŠ† U compact}` for `U` open. -/ @[protect_proj] class regular (ΞΌ : measure Ξ±) extends is_finite_measure_on_compacts ΞΌ, outer_regular ΞΌ : Prop := (inner_regular : inner_regular ΞΌ is_compact is_open) /-- A measure `ΞΌ` is weakly regular if - it is outer regular: `ΞΌ(A) = inf {ΞΌ(U) | A βŠ† U open}` for `A` measurable; - it is inner regular for open sets, using closed sets: `ΞΌ(U) = sup {ΞΌ(F) | F βŠ† U compact}` for `U` open. -/ @[protect_proj] class weakly_regular (ΞΌ : measure Ξ±) extends outer_regular ΞΌ : Prop := (inner_regular : inner_regular ΞΌ is_closed is_open) /-- A regular measure is weakly regular. -/ @[priority 100] -- see Note [lower instance priority] instance regular.weakly_regular [t2_space Ξ±] [regular ΞΌ] : weakly_regular ΞΌ := { inner_regular := Ξ» U hU r hr, let ⟨K, hKU, hcK, hK⟩ := regular.inner_regular hU r hr in ⟨K, hKU, hcK.is_closed, hK⟩ } namespace outer_regular instance zero : outer_regular (0 : measure Ξ±) := ⟨λ A hA r hr, ⟨univ, subset_univ A, is_open_univ, hr⟩⟩ /-- Given `r` larger than the measure of a set `A`, there exists an open superset of `A` with measure less than `r`. -/ lemma _root_.set.exists_is_open_lt_of_lt [outer_regular ΞΌ] (A : set Ξ±) (r : ℝβ‰₯0∞) (hr : ΞΌ A < r) : βˆƒ U βŠ‡ A, is_open U ∧ ΞΌ U < r := begin rcases outer_regular.outer_regular (measurable_set_to_measurable ΞΌ A) r (by rwa measure_to_measurable) with ⟨U, hAU, hUo, hU⟩, exact ⟨U, (subset_to_measurable _ _).trans hAU, hUo, hU⟩ end /-- For an outer regular measure, the measure of a set is the infimum of the measures of open sets containing it. -/ lemma _root_.set.measure_eq_infi_is_open (A : set Ξ±) (ΞΌ : measure Ξ±) [outer_regular ΞΌ] : ΞΌ A = (β¨… (U : set Ξ±) (h : A βŠ† U) (h2 : is_open U), ΞΌ U) := begin refine le_antisymm (le_infiβ‚‚ $ Ξ» s hs, le_infi $ Ξ» h2s, ΞΌ.mono hs) _, refine le_of_forall_lt' (Ξ» r hr, _), simpa only [infi_lt_iff, exists_prop] using A.exists_is_open_lt_of_lt r hr end lemma _root_.set.exists_is_open_lt_add [outer_regular ΞΌ] (A : set Ξ±) (hA : ΞΌ A β‰  ∞) {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) : βˆƒ U βŠ‡ A, is_open U ∧ ΞΌ U < ΞΌ A + Ξ΅ := A.exists_is_open_lt_of_lt _ (ennreal.lt_add_right hA hΞ΅) lemma _root_.set.exists_is_open_le_add (A : set Ξ±) (ΞΌ : measure Ξ±) [outer_regular ΞΌ] {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) : βˆƒ U βŠ‡ A, is_open U ∧ ΞΌ U ≀ ΞΌ A + Ξ΅ := begin rcases le_or_lt ∞ (ΞΌ A) with H|H, { exact ⟨univ, subset_univ _, is_open_univ, by simp only [top_le_iff.mp H, ennreal.top_add, le_top]⟩ }, { rcases A.exists_is_open_lt_add H.ne hΞ΅ with ⟨U, AU, U_open, hU⟩, exact ⟨U, AU, U_open, hU.le⟩ } end lemma _root_.measurable_set.exists_is_open_diff_lt [outer_regular ΞΌ] {A : set Ξ±} (hA : measurable_set A) (hA' : ΞΌ A β‰  ∞) {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) : βˆƒ U βŠ‡ A, is_open U ∧ ΞΌ U < ∞ ∧ ΞΌ (U \ A) < Ξ΅ := begin rcases A.exists_is_open_lt_add hA' hΞ΅ with ⟨U, hAU, hUo, hU⟩, use [U, hAU, hUo, hU.trans_le le_top], exact measure_diff_lt_of_lt_add hA hAU hA' hU, end protected lemma map [opens_measurable_space Ξ±] [measurable_space Ξ²] [topological_space Ξ²] [borel_space Ξ²] (f : Ξ± β‰ƒβ‚œ Ξ²) (ΞΌ : measure Ξ±) [outer_regular ΞΌ] : (measure.map f ΞΌ).outer_regular := begin refine ⟨λ A hA r hr, _⟩, rw [map_apply f.measurable hA, ← f.image_symm] at hr, rcases set.exists_is_open_lt_of_lt _ r hr with ⟨U, hAU, hUo, hU⟩, have : is_open (f.symm ⁻¹' U), from hUo.preimage f.symm.continuous, refine ⟨f.symm ⁻¹' U, image_subset_iff.1 hAU, this, _⟩, rwa [map_apply f.measurable this.measurable_set, f.preimage_symm, f.preimage_image], end protected lemma smul (ΞΌ : measure Ξ±) [outer_regular ΞΌ] {x : ℝβ‰₯0∞} (hx : x β‰  ∞) : (x β€’ ΞΌ).outer_regular := begin rcases eq_or_ne x 0 with rfl|h0, { rw zero_smul, exact outer_regular.zero }, { refine ⟨λ A hA r hr, _⟩, rw [smul_apply, A.measure_eq_infi_is_open, smul_eq_mul] at hr, simpa only [ennreal.mul_infi_of_ne h0 hx, gt_iff_lt, infi_lt_iff, exists_prop] using hr } end end outer_regular /-- If a measure `ΞΌ` admits finite spanning open sets such that the restriction of `ΞΌ` to each set is outer regular, then the original measure is outer regular as well. -/ protected lemma finite_spanning_sets_in.outer_regular [opens_measurable_space Ξ±] {ΞΌ : measure Ξ±} (s : ΞΌ.finite_spanning_sets_in {U | is_open U ∧ outer_regular (ΞΌ.restrict U)}) : outer_regular ΞΌ := begin refine ⟨λ A hA r hr, _⟩, have hm : βˆ€ n, measurable_set (s.set n), from Ξ» n, (s.set_mem n).1.measurable_set, haveI : βˆ€ n, outer_regular (ΞΌ.restrict (s.set n)) := Ξ» n, (s.set_mem n).2, -- Note that `A = ⋃ n, A ∩ disjointed s n`. We replace `A` with this sequence. obtain ⟨A, hAm, hAs, hAd, rfl⟩ : βˆƒ A' : β„• β†’ set Ξ±, (βˆ€ n, measurable_set (A' n)) ∧ (βˆ€ n, A' n βŠ† s.set n) ∧ pairwise (disjoint on A') ∧ A = ⋃ n, A' n, { refine ⟨λ n, A ∩ disjointed s.set n, Ξ» n, hA.inter (measurable_set.disjointed hm _), Ξ» n, (inter_subset_right _ _).trans (disjointed_subset _ _), (disjoint_disjointed s.set).mono (Ξ» k l hkl, hkl.mono inf_le_right inf_le_right), _⟩, rw [← inter_Union, Union_disjointed, s.spanning, inter_univ] }, rcases ennreal.exists_pos_sum_of_countable' (tsub_pos_iff_lt.2 hr).ne' β„• with ⟨δ, Ξ΄0, hδΡ⟩, rw [lt_tsub_iff_right, add_comm] at hδΡ, have : βˆ€ n, βˆƒ U βŠ‡ A n, is_open U ∧ ΞΌ U < ΞΌ (A n) + Ξ΄ n, { intro n, have H₁ : βˆ€ t, ΞΌ.restrict (s.set n) t = ΞΌ (t ∩ s.set n), from Ξ» t, restrict_apply' (hm n), have Ht : ΞΌ.restrict (s.set n) (A n) β‰  ⊀, { rw H₁, exact ((measure_mono $ inter_subset_right _ _).trans_lt (s.finite n)).ne }, rcases (A n).exists_is_open_lt_add Ht (Ξ΄0 n).ne' with ⟨U, hAU, hUo, hU⟩, rw [H₁, H₁, inter_eq_self_of_subset_left (hAs _)] at hU, exact ⟨U ∩ s.set n, subset_inter hAU (hAs _), hUo.inter (s.set_mem n).1, hU⟩ }, choose U hAU hUo hU, refine βŸ¨β‹ƒ n, U n, Union_mono hAU, is_open_Union hUo, _⟩, calc ΞΌ (⋃ n, U n) ≀ βˆ‘' n, ΞΌ (U n) : measure_Union_le _ ... ≀ βˆ‘' n, (ΞΌ (A n) + Ξ΄ n) : ennreal.tsum_le_tsum (Ξ» n, (hU n).le) ... = βˆ‘' n, ΞΌ (A n) + βˆ‘' n, Ξ΄ n : ennreal.tsum_add ... = ΞΌ (⋃ n, A n) + βˆ‘' n, Ξ΄ n : congr_arg2 (+) (measure_Union hAd hAm).symm rfl ... < r : hδΡ end namespace inner_regular variables {p q : set Ξ± β†’ Prop} {U s : set Ξ±} {Ξ΅ r : ℝβ‰₯0∞} /-- If a measure is inner regular (using closed or compact sets), then every measurable set of finite measure can by approximated by a (closed or compact) subset. -/ lemma measurable_set_of_open [outer_regular ΞΌ] (H : inner_regular ΞΌ p is_open) (h0 : p βˆ…) (hd : βˆ€ ⦃s U⦄, p s β†’ is_open U β†’ p (s \ U)) : inner_regular ΞΌ p (Ξ» s, measurable_set s ∧ ΞΌ s β‰  ∞) := begin rintros s ⟨hs, hΞΌs⟩ r hr, obtain ⟨Ρ, hΞ΅, hΞ΅s, rfl⟩ : βˆƒ Ξ΅ β‰  0, Ξ΅ + Ξ΅ ≀ ΞΌ s ∧ r = ΞΌ s - (Ξ΅ + Ξ΅), { use (ΞΌ s - r) / 2, simp [*, hr.le, ennreal.add_halves, ennreal.sub_sub_cancel, le_add_right] }, rcases hs.exists_is_open_diff_lt hΞΌs hΞ΅ with ⟨U, hsU, hUo, hUt, hΞΌU⟩, rcases (U \ s).exists_is_open_lt_of_lt _ hΞΌU with ⟨U', hsU', hU'o, hΞΌU'⟩, replace hsU' := diff_subset_comm.1 hsU', rcases H.exists_subset_lt_add h0 hUo hUt.ne hΞ΅ with ⟨K, hKU, hKc, hKr⟩, refine ⟨K \ U', Ξ» x hx, hsU' ⟨hKU hx.1, hx.2⟩, hd hKc hU'o, ennreal.sub_lt_of_lt_add hΞ΅s _⟩, calc ΞΌ s ≀ ΞΌ U : ΞΌ.mono hsU ... < ΞΌ K + Ξ΅ : hKr ... ≀ ΞΌ (K \ U') + ΞΌ U' + Ξ΅ : add_le_add_right (tsub_le_iff_right.1 le_measure_diff) _ ... ≀ ΞΌ (K \ U') + Ξ΅ + Ξ΅ : by { mono*, exacts [hΞΌU'.le, le_rfl] } ... = ΞΌ (K \ U') + (Ξ΅ + Ξ΅) : add_assoc _ _ _ end open finset /-- In a finite measure space, assume that any open set can be approximated from inside by closed sets. Then the measure is weakly regular. -/ lemma weakly_regular_of_finite [borel_space Ξ±] (ΞΌ : measure Ξ±) [is_finite_measure ΞΌ] (H : inner_regular ΞΌ is_closed is_open) : weakly_regular ΞΌ := begin have hfin : βˆ€ {s}, ΞΌ s β‰  ⊀ := measure_ne_top ΞΌ, suffices : βˆ€ s, measurable_set s β†’ βˆ€ Ξ΅ β‰  0, βˆƒ (F βŠ† s) (U βŠ‡ s), is_closed F ∧ is_open U ∧ ΞΌ s ≀ ΞΌ F + Ξ΅ ∧ ΞΌ U ≀ ΞΌ s + Ξ΅, { refine { outer_regular := Ξ» s hs r hr, _, inner_regular := H }, rcases exists_between hr with ⟨r', hsr', hr'r⟩, rcases this s hs _ (tsub_pos_iff_lt.2 hsr').ne' with ⟨-, -, U, hsU, -, hUo, -, H⟩, refine ⟨U, hsU, hUo, _⟩, rw [add_tsub_cancel_of_le hsr'.le] at H, exact H.trans_lt hr'r }, refine measurable_set.induction_on_open _ _ _, /- The proof is by measurable induction: we should check that the property is true for the empty set, for open sets, and is stable by taking the complement and by taking countable disjoint unions. The point of the property we are proving is that it is stable by taking complements (exchanging the roles of closed and open sets and thanks to the finiteness of the measure). -/ -- check for open set { intros U hU Ξ΅ hΞ΅, rcases H.exists_subset_lt_add is_closed_empty hU hfin hΞ΅ with ⟨F, hsF, hFc, hF⟩, exact ⟨F, hsF, U, subset.rfl, hFc, hU, hF.le, le_self_add⟩ }, -- check for complements { rintros s hs H Ξ΅ hΞ΅, rcases H Ξ΅ hΞ΅ with ⟨F, hFs, U, hsU, hFc, hUo, hF, hU⟩, refine ⟨Uᢜ, compl_subset_compl.2 hsU, Fᢜ, compl_subset_compl.2 hFs, hUo.is_closed_compl, hFc.is_open_compl, _⟩, simp only [measure_compl_le_add_iff, *, hUo.measurable_set, hFc.measurable_set, true_and] }, -- check for disjoint unions { intros s hsd hsm H Ξ΅ Ξ΅0, have Ξ΅0' : Ξ΅ / 2 β‰  0, from (ennreal.half_pos Ξ΅0).ne', rcases ennreal.exists_pos_sum_of_countable' Ξ΅0' β„• with ⟨δ, Ξ΄0, hδΡ⟩, choose F hFs U hsU hFc hUo hF hU using Ξ» n, H n (Ξ΄ n) (Ξ΄0 n).ne', -- the approximating closed set is constructed by considering finitely many sets `s i`, which -- cover all the measure up to `Ξ΅/2`, approximating each of these by a closed set `F i`, and -- taking the union of these (finitely many) `F i`. have : tendsto (Ξ» t, βˆ‘ k in t, ΞΌ (s k) + Ξ΅ / 2) at_top (𝓝 $ ΞΌ (⋃ n, s n) + Ξ΅ / 2), { rw measure_Union hsd hsm, exact tendsto.add ennreal.summable.has_sum tendsto_const_nhds }, rcases (this.eventually $ lt_mem_nhds $ ennreal.lt_add_right hfin Ξ΅0').exists with ⟨t, ht⟩, -- the approximating open set is constructed by taking for each `s n` an approximating open set -- `U n` with measure at most `ΞΌ (s n) + Ξ΄ n` for a summable `Ξ΄`, and taking the union of these. refine βŸ¨β‹ƒ k ∈ t, F k, Union_mono $ Ξ» k, Union_subset $ Ξ» _, hFs _, ⋃ n, U n, Union_mono hsU, is_closed_bUnion t.finite_to_set $ Ξ» k _, hFc k, is_open_Union hUo, ht.le.trans _, _⟩, { calc βˆ‘ k in t, ΞΌ (s k) + Ξ΅ / 2 ≀ βˆ‘ k in t, ΞΌ (F k) + βˆ‘ k in t, Ξ΄ k + Ξ΅ / 2 : by { rw ← sum_add_distrib, exact add_le_add_right (sum_le_sum $ Ξ» k hk, hF k) _ } ... ≀ βˆ‘ k in t, ΞΌ (F k) + Ξ΅ / 2 + Ξ΅ / 2 : add_le_add_right (add_le_add_left ((ennreal.sum_le_tsum _).trans hδΡ.le) _) _ ... = ΞΌ (⋃ k ∈ t, F k) + Ξ΅ : _, rw [measure_bUnion_finset, add_assoc, ennreal.add_halves], exacts [Ξ» k _ n _ hkn, (hsd hkn).mono (hFs k) (hFs n), Ξ» k hk, (hFc k).measurable_set] }, { calc ΞΌ (⋃ n, U n) ≀ βˆ‘' n, ΞΌ (U n) : measure_Union_le _ ... ≀ βˆ‘' n, (ΞΌ (s n) + Ξ΄ n) : ennreal.tsum_le_tsum hU ... = ΞΌ (⋃ n, s n) + βˆ‘' n, Ξ΄ n : by rw [measure_Union hsd hsm, ennreal.tsum_add] ... ≀ ΞΌ (⋃ n, s n) + Ξ΅ : add_le_add_left (hδΡ.le.trans ennreal.half_le_self) _ } } end /-- In a metric space (or even a pseudo emetric space), an open set can be approximated from inside by closed sets. -/ lemma of_pseudo_emetric_space {X : Type*} [pseudo_emetric_space X] [measurable_space X] (ΞΌ : measure X) : inner_regular ΞΌ is_closed is_open := begin intros U hU r hr, rcases hU.exists_Union_is_closed with ⟨F, F_closed, -, rfl, F_mono⟩, rw measure_Union_eq_supr F_mono.directed_le at hr, rcases lt_supr_iff.1 hr with ⟨n, hn⟩, exact ⟨F n, subset_Union _ _, F_closed n, hn⟩ end /-- In a `Οƒ`-compact space, any closed set can be approximated by a compact subset. -/ lemma is_compact_is_closed {X : Type*} [topological_space X] [sigma_compact_space X] [measurable_space X] (ΞΌ : measure X) : inner_regular ΞΌ is_compact is_closed := begin intros F hF r hr, set B : β„• β†’ set X := compact_covering X, have hBc : βˆ€ n, is_compact (F ∩ B n), from Ξ» n, (is_compact_compact_covering X n).inter_left hF, have hBU : (⋃ n, F ∩ B n) = F, by rw [← inter_Union, Union_compact_covering, set.inter_univ], have : ΞΌ F = ⨆ n, ΞΌ (F ∩ B n), { rw [← measure_Union_eq_supr, hBU], exact monotone.directed_le (Ξ» m n h, inter_subset_inter_right _ (compact_covering_subset _ h)) }, rw this at hr, rcases lt_supr_iff.1 hr with ⟨n, hn⟩, exact ⟨_, inter_subset_left _ _, hBc n, hn⟩ end end inner_regular namespace regular instance zero : regular (0 : measure Ξ±) := ⟨λ U hU r hr, βŸ¨βˆ…, empty_subset _, is_compact_empty, hr⟩⟩ /-- If `ΞΌ` is a regular measure, then any open set can be approximated by a compact subset. -/ lemma _root_.is_open.exists_lt_is_compact [regular ΞΌ] ⦃U : set α⦄ (hU : is_open U) {r : ℝβ‰₯0∞} (hr : r < ΞΌ U) : βˆƒ K βŠ† U, is_compact K ∧ r < ΞΌ K := regular.inner_regular hU r hr /-- The measure of an open set is the supremum of the measures of compact sets it contains. -/ lemma _root_.is_open.measure_eq_supr_is_compact ⦃U : set α⦄ (hU : is_open U) (ΞΌ : measure Ξ±) [regular ΞΌ] : ΞΌ U = (⨆ (K : set Ξ±) (h : K βŠ† U) (h2 : is_compact K), ΞΌ K) := regular.inner_regular.measure_eq_supr hU lemma exists_compact_not_null [regular ΞΌ] : (βˆƒ K, is_compact K ∧ ΞΌ K β‰  0) ↔ ΞΌ β‰  0 := by simp_rw [ne.def, ← measure_univ_eq_zero, is_open_univ.measure_eq_supr_is_compact, ennreal.supr_eq_zero, not_forall, exists_prop, subset_univ, true_and] /-- If `ΞΌ` is a regular measure, then any measurable set of finite measure can be approximated by a compact subset. See also `measurable_set.exists_is_compact_lt_add` and `measurable_set.exists_lt_is_compact_of_ne_top`. -/ lemma inner_regular_measurable [regular ΞΌ] : inner_regular ΞΌ is_compact (Ξ» s, measurable_set s ∧ ΞΌ s β‰  ∞) := regular.inner_regular.measurable_set_of_open is_compact_empty (Ξ» _ _, is_compact.diff) /-- If `ΞΌ` is a regular measure, then any measurable set of finite measure can be approximated by a compact subset. See also `measurable_set.exists_lt_is_compact_of_ne_top`. -/ lemma _root_.measurable_set.exists_is_compact_lt_add [regular ΞΌ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : ΞΌ A β‰  ∞) {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) : βˆƒ K βŠ† A, is_compact K ∧ ΞΌ A < ΞΌ K + Ξ΅ := regular.inner_regular_measurable.exists_subset_lt_add is_compact_empty ⟨hA, h'A⟩ h'A hΞ΅ /-- If `ΞΌ` is a regular measure, then any measurable set of finite measure can be approximated by a compact subset. See also `measurable_set.exists_is_compact_lt_add` and `measurable_set.exists_lt_is_compact_of_ne_top`. -/ lemma _root_.measurable_set.exists_is_compact_diff_lt [opens_measurable_space Ξ±] [t2_space Ξ±] [regular ΞΌ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : ΞΌ A β‰  ∞) {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) : βˆƒ K βŠ† A, is_compact K ∧ ΞΌ (A \ K) < Ξ΅ := begin rcases hA.exists_is_compact_lt_add h'A hΞ΅ with ⟨K, hKA, hKc, hK⟩, exact ⟨K, hKA, hKc, measure_diff_lt_of_lt_add hKc.measurable_set hKA (ne_top_of_le_ne_top h'A $ measure_mono hKA) hK⟩ end /-- If `ΞΌ` is a regular measure, then any measurable set of finite measure can be approximated by a compact subset. See also `measurable_set.exists_is_compact_lt_add`. -/ lemma _root_.measurable_set.exists_lt_is_compact_of_ne_top [regular ΞΌ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : ΞΌ A β‰  ∞) {r : ℝβ‰₯0∞} (hr : r < ΞΌ A) : βˆƒ K βŠ† A, is_compact K ∧ r < ΞΌ K := regular.inner_regular_measurable ⟨hA, h'A⟩ _ hr /-- Given a regular measure, any measurable set of finite mass can be approximated from inside by compact sets. -/ lemma _root_.measurable_set.measure_eq_supr_is_compact_of_ne_top [regular ΞΌ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : ΞΌ A β‰  ∞) : ΞΌ A = (⨆ (K βŠ† A) (h : is_compact K), ΞΌ K) := regular.inner_regular_measurable.measure_eq_supr ⟨hA, h'A⟩ protected lemma map [opens_measurable_space Ξ±] [measurable_space Ξ²] [topological_space Ξ²] [t2_space Ξ²] [borel_space Ξ²] [regular ΞΌ] (f : Ξ± β‰ƒβ‚œ Ξ²) : (measure.map f ΞΌ).regular := begin haveI := outer_regular.map f ΞΌ, haveI := is_finite_measure_on_compacts.map ΞΌ f, exact ⟨regular.inner_regular.map f.to_equiv f.measurable.ae_measurable (Ξ» U hU, hU.preimage f.continuous) (Ξ» K hK, hK.image f.continuous) (Ξ» K hK, hK.measurable_set) (Ξ» U hU, hU.measurable_set)⟩ end protected lemma smul [regular ΞΌ] {x : ℝβ‰₯0∞} (hx : x β‰  ∞) : (x β€’ ΞΌ).regular := begin haveI := outer_regular.smul ΞΌ hx, haveI := is_finite_measure_on_compacts.smul ΞΌ hx, exact ⟨regular.inner_regular.smul x⟩ end /-- A regular measure in a Οƒ-compact space is Οƒ-finite. -/ @[priority 100] -- see Note [lower instance priority] instance sigma_finite [sigma_compact_space Ξ±] [regular ΞΌ] : sigma_finite ΞΌ := ⟨⟨{ set := compact_covering Ξ±, set_mem := Ξ» n, trivial, finite := Ξ» n, (is_compact_compact_covering Ξ± n).measure_lt_top, spanning := Union_compact_covering Ξ± }⟩⟩ end regular namespace weakly_regular /-- If `ΞΌ` is a weakly regular measure, then any open set can be approximated by a closed subset. -/ lemma _root_.is_open.exists_lt_is_closed [weakly_regular ΞΌ] ⦃U : set α⦄ (hU : is_open U) {r : ℝβ‰₯0∞} (hr : r < ΞΌ U) : βˆƒ F βŠ† U, is_closed F ∧ r < ΞΌ F := weakly_regular.inner_regular hU r hr /-- If `ΞΌ` is a weakly regular measure, then any open set can be approximated by a closed subset. -/ lemma _root_.is_open.measure_eq_supr_is_closed ⦃U : set α⦄ (hU : is_open U) (ΞΌ : measure Ξ±) [weakly_regular ΞΌ] : ΞΌ U = (⨆ (F βŠ† U) (h : is_closed F), ΞΌ F) := weakly_regular.inner_regular.measure_eq_supr hU lemma inner_regular_measurable [weakly_regular ΞΌ] : inner_regular ΞΌ is_closed (Ξ» s, measurable_set s ∧ ΞΌ s β‰  ∞) := weakly_regular.inner_regular.measurable_set_of_open is_closed_empty (Ξ» _ _ h₁ hβ‚‚, h₁.inter hβ‚‚.is_closed_compl) /-- If `s` is a measurable set, a weakly regular measure `ΞΌ` is finite on `s`, and `Ξ΅` is a positive number, then there exist a closed set `K βŠ† s` such that `ΞΌ s < ΞΌ K + Ξ΅`. -/ lemma _root_.measurable_set.exists_is_closed_lt_add [weakly_regular ΞΌ] {s : set Ξ±} (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) : βˆƒ K βŠ† s, is_closed K ∧ ΞΌ s < ΞΌ K + Ξ΅ := inner_regular_measurable.exists_subset_lt_add is_closed_empty ⟨hs, hΞΌs⟩ hΞΌs hΞ΅ lemma _root_.measurable_set.exists_is_closed_diff_lt [opens_measurable_space Ξ±] [weakly_regular ΞΌ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : ΞΌ A β‰  ∞) {Ξ΅ : ℝβ‰₯0∞} (hΞ΅ : Ξ΅ β‰  0) : βˆƒ F βŠ† A, is_closed F ∧ ΞΌ (A \ F) < Ξ΅ := begin rcases hA.exists_is_closed_lt_add h'A hΞ΅ with ⟨F, hFA, hFc, hF⟩, exact ⟨F, hFA, hFc, measure_diff_lt_of_lt_add hFc.measurable_set hFA (ne_top_of_le_ne_top h'A $ measure_mono hFA) hF⟩ end /-- Given a weakly regular measure, any measurable set of finite mass can be approximated from inside by closed sets. -/ lemma _root_.measurable_set.exists_lt_is_closed_of_ne_top [weakly_regular ΞΌ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : ΞΌ A β‰  ∞) {r : ℝβ‰₯0∞} (hr : r < ΞΌ A) : βˆƒ K βŠ† A, is_closed K ∧ r < ΞΌ K := inner_regular_measurable ⟨hA, h'A⟩ _ hr /-- Given a weakly regular measure, any measurable set of finite mass can be approximated from inside by closed sets. -/ lemma _root_.measurable_set.measure_eq_supr_is_closed_of_ne_top [weakly_regular ΞΌ] ⦃A : set α⦄ (hA : measurable_set A) (h'A : ΞΌ A β‰  ∞) : ΞΌ A = (⨆ (K βŠ† A) (h : is_closed K), ΞΌ K) := inner_regular_measurable.measure_eq_supr ⟨hA, h'A⟩ /-- The restriction of a weakly regular measure to a measurable set of finite measure is weakly regular. -/ lemma restrict_of_measurable_set [borel_space Ξ±] [weakly_regular ΞΌ] (A : set Ξ±) (hA : measurable_set A) (h'A : ΞΌ A β‰  ∞) : weakly_regular (ΞΌ.restrict A) := begin haveI : fact (ΞΌ A < ∞) := ⟨h'A.lt_top⟩, refine inner_regular.weakly_regular_of_finite _ (Ξ» V V_open, _), simp only [restrict_apply' hA], intros r hr, have : ΞΌ (V ∩ A) β‰  ∞, from ne_top_of_le_ne_top h'A (measure_mono $ inter_subset_right _ _), rcases (V_open.measurable_set.inter hA).exists_lt_is_closed_of_ne_top this hr with ⟨F, hFVA, hFc, hF⟩, refine ⟨F, hFVA.trans (inter_subset_left _ _), hFc, _⟩, rwa inter_eq_self_of_subset_left (hFVA.trans $ inter_subset_right _ _) end /-- Any finite measure on a metric space (or even a pseudo emetric space) is weakly regular. -/ @[priority 100] -- see Note [lower instance priority] instance of_pseudo_emetric_space_of_is_finite_measure {X : Type*} [pseudo_emetric_space X] [measurable_space X] [borel_space X] (ΞΌ : measure X) [is_finite_measure ΞΌ] : weakly_regular ΞΌ := (inner_regular.of_pseudo_emetric_space ΞΌ).weakly_regular_of_finite ΞΌ /-- Any locally finite measure on a `Οƒ`-compact metric space (or even a pseudo emetric space) is weakly regular. -/ @[priority 100] -- see Note [lower instance priority] instance of_pseudo_emetric_sigma_compact_space_of_locally_finite {X : Type*} [pseudo_emetric_space X] [sigma_compact_space X] [measurable_space X] [borel_space X] (ΞΌ : measure X) [is_locally_finite_measure ΞΌ] : weakly_regular ΞΌ := begin haveI : outer_regular ΞΌ, { refine (ΞΌ.finite_spanning_sets_in_open.mono' $ Ξ» U hU, _).outer_regular, haveI : fact (ΞΌ U < ∞), from ⟨hU.2⟩, exact ⟨hU.1, infer_instance⟩ }, exact ⟨inner_regular.of_pseudo_emetric_space μ⟩ end end weakly_regular /-- Any locally finite measure on a `Οƒ`-compact (e)metric space is regular. -/ @[priority 100] -- see Note [lower instance priority] instance regular.of_sigma_compact_space_of_is_locally_finite_measure {X : Type*} [emetric_space X] [sigma_compact_space X] [measurable_space X] [borel_space X] (ΞΌ : measure X) [is_locally_finite_measure ΞΌ] : regular ΞΌ := { lt_top_of_is_compact := Ξ» K hK, hK.measure_lt_top, inner_regular := (inner_regular.is_compact_is_closed ΞΌ).trans (inner_regular.of_pseudo_emetric_space ΞΌ) } end measure end measure_theory
2075c6d56e67fd282c5ddcaf87fdda85c027100c
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/tree.lean
90b76db4ca0d89a047708a911211e706523b87a9
[ "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
1,994
lean
import logic data.prod open eq.ops prod inductive tree (A : Type) := | leaf : A β†’ tree A | node : tree A β†’ tree A β†’ tree A inductive one1.{l} : Type.{max 1 l} := star : one1 set_option pp.universes true namespace tree namespace manual section universe variables l₁ lβ‚‚ variable {A : Type.{l₁}} variable (C : tree A β†’ Type.{lβ‚‚}) definition below (t : tree A) : Type := tree.rec_on t (Ξ» a, one1.{lβ‚‚}) (Ξ» t₁ tβ‚‚ r₁ rβ‚‚, C t₁ Γ— C tβ‚‚ Γ— r₁ Γ— rβ‚‚) end section universe variables l₁ lβ‚‚ variable {A : Type.{l₁}} variable {C : tree A β†’ Type.{lβ‚‚}} definition below_rec_on (t : tree A) (H : Ξ  (n : tree A), below C n β†’ C n) : C t := have general : C t Γ— below C t, from tree.rec_on t (Ξ»a, (H (leaf a) one1.star, one1.star)) (Ξ» (l r : tree A) (Hl : C l Γ— below C l) (Hr : C r Γ— below C r), have b : below C (node l r), from (pr₁ Hl, pr₁ Hr, prβ‚‚ Hl, prβ‚‚ Hr), have c : C (node l r), from H (node l r) b, (c, b)), pr₁ general end end manual section universe variables l₁ lβ‚‚ variable {A : Type.{l₁}} variable {C : tree A β†’ Type.{lβ‚‚+1}} definition below_rec_on (t : tree A) (H : Ξ  (n : tree A), @tree.below A C n β†’ C n) : C t := have general : C t Γ— @tree.below A C t, from tree.rec_on t (Ξ»a, (H (leaf a) poly_unit.star, poly_unit.star)) (Ξ» (l r : tree A) (Hl : C l Γ— @tree.below A C l) (Hr : C r Γ— @tree.below A C r), have b : @tree.below A C (node l r), from ((pr₁ Hl, prβ‚‚ Hl), (pr₁ Hr, prβ‚‚ Hr)), have c : C (node l r), from H (node l r) b, (c, b)), pr₁ general end set_option pp.universes true theorem leaf_ne_tree {A : Type} (a : A) (l r : tree A) : leaf a β‰  node l r := assume h : leaf a = node l r, tree.no_confusion h end tree
5e8c2acf54bdf96ed431e9531b494ef3aba853b0
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/measure_theory/function/conditional_expectation.lean
de4ba9188a3d50f6d7d53417906eaceecf6ade4f
[ "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
85,748
lean
/- Copyright (c) 2021 RΓ©my Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: RΓ©my Degenne -/ import analysis.inner_product_space.projection import measure_theory.function.l2_space import measure_theory.decomposition.radon_nikodym /-! # Conditional expectation We build the conditional expectation of a function `f` with value in a Banach space with respect to a measure `ΞΌ` (defined on a measurable space structure `m0`) and a measurable space structure `m` with `hm : m ≀ m0` (a sub-sigma-algebra). This is an `m`-measurable function `ΞΌ[f|hm]` which is integrable and verifies `∫ x in s, ΞΌ[f|hm] x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ` for any `m`-measurable sets `s`. It is unique as an element of `LΒΉ`. The construction is done in four steps: * Define the conditional expectation of an `LΒ²` function, as an element of `LΒ²`. This is the orthogonal projection on the subspace of almost everywhere `m`-measurable functions. * Show that the conditional expectation of the indicator of a measurable set with finite measure is integrable and define a map `set Ξ± β†’ (E β†’L[ℝ] (Ξ± →₁[ΞΌ] E))` which to a set associates a linear map. That linear map sends `x ∈ E` to the conditional expectation of the indicator of the set with value `x`. * Extend that map to `condexp_L1_clm : (Ξ± →₁[ΞΌ] E) β†’L[ℝ] (Ξ± →₁[ΞΌ] E)`. This is done using the same construction as the Bochner integral (see the file `measure_theory/integral/set_to_L1`). * Define the conditional expectation of a function `f : Ξ± β†’ E`, which is an integrable function `Ξ± β†’ E` equal to 0 if `f` is not integrable, and equal to an `m`-measurable representative of `condexp_L1_clm` applied to `[f]`, the equivalence class of `f` in `LΒΉ`. ## Main results The conditional expectation and its properties * `condexp (hm : m ≀ m0) (ΞΌ : measure Ξ±) (f : Ξ± β†’ E)`: conditional expectation of `f` with respect to `m`. * `integrable_condexp` : `condexp` is integrable. * `measurable_condexp` : `condexp` is `m`-measurable. * `set_integral_condexp (hf : integrable f ΞΌ) (hs : measurable_set[m] s)` : the conditional expectation verifies `∫ x in s, condexp hm ΞΌ f x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ` for any `m`-measurable set `s`. While `condexp` is function-valued, we also define `condexp_L1` with value in `L1` and a continuous linear map `condexp_L1_clm` from `L1` to `L1`. `condexp` should be used in most cases. Uniqueness of the conditional expectation * `Lp.ae_eq_of_forall_set_integral_eq'`: two `Lp` functions verifying the equality of integrals defining the conditional expectation are equal everywhere. * `ae_eq_of_forall_set_integral_eq_of_sigma_finite'`: two functions verifying the equality of integrals defining the conditional expectation are equal everywhere. Requires `[sigma_finite (ΞΌ.trim hm)]`. * `ae_eq_condexp_of_forall_set_integral_eq`: an a.e. `m`-measurable function which verifies the equality of integrals is a.e. equal to `condexp`. ## Notations For a measure `ΞΌ` defined on a measurable space structure `m0`, another measurable space structure `m` with `hm : m ≀ m0` (a sub-sigma-algebra) and a function `f`, we define the notation * `ΞΌ[f|hm] = condexp hm ΞΌ f`. ## Implementation notes Most of the results in this file are valid for a second countable, borel, real normed space `F`. However, some lemmas also use `π•œ : is_R_or_C`: * `condexp_L2` is defined only for an `inner_product_space` for now, and we use `π•œ` for its field. * results about scalar multiplication are stated not only for `ℝ` but also for `π•œ` if we happen to have `normed_space π•œ F`. ## Tags conditional expectation, conditional expected value -/ noncomputable theory open topological_space measure_theory.Lp filter continuous_linear_map open_locale nnreal ennreal topological_space big_operators measure_theory namespace measure_theory /-- A function `f` verifies `ae_measurable' m f ΞΌ` if it is `ΞΌ`-a.e. equal to an `m`-measurable function. This is similar to `ae_measurable`, but the `measurable_space` structures used for the measurability statement and for the measure are different. -/ def ae_measurable' {Ξ± Ξ²} [measurable_space Ξ²] (m : measurable_space Ξ±) {m0 : measurable_space Ξ±} (f : Ξ± β†’ Ξ²) (ΞΌ : measure Ξ±) : Prop := βˆƒ g : Ξ± β†’ Ξ², measurable[m] g ∧ f =ᡐ[ΞΌ] g namespace ae_measurable' variables {Ξ± Ξ² π•œ : Type*} {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} [measurable_space Ξ²] [measurable_space π•œ] {f g : Ξ± β†’ Ξ²} lemma congr (hf : ae_measurable' m f ΞΌ) (hfg : f =ᡐ[ΞΌ] g) : ae_measurable' m g ΞΌ := by { obtain ⟨f', hf'_meas, hff'⟩ := hf, exact ⟨f', hf'_meas, hfg.symm.trans hff'⟩, } lemma add [has_add Ξ²] [has_measurable_addβ‚‚ Ξ²] (hf : ae_measurable' m f ΞΌ) (hg : ae_measurable' m g ΞΌ) : ae_measurable' m (f+g) ΞΌ := begin rcases hf with ⟨f', h_f'_meas, hff'⟩, rcases hg with ⟨g', h_g'_meas, hgg'⟩, exact ⟨f' + g', h_f'_meas.add h_g'_meas, hff'.add hgg'⟩, end lemma neg [has_neg Ξ²] [has_measurable_neg Ξ²] {f : Ξ± β†’ Ξ²} (hfm : ae_measurable' m f ΞΌ) : ae_measurable' m (-f) ΞΌ := begin rcases hfm with ⟨f', hf'_meas, hf_ae⟩, refine ⟨-f', hf'_meas.neg, hf_ae.mono (Ξ» x hx, _)⟩, simp_rw pi.neg_apply, rw hx, end lemma sub [has_sub Ξ²] [has_measurable_subβ‚‚ Ξ²] {f g : Ξ± β†’ Ξ²} (hfm : ae_measurable' m f ΞΌ) (hgm : ae_measurable' m g ΞΌ) : ae_measurable' m (f - g) ΞΌ := begin rcases hfm with ⟨f', hf'_meas, hf_ae⟩, rcases hgm with ⟨g', hg'_meas, hg_ae⟩, refine ⟨f'-g', hf'_meas.sub hg'_meas, hf_ae.mp (hg_ae.mono (Ξ» x hx1 hx2, _))⟩, simp_rw pi.sub_apply, rw [hx1, hx2], end lemma const_smul [has_scalar π•œ Ξ²] [has_measurable_smul π•œ Ξ²] (c : π•œ) (hf : ae_measurable' m f ΞΌ) : ae_measurable' m (c β€’ f) ΞΌ := begin rcases hf with ⟨f', h_f'_meas, hff'⟩, refine ⟨c β€’ f', h_f'_meas.const_smul c, _⟩, exact eventually_eq.fun_comp hff' (Ξ» x, c β€’ x), end lemma const_inner {π•œ} [is_R_or_C π•œ] [inner_product_space π•œ Ξ²] [second_countable_topology Ξ²] [opens_measurable_space Ξ²] {f : Ξ± β†’ Ξ²} (hfm : ae_measurable' m f ΞΌ) (c : Ξ²) : ae_measurable' m (Ξ» x, (inner c (f x) : π•œ)) ΞΌ := begin rcases hfm with ⟨f', hf'_meas, hf_ae⟩, refine ⟨λ x, (inner c (f' x) : π•œ), (@measurable_const _ _ _ m _).inner hf'_meas, hf_ae.mono (Ξ» x hx, _)⟩, dsimp only, rw hx, end /-- A m-measurable function almost everywhere equal to `f`. -/ def mk (f : Ξ± β†’ Ξ²) (hfm : ae_measurable' m f ΞΌ) : Ξ± β†’ Ξ² := hfm.some lemma measurable_mk {f : Ξ± β†’ Ξ²} (hfm : ae_measurable' m f ΞΌ) : measurable[m] (hfm.mk f) := hfm.some_spec.1 lemma ae_eq_mk {f : Ξ± β†’ Ξ²} (hfm : ae_measurable' m f ΞΌ) : f =ᡐ[ΞΌ] hfm.mk f := hfm.some_spec.2 lemma measurable_comp {Ξ³} [measurable_space Ξ³] {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} (hg : measurable g) (hf : ae_measurable' m f ΞΌ) : ae_measurable' m (g ∘ f) ΞΌ := ⟨λ x, g (hf.mk _ x), @measurable.comp _ _ _ m _ _ _ _ hg hf.measurable_mk, hf.ae_eq_mk.mono (Ξ» x hx, by rw [function.comp_apply, hx])⟩ end ae_measurable' lemma ae_measurable'_of_ae_measurable'_trim {Ξ± Ξ²} {m m0 m0' : measurable_space Ξ±} [measurable_space Ξ²] (hm0 : m0 ≀ m0') {ΞΌ : measure Ξ±} {f : Ξ± β†’ Ξ²} (hf : ae_measurable' m f (ΞΌ.trim hm0)) : ae_measurable' m f ΞΌ := by { obtain ⟨g, hg_meas, hfg⟩ := hf, exact ⟨g, hg_meas, ae_eq_of_ae_eq_trim hfg⟩, } lemma measurable.ae_measurable' {Ξ± Ξ²} {m m0 : measurable_space Ξ±} [measurable_space Ξ²] {ΞΌ : measure Ξ±} {f : Ξ± β†’ Ξ²} (hf : measurable[m] f) : ae_measurable' m f ΞΌ := ⟨f, hf, ae_eq_refl _⟩ lemma ae_eq_trim_iff_of_ae_measurable' {Ξ± Ξ²} [add_group Ξ²] [measurable_space Ξ²] [measurable_singleton_class Ξ²] [has_measurable_subβ‚‚ Ξ²] {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f g : Ξ± β†’ Ξ²} (hm : m ≀ m0) (hfm : ae_measurable' m f ΞΌ) (hgm : ae_measurable' m g ΞΌ) : hfm.mk f =ᡐ[ΞΌ.trim hm] hgm.mk g ↔ f =ᡐ[ΞΌ] g := (ae_eq_trim_iff hm hfm.measurable_mk hgm.measurable_mk).trans ⟨λ h, hfm.ae_eq_mk.trans (h.trans hgm.ae_eq_mk.symm), Ξ» h, hfm.ae_eq_mk.symm.trans (h.trans hgm.ae_eq_mk)⟩ variables {Ξ± Ξ² Ξ³ E E' F F' G G' H π•œ : Type*} {p : ℝβ‰₯0∞} [is_R_or_C π•œ] -- π•œ for ℝ or β„‚ [measurable_space Ξ²] -- Ξ² for a generic measurable space -- E for an inner product space [inner_product_space π•œ E] [measurable_space E] [borel_space E] [second_countable_topology E] -- E' for an inner product space on which we compute integrals [inner_product_space π•œ E'] [measurable_space E'] [borel_space E'] [second_countable_topology E'] [complete_space E'] [normed_space ℝ E'] -- F for a Lp submodule [normed_group F] [normed_space π•œ F] [measurable_space F] [borel_space F] [second_countable_topology F] -- F' for integrals on a Lp submodule [normed_group F'] [normed_space π•œ F'] [measurable_space F'] [borel_space F'] [second_countable_topology F'] [normed_space ℝ F'] [complete_space F'] -- G for a Lp add_subgroup [normed_group G] [measurable_space G] [borel_space G] [second_countable_topology G] -- G' for integrals on a Lp add_subgroup [normed_group G'] [measurable_space G'] [borel_space G'] [second_countable_topology G'] [normed_space ℝ G'] [complete_space G'] -- H for measurable space and normed group (hypotheses of mem_β„’p) [measurable_space H] [normed_group H] section Lp_meas /-! ## The subset `Lp_meas` of `Lp` functions a.e. measurable with respect to a sub-sigma-algebra -/ variables (F) /-- `Lp_meas_subgroup F m p ΞΌ` is the subspace of `Lp F p ΞΌ` containing functions `f` verifying `ae_measurable' m f ΞΌ`, i.e. functions which are `ΞΌ`-a.e. equal to an `m`-measurable function. -/ def Lp_meas_subgroup (m : measurable_space Ξ±) [measurable_space Ξ±] (p : ℝβ‰₯0∞) (ΞΌ : measure Ξ±) : add_subgroup (Lp F p ΞΌ) := { carrier := {f : (Lp F p ΞΌ) | ae_measurable' m f ΞΌ} , zero_mem' := ⟨(0 : Ξ± β†’ F), @measurable_zero _ Ξ± m _ _, Lp.coe_fn_zero _ _ _⟩, add_mem' := Ξ» f g hf hg, (hf.add hg).congr (Lp.coe_fn_add f g).symm, neg_mem' := Ξ» f hf, ae_measurable'.congr hf.neg (Lp.coe_fn_neg f).symm, } variables (π•œ) /-- `Lp_meas F π•œ m p ΞΌ` is the subspace of `Lp F p ΞΌ` containing functions `f` verifying `ae_measurable' m f ΞΌ`, i.e. functions which are `ΞΌ`-a.e. equal to an `m`-measurable function. -/ def Lp_meas [opens_measurable_space π•œ] (m : measurable_space Ξ±) [measurable_space Ξ±] (p : ℝβ‰₯0∞) (ΞΌ : measure Ξ±) : submodule π•œ (Lp F p ΞΌ) := { carrier := {f : (Lp F p ΞΌ) | ae_measurable' m f ΞΌ} , zero_mem' := ⟨(0 : Ξ± β†’ F), @measurable_zero _ Ξ± m _ _, Lp.coe_fn_zero _ _ _⟩, add_mem' := Ξ» f g hf hg, (hf.add hg).congr (Lp.coe_fn_add f g).symm, smul_mem' := Ξ» c f hf, (hf.const_smul c).congr (Lp.coe_fn_smul c f).symm, } variables {F π•œ} variables [opens_measurable_space π•œ] lemma mem_Lp_meas_subgroup_iff_ae_measurable' {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f : Lp F p ΞΌ} : f ∈ Lp_meas_subgroup F m p ΞΌ ↔ ae_measurable' m f ΞΌ := by rw [← add_subgroup.mem_carrier, Lp_meas_subgroup, set.mem_set_of_eq] lemma mem_Lp_meas_iff_ae_measurable' {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f : Lp F p ΞΌ} : f ∈ Lp_meas F π•œ m p ΞΌ ↔ ae_measurable' m f ΞΌ := by rw [← set_like.mem_coe, ← submodule.mem_carrier, Lp_meas, set.mem_set_of_eq] lemma Lp_meas.ae_measurable' {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} (f : Lp_meas F π•œ m p ΞΌ) : ae_measurable' m f ΞΌ := mem_Lp_meas_iff_ae_measurable'.mp f.mem lemma mem_Lp_meas_self {m0 : measurable_space Ξ±} (ΞΌ : measure Ξ±) (f : Lp F p ΞΌ) : f ∈ Lp_meas F π•œ m0 p ΞΌ := mem_Lp_meas_iff_ae_measurable'.mpr (Lp.ae_measurable f) lemma Lp_meas_subgroup_coe {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f : Lp_meas_subgroup F m p ΞΌ} : ⇑f = (f : Lp F p ΞΌ) := coe_fn_coe_base f lemma Lp_meas_coe {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {f : Lp_meas F π•œ m p ΞΌ} : ⇑f = (f : Lp F p ΞΌ) := coe_fn_coe_base f lemma mem_Lp_meas_indicator_const_Lp {m m0 : measurable_space Ξ±} (hm : m ≀ m0) {ΞΌ : measure Ξ±} {s : set Ξ±} (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) {c : F} : indicator_const_Lp p (hm s hs) hΞΌs c ∈ Lp_meas F π•œ m p ΞΌ := ⟨s.indicator (Ξ» x : Ξ±, c), (@measurable_const _ Ξ± _ m _).indicator hs, indicator_const_Lp_coe_fn⟩ section complete_subspace /-! ## The subspace `Lp_meas` is complete. We define an `isometric` between `Lp_meas_subgroup` and the `Lp` space corresponding to the measure `ΞΌ.trim hm`. As a consequence, the completeness of `Lp` implies completeness of `Lp_meas_subgroup` (and `Lp_meas`). -/ variables {ΞΉ : Type*} {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} /-- If `f` belongs to `Lp_meas_subgroup F m p ΞΌ`, then the measurable function it is almost everywhere equal to (given by `ae_measurable.mk`) belongs to `β„’p` for the measure `ΞΌ.trim hm`. -/ lemma mem_β„’p_trim_of_mem_Lp_meas_subgroup (hm : m ≀ m0) (f : Lp F p ΞΌ) (hf_meas : f ∈ Lp_meas_subgroup F m p ΞΌ) : mem_β„’p (mem_Lp_meas_subgroup_iff_ae_measurable'.mp hf_meas).some p (ΞΌ.trim hm) := begin have hf : ae_measurable' m f ΞΌ, from (mem_Lp_meas_subgroup_iff_ae_measurable'.mp hf_meas), let g := hf.some, obtain ⟨hg, hfg⟩ := hf.some_spec, change mem_β„’p g p (ΞΌ.trim hm), refine ⟨hg.ae_measurable, _⟩, have h_snorm_fg : snorm g p (ΞΌ.trim hm) = snorm f p ΞΌ, by { rw snorm_trim hm hg, exact snorm_congr_ae hfg.symm, }, rw h_snorm_fg, exact Lp.snorm_lt_top f, end /-- If `f` belongs to `Lp` for the measure `ΞΌ.trim hm`, then it belongs to the subgroup `Lp_meas_subgroup F m p ΞΌ`. -/ lemma mem_Lp_meas_subgroup_to_Lp_of_trim (hm : m ≀ m0) (f : Lp F p (ΞΌ.trim hm)) : (mem_β„’p_of_mem_β„’p_trim hm (Lp.mem_β„’p f)).to_Lp f ∈ Lp_meas_subgroup F m p ΞΌ := begin let hf_mem_β„’p := mem_β„’p_of_mem_β„’p_trim hm (Lp.mem_β„’p f), rw mem_Lp_meas_subgroup_iff_ae_measurable', refine ae_measurable'.congr _ (mem_β„’p.coe_fn_to_Lp hf_mem_β„’p).symm, refine ae_measurable'_of_ae_measurable'_trim hm _, exact (Lp.ae_measurable f), end variables (F p ΞΌ) /-- Map from `Lp_meas_subgroup` to `Lp F p (ΞΌ.trim hm)`. -/ def Lp_meas_subgroup_to_Lp_trim (hm : m ≀ m0) (f : Lp_meas_subgroup F m p ΞΌ) : Lp F p (ΞΌ.trim hm) := mem_β„’p.to_Lp (mem_Lp_meas_subgroup_iff_ae_measurable'.mp f.mem).some (mem_β„’p_trim_of_mem_Lp_meas_subgroup hm f f.mem) variables (π•œ) /-- Map from `Lp_meas` to `Lp F p (ΞΌ.trim hm)`. -/ def Lp_meas_to_Lp_trim (hm : m ≀ m0) (f : Lp_meas F π•œ m p ΞΌ) : Lp F p (ΞΌ.trim hm) := mem_β„’p.to_Lp (mem_Lp_meas_iff_ae_measurable'.mp f.mem).some (mem_β„’p_trim_of_mem_Lp_meas_subgroup hm f f.mem) variables {π•œ} /-- Map from `Lp F p (ΞΌ.trim hm)` to `Lp_meas_subgroup`, inverse of `Lp_meas_subgroup_to_Lp_trim`. -/ def Lp_trim_to_Lp_meas_subgroup (hm : m ≀ m0) (f : Lp F p (ΞΌ.trim hm)) : Lp_meas_subgroup F m p ΞΌ := ⟨(mem_β„’p_of_mem_β„’p_trim hm (Lp.mem_β„’p f)).to_Lp f, mem_Lp_meas_subgroup_to_Lp_of_trim hm f⟩ variables (π•œ) /-- Map from `Lp F p (ΞΌ.trim hm)` to `Lp_meas`, inverse of `Lp_meas_to_Lp_trim`. -/ def Lp_trim_to_Lp_meas (hm : m ≀ m0) (f : Lp F p (ΞΌ.trim hm)) : Lp_meas F π•œ m p ΞΌ := ⟨(mem_β„’p_of_mem_β„’p_trim hm (Lp.mem_β„’p f)).to_Lp f, mem_Lp_meas_subgroup_to_Lp_of_trim hm f⟩ variables {F π•œ p ΞΌ} lemma Lp_meas_subgroup_to_Lp_trim_ae_eq (hm : m ≀ m0) (f : Lp_meas_subgroup F m p ΞΌ) : Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f =ᡐ[ΞΌ] f := (ae_eq_of_ae_eq_trim (mem_β„’p.coe_fn_to_Lp (mem_β„’p_trim_of_mem_Lp_meas_subgroup hm ↑f f.mem))).trans (mem_Lp_meas_subgroup_iff_ae_measurable'.mp f.mem).some_spec.2.symm lemma Lp_trim_to_Lp_meas_subgroup_ae_eq (hm : m ≀ m0) (f : Lp F p (ΞΌ.trim hm)) : Lp_trim_to_Lp_meas_subgroup F p ΞΌ hm f =ᡐ[ΞΌ] f := mem_β„’p.coe_fn_to_Lp _ lemma Lp_meas_to_Lp_trim_ae_eq (hm : m ≀ m0) (f : Lp_meas F π•œ m p ΞΌ) : Lp_meas_to_Lp_trim F π•œ p ΞΌ hm f =ᡐ[ΞΌ] f := (ae_eq_of_ae_eq_trim (mem_β„’p.coe_fn_to_Lp (mem_β„’p_trim_of_mem_Lp_meas_subgroup hm ↑f f.mem))).trans (mem_Lp_meas_subgroup_iff_ae_measurable'.mp f.mem).some_spec.2.symm lemma Lp_trim_to_Lp_meas_ae_eq (hm : m ≀ m0) (f : Lp F p (ΞΌ.trim hm)) : Lp_trim_to_Lp_meas F π•œ p ΞΌ hm f =ᡐ[ΞΌ] f := mem_β„’p.coe_fn_to_Lp _ /-- `Lp_trim_to_Lp_meas_subgroup` is a right inverse of `Lp_meas_subgroup_to_Lp_trim`. -/ lemma Lp_meas_subgroup_to_Lp_trim_right_inv (hm : m ≀ m0) : function.right_inverse (Lp_trim_to_Lp_meas_subgroup F p ΞΌ hm) (Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm) := begin intro f, ext1, refine ae_eq_trim_of_measurable hm (Lp.measurable _) (Lp.measurable _) _, exact (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _).trans (Lp_trim_to_Lp_meas_subgroup_ae_eq hm _), end /-- `Lp_trim_to_Lp_meas_subgroup` is a left inverse of `Lp_meas_subgroup_to_Lp_trim`. -/ lemma Lp_meas_subgroup_to_Lp_trim_left_inv (hm : m ≀ m0) : function.left_inverse (Lp_trim_to_Lp_meas_subgroup F p ΞΌ hm) (Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm) := begin intro f, ext1, ext1, rw ← Lp_meas_subgroup_coe, exact (Lp_trim_to_Lp_meas_subgroup_ae_eq hm _).trans (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _), end lemma Lp_meas_subgroup_to_Lp_trim_add (hm : m ≀ m0) (f g : Lp_meas_subgroup F m p ΞΌ) : Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm (f + g) = Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f + Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm g := begin ext1, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, refine ae_eq_trim_of_measurable hm (Lp.measurable _) _ _, { exact (Lp.measurable _).add (Lp.measurable _), }, refine (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _).trans _, refine eventually_eq.trans _ (eventually_eq.add (Lp_meas_subgroup_to_Lp_trim_ae_eq hm f).symm (Lp_meas_subgroup_to_Lp_trim_ae_eq hm g).symm), refine (Lp.coe_fn_add _ _).trans _, simp_rw Lp_meas_subgroup_coe, exact eventually_of_forall (Ξ» x, by refl), end lemma Lp_meas_subgroup_to_Lp_trim_neg (hm : m ≀ m0) (f : Lp_meas_subgroup F m p ΞΌ) : Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm (-f) = -Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f := begin ext1, refine eventually_eq.trans _ (Lp.coe_fn_neg _).symm, refine ae_eq_trim_of_measurable hm (Lp.measurable _) _ _, { exact @measurable.neg _ _ _ _ _ m _ (Lp.measurable _), }, refine (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _).trans _, refine eventually_eq.trans _ (eventually_eq.neg (Lp_meas_subgroup_to_Lp_trim_ae_eq hm f).symm), refine (Lp.coe_fn_neg _).trans _, simp_rw Lp_meas_subgroup_coe, exact eventually_of_forall (Ξ» x, by refl), end lemma Lp_meas_subgroup_to_Lp_trim_sub (hm : m ≀ m0) (f g : Lp_meas_subgroup F m p ΞΌ) : Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm (f - g) = Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f - Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm g := by rw [sub_eq_add_neg, sub_eq_add_neg, Lp_meas_subgroup_to_Lp_trim_add, Lp_meas_subgroup_to_Lp_trim_neg] lemma Lp_meas_to_Lp_trim_smul (hm : m ≀ m0) (c : π•œ) (f : Lp_meas F π•œ m p ΞΌ) : Lp_meas_to_Lp_trim F π•œ p ΞΌ hm (c β€’ f) = c β€’ Lp_meas_to_Lp_trim F π•œ p ΞΌ hm f := begin ext1, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, refine ae_eq_trim_of_measurable hm (Lp.measurable _) _ _, { exact (Lp.measurable _).const_smul c, }, refine (Lp_meas_to_Lp_trim_ae_eq hm _).trans _, refine (Lp.coe_fn_smul _ _).trans _, refine (Lp_meas_to_Lp_trim_ae_eq hm f).mono (Ξ» x hx, _), rw [pi.smul_apply, pi.smul_apply, hx], refl, end /-- `Lp_meas_subgroup_to_Lp_trim` preserves the norm. -/ lemma Lp_meas_subgroup_to_Lp_trim_norm_map [hp : fact (1 ≀ p)] (hm : m ≀ m0) (f : Lp_meas_subgroup F m p ΞΌ) : βˆ₯Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm fβˆ₯ = βˆ₯fβˆ₯ := begin rw [Lp.norm_def, snorm_trim hm (Lp.measurable _)], swap, { apply_instance, }, rw [snorm_congr_ae (Lp_meas_subgroup_to_Lp_trim_ae_eq hm _), Lp_meas_subgroup_coe, ← Lp.norm_def], congr, end lemma isometry_Lp_meas_subgroup_to_Lp_trim [hp : fact (1 ≀ p)] (hm : m ≀ m0) : isometry (Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm) := begin rw isometry_emetric_iff_metric, intros f g, rw [dist_eq_norm, ← Lp_meas_subgroup_to_Lp_trim_sub, Lp_meas_subgroup_to_Lp_trim_norm_map, dist_eq_norm], end variables (F p ΞΌ) /-- `Lp_meas_subgroup` and `Lp F p (ΞΌ.trim hm)` are isometric. -/ def Lp_meas_subgroup_to_Lp_trim_iso [hp : fact (1 ≀ p)] (hm : m ≀ m0) : Lp_meas_subgroup F m p ΞΌ ≃ᡒ Lp F p (ΞΌ.trim hm) := { to_fun := Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm, inv_fun := Lp_trim_to_Lp_meas_subgroup F p ΞΌ hm, left_inv := Lp_meas_subgroup_to_Lp_trim_left_inv hm, right_inv := Lp_meas_subgroup_to_Lp_trim_right_inv hm, isometry_to_fun := isometry_Lp_meas_subgroup_to_Lp_trim hm, } variables (π•œ) /-- `Lp_meas_subgroup` and `Lp_meas` are isometric. -/ def Lp_meas_subgroup_to_Lp_meas_iso [hp : fact (1 ≀ p)] : Lp_meas_subgroup F m p ΞΌ ≃ᡒ Lp_meas F π•œ m p ΞΌ := isometric.refl (Lp_meas_subgroup F m p ΞΌ) /-- `Lp_meas` and `Lp F p (ΞΌ.trim hm)` are isometric, with a linear equivalence. -/ def Lp_meas_to_Lp_trim_lie [hp : fact (1 ≀ p)] (hm : m ≀ m0) : Lp_meas F π•œ m p ΞΌ ≃ₗᡒ[π•œ] Lp F p (ΞΌ.trim hm) := { to_fun := Lp_meas_to_Lp_trim F π•œ p ΞΌ hm, inv_fun := Lp_trim_to_Lp_meas F π•œ p ΞΌ hm, left_inv := Lp_meas_subgroup_to_Lp_trim_left_inv hm, right_inv := Lp_meas_subgroup_to_Lp_trim_right_inv hm, map_add' := Lp_meas_subgroup_to_Lp_trim_add hm, map_smul' := Lp_meas_to_Lp_trim_smul hm, norm_map' := Lp_meas_subgroup_to_Lp_trim_norm_map hm, } variables {F π•œ p ΞΌ} instance [hm : fact (m ≀ m0)] [complete_space F] [hp : fact (1 ≀ p)] : complete_space (Lp_meas_subgroup F m p ΞΌ) := by { rw (Lp_meas_subgroup_to_Lp_trim_iso F p ΞΌ hm.elim).complete_space_iff, apply_instance, } instance [hm : fact (m ≀ m0)] [complete_space F] [hp : fact (1 ≀ p)] : complete_space (Lp_meas F π•œ m p ΞΌ) := by { rw (Lp_meas_subgroup_to_Lp_meas_iso F π•œ p ΞΌ).symm.complete_space_iff, apply_instance, } lemma is_complete_ae_measurable' [hp : fact (1 ≀ p)] [complete_space F] (hm : m ≀ m0) : is_complete {f : Lp F p ΞΌ | ae_measurable' m f ΞΌ} := begin rw ← complete_space_coe_iff_is_complete, haveI : fact (m ≀ m0) := ⟨hm⟩, change complete_space (Lp_meas_subgroup F m p ΞΌ), apply_instance, end lemma is_closed_ae_measurable' [hp : fact (1 ≀ p)] [complete_space F] (hm : m ≀ m0) : is_closed {f : Lp F p ΞΌ | ae_measurable' m f ΞΌ} := is_complete.is_closed (is_complete_ae_measurable' hm) end complete_subspace section strongly_measurable variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} /-- We do not get `ae_fin_strongly_measurable f (ΞΌ.trim hm)`, since we don't have `f =ᡐ[ΞΌ.trim hm] Lp_meas_to_Lp_trim F π•œ p ΞΌ hm f` but only the weaker `f =ᡐ[ΞΌ] Lp_meas_to_Lp_trim F π•œ p ΞΌ hm f`. -/ lemma Lp_meas.ae_fin_strongly_measurable' (hm : m ≀ m0) (f : Lp_meas F π•œ m p ΞΌ) (hp_ne_zero : p β‰  0) (hp_ne_top : p β‰  ∞) : βˆƒ g, fin_strongly_measurable g (ΞΌ.trim hm) ∧ f =ᡐ[ΞΌ] g := ⟨Lp_meas_subgroup_to_Lp_trim F p ΞΌ hm f, Lp.fin_strongly_measurable _ hp_ne_zero hp_ne_top, (Lp_meas_subgroup_to_Lp_trim_ae_eq hm f).symm⟩ end strongly_measurable end Lp_meas section uniqueness_of_conditional_expectation /-! ## Uniqueness of the conditional expectation -/ variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} lemma Lp_meas.ae_eq_zero_of_forall_set_integral_eq_zero (hm : m ≀ m0) (f : Lp_meas E' π•œ m p ΞΌ) (hp_ne_zero : p β‰  0) (hp_ne_top : p β‰  ∞) (hf_int_finite : βˆ€ s, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ integrable_on f s ΞΌ) (hf_zero : βˆ€ s : set Ξ±, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ ∫ x in s, f x βˆ‚ΞΌ = 0) : f =ᡐ[ΞΌ] 0 := begin obtain ⟨g, hg_sm, hfg⟩ := Lp_meas.ae_fin_strongly_measurable' hm f hp_ne_zero hp_ne_top, refine hfg.trans _, refine ae_eq_zero_of_forall_set_integral_eq_of_fin_strongly_measurable_trim hm _ _ hg_sm, { intros s hs hΞΌs, have hfg_restrict : f =ᡐ[ΞΌ.restrict s] g, from ae_restrict_of_ae hfg, rw [integrable_on, integrable_congr hfg_restrict.symm], exact hf_int_finite s hs hΞΌs, }, { intros s hs hΞΌs, have hfg_restrict : f =ᡐ[ΞΌ.restrict s] g, from ae_restrict_of_ae hfg, rw integral_congr_ae hfg_restrict.symm, exact hf_zero s hs hΞΌs, }, end include π•œ lemma Lp.ae_eq_zero_of_forall_set_integral_eq_zero' (hm : m ≀ m0) (f : Lp E' p ΞΌ) (hp_ne_zero : p β‰  0) (hp_ne_top : p β‰  ∞) (hf_int_finite : βˆ€ s, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ integrable_on f s ΞΌ) (hf_zero : βˆ€ s : set Ξ±, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ ∫ x in s, f x βˆ‚ΞΌ = 0) (hf_meas : ae_measurable' m f ΞΌ) : f =ᡐ[ΞΌ] 0 := begin let f_meas : Lp_meas E' π•œ m p ΞΌ := ⟨f, hf_meas⟩, have hf_f_meas : f =ᡐ[ΞΌ] f_meas, by simp only [coe_fn_coe_base', subtype.coe_mk], refine hf_f_meas.trans _, refine Lp_meas.ae_eq_zero_of_forall_set_integral_eq_zero hm f_meas hp_ne_zero hp_ne_top _ _, { intros s hs hΞΌs, have hfg_restrict : f =ᡐ[ΞΌ.restrict s] f_meas, from ae_restrict_of_ae hf_f_meas, rw [integrable_on, integrable_congr hfg_restrict.symm], exact hf_int_finite s hs hΞΌs, }, { intros s hs hΞΌs, have hfg_restrict : f =ᡐ[ΞΌ.restrict s] f_meas, from ae_restrict_of_ae hf_f_meas, rw integral_congr_ae hfg_restrict.symm, exact hf_zero s hs hΞΌs, }, end /-- **Uniqueness of the conditional expectation** -/ lemma Lp.ae_eq_of_forall_set_integral_eq' (hm : m ≀ m0) (f g : Lp E' p ΞΌ) (hp_ne_zero : p β‰  0) (hp_ne_top : p β‰  ∞) (hf_int_finite : βˆ€ s, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ integrable_on f s ΞΌ) (hg_int_finite : βˆ€ s, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ integrable_on g s ΞΌ) (hfg : βˆ€ s : set Ξ±, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ ∫ x in s, f x βˆ‚ΞΌ = ∫ x in s, g x βˆ‚ΞΌ) (hf_meas : ae_measurable' m f ΞΌ) (hg_meas : ae_measurable' m g ΞΌ) : f =ᡐ[ΞΌ] g := begin suffices h_sub : ⇑(f-g) =ᡐ[ΞΌ] 0, by { rw ← sub_ae_eq_zero, exact (Lp.coe_fn_sub f g).symm.trans h_sub, }, have hfg' : βˆ€ s : set Ξ±, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ ∫ x in s, (f - g) x βˆ‚ΞΌ = 0, { intros s hs hΞΌs, rw integral_congr_ae (ae_restrict_of_ae (Lp.coe_fn_sub f g)), rw integral_sub' (hf_int_finite s hs hΞΌs) (hg_int_finite s hs hΞΌs), exact sub_eq_zero.mpr (hfg s hs hΞΌs), }, have hfg_int : βˆ€ s, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ integrable_on ⇑(f-g) s ΞΌ, { intros s hs hΞΌs, rw [integrable_on, integrable_congr (ae_restrict_of_ae (Lp.coe_fn_sub f g))], exact (hf_int_finite s hs hΞΌs).sub (hg_int_finite s hs hΞΌs), }, have hfg_meas : ae_measurable' m ⇑(f - g) ΞΌ, from ae_measurable'.congr (hf_meas.sub hg_meas) (Lp.coe_fn_sub f g).symm, exact Lp.ae_eq_zero_of_forall_set_integral_eq_zero' hm (f-g) hp_ne_zero hp_ne_top hfg_int hfg' hfg_meas, end omit π•œ lemma ae_eq_of_forall_set_integral_eq_of_sigma_finite' (hm : m ≀ m0) [sigma_finite (ΞΌ.trim hm)] {f g : Ξ± β†’ F'} (hf_int_finite : βˆ€ s, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ integrable_on f s ΞΌ) (hg_int_finite : βˆ€ s, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ integrable_on g s ΞΌ) (hfg_eq : βˆ€ s : set Ξ±, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ ∫ x in s, f x βˆ‚ΞΌ = ∫ x in s, g x βˆ‚ΞΌ) (hfm : ae_measurable' m f ΞΌ) (hgm : ae_measurable' m g ΞΌ) : f =ᡐ[ΞΌ] g := begin rw ← ae_eq_trim_iff_of_ae_measurable' hm hfm hgm, have hf_mk_int_finite : βˆ€ s, measurable_set[m] s β†’ ΞΌ.trim hm s < ∞ β†’ @integrable_on _ _ m _ _ (hfm.mk f) s (ΞΌ.trim hm), { intros s hs hΞΌs, rw trim_measurable_set_eq hm hs at hΞΌs, rw [integrable_on, restrict_trim hm _ hs], refine integrable.trim hm _ hfm.measurable_mk, exact integrable.congr (hf_int_finite s hs hΞΌs) (ae_restrict_of_ae hfm.ae_eq_mk), }, have hg_mk_int_finite : βˆ€ s, measurable_set[m] s β†’ ΞΌ.trim hm s < ∞ β†’ @integrable_on _ _ m _ _ (hgm.mk g) s (ΞΌ.trim hm), { intros s hs hΞΌs, rw trim_measurable_set_eq hm hs at hΞΌs, rw [integrable_on, restrict_trim hm _ hs], refine integrable.trim hm _ hgm.measurable_mk, exact integrable.congr (hg_int_finite s hs hΞΌs) (ae_restrict_of_ae hgm.ae_eq_mk), }, have hfg_mk_eq : βˆ€ s : set Ξ±, measurable_set[m] s β†’ ΞΌ.trim hm s < ∞ β†’ ∫ x in s, (hfm.mk f x) βˆ‚(ΞΌ.trim hm) = ∫ x in s, (hgm.mk g x) βˆ‚(ΞΌ.trim hm), { intros s hs hΞΌs, rw trim_measurable_set_eq hm hs at hΞΌs, rw [restrict_trim hm _ hs, ← integral_trim hm hfm.measurable_mk, ← integral_trim hm hgm.measurable_mk, integral_congr_ae (ae_restrict_of_ae hfm.ae_eq_mk.symm), integral_congr_ae (ae_restrict_of_ae hgm.ae_eq_mk.symm)], exact hfg_eq s hs hΞΌs, }, exact ae_eq_of_forall_set_integral_eq_of_sigma_finite hf_mk_int_finite hg_mk_int_finite hfg_mk_eq, end end uniqueness_of_conditional_expectation section integral_norm_le variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {s : set Ξ±} /-- Let `m` be a sub-Οƒ-algebra of `m0`, `f` a `m0`-measurable function and `g` a `m`-measurable function, such that their integrals coincide on `m`-measurable sets with finite measure. Then `∫ x in s, βˆ₯g xβˆ₯ βˆ‚ΞΌ ≀ ∫ x in s, βˆ₯f xβˆ₯ βˆ‚ΞΌ` on all `m`-measurable sets with finite measure. -/ lemma integral_norm_le_of_forall_fin_meas_integral_eq (hm : m ≀ m0) {f g : Ξ± β†’ ℝ} (hf : measurable f) (hfi : integrable_on f s ΞΌ) (hg : measurable[m] g) (hgi : integrable_on g s ΞΌ) (hgf : βˆ€ t, measurable_set[m] t β†’ ΞΌ t < ∞ β†’ ∫ x in t, g x βˆ‚ΞΌ = ∫ x in t, f x βˆ‚ΞΌ) (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) : ∫ x in s, βˆ₯g xβˆ₯ βˆ‚ΞΌ ≀ ∫ x in s, βˆ₯f xβˆ₯ βˆ‚ΞΌ := begin rw [integral_norm_eq_pos_sub_neg (hg.mono hm le_rfl) hgi, integral_norm_eq_pos_sub_neg hf hfi], have h_meas_nonneg_g : measurable_set[m] {x | 0 ≀ g x}, from @measurable_set_le _ Ξ± _ _ _ m _ _ _ _ g (@measurable_const _ Ξ± _ m _) hg, have h_meas_nonneg_f : measurable_set {x | 0 ≀ f x}, from measurable_set_le measurable_const hf, have h_meas_nonpos_g : measurable_set[m] {x | g x ≀ 0}, from @measurable_set_le _ Ξ± _ _ _ m _ _ _ g _ hg (@measurable_const _ Ξ± _ m _), have h_meas_nonpos_f : measurable_set {x | f x ≀ 0}, from measurable_set_le hf measurable_const, refine sub_le_sub _ _, { rw [measure.restrict_restrict (hm _ h_meas_nonneg_g), measure.restrict_restrict h_meas_nonneg_f, hgf _ (@measurable_set.inter Ξ± m _ _ h_meas_nonneg_g hs) ((measure_mono (set.inter_subset_right _ _)).trans_lt (lt_top_iff_ne_top.mpr hΞΌs)), ← measure.restrict_restrict (hm _ h_meas_nonneg_g), ← measure.restrict_restrict h_meas_nonneg_f], exact set_integral_le_nonneg (hm _ h_meas_nonneg_g) hf hfi, }, { rw [measure.restrict_restrict (hm _ h_meas_nonpos_g), measure.restrict_restrict h_meas_nonpos_f, hgf _ (@measurable_set.inter Ξ± m _ _ h_meas_nonpos_g hs) ((measure_mono (set.inter_subset_right _ _)).trans_lt (lt_top_iff_ne_top.mpr hΞΌs)), ← measure.restrict_restrict (hm _ h_meas_nonpos_g), ← measure.restrict_restrict h_meas_nonpos_f], exact set_integral_nonpos_le (hm _ h_meas_nonpos_g) hf hfi, }, end /-- Let `m` be a sub-Οƒ-algebra of `m0`, `f` a `m0`-measurable function and `g` a `m`-measurable function, such that their integrals coincide on `m`-measurable sets with finite measure. Then `∫⁻ x in s, βˆ₯g xβˆ₯β‚Š βˆ‚ΞΌ ≀ ∫⁻ x in s, βˆ₯f xβˆ₯β‚Š βˆ‚ΞΌ` on all `m`-measurable sets with finite measure. -/ lemma lintegral_nnnorm_le_of_forall_fin_meas_integral_eq (hm : m ≀ m0) {f g : Ξ± β†’ ℝ} (hf : measurable f) (hfi : integrable_on f s ΞΌ) (hg : measurable[m] g) (hgi : integrable_on g s ΞΌ) (hgf : βˆ€ t, measurable_set[m] t β†’ ΞΌ t < ∞ β†’ ∫ x in t, g x βˆ‚ΞΌ = ∫ x in t, f x βˆ‚ΞΌ) (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) : ∫⁻ x in s, βˆ₯g xβˆ₯β‚Š βˆ‚ΞΌ ≀ ∫⁻ x in s, βˆ₯f xβˆ₯β‚Š βˆ‚ΞΌ := begin rw [← of_real_integral_norm_eq_lintegral_nnnorm hfi, ← of_real_integral_norm_eq_lintegral_nnnorm hgi, ennreal.of_real_le_of_real_iff], { exact integral_norm_le_of_forall_fin_meas_integral_eq hm hf hfi hg hgi hgf hs hΞΌs, }, { exact integral_nonneg (Ξ» x, norm_nonneg _), }, end end integral_norm_le /-! ## Conditional expectation in L2 We define a conditional expectation in `L2`: it is the orthogonal projection on the subspace `Lp_meas`. -/ section condexp_L2 variables [complete_space E] {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {s t : set Ξ±} local notation `βŸͺ`x`, `y`⟫` := @inner π•œ E _ x y local notation `βŸͺ`x`, `y`βŸ«β‚‚` := @inner π•œ (Ξ± β†’β‚‚[ΞΌ] E) _ x y variables (π•œ) /-- Conditional expectation of a function in L2 with respect to a sigma-algebra -/ def condexp_L2 (hm : m ≀ m0) : (Ξ± β†’β‚‚[ΞΌ] E) β†’L[π•œ] (Lp_meas E π•œ m 2 ΞΌ) := @orthogonal_projection π•œ (Ξ± β†’β‚‚[ΞΌ] E) _ _ (Lp_meas E π•œ m 2 ΞΌ) (by { haveI : fact (m ≀ m0) := ⟨hm⟩, exact infer_instance, }) variables {π•œ} lemma ae_measurable'_condexp_L2 (hm : m ≀ m0) (f : Ξ± β†’β‚‚[ΞΌ] E) : ae_measurable' m (condexp_L2 π•œ hm f) ΞΌ := Lp_meas.ae_measurable' _ lemma integrable_on_condexp_L2_of_measure_ne_top (hm : m ≀ m0) (hΞΌs : ΞΌ s β‰  ∞) (f : Ξ± β†’β‚‚[ΞΌ] E) : integrable_on (condexp_L2 π•œ hm f) s ΞΌ := integrable_on_Lp_of_measure_ne_top ((condexp_L2 π•œ hm f) : Ξ± β†’β‚‚[ΞΌ] E) fact_one_le_two_ennreal.elim hΞΌs lemma integrable_condexp_L2_of_is_finite_measure (hm : m ≀ m0) [is_finite_measure ΞΌ] {f : Ξ± β†’β‚‚[ΞΌ] E} : integrable (condexp_L2 π•œ hm f) ΞΌ := integrable_on_univ.mp $ integrable_on_condexp_L2_of_measure_ne_top hm (measure_ne_top _ _) f lemma norm_condexp_L2_le_one (hm : m ≀ m0) : βˆ₯@condexp_L2 Ξ± E π•œ _ _ _ _ _ _ _ _ ΞΌ hmβˆ₯ ≀ 1 := by { haveI : fact (m ≀ m0) := ⟨hm⟩, exact orthogonal_projection_norm_le _, } lemma norm_condexp_L2_le (hm : m ≀ m0) (f : Ξ± β†’β‚‚[ΞΌ] E) : βˆ₯condexp_L2 π•œ hm fβˆ₯ ≀ βˆ₯fβˆ₯ := ((@condexp_L2 _ E π•œ _ _ _ _ _ _ _ _ ΞΌ hm).le_op_norm f).trans (mul_le_of_le_one_left (norm_nonneg _) (norm_condexp_L2_le_one hm)) lemma snorm_condexp_L2_le (hm : m ≀ m0) (f : Ξ± β†’β‚‚[ΞΌ] E) : snorm (condexp_L2 π•œ hm f) 2 ΞΌ ≀ snorm f 2 ΞΌ := begin rw [Lp_meas_coe, ← ennreal.to_real_le_to_real (Lp.snorm_ne_top _) (Lp.snorm_ne_top _), ← Lp.norm_def, ← Lp.norm_def, submodule.norm_coe], exact norm_condexp_L2_le hm f, end lemma norm_condexp_L2_coe_le (hm : m ≀ m0) (f : Ξ± β†’β‚‚[ΞΌ] E) : βˆ₯(condexp_L2 π•œ hm f : Ξ± β†’β‚‚[ΞΌ] E)βˆ₯ ≀ βˆ₯fβˆ₯ := begin rw [Lp.norm_def, Lp.norm_def, ← Lp_meas_coe], refine (ennreal.to_real_le_to_real _ (Lp.snorm_ne_top _)).mpr (snorm_condexp_L2_le hm f), exact Lp.snorm_ne_top _, end lemma inner_condexp_L2_left_eq_right (hm : m ≀ m0) {f g : Ξ± β†’β‚‚[ΞΌ] E} : βŸͺ(condexp_L2 π•œ hm f : Ξ± β†’β‚‚[ΞΌ] E), gβŸ«β‚‚ = βŸͺf, (condexp_L2 π•œ hm g : Ξ± β†’β‚‚[ΞΌ] E)βŸ«β‚‚ := by { haveI : fact (m ≀ m0) := ⟨hm⟩, exact inner_orthogonal_projection_left_eq_right _ f g, } lemma condexp_L2_indicator_of_measurable (hm : m ≀ m0) (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) (c : E) : (condexp_L2 π•œ hm (indicator_const_Lp 2 (hm s hs) hΞΌs c) : Ξ± β†’β‚‚[ΞΌ] E) = indicator_const_Lp 2 (hm s hs) hΞΌs c := begin rw condexp_L2, haveI : fact (m ≀ m0) := ⟨hm⟩, have h_mem : indicator_const_Lp 2 (hm s hs) hΞΌs c ∈ Lp_meas E π•œ m 2 ΞΌ, from mem_Lp_meas_indicator_const_Lp hm hs hΞΌs, let ind := (⟨indicator_const_Lp 2 (hm s hs) hΞΌs c, h_mem⟩ : Lp_meas E π•œ m 2 ΞΌ), have h_coe_ind : (ind : Ξ± β†’β‚‚[ΞΌ] E) = indicator_const_Lp 2 (hm s hs) hΞΌs c, by refl, have h_orth_mem := orthogonal_projection_mem_subspace_eq_self ind, rw [← h_coe_ind, h_orth_mem], end lemma inner_condexp_L2_eq_inner_fun (hm : m ≀ m0) (f g : Ξ± β†’β‚‚[ΞΌ] E) (hg : ae_measurable' m g ΞΌ) : βŸͺ(condexp_L2 π•œ hm f : Ξ± β†’β‚‚[ΞΌ] E), gβŸ«β‚‚ = βŸͺf, gβŸ«β‚‚ := begin symmetry, rw [← sub_eq_zero, ← inner_sub_left, condexp_L2], simp only [mem_Lp_meas_iff_ae_measurable'.mpr hg, orthogonal_projection_inner_eq_zero], end section real variables {hm : m ≀ m0} lemma integral_condexp_L2_eq_of_fin_meas_real (f : Lp π•œ 2 ΞΌ) (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) : ∫ x in s, condexp_L2 π•œ hm f x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ := begin rw ← L2.inner_indicator_const_Lp_one (hm s hs) hΞΌs, have h_eq_inner : ∫ x in s, condexp_L2 π•œ hm f x βˆ‚ΞΌ = inner (indicator_const_Lp 2 (hm s hs) hΞΌs (1 : π•œ)) (condexp_L2 π•œ hm f), { rw L2.inner_indicator_const_Lp_one (hm s hs) hΞΌs, congr, }, rw [h_eq_inner, ← inner_condexp_L2_left_eq_right, condexp_L2_indicator_of_measurable hm hs hΞΌs], end lemma lintegral_nnnorm_condexp_L2_le (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) (f : Lp ℝ 2 ΞΌ) : ∫⁻ x in s, βˆ₯condexp_L2 ℝ hm f xβˆ₯β‚Š βˆ‚ΞΌ ≀ ∫⁻ x in s, βˆ₯f xβˆ₯β‚Š βˆ‚ΞΌ := begin let h_meas := Lp_meas.ae_measurable' (condexp_L2 ℝ hm f), let g := h_meas.some, have hg_meas : measurable[m] g, from h_meas.some_spec.1, have hg_eq : g =ᡐ[ΞΌ] condexp_L2 ℝ hm f, from h_meas.some_spec.2.symm, have hg_eq_restrict : g =ᡐ[ΞΌ.restrict s] condexp_L2 ℝ hm f, from ae_restrict_of_ae hg_eq, have hg_nnnorm_eq : (Ξ» x, (βˆ₯g xβˆ₯β‚Š : ℝβ‰₯0∞)) =ᡐ[ΞΌ.restrict s] (Ξ» x, (βˆ₯condexp_L2 ℝ hm f xβˆ₯β‚Š : ℝβ‰₯0∞)), { refine hg_eq_restrict.mono (Ξ» x hx, _), dsimp only, rw hx, }, rw lintegral_congr_ae hg_nnnorm_eq.symm, refine lintegral_nnnorm_le_of_forall_fin_meas_integral_eq hm (Lp.measurable f) _ _ _ _ hs hΞΌs, { exact integrable_on_Lp_of_measure_ne_top f fact_one_le_two_ennreal.elim hΞΌs, }, { exact hg_meas, }, { rw [integrable_on, integrable_congr hg_eq_restrict], exact integrable_on_condexp_L2_of_measure_ne_top hm hΞΌs f, }, { intros t ht hΞΌt, rw ← integral_condexp_L2_eq_of_fin_meas_real f ht hΞΌt.ne, exact set_integral_congr_ae (hm t ht) (hg_eq.mono (Ξ» x hx _, hx)), }, end lemma condexp_L2_ae_eq_zero_of_ae_eq_zero (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) {f : Lp ℝ 2 ΞΌ} (hf : f =ᡐ[ΞΌ.restrict s] 0) : condexp_L2 ℝ hm f =ᡐ[ΞΌ.restrict s] 0 := begin suffices h_nnnorm_eq_zero : ∫⁻ x in s, βˆ₯condexp_L2 ℝ hm f xβˆ₯β‚Š βˆ‚ΞΌ = 0, { rw lintegral_eq_zero_iff at h_nnnorm_eq_zero, refine h_nnnorm_eq_zero.mono (Ξ» x hx, _), dsimp only at hx, rw pi.zero_apply at hx ⊒, { rwa [ennreal.coe_eq_zero, nnnorm_eq_zero] at hx, }, { refine measurable.coe_nnreal_ennreal (measurable.nnnorm _), rw Lp_meas_coe, exact Lp.measurable _, }, }, refine le_antisymm _ (zero_le _), refine (lintegral_nnnorm_condexp_L2_le hs hΞΌs f).trans (le_of_eq _), rw lintegral_eq_zero_iff, { refine hf.mono (Ξ» x hx, _), dsimp only, rw hx, simp, }, { exact (Lp.measurable _).nnnorm.coe_nnreal_ennreal, }, end lemma lintegral_nnnorm_condexp_L2_indicator_le_real (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (ht : measurable_set[m] t) (hΞΌt : ΞΌ t β‰  ∞) : ∫⁻ a in t, βˆ₯condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)) aβˆ₯β‚Š βˆ‚ΞΌ ≀ ΞΌ (s ∩ t) := begin refine (lintegral_nnnorm_condexp_L2_le ht hΞΌt _).trans (le_of_eq _), have h_eq : ∫⁻ x in t, βˆ₯(indicator_const_Lp 2 hs hΞΌs (1 : ℝ)) xβˆ₯β‚Š βˆ‚ΞΌ = ∫⁻ x in t, s.indicator (Ξ» x, (1 : ℝβ‰₯0∞)) x βˆ‚ΞΌ, { refine lintegral_congr_ae (ae_restrict_of_ae _), refine (@indicator_const_Lp_coe_fn _ _ _ 2 _ _ _ _ hs hΞΌs (1 : ℝ) _ _).mono (Ξ» x hx, _), rw hx, simp_rw set.indicator_apply, split_ifs; simp, }, rw [h_eq, lintegral_indicator _ hs, lintegral_const, measure.restrict_restrict hs], simp only [one_mul, set.univ_inter, measurable_set.univ, measure.restrict_apply], end end real /-- `condexp_L2` commutes with taking inner products with constants. See the lemma `condexp_L2_comp_continuous_linear_map` for a more general result about commuting with continuous linear maps. -/ lemma condexp_L2_const_inner (hm : m ≀ m0) (f : Lp E 2 ΞΌ) (c : E) : condexp_L2 π•œ hm (((Lp.mem_β„’p f).const_inner c).to_Lp (Ξ» a, βŸͺc, f a⟫)) =ᡐ[ΞΌ] Ξ» a, βŸͺc, condexp_L2 π•œ hm f a⟫ := begin rw Lp_meas_coe, have h_mem_Lp : mem_β„’p (Ξ» a, βŸͺc, condexp_L2 π•œ hm f a⟫) 2 ΞΌ, { refine mem_β„’p.const_inner _ _, rw Lp_meas_coe, exact Lp.mem_β„’p _, }, have h_eq : h_mem_Lp.to_Lp _ =ᡐ[ΞΌ] Ξ» a, βŸͺc, condexp_L2 π•œ hm f a⟫, from h_mem_Lp.coe_fn_to_Lp, refine eventually_eq.trans _ h_eq, refine Lp.ae_eq_of_forall_set_integral_eq' hm _ _ ennreal.zero_lt_two.ne.symm ennreal.coe_ne_top (Ξ» s hs hΞΌs, integrable_on_condexp_L2_of_measure_ne_top hm hΞΌs.ne _) _ _ _ _, { intros s hs hΞΌs, rw [integrable_on, integrable_congr (ae_restrict_of_ae h_eq)], exact (integrable_on_condexp_L2_of_measure_ne_top hm hΞΌs.ne _).const_inner _, }, { intros s hs hΞΌs, rw [← Lp_meas_coe, integral_condexp_L2_eq_of_fin_meas_real _ hs hΞΌs.ne, integral_congr_ae (ae_restrict_of_ae h_eq), Lp_meas_coe, ← L2.inner_indicator_const_Lp_eq_set_integral_inner π•œ ↑(condexp_L2 π•œ hm f) (hm s hs) c hΞΌs.ne, ← inner_condexp_L2_left_eq_right, condexp_L2_indicator_of_measurable, L2.inner_indicator_const_Lp_eq_set_integral_inner π•œ f (hm s hs) c hΞΌs.ne, set_integral_congr_ae (hm s hs) ((mem_β„’p.coe_fn_to_Lp ((Lp.mem_β„’p f).const_inner c)).mono (Ξ» x hx hxs, hx))], }, { rw ← Lp_meas_coe, exact Lp_meas.ae_measurable' _, }, { refine ae_measurable'.congr _ h_eq.symm, exact (Lp_meas.ae_measurable' _).const_inner _, }, end /-- `condexp_L2` verifies the equality of integrals defining the conditional expectation. -/ lemma integral_condexp_L2_eq (hm : m ≀ m0) (f : Lp E' 2 ΞΌ) (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) : ∫ x in s, condexp_L2 π•œ hm f x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ := begin rw [← sub_eq_zero, Lp_meas_coe, ← integral_sub' (integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs) (integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs)], refine integral_eq_zero_of_forall_integral_inner_eq_zero _ _ _, { rw integrable_congr (ae_restrict_of_ae (Lp.coe_fn_sub ↑(condexp_L2 π•œ hm f) f).symm), exact integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs, }, intro c, simp_rw [pi.sub_apply, inner_sub_right], rw integral_sub ((integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs).const_inner c) ((integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs).const_inner c), have h_ae_eq_f := mem_β„’p.coe_fn_to_Lp ((Lp.mem_β„’p f).const_inner c), rw [← Lp_meas_coe, sub_eq_zero, ← set_integral_congr_ae (hm s hs) ((condexp_L2_const_inner hm f c).mono (Ξ» x hx _, hx)), ← set_integral_congr_ae (hm s hs) (h_ae_eq_f.mono (Ξ» x hx _, hx))], exact integral_condexp_L2_eq_of_fin_meas_real _ hs hΞΌs, end variables {E'' π•œ' : Type*} [is_R_or_C π•œ'] [measurable_space E''] [inner_product_space π•œ' E''] [borel_space E''] [second_countable_topology E''] [complete_space E''] [normed_space ℝ E''] variables (π•œ π•œ') lemma condexp_L2_comp_continuous_linear_map (hm : m ≀ m0) (T : E' β†’L[ℝ] E'') (f : Ξ± β†’β‚‚[ΞΌ] E') : (condexp_L2 π•œ' hm (T.comp_Lp f) : Ξ± β†’β‚‚[ΞΌ] E'') =ᡐ[ΞΌ] T.comp_Lp (condexp_L2 π•œ hm f : Ξ± β†’β‚‚[ΞΌ] E') := begin refine Lp.ae_eq_of_forall_set_integral_eq' hm _ _ ennreal.zero_lt_two.ne.symm ennreal.coe_ne_top (Ξ» s hs hΞΌs, integrable_on_condexp_L2_of_measure_ne_top hm hΞΌs.ne _) (Ξ» s hs hΞΌs, integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs.ne) _ _ _, { intros s hs hΞΌs, rw [T.set_integral_comp_Lp _ (hm s hs), T.integral_comp_comm (integrable_on_Lp_of_measure_ne_top _ fact_one_le_two_ennreal.elim hΞΌs.ne), ← Lp_meas_coe, ← Lp_meas_coe, integral_condexp_L2_eq hm f hs hΞΌs.ne, integral_condexp_L2_eq hm (T.comp_Lp f) hs hΞΌs.ne, T.set_integral_comp_Lp _ (hm s hs), T.integral_comp_comm (integrable_on_Lp_of_measure_ne_top f fact_one_le_two_ennreal.elim hΞΌs.ne)], }, { rw ← Lp_meas_coe, exact Lp_meas.ae_measurable' _, }, { have h_coe := T.coe_fn_comp_Lp (condexp_L2 π•œ hm f : Ξ± β†’β‚‚[ΞΌ] E'), rw ← eventually_eq at h_coe, refine ae_measurable'.congr _ h_coe.symm, exact (Lp_meas.ae_measurable' (condexp_L2 π•œ hm f)).measurable_comp T.measurable, }, end variables {π•œ π•œ'} section condexp_L2_indicator variables (π•œ) lemma condexp_L2_indicator_ae_eq_smul (hm : m ≀ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : E') : condexp_L2 π•œ hm (indicator_const_Lp 2 hs hΞΌs x) =ᡐ[ΞΌ] Ξ» a, (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)) a) β€’ x := begin rw indicator_const_Lp_eq_to_span_singleton_comp_Lp hs hΞΌs x, have h_comp := condexp_L2_comp_continuous_linear_map ℝ π•œ hm (to_span_singleton ℝ x) (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)), rw ← Lp_meas_coe at h_comp, refine h_comp.trans _, exact (to_span_singleton ℝ x).coe_fn_comp_Lp _, end lemma condexp_L2_indicator_eq_to_span_singleton_comp (hm : m ≀ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : E') : (condexp_L2 π•œ hm (indicator_const_Lp 2 hs hΞΌs x) : Ξ± β†’β‚‚[ΞΌ] E') = (to_span_singleton ℝ x).comp_Lp (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ))) := begin ext1, rw ← Lp_meas_coe, refine (condexp_L2_indicator_ae_eq_smul π•œ hm hs hΞΌs x).trans _, have h_comp := (to_span_singleton ℝ x).coe_fn_comp_Lp (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)) : Ξ± β†’β‚‚[ΞΌ] ℝ), rw ← eventually_eq at h_comp, refine eventually_eq.trans _ h_comp.symm, refine eventually_of_forall (Ξ» y, _), refl, end variables {π•œ} lemma set_lintegral_nnnorm_condexp_L2_indicator_le (hm : m ≀ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : E') {t : set Ξ±} (ht : measurable_set[m] t) (hΞΌt : ΞΌ t β‰  ∞) : ∫⁻ a in t, βˆ₯condexp_L2 π•œ hm (indicator_const_Lp 2 hs hΞΌs x) aβˆ₯β‚Š βˆ‚ΞΌ ≀ ΞΌ (s ∩ t) * βˆ₯xβˆ₯β‚Š := calc ∫⁻ a in t, βˆ₯condexp_L2 π•œ hm (indicator_const_Lp 2 hs hΞΌs x) aβˆ₯β‚Š βˆ‚ΞΌ = ∫⁻ a in t, βˆ₯(condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)) a) β€’ xβˆ₯β‚Š βˆ‚ΞΌ : set_lintegral_congr_fun (hm t ht) ((condexp_L2_indicator_ae_eq_smul π•œ hm hs hΞΌs x).mono (Ξ» a ha hat, by rw ha)) ... = ∫⁻ a in t, βˆ₯condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)) aβˆ₯β‚Š βˆ‚ΞΌ * βˆ₯xβˆ₯β‚Š : begin simp_rw [nnnorm_smul, ennreal.coe_mul], rw [lintegral_mul_const, Lp_meas_coe], exact (Lp.measurable _).nnnorm.coe_nnreal_ennreal, end ... ≀ ΞΌ (s ∩ t) * βˆ₯xβˆ₯β‚Š : ennreal.mul_le_mul (lintegral_nnnorm_condexp_L2_indicator_le_real hs hΞΌs ht hΞΌt) le_rfl lemma lintegral_nnnorm_condexp_L2_indicator_le (hm : m ≀ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : E') [sigma_finite (ΞΌ.trim hm)] : ∫⁻ a, βˆ₯condexp_L2 π•œ hm (indicator_const_Lp 2 hs hΞΌs x) aβˆ₯β‚Š βˆ‚ΞΌ ≀ ΞΌ s * βˆ₯xβˆ₯β‚Š := begin refine lintegral_le_of_forall_fin_meas_le' hm (ΞΌ s * βˆ₯xβˆ₯β‚Š) _ (Ξ» t ht hΞΌt, _), { rw Lp_meas_coe, exact (Lp.ae_measurable _).nnnorm.coe_nnreal_ennreal, }, refine (set_lintegral_nnnorm_condexp_L2_indicator_le hm hs hΞΌs x ht hΞΌt).trans _, refine ennreal.mul_le_mul _ le_rfl, exact measure_mono (set.inter_subset_left _ _), end /-- If the measure `ΞΌ.trim hm` is sigma-finite, then the conditional expectation of a measurable set with finite measure is integrable. -/ lemma integrable_condexp_L2_indicator (hm : m ≀ m0) [sigma_finite (ΞΌ.trim hm)] (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : E') : integrable (condexp_L2 π•œ hm (indicator_const_Lp 2 hs hΞΌs x)) ΞΌ := begin refine integrable_of_forall_fin_meas_le' hm (ΞΌ s * βˆ₯xβˆ₯β‚Š) (ennreal.mul_lt_top hΞΌs ennreal.coe_ne_top) _ _, { rw Lp_meas_coe, exact Lp.ae_measurable _, }, { refine Ξ» t ht hΞΌt, (set_lintegral_nnnorm_condexp_L2_indicator_le hm hs hΞΌs x ht hΞΌt).trans _, exact ennreal.mul_le_mul (measure_mono (set.inter_subset_left _ _)) le_rfl, }, end end condexp_L2_indicator section condexp_ind_smul variables [normed_space ℝ G] {hm : m ≀ m0} /-- Conditional expectation of the indicator of a measurable set with finite measure, in L2. -/ def condexp_ind_smul (hm : m ≀ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : Lp G 2 ΞΌ := (to_span_singleton ℝ x).comp_LpL 2 ΞΌ (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ))) lemma ae_measurable'_condexp_ind_smul (hm : m ≀ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : ae_measurable' m (condexp_ind_smul hm hs hΞΌs x) ΞΌ := begin have h : ae_measurable' m (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ))) ΞΌ, from ae_measurable'_condexp_L2 _ _, rw condexp_ind_smul, suffices : ae_measurable' m ((to_span_singleton ℝ x) ∘ (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)))) ΞΌ, { refine ae_measurable'.congr this _, refine eventually_eq.trans _ (coe_fn_comp_LpL _ _).symm, rw Lp_meas_coe, }, exact ae_measurable'.measurable_comp (to_span_singleton ℝ x).measurable h, end lemma condexp_ind_smul_add (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x y : G) : condexp_ind_smul hm hs hΞΌs (x + y) = condexp_ind_smul hm hs hΞΌs x + condexp_ind_smul hm hs hΞΌs y := by { simp_rw [condexp_ind_smul], rw [to_span_singleton_add, add_comp_LpL, add_apply], } lemma condexp_ind_smul_smul (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (c : ℝ) (x : G) : condexp_ind_smul hm hs hΞΌs (c β€’ x) = c β€’ condexp_ind_smul hm hs hΞΌs x := by { simp_rw [condexp_ind_smul], rw [to_span_singleton_smul, smul_comp_LpL, smul_apply], } lemma condexp_ind_smul_smul' [normed_space ℝ F] [smul_comm_class ℝ π•œ F] (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (c : π•œ) (x : F) : condexp_ind_smul hm hs hΞΌs (c β€’ x) = c β€’ condexp_ind_smul hm hs hΞΌs x := by rw [condexp_ind_smul, condexp_ind_smul, to_span_singleton_smul', (to_span_singleton ℝ x).smul_comp_LpL_apply c ↑(condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)))] lemma condexp_ind_smul_ae_eq_smul (hm : m ≀ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : condexp_ind_smul hm hs hΞΌs x =ᡐ[ΞΌ] Ξ» a, (condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)) a) β€’ x := (to_span_singleton ℝ x).coe_fn_comp_LpL _ lemma set_lintegral_nnnorm_condexp_ind_smul_le (hm : m ≀ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) {t : set Ξ±} (ht : measurable_set[m] t) (hΞΌt : ΞΌ t β‰  ∞) : ∫⁻ a in t, βˆ₯condexp_ind_smul hm hs hΞΌs x aβˆ₯β‚Š βˆ‚ΞΌ ≀ ΞΌ (s ∩ t) * βˆ₯xβˆ₯β‚Š := calc ∫⁻ a in t, βˆ₯condexp_ind_smul hm hs hΞΌs x aβˆ₯β‚Š βˆ‚ΞΌ = ∫⁻ a in t, βˆ₯condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)) a β€’ xβˆ₯β‚Š βˆ‚ΞΌ : set_lintegral_congr_fun (hm t ht) ((condexp_ind_smul_ae_eq_smul hm hs hΞΌs x).mono (Ξ» a ha hat, by rw ha )) ... = ∫⁻ a in t, βˆ₯condexp_L2 ℝ hm (indicator_const_Lp 2 hs hΞΌs (1 : ℝ)) aβˆ₯β‚Š βˆ‚ΞΌ * βˆ₯xβˆ₯β‚Š : begin simp_rw [nnnorm_smul, ennreal.coe_mul], rw [lintegral_mul_const, Lp_meas_coe], exact (Lp.measurable _).nnnorm.coe_nnreal_ennreal, end ... ≀ ΞΌ (s ∩ t) * βˆ₯xβˆ₯β‚Š : ennreal.mul_le_mul (lintegral_nnnorm_condexp_L2_indicator_le_real hs hΞΌs ht hΞΌt) le_rfl lemma lintegral_nnnorm_condexp_ind_smul_le (hm : m ≀ m0) (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) [sigma_finite (ΞΌ.trim hm)] : ∫⁻ a, βˆ₯condexp_ind_smul hm hs hΞΌs x aβˆ₯β‚Š βˆ‚ΞΌ ≀ ΞΌ s * βˆ₯xβˆ₯β‚Š := begin refine lintegral_le_of_forall_fin_meas_le' hm (ΞΌ s * βˆ₯xβˆ₯β‚Š) _ (Ξ» t ht hΞΌt, _), { exact (Lp.ae_measurable _).nnnorm.coe_nnreal_ennreal, }, refine (set_lintegral_nnnorm_condexp_ind_smul_le hm hs hΞΌs x ht hΞΌt).trans _, refine ennreal.mul_le_mul _ le_rfl, exact measure_mono (set.inter_subset_left _ _), end /-- If the measure `ΞΌ.trim hm` is sigma-finite, then the conditional expectation of a measurable set with finite measure is integrable. -/ lemma integrable_condexp_ind_smul (hm : m ≀ m0) [sigma_finite (ΞΌ.trim hm)] (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : integrable (condexp_ind_smul hm hs hΞΌs x) ΞΌ := begin refine integrable_of_forall_fin_meas_le' hm (ΞΌ s * βˆ₯xβˆ₯β‚Š) (ennreal.mul_lt_top hΞΌs ennreal.coe_ne_top) _ _, { exact Lp.ae_measurable _, }, { refine Ξ» t ht hΞΌt, (set_lintegral_nnnorm_condexp_ind_smul_le hm hs hΞΌs x ht hΞΌt).trans _, exact ennreal.mul_le_mul (measure_mono (set.inter_subset_left _ _)) le_rfl, }, end lemma condexp_ind_smul_empty {x : G} : condexp_ind_smul hm measurable_set.empty ((@measure_empty _ _ ΞΌ).le.trans_lt ennreal.coe_lt_top).ne x = 0 := begin rw [condexp_ind_smul, indicator_const_empty], simp only [coe_fn_coe_base, submodule.coe_zero, continuous_linear_map.map_zero], end lemma set_integral_condexp_ind_smul (hs : measurable_set[m] s) (ht : measurable_set t) (hΞΌs : ΞΌ s β‰  ∞) (hΞΌt : ΞΌ t β‰  ∞) (x : G') : ∫ a in s, (condexp_ind_smul hm ht hΞΌt x) a βˆ‚ΞΌ = (ΞΌ (t ∩ s)).to_real β€’ x := calc ∫ a in s, (condexp_ind_smul hm ht hΞΌt x) a βˆ‚ΞΌ = (∫ a in s, (condexp_L2 ℝ hm (indicator_const_Lp 2 ht hΞΌt (1 : ℝ)) a β€’ x) βˆ‚ΞΌ) : set_integral_congr_ae (hm s hs) ((condexp_ind_smul_ae_eq_smul hm ht hΞΌt x).mono (Ξ» x hx hxs, hx)) ... = (∫ a in s, condexp_L2 ℝ hm (indicator_const_Lp 2 ht hΞΌt (1 : ℝ)) a βˆ‚ΞΌ) β€’ x : integral_smul_const _ x ... = (∫ a in s, indicator_const_Lp 2 ht hΞΌt (1 : ℝ) a βˆ‚ΞΌ) β€’ x : by rw @integral_condexp_L2_eq Ξ± _ ℝ _ _ _ _ _ _ _ _ _ _ _ hm (indicator_const_Lp 2 ht hΞΌt (1 : ℝ)) hs hΞΌs ... = (ΞΌ (t ∩ s)).to_real β€’ x : by rw [set_integral_indicator_const_Lp (hm s hs), smul_assoc, one_smul] end condexp_ind_smul end condexp_L2 section condexp_ind /-! ## Conditional expectation of an indicator as a continuous linear map. The goal of this section is to build `condexp_ind (hm : m ≀ m0) (ΞΌ : measure Ξ±) (s : set s) : G β†’L[ℝ] Ξ± →₁[ΞΌ] G`, which takes `x : G` to the conditional expectation of the indicator of the set `s` with value `x`, seen as an element of `Ξ± →₁[ΞΌ] G`. -/ variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {s t : set Ξ±} [normed_space ℝ G] section condexp_ind_L1_fin /-- Conditional expectation of the indicator of a measurable set with finite measure, as a function in L1. -/ def condexp_ind_L1_fin (hm : m ≀ m0) [sigma_finite (ΞΌ.trim hm)] (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : Ξ± →₁[ΞΌ] G := (integrable_condexp_ind_smul hm hs hΞΌs x).to_L1 _ lemma condexp_ind_L1_fin_ae_eq_condexp_ind_smul (hm : m ≀ m0) [sigma_finite (ΞΌ.trim hm)] (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : condexp_ind_L1_fin hm hs hΞΌs x =ᡐ[ΞΌ] condexp_ind_smul hm hs hΞΌs x := (integrable_condexp_ind_smul hm hs hΞΌs x).coe_fn_to_L1 variables {hm : m ≀ m0} [sigma_finite (ΞΌ.trim hm)] lemma condexp_ind_L1_fin_add (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x y : G) : condexp_ind_L1_fin hm hs hΞΌs (x + y) = condexp_ind_L1_fin hm hs hΞΌs x + condexp_ind_L1_fin hm hs hΞΌs y := begin ext1, refine (mem_β„’p.coe_fn_to_Lp _).trans _, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, refine eventually_eq.trans _ (eventually_eq.add (mem_β„’p.coe_fn_to_Lp _).symm (mem_β„’p.coe_fn_to_Lp _).symm), rw condexp_ind_smul_add, refine (Lp.coe_fn_add _ _).trans (eventually_of_forall (Ξ» a, _)), refl, end lemma condexp_ind_L1_fin_smul (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (c : ℝ) (x : G) : condexp_ind_L1_fin hm hs hΞΌs (c β€’ x) = c β€’ condexp_ind_L1_fin hm hs hΞΌs x := begin ext1, refine (mem_β„’p.coe_fn_to_Lp _).trans _, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, rw condexp_ind_smul_smul hs hΞΌs c x, refine (Lp.coe_fn_smul _ _).trans _, refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x).mono (Ξ» y hy, _), rw [pi.smul_apply, pi.smul_apply, hy], end lemma condexp_ind_L1_fin_smul' [normed_space ℝ F] [smul_comm_class ℝ π•œ F] (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (c : π•œ) (x : F) : condexp_ind_L1_fin hm hs hΞΌs (c β€’ x) = c β€’ condexp_ind_L1_fin hm hs hΞΌs x := begin ext1, refine (mem_β„’p.coe_fn_to_Lp _).trans _, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, rw condexp_ind_smul_smul' hs hΞΌs c x, refine (Lp.coe_fn_smul _ _).trans _, refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x).mono (Ξ» y hy, _), rw [pi.smul_apply, pi.smul_apply, hy], end lemma norm_condexp_ind_L1_fin_le (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : βˆ₯condexp_ind_L1_fin hm hs hΞΌs xβˆ₯ ≀ (ΞΌ s).to_real * βˆ₯xβˆ₯ := begin have : 0 ≀ ∫ (a : Ξ±), βˆ₯condexp_ind_L1_fin hm hs hΞΌs x aβˆ₯ βˆ‚ΞΌ, from integral_nonneg (Ξ» a, norm_nonneg _), rw [L1.norm_eq_integral_norm, ← ennreal.to_real_of_real (norm_nonneg x), ← ennreal.to_real_mul, ← ennreal.to_real_of_real this, ennreal.to_real_le_to_real ennreal.of_real_ne_top (ennreal.mul_ne_top hΞΌs ennreal.of_real_ne_top), of_real_integral_norm_eq_lintegral_nnnorm], swap, { rw [← mem_β„’p_one_iff_integrable], exact Lp.mem_β„’p _, }, have h_eq : ∫⁻ a, βˆ₯condexp_ind_L1_fin hm hs hΞΌs x aβˆ₯β‚Š βˆ‚ΞΌ = ∫⁻ a, nnnorm (condexp_ind_smul hm hs hΞΌs x a) βˆ‚ΞΌ, { refine lintegral_congr_ae _, refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x).mono (Ξ» z hz, _), dsimp only, rw hz, }, rw [h_eq, of_real_norm_eq_coe_nnnorm], exact lintegral_nnnorm_condexp_ind_smul_le hm hs hΞΌs x, end lemma condexp_ind_L1_fin_disjoint_union (hs : measurable_set s) (ht : measurable_set t) (hΞΌs : ΞΌ s β‰  ∞) (hΞΌt : ΞΌ t β‰  ∞) (hst : s ∩ t = βˆ…) (x : G) : condexp_ind_L1_fin hm (hs.union ht) ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hΞΌs, hΞΌt⟩))).ne x = condexp_ind_L1_fin hm hs hΞΌs x + condexp_ind_L1_fin hm ht hΞΌt x := begin ext1, have hΞΌst := ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hΞΌs, hΞΌt⟩))).ne, refine (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm (hs.union ht) hΞΌst x).trans _, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, have hs_eq := condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x, have ht_eq := condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm ht hΞΌt x, refine eventually_eq.trans _ (eventually_eq.add hs_eq.symm ht_eq.symm), rw condexp_ind_smul, rw indicator_const_Lp_disjoint_union hs ht hΞΌs hΞΌt hst (1 : ℝ), rw (condexp_L2 ℝ hm).map_add, push_cast, rw ((to_span_singleton ℝ x).comp_LpL 2 ΞΌ).map_add, refine (Lp.coe_fn_add _ _).trans _, refine eventually_of_forall (Ξ» y, _), refl, end end condexp_ind_L1_fin open_locale classical section condexp_ind_L1 /-- Conditional expectation of the indicator of a set, as a function in L1. Its value for sets which are not both measurable and of finite measure is not used: we set it to 0. -/ def condexp_ind_L1 {m m0 : measurable_space Ξ±} (hm : m ≀ m0) (ΞΌ : measure Ξ±) (s : set Ξ±) [sigma_finite (ΞΌ.trim hm)] (x : G) : Ξ± →₁[ΞΌ] G := if hs : measurable_set s ∧ ΞΌ s β‰  ∞ then condexp_ind_L1_fin hm hs.1 hs.2 x else 0 variables {hm : m ≀ m0} [sigma_finite (ΞΌ.trim hm)] lemma condexp_ind_L1_of_measurable_set_of_measure_ne_top (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : condexp_ind_L1 hm ΞΌ s x = condexp_ind_L1_fin hm hs hΞΌs x := by simp only [condexp_ind_L1, and.intro hs hΞΌs, dif_pos, ne.def, not_false_iff, and_self] lemma condexp_ind_L1_of_measure_eq_top (hΞΌs : ΞΌ s = ∞) (x : G) : condexp_ind_L1 hm ΞΌ s x = 0 := by simp only [condexp_ind_L1, hΞΌs, eq_self_iff_true, not_true, ne.def, dif_neg, not_false_iff, and_false] lemma condexp_ind_L1_of_not_measurable_set (hs : Β¬ measurable_set s) (x : G) : condexp_ind_L1 hm ΞΌ s x = 0 := by simp only [condexp_ind_L1, hs, dif_neg, not_false_iff, false_and] lemma condexp_ind_L1_add (x y : G) : condexp_ind_L1 hm ΞΌ s (x + y) = condexp_ind_L1 hm ΞΌ s x + condexp_ind_L1 hm ΞΌ s y := begin by_cases hs : measurable_set s, swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw zero_add, }, by_cases hΞΌs : ΞΌ s = ∞, { simp_rw condexp_ind_L1_of_measure_eq_top hΞΌs, rw zero_add, }, { simp_rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs, exact condexp_ind_L1_fin_add hs hΞΌs x y, }, end lemma condexp_ind_L1_smul (c : ℝ) (x : G) : condexp_ind_L1 hm ΞΌ s (c β€’ x) = c β€’ condexp_ind_L1 hm ΞΌ s x := begin by_cases hs : measurable_set s, swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw smul_zero, }, by_cases hΞΌs : ΞΌ s = ∞, { simp_rw condexp_ind_L1_of_measure_eq_top hΞΌs, rw smul_zero, }, { simp_rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs, exact condexp_ind_L1_fin_smul hs hΞΌs c x, }, end lemma condexp_ind_L1_smul' [normed_space ℝ F] [smul_comm_class ℝ π•œ F] (c : π•œ) (x : F) : condexp_ind_L1 hm ΞΌ s (c β€’ x) = c β€’ condexp_ind_L1 hm ΞΌ s x := begin by_cases hs : measurable_set s, swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw smul_zero, }, by_cases hΞΌs : ΞΌ s = ∞, { simp_rw condexp_ind_L1_of_measure_eq_top hΞΌs, rw smul_zero, }, { simp_rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs, exact condexp_ind_L1_fin_smul' hs hΞΌs c x, }, end lemma norm_condexp_ind_L1_le (x : G) : βˆ₯condexp_ind_L1 hm ΞΌ s xβˆ₯ ≀ (ΞΌ s).to_real * βˆ₯xβˆ₯ := begin by_cases hs : measurable_set s, swap, {simp_rw condexp_ind_L1_of_not_measurable_set hs, rw Lp.norm_zero, exact mul_nonneg ennreal.to_real_nonneg (norm_nonneg _), }, by_cases hΞΌs : ΞΌ s = ∞, { rw [condexp_ind_L1_of_measure_eq_top hΞΌs x, Lp.norm_zero], exact mul_nonneg ennreal.to_real_nonneg (norm_nonneg _), }, { rw condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs x, exact norm_condexp_ind_L1_fin_le hs hΞΌs x, }, end lemma continuous_condexp_ind_L1 : continuous (Ξ» x : G, condexp_ind_L1 hm ΞΌ s x) := continuous_of_linear_of_bound condexp_ind_L1_add condexp_ind_L1_smul norm_condexp_ind_L1_le lemma condexp_ind_L1_disjoint_union (hs : measurable_set s) (ht : measurable_set t) (hΞΌs : ΞΌ s β‰  ∞) (hΞΌt : ΞΌ t β‰  ∞) (hst : s ∩ t = βˆ…) (x : G) : condexp_ind_L1 hm ΞΌ (s βˆͺ t) x = condexp_ind_L1 hm ΞΌ s x + condexp_ind_L1 hm ΞΌ t x := begin have hΞΌst : ΞΌ (s βˆͺ t) β‰  ∞, from ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hΞΌs, hΞΌt⟩))).ne, rw [condexp_ind_L1_of_measurable_set_of_measure_ne_top hs hΞΌs x, condexp_ind_L1_of_measurable_set_of_measure_ne_top ht hΞΌt x, condexp_ind_L1_of_measurable_set_of_measure_ne_top (hs.union ht) hΞΌst x], exact condexp_ind_L1_fin_disjoint_union hs ht hΞΌs hΞΌt hst x, end end condexp_ind_L1 /-- Conditional expectation of the indicator of a set, as a linear map from `G` to L1. -/ def condexp_ind {m m0 : measurable_space Ξ±} (hm : m ≀ m0) (ΞΌ : measure Ξ±) [sigma_finite (ΞΌ.trim hm)] (s : set Ξ±) : G β†’L[ℝ] Ξ± →₁[ΞΌ] G := { to_fun := condexp_ind_L1 hm ΞΌ s, map_add' := condexp_ind_L1_add, map_smul' := condexp_ind_L1_smul, cont := continuous_condexp_ind_L1, } lemma condexp_ind_ae_eq_condexp_ind_smul (hm : m ≀ m0) [sigma_finite (ΞΌ.trim hm)] (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : condexp_ind hm ΞΌ s x =ᡐ[ΞΌ] condexp_ind_smul hm hs hΞΌs x := begin refine eventually_eq.trans _ (condexp_ind_L1_fin_ae_eq_condexp_ind_smul hm hs hΞΌs x), simp [condexp_ind, condexp_ind_L1, hs, hΞΌs], end variables {hm : m ≀ m0} [sigma_finite (ΞΌ.trim hm)] lemma ae_measurable'_condexp_ind (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : G) : ae_measurable' m (condexp_ind hm ΞΌ s x) ΞΌ := ae_measurable'.congr (ae_measurable'_condexp_ind_smul hm hs hΞΌs x) (condexp_ind_ae_eq_condexp_ind_smul hm hs hΞΌs x).symm @[simp] lemma condexp_ind_empty : condexp_ind hm ΞΌ βˆ… = (0 : G β†’L[ℝ] Ξ± →₁[ΞΌ] G) := begin ext1, ext1, refine (condexp_ind_ae_eq_condexp_ind_smul hm measurable_set.empty (by simp) x).trans _, rw condexp_ind_smul_empty, refine (Lp.coe_fn_zero G 2 ΞΌ).trans _, refine eventually_eq.trans _ (Lp.coe_fn_zero G 1 ΞΌ).symm, refl, end lemma condexp_ind_smul' [normed_space ℝ F] [smul_comm_class ℝ π•œ F] (c : π•œ) (x : F) : condexp_ind hm ΞΌ s (c β€’ x) = c β€’ condexp_ind hm ΞΌ s x := condexp_ind_L1_smul' c x lemma norm_condexp_ind_apply_le (x : G) : βˆ₯condexp_ind hm ΞΌ s xβˆ₯ ≀ (ΞΌ s).to_real * βˆ₯xβˆ₯ := norm_condexp_ind_L1_le x lemma norm_condexp_ind_le : βˆ₯(condexp_ind hm ΞΌ s : G β†’L[ℝ] Ξ± →₁[ΞΌ] G)βˆ₯ ≀ (ΞΌ s).to_real := continuous_linear_map.op_norm_le_bound _ ennreal.to_real_nonneg norm_condexp_ind_apply_le lemma condexp_ind_disjoint_union_apply (hs : measurable_set s) (ht : measurable_set t) (hΞΌs : ΞΌ s β‰  ∞) (hΞΌt : ΞΌ t β‰  ∞) (hst : s ∩ t = βˆ…) (x : G) : condexp_ind hm ΞΌ (s βˆͺ t) x = condexp_ind hm ΞΌ s x + condexp_ind hm ΞΌ t x := condexp_ind_L1_disjoint_union hs ht hΞΌs hΞΌt hst x lemma condexp_ind_disjoint_union (hs : measurable_set s) (ht : measurable_set t) (hΞΌs : ΞΌ s β‰  ∞) (hΞΌt : ΞΌ t β‰  ∞) (hst : s ∩ t = βˆ…) : (condexp_ind hm ΞΌ (s βˆͺ t) : G β†’L[ℝ] Ξ± →₁[ΞΌ] G) = condexp_ind hm ΞΌ s + condexp_ind hm ΞΌ t := by { ext1, push_cast, exact condexp_ind_disjoint_union_apply hs ht hΞΌs hΞΌt hst x, } variables (G) lemma dominated_fin_meas_additive_condexp_ind (hm : m ≀ m0) (ΞΌ : measure Ξ±) [sigma_finite (ΞΌ.trim hm)] : dominated_fin_meas_additive ΞΌ (condexp_ind hm ΞΌ : set Ξ± β†’ G β†’L[ℝ] Ξ± →₁[ΞΌ] G) 1 := ⟨λ s t, condexp_ind_disjoint_union, Ξ» s _ _, norm_condexp_ind_le.trans (one_mul _).symm.le⟩ variables {G} lemma set_integral_condexp_ind (hs : measurable_set[m] s) (ht : measurable_set t) (hΞΌs : ΞΌ s β‰  ∞) (hΞΌt : ΞΌ t β‰  ∞) (x : G') : ∫ a in s, condexp_ind hm ΞΌ t x a βˆ‚ΞΌ = (ΞΌ (t ∩ s)).to_real β€’ x := calc ∫ a in s, condexp_ind hm ΞΌ t x a βˆ‚ΞΌ = ∫ a in s, condexp_ind_smul hm ht hΞΌt x a βˆ‚ΞΌ : set_integral_congr_ae (hm s hs) ((condexp_ind_ae_eq_condexp_ind_smul hm ht hΞΌt x).mono (Ξ» x hx hxs, hx)) ... = (ΞΌ (t ∩ s)).to_real β€’ x : set_integral_condexp_ind_smul hs ht hΞΌs hΞΌt x lemma condexp_ind_of_measurable (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) (c : G) : condexp_ind hm ΞΌ s c = indicator_const_Lp 1 (hm s hs) hΞΌs c := begin ext1, refine eventually_eq.trans _ indicator_const_Lp_coe_fn.symm, refine (condexp_ind_ae_eq_condexp_ind_smul hm (hm s hs) hΞΌs c).trans _, refine (condexp_ind_smul_ae_eq_smul hm (hm s hs) hΞΌs c).trans _, rw [Lp_meas_coe, condexp_L2_indicator_of_measurable hm hs hΞΌs (1 : ℝ)], refine (@indicator_const_Lp_coe_fn Ξ± _ _ 2 ΞΌ _ _ s (hm s hs) hΞΌs (1 : ℝ) _ _).mono (Ξ» x hx, _), dsimp only, rw hx, by_cases hx_mem : x ∈ s; simp [hx_mem], end end condexp_ind section condexp_L1 variables {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {hm : m ≀ m0} [sigma_finite (ΞΌ.trim hm)] {f g : Ξ± β†’ F'} {s : set Ξ±} /-- Conditional expectation of a function as a linear map from `Ξ± →₁[ΞΌ] F'` to itself. -/ def condexp_L1_clm (hm : m ≀ m0) (ΞΌ : measure Ξ±) [sigma_finite (ΞΌ.trim hm)] : (Ξ± →₁[ΞΌ] F') β†’L[ℝ] Ξ± →₁[ΞΌ] F' := L1.set_to_L1 (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) lemma condexp_L1_clm_smul (c : π•œ) (f : Ξ± →₁[ΞΌ] F') : condexp_L1_clm hm ΞΌ (c β€’ f) = c β€’ condexp_L1_clm hm ΞΌ f := L1.set_to_L1_smul (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) (Ξ» c s x, condexp_ind_smul' c x) c f lemma condexp_L1_clm_indicator_const_Lp (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : F') : (condexp_L1_clm hm ΞΌ) (indicator_const_Lp 1 hs hΞΌs x) = condexp_ind hm ΞΌ s x := L1.set_to_L1_indicator_const_Lp (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) hs hΞΌs x lemma condexp_L1_clm_indicator_const (hs : measurable_set s) (hΞΌs : ΞΌ s β‰  ∞) (x : F') : (condexp_L1_clm hm ΞΌ) ↑(simple_func.indicator_const 1 hs hΞΌs x) = condexp_ind hm ΞΌ s x := by { rw Lp.simple_func.coe_indicator_const, exact condexp_L1_clm_indicator_const_Lp hs hΞΌs x, } /-- Auxiliary lemma used in the proof of `set_integral_condexp_L1_clm`. -/ lemma set_integral_condexp_L1_clm_of_measure_ne_top (f : Ξ± →₁[ΞΌ] F') (hs : measurable_set[m] s) (hΞΌs : ΞΌ s β‰  ∞) : ∫ x in s, condexp_L1_clm hm ΞΌ f x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ := begin refine Lp.induction ennreal.one_ne_top (Ξ» f : Ξ± →₁[ΞΌ] F', ∫ x in s, condexp_L1_clm hm ΞΌ f x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ) _ _ (is_closed_eq _ _) f, { intros x t ht hΞΌt, simp_rw condexp_L1_clm_indicator_const ht hΞΌt.ne x, rw [Lp.simple_func.coe_indicator_const, set_integral_indicator_const_Lp (hm _ hs)], exact set_integral_condexp_ind hs ht hΞΌs hΞΌt.ne x, }, { intros f g hf_Lp hg_Lp hfg_disj hf hg, simp_rw (condexp_L1_clm hm ΞΌ).map_add, rw set_integral_congr_ae (hm s hs) ((Lp.coe_fn_add (condexp_L1_clm hm ΞΌ (hf_Lp.to_Lp f)) (condexp_L1_clm hm ΞΌ (hg_Lp.to_Lp g))).mono (Ξ» x hx hxs, hx)), rw set_integral_congr_ae (hm s hs) ((Lp.coe_fn_add (hf_Lp.to_Lp f) (hg_Lp.to_Lp g)).mono (Ξ» x hx hxs, hx)), simp_rw pi.add_apply, rw [integral_add (L1.integrable_coe_fn _).integrable_on (L1.integrable_coe_fn _).integrable_on, integral_add (L1.integrable_coe_fn _).integrable_on (L1.integrable_coe_fn _).integrable_on, hf, hg], }, { exact (continuous_set_integral s).comp (condexp_L1_clm hm ΞΌ).continuous, }, { exact continuous_set_integral s, }, end /-- The integral of the conditional expectation `condexp_L1_clm` over an `m`-measurable set is equal to the integral of `f` on that set. See also `set_integral_condexp`, the similar statement for `condexp`. -/ lemma set_integral_condexp_L1_clm (f : Ξ± →₁[ΞΌ] F') (hs : measurable_set[m] s) : ∫ x in s, condexp_L1_clm hm ΞΌ f x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ := begin let S := spanning_sets (ΞΌ.trim hm), have hS_meas : βˆ€ i, measurable_set[m] (S i) := measurable_spanning_sets (ΞΌ.trim hm), have hS_meas0 : βˆ€ i, measurable_set (S i) := Ξ» i, hm _ (hS_meas i), have hs_eq : s = ⋃ i, S i ∩ s, { simp_rw set.inter_comm, rw [← set.inter_Union, (Union_spanning_sets (ΞΌ.trim hm)), set.inter_univ], }, have hS_finite : βˆ€ i, ΞΌ (S i ∩ s) < ∞, { refine Ξ» i, (measure_mono (set.inter_subset_left _ _)).trans_lt _, have hS_finite_trim := measure_spanning_sets_lt_top (ΞΌ.trim hm) i, rwa trim_measurable_set_eq hm (hS_meas i) at hS_finite_trim, }, have h_mono : monotone (Ξ» i, (S i) ∩ s), { intros i j hij x, simp_rw set.mem_inter_iff, exact Ξ» h, ⟨monotone_spanning_sets (ΞΌ.trim hm) hij h.1, h.2⟩, }, have h_eq_forall : (Ξ» i, ∫ x in (S i) ∩ s, condexp_L1_clm hm ΞΌ f x βˆ‚ΞΌ) = Ξ» i, ∫ x in (S i) ∩ s, f x βˆ‚ΞΌ, from funext (Ξ» i, set_integral_condexp_L1_clm_of_measure_ne_top f (@measurable_set.inter Ξ± m _ _ (hS_meas i) hs) (hS_finite i).ne), have h_right : tendsto (Ξ» i, ∫ x in (S i) ∩ s, f x βˆ‚ΞΌ) at_top (𝓝 (∫ x in s, f x βˆ‚ΞΌ)), { have h := tendsto_set_integral_of_monotone (Ξ» i, (hS_meas0 i).inter (hm s hs)) h_mono (L1.integrable_coe_fn f).integrable_on, rwa ← hs_eq at h, }, have h_left : tendsto (Ξ» i, ∫ x in (S i) ∩ s, condexp_L1_clm hm ΞΌ f x βˆ‚ΞΌ) at_top (𝓝 (∫ x in s, condexp_L1_clm hm ΞΌ f x βˆ‚ΞΌ)), { have h := tendsto_set_integral_of_monotone (Ξ» i, (hS_meas0 i).inter (hm s hs)) h_mono (L1.integrable_coe_fn (condexp_L1_clm hm ΞΌ f)).integrable_on, rwa ← hs_eq at h, }, rw h_eq_forall at h_left, exact tendsto_nhds_unique h_left h_right, end lemma ae_measurable'_condexp_L1_clm (f : Ξ± →₁[ΞΌ] F') : ae_measurable' m (condexp_L1_clm hm ΞΌ f) ΞΌ := begin refine Lp.induction ennreal.one_ne_top (Ξ» f : Ξ± →₁[ΞΌ] F', ae_measurable' m (condexp_L1_clm hm ΞΌ f) ΞΌ) _ _ _ f, { intros c s hs hΞΌs, rw condexp_L1_clm_indicator_const hs hΞΌs.ne c, exact ae_measurable'_condexp_ind hs hΞΌs.ne c, }, { intros f g hf hg h_disj hfm hgm, rw (condexp_L1_clm hm ΞΌ).map_add, refine ae_measurable'.congr _ (coe_fn_add _ _).symm, exact ae_measurable'.add hfm hgm, }, { have : {f : Lp F' 1 ΞΌ | ae_measurable' m (condexp_L1_clm hm ΞΌ f) ΞΌ} = (condexp_L1_clm hm ΞΌ) ⁻¹' {f | ae_measurable' m f ΞΌ}, by refl, rw this, refine is_closed.preimage (condexp_L1_clm hm ΞΌ).continuous _, exact is_closed_ae_measurable' hm, }, end lemma Lp_meas_to_Lp_trim_lie_symm_indicator [normed_space ℝ F] {ΞΌ : measure Ξ±} (hs : measurable_set[m] s) (hΞΌs : ΞΌ.trim hm s β‰  ∞) (c : F) : ((Lp_meas_to_Lp_trim_lie F ℝ 1 ΞΌ hm).symm (indicator_const_Lp 1 hs hΞΌs c) : Ξ± →₁[ΞΌ] F) = indicator_const_Lp 1 (hm s hs) ((le_trim hm).trans_lt hΞΌs.lt_top).ne c := begin ext1, rw ← Lp_meas_coe, change Lp_trim_to_Lp_meas F ℝ 1 ΞΌ hm (indicator_const_Lp 1 hs hΞΌs c) =ᡐ[ΞΌ] (indicator_const_Lp 1 _ _ c : Ξ± β†’ F), refine (Lp_trim_to_Lp_meas_ae_eq hm _).trans _, exact (ae_eq_of_ae_eq_trim indicator_const_Lp_coe_fn).trans indicator_const_Lp_coe_fn.symm, end lemma condexp_L1_clm_Lp_meas (f : Lp_meas F' ℝ m 1 ΞΌ) : condexp_L1_clm hm ΞΌ (f : Ξ± →₁[ΞΌ] F') = ↑f := begin let g := Lp_meas_to_Lp_trim_lie F' ℝ 1 ΞΌ hm f, have hfg : f = (Lp_meas_to_Lp_trim_lie F' ℝ 1 ΞΌ hm).symm g, by simp only [linear_isometry_equiv.symm_apply_apply], rw hfg, refine @Lp.induction Ξ± F' m _ _ _ _ 1 (ΞΌ.trim hm) _ ennreal.coe_ne_top (Ξ» g : Ξ± →₁[ΞΌ.trim hm] F', condexp_L1_clm hm ΞΌ ((Lp_meas_to_Lp_trim_lie F' ℝ 1 ΞΌ hm).symm g : Ξ± →₁[ΞΌ] F') = ↑((Lp_meas_to_Lp_trim_lie F' ℝ 1 ΞΌ hm).symm g)) _ _ _ g, { intros c s hs hΞΌs, rw [Lp.simple_func.coe_indicator_const, Lp_meas_to_Lp_trim_lie_symm_indicator hs hΞΌs.ne c, condexp_L1_clm_indicator_const_Lp], exact condexp_ind_of_measurable hs ((le_trim hm).trans_lt hΞΌs).ne c, }, { intros f g hf hg hfg_disj hf_eq hg_eq, rw linear_isometry_equiv.map_add, push_cast, rw [map_add, hf_eq, hg_eq], }, { refine is_closed_eq _ _, { refine (condexp_L1_clm hm ΞΌ).continuous.comp (continuous_induced_dom.comp _), exact linear_isometry_equiv.continuous _, }, { refine continuous_induced_dom.comp _, exact linear_isometry_equiv.continuous _, }, }, end lemma condexp_L1_clm_of_ae_measurable' (f : Ξ± →₁[ΞΌ] F') (hfm : ae_measurable' m f ΞΌ) : condexp_L1_clm hm ΞΌ f = f := condexp_L1_clm_Lp_meas (⟨f, hfm⟩ : Lp_meas F' ℝ m 1 ΞΌ) /-- Conditional expectation of a function, in L1. Its value is 0 if the function is not integrable. The function-valued `condexp` should be used instead in most cases. -/ def condexp_L1 (hm : m ≀ m0) (ΞΌ : measure Ξ±) [sigma_finite (ΞΌ.trim hm)] (f : Ξ± β†’ F') : Ξ± →₁[ΞΌ] F' := set_to_fun ΞΌ (condexp_ind hm ΞΌ) (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) f lemma condexp_L1_undef (hf : Β¬ integrable f ΞΌ) : condexp_L1 hm ΞΌ f = 0 := set_to_fun_undef (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) hf lemma condexp_L1_eq (hf : integrable f ΞΌ) : condexp_L1 hm ΞΌ f = condexp_L1_clm hm ΞΌ (hf.to_L1 f) := set_to_fun_eq (dominated_fin_meas_additive_condexp_ind F' hm ΞΌ) hf lemma condexp_L1_zero : condexp_L1 hm ΞΌ (0 : Ξ± β†’ F') = 0 := set_to_fun_zero _ lemma ae_measurable'_condexp_L1 {f : Ξ± β†’ F'} : ae_measurable' m (condexp_L1 hm ΞΌ f) ΞΌ := begin by_cases hf : integrable f ΞΌ, { rw condexp_L1_eq hf, exact ae_measurable'_condexp_L1_clm _, }, { rw condexp_L1_undef hf, refine ae_measurable'.congr _ (coe_fn_zero _ _ _).symm, exact measurable.ae_measurable' (@measurable_zero _ _ m _ _), }, end lemma integrable_condexp_L1 (f : Ξ± β†’ F') : integrable (condexp_L1 hm ΞΌ f) ΞΌ := L1.integrable_coe_fn _ /-- The integral of the conditional expectation `condexp_L1` over an `m`-measurable set is equal to the integral of `f` on that set. See also `set_integral_condexp`, the similar statement for `condexp`. -/ lemma set_integral_condexp_L1 (hf : integrable f ΞΌ) (hs : measurable_set[m] s) : ∫ x in s, condexp_L1 hm ΞΌ f x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ := begin simp_rw condexp_L1_eq hf, rw set_integral_condexp_L1_clm (hf.to_L1 f) hs, exact set_integral_congr_ae (hm s hs) ((hf.coe_fn_to_L1).mono (Ξ» x hx hxs, hx)), end lemma condexp_L1_add (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) : condexp_L1 hm ΞΌ (f + g) = condexp_L1 hm ΞΌ f + condexp_L1 hm ΞΌ g := set_to_fun_add _ hf hg lemma condexp_L1_neg (f : Ξ± β†’ F') : condexp_L1 hm ΞΌ (-f) = - condexp_L1 hm ΞΌ f := set_to_fun_neg _ f lemma condexp_L1_smul (c : π•œ) (f : Ξ± β†’ F') : condexp_L1 hm ΞΌ (c β€’ f) = c β€’ condexp_L1 hm ΞΌ f := set_to_fun_smul _ (Ξ» c _ x, condexp_ind_smul' c x) c f lemma condexp_L1_sub (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) : condexp_L1 hm ΞΌ (f - g) = condexp_L1 hm ΞΌ f - condexp_L1 hm ΞΌ g := set_to_fun_sub _ hf hg lemma condexp_L1_of_ae_measurable' (hfm : ae_measurable' m f ΞΌ) (hfi : integrable f ΞΌ) : condexp_L1 hm ΞΌ f =ᡐ[ΞΌ] f := begin rw condexp_L1_eq hfi, refine eventually_eq.trans _ (integrable.coe_fn_to_L1 hfi), rw condexp_L1_clm_of_ae_measurable', exact ae_measurable'.congr hfm (integrable.coe_fn_to_L1 hfi).symm, end end condexp_L1 section condexp /-! ### Conditional expectation of a function -/ open_locale classical variables {π•œ} {m m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} {hm : m ≀ m0} [sigma_finite (ΞΌ.trim hm)] {f g : Ξ± β†’ F'} {s : set Ξ±} variables (m) /-- Conditional expectation of a function. Its value is 0 if the function is not integrable. -/ @[irreducible] def condexp (hm : m ≀ m0) (ΞΌ : measure Ξ±) [sigma_finite (ΞΌ.trim hm)] (f : Ξ± β†’ F') : Ξ± β†’ F' := if (measurable[m] f ∧ integrable f ΞΌ) then f else ae_measurable'_condexp_L1.mk (condexp_L1 hm ΞΌ f) variables {m} -- We define notations `ΞΌ[f|hm]` and `ΞΌ[f|m,hm]` for the conditional expectation of `f` with -- respect to `m`. Both can be used in code but only the second one will be used by the goal view. -- The first notation avoids the repetition of `m`, which is already present in `hm`. The second -- one ensures that `m` stays visible in the goal view: when `hm` is complicated, it gets rendered -- as `_` and the measurable space would not be visible in `ΞΌ[f|_]`, but is clear in `ΞΌ[f|m,_]`. localized "notation ΞΌ `[` f `|` hm `]` := measure_theory.condexp _ hm ΞΌ f" in measure_theory localized "notation ΞΌ `[` f `|` m `,` hm `]` := measure_theory.condexp m hm ΞΌ f" in measure_theory lemma condexp_of_measurable {f : Ξ± β†’ F'} (hf : measurable[m] f) (hfi : integrable f ΞΌ) : ΞΌ[f|m,hm] = f := by rw [condexp, if_pos (⟨hf, hfi⟩ : measurable[m] f ∧ integrable f ΞΌ)] lemma condexp_const (c : F') [is_finite_measure ΞΌ] : ΞΌ[(Ξ» x : Ξ±, c)|m,hm] = Ξ» _, c := condexp_of_measurable (@measurable_const _ _ _ m _) (integrable_const c) lemma condexp_ae_eq_condexp_L1 (f : Ξ± β†’ F') : ΞΌ[f|m,hm] =ᡐ[ΞΌ] condexp_L1 hm ΞΌ f := begin unfold condexp, by_cases hfm : measurable[m] f, { by_cases hfi : integrable f ΞΌ, { rw if_pos (⟨hfm, hfi⟩ : measurable[m] f ∧ integrable f ΞΌ), exact (condexp_L1_of_ae_measurable' (measurable.ae_measurable' hfm) hfi).symm, }, { simp only [hfi, if_false, and_false], exact (ae_measurable'.ae_eq_mk ae_measurable'_condexp_L1).symm, }, }, simp only [hfm, if_false, false_and], exact (ae_measurable'.ae_eq_mk ae_measurable'_condexp_L1).symm, end lemma condexp_ae_eq_condexp_L1_clm (hf : integrable f ΞΌ) : ΞΌ[f|m,hm] =ᡐ[ΞΌ] condexp_L1_clm hm ΞΌ (hf.to_L1 f) := begin refine (condexp_ae_eq_condexp_L1 f).trans (eventually_of_forall (Ξ» x, _)), rw condexp_L1_eq hf, end lemma condexp_undef (hf : Β¬ integrable f ΞΌ) : ΞΌ[f|m,hm] =ᡐ[ΞΌ] 0 := begin refine (condexp_ae_eq_condexp_L1 f).trans (eventually_eq.trans _ (coe_fn_zero _ 1 _)), rw condexp_L1_undef hf, end @[simp] lemma condexp_zero : ΞΌ[(0 : Ξ± β†’ F')|m,hm] = 0 := condexp_of_measurable (@measurable_zero _ _ m _ _) (integrable_zero _ _ _) lemma measurable_condexp : measurable[m] (ΞΌ[f|m,hm]) := begin unfold condexp, by_cases hfm : measurable[m] f, { by_cases hfi : integrable f ΞΌ, { rwa if_pos (⟨hfm, hfi⟩ : measurable[m] f ∧ integrable f ΞΌ), }, { simp only [hfi, if_false, and_false], exact ae_measurable'.measurable_mk _, }, }, simp only [hfm, if_false, false_and], exact ae_measurable'.measurable_mk _, end lemma integrable_condexp : integrable (ΞΌ[f|m,hm]) ΞΌ := (integrable_condexp_L1 f).congr (condexp_ae_eq_condexp_L1 f).symm variable (hm) /-- The integral of the conditional expectation `ΞΌ[f|hm]` over an `m`-measurable set is equal to the integral of `f` on that set. -/ lemma set_integral_condexp (hf : integrable f ΞΌ) (hs : measurable_set[m] s) : ∫ x in s, ΞΌ[f|m,hm] x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ := begin rw set_integral_congr_ae (hm s hs) ((condexp_ae_eq_condexp_L1 f).mono (Ξ» x hx _, hx)), exact set_integral_condexp_L1 hf hs, end variable {hm} lemma integral_condexp (hf : integrable f ΞΌ) : ∫ x, ΞΌ[f|m,hm] x βˆ‚ΞΌ = ∫ x, f x βˆ‚ΞΌ := begin suffices : ∫ x in set.univ, ΞΌ[f|m,hm] x βˆ‚ΞΌ = ∫ x in set.univ, f x βˆ‚ΞΌ, by { simp_rw integral_univ at this, exact this, }, exact set_integral_condexp hm hf (@measurable_set.univ _ m), end /-- **Uniqueness of the conditional expectation** If a function is a.e. `m`-measurable, verifies an integrability condition and has same integral as `f` on all `m`-measurable sets, then it is a.e. equal to `ΞΌ[f|hm]`. -/ lemma ae_eq_condexp_of_forall_set_integral_eq (hm : m ≀ m0) [sigma_finite (ΞΌ.trim hm)] {f g : Ξ± β†’ F'} (hf : integrable f ΞΌ) (hg_int_finite : βˆ€ s, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ integrable_on g s ΞΌ) (hg_eq : βˆ€ s : set Ξ±, measurable_set[m] s β†’ ΞΌ s < ∞ β†’ ∫ x in s, g x βˆ‚ΞΌ = ∫ x in s, f x βˆ‚ΞΌ) (hgm : ae_measurable' m g ΞΌ) : g =ᡐ[ΞΌ] ΞΌ[f|m,hm] := begin refine ae_eq_of_forall_set_integral_eq_of_sigma_finite' hm hg_int_finite (Ξ» s hs hΞΌs, integrable_condexp.integrable_on) (Ξ» s hs hΞΌs, _) hgm (measurable.ae_measurable' measurable_condexp), rw [hg_eq s hs hΞΌs, set_integral_condexp hm hf hs], end lemma condexp_add (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) : ΞΌ[f + g | m,hm] =ᡐ[ΞΌ] ΞΌ[f|m,hm] + ΞΌ[g|m,hm] := begin refine (condexp_ae_eq_condexp_L1 _).trans _, rw condexp_L1_add hf hg, exact (coe_fn_add _ _).trans ((condexp_ae_eq_condexp_L1 _).symm.add (condexp_ae_eq_condexp_L1 _).symm), end lemma condexp_smul (c : π•œ) (f : Ξ± β†’ F') : ΞΌ[c β€’ f | m,hm] =ᡐ[ΞΌ] c β€’ ΞΌ[f|m,hm] := begin refine (condexp_ae_eq_condexp_L1 _).trans _, rw condexp_L1_smul c f, refine (@condexp_ae_eq_condexp_L1 _ _ _ _ _ _ _ _ m _ _ hm _ f).mp _, refine (coe_fn_smul c (condexp_L1 hm ΞΌ f)).mono (Ξ» x hx1 hx2, _), rw [hx1, pi.smul_apply, pi.smul_apply, hx2], end lemma condexp_neg (f : Ξ± β†’ F') : ΞΌ[-f|m,hm] =ᡐ[ΞΌ] - ΞΌ[f|m,hm] := by letI : module ℝ (Ξ± β†’ F') := @pi.module Ξ± (Ξ» _, F') ℝ _ _ (Ξ» _, infer_instance); calc ΞΌ[-f|m,hm] = ΞΌ[(-1 : ℝ) β€’ f|m,hm] : by rw neg_one_smul ℝ f ... =ᡐ[ΞΌ] (-1 : ℝ) β€’ ΞΌ[f|m,hm] : condexp_smul (-1) f ... = -ΞΌ[f|m,hm] : neg_one_smul ℝ (ΞΌ[f|m,hm]) lemma condexp_sub (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) : ΞΌ[f - g | m,hm] =ᡐ[ΞΌ] ΞΌ[f|m,hm] - ΞΌ[g|m,hm] := begin simp_rw sub_eq_add_neg, exact (condexp_add hf hg.neg).trans (eventually_eq.rfl.add (condexp_neg g)), end lemma condexp_condexp_of_le {m₁ mβ‚‚ m0 : measurable_space Ξ±} {ΞΌ : measure Ξ±} (hm₁₂ : m₁ ≀ mβ‚‚) (hmβ‚‚ : mβ‚‚ ≀ m0) [sigma_finite (ΞΌ.trim (hm₁₂.trans hmβ‚‚))] [sigma_finite (ΞΌ.trim hmβ‚‚)] : ΞΌ[ ΞΌ[f|mβ‚‚, hmβ‚‚] | m₁, hm₁₂.trans hmβ‚‚] =ᡐ[ΞΌ] ΞΌ[f | m₁, hm₁₂.trans hmβ‚‚] := begin refine ae_eq_of_forall_set_integral_eq_of_sigma_finite' (hm₁₂.trans hmβ‚‚) (Ξ» s hs hΞΌs, integrable_condexp.integrable_on) (Ξ» s hs hΞΌs, integrable_condexp.integrable_on) _ (measurable.ae_measurable' measurable_condexp) (measurable.ae_measurable' measurable_condexp), intros s hs hΞΌs, rw set_integral_condexp _ integrable_condexp hs, by_cases hf : integrable f ΞΌ, { rw [set_integral_condexp _ hf hs, set_integral_condexp _ hf (hm₁₂ s hs)], }, { simp_rw integral_congr_ae (ae_restrict_of_ae (condexp_undef hf)), }, end section real lemma rn_deriv_ae_eq_condexp {f : Ξ± β†’ ℝ} (hf : integrable f ΞΌ) : signed_measure.rn_deriv ((ΞΌ.with_densityα΅₯ f).trim hm) (ΞΌ.trim hm) =ᡐ[ΞΌ] ΞΌ[f | m,hm] := begin refine ae_eq_condexp_of_forall_set_integral_eq hm hf _ _ _, { exact Ξ» _ _ _, (integrable_of_integrable_trim hm (signed_measure.integrable_rn_deriv ((ΞΌ.with_densityα΅₯ f).trim hm) (ΞΌ.trim hm))).integrable_on }, { intros s hs hlt, conv_rhs { rw [← hf.with_densityα΅₯_trim_eq_integral hm hs, ← signed_measure.with_densityα΅₯_rn_deriv_eq ((ΞΌ.with_densityα΅₯ f).trim hm) (ΞΌ.trim hm) (hf.with_densityα΅₯_trim_absolutely_continuous hm)], }, rw [with_densityα΅₯_apply (signed_measure.integrable_rn_deriv ((ΞΌ.with_densityα΅₯ f).trim hm) (ΞΌ.trim hm)) hs, ← set_integral_trim hm _ hs], exact signed_measure.measurable_rn_deriv _ _ }, { exact measurable.ae_measurable' (signed_measure.measurable_rn_deriv _ _) }, end end real end condexp end measure_theory
6ea1e68dc2ae6d78d947e67b27126cbd666ba690
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/1705.lean
2397ddf674529211c2e93d0482f69ae5a4b999ad
[ "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
24
lean
class S (R : T u) M : R
f5d360a187474ecaa21cee52640f48cc2198cd3a
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/hott/crash1.hlean
0412ce4aadeeae2069cac5e78cab7e6f46002663
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
740
hlean
import types.sigma types.prod import algebra.binary algebra.group open eq eq.ops namespace path_algebra variable {A : Type} 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)) 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) structure zero_ne_one_class [class] (A : Type) extends has_zero A, has_one A := (zero_ne_one : zero β‰  one) structure semiring [class] (A : Type) extends add_comm_monoid A, monoid A, distrib A, mul_zero_class A, zero_ne_one_class A end path_algebra
faabf777e5ea10a9ee498050e2ae684e8356920b
2c819623a83d8c53b7282622429e344389ce4030
/proofs/util/nat.lean
482f39675c5c466e3f9164edce7fe929544ed1cc
[]
no_license
uw-unsat/jitsynth
6bc97dd1ede9da42c0b9313a4766095345493adc
69529e18d4a8d4dace884bfde91aa26b549523fa
refs/heads/master
1,653,802,737,129
1,588,361,732,000
1,588,361,732,000
260,513,113
14
0
null
null
null
null
UTF-8
Lean
false
false
4,307
lean
import tactic import tactic.find import tactic.core -- Lemmas involving facts about β„• namespace jitsynth lemma sub_lt_sub : βˆ€ a b c : β„•, c < b β†’ c < a β†’ a - b < a - c | 0 b c h₁ hβ‚‚ := -- base case begin have c_le_zero := nat.le_of_lt hβ‚‚, have c_eq_zero := nat.eq_zero_of_le_zero c_le_zero, rewrite c_eq_zero at hβ‚‚, repeat {rewrite nat.zero_sub}, exact hβ‚‚ end | (a+1) b c h₁ hβ‚‚ := -- inductive case begin have ih := sub_lt_sub a b c, have h₃ := nat.succ_le_of_lt hβ‚‚, have hc : nat.succ c = c + 1, simp, rewrite hc at h₃, have hβ‚„ : c ≀ a, rewrite ← nat.add_le_add_iff_le_right, apply h₃, have hβ‚… := nat.eq_or_lt_of_le hβ‚„, cases hβ‚…, -- c = a rewrite ← hβ‚…, rewrite nat.add_sub_cancel_left, rewrite nat.sub_eq_zero_of_le, apply nat.zero_lt_one, ---- apply nat.succ_le_of_lt; assumption, -- c < a have h₆ := nat.lt_or_ge a b, cases h₆, -- a < b have h₇ : a + 1 ≀ b, apply nat.succ_le_of_lt; assumption, have rewrite_l : a + 1 - b = 0, apply nat.sub_eq_zero_of_le; assumption, rewrite rewrite_l, rewrite nat.add_comm, rewrite nat.add_sub_assoc, have temp : 1 + (a - c) = nat.succ (a - c), rewrite nat.add_comm, rewrite temp, apply nat.zero_lt_succ, assumption, -- a β‰₯ b rewrite nat.add_comm, rewrite nat.add_sub_assoc, rewrite nat.add_sub_assoc, apply add_lt_add_left, apply ih, repeat {assumption} end lemma sub_le : βˆ€ n m : β„•, n - m ≀ n | 0 m := by simp | n 0 := by simp | (n+1) (m+1) := begin rewrite nat.add_comm, simp, apply nat.le_succ_of_le, apply sub_le end lemma sub_succ : βˆ€ n : β„•, 1 ≀ n β†’ n - 1 + 1 = n | 0 h := by cases h | (n+1) h := by simp lemma one_le_of_add_le : βˆ€ n m : β„•, 1 + m ≀ n β†’ 1 ≀ n | n 0 h := by simp at *; assumption | n (m+1) h := begin apply one_le_of_add_le n m, apply nat.le_of_succ_le, assumption end lemma succ_le_le_sub : βˆ€ n m : β„•, n + 1 ≀ m β†’ n ≀ m - 1 | n 0 h := by cases h | n (m+1) h := begin simp, rewrite ← nat.add_le_add_iff_le_right, assumption end lemma smaller_sub : βˆ€ n a b : β„•, a < b β†’ a < n β†’ n - b < n - a | 0 a b h1 h2 := by cases h2 | n a 0 h1 h2 := by cases h1 | n 0 b h1 h2 := begin simp, cases nat.lt_or_ge n b, rewrite nat.sub_eq_zero_of_le, assumption, apply nat.le_of_lt h, --- apply nat.sub_lt_of_pos_le, assumption, assumption, end | (n+1) (a+1) (b+1) h1 h2 := begin repeat {rewrite nat.add_sub_add_right}, apply smaller_sub, rewrite nat.add_comm at h1, rewrite nat.add_comm b 1 at h1, apply nat.lt_of_add_lt_add_left; assumption, rewrite nat.add_comm at h2, rewrite nat.add_comm n 1 at h2, apply nat.lt_of_add_lt_add_left; assumption, end lemma sub_le_of_sub_le_succ_of_lt : βˆ€ n m a b : β„•, a < b β†’ n - a ≀ m + 1 β†’ n - b ≀ m | n m a b h₁ hβ‚‚ := begin cases nat.lt_or_ge b n, -- b < n have h' := nat.lt_trans h₁ h, have h'' := smaller_sub n a b h₁ h', apply nat.le_of_lt_succ, cases nat.eq_or_lt_of_le hβ‚‚ with heq hlt, rewrite heq at h'', apply h'', --- apply nat.lt_trans h'' hlt, -- b β‰₯ n rewrite nat.sub_eq_zero_of_le, apply nat.zero_le, assumption end lemma lt_sub_succ_n : βˆ€ n m k : β„•, n - m = k + 1 β†’ m < n | n m k h := begin cases nat.lt_or_ge m n, assumption, rewrite nat.sub_eq_zero_of_le at h, cases h, assumption end lemma one_add : βˆ€ n : β„•, nat.succ n = 1 + n | n := by rewrite ← nat.add_one; simp lemma only_zero_lt_one : βˆ€ n : β„•, n < 1 β†’ n = 0 | 0 h := by reflexivity | (n+1) h := begin simp at h, cases h end lemma mul_le_inside : βˆ€ a b n m : β„•, a ≀ b β†’ n * b ≀ m β†’ n * a ≀ m | a b n m h1 h2 := begin have h3 := nat.mul_le_mul_left n h1, apply nat.le_trans, assumption, assumption end lemma diff_ge_1 : βˆ€ n m : β„•, n < m β†’ 1 ≀ m - n | n m h := begin -- have h' := nat.le_of_lt_succ h, rewrite ← nat.add_le_add_iff_le_right n, rewrite nat.sub_add_cancel, -- rewrite hm, apply nat.le_of_lt_succ, rewrite ← nat.add_one, rewrite nat.add_comm, apply nat.add_lt_add_right, assumption, apply nat.le_of_lt, assumption end end jitsynth
3e04964e810fa0191f306af446f48520da03fcaf
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/ring_theory/roots_of_unity.lean
666bec8ac669aeb6514457b3a50aaceb28b2d2b7
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
40,537
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.nat.parity import data.polynomial.ring_division import group_theory.specific_groups.cyclic import ring_theory.integral_domain import number_theory.divisors import data.zmod.basic import tactic.zify import field_theory.separable import field_theory.finite.basic /-! # Roots of unity and primitive roots of unity We define roots of unity in the context of an arbitrary commutative monoid, as a subgroup of the group of units. We also define a predicate `is_primitive_root` on commutative monoids, expressing that an element is a primitive root of unity. ## Main definitions * `roots_of_unity n M`, for `n : β„•+` is the subgroup of the units of a commutative monoid `M` consisting of elements `x` that satisfy `x ^ n = 1`. * `is_primitive_root ΞΆ k`: an element `ΞΆ` is a primitive `k`-th root of unity if `ΞΆ ^ k = 1`, and if `l` satisfies `ΞΆ ^ l = 1` then `k ∣ l`. * `primitive_roots k R`: the finset of primitive `k`-th roots of unity in an integral domain `R`. ## Main results * `roots_of_unity.is_cyclic`: the roots of unity in an integral domain form a cyclic group. * `is_primitive_root.zmod_equiv_gpowers`: `zmod k` is equivalent to the subgroup generated by a primitive `k`-th root of unity. * `is_primitive_root.gpowers_eq`: in an integral domain, the subgroup generated by a primitive `k`-th root of unity is equal to the `k`-th roots of unity. * `is_primitive_root.card_primitive_roots`: if an integral domain has a primitive `k`-th root of unity, then it has `Ο† k` of them. ## Implementation details It is desirable that `roots_of_unity` is a subgroup, and it will mainly be applied to rings (e.g. the ring of integers in a number field) and fields. We therefore implement it as a subgroup of the units of a commutative monoid. We have chosen to define `roots_of_unity n` for `n : β„•+`, instead of `n : β„•`, because almost all lemmas need the positivity assumption, and in particular the type class instances for `fintype` and `is_cyclic`. On the other hand, for primitive roots of unity, it is desirable to have a predicate not just on units, but directly on elements of the ring/field. For example, we want to say that `exp (2 * pi * I / n)` is a primitive `n`-th root of unity in the complex numbers, without having to turn that number into a unit first. This creates a little bit of friction, but lemmas like `is_primitive_root.is_unit` and `is_primitive_root.coe_units_iff` should provide the necessary glue. -/ open_locale classical big_operators noncomputable theory open polynomial open finset variables {M N G Gβ‚€ R S : Type*} variables [comm_monoid M] [comm_monoid N] [comm_group G] [comm_group_with_zero Gβ‚€] section roots_of_unity variables {k l : β„•+} /-- `roots_of_unity k M` is the subgroup of elements `m : units M` that satisfy `m ^ k = 1` -/ def roots_of_unity (k : β„•+) (M : Type*) [comm_monoid M] : subgroup (units M) := { carrier := { ΞΆ | ΞΆ ^ (k : β„•) = 1 }, one_mem' := one_pow _, mul_mem' := Ξ» ΞΆ ΞΎ hΞΆ hΞΎ, by simp only [*, set.mem_set_of_eq, mul_pow, one_mul] at *, inv_mem' := Ξ» ΞΆ hΞΆ, by simp only [*, set.mem_set_of_eq, inv_pow, one_inv] at * } @[simp] lemma mem_roots_of_unity (k : β„•+) (ΞΆ : units M) : ΞΆ ∈ roots_of_unity k M ↔ ΞΆ ^ (k : β„•) = 1 := iff.rfl lemma roots_of_unity_le_of_dvd (h : k ∣ l) : roots_of_unity k M ≀ roots_of_unity l M := begin obtain ⟨d, rfl⟩ := h, intros ΞΆ h, simp only [mem_roots_of_unity, pnat.mul_coe, pow_mul, one_pow, *] at *, end lemma map_roots_of_unity (f : units M β†’* units N) (k : β„•+) : (roots_of_unity k M).map f ≀ roots_of_unity k N := begin rintros _ ⟨΢, h, rfl⟩, simp only [←monoid_hom.map_pow, *, mem_roots_of_unity, set_like.mem_coe, monoid_hom.map_one] at * end variables [comm_ring R] @[norm_cast] lemma roots_of_unity.coe_pow (ΞΆ : roots_of_unity k R) (m : β„•) : ↑(ΞΆ ^ m) = (ΞΆ ^ m : R) := begin change ↑(↑(ΞΆ ^ m) : units R) = ↑(ΞΆ : units R) ^ m, rw [subgroup.coe_pow, units.coe_pow], end variables [comm_ring S] /-- Restrict a ring homomorphism between integral domains to the nth roots of unity -/ def ring_hom.restrict_roots_of_unity (Οƒ : R β†’+* S) (n : β„•+) : roots_of_unity n R β†’* roots_of_unity n S := let h : βˆ€ ΞΎ : roots_of_unity n R, (Οƒ ΞΎ) ^ (n : β„•) = 1 := Ξ» ΞΎ, by { change (Οƒ (ΞΎ : units R)) ^ (n : β„•) = 1, rw [←σ.map_pow, ←units.coe_pow, show ((ΞΎ : units R) ^ (n : β„•) = 1), from ΞΎ.2, units.coe_one, Οƒ.map_one] } in { to_fun := Ξ» ΞΎ, ⟨@unit_of_invertible _ _ _ (invertible_of_pow_eq_one _ _ (h ΞΎ) n.2), by { ext, rw units.coe_pow, exact h ΞΎ }⟩, map_one' := by { ext, exact Οƒ.map_one }, map_mul' := Ξ» ξ₁ ΞΎβ‚‚, by { ext, rw [subgroup.coe_mul, units.coe_mul], exact Οƒ.map_mul _ _ } } @[simp] lemma ring_hom.restrict_roots_of_unity_coe_apply (Οƒ : R β†’+* S) (ΞΆ : roots_of_unity k R) : ↑(Οƒ.restrict_roots_of_unity k ΞΆ) = Οƒ ↑΢ := rfl /-- Restrict a ring isomorphism between integral domains to the nth roots of unity -/ def ring_equiv.restrict_roots_of_unity (Οƒ : R ≃+* S) (n : β„•+) : roots_of_unity n R ≃* roots_of_unity n S := { to_fun := Οƒ.to_ring_hom.restrict_roots_of_unity n, inv_fun := Οƒ.symm.to_ring_hom.restrict_roots_of_unity n, left_inv := Ξ» ΞΎ, by { ext, exact Οƒ.symm_apply_apply ΞΎ }, right_inv := Ξ» ΞΎ, by { ext, exact Οƒ.apply_symm_apply ΞΎ }, map_mul' := (Οƒ.to_ring_hom.restrict_roots_of_unity n).map_mul } @[simp] lemma ring_equiv.restrict_roots_of_unity_coe_apply (Οƒ : R ≃+* S) (ΞΆ : roots_of_unity k R) : ↑(Οƒ.restrict_roots_of_unity k ΞΆ) = Οƒ ↑΢ := rfl @[simp] lemma ring_equiv.restrict_roots_of_unity_symm (Οƒ : R ≃+* S) : (Οƒ.restrict_roots_of_unity k).symm = Οƒ.symm.restrict_roots_of_unity k := rfl variables [is_domain R] lemma mem_roots_of_unity_iff_mem_nth_roots {ΞΆ : units R} : ΞΆ ∈ roots_of_unity k R ↔ (ΞΆ : R) ∈ nth_roots k (1 : R) := by simp only [mem_roots_of_unity, mem_nth_roots k.pos, units.ext_iff, units.coe_one, units.coe_pow] variables (k R) /-- Equivalence between the `k`-th roots of unity in `R` and the `k`-th roots of `1`. This is implemented as equivalence of subtypes, because `roots_of_unity` is a subgroup of the group of units, whereas `nth_roots` is a multiset. -/ def roots_of_unity_equiv_nth_roots : roots_of_unity k R ≃ {x // x ∈ nth_roots k (1 : R)} := begin refine { to_fun := Ξ» x, ⟨x, mem_roots_of_unity_iff_mem_nth_roots.mp x.2⟩, inv_fun := Ξ» x, ⟨⟨x, x ^ (k - 1 : β„•), _, _⟩, _⟩, left_inv := _, right_inv := _ }, swap 4, { rintro ⟨x, hx⟩, ext, refl }, swap 4, { rintro ⟨x, hx⟩, ext, refl }, all_goals { rcases x with ⟨x, hx⟩, rw [mem_nth_roots k.pos] at hx, simp only [subtype.coe_mk, ← pow_succ, ← pow_succ', hx, tsub_add_cancel_of_le (show 1 ≀ (k : β„•), from k.one_le)] }, { show (_ : units R) ^ (k : β„•) = 1, simp only [units.ext_iff, hx, units.coe_mk, units.coe_one, subtype.coe_mk, units.coe_pow] } end variables {k R} @[simp] lemma roots_of_unity_equiv_nth_roots_apply (x : roots_of_unity k R) : (roots_of_unity_equiv_nth_roots R k x : R) = x := rfl @[simp] lemma roots_of_unity_equiv_nth_roots_symm_apply (x : {x // x ∈ nth_roots k (1 : R)}) : ((roots_of_unity_equiv_nth_roots R k).symm x : R) = x := rfl variables (k R) instance roots_of_unity.fintype : fintype (roots_of_unity k R) := fintype.of_equiv {x // x ∈ nth_roots k (1 : R)} $ (roots_of_unity_equiv_nth_roots R k).symm instance roots_of_unity.is_cyclic : is_cyclic (roots_of_unity k R) := is_cyclic_of_subgroup_is_domain ((units.coe_hom R).comp (roots_of_unity k R).subtype) (units.ext.comp subtype.val_injective) lemma card_roots_of_unity : fintype.card (roots_of_unity k R) ≀ k := calc fintype.card (roots_of_unity k R) = fintype.card {x // x ∈ nth_roots k (1 : R)} : fintype.card_congr (roots_of_unity_equiv_nth_roots R k) ... ≀ (nth_roots k (1 : R)).attach.card : multiset.card_le_of_le (multiset.erase_dup_le _) ... = (nth_roots k (1 : R)).card : multiset.card_attach ... ≀ k : card_nth_roots k 1 variables {k R} lemma ring_hom.map_root_of_unity_eq_pow_self (Οƒ : R β†’+* R) (ΞΆ : roots_of_unity k R) : βˆƒ m : β„•, Οƒ ΞΆ = ΞΆ ^ m := begin obtain ⟨m, hm⟩ := (Οƒ.restrict_roots_of_unity k).map_cyclic, rw [←σ.restrict_roots_of_unity_coe_apply, hm, gpow_eq_mod_order_of, ←int.to_nat_of_nonneg (m.mod_nonneg (int.coe_nat_ne_zero.mpr (pos_iff_ne_zero.mp (order_of_pos ΞΆ)))), gpow_coe_nat, roots_of_unity.coe_pow], exact ⟨(m % (order_of ΞΆ)).to_nat, rfl⟩, end end roots_of_unity /-- An element `ΞΆ` is a primitive `k`-th root of unity if `ΞΆ ^ k = 1`, and if `l` satisfies `ΞΆ ^ l = 1` then `k ∣ l`. -/ structure is_primitive_root (ΞΆ : M) (k : β„•) : Prop := (pow_eq_one : ΞΆ ^ (k : β„•) = 1) (dvd_of_pow_eq_one : βˆ€ l : β„•, ΞΆ ^ l = 1 β†’ k ∣ l) section primitive_roots variables {k : β„•} /-- `primitive_roots k R` is the finset of primitive `k`-th roots of unity in the integral domain `R`. -/ def primitive_roots (k : β„•) (R : Type*) [comm_ring R] [is_domain R] : finset R := (nth_roots k (1 : R)).to_finset.filter (Ξ» ΞΆ, is_primitive_root ΞΆ k) variables [comm_ring R] [is_domain R] @[simp] lemma mem_primitive_roots {ΞΆ : R} (h0 : 0 < k) : ΞΆ ∈ primitive_roots k R ↔ is_primitive_root ΞΆ k := begin rw [primitive_roots, mem_filter, multiset.mem_to_finset, mem_nth_roots h0, and_iff_right_iff_imp], exact is_primitive_root.pow_eq_one end end primitive_roots namespace is_primitive_root variables {k l : β„•} lemma iff_def (ΞΆ : M) (k : β„•) : is_primitive_root ΞΆ k ↔ (ΞΆ ^ k = 1) ∧ (βˆ€ l : β„•, ΞΆ ^ l = 1 β†’ k ∣ l) := ⟨λ ⟨h1, h2⟩, ⟨h1, h2⟩, Ξ» ⟨h1, h2⟩, ⟨h1, h2⟩⟩ lemma mk_of_lt (ΞΆ : M) (hk : 0 < k) (h1 : ΞΆ ^ k = 1) (h : βˆ€ l : β„•, 0 < l β†’ l < k β†’ ΞΆ ^ l β‰  1) : is_primitive_root ΞΆ k := begin refine ⟨h1, _⟩, intros l hl, apply dvd_trans _ (k.gcd_dvd_right l), suffices : k.gcd l = k, { rw this }, rw eq_iff_le_not_lt, refine ⟨nat.le_of_dvd hk (k.gcd_dvd_left l), _⟩, intro h', apply h _ (nat.gcd_pos_of_pos_left _ hk) h', exact pow_gcd_eq_one _ h1 hl end section comm_monoid variables {ΞΆ : M} (h : is_primitive_root ΞΆ k) lemma pow_eq_one_iff_dvd (l : β„•) : ΞΆ ^ l = 1 ↔ k ∣ l := ⟨h.dvd_of_pow_eq_one l, by { rintro ⟨i, rfl⟩, simp only [pow_mul, h.pow_eq_one, one_pow, pnat.mul_coe] }⟩ lemma is_unit (h : is_primitive_root ΞΆ k) (h0 : 0 < k) : is_unit ΞΆ := begin apply is_unit_of_mul_eq_one ΞΆ (ΞΆ ^ (k - 1)), rw [← pow_succ, tsub_add_cancel_of_le h0.nat_succ_le, h.pow_eq_one] end lemma pow_ne_one_of_pos_of_lt (h0 : 0 < l) (hl : l < k) : ΞΆ ^ l β‰  1 := mt (nat.le_of_dvd h0 ∘ h.dvd_of_pow_eq_one _) $ not_le_of_lt hl lemma pow_inj (h : is_primitive_root ΞΆ k) ⦃i j : ℕ⦄ (hi : i < k) (hj : j < k) (H : ΞΆ ^ i = ΞΆ ^ j) : i = j := begin wlog hij : i ≀ j, apply le_antisymm hij, rw ← tsub_eq_zero_iff_le, apply nat.eq_zero_of_dvd_of_lt _ (lt_of_le_of_lt tsub_le_self hj), apply h.dvd_of_pow_eq_one, rw [← ((h.is_unit (lt_of_le_of_lt (nat.zero_le _) hi)).pow i).mul_left_inj, ← pow_add, tsub_add_cancel_of_le hij, H, one_mul] end lemma one : is_primitive_root (1 : M) 1 := { pow_eq_one := pow_one _, dvd_of_pow_eq_one := Ξ» l hl, one_dvd _ } @[simp] lemma one_right_iff : is_primitive_root ΞΆ 1 ↔ ΞΆ = 1 := begin split, { intro h, rw [← pow_one ΞΆ, h.pow_eq_one] }, { rintro rfl, exact one } end @[simp] lemma coe_units_iff {ΞΆ : units M} : is_primitive_root (ΞΆ : M) k ↔ is_primitive_root ΞΆ k := by simp only [iff_def, units.ext_iff, units.coe_pow, units.coe_one] lemma pow_of_coprime (h : is_primitive_root ΞΆ k) (i : β„•) (hi : i.coprime k) : is_primitive_root (ΞΆ ^ i) k := begin by_cases h0 : k = 0, { subst k, simp only [*, pow_one, nat.coprime_zero_right] at * }, rcases h.is_unit (nat.pos_of_ne_zero h0) with ⟨΢, rfl⟩, rw [← units.coe_pow], rw coe_units_iff at h ⊒, refine { pow_eq_one := by rw [← pow_mul', pow_mul, h.pow_eq_one, one_pow], dvd_of_pow_eq_one := _ }, intros l hl, apply h.dvd_of_pow_eq_one, rw [← pow_one ΞΆ, ← gpow_coe_nat ΞΆ, ← hi.gcd_eq_one, nat.gcd_eq_gcd_ab, gpow_add, mul_pow, ← gpow_coe_nat, ← gpow_mul, mul_right_comm], simp only [gpow_mul, hl, h.pow_eq_one, one_gpow, one_pow, one_mul, gpow_coe_nat] end lemma pow_of_prime (h : is_primitive_root ΞΆ k) {p : β„•} (hprime : nat.prime p) (hdiv : Β¬ p ∣ k) : is_primitive_root (ΞΆ ^ p) k := h.pow_of_coprime p (hprime.coprime_iff_not_dvd.2 hdiv) lemma pow_iff_coprime (h : is_primitive_root ΞΆ k) (h0 : 0 < k) (i : β„•) : is_primitive_root (ΞΆ ^ i) k ↔ i.coprime k := begin refine ⟨_, h.pow_of_coprime i⟩, intro hi, obtain ⟨a, ha⟩ := i.gcd_dvd_left k, obtain ⟨b, hb⟩ := i.gcd_dvd_right k, suffices : b = k, { rwa [this, ← one_mul k, nat.mul_left_inj h0, eq_comm] at hb { occs := occurrences.pos [1] } }, rw [ha] at hi, rw [mul_comm] at hb, apply nat.dvd_antisymm ⟨i.gcd k, hb⟩ (hi.dvd_of_pow_eq_one b _), rw [← pow_mul', ← mul_assoc, ← hb, pow_mul, h.pow_eq_one, one_pow] end end comm_monoid section comm_group variables {ΞΆ : G} lemma gpow_eq_one (h : is_primitive_root ΞΆ k) : ΞΆ ^ (k : β„€) = 1 := by { rw gpow_coe_nat, exact h.pow_eq_one } lemma gpow_eq_one_iff_dvd (h : is_primitive_root ΞΆ k) (l : β„€) : ΞΆ ^ l = 1 ↔ (k : β„€) ∣ l := begin by_cases h0 : 0 ≀ l, { lift l to β„• using h0, rw [gpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l }, { have : 0 ≀ -l, { simp only [not_le, neg_nonneg] at h0 ⊒, exact le_of_lt h0 }, lift -l to β„• using this with l' hl', rw [← dvd_neg, ← hl'], norm_cast, rw [← h.pow_eq_one_iff_dvd, ← inv_inj, ← gpow_neg, ← hl', gpow_coe_nat, one_inv] } end lemma inv (h : is_primitive_root ΞΆ k) : is_primitive_root ΢⁻¹ k := { pow_eq_one := by simp only [h.pow_eq_one, one_inv, eq_self_iff_true, inv_pow], dvd_of_pow_eq_one := begin intros l hl, apply h.dvd_of_pow_eq_one l, rw [← inv_inj, ← inv_pow, hl, one_inv] end } @[simp] lemma inv_iff : is_primitive_root ΢⁻¹ k ↔ is_primitive_root ΞΆ k := by { refine ⟨_, Ξ» h, inv h⟩, intro h, rw [← inv_inv ΞΆ], exact inv h } lemma gpow_of_gcd_eq_one (h : is_primitive_root ΞΆ k) (i : β„€) (hi : i.gcd k = 1) : is_primitive_root (ΞΆ ^ i) k := begin by_cases h0 : 0 ≀ i, { lift i to β„• using h0, rw gpow_coe_nat, exact h.pow_of_coprime i hi }, have : 0 ≀ -i, { simp only [not_le, neg_nonneg] at h0 ⊒, exact le_of_lt h0 }, lift -i to β„• using this with i' hi', rw [← inv_iff, ← gpow_neg, ← hi', gpow_coe_nat], apply h.pow_of_coprime, rw [int.gcd, ← int.nat_abs_neg, ← hi'] at hi, exact hi end @[simp] lemma coe_subgroup_iff (H : subgroup G) {ΞΆ : H} : is_primitive_root (ΞΆ : G) k ↔ is_primitive_root ΞΆ k := by simp only [iff_def, ← subgroup.coe_pow, ← H.coe_one, ← subtype.ext_iff] end comm_group section comm_group_with_zero variables {ΞΆ : Gβ‚€} lemma fpow_eq_one (h : is_primitive_root ΞΆ k) : ΞΆ ^ (k : β„€) = 1 := by { rw gpow_coe_nat, exact h.pow_eq_one } lemma fpow_eq_one_iff_dvd (h : is_primitive_root ΞΆ k) (l : β„€) : ΞΆ ^ l = 1 ↔ (k : β„€) ∣ l := begin by_cases h0 : 0 ≀ l, { lift l to β„• using h0, rw [gpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l }, { have : 0 ≀ -l, { simp only [not_le, neg_nonneg] at h0 ⊒, exact le_of_lt h0 }, lift -l to β„• using this with l' hl', rw [← dvd_neg, ← hl'], norm_cast, rw [← h.pow_eq_one_iff_dvd, ← inv_injβ‚€, ← fpow_neg, ← hl', gpow_coe_nat, inv_one] } end lemma inv' (h : is_primitive_root ΞΆ k) : is_primitive_root ΢⁻¹ k := { pow_eq_one := by simp only [h.pow_eq_one, inv_one, eq_self_iff_true, inv_powβ‚€], dvd_of_pow_eq_one := begin intros l hl, apply h.dvd_of_pow_eq_one l, rw [← inv_injβ‚€, ← inv_powβ‚€, hl, inv_one] end } @[simp] lemma inv_iff' : is_primitive_root ΢⁻¹ k ↔ is_primitive_root ΞΆ k := by { refine ⟨_, Ξ» h, inv' h⟩, intro h, rw [← inv_invβ‚€ ΞΆ], exact inv' h } lemma fpow_of_gcd_eq_one (h : is_primitive_root ΞΆ k) (i : β„€) (hi : i.gcd k = 1) : is_primitive_root (ΞΆ ^ i) k := begin by_cases h0 : 0 ≀ i, { lift i to β„• using h0, rw gpow_coe_nat, exact h.pow_of_coprime i hi }, have : 0 ≀ -i, { simp only [not_le, neg_nonneg] at h0 ⊒, exact le_of_lt h0 }, lift -i to β„• using this with i' hi', rw [← inv_iff', ← fpow_neg, ← hi', gpow_coe_nat], apply h.pow_of_coprime, rw [int.gcd, ← int.nat_abs_neg, ← hi'] at hi, exact hi end end comm_group_with_zero section is_domain variables {ΞΆ : R} variables [comm_ring R] [is_domain R] @[simp] lemma primitive_roots_zero : primitive_roots 0 R = βˆ… := begin rw [← finset.val_eq_zero, ← multiset.subset_zero, ← nth_roots_zero (1 : R), primitive_roots], simp only [finset.not_mem_empty, forall_const, forall_prop_of_false, multiset.to_finset_zero, finset.filter_true_of_mem, finset.empty_val, not_false_iff, multiset.zero_subset, nth_roots_zero] end @[simp] lemma primitive_roots_one : primitive_roots 1 R = {(1 : R)} := begin apply finset.eq_singleton_iff_unique_mem.2, split, { simp only [is_primitive_root.one_right_iff, mem_primitive_roots zero_lt_one] }, { intros x hx, rw [mem_primitive_roots zero_lt_one, is_primitive_root.one_right_iff] at hx, exact hx } end lemma neg_one (p : β„•) [char_p R p] (hp : p β‰  2) : is_primitive_root (-1 : R) 2 := mk_of_lt (-1 : R) dec_trivial (by simp only [one_pow, neg_sq]) $ begin intros l hl0 hl2, obtain rfl : l = 1, { unfreezingI { clear_dependent R p }, dec_trivial! }, simp only [pow_one, ne.def], intro h, suffices h2 : p ∣ 2, { have := char_p.char_ne_one R p, unfreezingI { clear_dependent R }, have aux := nat.le_of_dvd dec_trivial h2, revert this hp h2, revert p, dec_trivial }, simp only [← char_p.cast_eq_zero_iff R p, nat.cast_bit0, nat.cast_one], rw [bit0, ← h, neg_add_self] { occs := occurrences.pos [1] } end lemma eq_neg_one_of_two_right (h : is_primitive_root ΞΆ 2) : ΞΆ = -1 := begin apply (eq_or_eq_neg_of_sq_eq_sq ΞΆ 1 _).resolve_left, { rw [← pow_one ΞΆ], apply h.pow_ne_one_of_pos_of_lt; dec_trivial }, { simp only [h.pow_eq_one, one_pow] } end end is_domain section is_domain variables [comm_ring R] variables {ΞΆ : units R} (h : is_primitive_root ΞΆ k) protected lemma mem_roots_of_unity {n : β„•+} (h : is_primitive_root ΞΆ n) : ΞΆ ∈ roots_of_unity n R := h.pow_eq_one /-- The (additive) monoid equivalence between `zmod k` and the powers of a primitive root of unity `ΞΆ`. -/ def zmod_equiv_gpowers (h : is_primitive_root ΞΆ k) : zmod k ≃+ additive (subgroup.gpowers ΞΆ) := add_equiv.of_bijective (add_monoid_hom.lift_of_right_inverse (int.cast_add_hom $ zmod k) _ zmod.int_cast_right_inverse ⟨{ to_fun := Ξ» i, additive.of_mul (⟨_, i, rfl⟩ : subgroup.gpowers ΞΆ), map_zero' := by { simp only [gpow_zero], refl }, map_add' := by { intros i j, simp only [gpow_add], refl } }, (Ξ» i hi, begin simp only [add_monoid_hom.mem_ker, char_p.int_cast_eq_zero_iff (zmod k) k, add_monoid_hom.coe_mk, int.coe_cast_add_hom] at hi ⊒, obtain ⟨i, rfl⟩ := hi, simp only [gpow_mul, h.pow_eq_one, one_gpow, gpow_coe_nat], refl end)⟩) begin split, { rw add_monoid_hom.injective_iff, intros i hi, rw subtype.ext_iff at hi, have := (h.gpow_eq_one_iff_dvd _).mp hi, rw [← (char_p.int_cast_eq_zero_iff (zmod k) k _).mpr this, eq_comm], exact zmod.int_cast_right_inverse i }, { rintro ⟨ξ, i, rfl⟩, refine ⟨int.cast_add_hom _ i, _⟩, rw [add_monoid_hom.lift_of_right_inverse_comp_apply], refl } end @[simp] lemma zmod_equiv_gpowers_apply_coe_int (i : β„€) : h.zmod_equiv_gpowers i = additive.of_mul (⟨΢ ^ i, i, rfl⟩ : subgroup.gpowers ΞΆ) := add_monoid_hom.lift_of_right_inverse_comp_apply _ _ zmod.int_cast_right_inverse _ _ @[simp] lemma zmod_equiv_gpowers_apply_coe_nat (i : β„•) : h.zmod_equiv_gpowers i = additive.of_mul (⟨΢ ^ i, i, rfl⟩ : subgroup.gpowers ΞΆ) := begin have : (i : zmod k) = (i : β„€), by norm_cast, simp only [this, zmod_equiv_gpowers_apply_coe_int, gpow_coe_nat], refl end @[simp] lemma zmod_equiv_gpowers_symm_apply_gpow (i : β„€) : h.zmod_equiv_gpowers.symm (additive.of_mul (⟨΢ ^ i, i, rfl⟩ : subgroup.gpowers ΞΆ)) = i := by rw [← h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_int] @[simp] lemma zmod_equiv_gpowers_symm_apply_gpow' (i : β„€) : h.zmod_equiv_gpowers.symm ⟨΢ ^ i, i, rfl⟩ = i := h.zmod_equiv_gpowers_symm_apply_gpow i @[simp] lemma zmod_equiv_gpowers_symm_apply_pow (i : β„•) : h.zmod_equiv_gpowers.symm (additive.of_mul (⟨΢ ^ i, i, rfl⟩ : subgroup.gpowers ΞΆ)) = i := by rw [← h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_nat] @[simp] lemma zmod_equiv_gpowers_symm_apply_pow' (i : β„•) : h.zmod_equiv_gpowers.symm ⟨΢ ^ i, i, rfl⟩ = i := h.zmod_equiv_gpowers_symm_apply_pow i /-- If there is a `n`-th primitive root of unity in `R` and `b` divides `n`, then there is a `b`-th primitive root of unity in `R`. -/ lemma pow {ΞΆ : R} {n : β„•} {a b : β„•} (hn : 0 < n) (h : is_primitive_root ΞΆ n) (hprod : n = a * b) : is_primitive_root (ΞΆ ^ a) b := begin subst n, simp only [iff_def, ← pow_mul, h.pow_eq_one, eq_self_iff_true, true_and], intros l hl, have ha0 : a β‰  0, { rintro rfl, simpa only [nat.not_lt_zero, zero_mul] using hn }, rwa ← mul_dvd_mul_iff_left ha0, exact h.dvd_of_pow_eq_one _ hl end variables [is_domain R] lemma gpowers_eq {k : β„•+} {ΞΆ : units R} (h : is_primitive_root ΞΆ k) : subgroup.gpowers ΞΆ = roots_of_unity k R := begin apply set_like.coe_injective, haveI : fact (0 < (k : β„•)) := ⟨k.pos⟩, haveI F : fintype (subgroup.gpowers ΞΆ) := fintype.of_equiv _ (h.zmod_equiv_gpowers).to_equiv, refine @set.eq_of_subset_of_card_le (units R) (subgroup.gpowers ΞΆ) (roots_of_unity k R) F (roots_of_unity.fintype R k) (subgroup.gpowers_subset $ show ΞΆ ∈ roots_of_unity k R, from h.pow_eq_one) _, calc fintype.card (roots_of_unity k R) ≀ k : card_roots_of_unity R k ... = fintype.card (zmod k) : (zmod.card k).symm ... = fintype.card (subgroup.gpowers ΞΆ) : fintype.card_congr (h.zmod_equiv_gpowers).to_equiv end lemma eq_pow_of_mem_roots_of_unity {k : β„•+} {ΞΆ ΞΎ : units R} (h : is_primitive_root ΞΆ k) (hΞΎ : ΞΎ ∈ roots_of_unity k R) : βˆƒ (i : β„•) (hi : i < k), ΞΆ ^ i = ΞΎ := begin obtain ⟨n, rfl⟩ : βˆƒ n : β„€, ΞΆ ^ n = ΞΎ, by rwa [← h.gpowers_eq] at hΞΎ, have hk0 : (0 : β„€) < k := by exact_mod_cast k.pos, let i := n % k, have hi0 : 0 ≀ i := int.mod_nonneg _ (ne_of_gt hk0), lift i to β„• using hi0 with iβ‚€ hiβ‚€, refine ⟨iβ‚€, _, _⟩, { zify, rw [hiβ‚€], exact int.mod_lt_of_pos _ hk0 }, { have aux := h.gpow_eq_one, rw [← coe_coe] at aux, rw [← gpow_coe_nat, hiβ‚€, ← int.mod_add_div n k, gpow_add, gpow_mul, aux, one_gpow, mul_one] } end lemma eq_pow_of_pow_eq_one {k : β„•} {ΞΆ ΞΎ : R} (h : is_primitive_root ΞΆ k) (hΞΎ : ΞΎ ^ k = 1) (h0 : 0 < k) : βˆƒ i < k, ΞΆ ^ i = ΞΎ := begin obtain ⟨΢, rfl⟩ := h.is_unit h0, obtain ⟨ξ, rfl⟩ := is_unit_of_pow_eq_one ΞΎ k hΞΎ h0, obtain ⟨k, rfl⟩ : βˆƒ k' : β„•+, k = k' := ⟨⟨k, h0⟩, rfl⟩, simp only [← units.coe_pow, ← units.ext_iff], rw coe_units_iff at h, apply h.eq_pow_of_mem_roots_of_unity, rw [mem_roots_of_unity, units.ext_iff, units.coe_pow, hΞΎ, units.coe_one] end lemma is_primitive_root_iff' {k : β„•+} {ΞΆ ΞΎ : units R} (h : is_primitive_root ΞΆ k) : is_primitive_root ΞΎ k ↔ βˆƒ (i < (k : β„•)) (hi : i.coprime k), ΞΆ ^ i = ΞΎ := begin split, { intro hΞΎ, obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_mem_roots_of_unity hΞΎ.pow_eq_one, rw h.pow_iff_coprime k.pos at hΞΎ, exact ⟨i, hik, hΞΎ, rfl⟩ }, { rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi } end lemma is_primitive_root_iff {k : β„•} {ΞΆ ΞΎ : R} (h : is_primitive_root ΞΆ k) (h0 : 0 < k) : is_primitive_root ΞΎ k ↔ βˆƒ (i < k) (hi : i.coprime k), ΞΆ ^ i = ΞΎ := begin split, { intro hΞΎ, obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_pow_eq_one hΞΎ.pow_eq_one h0, rw h.pow_iff_coprime h0 at hΞΎ, exact ⟨i, hik, hΞΎ, rfl⟩ }, { rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi } end lemma card_roots_of_unity' {n : β„•+} (h : is_primitive_root ΞΆ n) : fintype.card (roots_of_unity n R) = n := begin haveI : fact (0 < ↑n) := ⟨n.pos⟩, let e := h.zmod_equiv_gpowers, haveI F : fintype (subgroup.gpowers ΞΆ) := fintype.of_equiv _ e.to_equiv, calc fintype.card (roots_of_unity n R) = fintype.card (subgroup.gpowers ΞΆ) : fintype.card_congr $ by rw h.gpowers_eq ... = fintype.card (zmod n) : fintype.card_congr e.to_equiv.symm ... = n : zmod.card n end lemma card_roots_of_unity {ΞΆ : R} {n : β„•+} (h : is_primitive_root ΞΆ n) : fintype.card (roots_of_unity n R) = n := begin obtain ⟨΢, h΢⟩ := h.is_unit n.pos, rw [← hΞΆ, is_primitive_root.coe_units_iff] at h, exact h.card_roots_of_unity' end /-- The cardinality of the multiset `nth_roots ↑n (1 : R)` is `n` if there is a primitive root of unity in `R`. -/ lemma card_nth_roots {ΞΆ : R} {n : β„•} (h : is_primitive_root ΞΆ n) : (nth_roots n (1 : R)).card = n := begin cases nat.eq_zero_or_pos n with hzero hpos, { simp only [hzero, multiset.card_zero, nth_roots_zero] }, rw eq_iff_le_not_lt, use card_nth_roots n 1, { rw [not_lt], have hcard : fintype.card {x // x ∈ nth_roots n (1 : R)} ≀ (nth_roots n (1 : R)).attach.card := multiset.card_le_of_le (multiset.erase_dup_le _), rw multiset.card_attach at hcard, rw ← pnat.to_pnat'_coe hpos at hcard h ⊒, set m := nat.to_pnat' n, rw [← fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at hcard, exact hcard } end /-- The multiset `nth_roots ↑n (1 : R)` has no repeated elements if there is a primitive root of unity in `R`. -/ lemma nth_roots_nodup {ΞΆ : R} {n : β„•} (h : is_primitive_root ΞΆ n) : (nth_roots n (1 : R)).nodup := begin cases nat.eq_zero_or_pos n with hzero hpos, { simp only [hzero, multiset.nodup_zero, nth_roots_zero] }, apply (@multiset.erase_dup_eq_self R _ _).1, rw eq_iff_le_not_lt, split, { exact multiset.erase_dup_le (nth_roots n (1 : R)) }, { by_contra ha, replace ha := multiset.card_lt_of_lt ha, rw card_nth_roots h at ha, have hrw : (nth_roots n (1 : R)).erase_dup.card = fintype.card {x // x ∈ (nth_roots n (1 : R))}, { set fs := (⟨(nth_roots n (1 : R)).erase_dup, multiset.nodup_erase_dup _⟩ : finset R), rw [← finset.card_mk, ← fintype.card_of_subtype fs _], intro x, simp only [multiset.mem_erase_dup, finset.mem_mk] }, rw ← pnat.to_pnat'_coe hpos at h hrw ha, set m := nat.to_pnat' n, rw [hrw, ← fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at ha, exact nat.lt_asymm ha ha } end @[simp] lemma card_nth_roots_finset {ΞΆ : R} {n : β„•} (h : is_primitive_root ΞΆ n) : (nth_roots_finset n R).card = n := by rw [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h), card_mk, h.card_nth_roots] open_locale nat /-- If an integral domain has a primitive `k`-th root of unity, then it has `Ο† k` of them. -/ lemma card_primitive_roots {ΞΆ : R} {k : β„•} (h : is_primitive_root ΞΆ k) (h0 : 0 < k) : (primitive_roots k R).card = Ο† k := begin symmetry, refine finset.card_congr (Ξ» i _, ΞΆ ^ i) _ _ _, { simp only [true_and, and_imp, mem_filter, mem_range, mem_univ], rintro i - hi, rw mem_primitive_roots h0, exact h.pow_of_coprime i hi.symm }, { simp only [true_and, and_imp, mem_filter, mem_range, mem_univ], rintro i j hi - hj - H, exact h.pow_inj hi hj H }, { simp only [exists_prop, true_and, mem_filter, mem_range, mem_univ], intros ΞΎ hΞΎ, rw [mem_primitive_roots h0, h.is_primitive_root_iff h0] at hΞΎ, rcases hΞΎ with ⟨i, hin, hi, H⟩, exact ⟨i, ⟨hin, hi.symm⟩, H⟩ } end /-- The sets `primitive_roots k R` are pairwise disjoint. -/ lemma disjoint {k l : β„•} (hk : 0 < k) (hl : 0 < l) (h : k β‰  l) : disjoint (primitive_roots k R) (primitive_roots l R) := begin intro z, simp only [finset.inf_eq_inter, finset.mem_inter, mem_primitive_roots, hk, hl, iff_def], rintro ⟨⟨hzk, Hzk⟩, ⟨hzl, Hzl⟩⟩, apply_rules [h, nat.dvd_antisymm, Hzk, Hzl, hzk, hzl] end /-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n` if there is a primitive root of unity in `R`. -/ lemma nth_roots_one_eq_bUnion_primitive_roots' {ΞΆ : R} {n : β„•+} (h : is_primitive_root ΞΆ n) : nth_roots_finset n R = (nat.divisors ↑n).bUnion (Ξ» i, (primitive_roots i R)) := begin symmetry, apply finset.eq_of_subset_of_card_le, { intros x, simp only [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h), exists_prop, finset.mem_bUnion, finset.mem_filter, finset.mem_range, mem_nth_roots, finset.mem_mk, nat.mem_divisors, and_true, ne.def, pnat.ne_zero, pnat.pos, not_false_iff], rintro ⟨a, ⟨d, hd⟩, ha⟩, have hazero : 0 < a, { contrapose! hd with ha0, simp only [nonpos_iff_eq_zero, zero_mul, *] at *, exact n.ne_zero }, rw mem_primitive_roots hazero at ha, rw [hd, pow_mul, ha.pow_eq_one, one_pow] }, { apply le_of_eq, rw [h.card_nth_roots_finset, finset.card_bUnion], { rw [← nat.sum_totient n, nat.filter_dvd_eq_divisors (pnat.ne_zero n), sum_congr rfl] { occs := occurrences.pos [1] }, simp only [finset.mem_filter, finset.mem_range, nat.mem_divisors], rintro k ⟨H, hk⟩, have hdvd := H, rcases H with ⟨d, hd⟩, rw mul_comm at hd, rw (h.pow n.pos hd).card_primitive_roots (pnat.pos_of_div_pos hdvd) }, { intros i hi j hj hdiff, simp only [nat.mem_divisors, and_true, ne.def, pnat.ne_zero, not_false_iff] at hi hj, exact disjoint (pnat.pos_of_div_pos hi) (pnat.pos_of_div_pos hj) hdiff } } end /-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n` if there is a primitive root of unity in `R`. -/ lemma nth_roots_one_eq_bUnion_primitive_roots {ΞΆ : R} {n : β„•} (hpos : 0 < n) (h : is_primitive_root ΞΆ n) : nth_roots_finset n R = (nat.divisors n).bUnion (Ξ» i, (primitive_roots i R)) := @nth_roots_one_eq_bUnion_primitive_roots' _ _ _ _ ⟨n, hpos⟩ h end is_domain section minpoly open minpoly variables {n : β„•} {K : Type*} [field K] {ΞΌ : K} (h : is_primitive_root ΞΌ n) (hpos : 0 < n) include n ΞΌ h hpos /--`ΞΌ` is integral over `β„€`. -/ lemma is_integral : is_integral β„€ ΞΌ := begin use (X ^ n - 1), split, { exact (monic_X_pow_sub_C 1 (ne_of_lt hpos).symm) }, { simp only [((is_primitive_root.iff_def ΞΌ n).mp h).left, evalβ‚‚_one, evalβ‚‚_X_pow, evalβ‚‚_sub, sub_self] } end variables [char_zero K] /--The minimal polynomial of a root of unity `ΞΌ` divides `X ^ n - 1`. -/ lemma minpoly_dvd_X_pow_sub_one : minpoly β„€ ΞΌ ∣ X ^ n - 1 := begin apply minpoly.gcd_domain_dvd β„š (is_integral h hpos) (polynomial.monic.is_primitive (monic_X_pow_sub_C 1 (ne_of_lt hpos).symm)), simp only [((is_primitive_root.iff_def ΞΌ n).mp h).left, aeval_X_pow, ring_hom.eq_int_cast, int.cast_one, aeval_one, alg_hom.map_sub, sub_self] end /-- The reduction modulo `p` of the minimal polynomial of a root of unity `ΞΌ` is separable. -/ lemma separable_minpoly_mod {p : β„•} [fact p.prime] (hdiv : Β¬p ∣ n) : separable (map (int.cast_ring_hom (zmod p)) (minpoly β„€ ΞΌ)) := begin have hdvd : (map (int.cast_ring_hom (zmod p)) (minpoly β„€ ΞΌ)) ∣ X ^ n - 1, { simpa [map_pow, map_X, map_one, map_sub] using ring_hom.map_dvd (map_ring_hom (int.cast_ring_hom (zmod p))) (minpoly_dvd_X_pow_sub_one h hpos) }, refine separable.of_dvd (separable_X_pow_sub_C 1 _ one_ne_zero) hdvd, by_contra hzero, exact hdiv ((zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 hzero) end /-- The reduction modulo `p` of the minimal polynomial of a root of unity `ΞΌ` is squarefree. -/ lemma squarefree_minpoly_mod {p : β„•} [fact p.prime] (hdiv : Β¬ p ∣ n) : squarefree (map (int.cast_ring_hom (zmod p)) (minpoly β„€ ΞΌ)) := (separable_minpoly_mod h hpos hdiv).squarefree /- Let `P` be the minimal polynomial of a root of unity `ΞΌ` and `Q` be the minimal polynomial of `ΞΌ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `expand β„€ p Q`. -/ lemma minpoly_dvd_expand {p : β„•} (hprime : nat.prime p) (hdiv : Β¬ p ∣ n) : minpoly β„€ ΞΌ ∣ expand β„€ p (minpoly β„€ (ΞΌ ^ p)) := begin apply minpoly.gcd_domain_dvd β„š (h.is_integral hpos), { apply monic.is_primitive, rw [polynomial.monic, leading_coeff, nat_degree_expand, mul_comm, coeff_expand_mul' (nat.prime.pos hprime), ← leading_coeff, ← polynomial.monic], exact minpoly.monic (is_integral (pow_of_prime h hprime hdiv) hpos) }, { rw [aeval_def, coe_expand, ← comp, evalβ‚‚_eq_eval_map, map_comp, map_pow, map_X, eval_comp, eval_pow, eval_X, ← evalβ‚‚_eq_eval_map, ← aeval_def], exact minpoly.aeval _ _ } end /- Let `P` be the minimal polynomial of a root of unity `ΞΌ` and `Q` be the minimal polynomial of `ΞΌ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q ^ p` modulo `p`. -/ lemma minpoly_dvd_pow_mod {p : β„•} [hprime : fact p.prime] (hdiv : Β¬ p ∣ n) : map (int.cast_ring_hom (zmod p)) (minpoly β„€ ΞΌ) ∣ map (int.cast_ring_hom (zmod p)) (minpoly β„€ (ΞΌ ^ p)) ^ p := begin set Q := minpoly β„€ (ΞΌ ^ p), have hfrob : map (int.cast_ring_hom (zmod p)) Q ^ p = map (int.cast_ring_hom (zmod p)) (expand β„€ p Q), by rw [← zmod.expand_card, map_expand hprime.1.pos], rw [hfrob], apply ring_hom.map_dvd (map_ring_hom (int.cast_ring_hom (zmod p))), exact minpoly_dvd_expand h hpos hprime.1 hdiv end /- Let `P` be the minimal polynomial of a root of unity `ΞΌ` and `Q` be the minimal polynomial of `ΞΌ ^ p`, where `p` is a prime that does not divide `n`. Then `P` divides `Q` modulo `p`. -/ lemma minpoly_dvd_mod_p {p : β„•} [hprime : fact p.prime] (hdiv : Β¬ p ∣ n) : map (int.cast_ring_hom (zmod p)) (minpoly β„€ ΞΌ) ∣ map (int.cast_ring_hom (zmod p)) (minpoly β„€ (ΞΌ ^ p)) := (unique_factorization_monoid.dvd_pow_iff_dvd_of_squarefree (squarefree_minpoly_mod h hpos hdiv) hprime.1.ne_zero).1 (minpoly_dvd_pow_mod h hpos hdiv) /-- If `p` is a prime that does not divide `n`, then the minimal polynomials of a primitive `n`-th root of unity `ΞΌ` and of `ΞΌ ^ p` are the same. -/ lemma minpoly_eq_pow {p : β„•} [hprime : fact p.prime] (hdiv : Β¬ p ∣ n) : minpoly β„€ ΞΌ = minpoly β„€ (ΞΌ ^ p) := begin by_contra hdiff, set P := minpoly β„€ ΞΌ, set Q := minpoly β„€ (ΞΌ ^ p), have Pmonic : P.monic := minpoly.monic (h.is_integral hpos), have Qmonic : Q.monic := minpoly.monic ((h.pow_of_prime hprime.1 hdiv).is_integral hpos), have Pirr : irreducible P := minpoly.irreducible (h.is_integral hpos), have Qirr : irreducible Q := minpoly.irreducible ((h.pow_of_prime hprime.1 hdiv).is_integral hpos), have PQprim : is_primitive (P * Q) := Pmonic.is_primitive.mul Qmonic.is_primitive, have prod : P * Q ∣ X ^ n - 1, { rw [(is_primitive.int.dvd_iff_map_cast_dvd_map_cast (P * Q) (X ^ n - 1) PQprim (monic_X_pow_sub_C (1 : β„€) (ne_of_gt hpos)).is_primitive), map_mul], refine is_coprime.mul_dvd _ _ _, { have aux := is_primitive.int.irreducible_iff_irreducible_map_cast Pmonic.is_primitive, refine (dvd_or_coprime _ _ (aux.1 Pirr)).resolve_left _, rw map_dvd_map (int.cast_ring_hom β„š) int.cast_injective Pmonic, intro hdiv, refine hdiff (eq_of_monic_of_associated Pmonic Qmonic _), exact associated_of_dvd_dvd hdiv (Pirr.dvd_symm Qirr hdiv) }, { apply (map_dvd_map (int.cast_ring_hom β„š) int.cast_injective Pmonic).2, exact minpoly_dvd_X_pow_sub_one h hpos }, { apply (map_dvd_map (int.cast_ring_hom β„š) int.cast_injective Qmonic).2, exact minpoly_dvd_X_pow_sub_one (pow_of_prime h hprime.1 hdiv) hpos } }, replace prod := ring_hom.map_dvd ((map_ring_hom (int.cast_ring_hom (zmod p)))) prod, rw [coe_map_ring_hom, map_mul, map_sub, map_one, map_pow, map_X] at prod, obtain ⟨R, hR⟩ := minpoly_dvd_mod_p h hpos hdiv, rw [hR, ← mul_assoc, ← map_mul, ← sq, map_pow] at prod, have habs : map (int.cast_ring_hom (zmod p)) P ^ 2 ∣ map (int.cast_ring_hom (zmod p)) P ^ 2 * R, { use R }, replace habs := lt_of_lt_of_le (enat.coe_lt_coe.2 one_lt_two) (multiplicity.le_multiplicity_of_pow_dvd (dvd_trans habs prod)), have hfree : squarefree (X ^ n - 1 : polynomial (zmod p)), { refine squarefree_X_pow_sub_C 1 _ one_ne_zero, by_contra hzero, exact hdiv ((zmod.nat_coe_zmod_eq_zero_iff_dvd n p).1 hzero) }, cases (multiplicity.squarefree_iff_multiplicity_le_one (X ^ n - 1)).1 hfree (map (int.cast_ring_hom (zmod p)) P) with hle hunit, { rw nat.cast_one at habs, exact hle.not_lt habs }, { replace hunit := degree_eq_zero_of_is_unit hunit, rw degree_map_eq_of_leading_coeff_ne_zero (int.cast_ring_hom (zmod p)) _ at hunit, { exact (minpoly.degree_pos (is_integral h hpos)).ne' hunit }, simp only [Pmonic, ring_hom.eq_int_cast, monic.leading_coeff, int.cast_one, ne.def, not_false_iff, one_ne_zero] } end /-- If `m : β„•` is coprime with `n`, then the minimal polynomials of a primitive `n`-th root of unity `ΞΌ` and of `ΞΌ ^ m` are the same. -/ lemma minpoly_eq_pow_coprime {m : β„•} (hcop : nat.coprime m n) : minpoly β„€ ΞΌ = minpoly β„€ (ΞΌ ^ m) := begin revert n hcop, refine unique_factorization_monoid.induction_on_prime m _ _ _, { intros n hn h hpos, congr, simpa [(nat.coprime_zero_left n).mp hn] using h }, { intros u hunit n hcop h hpos, congr, simp [nat.is_unit_iff.mp hunit] }, { intros a p ha hprime hind n hcop h hpos, rw hind (nat.coprime.coprime_mul_left hcop) h hpos, clear hind, replace hprime := nat.prime_iff.2 hprime, have hdiv := (nat.prime.coprime_iff_not_dvd hprime).1 (nat.coprime.coprime_mul_right hcop), haveI := fact.mk hprime, rw [minpoly_eq_pow (h.pow_of_coprime a (nat.coprime.coprime_mul_left hcop)) hpos hdiv], congr' 1, ring_exp } end /-- If `m : β„•` is coprime with `n`, then the minimal polynomial of a primitive `n`-th root of unity `ΞΌ` has `ΞΌ ^ m` as root. -/ lemma pow_is_root_minpoly {m : β„•} (hcop : nat.coprime m n) : is_root (map (int.cast_ring_hom K) (minpoly β„€ ΞΌ)) (ΞΌ ^ m) := by simpa [minpoly_eq_pow_coprime h hpos hcop, eval_map, aeval_def (ΞΌ ^ m) _] using minpoly.aeval β„€ (ΞΌ ^ m) /-- `primitive_roots n K` is a subset of the roots of the minimal polynomial of a primitive `n`-th root of unity `ΞΌ`. -/ lemma is_roots_of_minpoly : primitive_roots n K βŠ† (map (int.cast_ring_hom K) (minpoly β„€ ΞΌ)).roots.to_finset := begin intros x hx, obtain ⟨m, hle, hcop, rfl⟩ := (is_primitive_root_iff h hpos).1 ((mem_primitive_roots hpos).1 hx), simpa [multiset.mem_to_finset, mem_roots (map_monic_ne_zero $ minpoly.monic $ is_integral h hpos)] using pow_is_root_minpoly h hpos hcop end /-- The degree of the minimal polynomial of `ΞΌ` is at least `totient n`. -/ lemma totient_le_degree_minpoly : nat.totient n ≀ (minpoly β„€ ΞΌ).nat_degree := let P : polynomial β„€ := minpoly β„€ ΞΌ,-- minimal polynomial of `ΞΌ` P_K : polynomial K := map (int.cast_ring_hom K) P -- minimal polynomial of `ΞΌ` sent to `K[X]` in calc n.totient = (primitive_roots n K).card : (h.card_primitive_roots hpos).symm ... ≀ P_K.roots.to_finset.card : finset.card_le_of_subset (is_roots_of_minpoly h hpos) ... ≀ P_K.roots.card : multiset.to_finset_card_le _ ... ≀ P_K.nat_degree : (card_roots' $ map_monic_ne_zero (minpoly.monic $ is_integral h hpos)) ... ≀ P.nat_degree : nat_degree_map_le _ _ end minpoly end is_primitive_root
e625dacdbb4948012ee3b3f71491267465827a61
9dc8cecdf3c4634764a18254e94d43da07142918
/src/field_theory/ratfunc.lean
234b2cfa9914f9de6035fe874a2090a4cae9fecb
[ "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
59,954
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import ring_theory.euclidean_domain import ring_theory.laurent_series import ring_theory.localization.fraction_ring import ring_theory.polynomial.content /-! # The field of rational functions This file defines the field `ratfunc K` of rational functions over a field `K`, and shows it is the field of fractions of `polynomial K`. ## Main definitions Working with rational functions as polynomials: - `ratfunc.field` provides a field structure - `ratfunc.C` is the constant polynomial - `ratfunc.X` is the indeterminate - `ratfunc.eval` evaluates a rational function given a value for the indeterminate You can use `is_fraction_ring` API to treat `ratfunc` as the field of fractions of polynomials: * `algebra_map K[X] (ratfunc K)` maps polynomials to rational functions * `is_fraction_ring.alg_equiv` maps other fields of fractions of `polynomial K` to `ratfunc K`, in particular: * `fraction_ring.alg_equiv K[X] (ratfunc K)` maps the generic field of fraction construction to `ratfunc K`. Combine this with `alg_equiv.restrict_scalars` to change the `fraction_ring K[X] ≃ₐ[polynomial K] ratfunc K` to `fraction_ring K[X] ≃ₐ[K] ratfunc K`. Working with rational functions as fractions: - `ratfunc.num` and `ratfunc.denom` give the numerator and denominator. These values are chosen to be coprime and such that `ratfunc.denom` is monic. Embedding of rational functions into Laurent series, provided as a coercion, utilizing the underlying `ratfunc.coe_alg_hom`. Lifting homomorphisms of polynomials to other types, by mapping and dividing, as long as the homomorphism retains the non-zero-divisor property: - `ratfunc.lift_monoid_with_zero_hom` lifts a `polynomial K β†’*β‚€ Gβ‚€` to a `ratfunc K β†’*β‚€ Gβ‚€`, where `[comm_ring K] [comm_group_with_zero Gβ‚€]` - `ratfunc.lift_ring_hom` lifts a `polynomial K β†’+* L` to a `ratfunc K β†’+* L`, where `[comm_ring K] [field L]` - `ratfunc.lift_alg_hom` lifts a `polynomial K →ₐ[S] L` to a `ratfunc K →ₐ[S] L`, where `[comm_ring K] [field L] [comm_semiring S] [algebra S K[X]] [algebra S L]` This is satisfied by injective homs. We also have lifting homomorphisms of polynomials to other polynomials, with the same condition on retaining the non-zero-divisor property across the map: - `ratfunc.map` lifts `polynomial K β†’* R[X]` when `[comm_ring K] [comm_ring R]` - `ratfunc.map_ring_hom` lifts `polynomial K β†’+* R[X]` when `[comm_ring K] [comm_ring R]` - `ratfunc.map_alg_hom` lifts `polynomial K →ₐ[S] R[X]` when `[comm_ring K] [is_domain K] [comm_ring R] [is_domain R]` We also have a set of recursion and induction principles: - `ratfunc.lift_on`: define a function by mapping a fraction of polynomials `p/q` to `f p q`, if `f` is well-defined in the sense that `p/q = p'/q' β†’ f p q = f p' q'`. - `ratfunc.lift_on'`: define a function by mapping a fraction of polynomials `p/q` to `f p q`, if `f` is well-defined in the sense that `f (a * p) (a * q) = f p' q'`. - `ratfunc.induction_on`: if `P` holds on `p / q` for all polynomials `p q`, then `P` holds on all rational functions We define the degree of a rational function, with values in `β„€`: - `int_degree` is the degree of a rational function, defined as the difference between the `nat_degree` of its numerator and the `nat_degree` of its denominator. In particular, `int_degree 0 = 0`. ## Implementation notes To provide good API encapsulation and speed up unification problems, `ratfunc` is defined as a structure, and all operations are `@[irreducible] def`s We need a couple of maps to set up the `field` and `is_fraction_ring` structure, namely `ratfunc.of_fraction_ring`, `ratfunc.to_fraction_ring`, `ratfunc.mk` and `ratfunc.to_fraction_ring_ring_equiv`. All these maps get `simp`ed to bundled morphisms like `algebra_map K[X] (ratfunc K)` and `is_localization.alg_equiv`. There are separate lifts and maps of homomorphisms, to provide routes of lifting even when the codomain is not a field or even an integral domain. ## References * [Kleiman, *Misconceptions about $K_X$*][kleiman1979] * https://freedommathdance.blogspot.com/2012/11/misconceptions-about-kx.html * https://stacks.math.columbia.edu/tag/01X1 -/ noncomputable theory open_locale classical open_locale non_zero_divisors polynomial universes u v variables (K : Type u) [hring : comm_ring K] [hdomain : is_domain K] include hring /-- `ratfunc K` is `K(x)`, the field of rational functions over `K`. The inclusion of polynomials into `ratfunc` is `algebra_map K[X] (ratfunc K)`, the maps between `ratfunc K` and another field of fractions of `polynomial K`, especially `fraction_ring K[X]`, are given by `is_localization.algebra_equiv`. -/ structure ratfunc : Type u := of_fraction_ring :: (to_fraction_ring : fraction_ring K[X]) namespace ratfunc variables {K} section rec /-! ### Constructing `ratfunc`s and their induction principles -/ lemma of_fraction_ring_injective : function.injective (of_fraction_ring : _ β†’ ratfunc K) := Ξ» x y, of_fraction_ring.inj lemma to_fraction_ring_injective : function.injective (to_fraction_ring : _ β†’ fraction_ring K[X]) | ⟨x⟩ ⟨y⟩ rfl := rfl /-- Non-dependent recursion principle for `ratfunc K`: To construct a term of `P : Sort*` out of `x : ratfunc K`, it suffices to provide a constructor `f : Ξ  (p q : K[X]), P` and a proof that `f p q = f p' q'` for all `p q p' q'` such that `p * q' = p' * q` where both `q` and `q'` are not zero divisors, stated as `q βˆ‰ K[X]⁰`, `q' βˆ‰ K[X]⁰`. If considering `K` as an integral domain, this is the same as saying that we construct a value of `P` for such elements of `ratfunc K` by setting `lift_on (p / q) f _ = f p q`. When `[is_domain K]`, one can use `ratfunc.lift_on'`, which has the stronger requirement of `βˆ€ {p q a : K[X]} (hq : q β‰  0) (ha : a β‰  0), f (a * p) (a * q) = f p q)`. -/ @[irreducible] protected def lift_on {P : Sort v} (x : ratfunc K) (f : βˆ€ (p q : K[X]), P) (H : βˆ€ {p q p' q'} (hq : q ∈ K[X]⁰) (hq' : q' ∈ K[X]⁰), p * q' = p' * q β†’ f p q = f p' q') : P := localization.lift_on (to_fraction_ring x) (Ξ» p q, f p q) (Ξ» p p' q q' h, H q.2 q'.2 (let ⟨⟨c, hc⟩, mul_eq⟩ := (localization.r_iff_exists).mp h in mul_cancel_right_coe_non_zero_divisor.mp mul_eq)) lemma lift_on_of_fraction_ring_mk {P : Sort v} (n : K[X]) (d : K[X]⁰) (f : βˆ€ (p q : K[X]), P) (H : βˆ€ {p q p' q'} (hq : q ∈ K[X]⁰) (hq' : q' ∈ K[X]⁰), p * q' = p' * q β†’ f p q = f p' q') : ratfunc.lift_on (of_fraction_ring (localization.mk n d)) f @H = f n d := begin unfold ratfunc.lift_on, exact localization.lift_on_mk _ _ _ _ end include hdomain /-- `ratfunc.mk (p q : K[X])` is `p / q` as a rational function. If `q = 0`, then `mk` returns 0. This is an auxiliary definition used to define an `algebra` structure on `ratfunc`; the `simp` normal form of `mk p q` is `algebra_map _ _ p / algebra_map _ _ q`. -/ @[irreducible] protected def mk (p q : K[X]) : ratfunc K := of_fraction_ring (algebra_map _ _ p / algebra_map _ _ q) lemma mk_eq_div' (p q : K[X]) : ratfunc.mk p q = of_fraction_ring (algebra_map _ _ p / algebra_map _ _ q) := by unfold ratfunc.mk lemma mk_zero (p : K[X]) : ratfunc.mk p 0 = of_fraction_ring 0 := by rw [mk_eq_div', ring_hom.map_zero, div_zero] lemma mk_coe_def (p : K[X]) (q : K[X]⁰) : ratfunc.mk p q = of_fraction_ring (is_localization.mk' _ p q) := by simp only [mk_eq_div', ← localization.mk_eq_mk', fraction_ring.mk_eq_div] lemma mk_def_of_mem (p : K[X]) {q} (hq : q ∈ K[X]⁰) : ratfunc.mk p q = of_fraction_ring (is_localization.mk' _ p ⟨q, hq⟩) := by simp only [← mk_coe_def, set_like.coe_mk] lemma mk_def_of_ne (p : K[X]) {q : K[X]} (hq : q β‰  0) : ratfunc.mk p q = of_fraction_ring (is_localization.mk' _ p ⟨q, mem_non_zero_divisors_iff_ne_zero.mpr hq⟩) := mk_def_of_mem p _ lemma mk_eq_localization_mk (p : K[X]) {q : K[X]} (hq : q β‰  0) : ratfunc.mk p q = of_fraction_ring (localization.mk p ⟨q, mem_non_zero_divisors_iff_ne_zero.mpr hq⟩) := by rw [mk_def_of_ne, localization.mk_eq_mk'] lemma mk_one' (p : K[X]) : ratfunc.mk p 1 = of_fraction_ring (algebra_map _ _ p) := by rw [← is_localization.mk'_one (fraction_ring K[X]) p, ← mk_coe_def, submonoid.coe_one] lemma mk_eq_mk {p q p' q' : K[X]} (hq : q β‰  0) (hq' : q' β‰  0) : ratfunc.mk p q = ratfunc.mk p' q' ↔ p * q' = p' * q := by rw [mk_def_of_ne _ hq, mk_def_of_ne _ hq', of_fraction_ring_injective.eq_iff, is_localization.mk'_eq_iff_eq, set_like.coe_mk, set_like.coe_mk, (is_fraction_ring.injective K[X] (fraction_ring K[X])).eq_iff] lemma lift_on_mk {P : Sort v} (p q : K[X]) (f : βˆ€ (p q : K[X]), P) (f0 : βˆ€ p, f p 0 = f 0 1) (H' : βˆ€ {p q p' q'} (hq : q β‰  0) (hq' : q' β‰  0), p * q' = p' * q β†’ f p q = f p' q') (H : βˆ€ {p q p' q'} (hq : q ∈ K[X]⁰) (hq' : q' ∈ K[X]⁰), p * q' = p' * q β†’ f p q = f p' q' := Ξ» p q p' q' hq hq' h, H' (non_zero_divisors.ne_zero hq) (non_zero_divisors.ne_zero hq') h) : (ratfunc.mk p q).lift_on f @H = f p q := begin by_cases hq : q = 0, { subst hq, simp only [mk_zero, f0, ← localization.mk_zero 1, localization.lift_on_mk, lift_on_of_fraction_ring_mk, submonoid.coe_one], }, { simp only [mk_eq_localization_mk _ hq, localization.lift_on_mk, lift_on_of_fraction_ring_mk, set_like.coe_mk] } end lemma lift_on_condition_of_lift_on'_condition {P : Sort v} {f : βˆ€ (p q : K[X]), P} (H : βˆ€ {p q a} (hq : q β‰  0) (ha : a β‰  0), f (a * p) (a * q) = f p q) ⦃p q p' q' : K[X]⦄ (hq : q β‰  0) (hq' : q' β‰  0) (h : p * q' = p' * q) : f p q = f p' q' := begin have H0 : f 0 q = f 0 q', { calc f 0 q = f (q' * 0) (q' * q) : (H hq hq').symm ... = f (q * 0) (q * q') : by rw [mul_zero, mul_zero, mul_comm] ... = f 0 q' : H hq' hq }, by_cases hp : p = 0, { simp only [hp, hq, zero_mul, or_false, zero_eq_mul] at ⊒ h, rw [h, H0] }, by_cases hp' : p' = 0, { simpa only [hp, hp', hq', zero_mul, or_self, mul_eq_zero] using h }, calc f p q = f (p' * p) (p' * q) : (H hq hp').symm ... = f (p * p') (p * q') : by rw [mul_comm p p', h] ... = f p' q' : H hq' hp end -- f /-- Non-dependent recursion principle for `ratfunc K`: if `f p q : P` for all `p q`, such that `f (a * p) (a * q) = f p q`, then we can find a value of `P` for all elements of `ratfunc K` by setting `lift_on' (p / q) f _ = f p q`. The value of `f p 0` for any `p` is never used and in principle this may be anything, although many usages of `lift_on'` assume `f p 0 = f 0 1`. -/ @[irreducible] protected def lift_on' {P : Sort v} (x : ratfunc K) (f : βˆ€ (p q : K[X]), P) (H : βˆ€ {p q a} (hq : q β‰  0) (ha : a β‰  0), f (a * p) (a * q) = f p q) : P := x.lift_on f (Ξ» p q p' q' hq hq', lift_on_condition_of_lift_on'_condition @H (non_zero_divisors.ne_zero hq) (non_zero_divisors.ne_zero hq')) lemma lift_on'_mk {P : Sort v} (p q : K[X]) (f : βˆ€ (p q : K[X]), P) (f0 : βˆ€ p, f p 0 = f 0 1) (H : βˆ€ {p q a} (hq : q β‰  0) (ha : a β‰  0), f (a * p) (a * q) = f p q) : (ratfunc.mk p q).lift_on' f @H = f p q := begin rw [ratfunc.lift_on', ratfunc.lift_on_mk _ _ _ f0], exact lift_on_condition_of_lift_on'_condition @H end /-- Induction principle for `ratfunc K`: if `f p q : P (ratfunc.mk p q)` for all `p q`, then `P` holds on all elements of `ratfunc K`. See also `induction_on`, which is a recursion principle defined in terms of `algebra_map`. -/ @[irreducible] protected lemma induction_on' {P : ratfunc K β†’ Prop} : Ξ  (x : ratfunc K) (f : βˆ€ (p q : K[X]) (hq : q β‰  0), P (ratfunc.mk p q)), P x | ⟨x⟩ f := localization.induction_on x (Ξ» ⟨p, q⟩, by simpa only [mk_coe_def, localization.mk_eq_mk'] using f p q (mem_non_zero_divisors_iff_ne_zero.mp q.2)) end rec section field /-! ### Defining the field structure -/ /-- The zero rational function. -/ @[irreducible] protected def zero : ratfunc K := ⟨0⟩ instance : has_zero (ratfunc K) := ⟨ratfunc.zero⟩ lemma of_fraction_ring_zero : (of_fraction_ring 0 : ratfunc K) = 0 := by unfold has_zero.zero ratfunc.zero /-- Addition of rational functions. -/ @[irreducible] protected def add : ratfunc K β†’ ratfunc K β†’ ratfunc K | ⟨p⟩ ⟨q⟩ := ⟨p + q⟩ instance : has_add (ratfunc K) := ⟨ratfunc.add⟩ lemma of_fraction_ring_add (p q : fraction_ring K[X]) : of_fraction_ring (p + q) = of_fraction_ring p + of_fraction_ring q := by unfold has_add.add ratfunc.add /-- Subtraction of rational functions. -/ @[irreducible] protected def sub : ratfunc K β†’ ratfunc K β†’ ratfunc K | ⟨p⟩ ⟨q⟩ := ⟨p - q⟩ instance : has_sub (ratfunc K) := ⟨ratfunc.sub⟩ lemma of_fraction_ring_sub (p q : fraction_ring K[X]) : of_fraction_ring (p - q) = of_fraction_ring p - of_fraction_ring q := by unfold has_sub.sub ratfunc.sub /-- Additive inverse of a rational function. -/ @[irreducible] protected def neg : ratfunc K β†’ ratfunc K | ⟨p⟩ := ⟨-p⟩ instance : has_neg (ratfunc K) := ⟨ratfunc.neg⟩ lemma of_fraction_ring_neg (p : fraction_ring K[X]) : of_fraction_ring (-p) = - of_fraction_ring p := by unfold has_neg.neg ratfunc.neg /-- The multiplicative unit of rational functions. -/ @[irreducible] protected def one : ratfunc K := ⟨1⟩ instance : has_one (ratfunc K) := ⟨ratfunc.one⟩ lemma of_fraction_ring_one : (of_fraction_ring 1 : ratfunc K) = 1 := by unfold has_one.one ratfunc.one /-- Multiplication of rational functions. -/ @[irreducible] protected def mul : ratfunc K β†’ ratfunc K β†’ ratfunc K | ⟨p⟩ ⟨q⟩ := ⟨p * q⟩ instance : has_mul (ratfunc K) := ⟨ratfunc.mul⟩ lemma of_fraction_ring_mul (p q : fraction_ring K[X]) : of_fraction_ring (p * q) = of_fraction_ring p * of_fraction_ring q := by unfold has_mul.mul ratfunc.mul include hdomain /-- Division of rational functions. -/ @[irreducible] protected def div : ratfunc K β†’ ratfunc K β†’ ratfunc K | ⟨p⟩ ⟨q⟩ := ⟨p / q⟩ instance : has_div (ratfunc K) := ⟨ratfunc.div⟩ lemma of_fraction_ring_div (p q : fraction_ring K[X]) : of_fraction_ring (p / q) = of_fraction_ring p / of_fraction_ring q := by unfold has_div.div ratfunc.div /-- Multiplicative inverse of a rational function. -/ @[irreducible] protected def inv : ratfunc K β†’ ratfunc K | ⟨p⟩ := ⟨p⁻¹⟩ instance : has_inv (ratfunc K) := ⟨ratfunc.inv⟩ lemma of_fraction_ring_inv (p : fraction_ring K[X]) : of_fraction_ring (p⁻¹) = (of_fraction_ring p)⁻¹ := by unfold has_inv.inv ratfunc.inv -- Auxiliary lemma for the `field` instance lemma mul_inv_cancel : βˆ€ {p : ratfunc K} (hp : p β‰  0), p * p⁻¹ = 1 | ⟨p⟩ h := have p β‰  0 := Ξ» hp, h $ by rw [hp, of_fraction_ring_zero], by simpa only [← of_fraction_ring_inv, ← of_fraction_ring_mul, ← of_fraction_ring_one] using _root_.mul_inv_cancel this section has_smul omit hdomain variables {R : Type*} /-- Scalar multiplication of rational functions. -/ @[irreducible] protected def smul [has_smul R (fraction_ring K[X])] : R β†’ ratfunc K β†’ ratfunc K | r ⟨p⟩ := ⟨r β€’ p⟩ @[nolint fails_quickly] -- cannot reproduce instance [has_smul R (fraction_ring K[X])] : has_smul R (ratfunc K) := ⟨ratfunc.smul⟩ lemma of_fraction_ring_smul [has_smul R (fraction_ring K[X])] (c : R) (p : fraction_ring K[X]) : of_fraction_ring (c β€’ p) = c β€’ of_fraction_ring p := by unfold has_smul.smul ratfunc.smul lemma to_fraction_ring_smul [has_smul R (fraction_ring K[X])] (c : R) (p : ratfunc K) : to_fraction_ring (c β€’ p) = c β€’ to_fraction_ring p := by { cases p, rw ←of_fraction_ring_smul } lemma smul_eq_C_smul (x : ratfunc K) (r : K) : r β€’ x = polynomial.C r β€’ x := begin cases x, induction x, { rw [←of_fraction_ring_smul, ←of_fraction_ring_smul, localization.smul_mk, localization.smul_mk, smul_eq_mul, polynomial.smul_eq_C_mul] }, { simp only } end include hdomain variables [monoid R] [distrib_mul_action R K[X]] variables [htower : is_scalar_tower R K[X] K[X]] include htower lemma mk_smul (c : R) (p q : K[X]) : ratfunc.mk (c β€’ p) q = c β€’ ratfunc.mk p q := begin by_cases hq : q = 0, { rw [hq, mk_zero, mk_zero, ←of_fraction_ring_smul, smul_zero] }, { rw [mk_eq_localization_mk _ hq, mk_eq_localization_mk _ hq, ←localization.smul_mk, ←of_fraction_ring_smul] } end instance : is_scalar_tower R K[X] (ratfunc K) := ⟨λ c p q, q.induction_on' (Ξ» q r _, by rw [← mk_smul, smul_assoc, mk_smul, mk_smul])⟩ end has_smul variables (K) omit hdomain instance [subsingleton K] : subsingleton (ratfunc K) := to_fraction_ring_injective.subsingleton instance : inhabited (ratfunc K) := ⟨0⟩ instance [nontrivial K] : nontrivial (ratfunc K) := of_fraction_ring_injective.nontrivial /-- `ratfunc K` is isomorphic to the field of fractions of `polynomial K`, as rings. This is an auxiliary definition; `simp`-normal form is `is_localization.alg_equiv`. -/ @[simps apply] def to_fraction_ring_ring_equiv : ratfunc K ≃+* fraction_ring K[X] := { to_fun := to_fraction_ring, inv_fun := of_fraction_ring, left_inv := Ξ» ⟨_⟩, rfl, right_inv := Ξ» _, rfl, map_add' := Ξ» ⟨_⟩ ⟨_⟩, by simp [←of_fraction_ring_add], map_mul' := Ξ» ⟨_⟩ ⟨_⟩, by simp [←of_fraction_ring_mul] } omit hring /-- Solve equations for `ratfunc K` by working in `fraction_ring K[X]`. -/ meta def frac_tac : tactic unit := `[repeat { rintro (⟨⟩ : ratfunc _) }, simp only [← of_fraction_ring_zero, ← of_fraction_ring_add, ← of_fraction_ring_sub, ← of_fraction_ring_neg, ← of_fraction_ring_one, ← of_fraction_ring_mul, ← of_fraction_ring_div, ← of_fraction_ring_inv, add_assoc, zero_add, add_zero, mul_assoc, mul_zero, mul_one, mul_add, inv_zero, add_comm, add_left_comm, mul_comm, mul_left_comm, sub_eq_add_neg, div_eq_mul_inv, add_mul, zero_mul, one_mul, neg_mul, mul_neg, add_right_neg]] /-- Solve equations for `ratfunc K` by applying `ratfunc.induction_on`. -/ meta def smul_tac : tactic unit := `[repeat { rintro (⟨⟩ : ratfunc _) <|> intro }, simp_rw [←of_fraction_ring_smul], simp only [add_comm, mul_comm, zero_smul, succ_nsmul, zsmul_eq_mul, mul_add, mul_one, mul_zero, neg_add, mul_neg, int.of_nat_eq_coe, int.coe_nat_succ, int.cast_zero, int.cast_add, int.cast_one, int.cast_neg_succ_of_nat, int.cast_coe_nat, nat.cast_succ, localization.mk_zero, localization.add_mk_self, localization.neg_mk, of_fraction_ring_zero, ← of_fraction_ring_add, ← of_fraction_ring_neg]] include hring instance : comm_ring (ratfunc K) := { add := (+), add_assoc := by frac_tac, add_comm := by frac_tac, zero := 0, zero_add := by frac_tac, add_zero := by frac_tac, neg := has_neg.neg, add_left_neg := by frac_tac, sub := has_sub.sub, sub_eq_add_neg := by frac_tac, mul := (*), mul_assoc := by frac_tac, mul_comm := by frac_tac, left_distrib := by frac_tac, right_distrib := by frac_tac, one := 1, one_mul := by frac_tac, mul_one := by frac_tac, nsmul := (β€’), nsmul_zero' := by smul_tac, nsmul_succ' := Ξ» _, by smul_tac, zsmul := (β€’), zsmul_zero' := by smul_tac, zsmul_succ' := Ξ» _, by smul_tac, zsmul_neg' := Ξ» _, by smul_tac, npow := npow_rec } variables {K} section lift_hom variables {Gβ‚€ L R S F : Type*} [comm_group_with_zero Gβ‚€] [field L] [comm_ring R] [comm_ring S] omit hring /-- Lift a monoid homomorphism that maps polynomials `Ο† : R[X] β†’* S[X]` to a `ratfunc R β†’* ratfunc S`, on the condition that `Ο†` maps non zero divisors to non zero divisors, by mapping both the numerator and denominator and quotienting them. -/ def map [monoid_hom_class F R[X] S[X]] (Ο† : F) (hΟ† : R[X]⁰ ≀ S[X]⁰.comap Ο†) : ratfunc R β†’* ratfunc S := { to_fun := Ξ» f, ratfunc.lift_on f (Ξ» n d, if h : Ο† d ∈ S[X]⁰ then of_fraction_ring (localization.mk (Ο† n) βŸ¨Ο† d, h⟩) else 0) $ Ξ» p q p' q' hq hq' h, begin rw [dif_pos, dif_pos, of_fraction_ring.inj_eq, localization.mk_eq_mk_iff], rotate, { exact hΟ† hq' }, { exact hΟ† hq }, refine localization.r_of_eq _, simpa only [map_mul] using (congr_arg Ο† h).symm, end, map_one' := begin rw [←of_fraction_ring_one, ←localization.mk_one, lift_on_of_fraction_ring_mk, dif_pos], { simpa using of_fraction_ring_one }, { simpa using submonoid.one_mem _} end, map_mul' := Ξ» x y, begin cases x, cases y, induction x with p q, induction y with p' q', { have hq : Ο† q ∈ S[X]⁰ := hΟ† q.prop, have hq' : Ο† q' ∈ S[X]⁰ := hΟ† q'.prop, have hqq' : Ο† ↑(q * q') ∈ S[X]⁰, { simpa using submonoid.mul_mem _ hq hq' }, simp_rw [←of_fraction_ring_mul, localization.mk_mul, lift_on_of_fraction_ring_mk, dif_pos hq, dif_pos hq', dif_pos hqq', ←of_fraction_ring_mul, submonoid.coe_mul, map_mul, localization.mk_mul, submonoid.mk_mul_mk] }, { refl }, { refl } end } lemma map_apply_of_fraction_ring_mk [monoid_hom_class F R[X] S[X]] (Ο† : F) (hΟ† : R[X]⁰ ≀ S[X]⁰.comap Ο†) (n : R[X]) (d : R[X]⁰) : map Ο† hΟ† (of_fraction_ring (localization.mk n d)) = of_fraction_ring (localization.mk (Ο† n) βŸ¨Ο† d, hΟ† d.prop⟩) := begin convert lift_on_of_fraction_ring_mk _ _ _ _, rw dif_pos end lemma map_injective [monoid_hom_class F R[X] S[X]] (Ο† : F) (hΟ† : R[X]⁰ ≀ S[X]⁰.comap Ο†) (hf : function.injective Ο†) : function.injective (map Ο† hΟ†) := begin rintro ⟨x⟩ ⟨y⟩ h, induction x, induction y, { simpa only [map_apply_of_fraction_ring_mk, of_fraction_ring_injective.eq_iff, localization.mk_eq_mk_iff, localization.r_iff_exists, mul_cancel_right_coe_non_zero_divisor, exists_const, set_like.coe_mk, ←map_mul, hf.eq_iff] using h }, { refl }, { refl } end /-- Lift a ring homomorphism that maps polynomials `Ο† : R[X] β†’+* S[X]` to a `ratfunc R β†’+* ratfunc S`, on the condition that `Ο†` maps non zero divisors to non zero divisors, by mapping both the numerator and denominator and quotienting them. -/ def map_ring_hom [ring_hom_class F R[X] S[X]] (Ο† : F) (hΟ† : R[X]⁰ ≀ S[X]⁰.comap Ο†) : ratfunc R β†’+* ratfunc S := { map_zero' := begin simp_rw [monoid_hom.to_fun_eq_coe, ←of_fraction_ring_zero, ←localization.mk_zero (1 : R[X]⁰), ←localization.mk_zero (1 : S[X]⁰), map_apply_of_fraction_ring_mk, map_zero, localization.mk_eq_mk', is_localization.mk'_zero], end, map_add' := begin rintro ⟨x⟩ ⟨y⟩, induction x, induction y, { simp only [←of_fraction_ring_add, localization.add_mk, map_add, set_like.coe_mk, map_mul, monoid_hom.to_fun_eq_coe, map_apply_of_fraction_ring_mk, submonoid.mk_mul_mk, submonoid.coe_mul] }, { refl }, { refl } end, ..map Ο† hΟ† } lemma coe_map_ring_hom_eq_coe_map [ring_hom_class F R[X] S[X]] (Ο† : F) (hΟ† : R[X]⁰ ≀ S[X]⁰.comap Ο†) : (map_ring_hom Ο† hΟ† : ratfunc R β†’ ratfunc S) = map Ο† hΟ† := rfl -- TODO: Generalize to `fun_like` classes, /-- Lift an monoid with zero homomorphism `polynomial R β†’*β‚€ Gβ‚€` to a `ratfunc R β†’*β‚€ Gβ‚€` on the condition that `Ο†` maps non zero divisors to non zero divisors, by mapping both the numerator and denominator and quotienting them. --/ def lift_monoid_with_zero_hom (Ο† : R[X] β†’*β‚€ Gβ‚€) (hΟ† : R[X]⁰ ≀ G₀⁰.comap Ο†) : ratfunc R β†’*β‚€ Gβ‚€ := { to_fun := Ξ» f, ratfunc.lift_on f (Ξ» p q, Ο† p / (Ο† q)) $ Ξ» p q p' q' hq hq' h, begin casesI subsingleton_or_nontrivial R, { rw [subsingleton.elim p q, subsingleton.elim p' q, subsingleton.elim q' q] }, rw [div_eq_div_iff, ←map_mul, h, map_mul]; exact non_zero_divisors.ne_zero (hΟ† β€Ή_β€Ί), end, map_one' := by { rw [←of_fraction_ring_one, ←localization.mk_one, lift_on_of_fraction_ring_mk], simp only [map_one, submonoid.coe_one, div_one] }, map_mul' := Ξ» x y, by { cases x, cases y, induction x with p q, induction y with p' q', { rw [←of_fraction_ring_mul, localization.mk_mul], simp only [lift_on_of_fraction_ring_mk, div_mul_div_comm, map_mul, submonoid.coe_mul] }, { refl }, { refl } }, map_zero' := by { rw [←of_fraction_ring_zero, ←localization.mk_zero (1 : R[X]⁰), lift_on_of_fraction_ring_mk], simp only [map_zero, zero_div] } } lemma lift_monoid_with_zero_hom_apply_of_fraction_ring_mk (Ο† : R[X] β†’*β‚€ Gβ‚€) (hΟ† : R[X]⁰ ≀ G₀⁰.comap Ο†) (n : R[X]) (d : R[X]⁰) : lift_monoid_with_zero_hom Ο† hΟ† (of_fraction_ring (localization.mk n d)) = Ο† n / Ο† d := lift_on_of_fraction_ring_mk _ _ _ _ lemma lift_monoid_with_zero_hom_injective [nontrivial R] (Ο† : R[X] β†’*β‚€ Gβ‚€) (hΟ† : function.injective Ο†) (hΟ†' : R[X]⁰ ≀ G₀⁰.comap Ο† := non_zero_divisors_le_comap_non_zero_divisors_of_injective _ hΟ†) : function.injective (lift_monoid_with_zero_hom Ο† hΟ†') := begin rintro ⟨x⟩ ⟨y⟩, induction x, induction y, { simp_rw [lift_monoid_with_zero_hom_apply_of_fraction_ring_mk, localization.mk_eq_mk_iff], intro h, refine localization.r_of_eq _, simpa only [←hΟ†.eq_iff, map_mul] using mul_eq_mul_of_div_eq_div _ _ _ _ h.symm; exact (map_ne_zero_of_mem_non_zero_divisors _ hΟ† (set_like.coe_mem _)) }, { exact Ξ» _, rfl }, { exact Ξ» _, rfl } end /-- Lift an injective ring homomorphism `polynomial R β†’+* L` to a `ratfunc R β†’+* L` by mapping both the numerator and denominator and quotienting them. --/ def lift_ring_hom (Ο† : R[X] β†’+* L) (hΟ† : R[X]⁰ ≀ L⁰.comap Ο†) : ratfunc R β†’+* L := { map_add' := Ξ» x y, by { simp only [monoid_with_zero_hom.to_fun_eq_coe], casesI subsingleton_or_nontrivial R, { rw [subsingleton.elim (x + y) y, subsingleton.elim x 0, map_zero, zero_add] }, cases x, cases y, induction x with p q, induction y with p' q', { rw [←of_fraction_ring_add, localization.add_mk], simp only [ring_hom.to_monoid_with_zero_hom_eq_coe, lift_monoid_with_zero_hom_apply_of_fraction_ring_mk], rw [div_add_div, div_eq_div_iff], { rw [mul_comm _ p, mul_comm _ p', mul_comm _ (Ο† p'), add_comm], simp only [map_add, map_mul, submonoid.coe_mul] }, all_goals { try { simp only [←map_mul, ←submonoid.coe_mul] }, exact non_zero_divisors.ne_zero (hΟ† (set_like.coe_mem _)) } }, { refl }, { refl } }, ..lift_monoid_with_zero_hom Ο†.to_monoid_with_zero_hom hΟ† } lemma lift_ring_hom_apply_of_fraction_ring_mk (Ο† : R[X] β†’+* L) (hΟ† : R[X]⁰ ≀ L⁰.comap Ο†) (n : R[X]) (d : R[X]⁰) : lift_ring_hom Ο† hΟ† (of_fraction_ring (localization.mk n d)) = Ο† n / Ο† d := lift_monoid_with_zero_hom_apply_of_fraction_ring_mk _ _ _ _ lemma lift_ring_hom_injective [nontrivial R] (Ο† : R[X] β†’+* L) (hΟ† : function.injective Ο†) (hΟ†' : R[X]⁰ ≀ L⁰.comap Ο† := non_zero_divisors_le_comap_non_zero_divisors_of_injective _ hΟ†) : function.injective (lift_ring_hom Ο† hΟ†') := lift_monoid_with_zero_hom_injective _ hΟ† end lift_hom variables (K) include hdomain instance : field (ratfunc K) := { inv := has_inv.inv, inv_zero := by frac_tac, div := (/), div_eq_mul_inv := by frac_tac, mul_inv_cancel := Ξ» _, mul_inv_cancel, zpow := zpow_rec, .. ratfunc.comm_ring K, .. ratfunc.nontrivial K } end field section is_fraction_ring /-! ### `ratfunc` as field of fractions of `polynomial` -/ include hdomain instance (R : Type*) [comm_semiring R] [algebra R K[X]] : algebra R (ratfunc K) := { to_fun := Ξ» x, ratfunc.mk (algebra_map _ _ x) 1, map_add' := Ξ» x y, by simp only [mk_one', ring_hom.map_add, of_fraction_ring_add], map_mul' := Ξ» x y, by simp only [mk_one', ring_hom.map_mul, of_fraction_ring_mul], map_one' := by simp only [mk_one', ring_hom.map_one, of_fraction_ring_one], map_zero' := by simp only [mk_one', ring_hom.map_zero, of_fraction_ring_zero], smul := (β€’), smul_def' := Ξ» c x, x.induction_on' $ Ξ» p q hq, by simp_rw [mk_one', ← mk_smul, mk_def_of_ne (c β€’ p) hq, mk_def_of_ne p hq, ← of_fraction_ring_mul, is_localization.mul_mk'_eq_mk'_of_mul, algebra.smul_def], commutes' := Ξ» c x, mul_comm _ _ } variables {K} lemma mk_one (x : K[X]) : ratfunc.mk x 1 = algebra_map _ _ x := rfl lemma of_fraction_ring_algebra_map (x : K[X]) : of_fraction_ring (algebra_map _ (fraction_ring K[X]) x) = algebra_map _ _ x := by rw [← mk_one, mk_one'] @[simp] lemma mk_eq_div (p q : K[X]) : ratfunc.mk p q = (algebra_map _ _ p / algebra_map _ _ q) := by simp only [mk_eq_div', of_fraction_ring_div, of_fraction_ring_algebra_map] @[simp] lemma div_smul {R} [monoid R] [distrib_mul_action R K[X]] [is_scalar_tower R K[X] K[X]] (c : R) (p q : K[X]) : algebra_map _ (ratfunc K) (c β€’ p) / (algebra_map _ _ q) = c β€’ (algebra_map _ _ p / algebra_map _ _ q) := by rw [←mk_eq_div, mk_smul, mk_eq_div] lemma algebra_map_apply {R : Type*} [comm_semiring R] [algebra R K[X]] (x : R) : algebra_map R (ratfunc K) x = (algebra_map _ _ (algebra_map R K[X] x)) / (algebra_map K[X] _ 1) := by { rw [←mk_eq_div], refl } lemma map_apply_div_ne_zero {R F : Type*} [comm_ring R] [is_domain R] [monoid_hom_class F K[X] R[X]] (Ο† : F) (hΟ† : K[X]⁰ ≀ R[X]⁰.comap Ο†) (p q : K[X]) (hq : q β‰  0) : map Ο† hΟ† (algebra_map _ _ p / algebra_map _ _ q) = algebra_map _ _ (Ο† p) / algebra_map _ _ (Ο† q) := begin have hq' : Ο† q β‰  0 := non_zero_divisors.ne_zero (hΟ† (mem_non_zero_divisors_iff_ne_zero.mpr hq)), simp only [←mk_eq_div, mk_eq_localization_mk _ hq, map_apply_of_fraction_ring_mk, mk_eq_localization_mk _ hq', set_like.coe_mk], end @[simp] lemma map_apply_div {R F : Type*} [comm_ring R] [is_domain R] [monoid_with_zero_hom_class F K[X] R[X]] (Ο† : F) (hΟ† : K[X]⁰ ≀ R[X]⁰.comap Ο†) (p q : K[X]) : map Ο† hΟ† (algebra_map _ _ p / algebra_map _ _ q) = algebra_map _ _ (Ο† p) / algebra_map _ _ (Ο† q) := begin rcases eq_or_ne q 0 with rfl|hq, { have : (0 : ratfunc K) = algebra_map K[X] _ 0 / algebra_map K[X] _ 1, { simp }, rw [map_zero, map_zero, map_zero, div_zero, div_zero, this, map_apply_div_ne_zero, map_one, map_one, div_one, map_zero, map_zero], exact one_ne_zero }, exact map_apply_div_ne_zero _ _ _ _ hq end @[simp] lemma lift_monoid_with_zero_hom_apply_div {L : Type*} [comm_group_with_zero L] (Ο† : monoid_with_zero_hom K[X] L) (hΟ† : K[X]⁰ ≀ L⁰.comap Ο†) (p q : K[X]) : lift_monoid_with_zero_hom Ο† hΟ† (algebra_map _ _ p / algebra_map _ _ q) = Ο† p / Ο† q := begin rcases eq_or_ne q 0 with rfl|hq, { simp only [div_zero, map_zero] }, simpa only [←mk_eq_div, mk_eq_localization_mk _ hq, lift_monoid_with_zero_hom_apply_of_fraction_ring_mk], end @[simp] lemma lift_ring_hom_apply_div {L : Type*} [field L] (Ο† : K[X] β†’+* L) (hΟ† : K[X]⁰ ≀ L⁰.comap Ο†) (p q : K[X]) : lift_ring_hom Ο† hΟ† (algebra_map _ _ p / algebra_map _ _ q) = Ο† p / Ο† q := lift_monoid_with_zero_hom_apply_div _ _ _ _ variables (K) lemma of_fraction_ring_comp_algebra_map : of_fraction_ring ∘ algebra_map K[X] (fraction_ring K[X]) = algebra_map _ _ := funext of_fraction_ring_algebra_map lemma algebra_map_injective : function.injective (algebra_map K[X] (ratfunc K)) := begin rw ← of_fraction_ring_comp_algebra_map, exact of_fraction_ring_injective.comp (is_fraction_ring.injective _ _), end @[simp] lemma algebra_map_eq_zero_iff {x : K[X]} : algebra_map K[X] (ratfunc K) x = 0 ↔ x = 0 := ⟨(injective_iff_map_eq_zero _).mp (algebra_map_injective K) _, Ξ» hx, by rw [hx, ring_hom.map_zero]⟩ variables {K} lemma algebra_map_ne_zero {x : K[X]} (hx : x β‰  0) : algebra_map K[X] (ratfunc K) x β‰  0 := mt (algebra_map_eq_zero_iff K).mp hx section lift_alg_hom variables {L R S : Type*} [field L] [comm_ring R] [is_domain R] [comm_semiring S] [algebra S K[X]] [algebra S L] [algebra S R[X]] (Ο† : K[X] →ₐ[S] L) (hΟ† : K[X]⁰ ≀ L⁰.comap Ο†) /-- Lift an algebra homomorphism that maps polynomials `Ο† : polynomial K →ₐ[S] R[X]` to a `ratfunc K →ₐ[S] ratfunc R`, on the condition that `Ο†` maps non zero divisors to non zero divisors, by mapping both the numerator and denominator and quotienting them. -/ def map_alg_hom (Ο† : K[X] →ₐ[S] R[X]) (hΟ† : K[X]⁰ ≀ R[X]⁰.comap Ο†) : ratfunc K →ₐ[S] ratfunc R := { commutes' := Ξ» r, by simp_rw [ring_hom.to_fun_eq_coe, coe_map_ring_hom_eq_coe_map, algebra_map_apply r, map_apply_div, map_one, alg_hom.commutes], ..map_ring_hom Ο† hΟ† } lemma coe_map_alg_hom_eq_coe_map (Ο† : K[X] →ₐ[S] R[X]) (hΟ† : K[X]⁰ ≀ R[X]⁰.comap Ο†) : (map_alg_hom Ο† hΟ† : ratfunc K β†’ ratfunc R) = map Ο† hΟ† := rfl /-- Lift an injective algebra homomorphism `polynomial K →ₐ[S] L` to a `ratfunc K →ₐ[S] L` by mapping both the numerator and denominator and quotienting them. --/ def lift_alg_hom : ratfunc K →ₐ[S] L := { commutes' := Ξ» r, by simp_rw [ring_hom.to_fun_eq_coe, alg_hom.to_ring_hom_eq_coe, algebra_map_apply r, lift_ring_hom_apply_div, alg_hom.coe_to_ring_hom, map_one, div_one, alg_hom.commutes], ..lift_ring_hom Ο†.to_ring_hom hΟ† } lemma lift_alg_hom_apply_of_fraction_ring_mk (n : K[X]) (d : K[X]⁰) : lift_alg_hom Ο† hΟ† (of_fraction_ring (localization.mk n d)) = Ο† n / Ο† d := lift_monoid_with_zero_hom_apply_of_fraction_ring_mk _ _ _ _ lemma lift_alg_hom_injective (Ο† : K[X] →ₐ[S] L) (hΟ† : function.injective Ο†) (hΟ†' : K[X]⁰ ≀ L⁰.comap Ο† := non_zero_divisors_le_comap_non_zero_divisors_of_injective _ hΟ†) : function.injective (lift_alg_hom Ο† hΟ†') := lift_monoid_with_zero_hom_injective _ hΟ† @[simp] lemma lift_alg_hom_apply_div (p q : K[X]) : lift_alg_hom Ο† hΟ† (algebra_map _ _ p / algebra_map _ _ q) = Ο† p / Ο† q := lift_monoid_with_zero_hom_apply_div _ _ _ _ end lift_alg_hom variables (K) omit hdomain include hdomain /-- `ratfunc K` is the field of fractions of the polynomials over `K`. -/ instance : is_fraction_ring K[X] (ratfunc K) := { map_units := Ξ» y, by rw ← of_fraction_ring_algebra_map; exact (to_fraction_ring_ring_equiv K).symm.to_ring_hom.is_unit_map (is_localization.map_units _ y), eq_iff_exists := Ξ» x y, by rw [← of_fraction_ring_algebra_map, ← of_fraction_ring_algebra_map]; exact (to_fraction_ring_ring_equiv K).symm.injective.eq_iff.trans (is_localization.eq_iff_exists _ _), surj := by { rintro ⟨z⟩, convert is_localization.surj K[X]⁰ z, ext ⟨x, y⟩, simp only [← of_fraction_ring_algebra_map, function.comp_app, ← of_fraction_ring_mul] } } variables {K} @[simp] lemma lift_on_div {P : Sort v} (p q : K[X]) (f : βˆ€ (p q : K[X]), P) (f0 : βˆ€ p, f p 0 = f 0 1) (H' : βˆ€ {p q p' q'} (hq : q β‰  0) (hq' : q' β‰  0), p * q' = p' * q β†’ f p q = f p' q') (H : βˆ€ {p q p' q'} (hq : q ∈ K[X]⁰) (hq' : q' ∈ K[X]⁰), p * q' = p' * q β†’ f p q = f p' q' := Ξ» p q p' q' hq hq' h, H' (non_zero_divisors.ne_zero hq) (non_zero_divisors.ne_zero hq') h) : (algebra_map _ (ratfunc K) p / algebra_map _ _ q).lift_on f @H = f p q := by rw [← mk_eq_div, lift_on_mk _ _ f f0 @H'] @[simp] lemma lift_on'_div {P : Sort v} (p q : K[X]) (f : βˆ€ (p q : K[X]), P) (f0 : βˆ€ p, f p 0 = f 0 1) (H) : (algebra_map _ (ratfunc K) p / algebra_map _ _ q).lift_on' f @H = f p q := begin rw [ratfunc.lift_on', lift_on_div _ _ _ f0], exact lift_on_condition_of_lift_on'_condition @H end /-- Induction principle for `ratfunc K`: if `f p q : P (p / q)` for all `p q : polynomial K`, then `P` holds on all elements of `ratfunc K`. See also `induction_on'`, which is a recursion principle defined in terms of `ratfunc.mk`. -/ protected lemma induction_on {P : ratfunc K β†’ Prop} (x : ratfunc K) (f : βˆ€ (p q : K[X]) (hq : q β‰  0), P (algebra_map _ (ratfunc K) p / algebra_map _ _ q)) : P x := x.induction_on' (Ξ» p q hq, by simpa using f p q hq) lemma of_fraction_ring_mk' (x : K[X]) (y : K[X]⁰) : of_fraction_ring (is_localization.mk' _ x y) = is_localization.mk' (ratfunc K) x y := by rw [is_fraction_ring.mk'_eq_div, is_fraction_ring.mk'_eq_div, ← mk_eq_div', ← mk_eq_div] @[simp] lemma of_fraction_ring_eq : (of_fraction_ring : fraction_ring K[X] β†’ ratfunc K) = is_localization.alg_equiv K[X]⁰ _ _ := funext $ Ξ» x, localization.induction_on x $ Ξ» x, by simp only [is_localization.alg_equiv_apply, is_localization.ring_equiv_of_ring_equiv_apply, ring_equiv.to_fun_eq_coe, localization.mk_eq_mk'_apply, is_localization.map_mk', of_fraction_ring_mk', ring_equiv.coe_to_ring_hom, ring_equiv.refl_apply, set_like.eta] @[simp] lemma to_fraction_ring_eq : (to_fraction_ring : ratfunc K β†’ fraction_ring K[X]) = is_localization.alg_equiv K[X]⁰ _ _ := funext $ Ξ» ⟨x⟩, localization.induction_on x $ Ξ» x, by simp only [localization.mk_eq_mk'_apply, of_fraction_ring_mk', is_localization.alg_equiv_apply, ring_equiv.to_fun_eq_coe, is_localization.ring_equiv_of_ring_equiv_apply, is_localization.map_mk', ring_equiv.coe_to_ring_hom, ring_equiv.refl_apply, set_like.eta] @[simp] lemma to_fraction_ring_ring_equiv_symm_eq : (to_fraction_ring_ring_equiv K).symm = (is_localization.alg_equiv K[X]⁰ _ _).to_ring_equiv := by { ext x, simp [to_fraction_ring_ring_equiv, of_fraction_ring_eq, alg_equiv.coe_ring_equiv'] } end is_fraction_ring section num_denom /-! ### Numerator and denominator -/ open gcd_monoid polynomial omit hring variables [hfield : field K] include hfield /-- `ratfunc.num_denom` are numerator and denominator of a rational function over a field, normalized such that the denominator is monic. -/ def num_denom (x : ratfunc K) : K[X] Γ— K[X] := x.lift_on' (Ξ» p q, if q = 0 then ⟨0, 1⟩ else let r := gcd p q in ⟨polynomial.C ((q / r).leading_coeff⁻¹) * (p / r), polynomial.C ((q / r).leading_coeff⁻¹) * (q / r)⟩) begin intros p q a hq ha, rw [if_neg hq, if_neg (mul_ne_zero ha hq)], have hpq : gcd p q β‰  0 := mt (and.right ∘ (gcd_eq_zero_iff _ _).mp) hq, have ha' : a.leading_coeff β‰  0 := polynomial.leading_coeff_ne_zero.mpr ha, have hainv : (a.leading_coeff)⁻¹ β‰  0 := inv_ne_zero ha', simp only [prod.ext_iff, gcd_mul_left, normalize_apply, polynomial.coe_norm_unit, mul_assoc, comm_group_with_zero.coe_norm_unit _ ha'], have hdeg : (gcd p q).degree ≀ q.degree := degree_gcd_le_right _ hq, have hdeg' : (polynomial.C (a.leading_coeff⁻¹) * gcd p q).degree ≀ q.degree, { rw [polynomial.degree_mul, polynomial.degree_C hainv, zero_add], exact hdeg }, have hdivp : (polynomial.C a.leading_coeff⁻¹) * gcd p q ∣ p := (C_mul_dvd hainv).mpr (gcd_dvd_left p q), have hdivq : (polynomial.C a.leading_coeff⁻¹) * gcd p q ∣ q := (C_mul_dvd hainv).mpr (gcd_dvd_right p q), rw [euclidean_domain.mul_div_mul_cancel ha hdivp, euclidean_domain.mul_div_mul_cancel ha hdivq, leading_coeff_div hdeg, leading_coeff_div hdeg', polynomial.leading_coeff_mul, polynomial.leading_coeff_C, div_C_mul, div_C_mul, ← mul_assoc, ← polynomial.C_mul, ← mul_assoc, ← polynomial.C_mul], split; congr; rw [inv_div, mul_comm, mul_div_assoc, ← mul_assoc, inv_inv, _root_.mul_inv_cancel ha', one_mul, inv_div], end @[simp] lemma num_denom_div (p : K[X]) {q : K[X]} (hq : q β‰  0) : num_denom (algebra_map _ _ p / algebra_map _ _ q) = (polynomial.C ((q / gcd p q).leading_coeff⁻¹) * (p / gcd p q), polynomial.C ((q / gcd p q).leading_coeff⁻¹) * (q / gcd p q)) := begin rw [num_denom, lift_on'_div, if_neg hq], intros p, rw [if_pos rfl, if_neg (@one_ne_zero K[X] _ _)], simp, end /-- `ratfunc.num` is the numerator of a rational function, normalized such that the denominator is monic. -/ def num (x : ratfunc K) : K[X] := x.num_denom.1 private lemma num_div' (p : K[X]) {q : K[X]} (hq : q β‰  0) : num (algebra_map _ _ p / algebra_map _ _ q) = polynomial.C ((q / gcd p q).leading_coeff⁻¹) * (p / gcd p q) := by rw [num, num_denom_div _ hq] @[simp] lemma num_zero : num (0 : ratfunc K) = 0 := by { convert num_div' (0 : K[X]) one_ne_zero; simp } @[simp] lemma num_div (p q : K[X]) : num (algebra_map _ _ p / algebra_map _ _ q) = polynomial.C ((q / gcd p q).leading_coeff⁻¹) * (p / gcd p q) := begin by_cases hq : q = 0, { simp [hq] }, { exact num_div' p hq, }, end @[simp] lemma num_one : num (1 : ratfunc K) = 1 := by { convert num_div (1 : K[X]) 1; simp } @[simp] lemma num_algebra_map (p : K[X]) : num (algebra_map _ _ p) = p := by { convert num_div p 1; simp } lemma num_div_dvd (p : K[X]) {q : K[X]} (hq : q β‰  0) : num (algebra_map _ _ p / algebra_map _ _ q) ∣ p := begin rw [num_div _ q, C_mul_dvd], { exact euclidean_domain.div_dvd_of_dvd (gcd_dvd_left p q) }, { simpa only [ne.def, inv_eq_zero, polynomial.leading_coeff_eq_zero] using right_div_gcd_ne_zero hq }, end /-- A version of `num_div_dvd` with the LHS in simp normal form -/ @[simp] lemma num_div_dvd' (p : K[X]) {q : K[X]} (hq : q β‰  0) : C ((q / gcd p q).leading_coeff)⁻¹ * (p / gcd p q) ∣ p := by simpa using num_div_dvd p hq /-- `ratfunc.denom` is the denominator of a rational function, normalized such that it is monic. -/ def denom (x : ratfunc K) : K[X] := x.num_denom.2 @[simp] lemma denom_div (p : K[X]) {q : K[X]} (hq : q β‰  0) : denom (algebra_map _ _ p / algebra_map _ _ q) = polynomial.C ((q / gcd p q).leading_coeff⁻¹) * (q / gcd p q) := by rw [denom, num_denom_div _ hq] lemma monic_denom (x : ratfunc K) : (denom x).monic := x.induction_on (Ξ» p q hq, begin rw [denom_div p hq, mul_comm], exact polynomial.monic_mul_leading_coeff_inv (right_div_gcd_ne_zero hq) end) lemma denom_ne_zero (x : ratfunc K) : denom x β‰  0 := (monic_denom x).ne_zero @[simp] lemma denom_zero : denom (0 : ratfunc K) = 1 := by { convert denom_div (0 : K[X]) one_ne_zero; simp } @[simp] lemma denom_one : denom (1 : ratfunc K) = 1 := by { convert denom_div (1 : K[X]) one_ne_zero; simp } @[simp] lemma denom_algebra_map (p : K[X]) : denom (algebra_map _ (ratfunc K) p) = 1 := by { convert denom_div p one_ne_zero; simp } @[simp] lemma denom_div_dvd (p q : K[X]) : denom (algebra_map _ _ p / algebra_map _ _ q) ∣ q := begin by_cases hq : q = 0, { simp [hq], }, rw [denom_div _ hq, C_mul_dvd], { exact euclidean_domain.div_dvd_of_dvd (gcd_dvd_right p q) }, { simpa only [ne.def, inv_eq_zero, polynomial.leading_coeff_eq_zero] using right_div_gcd_ne_zero hq }, end @[simp] lemma num_div_denom (x : ratfunc K) : algebra_map _ _ (num x) / algebra_map _ _ (denom x) = x := x.induction_on (Ξ» p q hq, begin have q_div_ne_zero := right_div_gcd_ne_zero hq, rw [num_div p q, denom_div p hq, ring_hom.map_mul, ring_hom.map_mul, mul_div_mul_left, div_eq_div_iff, ← ring_hom.map_mul, ← ring_hom.map_mul, mul_comm _ q, ← euclidean_domain.mul_div_assoc, ← euclidean_domain.mul_div_assoc, mul_comm], { apply gcd_dvd_right }, { apply gcd_dvd_left }, { exact algebra_map_ne_zero q_div_ne_zero }, { exact algebra_map_ne_zero hq }, { refine algebra_map_ne_zero (mt polynomial.C_eq_zero.mp _), exact inv_ne_zero (polynomial.leading_coeff_ne_zero.mpr q_div_ne_zero) }, end) @[simp] lemma num_eq_zero_iff {x : ratfunc K} : num x = 0 ↔ x = 0 := ⟨λ h, by rw [← num_div_denom x, h, ring_hom.map_zero, zero_div], Ξ» h, h.symm β–Έ num_zero⟩ lemma num_ne_zero {x : ratfunc K} (hx : x β‰  0) : num x β‰  0 := mt num_eq_zero_iff.mp hx lemma num_mul_eq_mul_denom_iff {x : ratfunc K} {p q : K[X]} (hq : q β‰  0) : x.num * q = p * x.denom ↔ x = algebra_map _ _ p / algebra_map _ _ q := begin rw [← (algebra_map_injective K).eq_iff, eq_div_iff (algebra_map_ne_zero hq)], conv_rhs { rw ← num_div_denom x }, rw [ring_hom.map_mul, ring_hom.map_mul, div_eq_mul_inv, mul_assoc, mul_comm (has_inv.inv _), ← mul_assoc, ← div_eq_mul_inv, div_eq_iff], exact algebra_map_ne_zero (denom_ne_zero x) end lemma num_denom_add (x y : ratfunc K) : (x + y).num * (x.denom * y.denom) = (x.num * y.denom + x.denom * y.num) * (x + y).denom := (num_mul_eq_mul_denom_iff (mul_ne_zero (denom_ne_zero x) (denom_ne_zero y))).mpr $ begin conv_lhs { rw [← num_div_denom x, ← num_div_denom y] }, rw [div_add_div, ring_hom.map_mul, ring_hom.map_add, ring_hom.map_mul, ring_hom.map_mul], { exact algebra_map_ne_zero (denom_ne_zero x) }, { exact algebra_map_ne_zero (denom_ne_zero y) } end lemma num_denom_neg (x : ratfunc K) : (-x).num * x.denom = - x.num * (-x).denom := by rw [num_mul_eq_mul_denom_iff (denom_ne_zero x), _root_.map_neg, neg_div, num_div_denom] lemma num_denom_mul (x y : ratfunc K) : (x * y).num * (x.denom * y.denom) = x.num * y.num * (x * y).denom := (num_mul_eq_mul_denom_iff (mul_ne_zero (denom_ne_zero x) (denom_ne_zero y))).mpr $ by conv_lhs { rw [← num_div_denom x, ← num_div_denom y, div_mul_div_comm, ← ring_hom.map_mul, ← ring_hom.map_mul] } lemma num_dvd {x : ratfunc K} {p : K[X]} (hp : p β‰  0) : num x ∣ p ↔ βˆƒ (q : K[X]) (hq : q β‰  0), x = algebra_map _ _ p / algebra_map _ _ q := begin split, { rintro ⟨q, rfl⟩, obtain ⟨hx, hq⟩ := mul_ne_zero_iff.mp hp, use denom x * q, rw [ring_hom.map_mul, ring_hom.map_mul, ← div_mul_div_comm, div_self, mul_one, num_div_denom], { exact ⟨mul_ne_zero (denom_ne_zero x) hq, rfl⟩ }, { exact algebra_map_ne_zero hq } }, { rintro ⟨q, hq, rfl⟩, exact num_div_dvd p hq }, end lemma denom_dvd {x : ratfunc K} {q : K[X]} (hq : q β‰  0) : denom x ∣ q ↔ βˆƒ (p : K[X]), x = algebra_map _ _ p / algebra_map _ _ q := begin split, { rintro ⟨p, rfl⟩, obtain ⟨hx, hp⟩ := mul_ne_zero_iff.mp hq, use num x * p, rw [ring_hom.map_mul, ring_hom.map_mul, ← div_mul_div_comm, div_self, mul_one, num_div_denom], { exact algebra_map_ne_zero hp } }, { rintro ⟨p, rfl⟩, exact denom_div_dvd p q }, end lemma num_mul_dvd (x y : ratfunc K) : num (x * y) ∣ num x * num y := begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, rw num_dvd (mul_ne_zero (num_ne_zero hx) (num_ne_zero hy)), refine ⟨x.denom * y.denom, mul_ne_zero (denom_ne_zero x) (denom_ne_zero y), _⟩, rw [ring_hom.map_mul, ring_hom.map_mul, ← div_mul_div_comm, num_div_denom, num_div_denom] end lemma denom_mul_dvd (x y : ratfunc K) : denom (x * y) ∣ denom x * denom y := begin rw denom_dvd (mul_ne_zero (denom_ne_zero x) (denom_ne_zero y)), refine ⟨x.num * y.num, _⟩, rw [ring_hom.map_mul, ring_hom.map_mul, ← div_mul_div_comm, num_div_denom, num_div_denom] end lemma denom_add_dvd (x y : ratfunc K) : denom (x + y) ∣ denom x * denom y := begin rw denom_dvd (mul_ne_zero (denom_ne_zero x) (denom_ne_zero y)), refine ⟨x.num * y.denom + x.denom * y.num, _⟩, rw [ring_hom.map_mul, ring_hom.map_add, ring_hom.map_mul, ring_hom.map_mul, ← div_add_div, num_div_denom, num_div_denom], { exact algebra_map_ne_zero (denom_ne_zero x) }, { exact algebra_map_ne_zero (denom_ne_zero y) }, end lemma map_denom_ne_zero {L F : Type*} [has_zero L] [zero_hom_class F K[X] L] (Ο† : F) (hΟ† : function.injective Ο†) (f : ratfunc K) : Ο† f.denom β‰  0 := Ξ» H, (denom_ne_zero f) ((map_eq_zero_iff Ο† hΟ†).mp H) lemma map_apply {R F : Type*} [comm_ring R] [is_domain R] [monoid_hom_class F K[X] R[X]] (Ο† : F) (hΟ† : K[X]⁰ ≀ R[X]⁰.comap Ο†) (f : ratfunc K) : map Ο† hΟ† f = algebra_map _ _ (Ο† f.num) / algebra_map _ _ (Ο† f.denom) := begin rw [←num_div_denom f, map_apply_div_ne_zero, num_div_denom f], exact denom_ne_zero _ end lemma lift_monoid_with_zero_hom_apply {L : Type*} [comm_group_with_zero L] (Ο† : K[X] β†’*β‚€ L) (hΟ† : K[X]⁰ ≀ L⁰.comap Ο†) (f : ratfunc K) : lift_monoid_with_zero_hom Ο† hΟ† f = Ο† f.num / Ο† f.denom := by rw [←num_div_denom f, lift_monoid_with_zero_hom_apply_div, num_div_denom] lemma lift_ring_hom_apply {L : Type*} [field L] (Ο† : K[X] β†’+* L) (hΟ† : K[X]⁰ ≀ L⁰.comap Ο†) (f : ratfunc K) : lift_ring_hom Ο† hΟ† f = Ο† f.num / Ο† f.denom := lift_monoid_with_zero_hom_apply _ _ _ lemma lift_alg_hom_apply {L S : Type*} [field L] [comm_semiring S] [algebra S K[X]] [algebra S L] (Ο† : K[X] →ₐ[S] L) (hΟ† : K[X]⁰ ≀ L⁰.comap Ο†) (f : ratfunc K) : lift_alg_hom Ο† hΟ† f = Ο† f.num / Ο† f.denom := lift_monoid_with_zero_hom_apply _ _ _ lemma num_mul_denom_add_denom_mul_num_ne_zero {x y : ratfunc K} (hxy : x + y β‰  0) : x.num * y.denom + x.denom * y.num β‰  0 := begin intro h_zero, have h := num_denom_add x y, rw [h_zero, zero_mul] at h, exact (mul_ne_zero (num_ne_zero hxy) (mul_ne_zero x.denom_ne_zero y.denom_ne_zero)) h end end num_denom section eval /-! ### Polynomial structure: `C`, `X`, `eval` -/ include hdomain /-- `ratfunc.C a` is the constant rational function `a`. -/ def C : K β†’+* ratfunc K := algebra_map _ _ @[simp] lemma algebra_map_eq_C : algebra_map K (ratfunc K) = C := rfl @[simp] lemma algebra_map_C (a : K) : algebra_map K[X] (ratfunc K) (polynomial.C a) = C a := rfl @[simp] lemma algebra_map_comp_C : (algebra_map K[X] (ratfunc K)).comp polynomial.C = C := rfl lemma smul_eq_C_mul (r : K) (x : ratfunc K) : r β€’ x = C r * x := by rw [algebra.smul_def, algebra_map_eq_C] /-- `ratfunc.X` is the polynomial variable (aka indeterminate). -/ def X : ratfunc K := algebra_map K[X] (ratfunc K) polynomial.X @[simp] lemma algebra_map_X : algebra_map K[X] (ratfunc K) polynomial.X = X := rfl omit hring hdomain variables [hfield : field K] include hfield @[simp] lemma num_C (c : K) : num (C c) = polynomial.C c := num_algebra_map _ @[simp] lemma denom_C (c : K) : denom (C c) = 1 := denom_algebra_map _ @[simp] lemma num_X : num (X : ratfunc K) = polynomial.X := num_algebra_map _ @[simp] lemma denom_X : denom (X : ratfunc K) = 1 := denom_algebra_map _ lemma X_ne_zero : (ratfunc.X : ratfunc K) β‰  0 := ratfunc.algebra_map_ne_zero polynomial.X_ne_zero variables {L : Type*} [field L] /-- Evaluate a rational function `p` given a ring hom `f` from the scalar field to the target and a value `x` for the variable in the target. Fractions are reduced by clearing common denominators before evaluating: `eval id 1 ((X^2 - 1) / (X - 1)) = eval id 1 (X + 1) = 2`, not `0 / 0 = 0`. -/ def eval (f : K β†’+* L) (a : L) (p : ratfunc K) : L := (num p).evalβ‚‚ f a / (denom p).evalβ‚‚ f a variables {f : K β†’+* L} {a : L} lemma eval_eq_zero_of_evalβ‚‚_denom_eq_zero {x : ratfunc K} (h : polynomial.evalβ‚‚ f a (denom x) = 0) : eval f a x = 0 := by rw [eval, h, div_zero] lemma evalβ‚‚_denom_ne_zero {x : ratfunc K} (h : eval f a x β‰  0) : polynomial.evalβ‚‚ f a (denom x) β‰  0 := mt eval_eq_zero_of_evalβ‚‚_denom_eq_zero h variables (f a) @[simp] lemma eval_C {c : K} : eval f a (C c) = f c := by simp [eval] @[simp] lemma eval_X : eval f a X = a := by simp [eval] @[simp] lemma eval_zero : eval f a 0 = 0 := by simp [eval] @[simp] lemma eval_one : eval f a 1 = 1 := by simp [eval] @[simp] lemma eval_algebra_map {S : Type*} [comm_semiring S] [algebra S K[X]] (p : S) : eval f a (algebra_map _ _ p) = (algebra_map _ K[X] p).evalβ‚‚ f a := by simp [eval, is_scalar_tower.algebra_map_apply S K[X] (ratfunc K)] /-- `eval` is an additive homomorphism except when a denominator evaluates to `0`. Counterexample: `eval _ 1 (X / (X-1)) + eval _ 1 (-1 / (X-1)) = 0` `... β‰  1 = eval _ 1 ((X-1) / (X-1))`. See also `ratfunc.evalβ‚‚_denom_ne_zero` to make the hypotheses simpler but less general. -/ lemma eval_add {x y : ratfunc K} (hx : polynomial.evalβ‚‚ f a (denom x) β‰  0) (hy : polynomial.evalβ‚‚ f a (denom y) β‰  0) : eval f a (x + y) = eval f a x + eval f a y := begin unfold eval, by_cases hxy : polynomial.evalβ‚‚ f a (denom (x + y)) = 0, { have := polynomial.evalβ‚‚_eq_zero_of_dvd_of_evalβ‚‚_eq_zero f a (denom_add_dvd x y) hxy, rw polynomial.evalβ‚‚_mul at this, cases mul_eq_zero.mp this; contradiction }, rw [div_add_div _ _ hx hy, eq_div_iff (mul_ne_zero hx hy), div_eq_mul_inv, mul_right_comm, ← div_eq_mul_inv, div_eq_iff hxy], simp only [← polynomial.evalβ‚‚_mul, ← polynomial.evalβ‚‚_add], congr' 1, apply num_denom_add end /-- `eval` is a multiplicative homomorphism except when a denominator evaluates to `0`. Counterexample: `eval _ 0 X * eval _ 0 (1/X) = 0 β‰  1 = eval _ 0 1 = eval _ 0 (X * 1/X)`. See also `ratfunc.evalβ‚‚_denom_ne_zero` to make the hypotheses simpler but less general. -/ lemma eval_mul {x y : ratfunc K} (hx : polynomial.evalβ‚‚ f a (denom x) β‰  0) (hy : polynomial.evalβ‚‚ f a (denom y) β‰  0) : eval f a (x * y) = eval f a x * eval f a y := begin unfold eval, by_cases hxy : polynomial.evalβ‚‚ f a (denom (x * y)) = 0, { have := polynomial.evalβ‚‚_eq_zero_of_dvd_of_evalβ‚‚_eq_zero f a (denom_mul_dvd x y) hxy, rw polynomial.evalβ‚‚_mul at this, cases mul_eq_zero.mp this; contradiction }, rw [div_mul_div_comm, eq_div_iff (mul_ne_zero hx hy), div_eq_mul_inv, mul_right_comm, ← div_eq_mul_inv, div_eq_iff hxy], repeat { rw ← polynomial.evalβ‚‚_mul }, congr' 1, apply num_denom_mul, end end eval section int_degree open polynomial omit hring variables [field K] /-- `int_degree x` is the degree of the rational function `x`, defined as the difference between the `nat_degree` of its numerator and the `nat_degree` of its denominator. In particular, `int_degree 0 = 0`. -/ def int_degree (x : ratfunc K) : β„€ := nat_degree x.num - nat_degree x.denom @[simp] lemma int_degree_zero : int_degree (0 : ratfunc K) = 0 := by rw [int_degree, num_zero, nat_degree_zero, denom_zero, nat_degree_one, sub_self] @[simp] lemma int_degree_one : int_degree (1 : ratfunc K) = 0 := by rw [int_degree, num_one, denom_one, sub_self] @[simp] lemma int_degree_C (k : K): int_degree (ratfunc.C k) = 0 := by rw [int_degree, num_C, nat_degree_C, denom_C, nat_degree_one, sub_self] @[simp] lemma int_degree_X : int_degree (X : ratfunc K) = 1 := by rw [int_degree, ratfunc.num_X, polynomial.nat_degree_X, ratfunc.denom_X, polynomial.nat_degree_one, int.coe_nat_one, int.coe_nat_zero, sub_zero] @[simp] lemma int_degree_polynomial {p : polynomial K} : int_degree (algebra_map (polynomial K) (ratfunc K) p) = nat_degree p := by rw [int_degree, ratfunc.num_algebra_map, ratfunc.denom_algebra_map, polynomial.nat_degree_one, int.coe_nat_zero, sub_zero] lemma int_degree_mul {x y : ratfunc K} (hx : x β‰  0) (hy : y β‰  0) : int_degree (x * y) = int_degree x + int_degree y := begin simp only [int_degree, add_sub, sub_add, sub_sub_eq_add_sub, sub_sub, sub_eq_sub_iff_add_eq_add], norm_cast, rw [← polynomial.nat_degree_mul x.denom_ne_zero y.denom_ne_zero, ← polynomial.nat_degree_mul (ratfunc.num_ne_zero (mul_ne_zero hx hy)) (mul_ne_zero x.denom_ne_zero y.denom_ne_zero), ← polynomial.nat_degree_mul (ratfunc.num_ne_zero hx) (ratfunc.num_ne_zero hy), ← polynomial.nat_degree_mul (mul_ne_zero (ratfunc.num_ne_zero hx) (ratfunc.num_ne_zero hy)) (x * y).denom_ne_zero, ratfunc.num_denom_mul] end @[simp] lemma int_degree_neg (x : ratfunc K) : int_degree (-x) = int_degree x := begin by_cases hx : x = 0, { rw [hx, neg_zero] }, { rw [int_degree, int_degree, ← nat_degree_neg x.num], exact nat_degree_sub_eq_of_prod_eq (num_ne_zero (neg_ne_zero.mpr hx)) (denom_ne_zero (- x)) (neg_ne_zero.mpr (num_ne_zero hx)) (denom_ne_zero x) (num_denom_neg x) } end lemma int_degree_add {x y : ratfunc K} (hxy : x + y β‰  0) : (x + y).int_degree = (x.num * y.denom + x.denom * y.num).nat_degree - (x.denom * y.denom).nat_degree := nat_degree_sub_eq_of_prod_eq (num_ne_zero hxy) ((x + y).denom_ne_zero) (num_mul_denom_add_denom_mul_num_ne_zero hxy) (mul_ne_zero x.denom_ne_zero y.denom_ne_zero) (num_denom_add x y) lemma nat_degree_num_mul_right_sub_nat_degree_denom_mul_left_eq_int_degree {x : ratfunc K} (hx : x β‰  0) {s : polynomial K} (hs : s β‰  0) : ((x.num * s).nat_degree : β„€) - (s * x.denom).nat_degree = x.int_degree := begin apply nat_degree_sub_eq_of_prod_eq (mul_ne_zero (num_ne_zero hx) hs) (mul_ne_zero hs x.denom_ne_zero) (num_ne_zero hx) x.denom_ne_zero, rw mul_assoc end lemma int_degree_add_le {x y : ratfunc K} (hy : y β‰  0) (hxy : x + y β‰  0) : int_degree (x + y) ≀ max (int_degree x) (int_degree y) := begin by_cases hx : x = 0, { simp [hx] at *, }, rw [int_degree_add hxy, ← nat_degree_num_mul_right_sub_nat_degree_denom_mul_left_eq_int_degree hx y.denom_ne_zero, mul_comm y.denom, ← nat_degree_num_mul_right_sub_nat_degree_denom_mul_left_eq_int_degree hy x.denom_ne_zero, le_max_iff,sub_le_sub_iff_right, int.coe_nat_le, sub_le_sub_iff_right, int.coe_nat_le, ← le_max_iff, mul_comm y.num], exact nat_degree_add_le _ _, end end int_degree section laurent_series open power_series laurent_series hahn_series omit hring variables {F : Type u} [field F] (p q : F[X]) (f g : ratfunc F) /-- The coercion `ratfunc F β†’ laurent_series F` as bundled alg hom. -/ def coe_alg_hom (F : Type u) [field F] : ratfunc F →ₐ[polynomial F] laurent_series F := lift_alg_hom (algebra.of_id _ _) $ non_zero_divisors_le_comap_non_zero_divisors_of_injective _ $ polynomial.algebra_map_hahn_series_injective _ instance coe_to_laurent_series : has_coe (ratfunc F) (laurent_series F) := ⟨coe_alg_hom F⟩ lemma coe_def : (f : laurent_series F) = coe_alg_hom F f := rfl lemma coe_num_denom : (f : laurent_series F) = f.num / f.denom := lift_alg_hom_apply _ _ f lemma coe_injective : function.injective (coe : ratfunc F β†’ laurent_series F) := lift_alg_hom_injective _ (polynomial.algebra_map_hahn_series_injective _) @[simp, norm_cast] lemma coe_apply : coe_alg_hom F f = f := rfl @[simp, norm_cast] lemma coe_zero : ((0 : ratfunc F) : laurent_series F) = 0 := (coe_alg_hom F).map_zero @[simp, norm_cast] lemma coe_one : ((1 : ratfunc F) : laurent_series F) = 1 := (coe_alg_hom F).map_one @[simp, norm_cast] lemma coe_add : ((f + g : ratfunc F) : laurent_series F) = f + g := (coe_alg_hom F).map_add _ _ @[simp, norm_cast] lemma coe_mul : ((f * g : ratfunc F) : laurent_series F) = f * g := (coe_alg_hom F).map_mul _ _ @[simp, norm_cast] lemma coe_div : ((f / g : ratfunc F) : laurent_series F) = (f : laurent_series F) / (g : laurent_series F) := map_divβ‚€ (coe_alg_hom F) _ _ @[simp, norm_cast] lemma coe_C (r : F) : ((C r : ratfunc F) : laurent_series F) = hahn_series.C r := by rw [coe_num_denom, num_C, denom_C, coe_coe, polynomial.coe_C, coe_C, coe_coe, polynomial.coe_one, power_series.coe_one, div_one] -- TODO: generalize over other modules @[simp, norm_cast] lemma coe_smul (r : F) : ((r β€’ f : ratfunc F) : laurent_series F) = r β€’ f := by rw [smul_eq_C_mul, ←C_mul_eq_smul, coe_mul, coe_C] @[simp, norm_cast] lemma coe_X : ((X : ratfunc F) : laurent_series F) = single 1 1 := by rw [coe_num_denom, num_X, denom_X, coe_coe, polynomial.coe_X, coe_X, coe_coe, polynomial.coe_one, power_series.coe_one, div_one] instance : algebra (ratfunc F) (laurent_series F) := ring_hom.to_algebra (coe_alg_hom F).to_ring_hom lemma algebra_map_apply_div : algebra_map (ratfunc F) (laurent_series F) (algebra_map _ _ p / algebra_map _ _ q) = algebra_map F[X] (laurent_series F) p / algebra_map _ _ q := begin convert coe_div _ _; rw [←mk_one, coe_def, coe_alg_hom, mk_eq_div, lift_alg_hom_apply_div, map_one, div_one, algebra.of_id_apply] end instance : is_scalar_tower F[X] (ratfunc F) (laurent_series F) := ⟨λ x y z, by { ext, simp }⟩ end laurent_series end ratfunc
199a2f24baabf934647cc8ea3e9d3cffff7661ea
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/data/string/thms.lean
30e045b35af365f2f5b920fee4d6adb16c6996f7
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
475
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 data.string.decl data.bool open bool inhabited namespace char protected definition is_inhabited [instance] : inhabited char := inhabited.mk (mk ff ff ff ff ff ff ff ff) end char namespace string protected definition is_inhabited [instance] : inhabited string := inhabited.mk empty end string
9c37e8b6efc9ae5f069af0b9c4fbbc4b156294bb
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_2397.lean
ee7063bfd8ec21436a18ef9be2d171db1ab7104e
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
364
lean
import data.real.basic def converges_to (s : β„• β†’ ℝ) (a : ℝ) := βˆ€ Ξ΅ > 0, βˆƒ N, βˆ€ n β‰₯ N, abs (s n - a) < Ξ΅ variables {s : β„• β†’ ℝ} {a : ℝ} -- BEGIN theorem exists_abs_le_of_converges_to (cs : converges_to s a) : βˆƒ N b, βˆ€ n, N ≀ n β†’ abs (s n) < b := begin cases cs 1 zero_lt_one with N h, use [N, abs a + 1], sorry end -- END
1eaa781ad47e1d52978a2d24bc80a645e29ab061
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/finmap.lean
b7b411e2cb3eccf98e515528d601edd095b126d6
[ "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
18,201
lean
/- Copyright (c) 2018 Sean Leather. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sean Leather, Mario Carneiro Finite maps over `multiset`. -/ import data.list.alist data.finset data.pfun universes u v w open list variables {Ξ± : Type u} {Ξ² : Ξ± β†’ Type v} namespace multiset /-- Multiset of keys of an association multiset. -/ def keys (s : multiset (sigma Ξ²)) : multiset Ξ± := s.map sigma.fst @[simp] theorem coe_keys {l : list (sigma Ξ²)} : keys (l : multiset (sigma Ξ²)) = (l.keys : multiset Ξ±) := rfl /-- `nodupkeys s` means that `s` has no duplicate keys. -/ def nodupkeys (s : multiset (sigma Ξ²)) : Prop := quot.lift_on s list.nodupkeys (Ξ» s t p, propext $ perm_nodupkeys p) @[simp] theorem coe_nodupkeys {l : list (sigma Ξ²)} : @nodupkeys Ξ± Ξ² l ↔ l.nodupkeys := iff.rfl end multiset /-- `finmap Ξ²` is the type of finite maps over a multiset. It is effectively a quotient of `alist Ξ²` by permutation of the underlying list. -/ structure finmap (Ξ² : Ξ± β†’ Type v) : Type (max u v) := (entries : multiset (sigma Ξ²)) (nodupkeys : entries.nodupkeys) /-- The quotient map from `alist` to `finmap`. -/ def alist.to_finmap (s : alist Ξ²) : finmap Ξ² := ⟨s.entries, s.nodupkeys⟩ local notation `⟦`:max a `⟧`:0 := alist.to_finmap a theorem alist.to_finmap_eq {s₁ sβ‚‚ : alist Ξ²} : ⟦sβ‚βŸ§ = ⟦sβ‚‚βŸ§ ↔ s₁.entries ~ sβ‚‚.entries := by cases s₁; cases sβ‚‚; simp [alist.to_finmap] @[simp] theorem alist.to_finmap_entries (s : alist Ξ²) : ⟦s⟧.entries = s.entries := rfl def list.to_finmap [decidable_eq Ξ±] (s : list (sigma Ξ²)) : finmap Ξ² := alist.to_finmap (list.to_alist s) namespace finmap open alist /-- Lift a permutation-respecting function on `alist` to `finmap`. -/ @[elab_as_eliminator] def lift_on {Ξ³} (s : finmap Ξ²) (f : alist Ξ² β†’ Ξ³) (H : βˆ€ a b : alist Ξ², a.entries ~ b.entries β†’ f a = f b) : Ξ³ := begin refine (quotient.lift_on s.1 (Ξ» l, (⟨_, Ξ» nd, f ⟨l, nd⟩⟩ : roption Ξ³)) (Ξ» l₁ lβ‚‚ p, roption.ext' (perm_nodupkeys p) _) : roption Ξ³).get _, { exact Ξ» h₁ hβ‚‚, H _ _ (by exact p) }, { have := s.nodupkeys, rcases s.entries with ⟨l⟩, exact id } end @[simp] theorem lift_on_to_finmap {Ξ³} (s : alist Ξ²) (f : alist Ξ² β†’ Ξ³) (H) : lift_on ⟦s⟧ f H = f s := by cases s; refl /-- Lift a permutation-respecting function on 2 `alist`s to 2 `finmap`s. -/ @[elab_as_eliminator] def lift_onβ‚‚ {Ξ³} (s₁ sβ‚‚ : finmap Ξ²) (f : alist Ξ² β†’ alist Ξ² β†’ Ξ³) (H : βˆ€ a₁ b₁ aβ‚‚ bβ‚‚ : alist Ξ², a₁.entries ~ aβ‚‚.entries β†’ b₁.entries ~ bβ‚‚.entries β†’ f a₁ b₁ = f aβ‚‚ bβ‚‚) : Ξ³ := lift_on s₁ (Ξ» l₁, lift_on sβ‚‚ (f l₁) (Ξ» b₁ bβ‚‚ p, H _ _ _ _ (perm.refl _) p)) (Ξ» a₁ aβ‚‚ p, have H' : f a₁ = f aβ‚‚ := funext (Ξ» _, H _ _ _ _ p (perm.refl _)), by simp only [H']) @[simp] theorem lift_onβ‚‚_to_finmap {Ξ³} (s₁ sβ‚‚ : alist Ξ²) (f : alist Ξ² β†’ alist Ξ² β†’ Ξ³) (H) : lift_onβ‚‚ ⟦sβ‚βŸ§ ⟦sβ‚‚βŸ§ f H = f s₁ sβ‚‚ := by cases s₁; cases sβ‚‚; refl @[elab_as_eliminator] theorem induction_on {C : finmap Ξ² β†’ Prop} (s : finmap Ξ²) (H : βˆ€ (a : alist Ξ²), C ⟦a⟧) : C s := by rcases s with ⟨⟨a⟩, h⟩; exact H ⟨a, h⟩ @[elab_as_eliminator] theorem induction_onβ‚‚ {C : finmap Ξ² β†’ finmap Ξ² β†’ Prop} (s₁ sβ‚‚ : finmap Ξ²) (H : βˆ€ (a₁ aβ‚‚ : alist Ξ²), C ⟦aβ‚βŸ§ ⟦aβ‚‚βŸ§) : C s₁ sβ‚‚ := induction_on s₁ $ Ξ» l₁, induction_on sβ‚‚ $ Ξ» lβ‚‚, H l₁ lβ‚‚ @[elab_as_eliminator] theorem induction_on₃ {C : finmap Ξ² β†’ finmap Ξ² β†’ finmap Ξ² β†’ Prop} (s₁ sβ‚‚ s₃ : finmap Ξ²) (H : βˆ€ (a₁ aβ‚‚ a₃ : alist Ξ²), C ⟦aβ‚βŸ§ ⟦aβ‚‚βŸ§ ⟦aβ‚ƒβŸ§) : C s₁ sβ‚‚ s₃ := induction_onβ‚‚ s₁ sβ‚‚ $ Ξ» l₁ lβ‚‚, induction_on s₃ $ Ξ» l₃, H l₁ lβ‚‚ l₃ @[ext] theorem ext : βˆ€ {s t : finmap Ξ²}, s.entries = t.entries β†’ s = t | ⟨l₁, hβ‚βŸ© ⟨lβ‚‚, hβ‚‚βŸ© H := by congr' @[simp] theorem ext_iff {s t : finmap Ξ²} : s.entries = t.entries ↔ s = t := ⟨ext, congr_arg _⟩ /-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/ instance : has_mem Ξ± (finmap Ξ²) := ⟨λ a s, a ∈ s.entries.keys⟩ theorem mem_def {a : Ξ±} {s : finmap Ξ²} : a ∈ s ↔ a ∈ s.entries.keys := iff.rfl @[simp] theorem mem_to_finmap {a : Ξ±} {s : alist Ξ²} : a ∈ ⟦s⟧ ↔ a ∈ s := iff.rfl /-- The set of keys of a finite map. -/ def keys (s : finmap Ξ²) : finset Ξ± := ⟨s.entries.keys, induction_on s keys_nodup⟩ @[simp] theorem keys_val (s : alist Ξ²) : (keys ⟦s⟧).val = s.keys := rfl @[simp] theorem keys_ext {s₁ sβ‚‚ : alist Ξ²} : keys ⟦sβ‚βŸ§ = keys ⟦sβ‚‚βŸ§ ↔ s₁.keys ~ sβ‚‚.keys := by simp [keys, alist.keys] theorem mem_keys {a : Ξ±} {s : finmap Ξ²} : a ∈ s.keys ↔ a ∈ s := induction_on s $ Ξ» s, alist.mem_keys /-- The empty map. -/ instance : has_emptyc (finmap Ξ²) := ⟨⟨0, nodupkeys_nil⟩⟩ instance : inhabited (finmap Ξ²) := βŸ¨βˆ…βŸ© @[simp] theorem empty_to_finmap : (βŸ¦βˆ…βŸ§ : finmap Ξ²) = βˆ… := rfl @[simp] theorem to_finmap_nil [decidable_eq Ξ±] : (list.to_finmap [] : finmap Ξ²) = βˆ… := rfl theorem not_mem_empty {a : Ξ±} : a βˆ‰ (βˆ… : finmap Ξ²) := multiset.not_mem_zero a @[simp] theorem keys_empty : (βˆ… : finmap Ξ²).keys = βˆ… := rfl /-- The singleton map. -/ def singleton (a : Ξ±) (b : Ξ² a) : finmap Ξ² := ⟦ alist.singleton a b ⟧ @[simp] theorem keys_singleton (a : Ξ±) (b : Ξ² a) : (singleton a b).keys = finset.singleton a := rfl @[simp] lemma mem_singleton (x y : Ξ±) (b : Ξ² y) : x ∈ singleton y b ↔ x = y := by simp only [singleton]; erw [mem_cons_eq,mem_nil_iff,or_false] variables [decidable_eq Ξ±] instance has_decidable_eq [βˆ€ a, decidable_eq (Ξ² a)] : decidable_eq (finmap Ξ²) | s₁ sβ‚‚ := decidable_of_iff _ ext_iff /-- Look up the value associated to a key in a map. -/ def lookup (a : Ξ±) (s : finmap Ξ²) : option (Ξ² a) := lift_on s (lookup a) (Ξ» s t, perm_lookup) @[simp] theorem lookup_to_finmap (a : Ξ±) (s : alist Ξ²) : lookup a ⟦s⟧ = s.lookup a := rfl @[simp] theorem lookup_list_to_finmap (a : Ξ±) (s : list (sigma Ξ²)) : lookup a s.to_finmap = s.lookup a := by rw [list.to_finmap,lookup_to_finmap,lookup_to_alist] @[simp] theorem lookup_empty (a) : lookup a (βˆ… : finmap Ξ²) = none := rfl theorem lookup_is_some {a : Ξ±} {s : finmap Ξ²} : (s.lookup a).is_some ↔ a ∈ s := induction_on s $ Ξ» s, alist.lookup_is_some theorem lookup_eq_none {a} {s : finmap Ξ²} : lookup a s = none ↔ a βˆ‰ s := induction_on s $ Ξ» s, alist.lookup_eq_none @[simp] lemma lookup_singleton_eq {a : Ξ±} {b : Ξ² a} : (singleton a b).lookup a = some b := by rw [singleton,lookup_to_finmap,alist.singleton,alist.lookup,lookup_cons_eq] instance (a : Ξ±) (s : finmap Ξ²) : decidable (a ∈ s) := decidable_of_iff _ lookup_is_some /-- Replace a key with a given value in a finite map. If the key is not present it does nothing. -/ def replace (a : Ξ±) (b : Ξ² a) (s : finmap Ξ²) : finmap Ξ² := lift_on s (Ξ» t, ⟦replace a b t⟧) $ Ξ» s₁ sβ‚‚ p, to_finmap_eq.2 $ perm_replace p @[simp] theorem replace_to_finmap (a : Ξ±) (b : Ξ² a) (s : alist Ξ²) : replace a b ⟦s⟧ = ⟦s.replace a b⟧ := by simp [replace] @[simp] theorem keys_replace (a : Ξ±) (b : Ξ² a) (s : finmap Ξ²) : (replace a b s).keys = s.keys := induction_on s $ Ξ» s, by simp @[simp] theorem mem_replace {a a' : Ξ±} {b : Ξ² a} {s : finmap Ξ²} : a' ∈ replace a b s ↔ a' ∈ s := induction_on s $ Ξ» s, by simp /-- Fold a commutative function over the key-value pairs in the map -/ def foldl {Ξ΄ : Type w} (f : Ξ΄ β†’ Ξ  a, Ξ² a β†’ Ξ΄) (H : βˆ€ d a₁ b₁ aβ‚‚ bβ‚‚, f (f d a₁ b₁) aβ‚‚ bβ‚‚ = f (f d aβ‚‚ bβ‚‚) a₁ b₁) (d : Ξ΄) (m : finmap Ξ²) : Ξ΄ := m.entries.foldl (Ξ» d s, f d s.1 s.2) (Ξ» d s t, H _ _ _ _ _) d def any (f : Ξ  x, Ξ² x β†’ bool) (s : finmap Ξ²) : bool := s.foldl (Ξ» x y z, x ∨ f y z) (by simp [or_assoc]; intros; congr' 2; rw or_comm) ff def all (f : Ξ  x, Ξ² x β†’ bool) (s : finmap Ξ²) : bool := s.foldl (Ξ» x y z, x ∧ f y z) (by simp [and_assoc]; intros; congr' 2; rw and_comm) ff /-- Erase a key from the map. If the key is not present it does nothing. -/ def erase (a : Ξ±) (s : finmap Ξ²) : finmap Ξ² := lift_on s (Ξ» t, ⟦erase a t⟧) $ Ξ» s₁ sβ‚‚ p, to_finmap_eq.2 $ perm_erase p @[simp] theorem erase_to_finmap (a : Ξ±) (s : alist Ξ²) : erase a ⟦s⟧ = ⟦s.erase a⟧ := by simp [erase] @[simp] theorem keys_erase_to_finset (a : Ξ±) (s : alist Ξ²) : keys ⟦s.erase a⟧ = (keys ⟦s⟧).erase a := by simp [finset.erase, keys, alist.erase, keys_kerase] @[simp] theorem keys_erase (a : Ξ±) (s : finmap Ξ²) : (erase a s).keys = s.keys.erase a := induction_on s $ Ξ» s, by simp @[simp] theorem mem_erase {a a' : Ξ±} {s : finmap Ξ²} : a' ∈ erase a s ↔ a' β‰  a ∧ a' ∈ s := induction_on s $ Ξ» s, by simp theorem not_mem_erase_self {a : Ξ±} {s : finmap Ξ²} : Β¬ a ∈ erase a s := by rw [mem_erase,not_and_distrib,not_not]; left; refl @[simp] theorem lookup_erase (a) (s : finmap Ξ²) : lookup a (erase a s) = none := induction_on s $ lookup_erase a @[simp] theorem lookup_erase_ne {a a'} {s : finmap Ξ²} (h : a β‰  a') : lookup a (erase a' s) = lookup a s := induction_on s $ Ξ» s, lookup_erase_ne h theorem erase_erase {a a' : Ξ±} {s : finmap Ξ²} : erase a (erase a' s) = erase a' (erase a s) := induction_on s $ Ξ» s, ext (by simp [alist.erase_erase]) lemma mem_iff {a : Ξ±} {s : finmap Ξ²} : a ∈ s ↔ βˆƒ b, s.lookup a = some b := induction_on s $ Ξ» s, iff.trans list.mem_keys $ exists_congr $ Ξ» b, (mem_lookup_iff s.nodupkeys).symm lemma mem_of_lookup_eq_some {a : Ξ±} {b : Ξ² a} {s : finmap Ξ²} (h : s.lookup a = some b) : a ∈ s := mem_iff.mpr ⟨_,h⟩ /- sub -/ def sdiff (s s' : finmap Ξ²) : finmap Ξ² := s'.foldl (Ξ» s x _, s.erase x) (Ξ» aβ‚€ a₁ _ aβ‚‚ _, erase_erase) s instance : has_sdiff (finmap Ξ²) := ⟨ sdiff ⟩ /- insert -/ /-- Insert a key-value pair into a finite map, replacing any existing pair with the same key. -/ def insert (a : Ξ±) (b : Ξ² a) (s : finmap Ξ²) : finmap Ξ² := lift_on s (Ξ» t, ⟦insert a b t⟧) $ Ξ» s₁ sβ‚‚ p, to_finmap_eq.2 $ perm_insert p @[simp] theorem insert_to_finmap (a : Ξ±) (b : Ξ² a) (s : alist Ξ²) : insert a b ⟦s⟧ = ⟦s.insert a b⟧ := by simp [insert] theorem insert_entries_of_neg {a : Ξ±} {b : Ξ² a} {s : finmap Ξ²} : a βˆ‰ s β†’ (insert a b s).entries = ⟨a, b⟩ :: s.entries := induction_on s $ Ξ» s h, by simp [insert_entries_of_neg (mt mem_to_finmap.1 h)] @[simp] theorem mem_insert {a a' : Ξ±} {b' : Ξ² a'} {s : finmap Ξ²} : a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s := induction_on s mem_insert @[simp] theorem lookup_insert {a} {b : Ξ² a} (s : finmap Ξ²) : lookup a (insert a b s) = some b := induction_on s $ Ξ» s, by simp only [insert_to_finmap, lookup_to_finmap, lookup_insert] @[simp] theorem lookup_insert_of_ne {a a'} {b : Ξ² a} (s : finmap Ξ²) (h : a' β‰  a) : lookup a' (insert a b s) = lookup a' s := induction_on s $ Ξ» s, by simp only [insert_to_finmap, lookup_to_finmap, lookup_insert_ne h] @[simp] theorem insert_insert {a} {b b' : Ξ² a} (s : finmap Ξ²) : (s.insert a b).insert a b' = s.insert a b' := induction_on s $ Ξ» s, by simp only [insert_to_finmap, insert_insert] theorem insert_insert_of_ne {a a'} {b : Ξ² a} {b' : Ξ² a'} (s : finmap Ξ²) (h : a β‰  a') : (s.insert a b).insert a' b' = (s.insert a' b').insert a b := induction_on s $ Ξ» s, by simp only [insert_to_finmap,alist.to_finmap_eq,insert_insert_of_ne _ h] theorem to_finmap_cons (a : Ξ±) (b : Ξ² a) (xs : list (sigma Ξ²)) : list.to_finmap (⟨a,b⟩ :: xs) = insert a b xs.to_finmap := rfl theorem mem_list_to_finmap (a : Ξ±) (xs : list (sigma Ξ²)) : a ∈ xs.to_finmap ↔ (βˆƒ b : Ξ² a, sigma.mk a b ∈ xs) := by { induction xs with x xs; [skip, cases x]; simp only [to_finmap_cons, *, not_mem_empty, exists_or_distrib, list.not_mem_nil, finmap.to_finmap_nil, iff_self, exists_false, mem_cons_iff, mem_insert, exists_and_distrib_left]; apply or_congr _ iff.rfl, conv { to_lhs, rw ← and_true (a = x_fst) }, apply and_congr_right, rintro ⟨⟩, simp only [exists_eq, iff_self, heq_iff_eq] } @[simp] theorem insert_singleton_eq {a : Ξ±} {b b' : Ξ² a} : insert a b (singleton a b') = singleton a b := by simp only [singleton, finmap.insert_to_finmap, alist.insert_singleton_eq] /- extract -/ /-- Erase a key from the map, and return the corresponding value, if found. -/ def extract (a : Ξ±) (s : finmap Ξ²) : option (Ξ² a) Γ— finmap Ξ² := lift_on s (Ξ» t, prod.map id to_finmap (extract a t)) $ Ξ» s₁ sβ‚‚ p, by simp [perm_lookup p, to_finmap_eq, perm_erase p] @[simp] theorem extract_eq_lookup_erase (a : Ξ±) (s : finmap Ξ²) : extract a s = (lookup a s, erase a s) := induction_on s $ Ξ» s, by simp [extract] /- union -/ /-- `s₁ βˆͺ sβ‚‚` is the key-based union of two finite maps. It is left-biased: if there exists an `a ∈ s₁`, `lookup a (s₁ βˆͺ sβ‚‚) = lookup a s₁`. -/ def union (s₁ sβ‚‚ : finmap Ξ²) : finmap Ξ² := lift_onβ‚‚ s₁ sβ‚‚ (Ξ» s₁ sβ‚‚, ⟦s₁ βˆͺ sβ‚‚βŸ§) $ Ξ» s₁ sβ‚‚ s₃ sβ‚„ p₁₃ pβ‚‚β‚„, to_finmap_eq.mpr $ perm_union p₁₃ pβ‚‚β‚„ instance : has_union (finmap Ξ²) := ⟨union⟩ @[simp] theorem mem_union {a} {s₁ sβ‚‚ : finmap Ξ²} : a ∈ s₁ βˆͺ sβ‚‚ ↔ a ∈ s₁ ∨ a ∈ sβ‚‚ := induction_onβ‚‚ s₁ sβ‚‚ $ Ξ» _ _, mem_union @[simp] theorem union_to_finmap (s₁ sβ‚‚ : alist Ξ²) : ⟦sβ‚βŸ§ βˆͺ ⟦sβ‚‚βŸ§ = ⟦s₁ βˆͺ sβ‚‚βŸ§ := by simp [(βˆͺ), union] theorem keys_union {s₁ sβ‚‚ : finmap Ξ²} : (s₁ βˆͺ sβ‚‚).keys = s₁.keys βˆͺ sβ‚‚.keys := induction_onβ‚‚ s₁ sβ‚‚ $ Ξ» s₁ sβ‚‚, finset.ext' $ by simp [keys] @[simp] theorem lookup_union_left {a} {s₁ sβ‚‚ : finmap Ξ²} : a ∈ s₁ β†’ lookup a (s₁ βˆͺ sβ‚‚) = lookup a s₁ := induction_onβ‚‚ s₁ sβ‚‚ $ Ξ» s₁ sβ‚‚, lookup_union_left @[simp] theorem lookup_union_right {a} {s₁ sβ‚‚ : finmap Ξ²} : a βˆ‰ s₁ β†’ lookup a (s₁ βˆͺ sβ‚‚) = lookup a sβ‚‚ := induction_onβ‚‚ s₁ sβ‚‚ $ Ξ» s₁ sβ‚‚, lookup_union_right theorem lookup_union_left_of_not_in {a} {s₁ sβ‚‚ : finmap Ξ²} : a βˆ‰ sβ‚‚ β†’ lookup a (s₁ βˆͺ sβ‚‚) = lookup a s₁ := begin intros h, by_cases h' : a ∈ s₁, { rw lookup_union_left h' }, { rw [lookup_union_right h',lookup_eq_none.mpr h,lookup_eq_none.mpr h'] } end @[simp] theorem mem_lookup_union {a} {b : Ξ² a} {s₁ sβ‚‚ : finmap Ξ²} : b ∈ lookup a (s₁ βˆͺ sβ‚‚) ↔ b ∈ lookup a s₁ ∨ a βˆ‰ s₁ ∧ b ∈ lookup a sβ‚‚ := induction_onβ‚‚ s₁ sβ‚‚ $ Ξ» s₁ sβ‚‚, mem_lookup_union theorem mem_lookup_union_middle {a} {b : Ξ² a} {s₁ sβ‚‚ s₃ : finmap Ξ²} : b ∈ lookup a (s₁ βˆͺ s₃) β†’ a βˆ‰ sβ‚‚ β†’ b ∈ lookup a (s₁ βˆͺ sβ‚‚ βˆͺ s₃) := induction_on₃ s₁ sβ‚‚ s₃ $ Ξ» s₁ sβ‚‚ s₃, mem_lookup_union_middle theorem insert_union {a} {b : Ξ² a} {s₁ sβ‚‚ : finmap Ξ²} : insert a b (s₁ βˆͺ sβ‚‚) = insert a b s₁ βˆͺ sβ‚‚ := induction_onβ‚‚ s₁ sβ‚‚ $ Ξ» a₁ aβ‚‚, by simp [insert_union] theorem union_assoc {s₁ sβ‚‚ s₃ : finmap Ξ²} : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = s₁ βˆͺ (sβ‚‚ βˆͺ s₃) := induction_on₃ s₁ sβ‚‚ s₃ $ Ξ» s₁ sβ‚‚ s₃, by simp only [alist.to_finmap_eq,union_to_finmap,alist.union_assoc] @[simp] theorem empty_union {s₁ : finmap Ξ²} : βˆ… βˆͺ s₁ = s₁ := induction_on s₁ $ Ξ» s₁, by rw ← empty_to_finmap; simp [- empty_to_finmap, alist.to_finmap_eq,union_to_finmap,alist.union_assoc] @[simp] theorem union_empty {s₁ : finmap Ξ²} : s₁ βˆͺ βˆ… = s₁ := induction_on s₁ $ Ξ» s₁, by rw ← empty_to_finmap; simp [- empty_to_finmap, alist.to_finmap_eq,union_to_finmap,alist.union_assoc] theorem ext_lookup {s₁ sβ‚‚ : finmap Ξ²} : (βˆ€ x, s₁.lookup x = sβ‚‚.lookup x) β†’ s₁ = sβ‚‚ := induction_onβ‚‚ s₁ sβ‚‚ $ Ξ» s₁ sβ‚‚ h, by simp only [alist.lookup, lookup_to_finmap] at h; rw [alist.to_finmap_eq]; apply lookup_ext s₁.nodupkeys sβ‚‚.nodupkeys; intros x y; rw h theorem erase_union_singleton (a : Ξ±) (b : Ξ² a) (s : finmap Ξ²) (h : s.lookup a = some b) : s.erase a βˆͺ singleton a b = s := ext_lookup (by { intro, by_cases h' : x = a, { subst a, rw [lookup_union_right not_mem_erase_self,lookup_singleton_eq,h], }, { have : x βˆ‰ singleton a b, { rw mem_singleton, exact h' }, rw [lookup_union_left_of_not_in this,lookup_erase_ne h'] } } ) /- disjoint -/ def disjoint (s₁ sβ‚‚ : finmap Ξ²) := βˆ€ x ∈ s₁, Β¬ x ∈ sβ‚‚ instance : decidable_rel (@disjoint Ξ± Ξ² _) := by intros x y; dsimp [disjoint]; apply_instance lemma disjoint_empty (x : finmap Ξ²) : disjoint βˆ… x . @[symm] lemma disjoint.symm (x y : finmap Ξ²) (h : disjoint x y) : disjoint y x := Ξ» p hy hx, h p hx hy lemma disjoint.symm_iff (x y : finmap Ξ²) : disjoint x y ↔ disjoint y x := ⟨ disjoint.symm x y, disjoint.symm y x ⟩ lemma disjoint_union_left (x y z : finmap Ξ²) : disjoint (x βˆͺ y) z ↔ disjoint x z ∧ disjoint y z := by simp [disjoint,finmap.mem_union,or_imp_distrib,forall_and_distrib] lemma disjoint_union_right (x y z : finmap Ξ²) : disjoint x (y βˆͺ z) ↔ disjoint x y ∧ disjoint x z := by rw [disjoint.symm_iff,disjoint_union_left,disjoint.symm_iff _ x,disjoint.symm_iff _ x] theorem union_comm_of_disjoint {s₁ sβ‚‚ : finmap Ξ²} : disjoint s₁ sβ‚‚ β†’ s₁ βˆͺ sβ‚‚ = sβ‚‚ βˆͺ s₁ := induction_onβ‚‚ s₁ sβ‚‚ $ Ξ» s₁ sβ‚‚, by { intros h, simp only [alist.to_finmap_eq,union_to_finmap,alist.union_comm_of_disjoint h] } theorem union_cancel {s₁ sβ‚‚ s₃ : finmap Ξ²} (h : disjoint s₁ s₃) (h' : disjoint sβ‚‚ s₃) : s₁ βˆͺ s₃ = sβ‚‚ βˆͺ s₃ ↔ s₁ = sβ‚‚ := ⟨λ h'', begin apply ext_lookup, intro x, have : (s₁ βˆͺ s₃).lookup x = (sβ‚‚ βˆͺ s₃).lookup x, from h'' β–Έ rfl, by_cases hs₁ : x ∈ s₁, { rw [lookup_union_left hs₁,lookup_union_left_of_not_in (h _ hs₁)] at this, exact this }, { by_cases hsβ‚‚ : x ∈ sβ‚‚, { rw [lookup_union_left_of_not_in (h' _ hsβ‚‚),lookup_union_left hsβ‚‚] at this; exact this }, { rw [lookup_eq_none.mpr hs₁,lookup_eq_none.mpr hsβ‚‚] } } end, Ξ» h, h β–Έ rfl⟩ end finmap
bc7c61a38aafb6920eaf788a1f602b475514eab5
3446e92e64a5de7ed1f2109cfb024f83cd904c34
/src/game/world2/level3.lean
ea12f38c326ad61194ec0162198ca55381b2d90d
[]
no_license
kckennylau/natural_number_game
019f4a5f419c9681e65234ecd124c564f9a0a246
ad8c0adaa725975be8a9f978c8494a39311029be
refs/heads/master
1,598,784,137,722
1,571,905,156,000
1,571,905,156,000
218,354,686
0
0
null
1,572,373,319,000
1,572,373,318,000
null
UTF-8
Lean
false
false
1,689
lean
import mynat.definition -- hide import mynat.add -- hide import game.world2.level2 -- hide namespace mynat -- hide /- # World 2 -- addition world ## Level 3 : `succ_inj` ## You are equipped with: * `zero_ne_succ : βˆ€ (a : mynat), zero β‰  succ(a)` * `succ_inj : βˆ€ a b : mynat, succ(a) = succ(b) β†’ a = b` * `add_zero : βˆ€ a : mynat, a + 0 = a` * `add_succ : βˆ€ a b : mynat, a + succ(b) = succ(a + b)` * `zero_add` : βˆ€ a : mynat, 0 + a = a` * `add_assoc : βˆ€ a b c : mynat, (a + b) + c = a + (b + c)` Oh no! On the way to `add_comm`, a wild `succ_add` appears. `succ_add` is the statement that `succ(a) + b = succ(a+b)` for `a` and `b` in your natural number type. You will need this theorem to prove `a + b = b + a` so you'd better prove it first. NB: think about why computer scientists called this result `succ_add` . There is a logic to all the names. Note that if you want to be more precise about exactly where you want to rewrite something like `add_succ`, you can do things like `rw add_succ (succ a)` or `rw add_succ (succ a) d`, telling Lean explicitly what to use for the input variables for the function `add_succ`. Indeed, `add_succ` is a function -- it takes as input two variables `a` and `b` and outputs a proof that `a + succ(b) = succ(a + b)`. The tactic `rw add_succ` just says to Lean "guess what the variables are". -/ /- Lemma For all natural numbers $a, b$, we have $$ \operatorname{succ}(a) + b = \operatorname{succ}(a + b). $$ -/ lemma succ_add (a b : mynat) : succ a + b = succ (a + b) := begin [less_leaky] induction b with d hd, { refl }, { rw add_succ, rw hd, rw add_succ, refl } end end mynat -- hide
67c9120ffbcb07fe51e08c6896977d5dcf755ae2
c777c32c8e484e195053731103c5e52af26a25d1
/src/data/mv_polynomial/rename.lean
858e5b2cf69d032ec38378f47ecae4a9d5bf1c3f
[ "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
11,035
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.mv_polynomial.basic /-! # Renaming variables of polynomials > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file establishes the `rename` operation on multivariate polynomials, which modifies the set of variables. ## Main declarations * `mv_polynomial.rename` * `mv_polynomial.rename_equiv` ## Notation As in other polynomial files, we typically use the notation: + `Οƒ Ο„ Ξ± : Type*` (indexing the variables) + `R S : Type*` `[comm_semiring R]` `[comm_semiring S]` (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` + `r : R` elements of the coefficient ring + `i : Οƒ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial Οƒ Ξ±` -/ noncomputable theory open_locale classical big_operators open set function finsupp add_monoid_algebra open_locale big_operators variables {Οƒ Ο„ Ξ± R S : Type*} [comm_semiring R] [comm_semiring S] namespace mv_polynomial section rename /-- Rename all the variables in a multivariable polynomial. -/ def rename (f : Οƒ β†’ Ο„) : mv_polynomial Οƒ R →ₐ[R] mv_polynomial Ο„ R := aeval (X ∘ f) @[simp] lemma rename_C (f : Οƒ β†’ Ο„) (r : R) : rename f (C r) = C r := evalβ‚‚_C _ _ _ @[simp] lemma rename_X (f : Οƒ β†’ Ο„) (i : Οƒ) : rename f (X i : mv_polynomial Οƒ R) = X (f i) := evalβ‚‚_X _ _ _ lemma map_rename (f : R β†’+* S) (g : Οƒ β†’ Ο„) (p : mv_polynomial Οƒ R) : map f (rename g p) = rename g (map f p) := mv_polynomial.induction_on p (Ξ» a, by simp only [map_C, rename_C]) (Ξ» p q hp hq, by simp only [hp, hq, alg_hom.map_add, ring_hom.map_add]) (Ξ» p n hp, by simp only [hp, rename_X, map_X, ring_hom.map_mul, alg_hom.map_mul]) @[simp] lemma rename_rename (f : Οƒ β†’ Ο„) (g : Ο„ β†’ Ξ±) (p : mv_polynomial Οƒ R) : rename g (rename f p) = rename (g ∘ f) p := show rename g (evalβ‚‚ C (X ∘ f) p) = _, begin simp only [rename, aeval_eq_evalβ‚‚_hom], simp [evalβ‚‚_comp_left _ C (X ∘ f) p, (∘), evalβ‚‚_C, eval_X], apply evalβ‚‚_hom_congr _ rfl rfl, ext1, simp only [comp_app, ring_hom.coe_comp, evalβ‚‚_hom_C], end @[simp] lemma rename_id (p : mv_polynomial Οƒ R) : rename id p = p := evalβ‚‚_eta p lemma rename_monomial (f : Οƒ β†’ Ο„) (d : Οƒ β†’β‚€ β„•) (r : R) : rename f (monomial d r) = monomial (d.map_domain f) r := begin rw [rename, aeval_monomial, monomial_eq, finsupp.prod_map_domain_index], { refl }, { exact assume n, pow_zero _ }, { exact assume n i₁ iβ‚‚, pow_add _ _ _ } end lemma rename_eq (f : Οƒ β†’ Ο„) (p : mv_polynomial Οƒ R) : rename f p = finsupp.map_domain (finsupp.map_domain f) p := begin simp only [rename, aeval_def, evalβ‚‚, finsupp.map_domain, algebra_map_eq, X_pow_eq_monomial, ← monomial_finsupp_sum_index], refl end lemma rename_injective (f : Οƒ β†’ Ο„) (hf : function.injective f) : function.injective (rename f : mv_polynomial Οƒ R β†’ mv_polynomial Ο„ R) := have (rename f : mv_polynomial Οƒ R β†’ mv_polynomial Ο„ R) = finsupp.map_domain (finsupp.map_domain f) := funext (rename_eq f), begin rw this, exact finsupp.map_domain_injective (finsupp.map_domain_injective hf) end section variables {f : Οƒ β†’ Ο„} (hf : function.injective f) open_locale classical /-- Given a function between sets of variables `f : Οƒ β†’ Ο„` that is injective with proof `hf`, `kill_compl hf` is the `alg_hom` from `R[Ο„]` to `R[Οƒ]` that is left inverse to `rename f : R[Οƒ] β†’ R[Ο„]` and sends the variables in the complement of the range of `f` to `0`. -/ def kill_compl : mv_polynomial Ο„ R →ₐ[R] mv_polynomial Οƒ R := aeval (Ξ» i, if h : i ∈ set.range f then X $ (equiv.of_injective f hf).symm ⟨i,h⟩ else 0) lemma kill_compl_comp_rename : (kill_compl hf).comp (rename f) = alg_hom.id R _ := alg_hom_ext $ Ξ» i, by { dsimp, rw [rename, kill_compl, aeval_X, aeval_X, dif_pos, equiv.of_injective_symm_apply] } @[simp] lemma kill_compl_rename_app (p : mv_polynomial Οƒ R) : kill_compl hf (rename f p) = p := alg_hom.congr_fun (kill_compl_comp_rename hf) p end section variables (R) /-- `mv_polynomial.rename e` is an equivalence when `e` is. -/ @[simps apply] def rename_equiv (f : Οƒ ≃ Ο„) : mv_polynomial Οƒ R ≃ₐ[R] mv_polynomial Ο„ R := { to_fun := rename f, inv_fun := rename f.symm, left_inv := Ξ» p, by rw [rename_rename, f.symm_comp_self, rename_id], right_inv := Ξ» p, by rw [rename_rename, f.self_comp_symm, rename_id], ..rename f} @[simp] lemma rename_equiv_refl : rename_equiv R (equiv.refl Οƒ) = alg_equiv.refl := alg_equiv.ext rename_id @[simp] lemma rename_equiv_symm (f : Οƒ ≃ Ο„) : (rename_equiv R f).symm = rename_equiv R f.symm := rfl @[simp] lemma rename_equiv_trans (e : Οƒ ≃ Ο„) (f : Ο„ ≃ Ξ±): (rename_equiv R e).trans (rename_equiv R f) = rename_equiv R (e.trans f) := alg_equiv.ext (rename_rename e f) end section variables (f : R β†’+* S) (k : Οƒ β†’ Ο„) (g : Ο„ β†’ S) (p : mv_polynomial Οƒ R) lemma evalβ‚‚_rename : (rename k p).evalβ‚‚ f g = p.evalβ‚‚ f (g ∘ k) := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma evalβ‚‚_hom_rename : evalβ‚‚_hom f g (rename k p) = evalβ‚‚_hom f (g ∘ k) p := evalβ‚‚_rename _ _ _ _ lemma aeval_rename [algebra R S] : aeval g (rename k p) = aeval (g ∘ k) p := evalβ‚‚_hom_rename _ _ _ _ lemma rename_evalβ‚‚ (g : Ο„ β†’ mv_polynomial Οƒ R) : rename k (p.evalβ‚‚ C (g ∘ k)) = (rename k p).evalβ‚‚ C (rename k ∘ g) := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma rename_prodmk_evalβ‚‚ (j : Ο„) (g : Οƒ β†’ mv_polynomial Οƒ R) : rename (prod.mk j) (p.evalβ‚‚ C g) = p.evalβ‚‚ C (Ξ» x, rename (prod.mk j) (g x)) := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma evalβ‚‚_rename_prodmk (g : Οƒ Γ— Ο„ β†’ S) (i : Οƒ) (p : mv_polynomial Ο„ R) : (rename (prod.mk i) p).evalβ‚‚ f g = evalβ‚‚ f (Ξ» j, g (i, j)) p := by apply mv_polynomial.induction_on p; { intros, simp [*] } lemma eval_rename_prodmk (g : Οƒ Γ— Ο„ β†’ R) (i : Οƒ) (p : mv_polynomial Ο„ R) : eval g (rename (prod.mk i) p) = eval (Ξ» j, g (i, j)) p := evalβ‚‚_rename_prodmk (ring_hom.id _) _ _ _ end /-- Every polynomial is a polynomial in finitely many variables. -/ theorem exists_finset_rename (p : mv_polynomial Οƒ R) : βˆƒ (s : finset Οƒ) (q : mv_polynomial {x // x ∈ s} R), p = rename coe q := begin apply induction_on p, { intro r, exact βŸ¨βˆ…, C r, by rw rename_C⟩ }, { rintro p q ⟨s, p, rfl⟩ ⟨t, q, rfl⟩, refine ⟨s βˆͺ t, ⟨_, _⟩⟩, { refine rename (subtype.map id _) p + rename (subtype.map id _) q; simp only [id.def, true_or, or_true, finset.mem_union, forall_true_iff] {contextual := tt}, }, { simp only [rename_rename, alg_hom.map_add], refl, }, }, { rintro p n ⟨s, p, rfl⟩, refine ⟨insert n s, ⟨_, _⟩⟩, { refine rename (subtype.map id _) p * X ⟨n, s.mem_insert_self n⟩, simp only [id.def, or_true, finset.mem_insert, forall_true_iff] {contextual := tt}, }, { simp only [rename_rename, rename_X, subtype.coe_mk, alg_hom.map_mul], refl, }, }, end /-- `exists_finset_rename` for two polyonomials at once: for any two polynomials `p₁`, `pβ‚‚` in a polynomial semiring `R[Οƒ]` of possibly infinitely many variables, `exists_finset_renameβ‚‚` yields a finite subset `s` of `Οƒ` such that both `p₁` and `pβ‚‚` are contained in the polynomial semiring `R[s]` of finitely many variables. -/ lemma exists_finset_renameβ‚‚ (p₁ pβ‚‚ : mv_polynomial Οƒ R) : βˆƒ (s : finset Οƒ) (q₁ qβ‚‚ : mv_polynomial s R), p₁ = rename coe q₁ ∧ pβ‚‚ = rename coe qβ‚‚ := begin obtain ⟨s₁,q₁,rfl⟩ := exists_finset_rename p₁, obtain ⟨sβ‚‚,qβ‚‚,rfl⟩ := exists_finset_rename pβ‚‚, classical, use s₁ βˆͺ sβ‚‚, use rename (set.inclusion $ s₁.subset_union_left sβ‚‚) q₁, use rename (set.inclusion $ s₁.subset_union_right sβ‚‚) qβ‚‚, split; simpa, end /-- Every polynomial is a polynomial in finitely many variables. -/ theorem exists_fin_rename (p : mv_polynomial Οƒ R) : βˆƒ (n : β„•) (f : fin n β†’ Οƒ) (hf : injective f) (q : mv_polynomial (fin n) R), p = rename f q := begin obtain ⟨s, q, rfl⟩ := exists_finset_rename p, let n := fintype.card {x // x ∈ s}, let e := fintype.equiv_fin {x // x ∈ s}, refine ⟨n, coe ∘ e.symm, subtype.val_injective.comp e.symm.injective, rename e q, _⟩, rw [← rename_rename, rename_rename e], simp only [function.comp, equiv.symm_apply_apply, rename_rename] end end rename lemma evalβ‚‚_cast_comp (f : Οƒ β†’ Ο„) (c : β„€ β†’+* R) (g : Ο„ β†’ R) (p : mv_polynomial Οƒ β„€) : evalβ‚‚ c (g ∘ f) p = evalβ‚‚ c g (rename f p) := mv_polynomial.induction_on p (Ξ» n, by simp only [evalβ‚‚_C, rename_C]) (Ξ» p q hp hq, by simp only [hp, hq, rename, evalβ‚‚_add, alg_hom.map_add]) (Ξ» p n hp, by simp only [hp, rename, aeval_def, evalβ‚‚_X, evalβ‚‚_mul]) section coeff @[simp] lemma coeff_rename_map_domain (f : Οƒ β†’ Ο„) (hf : injective f) (Ο† : mv_polynomial Οƒ R) (d : Οƒ β†’β‚€ β„•) : (rename f Ο†).coeff (d.map_domain f) = Ο†.coeff d := begin apply induction_on' Ο†, { intros u r, rw [rename_monomial, coeff_monomial, coeff_monomial], simp only [(finsupp.map_domain_injective hf).eq_iff] }, { intros, simp only [*, alg_hom.map_add, coeff_add], } end lemma coeff_rename_eq_zero (f : Οƒ β†’ Ο„) (Ο† : mv_polynomial Οƒ R) (d : Ο„ β†’β‚€ β„•) (h : βˆ€ u : Οƒ β†’β‚€ β„•, u.map_domain f = d β†’ Ο†.coeff u = 0) : (rename f Ο†).coeff d = 0 := begin rw [rename_eq, ← not_mem_support_iff], intro H, replace H := map_domain_support H, rw [finset.mem_image] at H, obtain ⟨u, hu, rfl⟩ := H, specialize h u rfl, simp at h hu, contradiction end lemma coeff_rename_ne_zero (f : Οƒ β†’ Ο„) (Ο† : mv_polynomial Οƒ R) (d : Ο„ β†’β‚€ β„•) (h : (rename f Ο†).coeff d β‰  0) : βˆƒ u : Οƒ β†’β‚€ β„•, u.map_domain f = d ∧ Ο†.coeff u β‰  0 := by { contrapose! h, apply coeff_rename_eq_zero _ _ _ h } @[simp] lemma constant_coeff_rename {Ο„ : Type*} (f : Οƒ β†’ Ο„) (Ο† : mv_polynomial Οƒ R) : constant_coeff (rename f Ο†) = constant_coeff Ο† := begin apply Ο†.induction_on, { intro a, simp only [constant_coeff_C, rename_C]}, { intros p q hp hq, simp only [hp, hq, ring_hom.map_add, alg_hom.map_add] }, { intros p n hp, simp only [hp, rename_X, constant_coeff_X, ring_hom.map_mul, alg_hom.map_mul] } end end coeff section support lemma support_rename_of_injective {p : mv_polynomial Οƒ R} {f : Οƒ β†’ Ο„} (h : function.injective f) : (rename f p).support = finset.image (map_domain f) p.support := begin rw rename_eq, exact finsupp.map_domain_support_of_injective (map_domain_injective h) _, end end support end mv_polynomial
4b3ea827ab9c2ac4eebbd4c614c057eccf36c343
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/polynomial/monomial.lean
e0a1a3cb90c7550eef77fdaa74262d0248c9e6fb
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,613
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.polynomial.basic import Mathlib.PostPort universes u u_1 namespace Mathlib /-! # Univariate monomials Preparatory lemmas for degree_basic. -/ namespace polynomial /-- `C a` is the constant polynomial `a`. `C` is provided as a ring homomorphism. -/ def C {R : Type u} [semiring R] : R β†’+* polynomial R := add_monoid_algebra.single_zero_ring_hom @[simp] theorem monomial_zero_left {R : Type u} [semiring R] (a : R) : coe_fn (monomial 0) a = coe_fn C a := rfl theorem C_0 {R : Type u} [semiring R] : coe_fn C 0 = 0 := finsupp.single_zero theorem C_1 {R : Type u} [semiring R] : coe_fn C 1 = 1 := rfl theorem C_mul {R : Type u} {a : R} {b : R} [semiring R] : coe_fn C (a * b) = coe_fn C a * coe_fn C b := ring_hom.map_mul C a b theorem C_add {R : Type u} {a : R} {b : R} [semiring R] : coe_fn C (a + b) = coe_fn C a + coe_fn C b := ring_hom.map_add C a b @[simp] theorem C_bit0 {R : Type u} {a : R} [semiring R] : coe_fn C (bit0 a) = bit0 (coe_fn C a) := C_add @[simp] theorem C_bit1 {R : Type u} {a : R} [semiring R] : coe_fn C (bit1 a) = bit1 (coe_fn C a) := sorry theorem C_pow {R : Type u} {a : R} {n : β„•} [semiring R] : coe_fn C (a ^ n) = coe_fn C a ^ n := ring_hom.map_pow C a n @[simp] theorem C_eq_nat_cast {R : Type u} [semiring R] (n : β„•) : coe_fn C ↑n = ↑n := ring_hom.map_nat_cast C n @[simp] theorem sum_C_index {R : Type u} [semiring R] {a : R} {Ξ² : Type u_1} [add_comm_monoid Ξ²] {f : β„• β†’ R β†’ Ξ²} (h : f 0 0 = 0) : finsupp.sum (coe_fn C a) f = f 0 a := finsupp.sum_single_index h theorem coeff_C {R : Type u} {a : R} {n : β„•} [semiring R] : coeff (coe_fn C a) n = ite (n = 0) a 0 := sorry @[simp] theorem coeff_C_zero {R : Type u} {a : R} [semiring R] : coeff (coe_fn C a) 0 = a := coeff_monomial theorem nontrivial.of_polynomial_ne {R : Type u} [semiring R] {p : polynomial R} {q : polynomial R} (h : p β‰  q) : nontrivial R := sorry theorem single_eq_C_mul_X {R : Type u} {a : R} [semiring R] {n : β„•} : coe_fn (monomial n) a = coe_fn C a * X ^ n := sorry @[simp] theorem C_inj {R : Type u} {a : R} {b : R} [semiring R] : coe_fn C a = coe_fn C b ↔ a = b := { mp := fun (h : coe_fn C a = coe_fn C b) => Eq.trans (Eq.symm coeff_C_zero) (Eq.symm h β–Έ coeff_C_zero), mpr := congr_arg ⇑C } @[simp] theorem C_eq_zero {R : Type u} {a : R} [semiring R] : coe_fn C a = 0 ↔ a = 0 := iff.trans (eq.mpr (id (Eq._oldrec (Eq.refl (coe_fn C a = 0 ↔ coe_fn C a = coe_fn C 0)) C_0)) (iff.refl (coe_fn C a = 0))) C_inj protected instance infinite {R : Type u} [semiring R] [nontrivial R] : infinite (polynomial R) := sorry theorem monomial_eq_smul_X {R : Type u} {a : R} [semiring R] {n : β„•} : coe_fn (monomial n) a = a β€’ X ^ n := sorry theorem ring_hom_ext {R : Type u} [semiring R] {S : Type u_1} [semiring S] {f : polynomial R β†’+* S} {g : polynomial R β†’+* S} (h₁ : βˆ€ (a : R), coe_fn f (coe_fn C a) = coe_fn g (coe_fn C a)) (hβ‚‚ : coe_fn f X = coe_fn g X) : f = g := add_monoid_algebra.ring_hom_ext' (ring_hom.ext fun (x : R) => h₁ x) (monoid_hom.ext_mnat hβ‚‚) theorem ring_hom_ext' {R : Type u} [semiring R] {S : Type u_1} [semiring S] {f : polynomial R β†’+* S} {g : polynomial R β†’+* S} (h₁ : ring_hom.comp f C = ring_hom.comp g C) (hβ‚‚ : coe_fn f X = coe_fn g X) : f = g := ring_hom_ext (ring_hom.congr_fun h₁) hβ‚‚
7b3fa3722ecf8130bd84322242040d29e783c2b6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/groupoid/free_groupoid.lean
33fb79fc5f4912c195be2e27b5bf754deb0059a6
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,694
lean
/- Copyright (c) 2022 RΓ©mi Bottinelli. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: RΓ©mi Bottinelli -/ import category_theory.category.basic import category_theory.functor.basic import category_theory.groupoid import tactic.nth_rewrite import category_theory.path_category import category_theory.quotient import combinatorics.quiver.symmetric /-! # Free groupoid on a quiver > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the free groupoid on a quiver, the lifting of a prefunctor to its unique extension as a functor from the free groupoid, and proves uniqueness of this extension. ## Main results Given the type `V` and a quiver instance on `V`: - `free_groupoid V`: a type synonym for `V`. - `free_groupoid_groupoid`: the `groupoid` instance on `free_groupoid V`. - `lift`: the lifting of a prefunctor from `V` to `V'` where `V'` is a groupoid, to a functor. `free_groupoid V β₯€ V'`. - `lift_spec` and `lift_unique`: the proofs that, respectively, `lift` indeed is a lifting and is the unique one. ## Implementation notes The free groupoid is first defined by symmetrifying the quiver, taking the induced path category and finally quotienting by the reducibility relation. -/ open set classical function local attribute [instance] prop_decidable namespace category_theory namespace groupoid namespace free universes u v u' v' u'' v'' variables {V : Type u} [quiver.{v+1} V] /-- Shorthand for the "forward" arrow corresponding to `f` in `paths $ symmetrify V` -/ abbreviation quiver.hom.to_pos_path {X Y : V} (f : X ⟢ Y) : ((category_theory.paths.category_paths $ quiver.symmetrify V).hom X Y) := f.to_pos.to_path /-- Shorthand for the "forward" arrow corresponding to `f` in `paths $ symmetrify V` -/ abbreviation quiver.hom.to_neg_path {X Y : V} (f : X ⟢ Y) : ((category_theory.paths.category_paths $ quiver.symmetrify V).hom Y X) := f.to_neg.to_path /-- The "reduction" relation -/ inductive red_step : hom_rel (paths (quiver.symmetrify V)) | step (X Z : quiver.symmetrify V) (f : X ⟢ Z) : red_step (πŸ™ X) (f.to_path ≫ (quiver.reverse f).to_path) /-- The underlying vertices of the free groupoid -/ def _root_.category_theory.free_groupoid (V) [Q : quiver V] := quotient (@red_step V Q) instance {V} [Q : quiver V] [h : nonempty V] : nonempty (free_groupoid V) := ⟨⟨h.some⟩⟩ lemma congr_reverse {X Y : paths $ quiver.symmetrify V} (p q : X ⟢ Y) : quotient.comp_closure red_step p q β†’ quotient.comp_closure red_step (p.reverse) (q.reverse) := begin rintro ⟨XW, pp, qq, WY, _, Z, f⟩, have : quotient.comp_closure red_step (WY.reverse ≫ πŸ™ _ ≫ XW.reverse) (WY.reverse ≫ (f.to_path ≫ (quiver.reverse f).to_path) ≫ XW.reverse), { apply quotient.comp_closure.intro, apply red_step.step, }, simpa only [category_struct.comp, category_struct.id, quiver.path.reverse, quiver.path.nil_comp, quiver.path.reverse_comp, quiver.reverse_reverse, quiver.path.reverse_to_path, quiver.path.comp_assoc] using this, end lemma congr_comp_reverse {X Y : paths $ quiver.symmetrify V} (p : X ⟢ Y) : quot.mk (@quotient.comp_closure _ _ red_step _ _) (p ≫ p.reverse) = quot.mk (@quotient.comp_closure _ _ red_step _ _) (πŸ™ X) := begin apply quot.eqv_gen_sound, induction p with _ _ q f ih, { apply eqv_gen.refl, }, { simp only [quiver.path.reverse], fapply eqv_gen.trans, { exact q ≫ q.reverse, }, { apply eqv_gen.symm, apply eqv_gen.rel, have : quotient.comp_closure red_step (q ≫ (πŸ™ _) ≫ q.reverse) (q ≫ (f.to_path ≫ (quiver.reverse f).to_path) ≫ q.reverse), by { apply quotient.comp_closure.intro, apply red_step.step, }, have that : q.cons f = q.comp f.to_path, by refl, rw that, simp only [category.assoc, category.id_comp] at this ⊒, simp only [category_struct.comp, quiver.path.comp_assoc] at this ⊒, exact this, }, { exact ih }, }, end lemma congr_reverse_comp {X Y : paths $ quiver.symmetrify V} (p : X ⟢ Y) : quot.mk (@quotient.comp_closure _ _ red_step _ _) (p.reverse ≫ p) = quot.mk (@quotient.comp_closure _ _ red_step _ _) (πŸ™ Y) := begin nth_rewrite 1 ←quiver.path.reverse_reverse p, apply congr_comp_reverse, end instance : category (free_groupoid V) := quotient.category red_step /-- The inverse of an arrow in the free groupoid -/ def quot_inv {X Y : free_groupoid V} (f : X ⟢ Y) : Y ⟢ X := quot.lift_on f (Ξ» pp, quot.mk _ $ pp.reverse) (Ξ» pp qq con, quot.sound $ congr_reverse pp qq con) instance : groupoid (free_groupoid V) := { inv := Ξ» X Y f, quot_inv f, inv_comp' := Ξ» X Y p, quot.induction_on p $ Ξ» pp, congr_reverse_comp pp, comp_inv' := Ξ» X Y p, quot.induction_on p $ Ξ» pp, congr_comp_reverse pp } /-- The inclusion of the quiver on `V` to the underlying quiver on `free_groupoid V`-/ def of (V) [quiver V] : V β₯€q (free_groupoid V) := { obj := Ξ» X, ⟨X⟩, map := Ξ» X Y f, quot.mk _ f.to_pos_path } lemma of_eq : of V = (quiver.symmetrify.of β‹™q paths.of).comp (quotient.functor $ @red_step V _).to_prefunctor := begin apply prefunctor.ext, rotate, { rintro X, refl, }, { rintro X Y f, refl, } end section universal_property variables {V' : Type u'} [groupoid V'] (Ο† : V β₯€q V') /-- The lift of a prefunctor to a groupoid, to a functor from `free_groupoid V` -/ def lift (Ο† : V β₯€q V') : free_groupoid V β₯€ V' := quotient.lift _ (paths.lift $ quiver.symmetrify.lift Ο†) (by { rintros _ _ _ _ ⟨X,Y,f⟩, simp only [quiver.symmetrify.lift_reverse, paths.lift_nil, quiver.path.comp_nil, paths.lift_cons, paths.lift_to_path], symmetry, apply groupoid.comp_inv, }) lemma lift_spec (Ο† : V β₯€q V') : of V β‹™q (lift Ο†).to_prefunctor = Ο† := begin rw [of_eq, prefunctor.comp_assoc, prefunctor.comp_assoc, functor.to_prefunctor_comp], dsimp [lift], rw [quotient.lift_spec, paths.lift_spec, quiver.symmetrify.lift_spec], end lemma lift_unique (Ο† : V β₯€q V') (Ξ¦ : free_groupoid V β₯€ V') (hΞ¦ : of V β‹™q Ξ¦.to_prefunctor = Ο†) : Ξ¦ = lift Ο† := begin apply quotient.lift_unique, apply paths.lift_unique, fapply @quiver.symmetrify.lift_unique _ _ _ _ _ _ _ _ _, { rw ←functor.to_prefunctor_comp, exact hΞ¦, }, { constructor, rintros X Y f, simp only [←functor.to_prefunctor_comp,prefunctor.comp_map, paths.of_map, inv_eq_inv], change Ξ¦.map (inv ((quotient.functor red_step).to_prefunctor.map f.to_path)) = inv (Ξ¦.map ((quotient.functor red_step).to_prefunctor.map f.to_path)), have := functor.map_inv Ξ¦ ((quotient.functor red_step).to_prefunctor.map f.to_path), convert this; simp only [inv_eq_inv], }, end end universal_property section functoriality variables {V' : Type u'} [quiver.{v'+1} V'] {V'' : Type u''} [quiver.{v''+1} V''] /-- The functor of free groupoid induced by a prefunctor of quivers -/ def _root_.category_theory.free_groupoid_functor (Ο† : V β₯€q V') : free_groupoid V β₯€ free_groupoid V' := lift (Ο† β‹™q of V') lemma free_groupoid_functor_id : free_groupoid_functor (prefunctor.id V) = functor.id (free_groupoid V) := begin dsimp only [free_groupoid_functor], symmetry, apply lift_unique, refl, end lemma free_groupoid_functor_comp (Ο† : V β₯€q V') (Ο†' : V' β₯€q V'') : free_groupoid_functor (Ο† β‹™q Ο†') = free_groupoid_functor Ο† β‹™ free_groupoid_functor Ο†' := begin dsimp only [free_groupoid_functor], symmetry, apply lift_unique, refl, end end functoriality end free end groupoid end category_theory
d5e7bd8e85b552a619b84caf9a96fe51c90e21d7
6214e13b31733dc9aeb4833db6a6466005763162
/src/others.lean
cf938e414b1edcf7f87ee3237ebc45c7c1fe2e99
[]
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
5,087
lean
-- other minor lemmas related to verification but not included in the other files import .definitions2 lemma unchanged_of_apply_termctx_without_hole {t tt: term}: t.to_termctx tt = t := begin induction t with v y unop t₁ ih₁ binop tβ‚‚ t₃ ihβ‚‚ ih₃ tβ‚„ tβ‚… ihβ‚„ ihβ‚…, show (termctx.apply (term.to_termctx (term.value v)) tt = term.value v), by begin unfold term.to_termctx, unfold termctx.apply end, show (termctx.apply (term.to_termctx (term.var y)) tt = term.var y), by begin unfold term.to_termctx, unfold termctx.apply end, show (termctx.apply (term.to_termctx (term.unop unop t₁)) tt = term.unop unop t₁), by begin unfold term.to_termctx, unfold termctx.apply, congr, from ih₁ end, show (termctx.apply (term.to_termctx (term.binop binop tβ‚‚ t₃)) tt = term.binop binop tβ‚‚ t₃), by begin unfold term.to_termctx, unfold termctx.apply, congr, from ihβ‚‚, from ih₃ end, show (termctx.apply (term.to_termctx (term.app tβ‚„ tβ‚…)) tt = term.app tβ‚„ tβ‚…), by begin unfold term.to_termctx, unfold termctx.apply, congr, from ihβ‚„, from ihβ‚… end end lemma unchanged_of_apply_propctx_without_hole {P: prop} {t: term}: P.to_propctx t = P := begin change (propctx.apply (prop.to_propctx P) t = P), induction P, case prop.term t₁ { unfold prop.to_propctx, unfold propctx.apply, congr, from unchanged_of_apply_termctx_without_hole }, case prop.not P₁ ih { unfold prop.to_propctx, unfold propctx.apply, congr, from ih }, case prop.and P₁ Pβ‚‚ ih₁ ihβ‚‚ { unfold prop.to_propctx, change (propctx.apply (propctx.and (prop.to_propctx P₁) (prop.to_propctx Pβ‚‚)) t = prop.and P₁ Pβ‚‚), unfold propctx.apply, congr, from ih₁, from ihβ‚‚ }, case prop.or P₁ Pβ‚‚ ih₁ ihβ‚‚ { unfold prop.to_propctx, change (propctx.apply (propctx.or (prop.to_propctx P₁) (prop.to_propctx Pβ‚‚)) t = prop.or P₁ Pβ‚‚), unfold propctx.apply, congr, from ih₁, from ihβ‚‚ }, case prop.pre t₁ tβ‚‚ { unfold prop.to_propctx, unfold propctx.apply, congr, from unchanged_of_apply_termctx_without_hole, from unchanged_of_apply_termctx_without_hole }, case prop.pre₁ op t₁ { unfold prop.to_propctx, unfold propctx.apply, congr, from unchanged_of_apply_termctx_without_hole }, case prop.preβ‚‚ op t₁ tβ‚‚ { unfold prop.to_propctx, unfold propctx.apply, congr, from unchanged_of_apply_termctx_without_hole, from unchanged_of_apply_termctx_without_hole }, case prop.post t₁ tβ‚‚ { unfold prop.to_propctx, unfold propctx.apply, congr, from unchanged_of_apply_termctx_without_hole, from unchanged_of_apply_termctx_without_hole }, case prop.call t₁ tβ‚‚ { unfold prop.to_propctx, unfold propctx.apply, congr, from unchanged_of_apply_termctx_without_hole }, case prop.forallc y P₁ ih { unfold prop.to_propctx, unfold propctx.apply, congr, from ih }, case prop.exis y P₁ ih { unfold prop.to_propctx, unfold propctx.apply, congr, from ih } end lemma vc.term.inj.inv {t₁ tβ‚‚: term}: (t₁ = tβ‚‚) β†’ (vc.term t₁ = vc.term tβ‚‚) := begin assume h1, congr, from h1 end lemma vc.not.inj.inv {P Q: vc}: (P = Q) β†’ (vc.not P = vc.not Q) := begin assume h1, congr, from h1 end lemma vc.and.inj.inv {P₁ Pβ‚‚ P₃ Pβ‚„: vc}: (P₁ = Pβ‚‚) β†’ (P₃ = Pβ‚„) β†’ (vc.and P₁ P₃ = vc.and Pβ‚‚ Pβ‚„) := begin assume h1, assume h2, congr, from h1, from h2 end lemma vc.or.inj.inv {P₁ Pβ‚‚ P₃ Pβ‚„: vc}: (P₁ = Pβ‚‚) β†’ (P₃ = Pβ‚„) β†’ (vc.or P₁ P₃ = vc.or Pβ‚‚ Pβ‚„) := begin assume h1, assume h2, congr, from h1, from h2 end lemma vc.pre.inj.inv {t₁ tβ‚‚ t₃ tβ‚„: term}: (t₁ = tβ‚‚) β†’ (t₃ = tβ‚„) β†’ (vc.pre t₁ t₃ = vc.pre tβ‚‚ tβ‚„) := begin assume h1, assume h2, congr, from h1, from h2 end lemma vc.pre₁.inj.inv {t₁ tβ‚‚: term} {op: unop}: (t₁ = tβ‚‚) β†’ (vc.pre₁ op t₁ = vc.pre₁ op tβ‚‚) := begin assume h1, congr, from h1 end lemma vc.preβ‚‚.inj.inv {t₁ tβ‚‚ t₃ tβ‚„: term} {op: binop}: (t₁ = tβ‚‚) β†’ (t₃ = tβ‚„) β†’ (vc.preβ‚‚ op t₁ t₃ = vc.preβ‚‚ op tβ‚‚ tβ‚„) := begin assume h1, assume h2, congr, from h1, from h2 end lemma vc.post.inj.inv {t₁ tβ‚‚ t₃ tβ‚„: term}: (t₁ = tβ‚‚) β†’ (t₃ = tβ‚„) β†’ (vc.post t₁ t₃ = vc.post tβ‚‚ tβ‚„) := begin assume h1, assume h2, congr, from h1, from h2 end lemma vc.univ.inj.inv {P Q: vc} {x: var}: (P = Q) β†’ (vc.univ x P = vc.univ x Q) := begin assume h1, congr, from h1 end
cf9b11a1bf12240d53c4660c3fc4a62c04d38fe5
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/products/basic.lean
a9f67a2b8244df3a035ba5bc440a3e53b7582008
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
5,457
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_theory.eq_to_hom namespace category_theory universes v₁ vβ‚‚ v₃ vβ‚„ u₁ uβ‚‚ u₃ uβ‚„ -- declare the `v`'s first; see `category_theory.category` for an explanation section variables (C : Type u₁) [category.{v₁} C] (D : Type uβ‚‚) [category.{vβ‚‚} D] /-- `prod C D` gives the cartesian product of two categories. -/ instance prod : category.{max v₁ vβ‚‚} (C Γ— D) := { hom := Ξ» X Y, ((X.1) ⟢ (Y.1)) Γ— ((X.2) ⟢ (Y.2)), id := Ξ» X, ⟨ πŸ™ (X.1), πŸ™ (X.2) ⟩, comp := Ξ» _ _ _ f g, (f.1 ≫ g.1, f.2 ≫ g.2) } -- rfl lemmas for category.prod @[simp] lemma prod_id (X : C) (Y : D) : πŸ™ (X, Y) = (πŸ™ X, πŸ™ Y) := rfl @[simp] lemma prod_comp {P Q R : C} {S T U : D} (f : (P, S) ⟢ (Q, T)) (g : (Q, T) ⟢ (R, U)) : f ≫ g = (f.1 ≫ g.1, f.2 ≫ g.2) := rfl @[simp] lemma prod_id_fst (X : prod C D) : prod.fst (πŸ™ X) = πŸ™ X.fst := rfl @[simp] lemma prod_id_snd (X : prod C D) : prod.snd (πŸ™ X) = πŸ™ X.snd := rfl @[simp] lemma prod_comp_fst {X Y Z : prod C D} (f : X ⟢ Y) (g : Y ⟢ Z) : (f ≫ g).1 = f.1 ≫ g.1 := rfl @[simp] lemma prod_comp_snd {X Y Z : prod C D} (f : X ⟢ Y) (g : Y ⟢ Z) : (f ≫ g).2 = f.2 ≫ g.2 := rfl end section variables (C : Type u₁) [category.{v₁} C] (D : Type u₁) [category.{v₁} D] /-- `prod.category.uniform C D` is an additional instance specialised so both factors have the same universe levels. This helps typeclass resolution. -/ instance uniform_prod : category (C Γ— D) := category_theory.prod C D end -- Next we define the natural functors into and out of product categories. For now this doesn't -- address the universal properties. namespace prod /-- `sectl C Z` is the functor `C β₯€ C Γ— D` given by `X ↦ (X, Z)`. -/ -- Here and below we specify explicitly the projections to generate `@[simp]` lemmas for, -- as the default behaviour of `@[simps]` will generate projections all the way down to components -- of pairs. @[simps obj map] def sectl (C : Type u₁) [category.{v₁} C] {D : Type uβ‚‚} [category.{vβ‚‚} D] (Z : D) : C β₯€ C Γ— D := { obj := Ξ» X, (X, Z), map := Ξ» X Y f, (f, πŸ™ Z) } /-- `sectr Z D` is the functor `D β₯€ C Γ— D` given by `Y ↦ (Z, Y)` . -/ @[simps obj map] def sectr {C : Type u₁} [category.{v₁} C] (Z : C) (D : Type uβ‚‚) [category.{vβ‚‚} D] : D β₯€ C Γ— D := { obj := Ξ» X, (Z, X), map := Ξ» X Y f, (πŸ™ Z, f) } variables (C : Type u₁) [category.{v₁} C] (D : Type uβ‚‚) [category.{vβ‚‚} D] /-- `fst` is the functor `(X, Y) ↦ X`. -/ @[simps obj map] def fst : C Γ— D β₯€ C := { obj := Ξ» X, X.1, map := Ξ» X Y f, f.1 } /-- `snd` is the functor `(X, Y) ↦ Y`. -/ @[simps obj map] def snd : C Γ— D β₯€ D := { obj := Ξ» X, X.2, map := Ξ» X Y f, f.2 } @[simps obj map] def swap : C Γ— D β₯€ D Γ— C := { obj := Ξ» X, (X.2, X.1), map := Ξ» _ _ f, (f.2, f.1) } @[simps hom_app inv_app] def symmetry : swap C D β‹™ swap D C β‰… 𝟭 (C Γ— D) := { hom := { app := Ξ» X, πŸ™ X }, inv := { app := Ξ» X, πŸ™ X } } def braiding : C Γ— D β‰Œ D Γ— C := equivalence.mk (swap C D) (swap D C) (nat_iso.of_components (Ξ» X, eq_to_iso (by simp)) (by tidy)) (nat_iso.of_components (Ξ» X, eq_to_iso (by simp)) (by tidy)) instance swap_is_equivalence : is_equivalence (swap C D) := (by apply_instance : is_equivalence (braiding C D).functor) end prod section variables (C : Type u₁) [category.{v₁} C] (D : Type uβ‚‚) [category.{vβ‚‚} D] @[simps] def evaluation : C β₯€ (C β₯€ D) β₯€ D := { obj := Ξ» X, { obj := Ξ» F, F.obj X, map := Ξ» F G Ξ±, Ξ±.app X, }, map := Ξ» X Y f, { app := Ξ» F, F.map f, naturality' := Ξ» F G Ξ±, eq.symm (Ξ±.naturality f) } } @[simps obj map] def evaluation_uncurried : C Γ— (C β₯€ D) β₯€ D := { obj := Ξ» p, p.2.obj p.1, map := Ξ» x y f, (x.2.map f.1) ≫ (f.2.app y.1), map_comp' := Ξ» X Y Z f g, begin cases g, cases f, cases Z, cases Y, cases X, simp only [prod_comp, nat_trans.comp_app, functor.map_comp, category.assoc], rw [←nat_trans.comp_app, nat_trans.naturality, nat_trans.comp_app, category.assoc, nat_trans.naturality], end } end variables {A : Type u₁} [category.{v₁} A] {B : Type uβ‚‚} [category.{vβ‚‚} B] {C : Type u₃} [category.{v₃} C] {D : Type uβ‚„} [category.{vβ‚„} D] namespace functor /-- The cartesian product of two functors. -/ @[simps obj map] def prod (F : A β₯€ B) (G : C β₯€ D) : A Γ— C β₯€ B Γ— D := { obj := Ξ» X, (F.obj X.1, G.obj X.2), map := Ξ» _ _ f, (F.map f.1, G.map f.2) } /- Because of limitations in Lean 3's handling of notations, we do not setup a notation `F Γ— G`. You can use `F.prod G` as a "poor man's infix", or just write `functor.prod F G`. -/ end functor namespace nat_trans /-- The cartesian product of two natural transformations. -/ @[simps app] def prod {F G : A β₯€ B} {H I : C β₯€ D} (Ξ± : F ⟢ G) (Ξ² : H ⟢ I) : F.prod H ⟢ G.prod I := { app := Ξ» X, (Ξ±.app X.1, Ξ².app X.2), naturality' := Ξ» X Y f, begin cases X, cases Y, simp only [functor.prod_map, prod.mk.inj_iff, prod_comp], split; rw naturality end } /- Again, it is inadvisable in Lean 3 to setup a notation `Ξ± Γ— Ξ²`; use instead `Ξ±.prod Ξ²` or `nat_trans.prod Ξ± Ξ²`. -/ end nat_trans end category_theory
363dd4397347ed36e70481efe6a53befcc93a938
3c9dc4ea6cc92e02634ef557110bde9eae393338
/src/Lean/Compiler/Util.lean
3bf806cf3ae1f32c447aebf5dffaa3d6210f96c2
[ "Apache-2.0" ]
permissive
shingtaklam1324/lean4
3d7efe0c8743a4e33d3c6f4adbe1300df2e71492
351285a2e8ad0cef37af05851cfabf31edfb5970
refs/heads/master
1,676,827,679,740
1,610,462,623,000
1,610,552,340,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,679
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.Environment namespace Lean.Compiler def neutralExpr : Expr := mkConst `_neutral def unreachableExpr : Expr := mkConst `_unreachable def objectType : Expr := mkConst `_obj def voidType : Expr := mkConst `_void def mkLcProof (pred : Expr) := mkApp (mkConst `lcProof []) pred namespace atMostOnce structure AtMostOnceData where found : Bool result : Bool def Visitor := AtMostOnceData β†’ AtMostOnceData @[inline] def seq (f g : Visitor) : Visitor := fun d => match f d with | ⟨found, false⟩ => ⟨found, false⟩ | other => g other instance : AndThen Visitor := ⟨seq⟩ @[inline] def skip : Visitor := id @[inline] def visitFVar (x y : Name) : Visitor | d@{result := false, ..} => d | {found := false, result := true} => {found := x == y, result := true} | {found := true, result := true} => {found := true, result := x != y} def visit (x : Name) : Expr β†’ Visitor | Expr.fvar y _ => visitFVar y x | Expr.app f a _ => visit x a >> visit x f | Expr.lam _ d b _ => visit x d >> visit x b | Expr.forallE _ d b _ => visit x d >> visit x b | Expr.letE _ t v b _ => visit x t >> visit x v >> visit x b | Expr.mdata _ e _ => visit x e | Expr.proj _ _ e _ => visit x e | _ => skip end atMostOnce open atMostOnce (visit) in /-- Return true iff the free variable with id `x` occurs at most once in `e` -/ @[export lean_at_most_once] def atMostOnce (e : Expr) (x : Name) : Bool := let {result := result, ..} := visit x e {found := false, result := true} result /- Helper functions for creating auxiliary names used in compiler passes. -/ @[export lean_mk_eager_lambda_lifting_name] def mkEagerLambdaLiftingName (n : Name) (idx : Nat) : Name := Name.mkStr n ("_elambda_" ++ toString idx) @[export lean_is_eager_lambda_lifting_name] def isEagerLambdaLiftingName : Name β†’ Bool | Name.str p s _ => "_elambda".isPrefixOf s || isEagerLambdaLiftingName p | Name.num p _ _ => isEagerLambdaLiftingName p | _ => false /-- Return the name of new definitions in the a given declaration. Here we consider only declarations we generate code for. We use this definition to implement `add_and_compile`. -/ @[export lean_get_decl_names_for_code_gen] private def getDeclNamesForCodeGen : Declaration β†’ List Name | Declaration.defnDecl { name := n, .. } => [n] | Declaration.mutualDefnDecl defs => defs.map fun d => d.name | Declaration.opaqueDecl { name := n, .. } => [n] | _ => [] def checkIsDefinition (env : Environment) (n : Name) : Except String Unit := match env.find? n with | (some (ConstantInfo.defnInfo _)) => Except.ok () | (some (ConstantInfo.opaqueInfo _)) => Except.ok () | none => Except.error s!"unknow declaration '{n}'" | _ => Except.error s!"declaration is not a definition '{n}'" /-- We generate auxiliary unsafe definitions for regular recursive definitions. The auxiliary unsafe definition has a clear runtime cost execution model. This function returns the auxiliary unsafe definition name for the given name. -/ @[export lean_mk_unsafe_rec_name] def mkUnsafeRecName (declName : Name) : Name := Name.mkStr declName "_unsafe_rec" /-- Return `some _` if the given name was created using `mkUnsafeRecName` -/ @[export lean_is_unsafe_rec_name] def isUnsafeRecName? : Name β†’ Option Name | Name.str n "_unsafe_rec" _ => some n | _ => none end Lean.Compiler
683148a6efa291577e0a71bc2397f08c82c0fef2
c055f4b7c29cf1aac2223bd8c1ac8d181a7c6447
/src/categories/products/default.lean
971296908647520a909095a369c9af7bb4c9b2a7
[ "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
6,751
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 ..functor_categories open categories open categories.functor open categories.natural_transformation open categories.functor_categories namespace categories.products universes u₁ v₁ uβ‚‚ vβ‚‚ u₃ v₃ uβ‚„ vβ‚„ -- For now we only allow products of categories at the same universe level, to make type inference a bit easier. This could be generalised, but we'd have to provide specific instances at various universe levels. section variable (C : Type u₁) variable [category.{u₁ v₁} C] variable (D : Type uβ‚‚) variable [category.{uβ‚‚ vβ‚‚} D] instance ProductCategory : category.{(max u₁ uβ‚‚) (max v₁ vβ‚‚)} (C Γ— D) := { Hom := Ξ» X Y, ((X.1) ⟢ (Y.1)) Γ— ((X.2) ⟢ (Y.2)), identity := Ξ» X, ⟨ πŸ™ (X.1), πŸ™ (X.2) ⟩, compose := Ξ» _ _ _ f g, (f.1 ≫ g.1, f.2 ≫ g.2), left_identity := begin -- `obviously'` says: intros, automatic_induction, dsimp, dsimp at *, simp end, right_identity := begin -- `obviously'` says: intros, automatic_induction, dsimp, dsimp at *, simp end, associativity := begin -- `obviously'` says: intros, automatic_induction, dsimp, dsimp at *, simp end } end instance ProductCategory' (C : Type u₁) [category.{u₁ v₁} C] (D : Type u₁) [category.{u₁ v₁} D] : category.{u₁ v₁} (C Γ— D) := products.ProductCategory C D section variable {C : Type u₁} variable [π’ž : category.{u₁ v₁} C] variable {D : Type uβ‚‚} variable [π’Ÿ : category.{uβ‚‚ vβ‚‚} D] include π’ž π’Ÿ @[simp] lemma ProductCategory.identity {X : C} {Y : D} : πŸ™ (X, Y) = (πŸ™ X, πŸ™ Y) := by refl @[simp] lemma ProductCategory.compose {P Q R : C} {S T U : D} (f : (P, S) ⟢ (Q, T)) (g : (Q, T) ⟢ (R, U)) : f ≫ g = (f.1 ≫ g.1, f.2 ≫ g.2) := by refl end definition RightInjectionAt (C : Type u₁) [category.{u₁ v₁} C] {D : Type u₁} [category.{u₁ v₁} D] (Z : D) : C ↝ (C Γ— D) := { onObjects := Ξ» X, (X, Z), onMorphisms := Ξ» X Y f, (f, πŸ™ Z), identities := begin -- `obviously'` says: intros, refl end, functoriality := begin -- `obviously'` says: intros, dsimp, simp end } definition LeftInjectionAt {C : Type u₁} [category.{u₁ v₁} C] (Z : C) (D : Type u₁) [category.{u₁ v₁} D] : D ↝ (C Γ— D) := { onObjects := Ξ» X, (Z, X), onMorphisms := Ξ» X Y f, (πŸ™ Z, f), identities := begin -- `obviously'` says: intros, refl end, functoriality := begin -- `obviously'` says: intros, dsimp, simp end } definition LeftProjection (C : Type u₁) [category.{u₁ v₁} C] (Z : C) (D : Type u₁) [category.{u₁ v₁} D] : (C Γ— D) ↝ C := { onObjects := Ξ» X, X.1, onMorphisms := Ξ» X Y f, f.1, identities := begin -- `obviously'` says: intros, refl end, functoriality := begin -- `obviously'` says: intros, refl end } definition RightProjection (C : Type u₁) [category.{u₁ v₁} C] (Z : C) (D : Type u₁) [category.{u₁ v₁} D] : (C Γ— D) ↝ D := { onObjects := Ξ» X, X.2, onMorphisms := Ξ» X Y f, f.2, identities := begin -- `obviously'` says: intros, refl end, functoriality := begin -- `obviously'` says: intros, refl end } section variables {A : Type u₁} [A_cat : category.{u₁ v₁} A] {B : Type uβ‚‚} [B_cat : category.{uβ‚‚ vβ‚‚} B] {C : Type u₃} [C_cat : category.{u₃ v₃} C] {D : Type uβ‚„} [D_cat : category.{uβ‚„ vβ‚„} D] include A_cat B_cat C_cat D_cat definition ProductFunctor (F : A ↝ B) (G : C ↝ D) : (A Γ— C) ↝ (B Γ— D) := { onObjects := Ξ» X, (F +> X.1, G +> X.2), onMorphisms := Ξ» _ _ f, (F &> f.1, G &> f.2), identities := begin -- `obviously'` says (something equivalent to): intros, cases X, dsimp, erw Functor.identities_lemma, erw Functor.identities_lemma, refl, end, functoriality := begin -- `obviously'` says (something equivalent to): intros, cases Z, cases Y, cases X, dsimp, cases f, cases g, dsimp, dsimp at *, erw Functor.functoriality_lemma, erw Functor.functoriality_lemma, refl end } notation F `Γ—` G := ProductFunctor F G @[simp,ematch] lemma ProductFunctor.onObjects (F : A ↝ B) (G : C ↝ D) (a : A) (c : C) : (F Γ— G) +> (a, c) = (F +> a, G +> c) := by refl @[simp,ematch] lemma ProductFunctor.onMorphisms (F : A ↝ B) (G : C ↝ D) {a a' : A} {c c' : C} (f : (a, c) ⟢ (a', c')) : (F Γ— G).onMorphisms f = (F &> f.1, G &> f.2) := by refl definition ProductNaturalTransformation {F G : A ↝ B} {H I : C ↝ D} (Ξ± : F ⟹ G) (Ξ² : H ⟹ I) : (F Γ— H) ⟹ (G Γ— I) := { components := Ξ» X, (Ξ±.components X.1, Ξ².components X.2), naturality := begin -- `obviously'` says: intros, cases f, cases Y, cases X, dsimp, dsimp at *, simp, dsimp, fsplit, erw [NaturalTransformation.naturality_lemma], erw [NaturalTransformation.naturality_lemma] end } notation Ξ± `Γ—` Ξ² := ProductNaturalTransformation Ξ± Ξ² end end categories.products
53f06edffee4b43ba7b573ba6e864adf48c26785
5a6ff5f8d173cbfe51967eb4c96837e3a791fe3d
/mm0-lean/mm0/set/post.lean
38dc7a6bbc8e59b966991be1121ed9449e00bda6
[ "CC0-1.0" ]
permissive
digama0/mm0
491ac09146708aa1bb775007bf3dbe339ffc0096
98496badaf6464e56ed7b4204e7d54b85667cb01
refs/heads/master
1,692,321,030,902
1,686,254,458,000
1,686,254,458,000
172,456,790
273
38
CC0-1.0
1,689,939,563,000
1,551,080,059,000
Rust
UTF-8
Lean
false
false
24,748
lean
import .set6 data.int.basic data.nat.prime data.set.finite namespace mm0 local notation x ` ≑ ` y := eq (↑x : Β«classΒ») ↑y local notation x ` ∈' `:50 y:50 := (x : Β«classΒ») ∈ (y : Β«classΒ») theorem welc' {x : setvar} {A : Β«classΒ»} : x ∈' A ↔ @has_mem.mem Set (set Set) _ x A := Class.mem_hom_left _ _ class rel_class (A : Β«classΒ») (Ξ± : out_param Type) := (aset : A ∈ V) (to : Ξ± β†’ Β«classΒ») (to_mem {} : βˆ€ a, to a ∈ A) (to_inj {} : function.injective to) (to_surj {} : βˆ€ x, x ∈ A β†’ βˆƒ a, x = to a) open rel_class theorem to_V {A Ξ±} [rel_class A Ξ±] (a) : to A a ∈ V := elexi (to_mem _) theorem to_inj_iff (A) {Ξ±} [rel_class A Ξ±] {a b} : to A a = to A b ↔ a = b := ⟨@to_inj _ _ _ _ _, congr_arg _⟩ theorem to_mem_iff (A) {Ξ±} [rel_class A Ξ±] {x} : x ∈ A ↔ βˆƒ a, x = to A a := ⟨to_surj _, by rintro ⟨a, rfl⟩; exact to_mem _⟩ @[elab_as_eliminator] theorem to_ind (A) {Ξ±} [rel_class A Ξ±] (P : Β«classΒ» β†’ Prop) (H : βˆ€ a, P (to A a)) : βˆ€ x, x ∈ A β†’ P x := Ξ» x hx, let ⟨a, e⟩ := to_surj x hx in e.symm β–Έ H _ theorem to_ind_ral (A) {Ξ±} [rel_class A Ξ±] (P : setvar β†’ wff) (H : βˆ€ a x, ↑x = to A a β†’ P x) : wral P (Ξ» _, A) := Ξ» x hx, let ⟨a, e⟩ := to_surj x hx in H a _ e theorem ral_iff (A) {Ξ±} [rel_class A Ξ±] (P : Β«classΒ» β†’ Prop) : wral (Ξ» x, P (cv x)) (Ξ» _, A) ↔ βˆ€ a, P (to A a) := ⟨λ H a, let ⟨x, e, hx⟩ := @to_mem A _ _ a in e β–Έ H _ (welc'.2 hx), Ξ» H, to_ind_ral _ _ (Ξ» a x e, by rw [cv, e]; exact H _)⟩ theorem rex_iff (A) {Ξ±} [rel_class A Ξ±] (P : Β«classΒ» β†’ Prop) : wrex (Ξ» x, P x) (Ξ» _, A) ↔ βˆƒ a, P (to A a) := by classical; exact dfrex2.trans ((notbii (ral_iff A (Ξ» x, Β¬ P x))).trans not_forall_not) def rel (A) {Ξ±} [rel_class A Ξ±] (x) (a : Ξ±) : Prop := x ∈ A ∧ to A a = x instance prod.rel_class (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] : rel_class (cxp A B) (Ξ± Γ— Ξ²) := { aset := xpex (aset _) (aset _), to := Ξ» p, cop (to A p.1) (to B p.2), to_mem := Ξ» ⟨a, b⟩, opelxp.2 ⟨to_mem _, to_mem _⟩, to_inj := Ξ» ⟨a, b⟩ ⟨c, d⟩ e, prod.mk.inj_iff.2 (((opth (to_V _) (to_V _)).1 e).imp (Ξ» h, to_inj h) (Ξ» h, to_inj h)), to_surj := Ξ» p h, begin rcases elxp.1 h with ⟨a, b, e, ha, hb⟩, cases to_surj _ ha with x hx, cases to_surj _ hb with y hy, refine ⟨⟨x, y⟩, e.trans _⟩, rw [hx, hy] end } @[simp] theorem to_xp_eq (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (a b) : to (cxp A B) (a, b) = cop (to A a) (to B b) := rfl def to_ab (A) {Ξ±} [rel_class A Ξ±] (P : Ξ± β†’ Prop) : Β«classΒ» := crab (Ξ» x, βˆƒ a, ↑x = to A a ∧ P a) (Ξ» _, A) theorem to_ab_ss (A) {Ξ±} [rel_class A Ξ±] (P : Ξ± β†’ Prop) : to_ab A P βŠ† A := ssrab2 theorem mem_to_ab (A) {Ξ±} [rel_class A Ξ±] (P : Ξ± β†’ Prop) {x} : x ∈ to_ab A P ↔ βˆƒ a, x = to A a ∧ P a := ⟨λ ⟨y, e, hy, h⟩, e β–Έ h, Ξ» ⟨a, e, h⟩, let ⟨y, e', _⟩ := @to_mem A _ _ a in ⟨y, e'.trans e.symm, e'.symm β–Έ to_mem a, a, e', h⟩⟩ theorem to_mem_to_ab (A) {Ξ±} [rel_class A Ξ±] (P : Ξ± β†’ Prop) {a} : to A a ∈ to_ab A P ↔ P a := (mem_to_ab _ _).trans ⟨λ ⟨a, e, h⟩, to_inj e.symm β–Έ h, Ξ» h, ⟨_, rfl, h⟩⟩ def to_opab (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (P : Ξ± β†’ Ξ² β†’ Prop) : Β«classΒ» := to_ab (cxp A B) (Ξ» p, P p.1 p.2) theorem to_opab_ss (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (P : Ξ± β†’ Ξ² β†’ Prop) : to_opab A B P βŠ† cxp A B := to_ab_ss _ _ theorem to_opab_rel (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (P : Ξ± β†’ Ξ² β†’ Prop) : wrel (to_opab A B P) := relss (to_opab_ss _ _ _) relxp theorem mem_to_opab (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (P : Ξ± β†’ Ξ² β†’ Prop) {x} : x ∈ to_opab A B P ↔ βˆƒ a b, x = cop (to A a) (to B b) ∧ P a b := (mem_to_ab _ _).trans $ by simp [eq_comm] theorem to_br_to_opab (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (P : Ξ± β†’ Ξ² β†’ Prop) {a b} : wbr (to A a) (to B b) (to_opab A B P) ↔ P a b := @to_mem_to_ab (cxp A B) _ _ _ (a, b) def to_mpt (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (f : Ξ± β†’ Ξ²) : Β«classΒ» := to_opab A B (Ξ» a b, f a = b) theorem to_mpt_fn (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (f : Ξ± β†’ Ξ²) : wfn (to_mpt A B f) A := begin refine ⟨dffun4.2 ⟨to_opab_rel _ _ _, _⟩, eqssi (sstri (dmss (to_opab_ss _ _ _)) dmxpss) _⟩, { rintro x y z ⟨h₁, hβ‚‚βŸ©, rcases (mem_to_opab _ _ _).1 h₁ with ⟨a, _, e₁, rfl⟩, rcases (mem_to_opab _ _ _).1 hβ‚‚ with ⟨b, _, eβ‚‚, rfl⟩, cases (opth (vex _) (vex _)).1 e₁ with l₁ r₁, cases (opth (vex _) (vex _)).1 eβ‚‚ with lβ‚‚ rβ‚‚, cases to_inj (l₁.symm.trans lβ‚‚), exact r₁.trans rβ‚‚.symm }, { refine Ξ» x h, welc'.1 $ to_ind A _ (Ξ» a, _) x (welc'.2 h), rcases to_mem (f a) with ⟨b, e, h⟩, refine breldm (to_V _) (vex b) _, rw [cv, e], exact (to_br_to_opab _ _ _).2 rfl } end @[simp] theorem to_mpt_fv (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (f : Ξ± β†’ Ξ²) (a) : cfv (to A a) (to_mpt A B f) = to B (f a) := (fnbrfvb ⟨to_mpt_fn _ _ _, to_mem _⟩).2 $ (to_br_to_opab _ _ _).2 rfl theorem to_mpt_f (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] (f : Ξ± β†’ Ξ²) : wf A B (to_mpt A B f) := ffnfv.2 ⟨to_mpt_fn _ _ _, to_ind_ral A _ $ Ξ» a x e, by rw [cv, e, to_mpt_fv A B f a]; apply to_mem⟩ def carrow (A B) := co B A cmap local infix ` cβ†’ `:25 := carrow theorem map.rel_class (A B) {Ξ± Ξ²} [rel_class A Ξ±] [rel_class B Ξ²] : rel_class (A cβ†’ B) (Ξ± β†’ Ξ²) := { aset := ovex, to := to_mpt A B, to_mem := Ξ» f, (elmap (aset _) (aset _)).2 (to_mpt_f _ _ f), to_inj := Ξ» f g e, funext $ Ξ» a, begin have := to_mpt_fv A B g a, rw [← e, to_mpt_fv A B f a] at this, exact to_inj this end, to_surj := Ξ» p hp, begin have fp := (elmap (aset _) (aset _)).1 hp, have := Ξ» a, to_surj (cfv (to A a) p) (ffvelrni fp (to_mem a)), choose f hf using this, use f, refine (eqfnfv ⟨ffn fp, to_mpt_fn _ _ _⟩).2 (to_ind_ral _ _ _), intros a x e, rw [cv, e, to_mpt_fv A B f a], apply hf end } instance : has_zero Β«classΒ» := ⟨cc0⟩ instance : has_one Β«classΒ» := ⟨c1⟩ instance : has_add Β«classΒ» := ⟨λ x y, co x y caddc⟩ instance : has_mul Β«classΒ» := ⟨λ x y, co x y cmul⟩ instance : has_neg Β«classΒ» := ⟨cneg⟩ instance : has_sub Β«classΒ» := ⟨λ x y, co x y cmin⟩ instance : has_div Β«classΒ» := ⟨λ x y, co x y cdiv⟩ instance : has_dvd Β«classΒ» := ⟨λ x y, wbr x y cdivides⟩ instance : has_lt Β«classΒ» := ⟨λ x y, wbr x y clt⟩ instance : has_le Β«classΒ» := ⟨λ x y, wbr x y cle⟩ instance : has_equiv Β«classΒ» := ⟨λ x y, wbr x y cen⟩ @[simp] theorem c0_eq : cc0 = 0 := rfl @[simp] theorem c1_eq : c1 = 1 := rfl @[simp] theorem add_eq (x y) : co x y caddc = x + y := rfl @[simp] theorem mul_eq (x y) : co x y cmul = x * y := rfl @[simp] theorem neg_eq (x) : cneg x = -x := rfl @[simp] theorem sub_eq (x y) : co x y cmin = x - y := rfl @[simp] theorem div_eq (x y) : co x y cdiv = x / y := rfl @[simp] theorem dvd_eq (x y) : wbr x y cdivides = (x ∣ y) := rfl @[simp] theorem lt_eq (x y) : wbr x y clt = (x < y) := rfl @[simp] theorem le_eq (x y) : wbr x y cle = (x ≀ y) := rfl @[simp] theorem en_eq (x y) : wbr x y cen = (x β‰ˆ y) := rfl local notation `cβ„•β‚€` := cn0 local notation `cβ„€` := cz local notation `cℝ` := cr local notation `cβ„‚` := cc instance : has_coe_to_sort Β«classΒ» := ⟨_, Ξ» A, {x // x ∈ A}⟩ def semiring_cn (A : Β«classΒ») (ss : A βŠ† cβ„‚) (Β«0clΒ» : (0 : Β«classΒ») ∈ A) (Β«1clΒ» : (1 : Β«classΒ») ∈ A) (addcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x + y ∈ A) (mulcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x * y ∈ A) : comm_semiring A := have h : βˆ€ x : A, x.1 ∈ cβ„‚ := Ξ» x, sselii ss x.2, { add := Ξ» x y, ⟨x.1 + y.1, addcl _ _ x.2 y.2⟩, add_assoc := Ξ» x y z, subtype.eq (addassi (h x) (h y) (h z)), mul := Ξ» x y, ⟨x.1 * y.1, mulcl _ _ x.2 y.2⟩, mul_assoc := Ξ» x y z, subtype.eq (mulassi (h x) (h y) (h z)), zero := ⟨0, Β«0cl»⟩, zero_add := Ξ» x, subtype.eq (addid2 (h x)), add_zero := Ξ» x, subtype.eq (addid1 (h x)), add_comm := Ξ» x y, subtype.eq (addcomi (h x) (h y)), mul := Ξ» x y, ⟨x.1 * y.1, mulcl _ _ x.2 y.2⟩, mul_assoc := Ξ» x y z, subtype.eq (mulassi (h x) (h y) (h z)), one := ⟨1, Β«1cl»⟩, one_mul := Ξ» x, subtype.eq (mulid2 (h x)), mul_one := Ξ» x, subtype.eq (mulid1 (h x)), mul_comm := Ξ» x y, subtype.eq (mulcomi (h x) (h y)), left_distrib := Ξ» x y z, subtype.eq (adddii (h x) (h y) (h z)), right_distrib := Ξ» x y z, subtype.eq (adddiri (h x) (h y) (h z)), zero_mul := Ξ» x, subtype.eq (mul02 (h x)), mul_zero := Ξ» x, subtype.eq (mul01 (h x)) } def linear_order_xr (A : Β«classΒ») (ss : A βŠ† cxr) : linear_order A := have h : βˆ€ x:A, x.1 ∈ cxr := Ξ» x, sselii ss x.2, { le := Ξ» x y, x.1 ≀ y.1, lt := Ξ» x y, x.1 < y.1, le_refl := Ξ» x, xrleid (h x), le_trans := Ξ» x y z h1 h2, xrletr ⟨h x, h y, h z⟩ ⟨h1, h2⟩, le_antisymm := Ξ» x y h1 h2, subtype.eq ((xrletri3 ⟨h x, h y⟩).2 ⟨h1, h2⟩), le_total := Ξ» x y, xrletri ⟨h x, h y⟩, lt_iff_le_not_le := Ξ» x y, ⟨ Ξ» h1, ⟨xrltle ⟨h x, h y⟩ h1, (xrltnle ⟨h x, h y⟩).1 h1⟩, Ξ» ⟨h1, h2⟩, (xrltnle ⟨h x, h y⟩).2 h2⟩ } noncomputable def ordered_semiring_re (A : Β«classΒ») (ss : A βŠ† cℝ) (Β«0clΒ» : (0 : Β«classΒ») ∈ A) (Β«1clΒ» : (1 : Β«classΒ») ∈ A) (addcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x + y ∈ A) (mulcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x * y ∈ A) : decidable_linear_ordered_semiring A := have h : βˆ€ x:A, x.1 ∈ cℝ := Ξ» x, sselii ss x.2, { add_left_cancel := Ξ» x y z e, subtype.eq ((readdcan ⟨h y, h z, h x⟩).1 (subtype.ext.1 e)), add_right_cancel := Ξ» x y z e, subtype.eq ((addcan2 ⟨recn (h x), recn (h z), recn (h y)⟩).1 (subtype.ext.1 e)), add_le_add_left := Ξ» x y h1 z, (leadd2 ⟨h x, h y, h z⟩).1 h1, le_of_add_le_add_left := Ξ» x y z h1, (leadd2 ⟨h y, h z, h x⟩).2 h1, mul_le_mul_of_nonneg_left := Ξ» x y z h1 h2, lemul2a ⟨⟨h x, h y, h z, h2⟩, h1⟩, mul_le_mul_of_nonneg_right := Ξ» x y z h1 h2, lemul1a ⟨⟨h x, h y, h z, h2⟩, h1⟩, mul_lt_mul_of_pos_left := Ξ» x y z h1 h2, (ltmul2 ⟨h x, h y, h z, h2⟩).1 h1, mul_lt_mul_of_pos_right := Ξ» x y z h1 h2, (ltmul1 ⟨h x, h y, h z, h2⟩).1 h1, zero_lt_one := Β«0lt1Β», decidable_le := by classical; apply_instance, ..linear_order_xr _ (sstri ss ressxr), ..semiring_cn _ (sstri ss ax_resscn) Β«0clΒ» Β«1clΒ» addcl mulcl } def ring_cn (A : Β«classΒ») (ss : A βŠ† cβ„‚) (Β«0clΒ» : (0 : Β«classΒ») ∈ A) (Β«1clΒ» : (1 : Β«classΒ») ∈ A) (addcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x + y ∈ A) (negcl : βˆ€ x, x ∈ A β†’ -x ∈ A) (mulcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x * y ∈ A) : comm_ring A := have h : βˆ€ x : A, x.1 ∈ cβ„‚ := Ξ» x, sselii ss x.2, { neg := Ξ» x, ⟨-x.1, negcl _ x.2⟩, add_left_neg := Ξ» x, subtype.eq $ (addcomi (mm0.negcl (h x)) (h x)).trans (negid (h x)), ..semiring_cn _ ss Β«0clΒ» Β«1clΒ» addcl mulcl } noncomputable def ordered_ring_re (A : Β«classΒ») (ss : A βŠ† cℝ) (Β«0clΒ» : (0 : Β«classΒ») ∈ A) (Β«1clΒ» : (1 : Β«classΒ») ∈ A) (addcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x + y ∈ A) (negcl : βˆ€ x, x ∈ A β†’ -x ∈ A) (mulcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x * y ∈ A) : decidable_linear_ordered_comm_ring A := have h : βˆ€ x : A, x.1 ∈ cℝ := Ξ» x, sselii ss x.2, { add_lt_add_left := Ξ» x y h1 z, (ltadd2 ⟨h x, h y, h z⟩).1 h1, zero_ne_one := mt subtype.ext.1 Β«0ne1Β», mul_nonneg := Ξ» x y h1 h2, mulge0 ⟨⟨h x, h1⟩, ⟨h y, h2⟩⟩, mul_pos := Ξ» x y h1 h2, mulgt0 ⟨⟨h x, h1⟩, ⟨h y, h2⟩⟩, ..ring_cn _ (sstri ss ax_resscn) Β«0clΒ» Β«1clΒ» addcl negcl mulcl, ..ordered_semiring_re _ ss Β«0clΒ» Β«1clΒ» addcl mulcl } noncomputable def field_cn (A : Β«classΒ») (ss : A βŠ† cβ„‚) (Β«0clΒ» : (0 : Β«classΒ») ∈ A) (Β«1clΒ» : (1 : Β«classΒ») ∈ A) (addcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x + y ∈ A) (negcl : βˆ€ x, x ∈ A β†’ -x ∈ A) (mulcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x * y ∈ A) (reccl : βˆ€ x:Β«classΒ», x ∈ A β†’ x β‰  0 β†’ 1 / x ∈ A) : discrete_field A := begin classical, letI := ring_cn _ ss Β«0clΒ» Β«1clΒ» addcl negcl mulcl, have h : βˆ€ {{x}}, x ∈ A β†’ x ∈ cβ„‚ := Ξ» x, sselii ss, let inv := Ξ» x : A, if h : x.1 = 0 then (0:A) else ⟨1 / x.1, reccl _ x.2 h⟩, have n0 : βˆ€ {x:A}, x β‰  0 β†’ x.1 β‰  0 := Ξ» x h, (mt subtype.eq h:_), have inveq : βˆ€ {x:A}, x β‰  0 β†’ (inv x).1 = 1 / x.1 := Ξ» x h, by simp [inv, n0 h], exact { inv := inv, zero_ne_one := mt subtype.ext.1 (necomi ax_1ne0), mul_inv_cancel := Ξ» x x0, subtype.eq (show x.1 * _ = 1, by rw [inveq x0]; exact recid ⟨h x.2, n0 x0⟩), inv_mul_cancel := Ξ» x x0, subtype.eq (show _ * x.1 = 1, by rw [inveq x0]; exact recid2 ⟨h x.2, n0 x0⟩), inv_zero := dif_pos rfl, has_decidable_eq := by apply_instance, ..β€Ήcomm_ring Aβ€Ί } end noncomputable def ordered_field_re (A : Β«classΒ») (ss : A βŠ† cℝ) (Β«0clΒ» : (0 : Β«classΒ») ∈ A) (Β«1clΒ» : (1 : Β«classΒ») ∈ A) (addcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x + y ∈ A) (negcl : βˆ€ x, x ∈ A β†’ -x ∈ A) (mulcl : βˆ€ x y, x ∈ A β†’ y ∈ A β†’ x * y ∈ A) (reccl : βˆ€ x:Β«classΒ», x ∈ A β†’ x β‰  0 β†’ 1 / x ∈ A) : discrete_linear_ordered_field A := { ..field_cn _ (sstri ss ax_resscn) Β«0clΒ» Β«1clΒ» addcl negcl mulcl reccl, ..ordered_ring_re _ ss Β«0clΒ» Β«1clΒ» addcl negcl mulcl } noncomputable instance nn0.ordered_semiring : decidable_linear_ordered_semiring cβ„•β‚€ := ordered_semiring_re _ nn0ssre Β«0nn0Β» Β«1nn0Β» (Ξ» x y, nn0addcli) (Ξ» x y, nn0mulcli) @[simp] theorem cn0_zero : (0 : cβ„•β‚€).1 = 0 := rfl @[simp] theorem cn0_one : (1 : cβ„•β‚€).1 = 1 := rfl @[simp] theorem cn0_add (x y : cβ„•β‚€) : (x + y).1 = x.1 + y.1 := rfl @[simp] theorem cn0_mul (x y : cβ„•β‚€) : (x * y).1 = x.1 * y.1 := rfl @[simp] theorem cn0_le {x y : cβ„•β‚€} : x.1 ≀ y.1 ↔ x ≀ y := iff.rfl @[simp] theorem cn0_lt {x y : cβ„•β‚€} : x.1 < y.1 ↔ x < y := iff.rfl noncomputable instance z.ordered_ring : decidable_linear_ordered_comm_ring cβ„€ := ordered_ring_re _ zssre Β«0zΒ» Β«1zΒ» (Ξ» x y h1 h2, zaddcl ⟨h1, h2⟩) (Ξ» x, znegcl) (Ξ» x y h1 h2, zmulcl ⟨h1, h2⟩) @[simp] theorem cz_zero : (0 : cβ„€).1 = 0 := rfl @[simp] theorem cz_one : (1 : cβ„€).1 = 1 := rfl @[simp] theorem cz_add (x y : cβ„€) : (x + y).1 = x.1 + y.1 := rfl @[simp] theorem cz_neg (x : cβ„€) : (-x).1 = -x.1 := rfl @[simp] theorem cz_sub (x y : cβ„€) : (x - y).1 = x.1 - y.1 := negsub ⟨zcn x.2, zcn y.2⟩ @[simp] theorem cz_mul (x y : cβ„€) : (x * y).1 = x.1 * y.1 := rfl @[simp] theorem cz_le {x y : cβ„€} : x.1 ≀ y.1 ↔ x ≀ y := iff.rfl @[simp] theorem cz_lt {x y : cβ„€} : x.1 < y.1 ↔ x < y := iff.rfl noncomputable instance re.ordered_field : discrete_linear_ordered_field cℝ := ordered_field_re _ ssid Β«0reΒ» Β«1reΒ» (Ξ» x y, readdcli) (Ξ» x, renegcl) (Ξ» x y, remulcli) (Ξ» x, rereccli) @[simp] theorem cr_zero : (0 : cℝ).1 = 0 := rfl @[simp] theorem cr_one : (1 : cℝ).1 = 1 := rfl @[simp] theorem cr_add (x y : cℝ) : (x + y).1 = x.1 + y.1 := rfl @[simp] theorem cr_neg (x : cℝ) : (-x).1 = -x.1 := rfl @[simp] theorem cr_sub (x y : cℝ) : (x - y).1 = x.1 - y.1 := negsub ⟨recn x.2, recn y.2⟩ @[simp] theorem cr_mul (x y : cℝ) : (x * y).1 = x.1 * y.1 := rfl @[simp] theorem cr_le {x y : cℝ} : x.1 ≀ y.1 ↔ x ≀ y := iff.rfl @[simp] theorem cr_lt {x y : cℝ} : x.1 < y.1 ↔ x < y := iff.rfl noncomputable instance cn.field : discrete_field cβ„‚ := field_cn _ ssid Β«0cnΒ» Β«ax_1cnΒ» (Ξ» x y, addcli) (Ξ» x, negcl) (Ξ» x y, mulcli) (Ξ» x, reccli) @[simp] theorem cc_zero : (0 : cβ„‚).1 = 0 := rfl @[simp] theorem cc_one : (1 : cβ„‚).1 = 1 := rfl @[simp] theorem cc_add (x y : cβ„‚) : (x + y).1 = x.1 + y.1 := rfl @[simp] theorem cc_neg (x : cβ„‚) : (-x).1 = -x.1 := rfl @[simp] theorem cc_sub (x y : cβ„‚) : (x - y).1 = x.1 - y.1 := negsub ⟨x.2, y.2⟩ @[simp] theorem cc_mul (x y : cβ„‚) : (x * y).1 = x.1 * y.1 := rfl theorem wb_congr (P : Β«classΒ» β†’ Prop) {x:setvar} {A : Β«classΒ»} : ↑x = A β†’ (P x ↔ P A) := Ξ» h, by rw h @[elab_as_eliminator] theorem nn0ind' (P : Β«classΒ» β†’ Prop) {n} (h : n ∈ cβ„•β‚€) (H0 : P 0) (H1 : βˆ€ x ∈ cβ„•β‚€, P x β†’ P (x + 1)) : P n := @nn0ind _ _ _ _ _ (Ξ» _, n) (Ξ» _ _, wb_congr P) (Ξ» _ _, wb_congr P) (Ξ» _ _, wb_congr P) (Ξ» _ _, wb_congr P) (Ξ» _, H0) (Ξ» x, H1 _) βˆ… h instance nn0.rel_class : rel_class cβ„•β‚€ β„• := { aset := nn0ex, to := Ξ» n, (n:cβ„•β‚€).1, to_mem := Ξ» n, (n:cβ„•β‚€).2, to_inj := Ξ» m n e, nat.cast_inj.1 (subtype.eq e), to_surj := Ξ» x h, begin refine nn0ind' _ h ⟨0, rfl⟩ _, rintro _ h ⟨n, rfl⟩, exact ⟨n+1, rfl⟩ end } @[simp] theorem to_cn0_val (x : β„•) : to cβ„•β‚€ x = (x : cβ„•β‚€).1 := rfl theorem to_cn0_mem {x : Β«classΒ»} : x ∈ cβ„•β‚€ ↔ βˆƒ n:β„•, x = (n : cβ„•β‚€).1 := to_mem_iff cβ„•β‚€ @[simp] theorem to_cn0_zero : ((0 : β„•) : cβ„•β‚€).1 = 0 := rfl @[simp] theorem to_cn0_one : ((1 : β„•) : cβ„•β‚€).1 = 1 := by simp @[simp] theorem to_cn0_add (x y : β„•) : ((x + y:β„•):cβ„•β‚€).1 = (x:cβ„•β‚€).1 + (y:cβ„•β‚€).1 := by simp @[simp] theorem to_cn0_mul (x y : β„•) : ((x * y:β„•):cβ„•β‚€).1 = (x:cβ„•β‚€).1 * (y:cβ„•β‚€).1 := by simp @[simp] theorem to_cn0_le {x y : β„•} : (x:cβ„•β‚€).1 ≀ (y:cβ„•β‚€).1 ↔ x ≀ y := by simp @[simp] theorem to_cn0_lt {x y : β„•} : (x:cβ„•β‚€).1 < (y:cβ„•β‚€).1 ↔ x < y := by simp theorem to_cn0_two : ((2 : β„•) : cβ„•β‚€).1 = c2 := oveq1 to_cn0_one @[simp] theorem to_cn0_eq {x y : β„•} : (x:cβ„•β‚€).1 = (y:cβ„•β‚€).1 ↔ x = y := to_inj_iff cβ„•β‚€ @[simp] theorem nat_cast_cz : βˆ€ n : β„•, (n : cβ„€).1 = (n : cβ„•β‚€).1 | 0 := rfl | (n+1) := oveq1 (nat_cast_cz n) @[simp] theorem nat_cast_cz' (n : β„•) : ((n : β„€) : cβ„€).1 = (n : cβ„•β‚€).1 := nat_cast_cz _ instance z.rel_class : rel_class cβ„€ β„€ := { aset := zex, to := Ξ» n, (n:cβ„€).1, to_mem := Ξ» n, (n:cβ„€).2, to_inj := Ξ» m n e, int.cast_inj.1 (subtype.eq e), to_surj := Ξ» x h, begin rcases elznn0.1 h with ⟨xre, h | h⟩, { cases to_surj _ h with n e, refine ⟨n, _⟩, exact e.trans (nat_cast_cz' n).symm }, { cases to_surj _ h with n e, use -n, rw [int.cast_neg n], show x = -((n:β„€):cβ„€).1, rw nat_cast_cz', exact (negneg (recn xre)).symm.trans (negeq e) } end } @[simp] theorem to_cz_val (x : β„€) : to cβ„€ x = (x : cβ„€).1 := rfl theorem to_cz_mem {x : Β«classΒ»} : x ∈ cβ„€ ↔ βˆƒ n:β„€, x = (n : cβ„€).1 := to_mem_iff cβ„€ @[simp] theorem to_cz_eq {x y : β„€} : (x:cβ„€).1 = (y:cβ„€).1 ↔ x = y := to_inj_iff cβ„€ @[simp] theorem to_cz_zero : ((0 : β„€) : cβ„€).1 = 0 := rfl @[simp] theorem to_cz_one : ((1 : β„€) : cβ„€).1 = 1 := by simp @[simp] theorem to_cz_add (x y : β„€) : ((x + y:β„€):cβ„€).1 = (x:cβ„€).1 + (y:cβ„€).1 := by simp @[simp] theorem to_cz_neg (x : β„€) : ((-x:β„€):cβ„€).1 = -(x:cβ„€).1 := by simp @[simp] theorem to_cz_sub (x y : β„€) : ((x - y:β„€):cβ„€).1 = (x:cβ„€).1 - (y:cβ„€).1 := by rw [int.cast_sub, cz_sub] @[simp] theorem to_cz_mul (x y : β„€) : ((x * y:β„€):cβ„€).1 = (x:cβ„€).1 * (y:cβ„€).1 := by simp @[simp] theorem to_cz_le {x y : β„€} : (x:cβ„€).1 ≀ (y:cβ„€).1 ↔ x ≀ y := by simp @[simp] theorem to_cz_lt {x y : β„€} : (x:cβ„€).1 < (y:cβ„€).1 ↔ x < y := by simp theorem to_cz_two : ((2 : β„€) : cβ„€).1 = c2 := oveq1 to_cz_one @[simp] theorem to_cz_dvd {x y : β„€} : (x:cβ„€).1 ∣ (y:cβ„€).1 ↔ x ∣ y := (divides ⟨(x:cβ„€).2, (y:cβ„€).2⟩).trans $ (rex_iff cβ„€ (Ξ» z, z * (x:cβ„€).1 = (y:cβ„€).1)).trans $ exists_congr $ Ξ» z, by rw [← to_inj_iff cβ„€, eq_comm, mul_comm]; simp @[simp] theorem to_cn0_dvd {x y : β„•} : (x:cβ„•β‚€).1 ∣ (y:cβ„•β‚€).1 ↔ x ∣ y := by rw [← int.coe_nat_dvd, ← to_cz_dvd, nat_cast_cz', nat_cast_cz'] def cz_gcd (a b : cβ„€) : cβ„•β‚€ := ⟨co a b cgcd, gcdcl ⟨a.2, b.2⟩⟩ theorem cz_gcd_eq (a b : β„€) : cz_gcd a b = int.gcd a b := begin refine subtype.eq (dvdseq ⟨⟨(_:cβ„•β‚€).2, (_:cβ„•β‚€).2⟩, ⟨_, _⟩⟩), { cases @to_surj cβ„•β‚€ _ _ _ (cz_gcd a b).2 with x hx, replace hx := subtype.eq hx, rw hx, cases gcddvds ⟨(a:cβ„€).2, (b:cβ„€).2⟩ with h1 h2, refine to_cn0_dvd.2 (nat.dvd_gcd _ _); rw [← int.coe_nat_dvd, int.dvd_nat_abs, ← to_cz_dvd, nat_cast_cz', ← hx], exacts [h1, h2] }, { rw ← nat_cast_cz', refine dvdsgcd ⟨(_:cβ„€).2, (_:cβ„€).2, (_:cβ„€).2⟩ ⟨_, _⟩; refine (@to_cz_dvd (int.gcd a b) _).2 (int.dvd_nat_abs.1 (int.coe_nat_dvd.2 _)); [apply nat.gcd_dvd_left, apply nat.gcd_dvd_right] } end theorem mem_cn {n : β„•} : (n:cβ„•β‚€).1 ∈ cn ↔ n β‰  0 := (baib (elnnne0) (_:cβ„•β‚€).2).trans $ not_congr $ @to_inj_iff cβ„•β‚€ _ _ n 0 theorem mem_cprime {p : β„•} : (p:cβ„•β‚€).1 ∈ cprime ↔ nat.prime p := begin have ge2 : (p:cβ„•β‚€).1 ∈ cfv c2 cuz ↔ 2 ≀ p, { refine (eluz ⟨«2zΒ», nn0z (_:cβ„•β‚€).2⟩).trans _, rw ← to_cn0_two, exact @nat.cast_le cβ„•β‚€ _ 2 p }, refine ⟨λ h, _, Ξ» h, _⟩, { refine ⟨ge2.1 (prmuz2 h), Ξ» m hm, _⟩, cases (dvdsprime ⟨h, mem_cn.2 _⟩).1 (to_cn0_dvd.2 hm) with e e, { exact or.inr (to_cn0_eq.1 e) }, { rw [c1_eq, ← cn0_one, ← nat.cast_one] at e, exact or.inl (to_cn0_eq.1 e) }, { rintro rfl, cases zero_dvd_iff.1 hm, exact Β«0nnnΒ» (prmnn h) } }, { refine isprm2.2 ⟨ge2.2 h.1, Ξ» z hz, _⟩, cases to_cn0_mem.1 (nnnn0 hz) with m hm, simp [hm], rw [← to_cn0_one, to_cn0_eq], exact h.2 _ } end theorem is_finite (A) {Ξ±} [rel_class A Ξ±] (s : set Ξ±) : set.finite s ↔ to_ab A (Ξ» a, a ∈ s) ∈ cfn := begin have ab0 : to_ab A (Ξ» (a : Ξ±), a ∈ (βˆ… : set Ξ±)) = βˆ…, { apply eq0.2, rintro x ⟨_, _, _, _, _, ⟨⟩⟩ }, have absuc : βˆ€ (a : Ξ±) (s : set Ξ±), to_ab A (Ξ» (a_1 : Ξ±), a_1 ∈ insert a s) = to_ab A (Ξ» (a : Ξ±), a ∈ s) βˆͺ csn (to A a), { refine Ξ» a s, eqriv (Ξ» x, _), refine pm5_21nii (sseli (to_ab_ss _ _)) (sseli (unssi (to_ab_ss _ _) (snssi (to_mem _)))) (Ξ» h, _), rcases to_surj _ h with ⟨b, e⟩, rw [e], simp [to_mem_to_ab], exact (elun.trans (or.comm.trans $ or_congr ((elsnc (to_V _)).trans (to_inj_iff _)) (to_mem_to_ab _ _))).symm }, split, { refine Ξ» h, set.finite.induction_on h _ (Ξ» a s hn h IH, _), { rw ab0, exact Β«0finΒ» }, { rw absuc, exact unfi ⟨IH, snfi⟩ } }, { let P := Ξ» x, x βŠ† A β†’ βˆƒ s:set Ξ±, set.finite s ∧ to_ab A (Ξ» (a : Ξ±), a ∈ s) = x, have := findcard2 (Ξ» _ _ _, wb_congr P) (Ξ» _ _ _, wb_congr P) (Ξ» _ _ _, wb_congr P) (Ξ» _ _ _, wb_congr P) (Ξ» _ _, (_ : P βˆ…)) (Ξ» x y _ IH, _) βˆ… βˆ…, { intro h, rcases this h (to_ab_ss A (Ξ» (a : Ξ±), a ∈ s)) with ⟨t, tf, e⟩, have : t = s := set.ext (Ξ» a, (to_mem_to_ab A _).symm.trans ((eleq2 e).trans (to_mem_to_ab A _))), exact this β–Έ tf }, { refine Ξ» _, βŸ¨βˆ…, set.finite_empty, ab0⟩ }, { intro h, cases unss.2 h with h1 h2, rcases IH h1 with ⟨s, sf, e⟩, cases to_surj _ ((snss (vex _)).2 h2) with a e', rw [e.symm, e'], exact ⟨_, set.finite_insert a sf, absuc _ _⟩ } } end theorem dirith_aux₁ {n a : Β«classΒ»} : n ∈ cn ∧ a ∈ cβ„€ ∧ co a n cgcd = 1 β†’ cab (Ξ» x, ↑x ∈ cprime ∧ n ∣ x - a) β‰ˆ cn := dirith theorem dirith_auxβ‚‚ {n : β„•} {a : β„€} (n0 : n β‰  0) (h : int.gcd a n = 1) : to_ab cβ„•β‚€ (Ξ» x, nat.prime x ∧ ↑n ∣ ↑x - a) β‰ˆ cn := begin refine eqbrtri (eqriv $ Ξ» x, _) (dirith_aux₁ ⟨mem_cn.2 n0, (a:cβ„€).2, _⟩), { refine (mem_to_ab _ _).trans (iff.trans _ (abid _).symm), dsimp, split, { rintro ⟨n, e, h₁, hβ‚‚βŸ©, simp [e], refine ⟨mem_cprime.2 h₁, _⟩, rwa [← nat_cast_cz', ← nat_cast_cz', ← to_cz_sub, to_cz_dvd] }, { rintro ⟨h₁, hβ‚‚βŸ©, cases to_cn0_mem.1 (nnnn0 (prmnn h₁)) with m hm, simp [hm] at h₁ hβ‚‚ ⊒, rw [← nat_cast_cz', ← nat_cast_cz', ← to_cz_sub, to_cz_dvd] at hβ‚‚, exact ⟨mem_cprime.1 h₁, hβ‚‚βŸ© } }, { rw [← nat_cast_cz'], refine congr_arg subtype.val (_ : cz_gcd a n = 1), rw [← int.cast_coe_nat, cz_gcd_eq, h, nat.cast_one] } end theorem dirith' {n : β„•} {a : β„€} (n0 : n β‰  0) (g1 : int.gcd a n = 1) : Β¬ set.finite {x | nat.prime x ∧ ↑n ∣ ↑x - a} := Ξ» h, ominf $ (enfi nnenom).1 $ (enfi (dirith_auxβ‚‚ n0 g1)).1 $ (is_finite cβ„•β‚€ _).1 h end mm0
5cd89f9ae6861246026b074fd20ed244624b010d
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/587.lean
8442145a2c72acdd64698f009e1873edd5366fa4
[ "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
300
lean
open quot setoid variables A B : Type₁ variable s : setoid A include s variable f : A β†’ B variable c : βˆ€ a₁ aβ‚‚, a₁ β‰ˆ aβ‚‚ β†’ f a₁ = f aβ‚‚ example (a : A) (g h : B β†’ B) : g = h β†’ g (quot.lift_on ⟦a⟧ f c) = h (f a) := begin intro gh, esimp, state, rewrite gh end
5d72b9a80b2c5092e1ba67baf3adb169820b5550
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/export.lean
ffbe4603232aece4243f8065efb379058e390f44
[ "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
132
lean
constants a b : nat namespace boo export nat (rec add) check a + b check nat.add end boo open boo check a + b check nat.rec
3128b1ad8765f20fbc996d61c95bb97da80329b8
94e33a31faa76775069b071adea97e86e218a8ee
/src/ring_theory/polynomial/scale_roots.lean
101465abaa35bccf468ec15ea398516548919c36
[ "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
5,176
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Devon Tuma -/ import ring_theory.non_zero_divisors import data.polynomial.algebra_map /-! # Scaling the roots of a polynomial This file defines `scale_roots p s` for a polynomial `p` in one variable and a ring element `s` to be the polynomial with root `r * s` for each root `r` of `p` and proves some basic results about it. -/ variables {A K R S : Type*} [comm_ring A] [is_domain A] [field K] [comm_ring R] [comm_ring S] variables {M : submonoid A} namespace polynomial open_locale big_operators polynomial /-- `scale_roots p s` is a polynomial with root `r * s` for each root `r` of `p`. -/ noncomputable def scale_roots (p : R[X]) (s : R) : R[X] := βˆ‘ i in p.support, monomial i (p.coeff i * s ^ (p.nat_degree - i)) @[simp] lemma coeff_scale_roots (p : R[X]) (s : R) (i : β„•) : (scale_roots p s).coeff i = coeff p i * s ^ (p.nat_degree - i) := by simp [scale_roots, coeff_monomial] {contextual := tt} lemma coeff_scale_roots_nat_degree (p : R[X]) (s : R) : (scale_roots p s).coeff p.nat_degree = p.leading_coeff := by rw [leading_coeff, coeff_scale_roots, tsub_self, pow_zero, mul_one] @[simp] lemma zero_scale_roots (s : R) : scale_roots 0 s = 0 := by { ext, simp } lemma scale_roots_ne_zero {p : R[X]} (hp : p β‰  0) (s : R) : scale_roots p s β‰  0 := begin intro h, have : p.coeff p.nat_degree β‰  0 := mt leading_coeff_eq_zero.mp hp, have : (scale_roots p s).coeff p.nat_degree = 0 := congr_fun (congr_arg (coeff : R[X] β†’ β„• β†’ R) h) p.nat_degree, rw [coeff_scale_roots_nat_degree] at this, contradiction end lemma support_scale_roots_le (p : R[X]) (s : R) : (scale_roots p s).support ≀ p.support := by { intro, simpa using left_ne_zero_of_mul } lemma support_scale_roots_eq (p : R[X]) {s : R} (hs : s ∈ non_zero_divisors R) : (scale_roots p s).support = p.support := le_antisymm (support_scale_roots_le p s) begin intro i, simp only [coeff_scale_roots, polynomial.mem_support_iff], intros p_ne_zero ps_zero, have := pow_mem hs (p.nat_degree - i) _ ps_zero, contradiction end @[simp] lemma degree_scale_roots (p : R[X]) {s : R} : degree (scale_roots p s) = degree p := begin haveI := classical.prop_decidable, by_cases hp : p = 0, { rw [hp, zero_scale_roots] }, have := scale_roots_ne_zero hp s, refine le_antisymm (finset.sup_mono (support_scale_roots_le p s)) (degree_le_degree _), rw coeff_scale_roots_nat_degree, intro h, have := leading_coeff_eq_zero.mp h, contradiction, end @[simp] lemma nat_degree_scale_roots (p : R[X]) (s : R) : nat_degree (scale_roots p s) = nat_degree p := by simp only [nat_degree, degree_scale_roots] lemma monic_scale_roots_iff {p : R[X]} (s : R) : monic (scale_roots p s) ↔ monic p := by simp only [monic, leading_coeff, nat_degree_scale_roots, coeff_scale_roots_nat_degree] lemma scale_roots_evalβ‚‚_eq_zero {p : S[X]} (f : S β†’+* R) {r : R} {s : S} (hr : evalβ‚‚ f r p = 0) : evalβ‚‚ f (f s * r) (scale_roots p s) = 0 := calc evalβ‚‚ f (f s * r) (scale_roots p s) = (scale_roots p s).support.sum (Ξ» i, f (coeff p i * s ^ (p.nat_degree - i)) * (f s * r) ^ i) : by simp [evalβ‚‚_eq_sum, sum_def] ... = p.support.sum (Ξ» i, f (coeff p i * s ^ (p.nat_degree - i)) * (f s * r) ^ i) : finset.sum_subset (support_scale_roots_le p s) (Ξ» i hi hi', let this : coeff p i * s ^ (p.nat_degree - i) = 0 := by simpa using hi' in by simp [this]) ... = p.support.sum (Ξ» (i : β„•), f (p.coeff i) * f s ^ (p.nat_degree - i + i) * r ^ i) : finset.sum_congr rfl (Ξ» i hi, by simp_rw [f.map_mul, f.map_pow, pow_add, mul_pow, mul_assoc]) ... = p.support.sum (Ξ» (i : β„•), f s ^ p.nat_degree * (f (p.coeff i) * r ^ i)) : finset.sum_congr rfl (Ξ» i hi, by { rw [mul_assoc, mul_left_comm, tsub_add_cancel_of_le], exact le_nat_degree_of_ne_zero (polynomial.mem_support_iff.mp hi) }) ... = f s ^ p.nat_degree * p.support.sum (Ξ» (i : β„•), (f (p.coeff i) * r ^ i)) : finset.mul_sum.symm ... = f s ^ p.nat_degree * evalβ‚‚ f r p : by { simp [evalβ‚‚_eq_sum, sum_def] } ... = 0 : by rw [hr, _root_.mul_zero] lemma scale_roots_aeval_eq_zero [algebra S R] {p : S[X]} {r : R} {s : S} (hr : aeval r p = 0) : aeval (algebra_map S R s * r) (scale_roots p s) = 0 := scale_roots_evalβ‚‚_eq_zero (algebra_map S R) hr lemma scale_roots_evalβ‚‚_eq_zero_of_evalβ‚‚_div_eq_zero {p : A[X]} {f : A β†’+* K} (hf : function.injective f) {r s : A} (hr : evalβ‚‚ f (f r / f s) p = 0) (hs : s ∈ non_zero_divisors A) : evalβ‚‚ f (f r) (scale_roots p s) = 0 := begin convert scale_roots_evalβ‚‚_eq_zero f hr, rw [←mul_div_assoc, mul_comm, mul_div_cancel], exact map_ne_zero_of_mem_non_zero_divisors _ hf hs end lemma scale_roots_aeval_eq_zero_of_aeval_div_eq_zero [algebra A K] (inj : function.injective (algebra_map A K)) {p : A[X]} {r s : A} (hr : aeval (algebra_map A K r / algebra_map A K s) p = 0) (hs : s ∈ non_zero_divisors A) : aeval (algebra_map A K r) (scale_roots p s) = 0 := scale_roots_evalβ‚‚_eq_zero_of_evalβ‚‚_div_eq_zero inj hr hs end polynomial
03ed60be71162ddf5229e2ee16b4cdb4d26622a2
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/default.lean
3ebbb27ebc4028a98ebe3f609d20dffd7d84b5f8
[ "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
1,221
lean
/- This file imports many useful tactics ("the kitchen sink"). You can use `import tactic` at the beginning of your file to get everything. (Although you may want to strip things down when you're polishing.) Because this file imports some complicated tactics, it has many transitive dependencies (which of course may not use `import tactic`, and must import selectively). As (non-exhaustive) examples, these includes things like: * algebra.group_power * algebra.order.ring * data.rat * data.nat.prime * data.list.perm * data.set.lattice * data.equiv.encodable.basic * order.complete_lattice -/ import tactic.basic -- ensure basic tactics are available import tactic.abel import tactic.ring_exp import tactic.noncomm_ring import tactic.linarith import tactic.omega import tactic.tfae import tactic.apply_fun import tactic.interval_cases import tactic.reassoc_axiom -- most likely useful only for category_theory import tactic.slice import tactic.subtype_instance import tactic.derive_fintype import tactic.group import tactic.cancel_denoms import tactic.zify import tactic.transport import tactic.unfold_cases import tactic.field_simp import tactic.linear_combination import tactic.polyrith import tactic.expand_exists
98dadff861cb7c74989e5ab606a3d02d1084007c
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/hex_numeral.lean
53b7249c53e0bce97a163a3bf18096485a2176fc
[ "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
110
lean
vm_eval (0xff : nat) vm_eval (0xffff : nat) vm_eval (0xaa : nat) vm_eval (0x10 : nat) vm_eval (0x10000 : nat)
7186759e48ddb97f46cd3106a1d3431c9a30bad1
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Meta/Tactic/Induction.lean
d1b6f7ccc094f03c66976254996c4afc32809e27
[ "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
11,794
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.RecursorInfo import Lean.Meta.SynthInstance import Lean.Meta.Tactic.Util import Lean.Meta.Tactic.Revert import Lean.Meta.Tactic.Intro import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.FVarSubst namespace Lean.Meta private partial def getTargetArity : Expr β†’ Nat | Expr.mdata _ b _ => getTargetArity b | Expr.forallE _ _ b _ => getTargetArity b + 1 | e => if e.isHeadBetaTarget then getTargetArity e.headBeta else 0 private def addRecParams (mvarId : MVarId) (majorTypeArgs : Array Expr) : List (Option Nat) β†’ Expr β†’ MetaM Expr | [], recursor => pure recursor | some pos :: rest, recursor => if h : pos < majorTypeArgs.size then addRecParams mvarId majorTypeArgs rest (mkApp recursor (majorTypeArgs.get ⟨pos, h⟩)) else throwTacticEx `induction mvarId "ill-formed recursor" | none :: rest, recursor => do let recursorType ← inferType recursor let recursorType ← whnfForall recursorType match recursorType with | Expr.forallE _ d _ _ => do let param ← try synthInstance d catch _ => throwTacticEx `induction mvarId "failed to generate type class instance parameter" addRecParams mvarId majorTypeArgs rest (mkApp recursor param) | _ => throwTacticEx `induction mvarId "ill-formed recursor" structure InductionSubgoal where mvarId : MVarId fields : Array Expr := #[] subst : FVarSubst := {} deriving Inhabited private def getTypeBody (mvarId : MVarId) (type : Expr) (x : Expr) : MetaM Expr := do let type ← whnfForall type match type with | Expr.forallE _ _ b _ => pure $ b.instantiate1 x | _ => throwTacticEx `induction mvarId "ill-formed recursor" structure AltVarNames where explicit : Bool := false -- true if `@` modifier was used varNames : List Name := [] deriving Inhabited private partial def finalize (mvarId : MVarId) (givenNames : Array AltVarNames) (recursorInfo : RecursorInfo) (reverted : Array FVarId) (major : Expr) (indices : Array Expr) (baseSubst : FVarSubst) (recursor : Expr) : MetaM (Array InductionSubgoal) := do let target ← getMVarType mvarId let initialArity := getTargetArity target let recursorType ← inferType recursor let numMinors := recursorInfo.produceMotive.length let rec loop (pos : Nat) (minorIdx : Nat) (recursor recursorType : Expr) (consumedMajor : Bool) (subgoals : Array InductionSubgoal) := do let recursorType ← whnfForall recursorType if recursorType.isForall && pos < recursorInfo.numArgs then if pos == recursorInfo.firstIndexPos then let (recursor, recursorType) ← indices.foldlM (init := (recursor, recursorType)) fun (recursor, recursorType) index => do let recursor := mkApp recursor index let recursorType ← getTypeBody mvarId recursorType index pure (recursor, recursorType) let recursor := mkApp recursor major let recursorType ← getTypeBody mvarId recursorType major loop (pos+1+indices.size) minorIdx recursor recursorType true subgoals else -- consume motive let tag ← getMVarTag mvarId if minorIdx β‰₯ numMinors then throwTacticEx `induction mvarId "ill-formed recursor" match recursorType with | Expr.forallE n d b c => let d := d.headBeta -- Remark is givenNames is not empty, then user provided explicit alternatives for each minor premise if c.binderInfo.isInstImplicit && givenNames.isEmpty then match (← synthInstance? d) with | some inst => let recursor := mkApp recursor inst let recursorType ← getTypeBody mvarId recursorType inst loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor subgoals | none => do -- Add newSubgoal if type class resolution failed let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n) let recursor := mkApp recursor mvar let recursorType ← getTypeBody mvarId recursorType mvar loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvar.mvarId! }) else let arity := getTargetArity d if arity < initialArity then throwTacticEx `induction mvarId "ill-formed recursor" let nparams := arity - initialArity -- number of fields due to minor premise let nextra := reverted.size - indices.size - 1 -- extra dependencies that have been reverted let minorGivenNames := if h : minorIdx < givenNames.size then givenNames.get ⟨minorIdx, h⟩ else {} let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n) let recursor := mkApp recursor mvar let recursorType ← getTypeBody mvarId recursorType mvar -- Try to clear major premise from new goal let mvarId' ← tryClear mvar.mvarId! major.fvarId! let (fields, mvarId') ← introN mvarId' nparams minorGivenNames.varNames (useNamesForExplicitOnly := !minorGivenNames.explicit) let (extra, mvarId') ← introNP mvarId' nextra let subst := reverted.size.fold (init := baseSubst) fun i (subst : FVarSubst) => if i < indices.size + 1 then subst else let revertedFVarId := reverted[i] let newFVarId := extra[i - indices.size - 1] subst.insert revertedFVarId (mkFVar newFVarId) let fields := fields.map mkFVar loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvarId', fields := fields, subst := subst }) | _ => unreachable! else unless consumedMajor do throwTacticEx `induction mvarId "ill-formed recursor" assignExprMVar mvarId recursor pure subgoals loop (recursorInfo.paramsPos.length + 1) 0 recursor recursorType false #[] private def throwUnexpectedMajorType {Ξ±} (mvarId : MVarId) (majorType : Expr) : MetaM Ξ± := throwTacticEx `induction mvarId m!"unexpected major premise type{indentExpr majorType}" def induction (mvarId : MVarId) (majorFVarId : FVarId) (recursorName : Name) (givenNames : Array AltVarNames := #[]) : MetaM (Array InductionSubgoal) := withMVarContext mvarId do trace[Meta.Tactic.induction] "initial\n{MessageData.ofGoal mvarId}" checkNotAssigned mvarId `induction let majorLocalDecl ← getLocalDecl majorFVarId let recursorInfo ← mkRecursorInfo recursorName let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type majorType.withApp fun _ majorTypeArgs => do recursorInfo.paramsPos.forM fun paramPos? => do match paramPos? with | none => pure () | some paramPos => if paramPos β‰₯ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}" let mctx ← getMCtx let indices ← recursorInfo.indicesPos.toArray.mapM fun idxPos => do if idxPos β‰₯ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}" let idx := majorTypeArgs.get! idxPos unless idx.isFVar do throwTacticEx `induction mvarId m!"major premise type index {idx} is not a variable{indentExpr majorType}" majorTypeArgs.size.forM fun i => do let arg := majorTypeArgs[i] if i != idxPos && arg == idx then throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs more than once{indentExpr majorType}" if i < idxPos && mctx.exprDependsOn arg idx.fvarId! then throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs in previous arguments{indentExpr majorType}" -- If arg is also and index and a variable occurring after `idx`, we need to make sure it doesn't depend on `idx`. -- Note that if `arg` is not a variable, we will fail anyway when we visit it. if i > idxPos && recursorInfo.indicesPos.contains i && arg.isFVar then let idxDecl ← getLocalDecl idx.fvarId! if mctx.localDeclDependsOn idxDecl arg.fvarId! then throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it depends on index occurring at position #{i+1}" pure idx let target ← getMVarType mvarId if !recursorInfo.depElim && mctx.exprDependsOn target majorFVarId then throwTacticEx `induction mvarId m!"recursor '{recursorName}' does not support dependent elimination, but conclusion depends on major premise" -- Revert indices and major premise preserving variable order let (reverted, mvarId) ← revert mvarId ((indices.map Expr.fvarId!).push majorFVarId) true -- Re-introduce indices and major let (indices', mvarId) ← introNP mvarId indices.size let (majorFVarId', mvarId) ← intro1P mvarId -- Create FVarSubst with indices let baseSubst := do let mut subst : FVarSubst := {} let mut i := 0 for index in indices do subst := subst.insert index.fvarId! (mkFVar indices'[i]) i := i + 1 pure subst trace[Meta.Tactic.induction] "after revert&intro\n{MessageData.ofGoal mvarId}" -- Update indices and major let indices := indices'.map mkFVar let majorFVarId := majorFVarId' let major := mkFVar majorFVarId withMVarContext mvarId do let target ← getMVarType mvarId let targetLevel ← getLevel target let targetLevel ← normalizeLevel targetLevel let majorLocalDecl ← getLocalDecl majorFVarId let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type majorType.withApp fun majorTypeFn majorTypeArgs => do match majorTypeFn with | Expr.const majorTypeFnName majorTypeFnLevels _ => do let majorTypeFnLevels := majorTypeFnLevels.toArray let (recursorLevels, foundTargetLevel) ← recursorInfo.univLevelPos.foldlM (init := (#[], false)) fun (recursorLevels, foundTargetLevel) (univPos : RecursorUnivLevelPos) => do match univPos with | RecursorUnivLevelPos.motive => pure (recursorLevels.push targetLevel, true) | RecursorUnivLevelPos.majorType idx => if idx β‰₯ majorTypeFnLevels.size then throwTacticEx `induction mvarId "ill-formed recursor" pure (recursorLevels.push (majorTypeFnLevels.get! idx), foundTargetLevel) if !foundTargetLevel && !targetLevel.isZero then throwTacticEx `induction mvarId m!"recursor '{recursorName}' can only eliminate into Prop" let recursor := mkConst recursorName recursorLevels.toList let recursor ← addRecParams mvarId majorTypeArgs recursorInfo.paramsPos recursor -- Compute motive let motive := target let motive ← if recursorInfo.depElim then mkLambdaFVars #[major] motive else pure motive let motive ← mkLambdaFVars indices motive let recursor := mkApp recursor motive finalize mvarId givenNames recursorInfo reverted major indices baseSubst recursor | _ => throwTacticEx `induction mvarId "major premise is not of the form (C ...)" builtin_initialize registerTraceClass `Meta.Tactic.induction end Lean.Meta
d55b447606dbbecba75035e8e10513c82d87f481
4950bf76e5ae40ba9f8491647d0b6f228ddce173
/src/ring_theory/roots_of_unity.lean
d213ba24b068cb95a68dd303426e5b669949144b
[ "Apache-2.0" ]
permissive
ntzwq/mathlib
ca50b21079b0a7c6781c34b62199a396dd00cee2
36eec1a98f22df82eaccd354a758ef8576af2a7f
refs/heads/master
1,675,193,391,478
1,607,822,996,000
1,607,822,996,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,234
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.nat.parity import data.polynomial.ring_division import group_theory.order_of_element import ring_theory.integral_domain import number_theory.divisors import data.zmod.basic import tactic.zify /-! # Roots of unity and primitive roots of unity We define roots of unity in the context of an arbitrary commutative monoid, as a subgroup of the group of units. We also define a predicate `is_primitive_root` on commutative monoids, expressing that an element is a primitive root of unity. ## Main definitions * `roots_of_unity n M`, for `n : β„•+` is the subgroup of the units of a commutative monoid `M` consisting of elements `x` that satisfy `x ^ n = 1`. * `is_primitive_root ΞΆ k`: an element `ΞΆ` is a primitive `k`-th root of unity if `ΞΆ ^ k = 1`, and if `l` satisfies `ΞΆ ^ l = 1` then `k ∣ l`. * `primitive_roots k R`: the finset of primitive `k`-th roots of unity in an integral domain `R`. ## Main results * `roots_of_unity.is_cyclic`: the roots of unity in an integral domain form a cyclic group. * `is_primitive_root.zmod_equiv_gpowers`: `zmod k` is equivalent to the subgroup generated by a primitive `k`-th root of unity. * `is_primitive_root.gpowers_eq`: in an integral domain, the subgroup generated by a primitive `k`-th root of unity is equal to the `k`-th roots of unity. * `is_primitive_root.card_primitive_roots`: if an integral domain has a primitive `k`-th root of unity, then it has `Ο† k` of them. ## Implementation details It is desirable that `roots_of_unity` is a subgroup, and it will mainly be applied to rings (e.g. the ring of integers in a number field) and fields. We therefore implement it as a subgroup of the units of a commutative monoid. We have chosen to define `roots_of_unity n` for `n : β„•+`, instead of `n : β„•`, because almost all lemmas need the positivity assumption, and in particular the type class instances for `fintype` and `is_cyclic`. On the other hand, for primitive roots of unity, it is desirable to have a predicate not just on units, but directly on elements of the ring/field. For example, we want to say that `exp (2 * pi * I / n)` is a primitive `n`-th root of unity in the complex numbers, without having to turn that number into a unit first. This creates a little bit of friction, but lemmas like `is_primitive_root.is_unit` and `is_primitive_root.coe_units_iff` should provide the necessary glue. -/ open_locale classical big_operators noncomputable theory open polynomial open finset variables {M N G Gβ‚€ R S : Type*} variables [comm_monoid M] [comm_monoid N] [comm_group G] [comm_group_with_zero Gβ‚€] variables [integral_domain R] [integral_domain S] section roots_of_unity variables {k l : β„•+} /-- `roots_of_unity k M` is the subgroup of elements `m : units M` that satisfy `m ^ k = 1` -/ def roots_of_unity (k : β„•+) (M : Type*) [comm_monoid M] : subgroup (units M) := { carrier := { ΞΆ | ΞΆ ^ (k : β„•) = 1 }, one_mem' := one_pow _, mul_mem' := Ξ» ΞΆ ΞΎ hΞΆ hΞΎ, by simp only [*, set.mem_set_of_eq, mul_pow, one_mul] at *, inv_mem' := Ξ» ΞΆ hΞΆ, by simp only [*, set.mem_set_of_eq, inv_pow, one_inv] at * } @[simp] lemma mem_roots_of_unity (k : β„•+) (ΞΆ : units M) : ΞΆ ∈ roots_of_unity k M ↔ ΞΆ ^ (k : β„•) = 1 := iff.rfl lemma roots_of_unity_le_of_dvd (h : k ∣ l) : roots_of_unity k M ≀ roots_of_unity l M := begin obtain ⟨d, rfl⟩ := h, intros ΞΆ h, simp only [mem_roots_of_unity, pnat.mul_coe, pow_mul, one_pow, *] at *, end lemma map_roots_of_unity (f : units M β†’* units N) (k : β„•+) : (roots_of_unity k M).map f ≀ roots_of_unity k N := begin rintros _ ⟨΢, h, rfl⟩, simp only [←monoid_hom.map_pow, *, mem_roots_of_unity, subgroup.mem_coe, monoid_hom.map_one] at * end lemma mem_roots_of_unity_iff_mem_nth_roots {ΞΆ : units R} : ΞΆ ∈ roots_of_unity k R ↔ (ΞΆ : R) ∈ nth_roots k (1 : R) := by simp only [mem_roots_of_unity, mem_nth_roots k.pos, units.ext_iff, units.coe_one, units.coe_pow] variables (k R) /-- Equivalence between the `k`-th roots of unity in `R` and the `k`-th roots of `1`. This is implemented as equivalence of subtypes, because `roots_of_unity` is a subgroup of the group of units, whereas `nth_roots` is a multiset. -/ def roots_of_unity_equiv_nth_roots : roots_of_unity k R ≃ {x // x ∈ nth_roots k (1 : R)} := begin refine { to_fun := Ξ» x, ⟨x, mem_roots_of_unity_iff_mem_nth_roots.mp x.2⟩, inv_fun := Ξ» x, ⟨⟨x, x ^ (k - 1 : β„•), _, _⟩, _⟩, left_inv := _, right_inv := _ }, swap 4, { rintro ⟨x, hx⟩, ext, refl }, swap 4, { rintro ⟨x, hx⟩, ext, refl }, all_goals { rcases x with ⟨x, hx⟩, rw [mem_nth_roots k.pos] at hx, simp only [subtype.coe_mk, ← pow_succ, ← pow_succ', hx, nat.sub_add_cancel (show 1 ≀ (k : β„•), from k.one_le)] }, { show (_ : units R) ^ (k : β„•) = 1, simp only [units.ext_iff, hx, units.coe_mk, units.coe_one, subtype.coe_mk, units.coe_pow] } end variables {k R} @[simp] lemma roots_of_unity_equiv_nth_roots_apply (x : roots_of_unity k R) : (roots_of_unity_equiv_nth_roots R k x : R) = x := rfl @[simp] lemma roots_of_unity_equiv_nth_roots_symm_apply (x : {x // x ∈ nth_roots k (1 : R)}) : ((roots_of_unity_equiv_nth_roots R k).symm x : R) = x := rfl variables (k R) instance roots_of_unity.fintype : fintype (roots_of_unity k R) := fintype.of_equiv {x // x ∈ nth_roots k (1 : R)} $ (roots_of_unity_equiv_nth_roots R k).symm instance roots_of_unity.is_cyclic : is_cyclic (roots_of_unity k R) := is_cyclic_of_subgroup_integral_domain ((units.coe_hom R).comp (roots_of_unity k R).subtype) (units.ext.comp subtype.val_injective) lemma card_roots_of_unity : fintype.card (roots_of_unity k R) ≀ k := calc fintype.card (roots_of_unity k R) = fintype.card {x // x ∈ nth_roots k (1 : R)} : fintype.card_congr (roots_of_unity_equiv_nth_roots R k) ... ≀ (nth_roots k (1 : R)).attach.card : multiset.card_le_of_le (multiset.erase_dup_le _) ... = (nth_roots k (1 : R)).card : multiset.card_attach ... ≀ k : card_nth_roots k 1 end roots_of_unity /-- An element `ΞΆ` is a primitive `k`-th root of unity if `ΞΆ ^ k = 1`, and if `l` satisfies `ΞΆ ^ l = 1` then `k ∣ l`. -/ structure is_primitive_root (ΞΆ : M) (k : β„•) : Prop := (pow_eq_one : ΞΆ ^ (k : β„•) = 1) (dvd_of_pow_eq_one : βˆ€ l : β„•, ΞΆ ^ l = 1 β†’ k ∣ l) section primitive_roots variables {k : β„•} /-- `primitive_roots k R` is the finset of primitive `k`-th roots of unity in the integral domain `R`. -/ def primitive_roots (k : β„•) (R : Type*) [integral_domain R] : finset R := (nth_roots k (1 : R)).to_finset.filter (Ξ» ΞΆ, is_primitive_root ΞΆ k) @[simp] lemma mem_primitive_roots {ΞΆ : R} (h0 : 0 < k) : ΞΆ ∈ primitive_roots k R ↔ is_primitive_root ΞΆ k := begin rw [primitive_roots, mem_filter, multiset.mem_to_finset, mem_nth_roots h0, and_iff_right_iff_imp], exact is_primitive_root.pow_eq_one end end primitive_roots namespace is_primitive_root variables {k l : β„•} lemma iff_def (ΞΆ : M) (k : β„•) : is_primitive_root ΞΆ k ↔ (ΞΆ ^ k = 1) ∧ (βˆ€ l : β„•, ΞΆ ^ l = 1 β†’ k ∣ l) := ⟨λ ⟨h1, h2⟩, ⟨h1, h2⟩, Ξ» ⟨h1, h2⟩, ⟨h1, h2⟩⟩ lemma mk_of_lt (ΞΆ : M) (hk : 0 < k) (h1 : ΞΆ ^ k = 1) (h : βˆ€ l : β„•, 0 < l β†’ l < k β†’ ΞΆ ^ l β‰  1) : is_primitive_root ΞΆ k := begin refine ⟨h1, _⟩, intros l hl, apply dvd_trans _ (k.gcd_dvd_right l), suffices : k.gcd l = k, { rw this }, rw eq_iff_le_not_lt, refine ⟨nat.le_of_dvd hk (k.gcd_dvd_left l), _⟩, intro h', apply h _ (nat.gcd_pos_of_pos_left _ hk) h', exact pow_gcd_eq_one _ h1 hl end section comm_monoid variables {ΞΆ : M} (h : is_primitive_root ΞΆ k) lemma pow_eq_one_iff_dvd (l : β„•) : ΞΆ ^ l = 1 ↔ k ∣ l := ⟨h.dvd_of_pow_eq_one l, by { rintro ⟨i, rfl⟩, simp only [pow_mul, h.pow_eq_one, one_pow, pnat.mul_coe] }⟩ lemma is_unit (h : is_primitive_root ΞΆ k) (h0 : 0 < k) : is_unit ΞΆ := begin apply is_unit_of_mul_eq_one ΞΆ (ΞΆ ^ (k - 1)), rw [← pow_succ, nat.sub_add_cancel h0, h.pow_eq_one] end lemma pow_ne_one_of_pos_of_lt (h0 : 0 < l) (hl : l < k) : ΞΆ ^ l β‰  1 := mt (nat.le_of_dvd h0 ∘ h.dvd_of_pow_eq_one _) $ not_le_of_lt hl lemma pow_inj (h : is_primitive_root ΞΆ k) ⦃i j : ℕ⦄ (hi : i < k) (hj : j < k) (H : ΞΆ ^ i = ΞΆ ^ j) : i = j := begin wlog hij : i ≀ j, apply le_antisymm hij, rw ← nat.sub_eq_zero_iff_le, apply nat.eq_zero_of_dvd_of_lt _ (lt_of_le_of_lt (nat.sub_le_self _ _) hj), apply h.dvd_of_pow_eq_one, rw [← ((h.is_unit (lt_of_le_of_lt (nat.zero_le _) hi)).pow i).mul_left_inj, ← pow_add, nat.sub_add_cancel hij, H, one_mul] end lemma one : is_primitive_root (1 : M) 1 := { pow_eq_one := pow_one _, dvd_of_pow_eq_one := Ξ» l hl, one_dvd _ } @[simp] lemma one_right_iff : is_primitive_root ΞΆ 1 ↔ ΞΆ = 1 := begin split, { intro h, rw [← pow_one ΞΆ, h.pow_eq_one] }, { rintro rfl, exact one } end @[simp] lemma coe_units_iff {ΞΆ : units M} : is_primitive_root (ΞΆ : M) k ↔ is_primitive_root ΞΆ k := by simp only [iff_def, units.ext_iff, units.coe_pow, units.coe_one] lemma pow_of_coprime (h : is_primitive_root ΞΆ k) (i : β„•) (hi : i.coprime k) : is_primitive_root (ΞΆ ^ i) k := begin by_cases h0 : k = 0, { subst k, simp only [*, pow_one, nat.coprime_zero_right] at * }, rcases h.is_unit (nat.pos_of_ne_zero h0) with ⟨΢, rfl⟩, rw [← units.coe_pow], rw coe_units_iff at h ⊒, refine { pow_eq_one := by rw [← pow_mul', pow_mul, h.pow_eq_one, one_pow], dvd_of_pow_eq_one := _ }, intros l hl, apply h.dvd_of_pow_eq_one, rw [← pow_one ΞΆ, ← gpow_coe_nat ΞΆ, ← hi.gcd_eq_one, nat.gcd_eq_gcd_ab, gpow_add, mul_pow, ← gpow_coe_nat, ← gpow_mul, mul_right_comm], simp only [gpow_mul, hl, h.pow_eq_one, one_gpow, one_pow, one_mul, gpow_coe_nat] end lemma pow_iff_coprime (h : is_primitive_root ΞΆ k) (h0 : 0 < k) (i : β„•) : is_primitive_root (ΞΆ ^ i) k ↔ i.coprime k := begin refine ⟨_, h.pow_of_coprime i⟩, intro hi, obtain ⟨a, ha⟩ := i.gcd_dvd_left k, obtain ⟨b, hb⟩ := i.gcd_dvd_right k, suffices : b = k, { rwa [this, ← one_mul k, nat.mul_left_inj h0, eq_comm] at hb { occs := occurrences.pos [1] } }, rw [ha] at hi, rw [mul_comm] at hb, apply nat.dvd_antisymm ⟨i.gcd k, hb⟩ (hi.dvd_of_pow_eq_one b _), rw [← pow_mul', ← mul_assoc, ← hb, pow_mul, h.pow_eq_one, one_pow] end end comm_monoid section comm_group variables {ΞΆ : G} (h : is_primitive_root ΞΆ k) lemma gpow_eq_one : ΞΆ ^ (k : β„€) = 1 := h.pow_eq_one lemma gpow_eq_one_iff_dvd (h : is_primitive_root ΞΆ k) (l : β„€) : ΞΆ ^ l = 1 ↔ (k : β„€) ∣ l := begin by_cases h0 : 0 ≀ l, { lift l to β„• using h0, rw [gpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l }, { have : 0 ≀ -l, { simp only [not_le, neg_nonneg] at h0 ⊒, exact le_of_lt h0 }, lift -l to β„• using this with l' hl', rw [← dvd_neg, ← hl'], norm_cast, rw [← h.pow_eq_one_iff_dvd, ← inv_inj, ← gpow_neg, ← hl', gpow_coe_nat, one_inv] } end lemma inv (h : is_primitive_root ΞΆ k) : is_primitive_root ΢⁻¹ k := { pow_eq_one := by simp only [h.pow_eq_one, one_inv, eq_self_iff_true, inv_pow], dvd_of_pow_eq_one := begin intros l hl, apply h.dvd_of_pow_eq_one l, rw [← inv_inj, ← inv_pow, hl, one_inv] end } @[simp] lemma inv_iff : is_primitive_root ΢⁻¹ k ↔ is_primitive_root ΞΆ k := by { refine ⟨_, Ξ» h, inv h⟩, intro h, rw [← inv_inv ΞΆ], exact inv h } lemma gpow_of_gcd_eq_one (h : is_primitive_root ΞΆ k) (i : β„€) (hi : i.gcd k = 1) : is_primitive_root (ΞΆ ^ i) k := begin by_cases h0 : 0 ≀ i, { lift i to β„• using h0, exact h.pow_of_coprime i hi }, have : 0 ≀ -i, { simp only [not_le, neg_nonneg] at h0 ⊒, exact le_of_lt h0 }, lift -i to β„• using this with i' hi', rw [← inv_iff, ← gpow_neg, ← hi'], apply h.pow_of_coprime, rw [int.gcd, ← int.nat_abs_neg, ← hi'] at hi, exact hi end @[simp] lemma coe_subgroup_iff (H : subgroup G) {ΞΆ : H} : is_primitive_root (ΞΆ : G) k ↔ is_primitive_root ΞΆ k := by simp only [iff_def, ← subgroup.coe_pow, ← H.coe_one, ← subtype.ext_iff] end comm_group section comm_group_with_zero variables {ΞΆ : Gβ‚€} (h : is_primitive_root ΞΆ k) lemma fpow_eq_one : ΞΆ ^ (k : β„€) = 1 := h.pow_eq_one lemma fpow_eq_one_iff_dvd (h : is_primitive_root ΞΆ k) (l : β„€) : ΞΆ ^ l = 1 ↔ (k : β„€) ∣ l := begin by_cases h0 : 0 ≀ l, { lift l to β„• using h0, rw [fpow_coe_nat], norm_cast, exact h.pow_eq_one_iff_dvd l }, { have : 0 ≀ -l, { simp only [not_le, neg_nonneg] at h0 ⊒, exact le_of_lt h0 }, lift -l to β„• using this with l' hl', rw [← dvd_neg, ← hl'], norm_cast, rw [← h.pow_eq_one_iff_dvd, ← inv_inj', ← fpow_neg, ← hl', fpow_coe_nat, inv_one] } end lemma inv' (h : is_primitive_root ΞΆ k) : is_primitive_root ΢⁻¹ k := { pow_eq_one := by simp only [h.pow_eq_one, inv_one, eq_self_iff_true, inv_pow'], dvd_of_pow_eq_one := begin intros l hl, apply h.dvd_of_pow_eq_one l, rw [← inv_inj', ← inv_pow', hl, inv_one] end } @[simp] lemma inv_iff' : is_primitive_root ΢⁻¹ k ↔ is_primitive_root ΞΆ k := by { refine ⟨_, Ξ» h, inv' h⟩, intro h, rw [← inv_inv' ΞΆ], exact inv' h } lemma fpow_of_gcd_eq_one (h : is_primitive_root ΞΆ k) (i : β„€) (hi : i.gcd k = 1) : is_primitive_root (ΞΆ ^ i) k := begin by_cases h0 : 0 ≀ i, { lift i to β„• using h0, exact h.pow_of_coprime i hi }, have : 0 ≀ -i, { simp only [not_le, neg_nonneg] at h0 ⊒, exact le_of_lt h0 }, lift -i to β„• using this with i' hi', rw [← inv_iff', ← fpow_neg, ← hi'], apply h.pow_of_coprime, rw [int.gcd, ← int.nat_abs_neg, ← hi'] at hi, exact hi end end comm_group_with_zero section integral_domain variables {ΞΆ : R} @[simp] lemma primitive_roots_zero : primitive_roots 0 R = βˆ… := begin rw [← finset.val_eq_zero, ← multiset.subset_zero, ← nth_roots_zero (1 : R), primitive_roots], simp only [finset.not_mem_empty, forall_const, forall_prop_of_false, multiset.to_finset_zero, finset.filter_true_of_mem, finset.empty_val, not_false_iff, multiset.zero_subset, nth_roots_zero] end @[simp] lemma primitive_roots_one : primitive_roots 1 R = {(1 : R)} := begin apply finset.eq_singleton_iff_unique_mem.2, split, { simp only [is_primitive_root.one_right_iff, mem_primitive_roots zero_lt_one] }, { intros x hx, rw [mem_primitive_roots zero_lt_one, is_primitive_root.one_right_iff] at hx, exact hx } end lemma neg_one (p : β„•) [char_p R p] (hp : p β‰  2) : is_primitive_root (-1 : R) 2 := mk_of_lt (-1 : R) dec_trivial (by simp only [one_pow, neg_square]) $ begin intros l hl0 hl2, obtain rfl : l = 1, { unfreezingI { clear_dependent R p }, dec_trivial! }, simp only [pow_one, ne.def], intro h, suffices h2 : p ∣ 2, { have := char_p.char_ne_one R p, unfreezingI { clear_dependent R }, have aux := nat.le_of_dvd dec_trivial h2, revert this hp h2, revert p, dec_trivial }, simp only [← char_p.cast_eq_zero_iff R p, nat.cast_bit0, nat.cast_one], rw [bit0, ← h, neg_add_self] { occs := occurrences.pos [1] } end lemma eq_neg_one_of_two_right (h : is_primitive_root ΞΆ 2) : ΞΆ = -1 := begin apply (eq_or_eq_neg_of_pow_two_eq_pow_two ΞΆ 1 _).resolve_left, { rw [← pow_one ΞΆ], apply h.pow_ne_one_of_pos_of_lt; dec_trivial }, { simp only [h.pow_eq_one, one_pow] } end end integral_domain section integral_domain variables {ΞΆ : units R} (h : is_primitive_root ΞΆ k) protected lemma mem_roots_of_unity {n : β„•+} (h : is_primitive_root ΞΆ n) : ΞΆ ∈ roots_of_unity n R := h.pow_eq_one /-- The (additive) monoid equivalence between `zmod k` and the powers of a primitive root of unity `ΞΆ`. -/ def zmod_equiv_gpowers (h : is_primitive_root ΞΆ k) : zmod k ≃+ additive (subgroup.gpowers ΞΆ) := add_equiv.of_bijective (add_monoid_hom.lift_of_surjective (int.cast_add_hom _) zmod.int_cast_surjective { to_fun := Ξ» i, additive.of_mul (⟨_, i, rfl⟩ : subgroup.gpowers ΞΆ), map_zero' := by { simp only [gpow_zero], refl }, map_add' := by { intros i j, simp only [gpow_add], refl } } (Ξ» i hi, begin simp only [add_monoid_hom.mem_ker, char_p.int_cast_eq_zero_iff (zmod k) k, add_monoid_hom.coe_mk, int.coe_cast_add_hom] at hi ⊒, obtain ⟨i, rfl⟩ := hi, simp only [gpow_mul, h.pow_eq_one, one_gpow, gpow_coe_nat], refl end)) $ begin split, { rw add_monoid_hom.injective_iff, intros i hi, rw subtype.ext_iff at hi, have := (h.gpow_eq_one_iff_dvd _).mp hi, rw [← (char_p.int_cast_eq_zero_iff (zmod k) k _).mpr this, eq_comm], exact classical.some_spec (zmod.int_cast_surjective i) }, { rintro ⟨ξ, i, rfl⟩, refine ⟨int.cast_add_hom _ i, _⟩, rw [add_monoid_hom.lift_of_surjective_comp_apply], refl } end @[simp] lemma zmod_equiv_gpowers_apply_coe_int (i : β„€) : h.zmod_equiv_gpowers i = additive.of_mul (⟨΢ ^ i, i, rfl⟩ : subgroup.gpowers ΞΆ) := begin apply add_monoid_hom.lift_of_surjective_comp_apply, intros j hj, simp only [add_monoid_hom.mem_ker, char_p.int_cast_eq_zero_iff (zmod k) k, add_monoid_hom.coe_mk, int.coe_cast_add_hom] at hj ⊒, obtain ⟨j, rfl⟩ := hj, simp only [gpow_mul, h.pow_eq_one, one_gpow, gpow_coe_nat], refl end @[simp] lemma zmod_equiv_gpowers_apply_coe_nat (i : β„•) : h.zmod_equiv_gpowers i = additive.of_mul (⟨΢ ^ i, i, rfl⟩ : subgroup.gpowers ΞΆ) := begin have : (i : zmod k) = (i : β„€), by norm_cast, simp only [this, zmod_equiv_gpowers_apply_coe_int, gpow_coe_nat], refl end @[simp] lemma zmod_equiv_gpowers_symm_apply_gpow (i : β„€) : h.zmod_equiv_gpowers.symm (additive.of_mul (⟨΢ ^ i, i, rfl⟩ : subgroup.gpowers ΞΆ)) = i := by rw [← h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_int] @[simp] lemma zmod_equiv_gpowers_symm_apply_gpow' (i : β„€) : h.zmod_equiv_gpowers.symm ⟨΢ ^ i, i, rfl⟩ = i := h.zmod_equiv_gpowers_symm_apply_gpow i @[simp] lemma zmod_equiv_gpowers_symm_apply_pow (i : β„•) : h.zmod_equiv_gpowers.symm (additive.of_mul (⟨΢ ^ i, i, rfl⟩ : subgroup.gpowers ΞΆ)) = i := by rw [← h.zmod_equiv_gpowers.symm_apply_apply i, zmod_equiv_gpowers_apply_coe_nat] @[simp] lemma zmod_equiv_gpowers_symm_apply_pow' (i : β„•) : h.zmod_equiv_gpowers.symm ⟨΢ ^ i, i, rfl⟩ = i := h.zmod_equiv_gpowers_symm_apply_pow i lemma gpowers_eq {k : β„•+} {ΞΆ : units R} (h : is_primitive_root ΞΆ k) : subgroup.gpowers ΞΆ = roots_of_unity k R := begin apply subgroup.ext', haveI : fact (0 < (k : β„•)) := k.pos, haveI F : fintype (subgroup.gpowers ΞΆ) := fintype.of_equiv _ (h.zmod_equiv_gpowers).to_equiv, refine @set.eq_of_subset_of_card_le (units R) (subgroup.gpowers ΞΆ) (roots_of_unity k R) F (roots_of_unity.fintype R k) (subgroup.gpowers_subset $ show ΞΆ ∈ roots_of_unity k R, from h.pow_eq_one) _, calc fintype.card (roots_of_unity k R) ≀ k : card_roots_of_unity R k ... = fintype.card (zmod k) : (zmod.card k).symm ... = fintype.card (subgroup.gpowers ΞΆ) : fintype.card_congr (h.zmod_equiv_gpowers).to_equiv end lemma eq_pow_of_mem_roots_of_unity {k : β„•+} {ΞΆ ΞΎ : units R} (h : is_primitive_root ΞΆ k) (hΞΎ : ΞΎ ∈ roots_of_unity k R) : βˆƒ (i : β„•) (hi : i < k), ΞΆ ^ i = ΞΎ := begin obtain ⟨n, rfl⟩ : βˆƒ n : β„€, ΞΆ ^ n = ΞΎ, by rwa [← h.gpowers_eq] at hΞΎ, have hk0 : (0 : β„€) < k := by exact_mod_cast k.pos, let i := n % k, have hi0 : 0 ≀ i := int.mod_nonneg _ (ne_of_gt hk0), lift i to β„• using hi0 with iβ‚€ hiβ‚€, refine ⟨iβ‚€, _, _⟩, { zify, rw [hiβ‚€], exact int.mod_lt_of_pos _ hk0 }, { have aux := h.gpow_eq_one, rw [← coe_coe] at aux, rw [← gpow_coe_nat, hiβ‚€, ← int.mod_add_div n k, gpow_add, gpow_mul, aux, one_gpow, mul_one] } end lemma eq_pow_of_pow_eq_one {k : β„•} {ΞΆ ΞΎ : R} (h : is_primitive_root ΞΆ k) (hΞΎ : ΞΎ ^ k = 1) (h0 : 0 < k) : βˆƒ i < k, ΞΆ ^ i = ΞΎ := begin obtain ⟨΢, rfl⟩ := h.is_unit h0, obtain ⟨ξ, rfl⟩ := is_unit_of_pow_eq_one ΞΎ k hΞΎ h0, obtain ⟨k, rfl⟩ : βˆƒ k' : β„•+, k = k' := ⟨⟨k, h0⟩, rfl⟩, simp only [← units.coe_pow, ← units.ext_iff], rw coe_units_iff at h, apply h.eq_pow_of_mem_roots_of_unity, rw [mem_roots_of_unity, units.ext_iff, units.coe_pow, hΞΎ, units.coe_one] end lemma is_primitive_root_iff' {k : β„•+} {ΞΆ ΞΎ : units R} (h : is_primitive_root ΞΆ k) : is_primitive_root ΞΎ k ↔ βˆƒ (i < (k : β„•)) (hi : i.coprime k), ΞΆ ^ i = ΞΎ := begin split, { intro hΞΎ, obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_mem_roots_of_unity hΞΎ.pow_eq_one, rw h.pow_iff_coprime k.pos at hΞΎ, exact ⟨i, hik, hΞΎ, rfl⟩ }, { rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi } end lemma is_primitive_root_iff {k : β„•} {ΞΆ ΞΎ : R} (h : is_primitive_root ΞΆ k) (h0 : 0 < k) : is_primitive_root ΞΎ k ↔ βˆƒ (i < k) (hi : i.coprime k), ΞΆ ^ i = ΞΎ := begin split, { intro hΞΎ, obtain ⟨i, hik, rfl⟩ := h.eq_pow_of_pow_eq_one hΞΎ.pow_eq_one h0, rw h.pow_iff_coprime h0 at hΞΎ, exact ⟨i, hik, hΞΎ, rfl⟩ }, { rintro ⟨i, -, hi, rfl⟩, exact h.pow_of_coprime i hi } end lemma card_roots_of_unity' {n : β„•+} (h : is_primitive_root ΞΆ n) : fintype.card (roots_of_unity n R) = n := begin haveI : fact (0 < ↑n) := n.pos, let e := h.zmod_equiv_gpowers, haveI F : fintype (subgroup.gpowers ΞΆ) := fintype.of_equiv _ e.to_equiv, calc fintype.card (roots_of_unity n R) = fintype.card (subgroup.gpowers ΞΆ) : fintype.card_congr $ by rw h.gpowers_eq ... = fintype.card (zmod n) : fintype.card_congr e.to_equiv.symm ... = n : zmod.card n end lemma card_roots_of_unity {ΞΆ : R} {n : β„•+} (h : is_primitive_root ΞΆ n) : fintype.card (roots_of_unity n R) = n := begin obtain ⟨΢, h΢⟩ := h.is_unit n.pos, rw [← hΞΆ, is_primitive_root.coe_units_iff] at h, exact h.card_roots_of_unity' end /-- The cardinality of the multiset `nth_roots ↑n (1 : R)` is `n` if there is a primitive root of unity in `R`. -/ lemma card_nth_roots {ΞΆ : R} {n : β„•} (h : is_primitive_root ΞΆ n) : (nth_roots n (1 : R)).card = n := begin cases nat.eq_zero_or_pos n with hzero hpos, { simp only [hzero, multiset.card_zero, nth_roots_zero] }, rw eq_iff_le_not_lt, use card_nth_roots n 1, { rw [not_lt], have hcard : fintype.card {x // x ∈ nth_roots n (1 : R)} ≀ (nth_roots n (1 : R)).attach.card := multiset.card_le_of_le (multiset.erase_dup_le _), rw multiset.card_attach at hcard, rw ← pnat.to_pnat'_coe hpos at hcard h ⊒, set m := nat.to_pnat' n, rw [← fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at hcard, exact hcard } end /-- The multiset `nth_roots ↑n (1 : R)` has no repeated elements if there is a primitive root of unity in `R`. -/ lemma nth_roots_nodup {ΞΆ : R} {n : β„•} (h : is_primitive_root ΞΆ n) : (nth_roots n (1 : R)).nodup := begin cases nat.eq_zero_or_pos n with hzero hpos, { simp only [hzero, multiset.nodup_zero, nth_roots_zero] }, apply (@multiset.erase_dup_eq_self R _ _).1, rw eq_iff_le_not_lt, split, { exact multiset.erase_dup_le (nth_roots n (1 : R)) }, { by_contra ha, replace ha := multiset.card_lt_of_lt ha, rw card_nth_roots h at ha, have hrw : (nth_roots n (1 : R)).erase_dup.card = fintype.card {x // x ∈ (nth_roots n (1 : R))}, { set fs := (⟨(nth_roots n (1 : R)).erase_dup, multiset.nodup_erase_dup _⟩ : finset R), rw [← finset.card_mk, ← fintype.card_of_subtype fs _], intro x, simp only [multiset.mem_erase_dup, finset.mem_mk] }, rw ← pnat.to_pnat'_coe hpos at h hrw ha, set m := nat.to_pnat' n, rw [hrw, ← fintype.card_congr (roots_of_unity_equiv_nth_roots R m), card_roots_of_unity h] at ha, exact nat.lt_asymm ha ha } end @[simp] lemma card_nth_roots_finset {ΞΆ : R} {n : β„•} (h : is_primitive_root ΞΆ n) : (nth_roots_finset n R).card = n := by rw [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h), card_mk, h.card_nth_roots] open_locale nat /-- If an integral domain has a primitive `k`-th root of unity, then it has `Ο† k` of them. -/ lemma card_primitive_roots {ΞΆ : R} {k : β„•} (h : is_primitive_root ΞΆ k) (h0 : 0 < k) : (primitive_roots k R).card = Ο† k := begin symmetry, refine finset.card_congr (Ξ» i _, ΞΆ ^ i) _ _ _, { simp only [true_and, and_imp, mem_filter, mem_range, mem_univ], rintro i - hi, rw mem_primitive_roots h0, exact h.pow_of_coprime i hi.symm }, { simp only [true_and, and_imp, mem_filter, mem_range, mem_univ], rintro i j hi - hj - H, exact h.pow_inj hi hj H }, { simp only [exists_prop, true_and, mem_filter, mem_range, mem_univ], intros ΞΎ hΞΎ, rw [mem_primitive_roots h0, h.is_primitive_root_iff h0] at hΞΎ, rcases hΞΎ with ⟨i, hin, hi, H⟩, exact ⟨i, ⟨hin, hi.symm⟩, H⟩ } end /-- The sets `primitive_roots k R` are pairwise disjoint. -/ lemma disjoint {k l : β„•} (hk : 0 < k) (hl : 0 < l) (h : k β‰  l) : disjoint (primitive_roots k R) (primitive_roots l R) := begin intro z, simp only [finset.inf_eq_inter, finset.mem_inter, mem_primitive_roots, hk, hl, iff_def], rintro ⟨⟨hzk, Hzk⟩, ⟨hzl, Hzl⟩⟩, apply_rules [h, nat.dvd_antisymm, Hzk, Hzl, hzk, hzl] end /-- If there is a `n`-th primitive root of unity in `R` and `b` divides `n`, then there is a `b`-th primitive root of unity in `R`. -/ lemma pow {ΞΆ : R} {n : β„•} {a b : β„•} (hn : 0 < n) (h : is_primitive_root ΞΆ n) (hprod : n = a * b) : is_primitive_root (ΞΆ ^ a) b := begin subst n, simp only [iff_def, ← pow_mul, h.pow_eq_one, eq_self_iff_true, true_and], intros l hl, have ha0 : a β‰  0, { rintro rfl, simpa only [nat.not_lt_zero, zero_mul] using hn }, rwa ← mul_dvd_mul_iff_left ha0, exact h.dvd_of_pow_eq_one _ hl end /-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n` if there is a primitive root of unity in `R`. -/ lemma nth_roots_one_eq_bind_primitive_roots' {ΞΆ : R} {n : β„•+} (h : is_primitive_root ΞΆ n) : nth_roots_finset n R = (nat.divisors ↑n).bind (Ξ» i, (primitive_roots i R)) := begin symmetry, apply finset.eq_of_subset_of_card_le, { intros x, simp only [nth_roots_finset, ← multiset.to_finset_eq (nth_roots_nodup h), exists_prop, finset.mem_bind, finset.mem_filter, finset.mem_range, mem_nth_roots, finset.mem_mk, nat.mem_divisors, and_true, ne.def, pnat.ne_zero, pnat.pos, not_false_iff], rintro ⟨a, ⟨d, hd⟩, ha⟩, have hazero : 0 < a, { contrapose! hd with ha0, simp only [le_zero_iff_eq, zero_mul, *] at *, exact n.ne_zero }, rw mem_primitive_roots hazero at ha, rw [hd, pow_mul, ha.pow_eq_one, one_pow] }, { apply le_of_eq, rw [h.card_nth_roots_finset, finset.card_bind], { rw [← nat.sum_totient n, nat.filter_dvd_eq_divisors (pnat.ne_zero n), sum_congr rfl] { occs := occurrences.pos [1] }, simp only [finset.mem_filter, finset.mem_range, nat.mem_divisors], rintro k ⟨H, hk⟩, have hdvd := H, rcases H with ⟨d, hd⟩, rw mul_comm at hd, rw (h.pow n.pos hd).card_primitive_roots (pnat.pos_of_div_pos hdvd) }, { intros i hi j hj hdiff, simp only [nat.mem_divisors, and_true, ne.def, pnat.ne_zero, not_false_iff] at hi hj, exact disjoint (pnat.pos_of_div_pos hi) (pnat.pos_of_div_pos hj) hdiff } } end /-- `nth_roots n` as a `finset` is equal to the union of `primitive_roots i R` for `i ∣ n` if there is a primitive root of unity in `R`. -/ lemma nth_roots_one_eq_bind_primitive_roots {ΞΆ : R} {n : β„•} (hpos : 0 < n) (h : is_primitive_root ΞΆ n) : nth_roots_finset n R = (nat.divisors n).bind (Ξ» i, (primitive_roots i R)) := @nth_roots_one_eq_bind_primitive_roots' _ _ _ ⟨n, hpos⟩ h end integral_domain end is_primitive_root
38635741c2ddbf10d14f1a0fb806781d0b4ff6be
4727251e0cd73359b15b664c3170e5d754078599
/src/group_theory/commensurable.lean
7ac884e35107693624b38e35447f6f72de7b72b5
[ "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,217
lean
/- Copyright (c) 2021 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck -/ import group_theory.index import group_theory.quotient_group import group_theory.subgroup.pointwise import group_theory.group_action.conj_act /-! # Commensurability for subgroups This file defines commensurability for subgroups of a group `G`. It then goes on to prove that commensurability defines an equivalence relation and finally defines the commensurator of a subgroup of `G`. ## Main definitions * `commensurable`: defines commensurability for two subgroups `H`, `K` of `G` * `commensurator`: defines the commensurator of a subgroup `H` of `G`. ## Implementation details We define the commensurator of a subgroup `H` of `G` by first defining it as a subgroup of `(conj_act G)`, which we call commensurator' and then taking the pre-image under the map `G β†’ (conj_act G)` to obtain our commensurator as a subgroup of `G`. -/ variables {G : Type*} [group G] /--Two subgroups `H K` of `G` are commensurable if `H βŠ“ K` has finite index in both `H` and `K` -/ def commensurable (H K : subgroup G) : Prop := H.relindex K β‰  0 ∧ K.relindex H β‰  0 namespace commensurable open_locale pointwise @[refl] protected lemma refl (H : subgroup G) : commensurable H H := by simp [commensurable] lemma comm {H K : subgroup G} : commensurable H K ↔ commensurable K H := and.comm @[symm] lemma symm {H K : subgroup G} : commensurable H K β†’ commensurable K H := and.symm @[trans] lemma trans {H K L : subgroup G} (hhk : commensurable H K) (hkl : commensurable K L) : commensurable H L := ⟨subgroup.relindex_ne_zero_trans hhk.1 hkl.1, subgroup.relindex_ne_zero_trans hkl.2 hhk.2⟩ lemma equivalence : equivalence (@commensurable G _) := ⟨commensurable.refl, Ξ» _ _, commensurable.symm, Ξ» _ _ _, commensurable.trans⟩ /--Equivalence of `K/H βŠ“ K` with `gKg⁻¹/gHg⁻¹ βŠ“ gKg⁻¹`-/ def quot_conj_equiv (H K : subgroup G) (g : conj_act G) : K β§Έ (H.subgroup_of K) ≃ (g β€’ K).1 β§Έ ((g β€’ H).subgroup_of (g β€’ K)) := quotient.congr (K.equiv_smul g).to_equiv (Ξ» a b, by { rw [←quotient.eq', ←quotient.eq', quotient_group.eq', quotient_group.eq', subgroup.mem_subgroup_of, subgroup.mem_subgroup_of, mul_equiv.to_equiv_eq_coe, mul_equiv.coe_to_equiv, ←mul_equiv.map_inv, ←mul_equiv.map_mul, subgroup.equiv_smul_apply_coe, subgroup.smul_mem_pointwise_smul_iff] }) lemma commensurable_conj {H K : subgroup G} (g : conj_act G) : commensurable H K ↔ commensurable (g β€’ H) (g β€’ K) := and_congr (not_iff_not.mpr (eq.congr_left (cardinal.to_nat_congr (quot_conj_equiv H K g)))) (not_iff_not.mpr (eq.congr_left (cardinal.to_nat_congr (quot_conj_equiv K H g)))) lemma commensurable_inv (H : subgroup G) (g : conj_act G) : commensurable (g β€’ H) H ↔ commensurable H (g⁻¹ β€’ H) := by rw [commensurable_conj, inv_smul_smul] /-- For `H` a subgroup of `G`, this is the subgroup of all elements `g : conj_aut G` such that `commensurable (g β€’ H) H` -/ def commensurator' (H : subgroup G) : subgroup (conj_act G) := { carrier := {g : conj_act G | commensurable (g β€’ H) H}, one_mem' := by rw [set.mem_set_of_eq, one_smul], mul_mem' := Ξ» a b ha hb, by { rw [set.mem_set_of_eq, mul_smul], exact trans ((commensurable_conj a).mp hb) ha }, inv_mem' := Ξ» a ha, by rwa [set.mem_set_of_eq, comm, ←commensurable_inv] } /-- For `H` a subgroup of `G`, this is the subgroup of all elements `g : G` such that `commensurable (g H g⁻¹) H` -/ def commensurator (H : subgroup G) : subgroup G := (commensurator' H).comap (conj_act.to_conj_act.to_monoid_hom) @[simp] lemma commensurator'_mem_iff (H : subgroup G) (g : conj_act G) : g ∈ (commensurator' H) ↔ commensurable (g β€’ H) H := iff.rfl @[simp] lemma commensurator_mem_iff (H : subgroup G) (g : G) : g ∈ (commensurator H) ↔ commensurable (conj_act.to_conj_act g β€’ H) H := iff.rfl lemma eq {H K : subgroup G} (hk : commensurable H K) : commensurator H = commensurator K := subgroup.ext (Ξ» x, let hx := (commensurable_conj x).1 hk in ⟨λ h, hx.symm.trans (h.trans hk), Ξ» h, hx.trans (h.trans hk.symm)⟩) end commensurable
eaec01e1a9b7e4f0b8090a411caad251ef868df3
958488bc7f3c2044206e0358e56d7690b6ae696c
/lean/negation.lean
68770db655a8077a22d82ea63181784ec5be72d9
[]
no_license
possientis/Prog
a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4
d4b3debc37610a88e0dac3ac5914903604fd1d1f
refs/heads/master
1,692,263,717,723
1,691,757,179,000
1,691,757,179,000
40,361,602
3
0
null
1,679,896,438,000
1,438,953,859,000
Coq
UTF-8
Lean
false
false
551
lean
lemma L1 : βˆ€ (p q:Prop), (p β†’ q) β†’ Β¬q β†’ Β¬p := Ξ» p q h nq hp, nq (h hp) --#check L1 lemma L1' : βˆ€ (p q:Prop), (p β†’ q) β†’ Β¬q β†’ Β¬p := Ξ» p q hpq hnq, assume hp : p, show false, from hnq (hpq hp) --#check L1' lemma L2 : βˆ€ (p q:Prop), p β†’ Β¬p β†’ q := Ξ» p q hp hnp, false.elim (hnp hp) --#check L2 lemma L2' : βˆ€ (p q:Prop), p β†’ Β¬p β†’ q := Ξ» p q hp hnp, absurd hp hnp --#check L2' lemma L3 : βˆ€ (p q r:Prop), Β¬p β†’ q β†’ (q β†’ p) β†’ r := Ξ» p q r hnp hq hqp, absurd (hqp hq) hnp --#check L3
2d7fe0af5d2bed8b981d3aa94cb638e79fff5eee
8c02fed42525b65813b55c064afe2484758d6d09
/src/irsem_exec.lean
1d493955a422a1bb328ef095898b2374255d6904
[ "LicenseRef-scancode-generic-cla", "MIT" ]
permissive
microsoft/AliveInLean
3eac351a34154efedd3ffc4fe2fa4ec01b219e0d
4b739dd6e4266b26a045613849df221374119871
refs/heads/master
1,691,419,737,939
1,689,365,567,000
1,689,365,568,000
131,156,103
23
18
NOASSERTION
1,660,342,040,000
1,524,747,538,000
Lean
UTF-8
Lean
false
false
2,444
lean
-- Copyright (c) Microsoft Corporation. All rights reserved. -- Licensed under the MIT license. import system.io import smt2.syntax import .irsem import .lang import .common import .bitvector open io -- poisonty := bool -- If it is false, it is poison. -- boolty := bool -- If it is false, it is UB. def irsem_exec : irsem := { intty := bitvector, poisonty := bool, boolty := bool } namespace irsem_exec open irsem @[reducible] instance iul: uint_like irsem_exec.intty := bitvector_is_uint_like @[reducible] instance ihc: has_comp irsem_exec.intty irsem_exec.boolty := bitvector_has_comp @[reducible] instance ioc: has_overflow_check irsem_exec.intty irsem_exec.boolty := bitvector_has_overflow_check @[reducible] instance pbl: bool_like irsem_exec.poisonty := bool_is_bool_like @[reducible] instance p2b: has_coe irsem_exec.poisonty irsem_exec.boolty := ⟨@id bool⟩ @[reducible] instance bbl: bool_like irsem_exec.boolty := bool_is_bool_like @[reducible] instance b2i: has_coe irsem_exec.boolty (irsem_exec.intty size.one) := bool_bitvec_has_coe @[reducible] instance bhi (sz:size): has_ite irsem_exec.boolty (irsem_exec.intty sz) := bool_has_ite (bitvector sz) @[reducible] instance bhi2 : has_ite irsem_exec.boolty irsem_exec.poisonty := bool_has_ite irsem_exec.poisonty def give_poison: irsem.valty irsem_exec β†’ irsem.valty irsem_exec | (valty.ival sz o b) := valty.ival sz o (@bool_like.ff irsem_exec.poisonty irsem_exec.pbl) instance poisonty_tostr: has_to_string irsem_exec.poisonty := ⟨λ b:bool, to_string b⟩ instance valty_tostr: has_to_string irsem_exec.valty := ⟨λ b, match b with | (irsem.valty.ival sz b p) := has_to_string.to_string (b.1) ++ ", poison:" ++ to_string (~p) end⟩ instance boolty_tostr: has_to_string irsem_exec.boolty := ⟨λ b:bool, to_string b⟩ instance tostr: has_to_string irsem_exec.irstate := ⟨λ st, irstate.to_string' irsem_exec st⟩ def int_to_smt (sz:size) : irsem_exec.intty sz β†’ smt2.term | z := smt2.term.const (smt2.special_constant.bitvec sz z.1) def val_to_smt : irsem_exec.valty β†’ smt2.term | (irsem.valty.ival sz i _) := int_to_smt sz i def poison_to_smt : irsem_exec.valty β†’ smt2.term | (irsem.valty.ival _ _ p) := smt2.term.const (smt2.special_constant.bool p) def bool_to_smt : irsem_exec.boolty β†’ smt2.term | p := smt2.term.const (smt2.special_constant.bool p) end irsem_exec
4071f25e996d2a6e287ca90b629b279e2e7eb694
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/order/partial_sups.lean
79a1be0e73f962bf7bc37580c11b3bb9c6c16e40
[ "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
5,783
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import data.finset.lattice import data.set.pairwise import order.preorder_hom /-! # The monotone sequence of partial supremums of a sequence We define `partial_sups : (β„• β†’ Ξ±) β†’ β„• β†’β‚˜ Ξ±` inductively. For `f : β„• β†’ Ξ±`, `partial_sups f` is the sequence `f 0 `, `f 0 βŠ” f 1`, `f 0 βŠ” f 1 βŠ” f 2`, ... The point of this definition is that * it doesn't need a `⨆`, as opposed to `⨆ (i ≀ n), f i`. * it doesn't need a `βŠ₯`, as opposed to `(finset.range (n + 1)).sup f`. * it avoids needing to prove that `finset.range (n + 1)` is nonempty to use `finset.sup'`. Equivalence with those definitions is shown by `partial_sups_eq_bsupr`, `partial_sups_eq_sup_range`, `partial_sups_eq_sup'_range` and respectively. ## Notes One might dispute whether this sequence should start at `f 0` or `βŠ₯`. We choose the former because : * Starting at `βŠ₯` requires... having a bottom element. * `Ξ» f n, (finset.range n).sup f` is already effectively the sequence starting at `βŠ₯`. * If we started at `βŠ₯` we wouldn't have the Galois insertion. See `partial_sups.gi`. ## TODO One could generalize `partial_sups` to any locally finite bot preorder domain, in place of `β„•`. Necessary for the TODO in the module docstring of `order.disjointed`. -/ variables {Ξ± : Type*} section semilattice_sup variables [semilattice_sup Ξ±] /-- The monotone sequence whose value at `n` is the supremum of the `f m` where `m ≀ n`. -/ def partial_sups (f : β„• β†’ Ξ±) : β„• β†’β‚˜ Ξ± := ⟨@nat.rec (Ξ» _, Ξ±) (f 0) (Ξ» (n : β„•) (a : Ξ±), a βŠ” f (n + 1)), monotone_nat_of_le_succ (Ξ» n, le_sup_left)⟩ @[simp] lemma partial_sups_zero (f : β„• β†’ Ξ±) : partial_sups f 0 = f 0 := rfl @[simp] lemma partial_sups_succ (f : β„• β†’ Ξ±) (n : β„•) : partial_sups f (n + 1) = partial_sups f n βŠ” f (n + 1) := rfl lemma le_partial_sups_of_le (f : β„• β†’ Ξ±) {m n : β„•} (h : m ≀ n) : f m ≀ partial_sups f n := begin induction n with n ih, { cases h, apply le_refl, }, { cases h with h h, { exact le_sup_right, }, { exact (ih h).trans le_sup_left, } }, end lemma le_partial_sups (f : β„• β†’ Ξ±) : f ≀ partial_sups f := Ξ» n, le_partial_sups_of_le f le_rfl lemma partial_sups_le (f : β„• β†’ Ξ±) (n : β„•) (a : Ξ±) (w : βˆ€ m, m ≀ n β†’ f m ≀ a) : partial_sups f n ≀ a := begin induction n with n ih, { apply w 0 le_rfl, }, { exact sup_le (ih (Ξ» m p, w m (nat.le_succ_of_le p))) (w (n + 1) le_rfl) } end lemma monotone.partial_sups_eq {f : β„• β†’ Ξ±} (hf : monotone f) : (partial_sups f : β„• β†’ Ξ±) = f := begin ext n, induction n with n ih, { refl }, { rw [partial_sups_succ, ih, sup_eq_right.2 (hf (nat.le_succ _))] } end lemma partial_sups_mono : monotone (partial_sups : (β„• β†’ Ξ±) β†’ β„• β†’β‚˜ Ξ±) := begin rintro f g h n, induction n with n ih, { exact h 0 }, { exact sup_le_sup ih (h _) } end /-- `partial_sups` forms a Galois insertion with the coercion from monotone functions to functions. -/ def partial_sups.gi : galois_insertion (partial_sups : (β„• β†’ Ξ±) β†’ β„• β†’β‚˜ Ξ±) coe_fn := { choice := Ξ» f h, ⟨f, begin convert (partial_sups f).monotone, exact (le_partial_sups f).antisymm h, end⟩, gc := Ξ» f g, begin refine ⟨(le_partial_sups f).trans, Ξ» h, _⟩, convert partial_sups_mono h, exact preorder_hom.ext _ _ g.monotone.partial_sups_eq.symm, end, le_l_u := Ξ» f, le_partial_sups f, choice_eq := Ξ» f h, preorder_hom.ext _ _ ((le_partial_sups f).antisymm h) } lemma partial_sups_eq_sup'_range (f : β„• β†’ Ξ±) (n : β„•) : partial_sups f n = (finset.range (n + 1)).sup' ⟨n, finset.self_mem_range_succ n⟩ f := begin induction n with n ih, { simp }, { dsimp [partial_sups] at ih ⊒, simp_rw @finset.range_succ n.succ, rw [ih, finset.sup'_insert, sup_comm] } end end semilattice_sup lemma partial_sups_eq_sup_range [semilattice_sup_bot Ξ±] (f : β„• β†’ Ξ±) (n : β„•) : partial_sups f n = (finset.range (n + 1)).sup f := begin induction n with n ih, { simp }, { dsimp [partial_sups] at ih ⊒, rw [finset.range_succ, finset.sup_insert, sup_comm, ih] } end /- Note this lemma requires a distributive lattice, so is not useful (or true) in situations such as submodules. -/ lemma partial_sups_disjoint_of_disjoint [distrib_lattice_bot Ξ±] (f : β„• β†’ Ξ±) (h : pairwise (disjoint on f)) {m n : β„•} (hmn : m < n) : disjoint (partial_sups f m) (f n) := begin induction m with m ih, { exact h 0 n hmn.ne, }, { rw [partial_sups_succ, disjoint_sup_left], exact ⟨ih (nat.lt_of_succ_lt hmn), h (m + 1) n hmn.ne⟩ } end section complete_lattice variables [complete_lattice Ξ±] lemma partial_sups_eq_bsupr (f : β„• β†’ Ξ±) (n : β„•) : partial_sups f n = ⨆ (i ≀ n), f i := begin rw [partial_sups_eq_sup_range, finset.sup_eq_supr], congr, ext a, exact supr_congr_Prop (by rw [finset.mem_range, nat.lt_succ_iff]) (Ξ» _, rfl), end @[simp] lemma supr_partial_sups_eq (f : β„• β†’ Ξ±) : (⨆ n, partial_sups f n) = ⨆ n, f n := begin refine (supr_le $ Ξ» n, _).antisymm (supr_le_supr $ le_partial_sups f), rw partial_sups_eq_bsupr, exact bsupr_le_supr _ _, end lemma supr_le_supr_of_partial_sups_le_partial_sups {f g : β„• β†’ Ξ±} (h : partial_sups f ≀ partial_sups g) : (⨆ n, f n) ≀ ⨆ n, g n := begin rw [←supr_partial_sups_eq f, ←supr_partial_sups_eq g], exact supr_le_supr h, end lemma supr_eq_supr_of_partial_sups_eq_partial_sups {f g : β„• β†’ Ξ±} (h : partial_sups f = partial_sups g) : (⨆ n, f n) = ⨆ n, g n := by simp_rw [←supr_partial_sups_eq f, ←supr_partial_sups_eq g, h] end complete_lattice
406a554632eebcddb0b948173c4009f04bc972ce
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/matchtac.lean
3df10bf5f527dc603a1d8125cd58323bb49c846e
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,598
lean
new_frontend theorem tst1 {Ξ± : Type} {p : Prop} (xs : List Ξ±) (h₁ : (a : Ξ±) β†’ (as : List Ξ±) β†’ xs = a :: as β†’ p) (hβ‚‚ : xs = [] β†’ p) : p := by match h:xs with | [] => exact hβ‚‚ h | z::zs => apply h₁ z zs; assumption theorem tst2 {Ξ± : Type} {p : Prop} (xs : List Ξ±) (h₁ : (a : Ξ±) β†’ (as : List Ξ±) β†’ xs = a :: as β†’ p) (hβ‚‚ : xs = [] β†’ p) : p := by match h:xs with | [] => ?nilCase | z::zs => ?consCase; case consCase => exact h₁ z zs h; case nilCase => exact hβ‚‚ h def tst3 {Ξ± Ξ² Ξ³ : Type} (h : Ξ± Γ— Ξ² Γ— Ξ³) : Ξ² Γ— Ξ± Γ— Ξ³ := by { match h with | (a, b, c) => exact (b, a, c) } theorem tst4 {Ξ± : Type} {p : Prop} (xs : List Ξ±) (h₁ : (a : Ξ±) β†’ (as : List Ξ±) β†’ xs = a :: as β†’ p) (hβ‚‚ : xs = [] β†’ p) : p := by match h:xs with | [] => _ | z::zs => _ case match_2 => exact h₁ z zs h exact hβ‚‚ h theorem tst5 {p q r} (h : p ∨ q ∨ r) : r ∨ q ∨ p:= by match h with | Or.inl h => exact Or.inr (Or.inr h) | Or.inr (Or.inl h) => ?c1 | Or.inr (Or.inr h) => ?c2 case c2 => apply Or.inl assumption case c1 => apply Or.inr apply Or.inl assumption theorem tst6 {p q r} (h : p ∨ q ∨ r) : r ∨ q ∨ p:= by match h with | Or.inl h => exact Or.inr (Or.inr h) | Or.inr (Or.inl h) => ?c1 | Or.inr (Or.inr h) => apply Or.inl assumption case c1 => apply Or.inr; apply Or.inl; assumption theorem tst7 {p q r} (h : p ∨ q ∨ r) : r ∨ q ∨ p:= by match h with | Or.inl h => exact Or.inr (Or.inr h) | Or.inr (Or.inl h) => apply Or.inr; apply Or.inl; assumption | Or.inr (Or.inr h) => apply Or.inl; assumption inductive ListLast.{u} {Ξ± : Type u} : List Ξ± β†’ Type u | empty : ListLast [] | nonEmpty : (as : List Ξ±) β†’ (a : Ξ±) β†’ ListLast (as ++ [a]) axiom last {Ξ±} (xs : List Ξ±) : ListLast xs axiom back {Ξ±} [Inhabited Ξ±] (xs : List Ξ±) : Ξ± axiom popBack {Ξ±} : List Ξ± β†’ List Ξ± axiom backEq {Ξ±} [Inhabited Ξ±] : (xs : List Ξ±) β†’ (x : Ξ±) β†’ back (xs ++ [x]) = x axiom popBackEq {Ξ±} : (xs : List Ξ±) β†’ (x : Ξ±) β†’ popBack (xs ++ [x]) = xs theorem tst8 {Ξ±} [Inhabited Ξ±] (xs : List Ξ±) : xs β‰  [] β†’ xs = popBack xs ++ [back xs] := match xs, h:last xs with | _, ListLast.empty => fun h => absurd rfl h | _, ListLast.nonEmpty ys y => fun _ => sorry theorem tst9 {Ξ±} [Inhabited Ξ±] (xs : List Ξ±) : xs β‰  [] β†’ xs = popBack xs ++ [back xs] := by match xs, h:last xs with | _, ListLast.empty => intro h; exact absurd rfl h | _, ListLast.nonEmpty ys y => intro; rw [popBackEq, backEq]; exact rfl
90b009a7db2582043551c6b5332ad892f4255a93
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/unifhint2.lean
c31691f22ca9afafb1c185363be6f76a6331c1d8
[ "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
639
lean
/- The following hints are too expensive, but good enough for small natural numbers -/ unif_hint natAddBase (x y : Nat) where y =?= 0 |- Nat.add (Nat.succ x) y =?= Nat.succ x unif_hint natAddStep (x y z w : Nat) where y =?= Nat.succ w z =?= Nat.add (Nat.succ x) w |- Nat.add (Nat.succ x) y =?= Nat.succ z def BV (n : Nat) := { a : Array Bool // a.size = n } def sext (x : BV s) (n : Nat) : BV (s+n) := ⟨mkArray (s+n) false, Array.size_mkArray ..⟩ def bvmul (x y : BV w) : BV w := x def tst1 (x y : BV 64) : BV 128 := bvmul (sext x 64) (sext y _) def tst2 (x y : BV 16) : BV 32 := bvmul (sext x 16) (sext y _)
570a0efff15ffa0072ab3d20782b97dcee65f992
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/125.lean
71e066b80f8b0881cd545bbb43cc0a9cdecd81bf
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
673
lean
new_frontend class HasElems (Ξ± : Type) : Type := (elems : Array Ξ±) def elems (Ξ± : Type) [HasElems Ξ±] : Array Ξ± := HasElems.elems inductive Foo : Type | mk1 : Bool β†’ Foo | mk2 : Bool β†’ Foo open Foo instance BoolElems : HasElems Bool := ⟨#[false, true]⟩ instance FooElems : HasElems Foo := ⟨(elems Bool).map mk1 ++ (elems Bool).map mk2⟩ def fooRepr (foo : Foo) := match foo with | mk1 b => "OH " ++ toString b | mk2 b => "DR " ++ toString b instance : HasRepr Foo := ⟨fooRepr⟩ #eval elems Foo #eval #[false, true].map Foo.mk1 def Foo.toBool : Foo β†’ Bool | Foo.mk1 b => b | Foo.mk2 b => b #eval #[Foo.mk1 false, Foo.mk2 true].map Foo.toBool
b1d66eb853218b33edd0dbfc5e06246582b29663
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/algebra/ordered_auto.lean
f544d9be8e86ddec17d99876355945ab52611b6f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,578
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.algebra.basic import Mathlib.algebra.module.ordered import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Ordered algebras An ordered algebra is an ordered semiring, which is an algebra over an ordered commutative semiring, for which scalar multiplication is "compatible" with the two orders. The prototypical example is 2x2 matrices over the reals or complexes (or indeed any C^* algebra) where the ordering the one determined by the positive cone of positive operators, i.e. `A ≀ B` iff `B - A = star R * R` for some `R`. (We don't yet have this example in mathlib.) ## Implementation Because the axioms for an ordered algebra are exactly the same as those for the underlying module being ordered, we don't actually introduce a new class, but just use the `ordered_semimodule` mixin. ## Tags ordered algebra -/ theorem algebra_map_monotone {R : Type u_1} {A : Type u_2} [ordered_comm_ring R] [ordered_ring A] [algebra R A] [ordered_semimodule R A] : monotone ⇑(algebra_map R A) := sorry protected instance linear_ordered_comm_ring.to_ordered_semimodule {R : Type u_1} [linear_ordered_comm_ring R] : ordered_semimodule R R := ordered_semimodule.mk ordered_semiring.mul_lt_mul_of_pos_left fun (a b c : R) (w₁ : c β€’ a < c β€’ b) (wβ‚‚ : 0 < c) => iff.mp (mul_lt_mul_left wβ‚‚) w₁ end Mathlib
8864e7fdb3d76990c9c7c0389c2b01fbe3ecf240
cf39355caa609c0f33405126beee2739aa3cb77e
/library/data/vector.lean
f329b45e2434f6eb29a3ff85528ae53ed4f7d2e7
[ "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
4,562
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura Tuples are lists of a fixed size. It is implemented as a subtype. -/ prelude import init.data.list init.data.subtype init.meta.interactive init.data.fin universes u v w def vector (Ξ± : Type u) (n : β„•) := { l : list Ξ± // l.length = n } namespace vector variables {Ξ± : Type u} {Ξ² : Type v} {Ο† : Type w} variable {n : β„•} instance [decidable_eq Ξ±] : decidable_eq (vector Ξ± n) := begin unfold vector, apply_instance end @[pattern] def nil : vector Ξ± 0 := ⟨[], rfl⟩ @[pattern] def cons : Ξ± β†’ vector Ξ± n β†’ vector Ξ± (nat.succ n) | a ⟨ v, h ⟩ := ⟨ a::v, congr_arg nat.succ h ⟩ @[reducible] def length (v : vector Ξ± n) : β„• := n open nat def head : vector Ξ± (nat.succ n) β†’ Ξ± | ⟨ [], h ⟩ := by contradiction | ⟨ a :: v, h ⟩ := a theorem head_cons (a : Ξ±) : Ξ  (v : vector Ξ± n), head (cons a v) = a | ⟨ l, h ⟩ := rfl def tail : vector Ξ± n β†’ vector Ξ± (n - 1) | ⟨ [], h ⟩ := ⟨ [], congr_arg pred h ⟩ | ⟨ a :: v, h ⟩ := ⟨ v, congr_arg pred h ⟩ theorem tail_cons (a : Ξ±) : Ξ  (v : vector Ξ± n), tail (cons a v) = v | ⟨ l, h ⟩ := rfl @[simp] theorem cons_head_tail : βˆ€ v : vector Ξ± (succ n), (cons (head v) (tail v)) = v | ⟨ [], h ⟩ := by contradiction | ⟨ a :: v, h ⟩ := rfl def to_list (v : vector Ξ± n) : list Ξ± := v.1 def nth : Ξ  (v : vector Ξ± n), fin n β†’ Ξ± | ⟨ l, h ⟩ i := l.nth_le i.1 (by rw h; exact i.2) def append {n m : nat} : vector Ξ± n β†’ vector Ξ± m β†’ vector Ξ± (n + m) | ⟨ l₁, h₁ ⟩ ⟨ lβ‚‚, hβ‚‚ ⟩ := ⟨ l₁ ++ lβ‚‚, by simp * ⟩ @[elab_as_eliminator] def elim {Ξ±} {C : Ξ  {n}, vector Ξ± n β†’ Sort u} (H : βˆ€l : list Ξ±, C ⟨l, rfl⟩) {n : nat} : Ξ  (v : vector Ξ± n), C v | ⟨l, h⟩ := match n, h with ._, rfl := H l end /- map -/ def map (f : Ξ± β†’ Ξ²) : vector Ξ± n β†’ vector Ξ² n | ⟨ l, h ⟩ := ⟨ list.map f l, by simp * ⟩ @[simp] theorem map_nil (f : Ξ± β†’ Ξ²) : map f nil = nil := rfl theorem map_cons (f : Ξ± β†’ Ξ²) (a : Ξ±) : Ξ  (v : vector Ξ± n), map f (cons a v) = cons (f a) (map f v) | ⟨l,h⟩ := rfl def mapβ‚‚ (f : Ξ± β†’ Ξ² β†’ Ο†) : vector Ξ± n β†’ vector Ξ² n β†’ vector Ο† n | ⟨ x, _ ⟩ ⟨ y, _ ⟩ := ⟨ list.mapβ‚‚ f x y, by simp * ⟩ def repeat (a : Ξ±) (n : β„•) : vector Ξ± n := ⟨ list.repeat a n, list.length_repeat a n ⟩ def drop (i : β„•) : vector Ξ± n β†’ vector Ξ± (n - i) | ⟨l, p⟩ := ⟨ list.drop i l, by simp * ⟩ def take (i : β„•) : vector Ξ± n β†’ vector Ξ± (min i n) | ⟨l, p⟩ := ⟨ list.take i l, by simp * ⟩ def remove_nth (i : fin n) : vector Ξ± n β†’ vector Ξ± (n - 1) | ⟨l, p⟩ := ⟨ list.remove_nth l i.1, by rw [l.length_remove_nth i.1]; rw p; exact i.2 ⟩ def of_fn : Ξ  {n}, (fin n β†’ Ξ±) β†’ vector Ξ± n | 0 f := nil | (n+1) f := cons (f 0) (of_fn (Ξ»i, f i.succ)) section accum open prod variable {Οƒ : Type} def map_accumr (f : Ξ± β†’ Οƒ β†’ Οƒ Γ— Ξ²) : vector Ξ± n β†’ Οƒ β†’ Οƒ Γ— vector Ξ² n | ⟨ x, px ⟩ c := let res := list.map_accumr f x c in ⟨ res.1, res.2, by simp * ⟩ def map_accumrβ‚‚ {Ξ± Ξ² Οƒ Ο† : Type} (f : Ξ± β†’ Ξ² β†’ Οƒ β†’ Οƒ Γ— Ο†) : vector Ξ± n β†’ vector Ξ² n β†’ Οƒ β†’ Οƒ Γ— vector Ο† n | ⟨ x, px ⟩ ⟨ y, py ⟩ c := let res := list.map_accumrβ‚‚ f x y c in ⟨ res.1, res.2, by simp * ⟩ end accum protected theorem eq {n : β„•} : βˆ€ (a1 a2 : vector Ξ± n), to_list a1 = to_list a2 β†’ a1 = a2 | ⟨x, h1⟩ ⟨._, h2⟩ rfl := rfl protected theorem eq_nil (v : vector Ξ± 0) : v = nil := v.eq nil (list.eq_nil_of_length_eq_zero v.2) @[simp] theorem to_list_mk (v : list Ξ±) (P : list.length v = n) : to_list (subtype.mk v P) = v := rfl @[simp] theorem to_list_nil : to_list nil = @list.nil Ξ± := rfl @[simp] theorem to_list_length (v : vector Ξ± n) : (to_list v).length = n := v.2 @[simp] theorem to_list_cons (a : Ξ±) (v : vector Ξ± n) : to_list (cons a v) = a :: to_list v := begin cases v, reflexivity end @[simp] theorem to_list_append {n m : nat} (v : vector Ξ± n) (w : vector Ξ± m) : to_list (append v w) = to_list v ++ to_list w := begin cases v, cases w, reflexivity end @[simp] theorem to_list_drop {n m : β„•} (v : vector Ξ± m) : to_list (drop n v) = list.drop n (to_list v) := begin cases v, reflexivity end @[simp] theorem to_list_take {n m : β„•} (v : vector Ξ± m) : to_list (take n v) = list.take n (to_list v) := begin cases v, reflexivity end end vector
271833b194db380b56f6dbd44b37ac96421b4357
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world08/level10.lean
d1b6c7c8f7e09e5eb9aaff827f09e54b6a477b34
[]
no_license
abdelq/natural-number-game
a1b5b8f1d52625a7addcefc97c966d3f06a48263
bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2
refs/heads/master
1,668,606,478,691
1,594,175,058,000
1,594,175,058,000
278,673,209
0
1
null
null
null
null
UTF-8
Lean
false
false
157
lean
lemma add_left_eq_zero {{a b : mynat}} (H : a + b = 0) : b = 0 := begin cases b with d, refl, rw add_succ at H, exfalso, apply succ_ne_zero, rw H, refl, end
d5c4e264c16bfe7928711b8aeb160c953131821d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/counterexamples/sorgenfrey_line.lean
6b21bb09c031dfe20595475dae84b6eaa7931787
[ "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
14,242
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.instances.irrational import topology.algebra.order.archimedean import topology.paracompact import topology.metric_space.metrizable import topology.metric_space.emetric_paracompact import data.set.intervals.monotone /-! # Sorgenfrey line In this file we define `sorgenfrey_line` (notation: `ℝₗ`) to be the Sorgenfrey line. It is the real line with the topology space structure generated by half-open intervals `set.Ico a b`. We prove that this line is a completely normal Hausdorff space but its product with itself is not a normal space. In particular, this implies that the topology on `ℝₗ` is neither metrizable, nor second countable. ## Notations - `ℝₗ`: Sorgenfrey line. ## TODO Prove that the Sorgenfrey line is a paracompact space. -/ open set filter topological_space open_locale topological_space filter noncomputable theory /-- The Sorgenfrey line. It is the real line with the topology space structure generated by half-open intervals `set.Ico a b`. -/ @[derive [conditionally_complete_linear_order, linear_ordered_field, archimedean]] def sorgenfrey_line : Type := ℝ localized "notation (name := sorgenfrey_line) `ℝₗ` := sorgenfrey_line" in sorgenfrey_line namespace sorgenfrey_line /-- Ring homomorphism between the Sorgenfrey line and the standard real line. -/ def to_real : ℝₗ ≃+* ℝ := ring_equiv.refl ℝ instance : topological_space ℝₗ := topological_space.generate_from {s : set ℝₗ | βˆƒ a b : ℝₗ, Ico a b = s} lemma is_open_Ico (a b : ℝₗ) : is_open (Ico a b) := topological_space.generate_open.basic _ ⟨a, b, rfl⟩ lemma is_open_Ici (a : ℝₗ) : is_open (Ici a) := Union_Ico_right a β–Έ is_open_Union (is_open_Ico a) lemma nhds_basis_Ico (a : ℝₗ) : (𝓝 a).has_basis (Ξ» b, a < b) (Ξ» b, Ico a b) := begin rw topological_space.nhds_generate_from, haveI : nonempty {x // x ≀ a} := set.nonempty_Iic_subtype, have : (β¨… (x : {i // i ≀ a}), π“Ÿ (Ici ↑x)) = π“Ÿ (Ici a), { refine (is_least.is_glb _).infi_eq, exact ⟨⟨⟨a, le_rfl⟩, rfl⟩, forall_range_iff.2 $ Ξ» b, principal_mono.2 $ Ici_subset_Ici.2 b.2⟩, }, simp only [mem_set_of_eq, infi_and, infi_exists, @infi_comm _ (_ ∈ _), @infi_comm _ (set ℝₗ), infi_infi_eq_right], simp_rw [@infi_comm _ ℝₗ (_ ≀ _), infi_subtype', ← Ici_inter_Iio, ← inf_principal, ← inf_infi, ← infi_inf, this, infi_subtype], suffices : (β¨… x ∈ Ioi a, π“Ÿ (Iio x)).has_basis ((<) a) Iio, from this.principal_inf _, refine has_basis_binfi_principal _ nonempty_Ioi, exact directed_on_iff_directed.2 (directed_of_inf $ Ξ» x y hxy, Iio_subset_Iio hxy), end lemma nhds_basis_Ico_rat (a : ℝₗ) : (𝓝 a).has_countable_basis (Ξ» r : β„š, a < r) (Ξ» r, Ico a r) := begin refine ⟨(nhds_basis_Ico a).to_has_basis (Ξ» b hb, _) (Ξ» r hr, ⟨_, hr, subset.rfl⟩), set.to_countable _⟩, rcases exists_rat_btwn hb with ⟨r, har, hrb⟩, exact ⟨r, har, Ico_subset_Ico_right hrb.le⟩ end lemma nhds_basis_Ico_inv_pnat (a : ℝₗ) : (𝓝 a).has_basis (Ξ» n : β„•+, true) (Ξ» n, Ico a (a + n⁻¹)) := begin refine (nhds_basis_Ico a).to_has_basis (Ξ» b hb, _) (Ξ» n hn, ⟨_, lt_add_of_pos_right _ (inv_pos.2 $ nat.cast_pos.2 n.pos), subset.rfl⟩), rcases exists_nat_one_div_lt (sub_pos.2 hb) with ⟨k, hk⟩, rw [one_div] at hk, rw [← nat.cast_add_one] at hk, exact ⟨k.succ_pnat, trivial, Ico_subset_Ico_right (le_sub_iff_add_le'.1 hk.le)⟩ end lemma nhds_countable_basis_Ico_inv_pnat (a : ℝₗ) : (𝓝 a).has_countable_basis (Ξ» n : β„•+, true) (Ξ» n, Ico a (a + n⁻¹)) := ⟨nhds_basis_Ico_inv_pnat a, set.to_countable _⟩ lemma nhds_antitone_basis_Ico_inv_pnat (a : ℝₗ) : (𝓝 a).has_antitone_basis (Ξ» n : β„•+, Ico a (a + n⁻¹)) := ⟨nhds_basis_Ico_inv_pnat a, monotone_const.Ico $ antitone.const_add (Ξ» k l hkl, inv_le_inv_of_le (nat.cast_pos.2 k.pos) (nat.mono_cast hkl)) _⟩ lemma is_open_iff {s : set ℝₗ} : is_open s ↔ βˆ€ x ∈ s, βˆƒ y > x, Ico x y βŠ† s := is_open_iff_mem_nhds.trans $ forallβ‚‚_congr $ Ξ» x hx, (nhds_basis_Ico x).mem_iff lemma is_closed_iff {s : set ℝₗ} : is_closed s ↔ βˆ€ x βˆ‰ s, βˆƒ y > x, disjoint (Ico x y) s := by simp only [← is_open_compl_iff, is_open_iff, mem_compl_iff, subset_compl_iff_disjoint_right] lemma exists_Ico_disjoint_closed {a : ℝₗ} {s : set ℝₗ} (hs : is_closed s) (ha : a βˆ‰ s) : βˆƒ b > a, disjoint (Ico a b) s := is_closed_iff.1 hs a ha @[simp] lemma map_to_real_nhds (a : ℝₗ) : map to_real (𝓝 a) = 𝓝[β‰₯] (to_real a) := begin refine ((nhds_basis_Ico a).map _).eq_of_same_basis _, simpa only [to_real.image_eq_preimage] using nhds_within_Ici_basis_Ico (to_real a) end lemma nhds_eq_map (a : ℝₗ) : 𝓝 a = map to_real.symm (𝓝[β‰₯] a.to_real) := by simp_rw [← map_to_real_nhds, map_map, (∘), to_real.symm_apply_apply, map_id'] lemma nhds_eq_comap (a : ℝₗ) : 𝓝 a = comap to_real (𝓝[β‰₯] a.to_real) := by rw [← map_to_real_nhds, comap_map to_real.injective] @[continuity] lemma continuous_to_real : continuous to_real := continuous_iff_continuous_at.2 $ Ξ» x, by { rw [continuous_at, tendsto, map_to_real_nhds], exact inf_le_left } instance : order_closed_topology ℝₗ := ⟨is_closed_le_prod.preimage (continuous_to_real.prod_map continuous_to_real)⟩ instance : has_continuous_add ℝₗ := begin refine ⟨continuous_iff_continuous_at.2 _⟩, rintro ⟨x, y⟩, simp only [continuous_at, nhds_prod_eq, nhds_eq_map, nhds_eq_comap (x + y), prod_map_map_eq, tendsto_comap_iff, tendsto_map'_iff, (∘), ← nhds_within_prod_eq], exact (continuous_add.tendsto _).inf (maps_to.tendsto $ Ξ» x hx, add_le_add hx.1 hx.2) end lemma is_clopen_Ici (a : ℝₗ) : is_clopen (Ici a) := ⟨is_open_Ici a, is_closed_Ici⟩ lemma is_clopen_Iio (a : ℝₗ) : is_clopen (Iio a) := by simpa only [compl_Ici] using (is_clopen_Ici a).compl lemma is_clopen_Ico (a b : ℝₗ) : is_clopen (Ico a b) := (is_clopen_Ici a).inter (is_clopen_Iio b) instance : totally_disconnected_space ℝₗ := ⟨λ s hs' hs x hx y hy, le_antisymm (hs.subset_clopen (is_clopen_Ici x) ⟨x, hx, le_rfl⟩ hy) (hs.subset_clopen (is_clopen_Ici y) ⟨y, hy, le_rfl⟩ hx)⟩ instance : first_countable_topology ℝₗ := ⟨λ x, (nhds_basis_Ico_rat x).is_countably_generated⟩ /-- Sorgenfrey line is a completely normal Hausdorff topological space. -/ instance : t5_space ℝₗ := begin /- Let `s` and `t` be disjoint closed sets. For each `x ∈ s` we choose `X x` such that `set.Ico x (X x)` is disjoint with `t`. Similarly, for each `y ∈ t` we choose `Y y` such that `set.Ico y (Y y)` is disjoint with `s`. Then `⋃ x ∈ s, Ico x (X x)` and `⋃ y ∈ t, Ico y (Y y)` are disjoint open sets that include `s` and `t`. -/ refine ⟨λ s t hd₁ hdβ‚‚, _⟩, choose! X hX hXd using Ξ» x (hx : x ∈ s), exists_Ico_disjoint_closed is_closed_closure (disjoint_left.1 hdβ‚‚ hx), choose! Y hY hYd using Ξ» y (hy : y ∈ t), exists_Ico_disjoint_closed is_closed_closure (disjoint_right.1 hd₁ hy), refine disjoint_of_disjoint_of_mem _ (bUnion_mem_nhds_set $ Ξ» x hx, (is_open_Ico x (X x)).mem_nhds $ left_mem_Ico.2 (hX x hx)) (bUnion_mem_nhds_set $ Ξ» y hy, (is_open_Ico y (Y y)).mem_nhds $ left_mem_Ico.2 (hY y hy)), simp only [disjoint_Union_left, disjoint_Union_right, Ico_disjoint_Ico], intros y hy x hx, cases le_total x y with hle hle, { calc min (X x) (Y y) ≀ X x : min_le_left _ _ ... ≀ y : not_lt.1 (Ξ» hyx, (hXd x hx).le_bot ⟨⟨hle, hyx⟩, subset_closure hy⟩) ... ≀ max x y : le_max_right _ _ }, { calc min (X x) (Y y) ≀ Y y : min_le_right _ _ ... ≀ x : not_lt.1 $ Ξ» hxy, (hYd y hy).le_bot ⟨⟨hle, hxy⟩, subset_closure hx⟩ ... ≀ max x y : le_max_left _ _ } end lemma dense_range_coe_rat : dense_range (coe : β„š β†’ ℝₗ) := begin refine dense_iff_inter_open.2 _, rintro U Uo ⟨x, hx⟩, rcases is_open_iff.1 Uo _ hx with ⟨y, hxy, hU⟩, rcases exists_rat_btwn hxy with ⟨z, hxz, hzy⟩, exact ⟨z, hU ⟨hxz.le, hzy⟩, mem_range_self _⟩ end instance : separable_space ℝₗ := ⟨⟨_, countable_range _, dense_range_coe_rat⟩⟩ lemma is_closed_antidiagonal (c : ℝₗ) : is_closed {x : ℝₗ Γ— ℝₗ | x.1 + x.2 = c} := is_closed_singleton.preimage continuous_add lemma is_clopen_Ici_prod (x : ℝₗ Γ— ℝₗ) : is_clopen (Ici x) := (Ici_prod_eq x).symm β–Έ (is_clopen_Ici _).prod (is_clopen_Ici _) /-- Any subset of an antidiagonal `{(x, y) : ℝₗ Γ— ℝₗ| x + y = c}` is a closed set. -/ lemma is_closed_of_subset_antidiagonal {s : set (ℝₗ Γ— ℝₗ)} {c : ℝₗ} (hs : βˆ€ x : ℝₗ Γ— ℝₗ, x ∈ s β†’ x.1 + x.2 = c) : is_closed s := begin rw [← closure_subset_iff_is_closed], rintro ⟨x, y⟩ H, obtain rfl : x + y = c, { change (x, y) ∈ {p : ℝₗ Γ— ℝₗ | p.1 + p.2 = c}, exact closure_minimal (hs : s βŠ† {x | x.1 + x.2 = c}) (is_closed_antidiagonal c) H }, rcases mem_closure_iff.1 H (Ici (x, y)) (is_clopen_Ici_prod _).1 le_rfl with ⟨⟨x', y'⟩, ⟨hx : x ≀ x', hy : y ≀ y'⟩, H⟩, convert H, { refine hx.antisymm _, rwa [← add_le_add_iff_right, hs _ H, add_le_add_iff_left] }, { refine hy.antisymm _, rwa [← add_le_add_iff_left, hs _ H, add_le_add_iff_right] } end lemma nhds_prod_antitone_basis_inv_pnat (x y : ℝₗ) : (𝓝 (x, y)).has_antitone_basis (Ξ» n : β„•+, Ico x (x + n⁻¹) Γ—Λ’ Ico y (y + n⁻¹)) := begin rw [nhds_prod_eq], exact (nhds_antitone_basis_Ico_inv_pnat x).prod (nhds_antitone_basis_Ico_inv_pnat y) end /-- The product of the Sorgenfrey line and itself is not a normal topological space. -/ lemma not_normal_space_prod : Β¬normal_space (ℝₗ Γ— ℝₗ) := begin have hβ‚€ : βˆ€ {n : β„•+}, (0 : ℝ) < n⁻¹, from Ξ» n, inv_pos.2 (nat.cast_pos.2 n.pos), have hβ‚€' : βˆ€ {n : β„•+} {x : ℝ}, x < x + n⁻¹, from Ξ» n x, lt_add_of_pos_right _ hβ‚€, introI, /- Let `S` be the set of points `(x, y)` on the line `x + y = 0` such that `x` is rational. Let `T` be the set of points `(x, y)` on the line `x + y = 0` such that `x` is irrational. These sets are closed, see `sorgenfrey_line.is_closed_of_subset_antidiagonal`, and disjoint. -/ set S := {x : ℝₗ Γ— ℝₗ | x.1 + x.2 = 0 ∧ βˆƒ r : β„š, ↑r = x.1}, set T := {x : ℝₗ Γ— ℝₗ | x.1 + x.2 = 0 ∧ irrational x.1.to_real}, have hSc : is_closed S, from is_closed_of_subset_antidiagonal (Ξ» x hx, hx.1), have hTc : is_closed T, from is_closed_of_subset_antidiagonal (Ξ» x hx, hx.1), have hd : disjoint S T, { rw disjoint_iff_inf_le, rintro ⟨x, y⟩ ⟨⟨-, r, rfl : _ = x⟩, -, hr⟩, exact r.not_irrational hr }, /- Consider disjoint open sets `U βŠ‡ S` and `V βŠ‡ T`. -/ rcases normal_separation hSc hTc hd with ⟨U, V, Uo, Vo, SU, TV, UV⟩, /- For each point `(x, -x) ∈ T`, choose a neighborhood `Ico x (x + k⁻¹) Γ—Λ’ Ico (-x) (-x + k⁻¹) βŠ† V`. -/ have : βˆ€ x : ℝₗ, irrational x.to_real β†’ βˆƒ k : β„•+, Ico x (x + k⁻¹) Γ—Λ’ Ico (-x) (-x + k⁻¹) βŠ† V, { intros x hx, have hV : V ∈ 𝓝 (x, -x), from Vo.mem_nhds (@TV (x, -x) ⟨add_neg_self x, hx⟩), exact (nhds_prod_antitone_basis_inv_pnat _ _).mem_iff.1 hV }, choose! k hkV, /- Since the set of irrational numbers is a dense GΞ΄ set in the usual topology of `ℝ`, there exists `N > 0` such that the set `C N = {x : ℝ | irrational x ∧ k x = N}` is dense in a nonempty interval. In other words, the closure of this set has a nonempty interior. -/ set C : β„•+ β†’ set ℝ := Ξ» n, closure {x | irrational x ∧ k (to_real.symm x) = n}, have H : {x : ℝ | irrational x} βŠ† ⋃ n, C n, from Ξ» x hx, mem_Union.2 ⟨_, subset_closure ⟨hx, rfl⟩⟩, have Hd : dense (⋃ n, interior (C n)) := is_GΞ΄_irrational.dense_Union_interior_of_closed dense_irrational (Ξ» _, is_closed_closure) H, obtain ⟨N, hN⟩ : βˆƒ n : β„•+, (interior $ C n).nonempty, from nonempty_Union.mp Hd.nonempty, /- Choose a rational number `r` in the interior of the closure of `C N`, then choose `n β‰₯ N > 0` such that `Ico r (r + n⁻¹) Γ— Ico (-r) (-r + n⁻¹) βŠ† U`. -/ rcases rat.dense_range_cast.exists_mem_open is_open_interior hN with ⟨r, hr⟩, have hrU : ((r, -r) : ℝₗ Γ— ℝₗ) ∈ U, from @SU (r, -r) ⟨add_neg_self _, r, rfl⟩, obtain ⟨n, hnN, hn⟩ : βˆƒ n (hnN : N ≀ n), Ico (r : ℝₗ) (r + n⁻¹) Γ—Λ’ Ico (-r : ℝₗ) (-r + n⁻¹) βŠ† U, from ((nhds_prod_antitone_basis_inv_pnat _ _).has_basis_ge N).mem_iff.1 (Uo.mem_nhds hrU), /- Finally, choose `x ∈ Ioo (r : ℝ) (r + n⁻¹) ∩ C N`. Then `(x, -r)` belongs both to `U` and `V`, so they are not disjoint. This contradiction completes the proof. -/ obtain ⟨x, hxn, hx_irr, rfl⟩ : βˆƒ x : ℝ, x ∈ Ioo (r : ℝ) (r + n⁻¹) ∧ irrational x ∧ k (to_real.symm x) = N, { have : (r : ℝ) ∈ closure (Ioo (r : ℝ) (r + n⁻¹)), { rw [closure_Ioo hβ‚€'.ne, left_mem_Icc], exact hβ‚€'.le }, rcases mem_closure_iff_nhds.1 this _ (mem_interior_iff_mem_nhds.1 hr) with ⟨x', hx', hx'Ρ⟩, exact mem_closure_iff.1 hx' _ is_open_Ioo hx'Ξ΅ }, refine UV.le_bot (_ : (to_real.symm x, -↑r) ∈ _), refine ⟨hn ⟨_, _⟩, hkV (to_real.symm x) hx_irr ⟨_, _⟩⟩, { exact Ioo_subset_Ico_self hxn }, { exact left_mem_Ico.2 hβ‚€' }, { exact left_mem_Ico.2 hβ‚€' }, { refine (nhds_antitone_basis_Ico_inv_pnat (-x)).2 hnN ⟨neg_le_neg hxn.1.le, _⟩, simp only [add_neg_lt_iff_le_add', lt_neg_add_iff_add_lt], exact hxn.2 } end /-- Topology on the Sorgenfrey line is not metrizable. -/ lemma not_metrizable_space : Β¬metrizable_space ℝₗ := begin introI, letI := metrizable_space_metric ℝₗ, exact not_normal_space_prod infer_instance end /-- Topology on the Sorgenfrey line is not second countable. -/ lemma not_second_countable_topology : Β¬second_countable_topology ℝₗ := by { introI, exact not_metrizable_space (metrizable_space_of_t3_second_countable _) } end sorgenfrey_line
0d10eef2e38f06efa0e6f26309034866bd612a85
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/have6.lean
a0623ab631df7af99770541158e105b4541fda71
[ "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
619
lean
prelude definition Prop : Type.{1} := Type.{0} constant and : Prop β†’ Prop β†’ Prop infixl `∧`:25 := and constant and_intro : forall (a b : Prop), a β†’ b β†’ a ∧ b constants a b c d : Prop axiom Ha : a axiom Hb : b axiom Hc : c check have a ∧ b, from and_intro a b Ha Hb, have b ∧ a, from and_intro b a Hb Ha, have H : a ∧ b, from and_intro a b Ha Hb, have H : a ∧ b, from and_intro a b Ha Hb, then have a ∧ b, from and_intro a b Ha Hb, then have b ∧ a, from and_intro b a Hb Ha, then have H : a ∧ b, from and_intro a b Ha Hb, then have H : a ∧ b, from and_intro a b Ha Hb, Ha
67a446e2605ffc36a7455c73350c91cfc6d140a3
c31182a012eec69da0a1f6c05f42b0f0717d212d
/src/rescale/FiltrationPow.lean
104d2a74f27d8ecbb66fc61c0a03ed8b7902b46a
[]
no_license
Ja1941/lean-liquid
fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc
8e80ed0cbdf5145d6814e833a674eaf05a1495c1
refs/heads/master
1,689,437,983,362
1,628,362,719,000
1,628,362,719,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
657
lean
import rescale.pseudo_normed_group import pseudo_normed_group.FP open_locale classical nnreal open ProFiltPseuNormGrpWithTinv universe variables u @[simp] theorem Filtration_rescale (r' c N : ℝβ‰₯0) [fact (0 < r')] (M) [profinitely_filtered_pseudo_normed_group_with_Tinv r' M] : ((Filtration r').obj c).obj (of r' (rescale N M)) = ((Filtration r').obj (c * N⁻¹)).obj (of r' M) := rfl @[simps hom inv] noncomputable def Filtration_cast_eq (r' c₁ cβ‚‚ : ℝβ‰₯0) (h : c₁ = cβ‚‚) [fact (0 < r')] (M) : ((Filtration r').obj c₁).obj M β‰… ((Filtration r').obj cβ‚‚).obj M := ((Filtration r').map_iso $ category_theory.eq_to_iso h).app M
99a73411b7bc9c76d85b41e5f4e2f227871b8237
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Meta/Injective.lean
14b73888b4c72af4bff6025aa5443898436641d1
[ "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
6,799
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.Transform import Lean.Meta.Tactic.Injection import Lean.Meta.Tactic.Apply import Lean.Meta.Tactic.Cases import Lean.Meta.Tactic.Subst import Lean.Meta.Tactic.Simp.Types import Lean.Meta.Tactic.Assumption namespace Lean.Meta private def mkAnd? (args : Array Expr) : Option Expr := Id.run <| do if args.isEmpty then return none else let mut result := args.back for arg in args.reverse[1:] do result := mkApp2 (mkConst ``And) arg result return result def elimOptParam (type : Expr) : CoreM Expr := do Core.transform type fun e => if e.isAppOfArity ``optParam 2 then return TransformStep.visit (e.getArg! 0) else return TransformStep.visit e private partial def mkInjectiveTheoremTypeCore? (ctorVal : ConstructorVal) (useEq : Bool) : MetaM (Option Expr) := do let us := ctorVal.levelParams.map mkLevelParam let type ← elimOptParam ctorVal.type forallBoundedTelescope type ctorVal.numParams fun params type => forallTelescope type fun args1 resultType => do let jp (args2 args2New : Array Expr) : MetaM (Option Expr) := do let lhs := mkAppN (mkAppN (mkConst ctorVal.name us) params) args1 let rhs := mkAppN (mkAppN (mkConst ctorVal.name us) params) args2 let eq ← mkEq lhs rhs let mut eqs := #[] for arg1 in args1, arg2 in args2 do let arg1Type ← inferType arg1 if !(← isProp arg1Type) && arg1 != arg2 then if (← isDefEq arg1Type (← inferType arg2)) then eqs := eqs.push (← mkEq arg1 arg2) else eqs := eqs.push (← mkHEq arg1 arg2) if let some andEqs ← mkAnd? eqs then let result ← if useEq then mkEq eq andEqs else mkArrow eq andEqs mkForallFVars params (← mkForallFVars args1 (← mkForallFVars args2New result)) else return none let rec mkArgs2 (i : Nat) (type : Expr) (args2 args2New : Array Expr) : MetaM (Option Expr) := do if h : i < args1.size then match (← whnf type) with | Expr.forallE n d b _ => let arg1 := args1.get ⟨i, h⟩ if arg1.occurs resultType then mkArgs2 (i + 1) (b.instantiate1 arg1) (args2.push arg1) args2New else withLocalDecl n (if useEq then BinderInfo.default else BinderInfo.implicit) d fun arg2 => mkArgs2 (i + 1) (b.instantiate1 arg2) (args2.push arg2) (args2New.push arg2) | _ => throwError "unexpected constructor type for '{ctorVal.name}'" else jp args2 args2New if useEq then mkArgs2 0 type #[] #[] else withNewBinderInfos (params.map fun param => (param.fvarId!, BinderInfo.implicit)) <| withNewBinderInfos (args1.map fun arg1 => (arg1.fvarId!, BinderInfo.implicit)) <| mkArgs2 0 type #[] #[] private def mkInjectiveTheoremType? (ctorVal : ConstructorVal) : MetaM (Option Expr) := mkInjectiveTheoremTypeCore? ctorVal false private def injTheoremFailureHeader (ctorName : Name) : MessageData := m!"failed to prove injectivity theorem for constructor '{ctorName}', use 'set_option genInjectivity false' to disable the generation" private def throwInjectiveTheoremFailure {Ξ±} (ctorName : Name) (mvarId : MVarId) : MetaM Ξ± := throwError "{injTheoremFailureHeader ctorName}{indentD <| MessageData.ofGoal mvarId}" private def solveEqOfCtorEq (ctorName : Name) (mvarId : MVarId) (h : FVarId) : MetaM Unit := do match (← injection mvarId h) with | InjectionResult.solved => unreachable! | InjectionResult.subgoal mvarId .. => (← splitAnd mvarId).forM fun mvarId => unless (← assumptionCore mvarId) do throwInjectiveTheoremFailure ctorName mvarId private def mkInjectiveTheoremValue (ctorName : Name) (targetType : Expr) : MetaM Expr := forallTelescopeReducing targetType fun xs type => do let mvar ← mkFreshExprSyntheticOpaqueMVar type solveEqOfCtorEq ctorName mvar.mvarId! xs.back.fvarId! mkLambdaFVars xs mvar def mkInjectiveTheoremNameFor (ctorName : Name) : Name := ctorName ++ `inj private def mkInjectiveTheorem (ctorVal : ConstructorVal) : MetaM Unit := do let some type ← mkInjectiveTheoremType? ctorVal | return () let value ← mkInjectiveTheoremValue ctorVal.name type addDecl <| Declaration.thmDecl { name := mkInjectiveTheoremNameFor ctorVal.name levelParams := ctorVal.levelParams type := (← instantiateMVars type) value := (← instantiateMVars value) } def mkInjectiveEqTheoremNameFor (ctorName : Name) : Name := ctorName ++ `injEq private def mkInjectiveEqTheoremType? (ctorVal : ConstructorVal) : MetaM (Option Expr) := mkInjectiveTheoremTypeCore? ctorVal true private def mkInjectiveEqTheoremValue (ctorName : Name) (targetType : Expr) : MetaM Expr := do forallTelescopeReducing targetType fun xs type => do let mvar ← mkFreshExprSyntheticOpaqueMVar type let [mvarId₁, mvarIdβ‚‚] ← apply mvar.mvarId! (mkConst ``Eq.propIntro) | throwError "unexpected number of subgoals when proving injective theorem for constructor '{ctorName}'" let (h, mvarId₁) ← intro1 mvarId₁ let (_, mvarIdβ‚‚) ← intro1 mvarIdβ‚‚ solveEqOfCtorEq ctorName mvarId₁ h let mvarIdβ‚‚ ← casesAnd mvarIdβ‚‚ let mvarIdβ‚‚ ← substEqs mvarIdβ‚‚ applyRefl mvarIdβ‚‚ (injTheoremFailureHeader ctorName) mkLambdaFVars xs mvar private def mkInjectiveEqTheorem (ctorVal : ConstructorVal) : MetaM Unit := do let some type ← mkInjectiveEqTheoremType? ctorVal | return () let value ← mkInjectiveEqTheoremValue ctorVal.name type let name := mkInjectiveEqTheoremNameFor ctorVal.name addDecl <| Declaration.thmDecl { name levelParams := ctorVal.levelParams type := (← instantiateMVars type) value := (← instantiateMVars value) } addSimpLemma (ext := simpExtension) name (post := true) (inv := false) AttributeKind.global (prio := eval_prio default) register_builtin_option genInjectivity : Bool := { defValue := true descr := "generate injectivity theorems for inductive datatype constructors" } def mkInjectiveTheorems (declName : Name) : MetaM Unit := do if (← getEnv).contains ``Eq.propIntro && genInjectivity.get (← getOptions) && !(← isInductivePredicate declName) then let info ← getConstInfoInduct declName unless info.isUnsafe do for ctor in info.ctors do let ctorVal ← getConstInfoCtor ctor if ctorVal.numFields > 0 then mkInjectiveTheorem ctorVal mkInjectiveEqTheorem ctorVal end Lean.Meta
9ad3829ddb88cf5af7e7e596d6e0c93162687f7c
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/error_full_names.lean
8ea3f51965ed2ec5f181e23d835c3dcc6ff1e801
[ "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
166
lean
namespace foo open nat inductive nat : Type | zero, foosucc : nat β†’ nat #check 0 + nat.zero --error end foo namespace foo #check nat.succ nat.zero --error end foo
928275afb81827f48223eaf900486786ec5b9a1f
a338c3e75cecad4fb8d091bfe505f7399febfd2b
/src/measure_theory/simple_func_dense.lean
42fef8d33c733e1094922e5384b2e141b5554b17
[ "Apache-2.0" ]
permissive
bacaimano/mathlib
88eb7911a9054874fba2a2b74ccd0627c90188af
f2edc5a3529d95699b43514d6feb7eb11608723f
refs/heads/master
1,686,410,075,833
1,625,497,070,000
1,625,497,070,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
38,693
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.integrable_on /-! # Density of simple functions Show that each Borel measurable function can be approximated, both pointwise and in `Lα΅–` norm, by a sequence of simple functions. ## 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`. * `measure_theory.L1.simple_func`, the type of `L1` simple functions (notation: `Ξ± →₁ₛ[ΞΌ] E`) * `coe_to_L1`, the embedding of `Ξ± →₁ₛ[ΞΌ] E` into `Ξ± →₁[ΞΌ] E` ## 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 `∞`. * `tendsto_approx_on_univ_Lp` (Lα΅– convergence): If `E` is a `normed_group` and `f` is measurable and `mem_β„’p` (for `p < ∞`), then the simple functions `simple_func.approx_on f hf s 0 hβ‚€ n` may be considered as elements of `Lp E p ΞΌ`, and they tend in Lα΅– to `f`. * `tendsto_approx_on_univ_L1` (LΒΉ convergence): If `E` is a `normed_group` and `f` is measurable and integrable, then the simple functions `simple_func.approx_on f hf s 0 hβ‚€ n` may be considered as elements of `Lp E 1 ΞΌ`, and they tend in LΒΉ to `f`. * `L1.simple_func.dense_embedding`: the embedding `coe_to_L1` of the `L1` simple functions into `L1` is dense. * `integrable.induction`: to prove a predicate for all elements of `L1`, it suffices to check that it behaves correctly on simple functions in `L1`. ## TODO For `E` finite-dimensional, simple functions `Ξ± β†’β‚› E` are dense in L^∞ -- prove this. ## Notations * `Ξ± β†’β‚› Ξ²` (local notation): the type of simple functions `Ξ± β†’ Ξ²`. * `Ξ± →₁ₛ[ΞΌ] E`: the type of `L1` simple functions `Ξ± β†’ Ξ²`. -/ open set function filter topological_space ennreal emetric finset open_locale classical topological_space 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 -/ section pointwise variables [measurable_space Ξ±] [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_Prop $ Ξ» 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_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) := begin dsimp only [approx_on, coe_comp, (∘)], exact edist_nearest_pt_le _ _ (zero_le _) end 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 pointwise /-! ### Lp approximation by simple functions -/ section Lp variables [measurable_space Ξ²] variables [measurable_space E] [normed_group E] {q : ℝ} {p : ℝβ‰₯0∞} lemma nnnorm_approx_on_le [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (x : Ξ²) (n : β„•) : βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š ≀ βˆ₯f x - yβ‚€βˆ₯β‚Š := begin have := edist_approx_on_le hf hβ‚€ x n, rw edist_comm yβ‚€ at this, simp only [edist_nndist, nndist_eq_nnnorm] at this, exact_mod_cast this end lemma norm_approx_on_yβ‚€_le [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (x : Ξ²) (n : β„•) : βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - yβ‚€βˆ₯ ≀ βˆ₯f x - yβ‚€βˆ₯ + βˆ₯f x - yβ‚€βˆ₯ := begin have := edist_approx_on_y0_le hf hβ‚€ x n, repeat { rw [edist_comm yβ‚€, edist_eq_coe_nnnorm_sub] at this }, exact_mod_cast this, end lemma norm_approx_on_zero_le [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} (hβ‚€ : (0 : E) ∈ s) [separable_space s] (x : Ξ²) (n : β„•) : βˆ₯approx_on f hf s 0 hβ‚€ n xβˆ₯ ≀ βˆ₯f xβˆ₯ + βˆ₯f xβˆ₯ := begin have := edist_approx_on_y0_le hf hβ‚€ x n, simp [edist_comm (0 : E), edist_eq_coe_nnnorm] at this, exact_mod_cast this, end lemma tendsto_approx_on_Lp_snorm [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (hp_ne_top : p β‰  ∞) {ΞΌ : measure Ξ²} (hΞΌ : βˆ€α΅ x βˆ‚ΞΌ, f x ∈ closure s) (hi : snorm (Ξ» x, f x - yβ‚€) p ΞΌ < ∞) : tendsto (Ξ» n, snorm (approx_on f hf s yβ‚€ hβ‚€ n - f) p ΞΌ) at_top (𝓝 0) := begin by_cases hp_zero : p = 0, { simpa only [hp_zero, snorm_exponent_zero] using tendsto_const_nhds }, have hp : 0 < p.to_real := to_real_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr hp_zero, hp_ne_top⟩, suffices : tendsto (Ξ» n, ∫⁻ x, βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š ^ p.to_real βˆ‚ΞΌ) at_top (𝓝 0), { simp only [snorm_eq_lintegral_rpow_nnnorm hp_zero hp_ne_top], convert continuous_rpow_const.continuous_at.tendsto.comp this; simp [_root_.inv_pos.mpr hp] }, -- We simply check the conditions of the Dominated Convergence Theorem: -- (1) The function "`p`-th power of distance between `f` and the approximation" is measurable have hF_meas : βˆ€ n, measurable (Ξ» x, (βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š : ℝβ‰₯0∞) ^ p.to_real), { simpa only [← edist_eq_coe_nnnorm_sub] using Ξ» n, (approx_on f hf s yβ‚€ hβ‚€ n).measurable_bind (Ξ» y x, (edist y (f x)) ^ p.to_real) (Ξ» y, (measurable_edist_right.comp hf).pow_const p.to_real) }, -- (2) The functions "`p`-th power of distance between `f` and the approximation" are uniformly -- bounded, at any given point, by `Ξ» x, βˆ₯f x - yβ‚€βˆ₯ ^ p.to_real` have h_bound : βˆ€ n, (Ξ» x, (βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š : ℝβ‰₯0∞) ^ p.to_real) ≀ᡐ[ΞΌ] (Ξ» x, βˆ₯f x - yβ‚€βˆ₯β‚Š ^ p.to_real), { exact Ξ» n, eventually_of_forall (Ξ» x, rpow_le_rpow (coe_mono (nnnorm_approx_on_le hf hβ‚€ x n)) to_real_nonneg) }, -- (3) The bounding function `Ξ» x, βˆ₯f x - yβ‚€βˆ₯ ^ p.to_real` has finite integral have h_fin : ∫⁻ (a : Ξ²), βˆ₯f a - yβ‚€βˆ₯β‚Š ^ p.to_real βˆ‚ΞΌ < ⊀, { exact lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp_zero hp_ne_top hi }, -- (4) The functions "`p`-th power of distance between `f` and the approximation" tend pointwise -- to zero have h_lim : βˆ€α΅ (a : Ξ²) βˆ‚ΞΌ, tendsto (Ξ» n, (βˆ₯approx_on f hf s yβ‚€ hβ‚€ n a - f aβˆ₯β‚Š : ℝβ‰₯0∞) ^ p.to_real) at_top (𝓝 0), { filter_upwards [hΞΌ], intros a ha, have : tendsto (Ξ» n, (approx_on f hf s yβ‚€ hβ‚€ n) a - f a) at_top (𝓝 (f a - f a)), { exact (tendsto_approx_on hf hβ‚€ ha).sub tendsto_const_nhds }, convert continuous_rpow_const.continuous_at.tendsto.comp (tendsto_coe.mpr this.nnnorm), simp [zero_rpow_of_pos hp] }, -- Then we apply the Dominated Convergence Theorem simpa using tendsto_lintegral_of_dominated_convergence _ hF_meas h_bound h_fin h_lim, end lemma mem_β„’p_approx_on [borel_space E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : mem_β„’p f p ΞΌ) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (hiβ‚€ : mem_β„’p (Ξ» x, yβ‚€) p ΞΌ) (n : β„•) : mem_β„’p (approx_on f fmeas s yβ‚€ hβ‚€ n) p ΞΌ := begin refine ⟨(approx_on f fmeas s yβ‚€ hβ‚€ n).ae_measurable, _⟩, suffices : snorm (Ξ» x, approx_on f fmeas s yβ‚€ hβ‚€ n x - yβ‚€) p ΞΌ < ⊀, { have : mem_β„’p (Ξ» x, approx_on f fmeas s yβ‚€ hβ‚€ n x - yβ‚€) p ΞΌ := ⟨(approx_on f fmeas s yβ‚€ hβ‚€ n - const Ξ² yβ‚€).ae_measurable, this⟩, convert snorm_add_lt_top this hiβ‚€, ext x, simp }, -- We don't necessarily have `mem_β„’p (Ξ» x, f x - yβ‚€) p ΞΌ`, because the `ae_measurable` part -- requires `ae_measurable.add`, which requires second-countability have hf' : mem_β„’p (Ξ» x, βˆ₯f x - yβ‚€βˆ₯) p ΞΌ, { have h_meas : measurable (Ξ» x, βˆ₯f x - yβ‚€βˆ₯), { simp only [← dist_eq_norm], exact (continuous_id.dist continuous_const).measurable.comp fmeas }, refine ⟨h_meas.ae_measurable, _⟩, rw snorm_norm, convert snorm_add_lt_top hf hiβ‚€.neg, ext x, simp [sub_eq_add_neg] }, have : βˆ€α΅ x βˆ‚ΞΌ, βˆ₯approx_on f fmeas s yβ‚€ hβ‚€ n x - yβ‚€βˆ₯ ≀ βˆ₯(βˆ₯f x - yβ‚€βˆ₯ + βˆ₯f x - yβ‚€βˆ₯)βˆ₯, { refine eventually_of_forall _, intros x, convert norm_approx_on_yβ‚€_le fmeas hβ‚€ x n, rw [real.norm_eq_abs, abs_of_nonneg], exact add_nonneg (norm_nonneg _) (norm_nonneg _) }, calc snorm (Ξ» x, approx_on f fmeas s yβ‚€ hβ‚€ n x - yβ‚€) p ΞΌ ≀ snorm (Ξ» x, βˆ₯f x - yβ‚€βˆ₯ + βˆ₯f x - yβ‚€βˆ₯) p ΞΌ : snorm_mono_ae this ... < ⊀ : snorm_add_lt_top hf' hf', end lemma tendsto_approx_on_univ_Lp_snorm [opens_measurable_space E] [second_countable_topology E] {f : Ξ² β†’ E} (hp_ne_top : p β‰  ∞) {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : snorm f p ΞΌ < ∞) : tendsto (Ξ» n, snorm (approx_on f fmeas univ 0 trivial n - f) p ΞΌ) at_top (𝓝 0) := tendsto_approx_on_Lp_snorm fmeas trivial hp_ne_top (by simp) (by simpa using hf) lemma mem_β„’p_approx_on_univ [borel_space E] [second_countable_topology E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : mem_β„’p f p ΞΌ) (n : β„•) : mem_β„’p (approx_on f fmeas univ 0 trivial n) p ΞΌ := mem_β„’p_approx_on fmeas hf (mem_univ _) zero_mem_β„’p n lemma tendsto_approx_on_univ_Lp [borel_space E] [second_countable_topology E] {f : Ξ² β†’ E} [hp : fact (1 ≀ p)] (hp_ne_top : p β‰  ∞) {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : mem_β„’p f p ΞΌ) : tendsto (Ξ» n, (mem_β„’p_approx_on_univ fmeas hf n).to_Lp (approx_on f fmeas univ 0 trivial n)) at_top (𝓝 (hf.to_Lp f)) := by simp [Lp.tendsto_Lp_iff_tendsto_β„’p'', tendsto_approx_on_univ_Lp_snorm hp_ne_top fmeas hf.2] end Lp /-! ### L1 approximation by simple functions -/ section integrable variables [measurable_space Ξ²] variables [measurable_space E] [normed_group E] lemma tendsto_approx_on_L1_nnnorm [opens_measurable_space E] {f : Ξ² β†’ E} (hf : measurable f) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] {ΞΌ : measure Ξ²} (hΞΌ : βˆ€α΅ x βˆ‚ΞΌ, f x ∈ closure s) (hi : has_finite_integral (Ξ» x, f x - yβ‚€) ΞΌ) : tendsto (Ξ» n, ∫⁻ x, βˆ₯approx_on f hf s yβ‚€ hβ‚€ n x - f xβˆ₯β‚Š βˆ‚ΞΌ) at_top (𝓝 0) := by simpa [snorm_one_eq_lintegral_nnnorm] using tendsto_approx_on_Lp_snorm hf hβ‚€ one_ne_top hΞΌ (by simpa [snorm_one_eq_lintegral_nnnorm] using hi) lemma integrable_approx_on [borel_space E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : integrable f ΞΌ) {s : set E} {yβ‚€ : E} (hβ‚€ : yβ‚€ ∈ s) [separable_space s] (hiβ‚€ : integrable (Ξ» x, yβ‚€) ΞΌ) (n : β„•) : integrable (approx_on f fmeas s yβ‚€ hβ‚€ n) ΞΌ := begin rw ← mem_β„’p_one_iff_integrable at hf hiβ‚€ ⊒, exact mem_β„’p_approx_on fmeas hf hβ‚€ hiβ‚€ n, end lemma tendsto_approx_on_univ_L1_nnnorm [opens_measurable_space E] [second_countable_topology E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : integrable f ΞΌ) : tendsto (Ξ» n, ∫⁻ x, βˆ₯approx_on f fmeas univ 0 trivial n x - f xβˆ₯β‚Š βˆ‚ΞΌ) at_top (𝓝 0) := tendsto_approx_on_L1_nnnorm fmeas trivial (by simp) (by simpa using hf.2) lemma integrable_approx_on_univ [borel_space E] [second_countable_topology E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : integrable f ΞΌ) (n : β„•) : integrable (approx_on f fmeas univ 0 trivial n) ΞΌ := integrable_approx_on fmeas hf _ (integrable_zero _ _ _) n local attribute [instance] fact_one_le_one_ennreal lemma tendsto_approx_on_univ_L1 [borel_space E] [second_countable_topology E] {f : Ξ² β†’ E} {ΞΌ : measure Ξ²} (fmeas : measurable f) (hf : integrable f ΞΌ) : tendsto (Ξ» n, integrable.to_L1 (approx_on f fmeas univ 0 trivial n) (integrable_approx_on_univ fmeas hf n)) at_top (𝓝 $ hf.to_L1 f) := tendsto_approx_on_univ_Lp one_ne_top fmeas _ end integrable section simple_func_properties variables [measurable_space Ξ±] variables [normed_group E] [measurable_space E] [normed_group F] variables {ΞΌ : measure Ξ±} {p : ℝβ‰₯0∞} /-! ### Properties of simple functions in `Lp` spaces A simple function `f : Ξ± β†’β‚› E` into a normed group `E` verifies, for a measure `ΞΌ`: - `mem_β„’p f 0 ΞΌ` and `mem_β„’p f ∞ ΞΌ`, since `f` is a.e.-measurable and bounded, - for `0 < p < ∞`, `mem_β„’p f p ΞΌ ↔ integrable f ΞΌ ↔ f.fin_meas_supp ΞΌ ↔ βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞`. -/ lemma exists_forall_norm_le (f : Ξ± β†’β‚› F) : βˆƒ C, βˆ€ x, βˆ₯f xβˆ₯ ≀ C := exists_forall_le (f.map (Ξ» x, βˆ₯xβˆ₯)) lemma mem_β„’p_zero (f : Ξ± β†’β‚› E) (ΞΌ : measure Ξ±) : mem_β„’p f 0 ΞΌ := mem_β„’p_zero_iff_ae_measurable.mpr f.ae_measurable lemma mem_β„’p_top (f : Ξ± β†’β‚› E) (ΞΌ : measure Ξ±) : mem_β„’p f ∞ ΞΌ := let ⟨C, hfC⟩ := f.exists_forall_norm_le in mem_β„’p_top_of_bound f.ae_measurable C $ eventually_of_forall hfC protected lemma snorm'_eq {p : ℝ} (f : Ξ± β†’β‚› F) (ΞΌ : measure Ξ±) : snorm' f p ΞΌ = (βˆ‘ y in f.range, (nnnorm y : ℝβ‰₯0∞) ^ p * ΞΌ (f ⁻¹' {y})) ^ (1/p) := have h_map : (Ξ» a, (nnnorm (f a) : ℝβ‰₯0∞) ^ p) = f.map (Ξ» a : F, (nnnorm a : ℝβ‰₯0∞) ^ p), by simp, by rw [snorm', h_map, lintegral_eq_lintegral, map_lintegral] lemma measure_preimage_lt_top_of_mem_β„’p (hp_pos : 0 < p) (hp_ne_top : p β‰  ∞) (f : Ξ± β†’β‚› E) (hf : mem_β„’p f p ΞΌ) (y : E) (hy_ne : y β‰  0) : ΞΌ (f ⁻¹' {y}) < ∞ := begin have hp_pos_real : 0 < p.to_real, from ennreal.to_real_pos_iff.mpr ⟨hp_pos, hp_ne_top⟩, have hf_snorm := mem_β„’p.snorm_lt_top hf, rw [snorm_eq_snorm' hp_pos.ne.symm hp_ne_top, f.snorm'_eq, ← @ennreal.lt_rpow_one_div_iff _ _ (1 / p.to_real) (by simp [hp_pos_real]), @ennreal.top_rpow_of_pos (1 / (1 / p.to_real)) (by simp [hp_pos_real]), ennreal.sum_lt_top_iff] at hf_snorm, by_cases hyf : y ∈ f.range, swap, { suffices h_empty : f ⁻¹' {y} = βˆ…, by { rw [h_empty, measure_empty], exact ennreal.coe_lt_top, }, ext1 x, rw [set.mem_preimage, set.mem_singleton_iff, mem_empty_eq, iff_false], refine Ξ» hxy, hyf _, rw [mem_range, set.mem_range], exact ⟨x, hxy⟩, }, specialize hf_snorm y hyf, rw ennreal.mul_lt_top_iff at hf_snorm, cases hf_snorm, { exact hf_snorm.2, }, cases hf_snorm, { refine absurd _ hy_ne, simpa [hp_pos_real] using hf_snorm, }, { simp [hf_snorm], }, end lemma mem_β„’p_of_finite_measure_preimage (p : ℝβ‰₯0∞) {f : Ξ± β†’β‚› E} (hf : βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞) : mem_β„’p f p ΞΌ := begin by_cases hp0 : p = 0, { rw [hp0, mem_β„’p_zero_iff_ae_measurable], exact f.ae_measurable, }, by_cases hp_top : p = ∞, { rw hp_top, exact mem_β„’p_top f ΞΌ, }, refine ⟨f.ae_measurable, _⟩, rw [snorm_eq_snorm' hp0 hp_top, f.snorm'_eq], refine ennreal.rpow_lt_top_of_nonneg (by simp) (ennreal.sum_lt_top_iff.mpr (Ξ» y hy, _)).ne, by_cases hy0 : y = 0, { simp [hy0, ennreal.to_real_pos_iff.mpr ⟨lt_of_le_of_ne (zero_le _) (ne.symm hp0), hp_top⟩], }, { refine ennreal.mul_lt_top _ (hf y hy0), exact ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg ennreal.coe_ne_top, }, end lemma mem_β„’p_iff {f : Ξ± β†’β‚› E} (hp_pos : 0 < p) (hp_ne_top : p β‰  ∞) : mem_β„’p f p ΞΌ ↔ βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞ := ⟨λ h, measure_preimage_lt_top_of_mem_β„’p hp_pos hp_ne_top f h, Ξ» h, mem_β„’p_of_finite_measure_preimage p h⟩ lemma integrable_iff {f : Ξ± β†’β‚› E} : integrable f ΞΌ ↔ βˆ€ y β‰  0, ΞΌ (f ⁻¹' {y}) < ∞ := mem_β„’p_one_iff_integrable.symm.trans $ mem_β„’p_iff ennreal.zero_lt_one ennreal.coe_ne_top lemma mem_β„’p_iff_integrable {f : Ξ± β†’β‚› E} (hp_pos : 0 < p) (hp_ne_top : p β‰  ∞) : mem_β„’p f p ΞΌ ↔ integrable f ΞΌ := (mem_β„’p_iff hp_pos hp_ne_top).trans integrable_iff.symm lemma mem_β„’p_iff_fin_meas_supp {f : Ξ± β†’β‚› E} (hp_pos : 0 < p) (hp_ne_top : p β‰  ∞) : mem_β„’p f p ΞΌ ↔ f.fin_meas_supp ΞΌ := (mem_β„’p_iff hp_pos hp_ne_top).trans fin_meas_supp_iff.symm lemma integrable_iff_fin_meas_supp {f : Ξ± β†’β‚› E} : integrable f ΞΌ ↔ f.fin_meas_supp ΞΌ := integrable_iff.trans fin_meas_supp_iff.symm lemma fin_meas_supp.integrable {f : Ξ± β†’β‚› E} (h : f.fin_meas_supp ΞΌ) : integrable f ΞΌ := integrable_iff_fin_meas_supp.2 h lemma integrable_pair [measurable_space F] {f : Ξ± β†’β‚› E} {g : Ξ± β†’β‚› F} : integrable f ΞΌ β†’ integrable g ΞΌ β†’ integrable (pair f g) ΞΌ := by simpa only [integrable_iff_fin_meas_supp] using fin_meas_supp.pair lemma mem_β„’p_of_finite_measure (f : Ξ± β†’β‚› E) (p : ℝβ‰₯0∞) (ΞΌ : measure Ξ±) [finite_measure ΞΌ] : mem_β„’p f p ΞΌ := let ⟨C, hfC⟩ := f.exists_forall_norm_le in mem_β„’p.of_bound f.ae_measurable C $ eventually_of_forall hfC lemma integrable_of_finite_measure [finite_measure ΞΌ] (f : Ξ± β†’β‚› E) : integrable f ΞΌ := mem_β„’p_one_iff_integrable.mp (f.mem_β„’p_of_finite_measure 1 ΞΌ) lemma measure_preimage_lt_top_of_integrable (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) {x : E} (hx : x β‰  0) : ΞΌ (f ⁻¹' {x}) < ∞ := integrable_iff.mp hf x hx end simple_func_properties end simple_func /-! Construction of the space of `L1` simple functions, and its dense embedding into `L1`. -/ namespace L1 open ae_eq_fun variables [measurable_space Ξ±] [normed_group E] [second_countable_topology E] [measurable_space E] [borel_space E] [normed_group F] [second_countable_topology F] [measurable_space F] [borel_space F] {ΞΌ : measure Ξ±} variables (Ξ± E ΞΌ) /-- `L1.simple_func` is a subspace of L1 consisting of equivalence classes of an integrable simple function. -/ def simple_func : add_subgroup (Lp E 1 ΞΌ) := { carrier := {f : Ξ± →₁[ΞΌ] E | βˆƒ (s : Ξ± β†’β‚› E), (ae_eq_fun.mk s s.ae_measurable : Ξ± β†’β‚˜[ΞΌ] E) = f}, zero_mem' := ⟨0, rfl⟩, add_mem' := Ξ» f g ⟨s, hs⟩ ⟨t, ht⟩, ⟨s + t, by simp only [←hs, ←ht, mk_add_mk, add_subgroup.coe_add, mk_eq_mk, simple_func.coe_add]⟩, neg_mem' := Ξ» f ⟨s, hs⟩, ⟨-s, by simp only [←hs, neg_mk, simple_func.coe_neg, mk_eq_mk, add_subgroup.coe_neg]⟩ } variables {Ξ± E ΞΌ} notation Ξ± ` →₁ₛ[`:25 ΞΌ `] ` E := measure_theory.L1.simple_func Ξ± E ΞΌ namespace simple_func section instances /-! Simple functions in L1 space form a `normed_space`. -/ instance : has_coe (Ξ± →₁ₛ[ΞΌ] E) (Ξ± →₁[ΞΌ] E) := coe_subtype instance : has_coe_to_fun (Ξ± →₁ₛ[ΞΌ] E) := ⟨λ f, Ξ± β†’ E, Ξ» f, ⇑(f : Ξ± →₁[ΞΌ] E)⟩ @[simp, norm_cast] lemma coe_coe (f : Ξ± →₁ₛ[ΞΌ] E) : ⇑(f : Ξ± →₁[ΞΌ] E) = f := rfl protected lemma eq {f g : Ξ± →₁ₛ[ΞΌ] E} : (f : Ξ± →₁[ΞΌ] E) = (g : Ξ± →₁[ΞΌ] E) β†’ f = g := subtype.eq protected lemma eq' {f g : Ξ± →₁ₛ[ΞΌ] E} : (f : Ξ± β†’β‚˜[ΞΌ] E) = (g : Ξ± β†’β‚˜[ΞΌ] E) β†’ f = g := subtype.eq ∘ subtype.eq @[norm_cast] protected lemma eq_iff {f g : Ξ± →₁ₛ[ΞΌ] E} : (f : Ξ± →₁[ΞΌ] E) = g ↔ f = g := subtype.ext_iff.symm @[norm_cast] protected lemma eq_iff' {f g : Ξ± →₁ₛ[ΞΌ] E} : (f : Ξ± β†’β‚˜[ΞΌ] E) = g ↔ f = g := iff.intro (simple_func.eq') (congr_arg _) /-- L1 simple functions forms a `normed_group`, with the metric being inherited from L1 space, i.e., `dist f g = ennreal.to_real (∫⁻ a, edist (f a) (g a)`). Not declared as an instance as `Ξ± →₁ₛ[ΞΌ] Ξ²` will only be useful in the construction of the Bochner integral. -/ protected def normed_group : normed_group (Ξ± →₁ₛ[ΞΌ] E) := by apply_instance local attribute [instance] simple_func.normed_group /-- Functions `Ξ± →₁ₛ[ΞΌ] E` form an additive commutative group. -/ instance : inhabited (Ξ± →₁ₛ[ΞΌ] E) := ⟨0⟩ @[simp, norm_cast] lemma coe_zero : ((0 : Ξ± →₁ₛ[ΞΌ] E) : Ξ± →₁[ΞΌ] E) = 0 := rfl @[simp, norm_cast] lemma coe_add (f g : Ξ± →₁ₛ[ΞΌ] E) : ((f + g : Ξ± →₁ₛ[ΞΌ] E) : Ξ± →₁[ΞΌ] E) = f + g := rfl @[simp, norm_cast] lemma coe_neg (f : Ξ± →₁ₛ[ΞΌ] E) : ((-f : Ξ± →₁ₛ[ΞΌ] E) : Ξ± →₁[ΞΌ] E) = -f := rfl @[simp, norm_cast] lemma coe_sub (f g : Ξ± →₁ₛ[ΞΌ] E) : ((f - g : Ξ± →₁ₛ[ΞΌ] E) : Ξ± →₁[ΞΌ] E) = f - g := rfl @[simp] lemma edist_eq (f g : Ξ± →₁ₛ[ΞΌ] E) : edist f g = edist (f : Ξ± →₁[ΞΌ] E) (g : Ξ± →₁[ΞΌ] E) := rfl @[simp] lemma dist_eq (f g : Ξ± →₁ₛ[ΞΌ] E) : dist f g = dist (f : Ξ± →₁[ΞΌ] E) (g : Ξ± →₁[ΞΌ] E) := rfl lemma norm_eq (f : Ξ± →₁ₛ[ΞΌ] E) : βˆ₯fβˆ₯ = βˆ₯(f : Ξ± →₁[ΞΌ] E)βˆ₯ := rfl variables [normed_field π•œ] [normed_space π•œ E] [measurable_space π•œ] [opens_measurable_space π•œ] /-- Not declared as an instance as `Ξ± →₁ₛ[ΞΌ] E` will only be useful in the construction of the Bochner integral. -/ protected def has_scalar : has_scalar π•œ (Ξ± →₁ₛ[ΞΌ] E) := ⟨λk f, ⟨k β€’ f, begin rcases f with ⟨f, ⟨s, hs⟩⟩, use k β€’ s, apply eq.trans (smul_mk k s s.ae_measurable).symm _, rw hs, refl, end ⟩⟩ local attribute [instance, priority 10000] simple_func.has_scalar @[simp, norm_cast] lemma coe_smul (c : π•œ) (f : Ξ± →₁ₛ[ΞΌ] E) : ((c β€’ f : Ξ± →₁ₛ[ΞΌ] E) : Ξ± →₁[ΞΌ] E) = c β€’ (f : Ξ± →₁[ΞΌ] E) := rfl /-- Not declared as an instance as `Ξ± →₁ₛ[ΞΌ] E` will only be useful in the construction of the Bochner integral. -/ protected def module : module π•œ (Ξ± →₁ₛ[ΞΌ] E) := { one_smul := Ξ»f, simple_func.eq (by { simp only [coe_smul], exact one_smul _ _ }), mul_smul := Ξ»x y f, simple_func.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }), smul_add := Ξ»x f g, simple_func.eq (by { simp only [coe_smul], exact smul_add _ _ _ }), smul_zero := Ξ»x, simple_func.eq (by { simp only [coe_smul], exact smul_zero _ }), add_smul := Ξ»x y f, simple_func.eq (by { simp only [coe_smul], exact add_smul _ _ _ }), zero_smul := Ξ»f, simple_func.eq (by { simp only [coe_smul], exact zero_smul _ _ }) } local attribute [instance] simple_func.normed_group simple_func.module /-- Not declared as an instance as `Ξ± →₁ₛ[ΞΌ] E` will only be useful in the construction of the Bochner integral. -/ protected def normed_space : normed_space π•œ (Ξ± →₁ₛ[ΞΌ] E) := ⟨ Ξ»c f, by { rw [norm_eq, norm_eq, coe_smul, norm_smul] } ⟩ end instances local attribute [instance] simple_func.normed_group simple_func.normed_space section to_L1 /-- Construct the equivalence class `[f]` of an integrable simple function `f`. -/ @[reducible] def to_L1 (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) : (Ξ± →₁ₛ[ΞΌ] E) := ⟨hf.to_L1 f, ⟨f, rfl⟩⟩ lemma to_L1_eq_to_L1 (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) : (to_L1 f hf : Ξ± →₁[ΞΌ] E) = hf.to_L1 f := rfl lemma to_L1_eq_mk (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) : (to_L1 f hf : Ξ± β†’β‚˜[ΞΌ] E) = ae_eq_fun.mk f f.ae_measurable := rfl lemma to_L1_zero : to_L1 (0 : Ξ± β†’β‚› E) (integrable_zero Ξ± E ΞΌ) = 0 := rfl lemma to_L1_add (f g : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) : to_L1 (f + g) (hf.add hg) = to_L1 f hf + to_L1 g hg := rfl lemma to_L1_neg (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) : to_L1 (-f) hf.neg = -to_L1 f hf := rfl lemma to_L1_sub (f g : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) (hg : integrable g ΞΌ) : to_L1 (f - g) (hf.sub hg) = to_L1 f hf - to_L1 g hg := by { simp only [sub_eq_add_neg, ← to_L1_neg, ← to_L1_add], refl } variables [normed_field π•œ] [normed_space π•œ E] [measurable_space π•œ] [opens_measurable_space π•œ] lemma to_L1_smul (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) (c : π•œ) : to_L1 (c β€’ f) (hf.smul c) = c β€’ to_L1 f hf := rfl lemma norm_to_L1 (f : Ξ± β†’β‚› E) (hf : integrable f ΞΌ) : βˆ₯to_L1 f hfβˆ₯ = ennreal.to_real (∫⁻ a, edist (f a) 0 βˆ‚ΞΌ) := by simp [to_L1, integrable.norm_to_L1] end to_L1 section to_simple_func /-- Find a representative of a `L1.simple_func`. -/ def to_simple_func (f : Ξ± →₁ₛ[ΞΌ] E) : Ξ± β†’β‚› E := classical.some f.2 /-- `(to_simple_func f)` is measurable. -/ @[measurability] protected lemma measurable (f : Ξ± →₁ₛ[ΞΌ] E) : measurable (to_simple_func f) := (to_simple_func f).measurable @[measurability] protected lemma ae_measurable (f : Ξ± →₁ₛ[ΞΌ] E) : ae_measurable (to_simple_func f) ΞΌ := (simple_func.measurable f).ae_measurable /-- `to_simple_func f` is integrable. -/ protected lemma integrable (f : Ξ± →₁ₛ[ΞΌ] E) : integrable (to_simple_func f) ΞΌ := begin apply (integrable_mk (simple_func.ae_measurable f)).1, convert integrable_coe_fn f.val, exact classical.some_spec f.2 end lemma to_L1_to_simple_func (f : Ξ± →₁ₛ[ΞΌ] E) : to_L1 (to_simple_func f) (simple_func.integrable f) = f := by { rw ← simple_func.eq_iff', exact classical.some_spec f.2 } lemma to_simple_func_to_L1 (f : Ξ± β†’β‚› E) (hfi : integrable f ΞΌ) : to_simple_func (to_L1 f hfi) =ᡐ[ΞΌ] f := by { rw ← mk_eq_mk, exact classical.some_spec (to_L1 f hfi).2 } lemma to_simple_func_eq_to_fun (f : Ξ± →₁ₛ[ΞΌ] E) : to_simple_func f =ᡐ[ΞΌ] f := begin simp_rw [← integrable.to_L1_eq_to_L1_iff (to_simple_func f) f (simple_func.integrable f) (integrable_coe_fn ↑f), subtype.ext_iff], convert classical.some_spec f.coe_prop, exact integrable.to_L1_coe_fn _ _, end variables (E ΞΌ) lemma zero_to_simple_func : to_simple_func (0 : Ξ± →₁ₛ[ΞΌ] E) =ᡐ[ΞΌ] 0 := begin filter_upwards [to_simple_func_eq_to_fun (0 : Ξ± →₁ₛ[ΞΌ] E), Lp.coe_fn_zero E 1 ΞΌ], assume a h₁ hβ‚‚, rwa h₁, end variables {E ΞΌ} lemma add_to_simple_func (f g : Ξ± →₁ₛ[ΞΌ] E) : to_simple_func (f + g) =ᡐ[ΞΌ] to_simple_func f + to_simple_func g := begin filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, Lp.coe_fn_add (f : Ξ± →₁[ΞΌ] E) g], assume a, simp only [← coe_coe, coe_add, pi.add_apply], iterate 4 { assume h, rw h } end lemma neg_to_simple_func (f : Ξ± →₁ₛ[ΞΌ] E) : to_simple_func (-f) =ᡐ[ΞΌ] - to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f, Lp.coe_fn_neg (f : Ξ± →₁[ΞΌ] E)], assume a, simp only [pi.neg_apply, coe_neg, ← coe_coe], repeat { assume h, rw h } end lemma sub_to_simple_func (f g : Ξ± →₁ₛ[ΞΌ] E) : to_simple_func (f - g) =ᡐ[ΞΌ] to_simple_func f - to_simple_func g := begin filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, Lp.coe_fn_sub (f : Ξ± →₁[ΞΌ] E) g], assume a, simp only [coe_sub, pi.sub_apply, ← coe_coe], repeat { assume h, rw h } end variables [normed_field π•œ] [normed_space π•œ E] [measurable_space π•œ] [opens_measurable_space π•œ] lemma smul_to_simple_func (k : π•œ) (f : Ξ± →₁ₛ[ΞΌ] E) : to_simple_func (k β€’ f) =ᡐ[ΞΌ] k β€’ to_simple_func f := begin filter_upwards [to_simple_func_eq_to_fun (k β€’ f), to_simple_func_eq_to_fun f, Lp.coe_fn_smul k (f : Ξ± →₁[ΞΌ] E)], assume a, simp only [pi.smul_apply, coe_smul, ← coe_coe], repeat { assume h, rw h } end lemma lintegral_edist_to_simple_func_lt_top (f g : Ξ± →₁ₛ[ΞΌ] E) : ∫⁻ (x : Ξ±), edist (to_simple_func f x) (to_simple_func g x) βˆ‚ΞΌ < ∞ := begin rw lintegral_rwβ‚‚ (to_simple_func_eq_to_fun f) (to_simple_func_eq_to_fun g), exact lintegral_edist_lt_top (integrable_coe_fn _) (integrable_coe_fn _) end lemma dist_to_simple_func (f g : Ξ± →₁ₛ[ΞΌ] E) : dist f g = ennreal.to_real (∫⁻ x, edist (to_simple_func f x) (to_simple_func g x) βˆ‚ΞΌ) := begin rw [dist_eq, L1.dist_def, ennreal.to_real_eq_to_real], { rw lintegral_rwβ‚‚, repeat { exact ae_eq_symm (to_simple_func_eq_to_fun _) } }, { exact lintegral_edist_lt_top (integrable_coe_fn _) (integrable_coe_fn _) }, { exact lintegral_edist_to_simple_func_lt_top _ _ } end lemma norm_to_simple_func (f : Ξ± →₁ₛ[ΞΌ] E) : βˆ₯fβˆ₯ = ennreal.to_real (∫⁻ (a : Ξ±), nnnorm ((to_simple_func f) a) βˆ‚ΞΌ) := calc βˆ₯fβˆ₯ = ennreal.to_real (∫⁻x, edist ((to_simple_func f) x) (to_simple_func (0 : Ξ± →₁ₛ[ΞΌ] E) x) βˆ‚ΞΌ) : begin rw [← dist_zero_right, dist_to_simple_func] end ... = ennreal.to_real (∫⁻ (x : Ξ±), (coe ∘ nnnorm) ((to_simple_func f) x) βˆ‚ΞΌ) : begin rw lintegral_nnnorm_eq_lintegral_edist, have : ∫⁻ x, edist ((to_simple_func f) x) ((to_simple_func (0 : Ξ± →₁ₛ[ΞΌ] E)) x) βˆ‚ΞΌ = ∫⁻ x, edist ((to_simple_func f) x) 0 βˆ‚ΞΌ, { refine lintegral_congr_ae ((zero_to_simple_func E ΞΌ).mono (Ξ» a h, _)), rw [h, pi.zero_apply] }, rw [ennreal.to_real_eq_to_real], { exact this }, { exact lintegral_edist_to_simple_func_lt_top _ _ }, { rw ← this, exact lintegral_edist_to_simple_func_lt_top _ _ } end end to_simple_func section coe_to_L1 protected lemma uniform_continuous : uniform_continuous (coe : (Ξ± →₁ₛ[ΞΌ] E) β†’ (Ξ± →₁[ΞΌ] E)) := uniform_continuous_comap protected lemma uniform_embedding : uniform_embedding (coe : (Ξ± →₁ₛ[ΞΌ] E) β†’ (Ξ± →₁[ΞΌ] E)) := uniform_embedding_comap subtype.val_injective protected lemma uniform_inducing : uniform_inducing (coe : (Ξ± →₁ₛ[ΞΌ] E) β†’ (Ξ± →₁[ΞΌ] E)) := simple_func.uniform_embedding.to_uniform_inducing protected lemma dense_embedding : dense_embedding (coe : (Ξ± →₁ₛ[ΞΌ] E) β†’ (Ξ± →₁[ΞΌ] E)) := begin apply simple_func.uniform_embedding.dense_embedding, assume f, rw mem_closure_iff_seq_limit, have hfi' : integrable f ΞΌ := integrable_coe_fn f, refine ⟨λ n, ↑(to_L1 (simple_func.approx_on f (Lp.measurable f) univ 0 trivial n) (simple_func.integrable_approx_on_univ (Lp.measurable f) hfi' n)), Ξ» n, mem_range_self _, _⟩, convert simple_func.tendsto_approx_on_univ_L1 (Lp.measurable f) hfi', rw integrable.to_L1_coe_fn end protected lemma dense_inducing : dense_inducing (coe : (Ξ± →₁ₛ[ΞΌ] E) β†’ (Ξ± →₁[ΞΌ] E)) := simple_func.dense_embedding.to_dense_inducing protected lemma dense_range : dense_range (coe : (Ξ± →₁ₛ[ΞΌ] E) β†’ (Ξ± →₁[ΞΌ] E)) := simple_func.dense_inducing.dense variables [normed_field π•œ] [normed_space π•œ E] [measurable_space π•œ] [opens_measurable_space π•œ] variables (Ξ± E π•œ) /-- The uniform and dense embedding of L1 simple functions into L1 functions. -/ def coe_to_L1 : (Ξ± →₁ₛ[ΞΌ] E) β†’L[π•œ] (Ξ± →₁[ΞΌ] E) := { to_fun := (coe : (Ξ± →₁ₛ[ΞΌ] E) β†’ (Ξ± →₁[ΞΌ] E)), map_add' := Ξ»f g, rfl, map_smul' := Ξ»k f, rfl, cont := L1.simple_func.uniform_continuous.continuous, } variables {Ξ± E π•œ} end coe_to_L1 end simple_func end L1 variables [measurable_space Ξ±] [normed_group E] [measurable_space E] {f g : Ξ± β†’ E} {s t : set Ξ±} {ΞΌ Ξ½ : measure Ξ±} {l l' : filter Ξ±} [borel_space E] [second_countable_topology E] /-- To prove something for an arbitrary integrable function in a second countable Borel normed group, it suffices to show that * the property holds for (multiples of) characteristic functions; * is closed under addition; * the set of functions in the `LΒΉ` space for which the property holds is closed. * the property is closed under the almost-everywhere equal relation. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_add` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`). -/ @[elab_as_eliminator] lemma integrable.induction (P : (Ξ± β†’ E) β†’ Prop) (h_ind : βˆ€ (c : E) ⦃s⦄, measurable_set s β†’ ΞΌ s < ∞ β†’ P (s.indicator (Ξ» _, c))) (h_add : βˆ€ ⦃f g : Ξ± β†’ E⦄, disjoint (support f) (support g) β†’ integrable f ΞΌ β†’ integrable g ΞΌ β†’ P f β†’ P g β†’ P (f + g)) (h_closed : is_closed {f : Ξ± →₁[ΞΌ] E | P f} ) (h_ae : βˆ€ ⦃f g⦄, f =ᡐ[ΞΌ] g β†’ integrable f ΞΌ β†’ P f β†’ P g) : βˆ€ ⦃f : Ξ± β†’ E⦄ (hf : integrable f ΞΌ), P f := begin have : βˆ€ (f : simple_func Ξ± E), integrable f ΞΌ β†’ P f, { refine simple_func.induction _ _, { intros c s hs h, dsimp only [simple_func.coe_const, simple_func.const_zero, piecewise_eq_indicator, simple_func.coe_zero, simple_func.coe_piecewise] at h ⊒, by_cases hc : c = 0, { subst hc, convert h_ind 0 measurable_set.empty (by simp) using 1, simp [const] }, apply h_ind c hs, have : (nnnorm c : ℝβ‰₯0∞) * ΞΌ s < ∞, { have := @comp_indicator _ _ _ _ (Ξ» x : E, (nnnorm x : ℝβ‰₯0∞)) (const Ξ± c) s, dsimp only at this, have h' := h.has_finite_integral, simpa [has_finite_integral, this, lintegral_indicator, hs] using h' }, exact ennreal.lt_top_of_mul_lt_top_right this (by simp [hc]) }, { intros f g hfg hf hg int_fg, rw [simple_func.coe_add, integrable_add hfg f.measurable g.measurable] at int_fg, refine h_add hfg int_fg.1 int_fg.2 (hf int_fg.1) (hg int_fg.2) } }, have : βˆ€ (f : Ξ± →₁ₛ[ΞΌ] E), P f, { intro f, exact h_ae (L1.simple_func.to_simple_func_eq_to_fun f) (L1.simple_func.integrable f) (this (L1.simple_func.to_simple_func f) (L1.simple_func.integrable f)) }, have : βˆ€ (f : Ξ± →₁[ΞΌ] E), P f := Ξ» f, L1.simple_func.dense_range.induction_on f h_closed this, exact Ξ» f hf, h_ae hf.coe_fn_to_L1 (L1.integrable_coe_fn _) (this (hf.to_L1 f)), end end measure_theory
fa8d44af50b4e1b4fd60dfda0a746b2d36326f52
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/local_context_auto.lean
9829e6b0e9a518031eb350ef03e888a280fbc59f
[]
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
179
lean
import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.name import Mathlib.Lean3Lib.init.meta.expr import Mathlib.Lean3Lib.init.data.option.basic namespace Mathlib end Mathlib
a1e50fd80016f18f4dccef121ddc8725694ec189
ad3e8f15221a986da27c99f371922c0b3f5792b6
/src/week06/solutions/e01_Z.lean
b90ec59cc6bd389c821abe108883e43be183c5f1
[]
no_license
VArtem/lean-itmo
a0e1424c8cc4c2de2ac85ab6fd4a12d80e9b85f1
dc44cd06f9f5b984d051831b3aaa7364e64c2dc4
refs/heads/main
1,683,761,214,467
1,622,821,295,000
1,622,821,295,000
357,236,048
12
0
null
null
null
null
UTF-8
Lean
false
false
11,041
lean
import tactic -- Основано Π½Π° https://github.com/ImperialCollegeLondon/formalising-mathematics/tree/master/src/week_7 -- Π― ΡƒΠ±Ρ€Π°Π» довольно ΠΌΠ½ΠΎΠ³ΠΎ ΠΌΠ°Ρ‚Π΅Ρ€ΠΈΠ°Π»Π° ΠΎΡ‚Ρ‚ΡƒΠ΄Π°, посмотритС ΠΏΠΎ возмоТности ΠΎΡ€ΠΈΠ³ΠΈΠ½Π°Π», Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Kevin ΡƒΡ…ΠΎΠ΄ΠΈΡ‚ Π³ΠΎΡ€Π°Π·Π΄ΠΎ Π³Π»ΡƒΠ±ΠΆΠ΅ Π² ΠΊΠΎΠ½Ρ†Π΅ΠΏΡ†ΠΈΡŽ `quotient` -- Π‘ΠΎΠ»ΡŒΡˆΠ΅ Π² ΠΊΠ½ΠΈΠ³Π΅ Theorem Proving in Lean: https://leanprover.github.io/theorem_proving_in_lean/axioms_and_computation.html#quotients -- Π’ этом Ρ„Π°ΠΉΠ»Π΅ ΠΌΡ‹ ΠΈΠ·ΡƒΡ‡ΠΈΠΌ ΠΊΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ†ΠΈΡŽ `quotient` -- И построим Ρ†Π΅Π»Ρ‹Π΅ числа ΠΊΠ°ΠΊ классы эквивалСнтности ΠΏΠ°Ρ€ Π½Π°Ρ‚ΡƒΡ€Π°Π»ΡŒΠ½Ρ‹Ρ… чисСл (a, b) -- Π“Π΄Π΅ (a, b) β‰ˆ (c, d) ↔ a + d = b + c -- Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ ΠΏΡ€ΠΈΠ±Π»ΠΈΠΆΠ΅Π½ΠΈΠΈ `quotient` ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠΏΠΈΡΠ°Ρ‚ΡŒ ΠΊΠ°ΠΊ "Ρ‚ΠΈΠΏ классов эквивалСнтности ΠΏΠΎ Π±ΠΈΠ½Π°Ρ€Π½ΠΎΠΌΡƒ ΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΡŽ" -- ΠŸΡƒΡΡ‚ΡŒ Ρƒ нас Π΅ΡΡ‚ΡŒ Ρ‚ΠΈΠΏ `Ξ±` ΠΈ ΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΠ΅ эквивалСнтности `r : Ξ± β†’ Ξ± β†’ Prop` -- `abbreviation` это просто синтаксичСский синоним abbreviation N2 := β„• Γ— β„• namespace N2 section product def foo : β„• Γ— β„• := (3, 4) #reduce (foo.fst, foo.snd) -- Аналогично ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΈΡΠ°Ρ‚ΡŒ `foo.1` ΠΈΠ»ΠΈ `foo.2` -- Π§Ρ‚ΠΎΠ±Ρ‹ Π΄ΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ равСнство Π΄Π²ΡƒΡ… ΠΏΠ°Ρ€, ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ `ext` example (X Y : Type) (s t : X Γ— Y) (h1 : s.fst = t.fst) (h2 : s.snd = t.snd) : s = t := begin ext, { exact h1 }, { exact h2 }, end -- Π§Ρ‚ΠΎΠ±Ρ‹ Ρ€Π°Π·Π±ΠΈΡ‚ΡŒ ΠΏΠ°Ρ€Ρƒ `x` Π½Π° Π΄Π²Π° Π½Π°Ρ‚ΡƒΡ€Π°Π»ΡŒΠ½Ρ‹Ρ… числа, ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ `cases x with a b` example (A B : Type) (x : A Γ— B) : x = (x.1, x.2) := begin cases x with a b, -- ⊒ (a, b) = ((a, b).fst, (a, b).snd) dsimp only, -- ΡƒΠΏΡ€ΠΎΡ‰Π°Π΅Ρ‚ `(a, b).fst` Π΄ΠΎ `a`. -- ⊒ (a, b) = (a, b) refl, end end product -- Как ΠΏΠΎΡΡ‚Ρ€ΠΎΠΈΡ‚ΡŒ Ρ†Π΅Π»Ρ‹Π΅ числа Π½Π° основС Π½Π°Ρ‚ΡƒΡ€Π°Π»ΡŒΠ½Ρ‹Ρ…? -- Бтандартная конструкция: Ρ†Π΅Π»ΠΎΠ΅ число Π»ΠΈΠ±ΠΎ `n : β„•`, Π»ΠΈΠ±ΠΎ `-n-1` #check β„€ -- Π˜Π½Π°Ρ‡Π΅ Ρ†Π΅Π»Ρ‹Π΅ числа ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡ€Π΅Π΄ΡΡ‚Π°Π²ΠΈΡ‚ΡŒ, ΠΊΠ°ΠΊ Ρ€Π°Π·Π½ΠΎΡΡ‚ΡŒ Π΄Π²ΡƒΡ… Π½Π°Ρ‚ΡƒΡ€Π°Π»ΡŒΠ½Ρ‹Ρ… чисСл, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, -1 = 3 - 4 -- Π’Π°ΠΊΠΎΠ΅ прСдставлСниС Π½Π΅ΠΎΠ΄Π½ΠΎΠ·Π½Π°Ρ‡Π½ΠΎ, 3 - 4 = 5 - 6 = ... -- Π’ ΠΊΠ°ΠΊΠΎΠΌ случаС Π΄Π²Π΅ ΠΏΠ°Ρ€Ρ‹ Π·Π°Π΄Π°ΡŽΡ‚ ΠΎΠ΄Π½ΠΎ число? a - b = c - d ↔ a + d = c + b -- ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ ΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΠ΅ `r : N2 β†’ N2 β†’ Prop`: r (a, b) (c, d) = a + d = c + b def r (ab cd : N2) : Prop := ab.1 + cd.2 = cd.1 + ab.2 lemma r_def (ab cd : N2) : r ab cd ↔ ab.1 + cd.2 = cd.1 + ab.2 := iff.rfl lemma r_def' (a b c d : β„•) : r (a,b) (c,d) ↔ a + d = c + b := iff.rfl def r_refl : reflexive r := begin -- `unfold reflexive` раскроСт ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠΊΠ°ΠΆΠ΅Ρ‚, ΠΊΠ°ΠΊ ΠΎΠ½ΠΎ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΎ -- Π½Π΅ Π·Π°Π±Ρ‹Π²Π°ΠΉΡ‚Π΅, Ρ‡Ρ‚ΠΎ N2 = β„• Γ— β„•, поэтому ΠΌΠΎΠΆΠ½ΠΎ Π΄Π΅Π»Π°Ρ‚ΡŒ `rintro ⟨a, b⟩` rintro ⟨a, b⟩, rw r_def', end def r_symm : symmetric r := begin rintro ⟨a, b⟩ ⟨c, d⟩ h, rw r_def' at *, linarith, end def r_trans : transitive r := begin intros x y z rxy ryz, rw r_def at *, linarith, end -- ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ инстанс класса `setoid`: это просто ΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΠ΅ эквивалСнтности Π½Π° `N2` instance setoid : setoid N2 := ⟨r, r_refl, r_symm, r_trans⟩ -- Π’Π΅ΠΏΠ΅Ρ€ΡŒ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ `β‰ˆ` (\~~ ΠΈΠ»ΠΈ \approx) example (x y : N2) : x β‰ˆ y ↔ r x y := iff.rfl -- Π”ΠΎΠ±Π°Π²ΠΈΠΌ simp-Π»Π΅ΠΌΠΌΡ‹ для упрощСния ΠΆΠΈΠ·Π½ΠΈ Π² Π±ΡƒΠ΄ΡƒΡ‰Π΅ΠΌ @[simp] lemma equiv_def (ab cd : N2) : ab β‰ˆ cd ↔ ab.1 + cd.2 = cd.1 + ab.2 := begin refl end @[simp] lemma equiv_def' (a b c d : β„•) : (a,b) β‰ˆ (c,d) ↔ a + d = c + b := iff.rfl end N2 open N2 -- ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ Z ΠΊΠ°ΠΊ `quotient` ΠΏΠΎ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠΌΡƒ ΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΡŽ эквивалСнтности def Z := quotient N2.setoid namespace Z -- По Π·Π°Π΄Π°Π½Π½ΠΎΠΉ ΠΏΠ°Ρ€Π΅ Ρ‚ΠΈΠΏΠ° N2 ΠΌΠΎΠΆΠ½ΠΎ Π»Π΅Π³ΠΊΠΎ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ элСмСнт Ρ‚ΠΈΠΏΠ° Z ΠΊΠ°ΠΊ `quotient.mk x` -- Или ΠΆΠ΅ `⟦x⟧` def bar : Z := quotient.mk foo -- соотвСтствуСт Ρ†Π΅Π»ΠΎΠΌΡƒ числу -1. example : bar = ⟦foo⟧ := rfl -- ΠŸΠΎΡΠΌΠΎΡ‚Ρ€ΠΈΠΌ Π½Π° API `quotient` example (x y : N2) : x β‰ˆ y β†’ ⟦x⟧ = ⟦y⟧ := quotient.sound example (x y : N2) : ⟦x⟧ = ⟦y⟧ β†’ x β‰ˆ y := quotient.exact example (x y : N2) : ⟦x⟧ = ⟦y⟧ ↔ x β‰ˆ y := quotient.eq -- ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ ΠΈ Π΄ΠΎΠΊΠ°ΠΆΠ΅ΠΌ всС ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ Π½Π°Π΄ Z, дойдя Π΄ΠΎ ΠΊΠΎΠΌΠΌΡƒΡ‚Π°Ρ‚ΠΈΠ²Π½ΠΎΠ³ΠΎ ΠΊΠΎΠ»ΡŒΡ†Π° -- ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ 0 ΠΈ 1: def zero : Z := ⟦(0, 0)⟧ def one : Z := ⟦(1, 0)⟧ instance : has_zero Z := ⟨zero⟩ instance : has_one Z := ⟨one⟩ @[simp] lemma zero_def : (0 : Z) = ⟦(0, 0)⟧ := rfl @[simp] lemma one_def : (1 : Z) = ⟦(1, 0)⟧ := rfl /- ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠ΅ ΠΏΠΎ слоТСнию число. Как ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ для `quotient`? 1) ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ Π²ΡΠΏΠΎΠΌΠΎΠ³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ ΠΎΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ `N2 β†’ Z`: ΠΏΠΎ `(a,b)` Π²Ρ‹Π΄Π°Π΅ΠΌ `⟦(b,a)⟧` 2) Π”ΠΎΠΊΠ°ΠΆΠ΅ΠΌ, Ρ‡Ρ‚ΠΎ эта функция константна Π½Π° классах эквивалСнтности: `(a, b) β‰ˆ (c, d) β†’ ⟦(b, a)⟧ = ⟦(d, c)⟧` 3) Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ `quotient.lift`, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ `Z β†’ Z`. -/ def neg_aux (ab : N2) : Z := ⟦(ab.2, ab.1)⟧ @[simp] lemma neg_aux_def (ab : N2) : neg_aux ab = ⟦(ab.2, ab.1)⟧ := rfl def neg : Z β†’ Z := quotient.lift neg_aux begin -- ⊒ βˆ€ (a b : N2), a β‰ˆ b β†’ neg_aux a = neg_aux b intros a b rab, simp at *, linarith, end -- нотация `-z` instance : has_neg Z := ⟨neg⟩ @[simp] lemma neg_def (a b : β„•) : (-⟦(a, b)⟧ : Z) = ⟦(b, a)⟧ := rfl /- ΠžΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ слоТСниС. Если Π±Ρ‹ ΠΌΡ‹ опрСдСляли слоТСниС Ρ‚Π°ΠΊ ΠΆΠ΅, ΠΊΠ°ΠΊ ΠΎΡ‚Ρ€ΠΈΡ†Π°Π½ΠΈΠ΅, Ρ‚ΠΎ ΠΏΡ€ΠΈΡˆΠ»ΠΎΡΡŒ Π±Ρ‹ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ `lift` Π΄Π²Π°ΠΆΠ΄Ρ‹: сначала ΠΏΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ, Ρ‡Ρ‚ΠΎ функция нСзависима ΠΏΠΎ ΠΏΠ΅Ρ€Π²ΠΎΠΌΡƒ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Ρƒ, ΠΏΠΎΡ‚ΠΎΠΌ ΠΏΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌΡƒ. Π•ΡΡ‚ΡŒ функция `quotient.liftβ‚‚`, которая позволяСт Π²Π·ΡΡ‚ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ `f : A β†’ B β†’ C`, ΠΈ Π΄ΠΎΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒΡΡ‚Π²ΠΎ, Ρ‡Ρ‚ΠΎ ΠΎΠ½Π° Π½Π΅ мСняСт Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅, Ссли Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ ΠΏΠ΅Ρ€Π²Ρ‹Π΅ Π΄Π²Π° Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π° Π½Π° эквивалСнтныС, ΠΈ Π²Ρ‹Π΄Π°Π΅Ρ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ `A/~ β†’ B/~ β†’ C`. -/ -- Π’ΡΠΏΠΎΠΌΠΎΠ³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ слоТСния (note `(a-b)+(c-d)=(a+c)-(b+d)` ) def add_aux (ab cd : N2) : Z := ⟦(ab.1 + cd.1, ab.2 + cd.2)⟧ -- И simp-Π»Π΅ΠΌΠΌΠ° для Π½Π΅Π³ΠΎ @[simp] lemma add_aux_def (ab cd : N2) : add_aux ab cd = ⟦(ab.1 + cd.1, ab.2 + cd.2)⟧ := rfl def add : Z β†’ Z β†’ Z := quotient.liftβ‚‚ add_aux begin rintro ⟨a₁, bβ‚βŸ© ⟨aβ‚‚, bβ‚‚βŸ© ⟨c₁, dβ‚βŸ© ⟨cβ‚‚, dβ‚‚βŸ© rab rcd, simp at *, linarith, end instance : has_add Z := ⟨add⟩ @[simp] lemma add_def (a b c d : β„•) : (⟦(a, b)⟧ + ⟦(c, d)⟧ : Z) = ⟦(a+c, b+d)⟧ := rfl -- И ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ Π²Ρ‹Ρ‡ΠΈΡ‚Π°Π½ΠΈΠ΅ Ρ†Π΅Π»Ρ‹Ρ… чисСл def sub (x y : Z) : Z := x + -y instance : has_sub Z := ⟨sub⟩ -- ПокаТСм, Ρ‡Ρ‚ΠΎ Z - коммутативная Π³Ρ€ΡƒΠΏΠΏΠ° ΠΏΠΎ слоТСнию def add_comm_group : add_comm_group Z := { zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, -- Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ `quotient.induction_on` для Π΄ΠΎΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒΡΡ‚Π²Π° Π»Π΅ΠΌΠΌ ΠΏΡ€ΠΎ свойства ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ Π½Π°Π΄ `quotient` -- "Если для всСх `x : N2` Π²Π΅Ρ€Π½ΠΎ `p ⟦x⟧`, Ρ‚ΠΎ для всСх `y : Z` Π²Π΅Ρ€Π½ΠΎ p y" zero_add := begin intro x, apply quotient.induction_on x, clear x, rintro ⟨a, b⟩, simp, end, add_zero := begin intro x, apply quotient.induction_on x, rintro ⟨a, b⟩, simp, end, -- Для вСрсий с 2 ΠΈ 3 Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π°ΠΌΠΈ Π΅ΡΡ‚ΡŒ `quotient.induction_onβ‚‚` ΠΈ `quotient.induction_on₃` -- НС Π·Π°Π±Ρ‹Π²Π°ΠΉΡ‚Π΅ ΠΏΡ€ΠΎ `linarith` ΠΈ `ring` add_assoc := begin intros a b c, apply quotient.induction_on₃ a b c, rintro ⟨x₁, xβ‚‚βŸ© ⟨y₁, yβ‚‚βŸ© ⟨z₁, zβ‚‚βŸ©, simp, ring, end, add_left_neg := begin intro a, apply quotient.induction_on a, rintro ⟨x₁, xβ‚‚βŸ©, simp [add_comm], end, add_comm := begin intros a b, apply quotient.induction_onβ‚‚ a b, rintro ⟨x₁, xβ‚‚βŸ© ⟨y₁, yβ‚‚βŸ©, simp, ring, end, } -- Π£ΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅: `(a-b)*(c-d) = (a*c+b*d)-(a*d+b*c)` def mul_aux (ab cd : N2) : N2 := (ab.1 * cd.1 + ab.2 * cd.2, ab.1 * cd.2 + ab.2 * cd.1) @[simp] lemma mul_aux_def (a b c d : β„•) : mul_aux (a,b) (c,d) = (a*c+b*d,a*d+b*c) := rfl -- Для разнообразия ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠΌ ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ слСгка ΠΈΠ½Π°Ρ‡Π΅. -- ВмСсто использования `quotient.liftβ‚‚` (ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΏΡ€Π΅Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ `N2 β†’ N2 β†’ Z` Π² `Z β†’ Z β†’ Z`), -- ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ `quotient.mapβ‚‚`, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΏΡ€Π΅Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ `N2 β†’ N2 β†’ N2` Π² `Z β†’ Z β†’ Z`. -- `nlinarith` ΡƒΠΌΠ΅Π΅Ρ‚ Π΄ΠΎΠΊΠ°Π·Ρ‹Π²Π°Ρ‚ΡŒ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Ρ†Π΅Π»ΠΈ, содСрТащиС Π½Π΅Π»ΠΈΠ½Π΅ΠΉΠ½Ρ‹Π΅ [Π½Π΅]равСнства def mul : Z β†’ Z β†’ Z := quotient.mapβ‚‚ mul_aux begin rintro ⟨a₁, bβ‚βŸ© ⟨aβ‚‚, bβ‚‚βŸ© rab ⟨c₁, dβ‚βŸ© ⟨cβ‚‚, dβ‚‚βŸ© rcd, simp at *, nlinarith, end instance : has_mul Z := ⟨mul⟩ @[simp] lemma mul_def (a b c d : β„•) : (⟦(a, b)⟧ * ⟦(c, d)⟧ : Z) = ⟦(a*c+b*d, a*d+b*c)⟧ := rfl -- И Π½Π°ΠΊΠΎΠ½Π΅Ρ†, Π΄ΠΎΠΊΠ°ΠΆΠ΅ΠΌ, Ρ‡Ρ‚ΠΎ Z - ΠΊΠΎΠΌΠΌΡƒΡ‚Π°Ρ‚ΠΈΠ²Π½ΠΎΠ΅ ΠΊΠΎΠ»ΡŒΡ†ΠΎ def comm_ring : comm_ring Z := { one := 1, add := (+), mul := (*), mul_assoc := begin intros x y z, apply quotient.induction_on₃ x y z, clear x y z, rintros ⟨a, b⟩ ⟨c, d⟩ ⟨e, f⟩, simp, ring, end, one_mul := begin intro a, apply quotient.induction_on a, clear a, rintro ⟨x, y⟩, simp, end, mul_one := begin intro a, apply quotient.induction_on a, clear a, rintro ⟨x, y⟩, simp, end, left_distrib := begin intros x y z, apply quotient.induction_on₃ x y z, clear x y z, rintros ⟨a, b⟩ ⟨c, d⟩ ⟨e, f⟩, simp, ring, end, right_distrib := begin intros x y z, apply quotient.induction_on₃ x y z, clear x y z, rintros ⟨a, b⟩ ⟨c, d⟩ ⟨e, f⟩, simp, ring, end, mul_comm := begin intros x y, apply quotient.induction_onβ‚‚ x y, clear x y, rintros ⟨a, b⟩ ⟨c, d⟩, simp, ring, end, ..add_comm_group } end Z
d3e79f6f4fa0182068060842bede530e9b299932
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/measure_theory/measurable_space.lean
26a1becfe4763a859f951994896c06737659dae6
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
61,802
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import data.set.disjointed import data.set.countable import data.indicator_function import data.equiv.encodable.lattice import data.tprod import order.filter.lift /-! # Measurable spaces and measurable functions This file defines measurable spaces and the functions and isomorphisms between them. A measurable space is a set equipped with a Οƒ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. Οƒ-algebras on a fixed set `Ξ±` form a complete lattice. Here we order Οƒ-algebras by writing `m₁ ≀ mβ‚‚` if every set which is `m₁`-measurable is also `mβ‚‚`-measurable (that is, `m₁` is a subset of `mβ‚‚`). In particular, any collection of subsets of `Ξ±` generates a smallest Οƒ-algebra which contains all of them. A function `f : Ξ± β†’ Ξ²` induces a Galois connection between the lattices of Οƒ-algebras on `Ξ±` and `Ξ²`. A measurable equivalence between measurable spaces is an equivalence which respects the Οƒ-algebras, that is, for which both directions of the equivalence are measurable functions. We say that a filter `f` is measurably generated if every set `s ∈ f` includes a measurable set `t ∈ f`. This property is useful, e.g., to extract a measurable witness of `filter.eventually`. ## Main statements The main theorem of this file is Dynkin's Ο€-Ξ» theorem, which appears here as an induction principle `induction_on_inter`. Suppose `s` is a collection of subsets of `Ξ±` such that the intersection of two members of `s` belongs to `s` whenever it is nonempty. Let `m` be the Οƒ-algebra generated by `s`. In order to check that a predicate `C` holds on every member of `m`, it suffices to check that `C` holds on the members of `s` and that `C` is preserved by complementation and *disjoint* countable unions. ## Notation * We write `Ξ± ≃ᡐ Ξ²` for measurable equivalences between the measurable spaces `Ξ±` and `Ξ²`. This should not be confused with `β‰ƒβ‚˜` which is used for diffeomorphisms between manifolds. ## Implementation notes Measurability of a function `f : Ξ± β†’ Ξ²` between measurable spaces is defined in terms of the Galois connection induced by f. ## References * <https://en.wikipedia.org/wiki/Measurable_space> * <https://en.wikipedia.org/wiki/Sigma-algebra> * <https://en.wikipedia.org/wiki/Dynkin_system> ## Tags measurable space, Οƒ-algebra, measurable function, measurable equivalence, dynkin system, Ο€-Ξ» theorem, Ο€-system -/ open set encodable function equiv open_locale classical filter variables {Ξ± Ξ² Ξ³ Ξ΄ Ξ΄' : Type*} {ΞΉ : Sort*} {s t u : set Ξ±} /-- A measurable space is a space equipped with a Οƒ-algebra. -/ structure measurable_space (Ξ± : Type*) := (measurable_set' : set Ξ± β†’ Prop) (measurable_set_empty : measurable_set' βˆ…) (measurable_set_compl : βˆ€ s, measurable_set' s β†’ measurable_set' sᢜ) (measurable_set_Union : βˆ€ f : β„• β†’ set Ξ±, (βˆ€ i, measurable_set' (f i)) β†’ measurable_set' (⋃ i, f i)) attribute [class] measurable_space instance [h : measurable_space Ξ±] : measurable_space (order_dual Ξ±) := h section variable [measurable_space Ξ±] /-- `measurable_set s` means that `s` is measurable (in the ambient measure space on `Ξ±`) -/ def measurable_set : set Ξ± β†’ Prop := β€Ήmeasurable_space Ξ±β€Ί.measurable_set' @[simp] lemma measurable_set.empty : measurable_set (βˆ… : set Ξ±) := β€Ήmeasurable_space Ξ±β€Ί.measurable_set_empty lemma measurable_set.compl : measurable_set s β†’ measurable_set sᢜ := β€Ήmeasurable_space Ξ±β€Ί.measurable_set_compl s lemma measurable_set.of_compl (h : measurable_set sᢜ) : measurable_set s := compl_compl s β–Έ h.compl @[simp] lemma measurable_set.compl_iff : measurable_set sᢜ ↔ measurable_set s := ⟨measurable_set.of_compl, measurable_set.compl⟩ @[simp] lemma measurable_set.univ : measurable_set (univ : set Ξ±) := by simpa using (@measurable_set.empty Ξ± _).compl @[nontriviality] lemma subsingleton.measurable_set [subsingleton Ξ±] {s : set Ξ±} : measurable_set s := subsingleton.set_cases measurable_set.empty measurable_set.univ s lemma measurable_set.congr {s t : set Ξ±} (hs : measurable_set s) (h : s = t) : measurable_set t := by rwa ← h lemma measurable_set.bUnion_decode2 [encodable Ξ²] ⦃f : Ξ² β†’ set α⦄ (h : βˆ€ b, measurable_set (f b)) (n : β„•) : measurable_set (⋃ b ∈ decode2 Ξ² n, f b) := encodable.Union_decode2_cases measurable_set.empty h lemma measurable_set.Union [encodable Ξ²] ⦃f : Ξ² β†’ set α⦄ (h : βˆ€ b, measurable_set (f b)) : measurable_set (⋃ b, f b) := begin rw ← encodable.Union_decode2, exact β€Ήmeasurable_space Ξ±β€Ί.measurable_set_Union _ (measurable_set.bUnion_decode2 h) end lemma measurable_set.bUnion {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : countable s) (h : βˆ€ b ∈ s, measurable_set (f b)) : measurable_set (⋃ b ∈ s, f b) := begin rw bUnion_eq_Union, haveI := hs.to_encodable, exact measurable_set.Union (by simpa using h) end lemma set.finite.measurable_set_bUnion {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : finite s) (h : βˆ€ b ∈ s, measurable_set (f b)) : measurable_set (⋃ b ∈ s, f b) := measurable_set.bUnion hs.countable h lemma finset.measurable_set_bUnion {f : Ξ² β†’ set Ξ±} (s : finset Ξ²) (h : βˆ€ b ∈ s, measurable_set (f b)) : measurable_set (⋃ b ∈ s, f b) := s.finite_to_set.measurable_set_bUnion h lemma measurable_set.sUnion {s : set (set Ξ±)} (hs : countable s) (h : βˆ€ t ∈ s, measurable_set t) : measurable_set (⋃₀ s) := by { rw sUnion_eq_bUnion, exact measurable_set.bUnion hs h } lemma set.finite.measurable_set_sUnion {s : set (set Ξ±)} (hs : finite s) (h : βˆ€ t ∈ s, measurable_set t) : measurable_set (⋃₀ s) := measurable_set.sUnion hs.countable h lemma measurable_set.Union_Prop {p : Prop} {f : p β†’ set Ξ±} (hf : βˆ€ b, measurable_set (f b)) : measurable_set (⋃ b, f b) := by { by_cases p; simp [h, hf, measurable_set.empty] } lemma measurable_set.Inter [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (h : βˆ€ b, measurable_set (f b)) : measurable_set (β‹‚ b, f b) := measurable_set.compl_iff.1 $ by { rw compl_Inter, exact measurable_set.Union (Ξ» b, (h b).compl) } section fintype local attribute [instance] fintype.encodable lemma measurable_set.Union_fintype [fintype Ξ²] {f : Ξ² β†’ set Ξ±} (h : βˆ€ b, measurable_set (f b)) : measurable_set (⋃ b, f b) := measurable_set.Union h lemma measurable_set.Inter_fintype [fintype Ξ²] {f : Ξ² β†’ set Ξ±} (h : βˆ€ b, measurable_set (f b)) : measurable_set (β‹‚ b, f b) := measurable_set.Inter h end fintype lemma measurable_set.bInter {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : countable s) (h : βˆ€ b ∈ s, measurable_set (f b)) : measurable_set (β‹‚ b ∈ s, f b) := measurable_set.compl_iff.1 $ by { rw compl_bInter, exact measurable_set.bUnion hs (Ξ» b hb, (h b hb).compl) } lemma set.finite.measurable_set_bInter {f : Ξ² β†’ set Ξ±} {s : set Ξ²} (hs : finite s) (h : βˆ€ b ∈ s, measurable_set (f b)) : measurable_set (β‹‚ b ∈ s, f b) := measurable_set.bInter hs.countable h lemma finset.measurable_set_bInter {f : Ξ² β†’ set Ξ±} (s : finset Ξ²) (h : βˆ€ b ∈ s, measurable_set (f b)) : measurable_set (β‹‚ b ∈ s, f b) := s.finite_to_set.measurable_set_bInter h lemma measurable_set.sInter {s : set (set Ξ±)} (hs : countable s) (h : βˆ€ t ∈ s, measurable_set t) : measurable_set (β‹‚β‚€ s) := by { rw sInter_eq_bInter, exact measurable_set.bInter hs h } lemma set.finite.measurable_set_sInter {s : set (set Ξ±)} (hs : finite s) (h : βˆ€ t ∈ s, measurable_set t) : measurable_set (β‹‚β‚€ s) := measurable_set.sInter hs.countable h lemma measurable_set.Inter_Prop {p : Prop} {f : p β†’ set Ξ±} (hf : βˆ€ b, measurable_set (f b)) : measurable_set (β‹‚ b, f b) := by { by_cases p; simp [h, hf, measurable_set.univ] } @[simp] lemma measurable_set.union {s₁ sβ‚‚ : set Ξ±} (h₁ : measurable_set s₁) (hβ‚‚ : measurable_set sβ‚‚) : measurable_set (s₁ βˆͺ sβ‚‚) := by { rw union_eq_Union, exact measurable_set.Union (bool.forall_bool.2 ⟨hβ‚‚, hβ‚βŸ©) } @[simp] lemma measurable_set.inter {s₁ sβ‚‚ : set Ξ±} (h₁ : measurable_set s₁) (hβ‚‚ : measurable_set sβ‚‚) : measurable_set (s₁ ∩ sβ‚‚) := by { rw inter_eq_compl_compl_union_compl, exact (h₁.compl.union hβ‚‚.compl).compl } @[simp] lemma measurable_set.diff {s₁ sβ‚‚ : set Ξ±} (h₁ : measurable_set s₁) (hβ‚‚ : measurable_set sβ‚‚) : measurable_set (s₁ \ sβ‚‚) := h₁.inter hβ‚‚.compl @[simp] lemma measurable_set.disjointed {f : β„• β†’ set Ξ±} (h : βˆ€ i, measurable_set (f i)) (n) : measurable_set (disjointed f n) := disjointed_induct (h n) (assume t i ht, measurable_set.diff ht $ h _) @[simp] lemma measurable_set.const (p : Prop) : measurable_set {a : Ξ± | p} := by { by_cases p; simp [h, measurable_set.empty]; apply measurable_set.univ } /-- Every set has a measurable superset. Declare this as local instance as needed. -/ lemma nonempty_measurable_superset (s : set Ξ±) : nonempty { t // s βŠ† t ∧ measurable_set t} := ⟨⟨univ, subset_univ s, measurable_set.univ⟩⟩ end @[ext] lemma measurable_space.ext : βˆ€ {m₁ mβ‚‚ : measurable_space Ξ±}, (βˆ€ s : set Ξ±, m₁.measurable_set' s ↔ mβ‚‚.measurable_set' s) β†’ m₁ = mβ‚‚ | ⟨s₁, _, _, _⟩ ⟨sβ‚‚, _, _, _⟩ h := have s₁ = sβ‚‚, from funext $ assume x, propext $ h x, by subst this @[ext] lemma measurable_space.ext_iff {m₁ mβ‚‚ : measurable_space Ξ±} : m₁ = mβ‚‚ ↔ (βˆ€ s : set Ξ±, m₁.measurable_set' s ↔ mβ‚‚.measurable_set' s) := ⟨by { unfreezingI {rintro rfl}, intro s, refl }, measurable_space.ext⟩ /-- A typeclass mixin for `measurable_space`s such that each singleton is measurable. -/ class measurable_singleton_class (Ξ± : Type*) [measurable_space Ξ±] : Prop := (measurable_set_singleton : βˆ€ x, measurable_set ({x} : set Ξ±)) export measurable_singleton_class (measurable_set_singleton) attribute [simp] measurable_set_singleton section measurable_singleton_class variables [measurable_space Ξ±] [measurable_singleton_class Ξ±] lemma measurable_set_eq {a : Ξ±} : measurable_set {x | x = a} := measurable_set_singleton a lemma measurable_set.insert {s : set Ξ±} (hs : measurable_set s) (a : Ξ±) : measurable_set (insert a s) := (measurable_set_singleton a).union hs @[simp] lemma measurable_set_insert {a : Ξ±} {s : set Ξ±} : measurable_set (insert a s) ↔ measurable_set s := ⟨λ h, if ha : a ∈ s then by rwa ← insert_eq_of_mem ha else insert_diff_self_of_not_mem ha β–Έ h.diff (measurable_set_singleton _), Ξ» h, h.insert a⟩ lemma set.finite.measurable_set {s : set Ξ±} (hs : finite s) : measurable_set s := finite.induction_on hs measurable_set.empty $ Ξ» a s ha hsf hsm, hsm.insert _ protected lemma finset.measurable_set (s : finset Ξ±) : measurable_set (↑s : set Ξ±) := s.finite_to_set.measurable_set end measurable_singleton_class namespace measurable_space section complete_lattice instance : partial_order (measurable_space Ξ±) := { le := Ξ» m₁ mβ‚‚, m₁.measurable_set' ≀ mβ‚‚.measurable_set', le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ hβ‚‚, measurable_space.ext $ assume s, ⟨h₁ s, hβ‚‚ s⟩ } /-- The smallest Οƒ-algebra containing a collection `s` of basic sets -/ inductive generate_measurable (s : set (set Ξ±)) : set Ξ± β†’ Prop | basic : βˆ€ u ∈ s, generate_measurable u | empty : generate_measurable βˆ… | compl : βˆ€ s, generate_measurable s β†’ generate_measurable sᢜ | union : βˆ€ f : β„• β†’ set Ξ±, (βˆ€ n, generate_measurable (f n)) β†’ generate_measurable (⋃ i, f i) /-- Construct the smallest measure space containing a collection of basic sets -/ def generate_from (s : set (set Ξ±)) : measurable_space Ξ± := { measurable_set' := generate_measurable s, measurable_set_empty := generate_measurable.empty, measurable_set_compl := generate_measurable.compl, measurable_set_Union := generate_measurable.union } lemma measurable_set_generate_from {s : set (set Ξ±)} {t : set Ξ±} (ht : t ∈ s) : (generate_from s).measurable_set' t := generate_measurable.basic t ht lemma generate_from_le {s : set (set Ξ±)} {m : measurable_space Ξ±} (h : βˆ€ t ∈ s, m.measurable_set' t) : generate_from s ≀ m := assume t (ht : generate_measurable s t), ht.rec_on h (measurable_set_empty m) (assume s _ hs, measurable_set_compl m s hs) (assume f _ hf, measurable_set_Union m f hf) lemma generate_from_le_iff {s : set (set Ξ±)} (m : measurable_space Ξ±) : generate_from s ≀ m ↔ s βŠ† {t | m.measurable_set' t} := iff.intro (assume h u hu, h _ $ measurable_set_generate_from hu) (assume h, generate_from_le h) @[simp] lemma generate_from_measurable_set [measurable_space Ξ±] : generate_from {s : set Ξ± | measurable_set s} = β€Ή_β€Ί := le_antisymm (generate_from_le $ Ξ» _, id) $ Ξ» s, measurable_set_generate_from /-- If `g` is a collection of subsets of `Ξ±` such that the `Οƒ`-algebra generated from `g` contains the same sets as `g`, then `g` was already a `Οƒ`-algebra. -/ protected def mk_of_closure (g : set (set Ξ±)) (hg : {t | (generate_from g).measurable_set' t} = g) : measurable_space Ξ± := { measurable_set' := Ξ» s, s ∈ g, measurable_set_empty := hg β–Έ measurable_set_empty _, measurable_set_compl := hg β–Έ measurable_set_compl _, measurable_set_Union := hg β–Έ measurable_set_Union _ } lemma mk_of_closure_sets {s : set (set Ξ±)} {hs : {t | (generate_from s).measurable_set' t} = s} : measurable_space.mk_of_closure s hs = generate_from s := measurable_space.ext $ assume t, show t ∈ s ↔ _, by { conv_lhs { rw [← hs] }, refl } /-- We get a Galois insertion between `Οƒ`-algebras on `Ξ±` and `set (set Ξ±)` by using `generate_from` on one side and the collection of measurable sets on the other side. -/ def gi_generate_from : galois_insertion (@generate_from Ξ±) (Ξ» m, {t | @measurable_set Ξ± m t}) := { gc := assume s, generate_from_le_iff, le_l_u := assume m s, measurable_set_generate_from, choice := Ξ» g hg, measurable_space.mk_of_closure g $ le_antisymm hg $ (generate_from_le_iff _).1 le_rfl, choice_eq := assume g hg, mk_of_closure_sets } instance : complete_lattice (measurable_space Ξ±) := gi_generate_from.lift_complete_lattice instance : inhabited (measurable_space Ξ±) := ⟨⊀⟩ lemma measurable_set_bot_iff {s : set Ξ±} : @measurable_set Ξ± βŠ₯ s ↔ (s = βˆ… ∨ s = univ) := let b : measurable_space Ξ± := { measurable_set' := Ξ» s, s = βˆ… ∨ s = univ, measurable_set_empty := or.inl rfl, measurable_set_compl := by simp [or_imp_distrib] {contextual := tt}, measurable_set_Union := assume f hf, classical.by_cases (assume h : βˆƒi, f i = univ, let ⟨i, hi⟩ := h in or.inr $ eq_univ_of_univ_subset $ hi β–Έ le_supr f i) (assume h : Β¬ βˆƒi, f i = univ, or.inl $ eq_empty_of_subset_empty $ Union_subset $ assume i, (hf i).elim (by simp {contextual := tt}) (assume hi, false.elim $ h ⟨i, hi⟩)) } in have b = βŠ₯, from bot_unique $ assume s hs, hs.elim (Ξ» s, s.symm β–Έ @measurable_set_empty _ βŠ₯) (Ξ» s, s.symm β–Έ @measurable_set.univ _ βŠ₯), this β–Έ iff.rfl @[simp] theorem measurable_set_top {s : set Ξ±} : @measurable_set _ ⊀ s := trivial @[simp] theorem measurable_set_inf {m₁ mβ‚‚ : measurable_space Ξ±} {s : set Ξ±} : @measurable_set _ (m₁ βŠ“ mβ‚‚) s ↔ @measurable_set _ m₁ s ∧ @measurable_set _ mβ‚‚ s := iff.rfl @[simp] theorem measurable_set_Inf {ms : set (measurable_space Ξ±)} {s : set Ξ±} : @measurable_set _ (Inf ms) s ↔ βˆ€ m ∈ ms, @measurable_set _ m s := show s ∈ (β‹‚ m ∈ ms, {t | @measurable_set _ m t }) ↔ _, by simp @[simp] theorem measurable_set_infi {ΞΉ} {m : ΞΉ β†’ measurable_space Ξ±} {s : set Ξ±} : @measurable_set _ (infi m) s ↔ βˆ€ i, @measurable_set _ (m i) s := show s ∈ (Ξ» m, {s | @measurable_set _ m s }) (infi m) ↔ _, by { rw (@gi_generate_from Ξ±).gc.u_infi, simp } theorem measurable_set_sup {m₁ mβ‚‚ : measurable_space Ξ±} {s : set Ξ±} : @measurable_set _ (m₁ βŠ” mβ‚‚) s ↔ generate_measurable (m₁.measurable_set' βˆͺ mβ‚‚.measurable_set') s := iff.refl _ theorem measurable_set_Sup {ms : set (measurable_space Ξ±)} {s : set Ξ±} : @measurable_set _ (Sup ms) s ↔ generate_measurable {s : set Ξ± | βˆƒ m ∈ ms, @measurable_set _ m s} s := begin change @measurable_set' _ (generate_from $ ⋃ m ∈ ms, _) _ ↔ _, simp [generate_from, ← set_of_exists] end theorem measurable_set_supr {ΞΉ} {m : ΞΉ β†’ measurable_space Ξ±} {s : set Ξ±} : @measurable_set _ (supr m) s ↔ generate_measurable {s : set Ξ± | βˆƒ i, @measurable_set _ (m i) s} s := begin convert @measurable_set_Sup _ (range m) s, simp, end end complete_lattice section functors variables {m m₁ mβ‚‚ : measurable_space Ξ±} {m' : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ±} /-- The forward image of a measure space under a function. `map f m` contains the sets `s : set Ξ²` whose preimage under `f` is measurable. -/ protected def map (f : Ξ± β†’ Ξ²) (m : measurable_space Ξ±) : measurable_space Ξ² := { measurable_set' := Ξ» s, m.measurable_set' $ f ⁻¹' s, measurable_set_empty := m.measurable_set_empty, measurable_set_compl := assume s hs, m.measurable_set_compl _ hs, measurable_set_Union := assume f hf, by { rw preimage_Union, exact m.measurable_set_Union _ hf }} @[simp] lemma map_id : m.map id = m := measurable_space.ext $ assume s, iff.rfl @[simp] lemma map_comp {f : Ξ± β†’ Ξ²} {g : Ξ² β†’ Ξ³} : (m.map f).map g = m.map (g ∘ f) := measurable_space.ext $ assume s, iff.rfl /-- The reverse image of a measure space under a function. `comap f m` contains the sets `s : set Ξ±` such that `s` is the `f`-preimage of a measurable set in `Ξ²`. -/ protected def comap (f : Ξ± β†’ Ξ²) (m : measurable_space Ξ²) : measurable_space Ξ± := { measurable_set' := Ξ» s, βˆƒs', m.measurable_set' s' ∧ f ⁻¹' s' = s, measurable_set_empty := βŸ¨βˆ…, m.measurable_set_empty, rfl⟩, measurable_set_compl := assume s ⟨s', h₁, hβ‚‚βŸ©, ⟨s'ᢜ, m.measurable_set_compl _ h₁, hβ‚‚ β–Έ rfl⟩, measurable_set_Union := assume s hs, let ⟨s', hs'⟩ := classical.axiom_of_choice hs in βŸ¨β‹ƒ i, s' i, m.measurable_set_Union _ (Ξ» i, (hs' i).left), by simp [hs'] ⟩ } @[simp] lemma comap_id : m.comap id = m := measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h β–Έ hs', assume h, ⟨s, h, rfl⟩⟩ @[simp] lemma comap_comp {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ²} : (m.comap f).comap g = m.comap (f ∘ g) := measurable_space.ext $ assume s, ⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht β–Έ hu β–Έ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩ lemma comap_le_iff_le_map {f : Ξ± β†’ Ξ²} : m'.comap f ≀ m ↔ m' ≀ m.map f := ⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq β–Έ h _ ht⟩ lemma gc_comap_map (f : Ξ± β†’ Ξ²) : galois_connection (measurable_space.comap f) (measurable_space.map f) := assume f g, comap_le_iff_le_map lemma map_mono (h : m₁ ≀ mβ‚‚) : m₁.map f ≀ mβ‚‚.map f := (gc_comap_map f).monotone_u h lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h lemma comap_mono (h : m₁ ≀ mβ‚‚) : m₁.comap g ≀ mβ‚‚.comap g := (gc_comap_map g).monotone_l h lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h @[simp] lemma comap_bot : (βŠ₯ : measurable_space Ξ±).comap g = βŠ₯ := (gc_comap_map g).l_bot @[simp] lemma comap_sup : (m₁ βŠ” mβ‚‚).comap g = m₁.comap g βŠ” mβ‚‚.comap g := (gc_comap_map g).l_sup @[simp] lemma comap_supr {m : ΞΉ β†’ measurable_space Ξ±} : (⨆i, m i).comap g = (⨆i, (m i).comap g) := (gc_comap_map g).l_supr @[simp] lemma map_top : (⊀ : measurable_space Ξ±).map f = ⊀ := (gc_comap_map f).u_top @[simp] lemma map_inf : (m₁ βŠ“ mβ‚‚).map f = m₁.map f βŠ“ mβ‚‚.map f := (gc_comap_map f).u_inf @[simp] lemma map_infi {m : ΞΉ β†’ measurable_space Ξ±} : (β¨…i, m i).map f = (β¨…i, (m i).map f) := (gc_comap_map f).u_infi lemma comap_map_le : (m.map f).comap f ≀ m := (gc_comap_map f).l_u_le _ lemma le_map_comap : m ≀ (m.comap g).map g := (gc_comap_map g).le_u_l _ end functors lemma generate_from_le_generate_from {s t : set (set Ξ±)} (h : s βŠ† t) : generate_from s ≀ generate_from t := gi_generate_from.gc.monotone_l h lemma generate_from_sup_generate_from {s t : set (set Ξ±)} : generate_from s βŠ” generate_from t = generate_from (s βˆͺ t) := (@gi_generate_from Ξ±).gc.l_sup.symm lemma comap_generate_from {f : Ξ± β†’ Ξ²} {s : set (set Ξ²)} : (generate_from s).comap f = generate_from (preimage f '' s) := le_antisymm (comap_le_iff_le_map.2 $ generate_from_le $ assume t hts, generate_measurable.basic _ $ mem_image_of_mem _ $ hts) (generate_from_le $ assume t ⟨u, hu, eq⟩, eq β–Έ ⟨u, generate_measurable.basic _ hu, rfl⟩) end measurable_space section measurable_functions open measurable_space /-- A function `f` between measurable spaces is measurable if the preimage of every measurable set is measurable. -/ def measurable [measurable_space Ξ±] [measurable_space Ξ²] (f : Ξ± β†’ Ξ²) : Prop := βˆ€ ⦃t : set β⦄, measurable_set t β†’ measurable_set (f ⁻¹' t) lemma measurable_iff_le_map {m₁ : measurable_space Ξ±} {mβ‚‚ : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} : measurable f ↔ mβ‚‚ ≀ m₁.map f := iff.rfl alias measurable_iff_le_map ↔ measurable.le_map measurable.of_le_map lemma measurable_iff_comap_le {m₁ : measurable_space Ξ±} {mβ‚‚ : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} : measurable f ↔ mβ‚‚.comap f ≀ m₁ := comap_le_iff_le_map.symm alias measurable_iff_comap_le ↔ measurable.comap_le measurable.of_comap_le lemma measurable.mono {ma ma' : measurable_space Ξ±} {mb mb' : measurable_space Ξ²} {f : Ξ± β†’ Ξ²} (hf : @measurable Ξ± Ξ² ma mb f) (ha : ma ≀ ma') (hb : mb' ≀ mb) : @measurable Ξ± Ξ² ma' mb' f := Ξ» t ht, ha _ $ hf $ hb _ ht lemma measurable_from_top [measurable_space Ξ²] {f : Ξ± β†’ Ξ²} : @measurable _ _ ⊀ _ f := Ξ» s hs, trivial lemma measurable_generate_from [measurable_space Ξ±] {s : set (set Ξ²)} {f : Ξ± β†’ Ξ²} (h : βˆ€ t ∈ s, measurable_set (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f := measurable.of_le_map $ generate_from_le h variables [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] lemma measurable_id : measurable (@id Ξ±) := Ξ» t, id lemma measurable.comp {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} (hg : measurable g) (hf : measurable f) : measurable (g ∘ f) := Ξ» t ht, hf (hg ht) @[nontriviality] lemma subsingleton.measurable [subsingleton Ξ±] {f : Ξ± β†’ Ξ²} : measurable f := Ξ» s hs, @subsingleton.measurable_set Ξ± _ _ _ lemma measurable.piecewise {s : set Ξ±} {_ : decidable_pred s} {f g : Ξ± β†’ Ξ²} (hs : measurable_set s) (hf : measurable f) (hg : measurable g) : measurable (piecewise s f g) := begin intros t ht, simp only [piecewise_preimage], exact (hs.inter $ hf ht).union (hs.compl.inter $ hg ht) end /-- this is slightly different from `measurable.piecewise`. It can be used to show `measurable (ite (x=0) 0 1)` by `exact measurable.ite (measurable_set_singleton 0) measurable_const measurable_const`, but replacing `measurable.ite` by `measurable.piecewise` in that example proof does not work. -/ lemma measurable.ite {p : Ξ± β†’ Prop} {_ : decidable_pred p} {f g : Ξ± β†’ Ξ²} (hp : measurable_set {a : Ξ± | p a}) (hf : measurable f) (hg : measurable g) : measurable (Ξ» x, ite (p x) (f x) (g x)) := measurable.piecewise hp hf hg @[simp] lemma measurable_const {a : Ξ±} : measurable (Ξ» b : Ξ², a) := assume s hs, measurable_set.const (a ∈ s) lemma measurable.indicator [has_zero Ξ²] {s : set Ξ±} {f : Ξ± β†’ Ξ²} (hf : measurable f) (hs : measurable_set s) : measurable (s.indicator f) := hf.piecewise hs measurable_const @[to_additive] lemma measurable_one [has_one Ξ±] : measurable (1 : Ξ² β†’ Ξ±) := @measurable_const _ _ _ _ 1 lemma measurable_of_not_nonempty (h : Β¬ nonempty Ξ±) (f : Ξ± β†’ Ξ²) : measurable f := begin assume s hs, convert measurable_set.empty, exact eq_empty_of_not_nonempty h _, end end measurable_functions section constructions variables [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] instance : measurable_space empty := ⊀ instance : measurable_space punit := ⊀ -- this also works for `unit` instance : measurable_space bool := ⊀ instance : measurable_space β„• := ⊀ instance : measurable_space β„€ := ⊀ instance : measurable_space β„š := ⊀ lemma measurable_to_encodable [encodable Ξ±] {f : Ξ² β†’ Ξ±} (h : βˆ€ y, measurable_set (f ⁻¹' {f y})) : measurable f := begin assume s hs, rw [← bUnion_preimage_singleton], refine measurable_set.Union (Ξ» y, measurable_set.Union_Prop $ Ξ» hy, _), by_cases hyf : y ∈ range f, { rcases hyf with ⟨y, rfl⟩, apply h }, { simp only [preimage_singleton_eq_empty.2 hyf, measurable_set.empty] } end lemma measurable_unit (f : unit β†’ Ξ±) : measurable f := measurable_from_top section nat lemma measurable_from_nat {f : β„• β†’ Ξ±} : measurable f := measurable_from_top lemma measurable_to_nat {f : Ξ± β†’ β„•} : (βˆ€ y, measurable_set (f ⁻¹' {f y})) β†’ measurable f := measurable_to_encodable lemma measurable_find_greatest' {p : Ξ± β†’ β„• β†’ Prop} {N} (hN : βˆ€ k ≀ N, measurable_set {x | nat.find_greatest (p x) N = k}) : measurable (Ξ» x, nat.find_greatest (p x) N) := measurable_to_nat $ Ξ» x, hN _ nat.find_greatest_le lemma measurable_find_greatest {p : Ξ± β†’ β„• β†’ Prop} {N} (hN : βˆ€ k ≀ N, measurable_set {x | p x k}) : measurable (Ξ» x, nat.find_greatest (p x) N) := begin refine measurable_find_greatest' (Ξ» k hk, _), simp only [nat.find_greatest_eq_iff, set_of_and, set_of_forall, ← compl_set_of], repeat { apply_rules [measurable_set.inter, measurable_set.const, measurable_set.Inter, measurable_set.Inter_Prop, measurable_set.compl, hN]; try { intros } } end lemma measurable_find {p : Ξ± β†’ β„• β†’ Prop} (hp : βˆ€ x, βˆƒ N, p x N) (hm : βˆ€ k, measurable_set {x | p x k}) : measurable (Ξ» x, nat.find (hp x)) := begin refine measurable_to_nat (Ξ» x, _), simp only [set.preimage, mem_singleton_iff, nat.find_eq_iff, set_of_and, set_of_forall, ← compl_set_of], repeat { apply_rules [measurable_set.inter, hm, measurable_set.Inter, measurable_set.Inter_Prop, measurable_set.compl]; try { intros } } end end nat section subtype instance {Ξ±} {p : Ξ± β†’ Prop} [m : measurable_space Ξ±] : measurable_space (subtype p) := m.comap (coe : _ β†’ Ξ±) lemma measurable_subtype_coe {p : Ξ± β†’ Prop} : measurable (coe : subtype p β†’ Ξ±) := measurable_space.le_map_comap lemma measurable.subtype_coe {p : Ξ² β†’ Prop} {f : Ξ± β†’ subtype p} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a : Ξ²)) := measurable_subtype_coe.comp hf lemma measurable.subtype_mk {p : Ξ² β†’ Prop} {f : Ξ± β†’ Ξ²} (hf : measurable f) {h : βˆ€ x, p (f x)} : measurable (Ξ» x, (⟨f x, h x⟩ : subtype p)) := Ξ» t ⟨s, hs⟩, hs.2 β–Έ by simp only [← preimage_comp, (∘), subtype.coe_mk, hf hs.1] lemma measurable_set.subtype_image {s : set Ξ±} {t : set s} (hs : measurable_set s) : measurable_set t β†’ measurable_set ((coe : s β†’ Ξ±) '' t) | ⟨u, (hu : measurable_set u), (eq : coe ⁻¹' u = t)⟩ := begin rw [← eq, subtype.image_preimage_coe], exact hu.inter hs end lemma measurable_of_measurable_union_cover {f : Ξ± β†’ Ξ²} (s t : set Ξ±) (hs : measurable_set s) (ht : measurable_set t) (h : univ βŠ† s βˆͺ t) (hc : measurable (Ξ» a : s, f a)) (hd : measurable (Ξ» a : t, f a)) : measurable f := begin intros u hu, convert (hs.subtype_image (hc hu)).union (ht.subtype_image (hd hu)), change f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) βˆͺ coe '' (coe ⁻¹' (f ⁻¹' u) : set t), rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, subtype.range_coe, subtype.range_coe, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ], end lemma measurable_of_measurable_on_compl_singleton [measurable_singleton_class Ξ±] {f : Ξ± β†’ Ξ²} (a : Ξ±) (hf : measurable (set.restrict f {x | x β‰  a})) : measurable f := measurable_of_measurable_union_cover _ _ measurable_set_eq measurable_set_eq.compl (Ξ» x hx, classical.em _) (@subsingleton.measurable {x | x = a} _ _ _ ⟨λ x y, subtype.eq $ x.2.trans y.2.symm⟩ _) hf end subtype section prod instance {Ξ± Ξ²} [m₁ : measurable_space Ξ±] [mβ‚‚ : measurable_space Ξ²] : measurable_space (Ξ± Γ— Ξ²) := m₁.comap prod.fst βŠ” mβ‚‚.comap prod.snd lemma measurable_fst : measurable (prod.fst : Ξ± Γ— Ξ² β†’ Ξ±) := measurable.of_comap_le le_sup_left lemma measurable.fst {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a).1) := measurable_fst.comp hf lemma measurable_snd : measurable (prod.snd : Ξ± Γ— Ξ² β†’ Ξ²) := measurable.of_comap_le le_sup_right lemma measurable.snd {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf : measurable f) : measurable (Ξ» a : Ξ±, (f a).2) := measurable_snd.comp hf lemma measurable.prod {f : Ξ± β†’ Ξ² Γ— Ξ³} (hf₁ : measurable (Ξ» a, (f a).1)) (hfβ‚‚ : measurable (Ξ» a, (f a).2)) : measurable f := measurable.of_le_map $ sup_le (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₁ }) (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hfβ‚‚ }) lemma measurable_prod {f : Ξ± β†’ Ξ² Γ— Ξ³} : measurable f ↔ measurable (Ξ» a, (f a).1) ∧ measurable (Ξ» a, (f a).2) := ⟨λ hf, ⟨measurable_fst.comp hf, measurable_snd.comp hf⟩, Ξ» h, measurable.prod h.1 h.2⟩ lemma measurable.prod_mk {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} (hf : measurable f) (hg : measurable g) : measurable (Ξ» a : Ξ±, (f a, g a)) := measurable.prod hf hg lemma measurable_prod_mk_left {x : Ξ±} : measurable (@prod.mk _ Ξ² x) := measurable_const.prod_mk measurable_id lemma measurable_prod_mk_right {y : Ξ²} : measurable (Ξ» x : Ξ±, (x, y)) := measurable_id.prod_mk measurable_const lemma measurable.of_uncurry_left {f : Ξ± β†’ Ξ² β†’ Ξ³} (hf : measurable (uncurry f)) {x : Ξ±} : measurable (f x) := hf.comp measurable_prod_mk_left lemma measurable.of_uncurry_right {f : Ξ± β†’ Ξ² β†’ Ξ³} (hf : measurable (uncurry f)) {y : Ξ²} : measurable (Ξ» x, f x y) := hf.comp measurable_prod_mk_right lemma measurable_swap : measurable (prod.swap : Ξ± Γ— Ξ² β†’ Ξ² Γ— Ξ±) := measurable.prod measurable_snd measurable_fst lemma measurable_swap_iff {f : Ξ± Γ— Ξ² β†’ Ξ³} : measurable (f ∘ prod.swap) ↔ measurable f := ⟨λ hf, by { convert hf.comp measurable_swap, ext ⟨x, y⟩, refl }, Ξ» hf, hf.comp measurable_swap⟩ lemma measurable_set.prod {s : set Ξ±} {t : set Ξ²} (hs : measurable_set s) (ht : measurable_set t) : measurable_set (s.prod t) := measurable_set.inter (measurable_fst hs) (measurable_snd ht) lemma measurable_set_prod_of_nonempty {s : set Ξ±} {t : set Ξ²} (h : (s.prod t).nonempty) : measurable_set (s.prod t) ↔ measurable_set s ∧ measurable_set t := begin rcases h with ⟨⟨x, y⟩, hx, hy⟩, refine ⟨λ hst, _, Ξ» h, h.1.prod h.2⟩, have : measurable_set ((Ξ» x, (x, y)) ⁻¹' s.prod t) := measurable_id.prod_mk measurable_const hst, have : measurable_set (prod.mk x ⁻¹' s.prod t) := measurable_const.prod_mk measurable_id hst, simp * at * end lemma measurable_set_prod {s : set Ξ±} {t : set Ξ²} : measurable_set (s.prod t) ↔ (measurable_set s ∧ measurable_set t) ∨ s = βˆ… ∨ t = βˆ… := begin cases (s.prod t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.mp h] }, { simp [←not_nonempty_iff_eq_empty, prod_nonempty_iff.mp h, measurable_set_prod_of_nonempty h] } end lemma measurable_set_swap_iff {s : set (Ξ± Γ— Ξ²)} : measurable_set (prod.swap ⁻¹' s) ↔ measurable_set s := ⟨λ hs, by { convert measurable_swap hs, ext ⟨x, y⟩, refl }, Ξ» hs, measurable_swap hs⟩ end prod section pi variables {Ο€ : Ξ΄ β†’ Type*} instance measurable_space.pi [m : Ξ  a, measurable_space (Ο€ a)] : measurable_space (Ξ  a, Ο€ a) := ⨆ a, (m a).comap (Ξ» b, b a) variables [Ξ  a, measurable_space (Ο€ a)] [measurable_space Ξ³] lemma measurable_pi_iff {g : Ξ± β†’ Ξ  a, Ο€ a} : measurable g ↔ βˆ€ a, measurable (Ξ» x, g x a) := by simp_rw [measurable_iff_comap_le, measurable_space.pi, measurable_space.comap_supr, measurable_space.comap_comp, function.comp, supr_le_iff] lemma measurable_pi_apply (a : Ξ΄) : measurable (Ξ» f : Ξ  a, Ο€ a, f a) := measurable.of_comap_le $ le_supr _ a lemma measurable.eval {a : Ξ΄} {g : Ξ± β†’ Ξ  a, Ο€ a} (hg : measurable g) : measurable (Ξ» x, g x a) := (measurable_pi_apply a).comp hg lemma measurable_pi_lambda (f : Ξ± β†’ Ξ  a, Ο€ a) (hf : βˆ€ a, measurable (Ξ» c, f c a)) : measurable f := measurable_pi_iff.mpr hf /-- The function `update f a : Ο€ a β†’ Ξ  a, Ο€ a` is always measurable. This doesn't require `f` to be measurable. This should not be confused with the statement that `update f a x` is measurable. -/ lemma measurable_update (f : Ξ  (a : Ξ΄), Ο€ a) {a : Ξ΄} : measurable (update f a) := begin apply measurable_pi_lambda, intro x, by_cases hx : x = a, { cases hx, convert measurable_id, ext, simp }, simp_rw [update_noteq hx], apply measurable_const, end /- Even though we cannot use projection notation, we still keep a dot to be consistent with similar lemmas, like `measurable_set.prod`. -/ lemma measurable_set.pi {s : set Ξ΄} {t : Ξ  i : Ξ΄, set (Ο€ i)} (hs : countable s) (ht : βˆ€ i ∈ s, measurable_set (t i)) : measurable_set (s.pi t) := by { rw [pi_def], exact measurable_set.bInter hs (Ξ» i hi, measurable_pi_apply _ (ht i hi)) } lemma measurable_set.univ_pi [encodable Ξ΄] {t : Ξ  i : Ξ΄, set (Ο€ i)} (ht : βˆ€ i, measurable_set (t i)) : measurable_set (pi univ t) := measurable_set.pi (countable_encodable _) (Ξ» i _, ht i) lemma measurable_set_pi_of_nonempty {s : set Ξ΄} {t : Ξ  i, set (Ο€ i)} (hs : countable s) (h : (pi s t).nonempty) : measurable_set (pi s t) ↔ βˆ€ i ∈ s, measurable_set (t i) := begin rcases h with ⟨f, hf⟩, refine ⟨λ hst i hi, _, measurable_set.pi hs⟩, convert measurable_update f hst, rw [update_preimage_pi hi], exact Ξ» j hj _, hf j hj end lemma measurable_set_pi {s : set Ξ΄} {t : Ξ  i, set (Ο€ i)} (hs : countable s) : measurable_set (pi s t) ↔ (βˆ€ i ∈ s, measurable_set (t i)) ∨ pi s t = βˆ… := begin cases (pi s t).eq_empty_or_nonempty with h h, { simp [h] }, { simp [measurable_set_pi_of_nonempty hs, h, ← not_nonempty_iff_eq_empty] } end section fintype local attribute [instance] fintype.encodable lemma measurable_set.pi_fintype [fintype Ξ΄] {s : set Ξ΄} {t : Ξ  i, set (Ο€ i)} (ht : βˆ€ i ∈ s, measurable_set (t i)) : measurable_set (pi s t) := measurable_set.pi (countable_encodable _) ht lemma measurable_set.univ_pi_fintype [fintype Ξ΄] {t : Ξ  i, set (Ο€ i)} (ht : βˆ€ i, measurable_set (t i)) : measurable_set (pi univ t) := measurable_set.pi_fintype (Ξ» i _, ht i) end fintype end pi instance tprod.measurable_space (Ο€ : Ξ΄ β†’ Type*) [βˆ€ x, measurable_space (Ο€ x)] : βˆ€ (l : list Ξ΄), measurable_space (list.tprod Ο€ l) | [] := punit.measurable_space | (i :: is) := @prod.measurable_space _ _ _ (tprod.measurable_space is) section tprod open list variables {Ο€ : Ξ΄ β†’ Type*} [βˆ€ x, measurable_space (Ο€ x)] lemma measurable_tprod_mk (l : list Ξ΄) : measurable (@tprod.mk Ξ΄ Ο€ l) := begin induction l with i l ih, { exact measurable_const }, { exact (measurable_pi_apply i).prod_mk ih } end lemma measurable_tprod_elim : βˆ€ {l : list Ξ΄} {i : Ξ΄} (hi : i ∈ l), measurable (Ξ» (v : tprod Ο€ l), v.elim hi) | (i :: is) j hj := begin by_cases hji : j = i, { subst hji, simp [measurable_fst] }, { rw [funext $ tprod.elim_of_ne _ hji], exact (measurable_tprod_elim (hj.resolve_left hji)).comp measurable_snd } end lemma measurable_tprod_elim' {l : list Ξ΄} (h : βˆ€ i, i ∈ l) : measurable (tprod.elim' h : tprod Ο€ l β†’ Ξ  i, Ο€ i) := measurable_pi_lambda _ (Ξ» i, measurable_tprod_elim (h i)) lemma measurable_set.tprod (l : list Ξ΄) {s : βˆ€ i, set (Ο€ i)} (hs : βˆ€ i, measurable_set (s i)) : measurable_set (set.tprod l s) := by { induction l with i l ih, exact measurable_set.univ, exact (hs i).prod ih } end tprod instance {Ξ± Ξ²} [m₁ : measurable_space Ξ±] [mβ‚‚ : measurable_space Ξ²] : measurable_space (Ξ± βŠ• Ξ²) := m₁.map sum.inl βŠ“ mβ‚‚.map sum.inr section sum lemma measurable_inl : measurable (@sum.inl Ξ± Ξ²) := measurable.of_le_map inf_le_left lemma measurable_inr : measurable (@sum.inr Ξ± Ξ²) := measurable.of_le_map inf_le_right lemma measurable_sum {f : Ξ± βŠ• Ξ² β†’ Ξ³} (hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f := measurable.of_comap_le $ le_inf (measurable_space.comap_le_iff_le_map.2 $ hl) (measurable_space.comap_le_iff_le_map.2 $ hr) lemma measurable.sum_elim {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ³} (hf : measurable f) (hg : measurable g) : measurable (sum.elim f g) := measurable_sum hf hg lemma measurable_set.inl_image {s : set Ξ±} (hs : measurable_set s) : measurable_set (sum.inl '' s : set (Ξ± βŠ• Ξ²)) := ⟨show measurable_set (sum.inl ⁻¹' _), by { rwa [preimage_image_eq], exact (Ξ» a b, sum.inl.inj) }, have sum.inr ⁻¹' (sum.inl '' s : set (Ξ± βŠ• Ξ²)) = βˆ… := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show measurable_set (sum.inr ⁻¹' _), by { rw [this], exact measurable_set.empty }⟩ lemma measurable_set_range_inl : measurable_set (range sum.inl : set (Ξ± βŠ• Ξ²)) := by { rw [← image_univ], exact measurable_set.univ.inl_image } lemma measurable_set_inr_image {s : set Ξ²} (hs : measurable_set s) : measurable_set (sum.inr '' s : set (Ξ± βŠ• Ξ²)) := ⟨ have sum.inl ⁻¹' (sum.inr '' s : set (Ξ± βŠ• Ξ²)) = βˆ… := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show measurable_set (sum.inl ⁻¹' _), by { rw [this], exact measurable_set.empty }, show measurable_set (sum.inr ⁻¹' _), by { rwa [preimage_image_eq], exact Ξ» a b, sum.inr.inj }⟩ lemma measurable_set_range_inr : measurable_set (range sum.inr : set (Ξ± βŠ• Ξ²)) := by { rw [← image_univ], exact measurable_set_inr_image measurable_set.univ } end sum instance {Ξ±} {Ξ² : Ξ± β†’ Type*} [m : Ξ a, measurable_space (Ξ² a)] : measurable_space (sigma Ξ²) := β¨…a, (m a).map (sigma.mk a) end constructions /-- Equivalences between measurable spaces. Main application is the simplification of measurability statements along measurable equivalences. -/ structure measurable_equiv (Ξ± Ξ² : Type*) [measurable_space Ξ±] [measurable_space Ξ²] extends Ξ± ≃ Ξ² := (measurable_to_fun : measurable to_fun) (measurable_inv_fun : measurable inv_fun) infix ` ≃ᡐ `:25 := measurable_equiv namespace measurable_equiv variables (Ξ± Ξ²) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] instance : has_coe_to_fun (Ξ± ≃ᡐ Ξ²) := ⟨λ _, Ξ± β†’ Ξ², Ξ» e, e.to_equiv⟩ variables {Ξ± Ξ²} lemma coe_eq (e : Ξ± ≃ᡐ Ξ²) : (e : Ξ± β†’ Ξ²) = e.to_equiv := rfl protected lemma measurable (e : Ξ± ≃ᡐ Ξ²) : measurable (e : Ξ± β†’ Ξ²) := e.measurable_to_fun @[simp] lemma coe_mk (e : Ξ± ≃ Ξ²) (h1 : measurable e) (h2 : measurable e.symm) : ((⟨e, h1, h2⟩ : Ξ± ≃ᡐ Ξ²) : Ξ± β†’ Ξ²) = e := rfl /-- Any measurable space is equivalent to itself. -/ def refl (Ξ± : Type*) [measurable_space Ξ±] : Ξ± ≃ᡐ Ξ± := { to_equiv := equiv.refl Ξ±, measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id } instance : inhabited (Ξ± ≃ᡐ Ξ±) := ⟨refl α⟩ /-- The composition of equivalences between measurable spaces. -/ @[simps] def trans (ab : Ξ± ≃ᡐ Ξ²) (bc : Ξ² ≃ᡐ Ξ³) : Ξ± ≃ᡐ Ξ³ := { to_equiv := ab.to_equiv.trans bc.to_equiv, measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun, measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun } /-- The inverse of an equivalence between measurable spaces. -/ @[simps] def symm (ab : Ξ± ≃ᡐ Ξ²) : Ξ² ≃ᡐ Ξ± := { to_equiv := ab.to_equiv.symm, measurable_to_fun := ab.measurable_inv_fun, measurable_inv_fun := ab.measurable_to_fun } @[simp] lemma coe_symm_mk (e : Ξ± ≃ Ξ²) (h1 : measurable e) (h2 : measurable e.symm) : ((⟨e, h1, h2⟩ : Ξ± ≃ᡐ Ξ²).symm : Ξ² β†’ Ξ±) = e.symm := rfl @[simp] theorem symm_comp_self (e : Ξ± ≃ᡐ Ξ²) : e.symm ∘ e = id := funext e.left_inv @[simp] theorem self_comp_symm (e : Ξ± ≃ᡐ Ξ²) : e ∘ e.symm = id := funext e.right_inv /-- Equal measurable spaces are equivalent. -/ protected def cast {Ξ± Ξ²} [i₁ : measurable_space Ξ±] [iβ‚‚ : measurable_space Ξ²] (h : Ξ± = Ξ²) (hi : i₁ == iβ‚‚) : Ξ± ≃ᡐ Ξ² := { to_equiv := equiv.cast h, measurable_to_fun := by { substI h, substI hi, exact measurable_id }, measurable_inv_fun := by { substI h, substI hi, exact measurable_id }} protected lemma measurable_coe_iff {f : Ξ² β†’ Ξ³} (e : Ξ± ≃ᡐ Ξ²) : measurable (f ∘ e) ↔ measurable f := iff.intro (assume hfe, have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable, by rwa [trans_to_equiv, symm_to_equiv, equiv.symm_trans] at this) (Ξ» h, h.comp e.measurable) /-- Products of equivalent measurable spaces are equivalent. -/ def prod_congr (ab : Ξ± ≃ᡐ Ξ²) (cd : Ξ³ ≃ᡐ Ξ΄) : Ξ± Γ— Ξ³ ≃ᡐ Ξ² Γ— Ξ΄ := { to_equiv := prod_congr ab.to_equiv cd.to_equiv, measurable_to_fun := (ab.measurable_to_fun.comp measurable_id.fst).prod_mk (cd.measurable_to_fun.comp measurable_id.snd), measurable_inv_fun := (ab.measurable_inv_fun.comp measurable_id.fst).prod_mk (cd.measurable_inv_fun.comp measurable_id.snd) } /-- Products of measurable spaces are symmetric. -/ def prod_comm : Ξ± Γ— Ξ² ≃ᡐ Ξ² Γ— Ξ± := { to_equiv := prod_comm Ξ± Ξ², measurable_to_fun := measurable_id.snd.prod_mk measurable_id.fst, measurable_inv_fun := measurable_id.snd.prod_mk measurable_id.fst } /-- Products of measurable spaces are associative. -/ def prod_assoc : (Ξ± Γ— Ξ²) Γ— Ξ³ ≃ᡐ Ξ± Γ— (Ξ² Γ— Ξ³) := { to_equiv := prod_assoc Ξ± Ξ² Ξ³, measurable_to_fun := measurable_fst.fst.prod_mk $ measurable_fst.snd.prod_mk measurable_snd, measurable_inv_fun := (measurable_fst.prod_mk measurable_snd.fst).prod_mk measurable_snd.snd } /-- Sums of measurable spaces are symmetric. -/ def sum_congr (ab : Ξ± ≃ᡐ Ξ²) (cd : Ξ³ ≃ᡐ Ξ΄) : Ξ± βŠ• Ξ³ ≃ᡐ Ξ² βŠ• Ξ΄ := { to_equiv := sum_congr ab.to_equiv cd.to_equiv, measurable_to_fun := begin cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end, measurable_inv_fun := begin cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end } /-- `set.prod s t ≃ (s Γ— t)` as measurable spaces. -/ def set.prod (s : set Ξ±) (t : set Ξ²) : s.prod t ≃ᡐ s Γ— t := { to_equiv := equiv.set.prod s t, measurable_to_fun := measurable_id.subtype_coe.fst.subtype_mk.prod_mk measurable_id.subtype_coe.snd.subtype_mk, measurable_inv_fun := measurable.subtype_mk $ measurable_id.fst.subtype_coe.prod_mk measurable_id.snd.subtype_coe } /-- `univ Ξ± ≃ Ξ±` as measurable spaces. -/ def set.univ (Ξ± : Type*) [measurable_space Ξ±] : (univ : set Ξ±) ≃ᡐ Ξ± := { to_equiv := equiv.set.univ Ξ±, measurable_to_fun := measurable_id.subtype_coe, measurable_inv_fun := measurable_id.subtype_mk } /-- `{a} ≃ unit` as measurable spaces. -/ def set.singleton (a : Ξ±) : ({a} : set Ξ±) ≃ᡐ unit := { to_equiv := equiv.set.singleton a, measurable_to_fun := measurable_const, measurable_inv_fun := measurable_const } /-- A set is equivalent to its image under a function `f` as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.image (f : Ξ± β†’ Ξ²) (s : set Ξ±) (hf : injective f) (hfm : measurable f) (hfi : βˆ€ s, measurable_set s β†’ measurable_set (f '' s)) : s ≃ᡐ (f '' s) := { to_equiv := equiv.set.image f s hf, measurable_to_fun := (hfm.comp measurable_id.subtype_coe).subtype_mk, measurable_inv_fun := begin rintro t ⟨u, hu, rfl⟩, simp [preimage_preimage, set.image_symm_preimage hf], exact measurable_subtype_coe (hfi u hu) end } /-- The domain of `f` is equivalent to its range as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.range (f : Ξ± β†’ Ξ²) (hf : injective f) (hfm : measurable f) (hfi : βˆ€ s, measurable_set s β†’ measurable_set (f '' s)) : Ξ± ≃ᡐ (range f) := (measurable_equiv.set.univ _).symm.trans $ (measurable_equiv.set.image f univ hf hfm hfi).trans $ measurable_equiv.cast (by rw image_univ) (by rw image_univ) /-- `Ξ±` is equivalent to its image in `Ξ± βŠ• Ξ²` as measurable spaces. -/ def set.range_inl : (range sum.inl : set (Ξ± βŠ• Ξ²)) ≃ᡐ Ξ± := { to_fun := Ξ» ab, match ab with | ⟨sum.inl a, _⟩ := a | ⟨sum.inr b, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := Ξ» a, ⟨sum.inl a, a, rfl⟩, left_inv := by { rintro ⟨ab, a, rfl⟩, refl }, right_inv := assume a, rfl, measurable_to_fun := assume s (hs : measurable_set s), begin refine ⟨_, hs.inl_image, set.ext _⟩, rintros ⟨ab, a, rfl⟩, simp [set.range_inl._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inl } /-- `Ξ²` is equivalent to its image in `Ξ± βŠ• Ξ²` as measurable spaces. -/ def set.range_inr : (range sum.inr : set (Ξ± βŠ• Ξ²)) ≃ᡐ Ξ² := { to_fun := Ξ» ab, match ab with | ⟨sum.inr b, _⟩ := b | ⟨sum.inl a, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := Ξ» b, ⟨sum.inr b, b, rfl⟩, left_inv := by { rintro ⟨ab, b, rfl⟩, refl }, right_inv := assume b, rfl, measurable_to_fun := assume s (hs : measurable_set s), begin refine ⟨_, measurable_set_inr_image hs, set.ext _⟩, rintros ⟨ab, b, rfl⟩, simp [set.range_inr._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inr } /-- Products distribute over sums (on the right) as measurable spaces. -/ def sum_prod_distrib (Ξ± Ξ² Ξ³) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] : (Ξ± βŠ• Ξ²) Γ— Ξ³ ≃ᡐ (Ξ± Γ— Ξ³) βŠ• (Ξ² Γ— Ξ³) := { to_equiv := sum_prod_distrib Ξ± Ξ² Ξ³, measurable_to_fun := begin refine measurable_of_measurable_union_cover ((range sum.inl).prod univ) ((range sum.inr).prod univ) (measurable_set_range_inl.prod measurable_set.univ) (measurable_set_range_inr.prod measurable_set.univ) (by { rintro ⟨a|b, c⟩; simp [set.prod_eq] }) _ _, { refine (set.prod (range sum.inl) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inl, ext ⟨a, c⟩, refl }, { refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inr, ext ⟨b, c⟩, refl } end, measurable_inv_fun := measurable_sum ((measurable_inl.comp measurable_fst).prod_mk measurable_snd) ((measurable_inr.comp measurable_fst).prod_mk measurable_snd) } /-- Products distribute over sums (on the left) as measurable spaces. -/ def prod_sum_distrib (Ξ± Ξ² Ξ³) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] : Ξ± Γ— (Ξ² βŠ• Ξ³) ≃ᡐ (Ξ± Γ— Ξ²) βŠ• (Ξ± Γ— Ξ³) := prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm /-- Products distribute over sums as measurable spaces. -/ def sum_prod_sum (Ξ± Ξ² Ξ³ Ξ΄) [measurable_space Ξ±] [measurable_space Ξ²] [measurable_space Ξ³] [measurable_space Ξ΄] : (Ξ± βŠ• Ξ²) Γ— (Ξ³ βŠ• Ξ΄) ≃ᡐ ((Ξ± Γ— Ξ³) βŠ• (Ξ± Γ— Ξ΄)) βŠ• ((Ξ² Γ— Ξ³) βŠ• (Ξ² Γ— Ξ΄)) := (sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _) variables {Ο€ Ο€' : Ξ΄' β†’ Type*} [βˆ€ x, measurable_space (Ο€ x)] [βˆ€ x, measurable_space (Ο€' x)] /-- A family of measurable equivalences `Ξ  a, β₁ a ≃ᡐ Ξ²β‚‚ a` generates a measurable equivalence between `Ξ  a, β₁ a` and `Ξ  a, Ξ²β‚‚ a`. -/ def Pi_congr_right (e : Ξ  a, Ο€ a ≃ᡐ Ο€' a) : (Ξ  a, Ο€ a) ≃ᡐ (Ξ  a, Ο€' a) := { to_equiv := Pi_congr_right (Ξ» a, (e a).to_equiv), measurable_to_fun := measurable_pi_lambda _ (Ξ» i, (e i).measurable_to_fun.comp (measurable_pi_apply i)), measurable_inv_fun := measurable_pi_lambda _ (Ξ» i, (e i).measurable_inv_fun.comp (measurable_pi_apply i)) } /-- Pi-types are measurably equivalent to iterated products. -/ noncomputable def pi_measurable_equiv_tprod {l : list Ξ΄'} (hnd : l.nodup) (h : βˆ€ i, i ∈ l) : (Ξ  i, Ο€ i) ≃ᡐ list.tprod Ο€ l := { to_equiv := list.tprod.pi_equiv_tprod hnd h, measurable_to_fun := measurable_tprod_mk l, measurable_inv_fun := measurable_tprod_elim' h } end measurable_equiv /-- A pi-system is a collection of subsets of `Ξ±` that is closed under intersections of sets that are not disjoint. Usually it is also required that the collection is nonempty, but we don't do that here. -/ def is_pi_system {Ξ±} (C : set (set Ξ±)) : Prop := βˆ€ s t ∈ C, (s ∩ t : set Ξ±).nonempty β†’ s ∩ t ∈ C namespace measurable_space lemma is_pi_system_measurable_set [measurable_space Ξ±] : is_pi_system {s : set Ξ± | measurable_set s} := Ξ» s t hs ht _, hs.inter ht /-- A Dynkin system is a collection of subsets of a type `Ξ±` that contains the empty set, is closed under complementation and under countable union of pairwise disjoint sets. The disjointness condition is the only difference with `Οƒ`-algebras. The main purpose of Dynkin systems is to provide a powerful induction rule for Οƒ-algebras generated by intersection stable set systems. A Dynkin system is also known as a "Ξ»-system" or a "d-system". -/ structure dynkin_system (Ξ± : Type*) := (has : set Ξ± β†’ Prop) (has_empty : has βˆ…) (has_compl : βˆ€ {a}, has a β†’ has aᢜ) (has_Union_nat : βˆ€ {f : β„• β†’ set Ξ±}, pairwise (disjoint on f) β†’ (βˆ€ i, has (f i)) β†’ has (⋃ i, f i)) namespace dynkin_system @[ext] lemma ext : βˆ€ {d₁ dβ‚‚ : dynkin_system Ξ±}, (βˆ€ s : set Ξ±, d₁.has s ↔ dβ‚‚.has s) β†’ d₁ = dβ‚‚ | ⟨s₁, _, _, _⟩ ⟨sβ‚‚, _, _, _⟩ h := have s₁ = sβ‚‚, from funext $ assume x, propext $ h x, by subst this variable (d : dynkin_system Ξ±) lemma has_compl_iff {a} : d.has aᢜ ↔ d.has a := ⟨λ h, by simpa using d.has_compl h, Ξ» h, d.has_compl h⟩ lemma has_univ : d.has univ := by simpa using d.has_compl d.has_empty theorem has_Union {Ξ²} [encodable Ξ²] {f : Ξ² β†’ set Ξ±} (hd : pairwise (disjoint on f)) (h : βˆ€ i, d.has (f i)) : d.has (⋃ i, f i) := by { rw ← encodable.Union_decode2, exact d.has_Union_nat (Union_decode2_disjoint_on hd) (Ξ» n, encodable.Union_decode2_cases d.has_empty h) } theorem has_union {s₁ sβ‚‚ : set Ξ±} (h₁ : d.has s₁) (hβ‚‚ : d.has sβ‚‚) (h : s₁ ∩ sβ‚‚ βŠ† βˆ…) : d.has (s₁ βˆͺ sβ‚‚) := by { rw union_eq_Union, exact d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 ⟨hβ‚‚, hβ‚βŸ©) } lemma has_diff {s₁ sβ‚‚ : set Ξ±} (h₁ : d.has s₁) (hβ‚‚ : d.has sβ‚‚) (h : sβ‚‚ βŠ† s₁) : d.has (s₁ \ sβ‚‚) := begin apply d.has_compl_iff.1, simp [diff_eq, compl_inter], exact d.has_union (d.has_compl h₁) hβ‚‚ (Ξ» x ⟨h₁, hβ‚‚βŸ©, h₁ (h hβ‚‚)), end instance : partial_order (dynkin_system Ξ±) := { le := Ξ» m₁ mβ‚‚, m₁.has ≀ mβ‚‚.has, le_refl := assume a b, le_refl _, le_trans := assume a b c, le_trans, le_antisymm := assume a b h₁ hβ‚‚, ext $ assume s, ⟨h₁ s, hβ‚‚ s⟩ } /-- Every measurable space (Οƒ-algebra) forms a Dynkin system -/ def of_measurable_space (m : measurable_space Ξ±) : dynkin_system Ξ± := { has := m.measurable_set', has_empty := m.measurable_set_empty, has_compl := m.measurable_set_compl, has_Union_nat := assume f _ hf, m.measurable_set_Union f hf } lemma of_measurable_space_le_of_measurable_space_iff {m₁ mβ‚‚ : measurable_space Ξ±} : of_measurable_space m₁ ≀ of_measurable_space mβ‚‚ ↔ m₁ ≀ mβ‚‚ := iff.rfl /-- The least Dynkin system containing a collection of basic sets. This inductive type gives the underlying collection of sets. -/ inductive generate_has (s : set (set Ξ±)) : set Ξ± β†’ Prop | basic : βˆ€ t ∈ s, generate_has t | empty : generate_has βˆ… | compl : βˆ€ {a}, generate_has a β†’ generate_has aᢜ | Union : βˆ€ {f : β„• β†’ set Ξ±}, pairwise (disjoint on f) β†’ (βˆ€ i, generate_has (f i)) β†’ generate_has (⋃ i, f i) lemma generate_has_compl {C : set (set Ξ±)} {s : set Ξ±} : generate_has C sᢜ ↔ generate_has C s := by { refine ⟨_, generate_has.compl⟩, intro h, convert generate_has.compl h, simp } /-- The least Dynkin system containing a collection of basic sets. -/ def generate (s : set (set Ξ±)) : dynkin_system Ξ± := { has := generate_has s, has_empty := generate_has.empty, has_compl := assume a, generate_has.compl, has_Union_nat := assume f, generate_has.Union } lemma generate_has_def {C : set (set Ξ±)} : (generate C).has = generate_has C := rfl instance : inhabited (dynkin_system Ξ±) := ⟨generate univ⟩ /-- If a Dynkin system is closed under binary intersection, then it forms a `Οƒ`-algebra. -/ def to_measurable_space (h_inter : βˆ€ s₁ sβ‚‚, d.has s₁ β†’ d.has sβ‚‚ β†’ d.has (s₁ ∩ sβ‚‚)) := { measurable_space . measurable_set' := d.has, measurable_set_empty := d.has_empty, measurable_set_compl := assume s h, d.has_compl h, measurable_set_Union := assume f hf, have βˆ€ n, d.has (disjointed f n), from assume n, disjointed_induct (hf n) (assume t i h, h_inter _ _ h $ d.has_compl $ hf i), have d.has (⋃ n, disjointed f n), from d.has_Union disjoint_disjointed this, by rwa [Union_disjointed] at this } lemma of_measurable_space_to_measurable_space (h_inter : βˆ€ s₁ sβ‚‚, d.has s₁ β†’ d.has sβ‚‚ β†’ d.has (s₁ ∩ sβ‚‚)) : of_measurable_space (d.to_measurable_space h_inter) = d := ext $ assume s, iff.rfl /-- If `s` is in a Dynkin system `d`, we can form the new Dynkin system `{s ∩ t | t ∈ d}`. -/ def restrict_on {s : set Ξ±} (h : d.has s) : dynkin_system Ξ± := { has := Ξ» t, d.has (t ∩ s), has_empty := by simp [d.has_empty], has_compl := assume t hts, have tᢜ ∩ s = ((t ∩ s)ᢜ) \ sᢜ, from set.ext $ assume x, by { by_cases x ∈ s; simp [h] }, by { rw [this], exact d.has_diff (d.has_compl hts) (d.has_compl h) (compl_subset_compl.mpr $ inter_subset_right _ _) }, has_Union_nat := assume f hd hf, begin rw [inter_comm, inter_Union], apply d.has_Union_nat, { exact Ξ» i j h x ⟨⟨_, hβ‚βŸ©, _, hβ‚‚βŸ©, hd i j h ⟨h₁, hβ‚‚βŸ© }, { simpa [inter_comm] using hf }, end } lemma generate_le {s : set (set Ξ±)} (h : βˆ€ t ∈ s, d.has t) : generate s ≀ d := Ξ» t ht, ht.rec_on h d.has_empty (assume a _ h, d.has_compl h) (assume f hd _ hf, d.has_Union hd hf) lemma generate_has_subset_generate_measurable {C : set (set Ξ±)} {s : set Ξ±} (hs : (generate C).has s) : (generate_from C).measurable_set' s := generate_le (of_measurable_space (generate_from C)) (Ξ» t, measurable_set_generate_from) s hs lemma generate_inter {s : set (set Ξ±)} (hs : is_pi_system s) {t₁ tβ‚‚ : set Ξ±} (ht₁ : (generate s).has t₁) (htβ‚‚ : (generate s).has tβ‚‚) : (generate s).has (t₁ ∩ tβ‚‚) := have generate s ≀ (generate s).restrict_on htβ‚‚, from generate_le _ $ assume s₁ hs₁, have (generate s).has s₁, from generate_has.basic s₁ hs₁, have generate s ≀ (generate s).restrict_on this, from generate_le _ $ assume sβ‚‚ hsβ‚‚, show (generate s).has (sβ‚‚ ∩ s₁), from (sβ‚‚ ∩ s₁).eq_empty_or_nonempty.elim (Ξ» h, h.symm β–Έ generate_has.empty) (Ξ» h, generate_has.basic _ (hs _ _ hsβ‚‚ hs₁ h)), have (generate s).has (tβ‚‚ ∩ s₁), from this _ htβ‚‚, show (generate s).has (s₁ ∩ tβ‚‚), by rwa [inter_comm], this _ ht₁ /-- If we have a collection of sets closed under binary intersections, then the Dynkin system it generates is equal to the Οƒ-algebra it generates. This result is known as the Ο€-Ξ» theorem. A collection of sets closed under binary intersection is called a "Ο€-system" if it is non-empty. -/ lemma generate_from_eq {s : set (set Ξ±)} (hs : is_pi_system s) : generate_from s = (generate s).to_measurable_space (assume t₁ tβ‚‚, generate_inter hs) := le_antisymm (generate_from_le $ assume t ht, generate_has.basic t ht) (of_measurable_space_le_of_measurable_space_iff.mp $ by { rw [of_measurable_space_to_measurable_space], exact (generate_le _ $ assume t ht, measurable_set_generate_from ht) }) end dynkin_system lemma induction_on_inter {C : set Ξ± β†’ Prop} {s : set (set Ξ±)} [m : measurable_space Ξ±] (h_eq : m = generate_from s) (h_inter : is_pi_system s) (h_empty : C βˆ…) (h_basic : βˆ€ t ∈ s, C t) (h_compl : βˆ€ t, measurable_set t β†’ C t β†’ C tᢜ) (h_union : βˆ€ f : β„• β†’ set Ξ±, pairwise (disjoint on f) β†’ (βˆ€ i, measurable_set (f i)) β†’ (βˆ€ i, C (f i)) β†’ C (⋃ i, f i)) : βˆ€ ⦃t⦄, measurable_set t β†’ C t := have eq : measurable_set = dynkin_system.generate_has s, by { rw [h_eq, dynkin_system.generate_from_eq h_inter], refl }, assume t ht, have dynkin_system.generate_has s t, by rwa [eq] at ht, this.rec_on h_basic h_empty (assume t ht, h_compl t $ by { rw [eq], exact ht }) (assume f hf ht, h_union f hf $ assume i, by { rw [eq], exact ht _ }) end measurable_space namespace filter variables [measurable_space Ξ±] /-- A filter `f` is measurably generates if each `s ∈ f` includes a measurable `t ∈ f`. -/ class is_measurably_generated (f : filter Ξ±) : Prop := (exists_measurable_subset : βˆ€ ⦃s⦄, s ∈ f β†’ βˆƒ t ∈ f, measurable_set t ∧ t βŠ† s) instance is_measurably_generated_bot : is_measurably_generated (βŠ₯ : filter Ξ±) := ⟨λ _ _, βŸ¨βˆ…, mem_bot_sets, measurable_set.empty, empty_subset _⟩⟩ instance is_measurably_generated_top : is_measurably_generated (⊀ : filter Ξ±) := ⟨λ s hs, ⟨univ, univ_mem_sets, measurable_set.univ, Ξ» x _, hs x⟩⟩ lemma eventually.exists_measurable_mem {f : filter Ξ±} [is_measurably_generated f] {p : Ξ± β†’ Prop} (h : βˆ€αΆ  x in f, p x) : βˆƒ s ∈ f, measurable_set s ∧ βˆ€ x ∈ s, p x := is_measurably_generated.exists_measurable_subset h lemma eventually.exists_measurable_mem_of_lift' {f : filter Ξ±} [is_measurably_generated f] {p : set Ξ± β†’ Prop} (h : βˆ€αΆ  s in f.lift' powerset, p s) : βˆƒ s ∈ f, measurable_set s ∧ p s := let ⟨s, hsf, hs⟩ := eventually_lift'_powerset.1 h, ⟨t, htf, htm, hts⟩ := is_measurably_generated.exists_measurable_subset hsf in ⟨t, htf, htm, hs t hts⟩ instance inf_is_measurably_generated (f g : filter Ξ±) [is_measurably_generated f] [is_measurably_generated g] : is_measurably_generated (f βŠ“ g) := begin refine ⟨_⟩, rintros t ⟨sf, hsf, sg, hsg, ht⟩, rcases is_measurably_generated.exists_measurable_subset hsf with ⟨s'f, hs'f, hmf, hs'sf⟩, rcases is_measurably_generated.exists_measurable_subset hsg with ⟨s'g, hs'g, hmg, hs'sg⟩, refine ⟨s'f ∩ s'g, inter_mem_inf_sets hs'f hs'g, hmf.inter hmg, _⟩, exact subset.trans (inter_subset_inter hs'sf hs'sg) ht end lemma principal_is_measurably_generated_iff {s : set Ξ±} : is_measurably_generated (π“Ÿ s) ↔ measurable_set s := begin refine ⟨_, Ξ» hs, ⟨λ t ht, ⟨s, mem_principal_self s, hs, ht⟩⟩⟩, rintros ⟨hs⟩, rcases hs (mem_principal_self s) with ⟨t, ht, htm, hts⟩, have : t = s := subset.antisymm hts ht, rwa ← this end alias principal_is_measurably_generated_iff ↔ _ measurable_set.principal_is_measurably_generated instance infi_is_measurably_generated {f : ΞΉ β†’ filter Ξ±} [βˆ€ i, is_measurably_generated (f i)] : is_measurably_generated (β¨… i, f i) := begin refine ⟨λ s hs, _⟩, rw [← equiv.plift.surjective.infi_comp, mem_infi_iff] at hs, rcases hs with ⟨t, ht, ⟨V, hVf, hVs⟩⟩, choose U hUf hU using Ξ» i, is_measurably_generated.exists_measurable_subset (hVf i), refine βŸ¨β‹‚ i : t, U i, _, _, _⟩, { rw [← equiv.plift.surjective.infi_comp, mem_infi_iff], refine ⟨t, ht, U, hUf, subset.refl _⟩ }, { haveI := ht.countable.to_encodable, refine measurable_set.Inter (Ξ» i, (hU i).1) }, { exact subset.trans (Inter_subset_Inter $ Ξ» i, (hU i).2) hVs } end end filter /-- We say that a collection of sets is countably spanning if a countable subset spans the whole type. This is a useful condition in various parts of measure theory. For example, it is a needed condition to show that the product of two collections generate the product sigma algebra, see `generate_from_prod_eq`. -/ def is_countably_spanning (C : set (set Ξ±)) : Prop := βˆƒ (s : β„• β†’ set Ξ±), (βˆ€ n, s n ∈ C) ∧ (⋃ n, s n) = univ lemma is_countably_spanning_measurable_set [measurable_space Ξ±] : is_countably_spanning {s : set Ξ± | measurable_set s} := ⟨λ _, univ, Ξ» _, measurable_set.univ, Union_const _⟩
04c088d6372caa05387d942d175d2f5b898fb662
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/data/nat/enat.lean
45cb97c4043df9ea18f9997a17925170f62ac03a
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
7,898
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes Natural numbers with infinity, represented as roption β„•. -/ import data.pfun algebra.ordered_group import tactic.norm_cast open roption lattice def enat : Type := roption β„• namespace enat instance : has_zero enat := ⟨some 0⟩ instance : has_one enat := ⟨some 1⟩ instance : has_add enat := ⟨λ x y, ⟨x.dom ∧ y.dom, Ξ» h, get x h.1 + get y h.2⟩⟩ instance : has_coe β„• enat := ⟨some⟩ instance (n : β„•) : decidable (n : enat).dom := is_true trivial @[simp] lemma coe_inj {x y : β„•} : (x : enat) = y ↔ x = y := roption.some_inj instance : add_comm_monoid enat := { add := (+), zero := (0), add_comm := Ξ» x y, roption.ext' and.comm (Ξ» _ _, add_comm _ _), zero_add := Ξ» x, roption.ext' (true_and _) (Ξ» _ _, zero_add _), add_zero := Ξ» x, roption.ext' (and_true _) (Ξ» _ _, add_zero _), add_assoc := Ξ» x y z, roption.ext' and.assoc (Ξ» _ _, add_assoc _ _ _) } instance : has_le enat := ⟨λ x y, βˆƒ h : y.dom β†’ x.dom, βˆ€ hy : y.dom, x.get (h hy) ≀ y.get hy⟩ instance : has_top enat := ⟨none⟩ instance : has_bot enat := ⟨0⟩ instance : has_sup enat := ⟨λ x y, ⟨x.dom ∧ y.dom, Ξ» h, x.get h.1 βŠ” y.get h.2⟩⟩ @[elab_as_eliminator] protected lemma cases_on {P : enat β†’ Prop} : βˆ€ a : enat, P ⊀ β†’ (βˆ€ n : β„•, P n) β†’ P a := roption.induction_on @[simp] lemma top_add (x : enat) : ⊀ + x = ⊀ := roption.ext' (false_and _) (Ξ» h, h.left.elim) @[simp] lemma add_top (x : enat) : x + ⊀ = ⊀ := by rw [add_comm, top_add] @[simp, squash_cast] lemma coe_zero : ((0 : β„•) : enat) = 0 := rfl @[simp, squash_cast] lemma coe_one : ((1 : β„•) : enat) = 1 := rfl @[simp, move_cast] lemma coe_add (x y : β„•) : ((x + y : β„•) : enat) = x + y := roption.ext' (and_true _).symm (Ξ» _ _, rfl) @[simp] lemma coe_add_get {x : β„•} {y : enat} (h : ((x : enat) + y).dom) : get ((x : enat) + y) h = x + get y h.2 := rfl @[simp] lemma get_add {x y : enat} (h : (x + y).dom) : get (x + y) h = x.get h.1 + y.get h.2 := rfl @[simp, squash_cast] lemma coe_get {x : enat} (h : x.dom) : (x.get h : enat) = x := roption.ext' (iff_of_true trivial h) (Ξ» _ _, rfl) @[simp] lemma get_zero (h : (0 : enat).dom) : (0 : enat).get h = 0 := rfl @[simp] lemma get_one (h : (1 : enat).dom) : (1 : enat).get h = 1 := rfl lemma dom_of_le_some {x : enat} {y : β„•} : x ≀ y β†’ x.dom := Ξ» ⟨h, _⟩, h trivial instance : partial_order enat := { le := (≀), le_refl := Ξ» x, ⟨id, Ξ» _, le_refl _⟩, le_trans := Ξ» x y z ⟨hxy₁, hxyβ‚‚βŸ© ⟨hyz₁, hyzβ‚‚βŸ©, ⟨hxy₁ ∘ hyz₁, Ξ» _, le_trans (hxyβ‚‚ _) (hyzβ‚‚ _)⟩, le_antisymm := Ξ» x y ⟨hxy₁, hxyβ‚‚βŸ© ⟨hyx₁, hyxβ‚‚βŸ©, roption.ext' ⟨hyx₁, hxyβ‚βŸ© (Ξ» _ _, le_antisymm (hxyβ‚‚ _) (hyxβ‚‚ _)) } @[simp, elim_cast] lemma coe_le_coe {x y : β„•} : (x : enat) ≀ y ↔ x ≀ y := ⟨λ ⟨_, h⟩, h trivial, Ξ» h, ⟨λ _, trivial, Ξ» _, h⟩⟩ @[simp, elim_cast] lemma coe_lt_coe {x y : β„•} : (x : enat) < y ↔ x < y := by rw [lt_iff_le_not_le, lt_iff_le_not_le, coe_le_coe, coe_le_coe] lemma get_le_get {x y : enat} {hx : x.dom} {hy : y.dom} : x.get hx ≀ y.get hy ↔ x ≀ y := by conv { to_lhs, rw [← coe_le_coe, coe_get, coe_get]} instance semilattice_sup_bot : semilattice_sup_bot enat := { sup := (βŠ”), bot := (βŠ₯), bot_le := Ξ» _, ⟨λ _, trivial, Ξ» _, nat.zero_le _⟩, le_sup_left := Ξ» _ _, ⟨and.left, Ξ» _, le_sup_left⟩, le_sup_right := Ξ» _ _, ⟨and.right, Ξ» _, le_sup_right⟩, sup_le := Ξ» x y z ⟨hx₁, hxβ‚‚βŸ© ⟨hy₁, hyβ‚‚βŸ©, ⟨λ hz, ⟨hx₁ hz, hy₁ hz⟩, Ξ» _, sup_le (hxβ‚‚ _) (hyβ‚‚ _)⟩, ..enat.partial_order } instance order_top : order_top enat := { top := (⊀), le_top := Ξ» x, ⟨λ h, false.elim h, Ξ» hy, false.elim hy⟩, ..enat.semilattice_sup_bot } lemma coe_lt_top (x : β„•) : (x : enat) < ⊀ := lt_of_le_of_ne le_top (Ξ» h, absurd (congr_arg dom h) true_ne_false) @[simp] lemma coe_ne_top (x : β„•) : (x : enat) β‰  ⊀ := ne_of_lt (coe_lt_top x) lemma pos_iff_one_le {x : enat} : 0 < x ↔ 1 ≀ x := enat.cases_on x ⟨λ _, le_top, Ξ» _, coe_lt_top _⟩ (Ξ» n, ⟨λ h, enat.coe_le_coe.2 (enat.coe_lt_coe.1 h), Ξ» h, enat.coe_lt_coe.2 (enat.coe_le_coe.1 h)⟩) noncomputable instance : decidable_linear_order enat := { le_total := Ξ» x y, enat.cases_on x (or.inr le_top) (enat.cases_on y (Ξ» _, or.inl le_top) (Ξ» x y, (le_total x y).elim (or.inr ∘ coe_le_coe.2) (or.inl ∘ coe_le_coe.2))), decidable_le := classical.dec_rel _, ..enat.partial_order } noncomputable instance : bounded_lattice enat := { inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := Ξ» _ _ _, le_min, ..enat.order_top, ..enat.semilattice_sup_bot } lemma sup_eq_max {a b : enat} : a βŠ” b = max a b := le_antisymm (sup_le (le_max_left _ _) (le_max_right _ _)) (max_le le_sup_left le_sup_right) lemma inf_eq_min {a b : enat} : a βŠ“ b = min a b := rfl instance : ordered_comm_monoid enat := { add_le_add_left := Ξ» a b ⟨h₁, hβ‚‚βŸ© c, enat.cases_on c (by simp) (Ξ» c, ⟨λ h, and.intro trivial (h₁ h.2), Ξ» _, add_le_add_left (hβ‚‚ _) c⟩), lt_of_add_lt_add_left := Ξ» a b c, enat.cases_on a (Ξ» h, by simpa [lt_irrefl] using h) (Ξ» a, enat.cases_on b (Ξ» h, absurd h (not_lt_of_ge (by rw add_top; exact le_top))) (Ξ» b, enat.cases_on c (Ξ» _, coe_lt_top _) (Ξ» c h, coe_lt_coe.2 (by rw [← coe_add, ← coe_add, coe_lt_coe] at h; exact lt_of_add_lt_add_left h)))), ..enat.decidable_linear_order, ..enat.add_comm_monoid } instance : canonically_ordered_monoid enat := { le_iff_exists_add := Ξ» a b, enat.cases_on b (iff_of_true le_top ⟨⊀, (add_top _).symm⟩) (Ξ» b, enat.cases_on a (iff_of_false (not_le_of_gt (coe_lt_top _)) (not_exists.2 (Ξ» x, ne_of_lt (by rw [top_add]; exact coe_lt_top _)))) (Ξ» a, ⟨λ h, ⟨(b - a : β„•), by rw [← coe_add, coe_inj, add_comm, nat.sub_add_cancel (coe_le_coe.1 h)]⟩, (Ξ» ⟨c, hc⟩, enat.cases_on c (Ξ» hc, hc.symm β–Έ show (a : enat) ≀ a + ⊀, by rw [add_top]; exact le_top) (Ξ» c (hc : (b : enat) = a + c), coe_le_coe.2 (by rw [← coe_add, coe_inj] at hc; rw hc; exact nat.le_add_right _ _)) hc)⟩)), ..enat.semilattice_sup_bot, ..enat.ordered_comm_monoid } section with_top def to_with_top (x : enat) [decidable x.dom]: with_top β„• := x.to_option lemma to_with_top_top : to_with_top ⊀ = ⊀ := rfl @[simp] lemma to_with_top_top' {h : decidable (⊀ : enat).dom} : to_with_top ⊀ = ⊀ := by convert to_with_top_top lemma to_with_top_zero : to_with_top 0 = 0 := rfl @[simp] lemma to_with_top_zero' {h : decidable (0 : enat).dom}: to_with_top 0 = 0 := by convert to_with_top_zero lemma to_with_top_coe (n : β„•) : to_with_top n = n := rfl @[simp] lemma to_with_top_coe' (n : β„•) {h : decidable (n : enat).dom} : to_with_top (n : enat) = n := by convert to_with_top_coe n @[simp] lemma to_with_top_le {x y : enat} : Ξ  [decidable x.dom] [decidable y.dom], by exactI to_with_top x ≀ to_with_top y ↔ x ≀ y := enat.cases_on y (by simp) (enat.cases_on x (by simp) (by intros; simp)) @[simp] lemma to_with_top_lt {x y : enat} [decidable x.dom] [decidable y.dom] : to_with_top x < to_with_top y ↔ x < y := by simp only [lt_iff_le_not_le, to_with_top_le] end with_top lemma lt_wf : well_founded ((<) : enat β†’ enat β†’ Prop) := show well_founded (Ξ» a b : enat, a < b), by haveI := classical.dec; simp only [to_with_top_lt.symm] {eta := ff}; exact inv_image.wf _ (with_top.well_founded_lt nat.lt_wf) instance : has_well_founded enat := ⟨(<), lt_wf⟩ end enat
1a83123e17c3f351203ef432f4b676e80ba0a2fc
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Init/NotationExtra.lean
916ec8d78282bdaef7f39de717d3cfe804d3da6e
[ "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
6,812
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 Extra notation that depends on Init/Meta -/ prelude import Init.Meta import Init.Data.Array.Subarray namespace Lean -- Auxiliary parsers and functions for declaring notation with binders syntax binderIdent := ident <|> "_" syntax unbracktedExplicitBinders := binderIdent+ (" : " term)? syntax bracketedExplicitBinders := "(" binderIdent+ " : " term ")" syntax explicitBinders := bracketedExplicitBinders+ <|> unbracktedExplicitBinders def expandExplicitBindersAux (combinator : Syntax) (idents : Array Syntax) (type? : Option Syntax) (body : Syntax) : MacroM Syntax := let rec loop (i : Nat) (acc : Syntax) := do match i with | 0 => pure acc | i+1 => let ident := idents[i][0] let acc ← match ident.isIdent, type? with | true, none => `($combinator fun $ident => $acc) | true, some type => `($combinator fun $ident:ident : $type => $acc) | false, none => `($combinator fun _ => $acc) | false, some type => `($combinator fun _ : $type => $acc) loop i (acc.copyInfo (← getRef)) loop idents.size body def expandBrackedBindersAux (combinator : Syntax) (binders : Array Syntax) (body : Syntax) : MacroM Syntax := let rec loop (i : Nat) (acc : Syntax) := do match i with | 0 => pure acc | i+1 => let idents := binders[i][1].getArgs let type := binders[i][3] loop i (← expandExplicitBindersAux combinator idents (some type) acc) loop binders.size body def expandExplicitBinders (combinatorDeclName : Name) (explicitBinders : Syntax) (body : Syntax) : MacroM Syntax := do let combinator := mkIdentFrom (← getRef) combinatorDeclName let explicitBinders := explicitBinders[0] if explicitBinders.getKind == `Lean.unbracktedExplicitBinders then let idents := explicitBinders[0].getArgs let type? := if explicitBinders[1].isNone then none else some explicitBinders[1][1] expandExplicitBindersAux combinator idents type? body else if explicitBinders.getArgs.all (Β·.getKind == `Lean.bracketedExplicitBinders) then expandBrackedBindersAux combinator explicitBinders.getArgs body else Macro.throwError "unexpected explicit binder" def expandBrackedBinders (combinatorDeclName : Name) (bracketedExplicitBinders : Syntax) (body : Syntax) : MacroM Syntax := do let combinator := mkIdentFrom (← getRef) combinatorDeclName expandBrackedBindersAux combinator #[bracketedExplicitBinders] body syntax unifConstraint := term (" =?= " <|> " β‰Ÿ ") term syntax unifConstraintElem := colGe unifConstraint ", "? syntax attrKind "unif_hint " (ident)? bracketedBinder* " where " withPosition(unifConstraintElem*) ("|-" <|> "⊒ ") unifConstraint : command private def mkHintBody (cs : Array Syntax) (p : Syntax) : MacroM Syntax := do let mut body ← `($(p[0]) = $(p[2])) for c in cs.reverse do body ← `($(c[0][0]) = $(c[0][2]) β†’ $body) return body macro_rules | `($kind:attrKind unif_hint $bs:explicitBinder* where $cs* |- $p) => do let body ← mkHintBody cs p `(@[$kind:attrKind unificationHint] def hint $bs:explicitBinder* : Sort _ := $body) | `($kind:attrKind unif_hint $n:ident $bs* where $cs* |- $p) => do let body ← mkHintBody cs p `(@[$kind:attrKind unificationHint] def $n:ident $bs:explicitBinder* : Sort _ := $body) end Lean open Lean macro "βˆƒ" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Exists xs b macro "exists" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Exists xs b macro "Ξ£" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Sigma xs b macro "Ξ£'" xs:explicitBinders ", " b:term : term => expandExplicitBinders `PSigma xs b macro:35 xs:bracketedExplicitBinders "Γ—" b:term:35 : term => expandBrackedBinders `Sigma xs b macro:35 xs:bracketedExplicitBinders "Γ—'" b:term:35 : term => expandBrackedBinders `PSigma xs b syntax "funext " (colGt term:max)+ : tactic macro_rules | `(tactic|funext $xs*) => if xs.size == 1 then `(tactic| apply funext; intro $(xs[0]):term) else `(tactic| apply funext; intro $(xs[0]):term; funext $(xs[1:])*) macro_rules | `(%[ $[$x],* | $k ]) => if x.size < 8 then x.foldrM (init := k) fun x k => `(List.cons $x $k) else let m := x.size / 2 let y := x[m:] let z := x[:m] `(let y := %[ $[$y],* | $k ] %[ $[$z],* | y ]) /- Expands ``` class abbrev C <params> := D_1, ..., D_n ``` into ``` class C <params> extends D_1, ..., D_n instance <params> [D_1] ... [D_n] : C <params> := C.mk _ ... _ inferInstance ... inferInstance ``` -/ syntax "class " "abbrev " ident bracketedBinder* ":=" withPosition(group(colGe term ","?)*) : command macro_rules | `(class abbrev $name:ident $params* := $[ $parents:term $[,]? ]*) => do let mut auxBinders := #[] let mut typeArgs := #[] let mut ctorArgs := #[] let hole ← `(_) for param in params do match param with | `(bracketedBinder| ( $ids:ident* $[: $type:term]? ) ) => auxBinders := auxBinders.push (← `(bracketedBinder| { $ids:ident* }) ) typeArgs := typeArgs ++ ids ctorArgs := ctorArgs ++ ids.map fun _ => hole | `(bracketedBinder| [ $id:ident : $type:term ] ) => auxBinders := auxBinders.push param typeArgs := typeArgs.push hole ctorArgs := ctorArgs.push hole | `(bracketedBinder| [ $type:term ] ) => auxBinders := auxBinders.push param typeArgs := typeArgs.push hole ctorArgs := ctorArgs.push hole | `(bracketedBinder| { $ids:ident* $[: $type ]? } ) => auxBinders := auxBinders.push param typeArgs := typeArgs ++ ids.map fun _ => hole ctorArgs := typeArgs ++ ids.map fun _ => hole | _ => Lean.Macro.throwErrorAt param "unexpected binder at `class abbrev` macro" let inferInst ← `(inferInstance) for parent in parents do auxBinders := auxBinders.push (← `(bracketedBinder| [ $parent:term ])) ctorArgs := ctorArgs.push inferInst let view := Lean.extractMacroScopes name.getId let ctor := mkIdentFrom name { view with name := view.name ++ `mk }.review `(class $name:ident $params* extends $[$parents:term],* instance $auxBinders:explicitBinder* : @ $name:ident $typeArgs* := @ $ctor:ident $ctorArgs*) /- Similar to `first`, but succeeds only if one the given tactics solves the curretn goal. -/ syntax "solve " "|"? sepBy1(tacticSeq, "|") : tactic macro_rules | `(tactic| solve $[|]? $ts:tacticSeq|*) => `(tactic| focus first $[($ts); done]|*)
9da21a648268976532e7933958447af537f1996c
4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1
/omin/presheaf2.lean
dd08128ea2a013eea2fe70deedc94465fec48919
[]
no_license
rwbarton/lean-omin
da209ed061d64db65a8f7f71f198064986f30eb9
fd733c6d95ef6f4743aae97de5e15df79877c00e
refs/heads/master
1,674,408,673,325
1,607,343,535,000
1,607,343,535,000
285,150,399
9
0
null
null
null
null
UTF-8
Lean
false
false
15,052
lean
import .def_coords universe u namespace o_minimal open_locale finvec variables {R : Type u} (S : struc R) -- We set up a minimal theory of (literal) definable subsets of Rⁿ -- and definable functions between them. /-- A bundled definable subset of some Rⁿ. -/ structure Def : Type u := (ambdim : β„•) (to_set : set (finvec ambdim R)) (is_definable : S.definable to_set) variables {S} instance : has_coe_to_sort (Def S) := ⟨Type u, Ξ» X, X.to_set⟩ lemma Def.is_def_coords (X : Def S) : S.def_coords (set.univ : set X) := begin unfold struc.def_coords, convert X.is_definable, simp end variables (S) def struc.definable_fun {X Y : Def S} (f : X β†’ Y) : Prop := S.def_coords {z : X Γ— Y | f z.1 = z.2} variables {S} @[ext] structure Hom (X Y : Def S) : Type u := (to_fun : X β†’ Y) (is_definable : S.definable_fun to_fun) instance {X Y : Def S} : has_coe_to_fun (Hom X Y) := ⟨_, Ξ» f, f.to_fun⟩ --instance : category_theory.has_hom (Def S) := { hom := Hom } local infixr ` ⟢ `:10 := Hom @[simp] lemma Hom.to_fun_eq_coe {X Y : Def S} (f : X ⟢ Y) : f.to_fun = f := rfl -- TODO float out proofs of definability of identity, composition -- (useful later in presheaf stuff, notably representable instance) def Def.id (X : Def S) : X ⟢ X := { to_fun := id, is_definable := struc.def_coords.diag X.is_def_coords } def Hom.comp {X Y Z : Def S} (g : Y ⟢ Z) (f : X ⟢ Y) : X ⟢ Z := { to_fun := g.to_fun ∘ f.to_fun, is_definable := begin suffices : S.def_coords {p : X Γ— Z | βˆƒ y, f p.1 = y ∧ g y = p.2}, { convert this, ext ⟨x, z⟩, simp [struc.definable_fun] }, have dXZY : S.def_coords (set.univ : set ((X Γ— Z) Γ— Y)) := (X.is_def_coords.prod_univ Z.is_def_coords).prod_univ Y.is_def_coords, apply struc.def_coords.exists, apply struc.def_coords.inter, { let Ο† : (X Γ— Z) Γ— Y β†’ X Γ— Y := Ξ» p, (p.1.1, p.2), have : is_reindexing R Ο† := is_reindexing.prod R ((is_reindexing.fst R).comp R (is_reindexing.fst R)) (is_reindexing.snd R), refine struc.def_coords.reindex dXZY this f.is_definable }, { let Ο† : (X Γ— Z) Γ— Y β†’ Y Γ— Z := Ξ» p, (p.2, p.1.2), have : is_reindexing R Ο† := is_reindexing.prod R (is_reindexing.snd R) ((is_reindexing.snd R).comp R (is_reindexing.fst R)), refine struc.def_coords.reindex dXZY this g.is_definable }, end } lemma Hom.comp_id {X Y : Def S} (f : X ⟢ Y) : f.comp (Def.id X) = f := by { ext, refl } lemma Hom.id_comp {X Y : Def S} (f : X ⟢ Y) : (Def.id Y).comp f = f := by { ext, refl } lemma Hom.comp_assoc {W X Y Z : Def S} (h : Y ⟢ Z) (g : X ⟢ Y) (f : W ⟢ X) : (h.comp g).comp f = h.comp (g.comp f) := rfl def pt : Def S := { ambdim := 0, to_set := set.univ, is_definable := S.definable_univ 0 } instance pt.unique : unique (pt : Def S) := ⟨⟨⟨fin_zero_elim, trivial⟩⟩, Ξ» x, by { ext i, fin_cases i }⟩ /-! ### Presheaf stuff -/ variables (S) -- TODO: generalize to Sort? class definable_psh (X : Type*) := (definable : Ξ  {K : Def S}, (K β†’ X) β†’ Prop) (definable_precomp : βˆ€ {L K : Def S} (Ο† : L ⟢ K) {f : K β†’ X}, definable f β†’ definable (f ∘ Ο†)) -- TODO: apply bug?? def definable {X : Type*} [definable_psh S X] (x : X) : Prop := definable_psh.definable (Ξ» (_ : (pt : Def S)), x) variables {S} def definable_psh.definable' {X : Type*} (h : definable_psh S X) {K : Def S} (f : K β†’ X) : Prop := definable_psh.definable f instance Def.definable_psh (X : Def S) : definable_psh S X := { definable := Ξ» K f, S.definable_fun f, definable_precomp := begin rintros L K Ο† f h, exact ((⟨f, h⟩ : K ⟢ X).comp Ο†).is_definable end } lemma pt.definable {K : Def S} {f : K β†’ (pt : Def S)} : definable_psh.definable f := begin change S.definable _, convert K.is_def_coords using 1, ext x, split, { rintros ⟨⟨k, p⟩, -, rfl⟩, refine ⟨k, trivial, _⟩, simp }, { rintros ⟨k, -, rfl⟩, refine ⟨⟨k, default _⟩, show _ = _, by cc, _⟩, simp } end instance {X Y : Type*} [definable_psh S X] [definable_psh S Y] : definable_psh S (X Γ— Y) := { definable := Ξ» K f, definable_psh.definable (prod.fst ∘ f) ∧ definable_psh.definable (prod.snd ∘ f), definable_precomp := begin rintros L K Ο† _ ⟨h₁, hβ‚‚βŸ©, exact ⟨definable_psh.definable_precomp Ο† h₁, definable_psh.definable_precomp Ο† hβ‚‚βŸ©, end } instance function.definable_psh {X Y : Type*} [hX : definable_psh S X] [hY : definable_psh S Y] : definable_psh S (X β†’ Y) := { definable := Ξ» K f, βˆ€ (M : Def S) {g : M β†’ K Γ— X} (h : definable_psh.definable g), definable_psh.definable (function.uncurry f ∘ g), definable_precomp := Ξ» L K Ο† f hf M g hg, begin suffices : definable_psh.definable (Ξ» m, (Ο† (g m).1, (g m).2)), { apply hf M this }, split, { exact definable_psh.definable_precomp ⟨λ m, (g m).1, hg.1⟩ (show definable_psh.definable Ο†, from Ο†.is_definable) }, { exact hg.2 } end } lemma definable_fun {X Y : Type*} [definable_psh S X] [definable_psh S Y] {f : X β†’ Y} : definable S f ↔ βˆ€ {K : Def S} (Ο† : K β†’ X), definable_psh.definable Ο† β†’ definable_psh.definable (f ∘ Ο†) := begin split; intro H, { intros K Ο† hΟ†, -- TODO: This proof is awkward specialize H K, swap, { exact (Ξ» k, (default _, Ο† k)) }, exact H ⟨pt.definable, hΟ†βŸ© }, { intros K Ο† hΟ†, exact H _ hΟ†.2 } end lemma definable_app {X Y : Type*} [definable_psh S X] [definable_psh S Y] {f : X β†’ Y} (hf : definable S f) {x : X} (hx : definable S x) : definable S (f x) := begin rw definable_fun at hf, exact hf _ hx end lemma definable.app_ctx {Ξ“ X Y : Type*} [definable_psh S Ξ“] [definable_psh S X] [definable_psh S Y] {f : Ξ“ β†’ X β†’ Y} (hf : definable S f) {x : Ξ“ β†’ X} (hx : definable S x) : definable S (Ξ» Ξ³, f Ξ³ (x Ξ³)) := begin rw definable_fun at ⊒ hf hx, intros K Ο† hΟ†, change definable_psh.definable (Ξ» k, f (Ο† k) (x (Ο† k))), specialize hf Ο† hΟ†, specialize hf K, swap, { exact Ξ» k, (k, x (Ο† k)) }, exact hf ⟨(Def.id K).is_definable, hx Ο† hΟ†βŸ© end lemma definable_yoneda {K : Def S} {X : Type*} [definable_psh S X] {f : K β†’ X} : definable_psh.definable f ↔ definable S f := begin rw definable_fun, split, { intros h L Ο† hΟ†, exact definable_psh.definable_precomp βŸ¨Ο†, hΟ†βŸ© h }, { intros H, refine H id _, exact (Def.id K).is_definable } end lemma definable_prod_mk {X Y : Type*} [definable_psh S X] [definable_psh S Y] : definable S (prod.mk : X β†’ Y β†’ X Γ— Y) := -- I have no idea how to come up with these proofs. -- Maybe we should tweak the type of definable_precomp (or write a lemma) -- that takes the underlying map Ο† and its proof of definability separately -- so that Lean has a better chance of guessing what's happening? Ξ» L g h L' g' h', ⟨definable_psh.definable_precomp ⟨λ x, (g' x).fst, h'.1⟩ h.2, h'.2⟩ lemma definable_fst {X Y : Type*} [definable_psh S X] [definable_psh S Y] : definable S (prod.fst : X Γ— Y β†’ X) := begin rw definable_fun, intros K Ο† hΟ†, exact hΟ†.1 end lemma definable_snd {X Y : Type*} [definable_psh S X] [definable_psh S Y] : definable S (prod.snd : X Γ— Y β†’ Y) := begin rw definable_fun, intros K Ο† hΟ†, exact hΟ†.2 end lemma definable.prod_mk {W X Y : Type*} [definable_psh S W] [definable_psh S X] [definable_psh S Y] {f : W β†’ X} (hf : definable S f) {g : W β†’ Y} (hg : definable S g) : definable S (Ξ» w, (f w, g w)) := begin rw definable_fun at ⊒ hf hg, intros K Ο† hΟ†, exact ⟨hf Ο† hΟ†, hg Ο† hΟ†βŸ© end lemma definable_funβ‚‚ {X Y Z : Type*} [definable_psh S X] [definable_psh S Y] [definable_psh S Z] {f : X β†’ Y β†’ Z} : (βˆ€ {L : Def S} (Ο† : L β†’ X), definable S Ο† β†’ definable S (f ∘ Ο†)) ↔ (βˆ€ {L : Def S} (Ο† : L β†’ X Γ— Y), definable S Ο† β†’ definable S (function.uncurry f ∘ Ο†)) := begin split; intro H, { intros L Ο† hΟ†, rw definable_fun at ⊒, intros K ψ hψ, have : definable S (prod.fst ∘ Ο†), { rw ←definable_yoneda at ⊒ hΟ†, exact hΟ†.1 }, specialize H (Ξ» l, (Ο† l).1) this, rw definable_fun at H, specialize H ψ hψ, specialize H K, swap, { exact Ξ» k, (k, (Ο† (ψ k)).2) }, refine H ⟨(Def.id K).is_definable, _⟩, clear H, rw ←definable_yoneda at hΟ†, exact definable_psh.definable_precomp ⟨ψ, hψ⟩ hΟ†.2 }, { intros L Ο† hΟ†, rw definable_fun, intros K ψ hψ, intros K' ψ' hψ', dsimp [function.uncurry, function.comp], specialize H (Ξ» k', (Ο† (ψ (ψ' k').1), (ψ' k').2)), have : definable S (Ξ» k', (Ο† (ψ (ψ' k').1), (ψ' k').2)), { rw ←definable_yoneda at ⊒ hΟ†, split, { refine definable_psh.definable_precomp ⟨λ k', ψ (ψ' k').fst, _⟩ hΟ†, exact (Hom.comp ⟨ψ, hψ⟩ ⟨_, hψ'.1⟩).is_definable }, { exact hψ'.2 } }, specialize H this, rw definable_fun at H, exact H _ (Def.id _).is_definable } end lemma definable_comp {X Y Z : Type*} [definable_psh S X] [definable_psh S Y] [definable_psh S Z] : definable S (function.comp : (Y β†’ Z) β†’ (X β†’ Y) β†’ (X β†’ Z)) := begin -- TODO: Make these 4 lines a lemma. rw definable_fun, intros L₁ φ₁ hφ₁, rw definable_yoneda at ⊒ hφ₁, revert L₁, -- end lemma rw definable_funβ‚‚, rw definable_funβ‚‚, rintros L Ο† hΟ†, rw ←definable_yoneda at hΟ†, obtain ⟨⟨hφ₁, hΟ†β‚‚βŸ©, hΟ†β‚ƒβŸ© := hΟ†, rw definable_yoneda at hφ₁ hΟ†β‚‚ hφ₃, dsimp [function.uncurry, function.comp], exact hφ₁.app_ctx (hΟ†β‚‚.app_ctx hφ₃) end lemma definable.comp {X Y Z : Type*} [definable_psh S X] [definable_psh S Y] [definable_psh S Z] {g : Y β†’ Z} (hg : definable S g) {f : X β†’ Y} (hf : definable S f) : definable S (g ∘ f) := definable_app (definable_app definable_comp hg) hf lemma definable.comp_ctx {Ξ“ X Y Z : Type*} [definable_psh S Ξ“] [definable_psh S X] [definable_psh S Y] [definable_psh S Z] {g : Ξ“ β†’ Y β†’ Z} (hg : definable S g) {f : Ξ“ β†’ X β†’ Y} (hf : definable S f) : definable S (Ξ» Ξ³, g Ξ³ ∘ f Ξ³) := definable.app_ctx (definable.comp definable_comp hg) hf lemma definable_curry {X Y Z : Type*} [definable_psh S X] [definable_psh S Y] [definable_psh S Z] : definable S (function.curry : (X Γ— Y β†’ Z) β†’ X β†’ Y β†’ Z) := begin -- TODO: Make these 4 lines a lemma. rw definable_fun, intros L₁ φ₁ hφ₁, rw definable_yoneda at ⊒ hφ₁, revert L₁, -- end lemma rw definable_funβ‚‚, rw definable_funβ‚‚, rintros L Ο† hΟ†, rw ←definable_yoneda at hΟ†, obtain ⟨⟨hφ₁, hΟ†β‚‚βŸ©, hΟ†β‚ƒβŸ© := hΟ†, rw definable_yoneda at hφ₁ hΟ†β‚‚ hφ₃, exact definable.app_ctx hφ₁ (hΟ†β‚‚.prod_mk hφ₃) end instance Prop.definable_psh : definable_psh S Prop := { definable := Ξ» K s, S.def_coords s, definable_precomp := Ξ» L K Ο† f hf, sorry } -- preimage instance set.definable_psh {X : Type*} [definable_psh S X] : definable_psh S (set X) := show definable_psh S (X β†’ Prop), by apply_instance lemma definable_and : definable S (∧) := begin suffices : definable S (Ξ» r : Prop Γ— Prop, r.1 ∧ r.2), { exact definable_app definable_curry this }, rw definable_fun, rintros K Ο† ⟨hφ₁, hΟ†β‚‚βŸ©, exact hφ₁.inter hΟ†β‚‚ end lemma definable.and {W : Type*} [definable_psh S W] {f : W β†’ Prop} (hf : definable S f) {g : W β†’ Prop} (hg : definable S g) : definable S (Ξ» w, f w ∧ g w) := definable.app_ctx (definable.comp definable_and hf) hg lemma definable_inter {X : Type*} [definable_psh S X] : definable S ((∩) : set X β†’ set X β†’ set X) := begin suffices : definable S (Ξ» (r : set X Γ— set X) (x : X), r.1 x ∧ r.2 x), { exact (definable_app definable_curry this : _) }, -- TODO: Make these 4 lines a lemma. rw definable_fun, intros L₁ φ₁ hφ₁, rw definable_yoneda at ⊒ hφ₁, revert L₁, -- end lemma rw definable_funβ‚‚, intros L Ο† hΟ†, rw ←definable_yoneda at hΟ†, obtain ⟨⟨hφ₁, hΟ†β‚‚βŸ©, hΟ†β‚ƒβŸ© := hΟ†, rw definable_yoneda at hφ₁ hΟ†β‚‚ hφ₃, apply definable.and, { exact hφ₁.app_ctx hφ₃ }, { exact hΟ†β‚‚.app_ctx hφ₃ } end /- instance foo {X Y : Type*} [definable_psh S X] [definable_psh S Y] {p : X β†’ Prop} : definable_psh S (Ξ  (x : X) (h : p x), Y) := sorry -/ lemma definable_definable {X : Type*} [definable_psh S X] : definable S (definable S : X β†’ Prop) := begin rw definable_fun, intros K Ο† hΟ†, change S.def_coords _, convert K.is_def_coords, apply set.eq_univ_of_forall, intro x, change definable S (Ο† x), apply definable_app, { rw ←definable_yoneda, exact hΟ† }, -- now we need to know that every point of a representable guy -- is definable. this needs definable constants! sorry -- In general, the definable elements of a structure -- might or might not form a definable set. -- Counterexample: take (ℝ, +, *) without constants; -- definable elements are the algebraic real numbers, -- but only tame sets can be definable. -- However, once the structure has definable constants, -- then everything is definable and of course the set `univ` is definable. end -- Important note: it is definitely *not* true that -- `definable S` = `set.univ` on *every* X with a definable_psh structure; -- just represented guys. /- similarly, in an o-minimal structure: lemma definable_finite [DUNLO R] [o_minimal S] : definable S (set.finite : set R β†’ Prop) := sorry because "finite" is equivalent to "does not contain an interval" on the tame = definable sets, which are the only ones that matter. -/ instance self : definable_psh S R := sorry variables (S) #exit class definable_fam {X : Type*} [definable_psh S X] (Y : X β†’ Sort*) := (definable : Ξ  {K : Def S} (x : K β†’ X) (hx : definable_psh.definable x), (Ξ  k, Y (x k)) β†’ Prop) -- s.t. blah blah blah... instance moo {X : Type*} {Y : X β†’ Type*} [definable_psh S X] [definable_fam S Y] : definable_psh S (Ξ  (x : X), Y x) := sorry constant choice : Ξ  (X : set R), X.nonempty β†’ X example : definable S (set.nonempty : set R β†’ Prop) := sorry instance : definable_fam S (set.nonempty : set R β†’ Prop) := sorry instance pi {X : Type*} [definable_psh S X] {Y Z : X β†’ Sort*} [definable_fam S Y] [definable_fam S Z] : definable_fam S (Ξ» x, Y x β†’ Z x) := sorry instance subtype {X : Type*} [definable_psh S X] : definable_fam S (Ξ» (s : set X), s) := sorry example : definable S ((Ξ» x hx₁ hxβ‚‚, choice x hx₁) : Ξ  (X : set R), X.nonempty β†’ X.nonempty β†’ X) := sorry -- can we do without this `definable_fam` stuff? even as a hack? -- or maybe stick with this for now? /- TODO: * class represented [has_coordinates R X] expressing compatibility * prove this notion of definability of functions, sets reduces to the original one in the represented case Then: * prove stuff like `is_least : set R β†’ R β†’ Prop` is definable -/ end o_minimal
72219305e0b51866a81f80b94c9d91fd4f2c7dfa
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/algebra/category/Module/basic.lean
114af8126410241aaba9e741dbae2eef1b8e98be
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
5,847
lean
/- Copyright (c) 2019 Robert A. Spencer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert A. Spencer, Markus Himmel -/ import algebra.module import algebra.punit_instances import algebra.category.Group.basic import category_theory.concrete_category import category_theory.limits.shapes.zero import category_theory.limits.shapes.kernels import linear_algebra.basic open category_theory open category_theory.limits open category_theory.limits.walking_parallel_pair universe u variables (R : Type u) [ring R] /-- The category of R-modules and their morphisms. -/ structure Module := (carrier : Type u) [is_add_comm_group : add_comm_group carrier] [is_module : module R carrier] attribute [instance] Module.is_add_comm_group Module.is_module namespace Module -- TODO revisit this after #1438 merges, to check coercions and instances are handled consistently instance : has_coe_to_sort (Module R) := { S := Type u, coe := Module.carrier } instance : category (Module.{u} R) := { hom := Ξ» M N, M β†’β‚—[R] N, id := Ξ» M, 1, comp := Ξ» A B C f g, g.comp f } instance : concrete_category (Module.{u} R) := { forget := { obj := Ξ» R, R, map := Ξ» R S f, (f : R β†’ S) }, forget_faithful := { } } instance has_forget_to_AddCommGroup : has_forgetβ‚‚ (Module R) AddCommGroup := { forgetβ‚‚ := { obj := Ξ» M, AddCommGroup.of M, map := Ξ» M₁ Mβ‚‚ f, linear_map.to_add_monoid_hom f } } /-- The object in the category of R-modules associated to an R-module -/ def of (X : Type u) [add_comm_group X] [module R X] : Module R := ⟨X⟩ instance : inhabited (Module R) := ⟨of R punit⟩ @[simp] lemma of_apply (X : Type u) [add_comm_group X] [module R X] : (of R X : Type u) = X := rfl variables {R} /-- Forgetting to the underlying type and then building the bundled object returns the original module. -/ @[simps] def of_self_iso (M : Module R) : Module.of R M β‰… M := { hom := πŸ™ M, inv := πŸ™ M } instance : subsingleton (of R punit) := by { rw of_apply R punit, apply_instance } instance : has_zero_object.{u} (Module R) := { zero := of R punit, unique_to := Ξ» X, { default := (0 : punit β†’β‚—[R] X), uniq := Ξ» _, linear_map.ext $ Ξ» x, have h : x = 0, from subsingleton.elim _ _, by simp only [h, linear_map.map_zero]}, unique_from := Ξ» X, { default := (0 : X β†’β‚—[R] punit), uniq := Ξ» _, linear_map.ext $ Ξ» x, subsingleton.elim _ _ } } variables {R} {M N U : Module R} @[simp] lemma id_apply (m : M) : (πŸ™ M : M β†’ M) m = m := rfl @[simp] lemma coe_comp (f : M ⟢ N) (g : N ⟢ U) : ((f ≫ g) : M β†’ U) = g ∘ f := rfl instance hom_is_module_hom (f : M ⟢ N) : is_linear_map R (f : M β†’ N) := linear_map.is_linear _ end Module variables {R} variables {X₁ Xβ‚‚ : Type u} /-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. -/ @[simps] def linear_equiv.to_Module_iso {g₁ : add_comm_group X₁} {gβ‚‚ : add_comm_group Xβ‚‚} {m₁ : module R X₁} {mβ‚‚ : module R Xβ‚‚} (e : X₁ ≃ₗ[R] Xβ‚‚) : Module.of R X₁ β‰… Module.of R Xβ‚‚ := { hom := (e : X₁ β†’β‚—[R] Xβ‚‚), inv := (e.symm : Xβ‚‚ β†’β‚—[R] X₁), hom_inv_id' := begin ext, exact e.left_inv x, end, inv_hom_id' := begin ext, exact e.right_inv x, end, } namespace category_theory.iso /-- Build a `linear_equiv` from an isomorphism in the category `Module R`. -/ @[simps] def to_linear_equiv {X Y : Module.{u} R} (i : X β‰… Y) : X ≃ₗ[R] Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := by tidy, right_inv := by tidy, add := by tidy, smul := by tidy, }. end category_theory.iso /-- linear equivalences between `module`s are the same as (isomorphic to) isomorphisms in `Module` -/ @[simps] def linear_equiv_iso_Group_iso {X Y : Type u} [add_comm_group X] [add_comm_group Y] [module R X] [module R Y] : (X ≃ₗ[R] Y) β‰… (Module.of R X β‰… Module.of R Y) := { hom := Ξ» e, e.to_Module_iso, inv := Ξ» i, i.to_linear_equiv, } namespace Module section zero_morphisms instance : has_zero_morphisms.{u} (Module R) := { has_zero := Ξ» M N, ⟨0⟩, comp_zero' := Ξ» M N f Z, by ext; erw linear_map.zero_apply, zero_comp' := Ξ» M N Z f, by ext; erw [linear_map.comp_apply, linear_map.zero_apply, linear_map.zero_apply, linear_map.map_zero] } end zero_morphisms section kernel variables {R} {M N : Module R} (f : M ⟢ N) /-- The cone on the equalizer diagram of f and 0 induced by the kernel of f -/ def kernel_cone : cone (parallel_pair f 0) := { X := of R f.ker, Ο€ := { app := Ξ» j, match j with | zero := f.ker.subtype | one := 0 end, naturality' := Ξ» j j' g, by { cases j; cases j'; cases g; tidy } } } /-- The kernel of a linear map is a kernel in the categorical sense -/ def kernel_is_limit : is_limit (kernel_cone f) := { lift := Ξ» s, linear_map.cod_restrict f.ker (fork.ΞΉ s) (Ξ» c, linear_map.mem_ker.2 $ by { erw [←@function.comp_apply _ _ _ f (fork.ΞΉ s) c, ←coe_comp, fork.condition, has_zero_morphisms.comp_zero (fork.ΞΉ s) N], refl }), fac' := Ξ» s j, linear_map.ext $ Ξ» x, begin rw [coe_comp, function.comp_app, ←linear_map.comp_apply], cases j, { erw @linear_map.subtype_comp_cod_restrict _ _ _ _ _ _ _ _ (fork.ΞΉ s) f.ker _ }, { rw [←fork.app_zero_left, ←fork.app_zero_left], refl } end, uniq' := Ξ» s m h, linear_map.ext $ Ξ» x, subtype.ext.2 $ have h₁ : (m ≫ (kernel_cone f).Ο€.app zero).to_fun = (s.Ο€.app zero).to_fun, by { congr, exact h zero }, by convert @congr_fun _ _ _ _ h₁ x } end kernel instance : has_kernels.{u} (Module R) := ⟨λ _ _ f, ⟨kernel_cone f, kernel_is_limit f⟩⟩ end Module instance (M : Type u) [add_comm_group M] [module R M] : has_coe (submodule R M) (Module R) := ⟨ Ξ» N, Module.of R N ⟩
87a41d3724ff4bb8d8f5345f33acbf04c3d70d13
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/src/Std/Data/PersistentArray.lean
c9c2701e519e1a67f7b57797a0eead9bea08465b
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
13,822
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 -/ universe u v w namespace Std inductive PersistentArrayNode (Ξ± : Type u) where | node (cs : Array (PersistentArrayNode Ξ±)) : PersistentArrayNode Ξ± | leaf (vs : Array Ξ±) : PersistentArrayNode Ξ± deriving Inhabited namespace PersistentArrayNode def isNode {Ξ±} : PersistentArrayNode Ξ± β†’ Bool | node _ => true | leaf _ => false end PersistentArrayNode abbrev PersistentArray.initShift : USize := 5 abbrev PersistentArray.branching : USize := USize.ofNat (2 ^ PersistentArray.initShift.toNat) structure PersistentArray (Ξ± : Type u) where /- Recall that we run out of memory if we have more than `usizeSz/8` elements. So, we can stop adding elements at `root` after `size > usizeSz`, and keep growing the `tail`. This modification allow us to use `USize` instead of `Nat` when traversing `root`. -/ root : PersistentArrayNode Ξ± := PersistentArrayNode.node (Array.mkEmpty PersistentArray.branching.toNat) tail : Array Ξ± := Array.mkEmpty PersistentArray.branching.toNat size : Nat := 0 shift : USize := PersistentArray.initShift tailOff : Nat := 0 deriving Inhabited abbrev PArray (Ξ± : Type u) := PersistentArray Ξ± namespace PersistentArray /- TODO: use proofs for showing that array accesses are not out of bounds. We can do it after we reimplement the tactic framework. -/ variable {Ξ± : Type u} open Std.PersistentArrayNode def empty : PersistentArray Ξ± := {} def isEmpty (a : PersistentArray Ξ±) : Bool := a.size == 0 def mkEmptyArray : Array Ξ± := Array.mkEmpty branching.toNat abbrev mul2Shift (i : USize) (shift : USize) : USize := i.shiftLeft shift abbrev div2Shift (i : USize) (shift : USize) : USize := i.shiftRight shift abbrev mod2Shift (i : USize) (shift : USize) : USize := USize.land i ((USize.shiftLeft 1 shift) - 1) partial def getAux [Inhabited Ξ±] : PersistentArrayNode Ξ± β†’ USize β†’ USize β†’ Ξ± | node cs, i, shift => getAux (cs.get! (div2Shift i shift).toNat) (mod2Shift i shift) (shift - initShift) | leaf cs, i, _ => cs.get! i.toNat def get! [Inhabited Ξ±] (t : PersistentArray Ξ±) (i : Nat) : Ξ± := if i >= t.tailOff then t.tail.get! (i - t.tailOff) else getAux t.root (USize.ofNat i) t.shift def getOp [Inhabited Ξ±] (self : PersistentArray Ξ±) (idx : Nat) : Ξ± := self.get! idx partial def setAux : PersistentArrayNode Ξ± β†’ USize β†’ USize β†’ Ξ± β†’ PersistentArrayNode Ξ± | node cs, i, shift, a => let j := div2Shift i shift let i := mod2Shift i shift let shift := shift - initShift node $ cs.modify j.toNat $ fun c => setAux c i shift a | leaf cs, i, _, a => leaf (cs.set! i.toNat a) def set (t : PersistentArray Ξ±) (i : Nat) (a : Ξ±) : PersistentArray Ξ± := if i >= t.tailOff then { t with tail := t.tail.set! (i - t.tailOff) a } else { t with root := setAux t.root (USize.ofNat i) t.shift a } @[specialize] partial def modifyAux [Inhabited Ξ±] (f : Ξ± β†’ Ξ±) : PersistentArrayNode Ξ± β†’ USize β†’ USize β†’ PersistentArrayNode Ξ± | node cs, i, shift => let j := div2Shift i shift let i := mod2Shift i shift let shift := shift - initShift node $ cs.modify j.toNat $ fun c => modifyAux f c i shift | leaf cs, i, _ => leaf (cs.modify i.toNat f) @[specialize] def modify [Inhabited Ξ±] (t : PersistentArray Ξ±) (i : Nat) (f : Ξ± β†’ Ξ±) : PersistentArray Ξ± := if i >= t.tailOff then { t with tail := t.tail.modify (i - t.tailOff) f } else { t with root := modifyAux f t.root (USize.ofNat i) t.shift } partial def mkNewPath (shift : USize) (a : Array Ξ±) : PersistentArrayNode Ξ± := if shift == 0 then leaf a else node (mkEmptyArray.push (mkNewPath (shift - initShift) a)) partial def insertNewLeaf : PersistentArrayNode Ξ± β†’ USize β†’ USize β†’ Array Ξ± β†’ PersistentArrayNode Ξ± | node cs, i, shift, a => if i < branching then node (cs.push (leaf a)) else let j := div2Shift i shift let i := mod2Shift i shift let shift := shift - initShift if j.toNat < cs.size then node $ cs.modify j.toNat fun c => insertNewLeaf c i shift a else node $ cs.push $ mkNewPath shift a | n, _, _, _ => n -- unreachable def mkNewTail (t : PersistentArray Ξ±) : PersistentArray Ξ± := if t.size <= (mul2Shift 1 (t.shift + initShift)).toNat then { t with tail := mkEmptyArray, root := insertNewLeaf t.root (USize.ofNat (t.size - 1)) t.shift t.tail, tailOff := t.size } else { t with tail := #[], root := let n := mkEmptyArray.push t.root; node (n.push (mkNewPath t.shift t.tail)), shift := t.shift + initShift, tailOff := t.size } def tooBig : Nat := USize.size / 8 def push (t : PersistentArray Ξ±) (a : Ξ±) : PersistentArray Ξ± := let r := { t with tail := t.tail.push a, size := t.size + 1 } if r.tail.size < branching.toNat || t.size >= tooBig then r else mkNewTail r private def emptyArray {Ξ± : Type u} : Array (PersistentArrayNode Ξ±) := Array.mkEmpty PersistentArray.branching.toNat partial def popLeaf : PersistentArrayNode Ξ± β†’ Option (Array Ξ±) Γ— Array (PersistentArrayNode Ξ±) | n@(node cs) => if h : cs.size β‰  0 then let idx : Fin cs.size := ⟨cs.size - 1, by exact Nat.pred_lt h⟩ let last := cs.get idx let cs' := cs.set idx arbitrary match popLeaf last with | (none, _) => (none, emptyArray) | (some l, newLast) => if newLast.size == 0 then let cs' := cs'.pop if cs'.isEmpty then (some l, emptyArray) else (some l, cs') else (some l, cs'.set (Array.size_set cs idx _ β–Έ idx) (node newLast)) else (none, emptyArray) | leaf vs => (some vs, emptyArray) def pop (t : PersistentArray Ξ±) : PersistentArray Ξ± := if t.tail.size > 0 then { t with tail := t.tail.pop, size := t.size - 1 } else match popLeaf t.root with | (none, _) => t | (some last, newRoots) => let last := last.pop let newSize := t.size - 1 let newTailOff := newSize - last.size if newRoots.size == 1 && (newRoots.get! 0).isNode then { root := newRoots.get! 0, shift := t.shift - initShift, size := newSize, tail := last, tailOff := newTailOff } else { t with root := node newRoots, size := newSize, tail := last, tailOff := newTailOff } section variable {m : Type v β†’ Type w} [Monad m] variable {Ξ² : Type v} @[specialize] private partial def foldlMAux (f : Ξ² β†’ Ξ± β†’ m Ξ²) : PersistentArrayNode Ξ± β†’ Ξ² β†’ m Ξ² | node cs, b => cs.foldlM (fun b c => foldlMAux f c b) b | leaf vs, b => vs.foldlM f b @[specialize] private partial def foldlFromMAux (f : Ξ² β†’ Ξ± β†’ m Ξ²) : PersistentArrayNode Ξ± β†’ USize β†’ USize β†’ Ξ² β†’ m Ξ² | node cs, i, shift, b => do let j := (div2Shift i shift).toNat let b ← foldlFromMAux f (cs.get! j) (mod2Shift i shift) (shift - initShift) b cs.foldlM (init := b) (start := j+1) fun b c => foldlMAux f c b | leaf vs, i, _, b => vs.foldlM (init := b) (start := i.toNat) f @[specialize] def foldlM (t : PersistentArray Ξ±) (f : Ξ² β†’ Ξ± β†’ m Ξ²) (init : Ξ²) (start : Nat := 0) : m Ξ² := do if start == 0 then let b ← foldlMAux f t.root init t.tail.foldlM f b else if start >= t.tailOff then t.tail.foldlM (init := init) (start := start - t.tailOff) f else do let b ← foldlFromMAux f t.root (USize.ofNat start) t.shift init; t.tail.foldlM f b @[specialize] partial def forInAux {Ξ± : Type u} {Ξ² : Type v} {m : Type v β†’ Type w} [Monad m] [inh : Inhabited Ξ²] (f : Ξ± β†’ Ξ² β†’ m (ForInStep Ξ²)) (n : PersistentArrayNode Ξ±) (b : Ξ²) : m (ForInStep Ξ²) := do let mut b := b match n with | leaf vs => for v in vs do match (← f v b) with | r@(ForInStep.done _) => return r | ForInStep.yield bNew => b := bNew return ForInStep.yield b | node cs => for c in cs do match (← forInAux f c b) with | r@(ForInStep.done _) => return r | ForInStep.yield bNew => b := bNew return ForInStep.yield b @[specialize] protected def forIn (t : PersistentArray Ξ±) (init : Ξ²) (f : Ξ± β†’ Ξ² β†’ m (ForInStep Ξ²)) : m Ξ² := do match (← forInAux (inh := ⟨init⟩) f t.root init) with | ForInStep.done b => pure b | ForInStep.yield b => let mut b := b for v in t.tail do match (← f v b) with | ForInStep.done r => return r | ForInStep.yield bNew => b := bNew return b instance : ForIn m (PersistentArray Ξ±) Ξ± where forIn := PersistentArray.forIn @[specialize] partial def findSomeMAux (f : Ξ± β†’ m (Option Ξ²)) : PersistentArrayNode Ξ± β†’ m (Option Ξ²) | node cs => cs.findSomeM? (fun c => findSomeMAux f c) | leaf vs => vs.findSomeM? f @[specialize] def findSomeM? (t : PersistentArray Ξ±) (f : Ξ± β†’ m (Option Ξ²)) : m (Option Ξ²) := do match (← findSomeMAux f t.root) with | none => t.tail.findSomeM? f | some b => pure (some b) @[specialize] partial def findSomeRevMAux (f : Ξ± β†’ m (Option Ξ²)) : PersistentArrayNode Ξ± β†’ m (Option Ξ²) | node cs => cs.findSomeRevM? (fun c => findSomeRevMAux f c) | leaf vs => vs.findSomeRevM? f @[specialize] def findSomeRevM? (t : PersistentArray Ξ±) (f : Ξ± β†’ m (Option Ξ²)) : m (Option Ξ²) := do match (← t.tail.findSomeRevM? f) with | none => findSomeRevMAux f t.root | some b => pure (some b) @[specialize] partial def forMAux (f : Ξ± β†’ m PUnit) : PersistentArrayNode Ξ± β†’ m PUnit | node cs => cs.forM (fun c => forMAux f c) | leaf vs => vs.forM f @[specialize] def forM (t : PersistentArray Ξ±) (f : Ξ± β†’ m PUnit) : m PUnit := forMAux f t.root *> t.tail.forM f end @[inline] def foldl {Ξ²} (t : PersistentArray Ξ±) (f : Ξ² β†’ Ξ± β†’ Ξ²) (init : Ξ²) (start : Nat := 0) : Ξ² := Id.run $ t.foldlM f init start @[inline] def filter (as : PersistentArray Ξ±) (p : Ξ± β†’ Bool) : PersistentArray Ξ± := as.foldl (init := {}) fun asNew a => if p a then asNew.push a else asNew def toArray (t : PersistentArray Ξ±) : Array Ξ± := t.foldl Array.push #[] def append (t₁ tβ‚‚ : PersistentArray Ξ±) : PersistentArray Ξ± := if t₁.isEmpty then tβ‚‚ else tβ‚‚.foldl PersistentArray.push t₁ instance : Append (PersistentArray Ξ±) := ⟨append⟩ @[inline] def findSome? {Ξ²} (t : PersistentArray Ξ±) (f : Ξ± β†’ (Option Ξ²)) : Option Ξ² := Id.run $ t.findSomeM? f @[inline] def findSomeRev? {Ξ²} (t : PersistentArray Ξ±) (f : Ξ± β†’ (Option Ξ²)) : Option Ξ² := Id.run $ t.findSomeRevM? f def toList (t : PersistentArray Ξ±) : List Ξ± := (t.foldl (init := []) fun xs x => x :: xs).reverse section variable {m : Type β†’ Type w} [Monad m] @[specialize] partial def anyMAux (p : Ξ± β†’ m Bool) : PersistentArrayNode Ξ± β†’ m Bool | node cs => cs.anyM fun c => anyMAux p c | leaf vs => vs.anyM p @[specialize] def anyM (t : PersistentArray Ξ±) (p : Ξ± β†’ m Bool) : m Bool := anyMAux p t.root <||> t.tail.anyM p @[inline] def allM (a : PersistentArray Ξ±) (p : Ξ± β†’ m Bool) : m Bool := do let b ← anyM a (fun v => do let b ← p v; pure (not b)) pure (not b) end @[inline] def any (a : PersistentArray Ξ±) (p : Ξ± β†’ Bool) : Bool := Id.run $ anyM a p @[inline] def all (a : PersistentArray Ξ±) (p : Ξ± β†’ Bool) : Bool := !any a fun v => !p v section variable {m : Type u β†’ Type v} [Monad m] variable {Ξ² : Type u} @[specialize] partial def mapMAux (f : Ξ± β†’ m Ξ²) : PersistentArrayNode Ξ± β†’ m (PersistentArrayNode Ξ²) | node cs => node <$> cs.mapM (fun c => mapMAux f c) | leaf vs => leaf <$> vs.mapM f @[specialize] def mapM (f : Ξ± β†’ m Ξ²) (t : PersistentArray Ξ±) : m (PersistentArray Ξ²) := do let root ← mapMAux f t.root let tail ← t.tail.mapM f pure { t with tail := tail, root := root } end @[inline] def map {Ξ²} (f : Ξ± β†’ Ξ²) (t : PersistentArray Ξ±) : PersistentArray Ξ² := Id.run $ t.mapM f structure Stats where numNodes : Nat depth : Nat tailSize : Nat partial def collectStats : PersistentArrayNode Ξ± β†’ Stats β†’ Nat β†’ Stats | node cs, s, d => cs.foldl (fun s c => collectStats c s (d+1)) { s with numNodes := s.numNodes + 1, depth := Nat.max d s.depth } | leaf vs, s, d => { s with numNodes := s.numNodes + 1, depth := Nat.max d s.depth } def stats (r : PersistentArray Ξ±) : Stats := collectStats r.root { numNodes := 0, depth := 0, tailSize := r.tail.size } 0 def Stats.toString (s : Stats) : String := s!"\{nodes := {s.numNodes}, depth := {s.depth}, tail size := {s.tailSize}}" instance : ToString Stats := ⟨Stats.toString⟩ end PersistentArray def mkPersistentArray {Ξ± : Type u} (n : Nat) (v : Ξ±) : PArray Ξ± := n.fold (init := PersistentArray.empty) fun i p => p.push v @[inline] def mkPArray {Ξ± : Type u} (n : Nat) (v : Ξ±) : PArray Ξ± := mkPersistentArray n v end Std open Std (PersistentArray PersistentArray.empty) def List.toPersistentArrayAux {Ξ± : Type u} : List Ξ± β†’ PersistentArray Ξ± β†’ PersistentArray Ξ± | [], t => t | x::xs, t => toPersistentArrayAux xs (t.push x) def List.toPersistentArray {Ξ± : Type u} (xs : List Ξ±) : PersistentArray Ξ± := xs.toPersistentArrayAux {} def Array.toPersistentArray {Ξ± : Type u} (xs : Array Ξ±) : PersistentArray Ξ± := xs.foldl (init := PersistentArray.empty) fun p x => p.push x @[inline] def Array.toPArray {Ξ± : Type u} (xs : Array Ξ±) : PersistentArray Ξ± := xs.toPersistentArray
bd02ef19fa1db9f34e8c4e8480f25ce13938388a
618003631150032a5676f229d13a079ac875ff77
/src/set_theory/pgame.lean
febe8a41fc3546a394fd0ed24b08168c02435118
[ "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
39,216
lean
/- Copyright (c) 2019 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Mario Carneiro, Isabel Longbottom, Scott Morrison -/ import logic.embedding import data.nat.cast /-! # Combinatorial (pre-)games. The basic theory of combinatorial games, following Conway's book `On Numbers and Games`. We construct "pregames", define an ordering and arithmetic operations on them, then show that the operations descend to "games", defined via the equivalence relation `p β‰ˆ q ↔ p ≀ q ∧ q ≀ p`. The surreal numbers will be built as a quotient of a subtype of pregames. A pregame (`pgame` below) is axiomatised via an inductive type, whose sole constructor takes two types (thought of as indexing the the possible moves for the players Left and Right), and a pair of functions out of these types to `pgame` (thought of as describing the resulting game after making a move). Combinatorial games themselves, as a quotient of pregames, are constructed in `game.lean`. ## Conway induction By construction, the induction principle for pregames is exactly "Conway induction". That is, to prove some predicate `pgame β†’ Prop` holds for all pregames, it suffices to prove that for every pregame `g`, if the predicate holds for every game resulting from making a move, then it also holds for `g`. While it is often convenient to work "by induction" on pregames, in some situations this becomes awkward, so we also define accessor functions `left_moves`, `right_moves`, `move_left` and `move_right`. There is a relation `subsequent p q`, saying that `p` can be reached by playing some non-empty sequence of moves starting from `q`, an instance `well_founded subsequent`, and a local tactic `pgame_wf_tac` which is helpful for discharging proof obligations in inductive proofs relying on this relation. ## Order properties Pregames have both a `≀` and a `<` relation, which are related in quite a subtle way. In particular, it is worth noting that in Lean's (perhaps unfortunate?) definition of a `preorder`, we have `lt_iff_le_not_le : βˆ€ a b : Ξ±, a < b ↔ (a ≀ b ∧ Β¬ b ≀ a)`, but this is _not_ satisfied by the usual `≀` and `<` relations on pregames. (It is satisfied once we restrict to the surreal numbers.) In particular, `<` is not transitive; there is an example below showing `0 < star ∧ star < 0`. We do have ``` theorem not_le {x y : pgame} : Β¬ x ≀ y ↔ y < x := ... theorem not_lt {x y : pgame} : Β¬ x < y ↔ y ≀ x := ... ``` The statement `0 ≀ x` means that Left has a good response to any move by Right; in particular, the theorem `zero_le` below states ``` 0 ≀ x ↔ βˆ€ j : x.right_moves, βˆƒ i : (x.move_right j).left_moves, 0 ≀ (x.move_right j).move_left i ``` On the other hand the statement `0 < x` means that Left has a good move right now; in particular the theorem `zero_lt` below states ``` 0 < x ↔ βˆƒ i : left_moves x, βˆ€ j : right_moves (x.move_left i), 0 < (x.move_left i).move_right j ``` The theorems `le_def`, `lt_def`, give a recursive characterisation of each relation, in terms of themselves two moves later. The theorems `le_def_lt` and `lt_def_lt` give recursive characterisations of each relation in terms of the other relation one move later. We define an equivalence relation `equiv p q ↔ p ≀ q ∧ q ≀ p`. Later, games will be defined as the quotient by this relation. ## Algebraic structures We next turn to defining the operations necessary to make games into a commutative additive group. Addition is defined for $x = \{xL | xR\}$ and $y = \{yL | yR\}$ by $x + y = \{xL + y, x + yL | xR + y, x + yR\}$. Negation is defined by $\{xL | xR\} = \{-xR | -xL\}$. The order structures interact in the expected way with addition, so we have ``` theorem le_iff_sub_nonneg {x y : pgame} : x ≀ y ↔ 0 ≀ y - x := sorry theorem lt_iff_sub_pos {x y : pgame} : x < y ↔ 0 < y - x := sorry ``` We show that these operations respect the equivalence relation, and hence descend to games. At the level of games, these operations satisfy all the laws of a commutative group. To prove the necessary equivalence relations at the level of pregames, we introduce the notion of a `relabelling` of a game, and show, for example, that there is a relabelling between `x + (y + z)` and `(x + y) + z`. ## Future work * The theory of dominated and reversible positions, and unique normal form for short games. * Analysis of basic domineering positions. * Impartial games, nim, and the Sprague-Grundy theorem. * Hex. * Temperature. * The development of surreal numbers, based on this development of combinatorial games, is still quite incomplete. ## References The material here is all drawn from * [Conway, *On numbers and games*][conway2001] An interested reader may like to formalise some of the material from * [Andreas Blass, *A game semantics for linear logic*][MR1167694] * [AndrΓ© Joyal, *Remarques sur la thΓ©orie des jeux Γ  deux personnes*][joyal1997] -/ universes u /-- The type of pre-games, before we have quotiented by extensionality. In ZFC, a combinatorial game is constructed from two sets of combinatorial games that have been constructed at an earlier stage. To do this in type theory, we say that a pre-game is built inductively from two families of pre-games indexed over any type in Type u. The resulting type `pgame.{u}` lives in `Type (u+1)`, reflecting that it is a proper class in ZFC. -/ inductive pgame : Type (u+1) | mk : βˆ€ Ξ± Ξ² : Type u, (Ξ± β†’ pgame) β†’ (Ξ² β†’ pgame) β†’ pgame namespace pgame /-- Construct a pre-game from list of pre-games describing the available moves for Left and Right. -/ -- TODO provide some API describing the interaction with -- `left_moves`, `right_moves`, `move_left` and `move_right` below. -- TODO define this at the level of games, as well, and perhaps also for finsets of games. def of_lists (L R : list pgame.{0}) : pgame.{0} := pgame.mk (fin L.length) (fin R.length) (Ξ» i, L.nth_le i.val i.is_lt) (Ξ» j, R.nth_le j.val j.is_lt) /-- The indexing type for allowable moves by Left. -/ def left_moves : pgame β†’ Type u | (mk l _ _ _) := l /-- The indexing type for allowable moves by Right. -/ def right_moves : pgame β†’ Type u | (mk _ r _ _) := r /-- The new game after Left makes an allowed move. -/ def move_left : Ξ  (g : pgame), left_moves g β†’ pgame | (mk l _ L _) i := L i /-- The new game after Right makes an allowed move. -/ def move_right : Ξ  (g : pgame), right_moves g β†’ pgame | (mk _ r _ R) j := R j @[simp] lemma left_moves_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : pgame).left_moves = xl := rfl @[simp] lemma move_left_mk {xl xr xL xR i} : (⟨xl, xr, xL, xR⟩ : pgame).move_left i = xL i := rfl @[simp] lemma right_moves_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : pgame).right_moves = xr := rfl @[simp] lemma move_right_mk {xl xr xL xR j} : (⟨xl, xr, xL, xR⟩ : pgame).move_right j = xR j := rfl /-- `subsequent p q` says that `p` can be obtained by playing some nonempty sequence of moves from `q`. -/ inductive subsequent : pgame β†’ pgame β†’ Prop | left : Ξ  (x : pgame) (i : x.left_moves), subsequent (x.move_left i) x | right : Ξ  (x : pgame) (j : x.right_moves), subsequent (x.move_right j) x | trans : Ξ  (x y z : pgame), subsequent x y β†’ subsequent y z β†’ subsequent x z theorem wf_subsequent : well_founded subsequent := ⟨λ x, begin induction x with l r L R IHl IHr, refine ⟨_, Ξ» y h, _⟩, generalize_hyp e : mk l r L R = x at h, induction h with _ i _ j a b _ h1 h2 IH1 IH2; subst e, { apply IHl }, { apply IHr }, { exact acc.inv (IH2 rfl) h1 } end⟩ instance : has_well_founded pgame := { r := subsequent, wf := wf_subsequent } /-- A move by Left produces a subsequent game. (For use in pgame_wf_tac.) -/ lemma subsequent.left_move {xl xr} {xL : xl β†’ pgame} {xR : xr β†’ pgame} {i : xl} : subsequent (xL i) (mk xl xr xL xR) := subsequent.left (mk xl xr xL xR) i /-- A move by Right produces a subsequent game. (For use in pgame_wf_tac.) -/ lemma subsequent.right_move {xl xr} {xL : xl β†’ pgame} {xR : xr β†’ pgame} {j : xr} : subsequent (xR j) (mk xl xr xL xR) := subsequent.right (mk xl xr xL xR) j /-- A local tactic for proving well-foundedness of recursive definitions involving pregames. -/ meta def pgame_wf_tac := `[solve_by_elim [psigma.lex.left, psigma.lex.right, subsequent.left_move, subsequent.right_move, subsequent.left, subsequent.right, subsequent.trans] { max_depth := 6 }] /-- The pre-game `zero` is defined by `0 = { | }`. -/ instance : has_zero pgame := ⟨⟨pempty, pempty, pempty.elim, pempty.elim⟩⟩ @[simp] lemma zero_left_moves : (0 : pgame).left_moves = pempty := rfl @[simp] lemma zero_right_moves : (0 : pgame).right_moves = pempty := rfl instance : inhabited pgame := ⟨0⟩ /-- The pre-game `one` is defined by `1 = { 0 | }`. -/ instance : has_one pgame := ⟨⟨punit, pempty, Ξ» _, 0, pempty.elim⟩⟩ @[simp] lemma one_left_moves : (1 : pgame).left_moves = punit := rfl @[simp] lemma one_move_left : (1 : pgame).move_left punit.star = 0 := rfl @[simp] lemma one_right_moves : (1 : pgame).right_moves = pempty := rfl /-- Define simultaneously by mutual induction the `<=` and `<` relation on pre-games. The ZFC definition says that `x = {xL | xR}` is less or equal to `y = {yL | yR}` if `βˆ€ x₁ ∈ xL, x₁ < y` and `βˆ€ yβ‚‚ ∈ yR, x < yβ‚‚`, where `x < y` is the same as `Β¬ y <= x`. This is a tricky induction because it only decreases one side at a time, and it also swaps the arguments in the definition of `<`. The solution is to define `x < y` and `x <= y` simultaneously. -/ def le_lt : Ξ  (x y : pgame), Prop Γ— Prop | (mk xl xr xL xR) (mk yl yr yL yR) := -- the orderings of the clauses here are carefully chosen so that -- and.left/or.inl refer to moves by Left, and -- and.right/or.inr refer to moves by Right. ((βˆ€ i : xl, (le_lt (xL i) ⟨yl, yr, yL, yR⟩).2) ∧ (βˆ€ j : yr, (le_lt ⟨xl, xr, xL, xR⟩ (yR j)).2), (βˆƒ i : yl, (le_lt ⟨xl, xr, xL, xR⟩ (yL i)).1) ∨ (βˆƒ j : xr, (le_lt (xR j) ⟨yl, yr, yL, yR⟩).1)) using_well_founded { dec_tac := pgame_wf_tac } instance : has_le pgame := ⟨λ x y, (le_lt x y).1⟩ instance : has_lt pgame := ⟨λ x y, (le_lt x y).2⟩ /-- Definition of `x ≀ y` on pre-games built using the constructor. -/ @[simp] theorem mk_le_mk {xl xr xL xR yl yr yL yR} : (⟨xl, xr, xL, xR⟩ : pgame) ≀ ⟨yl, yr, yL, yR⟩ ↔ (βˆ€ i, xL i < ⟨yl, yr, yL, yR⟩) ∧ (βˆ€ j, (⟨xl, xr, xL, xR⟩ : pgame) < yR j) := iff.rfl /-- Definition of `x ≀ y` on pre-games, in terms of `<` -/ theorem le_def_lt {x y : pgame} : x ≀ y ↔ (βˆ€ i : x.left_moves, x.move_left i < y) ∧ (βˆ€ j : y.right_moves, x < y.move_right j) := by { cases x, cases y, refl } /-- Definition of `x < y` on pre-games built using the constructor. -/ @[simp] theorem mk_lt_mk {xl xr xL xR yl yr yL yR} : (⟨xl, xr, xL, xR⟩ : pgame) < ⟨yl, yr, yL, yR⟩ ↔ (βˆƒ i, (⟨xl, xr, xL, xR⟩ : pgame) ≀ yL i) ∨ (βˆƒ j, xR j ≀ ⟨yl, yr, yL, yR⟩) := iff.rfl /-- Definition of `x < y` on pre-games, in terms of `≀` -/ theorem lt_def_le {x y : pgame} : x < y ↔ (βˆƒ i : y.left_moves, x ≀ y.move_left i) ∨ (βˆƒ j : x.right_moves, x.move_right j ≀ y) := by { cases x, cases y, refl } /-- The definition of `x ≀ y` on pre-games, in terms of `≀` two moves later. -/ theorem le_def {x y : pgame} : x ≀ y ↔ (βˆ€ i : x.left_moves, (βˆƒ i' : y.left_moves, x.move_left i ≀ y.move_left i') ∨ (βˆƒ j : (x.move_left i).right_moves, (x.move_left i).move_right j ≀ y)) ∧ (βˆ€ j : y.right_moves, (βˆƒ i : (y.move_right j).left_moves, x ≀ (y.move_right j).move_left i) ∨ (βˆƒ j' : x.right_moves, x.move_right j' ≀ y.move_right j)) := begin rw [le_def_lt], conv { to_lhs, simp only [lt_def_le] }, end /-- The definition of `x < y` on pre-games, in terms of `<` two moves later. -/ theorem lt_def {x y : pgame} : x < y ↔ (βˆƒ i : y.left_moves, (βˆ€ i' : x.left_moves, x.move_left i' < y.move_left i) ∧ (βˆ€ j : (y.move_left i).right_moves, x < (y.move_left i).move_right j)) ∨ (βˆƒ j : x.right_moves, (βˆ€ i : (x.move_right j).left_moves, (x.move_right j).move_left i < y) ∧ (βˆ€ j' : y.right_moves, x.move_right j < y.move_right j')) := begin rw [lt_def_le], conv { to_lhs, simp only [le_def_lt] }, end /-- The definition of `x ≀ 0` on pre-games, in terms of `≀ 0` two moves later. -/ theorem le_zero {x : pgame} : x ≀ 0 ↔ βˆ€ i : x.left_moves, βˆƒ j : (x.move_left i).right_moves, (x.move_left i).move_right j ≀ 0 := begin rw le_def, dsimp, simp [forall_pempty, exists_pempty] end /-- The definition of `0 ≀ x` on pre-games, in terms of `0 ≀` two moves later. -/ theorem zero_le {x : pgame} : 0 ≀ x ↔ βˆ€ j : x.right_moves, βˆƒ i : (x.move_right j).left_moves, 0 ≀ (x.move_right j).move_left i := begin rw le_def, dsimp, simp [forall_pempty, exists_pempty] end /-- The definition of `x < 0` on pre-games, in terms of `< 0` two moves later. -/ theorem lt_zero {x : pgame} : x < 0 ↔ βˆƒ j : x.right_moves, βˆ€ i : (x.move_right j).left_moves, (x.move_right j).move_left i < 0 := begin rw lt_def, dsimp, simp [forall_pempty, exists_pempty] end /-- The definition of `0 < x` on pre-games, in terms of `< x` two moves later. -/ theorem zero_lt {x : pgame} : 0 < x ↔ βˆƒ i : x.left_moves, βˆ€ j : (x.move_left i).right_moves, 0 < (x.move_left i).move_right j := begin rw lt_def, dsimp, simp [forall_pempty, exists_pempty] end /-- Given a right-player-wins game, provide a response to any move by left. -/ noncomputable def right_response {x : pgame} (h : x ≀ 0) (i : x.left_moves) : (x.move_left i).right_moves := classical.some $ (le_zero.1 h) i /-- Show that the response for right provided by `right_response` preserves the right-player-wins condition. -/ lemma right_response_spec {x : pgame} (h : x ≀ 0) (i : x.left_moves) : (x.move_left i).move_right (right_response h i) ≀ 0 := classical.some_spec $ (le_zero.1 h) i /-- Given a left-player-wins game, provide a response to any move by right. -/ noncomputable def left_response {x : pgame} (h : 0 ≀ x) (j : x.right_moves) : (x.move_right j).left_moves := classical.some $ (zero_le.1 h) j /-- Show that the response for left provided by `left_response` preserves the left-player-wins condition. -/ lemma left_response_spec {x : pgame} (h : 0 ≀ x) (j : x.right_moves) : 0 ≀ (x.move_right j).move_left (left_response h j) := classical.some_spec $ (zero_le.1 h) j theorem lt_of_le_mk {xl xr xL xR y i} : (⟨xl, xr, xL, xR⟩ : pgame) ≀ y β†’ xL i < y := by cases y; exact Ξ» h, h.1 i theorem lt_of_mk_le {x : pgame} {yl yr yL yR i} : x ≀ ⟨yl, yr, yL, yR⟩ β†’ x < yR i := by cases x; exact Ξ» h, h.2 i theorem mk_lt_of_le {xl xr xL xR y i} : (by exact xR i ≀ y) β†’ (⟨xl, xr, xL, xR⟩ : pgame) < y := by cases y; exact Ξ» h, or.inr ⟨i, h⟩ theorem lt_mk_of_le {x : pgame} {yl yr yL yR i} : (by exact x ≀ yL i) β†’ x < ⟨yl, yr, yL, yR⟩ := by cases x; exact Ξ» h, or.inl ⟨i, h⟩ theorem not_le_lt {x y : pgame} : (Β¬ x ≀ y ↔ y < x) ∧ (Β¬ x < y ↔ y ≀ x) := begin induction x with xl xr xL xR IHxl IHxr generalizing y, induction y with yl yr yL yR IHyl IHyr, classical, simp only [mk_le_mk, mk_lt_mk, not_and_distrib, not_or_distrib, not_forall, not_exists, and_comm, or_comm, IHxl, IHxr, IHyl, IHyr, iff_self, and_self] end theorem not_le {x y : pgame} : Β¬ x ≀ y ↔ y < x := not_le_lt.1 theorem not_lt {x y : pgame} : Β¬ x < y ↔ y ≀ x := not_le_lt.2 @[refl] theorem le_refl : βˆ€ x : pgame, x ≀ x | ⟨l, r, L, R⟩ := ⟨λ i, lt_mk_of_le (le_refl _), Ξ» i, mk_lt_of_le (le_refl _)⟩ theorem lt_irrefl (x : pgame) : Β¬ x < x := not_lt.2 (le_refl _) theorem ne_of_lt : βˆ€ {x y : pgame}, x < y β†’ x β‰  y | x _ h rfl := lt_irrefl x h theorem le_trans_aux {xl xr} {xL : xl β†’ pgame} {xR : xr β†’ pgame} {yl yr} {yL : yl β†’ pgame} {yR : yr β†’ pgame} {zl zr} {zL : zl β†’ pgame} {zR : zr β†’ pgame} (h₁ : βˆ€ i, mk yl yr yL yR ≀ mk zl zr zL zR β†’ mk zl zr zL zR ≀ xL i β†’ mk yl yr yL yR ≀ xL i) (hβ‚‚ : βˆ€ i, zR i ≀ mk xl xr xL xR β†’ mk xl xr xL xR ≀ mk yl yr yL yR β†’ zR i ≀ mk yl yr yL yR) : mk xl xr xL xR ≀ mk yl yr yL yR β†’ mk yl yr yL yR ≀ mk zl zr zL zR β†’ mk xl xr xL xR ≀ mk zl zr zL zR := Ξ» ⟨xLy, xyR⟩ ⟨yLz, yzR⟩, ⟨ Ξ» i, not_le.1 (Ξ» h, not_lt.2 (h₁ _ ⟨yLz, yzR⟩ h) (xLy _)), Ξ» i, not_le.1 (Ξ» h, not_lt.2 (hβ‚‚ _ h ⟨xLy, xyR⟩) (yzR _))⟩ @[trans] theorem le_trans {x y z : pgame} : x ≀ y β†’ y ≀ z β†’ x ≀ z := suffices βˆ€ {x y z : pgame}, (x ≀ y β†’ y ≀ z β†’ x ≀ z) ∧ (y ≀ z β†’ z ≀ x β†’ y ≀ x) ∧ (z ≀ x β†’ x ≀ y β†’ z ≀ y), from this.1, begin clear x y z, intros, induction x with xl xr xL xR IHxl IHxr generalizing y z, induction y with yl yr yL yR IHyl IHyr generalizing z, induction z with zl zr zL zR IHzl IHzr, exact ⟨ le_trans_aux (Ξ» i, (IHxl _).2.1) (Ξ» i, (IHzr _).2.2), le_trans_aux (Ξ» i, (IHyl _).2.2) (Ξ» i, (IHxr _).1), le_trans_aux (Ξ» i, (IHzl _).1) (Ξ» i, (IHyr _).2.1)⟩, end @[trans] theorem lt_of_le_of_lt {x y z : pgame} (hxy : x ≀ y) (hyz : y < z) : x < z := begin rw ←not_le at ⊒ hyz, exact mt (Ξ» H, le_trans H hxy) hyz end @[trans] theorem lt_of_lt_of_le {x y z : pgame} (hxy : x < y) (hyz : y ≀ z) : x < z := begin rw ←not_le at ⊒ hxy, exact mt (Ξ» H, le_trans hyz H) hxy end /-- Define the equivalence relation on pre-games. Two pre-games `x`, `y` are equivalent if `x ≀ y` and `y ≀ x`. -/ def equiv (x y : pgame) : Prop := x ≀ y ∧ y ≀ x local infix ` β‰ˆ ` := pgame.equiv @[refl] theorem equiv_refl (x) : x β‰ˆ x := ⟨le_refl _, le_refl _⟩ @[symm] theorem equiv_symm {x y} : x β‰ˆ y β†’ y β‰ˆ x | ⟨xy, yx⟩ := ⟨yx, xy⟩ @[trans] theorem equiv_trans {x y z} : x β‰ˆ y β†’ y β‰ˆ z β†’ x β‰ˆ z | ⟨xy, yx⟩ ⟨yz, zy⟩ := ⟨le_trans xy yz, le_trans zy yx⟩ theorem lt_of_lt_of_equiv {x y z} (h₁ : x < y) (hβ‚‚ : y β‰ˆ z) : x < z := lt_of_lt_of_le h₁ hβ‚‚.1 theorem le_of_le_of_equiv {x y z} (h₁ : x ≀ y) (hβ‚‚ : y β‰ˆ z) : x ≀ z := le_trans h₁ hβ‚‚.1 theorem lt_of_equiv_of_lt {x y z} (h₁ : x β‰ˆ y) (hβ‚‚ : y < z) : x < z := lt_of_le_of_lt h₁.1 hβ‚‚ theorem le_of_equiv_of_le {x y z} (h₁ : x β‰ˆ y) (hβ‚‚ : y ≀ z) : x ≀ z := le_trans h₁.1 hβ‚‚ theorem le_congr {x₁ y₁ xβ‚‚ yβ‚‚} : x₁ β‰ˆ xβ‚‚ β†’ y₁ β‰ˆ yβ‚‚ β†’ (x₁ ≀ y₁ ↔ xβ‚‚ ≀ yβ‚‚) | ⟨x12, x21⟩ ⟨y12, y21⟩ := ⟨λ h, le_trans x21 (le_trans h y12), Ξ» h, le_trans x12 (le_trans h y21)⟩ theorem lt_congr {x₁ y₁ xβ‚‚ yβ‚‚} (hx : x₁ β‰ˆ xβ‚‚) (hy : y₁ β‰ˆ yβ‚‚) : x₁ < y₁ ↔ xβ‚‚ < yβ‚‚ := not_le.symm.trans $ (not_congr (le_congr hy hx)).trans not_le /-- `restricted x y` says that Left always has no more moves in `x` than in `y`, and Right always has no more moves in `y` than in `x` -/ inductive restricted : pgame.{u} β†’ pgame.{u} β†’ Type (u+1) | mk : Ξ  {x y : pgame} (L : x.left_moves β†’ y.left_moves) (R : y.right_moves β†’ x.right_moves), (βˆ€ (i : x.left_moves), restricted (x.move_left i) (y.move_left (L i))) β†’ (βˆ€ (j : y.right_moves), restricted (x.move_right (R j)) (y.move_right j)) β†’ restricted x y /-- The identity restriction. -/ @[refl] def restricted.refl : Ξ  (x : pgame), restricted x x | (mk xl xr xL xR) := restricted.mk id id (Ξ» i, restricted.refl _) (Ξ» j, restricted.refl _) using_well_founded { dec_tac := pgame_wf_tac } -- TODO trans for restricted theorem le_of_restricted : Ξ  {x y : pgame} (r : restricted x y), x ≀ y | (mk xl xr xL xR) (mk yl yr yL yR) (restricted.mk L_embedding R_embedding L_restriction R_restriction) := begin rw le_def, exact ⟨λ i, or.inl ⟨L_embedding i, le_of_restricted (L_restriction i)⟩, Ξ» i, or.inr ⟨R_embedding i, le_of_restricted (R_restriction i)⟩⟩ end /-- `relabelling x y` says that `x` and `y` are really the same game, just dressed up differently. Specifically, there is a bijection between the moves for Left in `x` and in `y`, and similarly for Right, and under these bijections we inductively have `relabelling`s for the consequent games. -/ inductive relabelling : pgame.{u} β†’ pgame.{u} β†’ Type (u+1) | mk : Ξ  {x y : pgame} (L : x.left_moves ≃ y.left_moves) (R : x.right_moves ≃ y.right_moves), (βˆ€ (i : x.left_moves), relabelling (x.move_left i) (y.move_left (L i))) β†’ (βˆ€ (j : y.right_moves), relabelling (x.move_right (R.symm j)) (y.move_right j)) β†’ relabelling x y /-- If `x` is a relabelling of `y`, then Left and Right have the same moves in either game, so `x` is a restriction of `y`. -/ def restricted_of_relabelling : Ξ  {x y : pgame} (r : relabelling x y), restricted x y | (mk xl xr xL xR) (mk yl yr yL yR) (relabelling.mk L_equiv R_equiv L_relabelling R_relabelling) := restricted.mk L_equiv.to_embedding R_equiv.symm.to_embedding (Ξ» i, restricted_of_relabelling (L_relabelling i)) (Ξ» j, restricted_of_relabelling (R_relabelling j)) -- It's not the case that `restricted x y β†’ restricted y x β†’ relabelling x y`, -- but if we insisted that the maps in a restriction were injective, then one -- could use SchrΓΆder-Bernstein for do this. /-- The identity relabelling. -/ @[refl] def relabelling.refl : Ξ  (x : pgame), relabelling x x | (mk xl xr xL xR) := relabelling.mk (equiv.refl _) (equiv.refl _) (Ξ» i, relabelling.refl _) (Ξ» j, relabelling.refl _) using_well_founded { dec_tac := pgame_wf_tac } /-- Reverse a relabelling. -/ @[symm] def relabelling.symm : Ξ  {x y : pgame}, relabelling x y β†’ relabelling y x | (mk xl xr xL xR) (mk yl yr yL yR) (relabelling.mk L_equiv R_equiv L_relabelling R_relabelling) := begin refine relabelling.mk L_equiv.symm R_equiv.symm _ _, { intro i, simpa using (L_relabelling (L_equiv.symm i)).symm }, { intro j, simpa using (R_relabelling (R_equiv j)).symm } end /-- Transitivity of relabelling -/ @[trans] def relabelling.trans : Ξ  {x y z : pgame}, relabelling x y β†’ relabelling y z β†’ relabelling x z | (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) (relabelling.mk L_equiv₁ R_equiv₁ L_relabelling₁ R_relabelling₁) (relabelling.mk L_equivβ‚‚ R_equivβ‚‚ L_relabellingβ‚‚ R_relabellingβ‚‚) := begin refine relabelling.mk (L_equiv₁.trans L_equivβ‚‚) (R_equiv₁.trans R_equivβ‚‚) _ _, { intro i, simpa using (L_relabelling₁ _).trans (L_relabellingβ‚‚ _) }, { intro j, simpa using (R_relabelling₁ _).trans (R_relabellingβ‚‚ _) }, end theorem le_of_relabelling {x y : pgame} (r : relabelling x y) : x ≀ y := le_of_restricted (restricted_of_relabelling r) /-- A relabelling lets us prove equivalence of games. -/ theorem equiv_of_relabelling {x y : pgame} (r : relabelling x y) : x β‰ˆ y := ⟨le_of_relabelling r, le_of_relabelling r.symm⟩ instance {x y : pgame} : has_coe (relabelling x y) (x β‰ˆ y) := ⟨equiv_of_relabelling⟩ /-- Replace the types indexing the next moves for Left and Right by equivalent types. -/ def relabel {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') := pgame.mk xl' xr' (Ξ» i, x.move_left (el.symm i)) (Ξ» j, x.move_right (er.symm j)) @[simp] lemma relabel_move_left' {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (i : xl') : move_left (relabel el er) i = x.move_left (el.symm i) := rfl @[simp] lemma relabel_move_left {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (i : x.left_moves) : move_left (relabel el er) (el i) = x.move_left i := by simp @[simp] lemma relabel_move_right' {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (j : xr') : move_right (relabel el er) j = x.move_right (er.symm j) := rfl @[simp] lemma relabel_move_right {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (j : x.right_moves) : move_right (relabel el er) (er j) = x.move_right j := by simp /-- The game obtained by relabelling the next moves is a relabelling of the original game. -/ def relabel_relabelling {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') : relabelling x (relabel el er) := relabelling.mk el er (Ξ» i, by simp) (Ξ» j, by simp) /-- The negation of `{L | R}` is `{-R | -L}`. -/ def neg : pgame β†’ pgame | ⟨l, r, L, R⟩ := ⟨r, l, Ξ» i, neg (R i), Ξ» i, neg (L i)⟩ instance : has_neg pgame := ⟨neg⟩ @[simp] lemma neg_def {xl xr xL xR} : -(mk xl xr xL xR) = mk xr xl (Ξ» j, -(xR j)) (Ξ» i, -(xL i)) := rfl @[simp] theorem neg_neg : Ξ  {x : pgame}, -(-x) = x | (mk xl xr xL xR) := begin dsimp [has_neg.neg, neg], congr; funext i; apply neg_neg end @[simp] theorem neg_zero : -(0 : pgame) = 0 := begin dsimp [has_zero.zero, has_neg.neg, neg], congr; funext i; cases i end /-- An explicit equivalence between the moves for Left in `-x` and the moves for Right in `x`. -/ -- This equivalence is useful to avoid having to use `cases` unnecessarily. def left_moves_neg (x : pgame) : (-x).left_moves ≃ x.right_moves := by { cases x, refl } /-- An explicit equivalence between the moves for Right in `-x` and the moves for Left in `x`. -/ def right_moves_neg (x : pgame) : (-x).right_moves ≃ x.left_moves := by { cases x, refl } @[simp] lemma move_right_left_moves_neg {x : pgame} (i : left_moves (-x)) : move_right x ((left_moves_neg x) i) = -(move_left (-x) i) := begin induction x, exact neg_neg.symm end @[simp] lemma move_left_right_moves_neg_symm {x : pgame} (i : right_moves x) : move_left (-x) ((left_moves_neg x).symm i) = -(move_right x i) := by { cases x, refl } @[simp] lemma move_left_right_moves_neg {x : pgame} (i : right_moves (-x)) : move_left x ((right_moves_neg x) i) = -(move_right (-x) i) := begin induction x, exact neg_neg.symm end @[simp] lemma move_right_right_moves_neg_symm {x : pgame} (i : left_moves x) : move_right (-x) ((right_moves_neg x).symm i) = -(move_left x i) := by { cases x, refl } theorem le_iff_neg_ge : Ξ  {x y : pgame}, x ≀ y ↔ -y ≀ -x | (mk xl xr xL xR) (mk yl yr yL yR) := begin rw [le_def], rw [le_def], dsimp [neg], split, { intro h, split, { intro i, have t := h.right i, cases t, { right, cases t, use (@right_moves_neg (yR i)).symm t_w, convert le_iff_neg_ge.1 t_h, simp }, { left, cases t, use t_w, exact le_iff_neg_ge.1 t_h, } }, { intro j, have t := h.left j, cases t, { right, cases t, use t_w, exact le_iff_neg_ge.1 t_h, }, { left, cases t, use (@left_moves_neg (xL j)).symm t_w, convert le_iff_neg_ge.1 t_h, simp, } } }, { intro h, split, { intro i, have t := h.right i, cases t, { right, cases t, use (@left_moves_neg (xL i)) t_w, convert le_iff_neg_ge.2 _, convert t_h, simp, }, { left, cases t, use t_w, exact le_iff_neg_ge.2 t_h, } }, { intro j, have t := h.left j, cases t, { right, cases t, use t_w, exact le_iff_neg_ge.2 t_h, }, { left, cases t, use (@right_moves_neg (yR j)) t_w, convert le_iff_neg_ge.2 _, convert t_h, simp } } }, end using_well_founded { dec_tac := pgame_wf_tac } theorem neg_congr {x y : pgame} (h : x β‰ˆ y) : -x β‰ˆ -y := ⟨le_iff_neg_ge.1 h.2, le_iff_neg_ge.1 h.1⟩ theorem lt_iff_neg_gt : Ξ  {x y : pgame}, x < y ↔ -y < -x := begin classical, intros, rw [←not_le, ←not_le, not_iff_not], apply le_iff_neg_ge end theorem zero_le_iff_neg_le_zero {x : pgame} : 0 ≀ x ↔ -x ≀ 0 := begin convert le_iff_neg_ge, rw neg_zero end theorem le_zero_iff_zero_le_neg {x : pgame} : x ≀ 0 ↔ 0 ≀ -x := begin convert le_iff_neg_ge, rw neg_zero end /-- The sum of `x = {xL | xR}` and `y = {yL | yR}` is `{xL + y, x + yL | xR + y, x + yR}`. -/ def add (x y : pgame) : pgame := begin induction x with xl xr xL xR IHxl IHxr generalizing y, induction y with yl yr yL yR IHyl IHyr, have y := mk yl yr yL yR, refine ⟨xl βŠ• yl, xr βŠ• yr, sum.rec _ _, sum.rec _ _⟩, { exact Ξ» i, IHxl i y }, { exact Ξ» i, IHyl i }, { exact Ξ» i, IHxr i y }, { exact Ξ» i, IHyr i } end instance : has_add pgame := ⟨add⟩ /-- `x + 0` has exactly the same moves as `x`. -/ def add_zero_relabelling : Ξ  (x : pgame.{u}), relabelling (x + 0) x | (mk xl xr xL xR) := begin refine ⟨equiv.sum_pempty xl, equiv.sum_pempty xr, _, _⟩, { rintro (⟨i⟩|⟨⟨⟩⟩), apply add_zero_relabelling, }, { rintro j, apply add_zero_relabelling, } end /-- `x + 0` is equivalent to `x`. -/ lemma add_zero_equiv (x : pgame.{u}) : x + 0 β‰ˆ x := equiv_of_relabelling (add_zero_relabelling x) /-- `0 + x` has exactly the same moves as `x`. -/ def zero_add_relabelling : Ξ  (x : pgame.{u}), relabelling (0 + x) x | (mk xl xr xL xR) := begin refine ⟨equiv.pempty_sum xl, equiv.pempty_sum xr, _, _⟩, { rintro (⟨⟨⟩⟩|⟨i⟩), apply zero_add_relabelling, }, { rintro j, apply zero_add_relabelling, } end /-- `0 + x` is equivalent to `x`. -/ lemma zero_add_equiv (x : pgame.{u}) : 0 + x β‰ˆ x := equiv_of_relabelling (zero_add_relabelling x) /-- An explicit equivalence between the moves for Left in `x + y` and the type-theory sum of the moves for Left in `x` and in `y`. -/ def left_moves_add (x y : pgame) : (x + y).left_moves ≃ x.left_moves βŠ• y.left_moves := by { cases x, cases y, refl, } /-- An explicit equivalence between the moves for Right in `x + y` and the type-theory sum of the moves for Right in `x` and in `y`. -/ def right_moves_add (x y : pgame) : (x + y).right_moves ≃ x.right_moves βŠ• y.right_moves := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_left_inl {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_left (sum.inl i) = (mk xl xr xL xR).move_left i + (mk yl yr yL yR) := rfl @[simp] lemma add_move_left_inl {x y : pgame} {i} : (x + y).move_left ((@left_moves_add x y).symm (sum.inl i)) = x.move_left i + y := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_right_inl {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_right (sum.inl i) = (mk xl xr xL xR).move_right i + (mk yl yr yL yR) := rfl @[simp] lemma add_move_right_inl {x y : pgame} {i} : (x + y).move_right ((@right_moves_add x y).symm (sum.inl i)) = x.move_right i + y := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_left_inr {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_left (sum.inr i) = (mk xl xr xL xR) + (mk yl yr yL yR).move_left i := rfl @[simp] lemma add_move_left_inr {x y : pgame} {i : y.left_moves} : (x + y).move_left ((@left_moves_add x y).symm (sum.inr i)) = x + y.move_left i := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_right_inr {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_right (sum.inr i) = (mk xl xr xL xR) + (mk yl yr yL yR).move_right i := rfl @[simp] lemma add_move_right_inr {x y : pgame} {i} : (x + y).move_right ((@right_moves_add x y).symm (sum.inr i)) = x + y.move_right i := by { cases x, cases y, refl, } instance : has_sub pgame := ⟨λ x y, x + -y⟩ /-- `-(x+y)` has exactly the same moves as `-x + -y`. -/ def neg_add_relabelling : Ξ  (x y : pgame), relabelling (-(x + y)) (-x + -y) | (mk xl xr xL xR) (mk yl yr yL yR) := ⟨equiv.refl _, equiv.refl _, Ξ» j, sum.cases_on j (Ξ» j, neg_add_relabelling (xR j) (mk yl yr yL yR)) (Ξ» j, neg_add_relabelling (mk xl xr xL xR) (yR j)), Ξ» i, sum.cases_on i (Ξ» i, neg_add_relabelling (xL i) (mk yl yr yL yR)) (Ξ» i, neg_add_relabelling (mk xl xr xL xR) (yL i))⟩ using_well_founded { dec_tac := pgame_wf_tac } theorem neg_add_le {x y : pgame} : -(x + y) ≀ -x + -y := le_of_relabelling (neg_add_relabelling x y) /-- `x+y` has exactly the same moves as `y+x`. -/ def add_comm_relabelling : Ξ  (x y : pgame.{u}), relabelling (x + y) (y + x) | (mk xl xr xL xR) (mk yl yr yL yR) := begin refine ⟨equiv.sum_comm _ _, equiv.sum_comm _ _, _, _⟩; rintros (_|_); { simp [left_moves_add, right_moves_add], apply add_comm_relabelling } end using_well_founded { dec_tac := pgame_wf_tac } theorem add_comm_le {x y : pgame} : x + y ≀ y + x := le_of_relabelling (add_comm_relabelling x y) theorem add_comm_equiv {x y : pgame} : x + y β‰ˆ y + x := equiv_of_relabelling (add_comm_relabelling x y) /-- `(x + y) + z` has exactly the same moves as `x + (y + z)`. -/ def add_assoc_relabelling : Ξ  (x y z : pgame.{u}), relabelling ((x + y) + z) (x + (y + z)) | (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) := begin refine ⟨equiv.sum_assoc _ _ _, equiv.sum_assoc _ _ _, _, _⟩, { rintro (⟨i|i⟩|i), { apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + yL i + mk zl zr zL zR) (mk xl xr xL xR + (yL i + mk zl zr zL zR)), apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + mk yl yr yL yR + zL i) (mk xl xr xL xR + (mk yl yr yL yR + zL i)), apply add_assoc_relabelling, } }, { rintro (j|⟨j|j⟩), { apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + yR j + mk zl zr zL zR) (mk xl xr xL xR + (yR j + mk zl zr zL zR)), apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + mk yl yr yL yR + zR j) (mk xl xr xL xR + (mk yl yr yL yR + zR j)), apply add_assoc_relabelling, } }, end using_well_founded { dec_tac := pgame_wf_tac } theorem add_assoc_equiv {x y z : pgame} : (x + y) + z β‰ˆ x + (y + z) := equiv_of_relabelling (add_assoc_relabelling x y z) theorem add_le_add_right : Ξ  {x y z : pgame} (h : x ≀ y), x + z ≀ y + z | (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) := begin intros h, rw le_def, split, { -- if Left plays first intros i, change xl βŠ• zl at i, cases i, { -- either they play in x rw le_def at h, cases h, have t := h_left i, rcases t with ⟨i', ih⟩ | ⟨j, jh⟩, { left, refine ⟨(left_moves_add _ _).inv_fun (sum.inl i'), _⟩, exact add_le_add_right ih, }, { right, refine ⟨(right_moves_add _ _).inv_fun (sum.inl j), _⟩, convert add_le_add_right jh, apply add_move_right_inl }, }, { -- or play in z left, refine ⟨(left_moves_add _ _).inv_fun (sum.inr i), _⟩, exact add_le_add_right h, }, }, { -- if Right plays first intros j, change yr βŠ• zr at j, cases j, { -- either they play in y rw le_def at h, cases h, have t := h_right j, rcases t with ⟨i, ih⟩ | ⟨j', jh⟩, { left, refine ⟨(left_moves_add _ _).inv_fun (sum.inl i), _⟩, convert add_le_add_right ih, apply add_move_left_inl }, { right, refine ⟨(right_moves_add _ _).inv_fun (sum.inl j'), _⟩, exact add_le_add_right jh } }, { -- or play in z right, refine ⟨(right_moves_add _ _).inv_fun (sum.inr j), _⟩, exact add_le_add_right h } } end using_well_founded { dec_tac := pgame_wf_tac } theorem add_le_add_left {x y z : pgame} (h : y ≀ z) : x + y ≀ x + z := calc x + y ≀ y + x : add_comm_le ... ≀ z + x : add_le_add_right h ... ≀ x + z : add_comm_le theorem add_congr {w x y z : pgame} (h₁ : w β‰ˆ x) (hβ‚‚ : y β‰ˆ z) : w + y β‰ˆ x + z := ⟨calc w + y ≀ w + z : add_le_add_left hβ‚‚.1 ... ≀ x + z : add_le_add_right h₁.1, calc x + z ≀ x + y : add_le_add_left hβ‚‚.2 ... ≀ w + y : add_le_add_right h₁.2⟩ theorem add_left_neg_le_zero : Ξ  {x : pgame}, (-x) + x ≀ 0 | ⟨xl, xr, xL, xR⟩ := begin rw [le_def], split, { intro i, change xr βŠ• xl at i, cases i, { -- If Left played in -x, Right responds with the same move in x. right, refine ⟨(right_moves_add _ _).inv_fun (sum.inr i), _⟩, convert @add_left_neg_le_zero (xR i), exact add_move_right_inr }, { -- If Left in x, Right responds with the same move in -x. right, dsimp, refine ⟨(right_moves_add _ _).inv_fun (sum.inl i), _⟩, convert @add_left_neg_le_zero (xL i), exact add_move_right_inl }, }, { rintro ⟨⟩, } end using_well_founded { dec_tac := pgame_wf_tac } theorem zero_le_add_left_neg : Ξ  {x : pgame}, 0 ≀ (-x) + x := begin intro x, rw [le_iff_neg_ge, neg_zero], exact le_trans neg_add_le add_left_neg_le_zero end theorem add_left_neg_equiv {x : pgame} : (-x) + x β‰ˆ 0 := ⟨add_left_neg_le_zero, zero_le_add_left_neg⟩ theorem add_right_neg_le_zero {x : pgame} : x + (-x) ≀ 0 := calc x + (-x) ≀ (-x) + x : add_comm_le ... ≀ 0 : add_left_neg_le_zero theorem zero_le_add_right_neg {x : pgame} : 0 ≀ x + (-x) := calc 0 ≀ (-x) + x : zero_le_add_left_neg ... ≀ x + (-x) : add_comm_le theorem add_lt_add_right {x y z : pgame} (h : x < y) : x + z < y + z := suffices y + z ≀ x + z β†’ y ≀ x, by { rw ←not_le at ⊒ h, exact mt this h }, assume w, calc y ≀ y + 0 : le_of_relabelling (add_zero_relabelling _).symm ... ≀ y + (z + -z) : add_le_add_left zero_le_add_right_neg ... ≀ (y + z) + (-z) : le_of_relabelling (add_assoc_relabelling _ _ _).symm ... ≀ (x + z) + (-z) : add_le_add_right w ... ≀ x + (z + -z) : le_of_relabelling (add_assoc_relabelling _ _ _) ... ≀ x + 0 : add_le_add_left add_right_neg_le_zero ... ≀ x : le_of_relabelling (add_zero_relabelling _) theorem add_lt_add_left {x y z : pgame} (h : y < z) : x + y < x + z := calc x + y ≀ y + x : add_comm_le ... < z + x : add_lt_add_right h ... ≀ x + z : add_comm_le theorem le_iff_sub_nonneg {x y : pgame} : x ≀ y ↔ 0 ≀ y - x := ⟨λ h, le_trans zero_le_add_right_neg (add_le_add_right h), Ξ» h, calc x ≀ 0 + x : le_of_relabelling (zero_add_relabelling x).symm ... ≀ (y - x) + x : add_le_add_right h ... ≀ y + (-x + x) : le_of_relabelling (add_assoc_relabelling _ _ _) ... ≀ y + 0 : add_le_add_left (add_left_neg_le_zero) ... ≀ y : le_of_relabelling (add_zero_relabelling y)⟩ theorem lt_iff_sub_pos {x y : pgame} : x < y ↔ 0 < y - x := ⟨λ h, lt_of_le_of_lt zero_le_add_right_neg (add_lt_add_right h), Ξ» h, calc x ≀ 0 + x : le_of_relabelling (zero_add_relabelling x).symm ... < (y - x) + x : add_lt_add_right h ... ≀ y + (-x + x) : le_of_relabelling (add_assoc_relabelling _ _ _) ... ≀ y + 0 : add_le_add_left (add_left_neg_le_zero) ... ≀ y : le_of_relabelling (add_zero_relabelling y)⟩ /-- The pre-game `star`, which is fuzzy/confused with zero. -/ def star : pgame := pgame.of_lists [0] [0] theorem star_lt_zero : star < 0 := or.inr ⟨⟨0, zero_lt_one⟩, (by split; rintros ⟨⟩)⟩ theorem zero_lt_star : 0 < star := or.inl ⟨⟨0, zero_lt_one⟩, (by split; rintros ⟨⟩)⟩ /-- The pre-game `Ο‰`. (In fact all ordinals have game and surreal representatives.) -/ def omega : pgame := ⟨ulift β„•, pempty, Ξ» n, ↑n.1, pempty.elim⟩ end pgame
4c0762a5bd1de92cef5eb31a2009bc5affd57d40
74caf7451c921a8d5ab9c6e2b828c9d0a35aae95
/library/init/data/list/basic.lean
06c689964edf18df67d076438d138de5ff1f8b69
[ "Apache-2.0" ]
permissive
sakas--/lean
f37b6fad4fd4206f2891b89f0f8135f57921fc3f
570d9052820be1d6442a5cc58ece37397f8a9e4c
refs/heads/master
1,586,127,145,194
1,480,960,018,000
1,480,960,635,000
40,137,176
0
0
null
1,438,621,351,000
1,438,621,351,000
null
UTF-8
Lean
false
false
3,499
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.logic init.data.nat.basic open decidable list notation h :: t := cons h t notation `[` l:(foldr `, ` (h t, cons h t) nil `]`) := l universe variables u v instance (Ξ± : Type u) : inhabited (list Ξ±) := ⟨list.nil⟩ variables {Ξ± : Type u} {Ξ² : Type v} namespace list protected def append : list Ξ± β†’ list Ξ± β†’ list Ξ± | [] l := l | (h :: s) t := h :: (append s t) instance : has_append (list Ξ±) := ⟨list.append⟩ protected def mem : Ξ± β†’ list Ξ± β†’ Prop | a [] := false | a (b :: l) := a = b ∨ mem a l instance : has_mem Ξ± list := ⟨list.mem⟩ instance decidable_mem [decidable_eq Ξ±] (a : Ξ±) : βˆ€ (l : list Ξ±), decidable (a ∈ l) | [] := is_false not_false | (b::l) := if h₁ : a = b then is_true (or.inl h₁) else match decidable_mem l with | is_true hβ‚‚ := is_true (or.inr hβ‚‚) | is_false hβ‚‚ := is_false (not_or h₁ hβ‚‚) end def concat : list Ξ± β†’ Ξ± β†’ list Ξ± | [] a := [a] | (b::l) a := b :: concat l a instance : has_emptyc (list Ξ±) := ⟨list.nil⟩ protected def insert [decidable_eq Ξ±] (a : Ξ±) (l : list Ξ±) : list Ξ± := if a ∈ l then l else concat l a instance [decidable_eq Ξ±] : has_insert Ξ± list := ⟨list.insert⟩ protected def union [decidable_eq Ξ±] : list Ξ± β†’ list Ξ± β†’ list Ξ± | l₁ [] := l₁ | l₁ (a::lβ‚‚) := union (insert a l₁) lβ‚‚ instance [decidable_eq Ξ±] : has_union (list Ξ±) := ⟨list.union⟩ protected def inter [decidable_eq Ξ±] : list Ξ± β†’ list Ξ± β†’ list Ξ± | [] lβ‚‚ := [] | (a::l₁) lβ‚‚ := if a ∈ lβ‚‚ then a :: inter l₁ lβ‚‚ else inter l₁ lβ‚‚ instance [decidable_eq Ξ±] : has_inter (list Ξ±) := ⟨list.inter⟩ def length : list Ξ± β†’ nat | [] := 0 | (a :: l) := length l + 1 open option nat def nth : list Ξ± β†’ nat β†’ option Ξ± | [] n := none | (a :: l) 0 := some a | (a :: l) (n+1) := nth l n def head [inhabited Ξ±] : list Ξ± β†’ Ξ± | [] := default Ξ± | (a :: l) := a def tail : list Ξ± β†’ list Ξ± | [] := [] | (a :: l) := l def reverse : list Ξ± β†’ list Ξ± | [] := [] | (a :: l) := concat (reverse l) a def map (f : Ξ± β†’ Ξ²) : list Ξ± β†’ list Ξ² | [] := [] | (a :: l) := f a :: map l def for : list Ξ± β†’ (Ξ± β†’ Ξ²) β†’ list Ξ² := flip map def join : list (list Ξ±) β†’ list Ξ± | [] := [] | (l :: ls) := append l (join ls) def filter (p : Ξ± β†’ Prop) [decidable_pred p] : list Ξ± β†’ list Ξ± | [] := [] | (a::l) := if p a then a :: filter l else filter l def dropn : β„• β†’ list Ξ± β†’ list Ξ± | 0 a := a | (succ n) [] := [] | (succ n) (x::r) := dropn n r definition foldl (f : Ξ± β†’ Ξ² β†’ Ξ±) : Ξ± β†’ list Ξ² β†’ Ξ± | a [] := a | a (b :: l) := foldl (f a b) l definition foldr (f : Ξ± β†’ Ξ² β†’ Ξ²) : Ξ² β†’ list Ξ± β†’ Ξ² | b [] := b | b (a :: l) := f a (foldr b l) definition any (l : list Ξ±) (p : Ξ± β†’ bool) : bool := foldr (Ξ» a r, p a || r) ff l definition all (l : list Ξ±) (p : Ξ± β†’ bool) : bool := foldr (Ξ» a r, p a && r) tt l def zip : list Ξ± β†’ list Ξ² β†’ list (prod Ξ± Ξ²) | [] _ := [] | _ [] := [] | (x::xs) (y::ys) := (prod.mk x y) :: zip xs ys def repeat (a : Ξ±) : β„• β†’ list Ξ± | 0 := [] | (succ n) := a :: repeat n def iota : β„• β†’ list β„• | 0 := [] | (succ n) := iota n ++ [succ n] end list
0b11be5a0979668fc59b0094ff4bcd85ace04009
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/linear_algebra/tensor_product.lean
ce1e87e72d4e166053290263eb1a4fe72f2bddbc
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
35,731
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro -/ import group_theory.congruence import linear_algebra.basic /-! # Tensor product of semimodules over commutative semirings. This file constructs the tensor product of semimodules over commutative semirings. Given a semiring `R` and semimodules over it `M` and `N`, the standard construction of the tensor product is `tensor_product R M N`. It is also a semimodule over `R`. It comes with a canonical bilinear map `M β†’ N β†’ tensor_product R M N`. Given any bilinear map `M β†’ N β†’ P`, there is a unique linear map `tensor_product R M N β†’ P` whose composition with the canonical bilinear map `M β†’ N β†’ tensor_product R M N` is the given bilinear map `M β†’ N β†’ P`. We start by proving basic lemmas about bilinear maps. ## Notations This file uses the localized notation `M βŠ— N` and `M βŠ—[R] N` for `tensor_product R M N`, as well as `m βŠ—β‚œ n` and `m βŠ—β‚œ[R] n` for `tensor_product.tmul R m n`. ## Tags bilinear, tensor, tensor product -/ namespace linear_map section semiring variables {R : Type*} [semiring R] {S : Type*} [semiring S] variables {M : Type*} {N : Type*} {P : Type*} variables {M' : Type*} {N' : Type*} {P' : Type*} variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] variables [add_comm_group M'] [add_comm_group N'] [add_comm_group P'] variables [semimodule R M] [semimodule S N] [semimodule R P] [semimodule S P] variables [semimodule R M'] [semimodule S N'] [semimodule R P'] [semimodule S P'] variables [smul_comm_class S R P] [smul_comm_class S R P'] include R variables (R S) /-- Create a bilinear map from a function that is linear in each component. See `mkβ‚‚` for the special case where both arguments come from modules over the same ring. -/ def mkβ‚‚' (f : M β†’ N β†’ P) (H1 : βˆ€ m₁ mβ‚‚ n, f (m₁ + mβ‚‚) n = f m₁ n + f mβ‚‚ n) (H2 : βˆ€ (c:R) m n, f (c β€’ m) n = c β€’ f m n) (H3 : βˆ€ m n₁ nβ‚‚, f m (n₁ + nβ‚‚) = f m n₁ + f m nβ‚‚) (H4 : βˆ€ (c:S) m n, f m (c β€’ n) = c β€’ f m n) : M β†’β‚—[R] N β†’β‚—[S] P := ⟨λ m, ⟨f m, H3 m, Ξ» c, H4 c m⟩, Ξ» m₁ mβ‚‚, linear_map.ext $ H1 m₁ mβ‚‚, Ξ» c m, linear_map.ext $ H2 c m⟩ variables {R S} @[simp] theorem mkβ‚‚'_apply (f : M β†’ N β†’ P) {H1 H2 H3 H4} (m : M) (n : N) : (mkβ‚‚' R S f H1 H2 H3 H4 : M β†’β‚—[R] N β†’β‚—[S] P) m n = f m n := rfl theorem extβ‚‚ {f g : M β†’β‚—[R] N β†’β‚—[S] P} (H : βˆ€ m n, f m n = g m n) : f = g := linear_map.ext (Ξ» m, linear_map.ext $ Ξ» n, H m n) section local attribute [instance] smul_comm_class.symm /-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map from `M Γ— N` to `P`, change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/ def flip (f : M β†’β‚—[R] N β†’β‚—[S] P) : N β†’β‚—[S] M β†’β‚—[R] P := mkβ‚‚' S R (Ξ» n m, f m n) (Ξ» n₁ nβ‚‚ m, (f m).map_add _ _) (Ξ» c n m, (f m).map_smul _ _) (Ξ» n m₁ mβ‚‚, by rw f.map_add; refl) (Ξ» c n m, by rw f.map_smul; refl) end @[simp] theorem flip_apply (f : M β†’β‚—[R] N β†’β‚—[S] P) (m : M) (n : N) : flip f n m = f m n := rfl variables {R} theorem flip_inj {f g : M β†’β‚—[R] N β†’β‚—[S] P} (H : flip f = flip g) : f = g := extβ‚‚ $ Ξ» m n, show flip f n m = flip g n m, by rw H theorem map_zeroβ‚‚ (f : M β†’β‚—[R] N β†’β‚—[S] P) (y) : f 0 y = 0 := (flip f y).map_zero theorem map_negβ‚‚ (f : M' β†’β‚—[R] N β†’β‚—[S] P') (x y) : f (-x) y = -f x y := (flip f y).map_neg _ theorem map_subβ‚‚ (f : M' β†’β‚—[R] N β†’β‚—[S] P') (x y z) : f (x - y) z = f x z - f y z := (flip f z).map_sub _ _ theorem map_addβ‚‚ (f : M β†’β‚—[R] N β†’β‚—[S] P) (x₁ xβ‚‚ y) : f (x₁ + xβ‚‚) y = f x₁ y + f xβ‚‚ y := (flip f y).map_add _ _ theorem map_smulβ‚‚ (f : M β†’β‚—[R] N β†’β‚—[S] P) (r : R) (x y) : f (r β€’ x) y = r β€’ f x y := (flip f y).map_smul _ _ end semiring section comm_semiring variables {R : Type*} [comm_semiring R] variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q] variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] variables (R) /-- Create a bilinear map from a function that is linear in each component. This is a shorthand for `mkβ‚‚'` for the common case when `R = S`. -/ def mkβ‚‚ (f : M β†’ N β†’ P) (H1 : βˆ€ m₁ mβ‚‚ n, f (m₁ + mβ‚‚) n = f m₁ n + f mβ‚‚ n) (H2 : βˆ€ (c:R) m n, f (c β€’ m) n = c β€’ f m n) (H3 : βˆ€ m n₁ nβ‚‚, f m (n₁ + nβ‚‚) = f m n₁ + f m nβ‚‚) (H4 : βˆ€ (c:R) m n, f m (c β€’ n) = c β€’ f m n) : M β†’β‚—[R] N β†’β‚—[R] P := mkβ‚‚' R R f H1 H2 H3 H4 @[simp] theorem mkβ‚‚_apply (f : M β†’ N β†’ P) {H1 H2 H3 H4} (m : M) (n : N) : (mkβ‚‚ R f H1 H2 H3 H4 : M β†’β‚—[R] N β†’β‚—[R] P) m n = f m n := rfl variables (R M N P) /-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map `M β†’ N β†’ P`, change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/ def lflip : (M β†’β‚—[R] N β†’β‚—[R] P) β†’β‚—[R] N β†’β‚—[R] M β†’β‚—[R] P := ⟨flip, Ξ» _ _, rfl, Ξ» _ _, rfl⟩ variables {R M N P} variables (f : M β†’β‚—[R] N β†’β‚—[R] P) @[simp] theorem lflip_apply (m : M) (n : N) : lflip R M N P f n m = f m n := rfl variables (R P) /-- Composing a linear map `M β†’ N` and a linear map `N β†’ P` to form a linear map `M β†’ P`. -/ def lcomp (f : M β†’β‚—[R] N) : (N β†’β‚—[R] P) β†’β‚—[R] M β†’β‚—[R] P := flip $ linear_map.comp (flip id) f variables {R P} @[simp] theorem lcomp_apply (f : M β†’β‚—[R] N) (g : N β†’β‚— P) (x : M) : lcomp R P f g x = g (f x) := rfl variables (R M N P) /-- Composing a linear map `M β†’ N` and a linear map `N β†’ P` to form a linear map `M β†’ P`. -/ def llcomp : (N β†’β‚—[R] P) β†’β‚—[R] (M β†’β‚—[R] N) β†’β‚— M β†’β‚— P := flip ⟨lcomp R P, Ξ» f f', extβ‚‚ $ Ξ» g x, g.map_add _ _, Ξ» (c : R) f, extβ‚‚ $ Ξ» g x, g.map_smul _ _⟩ variables {R M N P} section @[simp] theorem llcomp_apply (f : N β†’β‚—[R] P) (g : M β†’β‚—[R] N) (x : M) : llcomp R M N P f g x = f (g x) := rfl end /-- Composing a linear map `Q β†’ N` and a bilinear map `M β†’ N β†’ P` to form a bilinear map `M β†’ Q β†’ P`. -/ def complβ‚‚ (g : Q β†’β‚— N) : M β†’β‚— Q β†’β‚— P := (lcomp R _ g).comp f @[simp] theorem complβ‚‚_apply (g : Q β†’β‚—[R] N) (m : M) (q : Q) : f.complβ‚‚ g m q = f m (g q) := rfl /-- Composing a linear map `P β†’ Q` and a bilinear map `M Γ— N β†’ P` to form a bilinear map `M β†’ N β†’ Q`. -/ def comprβ‚‚ (g : P β†’β‚— Q) : M β†’β‚— N β†’β‚— Q := linear_map.comp (llcomp R N P Q g) f @[simp] theorem comprβ‚‚_apply (g : P β†’β‚—[R] Q) (m : M) (n : N) : f.comprβ‚‚ g m n = g (f m n) := rfl variables (R M) /-- Scalar multiplication as a bilinear map `R β†’ M β†’ M`. -/ def lsmul : R β†’β‚— M β†’β‚— M := mkβ‚‚ R (β€’) add_smul (Ξ» _ _ _, mul_smul _ _ _) smul_add (Ξ» r s m, by simp only [smul_smul, smul_eq_mul, mul_comm]) variables {R M} @[simp] theorem lsmul_apply (r : R) (m : M) : lsmul R M r m = r β€’ m := rfl end comm_semiring end linear_map section semiring variables {R : Type*} [comm_semiring R] variables {R' : Type*} [comm_semiring R'] variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*} variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q] [add_comm_monoid S] variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S] variables [semimodule R' M] [semimodule R' N] include R variables (M N) namespace tensor_product section -- open free_add_monoid variables (R) /-- The relation on `free_add_monoid (M Γ— N)` that generates a congruence whose quotient is the tensor product. -/ inductive eqv : free_add_monoid (M Γ— N) β†’ free_add_monoid (M Γ— N) β†’ Prop | of_zero_left : βˆ€ n : N, eqv (free_add_monoid.of (0, n)) 0 | of_zero_right : βˆ€ m : M, eqv (free_add_monoid.of (m, 0)) 0 | of_add_left : βˆ€ (m₁ mβ‚‚ : M) (n : N), eqv (free_add_monoid.of (m₁, n) + free_add_monoid.of (mβ‚‚, n)) (free_add_monoid.of (m₁ + mβ‚‚, n)) | of_add_right : βˆ€ (m : M) (n₁ nβ‚‚ : N), eqv (free_add_monoid.of (m, n₁) + free_add_monoid.of (m, nβ‚‚)) (free_add_monoid.of (m, n₁ + nβ‚‚)) | of_smul : βˆ€ (r : R) (m : M) (n : N), eqv (free_add_monoid.of (r β€’ m, n)) (free_add_monoid.of (m, r β€’ n)) | add_comm : βˆ€ x y, eqv (x + y) (y + x) end end tensor_product variables (R) /-- The tensor product of two semimodules `M` and `N` over the same commutative semiring `R`. The localized notations are `M βŠ— N` and `M βŠ—[R] N`, accessed by `open_locale tensor_product`. -/ def tensor_product : Type* := (add_con_gen (tensor_product.eqv R M N)).quotient variables {R} localized "infix ` βŠ— `:100 := tensor_product _" in tensor_product localized "notation M ` βŠ—[`:100 R `] `:0 N:100 := tensor_product R M N" in tensor_product namespace tensor_product section module instance : add_comm_monoid (M βŠ—[R] N) := { add_comm := Ξ» x y, add_con.induction_onβ‚‚ x y $ Ξ» x y, quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.add_comm _ _, .. (add_con_gen (tensor_product.eqv R M N)).add_monoid } instance : inhabited (M βŠ—[R] N) := ⟨0⟩ variables (R) {M N} /-- The canonical function `M β†’ N β†’ M βŠ— N`. The localized notations are `m βŠ—β‚œ n` and `m βŠ—β‚œ[R] n`, accessed by `open_locale tensor_product`. -/ def tmul (m : M) (n : N) : M βŠ—[R] N := add_con.mk' _ $ free_add_monoid.of (m, n) variables {R} infix ` βŠ—β‚œ `:100 := tmul _ notation x ` βŠ—β‚œ[`:100 R `] `:0 y:100 := tmul R x y @[elab_as_eliminator] protected theorem induction_on {C : (M βŠ—[R] N) β†’ Prop} (z : M βŠ—[R] N) (C0 : C 0) (C1 : βˆ€ {x y}, C $ x βŠ—β‚œ[R] y) (Cp : βˆ€ {x y}, C x β†’ C y β†’ C (x + y)) : C z := add_con.induction_on z $ Ξ» x, free_add_monoid.rec_on x C0 $ Ξ» ⟨m, n⟩ y ih, by { rw add_con.coe_add, exact Cp C1 ih } variables (M) @[simp] lemma zero_tmul (n : N) : (0 : M) βŠ—β‚œ[R] n = 0 := quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_left _ variables {M} lemma add_tmul (m₁ mβ‚‚ : M) (n : N) : (m₁ + mβ‚‚) βŠ—β‚œ n = m₁ βŠ—β‚œ n + mβ‚‚ βŠ—β‚œ[R] n := eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_left _ _ _ variables (N) @[simp] lemma tmul_zero (m : M) : m βŠ—β‚œ[R] (0 : N) = 0 := quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_right _ variables {N} lemma tmul_add (m : M) (n₁ nβ‚‚ : N) : m βŠ—β‚œ (n₁ + nβ‚‚) = m βŠ—β‚œ n₁ + m βŠ—β‚œ[R] nβ‚‚ := eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_right _ _ _ section variables (R R' M N) /-- A typeclass for `has_scalar` structures which can be moved across a tensor product. This typeclass is generated automatically from a `is_scalar_tower` instance, but exists so that we can also add an instance for `add_comm_group.int_module`, allowing `z β€’` to be moved even if `R` does not support negation. Note that `semimodule R' (M βŠ—[R] N)` is available even without this typeclass on `R'`; it's only needed if `tensor_product.smul_tmul`, `tensor_product.smul_tmul'`, or `tensor_product.tmul_smul` is used. -/ class compatible_smul := (smul_tmul : βˆ€ (r : R') (m : M) (n : N), (r β€’ m) βŠ—β‚œ n = m βŠ—β‚œ[R] (r β€’ n)) end /-- Note that this provides the default `compatible_smul R R M N` instance through `mul_action.is_scalar_tower.left`. -/ @[priority 100] instance compatible_smul.is_scalar_tower [has_scalar R' R] [is_scalar_tower R' R M] [is_scalar_tower R' R N] : compatible_smul R R' M N := ⟨λ r m n, begin conv_lhs {rw ← one_smul R m}, conv_rhs {rw ← one_smul R n}, rw [←smul_assoc, ←smul_assoc], exact (quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_smul _ _ _), end⟩ /-- `smul` can be moved from one side of the product to the other .-/ lemma smul_tmul [compatible_smul R R' M N] (r : R') (m : M) (n : N) : (r β€’ m) βŠ—β‚œ n = m βŠ—β‚œ[R] (r β€’ n) := compatible_smul.smul_tmul _ _ _ /-- Auxiliary function to defining scalar multiplication on tensor product. -/ def smul.aux {R' : Type*} [has_scalar R' M] (r : R') : free_add_monoid (M Γ— N) β†’+ M βŠ—[R] N := free_add_monoid.lift $ Ξ» p : M Γ— N, (r β€’ p.1) βŠ—β‚œ p.2 theorem smul.aux_of {R' : Type*} [has_scalar R' M] (r : R') (m : M) (n : N) : smul.aux r (free_add_monoid.of (m, n)) = (r β€’ m) βŠ—β‚œ[R] n := rfl variables [smul_comm_class R R' M] [smul_comm_class R R' N] -- Most of the time we want the instance below this one, which is easier for typeclass resolution -- to find. The `unused_arguments` is from one of the two comm_classes - while we only make use -- of one, it makes sense to make the API symmetric. @[nolint unused_arguments] instance has_scalar' : has_scalar R' (M βŠ—[R] N) := ⟨λ r, (add_con_gen (tensor_product.eqv R M N)).lift (smul.aux r : _ β†’+ M βŠ—[R] N) $ add_con.add_con_gen_le $ Ξ» x y hxy, match x, y, hxy with | _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, smul.aux_of, smul_zero, zero_tmul] | _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, smul.aux_of, tmul_zero] | _, _, (eqv.of_add_left m₁ mβ‚‚ n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, smul.aux_of, smul_add, add_tmul] | _, _, (eqv.of_add_right m n₁ nβ‚‚) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, smul.aux_of, tmul_add] | _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $ by rw [smul.aux_of, smul.aux_of, ←smul_comm, smul_tmul] | _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, add_comm] end⟩ instance : has_scalar R (M βŠ—[R] N) := tensor_product.has_scalar' protected theorem smul_zero (r : R') : (r β€’ 0 : M βŠ—[R] N) = 0 := add_monoid_hom.map_zero _ protected theorem smul_add (r : R') (x y : M βŠ—[R] N) : r β€’ (x + y) = r β€’ x + r β€’ y := add_monoid_hom.map_add _ _ _ -- Most of the time we want the instance below this one, which is easier for typeclass resolution -- to find. instance semimodule' : semimodule R' (M βŠ—[R] N) := have βˆ€ (r : R') (m : M) (n : N), r β€’ (m βŠ—β‚œ[R] n) = (r β€’ m) βŠ—β‚œ n := Ξ» _ _ _, rfl, { smul := (β€’), smul_add := Ξ» r x y, tensor_product.smul_add r x y, mul_smul := Ξ» r s x, tensor_product.induction_on x (by simp_rw tensor_product.smul_zero) (Ξ» m n, by simp_rw [this, mul_smul]) (Ξ» x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy] }), one_smul := Ξ» x, tensor_product.induction_on x (by rw tensor_product.smul_zero) (Ξ» m n, by rw [this, one_smul]) (Ξ» x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy]), add_smul := Ξ» r s x, tensor_product.induction_on x (by simp_rw [tensor_product.smul_zero, add_zero]) (Ξ» m n, by simp_rw [this, add_smul, add_tmul]) (Ξ» x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy, add_add_add_comm] }), smul_zero := Ξ» r, tensor_product.smul_zero r, zero_smul := Ξ» x, tensor_product.induction_on x (by rw tensor_product.smul_zero) (Ξ» m n, by rw [this, zero_smul, zero_tmul]) (Ξ» x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy, add_zero]) } instance : semimodule R (M βŠ—[R] N) := tensor_product.semimodule' -- note that we don't actually need `compatible_smul` here, but we include it for symmetry -- with `tmul_smul` to avoid exposing our asymmetric definition. @[nolint unused_arguments] theorem smul_tmul' [compatible_smul R R' M N] (r : R') (m : M) (n : N) : r β€’ (m βŠ—β‚œ[R] n) = (r β€’ m) βŠ—β‚œ n := rfl @[simp] lemma tmul_smul [compatible_smul R R' M N] (r : R') (x : M) (y : N) : x βŠ—β‚œ (r β€’ y) = r β€’ (x βŠ—β‚œ[R] y) := (smul_tmul _ _ _).symm variables (R M N) /-- The canonical bilinear map `M β†’ N β†’ M βŠ—[R] N`. -/ def mk : M β†’β‚— N β†’β‚— M βŠ—[R] N := linear_map.mkβ‚‚ R (βŠ—β‚œ) add_tmul (Ξ» c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul variables {R M N} @[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m βŠ—β‚œ n := rfl lemma ite_tmul (x₁ : M) (xβ‚‚ : N) (P : Prop) [decidable P] : (if P then x₁ else 0) βŠ—β‚œ[R] xβ‚‚ = if P then x₁ βŠ—β‚œ xβ‚‚ else 0 := by { split_ifs; simp } lemma tmul_ite (x₁ : M) (xβ‚‚ : N) (P : Prop) [decidable P] : x₁ βŠ—β‚œ[R] (if P then xβ‚‚ else 0) = if P then x₁ βŠ—β‚œ xβ‚‚ else 0 := by { split_ifs; simp } section open_locale big_operators lemma sum_tmul {Ξ± : Type*} (s : finset Ξ±) (m : Ξ± β†’ M) (n : N) : (βˆ‘ a in s, m a) βŠ—β‚œ[R] n = βˆ‘ a in s, m a βŠ—β‚œ[R] n := begin classical, induction s using finset.induction with a s has ih h, { simp, }, { simp [finset.sum_insert has, add_tmul, ih], }, end lemma tmul_sum (m : M) {Ξ± : Type*} (s : finset Ξ±) (n : Ξ± β†’ N) : m βŠ—β‚œ[R] (βˆ‘ a in s, n a) = βˆ‘ a in s, m βŠ—β‚œ[R] n a := begin classical, induction s using finset.induction with a s has ih h, { simp, }, { simp [finset.sum_insert has, tmul_add, ih], }, end end end module section UMP variables {M N P Q} variables (f : M β†’β‚—[R] N β†’β‚—[R] P) /-- Auxiliary function to constructing a linear map `M βŠ— N β†’ P` given a bilinear map `M β†’ N β†’ P` with the property that its composition with the canonical bilinear map `M β†’ N β†’ M βŠ— N` is the given bilinear map `M β†’ N β†’ P`. -/ def lift_aux : (M βŠ—[R] N) β†’+ P := (add_con_gen (tensor_product.eqv R M N)).lift (free_add_monoid.lift $ Ξ» p : M Γ— N, f p.1 p.2) $ add_con.add_con_gen_le $ Ξ» x y hxy, match x, y, hxy with | _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, f.map_zeroβ‚‚] | _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, (f m).map_zero] | _, _, (eqv.of_add_left m₁ mβ‚‚ n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, f.map_addβ‚‚] | _, _, (eqv.of_add_right m n₁ nβ‚‚) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, (f m).map_add] | _, _, (eqv.of_smul r m n) := (add_con.ker_rel _).2 $ by simp_rw [free_add_monoid.lift_eval_of, f.map_smulβ‚‚, (f m).map_smul] | _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, add_comm] end lemma lift_aux_tmul (m n) : lift_aux f (m βŠ—β‚œ n) = f m n := zero_add _ variable {f} @[simp] lemma lift_aux.smul (r : R) (x) : lift_aux f (r β€’ x) = r β€’ lift_aux f x := tensor_product.induction_on x (smul_zero _).symm (Ξ» p q, by rw [← tmul_smul, lift_aux_tmul, lift_aux_tmul, (f p).map_smul]) (Ξ» p q ih1 ih2, by rw [smul_add, (lift_aux f).map_add, ih1, ih2, (lift_aux f).map_add, smul_add]) variable (f) /-- Constructing a linear map `M βŠ— N β†’ P` given a bilinear map `M β†’ N β†’ P` with the property that its composition with the canonical bilinear map `M β†’ N β†’ M βŠ— N` is the given bilinear map `M β†’ N β†’ P`. -/ def lift : M βŠ— N β†’β‚— P := { map_smul' := lift_aux.smul, .. lift_aux f } variable {f} @[simp] lemma lift.tmul (x y) : lift f (x βŠ—β‚œ y) = f x y := zero_add _ @[simp] lemma lift.tmul' (x y) : (lift f).1 (x βŠ—β‚œ y) = f x y := lift.tmul _ _ theorem ext {g h : (M βŠ—[R] N) β†’β‚—[R] P} (H : βˆ€ x y, g (x βŠ—β‚œ y) = h (x βŠ—β‚œ y)) : g = h := linear_map.ext $ Ξ» z, tensor_product.induction_on z (by simp_rw linear_map.map_zero) H $ Ξ» x y ihx ihy, by rw [g.map_add, h.map_add, ihx, ihy] theorem lift.unique {g : (M βŠ—[R] N) β†’β‚—[R] P} (H : βˆ€ x y, g (x βŠ—β‚œ y) = f x y) : g = lift f := ext $ Ξ» m n, by rw [H, lift.tmul] theorem lift_mk : lift (mk R M N) = linear_map.id := eq.symm $ lift.unique $ Ξ» x y, rfl theorem lift_comprβ‚‚ (g : P β†’β‚— Q) : lift (f.comprβ‚‚ g) = g.comp (lift f) := eq.symm $ lift.unique $ Ξ» x y, by simp theorem lift_mk_comprβ‚‚ (f : M βŠ— N β†’β‚— P) : lift ((mk R M N).comprβ‚‚ f) = f := by rw [lift_comprβ‚‚ f, lift_mk, linear_map.comp_id] /-- Using this as the `@[ext]` lemma instead of `tensor_product.ext` allows `ext` to apply lemmas specific to `M β†’β‚— _` and `N β†’β‚— _`. See note [partially-applied ext lemmas]. -/ @[ext] theorem mk_comprβ‚‚_inj {g h : M βŠ— N β†’β‚— P} (H : (mk R M N).comprβ‚‚ g = (mk R M N).comprβ‚‚ h) : g = h := by rw [← lift_mk_comprβ‚‚ g, H, lift_mk_comprβ‚‚] example : M β†’ N β†’ (M β†’ N β†’ P) β†’ P := Ξ» m, flip $ Ξ» f, f m variables (R M N P) /-- Linearly constructing a linear map `M βŠ— N β†’ P` given a bilinear map `M β†’ N β†’ P` with the property that its composition with the canonical bilinear map `M β†’ N β†’ M βŠ— N` is the given bilinear map `M β†’ N β†’ P`. -/ def uncurry : (M β†’β‚—[R] N β†’β‚—[R] P) β†’β‚—[R] M βŠ—[R] N β†’β‚—[R] P := linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp (linear_map.flip linear_map.id) variables {R M N P} @[simp] theorem uncurry_apply (f : M β†’β‚—[R] N β†’β‚—[R] P) (m : M) (n : N) : uncurry R M N P f (m βŠ—β‚œ n) = f m n := by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl variables (R M N P) /-- A linear equivalence constructing a linear map `M βŠ— N β†’ P` given a bilinear map `M β†’ N β†’ P` with the property that its composition with the canonical bilinear map `M β†’ N β†’ M βŠ— N` is the given bilinear map `M β†’ N β†’ P`. -/ def lift.equiv : (M β†’β‚— N β†’β‚— P) ≃ₗ (M βŠ— N β†’β‚— P) := { inv_fun := Ξ» f, (mk R M N).comprβ‚‚ f, left_inv := Ξ» f, linear_map.extβ‚‚ $ Ξ» m n, lift.tmul _ _, right_inv := Ξ» f, ext $ Ξ» m n, lift.tmul _ _, .. uncurry R M N P } /-- Given a linear map `M βŠ— N β†’ P`, compose it with the canonical bilinear map `M β†’ N β†’ M βŠ— N` to form a bilinear map `M β†’ N β†’ P`. -/ def lcurry : (M βŠ—[R] N β†’β‚—[R] P) β†’β‚—[R] M β†’β‚—[R] N β†’β‚—[R] P := (lift.equiv R M N P).symm variables {R M N P} @[simp] theorem lcurry_apply (f : M βŠ—[R] N β†’β‚—[R] P) (m : M) (n : N) : lcurry R M N P f m n = f (m βŠ—β‚œ n) := rfl /-- Given a linear map `M βŠ— N β†’ P`, compose it with the canonical bilinear map `M β†’ N β†’ M βŠ— N` to form a bilinear map `M β†’ N β†’ P`. -/ def curry (f : M βŠ— N β†’β‚— P) : M β†’β‚— N β†’β‚— P := lcurry R M N P f @[simp] theorem curry_apply (f : M βŠ— N β†’β‚—[R] P) (m : M) (n : N) : curry f m n = f (m βŠ—β‚œ n) := rfl theorem ext_threefold {g h : (M βŠ—[R] N) βŠ—[R] P β†’β‚—[R] Q} (H : βˆ€ x y z, g ((x βŠ—β‚œ y) βŠ—β‚œ z) = h ((x βŠ—β‚œ y) βŠ—β‚œ z)) : g = h := begin ext x y z, exact H x y z end -- We'll need this one for checking the pentagon identity! theorem ext_fourfold {g h : ((M βŠ—[R] N) βŠ—[R] P) βŠ—[R] Q β†’β‚—[R] S} (H : βˆ€ w x y z, g (((w βŠ—β‚œ x) βŠ—β‚œ y) βŠ—β‚œ z) = h (((w βŠ—β‚œ x) βŠ—β‚œ y) βŠ—β‚œ z)) : g = h := begin ext w x y z, exact H w x y z, end end UMP variables {M N} section variables (R M) /-- The base ring is a left identity for the tensor product of modules, up to linear equivalence. -/ protected def lid : R βŠ— M ≃ₗ M := linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1) (linear_map.ext $ Ξ» _, by simp) (ext $ Ξ» r m, by simp; rw [← tmul_smul, ← smul_tmul, smul_eq_mul, mul_one]) end @[simp] theorem lid_tmul (m : M) (r : R) : ((tensor_product.lid R M) : (R βŠ— M β†’ M)) (r βŠ—β‚œ m) = r β€’ m := begin dsimp [tensor_product.lid], simp, end @[simp] lemma lid_symm_apply (m : M) : (tensor_product.lid R M).symm m = 1 βŠ—β‚œ m := rfl section variables (R M N) /-- The tensor product of modules is commutative, up to linear equivalence. -/ protected def comm : M βŠ— N ≃ₗ N βŠ— M := linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip) (ext $ Ξ» m n, rfl) (ext $ Ξ» m n, rfl) @[simp] theorem comm_tmul (m : M) (n : N) : (tensor_product.comm R M N) (m βŠ—β‚œ n) = n βŠ—β‚œ m := rfl @[simp] theorem comm_symm_tmul (m : M) (n : N) : (tensor_product.comm R M N).symm (n βŠ—β‚œ m) = m βŠ—β‚œ n := rfl end section variables (R M) /-- The base ring is a right identity for the tensor product of modules, up to linear equivalence. -/ protected def rid : M βŠ—[R] R ≃ₗ M := linear_equiv.trans (tensor_product.comm R M R) (tensor_product.lid R M) end @[simp] theorem rid_tmul (m : M) (r : R) : (tensor_product.rid R M) (m βŠ—β‚œ r) = r β€’ m := begin dsimp [tensor_product.rid, tensor_product.comm, tensor_product.lid], simp, end @[simp] lemma rid_symm_apply (m : M) : (tensor_product.rid R M).symm m = m βŠ—β‚œ 1 := rfl open linear_map section variables (R M N P) /-- The associator for tensor product of R-modules, as a linear equivalence. -/ protected def assoc : (M βŠ—[R] N) βŠ—[R] P ≃ₗ[R] M βŠ—[R] (N βŠ—[R] P) := begin refine linear_equiv.of_linear (lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _) (lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _) (mk_comprβ‚‚_inj $ linear_map.ext $ Ξ» m, ext $ Ξ» n p, _) (mk_comprβ‚‚_inj $ flip_inj $ linear_map.ext $ Ξ» p, ext $ Ξ» m n, _); repeat { rw lift.tmul <|> rw comprβ‚‚_apply <|> rw comp_apply <|> rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|> rw uncurry_apply <|> rw curry_apply <|> rw id_apply } end end @[simp] theorem assoc_tmul (m : M) (n : N) (p : P) : (tensor_product.assoc R M N P) ((m βŠ—β‚œ n) βŠ—β‚œ p) = m βŠ—β‚œ (n βŠ—β‚œ p) := rfl @[simp] theorem assoc_symm_tmul (m : M) (n : N) (p : P) : (tensor_product.assoc R M N P).symm (m βŠ—β‚œ (n βŠ—β‚œ p)) = (m βŠ—β‚œ n) βŠ—β‚œ p := rfl /-- The tensor product of a pair of linear maps between modules. -/ def map (f : M β†’β‚—[R] P) (g : N β†’β‚— Q) : M βŠ— N β†’β‚—[R] P βŠ— Q := lift $ comp (complβ‚‚ (mk _ _ _) g) f @[simp] theorem map_tmul (f : M β†’β‚—[R] P) (g : N β†’β‚—[R] Q) (m : M) (n : N) : map f g (m βŠ—β‚œ n) = f m βŠ—β‚œ g n := rfl section variables {P' Q' : Type*} variables [add_comm_monoid P'] [semimodule R P'] variables [add_comm_monoid Q'] [semimodule R Q'] lemma map_comp (fβ‚‚ : P β†’β‚—[R] P') (f₁ : M β†’β‚—[R] P) (gβ‚‚ : Q β†’β‚—[R] Q') (g₁ : N β†’β‚—[R] Q) : map (fβ‚‚.comp f₁) (gβ‚‚.comp g₁) = (map fβ‚‚ gβ‚‚).comp (map f₁ g₁) := ext $ Ξ» _ _, by simp only [linear_map.comp_apply, map_tmul] lemma lift_comp_map (i : P β†’β‚—[R] Q β†’β‚—[R] Q') (f : M β†’β‚—[R] P) (g : N β†’β‚—[R] Q) : (lift i).comp (map f g) = lift ((i.comp f).complβ‚‚ g) := ext $ Ξ» _ _, by simp only [lift.tmul, map_tmul, linear_map.complβ‚‚_apply, linear_map.comp_apply] end /-- If `M` and `P` are linearly equivalent and `N` and `Q` are linearly equivalent then `M βŠ— N` and `P βŠ— Q` are linearly equivalent. -/ def congr (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) : M βŠ— N ≃ₗ[R] P βŠ— Q := linear_equiv.of_linear (map f g) (map f.symm g.symm) (ext $ Ξ» m n, by simp; simp only [linear_equiv.apply_symm_apply]) (ext $ Ξ» m n, by simp; simp only [linear_equiv.symm_apply_apply]) @[simp] theorem congr_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (m : M) (n : N) : congr f g (m βŠ—β‚œ n) = f m βŠ—β‚œ g n := rfl @[simp] theorem congr_symm_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (p : P) (q : Q) : (congr f g).symm (p βŠ—β‚œ q) = f.symm p βŠ—β‚œ g.symm q := rfl end tensor_product namespace linear_map variables {R} (M) {N P Q} /-- `ltensor M f : M βŠ— N β†’β‚— M βŠ— P` is the natural linear map induced by `f : N β†’β‚— P`. -/ def ltensor (f : N β†’β‚—[R] P) : M βŠ— N β†’β‚—[R] M βŠ— P := tensor_product.map id f /-- `rtensor f M : N₁ βŠ— M β†’β‚— Nβ‚‚ βŠ— M` is the natural linear map induced by `f : N₁ β†’β‚— Nβ‚‚`. -/ def rtensor (f : N β†’β‚—[R] P) : N βŠ— M β†’β‚—[R] P βŠ— M := tensor_product.map f id variables (g : P β†’β‚—[R] Q) (f : N β†’β‚—[R] P) @[simp] lemma ltensor_tmul (m : M) (n : N) : f.ltensor M (m βŠ—β‚œ n) = m βŠ—β‚œ (f n) := rfl @[simp] lemma rtensor_tmul (m : M) (n : N) : f.rtensor M (n βŠ—β‚œ m) = (f n) βŠ—β‚œ m := rfl open tensor_product /-- `ltensor_hom M` is the natural linear map that sends a linear map `f : N β†’β‚— P` to `M βŠ— f`. -/ def ltensor_hom : (N β†’β‚—[R] P) β†’β‚—[R] (M βŠ—[R] N β†’β‚—[R] M βŠ—[R] P) := { to_fun := ltensor M, map_add' := Ξ» f g, by { ext x y, simp only [comprβ‚‚_apply, mk_apply, add_apply, ltensor_tmul, tmul_add] }, map_smul' := Ξ» r f, by { ext x y, simp only [comprβ‚‚_apply, mk_apply, tmul_smul, smul_apply, ltensor_tmul] } } /-- `rtensor_hom M` is the natural linear map that sends a linear map `f : N β†’β‚— P` to `M βŠ— f`. -/ def rtensor_hom : (N β†’β‚—[R] P) β†’β‚—[R] (N βŠ—[R] M β†’β‚—[R] P βŠ—[R] M) := { to_fun := Ξ» f, f.rtensor M, map_add' := Ξ» f g, by { ext x y, simp only [comprβ‚‚_apply, mk_apply, add_apply, rtensor_tmul, add_tmul] }, map_smul' := Ξ» r f, by { ext x y, simp only [comprβ‚‚_apply, mk_apply, smul_tmul, tmul_smul, smul_apply, rtensor_tmul] } } @[simp] lemma coe_ltensor_hom : (ltensor_hom M : (N β†’β‚—[R] P) β†’ (M βŠ—[R] N β†’β‚—[R] M βŠ—[R] P)) = ltensor M := rfl @[simp] lemma coe_rtensor_hom : (rtensor_hom M : (N β†’β‚—[R] P) β†’ (N βŠ—[R] M β†’β‚—[R] P βŠ—[R] M)) = rtensor M := rfl @[simp] lemma ltensor_add (f g : N β†’β‚—[R] P) : (f + g).ltensor M = f.ltensor M + g.ltensor M := (ltensor_hom M).map_add f g @[simp] lemma rtensor_add (f g : N β†’β‚—[R] P) : (f + g).rtensor M = f.rtensor M + g.rtensor M := (rtensor_hom M).map_add f g @[simp] lemma ltensor_zero : ltensor M (0 : N β†’β‚—[R] P) = 0 := (ltensor_hom M).map_zero @[simp] lemma rtensor_zero : rtensor M (0 : N β†’β‚—[R] P) = 0 := (rtensor_hom M).map_zero @[simp] lemma ltensor_smul (r : R) (f : N β†’β‚—[R] P) : (r β€’ f).ltensor M = r β€’ (f.ltensor M) := (ltensor_hom M).map_smul r f @[simp] lemma rtensor_smul (r : R) (f : N β†’β‚—[R] P) : (r β€’ f).rtensor M = r β€’ (f.rtensor M) := (rtensor_hom M).map_smul r f lemma ltensor_comp : (g.comp f).ltensor M = (g.ltensor M).comp (f.ltensor M) := by { ext m n, simp only [comprβ‚‚_apply, mk_apply, comp_apply, ltensor_tmul] } lemma rtensor_comp : (g.comp f).rtensor M = (g.rtensor M).comp (f.rtensor M) := by { ext m n, simp only [comprβ‚‚_apply, mk_apply, comp_apply, rtensor_tmul] } variables (N) @[simp] lemma ltensor_id : (id : N β†’β‚—[R] N).ltensor M = id := by { ext m n, simp only [comprβ‚‚_apply, mk_apply, id_coe, id.def, ltensor_tmul] } @[simp] lemma rtensor_id : (id : N β†’β‚—[R] N).rtensor M = id := by { ext m n, simp only [comprβ‚‚_apply, mk_apply, id_coe, id.def, rtensor_tmul] } variables {N} @[simp] lemma ltensor_comp_rtensor (f : M β†’β‚—[R] P) (g : N β†’β‚—[R] Q) : (g.ltensor P).comp (f.rtensor N) = map f g := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma rtensor_comp_ltensor (f : M β†’β‚—[R] P) (g : N β†’β‚—[R] Q) : (f.rtensor Q).comp (g.ltensor M) = map f g := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma map_comp_rtensor (f : M β†’β‚—[R] P) (g : N β†’β‚—[R] Q) (f' : S β†’β‚—[R] M) : (map f g).comp (f'.rtensor _) = map (f.comp f') g := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma map_comp_ltensor (f : M β†’β‚—[R] P) (g : N β†’β‚—[R] Q) (g' : S β†’β‚—[R] N) : (map f g).comp (g'.ltensor _) = map f (g.comp g') := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma rtensor_comp_map (f' : P β†’β‚—[R] S) (f : M β†’β‚—[R] P) (g : N β†’β‚—[R] Q) : (f'.rtensor _).comp (map f g) = map (f'.comp f) g := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] @[simp] lemma ltensor_comp_map (g' : Q β†’β‚—[R] S) (f : M β†’β‚—[R] P) (g : N β†’β‚—[R] Q) : (g'.ltensor _).comp (map f g) = map f (g'.comp g) := by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id] end linear_map end semiring section ring variables {R : Type*} [comm_semiring R] variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*} variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q] [add_comm_group S] variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S] namespace tensor_product open_locale tensor_product open linear_map variables (R) /-- Auxiliary function to defining negation multiplication on tensor product. -/ def neg.aux : free_add_monoid (M Γ— N) β†’+ M βŠ—[R] N := free_add_monoid.lift $ Ξ» p : M Γ— N, (-p.1) βŠ—β‚œ p.2 variables {R} theorem neg.aux_of (m : M) (n : N) : neg.aux R (free_add_monoid.of (m, n)) = (-m) βŠ—β‚œ[R] n := rfl instance : has_neg (M βŠ—[R] N) := { neg := (add_con_gen (tensor_product.eqv R M N)).lift (neg.aux R) $ add_con.add_con_gen_le $ Ξ» x y hxy, match x, y, hxy with | _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, neg.aux_of, neg_zero, zero_tmul] | _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_zero, neg.aux_of, tmul_zero] | _, _, (eqv.of_add_left m₁ mβ‚‚ n) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, neg.aux_of, neg_add, add_tmul] | _, _, (eqv.of_add_right m n₁ nβ‚‚) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, neg.aux_of, tmul_add] | _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $ by simp_rw [neg.aux_of, tmul_smul s, smul_tmul', smul_neg] | _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $ by simp_rw [add_monoid_hom.map_add, add_comm] end } instance : add_comm_group (M βŠ—[R] N) := { neg := has_neg.neg, sub := _, sub_eq_add_neg := Ξ» _ _, rfl, add_left_neg := Ξ» x, tensor_product.induction_on x (by { rw [add_zero], apply (neg.aux R).map_zero, }) (Ξ» x y, by { convert (add_tmul (-x) x y).symm, rw [add_left_neg, zero_tmul], }) (Ξ» x y hx hy, by { unfold has_neg.neg sub_neg_monoid.neg, rw add_monoid_hom.map_add, ac_change (-x + x) + (-y + y) = 0, rw [hx, hy, add_zero], }), ..(infer_instance : add_comm_monoid (M βŠ—[R] N)) } lemma neg_tmul (m : M) (n : N) : (-m) βŠ—β‚œ n = -(m βŠ—β‚œ[R] n) := rfl lemma tmul_neg (m : M) (n : N) : m βŠ—β‚œ (-n) = -(m βŠ—β‚œ[R] n) := (mk R M N _).map_neg _ lemma tmul_sub (m : M) (n₁ nβ‚‚ : N) : m βŠ—β‚œ (n₁ - nβ‚‚) = (m βŠ—β‚œ[R] n₁) - (m βŠ—β‚œ[R] nβ‚‚) := (mk R M N _).map_sub _ _ lemma sub_tmul (m₁ mβ‚‚ : M) (n : N) : (m₁ - mβ‚‚) βŠ—β‚œ n = (m₁ βŠ—β‚œ[R] n) - (mβ‚‚ βŠ—β‚œ[R] n) := (mk R M N).map_subβ‚‚ _ _ _ /-- While the tensor product will automatically inherit a β„€-module structure from `add_comm_group.int_module`, that structure won't be compatible with lemmas like `tmul_smul` unless we use a `β„€-module` instance provided by `tensor_product.semimodule'`. When `R` is a `ring` we get the required `tensor_product.compatible_smul` instance through `is_scalar_tower`, but when it is only a `semiring` we need to build it from scratch. The instance diamond in `compatible_smul` doesn't matter because it's in `Prop`. -/ instance compatible_smul.int [semimodule β„€ M] [semimodule β„€ N] : compatible_smul R β„€ M N := ⟨λ r m n, int.induction_on r (by simp) (Ξ» r ih, by simpa [add_smul, tmul_add, add_tmul] using ih) (Ξ» r ih, by simpa [sub_smul, tmul_sub, sub_tmul] using ih)⟩ end tensor_product namespace linear_map @[simp] lemma ltensor_sub (f g : N β†’β‚—[R] P) : (f - g).ltensor M = f.ltensor M - g.ltensor M := by simp only [← coe_ltensor_hom, map_sub] @[simp] lemma rtensor_sub (f g : N β†’β‚—[R] P) : (f - g).rtensor M = f.rtensor M - g.rtensor M := by simp only [← coe_rtensor_hom, map_sub] @[simp] lemma ltensor_neg (f : N β†’β‚—[R] P) : (-f).ltensor M = -(f.ltensor M) := by simp only [← coe_ltensor_hom, map_neg] @[simp] lemma rtensor_neg (f : N β†’β‚—[R] P) : (-f).rtensor M = -(f.rtensor M) := by simp only [← coe_rtensor_hom, map_neg] end linear_map end ring
cab7e3344f740ac7dd158f93932a7e449a64ba15
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/unfoldDefEq.lean
f03ebafebe97fdc416be51d11c61c00621dad94c
[ "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
351
lean
def Wrapper (n : Nat) : Type Γ— Type := (Fin n, Fin n) def some_property {n} (x : Fin n) : Prop := True example (x : (Wrapper n).1) (h : some_property x) : True := by unfold Wrapper at x trace_state simp at x trace_state sorry example (x : (Wrapper n).1) (h : some_property x) : True := by simp [Wrapper] at x trace_state sorry
4e0395759f3930c3eaf7d7ebe75fab99f586913a
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/coe8.lean
d58eb7db884236825132678a5e18afd9807f2909
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
357
lean
import logic namespace experiment constant nat : Type.{1} constant int : Type.{1} constant of_nat : nat β†’ int coercion of_nat constant nat_add : nat β†’ nat β†’ nat constant int_add : int β†’ int β†’ int infixl `+` := int_add infixl `+` := nat_add print "================" constant tst (n m : nat) : @eq int (of_nat n + of_nat m) (n + m) check tst exit
f1adc3e55de304b55a5140443bfebc825399985e
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/compiler/float_cases_bug.lean
1ac9b2673e84794e5e3146f18032cd4b0e3fd5b8
[ "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
912
lean
inductive Term : Type | const : Nat -> Term | app : List Term -> Term namespace Term instance : Inhabited Term := ⟨Term.const 0⟩ partial def hasToString : Term -> String | const n => "CONST(" ++ toString n ++ ")" | app ts => "APP" instance : ToString Term := ⟨hasToString⟩ end Term open Term structure MyState : Type := (ts : List Term) def emit (t : Term) : StateM MyState Unit := modify (Ξ» ms => ⟨t::ms.ts⟩) partial def foo : MyState -> Term -> Term -> List Term | msβ‚€, t, u => let stateT : StateM MyState Unit := do { match t with | const _ => pure () | app _ => emit (const 1); match t, u with | app _, app _ => emit (app []) | _, _ => pure () ; match t, u with | app _, app _ => emit (app []) | _, _ => emit (const 2) } ; (stateT.run ⟨[]⟩).2.ts.reverse def main : IO Unit := IO.println $ foo ⟨[]⟩ (app []) (app [])
b39e9608c0df8304876979f3548c2c95d2cdb930
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/linear_algebra/adic_completion.lean
e4cb3721b2a5d8d2d653f622ea304da76f203713
[]
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
11,751
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.linear_algebra.smodeq import Mathlib.ring_theory.ideal.operations import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # Completion of a module with respect to an ideal. In this file we define the notions of Hausdorff, precomplete, and complete for an `R`-module `M` with respect to an ideal `I`: ## Main definitions - `is_Hausdorff I M`: this says that the intersection of `I^n M` is `0`. - `is_precomplete I M`: this says that every Cauchy sequence converges. - `is_adic_complete I M`: this says that `M` is Hausdorff and precomplete. - `Hausdorffification I M`: this is the universal Hausdorff module with a map from `M`. - `completion I M`: if `I` is finitely generated, then this is the universal complete module (TODO) with a map from `M`. This map is injective iff `M` is Hausdorff and surjective iff `M` is precomplete. -/ /-- A module `M` is Hausdorff with respect to an ideal `I` if `β‹‚ I^n M = 0`. -/ def is_Hausdorff {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] := βˆ€ (x : M), (βˆ€ (n : β„•), smodeq (I ^ n β€’ ⊀) x 0) β†’ x = 0 /-- A module `M` is precomplete with respect to an ideal `I` if every Cauchy sequence converges. -/ def is_precomplete {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] := βˆ€ (f : β„• β†’ M), (βˆ€ {m n : β„•}, m ≀ n β†’ smodeq (I ^ m β€’ ⊀) (f m) (f n)) β†’ βˆƒ (L : M), βˆ€ (n : β„•), smodeq (I ^ n β€’ ⊀) (f n) L /-- A module `M` is `I`-adically complete if it is Hausdorff and precomplete. -/ def is_adic_complete {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] := is_Hausdorff I M ∧ is_precomplete I M /-- The Hausdorffification of a module with respect to an ideal. -/ def Hausdorffification {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] := submodule.quotient (infi fun (n : β„•) => I ^ n β€’ ⊀) /-- The completion of a module with respect to an ideal. This is not necessarily Hausdorff. In fact, this is only complete if the ideal is finitely generated. -/ def adic_completion {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : submodule R ((n : β„•) β†’ submodule.quotient (I ^ n β€’ ⊀)) := submodule.mk (set_of fun (f : (n : β„•) β†’ submodule.quotient (I ^ n β€’ ⊀)) => βˆ€ {m n : β„•}, m ≀ n β†’ coe_fn (submodule.liftq (I ^ n β€’ ⊀) (submodule.mkq (I ^ m β€’ ⊀)) sorry) (f n) = f m) sorry sorry sorry namespace is_Hausdorff protected instance bot {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] : is_Hausdorff βŠ₯ M := fun (x : M) (hx : βˆ€ (n : β„•), smodeq (βŠ₯ ^ n β€’ ⊀) x 0) => eq.mpr (id (Eq.refl (x = 0))) (eq.mp (Eq.trans ((fun (U U_1 : submodule R M) (e_1 : U = U_1) (x x_1 : M) (e_2 : x = x_1) (y y_1 : M) (e_3 : y = y_1) => congr (congr (congr_arg smodeq e_1) e_2) e_3) (βŠ₯ ^ 1 β€’ ⊀) βŠ₯ (Eq.trans ((fun (αΎ° αΎ°_1 : ideal R) (e_2 : αΎ° = αΎ°_1) (αΎ°_2 αΎ°_3 : submodule R M) (e_3 : αΎ°_2 = αΎ°_3) => congr (congr_arg has_scalar.smul e_2) e_3) (βŠ₯ ^ 1) βŠ₯ (pow_one βŠ₯) ⊀ ⊀ (Eq.refl ⊀)) (submodule.bot_smul ⊀)) x x (Eq.refl x) 0 0 (Eq.refl 0)) (propext smodeq.bot)) (hx 1)) protected theorem subsingleton {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] (h : is_Hausdorff ⊀ M) : subsingleton M := sorry protected instance of_subsingleton {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] [subsingleton M] : is_Hausdorff I M := fun (x : M) (_x : βˆ€ (n : β„•), smodeq (I ^ n β€’ ⊀) x 0) => subsingleton.elim x 0 theorem infi_pow_smul {R : Type u_1} [comm_ring R] {I : ideal R} {M : Type u_2} [add_comm_group M] [module R M] (h : is_Hausdorff I M) : (infi fun (n : β„•) => I ^ n β€’ ⊀) = βŠ₯ := sorry end is_Hausdorff namespace Hausdorffification /-- The canonical linear map to the Hausdorffification. -/ def of {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : linear_map R M (Hausdorffification I M) := submodule.mkq (infi fun (n : β„•) => I ^ n β€’ ⊀) theorem induction_on {R : Type u_1} [comm_ring R] {I : ideal R} {M : Type u_2} [add_comm_group M] [module R M] {C : Hausdorffification I M β†’ Prop} (x : Hausdorffification I M) (ih : βˆ€ (x : M), C (coe_fn (of I M) x)) : C x := quotient.induction_on' x ih protected instance is_Hausdorff {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : is_Hausdorff I (Hausdorffification I M) := sorry /-- universal property of Hausdorffification: any linear map to a Hausdorff module extends to a unique map from the Hausdorffification. -/ def lift {R : Type u_1} [comm_ring R] (I : ideal R) {M : Type u_2} [add_comm_group M] [module R M] {N : Type u_3} [add_comm_group N] [module R N] [h : is_Hausdorff I N] (f : linear_map R M N) : linear_map R (Hausdorffification I M) N := submodule.liftq (infi fun (n : β„•) => I ^ n β€’ ⊀) f sorry theorem lift_of {R : Type u_1} [comm_ring R] (I : ideal R) {M : Type u_2} [add_comm_group M] [module R M] {N : Type u_3} [add_comm_group N] [module R N] [h : is_Hausdorff I N] (f : linear_map R M N) (x : M) : coe_fn (lift I f) (coe_fn (of I M) x) = coe_fn f x := rfl theorem lift_comp_of {R : Type u_1} [comm_ring R] (I : ideal R) {M : Type u_2} [add_comm_group M] [module R M] {N : Type u_3} [add_comm_group N] [module R N] [h : is_Hausdorff I N] (f : linear_map R M N) : linear_map.comp (lift I f) (of I M) = f := linear_map.ext fun (_x : M) => rfl /-- Uniqueness of lift. -/ theorem lift_eq {R : Type u_1} [comm_ring R] (I : ideal R) {M : Type u_2} [add_comm_group M] [module R M] {N : Type u_3} [add_comm_group N] [module R N] [h : is_Hausdorff I N] (f : linear_map R M N) (g : linear_map R (Hausdorffification I M) N) (hg : linear_map.comp g (of I M) = f) : g = lift I f := sorry end Hausdorffification namespace is_precomplete protected instance bot {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] : is_precomplete βŠ₯ M := fun (f : β„• β†’ M) (hf : βˆ€ {m n : β„•}, m ≀ n β†’ smodeq (βŠ₯ ^ m β€’ ⊀) (f m) (f n)) => Exists.intro (f 1) fun (n : β„•) => nat.cases_on n (eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (βŠ₯ ^ 0 β€’ ⊀) (f 0) (f 1))) (pow_zero βŠ₯))) (eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (1 β€’ ⊀) (f 0) (f 1))) ideal.one_eq_top)) (eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (⊀ β€’ ⊀) (f 0) (f 1))) (submodule.top_smul ⊀))) smodeq.top))) fun (n : β„•) => eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (βŠ₯ ^ Nat.succ n β€’ ⊀) (f (Nat.succ n)) (f 1))) (eq.mp (Eq._oldrec (Eq.refl (smodeq βŠ₯ (f 1) (f (n + 1)))) (propext smodeq.bot)) (eq.mp (Eq._oldrec (Eq.refl (smodeq (βŠ₯ β€’ ⊀) (f 1) (f (n + 1)))) (submodule.bot_smul ⊀)) (eq.mp (Eq._oldrec (Eq.refl (smodeq (βŠ₯ ^ 1 β€’ ⊀) (f 1) (f (n + 1)))) (pow_one βŠ₯)) (hf (nat.le_add_left 1 n))))))) smodeq.refl protected instance top {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] : is_precomplete ⊀ M := fun (f : β„• β†’ M) (hf : βˆ€ {m n : β„•}, m ≀ n β†’ smodeq (⊀ ^ m β€’ ⊀) (f m) (f n)) => Exists.intro 0 fun (n : β„•) => eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (⊀ ^ n β€’ ⊀) (f n) 0)) (ideal.top_pow R n))) (eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (⊀ β€’ ⊀) (f n) 0)) (submodule.top_smul ⊀))) smodeq.top) protected instance of_subsingleton {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] [subsingleton M] : is_precomplete I M := fun (f : β„• β†’ M) (hf : βˆ€ {m n : β„•}, m ≀ n β†’ smodeq (I ^ m β€’ ⊀) (f m) (f n)) => Exists.intro 0 fun (n : β„•) => eq.mpr (id (Eq._oldrec (Eq.refl (smodeq (I ^ n β€’ ⊀) (f n) 0)) (subsingleton.elim (f n) 0))) smodeq.refl end is_precomplete namespace adic_completion /-- The canonical linear map to the completion. -/ def of {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : linear_map R M β†₯(adic_completion I M) := linear_map.mk (fun (x : M) => { val := fun (n : β„•) => coe_fn (submodule.mkq (I ^ n β€’ ⊀)) x, property := sorry }) sorry sorry @[simp] theorem of_apply {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (x : M) (n : β„•) : subtype.val (coe_fn (of I M) x) n = coe_fn (submodule.mkq (I ^ n β€’ ⊀)) x := rfl /-- Linearly evaluating a sequence in the completion at a given input. -/ def eval {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β„•) : linear_map R (β†₯(adic_completion I M)) (submodule.quotient (I ^ n β€’ ⊀)) := linear_map.mk (fun (f : β†₯(adic_completion I M)) => subtype.val f n) sorry sorry @[simp] theorem coe_eval {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β„•) : ⇑(eval I M n) = fun (f : β†₯(adic_completion I M)) => subtype.val f n := rfl theorem eval_apply {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β„•) (f : β†₯(adic_completion I M)) : coe_fn (eval I M n) f = subtype.val f n := rfl theorem eval_of {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β„•) (x : M) : coe_fn (eval I M n) (coe_fn (of I M) x) = coe_fn (submodule.mkq (I ^ n β€’ ⊀)) x := rfl @[simp] theorem eval_comp_of {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β„•) : linear_map.comp (eval I M n) (of I M) = submodule.mkq (I ^ n β€’ ⊀) := rfl @[simp] theorem range_eval {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] (n : β„•) : linear_map.range (eval I M n) = ⊀ := iff.mpr linear_map.range_eq_top fun (x : submodule.quotient (I ^ n β€’ ⊀)) => quotient.induction_on' x fun (x : M) => Exists.intro (coe_fn (of I M) x) rfl theorem ext {R : Type u_1} [comm_ring R] {I : ideal R} {M : Type u_2} [add_comm_group M] [module R M] {x : β†₯(adic_completion I M)} {y : β†₯(adic_completion I M)} (h : βˆ€ (n : β„•), coe_fn (eval I M n) x = coe_fn (eval I M n) y) : x = y := subtype.eq (funext h) protected instance is_Hausdorff {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] : is_Hausdorff I β†₯(adic_completion I M) := sorry end adic_completion namespace is_adic_complete protected instance bot {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] : is_adic_complete βŠ₯ M := { left := is_Hausdorff.bot M, right := is_precomplete.bot M } protected theorem subsingleton {R : Type u_1} [comm_ring R] (M : Type u_2) [add_comm_group M] [module R M] (h : is_adic_complete ⊀ M) : subsingleton M := is_Hausdorff.subsingleton (and.left h) protected instance of_subsingleton {R : Type u_1} [comm_ring R] (I : ideal R) (M : Type u_2) [add_comm_group M] [module R M] [subsingleton M] : is_adic_complete I M := { left := is_Hausdorff.of_subsingleton I M, right := is_precomplete.of_subsingleton I M }
f6bf4b00dcbdab31df3aee4fbd731920f5d5a5fe
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/data/finset/basic.lean
0b70c1aee9cbdfbdaa345c4f72f491910151e86a
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
104,999
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro -/ import data.multiset.finset_ops import tactic.monotonicity import tactic.apply import tactic.nth_rewrite /-! # Finite sets mathlib has several different models for finite sets, and it can be confusing when you're first getting used to them! This file builds the basic theory of `finset Ξ±`, modelled as a `multiset Ξ±` without duplicates. It's "constructive" in the since that there is an underlying list of elements, although this is wrapped in a quotient by permutations, so anytime you actually use this list you're obligated to show you didn't depend on the ordering. There's also the typeclass `fintype Ξ±` (which asserts that there is some `finset Ξ±` containing every term of type `Ξ±`) as well as the predicate `finite` on `s : set Ξ±` (which asserts `nonempty (fintype s)`). -/ open multiset subtype nat function variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} /-- `finset Ξ±` is the type of finite sets of elements of `Ξ±`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (Ξ± : Type*) := (val : multiset Ξ±) (nodup : nodup val) namespace finset theorem eq_of_veq : βˆ€ {s t : finset Ξ±}, s.1 = t.1 β†’ s = t | ⟨s, _⟩ ⟨t, _⟩ rfl := rfl @[simp] theorem val_inj {s t : finset Ξ±} : s.1 = t.1 ↔ s = t := ⟨eq_of_veq, congr_arg _⟩ @[simp] theorem erase_dup_eq_self [decidable_eq Ξ±] (s : finset Ξ±) : erase_dup s.1 = s.1 := erase_dup_eq_self.2 s.2 instance has_decidable_eq [decidable_eq Ξ±] : decidable_eq (finset Ξ±) | s₁ sβ‚‚ := decidable_of_iff _ val_inj /-! ### membership -/ instance : has_mem Ξ± (finset Ξ±) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : Ξ±} {s : finset Ξ±} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] theorem mem_mk {a : Ξ±} {s nd} : a ∈ @finset.mk Ξ± s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) : decidable (a ∈ s) := multiset.decidable_mem _ _ /-! ### set coercion -/ /-- Convert a finset to a set in the natural way. -/ instance : has_coe_t (finset Ξ±) (set Ξ±) := ⟨λ s, {x | x ∈ s}⟩ @[simp, norm_cast] lemma mem_coe {a : Ξ±} {s : finset Ξ±} : a ∈ (s : set Ξ±) ↔ a ∈ s := iff.rfl @[simp] lemma set_of_mem {Ξ±} {s : finset Ξ±} : {a | a ∈ s} = s := rfl @[simp] lemma coe_mem {s : finset Ξ±} (x : (s : set Ξ±)) : ↑x ∈ s := x.2 @[simp] lemma mk_coe {s : finset Ξ±} (x : (s : set Ξ±)) {h} : (⟨x, h⟩ : (s : set Ξ±)) = x := subtype.coe_eta _ _ instance decidable_mem' [decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) : decidable (a ∈ (s : set Ξ±)) := s.decidable_mem _ /-! ### extensionality -/ theorem ext_iff {s₁ sβ‚‚ : finset Ξ±} : s₁ = sβ‚‚ ↔ βˆ€ a, a ∈ s₁ ↔ a ∈ sβ‚‚ := val_inj.symm.trans $ nodup_ext s₁.2 sβ‚‚.2 @[ext] theorem ext {s₁ sβ‚‚ : finset Ξ±} : (βˆ€ a, a ∈ s₁ ↔ a ∈ sβ‚‚) β†’ s₁ = sβ‚‚ := ext_iff.2 @[simp, norm_cast] theorem coe_inj {s₁ sβ‚‚ : finset Ξ±} : (s₁ : set Ξ±) = sβ‚‚ ↔ s₁ = sβ‚‚ := set.ext_iff.trans ext_iff.symm lemma coe_injective {Ξ±} : injective (coe : finset Ξ± β†’ set Ξ±) := Ξ» s t, coe_inj.1 /-! ### subset -/ instance : has_subset (finset Ξ±) := ⟨λ s₁ sβ‚‚, βˆ€ ⦃a⦄, a ∈ s₁ β†’ a ∈ sβ‚‚βŸ© theorem subset_def {s₁ sβ‚‚ : finset Ξ±} : s₁ βŠ† sβ‚‚ ↔ s₁.1 βŠ† sβ‚‚.1 := iff.rfl @[simp] theorem subset.refl (s : finset Ξ±) : s βŠ† s := subset.refl _ theorem subset_of_eq {s t : finset Ξ±} (h : s = t) : s βŠ† t := h β–Έ subset.refl _ theorem subset.trans {s₁ sβ‚‚ s₃ : finset Ξ±} : s₁ βŠ† sβ‚‚ β†’ sβ‚‚ βŠ† s₃ β†’ s₁ βŠ† s₃ := subset.trans theorem superset.trans {s₁ sβ‚‚ s₃ : finset Ξ±} : s₁ βŠ‡ sβ‚‚ β†’ sβ‚‚ βŠ‡ s₃ β†’ s₁ βŠ‡ s₃ := Ξ» h' h, subset.trans h h' -- TODO: these should be global attributes, but this will require fixing other files local attribute [trans] subset.trans superset.trans theorem mem_of_subset {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} : s₁ βŠ† sβ‚‚ β†’ a ∈ s₁ β†’ a ∈ sβ‚‚ := mem_of_subset theorem subset.antisymm {s₁ sβ‚‚ : finset Ξ±} (H₁ : s₁ βŠ† sβ‚‚) (Hβ‚‚ : sβ‚‚ βŠ† s₁) : s₁ = sβ‚‚ := ext $ Ξ» a, ⟨@H₁ a, @Hβ‚‚ a⟩ theorem subset_iff {s₁ sβ‚‚ : finset Ξ±} : s₁ βŠ† sβ‚‚ ↔ βˆ€ ⦃x⦄, x ∈ s₁ β†’ x ∈ sβ‚‚ := iff.rfl @[simp, norm_cast] theorem coe_subset {s₁ sβ‚‚ : finset Ξ±} : (s₁ : set Ξ±) βŠ† sβ‚‚ ↔ s₁ βŠ† sβ‚‚ := iff.rfl @[simp] theorem val_le_iff {s₁ sβ‚‚ : finset Ξ±} : s₁.1 ≀ sβ‚‚.1 ↔ s₁ βŠ† sβ‚‚ := le_iff_subset s₁.2 instance : has_ssubset (finset Ξ±) := ⟨λa b, a βŠ† b ∧ Β¬ b βŠ† a⟩ instance : partial_order (finset Ξ±) := { le := (βŠ†), lt := (βŠ‚), le_refl := subset.refl, le_trans := @subset.trans _, le_antisymm := @subset.antisymm _ } theorem subset.antisymm_iff {s₁ sβ‚‚ : finset Ξ±} : s₁ = sβ‚‚ ↔ s₁ βŠ† sβ‚‚ ∧ sβ‚‚ βŠ† s₁ := le_antisymm_iff @[simp] theorem le_iff_subset {s₁ sβ‚‚ : finset Ξ±} : s₁ ≀ sβ‚‚ ↔ s₁ βŠ† sβ‚‚ := iff.rfl @[simp] theorem lt_iff_ssubset {s₁ sβ‚‚ : finset Ξ±} : s₁ < sβ‚‚ ↔ s₁ βŠ‚ sβ‚‚ := iff.rfl @[simp, norm_cast] lemma coe_ssubset {s₁ sβ‚‚ : finset Ξ±} : (s₁ : set Ξ±) βŠ‚ sβ‚‚ ↔ s₁ βŠ‚ sβ‚‚ := show (s₁ : set Ξ±) βŠ‚ sβ‚‚ ↔ s₁ βŠ† sβ‚‚ ∧ Β¬sβ‚‚ βŠ† s₁, by simp only [set.ssubset_def, finset.coe_subset] @[simp] theorem val_lt_iff {s₁ sβ‚‚ : finset Ξ±} : s₁.1 < sβ‚‚.1 ↔ s₁ βŠ‚ sβ‚‚ := and_congr val_le_iff $ not_congr val_le_iff theorem ssubset_iff_of_subset {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : s₁ βŠ‚ sβ‚‚ ↔ βˆƒ x ∈ sβ‚‚, x βˆ‰ s₁ := set.ssubset_iff_of_subset h /-! ### Nonempty -/ /-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used in theorem assumptions instead of `βˆƒ x, x ∈ s` or `s β‰  βˆ…` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : finset Ξ±) : Prop := βˆƒ x:Ξ±, x ∈ s @[simp, norm_cast] lemma coe_nonempty {s : finset Ξ±} : (s:set Ξ±).nonempty ↔ s.nonempty := iff.rfl lemma nonempty.bex {s : finset Ξ±} (h : s.nonempty) : βˆƒ x:Ξ±, x ∈ s := h lemma nonempty.mono {s t : finset Ξ±} (hst : s βŠ† t) (hs : s.nonempty) : t.nonempty := set.nonempty.mono hst hs lemma nonempty.forall_const {s : finset Ξ±} (h : s.nonempty) {p : Prop} : (βˆ€ x ∈ s, p) ↔ p := let ⟨x, hx⟩ := h in ⟨λ h, h x hx, Ξ» h x hx, h⟩ /-! ### empty -/ /-- The empty finset -/ protected def empty : finset Ξ± := ⟨0, nodup_zero⟩ instance : has_emptyc (finset Ξ±) := ⟨finset.empty⟩ instance : inhabited (finset Ξ±) := βŸ¨βˆ…βŸ© @[simp] theorem empty_val : (βˆ… : finset Ξ±).1 = 0 := rfl @[simp] theorem not_mem_empty (a : Ξ±) : a βˆ‰ (βˆ… : finset Ξ±) := id @[simp] theorem not_nonempty_empty : Β¬(βˆ… : finset Ξ±).nonempty := Ξ» ⟨x, hx⟩, not_mem_empty x hx @[simp] theorem mk_zero : (⟨0, nodup_zero⟩ : finset Ξ±) = βˆ… := rfl theorem ne_empty_of_mem {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : s β‰  βˆ… := Ξ» e, not_mem_empty a $ e β–Έ h theorem nonempty.ne_empty {s : finset Ξ±} (h : s.nonempty) : s β‰  βˆ… := exists.elim h $ Ξ» a, ne_empty_of_mem @[simp] theorem empty_subset (s : finset Ξ±) : βˆ… βŠ† s := zero_subset _ theorem eq_empty_of_forall_not_mem {s : finset Ξ±} (H : βˆ€x, x βˆ‰ s) : s = βˆ… := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset Ξ±} : s = βˆ… ↔ βˆ€ x, x βˆ‰ s := ⟨by rintro rfl x; exact id, Ξ» h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset Ξ±} : s.1 = 0 ↔ s = βˆ… := @val_inj _ s βˆ… theorem subset_empty {s : finset Ξ±} : s βŠ† βˆ… ↔ s = βˆ… := subset_zero.trans val_eq_zero theorem nonempty_of_ne_empty {s : finset Ξ±} (h : s β‰  βˆ…) : s.nonempty := exists_mem_of_ne_zero (mt val_eq_zero.1 h) theorem nonempty_iff_ne_empty {s : finset Ξ±} : s.nonempty ↔ s β‰  βˆ… := ⟨nonempty.ne_empty, nonempty_of_ne_empty⟩ @[simp] theorem not_nonempty_iff_eq_empty {s : finset Ξ±} : Β¬s.nonempty ↔ s = βˆ… := by { rw nonempty_iff_ne_empty, exact not_not, } theorem eq_empty_or_nonempty (s : finset Ξ±) : s = βˆ… ∨ s.nonempty := classical.by_cases or.inl (Ξ» h, or.inr (nonempty_of_ne_empty h)) @[simp] lemma coe_empty : ((βˆ… : finset Ξ±) : set Ξ±) = βˆ… := rfl /-- A `finset` for an empty type is empty. -/ lemma eq_empty_of_not_nonempty (h : Β¬ nonempty Ξ±) (s : finset Ξ±) : s = βˆ… := finset.eq_empty_of_forall_not_mem $ Ξ» x, false.elim $ not_nonempty_iff_imp_false.1 h x /-! ### singleton -/ /-- `{a} : finset a` is the set `{a}` containing `a` and nothing else. This differs from `insert a βˆ…` in that it does not require a `decidable_eq` instance for `Ξ±`. -/ instance : has_singleton Ξ± (finset Ξ±) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩ @[simp] theorem singleton_val (a : Ξ±) : ({a} : finset Ξ±).1 = a ::β‚˜ 0 := rfl @[simp] theorem mem_singleton {a b : Ξ±} : b ∈ ({a} : finset Ξ±) ↔ b = a := mem_singleton theorem not_mem_singleton {a b : Ξ±} : a βˆ‰ ({b} : finset Ξ±) ↔ a β‰  b := not_congr mem_singleton theorem mem_singleton_self (a : Ξ±) : a ∈ ({a} : finset Ξ±) := or.inl rfl theorem singleton_inj {a b : Ξ±} : ({a} : finset Ξ±) = {b} ↔ a = b := ⟨λ h, mem_singleton.1 (h β–Έ mem_singleton_self _), congr_arg _⟩ @[simp] theorem singleton_nonempty (a : Ξ±) : ({a} : finset Ξ±).nonempty := ⟨a, mem_singleton_self a⟩ @[simp] theorem singleton_ne_empty (a : Ξ±) : ({a} : finset Ξ±) β‰  βˆ… := (singleton_nonempty a).ne_empty @[simp, norm_cast] lemma coe_singleton (a : Ξ±) : (({a} : finset Ξ±) : set Ξ±) = {a} := by { ext, simp } lemma eq_singleton_iff_unique_mem {s : finset Ξ±} {a : Ξ±} : s = {a} ↔ a ∈ s ∧ βˆ€ x ∈ s, x = a := begin split; intro t, rw t, refine ⟨finset.mem_singleton_self _, Ξ» _, finset.mem_singleton.1⟩, ext, rw finset.mem_singleton, refine ⟨t.right _, Ξ» r, r.symm β–Έ t.left⟩ end lemma eq_singleton_iff_nonempty_unique_mem {s : finset Ξ±} {a : Ξ±} : s = {a} ↔ s.nonempty ∧ βˆ€ x ∈ s, x = a := begin split, { intros h, subst h, simp, }, { rintros ⟨hne, h_uniq⟩, rw eq_singleton_iff_unique_mem, refine ⟨_, h_uniq⟩, rw ← h_uniq hne.some hne.some_spec, apply hne.some_spec, }, end lemma singleton_iff_unique_mem (s : finset Ξ±) : (βˆƒ a, s = {a}) ↔ βˆƒ! a, a ∈ s := by simp only [eq_singleton_iff_unique_mem, exists_unique] lemma singleton_subset_set_iff {s : set Ξ±} {a : Ξ±} : ↑({a} : finset Ξ±) βŠ† s ↔ a ∈ s := by rw [coe_singleton, set.singleton_subset_iff] @[simp] lemma singleton_subset_iff {s : finset Ξ±} {a : Ξ±} : {a} βŠ† s ↔ a ∈ s := singleton_subset_set_iff /-! ### cons -/ /-- `cons a s h` is the set `{a} βˆͺ s` containing `a` and the elements of `s`. It is the same as `insert a s` when it is defined, but unlike `insert a s` it does not require `decidable_eq Ξ±`, and the union is guaranteed to be disjoint. -/ def cons {Ξ±} (a : Ξ±) (s : finset Ξ±) (h : a βˆ‰ s) : finset Ξ± := ⟨a ::β‚˜ s.1, multiset.nodup_cons.2 ⟨h, s.2⟩⟩ @[simp] theorem mem_cons {Ξ± a s h b} : b ∈ @cons Ξ± a s h ↔ b = a ∨ b ∈ s := by rcases s with ⟨⟨s⟩⟩; apply list.mem_cons_iff @[simp] theorem cons_val {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : (cons a s h).1 = a ::β‚˜ s.1 := rfl @[simp] theorem mk_cons {a : Ξ±} {s : multiset Ξ±} (h : (a ::β‚˜ s).nodup) : (⟨a ::β‚˜ s, h⟩ : finset Ξ±) = cons a ⟨s, (multiset.nodup_cons.1 h).2⟩ (multiset.nodup_cons.1 h).1 := rfl @[simp] theorem nonempty_cons {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : (cons a s h).nonempty := ⟨a, mem_cons.2 (or.inl rfl)⟩ @[simp] lemma nonempty_mk_coe : βˆ€ {l : list Ξ±} {hl}, (βŸ¨β†‘l, hl⟩ : finset Ξ±).nonempty ↔ l β‰  [] | [] hl := by simp | (a::l) hl := by simp [← multiset.cons_coe] /-! ### disjoint union -/ /-- `disj_union s t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`. It is the same as `s βˆͺ t`, but it does not require decidable equality on the type. The hypothesis ensures that the sets are disjoint. -/ def disj_union {Ξ±} (s t : finset Ξ±) (h : βˆ€ a ∈ s, a βˆ‰ t) : finset Ξ± := ⟨s.1 + t.1, multiset.nodup_add.2 ⟨s.2, t.2, h⟩⟩ @[simp] theorem mem_disj_union {Ξ± s t h a} : a ∈ @disj_union Ξ± s t h ↔ a ∈ s ∨ a ∈ t := by rcases s with ⟨⟨s⟩⟩; rcases t with ⟨⟨t⟩⟩; apply list.mem_append /-! ### insert -/ section decidable_eq variables [decidable_eq Ξ±] /-- `insert a s` is the set `{a} βˆͺ s` containing `a` and the elements of `s`. -/ instance : has_insert Ξ± (finset Ξ±) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩ theorem insert_def (a : Ξ±) (s : finset Ξ±) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl @[simp] theorem insert_val (a : Ξ±) (s : finset Ξ±) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : Ξ±) (s : finset Ξ±) : (insert a s).1 = erase_dup (a ::β‚˜ s.1) := by rw [erase_dup_cons, erase_dup_eq_self]; refl theorem insert_val_of_not_mem {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : (insert a s).1 = a ::β‚˜ s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] theorem mem_insert {a b : Ξ±} {s : finset Ξ±} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : Ξ±) (s : finset Ξ±) : a ∈ insert a s := mem_ndinsert_self a s.1 theorem mem_insert_of_mem {a b : Ξ±} {s : finset Ξ±} (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h theorem mem_of_mem_insert_of_ne {a b : Ξ±} {s : finset Ξ±} (h : b ∈ insert a s) : b β‰  a β†’ b ∈ s := (mem_insert.1 h).resolve_left @[simp] theorem cons_eq_insert {Ξ±} [decidable_eq Ξ±] (a s h) : @cons Ξ± a s h = insert a s := ext $ Ξ» a, by simp @[simp, norm_cast] lemma coe_insert (a : Ξ±) (s : finset Ξ±) : ↑(insert a s) = (insert a s : set Ξ±) := set.ext $ Ξ» x, by simp only [mem_coe, mem_insert, set.mem_insert_iff] lemma mem_insert_coe {s : finset Ξ±} {x y : Ξ±} : x ∈ insert y s ↔ x ∈ insert y (s : set Ξ±) := by simp instance : is_lawful_singleton Ξ± (finset Ξ±) := ⟨λ a, by { ext, simp }⟩ @[simp] theorem insert_eq_of_mem {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] theorem insert_singleton_self_eq (a : Ξ±) : ({a, a} : finset Ξ±) = {a} := insert_eq_of_mem $ mem_singleton_self _ theorem insert.comm (a b : Ξ±) (s : finset Ξ±) : insert a (insert b s) = insert b (insert a s) := ext $ Ξ» x, by simp only [mem_insert, or.left_comm] theorem insert_singleton_comm (a b : Ξ±) : ({a, b} : finset Ξ±) = {b, a} := begin ext, simp [or.comm] end @[simp] theorem insert_idem (a : Ξ±) (s : finset Ξ±) : insert a (insert a s) = insert a s := ext $ Ξ» x, by simp only [mem_insert, or.assoc.symm, or_self] @[simp] theorem insert_nonempty (a : Ξ±) (s : finset Ξ±) : (insert a s).nonempty := ⟨a, mem_insert_self a s⟩ @[simp] theorem insert_ne_empty (a : Ξ±) (s : finset Ξ±) : insert a s β‰  βˆ… := (insert_nonempty a s).ne_empty section universe u /-! The universe annotation is required for the following instance, possibly this is a bug in Lean. See leanprover.zulipchat.com/#narrow/stream/113488-general/topic/strange.20error.20(universe.20issue.3F) -/ instance {Ξ± : Type u} [decidable_eq Ξ±] (i : Ξ±) (s : finset Ξ±) : nonempty.{u + 1} ((insert i s : finset Ξ±) : set Ξ±) := (finset.coe_nonempty.mpr (s.insert_nonempty i)).to_subtype end lemma ne_insert_of_not_mem (s t : finset Ξ±) {a : Ξ±} (h : a βˆ‰ s) : s β‰  insert a t := by { contrapose! h, simp [h] } theorem insert_subset {a : Ξ±} {s t : finset Ξ±} : insert a s βŠ† t ↔ a ∈ t ∧ s βŠ† t := by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib] theorem subset_insert (a : Ξ±) (s : finset Ξ±) : s βŠ† insert a s := Ξ» b, mem_insert_of_mem theorem insert_subset_insert (a : Ξ±) {s t : finset Ξ±} (h : s βŠ† t) : insert a s βŠ† insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma ssubset_iff {s t : finset Ξ±} : s βŠ‚ t ↔ (βˆƒa βˆ‰ s, insert a s βŠ† t) := by exact_mod_cast @set.ssubset_iff_insert Ξ± s t lemma ssubset_insert {s : finset Ξ±} {a : Ξ±} (h : a βˆ‰ s) : s βŠ‚ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[elab_as_eliminator] protected theorem induction {Ξ± : Type*} {p : finset Ξ± β†’ Prop} [decidable_eq Ξ±] (h₁ : p βˆ…) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : finset Ξ±}, a βˆ‰ s β†’ p s β†’ p (insert a s)) : βˆ€ s, p s | ⟨s, nd⟩ := multiset.induction_on s (Ξ» _, h₁) (Ξ» a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a ::β‚˜ s, nd⟩)], { exact hβ‚‚ (by exact m) (IH nd') }, { rw [insert_val, ndinsert_of_not_mem m] } end) nd /-- To prove a proposition about an arbitrary `finset Ξ±`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset Ξ±`, then it holds for the `finset` obtained by inserting a new element. -/ @[elab_as_eliminator] protected theorem induction_on {Ξ± : Type*} {p : finset Ξ± β†’ Prop} [decidable_eq Ξ±] (s : finset Ξ±) (h₁ : p βˆ…) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : finset Ξ±}, a βˆ‰ s β†’ p s β†’ p (insert a s)) : p s := finset.induction h₁ hβ‚‚ s /-- To prove a proposition about `S : finset Ξ±`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset Ξ± βŠ† S`, then it holds for the `finset` obtained by inserting a new element of `S`. -/ @[elab_as_eliminator] theorem induction_on' {Ξ± : Type*} {p : finset Ξ± β†’ Prop} [decidable_eq Ξ±] (S : finset Ξ±) (h₁ : p βˆ…) (hβ‚‚ : βˆ€ {a s}, a ∈ S β†’ s βŠ† S β†’ a βˆ‰ s β†’ p s β†’ p (insert a s)) : p S := @finset.induction_on Ξ± (Ξ» T, T βŠ† S β†’ p T) _ S (Ξ» _, h₁) (Ξ» a s has hqs hs, let ⟨hS, sS⟩ := finset.insert_subset.1 hs in hβ‚‚ hS sS has (hqs sS)) (finset.subset.refl S) /-- Inserting an element to a finite set is equivalent to the option type. -/ def subtype_insert_equiv_option {t : finset Ξ±} {x : Ξ±} (h : x βˆ‰ t) : {i // i ∈ insert x t} ≃ option {i // i ∈ t} := begin refine { to_fun := Ξ» y, if h : ↑y = x then none else some ⟨y, (mem_insert.mp y.2).resolve_left h⟩, inv_fun := Ξ» y, y.elim ⟨x, mem_insert_self _ _⟩ $ Ξ» z, ⟨z, mem_insert_of_mem z.2⟩, .. }, { intro y, by_cases h : ↑y = x, simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk], simp only [h, option.elim, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, { rintro (_|y), simp only [option.elim, dif_pos, subtype.coe_mk], have : ↑y β‰  x, { rintro ⟨⟩, exact h y.2 }, simp only [this, option.elim, subtype.eta, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, end /-! ### union -/ /-- `s βˆͺ t` is the set such that `a ∈ s βˆͺ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset Ξ±) := ⟨λ s₁ sβ‚‚, ⟨_, nodup_ndunion s₁.1 sβ‚‚.2⟩⟩ theorem union_val_nd (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).1 = ndunion s₁.1 sβ‚‚.1 := rfl @[simp] theorem union_val (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).1 = s₁.1 βˆͺ sβ‚‚.1 := ndunion_eq_union s₁.2 @[simp] theorem mem_union {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a ∈ s₁ βˆͺ sβ‚‚ ↔ a ∈ s₁ ∨ a ∈ sβ‚‚ := mem_ndunion @[simp] theorem disj_union_eq_union {Ξ±} [decidable_eq Ξ±] (s t h) : @disj_union Ξ± s t h = s βˆͺ t := ext $ Ξ» a, by simp theorem mem_union_left {a : Ξ±} {s₁ : finset Ξ±} (sβ‚‚ : finset Ξ±) (h : a ∈ s₁) : a ∈ s₁ βˆͺ sβ‚‚ := mem_union.2 $ or.inl h theorem mem_union_right {a : Ξ±} {sβ‚‚ : finset Ξ±} (s₁ : finset Ξ±) (h : a ∈ sβ‚‚) : a ∈ s₁ βˆͺ sβ‚‚ := mem_union.2 $ or.inr h theorem forall_mem_union {s₁ sβ‚‚ : finset Ξ±} {p : Ξ± β†’ Prop} : (βˆ€ ab ∈ (s₁ βˆͺ sβ‚‚), p ab) ↔ (βˆ€ a ∈ s₁, p a) ∧ (βˆ€ b ∈ sβ‚‚, p b) := ⟨λ h, ⟨λ a, h a ∘ mem_union_left _, Ξ» b, h b ∘ mem_union_right _⟩, Ξ» h ab hab, (mem_union.mp hab).elim (h.1 _) (h.2 _)⟩ theorem not_mem_union {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a βˆ‰ s₁ βˆͺ sβ‚‚ ↔ a βˆ‰ s₁ ∧ a βˆ‰ sβ‚‚ := by rw [mem_union, not_or_distrib] @[simp, norm_cast] lemma coe_union (s₁ sβ‚‚ : finset Ξ±) : ↑(s₁ βˆͺ sβ‚‚) = (s₁ βˆͺ sβ‚‚ : set Ξ±) := set.ext $ Ξ» x, mem_union theorem union_subset {s₁ sβ‚‚ s₃ : finset Ξ±} (h₁ : s₁ βŠ† s₃) (hβ‚‚ : sβ‚‚ βŠ† s₃) : s₁ βˆͺ sβ‚‚ βŠ† s₃ := val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 hβ‚‚βŸ©) theorem subset_union_left (s₁ sβ‚‚ : finset Ξ±) : s₁ βŠ† s₁ βˆͺ sβ‚‚ := Ξ» x, mem_union_left _ theorem subset_union_right (s₁ sβ‚‚ : finset Ξ±) : sβ‚‚ βŠ† s₁ βˆͺ sβ‚‚ := Ξ» x, mem_union_right _ lemma union_subset_union {s1 t1 s2 t2 : finset Ξ±} (h1 : s1 βŠ† t1) (h2 : s2 βŠ† t2) : s1 βˆͺ s2 βŠ† t1 βˆͺ t2 := by { intros x hx, rw finset.mem_union at hx ⊒, tauto } theorem union_comm (s₁ sβ‚‚ : finset Ξ±) : s₁ βˆͺ sβ‚‚ = sβ‚‚ βˆͺ s₁ := ext $ Ξ» x, by simp only [mem_union, or_comm] instance : is_commutative (finset Ξ±) (βˆͺ) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = s₁ βˆͺ (sβ‚‚ βˆͺ s₃) := ext $ Ξ» x, by simp only [mem_union, or_assoc] instance : is_associative (finset Ξ±) (βˆͺ) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset Ξ±) : s βˆͺ s = s := ext $ Ξ» _, mem_union.trans $ or_self _ instance : is_idempotent (finset Ξ±) (βˆͺ) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : s₁ βˆͺ (sβ‚‚ βˆͺ s₃) = sβ‚‚ βˆͺ (s₁ βˆͺ s₃) := ext $ Ξ» _, by simp only [mem_union, or.left_comm] theorem union_right_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ βˆͺ sβ‚‚) βˆͺ s₃ = (s₁ βˆͺ s₃) βˆͺ sβ‚‚ := ext $ Ξ» x, by simp only [mem_union, or_assoc, or_comm (x ∈ sβ‚‚)] theorem union_self (s : finset Ξ±) : s βˆͺ s = s := union_idempotent s @[simp] theorem union_empty (s : finset Ξ±) : s βˆͺ βˆ… = s := ext $ Ξ» x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset Ξ±) : βˆ… βˆͺ s = s := ext $ Ξ» x, mem_union.trans $ false_or _ theorem insert_eq (a : Ξ±) (s : finset Ξ±) : insert a s = {a} βˆͺ s := rfl @[simp] theorem insert_union (a : Ξ±) (s t : finset Ξ±) : insert a s βˆͺ t = insert a (s βˆͺ t) := by simp only [insert_eq, union_assoc] @[simp] theorem union_insert (a : Ξ±) (s t : finset Ξ±) : s βˆͺ insert a t = insert a (s βˆͺ t) := by simp only [insert_eq, union_left_comm] theorem insert_union_distrib (a : Ξ±) (s t : finset Ξ±) : insert a (s βˆͺ t) = insert a s βˆͺ insert a t := by simp only [insert_union, union_insert, insert_idem] @[simp] lemma union_eq_left_iff_subset {s t : finset Ξ±} : s βˆͺ t = s ↔ t βŠ† s := begin split, { assume h, have : t βŠ† s βˆͺ t := subset_union_right _ _, rwa h at this }, { assume h, exact subset.antisymm (union_subset (subset.refl _) h) (subset_union_left _ _) } end @[simp] lemma left_eq_union_iff_subset {s t : finset Ξ±} : s = s βˆͺ t ↔ t βŠ† s := by rw [← union_eq_left_iff_subset, eq_comm] @[simp] lemma union_eq_right_iff_subset {s t : finset Ξ±} : t βˆͺ s = s ↔ t βŠ† s := by rw [union_comm, union_eq_left_iff_subset] @[simp] lemma right_eq_union_iff_subset {s t : finset Ξ±} : s = t βˆͺ s ↔ t βŠ† s := by rw [← union_eq_right_iff_subset, eq_comm] /-- To prove a relation on pairs of `finset X`, it suffices to show that it is * symmetric, * it holds when one of the `finset`s is empty, * it holds for pairs of singletons, * if it holds for `[a, c]` and for `[b, c]`, then it holds for `[a βˆͺ b, c]`. -/ lemma induction_on_union (P : finset Ξ± β†’ finset Ξ± β†’ Prop) (symm : βˆ€ {a b}, P a b β†’ P b a) (empty_right : βˆ€ {a}, P a βˆ…) (singletons : βˆ€ {a b}, P {a} {b}) (union_of : βˆ€ {a b c}, P a c β†’ P b c β†’ P (a βˆͺ b) c) : βˆ€ a b, P a b := begin intros a b, refine finset.induction_on b empty_right (Ξ» x s xs hi, symm _), rw finset.insert_eq, apply union_of _ (symm hi), refine finset.induction_on a empty_right (Ξ» a t ta hi, symm _), rw finset.insert_eq, exact union_of singletons (symm hi), end /-! ### inter -/ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset Ξ±) := ⟨λ s₁ sβ‚‚, ⟨_, nodup_ndinter sβ‚‚.1 s₁.2⟩⟩ theorem inter_val_nd (s₁ sβ‚‚ : finset Ξ±) : (s₁ ∩ sβ‚‚).1 = ndinter s₁.1 sβ‚‚.1 := rfl @[simp] theorem inter_val (s₁ sβ‚‚ : finset Ξ±) : (s₁ ∩ sβ‚‚).1 = s₁.1 ∩ sβ‚‚.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a ∈ s₁ ∩ sβ‚‚ ↔ a ∈ s₁ ∧ a ∈ sβ‚‚ := mem_ndinter theorem mem_of_mem_inter_left {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} (h : a ∈ s₁ ∩ sβ‚‚) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} (h : a ∈ s₁ ∩ sβ‚‚) : a ∈ sβ‚‚ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a ∈ s₁ β†’ a ∈ sβ‚‚ β†’ a ∈ s₁ ∩ sβ‚‚ := and_imp.1 mem_inter.2 theorem inter_subset_left (s₁ sβ‚‚ : finset Ξ±) : s₁ ∩ sβ‚‚ βŠ† s₁ := Ξ» a, mem_of_mem_inter_left theorem inter_subset_right (s₁ sβ‚‚ : finset Ξ±) : s₁ ∩ sβ‚‚ βŠ† sβ‚‚ := Ξ» a, mem_of_mem_inter_right theorem subset_inter {s₁ sβ‚‚ s₃ : finset Ξ±} : s₁ βŠ† sβ‚‚ β†’ s₁ βŠ† s₃ β†’ s₁ βŠ† sβ‚‚ ∩ s₃ := by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial @[simp, norm_cast] lemma coe_inter (s₁ sβ‚‚ : finset Ξ±) : ↑(s₁ ∩ sβ‚‚) = (s₁ ∩ sβ‚‚ : set Ξ±) := set.ext $ Ξ» _, mem_inter @[simp] theorem union_inter_cancel_left {s t : finset Ξ±} : (s βˆͺ t) ∩ s = s := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left] @[simp] theorem union_inter_cancel_right {s t : finset Ξ±} : (s βˆͺ t) ∩ t = t := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right] theorem inter_comm (s₁ sβ‚‚ : finset Ξ±) : s₁ ∩ sβ‚‚ = sβ‚‚ ∩ s₁ := ext $ Ξ» _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = s₁ ∩ (sβ‚‚ ∩ s₃) := ext $ Ξ» _, by simp only [mem_inter, and_assoc] theorem inter_left_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : s₁ ∩ (sβ‚‚ ∩ s₃) = sβ‚‚ ∩ (s₁ ∩ s₃) := ext $ Ξ» _, by simp only [mem_inter, and.left_comm] theorem inter_right_comm (s₁ sβ‚‚ s₃ : finset Ξ±) : (s₁ ∩ sβ‚‚) ∩ s₃ = (s₁ ∩ s₃) ∩ sβ‚‚ := ext $ Ξ» _, by simp only [mem_inter, and.right_comm] @[simp] theorem inter_self (s : finset Ξ±) : s ∩ s = s := ext $ Ξ» _, mem_inter.trans $ and_self _ @[simp] theorem inter_empty (s : finset Ξ±) : s ∩ βˆ… = βˆ… := ext $ Ξ» _, mem_inter.trans $ and_false _ @[simp] theorem empty_inter (s : finset Ξ±) : βˆ… ∩ s = βˆ… := ext $ Ξ» _, mem_inter.trans $ false_and _ @[simp] lemma inter_union_self (s t : finset Ξ±) : s ∩ (t βˆͺ s) = s := by rw [inter_comm, union_inter_cancel_right] @[simp] theorem insert_inter_of_mem {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} (h : a ∈ sβ‚‚) : insert a s₁ ∩ sβ‚‚ = insert a (s₁ ∩ sβ‚‚) := ext $ Ξ» x, have x = a ∨ x ∈ sβ‚‚ ↔ x ∈ sβ‚‚, from or_iff_right_of_imp $ by rintro rfl; exact h, by simp only [mem_inter, mem_insert, or_and_distrib_left, this] @[simp] theorem inter_insert_of_mem {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} (h : a ∈ s₁) : s₁ ∩ insert a sβ‚‚ = insert a (s₁ ∩ sβ‚‚) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} (h : a βˆ‰ sβ‚‚) : insert a s₁ ∩ sβ‚‚ = s₁ ∩ sβ‚‚ := ext $ Ξ» x, have Β¬ (x = a ∧ x ∈ sβ‚‚), by rintro ⟨rfl, H⟩; exact h H, by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or] @[simp] theorem inter_insert_of_not_mem {s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} (h : a βˆ‰ s₁) : s₁ ∩ insert a sβ‚‚ = s₁ ∩ sβ‚‚ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : Ξ±} {s : finset Ξ±} (H : a ∈ s) : {a} ∩ s = {a} := show insert a βˆ… ∩ s = insert a βˆ…, by rw [insert_inter_of_mem H, empty_inter] @[simp] theorem singleton_inter_of_not_mem {a : Ξ±} {s : finset Ξ±} (H : a βˆ‰ s) : {a} ∩ s = βˆ… := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h @[simp] theorem inter_singleton_of_mem {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : s ∩ {a} = {a} := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : s ∩ {a} = βˆ… := by rw [inter_comm, singleton_inter_of_not_mem h] @[mono] lemma inter_subset_inter {x y s t : finset Ξ±} (h : x βŠ† y) (h' : s βŠ† t) : x ∩ s βŠ† y ∩ t := begin intros a a_in, rw finset.mem_inter at a_in ⊒, exact ⟨h a_in.1, h' a_in.2⟩ end lemma inter_subset_inter_right {x y s : finset Ξ±} (h : x βŠ† y) : x ∩ s βŠ† y ∩ s := finset.inter_subset_inter h (finset.subset.refl _) lemma inter_subset_inter_left {x y s : finset Ξ±} (h : x βŠ† y) : s ∩ x βŠ† s ∩ y := finset.inter_subset_inter (finset.subset.refl _) h /-! ### lattice laws -/ instance : lattice (finset Ξ±) := { sup := (βˆͺ), sup_le := assume a b c, union_subset, le_sup_left := subset_union_left, le_sup_right := subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, ..finset.partial_order } @[simp] theorem sup_eq_union (s t : finset Ξ±) : s βŠ” t = s βˆͺ t := rfl @[simp] theorem inf_eq_inter (s t : finset Ξ±) : s βŠ“ t = s ∩ t := rfl instance : semilattice_inf_bot (finset Ξ±) := { bot := βˆ…, bot_le := empty_subset, ..finset.lattice } instance {Ξ± : Type*} [decidable_eq Ξ±] : semilattice_sup_bot (finset Ξ±) := { ..finset.semilattice_inf_bot, ..finset.lattice } instance : distrib_lattice (finset Ξ±) := { le_sup_inf := assume a b c, show (a βˆͺ b) ∩ (a βˆͺ c) βŠ† a βˆͺ b ∩ c, by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt}; simp only [true_or, imp_true_iff, true_and, or_true], ..finset.lattice } theorem inter_distrib_left (s t u : finset Ξ±) : s ∩ (t βˆͺ u) = (s ∩ t) βˆͺ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : finset Ξ±) : (s βˆͺ t) ∩ u = (s ∩ u) βˆͺ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : finset Ξ±) : s βˆͺ (t ∩ u) = (s βˆͺ t) ∩ (s βˆͺ u) := sup_inf_left theorem union_distrib_right (s t u : finset Ξ±) : (s ∩ t) βˆͺ u = (s βˆͺ u) ∩ (t βˆͺ u) := sup_inf_right lemma union_eq_empty_iff (A B : finset Ξ±) : A βˆͺ B = βˆ… ↔ A = βˆ… ∧ B = βˆ… := sup_eq_bot_iff /-! ### erase -/ /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset Ξ±) (a : Ξ±) : finset Ξ± := ⟨_, nodup_erase_of_nodup a s.2⟩ @[simp] theorem erase_val (s : finset Ξ±) (a : Ξ±) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : Ξ±} {s : finset Ξ±} : a ∈ erase s b ↔ a β‰  b ∧ a ∈ s := mem_erase_iff_of_nodup s.2 theorem not_mem_erase (a : Ξ±) (s : finset Ξ±) : a βˆ‰ erase s a := mem_erase_of_nodup s.2 @[simp] theorem erase_empty (a : Ξ±) : erase βˆ… a = βˆ… := rfl theorem ne_of_mem_erase {a b : Ξ±} {s : finset Ξ±} : b ∈ erase s a β†’ b β‰  a := by simp only [mem_erase]; exact and.left theorem mem_of_mem_erase {a b : Ξ±} {s : finset Ξ±} : b ∈ erase s a β†’ b ∈ s := mem_of_mem_erase theorem mem_erase_of_ne_of_mem {a b : Ξ±} {s : finset Ξ±} : a β‰  b β†’ a ∈ s β†’ a ∈ erase s b := by simp only [mem_erase]; exact and.intro /-- An element of `s` that is not an element of `erase s a` must be `a`. -/ lemma eq_of_mem_of_not_mem_erase {a b : Ξ±} {s : finset Ξ±} (hs : b ∈ s) (hsa : b βˆ‰ s.erase a) : b = a := begin rw [mem_erase, not_and] at hsa, exact not_imp_not.mp hsa hs end theorem erase_insert {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : erase (insert a s) a = s := ext $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or]; apply and_iff_right_of_imp; rintro H rfl; exact h H theorem insert_erase {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : insert a (erase s a) = s := ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and]; apply or_iff_right_of_imp; rintro rfl; exact h theorem erase_subset_erase (a : Ξ±) {s t : finset Ξ±} (h : s βŠ† t) : erase s a βŠ† erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : Ξ±) (s : finset Ξ±) : erase s a βŠ† s := erase_subset _ _ @[simp, norm_cast] lemma coe_erase (a : Ξ±) (s : finset Ξ±) : ↑(erase s a) = (s \ {a} : set Ξ±) := set.ext $ Ξ» _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl lemma erase_ssubset {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : s.erase a βŠ‚ s := calc s.erase a βŠ‚ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h theorem erase_eq_of_not_mem {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h theorem subset_insert_iff {a : Ξ±} {s t : finset Ξ±} : s βŠ† insert a t ↔ erase s a βŠ† t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp]; exact forall_congr (Ξ» x, forall_swap) theorem erase_insert_subset (a : Ξ±) (s : finset Ξ±) : erase (insert a s) a βŠ† s := subset_insert_iff.1 $ subset.refl _ theorem insert_erase_subset (a : Ξ±) (s : finset Ξ±) : s βŠ† insert a (erase s a) := subset_insert_iff.2 $ subset.refl _ /-! ### sdiff -/ /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset Ξ±) := ⟨λs₁ sβ‚‚, ⟨s₁.1 - sβ‚‚.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩ @[simp] theorem mem_sdiff {a : Ξ±} {s₁ sβ‚‚ : finset Ξ±} : a ∈ s₁ \ sβ‚‚ ↔ a ∈ s₁ ∧ a βˆ‰ sβ‚‚ := mem_sub_of_nodup s₁.2 lemma not_mem_sdiff_of_mem_right {a : Ξ±} {s t : finset Ξ±} (h : a ∈ t) : a βˆ‰ s \ t := by simp only [mem_sdiff, h, not_true, not_false_iff, and_false] theorem sdiff_union_of_subset {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : (sβ‚‚ \ s₁) βˆͺ s₁ = sβ‚‚ := ext $ Ξ» a, by simpa only [mem_sdiff, mem_union, or_comm, or_and_distrib_left, dec_em, and_true] using or_iff_right_of_imp (@h a) theorem union_sdiff_of_subset {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : s₁ βˆͺ (sβ‚‚ \ s₁) = sβ‚‚ := (union_comm _ _).trans (sdiff_union_of_subset h) theorem inter_sdiff (s t u : finset Ξ±) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] } @[simp] theorem inter_sdiff_self (s₁ sβ‚‚ : finset Ξ±) : s₁ ∩ (sβ‚‚ \ s₁) = βˆ… := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h @[simp] theorem sdiff_inter_self (s₁ sβ‚‚ : finset Ξ±) : (sβ‚‚ \ s₁) ∩ s₁ = βˆ… := (inter_comm _ _).trans (inter_sdiff_self _ _) @[simp] theorem sdiff_self (s₁ : finset Ξ±) : s₁ \ s₁ = βˆ… := by ext; simp theorem sdiff_inter_distrib_right (s₁ sβ‚‚ s₃ : finset Ξ±) : s₁ \ (sβ‚‚ ∩ s₃) = (s₁ \ sβ‚‚) βˆͺ (s₁ \ s₃) := by ext; simp only [and_or_distrib_left, mem_union, not_and_distrib, mem_sdiff, mem_inter] @[simp] theorem sdiff_inter_self_left (s₁ sβ‚‚ : finset Ξ±) : s₁ \ (s₁ ∩ sβ‚‚) = s₁ \ sβ‚‚ := by simp only [sdiff_inter_distrib_right, sdiff_self, empty_union] @[simp] theorem sdiff_inter_self_right (s₁ sβ‚‚ : finset Ξ±) : s₁ \ (sβ‚‚ ∩ s₁) = s₁ \ sβ‚‚ := by simp only [sdiff_inter_distrib_right, sdiff_self, union_empty] @[simp] theorem sdiff_empty {s₁ : finset Ξ±} : s₁ \ βˆ… = s₁ := ext (by simp) @[mono] theorem sdiff_subset_sdiff {s₁ sβ‚‚ t₁ tβ‚‚ : finset Ξ±} (h₁ : t₁ βŠ† tβ‚‚) (hβ‚‚ : sβ‚‚ βŠ† s₁) : t₁ \ s₁ βŠ† tβ‚‚ \ sβ‚‚ := by simpa only [subset_iff, mem_sdiff, and_imp] using Ξ» a m₁ mβ‚‚, and.intro (h₁ m₁) (mt (@hβ‚‚ _) mβ‚‚) theorem sdiff_subset_self {s₁ sβ‚‚ : finset Ξ±} : s₁ \ sβ‚‚ βŠ† s₁ := suffices s₁ \ sβ‚‚ βŠ† s₁ \ βˆ…, by simpa [sdiff_empty] using this, sdiff_subset_sdiff (subset.refl _) (empty_subset _) @[simp, norm_cast] lemma coe_sdiff (s₁ sβ‚‚ : finset Ξ±) : ↑(s₁ \ sβ‚‚) = (s₁ \ sβ‚‚ : set Ξ±) := set.ext $ Ξ» _, mem_sdiff @[simp] theorem union_sdiff_self_eq_union {s t : finset Ξ±} : s βˆͺ (t \ s) = s βˆͺ t := ext $ Ξ» a, by simp only [mem_union, mem_sdiff, or_iff_not_imp_left, imp_and_distrib, and_iff_left id] @[simp] theorem sdiff_union_self_eq_union {s t : finset Ξ±} : (s \ t) βˆͺ t = s βˆͺ t := by rw [union_comm, union_sdiff_self_eq_union, union_comm] lemma union_sdiff_symm {s t : finset Ξ±} : s βˆͺ (t \ s) = t βˆͺ (s \ t) := by rw [union_sdiff_self_eq_union, union_sdiff_self_eq_union, union_comm] lemma sdiff_union_inter (s t : finset Ξ±) : (s \ t) βˆͺ (s ∩ t) = s := by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto } @[simp] lemma sdiff_idem (s t : finset Ξ±) : s \ t \ t = s \ t := by { simp only [ext_iff, mem_sdiff], tauto } lemma sdiff_eq_empty_iff_subset {s t : finset Ξ±} : s \ t = βˆ… ↔ s βŠ† t := by { rw [subset_iff, ext_iff], simp } @[simp] lemma empty_sdiff (s : finset Ξ±) : βˆ… \ s = βˆ… := by { rw sdiff_eq_empty_iff_subset, exact empty_subset _ } lemma insert_sdiff_of_not_mem (s : finset Ξ±) {t : finset Ξ±} {x : Ξ±} (h : x βˆ‰ t) : (insert x s) \ t = insert x (s \ t) := begin rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_not_mem s h end lemma insert_sdiff_of_mem (s : finset Ξ±) {t : finset Ξ±} {x : Ξ±} (h : x ∈ t) : (insert x s) \ t = s \ t := begin rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_mem s h end @[simp] lemma insert_sdiff_insert (s t : finset Ξ±) (x : Ξ±) : (insert x s) \ (insert x t) = s \ insert x t := insert_sdiff_of_mem _ (mem_insert_self _ _) lemma sdiff_insert_of_not_mem {s : finset Ξ±} {x : Ξ±} (h : x βˆ‰ s) (t : finset Ξ±) : s \ (insert x t) = s \ t := begin refine subset.antisymm (sdiff_subset_sdiff (subset.refl _) (subset_insert _ _)) (Ξ» y hy, _), simp only [mem_sdiff, mem_insert, not_or_distrib] at hy ⊒, exact ⟨hy.1, Ξ» hxy, h $ hxy β–Έ hy.1, hy.2⟩ end @[simp] lemma sdiff_subset (s t : finset Ξ±) : s \ t βŠ† s := by simp [subset_iff, mem_sdiff] {contextual := tt} lemma union_sdiff_distrib (s₁ sβ‚‚ t : finset Ξ±) : (s₁ βˆͺ sβ‚‚) \ t = s₁ \ t βˆͺ sβ‚‚ \ t := by { simp only [ext_iff, mem_sdiff, mem_union], tauto } lemma sdiff_union_distrib (s t₁ tβ‚‚ : finset Ξ±) : s \ (t₁ βˆͺ tβ‚‚) = (s \ t₁) ∩ (s \ tβ‚‚) := by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto } lemma union_sdiff_self (s t : finset Ξ±) : (s βˆͺ t) \ t = s \ t := by rw [union_sdiff_distrib, sdiff_self, union_empty] lemma sdiff_singleton_eq_erase (a : Ξ±) (s : finset Ξ±) : s \ singleton a = erase s a := by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto } lemma sdiff_sdiff_self_left (s t : finset Ξ±) : s \ (s \ t) = s ∩ t := by { simp only [ext_iff, mem_sdiff, mem_inter], tauto } lemma inter_eq_inter_of_sdiff_eq_sdiff {s t₁ tβ‚‚ : finset Ξ±} : s \ t₁ = s \ tβ‚‚ β†’ s ∩ t₁ = s ∩ tβ‚‚ := by { simp only [ext_iff, mem_sdiff, mem_inter], intros b c, replace b := b c, split; tauto } end decidable_eq /-! ### attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset Ξ±) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ theorem sizeof_lt_sizeof_of_mem [has_sizeof Ξ±] {x : Ξ±} {s : finset Ξ±} (hx : x ∈ s) : sizeof x < sizeof s := by { cases s, dsimp [sizeof, has_sizeof.sizeof, finset.sizeof], apply lt_add_left, exact multiset.sizeof_lt_sizeof_of_mem hx } @[simp] theorem attach_val (s : finset Ξ±) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset Ξ±) : βˆ€ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (βˆ… : finset Ξ±) = βˆ… := rfl /-! ### piecewise -/ section piecewise /-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its complement. -/ def piecewise {Ξ± : Type*} {Ξ΄ : Ξ± β†’ Sort*} (s : finset Ξ±) (f g : Ξ i, Ξ΄ i) [βˆ€j, decidable (j ∈ s)] : Ξ i, Ξ΄ i := Ξ»i, if i ∈ s then f i else g i variables {Ξ΄ : Ξ± β†’ Sort*} (s : finset Ξ±) (f g : Ξ i, Ξ΄ i) @[simp] lemma piecewise_insert_self [decidable_eq Ξ±] {j : Ξ±} [βˆ€i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] @[simp] lemma piecewise_empty [βˆ€i : Ξ±, decidable (i ∈ (βˆ… : finset Ξ±))] : piecewise βˆ… f g = g := by { ext i, simp [piecewise] } variable [βˆ€j, decidable (j ∈ s)] @[norm_cast] lemma piecewise_coe [βˆ€j, decidable (j ∈ (s : set Ξ±))] : (s : set Ξ±).piecewise f g = s.piecewise f g := by { ext, congr } @[simp, priority 980] lemma piecewise_eq_of_mem {i : Ξ±} (hi : i ∈ s) : s.piecewise f g i = f i := by simp [piecewise, hi] @[simp, priority 980] lemma piecewise_eq_of_not_mem {i : Ξ±} (hi : i βˆ‰ s) : s.piecewise f g i = g i := by simp [piecewise, hi] lemma piecewise_congr {f f' g g' : Ξ  i, Ξ΄ i} (hf : βˆ€ i ∈ s, f i = f' i) (hg : βˆ€ i βˆ‰ s, g i = g' i) : s.piecewise f g = s.piecewise f' g' := funext $ Ξ» i, if_ctx_congr iff.rfl (hf i) (hg i) @[simp, priority 990] lemma piecewise_insert_of_ne [decidable_eq Ξ±] {i j : Ξ±} [βˆ€i, decidable (i ∈ insert j s)] (h : i β‰  j) : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] lemma piecewise_insert [decidable_eq Ξ±] (j : Ξ±) [βˆ€i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = update (s.piecewise f g) j (f j) := begin classical, rw [← piecewise_coe, ← piecewise_coe, ← set.piecewise_insert, ← coe_insert j s], congr end lemma piecewise_cases {i} (p : Ξ΄ i β†’ Prop) (hf : p (f i)) (hg : p (g i)) : p (s.piecewise f g i) := by by_cases hi : i ∈ s; simpa [hi] lemma piecewise_mem_set_pi {Ξ΄ : Ξ± β†’ Type*} {t : set Ξ±} {t' : Ξ  i, set (Ξ΄ i)} {f g} (hf : f ∈ set.pi t t') (hg : g ∈ set.pi t t') : s.piecewise f g ∈ set.pi t t' := by { classical, rw ← piecewise_coe, exact set.piecewise_mem_pi ↑s hf hg } lemma piecewise_singleton [decidable_eq Ξ±] (i : Ξ±) : piecewise {i} f g = update g i (f i) := by rw [← insert_emptyc_eq, piecewise_insert, piecewise_empty] lemma piecewise_piecewise_of_subset_left {s t : finset Ξ±} [Ξ  i, decidable (i ∈ s)] [Ξ  i, decidable (i ∈ t)] (h : s βŠ† t) (f₁ fβ‚‚ g : Ξ  a, Ξ΄ a) : s.piecewise (t.piecewise f₁ fβ‚‚) g = s.piecewise f₁ g := s.piecewise_congr (Ξ» i hi, piecewise_eq_of_mem _ _ _ (h hi)) (Ξ» _ _, rfl) @[simp] lemma piecewise_idem_left (f₁ fβ‚‚ g : Ξ  a, Ξ΄ a) : s.piecewise (s.piecewise f₁ fβ‚‚) g = s.piecewise f₁ g := piecewise_piecewise_of_subset_left (subset.refl _) _ _ _ lemma piecewise_piecewise_of_subset_right {s t : finset Ξ±} [Ξ  i, decidable (i ∈ s)] [Ξ  i, decidable (i ∈ t)] (h : t βŠ† s) (f g₁ gβ‚‚ : Ξ  a, Ξ΄ a) : s.piecewise f (t.piecewise g₁ gβ‚‚) = s.piecewise f gβ‚‚ := s.piecewise_congr (Ξ» _ _, rfl) (Ξ» i hi, t.piecewise_eq_of_not_mem _ _ (mt (@h _) hi)) @[simp] lemma piecewise_idem_right (f g₁ gβ‚‚ : Ξ  a, Ξ΄ a) : s.piecewise f (s.piecewise g₁ gβ‚‚) = s.piecewise f gβ‚‚ := piecewise_piecewise_of_subset_right (subset.refl _) f g₁ gβ‚‚ lemma update_eq_piecewise {Ξ² : Type*} [decidable_eq Ξ±] (f : Ξ± β†’ Ξ²) (i : Ξ±) (v : Ξ²) : update f i v = piecewise (singleton i) (Ξ»j, v) f := (piecewise_singleton _ _ _).symm lemma update_piecewise [decidable_eq Ξ±] (i : Ξ±) (v : Ξ΄ i) : update (s.piecewise f g) i v = s.piecewise (update f i v) (update g i v) := begin ext j, rcases em (j = i) with (rfl|hj); by_cases hs : j ∈ s; simp * end lemma update_piecewise_of_mem [decidable_eq Ξ±] {i : Ξ±} (hi : i ∈ s) (v : Ξ΄ i) : update (s.piecewise f g) i v = s.piecewise (update f i v) g := begin rw update_piecewise, refine s.piecewise_congr (Ξ» _ _, rfl) (Ξ» j hj, update_noteq _ _ _), exact Ξ» h, hj (h.symm β–Έ hi) end lemma update_piecewise_of_not_mem [decidable_eq Ξ±] {i : Ξ±} (hi : i βˆ‰ s) (v : Ξ΄ i) : update (s.piecewise f g) i v = s.piecewise f (update g i v) := begin rw update_piecewise, refine s.piecewise_congr (Ξ» j hj, update_noteq _ _ _) (Ξ» _ _, rfl), exact Ξ» h, hi (h β–Έ hj) end lemma piecewise_le_of_le_of_le {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g h : Ξ  i, Ξ΄ i} (Hf : f ≀ h) (Hg : g ≀ h) : s.piecewise f g ≀ h := Ξ» x, piecewise_cases s f g (≀ h x) (Hf x) (Hg x) lemma le_piecewise_of_le_of_le {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g h : Ξ  i, Ξ΄ i} (Hf : h ≀ f) (Hg : h ≀ g) : h ≀ s.piecewise f g := Ξ» x, piecewise_cases s f g (Ξ» y, h x ≀ y) (Hf x) (Hg x) lemma piecewise_le_piecewise' {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g f' g' : Ξ  i, Ξ΄ i} (Hf : βˆ€ x ∈ s, f x ≀ f' x) (Hg : βˆ€ x βˆ‰ s, g x ≀ g' x) : s.piecewise f g ≀ s.piecewise f' g' := Ξ» x, by { by_cases hx : x ∈ s; simp [hx, *] } lemma piecewise_le_piecewise {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g f' g' : Ξ  i, Ξ΄ i} (Hf : f ≀ f') (Hg : g ≀ g') : s.piecewise f g ≀ s.piecewise f' g' := s.piecewise_le_piecewise' (Ξ» x _, Hf x) (Ξ» x _, Hg x) lemma piecewise_mem_Icc_of_mem_of_mem {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f f₁ g g₁ : Ξ  i, Ξ΄ i} (hf : f ∈ set.Icc f₁ g₁) (hg : g ∈ set.Icc f₁ g₁) : s.piecewise f g ∈ set.Icc f₁ g₁ := ⟨le_piecewise_of_le_of_le _ hf.1 hg.1, piecewise_le_of_le_of_le _ hf.2 hg.2⟩ lemma piecewise_mem_Icc {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g : Ξ  i, Ξ΄ i} (h : f ≀ g) : s.piecewise f g ∈ set.Icc f g := piecewise_mem_Icc_of_mem_of_mem _ (set.left_mem_Icc.2 h) (set.right_mem_Icc.2 h) lemma piecewise_mem_Icc' {Ξ΄ : Ξ± β†’ Type*} [Ξ  i, preorder (Ξ΄ i)] {f g : Ξ  i, Ξ΄ i} (h : g ≀ f) : s.piecewise f g ∈ set.Icc g f := piecewise_mem_Icc_of_mem_of_mem _ (set.right_mem_Icc.2 h) (set.left_mem_Icc.2 h) end piecewise section decidable_pi_exists variables {s : finset Ξ±} instance decidable_dforall_finset {p : Ξ a∈s, Prop} [hp : βˆ€a (h : a ∈ s), decidable (p a h)] : decidable (βˆ€a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {Ξ² : Ξ± β†’ Type*} [h : βˆ€a, decidable_eq (Ξ² a)] : decidable_eq (Ξ a∈s, Ξ² a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Ξ a∈s, Prop} [hp : βˆ€a (h : a ∈ s), decidable (p a h)] : decidable (βˆƒa (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /-! ### filter -/ section filter variables (p q : Ξ± β†’ Prop) [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (s : finset Ξ±) : finset Ξ± := ⟨_, nodup_filter p s.2⟩ @[simp] theorem filter_val (s : finset Ξ±) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem filter_subset (s : finset Ξ±) : s.filter p βŠ† s := filter_subset _ _ variable {p} @[simp] theorem mem_filter {s : finset Ξ±} {a : Ξ±} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter theorem filter_ssubset {s : finset Ξ±} : s.filter p βŠ‚ s ↔ βˆƒ x ∈ s, Β¬ p x := ⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (Ξ» hp, mem_filter.2 ⟨hs, hp⟩) hp⟩, Ξ» ⟨x, hs, hp⟩, ⟨s.filter_subset _, Ξ» h, hp (mem_filter.1 (h hs)).2⟩⟩ variable (p) theorem filter_filter (s : finset Ξ±) : (s.filter p).filter q = s.filter (Ξ»a, p a ∧ q a) := ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm] lemma filter_true {s : finset Ξ±} [h : decidable_pred (Ξ» _, true)] : @finset.filter Ξ± (Ξ» _, true) h s = s := by ext; simp @[simp] theorem filter_false {h} (s : finset Ξ±) : @filter Ξ± (Ξ»a, false) h s = βˆ… := ext $ assume a, by simp only [mem_filter, and_false]; refl variables {p q} /-- If all elements of a `finset` satisfy the predicate `p`, `s.filter p` is `s`. -/ @[simp] lemma filter_true_of_mem {s : finset Ξ±} (h : βˆ€ x ∈ s, p x) : s.filter p = s := ext $ Ξ» x, ⟨λ h, (mem_filter.1 h).1, Ξ» hx, mem_filter.2 ⟨hx, h x hx⟩⟩ /-- If all elements of a `finset` fail to satisfy the predicate `p`, `s.filter p` is `βˆ…`. -/ lemma filter_false_of_mem {s : finset Ξ±} (h : βˆ€ x ∈ s, Β¬ p x) : s.filter p = βˆ… := eq_empty_of_forall_not_mem (by simpa) lemma filter_congr {s : finset Ξ±} (H : βˆ€ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H variables (p q) lemma filter_empty : filter p βˆ… = βˆ… := subset_empty.1 $ filter_subset _ _ lemma filter_subset_filter {s t : finset Ξ±} (h : s βŠ† t) : s.filter p βŠ† t.filter p := assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩ @[simp, norm_cast] lemma coe_filter (s : finset Ξ±) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set Ξ±) := set.ext $ Ξ» _, mem_filter theorem filter_singleton (a : Ξ±) : filter p (singleton a) = if p a then singleton a else βˆ… := by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } variable [decidable_eq Ξ±] theorem filter_union (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).filter p = s₁.filter p βˆͺ sβ‚‚.filter p := ext $ Ξ» _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_union_right (s : finset Ξ±) : s.filter p βˆͺ s.filter q = s.filter (Ξ»x, p x ∨ q x) := ext $ Ξ» x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm] lemma filter_mem_eq_inter {s t : finset Ξ±} [Ξ  i, decidable (i ∈ t)] : s.filter (Ξ» i, i ∈ t) = s ∩ t := ext $ Ξ» i, by rw [mem_filter, mem_inter] theorem filter_inter (s t : finset Ξ±) : filter p s ∩ t = filter p (s ∩ t) := by { ext, simp only [mem_inter, mem_filter, and.right_comm] } theorem inter_filter (s t : finset Ξ±) : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : Ξ±) (s : finset Ξ±) : filter p (insert a s) = if p a then insert a (filter p s) else filter p s := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_or [decidable_pred (Ξ» a, p a ∨ q a)] (s : finset Ξ±) : s.filter (Ξ» a, p a ∨ q a) = s.filter p βˆͺ s.filter q := ext $ Ξ» _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and [decidable_pred (Ξ» a, p a ∧ q a)] (s : finset Ξ±) : s.filter (Ξ» a, p a ∧ q a) = s.filter p ∩ s.filter q := ext $ Ξ» _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not [decidable_pred (Ξ» a, Β¬ p a)] (s : finset Ξ±) : s.filter (Ξ» a, Β¬ p a) = s \ s.filter p := ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using Ξ» a, and_congr_right $ Ξ» h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ sβ‚‚ : finset Ξ±) : s₁ \ sβ‚‚ = filter (βˆ‰ sβ‚‚) s₁ := ext $ Ξ» _, by simp only [mem_sdiff, mem_filter] theorem sdiff_eq_self (s₁ sβ‚‚ : finset Ξ±) : s₁ \ sβ‚‚ = s₁ ↔ s₁ ∩ sβ‚‚ βŠ† βˆ… := by { simp [subset.antisymm_iff,sdiff_subset_self], split; intro h, { transitivity' ((s₁ \ sβ‚‚) ∩ sβ‚‚), mono, simp }, { calc s₁ \ sβ‚‚ βŠ‡ s₁ \ (s₁ ∩ sβ‚‚) : by simp [(βŠ‡)] ... βŠ‡ s₁ \ βˆ… : by mono using [(βŠ‡)] ... βŠ‡ s₁ : by simp [(βŠ‡)] } } theorem filter_union_filter_neg_eq [decidable_pred (Ξ» a, Β¬ p a)] (s : finset Ξ±) : s.filter p βˆͺ s.filter (Ξ»a, Β¬ p a) = s := by simp only [filter_not, union_sdiff_of_subset (filter_subset p s)] theorem filter_inter_filter_neg_eq (s : finset Ξ±) : s.filter p ∩ s.filter (Ξ»a, Β¬ p a) = βˆ… := by simp only [filter_not, inter_sdiff_self] lemma subset_union_elim {s : finset Ξ±} {t₁ tβ‚‚ : set Ξ±} (h : ↑s βŠ† t₁ βˆͺ tβ‚‚) : βˆƒs₁ sβ‚‚ : finset Ξ±, s₁ βˆͺ sβ‚‚ = s ∧ ↑s₁ βŠ† t₁ ∧ ↑sβ‚‚ βŠ† tβ‚‚ \ t₁ := begin classical, refine ⟨s.filter (∈ t₁), s.filter (βˆ‰ t₁), _, _ , _⟩, { simp [filter_union_right, em] }, { intro x, simp }, { intro x, simp, intros hx hxβ‚‚, refine ⟨or.resolve_left (h hx) hxβ‚‚, hxβ‚‚βŸ© } end /- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/ @[simp] lemma filter_congr_decidable {Ξ±} (s : finset Ξ±) (p : Ξ± β†’ Prop) (h : decidable_pred p) [decidable_pred p] : @filter Ξ± p h s = s.filter p := by congr section classical open_locale classical /-- The following instance allows us to write `{ x ∈ s | p x }` for `finset.filter s p`. Since the former notation requires us to define this for all propositions `p`, and `finset.filter` only works for decidable propositions, the notation `{ x ∈ s | p x }` is only compatible with classical logic because it uses `classical.prop_decidable`. We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp` unfolds the notation `{ x ∈ s | p x }` to `finset.filter s p`. If `p` happens to be decidable, the simp-lemma `filter_congr_decidable` will make sure that `finset.filter` uses the right instance for decidability. -/ noncomputable instance {Ξ± : Type*} : has_sep Ξ± (finset Ξ±) := ⟨λ p x, x.filter p⟩ @[simp] lemma sep_def {Ξ± : Type*} (s : finset Ξ±) (p : Ξ± β†’ Prop) : {x ∈ s | p x} = s.filter p := rfl end classical /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ -- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter(eq b)`. lemma filter_eq [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) : s.filter (eq b) = ite (b ∈ s) {b} βˆ… := begin split_ifs, { ext, simp only [mem_filter, mem_singleton], exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩, }⟩ }, { ext, simp only [mem_filter, not_and, iff_false, not_mem_empty], rintros m ⟨e⟩, exact h m, } end /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ lemma filter_eq' [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) : s.filter (Ξ» a, a = b) = ite (b ∈ s) {b} βˆ… := trans (filter_congr (Ξ» _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b) lemma filter_ne [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) : s.filter (Ξ» a, b β‰  a) = s.erase b := by { ext, simp only [mem_filter, mem_erase, ne.def], cc, } lemma filter_ne' [decidable_eq Ξ²] (s : finset Ξ²) (b : Ξ²) : s.filter (Ξ» a, a β‰  b) = s.erase b := trans (filter_congr (Ξ» _ _, ⟨ne.symm, ne.symm⟩)) (filter_ne s b) end filter /-! ### range -/ section range variables {n m l : β„•} /-- `range n` is the set of natural numbers less than `n`. -/ def range (n : β„•) : finset β„• := ⟨_, nodup_range n⟩ @[simp] theorem range_coe (n : β„•) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp] theorem range_zero : range 0 = βˆ… := rfl @[simp] theorem range_one : range 1 = {0} := rfl theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm theorem range_add_one : range (n + 1) = insert n (range n) := range_succ @[simp] theorem not_mem_range_self : n βˆ‰ range n := not_mem_range_self @[simp] theorem self_mem_range_succ (n : β„•) : n ∈ range (n + 1) := multiset.self_mem_range_succ n @[simp] theorem range_subset {n m} : range n βŠ† range m ↔ n ≀ m := range_subset theorem range_mono : monotone range := Ξ» _ _, range_subset.2 lemma mem_range_succ_iff {a b : β„•} : a ∈ finset.range b.succ ↔ a ≀ b := finset.mem_range.trans nat.lt_succ_iff end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : Ξ± β†’ Prop) : (βˆƒ x, x ∈ (βˆ… : finset Ξ±) ∧ p x) ↔ false := by simp only [not_mem_empty, false_and, exists_false] theorem exists_mem_insert [d : decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) (p : Ξ± β†’ Prop) : (βˆƒ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (βˆƒ x, x ∈ s ∧ p x) := by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem forall_mem_empty_iff (p : Ξ± β†’ Prop) : (βˆ€ x, x ∈ (βˆ… : finset Ξ±) β†’ p x) ↔ true := iff_true_intro $ Ξ» _, false.elim theorem forall_mem_insert [d : decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) (p : Ξ± β†’ Prop) : (βˆ€ x, x ∈ insert a s β†’ p x) ↔ p a ∧ (βˆ€ x, x ∈ s β†’ p x) := by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] end finset /-- Equivalence between the set of natural numbers which are `β‰₯ k` and `β„•`, given by `n β†’ n - k`. -/ def not_mem_range_equiv (k : β„•) : {n // n βˆ‰ range k} ≃ β„• := { to_fun := Ξ» i, i.1 - k, inv_fun := Ξ» j, ⟨j + k, by simp⟩, left_inv := begin assume j, rw subtype.ext_iff_val, apply nat.sub_add_cancel, simpa using j.2 end, right_inv := Ξ» j, nat.add_sub_cancel _ _ } @[simp] lemma coe_not_mem_range_equiv (k : β„•) : (not_mem_range_equiv k : {n // n βˆ‰ range k} β†’ β„•) = (Ξ» i, i - k) := rfl @[simp] lemma coe_not_mem_range_equiv_symm (k : β„•) : ((not_mem_range_equiv k).symm : β„• β†’ {n // n βˆ‰ range k}) = Ξ» j, ⟨j + k, by simp⟩ := rfl namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option Ξ±) : finset Ξ± := match o with | none := βˆ… | some a := {a} end @[simp] theorem to_finset_none : none.to_finset = (βˆ… : finset Ξ±) := rfl @[simp] theorem to_finset_some {a : Ξ±} : (some a).to_finset = {a} := rfl @[simp] theorem mem_to_finset {a : Ξ±} {o : option Ξ±} : a ∈ o.to_finset ↔ a ∈ o := by cases o; simp only [to_finset, finset.mem_singleton, option.mem_def, eq_comm]; refl end option /-! ### erase_dup on list and multiset -/ namespace multiset variable [decidable_eq Ξ±] /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset Ξ±) : finset Ξ± := ⟨_, nodup_erase_dup s⟩ @[simp] theorem to_finset_val (s : multiset Ξ±) : s.to_finset.1 = s.erase_dup := rfl theorem to_finset_eq {s : multiset Ξ±} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 (erase_dup_eq_self.2 n).symm @[simp] theorem mem_to_finset {a : Ξ±} {s : multiset Ξ±} : a ∈ s.to_finset ↔ a ∈ s := mem_erase_dup @[simp] lemma to_finset_zero : to_finset (0 : multiset Ξ±) = βˆ… := rfl @[simp] lemma to_finset_cons (a : Ξ±) (s : multiset Ξ±) : to_finset (a ::β‚˜ s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons @[simp] lemma to_finset_add (s t : multiset Ξ±) : to_finset (s + t) = to_finset s βˆͺ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_nsmul (s : multiset Ξ±) : βˆ€(n : β„•) (hn : n β‰  0), (n β€’β„• s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, one_nsmul] }, { rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] } end @[simp] lemma to_finset_inter (s t : multiset Ξ±) : to_finset (s ∩ t) = to_finset s ∩ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_union (s t : multiset Ξ±) : (s βˆͺ t).to_finset = s.to_finset βˆͺ t.to_finset := by ext; simp theorem to_finset_eq_empty {m : multiset Ξ±} : m.to_finset = βˆ… ↔ m = 0 := finset.val_inj.symm.trans multiset.erase_dup_eq_zero @[simp] lemma to_finset_subset (m1 m2 : multiset Ξ±) : m1.to_finset βŠ† m2.to_finset ↔ m1 βŠ† m2 := by simp only [finset.subset_iff, multiset.subset_iff, multiset.mem_to_finset] end multiset namespace finset @[simp] lemma val_to_finset [decidable_eq Ξ±] (s : finset Ξ±) : s.val.to_finset = s := by { ext, rw [multiset.mem_to_finset, ←mem_def] } end finset namespace list variable [decidable_eq Ξ±] /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list Ξ±) : finset Ξ± := multiset.to_finset l @[simp] theorem to_finset_val (l : list Ξ±) : l.to_finset.1 = (l.erase_dup : multiset Ξ±) := rfl theorem to_finset_eq {l : list Ξ±} (n : nodup l) : @finset.mk Ξ± l n = l.to_finset := multiset.to_finset_eq n @[simp] theorem mem_to_finset {a : Ξ±} {l : list Ξ±} : a ∈ l.to_finset ↔ a ∈ l := mem_erase_dup @[simp] theorem to_finset_nil : to_finset (@nil Ξ±) = βˆ… := rfl @[simp] theorem to_finset_cons {a : Ξ±} {l : list Ξ±} : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h] lemma to_finset_surj_on : set.surj_on to_finset {l : list Ξ± | l.nodup} set.univ := begin rintro s -, cases s with t hl, induction t using quot.ind with l, refine ⟨l, hl, (to_finset_eq hl).symm⟩ end theorem to_finset_surjective : surjective (to_finset : list Ξ± β†’ finset Ξ±) := by { intro s, rcases to_finset_surj_on (set.mem_univ s) with ⟨l, -, hls⟩, exact ⟨l, hls⟩ } end list namespace finset /-! ### map -/ section map open function /-- When `f` is an embedding of `Ξ±` in `Ξ²` and `s` is a finset in `Ξ±`, then `s.map f` is the image finset in `Ξ²`. The embedding condition guarantees that there are no duplicates in the image. -/ def map (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : finset Ξ² := ⟨s.1.map f, nodup_map f.2 s.2⟩ @[simp] theorem map_val (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : Ξ± β†ͺ Ξ²) : (βˆ… : finset Ξ±).map f = βˆ… := rfl variables {f : Ξ± β†ͺ Ξ²} {s : finset Ξ±} @[simp] theorem mem_map {b : Ξ²} : b ∈ s.map f ↔ βˆƒ a ∈ s, f a = b := mem_map.trans $ by simp only [exists_prop]; refl theorem mem_map' (f : Ξ± β†ͺ Ξ²) {a} {s : finset Ξ±} : f a ∈ s.map f ↔ a ∈ s := mem_map_of_injective f.2 theorem mem_map_of_mem (f : Ξ± β†ͺ Ξ²) {a} {s : finset Ξ±} : a ∈ s β†’ f a ∈ s.map f := (mem_map' _).2 @[simp, norm_cast] theorem coe_map (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : (↑(s.map f) : set Ξ²) = f '' ↑s := set.ext $ Ξ» x, mem_map.trans set.mem_image_iff_bex.symm theorem coe_map_subset_range (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : (↑(s.map f) : set Ξ²) βŠ† set.range f := calc ↑(s.map f) = f '' ↑s : coe_map f s ... βŠ† set.range f : set.image_subset_range f ↑s theorem map_to_finset [decidable_eq Ξ±] [decidable_eq Ξ²] {s : multiset Ξ±} : s.to_finset.map f = (s.map f).to_finset := ext $ Ξ» _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset] @[simp] theorem map_refl : s.map (embedding.refl _) = s := ext $ Ξ» _, by simpa only [mem_map, exists_prop] using exists_eq_right theorem map_map {g : Ξ² β†ͺ Ξ³} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp only [map_val, multiset.map_map]; refl theorem map_subset_map {s₁ sβ‚‚ : finset Ξ±} : s₁.map f βŠ† sβ‚‚.map f ↔ s₁ βŠ† sβ‚‚ := ⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs, Ξ» h, by simp [subset_def, map_subset_map h]⟩ theorem map_inj {s₁ sβ‚‚ : finset Ξ±} : s₁.map f = sβ‚‚.map f ↔ s₁ = sβ‚‚ := by simp only [subset.antisymm_iff, map_subset_map] /-- Associate to an embedding `f` from `Ξ±` to `Ξ²` the embedding that maps a finset to its image under `f`. -/ def map_embedding (f : Ξ± β†ͺ Ξ²) : finset Ξ± β†ͺ finset Ξ² := ⟨map f, Ξ» s₁ sβ‚‚, map_inj.1⟩ @[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl theorem map_filter {p : Ξ² β†’ Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := ext $ Ξ» b, by simp only [mem_filter, mem_map, exists_prop, and_assoc]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, h1, h2, rfl⟩, by rintro ⟨x, h1, h2, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem map_union [decidable_eq Ξ±] [decidable_eq Ξ²] {f : Ξ± β†ͺ Ξ²} (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).map f = s₁.map f βˆͺ sβ‚‚.map f := ext $ Ξ» _, by simp only [mem_map, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem map_inter [decidable_eq Ξ±] [decidable_eq Ξ²] {f : Ξ± β†ͺ Ξ²} (s₁ sβ‚‚ : finset Ξ±) : (s₁ ∩ sβ‚‚).map f = s₁.map f ∩ sβ‚‚.map f := ext $ Ξ» b, by simp only [mem_map, mem_inter, exists_prop]; exact ⟨by rintro ⟨a, ⟨m₁, mβ‚‚βŸ©, rfl⟩; exact ⟨⟨a, m₁, rfl⟩, ⟨a, mβ‚‚, rfl⟩⟩, by rintro ⟨⟨a, m₁, e⟩, ⟨a', mβ‚‚, rfl⟩⟩; cases f.2 e; exact ⟨_, ⟨m₁, mβ‚‚βŸ©, rfl⟩⟩ @[simp] theorem map_singleton (f : Ξ± β†ͺ Ξ²) (a : Ξ±) : map f {a} = {f a} := ext $ Ξ» _, by simp only [mem_map, mem_singleton, exists_prop, exists_eq_left]; exact eq_comm @[simp] theorem map_insert [decidable_eq Ξ±] [decidable_eq Ξ²] (f : Ξ± β†ͺ Ξ²) (a : Ξ±) (s : finset Ξ±) : (insert a s).map f = insert (f a) (s.map f) := by simp only [insert_eq, map_union, map_singleton] @[simp] theorem map_eq_empty : s.map f = βˆ… ↔ s = βˆ… := ⟨λ h, eq_empty_of_forall_not_mem $ Ξ» a m, ne_empty_of_mem (mem_map_of_mem _ m) h, Ξ» e, e.symm β–Έ rfl⟩ lemma attach_map_val {s : finset Ξ±} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _ lemma nonempty.map (h : s.nonempty) (f : Ξ± β†ͺ Ξ²) : (s.map f).nonempty := let ⟨a, ha⟩ := h in ⟨f a, (mem_map' f).mpr ha⟩ end map lemma range_add_one' (n : β„•) : range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ.inj⟩) := by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n] /-! ### image -/ section image variables [decidable_eq Ξ²] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : Ξ± β†’ Ξ²) (s : finset Ξ±) : finset Ξ² := (s.1.map f).to_finset @[simp] theorem image_val (f : Ξ± β†’ Ξ²) (s : finset Ξ±) : (image f s).1 = (s.1.map f).erase_dup := rfl @[simp] theorem image_empty (f : Ξ± β†’ Ξ²) : (βˆ… : finset Ξ±).image f = βˆ… := rfl variables {f : Ξ± β†’ Ξ²} {s : finset Ξ±} @[simp] theorem mem_image {b : Ξ²} : b ∈ s.image f ↔ βˆƒ a ∈ s, f a = b := by simp only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop] theorem mem_image_of_mem (f : Ξ± β†’ Ξ²) {a} {s : finset Ξ±} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ lemma filter_mem_image_eq_image (f : Ξ± β†’ Ξ²) (s : finset Ξ±) (t : finset Ξ²) (h : βˆ€ x ∈ s, f x ∈ t) : t.filter (Ξ» y, y ∈ s.image f) = s.image f := by { ext, rw [mem_filter, mem_image], simp only [and_imp, exists_prop, and_iff_right_iff_imp, exists_imp_distrib], rintros x xel rfl, exact h _ xel } lemma fiber_nonempty_iff_mem_image (f : Ξ± β†’ Ξ²) (s : finset Ξ±) (y : Ξ²) : (s.filter (Ξ» x, f x = y)).nonempty ↔ y ∈ s.image f := by simp [finset.nonempty] @[simp, norm_cast] lemma coe_image {f : Ξ± β†’ Ξ²} : ↑(s.image f) = f '' ↑s := set.ext $ Ξ» _, mem_image.trans set.mem_image_iff_bex.symm lemma nonempty.image (h : s.nonempty) (f : Ξ± β†’ Ξ²) : (s.image f).nonempty := let ⟨a, ha⟩ := h in ⟨f a, mem_image_of_mem f ha⟩ theorem image_to_finset [decidable_eq Ξ±] {s : multiset Ξ±} : s.to_finset.image f = (s.map f).to_finset := ext $ Ξ» _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map] theorem image_val_of_inj_on (H : βˆ€x∈s, βˆ€y∈s, f x = f y β†’ x = y) : (image f s).1 = s.1.map f := multiset.erase_dup_eq_self.2 (nodup_map_on H s.2) @[simp] theorem image_id [decidable_eq Ξ±] : s.image id = s := ext $ Ξ» _, by simp only [mem_image, exists_prop, id, exists_eq_right] theorem image_image [decidable_eq Ξ³] {g : Ξ² β†’ Ξ³} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map] theorem image_subset_image {s₁ sβ‚‚ : finset Ξ±} (h : s₁ βŠ† sβ‚‚) : s₁.image f βŠ† sβ‚‚.image f := by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset', multiset.map_subset_map h] theorem image_subset_iff {s : finset Ξ±} {t : finset Ξ²} {f : Ξ± β†’ Ξ²} : s.image f βŠ† t ↔ βˆ€ x ∈ s, f x ∈ t := calc s.image f βŠ† t ↔ f '' ↑s βŠ† ↑t : by norm_cast ... ↔ _ : set.image_subset_iff theorem image_mono (f : Ξ± β†’ Ξ²) : monotone (finset.image f) := Ξ» _ _, image_subset_image theorem coe_image_subset_range : ↑(s.image f) βŠ† set.range f := calc ↑(s.image f) = f '' ↑s : coe_image ... βŠ† set.range f : set.image_subset_range f ↑s theorem image_filter {p : Ξ² β†’ Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext $ Ξ» b, by simp only [mem_filter, mem_image, exists_prop]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩, by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem image_union [decidable_eq Ξ±] {f : Ξ± β†’ Ξ²} (s₁ sβ‚‚ : finset Ξ±) : (s₁ βˆͺ sβ‚‚).image f = s₁.image f βˆͺ sβ‚‚.image f := ext $ Ξ» _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem image_inter [decidable_eq Ξ±] (s₁ sβ‚‚ : finset Ξ±) (hf : βˆ€x y, f x = f y β†’ x = y) : (s₁ ∩ sβ‚‚).image f = s₁.image f ∩ sβ‚‚.image f := ext $ by simp only [mem_image, exists_prop, mem_inter]; exact Ξ» b, ⟨λ ⟨a, ⟨m₁, mβ‚‚βŸ©, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, mβ‚‚, e⟩⟩, Ξ» ⟨⟨a, m₁, eβ‚βŸ©, ⟨a', mβ‚‚, eβ‚‚βŸ©βŸ©, ⟨a, ⟨m₁, hf _ _ (eβ‚‚.trans e₁.symm) β–Έ mβ‚‚βŸ©, eβ‚βŸ©βŸ©. @[simp] theorem image_singleton (f : Ξ± β†’ Ξ²) (a : Ξ±) : image f {a} = {f a} := ext $ Ξ» x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm @[simp] theorem image_insert [decidable_eq Ξ±] (f : Ξ± β†’ Ξ²) (a : Ξ±) (s : finset Ξ±) : (insert a s).image f = insert (f a) (s.image f) := by simp only [insert_eq, image_singleton, image_union] @[simp] theorem image_eq_empty : s.image f = βˆ… ↔ s = βˆ… := ⟨λ h, eq_empty_of_forall_not_mem $ Ξ» a m, ne_empty_of_mem (mem_image_of_mem _ m) h, Ξ» e, e.symm β–Έ rfl⟩ lemma attach_image_val [decidable_eq Ξ±] {s : finset Ξ±} : s.attach.image subtype.val = s := eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self] @[simp] lemma attach_insert [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (Ξ»x, ⟨x.1, mem_insert_of_mem x.2⟩)) := ext $ Ξ» ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx) (Ξ» h : x = a, Ξ» _, mem_insert.2 $ or.inl $ subtype.eq h) (Ξ» h : x ∈ s, Ξ» _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩), Ξ» _, finset.mem_attach _ _⟩ theorem map_eq_image (f : Ξ± β†ͺ Ξ²) (s : finset Ξ±) : s.map f = s.image f := eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm lemma image_const {s : finset Ξ±} (h : s.nonempty) (b : Ξ²) : s.image (Ξ»a, b) = singleton b := ext $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, h.bex, true_and, mem_singleton, eq_comm] /-- Because `finset.image` requires a `decidable_eq` instances for the target type, we can only construct a `functor finset` when working classically. -/ instance [Ξ  P, decidable P] : functor finset := { map := Ξ» Ξ± Ξ² f s, s.image f, } instance [Ξ  P, decidable P] : is_lawful_functor finset := { id_map := Ξ» Ξ± x, image_id, comp_map := Ξ» Ξ± Ξ² Ξ³ f g s, image_image.symm, } /-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose elements belong to `s`. -/ protected def subtype {Ξ±} (p : Ξ± β†’ Prop) [decidable_pred p] (s : finset Ξ±) : finset (subtype p) := (s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩, Ξ» x y H, subtype.eq $ subtype.mk.inj H⟩ @[simp] lemma mem_subtype {p : Ξ± β†’ Prop} [decidable_pred p] {s : finset Ξ±} : βˆ€{a : subtype p}, a ∈ s.subtype p ↔ (a : Ξ±) ∈ s | ⟨a, ha⟩ := by simp [finset.subtype, ha] lemma subtype_eq_empty {p : Ξ± β†’ Prop} [decidable_pred p] {s : finset Ξ±} : s.subtype p = βˆ… ↔ βˆ€ x, p x β†’ x βˆ‰ s := by simp [ext_iff, subtype.forall, subtype.coe_mk]; refl /-- `s.subtype p` converts back to `s.filter p` with `embedding.subtype`. -/ @[simp] lemma subtype_map (p : Ξ± β†’ Prop) [decidable_pred p] : (s.subtype p).map (embedding.subtype _) = s.filter p := begin ext x, rw mem_map, change (βˆƒ a : {x // p x}, βˆƒ H, (a : Ξ±) = x) ↔ _, split, { rintros ⟨y, hy, hyval⟩, rw [mem_subtype, hyval] at hy, rw mem_filter, use hy, rw ← hyval, use y.property }, { intro hx, rw mem_filter at hx, use ⟨⟨x, hx.2⟩, mem_subtype.2 hx.1, rfl⟩ } end /-- If all elements of a `finset` satisfy the predicate `p`, `s.subtype p` converts back to `s` with `embedding.subtype`. -/ lemma subtype_map_of_mem {p : Ξ± β†’ Prop} [decidable_pred p] (h : βˆ€ x ∈ s, p x) : (s.subtype p).map (embedding.subtype _) = s := by rw [subtype_map, filter_true_of_mem h] /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, all elements of the result have the property of the subtype. -/ lemma property_of_mem_map_subtype {p : Ξ± β†’ Prop} (s : finset {x // p x}) {a : Ξ±} (h : a ∈ s.map (embedding.subtype _)) : p a := begin rcases mem_map.1 h with ⟨x, hx, rfl⟩, exact x.2 end /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, the result does not contain any value that does not satisfy the property of the subtype. -/ lemma not_mem_map_subtype_of_not_property {p : Ξ± β†’ Prop} (s : finset {x // p x}) {a : Ξ±} (h : Β¬ p a) : a βˆ‰ (s.map (embedding.subtype _)) := mt s.property_of_mem_map_subtype h /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, the result is a subset of the set giving the subtype. -/ lemma map_subtype_subset {t : set Ξ±} (s : finset t) : ↑(s.map (embedding.subtype _)) βŠ† t := begin intros a ha, rw mem_coe at ha, convert property_of_mem_map_subtype s ha end lemma subset_image_iff {f : Ξ± β†’ Ξ²} {s : finset Ξ²} {t : set Ξ±} : ↑s βŠ† f '' t ↔ βˆƒs' : finset Ξ±, ↑s' βŠ† t ∧ s'.image f = s := begin classical, split, swap, { rintro ⟨s, hs, rfl⟩, rw [coe_image], exact set.image_subset f hs }, intro h, induction s using finset.induction with a s has ih h, { refine βŸ¨βˆ…, set.empty_subset _, _⟩, convert finset.image_empty _ }, rw [finset.coe_insert, set.insert_subset] at h, rcases ih h.2 with ⟨s', hst, hsi⟩, rcases h.1 with ⟨x, hxt, rfl⟩, refine ⟨insert x s', _, _⟩, { rw [finset.coe_insert, set.insert_subset], exact ⟨hxt, hst⟩ }, rw [finset.image_insert, hsi], congr end end image end finset theorem multiset.to_finset_map [decidable_eq Ξ±] [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) (m : multiset Ξ±) : (m.map f).to_finset = m.to_finset.image f := finset.val_inj.1 (multiset.erase_dup_map_erase_dup_eq _ _).symm namespace finset /-! ### card -/ section card /-- `card s` is the cardinality (number of elements) of `s`. -/ def card (s : finset Ξ±) : nat := s.1.card theorem card_def (s : finset Ξ±) : s.card = s.1.card := rfl @[simp] lemma card_mk {m nodup} : (⟨m, nodup⟩ : finset Ξ±).card = m.card := rfl @[simp] theorem card_empty : card (βˆ… : finset Ξ±) = 0 := rfl @[simp] theorem card_eq_zero {s : finset Ξ±} : card s = 0 ↔ s = βˆ… := card_eq_zero.trans val_eq_zero theorem card_pos {s : finset Ξ±} : 0 < card s ↔ s.nonempty := pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm theorem card_ne_zero_of_mem {s : finset Ξ±} {a : Ξ±} (h : a ∈ s) : card s β‰  0 := (not_congr card_eq_zero).2 (ne_empty_of_mem h) theorem card_eq_one {s : finset Ξ±} : s.card = 1 ↔ βˆƒ a, s = {a} := by cases s; simp only [multiset.card_eq_one, finset.card, ← val_inj, singleton_val] @[simp] theorem card_insert_of_not_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} (h : a βˆ‰ s) : card (insert a s) = card s + 1 := by simpa only [card_cons, card, insert_val] using congr_arg multiset.card (ndinsert_of_not_mem h) theorem card_insert_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} (h : a ∈ s) : card (insert a s) = card s := by rw insert_eq_of_mem h theorem card_insert_le [decidable_eq Ξ±] (a : Ξ±) (s : finset Ξ±) : card (insert a s) ≀ card s + 1 := by by_cases a ∈ s; [{rw [insert_eq_of_mem h], apply nat.le_add_right}, rw [card_insert_of_not_mem h]] @[simp] theorem card_singleton (a : Ξ±) : card ({a} : finset Ξ±) = 1 := card_singleton _ lemma card_singleton_inter [decidable_eq Ξ±] {x : Ξ±} {s : finset Ξ±} : ({x} ∩ s).card ≀ 1 := begin cases (finset.decidable_mem x s), { simp [finset.singleton_inter_of_not_mem h] }, { simp [finset.singleton_inter_of_mem h] }, end theorem card_erase_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : a ∈ s β†’ card (erase s a) = pred (card s) := card_erase_of_mem theorem card_erase_lt_of_mem [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : a ∈ s β†’ card (erase s a) < card s := card_erase_lt_of_mem theorem card_erase_le [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : card (erase s a) ≀ card s := card_erase_le theorem pred_card_le_card_erase [decidable_eq Ξ±] {a : Ξ±} {s : finset Ξ±} : card s - 1 ≀ card (erase s a) := begin by_cases h : a ∈ s, { rw [card_erase_of_mem h], refl }, { rw [erase_eq_of_not_mem h], apply nat.sub_le } end @[simp] theorem card_range (n : β„•) : card (range n) = n := card_range n @[simp] theorem card_attach {s : finset Ξ±} : card (attach s) = card s := multiset.card_attach end card end finset theorem multiset.to_finset_card_le [decidable_eq Ξ±] (m : multiset Ξ±) : m.to_finset.card ≀ m.card := card_le_of_le (erase_dup_le _) theorem list.to_finset_card_le [decidable_eq Ξ±] (l : list Ξ±) : l.to_finset.card ≀ l.length := multiset.to_finset_card_le ⟦l⟧ namespace finset section card theorem card_image_le [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} : card (image f s) ≀ card s := by simpa only [card_map] using (s.1.map f).to_finset_card_le theorem card_image_of_inj_on [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} (H : βˆ€x∈s, βˆ€y∈s, f x = f y β†’ x = y) : card (image f s) = card s := by simp only [card, image_val_of_inj_on H, card_map] theorem card_image_of_injective [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (s : finset Ξ±) (H : injective f) : card (image f s) = card s := card_image_of_inj_on $ Ξ» x _ y _ h, H h lemma fiber_card_ne_zero_iff_mem_image (s : finset Ξ±) (f : Ξ± β†’ Ξ²) [decidable_eq Ξ²] (y : Ξ²) : (s.filter (Ξ» x, f x = y)).card β‰  0 ↔ y ∈ s.image f := by { rw [←pos_iff_ne_zero, card_pos, fiber_nonempty_iff_mem_image] } @[simp] lemma card_map {Ξ± Ξ²} (f : Ξ± β†ͺ Ξ²) {s : finset Ξ±} : (s.map f).card = s.card := multiset.card_map _ _ @[simp] lemma card_subtype (p : Ξ± β†’ Prop) [decidable_pred p] (s : finset Ξ±) : (s.subtype p).card = (s.filter p).card := by simp [finset.subtype] lemma card_eq_of_bijective {s : finset Ξ±} {n : β„•} (f : βˆ€i, i < n β†’ Ξ±) (hf : βˆ€a∈s, βˆƒi, βˆƒh:i<n, f i h = a) (hf' : βˆ€i (h : i < n), f i h ∈ s) (f_inj : βˆ€i j (hi : i < n) (hj : j < n), f i hi = f j hj β†’ i = j) : card s = n := begin classical, have : βˆ€ (a : Ξ±), a ∈ s ↔ βˆƒi (hi : i ∈ range n), f i (mem_range.1 hi) = a, from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, assume ⟨i, hi, eq⟩, eq β–Έ hf' i (mem_range.1 hi)⟩, have : s = ((range n).attach.image $ Ξ»i, f i.1 (mem_range.1 i.2)), by simpa only [ext_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and], calc card s = card ((range n).attach.image $ Ξ»i, f i.1 (mem_range.1 i.2)) : by rw [this] ... = card ((range n).attach) : card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n end lemma card_eq_succ [decidable_eq Ξ±] {s : finset Ξ±} {n : β„•} : s.card = n + 1 ↔ (βˆƒa t, a βˆ‰ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have 0 < card s, from eq.symm β–Έ nat.zero_lt_succ _, let ⟨a, has⟩ := card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [eq, card_erase_of_mem has, pred_succ]⟩) (assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq β–Έ n_eq β–Έ card_insert_of_not_mem hat) theorem card_le_of_subset {s t : finset Ξ±} : s βŠ† t β†’ card s ≀ card t := multiset.card_le_of_le ∘ val_le_iff.mpr theorem eq_of_subset_of_card_le {s t : finset Ξ±} (h : s βŠ† t) (hβ‚‚ : card t ≀ card s) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) hβ‚‚ lemma card_lt_card {s t : finset Ξ±} (h : s βŠ‚ t) : s.card < t.card := card_lt_of_lt (val_lt_iff.2 h) lemma card_le_card_of_inj_on {s : finset Ξ±} {t : finset Ξ²} (f : Ξ± β†’ Ξ²) (hf : βˆ€a∈s, f a ∈ t) (f_inj : βˆ€aβ‚βˆˆs, βˆ€aβ‚‚βˆˆs, f a₁ = f aβ‚‚ β†’ a₁ = aβ‚‚) : card s ≀ card t := begin classical, calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj] ... ≀ card t : card_le_of_subset $ assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end end /-- If there are more pigeons than pigeonholes, then there are two pigeons in the same pigeonhole. -/ lemma exists_ne_map_eq_of_card_lt_of_maps_to {s : finset Ξ±} {t : finset Ξ²} (hc : t.card < s.card) {f : Ξ± β†’ Ξ²} (hf : βˆ€ a ∈ s, f a ∈ t) : βˆƒ (x ∈ s) (y ∈ s), x β‰  y ∧ f x = f y := begin classical, by_contra hz, push_neg at hz, refine hc.not_le (card_le_card_of_inj_on f hf _), intros x hx y hy, contrapose, exact hz x hx y hy, end lemma card_le_of_inj_on {n} {s : finset Ξ±} (f : β„• β†’ Ξ±) (hf : βˆ€i<n, f i ∈ s) (f_inj : βˆ€i j, i<n β†’ j<n β†’ f i = f j β†’ i = j) : n ≀ card s := calc n = card (range n) : (card_range n).symm ... ≀ card s : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simp only [mem_range]; exact assume a₁ h₁ aβ‚‚ hβ‚‚, f_inj a₁ aβ‚‚ h₁ hβ‚‚) /-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to define an object on `s`. Then one can inductively define an object on all finsets, starting from the empty set and iterating. This can be used either to define data, or to prove properties. -/ @[elab_as_eliminator] def strong_induction_on {p : finset Ξ± β†’ Sort*} : βˆ€ (s : finset Ξ±), (βˆ€s, (βˆ€t βŠ‚ s, p t) β†’ p s) β†’ p s | ⟨s, nd⟩ ih := multiset.strong_induction_on s (Ξ» s IH nd, ih ⟨s, nd⟩ (Ξ» ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq Ξ±] {p : finset Ξ± β†’ Prop} (s : finset Ξ±) (hβ‚€ : p βˆ…) (h₁ : βˆ€ a s, a βˆ‰ s β†’ (βˆ€t βŠ† s, p t) β†’ p (insert a s)) : p s := finset.strong_induction_on s $ Ξ» s, finset.induction_on s (Ξ» _, hβ‚€) $ Ξ» a s n _ ih, h₁ a s n $ Ξ» t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) lemma card_congr {s : finset Ξ±} {t : finset Ξ²} (f : Ξ  a ∈ s, Ξ²) (h₁ : βˆ€ a ha, f a ha ∈ t) (hβ‚‚ : βˆ€ a b ha hb, f a ha = f b hb β†’ a = b) (h₃ : βˆ€ b ∈ t, βˆƒ a ha, f a ha = b) : s.card = t.card := by haveI := classical.prop_decidable; exact calc s.card = s.attach.card : card_attach.symm ... = (s.attach.image (Ξ» (a : {a // a ∈ s}), f a.1 a.2)).card : eq.symm (card_image_of_injective _ (Ξ» a b h, subtype.eq (hβ‚‚ _ _ _ _ h))) ... = t.card : congr_arg card (finset.ext $ Ξ» b, ⟨λ h, let ⟨a, ha₁, haβ‚‚βŸ© := mem_image.1 h in haβ‚‚ β–Έ h₁ _ _, Ξ» h, let ⟨a, ha₁, haβ‚‚βŸ© := h₃ b h in mem_image.2 ⟨⟨a, haβ‚βŸ©, by simp [haβ‚‚]⟩⟩) lemma card_union_add_card_inter [decidable_eq Ξ±] (s t : finset Ξ±) : (s βˆͺ t).card + (s ∩ t).card = s.card + t.card := finset.induction_on t (by simp) $ Ξ» a r har, by by_cases a ∈ s; simp *; cc lemma card_union_le [decidable_eq Ξ±] (s t : finset Ξ±) : (s βˆͺ t).card ≀ s.card + t.card := card_union_add_card_inter s t β–Έ le_add_right _ _ lemma card_union_eq [decidable_eq Ξ±] {s t : finset Ξ±} (h : disjoint s t) : (s βˆͺ t).card = s.card + t.card := begin rw [← card_union_add_card_inter], convert (add_zero _).symm, rw [card_eq_zero], rwa [disjoint_iff] at h end lemma surj_on_of_inj_on_of_card_le {s : finset Ξ±} {t : finset Ξ²} (f : Ξ  a ∈ s, Ξ²) (hf : βˆ€ a ha, f a ha ∈ t) (hinj : βˆ€ a₁ aβ‚‚ ha₁ haβ‚‚, f a₁ ha₁ = f aβ‚‚ haβ‚‚ β†’ a₁ = aβ‚‚) (hst : card t ≀ card s) : (βˆ€ b ∈ t, βˆƒ a ha, b = f a ha) := by haveI := classical.dec_eq Ξ²; exact Ξ» b hb, have h : card (image (Ξ» (a : {a // a ∈ s}), f a a.prop) (attach s)) = card s, from @card_attach _ s β–Έ card_image_of_injective _ (Ξ» ⟨a₁, haβ‚βŸ© ⟨aβ‚‚, haβ‚‚βŸ© h, subtype.eq $ hinj _ _ _ _ h), have h₁ : image (Ξ» a : {a // a ∈ s}, f a a.prop) s.attach = t := eq_of_subset_of_card_le (Ξ» b h, let ⟨a, ha₁, haβ‚‚βŸ© := mem_image.1 h in haβ‚‚ β–Έ hf _ _) (by simp [hst, h]), begin rw ← h₁ at hb, rcases mem_image.1 hb with ⟨a, ha₁, haβ‚‚βŸ©, exact ⟨a, a.2, haβ‚‚.symm⟩, end open function lemma inj_on_of_surj_on_of_card_le {s : finset Ξ±} {t : finset Ξ²} (f : Ξ  a ∈ s, Ξ²) (hf : βˆ€ a ha, f a ha ∈ t) (hsurj : βˆ€ b ∈ t, βˆƒ a ha, b = f a ha) (hst : card s ≀ card t) ⦃a₁ a₂⦄ (ha₁ : a₁ ∈ s) (haβ‚‚ : aβ‚‚ ∈ s) (ha₁aβ‚‚: f a₁ ha₁ = f aβ‚‚ haβ‚‚) : a₁ = aβ‚‚ := by haveI : inhabited {x // x ∈ s} := ⟨⟨a₁, haβ‚βŸ©βŸ©; exact let f' : {x // x ∈ s} β†’ {x // x ∈ t} := Ξ» x, ⟨f x.1 x.2, hf x.1 x.2⟩ in let g : {x // x ∈ t} β†’ {x // x ∈ s} := @surj_inv _ _ f' (Ξ» x, let ⟨y, hy₁, hyβ‚‚βŸ© := hsurj x.1 x.2 in ⟨⟨y, hyβ‚βŸ©, subtype.eq hyβ‚‚.symm⟩) in have hg : injective g, from injective_surj_inv _, have hsg : surjective g, from Ξ» x, let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (Ξ» (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x) (Ξ» x _, show (g x) ∈ s.attach, from mem_attach _ _) (Ξ» x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in ⟨y, hy.snd.symm⟩, have hif : injective f', from (left_inverse_of_surjective_of_right_inverse hsg (right_inverse_surj_inv _)).injective, subtype.ext_iff_val.1 (@hif ⟨a₁, haβ‚βŸ© ⟨aβ‚‚, haβ‚‚βŸ© (subtype.eq ha₁aβ‚‚)) end card section bUnion /-! ### bUnion This section is about the bounded union of an indexed family `t : Ξ± β†’ finset Ξ²` of finite sets over a finite set `s : finset Ξ±`. -/ variables [decidable_eq Ξ²] {s : finset Ξ±} {t : Ξ± β†’ finset Ξ²} /-- `bUnion s t` is the union of `t x` over `x ∈ s`. (This was formerly `bind` due to the monad structure on types with `decidable_eq`.) -/ protected def bUnion (s : finset Ξ±) (t : Ξ± β†’ finset Ξ²) : finset Ξ² := (s.1.bind (Ξ» a, (t a).1)).to_finset @[simp] theorem bUnion_val (s : finset Ξ±) (t : Ξ± β†’ finset Ξ²) : (s.bUnion t).1 = (s.1.bind (Ξ» a, (t a).1)).erase_dup := rfl @[simp] theorem bUnion_empty : finset.bUnion βˆ… t = βˆ… := rfl @[simp] theorem mem_bUnion {b : Ξ²} : b ∈ s.bUnion t ↔ βˆƒa∈s, b ∈ t a := by simp only [mem_def, bUnion_val, mem_erase_dup, mem_bind, exists_prop] @[simp] theorem bUnion_insert [decidable_eq Ξ±] {a : Ξ±} : (insert a s).bUnion t = t a βˆͺ s.bUnion t := ext $ Ξ» x, by simp only [mem_bUnion, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext $ Ξ» x, by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bUnion {a : Ξ±} : finset.bUnion {a} t = t a := begin classical, rw [← insert_emptyc_eq, bUnion_insert, bUnion_empty, union_empty] end theorem bUnion_inter (s : finset Ξ±) (f : Ξ± β†’ finset Ξ²) (t : finset Ξ²) : s.bUnion f ∩ t = s.bUnion (Ξ» x, f x ∩ t) := begin ext x, simp only [mem_bUnion, mem_inter], tauto end theorem inter_bUnion (t : finset Ξ²) (s : finset Ξ±) (f : Ξ± β†’ finset Ξ²) : t ∩ s.bUnion f = s.bUnion (Ξ» x, t ∩ f x) := by rw [inter_comm, bUnion_inter]; simp [inter_comm] theorem image_bUnion [decidable_eq Ξ³] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} {t : Ξ² β†’ finset Ξ³} : (s.image f).bUnion t = s.bUnion (Ξ»a, t (f a)) := by haveI := classical.dec_eq Ξ±; exact finset.induction_on s rfl (Ξ» a s has ih, by simp only [image_insert, bUnion_insert, ih]) theorem bUnion_image [decidable_eq Ξ³] {s : finset Ξ±} {t : Ξ± β†’ finset Ξ²} {f : Ξ² β†’ Ξ³} : (s.bUnion t).image f = s.bUnion (Ξ»a, (t a).image f) := by haveI := classical.dec_eq Ξ±; exact finset.induction_on s rfl (Ξ» a s has ih, by simp only [bUnion_insert, image_union, ih]) theorem bind_to_finset [decidable_eq Ξ±] (s : multiset Ξ±) (t : Ξ± β†’ multiset Ξ²) : (s.bind t).to_finset = s.to_finset.bUnion (Ξ»a, (t a).to_finset) := ext $ Ξ» x, by simp only [multiset.mem_to_finset, mem_bUnion, multiset.mem_bind, exists_prop] lemma bUnion_mono {t₁ tβ‚‚ : Ξ± β†’ finset Ξ²} (h : βˆ€a∈s, t₁ a βŠ† tβ‚‚ a) : s.bUnion t₁ βŠ† s.bUnion tβ‚‚ := have βˆ€b a, a ∈ s β†’ b ∈ t₁ a β†’ (βˆƒ (a : Ξ±), a ∈ s ∧ b ∈ tβ‚‚ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa only [subset_iff, mem_bUnion, exists_imp_distrib, and_imp, exists_prop] lemma bUnion_subset_bUnion_of_subset_left {Ξ± : Type*} {s₁ sβ‚‚ : finset Ξ±} (t : Ξ± β†’ finset Ξ²) (h : s₁ βŠ† sβ‚‚) : s₁.bUnion t βŠ† sβ‚‚.bUnion t := begin intro x, simp only [and_imp, mem_bUnion, exists_prop], exact Exists.imp (Ξ» a ha, ⟨h ha.1, ha.2⟩) end lemma bUnion_singleton {f : Ξ± β†’ Ξ²} : s.bUnion (Ξ»a, {f a}) = s.image f := ext $ Ξ» x, by simp only [mem_bUnion, mem_image, mem_singleton, eq_comm] @[simp] lemma bUnion_singleton_eq_self [decidable_eq Ξ±] : s.bUnion (singleton : Ξ± β†’ finset Ξ±) = s := by { rw bUnion_singleton, exact image_id } lemma bUnion_filter_eq_of_maps_to [decidable_eq Ξ±] {s : finset Ξ±} {t : finset Ξ²} {f : Ξ± β†’ Ξ²} (h : βˆ€ x ∈ s, f x ∈ t) : t.bUnion (Ξ»a, s.filter $ (Ξ»c, f c = a)) = s := begin ext b, suffices : (βˆƒ a ∈ t, b ∈ s ∧ f b = a) ↔ b ∈ s, by simpa, exact ⟨λ ⟨a, ha, hb, hab⟩, hb, Ξ» hb, ⟨f b, h b hb, hb, rfl⟩⟩ end lemma image_bUnion_filter_eq [decidable_eq Ξ±] (s : finset Ξ²) (g : Ξ² β†’ Ξ±) : (s.image g).bUnion (Ξ»a, s.filter $ (Ξ»c, g c = a)) = s := bUnion_filter_eq_of_maps_to (Ξ» x, mem_image_of_mem g) end bUnion /-! ### prod -/ section prod variables {s : finset Ξ±} {t : finset Ξ²} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset Ξ±) (t : finset Ξ²) : finset (Ξ± Γ— Ξ²) := ⟨_, nodup_product s.2 t.2⟩ @[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] theorem mem_product {p : Ξ± Γ— Ξ²} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product theorem subset_product [decidable_eq Ξ±] [decidable_eq Ξ²] {s : finset (Ξ± Γ— Ξ²)} : s βŠ† (s.image prod.fst).product (s.image prod.snd) := Ξ» p hp, mem_product.2 ⟨mem_image_of_mem _ hp, mem_image_of_mem _ hp⟩ theorem product_eq_bUnion [decidable_eq Ξ±] [decidable_eq Ξ²] (s : finset Ξ±) (t : finset Ξ²) : s.product t = s.bUnion (Ξ»a, t.image $ Ξ»b, (a, b)) := ext $ Ξ» ⟨x, y⟩, by simp only [mem_product, mem_bUnion, mem_image, exists_prop, prod.mk.inj_iff, and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left] @[simp] theorem card_product (s : finset Ξ±) (t : finset Ξ²) : card (s.product t) = card s * card t := multiset.card_product _ _ theorem filter_product (p : Ξ± β†’ Prop) (q : Ξ² β†’ Prop) [decidable_pred p] [decidable_pred q] : (s.product t).filter (Ξ» (x : Ξ± Γ— Ξ²), p x.1 ∧ q x.2) = (s.filter p).product (t.filter q) := by { ext ⟨a, b⟩, simp only [mem_filter, mem_product], finish, } lemma filter_product_card (s : finset Ξ±) (t : finset Ξ²) (p : Ξ± β†’ Prop) (q : Ξ² β†’ Prop) [decidable_pred p] [decidable_pred q] : ((s.product t).filter (Ξ» (x : Ξ± Γ— Ξ²), p x.1 ↔ q x.2)).card = (s.filter p).card * (t.filter q).card + (s.filter (not ∘ p)).card * (t.filter (not ∘ q)).card := begin classical, rw [← card_product, ← card_product, ← filter_product, ← filter_product, ← card_union_eq], { apply congr_arg, ext ⟨a, b⟩, simp only [filter_union_right, mem_filter, mem_product], split; intros; finish, }, { rw disjoint_iff, change _ ∩ _ = βˆ…, ext ⟨a, b⟩, rw mem_inter, finish, }, end end prod /-! ### sigma -/ section sigma variables {Οƒ : Ξ± β†’ Type*} {s : finset Ξ±} {t : Ξ a, finset (Οƒ a)} /-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/ protected def sigma (s : finset Ξ±) (t : Ξ a, finset (Οƒ a)) : finset (Ξ£a, Οƒ a) := ⟨_, nodup_sigma s.2 (Ξ» a, (t a).2)⟩ @[simp] theorem mem_sigma {p : sigma Οƒ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma theorem sigma_mono {s₁ sβ‚‚ : finset Ξ±} {t₁ tβ‚‚ : Ξ a, finset (Οƒ a)} (H1 : s₁ βŠ† sβ‚‚) (H2 : βˆ€a, t₁ a βŠ† tβ‚‚ a) : s₁.sigma t₁ βŠ† sβ‚‚.sigma tβ‚‚ := Ξ» ⟨x, sx⟩ H, let ⟨H3, H4⟩ := mem_sigma.1 H in mem_sigma.2 ⟨H1 H3, H2 x H4⟩ theorem sigma_eq_bUnion [decidable_eq (Ξ£ a, Οƒ a)] (s : finset Ξ±) (t : Ξ a, finset (Οƒ a)) : s.sigma t = s.bUnion (Ξ»a, (t a).map $ embedding.sigma_mk a) := by { ext ⟨x, y⟩, simp [and.left_comm] } end sigma /-! ### disjoint -/ section disjoint variable [decidable_eq Ξ±] theorem disjoint_left {s t : finset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ s β†’ a βˆ‰ t := by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and, and_imp]; refl theorem disjoint_val {s t : finset Ξ±} : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left theorem disjoint_iff_inter_eq_empty {s t : finset Ξ±} : disjoint s t ↔ s ∩ t = βˆ… := disjoint_iff instance decidable_disjoint (U V : finset Ξ±) : decidable (disjoint U V) := decidable_of_decidable_of_iff (by apply_instance) eq_bot_iff theorem disjoint_right {s t : finset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ t β†’ a βˆ‰ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_iff_ne {s t : finset Ξ±} : disjoint s t ↔ βˆ€ a ∈ s, βˆ€ b ∈ t, a β‰  b := by simp only [disjoint_left, imp_not_comm, forall_eq'] theorem disjoint_of_subset_left {s t u : finset Ξ±} (h : s βŠ† u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (Ξ» x m₁, (disjoint_left.1 d) (h m₁)) theorem disjoint_of_subset_right {s t u : finset Ξ±} (h : t βŠ† u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (Ξ» x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset Ξ±) : disjoint βˆ… s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset Ξ±) : disjoint s βˆ… := disjoint_bot_right @[simp] theorem singleton_disjoint {s : finset Ξ±} {a : Ξ±} : disjoint (singleton a) s ↔ a βˆ‰ s := by simp only [disjoint_left, mem_singleton, forall_eq] @[simp] theorem disjoint_singleton {s : finset Ξ±} {a : Ξ±} : disjoint s (singleton a) ↔ a βˆ‰ s := disjoint.comm.trans singleton_disjoint @[simp] theorem disjoint_insert_left {a : Ξ±} {s t : finset Ξ±} : disjoint (insert a s) t ↔ a βˆ‰ t ∧ disjoint s t := by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem disjoint_insert_right {a : Ξ±} {s t : finset Ξ±} : disjoint s (insert a t) ↔ a βˆ‰ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] @[simp] theorem disjoint_union_left {s t u : finset Ξ±} : disjoint (s βˆͺ t) u ↔ disjoint s u ∧ disjoint t u := by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right {s t u : finset Ξ±} : disjoint s (t βˆͺ u) ↔ disjoint s t ∧ disjoint s u := by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib] lemma sdiff_disjoint {s t : finset Ξ±} : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2 lemma disjoint_sdiff {s t : finset Ξ±} : disjoint s (t \ s) := sdiff_disjoint.symm lemma disjoint_sdiff_inter (s t : finset Ξ±) : disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint lemma sdiff_eq_self_iff_disjoint {s t : finset Ξ±} : s \ t = s ↔ disjoint s t := by rw [sdiff_eq_self, subset_empty, disjoint_iff_inter_eq_empty] lemma sdiff_eq_self_of_disjoint {s t : finset Ξ±} (h : disjoint s t) : s \ t = s := sdiff_eq_self_iff_disjoint.2 h lemma disjoint_self_iff_empty (s : finset Ξ±) : disjoint s s ↔ s = βˆ… := disjoint_self lemma disjoint_bUnion_left {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ finset Ξ±) (t : finset Ξ±) : disjoint (s.bUnion f) t ↔ (βˆ€i∈s, disjoint (f i) t) := begin classical, refine s.induction _ _, { simp only [forall_mem_empty_iff, bUnion_empty, disjoint_empty_left] }, { assume i s his ih, simp only [disjoint_union_left, bUnion_insert, his, forall_mem_insert, ih] } end lemma disjoint_bUnion_right {ΞΉ : Type*} (s : finset Ξ±) (t : finset ΞΉ) (f : ΞΉ β†’ finset Ξ±) : disjoint s (t.bUnion f) ↔ (βˆ€i∈t, disjoint s (f i)) := by simpa only [disjoint.comm] using disjoint_bUnion_left t f s @[simp] theorem card_disjoint_union {s t : finset Ξ±} (h : disjoint s t) : card (s βˆͺ t) = card s + card t := by rw [← card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero] theorem card_sdiff {s t : finset Ξ±} (h : s βŠ† t) : card (t \ s) = card t - card s := suffices card (t \ s) = card ((t \ s) βˆͺ s) - card s, by rwa sdiff_union_of_subset h at this, by rw [card_disjoint_union sdiff_disjoint, nat.add_sub_cancel] lemma disjoint_filter {s : finset Ξ±} {p q : Ξ± β†’ Prop} [decidable_pred p] [decidable_pred q] : disjoint (s.filter p) (s.filter q) ↔ (βˆ€ x ∈ s, p x β†’ Β¬ q x) := by split; simp [disjoint_left] {contextual := tt} lemma disjoint_filter_filter {s t : finset Ξ±} {p q : Ξ± β†’ Prop} [decidable_pred p] [decidable_pred q] : (disjoint s t) β†’ disjoint (s.filter p) (t.filter q) := disjoint.mono (filter_subset _ _) (filter_subset _ _) lemma disjoint_iff_disjoint_coe {Ξ± : Type*} {a b : finset Ξ±} [decidable_eq Ξ±] : disjoint a b ↔ disjoint (↑a : set Ξ±) (↑b : set Ξ±) := by { rw [finset.disjoint_left, set.disjoint_left], refl } lemma filter_card_add_filter_neg_card_eq_card {Ξ± : Type*} {s : finset Ξ±} (p : Ξ± β†’ Prop) [decidable_pred p] : (s.filter p).card + (s.filter (not ∘ p)).card = s.card := by { classical, simp [← card_union_eq, filter_union_filter_neg_eq, disjoint_filter], } end disjoint section self_prod variables (s : finset Ξ±) [decidable_eq Ξ±] /-- Given a finite set `s`, the diagonal, `s.diag` is the set of pairs of the form `(a, a)` for `a ∈ s`. -/ def diag := (s.product s).filter (Ξ» (a : Ξ± Γ— Ξ±), a.fst = a.snd) /-- Given a finite set `s`, the off-diagonal, `s.off_diag` is the set of pairs `(a, b)` with `a β‰  b` for `a, b ∈ s`. -/ def off_diag := (s.product s).filter (Ξ» (a : Ξ± Γ— Ξ±), a.fst β‰  a.snd) @[simp] lemma mem_diag (x : Ξ± Γ— Ξ±) : x ∈ s.diag ↔ x.1 ∈ s ∧ x.1 = x.2 := by { simp only [diag, mem_filter, mem_product], split; intros; finish, } @[simp] lemma mem_off_diag (x : Ξ± Γ— Ξ±) : x ∈ s.off_diag ↔ x.1 ∈ s ∧ x.2 ∈ s ∧ x.1 β‰  x.2 := by { simp only [off_diag, mem_filter, mem_product], split; intros; finish, } @[simp] lemma diag_card : (diag s).card = s.card := begin suffices : diag s = s.image (Ξ» a, (a, a)), { rw this, apply card_image_of_inj_on, finish, }, ext ⟨a₁, aβ‚‚βŸ©, rw mem_diag, split; intros; finish, end @[simp] lemma off_diag_card : (off_diag s).card = s.card * s.card - s.card := begin suffices : (diag s).card + (off_diag s).card = s.card * s.card, { nth_rewrite 2 ← s.diag_card, finish, }, rw ← card_product, apply filter_card_add_filter_neg_card_eq_card, end end self_prod /-- Given a set A and a set B inside it, we can shrink A to any appropriate size, and keep B inside it. -/ lemma exists_intermediate_set {A B : finset Ξ±} (i : β„•) (h₁ : i + card B ≀ card A) (hβ‚‚ : B βŠ† A) : βˆƒ (C : finset Ξ±), B βŠ† C ∧ C βŠ† A ∧ card C = i + card B := begin classical, rcases nat.le.dest h₁ with ⟨k, _⟩, clear h₁, induction k with k ih generalizing A, { exact ⟨A, hβ‚‚, subset.refl _, h.symm⟩ }, { have : (A \ B).nonempty, { rw [← card_pos, card_sdiff hβ‚‚, ← h, nat.add_right_comm, nat.add_sub_cancel, nat.add_succ], apply nat.succ_pos }, rcases this with ⟨a, ha⟩, have z : i + card B + k = card (erase A a), { rw [card_erase_of_mem, ← h, nat.add_succ, nat.pred_succ], rw mem_sdiff at ha, exact ha.1 }, rcases ih _ z with ⟨B', hB', B'subA', cards⟩, { exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ }, { rintros t th, apply mem_erase_of_ne_of_mem _ (hβ‚‚ th), rintro rfl, exact not_mem_sdiff_of_mem_right th ha } } end /-- We can shrink A to any smaller size. -/ lemma exists_smaller_set (A : finset Ξ±) (i : β„•) (h₁ : i ≀ card A) : βˆƒ (B : finset Ξ±), B βŠ† A ∧ card B = i := let ⟨B, _, x₁, xβ‚‚βŸ© := exists_intermediate_set i (by simpa) (empty_subset A) in ⟨B, x₁, xβ‚‚βŸ© /-- `finset.fin_range k` is the finset `{0, 1, ..., k-1}`, as a `finset (fin k)`. -/ def fin_range (k : β„•) : finset (fin k) := ⟨list.fin_range k, list.nodup_fin_range k⟩ @[simp] lemma fin_range_card {k : β„•} : (fin_range k).card = k := by simp [fin_range] @[simp] lemma mem_fin_range {k : β„•} (m : fin k) : m ∈ fin_range k := list.mem_fin_range m @[simp] lemma coe_fin_range (k : β„•) : (fin_range k : set (fin k)) = set.univ := set.eq_univ_of_forall mem_fin_range /-- Given a finset `s` of `β„•` contained in `{0,..., n-1}`, the corresponding finset in `fin n` is `s.attach_fin h` where `h` is a proof that all elements of `s` are less than `n`. -/ def attach_fin (s : finset β„•) {n : β„•} (h : βˆ€ m ∈ s, m < n) : finset (fin n) := ⟨s.1.pmap (Ξ» a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (Ξ» _ _ _ _, fin.veq_of_eq) s.2⟩ @[simp] lemma mem_attach_fin {n : β„•} {s : finset β„•} (h : βˆ€ m ∈ s, m < n) {a : fin n} : a ∈ s.attach_fin h ↔ (a : β„•) ∈ s := ⟨λ h, let ⟨b, hb₁, hbβ‚‚βŸ© := multiset.mem_pmap.1 h in hbβ‚‚ β–Έ hb₁, Ξ» h, multiset.mem_pmap.2 ⟨a, h, fin.eta _ _⟩⟩ @[simp] lemma card_attach_fin {n : β„•} (s : finset β„•) (h : βˆ€ m ∈ s, m < n) : (s.attach_fin h).card = s.card := multiset.card_pmap _ _ _ /-! ### choose -/ section choose variables (p : Ξ± β†’ Prop) [decidable_pred p] (l : finset Ξ±) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : (βˆƒ! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : βˆƒ! a, a ∈ l ∧ p a) : Ξ± := choose_x p l hp lemma choose_spec (hp : βˆƒ! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : βˆƒ! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : βˆƒ! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose theorem lt_wf {Ξ±} : well_founded (@has_lt.lt (finset Ξ±) _) := have H : subrelation (@has_lt.lt (finset Ξ±) _) (inv_image (<) card), from Ξ» x y hxy, card_lt_card hxy, subrelation.wf H $ inv_image.wf _ $ nat.lt_wf end finset namespace equiv /-- Given an equivalence `Ξ±` to `Ξ²`, produce an equivalence between `finset Ξ±` and `finset Ξ²`. -/ protected def finset_congr (e : Ξ± ≃ Ξ²) : finset Ξ± ≃ finset Ξ² := { to_fun := Ξ» s, s.map e.to_embedding, inv_fun := Ξ» s, s.map e.symm.to_embedding, left_inv := Ξ» s, by simp [finset.map_map], right_inv := Ξ» s, by simp [finset.map_map] } @[simp] lemma finset_congr_apply (e : Ξ± ≃ Ξ²) (s : finset Ξ±) : e.finset_congr s = s.map e.to_embedding := rfl @[simp] lemma finset_congr_symm_apply (e : Ξ± ≃ Ξ²) (s : finset Ξ²) : e.finset_congr.symm s = s.map e.symm.to_embedding := rfl end equiv namespace list variable [decidable_eq Ξ±] theorem to_finset_card_of_nodup {l : list Ξ±} (h : l.nodup) : l.to_finset.card = l.length := congr_arg card $ (@multiset.erase_dup_eq_self Ξ± _ l).2 h end list namespace multiset variable [decidable_eq Ξ±] theorem to_finset_card_of_nodup {l : multiset Ξ±} (h : l.nodup) : l.to_finset.card = l.card := congr_arg card $ (@multiset.erase_dup_eq_self Ξ± _ l).2 h lemma disjoint_to_finset (m1 m2 : multiset Ξ±) : _root_.disjoint m1.to_finset m2.to_finset ↔ m1.disjoint m2 := begin rw finset.disjoint_iff_ne, split, { intro h, intros a ha1 ha2, rw ← multiset.mem_to_finset at ha1 ha2, exact h _ ha1 _ ha2 rfl }, { rintros h a ha b hb rfl, rw multiset.mem_to_finset at ha hb, exact h ha hb } end end multiset
8ebe5b5d4f42015a92acc35313d3ec697fcd3ed9
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/calculus/iterated_deriv_auto.lean
41b3501a1b57b123be26a5235cb79f3d63ab81b5
[]
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
15,954
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.deriv import Mathlib.analysis.calculus.times_cont_diff import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # One-dimensional iterated derivatives We define the `n`-th derivative of a function `f : π•œ β†’ F` as a function `iterated_deriv n f : π•œ β†’ F`, as well as a version on domains `iterated_deriv_within n f s : π•œ β†’ F`, and prove their basic properties. ## Main definitions and results Let `π•œ` be a nondiscrete normed field, and `F` a normed vector space over `π•œ`. Let `f : π•œ β†’ F`. * `iterated_deriv n f` is the `n`-th derivative of `f`, seen as a function from `π•œ` to `F`. It is defined as the `n`-th FrΓ©chet derivative (which is a multilinear map) applied to the vector `(1, ..., 1)`, to take advantage of all the existing framework, but we show that it coincides with the naive iterative definition. * `iterated_deriv_eq_iterate` states that the `n`-th derivative of `f` is obtained by starting from `f` and differentiating it `n` times. * `iterated_deriv_within n f s` is the `n`-th derivative of `f` within the domain `s`. It only behaves well when `s` has the unique derivative property. * `iterated_deriv_within_eq_iterate` states that the `n`-th derivative of `f` in the domain `s` is obtained by starting from `f` and differentiating it `n` times within `s`. This only holds when `s` has the unique derivative property. ## Implementation details The results are deduced from the corresponding results for the more general (multilinear) iterated FrΓ©chet derivative. For this, we write `iterated_deriv n f` as the composition of `iterated_fderiv π•œ n f` and a continuous linear equiv. As continuous linear equivs respect differentiability and commute with differentiation, this makes it possible to prove readily that the derivative of the `n`-th derivative is the `n+1`-th derivative in `iterated_deriv_within_succ`, by translating the corresponding result `iterated_fderiv_within_succ_apply_left` for the iterated FrΓ©chet derivative. -/ /-- The `n`-th iterated derivative of a function from `π•œ` to `F`, as a function from `π•œ` to `F`. -/ def iterated_deriv {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] (n : β„•) (f : π•œ β†’ F) (x : π•œ) : F := coe_fn (iterated_fderiv π•œ n f x) fun (i : fin n) => 1 /-- The `n`-th iterated derivative of a function from `π•œ` to `F` within a set `s`, as a function from `π•œ` to `F`. -/ def iterated_deriv_within {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] (n : β„•) (f : π•œ β†’ F) (s : set π•œ) (x : π•œ) : F := coe_fn (iterated_fderiv_within π•œ n f s x) fun (i : fin n) => 1 theorem iterated_deriv_within_univ {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} : iterated_deriv_within n f set.univ = iterated_deriv n f := sorry /-! ### Properties of the iterated derivative within a set -/ theorem iterated_deriv_within_eq_iterated_fderiv_within {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} {s : set π•œ} {x : π•œ} : iterated_deriv_within n f s x = coe_fn (iterated_fderiv_within π•œ n f s x) fun (i : fin n) => 1 := rfl /-- Write the iterated derivative as the composition of a continuous linear equiv and the iterated FrΓ©chet derivative -/ theorem iterated_deriv_within_eq_equiv_comp {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} {s : set π•œ} : iterated_deriv_within n f s = ⇑(continuous_linear_equiv.symm (continuous_multilinear_map.pi_field_equiv π•œ (fin n) F)) ∘ iterated_fderiv_within π•œ n f s := funext fun (x : π•œ) => Eq.refl (iterated_deriv_within n f s x) /-- Write the iterated FrΓ©chet derivative as the composition of a continuous linear equiv and the iterated derivative. -/ theorem iterated_fderiv_within_eq_equiv_comp {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} {s : set π•œ} : iterated_fderiv_within π•œ n f s = ⇑(continuous_multilinear_map.pi_field_equiv π•œ (fin n) F) ∘ iterated_deriv_within n f s := sorry /-- The `n`-th FrΓ©chet derivative applied to a vector `(m 0, ..., m (n-1))` is the derivative multiplied by the product of the `m i`s. -/ theorem iterated_fderiv_within_apply_eq_iterated_deriv_within_mul_prod {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} {s : set π•œ} {x : π•œ} {m : fin n β†’ π•œ} : coe_fn (iterated_fderiv_within π•œ n f s x) m = (finset.prod finset.univ fun (i : fin n) => m i) β€’ iterated_deriv_within n f s x := sorry @[simp] theorem iterated_deriv_within_zero {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {s : set π•œ} : iterated_deriv_within 0 f s = f := sorry @[simp] theorem iterated_deriv_within_one {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {s : set π•œ} (hs : unique_diff_on π•œ s) {x : π•œ} (hx : x ∈ s) : iterated_deriv_within 1 f s x = deriv_within f s x := sorry /-- If the first `n` derivatives within a set of a function are continuous, and its first `n-1` derivatives are differentiable, then the function is `C^n`. This is not an equivalence in general, but this is an equivalence when the set has unique derivatives, see `times_cont_diff_on_iff_continuous_on_differentiable_on_deriv`. -/ theorem times_cont_diff_on_of_continuous_on_differentiable_on_deriv {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {s : set π•œ} {n : with_top β„•} (Hcont : βˆ€ (m : β„•), ↑m ≀ n β†’ continuous_on (fun (x : π•œ) => iterated_deriv_within m f s x) s) (Hdiff : βˆ€ (m : β„•), ↑m < n β†’ differentiable_on π•œ (fun (x : π•œ) => iterated_deriv_within m f s x) s) : times_cont_diff_on π•œ n f s := sorry /-- To check that a function is `n` times continuously differentiable, it suffices to check that its first `n` derivatives are differentiable. This is slightly too strong as the condition we require on the `n`-th derivative is differentiability instead of continuity, but it has the advantage of avoiding the discussion of continuity in the proof (and for `n = ∞` this is optimal). -/ theorem times_cont_diff_on_of_differentiable_on_deriv {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {s : set π•œ} {n : with_top β„•} (h : βˆ€ (m : β„•), ↑m ≀ n β†’ differentiable_on π•œ (iterated_deriv_within m f s) s) : times_cont_diff_on π•œ n f s := sorry /-- On a set with unique derivatives, a `C^n` function has derivatives up to `n` which are continuous. -/ theorem times_cont_diff_on.continuous_on_iterated_deriv_within {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {s : set π•œ} {n : with_top β„•} {m : β„•} (h : times_cont_diff_on π•œ n f s) (hmn : ↑m ≀ n) (hs : unique_diff_on π•œ s) : continuous_on (iterated_deriv_within m f s) s := sorry /-- On a set with unique derivatives, a `C^n` function has derivatives less than `n` which are differentiable. -/ theorem times_cont_diff_on.differentiable_on_iterated_deriv_within {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {s : set π•œ} {n : with_top β„•} {m : β„•} (h : times_cont_diff_on π•œ n f s) (hmn : ↑m < n) (hs : unique_diff_on π•œ s) : differentiable_on π•œ (iterated_deriv_within m f s) s := sorry /-- The property of being `C^n`, initially defined in terms of the FrΓ©chet derivative, can be reformulated in terms of the one-dimensional derivative on sets with unique derivatives. -/ theorem times_cont_diff_on_iff_continuous_on_differentiable_on_deriv {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {s : set π•œ} {n : with_top β„•} (hs : unique_diff_on π•œ s) : times_cont_diff_on π•œ n f s ↔ (βˆ€ (m : β„•), ↑m ≀ n β†’ continuous_on (iterated_deriv_within m f s) s) ∧ βˆ€ (m : β„•), ↑m < n β†’ differentiable_on π•œ (iterated_deriv_within m f s) s := sorry /-- The `n+1`-th iterated derivative within a set with unique derivatives can be obtained by differentiating the `n`-th iterated derivative. -/ theorem iterated_deriv_within_succ {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} {s : set π•œ} {x : π•œ} (hxs : unique_diff_within_at π•œ s x) : iterated_deriv_within (n + 1) f s x = deriv_within (iterated_deriv_within n f s) s x := sorry /-- The `n`-th iterated derivative within a set with unique derivatives can be obtained by iterating `n` times the differentiation operation. -/ theorem iterated_deriv_within_eq_iterate {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} {s : set π•œ} {x : π•œ} (hs : unique_diff_on π•œ s) (hx : x ∈ s) : iterated_deriv_within n f s x = nat.iterate (fun (g : π•œ β†’ F) => deriv_within g s) n f x := sorry /-- The `n+1`-th iterated derivative within a set with unique derivatives can be obtained by taking the `n`-th derivative of the derivative. -/ theorem iterated_deriv_within_succ' {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} {s : set π•œ} {x : π•œ} (hxs : unique_diff_on π•œ s) (hx : x ∈ s) : iterated_deriv_within (n + 1) f s x = iterated_deriv_within n (deriv_within f s) s x := sorry /-! ### Properties of the iterated derivative on the whole space -/ theorem iterated_deriv_eq_iterated_fderiv {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} {x : π•œ} : iterated_deriv n f x = coe_fn (iterated_fderiv π•œ n f x) fun (i : fin n) => 1 := rfl /-- Write the iterated derivative as the composition of a continuous linear equiv and the iterated FrΓ©chet derivative -/ theorem iterated_deriv_eq_equiv_comp {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} : iterated_deriv n f = ⇑(continuous_linear_equiv.symm (continuous_multilinear_map.pi_field_equiv π•œ (fin n) F)) ∘ iterated_fderiv π•œ n f := funext fun (x : π•œ) => Eq.refl (iterated_deriv n f x) /-- Write the iterated FrΓ©chet derivative as the composition of a continuous linear equiv and the iterated derivative. -/ theorem iterated_fderiv_eq_equiv_comp {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} : iterated_fderiv π•œ n f = ⇑(continuous_multilinear_map.pi_field_equiv π•œ (fin n) F) ∘ iterated_deriv n f := sorry /-- The `n`-th FrΓ©chet derivative applied to a vector `(m 0, ..., m (n-1))` is the derivative multiplied by the product of the `m i`s. -/ theorem iterated_fderiv_apply_eq_iterated_deriv_mul_prod {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} {x : π•œ} {m : fin n β†’ π•œ} : coe_fn (iterated_fderiv π•œ n f x) m = (finset.prod finset.univ fun (i : fin n) => m i) β€’ iterated_deriv n f x := sorry @[simp] theorem iterated_deriv_zero {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} : iterated_deriv 0 f = f := sorry @[simp] theorem iterated_deriv_one {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} : iterated_deriv 1 f = deriv f := sorry /-- The property of being `C^n`, initially defined in terms of the FrΓ©chet derivative, can be reformulated in terms of the one-dimensional derivative. -/ theorem times_cont_diff_iff_iterated_deriv {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {n : with_top β„•} : times_cont_diff π•œ n f ↔ (βˆ€ (m : β„•), ↑m ≀ n β†’ continuous (iterated_deriv m f)) ∧ βˆ€ (m : β„•), ↑m < n β†’ differentiable π•œ (iterated_deriv m f) := sorry /-- To check that a function is `n` times continuously differentiable, it suffices to check that its first `n` derivatives are differentiable. This is slightly too strong as the condition we require on the `n`-th derivative is differentiability instead of continuity, but it has the advantage of avoiding the discussion of continuity in the proof (and for `n = ∞` this is optimal). -/ theorem times_cont_diff_of_differentiable_iterated_deriv {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {n : with_top β„•} (h : βˆ€ (m : β„•), ↑m ≀ n β†’ differentiable π•œ (iterated_deriv m f)) : times_cont_diff π•œ n f := iff.mpr times_cont_diff_iff_iterated_deriv { left := fun (m : β„•) (hm : ↑m ≀ n) => differentiable.continuous (h m hm), right := fun (m : β„•) (hm : ↑m < n) => h m (le_of_lt hm) } theorem times_cont_diff.continuous_iterated_deriv {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {n : with_top β„•} (m : β„•) (h : times_cont_diff π•œ n f) (hmn : ↑m ≀ n) : continuous (iterated_deriv m f) := and.left (iff.mp times_cont_diff_iff_iterated_deriv h) m hmn theorem times_cont_diff.differentiable_iterated_deriv {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {f : π•œ β†’ F} {n : with_top β„•} (m : β„•) (h : times_cont_diff π•œ n f) (hmn : ↑m < n) : differentiable π•œ (iterated_deriv m f) := and.right (iff.mp times_cont_diff_iff_iterated_deriv h) m hmn /-- The `n+1`-th iterated derivative can be obtained by differentiating the `n`-th iterated derivative. -/ theorem iterated_deriv_succ {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} : iterated_deriv (n + 1) f = deriv (iterated_deriv n f) := sorry /-- The `n`-th iterated derivative can be obtained by iterating `n` times the differentiation operation. -/ theorem iterated_deriv_eq_iterate {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} : iterated_deriv n f = nat.iterate deriv n f := sorry /-- The `n+1`-th iterated derivative can be obtained by taking the `n`-th derivative of the derivative. -/ theorem iterated_deriv_succ' {π•œ : Type u_1} [nondiscrete_normed_field π•œ] {F : Type u_2} [normed_group F] [normed_space π•œ F] {n : β„•} {f : π•œ β†’ F} : iterated_deriv (n + 1) f = iterated_deriv n (deriv f) := sorry end Mathlib
fe973a7ae39dc84152f89375f87386b475b39887
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/algebra/subalgebra/tower.lean
8edd9a4b26b9470b483b715cf51c7efe30ea3d1b
[ "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
4,406
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Anne Baanen -/ import algebra.algebra.subalgebra.basic import algebra.algebra.tower /-! # Subalgebras in towers of algebras In this file we prove facts about subalgebras in towers of algebra. An algebra tower A/S/R is expressed by having instances of `algebra A S`, `algebra R S`, `algebra R A` and `is_scalar_tower R S A`, the later asserting the compatibility condition `(r β€’ s) β€’ a = r β€’ (s β€’ a)`. ## Main results * `is_scalar_tower.subalgebra`: if `A/S/R` is a tower and `Sβ‚€` is a subalgebra between `S` and `R`, then `A/S/Sβ‚€` is a tower * `is_scalar_tower.subalgebra'`: if `A/S/R` is a tower and `Sβ‚€` is a subalgebra between `S` and `R`, then `A/Sβ‚€/R` is a tower * `subalgebra.restrict_scalars`: turn an `S`-subalgebra of `A` into an `R`-subalgebra of `A`, given that `A/S/R` is a tower -/ open_locale pointwise universes u v w u₁ v₁ variables (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) (M : Type v₁) namespace algebra variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables {A} lemma lmul_algebra_map (x : R) : algebra.lmul R A (algebra_map R A x) = algebra.lsmul R A x := eq.symm $ linear_map.ext $ smul_def x end algebra namespace is_scalar_tower section semiring variables [comm_semiring R] [comm_semiring S] [semiring A] variables [algebra R S] [algebra S A] variables (R S A) instance subalgebra (Sβ‚€ : subalgebra R S) : is_scalar_tower Sβ‚€ S A := of_algebra_map_eq $ Ξ» x, rfl variables [algebra R A] [is_scalar_tower R S A] instance subalgebra' (Sβ‚€ : subalgebra R S) : is_scalar_tower R Sβ‚€ A := @is_scalar_tower.of_algebra_map_eq R Sβ‚€ A _ _ _ _ _ _ $ Ξ» _, (is_scalar_tower.algebra_map_apply R S A _ : _) end semiring end is_scalar_tower namespace subalgebra open is_scalar_tower section semiring variables (R) {S A B} [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra R A] [algebra S B] [algebra R B] variables [is_scalar_tower R S A] [is_scalar_tower R S B] /-- Given a tower `A / β†₯U / S / R` of algebras, where `U` is an `S`-subalgebra of `A`, reinterpret `U` as an `R`-subalgebra of `A`. -/ def restrict_scalars (U : subalgebra S A) : subalgebra R A := { algebra_map_mem' := Ξ» x, by { rw algebra_map_apply R S A, exact U.algebra_map_mem _ }, .. U } @[simp] lemma coe_restrict_scalars {U : subalgebra S A} : (restrict_scalars R U : set A) = (U : set A) := rfl @[simp] lemma restrict_scalars_top : restrict_scalars R (⊀ : subalgebra S A) = ⊀ := set_like.coe_injective rfl @[simp] lemma restrict_scalars_to_submodule {U : subalgebra S A} : (U.restrict_scalars R).to_submodule = U.to_submodule.restrict_scalars R := set_like.coe_injective rfl @[simp] lemma mem_restrict_scalars {U : subalgebra S A} {x : A} : x ∈ restrict_scalars R U ↔ x ∈ U := iff.rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : subalgebra S A β†’ subalgebra R A) := Ξ» U V H, ext $ Ξ» x, by rw [← mem_restrict_scalars R, H, mem_restrict_scalars] /-- Produces an `R`-algebra map from `U.restrict_scalars R` given an `S`-algebra map from `U`. This is a special case of `alg_hom.restrict_scalars` that can be helpful in elaboration. -/ @[simp] def of_restrict_scalars (U : subalgebra S A) (f : U →ₐ[S] B) : U.restrict_scalars R →ₐ[R] B := f.restrict_scalars R end semiring end subalgebra namespace is_scalar_tower open subalgebra variables [comm_semiring R] [comm_semiring S] [comm_semiring A] variables [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] theorem adjoin_range_to_alg_hom (t : set A) : (algebra.adjoin (to_alg_hom R S A).range t).restrict_scalars R = (algebra.adjoin S t).restrict_scalars R := subalgebra.ext $ Ξ» z, show z ∈ subsemiring.closure (set.range (algebra_map (to_alg_hom R S A).range A) βˆͺ t : set A) ↔ z ∈ subsemiring.closure (set.range (algebra_map S A) βˆͺ t : set A), from suffices set.range (algebra_map (to_alg_hom R S A).range A) = set.range (algebra_map S A), by rw this, by { ext z, exact ⟨λ ⟨⟨x, y, h1⟩, h2⟩, ⟨y, h2 β–Έ h1⟩, Ξ» ⟨y, hy⟩, ⟨⟨z, y, hy⟩, rfl⟩⟩ } end is_scalar_tower